LeechCraft  0.6.70-18450-gabe19ee3b0
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  * 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>
12 #include <interfaces/idatafilter.h>
17 #include <interfaces/ijobholder.h>
18 
19 Q_DECLARE_METATYPE (QVariantList*);
20 
21 namespace LC::Util
22 {
23  Entity MakeAN (const QString& header, const QString& text, Priority priority,
24  const QByteArray& 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 QByteArray& senderID,
45  const QString& cat, const QStringList& types, AN::NotifyFlags flags,
46  bool openConfiguration, const QList<QPair<QString, AN::FieldValue>>& 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 
107  Entity MakeANCancel (const Entity& event)
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 QByteArray& 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 }
Proxy to core entity manager.
constexpr detail::AggregateType< detail::AggregateFunction::Count, Ptr > count
Definition: oral.h:1104
virtual QList< QObject * > GetPossibleHandlers(const LC::Entity &entity)=0
Queries what plugins can handle the given entity.
Q_DECL_IMPORT const QString NotifyAudio
Whether an audio notifier should be enabled by default in the rule being created (bool).
Base interface for data filter plugins.
Definition: idatafilter.h:69
Q_DECL_IMPORT const QString Count
The new total event count (int).
QString Mime_
MIME type of the entity.
Definition: structures.h:149
TaskParameters Parameters_
Parameters of this task.
Definition: structures.h:153
QList< QObject * > GetDataFilters(const QVariant &data, IEntityManager *manager)
Returns the data filter plugins that can handle data.
Definition: util.cpp:71
QVariant Entity_
The entity that this object represents.
Definition: structures.h:113
Entity MakeANCancel(const Entity &event)
Makes an event for canceling another Advanced Notifications event.
Definition: util.cpp:107
Entity MakeEntity(const QVariant &entity, const QString &location, TaskParameters tp, const QString &mime)
Definition: util.cpp:82
Notify by playing back an audio file.
Definition: constants.h:214
Entity MakeANRule(const QString &title, const QByteArray &senderID, const QString &cat, const QStringList &types, AN::NotifyFlags flags, bool openConfiguration, const QList< QPair< QString, AN::FieldValue >> &fields)
Creates an Entity defining an Advanced Notifications rule.
Definition: util.cpp:44
Q_DECL_IMPORT const QString EventID
The ID of the event (QString).
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)...
Rule should be triggered only once.
Definition: constants.h:187
Q_DECL_IMPORT const QString EventCategory
The category of the event (QString).
Q_DECL_IMPORT const QString NotifyPersistent
Whether a persistent notifier should be enabled by default in the rule being created (bool)...
std::shared_ptr< ICoreProxy > ICoreProxy_ptr
Definition: icoreproxy.h:177
Q_DECL_IMPORT const QString IsSingleShot
Whether the created rule should be single-shot (bool).
User should be notified visually via persistent notifications.
Definition: constants.h:210
QMap< QString, QVariant > Additional_
Additional parameters.
Definition: structures.h:165
Priority
Definition: structures.h:190
Q_DECL_IMPORT const QString SenderID
The plugin ID of the sender (QByteArray or QString).
Q_DECL_IMPORT const QString FullText
The detailed text of the event (QString).
Q_DECLARE_METATYPE(QVariantList *)
Q_DECL_IMPORT const QString DeltaCount
The change in event count (int).
constexpr detail::MemberPtrs< Ptrs... > fields
Definition: oral.h:1089
Q_DECL_IMPORT const QString EventType
The type of the event (QString).
A message used for inter-plugin communication.
Definition: structures.h:96
User should be notified visually.
Definition: constants.h:198
Q_DECL_IMPORT const QString OpenConfiguration
Whether configuration dialog should be opened (bool).
QString Location_
Source or destination.
Definition: structures.h:123
Entity MakeNotification(const QString &header, const QString &text, Priority priority)
An utility function to make a Entity with notification.
Definition: util.cpp:95
QVariant GetPersistentData(const QByteArray &key, const ICoreProxy_ptr &proxy)
Returns persistent data stored under given key.
Definition: util.cpp:125
Q_DECL_IMPORT const QString CatEventCancel
Event cancel pseudo-category.
Q_DECL_IMPORT const QString VisualPath
Visual path to this event (QStringList).
Entity MakeAN(const QString &header, const QString &text, Priority priority, const QByteArray &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