Back to index

indicator-appmenu  12.10.0
mock-json-app.c
Go to the documentation of this file.
00001 /*
00002 A little utility to make a mock application using the JSON
00003 menu description.
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 <gtk/gtk.h>
00024 #include <gdk/gdkx.h>
00025 #include <gio/gio.h>
00026 #include <libdbusmenu-glib/server.h>
00027 #include <libdbusmenu-jsonloader/json-loader.h>
00028 
00029 #include "../src/dbus-shared.h"
00030 
00031 #define MENU_PATH "/mock/json/app/menu"
00032 
00033 GtkWidget * window = NULL;
00034 DbusmenuServer * server = NULL;
00035 GDBusProxy * registrar = NULL;
00036 
00037 static void
00038 dbus_owner_change (GObject *gobject, GParamSpec *pspec, gpointer user_data)
00039 {
00040        GDBusProxy * proxy = G_DBUS_PROXY(gobject);
00041 
00042        gchar * owner = g_dbus_proxy_get_name_owner(proxy);
00043 
00044        if (owner == NULL || owner[0] == '\0') {
00045               /* We only care about folks coming on the bus.  Exit quickly otherwise. */
00046               g_free(owner);
00047               return;
00048        }
00049 
00050        if (g_strcmp0(owner, DBUS_NAME)) {
00051               /* We only care about this address, reject all others. */
00052               g_free(owner);
00053               return;
00054        }
00055 
00056        GError * error = NULL;
00057        g_dbus_proxy_call_sync(registrar, "RegisterWindow",
00058                               g_variant_new("(uo)",
00059                                             GDK_WINDOW_XID (gtk_widget_get_window (window)),
00060                                             MENU_PATH),
00061                               G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL);
00062        if (error != NULL) {
00063               g_warning("Unable to register: %s", error->message);
00064               g_error_free(error);
00065        }
00066 
00067        g_free(owner);
00068        return;
00069 }
00070 
00071 static gboolean
00072 idle_func (gpointer user_data)
00073 {
00074        DbusmenuMenuitem * root = dbusmenu_json_build_from_file((const gchar *)user_data);
00075        g_return_val_if_fail(root != NULL, FALSE);
00076 
00077        DbusmenuServer * server = dbusmenu_server_new(MENU_PATH);
00078        dbusmenu_server_set_root(server, root);
00079 
00080        registrar = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SESSION,
00081                                                  G_DBUS_PROXY_FLAGS_NONE,
00082                                                  NULL, DBUS_NAME,
00083                                                  REG_OBJECT, REG_IFACE,
00084                                                  NULL, NULL);
00085        g_return_val_if_fail(registrar != NULL, FALSE);
00086 
00087        GError * error = NULL;
00088        g_dbus_proxy_call_sync(registrar, "RegisterWindow",
00089                               g_variant_new("(uo)",
00090                                             GDK_WINDOW_XID (gtk_widget_get_window (window)),
00091                                             MENU_PATH),
00092                               G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
00093        if (error != NULL) {
00094               g_warning("Unable to register: %s", error->message);
00095               g_error_free(error);
00096        }
00097 
00098        g_signal_connect(registrar, "notify::g-name-owner",
00099                         G_CALLBACK(dbus_owner_change), NULL);
00100 
00101        return FALSE;
00102 }
00103 
00104 static void
00105 destroy_cb (void)
00106 {
00107        gtk_main_quit();
00108        return;
00109 }
00110 
00111 int
00112 main (int argv, char ** argc)
00113 {
00114        if (argv != 2) {
00115               g_print("'%s <JSON file>' is how you should use this program.\n", argc[0]);
00116               return 1;
00117        }
00118 
00119        gtk_init(&argv, &argc);
00120 
00121        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
00122        g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(destroy_cb), NULL);
00123        gtk_widget_show(window);
00124 
00125        g_idle_add(idle_func, argc[1]);
00126 
00127        gtk_main();
00128 
00129        return 0;
00130 }