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