comparison libervia/desktop/core/main.py @ 0:9754c1d6f4f4 draft default tip

Libervia Desktop initial commit
author Goffi <goffi@goffi.org>
date Fri, 12 Jul 2024 14:13:36 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:9754c1d6f4f4
1 #!/usr/bin/env python3
2
3 # Desktop frontend for Libervia XMPP client
4 # Copyright (C) 2009-2024 Jérôme Poisson (goffi@goffi.org)
5
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU Affero General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU Affero General Public License for more details.
15
16 # You should have received a copy of the GNU Affero General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 import asyncio
20 from importlib import resources
21 import sys
22
23 from PyQt6 import uic
24 from PyQt6.QtWidgets import QApplication, QMainWindow
25 from qasync import QApplication, QEventLoop
26
27 from libervia.backend.core import log as logging
28 from libervia.backend.tools import config
29 from libervia.backend.tools.common import dynamic_import
30 from libervia.frontends.tools import aio
31
32 log = logging.getLogger(__name__)
33
34
35 class MainWindow(QMainWindow):
36
37 def __init__(self):
38 super().__init__()
39 with resources.path("libervia.desktop.ui", "main_window.ui") as ui_file:
40 uic.loadUi(ui_file, self)
41
42
43 class LiberviaDesktopApp:
44
45 async def _init(self) -> None:
46 await self.bridge.bridge_connect()
47 await self.bridge.ready_get()
48 version = await self.bridge.version_get()
49 log.info(f"Connected to Libervia {version}.")
50
51 def run(self) -> None:
52 self.app = QApplication(sys.argv)
53 self.event_loop = QEventLoop(self.app)
54 asyncio.set_event_loop(self.event_loop)
55
56 self.app_close_event = asyncio.Event()
57 self.app.aboutToQuit.connect(self.app_close_event.set)
58
59 self.main_window = MainWindow()
60 main_config = config.parse_main_conf(log_filenames=True)
61 bridge_name = config.config_get(main_config, "", "bridge", "dbus")
62
63 if "dbus" in bridge_name:
64 aio.install_glib_asyncio_iteration()
65 bridge_module = dynamic_import.bridge(bridge_name, "libervia.frontends.bridge")
66 if bridge_module is None:
67 log.error(f"Can't import {bridge_name} bridge")
68 sys.exit(3)
69 else:
70 log.debug(f"Loading {bridge_name} bridge")
71 self.bridge = bridge_module.AIOBridge()
72 self.main_window.show()
73 self.event_loop.create_task(self._init())
74 with self.event_loop:
75 self.event_loop.run_until_complete(self.app_close_event.wait())
76 self.event_loop.close()