use QLocalSocket and QLocalServer

use QLocalSocket and QLocalServer instead of lockfile
This allows to open window of running instance
when user is trying to start new instance.
This commit is contained in:
Wojtek Widomski 2023-06-24 13:46:42 +02:00
parent 11bb32cbae
commit 4e87b1f8e4
2 changed files with 15 additions and 11 deletions

View file

@ -22,6 +22,7 @@
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtCore import QCoreApplication as QC
from PyQt5.QtNetwork import QLocalServer, QLocalSocket
import sys
import os
@ -37,6 +38,8 @@ dist_path = '/usr/lib/python3/dist-packages/'
if dist_path not in sys.path:
sys.path.append(dist_path)
app_id = "io.github.evilsocket.opensnitch"
from opensnitch.service import UIService
from opensnitch.config import Config
from opensnitch.utils import Themes, Utils, Versions, Message
@ -47,7 +50,6 @@ from opensnitch import auth
def on_exit():
server.stop(0)
lockfile.unlock()
app.quit()
sys.exit(0)
@ -100,15 +102,16 @@ Examples:
try:
app = QtWidgets.QApplication(sys.argv)
lockfile = QtCore.QLockFile(os.path.join(xdg_runtime_dir, 'osui.lock'))
if not lockfile.tryLock(100):
Message.ok(
QC.translate("stats", "Error"),
QC.translate("stats", "OpenSnitch UI is already running!"),
QtWidgets.QMessageBox.Warning
)
raise Exception("GUI already running, exiting.")
localsocket = QLocalSocket()
localsocket.connectToServer(app_id)
if localsocket.waitForConnected():
raise Exception("GUI already running, opening its window and exiting.")
else:
localserver = QLocalServer()
localserver.removeServer(app_id)
localserver.listen(app_id)
if hasattr(QtCore.Qt, 'AA_UseHighDpiPixmaps'):
app.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True)
@ -136,6 +139,7 @@ Examples:
maxmsglen = cfg.getMaxMsgLength()
service = UIService(app, on_exit)
localserver.newConnection.connect(service.OpenWindow)
# @doc: https://grpc.github.io/grpc/python/grpc.html#server-object
server = grpc.server(futures.ThreadPoolExecutor(),
options=(
@ -192,5 +196,3 @@ Examples:
on_exit()
except Exception as e:
print(e)
finally:
lockfile.unlock()

View file

@ -881,3 +881,5 @@ class UIService(ui_pb2_grpc.UIServicer, QtWidgets.QGraphicsObject):
return node_iter
def OpenWindow(self):
self._stats_dialog.show()