-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathabstractskillview.cpp
811 lines (673 loc) · 29.9 KB
/
abstractskillview.cpp
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
/*
* Copyright 2018 by Marco Martin <[email protected]>
* Copyright 2018 David Edmundson <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include "abstractskillview.h"
#include "activeskillsmodel.h"
#include "abstractdelegate.h"
#include "sessiondatamap.h"
#include "sessiondatamodel.h"
#include "delegatesmodel.h"
#include <QWebSocket>
#include <QUuid>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonDocument>
#include <QQmlContext>
#include <QQmlEngine>
#include <QTranslator>
AbstractSkillView::AbstractSkillView(QQuickItem *parent)
: QQuickItem(parent),
m_id(QUuid::createUuid().toString()),
m_controller(MycroftController::instance())
{
m_activeSkillsModel = new ActiveSkillsModel(this);
m_guiWebSocket = new QWebSocket(QString(), QWebSocketProtocol::VersionLatest, this);
m_controller->registerView(this);
connect(m_guiWebSocket, &QWebSocket::connected, this,
[this] () {
m_reconnectTimer.stop();
emit statusChanged();
});
connect(m_guiWebSocket, &QWebSocket::disconnected, this, &AbstractSkillView::closed);
connect(m_guiWebSocket, &QWebSocket::disconnected, this, [this]() {
m_activeSkillsModel->removeRows(0, m_activeSkillsModel->rowCount());
});
connect(m_guiWebSocket, &QWebSocket::stateChanged, this,
[this] (QAbstractSocket::SocketState state) {
emit statusChanged();
});
connect(m_guiWebSocket, &QWebSocket::textMessageReceived, this, &AbstractSkillView::onGuiSocketMessageReceived);
connect(m_guiWebSocket, &QWebSocket::stateChanged, this,
[this](QAbstractSocket::SocketState socketState) {
//TODO: when the connection closes, all session data and guis should be destroyed
//qWarning()<<"GUI SOCKET STATE:"<<socketState;
//Try to reconnect if our connection died but the main server connection is still alive
if (socketState == QAbstractSocket::UnconnectedState && m_url.isValid() && m_controller->status() == MycroftController::Open) {
m_reconnectTimer.start();
}
});
connect(m_guiWebSocket, QOverload<QAbstractSocket::SocketError>::of(&QWebSocket::error), this,
[this](QAbstractSocket::SocketError error) {
qWarning() << "Gui socket Connection Error:" << error;
m_reconnectTimer.start();
});
connect(m_controller, &MycroftController::socketStatusChanged, this,
[this]() {
if (m_controller->status() != MycroftController::Open) {
m_guiWebSocket->close();
//don't assume the url will be still valid
m_url = QUrl();
}
});
// Reconnect timer
m_reconnectTimer.setInterval(1000);
connect(&m_reconnectTimer, &QTimer::timeout, this, [this]() {
m_guiWebSocket->close();
m_guiWebSocket->open(m_url);
});
// Trim components cache timer
m_trimComponentsTimer.setInterval(100);
m_trimComponentsTimer.setSingleShot(true);
connect(&m_trimComponentsTimer, &QTimer::timeout, this, [this]() {
QQmlEngine *engine = qmlEngine(this);
if (engine) {
engine->clearComponentCache();
}
});
connect(m_controller, &MycroftController::utteranceManagedBySkill, this,
[this](const QString &skillId) {
m_activeSkillsModel->checkGuiActivation(skillId);
});
}
AbstractSkillView::~AbstractSkillView()
{
}
QUrl AbstractSkillView::url() const
{
return m_url;
}
void AbstractSkillView::setUrl(const QUrl &url)
{
if (m_url == url) {
return;
}
m_url = url;
//don't connect if the controller is offline
if (m_controller->status() == MycroftController::Open) {
m_guiWebSocket->close();
m_guiWebSocket->open(url);
}
}
QString AbstractSkillView::id() const
{
return m_id;
}
void AbstractSkillView::triggerEvent(const QString &skillId, const QString &eventName, const QVariantMap ¶meters)
{
if (m_guiWebSocket->state() != QAbstractSocket::ConnectedState) {
qWarning() << "Error: Mycroft gui connection not open!";
return;
}
QJsonObject root;
root[QStringLiteral("type")] = QStringLiteral("mycroft.events.triggered");
root[QStringLiteral("namespace")] = skillId;
root[QStringLiteral("event_name")] = eventName;
root[QStringLiteral("parameters")] = QJsonObject::fromVariantMap(parameters);
QJsonDocument doc(root);
m_guiWebSocket->sendTextMessage(QString::fromUtf8(doc.toJson()));
}
void AbstractSkillView::writeProperties(const QString &skillId, const QVariantMap &data)
{
if (m_guiWebSocket->state() != QAbstractSocket::ConnectedState) {
qWarning() << "Error: Mycroft gui connection not open!";
return;
}
QJsonObject root;
root[QStringLiteral("type")] = QStringLiteral("mycroft.session.set");
root[QStringLiteral("namespace")] = skillId;
root[QStringLiteral("data")] = QJsonObject::fromVariantMap(data);
QJsonDocument doc(root);
m_guiWebSocket->sendTextMessage(QString::fromUtf8(doc.toJson()));
}
void AbstractSkillView::deleteProperty(const QString &skillId, const QString &property)
{
if (m_guiWebSocket->state() != QAbstractSocket::ConnectedState) {
qWarning() << "Error: Mycroft gui connection not open!";
return;
}
QJsonObject root;
root[QStringLiteral("type")] = QStringLiteral("mycroft.session.delete");
root[QStringLiteral("namespace")] = skillId;
root[QStringLiteral("property")] = property;
QJsonDocument doc(root);
m_guiWebSocket->sendTextMessage(QString::fromUtf8(doc.toJson()));
}
void AbstractSkillView::showHomeScreen(const QString &skillId, const QString &skillPage)
{
m_controller->sendRequest(QStringLiteral("mycroft.device.display.timeout"),
QVariantMap({{QStringLiteral("skillId"), skillId},
{QStringLiteral("skillPage"), skillPage}}));
}
MycroftController::Status AbstractSkillView::status() const
{
if (m_reconnectTimer.isActive()) {
return MycroftController::Connecting;
}
switch(m_guiWebSocket->state())
{
case QAbstractSocket::ConnectingState:
case QAbstractSocket::BoundState:
case QAbstractSocket::HostLookupState:
return MycroftController::Connecting;
case QAbstractSocket::UnconnectedState:
return MycroftController::Closed;
case QAbstractSocket::ConnectedState:
return MycroftController::Open;
case QAbstractSocket::ClosingState:
return MycroftController::Closing;
default:
return MycroftController::Connecting;
}
}
ActiveSkillsModel *AbstractSkillView::activeSkills() const
{
return m_activeSkillsModel;
}
SessionDataMap *AbstractSkillView::sessionDataForSkill(const QString &skillId)
{
SessionDataMap *map = nullptr;
if (m_skillData.contains(skillId)) {
map = m_skillData[skillId];
} else if (m_activeSkillsModel->skillIndex(skillId).isValid()) {
map = new SessionDataMap(skillId, this);
m_skillData[skillId] = map;
}
return map;
}
QList<QVariantMap> variantListToOrderedMap(const QVariantList &data)
{
QList<QVariantMap> ordMap;
QStringList roleNames;
for (const auto &item : data) {
if (!item.canConvert<QVariantMap>()) {
qWarning() << "Error: Array data structure corrupted: " << data;
return ordMap;
}
const auto &map = item.value<QVariantMap>();
if (roleNames.isEmpty()) {
roleNames = map.keys();
} else if (roleNames != map.keys()) {
qWarning() << "WARNING: Item with a wrong set of roles encountered, some roles will be inaccessible from QML, expected: " << roleNames << "Encountered: " << map.keys();
}
ordMap << map;
}
return ordMap;
}
QStringList jsonModelToStringList(const QString &key, const QJsonValue &data)
{
QStringList items;
if (!data.isArray()) {
qWarning() << "Error: Model data is not an Array" << data;
return items;
}
const auto &array = data.toArray();
for (const auto &item : array) {
if (!item.isObject()) {
qWarning() << "Error: Array data structure currupted: " << data;
items.clear();
return items;
}
const auto &obj = item.toObject();
if (obj.keys().length() != 1 || !obj.contains(key)) {
qWarning() << "Error: Item with a wrong key encountered, expected: " << key << "Encountered: " << obj.keys();
items.clear();
return items;
}
const auto &value = obj.value(key);
if (!value.isString()) {
qWarning() << "Error: item in model not a string" << value;
}
items << value.toString();
}
return items;
}
void AbstractSkillView::onGuiSocketMessageReceived(const QString &message)
{
QJsonParseError parseError;
auto doc = QJsonDocument::fromJson(message.toUtf8(), &parseError);
if (doc.isEmpty()) {
qWarning() << "Empty or invalid JSON message arrived on the gui socket:" << message << "Error:" << parseError.errorString();
return;
}
auto type = doc[QStringLiteral("type")].toString();
if (type.isEmpty()) {
qWarning() << "Empty type in the JSON message on the gui socket";
return;
}
//qDebug() << "gui message type" << type;
//BEGIN SKILLDATA
// The SkillData was updated by the server
if (type == QLatin1String("mycroft.session.set")) {
const QString skillId = doc[QStringLiteral("namespace")].toString();
const QVariantMap data = doc[QStringLiteral("data")].toVariant().toMap();
if (skillId.isEmpty()) {
qWarning() << "Empty skill_id in mycroft.session.set";
return;
}
if (!m_activeSkillsModel->skillIndex(skillId).isValid()) {
qWarning() << "Invalid skill_id in mycroft.session.set:" << skillId;
return;
}
if (data.isEmpty()) {
qWarning() << "Empty data in mycroft.session.set";
return;
}
//we already checked, assume *map is valid
SessionDataMap *map = sessionDataForSkill(skillId);
if (!map) {
return;
}
QVariantMap::const_iterator i;
for (i = data.constBegin(); i != data.constEnd(); ++i) {
//insert it as a model
QList<QVariantMap> list = variantListToOrderedMap(i.value().value<QVariantList>());
SessionDataModel *dm = map->value(i.key()).value<SessionDataModel *>();
if (!list.isEmpty()) {
if (!dm) {
dm = new SessionDataModel(map);
map->insertAndNotify(i.key(), QVariant::fromValue(dm));
} else {
dm->clear();
}
dm->insertData(0, list);
//insert it as is.
} else {
if (dm) {
dm->deleteLater();
}
map->insertAndNotify(i.key(), i.value());
}
//qDebug() << " " << i.key() << " = " << i.value();
}
// The SkillData was removed by the server
} else if (type == QLatin1String("mycroft.session.delete")) {
const QString skillId = doc[QStringLiteral("namespace")].toString();
const QString property = doc[QStringLiteral("property")].toString();
if (skillId.isEmpty()) {
qWarning() << "No skill_id provided in mycroft.session.delete";
return;
}
if (!m_activeSkillsModel->skillIndex(skillId).isValid()) {
qWarning() << "Invalid skill_id in mycroft.session.delete:" << skillId;
return;
}
if (property.isEmpty()) {
qWarning() << "No property provided in mycroft.session.delete";
return;
}
SessionDataMap *map = sessionDataForSkill(skillId);
SessionDataModel *dm = map->value(property).value<SessionDataModel *>();
map->clearAndNotify(property);
//a model will need to be manually deleted
if (dm) {
dm->deleteLater();
}
//END SKILLDATA
//BEGIN ACTIVESKILLS
// Insert new active skill
} else if (type == QLatin1String("mycroft.session.list.insert") && doc[QStringLiteral("namespace")].toString() == QLatin1String("mycroft.system.active_skills")) {
const int position = doc[QStringLiteral("position")].toInt();
if (position < 0 || position > m_activeSkillsModel->rowCount()) {
qWarning() << "Error: Invalid position in mycroft.session.list.insert of mycroft.system.active_skills";
return;
}
const QStringList skillList = jsonModelToStringList(QStringLiteral("skill_id"), doc[QStringLiteral("data")]);
if (skillList.isEmpty()) {
qWarning() << "Error: no valid skills received in mycroft.session.list.insert of mycroft.system.active_skills";
return;
}
m_activeSkillsModel->insertSkills(position, skillList);
// Active skill removed
} else if (type == QLatin1String("mycroft.session.list.remove") && doc[QStringLiteral("namespace")].toString() == QLatin1String("mycroft.system.active_skills")) {
const int position = doc[QStringLiteral("position")].toInt();
const int itemsNumber = doc[QStringLiteral("items_number")].toInt();
if (position < 0 || position > m_activeSkillsModel->rowCount() - 1) {
qWarning() << "Error: Invalid position in mycroft.session.list.remove of mycroft.system.active_skills";
return;
}
if (itemsNumber < 0 || itemsNumber > m_activeSkillsModel->rowCount() - position) {
qWarning() << "Error: Invalid items_number in mycroft.session.list.remove of mycroft.system.active_skills";
return;
}
for (int i = 0; i < itemsNumber; ++i) {
const QString skillId = m_activeSkillsModel->data(m_activeSkillsModel->index(position+i, 0)).toString();
if (!m_translatorsForSkill.contains(skillId)) {
QTranslator *translator = m_translatorsForSkill[skillId];
QCoreApplication::removeTranslator(translator);
m_translatorsForSkill.remove(skillId);
delete translator;
}
//TODO: do this after an animation
{
auto i = m_skillData.find(skillId);
if (i != m_skillData.end()) {
i.value()->deleteLater();
m_skillData.erase(i);
}
}
}
m_activeSkillsModel->removeRows(position, itemsNumber);
// Active skill moved
} else if (type == QLatin1String("mycroft.session.list.move") && doc[QStringLiteral("namespace")].toString() == QLatin1String("mycroft.system.active_skills")) {
const int from = doc[QStringLiteral("from")].toInt();
const int to = doc[QStringLiteral("to")].toInt();
const int itemsNumber = doc[QStringLiteral("items_number")].toInt();
if (from < 0 || from > m_activeSkillsModel->rowCount() - 1) {
qWarning() << "Error: Invalid from position in mycroft.session.list.move of mycroft.system.active_skills";
return;
}
if (to < 0 || to > m_activeSkillsModel->rowCount() - 1) {
qWarning() << "Error: Invalid to position in mycroft.session.list.move of mycroft.system.active_skills";
return;
}
if (itemsNumber <= 0 || itemsNumber > m_activeSkillsModel->rowCount() - from) {
qWarning() << "Error: Invalid items_number in mycroft.session.list.move of mycroft.system.active_skills";
return;
}
m_activeSkillsModel->moveRows(QModelIndex(), from, itemsNumber, QModelIndex(), to);
//END ACTIVESKILLS
//BEGIN GUI MODEL
// Insert new new gui delegates
} else if (type == QLatin1String("mycroft.gui.list.insert")) {
const QString skillId = doc[QStringLiteral("namespace")].toString();
if (skillId.isEmpty()) {
qWarning() << "No skill_id provided in mycroft.gui.list.insert";
return;
}
const int position = doc[QStringLiteral("position")].toInt();
const int timeout = doc[QStringLiteral("override")].isDouble() ? doc[QStringLiteral("override")].toInt() : 0;
DelegatesModel *delegatesModel = m_activeSkillsModel->delegatesModelForSkill(skillId);
if (!delegatesModel) {
qWarning() << "Error: no delegates model for skill" << skillId;
return;
}
if (position < 0 || position > delegatesModel->rowCount()) {
qWarning() << "Error: Invalid position in mycroft.gui.list.insert";
return;
}
const QStringList delegateUrls = jsonModelToStringList(QStringLiteral("url"), doc[QStringLiteral("data")]);
if (delegateUrls.isEmpty()) {
qWarning() << "Error: no valid skills received in mycroft.gui.list.insert";
return;
}
QList <DelegateLoader *> delegateLoaders;
for (const auto &urlString : delegateUrls) {
const QUrl delegateUrl = QUrl::fromUserInput(urlString);
if (!delegateUrl.isValid()) {
continue;
}
DelegateLoader *loader = new DelegateLoader(this);
loader->init(skillId, delegateUrl);
loader->setTimeout(timeout);
if (!m_translatorsForSkill.contains(skillId)) {
QTranslator *translator = new QTranslator(this);
// TODO: download translations if skills are remote
if (translator->load(QLocale(), skillId, QLatin1String("_"), loader->translationsUrl().path())) {
QCoreApplication::installTranslator(translator);
m_translatorsForSkill[skillId] = translator;
} else {
translator->deleteLater();
}
}
connect(loader, &QObject::destroyed, &m_trimComponentsTimer, QOverload<>::of(&QTimer::start));
delegateLoaders << loader;
}
if (delegateLoaders.count() > 0) {
delegatesModel->insertDelegateLoaders(position, delegateLoaders);
//give the focus to the first
delegateLoaders.first()->setFocus(true);
}
// Gui delegates removed
} else if (type == QLatin1String("mycroft.gui.list.remove")) {
const QString skillId = doc[QStringLiteral("namespace")].toString();
if (skillId.isEmpty()) {
qWarning() << "No skill_id provided in mycroft.gui.list.remove";
return;
}
const int position = doc[QStringLiteral("position")].toInt();
const int itemsNumber = doc[QStringLiteral("items_number")].toInt();
//TODO: try with lifecycle managed by the view?
DelegatesModel *delegatesModel = m_activeSkillsModel->delegatesModelForSkill(skillId);
if (!delegatesModel) {
qWarning() << "Error: no delegates model for skill" << skillId;
return;
}
if (position < 0 || position > delegatesModel->rowCount() - 1) {
qWarning() << "Error: Invalid position in mycroft.gui.list.remove";
return;
}
if (itemsNumber < 0 || itemsNumber > delegatesModel->rowCount()) {
qWarning() << "Error: Invalid items_number in mycroft.gui.list.remove";
return;
}
delegatesModel->removeRows(position, itemsNumber);
// Gui delegates moved
} else if (type == QLatin1String("mycroft.gui.list.move")) {
const QString skillId = doc[QStringLiteral("namespace")].toString();
if (skillId.isEmpty()) {
qWarning() << "No skill_id provided in mycroft.gui.list.move";
return;
}
const int from = doc[QStringLiteral("from")].toInt();
const int to = doc[QStringLiteral("to")].toInt();
const int itemsNumber = doc[QStringLiteral("items_number")].toInt();
DelegatesModel *delegatesModel = m_activeSkillsModel->delegatesModelForSkill(skillId);
if (!delegatesModel) {
qWarning() << "Error: no delegates model for skill" << skillId;
return;
}
if (from < 0 || from > delegatesModel->rowCount() - 1) {
qWarning() << "Error: Invalid from position in mycroft.gui.list.move";
return;
}
if (to < 0 || to > delegatesModel->rowCount() - 1) {
qWarning() << "Error: Invalid to position in mycroft.gui.list.move";
return;
}
if (itemsNumber <= 0 || itemsNumber > delegatesModel->rowCount() - from) {
qWarning() << "Error: Invalid items_number in mycroft.gui.list.move";
return;
}
delegatesModel->moveRows(QModelIndex(), from, itemsNumber, QModelIndex(), to);
//END GUI MODELS
//TODO: manage nested models?
//BEGIN DATA MODELS
// Insert new items in an existing list, or creates one under "property"
} else if (type == QLatin1String("mycroft.session.list.insert")) {
const QString skillId = doc[QStringLiteral("namespace")].toString();
if (skillId.isEmpty()) {
qWarning() << "No skill_id provided in mycroft.session.list.insert";
return;
}
const QString &property = doc[QStringLiteral("property")].toString();
if (property.isEmpty()) {
qWarning() << "Error: Invalid or empty \"property\" in mycroft.session.list.insert";
return;
}
SessionDataMap *map = sessionDataForSkill(skillId);
SessionDataModel *dm = map->value(property).value<SessionDataModel *>();
if (!dm) {
dm = new SessionDataModel(map);
map->insertAndNotify(property, QVariant::fromValue(dm));
}
const int position = doc[QStringLiteral("position")].toInt();
if (position < 0 || position > dm->rowCount()) {
qWarning() << "Error: Invalid position in mycroft.session.list.insert";
return;
}
QList<QVariantMap> list = variantListToOrderedMap(doc[QStringLiteral("data")].toVariant().value<QVariantList>());
if (list.isEmpty()) {
qWarning() << "Error: invalid data in mycroft.session.list.insert:" << doc[QStringLiteral("data")];
return;
}
dm->insertData(position, list);
// Updates the value of items in an existing list, Error if under "property" no list exists
} else if (type == QLatin1String("mycroft.session.list.update")) {
const QString skillId = doc[QStringLiteral("namespace")].toString();
if (skillId.isEmpty()) {
qWarning() << "No skill_id provided in mycroft.session.list.update";
return;
}
const QString &property = doc[QStringLiteral("property")].toString();
if (property.isEmpty()) {
qWarning() << "Error: Invalid or empty \"property\" in mycroft.session.list.update";
return;
}
SessionDataMap *map = sessionDataForSkill(skillId);
SessionDataModel *dm = map->value(property).value<SessionDataModel *>();
if (!dm) {
qWarning() << "Error: no list model existing under property" << property << "in mycroft.session.list.update";
return;
}
const int position = doc[QStringLiteral("position")].toInt();
if (position < 0 || position > m_activeSkillsModel->rowCount()) {
qWarning() << "Error: Invalid position in mycroft.session.list.update";
return;
}
QList<QVariantMap> list = variantListToOrderedMap(doc[QStringLiteral("data")].toVariant().value<QVariantList>());
if (list.isEmpty()) {
qWarning() << "Error: invalid data in mycroft.session.list.insert:" << doc[QStringLiteral("data")];
return;
}
dm->updateData(position, list);
// Moves items within an existing list, Error if under "property" no list exists
} else if (type == QLatin1String("mycroft.session.list.move")) {
const QString skillId = doc[QStringLiteral("namespace")].toString();
if (skillId.isEmpty()) {
qWarning() << "No skill_id provided in mycroft.session.list.update";
return;
}
const QString &property = doc[QStringLiteral("property")].toString();
if (property.isEmpty()) {
qWarning() << "Error: Invalid or empty \"property\" in mycroft.session.list.move";
return;
}
SessionDataMap *map = sessionDataForSkill(skillId);
SessionDataModel *dm = map->value(property).value<SessionDataModel *>();
if (!dm) {
qWarning() << "Error: no list model existing under property" << property << "in mycroft.session.list.move";
return;
}
const int from = doc[QStringLiteral("from")].toInt();
const int to = doc[QStringLiteral("to")].toInt();
const int itemsNumber = doc[QStringLiteral("items_number")].toInt();
if (from < 0 || from > dm->rowCount() - 1) {
qWarning() << "Error: Invalid from position in mycroft.session.list.move";
return;
}
if (to < 0 || to > dm->rowCount()) {
qWarning() << "Error: Invalid to position in mycroft.session.list.move";
return;
}
if (itemsNumber <= 0 || itemsNumber > dm->rowCount() - from) {
qWarning() << "Error: Invalid items_number in mycroft.session.list.move";
return;
}
dm->moveRows(QModelIndex(), from, itemsNumber, QModelIndex(), to);
// Removes items from an existing list, Error if under "property" no list exists
} else if (type == QLatin1String("mycroft.session.list.remove")) {
const QString skillId = doc[QStringLiteral("namespace")].toString();
if (skillId.isEmpty()) {
qWarning() << "No skill_id provided in mycroft.session.list.update";
return;
}
const QString &property = doc[QStringLiteral("property")].toString();
if (property.isEmpty()) {
qWarning() << "Error: Invalid or empty \"property\" in mycroft.session.list.move";
return;
}
SessionDataMap *map = sessionDataForSkill(skillId);
SessionDataModel *dm = map->value(property).value<SessionDataModel *>();
if (!dm) {
qWarning() << "Error: no list model existing under property" << property << "in mycroft.session.list.move";
return;
}
const int position = doc[QStringLiteral("position")].toInt();
const int itemsNumber = doc[QStringLiteral("items_number")].toInt();
if (position < 0 || position > dm->rowCount() - 1) {
qWarning() << "Error: Invalid position in mycroft.session.list.remove of mycroft.system.active_skills";
return;
}
if (itemsNumber < 0 || itemsNumber > dm->rowCount() - position) {
qWarning() << "Error: Invalid items_number in mycroft.session.list.remove of mycroft.system.active_skills";
return;
}
dm->removeRows(position, itemsNumber);
//END DATA MODELS
//BEGIN EVENTS
// Action triggered from the server
} else if (type == QLatin1String("mycroft.events.triggered")) {
const QString skillOrSystem = doc[QStringLiteral("namespace")].toString();
if (skillOrSystem.isEmpty()) {
qWarning() << "No namespace provided for mycroft.events.triggered";
return;
}
/*FIXME: do we need to keep this check? we need to also include skills without gui
// If it's a skill it must exist
if (skillOrSystem != QLatin1String("system") && !m_activeSkillsModel->skillIndex(skillOrSystem).isValid()) {
qWarning() << "Invalid skill id passed as namespace for mycroft.events.triggered:" << skillOrSystem;
return;
}*/
const QString eventName = doc[QStringLiteral("event_name")].toString();
if (eventName.isEmpty()) {
qWarning() << "No namespace provided for mycroft.events.triggered";
return;
}
// data can also be empty
const QVariantMap data = doc[QStringLiteral("data")].toVariant().toMap();
QList<AbstractDelegate *> delegates;
if (skillOrSystem == QLatin1String("system")) {
for (auto *delegatesModel : activeSkills()->delegatesModels()) {
delegates << delegatesModel->delegates();
}
} else {
DelegatesModel *delegatesModel = activeSkills()->delegatesModelForSkill(skillOrSystem);
if (delegatesModel) {
delegates << delegatesModel->delegates();
}
}
// page_gained_focus is special: interests only one single delegate
if (eventName == QStringLiteral("page_gained_focus")) {
int pos = data.value(QStringLiteral("number")).toInt();
if (pos >= 0 && pos < delegates.count()) {
AbstractDelegate *delegate = delegates[pos];
delegate->forceActiveFocus((Qt::FocusReason)ServerEventFocusReason);
emit delegate->guiEvent(eventName, data);
}
} else if (eventName == QStringLiteral("mycroft.gui.close.screen")) {
emit activeSkillClosed();
} else {
for (auto *delegate : delegates) {
emit delegate->guiEvent(eventName, data);
}
}
} else {
qWarning() << "Unrecognized operation" << type;
}
//END EVENTS
}
#include "moc_abstractskillview.cpp"