aboutsummaryrefslogtreecommitdiff
path: root/src/indicator-printer-state-notifier.c
blob: 12f77c64edf924db569ff5a0f66f235d90caa6af (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
/*
 * Copyright 2012 Canonical Ltd.
 * Copyright 2022 Robert Tari
 *
 * Authors: Lars Uebernickel <lars.uebernickel@canonical.com>
 *          Robert Tari <robert@tari.in>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as published
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, 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/>.
 */

#include "indicator-printer-state-notifier.h"
#include <ayatana/common/utils.h>
#include <glib/gi18n.h>
#include <cups/cups.h>
#include <string.h>
#include <stdarg.h>

#include "cups-notifier.h"
#include "spawn-printer-settings.h"

struct _IndicatorPrinterStateNotifierPrivate
{
    CupsNotifier *cups_notifier;

    /* printer states that were already notified about in this session */
    GHashTable *notified_printer_states;

    /* state-reason -> user visible string with a %s for printer name */
    GHashTable *printer_alerts;
};

G_DEFINE_TYPE_WITH_PRIVATE(IndicatorPrinterStateNotifier, indicator_printer_state_notifier, G_TYPE_OBJECT)

enum {
    PROP_0,
    PROP_CUPS_NOTIFIER,
    NUM_PROPERTIES
};

static GParamSpec *properties[NUM_PROPERTIES];


static void
g_hash_table_insert_many (GHashTable *hash_table,
                          gpointer key,
                          ...)
{
    va_list args;

    va_start (args, key);
    while (key) {
        gpointer value = va_arg (args, gpointer);
        g_hash_table_insert (hash_table, key, value);
        key = va_arg (args, gpointer);
    }
    va_end (args);
}


/* returns a list of strings that are in a but not in b; does not copy the
 * strings */
static GList *
g_strv_diff (gchar **a,
             gchar **b)
{
    GList *result = NULL;
    gchar **p;

    if (!a)
        return NULL;

    for (p = a; *p; p++) {
        if (!g_strv_contains ((const gchar * const *) b, *p))
            result = g_list_prepend (result, *p);
    }

    return g_list_reverse (result);
}


void
show_alert_box (const gchar *printer,
                const gchar *reason,
                int njobs)
{
    gchar *primary_text;
    gchar *secondary_text;

    primary_text = g_strdup_printf (reason, printer);

    secondary_text = g_strdup_printf (ngettext(
                   "You have %d job queued to print on this printer.",
                   "You have %d jobs queued to print on this printer.", njobs),
                   njobs);

    gchar *sText = g_strdup_printf("<b>%s</b>\n\n%s", primary_text, secondary_text);
    g_free (primary_text);
    g_free (secondary_text);

    ayatana_common_utils_zenity_warning ("printer", _("Printing Problem"), sText);
    g_free (sText);

    spawn_printer_settings ();
}


static void
on_printer_state_changed (CupsNotifier *object,
                          const gchar *text,
                          const gchar *printer_uri,
                          const gchar *printer,
                          guint printer_state,
                          const gchar *printer_state_reasons,
                          gboolean printer_is_accepting_jobs,
                          gpointer user_data)
{
    IndicatorPrinterStateNotifierPrivate *priv = INDICATOR_PRINTER_STATE_NOTIFIER (user_data)->priv;
    int njobs;
    cups_job_t *jobs;
    gchar **state_reasons, **already_notified;
    GList *new_state_reasons, *it;

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

    /* don't show any events if the current user does not have jobs queued on
     * that printer or this printer is unknown to CUPS */
    if (njobs <= 0)
        return;

    state_reasons = g_strsplit (printer_state_reasons, " ", 0);
    already_notified = g_hash_table_lookup (priv->notified_printer_states,
                                            printer);

    new_state_reasons = g_strv_diff (state_reasons, already_notified);

    for (it = new_state_reasons; it; it = g_list_next (new_state_reasons)) {
        const gchar *reason_text = g_hash_table_lookup (priv->printer_alerts,
                                                        it->data);
        if (reason_text)
            show_alert_box (printer, reason_text, njobs);
    }

    g_list_free (new_state_reasons);

    g_hash_table_replace (priv->notified_printer_states,
                          g_strdup (printer),
                          state_reasons);
}


static void
get_property (GObject    *object,
              guint       property_id,
              GValue     *value,
              GParamSpec *pspec)
{
    IndicatorPrinterStateNotifier *self = INDICATOR_PRINTER_STATE_NOTIFIER (object);

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

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


static void
set_property (GObject      *object,
              guint         property_id,
              const GValue *value,
              GParamSpec   *pspec)
{
    IndicatorPrinterStateNotifier *self = INDICATOR_PRINTER_STATE_NOTIFIER (object);

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

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


static void
dispose (GObject *object)
{
    IndicatorPrinterStateNotifier *self = INDICATOR_PRINTER_STATE_NOTIFIER (object);

    if (self->priv->notified_printer_states) {
        g_hash_table_unref (self->priv->notified_printer_states);
        self->priv->notified_printer_states = NULL;
    }
    if (self->priv->printer_alerts) {
        g_hash_table_unref (self->priv->printer_alerts);
        self->priv->printer_alerts = NULL;
    }
    g_clear_object (&self->priv->cups_notifier);

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


static void
finalize (GObject *object)
{
    G_OBJECT_CLASS (indicator_printer_state_notifier_parent_class)->finalize (object);
}


static void
indicator_printer_state_notifier_class_init (IndicatorPrinterStateNotifierClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);

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

    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 void
indicator_printer_state_notifier_init (IndicatorPrinterStateNotifier *self)
{
    IndicatorPrinterStateNotifierPrivate *priv;

    priv = indicator_printer_state_notifier_get_instance_private(self);
    self->priv = priv;

    priv->notified_printer_states = g_hash_table_new_full (g_str_hash,
                                                           g_str_equal,
                                                           g_free,
                                                           (GDestroyNotify)g_strfreev);

    priv->printer_alerts = g_hash_table_new (g_str_hash, g_str_equal);
    g_hash_table_insert_many ( priv->printer_alerts,
                               "media-low", _("The printer “%s” is low on paper."),
                               "media-empty", _("The printer “%s” is out of paper."),
                               "toner-low", _("The printer “%s” is low on toner."),
                               "toner-empty", _("The printer “%s” is out of toner."),
                               "cover-open", _("A cover is open on the printer “%s”."),
                               "door-open", _("A door is open on the printer “%s”."),
                               "cups-missing-filter", _("The printer “%s” can’t be used, because required software is missing."),
                               "offline", _("The printer “%s” is currently off-line."),
                               NULL);
}


CupsNotifier *
indicator_printer_state_notifier_get_cups_notifier (IndicatorPrinterStateNotifier *self)
{
    return self->priv->cups_notifier;
}


void
indicator_printer_state_notifier_set_cups_notifier (IndicatorPrinterStateNotifier *self,
                                                    CupsNotifier *cups_notifier)
{
    if (self->priv->cups_notifier) {
        g_signal_handlers_disconnect_by_func (self->priv->cups_notifier,
                                              on_printer_state_changed,
                                              self);
        g_clear_object (&self->priv->cups_notifier);
    }

    if (cups_notifier) {
        self->priv->cups_notifier = g_object_ref (cups_notifier);
        g_signal_connect (cups_notifier, "printer-state-changed",
                          G_CALLBACK (on_printer_state_changed), self);
    }
}