forked from sumoprojects/SumoGUIWallet
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathQSingleApplication.py
70 lines (57 loc) · 2.51 KB
/
QSingleApplication.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/python
# -*- coding: utf-8 -*-
## Copyright (c) 2017, The Sumokoin Project (www.sumokoin.org)
'''
QSingleApplication is a wrapper class for creating single interface
of appliaction
'''
from __future__ import print_function
import sys, os
from PySide.QtGui import QApplication
from PySide.QtCore import QIODevice, QTimer
from PySide.QtNetwork import QLocalServer, QLocalSocket
from utils.common import getSockDir, makeDir
DATA_DIR = makeDir(os.path.join(getSockDir(), 'OmbreGUIWallet'))
class QSingleApplication(QApplication):
sock_file = 'ombre_wallet_sock'
if sys.platform == 'win32':
sock_file = "\\\\.\\pipe\\%s" % sock_file
elif sys.platform == 'darwin':
sock_file = os.path.join(DATA_DIR, '.%s' % sock_file)
else:
sock_file = os.path.join(getSockDir(), sock_file)
def singleStart(self, appMain):
self.appMain = appMain
# Socket
self.m_socket = QLocalSocket()
self.m_socket.connected.connect(self.connectToExistingApp)
self.m_socket.error.connect(lambda:self.startApplication(first_start=True))
self.m_socket.connectToServer(self.sock_file, QIODevice.WriteOnly)
def connectToExistingApp(self):
# Quit application in 250 ms
QTimer.singleShot(250, self.quit)
print( "App is already running.", file=sys.stderr )
def startApplication(self, first_start=True):
self.m_server = QLocalServer()
if self.m_server.listen(self.sock_file):
print( "Starting app..." )
self.appMain.run()
else:
if not first_start:
print( "Error listening the socket. App can't start!", file=sys.stderr )
QTimer.singleShot(250, self.quit)
return
# remove the listener path file and try to restart app one more time
print( "Error listening the socket. Try to restart application...", file=sys.stderr )
if sys.platform != 'win32':
try:
os.unlink(self.sock_file)
except Exception, err:
print( err, file=sys.stderr )
QTimer.singleShot(250, lambda : self.startApplication(first_start=False))
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.appMain.processURLProtocol(str(f))