dd6ed3303e425ca2b244ad50a64044bda9c7948e
[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
31 #include "eggtrayicon.h"
32 #include "newmail.xpm"
33 #include "unreadmail.xpm"
34 #include "nomail.xpm"
35
36 static gint hook_id;
37
38 static GdkPixmap *newmail_pixmap;
39 static GdkPixmap *newmail_bitmap;
40 static GdkPixmap *unreadmail_pixmap;
41 static GdkPixmap *unreadmail_bitmap;
42 static GdkPixmap *nomail_pixmap;
43 static GdkPixmap *nomail_bitmap;
44
45 static EggTrayIcon *trayicon;
46 static GtkWidget *eventbox;
47 static GtkWidget *image;
48 static GtkTooltips *tooltips;
49
50 typedef enum
51 {
52         TRAYICON_NEW,
53         TRAYICON_UNREAD,
54         TRAYICON_UNREADMARKED,
55         TRAYICON_NOTHING,
56 } TrayIconType;
57
58 /* static gboolean mainwin_hidden = FALSE; */
59
60 static void set_trayicon_pixmap(TrayIconType icontype)
61 {
62         GdkPixmap *pixmap = NULL;
63         GdkBitmap *bitmap = NULL;
64
65         switch(icontype) {
66         case TRAYICON_NEW:
67                 pixmap = newmail_pixmap;
68                 bitmap = newmail_bitmap;
69                 break;
70         case TRAYICON_UNREAD:
71         case TRAYICON_UNREADMARKED:
72                 pixmap = unreadmail_pixmap;
73                 bitmap = unreadmail_bitmap;
74                 break;
75         default:
76                 pixmap = nomail_pixmap;
77                 bitmap = nomail_bitmap;
78                 break;
79         }
80
81         gtk_pixmap_set(GTK_PIXMAP(image), pixmap, bitmap);
82         gtk_widget_shape_combine_mask(GTK_WIDGET(trayicon), bitmap, GTK_WIDGET(image)->allocation.x, GTK_WIDGET(image)->allocation.y);
83 }
84
85 static void update()
86 {
87         gint new, unread, unreadmarked, total;
88         gchar *buf;
89
90         folder_count_total_msgs(&new, &unread, &unreadmarked, &total);
91         buf = g_strdup_printf("New %d, Unread: %d, Total: %d", new, unread, total);
92
93         gtk_tooltips_set_tip(tooltips, eventbox, buf, "");
94         g_free(buf);
95
96         set_trayicon_pixmap(new > 0 ? TRAYICON_NEW : (unread > 0 ? TRAYICON_UNREAD : TRAYICON_NOTHING));
97 }
98
99 static gboolean folder_item_update_hook(gpointer source, gpointer data)
100 {
101         update();
102
103         return FALSE;
104 }
105
106 static gboolean click_cb(GtkWidget * widget,
107                          GdkEventButton * event, gpointer user_data)
108 {
109 /*
110         MainWindow *mainwin;
111
112         mainwin = mainwindow_get_mainwindow();
113         if (mainwin_hidden) {
114                 gtk_widget_show(mainwin->window);
115                 mainwin_hidden = FALSE;
116         } else {
117                 gtk_widget_hide(mainwin->window);
118                 mainwin_hidden = TRUE;
119         }
120 */
121
122         return TRUE;
123 }
124
125 static void resize_cb(GtkWidget *widget, GtkAllocation *allocation)
126 {
127         update();
128 }
129
130 int plugin_init(gchar **error)
131 {
132         GtkPacker *packer;
133
134         hook_id = hooks_register_hook (FOLDER_ITEM_UPDATE_HOOKLIST, folder_item_update_hook, NULL);
135         if (hook_id == -1) {
136                 *error = g_strdup("Failed to register folder item update hook");
137                 return -1;
138         }
139
140         trayicon = egg_tray_icon_new("Sylpheed-Claws");
141 //        trayicon = gtk_window_new(GTK_WINDOW_TOPLEVEL);
142         gtk_window_set_default_size(GTK_WINDOW(trayicon), 16, 16);
143         gtk_container_set_border_width(GTK_CONTAINER(trayicon), 0);
144
145         PIXMAP_CREATE(GTK_WIDGET(trayicon), nomail_pixmap, nomail_bitmap, nomail_xpm);
146         PIXMAP_CREATE(GTK_WIDGET(trayicon), unreadmail_pixmap, unreadmail_bitmap, unreadmail_xpm);
147         PIXMAP_CREATE(GTK_WIDGET(trayicon), newmail_pixmap, newmail_bitmap, newmail_xpm);
148
149         eventbox = gtk_event_box_new();
150         gtk_container_set_border_width(GTK_CONTAINER(eventbox), 0);
151         gtk_container_add(GTK_CONTAINER(trayicon), GTK_WIDGET(eventbox));
152
153         packer = GTK_PACKER(gtk_packer_new());
154         gtk_container_add(GTK_CONTAINER(eventbox), GTK_WIDGET(packer));
155         gtk_container_set_border_width(GTK_CONTAINER(packer), 0);
156
157         image = gtk_pixmap_new(nomail_pixmap, nomail_bitmap);
158         gtk_packer_add_defaults(GTK_PACKER(packer), GTK_WIDGET(image), GTK_SIDE_TOP, GTK_ANCHOR_CENTER, GTK_PACK_EXPAND);
159
160         gtk_signal_connect(GTK_OBJECT(trayicon), "size_allocate", GTK_SIGNAL_FUNC(resize_cb), NULL);
161         gtk_signal_connect(GTK_OBJECT(eventbox), "button-press-event", GTK_SIGNAL_FUNC(click_cb), NULL);
162
163         tooltips = gtk_tooltips_new();
164         gtk_tooltips_set_delay(tooltips, 1000);
165         gtk_tooltips_enable(tooltips);
166
167         gtk_widget_show_all(GTK_WIDGET(trayicon));
168
169         update();
170
171         return 0;
172 }
173
174 void plugin_done()
175 {
176         gtk_widget_destroy(GTK_WIDGET(trayicon));
177         hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, hook_id);
178 }
179
180 const gchar *plugin_name()
181 {
182         return "Trayicon";
183 }
184
185 const gchar *plugin_desc()
186 {
187         return "";
188 }
189
190 const gchar *plugin_type()
191 {
192         return "GTK";
193 }