-
Notifications
You must be signed in to change notification settings - Fork 15
/
ornpm.cpp
1020 lines (866 loc) · 27.1 KB
/
ornpm.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
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "ornpm_p.h"
#include "ornpackageversion.h"
#include "ornrepo.h"
#include "ornutils.h"
#include "ornconst.h"
#include <connman-qt5/networkmanager.h>
#include <mlite5/MDesktopEntry>
#include <QDBusPendingCallWatcher>
#include <QtConcurrent>
#include <QProcess>
#include <QDebug>
using namespace PackageKit;
#define CHECK_INITIALISED(d) \
Q_ASSERT_X((d)->initialised, Q_FUNC_INFO, "Call only after OrnPm was initialised!")
#define CHECK_NETWORK(RET) \
if (!NetworkManager::instance()->connected()) { \
qWarning("Network is unavailable!"); \
return RET; \
}
const QLatin1String OrnPm::repoNamePrefix("openrepos-");
const QString OrnPm::storemanRepo("harbour-storeman-obs");
OrnPm::OrnPm(QObject *parent)
: QObject(*new OrnPmPrivate(), parent)
{
Q_D(OrnPm);
d->ssuInterface = new OrnSsu(this);
d->pkDaemon = new OrnPkDaemon(this);
connect_priv(d->pkDaemon, &OrnPkDaemon::UpdatesChanged, this, &OrnPmPrivate::getUpdates);
connect(d->pkDaemon, &OrnPkDaemon::TransactionListChanged, this, [this](const QStringList &transactions) {
static QSet<QString> active;
for (const auto &path : transactions)
{
if (!active.contains(path))
{
active.insert(path);
auto t = new OrnPkTransaction(path, true, this);
connect_priv(t, &OrnPkTransaction::ItemProgress, this, &OrnPmPrivate::onItemProgress);
}
}
active = transactions.toSet();
});
}
QString OrnPm::repoUrl(const QString &author)
{
return QStringLiteral("https://sailfish.openrepos.net/%0/personal/main").arg(author);
}
QString OrnPm::rpmQuery(const QString &packageFile, const QString &query)
{
QProcess rpm;
rpm.start(QStringLiteral("rpm"), {
QStringLiteral("-qp"),
QStringLiteral("--qf"),
query,
packageFile,
});
rpm.waitForFinished();
return QString::fromUtf8(rpm.readAll());
}
OrnPm *OrnPm::instance()
{
static OrnPm *instance = nullptr;
if (!instance)
{
instance = new OrnPm(qApp);
auto fw = new QFutureWatcher<void>();
connect(fw, &QFutureWatcher<void>::finished, fw, [fw]()
{
fw->deleteLater();
OrnPm::instance()->d_func()->getUpdates();
});
fw->setFuture(QtConcurrent::run(instance->d_func(), &OrnPmPrivate::initialise));
}
return instance;
}
void OrnPmPrivate::initialise()
{
solvPathTmpl = OrnUtils::systemVersion() >= QVersionNumber(3, 0, 3) ?
QStringLiteral("/home/.zypp-cache/solv/%0/solv") :
QStringLiteral("/var/cache/zypp/solv/%0/solv");
qDebug() << "Getting the list of ORN repositories";
// NOTE: A hack for SSU repos. Can break on ssu config changes.
QSettings ssuSettings(QStringLiteral("/etc/ssu/ssu.ini"), QSettings::IniFormat);
archs << ssuSettings.value(QStringLiteral("arch")).toString()
<< QStringLiteral("noarch");
auto disabled = ssuSettings.value(QStringLiteral("disabled-repos")).toStringList().toSet();
ssuSettings.beginGroup(QStringLiteral("repository-urls"));
auto aliases = ssuSettings.childKeys();
for (const auto &alias : aliases)
{
if (alias.startsWith(OrnPm::repoNamePrefix))
{
auto enabled = !disabled.contains(alias);
qDebug() << "Found" << (enabled ? "enabled" : "disabled") << "repo" << alias;
repos.insert(alias, enabled);
}
}
qDebug() << "System has" << repos.size() << "ORN repositories";
qDebug() << "Getting the Storeman OBS repository";
repos.insert(OrnPm::storemanRepo, !disabled.contains(OrnPm::storemanRepo));
qDebug() << "Getting the list of installed packages";
auto spool = pool_create();
auto srepo = repo_create(spool, OrnConst::installed.toLatin1().data());
auto systemSolv = solvPathTmpl.arg(QStringLiteral("@System"));
auto sfile = fopen(systemSolv.toUtf8().data(), "r");
if (!sfile)
{
qCritical() << "Could not read" << systemSolv;
repo_free(srepo, 0);
pool_free(spool);
return;
}
repo_add_solv(srepo, sfile, 0);
fclose(sfile);
const auto installed = QString::fromLatin1("installed");
for (int i = 0; i < spool->nsolvables; ++i)
{
auto s = &spool->solvables[i];
auto name = QString::fromLatin1(solvable_lookup_str(s, SOLVABLE_NAME));
if (name.size() > 0)
{
const auto id = QStringList{
name,
QString::fromLatin1(solvable_lookup_str(s, SOLVABLE_EVR)),
QString::fromLatin1(solvable_lookup_str(s, SOLVABLE_ARCH)),
installed,
}.join(QChar{';'});
installedPackages.insert(name, id);
}
}
repo_free(srepo, 0);
pool_free(spool);
qDebug() << installedPackages.size() << "packages are installed";
qDebug() << "Initialisation finished";
initialised = true;
emit this->q_func()->initialisedChanged();
}
bool OrnPm::initialised() const
{
return this->d_func()->initialised;
}
QVariantList OrnPm::operations() const
{
Q_D(const OrnPm);
QString item {QStringLiteral("item")};
QString operation{QStringLiteral("operation")};
QVariantList res;
for (auto op = d->operations.cbegin(); op != d->operations.cend(); ++op)
{
res << QVariantMap{
{item, op.key()},
{operation, op.value()}
};
}
return res;
}
bool OrnPm::updatesAvailable() const
{
return !this->d_func()->updatablePackages.empty();
}
QStringList OrnPm::updatablePackages() const
{
return this->d_func()->updatablePackages.keys();
}
QString OrnPm::updateVersion(const QString &packageName) const
{
Q_D(const OrnPm);
if (d->updatablePackages.contains(packageName))
{
return OrnUtils::packageVersion(d->updatablePackages[packageName]);
}
return QString();
}
OrnPm::RepoStatus OrnPm::repoStatus(const QString &alias) const
{
Q_D(const OrnPm);
if (d->repos.contains(alias))
{
return d->repos[alias] ? RepoEnabled : RepoDisabled;
}
return RepoNotInstalled;
}
/*!
This will never return \l OrnPm::PackageAvailable.
*/
OrnPm::PackageStatus OrnPm::packageStatus(const QString &packageName) const
{
Q_D(const OrnPm);
if (d->operations.contains(packageName))
{
switch (d->operations[packageName])
{
case InstallingPackage:
return PackageInstalling;
case RemovingPackage:
return PackageRemoving;
case UpdatingPackage:
return PackageUpdating;
default:
Q_UNREACHABLE();
}
}
if (d->updatablePackages.contains(packageName))
{
return PackageUpdateAvailable;
}
if (d->installedPackages.contains(packageName))
{
return PackageInstalled;
}
return PackageNotInstalled;
}
OrnPkTransaction *OrnPmPrivate::transaction()
{
auto t = pkDaemon->transaction();
QObject::connect(t, &OrnPkTransaction::ErrorCode, q_func(), &OrnPm::error);
return t;
}
OrnPkTransaction *OrnPmPrivate::currentTransaction()
{
return qobject_cast<OrnPkTransaction *>(currentSender->sender);
}
void OrnPm::getPackageVersions(const QString &packageName)
{
Q_D(OrnPm);
Q_ASSERT(!packageName.isEmpty());
CHECK_INITIALISED(d);
qDebug() << "Resolving package versions for" << packageName;
QtConcurrent::run(d, &OrnPmPrivate::preparePackageVersions, packageName);
}
void OrnPmPrivate::preparePackageVersions(const QString &packageName)
{
OrnPackageVersionList versions;
auto spool = pool_create();
auto srepo = repo_create(spool, "");
auto systemSolv = solvPathTmpl.arg(QStringLiteral("@System"));
auto sfile = fopen(systemSolv.toUtf8().data(), "r");
if (!sfile)
{
qCritical() << "Could not read" << systemSolv;
repo_free(srepo, 0);
pool_free(spool);
return;
}
repo_add_solv(srepo, sfile, 0);
fclose(sfile);
for (int i = 0; i < spool->nsolvables && versions.isEmpty(); ++i)
{
auto s = &spool->solvables[i];
if (packageName == QLatin1String(solvable_lookup_str(s, SOLVABLE_NAME)))
{
versions << OrnPackageVersion(
0,
solvable_lookup_num(s, SOLVABLE_INSTALLSIZE, 0),
QString::fromLatin1(solvable_lookup_str(s, SOLVABLE_EVR)),
QString::fromLatin1(solvable_lookup_str(s, SOLVABLE_ARCH)),
OrnConst::installed);
}
}
repo_empty(srepo, 0);
pool_free(spool);
processSolvables(true, [this, &packageName, &versions](const auto alias, auto spool) {
for (int i = 0; i < spool->nsolvables; ++i)
{
auto s = &spool->solvables[i];
if (packageName == QLatin1String{solvable_lookup_str(s, SOLVABLE_NAME)})
{
const auto arch = QString::fromLatin1(solvable_lookup_str(s, SOLVABLE_ARCH));
if (archs.contains(arch))
{
versions << OrnPackageVersion{
solvable_lookup_num(s, SOLVABLE_DOWNLOADSIZE, 0),
solvable_lookup_num(s, SOLVABLE_INSTALLSIZE, 0),
QString::fromLatin1(solvable_lookup_str(s, SOLVABLE_EVR)),
arch,
alias
};
}
}
}
});
std::sort(versions.rbegin(), versions.rend());
qDebug() << "Finished resolving versions for package" << packageName;
emit this->q_func()->packageVersions(packageName, versions);
}
void OrnPm::installPackage(const QString &packageId)
{
CHECK_NETWORK();
auto name = OrnUtils::packageName(packageId);
auto t = d_func()->packageTransaction(name, OrnPm::InstallingPackage, OrnPm::PackageInstalling, &OrnPm::packageInstalled);
if (t)
{
t->installPackages(QStringList{packageId});
}
}
void OrnPm::installFile(const QString &packageFile)
{
CHECK_NETWORK();
// GetDetailsLocal is not supported for libzypp
auto name = rpmQuery(packageFile, QStringLiteral("%{NAME}"));
auto t = d_func()->packageTransaction(name, OrnPm::InstallingPackage, OrnPm::PackageInstalling, &OrnPm::packageInstalled);
if (t)
{
t->installFiles(QStringList{packageFile});
}
}
void OrnPm::removePackage(const QString &packageId, bool autoremove)
{
auto name = OrnUtils::packageName(packageId);
auto t = d_func()->packageTransaction(name, OrnPm::RemovingPackage, OrnPm::PackageRemoving, &OrnPm::packageRemoved);
if (t)
{
t->removePackages(QStringList{packageId}, autoremove);
}
}
void OrnPm::updatePackage(const QString &packageName)
{
Q_D(OrnPm);
CHECK_NETWORK();
if (!d->updatablePackages.contains(packageName))
{
qWarning() << "The package" << packageName << "has no updates!";
return;
}
auto t = d->packageTransaction(packageName, OrnPm::UpdatingPackage, OrnPm::PackageUpdating, &OrnPm::packageUpdated);
if (t)
{
auto packageId = d->updatablePackages[packageName];
t->updatePackages(QStringList{packageId});
}
}
void OrnPm::addRepo(const QString &author)
{
Q_D(OrnPm);
CHECK_NETWORK();
auto alias = repoNamePrefix + author;
if (d->startOperation(alias, AddingRepo))
{
auto url = OrnPm::repoUrl(author);
d->ssuInterface->addRepo(alias, url);
d->onRepoModified(alias, AddRepo);
}
}
OrnPm::Operation actionOperation(OrnPm::RepoAction action)
{
switch (action)
{
case OrnPm::RemoveRepo:
return OrnPm::RemovingRepo;
case OrnPm::DisableRepo:
return OrnPm::DisablingRepo;
case OrnPm::EnableRepo:
return OrnPm::EnablingRepo;
default:
Q_UNREACHABLE();
}
}
void OrnPm::modifyRepo(const QString &alias, OrnPm::RepoAction action)
{
Q_D(OrnPm);
CHECK_NETWORK();
if (d->startOperation(alias, actionOperation(action)))
{
d->ssuInterface->modifyRepo(action, alias);
d->onRepoModified(alias, action);
}
}
void OrnPm::enableRepos(bool enable)
{
Q_D(OrnPm);
CHECK_INITIALISED(d);
auto watcher = new QFutureWatcher<bool>();
auto future = QtConcurrent::run(d, &OrnPmPrivate::enableRepos, enable);
watcher->setFuture(future);
connect(watcher, &QFutureWatcher<bool>::finished, this, [watcher, this]() {
if (watcher->result())
{
this->refreshRepos();
}
watcher->deleteLater();
});
}
void OrnPm::removeAllRepos()
{
QtConcurrent::run(this->d_func(), &OrnPmPrivate::removeAllRepos);
}
bool OrnPm::refreshingCache() const
{
for (auto op : d_func()->operations)
{
if (op == OrnPm::RefreshingRepo || op == OrnPm::RefreshingCache)
{
return true;
}
}
return false;
}
bool OrnPmPrivate::enableRepos(bool enable)
{
Q_Q(OrnPm);
CHECK_NETWORK(false);
qDebug() << (enable ? "Enabling" : "Disabling") << "all repositories";
auto action = enable ? OrnPm::EnableRepo : OrnPm::DisableRepo;
bool refresh = false;
for (auto it = repos.begin(); it != repos.end(); ++it)
{
if (*it != enable)
{
ssuInterface->modifyRepo(action, it.key());
*it = enable;
refresh = true;
}
}
if (!enable && !updatablePackages.empty())
{
updatablePackages.clear();
emit q->updatablePackagesChanged();
}
qDebug() << "Finished" << (enable ? "enabling" : "disabling") << "all repositories";
emit q->enableReposFinished();
return refresh;
}
void OrnPmPrivate::removeAllRepos()
{
Q_Q(OrnPm);
qDebug() << "Removing all repositories";
for (auto it = repos.begin(); it != repos.end(); ++it)
{
ssuInterface->modifyRepo(OrnPm::RemoveRepo, it.key());
}
repos.clear();
updatablePackages.clear();
emit q->updatablePackagesChanged();
qDebug() << "Finished removing all repositories";
emit q->removeAllReposFinished();
}
void OrnPmPrivate::onRepoModified(const QString &alias, OrnPm::RepoAction action)
{
Q_Q(OrnPm);
bool needRefresh = false;
switch (action)
{
case OrnPm::RemoveRepo:
repos.remove(alias);
break;
case OrnPm::AddRepo:
repos.insert(alias, true);
needRefresh = true;
break;
case OrnPm::DisableRepo:
repos[alias] = false;
break;
case OrnPm::EnableRepo:
repos[alias] = true;
needRefresh = true;
break;
}
auto onFinish = [this, alias, action]() {
qDebug() << "Repo" << alias << "have been modified with" << action;
emit q_func()->repoModified(alias, action);
finishOperation(alias);
};
if (needRefresh)
{
operations[alias] = OrnPm::RefreshingRepo;
emit q->operationsChanged();
auto t = this->transaction();
QObject::connect(t, &OrnPkTransaction::Finished, onFinish);
t->repoRefreshNow(alias);
}
else
{
onFinish();
this->getUpdates();
}
}
void OrnPm::refreshRepo(const QString &alias, bool force)
{
Q_D(OrnPm);
if (d->operations.contains(alias))
{
qDebug() << "Already processing repo";
return;
}
CHECK_NETWORK();
if (d->startOperation(alias, RefreshingRepo))
{
auto t = d->transaction();
connect(t, &OrnPkTransaction::Finished, this, [this, alias]()
{
this->d_func()->finishOperation(alias);
});
t->repoRefreshNow(alias, force);
}
}
void OrnPm::refreshRepos(bool force)
{
Q_D(OrnPm);
if (refreshingCache())
{
qDebug() << "Already refreshing cache";
return;
}
CHECK_NETWORK();
for (auto it = d->repos.cbegin(); it != d->repos.cend(); ++it)
{
// Refresh only enabled repositories
if (it.value())
{
d->reposToRefresh << it.key();
}
}
if (d->reposToRefresh.isEmpty())
{
qDebug() << "No enabled repositories, skipping";
return;
}
qDebug() << "Starting refresh cache for all ORN repositories";
d->pkDaemon->blockSignals(true);
d->forceRefresh = OrnUtils::stringify(force);
#ifdef QT_DEBUG
d->refreshRuntime = 0;
#endif
d->refreshNextRepo(Transaction::ExitSuccess, 0);
}
void OrnPm::refreshCache(bool force)
{
Q_D(OrnPm);
if (refreshingCache())
{
qDebug() << "Already refreshing cache";
return;
}
CHECK_NETWORK();
QString name{QStringLiteral("__orn_refreshing_cache")};
if (d->startOperation(name, OrnPm::RefreshingCache))
{
auto t = d->transaction();
connect(t, &QObject::destroyed, this, [this, name]()
{
this->d_func()->finishOperation(name);
});
t->refreshCache(force);
}
}
QList<OrnRepo> OrnPm::repoList() const
{
Q_D(const OrnPm);
OrnRepoList repos;
repos.reserve(d->repos.size());
for (auto it = d->repos.cbegin(); it != d->repos.cend(); ++it)
{
repos << OrnRepo{it.value(), it.key()};
}
return repos;
}
void OrnPm::getUnusedRepos()
{
Q_D(const OrnPm);
QStringList unused;
const auto pos = repoNamePrefix.size();
d->processSolvables(true, [d, &unused, pos](const auto &alias, auto spool) {
if (alias == storemanRepo) {
return;
}
bool nopackages = true;
for (int i = 0; i < spool->nsolvables; ++i)
{
const auto pname = QString::fromLatin1(solvable_lookup_str(&spool->solvables[i], SOLVABLE_NAME));
if (d->installedPackages.contains(pname))
{
nopackages = false;
break;
}
}
if (nopackages) {
unused.append(alias.mid(pos));
}
});
emit unusedRepos(unused);
}
void OrnPm::getInstalledPackages(const QString &packageName)
{
auto watcher = new QFutureWatcher<OrnInstalledPackageList>();
connect(watcher, &QFutureWatcher<OrnInstalledPackageList>::finished, [this, watcher]()
{
auto res = watcher->result();
if (!res.isEmpty())
{
emit this->installedPackages(res);
}
watcher->deleteLater();
});
watcher->setFuture(QtConcurrent::run(this->d_func(), &OrnPmPrivate::prepareInstalledPackages, packageName));
}
bool OrnPmPrivate::startOperation(const QString &name, OrnPm::Operation operation)
{
CHECK_INITIALISED(this);
if (operations.contains(name))
{
qWarning() << name << "is already being processed!";
return false;
}
operations.insert(name, operation);
emit this->q_func()->operationsChanged();
return true;
}
void OrnPmPrivate::finishOperation(const QString &name)
{
if (operations.remove(name))
{
emit q_func()->operationsChanged();
}
}
OrnInstalledPackageList OrnPmPrivate::prepareInstalledPackages(const QString &packageName)
{
Q_ASSERT_X(packageName.isEmpty() || installedPackages.contains(packageName), Q_FUNC_INFO,
qPrintable(QString("The provided package \"%0\" is not installed").arg(packageName)));
OrnInstalledPackageList packages;
if (installedPackages.isEmpty() || repos.isEmpty())
{
qWarning() << "Installed packages or repositories list is empty";
return packages;
}
qDebug() << "Preparing installed packages list";
QStringList iconPaths{
QStringLiteral("/usr/share/icons/hicolor/86x86/apps/%0.png"),
QStringLiteral("/usr/share/icons/hicolor/108x108/apps/%0.png"),
QStringLiteral("/usr/share/icons/hicolor/128x128/apps/%0.png"),
QStringLiteral("/usr/share/icons/hicolor/172x172/apps/%0.png"),
QStringLiteral("/usr/share/icons/hicolor/256x256/apps/%0.png"),
};
// Prepare set to filter installed packages to show only those from OpenRepos
StringSet ornPackages;
processSolvables(true, [&ornPackages]([[maybe_unused]] const auto &alias, auto spool) {
for (int i = 0; i < spool->nsolvables; ++i)
{
auto s = &spool->solvables[i];
ornPackages.insert(QString::fromLatin1(solvable_lookup_str(s, SOLVABLE_NAME)));
}
});
StringHash installed;
if (packageName.isEmpty())
{
installed = installedPackages;
}
else
{
installed[packageName] = installedPackages[packageName];
}
packages.reserve(installed.size());
for (auto it = installed.cbegin(); it != installed.cend(); ++it)
{
const auto &name = it.key();
// Actual filtering
if (!ornPackages.contains(name))
{
continue;
}
qDebug() << "Adding installed package" << name;
OrnInstalledPackage package{
updatablePackages.contains(name),
it.value(),
name,
QString{},
QString{},
QString{}
};
MDesktopEntry desktop{OrnUtils::desktopFile(name)};
if (desktop.isValid())
{
// Read pretty name
package.title = desktop.name();
package.titleUnlocalized = desktop.nameUnlocalized();
qDebug()
<< "Using name" << (package.title.isEmpty() ? name : package.title)
<< "for package" << name;
// Find icon
auto iconName = desktop.icon();
if (!iconName.isEmpty())
{
for (const auto &path : iconPaths)
{
auto iconPath = path.arg(iconName);
if (QFileInfo(iconPath).isFile())
{
qDebug() << "Using package icon" << iconPath;
package.icon = iconPath;
break;
}
}
}
}
packages << package;
}
return packages;
}
void OrnPmPrivate::onItemProgress(const QString &id, uint status, uint percentage)
{
if ( percentage < 100
|| !(status == Transaction::StatusRemove || status == Transaction::StatusInstall || status == Transaction::StatusUpdate))
{
return;
}
Q_Q(OrnPm);
auto name = OrnUtils::packageName(id);
finishOperation(name);
if (status == Transaction::StatusRemove)
{
installedPackages.remove(name);
emit q->packageStatusChanged(name, OrnPm::PackageNotInstalled);
return;
}
installedPackages[name] = id;
emit q->packageStatusChanged(name, OrnPm::PackageInstalled);
if (updatablePackages.contains(name))
{
getUpdates();
}
}
OrnPkTransaction *OrnPmPrivate::packageTransaction(const QString &packageName,
OrnPm::Operation operation,
OrnPm::PackageStatus status,
OrnPmPrivate::ornpm_signal_t sgnl)
{
if (!startOperation(packageName, operation))
{
return nullptr;
}
Q_Q(OrnPm);
emit q->packageStatusChanged(packageName, status);
auto t = transaction();
QObject::connect(t, &OrnPkTransaction::Finished, q, [this, packageName, sgnl](uint status) {
Q_Q(OrnPm);
operations.remove(packageName);
emit q->operationsChanged();
if (status == Transaction::ExitSuccess) {
emit (q->*sgnl)(packageName);
}
});
return t;
}
void OrnPmPrivate::getUpdates()
{
CHECK_NETWORK();
Q_Q(OrnPm);
auto t = this->transaction();
QObject::connect(t, &OrnPkTransaction::Package, q, [this](quint32 info, const QString &packageId, [[maybe_unused]] const QString &summary) {
Q_ASSERT(info == Transaction::InfoEnhancement);
// Filter updates only for ORN and Storeman packages
const auto alias = OrnUtils::packageRepo(packageId);
if (alias.startsWith(OrnPm::repoNamePrefix) || alias == OrnPm::storemanRepo)
{
newUpdatablePackages.insert(OrnUtils::packageName(packageId), packageId);
}
});
QObject::connect(t, &OrnPkTransaction::Finished, q, [this](quint32 status, [[maybe_unused]] quint32 runtime) {
if (status != Transaction::ExitSuccess)
{
newUpdatablePackages.clear();
return;
}
Q_Q(OrnPm);
bool newupdates = false;
// If some client listen to packageStatusChanged() and want to take a package
// update ID we swap two hashes before the checks
updatablePackages.swap(newUpdatablePackages);
auto it = updatablePackages.begin();
while (it != updatablePackages.end())
{
auto &name = it.key();
auto &id = it.value();
auto repo = OrnUtils::packageRepo(id);
// A walkaround to skip inactual updates from removed/disabled repos
if (!repos.contains(repo) || !repos[repo])
{
it = updatablePackages.erase(it);
}
else
{
// Check if the update is really new
if (!newUpdatablePackages.contains(name) ||
newUpdatablePackages[name] != id)
{
emit q->packageStatusChanged(name, OrnPm::PackageUpdateAvailable);
newupdates = true;
}
++it;
}
}
if (newupdates ||
(updatablePackages.size() != newUpdatablePackages.size()))
{
emit q->updatablePackagesChanged();
}
newUpdatablePackages.clear();
});
t->getUpdates();
}
void OrnPmPrivate::refreshNextRepo(quint32 exit, quint32 runtime)
{
Q_Q(OrnPm);
Q_UNUSED(exit)
#ifdef QT_DEBUG
refreshRuntime += runtime;
#else
Q_UNUSED(runtime)
#endif
bool offline = !NetworkManager::instance()->connected();
if (offline)
{
reposToRefresh.clear();
}
if (reposToRefresh.isEmpty())
{
#ifdef QT_DEBUG
if (offline)
{
qWarning("Aborting operation due to network gone offline");
}
else
{
qDebug() << "Finished refreshing cache for all ORN repositories in"
<< refreshRuntime << "msec";
}
#endif
pkDaemon->blockSignals(false);
}
else
{
auto alias = reposToRefresh.takeFirst();
auto t = this->transaction();
QObject::connect(t, &OrnPkTransaction::Finished, q, [this, alias](quint32 exit, quint32 runtime) {
refreshNextRepo(exit, runtime);
finishOperation(alias);
});
startOperation(alias, OrnPm::RefreshingRepo);
t->repoRefreshNow(alias, forceRefresh);
}
}
void OrnPmPrivate::processSolvables(bool enabled, std::function<void(const QString&, Pool*)> callback) const
{
auto spool = pool_create();
auto srepo = repo_create(spool, "");
for (auto it = repos.cbegin(), end = repos.cend(); it != end; ++it)
{
if (it.value() != enabled)
{
continue;
}
const auto &alias = it.key();