File: font-manager-menu-provider.c

package info (click to toggle)
font-manager 0.9.4-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 17,684 kB
  • sloc: ansic: 13,202; perl: 684; xml: 494; python: 321; makefile: 123
file content (250 lines) | stat: -rw-r--r-- 8,260 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/* font-manager-menu-provider.c
 *
 * Copyright (C) 2018-2025 Jerry Casiano
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.
 *
 * If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>.
*/

#include <gio/gio.h>

#include "extension-common.h"
#include "font-manager-menu-provider.h"

struct _FontManagerMenuProvider
{
    GObject parent_instance;

    guint watch_id;
    gboolean active;
    gchar *uri;

    GDBusConnection *connection;
};

static void font_manager_menu_provider_interface_init (NemoMenuProviderIface *iface);

G_DEFINE_DYNAMIC_TYPE_EXTENDED (FontManagerMenuProvider,
                                font_manager_menu_provider,
                                G_TYPE_OBJECT,
                                0,
                                G_IMPLEMENT_INTERFACE_DYNAMIC (NEMO_TYPE_MENU_PROVIDER,
                                                               font_manager_menu_provider_interface_init))

static gboolean
nemo_file_info_is_font_file (NemoFileInfo *fileinfo)
{
    for (gint i = 0; i < N_MIMETYPES; i++)
        if (nemo_file_info_is_mime_type(fileinfo, MIMETYPES[i]))
            return TRUE;
    return FALSE;
}

static gboolean
file_list_contains_font_files (GList *nemo_file_info_list)
{
    for (GList *iter = nemo_file_info_list; iter != NULL; iter = iter->next)
        if (nemo_file_info_is_font_file(iter->data))
            return TRUE;
    return FALSE;
}

static void
install_task (GTask *task,
              FontManagerMenuProvider *self,
              NemoMenuItem *item,
              GCancellable *cancellable)
{
    GList *filelist = g_object_get_data(G_OBJECT(item), "filelist");
    g_return_if_fail(filelist != NULL);
    for (GList *iter = filelist; iter != NULL; iter = iter->next) {
        if (!nemo_file_info_is_font_file(iter->data))
            continue;
        g_autoptr(GFile) file = nemo_file_info_get_location(iter->data);
        g_autofree gchar *dir = font_manager_get_user_font_directory();
        g_autoptr(GFile) directory = g_file_new_for_path(dir);
        font_manager_install_file(file, directory, NULL);
    }
    return;
}

static void
on_install_selected (FontManagerMenuProvider *self, NemoMenuItem *item)
{
    g_autoptr(GTask) task = g_task_new(self, NULL, NULL, NULL);
    g_task_set_task_data(task, item, NULL);
    g_task_run_in_thread(task, (GTaskThreadFunc) install_task);
    return;
}

static GList *
font_manager_menu_provider_get_file_items (NemoMenuProvider *provider,
                                           G_GNUC_UNUSED GtkWidget *widget,
                                           GList *filelist)
{
    if (filelist == NULL)
        return NULL;

    FontManagerMenuProvider *self = FONT_MANAGER_MENU_PROVIDER(provider);

    GList *items = NULL;

    gboolean single_selection = (filelist->next == NULL);

    if (single_selection) {

        NemoFileInfo *fileinfo = g_list_nth_data(filelist, 0);

        if (!nemo_file_info_is_font_file(fileinfo))
            goto menu;

        if (!self->active)
            goto menu;

        g_autofree gchar *uri = nemo_file_info_get_activation_uri(fileinfo);

        if (g_strcmp0(self->uri, uri) == 0)
            goto menu;

        if (self->connection && !g_dbus_connection_is_closed(self->connection)) {

            g_dbus_connection_call(self->connection,
                                   FONT_VIEWER_BUS_ID,
                                   FONT_VIEWER_BUS_PATH,
                                   FONT_VIEWER_BUS_ID,
                                   "ShowUri",
                                   g_variant_new("(s)", uri),
                                   NULL,
                                   G_DBUS_CALL_FLAGS_NONE,
                                   -1,
                                   NULL,
                                   NULL,
                                   NULL);

        }

        g_free(self->uri);
        self->uri = g_strdup(uri);

    }

menu:

    if (file_list_contains_font_files(filelist)) {
        NemoMenuItem *install = nemo_menu_item_new("FontManager:install",
                                                    _("Install"),
                                                    single_selection ?
                                                    _("Install the selected font file") :
                                                    _("Install the selected font files"),
                                                    NULL);
        g_object_set_data_full(G_OBJECT(install), "filelist",
                                nemo_file_info_list_copy(filelist),
                                (GDestroyNotify) nemo_file_info_list_free);
        g_signal_connect_swapped(install, "activate", G_CALLBACK(on_install_selected), self);
        items = g_list_append(items, install);
    }

    return items;
}

static GList *
font_manager_menu_provider_get_background_items (G_GNUC_UNUSED NemoMenuProvider *provider,
                                                 G_GNUC_UNUSED GtkWidget *widget,
                                                 G_GNUC_UNUSED NemoFileInfo *current_folder)
{
    return NULL;
}

static void
font_manager_menu_provider_finalize (GObject *gobject)
{
    FontManagerMenuProvider *self = FONT_MANAGER_MENU_PROVIDER(gobject);
    g_bus_unwatch_name(self->watch_id);
    g_clear_pointer(&self->uri, g_free);
    g_clear_object(&self->connection);
    G_OBJECT_CLASS(font_manager_menu_provider_parent_class)->finalize(gobject);
    return;
}

static void
font_manager_menu_provider_class_finalize (G_GNUC_UNUSED FontManagerMenuProviderClass *klass)
{
    return;
}

static void
font_manager_menu_provider_interface_init (NemoMenuProviderIface *iface)
{
    iface->get_file_items = font_manager_menu_provider_get_file_items;
    iface->get_background_items = font_manager_menu_provider_get_background_items;
    return;
}

static void
font_manager_menu_provider_class_init (FontManagerMenuProviderClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS(klass);
    object_class->finalize = font_manager_menu_provider_finalize;
    return;
}

static void
font_viewer_active_callback (GDBusConnection *connection,
                             G_GNUC_UNUSED const gchar *name,
                             G_GNUC_UNUSED const gchar *name_owner,
                             gpointer user_data)
{
    FontManagerMenuProvider *self = FONT_MANAGER_MENU_PROVIDER(user_data);
    self->active = TRUE;
    g_clear_pointer(&self->uri, g_free);
    g_set_object(&self->connection, connection);
    return;
}

static void
font_viewer_inactive_callback (G_GNUC_UNUSED GDBusConnection *connection,
                               G_GNUC_UNUSED const gchar *name,
                               gpointer user_data)
{
    FontManagerMenuProvider *self = FONT_MANAGER_MENU_PROVIDER(user_data);
    self->active = FALSE;
    g_clear_pointer(&self->uri, g_free);
    g_clear_object(&self->connection);
    return;
}

static void
font_manager_menu_provider_init (FontManagerMenuProvider *self)
{
    g_return_if_fail(self != NULL);
    self->active = FALSE;
    self->uri = NULL;
    self->watch_id = g_bus_watch_name(G_BUS_TYPE_SESSION,
                                      FONT_VIEWER_BUS_ID,
                                      G_BUS_NAME_WATCHER_FLAGS_NONE,
                                      (GBusNameAppearedCallback) font_viewer_active_callback,
                                      (GBusNameVanishedCallback) font_viewer_inactive_callback,
                                      self,
                                      NULL);
    return;
}

void
font_manager_menu_provider_load (GTypeModule *module)
{
    font_manager_menu_provider_register_type(module);
    return;
}