interfaces
iplugin.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "iplugin.h"
00030 #include <kglobal.h>
00031 #include <kglobalsettings.h>
00032 #include <kcomponentdata.h>
00033 #include <kiconloader.h>
00034 #include <kmainwindow.h>
00035 #include <kxmlguiwindow.h>
00036 #include <kxmlguifactory.h>
00037 #include <kdebug.h>
00038 #include "icore.h"
00039 #include "iplugincontroller.h"
00040 #include "iprojectcontroller.h"
00041 #include "contextmenuextension.h"
00042 #include <QtDesigner/QExtensionFactory>
00043 #include <QtDesigner/QExtensionManager>
00044
00045 namespace KDevelop
00046 {
00047
00048 class PluginExtensionFactory : public QExtensionFactory {
00049 public:
00050 PluginExtensionFactory( const QStringList& extensions, QExtensionManager *parent = 0 )
00051 :QExtensionFactory( parent ), m_extensions( extensions )
00052 {
00053 }
00054 protected:
00055 virtual QObject *createExtension(QObject* object, const QString& iid, QObject* parent ) const
00056 {
00057 Q_UNUSED(parent)
00058 if( !m_extensions.contains( iid ) )
00059 return 0;
00060 IPlugin* p = qobject_cast<IPlugin *>(object);
00061 if( !p )
00062 return 0;
00063 return object;
00064 }
00065 private:
00066 QStringList m_extensions;
00067 };
00068
00069 class IPluginPrivate
00070 {
00071 public:
00072 IPluginPrivate(IPlugin *q)
00073 : q(q), iconLoader(0), m_factory(0)
00074 {}
00075
00076 ~IPluginPrivate()
00077 {
00078 delete iconLoader;
00079 }
00080
00081 void _k_guiClientAdded(KXMLGUIClient *client)
00082 {
00083 if (client != q)
00084 return;
00085
00086 q->initializeGuiState();
00087 _k_updateState();
00088 }
00089
00090 void _k_updateState()
00091 {
00092 const int projectCount =
00093 ICore::self()->projectController()->projectCount();
00094
00095 IPlugin::ReverseStateChange reverse = IPlugin::StateNoReverse;
00096 if (! projectCount)
00097 reverse = IPlugin::StateReverse;
00098
00099 q->stateChanged(QLatin1String("has_project"), reverse);
00100 }
00101
00102 IPlugin *q;
00103 ICore *core;
00104 KIconLoader* iconLoader;
00105 PluginExtensionFactory* m_factory;
00106 QStringList m_extensions;
00107 };
00108
00109 IPlugin::IPlugin( const KComponentData &instance, QObject *parent )
00110 : QObject( parent ),
00111 KXMLGUIClient(), d( new IPluginPrivate(this) )
00112 {
00113
00114
00115
00116
00117
00118
00119 d->core = static_cast<KDevelop::ICore*>(parent);
00120 setComponentData( instance );
00121
00122 foreach (KMainWindow* mw, KMainWindow::memberList()) {
00123 KXmlGuiWindow* guiWindow = qobject_cast<KXmlGuiWindow*>(mw);
00124 if (! guiWindow)
00125 continue;
00126
00127 connect(guiWindow->guiFactory(), SIGNAL(clientAdded(KXMLGUIClient *)),
00128 this, SLOT(_k_guiClientAdded(KXMLGUIClient *)));
00129 }
00130
00131 connect(ICore::self()->projectController(),
00132 SIGNAL(projectOpened(KDevelop::IProject *)),
00133 this, SLOT(_k_updateState()));
00134
00135 connect(ICore::self()->projectController(),
00136 SIGNAL(projectClosed(KDevelop::IProject *)),
00137 this, SLOT(_k_updateState()));
00138 }
00139
00140 IPlugin::~IPlugin()
00141 {
00142 delete d;
00143 }
00144
00145 void IPlugin::unload()
00146 {
00147 ICore::self()->pluginController()->pluginUnloading(this);
00148 }
00149
00150 KIconLoader *IPlugin::iconLoader() const
00151 {
00152 if ( d->iconLoader == 0 ) {
00153 d->iconLoader = new KIconLoader( componentData().componentName(), componentData().dirs() );
00154 d->iconLoader->addAppDir( "kdevelop" );
00155 connect(KGlobalSettings::self(), SIGNAL(iconChanged(int)),
00156 this, SLOT(newIconLoader()));
00157 }
00158
00159 return d->iconLoader;
00160 }
00161
00162 void IPlugin::newIconLoader() const
00163 {
00164 if (d->iconLoader) {
00165 d->iconLoader->reconfigure( componentData().componentName(), componentData().dirs() );
00166 }
00167 }
00168
00169 ICore *IPlugin::core() const
00170 {
00171 return d->core;
00172 }
00173
00174 QExtensionManager* IPlugin::extensionManager()
00175 {
00176 return core()->pluginController()->extensionManager();
00177 }
00178
00179 }
00180
00181 QStringList KDevelop::IPlugin::extensions( ) const
00182 {
00183 return d->m_extensions;
00184 }
00185
00186 void KDevelop::IPlugin::registerExtensions( )
00187 {
00188 if( extensions().isEmpty() )
00189 return;
00190 if( !d->m_factory )
00191 d->m_factory = new PluginExtensionFactory( extensions(), extensionManager() );
00192 Q_FOREACH( QString ext, extensions() )
00193 {
00194 extensionManager()->registerExtensions( d->m_factory, ext );
00195 }
00196 }
00197
00198 void KDevelop::IPlugin::unregisterExtensions( )
00199 {
00200 if( extensions().isEmpty() )
00201 return;
00202 if( !d->m_factory )
00203 return;
00204 Q_FOREACH( QString ext, extensions() )
00205 {
00206 extensionManager()->unregisterExtensions( d->m_factory, ext );
00207 }
00208
00209 }
00210
00211 void KDevelop::IPlugin::addExtension( const QString& ext )
00212 {
00213 d->m_extensions << ext;
00214 }
00215
00216 KDevelop::ContextMenuExtension KDevelop::IPlugin::contextMenuExtension(
00217 KDevelop::Context* )
00218 {
00219 return KDevelop::ContextMenuExtension();
00220 }
00221
00222 void KDevelop::IPlugin::initializeGuiState()
00223 { }
00224
00225 #include "iplugin.moc"
00226
00227