/* This file implements the tab showing personal attributes of persons.
 *
 * 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.13 $
 */

#include "look_edit_basictab.h"
#include "look_edit_tabperson.h"
#include "widget_editstringlist.h"
#include <kdatepik.h>
#include <kmessagebox.h>
#include <klocale.h>
#include <qlineedit.h>
#include <qmultilineedit.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qstringlist.h>
#include <kdebug.h>

TabPerson::TabPerson(QWidget *parent)
  : TabBasic(parent)    
{
  const int NoOfCols=3;
  int row=0;
  QHBoxLayout *hboxbuttons;
  const QString Texts[] = {
    i18n("Email"),
    i18n("Talk"),
    i18n("URLs"),
    i18n("Telephone numbers")
  };
  QPushButton ** buttons[] = {
    &buttonEmail, &buttonTalk, &buttonURLs, &buttonTelephone
  };
  const int Size=sizeof(Texts)/sizeof(Texts[0]);
  int count;
  // -----
  emails=new QStringList;
  talk=new QStringList;
  urls=new QStringList;
  // -----
  layout=new QGridLayout(this, 100 /* 7 */, NoOfCols, 3, 2);
  if(layout==0)
    {
      KMessageBox::sorry
	(this, i18n("Out of memory."),
	 i18n("General failure."));
      ::exit(-1);
    }
  for(count=0; count<NoOfCols; ++count)
    { // make sure all columns get the same width:
      layout->setColStretch(count, 1);
    }
  // ----- first add formatted name field:
  row=0;
  l_fn=new QLabel(i18n("Formatted name:"), this);
  fn=new QLineEdit(this);
  layout->addWidget(l_fn, row, 0);
  connect(fn, SIGNAL(textChanged(const QString&)),
	  SLOT(textChangedSlot(const QString&)));
  layout->addMultiCellWidget(fn, row, row, 1, 2);
  ++row;
  // ----- add first, middle and last name:
  l_firstname=new QLabel(i18n("First name:"), this);
  layout->addWidget(l_firstname, row, 0);
  l_middlename=new QLabel(i18n("Middle name:"), this);
  layout->addWidget(l_middlename, row, 1);
  l_lastname=new QLabel(i18n("Last name:"), this);
  layout->addWidget(l_lastname, row, 2);
  ++row;
  firstname=new QLineEdit(this);
  layout->addWidget(firstname, row, 0);
  connect(firstname, SIGNAL(textChanged(const QString&)),
	  SLOT(textChangedSlot(const QString&)));
  middlename=new QLineEdit(this);
  layout->addWidget(middlename, row, 1);
  connect(middlename, SIGNAL(textChanged(const QString&)),
	  SLOT(textChangedSlot(const QString&)));
  lastname=new QLineEdit(this);
  layout->addWidget(lastname, row, 2);
  connect(lastname, SIGNAL(textChanged(const QString&)),
	  SLOT(textChangedSlot(const QString&)));
  ++row;
  // ----- name prefix, title, rank:
  l_nameprefix=new QLabel(i18n("Name prefix:"), this);
  layout->addWidget(l_nameprefix, row, 0);
  l_title=new QLabel(i18n("Title:"), this);
  layout->addWidget(l_title, row, 1);
  l_rank=new QLabel(i18n("Rank:"), this);
  layout->addWidget(l_rank, row, 2);
  ++row;
  nameprefix=new QLineEdit(this);
  connect(nameprefix, SIGNAL(textChanged(const QString&)),
	  SLOT(textChangedSlot(const QString&)));
  layout->addWidget(nameprefix, row, 0);
  title=new QLineEdit(this);
  connect(title, SIGNAL(textChanged(const QString&)),
	  SLOT(textChangedSlot(const QString&)));
  layout->addWidget(title, row, 1);
  rank=new QLineEdit(this);
  connect(rank, SIGNAL(textChanged(const QString&)),
	  SLOT(textChangedSlot(const QString&)));
  layout->addWidget(rank, row, 2);
  ++row;
  // ----- button row on bottom:
  ++row; // one empty row
  hboxbuttons=new QHBoxLayout(layout);
  hboxbuttons->setDirection(QBoxLayout::LeftToRight);
  for(count=0; count<Size; ++count)
    {
      *buttons[count]=new QPushButton(Texts[count], this);
      hboxbuttons->addWidget(*buttons[count]);
    }
  // hboxbuttons->addStretch();
  layout->addMultiCell(hboxbuttons, row, row, 0, 2, AlignCenter);
  connect(buttonEmail, SIGNAL(clicked()), SLOT(editEmailAddresses()));
  connect(buttonTalk, SIGNAL(clicked()), SLOT(editTalkAddresses()));
  connect(buttonURLs, SIGNAL(clicked()), SLOT(editURLs()));
  connect(buttonTelephone, SIGNAL(clicked()), SLOT(editTelephoneNumbers()));  
}

TabPerson::~TabPerson()
{
  delete emails;
  delete talk;
  delete urls;
}

void TabPerson::textChangedSlot(const QString&)
{
  emit(changed());
}

void TabPerson::storeContents(AddressBook::Entry& entry)
{
  QLineEdit **lineedits[]= {
    &firstname, &middlename, &lastname,
    &fn, &nameprefix, &title, &rank 
  };
  QString *texts[]= {
    &entry.firstname, &entry.middlename, &entry.lastname,
    &entry.fn, &entry.nameprefix, &entry.title, &entry.rank
  };
  const int Size=sizeof(lineedits)/sizeof(lineedits[0]);
  int count;
  // -----
  for(count=0; count<Size; ++count)
    {
      *texts[count]=(*lineedits[count])->text();
    }
  entry.emails=*emails;
  entry.talk=*talk;
  entry.URLs=*urls;
}

void TabPerson::setContents(const AddressBook::Entry& entry)
{
  QLineEdit **lineedits[]= {
    &firstname, &middlename, &lastname,
    &fn, &nameprefix, &title, &rank 
  };
  QString const *texts[]= {
    &entry.firstname, &entry.middlename, &entry.lastname,
    &entry.fn, &entry.nameprefix, &entry.title, &entry.rank
  };
  const int Size=sizeof(lineedits)/sizeof(lineedits[0]);
  int count;
  // -----
  for(count=0; count<Size; ++count)
    {
      (*lineedits[count])->setText(*texts[count]);
    }
  *emails=entry.emails;
  *talk=entry.talk;
  *urls=entry.URLs;
}

void TabPerson::editEmailAddresses()
{ // a nice example of the use of KDialogBase with user defined main widgets:
  // make a dialog with ok and cancel button:
  KDialogBase dialog(this, "", true, i18n("Edit email addresses"),
		     KDialogBase::Ok|KDialogBase::Cancel,
		     KDialogBase::Ok, true); 
  StringListEditWidget w(&dialog); // make itīs main widget
  w.setStrings(*emails); // add values to it
  dialog.setMainWidget(&w); // make it known
  if(dialog.exec()) // and execute it
    {
      w.getStrings(*emails);
    }
}

void TabPerson::editTalkAddresses()
{
  KDialogBase dialog(this, "", true, i18n("Edit talk addresses"),
		     KDialogBase::Ok|KDialogBase::Cancel,
		     KDialogBase::Ok, true); 
  StringListEditWidget w(&dialog);
  w.setStrings(*talk);
  dialog.setMainWidget(&w);
  if(dialog.exec())
    {
      w.getStrings(*talk);
    }
}

void TabPerson::editURLs()
{
  KDialogBase dialog(this, "", true, i18n("Edit URLs"),
		     KDialogBase::Ok|KDialogBase::Cancel,
		     KDialogBase::Ok, true); 
  StringListEditWidget w(&dialog);
  w.setStrings(*urls);
  dialog.setMainWidget(&w);
  if(dialog.exec())
    {
      w.getStrings(*urls );
    }
}

void TabPerson::editTelephoneNumbers()
{
}

TabBirthday::TabBirthday(QWidget *parent)
  : TabBasic(parent)
{
  datepicker=new KDatePicker(this);
  if(datepicker==0)
    {
      KMessageBox::sorry
	(this, i18n("Out of memory."),
	 i18n("General failure."));
      ::exit(-1);
    }
  connect(datepicker, SIGNAL(dateSelected(QDate)), SLOT(dateChanged(QDate)));
  connect(datepicker, SIGNAL(dateEntered(QDate)), SLOT(dateChanged(QDate)));
}

void TabBirthday::resizeEvent(QResizeEvent*)
{
  const int Grid=3;
  // -----
  datepicker->setGeometry(Grid, Grid, width()-2*Grid, height()-2*Grid);
}

void TabBirthday::storeContents(AddressBook::Entry& entry)
{
  entry.birthday=datepicker->getDate();
}

void TabBirthday::setContents(const AddressBook::Entry& entry)
{
  datepicker->setDate(entry.birthday);
}

void TabBirthday::dateChanged(QDate)
{
  kDebugInfo("TabBirthday::dateChanged: birthday changed.");
  emit(changed());
}

TabComment::TabComment(QWidget *parent)
  : TabBasic(parent)
{
  mle=new QMultiLineEdit(this);
  if(mle==0)
    {
      KMessageBox::sorry
	(this, i18n("Out of memory."),
	 i18n("General failure."));
      ::exit(-1);
    }
  connect(mle, SIGNAL(textChanged()), SLOT(textChangedSlot()));
}

void TabComment::textChangedSlot()
{
  kDebugInfo("TabComment::textChangedSlot: comment changed.");
  emit(changed());
}

void TabComment::resizeEvent(QResizeEvent*)
{
  const int Grid=3;
  // -----
  mle->setGeometry(Grid, Grid, width()-2*Grid, height()-2*Grid);
}

void TabComment::storeContents(AddressBook::Entry& entry)
{
  entry.comment=mle->text();
}

void TabComment::setContents(const AddressBook::Entry& entry)
{
  mle->setText(entry.comment);
}

