Line data Source code
1 : #pragma once
2 :
3 : #include <QList>
4 : #include <QObject>
5 : #include <QString>
6 : #include <QTimer>
7 :
8 : // NotificationBatcher (67.A2) coalesces new-mail notifications so a
9 : // catch-up sync after hours offline produces one summary popup instead
10 : // of one popup per mail.
11 : //
12 : // Behavior:
13 : // - Suppressed until setSyncComplete() is called: mails added before
14 : // the first INBOX sync of the session finishes are buffered (they
15 : // flush as one batch at sync end) or dropped entirely when the sync
16 : // was the initial mailbox load (initial load != new mail).
17 : // - After that, a debounce timer (default 3 s, restarted on each add)
18 : // coalesces bursts: 1-2 mails flush as individual notifications,
19 : // >= 3 as a single summary.
20 : //
21 : // Pure QObject — no D-Bus. MainWindow connects the flush signals to
22 : // DesktopNotifier and owns the replaces-id of the on-screen summary.
23 : class NotificationBatcher : public QObject {
24 11 : Q_OBJECT
25 :
26 : public:
27 : struct PendingMail {
28 : QString from;
29 : QString subject;
30 : qint64 uid = -1;
31 : qint64 folderId = -1;
32 : };
33 :
34 : explicit NotificationBatcher(QObject *parent = nullptr);
35 :
36 : void setDebounceMs(int ms);
37 1 : int debounceMs() const { return m_debounce.interval(); }
38 :
39 2 : bool isSuppressed() const { return m_suppressed; }
40 3 : int pendingCount() const { return m_pending.size(); }
41 :
42 : // Queue a new mail. While suppressed the mail is buffered; otherwise
43 : // the debounce timer is (re)started.
44 : void addPending(const QString &from, const QString &subject, qint64 uid,
45 : qint64 folderId);
46 :
47 : // First INBOX sync of the session finished. dropBuffered = true when
48 : // the sync was the initial mailbox load (cache was empty — nothing in
49 : // it is "new"); otherwise the buffer flushes immediately as one batch.
50 : void setSyncComplete(bool dropBuffered);
51 :
52 : signals:
53 : // Burst of 1-2 mails: notify each mail individually.
54 : void notifyIndividual(const QString &from, const QString &subject,
55 : qint64 uid, qint64 folderId);
56 : // Burst of >= 3 mails: one summary. newestUid/folderId identify the
57 : // newest mail of the batch (target for the "open" action).
58 : void notifySummary(const QString &title, const QString &body, int count,
59 : qint64 folderId, qint64 newestUid);
60 :
61 : private:
62 : void flush();
63 :
64 : QList<PendingMail> m_pending;
65 : QTimer m_debounce;
66 : bool m_suppressed = true;
67 : };
|