• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.14.10 API Reference
  • KDE Home
  • Contact Us
 

kabc

  • kabc
formatfactory.cpp
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}
KABC::FormatFactory
Class for loading format plugins.
Definition formatfactory.h:95
KABC::FormatFactory::~FormatFactory
~FormatFactory()
Destructor.
Definition formatfactory.cpp:113
KABC::FormatFactory::info
FormatInfo info(const QString &type) const
Returns the info structure for a special type.
Definition formatfactory.cpp:136
KABC::FormatFactory::format
Format * format(const QString &type)
Returns a pointer to a format object or a null pointer if format type doesn't exist.
Definition formatfactory.cpp:145
KABC::FormatFactory::self
static FormatFactory * self()
Returns the global format factory.
Definition formatfactory.cpp:68
KABC::FormatFactory::formats
QStringList formats()
Returns a list of all available format types.
Definition formatfactory.cpp:118
KABC::Format
Base class for address book formats.
Definition format.h:43
KABC::Plugin::setType
virtual void setType(const QString &type)
Sets the plugin's type.
Definition plugin.cpp:43
KABC::Plugin::setDescriptionLabel
virtual void setDescriptionLabel(const QString &label)
Sets the plugin's description.
Definition plugin.cpp:63
KABC::Plugin::setNameLabel
virtual void setNameLabel(const QString &label)
Sets the plugin's name.
Definition plugin.cpp:53
KABC::VCardFormat
Interface of vCard backend for address book.
Definition vcardformat.h:38
KABC
Class that holds a Calendar Url (FBURL/CALADRURI/CALURI)
Definition address.h:29
KABC::FormatInfo
Dataype to hold information on format plugins.
Definition formatfactory.h:39
KABC::FormatInfo::library
QString library
Name of a KDE plugin library, e.g.
Definition formatfactory.h:55
KABC::FormatInfo::nameLabel
QString nameLabel
Localized string used as format's name, e.g.
Definition formatfactory.h:65
KABC::FormatInfo::descriptionLabel
QString descriptionLabel
Localized string to describe the format, e.g.
Definition formatfactory.h:75
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Wed Jan 24 2024 00:00:00 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kabc

Skip menu "kabc"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs-4.14.10 API Reference

Skip menu "kdepimlibs-4.14.10 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal