Display error message when unrecoverable error occurred
[qBittorrent.git] / src / app / main.cpp
blobfcd33212a8ff0470d63daae54f51427dda66bca7
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 <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 <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 #endif
99 // Main
100 int main(int argc, char *argv[])
102 #ifdef Q_OS_UNIX
103 adjustFileDescriptorLimit();
104 #endif
106 // We must save it here because QApplication constructor may change it
107 bool isOneArg = (argc == 2);
109 // `app` must be declared out of try block to allow display message box in case of exception
110 std::unique_ptr<Application> app;
113 // Create Application
114 app = std::make_unique<Application>(argc, argv);
116 #ifdef Q_OS_WIN
117 // QCoreApplication::applicationDirPath() needs an Application object instantiated first
118 // Let's hope that there won't be a crash before this line
119 const char *envName = "_NT_SYMBOL_PATH";
120 const QString envValue = qEnvironmentVariable(envName);
121 if (envValue.isEmpty())
122 qputenv(envName, Application::applicationDirPath().toLocal8Bit());
123 else
124 qputenv(envName, u"%1;%2"_s.arg(envValue, Application::applicationDirPath()).toLocal8Bit());
125 #endif
127 const QBtCommandLineParameters params = app->commandLineArgs();
128 if (!params.unknownParameter.isEmpty())
130 throw CommandLineParameterError(QCoreApplication::translate("Main", "%1 is an unknown command line parameter.",
131 "--random-parameter is an unknown command line parameter.")
132 .arg(params.unknownParameter));
134 #if !defined(Q_OS_WIN) || defined(DISABLE_GUI)
135 if (params.showVersion)
137 if (isOneArg)
139 displayVersion();
140 return EXIT_SUCCESS;
142 throw CommandLineParameterError(QCoreApplication::translate("Main", "%1 must be the single command line parameter.")
143 .arg(u"-v (or --version)"_s));
145 #endif
146 if (params.showHelp)
148 if (isOneArg)
150 displayUsage(QString::fromLocal8Bit(argv[0]));
151 return EXIT_SUCCESS;
153 throw CommandLineParameterError(QCoreApplication::translate("Main", "%1 must be the single command line parameter.")
154 .arg(u"-h (or --help)"_s));
157 const bool firstTimeUser = !Preferences::instance()->getAcceptedLegal();
158 if (firstTimeUser)
160 #ifndef DISABLE_GUI
161 if (!userAgreesWithLegalNotice())
162 return EXIT_SUCCESS;
163 #elif defined(Q_OS_WIN)
164 if (_isatty(_fileno(stdin))
165 && _isatty(_fileno(stdout))
166 && !userAgreesWithLegalNotice())
167 return EXIT_SUCCESS;
168 #else
169 if (!params.shouldDaemonize
170 && isatty(fileno(stdin))
171 && isatty(fileno(stdout))
172 && !userAgreesWithLegalNotice())
173 return EXIT_SUCCESS;
174 #endif
176 setCurrentMigrationVersion();
179 // Check if qBittorrent is already running for this user
180 if (app->isRunning())
182 #if defined(DISABLE_GUI) && !defined(Q_OS_WIN)
183 if (params.shouldDaemonize)
185 throw CommandLineParameterError(QCoreApplication::translate("Main", "You cannot use %1: qBittorrent is already running for this user.")
186 .arg(u"-d (or --daemon)"_s));
188 #endif
190 QThread::msleep(300);
191 app->callMainInstance();
193 return EXIT_SUCCESS;
196 #ifdef Q_OS_WIN
197 // This affects only Windows apparently and Qt5.
198 // When QNetworkAccessManager is instantiated it regularly starts polling
199 // the network interfaces to see what's available and their status.
200 // This polling creates jitter and high ping with wifi interfaces.
201 // So here we disable it for lack of better measure.
202 // It will also spew this message in the console: QObject::startTimer: Timers cannot have negative intervals
203 // For more info see:
204 // 1. https://github.com/qbittorrent/qBittorrent/issues/4209
205 // 2. https://bugreports.qt.io/browse/QTBUG-40332
206 // 3. https://bugreports.qt.io/browse/QTBUG-46015
208 qputenv("QT_BEARER_POLL_TIMEOUT", QByteArray::number(-1));
209 #endif // Q_OS_WIN
211 #ifdef Q_OS_MACOS
212 // Since Apple made difficult for users to set PATH, we set here for convenience.
213 // Users are supposed to install Homebrew Python for search function.
214 // For more info see issue #5571.
215 QByteArray path = "/usr/local/bin:";
216 path += qgetenv("PATH");
217 qputenv("PATH", path.constData());
219 // On OS X the standard is to not show icons in the menus
220 app->setAttribute(Qt::AA_DontShowIconsInMenus);
221 #else
222 if (!Preferences::instance()->iconsInMenusEnabled())
223 app->setAttribute(Qt::AA_DontShowIconsInMenus);
224 #endif
226 #if defined(DISABLE_GUI) && !defined(Q_OS_WIN)
227 if (params.shouldDaemonize)
229 app.reset(); // Destroy current application
230 if (daemon(1, 0) == 0)
232 app = std::make_unique<Application>(argc, argv);
233 if (app->isRunning())
235 // Another instance had time to start.
236 return EXIT_FAILURE;
239 else
241 qCritical("Something went wrong while daemonizing, exiting...");
242 return EXIT_FAILURE;
245 #elif !defined(DISABLE_GUI)
246 if (!(params.noSplash || Preferences::instance()->isSplashScreenDisabled()))
247 showSplashScreen();
248 #endif
250 registerSignalHandlers();
252 return app->exec();
254 catch (const CommandLineParameterError &er)
256 displayBadArgMessage(er.message());
257 return EXIT_FAILURE;
259 catch (const RuntimeError &er)
261 displayErrorMessage(er.message());
262 return EXIT_FAILURE;
266 #if !defined(DISABLE_GUI)
267 void showSplashScreen()
269 QPixmap splashImg(u":/icons/splash.png"_s);
270 QPainter painter(&splashImg);
271 const auto version = QStringLiteral(QBT_VERSION);
272 painter.setPen(QPen(Qt::white));
273 painter.setFont(QFont(u"Arial"_s, 22, QFont::Black));
274 painter.drawText(224 - painter.fontMetrics().horizontalAdvance(version), 270, version);
275 QSplashScreen *splash = new QSplashScreen(splashImg);
276 splash->show();
277 QTimer::singleShot(1500ms, Qt::CoarseTimer, splash, &QObject::deleteLater);
278 qApp->processEvents();
280 #endif // DISABLE_GUI
282 void displayVersion()
284 printf("%s %s\n", qUtf8Printable(qApp->applicationName()), QBT_VERSION);
287 void displayBadArgMessage(const QString &message)
289 const QString help = QCoreApplication::translate("Main", "Run application with -h option to read about command line parameters.");
290 #if defined(Q_OS_WIN) && !defined(DISABLE_GUI)
291 QMessageBox msgBox(QMessageBox::Critical, QCoreApplication::translate("Main", "Bad command line"),
292 (message + u'\n' + help), QMessageBox::Ok);
293 msgBox.show(); // Need to be shown or to moveToCenter does not work
294 msgBox.move(Utils::Gui::screenCenter(&msgBox));
295 msgBox.exec();
296 #else
297 const QString errMsg = QCoreApplication::translate("Main", "Bad command line: ") + u'\n'
298 + message + u'\n'
299 + help + u'\n';
300 fprintf(stderr, "%s", qUtf8Printable(errMsg));
301 #endif
304 void displayErrorMessage(const QString &message)
306 #ifndef DISABLE_GUI
307 if (QApplication::instance())
309 QMessageBox msgBox;
310 msgBox.setIcon(QMessageBox::Critical);
311 msgBox.setText(QCoreApplication::translate("Main", "An unrecoverable error occurred."));
312 msgBox.setInformativeText(message);
313 msgBox.show(); // Need to be shown or to moveToCenter does not work
314 msgBox.move(Utils::Gui::screenCenter(&msgBox));
315 msgBox.exec();
317 else
319 const QString errMsg = QCoreApplication::translate("Main", "qBittorrent has encountered an unrecoverable error.") + u'\n' + message + u'\n';
320 fprintf(stderr, "%s", qUtf8Printable(errMsg));
322 #else
323 const QString errMsg = QCoreApplication::translate("Main", "qBittorrent has encountered an unrecoverable error.") + u'\n' + message + u'\n';
324 fprintf(stderr, "%s", qUtf8Printable(errMsg));
325 #endif
328 bool userAgreesWithLegalNotice()
330 Preferences *const pref = Preferences::instance();
331 Q_ASSERT(!pref->getAcceptedLegal());
333 #ifdef DISABLE_GUI
334 const QString eula = u"\n*** %1 ***\n"_s.arg(QCoreApplication::translate("Main", "Legal Notice"))
335 + 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"
336 + QCoreApplication::translate("Main", "No further notices will be issued.") + u"\n\n"
337 + QCoreApplication::translate("Main", "Press %1 key to accept and continue...").arg(u"'y'"_s) + u'\n';
338 printf("%s", qUtf8Printable(eula));
340 const char ret = getchar(); // Read pressed key
341 if ((ret == 'y') || (ret == 'Y'))
343 // Save the answer
344 pref->setAcceptedLegal(true);
345 return true;
347 #else
348 QMessageBox msgBox;
349 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."));
350 msgBox.setWindowTitle(QCoreApplication::translate("Main", "Legal notice"));
351 msgBox.addButton(QCoreApplication::translate("Main", "Cancel"), QMessageBox::RejectRole);
352 const QAbstractButton *agreeButton = msgBox.addButton(QCoreApplication::translate("Main", "I Agree"), QMessageBox::AcceptRole);
353 msgBox.show(); // Need to be shown or to moveToCenter does not work
354 msgBox.move(Utils::Gui::screenCenter(&msgBox));
355 msgBox.exec();
356 if (msgBox.clickedButton() == agreeButton)
358 // Save the answer
359 pref->setAcceptedLegal(true);
360 return true;
362 #endif // DISABLE_GUI
364 return false;
367 #ifdef Q_OS_UNIX
368 void adjustFileDescriptorLimit()
370 rlimit limit {};
372 if (getrlimit(RLIMIT_NOFILE, &limit) != 0)
373 return;
375 limit.rlim_cur = limit.rlim_max;
376 setrlimit(RLIMIT_NOFILE, &limit);
378 #endif