Cppcheck
gui/main.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 "cppcheck.h"
20 #include "common.h"
21 #include "mainwindow.h"
22 #include "erroritem.h" // IWYU pragma: keep
23 #include "translationhandler.h"
24 
25 #ifdef _WIN32
26 #include "aboutdialog.h"
27 
28 #include <QMessageBox>
29 #else
30 #include <iostream>
31 #endif
32 #include <algorithm>
33 #include <string>
34 
35 #include <QApplication>
36 #include <QCoreApplication>
37 #include <QIcon>
38 #include <QSettings>
39 #include <QString>
40 #include <QStringList>
41 #include <QVariant>
42 
43 
44 static void ShowUsage();
45 static void ShowVersion();
46 static bool CheckArgs(const QStringList &args);
47 
48 int main(int argc, char *argv[])
49 {
50 
51 #if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)) && (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
52  QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
53  QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
54 #endif
55 
56  QApplication app(argc, argv);
57 
58  QCoreApplication::setOrganizationName("Cppcheck");
59  QCoreApplication::setApplicationName("Cppcheck-GUI");
60 
61  auto* settings = new QSettings("Cppcheck", "Cppcheck-GUI", &app);
62 
63  // Set data dir..
64  const QStringList args = QApplication::arguments();
65  auto it = std::find_if(args.cbegin(), args.cend(), [](const QString& arg) {
66  return arg.startsWith("--data-dir=");
67  });
68  if (it != args.end()) {
69  settings->setValue("DATADIR", it->mid(11));
70  return 0;
71  }
72 
73  auto* th = new TranslationHandler(&app);
74  th->setLanguage(settings->value(SETTINGS_LANGUAGE, th->suggestLanguage()).toString());
75 
76  if (!CheckArgs(QApplication::arguments()))
77  return 0;
78 
79  QApplication::setWindowIcon(QIcon(":cppcheck-gui.png"));
80 
81  // Register this metatype that is used to transfer error info
82  qRegisterMetaType<ErrorItem>("ErrorItem");
83 
84  MainWindow window(th, settings);
85  window.show();
86  return QApplication::exec();
87 }
88 
89 // Check only arguments needing action before GUI is shown.
90 // Rest of the arguments are handled in MainWindow::HandleCLIParams()
91 static bool CheckArgs(const QStringList &args)
92 {
93  if (args.contains("-h") || args.contains("--help")) {
94  ShowUsage();
95  return false;
96  }
97  if (args.contains("-v") || args.contains("--version")) {
98  ShowVersion();
99  return false;
100  }
101  return true;
102 }
103 
104 static void ShowUsage()
105 {
106  QString helpMessage = MainWindow::tr(
107  "Cppcheck GUI.\n\n"
108  "Syntax:\n"
109  " cppcheck-gui [OPTIONS] [files or paths]\n\n"
110  "Options:\n"
111  " -h, --help Print this help\n"
112  " -p <file> Open given project file and start checking it\n"
113  " -l <file> Open given results xml file\n"
114  " -d <directory> Specify the directory that was checked to generate the results xml specified with -l\n"
115  " -v, --version Show program version\n"
116  " --data-dir=<directory> This option is for installation scripts so they can configure the directory where\n"
117  " datafiles are located (translations, cfg). The GUI is not started when this option\n"
118  " is used.");
119 #if defined(_WIN32)
120  QMessageBox msgBox(QMessageBox::Information,
121  MainWindow::tr("Cppcheck GUI - Command line parameters"),
122  helpMessage,
123  QMessageBox::Ok
124  );
125  (void)msgBox.exec();
126 #else
127  std::cout << helpMessage.toStdString() << std::endl;
128 #endif
129 }
130 
131 static void ShowVersion()
132 {
133 #if defined(_WIN32)
135  dlg->exec();
136  delete dlg;
137 #else
138  std::string versionMessage("Cppcheck ");
139  versionMessage += CppCheck::version();
140  const char * extraVersion = CppCheck::extraVersion();
141  if (*extraVersion != 0)
142  versionMessage += std::string(" (") + extraVersion + ")";
143 
144  std::cout << versionMessage << std::endl;
145 #endif
146 }
About dialog.
Definition: aboutdialog.h:38
static const char * version()
Returns current version number as a string.
Definition: cppcheck.cpp:369
static const char * extraVersion()
Returns extra version info as a string.
Definition: cppcheck.cpp:374
Main window for cppcheck-gui.
Definition: mainwindow.h:58
A class handling the available translations.
#define SETTINGS_LANGUAGE
Definition: common.h:78
int main(int argc, char *argv[])
Definition: gui/main.cpp:48
static void ShowUsage()
Definition: gui/main.cpp:104
static bool CheckArgs(const QStringList &args)
Definition: gui/main.cpp:91
static void ShowVersion()
Definition: gui/main.cpp:131