aboutsummaryrefslogtreecommitdiff
path: root/src/indicator-printers-service.c
blob: f7878a7ca2a3163b27f6c641e2c021e2166c4265 (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
/*
 * Copyright 2012 Canonical Ltd.
 *
 * Authors: Lars Uebernickel <lars.uebernickel@canonical.com>
 *
 * 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 <libindicator/indicator-service.h>
#include <libdbusmenu-glib/dbusmenu-glib.h>
#include <gtk/gtk.h>
#include <cups/cups.h>
#include "dbus-names.h"
#include "config.h"

#include "cups-notifier.h"
#include "indicator-printers-menu.h"
#include "indicator-printer-state-notifier.h"

#define NOTIFY_LEASE_DURATION (24 * 60 * 60)


static int
create_subscription ()
{
    ipp_t *req;
    ipp_t *resp;
    ipp_attribute_t *attr;
    int id = 0;

    req = ippNewRequest (IPP_CREATE_PRINTER_SUBSCRIPTION);
    ippAddString (req, IPP_TAG_OPERATION, IPP_TAG_URI,
                  "printer-uri", NULL, "/");
    ippAddString (req, IPP_TAG_SUBSCRIPTION, IPP_TAG_KEYWORD,
                  "notify-events", NULL, "all");
    ippAddString (req, IPP_TAG_SUBSCRIPTION, IPP_TAG_URI,
                  "notify-recipient-uri", NULL, "dbus://");
    ippAddInteger (req, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
                   "notify-lease-duration", NOTIFY_LEASE_DURATION);

    resp = cupsDoRequest (CUPS_HTTP_DEFAULT, req, "/");
    if (!resp || cupsLastError() != IPP_OK) {
        g_warning ("Error subscribing to CUPS notifications: %s\n",
                   cupsLastErrorString ());
        return 0;
    }

    attr = ippFindAttribute (resp, "notify-subscription-id", IPP_TAG_INTEGER);
    if (attr)
        id = ippGetInteger (attr, 0);
    else
        g_warning ("ipp-create-printer-subscription response doesn't contain "
                   "subscription id.\n");

    ippDelete (resp);
    return id;
}


static gboolean
renew_subscription (int id)
{
    ipp_t *req;
    ipp_t *resp;

    req = ippNewRequest (IPP_RENEW_SUBSCRIPTION);
    ippAddInteger (req, IPP_TAG_OPERATION, IPP_TAG_INTEGER,
                   "notify-subscription-id", id);
    ippAddString (req, IPP_TAG_OPERATION, IPP_TAG_URI,
                  "printer-uri", NULL, "/");
    ippAddString (req, IPP_TAG_SUBSCRIPTION, IPP_TAG_URI,
                  "notify-recipient-uri", NULL, "dbus://");
    ippAddInteger (req, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
                   "notify-lease-duration", NOTIFY_LEASE_DURATION);

    resp = cupsDoRequest (CUPS_HTTP_DEFAULT, req, "/");
    if (!resp || cupsLastError() != IPP_OK) {
        g_warning ("Error renewing CUPS subscription %d: %s\n",
                   id, cupsLastErrorString ());
        return FALSE;
    }

    ippDelete (resp);
    return TRUE;
}


static gboolean
renew_subscription_timeout (gpointer userdata)
{
    int *subscription_id = userdata;

    if (*subscription_id <= 0 || !renew_subscription (*subscription_id))
        *subscription_id = create_subscription ();

    return TRUE;
}


void
cancel_subscription (int id)
{
    ipp_t *req;
    ipp_t *resp;

    if (id <= 0)
        return;

    req = ippNewRequest (IPP_CANCEL_SUBSCRIPTION);
    ippAddString (req, IPP_TAG_OPERATION, IPP_TAG_URI,
                  "printer-uri", NULL, "/");
    ippAddInteger (req, IPP_TAG_OPERATION, IPP_TAG_INTEGER,
                   "notify-subscription-id", id);

    resp = cupsDoRequest (CUPS_HTTP_DEFAULT, req, "/");
    if (!resp || cupsLastError() != IPP_OK) {
        g_warning ("Error subscribing to CUPS notifications: %s\n",
                   cupsLastErrorString ());
        return;
    }

    ippDelete (resp);
}

int main (int argc, char *argv[])
{
    /* Init i18n */
    setlocale (LC_ALL, "");
    bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
    bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
    textdomain (GETTEXT_PACKAGE);

    DbusmenuServer *menuserver;
    CupsNotifier *cups_notifier;
    IndicatorPrintersMenu *menu;
    IndicatorPrinterStateNotifier *state_notifier;
    GError *error = NULL;
    int subscription_id;

    gtk_init (&argc, &argv);

    subscription_id = create_subscription ();
    g_timeout_add_seconds (NOTIFY_LEASE_DURATION - 60,
                           renew_subscription_timeout,
                           &subscription_id);

    cups_notifier = cups_notifier_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
                                                          0,
                                                          NULL,
                                                          CUPS_DBUS_PATH,
                                                          NULL,
                                                          &error);
    if (error) {
        g_warning ("Error creating cups notify handler: %s", error->message);
        g_error_free (error);
        return 1;
    }

    menu = g_object_new (INDICATOR_TYPE_PRINTERS_MENU,
                         "cups-notifier", cups_notifier,
                         NULL);

    menuserver = dbusmenu_server_new (INDICATOR_PRINTERS_DBUS_OBJECT_PATH);
    dbusmenu_server_set_root (menuserver,
                              indicator_printers_menu_get_root (menu));

    state_notifier = g_object_new (INDICATOR_TYPE_PRINTER_STATE_NOTIFIER,
                                   "cups-notifier", cups_notifier,
                                   NULL);

    gtk_main ();

    g_object_unref (menu);
    g_object_unref (menuserver);
    g_object_unref (state_notifier);
    g_object_unref (cups_notifier);
    return 0;
}