#!/usr/bin/python3
#
# Copyright (c) 2012-2013 - The IBus Cangjie authors
#
# This file is part of ibus-cangjie, the IBus Cangjie input method engine.
#
# ibus-cangjie 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.
#
# ibus-cangjie 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 ibus-cangjie.  If not, see <http://www.gnu.org/licenses/>.

import argparse
parser = argparse.ArgumentParser(description="Cangjie input method setup")
parser.add_argument("engine", help="Input method engine to set up",
                    choices=("cangjie", "quick"))
args = parser.parse_args()

from gi.repository import GLib

# This has to be set before importing Gtk, or else the WM_CLASS property for
# the window will be wrong
GLib.set_prgname('ibus-setup-%s' % args.engine)


import locale

import gi
gi.require_version('Gtk','4.0')
gi.require_version('IBus','1.0')

from gi.repository import Gtk
from gi.repository import IBus

from ibus_cangjie.setup import Setup

gettext_package = "ibus-cangjie"
localedir = "/usr/share/locale"
datadir = "/usr/share/ibus-cangjie"
version = "2.5.0"

locale.bindtextdomain(gettext_package, localedir)
locale.bind_textdomain_codeset(gettext_package, "UTF-8")

# FIXME: Find a way to de-hardcode the gettext package
_ = lambda msg: locale.dgettext(gettext_package, msg)

try:
    bus = IBus.Bus()

except:
    message = [_("IBus daemon is not running."),
               _("%s engine settings cannot be saved."
                 % args.engine.capitalize())]
    dialog = Gtk.MessageDialog(type = Gtk.MessageType.ERROR,
                               buttons = Gtk.ButtonsType.CLOSE,
                               message_format = "\n".join(message))
    dialog.run()

    import sys
    sys.exit(1)

Setup(args.engine, datadir, gettext_package, version).run()
