LeechCraft  %{LEECHCRAFT_VERSION}
Modular cross-platform feature rich live environment.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 <util/util.h>
32 #include <interfaces/idatafilter.h>
35 
36 Q_DECLARE_METATYPE (QVariantList*);
37 
38 namespace LeechCraft
39 {
40 namespace Util
41 {
42  Entity MakeAN (const QString& header, const QString& text, Priority priority,
43  const QString& senderID, const QString& cat, const QString& type,
44  const QString& id, const QStringList& visualPath,
45  int delta, int count,
46  const QString& fullText, const QString& extendedText)
47  {
48  auto e = MakeNotification (header, text, priority);
49  e.Additional_ [AN::EF::SenderID] = senderID;
50  e.Additional_ [AN::EF::EventCategory] = cat;
51  e.Additional_ [AN::EF::EventID] = id;
52  e.Additional_ [AN::EF::VisualPath] = visualPath;
53  e.Additional_ [AN::EF::EventType] = type;
54  e.Additional_ [AN::EF::FullText] = fullText.isNull () ? text : fullText;
55  e.Additional_ [AN::EF::ExtendedText] = extendedText.isNull () ? text : extendedText;
56  if (delta)
57  e.Additional_ [AN::EF::DeltaCount] = delta;
58  else
59  e.Additional_ [AN::EF::Count] = count;
60  return e;
61  }
62 
63  Entity MakeANRule (const QString& title, const QString& senderID,
64  const QString& cat, const QStringList& types, AN::NotifyFlags flags,
65  bool openConfiguration, const QList<QPair<QString, ANFieldValue>>& fields)
66  {
67  auto e = MakeNotification (title, {}, PLog_);
68  e.Additional_ [AN::EF::SenderID] = senderID;
69  e.Additional_ [AN::EF::EventID] = "org.LC.AdvNotifications.RuleRegister";
70  e.Additional_ [AN::EF::EventCategory] = cat;
71  e.Additional_ [AN::EF::EventType] = types;
72  e.Additional_ [AN::EF::OpenConfiguration] = openConfiguration;
73  e.Mime_ += "-rule-create";
74 
75  for (const auto& field : fields)
76  e.Additional_ [field.first] = QVariant::fromValue (field.second);
77 
78  if (flags & AN::NotifySingleShot)
79  e.Additional_ [AN::EF::IsSingleShot] = true;
80  if (flags & AN::NotifyTransient)
81  e.Additional_ [AN::EF::NotifyTransient] = true;
82  if (flags & AN::NotifyPersistent)
83  e.Additional_ [AN::EF::NotifyPersistent] = true;
84  if (flags & AN::NotifyAudio)
85  e.Additional_ [AN::EF::NotifyAudio] = true;
86 
87  return e;
88  }
89 
90  QList<QObject*> GetDataFilters (const QVariant& data, IEntityManager* manager)
91  {
92  const auto& e = MakeEntity (data, QString (), {}, "x-leechcraft/data-filter-request");
93  const auto& handlers = manager->GetPossibleHandlers (e);
94 
95  QList<QObject*> result;
96  std::copy_if (handlers.begin (), handlers.end (), std::back_inserter (result),
97  [] (QObject *obj) { return qobject_cast<IDataFilter*> (obj); });
98  return result;
99  }
100 
101  Entity MakeEntity (const QVariant& entity,
102  const QString& location,
103  TaskParameters tp,
104  const QString& mime)
105  {
106  Entity result;
107  result.Entity_ = entity;
108  result.Location_ = location;
109  result.Parameters_ = tp;
110  result.Mime_ = mime;
111  return result;
112  }
113 
114  Entity MakeNotification (const QString& header,
115  const QString& text, Priority priority)
116  {
117  Entity result = MakeEntity (header,
118  QString (),
120  "x-leechcraft/notification");
121  result.Additional_ ["Text"] = text;
122  result.Additional_ ["Priority"] = priority;
123  return result;
124  }
125 
126  Entity MakeANCancel (const Entity& event)
127  {
128  Entity e = MakeNotification (event.Entity_.toString (), QString (), PInfo_);
129  e.Additional_ [AN::EF::SenderID] = event.Additional_ [AN::EF::SenderID];
130  e.Additional_ [AN::EF::EventID] = event.Additional_ [AN::EF::EventID];
132  return e;
133  }
134 
135  Entity MakeANCancel (const QString& senderId, const QString& eventId)
136  {
137  Entity e = MakeNotification (QString (), QString (), PInfo_);
138  e.Additional_ [AN::EF::SenderID] = senderId;
139  e.Additional_ [AN::EF::EventID] = eventId;
141  return e;
142  }
143 
144  QVariantList GetPersistentData (const QList<QVariant>& keys,
145  QObject* object)
146  {
147  Entity e = MakeEntity (keys,
148  {},
149  Internal,
150  "x-leechcraft/data-persistent-load");
151  QVariantList values;
152  e.Additional_ ["Values"] = QVariant::fromValue<QVariantList*> (&values);
153 
154  QMetaObject::invokeMethod (object,
155  "delegateEntity",
156  Q_ARG (LeechCraft::Entity, e),
157  Q_ARG (int*, 0),
158  Q_ARG (QObject**, 0));
159 
160  return values;
161  }
162 }
163 }
Q_DECLARE_METATYPE(QList< QModelIndex >)
Entity MakeEntity(const QVariant &entity, const QString &location, TaskParameters tp, const QString &mime)
Definition: util.cpp:101
Proxy to core entity manager.
const QString EventID
The ID of the event (QString).
Definition: entityfields.h:78
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:166
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
const QString EventCategory
The category of the event (QString).
Definition: entityfields.h:67
User should be notified visually.
Definition: constants.h:177
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)
Definition: util.cpp:63
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:213
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
const QString NotifyTransient
Whether a transient notifier should be enabled by default in the rule being created (bool)...
Definition: entityfields.h:159
Entity MakeNotification(const QString &header, const QString &text, Priority priority)
An utility function to make a Entity with notification.
Definition: util.cpp:114
Entity MakeANCancel(const Entity &event)
Makes an event for canceling another Advanced Notifications event.
Definition: util.cpp:126
QList< QObject * > GetDataFilters(const QVariant &data, IEntityManager *manager)
Returns the data filter plugins that can handle data.
Definition: util.cpp:90
const QString ExtendedText
The even more detailed text than FullText (QString).
Definition: entityfields.h:115
const QString EventType
The type of the event (QString).
Definition: entityfields.h:103
QMap< QString, QVariant > Additional_
Additional parameters.
Definition: structures.h:225
QVariantList GetPersistentData(const QList< QVariant > &keys, QObject *object)
Definition: util.cpp:144
QString Mime_
MIME type of the entity.
Definition: structures.h:209
User should be notified visually via persistent notifications.
Definition: constants.h:189
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:173
Notify by playing back an audio file.
Definition: constants.h:193
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:42
Describes parameters of an entity.
Definition: structures.h:156
QString Location_
Source or destination.
Definition: structures.h:183