~listen-devel/listen/devel

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
import keybinder
from plugins.generic import GenericPlugin
from player import Player
from widget.preference import HelperConfigureDialog
from config import config

class GlobalKeys(GenericPlugin):

    PLUGIN_NAME = "Global Key Bindings"
    PLUGIN_DESC = "Plugin to set global key bindings"
    PLUGIN_VERSION = "0.1"
    PLUGIN_AUTHOR = "Alexander Godlewski <alex@implode.ca>"
    PLUGIN_WEBSITE = ""
    
    func = {
        "globalkey_previous": Player.previous,
        "globalkey_pause": Player.pause,
        "globalkey_play": Player.playpause,
        "globalkey_stop": Player.stop,
        "globalkey_next": Player.next
        }
    
    def __init__(self):
        GenericPlugin.__init__(self)
        
        self.autoconnect(config, "config-changed", self.__on_config_changed)

        self.__bind(config.get("plugins", "globalkey_previous", "<Super>Z"), "Previous", self.func["globalkey_previous"])
        self.__bind(config.get("plugins", "globalkey_pause", "<Super>X"), "Pause", self.func["globalkey_pause"])
        self.__bind(config.get("plugins", "globalkey_play", "<Super>C"), "Play", self.func["globalkey_play"])
        self.__bind(config.get("plugins", "globalkey_stop", "<Super>V"), "Stop", self.func["globalkey_stop"])
        self.__bind(config.get("plugins", "globalkey_next", "<Super>B"), "Next", self.func["globalkey_next"])
        
        config.set("plugins", "globalkey_previous_last", config.get("plugins", "globalkey_previous", "<Super>Z"))
        config.set("plugins", "globalkey_pause_last", config.get("plugins", "globalkey_pause", "<Super>X"))
        config.set("plugins", "globalkey_play_last", config.get("plugins", "globalkey_play", "<Super>C"))
        config.set("plugins", "globalkey_stop_last", config.get("plugins", "globalkey_stop", "<Super>V"))
        config.set("plugins", "globalkey_next_last", config.get("plugins", "globalkey_next", "<Super>B"))
        
    def __handle_callback(self, text, callback):
    	self.logdebug(text)
    	callback()
    	
    def __bind(self, key, target, callback):
        try:
            self.__try_unbind(key)
        except:
            pass
            
        keybinder.bind(key, lambda(text): self.__handle_callback(text, callback), "Global binding for %s pressed(%s)" % (target, key))
        self.logdebug("Bound %s" % key)

    def __try_unbind(self, key):
        try:
            self.logdebug("Unbinding %s" % key)
            keybinder.unbind(key)
            self.logdebug("Unbound %s" % key)
        except:
            self.logdebug("Did not unbind %s" % key)

    def delete_thyself(self):
        for key in (
            config.get("plugins", "globalkey_previous", "<Super>Z"), 
            config.get("plugins", "globalkey_pause", "<Super>X"), 
            config.get("plugins", "globalkey_play", "<Super>C"), 
            config.get("plugins", "globalkey_stop", "<Super>V"), 
            config.get("plugins", "globalkey_next", "<Super>B")
            ):
            
            self.__try_unbind(key)
            
        GenericPlugin.delete_thyself(self)
        
    @staticmethod   
    def on_configure():
        GlobalKeysDialog()
        
    def __on_config_changed(self, dispatcher, section, option, value):
        if section == "plugins" and option.find("globalkey_") == 0 and option.find("_last") == -1:
            self.__try_unbind(config.get(section, option + "_last", value))            
            self.__bind(config.get(section, option, value), option, self.func[option])
            config.set(section, option + "_last", value)
        
class GlobalKeysDialog(HelperConfigureDialog):
    def __init__(self):
        HelperConfigureDialog.__init__(self, "Global Keys")
        self.add(self.make_lentry("Previous", "plugins", "globalkey_previous", "<Super>Z"))
        self.add(self.make_lentry("Pause", "plugins", "globalkey_pause", "<Super>X"))
        self.add(self.make_lentry("Play", "plugins", "globalkey_play", "<Super>C"))
        self.add(self.make_lentry("Stop", "plugins", "globalkey_stop", "<Super>V"))
        self.add(self.make_lentry("Next", "plugins", "globalkey_next", "<Super>B"))
        self.show_all()