00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "gluonobjectfactory.h"
00021 #include "gluonobject.h"
00022 #include "debughelper.h"
00023 #include "gluon_global.h"
00024
00025 #include <QtCore/QDir>
00026 #include <QtCore/QPluginLoader>
00027 #include <QtGui/QApplication>
00028 #include <QVariant>
00029
00030 #include <QtCore/QDebug>
00031
00032 using namespace GluonCore;
00033
00034 template<> GluonObjectFactory* Singleton<GluonObjectFactory>::m_instance = 0;
00035
00036 QStringList
00037 GluonObjectFactory::objectTypeNames() const
00038 {
00039 QStringList theNames;
00040
00041 QHash<QString, const QMetaObject*>::const_iterator i;
00042 for( i = m_objectTypes.constBegin(); i != m_objectTypes.constEnd(); ++i )
00043 theNames << i.key();
00044
00045 return theNames;
00046 }
00047
00048 QHash<QString, const QMetaObject*>
00049 GluonObjectFactory::objectTypes() const
00050 {
00051 return m_objectTypes;
00052 }
00053
00054 const QHash<QString, int>
00055 GluonObjectFactory::objectTypeIDs() const
00056 {
00057 return m_objectTypeIDs;
00058 }
00059
00060 QStringList
00061 GluonObjectFactory::objectMimeTypes() const
00062 {
00063 return m_mimeTypes.keys();
00064 }
00065
00066 GluonObject *
00067 GluonObjectFactory::instantiateObjectByName( const QString& objectTypeName )
00068 {
00069 DEBUG_BLOCK
00070 QString fullObjectTypeName( objectTypeName );
00071 if( !objectTypeName.contains( "::" ) )
00072 fullObjectTypeName = QString( "Gluon::" ) + fullObjectTypeName;
00073
00074 if( m_objectTypes.keys().indexOf( fullObjectTypeName ) > -1 )
00075 {
00076 const QMetaObject* meta = m_objectTypes.value( objectTypeName );
00077 if( meta )
00078 {
00079 GluonObject* obj = qobject_cast<GluonObject*>( meta->newInstance() );
00080 if( obj )
00081 {
00082
00083 return obj;
00084 }
00085 else
00086 {
00087 DEBUG_TEXT2( "If you are seeing this message, you have most likely failed to mark the constructor of %1 as Q_INVOKABLE", objectTypeName );
00088 return 0;
00089 }
00090 }
00091 }
00092
00093 DEBUG_TEXT( QString( "Object type named %1 not found in factory!" ).arg( objectTypeName ) );
00094
00095 return 0;
00096 }
00097
00098 GluonObject*
00099 GluonObjectFactory::instantiateObjectByMimetype( const QString& objectMimeType )
00100 {
00101 return instantiateObjectByName( m_mimeTypes[objectMimeType] );
00102 }
00103
00104 QVariant
00105 GluonObjectFactory::wrapObject( const QVariant& original, GluonObject* newValue )
00106 {
00107 DEBUG_BLOCK
00108 QString type = original.typeName();
00109
00110 if( type.endsWith( '*' ) )
00111 {
00112 type = type.left( type.length() - 1 );
00113 }
00114
00115 if( m_objectTypes.contains( type ) )
00116 {
00117 QScopedPointer<GluonObject> obj( instantiateObjectByName( type ) );
00118 return obj->toVariant( newValue );
00119 }
00120
00121 DEBUG_TEXT( QString( "Warning: Type %1 not found." ).arg( type ) );
00122
00123 return QVariant();
00124 }
00125
00126 QVariant
00127 GluonObjectFactory::wrapObject( const QString& type, GluonObject* newValue )
00128 {
00129 DEBUG_BLOCK
00130 QString typeName = type;
00131 if( type.endsWith( '*' ) )
00132 typeName = type.left( type.length() - 1 );
00133
00134 if( m_objectTypes.contains( typeName ) )
00135 {
00136 QScopedPointer<GluonObject> obj( instantiateObjectByName( typeName ) );
00137 return obj->toVariant( newValue );
00138 }
00139
00140 DEBUG_TEXT( QString( "Warning: Type %1 not found." ).arg( typeName ) );
00141
00142 return QVariant();
00143 }
00144
00145 GluonObject *
00146 GluonObjectFactory::wrappedObject( const QVariant& wrappedObject )
00147 {
00148 QString type( wrappedObject.typeName() );
00149 QString typeName = type;
00150 if( type.endsWith( '*' ) )
00151 typeName = type.left( type.length() - 1 );
00152
00153 if( m_objectTypes.contains( typeName ) )
00154 {
00155 QScopedPointer<GluonObject> obj( instantiateObjectByName( typeName ) );
00156 return obj->fromVariant( wrappedObject );
00157 }
00158
00159 DEBUG_BLOCK
00160 DEBUG_TEXT( QString( "Warning: Type %1 not found." ).arg( typeName ) );
00161
00162 return 0;
00163 }
00164
00165 void
00166 GluonObjectFactory::loadPlugins()
00167 {
00168 DEBUG_FUNC_NAME
00169 QList<QDir> pluginDirs;
00170
00171 QDir pluginDir( QApplication::applicationDirPath() );
00172
00173 #if defined(Q_OS_WIN)
00174 if( pluginDir.dirName().toLower() == "debug" || pluginDir.dirName().toLower() == "release" )
00175 pluginDir.cdUp();
00176 #elif defined(Q_OS_MAC)
00177 if( pluginDir.dirName() == "MacOS" )
00178 {
00179 pluginDir.cdUp();
00180 }
00181 #endif
00182 if( pluginDir.cd( "PlugIns" ) )
00183 pluginDirs.append( pluginDir );
00184
00185 if( pluginDir.cd( GluonCore::Global::libDirectory() ) )
00186 pluginDirs.append( pluginDir );
00187
00188 if( pluginDir.cd( GluonCore::Global::libDirectory() + "/gluon" ) )
00189 pluginDirs.append( pluginDir );
00190
00191 if( pluginDir.cd( QDir::homePath() + "/gluonplugins" ) )
00192 pluginDirs.append( pluginDir );
00193
00194 DEBUG_TEXT( QString( "Number of plugin locations: %1" ).arg( pluginDirs.count() ) );
00195 foreach( QDir theDir, pluginDirs )
00196 {
00197 DEBUG_TEXT( QString( "Looking for pluggable components in %1" ).arg( theDir.absolutePath() ) );
00198
00199 #ifdef Q_WS_X11
00200
00201
00202 theDir.setNameFilters( QStringList() << QString( "*.so.%1.%2.%3" ).arg( GLUON_VERSION_MAJOR ).arg( GLUON_VERSION_MINOR ).arg( GLUON_VERSION_PATCH ) );
00203 #endif
00204
00205 DEBUG_TEXT( QString( "Found %1 potential plugins. Attempting to load..." ).arg( theDir.count() - 2 ) );
00206 foreach( const QString & fileName, theDir.entryList( QDir::Files ) )
00207 {
00208
00209 if( !fileName.contains( "gluon" ) )
00210 continue;
00211
00212
00213 if( !QLibrary::isLibrary( theDir.absoluteFilePath( fileName ) ) )
00214 continue;
00215
00216 QPluginLoader loader( theDir.absoluteFilePath( fileName ) );
00217 loader.load();
00218
00219 if( !loader.isLoaded() )
00220 {
00221 DEBUG_TEXT( loader.errorString() );
00222 }
00223 }
00224 }
00225
00226 DEBUG_TEXT2( "Total number of objects in factory after loading: %1", m_objectTypes.count() );
00227 }
00228
00229 #include "gluonobjectfactory.moc"