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

kabc

  • kabc
stdaddressbook.cpp
1/*
2 This file is part of libkabc.
3 Copyright (c) 2001 Cornelius Schumacher <schumacher@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 "stdaddressbook.h"
22#include "resource.h"
23
24#include "kresources/manager.h"
25
26#include <kdebug.h>
27#include <klocalizedstring.h>
28#include <kconfig.h>
29#include <kstandarddirs.h>
30#include <kconfiggroup.h>
31
32 #include <QCoreApplication>
33
34#include <stdlib.h>
35
36using namespace KABC;
37
38class StdAddressBook::Private
39{
40 public:
41 Private( StdAddressBook *parent )
42 : mParent( parent )
43 {
44 }
45
46 void init( bool asynchronous );
47 bool saveAll();
48
49 StdAddressBook *mParent;
50 static bool mAutomaticSave;
51};
52
53static StdAddressBook *s_gStdAddressBook = 0;
54bool StdAddressBook::Private::mAutomaticSave = true;
55
56static void deleteGlobalStdAddressBook()
57{
58 if ( s_gStdAddressBook ) {
59 delete s_gStdAddressBook;
60 s_gStdAddressBook = 0;
61 }
62}
63
64QString StdAddressBook::fileName()
65{
66 return KStandardDirs::locateLocal( "data", QLatin1String( "kabc/std.vcf" ) );
67}
68
69QString StdAddressBook::directoryName()
70{
71 return KStandardDirs::locateLocal( "data", QLatin1String( "kabc/stdvcf" ) );
72}
73
74StdAddressBook *StdAddressBook::self()
75{
76 kDebug();
77
78 // delegate to other self() method since the only difference
79 // was the constructor being used and their only difference is
80 // what they pass to Private::init()
81 return self( false );
82}
83
84StdAddressBook *StdAddressBook::self( bool asynchronous )
85{
86 kDebug() << "asynchronous=" << asynchronous;
87
88 if ( !s_gStdAddressBook ) {
89 s_gStdAddressBook = new StdAddressBook( asynchronous, false );
90
91 kDebug() << "calling init after instance creation";
92 s_gStdAddressBook->d->init( asynchronous );
93
94 // We don't use a global static here for this reason:
95 //
96 // There are problems with the destruction order: The destructor of
97 // StdAddressBook calls save(), which for LDAP address books, needs KIO
98 // (more specific: KProtocolInfo) to be still alive. However, with a global
99 // static, KProtocolInfo is already deleted, and the app will crash.
100 //
101 // qAddPostRoutine deletes the objects when the QApplication is destroyed,
102 // which is earlier than the global statics, so this will work.
103 qAddPostRoutine( deleteGlobalStdAddressBook );
104 }
105
106 return s_gStdAddressBook;
107}
108
109StdAddressBook::StdAddressBook()
110 : AddressBook( QString() ), d( new Private( this ) )
111{
112 kDebug();
113
114 d->init( false );
115}
116
117StdAddressBook::StdAddressBook( bool asynchronous )
118 : AddressBook( QString() ), d( new Private( this ) )
119{
120 kDebug();
121
122 d->init( asynchronous );
123}
124
125StdAddressBook::StdAddressBook( bool asynchronous, bool doInit )
126 : AddressBook( QString() ), d( new Private( this ) )
127{
128 kDebug();
129
130 if ( doInit ) {
131 d->init( asynchronous );
132 }
133}
134
135StdAddressBook::~StdAddressBook()
136{
137 if ( Private::mAutomaticSave ) {
138 d->saveAll();
139 }
140
141 delete d;
142}
143
144void StdAddressBook::Private::init( bool asynchronous )
145{
146 KRES::Manager<Resource> *manager = mParent->resourceManager();
147
148 KRES::Manager<Resource>::ActiveIterator it;
149 for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) {
150 ( *it )->setAddressBook( mParent );
151 if ( !( *it )->open() ) {
152 mParent->error( i18n( "Unable to open resource '%1'.", ( *it )->resourceName() ) );
153 continue;
154 }
155 mParent->connect( *it, SIGNAL(loadingFinished(Resource*)),
156 mParent, SLOT(resourceLoadingFinished(Resource*)) );
157 mParent->connect( *it, SIGNAL(savingFinished(Resource*)),
158 mParent, SLOT(resourceSavingFinished(Resource*)) );
159
160 mParent->connect( *it, SIGNAL(loadingError(Resource*,QString)),
161 mParent, SLOT(resourceLoadingError(Resource*,QString)) );
162 mParent->connect( *it, SIGNAL(savingError(Resource*,QString)),
163 mParent, SLOT(resourceSavingError(Resource*,QString)) );
164 }
165
166 Resource *res = mParent->standardResource();
167 if ( !res ) {
168 res = manager->createResource( QLatin1String( "file" ) );
169 if ( res ) {
170 res->setResourceName( i18n( "Default Address Book" ) );
171 mParent->addResource( res );
172 } else {
173 kDebug() << "No resource available!!!";
174 }
175 }
176
177 mParent->setStandardResource( res );
178 manager->writeConfig();
179
180 if ( asynchronous ) {
181 mParent->asyncLoad();
182 } else {
183 mParent->load();
184 }
185}
186
187bool StdAddressBook::Private::saveAll()
188{
189 kDebug();
190 bool ok = true;
191
192 KRES::Manager<Resource>::ActiveIterator it;
193 KRES::Manager<Resource> *manager = mParent->resourceManager();
194 for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) {
195 if ( !( *it )->readOnly() && ( *it )->isOpen() ) {
196 Ticket *ticket = mParent->requestSaveTicket( *it );
197 if ( !ticket ) {
198 mParent->error( i18n( "Unable to save to resource '%1'. It is locked.",
199 ( *it )->resourceName() ) );
200 return false;
201 }
202
203 if ( !mParent->AddressBook::save( ticket ) ) {
204 ok = false;
205 mParent->releaseSaveTicket( ticket );
206 }
207 }
208 }
209
210 return ok;
211}
212
213bool StdAddressBook::save()
214{
215 kDebug();
216
217 if ( s_gStdAddressBook ) {
218 return s_gStdAddressBook->d->saveAll();
219 } else {
220 return true;
221 }
222}
223
224void StdAddressBook::close()
225{
226 delete s_gStdAddressBook;
227 s_gStdAddressBook = 0;
228}
229
230void StdAddressBook::setAutomaticSave( bool enable )
231{
232 Private::mAutomaticSave = enable;
233}
234
235bool StdAddressBook::automaticSave()
236{
237 return Private::mAutomaticSave;
238}
239
240Addressee StdAddressBook::whoAmI() const
241{
242 KConfig _config( QLatin1String( "kabcrc" ) );
243 KConfigGroup config(&_config, "General" );
244
245 return findByUid( config.readEntry( "WhoAmI" ) );
246}
247
248void StdAddressBook::setWhoAmI( const Addressee &addr )
249{
250 KConfig _config( QLatin1String( "kabcrc" ) );
251 KConfigGroup config(&_config, "General" );
252
253 config.writeEntry( "WhoAmI", addr.uid() );
254}
KABC::AddressBook
Address Book.
Definition addressbook.h:47
KABC::AddressBook::load
bool load()
Loads all addressees synchronously.
Definition addressbook.cpp:347
KABC::AddressBook::savingFinished
void savingFinished(Resource *resource)
Emitted when the asynchronous saving of one resource has finished after calling asyncSave().
KABC::AddressBook::addResource
bool addResource(Resource *resource)
Adds a resource to the address book.
Definition addressbook.cpp:831
KABC::AddressBook::setStandardResource
void setStandardResource(Resource *resource)
Sets the resource manager's standard resource.
Definition addressbook.cpp:915
KABC::AddressBook::asyncLoad
bool asyncLoad()
Loads all addressees asynchronously.
Definition addressbook.cpp:365
KABC::AddressBook::resourceLoadingFinished
void resourceLoadingFinished(Resource *resource)
Handles loading success.
Definition addressbook.cpp:935
KABC::AddressBook::error
void error(const QString &msg)
Shows GUI independent error messages.
Definition addressbook.cpp:901
KABC::AddressBook::resourceSavingFinished
void resourceSavingFinished(Resource *resource)
Handles saving success.
Definition addressbook.cpp:945
KABC::AddressBook::standardResource
Resource * standardResource()
Returns the addressbook resource manager's standard resource.
Definition addressbook.cpp:920
KABC::AddressBook::loadingFinished
void loadingFinished(Resource *resource)
Emitted when the asynchronous loading of one resource has finished after calling asyncLoad().
KABC::AddressBook::resourceLoadingError
void resourceLoadingError(Resource *resource, const QString &errMsg)
Handles loading errors.
Definition addressbook.cpp:952
KABC::AddressBook::resourceSavingError
void resourceSavingError(Resource *resource, const QString &errMsg)
Handles saving errors.
Definition addressbook.cpp:963
KABC::AddressBook::resourceManager
KRES::Manager< Resource > * resourceManager()
Returns the addressbook's resource manager.
Definition addressbook.cpp:925
KABC::Addressee
address book entry
Definition addressee.h:79
KABC::Addressee::uid
QString uid() const
Return unique identifier.
Definition addressee.cpp:442
KABC::Resource
Definition resource.h:65
KABC::StdAddressBook
Standard KDE address book.
Definition stdaddressbook.h:60
KABC::StdAddressBook::~StdAddressBook
~StdAddressBook()
Destructor.
Definition stdaddressbook.cpp:135
KABC::StdAddressBook::automaticSave
static bool automaticSave()
Returns whether the address book is saved at destruction time.
Definition stdaddressbook.cpp:235
KABC::StdAddressBook::whoAmI
Addressee whoAmI() const
Returns the contact, that is associated with the owner of the address book.
Definition stdaddressbook.cpp:240
KABC::StdAddressBook::setAutomaticSave
static void setAutomaticSave(bool state)
Sets the automatic save property of the address book.
Definition stdaddressbook.cpp:230
KABC::StdAddressBook::fileName
static QString fileName()
Returns the default file name for vcard-based addressbook.
Definition stdaddressbook.cpp:64
KABC::StdAddressBook::setWhoAmI
void setWhoAmI(const Addressee &addr)
Sets the users contact.
Definition stdaddressbook.cpp:248
KABC::StdAddressBook::save
static KABC_DEPRECATED bool save()
Saves the standard address book to disk.
Definition stdaddressbook.cpp:213
KABC::StdAddressBook::close
static void close()
Closes the address book.
Definition stdaddressbook.cpp:224
KABC::StdAddressBook::directoryName
static QString directoryName()
Returns the default directory name for vcard-based addressbook.
Definition stdaddressbook.cpp:69
KABC::StdAddressBook::self
static StdAddressBook * self()
Returns the standard addressbook object.
Definition stdaddressbook.cpp:74
KABC::Ticket
Helper class for handling coordinated save of address books.
Definition resource.h:38
KRES::Manager::ActiveIterator
KRES::Manager
KRES::Manager::writeConfig
void writeConfig(KConfig *cfg=0)
KRES::Manager::activeBegin
ActiveIterator activeBegin()
KRES::Manager::activeEnd
ActiveIterator activeEnd()
KRES::Manager::createResource
T * createResource(const QString &type)
KRES::Resource::setResourceName
virtual void setResourceName(const QString &name)
KABC
Class that holds a Calendar Url (FBURL/CALADRURI/CALURI)
Definition address.h:29
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