2011-10-07 [colin] 3.7.10cvs26
[claws.git] / src / prefs_filtering_action.c
index 5a97d5617272fa9d071419a589ea8cbd15886eeb..4a45d47af261e561118aa4e6bd28ef5b8e12d11f 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2007 Hiroyuki Yamamoto
+ * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 2003-2011 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
@@ -32,6 +32,9 @@
 #include <string.h>
 #include <errno.h>
 
+#if !GTK_CHECK_VERSION(3, 0, 0)
+#include "gtkcmoptionmenu.h"
+#endif
 #include "main.h"
 #include "prefs_gtk.h"
 #include "prefs_filtering_action.h"
@@ -92,7 +95,8 @@ static gboolean prefs_filtering_actions_selected              (GtkTreeSelection *selector,
                                                                 gboolean currently_selected,
                                                                 gpointer data);
 
-static void prefs_filtering_action_exec_info(void);
+static void prefs_filtering_action_exec_info                   (GtkWidget *widget,
+                                                                GtkWidget *parent);
 
 /*!
  *\brief       UI data for matcher dialog
@@ -111,7 +115,9 @@ static struct FilteringAction_ {
        GtkWidget *dest_entry;
        GtkWidget *dest_btn;
        GtkWidget *exec_btn;
+#if !GTK_CHECK_VERSION(3, 0, 0)
        GtkWidget *color_optmenu;
+#endif
        GtkWidget *header_combo;
        GtkWidget *header_entry;
        GtkWidget *addressbook_btn;
@@ -170,7 +176,9 @@ static struct {
        { N_("Message flags"),  N_("Mark as spam"),             ACTION_MARK_AS_SPAM },
        { N_("Message flags"),  N_("Mark as ham"),              ACTION_MARK_AS_HAM },
        { NULL,                 N_("Execute"),                  ACTION_EXECUTE },
-       { NULL,                 N_("Color"),                    ACTION_COLOR },
+#if !GTK_CHECK_VERSION(3, 0, 0)
+       { NULL,                 N_("Color label"),              ACTION_COLOR },
+#endif
        { N_("Resend"),         N_("Forward"),                  ACTION_FORWARD },
        { N_("Resend"),         N_("Forward as attachment"),    ACTION_FORWARD_AS_ATTACHMENT },
        { N_("Resend"),         N_("Redirect"),                 ACTION_REDIRECT },
@@ -189,6 +197,7 @@ static struct {
 enum {
        ACTION_COMBO_TEXT,
        ACTION_COMBO_DATA,
+       ACTION_COMBO_SENS,
        N_ACTION_COMBO
 };
 
@@ -204,31 +213,36 @@ static GtkTreeModel *prefs_filtering_action_create_model(void)
        gchar *curr_menu = NULL;
        gint i;
        
-       store = gtk_tree_store_new(N_ACTION_COMBO, G_TYPE_STRING, G_TYPE_INT);
+       store = gtk_tree_store_new(N_ACTION_COMBO, G_TYPE_STRING, G_TYPE_INT,
+                                  G_TYPE_BOOLEAN);
        
-       for(i = 0; action_menu[i].menu || action_menu[i].text; i++)
+       for (i = 0; action_menu[i].menu || action_menu[i].text; i++)
        {
-               if(action_menu[i].menu) {
-                       if(!curr_menu || strcmp(action_menu[i].menu, curr_menu)) {
+               if (action_menu[i].menu) {
+                       if (!curr_menu || strcmp(action_menu[i].menu, curr_menu)) {
                                gtk_tree_store_append(store, &iter, NULL);
                                gtk_tree_store_set(store, &iter,
-                                                  ACTION_COMBO_TEXT, action_menu[i].menu,
+                                                  ACTION_COMBO_TEXT,
+                                                  gettext(action_menu[i].menu),
+                                                  ACTION_COMBO_SENS, TRUE,
                                                   -1);
                                curr_menu = action_menu[i].menu;
                        } 
                        
                        gtk_tree_store_append(store, &iter2, &iter);
                        gtk_tree_store_set(store, &iter2,
-                                          ACTION_COMBO_TEXT, action_menu[i].text,
+                                          ACTION_COMBO_TEXT, gettext(action_menu[i].text),
                                           ACTION_COMBO_DATA, action_menu[i].action,
+                                          ACTION_COMBO_SENS, TRUE,
                                           -1);
                } else {
                        curr_menu = NULL;
                        
                        gtk_tree_store_append(store, &iter, NULL);
                        gtk_tree_store_set(store, &iter,
-                                          ACTION_COMBO_TEXT, action_menu[i].text,
+                                          ACTION_COMBO_TEXT, gettext(action_menu[i].text),
                                           ACTION_COMBO_DATA, action_menu[i].action,
+                                          ACTION_COMBO_SENS, TRUE,
                                           -1); 
                }   
        }
@@ -242,8 +256,9 @@ static void cell_is_sensitive(GtkCellLayout   *cell_layout,
                              GtkTreeIter     *iter,
                              gpointer         data)
 {
-       gboolean sensitive = !gtk_tree_model_iter_has_child (tree_model, iter);
-       g_object_set (cell, "sensitive", sensitive, NULL);
+       if(gtk_tree_model_iter_has_child (tree_model, iter)) {
+               g_object_set (cell, "sensitive", FALSE, NULL);
+       }
 }
 
 /*!
@@ -261,9 +276,11 @@ void prefs_filtering_action_open(GSList *action_list,
        if (!filtering_action.window) {
                prefs_filtering_action_create();
        } else {
+#if !GTK_CHECK_VERSION(3, 0, 0)
                /* update color label menu */
-               gtk_option_menu_set_menu(GTK_OPTION_MENU(filtering_action.color_optmenu),
+               gtk_cmoption_menu_set_menu(GTK_CMOPTION_MENU(filtering_action.color_optmenu),
                                colorlabel_create_color_menu());
+#endif
        }
 
        manage_window_set_transient(GTK_WINDOW(filtering_action.window));
@@ -282,7 +299,7 @@ void prefs_filtering_action_open(GSList *action_list,
 static void prefs_filtering_action_size_allocate_cb(GtkWidget *widget,
                                         GtkAllocation *allocation)
 {
-       g_return_if_fail(allocation != NULL);
+       cm_return_if_fail(allocation != NULL);
 
        prefs_common.filteringactionwin_width = allocation->width;
        prefs_common.filteringactionwin_height = allocation->height;
@@ -346,7 +363,9 @@ static void prefs_filtering_action_create(void)
 
        GtkWidget *exec_btn;
 
+#if !GTK_CHECK_VERSION(3, 0, 0)
        GtkWidget *color_optmenu;
+#endif
 
        static GdkGeometry geometry;
 
@@ -377,7 +396,7 @@ static void prefs_filtering_action_create(void)
        gtk_widget_grab_default(ok_btn);
 
        gtk_window_set_title(GTK_WINDOW(window),
-                            _("Filtering action configuration"));
+                            _("Action configuration"));
        g_signal_connect(G_OBJECT(window), "delete_event",
                         G_CALLBACK(prefs_filtering_action_deleted), NULL);
        g_signal_connect(G_OBJECT(window), "size_allocate",
@@ -440,6 +459,7 @@ static void prefs_filtering_action_create(void)
        gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(action_combo), renderer, TRUE);
         gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(action_combo), renderer,
                                       "text", ACTION_COMBO_TEXT,
+                                      "sensitive", ACTION_COMBO_SENS,
                                       NULL);
        gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(action_combo), renderer,
                                           cell_is_sensitive, NULL, NULL);
@@ -479,7 +499,7 @@ static void prefs_filtering_action_create(void)
        gtk_size_group_add_widget(size_action, header_combo);
        gtk_box_pack_start (GTK_BOX (hbox1), header_combo,
                            FALSE, FALSE, 0);
-       header_entry = GTK_BIN(header_combo)->child;
+       header_entry = gtk_bin_get_child(GTK_BIN((header_combo)));
 
        /* destination */
 
@@ -493,12 +513,14 @@ static void prefs_filtering_action_create(void)
        score_entry = gtk_spin_button_new_with_range(-1000, 1000, 1);
        gtk_box_pack_start(GTK_BOX(hbox1), score_entry, FALSE, FALSE, 0);
        
-       color_optmenu = gtk_option_menu_new();
+#if !GTK_CHECK_VERSION(3, 0, 0)
+       color_optmenu = gtk_cmoption_menu_new();
        gtk_size_group_add_widget(size_action, color_optmenu);
-       gtk_option_menu_set_menu(GTK_OPTION_MENU(color_optmenu),
+       gtk_cmoption_menu_set_menu(GTK_CMOPTION_MENU(color_optmenu),
                                 colorlabel_create_color_menu());
        prefs_filtering_action_check_widget_width(color_optmenu);
        gtk_box_pack_start(GTK_BOX(hbox1), color_optmenu, FALSE, FALSE, 0);
+#endif
 
        tags_combo = gtk_combo_box_new_text ();
        gtk_size_group_add_widget(size_action, tags_combo);
@@ -532,15 +554,11 @@ static void prefs_filtering_action_create(void)
                          G_CALLBACK(prefs_filtering_action_select_addressbook),
                          NULL);
 
-#if GTK_CHECK_VERSION(2, 8, 0)
        exec_btn = gtk_button_new_from_stock(GTK_STOCK_INFO);
-#else
-       exec_btn = gtk_button_new_with_label (_("Info..."));
-#endif
        gtk_box_pack_start (GTK_BOX (hbox1), exec_btn, FALSE, FALSE, 0);
        g_signal_connect (G_OBJECT (exec_btn), "clicked",
                          G_CALLBACK(prefs_filtering_action_exec_info),
-                         NULL);
+                         window);
 
        /* register / substitute / delete */
 
@@ -621,7 +639,9 @@ static void prefs_filtering_action_create(void)
        filtering_action.dest_entry = dest_entry;
        filtering_action.dest_btn = dest_btn;
        filtering_action.exec_btn = exec_btn;
+#if !GTK_CHECK_VERSION(3, 0, 0)
        filtering_action.color_optmenu = color_optmenu;
+#endif
        filtering_action.score_entry = score_entry;
        filtering_action.header_combo = header_combo;
        filtering_action.header_entry = header_entry;
@@ -642,7 +662,7 @@ static void prefs_filtering_action_create(void)
 static void prefs_filtering_action_list_view_set_row(GtkTreeIter *row, 
                                                     FilteringAction *action)
 {
-        gchar buf[256];
+        gchar *buf;
 
        if (row == NULL && action == NULL) {
                prefs_filtering_action_list_view_insert_action
@@ -651,11 +671,12 @@ static void prefs_filtering_action_list_view_set_row(GtkTreeIter *row,
                return;
        }                        
 
-        filteringaction_to_string(buf, sizeof buf, action);
+        buf = filteringaction_to_string(action);
 
        prefs_filtering_action_list_view_insert_action
                        (filtering_action.action_list_view,
                         row, buf, TRUE);
+       g_free(buf);
 }
 
 /*!
@@ -681,6 +702,9 @@ static void prefs_filtering_action_set_dialog(GSList *action_list)
        }
        
         prefs_filtering_action_reset_dialog();
+        
+        combobox_set_sensitive(GTK_COMBO_BOX(filtering_action.action_combo), 9,
+                       (tags_get_size() > 0) ? TRUE : FALSE);
 }
 
 /*!
@@ -816,8 +840,10 @@ static gint prefs_filtering_action_get_matching_from_action(Action action_id)
                return MATCHACTION_REDIRECT;
        case ACTION_EXECUTE:
                return MATCHACTION_EXECUTE;
+#if !GTK_CHECK_VERSION(3, 0, 0)
        case ACTION_COLOR:
                return MATCHACTION_COLOR;
+#endif
        case ACTION_HIDE:
                return MATCHACTION_HIDE;
        case ACTION_IGNORE:
@@ -872,7 +898,7 @@ static FilteringAction * prefs_filtering_action_dialog_to_action(gboolean alert)
                if (*destination == '\0') {
                        if (alert)
                                 alertpanel_error(action_id == ACTION_EXECUTE 
-                                                ? _("Command line not set")
+                                                ? _("Command-line not set")
                                                 : _("Destination is not set."));
                        g_free(destination);
                        return NULL;
@@ -889,11 +915,13 @@ static FilteringAction * prefs_filtering_action_dialog_to_action(gboolean alert)
                        return NULL;
                }
                break;
+#if !GTK_CHECK_VERSION(3, 0, 0)
        case ACTION_COLOR:
                labelcolor = colorlabel_get_color_menu_active_item(
-                       gtk_option_menu_get_menu(GTK_OPTION_MENU(filtering_action.color_optmenu)));
+                       gtk_cmoption_menu_get_menu(GTK_CMOPTION_MENU(filtering_action.color_optmenu)));
                destination = NULL;     
                break;
+#endif
         case ACTION_CHANGE_SCORE:
         case ACTION_SET_SCORE:
                score = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(
@@ -1119,7 +1147,7 @@ static void prefs_filtering_action_down(void)
 static gboolean prefs_filtering_action_key_pressed(GtkWidget *widget,
     GdkEventKey *event, gpointer data)
 {
-       if (event && event->keyval == GDK_Escape) {
+       if (event && event->keyval == GDK_KEY_Escape) {
                prefs_filtering_action_cancel();
                return TRUE;            
        }
@@ -1215,8 +1243,9 @@ static DescriptionWindow exec_desc_win = {
 /*!
  *\brief       Show Execute action's info
  */
-static void prefs_filtering_action_exec_info(void)
+static void prefs_filtering_action_exec_info(GtkWidget *widget, GtkWidget *parent)
 {
+       exec_desc_win.parent = parent;
        description_window_create(&exec_desc_win);
 }
 
@@ -1225,7 +1254,7 @@ static void prefs_filtering_action_select_dest(void)
        FolderItem *dest;
        gchar * path;
 
-       dest = foldersel_folder_sel(NULL, FOLDER_SEL_COPY, NULL);
+       dest = foldersel_folder_sel(NULL, FOLDER_SEL_COPY, NULL, FALSE);
        if (!dest) return;
 
        path = folder_item_get_identifier(dest);
@@ -1249,18 +1278,22 @@ static void prefs_filtering_action_select_addressbook(void)
 
 static void prefs_filtering_action_enable_widget(GtkWidget* widget, const gboolean enable)
 {
-       g_return_if_fail(widget != NULL);
+       cm_return_if_fail(widget != NULL);
 
        if(enable == TRUE)
        {
+#if !GTK_CHECK_VERSION(2,24,0)
                if(GTK_IS_COMBO_BOX(widget) || GTK_IS_COMBO_BOX_ENTRY(widget))
+#else
+               if(GTK_IS_COMBO_BOX(widget))
+#endif
                        gtk_combo_box_set_active(GTK_COMBO_BOX(widget), 0);
                else if(GTK_IS_SPIN_BUTTON(widget))
                        gtk_spin_button_set_value(GTK_SPIN_BUTTON(widget), 0);
                else if(GTK_IS_ENTRY(widget)) {
                        gtk_entry_set_text(GTK_ENTRY(widget), "");
-               } else if(GTK_IS_OPTION_MENU(widget))
-                       gtk_option_menu_set_history(GTK_OPTION_MENU(widget), 0);
+               } else if(GTK_IS_CMOPTION_MENU(widget))
+                       gtk_cmoption_menu_set_history(GTK_CMOPTION_MENU(widget), 0);
                
                gtk_widget_set_sensitive(widget, TRUE);
                gtk_widget_show(widget);
@@ -1324,8 +1357,10 @@ static void prefs_filtering_action_type_selection_changed(GtkWidget *combo,
        prefs_filtering_action_enable_widget(filtering_action.exec_btn,
                                                (value == ACTION_EXECUTE));
 
+#if !GTK_CHECK_VERSION(3, 0, 0)
        prefs_filtering_action_enable_widget(filtering_action.color_optmenu,
                                                (value == ACTION_COLOR));
+#endif
 
        prefs_filtering_action_enable_widget(filtering_action.header_combo,
                                        (value == ACTION_ADD_TO_ADDRESSBOOK));  
@@ -1351,9 +1386,11 @@ static void prefs_filtering_action_type_selection_changed(GtkWidget *combo,
        case ACTION_MOVE:
                gtk_label_set_text(GTK_LABEL(filtering_action.label3), _("Destination"));
                break;
+#if !GTK_CHECK_VERSION(3, 0, 0)
        case ACTION_COLOR:
                gtk_label_set_text(GTK_LABEL(filtering_action.label3), _("Color"));
                break;
+#endif
        case ACTION_EXECUTE:
                gtk_label_set_text(GTK_LABEL(filtering_action.label3), _("Execute"));
                break;
@@ -1417,6 +1454,7 @@ static GtkWidget *prefs_filtering_action_list_view_create(void)
        g_object_unref(model);  
        
        gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
+       gtk_tree_view_set_reorderable(list_view, TRUE);
 
        selector = gtk_tree_view_get_selection(list_view);
        gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
@@ -1456,6 +1494,7 @@ static gboolean prefs_filtering_actions_selected
        gint list_id;
        GtkTreeIter iter;
        gboolean is_valid;
+       GtkWidget *menu;
 
        if (currently_selected)
                return TRUE;
@@ -1485,11 +1524,6 @@ static gboolean prefs_filtering_actions_selected
         action = action_list->data;
         g_slist_free(action_list);
 
-       if (action->destination)
-               gtk_entry_set_text(GTK_ENTRY(filtering_action.dest_entry), action->destination);
-       else
-               gtk_entry_set_text(GTK_ENTRY(filtering_action.dest_entry), "");
-
        switch(action->type) {
        case MATCHACTION_MOVE:
                combobox_select_by_data(GTK_COMBO_BOX(filtering_action.action_combo),
@@ -1560,12 +1594,17 @@ static gboolean prefs_filtering_actions_selected
                combobox_select_by_data(GTK_COMBO_BOX(filtering_action.action_combo),
                                     ACTION_EXECUTE);
                break;
+#if !GTK_CHECK_VERSION(3, 0, 0)
        case MATCHACTION_COLOR:
                combobox_select_by_data(GTK_COMBO_BOX(filtering_action.action_combo),
                                     ACTION_COLOR);
-               gtk_option_menu_set_history(GTK_OPTION_MENU(filtering_action.color_optmenu),
-                                           action->labelcolor);     
+               gtk_cmoption_menu_set_history(GTK_CMOPTION_MENU(filtering_action.color_optmenu),
+                                           action->labelcolor + 1);
+               menu = gtk_cmoption_menu_get_menu(GTK_CMOPTION_MENU(
+                                               filtering_action.color_optmenu));
+               g_signal_emit_by_name(G_OBJECT(menu), "selection-done", menu);
                break;
+#endif
        case MATCHACTION_CHANGE_SCORE:
                combobox_select_by_data(GTK_COMBO_BOX(filtering_action.action_combo),
                                     ACTION_CHANGE_SCORE);
@@ -1601,7 +1640,30 @@ static gboolean prefs_filtering_actions_selected
                        gtk_entry_set_text(GTK_ENTRY(filtering_action.header_entry), "");
                combobox_select_by_data(GTK_COMBO_BOX(filtering_action.action_combo),
                                     ACTION_ADD_TO_ADDRESSBOOK);
+               break;
+       case MATCHACTION_SET_TAG:
+               combobox_select_by_data(GTK_COMBO_BOX(filtering_action.action_combo),
+                                    ACTION_SET_TAG);
+               combobox_select_by_text(GTK_COMBO_BOX(filtering_action.tags_combo),
+                                       action->destination);
+               break;
+       case MATCHACTION_UNSET_TAG:
+               combobox_select_by_data(GTK_COMBO_BOX(filtering_action.action_combo),
+                                    ACTION_UNSET_TAG);
+               combobox_select_by_text(GTK_COMBO_BOX(filtering_action.tags_combo),
+                                       action->destination);
+               break;
+       case MATCHACTION_CLEAR_TAGS:
+               combobox_select_by_data(GTK_COMBO_BOX(filtering_action.action_combo),
+                                    ACTION_CLEAR_TAGS);
+               break;
+       default:
+               g_warning("unhandled case !\n");
        }
+       if (action->destination)
+               gtk_entry_set_text(GTK_ENTRY(filtering_action.dest_entry), action->destination);
+       else
+               gtk_entry_set_text(GTK_ENTRY(filtering_action.dest_entry), "");
 
        filteringaction_free(action); /* XXX: memleak */
        return TRUE;