-rw-r--r-- | kabc/formatfactory.cpp | 181 |
1 files changed, 181 insertions, 0 deletions
diff --git a/kabc/formatfactory.cpp b/kabc/formatfactory.cpp new file mode 100644 index 0000000..2b073d7 --- a/dev/null +++ b/kabc/formatfactory.cpp | |||
@@ -0,0 +1,181 @@ | |||
1 | /* | ||
2 | This file is part of libkabc. | ||
3 | Copyright (c) 2002 Tobias Koenig <tokoe@kde.org> | ||
4 | |||
5 | This library is free software; you can redistribute it and/or | ||
6 | modify it under the terms of the GNU Library General Public | ||
7 | License as published by the Free Software Foundation; either | ||
8 | version 2 of the License, or (at your option) any later version. | ||
9 | |||
10 | This library is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Library General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Library General Public License | ||
16 | along with this library; see the file COPYING.LIB. If not, write to | ||
17 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | #include <kdebug.h> | ||
22 | #include <klocale.h> | ||
23 | #include <ksimpleconfig.h> | ||
24 | #include <kstandarddirs.h> | ||
25 | #include <kstaticdeleter.h> | ||
26 | |||
27 | #include <qfile.h> | ||
28 | #include <qstringlist.h> | ||
29 | |||
30 | #include "vcardformatplugin.h" | ||
31 | |||
32 | #include "formatfactory.h" | ||
33 | |||
34 | using namespace KABC; | ||
35 | |||
36 | FormatFactory *FormatFactory::mSelf = 0; | ||
37 | static KStaticDeleter<FormatFactory> factoryDeleter; | ||
38 | |||
39 | FormatFactory *FormatFactory::self() | ||
40 | { | ||
41 | kdDebug(5700) << "FormatFactory::self()" << endl; | ||
42 | |||
43 | if ( !mSelf ) { | ||
44 | #ifdef KAB_EMBEDDED | ||
45 | mSelf = factoryDeleter.setObject( new FormatFactory ); | ||
46 | #else //KAB_EMBEDDED | ||
47 | factoryDeleter.setObject( mSelf, new FormatFactory ); | ||
48 | #endif //KAB_EMBEDDED | ||
49 | |||
50 | } | ||
51 | return mSelf; | ||
52 | } | ||
53 | |||
54 | FormatFactory::FormatFactory() | ||
55 | { | ||
56 | mFormatList.setAutoDelete( true ); | ||
57 | |||
58 | // dummy entry for default format | ||
59 | FormatInfo *info = new FormatInfo; | ||
60 | info->library = "<NoLibrary>"; | ||
61 | info->nameLabel = i18n( "vCard" ); | ||
62 | info->descriptionLabel = i18n( "vCard Format" ); | ||
63 | mFormatList.insert( "vcard", info ); | ||
64 | |||
65 | QStringList list = KGlobal::dirs()->findAllResources( "data" ,"kabc/formats/*.desktop", true, true ); | ||
66 | for ( QStringList::ConstIterator it = list.begin(); it != list.end(); ++it ) | ||
67 | { | ||
68 | //US KSimpleConfig config( *it, true ); | ||
69 | KConfig config( *it ); | ||
70 | |||
71 | if ( !config.hasGroup( "Misc" ) || !config.hasGroup( "Plugin" ) ) | ||
72 | continue; | ||
73 | |||
74 | info = new FormatInfo; | ||
75 | |||
76 | config.setGroup( "Plugin" ); | ||
77 | QString type = config.readEntry( "Type" ); | ||
78 | info->library = config.readEntry( "X-KDE-Library" ); | ||
79 | |||
80 | config.setGroup( "Misc" ); | ||
81 | info->nameLabel = config.readEntry( "Name" ); | ||
82 | info->descriptionLabel = config.readEntry( "Comment", i18n( "No description available." ) ); | ||
83 | |||
84 | mFormatList.insert( type, info ); | ||
85 | } | ||
86 | } | ||
87 | |||
88 | FormatFactory::~FormatFactory() | ||
89 | { | ||
90 | mFormatList.clear(); | ||
91 | } | ||
92 | |||
93 | QStringList FormatFactory::formats() | ||
94 | { | ||
95 | QStringList retval; | ||
96 | |||
97 | // make sure 'vcard' is the first entry | ||
98 | retval << "vcard"; | ||
99 | |||
100 | QDictIterator<FormatInfo> it( mFormatList ); | ||
101 | for ( ; it.current(); ++it ) | ||
102 | if ( it.currentKey() != "vcard" ) | ||
103 | retval << it.currentKey(); | ||
104 | |||
105 | return retval; | ||
106 | } | ||
107 | |||
108 | FormatInfo *FormatFactory::info( const QString &type ) | ||
109 | { | ||
110 | if ( type.isEmpty() ) | ||
111 | return 0; | ||
112 | else | ||
113 | return mFormatList[ type ]; | ||
114 | } | ||
115 | |||
116 | FormatPlugin *FormatFactory::format( const QString& type ) | ||
117 | { | ||
118 | FormatPlugin *format = 0; | ||
119 | |||
120 | if ( type.isEmpty() ) | ||
121 | return 0; | ||
122 | |||
123 | if ( type == "vcard" ) { | ||
124 | format = new VCardFormatPlugin; | ||
125 | /* // LR | ||
126 | format->setType( type ); | ||
127 | format->setNameLabel( i18n( "vCard" ) ); | ||
128 | format->setDescriptionLabel( i18n( "vCard Format" ) ); | ||
129 | */ | ||
130 | return format; | ||
131 | } | ||
132 | |||
133 | FormatInfo *fi = mFormatList[ type ]; | ||
134 | if (!fi) | ||
135 | return 0; | ||
136 | QString libName = fi->library; | ||
137 | #ifndef DESKTOP_VERSION | ||
138 | KLibrary *library = openLibrary( libName ); | ||
139 | if ( !library ) | ||
140 | return 0; | ||
141 | |||
142 | void *format_func = library->symbol( "format" ); | ||
143 | |||
144 | if ( format_func ) { | ||
145 | format = ((FormatPlugin* (*)())format_func)(); | ||
146 | // LR | ||
147 | /* | ||
148 | format->setType( type ); | ||
149 | format->setNameLabel( fi->nameLabel ); | ||
150 | format->setDescriptionLabel( fi->descriptionLabel ); | ||
151 | */ | ||
152 | } else { | ||
153 | kdDebug( 5700 ) << "'" << libName << "' is not a format plugin." << endl; | ||
154 | return 0; | ||
155 | } | ||
156 | #endif | ||
157 | return format; | ||
158 | } | ||
159 | |||
160 | #ifndef DESKTOP_VERSION | ||
161 | KLibrary *FormatFactory::openLibrary( const QString& libName ) | ||
162 | { | ||
163 | KLibrary *library = 0; | ||
164 | |||
165 | QString path = KLibLoader::findLibrary( QFile::encodeName( libName ) ); | ||
166 | |||
167 | if ( path.isEmpty() ) { | ||
168 | kdDebug( 5700 ) << "No format plugin library was found!" << endl; | ||
169 | return 0; | ||
170 | } | ||
171 | |||
172 | library = KLibLoader::self()->library( QFile::encodeName( path ) ); | ||
173 | |||
174 | if ( !library ) { | ||
175 | kdDebug( 5700 ) << "Could not load library '" << libName << "'" << endl; | ||
176 | return 0; | ||
177 | } | ||
178 | |||
179 | return library; | ||
180 | } | ||
181 | #endif | ||