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

flashing_keyboard.py « flashing_keyboard - dev.gajim.org/gajim/gajim-plugins.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4ab2613a3d2c4f8f1009ce5e543bbd639aa50575 (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
# -*- coding: utf-8 -*-

from gi.repository import Gtk
from gi.repository import GObject
import subprocess

from common import gajim
from plugins import GajimPlugin
from plugins.helpers import log_calls, log
from plugins.gui import GajimPluginConfigDialog


class FlashingKeyboard(GajimPlugin):
    @log_calls('FlashingKeyboard')
    def init(self):
        self.description = _('Flashing keyboard led when there are unread messages.')
        self.config_dialog = FlashingKeyboardPluginConfigDialog(self)
        self.config_default_values = {
            'command1': ("xset led named 'Scroll Lock'", ''),
            'command2': ("xset -led named 'Scroll Lock'", ''),
            'flash': (True, ''),
        }

        self.is_active = None
        self.timeout = 500
        self.timeout_off = int(self.timeout / 2)
        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):
        if gajim.events.get_nb_systray_events():
            if self.id_0:
                return
            if self.config['flash']:
                self.id_0 = GObject.timeout_add(self.timeout, self.led_on)
            else:
                self.led_on()
                self.id_0 = True
        else:
            if self.id_0:
                if self.config['flash']:
                    GObject.source_remove(self.id_0)
                self.id_0 = None
                self.led_off()

    def led_on(self):
        subprocess.Popen('%s' % self.config['command1'], shell=True).wait()
        if self.config['flash']:
            GObject.timeout_add(self.timeout_off, self.led_off)
        return True

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

    @log_calls('FlashingKeyboard')
    def activate(self):
        gajim.events.event_added_subscribe(self.on_event_added)
        gajim.events.event_removed_subscribe(self.on_event_removed)
        if gajim.events.get_nb_systray_events():
            if self.config['flash']:
                self.id_0 = GObject.timeout_add(self.timeout, self.led_on)
            else:
                self.led_on()
                self.id_0 = True

    @log_calls('FlashingKeyboard')
    def deactivate(self):
        gajim.events.event_added_unsubscribe(self.on_event_added)
        gajim.events.event_removed_unsubscribe(self.on_event_removed)
        if self.id_0:
            GObject.source_remove(self.id_0)
            self.led_off()


class FlashingKeyboardPluginConfigDialog(GajimPluginConfigDialog):
    def init(self):
        self.GTK_BUILDER_FILE_PATH = self.plugin.local_file_path(
            'config_dialog.ui')
        self.xml = Gtk.Builder()
        self.xml.set_translation_domain('gajim_plugins')
        self.xml.add_objects_from_file(self.GTK_BUILDER_FILE_PATH,
            ['config_table'])
        config_table = self.xml.get_object('config_table')
        self.get_child().pack_start(config_table, True, True, 0)
        self.xml.connect_signals(self)

    def on_run(self):
        self.isactive = self.plugin.active
        if self.plugin.active:
            gajim.plugin_manager.deactivate_plugin(self.plugin)
        for name in ('command1', 'command2'):
            widget = self.xml.get_object(name)
            widget.set_text(self.plugin.config[name])
        widget = self.xml.get_object('flash_cb')
        widget.set_active(not self.plugin.config['flash'])

    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()
        widget = self.xml.get_object('flash_cb')
        self.plugin.config['flash'] = not widget.get_active()
        if self.isactive:
            gajim.plugin_manager.activate_plugin(self.plugin)
        GajimPluginConfigDialog.on_close_button_clicked(self, widget)