-rw-r--r-- | kabc/stdaddressbook.cpp | 249 |
1 files changed, 249 insertions, 0 deletions
diff --git a/kabc/stdaddressbook.cpp b/kabc/stdaddressbook.cpp new file mode 100644 index 0000000..f9fc70b --- a/dev/null +++ b/kabc/stdaddressbook.cpp | |||
@@ -0,0 +1,249 @@ | |||
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., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /* | ||
22 | Enhanced Version of the file for platform independent KDE tools. | ||
23 | Copyright (c) 2004 Ulf Schenk | ||
24 | |||
25 | $Id$ | ||
26 | */ | ||
27 | #ifndef DESKTOP_VERSION | ||
28 | #include <qpe/global.h> | ||
29 | #else | ||
30 | |||
31 | #include <qdir.h> | ||
32 | #endif | ||
33 | #ifdef KAB_EMBEDDED | ||
34 | |||
35 | #include <stdlib.h> | ||
36 | #else //KAB_EMBEDDED | ||
37 | |||
38 | #include <stdlib.h> | ||
39 | |||
40 | //US#include <kresources/manager.h> | ||
41 | #include <kapplication.h> | ||
42 | #include <kcrash.h> | ||
43 | #include <ksimpleconfig.h> | ||
44 | |||
45 | |||
46 | #endif //KAB_EMBEDDED | ||
47 | |||
48 | #include "resource.h" | ||
49 | #include <kresources/manager.h> | ||
50 | #include <kdebug.h> | ||
51 | #include <klocale.h> | ||
52 | #include <kstaticdeleter.h> | ||
53 | #include <kstandarddirs.h> | ||
54 | |||
55 | #include "stdaddressbook.h" | ||
56 | |||
57 | using namespace KABC; | ||
58 | |||
59 | StdAddressBook *StdAddressBook::mSelf = 0; | ||
60 | bool StdAddressBook::mAutomaticSave = true; | ||
61 | |||
62 | static KStaticDeleter<StdAddressBook> addressBookDeleter; | ||
63 | |||
64 | QString StdAddressBook::fileName() | ||
65 | { | ||
66 | return locateLocal( "data", "kabc/std.vcf" ); | ||
67 | } | ||
68 | |||
69 | QString StdAddressBook::directoryName() | ||
70 | { | ||
71 | qDebug("StdAddressBook::directoryName() WILL FAIL OR NOT" ); | ||
72 | return locateLocal( "data", "kabc/stdvcf" ); | ||
73 | } | ||
74 | |||
75 | void StdAddressBook::handleCrash() | ||
76 | { | ||
77 | StdAddressBook::self()->cleanUp(); | ||
78 | } | ||
79 | |||
80 | StdAddressBook *StdAddressBook::self() | ||
81 | { | ||
82 | |||
83 | if ( !mSelf ) | ||
84 | { | ||
85 | QString appdir = StdAddressBook::setTempAppDir(); | ||
86 | |||
87 | kdDebug(5700) << "StdAddressBook::self()" << endl; | ||
88 | // US im am not sure why I have to use the other format here?? | ||
89 | #ifdef KAB_EMBEDDED | ||
90 | mSelf = addressBookDeleter.setObject( new StdAddressBook ); | ||
91 | #else //KAB_EMBEDDED | ||
92 | addressBookDeleter.setObject( mSelf, new StdAddressBook ); | ||
93 | #endif //KAB_EMBEDDED | ||
94 | KStandardDirs::setAppDir( appdir ); | ||
95 | } | ||
96 | |||
97 | return mSelf; | ||
98 | } | ||
99 | |||
100 | QString StdAddressBook::setTempAppDir() | ||
101 | { | ||
102 | QString appDIR = KStandardDirs::appDir(); | ||
103 | #ifdef DESKTOP_VERSION | ||
104 | QString appdir = QDir::homeDirPath(); | ||
105 | if ( appdir.right(1) == "\\" || appdir.right(1) == "/" ) | ||
106 | appdir += "kaddressbook/"; | ||
107 | else | ||
108 | appdir += "/kaddressbook/"; | ||
109 | KStandardDirs::setAppDir( QDir::convertSeparators( appdir )); | ||
110 | #else | ||
111 | KStandardDirs::setAppDir( Global::applicationFileName( "kaddressbook", "" ) ); | ||
112 | #endif | ||
113 | |||
114 | return appDIR; | ||
115 | } | ||
116 | StdAddressBook *StdAddressBook::self( bool onlyFastResources ) | ||
117 | { | ||
118 | |||
119 | if ( !mSelf ) | ||
120 | { | ||
121 | QString appdir =StdAddressBook::setTempAppDir(); | ||
122 | #ifdef KAB_EMBEDDED | ||
123 | mSelf = addressBookDeleter.setObject( new StdAddressBook( onlyFastResources ) ); | ||
124 | #else //KAB_EMBEDDED | ||
125 | addressBookDeleter.setObject( mSelf, new StdAddressBook( onlyFastResources ) ); | ||
126 | #endif //KAB_EMBEDDED | ||
127 | KStandardDirs::setAppDir( appdir ); | ||
128 | } | ||
129 | return mSelf; | ||
130 | } | ||
131 | |||
132 | StdAddressBook::StdAddressBook() | ||
133 | //US : AddressBook( "kabcrc" ) | ||
134 | : AddressBook( locateLocal( "config", "kabcrc") ) | ||
135 | { | ||
136 | |||
137 | init( false ); | ||
138 | } | ||
139 | |||
140 | StdAddressBook::StdAddressBook( bool onlyFastResources ) | ||
141 | //US : AddressBook( "kabcrc" ) | ||
142 | : AddressBook( locateLocal( "config", "kabcrc") ) | ||
143 | { | ||
144 | |||
145 | init( onlyFastResources ); | ||
146 | } | ||
147 | |||
148 | StdAddressBook::~StdAddressBook() | ||
149 | { | ||
150 | if ( mAutomaticSave ) | ||
151 | save(); | ||
152 | } | ||
153 | |||
154 | void StdAddressBook::init( bool ) | ||
155 | { | ||
156 | KRES::Manager<Resource> *manager = resourceManager(); | ||
157 | KRES::Manager<Resource>::ActiveIterator it; | ||
158 | |||
159 | for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) { | ||
160 | (*it)->setAddressBook( this ); | ||
161 | if ( !(*it)->open() ) | ||
162 | error( QString( "Unable to open resource '%1'!" ).arg( (*it)->resourceName() ) ); | ||
163 | } | ||
164 | |||
165 | Resource *res = standardResource(); | ||
166 | if ( !res ) { | ||
167 | res = manager->createResource( "file" ); | ||
168 | if ( res ) | ||
169 | { | ||
170 | addResource( res ); | ||
171 | } | ||
172 | else | ||
173 | kdDebug(5700) << "No resource available!!!" << endl; | ||
174 | } | ||
175 | |||
176 | setStandardResource( res ); | ||
177 | manager->writeConfig(); | ||
178 | |||
179 | load(); | ||
180 | } | ||
181 | |||
182 | bool StdAddressBook::save() | ||
183 | { | ||
184 | kdDebug(5700) << "StdAddressBook::save()" << endl; | ||
185 | |||
186 | bool ok = true; | ||
187 | AddressBook *ab = self(); | ||
188 | |||
189 | ab->deleteRemovedAddressees(); | ||
190 | |||
191 | KRES::Manager<Resource>::ActiveIterator it; | ||
192 | KRES::Manager<Resource> *manager = ab->resourceManager(); | ||
193 | for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) { | ||
194 | if ( !(*it)->readOnly() && (*it)->isOpen() ) { | ||
195 | Ticket *ticket = ab->requestSaveTicket( *it ); | ||
196 | // qDebug("StdAddressBook::save '%s'", (*it)->resourceName().latin1() ); | ||
197 | if ( !ticket ) { | ||
198 | ab->error( i18n( "Unable to save to resource '%1'. It is locked." ) | ||
199 | .arg( (*it)->resourceName() ) ); | ||
200 | return false; | ||
201 | } | ||
202 | |||
203 | if ( !ab->save( ticket ) ) | ||
204 | ok = false; | ||
205 | } | ||
206 | } | ||
207 | |||
208 | return ok; | ||
209 | } | ||
210 | |||
211 | void StdAddressBook::close() | ||
212 | { | ||
213 | //US destructObject is not defined on my system???. Is setObject(0) the same ??? | ||
214 | #ifndef KAB_EMBEDDED | ||
215 | addressBookDeleter.destructObject(); | ||
216 | #else //KAB_EMBEDDED | ||
217 | addressBookDeleter.setObject(0); | ||
218 | #endif //KAB_EMBEDDED | ||
219 | |||
220 | } | ||
221 | |||
222 | void StdAddressBook::setAutomaticSave( bool enable ) | ||
223 | { | ||
224 | mAutomaticSave = enable; | ||
225 | } | ||
226 | |||
227 | bool StdAddressBook::automaticSave() | ||
228 | { | ||
229 | return mAutomaticSave; | ||
230 | } | ||
231 | |||
232 | // should get const for 4.X | ||
233 | Addressee StdAddressBook::whoAmI() | ||
234 | { | ||
235 | //US KConfig config( "kabcrc" ); | ||
236 | KConfig config( locateLocal("config", "kabcrc") ); | ||
237 | config.setGroup( "General" ); | ||
238 | |||
239 | return findByUid( config.readEntry( "WhoAmI" ) ); | ||
240 | } | ||
241 | |||
242 | void StdAddressBook::setWhoAmI( const Addressee &addr ) | ||
243 | { | ||
244 | //US KConfig config( "kabcrc" ); | ||
245 | KConfig config( locateLocal("config", "kabcrc") ); | ||
246 | config.setGroup( "General" ); | ||
247 | |||
248 | config.writeEntry( "WhoAmI", addr.uid() ); | ||
249 | } | ||