1 | 1 |
#include <stdlib.h>
|
2 | 2 |
#include <gio/gio.h>
|
3 | 3 |
#include <cloudprovider.h>
|
|
4 |
#include <cloudprovidermanager.h>
|
4 | 5 |
|
5 | 6 |
#define TIMEOUT 2000
|
|
7 |
#define COUNT_PLACEHOLDER_ACCOUNTS 3
|
6 | 8 |
|
7 | 9 |
typedef struct _TestCloudProviderClass TestCloudProviderClass;
|
8 | 10 |
typedef struct _TestCloudProvider TestCloudProvider;
|
|
22 | 24 |
gchar *path;
|
23 | 25 |
guint timeout_handler;
|
24 | 26 |
GDBusConnection *connection;
|
|
27 |
GDBusObjectManagerServer *manager;
|
25 | 28 |
};
|
26 | 29 |
|
27 | 30 |
|
28 | 31 |
static GType test_cloud_provider_get_type (void);
|
29 | 32 |
G_DEFINE_TYPE (TestCloudProvider, test_cloud_provider, G_TYPE_OBJECT);
|
|
33 |
|
|
34 |
TestCloudProvider*
|
|
35 |
test_cloud_provider_new (const gchar *name)
|
|
36 |
{
|
|
37 |
TestCloudProvider *self;
|
|
38 |
|
|
39 |
self = g_object_new (test_cloud_provider_get_type(), NULL);
|
|
40 |
self->name = g_strdup(name);
|
|
41 |
|
|
42 |
return self;
|
|
43 |
}
|
30 | 44 |
|
31 | 45 |
static void
|
32 | 46 |
test_cloud_provider_finalize (GObject *object)
|
|
52 | 66 |
self->name = "MyCloud";
|
53 | 67 |
self->path = g_strdup (current_dir);
|
54 | 68 |
self->status = CLOUD_PROVIDER_STATUS_INVALID;
|
55 | |
uri = g_build_filename (current_dir, "apple-red.png", NULL);
|
|
69 |
uri = g_build_filename (current_dir, "icon.png", NULL);
|
56 | 70 |
icon_file = g_file_new_for_uri (uri);
|
57 | 71 |
self->icon = g_file_icon_new (icon_file);
|
58 | 72 |
|
|
218 | 232 |
}
|
219 | 233 |
}
|
220 | 234 |
|
221 | |
static void
|
222 | |
handle_method_call (GDBusConnection *connection,
|
223 | |
const gchar *sender,
|
224 | |
const gchar *object_path,
|
225 | |
const gchar *interface_name,
|
226 | |
const gchar *method_name,
|
227 | |
GVariant *parameters,
|
228 | |
GDBusMethodInvocation *invocation,
|
229 | |
gpointer user_data)
|
230 | |
{
|
231 | |
TestCloudProvider *cloud_provider = user_data;
|
232 | |
|
233 | |
g_debug ("Handling dbus call in server\n");
|
234 | |
if (g_strcmp0 (method_name, "GetName") == 0)
|
235 | |
{
|
236 | |
g_dbus_method_invocation_return_value (invocation,
|
237 | |
g_variant_new ("(s)", cloud_provider->name));
|
238 | |
}
|
239 | |
else if (g_strcmp0 (method_name, "GetStatus") == 0)
|
240 | |
{
|
241 | |
g_dbus_method_invocation_return_value (invocation,
|
242 | |
g_variant_new ("(i)", cloud_provider->status));
|
243 | |
}
|
244 | |
else if (g_strcmp0 (method_name, "GetIcon") == 0)
|
245 | |
{
|
246 | |
g_dbus_method_invocation_return_value (invocation,
|
247 | |
g_variant_new ("(v)", g_icon_serialize (cloud_provider->icon)));
|
248 | |
}
|
249 | |
else if (g_strcmp0 (method_name, "GetPath") == 0)
|
250 | |
{
|
251 | |
g_dbus_method_invocation_return_value (invocation,
|
252 | |
g_variant_new ("(s)", cloud_provider->path));
|
253 | |
}
|
254 | |
}
|
255 | |
|
256 | |
static const GDBusInterfaceVTable interface_vtable =
|
257 | |
{
|
258 | |
handle_method_call,
|
259 | |
};
|
260 | |
|
261 | 235 |
static gboolean
|
262 | 236 |
test_cloud_provider_notify_change (gpointer user_data)
|
263 | 237 |
{
|
|
265 | 239 |
GRand *rand;
|
266 | 240 |
gint new_status;
|
267 | 241 |
|
268 | |
g_print("Emit changed signal for cloud provider\n");
|
269 | |
|
270 | 242 |
rand = g_rand_new ();
|
|
243 |
|
|
244 |
g_print("Emit changed signal for cloud providers\n");
|
|
245 |
gchar *account_object_name;
|
|
246 |
account_object_name = g_strdup_printf ("/org/freedesktop/CloudProviderServerExample/%03d",
|
|
247 |
g_rand_int_range(rand, 0, COUNT_PLACEHOLDER_ACCOUNTS));
|
|
248 |
|
271 | 249 |
new_status = g_rand_int_range (rand,
|
272 | 250 |
CLOUD_PROVIDER_STATUS_IDLE,
|
273 | 251 |
CLOUD_PROVIDER_STATUS_ERROR + 1);
|
|
276 | 254 |
|
277 | 255 |
g_dbus_connection_emit_signal (cloud_provider->connection,
|
278 | 256 |
NULL,
|
279 | |
"/org/freedesktop/CloudProviderServerExample/account1",
|
|
257 |
account_object_name,
|
280 | 258 |
"org.freedesktop.CloudProvider1",
|
281 | 259 |
"CloudProviderChanged",
|
282 | 260 |
NULL,
|
283 | 261 |
NULL /*error*/);
|
284 | |
|
285 | 262 |
return TRUE;
|
|
263 |
}
|
|
264 |
|
|
265 |
|
|
266 |
static void
|
|
267 |
on_get_name (CloudProvider1 *cloud_provider,
|
|
268 |
GDBusMethodInvocation *invocation,
|
|
269 |
gpointer user_data)
|
|
270 |
{
|
|
271 |
gchar *name = user_data;
|
|
272 |
g_dbus_method_invocation_return_value (invocation,
|
|
273 |
g_variant_new ("(s)", name));
|
|
274 |
}
|
|
275 |
|
|
276 |
static void
|
|
277 |
on_get_icon (CloudProvider1 *cloud_provider,
|
|
278 |
GDBusMethodInvocation *invocation,
|
|
279 |
gpointer user_data)
|
|
280 |
{
|
|
281 |
TestCloudProvider *self = user_data;
|
|
282 |
g_dbus_method_invocation_return_value (invocation,
|
|
283 |
g_variant_new ("(v)", g_icon_serialize(self->icon)));
|
|
284 |
}
|
|
285 |
|
|
286 |
static void
|
|
287 |
on_get_path (CloudProvider1 *cloud_provider,
|
|
288 |
GDBusMethodInvocation *invocation,
|
|
289 |
gpointer user_data)
|
|
290 |
{
|
|
291 |
TestCloudProvider *self = user_data;
|
|
292 |
g_dbus_method_invocation_return_value (invocation,
|
|
293 |
g_variant_new ("(s)", self->path));
|
|
294 |
}
|
|
295 |
|
|
296 |
static void
|
|
297 |
on_get_status (CloudProvider1 *cloud_provider,
|
|
298 |
GDBusMethodInvocation *invocation,
|
|
299 |
gpointer user_data)
|
|
300 |
{
|
|
301 |
TestCloudProvider *self = user_data;
|
|
302 |
g_dbus_method_invocation_return_value (invocation,
|
|
303 |
g_variant_new ("(i)", self->status));
|
286 | 304 |
}
|
287 | 305 |
|
288 | 306 |
static void
|
|
290 | 308 |
const gchar *name,
|
291 | 309 |
gpointer user_data)
|
292 | 310 |
{
|
293 | |
TestCloudProvider *cloud_provider = user_data;
|
294 | |
guint registration_id;
|
295 | |
|
296 | |
cloud_provider->connection = connection;
|
|
311 |
TestCloudProvider *self = user_data;
|
|
312 |
guint n;
|
|
313 |
ObjectSkeleton *object;
|
|
314 |
self->connection = connection;
|
297 | 315 |
|
298 | 316 |
g_debug ("Registering cloud provider server 'MyCloud'\n");
|
299 | 317 |
|
300 | |
registration_id = g_dbus_connection_register_object (connection,
|
301 | |
"/org/freedesktop/CloudProviderServerExample/account1",
|
302 | |
cloud_provider1_interface_info(),
|
303 | |
&interface_vtable,
|
304 | |
cloud_provider,
|
305 | |
NULL, /* user_data_free_func */
|
306 | |
NULL); /* GError** */
|
307 | |
|
308 | |
g_assert (registration_id > 0);
|
309 | |
/* Export a menu for our own application */
|
310 | |
export_menu (connection, "/org/freedesktop/CloudProviderServerExample/account1");
|
311 | |
|
312 | |
registration_id = g_dbus_connection_register_object (connection,
|
313 | |
"/org/freedesktop/CloudProviderServerExample/account2",
|
314 | |
cloud_provider1_interface_info(),
|
315 | |
&interface_vtable,
|
316 | |
cloud_provider,
|
317 | |
NULL, /* user_data_free_func */
|
318 | |
NULL); /* GError** */
|
319 | |
|
320 | |
g_assert (registration_id > 0);
|
321 | |
/* Export a menu for our own application */
|
322 | |
export_menu (connection, "/org/freedesktop/CloudProviderServerExample/account2");
|
|
318 |
self->manager = g_dbus_object_manager_server_new ("/org/freedesktop/CloudProviderServerExample");
|
|
319 |
for (n = 0; n < COUNT_PLACEHOLDER_ACCOUNTS; n++)
|
|
320 |
{
|
|
321 |
|
|
322 |
gchar *account_object_name;
|
|
323 |
gchar *account_name;
|
|
324 |
|
|
325 |
account_object_name = g_strdup_printf ("/org/freedesktop/CloudProviderServerExample/%03d", n);
|
|
326 |
account_name = g_strdup_printf ("MyCloud %d", n);
|
|
327 |
object = object_skeleton_new(account_object_name);
|
|
328 |
|
|
329 |
CloudProvider1 *cloud_provider = cloud_provider1_skeleton_new();
|
|
330 |
g_signal_connect(cloud_provider, "handle_get_name", G_CALLBACK (on_get_name), account_name);
|
|
331 |
g_signal_connect(cloud_provider, "handle_get_icon", G_CALLBACK (on_get_icon), self);
|
|
332 |
g_signal_connect(cloud_provider, "handle_get_path", G_CALLBACK (on_get_path), self);
|
|
333 |
g_signal_connect(cloud_provider, "handle_get_status", G_CALLBACK (on_get_status), self);
|
|
334 |
object_skeleton_set_cloud_provider1(object, cloud_provider);
|
|
335 |
g_dbus_object_manager_server_export (self->manager, G_DBUS_OBJECT_SKELETON(object));
|
|
336 |
|
|
337 |
export_menu (connection, account_object_name);
|
|
338 |
// FIXME: send initial changed signal to notify already running e.g. nautilus
|
|
339 |
g_dbus_connection_emit_signal (connection,
|
|
340 |
NULL,
|
|
341 |
account_object_name,
|
|
342 |
"org.freedesktop.CloudProvider1",
|
|
343 |
"CloudProviderChanged",
|
|
344 |
NULL,
|
|
345 |
NULL /*error*/);
|
|
346 |
|
|
347 |
g_free(account_object_name);
|
|
348 |
}
|
|
349 |
g_dbus_object_manager_server_set_connection (self->manager, connection);
|
|
350 |
|
|
351 |
return;
|
|
352 |
|
|
353 |
|
323 | 354 |
}
|
324 | 355 |
|
325 | 356 |
static void
|
|
346 | 377 |
main (int argc, char *argv[])
|
347 | 378 |
{
|
348 | 379 |
GMainLoop *loop;
|
349 | |
TestCloudProvider *cloud_provider;
|
|
380 |
TestCloudProvider *test_cloud_provider;
|
350 | 381 |
guint owner_id;
|
351 | 382 |
|
352 | |
cloud_provider = g_object_new (test_cloud_provider_get_type (), NULL);
|
|
383 |
|
|
384 |
test_cloud_provider = g_object_new (test_cloud_provider_get_type (), NULL);
|
|
385 |
|
353 | 386 |
|
354 | 387 |
owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
|
355 | 388 |
"org.freedesktop.CloudProviderServerExample",
|
|
357 | 390 |
on_bus_acquired,
|
358 | 391 |
on_name_acquired,
|
359 | 392 |
on_name_lost,
|
360 | |
cloud_provider,
|
|
393 |
test_cloud_provider,
|
361 | 394 |
NULL);
|
362 | 395 |
|
363 | 396 |
loop = g_main_loop_new (NULL, FALSE);
|
|
365 | 398 |
|
366 | 399 |
g_bus_unown_name (owner_id);
|
367 | 400 |
|
368 | |
g_object_unref (cloud_provider);
|
|
401 |
g_object_unref (test_cloud_provider);
|
369 | 402 |
|
370 | 403 |
return 0;
|
371 | 404 |
}
|