1 // Baulk - Common - Library Loader
3 // Baulk - Copyright (C) 2008 - Jacob Alexander
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.
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/>.
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() ) {
43 tmp
= xmlConfig
.option("libraryList").toString().split(", ");
44 libraryDirs
= tmp
.count() < 1 ? libraryDirs
: tmp
;
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
56 for ( int c
= 0; c
< libraryDirs
.count(); ++c
)
57 if ( !dir
.exists( libraryDirs
[ c
] ) ) {
58 libraryDirs
.removeAt( c
);
62 if ( libraryDirs
.count() < 1 )
63 qCritical( tr("%1\n\tNo library directories could be found").arg( errorName() ).toUtf8() );
66 libraryFilters
= QStringList()
71 // Error List *************************************************************************************
72 QStringList
LibraryLoader::errorList() const {
76 // Library Loaders ********************************************************************************
77 bool LibraryLoader::loadLibrary( QString libraryName
) {
78 setFileName( determineLibraryPath( libraryName
) );
79 setProperty( "widgetName", ( (QString(*)()) lrResolve("widgetName") )() );
80 bool success
= load();
82 allErrors
<< errorString();
86 bool LibraryLoader::loadLibrary( QString libraryName
, int versionNumber
) {
87 setFileNameAndVersion( determineLibraryPath( libraryName
), versionNumber
);
88 bool success
= load();
90 allErrors
<< errorString();
94 bool LibraryLoader::loadLibrary( QString libraryName
, bool detectVersion
, QString versionNumber
) {
96 setFileName( determineLibraryPath( libraryName
) ); // TODO - Implement version detection here
98 setFileNameAndVersion( determineLibraryPath( libraryName
), versionNumber
);
100 bool success
= load();
102 allErrors
<< errorString();
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();
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() );
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() );
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() );
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
) {
146 if ( !libraryName
.contains("lib") ) {
147 dir
= libraryDirs
[c
] + "/lib" + libraryName
;
155 dir
= libraryDirs
[c
] + "/" + libraryName
;
157 if ( QLibrary::isLibrary( dir
) )
160 qFatal( tr("%1\n\tCould not find specified Library\n\t||%2").arg( errorName() ).arg( libraryName
).toUtf8() );
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
;
184 QString
LibraryLoader::widgetName() const {
185 return property("widgetName").toString();