Tag Parser  6.5.0
C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags
statusprovider.h
Go to the documentation of this file.
1 #ifndef STATUSPROVIDER_H
2 #define STATUSPROVIDER_H
3 
4 #include "./notification.h"
5 
6 #include <functional>
7 #include <vector>
8 
9 namespace Media {
10 
11 class MediaFileInfo;
12 
14 {
15  // FIXME: make transferNotifications() public in next minor release and get rid of the friend class again
16  friend class MediaFileInfo;
17 
18 public:
19  typedef std::function<void (StatusProvider &sender)> CallbackFunction;
20  typedef std::vector<CallbackFunction> CallbackVector;
21  typedef std::pair<int, CallbackFunction> CallbackPair;
22 
23  const NotificationList &notifications() const;
24  bool hasNotifications() const;
25  bool hasCriticalNotifications() const;
26  NotificationType worstNotificationType() const;
27  const std::string &currentStatus() const;
28  double currentPercentage() const;
29  size_t registerCallback(CallbackFunction callback);
30  void unregisterCallback(size_t id);
31  void unregisterAllCallbacks();
32  void forwardStatusUpdateCalls(StatusProvider *other = nullptr);
33  inline StatusProvider *usedProvider();
34  void tryToAbort();
35  bool isAborted() const;
36  void invalidateStatus();
37  void invalidateNotifications();
38  void updateStatus(const std::string &status);
39  void updateStatus(const std::string &status, double percentage);
40  void updatePercentage(double percentage);
41  void addNotification(const Notification &notification);
42  void addNotification(NotificationType type, const std::string &message, const std::string &context);
43  void addNotifications(const StatusProvider &from);
44  void addNotifications(const std::string &higherContext, const StatusProvider &from);
45  void addNotifications(const NotificationList &notifications);
46 
47 protected:
49 
50 private:
51  inline void invokeCallbacks();
52  inline void updateWorstNotificationType(NotificationType notificationType);
53  inline void transferNotifications(StatusProvider &from);
54 
55  NotificationList m_notifications;
56  NotificationType m_worstNotificationType;
57  std::string m_status;
58  double m_percentage;
59  CallbackVector m_callbacks;
60  bool m_abort;
61  StatusProvider *m_forward;
62 };
63 
67 inline void StatusProvider::updateStatus(const std::string &status)
68 {
69  m_status = status;
70  invokeCallbacks();
71 }
72 
78 inline void StatusProvider::updateStatus(const std::string &status, double percentage)
79 {
80  m_status = status;
81  m_percentage = percentage;
82  invokeCallbacks();
83 }
84 
90 inline void StatusProvider::updatePercentage(double percentage)
91 {
92  m_percentage = percentage;
93  invokeCallbacks();
94 }
95 
102 {
103  return m_forward ? m_forward->usedProvider() : this;
104 }
105 
110 {
111  return m_notifications;
112 }
113 
118 {
119  return !m_notifications.empty();
120 }
121 
126 {
127  return m_worstNotificationType == NotificationType::Critical;
128 }
129 
134 {
135  return m_worstNotificationType;
136 }
137 
141 inline const std::string &StatusProvider::currentStatus() const
142 {
143  if(m_status.empty() && m_forward) {
144  return m_forward->currentStatus();
145  }
146  return m_status;
147 }
148 
153 {
154  if(m_percentage == 0.0 && m_forward) {
155  return m_forward->currentPercentage();
156  }
157  return m_percentage;
158 }
159 
166 inline bool StatusProvider::isAborted() const
167 {
168  return m_abort || (m_forward && m_forward->isAborted());
169 }
170 
178 {
179  if(id < m_callbacks.size()) {
180  m_callbacks[id] = nullptr;
181  }
182 }
183 
190 {
191  m_callbacks.clear();
192 }
193 
215 {
216  m_forward = other;
217 }
218 
227 {
228  m_abort = true;
229 }
230 
241 {
242  m_status.clear();
243  m_percentage = 0.0;
244  m_abort = false;
245 }
246 
253 {
254  m_notifications.clear();
255  m_worstNotificationType = NotificationType::None;
256 }
257 
261 inline void StatusProvider::invokeCallbacks()
262 {
263  for(std::function<void (StatusProvider &sender)> &callback : usedProvider()->m_callbacks) {
264  if(callback) {
265  callback(*this);
266  }
267  }
268 }
269 
273 inline void StatusProvider::updateWorstNotificationType(NotificationType notificationType)
274 {
275  if(static_cast<int>(m_worstNotificationType) < static_cast<int>(notificationType)) {
276  m_worstNotificationType = notificationType;
277  }
278 }
279 
286 void StatusProvider::transferNotifications(StatusProvider &from)
287 {
288  m_notifications.splice(m_notifications.end(), from.m_notifications);
289  m_worstNotificationType |= from.worstNotificationType();
290 }
291 
292 }
293 
294 
295 #endif // STATUSPROVIDER_H
void unregisterAllCallbacks()
Unregisters all callback functions.
bool hasCriticalNotifications() const
Returns an indication whether there are critical notifications for the current object.
void invalidateStatus()
Invalidates the current status.
bool isAborted() const
Returns an indication whether the current operation should be aborted.
const NotificationList & notifications() const
Returns notifications for the current object.
NotificationType
Specifies the notification type.
Definition: notification.h:18
double currentPercentage() const
Returns the progress percentage of the current object.
std::vector< CallbackFunction > CallbackVector
NotificationType worstNotificationType() const
Returns the worst notification type.
void unregisterCallback(size_t id)
Unregisters a previously registered callback function whith the specified id.
void updatePercentage(double percentage)
This method is meant to be called by the derived class to report updated progress percentage only...
bool hasNotifications() const
Returns an indication whether there are notifications for the current object.
void tryToAbort()
Commands the object to abort the current operation.
std::function< void(StatusProvider &sender)> CallbackFunction
std::list< Notification > NotificationList
Definition: notification.h:39
void invalidateNotifications()
Invalidates the object&#39;s notifications.
The MediaFileInfo class allows to read and write tag information providing a container/tag format ind...
Definition: mediafileinfo.h:53
The Notification class holds a notification message of a certain notification type.
Definition: notification.h:43
StatusProvider * usedProvider()
Returns the provider which callback functions will be called when the status or the percentage is upd...
const std::string & currentStatus() const
Returns a status information for the current object.
void updateStatus(const std::string &status)
This method is meant to be called by the derived class to report updated status information.
Contains all classes and functions of the TagInfo library.
Definition: exceptions.h:9
std::pair< int, CallbackFunction > CallbackPair
The StatusProvider class acts as a base class for objects providing status information.
void forwardStatusUpdateCalls(StatusProvider *other=nullptr)
Forwards all status updates calls to the specified statusProvider.
#define TAG_PARSER_EXPORT
Marks the symbol to be exported by the tagparser library.