Skip to content

Commit

Permalink
Adds single instance check (#165)
Browse files Browse the repository at this point in the history
Uses QLocalServer to listen for new instances starting. If a new
instance successfully connects to the running instance, the new instance
quits and the existing instance will show the main window.

Future improvements could allow a remote app to control the running
instance, e.g send a command to show the settings or quit.

Fixes #23
  • Loading branch information
Joseph R. Nosie authored and Manuel Schneider committed Apr 27, 2016
1 parent b174303 commit e9b93e5
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/application/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
find_package(Qt5Widgets 5.2 REQUIRED)
find_package(Qt5Svg 5.2 REQUIRED)
find_package(Qt5Network REQUIRED)

include_directories(
hotkey/
Expand Down Expand Up @@ -37,6 +38,7 @@ set(SRC
set(LIB
${Qt5Widgets_LIBRARIES}
${Qt5Svg_LIBRARIES}
${Qt5Network_LIBRARIES}
)

qt5_wrap_ui(UI
Expand Down
10 changes: 10 additions & 0 deletions src/application/albertapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ AlbertApp::AlbertApp(int &argc, char *argv[]) : QApplication(argc, argv) {
hotkeyManager_ = new HotkeyManager;
pluginManager_ = new PluginManager;
extensionManager_ = new ExtensionManager;
localServer_ = new QLocalServer(this);

// Start server so second instances will close
QLocalServer::removeServer("albertapp");
localServer_->listen("albertapp");

QObject::connect(localServer_, &QLocalServer::newConnection, [=] () {
mainWindow_->show();
localServer_->nextPendingConnection()->close();
});

// Propagade the extensions once
for (const unique_ptr<PluginSpec> &p : pluginManager_->plugins())
Expand Down
3 changes: 3 additions & 0 deletions src/application/albertapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#pragma once
#include <QApplication>
#include <QtNetwork/QLocalServer>
#include <QtNetwork/QLocalSocket>
#include <QPointer>
class MainWindow;
class HotkeyManager;
Expand Down Expand Up @@ -48,5 +50,6 @@ class AlbertApp final : public QApplication
PluginManager *pluginManager_;
ExtensionManager *extensionManager_;
QPointer<SettingsWidget> settingsWidget_;
QLocalServer *localServer_;
};

5 changes: 5 additions & 0 deletions src/application/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
#include "albertapp.h"

int main(int argc, char *argv[]) {
QLocalSocket socket;
socket.connectToServer("albertapp");
if (socket.waitForConnected(500))
return EXIT_SUCCESS;

AlbertApp app(argc, argv);
return app.exec();
}

0 comments on commit e9b93e5

Please sign in to comment.