File: pk-control-sync.c

package info (click to toggle)
packagekit 1.3.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,824 kB
  • sloc: ansic: 56,491; cpp: 15,652; xml: 5,532; python: 4,932; sh: 316; perl: 60; makefile: 56
file content (330 lines) | stat: -rw-r--r-- 9,241 bytes parent folder | download | duplicates (5)
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
321
322
323
324
325
326
327
328
329
330
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2008-2009 Richard Hughes <richard@hughsie.com>
 *
 * Licensed under the GNU Lesser General Public License Version 2.1
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 */

#include "config.h"

#include <stdio.h>
#include <string.h>
#include <gio/gio.h>
#include <glib.h>
#include <packagekit-glib2/pk-results.h>
#include <packagekit-glib2/pk-control.h>

#include "pk-control-sync.h"

/* tiny helper to help us do the async operation */
typedef struct {
	GError		**error;
	GMainContext	*context;
	GMainLoop	*loop;
	gboolean	 ret;
	guint		 seconds;
	gchar		**transaction_list;
} PkControlHelper;

/*
 * pk_control_get_properties_cb:
 **/
static void
pk_control_get_properties_cb (PkControl *control, GAsyncResult *res, PkControlHelper *helper)
{
	/* get the result */
	helper->ret = pk_control_get_properties_finish (control, res, helper->error);
	g_main_loop_quit (helper->loop);
}

/**
 * pk_control_get_properties:
 * @control: a valid #PkControl instance
 * @cancellable: a #GCancellable or %NULL
 * @error: A #GError or %NULL
 *
 * Gets the properties the daemon supports.
 * Warning: this function is synchronous, and may block. Do not use it in GUI
 * applications.
 *
 * Return value: %TRUE if the properties were set correctly
 *
 * Since: 0.5.3
 **/
gboolean
pk_control_get_properties (PkControl *control, GCancellable *cancellable, GError **error)
{
	gboolean ret;
	PkControlHelper helper;

	g_return_val_if_fail (PK_IS_CONTROL (control), FALSE);
	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

	/* create temp object */
	memset (&helper, 0, sizeof (PkControlHelper));
	helper.context = g_main_context_new ();
	helper.loop = g_main_loop_new (helper.context, FALSE);
	helper.error = error;

	g_main_context_push_thread_default (helper.context);

	/* run async method */
	pk_control_get_properties_async (control, cancellable, (GAsyncReadyCallback) pk_control_get_properties_cb, &helper);
	g_main_loop_run (helper.loop);

	ret = helper.ret;

	g_main_context_pop_thread_default (helper.context);

	/* free temp object */
	g_main_loop_unref (helper.loop);
	g_main_context_unref (helper.context);

	return ret;
}

/*
 * pk_control_get_transaction_list_cb:
 **/
static void
pk_control_get_transaction_list_cb (PkControl *control, GAsyncResult *res, PkControlHelper *helper)
{
	/* get the result */
	helper->transaction_list = pk_control_get_transaction_list_finish (control, res, helper->error);
	g_main_loop_quit (helper->loop);
}

/**
 * pk_control_get_transaction_list:
 * @control: a valid #PkControl instance
 * @cancellable: a #GCancellable or %NULL
 * @error: A #GError or %NULL
 *
 * Gets the transaction list in progress.
 * Warning: this function is synchronous, and may block. Do not use it in GUI
 * applications.
 *
 * Return value: (transfer full): The list of transaction id's, or %NULL, free with g_strfreev()
 *
 * Since: 0.5.3
 **/
gchar **
pk_control_get_transaction_list (PkControl *control, GCancellable *cancellable, GError **error)
{
	gchar **transaction_list;
	PkControlHelper helper;

	g_return_val_if_fail (PK_IS_CONTROL (control), NULL);
	g_return_val_if_fail (error == NULL || *error == NULL, NULL);

	/* create temp object */
	memset (&helper, 0, sizeof (PkControlHelper));
	helper.context = g_main_context_new ();
	helper.loop = g_main_loop_new (helper.context, FALSE);
	helper.error = error;

	g_main_context_push_thread_default (helper.context);

	/* run async method */
	pk_control_get_transaction_list_async (control, cancellable, (GAsyncReadyCallback) pk_control_get_transaction_list_cb, &helper);
	g_main_loop_run (helper.loop);

	transaction_list = helper.transaction_list;

	g_main_context_pop_thread_default (helper.context);

	/* free temp object */
	g_main_loop_unref (helper.loop);
	g_main_context_unref (helper.context);

	return transaction_list;
}

/*
 * pk_control_suggest_daemon_quit_cb:
 **/
static void
pk_control_suggest_daemon_quit_cb (PkControl *control, GAsyncResult *res, PkControlHelper *helper)
{
	/* get the result */
	helper->ret = pk_control_suggest_daemon_quit_finish (control, res, helper->error);
	g_main_loop_quit (helper->loop);
}

/**
 * pk_control_suggest_daemon_quit:
 * @control: a valid #PkControl instance
 * @cancellable: a #GCancellable or %NULL
 * @error: A #GError or %NULL
 *
 * Suggests to the daemon that it should quit as soon as possible.
 * Warning: this function is synchronous, and may block. Do not use it in GUI
 * applications.
 *
 * Return value: %TRUE if the suggestion was sent
 *
 * Since: 0.6.2
 **/
gboolean
pk_control_suggest_daemon_quit (PkControl *control, GCancellable *cancellable, GError **error)
{
	gboolean ret;
	PkControlHelper helper;

	g_return_val_if_fail (PK_IS_CONTROL (control), FALSE);
	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

	/* create temp object */
	memset (&helper, 0, sizeof (PkControlHelper));
	helper.context = g_main_context_new ();
	helper.loop = g_main_loop_new (helper.context, FALSE);
	helper.error = error;

	g_main_context_push_thread_default (helper.context);

	/* run async method */
	pk_control_suggest_daemon_quit_async (control, cancellable, (GAsyncReadyCallback) pk_control_suggest_daemon_quit_cb, &helper);
	g_main_loop_run (helper.loop);

	ret = helper.ret;

	g_main_context_pop_thread_default (helper.context);

	/* free temp object */
	g_main_loop_unref (helper.loop);
	g_main_context_unref (helper.context);

	return ret;
}

/*
 * pk_control_set_proxy_cb:
 **/
static void
pk_control_set_proxy_cb (PkControl *control, GAsyncResult *res, PkControlHelper *helper)
{
	/* get the result */
	helper->ret = pk_control_set_proxy_finish (control, res, helper->error);
	g_main_loop_quit (helper->loop);
}

/**
 * pk_control_set_proxy2:
 * @control: a valid #PkControl instance
 * @proxy_http: the HTTP proxy server
 * @proxy_https: the HTTPS proxy server
 * @proxy_ftp: the FTP proxy server
 * @proxy_socks: the SOCKS proxy server
 * @no_proxy: the list of download IPs that shouldn't go through the proxy
 * @pac: the PAC string
 * @cancellable: a #GCancellable or %NULL
 * @error: A #GError or %NULL
 *
 * Sets the network proxy to use in the daemon.
 * Warning: this function is synchronous, and may block. Do not use it in GUI
 * applications.
 *
 * Return value: %TRUE if the proxy was set correctly
 *
 * Since: 0.6.13
 **/
gboolean
pk_control_set_proxy2 (PkControl *control,
		       const gchar *proxy_http,
		       const gchar *proxy_https,
		       const gchar *proxy_ftp,
		       const gchar *proxy_socks,
		       const gchar *no_proxy,
		       const gchar *pac,
		       GCancellable *cancellable,
		       GError **error)
{
	gboolean ret;
	PkControlHelper helper;

	g_return_val_if_fail (PK_IS_CONTROL (control), FALSE);
	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

	/* create temp object */
	memset (&helper, 0, sizeof (PkControlHelper));
	helper.context = g_main_context_new ();
	helper.loop = g_main_loop_new (helper.context, FALSE);
	helper.error = error;

	g_main_context_push_thread_default (helper.context);

	/* run async method */
	pk_control_set_proxy2_async (control,
				     proxy_http,
				     proxy_https,
				     proxy_ftp,
				     proxy_socks,
				     no_proxy,
				     pac,
				     cancellable,
				     (GAsyncReadyCallback) pk_control_set_proxy_cb,
				     &helper);
	g_main_loop_run (helper.loop);

	ret = helper.ret;

	g_main_context_pop_thread_default (helper.context);

	/* free temp object */
	g_main_loop_unref (helper.loop);
	g_main_context_unref (helper.context);

	return ret;
}


/**
 * pk_control_set_proxy:
 * @control: a valid #PkControl instance
 * @proxy_http: the HTTP proxy server
 * @proxy_ftp: the FTP proxy server
 * @cancellable: a #GCancellable or %NULL
 * @error: A #GError or %NULL
 *
 * Sets the network proxy to use in the daemon.
 * Warning: this function is synchronous, and may block. Do not use it in GUI
 * applications.
 *
 * Return value: %TRUE if the proxy was set correctly
 *
 * NOTE: This is just provided for backwards compatibility.
 * Clients should really be using pk_control_set_proxy2().
 *
 * Since: 0.6.3
 **/
gboolean
pk_control_set_proxy (PkControl *control,
		      const gchar *proxy_http,
		      const gchar *proxy_ftp,
		      GCancellable *cancellable,
		      GError **error)
{
	return pk_control_set_proxy2 (control,
				      proxy_http,
				      NULL,
				      proxy_ftp,
				      NULL,
				      NULL,
				      NULL,
				      cancellable,
				      error);
}