GCC Code Coverage Report


Directory: ./
File: panels/printers/pp-cups.c
Date: 2024-05-04 07:58:27
Exec Total Coverage
Lines: 0 115 0.0%
Functions: 0 20 0.0%
Branches: 0 45 0.0%

Line Branch Exec Source
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
2 *
3 * Copyright 2012 Red Hat, Inc,
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 *
18 * Author: Marek Kasik <mkasik@redhat.com>
19 */
20
21 #include "config.h"
22
23 #include "pp-cups.h"
24
25 #if (CUPS_VERSION_MAJOR > 1) || (CUPS_VERSION_MINOR > 5)
26 #define HAVE_CUPS_1_6 1
27 #endif
28
29 #ifndef HAVE_CUPS_1_6
30 #define ippGetInteger(attr, element) attr->values[element].integer
31 #define ippGetStatusCode(ipp) ipp->request.status.status_code
32 #endif
33
34 struct _PpCups
35 {
36 GObject parent_instance;
37 };
38
39 G_DEFINE_TYPE (PpCups, pp_cups, G_TYPE_OBJECT);
40
41 static void
42 pp_cups_class_init (PpCupsClass *klass)
43 {
44 }
45
46 static void
47 pp_cups_init (PpCups *self)
48 {
49 }
50
51 PpCups *
52 pp_cups_new ()
53 {
54 return g_object_new (PP_TYPE_CUPS, NULL);
55 }
56
57 static void
58 pp_cups_dests_free (PpCupsDests *dests)
59 {
60 cupsFreeDests (dests->num_of_dests, dests->dests);
61 }
62
63 static void
64 _pp_cups_get_dests_thread (GTask *task,
65 gpointer *object,
66 gpointer task_data,
67 GCancellable *cancellable)
68 {
69 PpCupsDests *dests;
70
71 dests = g_new0 (PpCupsDests, 1);
72 dests->num_of_dests = cupsGetDests (&dests->dests);
73
74 if (g_task_set_return_on_cancel (task, FALSE))
75 {
76 g_task_return_pointer (task, dests, (GDestroyNotify) pp_cups_dests_free);
77 }
78 else
79 {
80 pp_cups_dests_free (dests);
81 }
82 }
83
84 void
85 pp_cups_get_dests_async (PpCups *self,
86 GCancellable *cancellable,
87 GAsyncReadyCallback callback,
88 gpointer user_data)
89 {
90 g_autoptr(GTask) task = NULL;
91
92 task = g_task_new (self, cancellable, callback, user_data);
93 g_task_set_return_on_cancel (task, TRUE);
94 g_task_run_in_thread (task, (GTaskThreadFunc) _pp_cups_get_dests_thread);
95 }
96
97 PpCupsDests *
98 pp_cups_get_dests_finish (PpCups *self,
99 GAsyncResult *res,
100 GError **error)
101 {
102 g_return_val_if_fail (g_task_is_valid (res, self), NULL);
103
104 return g_task_propagate_pointer (G_TASK (res), error);
105 }
106
107 static void
108 connection_test_thread (GTask *task,
109 gpointer source_object,
110 gpointer task_data,
111 GCancellable *cancellable)
112 {
113 http_t *http;
114
115 #ifdef HAVE_CUPS_HTTPCONNECT2
116 http = httpConnect2 (cupsServer (), ippPort (), NULL, AF_UNSPEC,
117 cupsEncryption (), 1, 30000, NULL);
118 #else
119 http = httpConnectEncrypt (cupsServer (), ippPort (), cupsEncryption ());
120 #endif
121 httpClose (http);
122
123 if (g_task_set_return_on_cancel (task, FALSE))
124 {
125 g_task_return_boolean (task, http != NULL);
126 }
127 }
128
129 void
130 pp_cups_connection_test_async (PpCups *self,
131 GCancellable *cancellable,
132 GAsyncReadyCallback callback,
133 gpointer user_data)
134 {
135 g_autoptr(GTask) task = NULL;
136
137 task = g_task_new (self, cancellable, callback, user_data);
138 g_task_set_return_on_cancel (task, TRUE);
139 g_task_run_in_thread (task, connection_test_thread);
140 }
141
142 gboolean
143 pp_cups_connection_test_finish (PpCups *self,
144 GAsyncResult *result,
145 GError **error)
146 {
147 g_return_val_if_fail (g_task_is_valid (result, self), FALSE);
148
149 return g_task_propagate_boolean (G_TASK (result), error);
150 }
151
152 /* Cancels subscription of given id */
153 static void
154 cancel_subscription_thread (GTask *task,
155 gpointer source_object,
156 gpointer task_data,
157 GCancellable *cancellable)
158 {
159 ipp_t *request;
160 ipp_t *response = NULL;
161 gint id = GPOINTER_TO_INT (task_data);
162
163 if (id >= 0)
164 {
165 request = ippNewRequest (IPP_CANCEL_SUBSCRIPTION);
166 ippAddString (request, IPP_TAG_OPERATION, IPP_TAG_URI,
167 "printer-uri", NULL, "/");
168 ippAddString (request, IPP_TAG_OPERATION, IPP_TAG_NAME,
169 "requesting-user-name", NULL, cupsUser ());
170 ippAddInteger (request, IPP_TAG_OPERATION, IPP_TAG_INTEGER,
171 "notify-subscription-id", id);
172 response = cupsDoRequest (CUPS_HTTP_DEFAULT, request, "/");
173 }
174
175 g_task_return_boolean (task, response != NULL && ippGetStatusCode (response) <= IPP_OK);
176
177 ippDelete (response);
178 }
179
180 void
181 pp_cups_cancel_subscription_async (PpCups *self,
182 gint subscription_id,
183 GAsyncReadyCallback callback,
184 gpointer user_data)
185 {
186 g_autoptr(GTask) task = NULL;
187
188 task = g_task_new (self, NULL, callback, user_data);
189 g_task_set_task_data (task, GINT_TO_POINTER (subscription_id), NULL);
190 g_task_run_in_thread (task, cancel_subscription_thread);
191 }
192
193 gboolean
194 pp_cups_cancel_subscription_finish (PpCups *self,
195 GAsyncResult *result)
196 {
197 g_return_val_if_fail (g_task_is_valid (result, self), FALSE);
198
199 return g_task_propagate_boolean (G_TASK (result), NULL);
200 }
201
202 typedef struct {
203 gint id;
204 gchar **events;
205 int lease_duration;
206 } CRSData;
207
208 static void
209 crs_data_free (CRSData *data)
210 {
211 g_strfreev (data->events);
212 g_slice_free (CRSData, data);
213 }
214
215 static void
216 renew_subscription_thread (GTask *task,
217 gpointer source_object,
218 gpointer task_data,
219 GCancellable *cancellable)
220 {
221 ipp_attribute_t *attr = NULL;
222 CRSData *subscription_data = task_data;
223 ipp_t *request;
224 ipp_t *response = NULL;
225 gint result = -1;
226
227 if (g_cancellable_is_cancelled (cancellable))
228 return;
229
230 if (subscription_data->id > 0)
231 {
232 request = ippNewRequest (IPP_RENEW_SUBSCRIPTION);
233 ippAddString (request, IPP_TAG_OPERATION, IPP_TAG_URI,
234 "printer-uri", NULL, "/");
235 ippAddString (request, IPP_TAG_OPERATION, IPP_TAG_NAME,
236 "requesting-user-name", NULL, cupsUser ());
237 ippAddInteger (request, IPP_TAG_OPERATION, IPP_TAG_INTEGER,
238 "notify-subscription-id", subscription_data->id);
239 ippAddInteger (request, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
240 "notify-lease-duration", subscription_data->lease_duration);
241 response = cupsDoRequest (CUPS_HTTP_DEFAULT, request, "/");
242 if (response != NULL && ippGetStatusCode (response) <= IPP_OK_CONFLICT)
243 {
244 if ((attr = ippFindAttribute (response, "notify-lease-duration", IPP_TAG_INTEGER)) == NULL)
245 g_debug ("No notify-lease-duration in response!\n");
246 else if (ippGetInteger (attr, 0) == subscription_data->lease_duration)
247 result = subscription_data->id;
248 }
249 }
250
251 if (result < 0)
252 {
253 request = ippNewRequest (IPP_CREATE_PRINTER_SUBSCRIPTION);
254 ippAddString (request, IPP_TAG_OPERATION, IPP_TAG_URI,
255 "printer-uri", NULL, "/");
256 ippAddString (request, IPP_TAG_OPERATION, IPP_TAG_NAME,
257 "requesting-user-name", NULL, cupsUser ());
258 ippAddStrings (request, IPP_TAG_SUBSCRIPTION, IPP_TAG_KEYWORD,
259 "notify-events", g_strv_length (subscription_data->events), NULL,
260 (const char * const *) subscription_data->events);
261 ippAddString (request, IPP_TAG_SUBSCRIPTION, IPP_TAG_KEYWORD,
262 "notify-pull-method", NULL, "ippget");
263 ippAddString (request, IPP_TAG_SUBSCRIPTION, IPP_TAG_URI,
264 "notify-recipient-uri", NULL, "dbus://");
265 ippAddInteger (request, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
266 "notify-lease-duration", subscription_data->lease_duration);
267 response = cupsDoRequest (CUPS_HTTP_DEFAULT, request, "/");
268
269 if (response != NULL && ippGetStatusCode (response) <= IPP_OK_CONFLICT)
270 {
271 if ((attr = ippFindAttribute (response, "notify-subscription-id", IPP_TAG_INTEGER)) == NULL)
272 g_debug ("No notify-subscription-id in response!\n");
273 else
274 result = ippGetInteger (attr, 0);
275 }
276 }
277
278 ippDelete (response);
279
280 g_task_return_int (task, result);
281 }
282
283 void
284 pp_cups_renew_subscription_async (PpCups *self,
285 gint subscription_id,
286 gchar **events,
287 gint lease_duration,
288 GCancellable *cancellable,
289 GAsyncReadyCallback callback,
290 gpointer user_data)
291 {
292 CRSData *subscription_data;
293 g_autoptr(GTask) task = NULL;
294
295 subscription_data = g_slice_new (CRSData);
296 subscription_data->id = subscription_id;
297 subscription_data->events = g_strdupv (events);
298 subscription_data->lease_duration = lease_duration;
299
300 task = g_task_new (self, cancellable, callback, user_data);
301 g_task_set_task_data (task, subscription_data, (GDestroyNotify) crs_data_free);
302 g_task_run_in_thread (task, renew_subscription_thread);
303 }
304
305 /* Returns id of renewed subscription or new id */
306 gint
307 pp_cups_renew_subscription_finish (PpCups *self,
308 GAsyncResult *result)
309 {
310 g_return_val_if_fail (g_task_is_valid (result, self), FALSE);
311
312 return g_task_propagate_int (G_TASK (result), NULL);
313 }
314