/* This file is part of the KDE Project
   Copyright (c) 2005 Jean-Remy Falleri <jr.falleri@laposte.net>
   Copyright (c) 2005 Kévin Ottens <ervin ipsquad net>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   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., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "notifiermodule.h"

#include <tdelocale.h>

#include <tqlayout.h>
#include <kcombobox.h>
#include <kpushbutton.h>
#include <kstdguiitem.h>

#include "notifiersettings.h"
#include "serviceconfigdialog.h"
#include "actionlistboxitem.h"
#include "mimetypelistboxitem.h"

NotifierModule::NotifierModule(TQWidget *parent, const char *name)
	: TDECModule(parent, name)
{
	TQBoxLayout *layout = new TQVBoxLayout( this, 0, KDialog::spacingHint() );
	
	m_view = new NotifierModuleView( this );
	layout->addWidget( m_view );
	
	m_view->addButton->setGuiItem( KStdGuiItem::add() );
	m_view->editButton->setGuiItem( KStdGuiItem::properties() );
	m_view->deleteButton->setGuiItem( KStdGuiItem::del() );
	
	m_view->mimetypesCombo->insertItem( i18n("All Mime Types") );

	TQStringList mimetypes = m_settings.supportedMimetypes();

	TQStringList::iterator it = mimetypes.begin();
	TQStringList::iterator end = mimetypes.end();

	for ( ; it!=end; ++it )
	{
		new MimetypeListBoxItem( *it, m_view->mimetypesCombo->listBox() );
	}
	
	updateListBox();

	connect( m_view->mimetypesCombo, TQT_SIGNAL( activated(int) ),
	         this, TQT_SLOT( slotMimeTypeChanged(int) ) );
	connect( m_view->actionsList, TQT_SIGNAL( selectionChanged(TQListBoxItem*) ),
	         this, TQT_SLOT( slotActionSelected(TQListBoxItem*) ) );
	connect( m_view->addButton, TQT_SIGNAL( clicked() ),
	         this, TQT_SLOT( slotAdd() ) );
	connect( m_view->editButton, TQT_SIGNAL( clicked() ),
	         this, TQT_SLOT( slotEdit() ) );
	connect( m_view->deleteButton, TQT_SIGNAL( clicked() ),
	         this, TQT_SLOT( slotDelete() ) );
	connect( m_view->toggleAutoButton, TQT_SIGNAL( clicked() ),
	         this, TQT_SLOT( slotToggleAuto() ) );
}

NotifierModule::~NotifierModule()
{
}

void NotifierModule::load()
{
	m_settings.reload();
	slotMimeTypeChanged( m_view->mimetypesCombo->currentItem() );
}

void NotifierModule::save()
{
	m_settings.save();
}

void NotifierModule::defaults()
{
	m_settings.clearAutoActions();
	slotMimeTypeChanged( m_view->mimetypesCombo->currentItem() );
}

void NotifierModule::updateListBox()
{
	m_view->actionsList->clear();
	slotActionSelected( 0L );

	TQValueList<NotifierAction*> services;
	if ( m_mimetype.isEmpty() )
	{
		services = m_settings.actions();
	}
	else
	{
		services = m_settings.actionsForMimetype( m_mimetype );
	}

	TQValueList<NotifierAction*>::iterator it;
	
	for ( it = services.begin(); it != services.end(); ++it )
	{
		new ActionListBoxItem( *it, m_mimetype, m_view->actionsList );
	}
}

void NotifierModule::slotActionSelected(TQListBoxItem *item)
{
	NotifierAction *action = 0L;

	if ( item!=0L )
	{
		ActionListBoxItem *action_item
			= static_cast<ActionListBoxItem*>(item);
		action = action_item->action();
	}

	bool isWritable = action!=0L && action->isWritable();
	m_view->deleteButton->setEnabled( isWritable );
	m_view->editButton->setEnabled( isWritable );
	m_view->addButton->setEnabled( TRUE );
	m_view->toggleAutoButton->setEnabled( action!=0L && !m_mimetype.isEmpty() );
}

void NotifierModule::slotMimeTypeChanged(int index)
{
	if ( index == 0 )
	{
		m_mimetype = TQString();
	}
	else
	{
		TQListBoxItem *item = m_view->mimetypesCombo->listBox()->item( index );
		MimetypeListBoxItem *mime_item
			= static_cast<MimetypeListBoxItem*>( item );
		m_mimetype = mime_item->mimetype();
	}

	updateListBox();
}

void NotifierModule::slotAdd()
{
	NotifierServiceAction *action = new NotifierServiceAction();
	ServiceConfigDialog dialog(action, m_settings.supportedMimetypes(), this);
	
	int value = dialog.exec();
	
	if ( value == TQDialog::Accepted )
	{
		m_settings.addAction( action );
		updateListBox();
		emit changed( true );
	}
	else
	{
		delete action;
	}
}

void NotifierModule::slotEdit()
{
	ActionListBoxItem *action_item
		= static_cast<ActionListBoxItem*>(m_view->actionsList->selectedItem());
	
	NotifierServiceAction * action = dynamic_cast<NotifierServiceAction*>( action_item->action() );
	if ( action )
	{
		ServiceConfigDialog dialog(action, m_settings.supportedMimetypes(), this);
		
		int value = dialog.exec();
		
		if ( value == TQDialog::Accepted )
		{
			updateListBox();
			emit changed( true );
		}
	}
}

void NotifierModule::slotDelete()
{
	ActionListBoxItem *action_item
		= static_cast<ActionListBoxItem*>(m_view->actionsList->selectedItem());
	
	NotifierServiceAction *action;
        action = dynamic_cast<NotifierServiceAction*>( action_item->action() );
	if ( action )
	{
		m_settings.deleteAction( action );
		updateListBox();
		emit changed( true );
	}
}

void NotifierModule::slotToggleAuto()
{
	ActionListBoxItem *action_item
		= static_cast<ActionListBoxItem*>( m_view->actionsList->selectedItem() );
	NotifierAction *action = action_item->action();

	int index = m_view->actionsList->index( action_item );
	
	if ( action->autoMimetypes().contains( m_mimetype ) )
	{
		m_settings.resetAutoAction( m_mimetype );
	}
	else
	{
		m_settings.setAutoAction( m_mimetype, action );
	}

	updateListBox();
	emit changed( true );

	m_view->actionsList->setSelected( index, true );
}

#include "notifiermodule.moc"