#include <string.h>
#include <gtk/gtk.h>
#include <gtk/gtkbutton.h>
#include <libgnomeui/libgnomeui.h>

#include <bonobo/bonobo-application.h>
#include <bonobo/bonobo-main.h>

static gboolean simple_main_delete_callback(GtkWidget *widget, GdkEvent *event, GtkWidget * app_data);
gint simple_new_instance_cb (BonoboApplication *app, gint argc, char *argv[], gpointer data);
gboolean simple_already_running(int argc, char *argv [], BonoboApplication ** app, const gchar * name);

static gboolean simple_main_delete_callback(GtkWidget *widget, GdkEvent *event, GtkWidget * app_data)
{
    gtk_widget_hide(app_data);
    return TRUE; //stop the processing of this event
}

gint simple_new_instance_cb (BonoboApplication *app, gint argc, char *argv[], gpointer data)
{
    g_message("simple_new_instance_cb got called\n");
    GtkWidget * app_data = (GtkWidget *)data;

    gboolean visible;
    g_object_get(app_data, "visible", &visible, NULL);
    if(!visible)
    {
        gtk_widget_show_all(app_data);
    }

    gtk_widget_grab_focus(app_data);
    gtk_window_present_with_time(GTK_WINDOW(app_data), gtk_get_current_event_time());

    return argc;
}

gboolean simple_already_running(int argc, char *argv [], BonoboApplication ** app, const gchar * name)
{
    if(bonobo_init(&argc, argv) == FALSE)
        g_error ("Problem with bonobo_init");
    if(!bonobo_activate ())
        g_error("Problem with bonobo_activate()");

    *app = bonobo_application_new(name);

    gchar const *envp[] = { "LANG", NULL };
    gchar * serverinfo = bonobo_application_create_serverinfo (*app, envp);

    BonoboAppClient *client;

    Bonobo_RegistrationResult reg_res = bonobo_application_register_unique(*app, serverinfo, &client);
    g_free (serverinfo);

    switch (reg_res)
    {
    case Bonobo_ACTIVATION_REG_ALREADY_ACTIVE: {
        int i;
        g_message ("I am already running.");
        bonobo_object_unref (BONOBO_OBJECT (*app));
        *app = NULL;
        i = bonobo_app_client_new_instance (client, argc, argv, NULL);

        g_object_unref (client);
        gdk_notify_startup_complete();
        bonobo_debug_shutdown ();
        return TRUE;
    }
    case Bonobo_ACTIVATION_REG_SUCCESS:
        g_message ("I am a new instance");
        return FALSE;
        break;

    case Bonobo_ACTIVATION_REG_ERROR:
    default:
        g_error("bonobo activation error when registering unique application");
        return FALSE;
    }
}

int main (int argc, char *argv [])
{
    BonoboApplication * bonobo_app = NULL;
    gnome_program_init("Simple App", "0.1", LIBGNOMEUI_MODULE, argc, argv, NULL);

    if(simple_already_running(argc, argv, &bonobo_app, "My-Very-Simple-App"))
        exit(1);

    GtkWidget * app_data = gnome_app_new("simple_name", "simple_title");
    gtk_window_set_default_size(GTK_WINDOW(app_data), 200, 100);
    gnome_app_set_contents(GNOME_APP(app_data), gtk_button_new_with_label("simple button"));

    g_signal_connect (bonobo_app, "new-instance", G_CALLBACK (simple_new_instance_cb), app_data);
    g_signal_connect(app_data, "delete-event", G_CALLBACK(simple_main_delete_callback), app_data);

    gtk_widget_show_all(GTK_WIDGET (app_data));
    gtk_main();

    if(bonobo_app) bonobo_object_unref(bonobo_app);
    bonobo_debug_shutdown();
    return 0;
};

