/*
    This file is part of KAddressBook.

    Copyright (c) 2009 Tobias Koenig <tokoe@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 "modelcolumnmanager.h"

#include "settings.h"

#include <kabc/addressee.h>
#include <klocale.h>

#include <QtCore/QEvent>
#include <QtCore/QTimer>
#include <QtGui/QContextMenuEvent>
#include <QtGui/QHeaderView>
#include <QtGui/QMenu>
#include <QtGui/QWidget>

ModelColumnManager::ModelColumnManager( AkonadiFuture::ContactsTreeModel *model, QObject *parent )
  : QObject( parent ), mModel( model ), mWidget( 0 )
{
}

void ModelColumnManager::load()
{
  const QList<int> settingsColumns = Settings::contactModelColumns();
  AkonadiFuture::ContactsTreeModel::Columns columns;

  foreach( int column, settingsColumns )
    columns.append( (AkonadiFuture::ContactsTreeModel::Column)column );

  mModel->setColumns( columns );
}

void ModelColumnManager::store()
{
  const AkonadiFuture::ContactsTreeModel::Columns columns = mModel->columns();
  QList<int> settingsColumns;

  foreach( int column, columns )
    settingsColumns.append( (int)column );

  Settings::setContactModelColumns( settingsColumns );
}

void ModelColumnManager::setWidget( QWidget *widget )
{
  mWidget = widget;
  mWidget->installEventFilter( this );
}

bool ModelColumnManager::eventFilter( QObject *watched, QEvent* event )
{
  if ( watched == mWidget ) {
    if ( event->type() == QEvent::ContextMenu ) {
      QMenu menu;

      AkonadiFuture::ContactsTreeModel::Columns columns = mModel->columns();

      QAction *fullNameAction = menu.addAction( i18n( "Full Name" ) );
      fullNameAction->setCheckable( true );
      fullNameAction->setChecked( columns.contains( AkonadiFuture::ContactsTreeModel::FullName ) );
      fullNameAction->setEnabled( false );

      QAction *birthdayAction = menu.addAction( KABC::Addressee::birthdayLabel() );
      birthdayAction->setCheckable( true );
      birthdayAction->setChecked( columns.contains( AkonadiFuture::ContactsTreeModel::Birthday ) );

      QAction *homeAddressAction = menu.addAction( i18n( "Home Address" ) );
      homeAddressAction->setCheckable( true );
      homeAddressAction->setChecked( columns.contains( AkonadiFuture::ContactsTreeModel::HomeAddress ) );

      QAction *businessAddressAction = menu.addAction( i18n( "Business Address" ) );
      businessAddressAction->setCheckable( true );
      businessAddressAction->setChecked( columns.contains( AkonadiFuture::ContactsTreeModel::BusinessAddress ) );

      QAction *phoneNumbersAction = menu.addAction( i18n( "Phone Numbers" ) );
      phoneNumbersAction->setCheckable( true );
      phoneNumbersAction->setChecked( columns.contains( AkonadiFuture::ContactsTreeModel::PhoneNumbers ) );

      QAction *preferredEmailAction = menu.addAction( i18n( "Preferred EMail" ) );
      preferredEmailAction->setCheckable( true );
      preferredEmailAction->setChecked( columns.contains( AkonadiFuture::ContactsTreeModel::PreferredEmail ) );

      QAction *allEmailsAction = menu.addAction( i18n( "All EMails" ) );
      allEmailsAction->setCheckable( true );
      allEmailsAction->setChecked( columns.contains( AkonadiFuture::ContactsTreeModel::AllEmails ) );

      QAction *organizationAction = menu.addAction( KABC::Addressee::organizationLabel() );
      organizationAction->setCheckable( true );
      organizationAction->setChecked( columns.contains( AkonadiFuture::ContactsTreeModel::Organization ) );

      QAction *homepageAction = menu.addAction( KABC::Addressee::urlLabel() );
      homepageAction->setCheckable( true );
      homepageAction->setChecked( columns.contains( AkonadiFuture::ContactsTreeModel::Homepage ) );

      QAction *noteAction = menu.addAction( KABC::Addressee::noteLabel() );
      noteAction->setCheckable( true );
      noteAction->setChecked( columns.contains( AkonadiFuture::ContactsTreeModel::Note ) );

      if ( menu.exec( ((QContextMenuEvent*)event)->globalPos() ) ) {
        AkonadiFuture::ContactsTreeModel::Columns columns;

        if ( fullNameAction->isChecked() )
          columns << AkonadiFuture::ContactsTreeModel::FullName;
        if ( birthdayAction->isChecked() )
          columns << AkonadiFuture::ContactsTreeModel::Birthday;
        if ( homeAddressAction->isChecked() )
          columns << AkonadiFuture::ContactsTreeModel::HomeAddress;
        if ( businessAddressAction->isChecked() )
          columns << AkonadiFuture::ContactsTreeModel::BusinessAddress;
        if ( phoneNumbersAction->isChecked() )
          columns << AkonadiFuture::ContactsTreeModel::PhoneNumbers;
        if ( preferredEmailAction->isChecked() )
          columns << AkonadiFuture::ContactsTreeModel::PreferredEmail;
        if ( allEmailsAction->isChecked() )
          columns << AkonadiFuture::ContactsTreeModel::AllEmails;
        if ( organizationAction->isChecked() )
          columns << AkonadiFuture::ContactsTreeModel::Organization;
        if ( homepageAction->isChecked() )
          columns << AkonadiFuture::ContactsTreeModel::Homepage;
        if ( noteAction->isChecked() )
          columns << AkonadiFuture::ContactsTreeModel::Note;

        mModel->setColumns( columns );
        QTimer::singleShot( 0, this, SLOT( adaptHeaderView() ) );
      }

      return true;
    } else
      return false;
  }

  return false;
}

void ModelColumnManager::adaptHeaderView()
{
  QHeaderView *view = qobject_cast<QHeaderView*>( mWidget );
  if ( view ) {
    view->resizeSections( QHeaderView::Stretch );

    view->setDefaultAlignment( mModel->columns().count() == 1 ? Qt::AlignCenter : Qt::AlignLeft );
  }
}
