/*    
    This file is part of kenvy24 KDE multimedia utility
    Copyright (c) 2007  Valentin RUSU kenvy24@rusu.info
    
    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) version 3 or, at the discretion of KDE e.V. (which shall
    act as a proxy as in section 14 of the GPLv3), 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, see <http://www.gnu.org/licenses/>.     
*/
#ifndef LOADPART_H_INCLUDED
#define LOADPART_H_INCLUDED

#include <QApplication>
#include <kdebug.h>
#include <kparts/part.h>
#include <kparts/partmanager.h>
#include <klibloader.h>
#include <kmessagebox.h>

inline KParts::ReadWritePart* loadPart(QWidget* parent, KParts::PartManager& manager, const char* name, bool quitOnFailure = true)
{
    KParts::ReadWritePart* thePart = 0;
    kDebug() << "Trying to create " << name;
    KPluginLoader loader( name );
    KPluginFactory* factory = loader.factory();
    if (factory)
    {
        thePart = factory->create< KParts::ReadWritePart >( parent );
        if (thePart)
        {
            manager.addPart( thePart, true );
        }
        else
        {
            kDebug() << "ERROR: Cannot instantiate part";
        }
    }
    else
    {
        kDebug() << "Cannot find part " << name;
        if ( quitOnFailure )
        {
            KMessageBox::error(0, "Could not find mixer part!");
            qApp->quit();
        }
    }
    return thePart;
}

#endif // LOADPART_H_INCLUDED

