view 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
line wrap: on
line source

#!/usr/bin/env python3

# Desktop frontend for Libervia XMPP client
# Copyright (C) 2009-2024 Jérôme Poisson (goffi@goffi.org)

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import asyncio
from importlib import resources
import sys

from PyQt6 import uic
from PyQt6.QtWidgets import QApplication, QMainWindow
from qasync import QApplication, QEventLoop

from libervia.backend.core import log as logging
from libervia.backend.tools import config
from libervia.backend.tools.common import dynamic_import
from libervia.frontends.tools import aio

log = logging.getLogger(__name__)


class MainWindow(QMainWindow):

    def __init__(self):
        super().__init__()
        with resources.path("libervia.desktop.ui", "main_window.ui") as ui_file:
            uic.loadUi(ui_file, self)


class LiberviaDesktopApp:

    async def _init(self) -> None:
        await self.bridge.bridge_connect()
        await self.bridge.ready_get()
        version = await self.bridge.version_get()
        log.info(f"Connected to Libervia {version}.")

    def run(self) -> None:
        self.app = QApplication(sys.argv)
        self.event_loop = QEventLoop(self.app)
        asyncio.set_event_loop(self.event_loop)

        self.app_close_event = asyncio.Event()
        self.app.aboutToQuit.connect(self.app_close_event.set)

        self.main_window = MainWindow()
        main_config = config.parse_main_conf(log_filenames=True)
        bridge_name = config.config_get(main_config, "", "bridge", "dbus")

        if "dbus" in bridge_name:
            aio.install_glib_asyncio_iteration()
        bridge_module = dynamic_import.bridge(bridge_name, "libervia.frontends.bridge")
        if bridge_module is None:
            log.error(f"Can't import {bridge_name} bridge")
            sys.exit(3)
        else:
            log.debug(f"Loading {bridge_name} bridge")
        self.bridge = bridge_module.AIOBridge()
        self.main_window.show()
        self.event_loop.create_task(self._init())
        with self.event_loop:
            self.event_loop.run_until_complete(self.app_close_event.wait())
            self.event_loop.close()