[pim/kdepim-runtime] agents/newmailnotifier: Limit history to 200 lines.

Laurent Montel <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit f0701bcc88a387f8b4d504b3b20712ed40429cd6 by Laurent Montel.
Committed on 16/08/2026 at 11:46.
Pushed by mlaurent into branch 'master'.

Limit history to 200 lines.

M  +20   -0    agents/newmailnotifier/autotests/newmailnotificationhistorymanagertest.cpp
M  +1    -0    agents/newmailnotifier/autotests/newmailnotificationhistorymanagertest.h
M  +35   -18   agents/newmailnotifier/newmailnotificationhistorymanager.cpp
M  +10   -1    agents/newmailnotifier/newmailnotificationhistorymanager.h

https://invent.kde.org/pim/kdepim-runtime/-/commit/f0701bcc88a387f8b4d504b3b20712ed40429cd6

diff --git a/agents/newmailnotifier/autotests/newmailnotificationhistorymanagertest.cpp b/agents/newmailnotifier/autotests/newmailnotificationhistorymanagertest.cpp
index 4e1414416..6c53a0faa 100644
--- a/agents/newmailnotifier/autotests/newmailnotificationhistorymanagertest.cpp
+++ b/agents/newmailnotifier/autotests/newmailnotificationhistorymanagertest.cpp
@@ -57,4 +57,24 @@ void NewMailNotificationHistoryManagerTest::generateHtmlFromFolders()
     QCOMPARE(w.joinHistory(), reference);
 }
 
+void NewMailNotificationHistoryManagerTest::shouldDropOldestEntries()
+{
+    NewMailNotificationHistoryManager w;
+    w.setTestModeEnabled(true);
+    w.setMaximumHistorySize(2);
+
+    NewMailNotificationHistoryManager::HistoryMailInfo info;
+    for (int i = 1; i <= 4; ++i) {
+        info.message = QStringLiteral("Mail %1").arg(i);
+        info.identifier = i;
+        w.addEmailInfoNotificationHistory(info);
+    }
+
+    // Only the last two notifications are kept, each one still carrying its own header.
+    const QString entry = QStringLiteral("<b> %1 </b><br>Mail %2 <a href=\"openmail:%2\">[Show Mail]</a><br>");
+    const QString reference = entry.arg(QDate::currentDate().toString(), QString::number(3)) + QStringLiteral("<br>")
+        + entry.arg(QDate::currentDate().toString(), QString::number(4));
+    QCOMPARE(w.joinHistory(), reference);
+}
+
 #include "moc_newmailnotificationhistorymanagertest.cpp"
diff --git a/agents/newmailnotifier/autotests/newmailnotificationhistorymanagertest.h b/agents/newmailnotifier/autotests/newmailnotificationhistorymanagertest.h
index 17099ce7e..484601274 100644
--- a/agents/newmailnotifier/autotests/newmailnotificationhistorymanagertest.h
+++ b/agents/newmailnotifier/autotests/newmailnotificationhistorymanagertest.h
@@ -18,4 +18,5 @@ private Q_SLOTS:
     void shouldHaveDefaultValues();
     void generateHtmlFromUniqueEmail();
     void generateHtmlFromFolders();
+    void shouldDropOldestEntries();
 };
diff --git a/agents/newmailnotifier/newmailnotificationhistorymanager.cpp b/agents/newmailnotifier/newmailnotificationhistorymanager.cpp
index a4039a83c..72782babe 100644
--- a/agents/newmailnotifier/newmailnotificationhistorymanager.cpp
+++ b/agents/newmailnotifier/newmailnotificationhistorymanager.cpp
@@ -40,29 +40,23 @@ QString NewMailNotificationHistoryManager::generateOpenMailStr(Akonadi::Item::Id
 
 void NewMailNotificationHistoryManager::addEmailInfoNotificationHistory(const NewMailNotificationHistoryManager::HistoryMailInfo &info)
 {
-    // qDebug() << "NewMailNotificationHistoryManager::addFoldersInfoNotificationHistory  " << info;
-    addHeader();
-    const QString messageInfo = info.message;
-    const QString message = messageInfo + generateOpenMailStr(info.identifier);
-    mHistory += message + QStringLiteral("<br>");
-    Q_EMIT historyAdded(joinHistory());
+    // qDebug() << "NewMailNotificationHistoryManager::addEmailInfoNotificationHistory  " << info;
+    const QString message = info.message + generateOpenMailStr(info.identifier);
+    appendEntry(header() + QStringLiteral("<br>") + message + QStringLiteral("<br>"));
 }
 
 void NewMailNotificationHistoryManager::addFoldersInfoNotificationHistory(const QList<NewMailNotificationHistoryManager::HistoryFolderInfo> &infos)
 {
     // qDebug() << "NewMailNotificationHistoryManager::addFoldersInfoNotificationHistory  " << infos;
-    addHeader();
     QString messages;
     for (const NewMailNotificationHistoryManager::HistoryFolderInfo &info : infos) {
         if (!messages.isEmpty()) {
             messages += QStringLiteral("<br>");
         }
-        const QString messageInfo = info.message;
-        messages += messageInfo + generateOpenFolderStr(info.identifier);
+        messages += info.message + generateOpenFolderStr(info.identifier);
     }
     messages += QStringLiteral("<br>");
-    mHistory += messages;
-    Q_EMIT historyAdded(joinHistory());
+    appendEntry(header() + QStringLiteral("<br>") + messages);
 }
 
 void NewMailNotificationHistoryManager::setTestModeEnabled(bool test)
@@ -70,15 +64,38 @@ void NewMailNotificationHistoryManager::setTestModeEnabled(bool test)
     mTestEnabled = test;
 }
 
-void NewMailNotificationHistoryManager::addHeader()
+int NewMailNotificationHistoryManager::maximumHistorySize() const
+{
+    return mMaximumHistorySize;
+}
+
+void NewMailNotificationHistoryManager::setMaximumHistorySize(int size)
+{
+    mMaximumHistorySize = qMax(1, size);
+    truncateHistory();
+}
+
+QString NewMailNotificationHistoryManager::header() const
 {
-    // if (!mHistory.isEmpty()) {
-    //     mHistory += QStringLiteral("<br>");
-    // }
     if (mTestEnabled) { // Only for test
-        mHistory += QStringLiteral("<b> %1 </b>").arg(QDate::currentDate().toString());
-    } else {
-        mHistory += QStringLiteral("<b> %1 </b>").arg(QLocale().toString(QDateTime::currentDateTime()));
+        return QStringLiteral("<b> %1 </b>").arg(QDate::currentDate().toString());
+    }
+    return QStringLiteral("<b> %1 </b>").arg(QLocale().toString(QDateTime::currentDateTime()));
+}
+
+// One entry == one notification, so that dropping the oldest ones never splits a
+// timestamp from the message it belongs to.
+void NewMailNotificationHistoryManager::appendEntry(const QString &entry)
+{
+    mHistory.append(entry);
+    truncateHistory();
+    Q_EMIT historyAdded(joinHistory());
+}
+
+void NewMailNotificationHistoryManager::truncateHistory()
+{
+    while (mHistory.count() > mMaximumHistorySize) {
+        mHistory.removeFirst();
     }
 }
 
diff --git a/agents/newmailnotifier/newmailnotificationhistorymanager.h b/agents/newmailnotifier/newmailnotificationhistorymanager.h
index 8d6ae30ef..d953e0233 100644
--- a/agents/newmailnotifier/newmailnotificationhistorymanager.h
+++ b/agents/newmailnotifier/newmailnotificationhistorymanager.h
@@ -26,6 +26,9 @@ public:
     explicit NewMailNotificationHistoryManager(QObject *parent = nullptr);
     ~NewMailNotificationHistoryManager() override;
 
+    // Number of notifications kept in memory before the oldest ones are dropped.
+    static constexpr int defaultMaximumHistorySize = 200;
+
     static NewMailNotificationHistoryManager *self();
 
     void clear();
@@ -36,14 +39,20 @@ public:
     void setTestModeEnabled(bool test);
     [[nodiscard]] QString joinHistory() const;
 
+    [[nodiscard]] int maximumHistorySize() const;
+    void setMaximumHistorySize(int size);
+
 Q_SIGNALS:
     void historyAdded(const QString &str);
 
 private:
     [[nodiscard]] static NEWMAILNOTIFIER_NO_EXPORT QString generateOpenMailStr(Akonadi::Item::Id id);
     [[nodiscard]] static NEWMAILNOTIFIER_NO_EXPORT QString generateOpenFolderStr(Akonadi::Collection::Id id);
-    NEWMAILNOTIFIER_NO_EXPORT void addHeader();
+    [[nodiscard]] NEWMAILNOTIFIER_NO_EXPORT QString header() const;
+    NEWMAILNOTIFIER_NO_EXPORT void appendEntry(const QString &entry);
+    NEWMAILNOTIFIER_NO_EXPORT void truncateHistory();
     QStringList mHistory;
+    int mMaximumHistorySize = defaultMaximumHistorySize;
     // Only for autotest
     bool mTestEnabled = false;
 };
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.