diff --git a/src/main.cpp b/src/main.cpp index a20162767..0a2ab676b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -78,6 +78,127 @@ Q_IMPORT_PLUGIN(qico) #error You seem to have updated QtSingleApplication without porting our custom QtSingleApplication::getRunningPid() function. Please see previous version to understate how it works. #endif +// Signal handlers +#if defined(Q_OS_UNIX) || defined(STACKTRACE_WIN) +void sigintHandler(int); +void sigtermHandler(int); +void sigsegvHandler(int); +void sigabrtHandler(int); +#endif + +#ifndef DISABLE_GUI +void showSplashScreen(); +void parseCommandLine(bool& showVersion, bool& showUsage, bool& noSplash, QStringList& torrents); +#else +void parseCommandLine(bool& showVersion, bool& showUsage, bool& shouldDaemonize, QStringList& torrents); +#endif +void displayVersion(); +void displayUsage(const char *prg_name); +bool userAgreesWithLegalNotice(); + +// Main +int main(int argc, char *argv[]) +{ + // We must save it here because QApplication constructor may change it + bool isOneArg = (argc == 2); + bool showVersion; + bool showUsage; +#ifndef DISABLE_GUI + bool noSplash; +#else + bool shouldDaemonize; +#endif + QStringList torrents; + + // Create Application + Application app("qBittorrent-" + misc::getUserIDString(), argc, argv); + +#ifndef DISABLE_GUI + parseCommandLine(showVersion, showUsage, noSplash, torrents); +#else + parseCommandLine(showVersion, showUsage, shouldDaemonize, torrents); +#endif + + if (showVersion) + displayVersion(); + + if (showUsage) + displayUsage(argv[0]); + + // If only one parameter is present and it is "--version" + // or "--help", program exits after printing appropriate message. + if (isOneArg && (showVersion || showUsage)) + return EXIT_SUCCESS; + + // Set environment variable + if (!qputenv("QBITTORRENT", QByteArray(VERSION))) { + std::cerr << "Couldn't set environment variable...\n"; + } + + if (!userAgreesWithLegalNotice()) + return EXIT_SUCCESS; + + // Check if qBittorrent is already running for this user + if (app.isRunning()) { + qDebug("qBittorrent is already running for this user."); +#ifdef Q_OS_WIN + DWORD pid = (DWORD)app.getRunningPid(); + if (pid > 0) { + BOOL b = AllowSetForegroundWindow(pid); + qDebug("AllowSetForegroundWindow() returns %s", b ? "TRUE" : "FALSE"); + } +#endif + if (!torrents.isEmpty()) { + QString message = torrents.join("|"); + qDebug("Passing program parameters to running instance..."); + qDebug("Message: %s", qPrintable(message)); + app.sendMessage(message); + } + else { // Raise main window + app.sendMessage("qbt://show"); + } + + return EXIT_SUCCESS; + } + + srand(time(0)); +#ifdef DISABLE_GUI + if (shouldDaemonize && (daemon(1, 0) != 0)) { + qCritical("Something went wrong while daemonizing, exiting..."); + return EXIT_FAILURE; + } +#else + if (!noSplash) + showSplashScreen(); +#endif + +#if defined(Q_OS_UNIX) || defined(STACKTRACE_WIN) + signal(SIGABRT, sigabrtHandler); + signal(SIGTERM, sigtermHandler); + signal(SIGINT, sigintHandler); + signal(SIGSEGV, sigsegvHandler); +#endif + +#ifndef DISABLE_GUI + MainWindow window(0, torrents); + QObject::connect(&app, SIGNAL(messageReceived(const QString&)), + &window, SLOT(processParams(const QString&))); + app.setActivationWindow(&window); +#ifdef Q_OS_MAC + static_cast(&app)->setReadyToProcessEvents(); +#endif // Q_OS_MAC +#else + // Load Headless class + HeadlessLoader loader(torrents); + QObject::connect(&app, SIGNAL(messageReceived(const QString&)), + &loader, SLOT(processParams(const QString&))); +#endif + + int ret = app.exec(); + qDebug("Application has exited"); + return ret; +} + #if defined(Q_OS_UNIX) || defined(STACKTRACE_WIN) void sigintHandler(int) { @@ -167,15 +288,11 @@ void displayUsage(const char *prg_name) std::cout << '\t' << prg_name << " " << qPrintable(QObject::tr("[files or urls]: downloads the torrents passed by the user (optional)")) << std::endl; } -void parseCommandLine( - bool& showVersion, bool& showUsage, #ifndef DISABLE_GUI - bool& noSplash, +void parseCommandLine(bool& showVersion, bool& showUsage, bool& noSplash, QStringList& torrents) #else - bool& shouldDaemonize, +void parseCommandLine(bool& showVersion, bool& showUsage, bool& shouldDaemonize, QStringList& torrents) #endif - QStringList& torrents - ) { // Default values showVersion = false; @@ -262,106 +379,3 @@ bool userAgreesWithLegalNotice() return false; } - -// Main -int main(int argc, char *argv[]) -{ - // We must save it here because QApplication constructor may change it - bool isOneArg = (argc == 2); - bool showVersion; - bool showUsage; -#ifndef DISABLE_GUI - bool noSplash; -#else - bool shouldDaemonize; -#endif - QStringList torrents; - - // Create Application - Application app("qBittorrent-" + misc::getUserIDString(), argc, argv); - -#ifndef DISABLE_GUI - parseCommandLine(showVersion, showUsage, noSplash, torrents); -#else - parseCommandLine(showVersion, showUsage, shouldDaemonize, torrents); -#endif - - if (showVersion) - displayVersion(); - - if (showUsage) - displayUsage(argv[0]); - - // If only one parameter is present and it is "--version" - // or "--help", program exits after printing appropriate message. - if (isOneArg && (showVersion || showUsage)) - return EXIT_SUCCESS; - - // Set environment variable - if (!qputenv("QBITTORRENT", QByteArray(VERSION))) { - std::cerr << "Couldn't set environment variable...\n"; - } - - if (!userAgreesWithLegalNotice()) - return EXIT_SUCCESS; - - // Check if qBittorrent is already running for this user - if (app.isRunning()) { - qDebug("qBittorrent is already running for this user."); -#ifdef Q_OS_WIN - DWORD pid = (DWORD)app.getRunningPid(); - if (pid > 0) { - BOOL b = AllowSetForegroundWindow(pid); - qDebug("AllowSetForegroundWindow() returns %s", b ? "TRUE" : "FALSE"); - } -#endif - if (!torrents.isEmpty()) { - QString message = torrents.join("|"); - qDebug("Passing program parameters to running instance..."); - qDebug("Message: %s", qPrintable(message)); - app.sendMessage(message); - } - else { // Raise main window - app.sendMessage("qbt://show"); - } - - return EXIT_SUCCESS; - } - - srand(time(0)); -#ifdef DISABLE_GUI - if (shouldDaemonize && (daemon(1, 0) != 0)) { - qCritical("Something went wrong while daemonizing, exiting..."); - return EXIT_FAILURE; - } -#else - if (!noSplash) - showSplashScreen(); -#endif - -#if defined(Q_OS_UNIX) || defined(STACKTRACE_WIN) - signal(SIGABRT, sigabrtHandler); - signal(SIGTERM, sigtermHandler); - signal(SIGINT, sigintHandler); - signal(SIGSEGV, sigsegvHandler); -#endif - -#ifndef DISABLE_GUI - MainWindow window(0, torrents); - QObject::connect(&app, SIGNAL(messageReceived(const QString&)), - &window, SLOT(processParams(const QString&))); - app.setActivationWindow(&window); -#ifdef Q_OS_MAC - static_cast(&app)->setReadyToProcessEvents(); -#endif // Q_OS_MAC -#else - // Load Headless class - HeadlessLoader loader(torrents); - QObject::connect(&app, SIGNAL(messageReceived(const QString&)), - &loader, SLOT(processParams(const QString&))); -#endif - - int ret = app.exec(); - qDebug("Application has exited"); - return ret; -}