/
launcherfoldermodel.cpp
839 lines (715 loc) · 26.2 KB
/
launcherfoldermodel.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
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
// This file is part of lipstick, a QML desktop library
//
// Copyright (c) 2014-2017 Jolla Ltd.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License version 2.1 as published by the Free Software Foundation
// and appearing in the file LICENSE.LGPL included in the packaging
// of this file.
//
// This code is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
#include "launcheritem.h"
#include "launcherfoldermodel.h"
#include "launchermodel.h"
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
#include <QFile>
#include <QFileInfo>
#include <QTemporaryFile>
#include <QStandardPaths>
#include <QDir>
#include <QStack>
#include <mdesktopentry.h>
#include <glib.h>
#include <QDebug>
static const int FOLDER_MODEL_SAVE_TIMER_MS = 1000;
static const QString CONFIG_FOLDER_SUBDIRECTORY("/lipstick/");
static const QString CONFIG_MENU_FILENAME("applications.menu");
static const QString DEFAULT_ICON_ID("icon-launcher-folder-01");
static QString configDir()
{
return QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + CONFIG_FOLDER_SUBDIRECTORY;
}
static QString absoluteConfigPath(const QString &fileName)
{
return configDir() + fileName;
}
static QString buildBlacklistValue(const QString &directoryId, int i)
{
QString indexStr = QString::number(i);
return QString(directoryId.isEmpty() ? indexStr : (directoryId + "-" + indexStr));
}
static void getDirAndIndex(const QString &positionId, QString &directoryId, int &index)
{
QStringList parts = positionId.split("-");
QString strIndex = parts.takeLast();
if (!parts.isEmpty()) {
directoryId = parts.join("");
}
index = strIndex.toInt();
}
struct FolderItem {
FolderItem(LauncherFolderItem *folder, LauncherItem *item)
: folder(folder)
, item(item)
{
}
LauncherFolderItem *folder;
LauncherItem *item;
};
// This is modeled after the freedesktop.org menu files http://standards.freedesktop.org/menu-spec/latest/
// but handles only the basic elements, i.e. no merging, filtering, layout, etc. is supported.
LauncherFolderItem::LauncherFolderItem(QObject *parent)
: QObjectListModel(parent), m_iconId(DEFAULT_ICON_ID)
{
connect(this, SIGNAL(itemRemoved(QObject*)), this, SLOT(handleRemoved(QObject*)));
connect(this, SIGNAL(itemAdded(QObject*)), this, SLOT(handleAdded(QObject*)));
connect(this, SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)), this, SIGNAL(saveNeeded()));
}
LauncherModel::ItemType LauncherFolderItem::type() const
{
return LauncherModel::Folder;
}
const QString &LauncherFolderItem::title() const
{
return m_title;
}
void LauncherFolderItem::setTitle(const QString &title)
{
if (title == m_title)
return;
m_title = title;
emit titleChanged();
emit saveNeeded();
}
const QString &LauncherFolderItem::iconId() const
{
return m_iconId;
}
void LauncherFolderItem::setIconId(const QString &icon)
{
if (icon == m_iconId)
return;
m_iconId = icon;
saveDirectoryFile();
emit iconIdChanged();
}
bool LauncherFolderItem::isUpdating() const
{
LauncherFolderItem *me = const_cast<LauncherFolderItem*>(this);
for (int i = 0; i < rowCount(); ++i) {
const LauncherItem *launcherItem = qobject_cast<const LauncherItem*>(me->get(i));
if (launcherItem && launcherItem->isUpdating())
return true;
}
return false;
}
int LauncherFolderItem::updatingProgress() const
{
int updatingCount = 0;
int updatingTotal = 0;
LauncherFolderItem *me = const_cast<LauncherFolderItem*>(this);
for (int i = 0; i < rowCount(); ++i) {
const LauncherItem *launcherItem = qobject_cast<const LauncherItem*>(me->get(i));
if (launcherItem && launcherItem->isUpdating()) {
int progress = launcherItem->updatingProgress();
if (progress < 0 || progress > 100)
return progress;
++updatingCount;
updatingTotal += progress;
}
}
return updatingCount ? updatingTotal / updatingCount : 0;
}
LauncherFolderItem *LauncherFolderItem::parentFolder() const
{
return m_parentFolder;
}
void LauncherFolderItem::setParentFolder(LauncherFolderItem *parent)
{
if (parent == m_parentFolder)
return;
m_parentFolder = parent;
emit parentFolderChanged();
}
// Creates a folder and moves the item at that index into the folder
LauncherFolderItem *LauncherFolderItem::createFolder(int index, const QString &name)
{
if (index < 0 || index > rowCount())
return 0;
LauncherFolderItem *folder = new LauncherFolderItem(this);
folder->setTitle(name);
folder->setParentFolder(this);
QObject *item = get(index);
insertItem(index, folder);
if (item) {
removeItem(item);
folder->addItem(item);
}
emit saveNeeded();
return folder;
}
void LauncherFolderItem::destroyFolder()
{
if (itemCount() != 0)
qWarning() << "Removing a folder that is not empty.";
if (m_parentFolder)
m_parentFolder->removeItem(this);
if (!m_directoryFile.isEmpty()) {
QFile file(m_directoryFile);
file.remove();
}
emit saveNeeded();
deleteLater();
}
LauncherFolderItem *LauncherFolderItem::findContainer(QObject *item)
{
LauncherFolderItem *me = const_cast<LauncherFolderItem*>(this);
for (int i = 0; i < rowCount(); ++i) {
QObject *obj = me->get(i);
if (obj == item) {
return this;
} else if (LauncherFolderItem *subFolder = qobject_cast<LauncherFolderItem*>(obj)) {
LauncherFolderItem *folder = subFolder->findContainer(item);
if (folder)
return folder;
}
}
return 0;
}
QString LauncherFolderItem::directoryFile() const
{
return m_directoryFile;
}
void LauncherFolderItem::loadDirectoryFile(const QString &filename)
{
m_directoryFile = filename;
if (!m_directoryFile.startsWith('/')) {
m_directoryFile = absoluteConfigPath(m_directoryFile);
}
GKeyFile *keyfile = g_key_file_new();
GError *err = NULL;
if (g_key_file_load_from_file(keyfile, m_directoryFile.toLatin1(), G_KEY_FILE_NONE, &err)) {
m_iconId = QString::fromLatin1(g_key_file_get_string(keyfile, "Desktop Entry", "Icon", &err));
emit iconIdChanged();
}
if (err != NULL) {
qWarning() << "Failed to load .directory file" << err->message;
g_error_free(err);
}
g_key_file_free(keyfile);
}
void LauncherFolderItem::saveDirectoryFile()
{
QScopedPointer<QFile> dirFile;
if (m_directoryFile.isEmpty()) {
QTemporaryFile *tempFile = new QTemporaryFile(absoluteConfigPath("FolderXXXXXX.directory"));
dirFile.reset(tempFile);
tempFile->open();
tempFile->setAutoRemove(false);
m_directoryFile = tempFile->fileName();
emit directoryFileChanged();
emit saveNeeded();
} else {
dirFile.reset(new QFile(m_directoryFile));
dirFile.data()->open(QIODevice::WriteOnly);
}
if (!dirFile.data()->isOpen()) {
qWarning() << "Cannot open" << m_directoryFile;
return;
}
GKeyFile *keyfile = g_key_file_new();
GError *err = NULL;
g_key_file_load_from_file(keyfile, m_directoryFile.toLatin1(), G_KEY_FILE_NONE, &err);
g_key_file_set_string(keyfile, "Desktop Entry", "Icon", m_iconId.toLatin1());
gchar *data = g_key_file_to_data(keyfile, NULL, &err);
dirFile.data()->write(data);
dirFile.data()->close();
g_free(data);
g_key_file_free(keyfile);
}
void LauncherFolderItem::clear()
{
for (int i = 0; i < rowCount(); ++i) {
QObject *item = get(i);
LauncherItem *launcherItem = qobject_cast<LauncherItem*>(item);
LauncherFolderItem *folder = qobject_cast<LauncherFolderItem*>(item);
if (launcherItem) {
disconnect(item, SIGNAL(isTemporaryChanged()), this, SIGNAL(saveNeeded()));
} else if (folder) {
disconnect(item, SIGNAL(saveNeeded()), this, SIGNAL(saveNeeded()));
}
if (launcherItem || folder) {
disconnect(item, SIGNAL(isUpdatingChanged()), this, SIGNAL(isUpdatingChanged()));
disconnect(item, SIGNAL(updatingProgressChanged()), this, SIGNAL(updatingProgressChanged()));
}
if (folder) {
folder->clear();
folder->deleteLater();
}
}
reset();
}
void LauncherFolderItem::handleAdded(QObject *item)
{
const LauncherItem *launcherItem = qobject_cast<const LauncherItem*>(item);
const LauncherFolderItem *folder = qobject_cast<const LauncherFolderItem*>(item);
if (launcherItem) {
if (launcherItem->isUpdating()) {
emit isUpdatingChanged();
emit updatingProgressChanged();
}
connect(item, SIGNAL(isTemporaryChanged()), this, SIGNAL(saveNeeded()));
} else if (folder) {
if (folder->isUpdating()) {
emit isUpdatingChanged();
emit updatingProgressChanged();
}
connect(item, SIGNAL(saveNeeded()), this, SIGNAL(saveNeeded()));
}
if (launcherItem || folder) {
connect(item, SIGNAL(isUpdatingChanged()), this, SIGNAL(isUpdatingChanged()));
connect(item, SIGNAL(updatingProgressChanged()), this, SIGNAL(updatingProgressChanged()));
}
emit saveNeeded();
}
void LauncherFolderItem::handleRemoved(QObject *item)
{
const LauncherItem *launcherItem = qobject_cast<const LauncherItem*>(item);
const LauncherFolderItem *folder = qobject_cast<const LauncherFolderItem*>(item);
if (launcherItem) {
if (launcherItem->isUpdating()) {
emit isUpdatingChanged();
emit updatingProgressChanged();
}
disconnect(item, SIGNAL(isTemporaryChanged()), this, SIGNAL(saveNeeded()));
} else if (folder) {
if (folder->isUpdating()) {
emit isUpdatingChanged();
emit updatingProgressChanged();
}
disconnect(item, SIGNAL(saveNeeded()), this, SIGNAL(saveNeeded()));
}
if (launcherItem || folder) {
disconnect(item, SIGNAL(isUpdatingChanged()), this, SIGNAL(isUpdatingChanged()));
disconnect(item, SIGNAL(updatingProgressChanged()), this, SIGNAL(updatingProgressChanged()));
}
emit saveNeeded();
}
class DeferredLauncherModel : public LauncherModel
{
public:
explicit DeferredLauncherModel(QObject *parent = 0)
: LauncherModel(DeferInitialization, parent)
{
}
using LauncherModel::initialize;
};
//============
LauncherFolderModel::LauncherFolderModel(QObject *parent)
: LauncherFolderItem(parent)
, m_launcherModel(new DeferredLauncherModel(this))
, m_loading(false)
, m_initialized(false)
{
connect(m_launcherModel, &LauncherModel::directoriesChanged, this, &LauncherFolderModel::directoriesChanged);
connect(m_launcherModel, &LauncherModel::blacklistedApplicationsChanged, this, &LauncherFolderModel::blacklistedApplicationsChanged);
connect(m_launcherModel, &LauncherModel::blacklistedApplicationsChanged, this, &LauncherFolderModel::updateblacklistedApplications);
connect(m_launcherModel, &LauncherModel::iconDirectoriesChanged, this, &LauncherFolderModel::iconDirectoriesChanged);
connect(m_launcherModel, &LauncherModel::categoriesChanged, this, &LauncherFolderModel::categoriesChanged);
initialize();
}
LauncherFolderModel::LauncherFolderModel(InitializationMode, QObject *parent)
: LauncherFolderItem(parent)
, m_launcherModel(new DeferredLauncherModel(this))
, m_loading(false)
, m_initialized(false)
{
connect(m_launcherModel, &LauncherModel::directoriesChanged, this, &LauncherFolderModel::directoriesChanged);
connect(m_launcherModel, &LauncherModel::blacklistedApplicationsChanged, this, &LauncherFolderModel::blacklistedApplicationsChanged);
connect(m_launcherModel, &LauncherModel::blacklistedApplicationsChanged, this, &LauncherFolderModel::updateblacklistedApplications);
connect(m_launcherModel, &LauncherModel::iconDirectoriesChanged, this, &LauncherFolderModel::iconDirectoriesChanged);
connect(m_launcherModel, &LauncherModel::categoriesChanged, this, &LauncherFolderModel::categoriesChanged);
}
void LauncherFolderModel::initialize()
{
if (m_initialized)
return;
m_initialized = true;
m_launcherModel->initialize();
m_saveTimer.setSingleShot(true);
connect(m_launcherModel, SIGNAL(itemRemoved(QObject*)),
this, SLOT(appRemoved(QObject*)));
connect(m_launcherModel, SIGNAL(itemAdded(QObject*)),
this, SLOT(appAdded(QObject*)));
connect(m_launcherModel, (void (LauncherModel::*)(LauncherItem *))&LauncherModel::notifyLaunching,
this, &LauncherFolderModel::notifyLaunching);
connect(&m_saveTimer, SIGNAL(timeout()), this, SLOT(save()));
QDir config;
config.mkpath(configDir());
load();
connect(this, SIGNAL(saveNeeded()), this, SLOT(scheduleSave()));
}
QString LauncherFolderModel::scope() const
{
return m_launcherModel->scope();
}
void LauncherFolderModel::setScope(const QString &scope)
{
if (m_launcherModel->scope() != scope) {
m_launcherModel->setScope(scope);
emit scopeChanged();
if (m_initialized) {
load();
}
}
}
QStringList LauncherFolderModel::directories() const
{
return m_launcherModel->directories();
}
void LauncherFolderModel::setDirectories(QStringList dirs)
{
m_launcherModel->setDirectories(dirs);
}
QStringList LauncherFolderModel::iconDirectories() const
{
return m_launcherModel->iconDirectories();
}
void LauncherFolderModel::setIconDirectories(QStringList dirs)
{
m_launcherModel->setIconDirectories(dirs);
}
QStringList LauncherFolderModel::categories() const
{
return m_launcherModel->categories();
}
void LauncherFolderModel::setCategories(const QStringList &categories)
{
m_launcherModel->setCategories(categories);
}
void LauncherFolderModel::blacklistApps(LauncherFolderItem *folder, const QString &directoryId)
{
for (int i = 0; i < folder->rowCount(); ++i) {
LauncherItem *item = qobject_cast<LauncherItem*>(folder->get(i));
if (item && m_launcherModel->isBlacklisted(item)) {
item->setIsBlacklisted(true);
if (!m_loading) {
m_blacklistedApplications.insert(item->filePath(), buildBlacklistValue(directoryId, i));
}
} else if (LauncherFolderItem *subFolder = qobject_cast<LauncherFolderItem*>(folder->get(i))) {
blacklistApps(subFolder, subFolder->directoryFile());
}
}
}
void LauncherFolderModel::removeAppsFromBlacklist()
{
QMap<QString, QString>::iterator i = m_blacklistedApplications.begin();
// Same index can exist in subfolders. Thus, multimap.
QMultiMap<int, FolderItem*> unblacklistedItems;
while (i != m_blacklistedApplications.end()) {
LauncherItem* item = m_launcherModel->itemInModel(i.key());
if (!item) {
i = m_blacklistedApplications.erase(i);
} else if (!m_launcherModel->isBlacklisted(item)) {
QString positionId = i.value();
QString directory;
int index = -1;
getDirAndIndex(positionId, directory, index);
LauncherFolderItem *folder = nullptr;
if (!directory.isEmpty()) {
folder = findContainerFolder(directory);
}
unblacklistedItems.insert(index, new FolderItem(folder, item));
item->setIsBlacklisted(false);
i = m_blacklistedApplications.erase(i);
} else {
++i;
}
}
QMultiMap<int, FolderItem*>::iterator unblacklistIterator = unblacklistedItems.begin();
while (unblacklistIterator != unblacklistedItems.end()) {
int index = unblacklistIterator.key();
FolderItem *folderItem = unblacklistIterator.value();
if (folderItem) {
if (folderItem->folder) {
folderItem->folder->insertItem(index, folderItem->item);
} else {
insertItem(index, folderItem->item);
}
}
delete folderItem;
unblacklistIterator = unblacklistedItems.erase(unblacklistIterator);
}
}
void LauncherFolderModel::updateAppsInBlacklistedFolders()
{
QMap<QString, QString> tmpDesktopFiles;
QMap<QString, QString>::iterator i = m_blacklistedApplications.begin();
while (i != m_blacklistedApplications.end()) {
QString positionId = i.value();
QString directory;
int index = -1;
getDirAndIndex(positionId, directory, index);
if (!findContainerFolder(directory) && positionId.startsWith(directory)) {
positionId.remove(directory + "-");
tmpDesktopFiles.insert(i.key(), positionId);
i = m_blacklistedApplications.erase(i);
} else {
++i;
}
}
i = tmpDesktopFiles.begin();
while (i != tmpDesktopFiles.end()) {
m_blacklistedApplications.insert(i.key(), i.value());
++i;
}
}
LauncherFolderItem *LauncherFolderModel::findContainerFolder(const QString &directoryId) const
{
LauncherFolderModel *me = const_cast<LauncherFolderModel*>(this);
for (int i = 0; i < rowCount(); ++i) {
QObject *obj = me->get(i);
LauncherFolderItem *folder = qobject_cast<LauncherFolderItem*>(obj);
if (folder && (folder->directoryFile() == directoryId)) {
return folder;
}
}
return 0;
}
void LauncherFolderModel::setBlacklistedApplications(const QStringList &applications)
{
m_launcherModel->setBlacklistedApplications(applications);
}
QStringList LauncherFolderModel::blacklistedApplications() const
{
return m_launcherModel->blacklistedApplications();
}
// Move item to folder at index. If index < 0 the item will be appended.
bool LauncherFolderModel::moveToFolder(QObject *item, LauncherFolderItem *folder, int index)
{
if (!item || !folder)
return false;
LauncherFolderItem *source = findContainer(item);
if (!source)
return false;
source->removeItem(item);
if (index >= 0)
folder->insertItem(index, item);
else
folder->addItem(item);
if (LauncherFolderItem *f = qobject_cast<LauncherFolderItem*>(item))
f->setParentFolder(folder);
scheduleSave();
return true;
}
// An app removed from system
void LauncherFolderModel::appRemoved(QObject *item)
{
if (LauncherItem *launcherItem = qobject_cast<LauncherItem*>(item)) {
emit applicationRemoved(launcherItem);
}
LauncherFolderItem *folder = findContainer(item);
if (folder) {
folder->removeItem(item);
scheduleSave();
}
}
// An app added to system
void LauncherFolderModel::appAdded(QObject *item)
{
addItem(item);
scheduleSave();
}
void LauncherFolderModel::updateblacklistedApplications()
{
// Remove apps from blacklist that are no longer blacklisted.
removeAppsFromBlacklist();
QString positionId;
blacklistApps(this, positionId);
QMap<QString, QString>::iterator i = m_blacklistedApplications.begin();
while (i != m_blacklistedApplications.end()) {
LauncherItem *item = m_launcherModel->itemInModel(i.key());
LauncherFolderItem *folder = findContainer(item);
if (folder) {
folder->removeItem(item);
} else {
removeItem(item);
}
++i;
}
}
void LauncherFolderModel::import()
{
for (int i = 0; i < m_launcherModel->rowCount(); ++i) {
addItem(m_launcherModel->get(i));
}
}
void LauncherFolderModel::scheduleSave()
{
if (!m_loading)
m_saveTimer.start(FOLDER_MODEL_SAVE_TIMER_MS);
}
QString LauncherFolderModel::configFile()
{
return configDir() + CONFIG_MENU_FILENAME;
}
static QString configurationFileForScope(const QString &scope)
{
return !scope.isEmpty()
? configDir() + scope + QStringLiteral(".menu")
: LauncherFolderModel::configFile();
}
void LauncherFolderModel::save()
{
m_saveTimer.stop();
QFile file(configurationFileForScope(m_launcherModel->scope()));
if (!file.open(QIODevice::WriteOnly)) {
qWarning() << "Failed to save apps menu" << configFile();
return;
}
// Folder might have been removed. Let's update blacklisted apps and their folders.
// When folder is removed, an app is shifted back to main level.
updateAppsInBlacklistedFolders();
QXmlStreamWriter xml(&file);
xml.setAutoFormatting(true);
xml.writeStartDocument();
QString directoryId;
saveFolder(xml, this, directoryId);
xml.writeEndDocument();
}
void LauncherFolderModel::saveFolder(QXmlStreamWriter &xml, LauncherFolderItem *folder, const QString &directoryId)
{
xml.writeStartElement("Menu");
xml.writeTextElement("Name", folder->title());
if (!folder->directoryFile().isEmpty())
xml.writeTextElement("Directory", folder->directoryFile());
for (int i = 0; i < folder->rowCount(); ++i) {
LauncherItem *item = qobject_cast<LauncherItem*>(folder->get(i));
LauncherFolderItem *subFolder = qobject_cast<LauncherFolderItem*>(folder->get(i));
QString currentPosId = buildBlacklistValue(directoryId, i);
// Blacklisted apps have been removed from the LauncherFolderModel already over here.
// Populate the menu still so that it contains also blacklisted apps.
QStringList desktopFiles;
if (!m_blacklistedApplications.key(currentPosId).isEmpty()) {
desktopFiles = m_blacklistedApplications.keys(currentPosId);
} else if (folder && !folder->directoryFile().isEmpty()) {
// If app is in out of bounds indexes of the current folder.
QMap<QString, QString>::const_iterator iter = m_blacklistedApplications.constBegin();
while (iter != m_blacklistedApplications.constEnd()) {
QString positionId = iter.value();
QString directory;
int folderIndex;
getDirAndIndex(positionId, directory, folderIndex);
if (positionId.startsWith(directoryId) && folderIndex >= i && (i == (folder->rowCount() - 1))) {
desktopFiles.append(iter.key());
}
++iter;
}
}
for (const QString &desktopFile : desktopFiles) {
if (LauncherItem * item = m_launcherModel->itemInModel(desktopFile)) {
xml.writeTextElement("Filename", item->filename());
}
}
if (item) {
if (!item->isTemporary()) {
xml.writeTextElement("Filename", item->filename());
}
} else if (subFolder) {
saveFolder(xml, subFolder, subFolder->directoryFile());
}
}
xml.writeEndElement();
}
void LauncherFolderModel::load()
{
m_loading = true;
clear();
QFile file(configurationFileForScope(m_launcherModel->scope()));
if (!file.open(QIODevice::ReadOnly)) {
// We haven't saved a folder model yet - import all apps.
import();
m_loading = false;
return;
}
QVector<bool> loadedItems(m_launcherModel->itemCount());
loadedItems.fill(false);
QStack<LauncherFolderItem*> menus;
QString textData;
QXmlStreamReader xml(&file);
while (!xml.atEnd()) {
xml.readNext();
if (xml.isStartElement()) {
if (xml.name() == QLatin1String("Menu")) {
LauncherFolderItem *folder = 0;
if (menus.isEmpty())
folder = this;
else
folder = new LauncherFolderItem(this);
if (!menus.isEmpty()) {
folder->setParentFolder(menus.top());
menus.top()->addItem(folder);
}
menus.push(folder);
}
} else if (xml.isEndElement()) {
if (xml.name() == QLatin1String("Menu")) {
menus.pop();
} else if (xml.name() == QLatin1String("Name")) {
if (!menus.isEmpty()) {
menus.top()->setTitle(textData);
}
} else if (xml.name() == QLatin1String("Directory")) {
if (!menus.isEmpty()) {
LauncherFolderItem *folder = menus.top();
folder->loadDirectoryFile(textData);
}
} else if (xml.name() == QLatin1String("Filename")) {
if (!menus.isEmpty()) {
int idx = m_launcherModel->indexInModel(textData);
if (idx >= 0) {
loadedItems[idx] = true;
LauncherItem *item = qobject_cast<LauncherItem*>(m_launcherModel->get(idx));
if (item) {
LauncherFolderItem *folder = menus.top();
if (!item->isBlacklisted()) {
folder->addItem(item);
} else {
QString positionId;
// Current count == normal index.
int lastIndex = folder->itemCount();
if (!folder->parentFolder()) {
positionId = QString::number(lastIndex);
} else {
positionId = QString("%1-%2").arg(folder->directoryFile()).arg(lastIndex);
}
m_blacklistedApplications.insert(item->filePath(), positionId);
}
}
}
}
}
textData.clear();
} else if (xml.isCharacters()) {
textData = xml.text().toString();
}
}
for (int i = 0; i < loadedItems.count(); ++i) {
if (!loadedItems.at(i)) {
LauncherItem *item = qobject_cast<LauncherItem*>(m_launcherModel->get(i));
if (item) {
if (!item->isBlacklisted()) {
addItem(item);
} else {
m_blacklistedApplications.insert(item->filePath(), QString::number(itemCount()));
}
}
}
}
m_loading = false;
}