Adding widgetName symbol loading
[baulk.git] / src / Common / libraryloader.cpp
blob05b009d4f59fae27ce020feb46bcd0cfe112d428
1 // Baulk - Common - Library Loader
2 //
3 // Baulk - Copyright (C) 2008 - Jacob Alexander
4 //
5 // Baulk is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // any later version, including version 3 of the License.
9 //
10 // Baulk 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 General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "baulkxml.h"
19 #include "libraryloader.h"
21 // Constructors ***********************************************************************************
22 LibraryLoader::LibraryLoader( QObject *parent ) : QLibrary( parent ) {
23 setupLibraryLoader( parent );
26 LibraryLoader::LibraryLoader( QString libraryName, QObject *parent ) : QLibrary( parent ) {
27 setupLibraryLoader( parent );
28 loadLibrary( libraryName );
31 void LibraryLoader::setupLibraryLoader( QObject *parent ) {
32 BaulkXML xmlConfig( "BaulkLibs", this );
34 // Default Library Directory Search - In order
35 libraryDirs = QStringList()
36 << QString("%1/BaulkLibs").arg( QDir::currentPath() ) // Build Directory
37 << "/usr/local/lib/BaulkLibs"
38 << "/usr/lib/BaulkLibs";
40 // Override Library Search if Configuration Found
41 if ( xmlConfig.loadSuccessful() ) {
42 QStringList tmp;
43 tmp = xmlConfig.option("libraryList").toString().split(", ");
44 libraryDirs = tmp.count() < 1 ? libraryDirs : tmp;
46 else {
47 // Save a Copy of the library directories
48 if ( libraryDirs.count() > 0 ) {
49 xmlConfig.setOption( "libraryList", QVariant( libraryDirs.join(", ") ) );
50 xmlConfig.saveConfig();
54 // Revise Library Directory List
55 QDir dir;
56 for ( int c = 0; c < libraryDirs.count(); ++c )
57 if ( !dir.exists( libraryDirs[ c ] ) ) {
58 libraryDirs.removeAt( c );
59 --c;
62 if ( libraryDirs.count() < 1 )
63 qCritical( tr("%1\n\tNo library directories could be found").arg( errorName() ).toUtf8() );
65 // Library Filters
66 libraryFilters = QStringList()
67 << "lib*.so"
68 << "lib*.dll";
71 // Error List *************************************************************************************
72 QStringList LibraryLoader::errorList() const {
73 return allErrors;
76 // Library Loaders ********************************************************************************
77 bool LibraryLoader::loadLibrary( QString libraryName ) {
78 setFileName( determineLibraryPath( libraryName ) );
79 setProperty( "widgetName", ( (QString(*)()) lrResolve("widgetName") )() );
80 bool success = load();
81 if ( !success )
82 allErrors << errorString();
83 return success;
86 bool LibraryLoader::loadLibrary( QString libraryName, int versionNumber ) {
87 setFileNameAndVersion( determineLibraryPath( libraryName ), versionNumber );
88 bool success = load();
89 if ( !success )
90 allErrors << errorString();
91 return success;
94 bool LibraryLoader::loadLibrary( QString libraryName, bool detectVersion, QString versionNumber ) {
95 if ( detectVersion )
96 setFileName( determineLibraryPath( libraryName ) ); // TODO - Implement version detection here
97 else
98 setFileNameAndVersion( determineLibraryPath( libraryName ), versionNumber );
100 bool success = load();
101 if ( !success )
102 allErrors << errorString();
103 return success;
106 // Symbol Resolvers *******************************************************************************
107 void *LibraryLoader::lrResolve( QString symbol ) {
108 const char *symbolName = symbol.toUtf8().data();
109 void *tmp = resolve( symbolName );
110 if ( allErrors.isEmpty() || allErrors.last() != errorString() )
111 allErrors << errorString();
112 return tmp;
115 BaulkWidget *LibraryLoader::loadBaulkWidget( QString symbolBase, BaulkWidget *parent ) {
116 for ( int c = 0; c < symbolList().count(); ++c )
117 if ( symbolList()[c].contains( symbolBase ) )
118 return ( (BaulkWidget*(*)( QWidget* )) lrResolve( symbolList()[c] ) )( parent );
120 qCritical( QString("%1\n\tNo %2 symbol exists for BaulkWidget load").arg( errorName() ).arg( symbolBase ).toUtf8() );
121 return 0;
124 QAction *LibraryLoader::loadQAction( QString symbolBase, BaulkWidget *parent ) {
125 for ( int c = 0; c < symbolList().count(); ++c )
126 if ( symbolList()[c].contains( symbolBase ) )
127 return ( (QAction*(*)( QObject* )) lrResolve( symbolList()[c] ) )( parent );
129 qCritical( QString("%1\n\tNo %2 symbol exists for QAction load").arg( errorName() ).arg( symbolBase ).toUtf8() );
130 return 0;
133 QObject *LibraryLoader::loadQObject( QString symbolBase, BaulkWidget *parent ) {
134 for ( int c = 0; c < symbolList().count(); ++c )
135 if ( symbolList()[c].contains( symbolBase ) )
136 return ( (QObject*(*)( QObject* )) lrResolve( symbolList()[c] ) )( parent );
138 qCritical( QString("%1\n\tNo %2 symbol exists for QObject load").arg( errorName() ).arg( symbolBase ).toUtf8() );
139 return 0;
142 // Gives the full path to the detected Baulk Library **********************************************
143 QString LibraryLoader::determineLibraryPath( QString libraryName ) {
144 for ( int c = 0; c < libraryDirs.count(); ++c ) {
145 QString dir;
146 if ( !libraryName.contains("lib") ) {
147 dir = libraryDirs[c] + "/lib" + libraryName;
148 #ifdef Q_OS_WIN32
149 dir += ".dll";
150 #else
151 dir += ".so";
152 #endif
154 else
155 dir = libraryDirs[c] + "/" + libraryName;
157 if ( QLibrary::isLibrary( dir ) )
158 return dir;
160 qFatal( tr("%1\n\tCould not find specified Library\n\t||%2").arg( errorName() ).arg( libraryName ).toUtf8() );
161 return "";
164 // Library Existance ******************************************************************************
165 bool LibraryLoader::exists( QString libraryName ) {
166 return isLibrary( determineLibraryPath( libraryName ) );
169 QStringList LibraryLoader::loadableLibraries() {
170 if ( libraryDirs.count() > 0 )
171 return QDir( libraryDirs.first() ).entryList( libraryFilters );
173 return QStringList();
176 // Symbol List ************************************************************************************
177 QStringList LibraryLoader::symbolList() {
178 if ( loadableSymbols.count() < 1 )
179 loadableSymbols = ( (QStringList(*)()) lrResolve("symbolList") )();
180 return loadableSymbols;
183 // Widget Name
184 QString LibraryLoader::widgetName() const {
185 return property("widgetName").toString();