From c2e10f5783678360dca6b3a12d2c261774205983 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Sat, 19 Dec 2020 23:47:01 -0800 Subject: [PATCH] try to fix restart (#1613) * consider AppImage and macOS bundle * a --- src/core/configmgr.cpp | 30 ++++++++++++++++++++++++++---- src/core/configmgr.h | 4 ++++ src/main.cpp | 2 +- src/widgets/toolbarhelper.cpp | 2 ++ 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/core/configmgr.cpp b/src/core/configmgr.cpp index 09a0da15..88758733 100644 --- a/src/core/configmgr.cpp +++ b/src/core/configmgr.cpp @@ -76,11 +76,12 @@ ConfigMgr::~ConfigMgr() void ConfigMgr::locateConfigFolder() { + const auto appDirPath = getApplicationDirPath(); + qInfo() << "app folder" << appDirPath; // Check app config. { const QString configFolderName("vnotex_files"); - QString folderPath(QCoreApplication::applicationDirPath() - + '/' + configFolderName); + QString folderPath(appDirPath + '/' + configFolderName); if (QDir(folderPath).exists()) { // Config folder in app/. m_appConfigFolderPath = PathUtils::cleanPath(folderPath); @@ -92,8 +93,7 @@ void ConfigMgr::locateConfigFolder() // Check user config. { const QString configFolderName("user_files"); - QString folderPath(QCoreApplication::applicationDirPath() - + '/' + configFolderName); + QString folderPath(appDirPath + '/' + configFolderName); if (QDir(folderPath).exists()) { // Config folder in app/. m_userConfigFolderPath = PathUtils::cleanPath(folderPath); @@ -375,3 +375,25 @@ QString ConfigMgr::getLogFile() const { return PathUtils::concatenateFilePath(ConfigMgr::getInst().getUserFolder(), "vnotex.log"); } + +QString ConfigMgr::getApplicationFilePath() +{ +#if defined(Q_OS_Linux) + // TODO: Check if it is from AppImage. + // We could get the APPIMAGE env variable from the AppRun script and pass it to vnote via cmd. +#elif defined(Q_OS_MACOS) + auto exePath = QCoreApplication::applicationFilePath(); + const QString exeName = c_appName.toLower() + ".app"; + int idx = exePath.indexOf(exeName + QStringLiteral("/Contents/MacOS/")); + if (idx != -1) { + return exePath.left(idx + exeName.size()); + } +#endif + + return QCoreApplication::applicationFilePath(); +} + +QString ConfigMgr::getApplicationDirPath() +{ + return PathUtils::parentDirPath(getApplicationFilePath()); +} diff --git a/src/core/configmgr.h b/src/core/configmgr.h index dbbd450e..fe6ae975 100644 --- a/src/core/configmgr.h +++ b/src/core/configmgr.h @@ -94,6 +94,10 @@ namespace vnotex // Called at boostrap without QApplication instance. static QString locateSessionConfigFilePathAtBootstrap(); + static QString getApplicationFilePath(); + + static QString getApplicationDirPath(); + static const QString c_orgName; static const QString c_appName; diff --git a/src/main.cpp b/src/main.cpp index a4c5e13e..9a66aebb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -131,7 +131,7 @@ int main(int argc, char *argv[]) if (ret == RESTART_EXIT_CODE) { // Asked to restart VNote. guard.exit(); - QProcess::startDetached(qApp->applicationFilePath(), QStringList()); + QProcess::startDetached(ConfigMgr::getApplicationFilePath(), QStringList()); return 0; } diff --git a/src/widgets/toolbarhelper.cpp b/src/widgets/toolbarhelper.cpp index 6731dadc..f7dc3c9a 100644 --- a/src/widgets/toolbarhelper.cpp +++ b/src/widgets/toolbarhelper.cpp @@ -313,11 +313,13 @@ QToolBar *ToolBarHelper::setupSettingsToolBar(MainWindow *p_win, QToolBar *p_too [p_win]() { p_win->quitApp(); }); +#if !defined(Q_OS_LINUX) menu->addAction(MainWindow::tr("Restart"), menu, [p_win]() { p_win->restart(); }); +#endif } // Help.