From c7b451b18f788455d394ad45206d6899c0a14ec9 Mon Sep 17 00:00:00 2001 From: knackebrot Date: Mon, 15 Oct 2018 10:59:17 +0200 Subject: [PATCH] Allow headless builds on Windows Daemonizing is disabled because no direct equivalent exists in win32. --- src/app/cmdoptions.cpp | 22 +++++++++++----------- src/app/cmdoptions.h | 4 ++-- src/app/main.cpp | 40 +++++++++++++++++++++++++++++----------- 3 files changed, 42 insertions(+), 24 deletions(-) diff --git a/src/app/cmdoptions.cpp b/src/app/cmdoptions.cpp index 3e23e120b..11ffefcdb 100644 --- a/src/app/cmdoptions.cpp +++ b/src/app/cmdoptions.cpp @@ -37,7 +37,7 @@ #include #include -#ifdef Q_OS_WIN +#if defined(Q_OS_WIN) && !defined(DISABLE_GUI) #include #endif @@ -306,7 +306,7 @@ namespace constexpr const BoolOption SHOW_HELP_OPTION {"help", 'h'}; constexpr const BoolOption SHOW_VERSION_OPTION {"version", 'v'}; -#ifdef DISABLE_GUI +#if defined(DISABLE_GUI) && !defined(Q_OS_WIN) constexpr const BoolOption DAEMON_OPTION {"daemon", 'd'}; #else constexpr const BoolOption NO_SPLASH_OPTION {"no-splash"}; @@ -332,12 +332,12 @@ QBtCommandLineParameters::QBtCommandLineParameters(const QProcessEnvironment &en , skipChecking(SKIP_HASH_CHECK_OPTION.value(env)) , sequential(SEQUENTIAL_OPTION.value(env)) , firstLastPiecePriority(FIRST_AND_LAST_OPTION.value(env)) -#ifndef Q_OS_WIN +#if !defined(Q_OS_WIN) || defined(DISABLE_GUI) , showVersion(false) #endif #ifndef DISABLE_GUI , noSplash(NO_SPLASH_OPTION.value(env)) -#else +#elif !defined(Q_OS_WIN) , shouldDaemonize(DAEMON_OPTION.value(env)) #endif , webUiPort(WEBUI_PORT_OPTION.value(env, -1)) @@ -407,7 +407,7 @@ QBtCommandLineParameters parseCommandLine(const QStringList &args) if (arg == SHOW_HELP_OPTION) { result.showHelp = true; } -#ifndef Q_OS_WIN +#if !defined(Q_OS_WIN) || defined(DISABLE_GUI) else if (arg == SHOW_VERSION_OPTION) { result.showVersion = true; } @@ -422,7 +422,7 @@ QBtCommandLineParameters parseCommandLine(const QStringList &args) else if (arg == NO_SPLASH_OPTION) { result.noSplash = true; } -#else +#elif !defined(Q_OS_WIN) else if (arg == DAEMON_OPTION) { result.shouldDaemonize = true; } @@ -520,7 +520,7 @@ QString makeUsage(const QString &prgName) stream << indentation << prgName << QLatin1String(" [options] [( | )...]") << '\n'; stream << QObject::tr("Options:") << '\n'; -#ifndef Q_OS_WIN +#if !defined(Q_OS_WIN) || defined(DISABLE_GUI) stream << SHOW_VERSION_OPTION.usage() << wrapText(QObject::tr("Display program version and exit")) << '\n'; #endif stream << SHOW_HELP_OPTION.usage() << wrapText(QObject::tr("Display this help message and exit")) << '\n'; @@ -529,7 +529,7 @@ QString makeUsage(const QString &prgName) << '\n'; #ifndef DISABLE_GUI stream << NO_SPLASH_OPTION.usage() << wrapText(QObject::tr("Disable splash screen")) << '\n'; -#else +#elif !defined(Q_OS_WIN) stream << DAEMON_OPTION.usage() << wrapText(QObject::tr("Run in daemon-mode (background)")) << '\n'; #endif //: Use appropriate short form or abbreviation of "directory" @@ -575,12 +575,12 @@ QString makeUsage(const QString &prgName) void displayUsage(const QString &prgName) { -#ifndef Q_OS_WIN - printf("%s\n", qUtf8Printable(makeUsage(prgName))); -#else +#if defined(Q_OS_WIN) && !defined(DISABLE_GUI) QMessageBox msgBox(QMessageBox::Information, QObject::tr("Help"), makeUsage(prgName), QMessageBox::Ok); msgBox.show(); // Need to be shown or to moveToCenter does not work msgBox.move(Utils::Misc::screenCenter(&msgBox)); msgBox.exec(); +#else + printf("%s\n", qUtf8Printable(makeUsage(prgName))); #endif } diff --git a/src/app/cmdoptions.h b/src/app/cmdoptions.h index 2b0762650..dec975951 100644 --- a/src/app/cmdoptions.h +++ b/src/app/cmdoptions.h @@ -43,12 +43,12 @@ class QProcessEnvironment; struct QBtCommandLineParameters { bool showHelp, relativeFastresumePaths, portableMode, skipChecking, sequential, firstLastPiecePriority; -#ifndef Q_OS_WIN +#if !defined(Q_OS_WIN) || defined(DISABLE_GUI) bool showVersion; #endif #ifndef DISABLE_GUI bool noSplash; -#else +#elif !defined(Q_OS_WIN) bool shouldDaemonize; #endif int webUiPort; diff --git a/src/app/main.cpp b/src/app/main.cpp index 629560e71..6b5be8a88 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -34,6 +34,8 @@ #if !defined Q_OS_WIN && !defined Q_OS_HAIKU #include +#elif defined Q_OS_WIN && defined DISABLE_GUI +#include #endif #include @@ -65,7 +67,9 @@ Q_IMPORT_PLUGIN(QICOPlugin) #include "stacktrace.h" #else #include "stacktrace_win.h" +#ifndef DISABLE_GUI #include "stacktracedialog.h" +#endif // DISABLE_GUI #endif // Q_OS_UNIX #endif //STACKTRACE @@ -96,7 +100,7 @@ const char *const sysSigName[] = { #endif }; -#if !defined Q_OS_WIN && !defined Q_OS_HAIKU +#if !(defined Q_OS_WIN && !defined DISABLE_GUI) && !defined Q_OS_HAIKU void reportToUser(const char *str); #endif @@ -126,7 +130,7 @@ int main(int argc, char *argv[]) "--random-parameter is an unknown command line parameter.") .arg(params.unknownParameter)); } -#ifndef Q_OS_WIN +#if !defined(Q_OS_WIN) || defined(DISABLE_GUI) if (params.showVersion) { if (isOneArg) { displayVersion(); @@ -152,6 +156,12 @@ int main(int argc, char *argv[]) #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)) @@ -162,7 +172,7 @@ int main(int argc, char *argv[]) // Check if qBittorrent is already running for this user if (app->isRunning()) { -#ifdef DISABLE_GUI +#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(QLatin1String("-d (or --daemon)"))); @@ -190,7 +200,7 @@ int main(int argc, char *argv[]) // 3. https://bugreports.qt.io/browse/QTBUG-46015 qputenv("QT_BEARER_POLL_TIMEOUT", QByteArray::number(-1)); -#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)) +#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)) && !defined(DISABLE_GUI) // this is the default in Qt6 app->setAttribute(Qt::AA_DisableWindowContextHelpButton); #endif @@ -210,12 +220,15 @@ int main(int argc, char *argv[]) #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 -#ifdef DISABLE_GUI +#if defined(DISABLE_GUI) && !defined(Q_OS_WIN) if (params.shouldDaemonize) { app.reset(); // Destroy current application if (daemon(1, 0) == 0) { @@ -230,7 +243,7 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } } -#else +#elif !defined(DISABLE_GUI) if (!(params.noSplash || Preferences::instance()->isSplashScreenDisabled())) showSplashScreen(); #endif @@ -250,12 +263,17 @@ int main(int argc, char *argv[]) } } -#if !defined Q_OS_WIN && !defined Q_OS_HAIKU +#if !(defined Q_OS_WIN && !defined DISABLE_GUI) && !defined Q_OS_HAIKU void reportToUser(const char *str) { const size_t strLen = strlen(str); +#ifndef Q_OS_WIN if (write(STDERR_FILENO, str, strLen) < static_cast(strLen)) { const auto dummy = write(STDOUT_FILENO, str, strLen); +#else + if (_write(STDERR_FILENO, str, strLen) < static_cast(strLen)) { + const auto dummy = _write(STDOUT_FILENO, str, strLen); +#endif Q_UNUSED(dummy); } } @@ -263,7 +281,7 @@ void reportToUser(const char *str) void sigNormalHandler(int signum) { -#if !defined Q_OS_WIN && !defined Q_OS_HAIKU +#if !(defined Q_OS_WIN && !defined DISABLE_GUI) && !defined Q_OS_HAIKU const char msg1[] = "Catching signal: "; const char msg2[] = "\nExiting cleanly\n"; reportToUser(msg1); @@ -278,7 +296,7 @@ void sigNormalHandler(int signum) void sigAbnormalHandler(int signum) { const char *sigName = sysSigName[signum]; -#if !defined Q_OS_WIN && !defined Q_OS_HAIKU +#if !(defined Q_OS_WIN && !defined DISABLE_GUI) && !defined Q_OS_HAIKU const char msg[] = "\n\n*************************************************************\n" "Please file a bug report at http://bug.qbittorrent.org and provide the following information:\n\n" "qBittorrent version: " QBT_VERSION "\n\n" @@ -289,7 +307,7 @@ void sigAbnormalHandler(int signum) print_stacktrace(); // unsafe #endif -#if defined Q_OS_WIN +#if defined Q_OS_WIN && !defined DISABLE_GUI StacktraceDialog dlg; // unsafe dlg.setStacktraceString(QLatin1String(sigName), straceWin::getBacktrace()); dlg.exec(); @@ -324,7 +342,7 @@ void displayVersion() void displayBadArgMessage(const QString &message) { const QString help = QObject::tr("Run application with -h option to read about command line parameters."); -#ifdef Q_OS_WIN +#if defined(Q_OS_WIN) && !defined(DISABLE_GUI) QMessageBox msgBox(QMessageBox::Critical, QObject::tr("Bad command line"), message + QLatin1Char('\n') + help, QMessageBox::Ok); msgBox.show(); // Need to be shown or to moveToCenter does not work