Python plugin: Example: Make recursive-mark-as-read also work on mailbox
authorHolger Berndt <hb@claws-mail.org>
Mon, 19 Aug 2013 22:13:20 +0000 (00:13 +0200)
committerHolger Berndt <hb@claws-mail.org>
Mon, 19 Aug 2013 22:13:20 +0000 (00:13 +0200)
src/plugins/python/examples/main/Recusively-mark-messages-as-read

index 85c108e..bcb3cb5 100644 (file)
@@ -2,34 +2,38 @@
 
 # Define the function to deal with each folder
 def deal_with_folder(folder):
-    # Get actions for selecting all messages, and marking the selection as read
-    action_group = clawsmail.get_mainwindow_action_group();
-    select_all_action = action_group.get_action("Edit/SelectAll")
-    mark_read_action = action_group.get_action("Message/Mark/MarkRead")
-    
-    # Select given folder
-    clawsmail.folderview_select_folder(folder)
-    
-    # Search for messages with age greater than 28 days
-    clawsmail.quicksearch_search("ag 28", clawsmail.QUICK_SEARCH_EXTENDED)
-    
-    # Mark all messages in the search result as read
-    select_all_action.activate()
-    mark_read_action.activate()
+    if folder.num_unread_messages > 0:
+        # Get actions for selecting all messages, and marking the selection as read
+        action_group = clawsmail.get_mainwindow_action_group();
+        select_all_action = action_group.get_action("Edit/SelectAll")
+        mark_read_action = action_group.get_action("Message/Mark/MarkRead")
+        
+        # Select given folder
+        clawsmail.folderview_select_folder(folder)
+        
+        # Search for messages with age greater than 28 days
+        clawsmail.quicksearch_search("ag 28", clawsmail.QUICK_SEARCH_EXTENDED)
+        
+        # Mark all messages in the search result as read
+        select_all_action.activate()
+        mark_read_action.activate()
 
 
 # Get selected folder
 root = clawsmail.get_folderview_selected_folder()
+if root is None:
+    root = clawsmail.get_folderview_selected_mailbox()
 
-# Get a tree of subfolders. The argument could also be a string of a mailbox name,
-# or left out for a list of mailbox trees.
-tree = clawsmail.get_folder_tree(root)
-
-# Call above function for all folders.
-tree.traverse(deal_with_folder)
-
-# Clear the quicksearch widget again
-clawsmail.quicksearch_clear()
-
-# Change back to original folder
-clawsmail.folderview_select_folder(root)
+if root is not None:
+    # Get a tree of subfolders. The argument could also be a string of a mailbox name,
+    # or left out for a list of mailbox trees.
+    tree = clawsmail.get_folder_tree(root)
+    
+    # Call above function for all folders.
+    tree.traverse(deal_with_folder)
+    
+    # Clear the quicksearch widget again
+    clawsmail.quicksearch_clear()
+    
+    # Change back to original folder
+    clawsmail.folderview_select(root)