annotate pui-backend.c @ 0:6884bb8130ca

Initial revision
author Guido Berhoerster <guido+pui@berhoerster.name>
date Sun, 20 May 2018 11:32:57 +0200
parents
children 3d72ca76538d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
1 /*
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
2 * Copyright (C) 2018 Guido Berhoerster <guido+pui@berhoerster.name>
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
3 *
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
4 * Permission is hereby granted, free of charge, to any person obtaining
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
5 * a copy of this software and associated documentation files (the
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
6 * "Software"), to deal in the Software without restriction, including
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
7 * without limitation the rights to use, copy, modify, merge, publish,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
8 * distribute, sublicense, and/or sell copies of the Software, and to
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
9 * permit persons to whom the Software is furnished to do so, subject to
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
10 * the following conditions:
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
11 *
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
12 * The above copyright notice and this permission notice shall be included
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
13 * in all copies or substantial portions of the Software.
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
14 *
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
22 */
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
23
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
24 #include <errno.h>
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
25 #include <fcntl.h>
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
26 #include <packagekit-glib2/packagekit.h>
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
27 #include <string.h>
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
28 #include <sys/stat.h>
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
29 #include <sys/types.h>
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
30 #include <utime.h>
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
31
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
32 #include "pui-common.h"
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
33 #include "pui-backend.h"
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
34 #include "pui-get-updates.h"
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
35
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
36 struct _PuiBackend {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
37 GObject parent_instance;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
38 PkControl *pk_control;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
39 GCancellable *cancellable;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
40 gint64 last_check;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
41 PkNetworkEnum network_state;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
42 guint periodic_check_id;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
43 guint refresh_interval;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
44 guint important_updates;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
45 guint normal_updates;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
46 };
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
47
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
48 static void pui_backend_async_initable_iface_init(gpointer, gpointer);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
49
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
50 G_DEFINE_TYPE_WITH_CODE(PuiBackend, pui_backend, G_TYPE_OBJECT,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
51 G_IMPLEMENT_INTERFACE(G_TYPE_ASYNC_INITABLE,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
52 pui_backend_async_initable_iface_init))
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
53
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
54 enum {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
55 STATE_CHANGED,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
56 RESTART_REQUIRED,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
57 SIGNAL_LAST
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
58 };
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
59
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
60 enum {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
61 PROP_0,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
62 PROP_IMPORTANT_UPDATES,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
63 PROP_NORMAL_UPDATES,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
64 PROP_REFRESH_INTERVAL,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
65 PROP_LAST
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
66 };
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
67
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
68 static guint signals[SIGNAL_LAST] = { 0 };
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
69 static GParamSpec *properties[PROP_LAST] = { NULL };
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
70
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
71 static gboolean periodic_check(gpointer);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
72
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
73 GQuark
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
74 pui_backend_error_quark(void)
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
75 {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
76 return (g_quark_from_static_string("pui-backend-error-quark"));
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
77 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
78
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
79 static void
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
80 process_pk_package(gpointer data, gpointer user_data)
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
81 {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
82 PkPackage *package = data;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
83 PuiBackend *self = user_data;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
84 PkInfoEnum type_info = pk_package_get_info(package);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
85
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
86 switch (type_info) {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
87 case PK_INFO_ENUM_LOW: /* FALLTHROUGH */
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
88 case PK_INFO_ENUM_ENHANCEMENT: /* FALLTHROUGH */
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
89 case PK_INFO_ENUM_NORMAL:
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
90 self->normal_updates++;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
91 break;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
92 case PK_INFO_ENUM_BUGFIX: /* FALLTHROUGH */
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
93 case PK_INFO_ENUM_IMPORTANT: /* FALLTHROUGH */
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
94 case PK_INFO_ENUM_SECURITY:
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
95 self->important_updates++;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
96 break;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
97 default:
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
98 break;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
99 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
100 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
101
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
102 static void
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
103 on_get_updates_finished(GObject *source_object, GAsyncResult *async_result,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
104 gpointer user_data)
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
105 {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
106 PuiBackend *self = user_data;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
107 GPtrArray *package_list = NULL;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
108 GError *error = NULL;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
109 guint prev_normal_updates = self->normal_updates;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
110 guint prev_important_updates = self->important_updates;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
111
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
112 package_list = pui_get_updates_finish(async_result, &error);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
113 if (package_list == NULL) {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
114 if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED) ||
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
115 g_error_matches(error, PUI_GET_UPDATES_ERROR,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
116 PUI_GET_UPDATES_ERROR_CANCELLED)) {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
117 /* cancelled */
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
118 g_debug("cancelled checking for updates");
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
119 } else {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
120 g_warning("failed to check for updates: %s",
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
121 error->message);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
122 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
123 g_error_free(error);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
124 goto out;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
125 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
126
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
127 self->normal_updates = 0;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
128 self->important_updates = 0;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
129 g_ptr_array_foreach(package_list, process_pk_package, self);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
130 g_debug("normal updates: %u, important updates: %u",
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
131 self->normal_updates, self->important_updates);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
132 if (self->normal_updates != prev_normal_updates) {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
133 g_object_notify_by_pspec(G_OBJECT(self),
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
134 properties[PROP_NORMAL_UPDATES]);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
135 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
136 if (self->important_updates != prev_important_updates) {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
137 g_object_notify_by_pspec(G_OBJECT(self),
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
138 properties[PROP_IMPORTANT_UPDATES]);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
139 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
140 if ((self->normal_updates != prev_normal_updates) ||
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
141 (self->important_updates != prev_important_updates)) {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
142 g_debug("emitting signal state-changed");
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
143 g_signal_emit(self, signals[STATE_CHANGED], 0);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
144 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
145
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
146 /* last successful check */
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
147 self->last_check = g_get_monotonic_time();
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
148
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
149 out:
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
150 /* reschedule periodic check */
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
151 if (self->network_state != PK_NETWORK_ENUM_OFFLINE) {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
152 self->periodic_check_id =
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
153 g_timeout_add_seconds(PUI_CHECK_UPDATES_INTERVAL,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
154 periodic_check, self);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
155 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
156
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
157 if (package_list != NULL) {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
158 g_ptr_array_unref(package_list);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
159 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
160 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
161
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
162 static gboolean
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
163 periodic_check(gpointer user_data)
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
164 {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
165 PuiBackend *self = user_data;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
166
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
167 g_debug("running periodic check");
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
168
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
169 pui_get_updates_async(self->pk_control, self->refresh_interval,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
170 self->cancellable, on_get_updates_finished, self);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
171
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
172 /* next periodic check will be scheduled after completion */
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
173 self->periodic_check_id = 0;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
174
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
175 return (G_SOURCE_REMOVE);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
176 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
177
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
178 static void
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
179 pui_backend_set_property(GObject *object, guint property_id,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
180 const GValue *value, GParamSpec *pspec)
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
181 {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
182 PuiBackend *self = PUI_BACKEND(object);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
183
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
184 switch (property_id) {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
185 case PROP_REFRESH_INTERVAL:
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
186 self->refresh_interval = g_value_get_uint(value);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
187 g_debug("property \"refresh-interval\" set to %u",
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
188 self->refresh_interval);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
189 break;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
190 default:
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
191 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
192 break;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
193 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
194 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
195
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
196 static void
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
197 pui_backend_get_property(GObject *object, guint property_id, GValue *value,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
198 GParamSpec *pspec)
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
199 {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
200 PuiBackend *self = PUI_BACKEND(object);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
201
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
202 switch (property_id) {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
203 case PROP_IMPORTANT_UPDATES:
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
204 g_value_set_uint(value, self->important_updates);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
205 break;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
206 case PROP_NORMAL_UPDATES:
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
207 g_value_set_uint(value, self->normal_updates);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
208 break;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
209 case PROP_REFRESH_INTERVAL:
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
210 g_value_set_uint(value, self->refresh_interval);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
211 break;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
212 default:
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
213 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
214 break;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
215 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
216 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
217
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
218 static void
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
219 pui_backend_dispose(GObject *object)
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
220 {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
221 PuiBackend *self = PUI_BACKEND(object);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
222
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
223 if (self->periodic_check_id != 0) {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
224 g_source_remove(self->periodic_check_id);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
225 self->periodic_check_id = 0;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
226 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
227
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
228 if (self->cancellable != NULL) {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
229 g_cancellable_cancel(self->cancellable);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
230 g_clear_object(&self->cancellable);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
231 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
232
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
233 if (self->pk_control != NULL) {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
234 g_clear_object(&self->pk_control);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
235 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
236
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
237 G_OBJECT_CLASS(pui_backend_parent_class)->dispose(object);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
238 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
239
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
240 static void
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
241 pui_backend_class_init(PuiBackendClass *klass)
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
242 {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
243 GObjectClass *object_class = G_OBJECT_CLASS(klass);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
244
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
245 object_class->set_property = pui_backend_set_property;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
246 object_class->get_property = pui_backend_get_property;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
247 object_class->dispose = pui_backend_dispose;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
248
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
249 properties[PROP_IMPORTANT_UPDATES] =
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
250 g_param_spec_uint("important-updates", "Important updates",
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
251 "Number of available important updates", 0, G_MAXUINT, 0,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
252 G_PARAM_READABLE);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
253
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
254 properties[PROP_NORMAL_UPDATES] =
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
255 g_param_spec_uint("normal-updates", "Normal updates",
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
256 "Number of available normal updates", 0, G_MAXUINT, 0,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
257 G_PARAM_READABLE);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
258
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
259 properties[PROP_REFRESH_INTERVAL] =
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
260 g_param_spec_uint("refresh-interval", "Refresh interval",
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
261 "Interval in seconds for refreshing the package cache", 0,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
262 G_MAXUINT, PUI_DEFAULT_REFRESH_INTERVAL, G_PARAM_READWRITE);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
263
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
264 g_object_class_install_properties(object_class, PROP_LAST, properties);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
265
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
266 signals[STATE_CHANGED] = g_signal_new("state-changed",
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
267 G_TYPE_FROM_CLASS(object_class),
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
268 G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS, 0,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
269 NULL, NULL, NULL, G_TYPE_NONE, 0);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
270
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
271 signals[RESTART_REQUIRED] = g_signal_new("restart-required",
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
272 G_TYPE_FROM_CLASS(object_class),
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
273 G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS, 0,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
274 NULL, NULL, NULL, G_TYPE_NONE, 0);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
275 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
276
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
277 static void
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
278 pui_backend_init(PuiBackend *self)
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
279 {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
280 self->cancellable = g_cancellable_new();
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
281 self->pk_control = pk_control_new();
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
282 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
283
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
284 static void
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
285 on_get_properties_finished(GObject *object, GAsyncResult *result,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
286 gpointer user_data)
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
287 {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
288 PkControl *control = PK_CONTROL(object);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
289 PuiBackend *self;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
290 GTask *task = user_data;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
291 GError *error = NULL;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
292 gchar *backend_name = NULL;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
293 PkBitfield roles = 0;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
294 gchar *roles_str = NULL;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
295
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
296 self = g_task_get_task_data(task);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
297
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
298 if (!pk_control_get_properties_finish(control, result, &error)) {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
299 g_task_return_error(task, error);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
300 goto out;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
301 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
302
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
303 /* check whether the backend supports GetUpdates */
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
304 g_object_get(control, "backend-name", &backend_name, "roles", &roles,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
305 "network-state", &self->network_state, NULL);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
306 g_debug("backend: %s", backend_name);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
307 roles_str = pk_role_bitfield_to_string(roles);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
308 g_debug("roles: %s", roles_str);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
309 g_debug("network-state: %s",
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
310 pk_network_enum_to_string(self->network_state));
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
311 if (!pk_bitfield_contain(roles, PK_ROLE_ENUM_GET_UPDATES)) {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
312 error = g_error_new(PUI_BACKEND_ERROR,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
313 PUI_BACKEND_ERROR_GET_UPDATES_NOT_IMPLEMENTED,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
314 "Getting updates is not implemented in the %s PackageKit "
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
315 "backend", backend_name);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
316 g_task_return_error(task, error);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
317 goto out;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
318 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
319
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
320 g_task_return_boolean(task, TRUE);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
321 out:
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
322 g_free(roles_str);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
323 g_free(backend_name);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
324 g_object_unref(task);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
325 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
326
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
327 static void
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
328 on_notify_network_state(PkControl *pk_control, GParamSpec *pspec,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
329 gpointer user_data)
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
330 {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
331 PuiBackend *self = user_data;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
332 PkNetworkEnum network_state;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
333 guint elapsed_time;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
334 guint remaining_time;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
335
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
336 g_object_get(pk_control, "network-state", &network_state, NULL);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
337 g_debug("network state changed: %s",
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
338 pk_network_enum_to_string(network_state));
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
339 if ((self->network_state == PK_NETWORK_ENUM_OFFLINE) &&
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
340 (network_state != PK_NETWORK_ENUM_OFFLINE)) {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
341 /* schedule periodic checks when coming back online */
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
342 elapsed_time = (g_get_monotonic_time() - self->last_check) /
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
343 G_USEC_PER_SEC;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
344 /*
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
345 * if more time that the check interval has passed since the
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
346 * last check, schedule a check after a short delay, otherwise
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
347 * wait until the interval has passed
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
348 */
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
349 remaining_time = (elapsed_time < PUI_CHECK_UPDATES_INTERVAL) ?
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
350 PUI_CHECK_UPDATES_INTERVAL - elapsed_time :
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
351 PUI_STARTUP_DELAY;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
352 self->periodic_check_id = g_timeout_add_seconds(remaining_time,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
353 periodic_check, self);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
354 } else if ((self->network_state != PK_NETWORK_ENUM_OFFLINE) &&
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
355 (network_state == PK_NETWORK_ENUM_OFFLINE)) {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
356 /* cancel periodic checks while offline */
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
357 if (self->periodic_check_id != 0) {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
358 g_source_remove(self->periodic_check_id);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
359 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
360 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
361 self->network_state = network_state;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
362 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
363
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
364 static void
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
365 on_updates_changed(PkControl *control, gpointer user_data)
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
366 {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
367 PuiBackend *self = user_data;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
368
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
369 /*
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
370 * schedule a check after a short delay so that a rapid succession of
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
371 * signals is coalesced
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
372 */
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
373 if (self->network_state != PK_NETWORK_ENUM_OFFLINE) {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
374 if (self->periodic_check_id != 0) {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
375 g_source_remove(self->periodic_check_id);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
376 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
377 self->periodic_check_id =
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
378 g_timeout_add_seconds(PUI_STARTUP_DELAY, periodic_check,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
379 self);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
380 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
381 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
382
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
383 static void
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
384 on_restart_schedule(PkControl *control, gpointer user_data)
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
385 {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
386 PuiBackend *self = user_data;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
387
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
388 g_debug("emitting signal restart-required");
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
389 g_signal_emit(self, signals[RESTART_REQUIRED], 0);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
390 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
391
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
392 static void
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
393 pui_backend_init_async(GAsyncInitable *initable, int io_priority,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
394 GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
395 {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
396 PuiBackend *self = PUI_BACKEND(initable);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
397 GTask *task;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
398
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
399 task = g_task_new(G_OBJECT(initable), cancellable, callback, user_data);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
400 g_task_set_priority(task, io_priority);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
401 g_task_set_task_data(task, g_object_ref(self),
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
402 (GDestroyNotify)g_object_unref);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
403
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
404 pk_control_get_properties_async(self->pk_control, cancellable,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
405 on_get_properties_finished, task);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
406 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
407
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
408 static gboolean
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
409 pui_backend_init_finish(GAsyncInitable *initable, GAsyncResult *result,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
410 GError **errorp)
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
411 {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
412 PuiBackend *self = PUI_BACKEND(initable);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
413 GTask *task = G_TASK(result);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
414
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
415 if (!g_task_propagate_boolean(task, errorp)) {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
416 return (FALSE);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
417 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
418
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
419 /* get notification when the network state changes */
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
420 g_signal_connect(self->pk_control, "notify::network-state",
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
421 G_CALLBACK(on_notify_network_state), self);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
422 /* get notifications when the available package updates change */
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
423 g_signal_connect(self->pk_control, "updates-changed",
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
424 G_CALLBACK(on_updates_changed), self);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
425 /* get notifications when an application restart is required */
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
426 g_signal_connect(self->pk_control, "restart-schedule",
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
427 G_CALLBACK(on_restart_schedule), self);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
428 /* schedule first check after a small delay */
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
429 self->periodic_check_id = g_timeout_add_seconds(PUI_STARTUP_DELAY,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
430 periodic_check, self);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
431
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
432 return (TRUE);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
433 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
434
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
435 static void
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
436 pui_backend_async_initable_iface_init(gpointer g_iface, gpointer iface_data)
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
437 {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
438 GAsyncInitableIface *iface = g_iface;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
439
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
440 iface->init_async = pui_backend_init_async;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
441 iface->init_finish = pui_backend_init_finish;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
442 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
443
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
444 void
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
445 pui_backend_new_async(GCancellable *cancellable, GAsyncReadyCallback callback,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
446 gpointer user_data)
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
447 {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
448 g_async_initable_new_async(PUI_TYPE_BACKEND, G_PRIORITY_DEFAULT,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
449 cancellable, callback, user_data, NULL);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
450 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
451
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
452 PuiBackend *
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
453 pui_backend_new_finish(GAsyncResult *result, GError **errorp)
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
454 {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
455 GObject *object;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
456 GObject *source_object;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
457
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
458 source_object = g_async_result_get_source_object(result);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
459 object = g_async_initable_new_finish(G_ASYNC_INITABLE(source_object),
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
460 result, errorp);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
461 g_object_unref(source_object);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
462
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
463 return ((object != NULL) ? PUI_BACKEND(object) : NULL);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
464 }