Further improve API isolation of folderview.
authorAndrej Kacian <ticho@claws-mail.org>
Thu, 21 Jul 2016 20:32:37 +0000 (22:32 +0200)
committerAndrej Kacian <ticho@claws-mail.org>
Thu, 21 Jul 2016 20:32:37 +0000 (22:32 +0200)
This commit gets rid of all outside access to "opened" and
"selected" struct members, and lot of outside access to
"ctree" member.

Additional work still needed. :)

src/folderview.c
src/folderview.h
src/imap_gtk.c
src/mainwindow.c
src/mh_gtk.c
src/news_gtk.c
src/plugins/mailmbox/plugin_gtk.c
src/plugins/rssyl/rssyl_cb_menu.c
src/plugins/vcalendar/vcal_folder.c
src/prefs_folder_item.c

index 92dc6cc..1b2783b 100644 (file)
@@ -58,6 +58,7 @@
 #include "manual.h"
 #include "timing.h"
 #include "log.h"
+#include "gtkcmctree.h"
 
 #define COL_FOLDER_WIDTH       150
 #define COL_NUM_WIDTH          32
@@ -982,6 +983,9 @@ out:
 
 FolderItem *folderview_get_selected_item(FolderView *folderview)
 {
+       g_return_val_if_fail(folderview != NULL, NULL);
+       g_return_val_if_fail(folderview->ctree != NULL, NULL);
+
        GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
 
        if (!folderview->selected) return NULL;
@@ -990,6 +994,9 @@ FolderItem *folderview_get_selected_item(FolderView *folderview)
 
 FolderItem *folderview_get_opened_item(FolderView *folderview)
 {
+       g_return_val_if_fail(folderview != NULL, NULL);
+       g_return_val_if_fail(folderview->ctree != NULL, NULL);
+
        GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
 
        if (!folderview->opened) return NULL;
@@ -2109,12 +2116,16 @@ static gboolean postpone_select(void *data)
        return FALSE;
 }
 
-void folderview_close_opened(FolderView *folderview)
+void folderview_close_opened(FolderView *folderview, gboolean dirty)
 {
        if (folderview->opened) {
-               FolderItem *olditem;
-               
-               olditem = gtk_cmctree_node_get_row_data(GTK_CMCTREE(folderview->ctree), 
+               if (dirty) {
+                       folderview->opened = NULL;
+                       return;
+               }
+
+               FolderItem *olditem =
+                       gtk_cmctree_node_get_row_data(GTK_CMCTREE(folderview->ctree), 
                                                      folderview->opened);
                if (olditem) {
                        gchar *buf = g_strdup_printf(_("Closing folder %s..."), 
@@ -2187,7 +2198,7 @@ static void folderview_selected(GtkCMCTree *ctree, GtkCMCTreeNode *row,
        /* Save cache for old folder */
        /* We don't want to lose all caches if sylpheed crashed */
        /* resets folderview->opened to NULL */
-       folderview_close_opened(folderview);
+       folderview_close_opened(folderview, FALSE);
        
        /* CLAWS: set compose button type: news folder items 
         * always have a news folder as parent */
@@ -3173,6 +3184,21 @@ void folderview_unregister_popup(FolderViewPopup *fpopup)
        g_hash_table_remove(folderview_popups, fpopup->klass);
 }
 
+void folderview_remove_item(FolderView *folderview, FolderItem *item)
+{
+       g_return_if_fail(folderview != NULL);
+       g_return_if_fail(item != NULL);
+
+       GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
+       g_return_if_fail(ctree != NULL);
+
+       GtkCMCTreeNode *node =
+               gtk_cmctree_find_by_row_data(ctree, NULL, item);
+       g_return_if_fail(node != NULL);
+
+       gtk_cmctree_remove_node(ctree, node);
+}
+
 void folderview_freeze(FolderView *folderview)
 {
        if (folderview)
index b7cc432..4ec6f48 100644 (file)
@@ -146,7 +146,10 @@ void folderview_finish_dnd         (const gchar            *data,
                                         GdkDragContext         *drag_context,
                                         guint                   time, 
                                         FolderItem             *item);
-void folderview_close_opened           (FolderView             *folderview);
+void folderview_close_opened           (FolderView             *folderview,
+                                        gboolean                dirty);
+void folderview_remove_item(FolderView *folderview,
+                           FolderItem *item);
 
 void folderview_freeze(FolderView *folderview);
 void folderview_thaw(FolderView *folderview);
index 7931d9b..b304ef2 100644 (file)
@@ -183,9 +183,8 @@ static void new_folder_cb(GtkAction *action, gpointer data)
        gchar *p;
        gchar separator = '/';
        
-       if (!folderview->selected) return;
+       if ((item = folderview_get_selected_item(folderview)) == NULL) return;
 
-       item = folderview_get_selected_item(folderview);
        cm_return_if_fail(item != NULL);
        cm_return_if_fail(item->folder != NULL);
        cm_return_if_fail(item->folder->account != NULL);
@@ -340,15 +339,14 @@ static void copy_folder_cb(GtkAction *action, gpointer data)
 static void delete_folder_cb(GtkAction *action, gpointer data)
 {
        FolderView *folderview = (FolderView *)data;
-       GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
-       FolderItem *item;
+       FolderItem *item, *opened;
        gchar *message, *name;
        AlertValue avalue;
        gchar *old_id;
 
-       if (!folderview->selected) return;
+       if ((item = folderview_get_selected_item(folderview)) == NULL) return;
+       opened = folderview_get_opened_item(folderview);
 
-       item = folderview_get_selected_item(folderview);
        cm_return_if_fail(item != NULL);
        cm_return_if_fail(item->path != NULL);
        cm_return_if_fail(item->folder != NULL);
@@ -367,12 +365,10 @@ static void delete_folder_cb(GtkAction *action, gpointer data)
 
        old_id = folder_item_get_identifier(item);
 
-       if (folderview->opened == folderview->selected ||
-           gtk_cmctree_is_ancestor(ctree,
-                                 folderview->selected,
-                                 folderview->opened)) {
+       if (item == opened ||
+                       folder_is_child_of(item, opened)) {
                summary_clear_all(folderview->summaryview);
-               folderview->opened = NULL;
+               folderview_close_opened(folderview, TRUE);
        }
 
        if (item->folder->klass->remove_folder(item->folder, item) < 0) {
@@ -482,17 +478,13 @@ static gboolean imap_gtk_subscribe_func(GNode *node, gpointer data)
 
 static void subscribe_cb_full(FolderView *folderview, guint action)
 {
-       GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
        FolderItem *item;
        gchar *message, *name;
        AlertValue avalue;
        GtkWidget *rec_chk;
        gboolean recurse = FALSE;
 
-       if (!folderview->selected) return;
-
-       item = folderview_get_selected_item(folderview);
-       cm_return_if_fail(item != NULL);
+       if ((item = folderview_get_selected_item(folderview)) == NULL) return;
        cm_return_if_fail(item->folder != NULL);
 
        name = trim_string(item->name, 32);
@@ -577,14 +569,13 @@ static void subscribe_cb_full(FolderView *folderview, guint action)
        g_free(message);
        if (avalue != G_ALERTALTERNATE) return;
        
-       
+       FolderItem *opened = folderview_get_opened_item(folderview);
+       FolderItem *selected = folderview_get_selected_item(folderview);
        if (!action) {
-               if (folderview->opened == folderview->selected ||
-                   gtk_cmctree_is_ancestor(ctree,
-                                         folderview->selected,
-                                         folderview->opened)) {
+               if (opened == selected ||
+                               folder_is_child_of(selected, opened)) {
                        summary_clear_all(folderview->summaryview);
-                       folderview->opened = NULL;
+                       folderview_close_opened(folderview, TRUE);
                }
        }
 
@@ -612,20 +603,18 @@ static void unsubscribe_cb(GtkAction *action, gpointer data)
 static void subscribed_cb(GtkAction *action, gpointer data)
 {
        FolderView *folderview = (FolderView *)data;
-       GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
        FolderItem *item = folderview_get_selected_item(folderview);
+       FolderItem *opened = folderview_get_opened_item(folderview);
        
        if (!item || !item->folder || !item->folder->account)
                return;
        if (item->folder->account->imap_subsonly == gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)))
                return;
 
-       if (folderview->opened == folderview->selected ||
-           gtk_cmctree_is_ancestor(ctree,
-                                 folderview->selected,
-                                 folderview->opened)) {
+       if (opened == item ||
+                       folder_is_child_of(item, opened)) {
                summary_clear_all(folderview->summaryview);
-               folderview->opened = NULL;
+               folderview_close_opened(folderview, TRUE);
        }
 
        item->folder->account->imap_subsonly = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
@@ -637,8 +626,6 @@ static void download_cb(GtkAction *action, gpointer data)
        FolderView *folderview = (FolderView *)data;
        FolderItem *item;
 
-       if (!folderview->selected) return;
-
-       item = folderview_get_selected_item(folderview);
+       if ((item = folderview_get_selected_item(folderview)) == NULL) return
        imap_gtk_synchronise(item, 0);
 }
index 71e0320..f81f512 100644 (file)
@@ -4771,14 +4771,9 @@ static void update_summary_cb(GtkAction *action, gpointer data)
        FolderView *folderview = mainwin->folderview;
 
        if (!mainwin->summaryview->folder_item) return;
-       if (!folderview->opened) return;
+       if ((fitem = folderview_get_opened_item(folderview)) == NULL) return;
 
        folder_update_op_count();
-
-       fitem = gtk_cmctree_node_get_row_data(GTK_CMCTREE(folderview->ctree),
-                                           folderview->opened);
-       if (!fitem) return;
-
        folder_item_scan(fitem);
        summary_show(mainwin->summaryview, fitem);
 }
@@ -5439,7 +5434,7 @@ void mainwindow_jump_to(const gchar *target, gboolean popup)
 
 void mainwindow_exit_folder(MainWindow *mainwin) {
        if (prefs_common.layout_mode == SMALL_LAYOUT) {
-               folderview_close_opened(mainwin->folderview);
+               folderview_close_opened(mainwin->folderview, FALSE);
                mainwin_paned_show_first(GTK_PANED(mainwin->hpaned));
                folderview_grab_focus(mainwin->folderview);
        }
index f98cc1e..985f05e 100644 (file)
@@ -135,7 +135,7 @@ static void new_folder_cb(GtkAction *action, gpointer data)
        gchar *name;
        gchar *p;
 
-       if (!folderview->selected) return;
+       if (!folderview_get_selected_item(folderview)) return;
 
        item = folderview_get_selected_item(folderview);
        cm_return_if_fail(item != NULL);
@@ -184,14 +184,15 @@ static void new_folder_cb(GtkAction *action, gpointer data)
 static void delete_folder_cb(GtkAction *action, gpointer data)
 {
        FolderView *folderview = (FolderView *)data;
-       GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
-       FolderItem *item;
+       FolderItem *item, *opened;
        gchar *message, *name;
        AlertValue avalue;
        gchar *old_id;
        gint ret;
 
        item = folderview_get_selected_item(folderview);
+       opened = folderview_get_opened_item(folderview);
+
        cm_return_if_fail(item != NULL);
        cm_return_if_fail(item->path != NULL);
        cm_return_if_fail(item->folder != NULL);
@@ -210,12 +211,10 @@ static void delete_folder_cb(GtkAction *action, gpointer data)
 
        old_id = folder_item_get_identifier(item);
 
-       if (folderview->opened == folderview->selected ||
-           gtk_cmctree_is_ancestor(ctree,
-                                 folderview->selected,
-                                 folderview->opened)) {
+       if (item == opened ||
+                       folder_is_child_of(item, opened)) {
                summary_clear_all(folderview->summaryview);
-               folderview->opened = NULL;
+               folderview_close_opened(folderview, TRUE);
        }
 
        if ((ret = item->folder->klass->remove_folder(item->folder, item)) < 0) {
index 2783f78..b5d3fe1 100644 (file)
@@ -150,7 +150,6 @@ static void subscribe_newsgroup_cb(GtkAction *action, gpointer data)
 {
        FolderView *folderview = (FolderView *)data;
        GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
-       GtkCMCTreeNode *servernode, *node;
        Folder *folder;
        FolderItem *item;
        FolderItem *rootitem;
@@ -160,10 +159,7 @@ static void subscribe_newsgroup_cb(GtkAction *action, gpointer data)
        GNode *gnode;
        MainWindow *mainwin = mainwindow_get_mainwindow();
        
-       if (!folderview->selected) return;
-
-       item = folderview_get_selected_item(folderview);
-       cm_return_if_fail(item != NULL);
+       if ((item = folderview_get_selected_item(folderview)) == NULL) return;
 
        if (mainwin->lock_count || news_folder_locked(item->folder))
                return;
@@ -173,12 +169,8 @@ static void subscribe_newsgroup_cb(GtkAction *action, gpointer data)
        cm_return_if_fail(FOLDER_TYPE(folder) == F_NEWS);
        cm_return_if_fail(folder->account != NULL);
 
-       if (GTK_CMCTREE_ROW(folderview->selected)->parent != NULL)
-               servernode = GTK_CMCTREE_ROW(folderview->selected)->parent;
-       else
-               servernode = folderview->selected;
-
-       rootitem = gtk_cmctree_node_get_row_data(ctree, servernode);
+       if ((rootitem = folder_item_parent(item)) == NULL)
+               rootitem = item;
 
        new_subscr = grouplist_dialog(folder);
 
@@ -193,18 +185,12 @@ static void subscribe_newsgroup_cb(GtkAction *action, gpointer data)
                        continue;
                }
 
-               node = gtk_cmctree_find_by_row_data(ctree, servernode, item);
-               if (!node) {
-                       gnode = next;
-                       continue;
-               }
-
-               if (folderview->opened == node) {
+               if (folderview_get_opened_item(folderview) == item) {
                        summary_clear_all(folderview->summaryview);
-                       folderview->opened = NULL;
+                       folderview_close_opened(folderview, TRUE);
                }
 
-               gtk_cmctree_remove_node(ctree, node);
+               folderview_remove_item(folderview, item);
                folder_item_remove(item);
 
                gnode = next;
@@ -270,9 +256,9 @@ static void unsubscribe_newsgroup_cb(GtkAction *action, gpointer data)
        g_free(name);
        if (avalue != G_ALERTALTERNATE) return;
 
-       if (folderview->opened == folderview->selected) {
+       if (item == folderview_get_opened_item(folderview)) {
                summary_clear_all(folderview->summaryview);
-               folderview->opened = NULL;
+               folderview_close_opened(folderview, TRUE);
        }
 
        if(item->folder->klass->remove_folder(item->folder, item) < 0) {
index 16ea162..a578616 100644 (file)
@@ -313,8 +313,7 @@ static void remove_mailbox_cb(GtkAction *action, gpointer data)
 static void delete_folder_cb(GtkAction *action, gpointer data)
 {
        FolderView *folderview = (FolderView *)data;
-       GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
-       FolderItem *item;
+       FolderItem *item, *opened;
        gchar *message, *name;
        AlertValue avalue;
        gchar *old_id;
@@ -323,6 +322,7 @@ static void delete_folder_cb(GtkAction *action, gpointer data)
        g_return_if_fail(item != NULL);
        g_return_if_fail(item->path != NULL);
        g_return_if_fail(item->folder != NULL);
+       opened = folderview_get_opened_item(folderview);
 
        name = trim_string(item->name, 32);
        AUTORELEASE_STR(name, {g_free(name); return;});
@@ -337,17 +337,15 @@ static void delete_folder_cb(GtkAction *action, gpointer data)
 
        old_id = folder_item_get_identifier(item);
 
-       if (folderview->opened == folderview->selected ||
-           gtk_cmctree_is_ancestor(ctree,
-                                 folderview->selected,
-                                 folderview->opened)) {
+       if (item == opened ||
+                       folder_is_child_of(item, opened)) {
                summary_clear_all(folderview->summaryview);
-               folderview->opened = NULL;
+               folderview_close_opened(folderview, TRUE);
        }
 
        if (item->folder->klass->remove_folder(item->folder, item) < 0) {
                alertpanel_error(_("Can't remove the folder '%s'."), name);
-               if (folderview->opened == folderview->selected)
+               if (item == opened)
                        summary_show(folderview->summaryview,
                                     folderview->summaryview->folder_item);
                g_free(old_id);
index 7a1e8fb..826e171 100644 (file)
@@ -135,8 +135,7 @@ void rssyl_remove_folder_cb(GtkAction *action,
                             gpointer data)
 {
        FolderView *folderview = (FolderView*)data;
-       GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
-       FolderItem *item;
+       FolderItem *item, *opened;
        gchar *message, *name;
        AlertValue avalue;
        gchar *old_id;
@@ -145,6 +144,7 @@ void rssyl_remove_folder_cb(GtkAction *action,
        g_return_if_fail(item != NULL);
        g_return_if_fail(item->path != NULL);
        g_return_if_fail(item->folder != NULL);
+       opened = folderview_get_opened_item(folderview);
 
        name = trim_string(item->name, 32);
        AUTORELEASE_STR(name, {g_free(name); return;});
@@ -160,12 +160,10 @@ void rssyl_remove_folder_cb(GtkAction *action,
 
        old_id = folder_item_get_identifier(item);
 
-       if (folderview->opened == folderview->selected ||
-           gtk_cmctree_is_ancestor(ctree,
-                                 folderview->selected,
-                                 folderview->opened)) {
+       if (item == opened ||
+                       folder_is_child_of(item, opened)) {
                summary_clear_all(folderview->summaryview);
-               folderview->opened = NULL;
+               folderview_close_opened(folderview, TRUE);
        }
 
        if (item->folder->klass->remove_folder(item->folder, item) < 0) {
index b495950..af30bbb 100644 (file)
@@ -1966,8 +1966,7 @@ static void subscribe_cal_cb(GtkAction *action, gpointer data)
 static void unsubscribe_cal_cb(GtkAction *action, gpointer data)
 {
        FolderView *folderview = (FolderView *)data;
-       GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
-       FolderItem *item;
+       FolderItem *item, *opened;
        gchar *message;
        AlertValue avalue;
        gchar *old_id;
@@ -1978,6 +1977,7 @@ static void unsubscribe_cal_cb(GtkAction *action, gpointer data)
        g_return_if_fail(item != NULL);
        g_return_if_fail(item->path != NULL);
        g_return_if_fail(item->folder != NULL);
+       opened = folderview_get_opened_item(folderview);
 
        message = g_strdup_printf
                (_("Do you really want to unsubscribe?"));
@@ -1991,12 +1991,10 @@ static void unsubscribe_cal_cb(GtkAction *action, gpointer data)
 
        vcal_item_closed(item);
 
-       if (folderview->opened == folderview->selected ||
-           gtk_cmctree_is_ancestor(ctree,
-                                 folderview->selected,
-                                 folderview->opened)) {
+       if (item == opened ||
+                       folder_is_child_of(item, opened)) {
                summary_clear_all(folderview->summaryview);
-               folderview->opened = NULL;
+               folderview_close_opened(folderview, TRUE);
        }
 
        if (item->folder->klass->remove_folder(item->folder, item) < 0) {
index 6f4a4c5..6990206 100644 (file)
@@ -1681,7 +1681,7 @@ static void clean_cache_cb(GtkWidget *widget, gpointer data)
                folderview = mainwindow_get_mainwindow()->folderview;
 
        if (folderview && item->opened) {
-               folderview_close_opened(folderview);
+               folderview_close_opened(folderview, FALSE);
                was_open = TRUE;
        }
        folder_item_discard_cache(item);