Optimize (for speed) and factorize code related to marking folders
authorwwp <subscript@free.fr>
Fri, 24 Aug 2018 12:59:13 +0000 (14:59 +0200)
committerwwp <subscript@free.fr>
Fri, 24 Aug 2018 12:59:13 +0000 (14:59 +0200)
as read/unread recursively. A bit of code format fixing too.

src/folderutils.c
src/folderutils.h
src/folderview.c
src/summaryview.c

index 355af85..9d71a51 100644 (file)
@@ -27,6 +27,9 @@
 #include "mainwindow.h"
 #include "summaryview.h"
 
+static gboolean folderutils_mark_all_read_node_func(GNode *node, gpointer data);
+
+
 gint folderutils_delete_duplicates(FolderItem *item,
                                   DeleteDuplicatesMode mode)
 {
@@ -119,60 +122,23 @@ gint folderutils_delete_duplicates(FolderItem *item,
        return dups;
 }
 
-void folderutils_mark_all_read(FolderItem *item)
+void folderutils_mark_all_read(FolderItem *item, gboolean mark_as_read)
 {
        MsgInfoList *msglist, *cur;
        MainWindow *mainwin = mainwindow_get_mainwindow();
        int i = 0, m = 0;
-       debug_print("marking all read in item %s\n", (item==NULL)?"NULL":item->name);
-       cm_return_if_fail(item != NULL);
-
-
-       folder_item_update_freeze();
-       if (mainwin && mainwin->summaryview &&
-           mainwin->summaryview->folder_item == item) {
-               debug_print("folder opened, using summary\n");
-               summary_mark_all_read(mainwin->summaryview, FALSE);
-       } else {
-               msglist = folder_item_get_msg_list(item);
-               debug_print("got msglist %p\n", msglist);
-               if (msglist == NULL) {
-                       folder_item_update_thaw();
-                       return;
-               }
-               folder_item_set_batch(item, TRUE);
-               for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
-                       MsgInfo *msginfo = cur->data;
-
-                       if (msginfo->flags.perm_flags & (MSG_NEW | MSG_UNREAD)) {
-                               procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
-                               m++;
-                       }
-                       i++;
-                       procmsg_msginfo_free(&msginfo);
-               }
-               folder_item_set_batch(item, FALSE);
-               folder_item_close(item);
-               debug_print("marked %d messages out of %d as read\n", m, i);
-               g_slist_free(msglist);
-       }
-       folder_item_update_thaw();
-}
+       gchar *msg = mark_as_read?"read":"unread";
+       void(*summary_mark_func)(SummaryView*, gboolean) =
+                       mark_as_read?summary_mark_all_read:summary_mark_all_unread;
 
-void folderutils_mark_all_unread(FolderItem *item)
-{
-       MsgInfoList *msglist, *cur;
-       MainWindow *mainwin = mainwindow_get_mainwindow();
-       int i = 0, m = 0;
-       debug_print("marking all unread in item %s\n", (item==NULL)?"NULL":item->name);
+       debug_print("marking all %s in item %s\n", msg, (item==NULL)?"NULL":item->name);
        cm_return_if_fail(item != NULL);
 
-
        folder_item_update_freeze();
        if (mainwin && mainwin->summaryview &&
            mainwin->summaryview->folder_item == item) {
                debug_print("folder opened, using summary\n");
-               summary_mark_all_unread(mainwin->summaryview, FALSE);
+               summary_mark_func(mainwin->summaryview, FALSE);
        } else {
                msglist = folder_item_get_msg_list(item);
                debug_print("got msglist %p\n", msglist);
@@ -184,65 +150,45 @@ void folderutils_mark_all_unread(FolderItem *item)
                for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
                        MsgInfo *msginfo = cur->data;
 
-                       if (!(msginfo->flags.perm_flags & MSG_UNREAD)) {
-                               procmsg_msginfo_set_flags(msginfo, MSG_UNREAD, 0);
-                               m++;
+                       if (mark_as_read) {
+                               if (msginfo->flags.perm_flags & (MSG_NEW | MSG_UNREAD)) {
+                                       procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
+                                       m++;
+                               }
+                       } else {
+                               if (!(msginfo->flags.perm_flags & MSG_UNREAD)) {
+                                       procmsg_msginfo_set_flags(msginfo, MSG_UNREAD, 0);
+                                       m++;
+                               }
                        }
                        i++;
                        procmsg_msginfo_free(&msginfo);
                }
                folder_item_set_batch(item, FALSE);
                folder_item_close(item);
-               debug_print("marked %d messages out of %d as unread\n", m, i);
+               debug_print("marked %d messages out of %d as %s\n", m, i, msg);
                g_slist_free(msglist);
        }
        folder_item_update_thaw();
 }
 
-void folderutils_mark_all_read_recursive(FolderItem *item)
+static gboolean folderutils_mark_all_read_node_func(GNode *node, gpointer data)
 {
-       GNode *node;
-
-       cm_return_if_fail(item != NULL);
-
-       folderutils_mark_all_read(item);
-
-       cm_return_if_fail(item->folder != NULL);
-       cm_return_if_fail(item->folder->node != NULL);
-
-       node = item->folder->node;
-       node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
-       node = node->children;
-
-       while (node != NULL) {
-               if (node->data != NULL) {
-                       FolderItem *sub_item = (FolderItem *) node->data;
-                       node = node->next;
-                       folderutils_mark_all_read_recursive(sub_item);
-               }
+       if (node) {
+               FolderItem *sub_item = (FolderItem *) node->data;
+               folderutils_mark_all_read(sub_item, (gboolean) GPOINTER_TO_INT(data));
        }
+       return(FALSE);
 }
 
-void folderutils_mark_all_unread_recursive(FolderItem *item)
+void folderutils_mark_all_read_recursive(FolderItem *item, gboolean mark_as_read)
 {
-       GNode *node;
-
        cm_return_if_fail(item != NULL);
 
-       folderutils_mark_all_unread(item);
-
        cm_return_if_fail(item->folder != NULL);
        cm_return_if_fail(item->folder->node != NULL);
 
-       node = item->folder->node;
-       node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
-       node = node->children;
-
-       while (node != NULL) {
-               if (node->data != NULL) {
-                       FolderItem *sub_item = (FolderItem *) node->data;
-                       node = node->next;
-                       folderutils_mark_all_unread_recursive(sub_item);
-               }
-       }
+       g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
+               folderutils_mark_all_read_node_func,
+               GINT_TO_POINTER(mark_as_read));
 }
index 64a5c2e..9394d44 100644 (file)
@@ -29,9 +29,7 @@ typedef enum {
 
 gint folderutils_delete_duplicates(FolderItem *item,
                                   DeleteDuplicatesMode mode);
-void folderutils_mark_all_read   (FolderItem *item);
-void folderutils_mark_all_unread         (FolderItem *item);
-void folderutils_mark_all_read_recursive         (FolderItem *item);
-void folderutils_mark_all_unread_recursive       (FolderItem *item);
+void folderutils_mark_all_read   (FolderItem *item, gboolean read);
+void folderutils_mark_all_read_recursive         (FolderItem *item, gboolean read);
 
 #endif /* FOLDERUTILS_H */
index 8c19b19..d8526bf 100644 (file)
@@ -894,14 +894,14 @@ static void mark_all_read_unread_handler(GtkAction *action, gpointer data,
                
        if (read) {
                if (recursive)
-                       folderutils_mark_all_read_recursive(item);
+                       folderutils_mark_all_read_recursive(item, TRUE);
                else
-                       folderutils_mark_all_read(item);
+                       folderutils_mark_all_read(item, TRUE);
        } else {
                if (recursive)
-                       folderutils_mark_all_unread_recursive(item);
+                       folderutils_mark_all_read_recursive(item, FALSE);
                else
-                       folderutils_mark_all_unread(item);
+                       folderutils_mark_all_read(item, FALSE);
        }
        if (folderview->summaryview->folder_item != item && !recursive)
                summary_unlock(folderview->summaryview);
index d6f179e..1fd33a9 100644 (file)
@@ -4251,7 +4251,7 @@ void summary_mark_all_read(SummaryView *summaryview, gboolean ask_if_needed)
        GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
        GtkCMCTreeNode *node;
        gboolean froze = FALSE;
-       
+
        if (summary_is_locked(summaryview))
                return;
 
@@ -4261,16 +4261,16 @@ void summary_mark_all_read(SummaryView *summaryview, gboolean ask_if_needed)
        START_LONG_OPERATION(summaryview, TRUE);
        folder_item_set_batch(summaryview->folder_item, TRUE);
        for (node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list); node != NULL;
-            node = gtkut_ctree_node_next(ctree, node))
+               node = gtkut_ctree_node_next(ctree, node))
                summary_mark_row_as_read(summaryview, node);
        folder_item_set_batch(summaryview->folder_item, FALSE);
        for (node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list); node != NULL;
-            node = gtkut_ctree_node_next(ctree, node)) {
+               node = gtkut_ctree_node_next(ctree, node)) {
                if (!GTK_CMCTREE_ROW(node)->expanded)
                        summary_set_row_marks(summaryview, node);
        }
        END_LONG_OPERATION(summaryview);
-       
+
        summary_status_show(summaryview);
 }
 
@@ -4297,7 +4297,7 @@ void summary_mark_all_unread(SummaryView *summaryview, gboolean ask_if_needed)
        GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
        GtkCMCTreeNode *node;
        gboolean froze = FALSE;
-       
+
        if (summary_is_locked(summaryview))
                return;
 
@@ -4307,16 +4307,16 @@ void summary_mark_all_unread(SummaryView *summaryview, gboolean ask_if_needed)
        START_LONG_OPERATION(summaryview, TRUE);
        folder_item_set_batch(summaryview->folder_item, TRUE);
        for (node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list); node != NULL;
-            node = gtkut_ctree_node_next(ctree, node))
+               node = gtkut_ctree_node_next(ctree, node))
                summary_mark_row_as_unread(summaryview, node);
        folder_item_set_batch(summaryview->folder_item, FALSE);
        for (node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list); node != NULL;
-            node = gtkut_ctree_node_next(ctree, node)) {
+               node = gtkut_ctree_node_next(ctree, node)) {
                if (!GTK_CMCTREE_ROW(node)->expanded)
                        summary_set_row_marks(summaryview, node);
        }
        END_LONG_OPERATION(summaryview);
-       
+
        summary_status_show(summaryview);
 }