/* 
   MultiSync Evolution Plugin - Synchronize Ximian Evolution data
   Copyright (C) 2002-2003 Bo Lincoln <lincoln@lysator.liu.se>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License version 2 as
   published by the Free Software Foundation;

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
   SOFTWARE IS DISCLAIMED.
*/

/*
 *  $Id: gui.c,v 1.2.2.7 2004/10/28 14:13:12 azrael2 Exp $
 */

#include <stdlib.h>
#include <glib.h>
#include <gmodule.h>
#include <gtk/gtkmain.h>
#include <gtk/gtksignal.h>
#include <multisync.h>
#include <dirent.h>
#include "evolution_sync.h"
#include "gui.h"
#include "interface.h"
#include "support.h"

GtkWidget* evowindow = NULL;
evo_environment *evoconn = NULL;

void set_evo_environment(void)
{
	char *configfile = NULL;
	GtkWidget *item;

	configfile = g_strdup(evoconn->configfile);
	evo_env_free(evoconn);
	evoconn = evo_env_create(NULL, 0);
	evoconn->configfile = g_strdup(configfile);
	g_free(configfile);

	item = gtk_option_menu_get_menu (GTK_OPTION_MENU (lookup_widget(evowindow, "addressbookmenu")));
	item = gtk_menu_get_active (GTK_MENU (item));
	evoconn->adressbook_path = gtk_object_get_data (GTK_OBJECT (item), "uri");
	
	item = gtk_option_menu_get_menu (GTK_OPTION_MENU (lookup_widget(evowindow, "calendarmenu")));
	item = gtk_menu_get_active (GTK_MENU (item));
	evoconn->calendar_path = gtk_object_get_data (GTK_OBJECT (item), "uri");
	
	item = gtk_option_menu_get_menu (GTK_OPTION_MENU (lookup_widget(evowindow, "todomenu")));
	item = gtk_menu_get_active (GTK_MENU (item));
	evoconn->tasks_path = gtk_object_get_data (GTK_OBJECT (item), "uri");
}

void fill_calendar_menu(char *selected)
{
	GtkWidget *menuitem;
	GtkWidget *calendarmenu;
	calendarmenu = gtk_menu_new ();
	ESourceList *sources;
	ESource *source;
	
	menuitem = gtk_menu_item_new_with_label ("No Calendar");
	gtk_menu_append (GTK_MENU (calendarmenu), menuitem);
	gtk_object_set_data (GTK_OBJECT (menuitem), "uri", NULL);
	gtk_menu_item_activate(GTK_MENU_ITEM(menuitem));
	
  	if (!e_cal_get_sources(&sources, E_CAL_SOURCE_TYPE_EVENT, NULL)) {
		return;
	}
	
	int n = 1;
	GSList *g;
	for (g = e_source_list_peek_groups (sources); g; g = g->next) {
		ESourceGroup *group = E_SOURCE_GROUP (g->data);
		GSList *s;
		for (s = e_source_group_peek_sources (group); s; s = s->next) {
			source = E_SOURCE (s->data);
			menuitem = gtk_menu_item_new_with_label (e_source_peek_name(source));
			gtk_menu_append (GTK_MENU (calendarmenu), menuitem);
			gtk_object_set_data (GTK_OBJECT (menuitem), "uri", e_source_get_uri(source));
			if (selected && !strcmp(e_source_get_uri(source), selected)) {
				gtk_menu_item_activate(GTK_MENU_ITEM(menuitem));
				gtk_menu_set_active (GTK_MENU(calendarmenu), n);
			}
			n++;
		}
	}
	gtk_option_menu_set_menu (GTK_OPTION_MENU(lookup_widget(evowindow, "calendarmenu")), calendarmenu);
	gtk_widget_show_all(GTK_WIDGET(calendarmenu));
}

void fill_tasks_menu(char *selected)
{
	GtkWidget *menuitem;
	GtkWidget *tasksmenu;
	tasksmenu = gtk_menu_new ();
	ESourceList *sources;
	ESource *source;
	
	menuitem = gtk_menu_item_new_with_label ("No Tasks");
	gtk_menu_append (GTK_MENU (tasksmenu), menuitem);
	gtk_object_set_data (GTK_OBJECT (menuitem), "uri", NULL);
	gtk_menu_item_activate(GTK_MENU_ITEM(menuitem));
	
  	if (!e_cal_get_sources(&sources, E_CAL_SOURCE_TYPE_TODO, NULL)) {
		return;
	}
	
	int n = 1;
	GSList *g;
	for (g = e_source_list_peek_groups (sources); g; g = g->next) {
		ESourceGroup *group = E_SOURCE_GROUP (g->data);
		GSList *s;
		for (s = e_source_group_peek_sources (group); s; s = s->next) {
			source = E_SOURCE (s->data);
			menuitem = gtk_menu_item_new_with_label (e_source_peek_name(source));
			gtk_menu_append (GTK_MENU (tasksmenu), menuitem);
			gtk_object_set_data (GTK_OBJECT (menuitem), "uri", e_source_get_uri(source));
			if (selected && !strcmp(e_source_get_uri(source), selected)) {
				gtk_menu_item_activate(GTK_MENU_ITEM(menuitem));
				gtk_menu_set_active (GTK_MENU(tasksmenu), n);
			}
			n++;
		}
	}
	gtk_option_menu_set_menu (GTK_OPTION_MENU(lookup_widget(evowindow, "todomenu")), tasksmenu);
	gtk_widget_show_all(GTK_WIDGET(tasksmenu));
}

void fill_addressbook_menu(char *selected)
{
	GtkWidget *menuitem;
	GtkWidget *addressbookmenu;
	addressbookmenu = gtk_menu_new ();
	ESourceList *sources;
	ESource *source;
	
	menuitem = gtk_menu_item_new_with_label ("No Addressbook");
	gtk_menu_append (GTK_MENU (addressbookmenu), menuitem);
	gtk_object_set_data (GTK_OBJECT (menuitem), "uri", NULL);
	gtk_menu_item_activate(GTK_MENU_ITEM(menuitem));
	
  	if (!e_book_get_addressbooks(&sources, NULL)) {
		return;
	}
	
	GSList *g;
	int n = 1;
	for (g = e_source_list_peek_groups (sources); g; g = g->next) {
		ESourceGroup *group = E_SOURCE_GROUP (g->data);
		GSList *s;
		for (s = e_source_group_peek_sources (group); s; s = s->next) {
			source = E_SOURCE (s->data);
			menuitem = gtk_menu_item_new_with_label (e_source_peek_name(source));
			gtk_menu_append (GTK_MENU (addressbookmenu), menuitem);
			gtk_object_set_data (GTK_OBJECT (menuitem), "uri", e_source_get_uri(source));
			if (selected && !strcmp(e_source_get_uri(source), selected)) {
				gtk_menu_item_activate(GTK_MENU_ITEM(menuitem));
				gtk_menu_set_active (GTK_MENU(addressbookmenu), n);
			}
			n++;
		}
	}
	gtk_option_menu_set_menu (GTK_OPTION_MENU(lookup_widget(evowindow, "addressbookmenu")), addressbookmenu);
	gtk_widget_show_all(GTK_WIDGET(addressbookmenu));
}

GtkWidget* open_option_window(sync_pair *pair, connection_type type) 
{
	if (!evowindow) {
		// The current active connection, if exists
		evoconn = evo_env_create(pair, type);
		load_evo_settings(evoconn);
		
		evowindow = create_optwin();
		
		fill_addressbook_menu(evoconn->adressbook_path);
		fill_calendar_menu(evoconn->calendar_path);
		fill_tasks_menu(evoconn->tasks_path);
		
		gtk_widget_show (evowindow);
	}
	return(evowindow);
}


void evo2_window_closed(void) 
{
	gtk_widget_destroy(evowindow);
	sync_plugin_window_closed();
	if (evoconn) 
		evo_env_free(evoconn);
	
	evowindow = NULL;
}

void evo2_window_ok(void) 
{
	set_evo_environment();
	save_evo_settings(evoconn);
	evo2_window_closed();
	sync_plugin_window_closed();
}

void evo2_window_cancel(void) 
{
	evo2_window_closed();
	sync_plugin_window_closed();
}

