Cppcheck
applicationlist.cpp
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2023 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 #include "applicationlist.h"
20 
21 #include "application.h"
22 #include "common.h"
23 
24 #include <QFileInfo>
25 #include <QSettings>
26 #include <QStringList>
27 #include <QVariant>
28 
30  QObject(parent)
31 {
32  //ctor
33 }
34 
36 {
37  clear();
38 }
39 
41 {
42  QSettings settings;
43  QStringList names = settings.value(SETTINGS_APPLICATION_NAMES, QStringList()).toStringList();
44  QStringList paths = settings.value(SETTINGS_APPLICATION_PATHS, QStringList()).toStringList();
45  QStringList params = settings.value(SETTINGS_APPLICATION_PARAMS, QStringList()).toStringList();
46  int defapp = settings.value(SETTINGS_APPLICATION_DEFAULT, -1).toInt();
47 
48  // Params will be empty first time starting with the new setting.
49  // Return false and inform user about problem with application settings.
50  bool succeeded = true;
51  if (!names.empty() && !paths.empty() && params.empty()) {
52  for (int i = 0; i < paths.length(); i++)
53  params << QString();
54  succeeded = false;
55  }
56 
57  if (names.empty() && paths.empty() && params.empty()) {
58 #ifndef _WIN32
59  // use as default for gnome environments
60  if (QFileInfo("/usr/bin/gedit").isExecutable()) {
61  Application app;
62  app.setName("gedit");
63  app.setPath("/usr/bin/gedit");
64  app.setParameters("+(line) (file)");
65  addApplication(app);
66  defapp = 0;
67  }
68  checkAndAddApplication("/usr/bin/geany","geany","+(line) (file)");
69  checkAndAddApplication("/usr/bin/qtcreator","Qt Creator","-client (file):(line)");
70  // use as default for kde environments
71  if (QFileInfo("/usr/bin/kate").isExecutable()) {
72  Application app;
73  app.setName("kate");
74  app.setPath("/usr/bin/kate");
75  app.setParameters("-l(line) (file)");
76  addApplication(app);
77  defapp = 0;
78  }
79 #else
80  if (findDefaultWindowsEditor()) {
81  defapp = 0;
82  }
83 #endif
84  } else if (names.size() == paths.size()) {
85  for (int i = 0; i < names.size(); i++) {
86  const Application app(names[i], paths[i], params[i]);
87  addApplication(app);
88  }
89  }
90 
91  if (defapp == -1)
93  else if (defapp < names.size())
94  mDefaultApplicationIndex = defapp;
95  else
97 
98  return succeeded;
99 }
100 
102 {
103  QSettings settings;
104  QStringList names;
105  QStringList paths;
106  QStringList params;
107 
108  for (int i = 0; i < getApplicationCount(); i++) {
109  const Application& app = getApplication(i);
110  names << app.getName();
111  paths << app.getPath();
112  params << app.getParameters();
113  }
114 
115  settings.setValue(SETTINGS_APPLICATION_NAMES, names);
116  settings.setValue(SETTINGS_APPLICATION_PATHS, paths);
117  settings.setValue(SETTINGS_APPLICATION_PARAMS, params);
119 }
120 
122 {
123  return mApplications.size();
124 }
125 
127 {
128  if (index >= 0 && index < mApplications.size()) {
129  return mApplications[index];
130  }
131 
132  static Application dummy; // TODO: Throw exception instead?
133  return dummy;
134 }
135 
136 const Application& ApplicationList::getApplication(const int index) const
137 {
138  if (index >= 0 && index < mApplications.size()) {
139  return mApplications[index];
140  }
141 
142  static const Application dummy; // TODO: Throw exception instead?
143  return dummy;
144 }
145 
147 {
148  if (app.getName().isEmpty() || app.getPath().isEmpty()) {
149  return;
150  }
151  mApplications << app;
152 }
153 
155 {
156  mApplications.removeAt(index);
157 }
158 
159 void ApplicationList::setDefault(const int index)
160 {
161  if (index < mApplications.size() && index >= 0) {
162  mDefaultApplicationIndex = index;
163  }
164 }
165 
167 {
168  if (!list) {
169  return;
170  }
171 
172  clear();
173  for (int i = 0; i < list->getApplicationCount(); i++) {
174  const Application& app = list->getApplication(i);
175  addApplication(app);
176  }
178 }
179 
181 {
182  mApplications.clear();
184 }
185 
186 bool ApplicationList::checkAndAddApplication(const QString& appPath, const QString& name, const QString& parameters)
187 {
188  if (QFileInfo::exists(appPath) && QFileInfo(appPath).isExecutable()) {
189  Application app;
190  app.setName(name);
191  app.setPath("\"" + appPath + "\"");
192  app.setParameters(parameters);
193  addApplication(app);
194  return true;
195  }
196  return false;
197 }
198 
199 #ifdef _WIN32
200 bool ApplicationList::findDefaultWindowsEditor()
201 {
202  bool foundOne = false;
203 #ifdef WIN64 // As long as we do support 32-bit XP, we cannot be sure that the environment variable "ProgramFiles(x86)" exists
204  const QString appPathx86(getenv("ProgramFiles(x86)"));
205 #else
206  const QString appPathx86(getenv("ProgramFiles"));
207 #endif
208  const QString appPathx64(getenv("ProgramW6432"));
209  const QString windowsPath(getenv("windir"));
210 
211  if (checkAndAddApplication(appPathx86 + "\\Notepad++\\notepad++.exe", "Notepad++", "-n(line) (file)"))
212  foundOne = true;
213  else if (checkAndAddApplication(appPathx64 + "\\Notepad++\\notepad++.exe", "Notepad++", "-n(line) (file)"))
214  foundOne = true;
215 
216  if (checkAndAddApplication(appPathx86 + "\\Notepad2\\Notepad2.exe", "Notepad2", "/g (line) (file)"))
217  foundOne = true;
218  else if (checkAndAddApplication(appPathx64 + "\\Notepad2\\Notepad2.exe", "Notepad2", "/g (line) (file)"))
219  foundOne = true;
220 
221  if (checkAndAddApplication(windowsPath + "\\system32\\notepad.exe", "Notepad", "(file)"))
222  foundOne = true;
223 
224  QString regPath = "HKEY_CLASSES_ROOT\\Applications\\QtProject.QtCreator.pro\\shell\\Open\\command";
225  QSettings registry(regPath, QSettings::NativeFormat);
226  QString qtCreatorRegistry = registry.value("Default", QString()).toString();
227  QString qtCreatorPath = qtCreatorRegistry.left(qtCreatorRegistry.indexOf(".exe") + 4);
228  if (!qtCreatorRegistry.isEmpty() && checkAndAddApplication(qtCreatorPath, "Qt Creator", "-client (file):(line)")) {
229  foundOne = true;
230  }
231 
232  const QString regPathUEdit32 = "HKEY_CLASSES_ROOT\\Applications\\Uedit32.exe\\shell\\open\\Command";
233  const QSettings registryUEdit32(regPathUEdit32, QSettings::NativeFormat);
234  const QString uedit32Registry = registryUEdit32.value("Default", QString()).toString();
235  if (!uedit32Registry.isEmpty()) {
236  // Extract path to executable and make sure there is no single quotation mark at the beginning
237  const QString uedit32Path = uedit32Registry.left(uedit32Registry.indexOf(".exe") + 4).replace("\"", "");
238  if (checkAndAddApplication(uedit32Path, "UltraEdit 32", "(file)/(line)")) {
239  foundOne = true;
240  }
241  }
242 
243  const QString regPathUEdit64 = "HKEY_CLASSES_ROOT\\Applications\\uedit64.exe\\shell\\open\\Command";
244  const QSettings registryUEdit64(regPathUEdit64, QSettings::NativeFormat);
245  const QString uedit64Registry = registryUEdit64.value("Default", QString()).toString();
246  if (!uedit64Registry.isEmpty()) {
247  // Extract path to executable and make sure there is no single quotation mark at the beginning
248  const QString uedit64Path = uedit64Registry.left(uedit64Registry.indexOf(".exe") + 4).replace("\"", "");
249  if (checkAndAddApplication(uedit64Path, "UltraEdit 64", "(file)/(line)")) {
250  foundOne = true;
251  }
252  }
253 
254  const QString regPathMSVSCode = "HKEY_CLASSES_ROOT\\Applications\\Code.exe\\shell\\open\\command";
255  const QSettings registryMSVSCode(regPathMSVSCode, QSettings::NativeFormat);
256  const QString msvscodeRegistry = registryMSVSCode.value("Default", QString()).toString();
257  if (!msvscodeRegistry.isEmpty()) {
258  const QString msvscodePath = msvscodeRegistry.left(msvscodeRegistry.indexOf(".exe") + 4).replace("\"", "");
259  if (checkAndAddApplication(msvscodePath, "Microsoft VS Code", "-g (file):(line)")) {
260  foundOne = true;
261  }
262  }
263 
264  return foundOne;
265 }
266 #endif
List of applications user has specified to open errors with.
int mDefaultApplicationIndex
Index of the default application.
QList< Application > mApplications
List of applications.
bool loadSettings()
Load all applications.
void copy(const ApplicationList *list)
Remove all applications from this list and copy all applications from list given as a parameter.
void setDefault(const int index)
Set application as default application.
void clear()
Clear the list.
void saveSettings() const
Save all applications.
ApplicationList(QObject *parent=nullptr)
int getApplicationCount() const
Get the amount of applications in the list.
const Application & getApplication(const int index) const
Get specific application's name.
void removeApplication(const int index)
Remove an application from the list.
~ApplicationList() override
void addApplication(const Application &app)
Add a new application.
int getDefaultApplication() const
Return the default application.
bool checkAndAddApplication(const QString &appPath, const QString &name, const QString &parameters)
A class containing information of the application to execute.
Definition: application.h:43
const QString & getParameters() const
Get application command line parameters.
Definition: application.h:68
void setPath(const QString &path)
Set application path.
Definition: application.h:84
void setName(const QString &name)
Set application name.
Definition: application.h:76
void setParameters(const QString &parameters)
Set application command line parameters.
Definition: application.h:92
const QString & getPath() const
Get application path.
Definition: application.h:60
const QString & getName() const
Get application name.
Definition: application.h:52
#define SETTINGS_APPLICATION_DEFAULT
Definition: common.h:77
#define SETTINGS_APPLICATION_PATHS
Definition: common.h:75
#define SETTINGS_APPLICATION_NAMES
Definition: common.h:74
#define SETTINGS_APPLICATION_PARAMS
Definition: common.h:76