/***************************************************************************
 *   Copyright (C) 2010~2012 by CSSlayer                                   *
 *                                                                         *
 *   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 St, Fifth Floor, Boston, MA 02110-1301, USA.              *
 ***************************************************************************/

#include <gtk/gtk.h>
#include <langinfo.h>
#include <libintl.h>
#include <locale.h>
#include "config.h"
#include "config_widget.h"
#include <fcitx-config/fcitx-config.h>

#define BUFF_SIZE 128
gboolean getRunCmdOutput(const char *command, char *output, int max_size)
{

    FILE   *stream;
    if ((stream = popen(command, "r" )) == NULL)
        return FALSE;
    int ret = fread(output, sizeof(char), max_size, stream);
    if ( ret <= 0)
    {
        pclose(stream);
        return FALSE;
    }
    pclose(stream);
    return TRUE;
}

static void
fcitx_config_app_activate (GApplication *application)
{
    printf("fcitx_config_app_activate  1\n");
    GList* list = gtk_application_get_windows (GTK_APPLICATION(application));
    if (list)
    {
        printf("fcitx_config_app_activate  2\n");
        gtk_window_present (GTK_WINDOW (list->data));
    }
    else {
        printf("fcitx_config_app_activate  3\n");
        GtkWidget* window ;
        GtkWidget* content_area;
        GtkWidget *label;
        GtkWidget *hbox;
        GtkWidget *image;
        window = gtk_dialog_new_with_buttons("输入法",NULL,GTK_DIALOG_MODAL,
        "确定",
        GTK_RESPONSE_ACCEPT,
        "取消",
        GTK_RESPONSE_REJECT,
        NULL);
        label = gtk_label_new("确定要重启输入法么？");
        image = gtk_image_new_from_stock(GTK_STOCK_DIALOG_INFO,GTK_ICON_SIZE_DIALOG);

        content_area = gtk_dialog_get_content_area (GTK_DIALOG (window));
        hbox = gtk_hbox_new(FALSE,5);
        gtk_container_set_border_width(GTK_CONTAINER(hbox),10);
        gtk_container_add (GTK_CONTAINER (hbox), image);
        gtk_container_add (GTK_CONTAINER (hbox), label);
        gtk_container_add (GTK_CONTAINER (content_area), hbox);
//        gtk_window_set_title(GTK_WINDOW(window), "输入法");
        gtk_application_add_window(GTK_APPLICATION(application), GTK_WINDOW(window));
        gtk_window_set_icon_name(GTK_WINDOW(window), "fcitx");
        gtk_widget_show_all ((window));
        GtkResponseType result = gtk_dialog_run(GTK_DIALOG(window));

        if (result ==  GTK_RESPONSE_ACCEPT)
        {
            char cmd[BUFF_SIZE] = "/usr/bin/killlall fcitx -9";
            char buf[BUFF_SIZE] = {0};
            if(FALSE==getRunCmdOutput(cmd,buf,BUFF_SIZE))
                perror("killlall");

            gtk_widget_hide (window);

            printf("fcitx_config_app_activate  4\n");
            usleep(100000);
            fcitx_utils_launch_restart();
        }
        gtk_widget_destroy(window);
        g_application_quit(GTK_APPLICATION(application));
    }
}

typedef GtkApplication FcitxConfigApp;
typedef GtkApplicationClass FcitxConfigAppClass;

G_DEFINE_TYPE (FcitxConfigApp, fcitx_config_app, GTK_TYPE_APPLICATION)

static void
fcitx_config_app_finalize (GObject *object)
{
    G_OBJECT_CLASS (fcitx_config_app_parent_class)->finalize (object);
}

static void
fcitx_config_app_init (FcitxConfigApp *app)
{
    fcitx_config_gtk3_register_resource();
    g_resources_register (fcitx_config_gtk3_get_resource ());
}


int fcitx_config_app_handle_command_line (GApplication              *application,
                                          GApplicationCommandLine   *command_line,
                                          gpointer                   user_data
                                         )
{
    printf("fcitx_config_app_handle_command_line\n");
    int argc;
    gchar** argv = g_application_command_line_get_arguments(command_line, &argc);
    g_application_activate(G_APPLICATION (application));
    GList* list = gtk_application_get_windows (GTK_APPLICATION(application));
    if (list) {
        GtkWidget* window = GTK_DIALOG (list->data);
        if (window)
        {
            gtk_window_present (GTK_WIDGET(window));
        }
    }
    else
    {
        GtkWidget* window = gtk_message_dialog_new(window,
                  GTK_DIALOG_DESTROY_WITH_PARENT,
                  GTK_MESSAGE_QUESTION,
                  GTK_BUTTONS_YES_NO,
                  "确定要重启输入法么?");
        gtk_window_set_title(GTK_WINDOW(window), "输入法");
        if (window)
        {
            gtk_application_add_window(GTK_APPLICATION(application), GTK_WINDOW(window));
            GtkResponseType result = gtk_dialog_run(GTK_DIALOG(window));
            if (result == GTK_RESPONSE_YES)
            {
                fcitx_utils_launch_restart();
            }
            g_application_quit(GTK_APPLICATION(application));
        }

    }
    g_strfreev(argv);
    return 0;
}

static void
fcitx_config_app_class_init (FcitxConfigAppClass *klass)
{
    G_OBJECT_CLASS (klass)->finalize= fcitx_config_app_finalize;

    G_APPLICATION_CLASS (klass)->activate = fcitx_config_app_activate;
}

FcitxConfigApp *
fcitx_config_app_new (void)
{
#if !GLIB_CHECK_VERSION(2, 35, 1)
    g_type_init();
#endif

    FcitxConfigApp* app = g_object_new (fcitx_config_app_get_type (),
                         "application-id", "org.fcitx.FcitxRestartGtk3",NULL);
//                         "flags", G_APPLICATION_HANDLES_COMMAND_LINE,
//                         NULL);
//    g_signal_connect(app, "command-line", (GCallback)fcitx_config_app_handle_command_line, NULL);
    return app;
}

int
main(int argc, char **argv)
{
    gtk_init(&argc, &argv);
    GtkApplication* app = fcitx_config_app_new();

    int status = 0;
    if (app) {
        GError* error = NULL;
        if (!g_application_register(G_APPLICATION(app), NULL, &error)) {
            g_warning("Cannot register %s", error->message);
            g_error_free(error);
            error = NULL;
        }
        if (g_application_get_is_registered(G_APPLICATION(app))) {
            if (g_application_get_is_remote(G_APPLICATION(app))) {
                g_message("fcitx-restart-gtk3 is running.");
            }
        }
    }
    status = g_application_run (G_APPLICATION (app), argc, argv);

    g_object_unref (app);
    return status;
}




/*
int main( int argc, char *argv[])
{

  GtkWidget *window;

  gtk_init(&argc, &argv);

  window = gtk_message_dialog_new(window,
            GTK_DIALOG_DESTROY_WITH_PARENT,
            GTK_MESSAGE_QUESTION,
            GTK_BUTTONS_YES_NO,
            "确定要重启输入法么?");
  gtk_window_set_title(GTK_WINDOW(window), "输入法");
  GtkResponseType result = gtk_dialog_run(GTK_DIALOG(window));

  if (result == GTK_RESPONSE_OK || result == GTK_RESPONSE_APPLY)
  {

    goto EXIT;
  }
  else
  {
    goto EXIT;
  }
  gtk_widget_show_all(window);
  gtk_main();
EXIT:
    printf("程序结束\n");
    gtk_widget_destroy(window);
    return 0;
}
*/
