mirror of
https://github.com/ciromattia/kcc
synced 2025-12-13 17:56:30 +00:00
Allow only one instance + messaging
This commit is contained in:
60
kcc.py
60
kcc.py
@@ -27,7 +27,7 @@ import sys
|
||||
import os
|
||||
try:
|
||||
# noinspection PyUnresolvedReferences
|
||||
from PyQt4 import QtGui
|
||||
from PyQt4 import QtCore, QtGui, QtNetwork
|
||||
except ImportError:
|
||||
print "ERROR: PyQT4 is not installed!"
|
||||
exit(1)
|
||||
@@ -41,13 +41,67 @@ elif sys.platform.startswith('linux'):
|
||||
else:
|
||||
from kcc import KCC_ui
|
||||
|
||||
|
||||
class QApplicationMessaging(QtGui.QApplication):
|
||||
def __init__(self, argv):
|
||||
QtGui.QApplication.__init__(self, argv)
|
||||
self._memory = QtCore.QSharedMemory(self)
|
||||
self._memory.setKey('KCC')
|
||||
if self._memory.attach():
|
||||
self._running = True
|
||||
else:
|
||||
self._running = False
|
||||
if not self._memory.create(1):
|
||||
raise RuntimeError(self._memory.errorString().toLocal8Bit().data())
|
||||
self._key = 'KCC'
|
||||
self._timeout = 1000
|
||||
self._server = QtNetwork.QLocalServer(self)
|
||||
if not self.isRunning():
|
||||
self._server.newConnection.connect(self.handleMessage)
|
||||
self._server.listen(self._key)
|
||||
|
||||
def isRunning(self):
|
||||
return self._running
|
||||
|
||||
def handleMessage(self):
|
||||
socket = self._server.nextPendingConnection()
|
||||
if socket.waitForReadyRead(self._timeout):
|
||||
self.emit(QtCore.SIGNAL('messageFromOtherInstance'), socket.readAll().data())
|
||||
|
||||
def sendMessage(self, message):
|
||||
if self.isRunning():
|
||||
socket = QtNetwork.QLocalSocket(self)
|
||||
socket.connectToServer(self._key, QtCore.QIODevice.WriteOnly)
|
||||
if not socket.waitForConnected(self._timeout):
|
||||
return False
|
||||
socket.write(message.encode('utf8'))
|
||||
if not socket.waitForBytesWritten(self._timeout):
|
||||
return False
|
||||
socket.disconnectFromServer()
|
||||
return True
|
||||
return False
|
||||
|
||||
freeze_support()
|
||||
APP = QtGui.QApplication(sys.argv)
|
||||
APP = QApplicationMessaging(sys.argv)
|
||||
if APP.isRunning():
|
||||
if len(sys.argv) > 1:
|
||||
APP.sendMessage('Araise!')
|
||||
APP.sendMessage(sys.argv[1].decode(sys.getfilesystemencoding()))
|
||||
sys.exit(0)
|
||||
else:
|
||||
messageBox = QtGui.QMessageBox()
|
||||
icon = QtGui.QIcon()
|
||||
icon.addPixmap(QtGui.QPixmap(':/Icon/icons/comic2ebook.png'), QtGui.QIcon.Normal, QtGui.QIcon.Off)
|
||||
messageBox.setWindowIcon(icon)
|
||||
QtGui.QMessageBox.critical(messageBox, 'KCC - Error', 'KCC is already running!', QtGui.QMessageBox.Ok)
|
||||
sys.exit(1)
|
||||
KCC = QtGui.QMainWindow()
|
||||
UI = KCC_ui.Ui_KCC()
|
||||
UI.setupUi(KCC)
|
||||
GUI = KCC_gui.Ui_KCC(UI, KCC)
|
||||
GUI = KCC_gui.Ui_KCC(UI, KCC, APP)
|
||||
KCC.setWindowTitle("Kindle Comic Converter " + __version__)
|
||||
KCC.show()
|
||||
KCC.raise_()
|
||||
if len(sys.argv) > 1:
|
||||
GUI.handleMessage(sys.argv[1].decode(sys.getfilesystemencoding()))
|
||||
sys.exit(APP.exec_())
|
||||
|
||||
@@ -317,7 +317,7 @@ class Ui_KCC(object):
|
||||
try:
|
||||
str(dname)
|
||||
except Exception:
|
||||
QtGui.QMessageBox.critical(MainWindow, 'KCC Error', "Path cannot contain non-ASCII characters.",
|
||||
QtGui.QMessageBox.critical(MainWindow, 'KCC - Error', 'Path cannot contain non-ASCII characters.',
|
||||
QtGui.QMessageBox.Ok)
|
||||
return
|
||||
if str(dname) != "":
|
||||
@@ -349,7 +349,7 @@ class Ui_KCC(object):
|
||||
try:
|
||||
str(fname)
|
||||
except Exception:
|
||||
QtGui.QMessageBox.critical(MainWindow, 'KCC Error', "Path cannot contain non-ASCII characters.",
|
||||
QtGui.QMessageBox.critical(MainWindow, 'KCC - Error', 'Path cannot contain non-ASCII characters.',
|
||||
QtGui.QMessageBox.Ok)
|
||||
return
|
||||
if str(fname) != "":
|
||||
@@ -577,7 +577,7 @@ class Ui_KCC(object):
|
||||
GUI.JobList.scrollToBottom()
|
||||
|
||||
def showDialog(self, message):
|
||||
QtGui.QMessageBox.critical(MainWindow, 'KCC Error', message, QtGui.QMessageBox.Ok)
|
||||
QtGui.QMessageBox.critical(MainWindow, 'KCC - Error', message, QtGui.QMessageBox.Ok)
|
||||
|
||||
def updateProgressbar(self, new=False, status=False):
|
||||
if new == "status":
|
||||
@@ -643,7 +643,11 @@ class Ui_KCC(object):
|
||||
'GammaSlider': float(self.GammaValue)*100}))
|
||||
self.settings.sync()
|
||||
|
||||
def __init__(self, UI, KCC):
|
||||
def handleMessage(self, message):
|
||||
#TODO
|
||||
print message
|
||||
|
||||
def __init__(self, UI, KCC, APP):
|
||||
global GUI, MainWindow
|
||||
GUI = UI
|
||||
MainWindow = KCC
|
||||
@@ -714,6 +718,7 @@ class Ui_KCC(object):
|
||||
self.addMessage('Cannot find <a href="http://www.7-zip.org/download.html">7za</a>!'
|
||||
' Processing of CB7/7Z files will be disabled.', 'warning')
|
||||
|
||||
APP.connect(APP, QtCore.SIGNAL('messageFromOtherInstance'), self.handleMessage)
|
||||
GUI.BasicModeButton.clicked.connect(self.modeBasic)
|
||||
GUI.AdvModeButton.clicked.connect(self.modeAdvanced)
|
||||
GUI.DirectoryButton.clicked.connect(self.selectDir)
|
||||
|
||||
Reference in New Issue
Block a user