-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
application.cpp
1406 lines (1210 loc) · 48.7 KB
/
application.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2015-2024 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2006 Christophe Dumez
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
#include "application.h"
#include <algorithm>
#ifdef DISABLE_GUI
#include <cstdio>
#endif
#ifdef Q_OS_WIN
#include <memory>
#include <windows.h>
#include <shellapi.h>
#elif defined(Q_OS_UNIX)
#include <sys/resource.h>
#endif
#include <QByteArray>
#include <QDebug>
#include <QLibraryInfo>
#include <QMetaObject>
#include <QProcess>
#ifndef DISABLE_GUI
#include <QAbstractButton>
#include <QMenu>
#include <QMessageBox>
#include <QPixmapCache>
#include <QProgressDialog>
#ifdef Q_OS_WIN
#include <QSessionManager>
#endif // Q_OS_WIN
#ifdef Q_OS_MACOS
#include <QFileOpenEvent>
#endif // Q_OS_MACOS
#endif
#include "base/addtorrentmanager.h"
#include "base/bittorrent/infohash.h"
#include "base/bittorrent/session.h"
#include "base/bittorrent/torrent.h"
#include "base/exceptions.h"
#include "base/global.h"
#include "base/logger.h"
#include "base/net/downloadmanager.h"
#include "base/net/geoipmanager.h"
#include "base/net/proxyconfigurationmanager.h"
#include "base/net/smtp.h"
#include "base/preferences.h"
#include "base/profile.h"
#include "base/rss/rss_autodownloader.h"
#include "base/rss/rss_session.h"
#include "base/search/searchpluginmanager.h"
#include "base/settingsstorage.h"
#include "base/torrentfileswatcher.h"
#include "base/utils/fs.h"
#include "base/utils/misc.h"
#include "base/utils/os.h"
#include "base/utils/string.h"
#include "base/version.h"
#include "applicationinstancemanager.h"
#include "filelogger.h"
#include "upgrade.h"
#ifndef DISABLE_GUI
#include "gui/desktopintegration.h"
#include "gui/mainwindow.h"
#include "gui/shutdownconfirmdialog.h"
#include "gui/uithememanager.h"
#include "gui/windowstate.h"
#endif // DISABLE_GUI
#ifndef DISABLE_WEBUI
#include "webui/webui.h"
#ifdef DISABLE_GUI
#include "base/utils/password.h"
#endif
#endif
namespace
{
#define SETTINGS_KEY(name) u"Application/" name
#define FILELOGGER_SETTINGS_KEY(name) (SETTINGS_KEY(u"FileLogger/") name)
#define NOTIFICATIONS_SETTINGS_KEY(name) (SETTINGS_KEY(u"GUI/Notifications/"_s) name)
const QString LOG_FOLDER = u"logs"_s;
const QChar PARAMS_SEPARATOR = u'|';
const Path DEFAULT_PORTABLE_MODE_PROFILE_DIR {u"profile"_s};
const int MIN_FILELOG_SIZE = 1024; // 1KiB
const int MAX_FILELOG_SIZE = 1000 * 1024 * 1024; // 1000MiB
const int DEFAULT_FILELOG_SIZE = 65 * 1024; // 65KiB
#ifndef DISABLE_GUI
const int PIXMAP_CACHE_SIZE = 64 * 1024 * 1024; // 64MiB
#endif
QString serializeParams(const QBtCommandLineParameters ¶ms)
{
QStringList result;
// Because we're passing a string list to the currently running
// qBittorrent process, we need some way of passing along the options
// the user has specified. Here we place special strings that are
// almost certainly not going to collide with a file path or URL
// specified by the user, and placing them at the beginning of the
// string list so that they will be processed before the list of
// torrent paths or URLs.
const BitTorrent::AddTorrentParams &addTorrentParams = params.addTorrentParams;
if (!addTorrentParams.savePath.isEmpty())
result.append(u"@savePath=" + addTorrentParams.savePath.data());
if (addTorrentParams.addStopped.has_value())
result.append(*addTorrentParams.addStopped ? u"@addStopped=1"_s : u"@addStopped=0"_s);
if (addTorrentParams.skipChecking)
result.append(u"@skipChecking"_s);
if (!addTorrentParams.category.isEmpty())
result.append(u"@category=" + addTorrentParams.category);
if (addTorrentParams.sequential)
result.append(u"@sequential"_s);
if (addTorrentParams.firstLastPiecePriority)
result.append(u"@firstLastPiecePriority"_s);
if (params.skipDialog.has_value())
result.append(*params.skipDialog ? u"@skipDialog=1"_s : u"@skipDialog=0"_s);
result += params.torrentSources;
return result.join(PARAMS_SEPARATOR);
}
QBtCommandLineParameters parseParams(const QString &str)
{
QBtCommandLineParameters parsedParams;
BitTorrent::AddTorrentParams &addTorrentParams = parsedParams.addTorrentParams;
for (QString param : asConst(str.split(PARAMS_SEPARATOR, Qt::SkipEmptyParts)))
{
param = param.trimmed();
// Process strings indicating options specified by the user.
if (param.startsWith(u"@savePath="))
{
addTorrentParams.savePath = Path(param.mid(10));
continue;
}
if (param.startsWith(u"@addStopped="))
{
addTorrentParams.addStopped = (QStringView(param).mid(11).toInt() != 0);
continue;
}
if (param == u"@skipChecking")
{
addTorrentParams.skipChecking = true;
continue;
}
if (param.startsWith(u"@category="))
{
addTorrentParams.category = param.mid(10);
continue;
}
if (param == u"@sequential")
{
addTorrentParams.sequential = true;
continue;
}
if (param == u"@firstLastPiecePriority")
{
addTorrentParams.firstLastPiecePriority = true;
continue;
}
if (param.startsWith(u"@skipDialog="))
{
parsedParams.skipDialog = (QStringView(param).mid(12).toInt() != 0);
continue;
}
parsedParams.torrentSources.append(param);
}
return parsedParams;
}
}
Application::Application(int &argc, char **argv)
: BaseApplication(argc, argv)
, m_commandLineArgs(parseCommandLine(Application::arguments()))
, m_storeInstanceName(SETTINGS_KEY(u"InstanceName"_s))
, m_storeFileLoggerEnabled(FILELOGGER_SETTINGS_KEY(u"Enabled"_s))
, m_storeFileLoggerBackup(FILELOGGER_SETTINGS_KEY(u"Backup"_s))
, m_storeFileLoggerDeleteOld(FILELOGGER_SETTINGS_KEY(u"DeleteOld"_s))
, m_storeFileLoggerMaxSize(FILELOGGER_SETTINGS_KEY(u"MaxSizeBytes"_s))
, m_storeFileLoggerAge(FILELOGGER_SETTINGS_KEY(u"Age"_s))
, m_storeFileLoggerAgeType(FILELOGGER_SETTINGS_KEY(u"AgeType"_s))
, m_storeFileLoggerPath(FILELOGGER_SETTINGS_KEY(u"Path"_s))
, m_storeMemoryWorkingSetLimit(SETTINGS_KEY(u"MemoryWorkingSetLimit"_s))
#ifdef Q_OS_WIN
, m_processMemoryPriority(SETTINGS_KEY(u"ProcessMemoryPriority"_s))
#endif
#ifndef DISABLE_GUI
, m_startUpWindowState(u"GUI/StartUpWindowState"_s)
, m_storeNotificationTorrentAdded(NOTIFICATIONS_SETTINGS_KEY(u"TorrentAdded"_s))
#endif
{
qRegisterMetaType<Log::Msg>("Log::Msg");
qRegisterMetaType<Log::Peer>("Log::Peer");
setApplicationName(u"qBittorrent"_s);
setOrganizationDomain(u"qbittorrent.org"_s);
#if !defined(DISABLE_GUI)
setDesktopFileName(u"org.qbittorrent.qBittorrent"_s);
setQuitOnLastWindowClosed(false);
setQuitLockEnabled(false);
QPixmapCache::setCacheLimit(PIXMAP_CACHE_SIZE);
#endif
Logger::initInstance();
const auto portableProfilePath = Path(QCoreApplication::applicationDirPath()) / DEFAULT_PORTABLE_MODE_PROFILE_DIR;
const bool portableModeEnabled = m_commandLineArgs.profileDir.isEmpty() && Utils::Fs::isDir(portableProfilePath);
const Path profileDir = portableModeEnabled ? portableProfilePath : m_commandLineArgs.profileDir;
Profile::initInstance(profileDir, m_commandLineArgs.configurationName,
(m_commandLineArgs.relativeFastresumePaths || portableModeEnabled));
m_instanceManager = new ApplicationInstanceManager(Profile::instance()->location(SpecialFolder::Config), this);
SettingsStorage::initInstance();
Preferences::initInstance();
const bool firstTimeUser = SettingsStorage::instance()->isEmpty();
if (firstTimeUser)
{
setCurrentMigrationVersion();
handleChangedDefaults(DefaultPreferencesMode::Current);
}
else
{
if (!upgrade())
throw RuntimeError(u"Failed migration of old settings"_s); // Not translatable. Translation isn't configured yet.
handleChangedDefaults(DefaultPreferencesMode::Legacy);
}
initializeTranslation();
connect(this, &QCoreApplication::aboutToQuit, this, &Application::cleanup);
connect(m_instanceManager, &ApplicationInstanceManager::messageReceived, this, &Application::processMessage);
#if defined(Q_OS_WIN) && !defined(DISABLE_GUI)
connect(this, &QGuiApplication::commitDataRequest, this, &Application::shutdownCleanup, Qt::DirectConnection);
#endif
LogMsg(tr("qBittorrent %1 started. Process ID: %2", "qBittorrent v3.2.0alpha started")
.arg(QStringLiteral(QBT_VERSION), QString::number(QCoreApplication::applicationPid())));
if (portableModeEnabled)
{
LogMsg(tr("Running in portable mode. Auto detected profile folder at: %1").arg(profileDir.toString()));
if (m_commandLineArgs.relativeFastresumePaths)
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
}
else
{
LogMsg(tr("Using config directory: %1").arg(Profile::instance()->location(SpecialFolder::Config).toString()));
}
if (isFileLoggerEnabled())
m_fileLogger = new FileLogger(fileLoggerPath(), isFileLoggerBackup(), fileLoggerMaxSize(), isFileLoggerDeleteOld(), fileLoggerAge(), static_cast<FileLogger::FileLogAgeType>(fileLoggerAgeType()));
if (m_commandLineArgs.webUIPort > 0) // it will be -1 when user did not set any value
Preferences::instance()->setWebUIPort(m_commandLineArgs.webUIPort);
if (m_commandLineArgs.torrentingPort > 0) // it will be -1 when user did not set any value
{
SettingValue<int> port {u"BitTorrent/Session/Port"_s};
port = m_commandLineArgs.torrentingPort;
}
}
Application::~Application()
{
// we still need to call cleanup()
// in case the App failed to start
cleanup();
}
#ifndef DISABLE_GUI
DesktopIntegration *Application::desktopIntegration()
{
return m_desktopIntegration;
}
MainWindow *Application::mainWindow()
{
return m_window;
}
WindowState Application::startUpWindowState() const
{
return m_startUpWindowState;
}
void Application::setStartUpWindowState(const WindowState windowState)
{
m_startUpWindowState = windowState;
}
bool Application::isTorrentAddedNotificationsEnabled() const
{
return m_storeNotificationTorrentAdded;
}
void Application::setTorrentAddedNotificationsEnabled(const bool value)
{
m_storeNotificationTorrentAdded = value;
}
#endif
const QBtCommandLineParameters &Application::commandLineArgs() const
{
return m_commandLineArgs;
}
QString Application::instanceName() const
{
return m_storeInstanceName;
}
void Application::setInstanceName(const QString &name)
{
if (name == instanceName())
return;
m_storeInstanceName = name;
#ifndef DISABLE_GUI
if (MainWindow *mw = mainWindow())
mw->setTitleSuffix(name);
#endif
}
int Application::memoryWorkingSetLimit() const
{
return m_storeMemoryWorkingSetLimit.get(512);
}
void Application::setMemoryWorkingSetLimit(const int size)
{
if (size == memoryWorkingSetLimit())
return;
m_storeMemoryWorkingSetLimit = size;
#if defined(QBT_USES_LIBTORRENT2) && !defined(Q_OS_MACOS)
applyMemoryWorkingSetLimit();
#endif
}
bool Application::isFileLoggerEnabled() const
{
return m_storeFileLoggerEnabled.get(true);
}
void Application::setFileLoggerEnabled(const bool value)
{
if (value && !m_fileLogger)
m_fileLogger = new FileLogger(fileLoggerPath(), isFileLoggerBackup(), fileLoggerMaxSize(), isFileLoggerDeleteOld(), fileLoggerAge(), static_cast<FileLogger::FileLogAgeType>(fileLoggerAgeType()));
else if (!value)
delete m_fileLogger;
m_storeFileLoggerEnabled = value;
}
Path Application::fileLoggerPath() const
{
return m_storeFileLoggerPath.get(specialFolderLocation(SpecialFolder::Data) / Path(LOG_FOLDER));
}
void Application::setFileLoggerPath(const Path &path)
{
if (m_fileLogger)
m_fileLogger->changePath(path);
m_storeFileLoggerPath = path;
}
bool Application::isFileLoggerBackup() const
{
return m_storeFileLoggerBackup.get(true);
}
void Application::setFileLoggerBackup(const bool value)
{
if (m_fileLogger)
m_fileLogger->setBackup(value);
m_storeFileLoggerBackup = value;
}
bool Application::isFileLoggerDeleteOld() const
{
return m_storeFileLoggerDeleteOld.get(true);
}
void Application::setFileLoggerDeleteOld(const bool value)
{
if (value && m_fileLogger)
m_fileLogger->deleteOld(fileLoggerAge(), static_cast<FileLogger::FileLogAgeType>(fileLoggerAgeType()));
m_storeFileLoggerDeleteOld = value;
}
int Application::fileLoggerMaxSize() const
{
const int val = m_storeFileLoggerMaxSize.get(DEFAULT_FILELOG_SIZE);
return std::clamp(val, MIN_FILELOG_SIZE, MAX_FILELOG_SIZE);
}
void Application::setFileLoggerMaxSize(const int bytes)
{
const int clampedValue = std::clamp(bytes, MIN_FILELOG_SIZE, MAX_FILELOG_SIZE);
if (m_fileLogger)
m_fileLogger->setMaxSize(clampedValue);
m_storeFileLoggerMaxSize = clampedValue;
}
int Application::fileLoggerAge() const
{
const int val = m_storeFileLoggerAge.get(1);
return std::clamp(val, 1, 365);
}
void Application::setFileLoggerAge(const int value)
{
m_storeFileLoggerAge = std::clamp(value, 1, 365);
}
int Application::fileLoggerAgeType() const
{
const int val = m_storeFileLoggerAgeType.get(1);
return ((val < 0) || (val > 2)) ? 1 : val;
}
void Application::setFileLoggerAgeType(const int value)
{
m_storeFileLoggerAgeType = ((value < 0) || (value > 2)) ? 1 : value;
}
void Application::processMessage(const QString &message)
{
#ifndef DISABLE_GUI
if (message.isEmpty())
{
if (BitTorrent::Session::instance()->isRestored()) [[likely]]
{
m_window->activate(); // show UI
}
else if (m_startupProgressDialog)
{
m_startupProgressDialog->show();
m_startupProgressDialog->activateWindow();
m_startupProgressDialog->raise();
}
else
{
createStartupProgressDialog();
}
return;
}
#endif
const QBtCommandLineParameters params = parseParams(message);
// If Application is not allowed to process params immediately
// (i.e., other components are not ready) store params
if (m_isProcessingParamsAllowed)
processParams(params);
else
m_paramsQueue.append(params);
}
void Application::runExternalProgram(const QString &programTemplate, const BitTorrent::Torrent *torrent) const
{
// Cannot give users shell environment by default, as doing so could
// enable command injection via torrent name and other arguments
// (especially when some automated download mechanism has been setup).
// See: https://github.com/qbittorrent/qBittorrent/issues/10925
const auto replaceVariables = [torrent](QString str) -> QString
{
for (int i = (str.length() - 2); i >= 0; --i)
{
if (str[i] != u'%')
continue;
const ushort specifier = str[i + 1].unicode();
switch (specifier)
{
case u'C':
str.replace(i, 2, QString::number(torrent->filesCount()));
break;
case u'D':
str.replace(i, 2, torrent->savePath().toString());
break;
case u'F':
str.replace(i, 2, torrent->contentPath().toString());
break;
case u'G':
str.replace(i, 2, Utils::String::joinIntoString(torrent->tags(), u","_s));
break;
case u'I':
str.replace(i, 2, (torrent->infoHash().v1().isValid() ? torrent->infoHash().v1().toString() : u"-"_s));
break;
case u'J':
str.replace(i, 2, (torrent->infoHash().v2().isValid() ? torrent->infoHash().v2().toString() : u"-"_s));
break;
case u'K':
str.replace(i, 2, torrent->id().toString());
break;
case u'L':
str.replace(i, 2, torrent->category());
break;
case u'N':
str.replace(i, 2, torrent->name());
break;
case u'R':
str.replace(i, 2, torrent->rootPath().toString());
break;
case u'T':
str.replace(i, 2, torrent->currentTracker());
break;
case u'Z':
str.replace(i, 2, QString::number(torrent->totalSize()));
break;
default:
// do nothing
break;
}
// decrement `i` to avoid unwanted replacement, example pattern: "%%N"
--i;
}
return str;
};
const QString logMsg = tr("Running external program. Torrent: \"%1\". Command: `%2`");
const QString logMsgError = tr("Failed to run external program. Torrent: \"%1\". Command: `%2`");
// The processing sequenece is different for Windows and other OS, this is intentional
#if defined(Q_OS_WIN)
const QString program = replaceVariables(programTemplate);
const std::wstring programWStr = program.toStdWString();
// Need to split arguments manually because QProcess::startDetached(QString)
// will strip off empty parameters.
// E.g. `python.exe "1" "" "3"` will become `python.exe "1" "3"`
int argCount = 0;
std::unique_ptr<LPWSTR[], decltype(&::LocalFree)> args {::CommandLineToArgvW(programWStr.c_str(), &argCount), ::LocalFree};
if (argCount <= 0)
return;
QStringList argList;
for (int i = 1; i < argCount; ++i)
argList += QString::fromWCharArray(args[i]);
QProcess proc;
proc.setProgram(QString::fromWCharArray(args[0]));
proc.setArguments(argList);
proc.setCreateProcessArgumentsModifier([](QProcess::CreateProcessArguments *args)
{
if (Preferences::instance()->isAutoRunConsoleEnabled())
{
args->flags |= CREATE_NEW_CONSOLE;
args->flags &= ~(CREATE_NO_WINDOW | DETACHED_PROCESS);
}
else
{
args->flags |= CREATE_NO_WINDOW;
args->flags &= ~(CREATE_NEW_CONSOLE | DETACHED_PROCESS);
}
args->inheritHandles = false;
args->startupInfo->dwFlags &= ~STARTF_USESTDHANDLES;
::CloseHandle(args->startupInfo->hStdInput);
::CloseHandle(args->startupInfo->hStdOutput);
::CloseHandle(args->startupInfo->hStdError);
args->startupInfo->hStdInput = nullptr;
args->startupInfo->hStdOutput = nullptr;
args->startupInfo->hStdError = nullptr;
});
if (proc.startDetached())
LogMsg(logMsg.arg(torrent->name(), program));
else
LogMsg(logMsgError.arg(torrent->name(), program));
#else // Q_OS_WIN
QStringList args = Utils::String::splitCommand(programTemplate);
if (args.isEmpty())
return;
for (QString &arg : args)
{
// strip redundant quotes
if (arg.startsWith(u'"') && arg.endsWith(u'"'))
arg = arg.mid(1, (arg.size() - 2));
arg = replaceVariables(arg);
}
const QString command = args.takeFirst();
QProcess proc;
proc.setProgram(command);
proc.setArguments(args);
if (proc.startDetached())
{
// show intended command in log
LogMsg(logMsg.arg(torrent->name(), replaceVariables(programTemplate)));
}
else
{
// show intended command in log
LogMsg(logMsgError.arg(torrent->name(), replaceVariables(programTemplate)));
}
#endif
}
void Application::sendNotificationEmail(const BitTorrent::Torrent *torrent)
{
// Prepare mail content
const QString content = tr("Torrent name: %1").arg(torrent->name()) + u'\n'
+ tr("Torrent size: %1").arg(Utils::Misc::friendlyUnit(torrent->wantedSize())) + u'\n'
+ tr("Save path: %1").arg(torrent->savePath().toString()) + u"\n\n"
+ tr("The torrent was downloaded in %1.", "The torrent was downloaded in 1 hour and 20 seconds")
.arg(Utils::Misc::userFriendlyDuration(torrent->activeTime())) + u"\n\n\n"
+ tr("Thank you for using qBittorrent.") + u'\n';
// Send the notification email
const Preferences *pref = Preferences::instance();
auto *smtp = new Net::Smtp(this);
smtp->sendMail(pref->getMailNotificationSender(),
pref->getMailNotificationEmail(),
tr("Torrent \"%1\" has finished downloading").arg(torrent->name()),
content);
}
void Application::sendTestEmail() const
{
const Preferences *pref = Preferences::instance();
if (pref->isMailNotificationEnabled())
{
// Prepare mail content
const QString content = tr("This is a test email.") + u'\n'
+ tr("Thank you for using qBittorrent.") + u'\n';
// Send the notification email
auto *smtp = new Net::Smtp();
smtp->sendMail(pref->getMailNotificationSender(),
pref->getMailNotificationEmail(),
tr("Test email"),
content);
}
}
void Application::torrentAdded(const BitTorrent::Torrent *torrent) const
{
const Preferences *pref = Preferences::instance();
// AutoRun program
if (pref->isAutoRunOnTorrentAddedEnabled())
runExternalProgram(pref->getAutoRunOnTorrentAddedProgram().trimmed(), torrent);
}
void Application::torrentFinished(const BitTorrent::Torrent *torrent)
{
const Preferences *pref = Preferences::instance();
// AutoRun program
if (pref->isAutoRunOnTorrentFinishedEnabled())
runExternalProgram(pref->getAutoRunOnTorrentFinishedProgram().trimmed(), torrent);
// Mail notification
if (pref->isMailNotificationEnabled())
{
LogMsg(tr("Torrent: %1, sending mail notification").arg(torrent->name()));
sendNotificationEmail(torrent);
}
#ifndef DISABLE_GUI
if (Preferences::instance()->isRecursiveDownloadEnabled())
{
// Check whether it contains .torrent files
for (const Path &torrentRelpath : asConst(torrent->filePaths()))
{
if (torrentRelpath.hasExtension(u".torrent"_s))
{
askRecursiveTorrentDownloadConfirmation(torrent);
break;
}
}
}
#endif
}
void Application::allTorrentsFinished()
{
Preferences *const pref = Preferences::instance();
bool isExit = pref->shutdownqBTWhenDownloadsComplete();
bool isShutdown = pref->shutdownWhenDownloadsComplete();
bool isSuspend = pref->suspendWhenDownloadsComplete();
bool isHibernate = pref->hibernateWhenDownloadsComplete();
bool haveAction = isExit || isShutdown || isSuspend || isHibernate;
if (!haveAction) return;
ShutdownDialogAction action = ShutdownDialogAction::Exit;
if (isSuspend)
action = ShutdownDialogAction::Suspend;
else if (isHibernate)
action = ShutdownDialogAction::Hibernate;
else if (isShutdown)
action = ShutdownDialogAction::Shutdown;
#ifndef DISABLE_GUI
// ask confirm
if ((action == ShutdownDialogAction::Exit) && (pref->dontConfirmAutoExit()))
{
// do nothing & skip confirm
}
else
{
if (!ShutdownConfirmDialog::askForConfirmation(m_window, action)) return;
}
#endif // DISABLE_GUI
// Actually shut down
if (action != ShutdownDialogAction::Exit)
{
qDebug("Preparing for auto-shutdown because all downloads are complete!");
// Disabling it for next time
pref->setShutdownWhenDownloadsComplete(false);
pref->setSuspendWhenDownloadsComplete(false);
pref->setHibernateWhenDownloadsComplete(false);
// Make sure preferences are synced before exiting
m_shutdownAct = action;
}
qDebug("Exiting the application");
exit();
}
bool Application::callMainInstance()
{
return m_instanceManager->sendMessage(serializeParams(commandLineArgs()));
}
void Application::processParams(const QBtCommandLineParameters ¶ms)
{
#ifndef DISABLE_GUI
// There are two circumstances in which we want to show the torrent
// dialog. One is when the application settings specify that it should
// be shown and skipTorrentDialog is undefined. The other is when
// skipTorrentDialog is false, meaning that the application setting
// should be overridden.
AddTorrentOption addTorrentOption = AddTorrentOption::Default;
if (params.skipDialog.has_value())
addTorrentOption = params.skipDialog.value() ? AddTorrentOption::SkipDialog : AddTorrentOption::ShowDialog;
for (const QString &torrentSource : params.torrentSources)
m_addTorrentManager->addTorrent(torrentSource, params.addTorrentParams, addTorrentOption);
#else
for (const QString &torrentSource : params.torrentSources)
m_addTorrentManager->addTorrent(torrentSource, params.addTorrentParams);
#endif
}
int Application::exec()
{
#if !defined(DISABLE_WEBUI) && defined(DISABLE_GUI)
const QString loadingStr = tr("WebUI will be started shortly after internal preparations. Please wait...");
printf("%s\n", qUtf8Printable(loadingStr));
#endif
#if defined(QBT_USES_LIBTORRENT2) && !defined(Q_OS_MACOS)
applyMemoryWorkingSetLimit();
#endif
#ifdef Q_OS_WIN
applyMemoryPriority();
adjustThreadPriority();
#endif
Net::ProxyConfigurationManager::initInstance();
Net::DownloadManager::initInstance();
BitTorrent::Session::initInstance();
#ifndef DISABLE_GUI
UIThemeManager::initInstance();
m_desktopIntegration = new DesktopIntegration;
m_desktopIntegration->setToolTip(tr("Loading torrents..."));
#ifndef Q_OS_MACOS
auto *desktopIntegrationMenu = m_desktopIntegration->menu();
auto *actionExit = new QAction(tr("E&xit"), desktopIntegrationMenu);
actionExit->setIcon(UIThemeManager::instance()->getIcon(u"application-exit"_s));
actionExit->setMenuRole(QAction::QuitRole);
actionExit->setShortcut(Qt::CTRL | Qt::Key_Q);
connect(actionExit, &QAction::triggered, this, []
{
QApplication::exit();
});
desktopIntegrationMenu->addAction(actionExit);
const bool isHidden = m_desktopIntegration->isActive() && (startUpWindowState() == WindowState::Hidden);
#else
const bool isHidden = false;
#endif
if (!isHidden)
{
createStartupProgressDialog();
// Add a small delay to avoid "flashing" the progress dialog in case there are not many torrents to restore.
m_startupProgressDialog->setMinimumDuration(1000);
if (startUpWindowState() != WindowState::Normal)
m_startupProgressDialog->setWindowState(Qt::WindowMinimized);
}
else
{
connect(m_desktopIntegration, &DesktopIntegration::activationRequested, this, &Application::createStartupProgressDialog);
}
#endif
connect(BitTorrent::Session::instance(), &BitTorrent::Session::restored, this, [this]()
{
connect(BitTorrent::Session::instance(), &BitTorrent::Session::torrentAdded, this, &Application::torrentAdded);
connect(BitTorrent::Session::instance(), &BitTorrent::Session::torrentFinished, this, &Application::torrentFinished);
connect(BitTorrent::Session::instance(), &BitTorrent::Session::allTorrentsFinished, this, &Application::allTorrentsFinished, Qt::QueuedConnection);
m_addTorrentManager = new AddTorrentManagerImpl(this, BitTorrent::Session::instance(), this);
Net::GeoIPManager::initInstance();
TorrentFilesWatcher::initInstance();
new RSS::Session; // create RSS::Session singleton
new RSS::AutoDownloader(this); // create RSS::AutoDownloader singleton
#ifndef DISABLE_GUI
const auto *btSession = BitTorrent::Session::instance();
connect(btSession, &BitTorrent::Session::fullDiskError, this
, [this](const BitTorrent::Torrent *torrent, const QString &msg)
{
m_desktopIntegration->showNotification(tr("I/O Error", "i.e: Input/Output Error")
, tr("An I/O error occurred for torrent '%1'.\n Reason: %2"
, "e.g: An error occurred for torrent 'xxx.avi'.\n Reason: disk is full.").arg(torrent->name(), msg));
});
connect(btSession, &BitTorrent::Session::torrentFinished, this
, [this](const BitTorrent::Torrent *torrent)
{
m_desktopIntegration->showNotification(tr("Download completed"), tr("'%1' has finished downloading.", "e.g: xxx.avi has finished downloading.").arg(torrent->name()));
});
connect(m_addTorrentManager, &AddTorrentManager::torrentAdded, this
, [this]([[maybe_unused]] const QString &source, const BitTorrent::Torrent *torrent)
{
if (isTorrentAddedNotificationsEnabled())
m_desktopIntegration->showNotification(tr("Torrent added"), tr("'%1' was added.", "e.g: xxx.avi was added.").arg(torrent->name()));
});
connect(m_addTorrentManager, &AddTorrentManager::addTorrentFailed, this
, [this](const QString &source, const QString &reason)
{
m_desktopIntegration->showNotification(tr("Add torrent failed")
, tr("Couldn't add torrent '%1', reason: %2.").arg(source, reason));
});
disconnect(m_desktopIntegration, &DesktopIntegration::activationRequested, this, &Application::createStartupProgressDialog);
#ifndef Q_OS_MACOS
const WindowState windowState = !m_startupProgressDialog ? WindowState::Hidden
: (m_startupProgressDialog->windowState() & Qt::WindowMinimized) ? WindowState::Minimized
: WindowState::Normal;
#else
const WindowState windowState = (m_startupProgressDialog->windowState() & Qt::WindowMinimized)
? WindowState::Minimized : WindowState::Normal;
#endif
m_window = new MainWindow(this, windowState, instanceName());
delete m_startupProgressDialog;
#endif // DISABLE_GUI
#ifndef DISABLE_WEBUI
#ifndef DISABLE_GUI
m_webui = new WebUI(this);
#else
const auto *pref = Preferences::instance();
const QString tempPassword = pref->getWebUIPassword().isEmpty()
? Utils::Password::generate() : QString();
m_webui = new WebUI(this, (!tempPassword.isEmpty() ? Utils::Password::PBKDF2::generate(tempPassword) : QByteArray()));
connect(m_webui, &WebUI::error, this, [](const QString &message)
{
fprintf(stderr, "WebUI configuration failed. Reason: %s\n", qUtf8Printable(message));
});
printf("%s", qUtf8Printable(u"\n******** %1 ********\n"_s.arg(tr("Information"))));
if (m_webui->isErrored())
{
const QString error = m_webui->errorMessage() + u'\n'
+ tr("To fix the error, you may need to edit the config file manually.");
fprintf(stderr, "%s\n", qUtf8Printable(error));
}
else if (m_webui->isEnabled())
{
const QHostAddress address = m_webui->hostAddress();
const QString url = u"%1://%2:%3"_s.arg((m_webui->isHttps() ? u"https"_s : u"http"_s)
, (address.isEqual(QHostAddress::Any, QHostAddress::ConvertUnspecifiedAddress) ? u"localhost"_s : address.toString())
, QString::number(m_webui->port()));
printf("%s\n", qUtf8Printable(tr("To control qBittorrent, access the WebUI at: %1").arg(url)));
if (!tempPassword.isEmpty())
{
const QString warning = tr("The WebUI administrator username is: %1").arg(pref->getWebUIUsername()) + u'\n'
+ tr("The WebUI administrator password was not set. A temporary password is provided for this session: %1").arg(tempPassword) + u'\n'
+ tr("You should set your own password in program preferences.") + u'\n';
printf("%s", qUtf8Printable(warning));
}
}
else
{
printf("%s\n", qUtf8Printable(tr("The WebUI is disabled! To enable the WebUI, edit the config file manually.")));
}
#endif // DISABLE_GUI
#endif // DISABLE_WEBUI
m_isProcessingParamsAllowed = true;
for (const QBtCommandLineParameters ¶ms : m_paramsQueue)
processParams(params);
m_paramsQueue.clear();
});
const QBtCommandLineParameters params = commandLineArgs();
if (!params.torrentSources.isEmpty())
m_paramsQueue.append(params);
return BaseApplication::exec();
}
bool Application::hasAnotherInstance() const
{
return !m_instanceManager->isFirstInstance();
}
#ifndef DISABLE_GUI
void Application::createStartupProgressDialog()
{
Q_ASSERT(!m_startupProgressDialog);
Q_ASSERT(m_desktopIntegration);
disconnect(m_desktopIntegration, &DesktopIntegration::activationRequested, this, &Application::createStartupProgressDialog);
m_startupProgressDialog = new QProgressDialog(tr("Loading torrents..."), tr("Exit"), 0, 100);
m_startupProgressDialog->setAttribute(Qt::WA_DeleteOnClose);
m_startupProgressDialog->setWindowFlag(Qt::WindowMinimizeButtonHint);
m_startupProgressDialog->setMinimumDuration(0); // Show dialog immediately by default
m_startupProgressDialog->setAutoReset(false);
m_startupProgressDialog->setAutoClose(false);
connect(m_startupProgressDialog, &QProgressDialog::canceled, this, []()
{
QApplication::exit();