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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21#include "formatfactory.h"
22#include "vcardformat.h"
23
24#include <kdebug.h>
25#include <klocalizedstring.h>
26#include <kconfig.h>
27#include <kstandarddirs.h>
28#include <kconfiggroup.h>
29#include <klibrary.h>
30
31#include <QtCore/QCoreApplication>
32#include <QtCore/QFile>
33
34using namespace KABC;
35
36class FormatFactory::Private
37{
38 public:
39 ~Private() {
40 mFormatList.clear();
41 qRemovePostRoutine( cleanupFormatFactory );
42 }
43
44 KLibrary *openLibrary( const QString &libName );
45
46 QHash<QString, FormatInfo> mFormatList;
47
48 static FormatFactory *sSelf;
49 static void cleanupFormatFactory()
50 {
51 delete sSelf;
52 sSelf = 0;
53 }
54};
55FormatFactory *FormatFactory::Private::sSelf = 0;
56
57KLibrary *FormatFactory::Private::openLibrary( const QString &libName )
58{
59 KLibrary *library = new KLibrary( libName );
60 if ( library->load() ) {
61 return library;
62 }
63 kDebug() << library->errorString();
64 delete library;
65 return 0;
66}
67
68FormatFactory *FormatFactory::self()
69{
70 kDebug();
71
72 static Private p;
73 if ( !p.sSelf ) {
74 p.sSelf = new FormatFactory;
75 qAddPostRoutine( Private::cleanupFormatFactory );
76 }
77 return p.sSelf;
78}
79
80FormatFactory::FormatFactory()
81 : d( new Private )
82{
83 // dummy entry for default format
84 FormatInfo info;
85 info.library = QLatin1String( "<NoLibrary>" );
86 info.nameLabel = i18n( "vCard" );
87 info.descriptionLabel = i18n( "vCard Format" );
88 d->mFormatList.insert( QLatin1String( "vcard" ), info );
89
90 const QStringList list =
91 KGlobal::dirs()->findAllResources( "data", QLatin1String( "kabc/formats/*.desktop" ),
92 KStandardDirs::Recursive |
93 KStandardDirs::NoDuplicates );
94 for ( QStringList::ConstIterator it = list.begin(); it != list.end(); ++it ) {
95 KConfig config( *it, KConfig::SimpleConfig );
96
97 if ( !config.hasGroup( "Misc" ) || !config.hasGroup( "Plugin" ) ) {
98 continue;
99 }
100
101 KConfigGroup group = config.group( "Plugin" );
102 QString type = group.readEntry( "Type" );
103 info.library = group.readEntry( "X-KDE-Library" );
104
105 group = config.group( "Misc" );
106 info.nameLabel = group.readEntry( "Name" );
107 info.descriptionLabel = group.readEntry( "Comment", i18n( "No description available." ) );
108
109 d->mFormatList.insert( type, info );
110 }
111}
112
113FormatFactory::~FormatFactory()
114{
115 delete d;
116}
117
118QStringList FormatFactory::formats()
119{
120 QStringList retval;
121
122 // make sure 'vcard' is the first entry
123 retval << QLatin1String( "vcard" );
124
125 QHashIterator<QString, FormatInfo> it( d->mFormatList );
126 while ( it.hasNext() ) {
127 it.next();
128 if ( it.key() != QLatin1String( "vcard" ) ) {
129 retval << it.key();
130 }
131 }
132
133 return retval;
134}
135
136FormatInfo FormatFactory::info( const QString &type ) const
137{
138 if ( type.isEmpty() || !d->mFormatList.contains( type ) ) {
139 return FormatInfo();
140 } else {
141 return d->mFormatList[ type ];
142 }
143}
144
145Format *FormatFactory::format( const QString &type )
146{
147 Format *format = 0;
148
149 if ( type.isEmpty() ) {
150 return 0;
151 }
152
153 if ( type == QLatin1String( "vcard" ) ) {
154 format = new VCardFormat;
155 format->setType( type );
156 format->setNameLabel( i18n( "vCard" ) );
157 format->setDescriptionLabel( i18n( "vCard Format" ) );
158 return format;
159 }
160
161 if ( !d->mFormatList.contains( type ) ) {
162 return 0;
163 }
164
165 FormatInfo fi = d->mFormatList[ type ];
166 QString libName = fi.library;
167
168 KLibrary *library = d->openLibrary( libName );
169 if ( !library ) {
170 return 0;
171 }
172
173 KLibrary::void_function_ptr format_func = library->resolveFunction( "format" );
174
175 if ( format_func ) {
176 format = ( (Format *(*)())format_func )();
177 format->setType( type );
178 format->setNameLabel( fi.nameLabel );
179 format->setDescriptionLabel( fi.descriptionLabel );
180 } else {
181 kDebug() << "'" << libName << "' is not a format plugin.";
182 return 0;
183 }
184
185 return format;
186}
187