Sync translations from Transifex and run lupdate
[qBittorrent.git] / src / app / main.cpp
blobeab0156ee3af3793c5453f0d83bcd33c3f107c52
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2014 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 <QtGlobal>
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 <QPushButton>
59 #include <QSplashScreen>
60 #include <QTimer>
62 #ifdef QBT_STATIC_QT
63 #include <QtPlugin>
64 Q_IMPORT_PLUGIN(QICOPlugin)
65 #endif // QBT_STATIC_QT
67 #else // DISABLE_GUI
68 #include <cstdio>
69 #endif // DISABLE_GUI
71 #include "base/global.h"
72 #include "base/preferences.h"
73 #include "base/profile.h"
74 #include "base/version.h"
75 #include "application.h"
76 #include "cmdoptions.h"
77 #include "signalhandler.h"
78 #include "upgrade.h"
80 #ifndef DISABLE_GUI
81 #include "gui/utils.h"
82 #endif
84 using namespace std::chrono_literals;
86 void displayVersion();
87 bool userAgreesWithLegalNotice();
88 void displayBadArgMessage(const QString &message);
89 void displayErrorMessage(const QString &message);
91 #ifndef DISABLE_GUI
92 void showSplashScreen();
93 #endif // DISABLE_GUI
95 #ifdef Q_OS_UNIX
96 void adjustFileDescriptorLimit();
97 void adjustLocale();
98 #endif
100 // Main
101 int main(int argc, char *argv[])
103 #ifdef DISABLE_GUI
104 setvbuf(stdout, nullptr, _IONBF, 0);
105 #endif
107 #ifdef Q_OS_UNIX
108 adjustLocale();
109 adjustFileDescriptorLimit();
110 #endif
112 // We must save it here because QApplication constructor may change it
113 bool isOneArg = (argc == 2);
115 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) && !defined(DISABLE_GUI)
116 // Attribute Qt::AA_EnableHighDpiScaling must be set before QCoreApplication is created
117 if (qgetenv("QT_ENABLE_HIGHDPI_SCALING").isEmpty() && qgetenv("QT_AUTO_SCREEN_SCALE_FACTOR").isEmpty())
118 Application::setAttribute(Qt::AA_EnableHighDpiScaling, true);
119 // HighDPI scale factor policy must be set before QGuiApplication is created
120 if (qgetenv("QT_SCALE_FACTOR_ROUNDING_POLICY").isEmpty())
121 Application::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
122 #endif
124 // `app` must be declared out of try block to allow display message box in case of exception
125 std::unique_ptr<Application> app;
128 // Create Application
129 app = std::make_unique<Application>(argc, argv);
131 #ifdef Q_OS_WIN
132 // QCoreApplication::applicationDirPath() needs an Application object instantiated first
133 // Let's hope that there won't be a crash before this line
134 const char *envName = "_NT_SYMBOL_PATH";
135 const QString envValue = qEnvironmentVariable(envName);
136 if (envValue.isEmpty())
137 qputenv(envName, Application::applicationDirPath().toLocal8Bit());
138 else
139 qputenv(envName, u"%1;%2"_s.arg(envValue, Application::applicationDirPath()).toLocal8Bit());
140 #endif
142 const QBtCommandLineParameters params = app->commandLineArgs();
143 if (!params.unknownParameter.isEmpty())
145 throw CommandLineParameterError(QCoreApplication::translate("Main", "%1 is an unknown command line parameter.",
146 "--random-parameter is an unknown command line parameter.")
147 .arg(params.unknownParameter));
149 #if !defined(Q_OS_WIN) || defined(DISABLE_GUI)
150 if (params.showVersion)
152 if (isOneArg)
154 displayVersion();
155 return EXIT_SUCCESS;
157 throw CommandLineParameterError(QCoreApplication::translate("Main", "%1 must be the single command line parameter.")
158 .arg(u"-v (or --version)"_s));
160 #endif
161 if (params.showHelp)
163 if (isOneArg)
165 displayUsage(QString::fromLocal8Bit(argv[0]));
166 return EXIT_SUCCESS;
168 throw CommandLineParameterError(QCoreApplication::translate("Main", "%1 must be the single command line parameter.")
169 .arg(u"-h (or --help)"_s));
172 const bool firstTimeUser = !Preferences::instance()->getAcceptedLegal();
173 if (firstTimeUser)
175 #ifndef DISABLE_GUI
176 if (!userAgreesWithLegalNotice())
177 return EXIT_SUCCESS;
178 #elif defined(Q_OS_WIN)
179 if (_isatty(_fileno(stdin))
180 && _isatty(_fileno(stdout))
181 && !userAgreesWithLegalNotice())
182 return EXIT_SUCCESS;
183 #else
184 if (!params.shouldDaemonize
185 && isatty(fileno(stdin))
186 && isatty(fileno(stdout))
187 && !userAgreesWithLegalNotice())
188 return EXIT_SUCCESS;
189 #endif
191 setCurrentMigrationVersion();
194 // Check if qBittorrent is already running for this user
195 if (app->isRunning())
197 #if defined(DISABLE_GUI) && !defined(Q_OS_WIN)
198 if (params.shouldDaemonize)
200 throw CommandLineParameterError(QCoreApplication::translate("Main", "You cannot use %1: qBittorrent is already running for this user.")
201 .arg(u"-d (or --daemon)"_s));
203 #endif
205 QThread::msleep(300);
206 app->callMainInstance();
208 return EXIT_SUCCESS;
211 #ifdef Q_OS_WIN
212 // This affects only Windows apparently and Qt5.
213 // When QNetworkAccessManager is instantiated it regularly starts polling
214 // the network interfaces to see what's available and their status.
215 // This polling creates jitter and high ping with wifi interfaces.
216 // So here we disable it for lack of better measure.
217 // It will also spew this message in the console: QObject::startTimer: Timers cannot have negative intervals
218 // For more info see:
219 // 1. https://github.com/qbittorrent/qBittorrent/issues/4209
220 // 2. https://bugreports.qt.io/browse/QTBUG-40332
221 // 3. https://bugreports.qt.io/browse/QTBUG-46015
223 qputenv("QT_BEARER_POLL_TIMEOUT", QByteArray::number(-1));
224 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) && !defined(DISABLE_GUI)
225 // this is the default in Qt6
226 app->setAttribute(Qt::AA_DisableWindowContextHelpButton);
227 #endif
228 #endif // Q_OS_WIN
230 #ifdef Q_OS_MACOS
231 // Since Apple made difficult for users to set PATH, we set here for convenience.
232 // Users are supposed to install Homebrew Python for search function.
233 // For more info see issue #5571.
234 QByteArray path = "/usr/local/bin:";
235 path += qgetenv("PATH");
236 qputenv("PATH", path.constData());
238 // On OS X the standard is to not show icons in the menus
239 app->setAttribute(Qt::AA_DontShowIconsInMenus);
240 #else
241 if (!Preferences::instance()->iconsInMenusEnabled())
242 app->setAttribute(Qt::AA_DontShowIconsInMenus);
243 #endif
245 #if defined(DISABLE_GUI) && !defined(Q_OS_WIN)
246 if (params.shouldDaemonize)
248 app.reset(); // Destroy current application
249 if (daemon(1, 0) == 0)
251 app = std::make_unique<Application>(argc, argv);
252 if (app->isRunning())
254 // Another instance had time to start.
255 return EXIT_FAILURE;
258 else
260 qCritical("Something went wrong while daemonizing, exiting...");
261 return EXIT_FAILURE;
264 #elif !defined(DISABLE_GUI)
265 if (!(params.noSplash || Preferences::instance()->isSplashScreenDisabled()))
266 showSplashScreen();
267 #endif
269 registerSignalHandlers();
271 return app->exec();
273 catch (const CommandLineParameterError &er)
275 displayBadArgMessage(er.message());
276 return EXIT_FAILURE;
278 catch (const RuntimeError &er)
280 displayErrorMessage(er.message());
281 return EXIT_FAILURE;
285 #if !defined(DISABLE_GUI)
286 void showSplashScreen()
288 QPixmap splashImg(u":/icons/splash.png"_s);
289 QPainter painter(&splashImg);
290 const auto version = QStringLiteral(QBT_VERSION);
291 painter.setPen(QPen(Qt::white));
292 painter.setFont(QFont(u"Arial"_s, 22, QFont::Black));
293 painter.drawText(224 - painter.fontMetrics().horizontalAdvance(version), 270, version);
294 QSplashScreen *splash = new QSplashScreen(splashImg);
295 splash->show();
296 QTimer::singleShot(1500ms, Qt::CoarseTimer, splash, &QObject::deleteLater);
297 qApp->processEvents();
299 #endif // DISABLE_GUI
301 void displayVersion()
303 printf("%s %s\n", qUtf8Printable(qApp->applicationName()), QBT_VERSION);
306 void displayBadArgMessage(const QString &message)
308 const QString help = QCoreApplication::translate("Main", "Run application with -h option to read about command line parameters.");
309 #if defined(Q_OS_WIN) && !defined(DISABLE_GUI)
310 QMessageBox msgBox(QMessageBox::Critical, QCoreApplication::translate("Main", "Bad command line"),
311 (message + u'\n' + help), QMessageBox::Ok);
312 msgBox.show(); // Need to be shown or to moveToCenter does not work
313 msgBox.move(Utils::Gui::screenCenter(&msgBox));
314 msgBox.exec();
315 #else
316 const QString errMsg = QCoreApplication::translate("Main", "Bad command line: ") + u'\n'
317 + message + u'\n'
318 + help + u'\n';
319 fprintf(stderr, "%s", qUtf8Printable(errMsg));
320 #endif
323 void displayErrorMessage(const QString &message)
325 #ifndef DISABLE_GUI
326 if (QApplication::instance())
328 QMessageBox msgBox;
329 msgBox.setIcon(QMessageBox::Critical);
330 msgBox.setText(QCoreApplication::translate("Main", "An unrecoverable error occurred."));
331 msgBox.setInformativeText(message);
332 msgBox.show(); // Need to be shown or to moveToCenter does not work
333 msgBox.move(Utils::Gui::screenCenter(&msgBox));
334 msgBox.exec();
336 else
338 const QString errMsg = QCoreApplication::translate("Main", "qBittorrent has encountered an unrecoverable error.") + u'\n' + message + u'\n';
339 fprintf(stderr, "%s", qUtf8Printable(errMsg));
341 #else
342 const QString errMsg = QCoreApplication::translate("Main", "qBittorrent has encountered an unrecoverable error.") + u'\n' + message + u'\n';
343 fprintf(stderr, "%s", qUtf8Printable(errMsg));
344 #endif
347 bool userAgreesWithLegalNotice()
349 Preferences *const pref = Preferences::instance();
350 Q_ASSERT(!pref->getAcceptedLegal());
352 #ifdef DISABLE_GUI
353 const QString eula = u"\n*** %1 ***\n"_s.arg(QCoreApplication::translate("Main", "Legal Notice"))
354 + QCoreApplication::translate("Main", "qBittorrent is a file sharing program. When you run a torrent, its data will be made available to others by means of upload. Any content you share is your sole responsibility.") + u"\n\n"
355 + QCoreApplication::translate("Main", "No further notices will be issued.") + u"\n\n"
356 + QCoreApplication::translate("Main", "Press %1 key to accept and continue...").arg(u"'y'"_s) + u'\n';
357 printf("%s", qUtf8Printable(eula));
359 const char ret = getchar(); // Read pressed key
360 if ((ret == 'y') || (ret == 'Y'))
362 // Save the answer
363 pref->setAcceptedLegal(true);
364 return true;
366 #else
367 QMessageBox msgBox;
368 msgBox.setText(QCoreApplication::translate("Main", "qBittorrent is a file sharing program. When you run a torrent, its data will be made available to others by means of upload. Any content you share is your sole responsibility.\n\nNo further notices will be issued."));
369 msgBox.setWindowTitle(QCoreApplication::translate("Main", "Legal notice"));
370 msgBox.addButton(QCoreApplication::translate("Main", "Cancel"), QMessageBox::RejectRole);
371 const QAbstractButton *agreeButton = msgBox.addButton(QCoreApplication::translate("Main", "I Agree"), QMessageBox::AcceptRole);
372 msgBox.show(); // Need to be shown or to moveToCenter does not work
373 msgBox.move(Utils::Gui::screenCenter(&msgBox));
374 msgBox.exec();
375 if (msgBox.clickedButton() == agreeButton)
377 // Save the answer
378 pref->setAcceptedLegal(true);
379 return true;
381 #endif // DISABLE_GUI
383 return false;
386 #ifdef Q_OS_UNIX
387 void adjustFileDescriptorLimit()
389 rlimit limit {};
391 if (getrlimit(RLIMIT_NOFILE, &limit) != 0)
392 return;
394 limit.rlim_cur = limit.rlim_max;
395 setrlimit(RLIMIT_NOFILE, &limit);
398 void adjustLocale()
400 // specify the default locale just in case if user has not set any other locale
401 // only `C` locale is available universally without installing locale packages
402 if (qEnvironmentVariableIsEmpty("LANG"))
403 qputenv("LANG", "C.UTF-8");
405 #endif