|
82 | 82 | #include "base/utils/fs.h" |
83 | 83 | #include "base/utils/misc.h" |
84 | 84 | #include "base/utils/string.h" |
| 85 | +#include "applicationinstancemanager.h" |
85 | 86 | #include "filelogger.h" |
86 | 87 |
|
87 | 88 | #ifndef DISABLE_WEBUI |
@@ -116,7 +117,8 @@ namespace |
116 | 117 | } |
117 | 118 |
|
118 | 119 | Application::Application(const QString &id, int &argc, char **argv) |
119 | | - : BaseApplication(id, argc, argv) |
| 120 | + : BaseApplication(argc, argv) |
| 121 | + , m_instanceManager(new ApplicationInstanceManager {id, this}) |
120 | 122 | , m_running(false) |
121 | 123 | , m_shutdownAct(ShutdownDialogAction::Exit) |
122 | 124 | , m_commandLineArgs(parseCommandLine(this->arguments())) |
@@ -156,7 +158,7 @@ Application::Application(const QString &id, int &argc, char **argv) |
156 | 158 | connect(this, &QGuiApplication::commitDataRequest, this, &Application::shutdownCleanup, Qt::DirectConnection); |
157 | 159 | #endif |
158 | 160 |
|
159 | | - connect(this, &Application::messageReceived, this, &Application::processMessage); |
| 161 | + connect(m_instanceManager, &ApplicationInstanceManager::messageReceived, this, &Application::processMessage); |
160 | 162 | connect(this, &QCoreApplication::aboutToQuit, this, &Application::cleanup); |
161 | 163 |
|
162 | 164 | if (isFileLoggerEnabled()) |
@@ -417,7 +419,7 @@ void Application::allTorrentsFinished() |
417 | 419 |
|
418 | 420 | bool Application::sendParams(const QStringList ¶ms) |
419 | 421 | { |
420 | | - return sendMessage(params.join(PARAMS_SEPARATOR)); |
| 422 | + return m_instanceManager->sendMessage(params.join(PARAMS_SEPARATOR)); |
421 | 423 | } |
422 | 424 |
|
423 | 425 | // As program parameters, we can get paths or urls. |
@@ -572,34 +574,12 @@ int Application::exec(const QStringList ¶ms) |
572 | 574 | return BaseApplication::exec(); |
573 | 575 | } |
574 | 576 |
|
575 | | -#ifndef DISABLE_GUI |
576 | | -#ifdef Q_OS_WIN |
577 | 577 | bool Application::isRunning() |
578 | 578 | { |
579 | | - const bool running = BaseApplication::isRunning(); |
580 | | - QSharedMemory *sharedMem = new QSharedMemory(id() + QLatin1String("-shared-memory-key"), this); |
581 | | - if (!running) { |
582 | | - // First instance creates shared memory and store PID |
583 | | - if (sharedMem->create(sizeof(DWORD)) && sharedMem->lock()) { |
584 | | - *(static_cast<DWORD*>(sharedMem->data())) = ::GetCurrentProcessId(); |
585 | | - sharedMem->unlock(); |
586 | | - } |
587 | | - } |
588 | | - else { |
589 | | - // Later instances attach to shared memory and retrieve PID |
590 | | - if (sharedMem->attach() && sharedMem->lock()) { |
591 | | - ::AllowSetForegroundWindow(*(static_cast<DWORD*>(sharedMem->data()))); |
592 | | - sharedMem->unlock(); |
593 | | - } |
594 | | - } |
595 | | - |
596 | | - if (!sharedMem->isAttached()) |
597 | | - qWarning() << "Failed to initialize shared memory: " << sharedMem->errorString(); |
598 | | - |
599 | | - return running; |
| 579 | + return !m_instanceManager->isFirstInstance(); |
600 | 580 | } |
601 | | -#endif // Q_OS_WIN |
602 | 581 |
|
| 582 | +#ifndef DISABLE_GUI |
603 | 583 | #ifdef Q_OS_MAC |
604 | 584 | bool Application::event(QEvent *ev) |
605 | 585 | { |
@@ -740,11 +720,11 @@ void Application::cleanup() |
740 | 720 | #ifndef DISABLE_GUI |
741 | 721 | if (m_window) { |
742 | 722 | #ifdef Q_OS_WIN |
743 | | - typedef BOOL (WINAPI *PSHUTDOWNBRDESTROY)(HWND); |
| 723 | + using PSHUTDOWNBRDESTROY = BOOL (WINAPI *)(HWND); |
744 | 724 | const auto shutdownBRDestroy = Utils::Misc::loadWinAPI<PSHUTDOWNBRDESTROY>("User32.dll", "ShutdownBlockReasonDestroy"); |
745 | 725 | // Only available on Vista+ |
746 | 726 | if (shutdownBRDestroy) |
747 | | - shutdownBRDestroy((HWND)m_window->effectiveWinId()); |
| 727 | + shutdownBRDestroy(reinterpret_cast<HWND>(m_window->effectiveWinId())); |
748 | 728 | #endif // Q_OS_WIN |
749 | 729 | delete m_window; |
750 | 730 | } |
|
0 commit comments