diff options
author | Rael <rael.gc@gmail.com> | 2015-10-15 09:00:43 -0300 |
---|---|---|
committer | Rael <rael.gc@gmail.com> | 2015-10-15 09:00:43 -0300 |
commit | 1fd358c81dc78e546898a4a75b9a56e8dedc7f8d (patch) | |
tree | 54a30b565934f394d4420f5388aa417de1e01cba | |
parent | 4223b32aa74430f826409833758db17d8eb76c93 (diff) | |
download | scudcloud-1fd358c81dc78e546898a4a75b9a56e8dedc7f8d.zip scudcloud-1fd358c81dc78e546898a4a75b9a56e8dedc7f8d.tar.gz scudcloud-1fd358c81dc78e546898a4a75b9a56e8dedc7f8d.tar.bz2 |
Using native singleton app approach
-rw-r--r-- | scudcloud-1.0/lib/qsingleapplication.py | 75 | ||||
-rwxr-xr-x | scudcloud-1.0/scudcloud | 29 |
2 files changed, 23 insertions, 81 deletions
diff --git a/scudcloud-1.0/lib/qsingleapplication.py b/scudcloud-1.0/lib/qsingleapplication.py deleted file mode 100644 index cac4c99..0000000 --- a/scudcloud-1.0/lib/qsingleapplication.py +++ /dev/null @@ -1,75 +0,0 @@ -""" - The MIT License - - Copyright 2011 Thomas Dall'Agnese <thomas.dallagnese@gmail.com> - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. - -""" -__author__ = "Thomas Dall'Agnese" -__email__ = "thomas.dallagnese@gmail.com" -__version__ = "1.0" -__URL__ = "http://www.dallagnese.fr" - -from PyQt4.QtGui import QMessageBox, QApplication -from PyQt4.QtCore import QIODevice, QTimer, QCoreApplication -from PyQt4.QtNetwork import QLocalServer, QLocalSocket -import sys - -class QSingleApplication(QApplication): - def singleStart(self, mainWindow, pid): - self.mainWindow = mainWindow - self.pid = pid - # Socket - self.m_socket = QLocalSocket() - self.m_socket.connected.connect(self.connectToExistingApp) - self.m_socket.error.connect(self.startApplication) - self.m_socket.connectToServer(pid, QIODevice.WriteOnly) - def connectToExistingApp(self): - if len(sys.argv)>1 and sys.argv[1] is not None: - self.m_socket.write(sys.argv[1]) - self.m_socket.bytesWritten.connect(self.quit) - else: - QMessageBox.warning(None, self.applicationName(), self.tr("The program is already running.")) - # Quit application in 250 ms - QTimer.singleShot(250, self.quit) - def show(self): - self.m_server.newConnection.connect(self.getNewConnection) - if self.mainWindow.minimized is None: - self.mainWindow.show() - def startApplication(self): - self.m_server = QLocalServer() - if self.m_server.listen(self.pid): - self.show() - else: - # Try one more time, now deleting the pid - QLocalServer.removeServer(self.pid) - if self.m_server.listen(self.pid): - self.show() - else: - QMessageBox.critical(None, self.tr("Error"), self.tr("Error listening the socket.")) - def getNewConnection(self): - self.new_socket = self.m_server.nextPendingConnection() - self.new_socket.readyRead.connect(self.readSocket) - def readSocket(self): - f = self.new_socket.readLine() - self.mainWindow.getArgsFromOtherInstance(str(f)) - self.mainWindow.activateWindow() - self.mainWindow.show() - diff --git a/scudcloud-1.0/scudcloud b/scudcloud-1.0/scudcloud index e251feb..198cf27 100755 --- a/scudcloud-1.0/scudcloud +++ b/scudcloud-1.0/scudcloud @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -import os, sys, signal +import fcntl, os, sys, signal, tempfile from PyQt4 import QtGui # Flexible install dir (we assume that 'lib' and 'resources' will be subdirs) @@ -9,7 +9,6 @@ INSTALL_DIR = os.path.dirname(os.path.realpath(__file__)) sys.path.append(os.path.join(INSTALL_DIR, 'lib')) from resources import Resources from scudcloud import ScudCloud -from qsingleapplication import QSingleApplication # The ScudCloud QMainWindow win = None @@ -17,10 +16,11 @@ win = None VERSION = '1.0.81' def main(): - global win; + global win Resources.INSTALL_DIR = INSTALL_DIR signal.signal(signal.SIGINT, exit) - app = QSingleApplication(sys.argv) + lock() + app = QtGui.QApplication(sys.argv) app.setApplicationName(Resources.APP_NAME) app.setWindowIcon(QtGui.QIcon(Resources.get_path('scudcloud.png'))) args = parse_arguments() @@ -33,10 +33,27 @@ def main(): print("Configuration directory "+args.confdir+" could not be created! Exiting...") raise SystemExit() win = ScudCloud(settings_path=settings_path) - app.singleStart(win, "scudcloud.pid") + if win.minimized is None: + win.show() win.restore() sys.exit(app.exec_()) +def lock(): + global fp + if 'XDG_RUNTIME_DIR' in os.environ and os.environ['XDG_RUNTIME_DIR']: + runtime_dir = os.environ['XDG_RUNTIME_DIR'] + else: + runtime_dir = tempfile.gettempdir() + pid_file = runtime_dir+'/'+Resources.APP_NAME+'.pid' + fp = open(pid_file, 'w') + try: + fcntl.lockf(fp, fcntl.LOCK_EX | fcntl.LOCK_NB) + except IOError: + # Another instance is running + warning = QtGui.QApplication(sys.argv) + QtGui.QMessageBox.warning(None, "ScudCloud", warning.tr("Another instance of ScudCloud is already running.")) + sys.exit(0) + def load_settings(confdir): if not os.path.isdir(confdir): os.makedirs(confdir) @@ -68,7 +85,7 @@ def exit(*args): if win is not None: win.exit() else: - QSingleApplication.quit() + QtGui.QApplication.quit() if __name__ == '__main__': main() |