Browse Source

Merge pull request #2314 from glassez/daemon

Fix qbittorrent-nox daemon (discussed in #2300).
adaptive-webui-19844
sledgehammer999 10 years ago
parent
commit
33fe829eb4
  1. 39
      src/main.cpp

39
src/main.cpp

@ -30,6 +30,7 @@
*/ */
#include <QDebug> #include <QDebug>
#include <QScopedPointer>
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
#include <QFont> #include <QFont>
@ -125,10 +126,10 @@ int main(int argc, char *argv[])
QStringList torrents; QStringList torrents;
// Create Application // Create Application
Application app("qBittorrent-" + misc::getUserIDString(), argc, argv); QScopedPointer<Application> app(new Application("qBittorrent-" + misc::getUserIDString(), argc, argv));
MessagesCollector* messagesCollector = new MessagesCollector(); MessagesCollector* messagesCollector = new MessagesCollector();
QObject::connect(&app, SIGNAL(messageReceived(const QString &)), QObject::connect(app.data(), SIGNAL(messageReceived(const QString &)),
messagesCollector, SLOT(collectMessage(const QString &))); messagesCollector, SLOT(collectMessage(const QString &)));
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
@ -156,11 +157,11 @@ int main(int argc, char *argv[])
return EXIT_SUCCESS; return EXIT_SUCCESS;
// Check if qBittorrent is already running for this user // Check if qBittorrent is already running for this user
if (app.isRunning()) { if (app->isRunning()) {
qDebug("qBittorrent is already running for this user."); qDebug("qBittorrent is already running for this user.");
misc::msleep(300); misc::msleep(300);
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
DWORD pid = (DWORD)app.getRunningPid(); DWORD pid = (DWORD)app->getRunningPid();
if (pid > 0) { if (pid > 0) {
BOOL b = AllowSetForegroundWindow(pid); BOOL b = AllowSetForegroundWindow(pid);
qDebug("AllowSetForegroundWindow() returns %s", b ? "TRUE" : "FALSE"); qDebug("AllowSetForegroundWindow() returns %s", b ? "TRUE" : "FALSE");
@ -170,10 +171,10 @@ int main(int argc, char *argv[])
QString message = torrents.join("|"); QString message = torrents.join("|");
qDebug("Passing program parameters to running instance..."); qDebug("Passing program parameters to running instance...");
qDebug("Message: %s", qPrintable(message)); qDebug("Message: %s", qPrintable(message));
app.sendMessage(message); app->sendMessage(message);
} }
else { // Raise main window else { // Raise main window
app.sendMessage("qbt://show"); app->sendMessage("qbt://show");
} }
return EXIT_SUCCESS; return EXIT_SUCCESS;
@ -181,10 +182,20 @@ int main(int argc, char *argv[])
srand(time(0)); srand(time(0));
#ifdef DISABLE_GUI #ifdef DISABLE_GUI
if (shouldDaemonize && (daemon(1, 0) != 0)) { if (shouldDaemonize) {
app.reset(); // Destroy current application
if ((daemon(1, 0) == 0)) {
app.reset(new Application("qBittorrent-" + misc::getUserIDString(), argc, argv));
if (app->isRunning()) {
// Another instance had time to start.
return EXIT_FAILURE;
}
}
else {
qCritical("Something went wrong while daemonizing, exiting..."); qCritical("Something went wrong while daemonizing, exiting...");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
}
#else #else
if (!noSplash) if (!noSplash)
showSplashScreen(); showSplashScreen();
@ -199,28 +210,28 @@ int main(int argc, char *argv[])
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
MainWindow window(0, torrents); MainWindow window(0, torrents);
QObject::connect(&app, SIGNAL(messageReceived(const QString &)), QObject::connect(app.data(), SIGNAL(messageReceived(const QString &)),
&window, SLOT(processParams(const QString &))); &window, SLOT(processParams(const QString &)));
QObject::disconnect(&app, SIGNAL(messageReceived(const QString &)), QObject::disconnect(app.data(), SIGNAL(messageReceived(const QString &)),
messagesCollector, SLOT(collectMessage(const QString &))); messagesCollector, SLOT(collectMessage(const QString &)));
window.processParams(messagesCollector->messages); window.processParams(messagesCollector->messages);
delete messagesCollector; delete messagesCollector;
app.setActivationWindow(&window); app->setActivationWindow(&window);
#ifdef Q_OS_MAC #ifdef Q_OS_MAC
static_cast<QMacApplication*>(&app)->setReadyToProcessEvents(); static_cast<QMacApplication*>(app.data())->setReadyToProcessEvents();
#endif // Q_OS_MAC #endif // Q_OS_MAC
#else #else
// Load Headless class // Load Headless class
HeadlessLoader loader(torrents); HeadlessLoader loader(torrents);
QObject::connect(&app, SIGNAL(messageReceived(const QString &)), QObject::connect(app.data(), SIGNAL(messageReceived(const QString &)),
&loader, SLOT(processParams(const QString &))); &loader, SLOT(processParams(const QString &)));
QObject::disconnect(&app, SIGNAL(messageReceived(const QString &)), QObject::disconnect(app.data(), SIGNAL(messageReceived(const QString &)),
messagesCollector, SLOT(collectMessage(const QString &))); messagesCollector, SLOT(collectMessage(const QString &)));
loader.processParams(messagesCollector->messages); loader.processParams(messagesCollector->messages);
delete messagesCollector; delete messagesCollector;
#endif #endif
int ret = app.exec(); int ret = app->exec();
qDebug("Application has exited"); qDebug("Application has exited");
return ret; return ret;
} }

Loading…
Cancel
Save