LeechCraft  0.6.70-18450-gabe19ee3b0
Modular cross-platform feature rich live environment.
anutil.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 "anutil.h"
10 #include <QObject>
11 #include <QMap>
14 #include <util/sll/qtutil.h>
15 #include <util/sll/visitor.h>
16 
17 namespace LC::Util::AN
18 {
19  namespace LAN = LC::AN;
20 
22  {
23  static const QMap<QString, QString> cat2hr
24  {
25  { LAN::CatIM, QObject::tr ("Instant messaging") },
26  { LAN::CatOrganizer, QObject::tr ("Organizer") },
27  { LAN::CatDownloads, QObject::tr ("Downloads") },
28  { LAN::CatPackageManager, QObject::tr ("Package manager") },
29  { LAN::CatMediaPlayer, QObject::tr ("Media player") },
30  { LAN::CatTerminal, QObject::tr ("Terminal") },
31  { LAN::CatNews, QObject::tr ("News") },
32  { LAN::CatGeneric, QObject::tr ("Generic") }
33  };
34  return cat2hr;
35  }
36 
37  QStringList GetKnownEventTypes (const QString& category)
38  {
39  static const QMap<QString, QStringList> cat2types
40  {
41  {
42  LAN::CatIM,
43  {
60  }
61  },
62  {
64  {
66  }
67  },
68  {
70  {
73  }
74  },
75  {
77  {
79  }
80  },
81  {
83  {
85  }
86  },
87  {
89  {
93  }
94  },
95  {
97  {
100  }
101  },
102  {
104  {
106  }
107  }
108  };
109  return cat2types.value (category);
110  }
111 
112  QString GetCategoryName (const QString& category)
113  {
114  return GetCategoryNameMap ().value (category, category);
115  }
116 
117  QString GetTypeName (const QString& type)
118  {
119  static const QMap<QString, QString> type2hr
120  {
121  { LAN::TypeIMAttention, QObject::tr ("Attention request") },
122  { LAN::TypeIMIncFile, QObject::tr ("Incoming file transfer request") },
123  { LAN::TypeIMIncMsg, QObject::tr ("Incoming chat message") },
124  { LAN::TypeIMMUCHighlight, QObject::tr ("MUC highlight") },
125  { LAN::TypeIMMUCInvite, QObject::tr ("MUC invitation") },
126  { LAN::TypeIMMUCMsg, QObject::tr ("General MUC message") },
127  { LAN::TypeIMStatusChange, QObject::tr ("Contact status change") },
128  { LAN::TypeIMSubscrGrant, QObject::tr ("Authorization granted") },
129  { LAN::TypeIMSubscrRevoke, QObject::tr ("Authorization revoked") },
130  { LAN::TypeIMSubscrRequest, QObject::tr ("Authorization requested") },
131  { LAN::TypeIMSubscrSub, QObject::tr ("Contact subscribed") },
132  { LAN::TypeIMSubscrUnsub, QObject::tr ("Contact unsubscribed") },
133  { LAN::TypeIMEventTuneChange, QObject::tr ("Contact's tune changed") },
134  { LAN::TypeIMEventMoodChange, QObject::tr ("Contact's mood changed") },
135  { LAN::TypeIMEventActivityChange, QObject::tr ("Contact's activity changed") },
136  { LAN::TypeIMEventLocationChange, QObject::tr ("Contact's location changed") },
137 
138  { LAN::TypeOrganizerEventDue, QObject::tr ("Event is due") },
139 
140  { LAN::TypeDownloadError, QObject::tr ("Download error") },
141  { LAN::TypeDownloadFinished, QObject::tr ("Download finished") },
142 
143  { LAN::TypePackageUpdated, QObject::tr ("Package updated") },
144 
145  { LAN::TypeMediaPlaybackStatus, QObject::tr ("Media playback status changed") },
146 
147  { LAN::TypeTerminalBell, QObject::tr ("Bell in a terminal") },
148  { LAN::TypeTerminalActivity, QObject::tr ("Activity in a terminal") },
149  { LAN::TypeTerminalInactivity, QObject::tr ("Inactivity in a terminal") },
150 
151  { LAN::TypeNewsSourceUpdated, QObject::tr ("News source got updated") },
152  { LAN::TypeNewsSourceBroken, QObject::tr ("News source is detected to be broken") },
153 
154  { LAN::TypeGeneric, QObject::tr ("Generic") }
155  };
156  return type2hr.value (type, type);
157  }
158 
159  QVariant ToVariant (const LC::AN::StringMatcher& matcher)
160  {
161  const auto value = Util::Visit (matcher,
162  [] (const QRegularExpression& expr) { return QVariant { expr }; },
163  [] (const auto& wrapper) { return QVariant { wrapper.Pattern_ }; });
164  return QVariantMap
165  {
166  { "index"_qs, static_cast<int> (matcher.index ()) },
167  { "value"_qs, value },
168  };
169  }
170 
172  {
173  const auto& map = var.toMap ();
174  const auto idx = map ["index"_qs].toInt ();
175  const auto value = map ["value"_qs];
176  switch (idx)
177  {
178  case 0:
179  return LC::AN::Substring { value.toString () };
180  case 1:
181  return LC::AN::Wildcard { value.toString () };
182  case 2:
183  return value.toRegularExpression ();
184  default:
185  qWarning () << "unknown type index" << idx << map;
186  return LC::AN::Substring {};
187  }
188  }
189 
190  namespace
191  {
192  bool GenericMatch (auto&& val, const LC::AN::StringMatcher& pattern)
193  {
194  const auto pos = Util::Visit (pattern,
195  [&val] (const QRegularExpression& rx) { return val.indexOf (rx); },
196  [&val] (const LC::AN::Substring& em) { return val.indexOf (em.Pattern_); },
197  [&val] (const LC::AN::Wildcard& wc) { return val.indexOf (wc.Compiled_); });
198  return pos >= 0;
199  }
200  }
201 
202  bool Matches (const QString& string, const LC::AN::StringMatcher& pattern)
203  {
204  return GenericMatch (string, pattern);
205  }
206 
207  bool Matches (const QStringList& strings, const LC::AN::StringMatcher& pattern)
208  {
209  return GenericMatch (strings, pattern);
210  }
211 }
Q_DECL_IMPORT const QString TypeIMSubscrUnsub
Another user has unsubscribed from our user.
QVariant ToVariant(const LC::AN::StringMatcher &matcher)
Definition: anutil.cpp:159
Q_DECL_IMPORT const QString CatOrganizer
Category of Organizer-related events.
Q_DECL_IMPORT const QString TypePackageUpdated
A package has been updated.
Q_DECL_IMPORT const QString TypeIMMUCHighlight
User has been highlighted in a multiuser chat.
QStringList GetKnownEventTypes(const QString &category)
Returns the known events types for the given category.
Definition: anutil.cpp:37
Q_DECL_IMPORT const QString CatTerminal
Category for terminal emulation events.
Q_DECL_IMPORT const QString TypeIMEventLocationChange
User&#39;s location has changed.
Q_DECL_IMPORT const QString CatDownloads
Category of Downloads-related events.
Q_DECL_IMPORT const QString TypeTerminalActivity
Activity in terminal window.
Q_DECL_IMPORT const QString TypeIMSubscrSub
Another user has subscribed to our user.
Q_DECL_IMPORT const QString TypeOrganizerEventDue
An event due date is coming.
Q_DECL_IMPORT const QString TypeIMMUCInvite
User has been invited to a multiuser chat.
auto Visit(const Either< Left, Right > &either, Args &&... args)
Definition: either.h:180
Q_DECL_IMPORT const QString CatIM
Category of Instant Messaging-related events.
Q_DECL_IMPORT const QString TypeGeneric
Generic type for generic notifications.
QString GetCategoryName(const QString &category)
Returns the human-readable name of the event category.
Definition: anutil.cpp:112
Q_DECL_IMPORT const QString TypeTerminalBell
A bell has ringed in a terminal window.
Q_DECL_IMPORT const QString TypeNewsSourceBroken
News source is detected to be broken.
Q_DECL_IMPORT const QString TypeNewsSourceUpdated
News source got updated.
Q_DECL_IMPORT const QString TypeTerminalInactivity
Inactivity in terminal window.
QString GetTypeName(const QString &type)
Returns the human-readable name of the event type.
Definition: anutil.cpp:117
QMap< QString, QString > GetCategoryNameMap()
Returns the map from the category ID to its name.
Definition: anutil.cpp:21
Q_DECL_IMPORT const QString CatNews
Category for news-related events.
Q_DECL_IMPORT const QString CatGeneric
Generic notifications that don&#39;t fit into any other category.
bool Matches(const QString &string, const LC::AN::StringMatcher &pattern)
Definition: anutil.cpp:202
Q_DECL_IMPORT const QString TypeIMStatusChange
Another user in our user&#39;s contact list has changed its status.
Q_DECL_IMPORT const QString CatMediaPlayer
Category of media player-related events.
Q_DECL_IMPORT const QString TypeMediaPlaybackStatus
A media file playback status has been changed.
Q_DECL_IMPORT const QString TypeIMEventTuneChange
User&#39;s tune has changed.
Q_DECL_IMPORT const QString TypeDownloadFinished
A download has been finished successfully without errors.
LC::AN::StringMatcher StringMatcherFromVariant(const QVariant &var)
Definition: anutil.cpp:171
Q_DECL_IMPORT const QString CatPackageManager
Category of package manager-related events.
Q_DECL_IMPORT const QString TypeIMEventActivityChange
User&#39;s activity has changed.
Q_DECL_IMPORT const QString TypeIMIncFile
Another user has sent our user a file.
Q_DECL_IMPORT const QString TypeIMMUCMsg
A message has been sent to a multiuser chat.
Q_DECL_IMPORT const QString TypeIMSubscrRevoke
Another user has revoked subscription from our user.
Q_DECL_IMPORT const QString TypeIMSubscrGrant
Another user has granted subscription to our user.
Q_DECL_IMPORT const QString TypeDownloadError
A download has been failed.
Definition: anutil.h:15
Q_DECL_IMPORT const QString TypeIMEventMoodChange
User&#39;s mood has changed.
Q_DECL_IMPORT const QString TypeIMAttention
Another user has requested our user&#39;s attention.
Q_DECL_IMPORT const QString TypeIMIncMsg
User has received a message in a standard one-to-one chat.
Q_DECL_IMPORT const QString TypeIMSubscrRequest
Another user has requested subscription from our user.