uninitialized filter list when deleting a folder; debug_print some warning if destfol...
[claws.git] / src / prefs_filtering.c
index 900076f2b6a9eaea462f5fd2e0534f2fc14d0b18..9a9225085d2fedc36fa018638c6551e4e3b49b39 100644 (file)
@@ -52,6 +52,7 @@
 
 #include "matcher_parser.h"
 #include "matcher.h"
+#include "prefs_filtering_action.h"
 
 static struct Filtering {
        GtkWidget *window;
@@ -63,6 +64,8 @@ static struct Filtering {
        GtkWidget *cond_clist;
 } filtering;
 
+static GSList ** p_processing_list = NULL;
+
 /* widget creating functions */
 static void prefs_filtering_create             (void);
 
@@ -74,8 +77,10 @@ static void prefs_filtering_set_list (void);
 static void prefs_filtering_register_cb        (void);
 static void prefs_filtering_substitute_cb      (void);
 static void prefs_filtering_delete_cb  (void);
+static void prefs_filtering_top                (void);
 static void prefs_filtering_up         (void);
 static void prefs_filtering_down       (void);
+static void prefs_filtering_bottom     (void);
 static void prefs_filtering_select     (GtkCList       *clist,
                                         gint            row,
                                         gint            column,
@@ -84,7 +89,7 @@ static void prefs_filtering_select    (GtkCList       *clist,
 static gint prefs_filtering_deleted    (GtkWidget      *widget,
                                         GdkEventAny    *event,
                                         gpointer        data);
-static void prefs_filtering_key_pressed        (GtkWidget      *widget,
+static gboolean prefs_filtering_key_pressed(GtkWidget  *widget,
                                         GdkEventKey    *event,
                                         gpointer        data);
 static void prefs_filtering_cancel     (void);
@@ -93,160 +98,18 @@ static void prefs_filtering_ok             (void);
 static void prefs_filtering_condition_define   (void);
 static void prefs_filtering_action_define(void);
 static gint prefs_filtering_clist_set_row      (gint row, FilteringProp * prop);
-static void prefs_filtering_select_dest                (void);
-static void prefs_filtering_action_select      (GtkList *list,
-                                                GtkWidget *widget, 
-                                                gpointer user_data);
-
-static void prefs_filtering_action_selection_changed(GtkList *list,
-                                                    gpointer user_data);
                                          
 static void prefs_filtering_reset_dialog       (void);
 static gboolean prefs_filtering_rename_path_func(GNode *node, gpointer data);
 static gboolean prefs_filtering_delete_path_func(GNode *node, gpointer data);
 
-static FolderItem * cur_item = NULL; /* folder (if dialog opened for processing) */
-
-typedef enum Action_ {
-       ACTION_MOVE,
-       ACTION_COPY,
-       ACTION_DELETE,
-       ACTION_MARK,
-       ACTION_UNMARK,
-       ACTION_LOCK,
-       ACTION_UNLOCK,
-       ACTION_MARK_AS_READ,
-       ACTION_MARK_AS_UNREAD,
-       ACTION_FORWARD,
-       ACTION_FORWARD_AS_ATTACHMENT,
-       ACTION_REDIRECT,
-       ACTION_EXECUTE,
-       ACTION_COLOR,
-       /* add other action constants */
-} Action;
-
-static struct {
-       gchar *text;
-       Action action;
-} action_text [] = {
-       { N_("Move"),                   ACTION_MOVE     },      
-       { N_("Copy"),                   ACTION_COPY     },
-       { N_("Delete"),                 ACTION_DELETE   },
-       { N_("Mark"),                   ACTION_MARK     },
-       { N_("Unmark"),                 ACTION_UNMARK   },
-       { N_("Lock"),                   ACTION_LOCK     },
-       { N_("Unlock"),                 ACTION_UNLOCK   },
-       { N_("Mark as read"),           ACTION_MARK_AS_READ },
-       { N_("Mark as unread"),         ACTION_MARK_AS_UNREAD },
-       { N_("Forward"),                ACTION_FORWARD  },
-       { N_("Forward as attachment"),  ACTION_FORWARD_AS_ATTACHMENT },
-       { N_("Redirect"),               ACTION_REDIRECT },
-       { N_("Execute"),                ACTION_EXECUTE  },
-       { N_("Color"),                  ACTION_COLOR    }
-};
-
-static const gchar *get_action_text(Action action)
-{
-       int n;
-       for (n = 0; n < sizeof action_text / sizeof action_text[0]; n++)
-               if (action_text[n].action == action)
-                       return action_text[n].text;
-       return "";                      
-}
-
-static gint get_sel_from_list(GtkList * list)
-{
-       gint row = 0;
-       void * sel;
-       GList * child;
-
-       if (list->selection == NULL)
-               return -1;
-
-       sel = list->selection->data;
-       for(child = list->children ; child != NULL ;
-           child = g_list_next(child)) {
-               if (child->data == sel)
-                       return row;
-               row ++;
-       }
-       
-       return row;
-}
-
-static gint get_account_id_from_list_id(gint list_id)
-{
-       GList * accounts;
-
-       for (accounts = account_get_list() ; accounts != NULL;
-            accounts = accounts->next) {
-               PrefsAccount *ac = (PrefsAccount *)accounts->data;
-
-               if (list_id == 0)
-                       return ac->account_id;
-               list_id--;
-       }
-       return 0;
-}
-
-static gint get_list_id_from_account_id(gint account_id)
-{
-       GList * accounts;
-       gint list_id = 0;
+static void delete_path(GSList ** p_filters, const gchar * path);
 
-       for (accounts = account_get_list() ; accounts != NULL;
-            accounts = accounts->next) {
-               PrefsAccount *ac = (PrefsAccount *)accounts->data;
-
-               if (account_id == ac->account_id)
-                       return list_id;
-               list_id++;
-       }
-       return 0;
-}
-
-static gint prefs_filtering_get_matching_from_action(Action action_id)
-{
-       switch (action_id) {
-       case ACTION_MOVE:
-               return MATCHACTION_MOVE;
-       case ACTION_COPY:
-               return MATCHACTION_COPY;
-       case ACTION_DELETE:
-               return MATCHACTION_DELETE;
-       case ACTION_MARK:
-               return MATCHACTION_MARK;
-       case ACTION_UNMARK:
-               return MATCHACTION_UNMARK;
-       case ACTION_LOCK:
-               return MATCHACTION_LOCK;
-       case ACTION_UNLOCK:
-               return MATCHACTION_UNLOCK;
-       case ACTION_MARK_AS_READ:
-               return MATCHACTION_MARK_AS_READ;
-       case ACTION_MARK_AS_UNREAD:
-               return MATCHACTION_MARK_AS_UNREAD;
-       case ACTION_FORWARD:
-               return MATCHACTION_FORWARD;
-       case ACTION_FORWARD_AS_ATTACHMENT:
-               return MATCHACTION_FORWARD_AS_ATTACHMENT;
-       case ACTION_REDIRECT:
-               return MATCHACTION_REDIRECT;
-       case ACTION_EXECUTE:
-               return MATCHACTION_EXECUTE;
-       case ACTION_COLOR:
-               return MATCHACTION_COLOR;
-       default:
-               return -1;
-       }
-}
-
-void prefs_filtering_open(FolderItem * item,
+void prefs_filtering_open(GSList ** p_processing,
+                         const gchar * title,
                          const gchar *header,
                          const gchar *key)
 {
-       gchar *esckey;
-
        if (prefs_rc_is_readonly(FILTERING_RC))
                return;
 
@@ -258,12 +121,16 @@ void prefs_filtering_open(FolderItem * item,
 
        manage_window_set_transient(GTK_WINDOW(filtering.window));
        gtk_widget_grab_focus(filtering.ok_btn);
-
-       cur_item = item;
-
-       esckey = matcher_escape_str(key);
-       prefs_filtering_set_dialog(header, esckey);
-       g_free(esckey);
+       
+       if (title != NULL)
+               gtk_window_set_title(GTK_WINDOW(filtering.window), title);
+       else
+               gtk_window_set_title (GTK_WINDOW(filtering.window),
+                                     _("Filtering/Processing configuration"));
+       
+        p_processing_list = p_processing;
+        
+       prefs_filtering_set_dialog(header, key);
 
        gtk_widget_show(filtering.window);
 
@@ -309,15 +176,11 @@ static void prefs_filtering_create(void)
        GtkWidget *cond_clist;
 
        GtkWidget *btn_vbox;
+       GtkWidget *spc_vbox;
+       GtkWidget *top_btn;
        GtkWidget *up_btn;
        GtkWidget *down_btn;
-
-
-       GList *combo_items;
-       gint i;
-
-       GList *accounts;
-       GList * cur;
+       GtkWidget *bottom_btn;
 
        gchar *title[1];
 
@@ -462,6 +325,14 @@ static void prefs_filtering_create(void)
        gtk_widget_show (btn_vbox);
        gtk_box_pack_start (GTK_BOX (cond_hbox), btn_vbox, FALSE, FALSE, 0);
 
+       top_btn = gtk_button_new_with_label (_("Top"));
+       gtk_widget_show (top_btn);
+       gtk_box_pack_start (GTK_BOX (btn_vbox), top_btn, FALSE, FALSE, 0);
+       gtk_signal_connect (GTK_OBJECT (top_btn), "clicked",
+                           GTK_SIGNAL_FUNC (prefs_filtering_top), NULL);
+
+       PACK_VSPACER (btn_vbox, spc_vbox, VSPACING_NARROW_2);
+
        up_btn = gtk_button_new_with_label (_("Up"));
        gtk_widget_show (up_btn);
        gtk_box_pack_start (GTK_BOX (btn_vbox), up_btn, FALSE, FALSE, 0);
@@ -474,6 +345,14 @@ static void prefs_filtering_create(void)
        gtk_signal_connect (GTK_OBJECT (down_btn), "clicked",
                            GTK_SIGNAL_FUNC (prefs_filtering_down), NULL);
 
+       PACK_VSPACER (btn_vbox, spc_vbox, VSPACING_NARROW_2);
+
+       bottom_btn = gtk_button_new_with_label (_("Bottom"));
+       gtk_widget_show (bottom_btn);
+       gtk_box_pack_start (GTK_BOX (btn_vbox), bottom_btn, FALSE, FALSE, 0);
+       gtk_signal_connect (GTK_OBJECT (bottom_btn), "clicked",
+                           GTK_SIGNAL_FUNC (prefs_filtering_bottom), NULL);
+
        gtk_widget_set_usize(window, 500, -1);
 
        gtk_widget_show_all(window);
@@ -492,25 +371,32 @@ static void prefs_filtering_update_hscrollbar(void)
        gtk_clist_set_column_width(GTK_CLIST(filtering.cond_clist), 0, optwidth);
 }
 
+static void rename_path(GSList * filters,
+                       const gchar * old_path, const gchar * new_path);
+
 void prefs_filtering_rename_path(const gchar *old_path, const gchar *new_path)
 {
        GList * cur;
-       gchar *paths[2] = {NULL, NULL};
-       paths[0] = (gchar*)old_path;
-       paths[1] = (gchar*)new_path;
+       const gchar *paths[2] = {NULL, NULL};
+       paths[0] = old_path;
+       paths[1] = new_path;
        for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
                Folder *folder;
                folder = (Folder *) cur->data;
                g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
                                prefs_filtering_rename_path_func, paths);
        }
-       prefs_filtering_rename_path_func(NULL, paths);
+        
+       rename_path(pre_global_processing, old_path, new_path);
+       rename_path(post_global_processing, old_path, new_path);
+       rename_path(filtering_rules, old_path, new_path);
+        
+       prefs_matcher_write_config();
 }
 
-static gboolean prefs_filtering_rename_path_func(GNode *node, gpointer data)
+static void rename_path(GSList * filters,
+                       const gchar * old_path, const gchar * new_path)
 {
-       GSList *cur;
-       gchar *old_path, *new_path;
        gchar *base;
        gchar *prefix;
        gchar *suffix;
@@ -521,26 +407,12 @@ static gboolean prefs_filtering_rename_path_func(GNode *node, gpointer data)
        gint oldpathlen;
        FolderItem *item;
         GSList * action_cur;
-
-       old_path = ((gchar **)data)[0];
-       new_path = ((gchar **)data)[1];
-
-       g_return_val_if_fail(old_path != NULL, FALSE);
-       g_return_val_if_fail(new_path != NULL, FALSE);
+        GSList * cur;
 
        oldpathlen = strlen(old_path);
        old_path_with_sep = g_strconcat(old_path,G_DIR_SEPARATOR_S,NULL);
-       if (node == NULL)
-               cur = global_processing;
-       else {
-               item = node->data;
-               if (!item || !item->prefs) 
-                       return FALSE;
-               cur = item->prefs->processing;
-       }
-
 
-       for (; cur != NULL; cur = cur->next) {
+       for (cur = filters; cur != NULL; cur = cur->next) {
                FilteringProp   *filtering = (FilteringProp *)cur->data;
                 
                 for(action_cur = filtering->action_list ; action_cur != NULL ;
@@ -590,7 +462,7 @@ static gboolean prefs_filtering_rename_path_func(GNode *node, gpointer data)
                                 }
                         } else {
                                 /* folder-moving a leaf */
-                                if (!strcmp(old_path, action->destination)) {          
+                                if (!strcmp(old_path, action->destination)) {
                                         dest_path = g_strdup(new_path);
                                         g_free(action->destination);
                                         action->destination = dest_path;
@@ -598,9 +470,30 @@ static gboolean prefs_filtering_rename_path_func(GNode *node, gpointer data)
                         }
                 }
         }
+}
+
+static gboolean prefs_filtering_rename_path_func(GNode *node, gpointer data)
+{
+       GSList *filters;
+       const gchar * old_path;
+        const gchar * new_path;
+        const gchar ** paths;
+       FolderItem *item;
         
-       g_free(old_path_with_sep);
-       prefs_matcher_write_config();
+        paths = data;
+       old_path = paths[0];
+       new_path = paths[1];
+
+       g_return_val_if_fail(old_path != NULL, FALSE);
+       g_return_val_if_fail(new_path != NULL, FALSE);
+       g_return_val_if_fail(node != NULL, FALSE);
+
+        item = node->data;
+        if (!item || !item->prefs)
+                return FALSE;
+        filters = item->prefs->processing;
+
+        rename_path(filters, old_path, new_path);
 
        return FALSE;
 }
@@ -614,35 +507,30 @@ void prefs_filtering_delete_path(const gchar *path)
                g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
                                prefs_filtering_delete_path_func, (gchar *)path);
        }
-       prefs_filtering_delete_path_func(NULL, (gchar *)path);
+        delete_path(&pre_global_processing, path);
+        delete_path(&post_global_processing, path);
+        delete_path(&filtering_rules, path);
+        
+       prefs_matcher_write_config();
 }
 
-static gboolean prefs_filtering_delete_path_func(GNode *node, gpointer data)
+static void delete_path(GSList ** p_filters, const gchar * path)
 {
-       GSList *cur, *orig;
-       gchar *path = (gchar *)data;
+        GSList * filters;
+        GSList * duplist;
        gchar *suffix;
        gint destlen;
        gint prefixlen;
        gint pathlen;
        FolderItem *item;
         GSList * action_cur;
-       
-       g_return_val_if_fail(path != NULL, FALSE);
-
+       GSList * cur;
+        
+       filters = *p_filters;
        pathlen = strlen(path);
-       if (node == NULL)
-               cur = global_processing;
-       else {
-               item = node->data;
-               if (!item || !item->prefs)
-                       return FALSE;
-               cur = item->prefs->processing;
-       }
-       orig = cur;
-       
-       for (; cur != NULL; cur = cur->next) {
-               FilteringProp *filtering = (FilteringProp *)cur->data;
+       duplist = g_slist_copy(filters);
+       for (cur = duplist ; cur != NULL; cur = g_slist_next(cur)) {
+               FilteringProp *filtering = (FilteringProp *) cur->data;
                 
                 for(action_cur = filtering->action_list ; action_cur != NULL ;
                     action_cur = action_cur->next) {
@@ -661,26 +549,34 @@ static gboolean prefs_filtering_delete_path_func(GNode *node, gpointer data)
                                 
                                 if (suffix && !strncmp(path, suffix, pathlen)) {
                                         filteringprop_free(filtering);
-                                        orig = g_slist_remove(orig, filtering);
+                                        filters = g_slist_remove(filters, filtering);
                                 }
                         } else if (strcmp(action->destination, path) == 0) {
                                 filteringprop_free(filtering);
-                                orig = g_slist_remove(orig, filtering);
-                                
+                                filters = g_slist_remove(filters, filtering);
                         }
                 }
         }                
+       g_slist_free(duplist);
         
-        if (node == NULL)
-                global_processing = orig;
-        else {
-                item = node->data;
-                if (!item || !item->prefs)
-                       return FALSE;
-                item->prefs->processing = orig;
-        }
+        * p_filters = filters;
+}
+
+static gboolean prefs_filtering_delete_path_func(GNode *node, gpointer data)
+{
+       const gchar *path = data;
+       FolderItem *item;
+        GSList ** p_filters;
+       
+       g_return_val_if_fail(path != NULL, FALSE);
+       g_return_val_if_fail(node != NULL, FALSE);
+
+        item = node->data;
+        if (!item || !item->prefs)
+                return FALSE;
+        p_filters = &item->prefs->processing;
         
-       prefs_matcher_write_config();
+        delete_path(p_filters, path);
 
        return FALSE;
 }
@@ -700,10 +596,7 @@ static void prefs_filtering_set_dialog(const gchar *header, const gchar *key)
        row = gtk_clist_append(clist, cond_str);
        gtk_clist_set_row_data(clist, row, NULL);
 
-       if (cur_item == NULL)
-               prefs_filtering = global_processing;
-       else
-               prefs_filtering = cur_item->prefs->processing;
+        prefs_filtering = * p_processing_list;
 
        for(cur = prefs_filtering ; cur != NULL ; cur = g_slist_next(cur)) {
                FilteringProp * prop = (FilteringProp *) cur->data;
@@ -722,9 +615,15 @@ static void prefs_filtering_set_dialog(const gchar *header, const gchar *key)
        prefs_filtering_reset_dialog();
 
        if (header && key) {
-               gchar *match_str = g_strconcat(header, " ",
-                       get_matchparser_tab_str(MATCHTYPE_MATCHCASE),
-                       " \"", key, "\"", NULL);
+               gchar * quoted_key;
+               gchar *match_str;
+
+               quoted_key = matcher_quote_str(key);
+               
+               match_str = g_strconcat(header, " ", get_matchparser_tab_str(MATCHTYPE_MATCHCASE),
+                                       " \"", quoted_key, "\"", NULL);
+               g_free(quoted_key);
+               
                gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), match_str);
                g_free(match_str);
        }
@@ -744,10 +643,7 @@ static void prefs_filtering_set_list(void)
        gchar * filtering_str;
        GSList * prefs_filtering;
 
-       if (cur_item == NULL)
-               prefs_filtering = global_processing;
-       else
-               prefs_filtering = cur_item->prefs->processing;
+        prefs_filtering = * p_processing_list;
 
        for(cur = prefs_filtering ; cur != NULL ; cur = g_slist_next(cur))
                filteringprop_free((FilteringProp *) cur->data);
@@ -765,10 +661,7 @@ static void prefs_filtering_set_list(void)
                row++;
        }
 
-       if (cur_item == NULL)
-               global_processing = prefs_filtering;
-       else
-               cur_item->prefs->processing = prefs_filtering;
+        * p_processing_list = prefs_filtering;
 }
 
 static gint prefs_filtering_clist_set_row(gint row, FilteringProp * prop)
@@ -880,13 +773,6 @@ static FilteringProp * prefs_filtering_dialog_to_filtering(gboolean alert)
        gchar * cond_str;
        gchar * action_str;
        FilteringProp * prop;
-       FilteringAction * action;
-       gint list_id;
-       Action action_id;
-       gint action_type;
-       gint account_id;
-       gchar * destination;
-       gint labelcolor = 0;
        GSList * action_list;
 
        cond_str = gtk_entry_get_text(GTK_ENTRY(filtering.cond_entry));
@@ -976,6 +862,18 @@ static void prefs_filtering_delete_cb(void)
        prefs_filtering_update_hscrollbar();
 }
 
+static void prefs_filtering_top(void)
+{
+       GtkCList *clist = GTK_CLIST(filtering.cond_clist);
+       gint row;
+
+       if (!clist->selection) return;
+
+       row = GPOINTER_TO_INT(clist->selection->data);
+       if (row > 1)
+               gtk_clist_row_move(clist, row, 1);
+}
+
 static void prefs_filtering_up(void)
 {
        GtkCList *clist = GTK_CLIST(filtering.cond_clist);
@@ -986,9 +884,8 @@ static void prefs_filtering_up(void)
        row = GPOINTER_TO_INT(clist->selection->data);
        if (row > 1) {
                gtk_clist_row_move(clist, row, row - 1);
-               if(gtk_clist_row_is_visible(clist, row - 1) != GTK_VISIBILITY_FULL) {
-                       gtk_clist_moveto(clist, row - 1, 0, 0, 0);
-               } 
+               if (gtk_clist_row_is_visible(clist, row - 1) != GTK_VISIBILITY_FULL) 
+                       gtk_clist_moveto(clist, row - 1, 0, 0, 0); 
        }
 }
 
@@ -1002,17 +899,26 @@ static void prefs_filtering_down(void)
        row = GPOINTER_TO_INT(clist->selection->data);
        if (row > 0 && row < clist->rows - 1) {
                gtk_clist_row_move(clist, row, row + 1);
-               if(gtk_clist_row_is_visible(clist, row + 1) != GTK_VISIBILITY_FULL) {
-                       gtk_clist_moveto(clist, row + 1, 0, 1, 0);
-               } 
+               if (gtk_clist_row_is_visible(clist, row + 1) != GTK_VISIBILITY_FULL)
+                       gtk_clist_moveto(clist, row + 1, 0, 1, 0); 
        }
 }
 
+static void prefs_filtering_bottom(void)
+{
+       GtkCList *clist = GTK_CLIST(filtering.cond_clist);
+       gint row;
+
+       if (!clist->selection) return;
+
+       row = GPOINTER_TO_INT(clist->selection->data);
+       if (row > 0 && row < clist->rows - 1)
+               gtk_clist_row_move(clist, row, clist->rows - 1);
+}
+
 static void prefs_filtering_select_set(FilteringProp *prop)
 {
-       FilteringAction *action;
        gchar *matcher_str;
-       gint list_id;
         gchar *action_str;
 
        prefs_filtering_reset_dialog();
@@ -1066,11 +972,12 @@ static gint prefs_filtering_deleted(GtkWidget *widget, GdkEventAny *event,
        return TRUE;
 }
 
-static void prefs_filtering_key_pressed(GtkWidget *widget, GdkEventKey *event,
+static gboolean prefs_filtering_key_pressed(GtkWidget *widget, GdkEventKey *event,
                                     gpointer data)
 {
        if (event && event->keyval == GDK_Escape)
                prefs_filtering_cancel();
+       return TRUE;                    
 }
 
 static void prefs_filtering_ok(void)