/*
    Copyright (c) 2007 Volker Krause <vkrause@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 "messageactions.h"

#include "globalsettings.h"
#include "kmfolder.h"
#include "kmmessage.h"
#include "kmreaderwin.h"

#include <tdeaction.h>
#include <tdeactioncollection.h>
#include <kdebug.h>
#include <tdelocale.h>

#include <tqwidget.h>

using namespace KMail;

MessageActions::MessageActions( TDEActionCollection *ac, TQWidget * parent ) :
    TQObject( parent ),
    mParent( parent ),
    mActionCollection( ac ),
    mCurrentMessage( 0 ),
    mMessageView( 0 )
{
    mReplyActionMenu = new TDEActionMenu( i18n("Message->","&Reply"),
                                      "mail-reply-sender", mActionCollection,
                                      "message_reply_menu" );
  connect( mReplyActionMenu, TQ_SIGNAL(activated()), this,
           TQ_SLOT(slotReplyToMsg()) );

  mReplyAction = new TDEAction( i18n("&Reply..."), "mail-reply-sender", Key_R, this,
                              TQ_SLOT(slotReplyToMsg()), mActionCollection, "reply" );
  mReplyActionMenu->insert( mReplyAction );

  mReplyAuthorAction = new TDEAction( i18n("Reply to A&uthor..."), "mail-reply-sender",
                                    SHIFT+Key_A, this,
                                    TQ_SLOT(slotReplyAuthorToMsg()),
                                    mActionCollection, "reply_author" );
  mReplyActionMenu->insert( mReplyAuthorAction );

  mReplyAllAction = new TDEAction( i18n("Reply to &All..."), "mail-reply-all",
                                 Key_A, this, TQ_SLOT(slotReplyAllToMsg()),
                                 mActionCollection, "reply_all" );
  mReplyActionMenu->insert( mReplyAllAction );

  mReplyListAction = new TDEAction( i18n("Reply to Mailing-&List..."),
                                  "mail_replylist", Key_L, this,
                                  TQ_SLOT(slotReplyListToMsg()), mActionCollection,
                                  "reply_list" );
  mReplyActionMenu->insert( mReplyListAction );

  mNoQuoteReplyAction = new TDEAction( i18n("Reply Without &Quote..."), SHIFT+Key_R,
    this, TQ_SLOT(slotNoQuoteReplyToMsg()), mActionCollection, "noquotereply" );


  mCreateTodoAction = new TDEAction( i18n("Create Task/Reminder..."), "mail_todo",
                                   0, this, TQ_SLOT(slotCreateTodo()), mActionCollection,
                                   "create_todo" );


  mStatusMenu = new TDEActionMenu ( i18n( "Mar&k Message" ),
                                  mActionCollection, "set_status" );

  mStatusMenu->insert(new TDEAction(KGuiItem(i18n("Mark Message as &Read"), "kmmsgread",
                                          i18n("Mark selected messages as read")),
                                 0, this, TQ_SLOT(slotSetMsgStatusRead()),
                                 mActionCollection, "status_read"));

  mStatusMenu->insert(new TDEAction(KGuiItem(i18n("Mark Message as &New"), "kmmsgnew",
                                          i18n("Mark selected messages as new")),
                                 0, this, TQ_SLOT(slotSetMsgStatusNew()),
                                 mActionCollection, "status_new" ));

  mStatusMenu->insert(new TDEAction(KGuiItem(i18n("Mark Message as &Unread"), "kmmsgunseen",
                                          i18n("Mark selected messages as unread")),
                                 0, this, TQ_SLOT(slotSetMsgStatusUnread()),
                                 mActionCollection, "status_unread"));

  mStatusMenu->insert( new TDEActionSeparator( this ) );

  mToggleFlagAction = new TDEToggleAction(i18n("Mark Message as &Important"), "mail_flag",
                                 0, this, TQ_SLOT(slotSetMsgStatusFlag()),
                                 mActionCollection, "status_flag");
  mToggleFlagAction->setCheckedState( i18n("Remove &Important Message Mark") );
  mStatusMenu->insert( mToggleFlagAction );

  mToggleTodoAction = new TDEToggleAction(i18n("Mark Message as &Action Item"), "mail_todo",
                                 0, this, TQ_SLOT(slotSetMsgStatusTodo()),
                                 mActionCollection, "status_todo");
  mToggleTodoAction->setCheckedState( i18n("Remove &Action Item Message Mark") );
  mStatusMenu->insert( mToggleTodoAction );

  mEditAction = new TDEAction( i18n("&Edit Message"), "edit", Key_T, this,
                            TQ_SLOT(editCurrentMessage()), mActionCollection, "edit" );
  mEditAction->plugAccel( mActionCollection->tdeaccel() );

  updateActions();
}

void MessageActions::setCurrentMessage(KMMessage * msg)
{
  mCurrentMessage = msg;
  if ( !msg ) {
    mSelectedSernums.clear();
    mVisibleSernums.clear();
  }
  updateActions();
}

void MessageActions::setSelectedSernums(const TQValueList< TQ_UINT32 > & sernums)
{
  mSelectedSernums = sernums;
  updateActions();
}

void MessageActions::setSelectedVisibleSernums(const TQValueList< TQ_UINT32 > & sernums)
{
  mVisibleSernums = sernums;
  updateActions();
}

void MessageActions::updateActions()
{
  bool singleMsg = (mCurrentMessage != 0);
  if ( mCurrentMessage && mCurrentMessage->parent() ) {
    if ( mCurrentMessage->parent()->isTemplates() )
      singleMsg = false;
  }
  const bool multiVisible = mVisibleSernums.count() > 0 || mCurrentMessage;
  const bool flagsAvailable = GlobalSettings::self()->allowLocalFlags() ||
      !((mCurrentMessage && mCurrentMessage->parent()) ? mCurrentMessage->parent()->isReadOnly() : true);

  mCreateTodoAction->setEnabled( singleMsg );
  mReplyActionMenu->setEnabled( singleMsg );
  mReplyAction->setEnabled( singleMsg );
  mNoQuoteReplyAction->setEnabled( singleMsg );
  mReplyAuthorAction->setEnabled( singleMsg );
  mReplyAllAction->setEnabled( singleMsg );
  mReplyListAction->setEnabled( singleMsg );
  mNoQuoteReplyAction->setEnabled( singleMsg );

  mStatusMenu->setEnabled( multiVisible );
  mToggleFlagAction->setEnabled( flagsAvailable );
  mToggleTodoAction->setEnabled( flagsAvailable );

  if ( mCurrentMessage ) {
    mToggleTodoAction->setChecked( mCurrentMessage->isTodo() );
    mToggleFlagAction->setChecked( mCurrentMessage->isImportant() );
  }

  mEditAction->setEnabled( singleMsg );
}

template<typename T> void MessageActions::replyCommand()
{
  if ( !mCurrentMessage )
    return;
  const TQString text = mMessageView ? mMessageView->copyText() : "";
  KMCommand *command = new T( mParent, mCurrentMessage, text );
  connect( command, TQ_SIGNAL( completed( KMCommand * ) ),
           this, TQ_SIGNAL( replyActionFinished() ) );
  command->start();
}


void MessageActions::slotCreateTodo()
{
  if ( !mCurrentMessage )
    return;
  KMCommand *command = new CreateTodoCommand( mParent, mCurrentMessage );
  command->start();
}

void MessageActions::setMessageView(KMReaderWin * msgView)
{
  mMessageView = msgView;
}

void MessageActions::slotReplyToMsg()
{
  replyCommand<KMReplyToCommand>();
}

void MessageActions::slotReplyAuthorToMsg()
{
  replyCommand<KMReplyAuthorCommand>();
}

void MessageActions::slotReplyListToMsg()
{
  replyCommand<KMReplyListCommand>();
}

void MessageActions::slotReplyAllToMsg()
{
  replyCommand<KMReplyToAllCommand>();
}

void MessageActions::slotNoQuoteReplyToMsg()
{
  if ( !mCurrentMessage )
    return;
  KMCommand *command = new KMNoQuoteReplyToCommand( mParent, mCurrentMessage );
  command->start();
}

void MessageActions::slotSetMsgStatusNew()
{
  setMessageStatus( KMMsgStatusNew );
}

void MessageActions::slotSetMsgStatusUnread()
{
  setMessageStatus( KMMsgStatusUnread );
}

void MessageActions::slotSetMsgStatusRead()
{
  setMessageStatus( KMMsgStatusRead );
}

void MessageActions::slotSetMsgStatusFlag()
{
  setMessageStatus( KMMsgStatusFlag, true );
}

void MessageActions::slotSetMsgStatusTodo()
{
  setMessageStatus( KMMsgStatusTodo, true );
}

void MessageActions::setMessageStatus( KMMsgStatus status, bool toggle )
{
  TQValueList<TQ_UINT32> serNums = mVisibleSernums;
  if ( serNums.isEmpty() && mCurrentMessage )
    serNums.append( mCurrentMessage->getMsgSerNum() );
  if ( serNums.empty() )
    return;
  KMCommand *command = new KMSeStatusCommand( status, serNums, toggle );
  command->start();
}

void MessageActions::editCurrentMessage()
{
  if ( !mCurrentMessage )
    return;
  KMCommand *command = 0;
  KMFolder *folder = mCurrentMessage->parent();
  // edit, unlike send again, removes the message from the folder
  // we only want that for templates and drafts folders
  if ( folder && ( kmkernel->folderIsDraftOrOutbox( folder ) ||
       kmkernel->folderIsTemplates( folder ) ) )
    command = new KMEditMsgCommand( mParent, mCurrentMessage );
  else
    command = new KMResendMessageCommand( mParent, mCurrentMessage );
  command->start();
}

#include "messageactions.moc"