/***************************************************************************
 *   Copyright (C) 2004 by Jesper K. Pedersen                              *
 *   blackie@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.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "mainwindow.h"

#include <qtextedit.h>
#include <kmainwindow.h>
#include <klocale.h>
#include <kstdaction.h>
#include <kstatusbar.h>
#include <qapplication.h>
#include <kparts/componentfactory.h>
#include <plugindemo/plugin.h>
#include <kdebug.h>
#include "myselectioninterface.h"

MainWindow::MainWindow()
    : KMainWindow( 0, "MainWindow" )
{
    // set the shell's ui resource file
//    setXMLFile("plugindemoappui.rc");

    // The editor instance
    _editor = new QTextEdit( this );
    setCentralWidget( _editor );

    KStdAction::cut( this, SLOT( slotCut() ), actionCollection() );
    KStdAction::copy( this, SLOT( slotCopy() ), actionCollection() );
    KStdAction::paste( this, SLOT( slotPaste() ), actionCollection() );
    KStdAction::quit( qApp, SLOT( quit() ), actionCollection() );

    statusBar()->message( "Wellcome to the plugin demo", 3000 );
    createGUI(); // This must be called before plugins are loaded.

    // Sets up the plugin interface, and load the plugins
    _pluginInterface = new QObject( this, "_pluginInterface" );
    new MySelectionInterface( _editor, _pluginInterface, "selection interface" );
    loadPlugins();

}

void MainWindow::slotCut( )
{
	_editor->cut();
}

void MainWindow::slotCopy( )
{
    _editor->copy();
}

void MainWindow::slotPaste( )
{
    _editor->paste();
}

void MainWindow::loadPlugins()
{
    KTrader::OfferList offers = KTrader::self()->query("PluginDemo/Plugin");

    KTrader::OfferList::ConstIterator iter;
    for(iter = offers.begin(); iter != offers.end(); ++iter) {
        KService::Ptr service = *iter;
        int errCode = 0;
        PluginDemo::Plugin* plugin =
            KParts::ComponentFactory::createInstanceFromService<PluginDemo::Plugin>
            ( service, _pluginInterface, 0, QStringList(), &errCode);
        // here we ought to check the error code.

        if (plugin) {
            guiFactory()->addClient(plugin);

            kdDebug() << "PluginDemo: Loaded plugin "
                      << plugin->name() << endl;
        }

    }
}

#include "mainwindow.moc"
