From 51332b86a118704bc73cae43556b83a4591399b1 Mon Sep 17 00:00:00 2001 From: Marc Bouvier Date: Tue, 3 Sep 2013 01:44:21 -0500 Subject: [PATCH 1/4] [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 f7b7daee4..1eb8fd276 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 e05ebcdeb..c339d8769 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 380e434c2..2d50b68fe 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 7f6f9302e..3e40ee8cc 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(); } From 563bd0b4302519b11a33fb5092fbddd66b567d6a Mon Sep 17 00:00:00 2001 From: Marc Bouvier Date: Tue, 3 Sep 2013 04:12:19 -0500 Subject: [PATCH 2/4] [Feature #881] Raise OpenCS New Window After Re-execution The OpenCS startup window of the existing exist is raised when trying to start a new instance. This is done by the new instance connection to the existing instance's QLocalServer. Once the connection is established the existing instance raises the startup window. --- apps/opencs/editor.cpp | 24 +++++++++++++++++++++++- apps/opencs/editor.hpp | 11 +++++++++-- apps/opencs/main.cpp | 1 + 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/apps/opencs/editor.cpp b/apps/opencs/editor.cpp index c339d8769..79dc368b4 100644 --- a/apps/opencs/editor.cpp +++ b/apps/opencs/editor.cpp @@ -10,6 +10,8 @@ CS::Editor::Editor() : mViewManager (mDocumentManager) { + ipcServerName = "IPCServer"; + connect (&mViewManager, SIGNAL (newDocumentRequest ()), this, SLOT (createDocument ())); connect (&mViewManager, SIGNAL (loadDocumentRequest ()), this, SLOT (loadDocument ())); @@ -116,15 +118,35 @@ void CS::Editor::createNewFile() mFileDialog.hide(); } +void CS::Editor::showStartup() +{ + if(mStartup.isHidden()) + mStartup.show(); + mStartup.raise(); + mStartup.activateWindow(); +} + bool CS::Editor::makeIPCServer() { server = new QLocalServer(this); - if(server->listen("IPCServer")) + + if(server->listen(ipcServerName)) + { + connect(server, SIGNAL(newConnection()), this, SLOT(showStartup())); return true; + } + server->close(); return false; } +void CS::Editor::connectToIPCServer() +{ + clientToServerSocket = new QLocalSocket(this); + clientToServerSocket->connectToServer(ipcServerName); + clientToServerSocket->close(); +} + int CS::Editor::run() { mStartup.show(); diff --git a/apps/opencs/editor.hpp b/apps/opencs/editor.hpp index 2d50b68fe..258d5b18a 100644 --- a/apps/opencs/editor.hpp +++ b/apps/opencs/editor.hpp @@ -1,9 +1,11 @@ #ifndef CS_EDITOR_H #define CS_EDITOR_H -#include - #include +#include +#include +#include + #ifndef Q_MOC_RUN #include #endif @@ -38,6 +40,7 @@ namespace CS Editor(); bool makeIPCServer(); + void connectToIPCServer(); int run(); ///< \return error status @@ -50,9 +53,13 @@ namespace CS void openFiles(); void createNewFile(); + void showStartup(); + private: + QString ipcServerName; QLocalServer *server; + QLocalSocket *clientToServerSocket; }; } diff --git a/apps/opencs/main.cpp b/apps/opencs/main.cpp index 3e40ee8cc..eddeb1983 100644 --- a/apps/opencs/main.cpp +++ b/apps/opencs/main.cpp @@ -41,6 +41,7 @@ int main(int argc, char *argv[]) if(!editor.makeIPCServer()) { + editor.connectToIPCServer(); return 0; } From 0ae2bb2fae55c48b885546ec0791b6730b5f7215 Mon Sep 17 00:00:00 2001 From: Marc Bouvier Date: Tue, 3 Sep 2013 05:22:48 -0500 Subject: [PATCH 3/4] [Feature #881] Code Formatting Code formatted to the OpenMW policy. --- apps/opencs/editor.cpp | 16 ++++++++-------- apps/opencs/editor.hpp | 6 +++--- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/apps/opencs/editor.cpp b/apps/opencs/editor.cpp index 79dc368b4..85303fb26 100644 --- a/apps/opencs/editor.cpp +++ b/apps/opencs/editor.cpp @@ -10,7 +10,7 @@ CS::Editor::Editor() : mViewManager (mDocumentManager) { - ipcServerName = "IPCServer"; + mIpcServerName = "IPCServer"; connect (&mViewManager, SIGNAL (newDocumentRequest ()), this, SLOT (createDocument ())); connect (&mViewManager, SIGNAL (loadDocumentRequest ()), this, SLOT (loadDocument ())); @@ -128,23 +128,23 @@ void CS::Editor::showStartup() bool CS::Editor::makeIPCServer() { - server = new QLocalServer(this); + mServer = new QLocalServer(this); - if(server->listen(ipcServerName)) + if(mServer->listen(mIpcServerName)) { - connect(server, SIGNAL(newConnection()), this, SLOT(showStartup())); + connect(mServer, SIGNAL(newConnection()), this, SLOT(showStartup())); return true; } - server->close(); + mServer->close(); return false; } void CS::Editor::connectToIPCServer() { - clientToServerSocket = new QLocalSocket(this); - clientToServerSocket->connectToServer(ipcServerName); - clientToServerSocket->close(); + mClientSocket = new QLocalSocket(this); + mClientSocket->connectToServer(mIpcServerName); + mClientSocket->close(); } int CS::Editor::run() diff --git a/apps/opencs/editor.hpp b/apps/opencs/editor.hpp index 258d5b18a..80336d66f 100644 --- a/apps/opencs/editor.hpp +++ b/apps/opencs/editor.hpp @@ -57,9 +57,9 @@ namespace CS private: - QString ipcServerName; - QLocalServer *server; - QLocalSocket *clientToServerSocket; + QString mIpcServerName; + QLocalServer *mServer; + QLocalSocket *mClientSocket; }; } From 6b11f59ed4e51a2f81f606df2a435680a9d990d6 Mon Sep 17 00:00:00 2001 From: Marc Bouvier Date: Tue, 3 Sep 2013 06:24:11 -0500 Subject: [PATCH 4/4] [Feature #881] Rename QLocalServer The server name should be more unique to the project so there isn't a interprocess clash. --- apps/opencs/editor.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/opencs/editor.cpp b/apps/opencs/editor.cpp index 85303fb26..36d4f9735 100644 --- a/apps/opencs/editor.cpp +++ b/apps/opencs/editor.cpp @@ -10,7 +10,7 @@ CS::Editor::Editor() : mViewManager (mDocumentManager) { - mIpcServerName = "IPCServer"; + mIpcServerName = "org.openmw.OpenCS"; connect (&mViewManager, SIGNAL (newDocumentRequest ()), this, SLOT (createDocument ())); connect (&mViewManager, SIGNAL (loadDocumentRequest ()), this, SLOT (loadDocument ()));