コード例 #1
0
 def accept(self):
     logging.debug('connection accepted')
     conn = self.server.nextPendingConnection()
     conn.waitForReadyRead()
     key = str(conn.read(36).decode())
     memory = QSharedMemory()
     memory.setKey(key)
     memory.attach()
     logging.debug('attached to memory %s with size %s'%(key, memory.size()))
     atexit.register(memory.detach)
     self.conns.append(conn)
     self.shared_mems.append(memory)
     conn.readyRead.connect(lambda: self.read_from(conn, memory))
     conn.disconnected.connect(memory.detach)
     conn.write(b'ok')
コード例 #2
0
class MusicianSuite(QApplication):
    signalReceived = pyqtSignal(object)

    def __init__(self, argv, key):
        super().__init__(argv)
        QSharedMemory(key).attach()
        self._memory = QSharedMemory(self)
        self._memory.setKey(key)
        if self._memory.attach():
            self._running = True
        else:
            self._running = False
            if not self._memory.create(1):
                raise RuntimeError(self._memory.errorString())
        self._key = key
        self._timeout = 1000
        self._server = QLocalServer(self)
        if not self.isRunning():
            self._server.newConnection.connect(self.handleMessage)
            self._server.listen(self._key)

        self._settings = QSettings("Raul Sangonzalo", "Musician Suite")

        # self._settings.setValue("currentDatabase", None)  # testing purposes
        self.initialPrompt = InitialPrompt()
        self.initialPrompt.databaseSelected.connect(self.reloadInit)

    def isRunning(self):
        return self._running

    def handleMessage(self):
        socket = self._server.nextPendingConnection()
        if socket.waitForReadyRead(self._timeout):
            self.signalReceived.emit(
                socket.readAll().data().decode('utf-8'))
            socket.disconnectFromServer()
        else:
            Qt.QDebug(socket.errorString())

    def sendNotification(self, message):
        if self.isRunning():
            socket = QLocalSocket(self)
            socket.connectToServer(self._key, QIODevice.WriteOnly)
            if not socket.waitForConnected(self._timeout):
                print(socket.errorString())
                return False
            if not isinstance(message, bytes):
                message = message.encode('utf-8')
            socket.write(message)
            if not socket.waitForBytesWritten(self._timeout):
                print(socket.errorString())
                return False
            socket.disconnectFromServer()
            return True
        return False

    def init(self):
        """True initiation of the App, if only one instance"""

        if self._settings.value("currentDatabase") == None:
            self.initialPrompt.show()

        else:  # check in case user moves the location of db
            location = self._settings.value("currentDatabase")
            print("------>", location)
            if not os.path.exists(location):
                self.initialPrompt.show()
            else:
                self.mainWindow = MusicMainWindow()

                # mac madness
                os.chdir(os.path.dirname(os.path.abspath(__file__)))
                resourcesPath = os.getcwd()
                resourcesPath = os.path.join(resourcesPath, "resources")
                if not os.path.exists(resourcesPath):
                    os.mkdir(resourcesPath)

                self.MAIN_ICON = QIcon(os.path.join(resourcesPath, "test.ico"))
                self.ICON0 = QIcon(
                    QPixmap(os.path.join(resourcesPath, "icon0.png")))
                self.ICON1 = QIcon(
                    QPixmap(os.path.join(resourcesPath, "icon1.png")))
                self.RECORD_ICON = QIcon(
                    QPixmap(os.path.join(resourcesPath, "record.png")))

                self.widget = QWidget()
                self.trayIcon = QSystemTrayIcon(self.MAIN_ICON, self.widget)
                self.trayIcon.setToolTip("Musician Suite")
                self.trayIconMenu = QMenu()

                self.songListAction = QAction(self.ICON0, "Song List")
                self.recordIdeasAction = QAction(self.ICON1, "Recorded Ideas")
                self.recordAction = QAction(self.RECORD_ICON, "Record now!")
                self.exitAction = QAction("Exit")

                self.trayIconMenu.addAction(self.songListAction)
                self.trayIconMenu.addAction(self.recordIdeasAction)
                self.trayIconMenu.addAction(self.recordAction)
                self.trayIconMenu.addAction(self.exitAction)

                self.trayIcon.setContextMenu(self.trayIconMenu)

                self.trayIcon.show()

                self.trayIcon.activated.connect(self.iconDoubleClickMain)
                self.trayIconMenu.triggered.connect(self.messagePopup)

                self.mainWindow.stackedWidget.setCurrentIndex(0)

                self.signalReceived.connect(self.showMainWindow)
                self.mainWindow.show()

    def showMainWindow(self):
        self.mainWindow.show()

    def reloadInit(self):
        print("I am in reload Init")
        self.initialPrompt.hide()
        self.init()

    def iconDoubleClickMain(self, reason):
        if reason == QSystemTrayIcon.DoubleClick:
            self.mainWindow.stackedWidget.setCurrentIndex(0)
            self.mainWindow.show()

    def messagePopup(self, action):
        """Pressing the menu and toast """
        if action == self.songListAction:
            if not self.mainWindow.isVisible():
                self.mainWindow.stackedWidget.setCurrentIndex(0)
                self.mainWindow.show()
        elif action == self.recordIdeasAction:
            if not self.mainWindow.isVisible():
                self.mainWindow.stackedWidget.setCurrentIndex(1)
                self.mainWindow.show()
        elif action == self.recordAction:
            self.startRecordingHook()
        else:
            self.exit()

    def startRecordingHook(self):
        self.mainWindow.stackedWidget.setCurrentIndex(1)
        self.mainWindow.show()
        listRows = self.mainWindow.recordIdeas.recordedIdeasListWidget.count()
        print(listRows)
        self.mainWindow.recordIdeas.recordedIdeasListWidget.setCurrentRow(
            listRows-1)
        self.mainWindow.recordIdeas.record()