/*
 * sharing-dialog-example - Quick example showing a HildonFileSystemModel used for passing files to share via the dlopen()ed Sharing Dialog
 *
 * Copyright (c) 2009 Faheem Pervez <trippin1@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 3 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.
 *
 * I want credits if you use this in your stuff.
 *
 */

/* gcc -Wall sharing-dialog-example.c `pkg-config --cflags --libs gtk+-2.0 hildon-1 glib-2.0 hildon-fm-2 libosso` -ldl */

#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <glib.h>

#include <libosso.h>
#include <gtk/gtk.h>
#include <hildon/hildon.h>

#include <hildon/hildon-file-selection.h>
#include <hildon/hildon-file-system-model.h>

#define LIBSHARINGDIALOG_LIB "libsharingdialog.so.0"

void (*sharing_dialog_with_file) (osso_context_t *, GtkWidget *, const gchar *);

struct _Data
{
	osso_context_t *context;
	GtkWidget *window, *vbox, *file_selection;
	HildonProgram *program;
	HildonFileSystemModel *hildon_fm_model;

	void *dl_handle;
};

typedef struct _Data Data;

static void cleanup (Data *data)
{
	g_assert (data);

	if (data->dl_handle)
		dlclose (data->dl_handle);
	if (data->context)
		osso_deinitialize (data->context);
	g_free (data);
}

static void on_window_destroyed (gpointer user_data G_GNUC_UNUSED)
{
	gtk_main_quit ();
}

static void on_file_activated (HildonFileSelection *self, Data *data)
{
	GSList *file_list;
	gchar *uri;

	file_list = hildon_file_selection_get_selected_uris (self);
	uri = g_slist_nth_data (file_list, 0);

	sharing_dialog_with_file (data->context ? data->context : NULL, data->window, uri);

	g_slist_foreach (file_list, (GFunc) g_free, NULL);
	g_slist_free (file_list);
}

static void setup_handle (Data *data)
{
	gchar *error;

	g_assert (data);

	data->dl_handle = dlopen (LIBSHARINGDIALOG_LIB, RTLD_LAZY);
	if (!data->dl_handle)
	{
		g_printerr ("%s\n", dlerror());
		cleanup (data);
		exit (EXIT_FAILURE);
	}

	/* "I love the dirty, dirty..." */
	*(void **)(&sharing_dialog_with_file) = dlsym (data->dl_handle, "sharing_dialog_with_file");
	if ((error = dlerror ()) != NULL)
	{
		g_printerr ("%s\n", error);
		cleanup (data);
		exit (EXIT_FAILURE);
	}
}

static void setup_window (Data *data)
{
	g_return_if_fail (data);

	data->program = HILDON_PROGRAM (hildon_program_get_instance ());

	data->window = hildon_window_new ();
	gtk_window_set_title (GTK_WINDOW (data->window), g_get_application_name());
	g_signal_connect_swapped (G_OBJECT (data->window), "destroy", G_CALLBACK(on_window_destroyed), NULL);
	hildon_program_add_window (data->program, HILDON_WINDOW (data->window));

	data->vbox = gtk_vbox_new (FALSE, 0);
	gtk_container_add (GTK_CONTAINER (data->window), data->vbox);

	data->hildon_fm_model = HILDON_FILE_SYSTEM_MODEL( g_object_new (HILDON_TYPE_FILE_SYSTEM_MODEL, "ref-widget", data->vbox, NULL));
	data->file_selection = hildon_file_selection_new_with_model (data->hildon_fm_model);
	g_signal_connect (G_OBJECT (data->file_selection), "file-activated", G_CALLBACK (on_file_activated), data);

	gtk_box_pack_start (GTK_BOX (data->vbox), data->file_selection, TRUE, TRUE, 0);

	gtk_widget_show_all (data->window);
}

gint main (gint argc, gchar** argv)
{
	Data *data = g_new0 (Data, 1);

	setup_handle (data);

	hildon_gtk_init (&argc, &argv);

	data->context = osso_initialize ("com.qwerty12.sharingtest", "666", FALSE, NULL);

	g_set_application_name ("Sharing Dialog Test");
	setup_window (data);

	gtk_main ();

	cleanup (data);

	return EXIT_SUCCESS;
}
