1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
|
#include <QtCore/QFile>
#include <QtCore/QRegularExpression>
#include "msolimageiohandler.h"
#include "cardmanageradaptor.h"
#include "cardmanager.h"
Card::Card(const QString &id, QObject *parent) :
QObject(parent), _id(id),
_vibrate(true), _open(false), _visible(false)
{
}
QString Card::id() const
{
return _id;
}
QString Card::header() const
{
return _header;
}
void Card::setHeader(const QString &header)
{
if (_header != header) {
_header = header;
emit headerChanged();
}
}
QString Card::title() const
{
return _title;
}
void Card::setTitle(const QString &title)
{
if (_title != title) {
_title = title;
emit titleChanged();
}
}
QString Card::info() const
{
return _info;
}
void Card::setInfo(const QString &info)
{
if (_info != info) {
_info = info;
emit infoChanged();
}
}
QDateTime Card::dateTime() const
{
return _dateTime;
}
void Card::setDateTime(const QDateTime &dt)
{
if (_dateTime != dt) {
_dateTime = dt;
emit dateTimeChanged();
}
}
QString Card::icon() const
{
return _icon;
}
void Card::setIcon(const QString &url)
{
_icon = url;
}
QString Card::picture() const
{
return _picture;
}
void Card::setPicture(const QString &url)
{
_picture = url;
}
bool Card::isVibrate() const
{
return _vibrate;
}
void Card::setVibrate(bool vibrate)
{
if (_vibrate != vibrate) {
_vibrate = vibrate;
emit vibrateChanged();
}
}
QString Card::text() const
{
return _text;
}
void Card::setText(const QString &text)
{
if (_text != text) {
_text = text;
emit textChanged();
}
}
QStringList Card::menuOptions() const
{
return _options;
}
void Card::setMenuOptions(const QStringList &options)
{
if (_options != options) {
_options = options;
emit menuOptionsChanged();
}
}
bool Card::isOpen() const
{
return _open;
}
void Card::setOpen(bool open)
{
_open = open;
}
bool Card::isVisible() const
{
return _visible;
}
void Card::setVisible(bool visible)
{
_visible = visible;
}
CardDeck::CardDeck(const QString &package, const QString &application, QObject *parent) :
QObject(parent),
_package(package), _application(application),
_refreshTimer(new QTimer(this))
{
_refreshTimer->setSingleShot(true);
_refreshTimer->setInterval(100);
connect(_refreshTimer, &QTimer::timeout,
this, &CardDeck::needsRefresh);
}
QString CardDeck::package() const
{
return _package;
}
QString CardDeck::application() const
{
return _application;
}
QList<Card*> CardDeck::cards() const
{
return _deck;
}
Card* CardDeck::cardAt(int position) const
{
return _deck.at(position);
}
Card* CardDeck::cardAt(const QString &id) const
{
return _ids.value(id);
}
void CardDeck::appendCard(Card *card)
{
insertCard(_deck.size(), card);
}
void CardDeck::insertCard(int position, Card *card)
{
if (_ids.contains(card->id())) {
qWarning() << "Card" << card->id() << "already present";
return;
}
_deck.insert(position, card);
_ids.insert(card->id(), card);
connect(card, &Card::headerChanged, this, &CardDeck::scheduleRefresh);
connect(card, &Card::titleChanged, this, &CardDeck::scheduleRefresh);
connect(card, &Card::infoChanged, this, &CardDeck::scheduleRefresh);
connect(card, &Card::vibrateChanged, this, &CardDeck::scheduleRefresh);
connect(card, &Card::textChanged, this, &CardDeck::scheduleRefresh);
connect(card, &Card::menuOptionsChanged, this, &CardDeck::scheduleRefresh);
emit cardAdded(card);
scheduleRefresh();
}
void CardDeck::removeCard(int position)
{
Card * card = _deck.takeAt(position);
_ids.remove(card->id());
disconnect(card, 0, this, 0);
emit cardRemoved(card);
scheduleRefresh();
}
void CardDeck::removeCard(Card *card)
{
int position = _deck.indexOf(card);
if (position >= 0) {
removeCard(position);
} else {
qWarning() << "Card not found";
}
}
void CardDeck::scheduleRefresh()
{
if (!_refreshTimer->isActive()) {
_refreshTimer->start();
}
}
CardManager::CardManager(FmsManager *fms, ToqManager *toq) :
QObject(toq), _toq(toq), _fms(fms)
{
_toq->setEndpointListener(ToqConnection::AppMessagingEndpoint, this);
new CardManagerAdaptor(this);
QDBusConnection::sessionBus().registerObject("/com/javispedro/saltoq/CardManager", this,
QDBusConnection::ExportAdaptors | QDBusConnection::ExportChildObjects);
}
void CardManager::handleMessage(const ToqConnection::Message &msg)
{
QPair<CardDeck*, QString> p = unpackMessage(msg.payload);
CardDeck *deck = p.first;
if (!deck) {
return;
}
QHash<QString,QString> dict = unpackDictionary(p.second);
qDebug() << dict;
QString id = dict["id"];
Card *card = deck->cardAt(id);
if (card) {
QString event = dict["event"];
if (event == "open") {
card->setOpen(true);
} else if (event == "closed") {
card->setOpen(false);
} else if (event == "visible") {
card->setVisible(true);
} else if (event == "invisible") {
card->setVisible(false);
} else if (event == "selected") {
emit card->optionSelected(dict["eventdata"]);
} else {
qWarning() << "Unknown card event" << event;
}
} else {
qWarning() << "Card" << id << "not found in deck" << deck->package();
}
}
void CardManager::installDeck(CardDeck *deck)
{
if (_decks.contains(deck->package())) {
qWarning() << "Deck" << deck->package() << "is already installed";
return;
}
_decks.insert(deck->package(), deck);
connect(deck, &CardDeck::needsRefresh, this, &CardManager::handleDeckNeedsRefresh);
connect(deck, &CardDeck::cardAdded, this, &CardManager::handleCardAdded);
connect(deck, &CardDeck::cardRemoved, this, &CardManager::handleCardRemoved);
}
void CardManager::uninstallDeck(CardDeck *deck)
{
if (!_decks.contains(deck->package())) {
qWarning() << "Deck" << deck->package() << "is not installed";
}
_decks.remove(deck->package());
disconnect(deck, 0, this, 0);
}
QString CardManager::sendImage(CardDeck *deck, const QString &iconName, const QImage &image)
{
QString remoteFile = QString("/packages/%1/%2.img").arg(deck->package()).arg(iconName);
_fms->updateFile(remoteFile, convertImageToMsol(image));
return QString("fms:/%1.img").arg(iconName);
}
QString CardManager::escapeString(const QString &s)
{
QString e(s);
e.replace('\\', "\\\\");
e.replace('\n', "\\n");
e.replace('"', "\\\"");
return e;
}
QString CardManager::generateCardDescription(const QString &verb, Card *card) const
{
QString desc = verb + " { ";
desc += QString("id = \"%1\", ").arg(card->id());
desc += QString("version = 2, cardevents = \"true\", ");
if (!card->header().isEmpty()) {
desc += QString("header = \"%1\", ").arg(escapeString(card->header()));
}
if (!card->title().isEmpty()) {
desc += QString("title = \"%1\", ").arg(escapeString(card->title()));
}
if (card->dateTime().isValid()) {
desc += QString("time = \"%1\", ").arg(card->dateTime().toMSecsSinceEpoch());
}
if (!card->info().isEmpty()) {
desc += QString("info = \"%1\", ").arg(escapeString(card->info()));
}
if (!card->icon().isEmpty()) {
desc += QString("icon = \"%1\", ").arg(escapeString(card->icon()));
}
if (!card->isVibrate()) {
desc += QString("suppressvibe = \"true\", ");
}
if (!card->text().isEmpty()) {
desc += QString("detail = { \"%1\" }").arg(escapeString(card->text()));
}
desc += QString("}\n");
return desc;
}
QByteArray CardManager::packMessage(CardDeck *deck, const QString &msg) const
{
QByteArray data;
QByteArray target = QString("%1/%2").arg(deck->package()).arg(deck->application()).toUtf8();
QByteArray payload = msg.toUtf8();
target.append('\0'); // Null terminated
data.reserve(1 + target.size() + payload.size());
data.append(char(target.size()));
data.append(target);
data.append(payload);
return data;
}
QPair<CardDeck*,QString> CardManager::unpackMessage(const QByteArray &data) const
{
CardDeck *deck = 0;
QString payload;
int id_size = data.at(0);
QStringList id = QString::fromUtf8(data.mid(1, id_size)).split('/');
deck = _decks.value(id.at(0));
payload = QString::fromUtf8(data.mid(1 + id_size));
if (!deck) {
qWarning() << "Deck with id" << id[0] << "not found";
}
return QPair<CardDeck*, QString>(deck, payload);
}
QHash<QString,QString> CardManager::unpackDictionary(const QString &data) const
{
QHash<QString,QString> dict;
const QRegularExpression re("(\\w+) = \"([^\"]*)\"");
QRegularExpressionMatchIterator it = re.globalMatch(data);
while (it.hasNext()) {
QRegularExpressionMatch m = it.next();
dict.insert(m.captured(1), m.captured(2));
}
return dict;
}
void CardManager::refreshDeck(CardDeck *deck)
{
Q_ASSERT(deck);
Q_ASSERT(_toq->isConnected());
qDebug() << "Refreshing deck" << deck->package();
QList<Card*> cards = deck->cards();
QString data;
for (Card * card : cards) {
data += generateCardDescription("NotifyCard", card);
}
qDebug() << data;
QString cardsFile = QString("/packages/%1/cards.dat").arg(deck->package());
if (!data.isEmpty()) {
_fms->updateFile(cardsFile, data.toUtf8());
} else {
_fms->deleteFile(cardsFile);
}
}
void CardManager::handleToqConnected()
{
for (const QString &package : _pending) {
refreshDeck(_decks.value(package));
}
_pending.clear();
}
void CardManager::handleDeckNeedsRefresh()
{
CardDeck *deck = static_cast<CardDeck*>(sender());
qDebug() << deck->package() << "needs refresh";
if (_toq->isConnected()) {
refreshDeck(deck);
} else {
_pending.insert(deck->package());
}
}
void CardManager::handleCardAdded(Card *card)
{
CardDeck *deck = static_cast<CardDeck*>(sender());
qDebug() << "Card added";
if (_toq->isConnected()) {
QString cmd = generateCardDescription("PopupCard", card);
QByteArray payload = packMessage(deck, cmd);
qDebug() << cmd;
_toq->sendMessage(ToqConnection::AppMessagingEndpoint, ToqConnection::AppMessagingEndpoint + 1, 0x8000, payload);
}
}
void CardManager::handleCardRemoved(Card *card)
{
CardDeck *deck = static_cast<CardDeck*>(sender());
if (_toq->isConnected()) {
QString cmd = QString("DeleteCard { id = \"%1\" }\n").arg(card->id());
QByteArray payload = packMessage(deck, cmd);
qDebug() << cmd;
_toq->sendMessage(ToqConnection::AppMessagingEndpoint, ToqConnection::AppMessagingEndpoint + 1, 0x8000, payload);
}
}
|