playerdo / playerdo / install.py

"""
Installation utilities for keyboard shortcuts
"""
from subprocess import Popen, PIPE, call
import sys


KEYBINDINGS_PREFIX = "/desktop/gnome/keybindings"

def force_unicode(s):
    if type(s) is not unicode:
        return s.decode('UTF-8')
    else:
        return s

def install_gnome():
    """
    Creates stub keybindings for player_do commands in GNOME, and launches a GUI
    editor for the user to set keys.
    """
    already_installed = set()
    max_count = 0
    for d in get_gnome_keybindings():
        if d.startswith('custom'):
            action = get_gconf_val(KEYBINDINGS_PREFIX + "/" + d + "/action")
            if action.startswith("player_do "):
                already_installed.add(action.strip())
            n = int(d[len('custom'):])
            if n > max_count:
                max_count = n

    commands = ['play', 'pause', 'playpause', 'stop', 'next', 'prev']
    n = max_count + 1
    for cmd in commands:
        action = "player_do " + cmd
        name = "Player - " + cmd
        if action not in already_installed:
            install_action("custom%d" % n, action, name)
            n += 1

    launch_keybinding_editor()


def get_gnome_keybindings():
    p = Popen(["gconftool-2", "--all-dirs", KEYBINDINGS_PREFIX], stdout=PIPE)
    stdout, stderr = p.communicate(None)
    if p.returncode != 0:
        raise Exception("Could not use gconftool to manipulate settings")
    retval = []
    stdout = force_unicode(stdout)
    for x in stdout.split("\n"):
        x = x.strip()
        if x.startswith(KEYBINDINGS_PREFIX):
            retval.append(x[len(KEYBINDINGS_PREFIX) + 1:])
    return retval


def get_gconf_val(key):
    p = Popen(["gconftool-2", "--get", key], stdout=PIPE)
    stdout, stderr = p.communicate(None)
    if p.returncode != 0:
        raise Exception("Could not use gconftool to manipulate settings")
    return force_unicode(stdout)


def set_gconf_val(key, val):
    p = Popen(["gconftool-2", "--type", "string", "--set", key, val], stdout=PIPE)
    stdout, stderr = p.communicate(None)
    if p.returncode != 0:
        raise Exception("Could not use gconftool to manipulate settings")


def install_action(keybinding_name, action, name):
    set_gconf_val(KEYBINDINGS_PREFIX + "/" + keybinding_name + "/" + "action", action)
    set_gconf_val(KEYBINDINGS_PREFIX + "/" + keybinding_name + "/" + "binding", "")
    set_gconf_val(KEYBINDINGS_PREFIX + "/" + keybinding_name + "/" + "name", name)
    sys.stdout.write("Keybinding slot for action '%s' created\n" % action)


def launch_keybinding_editor():
    p = Popen(["which", "gnome-keybinding-properties"], stdout=PIPE)
    stdout, stderr = p.communicate(None)
    val = force_unicode(stdout).strip()
    if val == "":
        raise Exception("Can't find program gnome-keybinding-properties to configure keys")
    call(["nohup %s &" % val], shell=True, stdout=open("/dev/null"), stderr=open("/dev/null"))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.