summaryrefslogtreecommitdiffstats
path: root/panels/printers/pp-cups.c
blob: 96ace837ea773e34adcc0b5b6afddd2f43381727 (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
319
320
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright 2012  Red Hat, Inc,
 *
 * 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 2 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/>.
 *
 * Author: Marek Kasik <mkasik@redhat.com>
 */

#include "config.h"

#include "pp-cups.h"

#if (CUPS_VERSION_MAJOR > 1) || (CUPS_VERSION_MINOR > 5)
#define HAVE_CUPS_1_6 1
#endif

#ifndef HAVE_CUPS_1_6
#define ippGetInteger(attr, element) attr->values[element].integer
#define ippGetStatusCode(ipp) ipp->request.status.status_code
#endif

struct _PpCups
{
  GObject parent_instance;
};

G_DEFINE_TYPE (PpCups, pp_cups, G_TYPE_OBJECT);

static void
pp_cups_class_init (PpCupsClass *klass)
{
}

static void
pp_cups_init (PpCups *self)
{
}

PpCups *
pp_cups_new ()
{
  return g_object_new (PP_TYPE_CUPS, NULL);
}

static void
pp_cups_dests_free (PpCupsDests *dests)
{
  cupsFreeDests (dests->num_of_dests, dests->dests);
}

static void
_pp_cups_get_dests_thread (GTask        *task,
                           gpointer     *object,
                           gpointer      task_data,
                           GCancellable *cancellable)
{
  PpCupsDests *dests;

  dests = g_new0 (PpCupsDests, 1);
  dests->num_of_dests = cupsGetDests (&dests->dests);

  if (g_task_set_return_on_cancel (task, FALSE))
    {
      g_task_return_pointer (task, dests, (GDestroyNotify) pp_cups_dests_free);
    }
  else
    {
      pp_cups_dests_free (dests);
    }
}

void
pp_cups_get_dests_async (PpCups              *self,
                         GCancellable        *cancellable,
                         GAsyncReadyCallback  callback,
                         gpointer             user_data)
{
  GTask       *task;

  task = g_task_new (self, cancellable, callback, user_data);
  g_task_set_return_on_cancel (task, TRUE);
  g_task_run_in_thread (task, (GTaskThreadFunc) _pp_cups_get_dests_thread);
  g_object_unref (task);
}

PpCupsDests *
pp_cups_get_dests_finish (PpCups        *self,
                          GAsyncResult  *res,
                          GError       **error)
{
  g_return_val_if_fail (g_task_is_valid (res, self), NULL);

  return g_task_propagate_pointer (G_TASK (res), error);
}

static void
connection_test_thread (GTask        *task,
                        gpointer      source_object,
                        gpointer      task_data,
                        GCancellable *cancellable)
{
  http_t *http;

#ifdef HAVE_CUPS_HTTPCONNECT2
  http = httpConnect2 (cupsServer (), ippPort (), NULL, AF_UNSPEC,
                       cupsEncryption (), 1, 30000, NULL);
#else
  http = httpConnectEncrypt (cupsServer (), ippPort (), cupsEncryption ());
#endif
  httpClose (http);

  if (g_task_set_return_on_cancel (task, FALSE))
    {
      g_task_return_boolean (task, http != NULL);
    }
}

void
pp_cups_connection_test_async (PpCups              *self,
                               GCancellable        *cancellable,
                               GAsyncReadyCallback  callback,
                               gpointer             user_data)
{
  GTask *task;

  task = g_task_new (self, cancellable, callback, user_data);
  g_task_set_return_on_cancel (task, TRUE);
  g_task_run_in_thread (task, connection_test_thread);

  g_object_unref (task);
}

gboolean
pp_cups_connection_test_finish (PpCups         *self,
                                GAsyncResult   *result,
                                GError        **error)
{
  g_return_val_if_fail (g_task_is_valid (result, self), FALSE);

  return g_task_propagate_boolean (G_TASK (result), error);
}

/* Cancels subscription of given id */
static void
cancel_subscription_thread (GTask        *task,
                            gpointer      source_object,
                            gpointer      task_data,
                            GCancellable *cancellable)
{
  ipp_t *request;
  ipp_t *response = NULL;
  gint   id = GPOINTER_TO_INT (task_data);

  if (id >= 0)
    {
      request = ippNewRequest (IPP_CANCEL_SUBSCRIPTION);
      ippAddString (request, IPP_TAG_OPERATION, IPP_TAG_URI,
                    "printer-uri", NULL, "/");
      ippAddString (request, IPP_TAG_OPERATION, IPP_TAG_NAME,
                    "requesting-user-name", NULL, cupsUser ());
      ippAddInteger (request, IPP_TAG_OPERATION, IPP_TAG_INTEGER,
                     "notify-subscription-id", id);
      response = cupsDoRequest (CUPS_HTTP_DEFAULT, request, "/");
    }

  g_task_return_boolean (task, response != NULL && ippGetStatusCode (response) <= IPP_OK);

  ippDelete (response);
}

void
pp_cups_cancel_subscription_async (PpCups              *self,
                                   gint                 subscription_id,
                                   GAsyncReadyCallback  callback,
                                   gpointer             user_data)
{
  GTask *task;

  task = g_task_new (self, NULL, callback, user_data);
  g_task_set_task_data (task, GINT_TO_POINTER (subscription_id), NULL);
  g_task_run_in_thread (task, cancel_subscription_thread);

  g_object_unref (task);
}

gboolean
pp_cups_cancel_subscription_finish (PpCups       *self,
                                    GAsyncResult *result)
{
  g_return_val_if_fail (g_task_is_valid (result, self), FALSE);

  return g_task_propagate_boolean (G_TASK (result), NULL);
}

typedef struct {
  gint id;
  gchar **events;
  int lease_duration;
} CRSData;

static void
crs_data_free (CRSData *data)
{
  g_strfreev (data->events);
  g_slice_free (CRSData, data);
}

static void
renew_subscription_thread (GTask        *task,
                           gpointer      source_object,
                           gpointer      task_data,
                           GCancellable *cancellable)
{
  ipp_attribute_t *attr = NULL;
  CRSData         *subscription_data = task_data;
  ipp_t           *request;
  ipp_t           *response = NULL;
  gint             result = -1;

  if (g_cancellable_is_cancelled (cancellable))
    return;

  if (subscription_data->id > 0)
    {
      request = ippNewRequest (IPP_RENEW_SUBSCRIPTION);
      ippAddString (request, IPP_TAG_OPERATION, IPP_TAG_URI,
                   "printer-uri", NULL, "/");
      ippAddString (request, IPP_TAG_OPERATION, IPP_TAG_NAME,
                   "requesting-user-name", NULL, cupsUser ());
      ippAddInteger (request, IPP_TAG_OPERATION, IPP_TAG_INTEGER,
                    "notify-subscription-id", subscription_data->id);
      ippAddInteger (request, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
                    "notify-lease-duration", subscription_data->lease_duration);
      response = cupsDoRequest (CUPS_HTTP_DEFAULT, request, "/");
      if (response != NULL && ippGetStatusCode (response) <= IPP_OK_CONFLICT)
        {
          if ((attr = ippFindAttribute (response, "notify-lease-duration", IPP_TAG_INTEGER)) == NULL)
            g_debug ("No notify-lease-duration in response!\n");
          else if (ippGetInteger (attr, 0) == subscription_data->lease_duration)
            result = subscription_data->id;
        }
    }

  if (result < 0)
    {
      request = ippNewRequest (IPP_CREATE_PRINTER_SUBSCRIPTION);
      ippAddString (request, IPP_TAG_OPERATION, IPP_TAG_URI,
                   "printer-uri", NULL, "/");
      ippAddString (request, IPP_TAG_OPERATION, IPP_TAG_NAME,
                   "requesting-user-name", NULL, cupsUser ());
      ippAddStrings (request, IPP_TAG_SUBSCRIPTION, IPP_TAG_KEYWORD,
                    "notify-events", g_strv_length (subscription_data->events), NULL,
                     (const char * const *) subscription_data->events);
      ippAddString (request, IPP_TAG_SUBSCRIPTION, IPP_TAG_KEYWORD,
                   "notify-pull-method", NULL, "ippget");
      ippAddString (request, IPP_TAG_SUBSCRIPTION, IPP_TAG_URI,
                   "notify-recipient-uri", NULL, "dbus://");
      ippAddInteger (request, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
                    "notify-lease-duration", subscription_data->lease_duration);
      response = cupsDoRequest (CUPS_HTTP_DEFAULT, request, "/");

      if (response != NULL && ippGetStatusCode (response) <= IPP_OK_CONFLICT)
        {
          if ((attr = ippFindAttribute (response, "notify-subscription-id", IPP_TAG_INTEGER)) == NULL)
            g_debug ("No notify-subscription-id in response!\n");
          else
            result = ippGetInteger (attr, 0);
        }
    }

  ippDelete (response);

  g_task_return_int (task, result);
}

void
pp_cups_renew_subscription_async  (PpCups               *self,
                                   gint                  subscription_id,
                                   gchar               **events,
                                   gint                  lease_duration,
                                   GCancellable         *cancellable,
                                   GAsyncReadyCallback   callback,
                                   gpointer              user_data)
{
  CRSData *subscription_data;
  GTask   *task;

  subscription_data = g_slice_new (CRSData);
  subscription_data->id = subscription_id;
  subscription_data->events = g_strdupv (events);
  subscription_data->lease_duration = lease_duration;

  task = g_task_new (self, cancellable, callback, user_data);
  g_task_set_task_data (task, subscription_data, (GDestroyNotify) crs_data_free);
  g_task_run_in_thread (task, renew_subscription_thread);

  g_object_unref (task);
}

/* Returns id of renewed subscription or new id */
gint
pp_cups_renew_subscription_finish (PpCups       *self,
                                   GAsyncResult *result)
{
  g_return_val_if_fail (g_task_is_valid (result, self), FALSE);

  return g_task_propagate_int (G_TASK (result), NULL);
}