aboutsummaryrefslogtreecommitdiffstats
path: root/applets/systray/systray.c
blob: 68c8fea453d7ce51e8fbf750da74c5188019f140 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* 
 * (C) 2006 OpenedHand Ltd.
 *
 * Author: Jorn Baayen <jorn@openedhand.com>
 *
 * Licensed under the GPL v2 or greater.
 */

#include <config.h>
#include <gtk/gtkhbox.h>
#include <gtk/gtkvbox.h>
#include <matchbox-panel/mb-panel.h>

#ifdef USE_LIBNOTIFY
  #include <libnotify/notify.h>
#endif

#include "eggtraymanager.h"

/* Tray icon added */
static void
tray_icon_added_cb (EggTrayManager *manager,
                    GtkWidget      *icon,
                    GtkBox         *box)
{
        gtk_box_pack_start (box, icon, FALSE, FALSE, 0);
}

#ifdef USE_LIBNOTIFY
/* Notification closed */
static void
notification_closed_cb (NotifyNotification *n,
                        GtkWidget          *icon)
{
        GHashTable *hash;
        gpointer id;

        /* Remove reference to notification */
        hash = g_object_get_data (G_OBJECT (icon), "notification-hash");
        g_assert (hash);

        id = g_object_get_data (G_OBJECT (n), "id");

        g_hash_table_remove (hash, id);
}

/* Message sent */
static void
message_sent_cb (EggTrayManager      *manager,
                 GtkWidget           *icon,
                 const char          *message,
                 long                 id,
                 long                 timeout,
                 gpointer             user_data)
{
        GHashTable *hash;
        NotifyNotification *n;
        GError *error;

        /* Create notification */
        n = notify_notification_new (message,
                                     NULL,
                                     NULL,
                                     icon);
        notify_notification_set_timeout (n, timeout);

        error = NULL;
        if (!notify_notification_show (n, &error)) {
                g_warning (error->message);

                g_error_free (error);

                g_object_unref (n);

                return;
        }

        /* Keep a reference to the notification */
        hash = g_object_get_data (G_OBJECT (icon), "notification-hash");
        if (!hash) {
                hash = g_hash_table_new_full (g_int_hash,
                                              g_int_equal,
                                              NULL,
                                              (GDestroyNotify) g_object_unref);

                g_object_set_data_full (G_OBJECT (icon),
                                        "notification-hash",
                                        hash,
                                        (GDestroyNotify) g_hash_table_destroy);
        }

        g_hash_table_insert (hash, GINT_TO_POINTER (id), n);

        /* Make sure the notification is removed from the hash once
         * closed */
        g_object_set_data (G_OBJECT (n), "id", GINT_TO_POINTER (id));
        g_signal_connect (n,
                          "closed",
                          G_CALLBACK (notification_closed_cb),
                          icon);
}

/* Message cancelled */
static void
message_cancelled_cb (EggTrayManager *manager,
                      GtkWidget      *icon,
                      long            id,
                      gpointer        user_data)
{
        GHashTable *hash;
        NotifyNotification *n;
        GError *error;
        
        /* Look up reference to notification */
        hash = g_object_get_data (G_OBJECT (icon), "notification-hash");
        if (!hash)
                return;

        n = g_hash_table_lookup (hash, GINT_TO_POINTER (id));
        if (!n)
                return;

        /* Close it */
        error = NULL;
        notify_notification_close (n, &error);
        if (error) {
                g_warning (error->message);

                g_error_free (error);
        }
}
#endif

/* Screen changed */
static void
screen_changed_cb (GtkWidget      *widget,
                   GdkScreen      *old_screen,
                   EggTrayManager *manager)
{
        GdkScreen *screen;

        screen = gtk_widget_get_screen (widget);
        if (egg_tray_manager_check_running (screen)) {
                g_warning ("Another system tray manager is running. "
                           "Not managing screen.");

                return;
        }

        egg_tray_manager_manage_screen (manager, screen);
}

G_MODULE_EXPORT GtkWidget *
mb_panel_applet_create (const char    *id,
                        GtkOrientation orientation)
{
        EggTrayManager *manager;
        GtkWidget *box;

#ifdef USE_LIBNOTIFY
        if (!notify_is_initted ())
                notify_init ("matchbox-panel");
#endif

        /* Is this a horizontal panel? */
        if (orientation == GTK_ORIENTATION_HORIZONTAL)
                box = gtk_hbox_new (0, FALSE);
        else
                box = gtk_vbox_new (0, FALSE);

        gtk_widget_set_name (box, "MatchboxPanelSystemTray");

        /* Create tray manager */
        manager = egg_tray_manager_new ();
        egg_tray_manager_set_orientation (manager, orientation);

        g_signal_connect (manager,
                          "tray-icon-added",
                          G_CALLBACK (tray_icon_added_cb),
                          box);

#ifdef USE_LIBNOTIFY
        g_signal_connect (manager,
                          "message-sent",
                          G_CALLBACK (message_sent_cb),
                          NULL);
        g_signal_connect (manager,
                          "message-cancelled",
                          G_CALLBACK (message_cancelled_cb),
                          NULL);
#endif

        g_signal_connect (box,
                          "screen-changed",
                          G_CALLBACK (screen_changed_cb),
                          manager);

        g_object_weak_ref (G_OBJECT (box),
                           (GWeakNotify) g_object_unref,
                           manager);
        
        /* Show! */
        gtk_widget_show (box);

        return box;
}