2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2015 Vladimir Golovnev <glassez@yandex.ru>
4 * Copyright (C) 2006 Christophe Dumez
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 "application.h"
42 #elif defined(Q_OS_UNIX)
43 #include <sys/resource.h>
48 #include <QLibraryInfo>
49 #include <QMetaObject>
54 #include <QMessageBox>
55 #include <QPixmapCache>
56 #include <QProgressDialog>
58 #include <QSessionManager>
59 #include <QSharedMemory>
62 #include <QFileOpenEvent>
66 #include "base/bittorrent/infohash.h"
67 #include "base/bittorrent/session.h"
68 #include "base/bittorrent/torrent.h"
69 #include "base/exceptions.h"
70 #include "base/global.h"
71 #include "base/iconprovider.h"
72 #include "base/logger.h"
73 #include "base/net/downloadmanager.h"
74 #include "base/net/geoipmanager.h"
75 #include "base/net/proxyconfigurationmanager.h"
76 #include "base/net/smtp.h"
77 #include "base/preferences.h"
78 #include "base/profile.h"
79 #include "base/rss/rss_autodownloader.h"
80 #include "base/rss/rss_session.h"
81 #include "base/search/searchpluginmanager.h"
82 #include "base/settingsstorage.h"
83 #include "base/torrentfileswatcher.h"
84 #include "base/utils/compare.h"
85 #include "base/utils/fs.h"
86 #include "base/utils/misc.h"
87 #include "base/utils/string.h"
88 #include "base/version.h"
89 #include "applicationinstancemanager.h"
90 #include "filelogger.h"
94 #include "gui/addnewtorrentdialog.h"
95 #include "gui/desktopintegration.h"
96 #include "gui/mainwindow.h"
97 #include "gui/shutdownconfirmdialog.h"
98 #include "gui/uithememanager.h"
99 #include "gui/utils.h"
100 #include "gui/windowstate.h"
101 #endif // DISABLE_GUI
103 #ifndef DISABLE_WEBUI
104 #include "webui/webui.h"
109 #define SETTINGS_KEY(name) u"Application/" name
110 #define FILELOGGER_SETTINGS_KEY(name) (SETTINGS_KEY(u"FileLogger/") name)
111 #define NOTIFICATIONS_SETTINGS_KEY(name) (SETTINGS_KEY(u"GUI/Notifications/"_s) name)
113 const QString LOG_FOLDER
= u
"logs"_s
;
114 const QChar PARAMS_SEPARATOR
= u
'|';
116 const Path DEFAULT_PORTABLE_MODE_PROFILE_DIR
{u
"profile"_s
};
118 const int MIN_FILELOG_SIZE
= 1024; // 1KiB
119 const int MAX_FILELOG_SIZE
= 1000 * 1024 * 1024; // 1000MiB
120 const int DEFAULT_FILELOG_SIZE
= 65 * 1024; // 65KiB
123 const int PIXMAP_CACHE_SIZE
= 64 * 1024 * 1024; // 64MiB
126 QString
serializeParams(const QBtCommandLineParameters
¶ms
)
129 // Because we're passing a string list to the currently running
130 // qBittorrent process, we need some way of passing along the options
131 // the user has specified. Here we place special strings that are
132 // almost certainly not going to collide with a file path or URL
133 // specified by the user, and placing them at the beginning of the
134 // string list so that they will be processed before the list of
135 // torrent paths or URLs.
137 const BitTorrent::AddTorrentParams
&addTorrentParams
= params
.addTorrentParams
;
139 if (!addTorrentParams
.savePath
.isEmpty())
140 result
.append(u
"@savePath=" + addTorrentParams
.savePath
.data());
142 if (addTorrentParams
.addPaused
.has_value())
143 result
.append(*addTorrentParams
.addPaused
? u
"@addPaused=1"_s
: u
"@addPaused=0"_s
);
145 if (addTorrentParams
.skipChecking
)
146 result
.append(u
"@skipChecking"_s
);
148 if (!addTorrentParams
.category
.isEmpty())
149 result
.append(u
"@category=" + addTorrentParams
.category
);
151 if (addTorrentParams
.sequential
)
152 result
.append(u
"@sequential"_s
);
154 if (addTorrentParams
.firstLastPiecePriority
)
155 result
.append(u
"@firstLastPiecePriority"_s
);
157 if (params
.skipDialog
.has_value())
158 result
.append(*params
.skipDialog
? u
"@skipDialog=1"_s
: u
"@skipDialog=0"_s
);
160 result
+= params
.torrentSources
;
162 return result
.join(PARAMS_SEPARATOR
);
165 QBtCommandLineParameters
parseParams(const QString
&str
)
167 QBtCommandLineParameters parsedParams
;
168 BitTorrent::AddTorrentParams
&addTorrentParams
= parsedParams
.addTorrentParams
;
170 for (QString param
: asConst(str
.split(PARAMS_SEPARATOR
, Qt::SkipEmptyParts
)))
172 param
= param
.trimmed();
174 // Process strings indicating options specified by the user.
176 if (param
.startsWith(u
"@savePath="))
178 addTorrentParams
.savePath
= Path(param
.mid(10));
182 if (param
.startsWith(u
"@addPaused="))
184 addTorrentParams
.addPaused
= (QStringView(param
).mid(11).toInt() != 0);
188 if (param
== u
"@skipChecking")
190 addTorrentParams
.skipChecking
= true;
194 if (param
.startsWith(u
"@category="))
196 addTorrentParams
.category
= param
.mid(10);
200 if (param
== u
"@sequential")
202 addTorrentParams
.sequential
= true;
206 if (param
== u
"@firstLastPiecePriority")
208 addTorrentParams
.firstLastPiecePriority
= true;
212 if (param
.startsWith(u
"@skipDialog="))
214 parsedParams
.skipDialog
= (QStringView(param
).mid(12).toInt() != 0);
218 parsedParams
.torrentSources
.append(param
);
225 Application::Application(int &argc
, char **argv
)
226 : BaseApplication(argc
, argv
)
227 , m_commandLineArgs(parseCommandLine(Application::arguments()))
228 , m_storeFileLoggerEnabled(FILELOGGER_SETTINGS_KEY(u
"Enabled"_s
))
229 , m_storeFileLoggerBackup(FILELOGGER_SETTINGS_KEY(u
"Backup"_s
))
230 , m_storeFileLoggerDeleteOld(FILELOGGER_SETTINGS_KEY(u
"DeleteOld"_s
))
231 , m_storeFileLoggerMaxSize(FILELOGGER_SETTINGS_KEY(u
"MaxSizeBytes"_s
))
232 , m_storeFileLoggerAge(FILELOGGER_SETTINGS_KEY(u
"Age"_s
))
233 , m_storeFileLoggerAgeType(FILELOGGER_SETTINGS_KEY(u
"AgeType"_s
))
234 , m_storeFileLoggerPath(FILELOGGER_SETTINGS_KEY(u
"Path"_s
))
235 , m_storeMemoryWorkingSetLimit(SETTINGS_KEY(u
"MemoryWorkingSetLimit"_s
))
237 , m_processMemoryPriority(SETTINGS_KEY(u
"ProcessMemoryPriority"_s
))
240 , m_startUpWindowState(u
"GUI/StartUpWindowState"_s
)
241 , m_storeNotificationTorrentAdded(NOTIFICATIONS_SETTINGS_KEY(u
"TorrentAdded"_s
))
244 qRegisterMetaType
<Log::Msg
>("Log::Msg");
245 qRegisterMetaType
<Log::Peer
>("Log::Peer");
247 setApplicationName(u
"qBittorrent"_s
);
248 setOrganizationDomain(u
"qbittorrent.org"_s
);
249 #if !defined(DISABLE_GUI)
250 setDesktopFileName(u
"org.qbittorrent.qBittorrent"_s
);
251 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
252 setAttribute(Qt::AA_UseHighDpiPixmaps
, true); // opt-in to the high DPI pixmap support
254 setQuitOnLastWindowClosed(false);
255 QPixmapCache::setCacheLimit(PIXMAP_CACHE_SIZE
);
258 Logger::initInstance();
260 const auto portableProfilePath
= Path(QCoreApplication::applicationDirPath()) / DEFAULT_PORTABLE_MODE_PROFILE_DIR
;
261 const bool portableModeEnabled
= m_commandLineArgs
.profileDir
.isEmpty() && portableProfilePath
.exists();
263 const Path profileDir
= portableModeEnabled
264 ? portableProfilePath
265 : m_commandLineArgs
.profileDir
;
266 Profile::initInstance(profileDir
, m_commandLineArgs
.configurationName
,
267 (m_commandLineArgs
.relativeFastresumePaths
|| portableModeEnabled
));
269 m_instanceManager
= new ApplicationInstanceManager(Profile::instance()->location(SpecialFolder::Config
), this);
271 SettingsStorage::initInstance();
272 Preferences::initInstance();
274 const bool firstTimeUser
= !Preferences::instance()->getAcceptedLegal();
278 throw RuntimeError(u
"Failed migration of old settings"_s
); // Not translatable. Translation isn't configured yet.
279 handleChangedDefaults(DefaultPreferencesMode::Legacy
);
283 handleChangedDefaults(DefaultPreferencesMode::Current
);
286 initializeTranslation();
288 connect(this, &QCoreApplication::aboutToQuit
, this, &Application::cleanup
);
289 connect(m_instanceManager
, &ApplicationInstanceManager::messageReceived
, this, &Application::processMessage
);
290 #if defined(Q_OS_WIN) && !defined(DISABLE_GUI)
291 connect(this, &QGuiApplication::commitDataRequest
, this, &Application::shutdownCleanup
, Qt::DirectConnection
);
294 LogMsg(tr("qBittorrent %1 started", "qBittorrent v3.2.0alpha started").arg(QStringLiteral(QBT_VERSION
)));
295 if (portableModeEnabled
)
297 LogMsg(tr("Running in portable mode. Auto detected profile folder at: %1").arg(profileDir
.toString()));
298 if (m_commandLineArgs
.relativeFastresumePaths
)
299 LogMsg(tr("Redundant command line flag detected: \"%1\". Portable mode implies relative fastresume.").arg(u
"--relative-fastresume"_s
), Log::WARNING
); // to avoid translating the `--relative-fastresume` string
303 LogMsg(tr("Using config directory: %1").arg(Profile::instance()->location(SpecialFolder::Config
).toString()));
306 if (isFileLoggerEnabled())
307 m_fileLogger
= new FileLogger(fileLoggerPath(), isFileLoggerBackup(), fileLoggerMaxSize(), isFileLoggerDeleteOld(), fileLoggerAge(), static_cast<FileLogger::FileLogAgeType
>(fileLoggerAgeType()));
309 if (m_commandLineArgs
.webUiPort
> 0) // it will be -1 when user did not set any value
310 Preferences::instance()->setWebUiPort(m_commandLineArgs
.webUiPort
);
312 if (m_commandLineArgs
.torrentingPort
> 0) // it will be -1 when user did not set any value
314 SettingValue
<int> port
{u
"BitTorrent/Session/Port"_s
};
315 port
= m_commandLineArgs
.torrentingPort
;
319 Application::~Application()
321 // we still need to call cleanup()
322 // in case the App failed to start
327 DesktopIntegration
*Application::desktopIntegration()
329 return m_desktopIntegration
;
332 MainWindow
*Application::mainWindow()
337 WindowState
Application::startUpWindowState() const
339 return m_startUpWindowState
;
342 void Application::setStartUpWindowState(const WindowState windowState
)
344 m_startUpWindowState
= windowState
;
347 bool Application::isTorrentAddedNotificationsEnabled() const
349 return m_storeNotificationTorrentAdded
;
352 void Application::setTorrentAddedNotificationsEnabled(const bool value
)
354 m_storeNotificationTorrentAdded
= value
;
358 const QBtCommandLineParameters
&Application::commandLineArgs() const
360 return m_commandLineArgs
;
363 int Application::memoryWorkingSetLimit() const
365 return m_storeMemoryWorkingSetLimit
.get(512);
368 void Application::setMemoryWorkingSetLimit(const int size
)
370 if (size
== memoryWorkingSetLimit())
373 m_storeMemoryWorkingSetLimit
= size
;
374 #ifdef QBT_USES_LIBTORRENT2
375 applyMemoryWorkingSetLimit();
379 bool Application::isFileLoggerEnabled() const
381 return m_storeFileLoggerEnabled
.get(true);
384 void Application::setFileLoggerEnabled(const bool value
)
386 if (value
&& !m_fileLogger
)
387 m_fileLogger
= new FileLogger(fileLoggerPath(), isFileLoggerBackup(), fileLoggerMaxSize(), isFileLoggerDeleteOld(), fileLoggerAge(), static_cast<FileLogger::FileLogAgeType
>(fileLoggerAgeType()));
390 m_storeFileLoggerEnabled
= value
;
393 Path
Application::fileLoggerPath() const
395 return m_storeFileLoggerPath
.get(specialFolderLocation(SpecialFolder::Data
) / Path(LOG_FOLDER
));
398 void Application::setFileLoggerPath(const Path
&path
)
401 m_fileLogger
->changePath(path
);
402 m_storeFileLoggerPath
= path
;
405 bool Application::isFileLoggerBackup() const
407 return m_storeFileLoggerBackup
.get(true);
410 void Application::setFileLoggerBackup(const bool value
)
413 m_fileLogger
->setBackup(value
);
414 m_storeFileLoggerBackup
= value
;
417 bool Application::isFileLoggerDeleteOld() const
419 return m_storeFileLoggerDeleteOld
.get(true);
422 void Application::setFileLoggerDeleteOld(const bool value
)
424 if (value
&& m_fileLogger
)
425 m_fileLogger
->deleteOld(fileLoggerAge(), static_cast<FileLogger::FileLogAgeType
>(fileLoggerAgeType()));
426 m_storeFileLoggerDeleteOld
= value
;
429 int Application::fileLoggerMaxSize() const
431 const int val
= m_storeFileLoggerMaxSize
.get(DEFAULT_FILELOG_SIZE
);
432 return std::min(std::max(val
, MIN_FILELOG_SIZE
), MAX_FILELOG_SIZE
);
435 void Application::setFileLoggerMaxSize(const int bytes
)
437 const int clampedValue
= std::min(std::max(bytes
, MIN_FILELOG_SIZE
), MAX_FILELOG_SIZE
);
439 m_fileLogger
->setMaxSize(clampedValue
);
440 m_storeFileLoggerMaxSize
= clampedValue
;
443 int Application::fileLoggerAge() const
445 const int val
= m_storeFileLoggerAge
.get(1);
446 return std::min(std::max(val
, 1), 365);
449 void Application::setFileLoggerAge(const int value
)
451 m_storeFileLoggerAge
= std::min(std::max(value
, 1), 365);
454 int Application::fileLoggerAgeType() const
456 const int val
= m_storeFileLoggerAgeType
.get(1);
457 return ((val
< 0) || (val
> 2)) ? 1 : val
;
460 void Application::setFileLoggerAgeType(const int value
)
462 m_storeFileLoggerAgeType
= ((value
< 0) || (value
> 2)) ? 1 : value
;
465 void Application::processMessage(const QString
&message
)
468 if (message
.isEmpty())
470 // TODO: use [[likely]] in C++20
471 if (Q_LIKELY(BitTorrent::Session::instance()->isRestored()))
473 m_window
->activate(); // show UI
475 else if (m_startupProgressDialog
)
477 m_startupProgressDialog
->show();
478 m_startupProgressDialog
->activateWindow();
479 m_startupProgressDialog
->raise();
483 createStartupProgressDialog();
490 const QBtCommandLineParameters params
= parseParams(message
);
491 // If Application is not allowed to process params immediately
492 // (i.e., other components are not ready) store params
493 if (m_isProcessingParamsAllowed
)
494 processParams(params
);
496 m_paramsQueue
.append(params
);
499 void Application::runExternalProgram(const QString
&programTemplate
, const BitTorrent::Torrent
*torrent
) const
501 // Cannot give users shell environment by default, as doing so could
502 // enable command injection via torrent name and other arguments
503 // (especially when some automated download mechanism has been setup).
504 // See: https://github.com/qbittorrent/qBittorrent/issues/10925
506 const auto replaceVariables
= [torrent
](QString str
) -> QString
508 for (int i
= (str
.length() - 2); i
>= 0; --i
)
513 const ushort specifier
= str
[i
+ 1].unicode();
517 str
.replace(i
, 2, QString::number(torrent
->filesCount()));
520 str
.replace(i
, 2, torrent
->savePath().toString());
523 str
.replace(i
, 2, torrent
->contentPath().toString());
526 str
.replace(i
, 2, torrent
->tags().join(u
","_s
));
529 str
.replace(i
, 2, (torrent
->infoHash().v1().isValid() ? torrent
->infoHash().v1().toString() : u
"-"_s
));
532 str
.replace(i
, 2, (torrent
->infoHash().v2().isValid() ? torrent
->infoHash().v2().toString() : u
"-"_s
));
535 str
.replace(i
, 2, torrent
->id().toString());
538 str
.replace(i
, 2, torrent
->category());
541 str
.replace(i
, 2, torrent
->name());
544 str
.replace(i
, 2, torrent
->rootPath().toString());
547 str
.replace(i
, 2, torrent
->currentTracker());
550 str
.replace(i
, 2, QString::number(torrent
->totalSize()));
557 // decrement `i` to avoid unwanted replacement, example pattern: "%%N"
564 const QString logMsg
= tr("Running external program. Torrent: \"%1\". Command: `%2`");
565 const QString logMsgError
= tr("Failed to run external program. Torrent: \"%1\". Command: `%2`");
567 // The processing sequenece is different for Windows and other OS, this is intentional
568 #if defined(Q_OS_WIN)
569 const QString program
= replaceVariables(programTemplate
);
570 const std::wstring programWStr
= program
.toStdWString();
572 // Need to split arguments manually because QProcess::startDetached(QString)
573 // will strip off empty parameters.
574 // E.g. `python.exe "1" "" "3"` will become `python.exe "1" "3"`
576 std::unique_ptr
<LPWSTR
[], decltype(&::LocalFree
)> args
{::CommandLineToArgvW(programWStr
.c_str(), &argCount
), ::LocalFree
};
582 for (int i
= 1; i
< argCount
; ++i
)
583 argList
+= QString::fromWCharArray(args
[i
]);
586 proc
.setProgram(QString::fromWCharArray(args
[0]));
587 proc
.setArguments(argList
);
588 proc
.setCreateProcessArgumentsModifier([](QProcess::CreateProcessArguments
*args
)
590 if (Preferences::instance()->isAutoRunConsoleEnabled())
592 args
->flags
|= CREATE_NEW_CONSOLE
;
593 args
->flags
&= ~(CREATE_NO_WINDOW
| DETACHED_PROCESS
);
597 args
->flags
|= CREATE_NO_WINDOW
;
598 args
->flags
&= ~(CREATE_NEW_CONSOLE
| DETACHED_PROCESS
);
600 args
->inheritHandles
= false;
601 args
->startupInfo
->dwFlags
&= ~STARTF_USESTDHANDLES
;
602 ::CloseHandle(args
->startupInfo
->hStdInput
);
603 ::CloseHandle(args
->startupInfo
->hStdOutput
);
604 ::CloseHandle(args
->startupInfo
->hStdError
);
605 args
->startupInfo
->hStdInput
= nullptr;
606 args
->startupInfo
->hStdOutput
= nullptr;
607 args
->startupInfo
->hStdError
= nullptr;
610 if (proc
.startDetached())
611 LogMsg(logMsg
.arg(torrent
->name(), program
));
613 LogMsg(logMsgError
.arg(torrent
->name(), program
));
615 QStringList args
= Utils::String::splitCommand(programTemplate
);
620 for (QString
&arg
: args
)
622 // strip redundant quotes
623 if (arg
.startsWith(u
'"') && arg
.endsWith(u
'"'))
624 arg
= arg
.mid(1, (arg
.size() - 2));
626 arg
= replaceVariables(arg
);
629 const QString command
= args
.takeFirst();
631 proc
.setProgram(command
);
632 proc
.setArguments(args
);
634 if (proc
.startDetached())
636 // show intended command in log
637 LogMsg(logMsg
.arg(torrent
->name(), replaceVariables(programTemplate
)));
641 // show intended command in log
642 LogMsg(logMsgError
.arg(torrent
->name(), replaceVariables(programTemplate
)));
647 void Application::sendNotificationEmail(const BitTorrent::Torrent
*torrent
)
649 // Prepare mail content
650 const QString content
= tr("Torrent name: %1").arg(torrent
->name()) + u
'\n'
651 + tr("Torrent size: %1").arg(Utils::Misc::friendlyUnit(torrent
->wantedSize())) + u
'\n'
652 + tr("Save path: %1").arg(torrent
->savePath().toString()) + u
"\n\n"
653 + tr("The torrent was downloaded in %1.", "The torrent was downloaded in 1 hour and 20 seconds")
654 .arg(Utils::Misc::userFriendlyDuration(torrent
->activeTime())) + u
"\n\n\n"
655 + tr("Thank you for using qBittorrent.") + u
'\n';
657 // Send the notification email
658 const Preferences
*pref
= Preferences::instance();
659 auto *smtp
= new Net::Smtp(this);
660 smtp
->sendMail(pref
->getMailNotificationSender(),
661 pref
->getMailNotificationEmail(),
662 tr("Torrent \"%1\" has finished downloading").arg(torrent
->name()),
666 void Application::torrentAdded(const BitTorrent::Torrent
*torrent
) const
668 const Preferences
*pref
= Preferences::instance();
671 if (pref
->isAutoRunOnTorrentAddedEnabled())
672 runExternalProgram(pref
->getAutoRunOnTorrentAddedProgram().trimmed(), torrent
);
675 void Application::torrentFinished(const BitTorrent::Torrent
*torrent
)
677 const Preferences
*pref
= Preferences::instance();
680 if (pref
->isAutoRunOnTorrentFinishedEnabled())
681 runExternalProgram(pref
->getAutoRunOnTorrentFinishedProgram().trimmed(), torrent
);
684 if (pref
->isMailNotificationEnabled())
686 LogMsg(tr("Torrent: %1, sending mail notification").arg(torrent
->name()));
687 sendNotificationEmail(torrent
);
691 void Application::allTorrentsFinished()
693 Preferences
*const pref
= Preferences::instance();
694 bool isExit
= pref
->shutdownqBTWhenDownloadsComplete();
695 bool isShutdown
= pref
->shutdownWhenDownloadsComplete();
696 bool isSuspend
= pref
->suspendWhenDownloadsComplete();
697 bool isHibernate
= pref
->hibernateWhenDownloadsComplete();
699 bool haveAction
= isExit
|| isShutdown
|| isSuspend
|| isHibernate
;
700 if (!haveAction
) return;
702 ShutdownDialogAction action
= ShutdownDialogAction::Exit
;
704 action
= ShutdownDialogAction::Suspend
;
705 else if (isHibernate
)
706 action
= ShutdownDialogAction::Hibernate
;
708 action
= ShutdownDialogAction::Shutdown
;
712 if ((action
== ShutdownDialogAction::Exit
) && (pref
->dontConfirmAutoExit()))
714 // do nothing & skip confirm
718 if (!ShutdownConfirmDialog::askForConfirmation(m_window
, action
)) return;
720 #endif // DISABLE_GUI
722 // Actually shut down
723 if (action
!= ShutdownDialogAction::Exit
)
725 qDebug("Preparing for auto-shutdown because all downloads are complete!");
726 // Disabling it for next time
727 pref
->setShutdownWhenDownloadsComplete(false);
728 pref
->setSuspendWhenDownloadsComplete(false);
729 pref
->setHibernateWhenDownloadsComplete(false);
730 // Make sure preferences are synced before exiting
731 m_shutdownAct
= action
;
734 qDebug("Exiting the application");
738 bool Application::callMainInstance()
740 return m_instanceManager
->sendMessage(serializeParams(commandLineArgs()));
743 void Application::processParams(const QBtCommandLineParameters
¶ms
)
746 // There are two circumstances in which we want to show the torrent
747 // dialog. One is when the application settings specify that it should
748 // be shown and skipTorrentDialog is undefined. The other is when
749 // skipTorrentDialog is false, meaning that the application setting
750 // should be overridden.
751 const bool showDialog
= !params
.skipDialog
.value_or(!AddNewTorrentDialog::isEnabled());
754 for (const QString
&torrentSource
: params
.torrentSources
)
755 AddNewTorrentDialog::show(torrentSource
, params
.addTorrentParams
, m_window
);
760 for (const QString
&torrentSource
: params
.torrentSources
)
761 BitTorrent::Session::instance()->addTorrent(torrentSource
, params
.addTorrentParams
);
765 int Application::exec()
768 #if !defined(DISABLE_WEBUI) && defined(DISABLE_GUI)
769 const QString loadingStr
= tr("WebUI will be started shortly after internal preparations. Please wait...");
770 printf("%s\n", qUtf8Printable(loadingStr
));
773 #ifdef QBT_USES_LIBTORRENT2
774 applyMemoryWorkingSetLimit();
778 applyMemoryPriority();
779 adjustThreadPriority();
782 Net::ProxyConfigurationManager::initInstance();
783 Net::DownloadManager::initInstance();
784 IconProvider::initInstance();
786 BitTorrent::Session::initInstance();
788 UIThemeManager::initInstance();
790 m_desktopIntegration
= new DesktopIntegration
;
791 m_desktopIntegration
->setToolTip(tr("Loading torrents..."));
793 auto *desktopIntegrationMenu
= new QMenu
;
794 auto *actionExit
= new QAction(tr("E&xit"), desktopIntegrationMenu
);
795 actionExit
->setIcon(UIThemeManager::instance()->getIcon(u
"application-exit"_s
));
796 actionExit
->setMenuRole(QAction::QuitRole
);
797 actionExit
->setShortcut(Qt::CTRL
| Qt::Key_Q
);
798 connect(actionExit
, &QAction::triggered
, this, []
800 QApplication::exit();
802 desktopIntegrationMenu
->addAction(actionExit
);
804 m_desktopIntegration
->setMenu(desktopIntegrationMenu
);
806 const bool isHidden
= m_desktopIntegration
->isActive() && (startUpWindowState() == WindowState::Hidden
);
808 const bool isHidden
= false;
813 createStartupProgressDialog();
814 // Add a small delay to avoid "flashing" the progress dialog in case there are not many torrents to restore.
815 m_startupProgressDialog
->setMinimumDuration(1000);
816 if (startUpWindowState() != WindowState::Normal
)
817 m_startupProgressDialog
->setWindowState(Qt::WindowMinimized
);
821 connect(m_desktopIntegration
, &DesktopIntegration::activationRequested
, this, &Application::createStartupProgressDialog
);
824 connect(BitTorrent::Session::instance(), &BitTorrent::Session::restored
, this, [this]()
826 connect(BitTorrent::Session::instance(), &BitTorrent::Session::torrentAdded
, this, &Application::torrentAdded
);
827 connect(BitTorrent::Session::instance(), &BitTorrent::Session::torrentFinished
, this, &Application::torrentFinished
);
828 connect(BitTorrent::Session::instance(), &BitTorrent::Session::allTorrentsFinished
, this, &Application::allTorrentsFinished
, Qt::QueuedConnection
);
830 Net::GeoIPManager::initInstance();
831 TorrentFilesWatcher::initInstance();
833 new RSS::Session
; // create RSS::Session singleton
834 new RSS::AutoDownloader
; // create RSS::AutoDownloader singleton
837 const auto *btSession
= BitTorrent::Session::instance();
838 connect(btSession
, &BitTorrent::Session::fullDiskError
, this
839 , [this](const BitTorrent::Torrent
*torrent
, const QString
&msg
)
841 m_desktopIntegration
->showNotification(tr("I/O Error", "i.e: Input/Output Error")
842 , tr("An I/O error occurred for torrent '%1'.\n Reason: %2"
843 , "e.g: An error occurred for torrent 'xxx.avi'.\n Reason: disk is full.").arg(torrent
->name(), msg
));
845 connect(btSession
, &BitTorrent::Session::loadTorrentFailed
, this
846 , [this](const QString
&error
)
848 m_desktopIntegration
->showNotification(tr("Error"), tr("Failed to add torrent: %1").arg(error
));
850 connect(btSession
, &BitTorrent::Session::torrentAdded
, this
851 , [this](const BitTorrent::Torrent
*torrent
)
853 if (isTorrentAddedNotificationsEnabled())
854 m_desktopIntegration
->showNotification(tr("Torrent added"), tr("'%1' was added.", "e.g: xxx.avi was added.").arg(torrent
->name()));
856 connect(btSession
, &BitTorrent::Session::torrentFinished
, this
857 , [this](const BitTorrent::Torrent
*torrent
)
859 m_desktopIntegration
->showNotification(tr("Download completed"), tr("'%1' has finished downloading.", "e.g: xxx.avi has finished downloading.").arg(torrent
->name()));
861 connect(btSession
, &BitTorrent::Session::downloadFromUrlFailed
, this
862 , [this](const QString
&url
, const QString
&reason
)
864 m_desktopIntegration
->showNotification(tr("URL download error")
865 , tr("Couldn't download file at URL '%1', reason: %2.").arg(url
, reason
));
868 disconnect(m_desktopIntegration
, &DesktopIntegration::activationRequested
, this, &Application::createStartupProgressDialog
);
870 const WindowState windowState
= !m_startupProgressDialog
? WindowState::Hidden
871 : (m_startupProgressDialog
->windowState() & Qt::WindowMinimized
) ? WindowState::Minimized
872 : WindowState::Normal
;
874 const WindowState windowState
= (m_startupProgressDialog
->windowState() & Qt::WindowMinimized
)
875 ? WindowState::Minimized
: WindowState::Normal
;
877 m_window
= new MainWindow(this, windowState
);
878 delete m_startupProgressDialog
;
880 auto *pref
= Preferences::instance();
881 if (!pref
->neverCheckFileAssoc() && (!Preferences::isTorrentFileAssocSet() || !Preferences::isMagnetLinkAssocSet()))
883 if (QMessageBox::question(m_window
, tr("Torrent file association")
884 , tr("qBittorrent is not the default application for opening torrent files or Magnet links.\nDo you want to make qBittorrent the default application for these?")
885 , QMessageBox::Yes
| QMessageBox::No
, QMessageBox::Yes
) == QMessageBox::Yes
)
887 pref
->setTorrentFileAssoc(true);
888 pref
->setMagnetLinkAssoc(true);
892 pref
->setNeverCheckFileAssoc();
896 #endif // DISABLE_GUI
898 #ifndef DISABLE_WEBUI
899 m_webui
= new WebUI(this);
901 if (m_webui
->isErrored())
902 QCoreApplication::exit(EXIT_FAILURE
);
903 connect(m_webui
, &WebUI::fatalError
, this, []() { QCoreApplication::exit(EXIT_FAILURE
); });
905 const Preferences
*pref
= Preferences::instance();
907 const auto scheme
= pref
->isWebUiHttpsEnabled() ? u
"https"_s
: u
"http"_s
;
908 const auto url
= u
"%1://localhost:%2\n"_s
.arg(scheme
, QString::number(pref
->getWebUiPort()));
909 const QString mesg
= u
"\n******** %1 ********\n"_s
.arg(tr("Information"))
910 + tr("To control qBittorrent, access the WebUI at: %1").arg(url
);
911 printf("%s\n", qUtf8Printable(mesg
));
913 if (pref
->getWebUIPassword() == QByteArrayLiteral("ARQ77eY1NUZaQsuDHbIMCA==:0WMRkYTUWVT9wVvdDtHAjU9b3b7uB8NR1Gur2hmQCvCDpm39Q+PsJRJPaCU51dEiz+dTzh8qbPsL8WkFljQYFQ=="))
915 const QString warning
= tr("The Web UI administrator username is: %1").arg(pref
->getWebUiUsername()) + u
'\n'
916 + tr("The Web UI administrator password has not been changed from the default: %1").arg(u
"adminadmin"_s
) + u
'\n'
917 + tr("This is a security risk, please change your password in program preferences.") + u
'\n';
918 printf("%s", qUtf8Printable(warning
));
920 #endif // DISABLE_GUI
921 #endif // DISABLE_WEBUI
923 m_isProcessingParamsAllowed
= true;
924 for (const QBtCommandLineParameters
¶ms
: m_paramsQueue
)
925 processParams(params
);
926 m_paramsQueue
.clear();
929 const QBtCommandLineParameters params
= commandLineArgs();
930 if (!params
.torrentSources
.isEmpty())
931 m_paramsQueue
.append(params
);
933 return BaseApplication::exec();
935 catch (const RuntimeError
&err
)
938 fprintf(stderr
, "%s", qPrintable(err
.message()));
941 msgBox
.setIcon(QMessageBox::Critical
);
942 msgBox
.setText(QCoreApplication::translate("Application", "Application failed to start."));
943 msgBox
.setInformativeText(err
.message());
944 msgBox
.show(); // Need to be shown or to moveToCenter does not work
945 msgBox
.move(Utils::Gui::screenCenter(&msgBox
));
951 bool Application::isRunning()
953 return !m_instanceManager
->isFirstInstance();
957 void Application::createStartupProgressDialog()
959 Q_ASSERT(!m_startupProgressDialog
);
960 Q_ASSERT(m_desktopIntegration
);
962 disconnect(m_desktopIntegration
, &DesktopIntegration::activationRequested
, this, &Application::createStartupProgressDialog
);
964 m_startupProgressDialog
= new QProgressDialog(tr("Loading torrents..."), tr("Exit"), 0, 100);
965 m_startupProgressDialog
->setAttribute(Qt::WA_DeleteOnClose
);
966 m_startupProgressDialog
->setWindowFlag(Qt::WindowMinimizeButtonHint
);
967 m_startupProgressDialog
->setMinimumDuration(0); // Show dialog immediately by default
968 m_startupProgressDialog
->setAutoReset(false);
969 m_startupProgressDialog
->setAutoClose(false);
971 connect(m_startupProgressDialog
, &QProgressDialog::canceled
, this, []()
973 QApplication::exit();
976 connect(BitTorrent::Session::instance(), &BitTorrent::Session::startupProgressUpdated
, m_startupProgressDialog
, &QProgressDialog::setValue
);
978 connect(m_desktopIntegration
, &DesktopIntegration::activationRequested
, m_startupProgressDialog
, [this]()
981 if (!m_startupProgressDialog
->isVisible())
983 m_startupProgressDialog
->show();
984 m_startupProgressDialog
->activateWindow();
985 m_startupProgressDialog
->raise();
988 if (m_startupProgressDialog
->isHidden())
990 // Make sure the window is not minimized
991 m_startupProgressDialog
->setWindowState((m_startupProgressDialog
->windowState() & ~Qt::WindowMinimized
) | Qt::WindowActive
);
994 m_startupProgressDialog
->show();
995 m_startupProgressDialog
->raise();
996 m_startupProgressDialog
->activateWindow();
1000 m_startupProgressDialog
->hide();
1007 bool Application::event(QEvent
*ev
)
1009 if (ev
->type() == QEvent::FileOpen
)
1011 QString path
= static_cast<QFileOpenEvent
*>(ev
)->file();
1013 // Get the url instead
1014 path
= static_cast<QFileOpenEvent
*>(ev
)->url().toString();
1015 qDebug("Received a mac file open event: %s", qUtf8Printable(path
));
1017 QBtCommandLineParameters params
;
1018 params
.torrentSources
.append(path
);
1019 // If Application is not allowed to process params immediately
1020 // (i.e., other components are not ready) store params
1021 if (m_isProcessingParamsAllowed
)
1022 processParams(params
);
1024 m_paramsQueue
.append(params
);
1029 return BaseApplication::event(ev
);
1031 #endif // Q_OS_MACOS
1032 #endif // DISABLE_GUI
1034 void Application::initializeTranslation()
1036 Preferences
*const pref
= Preferences::instance();
1038 const QString localeStr
= pref
->getLocale();
1040 if (m_qtTranslator
.load((u
"qtbase_" + localeStr
), QLibraryInfo::location(QLibraryInfo::TranslationsPath
)) ||
1041 m_qtTranslator
.load((u
"qt_" + localeStr
), QLibraryInfo::location(QLibraryInfo::TranslationsPath
)))
1042 qDebug("Qt %s locale recognized, using translation.", qUtf8Printable(localeStr
));
1044 qDebug("Qt %s locale unrecognized, using default (en).", qUtf8Printable(localeStr
));
1046 installTranslator(&m_qtTranslator
);
1048 if (m_translator
.load(u
":/lang/qbittorrent_" + localeStr
))
1049 qDebug("%s locale recognized, using translation.", qUtf8Printable(localeStr
));
1051 qDebug("%s locale unrecognized, using default (en).", qUtf8Printable(localeStr
));
1052 installTranslator(&m_translator
);
1055 if (localeStr
.startsWith(u
"ar") || localeStr
.startsWith(u
"he"))
1057 qDebug("Right to Left mode");
1058 setLayoutDirection(Qt::RightToLeft
);
1062 setLayoutDirection(Qt::LeftToRight
);
1067 #if (!defined(DISABLE_GUI) && defined(Q_OS_WIN))
1068 void Application::shutdownCleanup(QSessionManager
&manager
)
1072 // This is only needed for a special case on Windows XP.
1073 // (but is called for every Windows version)
1074 // If a process takes too much time to exit during OS
1075 // shutdown, the OS presents a dialog to the user.
1076 // That dialog tells the user that qbt is blocking the
1077 // shutdown, it shows a progress bar and it offers
1078 // a "Terminate Now" button for the user. However,
1079 // after the progress bar has reached 100% another button
1080 // is offered to the user reading "Cancel". With this the
1081 // user can cancel the **OS** shutdown. If we don't do
1082 // the cleanup by handling the commitDataRequest() signal
1083 // and the user clicks "Cancel", it will result in qbt being
1084 // killed and the shutdown proceeding instead. Apparently
1085 // aboutToQuit() is emitted too late in the shutdown process.
1088 // According to the qt docs we shouldn't call quit() inside a slot.
1089 // aboutToQuit() is never emitted if the user hits "Cancel" in
1090 // the above dialog.
1091 QMetaObject::invokeMethod(qApp
, &QCoreApplication::quit
, Qt::QueuedConnection
);
1095 #ifdef QBT_USES_LIBTORRENT2
1096 void Application::applyMemoryWorkingSetLimit() const
1098 const size_t MiB
= 1024 * 1024;
1099 const QString logMessage
= tr("Failed to set physical memory (RAM) usage limit. Error code: %1. Error message: \"%2\"");
1102 const SIZE_T maxSize
= memoryWorkingSetLimit() * MiB
;
1103 const auto minSize
= std::min
<SIZE_T
>((64 * MiB
), (maxSize
/ 2));
1104 if (!::SetProcessWorkingSetSizeEx(::GetCurrentProcess(), minSize
, maxSize
, QUOTA_LIMITS_HARDWS_MAX_ENABLE
))
1106 const DWORD errorCode
= ::GetLastError();
1108 LPVOID lpMsgBuf
= nullptr;
1109 const DWORD msgLength
= ::FormatMessageW((FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_IGNORE_INSERTS
)
1110 , nullptr, errorCode
, LANG_USER_DEFAULT
, reinterpret_cast<LPWSTR
>(&lpMsgBuf
), 0, nullptr);
1113 message
= QString::fromWCharArray(reinterpret_cast<LPWSTR
>(lpMsgBuf
)).trimmed();
1114 ::LocalFree(lpMsgBuf
);
1116 LogMsg(logMessage
.arg(QString::number(errorCode
), message
), Log::WARNING
);
1118 #elif defined(Q_OS_UNIX)
1119 // has no effect on linux but it might be meaningful for other OS
1122 if (::getrlimit(RLIMIT_RSS
, &limit
) != 0)
1125 const size_t newSize
= memoryWorkingSetLimit() * MiB
;
1126 if (newSize
> limit
.rlim_max
)
1128 // try to raise the hard limit
1129 rlimit newLimit
= limit
;
1130 newLimit
.rlim_max
= newSize
;
1131 if (::setrlimit(RLIMIT_RSS
, &newLimit
) != 0)
1133 const auto message
= QString::fromLocal8Bit(strerror(errno
));
1134 LogMsg(tr("Failed to set physical memory (RAM) usage hard limit. Requested size: %1. System hard limit: %2. Error code: %3. Error message: \"%4\"")
1135 .arg(QString::number(newSize
), QString::number(limit
.rlim_max
), QString::number(errno
), message
), Log::WARNING
);
1140 limit
.rlim_cur
= newSize
;
1141 if (::setrlimit(RLIMIT_RSS
, &limit
) != 0)
1143 const auto message
= QString::fromLocal8Bit(strerror(errno
));
1144 LogMsg(logMessage
.arg(QString::number(errno
), message
), Log::WARNING
);
1151 MemoryPriority
Application::processMemoryPriority() const
1153 return m_processMemoryPriority
.get(MemoryPriority::BelowNormal
);
1156 void Application::setProcessMemoryPriority(const MemoryPriority priority
)
1158 if (processMemoryPriority() == priority
)
1161 m_processMemoryPriority
= priority
;
1162 applyMemoryPriority();
1165 void Application::applyMemoryPriority() const
1167 using SETPROCESSINFORMATION
= BOOL (WINAPI
*)(HANDLE
, PROCESS_INFORMATION_CLASS
, LPVOID
, DWORD
);
1168 const auto setProcessInformation
= Utils::Misc::loadWinAPI
<SETPROCESSINFORMATION
>(u
"Kernel32.dll"_s
, "SetProcessInformation");
1169 if (!setProcessInformation
) // only available on Windows >= 8
1172 using SETTHREADINFORMATION
= BOOL (WINAPI
*)(HANDLE
, THREAD_INFORMATION_CLASS
, LPVOID
, DWORD
);
1173 const auto setThreadInformation
= Utils::Misc::loadWinAPI
<SETTHREADINFORMATION
>(u
"Kernel32.dll"_s
, "SetThreadInformation");
1174 if (!setThreadInformation
) // only available on Windows >= 8
1177 #if (_WIN32_WINNT < _WIN32_WINNT_WIN8)
1178 // this dummy struct is required to compile successfully when targeting older Windows version
1179 struct MEMORY_PRIORITY_INFORMATION
1181 ULONG MemoryPriority
;
1184 #define MEMORY_PRIORITY_LOWEST 0
1185 #define MEMORY_PRIORITY_VERY_LOW 1
1186 #define MEMORY_PRIORITY_LOW 2
1187 #define MEMORY_PRIORITY_MEDIUM 3
1188 #define MEMORY_PRIORITY_BELOW_NORMAL 4
1189 #define MEMORY_PRIORITY_NORMAL 5
1192 MEMORY_PRIORITY_INFORMATION prioInfo
{};
1193 switch (processMemoryPriority())
1195 case MemoryPriority::Normal
:
1197 prioInfo
.MemoryPriority
= MEMORY_PRIORITY_NORMAL
;
1199 case MemoryPriority::BelowNormal
:
1200 prioInfo
.MemoryPriority
= MEMORY_PRIORITY_BELOW_NORMAL
;
1202 case MemoryPriority::Medium
:
1203 prioInfo
.MemoryPriority
= MEMORY_PRIORITY_MEDIUM
;
1205 case MemoryPriority::Low
:
1206 prioInfo
.MemoryPriority
= MEMORY_PRIORITY_LOW
;
1208 case MemoryPriority::VeryLow
:
1209 prioInfo
.MemoryPriority
= MEMORY_PRIORITY_VERY_LOW
;
1212 setProcessInformation(::GetCurrentProcess(), ProcessMemoryPriority
, &prioInfo
, sizeof(prioInfo
));
1214 // To avoid thrashing/sluggishness of the app, set "main event loop" thread to normal memory priority
1215 // which is higher/equal than other threads
1216 prioInfo
.MemoryPriority
= MEMORY_PRIORITY_NORMAL
;
1217 setThreadInformation(::GetCurrentThread(), ThreadMemoryPriority
, &prioInfo
, sizeof(prioInfo
));
1220 void Application::adjustThreadPriority() const
1222 // Workaround for improving responsiveness of qbt when CPU resources are scarce.
1223 // Raise main event loop thread to be just one level higher than libtorrent threads.
1224 // Also note that on *nix platforms there is no easy way to achieve it,
1225 // so implementation is omitted.
1227 ::SetThreadPriority(::GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL
);
1231 void Application::cleanup()
1233 // cleanup() can be called multiple times during shutdown. We only need it once.
1234 if (!m_isCleanupRun
.testAndSetAcquire(0, 1))
1237 LogMsg(tr("qBittorrent termination initiated"));
1240 if (m_desktopIntegration
)
1242 m_desktopIntegration
->disconnect();
1243 m_desktopIntegration
->setToolTip(tr("qBittorrent is shutting down..."));
1244 if (m_desktopIntegration
->menu())
1245 m_desktopIntegration
->menu()->setEnabled(false);
1250 // Hide the window and don't leave it on screen as
1251 // unresponsive. Also for Windows take the WinId
1252 // after it's hidden, because hide() may cause a
1257 const std::wstring msg
= tr("Saving torrent progress...").toStdWString();
1258 ::ShutdownBlockReasonCreate(reinterpret_cast<HWND
>(m_window
->effectiveWinId())
1262 // Do manual cleanup in MainWindow to force widgets
1263 // to save their Preferences, stop all timers and
1264 // delete as many widgets as possible to leave only
1265 // a 'shell' MainWindow.
1266 // We need a valid window handle for Windows Vista+
1267 // otherwise the system shutdown will continue even
1268 // though we created a ShutdownBlockReason
1269 m_window
->cleanup();
1271 #endif // DISABLE_GUI
1273 #ifndef DISABLE_WEBUI
1277 delete RSS::AutoDownloader::instance();
1278 delete RSS::Session::instance();
1280 TorrentFilesWatcher::freeInstance();
1281 BitTorrent::Session::freeInstance();
1282 Net::GeoIPManager::freeInstance();
1283 Net::DownloadManager::freeInstance();
1284 Net::ProxyConfigurationManager::freeInstance();
1285 Preferences::freeInstance();
1286 SettingsStorage::freeInstance();
1287 IconProvider::freeInstance();
1288 SearchPluginManager::freeInstance();
1289 Utils::Fs::removeDirRecursively(Utils::Fs::tempPath());
1291 LogMsg(tr("qBittorrent is now ready to exit"));
1292 Logger::freeInstance();
1293 delete m_fileLogger
;
1299 ::ShutdownBlockReasonDestroy(reinterpret_cast<HWND
>(m_window
->effectiveWinId()));
1302 delete m_desktopIntegration
;
1303 UIThemeManager::freeInstance();
1305 #endif // DISABLE_GUI
1307 Profile::freeInstance();
1309 if (m_shutdownAct
!= ShutdownDialogAction::Exit
)
1311 qDebug() << "Sending computer shutdown/suspend/hibernate signal...";
1312 Utils::Misc::shutdownComputer(m_shutdownAct
);