0494e1dab20a60d5ad3a3403378100d1d6e01da8
[claws.git] / src / plugins / trayicon / trayicon.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 Hiroyuki Yamamoto and the Sylpheed-Claws Team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include <stdio.h>
21
22 #include <glib.h>
23 #include <gtk/gtk.h>
24
25 #include "plugin.h"
26 #include "hooks.h"
27 #include "folder.h"
28 #include "mainwindow.h"
29 #include "gtkutils.h"
30 #include "intl.h"
31
32 #include "eggtrayicon.h"
33 #include "newmail.xpm"
34 #include "unreadmail.xpm"
35 #include "nomail.xpm"
36
37 static gint hook_id;
38
39 static GdkPixmap *newmail_pixmap;
40 static GdkPixmap *newmail_bitmap;
41 static GdkPixmap *unreadmail_pixmap;
42 static GdkPixmap *unreadmail_bitmap;
43 static GdkPixmap *nomail_pixmap;
44 static GdkPixmap *nomail_bitmap;
45
46 static EggTrayIcon *trayicon;
47 static GtkWidget *eventbox;
48 static GtkWidget *image;
49 static GtkTooltips *tooltips;
50
51 typedef enum
52 {
53         TRAYICON_NEW,
54         TRAYICON_UNREAD,
55         TRAYICON_UNREADMARKED,
56         TRAYICON_NOTHING,
57 } TrayIconType;
58
59 /* static gboolean mainwin_hidden = FALSE; */
60
61 static void set_trayicon_pixmap(TrayIconType icontype)
62 {
63         GdkPixmap *pixmap = NULL;
64         GdkBitmap *bitmap = NULL;
65
66         switch(icontype) {
67         case TRAYICON_NEW:
68                 pixmap = newmail_pixmap;
69                 bitmap = newmail_bitmap;
70                 break;
71         case TRAYICON_UNREAD:
72         case TRAYICON_UNREADMARKED:
73                 pixmap = unreadmail_pixmap;
74                 bitmap = unreadmail_bitmap;
75                 break;
76         default:
77                 pixmap = nomail_pixmap;
78                 bitmap = nomail_bitmap;
79                 break;
80         }
81
82         gtk_pixmap_set(GTK_PIXMAP(image), pixmap, bitmap);
83         gtk_widget_shape_combine_mask(GTK_WIDGET(trayicon), bitmap, GTK_WIDGET(image)->allocation.x, GTK_WIDGET(image)->allocation.y);
84 }
85
86 static void update(void)
87 {
88         gint new, unread, unreadmarked, total;
89         gchar *buf;
90
91         folder_count_total_msgs(&new, &unread, &unreadmarked, &total);
92         buf = g_strdup_printf("New %d, Unread: %d, Total: %d", new, unread, total);
93
94         gtk_tooltips_set_tip(tooltips, eventbox, buf, "");
95         g_free(buf);
96
97         set_trayicon_pixmap(new > 0 ? TRAYICON_NEW : (unread > 0 ? TRAYICON_UNREAD : TRAYICON_NOTHING));
98 }
99
100 static gboolean folder_item_update_hook(gpointer source, gpointer data)
101 {
102         update();
103
104         return FALSE;
105 }
106
107 static gboolean click_cb(GtkWidget * widget,
108                          GdkEventButton * event, gpointer user_data)
109 {
110         MainWindow *mainwin;
111
112 /*
113         mainwin = mainwindow_get_mainwindow();
114         if (GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window))) {
115                 gtk_widget_hide_all(mainwin->window);
116         } else {
117                 gtk_widget_show_all(mainwin->window);
118         }
119 */
120         return TRUE;
121 }
122
123 static void resize_cb(GtkWidget *widget, GtkAllocation *allocation)
124 {
125         update();
126 }
127
128 int plugin_init(gchar **error)
129 {
130         GtkPacker *packer;
131
132         hook_id = hooks_register_hook (FOLDER_ITEM_UPDATE_HOOKLIST, folder_item_update_hook, NULL);
133         if (hook_id == -1) {
134                 *error = g_strdup("Failed to register folder item update hook");
135                 return -1;
136         }
137
138         trayicon = egg_tray_icon_new("Sylpheed-Claws");
139 /*        trayicon = gtk_window_new(GTK_WINDOW_TOPLEVEL); */
140         gtk_window_set_default_size(GTK_WINDOW(trayicon), 16, 16);
141         gtk_container_set_border_width(GTK_CONTAINER(trayicon), 0);
142
143         PIXMAP_CREATE(GTK_WIDGET(trayicon), nomail_pixmap, nomail_bitmap, nomail_xpm);
144         PIXMAP_CREATE(GTK_WIDGET(trayicon), unreadmail_pixmap, unreadmail_bitmap, unreadmail_xpm);
145         PIXMAP_CREATE(GTK_WIDGET(trayicon), newmail_pixmap, newmail_bitmap, newmail_xpm);
146
147         eventbox = gtk_event_box_new();
148         gtk_container_set_border_width(GTK_CONTAINER(eventbox), 0);
149         gtk_container_add(GTK_CONTAINER(trayicon), GTK_WIDGET(eventbox));
150
151         packer = GTK_PACKER(gtk_packer_new());
152         gtk_container_add(GTK_CONTAINER(eventbox), GTK_WIDGET(packer));
153         gtk_container_set_border_width(GTK_CONTAINER(packer), 0);
154
155         image = gtk_pixmap_new(nomail_pixmap, nomail_bitmap);
156         gtk_packer_add_defaults(GTK_PACKER(packer), GTK_WIDGET(image), GTK_SIDE_TOP, GTK_ANCHOR_CENTER, GTK_PACK_EXPAND);
157
158         gtk_signal_connect(GTK_OBJECT(trayicon), "size_allocate", GTK_SIGNAL_FUNC(resize_cb), NULL);
159         gtk_signal_connect(GTK_OBJECT(eventbox), "button-press-event", GTK_SIGNAL_FUNC(click_cb), NULL);
160
161         tooltips = gtk_tooltips_new();
162         gtk_tooltips_set_delay(tooltips, 1000);
163         gtk_tooltips_enable(tooltips);
164
165         gtk_widget_show_all(GTK_WIDGET(trayicon));
166
167         update();
168
169         return 0;
170 }
171
172 void plugin_done(void)
173 {
174         gtk_widget_destroy(GTK_WIDGET(trayicon));
175         hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, hook_id);
176 }
177
178 const gchar *plugin_name(void)
179 {
180         return _("Trayicon");
181 }
182
183 const gchar *plugin_desc(void)
184 {
185         return _("This plugin places a mailbox icon in the system tray that "
186                  "indicates if you have new or unread mail.\n"
187                  "\n"
188                  "The mailbox is empty if you have no unread mail, otherwise "
189                  "it contains a letter. A tooltip shows new, unread and total "
190                  "number of messages.");
191 }
192
193 const gchar *plugin_type(void)
194 {
195         return "GTK";
196 }