0.8.11claws39
[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
30 #include "eggtrayicon.h"
31 #include "gnome-mail.xpm"
32 #include "gnome-nomail.xpm"
33
34 static gint hook_id;
35
36 static GdkPixmap *mail_pixmap;
37 static GdkPixmap *mail_bitmap;
38 static GdkPixmap *nomail_pixmap;
39 static GdkPixmap *nomail_bitmap;
40
41 static EggTrayIcon *trayicon;
42 static GtkWidget *eventbox;
43 static GtkWidget *image;
44 static GtkTooltips *tooltips;
45
46 typedef enum
47 {
48         TRAYICON_NEW,
49         TRAYICON_UNREAD,
50         TRAYICON_UNREADMARKED,
51         TRAYICON_NOTHING,
52 } TrayIconType;
53
54 static gboolean mainwin_hidden = FALSE;
55
56 static gboolean click_cb(GtkWidget * widget,
57                          GdkEventButton * event, gpointer user_data)
58 {
59 /*
60         MainWindow *mainwin;
61
62         mainwin = mainwindow_get_mainwindow();
63         if (mainwin_hidden) {
64                 gtk_widget_show(mainwin->window);
65                 mainwin_hidden = FALSE;
66         } else {
67                 gtk_widget_hide(mainwin->window);
68                 mainwin_hidden = TRUE;
69         }
70 */
71         return TRUE;
72 }
73
74 static void set_trayicon_pixmap(TrayIconType icontype)
75 {
76         GdkPixmap *pixmap = NULL;
77         GdkBitmap *bitmap = NULL;
78
79         switch(icontype) {
80         case TRAYICON_NEW:
81                 pixmap = mail_pixmap;
82                 bitmap = mail_bitmap;
83                 break;
84         default:
85                 pixmap = nomail_pixmap;
86                 bitmap = nomail_bitmap;
87                 break;
88         }
89
90         gtk_pixmap_set(GTK_PIXMAP(image), pixmap, bitmap);
91 }
92
93 static gboolean folder_item_update_hook(gpointer source, gpointer data)
94 {
95         gint new, unread, unreadmarked, total;
96         gchar *buf;
97
98         folder_count_total_msgs(&new, &unread, &unreadmarked, &total);
99         buf = g_strdup_printf("New %d, Unread: %d, Total: %d", new, unread, total);
100
101         gtk_tooltips_set_tip(tooltips, eventbox, buf, "");
102         g_free(buf);
103
104         set_trayicon_pixmap(new > 0 ? TRAYICON_NEW : TRAYICON_NOTHING);
105
106         return FALSE;
107 }
108
109 int plugin_init(gchar **error)
110 {
111         hook_id = hooks_register_hook (FOLDER_ITEM_UPDATE_HOOKLIST, folder_item_update_hook, NULL);
112         if (hook_id == -1) {
113                 *error = g_strdup("Failed to register folder item update hook");
114                 return -1;
115         }
116
117         trayicon = egg_tray_icon_new("Sylpheed-Claws");
118         gtk_container_set_border_width(GTK_CONTAINER(trayicon), 0);
119
120         nomail_pixmap = gdk_pixmap_create_from_xpm_d(GTK_WIDGET(trayicon)->window, &nomail_bitmap, NULL, nomail_xpm);
121         mail_pixmap = gdk_pixmap_create_from_xpm_d(GTK_WIDGET(trayicon)->window, &mail_bitmap, NULL, mail_xpm);
122     
123         eventbox = gtk_event_box_new();
124         gtk_container_set_border_width(eventbox, 0);
125         gtk_container_add(GTK_CONTAINER(trayicon), GTK_WIDGET(eventbox));
126
127         image = gtk_pixmap_new (nomail_pixmap, nomail_bitmap);
128         gtk_container_add(GTK_CONTAINER(eventbox), GTK_WIDGET(image));
129
130         gtk_signal_connect(GTK_OBJECT(eventbox), "button-press-event", GTK_SIGNAL_FUNC(click_cb), NULL);
131
132         tooltips = gtk_tooltips_new();
133         gtk_tooltips_set_delay(tooltips, 1000);
134         gtk_tooltips_enable(tooltips);
135
136         gtk_widget_show_all(GTK_WIDGET(trayicon));
137
138         return 0;
139 }
140
141 void plugin_done()
142 {
143         gtk_widget_destroy(GTK_WIDGET(trayicon));
144         hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, hook_id);
145 }
146
147 const gchar *plugin_name()
148 {
149         return "Trayicon";
150 }
151
152 const gchar *plugin_desc()
153 {
154         return "";
155 }
156
157 const gchar *plugin_type()
158 {
159         return "GTK";
160 }