| 1 |
/* cloudprovidersprovider.c |
| 2 |
* |
| 3 |
* Copyright (C) 2017 Carlos Soriano <[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 |
|
| 21 |
#include "cloudprovidersprovider.h" |
| 22 |
#include "cloudprovidersaccount.h" |
| 23 |
#include "cloudproviders-generated.h" |
| 24 |
|
| 25 |
struct _CloudProvidersProvider |
| 26 |
{ |
| 27 |
GObject parent_instance; |
| 28 |
|
| 29 |
GDBusObjectManager *manager; |
| 30 |
gchar *name; |
| 31 |
GList *accounts; |
| 32 |
gchar *manager_bus_name; |
| 33 |
gchar *manager_object_path; |
| 34 |
|
| 35 |
GDBusConnection *bus; |
| 36 |
CloudProvidersDbusProvider *proxy; |
| 37 |
GCancellable *cancellable; |
| 38 |
}; |
| 39 |
|
| 40 |
G_DEFINE_TYPE (CloudProvidersProvider, cloud_providers_provider, G_TYPE_OBJECT) |
| 41 |
|
| 42 |
/** |
| 43 |
* CloudProvidersProvider: |
| 44 |
* |
| 45 |
* Base object for representing a single provider for clients. |
| 46 |
* |
| 47 |
* This is the basic object object for client implementers that contains the properties of the |
| 48 |
* provider such as name, and the accounts associated with it. |
| 49 |
*/ |
| 50 |
|
| 51 |
enum |
| 52 |
{ |
| 53 |
PROP_0, |
| 54 |
PROP_NAME, |
| 55 |
N_PROPS |
| 56 |
}; |
| 57 |
|
| 58 |
enum |
| 59 |
{ |
| 60 |
ACCOUNTS_CHANGED, |
| 61 |
REMOVED, |
| 62 |
LAST_SIGNAL |
| 63 |
}; |
| 64 |
|
| 65 |
static guint signals [LAST_SIGNAL]; |
| 66 |
static GParamSpec *properties [N_PROPS]; |
| 67 |
|
| 68 |
static void |
| 69 |
on_name_changed (CloudProvidersProvider *self); |
| 70 |
static void |
| 71 |
on_cloud_providers_object_manager_object_added (GDBusObjectManager *manager, |
| 72 |
GDBusObject *object, |
| 73 |
gpointer user_data); |
| 74 |
|
| 75 |
static void |
| 76 |
on_cloud_providers_object_manager_object_removed (GDBusObjectManager *manager, |
| 77 |
GDBusObject *object, |
| 78 |
gpointer user_data); |
| 79 |
|
| 80 |
static void |
| 81 |
on_cloud_providers_object_manager_name_owner_changed (GObject *object, |
| 82 |
GParamSpec *pspec, |
| 83 |
gpointer user_data); |
| 84 |
|
| 85 |
CloudProvidersProvider * |
| 86 |
cloud_providers_provider_new (const gchar *bus_name, |
| 87 |
const gchar *object_path) |
| 88 |
{ |
| 89 |
CloudProvidersProvider *self; |
| 90 |
|
| 91 |
g_return_val_if_fail (bus_name != NULL, NULL); |
| 92 |
g_return_val_if_fail (object_path != NULL, NULL); |
| 93 |
|
| 94 |
self = g_object_new (CLOUD_PROVIDERS_TYPE_PROVIDER, NULL); |
| 95 |
self->manager_bus_name = g_strdup (bus_name); |
| 96 |
self->manager_object_path = g_strdup (object_path); |
| 97 |
|
| 98 |
return self; |
| 99 |
} |
| 100 |
|
| 101 |
static void |
| 102 |
cloud_providers_provider_dispose (GObject *object) |
| 103 |
{ |
| 104 |
CloudProvidersProvider *self = (CloudProvidersProvider *)object; |
| 105 |
|
| 106 |
g_cancellable_cancel (self->cancellable); |
| 107 |
g_clear_object (&self->cancellable); |
| 108 |
|
| 109 |
g_list_free_full (self->accounts, g_object_unref); |
| 110 |
self->accounts = NULL; |
| 111 |
|
| 112 |
if (self->proxy) |
| 113 |
g_signal_handlers_disconnect_by_func (self->proxy, G_CALLBACK (on_name_changed), self); |
| 114 |
g_clear_object (&self->proxy); |
| 115 |
|
| 116 |
if (self->manager) |
| 117 |
{ |
| 118 |
g_signal_handlers_disconnect_by_func (self->manager, |
| 119 |
G_CALLBACK (on_cloud_providers_object_manager_name_owner_changed), |
| 120 |
self); |
| 121 |
g_signal_handlers_disconnect_by_func (self->manager, |
| 122 |
G_CALLBACK (on_cloud_providers_object_manager_object_added), |
| 123 |
self); |
| 124 |
g_signal_handlers_disconnect_by_func (self->manager, |
| 125 |
G_CALLBACK (on_cloud_providers_object_manager_object_removed), |
| 126 |
self); |
| 127 |
} |
| 128 |
|
| 129 |
g_clear_object (&self->manager); |
| 130 |
g_clear_object (&self->bus); |
| 131 |
|
| 132 |
G_OBJECT_CLASS (cloud_providers_provider_parent_class)->dispose (object); |
| 133 |
} |
| 134 |
|
| 135 |
static void |
| 136 |
cloud_providers_provider_finalize (GObject *object) |
| 137 |
{ |
| 138 |
CloudProvidersProvider *self = (CloudProvidersProvider *)object; |
| 139 |
|
| 140 |
g_clear_pointer (&self->name, g_free); |
| 141 |
g_clear_pointer (&self->manager_bus_name, g_free); |
| 142 |
g_clear_pointer (&self->manager_object_path, g_free); |
| 143 |
|
| 144 |
G_OBJECT_CLASS (cloud_providers_provider_parent_class)->finalize (object); |
| 145 |
} |
| 146 |
|
| 147 |
static void |
| 148 |
cloud_providers_provider_get_property (GObject *object, |
| 149 |
guint prop_id, |
| 150 |
GValue *value, |
| 151 |
GParamSpec *pspec) |
| 152 |
{ |
| 153 |
CloudProvidersProvider *self = CLOUD_PROVIDERS_PROVIDER (object); |
| 154 |
|
| 155 |
switch (prop_id) |
| 156 |
{ |
| 157 |
case PROP_NAME: |
| 158 |
{ |
| 159 |
g_value_set_string (value, self->name); |
| 160 |
} |
| 161 |
break; |
| 162 |
|
| 163 |
default: |
| 164 |
{ |
| 165 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 166 |
} |
| 167 |
|
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
static void |
| 172 |
cloud_providers_provider_set_property (GObject *object, |
| 173 |
guint prop_id, |
| 174 |
const GValue *value, |
| 175 |
GParamSpec *pspec) |
| 176 |
{ |
| 177 |
CloudProvidersProvider *self = CLOUD_PROVIDERS_PROVIDER (object); |
| 178 |
|
| 179 |
switch (prop_id) |
| 180 |
{ |
| 181 |
case PROP_NAME: |
| 182 |
{ |
| 183 |
g_free (self->name); |
| 184 |
self->name = g_value_dup_string (value); |
| 185 |
g_object_notify_by_pspec (object, properties[PROP_NAME]); |
| 186 |
} |
| 187 |
break; |
| 188 |
|
| 189 |
break; |
| 190 |
default: |
| 191 |
{ |
| 192 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 193 |
} |
| 194 |
|
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
static void |
| 199 |
cloud_providers_provider_class_init (CloudProvidersProviderClass *klass) |
| 200 |
{ |
| 201 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 202 |
|
| 203 |
object_class->dispose = cloud_providers_provider_dispose; |
| 204 |
object_class->finalize = cloud_providers_provider_finalize; |
| 205 |
object_class->get_property = cloud_providers_provider_get_property; |
| 206 |
object_class->set_property = cloud_providers_provider_set_property; |
| 207 |
|
| 208 |
/** |
| 209 |
* CloudProvidersProvider:name: |
| 210 |
* |
| 211 |
* The name of the cloud provider. |
| 212 |
*/ |
| 213 |
properties [PROP_NAME] = |
| 214 |
g_param_spec_string ("name", NULL, NULL, |
| 215 |
NULL, |
| 216 |
(G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); |
| 217 |
g_object_class_install_properties (object_class, |
| 218 |
N_PROPS, |
| 219 |
properties); |
| 220 |
|
| 221 |
/** |
| 222 |
* CloudProviderProvider::accounts-changed |
| 223 |
* |
| 224 |
* This signal is emitted by a provider if the number of accounts changed. |
| 225 |
*/ |
| 226 |
signals [ACCOUNTS_CHANGED] = |
| 227 |
g_signal_new ("accounts-changed", |
| 228 |
G_TYPE_FROM_CLASS (klass), |
| 229 |
G_SIGNAL_RUN_LAST, |
| 230 |
0, |
| 231 |
NULL, |
| 232 |
NULL, |
| 233 |
g_cclosure_marshal_generic, |
| 234 |
G_TYPE_NONE, |
| 235 |
0); |
| 236 |
/** |
| 237 |
* CloudProviderProvider::removed |
| 238 |
* |
| 239 |
* This signal is emitted by a provider when the provider is removed in DBUS. |
| 240 |
*/ |
| 241 |
signals [REMOVED] = |
| 242 |
g_signal_new ("removed", |
| 243 |
G_TYPE_FROM_CLASS (klass), |
| 244 |
G_SIGNAL_RUN_LAST, |
| 245 |
0, |
| 246 |
NULL, |
| 247 |
NULL, |
| 248 |
g_cclosure_marshal_generic, |
| 249 |
G_TYPE_NONE, |
| 250 |
0); |
| 251 |
} |
| 252 |
|
| 253 |
static void |
| 254 |
update_cloud_providers_accounts (CloudProvidersProvider *self) |
| 255 |
{ |
| 256 |
GList *objects; |
| 257 |
GList *l; |
| 258 |
|
| 259 |
g_list_free_full (self->accounts, g_object_unref); |
| 260 |
self->accounts = NULL; |
| 261 |
objects = g_dbus_object_manager_get_objects (self->manager); |
| 262 |
if (objects == NULL) |
| 263 |
{ |
| 264 |
if (self->name != NULL) |
| 265 |
{ |
| 266 |
g_debug ("Provider accounts not ready server side for %s\n", self->name); |
| 267 |
} |
| 268 |
else |
| 269 |
{ |
| 270 |
g_debug ("Provider accounts not ready server side for %p\n", self); |
| 271 |
} |
| 272 |
return; |
| 273 |
} |
| 274 |
|
| 275 |
for (l = objects; l != NULL; l = l->next) |
| 276 |
{ |
| 277 |
CloudProvidersDbusObject *object; |
| 278 |
GDBusInterface *interface; |
| 279 |
|
| 280 |
object = CLOUD_PROVIDERS_DBUS_OBJECT (l->data); |
| 281 |
interface = g_dbus_object_get_interface (G_DBUS_OBJECT (object), |
| 282 |
CLOUD_PROVIDERS_PROVIDER_DBUS_IFACE); |
| 283 |
if (interface != NULL) |
| 284 |
{ |
| 285 |
self->proxy = cloud_providers_dbus_object_get_provider (object); |
| 286 |
g_signal_connect_swapped (self->proxy, "notify::name", G_CALLBACK (on_name_changed), self); |
| 287 |
on_name_changed (self); |
| 288 |
g_object_unref (interface); |
| 289 |
|
| 290 |
continue; |
| 291 |
} |
| 292 |
interface = g_dbus_object_get_interface (G_DBUS_OBJECT (object), |
| 293 |
CLOUD_PROVIDERS_ACCOUNT_DBUS_IFACE); |
| 294 |
if (interface != NULL) |
| 295 |
{ |
| 296 |
CloudProvidersAccount *account; |
| 297 |
|
| 298 |
account = cloud_providers_account_new (G_DBUS_PROXY (cloud_providers_dbus_object_peek_account (object))); |
| 299 |
|
| 300 |
self->accounts = g_list_append (self->accounts, account); |
| 301 |
g_object_unref (interface); |
| 302 |
|
| 303 |
continue; |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
g_list_free_full (objects, g_object_unref); |
| 308 |
|
| 309 |
g_signal_emit_by_name (G_OBJECT (self), "accounts-changed", NULL); |
| 310 |
} |
| 311 |
|
| 312 |
static void |
| 313 |
on_cloud_providers_object_manager_object_added (GDBusObjectManager *manager, |
| 314 |
GDBusObject *object, |
| 315 |
gpointer user_data) |
| 316 |
{ |
| 317 |
CloudProvidersProvider *self; |
| 318 |
|
| 319 |
self = CLOUD_PROVIDERS_PROVIDER (user_data); |
| 320 |
update_cloud_providers_accounts (self); |
| 321 |
} |
| 322 |
|
| 323 |
static void |
| 324 |
on_cloud_providers_object_manager_object_removed (GDBusObjectManager *manager, |
| 325 |
GDBusObject *object, |
| 326 |
gpointer user_data) |
| 327 |
{ |
| 328 |
CloudProvidersProvider *self; |
| 329 |
|
| 330 |
self = CLOUD_PROVIDERS_PROVIDER (user_data); |
| 331 |
update_cloud_providers_accounts (self); |
| 332 |
} |
| 333 |
|
| 334 |
static void |
| 335 |
on_cloud_providers_object_manager_name_owner_changed (GObject *object, |
| 336 |
GParamSpec *pspec, |
| 337 |
gpointer user_data) |
| 338 |
{ |
| 339 |
CloudProvidersProvider *self; |
| 340 |
|
| 341 |
self = CLOUD_PROVIDERS_PROVIDER (user_data); |
| 342 |
g_signal_emit_by_name (G_OBJECT (self), "removed", NULL); |
| 343 |
} |
| 344 |
|
| 345 |
static void |
| 346 |
on_name_changed (CloudProvidersProvider *self) |
| 347 |
{ |
| 348 |
g_free (self->name); |
| 349 |
self->name = cloud_providers_dbus_provider_dup_name (self->proxy); |
| 350 |
if (self->name == NULL) |
| 351 |
{ |
| 352 |
g_debug ("Provider name not ready for %p\n", self); |
| 353 |
return; |
| 354 |
} |
| 355 |
|
| 356 |
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_NAME]); |
| 357 |
} |
| 358 |
|
| 359 |
static void |
| 360 |
on_object_manager_created (GObject *source_object, |
| 361 |
GAsyncResult *res, |
| 362 |
gpointer user_data) |
| 363 |
{ |
| 364 |
CloudProvidersProvider *self; |
| 365 |
g_autoptr(GError) error = NULL; |
| 366 |
GDBusObjectManager *manager; |
| 367 |
|
| 368 |
manager = cloud_providers_dbus_object_manager_client_new_finish (res, &error); |
| 369 |
if (error != NULL) |
| 370 |
{ |
| 371 |
g_printerr ("Error getting object manager client: %s", error->message); |
| 372 |
return; |
| 373 |
} |
| 374 |
|
| 375 |
/* The CloudProvidersProvider could be destroyed before arriving here */ |
| 376 |
self = CLOUD_PROVIDERS_PROVIDER (user_data); |
| 377 |
self->manager = g_steal_pointer (&manager); |
| 378 |
g_signal_connect (self->manager, "notify::name-owner", |
| 379 |
G_CALLBACK (on_cloud_providers_object_manager_name_owner_changed), |
| 380 |
self); |
| 381 |
g_signal_connect (self->manager, "object-added", |
| 382 |
G_CALLBACK (on_cloud_providers_object_manager_object_added), |
| 383 |
self); |
| 384 |
g_signal_connect (self->manager, "object-removed", |
| 385 |
G_CALLBACK (on_cloud_providers_object_manager_object_removed), |
| 386 |
self); |
| 387 |
|
| 388 |
update_cloud_providers_accounts (self); |
| 389 |
} |
| 390 |
|
| 391 |
static void |
| 392 |
on_bus_acquired (GObject *source_object, |
| 393 |
GAsyncResult *res, |
| 394 |
gpointer user_data) |
| 395 |
{ |
| 396 |
CloudProvidersProvider *self; |
| 397 |
g_autoptr(GError) error = NULL; |
| 398 |
GDBusConnection *connection; |
| 399 |
|
| 400 |
connection = g_bus_get_finish (res, &error); |
| 401 |
if (error != NULL) |
| 402 |
{ |
| 403 |
if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) |
| 404 |
g_debug ("Error acquiring bus for cloud provider: %s", error->message); |
| 405 |
return; |
| 406 |
} |
| 407 |
|
| 408 |
/* The CloudProvidersProvider could be destroyed before arriving here */ |
| 409 |
self = CLOUD_PROVIDERS_PROVIDER (user_data); |
| 410 |
self->bus = g_steal_pointer (&connection); |
| 411 |
cloud_providers_dbus_object_manager_client_new (self->bus, |
| 412 |
G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE, |
| 413 |
self->manager_bus_name, |
| 414 |
self->manager_object_path, |
| 415 |
self->cancellable, |
| 416 |
on_object_manager_created, |
| 417 |
self); |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
static void |
| 422 |
cloud_providers_provider_init (CloudProvidersProvider *self) |
| 423 |
{ |
| 424 |
self->cancellable = g_cancellable_new (); |
| 425 |
g_bus_get (G_BUS_TYPE_SESSION, |
| 426 |
self->cancellable, |
| 427 |
on_bus_acquired, |
| 428 |
self); |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
const gchar* |
| 433 |
cloud_providers_provider_get_name (CloudProvidersProvider *self) |
| 434 |
{ |
| 435 |
g_return_val_if_fail (CLOUD_PROVIDERS_IS_PROVIDER (self), NULL); |
| 436 |
|
| 437 |
return self->name; |
| 438 |
} |
| 439 |
|
| 440 |
/** |
| 441 |
* cloud_providers_provider_get_accounts |
| 442 |
* @self: A CloudProvidersProvider |
| 443 |
* |
| 444 |
* Get the list of accounts from this provider. |
| 445 |
* |
| 446 |
* Returns: (element-type CloudProvidersAccount) (transfer none): A list of [class@Account] objects. |
| 447 |
*/ |
| 448 |
GList* |
| 449 |
cloud_providers_provider_get_accounts (CloudProvidersProvider *self) |
| 450 |
{ |
| 451 |
g_return_val_if_fail (CLOUD_PROVIDERS_IS_PROVIDER (self), NULL); |
| 452 |
|
| 453 |
return self->accounts; |
| 454 |
} |
| 455 |
|
| 456 |
|