Changeset View
Changeset View
Standalone View
Standalone View
src/core/logger/Logger.h
- This file was moved from src/core/interfaces/Logger.h.
Show All 14 Lines | |||||
15 | ****************************************************************************************/ | 15 | ****************************************************************************************/ | ||
16 | 16 | | |||
17 | #ifndef AMAROK_LOGGER_H | 17 | #ifndef AMAROK_LOGGER_H | ||
18 | #define AMAROK_LOGGER_H | 18 | #define AMAROK_LOGGER_H | ||
19 | 19 | | |||
20 | #include "core/amarokcore_export.h" | 20 | #include "core/amarokcore_export.h" | ||
21 | 21 | | |||
22 | #include <QMetaMethod> | 22 | #include <QMetaMethod> | ||
23 | #include <QMutex> | ||||
23 | #include <QObject> | 24 | #include <QObject> | ||
24 | 25 | | |||
25 | #include <functional> | 26 | #include <functional> | ||
26 | 27 | | |||
27 | 28 | | |||
28 | class KJob; | 29 | class KJob; | ||
29 | class QNetworkReply; | 30 | class QNetworkReply; | ||
31 | struct ShortMessage; | ||||
32 | struct LongMessage; | ||||
33 | struct ProgressData; | ||||
30 | 34 | | |||
31 | namespace Amarok | 35 | namespace Amarok | ||
32 | { | 36 | { | ||
33 | /** | 37 | /** | ||
34 | * This interface provides methods that allow backend components to notify the user. | 38 | * This class provides methods that allow backend components to notify the user. | ||
35 | * Users of this class may not make assumptions about the kind of notifications that | 39 | * Users of this class may not make assumptions about the kind of notifications that | ||
36 | * will be sent to the user. | 40 | * will be sent to the user. | ||
37 | * | 41 | * | ||
38 | * The class name is up for discussion btw. | 42 | * The class name is up for discussion btw. | ||
39 | */ | 43 | */ | ||
40 | class AMAROK_CORE_EXPORT Logger | 44 | class AMAROK_CORE_EXPORT Logger | ||
41 | { | 45 | { | ||
42 | public: | 46 | public: | ||
43 | | ||||
44 | enum MessageType { Information, Warning, Error }; | 47 | enum MessageType { Information, Warning, Error }; | ||
45 | 48 | | |||
46 | Logger() {} | 49 | Logger(); | ||
47 | virtual ~Logger() {} | 50 | virtual ~Logger(); | ||
48 | 51 | | |||
49 | /** | 52 | /** | ||
50 | * Informs the user about the progress of a job, i.e. a download job. | 53 | * Informs the user about the progress of a job, i.e. a download job. | ||
51 | * At the very least, the user is notified about the start and end of the job. | 54 | * At the very least, the user is notified about the start and end of the job. | ||
52 | * | 55 | * | ||
53 | * @param job The job whose progress should be monitored | 56 | * @param job The job whose progress should be monitored | ||
54 | * @param text An additional text that will be part of the notification | 57 | * @param text An additional text that will be part of the notification | ||
55 | * @param obj The object that will be called if the user cancels the job. If not set, the job will not be cancellable | 58 | * @param obj The object that will be called if the user cancels the job. If not set, the job will not be cancellable | ||
56 | * @param slot The slot on the given object that will be called if the user cancels the job. No slot will be called if not set. | 59 | * @param slot The slot on the given object that will be called if the user cancels the job. No slot will be called if not set. | ||
57 | * The signal will be emitted from the GUI thread. The receiver may not make assumptions about the sender | 60 | * The signal will be emitted from the GUI thread. The receiver may not make assumptions about the sender | ||
58 | * @param type The Qt connection type to use for the connection to the receiving slot. Defaults to Qt::AutoConnection | 61 | * @param type The Qt connection type to use for the connection to the receiving slot. Defaults to Qt::AutoConnection | ||
59 | */ | 62 | */ | ||
60 | template<class Object = QObject, class Func = void (QObject::*)()> | 63 | template<class Object = QObject, class Func = void (QObject::*)()> | ||
61 | void newProgressOperation( KJob *job, const QString &text, Object *obj = nullptr, Func slot = nullptr, Qt::ConnectionType type = Qt::AutoConnection ) | 64 | static void newProgressOperation( KJob *job, const QString &text, Object *obj = nullptr, Func slot = nullptr, Qt::ConnectionType type = Qt::AutoConnection ) | ||
62 | { | 65 | { | ||
66 | if( !job ) | ||||
67 | return; | ||||
68 | | ||||
63 | std::function<void ()> function = std::bind( slot, obj ); | 69 | std::function<void ()> function = std::bind( slot, obj ); | ||
64 | newProgressOperationImpl( job, text, obj, obj ? function : nullptr, type ); | 70 | addProgressOperation( job, nullptr, nullptr, QMetaMethod(), QMetaMethod(), text, 100, obj, obj ? function : nullptr, type ); | ||
65 | } | 71 | } | ||
66 | 72 | | |||
67 | /** | 73 | /** | ||
68 | * Informs the user about the progress of a job, i.e. a download job. | 74 | * Informs the user about the progress of a job, i.e. a download job. | ||
69 | * At the very least, the user is notified about the start and end of the job. | 75 | * At the very least, the user is notified about the start and end of the job. | ||
70 | * | 76 | * | ||
71 | * @param job The job whose progress should be monitored | 77 | * @param job The job whose progress should be monitored | ||
72 | * @param text An additional text that will be part of the notification | 78 | * @param text An additional text that will be part of the notification | ||
73 | * @param obj The object that will be called if the user cancels the job. | 79 | * @param obj The object that will be called if the user cancels the job. | ||
74 | * @param slot The slot on the given object that will be called if the user cancels the job. | 80 | * @param slot The slot on the given object that will be called if the user cancels the job. | ||
75 | * The signal will be emitted from the GUI thread. The receiver may not make assumptions about the sender | 81 | * The signal will be emitted from the GUI thread. The receiver may not make assumptions about the sender | ||
76 | * @param type The Qt connection type to use for the connection to the receiving slot. | 82 | * @param type The Qt connection type to use for the connection to the receiving slot. | ||
77 | * @param args Arguments given to the slot. | 83 | * @param args Arguments given to the slot. | ||
78 | */ | 84 | */ | ||
79 | template<class Object = QObject, class Func = void (QObject::*)(), class... FuncArgs> | 85 | template<class Object = QObject, class Func = void (QObject::*)(), class... FuncArgs> | ||
80 | void newProgressOperation( KJob *job, const QString &text, Object *obj, Func slot, Qt::ConnectionType type, FuncArgs... args ) | 86 | static void newProgressOperation( KJob *job, const QString &text, Object *obj, Func slot, Qt::ConnectionType type, FuncArgs... args ) | ||
81 | { | 87 | { | ||
88 | if( !job ) | ||||
89 | return; | ||||
90 | | ||||
82 | std::function<void ()> function = std::bind( slot, obj, args... ); | 91 | std::function<void ()> function = std::bind( slot, obj, args... ); | ||
83 | newProgressOperationImpl( job, text, obj, obj ? function : nullptr, type ); | 92 | addProgressOperation( job, nullptr, nullptr, QMetaMethod(), QMetaMethod(), text, 100, obj, obj ? function : nullptr, type ); | ||
84 | } | 93 | } | ||
85 | 94 | | |||
86 | /** | 95 | /** | ||
87 | * Informs the user about the progress of a network request. | 96 | * Informs the user about the progress of a network request. | ||
88 | * At the very least, the user is notified about the start and end of the request. | 97 | * At the very least, the user is notified about the start and end of the request. | ||
89 | * | 98 | * | ||
90 | * @param reply The network reply object whose progress should be monitored | 99 | * @param reply The network reply object whose progress should be monitored | ||
91 | * @param text An additional text that will be part of the notification | 100 | * @param text An additional text that will be part of the notification | ||
92 | * @param obj The object that will be called if the user cancels the network request. If not set, the progress will not be cancellable | 101 | * @param obj The object that will be called if the user cancels the network request. If not set, the progress will not be cancellable | ||
93 | * @param slot The slot on the given object that will be called if the user cancels the network request. No slot will be called if not set. | 102 | * @param slot The slot on the given object that will be called if the user cancels the network request. No slot will be called if not set. | ||
94 | * The signal will be emitted from the GUI thread. The receiver may not make assumptions about the sender | 103 | * The signal will be emitted from the GUI thread. The receiver may not make assumptions about the sender | ||
95 | * @param type The Qt connection type to use for the connection to the receiving slot. Defaults to Qt::AutoConnection | 104 | * @param type The Qt connection type to use for the connection to the receiving slot. Defaults to Qt::AutoConnection | ||
96 | */ | 105 | */ | ||
97 | template<class Object = QObject, class Func = void (QObject::*)()> | 106 | template<class Object = QObject, class Func = void (QObject::*)()> | ||
98 | void newProgressOperation( QNetworkReply *reply, const QString &text, Object *obj = nullptr, Func slot = nullptr, Qt::ConnectionType type = Qt::AutoConnection ) | 107 | static void newProgressOperation( QNetworkReply *reply, const QString &text, Object *obj = nullptr, Func slot = nullptr, Qt::ConnectionType type = Qt::AutoConnection ) | ||
99 | { | 108 | { | ||
109 | if( !reply ) | ||||
110 | return; | ||||
111 | | ||||
100 | std::function<void ()> function = std::bind( slot, obj ); | 112 | std::function<void ()> function = std::bind( slot, obj ); | ||
101 | newProgressOperationImpl( reply, text, obj, obj ? function : nullptr, type ); | 113 | addProgressOperation( nullptr, reply, nullptr, QMetaMethod(), QMetaMethod(), text, 100, obj, obj ? function : nullptr, type ); | ||
102 | } | 114 | } | ||
103 | 115 | | |||
104 | /** | 116 | /** | ||
105 | * Informs the user about the progress of a network request. | 117 | * Informs the user about the progress of a network request. | ||
106 | * At the very least, the user is notified about the start and end of the request. | 118 | * At the very least, the user is notified about the start and end of the request. | ||
107 | * | 119 | * | ||
108 | * @param reply The network reply object whose progress should be monitored | 120 | * @param reply The network reply object whose progress should be monitored | ||
109 | * @param text An additional text that will be part of the notification | 121 | * @param text An additional text that will be part of the notification | ||
110 | * @param obj The object that will be called if the user cancels the network request. | 122 | * @param obj The object that will be called if the user cancels the network request. | ||
111 | * @param slot The slot on the given object that will be called if the user cancels the network request. | 123 | * @param slot The slot on the given object that will be called if the user cancels the network request. | ||
112 | * The signal will be emitted from the GUI thread. The receiver may not make assumptions about the sender | 124 | * The signal will be emitted from the GUI thread. The receiver may not make assumptions about the sender | ||
113 | * @param type The Qt connection type to use for the connection to the receiving slot. | 125 | * @param type The Qt connection type to use for the connection to the receiving slot. | ||
114 | * @param args Arguments given to the slot. | 126 | * @param args Arguments given to the slot. | ||
115 | */ | 127 | */ | ||
116 | template<class Object = QObject, class Func = void (QObject::*)(), class... FuncArgs> | 128 | template<class Object = QObject, class Func = void (QObject::*)(), class... FuncArgs> | ||
117 | void newProgressOperation( QNetworkReply *reply, const QString &text, Object *obj, Func slot, Qt::ConnectionType type, FuncArgs... args ) | 129 | static void newProgressOperation( QNetworkReply *reply, const QString &text, Object *obj, Func slot, Qt::ConnectionType type, FuncArgs... args ) | ||
118 | { | 130 | { | ||
131 | if( !reply ) | ||||
132 | return; | ||||
133 | | ||||
119 | std::function<void ()> function = std::bind( slot, obj, args... ); | 134 | std::function<void ()> function = std::bind( slot, obj, args... ); | ||
120 | newProgressOperationImpl( reply, text, obj, obj ? function : nullptr, type ); | 135 | addProgressOperation( nullptr, reply, nullptr, QMetaMethod(), QMetaMethod(), text, 100, obj, obj ? function : nullptr, type ); | ||
121 | } | 136 | } | ||
122 | 137 | | |||
123 | /** | 138 | /** | ||
124 | * Informs the user about the progress of a generic QObject | 139 | * Informs the user about the progress of a generic QObject | ||
125 | * | 140 | * | ||
126 | * @param sender The object sending the required signals. This sender must emit signals | 141 | * @param sender The object sending the required signals. This sender must emit signals | ||
127 | * incrementProgress() and endProgressOperation() and optionally totalSteps(). | 142 | * incrementProgress() and endProgressOperation() and optionally totalSteps(). | ||
128 | * @param text An additional text that will be part of the notification | 143 | * @param text An additional text that will be part of the notification | ||
129 | * @param maximum The maximum value of the progress operation | 144 | * @param maximum The maximum value of the progress operation | ||
130 | * @param obj The object that will be called if the user cancels the network request. If not | 145 | * @param obj The object that will be called if the user cancels the network request. If not | ||
131 | * set, the progress will not be cancellable | 146 | * set, the progress will not be cancellable | ||
132 | * @param slot The slot on the given object that will be called if the user cancels the | 147 | * @param slot The slot on the given object that will be called if the user cancels the | ||
133 | * network request. No slot will be called if not set. | 148 | * network request. No slot will be called if not set. | ||
134 | * The signal will be emitted from the GUI thread. The receiver may not make assumptions | 149 | * The signal will be emitted from the GUI thread. The receiver may not make assumptions | ||
135 | * about the sender | 150 | * about the sender | ||
136 | * @param type The Qt connection type to use for the connection to the receiving slot. | 151 | * @param type The Qt connection type to use for the connection to the receiving slot. | ||
137 | * Defaults to Qt::AutoConnection | 152 | * Defaults to Qt::AutoConnection | ||
138 | */ | 153 | */ | ||
139 | template<class Sender, class Object = QObject, class Func = void (QObject::*)()> | 154 | template<class Sender, class Object = QObject, class Func = void (QObject::*)()> | ||
140 | typename std::enable_if<!std::is_convertible<Sender*, KJob*>::value && !std::is_convertible<Sender*, QNetworkReply*>::value && std::is_convertible<Sender*, QObject*>::value>::type | 155 | static typename std::enable_if<!std::is_convertible<Sender*, KJob*>::value && !std::is_convertible<Sender*, QNetworkReply*>::value && std::is_convertible<Sender*, QObject*>::value>::type | ||
141 | newProgressOperation( Sender *sender, const QString &text, int maximum = 100, Object *obj = nullptr, Func slot = nullptr, Qt::ConnectionType type = Qt::AutoConnection) | 156 | newProgressOperation( Sender *sender, const QString &text, int maximum = 100, Object *obj = nullptr, Func slot = nullptr, Qt::ConnectionType type = Qt::AutoConnection) | ||
142 | { | 157 | { | ||
158 | if( !sender ) | ||||
159 | return; | ||||
160 | | ||||
143 | auto increment = QMetaMethod::fromSignal( &Sender::incrementProgress ); | 161 | auto increment = QMetaMethod::fromSignal( &Sender::incrementProgress ); | ||
144 | auto end = QMetaMethod::fromSignal( &Sender::endProgressOperation ); | 162 | auto end = QMetaMethod::fromSignal( &Sender::endProgressOperation ); | ||
145 | | ||||
146 | std::function<void ()> function = std::bind( slot, obj ); | 163 | std::function<void ()> function = std::bind( slot, obj ); | ||
147 | newProgressOperationImpl( sender, increment, end, text, maximum, obj, obj ? function : nullptr, type ); | 164 | addProgressOperation( nullptr, nullptr, sender, increment, end, text, maximum, obj, obj ? function : nullptr, type ); | ||
148 | } | 165 | } | ||
149 | 166 | | |||
150 | /** | 167 | /** | ||
151 | * Informs the user about the progress of a generic QObject | 168 | * Informs the user about the progress of a generic QObject | ||
152 | * | 169 | * | ||
153 | * @param sender The object sending the required signals. This sender must emit signals | 170 | * @param sender The object sending the required signals. This sender must emit signals | ||
154 | * incrementProgress() and endProgressOperation() and optionally totalSteps(). | 171 | * incrementProgress() and endProgressOperation() and optionally totalSteps(). | ||
155 | * @param text An additional text that will be part of the notification | 172 | * @param text An additional text that will be part of the notification | ||
156 | * @param maximum The maximum value of the progress operation | 173 | * @param maximum The maximum value of the progress operation | ||
157 | * @param obj The object that will be called if the user cancels the network request. | 174 | * @param obj The object that will be called if the user cancels the network request. | ||
158 | * @param slot The slot on the given object that will be called if the user cancels the | 175 | * @param slot The slot on the given object that will be called if the user cancels the | ||
159 | * network request. | 176 | * network request. | ||
160 | * The signal will be emitted from the GUI thread. The receiver may not make assumptions | 177 | * The signal will be emitted from the GUI thread. The receiver may not make assumptions | ||
161 | * about the sender | 178 | * about the sender | ||
162 | * @param type The Qt connection type to use for the connection to the receiving slot. | 179 | * @param type The Qt connection type to use for the connection to the receiving slot. | ||
163 | * @param args Arguments given to the slot. | 180 | * @param args Arguments given to the slot. | ||
164 | */ | 181 | */ | ||
165 | template<class Sender, class Object = QObject, class Func = void (QObject::*)(), class... FuncArgs> | 182 | template<class Sender, class Object = QObject, class Func = void (QObject::*)(), class... FuncArgs> | ||
166 | typename std::enable_if<!std::is_convertible<Sender*, KJob*>::value && !std::is_convertible<Sender*, QNetworkReply*>::value && std::is_convertible<Sender*, QObject*>::value>::type | 183 | static typename std::enable_if<!std::is_convertible<Sender*, KJob*>::value && !std::is_convertible<Sender*, QNetworkReply*>::value && std::is_convertible<Sender*, QObject*>::value>::type | ||
167 | newProgressOperation( Sender *sender, const QString &text, int maximum, Object *obj, Func slot, Qt::ConnectionType type, FuncArgs... args ) | 184 | newProgressOperation( Sender *sender, const QString &text, int maximum, Object *obj, Func slot, Qt::ConnectionType type, FuncArgs... args ) | ||
168 | { | 185 | { | ||
186 | if( !sender ) | ||||
187 | return; | ||||
188 | | ||||
169 | auto increment = QMetaMethod::fromSignal( &Sender::incrementProgress ); | 189 | auto increment = QMetaMethod::fromSignal( &Sender::incrementProgress ); | ||
170 | auto end = QMetaMethod::fromSignal( &Sender::endProgressOperation ); | 190 | auto end = QMetaMethod::fromSignal( &Sender::endProgressOperation ); | ||
171 | std::function<void ()> function = std::bind( slot, obj, args... ); | 191 | std::function<void ()> function = std::bind( slot, obj, args... ); | ||
172 | newProgressOperationImpl( sender, increment, end, text, maximum, obj, obj ? function : nullptr, type ); | 192 | addProgressOperation( nullptr, nullptr, sender, increment, end, text, maximum, obj, obj ? function : nullptr, type ); | ||
173 | } | 193 | } | ||
174 | 194 | | |||
175 | /** | 195 | /** | ||
176 | * Sends a notification to the user. | 196 | * Sends a notification to the user. | ||
177 | * This method will send a notification containing the given text to the user. | 197 | * This method will send a notification containing the given text to the user. | ||
178 | * | 198 | * | ||
179 | * @param text The text that the notification will contain | 199 | * @param text The text that the notification will contain | ||
180 | */ | 200 | */ | ||
181 | virtual void shortMessage( const QString &text ) = 0; | 201 | static void shortMessage( const QString &text ); | ||
182 | 202 | | |||
183 | /** | 203 | /** | ||
184 | * Send a notification to the user with an additional context. | 204 | * Send a notification to the user with an additional context. | ||
185 | * A notification will be send to the user containing the given text. Additionally, it will convey the context given by @p type. | 205 | * A notification will be send to the user containing the given text. Additionally, it will convey the context given by @p type. | ||
186 | * @param text The text that the notification will contain | 206 | * @param text The text that the notification will contain | ||
187 | * @param type The context of the notification | 207 | * @param type The context of the notification | ||
188 | */ | 208 | */ | ||
189 | virtual void longMessage( const QString &text, MessageType type = Information ) = 0; | 209 | static void longMessage( const QString &text, MessageType type = Information ); | ||
190 | 210 | | |||
211 | protected: | ||||
191 | virtual void newProgressOperationImpl( KJob *job, const QString &text, QObject *context, const std::function<void ()> &function, Qt::ConnectionType type ) = 0; | 212 | virtual void newProgressOperationImpl( KJob *job, const QString &text, QObject *context, const std::function<void ()> &function, Qt::ConnectionType type ) = 0; | ||
192 | virtual void newProgressOperationImpl( QNetworkReply *reply, const QString &text, QObject *context, const std::function<void ()> &function, Qt::ConnectionType type ) = 0; | 213 | virtual void newProgressOperationImpl( QNetworkReply *reply, const QString &text, QObject *context, const std::function<void ()> &function, Qt::ConnectionType type ) = 0; | ||
193 | virtual void newProgressOperationImpl( QObject *sender, const QMetaMethod &increment, const QMetaMethod &end, const QString &text, | 214 | virtual void newProgressOperationImpl( QObject *sender, const QMetaMethod &increment, const QMetaMethod &end, const QString &text, | ||
194 | int maximum, QObject *context, const std::function<void ()> &function, Qt::ConnectionType type ) = 0; | 215 | int maximum, QObject *context, const std::function<void ()> &function, Qt::ConnectionType type ) = 0; | ||
216 | virtual void longMessageImpl( const QString &text, MessageType type = Information ) = 0; | ||||
217 | virtual void shortMessageImpl( const QString &text ) = 0; | ||||
218 | | ||||
219 | private: | ||||
220 | static void addProgressOperation( KJob *job = nullptr, QNetworkReply *reply = nullptr, QObject *sender = nullptr, const QMetaMethod &increment = QMetaMethod(), | ||||
221 | const QMetaMethod &end = QMetaMethod(), const QString &text = QString(), int maximum = 100, QObject *context = nullptr, | ||||
222 | const std::function<void ()> &function = nullptr, Qt::ConnectionType type = Qt::AutoConnection ); | ||||
223 | void loadExistingMessages(); | ||||
224 | | ||||
225 | static QMutex s_mutex; | ||||
226 | static QList<Logger*> s_loggers; | ||||
227 | static QList<QString> s_shortMessageList; | ||||
228 | static QList<LongMessage> s_longMessageList; | ||||
229 | static QList<ProgressData> s_progressList; | ||||
195 | }; | 230 | }; | ||
196 | } | 231 | } | ||
197 | 232 | | |||
198 | #endif | 233 | #endif |