WebUI: Use Map instead of Mootools Hash in Torrents table
[qBittorrent.git] / src / app / main.cpp
blob53455f99bea9b0677bda0053b3d1ca487a2d1abf
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2014-2023 Vladimir Golovnev <glassez@yandex.ru>
4 * Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * In addition, as a special exception, the copyright holders give permission to
21 * link this program with the OpenSSL project's "OpenSSL" library (or with
22 * modified versions of it that use the same license as the "OpenSSL" library),
23 * and distribute the linked executables. You must obey the GNU General Public
24 * License in all respects for all of the code used other than "OpenSSL". If you
25 * modify file(s), you may extend this exception to your version of the file(s),
26 * but you are not obligated to do so. If you do not wish to do so, delete this
27 * exception statement from your version.
30 #include <QtSystemDetection>
32 #include <chrono>
33 #include <cstdlib>
34 #include <memory>
36 #ifdef Q_OS_UNIX
37 #include <sys/resource.h>
38 #endif
40 #ifndef Q_OS_WIN
41 #ifndef Q_OS_HAIKU
42 #include <unistd.h>
43 #endif // Q_OS_HAIKU
44 #elif defined DISABLE_GUI
45 #include <io.h>
46 #endif
48 #include <QCoreApplication>
49 #include <QString>
50 #include <QThread>
52 #ifndef DISABLE_GUI
53 // GUI-only includes
54 #include <QFont>
55 #include <QMessageBox>
56 #include <QPainter>
57 #include <QPen>
58 #include <QSplashScreen>
59 #include <QTimer>
61 #ifdef Q_OS_WIN
62 #include <QOperatingSystemVersion>
63 #endif
65 #ifdef QBT_STATIC_QT
66 #include <QtPlugin>
67 Q_IMPORT_PLUGIN(QICOPlugin)
68 #endif // QBT_STATIC_QT
70 #else // DISABLE_GUI
71 #include <cstdio>
72 #endif // DISABLE_GUI
74 #include "base/global.h"
75 #include "base/logger.h"
76 #include "base/preferences.h"
77 #include "base/profile.h"
78 #include "base/settingvalue.h"
79 #include "base/version.h"
80 #include "application.h"
81 #include "cmdoptions.h"
82 #include "legalnotice.h"
83 #include "signalhandler.h"
85 #ifndef DISABLE_GUI
86 #include "gui/utils.h"
87 #endif
89 using namespace std::chrono_literals;
91 namespace
93 void displayBadArgMessage(const QString &message)
95 const QString help = QCoreApplication::translate("Main", "Run application with -h option to read about command line parameters.");
96 #if defined(Q_OS_WIN) && !defined(DISABLE_GUI)
97 QMessageBox msgBox(QMessageBox::Critical, QCoreApplication::translate("Main", "Bad command line"),
98 (message + u'\n' + help), QMessageBox::Ok);
99 msgBox.show(); // Need to be shown or to moveToCenter does not work
100 msgBox.move(Utils::Gui::screenCenter(&msgBox));
101 msgBox.exec();
102 #else
103 const QString errMsg = QCoreApplication::translate("Main", "Bad command line: ") + u'\n'
104 + message + u'\n'
105 + help + u'\n';
106 fprintf(stderr, "%s", qUtf8Printable(errMsg));
107 #endif
110 void displayErrorMessage(const QString &message)
112 #ifndef DISABLE_GUI
113 if (QApplication::instance())
115 QMessageBox msgBox;
116 msgBox.setIcon(QMessageBox::Critical);
117 msgBox.setText(QCoreApplication::translate("Main", "An unrecoverable error occurred."));
118 msgBox.setInformativeText(message);
119 msgBox.show(); // Need to be shown or to moveToCenter does not work
120 msgBox.move(Utils::Gui::screenCenter(&msgBox));
121 msgBox.exec();
123 else
125 const QString errMsg = QCoreApplication::translate("Main", "qBittorrent has encountered an unrecoverable error.") + u'\n' + message + u'\n';
126 fprintf(stderr, "%s", qUtf8Printable(errMsg));
128 #else
129 const QString errMsg = QCoreApplication::translate("Main", "qBittorrent has encountered an unrecoverable error.") + u'\n' + message + u'\n';
130 fprintf(stderr, "%s", qUtf8Printable(errMsg));
131 #endif
134 void displayVersion()
136 printf("%s %s\n", qUtf8Printable(qApp->applicationName()), QBT_VERSION);
139 #ifndef DISABLE_GUI
140 void showSplashScreen()
142 QPixmap splashImg(u":/icons/splash.png"_s);
143 QPainter painter(&splashImg);
144 const auto version = QStringLiteral(QBT_VERSION);
145 painter.setPen(QPen(Qt::white));
146 painter.setFont(QFont(u"Arial"_s, 22, QFont::Black));
147 painter.drawText(224 - painter.fontMetrics().horizontalAdvance(version), 270, version);
148 QSplashScreen *splash = new QSplashScreen(splashImg);
149 splash->show();
150 QTimer::singleShot(1500ms, Qt::CoarseTimer, splash, &QObject::deleteLater);
151 qApp->processEvents();
153 #endif // DISABLE_GUI
155 #ifdef Q_OS_UNIX
156 void adjustFileDescriptorLimit()
158 rlimit limit {};
160 if (getrlimit(RLIMIT_NOFILE, &limit) != 0)
161 return;
163 limit.rlim_cur = limit.rlim_max;
164 setrlimit(RLIMIT_NOFILE, &limit);
167 void adjustLocale()
169 // specify the default locale just in case if user has not set any other locale
170 // only `C` locale is available universally without installing locale packages
171 if (qEnvironmentVariableIsEmpty("LANG"))
172 qputenv("LANG", "C.UTF-8");
174 #endif
177 // Main
178 int main(int argc, char *argv[])
180 #ifdef DISABLE_GUI
181 setvbuf(stdout, nullptr, _IONBF, 0);
182 #endif
184 #ifdef Q_OS_UNIX
185 adjustLocale();
186 adjustFileDescriptorLimit();
187 #endif
189 // We must save it here because QApplication constructor may change it
190 const bool isOneArg = (argc == 2);
192 #if !defined(DISABLE_GUI) && defined(Q_OS_WIN)
193 if (QOperatingSystemVersion::current() >= QOperatingSystemVersion::Windows10)
194 QApplication::setStyle(u"Fusion"_s);
195 #endif
197 // `app` must be declared out of try block to allow display message box in case of exception
198 std::unique_ptr<Application> app;
201 // Create Application
202 app = std::make_unique<Application>(argc, argv);
204 #ifdef Q_OS_WIN
205 // QCoreApplication::applicationDirPath() needs an Application object instantiated first
206 // Let's hope that there won't be a crash before this line
207 const char envName[] = "_NT_SYMBOL_PATH";
208 const QString envValue = qEnvironmentVariable(envName);
209 if (envValue.isEmpty())
210 qputenv(envName, Application::applicationDirPath().toLocal8Bit());
211 else
212 qputenv(envName, u"%1;%2"_s.arg(envValue, Application::applicationDirPath()).toLocal8Bit());
213 #endif
215 const QBtCommandLineParameters params = app->commandLineArgs();
216 if (!params.unknownParameter.isEmpty())
218 throw CommandLineParameterError(QCoreApplication::translate("Main", "%1 is an unknown command line parameter.",
219 "--random-parameter is an unknown command line parameter.")
220 .arg(params.unknownParameter));
222 #if !defined(Q_OS_WIN) || defined(DISABLE_GUI)
223 if (params.showVersion)
225 if (isOneArg)
227 displayVersion();
228 return EXIT_SUCCESS;
230 throw CommandLineParameterError(QCoreApplication::translate("Main", "%1 must be the single command line parameter.")
231 .arg(u"-v (or --version)"_s));
233 #endif
234 if (params.showHelp)
236 if (isOneArg)
238 displayUsage(QString::fromLocal8Bit(argv[0]));
239 return EXIT_SUCCESS;
241 throw CommandLineParameterError(QCoreApplication::translate("Main", "%1 must be the single command line parameter.")
242 .arg(u"-h (or --help)"_s));
245 // Check if qBittorrent is already running
246 if (app->hasAnotherInstance())
248 #if defined(DISABLE_GUI) && !defined(Q_OS_WIN)
249 if (params.shouldDaemonize)
251 throw CommandLineParameterError(QCoreApplication::translate("Main", "You cannot use %1: qBittorrent is already running.")
252 .arg(u"-d (or --daemon)"_s));
255 // print friendly message if there are no other command line args
256 if (argc == 1)
258 const QString message = QCoreApplication::translate("Main", "Another qBittorrent instance is already running.");
259 printf("%s\n", qUtf8Printable(message));
261 #endif
263 QThread::msleep(300);
264 app->callMainInstance();
266 return EXIT_SUCCESS;
269 CachedSettingValue<bool> legalNoticeShown {u"LegalNotice/Accepted"_s, false};
270 if (params.confirmLegalNotice)
271 legalNoticeShown = true;
273 if (!legalNoticeShown)
275 #ifndef DISABLE_GUI
276 const bool isInteractive = true;
277 #elif defined(Q_OS_WIN)
278 const bool isInteractive = (_isatty(_fileno(stdin)) != 0) && (_isatty(_fileno(stdout)) != 0);
279 #else
280 // when run in daemon mode user can only dismiss the notice with command line option
281 const bool isInteractive = !params.shouldDaemonize
282 && ((isatty(fileno(stdin)) != 0) && (isatty(fileno(stdout)) != 0));
283 #endif
284 showLegalNotice(isInteractive);
285 if (isInteractive)
286 legalNoticeShown = true;
289 #ifdef Q_OS_MACOS
290 // Since Apple made difficult for users to set PATH, we set here for convenience.
291 // Users are supposed to install Homebrew Python for search function.
292 // For more info see issue #5571.
293 const QByteArray path = "/usr/local/bin:" + qgetenv("PATH");
294 qputenv("PATH", path.constData());
296 // On OS X the standard is to not show icons in the menus
297 app->setAttribute(Qt::AA_DontShowIconsInMenus);
298 #else
299 if (!Preferences::instance()->iconsInMenusEnabled())
300 app->setAttribute(Qt::AA_DontShowIconsInMenus);
301 #endif
303 #if defined(DISABLE_GUI) && !defined(Q_OS_WIN)
304 if (params.shouldDaemonize)
306 app.reset(); // Destroy current application instance
307 if (::daemon(1, 0) == 0)
309 app = std::make_unique<Application>(argc, argv);
310 if (app->hasAnotherInstance())
312 // It is undefined behavior to write to log file since there is another qbt instance
313 // in play. But we still do it since there is chance that the log message will survive.
314 const QString errorMessage = QCoreApplication::translate("Main", "Found unexpected qBittorrent instance. Exiting this instance. Current process ID: %1.")
315 .arg(QString::number(QCoreApplication::applicationPid()));
316 LogMsg(errorMessage, Log::CRITICAL);
317 // stdout, stderr is closed so we can't use them
318 return EXIT_FAILURE;
321 else
323 const QString errorMessage = QCoreApplication::translate("Main", "Error when daemonizing. Reason: \"%1\". Error code: %2.")
324 .arg(QString::fromLocal8Bit(strerror(errno)), QString::number(errno));
325 LogMsg(errorMessage, Log::CRITICAL);
326 qCritical("%s", qUtf8Printable(errorMessage));
327 return EXIT_FAILURE;
330 #elif !defined(DISABLE_GUI)
331 if (!(params.noSplash || Preferences::instance()->isSplashScreenDisabled()))
332 showSplashScreen();
333 #endif
335 registerSignalHandlers();
337 return app->exec();
339 catch (const CommandLineParameterError &er)
341 displayBadArgMessage(er.message());
342 return EXIT_FAILURE;
344 catch (const RuntimeError &er)
346 displayErrorMessage(er.message());
347 return EXIT_FAILURE;