/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/*
 *  GThumb
 *
 *  Copyright (C) 2010 Free Software Foundation, Inc.
 *
 *  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, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <glib/gi18n.h>
#include "picasa-account-chooser-dialog.h"

#define GET_WIDGET(x) (_gtk_builder_get_widget (self->priv->builder, (x)))


typedef enum {
	ITEM_TYPE_COMMAND,
	ITEM_TYPE_ENTRY,
	ITEM_TYPE_SEPARATOR
} ItemType;


enum {
	ACCOUNT_DATA_COLUMN,
	ACCOUNT_TYPE_COLUMN,
	ACCOUNT_NAME_COLUMN,
	ACCOUNT_ICON_COLUMN,
	ACCOUNT_SENSITIVE_COLUMN
};


static gpointer parent_class = NULL;


struct _PicasaAccountChooserDialogPrivate {
	GtkBuilder *builder;
};


static void
picasa_account_chooser_dialog_finalize (GObject *object)
{
	PicasaAccountChooserDialog *self;

	self = PICASA_ACCOUNT_CHOOSER_DIALOG (object);

	_g_object_unref (self->priv->builder);

	G_OBJECT_CLASS (parent_class)->finalize (object);
}


static void
picasa_account_chooser_dialog_class_init (PicasaAccountChooserDialogClass *klass)
{
	GObjectClass *object_class;

	parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (PicasaAccountChooserDialogPrivate));

	object_class = (GObjectClass*) klass;
	object_class->finalize = picasa_account_chooser_dialog_finalize;
}


static void
picasa_account_chooser_dialog_init (PicasaAccountChooserDialog *self)
{
	GtkWidget *content;

	self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, PICASA_TYPE_ACCOUNT_CHOOSER_DIALOG, PicasaAccountChooserDialogPrivate);
	self->priv->builder = _gtk_builder_new_from_file ("picasa-web-account-chooser.ui", "picasaweb");

	gtk_window_set_resizable (GTK_WINDOW (self), FALSE);
	gtk_box_set_spacing (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (self))), 5);
	gtk_container_set_border_width (GTK_CONTAINER (self), 5);

	content = _gtk_builder_get_widget (self->priv->builder, "account_chooser");
	gtk_container_set_border_width (GTK_CONTAINER (content), 5);
	gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (self))), content, TRUE, TRUE, 0);

	gtk_dialog_add_button (GTK_DIALOG (self),
			       GTK_STOCK_NEW,
			       PICASA_ACCOUNT_CHOOSER_RESPONSE_NEW);
	gtk_dialog_add_button (GTK_DIALOG (self),
			       GTK_STOCK_CANCEL,
			       GTK_RESPONSE_CANCEL);
	gtk_dialog_add_button (GTK_DIALOG (self),
			       GTK_STOCK_OK,
			       GTK_RESPONSE_OK);
	gtk_dialog_set_default_response (GTK_DIALOG (self), GTK_RESPONSE_OK);
}


GType
picasa_account_chooser_dialog_get_type (void)
{
	static GType type = 0;

	if (type == 0) {
		static const GTypeInfo g_define_type_info = {
			sizeof (PicasaAccountChooserDialogClass),
			(GBaseInitFunc) NULL,
			(GBaseFinalizeFunc) NULL,
			(GClassInitFunc) picasa_account_chooser_dialog_class_init,
			(GClassFinalizeFunc) NULL,
			NULL,
			sizeof (PicasaAccountChooserDialog),
			0,
			(GInstanceInitFunc) picasa_account_chooser_dialog_init,
			NULL
		};
		type = g_type_register_static (GTK_TYPE_DIALOG,
					       "PicasaAccountChooserDialog",
					       &g_define_type_info,
					       0);
	}

	return type;
}


static void
picasa_account_chooser_dialog_construct (PicasaAccountChooserDialog *self,
				         GList                      *accounts,
				         const char                 *_default)
{
	GtkTreeIter  iter;
	GList       *scan;
	int          active = 0;
	int          idx;

	gtk_list_store_clear (GTK_LIST_STORE (GET_WIDGET ("account_liststore")));

	for (scan = accounts, idx = 0; scan; scan = scan->next, idx++) {
		char *account = scan->data;

		if (g_strcmp0 (account, _default) == 0)
			active = idx;

		gtk_list_store_append (GTK_LIST_STORE (GET_WIDGET ("account_liststore")), &iter);
		gtk_list_store_set (GTK_LIST_STORE (GET_WIDGET ("account_liststore")), &iter,
				    ACCOUNT_DATA_COLUMN, account,
				    ACCOUNT_TYPE_COLUMN, ITEM_TYPE_ENTRY,
				    ACCOUNT_NAME_COLUMN, account,
				    ACCOUNT_SENSITIVE_COLUMN, TRUE,
				    -1);
	}

	gtk_combo_box_set_active (GTK_COMBO_BOX (GET_WIDGET ("account_combobox")), active);
}


GtkWidget *
picasa_account_chooser_dialog_new (GList      *accounts,
				   const char *_default)
{
	PicasaAccountChooserDialog *self;

	self = g_object_new (PICASA_TYPE_ACCOUNT_CHOOSER_DIALOG, NULL);
	picasa_account_chooser_dialog_construct (self, accounts, _default);

	return (GtkWidget *) self;
}


char *
picasa_account_chooser_dialog_get_active (PicasaAccountChooserDialog *self)
{
	GtkTreeIter  iter;
	char        *email;

	if (! gtk_combo_box_get_active_iter (GTK_COMBO_BOX (GET_WIDGET ("account_combobox")), &iter))
		return NULL;

	gtk_tree_model_get (GTK_TREE_MODEL (GET_WIDGET ("account_liststore")), &iter,
			    ACCOUNT_DATA_COLUMN, &email,
			    -1);

	return email;
}
