certmanager/lib
chiasmuslibrary.cpp00001
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
00030
00031
00032
00033
00034 #include "chiasmuslibrary.h"
00035
00036 #include "chiasmusbackend.h"
00037
00038 #include "kleo/cryptoconfig.h"
00039
00040 #include <klibloader.h>
00041 #include <kdebug.h>
00042 #include <klocale.h>
00043
00044 #include <qfile.h>
00045
00046 #include <vector>
00047 #include <algorithm>
00048
00049 #include <cassert>
00050 #include <cstdlib>
00051 #include <cstring>
00052
00053 Kleo::ChiasmusLibrary * Kleo::ChiasmusLibrary::self = 0;
00054
00055 Kleo::ChiasmusLibrary::ChiasmusLibrary() : mXiaLibrary( 0 ) {
00056 self = this;
00057 }
00058
00059 Kleo::ChiasmusLibrary::~ChiasmusLibrary() {
00060
00061 }
00062
00063 Kleo::ChiasmusLibrary::main_func Kleo::ChiasmusLibrary::chiasmus( QString * reason ) const {
00064 assert( ChiasmusBackend::instance() );
00065 assert( ChiasmusBackend::instance()->config() );
00066 const CryptoConfigEntry * lib = ChiasmusBackend::instance()->config()->entry( "Chiasmus", "General", "lib" );
00067 assert( lib );
00068 const QString libfile = lib->urlValue().path();
00069 if ( !mXiaLibrary )
00070 mXiaLibrary = KLibLoader::self()->library( QFile::encodeName( libfile ) );
00071 if ( !mXiaLibrary ) {
00072 if ( reason )
00073 *reason = i18n( "Failed to load %1: %2" )
00074 .arg( libfile,KLibLoader::self()->lastErrorMessage() );
00075 kdDebug(5150) << "ChiasmusLibrary: loading \"" << libfile
00076 << "\" failed: " << KLibLoader::self()->lastErrorMessage() << endl;
00077 return 0;
00078 }
00079 if ( !mXiaLibrary->hasSymbol( "Chiasmus" ) ) {
00080 if ( reason )
00081 *reason = i18n( "Failed to load %1: %2" )
00082 .arg( libfile, i18n( "Library does not contain the symbol \"Chiasmus\"." ) );
00083 kdDebug(5150) << "ChiasmusLibrary: loading \"" << libfile
00084 << "\" failed: " << "Library does not contain the symbol \"Chiasmus\"." << endl;
00085 return 0;
00086 }
00087 void * symbol = mXiaLibrary->symbol( "Chiasmus" );
00088 assert( symbol );
00089 return ( main_func )symbol;
00090 }
00091
00092 namespace {
00093 class ArgvProvider {
00094 char ** mArgv;
00095 int mArgc;
00096 public:
00097 ArgvProvider( const QValueVector<QCString> & args ) {
00098 mArgv = new char * [args.size()];
00099 for ( unsigned int i = 0 ; i < args.size() ; ++i )
00100 mArgv[i] = strdup( args[i].data() );
00101 }
00102 ~ArgvProvider() {
00103 std::for_each( mArgv, mArgv + mArgc, std::free );
00104 delete[] mArgv;
00105 }
00106 char ** argv() const { return mArgv; }
00107 };
00108 }
00109
00110 int Kleo::ChiasmusLibrary::perform( const QValueVector<QCString> & args ) const {
00111 if ( main_func func = chiasmus() )
00112 return func( args.size(), ArgvProvider( args ).argv() );
00113 else
00114 return -1;
00115 }
|