LeechCraft 0.6.70-16373-g319c272718
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
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 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7 **********************************************************************/
8
9#include "util.h"
10#include <QStandardItem>
11#include <util/sll/qtutil.h>
18
19Q_DECLARE_METATYPE (QVariantList*);
20
21namespace LC::Util
22{
23 Entity MakeAN (const QString& header, const QString& text, Priority priority,
24 const QString& senderID, const QString& cat, const QString& type,
25 const QString& id, const QStringList& visualPath,
26 int delta, int count,
27 const QString& fullText, const QString& extendedText)
28 {
29 auto e = MakeNotification (header, text, priority);
30 e.Additional_ [AN::EF::SenderID] = senderID;
31 e.Additional_ [AN::EF::EventCategory] = cat;
32 e.Additional_ [AN::EF::EventID] = id;
33 e.Additional_ [AN::EF::VisualPath] = visualPath;
34 e.Additional_ [AN::EF::EventType] = type;
35 e.Additional_ [AN::EF::FullText] = fullText.isNull () ? text : fullText;
36 e.Additional_ [AN::EF::ExtendedText] = extendedText.isNull () ? text : extendedText;
37 if (delta)
38 e.Additional_ [AN::EF::DeltaCount] = delta;
39 else
40 e.Additional_ [AN::EF::Count] = count;
41 return e;
42 }
43
44 Entity MakeANRule (const QString& title, const QString& senderID,
45 const QString& cat, const QStringList& types, AN::NotifyFlags flags,
46 bool openConfiguration, const QList<QPair<QString, ANFieldValue>>& fields)
47 {
48 auto e = MakeNotification (title, {}, {});
49 e.Additional_ [AN::EF::SenderID] = senderID;
50 e.Additional_ [AN::EF::EventID] = QStringLiteral ("org.LC.AdvNotifications.RuleRegister");
51 e.Additional_ [AN::EF::EventCategory] = cat;
52 e.Additional_ [AN::EF::EventType] = types;
53 e.Additional_ [AN::EF::OpenConfiguration] = openConfiguration;
54 e.Mime_ += "-rule-create";
55
56 for (const auto& field : fields)
57 e.Additional_ [field.first] = QVariant::fromValue (field.second);
58
59 if (flags & AN::NotifySingleShot)
60 e.Additional_ [AN::EF::IsSingleShot] = true;
61 if (flags & AN::NotifyTransient)
62 e.Additional_ [AN::EF::NotifyTransient] = true;
63 if (flags & AN::NotifyPersistent)
64 e.Additional_ [AN::EF::NotifyPersistent] = true;
65 if (flags & AN::NotifyAudio)
66 e.Additional_ [AN::EF::NotifyAudio] = true;
67
68 return e;
69 }
70
71 QList<QObject*> GetDataFilters (const QVariant& data, IEntityManager* manager)
72 {
73 const auto& e = MakeEntity (data, QString (), {}, QStringLiteral ("x-leechcraft/data-filter-request"));
74 const auto& handlers = manager->GetPossibleHandlers (e);
75
76 QList<QObject*> result;
77 std::copy_if (handlers.begin (), handlers.end (), std::back_inserter (result),
78 [] (QObject *obj) { return qobject_cast<IDataFilter*> (obj); });
79 return result;
80 }
81
82 Entity MakeEntity (const QVariant& entity,
83 const QString& location,
84 TaskParameters tp,
85 const QString& mime)
86 {
87 Entity result;
88 result.Entity_ = entity;
89 result.Location_ = location;
90 result.Parameters_ = tp;
91 result.Mime_ = mime;
92 return result;
93 }
94
95 Entity MakeNotification (const QString& header,
96 const QString& text, Priority priority)
97 {
98 auto result = MakeEntity (header,
99 {},
101 QStringLiteral ("x-leechcraft/notification"));
102 result.Additional_ [QStringLiteral ("Text")] = text;
103 result.Additional_ [QStringLiteral ("Priority")] = QVariant::fromValue (priority);
104 return result;
105 }
106
108 {
109 Entity e = MakeNotification (event.Entity_.toString (), QString (), Priority::Info);
110 e.Additional_ [AN::EF::SenderID] = event.Additional_ [AN::EF::SenderID];
111 e.Additional_ [AN::EF::EventID] = event.Additional_ [AN::EF::EventID];
113 return e;
114 }
115
116 Entity MakeANCancel (const QString& senderId, const QString& eventId)
117 {
118 Entity e = MakeNotification (QString (), QString (), Priority::Info);
119 e.Additional_ [AN::EF::SenderID] = senderId;
120 e.Additional_ [AN::EF::EventID] = eventId;
122 return e;
123 }
124
125 QVariant GetPersistentData (const QByteArray& key,
126 const ICoreProxy_ptr& proxy)
127 {
128 const auto& plugins = proxy->GetPluginsManager ()->
129 GetAllCastableTo<IPersistentStoragePlugin*> ();
130 for (const auto plug : plugins)
131 {
132 const auto& storage = plug->RequestStorage ();
133 if (!storage)
134 continue;
135
136 const auto& value = storage->Get (key);
137 if (!value.isNull ())
138 return value;
139 }
140 return {};
141 }
142
144 qint64 done, qint64 total, const QString& text)
145 {
146 const auto item = row.value (JobHolderColumn::JobProgress);
147 if (text.contains ("%1"_ql) && text.contains ("%2"_ql))
148 item->setText (text.arg (done).arg (total));
149 else
150 item->setText (text);
151 SetJobHolderProgress (item, done, total);
152 }
153
154 void SetJobHolderProgress (QStandardItem *item, qint64 done, qint64 total)
155 {
156 auto data = item->data (JobHolderRole::ProcessState).value<ProcessStateInfo> ();
157 data.Done_ = done;
158 data.Total_ = total;
159 item->setData (QVariant::fromValue (data), JobHolderRole::ProcessState);
160 }
161
163 {
164 for (const auto item : row)
165 {
166 item->setEditable (false);
167 item->setData (QVariant::fromValue<JobHolderRow> (JobHolderRow::ProcessProgress),
169 }
170
171 const auto item = row.value (JobHolderColumn::JobProgress);
172
173 const ProcessStateInfo state { 0, 0, {}, ProcessStateInfo::State::Running };
174 item->setData (QVariant::fromValue (state), JobHolderRole::ProcessState);
175 }
176}
virtual IPluginsManager * GetPluginsManager() const =0
Returns the application's plugin manager.
Proxy to core entity manager.
virtual QList< QObject * > GetPossibleHandlers(const LC::Entity &entity)=0
Queries what plugins can handle the given entity.
std::shared_ptr< ICoreProxy > ICoreProxy_ptr
Definition icoreproxy.h:181
Q_DECL_IMPORT const QString Count
The new total event count (int).
Q_DECL_IMPORT const QString EventID
The ID of the event (QString).
Q_DECL_IMPORT const QString SenderID
The plugin ID of the sender (QByteArray or QString).
Q_DECL_IMPORT const QString OpenConfiguration
Whether configuration dialog should be opened (bool).
Q_DECL_IMPORT const QString NotifyPersistent
Whether a persistent notifier should be enabled by default in the rule being created (bool).
Q_DECL_IMPORT const QString VisualPath
Visual path to this event (QStringList).
Q_DECL_IMPORT const QString NotifyAudio
Whether an audio notifier should be enabled by default in the rule being created (bool).
Q_DECL_IMPORT const QString ExtendedText
The even more detailed text than FullText (QString).
Q_DECL_IMPORT const QString NotifyTransient
Whether a transient notifier should be enabled by default in the rule being created (bool).
Q_DECL_IMPORT const QString EventCategory
The category of the event (QString).
Q_DECL_IMPORT const QString FullText
The detailed text of the event (QString).
Q_DECL_IMPORT const QString DeltaCount
The change in event count (int).
Q_DECL_IMPORT const QString EventType
The type of the event (QString).
Q_DECL_IMPORT const QString IsSingleShot
Whether the created rule should be single-shot (bool).
Q_DECL_IMPORT const QString CatEventCancel
Event cancel pseudo-category.
@ NotifySingleShot
Rule should be triggered only once.
Definition constants.h:187
@ NotifyTransient
User should be notified visually.
Definition constants.h:198
@ NotifyAudio
Notify by playing back an audio file.
Definition constants.h:214
@ NotifyPersistent
User should be notified visually via persistent notifications.
Definition constants.h:210
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:23
void InitJobHolderRow(const QList< QStandardItem * > &row)
Definition util.cpp:162
QVariant GetPersistentData(const QByteArray &key, const ICoreProxy_ptr &proxy)
Returns persistent data stored under given key.
Definition util.cpp:125
Entity MakeNotification(const QString &header, const QString &text, Priority priority)
An utility function to make a Entity with notification.
Definition util.cpp:95
Entity MakeANCancel(const Entity &event)
Makes an event for canceling another Advanced Notifications event.
Definition util.cpp:107
void SetJobHolderProgress(const QList< QStandardItem * > &row, qint64 done, qint64 total, const QString &text)
Sets the progress values on the given row.
Definition util.cpp:143
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:44
QList< QObject * > GetDataFilters(const QVariant &data, IEntityManager *manager)
Returns the data filter plugins that can handle data.
Definition util.cpp:71
Entity MakeEntity(const QVariant &entity, const QString &location, TaskParameters tp, const QString &mime)
Definition util.cpp:82
@ JobProgress
The column with the progress of the task, like the amount of data downloaded so far or last update.
Definition ijobholder.h:39
@ ProcessState
Describes the state of a process.
Definition ijobholder.h:196
@ RoleJobHolderRow
Definition structures.h:209
Priority
Definition structures.h:215
@ AutoAccept
Definition structures.h:78
@ OnlyHandle
Definition structures.h:68
@ ProcessProgress
Definition ijobholder.h:76
A message used for inter-plugin communication.
Definition structures.h:96
QString Mime_
MIME type of the entity.
Definition structures.h:148
QString Location_
Source or destination.
Definition structures.h:122
QVariant Entity_
The entity that this object represents.
Definition structures.h:112
TaskParameters Parameters_
Parameters of this task.
Definition structures.h:152
QMap< QString, QVariant > Additional_
Additional parameters.
Definition structures.h:164
State of a single process represented in a IJobHolder model.
Definition ijobholder.h:93
qlonglong Done_
The amount of items already processed or downloaded.
Definition ijobholder.h:100
@ Running
The process is running just fine.
Definition ijobholder.h:130
Q_DECLARE_METATYPE(QVariantList *)