annotate pui-backend.c @ 5:a4020e99e550

Do not check for updates if the battery is low
author Guido Berhoerster <guido+pui@berhoerster.name>
date Sun, 17 Jun 2018 17:05:16 +0200
parents 3d72ca76538d
children 2477a6151087
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>
5
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
30 #include <upower.h>
0
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
31 #include <utime.h>
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
32
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
33 #include "pui-common.h"
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
34 #include "pui-backend.h"
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
35 #include "pui-get-updates.h"
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
36
5
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
37 #define LOW_BATTERY_THRESHOLD 10.0
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
38
0
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
39 struct _PuiBackend {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
40 GObject parent_instance;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
41 PkControl *pk_control;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
42 GCancellable *cancellable;
5
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
43 UpClient *up_client;
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
44 UpDevice *up_device;
0
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
45 gint64 last_check;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
46 PkNetworkEnum network_state;
4
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
47 gboolean inhibited;
5
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
48 gboolean is_battery_low;
0
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
49 guint periodic_check_id;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
50 guint refresh_interval;
4
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
51 gboolean use_mobile_connection;
0
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
52 guint important_updates;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
53 guint normal_updates;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
54 };
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
55
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
56 static void pui_backend_async_initable_iface_init(gpointer, gpointer);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
57
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
58 G_DEFINE_TYPE_WITH_CODE(PuiBackend, pui_backend, G_TYPE_OBJECT,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
59 G_IMPLEMENT_INTERFACE(G_TYPE_ASYNC_INITABLE,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
60 pui_backend_async_initable_iface_init))
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
61
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
62 enum {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
63 STATE_CHANGED,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
64 RESTART_REQUIRED,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
65 SIGNAL_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 enum {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
69 PROP_0,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
70 PROP_IMPORTANT_UPDATES,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
71 PROP_NORMAL_UPDATES,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
72 PROP_REFRESH_INTERVAL,
4
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
73 PROP_USE_MOBILE_CONNECTION,
0
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
74 PROP_LAST
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
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
77 static guint signals[SIGNAL_LAST] = { 0 };
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
78 static GParamSpec *properties[PROP_LAST] = { NULL };
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
79
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
80 static gboolean periodic_check(gpointer);
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 GQuark
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
83 pui_backend_error_quark(void)
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
84 {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
85 return (g_quark_from_static_string("pui-backend-error-quark"));
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
86 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
87
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
88 static void
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
89 process_pk_package(gpointer data, gpointer user_data)
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
90 {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
91 PkPackage *package = data;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
92 PuiBackend *self = user_data;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
93 PkInfoEnum type_info = pk_package_get_info(package);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
94
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
95 switch (type_info) {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
96 case PK_INFO_ENUM_LOW: /* FALLTHROUGH */
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
97 case PK_INFO_ENUM_ENHANCEMENT: /* FALLTHROUGH */
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
98 case PK_INFO_ENUM_NORMAL:
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
99 self->normal_updates++;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
100 break;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
101 case PK_INFO_ENUM_BUGFIX: /* FALLTHROUGH */
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
102 case PK_INFO_ENUM_IMPORTANT: /* FALLTHROUGH */
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
103 case PK_INFO_ENUM_SECURITY:
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
104 self->important_updates++;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
105 break;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
106 default:
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
107 break;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
108 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
109 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
110
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
111 static void
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
112 on_get_updates_finished(GObject *source_object, GAsyncResult *async_result,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
113 gpointer user_data)
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
114 {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
115 PuiBackend *self = user_data;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
116 GPtrArray *package_list = NULL;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
117 GError *error = NULL;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
118 guint prev_normal_updates = self->normal_updates;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
119 guint prev_important_updates = self->important_updates;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
120
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
121 package_list = pui_get_updates_finish(async_result, &error);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
122 if (package_list == NULL) {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
123 if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED) ||
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
124 g_error_matches(error, PUI_GET_UPDATES_ERROR,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
125 PUI_GET_UPDATES_ERROR_CANCELLED)) {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
126 /* cancelled */
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
127 g_debug("cancelled checking for updates");
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
128 } else {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
129 g_warning("failed to check for updates: %s",
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
130 error->message);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
131 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
132 g_error_free(error);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
133 goto out;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
134 }
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 self->normal_updates = 0;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
137 self->important_updates = 0;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
138 g_ptr_array_foreach(package_list, process_pk_package, self);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
139 g_debug("normal updates: %u, important updates: %u",
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
140 self->normal_updates, self->important_updates);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
141 if (self->normal_updates != prev_normal_updates) {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
142 g_object_notify_by_pspec(G_OBJECT(self),
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
143 properties[PROP_NORMAL_UPDATES]);
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 if (self->important_updates != prev_important_updates) {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
146 g_object_notify_by_pspec(G_OBJECT(self),
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
147 properties[PROP_IMPORTANT_UPDATES]);
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 if ((self->normal_updates != prev_normal_updates) ||
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
150 (self->important_updates != prev_important_updates)) {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
151 g_debug("emitting signal state-changed");
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
152 g_signal_emit(self, signals[STATE_CHANGED], 0);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
153 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
154
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
155 /* last successful check */
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
156 self->last_check = g_get_monotonic_time();
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
157
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
158 out:
4
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
159 g_clear_object(&self->cancellable);
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
160
0
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
161 /* reschedule periodic check */
4
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
162 if (!self->inhibited) {
0
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
163 self->periodic_check_id =
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
164 g_timeout_add_seconds(PUI_CHECK_UPDATES_INTERVAL,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
165 periodic_check, self);
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
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
168 if (package_list != NULL) {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
169 g_ptr_array_unref(package_list);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
170 }
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
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
173 static gboolean
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
174 periodic_check(gpointer user_data)
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
175 {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
176 PuiBackend *self = user_data;
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 g_debug("running periodic check");
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
179
4
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
180 self->cancellable = g_cancellable_new();
0
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
181 pui_get_updates_async(self->pk_control, self->refresh_interval,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
182 self->cancellable, on_get_updates_finished, self);
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 /* next periodic check will be scheduled after completion */
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
185 self->periodic_check_id = 0;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
186
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
187 return (G_SOURCE_REMOVE);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
188 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
189
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
190 static void
4
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
191 check_inhibit(PuiBackend *self)
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
192 {
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
193 gboolean inhibited;
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
194 guint elapsed_time;
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
195 guint remaining_time;
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
196
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
197 inhibited = ((self->network_state == PK_NETWORK_ENUM_OFFLINE) ||
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
198 (!self->use_mobile_connection &&
5
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
199 (self->network_state == PK_NETWORK_ENUM_MOBILE)) ||
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
200 self->is_battery_low);
4
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
201 if (self->inhibited == inhibited) {
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
202 return;
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
203 }
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
204
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
205 self->inhibited = inhibited;
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
206 if (inhibited) {
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
207 /* cancel periodic checks */
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
208 if (self->periodic_check_id != 0) {
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
209 g_source_remove(self->periodic_check_id);
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
210 }
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
211
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
212 /* cancel running operation */
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
213 if ((self->cancellable != NULL) &&
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
214 !g_cancellable_is_cancelled(self->cancellable)) {
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
215 g_cancellable_cancel(self->cancellable);
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
216 g_clear_object(&self->cancellable);
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
217 }
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
218 } else {
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
219 /* schedule periodic checks when no longer inhibited */
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
220 elapsed_time = (g_get_monotonic_time() - self->last_check) /
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
221 G_USEC_PER_SEC;
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
222 /*
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
223 * if more time that the check interval has passed since the
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
224 * last check, schedule a check after a short delay, otherwise
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
225 * wait until the interval has passed
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
226 */
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
227 remaining_time = (elapsed_time < PUI_CHECK_UPDATES_INTERVAL) ?
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
228 PUI_CHECK_UPDATES_INTERVAL - elapsed_time :
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
229 PUI_STARTUP_DELAY;
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
230 self->periodic_check_id = g_timeout_add_seconds(remaining_time,
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
231 periodic_check, self);
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
232 }
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
233 }
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
234
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
235 static void
0
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
236 pui_backend_set_property(GObject *object, guint property_id,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
237 const GValue *value, GParamSpec *pspec)
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 PuiBackend *self = PUI_BACKEND(object);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
240
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
241 switch (property_id) {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
242 case PROP_REFRESH_INTERVAL:
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
243 self->refresh_interval = g_value_get_uint(value);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
244 g_debug("property \"refresh-interval\" set to %u",
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
245 self->refresh_interval);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
246 break;
4
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
247 case PROP_USE_MOBILE_CONNECTION:
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
248 self->use_mobile_connection = g_value_get_boolean(value);
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
249 g_debug("property \"use-mobile-connection\" set to %s",
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
250 self->use_mobile_connection ? "true" : "false");
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
251 check_inhibit(self);
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
252 break;
0
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
253 default:
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
254 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
255 break;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
256 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
257 }
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 static void
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
260 pui_backend_get_property(GObject *object, guint property_id, GValue *value,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
261 GParamSpec *pspec)
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
262 {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
263 PuiBackend *self = PUI_BACKEND(object);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
264
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
265 switch (property_id) {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
266 case PROP_IMPORTANT_UPDATES:
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
267 g_value_set_uint(value, self->important_updates);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
268 break;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
269 case PROP_NORMAL_UPDATES:
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
270 g_value_set_uint(value, self->normal_updates);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
271 break;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
272 case PROP_REFRESH_INTERVAL:
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
273 g_value_set_uint(value, self->refresh_interval);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
274 break;
4
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
275 case PROP_USE_MOBILE_CONNECTION:
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
276 g_value_set_boolean(value, self->use_mobile_connection);
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
277 break;
0
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
278 default:
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
279 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
280 break;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
281 }
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 pui_backend_dispose(GObject *object)
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
286 {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
287 PuiBackend *self = PUI_BACKEND(object);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
288
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
289 if (self->periodic_check_id != 0) {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
290 g_source_remove(self->periodic_check_id);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
291 self->periodic_check_id = 0;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
292 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
293
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
294 if (self->cancellable != NULL) {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
295 g_cancellable_cancel(self->cancellable);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
296 g_clear_object(&self->cancellable);
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
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
299 if (self->pk_control != NULL) {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
300 g_clear_object(&self->pk_control);
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
5
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
303 if (self->up_device != NULL) {
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
304 g_clear_object(&self->up_device);
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
305 }
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
306
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
307 if (self->up_client != NULL) {
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
308 g_clear_object(&self->up_client);
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
309 }
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
310
0
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
311 G_OBJECT_CLASS(pui_backend_parent_class)->dispose(object);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
312 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
313
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
314 static void
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
315 pui_backend_class_init(PuiBackendClass *klass)
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
316 {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
317 GObjectClass *object_class = G_OBJECT_CLASS(klass);
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 object_class->set_property = pui_backend_set_property;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
320 object_class->get_property = pui_backend_get_property;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
321 object_class->dispose = pui_backend_dispose;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
322
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
323 properties[PROP_IMPORTANT_UPDATES] =
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
324 g_param_spec_uint("important-updates", "Important updates",
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
325 "Number of available important updates", 0, G_MAXUINT, 0,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
326 G_PARAM_READABLE);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
327
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
328 properties[PROP_NORMAL_UPDATES] =
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
329 g_param_spec_uint("normal-updates", "Normal updates",
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
330 "Number of available normal updates", 0, G_MAXUINT, 0,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
331 G_PARAM_READABLE);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
332
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
333 properties[PROP_REFRESH_INTERVAL] =
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
334 g_param_spec_uint("refresh-interval", "Refresh interval",
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
335 "Interval in seconds for refreshing the package cache", 0,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
336 G_MAXUINT, PUI_DEFAULT_REFRESH_INTERVAL, G_PARAM_READWRITE);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
337
4
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
338 properties[PROP_USE_MOBILE_CONNECTION] =
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
339 g_param_spec_boolean("use-mobile-connection",
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
340 "Whether to use a mobile connection", "Whether to use a mobile "
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
341 "connection for refreshing the package cache", FALSE,
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
342 G_PARAM_READWRITE);
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
343
0
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
344 g_object_class_install_properties(object_class, PROP_LAST, properties);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
345
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
346 signals[STATE_CHANGED] = g_signal_new("state-changed",
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
347 G_TYPE_FROM_CLASS(object_class),
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
348 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
349 NULL, NULL, NULL, G_TYPE_NONE, 0);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
350
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
351 signals[RESTART_REQUIRED] = g_signal_new("restart-required",
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
352 G_TYPE_FROM_CLASS(object_class),
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
353 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
354 NULL, NULL, NULL, G_TYPE_NONE, 0);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
355 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
356
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
357 static void
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
358 pui_backend_init(PuiBackend *self)
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 self->pk_control = pk_control_new();
5
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
361
4
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
362 self->inhibited = TRUE;
5
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
363
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
364 self->up_client = up_client_new();
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
365 if (self->up_client) {
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
366 self->up_device = up_client_get_display_device(self->up_client);
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
367 }
0
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 static void
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
371 on_get_properties_finished(GObject *object, GAsyncResult *result,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
372 gpointer user_data)
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
373 {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
374 PkControl *control = PK_CONTROL(object);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
375 PuiBackend *self;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
376 GTask *task = user_data;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
377 GError *error = NULL;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
378 gchar *backend_name = NULL;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
379 PkBitfield roles = 0;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
380 gchar *roles_str = NULL;
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 self = g_task_get_task_data(task);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
383
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
384 if (!pk_control_get_properties_finish(control, result, &error)) {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
385 g_task_return_error(task, error);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
386 goto out;
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
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
389 /* check whether the backend supports GetUpdates */
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
390 g_object_get(control, "backend-name", &backend_name, "roles", &roles,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
391 "network-state", &self->network_state, NULL);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
392 g_debug("backend: %s", backend_name);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
393 roles_str = pk_role_bitfield_to_string(roles);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
394 g_debug("roles: %s", roles_str);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
395 g_debug("network-state: %s",
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
396 pk_network_enum_to_string(self->network_state));
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
397 if (!pk_bitfield_contain(roles, PK_ROLE_ENUM_GET_UPDATES)) {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
398 error = g_error_new(PUI_BACKEND_ERROR,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
399 PUI_BACKEND_ERROR_GET_UPDATES_NOT_IMPLEMENTED,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
400 "Getting updates is not implemented in the %s PackageKit "
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
401 "backend", backend_name);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
402 g_task_return_error(task, error);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
403 goto out;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
404 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
405
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
406 g_task_return_boolean(task, TRUE);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
407 out:
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
408 g_free(roles_str);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
409 g_free(backend_name);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
410 g_object_unref(task);
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
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
413 static void
5
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
414 on_notify_device_charge_percentage(UpDevice *device, GParamSpec *pspec,
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
415 gpointer user_data)
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
416 {
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
417 PuiBackend *self = user_data;
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
418 UpDeviceKind kind;
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
419 gdouble percentage;
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
420
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
421 g_object_get(device, "kind", &kind, "percentage", &percentage, NULL);
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
422 if ((kind != UP_DEVICE_KIND_BATTERY) && (kind != UP_DEVICE_KIND_UPS)) {
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
423 return;
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
424 }
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
425 g_debug("charge percentage changed: %.0f%%\n", percentage);
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
426 if ((self->is_battery_low && (percentage > LOW_BATTERY_THRESHOLD)) ||
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
427 (!self->is_battery_low && (percentage < LOW_BATTERY_THRESHOLD))) {
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
428 self->is_battery_low = !self->is_battery_low;
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
429 check_inhibit(self);
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
430 }
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
431 }
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
432
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
433 static void
0
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
434 on_notify_network_state(PkControl *pk_control, GParamSpec *pspec,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
435 gpointer user_data)
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
436 {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
437 PuiBackend *self = user_data;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
438
4
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
439 g_object_get(pk_control, "network-state", &self->network_state, NULL);
0
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
440 g_debug("network state changed: %s",
4
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
441 pk_network_enum_to_string(self->network_state));
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
442 check_inhibit(self);
0
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
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
445 static void
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
446 on_updates_changed(PkControl *control, 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 PuiBackend *self = user_data;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
449
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 * 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
452 * signals is coalesced
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
453 */
4
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
454 if (!self->inhibited) {
0
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
455 if (self->periodic_check_id != 0) {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
456 g_source_remove(self->periodic_check_id);
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 self->periodic_check_id =
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
459 g_timeout_add_seconds(PUI_STARTUP_DELAY, periodic_check,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
460 self);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
461 }
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
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
464 static void
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
465 on_restart_schedule(PkControl *control, gpointer user_data)
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
466 {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
467 PuiBackend *self = user_data;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
468
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
469 g_debug("emitting signal restart-required");
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
470 g_signal_emit(self, signals[RESTART_REQUIRED], 0);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
471 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
472
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
473 static void
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
474 pui_backend_init_async(GAsyncInitable *initable, int io_priority,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
475 GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
476 {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
477 PuiBackend *self = PUI_BACKEND(initable);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
478 GTask *task;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
479
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
480 task = g_task_new(G_OBJECT(initable), cancellable, callback, user_data);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
481 g_task_set_priority(task, io_priority);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
482 g_task_set_task_data(task, g_object_ref(self),
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
483 (GDestroyNotify)g_object_unref);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
484
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
485 pk_control_get_properties_async(self->pk_control, cancellable,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
486 on_get_properties_finished, task);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
487 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
488
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
489 static gboolean
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
490 pui_backend_init_finish(GAsyncInitable *initable, GAsyncResult *result,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
491 GError **errorp)
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
492 {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
493 PuiBackend *self = PUI_BACKEND(initable);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
494 GTask *task = G_TASK(result);
5
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
495 UpDeviceKind kind;
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
496 gdouble percentage;
0
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
497
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
498 if (!g_task_propagate_boolean(task, errorp)) {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
499 return (FALSE);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
500 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
501
5
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
502 if (self->up_device != NULL) {
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
503 /* get the kind of device and charge percentage */
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
504 g_object_get(self->up_device, "kind", &kind, "percentage",
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
505 &percentage, NULL);
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
506 if ((kind == UP_DEVICE_KIND_BATTERY) ||
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
507 (kind == UP_DEVICE_KIND_UPS)) {
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
508 self->is_battery_low =
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
509 (percentage < LOW_BATTERY_THRESHOLD);
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
510 }
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
511
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
512 /* get notification if the charge percentage changes */
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
513 g_signal_connect(self->up_device, "notify::percentage",
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
514 G_CALLBACK(on_notify_device_charge_percentage), self);
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
515 }
a4020e99e550 Do not check for updates if the battery is low
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 4
diff changeset
516
0
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
517 /* get notification when the network state changes */
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
518 g_signal_connect(self->pk_control, "notify::network-state",
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
519 G_CALLBACK(on_notify_network_state), self);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
520 /* get notifications when the available package updates change */
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
521 g_signal_connect(self->pk_control, "updates-changed",
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
522 G_CALLBACK(on_updates_changed), self);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
523 /* get notifications when an application restart is required */
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
524 g_signal_connect(self->pk_control, "restart-schedule",
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
525 G_CALLBACK(on_restart_schedule), self);
4
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
526
3d72ca76538d Add setting to control whether to use a mobile connection
Guido Berhoerster <guido+pui@berhoerster.name>
parents: 0
diff changeset
527 check_inhibit(self);
0
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
528
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
529 return (TRUE);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
530 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
531
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
532 static void
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
533 pui_backend_async_initable_iface_init(gpointer g_iface, gpointer iface_data)
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
534 {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
535 GAsyncInitableIface *iface = g_iface;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
536
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
537 iface->init_async = pui_backend_init_async;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
538 iface->init_finish = pui_backend_init_finish;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
539 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
540
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
541 void
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
542 pui_backend_new_async(GCancellable *cancellable, GAsyncReadyCallback callback,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
543 gpointer user_data)
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
544 {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
545 g_async_initable_new_async(PUI_TYPE_BACKEND, G_PRIORITY_DEFAULT,
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
546 cancellable, callback, user_data, NULL);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
547 }
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
548
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
549 PuiBackend *
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
550 pui_backend_new_finish(GAsyncResult *result, GError **errorp)
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
551 {
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
552 GObject *object;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
553 GObject *source_object;
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
554
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
555 source_object = g_async_result_get_source_object(result);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
556 object = g_async_initable_new_finish(G_ASYNC_INITABLE(source_object),
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
557 result, errorp);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
558 g_object_unref(source_object);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
559
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
560 return ((object != NULL) ? PUI_BACKEND(object) : NULL);
6884bb8130ca Initial revision
Guido Berhoerster <guido+pui@berhoerster.name>
parents:
diff changeset
561 }