summaryrefslogtreecommitdiffabout
path: root/kabc/stdaddressbook.cpp
blob: ec47a4edae35b770366d87d79e4fd21958da738a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
    This file is part of libkabc.
    Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.
*/

/*
Enhanced Version of the file for platform independent KDE tools.
Copyright (c) 2004 Ulf Schenk

$Id$
*/

#include <qdir.h>
#include "resource.h"
#include <kresources/manager.h>
#include <kdebug.h>
#include <klocale.h>
#include <kstaticdeleter.h>
#include <kstandarddirs.h>

#include "stdaddressbook.h"

using namespace KABC;

StdAddressBook *StdAddressBook::mSelf = 0;
bool StdAddressBook::mAutomaticSave = true;

static KStaticDeleter<StdAddressBook> addressBookDeleter;

QString StdAddressBook::fileName()
{
  return locateLocal( "data", "kabc/std.vcf" );
}

QString StdAddressBook::directoryName()
{
  return locateLocal( "data", "kabc/stdvcf" );
}

void StdAddressBook::handleCrash()
{
  StdAddressBook::self()->cleanUp();
}

StdAddressBook *StdAddressBook::self()
{

  if ( !mSelf )
  {
      QString appdir = StdAddressBook::setTempAppDir();
// US im am not sure why I have to use the other format here??
#ifdef KAB_EMBEDDED
    mSelf = addressBookDeleter.setObject( new StdAddressBook );
#else //KAB_EMBEDDED
    addressBookDeleter.setObject( mSelf, new StdAddressBook );
#endif //KAB_EMBEDDED
    KStandardDirs::setAppDir( appdir );
  }

  return mSelf;
}

QString StdAddressBook::setTempAppDir()
{
    QString appDIR = KStandardDirs::appDir();
#ifdef DESKTOP_VERSION
    QString appdir = QDir::homeDirPath();
      if ( appdir.right(1) == "\\" ||   appdir.right(1) == "/" )
          appdir += "kaddressbook/";
      else
          appdir += "/kaddressbook/";
      KStandardDirs::setAppDir( QDir::convertSeparators( appdir ));
#else
      QString appdir = QDir::homeDirPath() + "/kdepim/apps/kaddressbook";

      KStandardDirs::setAppDir( appdir );
#endif

    return appDIR;
}
StdAddressBook *StdAddressBook::self( bool onlyFastResources )
{

   if ( !mSelf )
  {
      QString appdir =StdAddressBook::setTempAppDir();
#ifdef KAB_EMBEDDED
    mSelf = addressBookDeleter.setObject( new StdAddressBook( onlyFastResources ) );
#else //KAB_EMBEDDED
    addressBookDeleter.setObject( mSelf, new StdAddressBook( onlyFastResources ) );
#endif //KAB_EMBEDDED
    KStandardDirs::setAppDir( appdir );
  }
  return mSelf;
}

StdAddressBook::StdAddressBook()
  : AddressBook( "kabcrc" )
{

  init( false );
}

StdAddressBook::StdAddressBook( bool onlyFastResources )
  : AddressBook( "kabcrc" )
{

  init( onlyFastResources );
}

StdAddressBook::~StdAddressBook()
{
  if ( mAutomaticSave )
    save();
}

void StdAddressBook::init( bool )
{
  KRES::Manager<Resource> *manager = resourceManager();
  KRES::Manager<Resource>::ActiveIterator it;

  for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) {
    (*it)->setAddressBook( this );
    if ( !(*it)->open() )
      error( QString( "Unable to open resource '%1'!" ).arg( (*it)->resourceName() ) );
  }

  Resource *res = standardResource();
  if ( !res ) {
    res = manager->createResource( "file" );
    if ( res )
    {
      addResource( res );
    }
    else
        qDebug(" No resource available!!!");
  }

  setStandardResource( res );
  manager->writeConfig();

  load();
}

bool StdAddressBook::save()
{
  kdDebug(5700) << "StdAddressBook::save()" << endl;

  bool ok = true;
  AddressBook *ab = self();

  ab->deleteRemovedAddressees();

  KRES::Manager<Resource>::ActiveIterator it;
  KRES::Manager<Resource> *manager = ab->resourceManager();
  for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) {
    if ( !(*it)->readOnly() && (*it)->isOpen() ) {
      Ticket *ticket = ab->requestSaveTicket( *it );
//        qDebug("StdAddressBook::save '%s'", (*it)->resourceName().latin1() );
      if ( !ticket ) {
        ab->error( i18n( "Unable to save to resource '%1'. It is locked." )
                   .arg( (*it)->resourceName() ) );
        return false;
      }

      if ( !ab->save( ticket ) )
        ok = false;
    }
  }

  return ok;
}

void StdAddressBook::close()
{
//US destructObject is not defined on my system???. Is setObject(0) the same ???
//US  addressBookDeleter.destructObject();
  addressBookDeleter.setObject(0);

}

void StdAddressBook::setAutomaticSave( bool enable )
{
  mAutomaticSave = enable;
}

bool StdAddressBook::automaticSave()
{
  return mAutomaticSave;
}

// should get const for 4.X
Addressee StdAddressBook::whoAmI()
{
//US  KConfig config( "kabcrc" );
  KConfig config( locateLocal("config", "kabcrc") );
  config.setGroup( "General" );

  return findByUid( config.readEntry( "WhoAmI" ) );
}

void StdAddressBook::setWhoAmI( const Addressee &addr )
{
//US  KConfig config( "kabcrc" );
  KConfig config( locateLocal("config", "kabcrc") );
  config.setGroup( "General" );

  config.writeEntry( "WhoAmI", addr.uid() );
}