~ubuntu-branches/ubuntu/maverick/indicator-applet/maverick

« back to all changes in this revision

Viewing changes to src-session/applet-main.c

  • Committer: Bazaar Package Importer
  • Author(s): Ted Gould
  • Date: 2010-01-08 08:53:18 UTC
  • mto: This revision was merged to the branch mainline in revision 20.
  • Revision ID: james.westby@ubuntu.com-20100108085318-sloka5g4ai091fks
Tags: upstream-0.3.1
ImportĀ upstreamĀ versionĀ 0.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
A small wrapper utility to load indicators and put them as menu items
3
 
into the gnome-panel using it's applet interface.
4
 
 
5
 
Copyright 2009 Canonical Ltd.
6
 
 
7
 
Authors:
8
 
    Ted Gould <ted@canonical.com>
9
 
 
10
 
This program is free software: you can redistribute it and/or modify it 
11
 
under the terms of the GNU General Public License version 3, as published 
12
 
by the Free Software Foundation.
13
 
 
14
 
This program is distributed in the hope that it will be useful, but 
15
 
WITHOUT ANY WARRANTY; without even the implied warranties of 
16
 
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
17
 
PURPOSE.  See the GNU General Public License for more details.
18
 
 
19
 
You should have received a copy of the GNU General Public License along 
20
 
with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 
*/
22
 
 
23
 
#include <config.h>
24
 
#include <panel-applet.h>
25
 
 
26
 
#include "libindicator/indicator-object.h"
27
 
 
28
 
static gboolean     applet_fill_cb (PanelApplet * applet, const gchar * iid, gpointer data);
29
 
 
30
 
 
31
 
static void cw_panel_background_changed (PanelApplet               *applet,
32
 
                                         PanelAppletBackgroundType  type,
33
 
                                                                 GdkColor                  *colour,
34
 
                                                                 GdkPixmap                 *pixmap,
35
 
                                         GtkWidget                 *menubar);
36
 
 
37
 
/*************
38
 
 * main
39
 
 * ***********/
40
 
 
41
 
PANEL_APPLET_BONOBO_FACTORY ("OAFIID:GNOME_FastUserSwitchApplet_Factory",
42
 
               PANEL_TYPE_APPLET,
43
 
               "indicator-applet-session", "0",
44
 
               applet_fill_cb, NULL);
45
 
 
46
 
/*************
47
 
 * init function
48
 
 * ***********/
49
 
static gboolean
50
 
load_module (const gchar * name, GtkWidget * menu)
51
 
{
52
 
        g_debug("Looking at Module: %s", name);
53
 
        g_return_val_if_fail(name != NULL, FALSE);
54
 
 
55
 
        if (!g_str_has_suffix(name, G_MODULE_SUFFIX)) {
56
 
                return FALSE;
57
 
        }
58
 
 
59
 
        g_debug("Loading Module: %s", name);
60
 
 
61
 
        gchar * fullpath = g_build_filename(INDICATOR_DIR, name, NULL);
62
 
        IndicatorObject * io = indicator_object_new_from_file(fullpath);
63
 
        g_free(fullpath);
64
 
 
65
 
        GList * entries = indicator_object_get_entries(io);
66
 
        GList * entry = NULL;
67
 
 
68
 
        for (entry = entries; entry != NULL; entry = g_list_next(entry)) {
69
 
                IndicatorObjectEntry * entrydata = (IndicatorObjectEntry *)entry->data;
70
 
 
71
 
                GtkWidget * menuitem = gtk_menu_item_new();
72
 
                GtkWidget * hbox = gtk_hbox_new(FALSE, 3);
73
 
                if (entrydata->image != NULL) {
74
 
                        gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(entrydata->image), FALSE, FALSE, 0);
75
 
                }
76
 
                if (entrydata->label != NULL) {
77
 
                        gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(entrydata->label), FALSE, FALSE, 0);
78
 
                }
79
 
                gtk_container_add(GTK_CONTAINER(menuitem), hbox);
80
 
                gtk_widget_show(hbox);
81
 
 
82
 
                if (entrydata->menu != NULL) {
83
 
                        gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), GTK_WIDGET(entrydata->menu));
84
 
                }
85
 
 
86
 
                gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
87
 
                gtk_widget_show(menuitem);
88
 
        }
89
 
 
90
 
        g_list_free(entries);
91
 
 
92
 
        return TRUE;
93
 
}
94
 
 
95
 
static gboolean
96
 
menubar_press (GtkWidget * widget,
97
 
                    GdkEventButton *event,
98
 
                    gpointer data)
99
 
{
100
 
        if (event->button != 1) {
101
 
                g_signal_stop_emission_by_name(widget, "button-press-event");
102
 
        }
103
 
 
104
 
        return FALSE;
105
 
}
106
 
 
107
 
static gboolean
108
 
menubar_on_expose (GtkWidget * widget,
109
 
                    GdkEventExpose *event,
110
 
                    GtkWidget * menubar)
111
 
{
112
 
        if (GTK_WIDGET_HAS_FOCUS(menubar))
113
 
                gtk_paint_focus(widget->style, widget->window, GTK_WIDGET_STATE(menubar),
114
 
                                NULL, widget, "menubar-applet", 0, 0, -1, -1);
115
 
 
116
 
        return FALSE;
117
 
}
118
 
 
119
 
static void
120
 
about_cb (BonoboUIComponent *ui_container,
121
 
          gpointer           data,
122
 
          const gchar       *cname)
123
 
{
124
 
        static const gchar *authors[] = {
125
 
                "Ted Gould <ted@canonical.com>",
126
 
                NULL
127
 
        };
128
 
 
129
 
        static gchar *license[] = {
130
 
        N_("This program is free software: you can redistribute it and/or modify it "
131
 
           "under the terms of the GNU General Public License version 3, as published "
132
 
           "by the Free Software Foundation."),
133
 
        N_("This program is distributed in the hope that it will be useful, but "
134
 
           "WITHOUT ANY WARRANTY; without even the implied warranties of "
135
 
           "MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR "
136
 
           "PURPOSE.  See the GNU General Public License for more details."),
137
 
        N_("You should have received a copy of the GNU General Public License along "
138
 
           "with this program.  If not, see <http://www.gnu.org/licenses/>."),
139
 
                NULL
140
 
        };
141
 
        gchar *license_i18n;
142
 
 
143
 
        license_i18n = g_strconcat (_(license[0]), "\n\n", _(license[1]), "\n\n", _(license[2]), NULL);
144
 
 
145
 
        gtk_show_about_dialog(NULL,
146
 
                "version", VERSION,
147
 
                "copyright", "Copyright \xc2\xa9 2009 Canonical, Ltd.",
148
 
                "comments", _("A place to adjust your status, change users or exit your session."),
149
 
                "authors", authors,
150
 
                "license", license_i18n,
151
 
                "wrap-license", TRUE,
152
 
                "translator-credits", _("translator-credits"),
153
 
                "logo-icon-name", "indicator-applet",
154
 
                "icon-name", "indicator-applet",
155
 
                "website", "http://launchpad.net/indicator-applet",
156
 
                "website-label", _("Indicator Applet Website"),
157
 
                NULL
158
 
        );
159
 
 
160
 
        g_free (license_i18n);
161
 
 
162
 
        return;
163
 
}
164
 
 
165
 
#ifdef N_
166
 
#undef N_
167
 
#endif
168
 
#define N_(x) x
169
 
 
170
 
static gboolean
171
 
applet_fill_cb (PanelApplet * applet, const gchar * iid, gpointer data)
172
 
{
173
 
        static const BonoboUIVerb menu_verbs[] = {
174
 
                BONOBO_UI_VERB ("IndicatorAppletAbout", about_cb),
175
 
                BONOBO_UI_VERB_END
176
 
        };
177
 
        static const gchar * menu_xml = 
178
 
                "<popup name=\"button3\">"
179
 
                        "<menuitem name=\"About Item\" verb=\"IndicatorAppletAbout\" _label=\"" N_("_About") "\" pixtype=\"stock\" pixname=\"gtk-about\"/>"
180
 
                "</popup>";
181
 
 
182
 
        GtkWidget *menubar;
183
 
        gint i;
184
 
        gint indicators_loaded = 0;
185
 
        static gboolean first_time = FALSE;
186
 
 
187
 
        /* check if we are running stracciatella session */
188
 
        if (g_strcmp0(g_getenv("GDMSESSION"), "gnome-stracciatella") == 0) {
189
 
                g_debug("Running stracciatella GNOME session, disabling myself");
190
 
                return TRUE;
191
 
        }
192
 
 
193
 
        if (!first_time)
194
 
        {
195
 
                first_time = TRUE;
196
 
                g_set_application_name(_("Indicator Applet Session"));
197
 
        }
198
 
 
199
 
        /* Set panel options */
200
 
        gtk_container_set_border_width(GTK_CONTAINER (applet), 0);
201
 
        panel_applet_set_flags(applet, PANEL_APPLET_EXPAND_MINOR);
202
 
        panel_applet_setup_menu(applet, menu_xml, menu_verbs, NULL);
203
 
    atk_object_set_name (gtk_widget_get_accessible (GTK_WIDGET (applet)),
204
 
                         "indicator-applet-session");
205
 
  
206
 
        /* Init some theme/icon stuff */
207
 
        gtk_icon_theme_append_search_path(gtk_icon_theme_get_default(),
208
 
                                          INDICATOR_ICONS_DIR);
209
 
        /* g_debug("Icons directory: %s", INDICATOR_ICONS_DIR); */
210
 
        gtk_rc_parse_string (
211
 
            "style \"indicator-applet-style\"\n"
212
 
        "{\n"
213
 
        "    GtkMenuBar::shadow-type = none\n"
214
 
        "    GtkMenuBar::internal-padding = 0\n"
215
 
        "    GtkWidget::focus-line-width = 0\n"
216
 
        "    GtkWidget::focus-padding = 0\n"
217
 
        "}\n"
218
 
            "style \"indicator-applet-menubar-style\"\n"
219
 
        "{\n"
220
 
        "    GtkMenuBar::shadow-type = none\n"
221
 
        "    GtkMenuBar::internal-padding = 0\n"
222
 
        "    GtkWidget::focus-line-width = 0\n"
223
 
        "    GtkWidget::focus-padding = 0\n"
224
 
        "    GtkMenuItem::horizontal-padding = 0\n"
225
 
        "}\n"
226
 
            "style \"indicator-applet-menuitem-style\"\n"
227
 
        "{\n"
228
 
        "    GtkWidget::focus-line-width = 0\n"
229
 
        "    GtkWidget::focus-padding = 0\n"
230
 
        "    GtkMenuItem::horizontal-padding = 0\n"
231
 
        "}\n"
232
 
        "widget \"*.fast-user-switch-applet\" style \"indicator-applet-style\""
233
 
        "widget \"*.fast-user-switch-menuitem\" style \"indicator-applet-menuitem-style\""
234
 
        "widget \"*.fast-user-switch-menubar\" style \"indicator-applet-menubar-style\"");
235
 
        //gtk_widget_set_name(GTK_WIDGET (applet), "indicator-applet-menubar");
236
 
        gtk_widget_set_name(GTK_WIDGET (applet), "fast-user-switch-applet");
237
 
 
238
 
        /* Build menubar */
239
 
        menubar = gtk_menu_bar_new();
240
 
        GTK_WIDGET_SET_FLAGS (menubar, GTK_WIDGET_FLAGS(menubar) | GTK_CAN_FOCUS);
241
 
        gtk_widget_set_name(GTK_WIDGET (menubar), "fast-user-switch-menubar");
242
 
        g_signal_connect(menubar, "button-press-event", G_CALLBACK(menubar_press), NULL);
243
 
        g_signal_connect_after(menubar, "expose-event", G_CALLBACK(menubar_on_expose), menubar);
244
 
        gtk_container_set_border_width(GTK_CONTAINER(menubar), 0);
245
 
 
246
 
        /* load 'em */
247
 
        if (g_file_test(INDICATOR_DIR, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))) {
248
 
                GDir * dir = g_dir_open(INDICATOR_DIR, 0, NULL);
249
 
 
250
 
                const gchar * name;
251
 
                while ((name = g_dir_read_name(dir)) != NULL) {
252
 
                        if (g_strcmp0(name, "libsession.so")) {
253
 
                                continue;
254
 
                        }
255
 
                        if (load_module(name, menubar)) {
256
 
                                indicators_loaded++;
257
 
                        }
258
 
                }
259
 
                g_dir_close (dir);
260
 
        }
261
 
 
262
 
        if (indicators_loaded == 0) {
263
 
                /* A label to allow for click through */
264
 
                GtkWidget * item = gtk_label_new(_("No Indicators"));
265
 
                gtk_container_add(GTK_CONTAINER(applet), item);
266
 
                gtk_widget_show(item);
267
 
        } else {
268
 
                gtk_container_add(GTK_CONTAINER(applet), menubar);
269
 
                panel_applet_set_background_widget(applet, menubar);
270
 
                gtk_widget_show(menubar);
271
 
        }
272
 
  
273
 
        /* Background of applet */
274
 
        g_signal_connect(applet, "change-background",
275
 
                          G_CALLBACK(cw_panel_background_changed), menubar);
276
 
  
277
 
        gtk_widget_show(GTK_WIDGET(applet));
278
 
 
279
 
        return TRUE;
280
 
}
281
 
 
282
 
static void 
283
 
cw_panel_background_changed (PanelApplet               *applet,
284
 
                             PanelAppletBackgroundType  type,
285
 
                             GdkColor                  *colour,
286
 
                             GdkPixmap                 *pixmap,
287
 
                             GtkWidget                 *menubar)
288
 
{
289
 
        GtkRcStyle *rc_style;
290
 
        GtkStyle *style;
291
 
 
292
 
        /* reset style */
293
 
        gtk_widget_set_style(GTK_WIDGET (applet), NULL);
294
 
        gtk_widget_set_style(menubar, NULL);
295
 
        rc_style = gtk_rc_style_new ();
296
 
        gtk_widget_modify_style(GTK_WIDGET (applet), rc_style);
297
 
        gtk_widget_modify_style(menubar, rc_style);
298
 
        gtk_rc_style_unref(rc_style);
299
 
 
300
 
        switch (type) 
301
 
        {
302
 
                case PANEL_NO_BACKGROUND:
303
 
                        break;
304
 
                case PANEL_COLOR_BACKGROUND:
305
 
                        gtk_widget_modify_bg(GTK_WIDGET (applet), GTK_STATE_NORMAL, colour);
306
 
                        gtk_widget_modify_bg(menubar, GTK_STATE_NORMAL, colour);
307
 
                        break;
308
 
    
309
 
                case PANEL_PIXMAP_BACKGROUND:
310
 
                        style = gtk_style_copy(GTK_WIDGET (applet)->style);
311
 
                        if (style->bg_pixmap[GTK_STATE_NORMAL])
312
 
                                g_object_unref(style->bg_pixmap[GTK_STATE_NORMAL]);
313
 
                        style->bg_pixmap[GTK_STATE_NORMAL] = g_object_ref (pixmap);
314
 
                        gtk_widget_set_style(GTK_WIDGET (applet), style);
315
 
                        gtk_widget_set_style(GTK_WIDGET (menubar), style);
316
 
                        g_object_unref(style);
317
 
                        break;
318
 
  }
319
 
}
320