Use signal on dbus interface for cloud provider
Julius Härtl
6 years ago
29 | 29 | <method name='GetPath'> |
30 | 30 | <arg type='s' name='path' direction='out'/> |
31 | 31 | </method> |
32 | <signal name="CloudProviderChanged"></signal> | |
32 | 33 | </interface> |
33 | 34 | </node> |
40 | 40 | |
41 | 41 | enum { |
42 | 42 | CHANGED, |
43 | CHANGED_NOTIFY, | |
43 | 44 | LAST_SIGNAL |
44 | 45 | }; |
45 | 46 | |
68 | 69 | |
69 | 70 | variant_dict = g_variant_get_child_value (variant_tuple, 0); |
70 | 71 | variant = g_variant_get_child_value (variant_dict, 0); |
71 | priv->icon = g_icon_deserialize (variant); | |
72 | priv->icon = g_icon_deserialize (variant_dict); | |
72 | 73 | g_variant_unref (variant); |
73 | 74 | g_variant_unref (variant_dict); |
74 | 75 | |
173 | 174 | } |
174 | 175 | } |
175 | 176 | |
177 | void | |
178 | cloud_provider_notify (CloudProvider *self) | |
179 | { | |
180 | g_print("cloud-provider-notify\n"); | |
181 | g_signal_emit_by_name (self, "changed-notify"); | |
182 | } | |
183 | ||
176 | 184 | static void |
177 | 185 | on_proxy_created (GObject *source_object, |
178 | 186 | GAsyncResult *res, |
195 | 203 | |
196 | 204 | priv->proxy = proxy; |
197 | 205 | |
198 | cloud_provider_update (self); | |
206 | g_signal_connect_swapped(priv->proxy, "cloud-provider-changed", G_CALLBACK(cloud_provider_update), self); | |
207 | g_signal_connect_swapped(priv->proxy, "notify", G_CALLBACK(cloud_provider_notify), self); | |
199 | 208 | } |
200 | 209 | |
201 | 210 | static void |
289 | 298 | g_cclosure_marshal_generic, |
290 | 299 | G_TYPE_NONE, |
291 | 300 | 0); |
301 | gSignals [CHANGED_NOTIFY] = | |
302 | g_signal_new ("changed-notify", | |
303 | G_TYPE_FROM_CLASS (klass), | |
304 | G_SIGNAL_RUN_LAST, | |
305 | 0, | |
306 | NULL, | |
307 | NULL, | |
308 | g_cclosure_marshal_generic, | |
309 | G_TYPE_NONE, | |
310 | 0); | |
292 | 311 | } |
293 | 312 | |
294 | 313 | static void |
346 | 365 | |
347 | 366 | return priv->path; |
348 | 367 | } |
368 | ||
369 | gchar * | |
370 | cloud_provider_get_owner (CloudProvider *self) | |
371 | { | |
372 | CloudProviderPrivate *priv = cloud_provider_get_instance_private (self); | |
373 | ||
374 | return g_dbus_proxy_get_name_owner (G_DBUS_PROXY(priv->proxy)); | |
375 | } | |
376 | ||
377 | gboolean cloud_provider_is_available(CloudProvider *self) | |
378 | { | |
379 | GIcon *icon; | |
380 | gchar *name; | |
381 | guint status; | |
382 | ||
383 | name = cloud_provider_get_name (self); | |
384 | icon = cloud_provider_get_icon (self); | |
385 | status = cloud_provider_get_status (self); | |
386 | if (name == NULL || icon == NULL || status == CLOUD_PROVIDER_STATUS_INVALID) | |
387 | return FALSE; | |
388 | return TRUE; | |
389 | } |
63 | 63 | GMenuModel *cloud_provider_get_menu_model (CloudProvider *self); |
64 | 64 | GActionGroup* cloud_provider_get_action_group (CloudProvider *self); |
65 | 65 | gchar *cloud_provider_get_path (CloudProvider *self); |
66 | ||
66 | gchar *cloud_provider_get_owner (CloudProvider *self); | |
67 | 67 | G_END_DECLS |
68 | gboolean cloud_provider_is_available(CloudProvider *self); | |
68 | 69 | |
69 | 70 | #endif /* CLOUD_PROVIDER_H */ |
46 | 46 | on_cloud_provider_changed (CloudProvider *cloud_provider, |
47 | 47 | CloudProviderManager *self) |
48 | 48 | { |
49 | GIcon *icon; | |
50 | gchar *name; | |
51 | guint status; | |
52 | ||
53 | name = cloud_provider_get_name (cloud_provider); | |
54 | icon = cloud_provider_get_icon (cloud_provider); | |
55 | status = cloud_provider_get_status (cloud_provider); | |
56 | if (name == NULL || icon == NULL || status == CLOUD_PROVIDER_STATUS_INVALID) | |
57 | return; | |
58 | ||
49 | if(!cloud_provider_is_available(cloud_provider)) | |
50 | return; | |
59 | 51 | g_signal_emit_by_name (self, "changed", NULL); |
60 | 52 | } |
53 | ||
54 | static void | |
55 | on_cloud_provider_changed_notify (CloudProvider *cloud_provider, CloudProviderManager *self) | |
56 | { | |
57 | if(!cloud_provider_is_available(cloud_provider)) | |
58 | return; | |
59 | ||
60 | // update manager to remove cloud providers after owner disappeared | |
61 | if(cloud_provider_get_owner(cloud_provider) == NULL) { | |
62 | cloud_provider_manager_update(self); | |
63 | g_signal_emit_by_name (self, "changed", NULL); | |
64 | } | |
65 | } | |
66 | ||
61 | 67 | |
62 | 68 | static void |
63 | 69 | on_bus_acquired (GDBusConnection *connection, |
89 | 95 | gpointer user_data) |
90 | 96 | { |
91 | 97 | CloudProviderManagerPrivate *priv = cloud_provider_manager_get_instance_private (CLOUD_PROVIDER_MANAGER (user_data)); |
92 | g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(priv->skeleton)); | |
98 | if (g_dbus_interface_skeleton_has_connection (G_DBUS_INTERFACE_SKELETON (priv->skeleton), connection)) | |
99 | { | |
100 | g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(priv->skeleton)); | |
101 | } | |
93 | 102 | } |
94 | 103 | |
95 | 104 | /** |
206 | 215 | cloud_provider = cloud_provider_new (bus_name, object_path); |
207 | 216 | g_signal_connect (cloud_provider, "changed", |
208 | 217 | G_CALLBACK (on_cloud_provider_changed), self); |
218 | g_signal_connect (cloud_provider, "changed-notify", | |
219 | G_CALLBACK (on_cloud_provider_changed_notify), self); | |
209 | 220 | priv->providers = g_list_append (priv->providers, cloud_provider); |
210 | 221 | |
211 | 222 | success = TRUE; |
51 | 51 | g_print ("##############\n"); |
52 | 52 | for (l = providers; l != NULL; l = l->next) |
53 | 53 | { |
54 | if(!cloud_provider_is_available(CLOUD_PROVIDER (l->data))) { | |
55 | continue; | |
56 | } | |
54 | 57 | provider_status = cloud_provider_get_status (CLOUD_PROVIDER (l->data)); |
55 | 58 | switch (provider_status) |
56 | 59 | { |
1 | 1 | #include <stdlib.h> |
2 | 2 | #include <gio/gio.h> |
3 | 3 | #include <cloudprovider.h> |
4 | #include <cloudprovidermanager.h> | |
5 | 4 | |
6 | 5 | #define TIMEOUT 2000 |
7 | 6 | |
21 | 20 | gint status; |
22 | 21 | GIcon *icon; |
23 | 22 | gchar *path; |
24 | CloudProviderManager1 *manager_proxy; | |
25 | 23 | guint timeout_handler; |
24 | GDBusConnection *connection; | |
26 | 25 | }; |
27 | 26 | |
28 | 27 | |
37 | 36 | g_free (self->name); |
38 | 37 | g_free (self->path); |
39 | 38 | g_clear_object (&self->icon); |
40 | g_clear_object (&self->manager_proxy); | |
41 | 39 | |
42 | 40 | G_OBJECT_CLASS (test_cloud_provider_parent_class)->finalize (object); |
43 | 41 | } |
75 | 73 | test_cloud_provider_set_status (TestCloudProvider *self, |
76 | 74 | gint status) |
77 | 75 | { |
78 | /* Inform the manager that the provider changed */ | |
79 | 76 | self->status = status; |
80 | cloud_provider_manager1_call_cloud_provider_changed(self->manager_proxy, | |
81 | NULL, | |
82 | NULL, | |
83 | NULL); | |
84 | 77 | } |
85 | 78 | |
86 | 79 | /* ---------------------------------------------------------------------------------------------------- */ |
265 | 258 | handle_method_call, |
266 | 259 | }; |
267 | 260 | |
261 | static gboolean | |
262 | test_cloud_provider_notify_change (gpointer user_data) | |
263 | { | |
264 | TestCloudProvider *cloud_provider = (TestCloudProvider *)user_data; | |
265 | GRand *rand; | |
266 | gint new_status; | |
267 | ||
268 | g_print("Emit changed signal for cloud provider\n"); | |
269 | ||
270 | rand = g_rand_new (); | |
271 | new_status = g_rand_int_range (rand, | |
272 | CLOUD_PROVIDER_STATUS_IDLE, | |
273 | CLOUD_PROVIDER_STATUS_ERROR + 1); | |
274 | ||
275 | test_cloud_provider_set_status (cloud_provider, new_status); | |
276 | ||
277 | g_dbus_connection_emit_signal (cloud_provider->connection, | |
278 | NULL, | |
279 | "/org/freedesktop/CloudProviderServerExample", | |
280 | "org.freedesktop.CloudProvider1", | |
281 | "CloudProviderChanged", | |
282 | NULL, | |
283 | NULL /*error*/); | |
284 | ||
285 | return TRUE; | |
286 | } | |
287 | ||
268 | 288 | static void |
269 | 289 | on_bus_acquired (GDBusConnection *connection, |
270 | 290 | const gchar *name, |
271 | 291 | gpointer user_data) |
272 | 292 | { |
273 | CloudProvider *cloud_provider = user_data; | |
293 | TestCloudProvider *cloud_provider = user_data; | |
274 | 294 | guint registration_id; |
275 | 295 | |
296 | cloud_provider->connection = connection; | |
276 | 297 | |
277 | 298 | g_debug ("Registering cloud provider server 'MyCloud'\n"); |
278 | 299 | |
294 | 315 | const gchar *name, |
295 | 316 | gpointer user_data) |
296 | 317 | { |
318 | TestCloudProvider *cloud_provider = (TestCloudProvider *)user_data; | |
319 | cloud_provider->timeout_handler = g_timeout_add (TIMEOUT, | |
320 | (GSourceFunc) test_cloud_provider_notify_change, | |
321 | cloud_provider); | |
297 | 322 | } |
298 | 323 | |
299 | 324 | static void |
304 | 329 | exit (1); |
305 | 330 | } |
306 | 331 | |
307 | static gboolean | |
308 | change_provider (gpointer user_data) | |
309 | { | |
310 | TestCloudProvider *cloud_provider = (TestCloudProvider *)user_data; | |
311 | GRand *rand; | |
312 | gint new_status; | |
313 | ||
314 | g_print("Send change_provider message to bus\n"); | |
315 | ||
316 | rand = g_rand_new (); | |
317 | new_status = g_rand_int_range (rand, | |
318 | CLOUD_PROVIDER_STATUS_IDLE, | |
319 | CLOUD_PROVIDER_STATUS_ERROR + 1); | |
320 | ||
321 | test_cloud_provider_set_status (cloud_provider, new_status); | |
322 | ||
323 | return TRUE; | |
324 | } | |
325 | ||
326 | static void | |
327 | on_manager_proxy_created (GObject *source_object, | |
328 | GAsyncResult *res, | |
329 | gpointer user_data) | |
330 | { | |
331 | TestCloudProvider *cloud_provider = user_data; | |
332 | GError *error = NULL; | |
333 | ||
334 | cloud_provider->manager_proxy = cloud_provider_manager1_proxy_new_for_bus_finish (res, &error); | |
335 | if (error != NULL) | |
336 | g_warning ("Error creating proxy for cloud provider manager %s", error->message); | |
337 | else | |
338 | g_print ("Manager proxy created for 'MyCloud'\n"); | |
339 | ||
340 | cloud_provider->timeout_handler = g_timeout_add (TIMEOUT, | |
341 | (GSourceFunc) change_provider, | |
342 | cloud_provider); | |
343 | } | |
344 | ||
345 | 332 | int |
346 | 333 | main (int argc, char *argv[]) |
347 | 334 | { |
348 | 335 | GMainLoop *loop; |
349 | CloudProvider *cloud_provider; | |
336 | TestCloudProvider *cloud_provider; | |
350 | 337 | guint owner_id; |
351 | 338 | |
352 | 339 | cloud_provider = g_object_new (test_cloud_provider_get_type (), NULL); |
360 | 347 | cloud_provider, |
361 | 348 | NULL); |
362 | 349 | |
363 | /* Create CloudProviderManager proxy for exporting cloud provider changes */ | |
364 | cloud_provider_manager1_proxy_new_for_bus(G_BUS_TYPE_SESSION, | |
365 | G_DBUS_PROXY_FLAGS_NONE, | |
366 | "org.freedesktop.CloudProviderManager", | |
367 | "/org/freedesktop/CloudProviderManager", | |
368 | NULL, | |
369 | on_manager_proxy_created, | |
370 | cloud_provider); | |
371 | ||
372 | ||
373 | 350 | loop = g_main_loop_new (NULL, FALSE); |
374 | 351 | g_main_loop_run (loop); |
375 | 352 |