2011-11-21 [pawel] 3.7.10cvs97
[claws.git] / src / prefs_filtering.c
index 44991f80c3b6cce1ef6b51d2bc6eeb419a3e82e3..ffdd8a45e5732aab3a07866f26c4b2e68b3e75bc 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail team
+ * Copyright (C) 1999-2011 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
@@ -114,6 +114,8 @@ static void prefs_filtering_bottom  (gpointer action, gpointer data);
 static gint prefs_filtering_deleted    (GtkWidget      *widget,
                                         GdkEventAny    *event,
                                         gpointer        data);
+static void prefs_filtering_row_selected(GtkTreeSelection *selection,
+                                        GtkTreeView *list_view);
 static gboolean prefs_filtering_key_pressed(GtkWidget  *widget,
                                         GdkEventKey    *event,
                                         gpointer        data);
@@ -220,6 +222,7 @@ void prefs_filtering_open(GSList ** p_processing,
        }
 
        gtk_widget_show(filtering.window);
+       gtk_window_set_modal(GTK_WINDOW(filtering.window), TRUE);
 
        start_address_completion(NULL);
 }
@@ -227,7 +230,7 @@ void prefs_filtering_open(GSList ** p_processing,
 static void prefs_filtering_size_allocate_cb(GtkWidget *widget,
                                         GtkAllocation *allocation)
 {
-       g_return_if_fail(allocation != NULL);
+       cm_return_if_fail(allocation != NULL);
 
        prefs_common.filteringwin_width = allocation->width;
        prefs_common.filteringwin_height = allocation->height;
@@ -244,6 +247,7 @@ static void prefs_filtering_close(void)
                                (filtering.cond_list_view)));
        gtk_list_store_clear(store);
        gtk_widget_hide(filtering.window);
+       gtk_window_set_modal(GTK_WINDOW(filtering.window), FALSE);
        inc_unlock();
 }
 
@@ -254,14 +258,14 @@ static void prefs_filtering_account_option_menu_populate(void)
 
        accounts = account_get_list();
 
-       g_return_if_fail(accounts != NULL);
+       cm_return_if_fail(accounts != NULL);
 
        COMBOBOX_ADD(filtering.account_combobox_list, Q_("Filtering Account Menu|All"), 0);
+       COMBOBOX_ADD(filtering.account_combobox_list, NULL, 0);
        for (; accounts != NULL; accounts = accounts->next) {
                PrefsAccount *ac = (PrefsAccount *)accounts->data;
 
-               if (ac->protocol != A_NONE)
-                       COMBOBOX_ADD_ESCAPED(filtering.account_combobox_list, ac->account_name, ac->account_id);
+               COMBOBOX_ADD_ESCAPED(filtering.account_combobox_list, ac->account_name, ac->account_id);
        }
 }
 
@@ -328,14 +332,12 @@ static void prefs_filtering_create(void)
        GtkWidget *bottom_btn;
        GtkWidget *table;
        static GdkGeometry geometry;
-       CLAWS_TIP_DECL();
 
        debug_print("Creating filtering configuration window...\n");
 
        window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "prefs_filtering");
        gtk_container_set_border_width (GTK_CONTAINER (window), 8);
        gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
-       gtk_window_set_modal (GTK_WINDOW (window), TRUE);
        gtk_window_set_resizable(GTK_WINDOW (window), TRUE);
 
        vbox = gtk_vbox_new (FALSE, 6);
@@ -654,9 +656,13 @@ static void rename_path(GSList * filters,
        gint oldpathlen;
         GSList * action_cur;
         GSList * cur;
-
+       const gchar *separator=G_DIR_SEPARATOR_S;
+       gboolean matched = FALSE;
+#ifdef G_OS_WIN32
+again:
+#endif
        oldpathlen = strlen(old_path);
-       old_path_with_sep = g_strconcat(old_path,G_DIR_SEPARATOR_S,NULL);
+       old_path_with_sep = g_strconcat(old_path,separator,NULL);
 
        for (cur = filters; cur != NULL; cur = cur->next) {
                FilteringProp   *filtering = (FilteringProp *)cur->data;
@@ -686,28 +692,30 @@ static void rename_path(GSList * filters,
                                         while (*base == G_DIR_SEPARATOR) base++;
                                         if (*base == '\0')
                                                 dest_path = g_strconcat(prefix,
-                                                    G_DIR_SEPARATOR_S,
+                                                    separator,
                                                     new_path, NULL);
                                         else
                                                 dest_path = g_strconcat(prefix,
-                                                    G_DIR_SEPARATOR_S,
+                                                    separator,
                                                     new_path,
-                                                    G_DIR_SEPARATOR_S,
+                                                    separator,
                                                     base, NULL);
                                         
                                         g_free(prefix);
                                         g_free(action->destination);
                                         action->destination = dest_path;
+                                       matched = TRUE;
                                 } else { /* for non-leaf folders */
                                         /* compare with trailing slash */
                                         if (!strncmp(old_path_with_sep, action->destination, oldpathlen+1)) {
                                                 
                                                 suffix = action->destination + oldpathlen + 1;
                                                 dest_path = g_strconcat(new_path,
-                                                    G_DIR_SEPARATOR_S,
+                                                    separator,
                                                     suffix, NULL);
                                                 g_free(action->destination);
                                                 action->destination = dest_path;
+                                               matched = TRUE;
                                         }
                                 }
                         } else {
@@ -716,10 +724,19 @@ static void rename_path(GSList * filters,
                                         dest_path = g_strdup(new_path);
                                         g_free(action->destination);
                                         action->destination = dest_path;
+                                       matched = TRUE;
                                 }
                         }
                 }
         }
+       
+       g_free(old_path_with_sep);
+#ifdef G_OS_WIN32
+       if (!strcmp(separator, G_DIR_SEPARATOR_S) && !matched) {
+               separator = "/";
+               goto again;
+       }
+#endif
 }
 
 static gboolean prefs_filtering_rename_path_func(GNode *node, gpointer data)
@@ -734,9 +751,9 @@ static gboolean prefs_filtering_rename_path_func(GNode *node, gpointer 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);
+       cm_return_val_if_fail(old_path != NULL, FALSE);
+       cm_return_val_if_fail(new_path != NULL, FALSE);
+       cm_return_val_if_fail(node != NULL, FALSE);
 
         item = node->data;
         if (!item || !item->prefs)
@@ -748,6 +765,83 @@ static gboolean prefs_filtering_rename_path_func(GNode *node, gpointer data)
        return FALSE;
 }
 
+static void rename_path_prefix(GSList *filters, const gchar *old_prefix,
+                               const gchar *new_prefix)
+{
+        GSList *action_cur, *cur;
+        gchar *new_path;
+        
+       for (cur = filters; cur != NULL; cur = cur->next) {
+               FilteringProp   *filtering = (FilteringProp *)cur->data;
+                
+                for(action_cur = filtering->action_list ; action_cur != NULL ;
+                    action_cur = action_cur->next) {
+
+                        FilteringAction *action = action_cur->data;
+                        
+                        if (action->type == MATCHACTION_SET_TAG ||
+                           action->type == MATCHACTION_UNSET_TAG)
+                               continue;
+                        if (!action->destination)
+                               continue;
+                       if (!g_str_has_prefix(action->destination, old_prefix))
+                               continue;
+                       
+                       new_path = g_strconcat(new_prefix,
+                               (action->destination + strlen(old_prefix)), NULL);
+                       g_free(action->destination);
+                       action->destination = new_path;
+               }
+       }
+}
+
+static gboolean prefs_filtering_rename_path_prefix_func(GNode *node, gpointer data)
+{
+       GSList *filters;
+       const gchar * old_prefix;
+        const gchar * new_prefix;
+        const gchar ** paths;
+       FolderItem *item;
+        
+        paths = data;
+       old_prefix = paths[0];
+       new_prefix = paths[1];
+
+       cm_return_val_if_fail(old_prefix != NULL, FALSE);
+       cm_return_val_if_fail(new_prefix != NULL, FALSE);
+       cm_return_val_if_fail(node != NULL, FALSE);
+
+        item = node->data;
+        if (!item || !item->prefs)
+                return FALSE;
+        filters = item->prefs->processing;
+
+        rename_path_prefix(filters, old_prefix, new_prefix);
+
+       return FALSE;
+}
+
+void prefs_filtering_rename_path_prefix(const gchar *old_prefix,
+                                       const gchar *new_prefix)
+{
+       GList * cur;
+       const gchar *paths[2] = {NULL, NULL};
+       paths[0] = old_prefix;
+       paths[1] = new_prefix;
+       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_prefix_func, paths);
+       }
+        
+       rename_path_prefix(pre_global_processing, old_prefix, new_prefix);
+       rename_path_prefix(post_global_processing, old_prefix, new_prefix);
+       rename_path_prefix(filtering_rules, old_prefix, new_prefix);
+        
+       prefs_matcher_write_config();
+}
+
 static void rename_tag(GSList * filters,
                        const gchar * old_tag, const gchar * new_tag)
 {
@@ -787,9 +881,9 @@ static gboolean prefs_filtering_rename_tag_func(GNode *node, gpointer data)
        old_tag = tags[0];
        new_tag = tags[1];
 
-       g_return_val_if_fail(old_tag != NULL, FALSE);
-       g_return_val_if_fail(new_tag != NULL, FALSE);
-       g_return_val_if_fail(node != NULL, FALSE);
+       cm_return_val_if_fail(old_tag != NULL, FALSE);
+       cm_return_val_if_fail(new_tag != NULL, FALSE);
+       cm_return_val_if_fail(node != NULL, FALSE);
 
         item = node->data;
         if (!item || !item->prefs)
@@ -874,8 +968,8 @@ static gboolean prefs_filtering_delete_path_func(GNode *node, gpointer data)
        FolderItem *item;
         GSList ** p_filters;
        
-       g_return_val_if_fail(path != NULL, FALSE);
-       g_return_val_if_fail(node != NULL, FALSE);
+       cm_return_val_if_fail(path != NULL, FALSE);
+       cm_return_val_if_fail(node != NULL, FALSE);
 
         item = node->data;
         if (!item || !item->prefs)
@@ -1035,14 +1129,15 @@ static void prefs_filtering_set_list(void)
 static gint prefs_filtering_list_view_set_row(gint row, FilteringProp * prop)
 {
        GtkTreeView *list_view = GTK_TREE_VIEW(filtering.cond_list_view);
-       gchar *str;
+       gchar *str = NULL;
        GtkListStore *list_store;
        gchar *name = NULL;
        gint account_id = 0;
        gchar *account_name = (gchar *)Q_("Filtering Account Menu|All");
        gboolean enabled = TRUE;
 
-       str = filteringprop_to_string(prop);
+       if (prop)
+               str = filteringprop_to_string(prop);
        if (str == NULL)
                return -1;
 
@@ -1245,7 +1340,9 @@ static void prefs_filtering_substitute_cb(gpointer action, gpointer data)
 
        filteringprop_free(prop);
 
-       prefs_filtering_reset_dialog();
+       prefs_filtering_row_selected(gtk_tree_view_get_selection(
+                               GTK_TREE_VIEW(filtering.cond_list_view)),
+                               GTK_TREE_VIEW(filtering.cond_list_view));
        modified = TRUE;
 }
 
@@ -1515,7 +1612,7 @@ static gint prefs_filtering_deleted(GtkWidget *widget, GdkEventAny *event,
 static gboolean prefs_filtering_key_pressed(GtkWidget *widget, GdkEventKey *event,
                                     gpointer data)
 {
-       if (event && event->keyval == GDK_Escape) {
+       if (event && event->keyval == GDK_KEY_Escape) {
                prefs_filtering_cancel(NULL, NULL);
                return TRUE;                    
        }
@@ -1761,6 +1858,21 @@ static GtkActionEntry prefs_filtering_popup_entries[] =
 #endif
 };
 
+static void prefs_filtering_row_selected(GtkTreeSelection *selection,
+                                        GtkTreeView *list_view)
+{
+       GtkTreePath *path;
+       GtkTreeIter iter;
+       GtkTreeModel *model;
+       
+       if (!gtk_tree_selection_get_selected(selection, &model, &iter))
+               return;
+       
+       path = gtk_tree_model_get_path(model, &iter);
+       prefs_filtering_select_row(list_view, path);
+       gtk_tree_path_free(path);
+}
+
 static gint prefs_filtering_list_btn_pressed(GtkWidget *widget, GdkEventButton *event,
                                    GtkTreeView *list_view)
 {
@@ -1864,6 +1976,8 @@ static GtkWidget *prefs_filtering_list_view_create(void)
        
        selector = gtk_tree_view_get_selection(list_view);
        gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
+       g_signal_connect(G_OBJECT(selector), "changed",
+                        G_CALLBACK(prefs_filtering_row_selected), list_view);
 
        /* create the columns */
        prefs_filtering_create_list_view_columns(GTK_WIDGET(list_view));