From 51a6f154e0bcb1775e564bba25a5036123c97bab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 29 Aug 2020 21:26:49 +0200 Subject: [PATCH] GUI: If DATADIR is not configured try to guess it. Try to use application path. Or look if there is a cppcheck path. --- gui/common.cpp | 14 ++++++++++++++ gui/common.h | 5 +++++ gui/helpdialog.cpp | 5 ++--- gui/librarydialog.cpp | 5 ++--- gui/mainwindow.cpp | 13 +++---------- gui/projectfiledialog.cpp | 7 +++---- gui/translationhandler.cpp | 7 ++----- 7 files changed, 31 insertions(+), 25 deletions(-) diff --git a/gui/common.cpp b/gui/common.cpp index 612c38a8c..a35c66304 100644 --- a/gui/common.cpp +++ b/gui/common.cpp @@ -66,3 +66,17 @@ QString toFilterString(const QMap& filters, bool addAllSupporte return entries.join(";;"); } + +QString getDataDir() +{ + QSettings settings; + const QString dataDir = settings.value("DATADIR", QString()).toString(); + if (!dataDir.isEmpty()) + return dataDir; + const QString appPath = QFileInfo(QCoreApplication::applicationFilePath()).canonicalPath(); + if (QFileInfo(appPath + "/std.cfg").exists()) + return appPath; + if (appPath.indexOf("/cppcheck/", 0, Qt::CaseInsensitive) > 0) + return appPath.left(appPath.indexOf("/cppcheck/", 0, Qt::CaseInsensitive) + 9); + return appPath; +} diff --git a/gui/common.h b/gui/common.h index 9a566fbc7..3bbfc0832 100644 --- a/gui/common.h +++ b/gui/common.h @@ -147,5 +147,10 @@ void setPath(const QString &type, const QString &value); */ QString toFilterString(const QMap& filters, bool addAllSupported=true, bool addAll=true); +/** + * Get configured data dir. If not configured then it will try to determine that from exe path. + */ +QString getDataDir(); + /// @} #endif diff --git a/gui/helpdialog.cpp b/gui/helpdialog.cpp index dfa7cbde6..706266968 100644 --- a/gui/helpdialog.cpp +++ b/gui/helpdialog.cpp @@ -1,12 +1,12 @@ #include "helpdialog.h" #include "ui_helpdialog.h" +#include "common.h" #include #include #include #include #include -#include void HelpBrowser::setHelpEngine(QHelpEngine *helpEngine) { @@ -26,8 +26,7 @@ QVariant HelpBrowser::loadResource(int type, const QUrl &name) static QString getHelpFile() { - QSettings settings; - const QString datadir = settings.value("DATADIR", QString()).toString(); + const QString datadir = getDataDir(); QStringList paths; paths << (datadir + "/help") diff --git a/gui/librarydialog.cpp b/gui/librarydialog.cpp index 0fa492601..1501cdd05 100644 --- a/gui/librarydialog.cpp +++ b/gui/librarydialog.cpp @@ -20,10 +20,10 @@ #include "ui_librarydialog.h" #include "libraryaddfunctiondialog.h" #include "libraryeditargdialog.h" +#include "common.h" #include "path.h" #include -#include #include #include #include @@ -75,8 +75,7 @@ CppcheckLibraryData::Function *LibraryDialog::currentFunction() void LibraryDialog::openCfg() { - const QSettings settings; - const QString datadir = settings.value("DATADIR",QString()).toString(); + const QString datadir = getDataDir(); QString selectedFilter; const QString filter(tr("Library files (*.cfg)")); diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index d7252bfb0..5608e82a7 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -54,13 +54,6 @@ static const QString OnlineHelpURL("http://cppcheck.net/manual.html"); static const QString compile_commands_json("compile_commands.json"); -static QString getDataDir(const QSettings *settings) -{ - const QString dataDir = settings->value("DATADIR", QString()).toString(); - const QString appPath = QFileInfo(QCoreApplication::applicationFilePath()).canonicalPath(); - return dataDir.isEmpty() ? appPath : dataDir; -} - MainWindow::MainWindow(TranslationHandler* th, QSettings* settings) : mSettings(settings), mApplications(new ApplicationList(this)), @@ -76,7 +69,7 @@ MainWindow::MainWindow(TranslationHandler* th, QSettings* settings) : { mUI.setupUi(this); mThread = new ThreadHandler(this); - mThread->setDataDir(getDataDir(settings)); + mThread->setDataDir(getDataDir()); mUI.mResults->initialize(mSettings, mApplications, mThread); // Filter timer to delay filtering results slightly while typing @@ -786,7 +779,7 @@ Library::Error MainWindow::loadLibrary(Library *library, const QString &filename #endif // Try to load the library from the cfg subfolder.. - const QString datadir = mSettings->value("DATADIR", QString()).toString(); + const QString datadir = getDataDir(); if (!datadir.isEmpty()) { ret = library->load(nullptr, (datadir+"/"+filename).toLatin1()); if (ret.errorcode != Library::ErrorCode::FILE_NOT_FOUND) @@ -943,7 +936,7 @@ Settings MainWindow::getCppcheckSettings() foreach (QString s, mProjectFile->getCheckUnknownFunctionReturn()) result.checkUnknownFunctionReturn.insert(s.toStdString()); - QString filesDir(getDataDir(mSettings)); + QString filesDir(getDataDir()); const QString pythonCmd = mSettings->value(SETTINGS_PYTHON_PATH).toString(); foreach (QString addon, mProjectFile->getAddons()) { QString addonFilePath = ProjectFile::getAddonFilePath(filesDir, addon); diff --git a/gui/projectfiledialog.cpp b/gui/projectfiledialog.cpp index c084ac658..6c43ecc2a 100644 --- a/gui/projectfiledialog.cpp +++ b/gui/projectfiledialog.cpp @@ -90,8 +90,7 @@ ProjectFileDialog::ProjectFileDialog(ProjectFile *projectFile, QWidget *parent) // Checkboxes for the libraries.. const QString applicationFilePath = QCoreApplication::applicationFilePath(); const QString appPath = QFileInfo(applicationFilePath).canonicalPath(); - QSettings settings; - const QString datadir = settings.value("DATADIR",QString()).toString(); + const QString datadir = getDataDir(); QStringList searchPaths; searchPaths << appPath << appPath + "/cfg" << inf.canonicalPath(); #ifdef FILESDIR @@ -332,7 +331,7 @@ void ProjectFileDialog::loadFromProjectFile(const ProjectFile *projectFile) // Addons.. QSettings settings; - const QString dataDir = settings.value("DATADIR", QString()).toString(); + const QString dataDir = getDataDir(); updateAddonCheckBox(mUI.mAddonThreadSafety, projectFile, dataDir, "threadsafety"); updateAddonCheckBox(mUI.mAddonY2038, projectFile, dataDir, "y2038"); updateAddonCheckBox(mUI.mAddonCert, projectFile, dataDir, "cert"); @@ -837,6 +836,6 @@ void ProjectFileDialog::browseMisraFile() mUI.mAddonMisra->setText("MISRA C 2012"); mUI.mAddonMisra->setEnabled(true); - updateAddonCheckBox(mUI.mAddonMisra, nullptr, settings.value("DATADIR", QString()).toString(), "misra"); + updateAddonCheckBox(mUI.mAddonMisra, nullptr, getDataDir(), "misra"); } } diff --git a/gui/translationhandler.cpp b/gui/translationhandler.cpp index 0cb80a0f2..dded1362f 100644 --- a/gui/translationhandler.cpp +++ b/gui/translationhandler.cpp @@ -21,9 +21,9 @@ #include #include #include -#include #include #include "translationhandler.h" +#include "common.h" // Provide own translations for standard buttons. This (garbage) code is needed to enforce them to appear in .ts files even after "lupdate gui.pro" @@ -111,10 +111,7 @@ bool TranslationHandler::setLanguage(const QString &code) //Load the new language const QString appPath = QFileInfo(QCoreApplication::applicationFilePath()).canonicalPath(); - QSettings settings; - QString datadir = settings.value("DATADIR").toString(); - if (datadir.isEmpty()) - datadir = appPath; + QString datadir = getDataDir(); QString translationFile; if (QFile::exists(datadir + "/lang/" + mTranslations[index].mFilename + ".qm"))