| 1 |
/* cloudproviders.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 "cloudproviderscollector.h" |
| 21 |
#include "cloudprovidersaccount.h" |
| 22 |
#include "cloudprovidersprovider.h" |
| 23 |
#include "cloudproviders-generated.h" |
| 24 |
#include <glib.h> |
| 25 |
#include <glib/gprintf.h> |
| 26 |
#include <gio/gio.h> |
| 27 |
|
| 28 |
#define KEY_FILE_GROUP "Cloud Providers" |
| 29 |
#define XDG_DESKTOP_IMPLEMENTS "org.freedesktop.CloudProviders" |
| 30 |
|
| 31 |
struct _CloudProvidersCollector |
| 32 |
{ |
| 33 |
GObject parent; |
| 34 |
|
| 35 |
GList *providers; |
| 36 |
GHashTable *providers_bus_names; |
| 37 |
GDBusConnection *bus; |
| 38 |
GCancellable *cancellable; |
| 39 |
GList *monitors; |
| 40 |
}; |
| 41 |
|
| 42 |
G_DEFINE_TYPE (CloudProvidersCollector, cloud_providers_collector, G_TYPE_OBJECT) |
| 43 |
|
| 44 |
/** |
| 45 |
* CloudProvidersCollector: |
| 46 |
* |
| 47 |
* Singleton for tracking all providers. |
| 48 |
* |
| 49 |
* This is a singleton to track all the changes in all providers. |
| 50 |
* Using this class, you can implement integration for all of them at once and represent them in the |
| 51 |
* UI, track new providers added or removed and their status. |
| 52 |
*/ |
| 53 |
|
| 54 |
enum |
| 55 |
{ |
| 56 |
PROVIDERS_CHANGED, |
| 57 |
LAST_SIGNAL |
| 58 |
}; |
| 59 |
|
| 60 |
static guint signals [LAST_SIGNAL]; |
| 61 |
|
| 62 |
static void |
| 63 |
update_cloud_providers (CloudProvidersCollector *self); |
| 64 |
|
| 65 |
static void |
| 66 |
on_bus_acquired (GObject *source_object, |
| 67 |
GAsyncResult *res, |
| 68 |
gpointer user_data) |
| 69 |
{ |
| 70 |
CloudProvidersCollector *self; |
| 71 |
g_autoptr(GError) error = NULL; |
| 72 |
GDBusConnection *connection; |
| 73 |
|
| 74 |
connection = g_bus_get_finish (res, &error); |
| 75 |
if (error != NULL) |
| 76 |
{ |
| 77 |
if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) |
| 78 |
g_debug ("Error acquiring bus for cloud providers: %s", error->message); |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
/* The CloudProvidersCollector could be destroyed before arriving here */ |
| 83 |
self = CLOUD_PROVIDERS_COLLECTOR (user_data); |
| 84 |
self->bus = g_steal_pointer (&connection); |
| 85 |
update_cloud_providers (self); |
| 86 |
} |
| 87 |
|
| 88 |
static void |
| 89 |
cloud_providers_collector_dispose (GObject *object) |
| 90 |
{ |
| 91 |
CloudProvidersCollector *self = (CloudProvidersCollector *)object; |
| 92 |
GList *l; |
| 93 |
|
| 94 |
g_cancellable_cancel (self->cancellable); |
| 95 |
g_clear_object (&self->cancellable); |
| 96 |
for (l = self->monitors; l != NULL; l = l->next) |
| 97 |
{ |
| 98 |
g_signal_handlers_disconnect_by_data (G_OBJECT (l->data), self); |
| 99 |
} |
| 100 |
for (l = self->providers; l != NULL; l = l->next) |
| 101 |
{ |
| 102 |
g_signal_handlers_disconnect_by_data (G_OBJECT (l->data), self); |
| 103 |
} |
| 104 |
g_clear_pointer (&self->providers_bus_names, g_hash_table_unref); |
| 105 |
g_list_free_full (self->providers, g_object_unref); |
| 106 |
self->providers = NULL; |
| 107 |
g_list_free_full (self->monitors, g_object_unref); |
| 108 |
self->monitors = NULL; |
| 109 |
g_clear_object (&self->bus); |
| 110 |
|
| 111 |
G_OBJECT_CLASS (cloud_providers_collector_parent_class)->dispose (object); |
| 112 |
} |
| 113 |
|
| 114 |
static void |
| 115 |
cloud_providers_collector_class_init (CloudProvidersCollectorClass *klass) |
| 116 |
{ |
| 117 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 118 |
|
| 119 |
object_class->dispose = cloud_providers_collector_dispose; |
| 120 |
|
| 121 |
/** |
| 122 |
* CloudProviderCollector::providers-changed |
| 123 |
* |
| 124 |
* This signal is emitted by the amount of providers changed. |
| 125 |
*/ |
| 126 |
signals [PROVIDERS_CHANGED] = g_signal_new ("providers-changed", |
| 127 |
G_TYPE_FROM_CLASS (klass), |
| 128 |
G_SIGNAL_RUN_LAST, |
| 129 |
0, |
| 130 |
NULL, |
| 131 |
NULL, |
| 132 |
g_cclosure_marshal_generic, |
| 133 |
G_TYPE_NONE, |
| 134 |
0); |
| 135 |
} |
| 136 |
|
| 137 |
static void |
| 138 |
cloud_providers_collector_init (CloudProvidersCollector *self) |
| 139 |
{ |
| 140 |
self->cancellable = g_cancellable_new (); |
| 141 |
self->providers_bus_names = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); |
| 142 |
|
| 143 |
g_bus_get (G_BUS_TYPE_SESSION, |
| 144 |
self->cancellable, |
| 145 |
on_bus_acquired, |
| 146 |
self); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* cloud_providers_collector_get_providers |
| 151 |
* @self: A collector |
| 152 |
* |
| 153 |
* Get the list of currently managed providers. |
| 154 |
* |
| 155 |
* Returns: (element-type CloudProvidersProvider) (transfer none): A list of [class@Provider] objects. |
| 156 |
*/ |
| 157 |
GList* |
| 158 |
cloud_providers_collector_get_providers (CloudProvidersCollector *self) |
| 159 |
{ |
| 160 |
return self->providers; |
| 161 |
} |
| 162 |
|
| 163 |
static void |
| 164 |
on_provider_removed (CloudProvidersCollector *self) |
| 165 |
{ |
| 166 |
update_cloud_providers (self); |
| 167 |
} |
| 168 |
|
| 169 |
static void |
| 170 |
add_cloud_provider (CloudProvidersCollector *self, |
| 171 |
const gchar *bus_name, |
| 172 |
const gchar *object_path) |
| 173 |
{ |
| 174 |
g_autoptr(CloudProvidersProvider) provider = NULL; |
| 175 |
|
| 176 |
provider = cloud_providers_provider_new (bus_name, object_path); |
| 177 |
if (g_hash_table_contains (self->providers_bus_names, bus_name)) |
| 178 |
{ |
| 179 |
g_debug ("Skipped duplicate provider: %s %s\n", bus_name, object_path); |
| 180 |
return; |
| 181 |
} |
| 182 |
|
| 183 |
self->providers = g_list_append (self->providers, g_object_ref (provider)); |
| 184 |
g_hash_table_add (self->providers_bus_names, g_strdup (bus_name)); |
| 185 |
g_signal_connect_swapped (provider, "removed", |
| 186 |
G_CALLBACK (on_provider_removed), self); |
| 187 |
|
| 188 |
g_debug ("Client loading provider: %s %s\n", bus_name, object_path); |
| 189 |
} |
| 190 |
|
| 191 |
static void |
| 192 |
load_cloud_provider_from_desktop_file (CloudProvidersCollector *self, |
| 193 |
GFile *file) |
| 194 |
{ |
| 195 |
g_autoptr(GKeyFile) key_file = NULL; |
| 196 |
g_autoptr(GError) error = NULL; |
| 197 |
g_autofree gchar *path = NULL; |
| 198 |
g_auto(GStrv) implements = NULL; |
| 199 |
g_autofree gchar *bus_name = NULL; |
| 200 |
g_autofree gchar *object_path = NULL; |
| 201 |
|
| 202 |
path = g_file_get_path (file); |
| 203 |
if (!g_str_has_suffix (path, ".desktop")) |
| 204 |
{ |
| 205 |
return; |
| 206 |
} |
| 207 |
|
| 208 |
key_file = g_key_file_new (); |
| 209 |
if (!g_key_file_load_from_file (key_file, path, G_KEY_FILE_NONE, &error)) |
| 210 |
{ |
| 211 |
g_warning ("Error loading .desktop file at %s: %s", path, error->message); |
| 212 |
return; |
| 213 |
} |
| 214 |
|
| 215 |
implements = g_key_file_get_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, "Implements", NULL, &error); |
| 216 |
if (implements == NULL) |
| 217 |
{ |
| 218 |
if (!g_error_matches (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND)) |
| 219 |
{ |
| 220 |
g_warning ("Error loading Implements key from %s: %s", path, error->message); |
| 221 |
} |
| 222 |
|
| 223 |
return; |
| 224 |
} |
| 225 |
|
| 226 |
if (!g_strv_contains((const gchar * const *) implements, XDG_DESKTOP_IMPLEMENTS)) |
| 227 |
{ |
| 228 |
return; |
| 229 |
} |
| 230 |
|
| 231 |
bus_name = g_key_file_get_string (key_file, XDG_DESKTOP_IMPLEMENTS, "BusName", NULL); |
| 232 |
object_path = g_key_file_get_string (key_file, XDG_DESKTOP_IMPLEMENTS, "ObjectPath", NULL); |
| 233 |
|
| 234 |
if (!bus_name || !object_path) |
| 235 |
{ |
| 236 |
g_warning ("Unable to find BusName or ObjectPath keys in "XDG_DESKTOP_IMPLEMENTS" section from %s", path); |
| 237 |
return; |
| 238 |
} |
| 239 |
|
| 240 |
add_cloud_provider (self, bus_name, object_path); |
| 241 |
} |
| 242 |
|
| 243 |
static void |
| 244 |
load_cloud_provider_from_key_file (CloudProvidersCollector *self, |
| 245 |
GFile *file) |
| 246 |
{ |
| 247 |
g_autoptr(GKeyFile) key_file = g_key_file_new (); |
| 248 |
g_autofree gchar *path = NULL; |
| 249 |
GError *error = NULL; |
| 250 |
g_autofree gchar *bus_name = NULL; |
| 251 |
g_autofree gchar *object_path = NULL; |
| 252 |
CloudProvidersProvider *provider; |
| 253 |
|
| 254 |
path = g_file_get_path (file); |
| 255 |
g_key_file_load_from_file (key_file, path, G_KEY_FILE_NONE, &error); |
| 256 |
if (error != NULL) |
| 257 |
{ |
| 258 |
g_debug ("Error while loading cloud provider key file at %s with error %s", path, error->message); |
| 259 |
return; |
| 260 |
} |
| 261 |
|
| 262 |
if (!g_key_file_has_group (key_file, KEY_FILE_GROUP)) |
| 263 |
{ |
| 264 |
g_debug ("Error while loading cloud provider key file at %s with error %s", path, error->message); |
| 265 |
return; |
| 266 |
} |
| 267 |
|
| 268 |
bus_name = g_key_file_get_string (key_file, KEY_FILE_GROUP, "BusName", &error); |
| 269 |
if (error != NULL) |
| 270 |
{ |
| 271 |
g_debug ("Error while loading cloud provider key file at %s with error %s", path, error->message); |
| 272 |
return; |
| 273 |
} |
| 274 |
object_path = g_key_file_get_string (key_file, KEY_FILE_GROUP, "ObjectPath", &error); |
| 275 |
if (error != NULL) |
| 276 |
{ |
| 277 |
g_debug ("Error while loading cloud provider key file at %s with error %s", path, error->message); |
| 278 |
return; |
| 279 |
} |
| 280 |
|
| 281 |
provider = cloud_providers_provider_new (bus_name, object_path); |
| 282 |
self->providers = g_list_append (self->providers, provider); |
| 283 |
g_signal_connect_swapped (provider, "removed", |
| 284 |
G_CALLBACK (on_provider_removed), self); |
| 285 |
|
| 286 |
g_debug("Client loading provider: %s %s\n", bus_name, object_path); |
| 287 |
} |
| 288 |
|
| 289 |
static void |
| 290 |
on_providers_file_changed (CloudProvidersCollector *self) |
| 291 |
{ |
| 292 |
update_cloud_providers (self); |
| 293 |
} |
| 294 |
|
| 295 |
static void |
| 296 |
load_cloud_providers (CloudProvidersCollector *self) |
| 297 |
{ |
| 298 |
const gchar* const *data_dirs; |
| 299 |
const gchar *data_subdirs[] = {"applications", "cloud-providers"}; |
| 300 |
guint i; |
| 301 |
gsize j; |
| 302 |
|
| 303 |
data_dirs = g_get_system_data_dirs (); |
| 304 |
for (i = 0; data_dirs[i] != NULL; i++) |
| 305 |
{ |
| 306 |
for (j = 0; j < G_N_ELEMENTS (data_subdirs); j++) |
| 307 |
{ |
| 308 |
g_autoptr (GFile) directory_file = NULL; |
| 309 |
g_autoptr (GFileInfo) info = NULL; |
| 310 |
g_autoptr (GFileEnumerator) file_enumerator = NULL; |
| 311 |
g_autoptr (GFileMonitor) monitor = NULL; |
| 312 |
g_autoptr (GError) error = NULL; |
| 313 |
|
| 314 |
directory_file = g_file_new_build_filename (data_dirs[i], data_subdirs[j], NULL); |
| 315 |
monitor = g_file_monitor (directory_file, G_FILE_MONITOR_WATCH_MOVES, |
| 316 |
self->cancellable, NULL); |
| 317 |
g_signal_connect_swapped (monitor, "changed", G_CALLBACK (on_providers_file_changed), self); |
| 318 |
self->monitors = g_list_append (self->monitors, g_steal_pointer (&monitor)); |
| 319 |
file_enumerator = g_file_enumerate_children (directory_file, |
| 320 |
"standard::name,standard::type", |
| 321 |
G_FILE_QUERY_INFO_NONE, |
| 322 |
NULL, |
| 323 |
&error); |
| 324 |
if (error) |
| 325 |
{ |
| 326 |
g_clear_error (&error); |
| 327 |
continue; |
| 328 |
} |
| 329 |
|
| 330 |
info = g_file_enumerator_next_file (file_enumerator, NULL, &error); |
| 331 |
if (error) |
| 332 |
{ |
| 333 |
g_autofree gchar *directory_path = g_file_get_path (directory_file); |
| 334 |
g_warning ("Error while enumerating file %s error: %s\n", directory_path, error->message); |
| 335 |
g_clear_error (&error); |
| 336 |
continue; |
| 337 |
} |
| 338 |
|
| 339 |
while (info != NULL && error == NULL) |
| 340 |
{ |
| 341 |
g_autoptr(GFile) child = g_file_enumerator_get_child (file_enumerator, info); |
| 342 |
if (strcmp (data_subdirs[j], "applications") == 0) |
| 343 |
{ |
| 344 |
load_cloud_provider_from_desktop_file (self, child); |
| 345 |
} |
| 346 |
else |
| 347 |
{ |
| 348 |
load_cloud_provider_from_key_file (self, child); |
| 349 |
} |
| 350 |
|
| 351 |
g_clear_object (&info); |
| 352 |
info = g_file_enumerator_next_file (file_enumerator, NULL, &error); |
| 353 |
} |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
static void |
| 359 |
update_cloud_providers (CloudProvidersCollector *self) |
| 360 |
{ |
| 361 |
GList *l; |
| 362 |
|
| 363 |
g_cancellable_cancel (self->cancellable); |
| 364 |
g_clear_object (&self->cancellable); |
| 365 |
self->cancellable = g_cancellable_new (); |
| 366 |
for (l = self->monitors; l != NULL; l = l->next) |
| 367 |
{ |
| 368 |
g_signal_handlers_disconnect_by_data (G_OBJECT (l->data), self); |
| 369 |
} |
| 370 |
for (l = self->providers; l != NULL; l = l->next) |
| 371 |
{ |
| 372 |
g_signal_handlers_disconnect_by_data (G_OBJECT (l->data), self); |
| 373 |
} |
| 374 |
g_hash_table_remove_all (self->providers_bus_names); |
| 375 |
g_list_free_full (self->providers, g_object_unref); |
| 376 |
g_list_free_full (self->monitors, g_object_unref); |
| 377 |
self->providers = NULL; |
| 378 |
self->monitors = NULL; |
| 379 |
|
| 380 |
load_cloud_providers (self); |
| 381 |
|
| 382 |
g_signal_emit_by_name (G_OBJECT (self), "providers-changed"); |
| 383 |
} |
| 384 |
|
| 385 |
static gpointer |
| 386 |
singleton_creation_thread (gpointer data) |
| 387 |
{ |
| 388 |
return g_object_new (CLOUD_PROVIDERS_TYPE_COLLECTOR, NULL); |
| 389 |
} |
| 390 |
|
| 391 |
/** |
| 392 |
* cloud_providers_collector_dup_singleton: |
| 393 |
* |
| 394 |
* Main object to track changes in all providers. |
| 395 |
* |
| 396 |
* Returns: (transfer full): A manager singleton |
| 397 |
*/ |
| 398 |
CloudProvidersCollector * |
| 399 |
cloud_providers_collector_dup_singleton (void) |
| 400 |
{ |
| 401 |
static GOnce collector_singleton = G_ONCE_INIT; |
| 402 |
CloudProvidersCollector *self; |
| 403 |
|
| 404 |
g_once (&collector_singleton, singleton_creation_thread, NULL); |
| 405 |
|
| 406 |
self = CLOUD_PROVIDERS_COLLECTOR (collector_singleton.retval); |
| 407 |
return g_object_ref (self); |
| 408 |
} |
| 409 |
|