LeechCraft  0.6.70-10870-g558588d6ec
Modular cross-platform feature rich live environment.
util.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #include "util.h"
31 #include <QStandardItem>
32 #include <util/util.h>
33 #include <interfaces/idatafilter.h>
38 #include <interfaces/ijobholder.h>
39 
40 Q_DECLARE_METATYPE (QVariantList*);
41 
42 namespace LeechCraft
43 {
44 namespace Util
45 {
46  Entity MakeAN (const QString& header, const QString& text, Priority priority,
47  const QString& senderID, const QString& cat, const QString& type,
48  const QString& id, const QStringList& visualPath,
49  int delta, int count,
50  const QString& fullText, const QString& extendedText)
51  {
52  auto e = MakeNotification (header, text, priority);
53  e.Additional_ [AN::EF::SenderID] = senderID;
54  e.Additional_ [AN::EF::EventCategory] = cat;
55  e.Additional_ [AN::EF::EventID] = id;
56  e.Additional_ [AN::EF::VisualPath] = visualPath;
57  e.Additional_ [AN::EF::EventType] = type;
58  e.Additional_ [AN::EF::FullText] = fullText.isNull () ? text : fullText;
59  e.Additional_ [AN::EF::ExtendedText] = extendedText.isNull () ? text : extendedText;
60  if (delta)
61  e.Additional_ [AN::EF::DeltaCount] = delta;
62  else
63  e.Additional_ [AN::EF::Count] = count;
64  return e;
65  }
66 
67  Entity MakeANRule (const QString& title, const QString& senderID,
68  const QString& cat, const QStringList& types, AN::NotifyFlags flags,
69  bool openConfiguration, const QList<QPair<QString, ANFieldValue>>& fields)
70  {
71  auto e = MakeNotification (title, {}, PLog_);
72  e.Additional_ [AN::EF::SenderID] = senderID;
73  e.Additional_ [AN::EF::EventID] = "org.LC.AdvNotifications.RuleRegister";
74  e.Additional_ [AN::EF::EventCategory] = cat;
75  e.Additional_ [AN::EF::EventType] = types;
76  e.Additional_ [AN::EF::OpenConfiguration] = openConfiguration;
77  e.Mime_ += "-rule-create";
78 
79  for (const auto& field : fields)
80  e.Additional_ [field.first] = QVariant::fromValue (field.second);
81 
82  if (flags & AN::NotifySingleShot)
83  e.Additional_ [AN::EF::IsSingleShot] = true;
84  if (flags & AN::NotifyTransient)
85  e.Additional_ [AN::EF::NotifyTransient] = true;
86  if (flags & AN::NotifyPersistent)
87  e.Additional_ [AN::EF::NotifyPersistent] = true;
88  if (flags & AN::NotifyAudio)
89  e.Additional_ [AN::EF::NotifyAudio] = true;
90 
91  return e;
92  }
93 
94  QList<QObject*> GetDataFilters (const QVariant& data, IEntityManager* manager)
95  {
96  const auto& e = MakeEntity (data, QString (), {}, "x-leechcraft/data-filter-request");
97  const auto& handlers = manager->GetPossibleHandlers (e);
98 
99  QList<QObject*> result;
100  std::copy_if (handlers.begin (), handlers.end (), std::back_inserter (result),
101  [] (QObject *obj) { return qobject_cast<IDataFilter*> (obj); });
102  return result;
103  }
104 
105  Entity MakeEntity (const QVariant& entity,
106  const QString& location,
107  TaskParameters tp,
108  const QString& mime)
109  {
110  Entity result;
111  result.Entity_ = entity;
112  result.Location_ = location;
113  result.Parameters_ = tp;
114  result.Mime_ = mime;
115  return result;
116  }
117 
118  Entity MakeNotification (const QString& header,
119  const QString& text, Priority priority)
120  {
121  Entity result = MakeEntity (header,
122  QString (),
124  "x-leechcraft/notification");
125  result.Additional_ ["Text"] = text;
126  result.Additional_ ["Priority"] = priority;
127  return result;
128  }
129 
130  Entity MakeANCancel (const Entity& event)
131  {
132  Entity e = MakeNotification (event.Entity_.toString (), QString (), PInfo_);
133  e.Additional_ [AN::EF::SenderID] = event.Additional_ [AN::EF::SenderID];
134  e.Additional_ [AN::EF::EventID] = event.Additional_ [AN::EF::EventID];
136  return e;
137  }
138 
139  Entity MakeANCancel (const QString& senderId, const QString& eventId)
140  {
141  Entity e = MakeNotification (QString (), QString (), PInfo_);
142  e.Additional_ [AN::EF::SenderID] = senderId;
143  e.Additional_ [AN::EF::EventID] = eventId;
145  return e;
146  }
147 
148  QVariant GetPersistentData (const QByteArray& key,
149  const ICoreProxy_ptr& proxy)
150  {
151  const auto& plugins = proxy->GetPluginsManager ()->
152  GetAllCastableTo<IPersistentStoragePlugin*> ();
153  for (const auto plug : plugins)
154  {
155  const auto& storage = plug->RequestStorage ();
156  if (!storage)
157  continue;
158 
159  const auto& value = storage->Get (key);
160  if (!value.isNull ())
161  return value;
162  }
163  return {};
164  }
165 
167  qint64 done, qint64 total, const QString& text)
168  {
169  const auto item = row.value (JobHolderColumn::JobProgress);
170  if (text.contains ("%1") && text.contains ("%2"))
171  item->setText (text.arg (done).arg (total));
172  else
173  item->setText (text);
174  SetJobHolderProgress (item, done, total);
175  }
176 
177  void SetJobHolderProgress (QStandardItem *item, qint64 done, qint64 total)
178  {
179  auto data = item->data (JobHolderRole::ProcessState).value<ProcessStateInfo> ();
180  data.Done_ = done;
181  data.Total_ = total;
182  item->setData (QVariant::fromValue (data), JobHolderRole::ProcessState);
183  }
184 
186  {
187  for (const auto item : row)
188  {
189  item->setEditable (false);
190  item->setData (QVariant::fromValue<JobHolderRow> (JobHolderRow::ProcessProgress),
192  }
193 
194  const auto item = row.value (JobHolderColumn::JobProgress);
195 
196  const ProcessStateInfo state { 0, 0, {}, ProcessStateInfo::State::Running };
197  item->setData (QVariant::fromValue (state), JobHolderRole::ProcessState);
198  }
199 }
200 }
Entity MakeEntity(const QVariant &entity, const QString &location, TaskParameters tp, const QString &mime)
Definition: util.cpp:105
Proxy to core entity manager.
constexpr detail::AggregateType< detail::AggregateFunction::Count > count
Definition: oral.h:936
const QString EventID
The ID of the event (QString).
Definition: entityfields.h:78
void InitJobHolderRow(const QList< QStandardItem *> &row)
Definition: util.cpp:185
Base interface for data filter plugins.
Definition: idatafilter.h:90
const QString VisualPath
Visual path to this event (QStringList).
Definition: entityfields.h:87
Rule should be triggered only once.
Definition: constants.h:178
const QString SenderID
The plugin ID of the sender (QByteArray or QString).
Definition: entityfields.h:53
const QString Count
The new total event count (int).
Definition: entityfields.h:138
const QString FullText
The detailed text of the event (QString).
Definition: entityfields.h:109
void SetJobHolderProgress(const QList< QStandardItem *> &row, qint64 done, qint64 total, const QString &text)
Sets the progress values on the given row.
Definition: util.cpp:166
const QString EventCategory
The category of the event (QString).
Definition: entityfields.h:67
User should be notified visually.
Definition: constants.h:189
QVariant GetPersistentData(const QByteArray &key, const ICoreProxy_ptr &proxy)
Returns persistent data stored under given key.
Definition: util.cpp:148
const QString CatEventCancel
Event cancel pseudo-category.
Definition: constants.h:43
const QString IsSingleShot
Whether the created rule should be single-shot (bool).
Definition: entityfields.h:152
virtual QList< QObject * > GetPossibleHandlers(const LeechCraft::Entity &entity)=0
Queries what plugins can handle the given entity.
const QString NotifyAudio
Whether an audio notifier should be enabled by default in the rule being created (bool).
Definition: entityfields.h:173
TaskParameters Parameters_
Parameters of this task.
Definition: structures.h:176
Entity MakeANRule(const QString &title, const QString &senderID, const QString &cat, const QStringList &types, AN::NotifyFlags flags, bool openConfiguration, const QList< QPair< QString, ANFieldValue >> &fields)
Creates an Entity defining an Advanced Notifications rule.
Definition: util.cpp:67
const QString DeltaCount
The change in event count (int).
Definition: entityfields.h:128
const QString OpenConfiguration
Whether configuration dialog should be opened (bool).
Definition: entityfields.h:146
Describes the state of a process.
Definition: ijobholder.h:218
std::shared_ptr< ICoreProxy > ICoreProxy_ptr
Definition: icoreproxy.h:225
const QString NotifyTransient
Whether a transient notifier should be enabled by default in the rule being created (bool)...
Definition: entityfields.h:159
The process is running just fine.
Entity MakeNotification(const QString &header, const QString &text, Priority priority)
An utility function to make a Entity with notification.
Definition: util.cpp:118
State of a single process represented in a IJobHolder model.
Definition: ijobholder.h:114
Entity MakeANCancel(const Entity &event)
Makes an event for canceling another Advanced Notifications event.
Definition: util.cpp:130
Q_DECLARE_METATYPE(QVariantList *)
QList< QObject * > GetDataFilters(const QVariant &data, IEntityManager *manager)
Returns the data filter plugins that can handle data.
Definition: util.cpp:94
const QString ExtendedText
The even more detailed text than FullText (QString).
Definition: entityfields.h:115
constexpr detail::MemberPtrs< Ptrs... > fields
Definition: oral.h:934
const QString EventType
The type of the event (QString).
Definition: entityfields.h:103
QMap< QString, QVariant > Additional_
Additional parameters.
Definition: structures.h:188
QString Mime_
MIME type of the entity.
Definition: structures.h:172
qlonglong Done_
The amount of items already processed or downloaded.
Definition: ijobholder.h:122
User should be notified visually via persistent notifications.
Definition: constants.h:201
const QString NotifyPersistent
Whether a persistent notifier should be enabled by default in the rule being created (bool)...
Definition: entityfields.h:166
QVariant Entity_
The entity that this object represents.
Definition: structures.h:136
Notify by playing back an audio file.
Definition: constants.h:205
Entity MakeAN(const QString &header, const QString &text, Priority priority, const QString &senderID, const QString &cat, const QString &type, const QString &id, const QStringList &visualPath, int delta, int count, const QString &fullText, const QString &extendedText)
Creates an Advanced Notifications-enabled notify entity.
Definition: util.cpp:46
A message used for inter-plugin communication.
Definition: structures.h:119
The column with the progress of the task, like the amount of data downloaded so far or last update...
Definition: ijobholder.h:61
QString Location_
Source or destination.
Definition: structures.h:146