/*
    This file is part of KitchenSync.

    Copyright (c) 2005 Cornelius Schumacher <schumacher@kde.org>

    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, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
    USA.
*/


#include <kdialog.h>
#include <klineedit.h>
#include <tdelocale.h>
#include <kdebug.h>

#include <tqlabel.h>
#include <tqlayout.h>
#include <tqcheckbox.h>

#include <libqopensync/group.h>
#include <libqopensync/conversion.h>
#include <libqopensync/environment.h>

#include "syncprocess.h"
#include "syncprocessmanager.h"

#include "groupconfigcommon.h"

ObjectTypeSelector::ObjectTypeSelector( TQWidget *parent )
  : TQWidget( parent )
{
  TQGridLayout *layout = new TQGridLayout( this );
  layout->setMargin( 0 );

  const QSync::Conversion conversion = SyncProcessManager::self()->environment()->conversion();

  TQMap<TQString, TQString> objectTypeMap;
  objectTypeMap.insert( "contact", i18n( "Contacts" ) );
  objectTypeMap.insert( "event", i18n( "Events" ) );
  objectTypeMap.insert( "todo", i18n( "To-dos" ) );
  objectTypeMap.insert( "note", i18n( "Notes" ) );

  TQStringList objectTypes = conversion.objectTypes();

  // reorder the entries so that contact and event are the first one
  qHeapSort( objectTypes );

  TQStringList reoderedObjectTypes, stack;
  for ( uint i = 0; i < objectTypes.count(); ++i ) {
    if ( objectTypes[ i ] == "contact" || objectTypes[ i ] == "event" )
      reoderedObjectTypes.append( objectTypes[ i ] );
    else
      stack.append( objectTypes[ i ] );
  }
  reoderedObjectTypes += stack;

  TQStringList::ConstIterator it;

  int row = 0;
  int col = 0;
  for( it = reoderedObjectTypes.begin(); it != reoderedObjectTypes.end(); ++it ) {
    TQString objectType = *it;

    // Don't display object type "data". Object type "data" is a kind of wildcard - so don't filter * 
    if ( objectType == "data" )
      continue;

    TQCheckBox *objectCheckBox = new TQCheckBox( objectTypeMap[ objectType ], this );
    layout->addWidget( objectCheckBox, row, col );
    mObjectTypeChecks.insert( objectType, objectCheckBox );

    col++;
    if ( (row == 0 && col == 2) || col == 3 ) {
      col = 0;
      row++;
    }
  }
}

void ObjectTypeSelector::load( const QSync::Group &group )
{
  const QSync::GroupConfig config = group.config();

  const TQStringList objectTypes = config.activeObjectTypes();

  // Enable everything on the inital load
  bool initialLoad = false;
  if ( objectTypes.isEmpty() )
    initialLoad = true;

  TQMap<TQString, TQCheckBox*>::ConstIterator it;
  for( it = mObjectTypeChecks.begin(); it != mObjectTypeChecks.end(); ++it ) {
    TQCheckBox *check = it.data();
    check->setChecked( objectTypes.contains( it.key() ) || initialLoad );
  }
}

void ObjectTypeSelector::save( QSync::Group group )
{
  TQStringList objectTypes;

  TQMap<TQString,TQCheckBox *>::ConstIterator it;
  for( it = mObjectTypeChecks.begin(); it != mObjectTypeChecks.end(); ++it ) {
    TQCheckBox *check = it.data();
    if ( check->isChecked() )
      objectTypes.append( it.key() );
  }

  // Always add object type "data"
  objectTypes.append( "data" );

  QSync::GroupConfig config = group.config();
  config.setActiveObjectTypes( objectTypes );
}

GroupConfigCommon::GroupConfigCommon( TQWidget *parent )
  : TQWidget( parent )
{
  TQGridLayout *layout = new TQGridLayout( this, 2, 2, KDialog::marginHint(), KDialog::spacingHint() );

  layout->addWidget( new TQLabel( i18n( "Name:" ), this ), 0, 0 );

  mGroupName = new KLineEdit( this );
  layout->addWidget( mGroupName, 0, 1 );

  layout->addWidget( new TQLabel( i18n( "Object Types to be Synchronized:"), this ), 1, 0, TQt::AlignTop );

  mObjectTypeSelector = new ObjectTypeSelector( this );
  layout->addWidget( mObjectTypeSelector, 1, 1 );

  layout->setRowStretch( 2, 1 );
}

void GroupConfigCommon::setSyncProcess( SyncProcess *syncProcess )
{
  mSyncProcess = syncProcess;

  mGroupName->setText( mSyncProcess->group().name() );
  mObjectTypeSelector->load( mSyncProcess->group() );
}

void GroupConfigCommon::save()
{
  mSyncProcess->group().setName( mGroupName->text() );
  mObjectTypeSelector->save( mSyncProcess->group() );
}
