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