Init
This commit is contained in:
101
main.py
Normal file
101
main.py
Normal file
@@ -0,0 +1,101 @@
|
||||
|
||||
|
||||
from PySide6.QtCore import QStandardPaths, QDataStream, QByteArray, QIODevice, Signal, Qt
|
||||
from PySide6.QtNetwork import QLocalServer, QLocalSocket
|
||||
from PySide6.QtWidgets import QApplication
|
||||
|
||||
import qasync
|
||||
import sys
|
||||
import asyncio
|
||||
import os
|
||||
import platform
|
||||
import argparse
|
||||
from pathlib import Path
|
||||
|
||||
from src.logs import configure_logging
|
||||
from windows.main_window import MainWindow
|
||||
|
||||
|
||||
class SingleApplication(QApplication):
|
||||
# Signal émis lorsque des fichiers sont reçus d'une instance secondaire
|
||||
files_received = Signal(list)
|
||||
|
||||
def __init__(self, app_id, args):
|
||||
super().__init__(args)
|
||||
self.app_id = app_id
|
||||
self.server = None
|
||||
self.is_primary_instance = self.try_connect_to_primary()
|
||||
|
||||
if self.is_primary_instance:
|
||||
# C'est la première instance, on crée un serveur local
|
||||
self.server = QLocalServer()
|
||||
self.server.newConnection.connect(self.handle_new_connection)
|
||||
if not self.server.listen(self.app_id):
|
||||
# En cas d'erreur (serveur déjà existant mais zombie), on le supprime et on réessaie
|
||||
QLocalServer.removeServer(self.app_id)
|
||||
self.server.listen(self.app_id)
|
||||
|
||||
def try_connect_to_primary(self):
|
||||
"""Essaie de se connecter à l'instance primaire de l'application"""
|
||||
socket = QLocalSocket()
|
||||
socket.connectToServer(self.app_id, QIODevice.OpenModeFlag.WriteOnly)
|
||||
|
||||
if socket.waitForConnected(500):
|
||||
# Récupérer les arguments pour les envoyer à l'instance primaire
|
||||
args = sys.argv[1:] if len(sys.argv) > 1 else []
|
||||
|
||||
# Envoyer les arguments à l'instance primaire
|
||||
stream = QDataStream(socket)
|
||||
stream.writeQString(";".join(args))
|
||||
socket.flush()
|
||||
socket.disconnectFromServer()
|
||||
return False # Ce n'est pas l'instance primaire
|
||||
return True # C'est l'instance primaire
|
||||
|
||||
def handle_new_connection(self):
|
||||
"""Gère une nouvelle connexion d'une instance secondaire"""
|
||||
socket = self.server.nextPendingConnection()
|
||||
if socket.waitForReadyRead(1000):
|
||||
stream = QDataStream(socket)
|
||||
args_str = stream.readQString()
|
||||
args = args_str.split(";") if args_str else []
|
||||
|
||||
# Émettre un signal pour informer l'application des fichiers à ouvrir
|
||||
if args:
|
||||
self.files_received.emit(args)
|
||||
|
||||
socket.disconnectFromServer()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Analyser les arguments de la ligne de commande
|
||||
parser = argparse.ArgumentParser(description='Application PySide6', allow_abbrev=False)
|
||||
parser.add_argument('--dev', action='store_true', help='Active le mode développement avec logs')
|
||||
parser.add_argument('files', nargs='*', help='Fichiers à ouvrir')
|
||||
args, unknown = parser.parse_known_args()
|
||||
|
||||
# Configurer le logging en fonction du mode
|
||||
configure_logging(args.dev)
|
||||
|
||||
os.environ["QTWEBENGINE_CHROMIUM_FLAGS"] = "--enable-gpu-rasterization --ignore-gpu-blocklist"
|
||||
if args.dev:
|
||||
os.environ["QTWEBENGINE_REMOTE_DEBUGGING"] = "4000"
|
||||
|
||||
app_id = "OxAPP25"
|
||||
app = SingleApplication(app_id, sys.argv)
|
||||
|
||||
event_loop = qasync.QEventLoop(app)
|
||||
asyncio.set_event_loop(event_loop)
|
||||
|
||||
app_close_event = asyncio.Event()
|
||||
app.aboutToQuit.connect(app_close_event.set)
|
||||
|
||||
window = MainWindow()
|
||||
# Connecter le signal de fichiers reçus à une méthode de traitement
|
||||
app.files_received.connect(window.handle_files)
|
||||
if args.files:
|
||||
window.handle_files(args.files)
|
||||
window.show()
|
||||
|
||||
with event_loop:
|
||||
event_loop.run_until_complete(app_close_event.wait())
|
||||
Reference in New Issue
Block a user