Sophie

Sophie

distrib > Mageia > 5 > i586 > by-pkgid > ddfeee3bedf84e44f20049fdcc070a8a > files > 40

kdepimlibs4-4.14.10-2.2.mga5.src.rpm

From 66e938c2b686c9efb9dfb14339fe1c089816880d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Daniel=20Vr=C3=A1til?= <daniel.vratil@kdab.com>
Date: Tue, 19 Apr 2016 15:47:29 +0200
Subject: [PATCH 39/47] Only enable "Mark All As Read Recursively" action when
 necessary

The action is only visible if the selected collection has any
descendants and is only enabled if the selected collection or
at least one of its descendants has unread emails.
---
 akonadi/kmime/standardmailactionmanager.cpp | 63 +++++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)

diff --git a/akonadi/kmime/standardmailactionmanager.cpp b/akonadi/kmime/standardmailactionmanager.cpp
index 6e886011e..e2d4091bc 100644
--- a/akonadi/kmime/standardmailactionmanager.cpp
+++ b/akonadi/kmime/standardmailactionmanager.cpp
@@ -49,6 +49,7 @@
 #include <kmime/kmime_message.h>
 
 #include <QtCore/QPointer>
+#include <QtCore/QStack>
 #include <QItemSelectionModel>
 
 using namespace Akonadi;
@@ -317,6 +318,58 @@ public:
 
     }
 
+    void checkMarkAllAsReadRecursive(const Collection &rootCollection,
+                                     bool &enableMarkAllAsReadRecursive,
+                                     bool &showMarkAllAsReadRecursive)
+    {
+        enableMarkAllAsReadRecursive = false;
+        showMarkAllAsReadRecursive = false;
+
+        const QAbstractItemModel *model = mCollectionSelectionModel->model();
+        const QModelIndex rootIdx = EntityTreeModel::modelIndexForCollection(model, rootCollection);
+        // This method requires model to be ETM or a proxy on top of ETM
+        if (!rootIdx.isValid()) {
+            return;
+        }
+
+        // If rootCollection does not have any descendants don't even bother
+        // showing the recursive action
+        if (model->rowCount(rootIdx) == 0) {
+            enableMarkAllAsReadRecursive = false;
+            showMarkAllAsReadRecursive = false;
+            return;
+        }
+
+        // We have at least one child, makes sense to show the action
+        showMarkAllAsReadRecursive = true;
+
+        // If rootCollection has unread emails, enable the action (this is a shortcut)
+        if (rootCollection.statistics().unreadCount() > 0) {
+            enableMarkAllAsReadRecursive = true;
+            return;
+        }
+
+        // ... otherwise check if there's at least one descendant with an unread email
+        enableMarkAllAsReadRecursive = false;
+
+        QStack<QModelIndex> stack;
+        stack.push(rootIdx);
+        while (!stack.isEmpty()) {
+            const QModelIndex idx = stack.pop();
+            for (int i = 0, e = model->rowCount(idx); i < e; ++i) {
+                const QModelIndex childIdx = model->index(i, 0, idx);
+                const Collection childCol = model->data(childIdx, EntityTreeModel::CollectionRole).value<Collection>();
+                if (childCol.statistics().unreadCount() > 0) {
+                    // We found at least one child with unread email, enable this action
+                    enableMarkAllAsReadRecursive = true;
+                    return;
+                } else if (model->rowCount(childIdx) > 0) {
+                    stack.push(childIdx);
+                }
+            }
+        }
+    }
+
     void updateActions()
     {
         const Akonadi::Item::List selectedItems = mGenericManager->selectedItems();
@@ -412,6 +465,8 @@ public:
         }
 
         bool enableMarkAllAsRead = false;
+        bool enableMarkAllAsReadRecursive = false;
+        bool showMarkAllAsReadRecursive = false;
         bool enableMarkAllAsUnread = false;
         bool canDeleteItem = true;
         bool isSystemFolder = false;
@@ -436,6 +491,9 @@ public:
                                           collection == SpecialMailCollections::self()->defaultCollection(SpecialMailCollections::Drafts) ||
                                           collection == SpecialMailCollections::self()->defaultCollection(SpecialMailCollections::Templates));
                     }
+
+                    checkMarkAllAsReadRecursive(collection, enableMarkAllAsReadRecursive, showMarkAllAsReadRecursive);
+
                     //We will not change after that.
                     if (enableMarkAllAsRead && enableMarkAllAsUnread && !canDeleteItem && isSystemFolder) {
                         break;
@@ -460,6 +518,11 @@ public:
         if (action) {
             action->setEnabled(enableMarkAllAsRead);
         }
+        action = mActions.value(Akonadi::StandardMailActionManager::MarkAllMailAsReadRecursive);
+        if (action) {
+            action->setEnabled(enableMarkAllAsReadRecursive);
+            action->setVisible(showMarkAllAsReadRecursive);
+        }
 
         action = mActions.value(Akonadi::StandardMailActionManager::MarkAllMailAsUnread);
         if (action) {
-- 
2.14.1