Debian Codegraph @

src/cloudprovidersaccount.c

raw
1 /* cloudprovidersaccount.c
2 *
3 * Copyright (C) 2015 Carlos Soriano <[email protected]>
4 * Copyright (C) 2017 Julius Haertl <[email protected]>
5 *
6 * This file is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * This file is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "cloudprovidersaccount.h"
21 #include "cloudprovidersprovider.h"
22 #include "cloudproviders-generated.h"
23 #include "enums.h"
24
25 struct _CloudProvidersAccount
26 {
27 GObject parent;
28
29 gchar *name;
30 gchar *path;
31 CloudProvidersAccountStatus status;
32 gchar *status_details;
33 GIcon *icon;
34 GMenuModel *menu_model;
35 GActionGroup *action_group;
36
37 GDBusConnection *bus;
38 CloudProvidersDbusAccount *proxy; // Owned by bus
39 };
40
41 G_DEFINE_TYPE (CloudProvidersAccount, cloud_providers_account, G_TYPE_OBJECT)
42
43 /**
44 * CloudProvidersAccount:
45 *
46 * Base object for representing a single account for clients.
47 *
48 * CloudProvidersAccount is the basic object used to construct the integrator UI
49 * and actions that a provider will present to the user, from the client side.
50 * Integrators of the cloud providers can use this object to poll the
51 * [class@Provider] menus, status and actions.
52 */
53
54 enum
55 {
56 PROP_0,
57 PROP_NAME,
58 PROP_ICON,
59 PROP_STATUS,
60 PROP_STATUS_DETAILS,
61 PROP_MENU_MODEL,
62 PROP_ACTION_GROUP,
63 PROP_PATH,
64 N_PROPS
65 };
66
67 static GParamSpec *properties [N_PROPS];
68
69 static void
70 on_name_changed (GObject *object,
71 GParamSpec *pspec,
72 CloudProvidersAccount *self)
73 {
74 g_free (self->name);
75 self->name = cloud_providers_dbus_account_dup_name (self->proxy);
76 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_NAME]);
77 }
78
79 static void
80 on_status_changed (GObject *object,
81 GParamSpec *pspec,
82 CloudProvidersAccount *self)
83 {
84 self->status = cloud_providers_dbus_account_get_status (self->proxy);
85 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_STATUS]);
86 }
87
88 static void
89 on_status_details_changed (GObject *object,
90 GParamSpec *pspec,
91 CloudProvidersAccount *self)
92 {
93 g_free (self->status_details);
94 self->status_details = cloud_providers_dbus_account_dup_status_details (self->proxy);
95 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_STATUS_DETAILS]);
96 }
97
98 static void
99 on_icon_changed (GObject *object,
100 GParamSpec *pspec,
101 CloudProvidersAccount *self)
102 {
103 GError *error = NULL;
104
105 g_clear_object (&self->icon);
106 self->icon = g_icon_new_for_string (cloud_providers_dbus_account_get_icon (self->proxy), &error);
107 if (error != NULL)
108 {
109 g_printerr ("Error getting the icon in the client %s", error->message);
110 }
111 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_ICON]);
112 }
113
114 static void
115 on_path_changed (GObject *object,
116 GParamSpec *pspec,
117 CloudProvidersAccount *self)
118 {
119 g_free (self->path);
120 self->path = cloud_providers_dbus_account_dup_path (self->proxy);
121 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_PATH]);
122 }
123
124 static void
125 setup_proxy (CloudProvidersAccount *self)
126 {
127 GMenuModel *menu_model;
128 GActionGroup *action_group;
129 GIcon *icon = NULL;
130 GError *error = NULL;
131 g_autofree gchar *bus_name = NULL;
132 const gchar *object_path;
133
134 g_signal_connect (self->proxy, "notify::name", G_CALLBACK (on_name_changed), self);
135 g_signal_connect (self->proxy, "notify::status", G_CALLBACK (on_status_changed), self);
136 g_signal_connect (self->proxy, "notify::status-details", G_CALLBACK (on_status_details_changed), self);
137 g_signal_connect (self->proxy, "notify::icon", G_CALLBACK (on_icon_changed), self);
138 g_signal_connect (self->proxy, "notify::path", G_CALLBACK (on_path_changed), self);
139
140 icon = g_icon_new_for_string (cloud_providers_dbus_account_get_icon (self->proxy), &error);
141 if (error != NULL)
142 {
143 g_printerr ("Error getting the icon in the client %s", error->message);
144 }
145
146 bus_name = g_dbus_proxy_get_name_owner (G_DBUS_PROXY (self->proxy));
147 object_path = g_dbus_proxy_get_object_path (G_DBUS_PROXY (self->proxy));
148 menu_model = (GMenuModel*) g_dbus_menu_model_get (self->bus,
149 bus_name,
150 object_path);
151 action_group = (GActionGroup*) g_dbus_action_group_get (self->bus,
152 bus_name,
153 object_path);
154 self->name = cloud_providers_dbus_account_dup_name (self->proxy);
155 self->status = cloud_providers_dbus_account_get_status (self->proxy);
156 self->status_details = cloud_providers_dbus_account_dup_status_details (self->proxy);
157 self->icon = icon;
158 self->path = cloud_providers_dbus_account_dup_path (self->proxy);
159 self->menu_model = menu_model;
160 self->action_group = action_group;
161
162 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_NAME]);
163 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_STATUS]);
164 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_STATUS_DETAILS]);
165 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_ICON]);
166 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_PATH]);
167 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_ACTION_GROUP]);
168 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_MENU_MODEL]);
169 }
170
171 static void
172 cloud_providers_account_get_property (GObject *object,
173 guint prop_id,
174 GValue *value,
175 GParamSpec *pspec)
176 {
177 CloudProvidersAccount *self = CLOUD_PROVIDERS_ACCOUNT (object);
178
179 switch (prop_id)
180 {
181 case PROP_NAME:
182 {
183 g_value_set_string (value, self->name);
184 }
185 break;
186
187 case PROP_STATUS:
188 {
189 g_value_set_enum (value, self->status);
190 }
191 break;
192
193 case PROP_STATUS_DETAILS:
194 {
195 g_value_set_string (value, self->status_details);
196 }
197 break;
198
199 case PROP_ICON:
200 {
201 g_value_set_object (value, self->icon);
202 }
203 break;
204
205 case PROP_PATH:
206 {
207 g_value_set_string (value, self->path);
208 }
209 break;
210
211 case PROP_ACTION_GROUP:
212 {
213 g_value_set_object (value, self->action_group);
214 }
215 break;
216
217 case PROP_MENU_MODEL:
218 {
219 g_value_set_object (value, self->menu_model);
220 }
221 break;
222
223 default:
224 {
225 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
226 }
227 }
228 }
229
230 static void
231 cloud_providers_account_set_property (GObject *object,
232 guint prop_id,
233 const GValue *value,
234 GParamSpec *pspec)
235 {
236 switch (prop_id)
237 {
238 default:
239 {
240 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
241 }
242
243 }
244 }
245
246 /**
247 * cloud_providers_account_new
248 * @proxy: DBus proxy
249 *
250 * A [class@Account] object is used to fetch details about cloud providers from DBus.
251 * It is usually fetched from [[email protected]_accounts] as a list.
252 */
253 CloudProvidersAccount*
254 cloud_providers_account_new (GDBusProxy *proxy)
255 {
256 CloudProvidersAccount *self;
257
258 g_return_val_if_fail (CLOUD_PROVIDERS_DBUS_IS_ACCOUNT (proxy), NULL);
259
260 self = g_object_new (CLOUD_PROVIDERS_TYPE_ACCOUNT, NULL);
261
262 self->proxy = CLOUD_PROVIDERS_DBUS_ACCOUNT (g_object_ref (proxy));
263 self->bus = g_dbus_proxy_get_connection (G_DBUS_PROXY (self->proxy));
264
265 setup_proxy (self);
266
267 return self;
268 }
269
270 static void
271 cloud_providers_account_dispose (GObject *object)
272 {
273 CloudProvidersAccount *self = (CloudProvidersAccount *)object;
274
275 g_clear_object (&self->icon);
276 g_clear_object (&self->action_group);
277 g_clear_object (&self->menu_model);
278 if (self->proxy)
279 g_signal_handlers_disconnect_by_data (self->proxy, self);
280 g_clear_object (&self->proxy);
281
282 G_OBJECT_CLASS (cloud_providers_account_parent_class)->dispose (object);
283 }
284
285 static void
286 cloud_providers_account_finalize (GObject *object)
287 {
288 CloudProvidersAccount *self = (CloudProvidersAccount *)object;
289
290 g_clear_pointer (&self->name, g_free);
291 g_clear_pointer (&self->path, g_free);
292 g_clear_pointer (&self->status_details, g_free);
293
294 G_OBJECT_CLASS (cloud_providers_account_parent_class)->finalize (object);
295 }
296
297 static void
298 cloud_providers_account_class_init (CloudProvidersAccountClass *klass)
299 {
300 GObjectClass *object_class = G_OBJECT_CLASS (klass);
301
302 object_class->dispose = cloud_providers_account_dispose;
303 object_class->finalize = cloud_providers_account_finalize;
304 object_class->set_property = cloud_providers_account_set_property;
305 object_class->get_property = cloud_providers_account_get_property;
306
307 /**
308 * CloudProvidersAccount:name:
309 *
310 * The name of the account.
311 */
312 properties [PROP_NAME] =
313 g_param_spec_string ("name", NULL, NULL,
314 NULL,
315 (G_PARAM_READABLE |
316 G_PARAM_STATIC_STRINGS));
317
318 /**
319 * CloudProvidersAccount:path:
320 *
321 * The path of the directory where files are located.
322 */
323 properties [PROP_PATH] =
324 g_param_spec_string ("path", NULL, NULL,
325 NULL,
326 (G_PARAM_READABLE |
327 G_PARAM_STATIC_STRINGS));
328
329 /**
330 * CloudProvidersAccount:status:
331 *
332 * Status of the account.
333 */
334 properties [PROP_STATUS] =
335 g_param_spec_enum ("status", NULL, NULL,
336 CLOUD_TYPE_PROVIDERS_ACCOUNT_STATUS,
337 CLOUD_PROVIDERS_ACCOUNT_STATUS_INVALID,
338 (G_PARAM_READABLE |
339 G_PARAM_STATIC_STRINGS));
340
341 /**
342 * CloudProvidersAccount:status-details:
343 *
344 * The details of the account status.
345 */
346 properties [PROP_STATUS_DETAILS] =
347 g_param_spec_string ("status-details", NULL, NULL,
348 NULL,
349 (G_PARAM_READABLE |
350 G_PARAM_STATIC_STRINGS));
351
352 /**
353 * CloudProvidersAccount:icon:
354 *
355 * The icon representing the account.
356 */
357 properties [PROP_ICON] =
358 g_param_spec_object ("icon", NULL, NULL,
359 G_TYPE_ICON,
360 (G_PARAM_READABLE |
361 G_PARAM_STATIC_STRINGS));
362
363 /**
364 * CloudProvidersAccount:menu-model:
365 *
366 * The menu model associated with the account.
367 */
368 properties [PROP_MENU_MODEL] =
369 g_param_spec_object ("menu-model", NULL, NULL,
370 G_TYPE_MENU_MODEL,
371 (G_PARAM_READABLE |
372 G_PARAM_STATIC_STRINGS));
373
374 /**
375 * CloudProvidersAccount:action-group:
376 *
377 * The action group associated with the account and menu model.
378 */
379 properties [PROP_ACTION_GROUP] =
380 g_param_spec_object ("action-group", NULL, NULL,
381 G_TYPE_ACTION_GROUP,
382 (G_PARAM_READABLE |
383 G_PARAM_STATIC_STRINGS));
384
385 g_object_class_install_properties (object_class,
386 N_PROPS,
387 properties);
388 }
389
390 static void
391 cloud_providers_account_init (CloudProvidersAccount *self)
392 {
393 }
394
395 /**
396 * cloud_providers_account_get_name:
397 * @self: A cloud provider account
398 *
399 * Get the name of the account
400 *
401 * Returns: The name of the cloud provider account
402 */
403 const gchar*
404 cloud_providers_account_get_name (CloudProvidersAccount *self)
405 {
406 g_return_val_if_fail (CLOUD_PROVIDERS_IS_ACCOUNT (self), NULL);
407
408 return self->name;
409 }
410
411 /**
412 * cloud_providers_account_get_status:
413 * @self: A cloud provider account
414 *
415 * Get the status of the account
416 *
417 * Returns: The status of the cloud provider account
418 */
419 CloudProvidersAccountStatus
420 cloud_providers_account_get_status (CloudProvidersAccount *self)
421 {
422 g_return_val_if_fail (CLOUD_PROVIDERS_IS_ACCOUNT (self), CLOUD_PROVIDERS_ACCOUNT_STATUS_INVALID);
423
424 return self->status;
425 }
426
427 /**
428 * cloud_providers_account_get_status_details:
429 * @self: A cloud provider account
430 *
431 * Get the status details of the account
432 *
433 * Returns: The status detail description of the cloud provider account
434 */
435 const gchar*
436 cloud_providers_account_get_status_details (CloudProvidersAccount *self)
437 {
438 g_return_val_if_fail (CLOUD_PROVIDERS_IS_ACCOUNT (self), NULL);
439
440 return self->status_details;
441 }
442
443 /**
444 * cloud_providers_account_get_icon:
445 * @self: A cloud provider account
446 *
447 * Get the icon of the account
448 *
449 * Returns: (transfer none) : The icon of the cloud provider account
450 */
451 GIcon*
452 cloud_providers_account_get_icon (CloudProvidersAccount *self)
453 {
454 g_return_val_if_fail (CLOUD_PROVIDERS_IS_ACCOUNT (self), NULL);
455
456 return self->icon;
457 }
458
459 /**
460 * cloud_providers_account_get_menu_model:
461 * @self: A cloud provider account
462 *
463 * Get the menu model exported for the account
464 *
465 * Returns: (transfer none) : The menu model exported by the cloud provider account
466 */
467 GMenuModel*
468 cloud_providers_account_get_menu_model (CloudProvidersAccount *self)
469 {
470 g_return_val_if_fail (CLOUD_PROVIDERS_IS_ACCOUNT (self), NULL);
471
472 return self->menu_model;
473 }
474
475 /**
476 * cloud_providers_account_get_action_group:
477 * @self: A cloud provider account
478 *
479 * Get the action group exported in addition to the [[email protected]] from
480 * [[email protected]_menu_model].
481 *
482 * Returns: (transfer none) : The action group exported by the cloud provider account
483 */
484 GActionGroup*
485 cloud_providers_account_get_action_group (CloudProvidersAccount *self)
486 {
487 g_return_val_if_fail (CLOUD_PROVIDERS_IS_ACCOUNT (self), NULL);
488
489 return self->action_group;
490 }
491
492 /**
493 * cloud_providers_account_get_path:
494 * @self: A cloud provider account
495 *
496 * Get the directory path of the account
497 *
498 * Returns: The directory path of the cloud provider account
499 */
500 const gchar *
501 cloud_providers_account_get_path (CloudProvidersAccount *self)
502 {
503 g_return_val_if_fail (CLOUD_PROVIDERS_IS_ACCOUNT (self), NULL);
504
505 return self->path;
506 }
507