source: vendor/trolltech/current/src/tools/qgpluginmanager.cpp

Last change on this file was 2, checked in by dmik, 20 years ago

Imported xplatform parts of the official release 3.3.1 from Trolltech

  • Property svn:keywords set to Id
File size: 18.1 KB
Line 
1/****************************************************************************
2** $Id: qgpluginmanager.cpp 2 2005-11-16 15:49:26Z dmik $
3**
4** Implementation of QGPluginManager class
5**
6** Copyright (C) 2000-2003 Trolltech AS. All rights reserved.
7**
8** This file is part of the tools module of the Qt GUI Toolkit.
9**
10** This file may be distributed under the terms of the Q Public License
11** as defined by Trolltech AS of Norway and appearing in the file
12** LICENSE.QPL included in the packaging of this file.
13**
14** This file may be distributed and/or modified under the terms of the
15** GNU General Public License version 2 as published by the Free Software
16** Foundation and appearing in the file LICENSE.GPL included in the
17** packaging of this file.
18**
19** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
20** licenses may use this file in accordance with the Qt Commercial License
21** Agreement provided with the Software.
22**
23** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
24** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
25**
26** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
27** information about Qt Commercial License Agreements.
28** See http://www.trolltech.com/qpl/ for QPL licensing information.
29** See http://www.trolltech.com/gpl/ for GPL licensing information.
30**
31** Contact info@trolltech.com if any conditions of this licensing are
32** not clear to you.
33**
34**********************************************************************/
35
36#include "qgpluginmanager_p.h"
37#ifndef QT_NO_COMPONENT
38#include "qcomlibrary_p.h"
39#include "qmap.h"
40#include "qdir.h"
41
42/*
43 The following co-occurrence code is borrowed from Qt Linguist.
44
45 How similar are two texts? The approach used here relies on
46 co-occurrence matrices and is very efficient.
47
48 Let's see with an example: how similar are "here" and "hither"? The
49 co-occurrence matrix M for "here" is M[h,e] = 1, M[e,r] = 1,
50 M[r,e] = 1 and 0 elsewhere; the matrix N for "hither" is N[h,i] = 1,
51 N[i,t] = 1, ..., N[h,e] = 1, N[e,r] = 1 and 0 elsewhere. The union
52 U of both matrices is the matrix U[i,j] = max { M[i,j], N[i,j] },
53 and the intersection V is V[i,j] = min { M[i,j], N[i,j] }. The score
54 for a pair of texts is
55
56 score = (sum of V[i,j] over all i, j) / (sum of U[i,j] over all i, j),
57
58 a formula suggested by Arnt Gulbrandsen. Here we have
59
60 score = 2 / 6,
61
62 or one third.
63
64 The implementation differs from this in a few details. Most
65 importantly, repetitions are ignored; for input "xxx", M[x,x] equals
66 1, not 2.
67*/
68
69/*
70 Every character is assigned to one of 20 buckets so that the
71 co-occurrence matrix requires only 20 * 20 = 400 bits, not
72 256 * 256 = 65536 bits or even more if we want the whole Unicode.
73 Which character falls in which bucket is arbitrary.
74
75 The second half of the table is a replica of the first half, because of
76 laziness.
77*/
78static const char indexOf[256] = {
79 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
80 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
81// ! " # $ % & ' ( ) * + , - . /
82 0, 2, 6, 7, 10, 12, 15, 19, 2, 6, 7, 10, 12, 15, 19, 0,
83// 0 1 2 3 4 5 6 7 8 9 : ; < = > ?
84 1, 3, 4, 5, 8, 9, 11, 13, 14, 16, 2, 6, 7, 10, 12, 15,
85// @ A B C D E F G H I J K L M N O
86 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 6, 10, 11, 12, 13, 14,
87// P Q R S T U V W X Y Z [ \ ] ^ _
88 15, 12, 16, 17, 18, 19, 2, 10, 15, 7, 19, 2, 6, 7, 10, 0,
89// ` a b c d e f g h i j k l m n o
90 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 6, 10, 11, 12, 13, 14,
91// p q r s t u v w x y z { | } ~
92 15, 12, 16, 17, 18, 19, 2, 10, 15, 7, 19, 2, 6, 7, 10, 0,
93
94 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
95 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
96 0, 2, 6, 7, 10, 12, 15, 19, 2, 6, 7, 10, 12, 15, 19, 0,
97 1, 3, 4, 5, 8, 9, 11, 13, 14, 16, 2, 6, 7, 10, 12, 15,
98 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 6, 10, 11, 12, 13, 14,
99 15, 12, 16, 17, 18, 19, 2, 10, 15, 7, 19, 2, 6, 7, 10, 0,
100 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 6, 10, 11, 12, 13, 14,
101 15, 12, 16, 17, 18, 19, 2, 10, 15, 7, 19, 2, 6, 7, 10, 0
102};
103
104/*
105 The entry bitCount[i] (for i between 0 and 255) is the number of
106 bits used to represent i in binary.
107*/
108static const char bitCount[256] = {
109 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
110 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
111 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
112 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
113 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
114 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
115 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
116 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
117 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
118 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
119 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
120 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
121 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
122 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
123 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
124 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
125};
126
127class QCoMatrix
128{
129public:
130 /*
131 The matrix has 20 * 20 = 400 entries. This requires 50 bytes, or
132 13 words. Some operations are performed on words for more
133 efficiency.
134 */
135 union {
136 Q_UINT8 b[52];
137 Q_UINT32 w[13];
138 };
139
140 QCoMatrix() { memset( b, 0, 52 ); }
141 QCoMatrix( const char *text ) {
142 char c = '\0', d;
143 memset( b, 0, 52 );
144 while ( (d = *text) != '\0' ) {
145 setCoocc( c, d );
146 if ( (c = *++text) != '\0' ) {
147 setCoocc( d, c );
148 text++;
149 }
150 }
151 }
152
153 void setCoocc( char c, char d ) {
154 int k = indexOf[(uchar) c] + 20 * indexOf[(uchar) d];
155 b[k >> 3] |= k & 0x7;
156 }
157
158 int worth() const {
159 int result = 0;
160 for ( int i = 0; i < 50; i++ )
161 result += bitCount[b[i]];
162 return result;
163 }
164
165 static QCoMatrix reunion( const QCoMatrix& m, const QCoMatrix& n )
166 {
167 QCoMatrix p;
168 for ( int i = 0; i < 13; i++ )
169 p.w[i] = m.w[i] | n.w[i];
170 return p;
171 }
172 static QCoMatrix intersection( const QCoMatrix& m, const QCoMatrix& n )
173 {
174 QCoMatrix p;
175 for ( int i = 0; i < 13; i++ )
176 p.w[i] = m.w[i] & n.w[i];
177 return p;
178 }
179};
180
181/*
182 Returns an integer between 0 (dissimilar) and 15 (very similar)
183 depending on how similar the string is to \a target.
184
185 This function is efficient, but its results might change in future
186 versions of Qt as the algorithm evolves.
187
188 \code
189 QString s( "color" );
190 a = similarity( s, "color" ); // a == 15
191 a = similarity( s, "colour" ); // a == 8
192 a = similarity( s, "flavor" ); // a == 4
193 a = similarity( s, "dahlia" ); // a == 0
194 \endcode
195*/
196static int similarity( const QString& s1, const QString& s2 )
197{
198 QCoMatrix m1( s1 );
199 QCoMatrix m2( s2 );
200 return ( 15 * (QCoMatrix::intersection(m1, m2).worth() + 1) ) /
201 ( QCoMatrix::reunion(m1, m2).worth() + 1 );
202}
203
204/*!
205 \class QPluginManager qpluginmanager.h
206 \reentrant
207 \brief The QPluginManager class provides basic functions to access a certain kind of functionality in libraries.
208 \ingroup componentmodel
209
210 \internal
211
212 A common usage of components is to extend the existing functionality in an application using plugins. The application
213 defines interfaces that abstract a certain group of functionality, and a plugin provides a specialized implementation
214 of one or more of those interfaces.
215
216 The QPluginManager template has to be instantiated with an interface definition and the IID for this interface.
217
218 \code
219 QPluginManager<MyPluginInterface> *manager = new QPluginManager<MyPluginInterface>( IID_MyPluginInterface );
220 \endcode
221
222 It searches a specified directory for all shared libraries, queries for components that implement the specific interface and
223 reads information about the features the plugin wants to add to the application. The component can provide the set of features
224 provided by implementing either the QFeatureListInterface or the QComponentInformationInterface. The strings returned by the implementations
225 of
226
227 \code
228 QStringList QFeatureListInterface::featureList() const
229 \endcode
230
231 or
232
233 \code
234 QString QComponentInformationInterface::name() const
235 \endcode
236
237 respectively, can then be used to access the component that provides the requested feature:
238
239 \code
240 MyPluginInterface *iface;
241 manager->queryInterface( "feature", &iface );
242 if ( iface )
243 iface->execute( "feature" );
244 \endcode
245
246 The application can use a QPluginManager instance to create parts of the user interface based on the list of features
247 found in plugins:
248
249 \code
250 QPluginManager<MyPluginInterface> *manager = new QPluginManager<MyPluginInterface>( IID_ImageFilterInterface );
251 manager->addLibraryPath(...);
252
253 QStringList features = manager->featureList();
254 for ( QStringList::Iterator it = features.begin(); it != features.end(); ++it ) {
255 MyPluginInterface *iface;
256 manager->queryInterface( *it, &iface );
257
258 // use QAction to provide toolbuttons and menuitems for each feature...
259 }
260 \endcode
261*/
262
263/*!
264 \fn QPluginManager::QPluginManager( const QUuid& id, const QStringList& paths = QString::null, const QString &suffix = QString::null, bool cs = TRUE )
265
266 Creates an QPluginManager for interfaces \a id that will load all shared library files in the \a paths + \a suffix.
267 If \a cs is FALSE the manager will handle feature strings case insensitive.
268
269 \warning
270 Setting the cs flag to FALSE requires that components also convert to lower case when comparing with passed strings, so this has
271 to be handled with care and documented very well.
272
273 \sa QApplication::libraryPaths()
274*/
275
276
277/*!
278 \fn QRESULT QPluginManager::queryInterface(const QString& feature, Type** iface) const
279
280 Sets \a iface to point to the interface providing \a feature.
281
282 \sa featureList(), library()
283*/
284
285
286
287#include <qptrlist.h>
288
289QGPluginManager::QGPluginManager( const QUuid& id, const QStringList& paths, const QString &suffix, bool cs )
290 : interfaceId( id ), plugDict( 17, cs ), casesens( cs ), autounload( TRUE )
291{
292 // Every QLibrary object is destroyed on destruction of the manager
293 libDict.setAutoDelete( TRUE );
294 for ( QStringList::ConstIterator it = paths.begin(); it != paths.end(); ++it ) {
295 QString path = *it;
296 addLibraryPath( path + suffix );
297 }
298}
299
300QGPluginManager::~QGPluginManager()
301{
302 if ( !autounload ) {
303 QDictIterator<QLibrary> it( libDict );
304 while ( it.current() ) {
305 QLibrary *lib = it.current();
306 ++it;
307 lib->setAutoUnload( FALSE );
308 }
309 }
310}
311
312void QGPluginManager::addLibraryPath( const QString& path )
313{
314 if ( !enabled() || !QDir( path ).exists( ".", TRUE ) )
315 return;
316
317#if defined(Q_OS_WIN32)
318 QString filter = "*.dll";
319#elif defined(Q_OS_MACX)
320 QString filter = "*.dylib; *.so; *.bundle";
321#elif defined(Q_OS_HPUX)
322 QString filter = "*.sl";
323#elif defined(Q_OS_UNIX)
324 QString filter = "*.so";
325#endif
326
327 QStringList plugins = QDir(path).entryList( filter );
328 for ( QStringList::Iterator p = plugins.begin(); p != plugins.end(); ++p ) {
329 QString lib = QDir::cleanDirPath( path + "/" + *p );
330 if ( libList.contains( lib ) )
331 continue;
332 libList.append( lib );
333 }
334}
335
336const QLibrary* QGPluginManager::library( const QString& feature ) const
337{
338 if ( !enabled() || feature.isEmpty() )
339 return 0;
340
341 // We already have a QLibrary object for this feature
342 QLibrary *library = 0;
343 if ( ( library = plugDict[feature] ) )
344 return library;
345
346 // Find the filename that matches the feature request best
347 QMap<int, QStringList> map;
348 QStringList::ConstIterator it = libList.begin();
349 int best = 0;
350 int worst = 15;
351 while ( it != libList.end() ) {
352 if ( (*it).isEmpty() || libDict[*it] ) {
353 ++it;
354 continue;
355 }
356 QString basename = QFileInfo(*it).baseName();
357 int s = similarity( feature, basename );
358 if ( s < worst )
359 worst = s;
360 if ( s > best )
361 best = s;
362 map[s].append( basename + QChar(0xfffd) + *it );
363 ++it;
364 }
365
366 if ( map.isEmpty() )
367 return 0; // no libraries to add
368
369 // Start with the best match to get the library object
370 QGPluginManager *that = (QGPluginManager*)this;
371 for ( int s = best; s >= worst; --s ) {
372 QStringList group = map[s];
373 group.sort(); // sort according to the base name
374 QStringList::ConstIterator git = group.begin();
375 while ( git != group.end() ) {
376 QString lib = (*git).mid( (*git).find( QChar(0xfffd) ) + 1 );
377 QString basename = (*git).left( (*git).find( QChar(0xfffd) ) );
378 ++git;
379
380 QStringList sameBasename;
381 while( git != group.end() &&
382 basename == (*git).left( (*git).find( QChar(0xfffd) ) ) ) {
383 sameBasename << (*git).mid( (*git).find( QChar(0xfffd) ) + 1 );
384 ++git;
385 }
386
387 if ( sameBasename.isEmpty() ) {
388 that->addLibrary( new QComLibrary( lib ) );
389 } else {
390 QPtrList<QComLibrary> same;
391 same.setAutoDelete( TRUE );
392 for ( QStringList::ConstIterator bit = sameBasename.begin();
393 bit != sameBasename.end(); ++bit )
394 same.append( new QComLibrary( *bit ) );
395 QComLibrary* bestMatch = 0;
396 for ( QComLibrary* candidate = same.first(); candidate; candidate = same.next() )
397 if ( candidate->qtVersion() && candidate->qtVersion() <= QT_VERSION
398 && ( !bestMatch || candidate->qtVersion() > bestMatch->qtVersion() ) )
399 bestMatch = candidate;
400 if ( bestMatch ) {
401 same.find( bestMatch );
402 that->addLibrary( same.take() );
403 }
404 }
405
406 if ( ( library = that->plugDict[feature] ) )
407 return library;
408 }
409 }
410 return 0;
411}
412
413QStringList QGPluginManager::featureList() const
414{
415 QStringList features;
416
417 if ( !enabled() )
418 return features;
419
420 QGPluginManager *that = (QGPluginManager*)this;
421 QStringList theLibs = libList;
422 QStringList phase2Libs;
423 QStringList phase2Deny;
424
425 /* In order to get the feature list we need to add all interesting
426 libraries. If there are libraries with the same base name, we
427 prioritze the one that fits our Qt version number and ignore the
428 others */
429 QStringList::Iterator it;
430 for ( it = theLibs.begin(); it != theLibs.end(); ++it ) {
431 if ( (*it).isEmpty() || libDict[*it] )
432 continue;
433 QComLibrary* library = new QComLibrary( *it );
434 if ( library->qtVersion() == QT_VERSION ) {
435 that->addLibrary( library );
436 phase2Deny << QFileInfo( *it ).baseName();
437 } else {
438 delete library;
439 phase2Libs << *it;
440 }
441 }
442 for ( it = phase2Libs.begin(); it != phase2Libs.end(); ++it )
443 if ( !phase2Deny.contains( QFileInfo( *it ).baseName() ) )
444 that->addLibrary( new QComLibrary( *it ) );
445
446 for ( QDictIterator<QLibrary> pit( plugDict ); pit.current(); ++pit )
447 features << pit.currentKey();
448
449 return features;
450}
451
452bool QGPluginManager::addLibrary( QLibrary* lib )
453{
454 if ( !enabled() || !lib )
455 return FALSE;
456
457 QComLibrary* plugin = (QComLibrary*)lib;
458 bool useful = FALSE;
459
460 QUnknownInterface* iFace = 0;
461 plugin->queryInterface( interfaceId, &iFace );
462 if ( iFace ) {
463 QFeatureListInterface *fliFace = 0;
464 QComponentInformationInterface *cpiFace = 0;
465 iFace->queryInterface( IID_QFeatureList, (QUnknownInterface**)&fliFace );
466 if ( !fliFace )
467 plugin->queryInterface( IID_QFeatureList, (QUnknownInterface**)&fliFace );
468 if ( !fliFace ) {
469 iFace->queryInterface( IID_QComponentInformation, (QUnknownInterface**)&cpiFace );
470 if ( !cpiFace )
471 plugin->queryInterface( IID_QComponentInformation, (QUnknownInterface**)&cpiFace );
472 }
473 QStringList fl;
474 if ( fliFace )
475 // Map all found features to the library
476 fl = fliFace->featureList();
477 else if ( cpiFace )
478 fl << cpiFace->name();
479
480 for ( QStringList::Iterator f = fl.begin(); f != fl.end(); ++f ) {
481 QLibrary *old = plugDict[*f];
482 if ( !old ) {
483 useful = TRUE;
484 plugDict.replace( *f, plugin );
485 } else {
486 // we have old *and* plugin, which one to pick?
487 QComLibrary* first = (QComLibrary*)old;
488 QComLibrary* second = (QComLibrary*)plugin;
489 bool takeFirst = TRUE;
490 if ( first->qtVersion() != QT_VERSION ) {
491 if ( second->qtVersion() == QT_VERSION )
492 takeFirst = FALSE;
493 else if ( second->qtVersion() < QT_VERSION &&
494 first->qtVersion() > QT_VERSION )
495 takeFirst = FALSE;
496 }
497 if ( !takeFirst ) {
498 useful = TRUE;
499 plugDict.replace( *f, plugin );
500 qWarning("%s: Discarding feature %s in %s!",
501 (const char*) QFile::encodeName( plugin->library()),
502 (*f).latin1(),
503 (const char*) QFile::encodeName( old->library() ) );
504 } else {
505 qWarning("%s: Feature %s already defined in %s!",
506 (const char*) QFile::encodeName( old->library() ),
507 (*f).latin1(),
508 (const char*) QFile::encodeName( plugin->library() ) );
509 }
510 }
511 }
512 if ( fliFace )
513 fliFace->release();
514 if ( cpiFace )
515 cpiFace->release();
516 iFace->release();
517 }
518
519 if ( useful ) {
520 libDict.replace( plugin->library(), plugin );
521 if ( !libList.contains( plugin->library() ) )
522 libList.append( plugin->library() );
523 return TRUE;
524 }
525 delete plugin;
526 return FALSE;
527}
528
529
530bool QGPluginManager::enabled() const
531{
532#ifdef QT_SHARED
533 return TRUE;
534#else
535 return FALSE;
536#endif
537}
538
539QRESULT QGPluginManager::queryUnknownInterface(const QString& feature, QUnknownInterface** iface) const
540{
541 QComLibrary* plugin = 0;
542 plugin = (QComLibrary*)library( feature );
543 return plugin ? plugin->queryInterface( interfaceId, (QUnknownInterface**)iface ) : QE_NOINTERFACE;
544}
545
546#endif //QT_NO_COMPONENT
Note: See TracBrowser for help on using the repository browser.