Improve handling of data directory
* Avoid hard-coding prefix at build time; always use a relative path so the build can always be relocated and not AppImage-specific code is needed * Avoid special handling for MacOS every time the data directory is used; handle the case of MacOS in the utility function itself
This commit is contained in:
parent
979d92a971
commit
658708d59b
|
@ -113,8 +113,7 @@ use_cpp_utilities(VISIBILITY PUBLIC)
|
|||
find_package(${PACKAGE_NAMESPACE_PREFIX}qtutilities${CONFIGURATION_PACKAGE_SUFFIX_QTUTILITIES} 6.11.0 REQUIRED)
|
||||
use_qt_utilities()
|
||||
|
||||
# configure prefix and data dir (FIXME: avoid hardcoding this and use relative lookup instead)
|
||||
add_compile_options("-DPREFIX=\"${CMAKE_INSTALL_PREFIX}\"")
|
||||
# configure prefix and data dir
|
||||
if (NOT DATA_DIR)
|
||||
set(DATA_DIR "share/games/pianobooster")
|
||||
endif ()
|
||||
|
|
|
@ -45,9 +45,7 @@ CDraw::CDraw(CSettings* settings)
|
|||
#ifndef NO_USE_FTGL
|
||||
QStringList listPathFonts;
|
||||
|
||||
listPathFonts.append(Util::dataDir()+"/fonts/DejaVuSans.ttf");
|
||||
listPathFonts.append(QApplication::applicationDirPath() + "/fonts/DejaVuSans.ttf");
|
||||
listPathFonts.append(QApplication::applicationDirPath() + "/../Resources/fonts/DejaVuSans.ttf");
|
||||
listPathFonts.append(Util::dataDir(QStringLiteral("fonts")) + "/DejaVuSans.ttf");
|
||||
listPathFonts.append("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf");
|
||||
listPathFonts.append("/usr/share/fonts/dejavu/DejaVuSans.ttf");
|
||||
listPathFonts.append("/usr/share/fonts/TTF/dejavu/DejaVuSans.ttf");
|
||||
|
|
|
@ -50,21 +50,11 @@ GuiPreferencesDialog::GuiPreferencesDialog(QWidget *parent)
|
|||
void GuiPreferencesDialog::initLanguageCombo(){
|
||||
#ifndef NO_LANGS
|
||||
|
||||
QString localeDirectory = QApplication::applicationDirPath() + "/translations/";
|
||||
|
||||
QFile fileTestLocale(localeDirectory);
|
||||
if (!fileTestLocale.exists()){
|
||||
localeDirectory=Util::dataDir()+"/translations/";
|
||||
#ifdef Q_OS_DARWIN
|
||||
localeDirectory=QApplication::applicationDirPath() + "/../Resources/translations/";
|
||||
#endif
|
||||
}
|
||||
|
||||
// read langs.json
|
||||
QJsonObject rootLangs;
|
||||
QFile file;
|
||||
file.setFileName(localeDirectory+"/langs.json");
|
||||
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
|
||||
const auto localeDirectory = Util::dataDir(QStringLiteral("translations"));
|
||||
auto file = QFile(localeDirectory + QStringLiteral("/langs.json"));
|
||||
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||
ppLogError("Error while opening langs.json");
|
||||
return;
|
||||
}else{
|
||||
|
|
|
@ -836,17 +836,7 @@ void QtWindow::refreshTranslate(){
|
|||
}
|
||||
}
|
||||
|
||||
QString translationsDir = QApplication::applicationDirPath() + "/translations/";
|
||||
|
||||
QFile fileTestLocale(translationsDir);
|
||||
if (!fileTestLocale.exists()){
|
||||
#if defined (Q_OS_LINUX) || defined (Q_OS_UNIX)
|
||||
translationsDir=Util::dataDir()+"/translations/";
|
||||
#endif
|
||||
#ifdef Q_OS_DARWIN
|
||||
translationsDir=QApplication::applicationDirPath() + "/../Resources/translations/";
|
||||
#endif
|
||||
}
|
||||
const auto translationsDir = Util::dataDir(QStringLiteral("translations"));
|
||||
ppLogInfo("Translations loaded from '%s'", qPrintable(translationsDir));
|
||||
|
||||
// set translator for app
|
||||
|
|
|
@ -414,25 +414,14 @@ void CSettings::loadSettings()
|
|||
void CSettings::unzipBoosterMusicBooks()
|
||||
{
|
||||
// Set default value
|
||||
const QString ZIPFILENAME("BoosterMusicBooks.zip");
|
||||
static const auto ZIPFILENAME = QStringLiteral("BoosterMusicBooks.zip");
|
||||
|
||||
if (m_mainWindow && value("PianoBooster/MusicRelease", 0).toInt() < MUSIC_RELEASE)
|
||||
{
|
||||
QString musicSrcDir = QApplication::applicationDirPath() + "/";
|
||||
|
||||
ppLogTrace("unzipBoosterMusicBooks resourceDir1 %s", qPrintable(musicSrcDir));
|
||||
|
||||
if (!QFile::exists(musicSrcDir + ZIPFILENAME))
|
||||
{
|
||||
#if defined (Q_OS_LINUX) || defined (Q_OS_UNIX)
|
||||
musicSrcDir=Util::dataDir()+"/music/";
|
||||
#endif
|
||||
#ifdef Q_OS_DARWIN
|
||||
musicSrcDir = QApplication::applicationDirPath() + "/../Resources/music/";
|
||||
#endif
|
||||
auto musicSrcDir = QCoreApplication::applicationDirPath() + QChar('/');
|
||||
if (!QFile::exists(musicSrcDir + ZIPFILENAME)) {
|
||||
musicSrcDir = Util::dataDir(QStringLiteral("music"));
|
||||
}
|
||||
|
||||
ppLogInfo(qPrintable("applicationDirPath=" + QApplication::applicationDirPath()));
|
||||
ppLogTrace("resourceDir %s", qPrintable(musicSrcDir));
|
||||
|
||||
QFileInfo zipFile(musicSrcDir + ZIPFILENAME);
|
||||
|
|
32
src/Util.cpp
32
src/Util.cpp
|
@ -26,13 +26,15 @@
|
|||
*/
|
||||
/*********************************************************************************/
|
||||
|
||||
#include <qtutilities/misc/compat.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QDir>
|
||||
#include <QElapsedTimer>
|
||||
#include <QFile>
|
||||
#include <QTime>
|
||||
#include <QStringBuilder>
|
||||
|
||||
#include "Util.h"
|
||||
#include "Cfg.h"
|
||||
|
@ -311,17 +313,17 @@ void benchMarkResults()
|
|||
}
|
||||
}
|
||||
|
||||
// Returns the location of where the data is stored
|
||||
// for an AppImage the dataDir is must be relative to the applicationDirPath
|
||||
QString Util::dataDir() {
|
||||
const auto appImagePath = qEnvironmentVariable("APPIMAGE");
|
||||
|
||||
if (appImagePath.isEmpty() )
|
||||
return QString(PREFIX)+"/"+QString(DATA_DIR);
|
||||
QString appImageInternalPath = QApplication::applicationDirPath();
|
||||
int i = appImageInternalPath.lastIndexOf(PREFIX);
|
||||
|
||||
appImageInternalPath.truncate(i);
|
||||
|
||||
return (appImageInternalPath+QString(PREFIX)+"/"+QString(DATA_DIR));
|
||||
// Returns the location where the data is stored
|
||||
QString Util::dataDir(const QString &subDir) {
|
||||
const auto appDirPath = QCoreApplication::applicationDirPath();
|
||||
if (const auto nextToBin = QString(appDirPath % QChar('/') % subDir); QFile::exists(nextToBin)) {
|
||||
return nextToBin;
|
||||
}
|
||||
#ifdef Q_OS_DARWIN
|
||||
return appDirPath % QStringLiteral("/../Resources/") % subDir;
|
||||
#else
|
||||
return appDirPath % (QDir(appDirPath).dirName() == QLatin1String("bin")
|
||||
? QStringLiteral("/../" DATA_DIR "/")
|
||||
: QStringLiteral("/" DATA_DIR "/")) % subDir;
|
||||
#endif
|
||||
}
|
||||
|
|
|
@ -92,7 +92,7 @@ void benchMarkResults();
|
|||
|
||||
class Util {
|
||||
public:
|
||||
static QString dataDir();
|
||||
static QString dataDir(const QString &subDir);
|
||||
};
|
||||
|
||||
#endif //__UTIL_H__
|
||||
|
|
Loading…
Reference in New Issue