| 1 |
#include <glib.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include <gio/gio.h> |
| 4 |
#include <cloudprovidersproviderexporter.h> |
| 5 |
#include <cloudprovidersaccountexporter.h> |
| 6 |
/* for CLoudProviderStatus enum */ |
| 7 |
#include <cloudprovidersaccount.h> |
| 8 |
|
| 9 |
|
| 10 |
#define TIMEOUT 800 |
| 11 |
#define COUNT_PLACEHOLDER_ACCOUNTS 3 |
| 12 |
#define TEST_CLOUD_PROVIDERS_BUS_NAME "org.freedesktop.CloudProviders.ServerExample" |
| 13 |
#define TEST_CLOUD_PROVIDERS_OBJECT_PATH "/org/freedesktop/CloudProviders/ServerExample" |
| 14 |
|
| 15 |
#define CLOUD_PROVIDERS_TYPE_TEST_SERVER (cloud_providers_test_server_get_type()) |
| 16 |
G_DECLARE_FINAL_TYPE (CloudProvidersTestServer, cloud_providers_test_server, CLOUD_PROVIDERS, TEST_SERVER, GObject); |
| 17 |
|
| 18 |
struct _CloudProvidersTestServerClass |
| 19 |
{ |
| 20 |
GObjectClass parent_class; |
| 21 |
}; |
| 22 |
|
| 23 |
struct _CloudProvidersTestServer |
| 24 |
{ |
| 25 |
GObject parent_instance; |
| 26 |
|
| 27 |
GHashTable *accounts; |
| 28 |
gchar *name; |
| 29 |
GIcon *icon; |
| 30 |
gchar *path; |
| 31 |
guint timeout_handler; |
| 32 |
CloudProvidersProviderExporter *exporter; |
| 33 |
}; |
| 34 |
|
| 35 |
G_DEFINE_TYPE (CloudProvidersTestServer, cloud_providers_test_server, G_TYPE_OBJECT); |
| 36 |
|
| 37 |
static CloudProvidersTestServer* |
| 38 |
cloud_providers_test_server_new (void) |
| 39 |
{ |
| 40 |
CloudProvidersTestServer *self; |
| 41 |
|
| 42 |
self = g_object_new (CLOUD_PROVIDERS_TYPE_TEST_SERVER, NULL); |
| 43 |
|
| 44 |
return self; |
| 45 |
} |
| 46 |
|
| 47 |
static void |
| 48 |
test_cloud_provider_finalize (GObject *object) |
| 49 |
{ |
| 50 |
CloudProvidersTestServer *self = CLOUD_PROVIDERS_TEST_SERVER (object); |
| 51 |
|
| 52 |
g_clear_handle_id (&self->timeout_handler, g_source_remove); |
| 53 |
g_clear_pointer (&self->accounts, g_hash_table_unref); |
| 54 |
g_clear_pointer (&self->name, g_free); |
| 55 |
g_clear_pointer (&self->path, g_free); |
| 56 |
g_clear_object (&self->icon); |
| 57 |
g_clear_object (&self->exporter); |
| 58 |
|
| 59 |
G_OBJECT_CLASS (cloud_providers_test_server_parent_class)->finalize (object); |
| 60 |
} |
| 61 |
|
| 62 |
static void |
| 63 |
cloud_providers_test_server_init (CloudProvidersTestServer *self) |
| 64 |
{ |
| 65 |
g_autoptr(GFile) icon_file = NULL; |
| 66 |
g_autofree gchar *current_dir = NULL; |
| 67 |
g_autofree gchar *uri = NULL; |
| 68 |
|
| 69 |
current_dir = g_get_current_dir (); |
| 70 |
|
| 71 |
self->accounts = g_hash_table_new_full (NULL, NULL, NULL, g_object_unref); |
| 72 |
self->name = g_strdup ("MyCloud"); |
| 73 |
self->path = g_strdup (current_dir); |
| 74 |
uri = g_build_filename (current_dir, "icon.svg", NULL); |
| 75 |
icon_file = g_file_new_for_uri (uri); |
| 76 |
self->icon = g_file_icon_new (icon_file); |
| 77 |
} |
| 78 |
|
| 79 |
static void |
| 80 |
cloud_providers_test_server_class_init (CloudProvidersTestServerClass *class) |
| 81 |
{ |
| 82 |
GObjectClass *gobject_class = G_OBJECT_CLASS (class); |
| 83 |
|
| 84 |
gobject_class->finalize = test_cloud_provider_finalize; |
| 85 |
} |
| 86 |
|
| 87 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 88 |
|
| 89 |
static void |
| 90 |
activate_action (GSimpleAction *action, |
| 91 |
GVariant *parameter, |
| 92 |
gpointer user_data) |
| 93 |
{ |
| 94 |
g_print ("Action %s activated\n", g_action_get_name (G_ACTION (action))); |
| 95 |
} |
| 96 |
|
| 97 |
static void |
| 98 |
activate_toggle (GSimpleAction *action, |
| 99 |
GVariant *parameter, |
| 100 |
gpointer user_data) |
| 101 |
{ |
| 102 |
GVariant *old_state, *new_state; |
| 103 |
|
| 104 |
old_state = g_action_get_state (G_ACTION (action)); |
| 105 |
new_state = g_variant_new_boolean (!g_variant_get_boolean (old_state)); |
| 106 |
|
| 107 |
g_print ("Toggle action %s activated, state changes from %d to %d\n", |
| 108 |
g_action_get_name (G_ACTION (action)), |
| 109 |
g_variant_get_boolean (old_state), |
| 110 |
g_variant_get_boolean (new_state)); |
| 111 |
|
| 112 |
g_simple_action_set_state (action, new_state); |
| 113 |
g_variant_unref (old_state); |
| 114 |
} |
| 115 |
|
| 116 |
static void |
| 117 |
activate_radio (GSimpleAction *action, |
| 118 |
GVariant *parameter, |
| 119 |
gpointer user_data) |
| 120 |
{ |
| 121 |
GVariant *old_state, *new_state; |
| 122 |
|
| 123 |
old_state = g_action_get_state (G_ACTION (action)); |
| 124 |
new_state = g_variant_new_string (g_variant_get_string (parameter, NULL)); |
| 125 |
|
| 126 |
g_print ("Radio action %s activated, state changes from %s to %s\n", |
| 127 |
g_action_get_name (G_ACTION (action)), |
| 128 |
g_variant_get_string (old_state, NULL), |
| 129 |
g_variant_get_string (new_state, NULL)); |
| 130 |
|
| 131 |
g_simple_action_set_state (action, new_state); |
| 132 |
g_variant_unref (old_state); |
| 133 |
} |
| 134 |
|
| 135 |
static GActionEntry actions[] = { |
| 136 |
{ "website", activate_action, NULL, NULL, NULL }, |
| 137 |
{ "photos", activate_action, NULL, NULL, NULL }, |
| 138 |
{ "notes", activate_action, NULL, NULL, NULL }, |
| 139 |
{ "allow-sync", activate_toggle, NULL, "true", NULL }, |
| 140 |
{ "buy", activate_radio, "s", NULL, NULL }, |
| 141 |
}; |
| 142 |
|
| 143 |
static GMenuModel * |
| 144 |
get_model (void) |
| 145 |
{ |
| 146 |
GMenu *section; |
| 147 |
GMenu *mainMenu; |
| 148 |
GMenuItem *website; |
| 149 |
GMenuItem *photos; |
| 150 |
GMenuItem *notes; |
| 151 |
GMenuItem *allowSync; |
| 152 |
GMenuItem *item; |
| 153 |
GMenu *submenu; |
| 154 |
|
| 155 |
mainMenu = g_menu_new(); |
| 156 |
|
| 157 |
section = g_menu_new(); |
| 158 |
website = g_menu_item_new("MyCloud website", "cloudprovider.website"); |
| 159 |
g_menu_append_item(section, website); |
| 160 |
g_object_unref (website); |
| 161 |
photos = g_menu_item_new("MyCloud photos", "cloudprovider.photos"); |
| 162 |
g_menu_append_item(section, photos); |
| 163 |
g_object_unref (photos); |
| 164 |
notes = g_menu_item_new("MyCloud notes", "cloudprovider.notes"); |
| 165 |
g_menu_append_item(section, notes); |
| 166 |
g_object_unref (notes); |
| 167 |
g_menu_append_section(mainMenu, NULL, G_MENU_MODEL(section)); |
| 168 |
g_object_unref (section); |
| 169 |
|
| 170 |
section = g_menu_new(); |
| 171 |
allowSync = g_menu_item_new("Allow Synchronization", "cloudprovider.allow-sync"); |
| 172 |
g_menu_append_item(section, allowSync); |
| 173 |
g_object_unref (allowSync); |
| 174 |
|
| 175 |
submenu = g_menu_new(); |
| 176 |
item = g_menu_item_new("5GB", "5"); |
| 177 |
g_menu_append_item(submenu, item); |
| 178 |
g_object_unref (item); |
| 179 |
item = g_menu_item_new("10GB", "10"); |
| 180 |
g_menu_append_item(submenu, item); |
| 181 |
g_object_unref (item); |
| 182 |
item = g_menu_item_new("50GB", "50"); |
| 183 |
g_menu_append_item(submenu, item); |
| 184 |
g_object_unref (item); |
| 185 |
item = g_menu_item_new_submenu("Buy storage", G_MENU_MODEL(submenu)); |
| 186 |
g_menu_append_item(section, item); |
| 187 |
g_object_unref (item); |
| 188 |
g_object_unref (submenu); |
| 189 |
g_menu_append_section(mainMenu, NULL, G_MENU_MODEL(section)); |
| 190 |
g_object_unref (section); |
| 191 |
|
| 192 |
return G_MENU_MODEL(mainMenu); |
| 193 |
} |
| 194 |
|
| 195 |
static GActionGroup * |
| 196 |
get_action_group (void) |
| 197 |
{ |
| 198 |
GSimpleActionGroup *group; |
| 199 |
|
| 200 |
group = g_simple_action_group_new (); |
| 201 |
|
| 202 |
g_action_map_add_action_entries (G_ACTION_MAP (group), |
| 203 |
actions, |
| 204 |
G_N_ELEMENTS (actions), NULL); |
| 205 |
|
| 206 |
return G_ACTION_GROUP (group); |
| 207 |
} |
| 208 |
|
| 209 |
static gboolean |
| 210 |
change_random_cloud_provider_state (gpointer user_data) |
| 211 |
{ |
| 212 |
CloudProvidersTestServer *self = (CloudProvidersTestServer *)user_data; |
| 213 |
CloudProvidersAccountExporter *account; |
| 214 |
GRand *rand; |
| 215 |
gint new_status; |
| 216 |
gint account_id; |
| 217 |
|
| 218 |
rand = g_rand_new (); |
| 219 |
account_id = g_rand_int_range (rand, 0, COUNT_PLACEHOLDER_ACCOUNTS); |
| 220 |
new_status = g_rand_int_range (rand, |
| 221 |
CLOUD_PROVIDERS_ACCOUNT_STATUS_IDLE, |
| 222 |
CLOUD_PROVIDERS_ACCOUNT_STATUS_ERROR + 1); |
| 223 |
g_rand_free (rand); |
| 224 |
|
| 225 |
g_print ("Change status of %03d to %d\n", account_id, new_status); |
| 226 |
account = g_hash_table_lookup (self->accounts, GINT_TO_POINTER (account_id)); |
| 227 |
cloud_providers_account_exporter_set_status (account, new_status); |
| 228 |
|
| 229 |
return TRUE; |
| 230 |
} |
| 231 |
|
| 232 |
static const gchar * |
| 233 |
get_status_details (CloudProvidersAccountStatus status) |
| 234 |
{ |
| 235 |
gchar *description = ""; |
| 236 |
switch (status) { |
| 237 |
case CLOUD_PROVIDERS_ACCOUNT_STATUS_IDLE: |
| 238 |
description = "Details: Sync idle"; |
| 239 |
break; |
| 240 |
case CLOUD_PROVIDERS_ACCOUNT_STATUS_SYNCING: |
| 241 |
description = "Details: Syncing"; |
| 242 |
break; |
| 243 |
case CLOUD_PROVIDERS_ACCOUNT_STATUS_ERROR: |
| 244 |
description = "Details: Error"; |
| 245 |
break; |
| 246 |
case CLOUD_PROVIDERS_ACCOUNT_STATUS_INVALID: |
| 247 |
description = "Details: Sync status details not set"; |
| 248 |
break; |
| 249 |
} |
| 250 |
return description; |
| 251 |
} |
| 252 |
|
| 253 |
static gboolean |
| 254 |
add_accounts (CloudProvidersTestServer *self) |
| 255 |
{ |
| 256 |
guint n; |
| 257 |
|
| 258 |
// export multiple accounts as DBus objects to the bus |
| 259 |
for (n = 0; n < COUNT_PLACEHOLDER_ACCOUNTS; n++) |
| 260 |
{ |
| 261 |
g_autoptr (CloudProvidersAccountExporter) account = NULL; |
| 262 |
g_autofree gchar *account_object_name = NULL; |
| 263 |
g_autofree gchar *account_name = NULL; |
| 264 |
GActionGroup *action_group = get_action_group (); |
| 265 |
g_autoptr(GMenuModel) menu_model = get_model (); |
| 266 |
|
| 267 |
account_object_name = g_strdup_printf ("MyAccount%d", n); |
| 268 |
account_name = g_strdup_printf ("MyAccount %d", n); |
| 269 |
g_debug ("Adding account %s", account_name); |
| 270 |
account = cloud_providers_account_exporter_new (self->exporter, |
| 271 |
account_object_name); |
| 272 |
|
| 273 |
cloud_providers_account_exporter_set_name (account, account_name); |
| 274 |
cloud_providers_account_exporter_set_icon (account, self->icon); |
| 275 |
cloud_providers_account_exporter_set_path (account, self->path); |
| 276 |
cloud_providers_account_exporter_set_status (account, |
| 277 |
CLOUD_PROVIDERS_ACCOUNT_STATUS_INVALID); |
| 278 |
cloud_providers_account_exporter_set_status_details (account, |
| 279 |
get_status_details (CLOUD_PROVIDERS_ACCOUNT_STATUS_INVALID)); |
| 280 |
cloud_providers_account_exporter_set_menu_model (account, menu_model); |
| 281 |
cloud_providers_account_exporter_set_action_group (account, action_group); |
| 282 |
g_hash_table_insert (self->accounts, GINT_TO_POINTER (n), g_steal_pointer (&account)); |
| 283 |
g_object_unref (action_group); |
| 284 |
} |
| 285 |
|
| 286 |
return G_SOURCE_REMOVE; |
| 287 |
} |
| 288 |
|
| 289 |
static void |
| 290 |
on_bus_acquired (GDBusConnection *connection, |
| 291 |
const gchar *name, |
| 292 |
gpointer user_data) |
| 293 |
{ |
| 294 |
CloudProvidersTestServer *self = CLOUD_PROVIDERS_TEST_SERVER (user_data); |
| 295 |
|
| 296 |
g_debug ("Bus acquired: %s\n", name); |
| 297 |
|
| 298 |
g_debug ("Registering cloud provider server 'MyCloud'\n"); |
| 299 |
|
| 300 |
self->exporter = cloud_providers_provider_exporter_new(connection, |
| 301 |
TEST_CLOUD_PROVIDERS_BUS_NAME, |
| 302 |
TEST_CLOUD_PROVIDERS_OBJECT_PATH); |
| 303 |
cloud_providers_provider_exporter_set_name (self->exporter, "My cloud"); |
| 304 |
add_accounts (self); |
| 305 |
|
| 306 |
} |
| 307 |
|
| 308 |
static void |
| 309 |
on_name_acquired (GDBusConnection *connection, |
| 310 |
const gchar *name, |
| 311 |
gpointer user_data) |
| 312 |
{ |
| 313 |
CloudProvidersTestServer *self = (CloudProvidersTestServer *)user_data; |
| 314 |
self->timeout_handler = g_timeout_add (TIMEOUT, |
| 315 |
(GSourceFunc) change_random_cloud_provider_state, |
| 316 |
self); |
| 317 |
g_debug ("Server test name acquired"); |
| 318 |
change_random_cloud_provider_state (self); |
| 319 |
} |
| 320 |
|
| 321 |
static void |
| 322 |
on_name_lost (GDBusConnection *connection, |
| 323 |
const gchar *name, |
| 324 |
gpointer user_data) |
| 325 |
{ |
| 326 |
g_critical ("Name lost: %s\n", name); |
| 327 |
} |
| 328 |
|
| 329 |
int |
| 330 |
main (int argc, char *argv[]) |
| 331 |
{ |
| 332 |
GMainLoop *loop; |
| 333 |
CloudProvidersTestServer *test_cloud_provider; |
| 334 |
guint owner_id; |
| 335 |
|
| 336 |
test_cloud_provider = cloud_providers_test_server_new (); |
| 337 |
|
| 338 |
owner_id = g_bus_own_name (G_BUS_TYPE_SESSION, |
| 339 |
TEST_CLOUD_PROVIDERS_BUS_NAME, |
| 340 |
G_BUS_NAME_OWNER_FLAGS_NONE, |
| 341 |
on_bus_acquired, |
| 342 |
on_name_acquired, |
| 343 |
on_name_lost, |
| 344 |
test_cloud_provider, |
| 345 |
NULL); |
| 346 |
|
| 347 |
loop = g_main_loop_new (NULL, FALSE); |
| 348 |
g_main_loop_run (loop); |
| 349 |
g_main_loop_unref (loop); |
| 350 |
g_debug("going oooooout/n"); |
| 351 |
g_bus_unown_name (owner_id); |
| 352 |
g_object_unref (test_cloud_provider); |
| 353 |
|
| 354 |
return 0; |
| 355 |
} |
| 356 |
|