summaryrefslogtreecommitdiffabout
path: root/pwmanager/pwmanager/csv.cpp
Unidiff
Diffstat (limited to 'pwmanager/pwmanager/csv.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--pwmanager/pwmanager/csv.cpp36
1 files changed, 19 insertions, 17 deletions
diff --git a/pwmanager/pwmanager/csv.cpp b/pwmanager/pwmanager/csv.cpp
index 194edf2..226cc08 100644
--- a/pwmanager/pwmanager/csv.cpp
+++ b/pwmanager/pwmanager/csv.cpp
@@ -10,52 +10,54 @@
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 * The original file version was 1.2 16 * The original file version was 1.2
17 * $Id$ 17 * $Id$
18 **************************************************************************/ 18 **************************************************************************/
19 19
20#include "csv.h" 20#include "csv.h"
21#include "pwmdoc.h" 21#include "pwmdoc.h"
22#include "pwmexception.h" 22#include "pwmexception.h"
23 23
24#include <kmessagebox.h> 24#include <kmessagebox.h>
25#include <klocale.h> 25#include <klocale.h>
26//Added by qt3to4:
27#include <Q3CString>
26 28
27 #define MAX_CSV_FILE_SIZE(50 * 1024 * 1024) // bytes 29 #define MAX_CSV_FILE_SIZE(50 * 1024 * 1024) // bytes
28 30
29 31
30Csv::Csv(QWidget *_parent) 32Csv::Csv(QWidget *_parent)
31 : parent (_parent) 33 : parent (_parent)
32{ 34{
33} 35}
34 36
35Csv::~Csv() 37Csv::~Csv()
36{ 38{
37} 39}
38 40
39bool Csv::importData(const QString &filepath, 41bool Csv::importData(const QString &filepath,
40 PwMDoc *doc) 42 PwMDoc *doc)
41{ 43{
42 bool ret = true; 44 bool ret = true;
43 QByteArray d; 45 QByteArray d;
44 QFile f(filepath); 46 QFile f(filepath);
45 if (!f.open(IO_ReadOnly)) { 47 if (!f.open(QIODevice::ReadOnly)) {
46 KMessageBox::error(parent, 48 KMessageBox::error(parent,
47 i18n("Could not open file.\n" 49 i18n("Could not open file.\n"
48 "Does the file exist?"), 50 "Does the file exist?"),
49 i18n("Open error.")); 51 i18n("Open error."));
50 ret = false; 52 ret = false;
51 goto out; 53 goto out;
52 } 54 }
53 if (f.size() > MAX_CSV_FILE_SIZE) { 55 if (f.size() > MAX_CSV_FILE_SIZE) {
54 KMessageBox::error(parent, 56 KMessageBox::error(parent,
55 i18n("File too big.\nMaximum file size is 1 Byte.", "File too big.\nMaximum file size is %n Bytes.", MAX_CSV_FILE_SIZE), 57 i18n("File too big.\nMaximum file size is 1 Byte.", "File too big.\nMaximum file size is %n Bytes.", MAX_CSV_FILE_SIZE),
56 i18n("File too big.")); 58 i18n("File too big."));
57 ret = false; 59 ret = false;
58 goto out_close; 60 goto out_close;
59 } 61 }
60 d = f.readAll(); 62 d = f.readAll();
61 if (d.isEmpty()) { 63 if (d.isEmpty()) {
@@ -76,119 +78,119 @@ bool Csv::importData(const QString &filepath,
76 78
77out_close: 79out_close:
78 f.close(); 80 f.close();
79out: 81out:
80 return ret; 82 return ret;
81} 83}
82 84
83bool Csv::doImport(const QByteArray &d, 85bool Csv::doImport(const QByteArray &d,
84 PwMDoc *doc) 86 PwMDoc *doc)
85{ 87{
86 PwMDataItem di; 88 PwMDataItem di;
87 //US ENH: initialize all members: 89 //US ENH: initialize all members:
88 di.clear(); 90 di.clear();
89 91
90 int refIndex = 0; 92 int refIndex = 0;
91 int ret; 93 int ret;
92 QCString s, curCat; 94 Q3CString s, curCat;
93 int fieldIndex = 0; 95 int fieldIndex = 0;
94 bool inRecord = false; 96 bool inRecord = false;
95 /* fieldIndex is a reference count to see which 97 /* fieldIndex is a reference count to see which
96 * value we are attaching to di. 98 * value we are attaching to di.
97 * Valid counts are: 99 * Valid counts are:
98 * 0 -> category 100 * 0 -> category
99 * 1 -> desc 101 * 1 -> desc
100 * 2 -> name 102 * 2 -> name
101 * 3 -> pw 103 * 3 -> pw
102 * 4 -> url 104 * 4 -> url
103 * 5 -> launcher 105 * 5 -> launcher
104 * 6 -> comment 106 * 6 -> comment
105 */ 107 */
106 108
107 while (1) { 109 while (1) {
108 ret = nextField(&s, d, inRecord, &refIndex); 110 ret = nextField(&s, d, inRecord, &refIndex);
109 switch (ret) { 111 switch (ret) {
110 case 0: 112 case 0:
111 // successfully got next field. 113 // successfully got next field.
112 inRecord = true; 114 inRecord = true;
113 switch (fieldIndex) { 115 switch (fieldIndex) {
114 case 0: // category 116 case 0: // category
115 if (s.isEmpty()) { 117 if (s.isEmpty()) {
116 /* This is special case. It's the category 118 /* This is special case. It's the category
117 * list terminating empty field. 119 * list terminating empty field.
118 */ 120 */
119 ++fieldIndex; 121 ++fieldIndex;
120 } else 122 } else
121 curCat = s; 123 curCat = s;
122 break; 124 break;
123 case 1:// desc 125 case 1:// desc
124 di.desc = s; 126 di.desc = std::string(s);
125 ++fieldIndex; 127 ++fieldIndex;
126 break; 128 break;
127 case 2: // name 129 case 2: // name
128 di.name = s; 130 di.name = std::string(s);
129 ++fieldIndex; 131 ++fieldIndex;
130 break; 132 break;
131 case 3: // pw 133 case 3: // pw
132 di.pw = s; 134 di.pw = std::string(s);
133 ++fieldIndex; 135 ++fieldIndex;
134 break; 136 break;
135 case 4: // url 137 case 4: // url
136 di.url = s; 138 di.url = std::string(s);
137 ++fieldIndex; 139 ++fieldIndex;
138 break; 140 break;
139 case 5: // launcher 141 case 5: // launcher
140 di.launcher = s; 142 di.launcher = std::string(s);
141 ++fieldIndex; 143 ++fieldIndex;
142 break; 144 break;
143 case 6: // comment 145 case 6: // comment
144 di.comment = s; 146 di.comment = std::string(s);
145 ++fieldIndex; 147 ++fieldIndex;
146 break; 148 break;
147 default: 149 default:
148 /* Too many fields in a record. 150 /* Too many fields in a record.
149 * We simply throw it away. 151 * We simply throw it away.
150 */ 152 */
151 break; 153 break;
152 } 154 }
153 break; 155 break;
154 case 1: 156 case 1:
155 // record complete. 157 // record complete.
156 if (fieldIndex == 6) 158 if (fieldIndex == 6)
157 di.comment = s; 159 di.comment = std::string(s);
158 inRecord = false; 160 inRecord = false;
159 fieldIndex = 0; 161 fieldIndex = 0;
160 doc->addEntry(curCat, &di, true); 162 doc->addEntry(curCat, &di, true);
161 //US ENH: clear di for the next row 163 //US ENH: clear di for the next row
162 di.clear(); 164 di.clear();
163 break; 165 break;
164 case 2: 166 case 2:
165 // data completely parsed. 167 // data completely parsed.
166 doc->flagDirty(); 168 doc->flagDirty();
167 return true; 169 return true;
168 case -1: 170 case -1:
169 // parse error 171 // parse error
170 doc->flagDirty(); 172 doc->flagDirty();
171 return false; 173 return false;
172 } 174 }
173 } 175 }
174 BUG(); 176 BUG();
175 return false; 177 return false;
176} 178}
177 179
178int Csv::nextField(QCString *ret, 180int Csv::nextField(Q3CString *ret,
179 const QByteArray &in, 181 const QByteArray &in,
180 bool inRecord, 182 bool inRecord,
181 int *_refIndex) 183 int *_refIndex)
182{ 184{
183 int rv = -2; 185 int rv = -2;
184 char c; 186 char c;
185 bool inField = false; 187 bool inField = false;
186 bool isQuoted = false; 188 bool isQuoted = false;
187 bool searchingTerminator = false; 189 bool searchingTerminator = false;
188 int refIndex; 190 int refIndex;
189 int inSize = static_cast<int>(in.size()); 191 int inSize = static_cast<int>(in.size());
190 ret->truncate(0); 192 ret->truncate(0);
191 193
192 for (refIndex = *_refIndex; refIndex < inSize; ++refIndex) { 194 for (refIndex = *_refIndex; refIndex < inSize; ++refIndex) {
193 c = in.at(refIndex); 195 c = in.at(refIndex);
194 if (!inField) { 196 if (!inField) {
@@ -340,89 +342,89 @@ bool Csv::exportData(const QString &filepath,
340 if (QFile::exists(filepath)) { 342 if (QFile::exists(filepath)) {
341 int ret; 343 int ret;
342 ret = KMessageBox::questionYesNo(parent, 344 ret = KMessageBox::questionYesNo(parent,
343 i18n("This file does already exist.\n" 345 i18n("This file does already exist.\n"
344 "Do you want to overwrite it?"), 346 "Do you want to overwrite it?"),
345 i18n("Overwrite file?")); 347 i18n("Overwrite file?"));
346 if (ret == KMessageBox::No) 348 if (ret == KMessageBox::No)
347 return false; 349 return false;
348 if (!QFile::remove(filepath)) { 350 if (!QFile::remove(filepath)) {
349 KMessageBox::error(parent, 351 KMessageBox::error(parent,
350 i18n("Could not delete the old file."), 352 i18n("Could not delete the old file."),
351 i18n("Delete error.")); 353 i18n("Delete error."));
352 return false; 354 return false;
353 } 355 }
354 } 356 }
355 QFile f(filepath); 357 QFile f(filepath);
356 if (!f.open(IO_ReadWrite)) { 358 if (!f.open(QIODevice::ReadWrite)) {
357 KMessageBox::error(parent, 359 KMessageBox::error(parent,
358 i18n("Could not open file for writing."), 360 i18n("Could not open file for writing."),
359 i18n("Open error.")); 361 i18n("Open error."));
360 ret = false; 362 ret = false;
361 goto out; 363 goto out;
362 } 364 }
363 doc->unlockAll_tempoary(); 365 doc->unlockAll_tempoary();
364 if (!doExport(f, doc)) 366 if (!doExport(f, doc))
365 ret = false; 367 ret = false;
366 doc->unlockAll_tempoary(true); 368 doc->unlockAll_tempoary(true);
367 f.close(); 369 f.close();
368out: 370out:
369 return ret; 371 return ret;
370} 372}
371 373
372bool Csv::doExport(QFile &f, 374bool Csv::doExport(QFile &f,
373 PwMDoc *doc) 375 PwMDoc *doc)
374{ 376{
375 unsigned int numCat = doc->numCategories(); 377 unsigned int numCat = doc->numCategories();
376 unsigned int numEntr; 378 unsigned int numEntr;
377 unsigned int i, j; 379 unsigned int i, j;
378 PwMDataItem d; 380 PwMDataItem d;
379 QCString s, catName; 381 Q3CString s, catName;
380 QByteArray b; 382 QByteArray b;
381 383
382 for (i = 0; i < numCat; ++i) { 384 for (i = 0; i < numCat; ++i) {
383 numEntr = doc->numEntries(i); 385 numEntr = doc->numEntries(i);
384 catName = newField(doc->getCategory(i)->c_str()); 386 catName = newField(doc->getCategory(i)->c_str());
385 for (j = 0; j < numEntr; ++j) { 387 for (j = 0; j < numEntr; ++j) {
386 doc->getEntry(i, j, &d); 388 doc->getEntry(i, j, &d);
387 s = catName; 389 s = catName;
388 s += ",,"; 390 s += ",,";
389 s += newField(d.desc.c_str()); 391 s += newField(d.desc.c_str());
390 s += ","; 392 s += ",";
391 s += newField(d.name.c_str()); 393 s += newField(d.name.c_str());
392 s += ","; 394 s += ",";
393 s += newField(d.pw.c_str()); 395 s += newField(d.pw.c_str());
394 s += ","; 396 s += ",";
395 s += newField(d.url.c_str()); 397 s += newField(d.url.c_str());
396 s += ","; 398 s += ",";
397 s += newField(d.launcher.c_str()); 399 s += newField(d.launcher.c_str());
398 s += ","; 400 s += ",";
399 s += newField(d.comment.c_str()); 401 s += newField(d.comment.c_str());
400 s += "\r\n"; 402 s += "\r\n";
401 b = s; 403 b = s;
402 // remove \0 termination 404 // remove \0 termination
403#ifndef PWM_EMBEDDED 405#ifndef PWM_EMBEDDED
404 b.resize(b.size() - 1, QGArray::SpeedOptim); 406 b.resize(b.size() - 1, Q3GArray::SpeedOptim);
405#else 407#else
406 b.resize(b.size() - 1); 408 b.resize(b.size() - 1);
407#endif 409#endif
408 if (!f.writeBlock(b)) 410 if (!f.writeBlock(b))
409 return false; 411 return false;
410 } 412 }
411 } 413 }
412 return true; 414 return true;
413} 415}
414 416
415QCString Csv::newField(QCString s) 417Q3CString Csv::newField(Q3CString s)
416{ 418{
417 if (s.isEmpty()) 419 if (s.isEmpty())
418 return QCString(); 420 return Q3CString();
419 QCString ret("\""); 421 Q3CString ret("\"");
420#ifndef PWM_EMBEDDED 422#ifndef PWM_EMBEDDED
421 s.replace('\"', "\"\""); 423 s.replace('\"', "\"\"");
422#else 424#else
423 s.replace(QRegExp("\""), "\"\""); 425 s.replace("\"", "\"\"");
424#endif 426#endif
425 ret += s; 427 ret += s;
426 ret += "\""; 428 ret += "\"";
427 return ret; 429 return ret;
428} 430}