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

kabc

  • sources
  • kde-4.12
  • kdepimlibs
  • 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 
36 using namespace KABC;
37 
38 class 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 
53 static StdAddressBook *s_gStdAddressBook = 0;
54 bool StdAddressBook::Private::mAutomaticSave = true;
55 
56 static void deleteGlobalStdAddressBook()
57 {
58  if ( s_gStdAddressBook ) {
59  delete s_gStdAddressBook;
60  s_gStdAddressBook = 0;
61  }
62 }
63 
64 QString StdAddressBook::fileName()
65 {
66  return KStandardDirs::locateLocal( "data", QLatin1String( "kabc/std.vcf" ) );
67 }
68 
69 QString StdAddressBook::directoryName()
70 {
71  return KStandardDirs::locateLocal( "data", QLatin1String( "kabc/stdvcf" ) );
72 }
73 
74 StdAddressBook *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 
84 StdAddressBook *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 
109 StdAddressBook::StdAddressBook()
110  : AddressBook( QString() ), d( new Private( this ) )
111 {
112  kDebug();
113 
114  d->init( false );
115 }
116 
117 StdAddressBook::StdAddressBook( bool asynchronous )
118  : AddressBook( QString() ), d( new Private( this ) )
119 {
120  kDebug();
121 
122  d->init( asynchronous );
123 }
124 
125 StdAddressBook::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 
135 StdAddressBook::~StdAddressBook()
136 {
137  if ( Private::mAutomaticSave ) {
138  d->saveAll();
139  }
140 
141  delete d;
142 }
143 
144 void 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 
187 bool 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 
213 bool StdAddressBook::save()
214 {
215  kDebug();
216 
217  if ( s_gStdAddressBook ) {
218  return s_gStdAddressBook->d->saveAll();
219  } else {
220  return true;
221  }
222 }
223 
224 void StdAddressBook::close()
225 {
226  delete s_gStdAddressBook;
227  s_gStdAddressBook = 0;
228 }
229 
230 void StdAddressBook::setAutomaticSave( bool enable )
231 {
232  Private::mAutomaticSave = enable;
233 }
234 
235 bool StdAddressBook::automaticSave()
236 {
237  return Private::mAutomaticSave;
238 }
239 
240 Addressee StdAddressBook::whoAmI() const
241 {
242  KConfig _config( QLatin1String( "kabcrc" ) );
243  KConfigGroup config(&_config, "General" );
244 
245  return findByUid( config.readEntry( "WhoAmI" ) );
246 }
247 
248 void 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::StdAddressBook
Standard KDE address book.
Definition: stdaddressbook.h:59
KABC::StdAddressBook::automaticSave
static bool automaticSave()
Returns whether the address book is saved at destruction time.
Definition: stdaddressbook.cpp:235
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::whoAmI
Addressee whoAmI() const
Returns the contact, that is associated with the owner of the address book.
Definition: stdaddressbook.cpp:240
KABC::StdAddressBook::save
static KABC_DEPRECATED bool save()
Saves the standard address book to disk.
Definition: stdaddressbook.cpp:213
KABC::AddressBook::findByUid
Addressee findByUid(const QString &uid) const
Searches an addressee with the specified unique identifier.
Definition: addressbook.cpp:613
KABC::Ticket
Helper class for handling coordinated save of address books.
Definition: resource.h:37
KRES::Manager::createResource
T * createResource(const QString &type)
KRES::Manager::activeEnd
ActiveIterator activeEnd()
KABC::StdAddressBook::~StdAddressBook
~StdAddressBook()
Destructor.
Definition: stdaddressbook.cpp:135
KABC::StdAddressBook::directoryName
static QString directoryName()
Returns the default directory name for vcard-based addressbook.
Definition: stdaddressbook.cpp:69
KRES::Manager::ActiveIterator
KABC::StdAddressBook::setWhoAmI
void setWhoAmI(const Addressee &addr)
Sets the users contact.
Definition: stdaddressbook.cpp:248
KRES::Manager::writeConfig
void writeConfig(KConfig *cfg=0)
KABC::Addressee
address book entry
Definition: addressee.h:74
KRES::Resource::setResourceName
virtual void setResourceName(const QString &name)
KABC::Resource
Definition: resource.h:64
KABC::Addressee::uid
QString uid() const
Return unique identifier.
Definition: addressee.cpp:377
KABC::StdAddressBook::self
static StdAddressBook * self()
Returns the standard addressbook object.
Definition: stdaddressbook.cpp:74
KABC::StdAddressBook::close
static void close()
Closes the address book.
Definition: stdaddressbook.cpp:224
KRES::Manager::activeBegin
ActiveIterator activeBegin()
KRES::Manager
KABC::AddressBook
Address Book.
Definition: addressbook.h:46
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:01:05 by doxygen 1.8.7 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 API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

Search



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