summaryrefslogtreecommitdiffabout
authorzautrix <zautrix>2005-04-21 22:07:10 (UTC)
committer zautrix <zautrix>2005-04-21 22:07:10 (UTC)
commit6284b1d7d597463347b471ed8ec2770f4e2d449b (patch) (unidiff)
tree5444a13ed788191733fec406791ddf03ec246b59
parentfd38343ef53c9b2a48208f747100579703cc1814 (diff)
downloadkdepimpi-6284b1d7d597463347b471ed8ec2770f4e2d449b.zip
kdepimpi-6284b1d7d597463347b471ed8ec2770f4e2d449b.tar.gz
kdepimpi-6284b1d7d597463347b471ed8ec2770f4e2d449b.tar.bz2
fixes
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--kabc/plugins/file/resourcefile.cpp49
-rw-r--r--kabc/plugins/file/resourcefile.h1
-rw-r--r--pwmanager/pwmanager/pwmdoc.cpp45
3 files changed, 93 insertions, 2 deletions
diff --git a/kabc/plugins/file/resourcefile.cpp b/kabc/plugins/file/resourcefile.cpp
index 2bd9e71..c89939d 100644
--- a/kabc/plugins/file/resourcefile.cpp
+++ b/kabc/plugins/file/resourcefile.cpp
@@ -1,151 +1,153 @@
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/*
22Enhanced Version of the file for platform independent KDE tools. 22Enhanced Version of the file for platform independent KDE tools.
23Copyright (c) 2004 Ulf Schenk 23Copyright (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 47
47#include "formatfactory.h" 48#include "formatfactory.h"
48 49
49#include "resource.h" 50#include "resource.h"
50#include "resourcefileconfig.h" 51#include "resourcefileconfig.h"
51#include "stdaddressbook.h" 52#include "stdaddressbook.h"
52#define NO_DIRWATCH 53#define NO_DIRWATCH
53#include "resourcefile.h" 54#include "resourcefile.h"
54 55
55//#define ALLOW_LOCKING 56//#define ALLOW_LOCKING
56 57
57 58
58using namespace KABC; 59using namespace KABC;
59 60
60extern "C" 61extern "C"
61#ifdef _WIN32_ 62#ifdef _WIN32_
62__declspec(dllexport) 63__declspec(dllexport)
63#else 64#else
64{ 65{
65#endif 66#endif
66 67
67//US void *init_kabc_file() 68//US void *init_kabc_file()
68 void *init_microkabc_file() 69 void *init_microkabc_file()
69 { 70 {
70 return new KRES::PluginFactory<ResourceFile,ResourceFileConfig>(); 71 return new KRES::PluginFactory<ResourceFile,ResourceFileConfig>();
71 } 72 }
72#ifndef _WIN32_ 73#ifndef _WIN32_
73} 74}
74#endif 75#endif
75 76
76ResourceFile::ResourceFile( const KConfig *config ) 77ResourceFile::ResourceFile( const KConfig *config )
77 : Resource( config ) , mFormat( 0 ) 78 : Resource( config ) , mFormat( 0 )
78{ 79{
79 QString fileName, formatName, default_fileName; 80 QString fileName, formatName, default_fileName;
80 81
81 default_fileName = StdAddressBook::fileName(); 82 default_fileName = StdAddressBook::fileName();
82 83 mLastBackupDate = -1;
83 KConfig *cfg = (KConfig *)config; 84 KConfig *cfg = (KConfig *)config;
84 if ( cfg ) { 85 if ( cfg ) {
85 fileName = cfg->readEntry( "FileName", default_fileName ); 86 fileName = cfg->readEntry( "FileName", default_fileName );
86 formatName = cfg->readEntry( "FileFormat", "vcard" ); 87 formatName = cfg->readEntry( "FileFormat", "vcard" );
87 mFamily = cfg->readEntry( "ResourceName", "std" ); 88 mFamily = cfg->readEntry( "ResourceName", "std" );
89 mLastBackupDate = cfg->readNumEntry( "LastBackupDate", 0 );
88 } else { 90 } else {
89 fileName = default_fileName; 91 fileName = default_fileName;
90 formatName = "vcard"; 92 formatName = "vcard";
91 } 93 }
92 94
93 init( fileName, formatName ); 95 init( fileName, formatName );
94} 96}
95 97
96ResourceFile::ResourceFile( const QString &fileName , 98ResourceFile::ResourceFile( const QString &fileName ,
97 const QString &formatName ) 99 const QString &formatName )
98 : Resource( 0 ) 100 : Resource( 0 )
99{ 101{
100// qDebug("ResourceFile::ResourceFile : 3 %s, %s", fileName.latin1(), formatName.latin1()); 102// qDebug("ResourceFile::ResourceFile : 3 %s, %s", fileName.latin1(), formatName.latin1());
101 103
102 104
103 105 mLastBackupDate = -1;
104 init( fileName, formatName ); 106 init( fileName, formatName );
105} 107}
106 108
107void ResourceFile::init( const QString &fileName, const QString &formatName ) 109void ResourceFile::init( const QString &fileName, const QString &formatName )
108{ 110{
109 mFormatName = formatName; 111 mFormatName = formatName;
110 112
111 FormatFactory *factory = FormatFactory::self(); 113 FormatFactory *factory = FormatFactory::self();
112 mFormat = factory->format( mFormatName ); 114 mFormat = factory->format( mFormatName );
113 115
114 if ( !mFormat ) { 116 if ( !mFormat ) {
115 mFormatName = "vcard"; 117 mFormatName = "vcard";
116 mFormat = factory->format( mFormatName ); 118 mFormat = factory->format( mFormatName );
117 } 119 }
118 120
119#ifndef NO_DIRWATCH 121#ifndef NO_DIRWATCH
120 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); 122 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) );
121 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); 123 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) );
122 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); 124 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) );
123#endif 125#endif
124 126
125 QString localKdeDir; 127 QString localKdeDir;
126 localKdeDir = readEnvPath("LOCALMICROKDEHOME"); 128 localKdeDir = readEnvPath("LOCALMICROKDEHOME");
127 if ( ! localKdeDir.isEmpty() ) { 129 if ( ! localKdeDir.isEmpty() ) {
128 qDebug("LOCALMICROKDEHOME is set to: %s",localKdeDir.latin1() ); 130 qDebug("LOCALMICROKDEHOME is set to: %s",localKdeDir.latin1() );
129 QFileInfo fi ( fileName ); 131 QFileInfo fi ( fileName );
130 QString localname = localKdeDir + "/apps/kabc/" + fi.fileName (); 132 QString localname = localKdeDir + "/apps/kabc/" + fi.fileName ();
131 QFileInfo fi2 ( localname ); 133 QFileInfo fi2 ( localname );
132 if ( ! fi2.exists() || mFamily == "sync_res" ) { 134 if ( ! fi2.exists() || mFamily == "sync_res" ) {
133 if ( fi.exists() && mFamily == "sync_res") { 135 if ( fi.exists() && mFamily == "sync_res") {
134 qDebug("LOCAL mode SYNC mode using absolute file path "); 136 qDebug("LOCAL mode SYNC mode using absolute file path ");
135 setFileName( fileName ); 137 setFileName( fileName );
136 return; 138 return;
137 } else { 139 } else {
138 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/") ); 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/") );
139 setFileName( localname ); 141 setFileName( localname );
140 return; 142 return;
141 } 143 }
142 144
143 } else { 145 } else {
144 qDebug("Local resource file found. Changing filename to: %s",localname.latin1() ); 146 qDebug("Local resource file found. Changing filename to: %s",localname.latin1() );
145 setFileName( localname ); 147 setFileName( localname );
146 return; 148 return;
147 } 149 }
148 150
149 } 151 }
150 setFileName( fileName ); 152 setFileName( fileName );
151} 153}
@@ -212,96 +214,139 @@ bool ResourceFile::doOpen()
212 return ok; 214 return ok;
213 } 215 }
214} 216}
215 217
216void ResourceFile::doClose() 218void ResourceFile::doClose()
217{ 219{
218} 220}
219 221
220bool ResourceFile::load() 222bool ResourceFile::load()
221{ 223{
222 224
223 QFile file( fileName() ); 225 QFile file( fileName() );
224 if ( !file.open( IO_ReadOnly ) ) { 226 if ( !file.open( IO_ReadOnly ) ) {
225 addressBook()->error( i18n( "Unable to open file '%1'." ).arg( fileName() ) ); 227 addressBook()->error( i18n( "Unable to open file '%1'." ).arg( fileName() ) );
226 return false; 228 return false;
227 } 229 }
228 230
229// qDebug("ResourceFile::load format %s, %s", mFileName.latin1(), mFormatName.latin1()); 231// qDebug("ResourceFile::load format %s, %s", mFileName.latin1(), mFormatName.latin1());
230 232
231 return mFormat->loadAll( addressBook(), this, &file ); 233 return mFormat->loadAll( addressBook(), this, &file );
232} 234}
233 235
234bool ResourceFile::save( Ticket *ticket ) 236bool ResourceFile::save( Ticket *ticket )
235{ 237{
236// qDebug("ResourceFile::save format %s, %s", mFileName.latin1(), mFormatName.latin1()); 238// qDebug("ResourceFile::save format %s, %s", mFileName.latin1(), mFormatName.latin1());
237 239
238 240
239 // create backup file 241 // create backup file
240 QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() ); 242 QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() );
241 243
242/*US we use a simpler method to create a backupfile 244/*US we use a simpler method to create a backupfile
243 245
244 (void) KSaveFile::backupFile( mFileName, QString::null 246 (void) KSaveFile::backupFile( mFileName, QString::null
245 ,extension ); 247 ,extension );
246 248
247 KSaveFile saveFile( mFileName ); 249 KSaveFile saveFile( mFileName );
248 bool ok = false; 250 bool ok = false;
249 if ( saveFile.status() == 0 && saveFile.file() ) 251 if ( saveFile.status() == 0 && saveFile.file() )
250 { 252 {
251 mFormat->saveAll( addressBook(), this, saveFile.file() ); 253 mFormat->saveAll( addressBook(), this, saveFile.file() );
252 ok = saveFile.close(); 254 ok = saveFile.close();
253 } 255 }
254*/ 256*/
255 257
256//US ToDo: write backupfile 258//US ToDo: write backupfile
257#ifndef NO_DIRWATCH 259#ifndef NO_DIRWATCH
258 mDirWatch.stopScan(); 260 mDirWatch.stopScan();
259#endif 261#endif
262 if ( mLastBackupDate >= 0 && mFamily != "sync_res") {
263 KConfig conf (locateLocal("config","microkdeglobalrc"));
264 conf.setGroup( "BackupSettings" );
265 bool b_enabled = conf.readBoolEntry( "BackupEnabled" );
266 if ( b_enabled ) {
267 int num = conf.readNumEntry( "BackupNumbers" );
268 int d_count = conf.readNumEntry( "BackupDayCount" );
269 bool stdDir = conf.readBoolEntry( "BackupUseDefaultDir" );
270 QString bupDir = conf.readEntry( "BackupDatadir" );
271 QDate reference ( 2000,1,1 );
272 int daysTo = reference.daysTo ( QDate::currentDate() );
273 bool saveDate = false;
274 if ( daysTo - d_count >= mLastBackupDate ) {
275 qDebug("KA: Last backup was %d days ago ", daysTo - mLastBackupDate );
276 if ( stdDir )
277 bupDir = KGlobalSettings::backupDataDir();
278 int retval = KApplication::createBackup( fileName(), bupDir, num );
279 if ( retval == 0 ) {
280 qDebug("KO: Backup cancelled. Will try again tomorrow ");
281 // retval == 0 : backup skipped for today, try again tomorrow
282 mLastBackupDate = daysTo - d_count+1;
283 saveDate = true;
284 } else if ( retval == 1 ){
285 qDebug("KO: Backup created.");
286 // backup ok
287 mLastBackupDate = daysTo;
288 saveDate = true;
289 } else if ( retval == 2 ){
290 qDebug("KO: Backup globally cancelled.");
291 // backup globally cancelled
292 b_enabled = false;
293 }
294 if ( !b_enabled ) {
295 conf.writeEntry( "mBackupEnabled", false );
296 }
297 if ( saveDate ) {
298 KConfig config ( locateLocal("config","kabcrc") );
299 config.setGroup( "Resource_" + identifier() );
300 config.writeEntry( "LastBackupDate", mLastBackupDate );
301 }
302 }
303 }
304 }
260 QFile info; 305 QFile info;
261 info.setName( fileName() ); 306 info.setName( fileName() );
262 bool ok = info.open( IO_WriteOnly ); 307 bool ok = info.open( IO_WriteOnly );
263 if ( ok ) { 308 if ( ok ) {
264 mFormat->saveAll( addressBook(), this, &info ); 309 mFormat->saveAll( addressBook(), this, &info );
265 310
266 info.close(); 311 info.close();
267 ok = true; 312 ok = true;
268 } 313 }
269 else { 314 else {
270 315
271 } 316 }
272 317
273 if ( !ok ) 318 if ( !ok )
274 addressBook()->error( i18n( "Unable to save file '%1'." ).arg( fileName() ) ); 319 addressBook()->error( i18n( "Unable to save file '%1'." ).arg( fileName() ) );
275#ifndef NO_DIRWATCH 320#ifndef NO_DIRWATCH
276 mDirWatch.startScan(); 321 mDirWatch.startScan();
277#endif 322#endif
278 delete ticket; 323 delete ticket;
279#ifdef ALLOW_LOCKING 324#ifdef ALLOW_LOCKING
280 unlock( mFileName ); 325 unlock( mFileName );
281#endif 326#endif
282 327
283 return ok; 328 return ok;
284} 329}
285 330
286bool ResourceFile::lock( const QString &fileName ) 331bool ResourceFile::lock( const QString &fileName )
287{ 332{
288#ifdef ALLOW_LOCKING 333#ifdef ALLOW_LOCKING
289 334
290 335
291 QString fn = fileName; 336 QString fn = fileName;
292 337
293//US change the implementation how the lockfilename is getting created 338//US change the implementation how the lockfilename is getting created
294//US fn.replace( QRegExp("/"), "_" ); 339//US fn.replace( QRegExp("/"), "_" );
295//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); 340//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" );
296 341
297 KURL url(fn); 342 KURL url(fn);
298 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 343 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
299 344
300 345
301 346
302 if (QFile::exists( lockName )) return false; 347 if (QFile::exists( lockName )) return false;
303 348
304 QString lockUniqueName; 349 QString lockUniqueName;
305 lockUniqueName = fn + KApplication::randomString( 8 ); 350 lockUniqueName = fn + KApplication::randomString( 8 );
306 351
307 url = lockUniqueName; 352 url = lockUniqueName;
diff --git a/kabc/plugins/file/resourcefile.h b/kabc/plugins/file/resourcefile.h
index 3e9edfc..61da154 100644
--- a/kabc/plugins/file/resourcefile.h
+++ b/kabc/plugins/file/resourcefile.h
@@ -102,63 +102,64 @@ public:
102 * Returns true if all addressees could be saved otherwise false. 102 * Returns true if all addressees could be saved otherwise false.
103 * 103 *
104 * @param ticket The ticket returned by @ref requestSaveTicket() 104 * @param ticket The ticket returned by @ref requestSaveTicket()
105 */ 105 */
106 virtual bool save( Ticket *ticket ); 106 virtual bool save( Ticket *ticket );
107 107
108 /** 108 /**
109 * Set name of file to be used for saving. 109 * Set name of file to be used for saving.
110 */ 110 */
111 void setFileName( const QString & ); 111 void setFileName( const QString & );
112 112
113 /** 113 /**
114 * Return name of file used for loading and saving the address book. 114 * Return name of file used for loading and saving the address book.
115 */ 115 */
116 QString fileName() const; 116 QString fileName() const;
117 117
118 /** 118 /**
119 Sets a new format by name. 119 Sets a new format by name.
120 */ 120 */
121 void setFormat( const QString &name ); 121 void setFormat( const QString &name );
122 122
123 /** 123 /**
124 Returns the format name. 124 Returns the format name.
125 */ 125 */
126 QString format() const; 126 QString format() const;
127 127
128 /** 128 /**
129 * Remove a addressee from its source. 129 * Remove a addressee from its source.
130 * This method is mainly called by KABC::AddressBook. 130 * This method is mainly called by KABC::AddressBook.
131 */ 131 */
132 virtual void removeAddressee( const Addressee& addr ); 132 virtual void removeAddressee( const Addressee& addr );
133 133
134 /** 134 /**
135 * This method is called by an error handler if the application 135 * This method is called by an error handler if the application
136 * crashed 136 * crashed
137 */ 137 */
138 virtual void cleanUp(); 138 virtual void cleanUp();
139 139
140protected slots: 140protected slots:
141 void fileChanged(); 141 void fileChanged();
142 142
143protected: 143protected:
144 void init( const QString &fileName, const QString &format ); 144 void init( const QString &fileName, const QString &format );
145 145
146 bool lock( const QString &fileName ); 146 bool lock( const QString &fileName );
147 void unlock( const QString &fileName ); 147 void unlock( const QString &fileName );
148 148
149private: 149private:
150 int mLastBackupDate;
150 QString mFamily; 151 QString mFamily;
151 QString mFileName2; 152 QString mFileName2;
152 QString mFormatName; 153 QString mFormatName;
153 154
154 FormatPlugin *mFormat; 155 FormatPlugin *mFormat;
155 156
156 QString mLockUniqueName; 157 QString mLockUniqueName;
157#ifndef NO_DIRWATCH 158#ifndef NO_DIRWATCH
158 KDirWatch mDirWatch; 159 KDirWatch mDirWatch;
159#endif 160#endif
160}; 161};
161 162
162} 163}
163 164
164#endif 165#endif
diff --git a/pwmanager/pwmanager/pwmdoc.cpp b/pwmanager/pwmanager/pwmdoc.cpp
index 61f2616..36b0df5 100644
--- a/pwmanager/pwmanager/pwmdoc.cpp
+++ b/pwmanager/pwmanager/pwmdoc.cpp
@@ -1,85 +1,87 @@
1/*************************************************************************** 1/***************************************************************************
2 * * 2 * *
3 * copyright (C) 2003, 2004 by Michael Buesch * 3 * copyright (C) 2003, 2004 by Michael Buesch *
4 * email: mbuesch@freenet.de * 4 * email: mbuesch@freenet.de *
5 * * 5 * *
6 * This program is free software; you can redistribute it and/or modify * 6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License version 2 * 7 * it under the terms of the GNU General Public License version 2 *
8 * as published by the Free Software Foundation. * 8 * as published by the Free Software Foundation. *
9 * * 9 * *
10 ***************************************************************************/ 10 ***************************************************************************/
11 11
12/*************************************************************************** 12/***************************************************************************
13 * copyright (C) 2004 by Ulf Schenk 13 * copyright (C) 2004 by Ulf Schenk
14 * This file is originaly based on version 1.1 of pwmanager 14 * This file is originaly based on version 1.1 of pwmanager
15 * and was modified to run on embedded devices that run microkde 15 * and was modified to run on embedded devices that run microkde
16 * 16 *
17 * $Id$ 17 * $Id$
18 **************************************************************************/ 18 **************************************************************************/
19 19
20#include "pwmdoc.h" 20#include "pwmdoc.h"
21#include "pwmview.h" 21#include "pwmview.h"
22#include "blowfish.h" 22#include "blowfish.h"
23#include "sha1.h" 23#include "sha1.h"
24#include "globalstuff.h" 24#include "globalstuff.h"
25#include "gpasmanfile.h" 25#include "gpasmanfile.h"
26#include "serializer.h" 26#include "serializer.h"
27#include "compressgzip.h" 27#include "compressgzip.h"
28//US#include "compressbzip2.h" 28//US#include "compressbzip2.h"
29#include "randomizer.h" 29#include "randomizer.h"
30#include "pwminit.h" 30#include "pwminit.h"
31#include "libgcryptif.h" 31#include "libgcryptif.h"
32#ifdef PWM_EMBEDDED 32#ifdef PWM_EMBEDDED
33#include "pwmprefs.h" 33#include "pwmprefs.h"
34#include "kglobal.h" 34#include "kglobal.h"
35#endif 35#endif
36 36
37#include <kmessagebox.h> 37#include <kmessagebox.h>
38#include <kconfig.h>
39#include <kglobalsettings.h>
38#include <libkcal/syncdefines.h> 40#include <libkcal/syncdefines.h>
39 41
40 42
41#ifdef CONFIG_KWALLETIF 43#ifdef CONFIG_KWALLETIF
42# include "kwalletemu.h" 44# include "kwalletemu.h"
43#endif // CONFIG_KWALLETIF 45#endif // CONFIG_KWALLETIF
44 46
45#include <qdatetime.h> 47#include <qdatetime.h>
46#include <qsize.h> 48#include <qsize.h>
47#include <qfileinfo.h> 49#include <qfileinfo.h>
48#include <qfile.h> 50#include <qfile.h>
49 51
50#include <stdio.h> 52#include <stdio.h>
51#include <stdlib.h> 53#include <stdlib.h>
52#include <errno.h> 54#include <errno.h>
53#include <string.h> 55#include <string.h>
54//US#include <iostream> 56//US#include <iostream>
55#include <algorithm> 57#include <algorithm>
56#include <sys/types.h> 58#include <sys/types.h>
57#include <sys/stat.h> 59#include <sys/stat.h>
58#ifndef _WIN32_ 60#ifndef _WIN32_
59#include <unistd.h> 61#include <unistd.h>
60#include <stdint.h> 62#include <stdint.h>
61#endif 63#endif
62 64
63#ifdef PWM_EMBEDDED 65#ifdef PWM_EMBEDDED
64#ifndef Q_LONG 66#ifndef Q_LONG
65#define Q_LONG long 67#define Q_LONG long
66#endif 68#endif
67 69
68#ifndef Q_ULONG 70#ifndef Q_ULONG
69#define Q_ULONG unsigned long 71#define Q_ULONG unsigned long
70#endif 72#endif
71#endif //PWM_EMBEDDED 73#endif //PWM_EMBEDDED
72 74
73 75
74//TODO: reset to its normal value. 76//TODO: reset to its normal value.
75//LR set to 5 min 77//LR set to 5 min
76 #define META_CHECK_TIMER_INTERVAL300 /* 10 300*/ /* sek */ 78 #define META_CHECK_TIMER_INTERVAL300 /* 10 300*/ /* sek */
77 79
78using namespace std; 80using namespace std;
79 81
80 82
81void PwMDocList::add(PwMDoc *doc, const string &id) 83void PwMDocList::add(PwMDoc *doc, const string &id)
82{ 84{
83#ifdef PWM_DEBUG 85#ifdef PWM_DEBUG
84 // check for existance of object in debug mode only. 86 // check for existance of object in debug mode only.
85 vector<listItem>::iterator begin = docList.begin(), 87 vector<listItem>::iterator begin = docList.begin(),
@@ -382,96 +384,139 @@ PwMerror PwMDoc::saveDoc(char compress, const QString *file)
382 emitDataChanged(this); 384 emitDataChanged(this);
383 return e_openFile; 385 return e_openFile;
384 } 386 }
385 } 387 }
386 388
387 if (!isPwAvailable()) { 389 if (!isPwAvailable()) {
388 /* password is not available. This means, the 390 /* password is not available. This means, the
389 * document wasn't saved, yet. 391 * document wasn't saved, yet.
390 */ 392 */
391 bool useChipcard = getDocStatFlag(DOC_STAT_USE_CHIPCARD); 393 bool useChipcard = getDocStatFlag(DOC_STAT_USE_CHIPCARD);
392 QString pw(requestNewMpw(&useChipcard)); 394 QString pw(requestNewMpw(&useChipcard));
393 if (pw != "") { 395 if (pw != "") {
394 currentPw = pw; 396 currentPw = pw;
395 } else { 397 } else {
396 return e_noPw; 398 return e_noPw;
397 } 399 }
398 if (useChipcard) { 400 if (useChipcard) {
399 setDocStatFlag(DOC_STAT_USE_CHIPCARD); 401 setDocStatFlag(DOC_STAT_USE_CHIPCARD);
400 } else { 402 } else {
401 unsetDocStatFlag(DOC_STAT_USE_CHIPCARD); 403 unsetDocStatFlag(DOC_STAT_USE_CHIPCARD);
402 } 404 }
403 } 405 }
404 406
405 int _cryptAlgo = conf()->confGlobCryptAlgo(); 407 int _cryptAlgo = conf()->confGlobCryptAlgo();
406 int _hashAlgo = conf()->confGlobHashAlgo(); 408 int _hashAlgo = conf()->confGlobHashAlgo();
407 409
408 // sanity check for the selected algorithms 410 // sanity check for the selected algorithms
409 if (_cryptAlgo < PWM_CRYPT_BLOWFISH || 411 if (_cryptAlgo < PWM_CRYPT_BLOWFISH ||
410 _cryptAlgo > PWM_CRYPT_TWOFISH128) { 412 _cryptAlgo > PWM_CRYPT_TWOFISH128) {
411 printWarn("Invalid Crypto-Algorithm selected! " 413 printWarn("Invalid Crypto-Algorithm selected! "
412 "Config-file seems to be corrupt. " 414 "Config-file seems to be corrupt. "
413 "Falling back to Blowfish."); 415 "Falling back to Blowfish.");
414 _cryptAlgo = PWM_CRYPT_BLOWFISH; 416 _cryptAlgo = PWM_CRYPT_BLOWFISH;
415 } 417 }
416 if (_hashAlgo < PWM_HASH_SHA1 || 418 if (_hashAlgo < PWM_HASH_SHA1 ||
417 _hashAlgo > PWM_HASH_TIGER) { 419 _hashAlgo > PWM_HASH_TIGER) {
418 printWarn("Invalid Hash-Algorithm selected! " 420 printWarn("Invalid Hash-Algorithm selected! "
419 "Config-file seems to be corrupt. " 421 "Config-file seems to be corrupt. "
420 "Falling back to SHA1."); 422 "Falling back to SHA1.");
421 _hashAlgo = PWM_HASH_SHA1; 423 _hashAlgo = PWM_HASH_SHA1;
422 } 424 }
423 char cryptAlgo = static_cast<char>(_cryptAlgo); 425 char cryptAlgo = static_cast<char>(_cryptAlgo);
424 char hashAlgo = static_cast<char>(_hashAlgo); 426 char hashAlgo = static_cast<char>(_hashAlgo);
425 427
426 if (conf()->confGlobMakeFileBackup()) { 428 if (conf()->confGlobMakeFileBackup()) {
427 if (!backupFile(filename)) 429 if (!backupFile(filename))
428 return e_fileBackup; 430 return e_fileBackup;
429 } 431 }
432 int mLastBackupDate = 0;
433 KConfig configGlobal (locateLocal("config","pwmanagerbuprc"));
434 QFileInfo fileInfo ( filename );
435 mLastBackupDate = configGlobal.readNumEntry( "LastBackupDate-"+ fileInfo.fileName (), 0 );
436 KConfig config (locateLocal("config","microkdeglobalrc"));
437 config.setGroup( "BackupSettings" );
438 bool b_enabled = config.readBoolEntry( "BackupEnabled" );
439 if ( b_enabled ) {
440 int num = config.readNumEntry( "BackupNumbers" );
441 int d_count = config.readNumEntry( "BackupDayCount" );
442 bool stdDir = config.readBoolEntry( "BackupUseDefaultDir" );
443 QString bupDir = config.readEntry( "BackupDatadir" );
444 QDate reference ( 2000,1,1 );
445 int daysTo = reference.daysTo ( QDate::currentDate() );
446 bool saveDate = false;
447 if ( daysTo - d_count >= mLastBackupDate ) {
448 qDebug("KA: Last backup was %d days ago ", daysTo - mLastBackupDate );
449 if ( stdDir )
450 bupDir = KGlobalSettings::backupDataDir();
451 int retval = KApplication::createBackup( filename, bupDir, num );
452 if ( retval == 0 ) {
453 qDebug("KO: Backup cancelled. Will try again tomorrow ");
454 // retval == 0 : backup skipped for today, try again tomorrow
455 mLastBackupDate = daysTo - d_count+1;
456 saveDate = true;
457 } else if ( retval == 1 ){
458 qDebug("KO: Backup created.");
459 // backup ok
460 mLastBackupDate = daysTo;
461 saveDate = true;
462 } else if ( retval == 2 ){
463 qDebug("KO: Backup globally cancelled.");
464 // backup globally cancelled
465 b_enabled = false;
466 }
467 if ( !b_enabled ) {
468 config.writeEntry( "mBackupEnabled", false );
469 }
470 if ( saveDate ) {
471 configGlobal.writeEntry( "LastBackupDate-"+ fileInfo.fileName (), mLastBackupDate );
472 }
473 }
474 }
430 if (QFile::exists(filename)) { 475 if (QFile::exists(filename)) {
431 /* Move the existing file to some tmp file. 476 /* Move the existing file to some tmp file.
432 * When saving file succeeds, delete tmp file. Otherwise 477 * When saving file succeeds, delete tmp file. Otherwise
433 * move tmp file back. See below. 478 * move tmp file back. See below.
434 */ 479 */
435 Randomizer *rnd = Randomizer::obj(); 480 Randomizer *rnd = Randomizer::obj();
436 char rnd_buf[5]; 481 char rnd_buf[5];
437 sprintf(rnd_buf, "%X%X%X%X", rnd->genRndChar() & 0xFF, rnd->genRndChar() & 0xFF, 482 sprintf(rnd_buf, "%X%X%X%X", rnd->genRndChar() & 0xFF, rnd->genRndChar() & 0xFF,
438 rnd->genRndChar() & 0xFF, rnd->genRndChar() & 0xFF); 483 rnd->genRndChar() & 0xFF, rnd->genRndChar() & 0xFF);
439 tmpFileMoved = filename + "." + rnd_buf + ".mv"; 484 tmpFileMoved = filename + "." + rnd_buf + ".mv";
440 if (!copyFile(filename, tmpFileMoved)) 485 if (!copyFile(filename, tmpFileMoved))
441 return e_openFile; 486 return e_openFile;
442 if (!QFile::remove(filename)) { 487 if (!QFile::remove(filename)) {
443 printWarn(string("removing orig file ") 488 printWarn(string("removing orig file ")
444 + filename.latin1() 489 + filename.latin1()
445 + " failed!"); 490 + " failed!");
446 } 491 }
447 } 492 }
448 f.setName(filename); 493 f.setName(filename);
449 if (!f.open(IO_ReadWrite)) { 494 if (!f.open(IO_ReadWrite)) {
450 ret = e_openFile; 495 ret = e_openFile;
451 goto out_moveback; 496 goto out_moveback;
452 } 497 }
453 e = writeFileHeader(hashAlgo, hashAlgo, 498 e = writeFileHeader(hashAlgo, hashAlgo,
454 cryptAlgo, compress, 499 cryptAlgo, compress,
455 &currentPw, &f); 500 &currentPw, &f);
456 if (e == e_hashNotImpl) { 501 if (e == e_hashNotImpl) {
457 printDebug("PwMDoc::saveDoc(): writeFileHeader() failed: e_hashNotImpl"); 502 printDebug("PwMDoc::saveDoc(): writeFileHeader() failed: e_hashNotImpl");
458 f.close(); 503 f.close();
459 ret = e_hashNotImpl; 504 ret = e_hashNotImpl;
460 goto out_moveback; 505 goto out_moveback;
461 } else if (e != e_success) { 506 } else if (e != e_success) {
462 printDebug("PwMDoc::saveDoc(): writeFileHeader() failed"); 507 printDebug("PwMDoc::saveDoc(): writeFileHeader() failed");
463 f.close(); 508 f.close();
464 ret = e_writeHeader; 509 ret = e_writeHeader;
465 goto out_moveback; 510 goto out_moveback;
466 } 511 }
467 if (!serializeDta(&serialized)) { 512 if (!serializeDta(&serialized)) {
468 printDebug("PwMDoc::saveDoc(): serializeDta() failed"); 513 printDebug("PwMDoc::saveDoc(): serializeDta() failed");
469 f.close(); 514 f.close();
470 ret = e_serializeDta; 515 ret = e_serializeDta;
471 goto out_moveback; 516 goto out_moveback;
472 } 517 }
473 e = writeDataHash(hashAlgo, &serialized, &f); 518 e = writeDataHash(hashAlgo, &serialized, &f);
474 if (e == e_hashNotImpl) { 519 if (e == e_hashNotImpl) {
475 printDebug("PwMDoc::saveDoc(): writeDataHash() failed: e_hashNotImpl"); 520 printDebug("PwMDoc::saveDoc(): writeDataHash() failed: e_hashNotImpl");
476 f.close(); 521 f.close();
477 ret = e_hashNotImpl; 522 ret = e_hashNotImpl;