~indicator-applet-developers/indicator-session/trunk.0.1

« back to all changes in this revision

Viewing changes to src/status-service.c

  • Committer: Ted Gould
  • Date: 2009-07-27 04:47:27 UTC
  • mfrom: (9.1.94 fusa.libindicator)
  • Revision ID: ted@canonical.com-20090727044727-m04ul5d8sxqt2pd4
Merging in the branch to port everything to libindicator and fleshing thigns out.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
#include <glib/gi18n.h>
 
3
 
 
4
#include <dbus/dbus-glib.h>
 
5
#include <dbus/dbus-glib-bindings.h>
 
6
 
 
7
#include <libdbusmenu-glib/server.h>
 
8
#include <libdbusmenu-glib/menuitem.h>
 
9
 
 
10
#include "dbus-shared-names.h"
 
11
 
 
12
#include "status-service-dbus.h"
 
13
 
 
14
#include "status-provider.h"
 
15
#include "status-provider-pidgin.h"
 
16
 
 
17
typedef StatusProvider * (*newfunc) (void);
 
18
#define STATUS_PROVIDER_CNT   1
 
19
static newfunc status_provider_newfuncs[STATUS_PROVIDER_CNT] = {
 
20
        status_provider_pidgin_new
 
21
};
 
22
static StatusProvider * status_providers[STATUS_PROVIDER_CNT] = { 0 };
 
23
 
 
24
static const gchar * status_strings [STATUS_PROVIDER_STATUS_LAST] = {
 
25
  /* STATUS_PROVIDER_STATUS_ONLINE,    */ N_("Available"),
 
26
  /* STATUS_PROVIDER_STATUS_AWAY,      */ N_("Away"),
 
27
  /* STATUS_PROVIDER_STATUS_DND        */ N_("Busy"),
 
28
  /* STATUS_PROVIDER_STATUS_INVISIBLE  */ N_("Invisible"),
 
29
  /* STATUS_PROVIDER_STATUS_OFFLINE,   */ N_("Offline")
 
30
};
 
31
 
 
32
static const gchar * status_icons[STATUS_PROVIDER_STATUS_LAST] = {
 
33
  /* STATUS_PROVIDER_STATUS_ONLINE, */     "user-online",
 
34
  /* STATUS_PROVIDER_STATUS_AWAY, */       "user-away",
 
35
  /* STATUS_PROVIDER_STATUS_DND, */        "user-busy",
 
36
  /* STATUS_PROVIDER_STATUS_INVISIBLE, */  "user-invisible",
 
37
  /* STATUS_PROVIDER_STATUS_OFFLINE */     "user-offline"
 
38
};
 
39
 
 
40
 
 
41
static DbusmenuMenuitem * root_menuitem = NULL;
 
42
static GMainLoop * mainloop = NULL;
 
43
 
 
44
/* A fun little function to actually lock the screen.  If,
 
45
   that's what you want, let's do it! */
 
46
static void
 
47
lock_screen (DbusmenuMenuitem * mi, gpointer data)
 
48
{
 
49
        g_debug("Lock Screen");
 
50
 
 
51
        DBusGConnection * session_bus = dbus_g_bus_get(DBUS_BUS_SESSION, NULL);
 
52
        g_return_if_fail(session_bus != NULL);
 
53
 
 
54
        DBusGProxy * proxy = dbus_g_proxy_new_for_name_owner(session_bus,
 
55
                                                             "org.gnome.ScreenSaver",
 
56
                                                             "/",
 
57
                                                             "org.gnome.ScreenSaver",
 
58
                                                             NULL);
 
59
        g_return_if_fail(proxy != NULL);
 
60
 
 
61
        dbus_g_proxy_call_no_reply(proxy,
 
62
                                   "Lock",
 
63
                                   G_TYPE_INVALID,
 
64
                                   G_TYPE_INVALID);
 
65
 
 
66
        g_object_unref(proxy);
 
67
 
 
68
        return;
 
69
}
 
70
 
 
71
static void
 
72
status_menu_click (DbusmenuMenuitem * mi, gpointer data)
 
73
{
 
74
        StatusProviderStatus status = (StatusProviderStatus)GPOINTER_TO_INT(data);
 
75
        g_debug("Setting status: %d", status);
 
76
        int i;
 
77
        for (i = 0; i < STATUS_PROVIDER_CNT; i++) {
 
78
                status_provider_set_status(status_providers[i], status);
 
79
        }
 
80
 
 
81
        return;
 
82
}
 
83
 
 
84
static gboolean
 
85
build_providers (gpointer data)
 
86
{
 
87
        int i;
 
88
        for (i = 0; i < STATUS_PROVIDER_CNT; i++) {
 
89
                status_providers[i] = status_provider_newfuncs[i]();
 
90
        }
 
91
 
 
92
        return FALSE;
 
93
}
 
94
 
 
95
static gboolean
 
96
build_menu (gpointer data)
 
97
{
 
98
        DbusmenuMenuitem * root = DBUSMENU_MENUITEM(data);
 
99
        g_return_val_if_fail(root != NULL, FALSE);
 
100
 
 
101
        StatusProviderStatus i;
 
102
        for (i = STATUS_PROVIDER_STATUS_ONLINE; i < STATUS_PROVIDER_STATUS_LAST; i++) {
 
103
                DbusmenuMenuitem * mi = dbusmenu_menuitem_new();
 
104
 
 
105
                dbusmenu_menuitem_property_set(mi, "label", _(status_strings[i]));
 
106
                dbusmenu_menuitem_property_set(mi, "icon", status_icons[i]);
 
107
                g_signal_connect(G_OBJECT(mi), DBUSMENU_MENUITEM_SIGNAL_ITEM_ACTIVATED, G_CALLBACK(status_menu_click), GINT_TO_POINTER(i));
 
108
 
 
109
                dbusmenu_menuitem_child_append(root, mi);
 
110
 
 
111
                g_debug("Built %s", status_strings[i]);
 
112
        }
 
113
 
 
114
        DbusmenuMenuitem * mi = dbusmenu_menuitem_new();
 
115
        dbusmenu_menuitem_property_set(mi, "label", _("Lock Screen"));
 
116
        g_signal_connect(G_OBJECT(mi), DBUSMENU_MENUITEM_SIGNAL_ITEM_ACTIVATED, G_CALLBACK(lock_screen), GINT_TO_POINTER(i));
 
117
        dbusmenu_menuitem_child_append(root, mi);
 
118
 
 
119
        return FALSE;
 
120
}
 
121
 
 
122
int
 
123
main (int argc, char ** argv)
 
124
{
 
125
    g_type_init();
 
126
 
 
127
    DBusGConnection * connection = dbus_g_bus_get(DBUS_BUS_SESSION, NULL);
 
128
    DBusGProxy * bus_proxy = dbus_g_proxy_new_for_name(connection, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS);
 
129
    GError * error = NULL;
 
130
    guint nameret = 0;
 
131
 
 
132
    if (!org_freedesktop_DBus_request_name(bus_proxy, INDICATOR_STATUS_DBUS_NAME, 0, &nameret, &error)) {
 
133
        g_error("Unable to call to request name");
 
134
        return 1;
 
135
    }   
 
136
 
 
137
    if (nameret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
 
138
        g_error("Unable to get name");
 
139
        return 1;
 
140
    }   
 
141
 
 
142
        g_idle_add(build_providers, NULL);
 
143
 
 
144
    root_menuitem = dbusmenu_menuitem_new();
 
145
    DbusmenuServer * server = dbusmenu_server_new(INDICATOR_STATUS_DBUS_OBJECT);
 
146
    dbusmenu_server_set_root(server, root_menuitem);
 
147
 
 
148
        g_idle_add(build_menu, root_menuitem);
 
149
 
 
150
    mainloop = g_main_loop_new(NULL, FALSE);
 
151
    g_main_loop_run(mainloop);
 
152
 
 
153
    return 0;
 
154
}
 
155