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>
37 #include <sys/resource.h>
44 #elif defined DISABLE_GUI
48 #include <QCoreApplication>
55 #include <QMessageBox>
58 #include <QPushButton>
59 #include <QSplashScreen>
64 Q_IMPORT_PLUGIN(QICOPlugin
)
65 #endif // QBT_STATIC_QT
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"
81 #include "gui/utils.h"
84 using namespace std::chrono_literals
;
86 void displayVersion();
87 bool userAgreesWithLegalNotice();
88 void displayBadArgMessage(const QString
&message
);
89 void displayErrorMessage(const QString
&message
);
92 void showSplashScreen();
96 void adjustFileDescriptorLimit();
100 int main(int argc
, char *argv
[])
103 adjustFileDescriptorLimit();
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
);
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());
124 qputenv(envName
, u
"%1;%2"_s
.arg(envValue
, Application::applicationDirPath()).toLocal8Bit());
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
)
142 throw CommandLineParameterError(QCoreApplication::translate("Main", "%1 must be the single command line parameter.")
143 .arg(u
"-v (or --version)"_s
));
150 displayUsage(QString::fromLocal8Bit(argv
[0]));
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();
161 if (!userAgreesWithLegalNotice())
163 #elif defined(Q_OS_WIN)
164 if (_isatty(_fileno(stdin
))
165 && _isatty(_fileno(stdout
))
166 && !userAgreesWithLegalNotice())
169 if (!params
.shouldDaemonize
170 && isatty(fileno(stdin
))
171 && isatty(fileno(stdout
))
172 && !userAgreesWithLegalNotice())
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
));
190 QThread::msleep(300);
191 app
->callMainInstance();
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));
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
);
222 if (!Preferences::instance()->iconsInMenusEnabled())
223 app
->setAttribute(Qt::AA_DontShowIconsInMenus
);
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.
241 qCritical("Something went wrong while daemonizing, exiting...");
245 #elif !defined(DISABLE_GUI)
246 if (!(params
.noSplash
|| Preferences::instance()->isSplashScreenDisabled()))
250 registerSignalHandlers();
254 catch (const CommandLineParameterError
&er
)
256 displayBadArgMessage(er
.message());
259 catch (const RuntimeError
&er
)
261 displayErrorMessage(er
.message());
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
);
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
));
297 const QString errMsg
= QCoreApplication::translate("Main", "Bad command line: ") + u
'\n'
300 fprintf(stderr
, "%s", qUtf8Printable(errMsg
));
304 void displayErrorMessage(const QString
&message
)
307 if (QApplication::instance())
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
));
319 const QString errMsg
= QCoreApplication::translate("Main", "qBittorrent has encountered an unrecoverable error.") + u
'\n' + message
+ u
'\n';
320 fprintf(stderr
, "%s", qUtf8Printable(errMsg
));
323 const QString errMsg
= QCoreApplication::translate("Main", "qBittorrent has encountered an unrecoverable error.") + u
'\n' + message
+ u
'\n';
324 fprintf(stderr
, "%s", qUtf8Printable(errMsg
));
328 bool userAgreesWithLegalNotice()
330 Preferences
*const pref
= Preferences::instance();
331 Q_ASSERT(!pref
->getAcceptedLegal());
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'))
344 pref
->setAcceptedLegal(true);
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
));
356 if (msgBox
.clickedButton() == agreeButton
)
359 pref
->setAcceptedLegal(true);
362 #endif // DISABLE_GUI
368 void adjustFileDescriptorLimit()
372 if (getrlimit(RLIMIT_NOFILE
, &limit
) != 0)
375 limit
.rlim_cur
= limit
.rlim_max
;
376 setrlimit(RLIMIT_NOFILE
, &limit
);