Cppcheck
common.cpp
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2024 Cppcheck team.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 
20 #include "common.h"
21 
22 #include <QCoreApplication>
23 #include <QDir>
24 #include <QFileInfo>
25 #include <QList>
26 #include <QSettings>
27 #include <QStringList>
28 #include <QVariant>
29 #include <Qt>
30 
31 
32 QString getPath(const QString &type)
33 {
34  QSettings settings;
35  QString path = settings.value(type, QString()).toString();
36  if (path.isEmpty()) {
37  // if not set, fallback to last check path hoping that it will be close enough
38  path = settings.value(SETTINGS_LAST_CHECK_PATH, QString()).toString();
39  if (path.isEmpty())
40  // if not set, return user's home directory as the best we can do for now
41  return QDir::homePath();
42  }
43  return path;
44 }
45 
46 void setPath(const QString &type, const QString &value)
47 {
48  QSettings settings;
49  settings.setValue(type, value);
50 }
51 
52 QString toFilterString(const QMap<QString,QString>& filters, bool addAllSupported, bool addAll)
53 {
54  QStringList entries;
55 
56  if (addAllSupported) {
57  entries << QCoreApplication::translate("toFilterString", "All supported files (%1)")
58  .arg(QStringList(filters.values()).join(" "));
59  }
60 
61  if (addAll) {
62  entries << QCoreApplication::translate("toFilterString", "All files (%1)").arg("*.*");
63  }
64 
65  // We're using the description of the filters as the map keys, the file
66  // name patterns are our values. The generated filter string list will
67  // thus be sorted alphabetically over the descriptions.
68  for (const auto& k: filters.keys()) {
69  entries << QString("%1 (%2)").arg(k).arg(filters.value(k));
70  }
71 
72  return entries.join(";;");
73 }
74 
75 QString getDataDir()
76 {
77  QSettings settings;
78  const QString dataDir = settings.value("DATADIR", QString()).toString();
79  if (!dataDir.isEmpty())
80  return dataDir;
81  const QString appPath = QFileInfo(QCoreApplication::applicationFilePath()).canonicalPath();
82  if (QFileInfo::exists(appPath + "/std.cfg"))
83  return appPath;
84  if (appPath.indexOf("/cppcheck/", 0, Qt::CaseInsensitive) > 0)
85  return appPath.left(appPath.indexOf("/cppcheck/", 0, Qt::CaseInsensitive) + 9);
86  return appPath;
87 }
#define SETTINGS_LAST_CHECK_PATH
Definition: common.h:98
QString getPath(const QString &type)
Obtains the path of specified type Returns the path of specified type if not empty.
Definition: common.cpp:32
QString getDataDir()
Get configured data dir.
Definition: common.cpp:75
QString toFilterString(const QMap< QString, QString > &filters, bool addAllSupported, bool addAll)
Creates a string suitable for passing as the filter argument to methods like QFileDialog::getOpenFile...
Definition: common.cpp:52
void setPath(const QString &type, const QString &value)
Stores last used path of specified type Stores provided path as last used path for specified type.
Definition: common.cpp:46