Codebase list libcloudproviders / b7e81d9
Remove gtk namespace from classes Julius Härtl 6 years ago
14 changed file(s) with 858 addition(s) and 855 deletion(s). Raw diff Collapse all Expand all
2020 conf = configuration_data ()
2121 conf.set_quoted ('VERSION', meson.project_version ())
2222 conf.set_quoted ('PACKAGE_VERSION', meson.project_version ())
23 conf.set_quoted ('GETTEXT_PACKAGE', 'gtkcloudproviders')
23 conf.set_quoted ('GETTEXT_PACKAGE', 'cloudproviders')
2424
2525 subdir ( 'src' )
2626 subdir ( 'test' )
0 /* cloudprovider.c
1 *
2 * Copyright (C) 2015 Carlos Soriano <csoriano@gnome.org>
3 * Copyright (C) 2017 Julius Haertl <jus@bitgrid.net>
4 *
5 * This file is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This file is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "cloudprovider.h"
20 #include "cloudprovider-generated.h"
21
22
23 typedef struct
24 {
25 gchar *name;
26 gchar *path;
27 CloudProviderStatus status;
28 GIcon *icon;
29 GMenuModel *menu_model;
30 GActionGroup *action_group;
31
32 GDBusConnection *bus;
33 CloudProvider1 *proxy;
34 gchar *bus_name;
35 gchar *object_path;
36 GCancellable *cancellable;
37 } CloudProviderPrivate;
38
39 G_DEFINE_TYPE_WITH_PRIVATE (CloudProvider, cloud_provider, G_TYPE_OBJECT)
40
41 enum {
42 CHANGED,
43 LAST_SIGNAL
44 };
45
46 static guint gSignals [LAST_SIGNAL];
47
48 static void
49 on_get_icon (GObject *source_object,
50 GAsyncResult *res,
51 gpointer user_data)
52 {
53 CloudProvider *self = CLOUD_PROVIDER (user_data);
54 CloudProviderPrivate *priv = cloud_provider_get_instance_private (self);
55 GError *error = NULL;
56 GVariant *variant_tuple;
57 GVariant *variant_dict;
58 GVariant *variant;
59
60 g_clear_object (&priv->icon);
61
62 cloud_provider1_call_get_icon_finish (priv->proxy, &variant_tuple, res, &error);
63 if (error != NULL)
64 {
65 g_warning ("Error getting the provider icon %s", error->message);
66 goto out;
67 }
68 g_print ("variant tuple %s\n", g_variant_print (variant_tuple, TRUE));
69
70 variant_dict = g_variant_get_child_value (variant_tuple, 0);
71 variant = g_variant_get_child_value (variant_dict, 0);
72 priv->icon = g_icon_deserialize (variant);
73 g_variant_unref (variant);
74 g_variant_unref (variant_dict);
75
76 out:
77 g_variant_unref (variant_tuple);
78 g_signal_emit_by_name (self, "changed");
79 }
80
81 static void
82 on_get_name (GObject *source_object,
83 GAsyncResult *res,
84 gpointer user_data)
85 {
86 CloudProvider *self = CLOUD_PROVIDER (user_data);
87 CloudProviderPrivate *priv = cloud_provider_get_instance_private (self);
88 GError *error = NULL;
89
90 if (priv->name != NULL)
91 g_free (priv->name);
92
93 cloud_provider1_call_get_name_finish (priv->proxy, &priv->name, res, &error);
94 if (error != NULL)
95 {
96 g_warning ("Error getting the provider name %s", error->message);
97 return;
98 }
99 g_signal_emit_by_name (self, "changed");
100 }
101
102
103 static void
104 on_get_path (GObject *source_object,
105 GAsyncResult *res,
106 gpointer user_data)
107 {
108 CloudProvider *self = CLOUD_PROVIDER (user_data);
109 CloudProviderPrivate *priv = cloud_provider_get_instance_private (self);
110 GError *error = NULL;
111
112 if (priv->path != NULL)
113 g_free (priv->path);
114
115 cloud_provider1_call_get_path_finish (priv->proxy, &priv->path, res, &error);
116 if (error != NULL)
117 {
118 g_warning ("Error getting the provider name %s", error->message);
119 return;
120 }
121 g_signal_emit_by_name (self, "changed");
122 }
123
124 static void
125 on_get_status (GObject *source_object,
126 GAsyncResult *res,
127 gpointer user_data)
128 {
129 CloudProvider *self = CLOUD_PROVIDER (user_data);
130 CloudProviderPrivate *priv = cloud_provider_get_instance_private (self);
131 GError *error = NULL;
132 gint status;
133
134 cloud_provider1_call_get_status_finish (priv->proxy, &status, res, &error);
135 if (error != NULL)
136 {
137 g_warning ("Error getting the provider name %s", error->message);
138 return;
139 }
140 priv->status = status;
141 g_signal_emit_by_name (self, "changed");
142 }
143
144 void
145 cloud_provider_update (CloudProvider *self)
146 {
147 CloudProviderPrivate *priv = cloud_provider_get_instance_private (self);
148
149 if (priv->proxy != NULL)
150 {
151 cloud_provider1_call_get_name (priv->proxy,
152 NULL,
153 (GAsyncReadyCallback) on_get_name,
154 self);
155 cloud_provider1_call_get_status (priv->proxy,
156 NULL,
157 (GAsyncReadyCallback) on_get_status,
158 self);
159 cloud_provider1_call_get_icon (priv->proxy,
160 NULL,
161 (GAsyncReadyCallback) on_get_icon,
162 self);
163 cloud_provider1_call_get_path (priv->proxy,
164 NULL,
165 (GAsyncReadyCallback) on_get_path,
166 self);
167
168 priv->menu_model = (GMenuModel*) g_dbus_menu_model_get (priv->bus,
169 priv->bus_name,
170 priv->object_path);
171 priv->action_group = (GActionGroup*) g_dbus_action_group_get (priv->bus,
172 priv->bus_name,
173 priv->object_path);
174 }
175 }
176
177 static void
178 on_proxy_created (GObject *source_object,
179 GAsyncResult *res,
180 gpointer user_data)
181 {
182 GError *error = NULL;
183 CloudProvider *self;
184 CloudProviderPrivate *priv;
185 CloudProvider1 *proxy;
186
187 proxy = cloud_provider1_proxy_new_for_bus_finish (res, &error);
188 if (error != NULL)
189 {
190 if (error->code != G_IO_ERROR_CANCELLED)
191 g_warning ("Error creating proxy for cloud provider %s", error->message);
192 return;
193 }
194 self = CLOUD_PROVIDER (user_data);
195 priv = cloud_provider_get_instance_private (self);
196
197 priv->proxy = proxy;
198
199 cloud_provider_update (self);
200 }
201
202 static void
203 on_bus_acquired (GObject *source_object,
204 GAsyncResult *res,
205 gpointer user_data)
206 {
207 GError *error = NULL;
208 CloudProvider *self;
209 GDBusConnection *bus;
210 CloudProviderPrivate *priv;
211
212 bus = g_bus_get_finish (res, &error);
213 if (error != NULL)
214 {
215 if (error->code != G_IO_ERROR_CANCELLED)
216 g_warning ("Error acdquiring bus for cloud provider %s", error->message);
217 return;
218 }
219
220 self = CLOUD_PROVIDER (user_data);
221 priv = cloud_provider_get_instance_private (user_data);
222 priv->bus = bus;
223 g_clear_object (&priv->cancellable);
224 priv->cancellable = g_cancellable_new ();
225 cloud_provider1_proxy_new (priv->bus,
226 G_DBUS_PROXY_FLAGS_NONE,
227 priv->bus_name,
228 priv->object_path,
229 priv->cancellable,
230 on_proxy_created,
231 self);
232 }
233
234 CloudProvider*
235 cloud_provider_new (const gchar *bus_name,
236 const gchar *object_path)
237 {
238 CloudProvider *self;
239 CloudProviderPrivate *priv;
240
241 self = g_object_new (TYPE_CLOUD_PROVIDER, NULL);
242 priv = cloud_provider_get_instance_private (self);
243
244 priv->bus_name = g_strdup (bus_name);
245 priv->object_path = g_strdup (object_path);
246 priv->cancellable = g_cancellable_new ();
247 priv->status = CLOUD_PROVIDER_STATUS_INVALID;
248 g_bus_get (G_BUS_TYPE_SESSION,
249 priv->cancellable,
250 on_bus_acquired,
251 self);
252
253 return self;
254 }
255
256 static void
257 cloud_provider_finalize (GObject *object)
258 {
259 CloudProvider *self = (CloudProvider *)object;
260 CloudProviderPrivate *priv = cloud_provider_get_instance_private (self);
261
262 g_cancellable_cancel (priv->cancellable);
263 g_clear_object (&priv->cancellable);
264 g_free (priv->name);
265 g_free (priv->path);
266 g_clear_object (&priv->icon);
267 g_clear_object (&priv->action_group);
268 g_clear_object (&priv->bus);
269 g_clear_object (&priv->proxy);
270 g_free (priv->bus_name);
271 g_free (priv->object_path);
272
273 G_OBJECT_CLASS (cloud_provider_parent_class)->finalize (object);
274 }
275
276 static void
277 cloud_provider_class_init (CloudProviderClass *klass)
278 {
279 GObjectClass *object_class = G_OBJECT_CLASS (klass);
280
281 object_class->finalize = cloud_provider_finalize;
282
283 gSignals [CHANGED] =
284 g_signal_new ("changed",
285 G_TYPE_FROM_CLASS (klass),
286 G_SIGNAL_RUN_LAST,
287 0,
288 NULL,
289 NULL,
290 g_cclosure_marshal_generic,
291 G_TYPE_NONE,
292 0);
293 }
294
295 static void
296 cloud_provider_init (CloudProvider *self)
297 {
298 CloudProviderPrivate *priv = cloud_provider_get_instance_private (self);
299
300 priv->status = CLOUD_PROVIDER_STATUS_INVALID;
301 }
302
303 gchar*
304 cloud_provider_get_name (CloudProvider *self)
305 {
306 CloudProviderPrivate *priv = cloud_provider_get_instance_private (self);
307
308 return priv->name;
309 }
310
311 CloudProviderStatus
312 cloud_provider_get_status (CloudProvider *self)
313 {
314 CloudProviderPrivate *priv = cloud_provider_get_instance_private (self);
315
316 return priv->status;
317 }
318
319 GIcon*
320 cloud_provider_get_icon (CloudProvider *self)
321 {
322 CloudProviderPrivate *priv = cloud_provider_get_instance_private (self);
323
324 return priv->icon;
325 }
326
327 GMenuModel*
328 cloud_provider_get_menu_model (CloudProvider *self)
329 {
330 CloudProviderPrivate *priv = cloud_provider_get_instance_private (self);
331
332 return priv->menu_model;
333 }
334
335 GActionGroup*
336 cloud_provider_get_action_group (CloudProvider *self)
337 {
338 CloudProviderPrivate *priv = cloud_provider_get_instance_private (self);
339
340 return priv->action_group;
341 }
342
343 gchar *
344 cloud_provider_get_path (CloudProvider *self)
345 {
346 CloudProviderPrivate *priv = cloud_provider_get_instance_private (self);
347
348 return priv->path;
349 }
0 /* cloudprovider.h
1 *
2 * Copyright (C) 2015 Carlos Soriano <csoriano@gnome.org>
3 * Copyright (C) 2017 Julius Haertl <jus@bitgrid.net>
4 *
5 * This file is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This file is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #ifndef CLOUD_PROVIDER_H
20 #define CLOUD_PROVIDER_H
21
22 #include <gio/gio.h>
23 #include "cloudprovider-generated.h"
24
25 G_BEGIN_DECLS
26
27 typedef enum {
28 CLOUD_PROVIDER_STATUS_INVALID,
29 CLOUD_PROVIDER_STATUS_IDLE,
30 CLOUD_PROVIDER_STATUS_SYNCING,
31 CLOUD_PROVIDER_STATUS_ERROR
32 } CloudProviderStatus;
33
34 #define TYPE_CLOUD_PROVIDER (cloud_provider_get_type())
35 #define CLOUD_PROVIDER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_CLOUD_PROVIDER, CloudProvider))
36 #define CLOUD_PROVIDER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_CLOUD_PROVIDER, CloudProviderClass))
37 #define IS_CLOUD_PROVIDER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_CLOUD_PROVIDER))
38 #define IS_CLOUD_PROVIDER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_CLOUD_PROVIDER))
39 #define CLOUD_PROVIDER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_CLOUD_PROVIDER, CloudProviderClass))
40
41 typedef struct _CloudProvider CloudProvider;
42 typedef struct _CloudProviderClass CloudProviderClass;
43
44
45 struct _CloudProviderClass
46 {
47 GObjectClass parent_class;
48 };
49
50 struct _CloudProvider
51 {
52 GObject parent_instance;
53 };
54
55
56 GType cloud_provider_get_type (void) G_GNUC_CONST;
57 CloudProvider *cloud_provider_new (const gchar *bus_name,
58 const gchar *object_path);
59
60 gchar* cloud_provider_get_name (CloudProvider *self);
61 CloudProviderStatus cloud_provider_get_status (CloudProvider *self);
62 GIcon *cloud_provider_get_icon (CloudProvider *self);
63 GMenuModel *cloud_provider_get_menu_model (CloudProvider *self);
64 GActionGroup* cloud_provider_get_action_group (CloudProvider *self);
65 gchar *cloud_provider_get_path (CloudProvider *self);
66
67 G_END_DECLS
68
69 #endif /* CLOUD_PROVIDER_H */
0 /* cloudprovidermanager.c
1 *
2 * Copyright (C) 2015 Carlos Soriano <csoriano@gnome.org>
3 * Copyright (C) 2017 Julius Haertl <jus@bitgrid.net>
4 *
5 * This file is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This file is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "cloudprovidermanager.h"
20 #include "cloudprovider.h"
21 #include <glib.h>
22 #include <glib/gprintf.h>
23 #include <gio/gio.h>
24
25 #define KEY_FILE_GROUP "Cloud Provider"
26
27 typedef struct
28 {
29 GList *providers;
30 guint dbus_owner_id;
31 GDBusNodeInfo *dbus_node_info;
32 } CloudProviderManagerPrivate;
33
34 G_DEFINE_TYPE_WITH_PRIVATE (CloudProviderManager, cloud_provider_manager, G_TYPE_OBJECT)
35
36 enum
37 {
38 CHANGED,
39 LAST_SIGNAL
40 };
41
42 static guint gSignals [LAST_SIGNAL];
43
44 static const gchar manager_xml[] =
45 "<node>"
46 " <interface name='org.freedesktop.CloudProviderManager1'>"
47 " <method name='CloudProviderChanged'>"
48 " </method>"
49 " </interface>"
50 "</node>";
51
52 static void
53 handle_method_call (GDBusConnection *connection,
54 const gchar *sender,
55 const gchar *object_path,
56 const gchar *interface_name,
57 const gchar *method_name,
58 GVariant *parameters,
59 GDBusMethodInvocation *invocation,
60 gpointer user_data)
61 {
62 if (g_strcmp0 (method_name, "CloudProviderChanged") == 0)
63 {
64 cloud_provider_manager_update (CLOUD_PROVIDER_MANAGER (user_data));
65 }
66 }
67
68 static const GDBusInterfaceVTable interface_vtable =
69 {
70 handle_method_call,
71 };
72
73 static void
74 on_bus_acquired (GDBusConnection *connection,
75 const gchar *name,
76 gpointer user_data)
77 {
78 CloudProviderManager *self = user_data;
79 CloudProviderManagerPrivate *priv = cloud_provider_manager_get_instance_private (self);
80 guint registration_id;
81
82 g_debug ("Registering cloud provider server 'MyCloud'\n");
83 registration_id = g_dbus_connection_register_object (connection,
84 CLOUD_PROVIDER_MANAGER_DBUS_PATH,
85 priv->dbus_node_info->interfaces[0],
86 &interface_vtable,
87 self,
88 NULL, /* user_data_free_func */
89 NULL); /* GError** */
90 g_assert (registration_id > 0);
91
92 /* In case some provider updated before adquiring the bus */
93 cloud_provider_manager_update (CLOUD_PROVIDER_MANAGER (user_data));
94 }
95
96 static void
97 on_name_acquired (GDBusConnection *connection,
98 const gchar *name,
99 gpointer user_data)
100 {
101 }
102
103 static void
104 on_name_lost (GDBusConnection *connection,
105 const gchar *name,
106 gpointer user_data)
107 {
108 }
109
110 /**
111 * cloud_provider_manager_dup_singleton
112 * Returns: (transfer none): A manager singleton
113 */
114 CloudProviderManager *
115 cloud_provider_manager_dup_singleton (void)
116 {
117 static GObject *self = NULL;
118
119 if (self == NULL)
120 {
121 CloudProviderManagerPrivate *priv;
122
123 self = g_object_new (TYPE_CLOUD_PROVIDER_MANAGER, NULL);
124 priv = cloud_provider_manager_get_instance_private (CLOUD_PROVIDER_MANAGER (self));
125
126 /* Export the interface we listen to, so clients can request properties of
127 * the cloud provider such as name, status or icon */
128 priv->dbus_node_info = g_dbus_node_info_new_for_xml (manager_xml, NULL);
129 g_assert (priv->dbus_node_info != NULL);
130
131 priv->dbus_owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
132 CLOUD_PROVIDER_MANAGER_DBUS_NAME,
133 G_BUS_NAME_OWNER_FLAGS_NONE,
134 on_bus_acquired,
135 on_name_acquired,
136 on_name_lost,
137 self,
138 NULL);
139 return CLOUD_PROVIDER_MANAGER (self);
140 }
141 else
142 {
143 return g_object_ref (self);
144 }
145 }
146
147 static void
148 cloud_provider_manager_finalize (GObject *object)
149 {
150 CloudProviderManager *self = (CloudProviderManager *)object;
151 CloudProviderManagerPrivate *priv = cloud_provider_manager_get_instance_private (self);
152
153 g_list_free_full (priv->providers, g_object_unref);
154 g_bus_unown_name (priv->dbus_owner_id);
155 g_dbus_node_info_unref (priv->dbus_node_info);
156
157 G_OBJECT_CLASS (cloud_provider_manager_parent_class)->finalize (object);
158 }
159
160 static void
161 cloud_provider_manager_class_init (CloudProviderManagerClass *klass)
162 {
163 GObjectClass *object_class = G_OBJECT_CLASS (klass);
164
165 object_class->finalize = cloud_provider_manager_finalize;
166
167 gSignals [CHANGED] =
168 g_signal_new ("changed",
169 G_TYPE_FROM_CLASS (klass),
170 G_SIGNAL_RUN_LAST,
171 0,
172 NULL,
173 NULL,
174 g_cclosure_marshal_generic,
175 G_TYPE_NONE,
176 0);
177 }
178
179 static void
180 cloud_provider_manager_init (CloudProviderManager *self)
181 {
182 }
183
184 /**
185 * cloud_provider_manager_get_providers
186 * @manager: A CloudProviderManager
187 * Returns: (transfer none): The list of providers.
188 */
189 GList*
190 cloud_provider_manager_get_providers (CloudProviderManager *manager)
191 {
192 CloudProviderManagerPrivate *priv = cloud_provider_manager_get_instance_private (manager);
193
194 return priv->providers;
195 }
196
197 static void
198 on_cloud_provider_changed (CloudProvider *cloud_provider,
199 CloudProviderManager *self)
200 {
201 GIcon *icon;
202 gchar *name;
203 guint status;
204
205 name = cloud_provider_get_name (cloud_provider);
206 icon = cloud_provider_get_icon (cloud_provider);
207 status = cloud_provider_get_status (cloud_provider);
208 if (name == NULL || icon == NULL || status == CLOUD_PROVIDER_STATUS_INVALID)
209 return;
210
211 g_signal_emit_by_name (self, "changed", NULL);
212 }
213
214 static void
215 load_cloud_provider (CloudProviderManager *self,
216 GFile *file)
217 {
218 CloudProviderManagerPrivate *priv = cloud_provider_manager_get_instance_private (self);
219 GKeyFile *key_file;
220 gchar *path;
221 GError *error = NULL;
222 gchar *bus_name;
223 gchar *object_path;
224 gboolean success = FALSE;
225 CloudProvider *cloud_provider;
226
227 key_file = g_key_file_new ();
228 path = g_file_get_path (file);
229 g_key_file_load_from_file (key_file, path, G_KEY_FILE_NONE, &error);
230 if (error != NULL)
231 goto out;
232
233 if (!g_key_file_has_group (key_file, KEY_FILE_GROUP))
234 goto out;
235
236 bus_name = g_key_file_get_string (key_file, KEY_FILE_GROUP, "BusName", &error);
237 if (error != NULL)
238 goto out;
239 object_path = g_key_file_get_string (key_file, KEY_FILE_GROUP, "ObjectPath", &error);
240 if (error != NULL)
241 goto out;
242
243 g_print ("cloud provider found %s %s\n", bus_name, object_path);
244 cloud_provider = cloud_provider_new (bus_name, object_path);
245 g_signal_connect (cloud_provider, "changed",
246 G_CALLBACK (on_cloud_provider_changed), self);
247 priv->providers = g_list_append (priv->providers, cloud_provider);
248
249 success = TRUE;
250 out:
251 if (!success)
252 g_warning ("Error while loading cloud provider key file at %s", path);
253 g_key_file_free (key_file);
254 }
255
256 /**
257 * cloud_provider_manager_update
258 * @manager: A CloudProviderManager
259 */
260 void
261 cloud_provider_manager_update (CloudProviderManager *manager)
262 {
263 CloudProviderManagerPrivate *priv = cloud_provider_manager_get_instance_private (manager);
264 const gchar* const *data_dirs;
265 gint i;
266 gint len;
267 gchar *key_files_directory_path;
268 GFile *key_files_directory_file;
269 GError *error = NULL;
270 GFileEnumerator *file_enumerator;
271
272
273 g_list_free_full (priv->providers, g_object_unref);
274 priv->providers = NULL;
275
276 data_dirs = g_get_system_data_dirs ();
277 len = g_strv_length ((gchar **)data_dirs);
278
279 for (i = 0; i < len; i++)
280 {
281 GFileInfo *info;
282
283 key_files_directory_path = g_build_filename (data_dirs[i], "cloud-providers", NULL);
284 key_files_directory_file = g_file_new_for_path (key_files_directory_path);
285 file_enumerator = g_file_enumerate_children (key_files_directory_file,
286 "standard::name,standard::type",
287 G_FILE_QUERY_INFO_NONE,
288 NULL,
289 &error);
290 if (error)
291 {
292 error = NULL;
293 continue;
294 }
295
296 info = g_file_enumerator_next_file (file_enumerator, NULL, &error);
297 if (error)
298 {
299 g_warning ("Error while enumerating file %s error: %s\n", key_files_directory_path, error->message);
300 error = NULL;
301 continue;
302 }
303 while (info != NULL && error == NULL)
304 {
305 load_cloud_provider (manager, g_file_enumerator_get_child (file_enumerator, info));
306 info = g_file_enumerator_next_file (file_enumerator, NULL, &error);
307 }
308 }
309
310 }
0 /* cloudprovidermanager.h
1 *
2 * Copyright (C) 2015 Carlos Soriano <csoriano@gnome.org>
3 * Copyright (C) 2017 Julius Haertl <jus@bitgrid.net>
4 *
5 * This file is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This file is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #ifndef CLOUD_PROVIDER_MANAGER_H
19 #define CLOUD_PROVIDER_MANAGER_H
20
21 #include <gio/gio.h>
22 #include "cloudprovider.h"
23 #include "cloudprovidermanager-generated.h"
24
25 #define CLOUD_PROVIDER_MANAGER_DBUS_IFACE "org.freedesktop.CloudProviderManager1"
26 #define CLOUD_PROVIDER_MANAGER_DBUS_NAME "org.freedesktop.CloudProviderManager"
27 #define CLOUD_PROVIDER_MANAGER_DBUS_PATH "/org/freedesktop/CloudProviderManager"
28
29 G_BEGIN_DECLS
30
31 #define TYPE_CLOUD_PROVIDER_MANAGER (cloud_provider_manager_get_type())
32 #define CLOUD_PROVIDER_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_CLOUD_PROVIDER_MANAGER, CloudProviderManager))
33 #define CLOUD_PROVIDER_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_CLOUD_PROVIDER_MANAGER, CloudProviderManagerClass))
34 #define IS_CLOUD_PROVIDER_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_CLOUD_PROVIDER_MANAGER))
35 #define IS_CLOUD_PROVIDER_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_CLOUD_PROVIDER_MANAGER))
36 #define CLOUD_PROVIDER_MANAGER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_CLOUD_PROVIDER_MANAGER, CloudProviderManagerClass))
37
38 typedef struct _CloudProviderManager CloudProviderManager;
39 typedef struct _CloudProviderManagerClass CloudProviderManagerClass;
40
41 struct _CloudProviderManagerClass
42 {
43 GObjectClass parent_class;
44 };
45
46 struct _CloudProviderManager
47 {
48 GObject parent_instance;
49 };
50
51 GType cloud_provider_manager_get_type (void) G_GNUC_CONST;
52 CloudProviderManager *cloud_provider_manager_dup_singleton (void);
53 void cloud_provider_manager_update (CloudProviderManager *self);
54 GList *cloud_provider_manager_get_providers (CloudProviderManager *self);
55 //static void on_cloud_provider_changed (CloudProvider *cloud_provider, CloudProviderManager *self);
56 G_END_DECLS
57
58 #endif /* CLOUD_PROVIDER_MANAGER_H */
+0
-349
src/gtkcloudprovider.c less more
0 /* gtkcloudprovider.c
1 *
2 * Copyright (C) 2015 Carlos Soriano <csoriano@gnome.org>
3 *
4 * This file is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation; either version 3 of the
7 * License, or (at your option) any later version.
8 *
9 * This file is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "gtkcloudprovider.h"
19 #include "cloudprovider-generated.h"
20
21
22 typedef struct
23 {
24 gchar *name;
25 gchar *path;
26 GtkCloudProviderStatus status;
27 GIcon *icon;
28 GMenuModel *menu_model;
29 GActionGroup *action_group;
30
31 GDBusConnection *bus;
32 CloudProvider1 *proxy;
33 gchar *bus_name;
34 gchar *object_path;
35 GCancellable *cancellable;
36 } GtkCloudProviderPrivate;
37
38 G_DEFINE_TYPE_WITH_PRIVATE (GtkCloudProvider, gtk_cloud_provider, G_TYPE_OBJECT)
39
40 enum {
41 CHANGED,
42 LAST_SIGNAL
43 };
44
45 static guint gSignals [LAST_SIGNAL];
46
47 static void
48 on_get_icon (GObject *source_object,
49 GAsyncResult *res,
50 gpointer user_data)
51 {
52 GtkCloudProvider *self = GTK_CLOUD_PROVIDER (user_data);
53 GtkCloudProviderPrivate *priv = gtk_cloud_provider_get_instance_private (self);
54 GError *error = NULL;
55 GVariant *variant_tuple;
56 GVariant *variant_dict;
57 GVariant *variant;
58
59 g_clear_object (&priv->icon);
60
61 cloud_provider1_call_get_icon_finish (priv->proxy, &variant_tuple, res, &error);
62 if (error != NULL)
63 {
64 g_warning ("Error getting the provider icon %s", error->message);
65 goto out;
66 }
67 g_print ("variant tuple %s\n", g_variant_print (variant_tuple, TRUE));
68
69 variant_dict = g_variant_get_child_value (variant_tuple, 0);
70 variant = g_variant_get_child_value (variant_dict, 0);
71 priv->icon = g_icon_deserialize (variant);
72 g_variant_unref (variant);
73 g_variant_unref (variant_dict);
74
75 out:
76 g_variant_unref (variant_tuple);
77 g_signal_emit_by_name (self, "changed");
78 }
79
80 static void
81 on_get_name (GObject *source_object,
82 GAsyncResult *res,
83 gpointer user_data)
84 {
85 GtkCloudProvider *self = GTK_CLOUD_PROVIDER (user_data);
86 GtkCloudProviderPrivate *priv = gtk_cloud_provider_get_instance_private (self);
87 GError *error = NULL;
88
89 if (priv->name != NULL)
90 g_free (priv->name);
91
92 cloud_provider1_call_get_name_finish (priv->proxy, &priv->name, res, &error);
93 if (error != NULL)
94 {
95 g_warning ("Error getting the provider name %s", error->message);
96 return;
97 }
98 g_signal_emit_by_name (self, "changed");
99 }
100
101
102 static void
103 on_get_path (GObject *source_object,
104 GAsyncResult *res,
105 gpointer user_data)
106 {
107 GtkCloudProvider *self = GTK_CLOUD_PROVIDER (user_data);
108 GtkCloudProviderPrivate *priv = gtk_cloud_provider_get_instance_private (self);
109 GError *error = NULL;
110
111 if (priv->path != NULL)
112 g_free (priv->path);
113
114 cloud_provider1_call_get_path_finish (priv->proxy, &priv->path, res, &error);
115 if (error != NULL)
116 {
117 g_warning ("Error getting the provider name %s", error->message);
118 return;
119 }
120 g_signal_emit_by_name (self, "changed");
121 }
122
123 static void
124 on_get_status (GObject *source_object,
125 GAsyncResult *res,
126 gpointer user_data)
127 {
128 GtkCloudProvider *self = GTK_CLOUD_PROVIDER (user_data);
129 GtkCloudProviderPrivate *priv = gtk_cloud_provider_get_instance_private (self);
130 GError *error = NULL;
131 gint status;
132
133 cloud_provider1_call_get_status_finish (priv->proxy, &status, res, &error);
134 if (error != NULL)
135 {
136 g_warning ("Error getting the provider name %s", error->message);
137 return;
138 }
139 priv->status = status;
140 g_signal_emit_by_name (self, "changed");
141 }
142
143 void
144 gtk_cloud_provider_update (GtkCloudProvider *self)
145 {
146 GtkCloudProviderPrivate *priv = gtk_cloud_provider_get_instance_private (self);
147
148 if (priv->proxy != NULL)
149 {
150 cloud_provider1_call_get_name (priv->proxy,
151 NULL,
152 (GAsyncReadyCallback) on_get_name,
153 self);
154 cloud_provider1_call_get_status (priv->proxy,
155 NULL,
156 (GAsyncReadyCallback) on_get_status,
157 self);
158 cloud_provider1_call_get_icon (priv->proxy,
159 NULL,
160 (GAsyncReadyCallback) on_get_icon,
161 self);
162 cloud_provider1_call_get_path (priv->proxy,
163 NULL,
164 (GAsyncReadyCallback) on_get_path,
165 self);
166
167 priv->menu_model = (GMenuModel*) g_dbus_menu_model_get (priv->bus,
168 priv->bus_name,
169 priv->object_path);
170 priv->action_group = (GActionGroup*) g_dbus_action_group_get (priv->bus,
171 priv->bus_name,
172 priv->object_path);
173 }
174 }
175
176 static void
177 on_proxy_created (GObject *source_object,
178 GAsyncResult *res,
179 gpointer user_data)
180 {
181 GError *error = NULL;
182 GtkCloudProvider *self;
183 GtkCloudProviderPrivate *priv;
184 CloudProvider1 *proxy;
185
186 proxy = cloud_provider1_proxy_new_for_bus_finish (res, &error);
187 if (error != NULL)
188 {
189 if (error->code != G_IO_ERROR_CANCELLED)
190 g_warning ("Error creating proxy for cloud provider %s", error->message);
191 return;
192 }
193 self = GTK_CLOUD_PROVIDER (user_data);
194 priv = gtk_cloud_provider_get_instance_private (self);
195
196 priv->proxy = proxy;
197
198 gtk_cloud_provider_update (self);
199 }
200
201 static void
202 on_bus_acquired (GObject *source_object,
203 GAsyncResult *res,
204 gpointer user_data)
205 {
206 GError *error = NULL;
207 GtkCloudProvider *self;
208 GDBusConnection *bus;
209 GtkCloudProviderPrivate *priv;
210
211 bus = g_bus_get_finish (res, &error);
212 if (error != NULL)
213 {
214 if (error->code != G_IO_ERROR_CANCELLED)
215 g_warning ("Error acdquiring bus for cloud provider %s", error->message);
216 return;
217 }
218
219 self = GTK_CLOUD_PROVIDER (user_data);
220 priv = gtk_cloud_provider_get_instance_private (user_data);
221 priv->bus = bus;
222 g_clear_object (&priv->cancellable);
223 priv->cancellable = g_cancellable_new ();
224 cloud_provider1_proxy_new (priv->bus,
225 G_DBUS_PROXY_FLAGS_NONE,
226 priv->bus_name,
227 priv->object_path,
228 priv->cancellable,
229 on_proxy_created,
230 self);
231 }
232
233 GtkCloudProvider*
234 gtk_cloud_provider_new (const gchar *bus_name,
235 const gchar *object_path)
236 {
237 GtkCloudProvider *self;
238 GtkCloudProviderPrivate *priv;
239
240 self = g_object_new (GTK_TYPE_CLOUD_PROVIDER, NULL);
241 priv = gtk_cloud_provider_get_instance_private (self);
242
243 priv->bus_name = g_strdup (bus_name);
244 priv->object_path = g_strdup (object_path);
245 priv->cancellable = g_cancellable_new ();
246 priv->status = GTK_CLOUD_PROVIDER_STATUS_INVALID;
247 g_bus_get (G_BUS_TYPE_SESSION,
248 priv->cancellable,
249 on_bus_acquired,
250 self);
251
252 return self;
253 }
254
255 static void
256 gtk_cloud_provider_finalize (GObject *object)
257 {
258 GtkCloudProvider *self = (GtkCloudProvider *)object;
259 GtkCloudProviderPrivate *priv = gtk_cloud_provider_get_instance_private (self);
260
261 g_cancellable_cancel (priv->cancellable);
262 g_clear_object (&priv->cancellable);
263 g_free (priv->name);
264 g_free (priv->path);
265 g_clear_object (&priv->icon);
266 g_clear_object (&priv->action_group);
267 g_clear_object (&priv->bus);
268 g_clear_object (&priv->proxy);
269 g_free (priv->bus_name);
270 g_free (priv->object_path);
271
272 G_OBJECT_CLASS (gtk_cloud_provider_parent_class)->finalize (object);
273 }
274
275 static void
276 gtk_cloud_provider_class_init (GtkCloudProviderClass *klass)
277 {
278 GObjectClass *object_class = G_OBJECT_CLASS (klass);
279
280 object_class->finalize = gtk_cloud_provider_finalize;
281
282 gSignals [CHANGED] =
283 g_signal_new ("changed",
284 G_TYPE_FROM_CLASS (klass),
285 G_SIGNAL_RUN_LAST,
286 0,
287 NULL,
288 NULL,
289 g_cclosure_marshal_generic,
290 G_TYPE_NONE,
291 0);
292 }
293
294 static void
295 gtk_cloud_provider_init (GtkCloudProvider *self)
296 {
297 GtkCloudProviderPrivate *priv = gtk_cloud_provider_get_instance_private (self);
298
299 priv->status = GTK_CLOUD_PROVIDER_STATUS_INVALID;
300 }
301
302 gchar*
303 gtk_cloud_provider_get_name (GtkCloudProvider *self)
304 {
305 GtkCloudProviderPrivate *priv = gtk_cloud_provider_get_instance_private (self);
306
307 return priv->name;
308 }
309
310 GtkCloudProviderStatus
311 gtk_cloud_provider_get_status (GtkCloudProvider *self)
312 {
313 GtkCloudProviderPrivate *priv = gtk_cloud_provider_get_instance_private (self);
314
315 return priv->status;
316 }
317
318 GIcon*
319 gtk_cloud_provider_get_icon (GtkCloudProvider *self)
320 {
321 GtkCloudProviderPrivate *priv = gtk_cloud_provider_get_instance_private (self);
322
323 return priv->icon;
324 }
325
326 GMenuModel*
327 gtk_cloud_provider_get_menu_model (GtkCloudProvider *self)
328 {
329 GtkCloudProviderPrivate *priv = gtk_cloud_provider_get_instance_private (self);
330
331 return priv->menu_model;
332 }
333
334 GActionGroup*
335 gtk_cloud_provider_get_action_group (GtkCloudProvider *self)
336 {
337 GtkCloudProviderPrivate *priv = gtk_cloud_provider_get_instance_private (self);
338
339 return priv->action_group;
340 }
341
342 gchar *
343 gtk_cloud_provider_get_path (GtkCloudProvider *self)
344 {
345 GtkCloudProviderPrivate *priv = gtk_cloud_provider_get_instance_private (self);
346
347 return priv->path;
348 }
+0
-69
src/gtkcloudprovider.h less more
0 /* gtkcloudprovider.h
1 *
2 * Copyright (C) 2015 Carlos Soriano <csoriano@gnome.org>
3 *
4 * This file is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation; either version 3 of the
7 * License, or (at your option) any later version.
8 *
9 * This file is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #ifndef GTK_CLOUD_PROVIDER_H
19 #define GTK_CLOUD_PROVIDER_H
20
21 #include <gio/gio.h>
22 #include "cloudprovider-generated.h"
23
24 G_BEGIN_DECLS
25
26 typedef enum {
27 GTK_CLOUD_PROVIDER_STATUS_INVALID,
28 GTK_CLOUD_PROVIDER_STATUS_IDLE,
29 GTK_CLOUD_PROVIDER_STATUS_SYNCING,
30 GTK_CLOUD_PROVIDER_STATUS_ERROR
31 } GtkCloudProviderStatus;
32
33 #define GTK_TYPE_CLOUD_PROVIDER (gtk_cloud_provider_get_type())
34 #define GTK_CLOUD_PROVIDER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_CLOUD_PROVIDER, GtkCloudProvider))
35 #define GTK_CLOUD_PROVIDER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_CLOUD_PROVIDER, GtkCloudProviderClass))
36 #define GTK_IS_CLOUD_PROVIDER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_CLOUD_PROVIDER))
37 #define GTK_IS_CLOUD_PROVIDER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_CLOUD_PROVIDER))
38 #define GTK_CLOUD_PROVIDER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_CLOUD_PROVIDER, GtkCloudProviderClass))
39
40 typedef struct _GtkCloudProvider GtkCloudProvider;
41 typedef struct _GtkCloudProviderClass GtkCloudProviderClass;
42
43
44 struct _GtkCloudProviderClass
45 {
46 GObjectClass parent_class;
47 };
48
49 struct _GtkCloudProvider
50 {
51 GObject parent_instance;
52 };
53
54
55 GType gtk_cloud_provider_get_type (void) G_GNUC_CONST;
56 GtkCloudProvider *gtk_cloud_provider_new (const gchar *bus_name,
57 const gchar *object_path);
58
59 gchar* gtk_cloud_provider_get_name (GtkCloudProvider *self);
60 GtkCloudProviderStatus gtk_cloud_provider_get_status (GtkCloudProvider *self);
61 GIcon *gtk_cloud_provider_get_icon (GtkCloudProvider *self);
62 GMenuModel *gtk_cloud_provider_get_menu_model (GtkCloudProvider *self);
63 GActionGroup* gtk_cloud_provider_get_action_group (GtkCloudProvider *self);
64 gchar *gtk_cloud_provider_get_path (GtkCloudProvider *self);
65
66 G_END_DECLS
67
68 #endif /* GTK_CLOUD_PROVIDER_H */
+0
-310
src/gtkcloudprovidermanager.c less more
0 /* gtkcloudprovidermanager.c
1 *
2 * Copyright (C) 2015 Carlos Soriano <csoriano@gnome.org>
3 *
4 * This file is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation; either version 3 of the
7 * License, or (at your option) any later version.
8 *
9 * This file is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "gtkcloudprovidermanager.h"
19 #include "gtkcloudprovider.h"
20 #include <glib.h>
21 #include <glib/gprintf.h>
22 #include <gio/gio.h>
23
24 #define KEY_FILE_GROUP "Gtk Cloud Provider"
25
26 typedef struct
27 {
28 GList *providers;
29 guint dbus_owner_id;
30 GDBusNodeInfo *dbus_node_info;
31 } GtkCloudProviderManagerPrivate;
32
33 G_DEFINE_TYPE_WITH_PRIVATE (GtkCloudProviderManager, gtk_cloud_provider_manager, G_TYPE_OBJECT)
34
35 enum
36 {
37 CHANGED,
38 LAST_SIGNAL
39 };
40
41 static guint gSignals [LAST_SIGNAL];
42
43 static const gchar manager_xml[] =
44 "<node>"
45 " <interface name='org.freedesktop.CloudProviderManager1'>"
46 " <method name='CloudProviderChanged'>"
47 " </method>"
48 " </interface>"
49 "</node>";
50
51 static void
52 handle_method_call (GDBusConnection *connection,
53 const gchar *sender,
54 const gchar *object_path,
55 const gchar *interface_name,
56 const gchar *method_name,
57 GVariant *parameters,
58 GDBusMethodInvocation *invocation,
59 gpointer user_data)
60 {
61 if (g_strcmp0 (method_name, "CloudProviderChanged") == 0)
62 {
63 gtk_cloud_provider_manager_update (GTK_CLOUD_PROVIDER_MANAGER (user_data));
64 }
65 }
66
67 static const GDBusInterfaceVTable interface_vtable =
68 {
69 handle_method_call,
70 };
71
72 static void
73 on_bus_acquired (GDBusConnection *connection,
74 const gchar *name,
75 gpointer user_data)
76 {
77 GtkCloudProviderManager *self = user_data;
78 GtkCloudProviderManagerPrivate *priv = gtk_cloud_provider_manager_get_instance_private (self);
79 guint registration_id;
80
81 g_debug ("Registering cloud provider server 'MyCloud'\n");
82 registration_id = g_dbus_connection_register_object (connection,
83 GTK_CLOUD_PROVIDER_MANAGER_DBUS_PATH,
84 priv->dbus_node_info->interfaces[0],
85 &interface_vtable,
86 self,
87 NULL, /* user_data_free_func */
88 NULL); /* GError** */
89 g_assert (registration_id > 0);
90
91 /* In case some provider updated before adquiring the bus */
92 gtk_cloud_provider_manager_update (GTK_CLOUD_PROVIDER_MANAGER (user_data));
93 }
94
95 static void
96 on_name_acquired (GDBusConnection *connection,
97 const gchar *name,
98 gpointer user_data)
99 {
100 }
101
102 static void
103 on_name_lost (GDBusConnection *connection,
104 const gchar *name,
105 gpointer user_data)
106 {
107 }
108
109 /**
110 * gtk_cloud_provider_manager_dup_singleton
111 * Returns: (transfer none): A manager singleton
112 */
113 GtkCloudProviderManager *
114 gtk_cloud_provider_manager_dup_singleton (void)
115 {
116 static GObject *self = NULL;
117
118 if (self == NULL)
119 {
120 GtkCloudProviderManagerPrivate *priv;
121
122 self = g_object_new (GTK_TYPE_CLOUD_PROVIDER_MANAGER, NULL);
123 priv = gtk_cloud_provider_manager_get_instance_private (GTK_CLOUD_PROVIDER_MANAGER (self));
124
125 /* Export the interface we listen to, so clients can request properties of
126 * the cloud provider such as name, status or icon */
127 priv->dbus_node_info = g_dbus_node_info_new_for_xml (manager_xml, NULL);
128 g_assert (priv->dbus_node_info != NULL);
129
130 priv->dbus_owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
131 GTK_CLOUD_PROVIDER_MANAGER_DBUS_NAME,
132 G_BUS_NAME_OWNER_FLAGS_NONE,
133 on_bus_acquired,
134 on_name_acquired,
135 on_name_lost,
136 self,
137 NULL);
138 return GTK_CLOUD_PROVIDER_MANAGER (self);
139 }
140 else
141 {
142 return g_object_ref (self);
143 }
144 }
145
146 static void
147 gtk_cloud_provider_manager_finalize (GObject *object)
148 {
149 GtkCloudProviderManager *self = (GtkCloudProviderManager *)object;
150 GtkCloudProviderManagerPrivate *priv = gtk_cloud_provider_manager_get_instance_private (self);
151
152 g_list_free_full (priv->providers, g_object_unref);
153 g_bus_unown_name (priv->dbus_owner_id);
154 g_dbus_node_info_unref (priv->dbus_node_info);
155
156 G_OBJECT_CLASS (gtk_cloud_provider_manager_parent_class)->finalize (object);
157 }
158
159 static void
160 gtk_cloud_provider_manager_class_init (GtkCloudProviderManagerClass *klass)
161 {
162 GObjectClass *object_class = G_OBJECT_CLASS (klass);
163
164 object_class->finalize = gtk_cloud_provider_manager_finalize;
165
166 gSignals [CHANGED] =
167 g_signal_new ("changed",
168 G_TYPE_FROM_CLASS (klass),
169 G_SIGNAL_RUN_LAST,
170 0,
171 NULL,
172 NULL,
173 g_cclosure_marshal_generic,
174 G_TYPE_NONE,
175 0);
176 }
177
178 static void
179 gtk_cloud_provider_manager_init (GtkCloudProviderManager *self)
180 {
181 }
182
183 /**
184 * gtk_cloud_provider_manager_get_providers
185 * @manager: A GtkCloudProviderManager
186 * Returns: (transfer none): The list of providers.
187 */
188 GList*
189 gtk_cloud_provider_manager_get_providers (GtkCloudProviderManager *manager)
190 {
191 GtkCloudProviderManagerPrivate *priv = gtk_cloud_provider_manager_get_instance_private (manager);
192
193 return priv->providers;
194 }
195
196 static void
197 on_cloud_provider_changed (GtkCloudProvider *cloud_provider,
198 GtkCloudProviderManager *self)
199 {
200 GIcon *icon;
201 gchar *name;
202 guint status;
203
204 name = gtk_cloud_provider_get_name (cloud_provider);
205 icon = gtk_cloud_provider_get_icon (cloud_provider);
206 status = gtk_cloud_provider_get_status (cloud_provider);
207 if (name == NULL || icon == NULL || status == GTK_CLOUD_PROVIDER_STATUS_INVALID)
208 return;
209
210 g_signal_emit_by_name (self, "changed", NULL);
211 }
212
213 static void
214 load_cloud_provider (GtkCloudProviderManager *self,
215 GFile *file)
216 {
217 GtkCloudProviderManagerPrivate *priv = gtk_cloud_provider_manager_get_instance_private (self);
218 GKeyFile *key_file;
219 gchar *path;
220 GError *error = NULL;
221 gchar *bus_name;
222 gchar *object_path;
223 gboolean success = FALSE;
224 GtkCloudProvider *cloud_provider;
225
226 key_file = g_key_file_new ();
227 path = g_file_get_path (file);
228 g_key_file_load_from_file (key_file, path, G_KEY_FILE_NONE, &error);
229 if (error != NULL)
230 goto out;
231
232 if (!g_key_file_has_group (key_file, KEY_FILE_GROUP))
233 goto out;
234
235 bus_name = g_key_file_get_string (key_file, KEY_FILE_GROUP, "BusName", &error);
236 if (error != NULL)
237 goto out;
238 object_path = g_key_file_get_string (key_file, KEY_FILE_GROUP, "ObjectPath", &error);
239 if (error != NULL)
240 goto out;
241
242 g_print ("cloud provider found %s %s\n", bus_name, object_path);
243 cloud_provider = gtk_cloud_provider_new (bus_name, object_path);
244 g_signal_connect (cloud_provider, "changed",
245 G_CALLBACK (on_cloud_provider_changed), self);
246 priv->providers = g_list_append (priv->providers, cloud_provider);
247
248 success = TRUE;
249 out:
250 if (!success)
251 g_warning ("Error while loading cloud provider key file at %s", path);
252 g_key_file_free (key_file);
253 }
254
255 /**
256 * gtk_cloud_provider_manager_update
257 * @manager: A GtkCloudProviderManager
258 */
259 void
260 gtk_cloud_provider_manager_update (GtkCloudProviderManager *manager)
261 {
262 GtkCloudProviderManagerPrivate *priv = gtk_cloud_provider_manager_get_instance_private (manager);
263 const gchar* const *data_dirs;
264 gint i;
265 gint len;
266 gchar *key_files_directory_path;
267 GFile *key_files_directory_file;
268 GError *error = NULL;
269 GFileEnumerator *file_enumerator;
270
271
272 g_list_free_full (priv->providers, g_object_unref);
273 priv->providers = NULL;
274
275 data_dirs = g_get_system_data_dirs ();
276 len = g_strv_length ((gchar **)data_dirs);
277
278 for (i = 0; i < len; i++)
279 {
280 GFileInfo *info;
281
282 key_files_directory_path = g_build_filename (data_dirs[i], "cloud-providers", NULL);
283 key_files_directory_file = g_file_new_for_path (key_files_directory_path);
284 file_enumerator = g_file_enumerate_children (key_files_directory_file,
285 "standard::name,standard::type",
286 G_FILE_QUERY_INFO_NONE,
287 NULL,
288 &error);
289 if (error)
290 {
291 error = NULL;
292 continue;
293 }
294
295 info = g_file_enumerator_next_file (file_enumerator, NULL, &error);
296 if (error)
297 {
298 g_warning ("Error while enumerating file %s error: %s\n", key_files_directory_path, error->message);
299 error = NULL;
300 continue;
301 }
302 while (info != NULL && error == NULL)
303 {
304 load_cloud_provider (manager, g_file_enumerator_get_child (file_enumerator, info));
305 info = g_file_enumerator_next_file (file_enumerator, NULL, &error);
306 }
307 }
308
309 }
+0
-58
src/gtkcloudprovidermanager.h less more
0 /* gtkcloudprovidermanager.h
1 *
2 * Copyright (C) 2015 Carlos Soriano <csoriano@gnome.org>
3 *
4 * This file is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation; either version 3 of the
7 * License, or (at your option) any later version.
8 *
9 * This file is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17 #ifndef GTK_CLOUD_PROVIDER_MANAGER_H
18 #define GTK_CLOUD_PROVIDER_MANAGER_H
19
20 #include <gio/gio.h>
21 #include "gtkcloudprovider.h"
22 #include "cloudprovidermanager-generated.h"
23
24 #define GTK_CLOUD_PROVIDER_MANAGER_DBUS_IFACE "org.freedesktop.CloudProviderManager1"
25 #define GTK_CLOUD_PROVIDER_MANAGER_DBUS_NAME "org.freedesktop.CloudProviderManager"
26 #define GTK_CLOUD_PROVIDER_MANAGER_DBUS_PATH "/org/freedesktop/CloudProviderManager"
27
28 G_BEGIN_DECLS
29
30 #define GTK_TYPE_CLOUD_PROVIDER_MANAGER (gtk_cloud_provider_manager_get_type())
31 #define GTK_CLOUD_PROVIDER_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_CLOUD_PROVIDER_MANAGER, GtkCloudProviderManager))
32 #define GTK_CLOUD_PROVIDER_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_CLOUD_PROVIDER_MANAGER, GtkCloudProviderManagerClass))
33 #define GTK_IS_CLOUD_PROVIDER_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_CLOUD_PROVIDER_MANAGER))
34 #define GTK_IS_CLOUD_PROVIDER_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_CLOUD_PROVIDER_MANAGER))
35 #define GTK_CLOUD_PROVIDER_MANAGER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_CLOUD_PROVIDER_MANAGER, GtkCloudProviderManagerClass))
36
37 typedef struct _GtkCloudProviderManager GtkCloudProviderManager;
38 typedef struct _GtkCloudProviderManagerClass GtkCloudProviderManagerClass;
39
40 struct _GtkCloudProviderManagerClass
41 {
42 GObjectClass parent_class;
43 };
44
45 struct _GtkCloudProviderManager
46 {
47 GObject parent_instance;
48 };
49
50 GType gtk_cloud_provider_manager_get_type (void) G_GNUC_CONST;
51 GtkCloudProviderManager *gtk_cloud_provider_manager_dup_singleton (void);
52 void gtk_cloud_provider_manager_update (GtkCloudProviderManager *self);
53 GList *gtk_cloud_provider_manager_get_providers (GtkCloudProviderManager *self);
54 //static void on_cloud_provider_changed (GtkCloudProvider *cloud_provider, GtkCloudProviderManager *self);
55 G_END_DECLS
56
57 #endif /* GTK_CLOUD_PROVIDER_MANAGER_H */
0 libgtkcloudproviders_headers = [
1 'gtkcloudprovidermanager.h',
2 'gtkcloudprovider.h',
0 libcloudproviders_headers = [
1 'cloudprovidermanager.h',
2 'cloudprovider.h',
33 'cloudprovider-generated.h',
44 'cloudprovidermanager-generated.h'
55 ]
66
7 libgtkcloudproviders_sources = [
8 'gtkcloudprovidermanager.c',
9 'gtkcloudprovider.c'
7 libcloudproviders_sources = [
8 'cloudprovidermanager.c',
9 'cloudprovider.c'
1010 ]
1111
12 libgtkcloudproviders_deps = [glib, gio, gio_unix]
12 libcloudproviders_deps = [glib, gio, gio_unix]
1313
1414 gdbus_sources = []
1515 gdbus_sources += gnome.gdbus_codegen ('cloudprovider-generated',
2121 interface_prefix: 'org.freedesktop',
2222 namespace: '')
2323
24 libgtkcloudproviders_sources += gdbus_sources
24 libcloudproviders_sources += gdbus_sources
2525
26 libgtkcloudproviders = shared_library ('cloudproviders',
27 libgtkcloudproviders_sources,
28 dependencies: libgtkcloudproviders_deps,
26 libcloudproviders = shared_library ('cloudproviders',
27 libcloudproviders_sources,
28 dependencies: libcloudproviders_deps,
2929 version: meson.project_version (),
3030 install: true)
3131
32 install_headers( libgtkcloudproviders_headers, subdir: 'cloudproviders')
32 install_headers( libcloudproviders_headers, subdir: 'cloudproviders')
3333
3434 pkg = import('pkgconfig')
35 pkg.generate(libraries : libgtkcloudproviders,
35 pkg.generate(libraries : libcloudproviders,
3636 subdirs : 'cloudproviders',
3737 version : '1.0',
3838 name : 'libcloudproviders',
0 gtk = dependency('gtk+-3.0')
1 gtkcloudprovidertest_deps = [glib, gio, gio_unix, gtk]
2 dep = declare_dependency (link_with: libgtkcloudproviders,
0 cloudprovidertest_deps = [glib, gio, gio_unix]
1 dep = declare_dependency (link_with: libcloudproviders,
32 include_directories: include_directories('../src/'),
4 dependencies: gtkcloudprovidertest_deps)
3 dependencies: cloudprovidertest_deps)
54
65 executable('testcloudproviderserver', 'testcloudproviderserver.c', dependencies: dep, install: true)
76 executable('testcloudproviderclient', 'testcloudproviderclient.c', dependencies: dep, install: true)
0 [Gtk Cloud Provider]
0 [Cloud Provider]
11 BusName=org.freedesktop.CloudProviderServerExample
22 ObjectPath=/org/freedesktop/CloudProviderServerExample
33 Version=1
00 #include <glib.h>
1 #include <gtkcloudprovider.h>
2 #include <gtkcloudprovidermanager.h>
1 #include <cloudprovider.h>
2 #include <cloudprovidermanager.h>
33
44 static void
55 print_gmenu_model (GMenuModel *model)
3434 }
3535
3636 static void
37 on_manager_changed (GtkCloudProviderManager *manager)
37 on_manager_changed (CloudProviderManager *manager)
3838 {
3939 GList *providers;
4040 GList *l;
4444 gchar *icon_representation;
4545 GMenuModel *menu;
4646
47 providers = gtk_cloud_provider_manager_get_providers (manager);
47 providers = cloud_provider_manager_get_providers (manager);
4848 g_print ("Providers data\n");
4949 g_print ("##############\n");
5050 for (l = providers; l != NULL; l = l->next)
5151 {
52 provider_status = gtk_cloud_provider_get_status (GTK_CLOUD_PROVIDER (l->data));
52 provider_status = cloud_provider_get_status (CLOUD_PROVIDER (l->data));
5353 switch (provider_status)
5454 {
55 case GTK_CLOUD_PROVIDER_STATUS_INVALID:
55 case CLOUD_PROVIDER_STATUS_INVALID:
5656 status_string = "invalid";
5757 break;
5858
59 case GTK_CLOUD_PROVIDER_STATUS_IDLE:
59 case CLOUD_PROVIDER_STATUS_IDLE:
6060 status_string = "idle";
6161 break;
6262
63 case GTK_CLOUD_PROVIDER_STATUS_SYNCING:
63 case CLOUD_PROVIDER_STATUS_SYNCING:
6464 status_string = "syncing";
6565 break;
6666
67 case GTK_CLOUD_PROVIDER_STATUS_ERROR:
67 case CLOUD_PROVIDER_STATUS_ERROR:
6868 status_string = "error";
6969 break;
7070
7272 g_assert_not_reached ();
7373 }
7474
75 icon = gtk_cloud_provider_get_icon (l->data);
75 icon = cloud_provider_get_icon (l->data);
7676 icon_representation = g_icon_to_string (icon);
7777
7878 g_print ("Name - %s, Status - %s, Path - %s, Icon - %s\n",
79 gtk_cloud_provider_get_name (GTK_CLOUD_PROVIDER (l->data)),
79 cloud_provider_get_name (CLOUD_PROVIDER (l->data)),
8080 status_string,
81 gtk_cloud_provider_get_path (GTK_CLOUD_PROVIDER (l->data)),
81 cloud_provider_get_path (CLOUD_PROVIDER (l->data)),
8282 icon_representation);
8383
8484 g_free (icon_representation);
8585
86 menu = gtk_cloud_provider_get_menu_model (l->data);
86 menu = cloud_provider_get_menu_model (l->data);
8787 g_print ("\nMenu\n");
8888 print_gmenu_model (menu);
8989 }
9494 main (gint argc,
9595 gchar *argv[])
9696 {
97 GtkCloudProviderManager *manager;
97 CloudProviderManager *manager;
9898
9999 GMainLoop *loop = g_main_loop_new(NULL, FALSE);
100100
101 manager = gtk_cloud_provider_manager_dup_singleton ();
101 manager = cloud_provider_manager_dup_singleton ();
102102 g_signal_connect (manager, "changed", G_CALLBACK (on_manager_changed), NULL);
103 gtk_cloud_provider_manager_update (manager);
103 cloud_provider_manager_update (manager);
104104
105105 g_print("Waiting for cloud providers\n\n");
106106
00 #include <glib.h>
11 #include <stdlib.h>
22 #include <gio/gio.h>
3 #include <gtkcloudprovider.h>
4 #include <gtkcloudprovidermanager.h>
3 #include <cloudprovider.h>
4 #include <cloudprovidermanager.h>
55 #define TIMEOUT 2000
66
7 typedef struct _CloudProviderClass CloudProviderClass;
8 typedef struct _CloudProvider CloudProvider;
9
10 struct _CloudProviderClass
7 typedef struct _TestCloudProviderClass TestCloudProviderClass;
8 typedef struct _TestCloudProvider TestCloudProvider;
9
10 struct _TestCloudProviderClass
1111 {
1212 GObjectClass parent_class;
1313 };
1414
15 struct _CloudProvider
15 struct _TestCloudProvider
1616 {
1717 GObject parent_instance;
1818
2525 };
2626
2727
28 static GType cloud_provider_get_type (void);
29 G_DEFINE_TYPE (CloudProvider, cloud_provider, G_TYPE_OBJECT);
30
31 static void
32 cloud_provider_finalize (GObject *object)
33 {
34 CloudProvider *self = (CloudProvider*)object;
28 static GType test_cloud_provider_get_type (void);
29 G_DEFINE_TYPE (TestCloudProvider, test_cloud_provider, G_TYPE_OBJECT);
30
31 static void
32 test_cloud_provider_finalize (GObject *object)
33 {
34 TestCloudProvider *self = (TestCloudProvider*)object;
3535
3636 g_free (self->name);
3737 g_free (self->path);
3838 g_clear_object (&self->icon);
3939 g_clear_object (&self->manager_proxy);
4040
41 G_OBJECT_CLASS (cloud_provider_parent_class)->finalize (object);
42 }
43
44 static void
45 cloud_provider_init (CloudProvider *self)
41 G_OBJECT_CLASS (test_cloud_provider_parent_class)->finalize (object);
42 }
43
44 static void
45 test_cloud_provider_init (TestCloudProvider *self)
4646 {
4747 GFile *icon_file;
4848 gchar *current_dir;
5252
5353 self->name = "MyCloud";
5454 self->path = g_strdup (current_dir);
55 self->status = GTK_CLOUD_PROVIDER_STATUS_INVALID;
55 self->status = CLOUD_PROVIDER_STATUS_INVALID;
5656 uri = g_build_filename (current_dir, "apple-red.png", NULL);
5757 icon_file = g_file_new_for_uri (uri);
5858 self->icon = g_file_icon_new (icon_file);
6363 }
6464
6565 static void
66 cloud_provider_class_init (CloudProviderClass *class)
66 test_cloud_provider_class_init (TestCloudProviderClass *class)
6767 {
6868 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
6969
70 gobject_class->finalize = cloud_provider_finalize;
71 }
72
73 static void
74 cloud_provider_set_status (CloudProvider *self,
70 gobject_class->finalize = test_cloud_provider_finalize;
71 }
72
73 static void
74 test_cloud_provider_set_status (TestCloudProvider *self,
7575 gint status)
7676 {
7777 /* Inform the manager that the provider changed */
234234 GDBusMethodInvocation *invocation,
235235 gpointer user_data)
236236 {
237 CloudProvider *cloud_provider = user_data;
237 TestCloudProvider *cloud_provider = user_data;
238238
239239 g_debug ("Handling dbus call in server\n");
240240 if (g_strcmp0 (method_name, "GetName") == 0)
306306 static gboolean
307307 change_provider (gpointer user_data)
308308 {
309 CloudProvider *cloud_provider = (CloudProvider *)user_data;
309 TestCloudProvider *cloud_provider = (TestCloudProvider *)user_data;
310310 GRand *rand;
311311 gint new_status;
312312
314314
315315 rand = g_rand_new ();
316316 new_status = g_rand_int_range (rand,
317 GTK_CLOUD_PROVIDER_STATUS_IDLE,
318 GTK_CLOUD_PROVIDER_STATUS_ERROR + 1);
319
320 cloud_provider_set_status (cloud_provider, new_status);
317 CLOUD_PROVIDER_STATUS_IDLE,
318 CLOUD_PROVIDER_STATUS_ERROR + 1);
319
320 test_cloud_provider_set_status (cloud_provider, new_status);
321321
322322 return TRUE;
323323 }
327327 GAsyncResult *res,
328328 gpointer user_data)
329329 {
330 CloudProvider *cloud_provider = user_data;
330 TestCloudProvider *cloud_provider = user_data;
331331 GError *error = NULL;
332332
333333 cloud_provider->manager_proxy = cloud_provider_manager1_proxy_new_for_bus_finish (res, &error);
348348 CloudProvider *cloud_provider;
349349 guint owner_id;
350350
351 cloud_provider = g_object_new (cloud_provider_get_type (), NULL);
351 cloud_provider = g_object_new (test_cloud_provider_get_type (), NULL);
352352
353353 owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
354354 "org.freedesktop.CloudProviderServerExample",