2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2014-2023 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 <QSplashScreen>
62 #include <QOperatingSystemVersion>
67 Q_IMPORT_PLUGIN(QICOPlugin
)
68 #endif // QBT_STATIC_QT
74 #include "base/global.h"
75 #include "base/logger.h"
76 #include "base/preferences.h"
77 #include "base/profile.h"
78 #include "base/settingvalue.h"
79 #include "base/version.h"
80 #include "application.h"
81 #include "cmdoptions.h"
82 #include "legalnotice.h"
83 #include "signalhandler.h"
86 #include "gui/utils.h"
89 using namespace std::chrono_literals
;
93 void displayBadArgMessage(const QString
&message
)
95 const QString help
= QCoreApplication::translate("Main", "Run application with -h option to read about command line parameters.");
96 #if defined(Q_OS_WIN) && !defined(DISABLE_GUI)
97 QMessageBox
msgBox(QMessageBox::Critical
, QCoreApplication::translate("Main", "Bad command line"),
98 (message
+ u
'\n' + help
), QMessageBox::Ok
);
99 msgBox
.show(); // Need to be shown or to moveToCenter does not work
100 msgBox
.move(Utils::Gui::screenCenter(&msgBox
));
103 const QString errMsg
= QCoreApplication::translate("Main", "Bad command line: ") + u
'\n'
106 fprintf(stderr
, "%s", qUtf8Printable(errMsg
));
110 void displayErrorMessage(const QString
&message
)
113 if (QApplication::instance())
116 msgBox
.setIcon(QMessageBox::Critical
);
117 msgBox
.setText(QCoreApplication::translate("Main", "An unrecoverable error occurred."));
118 msgBox
.setInformativeText(message
);
119 msgBox
.show(); // Need to be shown or to moveToCenter does not work
120 msgBox
.move(Utils::Gui::screenCenter(&msgBox
));
125 const QString errMsg
= QCoreApplication::translate("Main", "qBittorrent has encountered an unrecoverable error.") + u
'\n' + message
+ u
'\n';
126 fprintf(stderr
, "%s", qUtf8Printable(errMsg
));
129 const QString errMsg
= QCoreApplication::translate("Main", "qBittorrent has encountered an unrecoverable error.") + u
'\n' + message
+ u
'\n';
130 fprintf(stderr
, "%s", qUtf8Printable(errMsg
));
134 void displayVersion()
136 printf("%s %s\n", qUtf8Printable(qApp
->applicationName()), QBT_VERSION
);
140 void showSplashScreen()
142 QPixmap
splashImg(u
":/icons/splash.png"_s
);
143 QPainter
painter(&splashImg
);
144 const auto version
= QStringLiteral(QBT_VERSION
);
145 painter
.setPen(QPen(Qt::white
));
146 painter
.setFont(QFont(u
"Arial"_s
, 22, QFont::Black
));
147 painter
.drawText(224 - painter
.fontMetrics().horizontalAdvance(version
), 270, version
);
148 QSplashScreen
*splash
= new QSplashScreen(splashImg
);
150 QTimer::singleShot(1500ms
, Qt::CoarseTimer
, splash
, &QObject::deleteLater
);
151 qApp
->processEvents();
153 #endif // DISABLE_GUI
156 void adjustFileDescriptorLimit()
160 if (getrlimit(RLIMIT_NOFILE
, &limit
) != 0)
163 limit
.rlim_cur
= limit
.rlim_max
;
164 setrlimit(RLIMIT_NOFILE
, &limit
);
169 // specify the default locale just in case if user has not set any other locale
170 // only `C` locale is available universally without installing locale packages
171 if (qEnvironmentVariableIsEmpty("LANG"))
172 qputenv("LANG", "C.UTF-8");
178 int main(int argc
, char *argv
[])
181 setvbuf(stdout
, nullptr, _IONBF
, 0);
186 adjustFileDescriptorLimit();
189 // We must save it here because QApplication constructor may change it
190 const bool isOneArg
= (argc
== 2);
192 #if !defined(DISABLE_GUI) && defined(Q_OS_WIN)
193 if (QOperatingSystemVersion::current() >= QOperatingSystemVersion::Windows10
)
194 QApplication::setStyle(u
"Fusion"_s
);
197 // `app` must be declared out of try block to allow display message box in case of exception
198 std::unique_ptr
<Application
> app
;
201 // Create Application
202 app
= std::make_unique
<Application
>(argc
, argv
);
205 // QCoreApplication::applicationDirPath() needs an Application object instantiated first
206 // Let's hope that there won't be a crash before this line
207 const char envName
[] = "_NT_SYMBOL_PATH";
208 const QString envValue
= qEnvironmentVariable(envName
);
209 if (envValue
.isEmpty())
210 qputenv(envName
, Application::applicationDirPath().toLocal8Bit());
212 qputenv(envName
, u
"%1;%2"_s
.arg(envValue
, Application::applicationDirPath()).toLocal8Bit());
215 const QBtCommandLineParameters params
= app
->commandLineArgs();
216 if (!params
.unknownParameter
.isEmpty())
218 throw CommandLineParameterError(QCoreApplication::translate("Main", "%1 is an unknown command line parameter.",
219 "--random-parameter is an unknown command line parameter.")
220 .arg(params
.unknownParameter
));
222 #if !defined(Q_OS_WIN) || defined(DISABLE_GUI)
223 if (params
.showVersion
)
230 throw CommandLineParameterError(QCoreApplication::translate("Main", "%1 must be the single command line parameter.")
231 .arg(u
"-v (or --version)"_s
));
238 displayUsage(QString::fromLocal8Bit(argv
[0]));
241 throw CommandLineParameterError(QCoreApplication::translate("Main", "%1 must be the single command line parameter.")
242 .arg(u
"-h (or --help)"_s
));
245 // Check if qBittorrent is already running
246 if (app
->hasAnotherInstance())
248 #if defined(DISABLE_GUI) && !defined(Q_OS_WIN)
249 if (params
.shouldDaemonize
)
251 throw CommandLineParameterError(QCoreApplication::translate("Main", "You cannot use %1: qBittorrent is already running.")
252 .arg(u
"-d (or --daemon)"_s
));
255 // print friendly message if there are no other command line args
258 const QString message
= QCoreApplication::translate("Main", "Another qBittorrent instance is already running.");
259 printf("%s\n", qUtf8Printable(message
));
263 QThread::msleep(300);
264 app
->callMainInstance();
269 CachedSettingValue
<bool> legalNoticeShown
{u
"LegalNotice/Accepted"_s
, false};
270 if (params
.confirmLegalNotice
)
271 legalNoticeShown
= true;
273 if (!legalNoticeShown
)
276 const bool isInteractive
= true;
277 #elif defined(Q_OS_WIN)
278 const bool isInteractive
= (_isatty(_fileno(stdin
)) != 0) && (_isatty(_fileno(stdout
)) != 0);
280 // when run in daemon mode user can only dismiss the notice with command line option
281 const bool isInteractive
= !params
.shouldDaemonize
282 && ((isatty(fileno(stdin
)) != 0) && (isatty(fileno(stdout
)) != 0));
284 showLegalNotice(isInteractive
);
286 legalNoticeShown
= true;
290 // Since Apple made difficult for users to set PATH, we set here for convenience.
291 // Users are supposed to install Homebrew Python for search function.
292 // For more info see issue #5571.
293 const QByteArray path
= "/usr/local/bin:" + qgetenv("PATH");
294 qputenv("PATH", path
.constData());
296 // On OS X the standard is to not show icons in the menus
297 app
->setAttribute(Qt::AA_DontShowIconsInMenus
);
299 if (!Preferences::instance()->iconsInMenusEnabled())
300 app
->setAttribute(Qt::AA_DontShowIconsInMenus
);
303 #if defined(DISABLE_GUI) && !defined(Q_OS_WIN)
304 if (params
.shouldDaemonize
)
306 app
.reset(); // Destroy current application instance
307 if (::daemon(1, 0) == 0)
309 app
= std::make_unique
<Application
>(argc
, argv
);
310 if (app
->hasAnotherInstance())
312 // It is undefined behavior to write to log file since there is another qbt instance
313 // in play. But we still do it since there is chance that the log message will survive.
314 const QString errorMessage
= QCoreApplication::translate("Main", "Found unexpected qBittorrent instance. Exiting this instance. Current process ID: %1.")
315 .arg(QString::number(QCoreApplication::applicationPid()));
316 LogMsg(errorMessage
, Log::CRITICAL
);
317 // stdout, stderr is closed so we can't use them
323 const QString errorMessage
= QCoreApplication::translate("Main", "Error when daemonizing. Reason: \"%1\". Error code: %2.")
324 .arg(QString::fromLocal8Bit(strerror(errno
)), QString::number(errno
));
325 LogMsg(errorMessage
, Log::CRITICAL
);
326 qCritical("%s", qUtf8Printable(errorMessage
));
330 #elif !defined(DISABLE_GUI)
331 if (!(params
.noSplash
|| Preferences::instance()->isSplashScreenDisabled()))
335 registerSignalHandlers();
339 catch (const CommandLineParameterError
&er
)
341 displayBadArgMessage(er
.message());
344 catch (const RuntimeError
&er
)
346 displayErrorMessage(er
.message());