
#include "expirypropertiesdialog.h"
#include "folderrequester.h"
#include "kmfolder.h"
#include "kmfoldertree.h"

#include <tqvariant.h>
#include <tqpushbutton.h>
#include <tqgroupbox.h>
#include <tqcheckbox.h>
#include <tqspinbox.h>
#include <tqlabel.h>
#include <tqradiobutton.h>
#include <tqbuttongroup.h>
#include <tqcombobox.h>
#include <tqlayout.h>
#include <tqtooltip.h>
#include <tqwhatsthis.h>

#include <tdelocale.h>
#include <tdemessagebox.h>

using namespace KMail;

/*
 *  Constructs a ExpiryPropertiesDialog as a child of 'parent', with the
 *  name 'name'.
 *
 */
ExpiryPropertiesDialog::ExpiryPropertiesDialog( KMFolderTree* tree, KMFolder* folder )
    : KDialogBase( tree, "expiry_properties", false, i18n( "Mail Expiry Properties" ),
                   KDialogBase::Ok|KDialogBase::Cancel,
                   KDialogBase::Ok, true ),
      mFolder( folder )
{
  setWFlags( getWFlags() | WDestructiveClose );
  TQWidget* privateLayoutWidget = new TQWidget( this, "globalVBox" );
  setMainWidget( privateLayoutWidget );
  privateLayoutWidget->setGeometry( TQRect( 10, 20, 270, 138 ) );
  globalVBox = new TQVBoxLayout( privateLayoutWidget, 11, 6, "globalVBox");
  globalVBox->setSpacing( 20 );

  readHBox = new TQHBoxLayout( 0, 0, 6, "readHBox");

  expireReadMailCB = new TQCheckBox( privateLayoutWidget, "expireReadMailCB" );
  expireReadMailCB->setText( i18n( "Expire read mails after" ) );
  connect( expireReadMailCB, TQT_SIGNAL( toggled( bool )),
           this, TQT_SLOT( slotUpdateControls() ) );
  readHBox->addWidget( expireReadMailCB );

  expireReadMailSB = new TQSpinBox( privateLayoutWidget, "expireReadMailSB" );
  expireReadMailSB->setMaxValue( 999999 );
  expireReadMailSB->setValue( 30 );
  readHBox->addWidget( expireReadMailSB );

  labelDays = new TQLabel( privateLayoutWidget, "labelDays" );
  labelDays->setText( i18n( "days" ) );
  readHBox->addWidget( labelDays );
  globalVBox->addLayout( readHBox );

  unreadHBox = new TQHBoxLayout( 0, 0, 6, "unreadHBox");

  expireUnreadMailCB = new TQCheckBox( privateLayoutWidget, "expireUnreadMailCB" );
  expireUnreadMailCB->setText( i18n( "Expire unread mails after" ) );
  connect( expireUnreadMailCB, TQT_SIGNAL( toggled( bool )),
           this, TQT_SLOT( slotUpdateControls() ) );
  unreadHBox->addWidget( expireUnreadMailCB );

  expireUnreadMailSB = new TQSpinBox( privateLayoutWidget, "expireUnreadMailSB" );
  expireUnreadMailSB->setMaxValue( 99999 );
  expireUnreadMailSB->setValue( 30 );
  unreadHBox->addWidget( expireUnreadMailSB );

  labelDays2 = new TQLabel( privateLayoutWidget, "labelDays2" );
  labelDays2->setText( i18n( "days" ) );
  labelDays2->setAlignment( int( TQLabel::AlignTop ) );
  unreadHBox->addWidget( labelDays2 );
  globalVBox->addLayout( unreadHBox );

  expiryActionHBox = new TQHBoxLayout( 0, 0, 6, "expiryActionHBox");

  expiryActionLabel = new TQLabel( privateLayoutWidget, "expiryActionLabel" );
  expiryActionLabel->setText( i18n( "Expiry action:" ) );
  expiryActionLabel->setAlignment( int( TQLabel::AlignVCenter ) );
  expiryActionHBox->addWidget( expiryActionLabel );

  actionsHBox = new TQVBoxLayout( 0, 0, 6, "actionsHBox");
  actionsGroup = new TQButtonGroup( this );
  actionsGroup->hide(); // for mutual exclusion of the radio buttons

  moveToHBox = new TQHBoxLayout( 0, 0, 6, "moveToHBox");

  moveToRB = new TQRadioButton( privateLayoutWidget, "moveToRB" );
  actionsGroup->insert( moveToRB );
  connect( moveToRB, TQT_SIGNAL( toggled( bool ) ),
           this, TQT_SLOT( slotUpdateControls() ) );
  moveToRB->setText( i18n( "Move to:" ) );
  moveToHBox->addWidget( moveToRB );

  folderSelector = new KMail::FolderRequester( privateLayoutWidget, tree );
  folderSelector->setMustBeReadWrite( true );
  moveToHBox->addWidget( folderSelector );
  actionsHBox->addLayout( moveToHBox );

  deletePermanentlyRB = new TQRadioButton( privateLayoutWidget, "deletePermanentlyRB" );
  actionsGroup->insert( deletePermanentlyRB );
  deletePermanentlyRB->setText( i18n( "Delete permanently" ) );
  actionsHBox->addWidget( deletePermanentlyRB );
  expiryActionHBox->addLayout( actionsHBox );
  globalVBox->addLayout( expiryActionHBox );

  note = new TQLabel( privateLayoutWidget, "note" );
  note->setText( i18n( "Note: Expiry action will be applied immediately after confirming settings." ) );
  note->setAlignment( int( TQLabel::WordBreak | TQLabel::AlignVCenter ) );
  globalVBox->addWidget( note );

  // Load the values from the folder
  bool expiryGloballyOn = mFolder->isAutoExpire();
  int daysToExpireRead, daysToExpireUnread;
  mFolder->daysToExpire( daysToExpireUnread, daysToExpireRead);

  if ( expiryGloballyOn
      && mFolder->getReadExpireUnits() != expireNever
      && daysToExpireRead >= 0 ) {
    expireReadMailCB->setChecked( true );
    expireReadMailSB->setValue( daysToExpireRead );
  }
  if ( expiryGloballyOn
      && mFolder->getUnreadExpireUnits() != expireNever
      && daysToExpireUnread >= 0 ) {
    expireUnreadMailCB->setChecked( true );
    expireUnreadMailSB->setValue( daysToExpireUnread );
  }

  if ( mFolder->expireAction() == KMFolder::ExpireDelete )
    deletePermanentlyRB->setChecked( true );
  else
    moveToRB->setChecked( true );

  TQString destFolderID = mFolder->expireToFolderId();
  if ( !destFolderID.isEmpty() ) {
    KMFolder* destFolder = kmkernel->findFolderById( destFolderID );
    if ( destFolder )
      folderSelector->setFolder( destFolder );
  }
  slotUpdateControls();
  resize( TQSize(295, 204).expandedTo(minimumSizeHint()) );
  clearWState( WState_Polished );
}

/*
 *  Destroys the object and frees any allocated resources
 */
ExpiryPropertiesDialog::~ExpiryPropertiesDialog()
{
    // no need to delete child widgets, TQt does it all for us
}

void ExpiryPropertiesDialog::slotOk()
{
  bool enableGlobally = expireReadMailCB->isChecked() || expireUnreadMailCB->isChecked();

  KMFolder *expireToFolder = folderSelector->folder();
  if ( enableGlobally && moveToRB->isChecked() && !expireToFolder ) {
    KMessageBox::error(
      this,
      i18n( "Please select a folder to expire messages into." ),
      i18n( "No Folder Selected" ) );
    return;
  }

  if ( expireToFolder ) {
    if ( expireToFolder->idString() == mFolder->idString() ) {
      KMessageBox::error(
        this,
        i18n( "Please select a different folder than the current folder "
              "to expire message into." ),
        i18n( "Wrong Folder Selected" ) );
      return;
    } else {
      mFolder->setExpireToFolderId( expireToFolder->idString() );
    }
  }

  mFolder->setAutoExpire( enableGlobally );
  // we always write out days now
  mFolder->setReadExpireAge( expireReadMailSB->value() );
  mFolder->setUnreadExpireAge( expireUnreadMailSB->value() );
  mFolder->setReadExpireUnits( expireReadMailCB->isChecked()? expireDays : expireNever );
  mFolder->setUnreadExpireUnits( expireUnreadMailCB->isChecked()? expireDays : expireNever );

  if ( deletePermanentlyRB->isChecked() )
    mFolder->setExpireAction( KMFolder::ExpireDelete );
  else
    mFolder->setExpireAction( KMFolder::ExpireMove );

  // trigger immediate expiry if there is something to do
  if ( enableGlobally )
    mFolder->expireOldMessages( true /*immediate*/);
  KDialogBase::slotOk();
}

void ExpiryPropertiesDialog::slotUpdateControls()
{
  bool showExpiryActions = expireReadMailCB->isChecked() || expireUnreadMailCB->isChecked();
  moveToRB->setEnabled( showExpiryActions );
  folderSelector->setEnabled( showExpiryActions && moveToRB->isChecked() );
  deletePermanentlyRB->setEnabled( showExpiryActions );
}


#include "expirypropertiesdialog.moc"
