2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2014-2024 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>
63 Q_IMPORT_PLUGIN(QICOPlugin
)
64 #endif // QBT_STATIC_QT
70 #include "base/global.h"
71 #include "base/logger.h"
72 #include "base/preferences.h"
73 #include "base/profile.h"
74 #include "base/settingvalue.h"
75 #include "base/version.h"
76 #include "application.h"
77 #include "cmdoptions.h"
78 #include "legalnotice.h"
79 #include "signalhandler.h"
82 #include "gui/utils.h"
85 using namespace std::chrono_literals
;
89 void displayBadArgMessage(const QString
&message
)
91 const QString help
= QCoreApplication::translate("Main", "Run application with -h option to read about command line parameters.");
92 #if defined(Q_OS_WIN) && !defined(DISABLE_GUI)
93 QMessageBox
msgBox(QMessageBox::Critical
, QCoreApplication::translate("Main", "Bad command line"),
94 (message
+ u
'\n' + help
), QMessageBox::Ok
);
95 msgBox
.show(); // Need to be shown or to moveToCenter does not work
96 msgBox
.move(Utils::Gui::screenCenter(&msgBox
));
99 const QString errMsg
= QCoreApplication::translate("Main", "Bad command line: ") + u
'\n'
102 fprintf(stderr
, "%s", qUtf8Printable(errMsg
));
106 void displayErrorMessage(const QString
&message
)
109 if (QApplication::instance())
112 msgBox
.setIcon(QMessageBox::Critical
);
113 msgBox
.setText(QCoreApplication::translate("Main", "An unrecoverable error occurred."));
114 msgBox
.setInformativeText(message
);
115 msgBox
.show(); // Need to be shown or to moveToCenter does not work
116 msgBox
.move(Utils::Gui::screenCenter(&msgBox
));
121 const QString errMsg
= QCoreApplication::translate("Main", "qBittorrent has encountered an unrecoverable error.") + u
'\n' + message
+ u
'\n';
122 fprintf(stderr
, "%s", qUtf8Printable(errMsg
));
125 const QString errMsg
= QCoreApplication::translate("Main", "qBittorrent has encountered an unrecoverable error.") + u
'\n' + message
+ u
'\n';
126 fprintf(stderr
, "%s", qUtf8Printable(errMsg
));
130 void displayVersion()
132 printf("%s %s\n", qUtf8Printable(qApp
->applicationName()), QBT_VERSION
);
136 void showSplashScreen()
138 QPixmap
splashImg(u
":/icons/splash.png"_s
);
139 QPainter
painter(&splashImg
);
140 const auto version
= QStringLiteral(QBT_VERSION
);
141 painter
.setPen(QPen(Qt::white
));
142 painter
.setFont(QFont(u
"Arial"_s
, 22, QFont::Black
));
143 painter
.drawText(224 - painter
.fontMetrics().horizontalAdvance(version
), 270, version
);
144 QSplashScreen
*splash
= new QSplashScreen(splashImg
);
146 QTimer::singleShot(1500ms
, Qt::CoarseTimer
, splash
, &QObject::deleteLater
);
147 qApp
->processEvents();
149 #endif // DISABLE_GUI
152 void adjustFileDescriptorLimit()
156 if (getrlimit(RLIMIT_NOFILE
, &limit
) != 0)
159 limit
.rlim_cur
= limit
.rlim_max
;
160 setrlimit(RLIMIT_NOFILE
, &limit
);
165 // specify the default locale just in case if user has not set any other locale
166 // only `C` locale is available universally without installing locale packages
167 if (qEnvironmentVariableIsEmpty("LANG"))
168 qputenv("LANG", "C.UTF-8");
174 int main(int argc
, char *argv
[])
177 setvbuf(stdout
, nullptr, _IONBF
, 0);
182 adjustFileDescriptorLimit();
185 // We must save it here because QApplication constructor may change it
186 const bool isOneArg
= (argc
== 2);
188 // `app` must be declared out of try block to allow display message box in case of exception
189 std::unique_ptr
<Application
> app
;
192 // Create Application
193 app
= std::make_unique
<Application
>(argc
, argv
);
196 // QCoreApplication::applicationDirPath() needs an Application object instantiated first
197 // Let's hope that there won't be a crash before this line
198 const char envName
[] = "_NT_SYMBOL_PATH";
199 const QString envValue
= qEnvironmentVariable(envName
);
200 if (envValue
.isEmpty())
201 qputenv(envName
, Application::applicationDirPath().toLocal8Bit());
203 qputenv(envName
, u
"%1;%2"_s
.arg(envValue
, Application::applicationDirPath()).toLocal8Bit());
206 const QBtCommandLineParameters params
= app
->commandLineArgs();
207 if (!params
.unknownParameter
.isEmpty())
209 throw CommandLineParameterError(QCoreApplication::translate("Main", "%1 is an unknown command line parameter.",
210 "--random-parameter is an unknown command line parameter.")
211 .arg(params
.unknownParameter
));
213 #if !defined(Q_OS_WIN) || defined(DISABLE_GUI)
214 if (params
.showVersion
)
221 throw CommandLineParameterError(QCoreApplication::translate("Main", "%1 must be the single command line parameter.")
222 .arg(u
"-v (or --version)"_s
));
229 displayUsage(QString::fromLocal8Bit(argv
[0]));
232 throw CommandLineParameterError(QCoreApplication::translate("Main", "%1 must be the single command line parameter.")
233 .arg(u
"-h (or --help)"_s
));
236 // Check if qBittorrent is already running
237 if (app
->hasAnotherInstance())
239 #if defined(DISABLE_GUI) && !defined(Q_OS_WIN)
240 if (params
.shouldDaemonize
)
242 throw CommandLineParameterError(QCoreApplication::translate("Main", "You cannot use %1: qBittorrent is already running.")
243 .arg(u
"-d (or --daemon)"_s
));
246 // print friendly message if there are no other command line args
249 const QString message
= QCoreApplication::translate("Main", "Another qBittorrent instance is already running.");
250 printf("%s\n", qUtf8Printable(message
));
254 QThread::msleep(300);
255 app
->callMainInstance();
260 CachedSettingValue
<bool> legalNoticeShown
{u
"LegalNotice/Accepted"_s
, false};
261 if (params
.confirmLegalNotice
)
262 legalNoticeShown
= true;
264 if (!legalNoticeShown
)
267 const bool isInteractive
= true;
268 #elif defined(Q_OS_WIN)
269 const bool isInteractive
= (_isatty(_fileno(stdin
)) != 0) && (_isatty(_fileno(stdout
)) != 0);
271 // when run in daemon mode user can only dismiss the notice with command line option
272 const bool isInteractive
= !params
.shouldDaemonize
273 && ((isatty(fileno(stdin
)) != 0) && (isatty(fileno(stdout
)) != 0));
275 showLegalNotice(isInteractive
);
277 legalNoticeShown
= true;
281 // Since Apple made difficult for users to set PATH, we set here for convenience.
282 // Users are supposed to install Homebrew Python for search function.
283 // For more info see issue #5571.
284 const QByteArray path
= "/usr/local/bin:" + qgetenv("PATH");
285 qputenv("PATH", path
.constData());
287 // On OS X the standard is to not show icons in the menus
288 app
->setAttribute(Qt::AA_DontShowIconsInMenus
);
290 if (!Preferences::instance()->iconsInMenusEnabled())
291 app
->setAttribute(Qt::AA_DontShowIconsInMenus
);
294 #if defined(DISABLE_GUI) && !defined(Q_OS_WIN)
295 if (params
.shouldDaemonize
)
297 app
.reset(); // Destroy current application instance
298 if (::daemon(1, 0) == 0)
300 app
= std::make_unique
<Application
>(argc
, argv
);
301 if (app
->hasAnotherInstance())
303 // It is undefined behavior to write to log file since there is another qbt instance
304 // in play. But we still do it since there is chance that the log message will survive.
305 const QString errorMessage
= QCoreApplication::translate("Main", "Found unexpected qBittorrent instance. Exiting this instance. Current process ID: %1.")
306 .arg(QString::number(QCoreApplication::applicationPid()));
307 LogMsg(errorMessage
, Log::CRITICAL
);
308 // stdout, stderr is closed so we can't use them
314 const QString errorMessage
= QCoreApplication::translate("Main", "Error when daemonizing. Reason: \"%1\". Error code: %2.")
315 .arg(QString::fromLocal8Bit(strerror(errno
)), QString::number(errno
));
316 LogMsg(errorMessage
, Log::CRITICAL
);
317 qCritical("%s", qUtf8Printable(errorMessage
));
321 #elif !defined(DISABLE_GUI)
322 if (!(params
.noSplash
|| Preferences::instance()->isSplashScreenDisabled()))
326 registerSignalHandlers();
330 catch (const CommandLineParameterError
&er
)
332 displayBadArgMessage(er
.message());
335 catch (const RuntimeError
&er
)
337 displayErrorMessage(er
.message());