Welcome to mirror list, hosted at ThFree Co, Russian Federation.

plugin.py « quick_replies - dev.gajim.org/gajim/gajim-plugins.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b6fcf6edf57e91e3fa0062be752053c9abd98fbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# This file is part of Gajim.
#
# Gajim is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Gajim 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.

from __future__ import annotations

from typing import cast

import json
from pathlib import Path
from functools import partial

from gi.repository import Gio
from gi.repository import GLib
from gi.repository import Gtk

from gajim.common import app
from gajim.common import configpaths

from gajim.gtk.message_actions_box import MessageActionsBox
from gajim.gtk.message_input import MessageInputTextView

from gajim.plugins import GajimPlugin
from gajim.plugins.plugins_i18n import _

from quick_replies.quick_replies import DEFAULT_DATA
from quick_replies.gtk.config import ConfigDialog


class QuickRepliesPlugin(GajimPlugin):
    def init(self) -> None:
        self.description = _('Adds a menu with customizable quick replies')
        self.config_dialog = partial(ConfigDialog, self)
        self.gui_extension_points = {
            'message_actions_box': (self._message_actions_box_created, None),
        }
        self._button = None
        self.quick_replies = self._load_quick_replies()

    def deactivate(self) -> None:
        assert self._button is not None
        self._button.destroy()
        del self._button

    def _message_actions_box_created(self,
                                     message_actions_box: MessageActionsBox,
                                     gtk_box: Gtk.Box
                                     ) -> None:

        self._button = QuickRepliesButton(
            self,
            message_actions_box.msg_textview)
        gtk_box.pack_start(self._button, False, False, 0)
        self._button.show()

    @staticmethod
    def _load_quick_replies() -> list[str]:
        try:
            data_path = Path(configpaths.get('PLUGINS_DATA'))
        except KeyError:
            # PLUGINS_DATA was added in 1.0.99.1
            return DEFAULT_DATA

        path = data_path / 'quick_replies' / 'quick_replies'
        if not path.exists():
            return DEFAULT_DATA

        with path.open('r') as file:
            quick_replies = json.load(file)
        return quick_replies

    @staticmethod
    def _save_quick_replies(quick_replies: list[str]) -> None:
        try:
            data_path = Path(configpaths.get('PLUGINS_DATA'))
        except KeyError:
            # PLUGINS_DATA was added in 1.0.99.1
            return

        path = data_path / 'quick_replies'
        if not path.exists():
            path.mkdir(parents=True)

        filepath = path / 'quick_replies'
        with filepath.open('w') as file:
            json.dump(quick_replies, file)

    def set_quick_replies(self, quick_replies: list[str]) -> None:
        self.quick_replies = quick_replies
        self._save_quick_replies(quick_replies)
        assert self._button is not None
        self._button.update_menu()


class QuickRepliesButton(Gtk.MenuButton):
    def __init__(self,
                 plugin: QuickRepliesPlugin,
                 message_input: MessageInputTextView
                 ) -> None:

        Gtk.MenuButton.__init__(self)
        self.get_style_context().add_class('chatcontrol-actionbar-button')
        self.set_property('relief', Gtk.ReliefStyle.NONE)
        self.set_can_focus(False)
        plugin_path = Path(__file__).parent
        img_path = plugin_path.resolve() / 'quick_replies.png'
        img = Gtk.Image.new_from_file(str(img_path))
        self.set_image(img)
        self.set_tooltip_text(_('Quick Replies'))

        self._plugin = plugin
        self._message_input = message_input

        self._menu = Gio.Menu()
        self._popover = Gtk.Popover()
        self._popover.bind_model(self._menu)
        self.set_popover(self._popover)

        self.update_menu()

    def update_menu(self) -> None:
        self._menu.remove_all()

        # Add config item
        action_data = GLib.Variant('s', 'plugin-configuration')
        menu_item = Gio.MenuItem()
        menu_item.set_label(_('Manage Replies…'))
        menu_item.set_attribute_value('action-data', action_data)
        self._menu.append_item(menu_item)

        # Add quick replies
        for reply in self._plugin.quick_replies:
            assert isinstance(reply, str)
            action_data = GLib.Variant('s', reply)
            menu_item = Gio.MenuItem()
            menu_item.set_label(reply)
            menu_item.set_attribute_value('action-data', action_data)
            self._menu.append_item(menu_item)

        menu_buttons = self._get_menu_buttons()
        for button in menu_buttons:
            button.connect(
                'clicked',
                self._on_button_clicked,
                menu_buttons.index(button))

    def _on_button_clicked(self, _button: Gtk.MenuButton, index: int) -> None:
        variant = self._menu.get_item_attribute_value(
            index, 'action-data')
        if variant.get_string() == 'plugin-configuration':
            self._popover.popdown()
            self._plugin.config_dialog(app.window)
            return

        message_buffer = self._message_input.get_buffer()
        message_buffer.insert_at_cursor(variant.get_string().rstrip() + ' ')
        self._popover.popdown()
        self._message_input.grab_focus()

    def _get_menu_buttons(self) -> list[Gtk.ModelButton]:
        stack = cast(Gtk.Stack, self._popover.get_children()[0])
        menu_section_box = cast(Gtk.Box, stack.get_children()[0])
        box = cast(Gtk.Box, menu_section_box.get_children()[0])
        items = cast(list[Gtk.ModelButton], box.get_children())
        return items