Debian Codegraph @

src/cloudprovidersaccountexporter.c

raw
1 /* cloudprovidersaccountexporter.c
2 *
3 * Copyright (C) 2017 Julius Haertl <[email protected]>
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 <gio/gio.h>
20 #include "cloudprovidersaccount.h"
21 #include "cloudprovidersaccountexporter.h"
22 #include "cloudprovidersproviderexporter.h"
23 #include "cloudprovidersaccountexporterpriv.h"
24 #include "cloudprovidersproviderexporterpriv.h"
25 #include "cloudproviders-generated.h"
26 #include "enums.h"
27
28 struct _CloudProvidersAccountExporter
29 {
30 GObject parent;
31
32 gchar *bus_name;
33 CloudProvidersDbusAccount *skeleton;
34
35 GDBusConnection *bus; // unowned, owned by the provider
36 gchar *object_path;
37 gchar *name;
38 gchar *path;
39 CloudProvidersAccountStatus status;
40 gchar *status_details;
41 gchar *icon;
42 GMenuModel *menu_model;
43 GActionGroup *action_group;
44
45 gint menu_model_export_id;
46 gint action_group_export_id;
47 CloudProvidersProviderExporter *provider;
48 };
49
50 G_DEFINE_TYPE (CloudProvidersAccountExporter, cloud_providers_account_exporter, G_TYPE_OBJECT)
51
52 enum
53 {
54 PROP_0,
55 PROP_NAME,
56 PROP_BUS_NAME,
57 PROP_PROVIDER,
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 /**
70 * CloudProvidersAccountExporter:
71 *
72 * Base object for representing a cloud providers account
73 */
74
75 static void
76 export_menu_model (CloudProvidersAccountExporter *self)
77 {
78 g_autoptr(GError) error = NULL;
79
80 self->menu_model_export_id = g_dbus_connection_export_menu_model (self->bus,
81 self->object_path,
82 self->menu_model,
83 &error);
84 if (self->menu_model_export_id == 0)
85 g_warning ("Menu export failed: %s", error->message);
86 }
87
88 static void
89 unexport_menu_model (CloudProvidersAccountExporter *self)
90 {
91 if(self->menu_model_export_id != 0)
92 {
93 g_dbus_connection_unexport_menu_model(self->bus, self->menu_model_export_id);
94 self->menu_model_export_id = 0;
95 }
96 }
97
98 static void
99 export_action_group (CloudProvidersAccountExporter *self)
100 {
101 g_autoptr(GError) error = NULL;
102
103 self->action_group_export_id = g_dbus_connection_export_action_group (self->bus,
104 self->object_path,
105 self->action_group,
106 &error);
107 if (self->action_group_export_id == 0)
108 g_warning ("Action export failed: %s", error->message);
109 }
110
111 static void
112 unexport_action_group (CloudProvidersAccountExporter *self)
113 {
114 if (self->action_group_export_id != 0)
115 {
116 g_dbus_connection_unexport_action_group(self->bus, self->action_group_export_id);
117 self->action_group_export_id = 0;
118 }
119 }
120
121 const gchar *
122 cloud_providers_account_exporter_get_object_path (CloudProvidersAccountExporter *self)
123 {
124 g_return_val_if_fail (CLOUD_PROVIDERS_IS_ACCOUNT_EXPORTER (self), NULL);
125
126 return self->object_path;
127 }
128
129 const gchar *
130 cloud_providers_account_exporter_get_bus_name (CloudProvidersAccountExporter *self)
131 {
132 g_return_val_if_fail (CLOUD_PROVIDERS_IS_ACCOUNT_EXPORTER (self), NULL);
133
134 return self->bus_name;
135 }
136
137 CloudProvidersDbusAccount*
138 cloud_providers_account_exporter_get_skeleton (CloudProvidersAccountExporter *self)
139 {
140 g_return_val_if_fail (CLOUD_PROVIDERS_IS_ACCOUNT_EXPORTER (self), NULL);
141
142 return self->skeleton;
143 }
144
145 /**
146 * cloud_providers_account_exporter_new:
147 * @provider: The provider to which it will be associated.
148 * @bus_name: A unique name for the account. Must be a valid DBus object name.
149 *
150 * Create a new [class@AccountExporter] object
151 */
152 CloudProvidersAccountExporter*
153 cloud_providers_account_exporter_new (CloudProvidersProviderExporter *provider,
154 const gchar *bus_name)
155 {
156 CloudProvidersAccountExporter *self;
157
158 self = g_object_new (CLOUD_PROVIDERS_TYPE_ACCOUNT_EXPORTER,
159 "provider", provider,
160 "bus-name", bus_name,
161 NULL);
162 return self;
163 }
164
165 static void
166 cloud_providers_account_exporter_get_property (GObject *object,
167 guint prop_id,
168 GValue *value,
169 GParamSpec *pspec)
170 {
171 CloudProvidersAccountExporter *self = CLOUD_PROVIDERS_ACCOUNT_EXPORTER (object);
172
173 switch (prop_id)
174 {
175 case PROP_NAME:
176 {
177 g_value_set_string (value, self->name);
178 }
179 break;
180
181 case PROP_BUS_NAME:
182 {
183 g_value_set_string (value, self->bus_name);
184 }
185 break;
186
187 case PROP_PROVIDER:
188 {
189 g_value_set_object (value, self->provider);
190 }
191 break;
192
193 case PROP_STATUS:
194 {
195 g_value_set_enum (value, self->status);
196 }
197 break;
198
199 case PROP_STATUS_DETAILS:
200 {
201 g_value_set_string (value, self->status_details);
202 }
203 break;
204
205 case PROP_ICON:
206 {
207 g_value_take_object (value, g_icon_new_for_string (self->icon, NULL));
208 }
209 break;
210
211 case PROP_PATH:
212 {
213 g_value_set_string (value, self->path);
214 }
215 break;
216
217 case PROP_ACTION_GROUP:
218 {
219 g_value_set_object (value, self->action_group);
220 }
221 break;
222
223 case PROP_MENU_MODEL:
224 {
225 g_value_set_object (value, self->menu_model);
226 }
227 break;
228
229 default:
230 {
231 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
232 }
233
234 }
235 }
236
237 static void
238 cloud_providers_account_exporter_set_property (GObject *object,
239 guint prop_id,
240 const GValue *value,
241 GParamSpec *pspec)
242 {
243 CloudProvidersAccountExporter *self = CLOUD_PROVIDERS_ACCOUNT_EXPORTER (object);
244
245 switch (prop_id)
246 {
247 case PROP_NAME:
248 {
249 g_free (self->name);
250 self->name = g_value_dup_string (value);
251 cloud_providers_dbus_account_set_name (self->skeleton, self->name);
252 }
253 break;
254
255 case PROP_BUS_NAME:
256 {
257 g_return_if_fail (self->bus_name == NULL);
258 self->bus_name = g_value_dup_string (value);
259 }
260 break;
261
262 case PROP_PROVIDER:
263 {
264 g_return_if_fail (self->provider == NULL);
265 g_set_weak_pointer (&self->provider, g_value_get_object (value));
266 }
267 break;
268
269 case PROP_STATUS:
270 {
271 self->status = g_value_get_enum (value);
272 cloud_providers_dbus_account_set_status (self->skeleton, self->status);
273 }
274 break;
275
276 case PROP_STATUS_DETAILS:
277 {
278 g_free (self->status_details);
279 self->status_details = g_value_dup_string (value);
280 cloud_providers_dbus_account_set_status_details (self->skeleton, self->status_details);
281 }
282 break;
283
284 case PROP_ICON:
285 {
286 g_free (self->icon);
287 self->icon = g_icon_to_string (g_value_get_object (value));
288 cloud_providers_dbus_account_set_icon (self->skeleton,
289 self->icon);
290 }
291 break;
292
293 case PROP_PATH:
294 {
295 g_free (self->path);
296 self->path = g_value_dup_string (value);
297 cloud_providers_dbus_account_set_path (self->skeleton, self->path);
298 }
299 break;
300
301 case PROP_ACTION_GROUP:
302 {
303 cloud_providers_account_exporter_set_action_group (self, (GActionGroup *) g_value_get_object (value));
304 }
305 break;
306
307 case PROP_MENU_MODEL:
308 {
309 cloud_providers_account_exporter_set_menu_model (self, (GMenuModel *) g_value_get_object (value));
310 }
311 break;
312
313 default:
314 {
315 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
316 }
317 }
318 }
319
320 void
321 cloud_providers_account_exporter_set_name (CloudProvidersAccountExporter *self,
322 const gchar *name)
323 {
324 g_return_if_fail (CLOUD_PROVIDERS_IS_ACCOUNT_EXPORTER (self));
325
326 g_object_set (self, "name", name, NULL);
327 }
328
329 void
330 cloud_providers_account_exporter_set_status (CloudProvidersAccountExporter *self,
331 CloudProvidersAccountStatus status)
332 {
333 g_return_if_fail (CLOUD_PROVIDERS_IS_ACCOUNT_EXPORTER (self));
334
335 g_object_set (self, "status", status, NULL);
336 }
337
338 void
339 cloud_providers_account_exporter_set_status_details (CloudProvidersAccountExporter *self,
340 const gchar *status_details)
341 {
342 g_return_if_fail (CLOUD_PROVIDERS_IS_ACCOUNT_EXPORTER (self));
343
344 g_object_set (self, "status-details", status_details, NULL);
345 }
346
347 void
348 cloud_providers_account_exporter_set_icon (CloudProvidersAccountExporter *self,
349 GIcon *icon)
350 {
351 g_return_if_fail (CLOUD_PROVIDERS_IS_ACCOUNT_EXPORTER (self));
352
353 g_object_set (self, "icon", icon, NULL);
354 }
355
356 /**
357 * cloud_providers_account_exporter_set_menu_model:
358 * @self: The account
359 * @menu_model: The menu model to export
360 *
361 * One of the benefits of the integration is to display a menu with available
362 * options for an account. Use this function to export a [[email protected]] menu to be
363 * displayed by the chosen integration by the desktop environment or application.
364 */
365 void
366 cloud_providers_account_exporter_set_menu_model (CloudProvidersAccountExporter *self,
367 GMenuModel *menu_model)
368 {
369 g_return_if_fail (CLOUD_PROVIDERS_IS_ACCOUNT_EXPORTER (self));
370
371 g_return_if_fail (self->menu_model == NULL);
372
373 if (g_set_object (&self->menu_model, menu_model)) {
374 export_menu_model (self);
375 }
376 }
377
378 /**
379 * cloud_providers_account_exporter_set_action_group:
380 * @self: The cloud provider
381 * @action_group: The action group to be used to render the menu
382 *
383 * In order for a menu to receive events that will eventually call your callbacks, it needs the
384 * corresponding [[email protected]]. Use this function to export it.
385 */
386 void
387 cloud_providers_account_exporter_set_action_group (CloudProvidersAccountExporter *self,
388 GActionGroup *action_group)
389 {
390 g_return_if_fail (CLOUD_PROVIDERS_IS_ACCOUNT_EXPORTER (self));
391
392 g_return_if_fail (self->action_group == NULL);
393
394 if (g_set_object (&self->action_group, action_group)) {
395 export_action_group (self);
396 }
397 }
398
399 void
400 cloud_providers_account_exporter_set_path (CloudProvidersAccountExporter *self,
401 const gchar *path)
402 {
403 g_return_if_fail (CLOUD_PROVIDERS_IS_ACCOUNT_EXPORTER (self));
404
405 g_object_set (self, "path", path, NULL);
406 }
407
408 static void
409 cloud_providers_account_exporter_dispose (GObject *object)
410 {
411 CloudProvidersAccountExporter *self = (CloudProvidersAccountExporter *)object;
412 unexport_menu_model (self);
413 unexport_action_group (self);
414
415 g_clear_object (&self->skeleton);
416 g_clear_object (&self->action_group);
417 g_clear_object (&self->menu_model);
418 g_clear_weak_pointer (&self->provider);
419
420 G_OBJECT_CLASS (cloud_providers_account_exporter_parent_class)->dispose (object);
421 }
422
423 static void
424 cloud_providers_account_exporter_finalize (GObject *object)
425 {
426 CloudProvidersAccountExporter *self = (CloudProvidersAccountExporter *)object;
427
428 g_clear_pointer (&self->bus_name, g_free);
429 g_clear_pointer (&self->name, g_free);
430 g_clear_pointer (&self->object_path, g_free);
431 g_clear_pointer (&self->status_details, g_free);
432 g_clear_pointer (&self->path, g_free);
433 g_clear_pointer (&self->icon, g_free);
434
435 G_OBJECT_CLASS (cloud_providers_account_exporter_parent_class)->finalize (object);
436 }
437
438 static void
439 cloud_providers_account_exporter_constructed (GObject *object)
440 {
441 CloudProvidersAccountExporter *self = CLOUD_PROVIDERS_ACCOUNT_EXPORTER (object);
442 const gchar *provider_object_path;
443
444 self->bus = cloud_providers_provider_exporter_get_bus (self->provider);
445 provider_object_path = cloud_providers_provider_exporter_get_object_path (self->provider);
446 self->object_path = g_strconcat (provider_object_path, "/", self->bus_name, NULL);
447 cloud_providers_provider_exporter_add_account (self->provider, self);
448 }
449
450 static void
451 cloud_providers_account_exporter_init (CloudProvidersAccountExporter *self)
452 {
453 self->skeleton = cloud_providers_dbus_account_skeleton_new ();
454 }
455
456 static void
457 cloud_providers_account_exporter_class_init (CloudProvidersAccountExporterClass *klass)
458 {
459 GObjectClass *object_class = G_OBJECT_CLASS (klass);
460
461 object_class->get_property = cloud_providers_account_exporter_get_property;
462 object_class->set_property = cloud_providers_account_exporter_set_property;
463 object_class->constructed = cloud_providers_account_exporter_constructed;
464 object_class->dispose = cloud_providers_account_exporter_dispose;
465 object_class->finalize = cloud_providers_account_exporter_finalize;
466
467 /**
468 * CloudProvidersAccountExporter:name:
469 *
470 * The name of the account.
471 */
472 properties [PROP_NAME] =
473 g_param_spec_string ("name", NULL, NULL,
474 NULL,
475 (G_PARAM_READWRITE |
476 G_PARAM_STATIC_STRINGS));
477
478 /**
479 * CloudProvidersAccountExporter:bus-name:
480 *
481 * The bus name of the account.
482 */
483 properties [PROP_BUS_NAME] =
484 g_param_spec_string ("bus-name", NULL, NULL,
485 NULL,
486 (G_PARAM_READWRITE |
487 G_PARAM_STATIC_STRINGS|
488 G_PARAM_CONSTRUCT_ONLY));
489
490 /**
491 * CloudProvidersAccountExporter:provider:
492 *
493 * The provider associated with the account.
494 */
495 properties [PROP_PROVIDER] =
496 g_param_spec_object ("provider", NULL, NULL,
497 CLOUD_PROVIDERS_TYPE_PROVIDER_EXPORTER,
498 (G_PARAM_READWRITE |
499 G_PARAM_STATIC_STRINGS |
500 G_PARAM_CONSTRUCT_ONLY));
501
502 /**
503 * CloudProvidersAccountExporter:path:
504 *
505 * The path of the directory where files are located.
506 */
507 properties [PROP_PATH] =
508 g_param_spec_string ("path", NULL, NULL,
509 NULL,
510 (G_PARAM_READWRITE |
511 G_PARAM_STATIC_STRINGS));
512
513 /**
514 * CloudProvidersAccountExporter:status:
515 *
516 * Status of the account.
517 */
518 properties [PROP_STATUS] =
519 g_param_spec_enum ("status", NULL, NULL,
520 CLOUD_TYPE_PROVIDERS_ACCOUNT_STATUS,
521 CLOUD_PROVIDERS_ACCOUNT_STATUS_INVALID,
522 (G_PARAM_READWRITE |
523 G_PARAM_STATIC_STRINGS));
524
525 /**
526 * CloudProvidersAccountExporter:status-details:
527 *
528 * The details of the account status.
529 */
530 properties [PROP_STATUS_DETAILS] =
531 g_param_spec_string ("status-details", NULL, NULL,
532 NULL,
533 (G_PARAM_READWRITE |
534 G_PARAM_STATIC_STRINGS));
535
536 /**
537 * CloudProvidersAccountExporter:icon:
538 *
539 * The icon representing the account.
540 */
541 properties [PROP_ICON] =
542 g_param_spec_object ("icon", NULL, NULL,
543 G_TYPE_ICON,
544 (G_PARAM_READWRITE |
545 G_PARAM_STATIC_STRINGS));
546
547 /**
548 * CloudProvidersAccountExporter:menu-model:
549 *
550 * The menu model associated with the account.
551 */
552 properties [PROP_MENU_MODEL] =
553 g_param_spec_object ("menu-model", NULL, NULL,
554 G_TYPE_MENU_MODEL,
555 (G_PARAM_READWRITE |
556 G_PARAM_STATIC_STRINGS));
557
558 /**
559 * CloudProvidersAccountExporter:action-group:
560 *
561 * The action group associated with the account and menu model.
562 */
563 properties [PROP_ACTION_GROUP] =
564 g_param_spec_object ("action-group", NULL, NULL,
565 G_TYPE_ACTION_GROUP,
566 (G_PARAM_READWRITE |
567 G_PARAM_STATIC_STRINGS));
568
569 g_object_class_install_properties (object_class, N_PROPS, properties);
570 }
571