/* -*- C++ -*-
 * This file implements the dialog that is used to set file specific
 * information.
 * 
 * the KDE addressbook
 * copyright:  (C) Mirko Sucker, 1998 1999, 2000
 * mail to:    Mirko Sucker <mirko@kde.org>
 * requires:   recent C++-compiler, at least Qt 2.0
 
 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Library General Public
 License as published by the Free Software Foundation; either
 version 2 of the License, or (at your option) any later version.
 
 This library 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
 Library General Public License for more details.
 
 You should have received a copy of the GNU Library General Public License
 along with this library; see the file COPYING.LIB.  If not, write to
 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 Boston, MA 02111-1307, USA.
 
 * $Revision: 1.6 $
 */

#include "dialog_configurefile.h"
#include <qtabwidget.h>
#include <qconfigDB.h>
#include <qlineedit.h>
#include <qlayout.h>
#include <qlabel.h>
#include <kdialogbase.h>
#include <kmessagebox.h>
#include <klocale.h>
#include <kabapi.h>
#include "kab_topwidget.h"
#include "widget_editstringlist.h"
#include <kdebug.h>

#define KEY_ADDRESSTYPES "AddressTypes"

#ifdef KAB_KDEBUG_AREA
#undef KAB_KDEBUG_AREA
#endif

#define KAB_KDEBUG_AREA 800


void TopLevelWidget::configureFile()
{
  Section *config;
  // -----
  config=api->addressbook()->configurationSection();
  if(config==0)
    {
      return;
    }
  KDialogBase dialog(this);
  KabFileConfigWidget widget(config, &dialog);
  widget.setMinimumSize(320, 200);
  dialog.showButtonApply(false);
  dialog.enableButtonSeparator(true);
  dialog.setMainWidget(&widget);
  if(dialog.exec())
    {
      emit(setStatus(i18n("Accepted.")));
      if(widget.saveSettings(config)!=AddressBook::NoError)
	{
	  emit(setStatus(i18n("Permission denied.")));
	} else {
	  emit(setStatus(i18n("Settings applied.")));
	}
      save();
      createView(currentView, true);
    } else {
      emit(setStatus(i18n("Rejected.")));
    }
}

const int KabFileConfigWidget::Border=2;

KabFileConfigWidget::KabFileConfigWidget(Section* config, QWidget* parent,
					 const char* name)
  : QTabWidget(parent, name)
{
  // ###########################################################################
  kdDebug(KAB_KDEBUG_AREA) << "KabFileConfigWidget ctor: called." << endl;
  // ----- create some aggregats for the user fields tab:
  QLineEdit **ledits[]= {
    &userFieldsName, 
    &userField1, &userField2, &userField3, &userField4
  };
  QLabel **labels[]= {
    &userFieldsLabel,
    &userLabel1, &userLabel2, &userLabel3, &userLabel4
  };
  QString texts[]= {
    i18n("Tab name"),
    i18n("User field 1:"), i18n("User field 2:"),
    i18n("User field 3:"), i18n("User field 4:")
  };
  const QCString Keys[]= {
    "user_headline",
    "user_1", "user_2", "user_3", "user_4"
  };
  const int Size= sizeof(ledits)/sizeof(ledits[0]);
  int count;
  QGridLayout *layout;
  QString text;
  KeyValueMap *keys;
  QStringList strings;
  // -----
  keys=config->getKeys();
  userFields=new QWidget(this);
  layout=new QGridLayout(userFields, 4, 2, Border);
  layout->setAutoAdd(true);
  layout->setColStretch(0, 1);
  layout->setColStretch(1, 2);
  // ----- create local objects:
  if(userFields!=0)
    {
      for(count=0; count<Size; ++count)
	{
	  *labels[count]=new QLabel(texts[count], userFields);
	  text="";
	  if(!keys->get(Keys[count], text))
	    {
	      kdDebug() << "TabUser::configure: missing settings " <<
			 "in configuration section!";
	    }
	  *ledits[count]=new QLineEdit(text, userFields);
	  if(*labels[count]==0 || *ledits[count]==0)
	    {
	      KMessageBox::sorry
		(this, i18n("Out of memory."),
		 i18n("General failure."));
	      ::exit(-1);
	    }
	}
    }
  addTab(userFields, i18n("User fields"));
  // ----- add the address types tab:
  QWidget *helper=new QWidget(this);
  QHBoxLayout *hbl=new QHBoxLayout(helper, 6);
  hbl->setAutoAdd(true);
  editwidget=new StringListEditWidget(helper);
  if(!keys->get(KEY_ADDRESSTYPES, strings))
    {
      kdDebug() << "TabUser::configure: missing address types setting " <<
		 "in configuration section!";
    }
  editwidget->setStrings(strings);
  addTab(helper, i18n("Address types"));
  kdDebug(KAB_KDEBUG_AREA) << "KabFileConfigWidget ctor: done." << endl;
  // ###########################################################################
}

KabFileConfigWidget::~KabFileConfigWidget()
{
  // ###########################################################################
  kdDebug(KAB_KDEBUG_AREA) << "KabFileConfigWidget dtor: done." << endl;
  // ###########################################################################
}

AddressBook::ErrorCode
KabFileConfigWidget::saveSettings(Section *config)
{
  // ###########################################################################
  KeyValueMap *keys;
  QStringList strings;
  // ----- get the keys:
  keys=config->getKeys();
  // ----- insert all values:
  editwidget->getStrings(strings);
  if(!keys->insert("user_headline", userFieldsName->text(), true) ||
     !keys->insert("user_1", userField1->text(), true) ||
     !keys->insert("user_2", userField2->text(), true) ||
     !keys->insert("user_3", userField3->text(), true) ||
     !keys->insert("user_4", userField4->text(), true) ||
     !keys->insert(KEY_ADDRESSTYPES, strings, true))
    {
      KMessageBox::information
	(this,
	 i18n("Cannot save your settings.\n"
	      "Possibly you do not have permissions to write to this file."),
	 i18n("File error"));
      return AddressBook::PermDenied;
    } else {
      return AddressBook::NoError;
    }
  // ###########################################################################
}

