Python plugin: Example: Make recursive-mark-as-read also work on mailbox
[claws.git] / src / plugins / python / examples / main / Recusively-mark-messages-as-read
index 85c108ee3218c70b9f6de93572c5ca6e4748ffcf..bcb3cb54b00acc05bf1d364e08375f19618d0ebc 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)