From 51332b86a118704bc73cae43556b83a4591399b1 Mon Sep 17 00:00:00 2001 From: Marc Bouvier Date: Tue, 3 Sep 2013 01:44:21 -0500 Subject: [PATCH] [Feature #811] Only 1 Instance of OpenCS is Allowed If another instance of OpenCS is started, then it will terminate. This is done by creating a QLocalServer with a unique ID. If another QLocalServer with the same ID attempts to be opened, then the creation of the QLocalServer will fail and the application will terminate. --- apps/opencs/CMakeLists.txt | 2 +- apps/opencs/editor.cpp | 11 +++++++++++ apps/opencs/editor.hpp | 8 ++++++++ apps/opencs/main.cpp | 5 +++++ 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/apps/opencs/CMakeLists.txt b/apps/opencs/CMakeLists.txt index f7b7daee49..1eb8fd2761 100644 --- a/apps/opencs/CMakeLists.txt +++ b/apps/opencs/CMakeLists.txt @@ -127,7 +127,7 @@ if(WIN32) set(QT_USE_QTMAIN TRUE) endif(WIN32) -find_package(Qt4 COMPONENTS QtCore QtGui QtXml QtXmlPatterns REQUIRED) +find_package(Qt4 COMPONENTS QtCore QtGui QtNetwork QtXml QtXmlPatterns REQUIRED) include(${QT_USE_FILE}) qt4_wrap_ui(OPENCS_UI_HDR ${OPENCS_UI}) diff --git a/apps/opencs/editor.cpp b/apps/opencs/editor.cpp index e05ebcdeb2..c339d87699 100644 --- a/apps/opencs/editor.cpp +++ b/apps/opencs/editor.cpp @@ -2,6 +2,8 @@ #include "editor.hpp" #include +#include +#include #include "model/doc/document.hpp" #include "model/world/data.hpp" @@ -114,6 +116,15 @@ void CS::Editor::createNewFile() mFileDialog.hide(); } +bool CS::Editor::makeIPCServer() +{ + server = new QLocalServer(this); + if(server->listen("IPCServer")) + return true; + server->close(); + return false; +} + int CS::Editor::run() { mStartup.show(); diff --git a/apps/opencs/editor.hpp b/apps/opencs/editor.hpp index 380e434c24..2d50b68fe3 100644 --- a/apps/opencs/editor.hpp +++ b/apps/opencs/editor.hpp @@ -1,6 +1,8 @@ #ifndef CS_EDITOR_H #define CS_EDITOR_H +#include + #include #ifndef Q_MOC_RUN #include @@ -35,6 +37,8 @@ namespace CS Editor(); + bool makeIPCServer(); + int run(); ///< \return error status @@ -45,6 +49,10 @@ namespace CS void loadDocument(); void openFiles(); void createNewFile(); + + private: + + QLocalServer *server; }; } diff --git a/apps/opencs/main.cpp b/apps/opencs/main.cpp index 7f6f9302ed..3e40ee8ccb 100644 --- a/apps/opencs/main.cpp +++ b/apps/opencs/main.cpp @@ -39,5 +39,10 @@ int main(int argc, char *argv[]) CS::Editor editor; + if(!editor.makeIPCServer()) + { + return 0; + } + return editor.run(); }