Listener to allow our Qt form to get focus when another instance of the application is open. Based off this nice reimplmentation of MySingleApplication: http://stackoverflow.com/a/12712362/2679626
| 4338 | |
| 4339 | |
| 4340 | class MySingleApplication(QtGui.QApplication): |
| 4341 | """ |
| 4342 | Listener to allow our Qt form to get focus when another instance of the |
| 4343 | application is open. |
| 4344 | |
| 4345 | Based off this nice reimplmentation of MySingleApplication: |
| 4346 | http://stackoverflow.com/a/12712362/2679626 |
| 4347 | """ |
| 4348 | |
| 4349 | # Unique identifier for this application |
| 4350 | uuid = '6ec0149b-96e1-4be1-93ab-1465fb3ebf7c' |
| 4351 | |
| 4352 | def __init__(self, *argv): |
| 4353 | super(MySingleApplication, self).__init__(*argv) |
| 4354 | id = MySingleApplication.uuid |
| 4355 | |
| 4356 | self.server = None |
| 4357 | self.is_running = False |
| 4358 | |
| 4359 | socket = QLocalSocket() |
| 4360 | socket.connectToServer(id) |
| 4361 | self.is_running = socket.waitForConnected() |
| 4362 | |
| 4363 | # Cleanup past crashed servers |
| 4364 | if not self.is_running: |
| 4365 | if socket.error() == QLocalSocket.ConnectionRefusedError: |
| 4366 | socket.disconnectFromServer() |
| 4367 | QLocalServer.removeServer(id) |
| 4368 | |
| 4369 | socket.abort() |
| 4370 | |
| 4371 | # Checks if there's an instance of the local server id running |
| 4372 | if self.is_running: |
| 4373 | # This should be ignored, singleinstance.py will take care of exiting me. |
| 4374 | pass |
| 4375 | else: |
| 4376 | # Nope, create a local server with this id and assign on_new_connection |
| 4377 | # for whenever a second instance tries to run focus the application. |
| 4378 | self.server = QLocalServer() |
| 4379 | self.server.listen(id) |
| 4380 | self.server.newConnection.connect(self.on_new_connection) |
| 4381 | |
| 4382 | def __del__(self): |
| 4383 | if self.server: |
| 4384 | self.server.close() |
| 4385 | |
| 4386 | def on_new_connection(self): |
| 4387 | if myapp: |
| 4388 | myapp.appIndicatorShow() |
| 4389 | |
| 4390 | |
| 4391 | def init(): |