| 1 |
/* cloudprovidersexporter.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 "cloudprovidersproviderexporter.h" |
| 21 |
#include "cloudprovidersaccountexporterpriv.h" |
| 22 |
#include "cloudprovidersproviderexporterpriv.h" |
| 23 |
#include "cloudprovidersaccountexporter.h" |
| 24 |
#include "cloudproviders-generated.h" |
| 25 |
#include <gio/gio.h> |
| 26 |
|
| 27 |
struct _CloudProvidersProviderExporter |
| 28 |
{ |
| 29 |
GObject parent; |
| 30 |
|
| 31 |
CloudProvidersDbusProvider *skeleton; |
| 32 |
GDBusConnection *bus; |
| 33 |
GDBusObjectManagerServer *manager; |
| 34 |
|
| 35 |
gchar *bus_name; |
| 36 |
gchar *bus_path; |
| 37 |
gchar *provider_bus_path; |
| 38 |
|
| 39 |
gchar *name; |
| 40 |
GList *accounts; |
| 41 |
}; |
| 42 |
|
| 43 |
G_DEFINE_TYPE (CloudProvidersProviderExporter, cloud_providers_provider_exporter, G_TYPE_OBJECT) |
| 44 |
|
| 45 |
enum |
| 46 |
{ |
| 47 |
PROP_0, |
| 48 |
PROP_NAME, |
| 49 |
PROP_BUS_NAME, |
| 50 |
PROP_BUS_PATH, |
| 51 |
PROP_BUS, |
| 52 |
N_PROPS |
| 53 |
}; |
| 54 |
|
| 55 |
static GParamSpec *properties [N_PROPS]; |
| 56 |
|
| 57 |
/** |
| 58 |
* CloudProvidersProviderExporter: |
| 59 |
* |
| 60 |
* Base object for representing a single single provider. |
| 61 |
*/ |
| 62 |
|
| 63 |
static void |
| 64 |
export_provider (CloudProvidersProviderExporter *self) |
| 65 |
{ |
| 66 |
g_autoptr(CloudProvidersDbusObjectSkeleton) provider_object_skeleton = NULL; |
| 67 |
|
| 68 |
provider_object_skeleton = cloud_providers_dbus_object_skeleton_new (self->provider_bus_path); |
| 69 |
cloud_providers_dbus_object_skeleton_set_provider (provider_object_skeleton, self->skeleton); |
| 70 |
g_dbus_object_manager_server_export (self->manager, |
| 71 |
G_DBUS_OBJECT_SKELETON (provider_object_skeleton)); |
| 72 |
|
| 73 |
g_debug ("provider object path: %s %s\n", self->provider_bus_path, |
| 74 |
g_dbus_object_manager_get_object_path (G_DBUS_OBJECT_MANAGER (self->manager))); |
| 75 |
} |
| 76 |
|
| 77 |
static void |
| 78 |
export_account (CloudProvidersProviderExporter *self, |
| 79 |
CloudProvidersAccountExporter *account) |
| 80 |
{ |
| 81 |
CloudProvidersDbusAccount *account_skeleton; |
| 82 |
const gchar *account_object_path; |
| 83 |
g_autoptr(CloudProvidersDbusObjectSkeleton) account_object_skeleton = NULL; |
| 84 |
|
| 85 |
account_object_path = cloud_providers_account_exporter_get_object_path (account); |
| 86 |
account_skeleton = cloud_providers_account_exporter_get_skeleton (account); |
| 87 |
account_object_skeleton = cloud_providers_dbus_object_skeleton_new (account_object_path); |
| 88 |
cloud_providers_dbus_object_skeleton_set_account (account_object_skeleton, account_skeleton); |
| 89 |
g_dbus_object_manager_server_export (self->manager, G_DBUS_OBJECT_SKELETON (account_object_skeleton)); |
| 90 |
g_debug ("account object path: %s %s\n", account_object_path, |
| 91 |
g_dbus_object_manager_get_object_path (G_DBUS_OBJECT_MANAGER (self->manager))); |
| 92 |
} |
| 93 |
|
| 94 |
static void |
| 95 |
unexport_account(CloudProvidersProviderExporter *self, |
| 96 |
CloudProvidersAccountExporter *account) |
| 97 |
{ |
| 98 |
const gchar *object_path; |
| 99 |
CloudProvidersDbusAccount *account_skeleton; |
| 100 |
|
| 101 |
account_skeleton = cloud_providers_account_exporter_get_skeleton (account); |
| 102 |
object_path = g_dbus_interface_skeleton_get_object_path (G_DBUS_INTERFACE_SKELETON (account_skeleton)); |
| 103 |
g_dbus_object_manager_server_unexport (self->manager, object_path); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* cloud_providers_provider_exporter_add_account: |
| 108 |
* @self: The cloud provider exporter |
| 109 |
* @account: The account object |
| 110 |
* |
| 111 |
* Each cloud provider can have a variety of account associated with it. Use this |
| 112 |
* function to add the accounts the user set up. This function is currently only internal, |
| 113 |
* as we do automation for the dbus handling for adding and exporting an account. |
| 114 |
* This is handled in [[email protected]]. |
| 115 |
*/ |
| 116 |
void |
| 117 |
cloud_providers_provider_exporter_add_account (CloudProvidersProviderExporter *self, |
| 118 |
CloudProvidersAccountExporter *account) |
| 119 |
{ |
| 120 |
export_account (self, account); |
| 121 |
self->accounts = g_list_append (self->accounts, g_object_ref (account)); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* cloud_providers_provider_exporter_remove_account: |
| 126 |
* @self: The cloud provider exporter |
| 127 |
* @account: The account object |
| 128 |
* |
| 129 |
* Each cloud provider can have a variety of account associated with it. Use this |
| 130 |
* function to remove the accounts that were added when created by [[email protected]]. |
| 131 |
*/ |
| 132 |
void |
| 133 |
cloud_providers_provider_exporter_remove_account (CloudProvidersProviderExporter *self, |
| 134 |
CloudProvidersAccountExporter *account) |
| 135 |
{ |
| 136 |
GList *removed_account; |
| 137 |
|
| 138 |
unexport_account (self, account); |
| 139 |
removed_account = g_list_find (self->accounts, account); |
| 140 |
g_return_if_fail (removed_account != NULL); |
| 141 |
self->accounts = g_list_remove (self->accounts, removed_account); |
| 142 |
g_object_unref (account); |
| 143 |
} |
| 144 |
|
| 145 |
static void |
| 146 |
cloud_providers_provider_exporter_get_property (GObject *object, |
| 147 |
guint prop_id, |
| 148 |
GValue *value, |
| 149 |
GParamSpec *pspec) |
| 150 |
{ |
| 151 |
CloudProvidersProviderExporter *self = CLOUD_PROVIDERS_PROVIDER_EXPORTER (object); |
| 152 |
|
| 153 |
switch (prop_id) |
| 154 |
{ |
| 155 |
case PROP_NAME: |
| 156 |
{ |
| 157 |
g_value_set_string (value, self->name); |
| 158 |
} |
| 159 |
break; |
| 160 |
|
| 161 |
case PROP_BUS_NAME: |
| 162 |
{ |
| 163 |
g_value_set_string (value, self->bus_name); |
| 164 |
} |
| 165 |
break; |
| 166 |
|
| 167 |
case PROP_BUS_PATH: |
| 168 |
{ |
| 169 |
g_value_set_string (value, self->bus_path); |
| 170 |
} |
| 171 |
break; |
| 172 |
|
| 173 |
case PROP_BUS: |
| 174 |
{ |
| 175 |
g_value_set_object (value, self->bus); |
| 176 |
} |
| 177 |
break; |
| 178 |
|
| 179 |
default: |
| 180 |
{ |
| 181 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 182 |
} |
| 183 |
|
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
static void |
| 188 |
cloud_providers_provider_exporter_set_property (GObject *object, |
| 189 |
guint prop_id, |
| 190 |
const GValue *value, |
| 191 |
GParamSpec *pspec) |
| 192 |
{ |
| 193 |
CloudProvidersProviderExporter *self = CLOUD_PROVIDERS_PROVIDER_EXPORTER (object); |
| 194 |
|
| 195 |
switch (prop_id) |
| 196 |
{ |
| 197 |
case PROP_NAME: |
| 198 |
{ |
| 199 |
g_free (self->name); |
| 200 |
self->name = g_value_dup_string (value); |
| 201 |
g_debug ("setting name %s\n", self->name); |
| 202 |
cloud_providers_dbus_provider_set_name (self->skeleton, self->name); |
| 203 |
} |
| 204 |
break; |
| 205 |
|
| 206 |
case PROP_BUS_NAME: |
| 207 |
{ |
| 208 |
g_return_if_fail (self->bus_name == NULL); |
| 209 |
self->bus_name = g_value_dup_string (value); |
| 210 |
} |
| 211 |
break; |
| 212 |
|
| 213 |
case PROP_BUS_PATH: |
| 214 |
{ |
| 215 |
g_return_if_fail (self->bus_path == NULL); |
| 216 |
self->bus_path = g_value_dup_string (value); |
| 217 |
} |
| 218 |
break; |
| 219 |
|
| 220 |
case PROP_BUS: |
| 221 |
{ |
| 222 |
g_return_if_fail (self->bus == NULL); |
| 223 |
self->bus = g_value_dup_object (value); |
| 224 |
} |
| 225 |
break; |
| 226 |
|
| 227 |
default: |
| 228 |
{ |
| 229 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
static void |
| 235 |
cloud_providers_provider_exporter_dispose (GObject *object) |
| 236 |
{ |
| 237 |
CloudProvidersProviderExporter *self = (CloudProvidersProviderExporter *)object; |
| 238 |
|
| 239 |
g_clear_object (&self->skeleton); |
| 240 |
g_clear_object (&self->manager); |
| 241 |
g_clear_object (&self->bus); |
| 242 |
|
| 243 |
g_list_free_full (self->accounts, g_object_unref); |
| 244 |
self->accounts = NULL; |
| 245 |
|
| 246 |
G_OBJECT_CLASS (cloud_providers_provider_exporter_parent_class)->dispose (object); |
| 247 |
} |
| 248 |
|
| 249 |
static void |
| 250 |
cloud_providers_provider_exporter_finalize (GObject *object) |
| 251 |
{ |
| 252 |
CloudProvidersProviderExporter *self = (CloudProvidersProviderExporter *)object; |
| 253 |
|
| 254 |
g_clear_pointer (&self->bus_name, g_free); |
| 255 |
g_clear_pointer (&self->bus_path, g_free); |
| 256 |
g_clear_pointer (&self->provider_bus_path, g_free); |
| 257 |
g_clear_pointer (&self->name, g_free); |
| 258 |
|
| 259 |
G_OBJECT_CLASS (cloud_providers_provider_exporter_parent_class)->finalize (object); |
| 260 |
} |
| 261 |
|
| 262 |
static void |
| 263 |
cloud_providers_provider_exporter_constructed (GObject *object) |
| 264 |
{ |
| 265 |
CloudProvidersProviderExporter *self = CLOUD_PROVIDERS_PROVIDER_EXPORTER (object); |
| 266 |
|
| 267 |
self->manager = g_dbus_object_manager_server_new (self->bus_path); |
| 268 |
self->provider_bus_path = g_strconcat (self->bus_path, "/Provider", NULL); |
| 269 |
g_debug ("constructed, manager %s", self->bus_path); |
| 270 |
self->skeleton = cloud_providers_dbus_provider_skeleton_new (); |
| 271 |
g_dbus_object_manager_server_set_connection (self->manager, self->bus); |
| 272 |
export_provider (self); |
| 273 |
} |
| 274 |
|
| 275 |
static void |
| 276 |
cloud_providers_provider_exporter_init (CloudProvidersProviderExporter *self) |
| 277 |
{ |
| 278 |
} |
| 279 |
|
| 280 |
static void |
| 281 |
cloud_providers_provider_exporter_class_init (CloudProvidersProviderExporterClass *klass) |
| 282 |
{ |
| 283 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 284 |
object_class->set_property = cloud_providers_provider_exporter_set_property; |
| 285 |
object_class->get_property = cloud_providers_provider_exporter_get_property; |
| 286 |
object_class->constructed = cloud_providers_provider_exporter_constructed; |
| 287 |
object_class->dispose = cloud_providers_provider_exporter_dispose; |
| 288 |
object_class->finalize = cloud_providers_provider_exporter_finalize; |
| 289 |
|
| 290 |
/** |
| 291 |
* CloudProvidersProviderExporter:name: |
| 292 |
* |
| 293 |
* The name of the cloud provider. |
| 294 |
*/ |
| 295 |
properties [PROP_NAME] = |
| 296 |
g_param_spec_string ("name", NULL, NULL, |
| 297 |
NULL, |
| 298 |
(G_PARAM_READWRITE | |
| 299 |
G_PARAM_STATIC_STRINGS)); |
| 300 |
|
| 301 |
/** |
| 302 |
* CloudProvidersProviderExporter:bus-name: |
| 303 |
* |
| 304 |
* The bus name of the provider. |
| 305 |
*/ |
| 306 |
properties [PROP_BUS_NAME] = |
| 307 |
g_param_spec_string ("bus-name", NULL, NULL, |
| 308 |
NULL, |
| 309 |
(G_PARAM_READWRITE | |
| 310 |
G_PARAM_STATIC_STRINGS | |
| 311 |
G_PARAM_CONSTRUCT_ONLY)); |
| 312 |
|
| 313 |
/** |
| 314 |
* CloudProvidersProviderExporter:bus-path: |
| 315 |
* |
| 316 |
* The bus path of the provider. |
| 317 |
*/ |
| 318 |
properties [PROP_BUS_PATH] = |
| 319 |
g_param_spec_string ("bus-path", NULL, NULL, |
| 320 |
NULL, |
| 321 |
(G_PARAM_READWRITE | |
| 322 |
G_PARAM_STATIC_STRINGS | |
| 323 |
G_PARAM_CONSTRUCT_ONLY)); |
| 324 |
|
| 325 |
/** |
| 326 |
* CloudProvidersProviderExporter:bus: |
| 327 |
* |
| 328 |
* The bus connection object for the provider. |
| 329 |
*/ |
| 330 |
properties [PROP_BUS] = |
| 331 |
g_param_spec_object ("bus", NULL, NULL, |
| 332 |
G_TYPE_DBUS_CONNECTION, |
| 333 |
(G_PARAM_READWRITE | |
| 334 |
G_PARAM_STATIC_STRINGS | |
| 335 |
G_PARAM_CONSTRUCT_ONLY)); |
| 336 |
g_object_class_install_properties (object_class, |
| 337 |
N_PROPS, |
| 338 |
properties); |
| 339 |
} |
| 340 |
|
| 341 |
void |
| 342 |
cloud_providers_provider_exporter_set_name (CloudProvidersProviderExporter *self, |
| 343 |
const gchar *name) |
| 344 |
{ |
| 345 |
g_object_set (self, "name", name, NULL); |
| 346 |
} |
| 347 |
|
| 348 |
const gchar* |
| 349 |
cloud_providers_provider_exporter_get_name (CloudProvidersProviderExporter *self) |
| 350 |
{ |
| 351 |
return self->name; |
| 352 |
} |
| 353 |
|
| 354 |
/** |
| 355 |
* cloud_providers_provider_exporter_new |
| 356 |
* @bus: A connection to export the objects to |
| 357 |
* @bus_name: A DBus name to bind to |
| 358 |
* @bus_path: A DBus object path |
| 359 |
*/ |
| 360 |
CloudProvidersProviderExporter * |
| 361 |
cloud_providers_provider_exporter_new (GDBusConnection *bus, |
| 362 |
const gchar *bus_name, |
| 363 |
const gchar *bus_path) |
| 364 |
{ |
| 365 |
CloudProvidersProviderExporter *self; |
| 366 |
|
| 367 |
self = g_object_new (CLOUD_PROVIDERS_TYPE_PROVIDER_EXPORTER, |
| 368 |
"bus", bus, |
| 369 |
"bus-name", bus_name, |
| 370 |
"bus-path", bus_path, |
| 371 |
NULL); |
| 372 |
|
| 373 |
return self; |
| 374 |
} |
| 375 |
|
| 376 |
GDBusConnection* |
| 377 |
cloud_providers_provider_exporter_get_bus (CloudProvidersProviderExporter *self) |
| 378 |
{ |
| 379 |
return self->bus; |
| 380 |
} |
| 381 |
|
| 382 |
const gchar* |
| 383 |
cloud_providers_provider_exporter_get_object_path (CloudProvidersProviderExporter *self) |
| 384 |
{ |
| 385 |
return self->bus_path; |
| 386 |
} |