view cagou/core/platform_/base.py @ 353:19422bbd9c8e

core (widgets handler): refactoring: - CagouWidget now has class properties (to be overridden when needed) which indicate how if the widget handle must add a wrapping ScreenManager (global_screen_manager) or show all instances of the class in a Carousel (collection_carousel). If none of those options is used, a ScrollView will be wrapping the widget, to be sure that the widget will be resized correctly when necessary (without it, the widget could still be drawn in the backround when the size is too small and overflow on the WidgetWrapper, this would be the case with WidgetSelector) - some helper methods/properties have been added to CagouWidget. Check docstrings for details - better handling of (in)visible widget in WidgetsHandler - thanks to the new wrapping ScrollView, WidgetSelect will show scroll bars if the available space is too small. - bugs fixes
author Goffi <goffi@goffi.org>
date Fri, 17 Jan 2020 18:44:35 +0100
parents 83697218b9b2
children 4d3a0c4f2430
line wrap: on
line source

#!/usr/bin/env python3

# Cagou: desktop/mobile frontend for Salut à Toi XMPP client
# Copyright (C) 2016-2019 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 webbrowser
from pathlib import Path
from urllib.parse import urlparse
from kivy.config import Config as KivyConfig
from sat.core.log import getLogger
from sat.tools.common import files_utils
from sat_frontends.quick_frontend.quick_widgets import QuickWidget
from cagou import G


log = getLogger(__name__)


class Platform:
    """Base class to handle platform specific behaviours"""

    def init_platform(self):
        # we don't want multi-touch emulation with mouse

        # this option doesn't make sense on Android and cause troubles, so we only activate
        # it for other platforms (cf. https://github.com/kivy/kivy/issues/6229)
        KivyConfig.set('input', 'mouse', 'mouse,disable_multitouch')

    def on_app_build(self, wid):
        pass

    def on_host_init(self, host):
        pass

    def on_initFrontendState(self):
        pass

    def on_pause(self):
        pass

    def on_resume(self):
        pass

    def on_stop(self):
        pass

    def open_url(self, url, wid=None):
        """Open an URL in the way appropriate for the platform

        @param url(str): URL to open
        @param wid(CagouWidget, None): widget requesting the opening
            it may influence the way the URL is opened
        """
        parsed_url = urlparse(url)
        if parsed_url.scheme == "aesgcm" and wid is not None:
            # aesgcm files need to be decrypted first
            # so we download them before opening
            name = Path(parsed_url.path).name.strip() or "unnamed_file"
            log.info(f"downloading/decrypting file {name!r}")
            dest_path = files_utils.get_unique_name(Path(G.host.downloads_dir)/name)
            quick_widget = G.host.getAncestorWidget(wid, QuickWidget)
            if quick_widget is None:
                log.error("Can't find ancestor QuickWidget")
                return
            profile = quick_widget.profile
            G.host.bridge.fileDownloadComplete(
                url,
                str(dest_path),
                '',
                profile,
                callback=webbrowser.open,
                errback=G.host.errback
            )
        else:
            webbrowser.open(url)