plugin.py 4.71 KB
Newer Older
1 2 3
from gi.repository import Gtk
from gi.repository import GdkPixbuf

4
from gajim.common import app
5

6 7 8
from gajim.plugins import GajimPlugin
from gajim.plugins.gui import GajimPluginConfigDialog
from gajim.plugins.helpers import log_calls
9
from gajim.plugins.plugins_i18n import _
10 11 12 13 14 15 16 17 18 19 20

class QuickRepliesPlugin(GajimPlugin):

    @log_calls('QuickRepliesPlugin')
    def init(self):

        self.description = _('Plugin for quick replies')
        self.config_dialog = QuickRepliesPluginConfigDialog(self)
        self.chat_control = None
        self.gui_extension_points = {
            'chat_control_base': (self.connect_with_chat_control,
21 22 23
                                    self.disconnect_from_chat_control),
            'chat_control_base_update_toolbar': (self.update_button_state,
                                    None)}
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
        self.config_default_values = {
            'entry1': ('Hello!', ''),
            'entry2': ('How are you?', ''),
            'entry3': ('Good bye.', ''),
            'entry4': ('', ''),
            'entry5': ('', ''),
            'entry6': ('', ''),
            'entry7': ('', ''),
            'entry8': ('', ''),
            'entry9': ('', ''),
            'entry10': ('', ''),
            }
        self.controls = []

    @log_calls('QuickRepliesPlugin')
    def connect_with_chat_control(self, chat_control):

        self.chat_control = chat_control
42 43
        base = Base(self, chat_control)
        self.controls.append(base)
44 45 46 47 48

    @log_calls('QuickRepliesPlugin')
    def disconnect_from_chat_control(self, chat_control):

        for control in self.controls:
49
            control.disconnect_from_chat_control()
50 51
        self.controls = []

52
    @log_calls('QuickRepliesPlugin')
53 54 55 56
    def update_button_state(self, chat_control):
        for base in self.controls:
            if base.chat_control != chat_control:
                continue
57
            base.button.set_sensitive(chat_control.contact.show != 'offline' \
58
            and app.connections[chat_control.account].connected > 0)
59

Dicson's avatar
Dicson committed
60

61 62 63 64 65 66 67
class Base(object):

    def __init__(self, plugin, chat_control):

        self.plugin = plugin
        self.chat_control = chat_control
        self.create_button()
68
        self.create_menu()
69 70 71

    def create_button(self):

72 73 74 75
        actions_hbox = self.chat_control.xml.get_object('hbox')
        self.button = Gtk.MenuButton(label=None, stock=None, use_underline=True)
        self.button.get_style_context().add_class(
            'chatcontrol-actionbar-button')
76
        self.button.set_property('relief', Gtk.ReliefStyle.NONE)
77
        self.button.set_property('can-focus', False)
78
        img = Gtk.Image()
79
        img_path = self.plugin.local_file_path('quick_replies.png')
80
        pixbuf = GdkPixbuf.Pixbuf.new_from_file(img_path)
81
        img.set_from_pixbuf(pixbuf)
82 83
        self.button.set_image(img)
        self.button.set_tooltip_text(_('Quick replies'))
84 85
        actions_hbox.pack_start(self.button, False, False , 0)
        actions_hbox.reorder_child(self.button,
86
            len(actions_hbox.get_children()) - 2)
87 88 89 90 91 92
        self.button.show()

    def on_insert(self, widget, text):

        text = text.rstrip() + ' '
        message_buffer = self.chat_control.msg_textview.get_buffer()
93
        self.chat_control.msg_textview.remove_placeholder()
94 95 96 97 98
        message_buffer.insert_at_cursor(text)
        self.chat_control.msg_textview.grab_focus()

    def create_menu(self):

99
        self.menu = Gtk.Menu()
100

101
        for count in range(1, 11):
102 103 104
            text = self.plugin.config['entry' + str(count)]
            if not text:
                continue
105
            item = Gtk.MenuItem(text)
106 107 108
            item.connect('activate', self.on_insert, text)
            self.menu.append(item)
        self.menu.show_all()
109
        self.button.set_popup(self.menu)
110

111
    def disconnect_from_chat_control(self):
112
        actions_hbox = self.chat_control.xml.get_object('hbox')
113 114
        actions_hbox.remove(self.button)

Dicson's avatar
Dicson committed
115

116 117 118 119 120 121
class QuickRepliesPluginConfigDialog(GajimPluginConfigDialog):

    def init(self):

        self.GTK_BUILDER_FILE_PATH = self.plugin.local_file_path(
            'config_dialog.ui')
122
        self.xml = Gtk.Builder()
123 124 125
        self.xml.set_translation_domain('gajim_plugins')
        self.xml.add_objects_from_file(self.GTK_BUILDER_FILE_PATH, ['table1'])
        hbox = self.xml.get_object('table1')
126
        self.get_child().pack_start(hbox, True, True, 0)
127 128 129 130
        self.xml.connect_signals(self)

    def on_run(self):

131
        for count in range(1, 11):
132 133 134 135 136
            self.xml.get_object('entry' + str(count)).set_text(
                self.plugin.config['entry' + str(count)])

    def entry_changed(self, widget):

137
        name = Gtk.Buildable.get_name(widget)
138 139 140
        self.plugin.config[name] = widget.get_text()
        for control in self.plugin.controls:
            control.create_menu()