chiasmuslibrary.cpp
1 /*
2  chiasmuslibrary.cpp
3 
4  This file is part of libkleopatra, the KDE keymanagement library
5  Copyright (c) 2005 Klarälvdalens Datakonsult AB
6 
7  Libkleopatra is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of the
10  License, or (at your option) any later version.
11 
12  Libkleopatra is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21  In addition, as a special exception, the copyright holders give
22  permission to link the code of this program with any edition of
23  the TQt library by Trolltech AS, Norway (or with modified versions
24  of TQt that use the same license as TQt), and distribute linked
25  combinations including the two. You must obey the GNU General
26  Public License in all respects for all of the code used other than
27  TQt. If you modify this file, you may extend this exception to
28  your version of the file, but you are not obligated to do so. If
29  you do not wish to do so, delete this exception statement from
30  your version.
31 */
32 
33 
34 #include "chiasmuslibrary.h"
35 
36 #include "chiasmusbackend.h"
37 
38 #include "kleo/cryptoconfig.h"
39 
40 #include <klibloader.h>
41 #include <kdebug.h>
42 #include <tdelocale.h>
43 
44 #include <tqfile.h>
45 
46 #include <vector>
47 #include <algorithm>
48 
49 #include <cassert>
50 #include <cstdlib>
51 #include <cstring>
52 
53 Kleo::ChiasmusLibrary * Kleo::ChiasmusLibrary::self = 0;
54 
55 Kleo::ChiasmusLibrary::ChiasmusLibrary() : mXiaLibrary( 0 ) {
56  self = this;
57 }
58 
59 Kleo::ChiasmusLibrary::~ChiasmusLibrary() {
60  //delete mXiaLibrary; // hmm, how to get rid of it, then?
61 }
62 
63 Kleo::ChiasmusLibrary::main_func Kleo::ChiasmusLibrary::chiasmus( TQString * reason ) const {
64  assert( ChiasmusBackend::instance() );
65  assert( ChiasmusBackend::instance()->config() );
66  const CryptoConfigEntry * lib = ChiasmusBackend::instance()->config()->entry( "Chiasmus", "General", "lib" );
67  assert( lib );
68  const TQString libfile = lib->urlValue().path();
69  if ( !mXiaLibrary )
70  mXiaLibrary = KLibLoader::self()->library( TQFile::encodeName( libfile ) );
71  if ( !mXiaLibrary ) {
72  if ( reason )
73  *reason = i18n( "Failed to load %1: %2" )
74  .arg( libfile,KLibLoader::self()->lastErrorMessage() );
75  kdDebug(5150) << "ChiasmusLibrary: loading \"" << libfile
76  << "\" failed: " << KLibLoader::self()->lastErrorMessage() << endl;
77  return 0;
78  }
79  if ( !mXiaLibrary->hasSymbol( "Chiasmus" ) ) {
80  if ( reason )
81  *reason = i18n( "Failed to load %1: %2" )
82  .arg( libfile, i18n( "Library does not contain the symbol \"Chiasmus\"." ) );
83  kdDebug(5150) << "ChiasmusLibrary: loading \"" << libfile
84  << "\" failed: " << "Library does not contain the symbol \"Chiasmus\"." << endl;
85  return 0;
86  }
87  void * symbol = mXiaLibrary->symbol( "Chiasmus" );
88  assert( symbol );
89  return ( main_func )symbol;
90 }
91 
92 namespace {
93  class ArgvProvider {
94  char ** mArgv;
95  int mArgc;
96  public:
97  ArgvProvider( const TQValueVector<TQCString> & args ) {
98  mArgv = new char * [args.size()];
99  for ( unsigned int i = 0 ; i < args.size() ; ++i )
100  mArgv[i] = strdup( args[i].data() );
101  }
102  ~ArgvProvider() {
103  std::for_each( mArgv, mArgv + mArgc, std::free );
104  delete[] mArgv;
105  }
106  char ** argv() const { return mArgv; }
107  };
108 }
109 
110 int Kleo::ChiasmusLibrary::perform( const TQValueVector<TQCString> & args ) const {
111  if ( main_func func = chiasmus() )
112  return func( args.size(), ArgvProvider( args ).argv() );
113  else
114  return -1;
115 }
small helper class to load xia.o through xia.so and make the functionality available.