aboutsummaryrefslogtreecommitdiff
path: root/src/indicator-printers-menu.c
blob: 1548610271559f1e47913c7b650a3ef538c89a1f (plain)
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318

#include "indicator-printers-menu.h"

#include <cups/cups.h>


G_DEFINE_TYPE (IndicatorPrintersMenu, indicator_printers_menu, G_TYPE_OBJECT)


struct _IndicatorPrintersMenuPrivate
{
    DbusmenuMenuitem *root;
    GHashTable *printers;    /* printer name -> dbusmenuitem */
    CupsNotifier *cups_notifier;
};


enum {
    PROP_0,
    PROP_CUPS_NOTIFIER,
    NUM_PROPERTIES
};

GParamSpec *properties[NUM_PROPERTIES];


static void
dispose (GObject *object)
{
    IndicatorPrintersMenu *self = INDICATOR_PRINTERS_MENU (object);

    if (self->priv->printers) {
        g_hash_table_unref (self->priv->printers);
        self->priv->printers = NULL;
    }

    g_clear_object (&self->priv->root);
    g_clear_object (&self->priv->cups_notifier);

    G_OBJECT_CLASS (indicator_printers_menu_parent_class)->dispose (object);
}


void
set_property (GObject        *object,
              guint           property_id,
              const GValue   *value,
              GParamSpec     *pspec)
{
    IndicatorPrintersMenu *self = INDICATOR_PRINTERS_MENU (object);

    switch (property_id) {
        case PROP_CUPS_NOTIFIER:
            indicator_printers_menu_set_cups_notifier (self,
                                                       g_value_get_object (value));
            break;

        default:
            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
}


void
get_property (GObject        *object,
              guint           property_id,
              GValue         *value,
              GParamSpec     *pspec)
{
    IndicatorPrintersMenu *self = INDICATOR_PRINTERS_MENU (object);

    switch (property_id) {
        case PROP_CUPS_NOTIFIER:
            g_value_set_object (value,
                                indicator_printers_menu_get_cups_notifier (self));
            break;

        default:
            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
}


static void
indicator_printers_menu_class_init (IndicatorPrintersMenuClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);

    g_type_class_add_private (klass, sizeof (IndicatorPrintersMenuPrivate));

    object_class->dispose = dispose;
    object_class->get_property = get_property;
    object_class->set_property = set_property;

    properties[PROP_CUPS_NOTIFIER] = g_param_spec_object ("cups-notifier",
                                                          "Cups Notifier",
                                                          "A cups notifier object",
                                                          CUPS_TYPE_NOTIFIER,
                                                          G_PARAM_READWRITE);

    g_object_class_install_properties (object_class, NUM_PROPERTIES, properties);
}


static int
get_number_of_active_jobs (const gchar *printer)
{
    int njobs;
    cups_job_t *jobs;

    njobs = cupsGetJobs (&jobs, printer, 1, CUPS_WHICHJOBS_ACTIVE);
    cupsFreeJobs (njobs, jobs);

    return njobs;
}


static void
spawn_command_line_async_f (const gchar *fmt,
                            ...)
{
    va_list args;
    gchar *cmdline;
    GError *err = NULL;

    va_start (args, fmt);
    cmdline = g_strdup_vprintf (fmt, args);
    va_end (args);

    g_spawn_command_line_async (cmdline, &err);
    if (err) {
        g_warning ("Couldn't execute command `%s`: %s",
                   cmdline, err->message);
        g_error_free (err);
    }

    g_free (cmdline);
}


static void
on_printer_item_activated (DbusmenuMenuitem *menuitem,
                           guint timestamp,
                           gpointer user_data)
{
    const gchar *printer = user_data;

    spawn_command_line_async_f ("gnome-control-center printers show-printer %s",
                                printer);
}


static void
update_printer_menuitem (IndicatorPrintersMenu *self,
                         const char *printer,
                         int state,
                         int njobs)
{
    DbusmenuMenuitem *item;

    item = g_hash_table_lookup (self->priv->printers, printer);

    if (!item) {
        item = dbusmenu_menuitem_new ();
        dbusmenu_menuitem_property_set (item, "type", "indicator-item");
        dbusmenu_menuitem_property_set (item, "indicator-icon-name", "printer");
        dbusmenu_menuitem_property_set (item, "indicator-label", printer);
        g_signal_connect_data (item, "item-activated",
                               G_CALLBACK (on_printer_item_activated),
                               g_strdup (printer), (GClosureNotify) g_free, 0);

        dbusmenu_menuitem_child_append(self->priv->root, item);
        g_hash_table_insert (self->priv->printers, g_strdup (printer), item);
    }

    if (njobs == 0) {
        dbusmenu_menuitem_property_set_bool (item, "visible", FALSE);
        return;
    }

    dbusmenu_menuitem_property_set_bool (item, "visible", TRUE);

    switch (state) {
        case IPP_PRINTER_STOPPED:
            dbusmenu_menuitem_property_set (item, "indicator-right", "Paused");
            dbusmenu_menuitem_property_set_bool (item, "indicator-right-is-lozenge", FALSE);
            break;

        case IPP_PRINTER_PROCESSING: {
            gchar *jobstr = g_strdup_printf ("%d", njobs);
            dbusmenu_menuitem_property_set (item, "indicator-right", jobstr);
            dbusmenu_menuitem_property_set_bool (item, "indicator-right-is-lozenge", TRUE);
            g_free (jobstr);
            break;
        }
    }
}


static void
update_job (CupsNotifier *cups_notifier,
            const gchar *text,
            const gchar *printer_uri,
            const gchar *printer_name,
            guint printer_state,
            const gchar *printer_state_reasons,
            gboolean printer_is_accepting_jobs,
            guint job_id,
            guint job_state,
            const gchar *job_state_reasons,
            const gchar *job_name,
            guint job_impressions_completed,
            gpointer user_data)
{
    IndicatorPrintersMenu *self = INDICATOR_PRINTERS_MENU (user_data);

    update_printer_menuitem (self,
                             printer_name,
                             printer_state,
                             get_number_of_active_jobs (printer_name));
}


static void
on_printer_state_changed (CupsNotifier *object,
                          const gchar *text,
                          const gchar *printer_uri,
                          const gchar *printer_name,
                          guint printer_state,
                          const gchar *printer_state_reasons,
                          gboolean printer_is_accepting_jobs,
                          gpointer user_data)
{
    IndicatorPrintersMenu *self = INDICATOR_PRINTERS_MENU (user_data);

    update_printer_menuitem (self,
                             printer_name,
                             printer_state,
                             get_number_of_active_jobs (printer_name));
}


static void
indicator_printers_menu_init (IndicatorPrintersMenu *self)
{
    int ndests, i;
    cups_dest_t *dests;

    self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
                                              INDICATOR_TYPE_PRINTERS_MENU,
                                              IndicatorPrintersMenuPrivate);

    self->priv->root = dbusmenu_menuitem_new ();

    self->priv->printers = g_hash_table_new_full (g_str_hash,
                                                  g_str_equal,
                                                  g_free,
                                                  g_object_unref);

    /* create initial menu items */
    ndests = cupsGetDests (&dests);
    for (i = 0; i < ndests; i++) {
        int state = atoi (cupsGetOption ("printer-state",
                                         dests[i].num_options,
                                         dests[i].options));
        update_printer_menuitem (self,
                                 dests[i].name,
                                 state,
                                 get_number_of_active_jobs (dests[i].name));
    }
    cupsFreeDests (ndests, dests);
}


IndicatorPrintersMenu *
indicator_printers_menu_new (void)
{
    return g_object_new (INDICATOR_TYPE_PRINTERS_MENU, NULL);
}


DbusmenuMenuitem *
indicator_printers_menu_get_root (IndicatorPrintersMenu *self)
{
    return self->priv->root;
}


CupsNotifier *
indicator_printers_menu_get_cups_notifier (IndicatorPrintersMenu *self)
{
    return self->priv->cups_notifier;
}


void
indicator_printers_menu_set_cups_notifier (IndicatorPrintersMenu *self,
                                           CupsNotifier *cups_notifier)
{
    if (self->priv->cups_notifier) {
        g_object_disconnect (self->priv->cups_notifier,
                             "any-signal", update_job, self,
                             "any-signal", on_printer_state_changed, self,
                             NULL);
        g_clear_object (&self->priv->cups_notifier);
    }

    if (cups_notifier) {
        self->priv->cups_notifier = g_object_ref (cups_notifier);
        g_object_connect (self->priv->cups_notifier,
                          "signal::job-created", update_job, self,
                          "signal::job-state", update_job, self,
                          "signal::job-completed", update_job, self,
                          "signal::printer-state-changed", on_printer_state_changed, self,
                          NULL);
    }
}