/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/
/*
Author: Christian Hubinger <chubinger@gmail.com>, (C) 2001-2004
*/

#include "kmfipteditorpart.h"


// QT includes
#include <qfile.h>
#include <qtextstream.h>
#include <qmultilineedit.h>


// KDE includes
#include <kinstance.h>
#include <kaction.h>
#include <kstdaction.h>
#include <kaccel.h>
#include <kfiledialog.h>
#include <klocale.h>
#include <kdebug.h>
#include <kmessagebox.h>
#include <kiconloader.h>

// Project includes
#include "kmfruleedit.h"
#include "../interfaces/kmyfirewallinterface.h"
#include "../kmfwidgets/kmfmainwindow.h"
#include "../kmfwidgets/kmfiptdocoptions.h"
#include "../core/kmfdoc.h"
#include "../core/kmfiptdoc.h"

KMFIPTEditorPart::KMFIPTEditorPart( QWidget *parentWidget, const char *widgetName,
                                  QObject *parent, const char *name )
	: KParts::ReadWritePart( parent, name ) {
	KMFMainWindow *app = 0;
	// we need an instance
	setInstance( KMFIPTEditorPartFactory::instance() );
	
	// this should be your custom internal widget
	m_ruleedit = new KMFRuleEdit( parentWidget, widgetName );
	m_ruleedit->setFocusPolicy( QWidget::ClickFocus );
	
	m_editdoc = new KMFIPTDocOptions( parentWidget , "m_editdoc" );
	m_editdoc->hide();

	app = dynamic_cast<KMFMainWindow*>( parent );
	if ( ! app ) {
		KMessageBox::error(0,"Oops wrong parent class found for kmfinstallerplugin!!!");
	}
	
	m_ruleedit->loadDoc( (KMFIPTDoc*) app->doc() );
	m_editdoc->loadDoc( (KMFIPTDoc*) app->doc() );
	
	connect( app, SIGNAL( sigHasDoc( bool ) ),
		m_ruleedit, SLOT( setEnabled( bool ) ) );

	connect( app, SIGNAL( sigUpdateView() ),
		m_ruleedit, SLOT( slotUpdateView() ) );

	connect( app, SIGNAL( sigEnableActions( bool ) ),
		this, SLOT( slotEnableActions( bool ) ) );

	connect ( m_editdoc, SIGNAL( sigConfigChanged() ),
	          m_ruleedit, SLOT( slotUpdateView() ) );

	// notify the part that this is our internal widget
	setWidget( m_ruleedit );
	
	m_actionEditChain = new KAction( i18n( "&Edit Chain" ), QIconSet( BarIcon( "configure_toolbars", KMFIPTEditorPartFactory::instance() ) ),
	                                 0 , this, SLOT( slotEditChain() ), actionCollection(), "edit_chain" );

	m_actionNewChain = new KAction( i18n( "Add New Chain..." ), QIconSet( BarIcon( "view_tree", KMFIPTEditorPartFactory::instance() ) ),
	                                0 , this, SLOT( slotNewChain() ), actionCollection(), "new_chain" );

	m_actionDelChain = new KAction( i18n( "Delete Chain" ), QIconSet( BarIcon( "editdelete", KMFIPTEditorPartFactory::instance() ) ),
	                                0 , this, SLOT( slotDelChain() ), actionCollection(), "del_chain" );

	
	m_actionNewRule = new KAction( i18n( "Add New Rule..." ), QIconSet( KGlobal:: iconLoader()->loadIcon( "rule", KIcon::Toolbar ) ) ,
								   KStdAccel::shortcut(KStdAccel::New) , this, SLOT( slotNewRule() ), actionCollection(), "new_rule" );
	
//	m_actionNewRule->setIconSet( QIconSet(  loader->loadIcon( "rule-22", KIcon::User ) ) );							   
								   
	m_actionDelRule = new KAction( i18n( "Delete Rule" ), QIconSet( BarIcon( "editdelete", KMFIPTEditorPartFactory::instance() ) ),
								   KStdAccel::shortcut(KStdAccel::DeleteWordBack), this, SLOT( slotDelRule() ), actionCollection(), "del_rule" );
	
	m_actionEditDocOptions = new KAction( i18n( "&Configure Firewall Options..." ), "configure", 0 , this, SLOT( slotEditDocOptions() ),
	                                    actionCollection(), "edit_doc_options" );


	// set our XML-UI resource file
	setXMLFile( "kmfipteditorpartui.rc" );

	// we are read-write by default
	setReadWrite( true );

	// we are not modified since we haven't done anything yet
	setModified( false );
}

KMFIPTEditorPart::~KMFIPTEditorPart() {}

void KMFIPTEditorPart::setReadWrite( bool rw ) {
	// notify your internal widget of the read-write state

	ReadWritePart::setReadWrite( rw );
}

void KMFIPTEditorPart::slotEditDocOptions() {
	KMFMainWindow* app = dynamic_cast<KMFMainWindow*>( parent() );
	if ( ! app ) {
		KMessageBox::error(0,"Oops wrong parent class found for KMFIPTEditorPart!!!");
		return;
	}
	m_editdoc->loadDoc( (KMFIPTDoc*) app->doc() );
	m_editdoc ->show();
}


void KMFIPTEditorPart::slotEditChain() {
	m_ruleedit->slotEditChain();
}

void KMFIPTEditorPart::slotNewChain() {
	m_ruleedit->slotAddChain();
}

void KMFIPTEditorPart::slotDelChain() {
	m_ruleedit->slotDelChain();
}

void KMFIPTEditorPart::slotNewRule() {
	m_ruleedit->slotAddRule();
}

void KMFIPTEditorPart::slotDelRule() {
	m_ruleedit->slotDelRule();
}

//###########################################
void KMFIPTEditorPart::setModified( bool modified ) {
	// get a handle on our Save action and make sure it is valid
	KAction * save = actionCollection() ->action( KStdAction::stdName( KStdAction::Save ) );
	if ( !save )
		return ;

	// if so, we either enable or disable it based on the current
	// state
	if ( modified )
		save->setEnabled( true );
	else
		save->setEnabled( false );

	// in any event, we want our parent to do it's thing
	ReadWritePart::setModified( modified );
}

bool KMFIPTEditorPart::openFile() {
	// m_file is always local so we can use QFile on it
	//     QFile file(m_file);
	//     if (file.open(IO_ReadOnly) == false)
	//         return false;
	//
	//     // our example widget is text-based, so we use QTextStream instead
	//     // of a raw QDataStream
	//     QTextStream stream(&file);
	//     QString str;
	//     while (!stream.eof())
	//         str += stream.readLine() + "\n";
	//
	//     file.close();
	//
	//     // now that we have the entire file, display it
	// //     m_widget->setText(str);
	//
	//     // just for fun, set the status bar
	//     emit setStatusBarText( m_url.prettyURL() );

	return true;
}

bool KMFIPTEditorPart::saveFile() {
	// if we aren't read-write, return immediately
	/*    if (isReadWrite() == false)
	        return false;
	 
	    // m_file is always local, so we use QFile
	    QFile file(m_file);
	    if (file.open(IO_WriteOnly) == false)
	        return false;
	 
	    // use QTextStream to dump the text to the file
	    QTextStream stream(&file);
	    stream << m_widget->text();
	 
	    file.close();
	*/ 
	return true;
}

void KMFIPTEditorPart::fileOpen() {
	// this slot is called whenever the File->Open menu is selected,
	// the Open shortcut is pressed (usually CTRL+O) or the Open toolbar
	// button is clicked
	/*    QString file_name = KFileDialog::getOpenFileName();
	 
	    if (file_name.isEmpty() == false)
	        openURL(file_name);*/
}

void KMFIPTEditorPart::fileSaveAs() {
	// this slot is called whenever the File->Save As menu is selected,
	/*    QString file_name = KFileDialog::getSaveFileName();
	    if (file_name.isEmpty() == false)
	        saveAs(file_name);*/
}

void KMFIPTEditorPart::slotEnableActions( bool on ) {
	if ( on ) {
		m_actionEditChain->setEnabled( true );
		m_actionNewChain->setEnabled( true );
		m_actionNewRule->setEnabled( true );
		m_actionDelChain->setEnabled( true );
		m_actionDelRule->setEnabled( true );
		m_actionEditDocOptions->setEnabled( true );
	} else {
		m_actionEditChain->setEnabled( false );
		m_actionNewChain->setEnabled( false );
		m_actionNewRule->setEnabled( false );
		m_actionDelChain->setEnabled( false );
		m_actionDelRule->setEnabled( false );
		m_actionEditDocOptions->setEnabled( false );

	}
}


// It's usually safe to leave the factory code alone.. with the
// notable exception of the KAboutData data
#include <kaboutdata.h>
#include <klocale.h>

KInstance* KMFIPTEditorPartFactory::s_instance = 0L;
KAboutData* KMFIPTEditorPartFactory::s_about = 0L;

KMFIPTEditorPartFactory::KMFIPTEditorPartFactory()
		: KParts::Factory() {}

KMFIPTEditorPartFactory::~KMFIPTEditorPartFactory() {
	delete s_instance;
	delete s_about;

	s_instance = 0L;
}

KParts::Part* KMFIPTEditorPartFactory::createPartObject( QWidget *parentWidget, const char *widgetName,
        QObject *parent, const char *name,
        const char *classname, const QStringList& ) {
	// Create an instance of our Part
	KMFIPTEditorPart * obj = new KMFIPTEditorPart( parentWidget, widgetName, parent, name );

	// See if we are to be read-write or not
	if ( QCString( classname ) == "KParts::ReadOnlyPart" )
		obj->setReadWrite( false );

	return obj;
}

KInstance* KMFIPTEditorPartFactory::instance() {
	if ( !s_instance ) {
		s_about = new KAboutData( "kmfipteditorpart", I18N_NOOP( "kmfipteditorpartPart" ), "0.1" );
		s_about->addAuthor( "Christian Hubinger", 0, "chubinger@gmail.com" );
		s_instance = new KInstance( s_about );
	}
	return s_instance;
}

extern "C" {
	void* init_libkmfipteditorpart() {
		return new KMFIPTEditorPartFactory;
	}
}

#include "kmfipteditorpart.moc"
