Diffstat (limited to 'microkde/kdecore/klocale.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | microkde/kdecore/klocale.cpp | 23 |
1 files changed, 12 insertions, 11 deletions
diff --git a/microkde/kdecore/klocale.cpp b/microkde/kdecore/klocale.cpp index 1d8ae9f..dd310fa 100644 --- a/microkde/kdecore/klocale.cpp +++ b/microkde/kdecore/klocale.cpp | |||
@@ -1,172 +1,173 @@ | |||
1 | #include <qregexp.h> | 1 | #include <qregexp.h> |
2 | #include <qapplication.h> | 2 | #include <qapplication.h> |
3 | #include <QDesktopWidget> | ||
3 | 4 | ||
4 | #include "kdebug.h" | 5 | #include "kdebug.h" |
5 | #include "kcalendarsystemgregorian.h" | 6 | #include "kcalendarsystemgregorian.h" |
6 | 7 | ||
7 | #include "klocale.h" | 8 | #include "klocale.h" |
8 | 9 | ||
9 | #include <qstringlist.h> | 10 | #include <qstringlist.h> |
10 | 11 | ||
11 | //#define COLLECT_TRANSLATION | 12 | //#define COLLECT_TRANSLATION |
12 | 13 | ||
13 | 14 | ||
14 | QDict<QString> *mLocaleDict = 0; | 15 | Q3Dict<QString> *mLocaleDict = 0; |
15 | void setLocaleDict( QDict<QString> * dict ) | 16 | void setLocaleDict( Q3Dict<QString> * dict ) |
16 | { | 17 | { |
17 | mLocaleDict = dict; | 18 | mLocaleDict = dict; |
18 | 19 | ||
19 | } | 20 | } |
20 | 21 | ||
21 | #ifdef COLLECT_TRANSLATION | 22 | #ifdef COLLECT_TRANSLATION |
22 | 23 | ||
23 | QStringList missingTrans; | 24 | QStringList missingTrans; |
24 | QStringList existingTrans1; | 25 | QStringList existingTrans1; |
25 | QStringList existingTrans2; | 26 | QStringList existingTrans2; |
26 | 27 | ||
27 | void addMissing(const char *text) | 28 | void addMissing(const char *text) |
28 | { | 29 | { |
29 | 30 | ||
30 | QString mis ( text ); | 31 | QString mis ( text ); |
31 | if ( !missingTrans.contains( mis ) ) | 32 | if ( !missingTrans.contains( mis ) ) |
32 | missingTrans.append(mis); | 33 | missingTrans.append(mis); |
33 | 34 | ||
34 | } | 35 | } |
35 | void addExist(const char *text,QString trans ) | 36 | void addExist(const char *text,QString trans ) |
36 | { | 37 | { |
37 | //return; | 38 | //return; |
38 | QString mis ( text ); | 39 | QString mis ( text ); |
39 | if ( !existingTrans1.contains( mis ) ) { | 40 | if ( !existingTrans1.contains( mis ) ) { |
40 | existingTrans1.append(mis); | 41 | existingTrans1.append(mis); |
41 | existingTrans2.append(trans); | 42 | existingTrans2.append(trans); |
42 | 43 | ||
43 | } | 44 | } |
44 | 45 | ||
45 | } | 46 | } |
46 | 47 | ||
47 | #include <qfile.h> | 48 | #include <qfile.h> |
48 | #include <qtextstream.h> | 49 | #include <q3textstream.h> |
49 | #include <qtextcodec.h> | 50 | #include <qtextcodec.h> |
50 | #endif | 51 | #endif |
51 | void dumpMissing() | 52 | void dumpMissing() |
52 | { | 53 | { |
53 | #ifdef COLLECT_TRANSLATION | 54 | #ifdef COLLECT_TRANSLATION |
54 | QString fileName = "/tmp/usernewtrans.txt"; | 55 | QString fileName = "/tmp/usernewtrans.txt"; |
55 | QFile file( fileName ); | 56 | QFile file( fileName ); |
56 | if (!file.open( IO_WriteOnly ) ) { | 57 | if (!file.open( QIODevice::WriteOnly ) ) { |
57 | return ; | 58 | return ; |
58 | } | 59 | } |
59 | QTextStream ts( &file ); | 60 | Q3TextStream ts( &file ); |
60 | ts.setCodec( QTextCodec::codecForName("utf8") ); | 61 | ts.setCodec( QTextCodec::codecForName("utf8") ); |
61 | 62 | ||
62 | int i; | 63 | int i; |
63 | for ( i = 0; i< missingTrans.count(); ++i ) { | 64 | for ( i = 0; i< missingTrans.count(); ++i ) { |
64 | 65 | ||
65 | QString text = missingTrans[i].replace( QRegExp("\n"),"\\n" ); | 66 | QString text = missingTrans[i].replace( QRegExp("\n"),"\\n" ); |
66 | ts << "{ \""<<text<< "\",\""<< text <<"\" },\n"; | 67 | ts << "{ \""<<text<< "\",\""<< text <<"\" },\n"; |
67 | 68 | ||
68 | } | 69 | } |
69 | file.close(); | 70 | file.close(); |
70 | { | 71 | { |
71 | QString fileName = "/tmp/usertrans.txt"; | 72 | QString fileName = "/tmp/usertrans.txt"; |
72 | QFile file( fileName ); | 73 | QFile file( fileName ); |
73 | if (!file.open( IO_WriteOnly ) ) { | 74 | if (!file.open( QIODevice::WriteOnly ) ) { |
74 | return ; | 75 | return ; |
75 | } | 76 | } |
76 | QTextStream ts( &file ); | 77 | Q3TextStream ts( &file ); |
77 | ts.setCodec( QTextCodec::codecForName("utf8") ); | 78 | ts.setCodec( QTextCodec::codecForName("utf8") ); |
78 | 79 | ||
79 | int i; | 80 | int i; |
80 | for ( i = 0; i< existingTrans1.count(); ++i ) { | 81 | for ( i = 0; i< existingTrans1.count(); ++i ) { |
81 | 82 | ||
82 | QString text = existingTrans1[i].replace( QRegExp("\n"),"\\n" ); | 83 | QString text = existingTrans1[i].replace( QRegExp("\n"),"\\n" ); |
83 | QString text2 = existingTrans2[i].replace( QRegExp("\n"),"\\n" ); | 84 | QString text2 = existingTrans2[i].replace( QRegExp("\n"),"\\n" ); |
84 | ts << "{ \""<<text<< "\",\""<< text2 <<"\" },\n"; | 85 | ts << "{ \""<<text<< "\",\""<< text2 <<"\" },\n"; |
85 | 86 | ||
86 | } | 87 | } |
87 | file.close(); | 88 | file.close(); |
88 | } | 89 | } |
89 | #endif | 90 | #endif |
90 | } | 91 | } |
91 | QString i18n(const char *text) | 92 | QString i18n(const char *text) |
92 | { | 93 | { |
93 | if ( ! mLocaleDict ) { | 94 | if ( ! mLocaleDict ) { |
94 | #ifdef COLLECT_TRANSLATION | 95 | #ifdef COLLECT_TRANSLATION |
95 | addMissing( text ); | 96 | addMissing( text ); |
96 | #endif | 97 | #endif |
97 | return QString( text ); | 98 | return QString( text ); |
98 | } | 99 | } |
99 | else { | 100 | else { |
100 | QString* ret = mLocaleDict->find(QString(text)) ; | 101 | QString* ret = mLocaleDict->find(QString(text)) ; |
101 | if ( ret == 0 ) { | 102 | if ( ret == 0 ) { |
102 | #ifdef COLLECT_TRANSLATION | 103 | #ifdef COLLECT_TRANSLATION |
103 | addMissing( text ); | 104 | addMissing( text ); |
104 | #endif | 105 | #endif |
105 | return QString( text ); | 106 | return QString( text ); |
106 | } | 107 | } |
107 | else { | 108 | else { |
108 | if ( (*ret).isEmpty() ) { | 109 | if ( (*ret).isEmpty() ) { |
109 | #ifdef COLLECT_TRANSLATION | 110 | #ifdef COLLECT_TRANSLATION |
110 | addMissing( text ); | 111 | addMissing( text ); |
111 | #endif | 112 | #endif |
112 | return QString( text ); | 113 | return QString( text ); |
113 | } | 114 | } |
114 | else { | 115 | else { |
115 | #ifdef COLLECT_TRANSLATION | 116 | #ifdef COLLECT_TRANSLATION |
116 | addExist( text, *ret ); | 117 | addExist( text, *ret ); |
117 | #endif | 118 | #endif |
118 | return (*ret); | 119 | return (*ret); |
119 | } | 120 | } |
120 | } | 121 | } |
121 | } | 122 | } |
122 | 123 | ||
123 | } | 124 | } |
124 | 125 | ||
125 | QString i18n(const char *,const char *text) | 126 | QString i18n(const char *,const char *text) |
126 | { | 127 | { |
127 | return i18n( text ); | 128 | return i18n( text ); |
128 | } | 129 | } |
129 | 130 | ||
130 | QString i18n(const char *text1, const char *textn, int num) | 131 | QString i18n(const char *text1, const char *textn, int num) |
131 | { | 132 | { |
132 | if ( num == 1 ) return i18n( text1 ); | 133 | if ( num == 1 ) return i18n( text1 ); |
133 | else { | 134 | else { |
134 | QString text = i18n( textn ); | 135 | QString text = i18n( textn ); |
135 | int pos = text.find( "%n" ); | 136 | int pos = text.find( "%n" ); |
136 | if ( pos >= 0 ) text.replace( pos, 2, QString::number( num ) ); | 137 | if ( pos >= 0 ) text.replace( pos, 2, QString::number( num ) ); |
137 | return text; | 138 | return text; |
138 | } | 139 | } |
139 | } | 140 | } |
140 | 141 | ||
141 | inline void put_it_in( QChar *buffer, uint& index, const QString &s ) | 142 | inline void put_it_in( QChar *buffer, uint& index, const QString &s ) |
142 | { | 143 | { |
143 | for ( uint l = 0; l < s.length(); l++ ) | 144 | for ( uint l = 0; l < s.length(); l++ ) |
144 | buffer[index++] = s.at( l ); | 145 | buffer[index++] = s.at( l ); |
145 | } | 146 | } |
146 | 147 | ||
147 | inline void put_it_in( QChar *buffer, uint& index, int number ) | 148 | inline void put_it_in( QChar *buffer, uint& index, int number ) |
148 | { | 149 | { |
149 | buffer[index++] = number / 10 + '0'; | 150 | buffer[index++] = number / 10 + '0'; |
150 | buffer[index++] = number % 10 + '0'; | 151 | buffer[index++] = number % 10 + '0'; |
151 | } | 152 | } |
152 | 153 | ||
153 | static int readInt(const QString &str, uint &pos) | 154 | static int readInt(const QString &str, uint &pos) |
154 | { | 155 | { |
155 | if (!str.at(pos).isDigit()) return -1; | 156 | if (!str.at(pos).isDigit()) return -1; |
156 | int result = 0; | 157 | int result = 0; |
157 | for (; str.length() > pos && str.at(pos).isDigit(); pos++) | 158 | for (; str.length() > pos && str.at(pos).isDigit(); pos++) |
158 | { | 159 | { |
159 | result *= 10; | 160 | result *= 10; |
160 | result += str.at(pos).digitValue(); | 161 | result += str.at(pos).digitValue(); |
161 | } | 162 | } |
162 | 163 | ||
163 | return result; | 164 | return result; |
164 | } | 165 | } |
165 | 166 | ||
166 | KLocale::KLocale() : mCalendarSystem( 0 ) | 167 | KLocale::KLocale() : mCalendarSystem( 0 ) |
167 | { | 168 | { |
168 | 169 | ||
169 | m_decimalSymbol = "."; | 170 | m_decimalSymbol = "."; |
170 | m_positiveSign = ""; | 171 | m_positiveSign = ""; |
171 | m_negativeSign = "-"; | 172 | m_negativeSign = "-"; |
172 | m_thousandsSeparator = ","; | 173 | m_thousandsSeparator = ","; |
@@ -374,460 +375,460 @@ QString KLocale::formatTime(const QTime &pTime, bool includeSecs, IntDateFormat | |||
374 | } | 375 | } |
375 | default: | 376 | default: |
376 | buffer[index++] = rst.at( format_index ); | 377 | buffer[index++] = rst.at( format_index ); |
377 | break; | 378 | break; |
378 | } | 379 | } |
379 | escape = false; | 380 | escape = false; |
380 | } | 381 | } |
381 | } | 382 | } |
382 | QString ret( buffer, index ); | 383 | QString ret( buffer, index ); |
383 | delete [] buffer; | 384 | delete [] buffer; |
384 | return ret; | 385 | return ret; |
385 | } | 386 | } |
386 | 387 | ||
387 | QString KLocale::formatDate(const QDate &pDate, bool shortFormat, IntDateFormat intIntDateFormat) const | 388 | QString KLocale::formatDate(const QDate &pDate, bool shortFormat, IntDateFormat intIntDateFormat) const |
388 | { | 389 | { |
389 | const QString rst = shortFormat?dateFormatShort(intIntDateFormat):dateFormat(intIntDateFormat); | 390 | const QString rst = shortFormat?dateFormatShort(intIntDateFormat):dateFormat(intIntDateFormat); |
390 | 391 | ||
391 | // I'm rather safe than sorry | 392 | // I'm rather safe than sorry |
392 | QChar *buffer = new QChar[rst.length() * 3 / 2 + 50]; | 393 | QChar *buffer = new QChar[rst.length() * 3 / 2 + 50]; |
393 | 394 | ||
394 | unsigned int index = 0; | 395 | unsigned int index = 0; |
395 | bool escape = false; | 396 | bool escape = false; |
396 | int number = 0; | 397 | int number = 0; |
397 | 398 | ||
398 | for ( uint format_index = 0; format_index < rst.length(); ++format_index ) | 399 | for ( uint format_index = 0; format_index < rst.length(); ++format_index ) |
399 | { | 400 | { |
400 | if ( !escape ) | 401 | if ( !escape ) |
401 | { | 402 | { |
402 | if ( rst.at( format_index ).unicode() == '%' ) | 403 | if ( rst.at( format_index ).unicode() == '%' ) |
403 | escape = true; | 404 | escape = true; |
404 | else | 405 | else |
405 | buffer[index++] = rst.at( format_index ); | 406 | buffer[index++] = rst.at( format_index ); |
406 | } | 407 | } |
407 | else | 408 | else |
408 | { | 409 | { |
409 | switch ( rst.at( format_index ).unicode() ) | 410 | switch ( rst.at( format_index ).unicode() ) |
410 | { | 411 | { |
411 | case '%': | 412 | case '%': |
412 | buffer[index++] = '%'; | 413 | buffer[index++] = '%'; |
413 | break; | 414 | break; |
414 | case 'Y': | 415 | case 'Y': |
415 | put_it_in( buffer, index, pDate.year() / 100 ); | 416 | put_it_in( buffer, index, pDate.year() / 100 ); |
416 | case 'y': | 417 | case 'y': |
417 | put_it_in( buffer, index, pDate.year() % 100 ); | 418 | put_it_in( buffer, index, pDate.year() % 100 ); |
418 | break; | 419 | break; |
419 | case 'n': | 420 | case 'n': |
420 | number = pDate.month(); | 421 | number = pDate.month(); |
421 | case 'e': | 422 | case 'e': |
422 | // to share the code | 423 | // to share the code |
423 | if ( rst.at( format_index ).unicode() == 'e' ) | 424 | if ( rst.at( format_index ).unicode() == 'e' ) |
424 | number = pDate.day(); | 425 | number = pDate.day(); |
425 | if ( number / 10 ) | 426 | if ( number / 10 ) |
426 | buffer[index++] = number / 10 + '0'; | 427 | buffer[index++] = number / 10 + '0'; |
427 | buffer[index++] = number % 10 + '0'; | 428 | buffer[index++] = number % 10 + '0'; |
428 | break; | 429 | break; |
429 | case 'm': | 430 | case 'm': |
430 | put_it_in( buffer, index, pDate.month() ); | 431 | put_it_in( buffer, index, pDate.month() ); |
431 | break; | 432 | break; |
432 | case 'b': | 433 | case 'b': |
433 | put_it_in( buffer, index, monthName(pDate.month(), true) ); | 434 | put_it_in( buffer, index, monthName(pDate.month(), true) ); |
434 | break; | 435 | break; |
435 | case 'B': | 436 | case 'B': |
436 | put_it_in( buffer, index, monthName(pDate.month(), false) ); | 437 | put_it_in( buffer, index, monthName(pDate.month(), false) ); |
437 | break; | 438 | break; |
438 | case 'd': | 439 | case 'd': |
439 | put_it_in( buffer, index, pDate.day() ); | 440 | put_it_in( buffer, index, pDate.day() ); |
440 | break; | 441 | break; |
441 | case 'a': | 442 | case 'a': |
442 | put_it_in( buffer, index, weekDayName(pDate.dayOfWeek(), true) ); | 443 | put_it_in( buffer, index, weekDayName(pDate.dayOfWeek(), true) ); |
443 | break; | 444 | break; |
444 | case 'A': | 445 | case 'A': |
445 | put_it_in( buffer, index, weekDayName(pDate.dayOfWeek(), false) ); | 446 | put_it_in( buffer, index, weekDayName(pDate.dayOfWeek(), false) ); |
446 | break; | 447 | break; |
447 | default: | 448 | default: |
448 | buffer[index++] = rst.at( format_index ); | 449 | buffer[index++] = rst.at( format_index ); |
449 | break; | 450 | break; |
450 | } | 451 | } |
451 | escape = false; | 452 | escape = false; |
452 | } | 453 | } |
453 | } | 454 | } |
454 | QString ret( buffer, index ); | 455 | QString ret( buffer, index ); |
455 | delete [] buffer; | 456 | delete [] buffer; |
456 | return ret; | 457 | return ret; |
457 | } | 458 | } |
458 | 459 | ||
459 | QString KLocale::formatDateTime(const QDateTime &pDateTime, | 460 | QString KLocale::formatDateTime(const QDateTime &pDateTime, |
460 | bool shortFormat, | 461 | bool shortFormat, |
461 | bool includeSeconds, | 462 | bool includeSeconds, |
462 | IntDateFormat intIntDateFormat) const | 463 | IntDateFormat intIntDateFormat) const |
463 | { | 464 | { |
464 | QString format("%1 %2"); | 465 | QString format("%1 %2"); |
465 | 466 | ||
466 | if ( intIntDateFormat == Default ) | 467 | if ( intIntDateFormat == Default ) |
467 | format = "%1 %2"; | 468 | format = "%1 %2"; |
468 | else if ( intIntDateFormat == Format1 ) | 469 | else if ( intIntDateFormat == Format1 ) |
469 | format = "%1 %2"; | 470 | format = "%1 %2"; |
470 | else if ( intIntDateFormat == ISODate ) | 471 | else if ( intIntDateFormat == Qt::ISODate ) |
471 | format = "%1T%2"; | 472 | format = "%1T%2"; |
472 | 473 | ||
473 | QString res = format.arg(formatDate( pDateTime.date(), shortFormat, intIntDateFormat )) | 474 | QString res = format.arg(formatDate( pDateTime.date(), shortFormat, intIntDateFormat )) |
474 | .arg(formatTime( pDateTime.time(), includeSeconds , intIntDateFormat )); | 475 | .arg(formatTime( pDateTime.time(), includeSeconds , intIntDateFormat )); |
475 | 476 | ||
476 | //qDebug("KLocale::formatDateTime transformed %s, into %s", pDateTime.toString().latin1(), res.latin1() ); | 477 | //qDebug("KLocale::formatDateTime transformed %s, into %s", pDateTime.toString().latin1(), res.latin1() ); |
477 | 478 | ||
478 | return res; | 479 | return res; |
479 | } | 480 | } |
480 | 481 | ||
481 | QString KLocale::formatDateTime(const QDateTime &pDateTime, IntDateFormat intIntDateFormat) const | 482 | QString KLocale::formatDateTime(const QDateTime &pDateTime, IntDateFormat intIntDateFormat) const |
482 | { | 483 | { |
483 | return formatDateTime(pDateTime, true, true, intIntDateFormat); | 484 | return formatDateTime(pDateTime, true, true, intIntDateFormat); |
484 | } | 485 | } |
485 | 486 | ||
486 | QDate KLocale::readDate(const QString &intstr, bool* ok) const | 487 | QDate KLocale::readDate(const QString &intstr, bool* ok) const |
487 | { | 488 | { |
488 | QDate date; | 489 | QDate date; |
489 | date = readDate(intstr, true, ok); | 490 | date = readDate(intstr, true, ok); |
490 | if (date.isValid()) return date; | 491 | if (date.isValid()) return date; |
491 | return readDate(intstr, false, ok); | 492 | return readDate(intstr, false, ok); |
492 | } | 493 | } |
493 | 494 | ||
494 | QDate KLocale::readDate(const QString &intstr, bool shortFormat, bool* ok) const | 495 | QDate KLocale::readDate(const QString &intstr, bool shortFormat, bool* ok) const |
495 | { | 496 | { |
496 | QString fmt = (shortFormat ? dateFormatShort() : dateFormat()).simplifyWhiteSpace(); | 497 | QString fmt = (shortFormat ? dateFormatShort() : dateFormat()).simplifyWhiteSpace(); |
497 | return readDate( intstr, fmt, ok ); | 498 | return readDate( intstr, fmt, ok ); |
498 | } | 499 | } |
499 | 500 | ||
500 | QDate KLocale::readDate(const QString &intstr, const QString &fmt, bool* ok) const | 501 | QDate KLocale::readDate(const QString &intstr, const QString &fmt, bool* ok) const |
501 | { | 502 | { |
502 | //kdDebug(173) << "KLocale::readDate intstr=" << intstr << " fmt=" << fmt << endl; | 503 | //kdDebug(173) << "KLocale::readDate intstr=" << intstr << " fmt=" << fmt << endl; |
503 | QString str = intstr.simplifyWhiteSpace().lower(); | 504 | QString str = intstr.simplifyWhiteSpace().lower(); |
504 | int day = -1, month = -1; | 505 | int day = -1, month = -1; |
505 | // allow the year to be omitted if not in the format | 506 | // allow the year to be omitted if not in the format |
506 | int year = QDate::currentDate().year(); | 507 | int year = QDate::currentDate().year(); |
507 | uint strpos = 0; | 508 | uint strpos = 0; |
508 | uint fmtpos = 0; | 509 | uint fmtpos = 0; |
509 | 510 | ||
510 | while (fmt.length() > fmtpos || str.length() > strpos) | 511 | while (fmt.length() > fmtpos || str.length() > strpos) |
511 | { | 512 | { |
512 | if ( !(fmt.length() > fmtpos && str.length() > strpos) ) | 513 | if ( !(fmt.length() > fmtpos && str.length() > strpos) ) |
513 | goto error; | 514 | goto error; |
514 | 515 | ||
515 | QChar c = fmt.at(fmtpos++); | 516 | QChar c = fmt.at(fmtpos++); |
516 | 517 | ||
517 | if (c != '%') { | 518 | if (c != '%') { |
518 | if (c.isSpace()) | 519 | if (c.isSpace()) |
519 | strpos++; | 520 | strpos++; |
520 | else if (c != str.at(strpos++)) | 521 | else if (c != str.at(strpos++)) |
521 | goto error; | 522 | goto error; |
522 | continue; | 523 | continue; |
523 | } | 524 | } |
524 | 525 | ||
525 | // remove space at the begining | 526 | // remove space at the begining |
526 | if (str.length() > strpos && str.at(strpos).isSpace()) | 527 | if (str.length() > strpos && str.at(strpos).isSpace()) |
527 | strpos++; | 528 | strpos++; |
528 | 529 | ||
529 | c = fmt.at(fmtpos++); | 530 | c = fmt.at(fmtpos++); |
530 | switch (c) | 531 | switch (c.unicode()) |
531 | { | 532 | { |
532 | case 'a': | 533 | case 'a': |
533 | case 'A': | 534 | case 'A': |
534 | // this will just be ignored | 535 | // this will just be ignored |
535 | { // Cristian Tache: porting to Win: Block added because of "j" redefinition | 536 | { // Cristian Tache: porting to Win: Block added because of "j" redefinition |
536 | for (int j = 1; j < 8; j++) { | 537 | for (int j = 1; j < 8; j++) { |
537 | QString s = weekDayName(j, c == 'a').lower(); | 538 | QString s = weekDayName(j, c == 'a').lower(); |
538 | int len = s.length(); | 539 | int len = s.length(); |
539 | if (str.mid(strpos, len) == s) | 540 | if (str.mid(strpos, len) == s) |
540 | strpos += len; | 541 | strpos += len; |
541 | } | 542 | } |
542 | break; | 543 | break; |
543 | } | 544 | } |
544 | case 'b': | 545 | case 'b': |
545 | case 'B': | 546 | case 'B': |
546 | { // Cristian Tache: porting to Win: Block added because of "j" redefinition | 547 | { // Cristian Tache: porting to Win: Block added because of "j" redefinition |
547 | for (int j = 1; j < 13; j++) { | 548 | for (int j = 1; j < 13; j++) { |
548 | QString s = monthName(j, c == 'b').lower(); | 549 | QString s = monthName(j, c == 'b').lower(); |
549 | int len = s.length(); | 550 | int len = s.length(); |
550 | if (str.mid(strpos, len) == s) { | 551 | if (str.mid(strpos, len) == s) { |
551 | month = j; | 552 | month = j; |
552 | strpos += len; | 553 | strpos += len; |
553 | } | 554 | } |
554 | } | 555 | } |
555 | break; | 556 | break; |
556 | } | 557 | } |
557 | case 'd': | 558 | case 'd': |
558 | case 'e': | 559 | case 'e': |
559 | day = readInt(str, strpos); | 560 | day = readInt(str, strpos); |
560 | if (day < 1 || day > 31) | 561 | if (day < 1 || day > 31) |
561 | goto error; | 562 | goto error; |
562 | 563 | ||
563 | break; | 564 | break; |
564 | 565 | ||
565 | case 'n': | 566 | case 'n': |
566 | case 'm': | 567 | case 'm': |
567 | month = readInt(str, strpos); | 568 | month = readInt(str, strpos); |
568 | if (month < 1 || month > 12) | 569 | if (month < 1 || month > 12) |
569 | goto error; | 570 | goto error; |
570 | 571 | ||
571 | break; | 572 | break; |
572 | 573 | ||
573 | case 'Y': | 574 | case 'Y': |
574 | case 'y': | 575 | case 'y': |
575 | year = readInt(str, strpos); | 576 | year = readInt(str, strpos); |
576 | if (year < 0) | 577 | if (year < 0) |
577 | goto error; | 578 | goto error; |
578 | // Qt treats a year in the range 0-100 as 1900-1999. | 579 | // Qt treats a year in the range 0-100 as 1900-1999. |
579 | // It is nicer for the user if we treat 0-68 as 2000-2068 | 580 | // It is nicer for the user if we treat 0-68 as 2000-2068 |
580 | if (year < 69) | 581 | if (year < 69) |
581 | year += 2000; | 582 | year += 2000; |
582 | else if (c == 'y') | 583 | else if (c == 'y') |
583 | year += 1900; | 584 | year += 1900; |
584 | 585 | ||
585 | break; | 586 | break; |
586 | } | 587 | } |
587 | } | 588 | } |
588 | //kdDebug(173) << "KLocale::readDate day=" << day << " month=" << month << " year=" << year << endl; | 589 | //kdDebug(173) << "KLocale::readDate day=" << day << " month=" << month << " year=" << year << endl; |
589 | if ( year != -1 && month != -1 && day != -1 ) | 590 | if ( year != -1 && month != -1 && day != -1 ) |
590 | { | 591 | { |
591 | if (ok) *ok = true; | 592 | if (ok) *ok = true; |
592 | return QDate(year, month, day); | 593 | return QDate(year, month, day); |
593 | } | 594 | } |
594 | error: | 595 | error: |
595 | if (ok) *ok = false; | 596 | if (ok) *ok = false; |
596 | return QDate(); // invalid date | 597 | return QDate(); // invalid date |
597 | } | 598 | } |
598 | 599 | ||
599 | QTime KLocale::readTime(const QString &intstr, bool *ok) const | 600 | QTime KLocale::readTime(const QString &intstr, bool *ok) const |
600 | { | 601 | { |
601 | QTime _time; | 602 | QTime _time; |
602 | _time = readTime(intstr, false, ok); | 603 | _time = readTime(intstr, false, ok); |
603 | if (_time.isValid()) return _time; | 604 | if (_time.isValid()) return _time; |
604 | return readTime(intstr, true, ok); | 605 | return readTime(intstr, true, ok); |
605 | } | 606 | } |
606 | 607 | ||
607 | QTime KLocale::readTime(const QString &intstr, bool seconds, bool *ok) const | 608 | QTime KLocale::readTime(const QString &intstr, bool seconds, bool *ok) const |
608 | { | 609 | { |
609 | QString str = intstr.simplifyWhiteSpace().lower(); | 610 | QString str = intstr.simplifyWhiteSpace().lower(); |
610 | QString Format = timeFormat().simplifyWhiteSpace(); | 611 | QString Format = timeFormat().simplifyWhiteSpace(); |
611 | if (!seconds) | 612 | if (!seconds) |
612 | Format.replace(QRegExp(QString::fromLatin1(".%S")), QString::null); | 613 | Format.replace(QRegExp(QString::fromLatin1(".%S")), QString::null); |
613 | 614 | ||
614 | int hour = -1, minute = -1, second = seconds ? -1 : 0; // don't require seconds | 615 | int hour = -1, minute = -1, second = seconds ? -1 : 0; // don't require seconds |
615 | 616 | ||
616 | bool g_12h = false; | 617 | bool g_12h = false; |
617 | bool pm = false; | 618 | bool pm = false; |
618 | uint strpos = 0; | 619 | uint strpos = 0; |
619 | uint Formatpos = 0; | 620 | uint Formatpos = 0; |
620 | 621 | ||
621 | while (Format.length() > Formatpos || str.length() > strpos) | 622 | while (Format.length() > Formatpos || str.length() > strpos) |
622 | { | 623 | { |
623 | if ( !(Format.length() > Formatpos && str.length() > strpos) ) goto error; | 624 | if ( !(Format.length() > Formatpos && str.length() > strpos) ) goto error; |
624 | 625 | ||
625 | QChar c = Format.at(Formatpos++); | 626 | QChar c = Format.at(Formatpos++); |
626 | 627 | ||
627 | if (c != '%') | 628 | if (c != '%') |
628 | { | 629 | { |
629 | if (c.isSpace()) | 630 | if (c.isSpace()) |
630 | strpos++; | 631 | strpos++; |
631 | else if (c != str.at(strpos++)) | 632 | else if (c != str.at(strpos++)) |
632 | goto error; | 633 | goto error; |
633 | continue; | 634 | continue; |
634 | } | 635 | } |
635 | 636 | ||
636 | // remove space at the begining | 637 | // remove space at the begining |
637 | if (str.length() > strpos && str.at(strpos).isSpace()) | 638 | if (str.length() > strpos && str.at(strpos).isSpace()) |
638 | strpos++; | 639 | strpos++; |
639 | 640 | ||
640 | c = Format.at(Formatpos++); | 641 | c = Format.at(Formatpos++); |
641 | switch (c) | 642 | switch (c.unicode()) |
642 | { | 643 | { |
643 | case 'p': | 644 | case 'p': |
644 | { | 645 | { |
645 | QString s; | 646 | QString s; |
646 | s = i18n("pm").lower(); | 647 | s = i18n("pm").lower(); |
647 | int len = s.length(); | 648 | int len = s.length(); |
648 | if (str.mid(strpos, len) == s) | 649 | if (str.mid(strpos, len) == s) |
649 | { | 650 | { |
650 | pm = true; | 651 | pm = true; |
651 | strpos += len; | 652 | strpos += len; |
652 | } | 653 | } |
653 | else | 654 | else |
654 | { | 655 | { |
655 | s = i18n("am").lower(); | 656 | s = i18n("am").lower(); |
656 | len = s.length(); | 657 | len = s.length(); |
657 | if (str.mid(strpos, len) == s) { | 658 | if (str.mid(strpos, len) == s) { |
658 | pm = false; | 659 | pm = false; |
659 | strpos += len; | 660 | strpos += len; |
660 | } | 661 | } |
661 | else | 662 | else |
662 | goto error; | 663 | goto error; |
663 | } | 664 | } |
664 | } | 665 | } |
665 | break; | 666 | break; |
666 | 667 | ||
667 | case 'k': | 668 | case 'k': |
668 | case 'H': | 669 | case 'H': |
669 | g_12h = false; | 670 | g_12h = false; |
670 | hour = readInt(str, strpos); | 671 | hour = readInt(str, strpos); |
671 | if (hour < 0 || hour > 23) | 672 | if (hour < 0 || hour > 23) |
672 | goto error; | 673 | goto error; |
673 | 674 | ||
674 | break; | 675 | break; |
675 | 676 | ||
676 | case 'l': | 677 | case 'l': |
677 | case 'I': | 678 | case 'I': |
678 | g_12h = true; | 679 | g_12h = true; |
679 | hour = readInt(str, strpos); | 680 | hour = readInt(str, strpos); |
680 | if (hour < 1 || hour > 12) | 681 | if (hour < 1 || hour > 12) |
681 | goto error; | 682 | goto error; |
682 | 683 | ||
683 | break; | 684 | break; |
684 | 685 | ||
685 | case 'M': | 686 | case 'M': |
686 | minute = readInt(str, strpos); | 687 | minute = readInt(str, strpos); |
687 | if (minute < 0 || minute > 59) | 688 | if (minute < 0 || minute > 59) |
688 | goto error; | 689 | goto error; |
689 | 690 | ||
690 | break; | 691 | break; |
691 | 692 | ||
692 | case 'S': | 693 | case 'S': |
693 | second = readInt(str, strpos); | 694 | second = readInt(str, strpos); |
694 | if (second < 0 || second > 59) | 695 | if (second < 0 || second > 59) |
695 | goto error; | 696 | goto error; |
696 | 697 | ||
697 | break; | 698 | break; |
698 | } | 699 | } |
699 | } | 700 | } |
700 | if (g_12h) | 701 | if (g_12h) |
701 | { | 702 | { |
702 | hour %= 12; | 703 | hour %= 12; |
703 | if (pm) hour += 12; | 704 | if (pm) hour += 12; |
704 | } | 705 | } |
705 | 706 | ||
706 | if (ok) *ok = true; | 707 | if (ok) *ok = true; |
707 | return QTime(hour, minute, second); | 708 | return QTime(hour, minute, second); |
708 | 709 | ||
709 | error: | 710 | error: |
710 | if (ok) *ok = false; | 711 | if (ok) *ok = false; |
711 | return QTime(-1, -1, -1); // return invalid date if it didn't work | 712 | return QTime(-1, -1, -1); // return invalid date if it didn't work |
712 | // This will be removed in the near future, since it gives a warning on stderr. | 713 | // This will be removed in the near future, since it gives a warning on stderr. |
713 | // The presence of the bool* (since KDE-3.0) removes the need for an invalid QTime. | 714 | // The presence of the bool* (since KDE-3.0) removes the need for an invalid QTime. |
714 | } | 715 | } |
715 | 716 | ||
716 | QDateTime KLocale::readDateTime(const QString &intstr, | 717 | QDateTime KLocale::readDateTime(const QString &intstr, |
717 | IntDateFormat intIntDateFormat, | 718 | IntDateFormat intIntDateFormat, |
718 | bool* ok) const | 719 | bool* ok) const |
719 | { | 720 | { |
720 | bool ok1, ok2; | 721 | bool ok1, ok2; |
721 | 722 | ||
722 | // AT the moment we can not read any other format then ISODate | 723 | // AT the moment we can not read any other format then ISODate |
723 | if ( intIntDateFormat != ISODate ) | 724 | if ( intIntDateFormat != ISODate ) |
724 | { | 725 | { |
725 | qDebug("KLocale::readDateTime, only ISODate is supported."); | 726 | qDebug("KLocale::readDateTime, only ISODate is supported."); |
726 | return QDateTime(); | 727 | return QDateTime(); |
727 | } | 728 | } |
728 | 729 | ||
729 | int pos = intstr.find("T"); | 730 | int pos = intstr.find("T"); |
730 | QString date = intstr.left(pos); | 731 | QString date = intstr.left(pos); |
731 | QString time = intstr.mid(pos+1); | 732 | QString time = intstr.mid(pos+1); |
732 | 733 | ||
733 | QString dformat = dateFormat(intIntDateFormat); | 734 | QString dformat = dateFormat(intIntDateFormat); |
734 | QString tformat = timeFormat(intIntDateFormat); | 735 | QString tformat = timeFormat(intIntDateFormat); |
735 | 736 | ||
736 | QDate m_date = readDate(date, dformat, &ok1); | 737 | QDate m_date = readDate(date, dformat, &ok1); |
737 | QTime m_time = readTime(time, tformat, &ok2); | 738 | QTime m_time = readTime(time, !tformat.isEmpty(), &ok2); |
738 | 739 | ||
739 | QDateTime m_dt; | 740 | QDateTime m_dt; |
740 | 741 | ||
741 | if (ok) | 742 | if (ok) |
742 | { | 743 | { |
743 | if ((ok1 == false) || (ok2 == false)) | 744 | if ((ok1 == false) || (ok2 == false)) |
744 | *ok = false; | 745 | *ok = false; |
745 | else | 746 | else |
746 | *ok = true; | 747 | *ok = true; |
747 | } | 748 | } |
748 | 749 | ||
749 | //only set values if both operations returned true. | 750 | //only set values if both operations returned true. |
750 | if ((ok1 == true) && (ok2 == true)) | 751 | if ((ok1 == true) && (ok2 == true)) |
751 | { | 752 | { |
752 | m_dt.setDate(m_date); | 753 | m_dt.setDate(m_date); |
753 | m_dt.setTime(m_time); | 754 | m_dt.setTime(m_time); |
754 | } | 755 | } |
755 | 756 | ||
756 | //qDebug("KLocale::readDateTime() transformed %s into %s (%s), %s (%s) : err1=%i, err2=%i", intstr.latin1(), date.latin1(), dformat.latin1(), time.latin1(), tformat.latin1(), ok1, ok2); | 757 | //qDebug("KLocale::readDateTime() transformed %s into %s (%s), %s (%s) : err1=%i, err2=%i", intstr.latin1(), date.latin1(), dformat.latin1(), time.latin1(), tformat.latin1(), ok1, ok2); |
757 | return m_dt; | 758 | return m_dt; |
758 | } | 759 | } |
759 | 760 | ||
760 | QDate KLocale::readDate(const QString &intstr, | 761 | QDate KLocale::readDate(const QString &intstr, |
761 | IntDateFormat intIntDateFormat, | 762 | IntDateFormat intIntDateFormat, |
762 | bool* ok) const | 763 | bool* ok) const |
763 | { | 764 | { |
764 | bool ok1; | 765 | bool ok1; |
765 | 766 | ||
766 | QString dformat = dateFormat(intIntDateFormat); | 767 | QString dformat = dateFormat(intIntDateFormat); |
767 | 768 | ||
768 | QDate m_date = readDate(intstr, dformat, &ok1); | 769 | QDate m_date = readDate(intstr, dformat, &ok1); |
769 | 770 | ||
770 | if (ok) | 771 | if (ok) |
771 | *ok = ok1; | 772 | *ok = ok1; |
772 | 773 | ||
773 | //qDebug("KLocale::readDate() transformed %s into %s (%s), %s (%s) : err1=%i, err2=%i", intstr.latin1(), date.latin1(), dformat.latin1(), time.latin1(), tformat.latin1(), ok1, ok2); | 774 | //qDebug("KLocale::readDate() transformed %s into %s (%s), %s (%s) : err1=%i, err2=%i", intstr.latin1(), date.latin1(), dformat.latin1(), time.latin1(), tformat.latin1(), ok1, ok2); |
774 | return m_date; | 775 | return m_date; |
775 | } | 776 | } |
776 | 777 | ||
777 | 778 | ||
778 | bool KLocale::use12Clock() const | 779 | bool KLocale::use12Clock() const |
779 | { | 780 | { |
780 | return !mHourF24Format ;; | 781 | return !mHourF24Format ;; |
781 | } | 782 | } |
782 | 783 | ||
783 | bool KLocale::weekStartsMonday() const | 784 | bool KLocale::weekStartsMonday() const |
784 | { | 785 | { |
785 | return mWeekStartsMonday; | 786 | return mWeekStartsMonday; |
786 | } | 787 | } |
787 | 788 | ||
788 | int KLocale::weekStartDay() const | 789 | int KLocale::weekStartDay() const |
789 | { | 790 | { |
790 | if ( mWeekStartsMonday ) | 791 | if ( mWeekStartsMonday ) |
791 | return 1; | 792 | return 1; |
792 | return 7; | 793 | return 7; |
793 | } | 794 | } |
794 | 795 | ||
795 | QString KLocale::weekDayName(int i,bool shortName) const | 796 | QString KLocale::weekDayName(int i,bool shortName) const |
796 | { | 797 | { |
797 | if ( shortName ) | 798 | if ( shortName ) |
798 | switch ( i ) | 799 | switch ( i ) |
799 | { | 800 | { |
800 | case 1: return i18n("Monday", "Mon"); | 801 | case 1: return i18n("Monday", "Mon"); |
801 | case 2: return i18n("Tuesday", "Tue"); | 802 | case 2: return i18n("Tuesday", "Tue"); |
802 | case 3: return i18n("Wednesday", "Wed"); | 803 | case 3: return i18n("Wednesday", "Wed"); |
803 | case 4: return i18n("Thursday", "Thu"); | 804 | case 4: return i18n("Thursday", "Thu"); |
804 | case 5: return i18n("Friday", "Fri"); | 805 | case 5: return i18n("Friday", "Fri"); |
805 | case 6: return i18n("Saturday", "Sat"); | 806 | case 6: return i18n("Saturday", "Sat"); |
806 | case 7: return i18n("Sunday", "Sun"); | 807 | case 7: return i18n("Sunday", "Sun"); |
807 | } | 808 | } |
808 | else | 809 | else |
809 | switch ( i ) | 810 | switch ( i ) |
810 | { | 811 | { |
811 | case 1: return i18n("Monday"); | 812 | case 1: return i18n("Monday"); |
812 | case 2: return i18n("Tuesday"); | 813 | case 2: return i18n("Tuesday"); |
813 | case 3: return i18n("Wednesday"); | 814 | case 3: return i18n("Wednesday"); |
814 | case 4: return i18n("Thursday"); | 815 | case 4: return i18n("Thursday"); |
815 | case 5: return i18n("Friday"); | 816 | case 5: return i18n("Friday"); |
816 | case 6: return i18n("Saturday"); | 817 | case 6: return i18n("Saturday"); |
817 | case 7: return i18n("Sunday"); | 818 | case 7: return i18n("Sunday"); |
818 | } | 819 | } |
819 | 820 | ||
820 | return QString::null; | 821 | return QString::null; |
821 | } | 822 | } |
822 | 823 | ||
823 | QString KLocale::monthName(int i,bool shortName) const | 824 | QString KLocale::monthName(int i,bool shortName) const |
824 | { | 825 | { |
825 | if ( shortName ) | 826 | if ( shortName ) |
826 | switch ( i ) | 827 | switch ( i ) |
827 | { | 828 | { |
828 | case 1: return i18n("January", "Jan"); | 829 | case 1: return i18n("January", "Jan"); |
829 | case 2: return i18n("February", "Feb"); | 830 | case 2: return i18n("February", "Feb"); |
830 | case 3: return i18n("March", "Mar"); | 831 | case 3: return i18n("March", "Mar"); |
831 | case 4: return i18n("April", "Apr"); | 832 | case 4: return i18n("April", "Apr"); |
832 | case 5: return i18n("May short", "May"); | 833 | case 5: return i18n("May short", "May"); |
833 | case 6: return i18n("June", "Jun"); | 834 | case 6: return i18n("June", "Jun"); |