1 #include "common/ResourcePaths.h"
2 
3 #include <QLibraryInfo>
4 #include <QDir>
5 #include <QFileInfo>
6 #include <QApplication>
7 #include <QDebug>
8 
9 
10 #ifdef APPIMAGE
appimageRoot()11 static QDir appimageRoot()
12 {
13     auto dir = QDir(QCoreApplication::applicationDirPath()); // appimage/usr/bin
14     dir.cdUp(); // /usr
15     dir.cdUp(); // /
16     return dir;
17 }
18 #endif
19 
substitutePath(QString path)20 static QString substitutePath(QString path)
21 {
22 #ifdef APPIMAGE
23     if (path.startsWith("/usr")) {
24         return appimageRoot().absoluteFilePath("." + path);
25     }
26 #endif
27     return path;
28 }
29 
30 /**
31  * @brief Substitute or filter paths returned by standardLocations based on Cutter package kind.
32  * @param paths list of paths to process
33  * @return
34  */
substitutePaths(const QStringList & paths)35 static QStringList substitutePaths(const QStringList &paths)
36 {
37     QStringList result;
38     result.reserve(paths.size());
39     for (auto &path : paths) {
40         // consider ignoring some of system folders for portable packages here or standardLocations if it depends on path type
41         result.push_back(substitutePath(path));
42     }
43     return result;
44 }
45 
locateAll(QStandardPaths::StandardLocation type,const QString & fileName,QStandardPaths::LocateOptions options)46 QStringList Cutter::locateAll(QStandardPaths::StandardLocation type, const QString &fileName,
47                               QStandardPaths::LocateOptions options)
48 {
49     // This function is reimplemented here instead of forwarded to Qt becauase existence check needs to be done
50     // after substitutions
51     QStringList result;
52     for (auto path : standardLocations(type)) {
53         QString filePath = path + QLatin1Char('/') + fileName;
54         bool exists = false;
55         if (options & QStandardPaths::LocateDirectory) {
56             exists = QDir(filePath).exists();
57         } else {
58             exists = QFileInfo(filePath).isFile();
59         }
60         if (exists) {
61             result.append(filePath);
62         }
63     }
64     return result;
65 }
66 
standardLocations(QStandardPaths::StandardLocation type)67 QStringList Cutter::standardLocations(QStandardPaths::StandardLocation type)
68 {
69     return substitutePaths(QStandardPaths::standardLocations(type));
70 }
71 
writableLocation(QStandardPaths::StandardLocation type)72 QString Cutter::writableLocation(QStandardPaths::StandardLocation type)
73 {
74     return substitutePath(QStandardPaths::writableLocation(type));
75 }
76 
getTranslationsDirectories()77 QStringList Cutter::getTranslationsDirectories()
78 {
79     auto result = locateAll(QStandardPaths::DataLocation, "translations",
80                             QStandardPaths::LocateDirectory);
81     result << QLibraryInfo::location(QLibraryInfo::TranslationsPath);
82     return result;
83 }
84 
85