#! /usr/bin/env python
# -*- coding: utf-8 -*-
# fonts-tweak-tool
# Copyright (c) 2011-2012 Jian Ni <jni@redhat.com>
# Copyright (c) 2012 Red Hat, Inc.
#
# Authors:
#   Akira TAGOH  <tagoh@redhat.com>
#
# This library is free software: you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation, either
# version 3 of the License, or (at your option) any later version.
#
# This library 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 this program.  If not, see <http://www.gnu.org/licenses/>.

import gettext
import gi
import locale
import os
from fontstweak.fontstweak import FontsTweak
from fontstweak.util import FontsTweakUtil
from fontstweak.aliasui import FontsTweakAliasUI
from fontstweak.propui import FontsTweakPropUI
from fontstweak.langui import FontsTweakLangUI
from gi.repository import Easyfc
from gi.repository import Gtk

_ = gettext.gettext

try:
    locale.setlocale(locale.LC_ALL, '')
except Locale.Error, e:
    os.environ['LC_ALL'] = 'C'
    locale.setlocale(locale.LC_ALL, '')

gettext.bind_textdomain_codeset(FontsTweak.GETTEXT_PACKAGE, locale.nl_langinfo(locale.CODESET))
gettext.bindtextdomain(FontsTweak.GETTEXT_PACKAGE, FontsTweak.LOCALEDIR)
gettext.textdomain(FontsTweak.GETTEXT_PACKAGE)

Easyfc.init()
config = Easyfc.Config()
config.set_name("fontstweak")
try:
    # This method is available on libeasyfc >= 0.8
    config.set_migration(True)
except AttributeError, e:
    pass

try:
    config.load()
except gi._glib.GError, e:
    if e.domain != 'ezfc-error-quark' or e.code != 7:
        raise

dlgui = Gtk.Dialog('fonts-tweak-tool', None, Gtk.ResponseType.CLOSE)
content_area = dlgui.get_content_area()

aliasui_builder = FontsTweakUtil.create_builder('fonts-tweak-alias.ui')
aliasui_builder.connect_signals(FontsTweakAliasUI(config, aliasui_builder, dlgui))
try:
    ezfcver = Easyfc.version()
    if ezfcver >= '0.8':
        propui_builder = FontsTweakUtil.create_builder('fonts-tweak-prop.ui')
        propui_builder.connect_signals(FontsTweakPropUI(config, propui_builder, dlgui))
        propui = propui_builder.get_object('fonts-prop-ui')
except AttributeError:
    propui = None

langui_builder = FontsTweakUtil.create_builder('fonts-tweak-lang.ui')
langui_builder.connect_signals(FontsTweakLangUI(langui_builder, dlgui))

aliasui = aliasui_builder.get_object('fonts-alias-ui')
langui = langui_builder.get_object('fonts-lang-order-ui')

tabs = Gtk.Notebook()
tabs.append_page(aliasui, Gtk.Label(_('Fonts Aliases')))
if propui is not None:
    tabs.append_page(propui, Gtk.Label(_('Fonts Properties')))
tabs.append_page(langui, Gtk.Label(_('Language Ordering')))
content_area.set_border_width(2)
content_area.add(tabs)

close = Gtk.Button(stock=Gtk.STOCK_CLOSE)
close.connect('clicked', Gtk.main_quit)
dlgui.add_action_widget(close, Gtk.ResponseType.CLOSE)

dlgui.show_all()

Gtk.main()
