1 /* This file is part of the KDE project
2 Copyright (C) 2001 Simon Hausmann <hausmann@kde.org>
3 Copyright (C) 2002-2006 David Faure <faure@kde.org>
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
20 #ifndef KPARTS_COMPONENTFACTORY_H
21 #define KPARTS_COMPONENTFACTORY_H
23 #include <kparts/factory.h>
24 #include <kparts/part.h>
25 #include <kservicetypetrader.h>
26 #include <klibloader.h>
27 #include <kmimetypetrader.h>
31 namespace ComponentFactory
34 * This template function allows to ask the given kparts factory to
35 * create an instance of the given template type.
39 * KViewPart *doc = KParts::ComponentFactory::createPartInstanceFromFactory<KViewPart>( factory, parent );
42 * @deprecated use KPluginFactory::create instead
44 * @param factory The factory to ask for the creation of the component
45 * @param parentWidget the parent widget for the part
46 * @param parent The parent object (see QObject constructor)
47 * @param args A list of string arguments, passed to the factory and possibly
48 * to the component (see KLibFactory)
49 * @return A pointer to the newly created object or a null pointer if the
50 * factory was unable to create an object of the given type.
53 KDE_DEPRECATED T
*createPartInstanceFromFactory( KParts::Factory
*factory
,
54 QWidget
*parentWidget
= 0,
56 const QStringList
&args
= QStringList() )
58 KParts::Part
*object
= factory
->createPart( parentWidget
,
60 T::staticMetaObject
.className(),
63 T
*result
= dynamic_cast<T
*>( object
);
69 * @deprecated use KPluginFactory::create instead
72 KDE_DEPRECATED T
*createPartInstanceFromLibrary( const char *libraryName
,
73 QWidget
*parentWidget
= 0,
75 const QStringList
&args
= QStringList(),
78 KLibrary
*library
= KLibLoader::self()->library( libraryName
);
82 *error
= KLibLoader::ErrNoLibrary
;
85 KLibFactory
*factory
= library
->factory();
90 *error
= KLibLoader::ErrNoFactory
;
93 KParts::Factory
*partFactory
= dynamic_cast<KParts::Factory
*>( factory
);
98 *error
= KLibLoader::ErrNoFactory
;
101 T
*res
= createPartInstanceFromFactory
<T
>( partFactory
, parentWidget
,
107 *error
= KLibLoader::ErrNoComponent
;
113 * @deprecated use KService::createInstance instead
116 KDE_DEPRECATED T
*createPartInstanceFromService( const KService::Ptr
&service
,
117 QWidget
*parentWidget
= 0,
119 const QStringList
&args
= QStringList(),
122 QString library
= service
->library();
123 if ( library
.isEmpty() )
126 *error
= KLibLoader::ErrServiceProvidesNoLibrary
;
130 return createPartInstanceFromLibrary
<T
>( library
.toLocal8Bit().data(), parentWidget
,
131 parent
, args
, error
);
134 template <class T
, class ServiceIterator
>
135 KDE_DEPRECATED T
*createPartInstanceFromServices( ServiceIterator begin
,
137 QWidget
*parentWidget
= 0,
139 const QStringList
&args
= QStringList(),
142 for (; begin
!= end
; ++begin
)
144 KService::Ptr service
= *begin
;
149 T
*component
= createPartInstanceFromService
<T
>( service
, parentWidget
,
150 parent
, args
, error
);
156 *error
= KLibLoader::ErrNoServiceFound
;
163 * This method creates and returns a KParts part from a serviceType (e.g. a mimetype).
165 * You can use this method to create a generic viewer - that can display any
166 * kind of file, provided that there is a ReadOnlyPart installed for it - in 5 lines:
168 * // Given the following: KUrl url, QWidget* parentWidget and QObject* parentObject.
169 * QString mimetype = KMimeType::findByURL( url )->name();
170 * KParts::ReadOnlyPart* part = KParts::ComponentFactory::createPartInstanceFromQuery<KParts::ReadOnlyPart>( mimetype, QString(), parentWidget, parentObject );
172 * part->openUrl( url );
173 * part->widget()->show(); // also insert the widget into a layout, or simply use a KVBox as parentWidget
177 * @deprecated use KMimeTypeTrader::createPartInstanceFromQuery instead
179 * @param mimeType the mimetype which this part is associated with
180 * @param constraint an optional constraint to pass to the trader (see KTrader)
181 * @param parentWidget the parent widget, will be set as the parent of the part's widget
182 * @param parent the parent object for the part itself
183 * @param args A list of string arguments, passed to the factory and possibly
184 * to the component (see KLibFactory)
185 * @param error The int passed here will receive an error code in case of errors.
186 * (See enum KLibLoader::ComponentLoadingError)
187 * @return A pointer to the newly created object or a null pointer if the
188 * factory was unable to create an object of the given type.
191 T
*createPartInstanceFromQuery( const QString
&mimeType
,
192 const QString
&constraint
,
193 QWidget
*parentWidget
= 0,
195 const QStringList
&args
= QStringList(),
198 const KService::List offers
= KMimeTypeTrader::self()->query( mimeType
, QLatin1String("KParts/ReadOnlyPart"), constraint
);
199 if ( offers
.isEmpty() )
202 *error
= KLibLoader::ErrNoServiceFound
;
206 return createPartInstanceFromServices
<T
>( offers
.begin(), offers
.end(),
208 parent
, args
, error
);