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.
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();
101 int main(int argc
, char *argv
[])
104 setvbuf(stdout
, nullptr, _IONBF
, 0);
109 adjustFileDescriptorLimit();
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
);
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
);
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());
139 qputenv(envName
, u
"%1;%2"_s
.arg(envValue
, Application::applicationDirPath()).toLocal8Bit());
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
)
157 throw CommandLineParameterError(QCoreApplication::translate("Main", "%1 must be the single command line parameter.")
158 .arg(u
"-v (or --version)"_s
));
165 displayUsage(QString::fromLocal8Bit(argv
[0]));
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();
176 if (!userAgreesWithLegalNotice())
178 #elif defined(Q_OS_WIN)
179 if (_isatty(_fileno(stdin
))
180 && _isatty(_fileno(stdout
))
181 && !userAgreesWithLegalNotice())
184 if (!params
.shouldDaemonize
185 && isatty(fileno(stdin
))
186 && isatty(fileno(stdout
))
187 && !userAgreesWithLegalNotice())
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
));
205 QThread::msleep(300);
206 app
->callMainInstance();
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
);
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
);
241 if (!Preferences::instance()->iconsInMenusEnabled())
242 app
->setAttribute(Qt::AA_DontShowIconsInMenus
);
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.
260 qCritical("Something went wrong while daemonizing, exiting...");
264 #elif !defined(DISABLE_GUI)
265 if (!(params
.noSplash
|| Preferences::instance()->isSplashScreenDisabled()))
269 registerSignalHandlers();
273 catch (const CommandLineParameterError
&er
)
275 displayBadArgMessage(er
.message());
278 catch (const RuntimeError
&er
)
280 displayErrorMessage(er
.message());
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
);
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
));
316 const QString errMsg
= QCoreApplication::translate("Main", "Bad command line: ") + u
'\n'
319 fprintf(stderr
, "%s", qUtf8Printable(errMsg
));
323 void displayErrorMessage(const QString
&message
)
326 if (QApplication::instance())
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
));
338 const QString errMsg
= QCoreApplication::translate("Main", "qBittorrent has encountered an unrecoverable error.") + u
'\n' + message
+ u
'\n';
339 fprintf(stderr
, "%s", qUtf8Printable(errMsg
));
342 const QString errMsg
= QCoreApplication::translate("Main", "qBittorrent has encountered an unrecoverable error.") + u
'\n' + message
+ u
'\n';
343 fprintf(stderr
, "%s", qUtf8Printable(errMsg
));
347 bool userAgreesWithLegalNotice()
349 Preferences
*const pref
= Preferences::instance();
350 Q_ASSERT(!pref
->getAcceptedLegal());
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'))
363 pref
->setAcceptedLegal(true);
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
));
375 if (msgBox
.clickedButton() == agreeButton
)
378 pref
->setAcceptedLegal(true);
381 #endif // DISABLE_GUI
387 void adjustFileDescriptorLimit()
391 if (getrlimit(RLIMIT_NOFILE
, &limit
) != 0)
394 limit
.rlim_cur
= limit
.rlim_max
;
395 setrlimit(RLIMIT_NOFILE
, &limit
);
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");