/* vim:set et sts=4: */
/* ibus-hangul - The Hangul Engine For IBus
 * Copyright (C) 2008-2009 Peng Huang <shawn.p.huang@gmail.com>
 * Copyright (C) 2009-2011 Choe Hwanjin <choe.hwanjin@gmail.com>
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <ibus.h>
#include <stdlib.h>

#include <stdio.h>         // fprintf : print_engines_xml(void)
#include <gio/gio.h>    // Gfile : sync_engines_xml()

#include <locale.h>
#include <hangul.h>

#include "i18n.h"
#include "engine.h"

#include "utils.h"

#define get_component_xml() \
                        (g_build_filename(DATA_DIR, "/ibus/component/hangul.xml", NULL))
#define get_home_engines_xml() \
                        (g_build_filename(g_get_home_dir (), "/.config/ibus-hangul/engines.xml", NULL))
#define get_default_engines_xml() \
                        (g_build_filename(IBUSHANGUL_DATADIR, "/engine/default.xml", NULL))

static IBusBus *bus = NULL;
static IBusFactory *factory = NULL;

/* options */
static gboolean ibus = FALSE;
static gboolean verbose = FALSE;
static gboolean xml = FALSE;

static const GOptionEntry entries[] =
{
    { "ibus", 'i', 0, G_OPTION_ARG_NONE, &ibus, "component is executed by ibus", NULL },
    { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "verbose", NULL },
    { "xml", 'x', 0, G_OPTION_ARG_NONE, &xml, "read hangul engine desc from xml file", NULL },
    { NULL },
};


static void
ibus_disconnected_cb (IBusBus  *bus,
                      gpointer  user_data)
{
    g_debug ("bus disconnected");
    ibus_quit ();
}


static void
start_component (void)
{
    GList* engines;
    GList* p;
    IBusComponent *component;
	IBusConfig* config;
    gboolean res;

    ibus_init ();

    bus = ibus_bus_new ();
    
    res = ibus_bus_is_connected (bus);
    if (!res) {
        g_warning ("Unable to connect to IBus");
        exit (2);
    }

    config = ibus_bus_get_config (bus);
    if (config == NULL) {
        g_warning ("Unable to connect to IBus config component");
        exit (3);
    }

    g_signal_connect (bus, "disconnected", G_CALLBACK (ibus_disconnected_cb), NULL);

    ibus_hangul_init (bus);

    component = ibus_component_new_from_file(get_component_xml());

    factory = ibus_factory_new (ibus_bus_get_connection (bus));

    engines = ibus_component_get_engines(component);
    for (p = engines; p != NULL; p = p->next)
    {
        IBusEngineDesc* engine = (IBusEngineDesc*)p->data;
        ibus_factory_add_engine(factory, ibus_engine_desc_get_name(engine), IBUS_TYPE_HANGUL_ENGINE);
    }

    if (ibus) {
        ibus_bus_request_name (bus, "org.freedesktop.IBus.Hangul", 0);
    }
    else {
        ibus_bus_register_component (bus, component);
    }

    g_object_unref (component);

    ibus_main ();

    ibus_hangul_exit ();
}

static void
sync_engines_xml(void)
{
    gboolean equal = FALSE;
    if ( g_file_test(get_home_engines_xml(), 
                            (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) ) {
        equal = compare_engine_xml (
                        ibus_xml_parse_file(get_home_engines_xml()),
                        ibus_xml_parse_file(get_default_engines_xml()));
        if (!equal) {
            GFile *gfile;
            gfile = g_file_new_for_path (get_home_engines_xml());
            if (!g_file_delete (gfile, NULL, NULL)) {
                g_print("error on file_delete\n");
            }
            g_object_unref(gfile);
        }
    }
}

static void
print_engines_xml(void)
{
    GString* output;
    XMLNode *node = NULL;
    if ( g_file_test(get_home_engines_xml(), 
                            (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) ) {
        node = ibus_xml_parse_file(get_home_engines_xml());
    }
    
    if (node == NULL) {
        node = ibus_xml_parse_file(get_default_engines_xml());
    }

    output = g_string_new("");

    ibus_xml_output (node, output);

    fprintf(stdout, "%s", output->str);

    ibus_xml_free(node);
    g_string_free(output, TRUE);
}

int
main (gint argc, gchar **argv)
{
    GError *error = NULL;
    GOptionContext *context;

    setlocale (LC_ALL, "");

    bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
    bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
    textdomain(GETTEXT_PACKAGE);

    context = g_option_context_new ("- ibus hangul engine component");

    g_option_context_add_main_entries (context, entries, "ibus-hangul");

    if (!g_option_context_parse (context, &argc, &argv, &error)) {
        g_print ("Option parsing failed: %s\n", error->message);
        exit (-1);
    }

    if (xml) {
        sync_engines_xml();
        print_engines_xml ();
        return 0;
    }

    start_component ();
    return 0;
}
