WebUI: ensure cached info are initialized properly
[qBittorrent.git] / src / app / main.cpp
blobd80629f44aba6f73e6428bb2517140b6728ab8d8
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2014-2024 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 QBT_STATIC_QT
62 #include <QtPlugin>
63 Q_IMPORT_PLUGIN(QICOPlugin)
64 #endif // QBT_STATIC_QT
66 #else // DISABLE_GUI
67 #include <cstdio>
68 #endif // DISABLE_GUI
70 #include "base/global.h"
71 #include "base/logger.h"
72 #include "base/preferences.h"
73 #include "base/profile.h"
74 #include "base/settingvalue.h"
75 #include "base/version.h"
76 #include "application.h"
77 #include "cmdoptions.h"
78 #include "legalnotice.h"
79 #include "signalhandler.h"
81 #ifndef DISABLE_GUI
82 #include "gui/utils.h"
83 #endif
85 using namespace std::chrono_literals;
87 namespace
89 void displayBadArgMessage(const QString &message)
91 const QString help = QCoreApplication::translate("Main", "Run application with -h option to read about command line parameters.");
92 #if defined(Q_OS_WIN) && !defined(DISABLE_GUI)
93 QMessageBox msgBox(QMessageBox::Critical, QCoreApplication::translate("Main", "Bad command line"),
94 (message + u'\n' + help), QMessageBox::Ok);
95 msgBox.show(); // Need to be shown or to moveToCenter does not work
96 msgBox.move(Utils::Gui::screenCenter(&msgBox));
97 msgBox.exec();
98 #else
99 const QString errMsg = QCoreApplication::translate("Main", "Bad command line: ") + u'\n'
100 + message + u'\n'
101 + help + u'\n';
102 fprintf(stderr, "%s", qUtf8Printable(errMsg));
103 #endif
106 void displayErrorMessage(const QString &message)
108 #ifndef DISABLE_GUI
109 if (QApplication::instance())
111 QMessageBox msgBox;
112 msgBox.setIcon(QMessageBox::Critical);
113 msgBox.setText(QCoreApplication::translate("Main", "An unrecoverable error occurred."));
114 msgBox.setInformativeText(message);
115 msgBox.show(); // Need to be shown or to moveToCenter does not work
116 msgBox.move(Utils::Gui::screenCenter(&msgBox));
117 msgBox.exec();
119 else
121 const QString errMsg = QCoreApplication::translate("Main", "qBittorrent has encountered an unrecoverable error.") + u'\n' + message + u'\n';
122 fprintf(stderr, "%s", qUtf8Printable(errMsg));
124 #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));
127 #endif
130 void displayVersion()
132 printf("%s %s\n", qUtf8Printable(qApp->applicationName()), QBT_VERSION);
135 #ifndef DISABLE_GUI
136 void showSplashScreen()
138 QPixmap splashImg(u":/icons/splash.png"_s);
139 QPainter painter(&splashImg);
140 const auto version = QStringLiteral(QBT_VERSION);
141 painter.setPen(QPen(Qt::white));
142 painter.setFont(QFont(u"Arial"_s, 22, QFont::Black));
143 painter.drawText(224 - painter.fontMetrics().horizontalAdvance(version), 270, version);
144 QSplashScreen *splash = new QSplashScreen(splashImg);
145 splash->show();
146 QTimer::singleShot(1500ms, Qt::CoarseTimer, splash, &QObject::deleteLater);
147 qApp->processEvents();
149 #endif // DISABLE_GUI
151 #ifdef Q_OS_UNIX
152 void adjustFileDescriptorLimit()
154 rlimit limit {};
156 if (getrlimit(RLIMIT_NOFILE, &limit) != 0)
157 return;
159 limit.rlim_cur = limit.rlim_max;
160 setrlimit(RLIMIT_NOFILE, &limit);
163 void adjustLocale()
165 // specify the default locale just in case if user has not set any other locale
166 // only `C` locale is available universally without installing locale packages
167 if (qEnvironmentVariableIsEmpty("LANG"))
168 qputenv("LANG", "C.UTF-8");
170 #endif
173 // Main
174 int main(int argc, char *argv[])
176 #ifdef DISABLE_GUI
177 setvbuf(stdout, nullptr, _IONBF, 0);
178 #endif
180 #ifdef Q_OS_UNIX
181 adjustLocale();
182 adjustFileDescriptorLimit();
183 #endif
185 // We must save it here because QApplication constructor may change it
186 const bool isOneArg = (argc == 2);
188 // `app` must be declared out of try block to allow display message box in case of exception
189 std::unique_ptr<Application> app;
192 // Create Application
193 app = std::make_unique<Application>(argc, argv);
195 #ifdef Q_OS_WIN
196 // QCoreApplication::applicationDirPath() needs an Application object instantiated first
197 // Let's hope that there won't be a crash before this line
198 const char envName[] = "_NT_SYMBOL_PATH";
199 const QString envValue = qEnvironmentVariable(envName);
200 if (envValue.isEmpty())
201 qputenv(envName, Application::applicationDirPath().toLocal8Bit());
202 else
203 qputenv(envName, u"%1;%2"_s.arg(envValue, Application::applicationDirPath()).toLocal8Bit());
204 #endif
206 const QBtCommandLineParameters params = app->commandLineArgs();
207 if (!params.unknownParameter.isEmpty())
209 throw CommandLineParameterError(QCoreApplication::translate("Main", "%1 is an unknown command line parameter.",
210 "--random-parameter is an unknown command line parameter.")
211 .arg(params.unknownParameter));
213 #if !defined(Q_OS_WIN) || defined(DISABLE_GUI)
214 if (params.showVersion)
216 if (isOneArg)
218 displayVersion();
219 return EXIT_SUCCESS;
221 throw CommandLineParameterError(QCoreApplication::translate("Main", "%1 must be the single command line parameter.")
222 .arg(u"-v (or --version)"_s));
224 #endif
225 if (params.showHelp)
227 if (isOneArg)
229 displayUsage(QString::fromLocal8Bit(argv[0]));
230 return EXIT_SUCCESS;
232 throw CommandLineParameterError(QCoreApplication::translate("Main", "%1 must be the single command line parameter.")
233 .arg(u"-h (or --help)"_s));
236 // Check if qBittorrent is already running
237 if (app->hasAnotherInstance())
239 #if defined(DISABLE_GUI) && !defined(Q_OS_WIN)
240 if (params.shouldDaemonize)
242 throw CommandLineParameterError(QCoreApplication::translate("Main", "You cannot use %1: qBittorrent is already running.")
243 .arg(u"-d (or --daemon)"_s));
246 // print friendly message if there are no other command line args
247 if (argc == 1)
249 const QString message = QCoreApplication::translate("Main", "Another qBittorrent instance is already running.");
250 printf("%s\n", qUtf8Printable(message));
252 #endif
254 QThread::msleep(300);
255 app->callMainInstance();
257 return EXIT_SUCCESS;
260 CachedSettingValue<bool> legalNoticeShown {u"LegalNotice/Accepted"_s, false};
261 if (params.confirmLegalNotice)
262 legalNoticeShown = true;
264 if (!legalNoticeShown)
266 #ifndef DISABLE_GUI
267 const bool isInteractive = true;
268 #elif defined(Q_OS_WIN)
269 const bool isInteractive = (_isatty(_fileno(stdin)) != 0) && (_isatty(_fileno(stdout)) != 0);
270 #else
271 // when run in daemon mode user can only dismiss the notice with command line option
272 const bool isInteractive = !params.shouldDaemonize
273 && ((isatty(fileno(stdin)) != 0) && (isatty(fileno(stdout)) != 0));
274 #endif
275 showLegalNotice(isInteractive);
276 if (isInteractive)
277 legalNoticeShown = true;
280 #ifdef Q_OS_MACOS
281 // Since Apple made difficult for users to set PATH, we set here for convenience.
282 // Users are supposed to install Homebrew Python for search function.
283 // For more info see issue #5571.
284 const QByteArray path = "/usr/local/bin:" + qgetenv("PATH");
285 qputenv("PATH", path.constData());
287 // On OS X the standard is to not show icons in the menus
288 app->setAttribute(Qt::AA_DontShowIconsInMenus);
289 #else
290 if (!Preferences::instance()->iconsInMenusEnabled())
291 app->setAttribute(Qt::AA_DontShowIconsInMenus);
292 #endif
294 #if defined(DISABLE_GUI) && !defined(Q_OS_WIN)
295 if (params.shouldDaemonize)
297 app.reset(); // Destroy current application instance
298 if (::daemon(1, 0) == 0)
300 app = std::make_unique<Application>(argc, argv);
301 if (app->hasAnotherInstance())
303 // It is undefined behavior to write to log file since there is another qbt instance
304 // in play. But we still do it since there is chance that the log message will survive.
305 const QString errorMessage = QCoreApplication::translate("Main", "Found unexpected qBittorrent instance. Exiting this instance. Current process ID: %1.")
306 .arg(QString::number(QCoreApplication::applicationPid()));
307 LogMsg(errorMessage, Log::CRITICAL);
308 // stdout, stderr is closed so we can't use them
309 return EXIT_FAILURE;
312 else
314 const QString errorMessage = QCoreApplication::translate("Main", "Error when daemonizing. Reason: \"%1\". Error code: %2.")
315 .arg(QString::fromLocal8Bit(strerror(errno)), QString::number(errno));
316 LogMsg(errorMessage, Log::CRITICAL);
317 qCritical("%s", qUtf8Printable(errorMessage));
318 return EXIT_FAILURE;
321 #elif !defined(DISABLE_GUI)
322 if (!(params.noSplash || Preferences::instance()->isSplashScreenDisabled()))
323 showSplashScreen();
324 #endif
326 registerSignalHandlers();
328 return app->exec();
330 catch (const CommandLineParameterError &er)
332 displayBadArgMessage(er.message());
333 return EXIT_FAILURE;
335 catch (const RuntimeError &er)
337 displayErrorMessage(er.message());
338 return EXIT_FAILURE;