/* KPilot
**
** Copyright (C) 2001 by Dan Pilone
** Copyright (C) 2002-2003 Reinhold Kainhofer
**
** This file defines the setup dialog for the abbrowser-conduit plugin.
*/

/*
** 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 in a file called COPYING; if not, write to
** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
** MA 02110-1301, USA.
*/

/*
** Bug reports and questions can be sent to kde-pim@kde.org
*/

#include "options.h"

#include <tqcheckbox.h>
#include <tqcombobox.h>
#include <tqbuttongroup.h>

#include <kurlrequester.h>
#include <kaboutdata.h>

#include "kaddressbookConduit.h"
#include "abbrowser-setup.h"
#include "abbrowserSettings.h"



static KAboutData *createAbout()
{
	KAboutData *fAbout = new KAboutData("abbrowserconduit",
		I18N_NOOP("Abbrowser Conduit for KPilot"),
		KPILOT_VERSION,
		I18N_NOOP("Configures the Abbrowser Conduit for KPilot"),
		KAboutData::License_GPL,
		"(C) 2001, Dan Pilone\n(C) 2002-2003, Reinhold Kainhofer");
	fAbout->addAuthor("Greg Stern",
		I18N_NOOP("Primary Author"));
	fAbout->addAuthor("Adriaan de Groot",
		I18N_NOOP("Maintainer"),
		"groot@kde.org",
		"http://www.cs.kun.nl/~adridg/kpilot");
	fAbout->addAuthor("Reinhold Kainhofer", I18N_NOOP("Maintainer"),
		"reinhold@kainhofer.com", "http://reinhold.kainhofer.com");
	fAbout->addCredit("David Bishop", I18N_NOOP("UI"));
	return fAbout;
}

AbbrowserWidgetSetup::AbbrowserWidgetSetup(TQWidget *w, const char *n) :
	ConduitConfigBase(w,n),
	fConfigWidget(new AbbrowserWidget(w))
{
	FUNCTIONSETUP;

	fConduitName=i18n("Addressbook");
	fAbout = createAbout();
	ConduitConfigBase::addAboutPage(fConfigWidget->tabWidget,fAbout);
	fWidget=fConfigWidget;
	fConfigWidget->fAbookFile->setMode(KFile::File);
#define CM(a,b) connect(fConfigWidget->a,b,this,TQT_SLOT(modified()));
	CM(fSyncDestination,TQT_SIGNAL(clicked(int)));
	CM(fAbookFile,TQT_SIGNAL(textChanged(const TQString &)));
	CM(fArchive,TQT_SIGNAL(toggled(bool)));
	CM(fConflictResolution,TQT_SIGNAL(activated(int)));
	CM(fOtherPhone,TQT_SIGNAL(activated(int)));
	CM(fAddress,TQT_SIGNAL(activated(int)));
	CM(fFax,TQT_SIGNAL(activated(int)));
	CM(fCustom0,TQT_SIGNAL(activated(int)));
	CM(fCustom1,TQT_SIGNAL(activated(int)));
	CM(fCustom2,TQT_SIGNAL(activated(int)));
	CM(fCustom3,TQT_SIGNAL(activated(int)));
	CM(fCustomDate, TQT_SIGNAL(activated(int)));
	CM(fCustomDate, TQT_SIGNAL(textChanged(const TQString&)));
#undef CM
}

AbbrowserWidgetSetup::~AbbrowserWidgetSetup()
{
	FUNCTIONSETUP;
}

/* virtual */ void AbbrowserWidgetSetup::commit()
{
	FUNCTIONSETUP;

	TQButtonGroup*grp=fConfigWidget->fSyncDestination;
	AbbrowserSettings::setAddressbookType(grp->id(grp->selected()));
	AbbrowserSettings::setFileName(fConfigWidget->fAbookFile->url());
	AbbrowserSettings::setArchiveDeleted(fConfigWidget->fArchive->isChecked());

	// Conflicts page
	AbbrowserSettings::setConflictResolution(
		fConfigWidget->fConflictResolution->currentItem()+SyncAction::eCROffset);

	// Fields page
	AbbrowserSettings::setPilotOther(fConfigWidget->fOtherPhone->currentItem());
	AbbrowserSettings::setPilotStreet(fConfigWidget->fAddress->currentItem());
	AbbrowserSettings::setPilotFax(fConfigWidget->fFax->currentItem());

	// Custom fields page
	AbbrowserSettings::setCustom0(fConfigWidget->fCustom0->currentItem());
	AbbrowserSettings::setCustom1(fConfigWidget->fCustom1->currentItem());
	AbbrowserSettings::setCustom2(fConfigWidget->fCustom2->currentItem());
	AbbrowserSettings::setCustom3(fConfigWidget->fCustom3->currentItem());
#ifdef DEBUG
	DEBUGKPILOT <<fname<<
	  "Custom0: "<<fConfigWidget->fCustom0->currentItem()<<"   "<<
	  "Custom1: "<<fConfigWidget->fCustom1->currentItem()<<"   "<<
	  "Custom2: "<<fConfigWidget->fCustom2->currentItem()<<"   "<<
	  "Custom3: "<<fConfigWidget->fCustom3->currentItem()<<"   "
		<< " eCustom[0]=" << AbbrowserSettings::custom0()<<"    "
		<< " eCustom[1]=" << AbbrowserSettings::custom1()<<"    "
		<< " eCustom[2]=" << AbbrowserSettings::custom2()<<"    "
		<< " eCustom[3]=" << AbbrowserSettings::custom3()<<"    "<<
		endl;
#endif
	int fmtindex=fConfigWidget->fCustomDate->currentItem();
	AbbrowserSettings::setCustomDateFormat(
	  (fmtindex==0)?(TQString()):fConfigWidget->fCustomDate->currentText() );

	AbbrowserSettings::self()->writeConfig();
	unmodified();
}

/* virtual */ void AbbrowserWidgetSetup::load()
{
	FUNCTIONSETUP;
	AbbrowserSettings::self()->readConfig();

#ifdef DEBUG
	DEBUGKPILOT << fname
		<< ": Settings "
		<< " fPilotStreetHome=" << AbbrowserSettings::pilotStreet()
		<< " fPilotFaxHome=" << AbbrowserSettings::pilotFax()
		<< " fArchive=" << AbbrowserSettings::archiveDeleted()
		<< " eCustom[0]=" << AbbrowserSettings::custom0()
		<< " eCustom[1]=" << AbbrowserSettings::custom1()
		<< " eCustom[2]=" << AbbrowserSettings::custom2()
		<< " eCustom[3]=" << AbbrowserSettings::custom3()
		<< endl;
#endif

	// General page
	fConfigWidget->fSyncDestination->setButton(AbbrowserSettings::addressbookType());
	fConfigWidget->fAbookFile->setURL(AbbrowserSettings::fileName());
	fConfigWidget->fArchive->setChecked(AbbrowserSettings::archiveDeleted());

	// Conflicts page
	fConfigWidget->fConflictResolution->setCurrentItem(
	  AbbrowserSettings::conflictResolution() - SyncAction::eCROffset );

	// Fields page
	fConfigWidget->fOtherPhone->setCurrentItem(AbbrowserSettings::pilotOther());
	fConfigWidget->fAddress->setCurrentItem(AbbrowserSettings::pilotStreet());
	fConfigWidget->fFax->setCurrentItem(AbbrowserSettings::pilotFax());

	// Custom fields page
	fConfigWidget->fCustom0->setCurrentItem(AbbrowserSettings::custom0());
	fConfigWidget->fCustom1->setCurrentItem(AbbrowserSettings::custom1());
	fConfigWidget->fCustom2->setCurrentItem(AbbrowserSettings::custom2());
	fConfigWidget->fCustom3->setCurrentItem(AbbrowserSettings::custom3());
	TQString datefmt=AbbrowserSettings::customDateFormat();
	if (datefmt.isEmpty())
	{
		fConfigWidget->fCustomDate->setCurrentItem(0);
	}
	else
	{
		fConfigWidget->fCustomDate->setCurrentText(datefmt);
	}

	unmodified();
}

/* static */ ConduitConfigBase *AbbrowserWidgetSetup::create(TQWidget *w, const char *n)
{
	return new AbbrowserWidgetSetup(w,n);
}

