#!/usr/bin/python2
import re
import subprocess
import os

#get the current userset layout
vm_layout = "%s/.config/qubes-keyboard-layout.rc" % os.getenv("HOME")
cur_user_layout = ""
if os.path.isfile(vm_layout):
    try:
        fp = open(vm_layout,'r')
        cur_user_layout = fp.read().strip()
        fp.close()
    except IOError:
        pass

# get the list of all xkb layouts
xkb_rules_base = "/usr/share/X11/xkb/rules/base.lst"

layouts = []
xkb_rules = open( xkb_rules_base)

l = xkb_rules.readline()
while not (l.startswith("!") and l.startswith("! layout")):
    l = xkb_rules.readline()

#layouts section starts

l = xkb_rules.readline().strip()
while l.strip() != "":
    pair = re.split("\s+", l, maxsplit=1)
    layouts.append(pair)
    l = xkb_rules.readline().strip()

xkb_rules.close()

# create  list dialog
zenity_cmd = ['zenity', '--list', '--text', "Choose desired keyboard layout.", '--radiolist']
zenity_cmd.append( "--title='Keyboard Layout'")
zenity_cmd.append("--height=500")
zenity_cmd.append("--width=350")
zenity_cmd.append('--column')
zenity_cmd.append("")
zenity_cmd.append('--column')
zenity_cmd.append("Code")
zenity_cmd.append('--column')
zenity_cmd.append("Full name")

if cur_user_layout == "":
    zenity_cmd.append("TRUE")
else:
    zenity_cmd.append("FALSE")

zenity_cmd.append("none")
zenity_cmd.append("Qubes default layout")

for l in layouts:
    if l[0] == cur_user_layout:
        zenity_cmd.append("TRUE")
    else:
        zenity_cmd.append("FALSE")
    zenity_cmd.append(l[0])
    zenity_cmd.append(l[1])

p = subprocess.Popen(zenity_cmd, stdout=subprocess.PIPE)
choice = p.communicate()[0]
choice = choice.strip()

#apply the choice

if choice == "none":
    # Reset user defined keyboard settings
    try:
        subprocess.check_call(['gsettings', 'reset', 'org.gnome.libgnomekbd.keyboard', 'layouts'])
        fp = open(vm_layout, 'w')
        fp.write("")
        fp.close()
    except:
        pass

    subprocess.check_call(['/etc/X11/xinit/xinitrc.d/qubes-keymap.sh'])

elif choice != "":
    subprocess.check_call(['setxkbmap', choice])
    try:
        subprocess.check_call(['gsettings', 'set', 'org.gnome.libgnomekbd.keyboard', 'layouts', "['%s']" % choice])
        file = open(vm_layout, 'w+')
        file.write(choice)
        file.close()
    except:
        pass

