Add some more cases to the app-id unit tests
[glib.git] / gio / tests / gapplication-example-dbushooks.c
blobe93cf0848ff1d69867d291a4826d4d708baaf6f7
1 #include <gio/gio.h>
2 #include <stdlib.h>
3 #include <string.h>
5 static void
6 activate (GApplication *application)
8 g_print ("activated\n");
10 /* Note: when doing a longer-lasting action here that returns
11 * to the mainloop, you should use g_application_hold() and
12 * g_application_release() to keep the application alive until
13 * the action is completed.
17 typedef GApplication TestApplication;
18 typedef GApplicationClass TestApplicationClass;
20 static GType test_application_get_type (void);
21 G_DEFINE_TYPE (TestApplication, test_application, G_TYPE_APPLICATION)
23 static gboolean
24 test_application_dbus_register (GApplication *application,
25 GDBusConnection *connection,
26 const gchar *object_path,
27 GError **error)
29 /* We must chain up to the parent class */
30 if (!G_APPLICATION_CLASS (test_application_parent_class)->dbus_register (application,
31 connection,
32 object_path,
33 error))
34 return FALSE;
36 /* Now we can do our own stuff here. For example, we could export some D-Bus objects */
37 return TRUE;
40 static void
41 test_application_dbus_unregister (GApplication *application,
42 GDBusConnection *connection,
43 const gchar *object_path)
45 /* Do our own stuff here, e.g. unexport any D-Bus objects we exported in the dbus_register
46 * hook above. Be sure to check that we actually did export them, since the hook
47 * above might have returned early due to the parent class' hook returning FALSE!
50 /* Lastly, we must chain up to the parent class */
51 G_APPLICATION_CLASS (test_application_parent_class)->dbus_unregister (application,
52 connection,
53 object_path);
56 static void
57 test_application_init (TestApplication *app)
61 static void
62 test_application_class_init (TestApplicationClass *class)
64 GApplicationClass *g_application_class = G_APPLICATION_CLASS (class);
66 g_application_class->dbus_register = test_application_dbus_register;
67 g_application_class->dbus_unregister = test_application_dbus_unregister;
70 static GApplication *
71 test_application_new (const gchar *application_id,
72 GApplicationFlags flags)
74 g_return_val_if_fail (g_application_id_is_valid (application_id), NULL);
76 return g_object_new (test_application_get_type (),
77 "application-id", application_id,
78 "flags", flags,
79 NULL);
82 int
83 main (int argc, char **argv)
85 GApplication *app;
86 int status;
88 app = test_application_new ("org.gtk.TestApplication", 0);
89 g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
90 g_application_set_inactivity_timeout (app, 10000);
92 status = g_application_run (app, argc, argv);
94 g_object_unref (app);
96 return status;