-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinstaller.cpp
More file actions
1269 lines (1063 loc) · 40.7 KB
/
installer.cpp
File metadata and controls
1269 lines (1063 loc) · 40.7 KB
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
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
/*
Copyright (c) 2020 Queen Mary, University of London
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the names of the Centre for
Digital Music and Queen Mary, University of London shall not be
used in advertising or otherwise to promote the sale, use or other
dealings in this Software without prior written authorization.
*/
#include <QApplication>
#include <QString>
#include <QFile>
#include <QDir>
#include <QDialog>
#include <QFrame>
#include <QVBoxLayout>
#include <QCheckBox>
#include <QDialogButtonBox>
#include <QLabel>
#include <QFont>
#include <QFontInfo>
#include <QTemporaryFile>
#include <QMutex>
#include <QMutexLocker>
#include <QProcess>
#include <QToolButton>
#include <QPushButton>
#include <QMessageBox>
#include <QSvgRenderer>
#include <QPainter>
#include <QFontMetrics>
#include <QSpacerItem>
#include <QProgressDialog>
#include <QThread>
#include <QDateTime>
#include <QTimer>
#include <QRegularExpression>
#include <QScrollArea>
#include "base/Debug.h"
#include <vamp-hostsdk/PluginHostAdapter.h>
#include <dataquay/BasicStore.h>
#include <dataquay/RDFException.h>
#include <iostream>
#include <memory>
#include <set>
#if defined (Q_OS_MAC)
#include <sys/types.h>
#include <sys/sysctl.h>
#endif
#include "version.h"
using namespace Dataquay;
using std::vector;
using std::map;
using std::set;
using std::pair;
using std::unique_ptr;
using std::function;
using std::shared_ptr;
using std::make_shared;
QString
getDefaultInstallDirectory()
{
auto pathList = Vamp::PluginHostAdapter::getPluginPath();
if (pathList.empty()) {
// Build note: if the endl on the next line gives you an
// ambiguous overload error, that could mean you are building
// against Qt5 (we now expect Qt6)
SVCERR << "Failed to look up Vamp plugin path" << endl;
return QString();
}
auto firstPath = *pathList.begin();
QString target = QString::fromUtf8(firstPath.c_str(), firstPath.size());
return target;
}
QStringList
getPluginLibraryList()
{
QDir dir(":out/");
auto entries = dir.entryList({ "*.so", "*.dll", "*.dylib" });
for (auto e: entries) {
SVCERR << e.toStdString() << endl;
}
return entries;
}
void
loadLibraryRdf(BasicStore &store, QString filename)
{
QFile f(filename);
if (!f.open(QFile::ReadOnly | QFile::Text)) {
SVCERR << "Failed to open RDF resource file "
<< filename.toStdString() << endl;
return;
}
QByteArray content = f.readAll();
f.close();
try {
store.importString(QString::fromUtf8(content),
Uri("file:" + filename),
BasicStore::ImportIgnoreDuplicates);
} catch (const RDFException &ex) {
SVCERR << "Failed to import RDF resource file "
<< filename.toStdString() << ": " << ex.what() << endl;
}
}
unique_ptr<BasicStore>
loadLibrariesRdf()
{
unique_ptr<BasicStore> store(new BasicStore);
vector<QString> dirs { ":rdf/plugins", ":out" };
for (auto d: dirs) {
for (auto e: QDir(d).entryList({ "*.ttl", "*.n3" })) {
SVCERR << "Loading plugin RDF from " << (d + "/" + e) << endl;
loadLibraryRdf(*store, d + "/" + e);
}
}
return store;
}
struct LibraryInfo {
QString id;
QString fileName;
QString title;
QString maker;
QString description;
QString page;
QStringList pluginTitles;
QString licence;
};
struct Licence
{
static QString gpl;
static QString gpl2;
static QString gpl3;
static QString agpl;
static QString apache;
static QString mit;
};
QString Licence::gpl = "GNU General Public License";
QString Licence::gpl2 = "GNU General Public License, version 2";
QString Licence::gpl3 = "GNU General Public License, version 3";
QString Licence::agpl = "GNU Affero General Public License";
QString Licence::apache = "Apache License";
QString Licence::mit = "MIT License";
QString
identifyLicence(QString libraryBasename)
{
QString licenceFile = QString(":out/%1_COPYING.txt").arg(libraryBasename);
QFile f(licenceFile);
if (!f.open(QFile::ReadOnly | QFile::Text)) {
SVCERR << "Failed to open licence file "
<< licenceFile.toStdString() << endl;
return {};
}
QByteArray content = f.readAll();
f.close();
QString licenceText = QString::fromUtf8(content);
// NB these are not expected to identify an arbitrary licence! We
// know we have only a limited set here. But we do want to
// determine this from the actual licence text included with the
// plugin distribution, not just from e.g. RDF metadata
if (licenceText.contains(Licence::gpl.toUpper(), Qt::CaseSensitive)) {
if (licenceText.contains("Version 3, 29 June 2007")) {
return Licence::gpl3;
} else if (licenceText.contains("Version 2, June 1991")) {
return Licence::gpl2;
} else {
return Licence::gpl;
}
}
if (licenceText.contains(Licence::agpl.toUpper(), Qt::CaseSensitive)) {
return Licence::agpl;
}
if (licenceText.contains(Licence::apache)) {
return Licence::apache;
}
if (licenceText.contains("Permission is hereby granted, free of charge, to any person")) {
return Licence::mit;
}
SVCERR << "Didn't recognise licence for " << libraryBasename << endl;
return {};
}
QString
getLicenceURL(QString licence)
{
if (licence == Licence::gpl ||
licence == Licence::gpl3) {
return "https://www.gnu.org/licenses/gpl-3.0.en.html";
} else if (licence == Licence::gpl2) {
return "https://www.gnu.org/licenses/old-licenses/gpl-2.0.html";
} else if (licence == Licence::agpl) {
return "https://www.gnu.org/licenses/agpl-3.0.html";
} else if (licence == Licence::apache) {
return "https://www.apache.org/licenses/LICENSE-2.0";
} else if (licence == Licence::mit) {
return "https://opensource.org/licenses/MIT";
}
return {};
}
vector<LibraryInfo>
getLibraryInfo(const Store &store, QStringList libraries)
{
/* e.g.
plugbase:library a vamp:PluginLibrary ;
vamp:identifier "qm-vamp-plugins" ;
dc:title "Queen Mary plugin set"
*/
Triples tt = store.match(Triple(Node(),
Uri("a"),
store.expand("vamp:PluginLibrary")));
map<QString, QString> wanted; // basename -> full lib name
for (auto lib: libraries) {
wanted[QFileInfo(lib).baseName()] = lib;
}
vector<LibraryInfo> results;
for (auto t: tt) {
Node libId = store.complete(Triple(t.subject(),
store.expand("vamp:identifier"),
Node()));
if (libId.type != Node::Literal) {
SVCERR << "No literal vamp:identifier for " << t.subject() << endl;
continue;
}
auto wi = wanted.find(libId.value);
if (wi == wanted.end()) {
SVCERR << "RDF definition for identifier " << t.subject()
<< " (library " << libId
<< ") matches no library in our expected list" << endl;
continue;
}
Node title = store.complete(Triple(t.subject(),
store.expand("dc:title"),
Node()));
if (title.type != Node::Literal) {
SVCERR << "No literal dc:title for " << t.subject() << endl;
continue;
}
LibraryInfo info;
info.id = wi->first;
info.fileName = wi->second;
info.title = title.value;
Node maker = store.complete(Triple(t.subject(),
store.expand("foaf:maker"),
Node()));
if (maker.type == Node::Literal) {
info.maker = maker.value;
} else if (maker != Node()) {
maker = store.complete(Triple(maker,
store.expand("foaf:name"),
Node()));
if (maker.type == Node::Literal) {
info.maker = maker.value;
}
}
Node desc = store.complete(Triple(t.subject(),
store.expand("dc:description"),
Node()));
if (desc.type == Node::Literal) {
info.description = desc.value;
}
Node page = store.complete(Triple(t.subject(),
store.expand("foaf:page"),
Node()));
if (page.type == Node::URI) {
info.page = page.value;
}
Triples pp = store.match(Triple(t.subject(),
store.expand("vamp:available_plugin"),
Node()));
for (auto p: pp) {
Node ptitle = store.complete(Triple(p.object(),
store.expand("dc:title"),
Node()));
if (ptitle.type == Node::Literal) {
info.pluginTitles.push_back(ptitle.value);
}
}
info.licence = identifyLicence(libId.value);
// SVCERR << "licence = " << info.licence << endl;
results.push_back(info);
wanted.erase(libId.value);
}
for (auto wp: wanted) {
SVCERR << "Failed to find any RDF information about library "
<< wp.second << endl;
}
return results;
}
bool
unbundleFile(QString filePath, QString targetPath, bool isExecutable)
{
SVCERR << "Copying " << filePath.toStdString() << " to "
<< targetPath.toStdString() << "..." << endl;
// This has to be able to work even if the destination exists, and
// to do so without deleting it first - e.g. when copying to a
// temporary file. So we open the file and copy to it ourselves
// rather than use QFile::copy
QFile source(filePath);
if (!source.open(QFile::ReadOnly)) {
SVCERR << "ERROR: Failed to read bundled file " << filePath << endl;
return {};
}
QByteArray content = source.readAll();
source.close();
QFile target(targetPath);
if (!target.open(QFile::WriteOnly)) {
SVCERR << "ERROR: Failed to write target file " << targetPath << endl;
return {};
}
if (target.write(content) != content.size()) {
SVCERR << "ERROR: Incomplete write to target file" << endl;
return {};
}
target.close();
auto permissions =
QFile::ReadOwner | QFile::WriteOwner |
QFile::ReadGroup |
QFile::ReadOther;
if (isExecutable) {
permissions |=
QFile::ExeOwner |
QFile::ExeGroup |
QFile::ExeOther;
};
if (!QFile::setPermissions(targetPath, permissions)) {
SVCERR << "Failed to set permissions on "
<< targetPath.toStdString() << endl;
return false;
}
return true;
}
struct TempFileDeleter {
TempFileDeleter(QString name) : tempFile(name) { }
TempFileDeleter(TempFileDeleter &&other) : tempFile(other.tempFile) {
other.tempFile = "";
}
~TempFileDeleter() {
if (tempFile != "") {
QFile(tempFile).remove();
}
}
QString tempFile;
TempFileDeleter(const TempFileDeleter &other) =delete;
TempFileDeleter &operator=(const TempFileDeleter &other) =delete;
TempFileDeleter &operator=(TempFileDeleter &&other) =delete;
};
#if defined (Q_OS_MAC)
static bool processIsTranslated() {
int ret = 0;
size_t size = sizeof(ret);
if (sysctlbyname("sysctl.proc_translated", &ret, &size, NULL, 0) == -1) {
if (errno == ENOENT) {
SVCERR << "processIsTranslated: no, it's native" << endl;
return false;
}
SVCERR << "processIsTranslated: an unexpected error occurred (errno = "
<< errno << ")" << endl;
return false;
}
SVCERR << "processIsTranslated: sysctl returns " << ret << endl;
return ret ? true : false;
}
#endif
struct InstalledStatus {
bool isNative;
map<QString, int> pluginVersions;
};
InstalledStatus
getLibraryPluginVersions(QString libraryFilePath)
{
static QMutex mutex;
static vector<pair<QString, shared_ptr<TempFileDeleter>>> helperFiles;
static bool initHappened = false, initSucceeded = false;
QMutexLocker locker (&mutex);
if (!initHappened) {
initHappened = true;
QStringList bundledHelperPaths; // in order of preference
#if defined (Q_OS_WIN)
SVCERR << "getLibraryPluginVersions: looks like Windows" << endl;
bundledHelperPaths << ":out/get-version.exe";
#elif defined (Q_OS_MAC)
#if (defined(__aarch64__) || defined(__arm__) || defined(_M_ARM64))
SVCERR << "getLibraryPluginVersions: looks like an ARM Mac" << endl;
bundledHelperPaths << ":out/get-version-arm64";
bundledHelperPaths << ":out/get-version-x86_64";
#elif (defined(__x86_64__) || defined(__i386__) || defined(_M_IX86) || defined(_M_X64))
if (processIsTranslated()) {
SVCERR << "getLibraryPluginVersions: looks like an Intel binary running under translation" << endl;
bundledHelperPaths << ":out/get-version-arm64";
bundledHelperPaths << ":out/get-version-x86_64";
} else {
SVCERR << "getLibraryPluginVersions: looks like an Intel Mac" << endl;
bundledHelperPaths << ":out/get-version-x86_64";
}
#else // ! ARM64 and ! x86_64 (we don't know what to do)
SVCERR << "getLibraryPluginVersions: a Mac, but I don't know what sort" << endl;
bundledHelperPaths << ":out/get-version";
#endif
#else // ! Q_OS_WIN32 and ! Q_OS_MAC
SVCERR << "getLibraryPluginVersions: not Windows or Mac" << endl;
bundledHelperPaths << ":out/get-version";
#endif
for (auto path: bundledHelperPaths) {
QTemporaryFile tempFile;
tempFile.setAutoRemove(false);
if (!tempFile.open()) {
SVCERR << "ERROR: Failed to open a temporary file" << endl;
return {};
}
// We can't make the QTemporaryFile static, as it will
// hold the file open and that prevents us from executing
// it. Hence the separate deleter.
QString tempFileName = tempFile.fileName();
auto deleter = make_shared<TempFileDeleter>(tempFileName);
tempFile.close();
if (!unbundleFile(path, tempFileName, true)) {
SVCERR << "ERROR: Failed to unbundle helper from \"" << path
<< "\"" << endl;
return {};
}
helperFiles.push_back({ tempFileName, deleter });
}
initSucceeded = true;
}
if (!initSucceeded) {
return {};
}
bool native = true;
for (const auto &h: helperFiles) {
QString helper = h.first;
QProcess process;
process.start(helper, { libraryFilePath });
if (!process.waitForStarted()) {
QProcess::ProcessError err = process.error();
if (err == QProcess::FailedToStart) {
SVCERR << "Unable to start helper process " << helper << endl;
} else if (err == QProcess::Crashed) {
SVCERR << "Helper process " << helper
<< " crashed on startup" << endl;
} else {
SVCERR << "Helper process " << helper
<< " failed on startup with error code " << err << endl;
}
continue;
}
process.waitForFinished();
QByteArray stdOut = process.readAllStandardOutput();
QByteArray stdErr = process.readAllStandardError();
QString errStr = QString::fromUtf8(stdErr);
if (!errStr.isEmpty()) {
SVCERR << "Note: Helper process stderr follows:" << endl;
SVCERR << errStr << endl;
SVCERR << "Note: Helper process stderr ends" << endl;
}
QStringList lines = QString::fromUtf8(stdOut).split
(QRegularExpression("[\\r\\n]+"), Qt::SkipEmptyParts);
map<QString, int> versions;
for (QString line: lines) {
QStringList parts = line.split(":");
if (parts.size() != 2) {
SVCERR << "Unparseable output line: " << line << endl;
continue;
}
bool ok = false;
int version = parts[1].toInt(&ok);
if (!ok) {
SVCERR << "Unparseable version number in line: " << line << endl;
continue;
}
versions[parts[0]] = version;
}
if (!versions.empty()) {
return { native, versions };
}
native = false;
}
return {};
}
InstalledStatus
getBundledLibraryPluginVersions(QString libraryFileName)
{
QString tempFileName;
unique_ptr<TempFileDeleter> deleter;
{
QTemporaryFile tempFile;
tempFile.setAutoRemove(false);
if (!tempFile.open()) {
SVCERR << "ERROR: Failed to open a temporary file" << endl;
return {};
}
// We can't use QTemporaryFile's auto-remove, as it will hold
// the file open and that prevents us from executing it. Hence
// the separate deleter.
tempFileName = tempFile.fileName();
deleter = unique_ptr<TempFileDeleter>(new TempFileDeleter(tempFileName));
tempFile.close();
}
if (!unbundleFile(":out/" + libraryFileName, tempFileName, true)) {
return {};
}
return getLibraryPluginVersions(tempFileName);
}
bool isLibraryNewer(map<QString, int> a, map<QString, int> b)
{
// a and b are maps from plugin id to plugin version for libraries
// A and B. (There is no overarching library version number.) We
// deem library A to be newer than library B if:
//
// 1. A contains a plugin id that is also in B, whose version in
// A is newer than that in B, or
//
// 2. B is not newer than A according to rule 1, and neither A or
// B is empty, and A contains a plugin id that is not in B, and B
// does not contain any plugin id that is not in A
//
// (The not-empty part of rule 2 is just to avoid false positives
// when a library or its metadata could not be read at all.)
auto containsANewerPlugin = [](const map<QString, int> &m1,
const map<QString, int> &m2) {
for (auto p: m1) {
if (m2.find(p.first) != m2.end() &&
p.second > m2.at(p.first)) {
return true;
}
}
return false;
};
auto containsANovelPlugin = [](const map<QString, int> &m1,
const map<QString, int> &m2) {
for (auto p: m1) {
if (m2.find(p.first) == m2.end()) {
return true;
}
}
return false;
};
if (containsANewerPlugin(a, b)) {
return true;
}
if (!containsANewerPlugin(b, a) &&
!a.empty() &&
!b.empty() &&
containsANovelPlugin(a, b) &&
!containsANovelPlugin(b, a)) {
return true;
}
return false;
}
QString
versionsString(const map<QString, int> &vv)
{
QStringList pv;
for (auto v: vv) {
pv.push_back(QString("%1:%2").arg(v.first).arg(v.second));
}
return "{ " + pv.join(", ") + " }";
}
enum class RelativeStatus {
New,
Same,
Upgrade,
Downgrade,
UpgradeArchitecture,
TargetNotLoadable
};
QString
relativeStatusLabel(RelativeStatus status) {
switch (status) {
case RelativeStatus::New: return QObject::tr("Not yet installed");
case RelativeStatus::Same: return QObject::tr("Already installed");
case RelativeStatus::Upgrade: return QObject::tr("Update");
case RelativeStatus::Downgrade: return QObject::tr("Newer version installed");
case RelativeStatus::UpgradeArchitecture: return QObject::tr("Installed version is not native");
case RelativeStatus::TargetNotLoadable: return QObject::tr("Installed version not loadable");
default: return {};
}
}
RelativeStatus
getRelativeStatus(LibraryInfo info, QString targetDir)
{
QString destination = targetDir + "/" + info.fileName;
SVCERR << "\ngetRelativeStatus: " << info.fileName << ":\n";
if (!QFileInfo(destination).exists()) {
SVCERR << " - relative status: " << relativeStatusLabel(RelativeStatus::New) << endl;
return RelativeStatus::New;
}
RelativeStatus status = RelativeStatus::Same;
auto packaged = getBundledLibraryPluginVersions(info.fileName);
auto installed = getLibraryPluginVersions(destination);
SVCERR << " * installed: "
<< versionsString(installed.pluginVersions)
<< (installed.isNative ? "" : "(non-native)")
<< "\n * packaged: "
<< versionsString(packaged.pluginVersions)
<< (packaged.isNative ? "" : "(non-native)")
<< endl;
if (installed.pluginVersions.empty()) {
status = RelativeStatus::TargetNotLoadable;
}
if (isLibraryNewer(installed.pluginVersions, packaged.pluginVersions)) {
status = RelativeStatus::Downgrade;
}
if (isLibraryNewer(packaged.pluginVersions, installed.pluginVersions)) {
status = RelativeStatus::Upgrade;
}
if (!installed.isNative) {
status = RelativeStatus::UpgradeArchitecture;
}
SVCERR << " - relative status: " << relativeStatusLabel(status) << endl;
return status;
}
bool
backup(QString filePath, QString backupDir)
{
QFileInfo file(filePath);
if (!file.exists()) {
return true;
}
if (!QDir(backupDir).exists()) {
QDir().mkpath(backupDir);
}
QString backup = backupDir + "/" + file.fileName() + ".bak";
SVCERR << "Note: existing file " << filePath
<< " found, backing up to " << backup << endl;
if (!QFile(filePath).rename(backup)) {
SVCERR << "Failed to move " << filePath.toStdString()
<< " to backup " << backup.toStdString() << endl;
return false;
}
return true;
}
QString
installLibrary(LibraryInfo info, QString targetDir)
{
QString library = info.fileName;
QString source = ":out";
QString destination = targetDir + "/" + library;
static QString backupDirName;
if (backupDirName == "") {
// Static so as to be created once - don't go creating a
// second directory if the clock ticks over by one second
// between library installs
backupDirName =
QString("saved-%1").arg(QDateTime::currentDateTime().toString
("yyyyMMdd-hhmmss"));
}
QString backupDir = targetDir + "/" + backupDirName;
if (!QDir(targetDir).exists()) {
if (!QDir().mkpath(targetDir)) {
return QObject::tr("Failed to create target directory");
}
}
if (!backup(destination, backupDir)) {
return QObject::tr("Failed to move aside existing library");
}
if (!unbundleFile(source + "/" + library, destination, true)) {
return QObject::tr("Failed to copy library file to target directory");
}
QString base = QFileInfo(library).baseName();
QList<QDir> from;
from << QDir(source);
from << QDir(":rdf/plugins");
for (auto dir: from) {
auto entries = dir.entryList({ base + "*" });
for (auto e: entries) {
if (e == library) continue;
QString destination = targetDir + "/" + e;
if (!backup(destination, backupDir)) {
continue;
}
if (!unbundleFile(dir.filePath(e), destination, false)) {
continue;
}
}
}
return {};
}
QString
getHelpText(vector<LibraryInfo> libraries)
{
set<QString, function<bool (QString, QString)>>
makers
([](QString k1, QString k2) {
return k1.localeAwareCompare(k2) < 0;
});
for (auto info: libraries) {
makers.insert(info.maker);
}
QString makerList;
for (QString maker: makers) {
makerList += QObject::tr("<li>%1</li>").arg(maker);
}
return QObject::tr
("<p>Vamp Plugin Pack collects together a number of <a href=\"https://vamp-plugins.org\">Vamp audio analysis plugins</a> into a single installer.</p>"
"<p>The libraries you select will be installed into the standard Vamp plugin directory, where hosts such as <a href=\"https://sonicvisualiser.org/\">Sonic Visualiser</a> can find them.</p>"
"<p>The plugin libraries included here were developed and published by various different authors and institutions:</p><ul>%1</ul>"
"<p>All of the libraries are open source and are redistributable under open-source licences. Click the information icon to the right of each library in the main window for more details.</p>"
"<p>The entire pack may be redistributed under the <a href=\"%2\">GNU Affero General Public License v3</a>.</p>"
"<p>The plugins were collected together, and the installer was written and published, at the <a href=\"https://c4dm.eecs.qmul.ac.uk\">Centre for Digital Music</a>, Queen Mary University of London.</p>")
.arg(makerList)
.arg(getLicenceURL(Licence::agpl));
}
vector<LibraryInfo>
getUserApprovedPluginLibraries(vector<LibraryInfo> libraries,
QString targetDir)
{
QDialog dialog;
int fontHeight = QFontMetrics(dialog.font()).height();
int dpratio = dialog.devicePixelRatio();
auto mainLayout = new QGridLayout;
mainLayout->setSpacing(0);
dialog.setLayout(mainLayout);
int mainRow = 0;
auto selectionScroll = new QScrollArea;
selectionScroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
selectionScroll->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
selectionScroll->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
selectionScroll->setWidgetResizable(true);
mainLayout->addWidget(selectionScroll, mainRow, 0);
++mainRow;
auto selectionFrame = new QWidget;
auto selectionLayout = new QGridLayout;
selectionLayout->setContentsMargins(0, 0, 0, 0);
selectionLayout->setSpacing(fontHeight / 6);
selectionFrame->setLayout(selectionLayout);
int selectionRow = 0;
int checkColumn = 0;
int titleColumn = 1;
int statusColumn = 2;
int infoColumn = 4; // column 3 is a small sliver of spacing
QString additionalNote = "";
if (sizeof(char *) == 4) {
additionalNote = QObject::tr("(32-bit)");
}
selectionLayout->addWidget
(new QLabel(QObject::tr("<b>Vamp Plugin Pack</b> v%1 %2")
.arg(PACK_VERSION)
.arg(additionalNote)),
selectionRow, titleColumn, 1, 3);
++selectionRow;
selectionLayout->addWidget
(new QLabel(QObject::tr("Select the plugin libraries to install:")),
selectionRow, titleColumn, 1, 3);
++selectionRow;
auto checkAll = new QCheckBox;
checkAll->setChecked(true);
selectionLayout->addWidget
(checkAll, selectionRow, checkColumn, Qt::AlignHCenter);
++selectionRow;
auto checkArrow = new QLabel(
#ifdef Q_OS_MAC
" ▼"
#else
"▼"
#endif
);
checkArrow->setTextFormat(Qt::RichText);
selectionLayout->addWidget
(checkArrow, selectionRow, checkColumn, Qt::AlignHCenter);
++selectionRow;
map<QString, QCheckBox *> checkBoxMap; // filename -> checkbox
map<QString, LibraryInfo> libFileInfo; // filename -> info
map<QString, RelativeStatus> statuses; // filename -> status
map<QString, LibraryInfo, function<bool (QString, QString)>>
orderedInfo
([](QString k1, QString k2) {
return k1.localeAwareCompare(k2) < 0;
});
for (auto info: libraries) {
orderedInfo[info.title] = info;
}
QPixmap infoMap(fontHeight * dpratio, fontHeight * dpratio);
QPixmap moreMap(fontHeight * dpratio * 2, fontHeight * dpratio * 2);
infoMap.fill(Qt::transparent);
moreMap.fill(Qt::transparent);
QSvgRenderer renderer(QString(":icons/scalable/info.svg"));
QPainter painter;
painter.begin(&infoMap);
renderer.render(&painter);
painter.end();
painter.begin(&moreMap);
renderer.render(&painter);
painter.end();
auto shouldCheck = [](RelativeStatus status) {
return (status == RelativeStatus::New ||
status == RelativeStatus::Upgrade ||
status == RelativeStatus::UpgradeArchitecture ||
status == RelativeStatus::TargetNotLoadable);
};
for (auto ip: orderedInfo) {
auto cb = new QCheckBox;
selectionLayout->addWidget
(cb, selectionRow, checkColumn, Qt::AlignHCenter);
LibraryInfo info = ip.second;
auto shortLabel = new QLabel(info.title);
selectionLayout->addWidget(shortLabel, selectionRow, titleColumn);
RelativeStatus relativeStatus = getRelativeStatus(info, targetDir);
auto statusLabel = new QLabel(relativeStatusLabel(relativeStatus));
selectionLayout->addWidget(statusLabel, selectionRow, statusColumn);
cb->setChecked(shouldCheck(relativeStatus));
auto infoButton = new QToolButton;
infoButton->setAutoRaise(true);
infoButton->setIcon(infoMap);
infoButton->setIconSize(QSize(fontHeight, fontHeight));
#ifdef Q_OS_MAC
infoButton->setFixedSize(QSize(int(fontHeight * 1.2),
int(fontHeight * 1.2)));
infoButton->setStyleSheet("QToolButton { border: none; }");
#endif
selectionLayout->addWidget(infoButton, selectionRow, infoColumn);
++selectionRow;
QString moreTitleText = QObject::tr("<b>%1</b><br><i>%2</i>")
.arg(info.title)
.arg(info.maker);
QString moreInfoText = info.description;
if (info.page != "") {
moreInfoText += QObject::tr("<br><a href=\"%1\">%2</a>")
.arg(info.page)
.arg(info.page);
}
moreInfoText += QObject::tr("<br><br>Library contains:<ul>");