summaryrefslogtreecommitdiff
path: root/noncore/apps/tinykate/libkate/microkde/klocale.cpp
Unidiff
Diffstat (limited to 'noncore/apps/tinykate/libkate/microkde/klocale.cpp') (more/less context) (show whitespace changes)
-rw-r--r--noncore/apps/tinykate/libkate/microkde/klocale.cpp530
1 files changed, 530 insertions, 0 deletions
diff --git a/noncore/apps/tinykate/libkate/microkde/klocale.cpp b/noncore/apps/tinykate/libkate/microkde/klocale.cpp
new file mode 100644
index 0000000..dfdb97a
--- a/dev/null
+++ b/noncore/apps/tinykate/libkate/microkde/klocale.cpp
@@ -0,0 +1,530 @@
1#include <qregexp.h>
2
3#include "kdebug.h"
4
5#include "klocale.h"
6
7QString i18n(const char *text)
8{
9 return QString( text );
10}
11
12QString i18n(const char *,const char *text)
13{
14 return QString( text );
15}
16
17inline void put_it_in( QChar *buffer, uint& index, const QString &s )
18{
19 for ( uint l = 0; l < s.length(); l++ )
20 buffer[index++] = s.at( l );
21}
22
23inline void put_it_in( QChar *buffer, uint& index, int number )
24{
25 buffer[index++] = number / 10 + '0';
26 buffer[index++] = number % 10 + '0';
27}
28
29static int readInt(const QString &str, uint &pos)
30{
31 if (!str.at(pos).isDigit()) return -1;
32 int result = 0;
33 for (; str.length() > pos && str.at(pos).isDigit(); pos++)
34 {
35 result *= 10;
36 result += str.at(pos).digitValue();
37 }
38
39 return result;
40}
41
42QString KLocale::formatTime(const QTime &pTime, bool includeSecs) const
43{
44 const QString rst = timeFormat();
45
46 // only "pm/am" here can grow, the rest shrinks, but
47 // I'm rather safe than sorry
48 QChar *buffer = new QChar[rst.length() * 3 / 2 + 30];
49
50 uint index = 0;
51 bool escape = false;
52 int number = 0;
53
54 for ( uint format_index = 0; format_index < rst.length(); format_index++ )
55 {
56 if ( !escape )
57 {
58 if ( rst.at( format_index ).unicode() == '%' )
59 escape = true;
60 else
61 buffer[index++] = rst.at( format_index );
62 }
63 else
64 {
65 switch ( rst.at( format_index ).unicode() )
66 {
67 case '%':
68 buffer[index++] = '%';
69 break;
70 case 'H':
71 put_it_in( buffer, index, pTime.hour() );
72 break;
73 case 'I':
74 put_it_in( buffer, index, ( pTime.hour() + 11) % 12 + 1 );
75 break;
76 case 'M':
77 put_it_in( buffer, index, pTime.minute() );
78 break;
79 case 'S':
80 if (includeSecs)
81 put_it_in( buffer, index, pTime.second() );
82 else
83 {
84 // we remove the seperator sign before the seconds and
85 // assume that works everywhere
86 --index;
87 break;
88 }
89 break;
90 case 'k':
91 number = pTime.hour();
92 case 'l':
93 // to share the code
94 if ( rst.at( format_index ).unicode() == 'l' )
95 number = (pTime.hour() + 11) % 12 + 1;
96 if ( number / 10 )
97 buffer[index++] = number / 10 + '0';
98 buffer[index++] = number % 10 + '0';
99 break;
100 case 'p':
101 {
102 QString s;
103 if ( pTime.hour() >= 12 )
104 put_it_in( buffer, index, i18n("pm") );
105 else
106 put_it_in( buffer, index, i18n("am") );
107 break;
108 }
109 default:
110 buffer[index++] = rst.at( format_index );
111 break;
112 }
113 escape = false;
114 }
115 }
116 QString ret( buffer, index );
117 delete [] buffer;
118 return ret;
119}
120
121QString KLocale::formatDate(const QDate &pDate, bool shortFormat) const
122{
123 const QString rst = shortFormat?dateFormatShort():dateFormat();
124
125 // I'm rather safe than sorry
126 QChar *buffer = new QChar[rst.length() * 3 / 2 + 50];
127
128 unsigned int index = 0;
129 bool escape = false;
130 int number = 0;
131
132 for ( uint format_index = 0; format_index < rst.length(); ++format_index )
133 {
134 if ( !escape )
135 {
136 if ( rst.at( format_index ).unicode() == '%' )
137 escape = true;
138 else
139 buffer[index++] = rst.at( format_index );
140 }
141 else
142 {
143 switch ( rst.at( format_index ).unicode() )
144 {
145 case '%':
146 buffer[index++] = '%';
147 break;
148 case 'Y':
149 put_it_in( buffer, index, pDate.year() / 100 );
150 case 'y':
151 put_it_in( buffer, index, pDate.year() % 100 );
152 break;
153 case 'n':
154 number = pDate.month();
155 case 'e':
156 // to share the code
157 if ( rst.at( format_index ).unicode() == 'e' )
158 number = pDate.day();
159 if ( number / 10 )
160 buffer[index++] = number / 10 + '0';
161 buffer[index++] = number % 10 + '0';
162 break;
163 case 'm':
164 put_it_in( buffer, index, pDate.month() );
165 break;
166 case 'b':
167 put_it_in( buffer, index, monthName(pDate.month(), true) );
168 break;
169 case 'B':
170 put_it_in( buffer, index, monthName(pDate.month(), false) );
171 break;
172 case 'd':
173 put_it_in( buffer, index, pDate.day() );
174 break;
175 case 'a':
176 put_it_in( buffer, index, weekDayName(pDate.dayOfWeek(), true) );
177 break;
178 case 'A':
179 put_it_in( buffer, index, weekDayName(pDate.dayOfWeek(), false) );
180 break;
181 default:
182 buffer[index++] = rst.at( format_index );
183 break;
184 }
185 escape = false;
186 }
187 }
188 QString ret( buffer, index );
189 delete [] buffer;
190 return ret;
191}
192
193QString KLocale::formatDateTime(const QDateTime &pDateTime,
194 bool shortFormat,
195 bool includeSeconds) const
196{
197 return i18n("concatenation of dates and time", "%1 %2")
198 .arg( formatDate( pDateTime.date(), shortFormat ) )
199 .arg( formatTime( pDateTime.time(), includeSeconds ) );
200}
201
202QString KLocale::formatDateTime(const QDateTime &pDateTime) const
203{
204 return formatDateTime(pDateTime, true);
205}
206
207QDate KLocale::readDate(const QString &intstr, bool* ok) const
208{
209 QDate date;
210 date = readDate(intstr, true, ok);
211 if (date.isValid()) return date;
212 return readDate(intstr, false, ok);
213}
214
215QDate KLocale::readDate(const QString &intstr, bool shortFormat, bool* ok) const
216{
217 QString fmt = (shortFormat ? dateFormatShort() : dateFormat()).simplifyWhiteSpace();
218 return readDate( intstr, fmt, ok );
219}
220
221QDate KLocale::readDate(const QString &intstr, const QString &fmt, bool* ok) const
222{
223 //kdDebug(173) << "KLocale::readDate intstr=" << intstr << " fmt=" << fmt << endl;
224 QString str = intstr.simplifyWhiteSpace().lower();
225 int day = -1, month = -1;
226 // allow the year to be omitted if not in the format
227 int year = QDate::currentDate().year();
228 uint strpos = 0;
229 uint fmtpos = 0;
230
231 while (fmt.length() > fmtpos || str.length() > strpos)
232 {
233 if ( !(fmt.length() > fmtpos && str.length() > strpos) )
234 goto error;
235
236 QChar c = fmt.at(fmtpos++);
237
238 if (c != '%') {
239 if (c.isSpace())
240 strpos++;
241 else if (c != str.at(strpos++))
242 goto error;
243 continue;
244 }
245
246 // remove space at the begining
247 if (str.length() > strpos && str.at(strpos).isSpace())
248 strpos++;
249
250 c = fmt.at(fmtpos++);
251 switch (c)
252 {
253 case 'a':
254 case 'A':
255 // this will just be ignored
256 { // Cristian Tache: porting to Win: Block added because of "j" redefinition
257 for (int j = 1; j < 8; j++) {
258 QString s = weekDayName(j, c == 'a').lower();
259 int len = s.length();
260 if (str.mid(strpos, len) == s)
261 strpos += len;
262 }
263 break;
264 }
265 case 'b':
266 case 'B':
267 { // Cristian Tache: porting to Win: Block added because of "j" redefinition
268 for (int j = 1; j < 13; j++) {
269 QString s = monthName(j, c == 'b').lower();
270 int len = s.length();
271 if (str.mid(strpos, len) == s) {
272 month = j;
273 strpos += len;
274 }
275 }
276 break;
277 }
278 case 'd':
279 case 'e':
280 day = readInt(str, strpos);
281 if (day < 1 || day > 31)
282 goto error;
283
284 break;
285
286 case 'n':
287 case 'm':
288 month = readInt(str, strpos);
289 if (month < 1 || month > 12)
290 goto error;
291
292 break;
293
294 case 'Y':
295 case 'y':
296 year = readInt(str, strpos);
297 if (year < 0)
298 goto error;
299 // Qt treats a year in the range 0-100 as 1900-1999.
300 // It is nicer for the user if we treat 0-68 as 2000-2068
301 if (year < 69)
302 year += 2000;
303 else if (c == 'y')
304 year += 1900;
305
306 break;
307 }
308 }
309 //kdDebug(173) << "KLocale::readDate day=" << day << " month=" << month << " year=" << year << endl;
310 if ( year != -1 && month != -1 && day != -1 )
311 {
312 if (ok) *ok = true;
313 return QDate(year, month, day);
314 }
315 error:
316 if (ok) *ok = false;
317 return QDate(); // invalid date
318}
319
320QTime KLocale::readTime(const QString &intstr, bool *ok) const
321{
322 QTime _time;
323 _time = readTime(intstr, true, ok);
324 if (_time.isValid()) return _time;
325 return readTime(intstr, false, ok);
326}
327
328QTime KLocale::readTime(const QString &intstr, bool seconds, bool *ok) const
329{
330 QString str = intstr.simplifyWhiteSpace().lower();
331 QString Format = timeFormat().simplifyWhiteSpace();
332 if (!seconds)
333 Format.replace(QRegExp(QString::fromLatin1(".%S")), QString::null);
334
335 int hour = -1, minute = -1, second = seconds ? -1 : 0; // don't require seconds
336 bool g_12h = false;
337 bool pm = false;
338 uint strpos = 0;
339 uint Formatpos = 0;
340
341 while (Format.length() > Formatpos || str.length() > strpos)
342 {
343 if ( !(Format.length() > Formatpos && str.length() > strpos) ) goto error;
344
345 QChar c = Format.at(Formatpos++);
346
347 if (c != '%')
348 {
349 if (c.isSpace())
350 strpos++;
351 else if (c != str.at(strpos++))
352 goto error;
353 continue;
354 }
355
356 // remove space at the begining
357 if (str.length() > strpos && str.at(strpos).isSpace())
358 strpos++;
359
360 c = Format.at(Formatpos++);
361 switch (c)
362 {
363 case 'p':
364 {
365 QString s;
366 s = i18n("pm").lower();
367 int len = s.length();
368 if (str.mid(strpos, len) == s)
369 {
370 pm = true;
371 strpos += len;
372 }
373 else
374 {
375 s = i18n("am").lower();
376 len = s.length();
377 if (str.mid(strpos, len) == s) {
378 pm = false;
379 strpos += len;
380 }
381 else
382 goto error;
383 }
384 }
385 break;
386
387 case 'k':
388 case 'H':
389 g_12h = false;
390 hour = readInt(str, strpos);
391 if (hour < 0 || hour > 23)
392 goto error;
393
394 break;
395
396 case 'l':
397 case 'I':
398 g_12h = true;
399 hour = readInt(str, strpos);
400 if (hour < 1 || hour > 12)
401 goto error;
402
403 break;
404
405 case 'M':
406 minute = readInt(str, strpos);
407 if (minute < 0 || minute > 59)
408 goto error;
409
410 break;
411
412 case 'S':
413 second = readInt(str, strpos);
414 if (second < 0 || second > 59)
415 goto error;
416
417 break;
418 }
419 }
420 if (g_12h)
421 {
422 hour %= 12;
423 if (pm) hour += 12;
424 }
425
426 if (ok) *ok = true;
427 return QTime(hour, minute, second);
428
429 error:
430 if (ok) *ok = false;
431 return QTime(-1, -1, -1); // return invalid date if it didn't work
432 // This will be removed in the near future, since it gives a warning on stderr.
433 // The presence of the bool* (since KDE-3.0) removes the need for an invalid QTime.
434}
435
436bool KLocale::use12Clock() const
437{
438 return false;
439}
440
441bool KLocale::weekStartsMonday() const
442{
443 return true;
444}
445
446QString KLocale::weekDayName(int i,bool shortName) const
447{
448 if ( shortName )
449 switch ( i )
450 {
451 case 1: return i18n("Monday", "Mon");
452 case 2: return i18n("Tuesday", "Tue");
453 case 3: return i18n("Wednesday", "Wed");
454 case 4: return i18n("Thursday", "Thu");
455 case 5: return i18n("Friday", "Fri");
456 case 6: return i18n("Saturday", "Sat");
457 case 7: return i18n("Sunday", "Sun");
458 }
459 else
460 switch ( i )
461 {
462 case 1: return i18n("Monday");
463 case 2: return i18n("Tuesday");
464 case 3: return i18n("Wednesday");
465 case 4: return i18n("Thursday");
466 case 5: return i18n("Friday");
467 case 6: return i18n("Saturday");
468 case 7: return i18n("Sunday");
469 }
470
471 return QString::null;
472}
473
474QString KLocale::monthName(int i,bool shortName) const
475{
476 if ( shortName )
477 switch ( i )
478 {
479 case 1: return i18n("January", "Jan");
480 case 2: return i18n("February", "Feb");
481 case 3: return i18n("March", "Mar");
482 case 4: return i18n("April", "Apr");
483 case 5: return i18n("May short", "May");
484 case 6: return i18n("June", "Jun");
485 case 7: return i18n("July", "Jul");
486 case 8: return i18n("August", "Aug");
487 case 9: return i18n("September", "Sep");
488 case 10: return i18n("October", "Oct");
489 case 11: return i18n("November", "Nov");
490 case 12: return i18n("December", "Dec");
491 }
492 else
493 switch (i)
494 {
495 case 1: return i18n("January");
496 case 2: return i18n("February");
497 case 3: return i18n("March");
498 case 4: return i18n("April");
499 case 5: return i18n("May long", "May");
500 case 6: return i18n("June");
501 case 7: return i18n("July");
502 case 8: return i18n("August");
503 case 9: return i18n("September");
504 case 10: return i18n("October");
505 case 11: return i18n("November");
506 case 12: return i18n("December");
507 }
508
509 return QString::null;
510}
511
512QString KLocale::country() const
513{
514 return QString::null;
515}
516
517QString KLocale::dateFormat() const
518{
519 return "%A %d %B %Y";
520}
521
522QString KLocale::dateFormatShort() const
523{
524 return "%d.%m.%Y";
525}
526
527QString KLocale::timeFormat() const
528{
529 return "%H:%M:%S";
530}