/**
 * hhwX.c (hildon_helloworld-10)
 *
 * This maemo code example is licensed under a MIT-style license,
 * that can be found in the file called "License" in the same
 * directory as this file.
 * Copyright (c) 2007-2008 Nokia Corporation. All rights reserved.
 *
 * Add LibOSSO support for the application.
 *
 * Look for lines with "NEW" or "MODIFIED" in them.
 */

#include <stdlib.h>
#include <hildon/hildon-program.h>
#include <hildon/hildon-color-button.h>
#include <hildon/hildon-find-toolbar.h>
#include <hildon/hildon-file-chooser-dialog.h>
#include <hildon/hildon-banner.h>
#include <libgnomevfs/gnome-vfs.h>
#include <gconf/gconf-client.h>
/* Pull in the LibOSSO library declarations (NEW). */
#include <libosso.h>

  /*... Listing cut for brevity ...*/

/* Build up the D-Bus name for this application (NEW). */
#define PACKAGE_DBUS_NAME "org.maemo." PACKAGE_NAME

  /*... Listing cut for brevity ...*/

/**
 * MODIFIED
 *
 * Create a LibOSSO context and shut it down when done with the
 * application.
 */
int main(int argc, char** argv) {

  ApplicationState aState = {};

  GtkWidget* label = NULL;
  GtkWidget* vbox = NULL;
  GtkWidget* mainToolbar = NULL;
  GtkWidget* findToolbar = NULL;

  /* Pointer to the LibOSSO context object/connection (NEW). */
  osso_context_t* ctx = NULL;

  if(!gnome_vfs_init()) {
    g_error("Failed to initialize GnomeVFS-libraries, exiting\n");
  }

  /* Initialize the GTK+ */
  gtk_init(&argc, &argv);

  /* Setup the HildonProgram, HildonWindow and application name. */
  aState.program = HILDON_PROGRAM(hildon_program_get_instance());
  g_set_application_name("Hello Hildon!");
  aState.window = HILDON_WINDOW(hildon_window_new());
  hildon_program_add_window(aState.program,
                            HILDON_WINDOW(aState.window));

  /* Create a LibOSSO context (which will also attach this
     application to the D-Bus (NEW).
     NOTE:
       We use the name and version from the configure.ac.
       The D-Bus name is built by prefixing "org.maemo." to the
       package name. */
  g_print("Initializing LibOSSO context (" PACKAGE_DBUS_NAME ", "
          PACKAGE_VERSION ")\n");
  ctx = osso_initialize(PACKAGE_DBUS_NAME, PACKAGE_VERSION, TRUE,
                        NULL);
  if (ctx == NULL) {
    g_print("Failed to init LibOSSO\n");
    return EXIT_FAILURE;
  }
  g_print("LibOSSO Init done\n");

  label = gtk_label_new("<b>Hello</b> <i>Hildon</i> "
                        "(with LibOSSO!)");
  gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
  gtk_label_set_use_markup(GTK_LABEL(label), TRUE);
  aState.textLabel = label;

  buildMenu(&aState);

  vbox = gtk_vbox_new(FALSE, 0);
  gtk_container_add(GTK_CONTAINER(aState.window), vbox);
  gtk_box_pack_end(GTK_BOX(vbox), label, TRUE, TRUE, 0);

  mainToolbar = buildToolbar(&aState);
  findToolbar = buildFindToolbar(&aState);

  aState.mainToolbar = mainToolbar;
  aState.findToolbar = findToolbar;

  /* Connect the termination signals. */
  g_signal_connect(G_OBJECT(aState.window), "delete-event",
                   G_CALLBACK(cbEventDelete), &aState);
  g_signal_connect(G_OBJECT(aState.window), "destroy",
                   G_CALLBACK(cbActionTopDestroy), &aState);

  /* Show all widgets that are contained by the Window. */
  gtk_widget_show_all(GTK_WIDGET(aState.window));

  /* Add the toolbars to the Hildon Window. */
  hildon_window_add_toolbar(HILDON_WINDOW(aState.window),
                            GTK_TOOLBAR(mainToolbar));
  hildon_window_add_toolbar(HILDON_WINDOW(aState.window),
                            GTK_TOOLBAR(findToolbar));

  /* Register a callback to handle key presses. */
  g_signal_connect(G_OBJECT(aState.window), "key_press_event",
                   G_CALLBACK(cbKeyPressed), &aState);

  g_print("main: calling gtk_main\n");
  gtk_main();

  g_print("main: returned from gtk_main & de-initing LibOSSO\n");
  /* De-initialize LibOSSO (detaches from the D-Bus) (NEW). */
  osso_deinitialize(ctx);

  g_print("main: exiting with success\n");
  return EXIT_SUCCESS;
}
