-rw-r--r-- | library/datebookdb.cpp | 3 |
1 files changed, 2 insertions, 1 deletions
diff --git a/library/datebookdb.cpp b/library/datebookdb.cpp index 2ac9a0c..a26fe8f 100644 --- a/library/datebookdb.cpp +++ b/library/datebookdb.cpp | |||
@@ -352,769 +352,770 @@ static bool nextAlarm( const Event &ev, QDateTime& when, int& warn) | |||
352 | when = ralarm; | 352 | when = ralarm; |
353 | warn = ev.alarmTime(); | 353 | warn = ev.alarmTime(); |
354 | } | 354 | } |
355 | } | 355 | } |
356 | } | 356 | } |
357 | } else { | 357 | } else { |
358 | warn = ev.alarmTime(); | 358 | warn = ev.alarmTime(); |
359 | when = ev.start().addSecs( -ev.alarmTime()*60 ); | 359 | when = ev.start().addSecs( -ev.alarmTime()*60 ); |
360 | } | 360 | } |
361 | return when > now; | 361 | return when > now; |
362 | } | 362 | } |
363 | 363 | ||
364 | static void addEventAlarm( const Event &ev ) | 364 | static void addEventAlarm( const Event &ev ) |
365 | { | 365 | { |
366 | QDateTime when; | 366 | QDateTime when; |
367 | int warn; | 367 | int warn; |
368 | if ( nextAlarm(ev,when,warn) ) | 368 | if ( nextAlarm(ev,when,warn) ) |
369 | AlarmServer::addAlarm( when, | 369 | AlarmServer::addAlarm( when, |
370 | "QPE/Application/datebook", | 370 | "QPE/Application/datebook", |
371 | "alarm(QDateTime,int)", warn ); | 371 | "alarm(QDateTime,int)", warn ); |
372 | } | 372 | } |
373 | 373 | ||
374 | static void delEventAlarm( const Event &ev ) | 374 | static void delEventAlarm( const Event &ev ) |
375 | { | 375 | { |
376 | QDateTime when; | 376 | QDateTime when; |
377 | int warn; | 377 | int warn; |
378 | if ( nextAlarm(ev,when,warn) ) | 378 | if ( nextAlarm(ev,when,warn) ) |
379 | AlarmServer::deleteAlarm( when, | 379 | AlarmServer::deleteAlarm( when, |
380 | "QPE/Application/datebook", | 380 | "QPE/Application/datebook", |
381 | "alarm(QDateTime,int)", warn ); | 381 | "alarm(QDateTime,int)", warn ); |
382 | } | 382 | } |
383 | 383 | ||
384 | 384 | ||
385 | DateBookDB::DateBookDB() | 385 | DateBookDB::DateBookDB() |
386 | { | 386 | { |
387 | init(); | 387 | init(); |
388 | } | 388 | } |
389 | 389 | ||
390 | DateBookDB::~DateBookDB() | 390 | DateBookDB::~DateBookDB() |
391 | { | 391 | { |
392 | save(); | 392 | save(); |
393 | eventList.clear(); | 393 | eventList.clear(); |
394 | repeatEvents.clear(); | 394 | repeatEvents.clear(); |
395 | } | 395 | } |
396 | 396 | ||
397 | 397 | ||
398 | //#### Why is this code duplicated in getEffectiveEvents ????? | 398 | //#### Why is this code duplicated in getEffectiveEvents ????? |
399 | //#### Addendum. Don't use this function, lets faze it out if we can. | 399 | //#### Addendum. Don't use this function, lets faze it out if we can. |
400 | QValueList<Event> DateBookDB::getEvents( const QDate &from, const QDate &to ) | 400 | QValueList<Event> DateBookDB::getEvents( const QDate &from, const QDate &to ) |
401 | { | 401 | { |
402 | QValueList<Event> tmpList; | 402 | QValueList<Event> tmpList; |
403 | tmpList = getNonRepeatingEvents( from, to ); | 403 | tmpList = getNonRepeatingEvents( from, to ); |
404 | 404 | ||
405 | // check for repeating events... | 405 | // check for repeating events... |
406 | for (QValueList<Event>::ConstIterator it = repeatEvents.begin(); | 406 | for (QValueList<Event>::ConstIterator it = repeatEvents.begin(); |
407 | it != repeatEvents.end(); ++it) { | 407 | it != repeatEvents.end(); ++it) { |
408 | QDate itDate = from; | 408 | QDate itDate = from; |
409 | QDateTime due; | 409 | QDateTime due; |
410 | 410 | ||
411 | /* create a false end date, to short circuit on hard | 411 | /* create a false end date, to short circuit on hard |
412 | MonthlyDay recurences */ | 412 | MonthlyDay recurences */ |
413 | Event dummy_event = *it; | 413 | Event dummy_event = *it; |
414 | Event::RepeatPattern r = dummy_event.repeatPattern(); | 414 | Event::RepeatPattern r = dummy_event.repeatPattern(); |
415 | if ( !r.hasEndDate || r.endDate() > to ) { | 415 | if ( !r.hasEndDate || r.endDate() > to ) { |
416 | r.setEndDate( to ); | 416 | r.setEndDate( to ); |
417 | r.hasEndDate = TRUE; | 417 | r.hasEndDate = TRUE; |
418 | } | 418 | } |
419 | dummy_event.setRepeat(TRUE, r); | 419 | dummy_event.setRepeat(TRUE, r); |
420 | 420 | ||
421 | while (nextOccurance(dummy_event, itDate, due)) { | 421 | while (nextOccurance(dummy_event, itDate, due)) { |
422 | if (due.date() > to) | 422 | if (due.date() > to) |
423 | break; | 423 | break; |
424 | Event newEvent = *it; | 424 | Event newEvent = *it; |
425 | newEvent.setStart(due); | 425 | newEvent.setStart(due); |
426 | newEvent.setEnd(due.addSecs((*it).start().secsTo((*it).end()))); | 426 | newEvent.setEnd(due.addSecs((*it).start().secsTo((*it).end()))); |
427 | 427 | ||
428 | tmpList.append(newEvent); | 428 | tmpList.append(newEvent); |
429 | itDate = due.date().addDays(1); /* the next event */ | 429 | itDate = due.date().addDays(1); /* the next event */ |
430 | } | 430 | } |
431 | } | 431 | } |
432 | qHeapSort(tmpList); | 432 | qHeapSort(tmpList); |
433 | return tmpList; | 433 | return tmpList; |
434 | } | 434 | } |
435 | 435 | ||
436 | QValueList<Event> DateBookDB::getEvents( const QDateTime &start ) | 436 | QValueList<Event> DateBookDB::getEvents( const QDateTime &start ) |
437 | { | 437 | { |
438 | QValueList<Event> day = getEvents(start.date(),start.date()); | 438 | QValueList<Event> day = getEvents(start.date(),start.date()); |
439 | 439 | ||
440 | QValueListConstIterator<Event> it; | 440 | QValueListConstIterator<Event> it; |
441 | QDateTime dtTmp; | 441 | QDateTime dtTmp; |
442 | QValueList<Event> tmpList; | 442 | QValueList<Event> tmpList; |
443 | for (it = day.begin(); it != day.end(); ++it ) { | 443 | for (it = day.begin(); it != day.end(); ++it ) { |
444 | dtTmp = (*it).start(TRUE); | 444 | dtTmp = (*it).start(TRUE); |
445 | if ( dtTmp == start ) | 445 | if ( dtTmp == start ) |
446 | tmpList.append( *it ); | 446 | tmpList.append( *it ); |
447 | } | 447 | } |
448 | return tmpList; | 448 | return tmpList; |
449 | } | 449 | } |
450 | 450 | ||
451 | //#### Why is this code duplicated in getEvents ????? | 451 | //#### Why is this code duplicated in getEvents ????? |
452 | 452 | ||
453 | QValueList<EffectiveEvent> DateBookDB::getEffectiveEvents( const QDate &from, | 453 | QValueList<EffectiveEvent> DateBookDB::getEffectiveEvents( const QDate &from, |
454 | const QDate &to ) | 454 | const QDate &to ) |
455 | { | 455 | { |
456 | QValueList<EffectiveEvent> tmpList; | 456 | QValueList<EffectiveEvent> tmpList; |
457 | QValueListIterator<Event> it; | 457 | QValueListIterator<Event> it; |
458 | 458 | ||
459 | EffectiveEvent effEv; | 459 | EffectiveEvent effEv; |
460 | QDateTime dtTmp, | 460 | QDateTime dtTmp, |
461 | dtEnd; | 461 | dtEnd; |
462 | 462 | ||
463 | for (it = eventList.begin(); it != eventList.end(); ++it ) { | 463 | for (it = eventList.begin(); it != eventList.end(); ++it ) { |
464 | if (!(*it).isValidUid()) | 464 | if (!(*it).isValidUid()) |
465 | (*it).assignUid(); // FIXME: Hack to restore cleared uids | 465 | (*it).assignUid(); // FIXME: Hack to restore cleared uids |
466 | 466 | ||
467 | dtTmp = (*it).start(TRUE); | 467 | dtTmp = (*it).start(TRUE); |
468 | dtEnd = (*it).end(TRUE); | 468 | dtEnd = (*it).end(TRUE); |
469 | 469 | ||
470 | if ( dtTmp.date() >= from && dtTmp.date() <= to ) { | 470 | if ( dtTmp.date() >= from && dtTmp.date() <= to ) { |
471 | Event tmpEv = *it; | 471 | Event tmpEv = *it; |
472 | effEv.setEvent(tmpEv); | 472 | effEv.setEvent(tmpEv); |
473 | effEv.setDate( dtTmp.date() ); | 473 | effEv.setDate( dtTmp.date() ); |
474 | effEv.setStart( dtTmp.time() ); | 474 | effEv.setStart( dtTmp.time() ); |
475 | if ( dtTmp.date() != dtEnd.date() ) | 475 | if ( dtTmp.date() != dtEnd.date() ) |
476 | effEv.setEnd( QTime(23, 59, 0) ); | 476 | effEv.setEnd( QTime(23, 59, 0) ); |
477 | else | 477 | else |
478 | effEv.setEnd( dtEnd.time() ); | 478 | effEv.setEnd( dtEnd.time() ); |
479 | tmpList.append( effEv ); | 479 | tmpList.append( effEv ); |
480 | } | 480 | } |
481 | // we must also check for end date information... | 481 | // we must also check for end date information... |
482 | if ( dtEnd.date() != dtTmp.date() && dtEnd.date() >= from ) { | 482 | if ( dtEnd.date() != dtTmp.date() && dtEnd.date() >= from ) { |
483 | QDateTime dt = dtTmp.addDays( 1 ); | 483 | QDateTime dt = dtTmp.addDays( 1 ); |
484 | dt.setTime( QTime(0, 0, 0) ); | 484 | dt.setTime( QTime(0, 0, 0) ); |
485 | QDateTime dtStop; | 485 | QDateTime dtStop; |
486 | if ( dtEnd > to ) { | 486 | if ( dtEnd > to ) { |
487 | dtStop = to; | 487 | dtStop = to; |
488 | } else | 488 | } else |
489 | dtStop = dtEnd; | 489 | dtStop = dtEnd; |
490 | while ( dt <= dtStop ) { | 490 | while ( dt <= dtStop ) { |
491 | Event tmpEv = *it; | 491 | Event tmpEv = *it; |
492 | effEv.setEvent( tmpEv ); | 492 | effEv.setEvent( tmpEv ); |
493 | effEv.setDate( dt.date() ); | 493 | effEv.setDate( dt.date() ); |
494 | if ( dt >= from ) { | 494 | if ( dt >= from ) { |
495 | effEv.setStart( QTime(0, 0, 0) ); | 495 | effEv.setStart( QTime(0, 0, 0) ); |
496 | if ( dt.date() == dtEnd.date() ) | 496 | if ( dt.date() == dtEnd.date() ) |
497 | effEv.setEnd( dtEnd.time() ); | 497 | effEv.setEnd( dtEnd.time() ); |
498 | else | 498 | else |
499 | effEv.setEnd( QTime(23, 59, 59) ); | 499 | effEv.setEnd( QTime(23, 59, 59) ); |
500 | tmpList.append( effEv ); | 500 | tmpList.append( effEv ); |
501 | } | 501 | } |
502 | dt = dt.addDays( 1 ); | 502 | dt = dt.addDays( 1 ); |
503 | } | 503 | } |
504 | } | 504 | } |
505 | } | 505 | } |
506 | // check for repeating events... | 506 | // check for repeating events... |
507 | QDateTime repeat; | 507 | QDateTime repeat; |
508 | for ( it = repeatEvents.begin(); it != repeatEvents.end(); ++it ) { | 508 | for ( it = repeatEvents.begin(); it != repeatEvents.end(); ++it ) { |
509 | if (!(*it).isValidUid()) | 509 | if (!(*it).isValidUid()) |
510 | (*it).assignUid(); // FIXME: Hack to restore cleared uids | 510 | (*it).assignUid(); // FIXME: Hack to restore cleared uids |
511 | 511 | ||
512 | /* create a false end date, to short circuit on hard | 512 | /* create a false end date, to short circuit on hard |
513 | MonthlyDay recurences */ | 513 | MonthlyDay recurences */ |
514 | Event dummy_event = *it; | 514 | Event dummy_event = *it; |
515 | int duration = (*it).start().date().daysTo( (*it).end().date() ); | 515 | int duration = (*it).start().date().daysTo( (*it).end().date() ); |
516 | QDate itDate = from.addDays(-duration); | 516 | QDate itDate = from.addDays(-duration); |
517 | 517 | ||
518 | Event::RepeatPattern r = dummy_event.repeatPattern(); | 518 | Event::RepeatPattern r = dummy_event.repeatPattern(); |
519 | if ( !r.hasEndDate || r.endDate() > to ) { | 519 | if ( !r.hasEndDate || r.endDate() > to ) { |
520 | r.setEndDate( to ); | 520 | r.setEndDate( to ); |
521 | r.hasEndDate = TRUE; | 521 | r.hasEndDate = TRUE; |
522 | } | 522 | } |
523 | dummy_event.setRepeat(TRUE, r); | 523 | dummy_event.setRepeat(TRUE, r); |
524 | 524 | ||
525 | while (nextOccurance(dummy_event, itDate, repeat)) { | 525 | while (nextOccurance(dummy_event, itDate, repeat)) { |
526 | if(repeat.date() > to) | 526 | if(repeat.date() > to) |
527 | break; | 527 | break; |
528 | effEv.setDate( repeat.date() ); | 528 | effEv.setDate( repeat.date() ); |
529 | if ((*it).type() == Event::AllDay) { | 529 | if ((*it).type() == Event::AllDay) { |
530 | effEv.setStart( QTime(0,0,0) ); | 530 | effEv.setStart( QTime(0,0,0) ); |
531 | effEv.setEnd( QTime(23,59,59) ); | 531 | effEv.setEnd( QTime(23,59,59) ); |
532 | } else { | 532 | } else { |
533 | /* we only occur by days, not hours/minutes/seconds. Hence | 533 | /* we only occur by days, not hours/minutes/seconds. Hence |
534 | the actual end and start times will be the same for | 534 | the actual end and start times will be the same for |
535 | every repeated event. For multi day events this is | 535 | every repeated event. For multi day events this is |
536 | fixed up later if on wronge day span */ | 536 | fixed up later if on wronge day span */ |
537 | effEv.setStart( (*it).start().time() ); | 537 | effEv.setStart( (*it).start().time() ); |
538 | effEv.setEnd( (*it).end().time() ); | 538 | effEv.setEnd( (*it).end().time() ); |
539 | } | 539 | } |
540 | if ( duration != 0 ) { | 540 | if ( duration != 0 ) { |
541 | // multi-day repeating events | 541 | // multi-day repeating events |
542 | QDate sub_it = QMAX( repeat.date(), from ); | 542 | QDate sub_it = QMAX( repeat.date(), from ); |
543 | QDate startDate = repeat.date(); | 543 | QDate startDate = repeat.date(); |
544 | QDate endDate = startDate.addDays( duration ); | 544 | QDate endDate = startDate.addDays( duration ); |
545 | 545 | ||
546 | while ( sub_it <= endDate && sub_it <= to ) { | 546 | while ( sub_it <= endDate && sub_it <= to ) { |
547 | EffectiveEvent tmpEffEv = effEv; | 547 | EffectiveEvent tmpEffEv = effEv; |
548 | Event tmpEv = *it; | 548 | Event tmpEv = *it; |
549 | tmpEffEv.setEvent( tmpEv ); | 549 | tmpEffEv.setEvent( tmpEv ); |
550 | 550 | ||
551 | if ( sub_it != startDate ) | 551 | if ( sub_it != startDate ) |
552 | tmpEffEv.setStart( QTime(0,0,0) ); | 552 | tmpEffEv.setStart( QTime(0,0,0) ); |
553 | if ( sub_it != endDate ) | 553 | if ( sub_it != endDate ) |
554 | tmpEffEv.setEnd( QTime(23,59,59) ); | 554 | tmpEffEv.setEnd( QTime(23,59,59) ); |
555 | tmpEffEv.setDate( sub_it ); | 555 | tmpEffEv.setDate( sub_it ); |
556 | tmpEffEv.setEffectiveDates( startDate, endDate ); | 556 | tmpEffEv.setEffectiveDates( startDate, endDate ); |
557 | tmpList.append( tmpEffEv ); | 557 | tmpList.append( tmpEffEv ); |
558 | sub_it = sub_it.addDays( 1 ); | 558 | sub_it = sub_it.addDays( 1 ); |
559 | } | 559 | } |
560 | itDate = endDate; | 560 | itDate = endDate; |
561 | } else { | 561 | } else { |
562 | Event tmpEv = *it; | 562 | Event tmpEv = *it; |
563 | effEv.setEvent( tmpEv ); | 563 | effEv.setEvent( tmpEv ); |
564 | tmpList.append( effEv ); | 564 | tmpList.append( effEv ); |
565 | itDate = repeat.date().addDays( 1 ); | 565 | itDate = repeat.date().addDays( 1 ); |
566 | } | 566 | } |
567 | } | 567 | } |
568 | } | 568 | } |
569 | 569 | ||
570 | qHeapSort( tmpList ); | 570 | qHeapSort( tmpList ); |
571 | return tmpList; | 571 | return tmpList; |
572 | } | 572 | } |
573 | 573 | ||
574 | QValueList<EffectiveEvent> DateBookDB::getEffectiveEvents( const QDateTime &dt) | 574 | QValueList<EffectiveEvent> DateBookDB::getEffectiveEvents( const QDateTime &dt) |
575 | { | 575 | { |
576 | QValueList<EffectiveEvent> day = getEffectiveEvents(dt.date(), dt.date()); | 576 | QValueList<EffectiveEvent> day = getEffectiveEvents(dt.date(), dt.date()); |
577 | QValueListConstIterator<EffectiveEvent> it; | 577 | QValueListConstIterator<EffectiveEvent> it; |
578 | QValueList<EffectiveEvent> tmpList; | 578 | QValueList<EffectiveEvent> tmpList; |
579 | QDateTime dtTmp; | 579 | QDateTime dtTmp; |
580 | 580 | ||
581 | for (it = day.begin(); it != day.end(); ++it ) { | 581 | for (it = day.begin(); it != day.end(); ++it ) { |
582 | dtTmp = QDateTime( (*it).date(), (*it).start() ); | 582 | dtTmp = QDateTime( (*it).date(), (*it).start() ); |
583 | // at the moment we don't have second granularity, be nice about that.. | 583 | // at the moment we don't have second granularity, be nice about that.. |
584 | if ( QABS(dt.secsTo(dtTmp)) < 60 ) | 584 | if ( QABS(dt.secsTo(dtTmp)) < 60 ) |
585 | tmpList.append( *it ); | 585 | tmpList.append( *it ); |
586 | } | 586 | } |
587 | return tmpList; | 587 | return tmpList; |
588 | } | 588 | } |
589 | 589 | ||
590 | void DateBookDB::addEvent( const Event &ev, bool doalarm ) | 590 | void DateBookDB::addEvent( const Event &ev, bool doalarm ) |
591 | { | 591 | { |
592 | // write to the journal... | 592 | // write to the journal... |
593 | saveJournalEntry( ev, ACTION_ADD, -1, false ); | 593 | saveJournalEntry( ev, ACTION_ADD, -1, false ); |
594 | addJFEvent( ev, doalarm ); | 594 | addJFEvent( ev, doalarm ); |
595 | d->clean = false; | 595 | d->clean = false; |
596 | } | 596 | } |
597 | 597 | ||
598 | void DateBookDB::addJFEvent( const Event &ev, bool doalarm ) | 598 | void DateBookDB::addJFEvent( const Event &ev, bool doalarm ) |
599 | { | 599 | { |
600 | if ( doalarm && ev.hasAlarm() ) | 600 | if ( doalarm && ev.hasAlarm() ) |
601 | addEventAlarm( ev ); | 601 | addEventAlarm( ev ); |
602 | if ( ev.hasRepeat() ) | 602 | if ( ev.hasRepeat() ) |
603 | repeatEvents.append( ev ); | 603 | repeatEvents.append( ev ); |
604 | else | 604 | else |
605 | eventList.append( ev ); | 605 | eventList.append( ev ); |
606 | } | 606 | } |
607 | 607 | ||
608 | void DateBookDB::editEvent( const Event &old, Event &editedEv ) | 608 | void DateBookDB::editEvent( const Event &old, Event &editedEv ) |
609 | { | 609 | { |
610 | int oldIndex=0; | 610 | int oldIndex=0; |
611 | bool oldHadRepeat = old.hasRepeat(); | 611 | bool oldHadRepeat = old.hasRepeat(); |
612 | Event orig; | 612 | Event orig; |
613 | 613 | ||
614 | // write to the journal... | 614 | // write to the journal... |
615 | if ( oldHadRepeat ) { | 615 | if ( oldHadRepeat ) { |
616 | if ( origRepeat( old, orig ) ) // should work always... | 616 | if ( origRepeat( old, orig ) ) // should work always... |
617 | oldIndex = repeatEvents.findIndex( orig ); | 617 | oldIndex = repeatEvents.findIndex( orig ); |
618 | } else | 618 | } else |
619 | oldIndex = eventList.findIndex( old ); | 619 | oldIndex = eventList.findIndex( old ); |
620 | saveJournalEntry( editedEv, ACTION_REPLACE, oldIndex, oldHadRepeat ); | 620 | saveJournalEntry( editedEv, ACTION_REPLACE, oldIndex, oldHadRepeat ); |
621 | 621 | ||
622 | // Delete old event | 622 | // Delete old event |
623 | if ( old.hasAlarm() ) | 623 | if ( old.hasAlarm() ) |
624 | delEventAlarm( old ); | 624 | delEventAlarm( old ); |
625 | if ( oldHadRepeat ) { | 625 | if ( oldHadRepeat ) { |
626 | if ( editedEv.hasRepeat() ) { // This mean that origRepeat was run above and | 626 | if ( editedEv.hasRepeat() ) { // This mean that origRepeat was run above and |
627 | // orig is initialized | 627 | // orig is initialized |
628 | // assumption, when someone edits a repeating event, they | 628 | // assumption, when someone edits a repeating event, they |
629 | // want to change them all, maybe not perfect, but it works | 629 | // want to change them all, maybe not perfect, but it works |
630 | // for the moment... | 630 | // for the moment... |
631 | repeatEvents.remove( orig ); | 631 | repeatEvents.remove( orig ); |
632 | } else | 632 | } else |
633 | removeRepeat( old ); | 633 | removeRepeat( old ); |
634 | } else { | 634 | } else { |
635 | QValueList<Event>::Iterator it = eventList.find( old ); | 635 | QValueList<Event>::Iterator it = eventList.find( old ); |
636 | if ( it != eventList.end() ) | 636 | if ( it != eventList.end() ) |
637 | eventList.remove( it ); | 637 | eventList.remove( it ); |
638 | } | 638 | } |
639 | 639 | ||
640 | // Add new event | 640 | // Add new event |
641 | if ( editedEv.hasAlarm() ) | 641 | if ( editedEv.hasAlarm() ) |
642 | addEventAlarm( editedEv ); | 642 | addEventAlarm( editedEv ); |
643 | if ( editedEv.hasRepeat() ) | 643 | if ( editedEv.hasRepeat() ) |
644 | repeatEvents.append( editedEv ); | 644 | repeatEvents.append( editedEv ); |
645 | else | 645 | else |
646 | eventList.append( editedEv ); | 646 | eventList.append( editedEv ); |
647 | 647 | ||
648 | d->clean = false; | 648 | d->clean = false; |
649 | } | 649 | } |
650 | 650 | ||
651 | void DateBookDB::removeEvent( const Event &ev ) | 651 | void DateBookDB::removeEvent( const Event &ev ) |
652 | { | 652 | { |
653 | // write to the journal... | 653 | // write to the journal... |
654 | saveJournalEntry( ev, ACTION_REMOVE, -1, false ); | 654 | saveJournalEntry( ev, ACTION_REMOVE, -1, false ); |
655 | removeJFEvent( ev ); | 655 | removeJFEvent( ev ); |
656 | d->clean = false; | 656 | d->clean = false; |
657 | } | 657 | } |
658 | 658 | ||
659 | void DateBookDB::removeJFEvent( const Event&ev ) | 659 | void DateBookDB::removeJFEvent( const Event&ev ) |
660 | { | 660 | { |
661 | if ( ev.hasAlarm() ) | 661 | if ( ev.hasAlarm() ) |
662 | delEventAlarm( ev ); | 662 | delEventAlarm( ev ); |
663 | if ( ev.hasRepeat() ) { | 663 | if ( ev.hasRepeat() ) { |
664 | removeRepeat( ev ); | 664 | removeRepeat( ev ); |
665 | } else { | 665 | } else { |
666 | QValueList<Event>::Iterator it = eventList.find( ev ); | 666 | QValueList<Event>::Iterator it = eventList.find( ev ); |
667 | if ( it != eventList.end() ) | 667 | if ( it != eventList.end() ) |
668 | eventList.remove( it ); | 668 | eventList.remove( it ); |
669 | } | 669 | } |
670 | } | 670 | } |
671 | 671 | ||
672 | // also handles journaling... | 672 | // also handles journaling... |
673 | void DateBookDB::loadFile( const QString &strFile ) | 673 | void DateBookDB::loadFile( const QString &strFile ) |
674 | { | 674 | { |
675 | 675 | ||
676 | QFile f( strFile ); | 676 | QFile f( strFile ); |
677 | if ( !f.open( IO_ReadOnly ) ) | 677 | if ( !f.open( IO_ReadOnly ) ) |
678 | return; | 678 | return; |
679 | 679 | ||
680 | enum Attribute { | 680 | enum Attribute { |
681 | FDescription = 0, | 681 | FDescription = 0, |
682 | FLocation, | 682 | FLocation, |
683 | FCategories, | 683 | FCategories, |
684 | FUid, | 684 | FUid, |
685 | FType, | 685 | FType, |
686 | FAlarm, | 686 | FAlarm, |
687 | FSound, | 687 | FSound, |
688 | FRType, | 688 | FRType, |
689 | FRWeekdays, | 689 | FRWeekdays, |
690 | FRPosition, | 690 | FRPosition, |
691 | FRFreq, | 691 | FRFreq, |
692 | FRHasEndDate, | 692 | FRHasEndDate, |
693 | FREndDate, | 693 | FREndDate, |
694 | FRStart, | 694 | FRStart, |
695 | FREnd, | 695 | FREnd, |
696 | FNote, | 696 | FNote, |
697 | FCreated, | 697 | FCreated, |
698 | FAction, | 698 | FAction, |
699 | FActionKey, | 699 | FActionKey, |
700 | FJournalOrigHadRepeat | 700 | FJournalOrigHadRepeat |
701 | }; | 701 | }; |
702 | 702 | ||
703 | QAsciiDict<int> dict( 97 ); | 703 | QAsciiDict<int> dict( 97 ); |
704 | dict.setAutoDelete( TRUE ); | 704 | dict.setAutoDelete( TRUE ); |
705 | dict.insert( "description", new int(FDescription) ); | 705 | dict.insert( "description", new int(FDescription) ); |
706 | dict.insert( "location", new int(FLocation) ); | 706 | dict.insert( "location", new int(FLocation) ); |
707 | dict.insert( "categories", new int(FCategories) ); | 707 | dict.insert( "categories", new int(FCategories) ); |
708 | dict.insert( "uid", new int(FUid) ); | 708 | dict.insert( "uid", new int(FUid) ); |
709 | dict.insert( "type", new int(FType) ); | 709 | dict.insert( "type", new int(FType) ); |
710 | dict.insert( "alarm", new int(FAlarm) ); | 710 | dict.insert( "alarm", new int(FAlarm) ); |
711 | dict.insert( "sound", new int(FSound) ); | 711 | dict.insert( "sound", new int(FSound) ); |
712 | dict.insert( "rtype", new int(FRType) ); | 712 | dict.insert( "rtype", new int(FRType) ); |
713 | dict.insert( "rweekdays", new int(FRWeekdays) ); | 713 | dict.insert( "rweekdays", new int(FRWeekdays) ); |
714 | dict.insert( "rposition", new int(FRPosition) ); | 714 | dict.insert( "rposition", new int(FRPosition) ); |
715 | dict.insert( "rfreq", new int(FRFreq) ); | 715 | dict.insert( "rfreq", new int(FRFreq) ); |
716 | dict.insert( "rhasenddate", new int(FRHasEndDate) ); | 716 | dict.insert( "rhasenddate", new int(FRHasEndDate) ); |
717 | dict.insert( "enddt", new int(FREndDate) ); | 717 | dict.insert( "enddt", new int(FREndDate) ); |
718 | dict.insert( "start", new int(FRStart) ); | 718 | dict.insert( "start", new int(FRStart) ); |
719 | dict.insert( "end", new int(FREnd) ); | 719 | dict.insert( "end", new int(FREnd) ); |
720 | dict.insert( "note", new int(FNote) ); | 720 | dict.insert( "note", new int(FNote) ); |
721 | dict.insert( "created", new int(FCreated) ); | 721 | dict.insert( "created", new int(FCreated) ); |
722 | dict.insert( "action", new int(FAction) ); | 722 | dict.insert( "action", new int(FAction) ); |
723 | dict.insert( "actionkey", new int(FActionKey) ); | 723 | dict.insert( "actionkey", new int(FActionKey) ); |
724 | dict.insert( "actionorig", new int (FJournalOrigHadRepeat) ); | 724 | dict.insert( "actionorig", new int (FJournalOrigHadRepeat) ); |
725 | 725 | ||
726 | 726 | ||
727 | QByteArray ba = f.readAll(); | 727 | QByteArray ba = f.readAll(); |
728 | char* dt = ba.data(); | 728 | char* dt = ba.data(); |
729 | int len = ba.size(); | 729 | int len = ba.size(); |
730 | int currentAction, | 730 | int currentAction, |
731 | journalKey, | 731 | journalKey, |
732 | origHadRepeat; // should be bool, but we need tri-state(not being used) | 732 | origHadRepeat; // should be bool, but we need tri-state(not being used) |
733 | 733 | ||
734 | int i = 0; | 734 | int i = 0; |
735 | char *point; | 735 | char *point; |
736 | while ( ( point = strstr( dt+i, "<event " ) ) != 0 ) { | 736 | // hack to get rid of segfaults after reading </DATEBOOK> |
737 | while ( (dt+i != 0) && (( point = strstr( dt+i, "<event " ) ) != 0 )) { | ||
737 | i = point - dt; | 738 | i = point - dt; |
738 | // if we are reading in events in the general case, | 739 | // if we are reading in events in the general case, |
739 | // we are just adding them, so let the actions represent that... | 740 | // we are just adding them, so let the actions represent that... |
740 | currentAction = ACTION_ADD; | 741 | currentAction = ACTION_ADD; |
741 | journalKey = -1; | 742 | journalKey = -1; |
742 | origHadRepeat = -1; | 743 | origHadRepeat = -1; |
743 | // some temporary variables for dates and times ... | 744 | // some temporary variables for dates and times ... |
744 | //int startY = 0, startM = 0, startD = 0, starth = 0, startm = 0, starts = 0; | 745 | //int startY = 0, startM = 0, startD = 0, starth = 0, startm = 0, starts = 0; |
745 | //int endY = 0, endM = 0, endD = 0, endh = 0, endm = 0, ends = 0; | 746 | //int endY = 0, endM = 0, endD = 0, endh = 0, endm = 0, ends = 0; |
746 | //int enddtY = 0, enddtM = 0, enddtD = 0; | 747 | //int enddtY = 0, enddtM = 0, enddtD = 0; |
747 | 748 | ||
748 | // ... for the alarm settings ... | 749 | // ... for the alarm settings ... |
749 | int alarmTime = -1; Event::SoundTypeChoice alarmSound = Event::Silent; | 750 | int alarmTime = -1; Event::SoundTypeChoice alarmSound = Event::Silent; |
750 | // ... and for the recurrence | 751 | // ... and for the recurrence |
751 | Event::RepeatPattern rp; | 752 | Event::RepeatPattern rp; |
752 | Event e; | 753 | Event e; |
753 | 754 | ||
754 | i += 7; | 755 | i += 7; |
755 | 756 | ||
756 | while( 1 ) { | 757 | while( 1 ) { |
757 | while ( i < len && (dt[i] == ' ' || dt[i] == '\n' || dt[i] == '\r') ) | 758 | while ( i < len && (dt[i] == ' ' || dt[i] == '\n' || dt[i] == '\r') ) |
758 | ++i; | 759 | ++i; |
759 | if ( i >= len-2 || (dt[i] == '/' && dt[i+1] == '>') ) | 760 | if ( i >= len-2 || (dt[i] == '/' && dt[i+1] == '>') ) |
760 | break; | 761 | break; |
761 | // we have another attribute, read it. | 762 | // we have another attribute, read it. |
762 | int j = i; | 763 | int j = i; |
763 | while ( j < len && dt[j] != '=' ) | 764 | while ( j < len && dt[j] != '=' ) |
764 | ++j; | 765 | ++j; |
765 | char *attr = dt+i; | 766 | char *attr = dt+i; |
766 | dt[j] = '\0'; | 767 | dt[j] = '\0'; |
767 | i = ++j; // skip = | 768 | i = ++j; // skip = |
768 | while ( i < len && dt[i] != '"' ) | 769 | while ( i < len && dt[i] != '"' ) |
769 | ++i; | 770 | ++i; |
770 | j = ++i; | 771 | j = ++i; |
771 | bool haveAmp = FALSE; | 772 | bool haveAmp = FALSE; |
772 | bool haveUtf = FALSE; | 773 | bool haveUtf = FALSE; |
773 | while ( j < len && dt[j] != '"' ) { | 774 | while ( j < len && dt[j] != '"' ) { |
774 | if ( dt[j] == '&' ) | 775 | if ( dt[j] == '&' ) |
775 | haveAmp = TRUE; | 776 | haveAmp = TRUE; |
776 | if ( ((unsigned char)dt[j]) > 0x7f ) | 777 | if ( ((unsigned char)dt[j]) > 0x7f ) |
777 | haveUtf = TRUE; | 778 | haveUtf = TRUE; |
778 | ++j; | 779 | ++j; |
779 | } | 780 | } |
780 | 781 | ||
781 | if ( i == j ) { | 782 | if ( i == j ) { |
782 | // leave out empty attributes | 783 | // leave out empty attributes |
783 | i = j + 1; | 784 | i = j + 1; |
784 | continue; | 785 | continue; |
785 | } | 786 | } |
786 | 787 | ||
787 | QString value = haveUtf ? QString::fromUtf8( dt+i, j-i ) | 788 | QString value = haveUtf ? QString::fromUtf8( dt+i, j-i ) |
788 | : QString::fromLatin1( dt+i, j-i ); | 789 | : QString::fromLatin1( dt+i, j-i ); |
789 | if ( haveAmp ) | 790 | if ( haveAmp ) |
790 | value = Qtopia::plainString( value ); | 791 | value = Qtopia::plainString( value ); |
791 | i = j + 1; | 792 | i = j + 1; |
792 | 793 | ||
793 | //qDebug("attr='%s' value='%s'", attr.data(), value.latin1() ); | 794 | //qDebug("attr='%s' value='%s'", attr.data(), value.latin1() ); |
794 | int * find = dict[ attr ]; | 795 | int * find = dict[ attr ]; |
795 | #if 1 | 796 | #if 1 |
796 | if ( !find ) { | 797 | if ( !find ) { |
797 | // custom field | 798 | // custom field |
798 | e.setCustomField(attr, value); | 799 | e.setCustomField(attr, value); |
799 | continue; | 800 | continue; |
800 | } | 801 | } |
801 | 802 | ||
802 | switch( *find ) { | 803 | switch( *find ) { |
803 | case FDescription: | 804 | case FDescription: |
804 | e.setDescription( value ); | 805 | e.setDescription( value ); |
805 | break; | 806 | break; |
806 | case FLocation: | 807 | case FLocation: |
807 | e.setLocation( value ); | 808 | e.setLocation( value ); |
808 | break; | 809 | break; |
809 | case FCategories: | 810 | case FCategories: |
810 | e.setCategories( Qtopia::Record::idsFromString( value ) ); | 811 | e.setCategories( Qtopia::Record::idsFromString( value ) ); |
811 | break; | 812 | break; |
812 | case FUid: | 813 | case FUid: |
813 | e.setUid( value.toInt() ); | 814 | e.setUid( value.toInt() ); |
814 | break; | 815 | break; |
815 | case FType: | 816 | case FType: |
816 | if ( value == "AllDay" ) | 817 | if ( value == "AllDay" ) |
817 | e.setType( Event::AllDay ); | 818 | e.setType( Event::AllDay ); |
818 | else | 819 | else |
819 | e.setType( Event::Normal ); | 820 | e.setType( Event::Normal ); |
820 | break; | 821 | break; |
821 | case FAlarm: | 822 | case FAlarm: |
822 | alarmTime = value.toInt(); | 823 | alarmTime = value.toInt(); |
823 | break; | 824 | break; |
824 | case FSound: | 825 | case FSound: |
825 | alarmSound = value == "loud" ? Event::Loud : Event::Silent; | 826 | alarmSound = value == "loud" ? Event::Loud : Event::Silent; |
826 | break; | 827 | break; |
827 | // recurrence stuff | 828 | // recurrence stuff |
828 | case FRType: | 829 | case FRType: |
829 | if ( value == "Daily" ) | 830 | if ( value == "Daily" ) |
830 | rp.type = Event::Daily; | 831 | rp.type = Event::Daily; |
831 | else if ( value == "Weekly" ) | 832 | else if ( value == "Weekly" ) |
832 | rp.type = Event::Weekly; | 833 | rp.type = Event::Weekly; |
833 | else if ( value == "MonthlyDay" ) | 834 | else if ( value == "MonthlyDay" ) |
834 | rp.type = Event::MonthlyDay; | 835 | rp.type = Event::MonthlyDay; |
835 | else if ( value == "MonthlyDate" ) | 836 | else if ( value == "MonthlyDate" ) |
836 | rp.type = Event::MonthlyDate; | 837 | rp.type = Event::MonthlyDate; |
837 | else if ( value == "Yearly" ) | 838 | else if ( value == "Yearly" ) |
838 | rp.type = Event::Yearly; | 839 | rp.type = Event::Yearly; |
839 | else | 840 | else |
840 | rp.type = Event::NoRepeat; | 841 | rp.type = Event::NoRepeat; |
841 | break; | 842 | break; |
842 | case FRWeekdays: | 843 | case FRWeekdays: |
843 | rp.days = value.toInt(); | 844 | rp.days = value.toInt(); |
844 | break; | 845 | break; |
845 | case FRPosition: | 846 | case FRPosition: |
846 | rp.position = value.toInt(); | 847 | rp.position = value.toInt(); |
847 | break; | 848 | break; |
848 | case FRFreq: | 849 | case FRFreq: |
849 | rp.frequency = value.toInt(); | 850 | rp.frequency = value.toInt(); |
850 | break; | 851 | break; |
851 | case FRHasEndDate: | 852 | case FRHasEndDate: |
852 | rp.hasEndDate = value.toInt(); | 853 | rp.hasEndDate = value.toInt(); |
853 | break; | 854 | break; |
854 | case FREndDate: { | 855 | case FREndDate: { |
855 | rp.endDateUTC = (time_t) value.toLong(); | 856 | rp.endDateUTC = (time_t) value.toLong(); |
856 | break; | 857 | break; |
857 | } | 858 | } |
858 | case FRStart: { | 859 | case FRStart: { |
859 | e.setStart( (time_t) value.toLong() ); | 860 | e.setStart( (time_t) value.toLong() ); |
860 | break; | 861 | break; |
861 | } | 862 | } |
862 | case FREnd: { | 863 | case FREnd: { |
863 | e.setEnd( (time_t) value.toLong() ); | 864 | e.setEnd( (time_t) value.toLong() ); |
864 | break; | 865 | break; |
865 | } | 866 | } |
866 | case FNote: | 867 | case FNote: |
867 | e.setNotes( value ); | 868 | e.setNotes( value ); |
868 | break; | 869 | break; |
869 | case FCreated: | 870 | case FCreated: |
870 | rp.createTime = value.toInt(); | 871 | rp.createTime = value.toInt(); |
871 | break; | 872 | break; |
872 | case FAction: | 873 | case FAction: |
873 | currentAction = value.toInt(); | 874 | currentAction = value.toInt(); |
874 | break; | 875 | break; |
875 | case FActionKey: | 876 | case FActionKey: |
876 | journalKey = value.toInt(); | 877 | journalKey = value.toInt(); |
877 | break; | 878 | break; |
878 | case FJournalOrigHadRepeat: | 879 | case FJournalOrigHadRepeat: |
879 | origHadRepeat = value.toInt(); | 880 | origHadRepeat = value.toInt(); |
880 | break; | 881 | break; |
881 | default: | 882 | default: |
882 | qDebug( "huh??? missing enum? -- attr.: %s", attr ); | 883 | qDebug( "huh??? missing enum? -- attr.: %s", attr ); |
883 | break; | 884 | break; |
884 | } | 885 | } |
885 | #endif | 886 | #endif |
886 | } | 887 | } |
887 | // "post processing" (dates, times, alarm, recurrence) | 888 | // "post processing" (dates, times, alarm, recurrence) |
888 | // start date/time | 889 | // start date/time |
889 | e.setRepeat( rp.type != Event::NoRepeat, rp ); | 890 | e.setRepeat( rp.type != Event::NoRepeat, rp ); |
890 | 891 | ||
891 | if ( alarmTime != -1 ) | 892 | if ( alarmTime != -1 ) |
892 | e.setAlarm( TRUE, alarmTime, alarmSound ); | 893 | e.setAlarm( TRUE, alarmTime, alarmSound ); |
893 | 894 | ||
894 | // now do our action based on the current action... | 895 | // now do our action based on the current action... |
895 | switch ( currentAction ) { | 896 | switch ( currentAction ) { |
896 | case ACTION_ADD: | 897 | case ACTION_ADD: |
897 | addJFEvent( e ); | 898 | addJFEvent( e ); |
898 | break; | 899 | break; |
899 | case ACTION_REMOVE: | 900 | case ACTION_REMOVE: |
900 | removeJFEvent( e ); | 901 | removeJFEvent( e ); |
901 | break; | 902 | break; |
902 | case ACTION_REPLACE: | 903 | case ACTION_REPLACE: |
903 | // be a little bit careful, | 904 | // be a little bit careful, |
904 | // in case of a messed up journal... | 905 | // in case of a messed up journal... |
905 | if ( journalKey > -1 && origHadRepeat > -1 ) { | 906 | if ( journalKey > -1 && origHadRepeat > -1 ) { |
906 | // get the original from proper list... | 907 | // get the original from proper list... |
907 | if ( origHadRepeat ) | 908 | if ( origHadRepeat ) |
908 | removeJFEvent( *(repeatEvents.at(journalKey)) ); | 909 | removeJFEvent( *(repeatEvents.at(journalKey)) ); |
909 | else | 910 | else |
910 | removeJFEvent( *(eventList.at(journalKey)) ); | 911 | removeJFEvent( *(eventList.at(journalKey)) ); |
911 | addJFEvent( e ); | 912 | addJFEvent( e ); |
912 | } | 913 | } |
913 | break; | 914 | break; |
914 | default: | 915 | default: |
915 | break; | 916 | break; |
916 | } | 917 | } |
917 | } | 918 | } |
918 | f.close(); | 919 | f.close(); |
919 | } | 920 | } |
920 | 921 | ||
921 | void DateBookDB::init() | 922 | void DateBookDB::init() |
922 | { | 923 | { |
923 | d = new DateBookDBPrivate; | 924 | d = new DateBookDBPrivate; |
924 | d->clean = false; | 925 | d->clean = false; |
925 | QString str = dateBookFilename(); | 926 | QString str = dateBookFilename(); |
926 | if ( str.isNull() ) { | 927 | if ( str.isNull() ) { |
927 | QMessageBox::warning( 0, QObject::tr("Out of Space"), | 928 | QMessageBox::warning( 0, QObject::tr("Out of Space"), |
928 | QObject::tr("Unable to create start up files\n" | 929 | QObject::tr("Unable to create start up files\n" |
929 | "Please free up some space\n" | 930 | "Please free up some space\n" |
930 | "before entering data") ); | 931 | "before entering data") ); |
931 | } | 932 | } |
932 | // continuing along, we call this datebook filename again, | 933 | // continuing along, we call this datebook filename again, |
933 | // because they may fix it before continuing, though it seems | 934 | // because they may fix it before continuing, though it seems |
934 | // pretty unlikely... | 935 | // pretty unlikely... |
935 | loadFile( dateBookFilename() ); | 936 | loadFile( dateBookFilename() ); |
936 | 937 | ||
937 | if ( QFile::exists( dateBookJournalFile() ) ) { | 938 | if ( QFile::exists( dateBookJournalFile() ) ) { |
938 | // merge the journal | 939 | // merge the journal |
939 | loadFile( dateBookJournalFile() ); | 940 | loadFile( dateBookJournalFile() ); |
940 | // save in our changes and remove the journal... | 941 | // save in our changes and remove the journal... |
941 | save(); | 942 | save(); |
942 | } | 943 | } |
943 | d->clean = true; | 944 | d->clean = true; |
944 | } | 945 | } |
945 | 946 | ||
946 | bool DateBookDB::save() | 947 | bool DateBookDB::save() |
947 | { | 948 | { |
948 | if ( d->clean == true ) | 949 | if ( d->clean == true ) |
949 | return true; | 950 | return true; |
950 | QValueListIterator<Event> it; | 951 | QValueListIterator<Event> it; |
951 | int total_written; | 952 | int total_written; |
952 | QString strFileNew = dateBookFilename() + ".new"; | 953 | QString strFileNew = dateBookFilename() + ".new"; |
953 | 954 | ||
954 | QFile f( strFileNew ); | 955 | QFile f( strFileNew ); |
955 | if ( !f.open( IO_WriteOnly|IO_Raw ) ) | 956 | if ( !f.open( IO_WriteOnly|IO_Raw ) ) |
956 | return FALSE; | 957 | return FALSE; |
957 | 958 | ||
958 | QString buf( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ); | 959 | QString buf( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ); |
959 | buf += "<!DOCTYPE DATEBOOK><DATEBOOK>\n"; | 960 | buf += "<!DOCTYPE DATEBOOK><DATEBOOK>\n"; |
960 | buf += "<events>\n"; | 961 | buf += "<events>\n"; |
961 | QCString str = buf.utf8(); | 962 | QCString str = buf.utf8(); |
962 | total_written = f.writeBlock( str.data(), str.length() ); | 963 | total_written = f.writeBlock( str.data(), str.length() ); |
963 | if ( total_written != int(str.length()) ) { | 964 | if ( total_written != int(str.length()) ) { |
964 | f.close(); | 965 | f.close(); |
965 | QFile::remove( strFileNew ); | 966 | QFile::remove( strFileNew ); |
966 | return false; | 967 | return false; |
967 | } | 968 | } |
968 | 969 | ||
969 | for ( it = eventList.begin(); it != eventList.end(); ++it ) { | 970 | for ( it = eventList.begin(); it != eventList.end(); ++it ) { |
970 | buf = "<event"; | 971 | buf = "<event"; |
971 | (*it).save( buf ); | 972 | (*it).save( buf ); |
972 | buf += " />\n"; | 973 | buf += " />\n"; |
973 | str = buf.utf8(); | 974 | str = buf.utf8(); |
974 | total_written = f.writeBlock( str.data(), str.length() ); | 975 | total_written = f.writeBlock( str.data(), str.length() ); |
975 | if ( total_written != int(str.length()) ) { | 976 | if ( total_written != int(str.length()) ) { |
976 | f.close(); | 977 | f.close(); |
977 | QFile::remove( strFileNew ); | 978 | QFile::remove( strFileNew ); |
978 | return false; | 979 | return false; |
979 | } | 980 | } |
980 | } | 981 | } |
981 | for ( it = repeatEvents.begin(); it != repeatEvents.end(); ++it ) { | 982 | for ( it = repeatEvents.begin(); it != repeatEvents.end(); ++it ) { |
982 | buf = "<event"; | 983 | buf = "<event"; |
983 | (*it).save( buf ); | 984 | (*it).save( buf ); |
984 | buf += " />\n"; | 985 | buf += " />\n"; |
985 | str = buf.utf8(); | 986 | str = buf.utf8(); |
986 | total_written = f.writeBlock( str.data(), str.length() ); | 987 | total_written = f.writeBlock( str.data(), str.length() ); |
987 | if ( total_written != int(str.length()) ) { | 988 | if ( total_written != int(str.length()) ) { |
988 | f.close(); | 989 | f.close(); |
989 | QFile::remove( strFileNew ); | 990 | QFile::remove( strFileNew ); |
990 | return false; | 991 | return false; |
991 | } | 992 | } |
992 | } | 993 | } |
993 | buf = "</events>\n</DATEBOOK>\n"; | 994 | buf = "</events>\n</DATEBOOK>\n"; |
994 | str = buf.utf8(); | 995 | str = buf.utf8(); |
995 | total_written = f.writeBlock( str.data(), str.length() ); | 996 | total_written = f.writeBlock( str.data(), str.length() ); |
996 | if ( total_written != int(str.length()) ) { | 997 | if ( total_written != int(str.length()) ) { |
997 | f.close(); | 998 | f.close(); |
998 | QFile::remove( strFileNew ); | 999 | QFile::remove( strFileNew ); |
999 | return false; | 1000 | return false; |
1000 | } | 1001 | } |
1001 | f.close(); | 1002 | f.close(); |
1002 | 1003 | ||
1003 | // now rename... I like to use the systemcall | 1004 | // now rename... I like to use the systemcall |
1004 | if ( ::rename( strFileNew, dateBookFilename() ) < 0 ) { | 1005 | if ( ::rename( strFileNew, dateBookFilename() ) < 0 ) { |
1005 | qWarning( "problem renaming file %s to %s errno %d", | 1006 | qWarning( "problem renaming file %s to %s errno %d", |
1006 | strFileNew.latin1(), dateBookFilename().latin1(), errno ); | 1007 | strFileNew.latin1(), dateBookFilename().latin1(), errno ); |
1007 | // remove the file, otherwise it will just stick around... | 1008 | // remove the file, otherwise it will just stick around... |
1008 | QFile::remove( strFileNew ); | 1009 | QFile::remove( strFileNew ); |
1009 | } | 1010 | } |
1010 | 1011 | ||
1011 | // may as well remove the journal file... | 1012 | // may as well remove the journal file... |
1012 | QFile::remove( dateBookJournalFile() ); | 1013 | QFile::remove( dateBookJournalFile() ); |
1013 | d->clean = true; | 1014 | d->clean = true; |
1014 | return true; | 1015 | return true; |
1015 | } | 1016 | } |
1016 | 1017 | ||
1017 | void DateBookDB::reload() | 1018 | void DateBookDB::reload() |
1018 | { | 1019 | { |
1019 | QValueList<Event>::Iterator it = eventList.begin(); | 1020 | QValueList<Event>::Iterator it = eventList.begin(); |
1020 | for ( ; it != eventList.end(); ++it ) { | 1021 | for ( ; it != eventList.end(); ++it ) { |
1021 | if ( (*it).hasAlarm() ) | 1022 | if ( (*it).hasAlarm() ) |
1022 | delEventAlarm( *it ); | 1023 | delEventAlarm( *it ); |
1023 | if ( (*it).hasRepeat() ) | 1024 | if ( (*it).hasRepeat() ) |
1024 | removeRepeat( *it ); | 1025 | removeRepeat( *it ); |
1025 | } | 1026 | } |
1026 | eventList.clear(); | 1027 | eventList.clear(); |
1027 | repeatEvents.clear(); // should be a NOP | 1028 | repeatEvents.clear(); // should be a NOP |
1028 | init(); | 1029 | init(); |
1029 | } | 1030 | } |
1030 | 1031 | ||
1031 | bool DateBookDB::removeRepeat( const Event &ev ) | 1032 | bool DateBookDB::removeRepeat( const Event &ev ) |
1032 | { | 1033 | { |
1033 | time_t removeMe = ev.repeatPattern().createTime; | 1034 | time_t removeMe = ev.repeatPattern().createTime; |
1034 | QValueListIterator<Event> it; | 1035 | QValueListIterator<Event> it; |
1035 | for ( it = repeatEvents.begin(); it != repeatEvents.end(); ++it ) { | 1036 | for ( it = repeatEvents.begin(); it != repeatEvents.end(); ++it ) { |
1036 | if ( removeMe == (*it).repeatPattern().createTime ) { | 1037 | if ( removeMe == (*it).repeatPattern().createTime ) { |
1037 | repeatEvents.remove( *it ); | 1038 | repeatEvents.remove( *it ); |
1038 | // best break, or we are going into undefined territory! | 1039 | // best break, or we are going into undefined territory! |
1039 | return TRUE; | 1040 | return TRUE; |
1040 | } | 1041 | } |
1041 | } | 1042 | } |
1042 | return FALSE; | 1043 | return FALSE; |
1043 | } | 1044 | } |
1044 | 1045 | ||
1045 | bool DateBookDB::origRepeat( const Event &ev, Event &orig ) const | 1046 | bool DateBookDB::origRepeat( const Event &ev, Event &orig ) const |
1046 | { | 1047 | { |
1047 | time_t removeMe = ev.repeatPattern().createTime; | 1048 | time_t removeMe = ev.repeatPattern().createTime; |
1048 | QValueListConstIterator<Event> it; | 1049 | QValueListConstIterator<Event> it; |
1049 | for ( it = repeatEvents.begin(); it != repeatEvents.end(); ++it ) { | 1050 | for ( it = repeatEvents.begin(); it != repeatEvents.end(); ++it ) { |
1050 | if ( removeMe == (*it).repeatPattern().createTime ) { | 1051 | if ( removeMe == (*it).repeatPattern().createTime ) { |
1051 | orig = (*it); | 1052 | orig = (*it); |
1052 | return TRUE; | 1053 | return TRUE; |
1053 | } | 1054 | } |
1054 | } | 1055 | } |
1055 | return FALSE; | 1056 | return FALSE; |
1056 | } | 1057 | } |
1057 | 1058 | ||
1058 | void DateBookDB::saveJournalEntry( const Event &ev, journal_action action ) | 1059 | void DateBookDB::saveJournalEntry( const Event &ev, journal_action action ) |
1059 | { | 1060 | { |
1060 | saveJournalEntry( ev, action, -1, false ); | 1061 | saveJournalEntry( ev, action, -1, false ); |
1061 | } | 1062 | } |
1062 | 1063 | ||
1063 | bool DateBookDB::saveJournalEntry( const Event &evOld, journal_action action, | 1064 | bool DateBookDB::saveJournalEntry( const Event &evOld, journal_action action, |
1064 | int key, bool origHadRepeat ) | 1065 | int key, bool origHadRepeat ) |
1065 | { | 1066 | { |
1066 | bool status = false; | 1067 | bool status = false; |
1067 | Event ev = evOld; | 1068 | Event ev = evOld; |
1068 | // write our log based on the action | 1069 | // write our log based on the action |
1069 | QFile f( dateBookJournalFile() ); | 1070 | QFile f( dateBookJournalFile() ); |
1070 | if ( !f.open( IO_WriteOnly|IO_Append ) ) | 1071 | if ( !f.open( IO_WriteOnly|IO_Append ) ) |
1071 | return false; | 1072 | return false; |
1072 | QString buf = "<event"; | 1073 | QString buf = "<event"; |
1073 | ev.save( buf ); | 1074 | ev.save( buf ); |
1074 | buf += " action="; | 1075 | buf += " action="; |
1075 | buf += "\"" + QString::number(action) + "\""; | 1076 | buf += "\"" + QString::number(action) + "\""; |
1076 | buf += " actionkey=\"" + QString::number(key) + "\""; | 1077 | buf += " actionkey=\"" + QString::number(key) + "\""; |
1077 | buf += " actionorig=\"" + QString::number(origHadRepeat) +"\""; | 1078 | buf += " actionorig=\"" + QString::number(origHadRepeat) +"\""; |
1078 | buf += " />\n"; | 1079 | buf += " />\n"; |
1079 | QString str = buf.utf8(); | 1080 | QString str = buf.utf8(); |
1080 | status = ( f.writeBlock( str.data(), str.length() ) == int(str.length()) ); | 1081 | status = ( f.writeBlock( str.data(), str.length() ) == int(str.length()) ); |
1081 | f.close(); | 1082 | f.close(); |
1082 | return status; | 1083 | return status; |
1083 | } | 1084 | } |
1084 | 1085 | ||
1085 | QValueList<Event> DateBookDB::getRawRepeats() const | 1086 | QValueList<Event> DateBookDB::getRawRepeats() const |
1086 | { | 1087 | { |
1087 | return repeatEvents; | 1088 | return repeatEvents; |
1088 | } | 1089 | } |
1089 | 1090 | ||
1090 | QValueList<Event> DateBookDB::getNonRepeatingEvents( const QDate &from, | 1091 | QValueList<Event> DateBookDB::getNonRepeatingEvents( const QDate &from, |
1091 | const QDate &to ) const | 1092 | const QDate &to ) const |
1092 | { | 1093 | { |
1093 | QValueListConstIterator<Event> it; | 1094 | QValueListConstIterator<Event> it; |
1094 | QDateTime dtTmp, dtEnd; | 1095 | QDateTime dtTmp, dtEnd; |
1095 | QValueList<Event> tmpList; | 1096 | QValueList<Event> tmpList; |
1096 | for (it = eventList.begin(); it != eventList.end(); ++it ) { | 1097 | for (it = eventList.begin(); it != eventList.end(); ++it ) { |
1097 | dtTmp = (*it).start(TRUE); | 1098 | dtTmp = (*it).start(TRUE); |
1098 | dtEnd = (*it).end(TRUE); | 1099 | dtEnd = (*it).end(TRUE); |
1099 | 1100 | ||
1100 | if ( dtTmp.date() >= from && dtTmp.date() <= to ) { | 1101 | if ( dtTmp.date() >= from && dtTmp.date() <= to ) { |
1101 | Event e = *it; | 1102 | Event e = *it; |
1102 | if ( dtTmp.date() != dtEnd.date() ) | 1103 | if ( dtTmp.date() != dtEnd.date() ) |
1103 | e.setEnd( QDateTime(dtTmp.date(), QTime(23, 59, 0)) ); | 1104 | e.setEnd( QDateTime(dtTmp.date(), QTime(23, 59, 0)) ); |
1104 | tmpList.append( e ); | 1105 | tmpList.append( e ); |
1105 | } | 1106 | } |
1106 | // we must also check for end date information... | 1107 | // we must also check for end date information... |
1107 | if ( dtEnd.date() != dtTmp.date() && dtEnd.date() >= from ) { | 1108 | if ( dtEnd.date() != dtTmp.date() && dtEnd.date() >= from ) { |
1108 | QDateTime dt = dtTmp.addDays( 1 ); | 1109 | QDateTime dt = dtTmp.addDays( 1 ); |
1109 | dt.setTime( QTime(0, 0, 0) ); | 1110 | dt.setTime( QTime(0, 0, 0) ); |
1110 | QDateTime dtStop; | 1111 | QDateTime dtStop; |
1111 | if ( dtEnd > to ) { | 1112 | if ( dtEnd > to ) { |
1112 | dtStop = to; | 1113 | dtStop = to; |
1113 | } else | 1114 | } else |
1114 | dtStop = dtEnd; | 1115 | dtStop = dtEnd; |
1115 | while ( dt <= dtStop ) { | 1116 | while ( dt <= dtStop ) { |
1116 | Event ev = *it; | 1117 | Event ev = *it; |
1117 | if ( dt >= from ) { | 1118 | if ( dt >= from ) { |
1118 | ev.setStart( QDateTime(dt.date(), QTime(0, 0, 0)) ); | 1119 | ev.setStart( QDateTime(dt.date(), QTime(0, 0, 0)) ); |
1119 | if ( dt.date() == dtEnd.date() ) | 1120 | if ( dt.date() == dtEnd.date() ) |
1120 | ev.setEnd( QDateTime(dt.date(), dtEnd.time()) ); | 1121 | ev.setEnd( QDateTime(dt.date(), dtEnd.time()) ); |