summaryrefslogtreecommitdiffabout
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--pwmanager/pwmanager/libgcryptif.cpp25
-rw-r--r--pwmanager/pwmanager/libgcryptif.h12
-rw-r--r--pwmanager/pwmanager/pwmdoc.cpp20
-rw-r--r--pwmanager/pwmanager/pwmdoc.h8
4 files changed, 46 insertions, 19 deletions
diff --git a/pwmanager/pwmanager/libgcryptif.cpp b/pwmanager/pwmanager/libgcryptif.cpp
index ff94bf6..15f6cef 100644
--- a/pwmanager/pwmanager/libgcryptif.cpp
+++ b/pwmanager/pwmanager/libgcryptif.cpp
@@ -1,454 +1,463 @@
1/*************************************************************************** 1/***************************************************************************
2 * * 2 * *
3 * copyright (C) 2004 by Michael Buesch * 3 * copyright (C) 2004 by Michael Buesch *
4 * email: mbuesch@freenet.de * 4 * email: mbuesch@freenet.de *
5 * * 5 * *
6 * hashPassphrase() is derived from GnuPG and is * 6 * hashPassphrase() is derived from GnuPG and is *
7 * Copyright (C) 1998, 1999, 2000, 2001, 2003 * 7 * Copyright (C) 1998, 1999, 2000, 2001, 2003 *
8 * Free Software Foundation, Inc. * 8 * Free Software Foundation, Inc. *
9 * * 9 * *
10 * This program is free software; you can redistribute it and/or modify * 10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License version 2 * 11 * it under the terms of the GNU General Public License version 2 *
12 * as published by the Free Software Foundation. * 12 * as published by the Free Software Foundation. *
13 * * 13 * *
14 ***************************************************************************/ 14 ***************************************************************************/
15 15
16/*************************************************************************** 16/***************************************************************************
17 * copyright (C) 2004 by Ulf Schenk 17 * copyright (C) 2004 by Ulf Schenk
18 * This file is originaly based on version 1.1 of pwmanager 18 * This file is originaly based on version 1.1 of pwmanager
19 * and was modified to run on embedded devices that run microkde 19 * and was modified to run on embedded devices that run microkde
20 * 20 *
21 * $Id$ 21 * $Id$
22 **************************************************************************/ 22 **************************************************************************/
23 23
24#include "libgcryptif.h" 24#include "libgcryptif.h"
25 25
26#ifdef CONFIG_PWMANAGER_GCRY 26#ifdef CONFIG_PWMANAGER_GCRY
27 27
28#include "pwmdoc.h" 28#include "pwmdoc.h"
29#include "randomizer.h" 29#include "randomizer.h"
30 30
31#include <gcrypt.h> 31#include <gcrypt.h>
32 32
33#ifdef PWM_EMBEDDED 33#ifdef PWM_EMBEDDED
34#include <pwmprefs.h> 34#include <pwmprefs.h>
35#endif 35#endif
36 36
37 37
38PwMerror LibGCryptIf::encrypt(unsigned char **outBuf, 38PwMerror LibGCryptIf::encrypt(unsigned char **outBuf,
39 size_t *outBufLen, 39 size_t *outBufLen,
40 unsigned char *inBuf, 40 unsigned char *inBuf,
41 size_t inBufLen, 41 size_t inBufLen,
42 const unsigned char *key, 42 const unsigned char *key,
43 size_t keylen, 43 size_t keylen,
44 char _algo) 44 char _algo,
45 char _hashalgo //US BUG: pass _hashalgo because we need it in hashPassphrase
46 )
45{ 47{
46 PwMerror ret = e_success; 48 PwMerror ret = e_success;
47 gcry_error_t err; 49 gcry_error_t err;
48 gcry_cipher_hd_t handle; 50 gcry_cipher_hd_t handle;
49 size_t blklen; 51 size_t blklen;
50 size_t unpaddedLen = inBufLen; 52 size_t unpaddedLen = inBufLen;
51 size_t cipherKeylen; 53 size_t cipherKeylen;
52 unsigned char *hashedKey; 54 unsigned char *hashedKey;
53 unsigned char salt[STRING2KEY_SALTLEN]; 55 unsigned char salt[STRING2KEY_SALTLEN];
54 int algo = mapCipherId(_algo); 56 int algo = mapCipherId(_algo);
55 57
56 if (!inBufLen || !keylen) 58 if (!inBufLen || !keylen)
57 return e_invalidArg; 59 return e_invalidArg;
58 60
59 // test if algo is ready for encryption 61 // test if algo is ready for encryption
60 err = gcry_cipher_algo_info(algo, 62 err = gcry_cipher_algo_info(algo,
61 GCRYCTL_TEST_ALGO, 63 GCRYCTL_TEST_ALGO,
62 0, 0); 64 0, 0);
63 if (err != GPG_ERR_NO_ERROR) { 65 if (err != GPG_ERR_NO_ERROR) {
64 printDebug(string("LibGCryptIf::doEncrypt(): GCRYCTL_TEST_ALGO failed: ") 66 printDebug(string("LibGCryptIf::doEncrypt(): GCRYCTL_TEST_ALGO failed: ")
65 + gcry_strerror(err)); 67 + gcry_strerror(err));
66 ret = e_cryptNotImpl; 68 ret = e_cryptNotImpl;
67 goto out; 69 goto out;
68 } 70 }
69 // get the algo block length 71 // get the algo block length
70 err = gcry_cipher_algo_info(algo, 72 err = gcry_cipher_algo_info(algo,
71 GCRYCTL_GET_BLKLEN, 73 GCRYCTL_GET_BLKLEN,
72 0, 74 0,
73 &blklen); 75 &blklen);
74 if (err != GPG_ERR_NO_ERROR) { 76 if (err != GPG_ERR_NO_ERROR) {
75 printDebug(string("LibGCryptIf::doEncrypt(): GCRYCTL_GET_BLKLEN failed: ") 77 printDebug(string("LibGCryptIf::doEncrypt(): GCRYCTL_GET_BLKLEN failed: ")
76 + gcry_strerror(err)); 78 + gcry_strerror(err));
77 ret = e_cryptNotImpl; 79 ret = e_cryptNotImpl;
78 goto out; 80 goto out;
79 } 81 }
80 /* double check if we have enough space. 82 /* double check if we have enough space.
81 * We have only 1024 extra bytes for padding and salt. 83 * We have only 1024 extra bytes for padding and salt.
82 */ 84 */
83 BUG_ON(blklen > 1024 - STRING2KEY_SALTLEN); 85 BUG_ON(blklen > 1024 - STRING2KEY_SALTLEN);
84 // get the algo key length 86 // get the algo key length
85 err = gcry_cipher_algo_info(algo, 87 err = gcry_cipher_algo_info(algo,
86 GCRYCTL_GET_KEYLEN, 88 GCRYCTL_GET_KEYLEN,
87 0, 89 0,
88 &cipherKeylen); 90 &cipherKeylen);
89 if (err != GPG_ERR_NO_ERROR) { 91 if (err != GPG_ERR_NO_ERROR) {
90 printDebug(string("LibGCryptIf::doEncrypt(): GCRYCTL_GET_KEYLEN failed: ") 92 printDebug(string("LibGCryptIf::doEncrypt(): GCRYCTL_GET_KEYLEN failed: ")
91 + gcry_strerror(err)); 93 + gcry_strerror(err));
92 ret = e_cryptNotImpl; 94 ret = e_cryptNotImpl;
93 goto out; 95 goto out;
94 } 96 }
95 // now open the algo and get a handle 97 // now open the algo and get a handle
96 err = gcry_cipher_open(&handle, 98 err = gcry_cipher_open(&handle,
97 algo, 99 algo,
98 GCRY_CIPHER_MODE_CBC, 100 GCRY_CIPHER_MODE_CBC,
99 0); 101 0);
100 if (err != GPG_ERR_NO_ERROR) { 102 if (err != GPG_ERR_NO_ERROR) {
101 printDebug(string("LibGCryptIf::doEncrypt(): gcry_cipher_open() failed: ") 103 printDebug(string("LibGCryptIf::doEncrypt(): gcry_cipher_open() failed: ")
102 + gcry_strerror(err)); 104 + gcry_strerror(err));
103 ret = e_cryptNotImpl; 105 ret = e_cryptNotImpl;
104 goto out; 106 goto out;
105 } 107 }
106 // hash the "key" to a fixed size hash matching "cipherKeylen" 108 // hash the "key" to a fixed size hash matching "cipherKeylen"
107 hashedKey = new unsigned char[cipherKeylen]; 109 hashedKey = new unsigned char[cipherKeylen];
108 hashPassphrase(key, keylen, salt, hashedKey, cipherKeylen, true); 110 hashPassphrase(key, keylen, salt, hashedKey, cipherKeylen, true, _hashalgo);
109 // so now set the hashed key 111 // so now set the hashed key
110 err = gcry_cipher_setkey(handle, hashedKey, cipherKeylen); 112 err = gcry_cipher_setkey(handle, hashedKey, cipherKeylen);
111 if (err != GPG_ERR_NO_ERROR) { 113 if (err != GPG_ERR_NO_ERROR) {
112 printDebug(string("LibGCryptIf::doEncrypt(): gcry_cipher_setkey() failed: ") 114 printDebug(string("LibGCryptIf::doEncrypt(): gcry_cipher_setkey() failed: ")
113 + gcry_strerror(err)); 115 + gcry_strerror(err));
114 ret = e_cryptNotImpl; 116 ret = e_cryptNotImpl;
115 delete [] hashedKey; 117 delete [] hashedKey;
116 goto out_close; 118 goto out_close;
117 } 119 }
118 delete [] hashedKey; 120 delete [] hashedKey;
119 /* allocate a buffer for the encrypted data. 121 /* allocate a buffer for the encrypted data.
120 * The size of the buffer is the inBuf length, but blklen 122 * The size of the buffer is the inBuf length, but blklen
121 * aligned and plus the length of the salt, that is appended. 123 * aligned and plus the length of the salt, that is appended.
122 */ 124 */
123 *outBufLen = getBufLen(unpaddedLen, blklen) + STRING2KEY_SALTLEN; 125 *outBufLen = getBufLen(unpaddedLen, blklen) + STRING2KEY_SALTLEN;
124 *outBuf = new unsigned char[*outBufLen]; 126 *outBuf = new unsigned char[*outBufLen];
125 padData(inBuf, unpaddedLen, blklen); 127 padData(inBuf, unpaddedLen, blklen);
126 // encrypt the padded data 128 // encrypt the padded data
127 err = gcry_cipher_encrypt(handle, 129 err = gcry_cipher_encrypt(handle,
128 *outBuf, 130 *outBuf,
129 *outBufLen - STRING2KEY_SALTLEN, 131 *outBufLen - STRING2KEY_SALTLEN,
130 inBuf, 132 inBuf,
131 *outBufLen - STRING2KEY_SALTLEN); 133 *outBufLen - STRING2KEY_SALTLEN);
132 if (err != GPG_ERR_NO_ERROR) { 134 if (err != GPG_ERR_NO_ERROR) {
133 printDebug(string("LibGCryptIf::doEncrypt(): gcry_cipher_encrypt() failed: ") 135 printDebug(string("LibGCryptIf::doEncrypt(): gcry_cipher_encrypt() failed: ")
134 + gcry_strerror(err)); 136 + gcry_strerror(err));
135 ret = e_cryptNotImpl; 137 ret = e_cryptNotImpl;
136 goto out_delete; 138 goto out_delete;
137 } 139 }
138 // append the salt to the encrypted data 140 // append the salt to the encrypted data
139 memcpy(*outBuf + *outBufLen - STRING2KEY_SALTLEN, salt, STRING2KEY_SALTLEN); 141 memcpy(*outBuf + *outBufLen - STRING2KEY_SALTLEN, salt, STRING2KEY_SALTLEN);
140 goto out_close; 142 goto out_close;
141out_delete: 143out_delete:
142 delete [] *outBuf; 144 delete [] *outBuf;
143out_close: 145out_close:
144 gcry_cipher_close(handle); 146 gcry_cipher_close(handle);
145out: 147out:
146 return ret; 148 return ret;
147} 149}
148 150
149PwMerror LibGCryptIf::decrypt(unsigned char **outBuf, 151PwMerror LibGCryptIf::decrypt(unsigned char **outBuf,
150 size_t *outBufLen, 152 size_t *outBufLen,
151 const unsigned char *inBuf, 153 const unsigned char *inBuf,
152 size_t inBufLen, 154 size_t inBufLen,
153 const unsigned char *key, 155 const unsigned char *key,
154 size_t keylen, 156 size_t keylen,
155 char _algo) 157 char _algo,
158 char _hashalgo //US BUG: pass _hashalgo because we need it in hashPassphrase
159)
156{ 160{
157 PwMerror ret = e_success; 161 PwMerror ret = e_success;
158 gcry_error_t err; 162 gcry_error_t err;
159 gcry_cipher_hd_t handle; 163 gcry_cipher_hd_t handle;
160 size_t cipherKeylen; 164 size_t cipherKeylen;
161 unsigned char *hashedKey; 165 unsigned char *hashedKey;
162 unsigned char salt[STRING2KEY_SALTLEN]; 166 unsigned char salt[STRING2KEY_SALTLEN];
163 int algo = mapCipherId(_algo); 167 int algo = mapCipherId(_algo);
164 168
165 if (!inBufLen || !keylen) 169 if (!inBufLen || !keylen)
166 return e_invalidArg; 170 return e_invalidArg;
167 171
168 // test if algo is ready for encryption 172 // test if algo is ready for encryption
169 err = gcry_cipher_algo_info(algo, 173 err = gcry_cipher_algo_info(algo,
170 GCRYCTL_TEST_ALGO, 174 GCRYCTL_TEST_ALGO,
171 0, 0); 175 0, 0);
172 if (err != GPG_ERR_NO_ERROR) { 176 if (err != GPG_ERR_NO_ERROR) {
173 printDebug(string("LibGCryptIf::doDecrypt(): GCRYCTL_TEST_ALGO failed: ") 177 printDebug(string("LibGCryptIf::doDecrypt(): GCRYCTL_TEST_ALGO failed: ")
174 + gcry_strerror(err)); 178 + gcry_strerror(err));
175 ret = e_cryptNotImpl; 179 ret = e_cryptNotImpl;
176 goto out; 180 goto out;
177 } 181 }
178 // get algo key length 182 // get algo key length
179 err = gcry_cipher_algo_info(algo, 183 err = gcry_cipher_algo_info(algo,
180 GCRYCTL_GET_KEYLEN, 184 GCRYCTL_GET_KEYLEN,
181 0, 185 0,
182 &cipherKeylen); 186 &cipherKeylen);
183 if (err != GPG_ERR_NO_ERROR) { 187 if (err != GPG_ERR_NO_ERROR) {
184 printDebug(string("LibGCryptIf::doDecrypt(): GCRYCTL_GET_KEYLEN failed: ") 188 printDebug(string("LibGCryptIf::doDecrypt(): GCRYCTL_GET_KEYLEN failed: ")
185 + gcry_strerror(err)); 189 + gcry_strerror(err));
186 ret = e_cryptNotImpl; 190 ret = e_cryptNotImpl;
187 goto out; 191 goto out;
188 } 192 }
189 // extract the salt of the encrypted data buffer 193 // extract the salt of the encrypted data buffer
190 memcpy(salt, inBuf + inBufLen - STRING2KEY_SALTLEN, STRING2KEY_SALTLEN); 194 memcpy(salt, inBuf + inBufLen - STRING2KEY_SALTLEN, STRING2KEY_SALTLEN);
191 // open the algo and get a handle 195 // open the algo and get a handle
192 err = gcry_cipher_open(&handle, 196 err = gcry_cipher_open(&handle,
193 algo, 197 algo,
194 GCRY_CIPHER_MODE_CBC, 198 GCRY_CIPHER_MODE_CBC,
195 0); 199 0);
196 if (err != GPG_ERR_NO_ERROR) { 200 if (err != GPG_ERR_NO_ERROR) {
197 printDebug(string("LibGCryptIf::doDecrypt(): gcry_cipher_open() failed: ") 201 printDebug(string("LibGCryptIf::doDecrypt(): gcry_cipher_open() failed: ")
198 + gcry_strerror(err)); 202 + gcry_strerror(err));
199 ret = e_cryptNotImpl; 203 ret = e_cryptNotImpl;
200 goto out; 204 goto out;
201 } 205 }
202 // hash the "key" to a fixed size hash matching "cipherKeylen" 206 // hash the "key" to a fixed size hash matching "cipherKeylen"
203 hashedKey = new unsigned char[cipherKeylen]; 207 hashedKey = new unsigned char[cipherKeylen];
204 hashPassphrase(key, keylen, salt, hashedKey, cipherKeylen, false); 208 hashPassphrase(key, keylen, salt, hashedKey, cipherKeylen, false, _hashalgo);
205 // so now set the hashed key 209 // so now set the hashed key
206 err = gcry_cipher_setkey(handle, hashedKey, cipherKeylen); 210 err = gcry_cipher_setkey(handle, hashedKey, cipherKeylen);
207 if (err != GPG_ERR_NO_ERROR) { 211 if (err != GPG_ERR_NO_ERROR) {
208 printDebug(string("LibGCryptIf::doDecrypt(): gcry_cipher_setkey() failed: ") 212 printDebug(string("LibGCryptIf::doDecrypt(): gcry_cipher_setkey() failed: ")
209 + gcry_strerror(err)); 213 + gcry_strerror(err));
210 ret = e_cryptNotImpl; 214 ret = e_cryptNotImpl;
211 delete [] hashedKey; 215 delete [] hashedKey;
212 goto out_close; 216 goto out_close;
213 } 217 }
214 delete [] hashedKey; 218 delete [] hashedKey;
215 *outBufLen = inBufLen - STRING2KEY_SALTLEN; 219 *outBufLen = inBufLen - STRING2KEY_SALTLEN;
216 *outBuf = new unsigned char[*outBufLen]; 220 *outBuf = new unsigned char[*outBufLen];
217 // decrypt the data 221 // decrypt the data
218 err = gcry_cipher_decrypt(handle, 222 err = gcry_cipher_decrypt(handle,
219 *outBuf, 223 *outBuf,
220 *outBufLen, 224 *outBufLen,
221 inBuf, 225 inBuf,
222 *outBufLen); 226 *outBufLen);
223 if (err != GPG_ERR_NO_ERROR) { 227 if (err != GPG_ERR_NO_ERROR) {
224 printDebug(string("LibGCryptIf::doEncrypt(): gcry_cipher_encrypt() failed: ") 228 printDebug(string("LibGCryptIf::doEncrypt(): gcry_cipher_encrypt() failed: ")
225 + gcry_strerror(err)); 229 + gcry_strerror(err));
226 ret = e_cryptNotImpl; 230 ret = e_cryptNotImpl;
227 goto out_delete; 231 goto out_delete;
228 } 232 }
229 // remove all random padding 233 // remove all random padding
230 unpadData(*outBuf, outBufLen); 234 unpadData(*outBuf, outBufLen);
231 goto out_close; 235 goto out_close;
232out_delete: 236out_delete:
233 delete [] *outBuf; 237 delete [] *outBuf;
234out_close: 238out_close:
235 gcry_cipher_close(handle); 239 gcry_cipher_close(handle);
236out: 240out:
237 return ret; 241 return ret;
238} 242}
239 243
240PwMerror LibGCryptIf::hash(unsigned char **outBuf, 244PwMerror LibGCryptIf::hash(unsigned char **outBuf,
241 size_t *outBufLen, 245 size_t *outBufLen,
242 const unsigned char *inBuf, 246 const unsigned char *inBuf,
243 size_t inBufLen, 247 size_t inBufLen,
244 char _algo) 248 char _algo)
245{ 249{
246 PwMerror ret = e_success; 250 PwMerror ret = e_success;
247 unsigned int hashLen; 251 unsigned int hashLen;
248 int algo = mapHashId(_algo); 252 int algo = mapHashId(_algo);
249 253
250 hashLen = gcry_md_get_algo_dlen(algo); 254 hashLen = gcry_md_get_algo_dlen(algo);
251 *outBufLen = hashLen; 255 *outBufLen = hashLen;
252 *outBuf = new unsigned char[*outBufLen]; 256 *outBuf = new unsigned char[*outBufLen];
253 gcry_md_hash_buffer(algo, 257 gcry_md_hash_buffer(algo,
254 *outBuf, 258 *outBuf,
255 inBuf, 259 inBuf,
256 inBufLen); 260 inBufLen);
257 return ret; 261 return ret;
258} 262}
259 263
260unsigned int LibGCryptIf::hashLength(char _algo) 264unsigned int LibGCryptIf::hashLength(char _algo)
261{ 265{
262 unsigned int ret; 266 unsigned int ret;
263 int algo = mapHashId(_algo); 267 int algo = mapHashId(_algo);
264 ret = gcry_md_get_algo_dlen(algo); 268 ret = gcry_md_get_algo_dlen(algo);
265 return ret; 269 return ret;
266} 270}
267 271
268int LibGCryptIf::mapCipherId(char algo) 272int LibGCryptIf::mapCipherId(char algo)
269{ 273{
270 switch (algo) { 274 switch (algo) {
271 case PWM_CRYPT_AES128: 275 case PWM_CRYPT_AES128:
272 return GCRY_CIPHER_AES; 276 return GCRY_CIPHER_AES;
273 case PWM_CRYPT_AES192: 277 case PWM_CRYPT_AES192:
274 return GCRY_CIPHER_AES192; 278 return GCRY_CIPHER_AES192;
275 case PWM_CRYPT_AES256: 279 case PWM_CRYPT_AES256:
276 return GCRY_CIPHER_AES256; 280 return GCRY_CIPHER_AES256;
277 case PWM_CRYPT_3DES: 281 case PWM_CRYPT_3DES:
278 return GCRY_CIPHER_3DES; 282 return GCRY_CIPHER_3DES;
279 case PWM_CRYPT_TWOFISH: 283 case PWM_CRYPT_TWOFISH:
280 return GCRY_CIPHER_TWOFISH; 284 return GCRY_CIPHER_TWOFISH;
281 case PWM_CRYPT_TWOFISH128: 285 case PWM_CRYPT_TWOFISH128:
282 return GCRY_CIPHER_TWOFISH128; 286 return GCRY_CIPHER_TWOFISH128;
283 default: 287 default:
284 BUG(); 288 BUG();
285 } 289 }
286 return GCRY_CIPHER_NONE; 290 return GCRY_CIPHER_NONE;
287} 291}
288 292
289int LibGCryptIf::mapHashId(char algo) 293int LibGCryptIf::mapHashId(char algo)
290{ 294{
291 switch (algo) { 295 switch (algo) {
292 case PWM_HASH_SHA1: 296 case PWM_HASH_SHA1:
293 return GCRY_MD_SHA1; 297 return GCRY_MD_SHA1;
294 case PWM_HASH_SHA256: 298 case PWM_HASH_SHA256:
295 return GCRY_MD_SHA256; 299 return GCRY_MD_SHA256;
296 case PWM_HASH_SHA384: 300 case PWM_HASH_SHA384:
297 return GCRY_MD_SHA384; 301 return GCRY_MD_SHA384;
298 case PWM_HASH_SHA512: 302 case PWM_HASH_SHA512:
299 return GCRY_MD_SHA512; 303 return GCRY_MD_SHA512;
300 case PWM_HASH_MD5: 304 case PWM_HASH_MD5:
301 return GCRY_MD_MD5; 305 return GCRY_MD_MD5;
302 case PWM_HASH_RMD160: 306 case PWM_HASH_RMD160:
303 return GCRY_MD_RMD160; 307 return GCRY_MD_RMD160;
304 case PWM_HASH_TIGER: 308 case PWM_HASH_TIGER:
305 return GCRY_MD_TIGER; 309 return GCRY_MD_TIGER;
306 default: 310 default:
307 BUG(); 311 BUG();
308 } 312 }
309 return GCRY_MD_NONE; 313 return GCRY_MD_NONE;
310} 314}
311 315
312bool LibGCryptIf::hashPassphrase(const unsigned char *pw, 316bool LibGCryptIf::hashPassphrase(const unsigned char *pw,
313 size_t pwlen, 317 size_t pwlen,
314 unsigned char *salt, 318 unsigned char *salt,
315 unsigned char *key, 319 unsigned char *key,
316 size_t keylen, 320 size_t keylen,
317 bool create) 321 bool create,
322 char _hashalgo //US BUG: pass _hashalgo because we need it in hashPassphrase
323)
318{ 324{
319 DEK dek; 325 DEK dek;
320 STRING2KEY s2k; 326 STRING2KEY s2k;
321 bool ret; 327 bool ret;
322 328
323 dek.keylen = keylen; 329 dek.keylen = keylen;
324 s2k.mode = 1; 330 s2k.mode = 1;
325 s2k.hash_algo = mapHashId(conf()->confGlobHashAlgo()); 331 //US bug: do not use the global hash algo here. Use the passed ago instead. The hashalgo stored in the file can
332 // be different from the one in the configuration.
333 s2k.hash_algo = mapHashId(_hashalgo //conf()->confGlobHashAlgo()
334 );
326 s2k.count = 0; 335 s2k.count = 0;
327 if (!create) 336 if (!create)
328 memcpy(s2k.salt, salt, STRING2KEY_SALTLEN); 337 memcpy(s2k.salt, salt, STRING2KEY_SALTLEN);
329 ret = doHashPassphrase(&dek, 338 ret = doHashPassphrase(&dek,
330 pw, 339 pw,
331 pwlen, 340 pwlen,
332 &s2k, 341 &s2k,
333 create); 342 create);
334 if (!ret) 343 if (!ret)
335 goto out; 344 goto out;
336 memcpy(key, dek.key, dek.keylen); 345 memcpy(key, dek.key, dek.keylen);
337 if (create) 346 if (create)
338 memcpy(salt, s2k.salt, STRING2KEY_SALTLEN); 347 memcpy(salt, s2k.salt, STRING2KEY_SALTLEN);
339out: 348out:
340 return ret; 349 return ret;
341} 350}
342 351
343 352
344bool LibGCryptIf::doHashPassphrase(DEK *dek, 353bool LibGCryptIf::doHashPassphrase(DEK *dek,
345 const unsigned char *pw, 354 const unsigned char *pw,
346 size_t pwlen, 355 size_t pwlen,
347 STRING2KEY *s2k, 356 STRING2KEY *s2k,
348 bool create) 357 bool create)
349{ 358{
350 // This function is derived from GnuPG-1.2.5-rc2 359 // This function is derived from GnuPG-1.2.5-rc2
351 gcry_md_hd_t md; 360 gcry_md_hd_t md;
352 gcry_error_t err; 361 gcry_error_t err;
353 bool ret = true; 362 bool ret = true;
354 size_t pass, i; 363 size_t pass, i;
355 size_t used = 0; 364 size_t used = 0;
356 365
357 PWM_ASSERT(s2k->hash_algo); 366 PWM_ASSERT(s2k->hash_algo);
358 BUG_ON(!(dek->keylen > 0 && dek->keylen <= array_size(dek->key))); 367 BUG_ON(!(dek->keylen > 0 && dek->keylen <= array_size(dek->key)));
359 368
360 err = gcry_md_open(&md, s2k->hash_algo, 0); 369 err = gcry_md_open(&md, s2k->hash_algo, 0);
361 if (err != GPG_ERR_NO_ERROR) { 370 if (err != GPG_ERR_NO_ERROR) {
362 ret = false; 371 ret = false;
363 goto out; 372 goto out;
364 } 373 }
365 for (pass = 0; used < dek->keylen; pass++) { 374 for (pass = 0; used < dek->keylen; pass++) {
366 if (pass) { 375 if (pass) {
367 gcry_md_reset(md); 376 gcry_md_reset(md);
368 for (i = 0; i < pass; i++) // preset the hash context 377 for (i = 0; i < pass; i++) // preset the hash context
369 gcry_md_putc(md, 0); 378 gcry_md_putc(md, 0);
370 } 379 }
371 if (s2k->mode == 1 || s2k->mode == 3) { 380 if (s2k->mode == 1 || s2k->mode == 3) {
372 size_t len2 = pwlen + 8; 381 size_t len2 = pwlen + 8;
373 size_t count = len2; 382 size_t count = len2;
374 383
375 if (create && !pass) { 384 if (create && !pass) {
376 Randomizer *rnd = Randomizer::obj(); 385 Randomizer *rnd = Randomizer::obj();
377 const unsigned int salt_len = 8; 386 const unsigned int salt_len = 8;
378 string rndBuf(rnd->genRndBuf(salt_len)); 387 string rndBuf(rnd->genRndBuf(salt_len));
379 memcpy(s2k->salt, rndBuf.c_str(), salt_len); 388 memcpy(s2k->salt, rndBuf.c_str(), salt_len);
380 if (s2k->mode == 3) 389 if (s2k->mode == 3)
381 s2k->count = 96; // 65536 iterations 390 s2k->count = 96; // 65536 iterations
382 } 391 }
383 if (s2k->mode == 3) { 392 if (s2k->mode == 3) {
384 count = (16ul + (s2k->count & 15)) << ((s2k->count >> 4) + 6); 393 count = (16ul + (s2k->count & 15)) << ((s2k->count >> 4) + 6);
385 if (count < len2) 394 if (count < len2)
386 count = len2; 395 count = len2;
387 } 396 }
388 // a little bit complicated because we need a ulong for count 397 // a little bit complicated because we need a ulong for count
389 while (count > len2) { // maybe iterated+salted 398 while (count > len2) { // maybe iterated+salted
390 gcry_md_write(md, s2k->salt, 8); 399 gcry_md_write(md, s2k->salt, 8);
391 gcry_md_write(md, pw, pwlen); 400 gcry_md_write(md, pw, pwlen);
392 count -= len2; 401 count -= len2;
393 } 402 }
394 if (count < 8) { 403 if (count < 8) {
395 gcry_md_write(md, s2k->salt, count); 404 gcry_md_write(md, s2k->salt, count);
396 } else { 405 } else {
397 gcry_md_write(md, s2k->salt, 8); 406 gcry_md_write(md, s2k->salt, 8);
398 count -= 8; 407 count -= 8;
399 gcry_md_write(md, pw, count); 408 gcry_md_write(md, pw, count);
400 } 409 }
401 } else 410 } else
402 gcry_md_write(md, pw, pwlen); 411 gcry_md_write(md, pw, pwlen);
403 gcry_md_final(md); 412 gcry_md_final(md);
404 i = gcry_md_get_algo_dlen(s2k->hash_algo); 413 i = gcry_md_get_algo_dlen(s2k->hash_algo);
405 if (i > dek->keylen - used) 414 if (i > dek->keylen - used)
406 i = dek->keylen - used; 415 i = dek->keylen - used;
407 memcpy(dek->key+used, gcry_md_read(md, s2k->hash_algo), i); 416 memcpy(dek->key+used, gcry_md_read(md, s2k->hash_algo), i);
408 used += i; 417 used += i;
409 } 418 }
410 gcry_md_close(md); 419 gcry_md_close(md);
411out: 420out:
412 return ret; 421 return ret;
413} 422}
414 423
415void LibGCryptIf::padData(unsigned char *buf, 424void LibGCryptIf::padData(unsigned char *buf,
416 size_t bufLen, 425 size_t bufLen,
417 size_t boundary) 426 size_t boundary)
418{ 427{
419 size_t numPadBytes = boundary - ((bufLen + 1) % boundary); 428 size_t numPadBytes = boundary - ((bufLen + 1) % boundary);
420 buf[bufLen] = static_cast<char>(0x01); 429 buf[bufLen] = static_cast<char>(0x01);
421 size_t i = 0; 430 size_t i = 0;
422 Randomizer *rnd = Randomizer::obj(); 431 Randomizer *rnd = Randomizer::obj();
423 char c; 432 char c;
424 unsigned char *b; 433 unsigned char *b;
425 while (i < numPadBytes) { 434 while (i < numPadBytes) {
426 c = rnd->genRndChar(); 435 c = rnd->genRndChar();
427 if (c == static_cast<char>(0x01)) 436 if (c == static_cast<char>(0x01))
428 continue; 437 continue;
429 b = buf + bufLen + 1 + i; 438 b = buf + bufLen + 1 + i;
430 *b = c; 439 *b = c;
431 ++i; 440 ++i;
432 } 441 }
433} 442}
434 443
435void LibGCryptIf::unpadData(const unsigned char *buf, 444void LibGCryptIf::unpadData(const unsigned char *buf,
436 size_t *bufLen) 445 size_t *bufLen)
437{ 446{
438 size_t pos; 447 size_t pos;
439 BUG_ON(*bufLen % 8); 448 BUG_ON(*bufLen % 8);
440 pos = *bufLen - 1; 449 pos = *bufLen - 1;
441 while (buf[pos] != static_cast<char>(0x01)) { 450 while (buf[pos] != static_cast<char>(0x01)) {
442 qDebug("pos %d %d %d", pos, buf[pos], static_cast<char>(0x01) ); 451 //qDebug("pos %d %d %d", pos, buf[pos], static_cast<char>(0x01) );
443 BUG_ON(!pos); 452 BUG_ON(!pos);
444 //LR BUG we should terminte the loop if p == 0 453 //LR BUG we should terminte the loop if p == 0
445 if ( pos == 0 ) 454 if ( pos == 0 )
446 break; 455 break;
447 --pos; 456 --pos;
448 } 457 }
449 *bufLen = pos; 458 *bufLen = pos;
450 qDebug("ente "); 459 //qDebug("ente ");
451} 460}
452 461
453#endif // CONFIG_PWMANAGER_GCRY 462#endif // CONFIG_PWMANAGER_GCRY
454 463
diff --git a/pwmanager/pwmanager/libgcryptif.h b/pwmanager/pwmanager/libgcryptif.h
index 1a7b658..9a987a2 100644
--- a/pwmanager/pwmanager/libgcryptif.h
+++ b/pwmanager/pwmanager/libgcryptif.h
@@ -1,171 +1,177 @@
1/*************************************************************************** 1/***************************************************************************
2 * * 2 * *
3 * copyright (C) 2004 by Michael Buesch * 3 * copyright (C) 2004 by Michael Buesch *
4 * email: mbuesch@freenet.de * 4 * email: mbuesch@freenet.de *
5 * * 5 * *
6 * hashPassphrase() is derived from GnuPG and is * 6 * hashPassphrase() is derived from GnuPG and is *
7 * Copyright (C) 1998, 1999, 2000, 2001, 2003 * 7 * Copyright (C) 1998, 1999, 2000, 2001, 2003 *
8 * Free Software Foundation, Inc. * 8 * Free Software Foundation, Inc. *
9 * * 9 * *
10 * This program is free software; you can redistribute it and/or modify * 10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License version 2 * 11 * it under the terms of the GNU General Public License version 2 *
12 * as published by the Free Software Foundation. * 12 * as published by the Free Software Foundation. *
13 * * 13 * *
14 ***************************************************************************/ 14 ***************************************************************************/
15 15
16/*************************************************************************** 16/***************************************************************************
17 * copyright (C) 2004 by Ulf Schenk 17 * copyright (C) 2004 by Ulf Schenk
18 * This file is originaly based on version 1.1 of pwmanager 18 * This file is originaly based on version 1.1 of pwmanager
19 * and was modified to run on embedded devices that run microkde 19 * and was modified to run on embedded devices that run microkde
20 * 20 *
21 * $Id$ 21 * $Id$
22 **************************************************************************/ 22 **************************************************************************/
23 23
24#ifndef __LIBGCRYPTIF_H 24#ifndef __LIBGCRYPTIF_H
25#define __LIBGCRYPTIF_H 25#define __LIBGCRYPTIF_H
26 26
27#include "pwmexception.h" 27#include "pwmexception.h"
28 28
29//#undef CONFIG_PWMANAGER_GCRY // for debugging only. 29//#undef CONFIG_PWMANAGER_GCRY // for debugging only.
30#ifdef CONFIG_PWMANAGER_GCRY 30#ifdef CONFIG_PWMANAGER_GCRY
31 31
32#include <stddef.h> 32#include <stddef.h>
33#include <sys/types.h> 33#include <sys/types.h>
34#ifndef _WIN32_ 34#ifndef _WIN32_
35#include <stdint.h> 35#include <stdint.h>
36#else 36#else
37#define uint8_t Q_UINT8 37#define uint8_t Q_UINT8
38#define byte Q_UINT8 38#define byte Q_UINT8
39#define uint32_t Q_UINT32 39#define uint32_t Q_UINT32
40#endif 40#endif
41 #define STRING2KEY_SALTLEN8 41 #define STRING2KEY_SALTLEN8
42 42
43/** interface class for the libgcrypt cipher and hash algorithms 43/** interface class for the libgcrypt cipher and hash algorithms
44 * NOTE: Always allocate 1024 extra bytes for the inBuf (for padding) 44 * NOTE: Always allocate 1024 extra bytes for the inBuf (for padding)
45 */ 45 */
46class LibGCryptIf 46class LibGCryptIf
47{ 47{
48protected: 48protected:
49 struct STRING2KEY 49 struct STRING2KEY
50 { 50 {
51 int mode; 51 int mode;
52 int hash_algo; 52 int hash_algo;
53 uint8_t salt[STRING2KEY_SALTLEN]; 53 uint8_t salt[STRING2KEY_SALTLEN];
54 uint32_t count; 54 uint32_t count;
55 }; 55 };
56 struct DEK 56 struct DEK
57 { 57 {
58 size_t keylen; 58 size_t keylen;
59 uint8_t key[32]; // this is the largest used keylen (256 bit) 59 uint8_t key[32]; // this is the largest used keylen (256 bit)
60 }; 60 };
61 61
62public: 62public:
63 LibGCryptIf() { } 63 LibGCryptIf() { }
64 /** is libgcrypt available? */ 64 /** is libgcrypt available? */
65 static bool available() 65 static bool available()
66 { return true; } 66 { return true; }
67 /** encrypt data. _algo is the PWM_CRYPT_* ID 67 /** encrypt data. _algo is the PWM_CRYPT_* ID
68 * of the algorithm. 68 * of the algorithm.
69 */ 69 */
70 PwMerror encrypt(unsigned char **outBuf, 70 PwMerror encrypt(unsigned char **outBuf,
71 size_t *outBufLen, 71 size_t *outBufLen,
72 unsigned char *inBuf, 72 unsigned char *inBuf,
73 size_t inBufLen, 73 size_t inBufLen,
74 const unsigned char *key, 74 const unsigned char *key,
75 size_t keylen, 75 size_t keylen,
76 char _algo); 76 char _algo,
77 char _hashalgo //US BUG: pass _hashalgo because we need it in hashPassphrase
78);
77 /** decrypt data. _algo is the PWM_CRYPT_* ID 79 /** decrypt data. _algo is the PWM_CRYPT_* ID
78 * of the algorithm. 80 * of the algorithm.
79 */ 81 */
80 PwMerror decrypt(unsigned char **outBuf, 82 PwMerror decrypt(unsigned char **outBuf,
81 size_t *outBufLen, 83 size_t *outBufLen,
82 const unsigned char *inBuf, 84 const unsigned char *inBuf,
83 size_t inBufLen, 85 size_t inBufLen,
84 const unsigned char *key, 86 const unsigned char *key,
85 size_t keylen, 87 size_t keylen,
86 char _algo); 88 char _algo,
89 char _hashalgo //US BUG: pass _hashalgo because we need it in hashPassphrase
90);
87 /** hash data. _algo is the PWM_HASH_* ID of the hash */ 91 /** hash data. _algo is the PWM_HASH_* ID of the hash */
88 PwMerror hash(unsigned char **outBuf, 92 PwMerror hash(unsigned char **outBuf,
89 size_t *outBufLen, 93 size_t *outBufLen,
90 const unsigned char *inBuf, 94 const unsigned char *inBuf,
91 size_t inBufLen, 95 size_t inBufLen,
92 char _algo); 96 char _algo);
93 /** returns the length of the hash. _algo is the PWM_HASH_* 97 /** returns the length of the hash. _algo is the PWM_HASH_*
94 * id of the hash. returns 0 on error. 98 * id of the hash. returns 0 on error.
95 */ 99 */
96 unsigned int hashLength(char _algo); 100 unsigned int hashLength(char _algo);
97 101
98protected: 102protected:
99 /** returns the total buffer length */ 103 /** returns the total buffer length */
100 size_t getBufLen(size_t inBufLen, size_t boundary) 104 size_t getBufLen(size_t inBufLen, size_t boundary)
101 { 105 {
102 return ((boundary - (inBufLen % boundary)) + inBufLen); 106 return ((boundary - (inBufLen % boundary)) + inBufLen);
103 } 107 }
104 /** pad the data up to the given boundary. 108 /** pad the data up to the given boundary.
105 * "buf" has to be big enough! 109 * "buf" has to be big enough!
106 */ 110 */
107 void padData(unsigned char *buf, 111 void padData(unsigned char *buf,
108 size_t bufLen, 112 size_t bufLen,
109 size_t boundary); 113 size_t boundary);
110 /** unpad the data */ 114 /** unpad the data */
111 void unpadData(const unsigned char *buf, 115 void unpadData(const unsigned char *buf,
112 size_t *bufLen); 116 size_t *bufLen);
113 /** maps the PWM_CRYPT_* ID of an algorithm 117 /** maps the PWM_CRYPT_* ID of an algorithm
114 * to the libgcrypt GCRY_CIPHER_* ID 118 * to the libgcrypt GCRY_CIPHER_* ID
115 */ 119 */
116 int mapCipherId(char algo); 120 int mapCipherId(char algo);
117 /** maps the PWM_HASH_* ID of an algorithm 121 /** maps the PWM_HASH_* ID of an algorithm
118 * to the libgcrypt GCRY_MD_* ID 122 * to the libgcrypt GCRY_MD_* ID
119 */ 123 */
120 int mapHashId(char algo); 124 int mapHashId(char algo);
121 /** hash a passphrase to a cipher key */ 125 /** hash a passphrase to a cipher key */
122 bool hashPassphrase(const unsigned char *pw, 126 bool hashPassphrase(const unsigned char *pw,
123 size_t pwlen, 127 size_t pwlen,
124 unsigned char *salt, 128 unsigned char *salt,
125 unsigned char *key, 129 unsigned char *key,
126 size_t keylen, 130 size_t keylen,
127 bool create); 131 bool create,
132 char _hashalgo //US BUG: pass _hashalgo because we need it in hashPassphrase
133);
128 /** hash a passphrase to a cipher key */ 134 /** hash a passphrase to a cipher key */
129 bool doHashPassphrase(DEK *dek, 135 bool doHashPassphrase(DEK *dek,
130 const unsigned char *pw, 136 const unsigned char *pw,
131 size_t pwlen, 137 size_t pwlen,
132 STRING2KEY *s2k, 138 STRING2KEY *s2k,
133 bool create); 139 bool create);
134}; 140};
135 141
136 142
137#else // CONFIG_PWMANAGER_GCRY 143#else // CONFIG_PWMANAGER_GCRY
138/** libgcrypt is not installed. This is a NOP wrapper. */ 144/** libgcrypt is not installed. This is a NOP wrapper. */
139class LibGCryptIf 145class LibGCryptIf
140{ 146{
141public: 147public:
142 LibGCryptIf() { } 148 LibGCryptIf() { }
143 static bool available() 149 static bool available()
144 { return false; } 150 { return false; }
145 PwMerror encrypt(unsigned char **, 151 PwMerror encrypt(unsigned char **,
146 size_t *, 152 size_t *,
147 unsigned char *, 153 unsigned char *,
148 size_t, 154 size_t,
149 const unsigned char *, 155 const unsigned char *,
150 size_t, 156 size_t,
151 char) 157 char)
152 { return e_cryptNotImpl; } 158 { return e_cryptNotImpl; }
153 PwMerror decrypt(unsigned char **, 159 PwMerror decrypt(unsigned char **,
154 size_t *, 160 size_t *,
155 const unsigned char *, 161 const unsigned char *,
156 size_t, 162 size_t,
157 const unsigned char *, 163 const unsigned char *,
158 size_t, 164 size_t,
159 char) 165 char)
160 { return e_cryptNotImpl; } 166 { return e_cryptNotImpl; }
161 PwMerror hash(unsigned char **, 167 PwMerror hash(unsigned char **,
162 size_t *, 168 size_t *,
163 const unsigned char *, 169 const unsigned char *,
164 size_t, 170 size_t,
165 char) 171 char)
166 { return e_hashNotImpl; } 172 { return e_hashNotImpl; }
167 unsigned int hashLength(char) 173 unsigned int hashLength(char)
168 { return 0; } 174 { return 0; }
169}; 175};
170#endif // CONFIG_PWMANAGER_GCRY 176#endif // CONFIG_PWMANAGER_GCRY
171#endif // __LIBGCRYPTIF_H 177#endif // __LIBGCRYPTIF_H
diff --git a/pwmanager/pwmanager/pwmdoc.cpp b/pwmanager/pwmanager/pwmdoc.cpp
index 17cb74a..a740d6d 100644
--- a/pwmanager/pwmanager/pwmdoc.cpp
+++ b/pwmanager/pwmanager/pwmdoc.cpp
@@ -1,3474 +1,3482 @@
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 <libkcal/syncdefines.h> 38#include <libkcal/syncdefines.h>
39 39
40 40
41#ifdef CONFIG_KWALLETIF 41#ifdef CONFIG_KWALLETIF
42# include "kwalletemu.h" 42# include "kwalletemu.h"
43#endif // CONFIG_KWALLETIF 43#endif // CONFIG_KWALLETIF
44 44
45#include <qdatetime.h> 45#include <qdatetime.h>
46#include <qsize.h> 46#include <qsize.h>
47#include <qfileinfo.h> 47#include <qfileinfo.h>
48#include <qfile.h> 48#include <qfile.h>
49 49
50#include <stdio.h> 50#include <stdio.h>
51#include <stdlib.h> 51#include <stdlib.h>
52#include <errno.h> 52#include <errno.h>
53#include <string.h> 53#include <string.h>
54//US#include <iostream> 54//US#include <iostream>
55#include <algorithm> 55#include <algorithm>
56#include <sys/types.h> 56#include <sys/types.h>
57#include <sys/stat.h> 57#include <sys/stat.h>
58#ifndef _WIN32_ 58#ifndef _WIN32_
59#include <unistd.h> 59#include <unistd.h>
60#include <stdint.h> 60#include <stdint.h>
61#endif 61#endif
62 62
63#ifdef PWM_EMBEDDED 63#ifdef PWM_EMBEDDED
64#ifndef Q_LONG 64#ifndef Q_LONG
65#define Q_LONG long 65#define Q_LONG long
66#endif 66#endif
67 67
68#ifndef Q_ULONG 68#ifndef Q_ULONG
69#define Q_ULONG unsigned long 69#define Q_ULONG unsigned long
70#endif 70#endif
71#endif //PWM_EMBEDDED 71#endif //PWM_EMBEDDED
72 72
73 73
74//TODO: reset to its normal value. 74//TODO: reset to its normal value.
75 #define META_CHECK_TIMER_INTERVAL10/*300*/ /* sek */ 75 #define META_CHECK_TIMER_INTERVAL10/*300*/ /* sek */
76 76
77using namespace std; 77using namespace std;
78 78
79 79
80void PwMDocList::add(PwMDoc *doc, const string &id) 80void PwMDocList::add(PwMDoc *doc, const string &id)
81{ 81{
82#ifdef PWM_DEBUG 82#ifdef PWM_DEBUG
83 // check for existance of object in debug mode only. 83 // check for existance of object in debug mode only.
84 vector<listItem>::iterator begin = docList.begin(), 84 vector<listItem>::iterator begin = docList.begin(),
85 end = docList.end(), 85 end = docList.end(),
86 i = begin; 86 i = begin;
87 while (i != end) { 87 while (i != end) {
88 if (i->doc == doc) { 88 if (i->doc == doc) {
89 BUG(); 89 BUG();
90 return; 90 return;
91 } 91 }
92 ++i; 92 ++i;
93 } 93 }
94#endif 94#endif
95 listItem newItem; 95 listItem newItem;
96 newItem.doc = doc; 96 newItem.doc = doc;
97 newItem.docId = id; 97 newItem.docId = id;
98 docList.push_back(newItem); 98 docList.push_back(newItem);
99} 99}
100 100
101void PwMDocList::edit(PwMDoc *doc, const string &newId) 101void PwMDocList::edit(PwMDoc *doc, const string &newId)
102{ 102{
103 vector<listItem>::iterator begin = docList.begin(), 103 vector<listItem>::iterator begin = docList.begin(),
104 end = docList.end(), 104 end = docList.end(),
105 i = begin; 105 i = begin;
106 while (i != end) { 106 while (i != end) {
107 if (i->doc == doc) { 107 if (i->doc == doc) {
108 i->docId = newId; 108 i->docId = newId;
109 return; 109 return;
110 } 110 }
111 ++i; 111 ++i;
112 } 112 }
113} 113}
114 114
115void PwMDocList::del(PwMDoc *doc) 115void PwMDocList::del(PwMDoc *doc)
116{ 116{
117 vector<listItem>::iterator begin = docList.begin(), 117 vector<listItem>::iterator begin = docList.begin(),
118 end = docList.end(), 118 end = docList.end(),
119 i = begin; 119 i = begin;
120 while (i != end) { 120 while (i != end) {
121 if (i->doc == doc) { 121 if (i->doc == doc) {
122 docList.erase(i); 122 docList.erase(i);
123 return; 123 return;
124 } 124 }
125 ++i; 125 ++i;
126 } 126 }
127} 127}
128 128
129bool PwMDocList::find(const string &id, listItem *ret) 129bool PwMDocList::find(const string &id, listItem *ret)
130{ 130{
131 vector<listItem>::iterator begin = docList.begin(), 131 vector<listItem>::iterator begin = docList.begin(),
132 end = docList.end(), 132 end = docList.end(),
133 i = begin; 133 i = begin;
134 while (i != end) { 134 while (i != end) {
135 if (i->docId == id) { 135 if (i->docId == id) {
136 if (ret) 136 if (ret)
137 *ret = *i; 137 *ret = *i;
138 return true; 138 return true;
139 } 139 }
140 ++i; 140 ++i;
141 } 141 }
142 return false; 142 return false;
143} 143}
144 144
145 145
146 146
147DocTimer::DocTimer(PwMDoc *_doc) 147DocTimer::DocTimer(PwMDoc *_doc)
148 : doc (_doc) 148 : doc (_doc)
149 , mpwLock (0) 149 , mpwLock (0)
150 , autoLockLock (0) 150 , autoLockLock (0)
151 , metaCheckLock (0) 151 , metaCheckLock (0)
152{ 152{
153 mpwTimer = new QTimer; 153 mpwTimer = new QTimer;
154 autoLockTimer = new QTimer; 154 autoLockTimer = new QTimer;
155 metaCheckTimer = new QTimer; 155 metaCheckTimer = new QTimer;
156 connect(mpwTimer, SIGNAL(timeout()), 156 connect(mpwTimer, SIGNAL(timeout()),
157 this, SLOT(mpwTimeout())); 157 this, SLOT(mpwTimeout()));
158 connect(autoLockTimer, SIGNAL(timeout()), 158 connect(autoLockTimer, SIGNAL(timeout()),
159 this, SLOT(autoLockTimeout())); 159 this, SLOT(autoLockTimeout()));
160 connect(metaCheckTimer, SIGNAL(timeout()), 160 connect(metaCheckTimer, SIGNAL(timeout()),
161 this, SLOT(metaCheckTimeout())); 161 this, SLOT(metaCheckTimeout()));
162} 162}
163 163
164DocTimer::~DocTimer() 164DocTimer::~DocTimer()
165{ 165{
166 delete mpwTimer; 166 delete mpwTimer;
167 delete autoLockTimer; 167 delete autoLockTimer;
168 delete metaCheckTimer; 168 delete metaCheckTimer;
169} 169}
170 170
171void DocTimer::start(TimerIDs timer) 171void DocTimer::start(TimerIDs timer)
172{ 172{
173 switch (timer) { 173 switch (timer) {
174 case id_mpwTimer: 174 case id_mpwTimer:
175 if (mpwTimer->isActive()) 175 if (mpwTimer->isActive())
176 mpwTimer->stop(); 176 mpwTimer->stop();
177 doc->setDocStatFlag(DOC_STAT_UNLOCK_WITHOUT_PW); 177 doc->setDocStatFlag(DOC_STAT_UNLOCK_WITHOUT_PW);
178 mpwTimer->start(conf()->confGlobPwTimeout() * 1000, true); 178 mpwTimer->start(conf()->confGlobPwTimeout() * 1000, true);
179 break; 179 break;
180 case id_autoLockTimer: 180 case id_autoLockTimer:
181 if (autoLockTimer->isActive()) 181 if (autoLockTimer->isActive())
182 autoLockTimer->stop(); 182 autoLockTimer->stop();
183 if (conf()->confGlobLockTimeout() > 0) 183 if (conf()->confGlobLockTimeout() > 0)
184 autoLockTimer->start(conf()->confGlobLockTimeout() * 1000, true); 184 autoLockTimer->start(conf()->confGlobLockTimeout() * 1000, true);
185 break; 185 break;
186 case id_metaCheckTimer: 186 case id_metaCheckTimer:
187 if (metaCheckTimer->isActive()) 187 if (metaCheckTimer->isActive())
188 metaCheckTimer->stop(); 188 metaCheckTimer->stop();
189 metaCheckTimer->start(META_CHECK_TIMER_INTERVAL * 1000, true); 189 metaCheckTimer->start(META_CHECK_TIMER_INTERVAL * 1000, true);
190 break; 190 break;
191 } 191 }
192} 192}
193 193
194void DocTimer::stop(TimerIDs timer) 194void DocTimer::stop(TimerIDs timer)
195{ 195{
196 switch (timer) { 196 switch (timer) {
197 case id_mpwTimer: 197 case id_mpwTimer:
198 mpwTimer->stop(); 198 mpwTimer->stop();
199 break; 199 break;
200 case id_autoLockTimer: 200 case id_autoLockTimer:
201 autoLockTimer->stop(); 201 autoLockTimer->stop();
202 break; 202 break;
203 case id_metaCheckTimer: 203 case id_metaCheckTimer:
204 metaCheckTimer->stop(); 204 metaCheckTimer->stop();
205 break; 205 break;
206 } 206 }
207} 207}
208 208
209void DocTimer::getLock(TimerIDs timer) 209void DocTimer::getLock(TimerIDs timer)
210{ 210{
211 switch (timer) { 211 switch (timer) {
212 case id_mpwTimer: 212 case id_mpwTimer:
213 ++mpwLock; 213 ++mpwLock;
214 break; 214 break;
215 case id_autoLockTimer: 215 case id_autoLockTimer:
216 ++autoLockLock; 216 ++autoLockLock;
217 break; 217 break;
218 case id_metaCheckTimer: 218 case id_metaCheckTimer:
219 ++metaCheckLock; 219 ++metaCheckLock;
220 break; 220 break;
221 } 221 }
222} 222}
223 223
224void DocTimer::putLock(TimerIDs timer) 224void DocTimer::putLock(TimerIDs timer)
225{ 225{
226 switch (timer) { 226 switch (timer) {
227 case id_mpwTimer: 227 case id_mpwTimer:
228 if (mpwLock) 228 if (mpwLock)
229 --mpwLock; 229 --mpwLock;
230 break; 230 break;
231 case id_autoLockTimer: 231 case id_autoLockTimer:
232 if (autoLockLock) 232 if (autoLockLock)
233 --autoLockLock; 233 --autoLockLock;
234 break; 234 break;
235 case id_metaCheckTimer: 235 case id_metaCheckTimer:
236 if (metaCheckLock) 236 if (metaCheckLock)
237 --metaCheckLock; 237 --metaCheckLock;
238 break; 238 break;
239 } 239 }
240} 240}
241 241
242void DocTimer::mpwTimeout() 242void DocTimer::mpwTimeout()
243{ 243{
244 if (mpwLock) { 244 if (mpwLock) {
245 mpwTimer->start(1000, true); 245 mpwTimer->start(1000, true);
246 return; 246 return;
247 } 247 }
248 doc->unsetDocStatFlag(DOC_STAT_UNLOCK_WITHOUT_PW); 248 doc->unsetDocStatFlag(DOC_STAT_UNLOCK_WITHOUT_PW);
249} 249}
250 250
251void DocTimer::autoLockTimeout() 251void DocTimer::autoLockTimeout()
252{ 252{
253 if (autoLockLock) { 253 if (autoLockLock) {
254 autoLockTimer->start(1000, true); 254 autoLockTimer->start(1000, true);
255 return; 255 return;
256 } 256 }
257 if (conf()->confGlobAutoDeepLock() && 257 if (conf()->confGlobAutoDeepLock() &&
258 doc->filename != QString::null && 258 doc->filename != QString::null &&
259 doc->filename != "") { 259 doc->filename != "") {
260 doc->deepLock(true); 260 doc->deepLock(true);
261 } else { 261 } else {
262 doc->lockAll(true); 262 doc->lockAll(true);
263 } 263 }
264} 264}
265 265
266void DocTimer::metaCheckTimeout() 266void DocTimer::metaCheckTimeout()
267{ 267{
268 if (metaCheckLock) { 268 if (metaCheckLock) {
269 // check again in one second. 269 // check again in one second.
270 metaCheckTimer->start(1000, true); 270 metaCheckTimer->start(1000, true);
271 return; 271 return;
272 } 272 }
273 if (doc->isDeepLocked()) { 273 if (doc->isDeepLocked()) {
274 metaCheckTimer->start(META_CHECK_TIMER_INTERVAL * 1000, true); 274 metaCheckTimer->start(META_CHECK_TIMER_INTERVAL * 1000, true);
275 return; 275 return;
276 } 276 }
277 if (doc->isDocEmpty()) { 277 if (doc->isDocEmpty()) {
278 metaCheckTimer->start(META_CHECK_TIMER_INTERVAL * 1000, true); 278 metaCheckTimer->start(META_CHECK_TIMER_INTERVAL * 1000, true);
279 return; 279 return;
280 } 280 }
281#ifdef CONFIG_KWALLETIF 281#ifdef CONFIG_KWALLETIF
282 KWalletEmu *kwlEmu = doc->init->kwalletEmu(); 282 KWalletEmu *kwlEmu = doc->init->kwalletEmu();
283 if (kwlEmu) 283 if (kwlEmu)
284 kwlEmu->suspendDocSignals(); 284 kwlEmu->suspendDocSignals();
285#endif // CONFIG_KWALLETIF 285#endif // CONFIG_KWALLETIF
286 /* We simply trigger all views to update their 286 /* We simply trigger all views to update their
287 * displayed values. This way they have a chance 287 * displayed values. This way they have a chance
288 * to get notified when some meta changes over time. 288 * to get notified when some meta changes over time.
289 * (for example an entry expired). 289 * (for example an entry expired).
290 * The _view_ is responsive for not updating its 290 * The _view_ is responsive for not updating its
291 * contents if nothing really changed! 291 * contents if nothing really changed!
292 */ 292 */
293 emit doc->dataChanged(doc); 293 emit doc->dataChanged(doc);
294#ifdef CONFIG_KWALLETIF 294#ifdef CONFIG_KWALLETIF
295 if (kwlEmu) 295 if (kwlEmu)
296 kwlEmu->resumeDocSignals(); 296 kwlEmu->resumeDocSignals();
297#endif // CONFIG_KWALLETIF 297#endif // CONFIG_KWALLETIF
298 metaCheckTimer->start(META_CHECK_TIMER_INTERVAL * 1000, true); 298 metaCheckTimer->start(META_CHECK_TIMER_INTERVAL * 1000, true);
299} 299}
300 300
301 301
302 302
303PwMDocList PwMDoc::openDocList; 303PwMDocList PwMDoc::openDocList;
304unsigned int PwMDocList::unnamedDocCnt = 1; 304unsigned int PwMDocList::unnamedDocCnt = 1;
305 305
306PwMDoc::PwMDoc(QObject *parent, const char *name) 306PwMDoc::PwMDoc(QObject *parent, const char *name)
307 : PwMDocUi(parent, name) 307 : PwMDocUi(parent, name)
308 , dataChangedLock (0) 308 , dataChangedLock (0)
309{ 309{
310 deleted = false; 310 deleted = false;
311 unnamedNum = 0; 311 unnamedNum = 0;
312 getOpenDocList()->add(this, getTitle().latin1()); 312 getOpenDocList()->add(this, getTitle().latin1());
313 curDocStat = 0; 313 curDocStat = 0;
314 setMaxNumEntries(); 314 setMaxNumEntries();
315 _timer = new DocTimer(this); 315 _timer = new DocTimer(this);
316 timer()->start(DocTimer::id_mpwTimer); 316 timer()->start(DocTimer::id_mpwTimer);
317 timer()->start(DocTimer::id_autoLockTimer); 317 timer()->start(DocTimer::id_autoLockTimer);
318 timer()->start(DocTimer::id_metaCheckTimer); 318 timer()->start(DocTimer::id_metaCheckTimer);
319 addCategory(DEFAULT_CATEGORY, 0, false); 319 addCategory(DEFAULT_CATEGORY, 0, false);
320 listView = 0; 320 listView = 0;
321 emit docCreated(this); 321 emit docCreated(this);
322} 322}
323 323
324PwMDoc::~PwMDoc() 324PwMDoc::~PwMDoc()
325{ 325{
326 emit docClosed(this); 326 emit docClosed(this);
327 getOpenDocList()->del(this); 327 getOpenDocList()->del(this);
328 delete _timer; 328 delete _timer;
329} 329}
330 330
331PwMerror PwMDoc::saveDoc(char compress, const QString *file) 331PwMerror PwMDoc::saveDoc(char compress, const QString *file)
332{ 332{
333 PwMerror ret, e; 333 PwMerror ret, e;
334 string serialized; 334 string serialized;
335 QFile f; 335 QFile f;
336 QString tmpFileMoved(QString::null); 336 QString tmpFileMoved(QString::null);
337 bool wasDeepLocked; 337 bool wasDeepLocked;
338 QString savedFilename(filename); 338 QString savedFilename(filename);
339 339
340 if (!file) { 340 if (!file) {
341 if (filename == "") 341 if (filename == "")
342 return e_filename; 342 return e_filename;
343 if (isDeepLocked()) { 343 if (isDeepLocked()) {
344 /* We don't need to save any data. 344 /* We don't need to save any data.
345 * It's already all on disk, because 345 * It's already all on disk, because
346 * we are deeplocked. 346 * we are deeplocked.
347 */ 347 */
348 unsetDocStatFlag(DOC_STAT_DISK_DIRTY); 348 unsetDocStatFlag(DOC_STAT_DISK_DIRTY);
349 ret = e_success; 349 ret = e_success;
350 return ret; 350 return ret;
351 } 351 }
352 } else { 352 } else {
353 if (*file == "" && filename == "") 353 if (*file == "" && filename == "")
354 return e_filename; 354 return e_filename;
355 if (*file != "") 355 if (*file != "")
356 filename = *file; 356 filename = *file;
357 } 357 }
358 358
359 wasDeepLocked = isDeepLocked(); 359 wasDeepLocked = isDeepLocked();
360 if (wasDeepLocked) { 360 if (wasDeepLocked) {
361 /* We are deeplocked. That means all data is already 361 /* We are deeplocked. That means all data is already
362 * on disk. BUT we need to do saving procedure, 362 * on disk. BUT we need to do saving procedure,
363 * because *file != savedFilename. 363 * because *file != savedFilename.
364 * Additionally we need to tempoarly restore 364 * Additionally we need to tempoarly restore
365 * the old "filename", because deepLock() references it. 365 * the old "filename", because deepLock() references it.
366 */ 366 */
367 QString newFilename(filename); 367 QString newFilename(filename);
368 filename = savedFilename; 368 filename = savedFilename;
369 getDataChangedLock(); 369 getDataChangedLock();
370 e = deepLock(false); 370 e = deepLock(false);
371 putDataChangedLock(); 371 putDataChangedLock();
372 filename = newFilename; 372 filename = newFilename;
373 switch (e) { 373 switch (e) {
374 case e_success: 374 case e_success:
375 break; 375 break;
376 case e_wrongPw: 376 case e_wrongPw:
377 case e_noPw: 377 case e_noPw:
378 emitDataChanged(this); 378 emitDataChanged(this);
379 return e; 379 return e;
380 default: 380 default:
381 emitDataChanged(this); 381 emitDataChanged(this);
382 return e_openFile; 382 return e_openFile;
383 } 383 }
384 } 384 }
385 385
386 if (!isPwAvailable()) { 386 if (!isPwAvailable()) {
387 /* password is not available. This means, the 387 /* password is not available. This means, the
388 * document wasn't saved, yet. 388 * document wasn't saved, yet.
389 */ 389 */
390 bool useChipcard = getDocStatFlag(DOC_STAT_USE_CHIPCARD); 390 bool useChipcard = getDocStatFlag(DOC_STAT_USE_CHIPCARD);
391 QString pw(requestNewMpw(&useChipcard)); 391 QString pw(requestNewMpw(&useChipcard));
392 if (pw != "") { 392 if (pw != "") {
393 currentPw = pw; 393 currentPw = pw;
394 } else { 394 } else {
395 return e_noPw; 395 return e_noPw;
396 } 396 }
397 if (useChipcard) { 397 if (useChipcard) {
398 setDocStatFlag(DOC_STAT_USE_CHIPCARD); 398 setDocStatFlag(DOC_STAT_USE_CHIPCARD);
399 } else { 399 } else {
400 unsetDocStatFlag(DOC_STAT_USE_CHIPCARD); 400 unsetDocStatFlag(DOC_STAT_USE_CHIPCARD);
401 } 401 }
402 } 402 }
403 403
404 int _cryptAlgo = conf()->confGlobCryptAlgo(); 404 int _cryptAlgo = conf()->confGlobCryptAlgo();
405 int _hashAlgo = conf()->confGlobHashAlgo(); 405 int _hashAlgo = conf()->confGlobHashAlgo();
406 406
407 // sanity check for the selected algorithms 407 // sanity check for the selected algorithms
408 if (_cryptAlgo < PWM_CRYPT_BLOWFISH || 408 if (_cryptAlgo < PWM_CRYPT_BLOWFISH ||
409 _cryptAlgo > PWM_CRYPT_TWOFISH128) { 409 _cryptAlgo > PWM_CRYPT_TWOFISH128) {
410 printWarn("Invalid Crypto-Algorithm selected! " 410 printWarn("Invalid Crypto-Algorithm selected! "
411 "Config-file seems to be corrupt. " 411 "Config-file seems to be corrupt. "
412 "Falling back to Blowfish."); 412 "Falling back to Blowfish.");
413 _cryptAlgo = PWM_CRYPT_BLOWFISH; 413 _cryptAlgo = PWM_CRYPT_BLOWFISH;
414 } 414 }
415 if (_hashAlgo < PWM_HASH_SHA1 || 415 if (_hashAlgo < PWM_HASH_SHA1 ||
416 _hashAlgo > PWM_HASH_TIGER) { 416 _hashAlgo > PWM_HASH_TIGER) {
417 printWarn("Invalid Hash-Algorithm selected! " 417 printWarn("Invalid Hash-Algorithm selected! "
418 "Config-file seems to be corrupt. " 418 "Config-file seems to be corrupt. "
419 "Falling back to SHA1."); 419 "Falling back to SHA1.");
420 _hashAlgo = PWM_HASH_SHA1; 420 _hashAlgo = PWM_HASH_SHA1;
421 } 421 }
422 char cryptAlgo = static_cast<char>(_cryptAlgo); 422 char cryptAlgo = static_cast<char>(_cryptAlgo);
423 char hashAlgo = static_cast<char>(_hashAlgo); 423 char hashAlgo = static_cast<char>(_hashAlgo);
424 424
425 if (conf()->confGlobMakeFileBackup()) { 425 if (conf()->confGlobMakeFileBackup()) {
426 if (!backupFile(filename)) 426 if (!backupFile(filename))
427 return e_fileBackup; 427 return e_fileBackup;
428 } 428 }
429 if (QFile::exists(filename)) { 429 if (QFile::exists(filename)) {
430 /* Move the existing file to some tmp file. 430 /* Move the existing file to some tmp file.
431 * When saving file succeeds, delete tmp file. Otherwise 431 * When saving file succeeds, delete tmp file. Otherwise
432 * move tmp file back. See below. 432 * move tmp file back. See below.
433 */ 433 */
434 Randomizer *rnd = Randomizer::obj(); 434 Randomizer *rnd = Randomizer::obj();
435 char rnd_buf[5]; 435 char rnd_buf[5];
436 sprintf(rnd_buf, "%X%X%X%X", rnd->genRndChar() & 0xFF, rnd->genRndChar() & 0xFF, 436 sprintf(rnd_buf, "%X%X%X%X", rnd->genRndChar() & 0xFF, rnd->genRndChar() & 0xFF,
437 rnd->genRndChar() & 0xFF, rnd->genRndChar() & 0xFF); 437 rnd->genRndChar() & 0xFF, rnd->genRndChar() & 0xFF);
438 tmpFileMoved = filename + "." + rnd_buf + ".mv"; 438 tmpFileMoved = filename + "." + rnd_buf + ".mv";
439 if (!copyFile(filename, tmpFileMoved)) 439 if (!copyFile(filename, tmpFileMoved))
440 return e_openFile; 440 return e_openFile;
441 if (!QFile::remove(filename)) { 441 if (!QFile::remove(filename)) {
442 printWarn(string("removing orig file ") 442 printWarn(string("removing orig file ")
443 + filename.latin1() 443 + filename.latin1()
444 + " failed!"); 444 + " failed!");
445 } 445 }
446 } 446 }
447 f.setName(filename); 447 f.setName(filename);
448 if (!f.open(IO_ReadWrite)) { 448 if (!f.open(IO_ReadWrite)) {
449 ret = e_openFile; 449 ret = e_openFile;
450 goto out_moveback; 450 goto out_moveback;
451 } 451 }
452 e = writeFileHeader(hashAlgo, hashAlgo, 452 e = writeFileHeader(hashAlgo, hashAlgo,
453 cryptAlgo, compress, 453 cryptAlgo, compress,
454 &currentPw, &f); 454 &currentPw, &f);
455 if (e == e_hashNotImpl) { 455 if (e == e_hashNotImpl) {
456 printDebug("PwMDoc::saveDoc(): writeFileHeader() failed: e_hashNotImpl"); 456 printDebug("PwMDoc::saveDoc(): writeFileHeader() failed: e_hashNotImpl");
457 f.close(); 457 f.close();
458 ret = e_hashNotImpl; 458 ret = e_hashNotImpl;
459 goto out_moveback; 459 goto out_moveback;
460 } else if (e != e_success) { 460 } else if (e != e_success) {
461 printDebug("PwMDoc::saveDoc(): writeFileHeader() failed"); 461 printDebug("PwMDoc::saveDoc(): writeFileHeader() failed");
462 f.close(); 462 f.close();
463 ret = e_writeHeader; 463 ret = e_writeHeader;
464 goto out_moveback; 464 goto out_moveback;
465 } 465 }
466 if (!serializeDta(&serialized)) { 466 if (!serializeDta(&serialized)) {
467 printDebug("PwMDoc::saveDoc(): serializeDta() failed"); 467 printDebug("PwMDoc::saveDoc(): serializeDta() failed");
468 f.close(); 468 f.close();
469 ret = e_serializeDta; 469 ret = e_serializeDta;
470 goto out_moveback; 470 goto out_moveback;
471 } 471 }
472 e = writeDataHash(hashAlgo, &serialized, &f); 472 e = writeDataHash(hashAlgo, &serialized, &f);
473 if (e == e_hashNotImpl) { 473 if (e == e_hashNotImpl) {
474 printDebug("PwMDoc::saveDoc(): writeDataHash() failed: e_hashNotImpl"); 474 printDebug("PwMDoc::saveDoc(): writeDataHash() failed: e_hashNotImpl");
475 f.close(); 475 f.close();
476 ret = e_hashNotImpl; 476 ret = e_hashNotImpl;
477 goto out_moveback; 477 goto out_moveback;
478 } else if (e != e_success) { 478 } else if (e != e_success) {
479 printDebug("PwMDoc::saveDoc(): writeDataHash() failed"); 479 printDebug("PwMDoc::saveDoc(): writeDataHash() failed");
480 f.close(); 480 f.close();
481 ret = e_writeHeader; 481 ret = e_writeHeader;
482 goto out_moveback; 482 goto out_moveback;
483 } 483 }
484 if (!compressDta(&serialized, compress)) { 484 if (!compressDta(&serialized, compress)) {
485 printDebug("PwMDoc::saveDoc(): compressDta() failed"); 485 printDebug("PwMDoc::saveDoc(): compressDta() failed");
486 f.close(); 486 f.close();
487 ret = e_enc; 487 ret = e_enc;
488 goto out_moveback; 488 goto out_moveback;
489 } 489 }
490 e = encrypt(&serialized, &currentPw, &f, cryptAlgo); 490 e = encrypt(&serialized, &currentPw, &f, cryptAlgo, hashAlgo);
491 if (e == e_weakPw) { 491 if (e == e_weakPw) {
492 printDebug("PwMDoc::saveDoc(): encrypt() failed: e_weakPw"); 492 printDebug("PwMDoc::saveDoc(): encrypt() failed: e_weakPw");
493 f.close(); 493 f.close();
494 ret = e_weakPw; 494 ret = e_weakPw;
495 goto out_moveback; 495 goto out_moveback;
496 } else if (e == e_cryptNotImpl) { 496 } else if (e == e_cryptNotImpl) {
497 printDebug("PwMDoc::saveDoc(): encrypt() failed: e_cryptNotImpl"); 497 printDebug("PwMDoc::saveDoc(): encrypt() failed: e_cryptNotImpl");
498 f.close(); 498 f.close();
499 ret = e_cryptNotImpl; 499 ret = e_cryptNotImpl;
500 goto out_moveback; 500 goto out_moveback;
501 } else if (e != e_success) { 501 } else if (e != e_success) {
502 printDebug("PwMDoc::saveDoc(): encrypt() failed"); 502 printDebug("PwMDoc::saveDoc(): encrypt() failed");
503 f.close(); 503 f.close();
504 ret = e_enc; 504 ret = e_enc;
505 goto out_moveback; 505 goto out_moveback;
506 } 506 }
507 unsetDocStatFlag(DOC_STAT_DISK_DIRTY); 507 unsetDocStatFlag(DOC_STAT_DISK_DIRTY);
508 f.close(); 508 f.close();
509#ifndef _WIN32_ 509#ifndef _WIN32_
510 if (chmod(filename.latin1(), 510 if (chmod(filename.latin1(),
511 conf()->confGlobFilePermissions())) { 511 conf()->confGlobFilePermissions())) {
512 printWarn(string("chmod failed: ") + strerror(errno)); 512 printWarn(string("chmod failed: ") + strerror(errno));
513 } 513 }
514#endif 514#endif
515 openDocList.edit(this, getTitle().latin1()); 515 openDocList.edit(this, getTitle().latin1());
516 if (wasDeepLocked) { 516 if (wasDeepLocked) {
517 /* Do _not_ save the data with the deepLock() 517 /* Do _not_ save the data with the deepLock()
518 * call, because this will recurse 518 * call, because this will recurse
519 * into saveDoc() 519 * into saveDoc()
520 */ 520 */
521 deepLock(true, false); 521 deepLock(true, false);
522 /* We don't check return value here, because 522 /* We don't check return value here, because
523 * it won't fail. See NOTE in deepLock() 523 * it won't fail. See NOTE in deepLock()
524 */ 524 */
525 } 525 }
526 if (tmpFileMoved != QString::null) { 526 if (tmpFileMoved != QString::null) {
527 // now remove the moved file. 527 // now remove the moved file.
528 if (!QFile::remove(tmpFileMoved)) { 528 if (!QFile::remove(tmpFileMoved)) {
529 printWarn(string("removing file ") 529 printWarn(string("removing file ")
530 + tmpFileMoved.latin1() 530 + tmpFileMoved.latin1()
531 + " failed!"); 531 + " failed!");
532 } 532 }
533 } 533 }
534 ret = e_success; 534 ret = e_success;
535 printDebug(string("writing file { name: ") 535 printDebug(string("writing file { name: ")
536 + filename.latin1() + " compress: " 536 + filename.latin1() + " compress: "
537 + tostr(static_cast<int>(compress)) + " cryptAlgo: " 537 + tostr(static_cast<int>(compress)) + " cryptAlgo: "
538 + tostr(static_cast<int>(cryptAlgo)) + " hashAlgo: " 538 + tostr(static_cast<int>(cryptAlgo)) + " hashAlgo: "
539 + tostr(static_cast<int>(hashAlgo)) 539 + tostr(static_cast<int>(hashAlgo))
540 + " }"); 540 + " }");
541 goto out; 541 goto out;
542out_moveback: 542out_moveback:
543 if (tmpFileMoved != QString::null) { 543 if (tmpFileMoved != QString::null) {
544 if (copyFile(tmpFileMoved, filename)) { 544 if (copyFile(tmpFileMoved, filename)) {
545 if (!QFile::remove(tmpFileMoved)) { 545 if (!QFile::remove(tmpFileMoved)) {
546 printWarn(string("removing tmp file ") 546 printWarn(string("removing tmp file ")
547 + filename.latin1() 547 + filename.latin1()
548 + " failed!"); 548 + " failed!");
549 } 549 }
550 } else { 550 } else {
551 printWarn(string("couldn't copy file ") 551 printWarn(string("couldn't copy file ")
552 + tmpFileMoved.latin1() 552 + tmpFileMoved.latin1()
553 + " back to " 553 + " back to "
554 + filename.latin1()); 554 + filename.latin1());
555 } 555 }
556 } 556 }
557out: 557out:
558 return ret; 558 return ret;
559} 559}
560 560
561PwMerror PwMDoc::openDoc(const QString *file, int openLocked) 561PwMerror PwMDoc::openDoc(const QString *file, int openLocked)
562{ 562{
563 PWM_ASSERT(file); 563 PWM_ASSERT(file);
564 PWM_ASSERT(openLocked == 0 || openLocked == 1 || openLocked == 2); 564 PWM_ASSERT(openLocked == 0 || openLocked == 1 || openLocked == 2);
565 string decrypted, dataHash; 565 string decrypted, dataHash;
566 PwMerror ret; 566 PwMerror ret;
567 char cryptAlgo, dataHashType, compress; 567 char cryptAlgo, dataHashType, compress;
568 unsigned int headerLen; 568 unsigned int headerLen;
569 569
570 if (*file == "") 570 if (*file == "")
571 return e_readFile; 571 return e_readFile;
572 filename = *file; 572 filename = *file;
573 /* check if this file is already open. 573 /* check if this file is already open.
574 * This does not catch symlinks! 574 * This does not catch symlinks!
575 */ 575 */
576 if (!isDeepLocked()) { 576 if (!isDeepLocked()) {
577 if (getOpenDocList()->find(filename.latin1())) 577 if (getOpenDocList()->find(filename.latin1()))
578 return e_alreadyOpen; 578 return e_alreadyOpen;
579 } 579 }
580 QFile f(filename); 580 QFile f(filename);
581 581
582 if (openLocked == 2) { 582 if (openLocked == 2) {
583 // open deep-locked 583 // open deep-locked
584 if (!QFile::exists(filename)) 584 if (!QFile::exists(filename))
585 return e_openFile; 585 return e_openFile;
586 if (deepLock(true, false) != e_success) 586 if (deepLock(true, false) != e_success)
587 return e_openFile; 587 return e_openFile;
588 goto out_success; 588 goto out_success;
589 } 589 }
590 590
591 if (!f.open(IO_ReadOnly)) 591 if (!f.open(IO_ReadOnly))
592 return e_openFile; 592 return e_openFile;
593 593
594 ret = checkHeader(&cryptAlgo, &currentPw, &compress, &headerLen, 594 ret = checkHeader(&cryptAlgo, &currentPw, &compress, &headerLen,
595 &dataHashType, &dataHash, &f); 595 &dataHashType, &dataHash, &f);
596 if (ret != e_success) { 596 if (ret != e_success) {
597 printDebug("PwMDoc::openDoc(): checkHeader() failed"); 597 printDebug("PwMDoc::openDoc(): checkHeader() failed");
598 f.close(); 598 f.close();
599 if (ret == e_wrongPw) { 599 if (ret == e_wrongPw) {
600 wrongMpwMsgBox(getDocStatFlag(DOC_STAT_USE_CHIPCARD)); 600 wrongMpwMsgBox(getDocStatFlag(DOC_STAT_USE_CHIPCARD));
601 return ret; 601 return ret;
602 } else if (ret == e_noPw || 602 } else if (ret == e_noPw ||
603 ret == e_fileVer || 603 ret == e_fileVer ||
604 ret == e_fileFormat || 604 ret == e_fileFormat ||
605 ret == e_hashNotImpl) { 605 ret == e_hashNotImpl) {
606 return ret; 606 return ret;
607 } else 607 } else
608 return e_readFile; 608 return e_readFile;
609 } 609 }
610 ret = decrypt(&decrypted, headerLen, &currentPw, cryptAlgo, &f); 610 ret = decrypt(&decrypted, headerLen, &currentPw, cryptAlgo, dataHashType, &f);
611 if (ret == e_cryptNotImpl) { 611 if (ret == e_cryptNotImpl) {
612 printDebug("PwMDoc::openDoc(): decrypt() failed: e_cryptNotImpl"); 612 printDebug("PwMDoc::openDoc(): decrypt() failed: e_cryptNotImpl");
613 f.close(); 613 f.close();
614 return e_cryptNotImpl; 614 return e_cryptNotImpl;
615 } else if (ret != e_success) { 615 } else if (ret != e_success) {
616 printDebug("PwMDoc::openDoc(): decrypt() failed"); 616 printDebug("PwMDoc::openDoc(): decrypt() failed");
617 f.close(); 617 f.close();
618 return e_readFile; 618 return e_readFile;
619 } 619 }
620 if (!decompressDta(&decrypted, compress)) { 620 if (!decompressDta(&decrypted, compress)) {
621 printDebug("PwMDoc::openDoc(): decompressDta() failed"); 621 printDebug("PwMDoc::openDoc(): decompressDta() failed");
622 f.close(); 622 f.close();
623 return e_fileCorrupt; 623 return e_fileCorrupt;
624 } 624 }
625 ret = checkDataHash(dataHashType, &dataHash, &decrypted); 625 ret = checkDataHash(dataHashType, &dataHash, &decrypted);
626 if (ret == e_hashNotImpl) { 626 if (ret == e_hashNotImpl) {
627 printDebug("PwMDoc::openDoc(): checkDataHash() failed: e_hashNotImpl"); 627 printDebug("PwMDoc::openDoc(): checkDataHash() failed: e_hashNotImpl");
628 f.close(); 628 f.close();
629 return e_hashNotImpl; 629 return e_hashNotImpl;
630 } else if (ret != e_success) { 630 } else if (ret != e_success) {
631 printDebug("PwMDoc::openDoc(): checkDataHash() failed"); 631 printDebug("PwMDoc::openDoc(): checkDataHash() failed");
632 f.close(); 632 f.close();
633 return e_fileCorrupt; 633 return e_fileCorrupt;
634 } 634 }
635 if (!deSerializeDta(&decrypted, openLocked == 1)) { 635 if (!deSerializeDta(&decrypted, openLocked == 1)) {
636 printDebug("PwMDoc::openDoc(): deSerializeDta() failed"); 636 printDebug("PwMDoc::openDoc(): deSerializeDta() failed");
637 f.close(); 637 f.close();
638 return e_readFile; 638 return e_readFile;
639 } 639 }
640 f.close(); 640 f.close();
641 timer()->start(DocTimer::id_mpwTimer); 641 timer()->start(DocTimer::id_mpwTimer);
642 timer()->start(DocTimer::id_autoLockTimer); 642 timer()->start(DocTimer::id_autoLockTimer);
643out_success: 643out_success:
644 openDocList.edit(this, getTitle().latin1()); 644 openDocList.edit(this, getTitle().latin1());
645 emit docOpened(this); 645 emit docOpened(this);
646 return e_success; 646 return e_success;
647} 647}
648 648
649PwMerror PwMDoc::writeFileHeader(char keyHash, char dataHash, char crypt, char compress, 649PwMerror PwMDoc::writeFileHeader(char keyHash, char dataHash, char crypt, char compress,
650 QString *pw, QFile *f) 650 QString *pw, QFile *f)
651{ 651{
652 PWM_ASSERT(pw); 652 PWM_ASSERT(pw);
653 PWM_ASSERT(f); 653 PWM_ASSERT(f);
654 //US ENH: or maybe a bug: checking here for listView does not make sense because we do not check anywhere else 654 //US ENH: or maybe a bug: checking here for listView does not make sense because we do not check anywhere else
655 //Wenn I sync, I open a doc without a view => listView is 0 => Assertion 655 //Wenn I sync, I open a doc without a view => listView is 0 => Assertion
656 //USPWM_ASSERT(listView); 656 //USPWM_ASSERT(listView);
657 if (f->writeBlock(FILE_ID_HEADER, strlen(FILE_ID_HEADER)) != 657 if (f->writeBlock(FILE_ID_HEADER, strlen(FILE_ID_HEADER)) !=
658 static_cast<Q_LONG>(strlen(FILE_ID_HEADER))) { 658 static_cast<Q_LONG>(strlen(FILE_ID_HEADER))) {
659 return e_writeFile; 659 return e_writeFile;
660 } 660 }
661 if (f->putch(PWM_FILE_VER) == -1 || 661 if (f->putch(PWM_FILE_VER) == -1 ||
662 f->putch(keyHash) == -1 || 662 f->putch(keyHash) == -1 ||
663 f->putch(dataHash) == -1 || 663 f->putch(dataHash) == -1 ||
664 f->putch(crypt) == -1 || 664 f->putch(crypt) == -1 ||
665 f->putch(compress) == -1 || 665 f->putch(compress) == -1 ||
666 f->putch((getDocStatFlag(DOC_STAT_USE_CHIPCARD)) ? 666 f->putch((getDocStatFlag(DOC_STAT_USE_CHIPCARD)) ?
667 (static_cast<char>(0x01)) : (static_cast<char>(0x00))) == -1) { 667 (static_cast<char>(0x01)) : (static_cast<char>(0x00))) == -1) {
668 return e_writeFile; 668 return e_writeFile;
669 } 669 }
670 670
671 // write bytes of NUL-data. These bytes are reserved for future-use. 671 // write bytes of NUL-data. These bytes are reserved for future-use.
672 const int bufSize = 64; 672 const int bufSize = 64;
673 char tmp_buf[bufSize]; 673 char tmp_buf[bufSize];
674 memset(tmp_buf, 0x00, bufSize); 674 memset(tmp_buf, 0x00, bufSize);
675 if (f->writeBlock(tmp_buf, bufSize) != bufSize) 675 if (f->writeBlock(tmp_buf, bufSize) != bufSize)
676 return e_writeFile; 676 return e_writeFile;
677 677
678 switch (keyHash) { 678 switch (keyHash) {
679 case PWM_HASH_SHA1: { 679 case PWM_HASH_SHA1: {
680 const int hashlen = SHA1_HASH_LEN_BYTE; 680 const int hashlen = SHA1_HASH_LEN_BYTE;
681 Sha1 hash; 681 Sha1 hash;
682 hash.sha1_write(reinterpret_cast<const byte *>(pw->latin1()), pw->length()); 682 hash.sha1_write(reinterpret_cast<const byte *>(pw->latin1()), pw->length());
683 string ret = hash.sha1_read(); 683 string ret = hash.sha1_read();
684 if (f->writeBlock(ret.c_str(), hashlen) != hashlen) 684 if (f->writeBlock(ret.c_str(), hashlen) != hashlen)
685 return e_writeFile; 685 return e_writeFile;
686 break; 686 break;
687 } 687 }
688 case PWM_HASH_SHA256: 688 case PWM_HASH_SHA256:
689 /*... fall through */ 689 /*... fall through */
690 case PWM_HASH_SHA384: 690 case PWM_HASH_SHA384:
691 case PWM_HASH_SHA512: 691 case PWM_HASH_SHA512:
692 case PWM_HASH_MD5: 692 case PWM_HASH_MD5:
693 case PWM_HASH_RMD160: 693 case PWM_HASH_RMD160:
694 case PWM_HASH_TIGER: 694 case PWM_HASH_TIGER:
695 { 695 {
696 if (!LibGCryptIf::available()) 696 if (!LibGCryptIf::available())
697 return e_hashNotImpl; 697 return e_hashNotImpl;
698 LibGCryptIf gc; 698 LibGCryptIf gc;
699 PwMerror err; 699 PwMerror err;
700 unsigned char *buf; 700 unsigned char *buf;
701 size_t hashLen; 701 size_t hashLen;
702 err = gc.hash(&buf, 702 err = gc.hash(&buf,
703 &hashLen, 703 &hashLen,
704 reinterpret_cast<const unsigned char *>(pw->latin1()), 704 reinterpret_cast<const unsigned char *>(pw->latin1()),
705 pw->length(), 705 pw->length(),
706 keyHash); 706 keyHash);
707 if (err != e_success) 707 if (err != e_success)
708 return e_hashNotImpl; 708 return e_hashNotImpl;
709 if (f->writeBlock(reinterpret_cast<const char *>(buf), hashLen) 709 if (f->writeBlock(reinterpret_cast<const char *>(buf), hashLen)
710 != static_cast<Q_LONG>(hashLen)) { 710 != static_cast<Q_LONG>(hashLen)) {
711 delete [] buf; 711 delete [] buf;
712 return e_hashNotImpl; 712 return e_hashNotImpl;
713 } 713 }
714 delete [] buf; 714 delete [] buf;
715 break; 715 break;
716 } 716 }
717 default: { 717 default: {
718 return e_hashNotImpl; 718 return e_hashNotImpl;
719 } } 719 } }
720 return e_success; 720 return e_success;
721} 721}
722 722
723PwMerror PwMDoc::checkHeader(char *cryptAlgo, QString *pw, char *compress, 723PwMerror PwMDoc::checkHeader(char *cryptAlgo, QString *pw, char *compress,
724 unsigned int *headerLength, char *dataHashType, 724 unsigned int *headerLength, char *dataHashType,
725 string *dataHash, QFile *f) 725 string *dataHash, QFile *f)
726{ 726{
727 PWM_ASSERT(cryptAlgo); 727 PWM_ASSERT(cryptAlgo);
728 PWM_ASSERT(pw); 728 PWM_ASSERT(pw);
729 PWM_ASSERT(headerLength); 729 PWM_ASSERT(headerLength);
730 PWM_ASSERT(dataHashType); 730 PWM_ASSERT(dataHashType);
731 PWM_ASSERT(dataHash); 731 PWM_ASSERT(dataHash);
732 PWM_ASSERT(f); 732 PWM_ASSERT(f);
733 int tmpRet; 733 int tmpRet;
734 // check "magic" header 734 // check "magic" header
735 const char magicHdr[] = FILE_ID_HEADER; 735 const char magicHdr[] = FILE_ID_HEADER;
736 const int hdrLen = array_size(magicHdr) - 1; 736 const int hdrLen = array_size(magicHdr) - 1;
737 char tmp[hdrLen]; 737 char tmp[hdrLen];
738 if (f->readBlock(tmp, hdrLen) != hdrLen) 738 if (f->readBlock(tmp, hdrLen) != hdrLen)
739 return e_readFile; 739 return e_readFile;
740 if (memcmp(tmp, magicHdr, hdrLen) != 0) 740 if (memcmp(tmp, magicHdr, hdrLen) != 0)
741 return e_fileFormat; 741 return e_fileFormat;
742 // read and check file ver 742 // read and check file ver
743 int fileV = f->getch(); 743 int fileV = f->getch();
744 if (fileV == -1) 744 if (fileV == -1)
745 return e_fileFormat; 745 return e_fileFormat;
746 if (fileV != PWM_FILE_VER) 746 if (fileV != PWM_FILE_VER)
747 return e_fileVer; 747 return e_fileVer;
748 // read hash hash type 748 // read hash hash type
749 int keyHash = f->getch(); 749 int keyHash = f->getch();
750 if (keyHash == -1) 750 if (keyHash == -1)
751 return e_fileFormat; 751 return e_fileFormat;
752 // read data hash type 752 // read data hash type
753 tmpRet = f->getch(); 753 tmpRet = f->getch();
754 if (tmpRet == -1) 754 if (tmpRet == -1)
755 return e_fileFormat; 755 return e_fileFormat;
756 *dataHashType = tmpRet; 756 *dataHashType = tmpRet;
757 // read crypt algo 757 // read crypt algo
758 tmpRet = f->getch(); 758 tmpRet = f->getch();
759 if (tmpRet == -1) 759 if (tmpRet == -1)
760 return e_fileFormat; 760 return e_fileFormat;
761 *cryptAlgo = tmpRet; 761 *cryptAlgo = tmpRet;
762 // get compression-algo 762 // get compression-algo
763 tmpRet = f->getch(); 763 tmpRet = f->getch();
764 if (tmpRet == -1) 764 if (tmpRet == -1)
765 return e_fileFormat; 765 return e_fileFormat;
766 *compress = tmpRet; 766 *compress = tmpRet;
767 // get the MPW-flag 767 // get the MPW-flag
768 int mpw_flag = f->getch(); 768 int mpw_flag = f->getch();
769 if (mpw_flag == -1) 769 if (mpw_flag == -1)
770 return e_fileFormat; 770 return e_fileFormat;
771 if (mpw_flag == 0x01) 771 if (mpw_flag == 0x01)
772 setDocStatFlag(DOC_STAT_USE_CHIPCARD); 772 setDocStatFlag(DOC_STAT_USE_CHIPCARD);
773 else 773 else
774 unsetDocStatFlag(DOC_STAT_USE_CHIPCARD); 774 unsetDocStatFlag(DOC_STAT_USE_CHIPCARD);
775 // skip the "RESERVED"-bytes 775 // skip the "RESERVED"-bytes
776 if (!(f->at(f->at() + 64))) 776 if (!(f->at(f->at() + 64)))
777 return e_fileFormat; 777 return e_fileFormat;
778 778
779 *pw = requestMpw(getDocStatFlag(DOC_STAT_USE_CHIPCARD)); 779 *pw = requestMpw(getDocStatFlag(DOC_STAT_USE_CHIPCARD));
780 if (*pw == "") { 780 if (*pw == "") {
781 /* the user didn't give a master-password 781 /* the user didn't give a master-password
782 * or didn't insert a chipcard 782 * or didn't insert a chipcard
783 */ 783 */
784 return e_noPw; 784 return e_noPw;
785 } 785 }
786 // verify key-hash 786 // verify key-hash
787 switch (keyHash) { 787 switch (keyHash) {
788 case PWM_HASH_SHA1: { 788 case PWM_HASH_SHA1: {
789 // read hash from header 789 // read hash from header
790 const int hashLen = SHA1_HASH_LEN_BYTE; 790 const int hashLen = SHA1_HASH_LEN_BYTE;
791 string readHash; 791 string readHash;
792 int i; 792 int i;
793 for (i = 0; i < hashLen; ++i) 793 for (i = 0; i < hashLen; ++i)
794 readHash.push_back(f->getch()); 794 readHash.push_back(f->getch());
795 Sha1 hash; 795 Sha1 hash;
796 hash.sha1_write(reinterpret_cast<const byte *>(pw->latin1()), pw->length()); 796 hash.sha1_write(reinterpret_cast<const byte *>(pw->latin1()), pw->length());
797 string ret = hash.sha1_read(); 797 string ret = hash.sha1_read();
798 if (ret != readHash) 798 if (ret != readHash)
799 return e_wrongPw;// hash doesn't match (wrong key) 799 return e_wrongPw;// hash doesn't match (wrong key)
800 break; 800 break;
801 } 801 }
802 case PWM_HASH_SHA256: 802 case PWM_HASH_SHA256:
803 /*... fall through */ 803 /*... fall through */
804 case PWM_HASH_SHA384: 804 case PWM_HASH_SHA384:
805 case PWM_HASH_SHA512: 805 case PWM_HASH_SHA512:
806 case PWM_HASH_MD5: 806 case PWM_HASH_MD5:
807 case PWM_HASH_RMD160: 807 case PWM_HASH_RMD160:
808 case PWM_HASH_TIGER: { 808 case PWM_HASH_TIGER: {
809 if (!LibGCryptIf::available()) 809 if (!LibGCryptIf::available())
810 return e_hashNotImpl; 810 return e_hashNotImpl;
811 LibGCryptIf gc; 811 LibGCryptIf gc;
812 PwMerror err; 812 PwMerror err;
813 unsigned char *buf; 813 unsigned char *buf;
814 size_t hashLen; 814 size_t hashLen;
815 err = gc.hash(&buf, 815 err = gc.hash(&buf,
816 &hashLen, 816 &hashLen,
817 reinterpret_cast<const unsigned char *>(pw->latin1()), 817 reinterpret_cast<const unsigned char *>(pw->latin1()),
818 pw->length(), 818 pw->length(),
819 keyHash); 819 keyHash);
820 if (err != e_success) 820 if (err != e_success)
821 return e_hashNotImpl; 821 return e_hashNotImpl;
822 string calcHash(reinterpret_cast<const char *>(buf), 822 string calcHash(reinterpret_cast<const char *>(buf),
823 static_cast<string::size_type>(hashLen)); 823 static_cast<string::size_type>(hashLen));
824 delete [] buf; 824 delete [] buf;
825 // read hash from header 825 // read hash from header
826 string readHash; 826 string readHash;
827 size_t i; 827 size_t i;
828 for (i = 0; i < hashLen; ++i) 828 for (i = 0; i < hashLen; ++i)
829 readHash.push_back(f->getch()); 829 readHash.push_back(f->getch());
830 if (calcHash != readHash) 830 if (calcHash != readHash)
831 return e_wrongPw;// hash doesn't match (wrong key) 831 return e_wrongPw;// hash doesn't match (wrong key)
832 break; 832 break;
833 } 833 }
834 default: { 834 default: {
835 return e_hashNotImpl; 835 return e_hashNotImpl;
836 } } 836 } }
837 // read the data-hash from the file 837 // read the data-hash from the file
838 unsigned int hashLen, i; 838 unsigned int hashLen, i;
839 switch (*dataHashType) { 839 switch (*dataHashType) {
840 case PWM_HASH_SHA1: 840 case PWM_HASH_SHA1:
841 hashLen = SHA1_HASH_LEN_BYTE; 841 hashLen = SHA1_HASH_LEN_BYTE;
842 break; 842 break;
843 case PWM_HASH_SHA256: 843 case PWM_HASH_SHA256:
844 /*... fall through */ 844 /*... fall through */
845 case PWM_HASH_SHA384: 845 case PWM_HASH_SHA384:
846 case PWM_HASH_SHA512: 846 case PWM_HASH_SHA512:
847 case PWM_HASH_MD5: 847 case PWM_HASH_MD5:
848 case PWM_HASH_RMD160: 848 case PWM_HASH_RMD160:
849 case PWM_HASH_TIGER: { 849 case PWM_HASH_TIGER: {
850 if (!LibGCryptIf::available()) 850 if (!LibGCryptIf::available())
851 return e_hashNotImpl; 851 return e_hashNotImpl;
852 LibGCryptIf gc; 852 LibGCryptIf gc;
853 hashLen = gc.hashLength(*dataHashType); 853 hashLen = gc.hashLength(*dataHashType);
854 if (hashLen == 0) 854 if (hashLen == 0)
855 return e_hashNotImpl; 855 return e_hashNotImpl;
856 break; 856 break;
857 } 857 }
858 default: 858 default:
859 return e_hashNotImpl; 859 return e_hashNotImpl;
860 } 860 }
861 *dataHash = ""; 861 *dataHash = "";
862 for (i = 0; i < hashLen; ++i) { 862 for (i = 0; i < hashLen; ++i) {
863 tmpRet = f->getch(); 863 tmpRet = f->getch();
864 if (tmpRet == -1) 864 if (tmpRet == -1)
865 return e_fileFormat; 865 return e_fileFormat;
866 dataHash->push_back(static_cast<char>(tmpRet)); 866 dataHash->push_back(static_cast<char>(tmpRet));
867 } 867 }
868 *headerLength = f->at(); 868 *headerLength = f->at();
869#ifndef PWM_EMBEDDED 869#ifndef PWM_EMBEDDED
870 printDebug(string("opening file { compress: ") 870 printDebug(string("opening file { compress: ")
871 + tostr(static_cast<int>(*compress)) + " cryptAlgo: " 871 + tostr(static_cast<int>(*compress)) + " cryptAlgo: "
872 + tostr(static_cast<int>(*cryptAlgo)) + " keyHashAlgo: " 872 + tostr(static_cast<int>(*cryptAlgo)) + " keyHashAlgo: "
873 + tostr(static_cast<int>(keyHash)) 873 + tostr(static_cast<int>(keyHash))
874 + " }"); 874 + " }");
875#else 875#else
876 printDebug(string("opening file { compress: ") 876 printDebug(string("opening file { compress: ")
877 + tostr((int)(*compress)) + " cryptAlgo: " 877 + tostr((int)(*compress)) + " cryptAlgo: "
878 + tostr((int)(*cryptAlgo)) + " keyHashAlgo: " 878 + tostr((int)(*cryptAlgo)) + " keyHashAlgo: "
879 + tostr((int)(keyHash)) 879 + tostr((int)(keyHash))
880 + " }"); 880 + " }");
881#endif 881#endif
882 882
883 return e_success; 883 return e_success;
884} 884}
885 885
886PwMerror PwMDoc::writeDataHash(char dataHash, string *d, QFile *f) 886PwMerror PwMDoc::writeDataHash(char dataHash, string *d, QFile *f)
887{ 887{
888 PWM_ASSERT(d); 888 PWM_ASSERT(d);
889 PWM_ASSERT(f); 889 PWM_ASSERT(f);
890 890
891 switch (dataHash) { 891 switch (dataHash) {
892 case PWM_HASH_SHA1: { 892 case PWM_HASH_SHA1: {
893 const int hashLen = SHA1_HASH_LEN_BYTE; 893 const int hashLen = SHA1_HASH_LEN_BYTE;
894 Sha1 h; 894 Sha1 h;
895 h.sha1_write(reinterpret_cast<const byte *>(d->c_str()), d->size()); 895 h.sha1_write(reinterpret_cast<const byte *>(d->c_str()), d->size());
896 string hRet = h.sha1_read(); 896 string hRet = h.sha1_read();
897 if (f->writeBlock(hRet.c_str(), hashLen) != hashLen) 897 if (f->writeBlock(hRet.c_str(), hashLen) != hashLen)
898 return e_writeFile; 898 return e_writeFile;
899 break; 899 break;
900 } 900 }
901 case PWM_HASH_SHA256: 901 case PWM_HASH_SHA256:
902 /*... fall through */ 902 /*... fall through */
903 case PWM_HASH_SHA384: 903 case PWM_HASH_SHA384:
904 case PWM_HASH_SHA512: 904 case PWM_HASH_SHA512:
905 case PWM_HASH_MD5: 905 case PWM_HASH_MD5:
906 case PWM_HASH_RMD160: 906 case PWM_HASH_RMD160:
907 case PWM_HASH_TIGER: { 907 case PWM_HASH_TIGER: {
908 if (!LibGCryptIf::available()) 908 if (!LibGCryptIf::available())
909 return e_hashNotImpl; 909 return e_hashNotImpl;
910 LibGCryptIf gc; 910 LibGCryptIf gc;
911 PwMerror err; 911 PwMerror err;
912 unsigned char *buf; 912 unsigned char *buf;
913 size_t hashLen; 913 size_t hashLen;
914 err = gc.hash(&buf, 914 err = gc.hash(&buf,
915 &hashLen, 915 &hashLen,
916 reinterpret_cast<const unsigned char *>(d->c_str()), 916 reinterpret_cast<const unsigned char *>(d->c_str()),
917 d->size(), 917 d->size(),
918 dataHash); 918 dataHash);
919 if (err != e_success) 919 if (err != e_success)
920 return e_hashNotImpl; 920 return e_hashNotImpl;
921 if (f->writeBlock(reinterpret_cast<const char *>(buf), hashLen) 921 if (f->writeBlock(reinterpret_cast<const char *>(buf), hashLen)
922 != static_cast<Q_LONG>(hashLen)) { 922 != static_cast<Q_LONG>(hashLen)) {
923 delete [] buf; 923 delete [] buf;
924 return e_hashNotImpl; 924 return e_hashNotImpl;
925 } 925 }
926 delete [] buf; 926 delete [] buf;
927 break; 927 break;
928 } 928 }
929 default: { 929 default: {
930 return e_hashNotImpl; 930 return e_hashNotImpl;
931 } } 931 } }
932 932
933 return e_success; 933 return e_success;
934} 934}
935 935
936bool PwMDoc::backupFile(const QString &filePath) 936bool PwMDoc::backupFile(const QString &filePath)
937{ 937{
938 QFileInfo fi(filePath); 938 QFileInfo fi(filePath);
939 if (!fi.exists()) 939 if (!fi.exists())
940 return true; // Yes, true is correct. 940 return true; // Yes, true is correct.
941 QString pathOnly(fi.dirPath(true)); 941 QString pathOnly(fi.dirPath(true));
942 QString nameOnly(fi.fileName()); 942 QString nameOnly(fi.fileName());
943 QString backupPath = pathOnly 943 QString backupPath = pathOnly
944 + "/~" 944 + "/~"
945 + nameOnly 945 + nameOnly
946 + ".backup"; 946 + ".backup";
947 return copyFile(filePath, backupPath); 947 return copyFile(filePath, backupPath);
948} 948}
949 949
950bool PwMDoc::copyFile(const QString &src, const QString &dst) 950bool PwMDoc::copyFile(const QString &src, const QString &dst)
951{ 951{
952 QFileInfo fi(src); 952 QFileInfo fi(src);
953 if (!fi.exists()) 953 if (!fi.exists())
954 return false; 954 return false;
955 if (QFile::exists(dst)) { 955 if (QFile::exists(dst)) {
956 if (!QFile::remove(dst)) 956 if (!QFile::remove(dst))
957 return false; 957 return false;
958 } 958 }
959 QFile srcFd(src); 959 QFile srcFd(src);
960 if (!srcFd.open(IO_ReadOnly)) 960 if (!srcFd.open(IO_ReadOnly))
961 return false; 961 return false;
962 QFile dstFd(dst); 962 QFile dstFd(dst);
963 if (!dstFd.open(IO_ReadWrite)) { 963 if (!dstFd.open(IO_ReadWrite)) {
964 srcFd.close(); 964 srcFd.close();
965 return false; 965 return false;
966 } 966 }
967 const int tmpBuf_size = 512; 967 const int tmpBuf_size = 512;
968 char tmpBuf[tmpBuf_size]; 968 char tmpBuf[tmpBuf_size];
969 Q_LONG bytesRead, bytesWritten; 969 Q_LONG bytesRead, bytesWritten;
970 970
971 while (!srcFd.atEnd()) { 971 while (!srcFd.atEnd()) {
972 bytesRead = srcFd.readBlock(tmpBuf, 972 bytesRead = srcFd.readBlock(tmpBuf,
973 static_cast<Q_ULONG>(tmpBuf_size)); 973 static_cast<Q_ULONG>(tmpBuf_size));
974 if (bytesRead == -1) { 974 if (bytesRead == -1) {
975 srcFd.close(); 975 srcFd.close();
976 dstFd.close(); 976 dstFd.close();
977 return false; 977 return false;
978 } 978 }
979 bytesWritten = dstFd.writeBlock(tmpBuf, 979 bytesWritten = dstFd.writeBlock(tmpBuf,
980 static_cast<Q_ULONG>(bytesRead)); 980 static_cast<Q_ULONG>(bytesRead));
981 if (bytesWritten != bytesRead) { 981 if (bytesWritten != bytesRead) {
982 srcFd.close(); 982 srcFd.close();
983 dstFd.close(); 983 dstFd.close();
984 return false; 984 return false;
985 } 985 }
986 } 986 }
987 srcFd.close(); 987 srcFd.close();
988 dstFd.close(); 988 dstFd.close();
989 return true; 989 return true;
990} 990}
991 991
992PwMerror PwMDoc::addEntry(const QString &category, PwMDataItem *d, 992PwMerror PwMDoc::addEntry(const QString &category, PwMDataItem *d,
993 bool dontFlagDirty, bool updateMeta) 993 bool dontFlagDirty, bool updateMeta)
994{ 994{
995 PWM_ASSERT(d); 995 PWM_ASSERT(d);
996 unsigned int cat = 0; 996 unsigned int cat = 0;
997 997
998 if (isDeepLocked()) { 998 if (isDeepLocked()) {
999 PwMerror ret; 999 PwMerror ret;
1000 ret = deepLock(false); 1000 ret = deepLock(false);
1001 if (ret != e_success) 1001 if (ret != e_success)
1002 return e_lock; 1002 return e_lock;
1003 } 1003 }
1004 1004
1005 addCategory(category, &cat); 1005 addCategory(category, &cat);
1006 1006
1007 if (numEntries(category) >= maxEntries) 1007 if (numEntries(category) >= maxEntries)
1008 return e_maxAllowedEntr; 1008 return e_maxAllowedEntr;
1009 1009
1010 vector<unsigned int> foundPositions; 1010 vector<unsigned int> foundPositions;
1011 /* historically this was: 1011 /* historically this was:
1012 *const int searchIn = SEARCH_IN_DESC | SEARCH_IN_NAME | 1012 *const int searchIn = SEARCH_IN_DESC | SEARCH_IN_NAME |
1013 * SEARCH_IN_URL | SEARCH_IN_LAUNCHER; 1013 * SEARCH_IN_URL | SEARCH_IN_LAUNCHER;
1014 * But for now we only search in desc. 1014 * But for now we only search in desc.
1015 * That's a tweak to be KWallet compatible. But it should not add 1015 * That's a tweak to be KWallet compatible. But it should not add
1016 * usability-drop onto PwManager, does it? 1016 * usability-drop onto PwManager, does it?
1017 * (And yes, "int" was a bug. Correct is "unsigned int") 1017 * (And yes, "int" was a bug. Correct is "unsigned int")
1018 */ 1018 */
1019 const unsigned int searchIn = SEARCH_IN_DESC; 1019 const unsigned int searchIn = SEARCH_IN_DESC;
1020 findEntry(cat, *d, searchIn, &foundPositions, true); 1020 findEntry(cat, *d, searchIn, &foundPositions, true);
1021 if (foundPositions.size()) { 1021 if (foundPositions.size()) {
1022 // DOH! We found this entry. 1022 // DOH! We found this entry.
1023 return e_entryExists; 1023 return e_entryExists;
1024 } 1024 }
1025 1025
1026 d->listViewPos = -1; 1026 d->listViewPos = -1;
1027 d->lockStat = conf()->confGlobNewEntrLockStat(); 1027 d->lockStat = conf()->confGlobNewEntrLockStat();
1028 if (updateMeta) { 1028 if (updateMeta) {
1029 d->meta.create = QDateTime::currentDateTime(); 1029 d->meta.create = QDateTime::currentDateTime();
1030 d->meta.update = d->meta.create; 1030 d->meta.update = d->meta.create;
1031 } 1031 }
1032 dti.dta[cat].d.push_back(*d); 1032 dti.dta[cat].d.push_back(*d);
1033 1033
1034 delAllEmptyCat(true); 1034 delAllEmptyCat(true);
1035 1035
1036 if (!dontFlagDirty) 1036 if (!dontFlagDirty)
1037 flagDirty(); 1037 flagDirty();
1038 return e_success; 1038 return e_success;
1039} 1039}
1040 1040
1041PwMerror PwMDoc::addCategory(const QString &category, unsigned int *categoryIndex, 1041PwMerror PwMDoc::addCategory(const QString &category, unsigned int *categoryIndex,
1042 bool checkIfExist) 1042 bool checkIfExist)
1043{ 1043{
1044 if (isDeepLocked()) { 1044 if (isDeepLocked()) {
1045 PwMerror ret; 1045 PwMerror ret;
1046 ret = deepLock(false); 1046 ret = deepLock(false);
1047 if (ret != e_success) 1047 if (ret != e_success)
1048 return e_lock; 1048 return e_lock;
1049 } 1049 }
1050 if (checkIfExist) { 1050 if (checkIfExist) {
1051 if (findCategory(category, categoryIndex)) 1051 if (findCategory(category, categoryIndex))
1052 return e_categoryExists; 1052 return e_categoryExists;
1053 } 1053 }
1054 PwMCategoryItem item; 1054 PwMCategoryItem item;
1055 //US ENH: clear item to initialize with default values, or create a constructor 1055 //US ENH: clear item to initialize with default values, or create a constructor
1056 item.clear(); 1056 item.clear();
1057 1057
1058 item.name = category.latin1(); 1058 item.name = category.latin1();
1059 dti.dta.push_back(item); 1059 dti.dta.push_back(item);
1060 if (categoryIndex) 1060 if (categoryIndex)
1061 *categoryIndex = dti.dta.size() - 1; 1061 *categoryIndex = dti.dta.size() - 1;
1062 return e_success; 1062 return e_success;
1063} 1063}
1064 1064
1065bool PwMDoc::delEntry(const QString &category, unsigned int index, bool dontFlagDirty) 1065bool PwMDoc::delEntry(const QString &category, unsigned int index, bool dontFlagDirty)
1066{ 1066{
1067 unsigned int cat = 0; 1067 unsigned int cat = 0;
1068 1068
1069 if (!findCategory(category, &cat)) { 1069 if (!findCategory(category, &cat)) {
1070 BUG(); 1070 BUG();
1071 return false; 1071 return false;
1072 } 1072 }
1073 1073
1074 return delEntry(cat, index, dontFlagDirty); 1074 return delEntry(cat, index, dontFlagDirty);
1075} 1075}
1076 1076
1077bool PwMDoc::delEntry(unsigned int category, unsigned int index, bool dontFlagDirty) 1077bool PwMDoc::delEntry(unsigned int category, unsigned int index, bool dontFlagDirty)
1078{ 1078{
1079 if (isDeepLocked()) 1079 if (isDeepLocked())
1080 return false; 1080 return false;
1081 if (index > dti.dta[category].d.size() - 1) 1081 if (index > dti.dta[category].d.size() - 1)
1082 return false; 1082 return false;
1083 getDataChangedLock(); 1083 getDataChangedLock();
1084 if (!lockAt(category, index, false)) { 1084 if (!lockAt(category, index, false)) {
1085 putDataChangedLock(); 1085 putDataChangedLock();
1086 return false; 1086 return false;
1087 } 1087 }
1088 putDataChangedLock(); 1088 putDataChangedLock();
1089 int lvPos = dti.dta[category].d[index].listViewPos; 1089 int lvPos = dti.dta[category].d[index].listViewPos;
1090 1090
1091 // delete entry 1091 // delete entry
1092 dti.dta[category].d.erase(dti.dta[category].d.begin() + index); 1092 dti.dta[category].d.erase(dti.dta[category].d.begin() + index);
1093 1093
1094 unsigned int i, entries = numEntries(category); 1094 unsigned int i, entries = numEntries(category);
1095 if (!entries) { 1095 if (!entries) {
1096 // no more entries in this category, so 1096 // no more entries in this category, so
1097 // we can delete it, too. 1097 // we can delete it, too.
1098 BUG_ON(!delCategory(category)); 1098 BUG_ON(!delCategory(category));
1099 // delCategory() flags it dirty, so we need not to do so. 1099 // delCategory() flags it dirty, so we need not to do so.
1100 return true; 1100 return true;
1101 } 1101 }
1102 for (i = 0; i < entries; ++i) { 1102 for (i = 0; i < entries; ++i) {
1103 // decrement all listViewPositions that are greater than the deleted. 1103 // decrement all listViewPositions that are greater than the deleted.
1104 if (dti.dta[category].d[i].listViewPos > lvPos) 1104 if (dti.dta[category].d[i].listViewPos > lvPos)
1105 --dti.dta[category].d[i].listViewPos; 1105 --dti.dta[category].d[i].listViewPos;
1106 } 1106 }
1107 1107
1108 if (!dontFlagDirty) 1108 if (!dontFlagDirty)
1109 flagDirty(); 1109 flagDirty();
1110 return true; 1110 return true;
1111} 1111}
1112 1112
1113bool PwMDoc::editEntry(const QString &oldCategory, const QString &newCategory, 1113bool PwMDoc::editEntry(const QString &oldCategory, const QString &newCategory,
1114 unsigned int index, PwMDataItem *d, bool updateMeta) 1114 unsigned int index, PwMDataItem *d, bool updateMeta)
1115{ 1115{
1116 PWM_ASSERT(d); 1116 PWM_ASSERT(d);
1117 unsigned int oldCat = 0; 1117 unsigned int oldCat = 0;
1118 1118
1119 if (!findCategory(oldCategory, &oldCat)) { 1119 if (!findCategory(oldCategory, &oldCat)) {
1120 BUG(); 1120 BUG();
1121 return false; 1121 return false;
1122 } 1122 }
1123 1123
1124 return editEntry(oldCat, newCategory, index, d, updateMeta); 1124 return editEntry(oldCat, newCategory, index, d, updateMeta);
1125} 1125}
1126 1126
1127bool PwMDoc::editEntry(unsigned int oldCategory, const QString &newCategory, 1127bool PwMDoc::editEntry(unsigned int oldCategory, const QString &newCategory,
1128 unsigned int index, PwMDataItem *d, bool updateMeta) 1128 unsigned int index, PwMDataItem *d, bool updateMeta)
1129{ 1129{
1130 if (isDeepLocked()) 1130 if (isDeepLocked())
1131 return false; 1131 return false;
1132 if (updateMeta) { 1132 if (updateMeta) {
1133 d->meta.update = QDateTime::currentDateTime(); 1133 d->meta.update = QDateTime::currentDateTime();
1134 if (d->meta.create.isNull()) { 1134 if (d->meta.create.isNull()) {
1135 d->meta.create = d->meta.update; 1135 d->meta.create = d->meta.update;
1136 } 1136 }
1137 } 1137 }
1138 if (dti.dta[oldCategory].name != newCategory.latin1()) { 1138 if (dti.dta[oldCategory].name != newCategory.latin1()) {
1139 // the user changed the category. 1139 // the user changed the category.
1140 PwMerror ret; 1140 PwMerror ret;
1141 d->rev = 0; 1141 d->rev = 0;
1142 ret = addEntry(newCategory, d, true, false); 1142 ret = addEntry(newCategory, d, true, false);
1143 if (ret != e_success) 1143 if (ret != e_success)
1144 return false; 1144 return false;
1145 if (!delEntry(oldCategory, index, true)) 1145 if (!delEntry(oldCategory, index, true))
1146 return false; 1146 return false;
1147 } else { 1147 } else {
1148 d->rev = dti.dta[oldCategory].d[index].rev + 1; // increment revision counter. 1148 d->rev = dti.dta[oldCategory].d[index].rev + 1; // increment revision counter.
1149 dti.dta[oldCategory].d[index] = *d; 1149 dti.dta[oldCategory].d[index] = *d;
1150 } 1150 }
1151 flagDirty(); 1151 flagDirty();
1152 return true; 1152 return true;
1153} 1153}
1154 1154
1155unsigned int PwMDoc::numEntries(const QString &category) 1155unsigned int PwMDoc::numEntries(const QString &category)
1156{ 1156{
1157 unsigned int cat = 0; 1157 unsigned int cat = 0;
1158 1158
1159 if (!findCategory(category, &cat)) { 1159 if (!findCategory(category, &cat)) {
1160 BUG(); 1160 BUG();
1161 return 0; 1161 return 0;
1162 } 1162 }
1163 1163
1164 return numEntries(cat); 1164 return numEntries(cat);
1165} 1165}
1166 1166
1167bool PwMDoc::serializeDta(string *d) 1167bool PwMDoc::serializeDta(string *d)
1168{ 1168{
1169 PWM_ASSERT(d); 1169 PWM_ASSERT(d);
1170 Serializer ser; 1170 Serializer ser;
1171 if (!ser.serialize(dti)) 1171 if (!ser.serialize(dti))
1172 return false; 1172 return false;
1173 d->assign(ser.getXml()); 1173 d->assign(ser.getXml());
1174 if (!d->size()) 1174 if (!d->size())
1175 return false; 1175 return false;
1176 return true; 1176 return true;
1177} 1177}
1178 1178
1179bool PwMDoc::deSerializeDta(const string *d, bool entriesLocked) 1179bool PwMDoc::deSerializeDta(const string *d, bool entriesLocked)
1180{ 1180{
1181 PWM_ASSERT(d); 1181 PWM_ASSERT(d);
1182#ifndef PWM_EMBEDDED 1182#ifndef PWM_EMBEDDED
1183 try { 1183 try {
1184 1184
1185 Serializer ser(d->c_str()); 1185 Serializer ser(d->c_str());
1186 ser.setDefaultLockStat(entriesLocked); 1186 ser.setDefaultLockStat(entriesLocked);
1187 if (!ser.deSerialize(&dti)) 1187 if (!ser.deSerialize(&dti))
1188 return false; 1188 return false;
1189 } catch (PwMException) { 1189 } catch (PwMException) {
1190 return false; 1190 return false;
1191 } 1191 }
1192#else 1192#else
1193 Serializer ser(d->c_str()); 1193 Serializer ser(d->c_str());
1194 ser.setDefaultLockStat(entriesLocked); 1194 ser.setDefaultLockStat(entriesLocked);
1195 if (!ser.deSerialize(&dti)) 1195 if (!ser.deSerialize(&dti))
1196 return false; 1196 return false;
1197#endif 1197#endif
1198 1198
1199 emitDataChanged(this); 1199 emitDataChanged(this);
1200 return true; 1200 return true;
1201} 1201}
1202 1202
1203bool PwMDoc::getEntry(const QString &category, unsigned int index, 1203bool PwMDoc::getEntry(const QString &category, unsigned int index,
1204 PwMDataItem * d, bool unlockIfLocked) 1204 PwMDataItem * d, bool unlockIfLocked)
1205{ 1205{
1206 PWM_ASSERT(d); 1206 PWM_ASSERT(d);
1207 unsigned int cat = 0; 1207 unsigned int cat = 0;
1208 1208
1209 if (!findCategory(category, &cat)) { 1209 if (!findCategory(category, &cat)) {
1210 BUG(); 1210 BUG();
1211 return false; 1211 return false;
1212 } 1212 }
1213 1213
1214 return getEntry(cat, index, d, unlockIfLocked); 1214 return getEntry(cat, index, d, unlockIfLocked);
1215} 1215}
1216 1216
1217bool PwMDoc::getEntry(unsigned int category, unsigned int index, 1217bool PwMDoc::getEntry(unsigned int category, unsigned int index,
1218 PwMDataItem *d, bool unlockIfLocked) 1218 PwMDataItem *d, bool unlockIfLocked)
1219{ 1219{
1220 if (index > dti.dta[category].d.size() - 1) 1220 if (index > dti.dta[category].d.size() - 1)
1221 return false; 1221 return false;
1222 1222
1223 bool locked = isLocked(category, index); 1223 bool locked = isLocked(category, index);
1224 if (locked) { 1224 if (locked) {
1225 /* this entry is locked. We don't return a password, 1225 /* this entry is locked. We don't return a password,
1226 * until it's unlocked by the user by inserting 1226 * until it's unlocked by the user by inserting
1227 * chipcard or entering the mpw 1227 * chipcard or entering the mpw
1228 */ 1228 */
1229 if (unlockIfLocked) { 1229 if (unlockIfLocked) {
1230 if (!lockAt(category, index, false)) { 1230 if (!lockAt(category, index, false)) {
1231 return false; 1231 return false;
1232 } 1232 }
1233 locked = false; 1233 locked = false;
1234 } 1234 }
1235 } 1235 }
1236 1236
1237 *d = dti.dta[category].d[index]; 1237 *d = dti.dta[category].d[index];
1238 if (locked) 1238 if (locked)
1239 d->pw = LOCKED_STRING.latin1(); 1239 d->pw = LOCKED_STRING.latin1();
1240 1240
1241 return true; 1241 return true;
1242} 1242}
1243 1243
1244PwMerror PwMDoc::getCommentByLvp(const QString &category, int listViewPos, 1244PwMerror PwMDoc::getCommentByLvp(const QString &category, int listViewPos,
1245 string *foundComment) 1245 string *foundComment)
1246{ 1246{
1247 PWM_ASSERT(foundComment); 1247 PWM_ASSERT(foundComment);
1248 unsigned int cat = 0; 1248 unsigned int cat = 0;
1249 1249
1250 if (!findCategory(category, &cat)) 1250 if (!findCategory(category, &cat))
1251 return e_invalidArg; 1251 return e_invalidArg;
1252 1252
1253 unsigned int i, entries = numEntries(cat); 1253 unsigned int i, entries = numEntries(cat);
1254 for (i = 0; i < entries; ++i) { 1254 for (i = 0; i < entries; ++i) {
1255 if (dti.dta[cat].d[i].listViewPos == listViewPos) { 1255 if (dti.dta[cat].d[i].listViewPos == listViewPos) {
1256 *foundComment = dti.dta[cat].d[i].comment; 1256 *foundComment = dti.dta[cat].d[i].comment;
1257 if (dti.dta[cat].d[i].binary) 1257 if (dti.dta[cat].d[i].binary)
1258 return e_binEntry; 1258 return e_binEntry;
1259 return e_normalEntry; 1259 return e_normalEntry;
1260 } 1260 }
1261 } 1261 }
1262 BUG(); 1262 BUG();
1263 return e_generic; 1263 return e_generic;
1264} 1264}
1265 1265
1266bool PwMDoc::compressDta(string *d, char algo) 1266bool PwMDoc::compressDta(string *d, char algo)
1267{ 1267{
1268 PWM_ASSERT(d); 1268 PWM_ASSERT(d);
1269 switch (algo) { 1269 switch (algo) {
1270 case PWM_COMPRESS_GZIP: { 1270 case PWM_COMPRESS_GZIP: {
1271 CompressGzip comp; 1271 CompressGzip comp;
1272 return comp.compress(d); 1272 return comp.compress(d);
1273 } 1273 }
1274#ifndef PWM_EMBEDDED 1274#ifndef PWM_EMBEDDED
1275 case PWM_COMPRESS_BZIP2: { 1275 case PWM_COMPRESS_BZIP2: {
1276 CompressBzip2 comp; 1276 CompressBzip2 comp;
1277 return comp.compress(d); 1277 return comp.compress(d);
1278 } 1278 }
1279#endif 1279#endif
1280 case PWM_COMPRESS_NONE: { 1280 case PWM_COMPRESS_NONE: {
1281 return true; 1281 return true;
1282 } default: { 1282 } default: {
1283 BUG(); 1283 BUG();
1284 } 1284 }
1285 } 1285 }
1286 return false; 1286 return false;
1287} 1287}
1288 1288
1289bool PwMDoc::decompressDta(string *d, char algo) 1289bool PwMDoc::decompressDta(string *d, char algo)
1290{ 1290{
1291 PWM_ASSERT(d); 1291 PWM_ASSERT(d);
1292 switch (algo) { 1292 switch (algo) {
1293 case PWM_COMPRESS_GZIP: { 1293 case PWM_COMPRESS_GZIP: {
1294 CompressGzip comp; 1294 CompressGzip comp;
1295 return comp.decompress(d); 1295 return comp.decompress(d);
1296 } 1296 }
1297#ifndef PWM_EMBEDDED 1297#ifndef PWM_EMBEDDED
1298 case PWM_COMPRESS_BZIP2: { 1298 case PWM_COMPRESS_BZIP2: {
1299 CompressBzip2 comp; 1299 CompressBzip2 comp;
1300 return comp.decompress(d); 1300 return comp.decompress(d);
1301 } 1301 }
1302#endif 1302#endif
1303 case PWM_COMPRESS_NONE: { 1303 case PWM_COMPRESS_NONE: {
1304 return true; 1304 return true;
1305 } 1305 }
1306 } 1306 }
1307 return false; 1307 return false;
1308} 1308}
1309 1309
1310PwMerror PwMDoc::encrypt(string *d, const QString *pw, QFile *f, char algo) 1310PwMerror PwMDoc::encrypt(string *d, const QString *pw, QFile *f, char algo,
1311 char hashalgo //US BUG: pass _hashalgo because we need it in hashPassphrase
1312)
1311{ 1313{
1312 PWM_ASSERT(d); 1314 PWM_ASSERT(d);
1313 PWM_ASSERT(pw); 1315 PWM_ASSERT(pw);
1314 PWM_ASSERT(f); 1316 PWM_ASSERT(f);
1315 1317
1316 size_t encSize; 1318 size_t encSize;
1317 byte *encrypted = 0; 1319 byte *encrypted = 0;
1318 1320
1319 switch (algo) { 1321 switch (algo) {
1320 case PWM_CRYPT_BLOWFISH: { 1322 case PWM_CRYPT_BLOWFISH: {
1321 Blowfish::padNull(d); 1323 Blowfish::padNull(d);
1322 encSize = d->length(); 1324 encSize = d->length();
1323 encrypted = new byte[encSize]; 1325 encrypted = new byte[encSize];
1324 Blowfish bf; 1326 Blowfish bf;
1325 if (bf.bf_setkey((byte *) pw->latin1(), pw->length())) { 1327 if (bf.bf_setkey((byte *) pw->latin1(), pw->length())) {
1326 delete [] encrypted; 1328 delete [] encrypted;
1327 return e_weakPw; 1329 return e_weakPw;
1328 } 1330 }
1329 bf.bf_encrypt((byte *) encrypted, (byte *) d->c_str(), encSize); 1331 bf.bf_encrypt((byte *) encrypted, (byte *) d->c_str(), encSize);
1330 break; 1332 break;
1331 } 1333 }
1332 case PWM_CRYPT_AES128: 1334 case PWM_CRYPT_AES128:
1333 /*... fall through */ 1335 /*... fall through */
1334 case PWM_CRYPT_AES192: 1336 case PWM_CRYPT_AES192:
1335 case PWM_CRYPT_AES256: 1337 case PWM_CRYPT_AES256:
1336 case PWM_CRYPT_3DES: 1338 case PWM_CRYPT_3DES:
1337 case PWM_CRYPT_TWOFISH: 1339 case PWM_CRYPT_TWOFISH:
1338 case PWM_CRYPT_TWOFISH128: { 1340 case PWM_CRYPT_TWOFISH128: {
1339 if (!LibGCryptIf::available()) 1341 if (!LibGCryptIf::available())
1340 return e_cryptNotImpl; 1342 return e_cryptNotImpl;
1341 LibGCryptIf gc; 1343 LibGCryptIf gc;
1342 PwMerror err; 1344 PwMerror err;
1343 unsigned char *plain = new unsigned char[d->length() + 1024]; 1345 unsigned char *plain = new unsigned char[d->length() + 1024];
1344 memcpy(plain, d->c_str(), d->length()); 1346 memcpy(plain, d->c_str(), d->length());
1345 err = gc.encrypt(&encrypted, 1347 err = gc.encrypt(&encrypted,
1346 &encSize, 1348 &encSize,
1347 plain, 1349 plain,
1348 d->length(), 1350 d->length(),
1349 reinterpret_cast<const unsigned char *>(pw->latin1()), 1351 reinterpret_cast<const unsigned char *>(pw->latin1()),
1350 pw->length(), 1352 pw->length(),
1351 algo); 1353 algo,
1354 hashalgo //US BUG: pass _hashalgo because we need it in hashPassphrase
1355 );
1352 delete [] plain; 1356 delete [] plain;
1353 if (err != e_success) 1357 if (err != e_success)
1354 return e_cryptNotImpl; 1358 return e_cryptNotImpl;
1355 break; 1359 break;
1356 } 1360 }
1357 default: { 1361 default: {
1358 delete_ifnot_null_array(encrypted); 1362 delete_ifnot_null_array(encrypted);
1359 return e_cryptNotImpl; 1363 return e_cryptNotImpl;
1360 } } 1364 } }
1361 1365
1362 // write encrypted data to file 1366 // write encrypted data to file
1363 if (f->writeBlock(reinterpret_cast<const char *>(encrypted), 1367 if (f->writeBlock(reinterpret_cast<const char *>(encrypted),
1364 static_cast<Q_ULONG>(encSize)) 1368 static_cast<Q_ULONG>(encSize))
1365 != static_cast<Q_LONG>(encSize)) { 1369 != static_cast<Q_LONG>(encSize)) {
1366 delete_ifnot_null_array(encrypted); 1370 delete_ifnot_null_array(encrypted);
1367 return e_writeFile; 1371 return e_writeFile;
1368 } 1372 }
1369 delete_ifnot_null_array(encrypted); 1373 delete_ifnot_null_array(encrypted);
1370 return e_success; 1374 return e_success;
1371} 1375}
1372 1376
1373PwMerror PwMDoc::decrypt(string *d, unsigned int pos, const QString *pw, 1377PwMerror PwMDoc::decrypt(string *d, unsigned int pos, const QString *pw,
1374 char algo, QFile *f) 1378 char algo,
1379 char hashalgo, //US BUG: pass _hashalgo because we need it in hashPassphrase
1380 QFile *f)
1375{ 1381{
1376 PWM_ASSERT(d); 1382 PWM_ASSERT(d);
1377 PWM_ASSERT(pw); 1383 PWM_ASSERT(pw);
1378 PWM_ASSERT(f); 1384 PWM_ASSERT(f);
1379 1385
1380 unsigned int cryptLen = f->size() - pos; 1386 unsigned int cryptLen = f->size() - pos;
1381 byte *encrypted = new byte[cryptLen]; 1387 byte *encrypted = new byte[cryptLen];
1382 byte *decrypted = new byte[cryptLen]; 1388 byte *decrypted = new byte[cryptLen];
1383 1389
1384 f->at(pos); 1390 f->at(pos);
1385#ifndef PWM_EMBEDDED 1391#ifndef PWM_EMBEDDED
1386 if (f->readBlock(reinterpret_cast<char *>(encrypted), 1392 if (f->readBlock(reinterpret_cast<char *>(encrypted),
1387 static_cast<Q_ULONG>(cryptLen)) 1393 static_cast<Q_ULONG>(cryptLen))
1388 != static_cast<Q_LONG>(cryptLen)) { 1394 != static_cast<Q_LONG>(cryptLen)) {
1389 delete [] encrypted; 1395 delete [] encrypted;
1390 delete [] decrypted; 1396 delete [] decrypted;
1391 return e_readFile; 1397 return e_readFile;
1392 } 1398 }
1393#else 1399#else
1394 if (f->readBlock((char *)(encrypted), 1400 if (f->readBlock((char *)(encrypted),
1395 (unsigned long)(cryptLen)) 1401 (unsigned long)(cryptLen))
1396 != (long)(cryptLen)) { 1402 != (long)(cryptLen)) {
1397 delete [] encrypted; 1403 delete [] encrypted;
1398 delete [] decrypted; 1404 delete [] decrypted;
1399 return e_readFile; 1405 return e_readFile;
1400 } 1406 }
1401#endif 1407#endif
1402 switch (algo) { 1408 switch (algo) {
1403 case PWM_CRYPT_BLOWFISH: { 1409 case PWM_CRYPT_BLOWFISH: {
1404 Blowfish bf; 1410 Blowfish bf;
1405 bf.bf_setkey((byte *) pw->latin1(), pw->length()); 1411 bf.bf_setkey((byte *) pw->latin1(), pw->length());
1406 bf.bf_decrypt(decrypted, encrypted, cryptLen); 1412 bf.bf_decrypt(decrypted, encrypted, cryptLen);
1407 break; 1413 break;
1408 } 1414 }
1409 case PWM_CRYPT_AES128: 1415 case PWM_CRYPT_AES128:
1410 /*... fall through */ 1416 /*... fall through */
1411 case PWM_CRYPT_AES192: 1417 case PWM_CRYPT_AES192:
1412 case PWM_CRYPT_AES256: 1418 case PWM_CRYPT_AES256:
1413 case PWM_CRYPT_3DES: 1419 case PWM_CRYPT_3DES:
1414 case PWM_CRYPT_TWOFISH: 1420 case PWM_CRYPT_TWOFISH:
1415 case PWM_CRYPT_TWOFISH128: { 1421 case PWM_CRYPT_TWOFISH128: {
1416 if (!LibGCryptIf::available()) 1422 if (!LibGCryptIf::available())
1417 return e_cryptNotImpl; 1423 return e_cryptNotImpl;
1418 LibGCryptIf gc; 1424 LibGCryptIf gc;
1419 PwMerror err; 1425 PwMerror err;
1420 err = gc.decrypt(&decrypted, 1426 err = gc.decrypt(&decrypted,
1421 &cryptLen, 1427 &cryptLen,
1422 encrypted, 1428 encrypted,
1423 cryptLen, 1429 cryptLen,
1424 reinterpret_cast<const unsigned char *>(pw->latin1()), 1430 reinterpret_cast<const unsigned char *>(pw->latin1()),
1425 pw->length(), 1431 pw->length(),
1426 algo); 1432 algo,
1433 hashalgo //US BUG: pass _hashalgo because we need it in hashPassphrase
1434);
1427 if (err != e_success) { 1435 if (err != e_success) {
1428 delete [] encrypted; 1436 delete [] encrypted;
1429 delete [] decrypted; 1437 delete [] decrypted;
1430 return e_cryptNotImpl; 1438 return e_cryptNotImpl;
1431 } 1439 }
1432 break; 1440 break;
1433 } 1441 }
1434 default: { 1442 default: {
1435 delete [] encrypted; 1443 delete [] encrypted;
1436 delete [] decrypted; 1444 delete [] decrypted;
1437 return e_cryptNotImpl; 1445 return e_cryptNotImpl;
1438 } } 1446 } }
1439 delete [] encrypted; 1447 delete [] encrypted;
1440#ifndef PWM_EMBEDDED 1448#ifndef PWM_EMBEDDED
1441 d->assign(reinterpret_cast<const char *>(decrypted), 1449 d->assign(reinterpret_cast<const char *>(decrypted),
1442 static_cast<string::size_type>(cryptLen)); 1450 static_cast<string::size_type>(cryptLen));
1443#else 1451#else
1444 d->assign((const char *)(decrypted), 1452 d->assign((const char *)(decrypted),
1445 (string::size_type)(cryptLen)); 1453 (string::size_type)(cryptLen));
1446#endif 1454#endif
1447 delete [] decrypted; 1455 delete [] decrypted;
1448 if (algo == PWM_CRYPT_BLOWFISH) { 1456 if (algo == PWM_CRYPT_BLOWFISH) {
1449 if (!Blowfish::unpadNull(d)) { 1457 if (!Blowfish::unpadNull(d)) {
1450 BUG(); 1458 BUG();
1451 return e_readFile; 1459 return e_readFile;
1452 } 1460 }
1453 } 1461 }
1454 return e_success; 1462 return e_success;
1455} 1463}
1456 1464
1457PwMerror PwMDoc::checkDataHash(char dataHashType, const string *dataHash, 1465PwMerror PwMDoc::checkDataHash(char dataHashType, const string *dataHash,
1458 const string *dataStream) 1466 const string *dataStream)
1459{ 1467{
1460 PWM_ASSERT(dataHash); 1468 PWM_ASSERT(dataHash);
1461 PWM_ASSERT(dataStream); 1469 PWM_ASSERT(dataStream);
1462 switch(dataHashType) { 1470 switch(dataHashType) {
1463 case PWM_HASH_SHA1: { 1471 case PWM_HASH_SHA1: {
1464 Sha1 hash; 1472 Sha1 hash;
1465 hash.sha1_write((byte*)dataStream->c_str(), dataStream->length()); 1473 hash.sha1_write((byte*)dataStream->c_str(), dataStream->length());
1466 string ret = hash.sha1_read(); 1474 string ret = hash.sha1_read();
1467 if (ret != *dataHash) 1475 if (ret != *dataHash)
1468 return e_fileCorrupt; 1476 return e_fileCorrupt;
1469 break; 1477 break;
1470 } 1478 }
1471 case PWM_HASH_SHA256: 1479 case PWM_HASH_SHA256:
1472 /*... fall through */ 1480 /*... fall through */
1473 case PWM_HASH_SHA384: 1481 case PWM_HASH_SHA384:
1474 case PWM_HASH_SHA512: 1482 case PWM_HASH_SHA512:
1475 case PWM_HASH_MD5: 1483 case PWM_HASH_MD5:
1476 case PWM_HASH_RMD160: 1484 case PWM_HASH_RMD160:
1477 case PWM_HASH_TIGER: { 1485 case PWM_HASH_TIGER: {
1478 if (!LibGCryptIf::available()) 1486 if (!LibGCryptIf::available())
1479 return e_hashNotImpl; 1487 return e_hashNotImpl;
1480 LibGCryptIf gc; 1488 LibGCryptIf gc;
1481 PwMerror err; 1489 PwMerror err;
1482 unsigned char *buf; 1490 unsigned char *buf;
1483 size_t hashLen; 1491 size_t hashLen;
1484 err = gc.hash(&buf, 1492 err = gc.hash(&buf,
1485 &hashLen, 1493 &hashLen,
1486 reinterpret_cast<const unsigned char *>(dataStream->c_str()), 1494 reinterpret_cast<const unsigned char *>(dataStream->c_str()),
1487 dataStream->length(), 1495 dataStream->length(),
1488 dataHashType); 1496 dataHashType);
1489 if (err != e_success) 1497 if (err != e_success)
1490 return e_hashNotImpl; 1498 return e_hashNotImpl;
1491 string calcHash(reinterpret_cast<const char *>(buf), 1499 string calcHash(reinterpret_cast<const char *>(buf),
1492 static_cast<string::size_type>(hashLen)); 1500 static_cast<string::size_type>(hashLen));
1493 delete [] buf; 1501 delete [] buf;
1494 if (calcHash != *dataHash) 1502 if (calcHash != *dataHash)
1495 return e_fileCorrupt; 1503 return e_fileCorrupt;
1496 break; 1504 break;
1497 } 1505 }
1498 default: 1506 default:
1499 return e_hashNotImpl; 1507 return e_hashNotImpl;
1500 } 1508 }
1501 return e_success; 1509 return e_success;
1502} 1510}
1503 1511
1504bool PwMDoc::lockAt(unsigned int category, unsigned int index, 1512bool PwMDoc::lockAt(unsigned int category, unsigned int index,
1505 bool lock) 1513 bool lock)
1506{ 1514{
1507 if (index >= numEntries(category)) { 1515 if (index >= numEntries(category)) {
1508 BUG(); 1516 BUG();
1509 return false; 1517 return false;
1510 } 1518 }
1511 if (lock == dti.dta[category].d[index].lockStat) 1519 if (lock == dti.dta[category].d[index].lockStat)
1512 return true; 1520 return true;
1513 1521
1514 if (!lock && currentPw != "") { 1522 if (!lock && currentPw != "") {
1515 // "unlocking" and "password is already set" 1523 // "unlocking" and "password is already set"
1516 if (!getDocStatFlag(DOC_STAT_UNLOCK_WITHOUT_PW)) { 1524 if (!getDocStatFlag(DOC_STAT_UNLOCK_WITHOUT_PW)) {
1517 // unlocking without pw not allowed 1525 // unlocking without pw not allowed
1518 QString pw; 1526 QString pw;
1519 pw = requestMpw(getDocStatFlag(DOC_STAT_USE_CHIPCARD)); 1527 pw = requestMpw(getDocStatFlag(DOC_STAT_USE_CHIPCARD));
1520 if (pw != "") { 1528 if (pw != "") {
1521 if (pw != currentPw) { 1529 if (pw != currentPw) {
1522 wrongMpwMsgBox(getDocStatFlag(DOC_STAT_USE_CHIPCARD)); 1530 wrongMpwMsgBox(getDocStatFlag(DOC_STAT_USE_CHIPCARD));
1523 return false; 1531 return false;
1524 } else { 1532 } else {
1525 timer()->start(DocTimer::id_mpwTimer); 1533 timer()->start(DocTimer::id_mpwTimer);
1526 } 1534 }
1527 } else { 1535 } else {
1528 return false; 1536 return false;
1529 } 1537 }
1530 } else { 1538 } else {
1531 timer()->start(DocTimer::id_mpwTimer); 1539 timer()->start(DocTimer::id_mpwTimer);
1532 } 1540 }
1533 } 1541 }
1534 1542
1535 dti.dta[category].d[index].lockStat = lock; 1543 dti.dta[category].d[index].lockStat = lock;
1536 dti.dta[category].d[index].rev++; // increment revision counter. 1544 dti.dta[category].d[index].rev++; // increment revision counter.
1537 1545
1538 emitDataChanged(this); 1546 emitDataChanged(this);
1539 if (!lock) 1547 if (!lock)
1540 timer()->start(DocTimer::id_autoLockTimer); 1548 timer()->start(DocTimer::id_autoLockTimer);
1541 1549
1542 return true; 1550 return true;
1543 1551
1544} 1552}
1545 1553
1546bool PwMDoc::lockAt(const QString &category,unsigned int index, 1554bool PwMDoc::lockAt(const QString &category,unsigned int index,
1547 bool lock) 1555 bool lock)
1548{ 1556{
1549 unsigned int cat = 0; 1557 unsigned int cat = 0;
1550 1558
1551 if (!findCategory(category, &cat)) { 1559 if (!findCategory(category, &cat)) {
1552 BUG(); 1560 BUG();
1553 return false; 1561 return false;
1554 } 1562 }
1555 1563
1556 return lockAt(cat, index, lock); 1564 return lockAt(cat, index, lock);
1557} 1565}
1558 1566
1559bool PwMDoc::lockAll(bool lock) 1567bool PwMDoc::lockAll(bool lock)
1560{ 1568{
1561 if (!lock && isDeepLocked()) { 1569 if (!lock && isDeepLocked()) {
1562 PwMerror ret; 1570 PwMerror ret;
1563 ret = deepLock(false); 1571 ret = deepLock(false);
1564 if (ret != e_success) 1572 if (ret != e_success)
1565 return false; 1573 return false;
1566 return true; 1574 return true;
1567 } 1575 }
1568 if (isDocEmpty()) { 1576 if (isDocEmpty()) {
1569 return true; 1577 return true;
1570 } 1578 }
1571 if (!lock && currentPw != "") { 1579 if (!lock && currentPw != "") {
1572 // unlocking and password is already set 1580 // unlocking and password is already set
1573 if (!getDocStatFlag(DOC_STAT_UNLOCK_WITHOUT_PW)) { 1581 if (!getDocStatFlag(DOC_STAT_UNLOCK_WITHOUT_PW)) {
1574 // unlocking without pw not allowed 1582 // unlocking without pw not allowed
1575 QString pw; 1583 QString pw;
1576 pw = requestMpw(getDocStatFlag(DOC_STAT_USE_CHIPCARD)); 1584 pw = requestMpw(getDocStatFlag(DOC_STAT_USE_CHIPCARD));
1577 if (pw != "") { 1585 if (pw != "") {
1578 if (pw != currentPw) { 1586 if (pw != currentPw) {
1579 wrongMpwMsgBox(getDocStatFlag(DOC_STAT_USE_CHIPCARD)); 1587 wrongMpwMsgBox(getDocStatFlag(DOC_STAT_USE_CHIPCARD));
1580 return false; 1588 return false;
1581 } else { 1589 } else {
1582 timer()->start(DocTimer::id_mpwTimer); 1590 timer()->start(DocTimer::id_mpwTimer);
1583 } 1591 }
1584 } else { 1592 } else {
1585 return false; 1593 return false;
1586 } 1594 }
1587 } else { 1595 } else {
1588 timer()->start(DocTimer::id_mpwTimer); 1596 timer()->start(DocTimer::id_mpwTimer);
1589 } 1597 }
1590 } 1598 }
1591 1599
1592 vector<PwMCategoryItem>::iterator catBegin = dti.dta.begin(), 1600 vector<PwMCategoryItem>::iterator catBegin = dti.dta.begin(),
1593 catEnd = dti.dta.end(), 1601 catEnd = dti.dta.end(),
1594 catI = catBegin; 1602 catI = catBegin;
1595 vector<PwMDataItem>::iterator entrBegin, entrEnd, entrI; 1603 vector<PwMDataItem>::iterator entrBegin, entrEnd, entrI;
1596 while (catI != catEnd) { 1604 while (catI != catEnd) {
1597 entrBegin = catI->d.begin(); 1605 entrBegin = catI->d.begin();
1598 entrEnd = catI->d.end(); 1606 entrEnd = catI->d.end();
1599 entrI = entrBegin; 1607 entrI = entrBegin;
1600 while (entrI != entrEnd) { 1608 while (entrI != entrEnd) {
1601 entrI->lockStat = lock; 1609 entrI->lockStat = lock;
1602 entrI->rev++; // increment revision counter. 1610 entrI->rev++; // increment revision counter.
1603 ++entrI; 1611 ++entrI;
1604 } 1612 }
1605 ++catI; 1613 ++catI;
1606 } 1614 }
1607 1615
1608 emitDataChanged(this); 1616 emitDataChanged(this);
1609 if (lock) 1617 if (lock)
1610 timer()->stop(DocTimer::id_autoLockTimer); 1618 timer()->stop(DocTimer::id_autoLockTimer);
1611 else 1619 else
1612 timer()->start(DocTimer::id_autoLockTimer); 1620 timer()->start(DocTimer::id_autoLockTimer);
1613 1621
1614 return true; 1622 return true;
1615} 1623}
1616 1624
1617bool PwMDoc::isLocked(const QString &category, unsigned int index) 1625bool PwMDoc::isLocked(const QString &category, unsigned int index)
1618{ 1626{
1619 unsigned int cat = 0; 1627 unsigned int cat = 0;
1620 1628
1621 if (!findCategory(category, &cat)) { 1629 if (!findCategory(category, &cat)) {
1622 BUG(); 1630 BUG();
1623 return false; 1631 return false;
1624 } 1632 }
1625 1633
1626 return isLocked(cat, index); 1634 return isLocked(cat, index);
1627} 1635}
1628 1636
1629bool PwMDoc::unlockAll_tempoary(bool revert) 1637bool PwMDoc::unlockAll_tempoary(bool revert)
1630{ 1638{
1631 static vector< vector<bool> > *oldLockStates = 0; 1639 static vector< vector<bool> > *oldLockStates = 0;
1632 static bool wasDeepLocked; 1640 static bool wasDeepLocked;
1633 1641
1634 if (revert) {// revert the unlocking 1642 if (revert) {// revert the unlocking
1635 if (oldLockStates) { 1643 if (oldLockStates) {
1636 /* we actually _have_ unlocked something, because 1644 /* we actually _have_ unlocked something, because
1637 * we have allocated space for the oldLockStates. 1645 * we have allocated space for the oldLockStates.
1638 * So, go on and revert them! 1646 * So, go on and revert them!
1639 */ 1647 */
1640 if (wasDeepLocked) { 1648 if (wasDeepLocked) {
1641 PwMerror ret = deepLock(true); 1649 PwMerror ret = deepLock(true);
1642 if (ret == e_success) { 1650 if (ret == e_success) {
1643 /* deep-lock succeed. We are save. 1651 /* deep-lock succeed. We are save.
1644 * (but if it failed, just go on 1652 * (but if it failed, just go on
1645 * lock them normally) 1653 * lock them normally)
1646 */ 1654 */
1647 delete_and_null(oldLockStates); 1655 delete_and_null(oldLockStates);
1648 timer()->start(DocTimer::id_autoLockTimer); 1656 timer()->start(DocTimer::id_autoLockTimer);
1649 printDebug("tempoary unlocking of dta " 1657 printDebug("tempoary unlocking of dta "
1650 "reverted by deep-locking."); 1658 "reverted by deep-locking.");
1651 return true; 1659 return true;
1652 } 1660 }
1653 printDebug("deep-lock failed while reverting! " 1661 printDebug("deep-lock failed while reverting! "
1654 "Falling back to normal-lock."); 1662 "Falling back to normal-lock.");
1655 } 1663 }
1656 if (unlikely(!wasDeepLocked && 1664 if (unlikely(!wasDeepLocked &&
1657 numCategories() != oldLockStates->size())) { 1665 numCategories() != oldLockStates->size())) {
1658 /* DOH! We have modified "dta" while 1666 /* DOH! We have modified "dta" while
1659 * it was unlocked tempoary. DON'T DO THIS! 1667 * it was unlocked tempoary. DON'T DO THIS!
1660 */ 1668 */
1661 BUG(); 1669 BUG();
1662 delete_and_null(oldLockStates); 1670 delete_and_null(oldLockStates);
1663 timer()->start(DocTimer::id_autoLockTimer); 1671 timer()->start(DocTimer::id_autoLockTimer);
1664 return false; 1672 return false;
1665 } 1673 }
1666 vector<PwMCategoryItem>::iterator catBegin = dti.dta.begin(), 1674 vector<PwMCategoryItem>::iterator catBegin = dti.dta.begin(),
1667 catEnd = dti.dta.end(), 1675 catEnd = dti.dta.end(),
1668 catI = catBegin; 1676 catI = catBegin;
1669 vector<PwMDataItem>::iterator entrBegin, entrEnd, entrI; 1677 vector<PwMDataItem>::iterator entrBegin, entrEnd, entrI;
1670 vector< vector<bool> >::iterator oldCatStatI = oldLockStates->begin(); 1678 vector< vector<bool> >::iterator oldCatStatI = oldLockStates->begin();
1671 vector<bool>::iterator oldEntrStatBegin, 1679 vector<bool>::iterator oldEntrStatBegin,
1672 oldEntrStatEnd, 1680 oldEntrStatEnd,
1673 oldEntrStatI; 1681 oldEntrStatI;
1674 while (catI != catEnd) { 1682 while (catI != catEnd) {
1675 entrBegin = catI->d.begin(); 1683 entrBegin = catI->d.begin();
1676 entrEnd = catI->d.end(); 1684 entrEnd = catI->d.end();
1677 entrI = entrBegin; 1685 entrI = entrBegin;
1678 if (likely(!wasDeepLocked)) { 1686 if (likely(!wasDeepLocked)) {
1679 oldEntrStatBegin = oldCatStatI->begin(); 1687 oldEntrStatBegin = oldCatStatI->begin();
1680 oldEntrStatEnd = oldCatStatI->end(); 1688 oldEntrStatEnd = oldCatStatI->end();
1681 oldEntrStatI = oldEntrStatBegin; 1689 oldEntrStatI = oldEntrStatBegin;
1682 if (unlikely(catI->d.size() != oldCatStatI->size())) { 1690 if (unlikely(catI->d.size() != oldCatStatI->size())) {
1683 /* DOH! We have modified "dta" while 1691 /* DOH! We have modified "dta" while
1684 * it was unlocked tempoary. DON'T DO THIS! 1692 * it was unlocked tempoary. DON'T DO THIS!
1685 */ 1693 */
1686 BUG(); 1694 BUG();
1687 delete_and_null(oldLockStates); 1695 delete_and_null(oldLockStates);
1688 timer()->start(DocTimer::id_autoLockTimer); 1696 timer()->start(DocTimer::id_autoLockTimer);
1689 return false; 1697 return false;
1690 } 1698 }
1691 } 1699 }
1692 while (entrI != entrEnd) { 1700 while (entrI != entrEnd) {
1693 if (wasDeepLocked) { 1701 if (wasDeepLocked) {
1694 /* this is an error-fallback if 1702 /* this is an error-fallback if
1695 * deeplock didn't succeed 1703 * deeplock didn't succeed
1696 */ 1704 */
1697 entrI->lockStat = true; 1705 entrI->lockStat = true;
1698 } else { 1706 } else {
1699 entrI->lockStat = *oldEntrStatI; 1707 entrI->lockStat = *oldEntrStatI;
1700 } 1708 }
1701 ++entrI; 1709 ++entrI;
1702 if (likely(!wasDeepLocked)) 1710 if (likely(!wasDeepLocked))
1703 ++oldEntrStatI; 1711 ++oldEntrStatI;
1704 } 1712 }
1705 ++catI; 1713 ++catI;
1706 if (likely(!wasDeepLocked)) 1714 if (likely(!wasDeepLocked))
1707 ++oldCatStatI; 1715 ++oldCatStatI;
1708 } 1716 }
1709 delete_and_null(oldLockStates); 1717 delete_and_null(oldLockStates);
1710 if (unlikely(wasDeepLocked)) { 1718 if (unlikely(wasDeepLocked)) {
1711 /* error fallback... */ 1719 /* error fallback... */
1712 unsetDocStatFlag(DOC_STAT_DEEPLOCKED); 1720 unsetDocStatFlag(DOC_STAT_DEEPLOCKED);
1713 emitDataChanged(this); 1721 emitDataChanged(this);
1714 printDebug("WARNING: unlockAll_tempoary(true) " 1722 printDebug("WARNING: unlockAll_tempoary(true) "
1715 "deeplock fallback!"); 1723 "deeplock fallback!");
1716 } 1724 }
1717 printDebug("tempoary unlocking of dta reverted."); 1725 printDebug("tempoary unlocking of dta reverted.");
1718 } else { 1726 } else {
1719 printDebug("unlockAll_tempoary(true): nothing to do."); 1727 printDebug("unlockAll_tempoary(true): nothing to do.");
1720 } 1728 }
1721 timer()->start(DocTimer::id_autoLockTimer); 1729 timer()->start(DocTimer::id_autoLockTimer);
1722 } else {// unlock all data tempoary 1730 } else {// unlock all data tempoary
1723 if (unlikely(oldLockStates != 0)) { 1731 if (unlikely(oldLockStates != 0)) {
1724 /* DOH! We have already unlocked the data tempoarly. 1732 /* DOH! We have already unlocked the data tempoarly.
1725 * No need to do it twice. ;) 1733 * No need to do it twice. ;)
1726 */ 1734 */
1727 BUG(); 1735 BUG();
1728 return false; 1736 return false;
1729 } 1737 }
1730 wasDeepLocked = false; 1738 wasDeepLocked = false;
1731 bool mustUnlock = false; 1739 bool mustUnlock = false;
1732 if (isDeepLocked()) { 1740 if (isDeepLocked()) {
1733 PwMerror ret; 1741 PwMerror ret;
1734 while (1) { 1742 while (1) {
1735 ret = deepLock(false); 1743 ret = deepLock(false);
1736 if (ret == e_success) { 1744 if (ret == e_success) {
1737 break; 1745 break;
1738 } else if (ret == e_wrongPw) { 1746 } else if (ret == e_wrongPw) {
1739 wrongMpwMsgBox(getDocStatFlag(DOC_STAT_USE_CHIPCARD)); 1747 wrongMpwMsgBox(getDocStatFlag(DOC_STAT_USE_CHIPCARD));
1740 } else { 1748 } else {
1741 printDebug("deep-unlocking failed while " 1749 printDebug("deep-unlocking failed while "
1742 "tempoary unlocking!"); 1750 "tempoary unlocking!");
1743 return false; 1751 return false;
1744 } 1752 }
1745 } 1753 }
1746 wasDeepLocked = true; 1754 wasDeepLocked = true;
1747 mustUnlock = true; 1755 mustUnlock = true;
1748 } else { 1756 } else {
1749 // first check if it's needed to unlock some entries 1757 // first check if it's needed to unlock some entries
1750 vector<PwMCategoryItem>::iterator catBegin = dti.dta.begin(), 1758 vector<PwMCategoryItem>::iterator catBegin = dti.dta.begin(),
1751 catEnd = dti.dta.end(), 1759 catEnd = dti.dta.end(),
1752 catI = catBegin; 1760 catI = catBegin;
1753 vector<PwMDataItem>::iterator entrBegin, entrEnd, entrI; 1761 vector<PwMDataItem>::iterator entrBegin, entrEnd, entrI;
1754 while (catI != catEnd) { 1762 while (catI != catEnd) {
1755 entrBegin = catI->d.begin(); 1763 entrBegin = catI->d.begin();
1756 entrEnd = catI->d.end(); 1764 entrEnd = catI->d.end();
1757 entrI = entrBegin; 1765 entrI = entrBegin;
1758 while (entrI != entrEnd) { 1766 while (entrI != entrEnd) {
1759 if (entrI->lockStat == true) { 1767 if (entrI->lockStat == true) {
1760 mustUnlock = true; 1768 mustUnlock = true;
1761 break; 1769 break;
1762 } 1770 }
1763 ++entrI; 1771 ++entrI;
1764 } 1772 }
1765 if (mustUnlock) 1773 if (mustUnlock)
1766 break; 1774 break;
1767 ++catI; 1775 ++catI;
1768 } 1776 }
1769 } 1777 }
1770 if (!mustUnlock) { 1778 if (!mustUnlock) {
1771 // nothing to do. 1779 // nothing to do.
1772 timer()->stop(DocTimer::id_autoLockTimer); 1780 timer()->stop(DocTimer::id_autoLockTimer);
1773 printDebug("unlockAll_tempoary(): nothing to do."); 1781 printDebug("unlockAll_tempoary(): nothing to do.");
1774 return true; 1782 return true;
1775 } else if (!wasDeepLocked) { 1783 } else if (!wasDeepLocked) {
1776 if (!getDocStatFlag(DOC_STAT_UNLOCK_WITHOUT_PW) && 1784 if (!getDocStatFlag(DOC_STAT_UNLOCK_WITHOUT_PW) &&
1777 currentPw != "") { 1785 currentPw != "") {
1778 /* we can't unlock without mpw, so 1786 /* we can't unlock without mpw, so
1779 * we need to ask for it. 1787 * we need to ask for it.
1780 */ 1788 */
1781 QString pw; 1789 QString pw;
1782 while (1) { 1790 while (1) {
1783 pw = requestMpw(getDocStatFlag(DOC_STAT_USE_CHIPCARD)); 1791 pw = requestMpw(getDocStatFlag(DOC_STAT_USE_CHIPCARD));
1784 if (pw == "") { 1792 if (pw == "") {
1785 return false; 1793 return false;
1786 } else if (pw == currentPw) { 1794 } else if (pw == currentPw) {
1787 break; 1795 break;
1788 } 1796 }
1789 wrongMpwMsgBox(getDocStatFlag(DOC_STAT_USE_CHIPCARD)); 1797 wrongMpwMsgBox(getDocStatFlag(DOC_STAT_USE_CHIPCARD));
1790 } 1798 }
1791 } 1799 }
1792 } 1800 }
1793 timer()->stop(DocTimer::id_autoLockTimer); 1801 timer()->stop(DocTimer::id_autoLockTimer);
1794 oldLockStates = new vector< vector<bool> >; 1802 oldLockStates = new vector< vector<bool> >;
1795 vector<bool> tmp_vec; 1803 vector<bool> tmp_vec;
1796 vector<PwMCategoryItem>::iterator catBegin = dti.dta.begin(), 1804 vector<PwMCategoryItem>::iterator catBegin = dti.dta.begin(),
1797 catEnd = dti.dta.end(), 1805 catEnd = dti.dta.end(),
1798 catI = catBegin; 1806 catI = catBegin;
1799 vector<PwMDataItem>::iterator entrBegin, entrEnd, entrI; 1807 vector<PwMDataItem>::iterator entrBegin, entrEnd, entrI;
1800 while (catI != catEnd) { 1808 while (catI != catEnd) {
1801 entrBegin = catI->d.begin(); 1809 entrBegin = catI->d.begin();
1802 entrEnd = catI->d.end(); 1810 entrEnd = catI->d.end();
1803 entrI = entrBegin; 1811 entrI = entrBegin;
1804 while (entrI != entrEnd) { 1812 while (entrI != entrEnd) {
1805 if (!wasDeepLocked) { 1813 if (!wasDeepLocked) {
1806 tmp_vec.push_back(entrI->lockStat); 1814 tmp_vec.push_back(entrI->lockStat);
1807 } 1815 }
1808 entrI->lockStat = false; 1816 entrI->lockStat = false;
1809 ++entrI; 1817 ++entrI;
1810 } 1818 }
1811 if (!wasDeepLocked) { 1819 if (!wasDeepLocked) {
1812 oldLockStates->push_back(tmp_vec); 1820 oldLockStates->push_back(tmp_vec);
1813 tmp_vec.clear(); 1821 tmp_vec.clear();
1814 } 1822 }
1815 ++catI; 1823 ++catI;
1816 } 1824 }
1817 printDebug("tempoary unlocked dta."); 1825 printDebug("tempoary unlocked dta.");
1818 } 1826 }
1819 1827
1820 return true; 1828 return true;
1821} 1829}
1822 1830
1823PwMerror PwMDoc::deepLock(bool lock, bool saveToFile) 1831PwMerror PwMDoc::deepLock(bool lock, bool saveToFile)
1824{ 1832{
1825 PwMerror ret; 1833 PwMerror ret;
1826 /* NOTE: saveDoc() depends on this function to return 1834 /* NOTE: saveDoc() depends on this function to return
1827 * e_success if saveToFile == false 1835 * e_success if saveToFile == false
1828 */ 1836 */
1829 1837
1830 if (lock) { 1838 if (lock) {
1831 if (isDeepLocked()) 1839 if (isDeepLocked())
1832 return e_lock; 1840 return e_lock;
1833 if (saveToFile) { 1841 if (saveToFile) {
1834 if (isDocEmpty()) 1842 if (isDocEmpty())
1835 return e_docIsEmpty; 1843 return e_docIsEmpty;
1836 ret = saveDoc(conf()->confGlobCompression()); 1844 ret = saveDoc(conf()->confGlobCompression());
1837 if (ret == e_filename) { 1845 if (ret == e_filename) {
1838 /* the doc wasn't saved to a file 1846 /* the doc wasn't saved to a file
1839 * by the user, yet. 1847 * by the user, yet.
1840 */ 1848 */
1841 cantDeeplock_notSavedMsgBox(); 1849 cantDeeplock_notSavedMsgBox();
1842 return e_docNotSaved; 1850 return e_docNotSaved;
1843 } else if (ret != e_success) { 1851 } else if (ret != e_success) {
1844 return e_lock; 1852 return e_lock;
1845 } 1853 }
1846 } 1854 }
1847 timer()->stop(DocTimer::id_autoLockTimer); 1855 timer()->stop(DocTimer::id_autoLockTimer);
1848 clearDoc(); 1856 clearDoc();
1849 PwMDataItem d; 1857 PwMDataItem d;
1850 d.desc = IS_DEEPLOCKED_SHORTMSG.latin1(); 1858 d.desc = IS_DEEPLOCKED_SHORTMSG.latin1();
1851 d.comment = IS_DEEPLOCKED_MSG.latin1(); 1859 d.comment = IS_DEEPLOCKED_MSG.latin1();
1852 d.listViewPos = 0; 1860 d.listViewPos = 0;
1853 addEntry(DEFAULT_CATEGORY, &d, true); 1861 addEntry(DEFAULT_CATEGORY, &d, true);
1854 lockAt(DEFAULT_CATEGORY, 0, true); 1862 lockAt(DEFAULT_CATEGORY, 0, true);
1855 unsetDocStatFlag(DOC_STAT_DISK_DIRTY); 1863 unsetDocStatFlag(DOC_STAT_DISK_DIRTY);
1856 setDocStatFlag(DOC_STAT_DEEPLOCKED); 1864 setDocStatFlag(DOC_STAT_DEEPLOCKED);
1857 } else { 1865 } else {
1858 if (!isDeepLocked()) 1866 if (!isDeepLocked())
1859 return e_lock; 1867 return e_lock;
1860 ret = openDoc(&filename, (conf()->confGlobUnlockOnOpen()) 1868 ret = openDoc(&filename, (conf()->confGlobUnlockOnOpen())
1861 ? 0 : 1); 1869 ? 0 : 1);
1862 if (ret == e_wrongPw) { 1870 if (ret == e_wrongPw) {
1863 return e_wrongPw; 1871 return e_wrongPw;
1864 } else if (ret != e_success) { 1872 } else if (ret != e_success) {
1865 printDebug(string("PwMDoc::deepLock(false): ERR! openDoc() == ") 1873 printDebug(string("PwMDoc::deepLock(false): ERR! openDoc() == ")
1866 + tostr(static_cast<int>(ret))); 1874 + tostr(static_cast<int>(ret)));
1867 return e_lock; 1875 return e_lock;
1868 } 1876 }
1869 unsetDocStatFlag(DOC_STAT_DEEPLOCKED); 1877 unsetDocStatFlag(DOC_STAT_DEEPLOCKED);
1870 timer()->start(DocTimer::id_autoLockTimer); 1878 timer()->start(DocTimer::id_autoLockTimer);
1871 } 1879 }
1872 1880
1873 emitDataChanged(this); 1881 emitDataChanged(this);
1874 return e_success; 1882 return e_success;
1875} 1883}
1876 1884
1877void PwMDoc::_deepUnlock() 1885void PwMDoc::_deepUnlock()
1878{ 1886{
1879 deepLock(false); 1887 deepLock(false);
1880} 1888}
1881 1889
1882void PwMDoc::clearDoc() 1890void PwMDoc::clearDoc()
1883{ 1891{
1884 dti.clear(); 1892 dti.clear();
1885 PwMCategoryItem d; 1893 PwMCategoryItem d;
1886 //US ENH: to initialize all members with meaningfull data. 1894 //US ENH: to initialize all members with meaningfull data.
1887 d.clear(); 1895 d.clear();
1888 d.name = DEFAULT_CATEGORY.latin1(); 1896 d.name = DEFAULT_CATEGORY.latin1();
1889 dti.dta.push_back(d); 1897 dti.dta.push_back(d);
1890 currentPw = ""; 1898 currentPw = "";
1891 unsetDocStatFlag(DOC_STAT_UNLOCK_WITHOUT_PW); 1899 unsetDocStatFlag(DOC_STAT_UNLOCK_WITHOUT_PW);
1892} 1900}
1893 1901
1894void PwMDoc::changeCurrentPw() 1902void PwMDoc::changeCurrentPw()
1895{ 1903{
1896 if (currentPw == "") 1904 if (currentPw == "")
1897 return; // doc hasn't been saved. No mpw available. 1905 return; // doc hasn't been saved. No mpw available.
1898 bool useChipcard = getDocStatFlag(DOC_STAT_USE_CHIPCARD); 1906 bool useChipcard = getDocStatFlag(DOC_STAT_USE_CHIPCARD);
1899 QString pw = requestMpwChange(&currentPw, &useChipcard); 1907 QString pw = requestMpwChange(&currentPw, &useChipcard);
1900 if (pw == "") 1908 if (pw == "")
1901 return; 1909 return;
1902 if (useChipcard) 1910 if (useChipcard)
1903 setDocStatFlag(DOC_STAT_USE_CHIPCARD); 1911 setDocStatFlag(DOC_STAT_USE_CHIPCARD);
1904 else 1912 else
1905 unsetDocStatFlag(DOC_STAT_USE_CHIPCARD); 1913 unsetDocStatFlag(DOC_STAT_USE_CHIPCARD);
1906 setCurrentPw(pw); 1914 setCurrentPw(pw);
1907} 1915}
1908 1916
1909void PwMDoc::setListViewPos(const QString &category, unsigned int index, 1917void PwMDoc::setListViewPos(const QString &category, unsigned int index,
1910 int pos) 1918 int pos)
1911{ 1919{
1912 unsigned int cat = 0; 1920 unsigned int cat = 0;
1913 1921
1914 if (!findCategory(category, &cat)) { 1922 if (!findCategory(category, &cat)) {
1915 BUG(); 1923 BUG();
1916 return; 1924 return;
1917 } 1925 }
1918 setListViewPos(cat, index, pos); 1926 setListViewPos(cat, index, pos);
1919} 1927}
1920 1928
1921void PwMDoc::setListViewPos(unsigned int category, unsigned int index, 1929void PwMDoc::setListViewPos(unsigned int category, unsigned int index,
1922 int pos) 1930 int pos)
1923{ 1931{
1924 dti.dta[category].d[index].listViewPos = pos; 1932 dti.dta[category].d[index].listViewPos = pos;
1925 1933
1926/* FIXME workaround: don't flag dirty, because this function sometimes 1934/* FIXME workaround: don't flag dirty, because this function sometimes
1927 * get's called when it shouldn't. It's because PwMView assumes 1935 * get's called when it shouldn't. It's because PwMView assumes
1928 * the user resorted the UI on behalf of signal layoutChanged(). 1936 * the user resorted the UI on behalf of signal layoutChanged().
1929 * This is somewhat broken and incorrect, but I've no other 1937 * This is somewhat broken and incorrect, but I've no other
1930 * solution for now. 1938 * solution for now.
1931 */ 1939 */
1932 //setDocStatFlag(DOC_STAT_DISK_DIRTY); 1940 //setDocStatFlag(DOC_STAT_DISK_DIRTY);
1933} 1941}
1934 1942
1935int PwMDoc::getListViewPos(const QString &category, unsigned int index) 1943int PwMDoc::getListViewPos(const QString &category, unsigned int index)
1936{ 1944{
1937 unsigned int cat = 0; 1945 unsigned int cat = 0;
1938 1946
1939 if (!findCategory(category, &cat)) { 1947 if (!findCategory(category, &cat)) {
1940 BUG(); 1948 BUG();
1941 return -1; 1949 return -1;
1942 } 1950 }
1943 1951
1944 return dti.dta[cat].d[index].listViewPos; 1952 return dti.dta[cat].d[index].listViewPos;
1945} 1953}
1946 1954
1947void PwMDoc::findEntry(unsigned int category, PwMDataItem find, unsigned int searchIn, 1955void PwMDoc::findEntry(unsigned int category, PwMDataItem find, unsigned int searchIn,
1948 vector<unsigned int> *foundPositions, bool breakAfterFound, 1956 vector<unsigned int> *foundPositions, bool breakAfterFound,
1949 bool caseSensitive, bool exactWordMatch, bool sortByLvp) 1957 bool caseSensitive, bool exactWordMatch, bool sortByLvp)
1950{ 1958{
1951 PWM_ASSERT(foundPositions); 1959 PWM_ASSERT(foundPositions);
1952 PWM_ASSERT(searchIn); 1960 PWM_ASSERT(searchIn);
1953 foundPositions->clear(); 1961 foundPositions->clear();
1954 1962
1955 unsigned int i, entries = numEntries(category); 1963 unsigned int i, entries = numEntries(category);
1956 for (i = 0; i < entries; ++i) { 1964 for (i = 0; i < entries; ++i) {
1957 if (searchIn & SEARCH_IN_DESC) { 1965 if (searchIn & SEARCH_IN_DESC) {
1958 if (!compareString(find.desc, dti.dta[category].d[i].desc, 1966 if (!compareString(find.desc, dti.dta[category].d[i].desc,
1959 caseSensitive, exactWordMatch)) { 1967 caseSensitive, exactWordMatch)) {
1960 continue; 1968 continue;
1961 } 1969 }
1962 } 1970 }
1963 if (searchIn & SEARCH_IN_NAME) { 1971 if (searchIn & SEARCH_IN_NAME) {
1964 if (!compareString(find.name, dti.dta[category].d[i].name, 1972 if (!compareString(find.name, dti.dta[category].d[i].name,
1965 caseSensitive, exactWordMatch)) { 1973 caseSensitive, exactWordMatch)) {
1966 continue; 1974 continue;
1967 } 1975 }
1968 } 1976 }
1969 if (searchIn & SEARCH_IN_PW) { 1977 if (searchIn & SEARCH_IN_PW) {
1970 bool wasLocked = isLocked(category, i); 1978 bool wasLocked = isLocked(category, i);
1971 getDataChangedLock(); 1979 getDataChangedLock();
1972 lockAt(category, i, false); 1980 lockAt(category, i, false);
1973 if (!compareString(find.pw, dti.dta[category].d[i].pw, 1981 if (!compareString(find.pw, dti.dta[category].d[i].pw,
1974 caseSensitive, exactWordMatch)) { 1982 caseSensitive, exactWordMatch)) {
1975 lockAt(category, i, wasLocked); 1983 lockAt(category, i, wasLocked);
1976 putDataChangedLock(); 1984 putDataChangedLock();
1977 continue; 1985 continue;
1978 } 1986 }
1979 lockAt(category, i, wasLocked); 1987 lockAt(category, i, wasLocked);
1980 putDataChangedLock(); 1988 putDataChangedLock();
1981 } 1989 }
1982 if (searchIn & SEARCH_IN_COMMENT) { 1990 if (searchIn & SEARCH_IN_COMMENT) {
1983 if (!compareString(find.comment, dti.dta[category].d[i].comment, 1991 if (!compareString(find.comment, dti.dta[category].d[i].comment,
1984 caseSensitive, exactWordMatch)) { 1992 caseSensitive, exactWordMatch)) {
1985 continue; 1993 continue;
1986 } 1994 }
1987 } 1995 }
1988 if (searchIn & SEARCH_IN_URL) { 1996 if (searchIn & SEARCH_IN_URL) {
1989 if (!compareString(find.url, dti.dta[category].d[i].url, 1997 if (!compareString(find.url, dti.dta[category].d[i].url,
1990 caseSensitive, exactWordMatch)) { 1998 caseSensitive, exactWordMatch)) {
1991 continue; 1999 continue;
1992 } 2000 }
1993 } 2001 }
1994 if (searchIn & SEARCH_IN_LAUNCHER) { 2002 if (searchIn & SEARCH_IN_LAUNCHER) {
1995 if (!compareString(find.launcher, dti.dta[category].d[i].launcher, 2003 if (!compareString(find.launcher, dti.dta[category].d[i].launcher,
1996 caseSensitive, exactWordMatch)) { 2004 caseSensitive, exactWordMatch)) {
1997 continue; 2005 continue;
1998 } 2006 }
1999 } 2007 }
2000 2008
2001 // all selected "searchIn" matched. 2009 // all selected "searchIn" matched.
2002 foundPositions->push_back(i); 2010 foundPositions->push_back(i);
2003 if (breakAfterFound) 2011 if (breakAfterFound)
2004 break; 2012 break;
2005 } 2013 }
2006 2014
2007 if (sortByLvp && foundPositions->size() > 1) { 2015 if (sortByLvp && foundPositions->size() > 1) {
2008 vector< pair<unsigned int /* foundPosition (real doc pos) */, 2016 vector< pair<unsigned int /* foundPosition (real doc pos) */,
2009 unsigned int /* lvp-pos */> > tmp_vec; 2017 unsigned int /* lvp-pos */> > tmp_vec;
2010 2018
2011 unsigned int i, items = foundPositions->size(); 2019 unsigned int i, items = foundPositions->size();
2012 pair<unsigned int, unsigned int> tmp_pair; 2020 pair<unsigned int, unsigned int> tmp_pair;
2013 for (i = 0; i < items; ++i) { 2021 for (i = 0; i < items; ++i) {
2014 tmp_pair.first = (*foundPositions)[i]; 2022 tmp_pair.first = (*foundPositions)[i];
2015 tmp_pair.second = dti.dta[category].d[(*foundPositions)[i]].listViewPos; 2023 tmp_pair.second = dti.dta[category].d[(*foundPositions)[i]].listViewPos;
2016 tmp_vec.push_back(tmp_pair); 2024 tmp_vec.push_back(tmp_pair);
2017 } 2025 }
2018 sort(tmp_vec.begin(), tmp_vec.end(), dta_lvp_greater()); 2026 sort(tmp_vec.begin(), tmp_vec.end(), dta_lvp_greater());
2019 foundPositions->clear(); 2027 foundPositions->clear();
2020 for (i = 0; i < items; ++i) { 2028 for (i = 0; i < items; ++i) {
2021 foundPositions->push_back(tmp_vec[i].first); 2029 foundPositions->push_back(tmp_vec[i].first);
2022 } 2030 }
2023 } 2031 }
2024} 2032}
2025 2033
2026void PwMDoc::findEntry(const QString &category, PwMDataItem find, unsigned int searchIn, 2034void PwMDoc::findEntry(const QString &category, PwMDataItem find, unsigned int searchIn,
2027 vector<unsigned int> *foundPositions, bool breakAfterFound, 2035 vector<unsigned int> *foundPositions, bool breakAfterFound,
2028 bool caseSensitive, bool exactWordMatch, bool sortByLvp) 2036 bool caseSensitive, bool exactWordMatch, bool sortByLvp)
2029{ 2037{
2030 PWM_ASSERT(foundPositions); 2038 PWM_ASSERT(foundPositions);
2031 unsigned int cat = 0; 2039 unsigned int cat = 0;
2032 2040
2033 if (!findCategory(category, &cat)) { 2041 if (!findCategory(category, &cat)) {
2034 foundPositions->clear(); 2042 foundPositions->clear();
2035 return; 2043 return;
2036 } 2044 }
2037 2045
2038 findEntry(cat, find, searchIn, foundPositions, breakAfterFound, 2046 findEntry(cat, find, searchIn, foundPositions, breakAfterFound,
2039 caseSensitive, exactWordMatch, sortByLvp); 2047 caseSensitive, exactWordMatch, sortByLvp);
2040} 2048}
2041 2049
2042bool PwMDoc::compareString(const string &s1, const string &s2, bool caseSensitive, 2050bool PwMDoc::compareString(const string &s1, const string &s2, bool caseSensitive,
2043 bool exactWordMatch) 2051 bool exactWordMatch)
2044{ 2052{
2045 QString _s1(s1.c_str()); 2053 QString _s1(s1.c_str());
2046 QString _s2(s2.c_str()); 2054 QString _s2(s2.c_str());
2047 if (!caseSensitive) { 2055 if (!caseSensitive) {
2048 _s1 = _s1.lower(); 2056 _s1 = _s1.lower();
2049 _s2 = _s2.lower(); 2057 _s2 = _s2.lower();
2050 } 2058 }
2051 if (exactWordMatch ? (_s1 == _s2) : (_s2.find(_s1) != -1)) 2059 if (exactWordMatch ? (_s1 == _s2) : (_s2.find(_s1) != -1))
2052 return true; 2060 return true;
2053 return false; 2061 return false;
2054} 2062}
2055 2063
2056bool PwMDoc::findCategory(const QString &name, unsigned int *index) 2064bool PwMDoc::findCategory(const QString &name, unsigned int *index)
2057{ 2065{
2058 vector<PwMCategoryItem>::iterator i = dti.dta.begin(), 2066 vector<PwMCategoryItem>::iterator i = dti.dta.begin(),
2059 end = dti.dta.end(); 2067 end = dti.dta.end();
2060 while (i != end) { 2068 while (i != end) {
2061 if ((*i).name == name.latin1()) { 2069 if ((*i).name == name.latin1()) {
2062 if (index) { 2070 if (index) {
2063 *index = i - dti.dta.begin(); 2071 *index = i - dti.dta.begin();
2064 } 2072 }
2065 return true; 2073 return true;
2066 } 2074 }
2067 ++i; 2075 ++i;
2068 } 2076 }
2069 return false; 2077 return false;
2070} 2078}
2071 2079
2072bool PwMDoc::renameCategory(const QString &category, const QString &newName) 2080bool PwMDoc::renameCategory(const QString &category, const QString &newName)
2073{ 2081{
2074 unsigned int cat = 0; 2082 unsigned int cat = 0;
2075 2083
2076 if (!findCategory(category, &cat)) 2084 if (!findCategory(category, &cat))
2077 return false; 2085 return false;
2078 2086
2079 return renameCategory(cat, newName); 2087 return renameCategory(cat, newName);
2080} 2088}
2081 2089
2082bool PwMDoc::renameCategory(unsigned int category, const QString &newName, 2090bool PwMDoc::renameCategory(unsigned int category, const QString &newName,
2083 bool dontFlagDirty) 2091 bool dontFlagDirty)
2084{ 2092{
2085 if (category > numCategories() - 1) 2093 if (category > numCategories() - 1)
2086 return false; 2094 return false;
2087 2095
2088 dti.dta[category].name = newName.latin1(); 2096 dti.dta[category].name = newName.latin1();
2089 if (!dontFlagDirty) 2097 if (!dontFlagDirty)
2090 flagDirty(); 2098 flagDirty();
2091 2099
2092 return true; 2100 return true;
2093} 2101}
2094 2102
2095bool PwMDoc::delCategory(const QString &category) 2103bool PwMDoc::delCategory(const QString &category)
2096{ 2104{
2097 unsigned int cat = 0; 2105 unsigned int cat = 0;
2098 2106
2099 if (!findCategory(category, &cat)) 2107 if (!findCategory(category, &cat))
2100 return false; 2108 return false;
2101 2109
2102 return delCategory(cat); 2110 return delCategory(cat);
2103} 2111}
2104 2112
2105bool PwMDoc::delCategory(unsigned int category, bool dontFlagDirty) 2113bool PwMDoc::delCategory(unsigned int category, bool dontFlagDirty)
2106{ 2114{
2107 if (category > numCategories() - 1) 2115 if (category > numCategories() - 1)
2108 return false; 2116 return false;
2109 2117
2110 // We don't delete it, if it is the last existing 2118 // We don't delete it, if it is the last existing
2111 // category! Instead we rename it to "Default". 2119 // category! Instead we rename it to "Default".
2112 if (numCategories() > 1) { 2120 if (numCategories() > 1) {
2113 dti.dta.erase(dti.dta.begin() + category); 2121 dti.dta.erase(dti.dta.begin() + category);
2114 } else { 2122 } else {
2115 renameCategory(category, DEFAULT_CATEGORY, dontFlagDirty); 2123 renameCategory(category, DEFAULT_CATEGORY, dontFlagDirty);
2116 return true; 2124 return true;
2117 } 2125 }
2118 if (!dontFlagDirty) 2126 if (!dontFlagDirty)
2119 flagDirty(); 2127 flagDirty();
2120 2128
2121 return true; 2129 return true;
2122} 2130}
2123 2131
2124void PwMDoc::delAllEmptyCat(bool dontFlagDirty) 2132void PwMDoc::delAllEmptyCat(bool dontFlagDirty)
2125{ 2133{
2126 vector<PwMCategoryItem>::iterator begin = dti.dta.begin(), 2134 vector<PwMCategoryItem>::iterator begin = dti.dta.begin(),
2127 end = dti.dta.end(), 2135 end = dti.dta.end(),
2128 i = begin; 2136 i = begin;
2129 while (i != end) { 2137 while (i != end) {
2130 if (i->d.empty()) { 2138 if (i->d.empty()) {
2131 delCategory(begin - i, dontFlagDirty); 2139 delCategory(begin - i, dontFlagDirty);
2132 } 2140 }
2133 ++i; 2141 ++i;
2134 } 2142 }
2135} 2143}
2136 2144
2137void PwMDoc::getCategoryList(vector<string> *list) 2145void PwMDoc::getCategoryList(vector<string> *list)
2138{ 2146{
2139 PWM_ASSERT(list); 2147 PWM_ASSERT(list);
2140 list->clear(); 2148 list->clear();
2141 vector<PwMCategoryItem>::iterator i = dti.dta.begin(), 2149 vector<PwMCategoryItem>::iterator i = dti.dta.begin(),
2142 end = dti.dta.end(); 2150 end = dti.dta.end();
2143 while (i != end) { 2151 while (i != end) {
2144 list->push_back(i->name); 2152 list->push_back(i->name);
2145 ++i; 2153 ++i;
2146 } 2154 }
2147} 2155}
2148 2156
2149void PwMDoc::getCategoryList(QStringList *list) 2157void PwMDoc::getCategoryList(QStringList *list)
2150{ 2158{
2151 PWM_ASSERT(list); 2159 PWM_ASSERT(list);
2152 list->clear(); 2160 list->clear();
2153 vector<PwMCategoryItem>::iterator i = dti.dta.begin(), 2161 vector<PwMCategoryItem>::iterator i = dti.dta.begin(),
2154 end = dti.dta.end(); 2162 end = dti.dta.end();
2155 while (i != end) { 2163 while (i != end) {
2156#ifndef PWM_EMBEDDED 2164#ifndef PWM_EMBEDDED
2157 list->push_back(i->name.c_str()); 2165 list->push_back(i->name.c_str());
2158#else 2166#else
2159 list->append(i->name.c_str()); 2167 list->append(i->name.c_str());
2160#endif 2168#endif
2161 ++i; 2169 ++i;
2162 } 2170 }
2163} 2171}
2164 2172
2165void PwMDoc::getEntryList(const QString &category, QStringList *list) 2173void PwMDoc::getEntryList(const QString &category, QStringList *list)
2166{ 2174{
2167 PWM_ASSERT(list); 2175 PWM_ASSERT(list);
2168 unsigned int cat = 0; 2176 unsigned int cat = 0;
2169 if (!findCategory(category, &cat)) { 2177 if (!findCategory(category, &cat)) {
2170 list->clear(); 2178 list->clear();
2171 return; 2179 return;
2172 } 2180 }
2173 getEntryList(cat, list); 2181 getEntryList(cat, list);
2174} 2182}
2175 2183
2176void PwMDoc::getEntryList(const QString &category, vector<string> *list) 2184void PwMDoc::getEntryList(const QString &category, vector<string> *list)
2177{ 2185{
2178 PWM_ASSERT(list); 2186 PWM_ASSERT(list);
2179 unsigned int cat = 0; 2187 unsigned int cat = 0;
2180 if (!findCategory(category, &cat)) { 2188 if (!findCategory(category, &cat)) {
2181 list->clear(); 2189 list->clear();
2182 return; 2190 return;
2183 } 2191 }
2184 getEntryList(cat, list); 2192 getEntryList(cat, list);
2185} 2193}
2186 2194
2187void PwMDoc::getEntryList(unsigned int category, vector<string> *list) 2195void PwMDoc::getEntryList(unsigned int category, vector<string> *list)
2188{ 2196{
2189 PWM_ASSERT(list); 2197 PWM_ASSERT(list);
2190 list->clear(); 2198 list->clear();
2191 vector<PwMDataItem>::iterator begin = dti.dta[category].d.begin(), 2199 vector<PwMDataItem>::iterator begin = dti.dta[category].d.begin(),
2192 end = dti.dta[category].d.end(), 2200 end = dti.dta[category].d.end(),
2193 i = begin; 2201 i = begin;
2194 while (i != end) { 2202 while (i != end) {
2195 list->push_back(i->desc); 2203 list->push_back(i->desc);
2196 ++i; 2204 ++i;
2197 } 2205 }
2198} 2206}
2199 2207
2200void PwMDoc::getEntryList(unsigned int category, QStringList *list) 2208void PwMDoc::getEntryList(unsigned int category, QStringList *list)
2201{ 2209{
2202 PWM_ASSERT(list); 2210 PWM_ASSERT(list);
2203 list->clear(); 2211 list->clear();
2204 vector<PwMDataItem>::iterator begin = dti.dta[category].d.begin(), 2212 vector<PwMDataItem>::iterator begin = dti.dta[category].d.begin(),
2205 end = dti.dta[category].d.end(), 2213 end = dti.dta[category].d.end(),
2206 i = begin; 2214 i = begin;
2207 while (i != end) { 2215 while (i != end) {
2208#ifndef PWM_EMBEDDED 2216#ifndef PWM_EMBEDDED
2209 list->push_back(i->desc.c_str()); 2217 list->push_back(i->desc.c_str());
2210#else 2218#else
2211 list->append(i->desc.c_str()); 2219 list->append(i->desc.c_str());
2212#endif 2220#endif
2213 ++i; 2221 ++i;
2214 } 2222 }
2215} 2223}
2216 2224
2217bool PwMDoc::execLauncher(const QString &category, unsigned int entryIndex) 2225bool PwMDoc::execLauncher(const QString &category, unsigned int entryIndex)
2218{ 2226{
2219 unsigned int cat = 0; 2227 unsigned int cat = 0;
2220 2228
2221 if (!findCategory(category, &cat)) 2229 if (!findCategory(category, &cat))
2222 return false; 2230 return false;
2223 2231
2224 return execLauncher(cat, entryIndex); 2232 return execLauncher(cat, entryIndex);
2225} 2233}
2226 2234
2227bool PwMDoc::execLauncher(unsigned int category, unsigned int entryIndex) 2235bool PwMDoc::execLauncher(unsigned int category, unsigned int entryIndex)
2228{ 2236{
2229#ifndef _WIN32_ 2237#ifndef _WIN32_
2230 if (geteuid() == 0) { 2238 if (geteuid() == 0) {
2231 rootAlertMsgBox(); 2239 rootAlertMsgBox();
2232 return false; 2240 return false;
2233 } 2241 }
2234#endif 2242#endif
2235 QString command(dti.dta[category].d[entryIndex].launcher.c_str()); 2243 QString command(dti.dta[category].d[entryIndex].launcher.c_str());
2236 bool wasLocked = isLocked(category, entryIndex); 2244 bool wasLocked = isLocked(category, entryIndex);
2237 2245
2238 if (command.find("$p") != -1) { 2246 if (command.find("$p") != -1) {
2239 /* the user requested the password to be included 2247 /* the user requested the password to be included
2240 * into the command. We have to ask for the password, 2248 * into the command. We have to ask for the password,
2241 * if it's locked. We do that by unlocking the entry 2249 * if it's locked. We do that by unlocking the entry
2242 */ 2250 */
2243 if (!lockAt(category, entryIndex, false)) 2251 if (!lockAt(category, entryIndex, false))
2244 return false; 2252 return false;
2245 } 2253 }
2246#ifndef PWM_EMBEDDED 2254#ifndef PWM_EMBEDDED
2247 command.replace("$d", dti.dta[category].d[entryIndex].desc.c_str()); 2255 command.replace("$d", dti.dta[category].d[entryIndex].desc.c_str());
2248 command.replace("$n", dti.dta[category].d[entryIndex].name.c_str()); 2256 command.replace("$n", dti.dta[category].d[entryIndex].name.c_str());
2249 command.replace("$p", dti.dta[category].d[entryIndex].pw.c_str()); 2257 command.replace("$p", dti.dta[category].d[entryIndex].pw.c_str());
2250 command.replace("$u", dti.dta[category].d[entryIndex].url.c_str()); 2258 command.replace("$u", dti.dta[category].d[entryIndex].url.c_str());
2251 command.replace("$c", dti.dta[category].d[entryIndex].comment.c_str()); 2259 command.replace("$c", dti.dta[category].d[entryIndex].comment.c_str());
2252#else 2260#else
2253 command.replace(QRegExp("$d"), dti.dta[category].d[entryIndex].desc.c_str()); 2261 command.replace(QRegExp("$d"), dti.dta[category].d[entryIndex].desc.c_str());
2254 command.replace(QRegExp("$n"), dti.dta[category].d[entryIndex].name.c_str()); 2262 command.replace(QRegExp("$n"), dti.dta[category].d[entryIndex].name.c_str());
2255 command.replace(QRegExp("$p"), dti.dta[category].d[entryIndex].pw.c_str()); 2263 command.replace(QRegExp("$p"), dti.dta[category].d[entryIndex].pw.c_str());
2256 command.replace(QRegExp("$u"), dti.dta[category].d[entryIndex].url.c_str()); 2264 command.replace(QRegExp("$u"), dti.dta[category].d[entryIndex].url.c_str());
2257 command.replace(QRegExp("$c"), dti.dta[category].d[entryIndex].comment.c_str()); 2265 command.replace(QRegExp("$c"), dti.dta[category].d[entryIndex].comment.c_str());
2258#endif 2266#endif
2259 command.append(" &"); 2267 command.append(" &");
2260 2268
2261 QString customXterm(conf()->confGlobXtermCommand()); 2269 QString customXterm(conf()->confGlobXtermCommand());
2262 if (!customXterm.isEmpty()) 2270 if (!customXterm.isEmpty())
2263 command = customXterm + " " + command; 2271 command = customXterm + " " + command;
2264 2272
2265 system(command.latin1()); 2273 system(command.latin1());
2266 2274
2267 lockAt(category, entryIndex, wasLocked); 2275 lockAt(category, entryIndex, wasLocked);
2268 return true; 2276 return true;
2269} 2277}
2270 2278
2271bool PwMDoc::goToURL(const QString &category, unsigned int entryIndex) 2279bool PwMDoc::goToURL(const QString &category, unsigned int entryIndex)
2272{ 2280{
2273 unsigned int cat = 0; 2281 unsigned int cat = 0;
2274 2282
2275 if (!findCategory(category, &cat)) 2283 if (!findCategory(category, &cat))
2276 return false; 2284 return false;
2277 2285
2278 return goToURL(cat, entryIndex); 2286 return goToURL(cat, entryIndex);
2279} 2287}
2280 2288
2281bool PwMDoc::goToURL(unsigned int category, unsigned int entryIndex) 2289bool PwMDoc::goToURL(unsigned int category, unsigned int entryIndex)
2282{ 2290{
2283#ifndef _WIN32_ 2291#ifndef _WIN32_
2284 if (geteuid() == 0) { 2292 if (geteuid() == 0) {
2285 rootAlertMsgBox(); 2293 rootAlertMsgBox();
2286 return false; 2294 return false;
2287 } 2295 }
2288#endif 2296#endif
2289 QString url(dti.dta[category].d[entryIndex].url.c_str()); 2297 QString url(dti.dta[category].d[entryIndex].url.c_str());
2290 if (url.isEmpty()) 2298 if (url.isEmpty())
2291 return false; 2299 return false;
2292 2300
2293 QString customBrowser(conf()->confGlobBrowserCommand()); 2301 QString customBrowser(conf()->confGlobBrowserCommand());
2294 if (!customBrowser.isEmpty()) { 2302 if (!customBrowser.isEmpty()) {
2295 browserProc.clearArguments(); 2303 browserProc.clearArguments();
2296 browserProc << customBrowser << url; 2304 browserProc << customBrowser << url;
2297 if (browserProc.start(KProcess::DontCare)) 2305 if (browserProc.start(KProcess::DontCare))
2298 return true; 2306 return true;
2299 } 2307 }
2300 2308
2301 browserProc.clearArguments(); 2309 browserProc.clearArguments();
2302 browserProc << "konqueror" << url; 2310 browserProc << "konqueror" << url;
2303 if (browserProc.start(KProcess::DontCare)) 2311 if (browserProc.start(KProcess::DontCare))
2304 return true; 2312 return true;
2305 2313
2306 browserProc.clearArguments(); 2314 browserProc.clearArguments();
2307 browserProc << "mozilla" << url; 2315 browserProc << "mozilla" << url;
2308 if (browserProc.start(KProcess::DontCare)) 2316 if (browserProc.start(KProcess::DontCare))
2309 return true; 2317 return true;
2310 2318
2311 browserProc.clearArguments(); 2319 browserProc.clearArguments();
2312 browserProc << "opera" << url; 2320 browserProc << "opera" << url;
2313 if (browserProc.start(KProcess::DontCare)) 2321 if (browserProc.start(KProcess::DontCare))
2314 return true; 2322 return true;
2315 return false; 2323 return false;
2316} 2324}
2317 2325
2318PwMerror PwMDoc::exportToText(const QString *file) 2326PwMerror PwMDoc::exportToText(const QString *file)
2319{ 2327{
2320 PWM_ASSERT(file); 2328 PWM_ASSERT(file);
2321 if (QFile::exists(*file)) { 2329 if (QFile::exists(*file)) {
2322 if (!QFile::remove(*file)) 2330 if (!QFile::remove(*file))
2323 return e_accessFile; 2331 return e_accessFile;
2324 } 2332 }
2325 QFile f(*file); 2333 QFile f(*file);
2326 if (!f.open(IO_ReadWrite)) 2334 if (!f.open(IO_ReadWrite))
2327 return e_openFile; 2335 return e_openFile;
2328 2336
2329 if (!unlockAll_tempoary()) { 2337 if (!unlockAll_tempoary()) {
2330 f.close(); 2338 f.close();
2331 return e_lock; 2339 return e_lock;
2332 } 2340 }
2333 2341
2334 // write header 2342 // write header
2335 string header = i18n("Password table generated by\nPwM v").latin1(); 2343 string header = i18n("Password table generated by\nPwM v").latin1();
2336 header += PACKAGE_VER; 2344 header += PACKAGE_VER;
2337 header += i18n("\non ").latin1(); 2345 header += i18n("\non ").latin1();
2338 QDate currDate = QDate::currentDate(); 2346 QDate currDate = QDate::currentDate();
2339 QTime currTime = QTime::currentTime(); 2347 QTime currTime = QTime::currentTime();
2340 2348
2341#ifndef PWM_EMBEDDED 2349#ifndef PWM_EMBEDDED
2342 header += currDate.toString("ddd MMMM d ").latin1(); 2350 header += currDate.toString("ddd MMMM d ").latin1();
2343 header += currTime.toString("hh:mm:ss ").latin1(); 2351 header += currTime.toString("hh:mm:ss ").latin1();
2344#else 2352#else
2345 QString dfs = KGlobal::locale()->dateFormatShort(); 2353 QString dfs = KGlobal::locale()->dateFormatShort();
2346 bool ampm = KGlobal::locale()->use12Clock(); 2354 bool ampm = KGlobal::locale()->use12Clock();
2347 KGlobal::locale()->setDateFormatShort("%A %B %d"); 2355 KGlobal::locale()->setDateFormatShort("%A %B %d");
2348 KGlobal::locale()->setHore24Format(true); 2356 KGlobal::locale()->setHore24Format(true);
2349 2357
2350 header += KGlobal::locale()->formatDate(currDate, true, KLocale::Userdefined).latin1(); 2358 header += KGlobal::locale()->formatDate(currDate, true, KLocale::Userdefined).latin1();
2351 header += KGlobal::locale()->formatTime(currTime, true).latin1(); 2359 header += KGlobal::locale()->formatTime(currTime, true).latin1();
2352 KGlobal::locale()->setDateFormatShort(dfs); 2360 KGlobal::locale()->setDateFormatShort(dfs);
2353 KGlobal::locale()->setHore24Format(!ampm); 2361 KGlobal::locale()->setHore24Format(!ampm);
2354 2362
2355#endif 2363#endif
2356 header += tostr(currDate.year()); 2364 header += tostr(currDate.year());
2357 header += "\n==============================\n\n"; 2365 header += "\n==============================\n\n";
2358 2366
2359 2367
2360#ifndef PWM_EMBEDDED 2368#ifndef PWM_EMBEDDED
2361 if (f.writeBlock(header.c_str(), header.length()) != (Q_LONG)header.length()) { 2369 if (f.writeBlock(header.c_str(), header.length()) != (Q_LONG)header.length()) {
2362 unlockAll_tempoary(true); 2370 unlockAll_tempoary(true);
2363 f.close(); 2371 f.close();
2364 return e_writeFile; 2372 return e_writeFile;
2365 } 2373 }
2366#else 2374#else
2367 if (f.writeBlock(header.c_str(), header.length()) != (long)header.length()) { 2375 if (f.writeBlock(header.c_str(), header.length()) != (long)header.length()) {
2368 unlockAll_tempoary(true); 2376 unlockAll_tempoary(true);
2369 f.close(); 2377 f.close();
2370 return e_writeFile; 2378 return e_writeFile;
2371 } 2379 }
2372#endif 2380#endif
2373 unsigned int i, numCat = numCategories(); 2381 unsigned int i, numCat = numCategories();
2374 unsigned int j, numEnt; 2382 unsigned int j, numEnt;
2375 string exp; 2383 string exp;
2376 for (i = 0; i < numCat; ++i) { 2384 for (i = 0; i < numCat; ++i) {
2377 numEnt = numEntries(i); 2385 numEnt = numEntries(i);
2378 2386
2379 exp = "\n== Category: "; 2387 exp = "\n== Category: ";
2380 exp += dti.dta[i].name; 2388 exp += dti.dta[i].name;
2381 exp += " ==\n"; 2389 exp += " ==\n";
2382#ifndef PWM_EMBEDDED 2390#ifndef PWM_EMBEDDED
2383 if (f.writeBlock(exp.c_str(), exp.length()) != (Q_LONG)exp.length()) { 2391 if (f.writeBlock(exp.c_str(), exp.length()) != (Q_LONG)exp.length()) {
2384 unlockAll_tempoary(true); 2392 unlockAll_tempoary(true);
2385 f.close(); 2393 f.close();
2386 return e_writeFile; 2394 return e_writeFile;
2387 } 2395 }
2388#else 2396#else
2389 if (f.writeBlock(exp.c_str(), exp.length()) != (long)exp.length()) { 2397 if (f.writeBlock(exp.c_str(), exp.length()) != (long)exp.length()) {
2390 unlockAll_tempoary(true); 2398 unlockAll_tempoary(true);
2391 f.close(); 2399 f.close();
2392 return e_writeFile; 2400 return e_writeFile;
2393 } 2401 }
2394#endif 2402#endif
2395 for (j = 0; j < numEnt; ++j) { 2403 for (j = 0; j < numEnt; ++j) {
2396 exp = "\n-- "; 2404 exp = "\n-- ";
2397 exp += dti.dta[i].d[j].desc; 2405 exp += dti.dta[i].d[j].desc;
2398 exp += " --\n"; 2406 exp += " --\n";
2399 2407
2400 exp += i18n("Username: ").latin1(); 2408 exp += i18n("Username: ").latin1();
2401 exp += dti.dta[i].d[j].name; 2409 exp += dti.dta[i].d[j].name;
2402 exp += "\n"; 2410 exp += "\n";
2403 2411
2404 exp += i18n("Password: ").latin1(); 2412 exp += i18n("Password: ").latin1();
2405 exp += dti.dta[i].d[j].pw; 2413 exp += dti.dta[i].d[j].pw;
2406 exp += "\n"; 2414 exp += "\n";
2407 2415
2408 exp += i18n("Comment: ").latin1(); 2416 exp += i18n("Comment: ").latin1();
2409 exp += dti.dta[i].d[j].comment; 2417 exp += dti.dta[i].d[j].comment;
2410 exp += "\n"; 2418 exp += "\n";
2411 2419
2412 exp += i18n("URL: ").latin1(); 2420 exp += i18n("URL: ").latin1();
2413 exp += dti.dta[i].d[j].url; 2421 exp += dti.dta[i].d[j].url;
2414 exp += "\n"; 2422 exp += "\n";
2415 2423
2416 exp += i18n("Launcher: ").latin1(); 2424 exp += i18n("Launcher: ").latin1();
2417 exp += dti.dta[i].d[j].launcher; 2425 exp += dti.dta[i].d[j].launcher;
2418 exp += "\n"; 2426 exp += "\n";
2419 2427
2420#ifndef PWM_EMBEDDED 2428#ifndef PWM_EMBEDDED
2421 if (f.writeBlock(exp.c_str(), exp.length()) != (Q_LONG)exp.length()) { 2429 if (f.writeBlock(exp.c_str(), exp.length()) != (Q_LONG)exp.length()) {
2422 unlockAll_tempoary(true); 2430 unlockAll_tempoary(true);
2423 f.close(); 2431 f.close();
2424 return e_writeFile; 2432 return e_writeFile;
2425 } 2433 }
2426#else 2434#else
2427 if (f.writeBlock(exp.c_str(), exp.length()) != (long)exp.length()) { 2435 if (f.writeBlock(exp.c_str(), exp.length()) != (long)exp.length()) {
2428 unlockAll_tempoary(true); 2436 unlockAll_tempoary(true);
2429 f.close(); 2437 f.close();
2430 return e_writeFile; 2438 return e_writeFile;
2431 } 2439 }
2432#endif 2440#endif
2433 } 2441 }
2434 } 2442 }
2435 unlockAll_tempoary(true); 2443 unlockAll_tempoary(true);
2436 f.close(); 2444 f.close();
2437 2445
2438 return e_success; 2446 return e_success;
2439} 2447}
2440 2448
2441PwMerror PwMDoc::importFromText(const QString *file, int format) 2449PwMerror PwMDoc::importFromText(const QString *file, int format)
2442{ 2450{
2443 PWM_ASSERT(file); 2451 PWM_ASSERT(file);
2444 if (format == 0) 2452 if (format == 0)
2445 return importText_PwM(file); 2453 return importText_PwM(file);
2446 else if (format == -1) { 2454 else if (format == -1) {
2447 // probe for all formats 2455 // probe for all formats
2448 if (importText_PwM(file) == e_success) 2456 if (importText_PwM(file) == e_success)
2449 return e_success; 2457 return e_success;
2450 dti.clear(); 2458 dti.clear();
2451 emitDataChanged(this); 2459 emitDataChanged(this);
2452 // add next format here... 2460 // add next format here...
2453 return e_fileFormat; 2461 return e_fileFormat;
2454 } 2462 }
2455 return e_invalidArg; 2463 return e_invalidArg;
2456} 2464}
2457 2465
2458PwMerror PwMDoc::importText_PwM(const QString *file) 2466PwMerror PwMDoc::importText_PwM(const QString *file)
2459{ 2467{
2460#ifndef PWM_EMBEDDED 2468#ifndef PWM_EMBEDDED
2461 PWM_ASSERT(file); 2469 PWM_ASSERT(file);
2462 FILE *f; 2470 FILE *f;
2463 int tmp; 2471 int tmp;
2464 ssize_t ret; 2472 ssize_t ret;
2465 string curCat; 2473 string curCat;
2466 unsigned int entriesRead = 0; 2474 unsigned int entriesRead = 0;
2467 PwMDataItem currItem; 2475 PwMDataItem currItem;
2468 f = fopen(file->latin1(), "r"); 2476 f = fopen(file->latin1(), "r");
2469 if (!f) 2477 if (!f)
2470 return e_openFile; 2478 return e_openFile;
2471 size_t ch_tmp_size = 1024; 2479 size_t ch_tmp_size = 1024;
2472 char *ch_tmp = (char*)malloc(ch_tmp_size); 2480 char *ch_tmp = (char*)malloc(ch_tmp_size);
2473 if (!ch_tmp) { 2481 if (!ch_tmp) {
2474 fclose(f); 2482 fclose(f);
2475 return e_outOfMem; 2483 return e_outOfMem;
2476 } 2484 }
2477 2485
2478 // - check header 2486 // - check header
2479 if (getline(&ch_tmp, &ch_tmp_size, f) == -1) // skip first line. 2487 if (getline(&ch_tmp, &ch_tmp_size, f) == -1) // skip first line.
2480 goto formatError; 2488 goto formatError;
2481 // check version-string and return version in "ch_tmp". 2489 // check version-string and return version in "ch_tmp".
2482 if (fscanf(f, "PwM v%s", ch_tmp) != 1) { 2490 if (fscanf(f, "PwM v%s", ch_tmp) != 1) {
2483 // header not recognized as PwM generated header 2491 // header not recognized as PwM generated header
2484 goto formatError; 2492 goto formatError;
2485 } 2493 }
2486 // set filepointer behind version-string-line previously checked 2494 // set filepointer behind version-string-line previously checked
2487 if (getline(&ch_tmp, &ch_tmp_size, f) == -1) 2495 if (getline(&ch_tmp, &ch_tmp_size, f) == -1)
2488 goto formatError; 2496 goto formatError;
2489 // skip next line containing the build-date 2497 // skip next line containing the build-date
2490 if (getline(&ch_tmp, &ch_tmp_size, f) == -1) 2498 if (getline(&ch_tmp, &ch_tmp_size, f) == -1)
2491 goto formatError; 2499 goto formatError;
2492 // read header termination line 2500 // read header termination line
2493 if (getline(&ch_tmp, &ch_tmp_size, f) == -1) 2501 if (getline(&ch_tmp, &ch_tmp_size, f) == -1)
2494 goto formatError; 2502 goto formatError;
2495 if (strcmp(ch_tmp, "==============================\n")) 2503 if (strcmp(ch_tmp, "==============================\n"))
2496 goto formatError; 2504 goto formatError;
2497 2505
2498 // - read entries 2506 // - read entries
2499 do { 2507 do {
2500 // find beginning of next category 2508 // find beginning of next category
2501 do { 2509 do {
2502 tmp = fgetc(f); 2510 tmp = fgetc(f);
2503 } while (tmp == '\n' && tmp != EOF); 2511 } while (tmp == '\n' && tmp != EOF);
2504 if (tmp == EOF) 2512 if (tmp == EOF)
2505 break; 2513 break;
2506 2514
2507 // decrement filepos by one 2515 // decrement filepos by one
2508 fseek(f, -1, SEEK_CUR); 2516 fseek(f, -1, SEEK_CUR);
2509 // read cat-name 2517 // read cat-name
2510 if (getline(&ch_tmp, &ch_tmp_size, f) == -1) 2518 if (getline(&ch_tmp, &ch_tmp_size, f) == -1)
2511 goto formatError; 2519 goto formatError;
2512 // check cat-name format 2520 // check cat-name format
2513 if (memcmp(ch_tmp, "== Category: ", 13) != 0) 2521 if (memcmp(ch_tmp, "== Category: ", 13) != 0)
2514 goto formatError; 2522 goto formatError;
2515 if (memcmp(ch_tmp + (strlen(ch_tmp) - 1 - 3), " ==", 3) != 0) 2523 if (memcmp(ch_tmp + (strlen(ch_tmp) - 1 - 3), " ==", 3) != 0)
2516 goto formatError; 2524 goto formatError;
2517 // copy cat-name 2525 // copy cat-name
2518 curCat.assign(ch_tmp + 13, strlen(ch_tmp) - 1 - 16); 2526 curCat.assign(ch_tmp + 13, strlen(ch_tmp) - 1 - 16);
2519 2527
2520 do { 2528 do {
2521 // find beginning of next entry 2529 // find beginning of next entry
2522 do { 2530 do {
2523 tmp = fgetc(f); 2531 tmp = fgetc(f);
2524 } while (tmp == '\n' && tmp != EOF && tmp != '='); 2532 } while (tmp == '\n' && tmp != EOF && tmp != '=');
2525 if (tmp == EOF) 2533 if (tmp == EOF)
2526 break; 2534 break;
2527 if (tmp == '=') { 2535 if (tmp == '=') {
2528 fseek(f, -1, SEEK_CUR); 2536 fseek(f, -1, SEEK_CUR);
2529 break; 2537 break;
2530 } 2538 }
2531 // decrement filepos by one 2539 // decrement filepos by one
2532 fseek(f, -1, SEEK_CUR); 2540 fseek(f, -1, SEEK_CUR);
2533 // read desc-line 2541 // read desc-line
2534 if (getline(&ch_tmp, &ch_tmp_size, f) == -1) 2542 if (getline(&ch_tmp, &ch_tmp_size, f) == -1)
2535 goto formatError; 2543 goto formatError;
2536 // check desc-line format 2544 // check desc-line format
2537 if (memcmp(ch_tmp, "-- ", 3) != 0) 2545 if (memcmp(ch_tmp, "-- ", 3) != 0)
2538 goto formatError; 2546 goto formatError;
2539 if (memcmp(ch_tmp + (strlen(ch_tmp) - 1 - 3), " --", 3) != 0) 2547 if (memcmp(ch_tmp + (strlen(ch_tmp) - 1 - 3), " --", 3) != 0)
2540 goto formatError; 2548 goto formatError;
2541 // add desc-line 2549 // add desc-line
2542 currItem.desc.assign(ch_tmp + 3, strlen(ch_tmp) - 1 - 6); 2550 currItem.desc.assign(ch_tmp + 3, strlen(ch_tmp) - 1 - 6);
2543 2551
2544 // read username-line 2552 // read username-line
2545 if ((ret = getline(&ch_tmp, &ch_tmp_size, f)) == -1) 2553 if ((ret = getline(&ch_tmp, &ch_tmp_size, f)) == -1)
2546 goto formatError; 2554 goto formatError;
2547 if (!textExtractEntry_PwM(ch_tmp, ret, &currItem.name)) 2555 if (!textExtractEntry_PwM(ch_tmp, ret, &currItem.name))
2548 goto formatError; 2556 goto formatError;
2549 2557
2550 // read pw-line 2558 // read pw-line
2551 if ((ret = getline(&ch_tmp, &ch_tmp_size, f)) == -1) 2559 if ((ret = getline(&ch_tmp, &ch_tmp_size, f)) == -1)
2552 goto formatError; 2560 goto formatError;
2553 if (!textExtractEntry_PwM(ch_tmp, ret, &currItem.pw)) 2561 if (!textExtractEntry_PwM(ch_tmp, ret, &currItem.pw))
2554 goto formatError; 2562 goto formatError;
2555 2563
2556 // read comment-line 2564 // read comment-line
2557 if ((ret = getline(&ch_tmp, &ch_tmp_size, f)) == -1) 2565 if ((ret = getline(&ch_tmp, &ch_tmp_size, f)) == -1)
2558 goto formatError; 2566 goto formatError;
2559 if (!textExtractEntry_PwM(ch_tmp, ret, &currItem.comment)) 2567 if (!textExtractEntry_PwM(ch_tmp, ret, &currItem.comment))
2560 goto formatError; 2568 goto formatError;
2561 2569
2562 // read URL-line 2570 // read URL-line
2563 if ((ret = getline(&ch_tmp, &ch_tmp_size, f)) == -1) 2571 if ((ret = getline(&ch_tmp, &ch_tmp_size, f)) == -1)
2564 goto formatError; 2572 goto formatError;
2565 if (!textExtractEntry_PwM(ch_tmp, ret, &currItem.url)) 2573 if (!textExtractEntry_PwM(ch_tmp, ret, &currItem.url))
2566 goto formatError; 2574 goto formatError;
2567 2575
2568 // read launcher-line 2576 // read launcher-line
2569 if ((ret = getline(&ch_tmp, &ch_tmp_size, f)) == -1) 2577 if ((ret = getline(&ch_tmp, &ch_tmp_size, f)) == -1)
2570 goto formatError; 2578 goto formatError;
2571 if (!textExtractEntry_PwM(ch_tmp, ret, &currItem.launcher)) 2579 if (!textExtractEntry_PwM(ch_tmp, ret, &currItem.launcher))
2572 goto formatError; 2580 goto formatError;
2573 2581
2574 currItem.lockStat = true; 2582 currItem.lockStat = true;
2575 currItem.listViewPos = -1; 2583 currItem.listViewPos = -1;
2576 addEntry(curCat.c_str(), &currItem, true); 2584 addEntry(curCat.c_str(), &currItem, true);
2577 ++entriesRead; 2585 ++entriesRead;
2578 } while (1); 2586 } while (1);
2579 } while (1); 2587 } while (1);
2580 if (!entriesRead) 2588 if (!entriesRead)
2581 goto formatError; 2589 goto formatError;
2582 2590
2583 free(ch_tmp); 2591 free(ch_tmp);
2584 fclose(f); 2592 fclose(f);
2585 flagDirty(); 2593 flagDirty();
2586 return e_success; 2594 return e_success;
2587 2595
2588 formatError: 2596 formatError:
2589 free(ch_tmp); 2597 free(ch_tmp);
2590 fclose(f); 2598 fclose(f);
2591 return e_fileFormat; 2599 return e_fileFormat;
2592#else 2600#else
2593 PWM_ASSERT(file); 2601 PWM_ASSERT(file);
2594 QFile f(file->latin1()); 2602 QFile f(file->latin1());
2595 int tmp; 2603 int tmp;
2596 ssize_t ret; 2604 ssize_t ret;
2597 string curCat; 2605 string curCat;
2598 unsigned int entriesRead = 0; 2606 unsigned int entriesRead = 0;
2599 PwMDataItem currItem; 2607 PwMDataItem currItem;
2600 bool res = f.open(IO_ReadOnly); 2608 bool res = f.open(IO_ReadOnly);
2601 if (res == false) 2609 if (res == false)
2602 return e_openFile; 2610 return e_openFile;
2603 2611
2604 unsigned int ch_tmp_size = 1024; 2612 unsigned int ch_tmp_size = 1024;
2605 char *ch_tmp = (char*)malloc(ch_tmp_size); 2613 char *ch_tmp = (char*)malloc(ch_tmp_size);
2606 if (!ch_tmp) { 2614 if (!ch_tmp) {
2607 f.close(); 2615 f.close();
2608 return e_outOfMem; 2616 return e_outOfMem;
2609 } 2617 }
2610 2618
2611 // - check header 2619 // - check header
2612 if (f.readLine(ch_tmp, ch_tmp_size) == -1) // skip first line. 2620 if (f.readLine(ch_tmp, ch_tmp_size) == -1) // skip first line.
2613 goto formatError; 2621 goto formatError;
2614 2622
2615 //US read fileversion first, then check if ok. 2623 //US read fileversion first, then check if ok.
2616 if (f.readLine(ch_tmp, ch_tmp_size) == -1) 2624 if (f.readLine(ch_tmp, ch_tmp_size) == -1)
2617 goto formatError; 2625 goto formatError;
2618 2626
2619 // check version-string and return version in "ch_tmp". 2627 // check version-string and return version in "ch_tmp".
2620 //US if (fscanf(f, "PwM v%s", ch_tmp) != 1) { 2628 //US if (fscanf(f, "PwM v%s", ch_tmp) != 1) {
2621 //US // header not recognized as PwM generated header 2629 //US // header not recognized as PwM generated header
2622 //US goto formatError; 2630 //US goto formatError;
2623 //US} 2631 //US}
2624 //US set filepointer behind version-string-line previously checked 2632 //US set filepointer behind version-string-line previously checked
2625 //US if (f.readLine(ch_tmp, ch_tmp_size) == -1) 2633 //US if (f.readLine(ch_tmp, ch_tmp_size) == -1)
2626 //US goto formatError; 2634 //US goto formatError;
2627 // skip next line containing the build-date 2635 // skip next line containing the build-date
2628 if (f.readLine(ch_tmp, ch_tmp_size) == -1) 2636 if (f.readLine(ch_tmp, ch_tmp_size) == -1)
2629 goto formatError; 2637 goto formatError;
2630 // read header termination line 2638 // read header termination line
2631 if (f.readLine(ch_tmp, ch_tmp_size) == -1) 2639 if (f.readLine(ch_tmp, ch_tmp_size) == -1)
2632 goto formatError; 2640 goto formatError;
2633 if (strcmp(ch_tmp, "==============================\n")) 2641 if (strcmp(ch_tmp, "==============================\n"))
2634 goto formatError; 2642 goto formatError;
2635 2643
2636 // - read entries 2644 // - read entries
2637 do { 2645 do {
2638 // find beginning of next category 2646 // find beginning of next category
2639 do { 2647 do {
2640 tmp = f.getch(); 2648 tmp = f.getch();
2641 } while (tmp == '\n' && tmp != EOF); 2649 } while (tmp == '\n' && tmp != EOF);
2642 if (tmp == EOF) 2650 if (tmp == EOF)
2643 break; 2651 break;
2644 2652
2645 // decrement filepos by one 2653 // decrement filepos by one
2646 f.at(f.at()-1); 2654 f.at(f.at()-1);
2647 // read cat-name 2655 // read cat-name
2648 if (f.readLine(ch_tmp, ch_tmp_size) == -1) 2656 if (f.readLine(ch_tmp, ch_tmp_size) == -1)
2649 goto formatError; 2657 goto formatError;
2650 // check cat-name format 2658 // check cat-name format
2651 if (memcmp(ch_tmp, "== Category: ", 13) != 0) 2659 if (memcmp(ch_tmp, "== Category: ", 13) != 0)
2652 goto formatError; 2660 goto formatError;
2653 if (memcmp(ch_tmp + (strlen(ch_tmp) - 1 - 3), " ==", 3) != 0) 2661 if (memcmp(ch_tmp + (strlen(ch_tmp) - 1 - 3), " ==", 3) != 0)
2654 goto formatError; 2662 goto formatError;
2655 // copy cat-name 2663 // copy cat-name
2656 curCat.assign(ch_tmp + 13, strlen(ch_tmp) - 1 - 16); 2664 curCat.assign(ch_tmp + 13, strlen(ch_tmp) - 1 - 16);
2657 2665
2658 do { 2666 do {
2659 // find beginning of next entry 2667 // find beginning of next entry
2660 do { 2668 do {
2661 tmp = f.getch(); 2669 tmp = f.getch();
2662 } while (tmp == '\n' && tmp != EOF && tmp != '='); 2670 } while (tmp == '\n' && tmp != EOF && tmp != '=');
2663 if (tmp == EOF) 2671 if (tmp == EOF)
2664 break; 2672 break;
2665 if (tmp == '=') { 2673 if (tmp == '=') {
2666 f.at(f.at()-1); 2674 f.at(f.at()-1);
2667 break; 2675 break;
2668 } 2676 }
2669 // decrement filepos by one 2677 // decrement filepos by one
2670 f.at(f.at()-1); 2678 f.at(f.at()-1);
2671 // read desc-line 2679 // read desc-line
2672 if (f.readLine(ch_tmp, ch_tmp_size) == -1) 2680 if (f.readLine(ch_tmp, ch_tmp_size) == -1)
2673 goto formatError; 2681 goto formatError;
2674 // check desc-line format 2682 // check desc-line format
2675 if (memcmp(ch_tmp, "-- ", 3) != 0) 2683 if (memcmp(ch_tmp, "-- ", 3) != 0)
2676 goto formatError; 2684 goto formatError;
2677 if (memcmp(ch_tmp + (strlen(ch_tmp) - 1 - 3), " --", 3) != 0) 2685 if (memcmp(ch_tmp + (strlen(ch_tmp) - 1 - 3), " --", 3) != 0)
2678 goto formatError; 2686 goto formatError;
2679 // add desc-line 2687 // add desc-line
2680 currItem.desc.assign(ch_tmp + 3, strlen(ch_tmp) - 1 - 6); 2688 currItem.desc.assign(ch_tmp + 3, strlen(ch_tmp) - 1 - 6);
2681 2689
2682 // read username-line 2690 // read username-line
2683 if ((ret = f.readLine(ch_tmp, ch_tmp_size)) == -1) 2691 if ((ret = f.readLine(ch_tmp, ch_tmp_size)) == -1)
2684 goto formatError; 2692 goto formatError;
2685 if (!textExtractEntry_PwM(ch_tmp, ret, &currItem.name)) 2693 if (!textExtractEntry_PwM(ch_tmp, ret, &currItem.name))
2686 goto formatError; 2694 goto formatError;
2687 2695
2688 // read pw-line 2696 // read pw-line
2689 if ((ret = f.readLine(ch_tmp, ch_tmp_size)) == -1) 2697 if ((ret = f.readLine(ch_tmp, ch_tmp_size)) == -1)
2690 goto formatError; 2698 goto formatError;
2691 if (!textExtractEntry_PwM(ch_tmp, ret, &currItem.pw)) 2699 if (!textExtractEntry_PwM(ch_tmp, ret, &currItem.pw))
2692 goto formatError; 2700 goto formatError;
2693 2701
2694 // read comment-line 2702 // read comment-line
2695 if ((ret = f.readLine(ch_tmp, ch_tmp_size)) == -1) 2703 if ((ret = f.readLine(ch_tmp, ch_tmp_size)) == -1)
2696 goto formatError; 2704 goto formatError;
2697 if (!textExtractEntry_PwM(ch_tmp, ret, &currItem.comment)) 2705 if (!textExtractEntry_PwM(ch_tmp, ret, &currItem.comment))
2698 goto formatError; 2706 goto formatError;
2699 2707
2700 // read URL-line 2708 // read URL-line
2701 if ((ret = f.readLine(ch_tmp, ch_tmp_size)) == -1) 2709 if ((ret = f.readLine(ch_tmp, ch_tmp_size)) == -1)
2702 goto formatError; 2710 goto formatError;
2703 if (!textExtractEntry_PwM(ch_tmp, ret, &currItem.url)) 2711 if (!textExtractEntry_PwM(ch_tmp, ret, &currItem.url))
2704 goto formatError; 2712 goto formatError;
2705 2713
2706 // read launcher-line 2714 // read launcher-line
2707 if ((ret = f.readLine(ch_tmp, ch_tmp_size)) == -1) 2715 if ((ret = f.readLine(ch_tmp, ch_tmp_size)) == -1)
2708 goto formatError; 2716 goto formatError;
2709 if (!textExtractEntry_PwM(ch_tmp, ret, &currItem.launcher)) 2717 if (!textExtractEntry_PwM(ch_tmp, ret, &currItem.launcher))
2710 goto formatError; 2718 goto formatError;
2711 2719
2712 currItem.lockStat = true; 2720 currItem.lockStat = true;
2713 currItem.listViewPos = -1; 2721 currItem.listViewPos = -1;
2714 addEntry(curCat.c_str(), &currItem, true); 2722 addEntry(curCat.c_str(), &currItem, true);
2715 ++entriesRead; 2723 ++entriesRead;
2716 } while (1); 2724 } while (1);
2717 } while (1); 2725 } while (1);
2718 if (!entriesRead) 2726 if (!entriesRead)
2719 goto formatError; 2727 goto formatError;
2720 2728
2721 free(ch_tmp); 2729 free(ch_tmp);
2722 f.close(); 2730 f.close();
2723 flagDirty(); 2731 flagDirty();
2724 return e_success; 2732 return e_success;
2725 2733
2726 formatError: 2734 formatError:
2727 free(ch_tmp); 2735 free(ch_tmp);
2728 f.close(); 2736 f.close();
2729 return e_fileFormat; 2737 return e_fileFormat;
2730 2738
2731#endif 2739#endif
2732 2740
2733} 2741}
2734 2742
2735bool PwMDoc::textExtractEntry_PwM(const char *in, ssize_t in_size, string *out) 2743bool PwMDoc::textExtractEntry_PwM(const char *in, ssize_t in_size, string *out)
2736{ 2744{
2737 PWM_ASSERT(in && out); 2745 PWM_ASSERT(in && out);
2738 ssize_t i = 0, len = in_size - 1; 2746 ssize_t i = 0, len = in_size - 1;
2739 while (i < len) { 2747 while (i < len) {
2740 if (in[i] == ':') 2748 if (in[i] == ':')
2741 break; 2749 break;
2742 ++i; 2750 ++i;
2743 } 2751 }
2744 i += 2; 2752 i += 2;
2745 *out = ""; 2753 *out = "";
2746 out->append(in + i, in_size - i - 1); 2754 out->append(in + i, in_size - i - 1);
2747 return true; 2755 return true;
2748} 2756}
2749 2757
2750PwMerror PwMDoc::exportToGpasman(const QString *file) 2758PwMerror PwMDoc::exportToGpasman(const QString *file)
2751{ 2759{
2752 PWM_ASSERT(file); 2760 PWM_ASSERT(file);
2753 GpasmanFile gp; 2761 GpasmanFile gp;
2754 int ret; 2762 int ret;
2755 2763
2756 if (!unlockAll_tempoary()) 2764 if (!unlockAll_tempoary())
2757 return e_lock; 2765 return e_lock;
2758 2766
2759 QString gpmPassword; 2767 QString gpmPassword;
2760 while (1) { 2768 while (1) {
2761 gpmPassword = requestNewMpw(0); 2769 gpmPassword = requestNewMpw(0);
2762 if (gpmPassword == "") { 2770 if (gpmPassword == "") {
2763 unlockAll_tempoary(true); 2771 unlockAll_tempoary(true);
2764 return e_noPw; 2772 return e_noPw;
2765 } 2773 }
2766 if (gpmPassword.length() < 4) { 2774 if (gpmPassword.length() < 4) {
2767 gpmPwLenErrMsgBox(); 2775 gpmPwLenErrMsgBox();
2768 } else { 2776 } else {
2769 break; 2777 break;
2770 } 2778 }
2771 } 2779 }
2772 2780
2773 ret = gp.save_init(file->latin1(), gpmPassword.latin1()); 2781 ret = gp.save_init(file->latin1(), gpmPassword.latin1());
2774 if (ret != 1) { 2782 if (ret != 1) {
2775 unlockAll_tempoary(true); 2783 unlockAll_tempoary(true);
2776 return e_accessFile; 2784 return e_accessFile;
2777 } 2785 }
2778 2786
2779 char *entry[4]; 2787 char *entry[4];
2780 unsigned int numCat = numCategories(), i; 2788 unsigned int numCat = numCategories(), i;
2781 unsigned int numEntr, j; 2789 unsigned int numEntr, j;
2782 int descLen, nameLen, pwLen, commentLen; 2790 int descLen, nameLen, pwLen, commentLen;
2783 for (i = 0; i < numCat; ++i) { 2791 for (i = 0; i < numCat; ++i) {
2784 numEntr = numEntries(i); 2792 numEntr = numEntries(i);
2785 for (j = 0; j < numEntr; ++j) { 2793 for (j = 0; j < numEntr; ++j) {
2786 descLen = dti.dta[i].d[j].desc.length(); 2794 descLen = dti.dta[i].d[j].desc.length();
2787 nameLen = dti.dta[i].d[j].name.length(); 2795 nameLen = dti.dta[i].d[j].name.length();
2788 pwLen = dti.dta[i].d[j].pw.length(); 2796 pwLen = dti.dta[i].d[j].pw.length();
2789 commentLen = dti.dta[i].d[j].comment.length(); 2797 commentLen = dti.dta[i].d[j].comment.length();
2790 entry[0] = new char[descLen + 1]; 2798 entry[0] = new char[descLen + 1];
2791 entry[1] = new char[nameLen + 1]; 2799 entry[1] = new char[nameLen + 1];
2792 entry[2] = new char[pwLen + 1]; 2800 entry[2] = new char[pwLen + 1];
2793 entry[3] = new char[commentLen + 1]; 2801 entry[3] = new char[commentLen + 1];
2794 strcpy(entry[0], descLen == 0 ? " " : dti.dta[i].d[j].desc.c_str()); 2802 strcpy(entry[0], descLen == 0 ? " " : dti.dta[i].d[j].desc.c_str());
2795 strcpy(entry[1], nameLen == 0 ? " " : dti.dta[i].d[j].name.c_str()); 2803 strcpy(entry[1], nameLen == 0 ? " " : dti.dta[i].d[j].name.c_str());
2796 strcpy(entry[2], pwLen == 0 ? " " : dti.dta[i].d[j].pw.c_str()); 2804 strcpy(entry[2], pwLen == 0 ? " " : dti.dta[i].d[j].pw.c_str());
2797 strcpy(entry[3], commentLen == 0 ? " " : dti.dta[i].d[j].comment.c_str()); 2805 strcpy(entry[3], commentLen == 0 ? " " : dti.dta[i].d[j].comment.c_str());
2798 entry[0][descLen == 0 ? descLen + 1 : descLen] = '\0'; 2806 entry[0][descLen == 0 ? descLen + 1 : descLen] = '\0';
2799 entry[1][nameLen == 0 ? nameLen + 1 : nameLen] = '\0'; 2807 entry[1][nameLen == 0 ? nameLen + 1 : nameLen] = '\0';
2800 entry[2][pwLen == 0 ? pwLen + 1 : pwLen] = '\0'; 2808 entry[2][pwLen == 0 ? pwLen + 1 : pwLen] = '\0';
2801 entry[3][commentLen == 0 ? commentLen + 1 : commentLen] = '\0'; 2809 entry[3][commentLen == 0 ? commentLen + 1 : commentLen] = '\0';
2802 2810
2803 ret = gp.save_entry(entry); 2811 ret = gp.save_entry(entry);
2804 if (ret == -1){ 2812 if (ret == -1){
2805 delete [] entry[0]; 2813 delete [] entry[0];
2806 delete [] entry[1]; 2814 delete [] entry[1];
2807 delete [] entry[2]; 2815 delete [] entry[2];
2808 delete [] entry[3]; 2816 delete [] entry[3];
2809 gp.save_finalize(); 2817 gp.save_finalize();
2810 unlockAll_tempoary(true); 2818 unlockAll_tempoary(true);
2811 return e_writeFile; 2819 return e_writeFile;
2812 } 2820 }
2813 2821
2814 delete [] entry[0]; 2822 delete [] entry[0];
2815 delete [] entry[1]; 2823 delete [] entry[1];
2816 delete [] entry[2]; 2824 delete [] entry[2];
2817 delete [] entry[3]; 2825 delete [] entry[3];
2818 } 2826 }
2819 } 2827 }
2820 unlockAll_tempoary(true); 2828 unlockAll_tempoary(true);
2821 if (gp.save_finalize() == -1) 2829 if (gp.save_finalize() == -1)
2822 return e_writeFile; 2830 return e_writeFile;
2823 2831
2824 return e_success; 2832 return e_success;
2825} 2833}
2826 2834
2827PwMerror PwMDoc::importFromGpasman(const QString *file) 2835PwMerror PwMDoc::importFromGpasman(const QString *file)
2828{ 2836{
2829 PWM_ASSERT(file); 2837 PWM_ASSERT(file);
2830 QString pw = requestMpw(false); 2838 QString pw = requestMpw(false);
2831 if (pw == "") 2839 if (pw == "")
2832 return e_noPw; 2840 return e_noPw;
2833 GpasmanFile gp; 2841 GpasmanFile gp;
2834 int ret, i; 2842 int ret, i;
2835 PwMerror ret2; 2843 PwMerror ret2;
2836 char *entry[4]; 2844 char *entry[4];
2837 PwMDataItem tmpData; 2845 PwMDataItem tmpData;
2838 ret = gp.load_init(file->latin1(), pw.latin1()); 2846 ret = gp.load_init(file->latin1(), pw.latin1());
2839 if (ret != 1) 2847 if (ret != 1)
2840 return e_accessFile; 2848 return e_accessFile;
2841 2849
2842 do { 2850 do {
2843 ret = gp.load_entry(entry); 2851 ret = gp.load_entry(entry);
2844 if(ret != 1) 2852 if(ret != 1)
2845 break; 2853 break;
2846 tmpData.desc = entry[0]; 2854 tmpData.desc = entry[0];
2847 tmpData.name = entry[1]; 2855 tmpData.name = entry[1];
2848 tmpData.pw = entry[2]; 2856 tmpData.pw = entry[2];
2849 tmpData.comment = entry[3]; 2857 tmpData.comment = entry[3];
2850 tmpData.lockStat = true; 2858 tmpData.lockStat = true;
2851 tmpData.listViewPos = -1; 2859 tmpData.listViewPos = -1;
2852 ret2 = addEntry(DEFAULT_CATEGORY, &tmpData, true); 2860 ret2 = addEntry(DEFAULT_CATEGORY, &tmpData, true);
2853 for (i = 0; i < 4; ++i) 2861 for (i = 0; i < 4; ++i)
2854 free(entry[i]); 2862 free(entry[i]);
2855 if (ret2 == e_maxAllowedEntr) { 2863 if (ret2 == e_maxAllowedEntr) {
2856 gp.load_finalize(); 2864 gp.load_finalize();
2857 return e_maxAllowedEntr; 2865 return e_maxAllowedEntr;
2858 } 2866 }
2859 } while (1); 2867 } while (1);
2860 gp.load_finalize(); 2868 gp.load_finalize();
2861 if (isDocEmpty()) 2869 if (isDocEmpty())
2862 return e_wrongPw; // we assume this. 2870 return e_wrongPw; // we assume this.
2863 2871
2864 flagDirty(); 2872 flagDirty();
2865 return e_success; 2873 return e_success;
2866} 2874}
2867 2875
2868 2876
2869//US: we use the stl sort algorythm to sort all elements in the order 2877//US: we use the stl sort algorythm to sort all elements in the order
2870//of its listViewPos (in the order 1,2,3,5,...,x,-1, -1, -1 2878//of its listViewPos (in the order 1,2,3,5,...,x,-1, -1, -1
2871struct PwMDataItemListViewPosSort 2879struct PwMDataItemListViewPosSort
2872{ 2880{
2873 bool operator()(PwMDataItem* rpStart, PwMDataItem* rpEnd) 2881 bool operator()(PwMDataItem* rpStart, PwMDataItem* rpEnd)
2874 { 2882 {
2875 //qDebug("pwMDoc::PwMDataItemListViewPosSort()"); 2883 //qDebug("pwMDoc::PwMDataItemListViewPosSort()");
2876 if ((rpEnd)->listViewPos < 0) 2884 if ((rpEnd)->listViewPos < 0)
2877 return false; 2885 return false;
2878 else 2886 else
2879 return (rpStart)->listViewPos < (rpEnd)->listViewPos; 2887 return (rpStart)->listViewPos < (rpEnd)->listViewPos;
2880 } 2888 }
2881}; 2889};
2882 2890
2883void PwMDoc::ensureLvp() 2891void PwMDoc::ensureLvp()
2884{ 2892{
2885 if (isDocEmpty()) 2893 if (isDocEmpty())
2886 return; 2894 return;
2887 2895
2888 //US ENH BUG: when using syncronizing, this way of sorting 2896 //US ENH BUG: when using syncronizing, this way of sorting
2889 //is not sufficient, because there might be empty spaces 2897 //is not sufficient, because there might be empty spaces
2890 // at the beginning. But the old algorythm only can add elements 2898 // at the beginning. But the old algorythm only can add elements
2891 //to the end.The result are crashes because of list overflows 2899 //to the end.The result are crashes because of list overflows
2892 //we need something to fill all gaps. 2900 //we need something to fill all gaps.
2893 vector<PwMDataItem*> sorted; 2901 vector<PwMDataItem*> sorted;
2894 vector< PwMDataItem*>::iterator sortedBegin, 2902 vector< PwMDataItem*>::iterator sortedBegin,
2895 sortedEnd, 2903 sortedEnd,
2896 sortedI; 2904 sortedI;
2897 vector<PwMCategoryItem>::iterator catBegin = dti.dta.begin(), 2905 vector<PwMCategoryItem>::iterator catBegin = dti.dta.begin(),
2898 catEnd = dti.dta.end(), 2906 catEnd = dti.dta.end(),
2899 catI = catBegin; 2907 catI = catBegin;
2900 vector<PwMDataItem>::iterator entrBegin, entrEnd, entrI; 2908 vector<PwMDataItem>::iterator entrBegin, entrEnd, entrI;
2901 int lvpTop, tmpLvp; 2909 int lvpTop, tmpLvp;
2902 2910
2903 //qDebug("collect:"); 2911 //qDebug("collect:");
2904 2912
2905 while (catI != catEnd) { 2913 while (catI != catEnd) {
2906 lvpTop = -1; 2914 lvpTop = -1;
2907 sorted.clear(); 2915 sorted.clear();
2908 2916
2909 entrBegin = catI->d.begin(); 2917 entrBegin = catI->d.begin();
2910 entrEnd = catI->d.end(); 2918 entrEnd = catI->d.end();
2911 entrI = entrBegin; 2919 entrI = entrBegin;
2912 2920
2913 //US: we use the stl sort algorythm to sort all elements in the order 2921 //US: we use the stl sort algorythm to sort all elements in the order
2914 //of its listViewPos (in the order 1,2,2,3,5,...,x,-1, -1, -1 2922 //of its listViewPos (in the order 1,2,2,3,5,...,x,-1, -1, -1
2915 while (entrI != entrEnd) { 2923 while (entrI != entrEnd) {
2916 //qDebug("found: %s, pos=%i", (*entrI).desc.c_str(), (*entrI).listViewPos); 2924 //qDebug("found: %s, pos=%i", (*entrI).desc.c_str(), (*entrI).listViewPos);
2917 sorted.push_back((PwMDataItem*)&(*entrI)); 2925 sorted.push_back((PwMDataItem*)&(*entrI));
2918 ++entrI; 2926 ++entrI;
2919 } 2927 }
2920 2928
2921 sortedBegin = sorted.begin(); 2929 sortedBegin = sorted.begin();
2922 sortedEnd = sorted.end(); 2930 sortedEnd = sorted.end();
2923 2931
2924 sort(sortedBegin, sortedEnd, PwMDataItemListViewPosSort()); 2932 sort(sortedBegin, sortedEnd, PwMDataItemListViewPosSort());
2925 2933
2926 // qDebug("resort:"); 2934 // qDebug("resort:");
2927 //now we have all sorted in a collection 2935 //now we have all sorted in a collection
2928 //Now start with the sorted and reset listviewpos. 2936 //Now start with the sorted and reset listviewpos.
2929 sortedBegin = sorted.begin(); 2937 sortedBegin = sorted.begin();
2930 sortedEnd = sorted.end(); 2938 sortedEnd = sorted.end();
2931 sortedI = sortedBegin; 2939 sortedI = sortedBegin;
2932 2940
2933 while (sortedI != sortedEnd) { 2941 while (sortedI != sortedEnd) {
2934 // qDebug("reset defined: %s, from pos=%i to pos=%i", (*sortedI)->desc.c_str(), (*sortedI)->listViewPos, lvpTop+1); 2942 // qDebug("reset defined: %s, from pos=%i to pos=%i", (*sortedI)->desc.c_str(), (*sortedI)->listViewPos, lvpTop+1);
2935 (*sortedI)->listViewPos = ++lvpTop; 2943 (*sortedI)->listViewPos = ++lvpTop;
2936 ++sortedI; 2944 ++sortedI;
2937 } 2945 }
2938 2946
2939 /*/debug 2947 /*/debug
2940 entrBegin = catI->d.begin(); 2948 entrBegin = catI->d.begin();
2941 entrEnd = catI->d.end(); 2949 entrEnd = catI->d.end();
2942 entrI = entrBegin; 2950 entrI = entrBegin;
2943 2951
2944 while (entrI != entrEnd) { 2952 while (entrI != entrEnd) {
2945 qDebug("check: %s, pos=%i", (*entrI).desc.c_str(), (*entrI).listViewPos); 2953 qDebug("check: %s, pos=%i", (*entrI).desc.c_str(), (*entrI).listViewPos);
2946 ++entrI; 2954 ++entrI;
2947 } 2955 }
2948 */ 2956 */
2949 2957
2950 ++catI; 2958 ++catI;
2951 } 2959 }
2952} 2960}
2953 2961
2954QString PwMDoc::getTitle() 2962QString PwMDoc::getTitle()
2955{ 2963{
2956 /* NOTE: We have to ensure, that the returned title 2964 /* NOTE: We have to ensure, that the returned title
2957 * is unique and not reused somewhere else while 2965 * is unique and not reused somewhere else while
2958 * this document is valid (open). 2966 * this document is valid (open).
2959 */ 2967 */
2960 QString title(getFilename()); 2968 QString title(getFilename());
2961 2969
2962 //US ENH: The whole filename on PDAs is too long. So use only the last characters 2970 //US ENH: The whole filename on PDAs is too long. So use only the last characters
2963 if (QApplication::desktop()->width() < 640) 2971 if (QApplication::desktop()->width() < 640)
2964 { 2972 {
2965 if (title.length() > 30) 2973 if (title.length() > 30)
2966 title = "..." + title.right(30); 2974 title = "..." + title.right(30);
2967 2975
2968 } 2976 }
2969 2977
2970 2978
2971 if (title.isEmpty()) { 2979 if (title.isEmpty()) {
2972 if (unnamedNum == 0) { 2980 if (unnamedNum == 0) {
2973 unnamedNum = PwMDocList::getNewUnnamedNumber(); 2981 unnamedNum = PwMDocList::getNewUnnamedNumber();
2974 PWM_ASSERT(unnamedNum != 0); 2982 PWM_ASSERT(unnamedNum != 0);
2975 } 2983 }
2976 title = DEFAULT_TITLE; 2984 title = DEFAULT_TITLE;
2977 title += " "; 2985 title += " ";
2978 title += tostr(unnamedNum).c_str(); 2986 title += tostr(unnamedNum).c_str();
2979 } 2987 }
2980 return title; 2988 return title;
2981} 2989}
2982 2990
2983bool PwMDoc::tryDelete() 2991bool PwMDoc::tryDelete()
2984{ 2992{
2985 2993
2986 if (deleted) 2994 if (deleted)
2987 return true; 2995 return true;
2988 int ret; 2996 int ret;
2989 if (isDirty()) { 2997 if (isDirty()) {
2990 ret = dirtyAskSave(getTitle()); 2998 ret = dirtyAskSave(getTitle());
2991 if (ret == 0) { // save to disk 2999 if (ret == 0) { // save to disk
2992 if (!saveDocUi(this)) 3000 if (!saveDocUi(this))
2993 goto out_ignore; 3001 goto out_ignore;
2994 } else if (ret == 1) { // don't save and delete 3002 } else if (ret == 1) { // don't save and delete
2995 goto out_accept; 3003 goto out_accept;
2996 } else { // cancel operation 3004 } else { // cancel operation
2997 goto out_ignore; 3005 goto out_ignore;
2998 } 3006 }
2999 } 3007 }
3000out_accept: 3008out_accept:
3001 deleted = true; 3009 deleted = true;
3002 delete this; 3010 delete this;
3003 return true; 3011 return true;
3004out_ignore: 3012out_ignore:
3005 return false; 3013 return false;
3006} 3014}
3007 3015
3008 3016
3009 3017
3010#ifdef PWM_EMBEDDED 3018#ifdef PWM_EMBEDDED
3011//US ENH: this is the magic function that syncronizes the this doc with the remote doc 3019//US ENH: this is the magic function that syncronizes the this doc with the remote doc
3012//US it could have been defined as static, but I did not want to. 3020//US it could have been defined as static, but I did not want to.
3013PwMerror PwMDoc::syncronize(KSyncManager* manager, PwMDoc* syncLocal , PwMDoc* syncRemote, int mode ) 3021PwMerror PwMDoc::syncronize(KSyncManager* manager, PwMDoc* syncLocal , PwMDoc* syncRemote, int mode )
3014{ 3022{
3015 int addedPasswordsLocal = 0; 3023 int addedPasswordsLocal = 0;
3016 int addedPasswordsRemote = 0; 3024 int addedPasswordsRemote = 0;
3017 int deletedPasswordsRemote = 0; 3025 int deletedPasswordsRemote = 0;
3018 int deletedPasswordsLocal = 0; 3026 int deletedPasswordsLocal = 0;
3019 int changedLocal = 0; 3027 int changedLocal = 0;
3020 int changedRemote = 0; 3028 int changedRemote = 0;
3021 3029
3022 PwMSyncItem* syncItemLocal; 3030 PwMSyncItem* syncItemLocal;
3023 PwMSyncItem* syncItemRemote; 3031 PwMSyncItem* syncItemRemote;
3024 3032
3025 QString mCurrentSyncName = manager->getCurrentSyncName(); 3033 QString mCurrentSyncName = manager->getCurrentSyncName();
3026 QString mCurrentSyncDevice = manager->getCurrentSyncDevice(); 3034 QString mCurrentSyncDevice = manager->getCurrentSyncDevice();
3027 3035
3028 bool mSyncLauncher = true; 3036 bool mSyncLauncher = true;
3029 3037
3030 bool fullDateRange = false; 3038 bool fullDateRange = false;
3031 int take; 3039 int take;
3032 // local->resetTempSyncStat(); 3040 // local->resetTempSyncStat();
3033 QDateTime mLastSync = QDateTime::currentDateTime(); 3041 QDateTime mLastSync = QDateTime::currentDateTime();
3034 QDateTime modifiedSync = mLastSync; 3042 QDateTime modifiedSync = mLastSync;
3035 3043
3036 unsigned int index; 3044 unsigned int index;
3037 //Step 1. Find syncinfo in Local file and create if not existent. 3045 //Step 1. Find syncinfo in Local file and create if not existent.
3038 bool found = syncLocal->findSyncData(mCurrentSyncDevice, &index); 3046 bool found = syncLocal->findSyncData(mCurrentSyncDevice, &index);
3039 if (found == false) 3047 if (found == false)
3040 { 3048 {
3041 PwMSyncItem newSyncItemLocal; 3049 PwMSyncItem newSyncItemLocal;
3042 newSyncItemLocal.syncName = mCurrentSyncDevice.latin1(); 3050 newSyncItemLocal.syncName = mCurrentSyncDevice.latin1();
3043 newSyncItemLocal.lastSyncDate = mLastSync; 3051 newSyncItemLocal.lastSyncDate = mLastSync;
3044 syncLocal->addSyncDataEntry(&newSyncItemLocal, true); 3052 syncLocal->addSyncDataEntry(&newSyncItemLocal, true);
3045 found = syncLocal->findSyncData(mCurrentSyncDevice, &index); 3053 found = syncLocal->findSyncData(mCurrentSyncDevice, &index);
3046 if (found == false) { 3054 if (found == false) {
3047 qDebug("PwMDoc::syncronize : newly created local sync data could not be found"); 3055 qDebug("PwMDoc::syncronize : newly created local sync data could not be found");
3048 return e_syncError; 3056 return e_syncError;
3049 } 3057 }
3050 } 3058 }
3051 3059
3052 syncItemLocal = syncLocal->getSyncDataEntry(index); 3060 syncItemLocal = syncLocal->getSyncDataEntry(index);
3053 qDebug("Last Sync Local %s ", syncItemLocal->lastSyncDate.toString().latin1()); 3061 qDebug("Last Sync Local %s ", syncItemLocal->lastSyncDate.toString().latin1());
3054 3062
3055 //Step 2. Find syncinfo in remote file and create if not existent. 3063 //Step 2. Find syncinfo in remote file and create if not existent.
3056 found = syncRemote->findSyncData(mCurrentSyncName, &index); 3064 found = syncRemote->findSyncData(mCurrentSyncName, &index);
3057 if (found == false) 3065 if (found == false)
3058 { 3066 {
3059 qDebug("FULLDATE 1"); 3067 qDebug("FULLDATE 1");
3060 fullDateRange = true; 3068 fullDateRange = true;
3061 PwMSyncItem newSyncItemRemote; 3069 PwMSyncItem newSyncItemRemote;
3062 newSyncItemRemote.syncName = mCurrentSyncName.latin1(); 3070 newSyncItemRemote.syncName = mCurrentSyncName.latin1();
3063 newSyncItemRemote.lastSyncDate = mLastSync; 3071 newSyncItemRemote.lastSyncDate = mLastSync;
3064 syncRemote->addSyncDataEntry(&newSyncItemRemote, true); 3072 syncRemote->addSyncDataEntry(&newSyncItemRemote, true);
3065 found = syncRemote->findSyncData(mCurrentSyncName, &index); 3073 found = syncRemote->findSyncData(mCurrentSyncName, &index);
3066 if (found == false) { 3074 if (found == false) {
3067 qDebug("PwMDoc::syncronize : newly created remote sync data could not be found"); 3075 qDebug("PwMDoc::syncronize : newly created remote sync data could not be found");
3068 return e_syncError; 3076 return e_syncError;
3069 } 3077 }
3070 } 3078 }
3071 3079
3072 syncItemRemote = syncRemote->getSyncDataEntry(index); 3080 syncItemRemote = syncRemote->getSyncDataEntry(index);
3073 qDebug("Last Sync Remote %s ", syncItemRemote->lastSyncDate.toString().latin1()); 3081 qDebug("Last Sync Remote %s ", syncItemRemote->lastSyncDate.toString().latin1());
3074 //and remove the found entry here. We will reenter it later again. 3082 //and remove the found entry here. We will reenter it later again.
3075 //US syncRemote->delSyncDataEntry(index, true); 3083 //US syncRemote->delSyncDataEntry(index, true);
3076 3084
3077 3085
3078 if ( syncItemLocal->lastSyncDate == mLastSync ) { 3086 if ( syncItemLocal->lastSyncDate == mLastSync ) {
3079 qDebug("FULLDATE 2"); 3087 qDebug("FULLDATE 2");
3080 fullDateRange = true; 3088 fullDateRange = true;
3081 } 3089 }
3082 3090
3083 if ( ! fullDateRange ) { 3091 if ( ! fullDateRange ) {
3084 if ( syncItemLocal->lastSyncDate != syncItemRemote->lastSyncDate ) { 3092 if ( syncItemLocal->lastSyncDate != syncItemRemote->lastSyncDate ) {
3085 3093
3086 fullDateRange = true; 3094 fullDateRange = true;
3087 qDebug("FULLDATE 3 %s %s", syncItemLocal->lastSyncDate.toString().latin1() , syncItemRemote->lastSyncDate.toString().latin1() ); 3095 qDebug("FULLDATE 3 %s %s", syncItemLocal->lastSyncDate.toString().latin1() , syncItemRemote->lastSyncDate.toString().latin1() );
3088 } 3096 }
3089 } 3097 }
3090 // fullDateRange = true; // debug only! 3098 // fullDateRange = true; // debug only!
3091 if ( fullDateRange ) 3099 if ( fullDateRange )
3092 mLastSync = QDateTime::currentDateTime().addDays( -100*365); 3100 mLastSync = QDateTime::currentDateTime().addDays( -100*365);
3093 else 3101 else
3094 mLastSync = syncItemLocal->lastSyncDate; 3102 mLastSync = syncItemLocal->lastSyncDate;
3095 3103
3096 3104
3097 qDebug("*************************** "); 3105 qDebug("*************************** ");
3098 qDebug("mLastSync %s ",mLastSync.toString().latin1() ); 3106 qDebug("mLastSync %s ",mLastSync.toString().latin1() );
3099 QStringList er = syncRemote->getIDEntryList(); 3107 QStringList er = syncRemote->getIDEntryList();
3100 PwMDataItem* inRemote ;//= er.first(); 3108 PwMDataItem* inRemote ;//= er.first();
3101 PwMDataItem* inLocal; 3109 PwMDataItem* inLocal;
3102 unsigned int catLocal, indexLocal; 3110 unsigned int catLocal, indexLocal;
3103 unsigned int catRemote, indexRemote; 3111 unsigned int catRemote, indexRemote;
3104 3112
3105 QString uid; 3113 QString uid;
3106 manager->showProgressBar(0, i18n("Syncing - close to abort!"), er.count()); 3114 manager->showProgressBar(0, i18n("Syncing - close to abort!"), er.count());
3107 3115
3108 int modulo = (er.count()/10)+1; 3116 int modulo = (er.count()/10)+1;
3109 unsigned int incCounter = 0; 3117 unsigned int incCounter = 0;
3110 while ( incCounter < er.count()) { 3118 while ( incCounter < er.count()) {
3111 if (manager->isProgressBarCanceled()) 3119 if (manager->isProgressBarCanceled())
3112 return e_syncError; 3120 return e_syncError;
3113 if ( incCounter % modulo == 0 ) 3121 if ( incCounter % modulo == 0 )
3114 manager->showProgressBar(incCounter); 3122 manager->showProgressBar(incCounter);
3115 3123
3116 uid = er[ incCounter ]; 3124 uid = er[ incCounter ];
3117 qDebug("sync uid %s from remote file", uid.latin1()); 3125 qDebug("sync uid %s from remote file", uid.latin1());
3118 3126
3119 qApp->processEvents(); 3127 qApp->processEvents();
3120 3128
3121 inLocal = syncLocal->findEntryByID( uid, &catLocal, &indexLocal ); 3129 inLocal = syncLocal->findEntryByID( uid, &catLocal, &indexLocal );
3122 inRemote = syncRemote->findEntryByID( uid, &catRemote, &indexRemote ); 3130 inRemote = syncRemote->findEntryByID( uid, &catRemote, &indexRemote );
3123 PWM_ASSERT(inRemote); 3131 PWM_ASSERT(inRemote);
3124 if ( inLocal != 0 ) { // maybe conflict - same uid in both files 3132 if ( inLocal != 0 ) { // maybe conflict - same uid in both files
3125 if ( (take = takePwMDataItem( inLocal, inRemote, mLastSync, mode, fullDateRange) ) ) { 3133 if ( (take = takePwMDataItem( inLocal, inRemote, mLastSync, mode, fullDateRange) ) ) {
3126 qDebug("take %d %s ", take, inLocal->desc.c_str()); 3134 qDebug("take %d %s ", take, inLocal->desc.c_str());
3127 if ( take == 3 ) 3135 if ( take == 3 )
3128 return e_syncError; 3136 return e_syncError;
3129 if ( take == 1 ) {// take local 3137 if ( take == 1 ) {// take local
3130 inRemote->syncItem(*inLocal, mSyncLauncher); 3138 inRemote->syncItem(*inLocal, mSyncLauncher);
3131 ++changedRemote; 3139 ++changedRemote;
3132 } else { // take == 2 take remote 3140 } else { // take == 2 take remote
3133 inLocal->syncItem(*inRemote, mSyncLauncher); 3141 inLocal->syncItem(*inRemote, mSyncLauncher);
3134 ++changedLocal; 3142 ++changedLocal;
3135 } 3143 }
3136 } 3144 }
3137 } else { // no conflict 3145 } else { // no conflict
3138 if ( inRemote->meta.update > mLastSync || mode == 5 ) { 3146 if ( inRemote->meta.update > mLastSync || mode == 5 ) {
3139 inRemote->meta.update = modifiedSync; 3147 inRemote->meta.update = modifiedSync;
3140 3148
3141 //first check if we have a matching category in the local file 3149 //first check if we have a matching category in the local file
3142 const string* remotecat = syncRemote->getCategory(catRemote); 3150 const string* remotecat = syncRemote->getCategory(catRemote);
3143 syncLocal->addEntry(remotecat->c_str(), inRemote, true, false); 3151 syncLocal->addEntry(remotecat->c_str(), inRemote, true, false);
3144 3152
3145 ++addedPasswordsLocal; 3153 ++addedPasswordsLocal;
3146 } else { 3154 } else {
3147 // pending checkExternSyncAddressee(addresseeRSyncSharp, inR); 3155 // pending checkExternSyncAddressee(addresseeRSyncSharp, inR);
3148 syncRemote->delEntry(catRemote, indexRemote, true); 3156 syncRemote->delEntry(catRemote, indexRemote, true);
3149 ++deletedPasswordsRemote; 3157 ++deletedPasswordsRemote;
3150 } 3158 }
3151 } 3159 }
3152 3160
3153 ++incCounter; 3161 ++incCounter;
3154 } 3162 }
3155 3163
3156 3164
3157 er.clear(); 3165 er.clear();
3158 QStringList el = syncLocal->getIDEntryList(); 3166 QStringList el = syncLocal->getIDEntryList();
3159 modulo = (el.count()/10)+1; 3167 modulo = (el.count()/10)+1;
3160 3168
3161 manager->showProgressBar(0, i18n("Add / remove addressees"), el.count()); 3169 manager->showProgressBar(0, i18n("Add / remove addressees"), el.count());
3162 incCounter = 0; 3170 incCounter = 0;
3163 while ( incCounter < el.count()) { 3171 while ( incCounter < el.count()) {
3164 qApp->processEvents(); 3172 qApp->processEvents();
3165 if (manager->isProgressBarCanceled()) 3173 if (manager->isProgressBarCanceled())
3166 return e_syncError; 3174 return e_syncError;
3167 if ( incCounter % modulo == 0 ) 3175 if ( incCounter % modulo == 0 )
3168 manager->showProgressBar(incCounter); 3176 manager->showProgressBar(incCounter);
3169 uid = el[ incCounter ]; 3177 uid = el[ incCounter ];
3170 qDebug("sync uid %s from local file", uid.latin1()); 3178 qDebug("sync uid %s from local file", uid.latin1());
3171 3179
3172 inLocal = syncLocal->findEntryByID( uid, &catLocal, &indexLocal ); 3180 inLocal = syncLocal->findEntryByID( uid, &catLocal, &indexLocal );
3173 inRemote = syncRemote->findEntryByID( uid, &catRemote, &indexRemote ); 3181 inRemote = syncRemote->findEntryByID( uid, &catRemote, &indexRemote );
3174 PWM_ASSERT(inLocal); 3182 PWM_ASSERT(inLocal);
3175 3183
3176 if ( inRemote == 0 ) { 3184 if ( inRemote == 0 ) {
3177 if ( inLocal->meta.update < mLastSync && mode != 4 ) { 3185 if ( inLocal->meta.update < mLastSync && mode != 4 ) {
3178 // pending checkExternSyncAddressee(addresseeLSyncSharp, inL); 3186 // pending checkExternSyncAddressee(addresseeLSyncSharp, inL);
3179 syncLocal->delEntry(catLocal, indexLocal, true); 3187 syncLocal->delEntry(catLocal, indexLocal, true);
3180 ++deletedPasswordsLocal; 3188 ++deletedPasswordsLocal;
3181 } else { 3189 } else {
3182 if ( ! manager->mWriteBackExistingOnly ) { 3190 if ( ! manager->mWriteBackExistingOnly ) {
3183 ++addedPasswordsRemote; 3191 ++addedPasswordsRemote;
3184 inLocal->meta.update = modifiedSync; 3192 inLocal->meta.update = modifiedSync;
3185 3193
3186 //first check if we have a matching category in the remote file 3194 //first check if we have a matching category in the remote file
3187 const string* localcat = syncLocal->getCategory(catLocal); 3195 const string* localcat = syncLocal->getCategory(catLocal);
3188 3196
3189 PwMDataItem newEntry; 3197 PwMDataItem newEntry;
3190 newEntry = *inLocal; 3198 newEntry = *inLocal;
3191 inRemote = &newEntry; 3199 inRemote = &newEntry;
3192 3200
3193 //USsyncRemote->insertAddressee( inRemote, false ); 3201 //USsyncRemote->insertAddressee( inRemote, false );
3194 syncRemote->addEntry(localcat->c_str(), inRemote, true, false); 3202 syncRemote->addEntry(localcat->c_str(), inRemote, true, false);
3195 3203
3196 } 3204 }
3197 } 3205 }
3198 3206
3199 } 3207 }
3200 ++incCounter; 3208 ++incCounter;
3201 } 3209 }
3202 el.clear(); 3210 el.clear();
3203 manager->hideProgressBar(); 3211 manager->hideProgressBar();
3204 3212
3205 // Now write the info back into the sync data space of the files 3213 // Now write the info back into the sync data space of the files
3206 3214
3207 mLastSync = QDateTime::currentDateTime().addSecs( 1 ); 3215 mLastSync = QDateTime::currentDateTime().addSecs( 1 );
3208 // get rid of micro seconds 3216 // get rid of micro seconds
3209 QTime t = mLastSync.time(); 3217 QTime t = mLastSync.time();
3210 mLastSync.setTime( QTime (t.hour (), t.minute (), t.second () ) ); 3218 mLastSync.setTime( QTime (t.hour (), t.minute (), t.second () ) );
3211 3219
3212 3220
3213 syncItemLocal->lastSyncDate = mLastSync; 3221 syncItemLocal->lastSyncDate = mLastSync;
3214 syncItemRemote->lastSyncDate = mLastSync; 3222 syncItemRemote->lastSyncDate = mLastSync;
3215 3223
3216 QString mes; 3224 QString mes;
3217 mes .sprintf( i18n("Synchronization summary:\n\n %d items added to local\n %d items added to remote\n %d items updated on local\n %d items updated on remote\n %d items deleted on local\n %d items deleted on remote\n"),addedPasswordsLocal, addedPasswordsRemote, changedLocal, changedRemote, deletedPasswordsLocal, deletedPasswordsRemote ); 3225 mes .sprintf( i18n("Synchronization summary:\n\n %d items added to local\n %d items added to remote\n %d items updated on local\n %d items updated on remote\n %d items deleted on local\n %d items deleted on remote\n"),addedPasswordsLocal, addedPasswordsRemote, changedLocal, changedRemote, deletedPasswordsLocal, deletedPasswordsRemote );
3218 if ( manager->mShowSyncSummary ) { 3226 if ( manager->mShowSyncSummary ) {
3219 KMessageBox::information(0, mes, i18n("PWM/Pi Synchronization") ); 3227 KMessageBox::information(0, mes, i18n("PWM/Pi Synchronization") );
3220 } 3228 }
3221 qDebug( mes ); 3229 qDebug( mes );
3222 return e_success; 3230 return e_success;
3223} 3231}
3224 3232
3225 3233
3226int PwMDoc::takePwMDataItem( PwMDataItem* local, PwMDataItem* remote, QDateTime lastSync, int mode , bool full ) 3234int PwMDoc::takePwMDataItem( PwMDataItem* local, PwMDataItem* remote, QDateTime lastSync, int mode , bool full )
3227{ 3235{
3228 // 0 equal 3236 // 0 equal
3229 // 1 take local 3237 // 1 take local
3230 // 2 take remote 3238 // 2 take remote
3231 // 3 cancel 3239 // 3 cancel
3232 QDateTime localMod = local->meta.update; 3240 QDateTime localMod = local->meta.update;
3233 QDateTime remoteMod = remote->meta.update; 3241 QDateTime remoteMod = remote->meta.update;
3234 3242
3235 if ( localMod == remoteMod ) 3243 if ( localMod == remoteMod )
3236 return 0; 3244 return 0;
3237 3245
3238 qDebug(" %d %d conflict on %s %s ", mode, full, local->desc.c_str(), remote->desc.c_str() ); 3246 qDebug(" %d %d conflict on %s %s ", mode, full, local->desc.c_str(), remote->desc.c_str() );
3239 3247
3240 //qDebug("%s %d %s %d", local->lastModified().toString().latin1() , localMod, remote->lastModified().toString().latin1(), remoteMod); 3248 //qDebug("%s %d %s %d", local->lastModified().toString().latin1() , localMod, remote->lastModified().toString().latin1(), remoteMod);
3241 //qDebug("%d %d %d %d ", local->lastModified().time().second(), local->lastModified().time().msec(), remote->lastModified().time().second(), remote->lastModified().time().msec() ); 3249 //qDebug("%d %d %d %d ", local->lastModified().time().second(), local->lastModified().time().msec(), remote->lastModified().time().second(), remote->lastModified().time().msec() );
3242 //full = true; //debug only 3250 //full = true; //debug only
3243 if ( full ) { 3251 if ( full ) {
3244 bool equ = ( (*local) == (*remote) ); 3252 bool equ = ( (*local) == (*remote) );
3245 if ( equ ) { 3253 if ( equ ) {
3246 //qDebug("equal "); 3254 //qDebug("equal ");
3247 if ( mode < SYNC_PREF_FORCE_LOCAL ) 3255 if ( mode < SYNC_PREF_FORCE_LOCAL )
3248 return 0; 3256 return 0;
3249 3257
3250 }//else //debug only 3258 }//else //debug only
3251 //qDebug("not equal %s %s ", local->desc.c_str(), remote->desc.c_str()); 3259 //qDebug("not equal %s %s ", local->desc.c_str(), remote->desc.c_str());
3252 } 3260 }
3253 3261
3254 int result; 3262 int result;
3255 bool localIsNew; 3263 bool localIsNew;
3256 //qDebug("%s -- %s mLastCalendarSync %s lastsync %s --- local %s remote %s ",local->summary().latin1(), remote->summary().latin1(),mLastCalendarSync.toString().latin1() ,lastSync.toString().latin1() , local->lastModified().toString().latin1() , remote->lastModified().toString().latin1() ); 3264 //qDebug("%s -- %s mLastCalendarSync %s lastsync %s --- local %s remote %s ",local->summary().latin1(), remote->summary().latin1(),mLastCalendarSync.toString().latin1() ,lastSync.toString().latin1() , local->lastModified().toString().latin1() , remote->lastModified().toString().latin1() );
3257 3265
3258 if ( full && mode < SYNC_PREF_NEWEST ) 3266 if ( full && mode < SYNC_PREF_NEWEST )
3259 mode = SYNC_PREF_ASK; 3267 mode = SYNC_PREF_ASK;
3260 3268
3261 switch( mode ) { 3269 switch( mode ) {
3262 case SYNC_PREF_LOCAL: 3270 case SYNC_PREF_LOCAL:
3263 if ( lastSync > remoteMod ) 3271 if ( lastSync > remoteMod )
3264 return 1; 3272 return 1;
3265 if ( lastSync > localMod ) 3273 if ( lastSync > localMod )
3266 return 2; 3274 return 2;
3267 return 1; 3275 return 1;
3268 break; 3276 break;
3269 case SYNC_PREF_REMOTE: 3277 case SYNC_PREF_REMOTE:
3270 if ( lastSync > remoteMod ) 3278 if ( lastSync > remoteMod )
3271 return 1; 3279 return 1;
3272 if ( lastSync > localMod ) 3280 if ( lastSync > localMod )
3273 return 2; 3281 return 2;
3274 return 2; 3282 return 2;
3275 break; 3283 break;
3276 case SYNC_PREF_NEWEST: 3284 case SYNC_PREF_NEWEST:
3277 if ( localMod > remoteMod ) 3285 if ( localMod > remoteMod )
3278 return 1; 3286 return 1;
3279 else 3287 else
3280 return 2; 3288 return 2;
3281 break; 3289 break;
3282 case SYNC_PREF_ASK: 3290 case SYNC_PREF_ASK:
3283 //qDebug("lsy %s --- lo %s --- re %s ", lastSync.toString().latin1(), localMod.toString().latin1(), remoteMod.toString().latin1() ); 3291 //qDebug("lsy %s --- lo %s --- re %s ", lastSync.toString().latin1(), localMod.toString().latin1(), remoteMod.toString().latin1() );
3284 if ( lastSync > remoteMod ) 3292 if ( lastSync > remoteMod )
3285 return 1; 3293 return 1;
3286 if ( lastSync > localMod ) 3294 if ( lastSync > localMod )
3287 return 2; 3295 return 2;
3288 localIsNew = localMod >= remoteMod; 3296 localIsNew = localMod >= remoteMod;
3289 //qDebug("conflict! ************************************** "); 3297 //qDebug("conflict! ************************************** ");
3290 { 3298 {
3291 PwMDataItemChooser acd ( *local,*remote, localIsNew , 0/*this*/ ); 3299 PwMDataItemChooser acd ( *local,*remote, localIsNew , 0/*this*/ );
3292 result = acd.executeD(localIsNew); 3300 result = acd.executeD(localIsNew);
3293 return result; 3301 return result;
3294 } 3302 }
3295 break; 3303 break;
3296 case SYNC_PREF_FORCE_LOCAL: 3304 case SYNC_PREF_FORCE_LOCAL:
3297 return 1; 3305 return 1;
3298 break; 3306 break;
3299 case SYNC_PREF_FORCE_REMOTE: 3307 case SYNC_PREF_FORCE_REMOTE:
3300 return 2; 3308 return 2;
3301 break; 3309 break;
3302 3310
3303 default: 3311 default:
3304 // SYNC_PREF_TAKE_BOTH not implemented 3312 // SYNC_PREF_TAKE_BOTH not implemented
3305 break; 3313 break;
3306 } 3314 }
3307 return 0; 3315 return 0;
3308} 3316}
3309 3317
3310void PwMDoc::removeSyncInfo( QString syncProfile) 3318void PwMDoc::removeSyncInfo( QString syncProfile)
3311{ 3319{
3312 bool res, found; 3320 bool res, found;
3313 unsigned int count, i; 3321 unsigned int count, i;
3314 if ( syncProfile.isEmpty() ) { 3322 if ( syncProfile.isEmpty() ) {
3315 count = numSyncDataEntries(); 3323 count = numSyncDataEntries();
3316 for (i = count; count > 0; count-- ) { 3324 for (i = count; count > 0; count-- ) {
3317 res = delSyncDataEntry(i-1, false); 3325 res = delSyncDataEntry(i-1, false);
3318 if (res == false) { 3326 if (res == false) {
3319 qDebug("PwMDoc::removeSyncInfo: could not remove syncprofile"); 3327 qDebug("PwMDoc::removeSyncInfo: could not remove syncprofile");
3320 } 3328 }
3321 } 3329 }
3322 } else { 3330 } else {
3323 found = findSyncData(syncProfile, &count); 3331 found = findSyncData(syncProfile, &count);
3324 if (found == true) 3332 if (found == true)
3325 { 3333 {
3326 res = delSyncDataEntry(count, false); 3334 res = delSyncDataEntry(count, false);
3327 if (res == false) { 3335 if (res == false) {
3328 qDebug("PwMDoc::removeSyncInfo: could not remove %s", syncProfile.latin1()); 3336 qDebug("PwMDoc::removeSyncInfo: could not remove %s", syncProfile.latin1());
3329 3337
3330 } 3338 }
3331 } 3339 }
3332 } 3340 }
3333} 3341}
3334 3342
3335 3343
3336//this are the overwritten callbackmethods from the syncinterface 3344//this are the overwritten callbackmethods from the syncinterface
3337bool PwMDoc::sync(KSyncManager* manager, QString filename, int mode) 3345bool PwMDoc::sync(KSyncManager* manager, QString filename, int mode)
3338{ 3346{
3339 QString mCurrentSyncDevice = manager->getCurrentSyncDevice(); 3347 QString mCurrentSyncDevice = manager->getCurrentSyncDevice();
3340 3348
3341 //1) unlock local file first if necessary (ask for password) 3349 //1) unlock local file first if necessary (ask for password)
3342 if (this->isDeepLocked()) { 3350 if (this->isDeepLocked()) {
3343 PwMerror ret = this->deepLock(false); 3351 PwMerror ret = this->deepLock(false);
3344 if (ret != e_success) 3352 if (ret != e_success)
3345 return false; 3353 return false;
3346 } 3354 }
3347 3355
3348 //2) construct and open a new doc on the stack(automatic cleanup of remote file). 3356 //2) construct and open a new doc on the stack(automatic cleanup of remote file).
3349 PwMDoc syncTarget(this, "synctarget"); 3357 PwMDoc syncTarget(this, "synctarget");
3350 PwMDoc* pSyncTarget = &syncTarget; 3358 PwMDoc* pSyncTarget = &syncTarget;
3351 3359
3352 3360
3353 PwMerror err = pSyncTarget->openDoc(&filename, 1 /*== open with all entries locked*/); 3361 PwMerror err = pSyncTarget->openDoc(&filename, 1 /*== open with all entries locked*/);
3354 3362
3355 if (err == e_alreadyOpen) { 3363 if (err == e_alreadyOpen) {
3356 PwMDocList::listItem li; 3364 PwMDocList::listItem li;
3357 if (getOpenDocList()->find(filename.latin1(), &li)) 3365 if (getOpenDocList()->find(filename.latin1(), &li))
3358 pSyncTarget = li.doc; 3366 pSyncTarget = li.doc;
3359 else { 3367 else {
3360 qDebug("PwmDoc::sync: sync failed. Error %i while opening file %s",err, filename.latin1()); 3368 qDebug("PwmDoc::sync: sync failed. Error %i while opening file %s",err, filename.latin1());
3361 return false; 3369 return false;
3362 } 3370 }
3363 } 3371 }
3364 else if (err != e_success) { 3372 else if (err != e_success) {
3365 qDebug("PwmDoc::sync: sync failed. Error %i while opening file %s",err, filename.latin1()); 3373 qDebug("PwmDoc::sync: sync failed. Error %i while opening file %s",err, filename.latin1());
3366 return false; 3374 return false;
3367 } 3375 }
3368 3376
3369 qDebug("PWM file loaded %s,sync mode %d",filename.latin1(), mode ); 3377 qDebug("PWM file loaded %s,sync mode %d",filename.latin1(), mode );
3370 3378
3371 3379
3372 //3) unlock remote file first if necessary (ask for password) 3380 //3) unlock remote file first if necessary (ask for password)
3373 if (pSyncTarget->isDeepLocked()) { 3381 if (pSyncTarget->isDeepLocked()) {
3374 PwMerror ret = pSyncTarget->deepLock(false); 3382 PwMerror ret = pSyncTarget->deepLock(false);
3375 if (ret != e_success) 3383 if (ret != e_success)
3376 return false; 3384 return false;
3377 } 3385 }
3378 3386
3379 3387
3380 err = syncronize(manager, this, pSyncTarget, mode ); 3388 err = syncronize(manager, this, pSyncTarget, mode );
3381 3389
3382 if (err == e_success) { 3390 if (err == e_success) {
3383 if ( manager->mWriteBackFile ){ 3391 if ( manager->mWriteBackFile ){
3384 qDebug("Saving remote PWManager file"); 3392 qDebug("Saving remote PWManager file");
3385 err = pSyncTarget->saveDoc(conf()->confGlobCompression()); 3393 err = pSyncTarget->saveDoc(conf()->confGlobCompression());
3386 if (err != e_success) { 3394 if (err != e_success) {
3387 qDebug("PwmDoc::sync: Sync failed. Error %i while storing file %s",err, filename.latin1()); 3395 qDebug("PwmDoc::sync: Sync failed. Error %i while storing file %s",err, filename.latin1());
3388 return false; 3396 return false;
3389 } 3397 }
3390 } 3398 }
3391 3399
3392 flagDirty(); 3400 flagDirty();
3393 return true; 3401 return true;
3394 } 3402 }
3395 else { 3403 else {
3396 return false; 3404 return false;
3397 } 3405 }
3398} 3406}
3399 3407
3400#endif 3408#endif
3401 3409
3402 3410
3403bool PwMDoc::findSyncData(const QString &syncname, unsigned int *index) 3411bool PwMDoc::findSyncData(const QString &syncname, unsigned int *index)
3404{ 3412{
3405 vector<PwMSyncItem>::iterator i = dti.syncDta.begin(), 3413 vector<PwMSyncItem>::iterator i = dti.syncDta.begin(),
3406 end = dti.syncDta.end(); 3414 end = dti.syncDta.end();
3407 3415
3408 while (i != end) { 3416 while (i != end) {
3409 if ((*i).syncName == syncname.latin1()) { 3417 if ((*i).syncName == syncname.latin1()) {
3410 if (index) { 3418 if (index) {
3411 *index = i - dti.syncDta.begin(); 3419 *index = i - dti.syncDta.begin();
3412 } 3420 }
3413 return true; 3421 return true;
3414 } 3422 }
3415 ++i; 3423 ++i;
3416 } 3424 }
3417 return false; 3425 return false;
3418}; 3426};
3419 3427
3420/** add new syncdataentry */ 3428/** add new syncdataentry */
3421PwMerror PwMDoc::addSyncDataEntry(PwMSyncItem *d, bool dontFlagDirty) 3429PwMerror PwMDoc::addSyncDataEntry(PwMSyncItem *d, bool dontFlagDirty)
3422{ 3430{
3423 PWM_ASSERT(d); 3431 PWM_ASSERT(d);
3424 3432
3425 if (isDeepLocked()) { 3433 if (isDeepLocked()) {
3426 PwMerror ret; 3434 PwMerror ret;
3427 ret = deepLock(false); 3435 ret = deepLock(false);
3428 if (ret != e_success) 3436 if (ret != e_success)
3429 return e_lock; 3437 return e_lock;
3430 } 3438 }
3431 unsigned int index; 3439 unsigned int index;
3432 3440
3433 const QString tmp = d->syncName.c_str(); 3441 const QString tmp = d->syncName.c_str();
3434 bool exists = findSyncData(d->syncName.c_str(), &index); 3442 bool exists = findSyncData(d->syncName.c_str(), &index);
3435 3443
3436 if (exists == true) { 3444 if (exists == true) {
3437 // DOH! We found this entry. 3445 // DOH! We found this entry.
3438 return e_entryExists; 3446 return e_entryExists;
3439 } 3447 }
3440 3448
3441 dti.syncDta.push_back(*d); 3449 dti.syncDta.push_back(*d);
3442 3450
3443 if (!dontFlagDirty) 3451 if (!dontFlagDirty)
3444 flagDirty(); 3452 flagDirty();
3445 return e_success; 3453 return e_success;
3446} 3454}
3447 3455
3448 3456
3449 3457
3450/** delete syncdata entry */ 3458/** delete syncdata entry */
3451bool PwMDoc::delSyncDataEntry(unsigned int index, bool dontFlagDirty) 3459bool PwMDoc::delSyncDataEntry(unsigned int index, bool dontFlagDirty)
3452{ 3460{
3453 if (isDeepLocked()) 3461 if (isDeepLocked())
3454 return false; 3462 return false;
3455 if (index > dti.syncDta.size() - 1) 3463 if (index > dti.syncDta.size() - 1)
3456 return false; 3464 return false;
3457 3465
3458 // delete entry 3466 // delete entry
3459 dti.syncDta.erase(dti.syncDta.begin() + index); 3467 dti.syncDta.erase(dti.syncDta.begin() + index);
3460 3468
3461 if (!dontFlagDirty) 3469 if (!dontFlagDirty)
3462 flagDirty(); 3470 flagDirty();
3463 return true; 3471 return true;
3464} 3472}
3465 3473
3466 3474
3467PwMDataItem* PwMDoc::findEntryByID(const QString &uid, unsigned int *category, unsigned int *index) 3475PwMDataItem* PwMDoc::findEntryByID(const QString &uid, unsigned int *category, unsigned int *index)
3468{ 3476{
3469 vector<PwMCategoryItem>::iterator catcounter = dti.dta.begin(), 3477 vector<PwMCategoryItem>::iterator catcounter = dti.dta.begin(),
3470 catend = dti.dta.end(); 3478 catend = dti.dta.end();
3471 3479
3472 vector<PwMDataItem>::iterator entrBegin, entrEnd, entrI; 3480 vector<PwMDataItem>::iterator entrBegin, entrEnd, entrI;
3473 3481
3474 while (catcounter != catend) { 3482 while (catcounter != catend) {
diff --git a/pwmanager/pwmanager/pwmdoc.h b/pwmanager/pwmanager/pwmdoc.h
index 138dd3d..9fcdda7 100644
--- a/pwmanager/pwmanager/pwmdoc.h
+++ b/pwmanager/pwmanager/pwmdoc.h
@@ -1,827 +1,831 @@
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#ifndef __PWMDOC_H 20#ifndef __PWMDOC_H
21#define __PWMDOC_H 21#define __PWMDOC_H
22#ifdef _WIN32_ 22#ifdef _WIN32_
23#define ssize_t unsigned int 23#define ssize_t unsigned int
24#endif 24#endif
25 #define PWM_FILE_VER (static_cast<char>(0x05)) 25 #define PWM_FILE_VER (static_cast<char>(0x05))
26 26
27 #define PWM_HASH_SHA1 (static_cast<char>(0x01)) 27 #define PWM_HASH_SHA1 (static_cast<char>(0x01))
28 #define PWM_HASH_SHA256 (static_cast<char>(0x02)) 28 #define PWM_HASH_SHA256 (static_cast<char>(0x02))
29 #define PWM_HASH_SHA384 (static_cast<char>(0x03)) 29 #define PWM_HASH_SHA384 (static_cast<char>(0x03))
30 #define PWM_HASH_SHA512 (static_cast<char>(0x04)) 30 #define PWM_HASH_SHA512 (static_cast<char>(0x04))
31 #define PWM_HASH_MD5 (static_cast<char>(0x05)) 31 #define PWM_HASH_MD5 (static_cast<char>(0x05))
32 #define PWM_HASH_RMD160 (static_cast<char>(0x06)) 32 #define PWM_HASH_RMD160 (static_cast<char>(0x06))
33 #define PWM_HASH_TIGER (static_cast<char>(0x07)) 33 #define PWM_HASH_TIGER (static_cast<char>(0x07))
34 34
35 #define PWM_CRYPT_BLOWFISH(static_cast<char>(0x01)) 35 #define PWM_CRYPT_BLOWFISH(static_cast<char>(0x01))
36 #define PWM_CRYPT_AES128(static_cast<char>(0x02)) 36 #define PWM_CRYPT_AES128(static_cast<char>(0x02))
37 #define PWM_CRYPT_AES192(static_cast<char>(0x03)) 37 #define PWM_CRYPT_AES192(static_cast<char>(0x03))
38 #define PWM_CRYPT_AES256(static_cast<char>(0x04)) 38 #define PWM_CRYPT_AES256(static_cast<char>(0x04))
39 #define PWM_CRYPT_3DES (static_cast<char>(0x05)) 39 #define PWM_CRYPT_3DES (static_cast<char>(0x05))
40 #define PWM_CRYPT_TWOFISH(static_cast<char>(0x06)) 40 #define PWM_CRYPT_TWOFISH(static_cast<char>(0x06))
41 #define PWM_CRYPT_TWOFISH128(static_cast<char>(0x07)) 41 #define PWM_CRYPT_TWOFISH128(static_cast<char>(0x07))
42 42
43 #define PWM_COMPRESS_NONE(static_cast<char>(0x00)) 43 #define PWM_COMPRESS_NONE(static_cast<char>(0x00))
44 #define PWM_COMPRESS_GZIP(static_cast<char>(0x01)) 44 #define PWM_COMPRESS_GZIP(static_cast<char>(0x01))
45 #define PWM_COMPRESS_BZIP2(static_cast<char>(0x02)) 45 #define PWM_COMPRESS_BZIP2(static_cast<char>(0x02))
46 46
47 #define DEFAULT_MAX_ENTRIES(~(static_cast<unsigned int>(0))) 47 #define DEFAULT_MAX_ENTRIES(~(static_cast<unsigned int>(0)))
48 #define FILE_ID_HEADER "PWM_PASSWORD_FILE" 48 #define FILE_ID_HEADER "PWM_PASSWORD_FILE"
49 49
50 50
51#include "pwmexception.h" 51#include "pwmexception.h"
52#include "pwmdocui.h" 52#include "pwmdocui.h"
53 53
54#include <qobject.h> 54#include <qobject.h>
55#include <qtimer.h> 55#include <qtimer.h>
56#include <qdatetime.h> 56#include <qdatetime.h>
57 57
58#include <kprocess.h> 58#include <kprocess.h>
59 59
60#ifndef PWM_EMBEDDED 60#ifndef PWM_EMBEDDED
61#include "configuration.h" 61#include "configuration.h"
62#else 62#else
63#include <kapplication.h> 63#include <kapplication.h>
64#include <ksyncmanager.h> 64#include <ksyncmanager.h>
65#endif 65#endif
66 66
67#include <string> 67#include <string>
68#include <vector> 68#include <vector>
69#include <utility> 69#include <utility>
70 70
71using std::vector; 71using std::vector;
72using std::string; 72using std::string;
73using std::pair; 73using std::pair;
74 74
75/* used in findEntry() function */ 75/* used in findEntry() function */
76 #define SEARCH_IN_DESC (1) 76 #define SEARCH_IN_DESC (1)
77 #define SEARCH_IN_NAME (1 << 1) 77 #define SEARCH_IN_NAME (1 << 1)
78 #define SEARCH_IN_PW (1 << 2) 78 #define SEARCH_IN_PW (1 << 2)
79 #define SEARCH_IN_COMMENT(1 << 3) 79 #define SEARCH_IN_COMMENT(1 << 3)
80 #define SEARCH_IN_URL (1 << 4) 80 #define SEARCH_IN_URL (1 << 4)
81 #define SEARCH_IN_LAUNCHER(1 << 5) 81 #define SEARCH_IN_LAUNCHER(1 << 5)
82 #define SEARCH_IN_ALL (SEARCH_IN_DESC | SEARCH_IN_NAME| \ 82 #define SEARCH_IN_ALL (SEARCH_IN_DESC | SEARCH_IN_NAME| \
83 SEARCH_IN_PW | SEARCH_IN_COMMENT| \ 83 SEARCH_IN_PW | SEARCH_IN_COMMENT| \
84 SEARCH_IN_URL| SEARCH_IN_LAUNCHER) 84 SEARCH_IN_URL| SEARCH_IN_LAUNCHER)
85 85
86/** document deeplocked. Data is out for lunch to disk */ 86/** document deeplocked. Data is out for lunch to disk */
87 #define DOC_STAT_DEEPLOCKED (1) 87 #define DOC_STAT_DEEPLOCKED (1)
88/** encrypted document on disk is dirty. data has to go to disk. */ 88/** encrypted document on disk is dirty. data has to go to disk. */
89 #define DOC_STAT_DISK_DIRTY (1 << 1) 89 #define DOC_STAT_DISK_DIRTY (1 << 1)
90/** we are using a chipcard to encrypt the data */ 90/** we are using a chipcard to encrypt the data */
91 #define DOC_STAT_USE_CHIPCARD (1 << 2) 91 #define DOC_STAT_USE_CHIPCARD (1 << 2)
92/** use "currentPw" to unlock. (This flag is set/unset by a timer) */ 92/** use "currentPw" to unlock. (This flag is set/unset by a timer) */
93 #define DOC_STAT_UNLOCK_WITHOUT_PW(1 << 3) 93 #define DOC_STAT_UNLOCK_WITHOUT_PW(1 << 3)
94 94
95class PwMDoc; 95class PwMDoc;
96class PwMView; 96class PwMView;
97class QFile; 97class QFile;
98 98
99/* meta data for a PwMDataItem */ 99/* meta data for a PwMDataItem */
100struct PwMMetaData 100struct PwMMetaData
101{ 101{
102 PwMMetaData() 102 PwMMetaData()
103 : updateInt (0) 103 : updateInt (0)
104 { } 104 { }
105 /** creation date of the PwMDataItem to which 105 /** creation date of the PwMDataItem to which
106 * this meta data belongs. 106 * this meta data belongs.
107 */ 107 */
108 QDateTimecreate; 108 QDateTimecreate;
109 /** becomes valid on this date */ 109 /** becomes valid on this date */
110 QDateTimevalid; 110 QDateTimevalid;
111 /** expire date */ 111 /** expire date */
112 QDateTimeexpire; 112 QDateTimeexpire;
113 /** update date (last updated at this date) */ 113 /** update date (last updated at this date) */
114 QDateTimeupdate; 114 QDateTimeupdate;
115 /** update interval (in minutes). Time since the 115 /** update interval (in minutes). Time since the
116 * last update to remind the user to update the item. 116 * last update to remind the user to update the item.
117 * 0 disables. 117 * 0 disables.
118 */ 118 */
119 unsigned long updateInt; 119 unsigned long updateInt;
120 120
121 //US ENH: enhancements of the filestructure 121 //US ENH: enhancements of the filestructure
122 /* each entry gets a unique id assigned */ 122 /* each entry gets a unique id assigned */
123 string uniqueid; 123 string uniqueid;
124 124
125 125
126 void clear() 126 void clear()
127 { 127 {
128 create = QDateTime(); 128 create = QDateTime();
129 expire = QDateTime(); 129 expire = QDateTime();
130 update = QDateTime(); 130 update = QDateTime();
131 updateInt = 0; 131 updateInt = 0;
132 uniqueid = KApplication::randomString(8).latin1(); 132 uniqueid = KApplication::randomString(8).latin1();
133 } 133 }
134 134
135 inline bool isValid() const 135 inline bool isValid() const
136 { 136 {
137 if (valid.isNull()) 137 if (valid.isNull())
138 return true; 138 return true;
139 return (valid < QDateTime::currentDateTime()); 139 return (valid < QDateTime::currentDateTime());
140 } 140 }
141 inline bool isExpired() const 141 inline bool isExpired() const
142 { 142 {
143 if (expire.isNull()) 143 if (expire.isNull())
144 return false; 144 return false;
145 return (expire < QDateTime::currentDateTime()); 145 return (expire < QDateTime::currentDateTime());
146 } 146 }
147 inline bool isUpdateIntOver() const 147 inline bool isUpdateIntOver() const
148 { 148 {
149 if (updateInt == 0 || 149 if (updateInt == 0 ||
150 update.isNull()) 150 update.isNull())
151 return false; 151 return false;
152 QDateTime d(update); 152 QDateTime d(update);
153 return (d.addSecs(updateInt * 60) < QDateTime::currentDateTime()); 153 return (d.addSecs(updateInt * 60) < QDateTime::currentDateTime());
154 } 154 }
155}; 155};
156 156
157struct PwMDataItem 157struct PwMDataItem
158{ 158{
159 PwMDataItem() 159 PwMDataItem()
160 : lockStat (true) 160 : lockStat (true)
161 , listViewPos (-1) 161 , listViewPos (-1)
162 , binary (false) 162 , binary (false)
163 , rev (0) 163 , rev (0)
164 { } 164 { }
165 165
166 /** password description */ 166 /** password description */
167 stringdesc; 167 stringdesc;
168 /** user-name */ 168 /** user-name */
169 stringname; 169 stringname;
170 /** the password itself */ 170 /** the password itself */
171 stringpw; 171 stringpw;
172 /** some comment */ 172 /** some comment */
173 stringcomment; 173 stringcomment;
174 /** an URL string */ 174 /** an URL string */
175 stringurl; 175 stringurl;
176 /** launcher. Can be executed as a system() command */ 176 /** launcher. Can be executed as a system() command */
177 stringlauncher; 177 stringlauncher;
178 /** locking status. If locked (true), pw is not emitted through getEntry() */ 178 /** locking status. If locked (true), pw is not emitted through getEntry() */
179 boollockStat; 179 boollockStat;
180 /** position of this item in main "list-view" 180 /** position of this item in main "list-view"
181 * If -1, the position is not yet specified and should be appended to the list 181 * If -1, the position is not yet specified and should be appended to the list
182 */ 182 */
183 intlistViewPos; 183 intlistViewPos;
184 /** does this entry contain binary data? */ 184 /** does this entry contain binary data? */
185 bool binary; 185 bool binary;
186 /** meta data for this data item. */ 186 /** meta data for this data item. */
187 PwMMetaData meta; 187 PwMMetaData meta;
188 /** data revision counter. This counter can be used 188 /** data revision counter. This counter can be used
189 * to easily, efficiently determine if this data item 189 * to easily, efficiently determine if this data item
190 * has changed since some time. 190 * has changed since some time.
191 * This counter is incremented on every update. 191 * This counter is incremented on every update.
192 */ 192 */
193 unsigned int rev; 193 unsigned int rev;
194 194
195 void clear(bool clearMeta = true) 195 void clear(bool clearMeta = true)
196 { 196 {
197 /* NOTE: Don't use .clear() here to be 197 /* NOTE: Don't use .clear() here to be
198 * backward compatible with gcc-2 (Debian Woody) 198 * backward compatible with gcc-2 (Debian Woody)
199 */ 199 */
200 desc = ""; 200 desc = "";
201 name = ""; 201 name = "";
202 pw = ""; 202 pw = "";
203 comment = ""; 203 comment = "";
204 url = ""; 204 url = "";
205 launcher = ""; 205 launcher = "";
206 lockStat = true; 206 lockStat = true;
207 listViewPos = -1; 207 listViewPos = -1;
208 binary = false; 208 binary = false;
209 if (clearMeta) 209 if (clearMeta)
210 meta.clear(); 210 meta.clear();
211 } 211 }
212 //US ENH: we need this operator to compare two items if we have no unique ids 212 //US ENH: we need this operator to compare two items if we have no unique ids
213 //available. Generaly this happens before the first sync 213 //available. Generaly this happens before the first sync
214 214
215 bool PwMDataItem::operator==( const PwMDataItem &a ) const 215 bool PwMDataItem::operator==( const PwMDataItem &a ) const
216 { 216 {
217 //qDebug("oper==%s", a.desc.c_str()); 217 //qDebug("oper==%s", a.desc.c_str());
218 if ( desc != a.desc ) return false; 218 if ( desc != a.desc ) return false;
219 if ( name != a.name ) return false; 219 if ( name != a.name ) return false;
220 if ( pw != a.pw ) return false; 220 if ( pw != a.pw ) return false;
221 if ( comment != a.comment ) return false; 221 if ( comment != a.comment ) return false;
222 if ( url != a.url ) return false; 222 if ( url != a.url ) return false;
223 if ( launcher != a.launcher ) return false; 223 if ( launcher != a.launcher ) return false;
224 //all other field will not be checked. 224 //all other field will not be checked.
225 return true; 225 return true;
226 } 226 }
227 227
228 //US ENH: this sync method actually copies all values from the parameter like the =operator 228 //US ENH: this sync method actually copies all values from the parameter like the =operator
229 //does with two exceptions: listViewPos will not be changed, and the launcher only if required. 229 //does with two exceptions: listViewPos will not be changed, and the launcher only if required.
230 bool PwMDataItem::syncItem(const PwMDataItem &a, bool syncLauncher=true ) 230 bool PwMDataItem::syncItem(const PwMDataItem &a, bool syncLauncher=true )
231 { 231 {
232 desc = a.desc; 232 desc = a.desc;
233 name = a.name; 233 name = a.name;
234 pw = a.pw; 234 pw = a.pw;
235 comment = a.comment; 235 comment = a.comment;
236 url = a.url; 236 url = a.url;
237 if (syncLauncher == true) 237 if (syncLauncher == true)
238 launcher = a.launcher; 238 launcher = a.launcher;
239 meta = a.meta; 239 meta = a.meta;
240 binary = a.binary; 240 binary = a.binary;
241 lockStat = a.lockStat; 241 lockStat = a.lockStat;
242 rev = a.rev; 242 rev = a.rev;
243 243
244 return true; 244 return true;
245 } 245 }
246 246
247}; 247};
248 248
249struct PwMCategoryItem 249struct PwMCategoryItem
250{ 250{
251 /** all PwMDataItems (all passwords) within this category */ 251 /** all PwMDataItems (all passwords) within this category */
252 vector<PwMDataItem>d; 252 vector<PwMDataItem>d;
253 /** category name/description */ 253 /** category name/description */
254 string name; 254 string name;
255 255
256 //US ENH: enhancements of the filestructure 256 //US ENH: enhancements of the filestructure
257 /* each category stores the text for description,name and password */ 257 /* each category stores the text for description,name and password */
258 string desc_text; 258 string desc_text;
259 string name_text; 259 string name_text;
260 string pw_text; 260 string pw_text;
261 261
262 void clear() 262 void clear()
263 { 263 {
264 d.clear(); 264 d.clear();
265 name = ""; 265 name = "";
266 desc_text = "Description"; 266 desc_text = "Description";
267 name_text = "Username"; 267 name_text = "Username";
268 pw_text = "Password"; 268 pw_text = "Password";
269 } 269 }
270}; 270};
271 271
272struct PwMSyncItem 272struct PwMSyncItem
273{ 273{
274 string syncName; 274 string syncName;
275 QDateTime lastSyncDate; 275 QDateTime lastSyncDate;
276 276
277 void clear() 277 void clear()
278 { 278 {
279 lastSyncDate = QDateTime(); 279 lastSyncDate = QDateTime();
280 syncName = ""; 280 syncName = "";
281 } 281 }
282}; 282};
283 283
284struct PwMItem 284struct PwMItem
285{ 285{
286 vector<PwMCategoryItem> dta; 286 vector<PwMCategoryItem> dta;
287 vector<PwMSyncItem> syncDta; 287 vector<PwMSyncItem> syncDta;
288 288
289 void clear() 289 void clear()
290 { 290 {
291 dta.clear(); 291 dta.clear();
292 syncDta.clear(); 292 syncDta.clear();
293 } 293 }
294}; 294};
295 295
296 296
297/** "Function Object" for sort()ing PwMDataItem::listViewPos */ 297/** "Function Object" for sort()ing PwMDataItem::listViewPos */
298class dta_lvp_greater 298class dta_lvp_greater
299{ 299{
300public: 300public:
301 bool operator() (const pair<unsigned int, unsigned int> &d1, 301 bool operator() (const pair<unsigned int, unsigned int> &d1,
302 const pair<unsigned int, unsigned int> &d2) 302 const pair<unsigned int, unsigned int> &d2)
303 { 303 {
304 return d1.second > d2.second; 304 return d1.second > d2.second;
305 } 305 }
306}; 306};
307 307
308/** list of PwMDoc documents and it's IDs */ 308/** list of PwMDoc documents and it's IDs */
309class PwMDocList 309class PwMDocList
310{ 310{
311public: 311public:
312 struct listItem 312 struct listItem
313 { 313 {
314 /** document filename (known as ID, here) */ 314 /** document filename (known as ID, here) */
315 string docId; 315 string docId;
316 /** pointer to the document class */ 316 /** pointer to the document class */
317 PwMDoc *doc; 317 PwMDoc *doc;
318 }; 318 };
319 319
320 PwMDocList() {} 320 PwMDocList() {}
321 321
322 /** add a new item to the list */ 322 /** add a new item to the list */
323 void add(PwMDoc *doc, const string &id); 323 void add(PwMDoc *doc, const string &id);
324 /** changes the contents of an existing item */ 324 /** changes the contents of an existing item */
325 void edit(PwMDoc *doc, const string &newId); 325 void edit(PwMDoc *doc, const string &newId);
326 /** remove the given item */ 326 /** remove the given item */
327 void del(PwMDoc *doc); 327 void del(PwMDoc *doc);
328 /** get the item at index */ 328 /** get the item at index */
329 listItem getAt(int index) 329 listItem getAt(int index)
330 { return docList[index]; } 330 { return docList[index]; }
331 /** find an entry with this id */ 331 /** find an entry with this id */
332 bool find(const string &id, listItem *ret = 0); 332 bool find(const string &id, listItem *ret = 0);
333 /** returns a copy of the list */ 333 /** returns a copy of the list */
334 const vector<listItem>* getList() const 334 const vector<listItem>* getList() const
335 { return &docList; } 335 { return &docList; }
336 336
337 337
338 /** returns a new unique number to extend the name of 338 /** returns a new unique number to extend the name of
339 * an unnamed document. 339 * an unnamed document.
340 */ 340 */
341 static unsigned int getNewUnnamedNumber() 341 static unsigned int getNewUnnamedNumber()
342 { return unnamedDocCnt++; } 342 { return unnamedDocCnt++; }
343 343
344protected: 344protected:
345 /* Hm, I think we shouldn't really use a "list" here, should we? 345 /* Hm, I think we shouldn't really use a "list" here, should we?
346 * So I decided to actually use a vector. 346 * So I decided to actually use a vector.
347 */ 347 */
348 vector<listItem> docList; 348 vector<listItem> docList;
349 /** This value is used to get a new number for yet unnamed 349 /** This value is used to get a new number for yet unnamed
350 * documents. It is incremented on every request. So it's 350 * documents. It is incremented on every request. So it's
351 * theoretically possible to overflow it, but... :) 351 * theoretically possible to overflow it, but... :)
352 */ 352 */
353 static unsigned int unnamedDocCnt; 353 static unsigned int unnamedDocCnt;
354}; 354};
355 355
356/** implements timers for the document */ 356/** implements timers for the document */
357class DocTimer : public QObject 357class DocTimer : public QObject
358{ 358{
359 Q_OBJECT 359 Q_OBJECT
360public: 360public:
361 enum TimerIDs 361 enum TimerIDs
362 { 362 {
363 id_mpwTimer, 363 id_mpwTimer,
364 id_autoLockTimer, 364 id_autoLockTimer,
365 id_metaCheckTimer 365 id_metaCheckTimer
366 }; 366 };
367 367
368public: 368public:
369 DocTimer(PwMDoc *_doc); 369 DocTimer(PwMDoc *_doc);
370 ~DocTimer(); 370 ~DocTimer();
371 371
372 /** start the timer */ 372 /** start the timer */
373 void start(TimerIDs timer); 373 void start(TimerIDs timer);
374 /** stop the timer */ 374 /** stop the timer */
375 void stop(TimerIDs timer); 375 void stop(TimerIDs timer);
376 /** get the lock for a timer. 376 /** get the lock for a timer.
377 * This lock is a recursive lock. When a lock is 377 * This lock is a recursive lock. When a lock is
378 * held, the timer will be stopped and timeout is 378 * held, the timer will be stopped and timeout is
379 * guaranteed to not happen 379 * guaranteed to not happen
380 */ 380 */
381 void getLock(TimerIDs timer); 381 void getLock(TimerIDs timer);
382 /** put a recursive timer lock */ 382 /** put a recursive timer lock */
383 void putLock(TimerIDs timer); 383 void putLock(TimerIDs timer);
384 384
385protected slots: 385protected slots:
386 /** timeout slot for the mpw timer */ 386 /** timeout slot for the mpw timer */
387 void mpwTimeout(); 387 void mpwTimeout();
388 /** timeout slot for the autoLock timer */ 388 /** timeout slot for the autoLock timer */
389 void autoLockTimeout(); 389 void autoLockTimeout();
390 /** timeout slot for the metaCheck timer */ 390 /** timeout slot for the metaCheck timer */
391 void metaCheckTimeout(); 391 void metaCheckTimeout();
392 392
393protected: 393protected:
394 /** pointer to the document associated with this timer. */ 394 /** pointer to the document associated with this timer. */
395 PwMDoc *doc; 395 PwMDoc *doc;
396 /** timer object for mpw timer */ 396 /** timer object for mpw timer */
397 QTimer *mpwTimer; 397 QTimer *mpwTimer;
398 /** timer object for the autoLock timer */ 398 /** timer object for the autoLock timer */
399 QTimer *autoLockTimer; 399 QTimer *autoLockTimer;
400 /** timer object for the metaCheck timer */ 400 /** timer object for the metaCheck timer */
401 QTimer *metaCheckTimer; 401 QTimer *metaCheckTimer;
402 /** lock counter for the mpw timer */ 402 /** lock counter for the mpw timer */
403 unsigned int mpwLock; 403 unsigned int mpwLock;
404 /** lock counter for the autoLock timer */ 404 /** lock counter for the autoLock timer */
405 unsigned int autoLockLock; 405 unsigned int autoLockLock;
406 /** lock counter for the metaCheck timer */ 406 /** lock counter for the metaCheck timer */
407 unsigned int metaCheckLock; 407 unsigned int metaCheckLock;
408}; 408};
409 409
410/** Document class for PwM */ 410/** Document class for PwM */
411//US ENH: derived from KSyncInterfaces, to get called by PwM when a sync is required. 411//US ENH: derived from KSyncInterfaces, to get called by PwM when a sync is required.
412// But PwMDoc is handling the sync by itself. 412// But PwMDoc is handling the sync by itself.
413class PwMDoc : public PwMDocUi, public KSyncInterface 413class PwMDoc : public PwMDocUi, public KSyncInterface
414 414
415{ 415{
416 Q_OBJECT 416 Q_OBJECT
417 friend class DocTimer; 417 friend class DocTimer;
418 418
419public: 419public:
420 /** construtor */ 420 /** construtor */
421 PwMDoc(QObject* parent = 0, const char *name = 0); 421 PwMDoc(QObject* parent = 0, const char *name = 0);
422 /** destructor */ 422 /** destructor */
423 ~PwMDoc(); 423 ~PwMDoc();
424 424
425 /** returns a pointer to a list of all open documents */ 425 /** returns a pointer to a list of all open documents */
426 static PwMDocList* getOpenDocList() 426 static PwMDocList* getOpenDocList()
427 { return &openDocList; } 427 { return &openDocList; }
428 428
429 /** flag document dirty. dta changed */ 429 /** flag document dirty. dta changed */
430 void flagDirty() 430 void flagDirty()
431 { 431 {
432 setDocStatFlag(DOC_STAT_DISK_DIRTY); 432 setDocStatFlag(DOC_STAT_DISK_DIRTY);
433 emitDataChanged(this); 433 emitDataChanged(this);
434 } 434 }
435 /** modified? */ 435 /** modified? */
436 bool isDirty() 436 bool isDirty()
437 { return getDocStatFlag(DOC_STAT_DISK_DIRTY); } 437 { return getDocStatFlag(DOC_STAT_DISK_DIRTY); }
438 /** save document to disk */ 438 /** save document to disk */
439 PwMerror saveDoc(char compress, const QString *file = 0); 439 PwMerror saveDoc(char compress, const QString *file = 0);
440 /** read document from file. 440 /** read document from file.
441 * "openLocked is must be set to either of these values: 441 * "openLocked is must be set to either of these values:
442 * 0 == open with all entries unlocked 442 * 0 == open with all entries unlocked
443 * 1 == open with all entries locked 443 * 1 == open with all entries locked
444 * 2 == open deep-locked 444 * 2 == open deep-locked
445 */ 445 */
446 PwMerror openDoc(const QString *file, int openLocked); 446 PwMerror openDoc(const QString *file, int openLocked);
447 /** export document to ascii-textfile */ 447 /** export document to ascii-textfile */
448 PwMerror exportToText(const QString *file); 448 PwMerror exportToText(const QString *file);
449 /** export document to gpasman / kpasman file */ 449 /** export document to gpasman / kpasman file */
450 PwMerror exportToGpasman(const QString *file); 450 PwMerror exportToGpasman(const QString *file);
451 /** import document from ascii-textfile */ 451 /** import document from ascii-textfile */
452 PwMerror importFromText(const QString *file, int format = -1); 452 PwMerror importFromText(const QString *file, int format = -1);
453 /** import document from gpasman / kpasman file */ 453 /** import document from gpasman / kpasman file */
454 PwMerror importFromGpasman(const QString *file); 454 PwMerror importFromGpasman(const QString *file);
455 /** add new entry */ 455 /** add new entry */
456 PwMerror addEntry(const QString &category, PwMDataItem *d, 456 PwMerror addEntry(const QString &category, PwMDataItem *d,
457 bool dontFlagDirty = false, bool updateMeta = true); 457 bool dontFlagDirty = false, bool updateMeta = true);
458 /** add new category. This function doesn't flag the document dirty! */ 458 /** add new category. This function doesn't flag the document dirty! */
459 PwMerror addCategory(const QString &category, unsigned int *categoryIndex, 459 PwMerror addCategory(const QString &category, unsigned int *categoryIndex,
460 bool checkIfExist = true); 460 bool checkIfExist = true);
461 /** rename an existing category */ 461 /** rename an existing category */
462 bool renameCategory(const QString &category, const QString &newName); 462 bool renameCategory(const QString &category, const QString &newName);
463 /** rename an existing category */ 463 /** rename an existing category */
464 bool renameCategory(unsigned int category, const QString &newName, 464 bool renameCategory(unsigned int category, const QString &newName,
465 bool dontFlagDirty = false); 465 bool dontFlagDirty = false);
466 /** delete an existing category */ 466 /** delete an existing category */
467 bool delCategory(const QString &category); 467 bool delCategory(const QString &category);
468 /** delete an existing category */ 468 /** delete an existing category */
469 bool delCategory(unsigned int category, bool dontFlagDirty = false); 469 bool delCategory(unsigned int category, bool dontFlagDirty = false);
470 /** returns a list of all category-names */ 470 /** returns a list of all category-names */
471 void getCategoryList(vector<string> *list); 471 void getCategoryList(vector<string> *list);
472 /** returns a list of all category-names */ 472 /** returns a list of all category-names */
473 void getCategoryList(QStringList *list); 473 void getCategoryList(QStringList *list);
474 /** returns a list of all entry-descs in the given category */ 474 /** returns a list of all entry-descs in the given category */
475 void getEntryList(const QString &category, QStringList *list); 475 void getEntryList(const QString &category, QStringList *list);
476 /** returns a list of all entry-descs in the given category */ 476 /** returns a list of all entry-descs in the given category */
477 void getEntryList(const QString &category, vector<string> *list); 477 void getEntryList(const QString &category, vector<string> *list);
478 /** returns a list of all entry-descs in the given category */ 478 /** returns a list of all entry-descs in the given category */
479 void getEntryList(unsigned int category, vector<string> *list); 479 void getEntryList(unsigned int category, vector<string> *list);
480 /** returns a list of all entry-descs in the given category */ 480 /** returns a list of all entry-descs in the given category */
481 void getEntryList(unsigned int category, QStringList *list); 481 void getEntryList(unsigned int category, QStringList *list);
482 /** delete entry */ 482 /** delete entry */
483 bool delEntry(const QString &category, unsigned int index, bool dontFlagDirty = false); 483 bool delEntry(const QString &category, unsigned int index, bool dontFlagDirty = false);
484 /** delete entry */ 484 /** delete entry */
485 bool delEntry(unsigned int category, unsigned int index, bool dontFlagDirty = false); 485 bool delEntry(unsigned int category, unsigned int index, bool dontFlagDirty = false);
486 /** edit entry */ 486 /** edit entry */
487 bool editEntry(const QString &oldCategory, const QString &newCategory, 487 bool editEntry(const QString &oldCategory, const QString &newCategory,
488 unsigned int index, PwMDataItem *d, bool updateMeta = true); 488 unsigned int index, PwMDataItem *d, bool updateMeta = true);
489 /** edit entry */ 489 /** edit entry */
490 bool editEntry(unsigned int oldCategory, const QString &newCategory, 490 bool editEntry(unsigned int oldCategory, const QString &newCategory,
491 unsigned int index, PwMDataItem *d, bool updateMeta = true); 491 unsigned int index, PwMDataItem *d, bool updateMeta = true);
492 /** finds the category with the "name" and return it's index */ 492 /** finds the category with the "name" and return it's index */
493 bool findCategory(const QString &name, unsigned int *index); 493 bool findCategory(const QString &name, unsigned int *index);
494 /** search for an entry "find" and check while searching only for 494 /** search for an entry "find" and check while searching only for
495 * the data-fields specified by "searchIn". To set the "searchIn" 495 * the data-fields specified by "searchIn". To set the "searchIn"
496 * value, we may use one or more of the SEARCH_IN_* defines at 496 * value, we may use one or more of the SEARCH_IN_* defines at
497 * the top of this header-file. It returns the positions of all 497 * the top of this header-file. It returns the positions of all
498 * matched entries in "foundPositions". If "breakAfterFound" is true, 498 * matched entries in "foundPositions". If "breakAfterFound" is true,
499 * the function terminates after the first occurence of the entry 499 * the function terminates after the first occurence of the entry
500 * and doesn't go on searching. So foundPositions->size() is never 500 * and doesn't go on searching. So foundPositions->size() is never
501 * > 1 if breakAfterFound is true. 501 * > 1 if breakAfterFound is true.
502 */ 502 */
503 void findEntry(unsigned int category, PwMDataItem find, unsigned int searchIn, 503 void findEntry(unsigned int category, PwMDataItem find, unsigned int searchIn,
504 vector<unsigned int> *foundPositions, bool breakAfterFound = false, 504 vector<unsigned int> *foundPositions, bool breakAfterFound = false,
505 bool caseSensitive = true, bool exactWordMatch = true, 505 bool caseSensitive = true, bool exactWordMatch = true,
506 bool sortByLvp = false); 506 bool sortByLvp = false);
507 /** see the above funtion. This function allows to set the category by name. */ 507 /** see the above funtion. This function allows to set the category by name. */
508 void findEntry(const QString &category, PwMDataItem find, unsigned int searchIn, 508 void findEntry(const QString &category, PwMDataItem find, unsigned int searchIn,
509 vector<unsigned int> *foundPositions, bool breakAfterFound = false, 509 vector<unsigned int> *foundPositions, bool breakAfterFound = false,
510 bool caseSensitive = true, bool exactWordMatch = true, 510 bool caseSensitive = true, bool exactWordMatch = true,
511 bool sortByLvp = false); 511 bool sortByLvp = false);
512 /** returns number of entries */ 512 /** returns number of entries */
513 unsigned int numEntries(const QString &category); 513 unsigned int numEntries(const QString &category);
514 unsigned int numEntries(unsigned int category) 514 unsigned int numEntries(unsigned int category)
515 { return dti.dta[category].d.size(); } 515 { return dti.dta[category].d.size(); }
516 /** returns number of categories */ 516 /** returns number of categories */
517 unsigned int numCategories() 517 unsigned int numCategories()
518 { return dti.dta.size(); } 518 { return dti.dta.size(); }
519 /** returns the name of the category at "index" */ 519 /** returns the name of the category at "index" */
520 const string* getCategory(unsigned int index) 520 const string* getCategory(unsigned int index)
521 { return (&(dti.dta[index].name)); } 521 { return (&(dti.dta[index].name)); }
522 522
523 /** returns the data of item at "index". 523 /** returns the data of item at "index".
524 * It unlocks the entry if it's locked and unlockIfLocked is true. 524 * It unlocks the entry if it's locked and unlockIfLocked is true.
525 * If the entry is locked, but unlockIfLocked is false, it'll not return 525 * If the entry is locked, but unlockIfLocked is false, it'll not return
526 * the pw. 526 * the pw.
527 */ 527 */
528 bool getEntry(const QString &category, unsigned int index, 528 bool getEntry(const QString &category, unsigned int index,
529 PwMDataItem *d, bool unlockIfLocked = false); 529 PwMDataItem *d, bool unlockIfLocked = false);
530 bool getEntry(unsigned int category, unsigned int index, 530 bool getEntry(unsigned int category, unsigned int index,
531 PwMDataItem *d, bool unlockIfLocked = false); 531 PwMDataItem *d, bool unlockIfLocked = false);
532 /** returns the comment-string by looking at the category 532 /** returns the comment-string by looking at the category
533 * and the listViewPos 533 * and the listViewPos
534 */ 534 */
535 PwMerror getCommentByLvp(const QString &category, int listViewPos, 535 PwMerror getCommentByLvp(const QString &category, int listViewPos,
536 string *foundComment); 536 string *foundComment);
537 /** checks if a password is already available. (currentPw) */ 537 /** checks if a password is already available. (currentPw) */
538 bool isPwAvailable() 538 bool isPwAvailable()
539 { return (currentPw != ""); } 539 { return (currentPw != ""); }
540 /** un/lock entry at "index". If needed, ask for password. */ 540 /** un/lock entry at "index". If needed, ask for password. */
541 bool lockAt(const QString &category, unsigned int index, 541 bool lockAt(const QString &category, unsigned int index,
542 bool lock = true); 542 bool lock = true);
543 bool lockAt(unsigned int category, unsigned int index, 543 bool lockAt(unsigned int category, unsigned int index,
544 bool lock = true); 544 bool lock = true);
545 /** returns the lock-status at "index" */ 545 /** returns the lock-status at "index" */
546 bool isLocked(const QString &category, unsigned int index); 546 bool isLocked(const QString &category, unsigned int index);
547 bool isLocked(unsigned int category, unsigned int index) 547 bool isLocked(unsigned int category, unsigned int index)
548 { return dti.dta[category].d[index].lockStat; } 548 { return dti.dta[category].d[index].lockStat; }
549 /** returns the deeplock status */ 549 /** returns the deeplock status */
550 bool isDeepLocked() 550 bool isDeepLocked()
551 { return getDocStatFlag(DOC_STAT_DEEPLOCKED); } 551 { return getDocStatFlag(DOC_STAT_DEEPLOCKED); }
552 /** (un)lock all entries */ 552 /** (un)lock all entries */
553 bool lockAll(bool lock); 553 bool lockAll(bool lock);
554 /** unlocks all entries tempoarly. 554 /** unlocks all entries tempoarly.
555 * 1st NOTE: Be very careful with this function! :) 555 * 1st NOTE: Be very careful with this function! :)
556 * 2nd NOTE: After you have called unlockAll_Tempoary(); , 556 * 2nd NOTE: After you have called unlockAll_Tempoary(); ,
557 * please DON'T forget to call unlockAll_Tempoary(true); 557 * please DON'T forget to call unlockAll_Tempoary(true);
558 * _before_ the user (or someone else) is able to change 558 * _before_ the user (or someone else) is able to change
559 * the document! 559 * the document!
560 * 3rd NOTE: Please DON'T change "dta" while the data is tempoary 560 * 3rd NOTE: Please DON'T change "dta" while the data is tempoary
561 * unlocked! This will cause corruption. 561 * unlocked! This will cause corruption.
562 */ 562 */
563 bool unlockAll_tempoary(bool revert = false); 563 bool unlockAll_tempoary(bool revert = false);
564 /** deep-(un)locks the document. 564 /** deep-(un)locks the document.
565 * deep-locking writes all data to the file, deletes all data 565 * deep-locking writes all data to the file, deletes all data
566 * in memory, but doesn't close the document. 566 * in memory, but doesn't close the document.
567 * deep-locking is only available, if the user previously saved 567 * deep-locking is only available, if the user previously saved
568 * the doc to a file (with a password). 568 * the doc to a file (with a password).
569 * If "saveToFile" is false, it does NOT write the data to the file! 569 * If "saveToFile" is false, it does NOT write the data to the file!
570 */ 570 */
571 PwMerror deepLock(bool lock = true, bool saveToFile = true); 571 PwMerror deepLock(bool lock = true, bool saveToFile = true);
572 /** is unlockable without pw? */ 572 /** is unlockable without pw? */
573 bool unlockWoPw() 573 bool unlockWoPw()
574 { return getDocStatFlag(DOC_STAT_UNLOCK_WITHOUT_PW); } 574 { return getDocStatFlag(DOC_STAT_UNLOCK_WITHOUT_PW); }
575 /** get the "currentPassword" */ 575 /** get the "currentPassword" */
576 const QString& getCurrentPw() 576 const QString& getCurrentPw()
577 { return currentPw; } 577 { return currentPw; }
578 /** open a window and request the user to change the mpw */ 578 /** open a window and request the user to change the mpw */
579 void changeCurrentPw(); 579 void changeCurrentPw();
580 /** set the "listViewPos" variable of "dta" */ 580 /** set the "listViewPos" variable of "dta" */
581 void setListViewPos(const QString &category, unsigned int index, 581 void setListViewPos(const QString &category, unsigned int index,
582 int pos); 582 int pos);
583 /** set the "listViewPos" variable of "dta" */ 583 /** set the "listViewPos" variable of "dta" */
584 void setListViewPos(unsigned int category, unsigned int index, 584 void setListViewPos(unsigned int category, unsigned int index,
585 int pos); 585 int pos);
586 /** get the "listViewPos" variable of "dta" */ 586 /** get the "listViewPos" variable of "dta" */
587 int getListViewPos(const QString &category, unsigned int index); 587 int getListViewPos(const QString &category, unsigned int index);
588 /** set the maximum number of entries allowed */ 588 /** set the maximum number of entries allowed */
589 void setMaxNumEntries(unsigned int num = DEFAULT_MAX_ENTRIES) 589 void setMaxNumEntries(unsigned int num = DEFAULT_MAX_ENTRIES)
590 { maxEntries = num; } 590 { maxEntries = num; }
591 /** get the maximum number of entries allowed */ 591 /** get the maximum number of entries allowed */
592 unsigned int getMaxNumEntries() 592 unsigned int getMaxNumEntries()
593 { return maxEntries; } 593 { return maxEntries; }
594 /** ensure all listViewPos of all dta items are set. (are ! -1). 594 /** ensure all listViewPos of all dta items are set. (are ! -1).
595 * If there are some undefined entries, add them to the end of 595 * If there are some undefined entries, add them to the end of
596 * the listViewPos(itions). */ 596 * the listViewPos(itions). */
597 void ensureLvp(); 597 void ensureLvp();
598 /** execute the "launcher" of this entry */ 598 /** execute the "launcher" of this entry */
599 bool execLauncher(const QString &category, unsigned int entryIndex); 599 bool execLauncher(const QString &category, unsigned int entryIndex);
600 /** see above */ 600 /** see above */
601 bool execLauncher(unsigned int category, unsigned int entryIndex); 601 bool execLauncher(unsigned int category, unsigned int entryIndex);
602 /** open a browser with the URL-section of the given entry */ 602 /** open a browser with the URL-section of the given entry */
603 bool goToURL(const QString &category, unsigned int entryIndex); 603 bool goToURL(const QString &category, unsigned int entryIndex);
604 /** see above */ 604 /** see above */
605 bool goToURL(unsigned int category, unsigned int entryIndex); 605 bool goToURL(unsigned int category, unsigned int entryIndex);
606 /** returns true if there is no entry present in the document. 606 /** returns true if there is no entry present in the document.
607 * Note: The "default" Category is present everytime, so 607 * Note: The "default" Category is present everytime, so
608 * it's checked for it's entries. 608 * it's checked for it's entries.
609 */ 609 */
610 bool isDocEmpty() 610 bool isDocEmpty()
611 { 611 {
612 if (numCategories() > 1) 612 if (numCategories() > 1)
613 return false; 613 return false;
614 if (numEntries(0)) 614 if (numEntries(0))
615 return false; 615 return false;
616 return true; 616 return true;
617 } 617 }
618 /** returns the filename of this doc */ 618 /** returns the filename of this doc */
619 const QString& getFilename() 619 const QString& getFilename()
620 { return filename; } 620 { return filename; }
621 /** returns the title of the doc */ 621 /** returns the title of the doc */
622 QString getTitle(); 622 QString getTitle();
623 /** sets the list-view-pointer hold in the doc */ 623 /** sets the list-view-pointer hold in the doc */
624 void setListViewPointer(PwMView *_listView) 624 void setListViewPointer(PwMView *_listView)
625 { listView = _listView; } 625 { listView = _listView; }
626 /** returns the list-view-pointer */ 626 /** returns the list-view-pointer */
627 PwMView * getListViewPointer() 627 PwMView * getListViewPointer()
628 { return listView; } 628 { return listView; }
629 /** try to delete the doc. The user may be asked to save 629 /** try to delete the doc. The user may be asked to save
630 * the data. The user may cancel the whole operation. 630 * the data. The user may cancel the whole operation.
631 * false is returned, then. 631 * false is returned, then.
632 */ 632 */
633 bool tryDelete(); 633 bool tryDelete();
634 /** is the doc deleted? (with tryDelete() ) */ 634 /** is the doc deleted? (with tryDelete() ) */
635 bool isDeleted() 635 bool isDeleted()
636 { return deleted; } 636 { return deleted; }
637 /** returns the document timer object */ 637 /** returns the document timer object */
638 DocTimer * timer() 638 DocTimer * timer()
639 { return _timer; } 639 { return _timer; }
640 /** get a lock on the dataChanged signal. 640 /** get a lock on the dataChanged signal.
641 * If someone is holding a lock, the signal is not emitted. 641 * If someone is holding a lock, the signal is not emitted.
642 */ 642 */
643 void getDataChangedLock() 643 void getDataChangedLock()
644 { ++dataChangedLock; } 644 { ++dataChangedLock; }
645 /** put the dataChanged lock */ 645 /** put the dataChanged lock */
646 void putDataChangedLock() 646 void putDataChangedLock()
647 { --dataChangedLock; } 647 { --dataChangedLock; }
648 /** returns the revision count of the item at cat/index */ 648 /** returns the revision count of the item at cat/index */
649 unsigned int getEntryRevCnt(unsigned int category, unsigned int index) 649 unsigned int getEntryRevCnt(unsigned int category, unsigned int index)
650 { return dti.dta[category].d[index].rev; } 650 { return dti.dta[category].d[index].rev; }
651 /** returns a const pointer to the entries meta */ 651 /** returns a const pointer to the entries meta */
652 const PwMMetaData * getEntryMeta(unsigned int category, unsigned int index) 652 const PwMMetaData * getEntryMeta(unsigned int category, unsigned int index)
653 { return &(dti.dta[category].d[index].meta); } 653 { return &(dti.dta[category].d[index].meta); }
654 /** is the entry at "category" "index" a binary entry? */ 654 /** is the entry at "category" "index" a binary entry? */
655 bool isBinEntry(unsigned int category, unsigned int index) 655 bool isBinEntry(unsigned int category, unsigned int index)
656 { return dti.dta[category].d[index].binary; } 656 { return dti.dta[category].d[index].binary; }
657 657
658public slots: 658public slots:
659 /** wrapper for PwMTray */ 659 /** wrapper for PwMTray */
660 void _deepUnlock(); 660 void _deepUnlock();
661 661
662signals: 662signals:
663 /** the data of the document has changed and must be updated 663 /** the data of the document has changed and must be updated
664 * in all views. 664 * in all views.
665 * NOTE: use emitDataChanged(PwMDoc *document) to emit this signal! 665 * NOTE: use emitDataChanged(PwMDoc *document) to emit this signal!
666 */ 666 */
667 void dataChanged(PwMDoc *document); 667 void dataChanged(PwMDoc *document);
668 /** the document class is going to close. This signal may be 668 /** the document class is going to close. This signal may be
669 * used to nofify all views, that the user closed the document, 669 * used to nofify all views, that the user closed the document,
670 * so the views can go down, too. 670 * so the views can go down, too.
671 */ 671 */
672 void docClosed(PwMDoc *document); 672 void docClosed(PwMDoc *document);
673 /** somebody just opened the document */ 673 /** somebody just opened the document */
674 void docOpened(PwMDoc *document); 674 void docOpened(PwMDoc *document);
675 /** this document object just got created */ 675 /** this document object just got created */
676 void docCreated(PwMDoc *document); 676 void docCreated(PwMDoc *document);
677 677
678public: 678public:
679 /** emit the dataChanged signal after checking for a lock */ 679 /** emit the dataChanged signal after checking for a lock */
680 void emitDataChanged(PwMDoc *document) 680 void emitDataChanged(PwMDoc *document)
681 { 681 {
682 if (!dataChangedLock) 682 if (!dataChangedLock)
683 emit dataChanged(document); 683 emit dataChanged(document);
684 } 684 }
685 685
686protected: 686protected:
687 /** current file for this doc */ 687 /** current file for this doc */
688 QString filename; 688 QString filename;
689//US ENH: we need a place where we keep the syncentries. So I invented 689//US ENH: we need a place where we keep the syncentries. So I invented
690// struct PwMItem, that has a vector of PwMCategoryItem and vector of PwMSyncItem 690// struct PwMItem, that has a vector of PwMCategoryItem and vector of PwMSyncItem
691 /** holds all data */ 691 /** holds all data */
692 PwMItem dti; 692 PwMItem dti;
693 /** maximum number of entries */ 693 /** maximum number of entries */
694 unsigned int maxEntries; 694 unsigned int maxEntries;
695 /** currently used password to encrypt data */ 695 /** currently used password to encrypt data */
696 QString currentPw; 696 QString currentPw;
697 /** current global document status flags */ 697 /** current global document status flags */
698 unsigned int curDocStat; 698 unsigned int curDocStat;
699 /** browser process for goToURL() */ 699 /** browser process for goToURL() */
700 KProcess browserProc; 700 KProcess browserProc;
701 /** pointer to the list-view, using this document. 701 /** pointer to the list-view, using this document.
702 * As there can only be one list-view per doc, we 702 * As there can only be one list-view per doc, we
703 * don't need a list here. 703 * don't need a list here.
704 */ 704 */
705 PwMView *listView; 705 PwMView *listView;
706 /** unnamedNum is used to store the "unnamed counter" 706 /** unnamedNum is used to store the "unnamed counter"
707 * for this document, while it's unnamed. If it's 0, 707 * for this document, while it's unnamed. If it's 0,
708 * we have to get a new unique one. 708 * we have to get a new unique one.
709 */ 709 */
710 unsigned int unnamedNum; 710 unsigned int unnamedNum;
711 /** is this doc going to be deleted (executing in destructor context) */ 711 /** is this doc going to be deleted (executing in destructor context) */
712 bool deleted; 712 bool deleted;
713 /** document timer */ 713 /** document timer */
714 DocTimer *_timer; 714 DocTimer *_timer;
715 /** lock counter for the "dataChanged" signal */ 715 /** lock counter for the "dataChanged" signal */
716 unsigned int dataChangedLock; 716 unsigned int dataChangedLock;
717 717
718 /** list of all open documents */ 718 /** list of all open documents */
719 static PwMDocList openDocList; 719 static PwMDocList openDocList;
720 720
721protected: 721protected:
722 /** serialize "dta" and return it in "d". */ 722 /** serialize "dta" and return it in "d". */
723 bool serializeDta(string *d); 723 bool serializeDta(string *d);
724 /** de-serialize "d" and overwrite "dta" */ 724 /** de-serialize "d" and overwrite "dta" */
725 bool deSerializeDta(const string *d, bool entriesLocked); 725 bool deSerializeDta(const string *d, bool entriesLocked);
726 /** write header to file */ 726 /** write header to file */
727 PwMerror writeFileHeader(char keyHash, char dataHash, char crypt, char compress, 727 PwMerror writeFileHeader(char keyHash, char dataHash, char crypt, char compress,
728 QString *pw, QFile *f); 728 QString *pw, QFile *f);
729 /** write data-hash to file */ 729 /** write data-hash to file */
730 PwMerror writeDataHash(char dataHash, string *d, QFile *f); 730 PwMerror writeDataHash(char dataHash, string *d, QFile *f);
731 /** check header. Read header info and verify key-hash and filever. 731 /** check header. Read header info and verify key-hash and filever.
732 * returns length of header in "headerLength" */ 732 * returns length of header in "headerLength" */
733 PwMerror checkHeader(char *cryptAlgo, QString *pw, char *compress, 733 PwMerror checkHeader(char *cryptAlgo, QString *pw, char *compress,
734 unsigned int *headerLength, char *dataHashType, 734 unsigned int *headerLength, char *dataHashType,
735 string *dataHash, QFile *f); 735 string *dataHash, QFile *f);
736 /** check the data-hash */ 736 /** check the data-hash */
737 PwMerror checkDataHash(char dataHashType, const string *dataHash, const string *dataStream); 737 PwMerror checkDataHash(char dataHashType, const string *dataHash, const string *dataStream);
738 /** encrypt data "d" and write to "filename" */ 738 /** encrypt data "d" and write to "filename" */
739 PwMerror encrypt(string *d, const QString *pw, QFile *f, char algo); 739 PwMerror encrypt(string *d, const QString *pw, QFile *f, char algo,
740 char _hashalgo //US BUG: pass _hashalgo because we need it in hashPassphrase
741);
740 /** read data from file beginning at "pos", decrypt and return it */ 742 /** read data from file beginning at "pos", decrypt and return it */
741 PwMerror decrypt(string *d, unsigned int pos, const QString *pw, char algo, QFile *f); 743 PwMerror decrypt(string *d, unsigned int pos, const QString *pw, char algo,
744 char _hashalgo, //US BUG: pass _hashalgo because we need it in hashPassphrase
745QFile *f);
742 /** compress the data */ 746 /** compress the data */
743 bool compressDta(string *d, char algo); 747 bool compressDta(string *d, char algo);
744 /** uncompress the data */ 748 /** uncompress the data */
745 bool decompressDta(string *d, char algo); 749 bool decompressDta(string *d, char algo);
746 /** internal import function for a text-file generated by PwM. 750 /** internal import function for a text-file generated by PwM.
747 * If this is not a valid PwM-exported file, it returns e_fileFormat */ 751 * If this is not a valid PwM-exported file, it returns e_fileFormat */
748 PwMerror importText_PwM(const QString *file); 752 PwMerror importText_PwM(const QString *file);
749 /** PwM-text-import helper function to extract the name/pw/comment out 753 /** PwM-text-import helper function to extract the name/pw/comment out
750 * of one entry-line */ 754 * of one entry-line */
751 bool textExtractEntry_PwM(const char *in, ssize_t in_size, string *out); 755 bool textExtractEntry_PwM(const char *in, ssize_t in_size, string *out);
752 /** compare two strings */ 756 /** compare two strings */
753 bool compareString(const string &s1, const string &s2, bool caseSensitive, 757 bool compareString(const string &s1, const string &s2, bool caseSensitive,
754 bool exactWordMatch); 758 bool exactWordMatch);
755 /** clears all document-data */ 759 /** clears all document-data */
756 void clearDoc(); 760 void clearDoc();
757 /** delete all empty categories */ 761 /** delete all empty categories */
758 void delAllEmptyCat(bool dontFlagDirty); 762 void delAllEmptyCat(bool dontFlagDirty);
759 /** set a document status flag */ 763 /** set a document status flag */
760 void setDocStatFlag(unsigned int statFlag) 764 void setDocStatFlag(unsigned int statFlag)
761 { curDocStat |= statFlag; } 765 { curDocStat |= statFlag; }
762 /** unset a document status flag */ 766 /** unset a document status flag */
763 void unsetDocStatFlag(unsigned int statFlag) 767 void unsetDocStatFlag(unsigned int statFlag)
764 { curDocStat &= ~statFlag; } 768 { curDocStat &= ~statFlag; }
765 /** get a document status flag */ 769 /** get a document status flag */
766 bool getDocStatFlag(unsigned int statFlag) const 770 bool getDocStatFlag(unsigned int statFlag) const
767 { return (curDocStat & statFlag); } 771 { return (curDocStat & statFlag); }
768 /** set the "currentPassword" */ 772 /** set the "currentPassword" */
769 void setCurrentPw(const QString &pw) 773 void setCurrentPw(const QString &pw)
770 { 774 {
771 currentPw = pw; 775 currentPw = pw;
772 setDocStatFlag(DOC_STAT_DISK_DIRTY); 776 setDocStatFlag(DOC_STAT_DISK_DIRTY);
773 } 777 }
774 /** make a backup-copy of the given file */ 778 /** make a backup-copy of the given file */
775 bool backupFile(const QString &filePath); 779 bool backupFile(const QString &filePath);
776 /** copy a file from src to dst */ 780 /** copy a file from src to dst */
777 bool copyFile(const QString &src, const QString &dst); 781 bool copyFile(const QString &src, const QString &dst);
778 782
779 783
780 public: 784 public:
781#ifdef PWM_EMBEDDED 785#ifdef PWM_EMBEDDED
782 //US ENH: this is the magic function that syncronizes the local doc with the remote doc. 786 //US ENH: this is the magic function that syncronizes the local doc with the remote doc.
783 PwMerror syncronize(KSyncManager* manager, PwMDoc* syncLocal, PwMDoc* syncRemote, int mode ); 787 PwMerror syncronize(KSyncManager* manager, PwMDoc* syncLocal, PwMDoc* syncRemote, int mode );
784 788
785 //takePwMDataItem returns the following values 789 //takePwMDataItem returns the following values
786 // 0 equal 790 // 0 equal
787 // 1 take local 791 // 1 take local
788 // 2 take remote 792 // 2 take remote
789 // 3 cancel 793 // 3 cancel
790 int takePwMDataItem( PwMDataItem* local, PwMDataItem* remote, QDateTime lastSync, int mode , bool full ); 794 int takePwMDataItem( PwMDataItem* local, PwMDataItem* remote, QDateTime lastSync, int mode , bool full );
791 795
792 //the following methods are the overwritten callbackmethods from the syncinterface 796 //the following methods are the overwritten callbackmethods from the syncinterface
793 virtual bool sync(KSyncManager* manager, QString filename, int mode); 797 virtual bool sync(KSyncManager* manager, QString filename, int mode);
794 virtual void removeSyncInfo( QString syncProfile); 798 virtual void removeSyncInfo( QString syncProfile);
795 799
796#endif 800#endif
797 //US ENH: helpermethods to return a whole category entry 801 //US ENH: helpermethods to return a whole category entry
798 /** returns a pointer to the categoryitem */ 802 /** returns a pointer to the categoryitem */
799 PwMCategoryItem* getCategoryEntry(unsigned int index) 803 PwMCategoryItem* getCategoryEntry(unsigned int index)
800 { return &(dti.dta[index]); } 804 { return &(dti.dta[index]); }
801 805
802 private: 806 private:
803 //US ENH: helpermethods to access the sync data for a certain syncname. 807 //US ENH: helpermethods to access the sync data for a certain syncname.
804 // It returns the syncdatas index 808 // It returns the syncdatas index
805 bool findSyncData(const QString &syncname, unsigned int *index); 809 bool findSyncData(const QString &syncname, unsigned int *index);
806 810
807 /** add new syncdataentry */ 811 /** add new syncdataentry */
808 PwMerror addSyncDataEntry(PwMSyncItem *d, bool dontFlagDirty = false); 812 PwMerror addSyncDataEntry(PwMSyncItem *d, bool dontFlagDirty = false);
809 813
810 /** returns a pointer to the syncdata */ 814 /** returns a pointer to the syncdata */
811 PwMSyncItem* getSyncDataEntry(unsigned int index) 815 PwMSyncItem* getSyncDataEntry(unsigned int index)
812 { return &(dti.syncDta[index]); } 816 { return &(dti.syncDta[index]); }
813 817
814 /** delete entry */ 818 /** delete entry */
815 bool delSyncDataEntry(unsigned int index, bool dontFlagDirty = false); 819 bool delSyncDataEntry(unsigned int index, bool dontFlagDirty = false);
816 820
817 /** returns number of categories */ 821 /** returns number of categories */
818 unsigned int numSyncDataEntries() 822 unsigned int numSyncDataEntries()
819 { return dti.syncDta.size(); } 823 { return dti.syncDta.size(); }
820 824
821 PwMDataItem* findEntryByID(const QString &uid, unsigned int *category, unsigned int *index); 825 PwMDataItem* findEntryByID(const QString &uid, unsigned int *category, unsigned int *index);
822 826
823 QStringList getIDEntryList(); 827 QStringList getIDEntryList();
824 828
825}; 829};
826 830
827#endif 831#endif