chiasmuslibrary.cpp
00001 /* 00002 chiasmuslibrary.cpp 00003 00004 This file is part of libkleopatra, the KDE keymanagement library 00005 Copyright (c) 2005 Klarälvdalens Datakonsult AB 00006 00007 Libkleopatra is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU General Public License as 00009 published by the Free Software Foundation; either version 2 of the 00010 License, or (at your option) any later version. 00011 00012 Libkleopatra is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with this program; if not, write to the Free Software 00019 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 00021 In addition, as a special exception, the copyright holders give 00022 permission to link the code of this program with any edition of 00023 the TQt library by Trolltech AS, Norway (or with modified versions 00024 of TQt that use the same license as TQt), and distribute linked 00025 combinations including the two. You must obey the GNU General 00026 Public License in all respects for all of the code used other than 00027 TQt. If you modify this file, you may extend this exception to 00028 your version of the file, but you are not obligated to do so. If 00029 you do not wish to do so, delete this exception statement from 00030 your version. 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 <tqfile.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 //delete mXiaLibrary; // hmm, how to get rid of it, then? 00061 } 00062 00063 Kleo::ChiasmusLibrary::main_func Kleo::ChiasmusLibrary::chiasmus( TQString * 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 TQString libfile = lib->urlValue().path(); 00069 if ( !mXiaLibrary ) 00070 mXiaLibrary = KLibLoader::self()->library( TQFile::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 TQValueVector<TQCString> & 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 TQValueVector<TQCString> & args ) const { 00111 if ( main_func func = chiasmus() ) 00112 return func( args.size(), ArgvProvider( args ).argv() ); 00113 else 00114 return -1; 00115 }