Diffstat (limited to 'kabc/plugins/file/resourcefile.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | kabc/plugins/file/resourcefile.cpp | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/kabc/plugins/file/resourcefile.cpp b/kabc/plugins/file/resourcefile.cpp index c89939d..dad4571 100644 --- a/kabc/plugins/file/resourcefile.cpp +++ b/kabc/plugins/file/resourcefile.cpp | |||
@@ -1,277 +1,284 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of libkabc. | 2 | This file is part of libkabc. |
3 | Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> | 3 | Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> |
4 | 4 | ||
5 | This library is free software; you can redistribute it and/or | 5 | This library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Library General Public | 6 | modify it under the terms of the GNU Library General Public |
7 | License as published by the Free Software Foundation; either | 7 | License as published by the Free Software Foundation; either |
8 | version 2 of the License, or (at your option) any later version. | 8 | version 2 of the License, or (at your option) any later version. |
9 | 9 | ||
10 | This library is distributed in the hope that it will be useful, | 10 | This library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Library General Public License for more details. | 13 | Library General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU Library General Public License | 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 | 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, | 17 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
18 | Boston, MA 02111-1307, USA. | 18 | Boston, MA 02111-1307, USA. |
19 | */ | 19 | */ |
20 | 20 | ||
21 | /* | 21 | /* |
22 | Enhanced Version of the file for platform independent KDE tools. | 22 | Enhanced Version of the file for platform independent KDE tools. |
23 | Copyright (c) 2004 Ulf Schenk | 23 | Copyright (c) 2004 Ulf Schenk |
24 | 24 | ||
25 | $Id$ | 25 | $Id$ |
26 | */ | 26 | */ |
27 | 27 | ||
28 | #include <sys/types.h> | 28 | #include <sys/types.h> |
29 | #include <sys/stat.h> | 29 | #include <sys/stat.h> |
30 | #ifndef _WIN32_ | 30 | #ifndef _WIN32_ |
31 | #include <unistd.h> | 31 | #include <unistd.h> |
32 | #endif | 32 | #endif |
33 | 33 | ||
34 | #include <qfile.h> | 34 | #include <qfile.h> |
35 | #include <qfileinfo.h> | 35 | #include <qfileinfo.h> |
36 | #include <qregexp.h> | 36 | #include <qregexp.h> |
37 | #include <qtimer.h> | 37 | #include <qtimer.h> |
38 | 38 | ||
39 | #include <kapplication.h> | 39 | #include <kapplication.h> |
40 | #include <kconfig.h> | 40 | #include <kconfig.h> |
41 | #include <kdebug.h> | 41 | #include <kdebug.h> |
42 | #include <klocale.h> | 42 | #include <klocale.h> |
43 | //US #include <ksavefile.h> | 43 | //US #include <ksavefile.h> |
44 | #include <kstandarddirs.h> | 44 | #include <kstandarddirs.h> |
45 | #include <kmessagebox.h> | 45 | #include <kmessagebox.h> |
46 | #include <kglobalsettings.h> | 46 | #include <kglobalsettings.h> |
47 | 47 | ||
48 | #include "formatfactory.h" | 48 | #include "formatfactory.h" |
49 | 49 | ||
50 | #include "resource.h" | 50 | #include "resource.h" |
51 | #include "resourcefileconfig.h" | 51 | #include "resourcefileconfig.h" |
52 | #include "stdaddressbook.h" | 52 | #include "stdaddressbook.h" |
53 | #define NO_DIRWATCH | 53 | #define NO_DIRWATCH |
54 | #include "resourcefile.h" | 54 | #include "resourcefile.h" |
55 | 55 | ||
56 | //#define ALLOW_LOCKING | 56 | //#define ALLOW_LOCKING |
57 | 57 | ||
58 | 58 | ||
59 | using namespace KABC; | 59 | using namespace KABC; |
60 | 60 | ||
61 | extern "C" | 61 | extern "C" |
62 | #ifdef _WIN32_ | 62 | #ifdef _WIN32_ |
63 | __declspec(dllexport) | 63 | __declspec(dllexport) |
64 | #else | 64 | #else |
65 | { | 65 | { |
66 | #endif | 66 | #endif |
67 | 67 | ||
68 | //US void *init_kabc_file() | 68 | //US void *init_kabc_file() |
69 | void *init_microkabc_file() | 69 | void *init_microkabc_file() |
70 | { | 70 | { |
71 | return new KRES::PluginFactory<ResourceFile,ResourceFileConfig>(); | 71 | return new KRES::PluginFactory<ResourceFile,ResourceFileConfig>(); |
72 | } | 72 | } |
73 | #ifndef _WIN32_ | 73 | #ifndef _WIN32_ |
74 | } | 74 | } |
75 | #endif | 75 | #endif |
76 | 76 | ||
77 | ResourceFile::ResourceFile( const KConfig *config ) | 77 | ResourceFile::ResourceFile( const KConfig *config ) |
78 | : Resource( config ) , mFormat( 0 ) | 78 | : Resource( config ) , mFormat( 0 ) |
79 | { | 79 | { |
80 | QString fileName, formatName, default_fileName; | 80 | QString fileName, formatName, default_fileName; |
81 | 81 | ||
82 | default_fileName = StdAddressBook::fileName(); | 82 | default_fileName = StdAddressBook::fileName(); |
83 | mLastBackupDate = -1; | 83 | mLastBackupDate = -1; |
84 | KConfig *cfg = (KConfig *)config; | 84 | KConfig *cfg = (KConfig *)config; |
85 | if ( cfg ) { | 85 | if ( cfg ) { |
86 | #ifdef _WIN32_ | ||
87 | // we use plugins on win32. the group is stored in a static variable | ||
88 | // such that group info not available on win32 plugins | ||
89 | // to fix that, it would be a looooot of work | ||
90 | if ( !cfg->tempGroup().isEmpty() ) | ||
91 | cfg->setGroup( cfg->tempGroup() ); | ||
92 | #endif | ||
86 | fileName = cfg->readEntry( "FileName", default_fileName ); | 93 | fileName = cfg->readEntry( "FileName", default_fileName ); |
87 | formatName = cfg->readEntry( "FileFormat", "vcard" ); | 94 | formatName = cfg->readEntry( "FileFormat", "vcard" ); |
88 | mFamily = cfg->readEntry( "ResourceName", "std" ); | 95 | mFamily = cfg->readEntry( "ResourceName", "std" ); |
89 | mLastBackupDate = cfg->readNumEntry( "LastBackupDate", 0 ); | 96 | mLastBackupDate = cfg->readNumEntry( "LastBackupDate", 0 ); |
90 | } else { | 97 | } else { |
91 | fileName = default_fileName; | 98 | fileName = default_fileName; |
92 | formatName = "vcard"; | 99 | formatName = "vcard"; |
93 | } | 100 | } |
94 | 101 | ||
95 | init( fileName, formatName ); | 102 | init( fileName, formatName ); |
96 | } | 103 | } |
97 | 104 | ||
98 | ResourceFile::ResourceFile( const QString &fileName , | 105 | ResourceFile::ResourceFile( const QString &fileName , |
99 | const QString &formatName ) | 106 | const QString &formatName ) |
100 | : Resource( 0 ) | 107 | : Resource( 0 ) |
101 | { | 108 | { |
102 | // qDebug("ResourceFile::ResourceFile : 3 %s, %s", fileName.latin1(), formatName.latin1()); | 109 | // qDebug("ResourceFile::ResourceFile : 3 %s, %s", fileName.latin1(), formatName.latin1()); |
103 | 110 | ||
104 | 111 | ||
105 | mLastBackupDate = -1; | 112 | mLastBackupDate = -1; |
106 | init( fileName, formatName ); | 113 | init( fileName, formatName ); |
107 | } | 114 | } |
108 | 115 | ||
109 | void ResourceFile::init( const QString &fileName, const QString &formatName ) | 116 | void ResourceFile::init( const QString &fileName, const QString &formatName ) |
110 | { | 117 | { |
111 | mFormatName = formatName; | 118 | mFormatName = formatName; |
112 | 119 | ||
113 | FormatFactory *factory = FormatFactory::self(); | 120 | FormatFactory *factory = FormatFactory::self(); |
114 | mFormat = factory->format( mFormatName ); | 121 | mFormat = factory->format( mFormatName ); |
115 | 122 | ||
116 | if ( !mFormat ) { | 123 | if ( !mFormat ) { |
117 | mFormatName = "vcard"; | 124 | mFormatName = "vcard"; |
118 | mFormat = factory->format( mFormatName ); | 125 | mFormat = factory->format( mFormatName ); |
119 | } | 126 | } |
120 | 127 | ||
121 | #ifndef NO_DIRWATCH | 128 | #ifndef NO_DIRWATCH |
122 | connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); | 129 | connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); |
123 | connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); | 130 | connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); |
124 | connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); | 131 | connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); |
125 | #endif | 132 | #endif |
126 | 133 | ||
127 | QString localKdeDir; | 134 | QString localKdeDir; |
128 | localKdeDir = readEnvPath("LOCALMICROKDEHOME"); | 135 | localKdeDir = readEnvPath("LOCALMICROKDEHOME"); |
129 | if ( ! localKdeDir.isEmpty() ) { | 136 | if ( ! localKdeDir.isEmpty() ) { |
130 | qDebug("LOCALMICROKDEHOME is set to: %s",localKdeDir.latin1() ); | 137 | qDebug("LOCALMICROKDEHOME is set to: %s",localKdeDir.latin1() ); |
131 | QFileInfo fi ( fileName ); | 138 | QFileInfo fi ( fileName ); |
132 | QString localname = localKdeDir + "/apps/kabc/" + fi.fileName (); | 139 | QString localname = localKdeDir + "/apps/kabc/" + fi.fileName (); |
133 | QFileInfo fi2 ( localname ); | 140 | QFileInfo fi2 ( localname ); |
134 | if ( ! fi2.exists() || mFamily == "sync_res" ) { | 141 | if ( ! fi2.exists() || mFamily == "sync_res" ) { |
135 | if ( fi.exists() && mFamily == "sync_res") { | 142 | if ( fi.exists() && mFamily == "sync_res") { |
136 | qDebug("LOCAL mode SYNC mode using absolute file path "); | 143 | qDebug("LOCAL mode SYNC mode using absolute file path "); |
137 | setFileName( fileName ); | 144 | setFileName( fileName ); |
138 | return; | 145 | return; |
139 | } else { | 146 | } else { |
140 | KMessageBox::error(0,i18n("Addressbook resource file not found:\n '%1'.\nIn LOCAL mode only resource files in\n'%2'\nare supported.\n(i.e. in the dir ./apps/kabc/ relative to the kapi(.exe) binary.)\n\nIf you are starting KA/Pi for the very first time\nyou will get this error message as well.\nIt will create the missing file automatically for you.").arg(localname).arg(localKdeDir+"/apps/kabc/") ); | 147 | KMessageBox::error(0,i18n("Addressbook resource file not found:\n '%1'.\nIn LOCAL mode only resource files in\n'%2'\nare supported.\n(i.e. in the dir ./apps/kabc/ relative to the kapi(.exe) binary.)\n\nIf you are starting KA/Pi for the very first time\nyou will get this error message as well.\nIt will create the missing file automatically for you.").arg(localname).arg(localKdeDir+"/apps/kabc/") ); |
141 | setFileName( localname ); | 148 | setFileName( localname ); |
142 | return; | 149 | return; |
143 | } | 150 | } |
144 | 151 | ||
145 | } else { | 152 | } else { |
146 | qDebug("Local resource file found. Changing filename to: %s",localname.latin1() ); | 153 | qDebug("Local resource file found. Changing filename to: %s",localname.latin1() ); |
147 | setFileName( localname ); | 154 | setFileName( localname ); |
148 | return; | 155 | return; |
149 | } | 156 | } |
150 | 157 | ||
151 | } | 158 | } |
152 | setFileName( fileName ); | 159 | setFileName( fileName ); |
153 | } | 160 | } |
154 | 161 | ||
155 | ResourceFile::~ResourceFile() | 162 | ResourceFile::~ResourceFile() |
156 | { | 163 | { |
157 | delete mFormat; | 164 | delete mFormat; |
158 | mFormat = 0; | 165 | mFormat = 0; |
159 | } | 166 | } |
160 | 167 | ||
161 | void ResourceFile::writeConfig( KConfig *config ) | 168 | void ResourceFile::writeConfig( KConfig *config ) |
162 | { | 169 | { |
163 | 170 | ||
164 | config->setGroup( "Resource_" + identifier() ); | 171 | config->setGroup( "Resource_" + identifier() ); |
165 | Resource::writeConfig( config ); | 172 | Resource::writeConfig( config ); |
166 | 173 | ||
167 | config->writeEntry( "FileName", fileName() ); | 174 | config->writeEntry( "FileName", fileName() ); |
168 | config->writeEntry( "FileFormat", mFormatName ); | 175 | config->writeEntry( "FileFormat", mFormatName ); |
169 | 176 | ||
170 | // qDebug("ResourceFile::writeConfig format %s, %s", mFileName.latin1(), mFormatName.latin1()); | 177 | // qDebug("ResourceFile::writeConfig format %s, %s", mFileName.latin1(), mFormatName.latin1()); |
171 | 178 | ||
172 | } | 179 | } |
173 | 180 | ||
174 | Ticket *ResourceFile::requestSaveTicket() | 181 | Ticket *ResourceFile::requestSaveTicket() |
175 | { | 182 | { |
176 | kdDebug(5700) << "ResourceFile::requestSaveTicket()" << endl; | 183 | kdDebug(5700) << "ResourceFile::requestSaveTicket()" << endl; |
177 | 184 | ||
178 | if ( !addressBook() ) return 0; | 185 | if ( !addressBook() ) return 0; |
179 | 186 | ||
180 | #ifdef ALLOW_LOCKING | 187 | #ifdef ALLOW_LOCKING |
181 | if ( !lock( mFileName ) ) { | 188 | if ( !lock( mFileName ) ) { |
182 | qDebug("unablt to lock file "); | 189 | qDebug("unablt to lock file "); |
183 | return 0; | 190 | return 0; |
184 | } | 191 | } |
185 | #endif | 192 | #endif |
186 | return createTicket( this ); | 193 | return createTicket( this ); |
187 | } | 194 | } |
188 | 195 | ||
189 | 196 | ||
190 | bool ResourceFile::doOpen() | 197 | bool ResourceFile::doOpen() |
191 | { | 198 | { |
192 | QFile file( fileName() ); | 199 | QFile file( fileName() ); |
193 | qDebug("ResourceFile::openfile %s ", fileName().latin1()); | 200 | qDebug("ResourceFile::openfile %s ", fileName().latin1()); |
194 | 201 | ||
195 | if ( !file.exists() ) { | 202 | if ( !file.exists() ) { |
196 | // try to create the file | 203 | // try to create the file |
197 | bool ok = file.open( IO_WriteOnly ); | 204 | bool ok = file.open( IO_WriteOnly ); |
198 | if ( ok ) | 205 | if ( ok ) |
199 | file.close(); | 206 | file.close(); |
200 | 207 | ||
201 | return ok; | 208 | return ok; |
202 | } else { | 209 | } else { |
203 | if ( !file.open( IO_ReadWrite ) ) | 210 | if ( !file.open( IO_ReadWrite ) ) |
204 | return false; | 211 | return false; |
205 | 212 | ||
206 | if ( file.size() < 10 ) { | 213 | if ( file.size() < 10 ) { |
207 | file.close(); | 214 | file.close(); |
208 | return true; | 215 | return true; |
209 | } | 216 | } |
210 | 217 | ||
211 | bool ok = mFormat->checkFormat( &file ); | 218 | bool ok = mFormat->checkFormat( &file ); |
212 | file.close(); | 219 | file.close(); |
213 | 220 | ||
214 | return ok; | 221 | return ok; |
215 | } | 222 | } |
216 | } | 223 | } |
217 | 224 | ||
218 | void ResourceFile::doClose() | 225 | void ResourceFile::doClose() |
219 | { | 226 | { |
220 | } | 227 | } |
221 | 228 | ||
222 | bool ResourceFile::load() | 229 | bool ResourceFile::load() |
223 | { | 230 | { |
224 | 231 | ||
225 | QFile file( fileName() ); | 232 | QFile file( fileName() ); |
226 | if ( !file.open( IO_ReadOnly ) ) { | 233 | if ( !file.open( IO_ReadOnly ) ) { |
227 | addressBook()->error( i18n( "Unable to open file '%1'." ).arg( fileName() ) ); | 234 | addressBook()->error( i18n( "Unable to open file '%1'." ).arg( fileName() ) ); |
228 | return false; | 235 | return false; |
229 | } | 236 | } |
230 | 237 | ||
231 | // qDebug("ResourceFile::load format %s, %s", mFileName.latin1(), mFormatName.latin1()); | 238 | // qDebug("ResourceFile::load format %s, %s", mFileName.latin1(), mFormatName.latin1()); |
232 | 239 | ||
233 | return mFormat->loadAll( addressBook(), this, &file ); | 240 | return mFormat->loadAll( addressBook(), this, &file ); |
234 | } | 241 | } |
235 | 242 | ||
236 | bool ResourceFile::save( Ticket *ticket ) | 243 | bool ResourceFile::save( Ticket *ticket ) |
237 | { | 244 | { |
238 | // qDebug("ResourceFile::save format %s, %s", mFileName.latin1(), mFormatName.latin1()); | 245 | // qDebug("ResourceFile::save format %s, %s", mFileName.latin1(), mFormatName.latin1()); |
239 | 246 | ||
240 | 247 | ||
241 | // create backup file | 248 | // create backup file |
242 | QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() ); | 249 | QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() ); |
243 | 250 | ||
244 | /*US we use a simpler method to create a backupfile | 251 | /*US we use a simpler method to create a backupfile |
245 | 252 | ||
246 | (void) KSaveFile::backupFile( mFileName, QString::null | 253 | (void) KSaveFile::backupFile( mFileName, QString::null |
247 | ,extension ); | 254 | ,extension ); |
248 | 255 | ||
249 | KSaveFile saveFile( mFileName ); | 256 | KSaveFile saveFile( mFileName ); |
250 | bool ok = false; | 257 | bool ok = false; |
251 | if ( saveFile.status() == 0 && saveFile.file() ) | 258 | if ( saveFile.status() == 0 && saveFile.file() ) |
252 | { | 259 | { |
253 | mFormat->saveAll( addressBook(), this, saveFile.file() ); | 260 | mFormat->saveAll( addressBook(), this, saveFile.file() ); |
254 | ok = saveFile.close(); | 261 | ok = saveFile.close(); |
255 | } | 262 | } |
256 | */ | 263 | */ |
257 | 264 | ||
258 | //US ToDo: write backupfile | 265 | //US ToDo: write backupfile |
259 | #ifndef NO_DIRWATCH | 266 | #ifndef NO_DIRWATCH |
260 | mDirWatch.stopScan(); | 267 | mDirWatch.stopScan(); |
261 | #endif | 268 | #endif |
262 | if ( mLastBackupDate >= 0 && mFamily != "sync_res") { | 269 | if ( mLastBackupDate >= 0 && mFamily != "sync_res") { |
263 | KConfig conf (locateLocal("config","microkdeglobalrc")); | 270 | KConfig conf (locateLocal("config","microkdeglobalrc")); |
264 | conf.setGroup( "BackupSettings" ); | 271 | conf.setGroup( "BackupSettings" ); |
265 | bool b_enabled = conf.readBoolEntry( "BackupEnabled" ); | 272 | bool b_enabled = conf.readBoolEntry( "BackupEnabled" ); |
266 | if ( b_enabled ) { | 273 | if ( b_enabled ) { |
267 | int num = conf.readNumEntry( "BackupNumbers" ); | 274 | int num = conf.readNumEntry( "BackupNumbers" ); |
268 | int d_count = conf.readNumEntry( "BackupDayCount" ); | 275 | int d_count = conf.readNumEntry( "BackupDayCount" ); |
269 | bool stdDir = conf.readBoolEntry( "BackupUseDefaultDir" ); | 276 | bool stdDir = conf.readBoolEntry( "BackupUseDefaultDir" ); |
270 | QString bupDir = conf.readEntry( "BackupDatadir" ); | 277 | QString bupDir = conf.readEntry( "BackupDatadir" ); |
271 | QDate reference ( 2000,1,1 ); | 278 | QDate reference ( 2000,1,1 ); |
272 | int daysTo = reference.daysTo ( QDate::currentDate() ); | 279 | int daysTo = reference.daysTo ( QDate::currentDate() ); |
273 | bool saveDate = false; | 280 | bool saveDate = false; |
274 | if ( daysTo - d_count >= mLastBackupDate ) { | 281 | if ( daysTo - d_count >= mLastBackupDate ) { |
275 | qDebug("KA: Last backup was %d days ago ", daysTo - mLastBackupDate ); | 282 | qDebug("KA: Last backup was %d days ago ", daysTo - mLastBackupDate ); |
276 | if ( stdDir ) | 283 | if ( stdDir ) |
277 | bupDir = KGlobalSettings::backupDataDir(); | 284 | bupDir = KGlobalSettings::backupDataDir(); |