1 /*
2     SPDX-FileCopyrightText: 2019 Kai Uwe Broulik <kde@privat.broulik.de>
3 
4     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6 
7 #pragma once
8 
9 #include <QDBusContext>
10 #include <QDBusObjectPath>
11 #include <QDBusVariant>
12 #include <QDateTime>
13 #include <QObject>
14 #include <QSharedPointer>
15 #include <QString>
16 #include <QTimer>
17 #include <QUrl>
18 
19 #include <chrono>
20 
21 #include "job.h"
22 #include "notifications.h"
23 
24 class KFilePlacesModel;
25 
26 namespace NotificationManager
27 {
28 class JobPrivate : public QObject, protected QDBusContext
29 {
30     Q_OBJECT
31 
32 public:
33     JobPrivate(uint id, QObject *parent);
34     ~JobPrivate() override;
35 
36     enum class ShowCondition {
37         OnTimeout = 1 << 0,
38         OnSummary = 1 << 1,
39         OnTermination = 1 << 2,
40     };
41     Q_DECLARE_FLAGS(ShowConditions, ShowCondition)
42 
43     QDBusObjectPath objectPath() const;
44     QUrl descriptionUrl() const;
45     QString text() const;
46 
47     void delayedShow(std::chrono::milliseconds delay, ShowConditions showConditions);
48     void kill();
49 
50     // DBus
51     // JobViewV1
52     void terminate(const QString &errorMessage);
53     void setSuspended(bool suspended);
54     void setTotalAmount(quint64 amount, const QString &unit);
55     void setProcessedAmount(quint64 amount, const QString &unit);
56     void setPercent(uint percent);
57     void setSpeed(quint64 bytesPerSecond);
58     void setInfoMessage(const QString &infoMessage);
59     bool setDescriptionField(uint number, const QString &name, const QString &value);
60     void clearDescriptionField(uint number);
61     void setDestUrl(const QDBusVariant &urlVariant);
62     void setError(uint errorCode);
63 
64     // JobViewV2
65     void terminate(uint errorCode, const QString &errorMessage, const QVariantMap &hints);
66     void update(const QVariantMap &properties);
67 
68 Q_SIGNALS:
69     void showRequested();
70     void closed();
71 
72     void infoMessageChanged();
73 
74     // DBus
75     // V1 and V2
76     void suspendRequested();
77     void resumeRequested();
78     void cancelRequested();
79     // V2
80     void updateRequested();
81 
82 private:
83     friend class Job;
84 
85     template<typename T>
updateField(const T & newValue,T & target,void (Job::* changeSignal)())86     bool updateField(const T &newValue, T &target, void (Job::*changeSignal)())
87     {
88         if (target != newValue) {
89             target = newValue;
90             emit((static_cast<Job *>(parent()))->*changeSignal)();
91             return true;
92         }
93         return false;
94     }
95 
96     template<typename T>
updateFieldFromProperties(const QVariantMap & properties,const QString & keyName,T & target,void (Job::* changeSignal)())97     bool updateFieldFromProperties(const QVariantMap &properties, const QString &keyName, T &target, void (Job::*changeSignal)())
98     {
99         auto it = properties.find(keyName);
100         if (it == properties.end()) {
101             return false;
102         }
103 
104         return updateField(it->value<T>(), target, changeSignal);
105     }
106 
107     static QSharedPointer<KFilePlacesModel> createPlacesModel();
108 
109     static QUrl localFileOrUrl(const QString &stringUrl);
110 
111     void requestShow();
112 
113     QUrl destUrl() const;
114     QString prettyUrl(const QUrl &url) const;
115     void updateHasDetails();
116 
117     void finish();
118 
119     QTimer m_showTimer;
120     ShowConditions m_showConditions = {};
121     bool m_showRequested = false;
122 
123     QTimer *m_killTimer = nullptr;
124 
125     uint m_id = 0;
126     QDBusObjectPath m_objectPath;
127 
128     QDateTime m_created;
129     QDateTime m_updated;
130 
131     QString m_summary;
132     QString m_infoMessage;
133 
134     QString m_desktopEntry;
135     QString m_applicationName;
136     QString m_applicationIconName;
137 
138     Notifications::JobState m_state = Notifications::JobStateRunning;
139     int m_percentage = 0;
140     int m_error = 0;
141     QString m_errorText;
142     bool m_suspendable = false;
143     bool m_killable = false;
144 
145     QUrl m_destUrl;
146 
147     qulonglong m_speed = 0;
148 
149     qulonglong m_processedBytes = 0;
150     qulonglong m_processedFiles = 0;
151     qulonglong m_processedDirectories = 0;
152     qulonglong m_processedItems = 0;
153 
154     qulonglong m_totalBytes = 0;
155     qulonglong m_totalFiles = 0;
156     qulonglong m_totalDirectories = 0;
157     qulonglong m_totalItems = 0;
158 
159     QString m_descriptionLabel1;
160     QString m_descriptionValue1;
161 
162     QString m_descriptionLabel2;
163     QString m_descriptionValue2;
164 
165     bool m_hasDetails = false;
166 
167     bool m_expired = false;
168     bool m_dismissed = false;
169 
170     mutable QSharedPointer<KFilePlacesModel> m_placesModel;
171 };
172 
173 } // namespace NotificationManager
174 
175 Q_DECLARE_OPERATORS_FOR_FLAGS(NotificationManager::JobPrivate::ShowConditions)
176