2006-11-12 [paul] 2.6.0cvs34
[claws.git] / src / plugins / trayicon / trayicon.c
index 613ebe6a5cc9710e28e7a4f146eae8a35e8ef3e7..15fdfbdb9150a66850bd5eeceb9f647c4366459b 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws Team
+ * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Claws Mail Team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 #include "prefs_common.h"
 #include "main.h"
 #include "alertpanel.h"
+#include "account.h"
 #include "gtk/manage_window.h"
 
 #include "eggtrayicon.h"
+
 #include "newmarkedmail.xpm"
 #include "unreadmarkedmail.xpm"
 #include "newmail.xpm"
 #include "unreadmail.xpm"
 #include "nomail.xpm"
-
-static guint hook_id;
-
-static GdkPixmap *newmail_pixmap;
-static GdkPixmap *newmail_bitmap;
-static GdkPixmap *unreadmail_pixmap;
-static GdkPixmap *unreadmail_bitmap;
-static GdkPixmap *newmarkedmail_pixmap;
-static GdkPixmap *newmarkedmail_bitmap;
-static GdkPixmap *unreadmarkedmail_pixmap;
-static GdkPixmap *unreadmarkedmail_bitmap;
-static GdkPixmap *nomail_pixmap;
-static GdkPixmap *nomail_bitmap;
+#include "newmarkedmail.offline.xpm"
+#include "unreadmarkedmail.offline.xpm"
+#include "newmail.offline.xpm"
+#include "unreadmail.offline.xpm"
+#include "nomail.offline.xpm"
+
+static guint item_hook_id;
+static guint folder_hook_id;
+static guint offline_hook_id;
+static guint account_hook_id;
+
+static GdkPixmap *newmail_pixmap[2];
+static GdkPixmap *newmail_bitmap[2];
+static GdkPixmap *unreadmail_pixmap[2];
+static GdkPixmap *unreadmail_bitmap[2];
+static GdkPixmap *newmarkedmail_pixmap[2];
+static GdkPixmap *newmarkedmail_bitmap[2];
+static GdkPixmap *unreadmarkedmail_pixmap[2];
+static GdkPixmap *unreadmarkedmail_bitmap[2];
+static GdkPixmap *nomail_pixmap[2];
+static GdkPixmap *nomail_bitmap[2];
 
 static EggTrayIcon *trayicon;
 static GtkWidget *eventbox;
@@ -83,6 +93,7 @@ typedef enum
 
 static void trayicon_get_all_cb            (gpointer data, guint action, GtkWidget *widget);
 static void trayicon_compose_cb            (gpointer data, guint action, GtkWidget *widget);
+static void trayicon_compose_acc_cb (GtkMenuItem *menuitem, gpointer data );
 static void trayicon_addressbook_cb (gpointer data, guint action, GtkWidget *widget);
 static void trayicon_exit_cb       (gpointer data, guint action, GtkWidget *widget);
 static void trayicon_toggle_offline_cb (gpointer data, guint action, GtkWidget *widget);
@@ -93,13 +104,50 @@ static GtkItemFactoryEntry trayicon_popup_menu_entries[] =
        {N_("/_Get Mail"),              NULL, trayicon_get_all_cb,              0, NULL},
        {N_("/---"),                    NULL, NULL,                             0, "<Separator>"},
        {N_("/_Email"),                 NULL, trayicon_compose_cb,              0, NULL},
+       {N_("/_Email from account"),    NULL, NULL,                             0, "<Branch>"},
+       {N_("/---"),                    NULL, NULL,                             0, "<Separator>"},
        {N_("/Open A_ddressbook"),      NULL, trayicon_addressbook_cb,          0, NULL},
        {N_("/---"),                    NULL, NULL,                             0, "<Separator>"},
        {N_("/_Work Offline"),          NULL, trayicon_toggle_offline_cb,       0, "<CheckItem>"},
        {N_("/---"),                    NULL, NULL,                             0, "<Separator>"},
-       {N_("/E_xit Sylpheed-Claws"),   NULL, trayicon_exit_cb,                 0, NULL}
+       {N_("/E_xit Claws Mail"),       NULL, trayicon_exit_cb,                 0, NULL}
 };
 
+static gboolean trayicon_set_accounts_hook(gpointer source, gpointer data)
+{
+       GList *cur_ac, *cur_item = NULL;
+       GtkWidget *menu;
+       GtkWidget *menuitem;
+       PrefsAccount *ac_prefs;
+
+       GList *account_list = account_get_list();
+
+       menu = gtk_item_factory_get_widget(traymenu_factory,
+                                          "/Email from account");
+
+       /* destroy all previous menu item */
+       cur_item = GTK_MENU_SHELL(menu)->children;
+       while (cur_item != NULL) {
+               GList *next = cur_item->next;
+               gtk_widget_destroy(GTK_WIDGET(cur_item->data));
+               cur_item = next;
+       }
+
+       for (cur_ac = account_list; cur_ac != NULL; cur_ac = cur_ac->next) {
+               ac_prefs = (PrefsAccount *)cur_ac->data;
+
+               menuitem = gtk_menu_item_new_with_label
+                       (ac_prefs->account_name ? ac_prefs->account_name
+                        : _("Untitled"));
+               gtk_widget_show(menuitem);
+               gtk_menu_append(GTK_MENU(menu), menuitem);
+               g_signal_connect(G_OBJECT(menuitem), "activate",
+                                G_CALLBACK(trayicon_compose_acc_cb),
+                                ac_prefs);
+       }
+       return FALSE;
+}
+
 static void set_trayicon_pixmap(TrayIconType icontype)
 {
        GdkPixmap *pixmap = NULL;
@@ -108,24 +156,24 @@ static void set_trayicon_pixmap(TrayIconType icontype)
 
        switch(icontype) {
        case TRAYICON_NEW:
-               pixmap = newmail_pixmap;
-               bitmap = newmail_bitmap;
+               pixmap = newmail_pixmap[prefs_common.work_offline];
+               bitmap = newmail_bitmap[prefs_common.work_offline];
                break;
        case TRAYICON_NEWMARKED:
-               pixmap = newmarkedmail_pixmap;
-               bitmap = newmarkedmail_bitmap;
+               pixmap = newmarkedmail_pixmap[prefs_common.work_offline];
+               bitmap = newmarkedmail_bitmap[prefs_common.work_offline];
                break;
        case TRAYICON_UNREAD:
-               pixmap = unreadmail_pixmap;
-               bitmap = unreadmail_bitmap;
+               pixmap = unreadmail_pixmap[prefs_common.work_offline];
+               bitmap = unreadmail_bitmap[prefs_common.work_offline];
                break;
        case TRAYICON_UNREADMARKED:
-               pixmap = unreadmarkedmail_pixmap;
-               bitmap = unreadmarkedmail_bitmap;
+               pixmap = unreadmarkedmail_pixmap[prefs_common.work_offline];
+               bitmap = unreadmarkedmail_bitmap[prefs_common.work_offline];
                break;
        default:
-               pixmap = nomail_pixmap;
-               bitmap = nomail_bitmap;
+               pixmap = nomail_pixmap[prefs_common.work_offline];
+               bitmap = nomail_bitmap[prefs_common.work_offline];
                break;
        }
 
@@ -138,13 +186,19 @@ static void set_trayicon_pixmap(TrayIconType icontype)
        last_pixmap = pixmap;
 }
 
-static void update(void)
+static void update(FolderItem *removed_item)
 {
        guint new, unread, unreadmarked, marked, total;
        gchar *buf;
        TrayIconType icontype = TRAYICON_NOTHING;
 
        folder_count_total_msgs(&new, &unread, &unreadmarked, &marked, &total);
+       if (removed_item) {
+               total -= removed_item->total_msgs;
+               new -= removed_item->new_msgs;
+               unread -= removed_item->unread_msgs;
+       }
+
        buf = g_strdup_printf(_("New %d, Unread: %d, Total: %d"), new, unread, total);
 
         gtk_tooltips_set_tip(tooltips, eventbox, buf, "");
@@ -165,15 +219,33 @@ static void update(void)
 
 static gboolean folder_item_update_hook(gpointer source, gpointer data)
 {
-       update();
+       update(NULL);
+
+       return FALSE;
+}
+
+static gboolean folder_update_hook(gpointer source, gpointer data)
+{
+       FolderUpdateData *hookdata;
+       hookdata = source;
+       if (hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM)
+               update(hookdata->item);
+       else
+               update(NULL);
+
+       return FALSE;
+}
 
+static gboolean offline_update_hook(gpointer source, gpointer data)
+{
+       update(NULL);
        return FALSE;
 }
 
 static void resize_cb(GtkWidget *widget, GtkRequisition *req,
                      gpointer user_data)
 {
-       update();
+       update(NULL);
 }
 
 static gboolean click_cb(GtkWidget * widget,
@@ -229,22 +301,29 @@ static void create_trayicon()
 {
        gint n_entries = 0;
 
-       trayicon = egg_tray_icon_new("Sylpheed-Claws");
+       trayicon = egg_tray_icon_new("Claws Mail");
        gtk_widget_realize(GTK_WIDGET(trayicon));
        gtk_window_set_default_size(GTK_WINDOW(trayicon), 16, 16);
        gtk_container_set_border_width(GTK_CONTAINER(trayicon), 0);
 
-       PIXMAP_CREATE(GTK_WIDGET(trayicon), nomail_pixmap, nomail_bitmap, nomail_xpm);
-       PIXMAP_CREATE(GTK_WIDGET(trayicon), unreadmail_pixmap, unreadmail_bitmap, unreadmail_xpm);
-       PIXMAP_CREATE(GTK_WIDGET(trayicon), newmail_pixmap, newmail_bitmap, newmail_xpm);
-       PIXMAP_CREATE(GTK_WIDGET(trayicon), unreadmarkedmail_pixmap, unreadmarkedmail_bitmap, unreadmarkedmail_xpm);
-       PIXMAP_CREATE(GTK_WIDGET(trayicon), newmarkedmail_pixmap, newmarkedmail_bitmap, newmarkedmail_xpm);
+       PIXMAP_CREATE(GTK_WIDGET(trayicon), nomail_pixmap[0], nomail_bitmap[0], nomail_xpm);
+       PIXMAP_CREATE(GTK_WIDGET(trayicon), unreadmail_pixmap[0], unreadmail_bitmap[0], unreadmail_xpm);
+       PIXMAP_CREATE(GTK_WIDGET(trayicon), newmail_pixmap[0], newmail_bitmap[0], newmail_xpm);
+       PIXMAP_CREATE(GTK_WIDGET(trayicon), unreadmarkedmail_pixmap[0], unreadmarkedmail_bitmap[0], unreadmarkedmail_xpm);
+       PIXMAP_CREATE(GTK_WIDGET(trayicon), newmarkedmail_pixmap[0], newmarkedmail_bitmap[0], newmarkedmail_xpm);
+
+       PIXMAP_CREATE(GTK_WIDGET(trayicon), nomail_pixmap[1], nomail_bitmap[1], nomail_offline_xpm);
+       PIXMAP_CREATE(GTK_WIDGET(trayicon), unreadmail_pixmap[1], unreadmail_bitmap[1], unreadmail_offline_xpm);
+       PIXMAP_CREATE(GTK_WIDGET(trayicon), newmail_pixmap[1], newmail_bitmap[1], newmail_offline_xpm);
+       PIXMAP_CREATE(GTK_WIDGET(trayicon), unreadmarkedmail_pixmap[1], unreadmarkedmail_bitmap[1], unreadmarkedmail_offline_xpm);
+       PIXMAP_CREATE(GTK_WIDGET(trayicon), newmarkedmail_pixmap[1], newmarkedmail_bitmap[1], newmarkedmail_offline_xpm);
+
 
        eventbox = gtk_event_box_new();
        gtk_container_set_border_width(GTK_CONTAINER(eventbox), 0);
        gtk_container_add(GTK_CONTAINER(trayicon), GTK_WIDGET(eventbox));
 
-       image = gtk_image_new_from_pixmap(nomail_pixmap, nomail_bitmap);
+       image = gtk_image_new_from_pixmap(nomail_pixmap[0], nomail_bitmap[0]);
        gtk_container_add(GTK_CONTAINER(eventbox), image);
 
        destroy_signal_id =
@@ -267,41 +346,64 @@ static void create_trayicon()
 
        gtk_widget_show_all(GTK_WIDGET(trayicon));
 
-       update();
+       update(NULL);
 }
 
 int plugin_init(gchar **error)
 {
        if ((sylpheed_get_version() > VERSION_NUMERIC)) {
-               *error = g_strdup(_("Your version of Sylpheed-Claws is newer than the version the Trayicon plugin was built with"));
+               *error = g_strdup(_("Your version of Claws Mail is newer than the version the Trayicon plugin was built with"));
                return -1;
        }
 
        if ((sylpheed_get_version() < MAKE_NUMERIC_VERSION(0, 9, 3, 86))) {
-               *error = g_strdup(_("Your version of Sylpheed-Claws is too old for the Trayicon plugin"));
+               *error = g_strdup(_("Your version of Claws Mail is too old for the Trayicon plugin"));
                return -1;
        }
 
-       hook_id = hooks_register_hook (FOLDER_ITEM_UPDATE_HOOKLIST, folder_item_update_hook, NULL);
-       if (hook_id == -1) {
+       item_hook_id = hooks_register_hook (FOLDER_ITEM_UPDATE_HOOKLIST, folder_item_update_hook, NULL);
+       if (item_hook_id == -1) {
                *error = g_strdup(_("Failed to register folder item update hook"));
                return -1;
        }
 
+       folder_hook_id = hooks_register_hook (FOLDER_UPDATE_HOOKLIST, folder_update_hook, NULL);
+       if (folder_hook_id == -1) {
+               *error = g_strdup(_("Failed to register folder update hook"));
+               return -1;
+       }
+
+       offline_hook_id = hooks_register_hook (OFFLINE_SWITCH_HOOKLIST, offline_update_hook, NULL);
+       if (offline_hook_id == -1) {
+               *error = g_strdup(_("Failed to register offline switch hook"));
+               return -1;
+       }
+
+       account_hook_id = hooks_register_hook (ACCOUNT_LIST_CHANGED_HOOKLIST, trayicon_set_accounts_hook, NULL);
+       if (offline_hook_id == -1) {
+               *error = g_strdup(_("Failed to register offline switch hook"));
+               return -1;
+       }
+
        create_trayicon();
+       trayicon_set_accounts_hook(NULL, NULL);
 
        return 0;
 }
 
 void plugin_done(void)
 {
+       hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, item_hook_id);
+       hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, folder_hook_id);
+       hooks_unregister_hook(OFFLINE_SWITCH_HOOKLIST, offline_hook_id);
+       hooks_unregister_hook(ACCOUNT_LIST_CHANGED_HOOKLIST, account_hook_id);
+
        if (sylpheed_is_exiting())
                return;
 
        g_signal_handler_disconnect(G_OBJECT(trayicon), destroy_signal_id);
        
        gtk_widget_destroy(GTK_WIDGET(trayicon));
-       hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, hook_id);
 
        while (gtk_events_pending()) {
                gtk_main_iteration();
@@ -352,6 +454,11 @@ static void trayicon_compose_cb( gpointer data, guint action, GtkWidget *widget
        compose_mail_cb(mainwin, 0, NULL);
 }
 
+static void trayicon_compose_acc_cb( GtkMenuItem *menuitem, gpointer data )
+{
+       compose_new((PrefsAccount *)data, NULL, NULL);
+}
+
 static void trayicon_addressbook_cb( gpointer data, guint action, GtkWidget *widget )
 {
        addressbook_open(NULL);
@@ -362,7 +469,7 @@ static void trayicon_toggle_offline_cb( gpointer data, guint action, GtkWidget *
        /* toggle offline mode if menu checkitem has been clicked */
        if (!updating_menu) {
                MainWindow *mainwin = mainwindow_get_mainwindow();
-               main_window_toggle_work_offline(mainwin, !prefs_common.work_offline, FALSE);
+               main_window_toggle_work_offline(mainwin, !prefs_common.work_offline, TRUE);
        }
 }
 
@@ -388,3 +495,11 @@ static void trayicon_exit_cb( gpointer data, guint action, GtkWidget *widget )
                app_exit_cb(mainwin, 0, NULL);
        }
 }
+
+struct PluginFeature *plugin_provides(void)
+{
+       static struct PluginFeature features[] = 
+               { {PLUGIN_NOTIFIER, N_("Trayicon")},
+                 {PLUGIN_NOTHING, NULL}};
+       return features;
+}