1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
|
/***************************************************************************
mymoneyqifprofile.cpp - description
-------------------
begin : Tue Dec 24 2002
copyright : (C) 2002 by Thomas Baumgart
email : thb@net-bembel.de
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
// ----------------------------------------------------------------------------
// QT Includes
#include <tqregexp.h>
#include <tqvaluevector.h>
// ----------------------------------------------------------------------------
// KDE Includes
#include <kglobal.h>
#include <kconfig.h>
#include <klocale.h>
#include <kcalendarsystem.h>
// ----------------------------------------------------------------------------
// Project Includes
#include "mymoneyqifprofile.h"
#include "../mymoney/mymoneyexception.h"
#include "../mymoney/mymoneymoney.h"
/*
* CENTURY_BREAK is used to identfy the century for a two digit year
*
* if yr is < CENTURY_BREAK it is in 2000
* if yr is >= CENTURY_BREAK it is in 1900
*
* so with CENTURY_BREAK being 70 the following will happen:
*
* 00..69 -> 2000..2069
* 70..99 -> 1970..1999
*/
#define CENTURY_BREAK 70
class MyMoneyQifProfile::Private {
public:
Private() {
m_changeCount.resize(3, 0);
m_lastValue.resize(3, 0);
m_largestValue.resize(3, 0);
}
void getThirdPosition(void);
void dissectDate(TQValueVector<TQString>& parts, const TQString& txt) const;
TQValueVector<int> m_changeCount;
TQValueVector<int> m_lastValue;
TQValueVector<int> m_largestValue;
TQMap<TQChar, int> m_partPos;
};
void MyMoneyQifProfile::Private::dissectDate(TQValueVector<TQString>& parts, const TQString& txt) const
{
TQRegExp nonDelimChars("[ 0-9a-zA-Z]");
int part = 0; // the current part we scan
unsigned int pos; // the current scan position
unsigned int maxPartSize = txt.length() > 6 ? 4 : 2;
// the maximum size of a part
// some fu... up MS-Money versions write two delimiter in a row
// so we need to keep track of them. Example: D14/12/'08
bool lastWasDelim = false;
// separate the parts of the date and keep the locations of the delimiters
for(pos = 0; pos < txt.length() && part < 3; ++pos) {
if(nonDelimChars.search(txt[pos]) == -1) {
if(!lastWasDelim) {
++part;
maxPartSize = 0; // make sure to pick the right one depending if next char is numeric or not
lastWasDelim = true;
}
} else {
lastWasDelim = false;
// check if the part is over and we did not see a delimiter
if((maxPartSize != 0) && (parts[part].length() == maxPartSize)) {
++part;
maxPartSize = 0;
}
if(maxPartSize == 0) {
maxPartSize = txt[pos].isDigit() ? 2 : 3;
if(part == 2)
maxPartSize = 4;
}
if(part < 3)
parts[part] += txt[pos];
}
}
if(part == 3) { // invalid date
for(int i = 0; i < 3; ++i) {
parts[i] = "0";
}
}
}
void MyMoneyQifProfile::Private::getThirdPosition(void)
{
// if we have detected two parts we can calculate the third and its position
if(m_partPos.count() == 2) {
TQValueList<TQChar> partsPresent = m_partPos.keys();
TQStringList partsAvail = TQStringList::split(",", "d,m,y");
int missingIndex = -1;
int value = 0;
for(int i = 0; i < 3; ++i) {
if(!partsPresent.contains(partsAvail[i][0])) {
missingIndex = i;
} else {
value += m_partPos[partsAvail[i][0]];
}
}
m_partPos[partsAvail[missingIndex][0]] = 3 - value;
}
}
MyMoneyQifProfile::MyMoneyQifProfile() :
d(new Private),
m_isDirty(false)
{
clear();
}
MyMoneyQifProfile::MyMoneyQifProfile(const TQString& name) :
d(new Private),
m_isDirty(false)
{
loadProfile(name);
}
MyMoneyQifProfile::~MyMoneyQifProfile()
{
delete d;
}
void MyMoneyQifProfile::clear(void)
{
m_dateFormat = "%d.%m.%yyyy";
m_apostropheFormat = "2000-2099";
m_valueMode = "";
m_filterScriptImport = "";
m_filterScriptExport = "";
m_filterFileType = "*.qif";
m_decimal.clear();
m_decimal['$'] =
m_decimal['Q'] =
m_decimal['T'] =
m_decimal['O'] =
m_decimal['I'] = KGlobal::locale()->monetaryDecimalSymbol()[0];
m_thousands.clear();
m_thousands['$'] =
m_thousands['Q'] =
m_thousands['T'] =
m_thousands['O'] =
m_thousands['I'] = KGlobal::locale()->monetaryThousandsSeparator()[0];
m_openingBalanceText = "Opening Balance";
m_voidMark = "VOID ";
m_accountDelimiter = "[";
m_profileName = "";
m_profileDescription = "";
m_profileType = "Bank";
m_attemptMatchDuplicates = true;
}
void MyMoneyQifProfile::loadProfile(const TQString& name)
{
KConfig* config = KGlobal::config();
config->setGroup(name);
clear();
m_profileName = name;
m_profileDescription = config->readEntry("Description", m_profileDescription);
m_profileType = config->readEntry("Type", m_profileType);
m_dateFormat = config->readEntry("DateFormat", m_dateFormat);
m_apostropheFormat = config->readEntry("ApostropheFormat", m_apostropheFormat);
m_accountDelimiter = config->readEntry("AccountDelimiter", m_accountDelimiter);
m_openingBalanceText = config->readEntry("OpeningBalance", m_openingBalanceText);
m_voidMark = config->readEntry("VoidMark", m_voidMark);
m_filterScriptImport = config->readEntry("FilterScriptImport", m_filterScriptImport);
m_filterScriptExport = config->readEntry("FilterScriptExport", m_filterScriptExport);
m_filterFileType = config->readEntry("FilterFileType",m_filterFileType);
m_attemptMatchDuplicates = config->readBoolEntry("AttemptMatchDuplicates", m_attemptMatchDuplicates);
// make sure, we remove any old stuff for now
config->deleteEntry("FilterScript");
TQString tmp = TQString(m_decimal['Q']) + m_decimal['T'] + m_decimal['I'] +
m_decimal['$'] + m_decimal['O'];
tmp = config->readEntry("Decimal", tmp);
m_decimal['Q'] = tmp[0];
m_decimal['T'] = tmp[1];
m_decimal['I'] = tmp[2];
m_decimal['$'] = tmp[3];
m_decimal['O'] = tmp[4];
tmp = TQString(m_thousands['Q']) + m_thousands['T'] + m_thousands['I'] +
m_thousands['$'] + m_thousands['O'];
tmp = config->readEntry("Thousand", tmp);
m_thousands['Q'] = tmp[0];
m_thousands['T'] = tmp[1];
m_thousands['I'] = tmp[2];
m_thousands['$'] = tmp[3];
m_thousands['O'] = tmp[4];
m_isDirty = false;
}
void MyMoneyQifProfile::saveProfile(void)
{
if(m_isDirty == true) {
KConfig* config = KGlobal::config();
config->setGroup(m_profileName);
config->writeEntry("Description", m_profileDescription);
config->writeEntry("Type", m_profileType);
config->writeEntry("DateFormat", m_dateFormat);
config->writeEntry("ApostropheFormat", m_apostropheFormat);
config->writeEntry("AccountDelimiter", m_accountDelimiter);
config->writeEntry("OpeningBalance", m_openingBalanceText);
config->writeEntry("VoidMark", m_voidMark);
config->writeEntry("FilterScriptImport", m_filterScriptImport);
config->writeEntry("FilterScriptExport", m_filterScriptExport);
config->writeEntry("FilterFileType", m_filterFileType);
config->writeEntry("AttemptMatchDuplicates", m_attemptMatchDuplicates);
TQString tmp;
tmp = TQString(m_decimal['Q']) + m_decimal['T'] + m_decimal['I'] +
m_decimal['$'] + m_decimal['O'];
config->writeEntry("Decimal", tmp);
tmp = TQString(m_thousands['Q']) + m_thousands['T'] + m_thousands['I'] +
m_thousands['$'] + m_thousands['O'];
config->writeEntry("Thousand", tmp);
}
m_isDirty = false;
}
void MyMoneyQifProfile::setProfileName(const TQString& name)
{
if(m_profileName != name)
m_isDirty = true;
m_profileName = name;
}
void MyMoneyQifProfile::setProfileDescription(const TQString& desc)
{
if(m_profileDescription != desc)
m_isDirty = true;
m_profileDescription = desc;
}
void MyMoneyQifProfile::setProfileType(const TQString& type)
{
if(m_profileType != type)
m_isDirty = true;
m_profileType = type;
}
void MyMoneyQifProfile::setOutputDateFormat(const TQString& dateFormat)
{
if(m_dateFormat != dateFormat)
m_isDirty = true;
m_dateFormat = dateFormat;
}
void MyMoneyQifProfile::setInputDateFormat(const TQString& dateFormat)
{
int j = -1;
if(dateFormat.length() > 0) {
for(unsigned int i = 0; i < dateFormat.length()-1; ++i) {
if(dateFormat[i] == '%') {
d->m_partPos[dateFormat[++i]] = ++j;
}
}
}
}
void MyMoneyQifProfile::setApostropheFormat(const TQString& apostropheFormat)
{
if(m_apostropheFormat != apostropheFormat)
m_isDirty = true;
m_apostropheFormat = apostropheFormat;
}
void MyMoneyQifProfile::setAmountDecimal(const TQChar& def, const TQChar& chr)
{
TQChar ch(chr);
if(ch == TQChar())
ch = ' ';
if(m_decimal[def] != ch)
m_isDirty = true;
m_decimal[def] = ch;
}
void MyMoneyQifProfile::setAmountThousands(const TQChar& def, const TQChar& chr)
{
TQChar ch(chr);
if(ch == TQChar())
ch = ' ';
if(m_thousands[def] != ch)
m_isDirty = true;
m_thousands[def] = ch;
}
TQChar MyMoneyQifProfile::amountDecimal(const TQChar& def) const
{
TQChar chr = m_decimal[def];
return chr;
}
TQChar MyMoneyQifProfile::amountThousands(const TQChar& def) const
{
TQChar chr = m_thousands[def];
return chr;
}
void MyMoneyQifProfile::setAccountDelimiter(const TQString& delim)
{
TQString txt(delim);
if(txt.isEmpty())
txt = " ";
else if(txt[0] != '[')
txt = "[";
if(m_accountDelimiter[0] != txt[0])
m_isDirty = true;
m_accountDelimiter = txt[0];
}
void MyMoneyQifProfile::setOpeningBalanceText(const TQString& txt)
{
if(m_openingBalanceText != txt)
m_isDirty = true;
m_openingBalanceText = txt;
}
void MyMoneyQifProfile::setVoidMark(const TQString& txt)
{
if(m_voidMark != txt)
m_isDirty = true;
m_voidMark = txt;
}
TQString MyMoneyQifProfile::accountDelimiter(void) const
{
TQString rc;
switch(m_accountDelimiter[0]) {
case ' ':
rc = " ";
break;
default:
rc = "[]";
break;
}
return rc;
}
TQString MyMoneyQifProfile::date(const TQDate& datein) const
{
const char* format = m_dateFormat.latin1();
TQString buffer;
TQChar delim;
int maskLen;
char maskChar;
while(*format) {
switch(*format) {
case '%':
maskLen = 0;
maskChar = *++format;
while(*format && *format == maskChar) {
++maskLen;
++format;
}
switch(maskChar) {
case 'd':
if(delim)
buffer += delim;
buffer += TQString::number(datein.day()).rightJustify(2, '0');
break;
case 'm':
if(delim)
buffer += delim;
if(maskLen == 3)
buffer += KGlobal::locale()->calendar()->monthName(datein.month(), datein.year(), true);
else
buffer += TQString::number(datein.month()).rightJustify(2, '0');
break;
case 'y':
if(maskLen == 2) {
buffer += twoDigitYear(delim, datein.year());
} else {
if(delim)
buffer += delim;
buffer += TQString::number(datein.year());
}
break;
default:
throw new MYMONEYEXCEPTION("Invalid char in QifProfile date field");
break;
}
delim = 0;
break;
default:
if(delim)
buffer += delim;
delim = *format++;
break;
}
}
return buffer;
}
const TQDate MyMoneyQifProfile::date(const TQString& datein) const
{
// in case we don't know the format, we return an invalid date
if(d->m_partPos.count() != 3)
return TQDate();
TQValueVector<TQString> scannedParts(3);
d->dissectDate(scannedParts, datein);
int yr, mon, day;
bool ok;
yr = scannedParts[d->m_partPos['y']].toInt();
mon = scannedParts[d->m_partPos['m']].toInt(&ok);
if(!ok) {
TQStringList monthNames = TQStringList::split(",", "jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec");
int j;
for(j = 1; j <= 12; ++j) {
if((KGlobal::locale()->calendar()->monthName(j, 2000, true).lower() == scannedParts[d->m_partPos['m']].lower())
|| (monthNames[j-1] == scannedParts[d->m_partPos['m']].lower())) {
mon = j;
break;
}
}
if(j == 13) {
tqWarning("Unknown month '%s'", scannedParts[d->m_partPos['m']].data());
return TQDate();
}
}
day = scannedParts[d->m_partPos['d']].toInt();
if(yr < 100) { // two digit year information?
if(yr < CENTURY_BREAK) // less than the CENTURY_BREAK we assume this century
yr += 2000;
else
yr += 1900;
}
return TQDate(yr, mon, day);
#if 0
TQString scannedDelim[2];
TQString formatParts[3];
TQString formatDelim[2];
int part;
int delim;
unsigned int i,j;
part = -1;
delim = 0;
for(i = 0; i < m_dateFormat.length(); ++i) {
if(m_dateFormat[i] == '%') {
++part;
if(part == 3) {
tqWarning("MyMoneyQifProfile::date(const TQString& datein) Too many parts in date format");
return TQDate();
}
++i;
}
switch(m_dateFormat[i].latin1()) {
case 'm':
case 'd':
case 'y':
formatParts[part] += m_dateFormat[i];
break;
case '/':
case '-':
case '.':
case '\'':
if(delim == 2) {
tqWarning("MyMoneyQifProfile::date(const TQString& datein) Too many delimiters in date format");
return TQDate();
}
formatDelim[delim] = m_dateFormat[i];
++delim;
break;
default:
tqWarning("MyMoneyQifProfile::date(const TQString& datein) Invalid char in date format");
return TQDate();
}
}
part = 0;
delim = 0;
bool prevWasChar = false;
for(i = 0; i < datein.length(); ++i) {
switch(datein[i].latin1()) {
case '/':
case '.':
case '-':
case '\'':
if(delim == 2) {
tqWarning("MyMoneyQifProfile::date(const TQString& datein) Too many delimiters in date field");
return TQDate();
}
scannedDelim[delim] = datein[i];
++delim;
++part;
prevWasChar = false;
break;
default:
if(prevWasChar && datein[i].isDigit()) {
++part;
prevWasChar = false;
}
if(datein[i].isLetter())
prevWasChar = true;
// replace blank with 0
scannedParts[part] += (datein[i] == ' ') ? TQChar('0') : datein[i];
break;
}
}
int day = 1,
mon = 1,
yr = 1900;
bool ok = false;
for(i = 0; i < 2; ++i) {
if(scannedDelim[i] != formatDelim[i]
&& scannedDelim[i] != TQChar('\'')) {
tqWarning("MyMoneyQifProfile::date(const TQString& datein) Invalid delimiter '%s' when '%s' was expected",
scannedDelim[i].latin1(), formatDelim[i].latin1());
return TQDate();
}
}
TQString msg;
for(i = 0; i < 3; ++i) {
switch(formatParts[i][0].latin1()) {
case 'd':
day = scannedParts[i].toUInt(&ok);
if (!ok)
msg = "Invalid numeric character in day string";
break;
case 'm':
if(formatParts[i].length() != 3) {
mon = scannedParts[i].toUInt(&ok);
if (!ok)
msg = "Invalid numeric character in month string";
} else {
for(j = 1; j <= 12; ++j) {
if(KGlobal::locale()->calendar()->monthName(j, 2000, true).lower() == formatParts[i].lower()) {
mon = j;
ok = true;
break;
}
}
if(j == 13) {
msg = "Unknown month '" + scannedParts[i] + "'";
}
}
break;
case 'y':
ok = false;
if(scannedParts[i].length() == formatParts[i].length()) {
yr = scannedParts[i].toUInt(&ok);
if (!ok)
msg = "Invalid numeric character in month string";
if(yr < 100) { // two digit year info
if(i > 1) {
ok = true;
if(scannedDelim[i-1] == TQChar('\'')) {
if(m_apostropheFormat == "1900-1949") {
if(yr < 50)
yr += 1900;
else
yr += 2000;
} else if(m_apostropheFormat == "1900-1999") {
yr += 1900;
} else if(m_apostropheFormat == "2000-2099") {
yr += 2000;
} else {
msg = "Unsupported apostropheFormat!";
ok = false;
}
} else {
if(m_apostropheFormat == "1900-1949") {
if(yr < 50)
yr += 2000;
else
yr += 1900;
} else if(m_apostropheFormat == "1900-1999") {
yr += 2000;
} else if(m_apostropheFormat == "2000-2099") {
yr += 1900;
} else {
msg = "Unsupported apostropheFormat!";
ok = false;
}
}
} else {
msg = "Year as first parameter is not supported!";
}
} else if(yr < 1900) {
msg = "Year not in range < 100 or >= 1900!";
} else {
ok = true;
}
} else {
msg = TQString("Length of year (%1) does not match expected length (%2).")
.arg(scannedParts[i].length()).arg(formatParts[i].length());
}
break;
}
if(!msg.isEmpty()) {
tqWarning("MyMoneyQifProfile::date(const TQString& datein) %s",msg.latin1());
return TQDate();
}
}
return TQDate(yr, mon, day);
#endif
}
TQString MyMoneyQifProfile::twoDigitYear(const TQChar delim, int yr) const
{
TQChar realDelim = delim;
TQString buffer;
if(delim) {
if((m_apostropheFormat == "1900-1949" && yr <= 1949)
|| (m_apostropheFormat == "1900-1999" && yr <= 1999)
|| (m_apostropheFormat == "2000-2099" && yr >= 2000))
realDelim = '\'';
buffer += realDelim;
}
yr -= 1900;
if(yr > 100)
yr -= 100;
if(yr < 10)
buffer += "0";
buffer += TQString::number(yr);
return buffer;
}
TQString MyMoneyQifProfile::value(const TQChar& def, const MyMoneyMoney& valuein) const
{
unsigned char _decimalSeparator;
unsigned char _thousandsSeparator;
TQString res;
_decimalSeparator = MyMoneyMoney::decimalSeparator();
_thousandsSeparator = MyMoneyMoney::thousandSeparator();
MyMoneyMoney::signPosition _signPosition = MyMoneyMoney::negativeMonetarySignPosition();
MyMoneyMoney::setDecimalSeparator(amountDecimal(def));
MyMoneyMoney::setThousandSeparator(amountThousands(def));
MyMoneyMoney::setNegativeMonetarySignPosition(MyMoneyMoney::BeforeQuantityMoney);
res = valuein.formatMoney("", 2);
MyMoneyMoney::setDecimalSeparator(_decimalSeparator);
MyMoneyMoney::setThousandSeparator(_thousandsSeparator);
MyMoneyMoney::setNegativeMonetarySignPosition(_signPosition);
return res;
}
MyMoneyMoney MyMoneyQifProfile::value(const TQChar& def, const TQString& valuein) const
{
unsigned char _decimalSeparator;
unsigned char _thousandsSeparator;
MyMoneyMoney res;
_decimalSeparator = MyMoneyMoney::decimalSeparator();
_thousandsSeparator = MyMoneyMoney::thousandSeparator();
MyMoneyMoney::signPosition _signPosition = MyMoneyMoney::negativeMonetarySignPosition();
MyMoneyMoney::setDecimalSeparator(amountDecimal(def));
MyMoneyMoney::setThousandSeparator(amountThousands(def));
MyMoneyMoney::setNegativeMonetarySignPosition(MyMoneyMoney::BeforeQuantityMoney);
res = MyMoneyMoney(valuein);
MyMoneyMoney::setDecimalSeparator(_decimalSeparator);
MyMoneyMoney::setThousandSeparator(_thousandsSeparator);
MyMoneyMoney::setNegativeMonetarySignPosition(_signPosition);
return res;
}
void MyMoneyQifProfile::setFilterScriptImport(const TQString& script)
{
if(m_filterScriptImport != script)
m_isDirty = true;
m_filterScriptImport = script;
}
void MyMoneyQifProfile::setFilterScriptExport(const TQString& script)
{
if(m_filterScriptExport != script)
m_isDirty = true;
m_filterScriptExport = script;
}
void MyMoneyQifProfile::setFilterFileType(const TQString& txt)
{
if(m_filterFileType != txt)
m_isDirty = true;
m_filterFileType = txt;
}
void MyMoneyQifProfile::setAttemptMatchDuplicates(bool f)
{
if ( m_attemptMatchDuplicates != f )
m_isDirty = true;
m_attemptMatchDuplicates = f;
}
TQString MyMoneyQifProfile::inputDateFormat(void) const
{
TQStringList list;
possibleDateFormats(list);
if(list.count() == 1)
return list.first();
return TQString::null;
}
void MyMoneyQifProfile::possibleDateFormats(TQStringList& list) const
{
TQStringList defaultList = TQStringList::split(":", "y,m,d:y,d,m:m,d,y:m,y,d:d,m,y:d,y,m");
list.clear();
TQStringList::const_iterator it_d;
for(it_d = defaultList.begin(); it_d != defaultList.end(); ++it_d) {
TQStringList parts = TQStringList::split(",", *it_d);
int i;
for(i = 0; i < 3; ++i) {
if(d->m_partPos.contains(parts[i][0])) {
if(d->m_partPos[parts[i][0]] != i)
break;
}
// months can't be larger than 12
if(parts[i] == "m" && d->m_largestValue[i] > 12)
break;
// days can't be larger than 31
if(parts[i] == "d" && d->m_largestValue[i] > 31)
break;
}
// matches all tests
if(i == 3) {
TQString format = *it_d;
format.replace('y', "%y");
format.replace('m', "%m");
format.replace('d', "%d");
format.replace(',', " ");
list << format;
}
}
// if we haven't found any, then there's something wrong.
// in this case, we present the full list and let the user decide
if(list.count() == 0) {
for(it_d = defaultList.begin(); it_d != defaultList.end(); ++it_d) {
TQString format = *it_d;
format.replace('y', "%y");
format.replace('m', "%m");
format.replace('d', "%d");
format.replace(',', " ");
list << format;
}
}
}
void MyMoneyQifProfile::autoDetect(const TQStringList& lines)
{
m_dateFormat = TQString();
m_decimal.clear();
m_thousands.clear();
TQString numericRecords = "BT$OIQ";
TQStringList::const_iterator it;
int datesScanned = 0;
// section: used to switch between different QIF sections,
// because the Record identifiers are ambigous between sections
// eg. in transaction records, T identifies a total amount, in
// account sections it's the type.
//
// 0 - unknown
// 1 - account
// 2 - transactions
// 3 - prices
int section = 0;
TQRegExp price("\"(.*)\",(.*),\"(.*)\"");
for(it = lines.begin(); it != lines.end(); ++it) {
TQChar c((*it)[0]);
if(c == '!') {
TQString sname = (*it).lower();
section = 0;
if(sname.startsWith("!account"))
section = 1;
else if(sname.startsWith("!type")) {
if(sname.startsWith("!type:cat")
|| sname.startsWith("!type:payee")
|| sname.startsWith("!type:security")
|| sname.startsWith("!type:class")) {
section = 0;
} else if(sname.startsWith("!type:price")) {
section = 3;
} else
section = 2;
}
}
switch(section) {
case 1:
if(c == 'B') {
scanNumeric((*it).mid(1), m_decimal[c], m_thousands[c]);
}
break;
case 2:
if(numericRecords.contains(c)) {
scanNumeric((*it).mid(1), m_decimal[c], m_thousands[c]);
} else if((c == 'D') && (m_dateFormat.isEmpty())) {
if(d->m_partPos.count() != 3) {
scanDate((*it).mid(1));
++datesScanned;
if(d->m_partPos.count() == 2) {
// if we have detected two parts we can calculate the third and its position
d->getThirdPosition();
}
}
}
break;
case 3:
if(price.search(*it) != -1) {
scanNumeric(price.cap(2), m_decimal['P'], m_thousands['P']);
scanDate(price.cap(3));
++datesScanned;
}
break;
}
}
// the following algorithm is only applied if we have more
// than 20 dates found. Smaller numbers have shown that the
// results are inaccurate which leads to a reduced number of
// date formats presented to choose from.
if(d->m_partPos.count() != 3 && datesScanned > 20) {
TQMap<int, int> sortedPos;
// make sure to reset the known parts for the following algorithm
if(d->m_partPos.contains('y')) {
d->m_changeCount[d->m_partPos['y']] = -1;
for(int i = 0; i < 3; ++i) {
if(d->m_partPos['y'] == i)
continue;
// can we say for sure that we hit the day field?
if(d->m_largestValue[i] > 12) {
d->m_partPos['d'] = i;
}
}
}
if(d->m_partPos.contains('d'))
d->m_changeCount[d->m_partPos['d']] = -1;
if(d->m_partPos.contains('m'))
d->m_changeCount[d->m_partPos['m']] = -1;
for(int i = 0; i < 3; ++i) {
if(d->m_changeCount[i] != -1) {
sortedPos[d->m_changeCount[i]] = i;
}
}
TQMap<int, int>::const_iterator it_a;
TQMap<int, int>::const_iterator it_b;
switch(sortedPos.count()) {
case 1: // all the same
// let the user decide, we can't figure it out
break;
case 2: // two are the same, we treat the largest as the day
// if it's 20% larger than the other one and let the
// user pick the other two
{
it_b = sortedPos.begin();
it_a = it_b;
++it_b;
double a = d->m_changeCount[*it_a];
double b = d->m_changeCount[*it_b];
if(b > (a * 1.2)) {
d->m_partPos['d'] = *it_b;
}
}
break;
case 3: // three different, we check if they are 20% apart each
it_b = sortedPos.begin();
for(int i = 0; i < 2; ++i) {
it_a = it_b;
++it_b;
double a = d->m_changeCount[*it_a];
double b = d->m_changeCount[*it_b];
if(b > (a * 1.2)) {
switch(i) {
case 0:
d->m_partPos['y'] = *it_a;
break;
case 1:
d->m_partPos['d'] = *it_b;
break;
}
}
}
break;
}
// extract the last if necessary and possible date position
d->getThirdPosition();
}
}
void MyMoneyQifProfile::scanNumeric(const TQString& txt, TQChar& decimal, TQChar& thousands) const
{
TQChar first, second;
TQRegExp numericChars("[0-9-()]");
for(unsigned int i = 0; i < txt.length(); ++i) {
if(numericChars.search(txt[i]) == -1) {
first = second;
second = txt[i];
}
}
if(!second.isNull())
decimal = second;
if(!first.isNull())
thousands = first;
}
void MyMoneyQifProfile::scanDate(const TQString& txt) const
{
// extract the parts from the txt
TQValueVector<TQString> parts(3); // the various parts of the date
d->dissectDate(parts, txt);
// now analyse the parts
for(int i = 0; i < 3; ++i) {
bool ok;
int value = parts[i].toInt(&ok);
if(!ok) { // this should happen only if the part is non-numeric -> month
d->m_partPos['m'] = i;
} else if(value != 0) {
if(value != d->m_lastValue[i]) {
d->m_changeCount[i]++;
d->m_lastValue[i] = value;
if(value > d->m_largestValue[i])
d->m_largestValue[i] = value;
}
// if it's > 31 it can only be years
if(value > 31) {
d->m_partPos['y'] = i;
}
// and if it's in between 12 and 32 and we already identified the
// position for the year it must be days
if((value > 12) && (value < 32) && d->m_partPos.contains('y')) {
d->m_partPos['d'] = i;
}
}
}
}
#include "mymoneyqifprofile.moc"
|