flashing_keyboard.py 4.06 KB
Newer Older
1
import subprocess
Dicson's avatar
Dicson committed
2

Yann Leboulanger's avatar
Yann Leboulanger committed
3 4
from gi.repository import Gtk
from gi.repository import GObject
Dicson's avatar
Dicson committed
5

6 7
from gajim.common import app
from gajim.plugins import GajimPlugin
8
from gajim.plugins.helpers import log_calls
9
from gajim.plugins.gui import GajimPluginConfigDialog
Dicson's avatar
Dicson committed
10

11 12 13 14 15
# Since Gajim 1.1.0 _() has to be imported
try:
    from gajim.common.i18n import _
except ImportError:
    pass
Dicson's avatar
Dicson committed
16 17 18 19

class FlashingKeyboard(GajimPlugin):
    @log_calls('FlashingKeyboard')
    def init(self):
Dicson's avatar
Dicson committed
20
        self.description = _('Flashing keyboard led when there are unread messages.')
Dicson's avatar
Dicson committed
21 22
        self.config_dialog = FlashingKeyboardPluginConfigDialog(self)
        self.config_default_values = {
Yann Leboulanger's avatar
Yann Leboulanger committed
23
            'command1': ("xset led named 'Scroll Lock'", ''),
24 25 26
            'command2': ("xset -led named 'Scroll Lock'", ''),
            'flash': (True, ''),
        }
Dicson's avatar
Dicson committed
27 28 29

        self.is_active = None
        self.timeout = 500
Yann Leboulanger's avatar
Yann Leboulanger committed
30
        self.timeout_off = int(self.timeout / 2)
Dicson's avatar
Dicson committed
31 32 33 34 35 36 37 38 39 40
        self.id_0 = None

    def on_event_added(self, event):
        if event.show_in_systray:
            self.flash_trigger()

    def on_event_removed(self, event_list):
        self.flash_trigger()

    def flash_trigger(self):
41
        if app.events.get_nb_systray_events():
Dicson's avatar
Dicson committed
42 43
            if self.id_0:
                return
44 45 46 47 48
            if self.config['flash']:
                self.id_0 = GObject.timeout_add(self.timeout, self.led_on)
            else:
                self.led_on()
                self.id_0 = True
Dicson's avatar
Dicson committed
49 50
        else:
            if self.id_0:
51 52
                if self.config['flash']:
                    GObject.source_remove(self.id_0)
Dicson's avatar
Dicson committed
53
                self.id_0 = None
54
                self.led_off()
Dicson's avatar
Dicson committed
55 56 57

    def led_on(self):
        subprocess.Popen('%s' % self.config['command1'], shell=True).wait()
58 59
        if self.config['flash']:
            GObject.timeout_add(self.timeout_off, self.led_off)
Dicson's avatar
Dicson committed
60 61 62 63 64 65 66
        return True

    def led_off(self):
        subprocess.Popen('%s' % self.config['command2'], shell=True).wait()

    @log_calls('FlashingKeyboard')
    def activate(self):
67 68 69
        app.events.event_added_subscribe(self.on_event_added)
        app.events.event_removed_subscribe(self.on_event_removed)
        if app.events.get_nb_systray_events():
70 71 72 73 74
            if self.config['flash']:
                self.id_0 = GObject.timeout_add(self.timeout, self.led_on)
            else:
                self.led_on()
                self.id_0 = True
Dicson's avatar
Dicson committed
75 76 77

    @log_calls('FlashingKeyboard')
    def deactivate(self):
78 79
        app.events.event_added_unsubscribe(self.on_event_added)
        app.events.event_removed_unsubscribe(self.on_event_removed)
Dicson's avatar
Dicson committed
80
        if self.id_0:
Yann Leboulanger's avatar
Yann Leboulanger committed
81
            GObject.source_remove(self.id_0)
82
            self.led_off()
Dicson's avatar
Dicson committed
83

Dicson's avatar
Dicson committed
84

Dicson's avatar
Dicson committed
85 86 87
class FlashingKeyboardPluginConfigDialog(GajimPluginConfigDialog):
    def init(self):
        self.GTK_BUILDER_FILE_PATH = self.plugin.local_file_path(
Yann Leboulanger's avatar
Yann Leboulanger committed
88 89
            'config_dialog.ui')
        self.xml = Gtk.Builder()
90
        self.xml.set_translation_domain('gajim_plugins')
Dicson's avatar
Dicson committed
91
        self.xml.add_objects_from_file(self.GTK_BUILDER_FILE_PATH,
Dicson's avatar
Dicson committed
92
            ['config_table'])
Dicson's avatar
Dicson committed
93
        config_table = self.xml.get_object('config_table')
Yann Leboulanger's avatar
Yann Leboulanger committed
94
        self.get_child().pack_start(config_table, True, True, 0)
Dicson's avatar
Dicson committed
95 96 97 98 99
        self.xml.connect_signals(self)

    def on_run(self):
        self.isactive = self.plugin.active
        if self.plugin.active:
100
            app.plugin_manager.deactivate_plugin(self.plugin)
101
        for name in ('command1', 'command2'):
Dicson's avatar
Dicson committed
102 103
            widget = self.xml.get_object(name)
            widget.set_text(self.plugin.config[name])
104 105
        widget = self.xml.get_object('flash_cb')
        widget.set_active(not self.plugin.config['flash'])
Dicson's avatar
Dicson committed
106 107 108 109 110 111

    def on_close_button_clicked(self, widget):
        widget = self.xml.get_object('command1')
        self.plugin.config['command1'] = widget.get_text()
        widget = self.xml.get_object('command2')
        self.plugin.config['command2'] = widget.get_text()
112 113
        widget = self.xml.get_object('flash_cb')
        self.plugin.config['flash'] = not widget.get_active()
Dicson's avatar
Dicson committed
114
        if self.isactive:
115
            app.plugin_manager.activate_plugin(self.plugin)
116
        GajimPluginConfigDialog.on_close_button_clicked(self, widget)