Debian Codegraph @

lib/packagekit-glib2/pk-control-sync.c

raw
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2008-2009 Richard Hughes <[email protected]>
4 *
5 * Licensed under the GNU Lesser General Public License Version 2.1
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "config.h"
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <gio/gio.h>
27 #include <glib.h>
28 #include <packagekit-glib2/pk-results.h>
29 #include <packagekit-glib2/pk-control.h>
30
31 #include "pk-control-sync.h"
32
33 /* tiny helper to help us do the async operation */
34 typedef struct {
35 GError **error;
36 GMainContext *context;
37 GMainLoop *loop;
38 gboolean ret;
39 guint seconds;
40 gchar **transaction_list;
41 } PkControlHelper;
42
43 /*
44 * pk_control_get_properties_cb:
45 **/
46 static void
47 pk_control_get_properties_cb (PkControl *control, GAsyncResult *res, PkControlHelper *helper)
48 {
49 /* get the result */
50 helper->ret = pk_control_get_properties_finish (control, res, helper->error);
51 g_main_loop_quit (helper->loop);
52 }
53
54 /**
55 * pk_control_get_properties:
56 * @control: a valid #PkControl instance
57 * @cancellable: a #GCancellable or %NULL
58 * @error: A #GError or %NULL
59 *
60 * Gets the properties the daemon supports.
61 * Warning: this function is synchronous, and may block. Do not use it in GUI
62 * applications.
63 *
64 * Return value: %TRUE if the properties were set correctly
65 *
66 * Since: 0.5.3
67 **/
68 gboolean
69 pk_control_get_properties (PkControl *control, GCancellable *cancellable, GError **error)
70 {
71 gboolean ret;
72 PkControlHelper helper;
73
74 g_return_val_if_fail (PK_IS_CONTROL (control), FALSE);
75 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
76
77 /* create temp object */
78 memset (&helper, 0, sizeof (PkControlHelper));
79 helper.context = g_main_context_new ();
80 helper.loop = g_main_loop_new (helper.context, FALSE);
81 helper.error = error;
82
83 g_main_context_push_thread_default (helper.context);
84
85 /* run async method */
86 pk_control_get_properties_async (control, cancellable, (GAsyncReadyCallback) pk_control_get_properties_cb, &helper);
87 g_main_loop_run (helper.loop);
88
89 ret = helper.ret;
90
91 g_main_context_pop_thread_default (helper.context);
92
93 /* free temp object */
94 g_main_loop_unref (helper.loop);
95 g_main_context_unref (helper.context);
96
97 return ret;
98 }
99
100 /*
101 * pk_control_get_transaction_list_cb:
102 **/
103 static void
104 pk_control_get_transaction_list_cb (PkControl *control, GAsyncResult *res, PkControlHelper *helper)
105 {
106 /* get the result */
107 helper->transaction_list = pk_control_get_transaction_list_finish (control, res, helper->error);
108 g_main_loop_quit (helper->loop);
109 }
110
111 /**
112 * pk_control_get_transaction_list:
113 * @control: a valid #PkControl instance
114 * @cancellable: a #GCancellable or %NULL
115 * @error: A #GError or %NULL
116 *
117 * Gets the transaction list in progress.
118 * Warning: this function is synchronous, and may block. Do not use it in GUI
119 * applications.
120 *
121 * Return value: (transfer full): The list of transaction id's, or %NULL, free with g_strfreev()
122 *
123 * Since: 0.5.3
124 **/
125 gchar **
126 pk_control_get_transaction_list (PkControl *control, GCancellable *cancellable, GError **error)
127 {
128 gchar **transaction_list;
129 PkControlHelper helper;
130
131 g_return_val_if_fail (PK_IS_CONTROL (control), NULL);
132 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
133
134 /* create temp object */
135 memset (&helper, 0, sizeof (PkControlHelper));
136 helper.context = g_main_context_new ();
137 helper.loop = g_main_loop_new (helper.context, FALSE);
138 helper.error = error;
139
140 g_main_context_push_thread_default (helper.context);
141
142 /* run async method */
143 pk_control_get_transaction_list_async (control, cancellable, (GAsyncReadyCallback) pk_control_get_transaction_list_cb, &helper);
144 g_main_loop_run (helper.loop);
145
146 transaction_list = helper.transaction_list;
147
148 g_main_context_pop_thread_default (helper.context);
149
150 /* free temp object */
151 g_main_loop_unref (helper.loop);
152 g_main_context_unref (helper.context);
153
154 return transaction_list;
155 }
156
157 /*
158 * pk_control_suggest_daemon_quit_cb:
159 **/
160 static void
161 pk_control_suggest_daemon_quit_cb (PkControl *control, GAsyncResult *res, PkControlHelper *helper)
162 {
163 /* get the result */
164 helper->ret = pk_control_suggest_daemon_quit_finish (control, res, helper->error);
165 g_main_loop_quit (helper->loop);
166 }
167
168 /**
169 * pk_control_suggest_daemon_quit:
170 * @control: a valid #PkControl instance
171 * @cancellable: a #GCancellable or %NULL
172 * @error: A #GError or %NULL
173 *
174 * Suggests to the daemon that it should quit as soon as possible.
175 * Warning: this function is synchronous, and may block. Do not use it in GUI
176 * applications.
177 *
178 * Return value: %TRUE if the suggestion was sent
179 *
180 * Since: 0.6.2
181 **/
182 gboolean
183 pk_control_suggest_daemon_quit (PkControl *control, GCancellable *cancellable, GError **error)
184 {
185 gboolean ret;
186 PkControlHelper helper;
187
188 g_return_val_if_fail (PK_IS_CONTROL (control), FALSE);
189 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
190
191 /* create temp object */
192 memset (&helper, 0, sizeof (PkControlHelper));
193 helper.context = g_main_context_new ();
194 helper.loop = g_main_loop_new (helper.context, FALSE);
195 helper.error = error;
196
197 g_main_context_push_thread_default (helper.context);
198
199 /* run async method */
200 pk_control_suggest_daemon_quit_async (control, cancellable, (GAsyncReadyCallback) pk_control_suggest_daemon_quit_cb, &helper);
201 g_main_loop_run (helper.loop);
202
203 ret = helper.ret;
204
205 g_main_context_pop_thread_default (helper.context);
206
207 /* free temp object */
208 g_main_loop_unref (helper.loop);
209 g_main_context_unref (helper.context);
210
211 return ret;
212 }
213
214 /*
215 * pk_control_set_proxy_cb:
216 **/
217 static void
218 pk_control_set_proxy_cb (PkControl *control, GAsyncResult *res, PkControlHelper *helper)
219 {
220 /* get the result */
221 helper->ret = pk_control_set_proxy_finish (control, res, helper->error);
222 g_main_loop_quit (helper->loop);
223 }
224
225 /**
226 * pk_control_set_proxy2:
227 * @control: a valid #PkControl instance
228 * @proxy_http: the HTTP proxy server
229 * @proxy_https: the HTTPS proxy server
230 * @proxy_ftp: the FTP proxy server
231 * @proxy_socks: the SOCKS proxy server
232 * @no_proxy: the list of download IPs that shouldn't go through the proxy
233 * @pac: the PAC string
234 * @cancellable: a #GCancellable or %NULL
235 * @error: A #GError or %NULL
236 *
237 * Sets the network proxy to use in the daemon.
238 * Warning: this function is synchronous, and may block. Do not use it in GUI
239 * applications.
240 *
241 * Return value: %TRUE if the proxy was set correctly
242 *
243 * Since: 0.6.13
244 **/
245 gboolean
246 pk_control_set_proxy2 (PkControl *control,
247 const gchar *proxy_http,
248 const gchar *proxy_https,
249 const gchar *proxy_ftp,
250 const gchar *proxy_socks,
251 const gchar *no_proxy,
252 const gchar *pac,
253 GCancellable *cancellable,
254 GError **error)
255 {
256 gboolean ret;
257 PkControlHelper helper;
258
259 g_return_val_if_fail (PK_IS_CONTROL (control), FALSE);
260 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
261
262 /* create temp object */
263 memset (&helper, 0, sizeof (PkControlHelper));
264 helper.context = g_main_context_new ();
265 helper.loop = g_main_loop_new (helper.context, FALSE);
266 helper.error = error;
267
268 g_main_context_push_thread_default (helper.context);
269
270 /* run async method */
271 pk_control_set_proxy2_async (control,
272 proxy_http,
273 proxy_https,
274 proxy_ftp,
275 proxy_socks,
276 no_proxy,
277 pac,
278 cancellable,
279 (GAsyncReadyCallback) pk_control_set_proxy_cb,
280 &helper);
281 g_main_loop_run (helper.loop);
282
283 ret = helper.ret;
284
285 g_main_context_pop_thread_default (helper.context);
286
287 /* free temp object */
288 g_main_loop_unref (helper.loop);
289 g_main_context_unref (helper.context);
290
291 return ret;
292 }
293
294
295 /**
296 * pk_control_set_proxy:
297 * @control: a valid #PkControl instance
298 * @proxy_http: the HTTP proxy server
299 * @proxy_ftp: the FTP proxy server
300 * @cancellable: a #GCancellable or %NULL
301 * @error: A #GError or %NULL
302 *
303 * Sets the network proxy to use in the daemon.
304 * Warning: this function is synchronous, and may block. Do not use it in GUI
305 * applications.
306 *
307 * Return value: %TRUE if the proxy was set correctly
308 *
309 * NOTE: This is just provided for backwards compatibility.
310 * Clients should really be using pk_control_set_proxy2().
311 *
312 * Since: 0.6.3
313 **/
314 gboolean
315 pk_control_set_proxy (PkControl *control,
316 const gchar *proxy_http,
317 const gchar *proxy_ftp,
318 GCancellable *cancellable,
319 GError **error)
320 {
321 return pk_control_set_proxy2 (control,
322 proxy_http,
323 NULL,
324 proxy_ftp,
325 NULL,
326 NULL,
327 NULL,
328 cancellable,
329 error);
330 }