Back to index

libappindicator  12.10.0
test-libappindicator-fallback-item.c
Go to the documentation of this file.
00001 #include <glib.h>
00002 #include <glib-object.h>
00003 #include <dbus/dbus-glib.h>
00004 #include <dbus/dbus-glib-bindings.h>
00005 #include <app-indicator.h>
00006 
00007 #define TEST_LIBAPPINDICATOR_FALLBACK_ITEM_TYPE            (test_libappindicator_fallback_item_get_type ())
00008 #define TEST_LIBAPPINDICATOR_FALLBACK_ITEM(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), TEST_LIBAPPINDICATOR_FALLBACK_ITEM_TYPE, TestLibappindicatorFallbackItem))
00009 #define TEST_LIBAPPINDICATOR_FALLBACK_ITEM_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), TEST_LIBAPPINDICATOR_FALLBACK_ITEM_TYPE, TestLibappindicatorFallbackItemClass))
00010 #define IS_TEST_LIBAPPINDICATOR_FALLBACK_ITEM(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TEST_LIBAPPINDICATOR_FALLBACK_ITEM_TYPE))
00011 #define IS_TEST_LIBAPPINDICATOR_FALLBACK_ITEM_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TEST_LIBAPPINDICATOR_FALLBACK_ITEM_TYPE))
00012 #define TEST_LIBAPPINDICATOR_FALLBACK_ITEM_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), TEST_LIBAPPINDICATOR_FALLBACK_ITEM_TYPE, TestLibappindicatorFallbackItemClass))
00013 
00014 typedef struct _TestLibappindicatorFallbackItem      TestLibappindicatorFallbackItem;
00015 typedef struct _TestLibappindicatorFallbackItemClass TestLibappindicatorFallbackItemClass;
00016 
00017 struct _TestLibappindicatorFallbackItemClass {
00018        AppIndicatorClass parent_class;
00019 
00020 };
00021 
00022 struct _TestLibappindicatorFallbackItem {
00023        AppIndicator parent;
00024 
00025 };
00026 
00027 GType test_libappindicator_fallback_item_get_type (void);
00028 
00029 #define TEST_LIBAPPINDICATOR_FALLBACK_ITEM_GET_PRIVATE(o) \
00030 (G_TYPE_INSTANCE_GET_PRIVATE ((o), TEST_LIBAPPINDICATOR_FALLBACK_ITEM_TYPE, TestLibappindicatorFallbackItemPrivate))
00031 
00032 static void test_libappindicator_fallback_item_class_init (TestLibappindicatorFallbackItemClass *klass);
00033 static void test_libappindicator_fallback_item_init       (TestLibappindicatorFallbackItem *self);
00034 static GtkStatusIcon * fallback (AppIndicator * indicator);
00035 static void unfallback (AppIndicator * indicator, GtkStatusIcon * status_icon);
00036 
00037 G_DEFINE_TYPE (TestLibappindicatorFallbackItem, test_libappindicator_fallback_item, APP_INDICATOR_TYPE);
00038 
00039 static void
00040 test_libappindicator_fallback_item_class_init (TestLibappindicatorFallbackItemClass *klass)
00041 {
00042        AppIndicatorClass * aiclass = APP_INDICATOR_CLASS(klass);
00043 
00044        aiclass->fallback = fallback;
00045        aiclass->unfallback = unfallback;
00046 }
00047 
00048 static void
00049 test_libappindicator_fallback_item_init (TestLibappindicatorFallbackItem *self)
00050 {
00051 }
00052 
00053 GMainLoop * mainloop = NULL;
00054 gboolean passed = FALSE;
00055 
00056 enum {
00057        STATE_INIT,
00058        STATE_FALLBACK,
00059        STATE_UNFALLBACK,
00060        STATE_REFALLBACK,
00061        STATE_REUNFALLBACK
00062 };
00063 
00064 gint state = STATE_INIT;
00065 
00066 static GtkStatusIcon *
00067 fallback (AppIndicator * indicator)
00068 {
00069        g_debug("Fallback");
00070        if (state == STATE_INIT) {
00071               state = STATE_FALLBACK;
00072        } else if (state == STATE_UNFALLBACK) {
00073               state = STATE_REFALLBACK;
00074        } else {
00075               g_debug("Error, fallback in state: %d", state);
00076               passed = FALSE;
00077        }
00078        return (GtkStatusIcon *)5;
00079 }
00080 
00081 static void
00082 unfallback (AppIndicator * indicator, GtkStatusIcon * status_icon)
00083 {
00084        g_debug("Unfallback");
00085        if (state == STATE_FALLBACK) {
00086               state = STATE_UNFALLBACK;
00087        } else if (state == STATE_REFALLBACK) {
00088               state = STATE_REUNFALLBACK;
00089               passed = TRUE;
00090               g_main_loop_quit(mainloop);
00091        } else {
00092               g_debug("Error, unfallback in state: %d", state);
00093               passed = FALSE;
00094        }
00095        return;
00096 }
00097 
00098 gboolean
00099 kill_func (gpointer data)
00100 {
00101        g_debug("Kill Function");
00102        g_main_loop_quit(mainloop);
00103        return FALSE;
00104 }
00105 
00106 int
00107 main (int argc, char ** argv)
00108 {
00109        gtk_init(&argc, &argv);
00110 
00111        GError * error = NULL;
00112        DBusGConnection * session_bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
00113        if (error != NULL) {
00114               g_error("Unable to get session bus: %s", error->message);
00115               return 1;
00116        }
00117 
00118     DBusGProxy * bus_proxy = dbus_g_proxy_new_for_name(session_bus, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS);
00119 
00120        guint nameret = 0;
00121 
00122        if (!org_freedesktop_DBus_request_name(bus_proxy, "org.test", 0, &nameret, NULL)) {
00123               g_error("Unable to call to request name");
00124               return 1;
00125        }   
00126 
00127        if (nameret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
00128               g_error("Unable to get name");
00129               return 1;
00130        }
00131 
00132        TestLibappindicatorFallbackItem * item = g_object_new(TEST_LIBAPPINDICATOR_FALLBACK_ITEM_TYPE,
00133               "id", "test-id",
00134               "category", "Other",
00135               "icon-name", "bob",
00136               NULL);
00137        
00138        GtkWidget * menu = gtk_menu_new();
00139        app_indicator_set_menu(APP_INDICATOR(item), GTK_MENU(menu));
00140 
00141        g_timeout_add_seconds(2, kill_func, NULL);
00142 
00143        mainloop = g_main_loop_new(NULL, FALSE);
00144        g_main_loop_run(mainloop);
00145 
00146        g_object_unref(G_OBJECT(item));
00147 
00148        if (passed) {
00149               return 0;
00150        } else {
00151               return 1;
00152        }
00153 }