File indexing completed on 2024-04-21 03:42:17

0001 /*
0002     SPDX-FileCopyrightText: 2016 Artem Fedoskin <afedoskin3@gmail.com>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "auxiliary/kspaths.h"
0007 #include <QFileInfo>
0008 #include <QCoreApplication>
0009 
0010 QString KSPaths::locate(QStandardPaths::StandardLocation location, const QString &fileName,
0011                         QStandardPaths::LocateOptions options)
0012 {
0013     QString findings = QStandardPaths::locate(location, fileName, options);
0014 
0015     // If there was no result and we are running a test, if the location contains the app name, retry with installed name
0016     if (findings.isEmpty() && QStandardPaths::isTestModeEnabled())
0017     {
0018         switch(location)
0019         {
0020             case QStandardPaths::DataLocation:
0021             case QStandardPaths::AppDataLocation:
0022                 findings = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QDir("kstars").filePath(fileName), options);
0023                 break;
0024 
0025             case QStandardPaths::CacheLocation:
0026                 findings = QStandardPaths::locate(QStandardPaths::GenericCacheLocation, QDir("kstars").filePath(fileName), options);
0027                 break;
0028 
0029             case QStandardPaths::AppConfigLocation:
0030                 findings = QStandardPaths::locate(QStandardPaths::GenericConfigLocation, QDir("kstars").filePath(fileName), options);
0031                 break;
0032 
0033             default:
0034                 break;
0035         }
0036     }
0037 
0038 #ifdef ANDROID
0039     // If we are running Android, check the reserved-file area
0040     if (findings.isEmpty())
0041     {
0042         QFileInfo const rfile("/data/data/org.kde.kstars.lite/qt-reserved-files/share/kstars/" + fileName);
0043         if (rfile.exists())
0044             return rfile.filePath();
0045     }
0046 #endif
0047 
0048     return findings;
0049 }
0050 
0051 QStringList KSPaths::locateAll(QStandardPaths::StandardLocation location, const QString &fileName,
0052                                QStandardPaths::LocateOptions options)
0053 {
0054     QStringList findings = QStandardPaths::locateAll(location, fileName, options);
0055 
0056     // If there was no result and we are running a test, if the location contains the app name, retry with installed name
0057     if (findings.isEmpty() && QStandardPaths::isTestModeEnabled())
0058     {
0059         switch(location)
0060         {
0061             case QStandardPaths::DataLocation:
0062             case QStandardPaths::AppDataLocation:
0063                 findings = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QDir("kstars").filePath(fileName), options);
0064                 break;
0065 
0066             case QStandardPaths::CacheLocation:
0067                 findings = QStandardPaths::locateAll(QStandardPaths::GenericCacheLocation, QDir("kstars").filePath(fileName), options);
0068                 break;
0069 
0070             case QStandardPaths::AppConfigLocation:
0071                 findings = QStandardPaths::locateAll(QStandardPaths::GenericConfigLocation, QDir("kstars").filePath(fileName), options);
0072                 break;
0073 
0074             default:
0075                 break;
0076         }
0077     }
0078 
0079 #ifdef ANDROID
0080     // If we are running Android, check the reserved-file area
0081     if (findings.isEmpty())
0082     {
0083         QFileInfo const rfile("/data/data/org.kde.kstars.lite/qt-reserved-files/share/kstars/" + fileName);
0084         if (rfile.exists())
0085             return { rfile.filePath() };
0086     }
0087 #endif
0088 
0089     return findings;
0090 }
0091 
0092 QString KSPaths::writableLocation(QStandardPaths::StandardLocation type)
0093 {
0094     switch (type)
0095     {
0096         case QStandardPaths::GenericDataLocation:
0097         case QStandardPaths::GenericCacheLocation:
0098         case QStandardPaths::GenericConfigLocation:
0099             qWarning("Call to writableLocation without an application-based location.");
0100             break;
0101 
0102         default:
0103             break;
0104     }
0105 
0106     //Q_ASSERT_X(type != QStandardPaths::GenericDataLocation, __FUNCTION__, "GenericDataLocation is not writable.");
0107     //Q_ASSERT_X(type != QStandardPaths::GenericConfigLocation, __FUNCTION__, "GenericConfigLocation is not writable.");
0108     //Q_ASSERT_X(type != QStandardPaths::GenericCacheLocation, __FUNCTION__, "GenericCacheLocation is not writable.");
0109 
0110 #ifdef Q_OS_ANDROID
0111     // No more writableLocation calls on Generic(Data|Config|Cache)Location anymore in KStars
0112 #endif
0113 
0114 #ifdef Q_OS_WIN
0115     // As long as the application name on Windows and Linux is the same, no change here
0116 #endif
0117 
0118     return QStandardPaths::writableLocation(type);
0119 }