Back to index

libappindicator  12.10.0
test-libappindicator-fallback-watcher.c
Go to the documentation of this file.
00001 /*
00002 This puts the NotificationWatcher on the bus, kinda.  Enough to
00003 trick the Item into unfalling back.
00004 
00005 Copyright 2010 Canonical Ltd.
00006 
00007 Authors:
00008     Ted Gould <ted@canonical.com>
00009 
00010 This program is free software: you can redistribute it and/or modify it
00011 under the terms of the GNU General Public License version 3, as published
00012 by the Free Software Foundation.
00013 
00014 This program is distributed in the hope that it will be useful, but
00015 WITHOUT ANY WARRANTY; without even the implied warranties of
00016 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
00017 PURPOSE.  See the GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License along
00020 with this program.  If not, see <http://www.gnu.org/licenses/>.
00021 */
00022 
00023 #include <glib.h>
00024 #include <dbus/dbus-glib.h>
00025 #include <dbus/dbus-glib-bindings.h>
00026 #include <dbus/dbus-glib-lowlevel.h>
00027 
00028 #include "../src/dbus-shared.h"
00029 
00030 gboolean kill_func (gpointer userdata);
00031 
00032 static GMainLoop * mainloop = NULL;
00033 
00034 static DBusHandlerResult
00035 dbus_filter (DBusConnection * connection, DBusMessage * message, void * user_data)
00036 {
00037        if (dbus_message_is_method_call(message, NOTIFICATION_WATCHER_DBUS_ADDR, "RegisterStatusNotifierItem")) {
00038               DBusMessage * reply = dbus_message_new_method_return(message);
00039               dbus_connection_send(connection, reply, NULL);
00040               dbus_message_unref(reply);
00041 
00042               /* Let the messages get out, but we're done at this point */
00043               g_timeout_add(50, kill_func, NULL);
00044 
00045               return DBUS_HANDLER_RESULT_HANDLED;
00046        }
00047 
00048        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
00049 }
00050 
00051 gboolean
00052 kill_func (gpointer userdata)
00053 {
00054        g_main_loop_quit(mainloop);
00055        return FALSE;
00056 }
00057 
00058 int
00059 main (int argv, char ** argc)
00060 {
00061        g_type_init();
00062 
00063        g_debug("Waiting to init.");
00064 
00065 
00066        GError * error = NULL;
00067        DBusGConnection * session_bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
00068        if (error != NULL) {
00069               g_error("Unable to get session bus: %s", error->message);
00070               return 1;
00071        }
00072 
00073     DBusGProxy * bus_proxy = dbus_g_proxy_new_for_name(session_bus, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS);
00074 
00075        gboolean has_owner = FALSE;
00076        gint owner_count = 0;
00077        while (!has_owner && owner_count < 10000) {
00078               org_freedesktop_DBus_name_has_owner(bus_proxy, "org.test", &has_owner, NULL);
00079               owner_count++;
00080        }
00081 
00082        if (owner_count == 10000) {
00083               g_error("Unable to get name owner after 10000 tries");
00084               return 1;
00085        }
00086 
00087        g_usleep(500000);
00088 
00089        g_debug("Initing");
00090 
00091        guint nameret = 0;
00092 
00093        if (!org_freedesktop_DBus_request_name(bus_proxy, NOTIFICATION_WATCHER_DBUS_ADDR, 0, &nameret, &error)) {
00094               g_error("Unable to call to request name");
00095               return 1;
00096        }   
00097 
00098        if (nameret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
00099               g_error("Unable to get name");
00100               return 1;
00101        }
00102 
00103        dbus_connection_add_filter(dbus_g_connection_get_connection(session_bus), dbus_filter, NULL, NULL);
00104 
00105        /* This is the final kill function.  It really shouldn't happen
00106           unless we get an error. */
00107        g_timeout_add(2000, kill_func, NULL);
00108 
00109        g_debug("Entering Mainloop");
00110 
00111        mainloop = g_main_loop_new(NULL, FALSE);
00112        g_main_loop_run(mainloop);
00113 
00114        g_debug("Exiting");
00115 
00116        return 0;
00117 }