#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 
# Graphical tool to change keyboard settings. Language filters can
# be added to the command line:
#
#  $ python lang-config.py --langfilt fr,en
# 
# By default locales without translation files in /usr/share/locale/
# are removed. You can force inclusion with "--all-locales". You can
# also force removal of locales perviously selected (default: kept)
# with "--no-keep-locales".
# 
# Author: JM. Philippe <jean-michel.philippe@doudoulinux.org>
#################################
# This file is part of DoudouLinux.
#
# DoudouLinux is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# DoudouLinux is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with DoudouLinux.  If not, see <http://www.gnu.org/licenses/>.

############################
import sys, os, gtk, gettext

############################
# register the lib directory
if os.path.isfile("/usr/lib/system-tools/lang_widget.py"):
    # use the system directory
    sys.path.append("/usr/lib/system-tools")
else:
    # use the source code directory
    ScriptDir = os.path.dirname(__file__)
    sys.path.append( os.path.join(ScriptDir, "lib") )

from lang_widget import LangWidget
from common import DataDir

############################
# get language filter
langfilt = None
all_locales = "--all-locales" in sys.argv
keep_locales = "--no-keep-locales" not in sys.argv
if "--langfilt" in sys.argv:
    index = sys.argv.index("--langfilt")
    if len(sys.argv) <= index+1:
        print "ERROR: wrong number of arguments"
        sys.exit()
    langfilt = sys.argv[index+1].split(",")

############################
# create a window
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_size_request(600, 380)
window.set_title( _("System language") )
window.set_icon_from_file(  os.path.join(DataDir, "icons", "locales.png") )

############################
# create containers
container = gtk.VBox()
buttons = gtk.HBox()

############################
# create the ok button
ok = gtk.Button(stock="gtk-apply")
ok.set_use_stock(True)
buttons.pack_end(ok, False, False, 4)

############################
# create the cancel button
cancel = gtk.Button(stock="gtk-cancel")
cancel.set_use_stock(True)
buttons.pack_end(cancel, False, False, 4)

############################
# add the language widget
lang = LangWidget(
    window, localefilt=langfilt, all_locales=all_locales, keep_locales=keep_locales
)
#window.add( lang.root() )
container.pack_start(lang.root(), True, True, 4)
container.pack_start(buttons, False, False, 4)
window.add(container)

############################
# set bindings
def quit(*args):
    lang.set_language()
    gtk.main_quit()
def cancelquit(*args):
    gtk.main_quit()
window.connect("delete-event", cancelquit)
window.connect("destroy", cancelquit)
ok.connect("clicked", quit)
cancel.connect("clicked", cancelquit)

############################
# show window
window.show_all()
window.present()

gtk.main()
