view libervia/desktop_kivy/core/share_widget.py @ 493:b3cedbee561d

refactoring: rename `cagou` to `libervia.desktop_kivy` + update imports and names following backend changes
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 18:26:16 +0200
parents cagou/core/share_widget.py@203755bbe0fe
children
line wrap: on
line source

#!/usr/bin/env python3

#Libervia Desktop-Kivy
# Copyright (C) 2016-2021 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/>.


from pathlib import Path
from functools import partial
from libervia.backend.core import log as logging
from libervia.backend.core.i18n import _
from libervia.backend.tools.common import data_format
from libervia.frontends.tools import jid
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty, DictProperty, ObjectProperty
from kivy.metrics import dp
from .constants import Const as C
from libervia.desktop_kivy import G


log = logging.getLogger(__name__)


PLUGIN_INFO = {
    "name": _("share"),
    "main": "Share",
    "description": _("share a file"),
    "icon_symbol": "share",
}


class TextPreview(BoxLayout):
    """Widget previewing shared text"""
    text = StringProperty()


class ImagePreview(BoxLayout):
    """Widget previewing shared image"""
    path = StringProperty()
    reduce_layout = ObjectProperty()
    reduce_checkbox = ObjectProperty()

    def _check_image_cb(self, report_raw):
        self.report = data_format.deserialise(report_raw)
        if self.report['too_large']:
            self.reduce_layout.opacity = 1
            self.reduce_layout.height = self.reduce_layout.minimum_height + dp(10)
            self.reduce_layout.padding = [0, dp(5)]

    def _check_image_eb(self, failure_):
        log.error(f"Can't check image: {failure_}")

    def on_path(self, wid, path):
        G.host.bridge.image_check(
            path, callback=self._check_image_cb, errback=self._check_image_eb)

    def resize_image(self, data, callback, errback):

        def image_resize_cb(new_path):
            new_path = Path(new_path)
            log.debug(f"image {data['path']} resized at {new_path}")
            data['path'] = new_path
            data['cleaning_cb'] = lambda: new_path.unlink()
            callback(data)

        path = data['path']
        width, height = self.report['recommended_size']
        G.host.bridge.image_resize(
            path, width, height,
            callback=image_resize_cb,
            errback=errback
        )

    def get_filter(self):
        if self.report['too_large'] and self.reduce_checkbox.active:
            return self.resize_image
        else:
            return lambda data, callback, errback: callback(data)


class GenericPreview(BoxLayout):
    """Widget previewing shared image"""
    path = StringProperty()


class ShareWidget(BoxLayout):
    media_type = StringProperty()
    data = DictProperty()
    preview_box = ObjectProperty()

    def on_kv_post(self, wid):
        self.type, self.subtype = self.media_type.split('/')
        if self.type == 'text' and 'text' in self.data:
            self.preview_box.add_widget(TextPreview(text=self.data['text']))
        elif self.type == 'image':
            self.preview_box.add_widget(ImagePreview(path=self.data['path']))
        else:
            self.preview_box.add_widget(GenericPreview(path=self.data['path']))

    def close(self):
        G.host.close_ui()

    def get_filtered_data(self, callback, errback):
        """Apply filter if suitable, and call callback with with modified data"""
        try:
            get_filter = self.preview_box.children[0].get_filter
        except AttributeError:
            callback(self.data)
        else:
            filter_ = get_filter()
            filter_(self.data, callback=callback, errback=errback)

    def filter_data_cb(self, data, contact_jid, profile):
        chat_wid = G.host.do_action('chat', contact_jid, [profile])

        if self.type == 'text' and 'text' in self.data:
            text = self.data['text']
            chat_wid.message_input.text += text
        else:
            path = self.data['path']
            chat_wid.transfer_file(path, cleaning_cb=data.get('cleaning_cb'))
        self.close()

    def filter_data_eb(self, failure_):
        G.host.add_note(
            _("file filter error"),
            _("Can't apply filter to file: {msg}").format(msg=failure_),
            level=C.XMLUI_DATA_LVL_ERROR)

    def on_select(self, contact_button):
        contact_jid = jid.JID(contact_button.jid)
        self.get_filtered_data(
            partial(
                self.filter_data_cb,
                contact_jid=contact_jid,
                profile=contact_button.profile),
            self.filter_data_eb
        )

    def key_input(self, window, key, scancode, codepoint, modifier):
        if key == 27:
            return G.local_platform.on_key_back_share(self)