libtdepim

pluginloaderbase.cpp
1 /*
2  This file is part of libtdepim.
3 
4  Copyright (c) 2002,2004 Marc Mutz <mutz@kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
21 
22 #include <pluginloaderbase.h>
23 
24 #include <ksimpleconfig.h>
25 #include <tdelocale.h>
26 #include <kstandarddirs.h>
27 #include <klibloader.h>
28 #include <tdeglobal.h>
29 #include <kdebug.h>
30 
31 #include <tqfile.h>
32 #include <tqstringlist.h>
33 
34 static kdbgstream warning() {
35  return kdWarning( 5300 ) << "PluginLoaderBase: ";
36 }
37 #ifndef NDEBUG
38 static kdbgstream debug( bool cond )
39 #else
40 static kndbgstream debug( bool cond )
41 #endif
42 {
43  return kdDebug( cond, 5300 ) << "PluginLoaderBase: ";
44 }
45 
46 namespace KPIM {
47 
48  PluginLoaderBase::PluginLoaderBase() : d(0) {}
49  PluginLoaderBase::~PluginLoaderBase() {}
50 
51 
52  TQStringList PluginLoaderBase::types() const {
53  TQStringList result;
54  for ( TQMap< TQString, PluginMetaData >::const_iterator it = mPluginMap.begin();
55  it != mPluginMap.end() ; ++it )
56  result.push_back( it.key() );
57  return result;
58  }
59 
60  const PluginMetaData * PluginLoaderBase::infoForName( const TQString & type ) const {
61  return mPluginMap.contains( type ) ? &(mPluginMap[type]) : 0 ;
62  }
63 
64 
65  void PluginLoaderBase::doScan( const char * path ) {
66  mPluginMap.clear();
67 
68  const TQStringList list =
69  TDEGlobal::dirs()->findAllResources( "data", path, true, true );
70  for ( TQStringList::const_iterator it = list.begin() ;
71  it != list.end() ; ++it ) {
72  KSimpleConfig config( *it, true );
73  if ( config.hasGroup( "Misc" ) && config.hasGroup( "Plugin" ) ) {
74  config.setGroup( "Plugin" );
75 
76  const TQString type = config.readEntry( "Type" ).lower();
77  if ( type.isEmpty() ) {
78  warning() << "missing or empty [Plugin]Type value in \""
79  << *it << "\" - skipping" << endl;
80  continue;
81  }
82 
83  const TQString library = config.readEntry( "X-TDE-Library" );
84  if ( library.isEmpty() ) {
85  warning() << "missing or empty [Plugin]X-TDE-Library value in \""
86  << *it << "\" - skipping" << endl;
87  continue;
88  }
89 
90  config.setGroup( "Misc" );
91 
92  TQString name = config.readEntry( "Name" );
93  if ( name.isEmpty() ) {
94  warning() << "missing or empty [Misc]Name value in \""
95  << *it << "\" - inserting default name" << endl;
96  name = i18n("Unnamed plugin");
97  }
98 
99  TQString comment = config.readEntry( "Comment" );
100  if ( comment.isEmpty() ) {
101  warning() << "missing or empty [Misc]Comment value in \""
102  << *it << "\" - inserting default name" << endl;
103  comment = i18n("No description available");
104  }
105 
106  mPluginMap.insert( type, PluginMetaData( library, name, comment ) );
107  } else {
108  warning() << "Desktop file \"" << *it
109  << "\" doesn't seem to describe a plugin "
110  << "(misses Misc and/or Plugin group)" << endl;
111  }
112  }
113  }
114 
115  void * PluginLoaderBase::mainFunc( const TQString & type,
116  const char * mf_name ) const {
117  if ( type.isEmpty() || !mPluginMap.contains( type ) )
118  return 0;
119 
120  const TQString libName = mPluginMap[ type ].library;
121  if ( libName.isEmpty() )
122  return 0;
123 
124  const KLibrary * lib = openLibrary( libName );
125  if ( !lib )
126  return 0;
127 
128  mPluginMap[ type ].loaded = true;
129 
130  const TQString factory_name = libName + '_' + mf_name;
131  if ( !lib->hasSymbol( factory_name.latin1() ) ) {
132  warning() << "No symbol named \"" << factory_name.latin1() << "\" ("
133  << factory_name << ") was found in library \"" << libName
134  << "\"" << endl;
135  return 0;
136  }
137 
138  return lib->symbol( factory_name.latin1() );
139  }
140 
141  const KLibrary * PluginLoaderBase::openLibrary( const TQString & libName ) const {
142 
143  const TQString path = KLibLoader::findLibrary( TQFile::encodeName( libName ) );
144 
145  if ( path.isEmpty() ) {
146  warning() << "No plugin library named \"" << libName
147  << "\" was found!" << endl;
148  return 0;
149  }
150 
151  const KLibrary * library = KLibLoader::self()->library( TQFile::encodeName( path ) );
152 
153  debug( !library ) << "Could not load library '" << libName << "'" << endl;
154 
155  return library;
156  }
157 
158 
159 } // namespace KMime
TDEPIM classes for drag and drop of mails.