Exemple #1
0
class QSingleApplication(QApplication):
    def singleStart(self, mainWindow):
        self.mainWindow = mainWindow
        # Socket
        self.m_socket = QLocalSocket()
        self.m_socket.connected.connect(self.connectToExistingApp)
        self.m_socket.error.connect(self.startApplication)
        self.m_socket.connectToServer(self.applicationName(), 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.tr("Already running"), self.tr("The program is already running."))
            # Quit application in 250 ms
            QTimer.singleShot(250, self.quit)
    def startApplication(self):
        self.m_server = QLocalServer()
        if self.m_server.listen(self.applicationName()):
            self.m_server.newConnection.connect(self.getNewConnection)
            self.mainWindow.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()
Exemple #2
0
 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 singleStart(self, mainWindow):
     print "singleStart() function from QSingleApp got called."
     self.mainWindow = mainWindow
     # Socket
     self.m_socket = QLocalSocket()
     # Connected, error are signals that are being emitted
     self.m_socket.connected.connect(self.connectToExistingApp)
     self.m_socket.error.connect(self.startApplication)
     self.m_socket.connectToServer(self.applicationName(),
                                   QIODevice.WriteOnly)
Exemple #4
0
class QSingleApplication(QApplication):
    sock_file = 'ryo_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):
            if "--testnet" in sys.argv[1:]:
                print("Starting app... TESTNET MODE")
            else:
                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))
Exemple #5
0
 def singleStart(self, mainWindow):
     self.mainWindow = mainWindow
     # Socket
     self.m_socket = QLocalSocket()
     self.m_socket.connected.connect(self.connectToExistingApp)
     self.m_socket.error.connect(self.startApplication)
     self.m_socket.connectToServer(self.applicationName(), QIODevice.WriteOnly)
Exemple #6
0
 def _createSocket(self):
     from PySide.QtNetwork import QLocalSocket
     q = self.q
     ret = QLocalSocket(q)
     socketio.initsocket(ret)
     ret.error.connect(q.socketError)
     ret.connected.connect(q.connected)
     ret.disconnected.connect(q.disconnected)
     ret.readyRead.connect(self.readSocket)
     return ret
class QSingleApplication(QApplication):
    # Signal sent when another instance is launched with arguments
    argsReceived = QtCore.Signal((str, ))

    # Start the application,
    # either as a server (first instance) or as a client
    # others clients only send the argv they have been given and exit
    def singleStart(self, mainWindow):
        print "singleStart() function from QSingleApp got called."
        self.mainWindow = mainWindow
        # Socket
        self.m_socket = QLocalSocket()
        # Connected, error are signals that are being emitted
        self.m_socket.connected.connect(self.connectToExistingApp)
        self.m_socket.error.connect(self.startApplication)
        self.m_socket.connectToServer(self.applicationName(),
                                      QIODevice.WriteOnly)

    # used for the very first instance to create the main window and start server
    def startApplication(self):
        self.m_server = QLocalServer()
        print "startApplication() function from QSingleApplication got called"
        # Returns bool (True on success). Tells server to listen for incoming connections on 'name'
        if self.m_server.listen(self.applicationName()):
            print "Server is listening .... startApplication() function from QSingleApp"
            self.m_server.newConnection.connect(self.getNewConnection)
            # After emitting a signal, connecting it to the UI class function
            self.argsReceived.connect(self.mainWindow.receive_args)
            self.mainWindow.show()
        else:
            QMessageBox.critical(None, self.tr("Error"),
                                 self.tr("Error listening the socket."))

    # used by later instances to send argv to the main instance
    def connectToExistingApp(self):
        if len(sys.argv) > 1 and sys.argv[1] is not None:
            self.m_socket.write(sys.argv[1])
            print "exiting new app A"
            self.m_socket.bytesWritten.connect(self.quit)
        else:
            print "exiting new app B"
            self.m_socket.write("--show")
            self.m_socket.bytesWritten.connect(self.quit)

    def getNewConnection(self):
        print "getNewConnection() was called"
        self.mainWindow.show()
        self.new_socket = self.m_server.nextPendingConnection()
        self.new_socket.readyRead.connect(self.readSocket)

    def readSocket(self):
        print "readSocket() function in QSingleApplication.py"
        f = self.new_socket.readLine()
        self.argsReceived.emit(str(f))
class QSingleApplication(QApplication):
    # Signal sent when another instance is launched with arguments
    argsReceived = QtCore.Signal((str,))

    # Start the application,
    # either as a server (first instance) or as a client
    # others clients only send the argv they have been given and exit
    def singleStart(self, mainWindow):
        self.mainWindow = mainWindow
        # Socket
        self.m_socket = QLocalSocket()
        self.m_socket.connected.connect(self.connectToExistingApp)
        self.m_socket.error.connect(self.startApplication)
        self.m_socket.connectToServer(self.applicationName(), QIODevice.WriteOnly)

    # used for the very first instance to create the main window and start server
    def startApplication(self):
        self.m_server = QLocalServer()
        if self.m_server.listen(self.applicationName()):
            self.m_server.newConnection.connect(self.getNewConnection)
            self.argsReceived.connect(self.mainWindow.receive_args)
            self.mainWindow.show()
        else:
            QMessageBox.critical(None, self.tr("Error"), self.tr("Error listening the socket."))

    # used by later instances to send argv to the main instance
    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:
            self.m_socket.write("--show")
            self.m_socket.bytesWritten.connect(self.quit)

    def getNewConnection(self):
        self.mainWindow.show()
        self.new_socket = self.m_server.nextPendingConnection()
        self.new_socket.readyRead.connect(self.readSocket)

    def readSocket(self):
        f = self.new_socket.readLine()
        self.argsReceived.emit(str(f))
Exemple #9
0
    def send_message(self, message, callback=None):
        """
        Attempt to send a message to the previously running instance of the
        application. Returns True if the message is sent successfully, or False
        otherwise.
        Alternatively, if a callback is provided the function will return
        immediately and the boolean will be sent to the callback instead.
        """
        message = json.dumps(message)
        if sys.version_info.major == 3:
            packedLen = struct.pack("!I", len(message)).decode()
        else:
            packedLen = struct.pack("!I", len(message))
        message = packedLen + message

        # Create a socket.
        sock = QLocalSocket(self)

        # Build our helper functions.
        def error(err):
            """ Return False to the callback. """
            callback(False)

        def connected():
            """ Send our message. """
            sock.writeData(message, len(message))

        def bytesWritten(bytes):
            """ If we've written everything, close and return True. """
            if not sock.bytesToWrite():
                sock.close()
                callback(True)

        if callback:
            sock.error.connect(error)
            sock.connect.connect(connected)
            sock.bytesWritten.connect(bytesWritten)

        # Now connect.
        sock.connectToServer(self._app_id)

        if not callback:
            # Do things synchronously.
            connected = sock.waitForConnected(5000)
            if not connected:
                return False

            # Write it.
            sock.writeData(message, len(message))

            # Wait until we've written everything.
            while sock.bytesToWrite():
                success = sock.waitForBytesWritten(5000)
                if not success:
                    sock.close()
                    return False

            sock.close()
            return True