File indexing completed on 2024-04-28 16:54:34

0001 /*
0002     SPDX-FileCopyrightText: 2019 Kai Uwe Broulik <kde@privat.broulik.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QDBusContext>
0010 #include <QDBusObjectPath>
0011 #include <QDBusVariant>
0012 #include <QDateTime>
0013 #include <QObject>
0014 #include <QSharedPointer>
0015 #include <QString>
0016 #include <QTimer>
0017 #include <QUrl>
0018 
0019 #include <chrono>
0020 #include <memory>
0021 
0022 #include "job.h"
0023 #include "notifications.h"
0024 
0025 class KFilePlacesModel;
0026 
0027 namespace NotificationManager
0028 {
0029 class JobPrivate : public QObject, protected QDBusContext
0030 {
0031     Q_OBJECT
0032 
0033 public:
0034     JobPrivate(uint id, QObject *parent);
0035     ~JobPrivate() override;
0036 
0037     enum class ShowCondition {
0038         OnTimeout = 1 << 0,
0039         OnSummary = 1 << 1,
0040         OnTermination = 1 << 2,
0041     };
0042     Q_DECLARE_FLAGS(ShowConditions, ShowCondition)
0043 
0044     QDBusObjectPath objectPath() const;
0045     QUrl descriptionUrl() const;
0046     QString text() const;
0047 
0048     void delayedShow(std::chrono::milliseconds delay, ShowConditions showConditions);
0049     void kill();
0050 
0051     // DBus
0052     // JobViewV1
0053     void terminate(const QString &errorMessage);
0054     void setSuspended(bool suspended);
0055     void setTotalAmount(quint64 amount, const QString &unit);
0056     void setProcessedAmount(quint64 amount, const QString &unit);
0057     void setPercent(uint percent);
0058     void setSpeed(quint64 bytesPerSecond);
0059     void setInfoMessage(const QString &infoMessage);
0060     bool setDescriptionField(uint number, const QString &name, const QString &value);
0061     void clearDescriptionField(uint number);
0062     void setDestUrl(const QDBusVariant &urlVariant);
0063     void setError(uint errorCode);
0064 
0065     // JobViewV2
0066     void terminate(uint errorCode, const QString &errorMessage, const QVariantMap &hints);
0067     void update(const QVariantMap &properties);
0068 
0069 Q_SIGNALS:
0070     void showRequested();
0071     void closed();
0072 
0073     void infoMessageChanged();
0074 
0075     // DBus
0076     // V1 and V2
0077     void suspendRequested();
0078     void resumeRequested();
0079     void cancelRequested();
0080     // V2
0081     void updateRequested();
0082 
0083 private:
0084     friend class Job;
0085 
0086     template<typename T>
0087     bool updateField(const T &newValue, T &target, void (Job::*changeSignal)())
0088     {
0089         if (target != newValue) {
0090             target = newValue;
0091             Q_EMIT((static_cast<Job *>(parent()))->*changeSignal)();
0092             return true;
0093         }
0094         return false;
0095     }
0096 
0097     template<typename T>
0098     bool updateFieldFromProperties(const QVariantMap &properties, const QString &keyName, T &target, void (Job::*changeSignal)())
0099     {
0100         auto it = properties.find(keyName);
0101         if (it == properties.end()) {
0102             return false;
0103         }
0104 
0105         return updateField(it->value<T>(), target, changeSignal);
0106     }
0107 
0108     static std::shared_ptr<KFilePlacesModel> createPlacesModel();
0109 
0110     static QUrl localFileOrUrl(const QString &stringUrl);
0111     static QString linkify(const QUrl &url, const QString &caption);
0112 
0113     void requestShow();
0114 
0115     QUrl destUrl() const;
0116     QString prettyUrl(const QUrl &url) const;
0117     void updateHasDetails();
0118 
0119     void finish();
0120 
0121     QTimer m_showTimer;
0122     ShowConditions m_showConditions = {};
0123     bool m_showRequested = false;
0124 
0125     QTimer *m_killTimer = nullptr;
0126 
0127     uint m_id = 0;
0128     QDBusObjectPath m_objectPath;
0129 
0130     QDateTime m_created;
0131     QDateTime m_updated;
0132 
0133     QString m_summary;
0134     QString m_infoMessage;
0135 
0136     QString m_desktopEntry;
0137     QString m_applicationName;
0138     QString m_applicationIconName;
0139 
0140     Notifications::JobState m_state = Notifications::JobStateRunning;
0141     int m_percentage = 0;
0142     int m_error = 0;
0143     QString m_errorText;
0144     bool m_suspendable = false;
0145     bool m_killable = false;
0146     bool m_transient = false;
0147 
0148     QUrl m_destUrl;
0149 
0150     qulonglong m_speed = 0;
0151 
0152     qulonglong m_processedBytes = 0;
0153     qulonglong m_processedFiles = 0;
0154     qulonglong m_processedDirectories = 0;
0155     qulonglong m_processedItems = 0;
0156 
0157     qulonglong m_totalBytes = 0;
0158     qulonglong m_totalFiles = 0;
0159     qulonglong m_totalDirectories = 0;
0160     qulonglong m_totalItems = 0;
0161 
0162     QString m_descriptionLabel1;
0163     QString m_descriptionValue1;
0164 
0165     QString m_descriptionLabel2;
0166     QString m_descriptionValue2;
0167 
0168     bool m_hasDetails = false;
0169 
0170     bool m_expired = false;
0171     bool m_dismissed = false;
0172 
0173     mutable std::shared_ptr<KFilePlacesModel> m_placesModel;
0174 };
0175 
0176 } // namespace NotificationManager
0177 
0178 Q_DECLARE_OPERATORS_FOR_FLAGS(NotificationManager::JobPrivate::ShowConditions)