2007-08-29 [colin] 2.10.0cvs183
[claws.git] / src / plugins / trayicon / trayicon.c
index 20b3ced2a5122bc973286d5e04360dce161de65d..c2cd4e99c13cab904914f2141581191616adeab1 100644 (file)
@@ -1,10 +1,10 @@
 /*
- * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws Team
+ * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 2003-2007 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
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
@@ -13,8 +13,8 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
  */
 
 #ifdef HAVE_CONFIG_H
@@ -27,7 +27,7 @@
 #include <glib/gi18n.h>
 #include <gtk/gtk.h>
 
-#include "common/sylpheed.h"
+#include "common/claws.h"
 #include "common/version.h"
 #include "plugin.h"
 #include "utils.h"
 #include "prefs_common.h"
 #include "main.h"
 #include "alertpanel.h"
+#include "account.h"
 #include "gtk/manage_window.h"
 
 #include "eggtrayicon.h"
 
+#include "trayicon_prefs.h"
+
 #include "newmarkedmail.xpm"
 #include "unreadmarkedmail.xpm"
 #include "newmail.xpm"
 #include "unreadmail.offline.xpm"
 #include "nomail.offline.xpm"
 
+#define PLUGIN_NAME (_("Trayicon"))
+
 static guint item_hook_id;
 static guint folder_hook_id;
 static guint offline_hook_id;
+static guint account_hook_id;
+static guint close_hook_id;
+static guint iconified_hook_id;
 
 static GdkPixmap *newmail_pixmap[2];
 static GdkPixmap *newmail_bitmap[2];
@@ -91,6 +99,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);
@@ -99,15 +108,52 @@ static void resize_cb                  (GtkWidget *widget, GtkRequisition *req, gpointer use
 static GtkItemFactoryEntry trayicon_popup_menu_entries[] =
 {
        {N_("/_Get Mail"),              NULL, trayicon_get_all_cb,              0, NULL},
-       {N_("/---"),                    NULL, NULL,                             0, "<Separator>"},
+       {"/---",                        NULL, NULL,                             0, "<Separator>"},
        {N_("/_Email"),                 NULL, trayicon_compose_cb,              0, NULL},
+       {N_("/_Email from account"),    NULL, NULL,                             0, "<Branch>"},
+       {"/---",                        NULL, NULL,                             0, "<Separator>"},
        {N_("/Open A_ddressbook"),      NULL, trayicon_addressbook_cb,          0, NULL},
-       {N_("/---"),                    NULL, NULL,                             0, "<Separator>"},
+       {"/---",                        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}
+       {"/---",                        NULL, NULL,                             0, "<Separator>"},
+       {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;
@@ -202,6 +248,34 @@ static gboolean offline_update_hook(gpointer source, gpointer data)
        return FALSE;
 }
 
+static gboolean trayicon_close_hook(gpointer source, gpointer data)
+{
+       if (source) {
+               gboolean *close_allowed = (gboolean*)source;
+
+               if (trayicon_prefs.close_to_tray) {
+               MainWindow *mainwin = mainwindow_get_mainwindow();
+
+                       *close_allowed = FALSE;
+               if (GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window)))
+                       main_window_hide(mainwin);
+       }
+       }
+       return FALSE;
+}
+
+static gboolean trayicon_got_iconified_hook(gpointer source, gpointer data)
+{
+       MainWindow *mainwin = mainwindow_get_mainwindow();
+
+       if (trayicon_prefs.hide_when_iconified
+                       && GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window))
+                       && !gtk_window_get_skip_taskbar_hint(GTK_WINDOW(mainwin->window))) {
+               gtk_window_set_skip_taskbar_hint(GTK_WINDOW(mainwin->window), TRUE);
+       }
+       return FALSE;
+}
+
 static void resize_cb(GtkWidget *widget, GtkRequisition *req,
                      gpointer user_data)
 {
@@ -222,9 +296,20 @@ static gboolean click_cb(GtkWidget * widget,
        switch (event->button) {
        case 1:
                if (GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window))) {
-                       main_window_hide(mainwin);
+                       if ((gdk_window_get_state(GTK_WIDGET(mainwin->window)->window)&GDK_WINDOW_STATE_ICONIFIED)
+                                       || mainwindow_is_obscured()) {
+                               gtk_window_deiconify(GTK_WINDOW(mainwin->window));
+                               gtk_window_set_skip_taskbar_hint(GTK_WINDOW(mainwin->window), FALSE);
+                               main_window_show(mainwin);
+                               gtk_window_present(GTK_WINDOW(mainwin->window));
+                       } else {
+                               main_window_hide(mainwin);
+                       }
                } else {
+                       gtk_window_deiconify(GTK_WINDOW(mainwin->window));
+                       gtk_window_set_skip_taskbar_hint(GTK_WINDOW(mainwin->window), FALSE);
                        main_window_show(mainwin);
+                       gtk_window_present(GTK_WINDOW(mainwin->window));
         }
                break;
        case 3:
@@ -233,10 +318,10 @@ static gboolean click_cb(GtkWidget * widget,
                /* initialize checkitem according to current offline state */
                gtk_check_menu_item_set_active(
                        GTK_CHECK_MENU_ITEM(gtk_item_factory_get_item(traymenu_factory,
-                       _("/Work Offline"))), prefs_common.work_offline);
+                       "/Work Offline")), prefs_common.work_offline);
                gtk_widget_set_sensitive(
                        GTK_WIDGET(gtk_item_factory_get_item(traymenu_factory,
-                       _("/Get Mail"))), mainwin->lock_count == 0);
+                       "/Get Mail")), mainwin->lock_count == 0);
                updating_menu = FALSE;
 
                gtk_menu_popup( GTK_MENU(traymenu_popup), NULL, NULL, NULL, NULL,
@@ -261,7 +346,7 @@ 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);
@@ -295,7 +380,6 @@ static void create_trayicon()
                G_CALLBACK(click_cb), NULL);
 
        tooltips = gtk_tooltips_new();
-       gtk_tooltips_set_delay(tooltips, 1000);
        gtk_tooltips_enable(tooltips);
 
        n_entries = sizeof(trayicon_popup_menu_entries) /
@@ -311,47 +395,88 @@ static void create_trayicon()
 
 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"));
+       if (!check_plugin_version(MAKE_NUMERIC_VERSION(2,9,2,72),
+                               VERSION_NUMERIC, PLUGIN_NAME, error))
                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"));
-               return -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;
+               goto err_out_item;
        }
 
        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;
+               goto err_out_folder;
        }
 
        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;
+               goto err_out_offline;
+       }
+
+       account_hook_id = hooks_register_hook (ACCOUNT_LIST_CHANGED_HOOKLIST, trayicon_set_accounts_hook, NULL);
+       if (account_hook_id == -1) {
+               *error = g_strdup(_("Failed to register account list changed hook"));
+               goto err_out_account;
+       }
+
+       close_hook_id = hooks_register_hook (MAIN_WINDOW_CLOSE, trayicon_close_hook, NULL);
+       if (close_hook_id == -1) {
+               *error = g_strdup(_("Failed to register close hook"));
+               goto err_out_close;
+       }
+
+       iconified_hook_id = hooks_register_hook (MAIN_WINDOW_GOT_ICONIFIED, trayicon_got_iconified_hook, NULL);
+       if (iconified_hook_id == -1) {
+               *error = g_strdup(_("Failed to register got iconified hook"));
+               goto err_out_iconified;
        }
 
        create_trayicon();
+       trayicon_set_accounts_hook(NULL, NULL);
+
+       trayicon_prefs_init();
+
+       if (trayicon_prefs.hide_at_startup && claws_is_starting()) {
+               MainWindow *mainwin = mainwindow_get_mainwindow();
+
+               if (GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window)))
+                       main_window_hide(mainwin);
+               main_set_show_at_startup(FALSE);
+       }
 
        return 0;
+
+err_out_iconified:
+       hooks_unregister_hook(MAIN_WINDOW_CLOSE, close_hook_id);
+err_out_close:
+       hooks_unregister_hook(ACCOUNT_LIST_CHANGED_HOOKLIST, account_hook_id);
+err_out_account:
+       hooks_unregister_hook(OFFLINE_SWITCH_HOOKLIST, offline_hook_id);
+err_out_offline:
+       hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, folder_hook_id);
+err_out_folder:
+       hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, item_hook_id);
+err_out_item:
+       return -1;
 }
 
-void plugin_done(void)
+gboolean plugin_done(void)
 {
+       trayicon_prefs_done();
+
        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);
+       hooks_unregister_hook(MAIN_WINDOW_CLOSE, close_hook_id);
+       hooks_unregister_hook(MAIN_WINDOW_GOT_ICONIFIED, iconified_hook_id);
 
-       if (sylpheed_is_exiting())
-               return;
+       if (claws_is_exiting())
+               return TRUE;
 
        g_signal_handler_disconnect(G_OBJECT(trayicon), destroy_signal_id);
        
@@ -360,11 +485,12 @@ void plugin_done(void)
        while (gtk_events_pending()) {
                gtk_main_iteration();
        }
+       return TRUE;
 }
 
 const gchar *plugin_name(void)
 {
-       return _("Trayicon");
+       return PLUGIN_NAME;
 }
 
 const gchar *plugin_desc(void)
@@ -384,7 +510,7 @@ const gchar *plugin_type(void)
 
 const gchar *plugin_licence(void)
 {
-       return "GPL";
+       return "GPL3+";
 }
 
 const gchar *plugin_version(void)
@@ -406,6 +532,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);
@@ -423,7 +554,7 @@ static void trayicon_toggle_offline_cb( gpointer data, guint action, GtkWidget *
 static void app_exit_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
 {
        if (prefs_common.confirm_on_exit) {
-               if (alertpanel(_("Exit"), _("Exit this program?"),
+               if (alertpanel(_("Exit"), _("Exit Claws Mail?"),
                               GTK_STOCK_CANCEL, GTK_STOCK_OK,
                               NULL) != G_ALERTALTERNATE) {
                        return;