/* $Id: properties.c,v 1.49 2011/11/06 06:40:45 khorben Exp $ */
/* Copyright (c) 2011 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS Desktop Browser */
/* 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, version 3 of the License.
 *
 * 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, see <http://www.gnu.org/licenses/>. */
/* TODO:
 * - add a file count and disk usage tab for directories */



#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <locale.h>
#include <libintl.h>
#include <gtk/gtk.h>
#include <System.h>
#include "Browser.h"
#define _Browser _BrowserHidden /* XXX */
#include "browser.h"
#undef _Browser
#include "../config.h"
#define _(string) gettext(string)
#define N_(string) (string)

#define COMMON_CONFIG_FILENAME
#include "common.c"


/* constants */
#ifndef PREFIX
# define PREFIX		"/usr/local"
#endif
#ifndef DATADIR
# define DATADIR	PREFIX "/share"
#endif
#ifndef LOCALEDIR
# define LOCALEDIR	DATADIR "/locale"
#endif


/* properties */
/* types */
#define _Properties _Browser
typedef struct _Properties
{
	/* internal */
	Mime * mime;
	char * filename;

	/* plugins */
	BrowserPluginHelper helper;

	/* widgets */
	GtkIconTheme * theme;
	GtkWidget * window;
	GtkWidget * notebook;
} Properties;


/* variables */
static unsigned int _properties_cnt = 0; /* XXX set as static in _properties */

/* functions */
static int _properties(Mime * mime, int filec, char * const filev[]);

/* properties */
static Properties * _properties_new(Mime * mime, char const * filename);
static void _properties_delete(Properties * properties);

/* accessors */
static Mime * _properties_get_mime(Properties * properties);
static void _properties_set_location(Properties * properties,
		char const * filename);

/* useful */
static int _properties_error(Properties * properties, char const * message,
		int ret);
static int _properties_load(Properties * properties, char const * name);

/* callbacks */
static void _properties_on_close(gpointer data);
static gboolean _properties_on_closex(gpointer data);


/* properties */
static int _properties(Mime * mime, int filec, char * const filev[])
{
	int ret = 0;
	int i;
	Properties * properties;

	for(i = 0; i < filec; i++)
	{
		/* FIXME if relative path get the full path */
		if((properties = _properties_new(mime, filev[i])) == NULL)
			ret |= 1;
		else
			_properties_cnt++;
	}
	return ret;
}


/* properties_new */
static int _new_load(Properties * properties);

static Properties * _properties_new(Mime * mime, char const * filename)
{
	Properties * properties;
	GtkWidget * vbox;
	GtkWidget * bbox;
	GtkWidget * widget;
	gchar * p;
	char buf[256];

	if(filename == NULL)
		return NULL;
	if((properties = malloc(sizeof(*properties))) == NULL)
	{
		_properties_error(NULL, "malloc", 1);
		return NULL;
	}
	properties->mime = mime;
	properties->filename = strdup(filename);
	properties->helper.browser = properties;
	properties->helper.error = _properties_error;
	properties->helper.get_mime = _properties_get_mime;
	properties->helper.set_location = _properties_set_location;
	properties->window = NULL;
	if(properties->filename == NULL)
	{
		_properties_delete(properties);
		return NULL;
	}
	properties->theme = gtk_icon_theme_get_default();
	/* window */
	properties->window = gtk_dialog_new();
	p = g_filename_display_basename(filename);
	snprintf(buf, sizeof(buf), "%s%s", _("Properties of "), p);
	g_free(p);
	gtk_window_set_default_size(GTK_WINDOW(properties->window), 300, 400);
	gtk_window_set_title(GTK_WINDOW(properties->window), buf);
	g_signal_connect_swapped(G_OBJECT(properties->window), "delete-event",
			G_CALLBACK(_properties_on_closex), properties);
#if GTK_CHECK_VERSION(2, 14, 0)
	vbox = gtk_dialog_get_content_area(GTK_DIALOG(properties->window));
#else
	vbox = GTK_DIALOG(properties->window)->vbox;
#endif
	/* notebook */
	properties->notebook = gtk_notebook_new();
	gtk_notebook_set_scrollable(GTK_NOTEBOOK(properties->notebook), TRUE);
	gtk_box_pack_start(GTK_BOX(vbox), properties->notebook, TRUE, TRUE, 0);
	gtk_widget_show_all(vbox);
	/* button box */
#if GTK_CHECK_VERSION(2, 14, 0)
	bbox = gtk_dialog_get_action_area(GTK_DIALOG(properties->window));
#else
	bbox = GTK_DIALOG(properties->window)->action_area;
#endif
	widget = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
	g_signal_connect_swapped(G_OBJECT(widget), "clicked", G_CALLBACK(
				_properties_on_close), properties);
	gtk_container_add(GTK_CONTAINER(bbox), widget);
	gtk_widget_show_all(bbox);
	/* FIXME check errors */
	_new_load(properties);
	if(filename != NULL)
		_properties_set_location(properties, filename);
	gtk_widget_show(properties->window);
	return properties;
}

static int _new_load(Properties * properties)
{
	int ret = 0;
	Config * config;
	char const * plugins = NULL;
	char * p;
	char * q;
	size_t i;

	p = _common_config_filename(BROWSER_CONFIG_FILE);
	if((config = config_new()) != NULL && config_load(config, p) == 0
			&& (plugins = config_get(config, NULL, "properties"))
			== NULL)
		plugins = "properties,preview";
	string_delete(p);
	if(plugins != NULL && strlen(plugins) && (p = strdup(plugins)) != NULL)
	{
		/* XXX if plugins is only commas nothing will be loaded */
		for(q = p, i = 0;;)
		{
			if(q[i] == '\0')
			{
				ret |= _properties_load(properties, q);
				break;
			}
			if(q[i++] != ',')
				continue;
			q[i - 1] = '\0';
			ret |= _properties_load(properties, q);
			q += i;
			i = 0;
		}
		free(p);
	}
	else
	{
		ret |= _properties_load(properties, "properties");
		ret |= _properties_load(properties, "preview");
	}
	if(config != NULL)
		config_delete(config);
	return ret;
}


/* properties_delete */
static void _properties_delete(Properties * properties)
{
	if(properties->window != NULL)
		gtk_widget_destroy(properties->window);
	free(properties->filename);
	free(properties);
	_properties_cnt--;
}


/* accessors */
/* properties_get_mime */
static Mime * _properties_get_mime(Properties * properties)
{
	return properties->mime;
}


/* properties_set_location */
static void _properties_set_location(Properties * properties,
		char const * filename)
{
	char * p;

	if((p = strdup(filename)) == NULL)
	{
		_properties_error(NULL, filename, 1);
		return;
	}
	free(properties->filename);
	properties->filename = p;
}


/* _properties_error */
static void _error_response(GtkWidget * widget, gint arg, gpointer data);

static int _properties_error(Properties * properties, char const * message,
		int ret)
{
	GtkWidget * dialog;
	char const * error;

	error = strerror(errno);
	dialog = gtk_message_dialog_new((properties != NULL
				&& properties->window != NULL)
			? GTK_WINDOW(properties->window) : NULL, 0,
			GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
#if GTK_CHECK_VERSION(2, 6, 0)
			"%s", _("Error"));
	gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
#endif
			"%s: %s", message, error);
	gtk_window_set_title(GTK_WINDOW(dialog), _("Error"));
	if(properties != NULL && properties->window != NULL)
		gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(
					properties->window));
	g_signal_connect(G_OBJECT(dialog), "response", G_CALLBACK(
				_error_response), (ret != 0)
			? &_properties_cnt : NULL);
	gtk_widget_show(dialog);
	return ret;
}

static void _error_response(GtkWidget * widget, gint arg, gpointer data)
{
	unsigned int * cnt = data;

	if(cnt == NULL)
		gtk_widget_destroy(widget);
	else if(--(*cnt) == 0)
		gtk_main_quit();
	else
		gtk_widget_destroy(widget);
}


/* properties_load */
static int _properties_load(Properties * properties, char const * name)
{
	Plugin * p;
	BrowserPlugin * bp;
	GdkPixbuf * icon = NULL;
	GtkWidget * hbox;
	GtkWidget * widget;

	if((p = plugin_new(LIBDIR, PACKAGE, "plugins", name)) == NULL)
		return -_properties_error(NULL, error_get(), 1);
	if((bp = plugin_lookup(p, "plugin")) == NULL)
	{
		plugin_delete(p);
		return -_properties_error(NULL, error_get(), 1);
	}
	bp->helper = &properties->helper;
	if(bp->init == NULL || (widget = bp->init(bp)) == NULL)
	{
		plugin_delete(p);
		return -_properties_error(NULL, error_get(), 1);
	}
	bp->refresh(bp, properties->filename);
	/* label */
	if(bp->icon != NULL)
		icon = gtk_icon_theme_load_icon(properties->theme, bp->icon, 24,
				0, NULL);
	if(icon == NULL)
		icon = gtk_icon_theme_load_icon(properties->theme,
				"gnome-settings", 24, 0, NULL);
	hbox = gtk_hbox_new(FALSE, 4);
	gtk_box_pack_start(GTK_BOX(hbox), gtk_image_new_from_pixbuf(icon),
			FALSE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(hbox), gtk_label_new(_(bp->name)), TRUE,
			TRUE, 0);
	gtk_widget_show_all(hbox);
	gtk_notebook_append_page(GTK_NOTEBOOK(properties->notebook), widget,
			hbox);
	/* XXX configure the default plug-in somewhere */
	if(strcmp(name, "properties") == 0)
		gtk_notebook_set_current_page(GTK_NOTEBOOK(
					properties->notebook), -1);
	return 0;
}


/* callbacks */
static void _properties_on_close(gpointer data)
{
	Properties * properties = data;

	_properties_delete(properties);
	if(_properties_cnt == 0)
		gtk_main_quit();
}

static gboolean _properties_on_closex(gpointer data)
{
	_properties_on_close(data);
	return FALSE;
}


/* usage */
static int _usage(void)
{
	fputs(_("Usage: properties file...\n"), stderr);
	return 1;
}


/* main */
int main(int argc, char * argv[])
{
	int ret;
	int o;
	Mime * mime;

	setlocale(LC_ALL, "");
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);
	gtk_init(&argc, &argv);
	while((o = getopt(argc, argv, "")) != -1)
		switch(o)
		{
			default:
				return _usage();
		}
	if(optind == argc)
		return _usage();
	mime = mime_new(NULL);
	ret = _properties(mime, argc - optind, &argv[optind]);
	gtk_main();
	if(mime != NULL)
		mime_delete(mime);
	return (ret == 0) ? 0 : 2;
}