-
-
Notifications
You must be signed in to change notification settings - Fork 4k
/
main.cpp
364 lines (322 loc) · 12.2 KB
/
main.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
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2014 Vladimir Golovnev <[email protected]>
* Copyright (C) 2006 Christophe Dumez <[email protected]>
*
* 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 <QtGlobal>
#include <cstdlib>
#include <memory>
#ifdef Q_OS_UNIX
#include <sys/resource.h>
#endif
#ifndef Q_OS_WIN
#ifndef Q_OS_HAIKU
#include <unistd.h>
#endif // Q_OS_HAIKU
#elif defined DISABLE_GUI
#include <io.h>
#endif
#include <QDebug>
#include <QThread>
#ifndef DISABLE_GUI
// GUI-only includes
#include <QFont>
#include <QMessageBox>
#include <QPainter>
#include <QPen>
#include <QPushButton>
#include <QSplashScreen>
#include <QTimer>
#ifdef QBT_STATIC_QT
#include <QtPlugin>
Q_IMPORT_PLUGIN(QICOPlugin)
#endif // QBT_STATIC_QT
#else // DISABLE_GUI
#include <cstdio>
#endif // DISABLE_GUI
#include "base/global.h"
#include "base/preferences.h"
#include "base/profile.h"
#include "base/version.h"
#include "application.h"
#include "cmdoptions.h"
#include "signalhandler.h"
#include "upgrade.h"
#ifndef DISABLE_GUI
#include "gui/utils.h"
#endif
void displayVersion();
bool userAgreesWithLegalNotice();
void displayBadArgMessage(const QString &message);
#ifndef DISABLE_GUI
void showSplashScreen();
#endif // DISABLE_GUI
#ifdef Q_OS_UNIX
void adjustFileDescriptorLimit();
#endif
// Main
int main(int argc, char *argv[])
{
#ifdef Q_OS_UNIX
adjustFileDescriptorLimit();
#endif
// We must save it here because QApplication constructor may change it
bool isOneArg = (argc == 2);
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) && !defined(DISABLE_GUI)
// Attribute Qt::AA_EnableHighDpiScaling must be set before QCoreApplication is created
if (qgetenv("QT_ENABLE_HIGHDPI_SCALING").isEmpty() && qgetenv("QT_AUTO_SCREEN_SCALE_FACTOR").isEmpty())
Application::setAttribute(Qt::AA_EnableHighDpiScaling, true);
// HighDPI scale factor policy must be set before QGuiApplication is created
if (qgetenv("QT_SCALE_FACTOR_ROUNDING_POLICY").isEmpty())
Application::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
#endif
try
{
// Create Application
auto app = std::make_unique<Application>(argc, argv);
const QBtCommandLineParameters params = app->commandLineArgs();
if (!params.unknownParameter.isEmpty())
{
throw CommandLineParameterError(QObject::tr("%1 is an unknown command line parameter.",
"--random-parameter is an unknown command line parameter.")
.arg(params.unknownParameter));
}
#if !defined(Q_OS_WIN) || defined(DISABLE_GUI)
if (params.showVersion)
{
if (isOneArg)
{
displayVersion();
return EXIT_SUCCESS;
}
throw CommandLineParameterError(QObject::tr("%1 must be the single command line parameter.")
.arg(u"-v (or --version)"_qs));
}
#endif
if (params.showHelp)
{
if (isOneArg)
{
displayUsage(QString::fromLocal8Bit(argv[0]));
return EXIT_SUCCESS;
}
throw CommandLineParameterError(QObject::tr("%1 must be the single command line parameter.")
.arg(u"-h (or --help)"_qs));
}
const bool firstTimeUser = !Preferences::instance()->getAcceptedLegal();
if (firstTimeUser)
{
#ifndef DISABLE_GUI
if (!userAgreesWithLegalNotice())
return EXIT_SUCCESS;
#elif defined(Q_OS_WIN)
if (_isatty(_fileno(stdin))
&& _isatty(_fileno(stdout))
&& !userAgreesWithLegalNotice())
return EXIT_SUCCESS;
#else
if (!params.shouldDaemonize
&& isatty(fileno(stdin))
&& isatty(fileno(stdout))
&& !userAgreesWithLegalNotice())
return EXIT_SUCCESS;
#endif
setCurrentMigrationVersion();
}
// Check if qBittorrent is already running for this user
if (app->isRunning())
{
#if defined(DISABLE_GUI) && !defined(Q_OS_WIN)
if (params.shouldDaemonize)
{
throw CommandLineParameterError(QObject::tr("You cannot use %1: qBittorrent is already running for this user.")
.arg(u"-d (or --daemon)"_qs));
}
#endif
QThread::msleep(300);
app->sendParams(params.paramList());
return EXIT_SUCCESS;
}
#ifdef Q_OS_WIN
// This affects only Windows apparently and Qt5.
// When QNetworkAccessManager is instantiated it regularly starts polling
// the network interfaces to see what's available and their status.
// This polling creates jitter and high ping with wifi interfaces.
// So here we disable it for lack of better measure.
// It will also spew this message in the console: QObject::startTimer: Timers cannot have negative intervals
// For more info see:
// 1. https://github.com/qbittorrent/qBittorrent/issues/4209
// 2. https://bugreports.qt.io/browse/QTBUG-40332
// 3. https://bugreports.qt.io/browse/QTBUG-46015
qputenv("QT_BEARER_POLL_TIMEOUT", QByteArray::number(-1));
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) && !defined(DISABLE_GUI)
// this is the default in Qt6
app->setAttribute(Qt::AA_DisableWindowContextHelpButton);
#endif
#endif // Q_OS_WIN
#ifdef Q_OS_MACOS
// Since Apple made difficult for users to set PATH, we set here for convenience.
// Users are supposed to install Homebrew Python for search function.
// For more info see issue #5571.
QByteArray path = "/usr/local/bin:";
path += qgetenv("PATH");
qputenv("PATH", path.constData());
// On OS X the standard is to not show icons in the menus
app->setAttribute(Qt::AA_DontShowIconsInMenus);
#else
if (!Preferences::instance()->iconsInMenusEnabled())
app->setAttribute(Qt::AA_DontShowIconsInMenus);
#endif
if (!firstTimeUser)
{
handleChangedDefaults(DefaultPreferencesMode::Legacy);
#ifndef DISABLE_GUI
if (!upgrade()) return EXIT_FAILURE;
#elif defined(Q_OS_WIN)
if (!upgrade(_isatty(_fileno(stdin))
&& _isatty(_fileno(stdout)))) return EXIT_FAILURE;
#else
if (!upgrade(!params.shouldDaemonize
&& isatty(fileno(stdin))
&& isatty(fileno(stdout)))) return EXIT_FAILURE;
#endif
}
else
{
handleChangedDefaults(DefaultPreferencesMode::Current);
}
#if defined(DISABLE_GUI) && !defined(Q_OS_WIN)
if (params.shouldDaemonize)
{
app.reset(); // Destroy current application
if (daemon(1, 0) == 0)
{
app = std::make_unique<Application>(argc, argv);
if (app->isRunning())
{
// Another instance had time to start.
return EXIT_FAILURE;
}
}
else
{
qCritical("Something went wrong while daemonizing, exiting...");
return EXIT_FAILURE;
}
}
#elif !defined(DISABLE_GUI)
if (!(params.noSplash || Preferences::instance()->isSplashScreenDisabled()))
showSplashScreen();
#endif
registerSignalHandlers();
return app->exec(params.paramList());
}
catch (const CommandLineParameterError &er)
{
displayBadArgMessage(er.message());
return EXIT_FAILURE;
}
}
#if !defined(DISABLE_GUI)
void showSplashScreen()
{
QPixmap splashImg(u":/icons/splash.png"_qs);
QPainter painter(&splashImg);
const auto version = QStringLiteral(QBT_VERSION);
painter.setPen(QPen(Qt::white));
painter.setFont(QFont(u"Arial"_qs, 22, QFont::Black));
painter.drawText(224 - painter.fontMetrics().horizontalAdvance(version), 270, version);
QSplashScreen *splash = new QSplashScreen(splashImg);
splash->show();
QTimer::singleShot(1500, splash, &QObject::deleteLater);
qApp->processEvents();
}
#endif // DISABLE_GUI
void displayVersion()
{
printf("%s %s\n", qUtf8Printable(qApp->applicationName()), QBT_VERSION);
}
void displayBadArgMessage(const QString &message)
{
const QString help = QObject::tr("Run application with -h option to read about command line parameters.");
#if defined(Q_OS_WIN) && !defined(DISABLE_GUI)
QMessageBox msgBox(QMessageBox::Critical, QObject::tr("Bad command line"),
(message + u'\n' + help), QMessageBox::Ok);
msgBox.show(); // Need to be shown or to moveToCenter does not work
msgBox.move(Utils::Gui::screenCenter(&msgBox));
msgBox.exec();
#else
const QString errMsg = QObject::tr("Bad command line: ") + u'\n'
+ message + u'\n'
+ help + u'\n';
fprintf(stderr, "%s", qUtf8Printable(errMsg));
#endif
}
bool userAgreesWithLegalNotice()
{
Preferences *const pref = Preferences::instance();
Q_ASSERT(!pref->getAcceptedLegal());
#ifdef DISABLE_GUI
const QString eula = u"\n*** %1 ***\n"_qs.arg(QObject::tr("Legal Notice"))
+ QObject::tr("qBittorrent is a file sharing program. When you run a torrent, its data will be made available to others by means of upload. Any content you share is your sole responsibility.") + u"\n\n"
+ QObject::tr("No further notices will be issued.") + u"\n\n"
+ QObject::tr("Press %1 key to accept and continue...").arg(u"'y'"_qs) + u'\n';
printf("%s", qUtf8Printable(eula));
const char ret = getchar(); // Read pressed key
if ((ret == 'y') || (ret == 'Y'))
{
// Save the answer
pref->setAcceptedLegal(true);
return true;
}
#else
QMessageBox msgBox;
msgBox.setText(QObject::tr("qBittorrent is a file sharing program. When you run a torrent, its data will be made available to others by means of upload. Any content you share is your sole responsibility.\n\nNo further notices will be issued."));
msgBox.setWindowTitle(QObject::tr("Legal notice"));
msgBox.addButton(QObject::tr("Cancel"), QMessageBox::RejectRole);
const QAbstractButton *agreeButton = msgBox.addButton(QObject::tr("I Agree"), QMessageBox::AcceptRole);
msgBox.show(); // Need to be shown or to moveToCenter does not work
msgBox.move(Utils::Gui::screenCenter(&msgBox));
msgBox.exec();
if (msgBox.clickedButton() == agreeButton)
{
// Save the answer
pref->setAcceptedLegal(true);
return true;
}
#endif // DISABLE_GUI
return false;
}
#ifdef Q_OS_UNIX
void adjustFileDescriptorLimit()
{
rlimit limit {};
if (getrlimit(RLIMIT_NOFILE, &limit) != 0)
return;
limit.rlim_cur = limit.rlim_max;
setrlimit(RLIMIT_NOFILE, &limit);
}
#endif