-rw-r--r-- | library/datebookdb.cpp | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/library/datebookdb.cpp b/library/datebookdb.cpp index a26fe8f..0fedfa8 100644 --- a/library/datebookdb.cpp +++ b/library/datebookdb.cpp | |||
@@ -588,513 +588,518 @@ QValueList<EffectiveEvent> DateBookDB::getEffectiveEvents( const QDateTime &dt) | |||
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 | // hack to get rid of segfaults after reading </DATEBOOK> | 736 | // hack to get rid of segfaults after reading </DATEBOOK> |
737 | while ( (dt+i != 0) && (( point = strstr( dt+i, "<event " ) ) != 0 )) { | 737 | while ( (dt+i != 0) && (( point = strstr( dt+i, "<event " ) ) != 0 )) { |
738 | i = point - dt; | 738 | i = point - dt; |
739 | // if we are reading in events in the general case, | 739 | // if we are reading in events in the general case, |
740 | // we are just adding them, so let the actions represent that... | 740 | // we are just adding them, so let the actions represent that... |
741 | currentAction = ACTION_ADD; | 741 | currentAction = ACTION_ADD; |
742 | journalKey = -1; | 742 | journalKey = -1; |
743 | origHadRepeat = -1; | 743 | origHadRepeat = -1; |
744 | // some temporary variables for dates and times ... | 744 | // some temporary variables for dates and times ... |
745 | //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; |
746 | //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; |
747 | //int enddtY = 0, enddtM = 0, enddtD = 0; | 747 | //int enddtY = 0, enddtM = 0, enddtD = 0; |
748 | 748 | ||
749 | // ... for the alarm settings ... | 749 | // ... for the alarm settings ... |
750 | int alarmTime = -1; Event::SoundTypeChoice alarmSound = Event::Silent; | 750 | int alarmTime = -1; Event::SoundTypeChoice alarmSound = Event::Silent; |
751 | // ... and for the recurrence | 751 | // ... and for the recurrence |
752 | Event::RepeatPattern rp; | 752 | Event::RepeatPattern rp; |
753 | Event e; | 753 | Event e; |
754 | 754 | ||
755 | i += 7; | 755 | i += 7; |
756 | 756 | ||
757 | while( 1 ) { | 757 | while( 1 ) { |
758 | while ( i < len && (dt[i] == ' ' || dt[i] == '\n' || dt[i] == '\r') ) | 758 | while ( i < len && (dt[i] == ' ' || dt[i] == '\n' || dt[i] == '\r') ) |
759 | ++i; | 759 | ++i; |
760 | if ( i >= len-2 || (dt[i] == '/' && dt[i+1] == '>') ) | 760 | if ( i >= len-2 || (dt[i] == '/' && dt[i+1] == '>') ) |
761 | break; | 761 | break; |
762 | // we have another attribute, read it. | 762 | // we have another attribute, read it. |
763 | int j = i; | 763 | int j = i; |
764 | while ( j < len && dt[j] != '=' ) | 764 | while ( j < len && dt[j] != '=' ) |
765 | ++j; | 765 | ++j; |
766 | char *attr = dt+i; | 766 | char *attr = dt+i; |
767 | dt[j] = '\0'; | 767 | dt[j] = '\0'; |
768 | i = ++j; // skip = | 768 | i = ++j; // skip = |
769 | while ( i < len && dt[i] != '"' ) | 769 | while ( i < len && dt[i] != '"' ) |
770 | ++i; | 770 | ++i; |
771 | j = ++i; | 771 | j = ++i; |
772 | bool haveAmp = FALSE; | 772 | bool haveAmp = FALSE; |
773 | bool haveUtf = FALSE; | 773 | bool haveUtf = FALSE; |
774 | while ( j < len && dt[j] != '"' ) { | 774 | while ( j < len && dt[j] != '"' ) { |
775 | if ( dt[j] == '&' ) | 775 | if ( dt[j] == '&' ) |
776 | haveAmp = TRUE; | 776 | haveAmp = TRUE; |
777 | if ( ((unsigned char)dt[j]) > 0x7f ) | 777 | if ( ((unsigned char)dt[j]) > 0x7f ) |
778 | haveUtf = TRUE; | 778 | haveUtf = TRUE; |
779 | ++j; | 779 | ++j; |
780 | } | 780 | } |
781 | 781 | ||
782 | if ( i == j ) { | 782 | if ( i == j ) { |
783 | // leave out empty attributes | 783 | // leave out empty attributes |
784 | i = j + 1; | 784 | i = j + 1; |
785 | continue; | 785 | continue; |
786 | } | 786 | } |
787 | 787 | ||
788 | QString value = haveUtf ? QString::fromUtf8( dt+i, j-i ) | 788 | QString value = haveUtf ? QString::fromUtf8( dt+i, j-i ) |
789 | : QString::fromLatin1( dt+i, j-i ); | 789 | : QString::fromLatin1( dt+i, j-i ); |
790 | if ( haveAmp ) | 790 | if ( haveAmp ) |
791 | value = Qtopia::plainString( value ); | 791 | value = Qtopia::plainString( value ); |
792 | i = j + 1; | 792 | i = j + 1; |
793 | 793 | ||
794 | //qDebug("attr='%s' value='%s'", attr.data(), value.latin1() ); | 794 | //qDebug("attr='%s' value='%s'", attr.data(), value.latin1() ); |
795 | int * find = dict[ attr ]; | 795 | int * find = dict[ attr ]; |
796 | #if 1 | 796 | #if 1 |
797 | if ( !find ) { | 797 | if ( !find ) { |
798 | // custom field | 798 | // custom field |
799 | e.setCustomField(attr, value); | 799 | e.setCustomField(attr, value); |
800 | continue; | 800 | continue; |
801 | } | 801 | } |
802 | 802 | ||
803 | switch( *find ) { | 803 | switch( *find ) { |
804 | case FDescription: | 804 | case FDescription: |
805 | e.setDescription( value ); | 805 | e.setDescription( value ); |
806 | break; | 806 | break; |
807 | case FLocation: | 807 | case FLocation: |
808 | e.setLocation( value ); | 808 | e.setLocation( value ); |
809 | break; | 809 | break; |
810 | case FCategories: | 810 | case FCategories: |
811 | e.setCategories( Qtopia::Record::idsFromString( value ) ); | 811 | e.setCategories( Qtopia::Record::idsFromString( value ) ); |
812 | break; | 812 | break; |
813 | case FUid: | 813 | case FUid: |
814 | e.setUid( value.toInt() ); | 814 | e.setUid( value.toInt() ); |
815 | break; | 815 | break; |
816 | case FType: | 816 | case FType: |
817 | if ( value == "AllDay" ) | 817 | if ( value == "AllDay" ) |
818 | e.setType( Event::AllDay ); | 818 | e.setType( Event::AllDay ); |
819 | else | 819 | else |
820 | e.setType( Event::Normal ); | 820 | e.setType( Event::Normal ); |
821 | break; | 821 | break; |
822 | case FAlarm: | 822 | case FAlarm: |
823 | alarmTime = value.toInt(); | 823 | alarmTime = value.toInt(); |
824 | break; | 824 | break; |
825 | case FSound: | 825 | case FSound: |
826 | alarmSound = value == "loud" ? Event::Loud : Event::Silent; | 826 | alarmSound = value == "loud" ? Event::Loud : Event::Silent; |
827 | break; | 827 | break; |
828 | // recurrence stuff | 828 | // recurrence stuff |
829 | case FRType: | 829 | case FRType: |
830 | if ( value == "Daily" ) | 830 | if ( value == "Daily" ) |
831 | rp.type = Event::Daily; | 831 | rp.type = Event::Daily; |
832 | else if ( value == "Weekly" ) | 832 | else if ( value == "Weekly" ) |
833 | rp.type = Event::Weekly; | 833 | rp.type = Event::Weekly; |
834 | else if ( value == "MonthlyDay" ) | 834 | else if ( value == "MonthlyDay" ) |
835 | rp.type = Event::MonthlyDay; | 835 | rp.type = Event::MonthlyDay; |
836 | else if ( value == "MonthlyDate" ) | 836 | else if ( value == "MonthlyDate" ) |
837 | rp.type = Event::MonthlyDate; | 837 | rp.type = Event::MonthlyDate; |
838 | else if ( value == "Yearly" ) | 838 | else if ( value == "Yearly" ) |
839 | rp.type = Event::Yearly; | 839 | rp.type = Event::Yearly; |
840 | else | 840 | else |
841 | rp.type = Event::NoRepeat; | 841 | rp.type = Event::NoRepeat; |
842 | break; | 842 | break; |
843 | case FRWeekdays: | 843 | case FRWeekdays: |
844 | rp.days = value.toInt(); | 844 | // QtopiaDesktop 1.6 sometimes creates 'rweekdays="0"' |
845 | // when it goes mad. This causes datebook to crash.. (se) | ||
846 | if ( value.toInt() != 0 ) | ||
847 | rp.days = value.toInt(); | ||
848 | else | ||
849 | rp.days = 1; | ||
845 | break; | 850 | break; |
846 | case FRPosition: | 851 | case FRPosition: |
847 | rp.position = value.toInt(); | 852 | rp.position = value.toInt(); |
848 | break; | 853 | break; |
849 | case FRFreq: | 854 | case FRFreq: |
850 | rp.frequency = value.toInt(); | 855 | rp.frequency = value.toInt(); |
851 | break; | 856 | break; |
852 | case FRHasEndDate: | 857 | case FRHasEndDate: |
853 | rp.hasEndDate = value.toInt(); | 858 | rp.hasEndDate = value.toInt(); |
854 | break; | 859 | break; |
855 | case FREndDate: { | 860 | case FREndDate: { |
856 | rp.endDateUTC = (time_t) value.toLong(); | 861 | rp.endDateUTC = (time_t) value.toLong(); |
857 | break; | 862 | break; |
858 | } | 863 | } |
859 | case FRStart: { | 864 | case FRStart: { |
860 | e.setStart( (time_t) value.toLong() ); | 865 | e.setStart( (time_t) value.toLong() ); |
861 | break; | 866 | break; |
862 | } | 867 | } |
863 | case FREnd: { | 868 | case FREnd: { |
864 | e.setEnd( (time_t) value.toLong() ); | 869 | e.setEnd( (time_t) value.toLong() ); |
865 | break; | 870 | break; |
866 | } | 871 | } |
867 | case FNote: | 872 | case FNote: |
868 | e.setNotes( value ); | 873 | e.setNotes( value ); |
869 | break; | 874 | break; |
870 | case FCreated: | 875 | case FCreated: |
871 | rp.createTime = value.toInt(); | 876 | rp.createTime = value.toInt(); |
872 | break; | 877 | break; |
873 | case FAction: | 878 | case FAction: |
874 | currentAction = value.toInt(); | 879 | currentAction = value.toInt(); |
875 | break; | 880 | break; |
876 | case FActionKey: | 881 | case FActionKey: |
877 | journalKey = value.toInt(); | 882 | journalKey = value.toInt(); |
878 | break; | 883 | break; |
879 | case FJournalOrigHadRepeat: | 884 | case FJournalOrigHadRepeat: |
880 | origHadRepeat = value.toInt(); | 885 | origHadRepeat = value.toInt(); |
881 | break; | 886 | break; |
882 | default: | 887 | default: |
883 | qDebug( "huh??? missing enum? -- attr.: %s", attr ); | 888 | qDebug( "huh??? missing enum? -- attr.: %s", attr ); |
884 | break; | 889 | break; |
885 | } | 890 | } |
886 | #endif | 891 | #endif |
887 | } | 892 | } |
888 | // "post processing" (dates, times, alarm, recurrence) | 893 | // "post processing" (dates, times, alarm, recurrence) |
889 | // start date/time | 894 | // start date/time |
890 | e.setRepeat( rp.type != Event::NoRepeat, rp ); | 895 | e.setRepeat( rp.type != Event::NoRepeat, rp ); |
891 | 896 | ||
892 | if ( alarmTime != -1 ) | 897 | if ( alarmTime != -1 ) |
893 | e.setAlarm( TRUE, alarmTime, alarmSound ); | 898 | e.setAlarm( TRUE, alarmTime, alarmSound ); |
894 | 899 | ||
895 | // now do our action based on the current action... | 900 | // now do our action based on the current action... |
896 | switch ( currentAction ) { | 901 | switch ( currentAction ) { |
897 | case ACTION_ADD: | 902 | case ACTION_ADD: |
898 | addJFEvent( e ); | 903 | addJFEvent( e ); |
899 | break; | 904 | break; |
900 | case ACTION_REMOVE: | 905 | case ACTION_REMOVE: |
901 | removeJFEvent( e ); | 906 | removeJFEvent( e ); |
902 | break; | 907 | break; |
903 | case ACTION_REPLACE: | 908 | case ACTION_REPLACE: |
904 | // be a little bit careful, | 909 | // be a little bit careful, |
905 | // in case of a messed up journal... | 910 | // in case of a messed up journal... |
906 | if ( journalKey > -1 && origHadRepeat > -1 ) { | 911 | if ( journalKey > -1 && origHadRepeat > -1 ) { |
907 | // get the original from proper list... | 912 | // get the original from proper list... |
908 | if ( origHadRepeat ) | 913 | if ( origHadRepeat ) |
909 | removeJFEvent( *(repeatEvents.at(journalKey)) ); | 914 | removeJFEvent( *(repeatEvents.at(journalKey)) ); |
910 | else | 915 | else |
911 | removeJFEvent( *(eventList.at(journalKey)) ); | 916 | removeJFEvent( *(eventList.at(journalKey)) ); |
912 | addJFEvent( e ); | 917 | addJFEvent( e ); |
913 | } | 918 | } |
914 | break; | 919 | break; |
915 | default: | 920 | default: |
916 | break; | 921 | break; |
917 | } | 922 | } |
918 | } | 923 | } |
919 | f.close(); | 924 | f.close(); |
920 | } | 925 | } |
921 | 926 | ||
922 | void DateBookDB::init() | 927 | void DateBookDB::init() |
923 | { | 928 | { |
924 | d = new DateBookDBPrivate; | 929 | d = new DateBookDBPrivate; |
925 | d->clean = false; | 930 | d->clean = false; |
926 | QString str = dateBookFilename(); | 931 | QString str = dateBookFilename(); |
927 | if ( str.isNull() ) { | 932 | if ( str.isNull() ) { |
928 | QMessageBox::warning( 0, QObject::tr("Out of Space"), | 933 | QMessageBox::warning( 0, QObject::tr("Out of Space"), |
929 | QObject::tr("Unable to create start up files\n" | 934 | QObject::tr("Unable to create start up files\n" |
930 | "Please free up some space\n" | 935 | "Please free up some space\n" |
931 | "before entering data") ); | 936 | "before entering data") ); |
932 | } | 937 | } |
933 | // continuing along, we call this datebook filename again, | 938 | // continuing along, we call this datebook filename again, |
934 | // because they may fix it before continuing, though it seems | 939 | // because they may fix it before continuing, though it seems |
935 | // pretty unlikely... | 940 | // pretty unlikely... |
936 | loadFile( dateBookFilename() ); | 941 | loadFile( dateBookFilename() ); |
937 | 942 | ||
938 | if ( QFile::exists( dateBookJournalFile() ) ) { | 943 | if ( QFile::exists( dateBookJournalFile() ) ) { |
939 | // merge the journal | 944 | // merge the journal |
940 | loadFile( dateBookJournalFile() ); | 945 | loadFile( dateBookJournalFile() ); |
941 | // save in our changes and remove the journal... | 946 | // save in our changes and remove the journal... |
942 | save(); | 947 | save(); |
943 | } | 948 | } |
944 | d->clean = true; | 949 | d->clean = true; |
945 | } | 950 | } |
946 | 951 | ||
947 | bool DateBookDB::save() | 952 | bool DateBookDB::save() |
948 | { | 953 | { |
949 | if ( d->clean == true ) | 954 | if ( d->clean == true ) |
950 | return true; | 955 | return true; |
951 | QValueListIterator<Event> it; | 956 | QValueListIterator<Event> it; |
952 | int total_written; | 957 | int total_written; |
953 | QString strFileNew = dateBookFilename() + ".new"; | 958 | QString strFileNew = dateBookFilename() + ".new"; |
954 | 959 | ||
955 | QFile f( strFileNew ); | 960 | QFile f( strFileNew ); |
956 | if ( !f.open( IO_WriteOnly|IO_Raw ) ) | 961 | if ( !f.open( IO_WriteOnly|IO_Raw ) ) |
957 | return FALSE; | 962 | return FALSE; |
958 | 963 | ||
959 | QString buf( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ); | 964 | QString buf( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ); |
960 | buf += "<!DOCTYPE DATEBOOK><DATEBOOK>\n"; | 965 | buf += "<!DOCTYPE DATEBOOK><DATEBOOK>\n"; |
961 | buf += "<events>\n"; | 966 | buf += "<events>\n"; |
962 | QCString str = buf.utf8(); | 967 | QCString str = buf.utf8(); |
963 | total_written = f.writeBlock( str.data(), str.length() ); | 968 | total_written = f.writeBlock( str.data(), str.length() ); |
964 | if ( total_written != int(str.length()) ) { | 969 | if ( total_written != int(str.length()) ) { |
965 | f.close(); | 970 | f.close(); |
966 | QFile::remove( strFileNew ); | 971 | QFile::remove( strFileNew ); |
967 | return false; | 972 | return false; |
968 | } | 973 | } |
969 | 974 | ||
970 | for ( it = eventList.begin(); it != eventList.end(); ++it ) { | 975 | for ( it = eventList.begin(); it != eventList.end(); ++it ) { |
971 | buf = "<event"; | 976 | buf = "<event"; |
972 | (*it).save( buf ); | 977 | (*it).save( buf ); |
973 | buf += " />\n"; | 978 | buf += " />\n"; |
974 | str = buf.utf8(); | 979 | str = buf.utf8(); |
975 | total_written = f.writeBlock( str.data(), str.length() ); | 980 | total_written = f.writeBlock( str.data(), str.length() ); |
976 | if ( total_written != int(str.length()) ) { | 981 | if ( total_written != int(str.length()) ) { |
977 | f.close(); | 982 | f.close(); |
978 | QFile::remove( strFileNew ); | 983 | QFile::remove( strFileNew ); |
979 | return false; | 984 | return false; |
980 | } | 985 | } |
981 | } | 986 | } |
982 | for ( it = repeatEvents.begin(); it != repeatEvents.end(); ++it ) { | 987 | for ( it = repeatEvents.begin(); it != repeatEvents.end(); ++it ) { |
983 | buf = "<event"; | 988 | buf = "<event"; |
984 | (*it).save( buf ); | 989 | (*it).save( buf ); |
985 | buf += " />\n"; | 990 | buf += " />\n"; |
986 | str = buf.utf8(); | 991 | str = buf.utf8(); |
987 | total_written = f.writeBlock( str.data(), str.length() ); | 992 | total_written = f.writeBlock( str.data(), str.length() ); |
988 | if ( total_written != int(str.length()) ) { | 993 | if ( total_written != int(str.length()) ) { |
989 | f.close(); | 994 | f.close(); |
990 | QFile::remove( strFileNew ); | 995 | QFile::remove( strFileNew ); |
991 | return false; | 996 | return false; |
992 | } | 997 | } |
993 | } | 998 | } |
994 | buf = "</events>\n</DATEBOOK>\n"; | 999 | buf = "</events>\n</DATEBOOK>\n"; |
995 | str = buf.utf8(); | 1000 | str = buf.utf8(); |
996 | total_written = f.writeBlock( str.data(), str.length() ); | 1001 | total_written = f.writeBlock( str.data(), str.length() ); |
997 | if ( total_written != int(str.length()) ) { | 1002 | if ( total_written != int(str.length()) ) { |
998 | f.close(); | 1003 | f.close(); |
999 | QFile::remove( strFileNew ); | 1004 | QFile::remove( strFileNew ); |
1000 | return false; | 1005 | return false; |
1001 | } | 1006 | } |
1002 | f.close(); | 1007 | f.close(); |
1003 | 1008 | ||
1004 | // now rename... I like to use the systemcall | 1009 | // now rename... I like to use the systemcall |
1005 | if ( ::rename( strFileNew, dateBookFilename() ) < 0 ) { | 1010 | if ( ::rename( strFileNew, dateBookFilename() ) < 0 ) { |
1006 | qWarning( "problem renaming file %s to %s errno %d", | 1011 | qWarning( "problem renaming file %s to %s errno %d", |
1007 | strFileNew.latin1(), dateBookFilename().latin1(), errno ); | 1012 | strFileNew.latin1(), dateBookFilename().latin1(), errno ); |
1008 | // remove the file, otherwise it will just stick around... | 1013 | // remove the file, otherwise it will just stick around... |
1009 | QFile::remove( strFileNew ); | 1014 | QFile::remove( strFileNew ); |
1010 | } | 1015 | } |
1011 | 1016 | ||
1012 | // may as well remove the journal file... | 1017 | // may as well remove the journal file... |
1013 | QFile::remove( dateBookJournalFile() ); | 1018 | QFile::remove( dateBookJournalFile() ); |
1014 | d->clean = true; | 1019 | d->clean = true; |
1015 | return true; | 1020 | return true; |
1016 | } | 1021 | } |
1017 | 1022 | ||
1018 | void DateBookDB::reload() | 1023 | void DateBookDB::reload() |
1019 | { | 1024 | { |
1020 | QValueList<Event>::Iterator it = eventList.begin(); | 1025 | QValueList<Event>::Iterator it = eventList.begin(); |
1021 | for ( ; it != eventList.end(); ++it ) { | 1026 | for ( ; it != eventList.end(); ++it ) { |
1022 | if ( (*it).hasAlarm() ) | 1027 | if ( (*it).hasAlarm() ) |
1023 | delEventAlarm( *it ); | 1028 | delEventAlarm( *it ); |
1024 | if ( (*it).hasRepeat() ) | 1029 | if ( (*it).hasRepeat() ) |
1025 | removeRepeat( *it ); | 1030 | removeRepeat( *it ); |
1026 | } | 1031 | } |
1027 | eventList.clear(); | 1032 | eventList.clear(); |
1028 | repeatEvents.clear(); // should be a NOP | 1033 | repeatEvents.clear(); // should be a NOP |
1029 | init(); | 1034 | init(); |
1030 | } | 1035 | } |
1031 | 1036 | ||
1032 | bool DateBookDB::removeRepeat( const Event &ev ) | 1037 | bool DateBookDB::removeRepeat( const Event &ev ) |
1033 | { | 1038 | { |
1034 | time_t removeMe = ev.repeatPattern().createTime; | 1039 | time_t removeMe = ev.repeatPattern().createTime; |
1035 | QValueListIterator<Event> it; | 1040 | QValueListIterator<Event> it; |
1036 | for ( it = repeatEvents.begin(); it != repeatEvents.end(); ++it ) { | 1041 | for ( it = repeatEvents.begin(); it != repeatEvents.end(); ++it ) { |
1037 | if ( removeMe == (*it).repeatPattern().createTime ) { | 1042 | if ( removeMe == (*it).repeatPattern().createTime ) { |
1038 | repeatEvents.remove( *it ); | 1043 | repeatEvents.remove( *it ); |
1039 | // best break, or we are going into undefined territory! | 1044 | // best break, or we are going into undefined territory! |
1040 | return TRUE; | 1045 | return TRUE; |
1041 | } | 1046 | } |
1042 | } | 1047 | } |
1043 | return FALSE; | 1048 | return FALSE; |
1044 | } | 1049 | } |
1045 | 1050 | ||
1046 | bool DateBookDB::origRepeat( const Event &ev, Event &orig ) const | 1051 | bool DateBookDB::origRepeat( const Event &ev, Event &orig ) const |
1047 | { | 1052 | { |
1048 | time_t removeMe = ev.repeatPattern().createTime; | 1053 | time_t removeMe = ev.repeatPattern().createTime; |
1049 | QValueListConstIterator<Event> it; | 1054 | QValueListConstIterator<Event> it; |
1050 | for ( it = repeatEvents.begin(); it != repeatEvents.end(); ++it ) { | 1055 | for ( it = repeatEvents.begin(); it != repeatEvents.end(); ++it ) { |
1051 | if ( removeMe == (*it).repeatPattern().createTime ) { | 1056 | if ( removeMe == (*it).repeatPattern().createTime ) { |
1052 | orig = (*it); | 1057 | orig = (*it); |
1053 | return TRUE; | 1058 | return TRUE; |
1054 | } | 1059 | } |
1055 | } | 1060 | } |
1056 | return FALSE; | 1061 | return FALSE; |
1057 | } | 1062 | } |
1058 | 1063 | ||
1059 | void DateBookDB::saveJournalEntry( const Event &ev, journal_action action ) | 1064 | void DateBookDB::saveJournalEntry( const Event &ev, journal_action action ) |
1060 | { | 1065 | { |
1061 | saveJournalEntry( ev, action, -1, false ); | 1066 | saveJournalEntry( ev, action, -1, false ); |
1062 | } | 1067 | } |
1063 | 1068 | ||
1064 | bool DateBookDB::saveJournalEntry( const Event &evOld, journal_action action, | 1069 | bool DateBookDB::saveJournalEntry( const Event &evOld, journal_action action, |
1065 | int key, bool origHadRepeat ) | 1070 | int key, bool origHadRepeat ) |
1066 | { | 1071 | { |
1067 | bool status = false; | 1072 | bool status = false; |
1068 | Event ev = evOld; | 1073 | Event ev = evOld; |
1069 | // write our log based on the action | 1074 | // write our log based on the action |
1070 | QFile f( dateBookJournalFile() ); | 1075 | QFile f( dateBookJournalFile() ); |
1071 | if ( !f.open( IO_WriteOnly|IO_Append ) ) | 1076 | if ( !f.open( IO_WriteOnly|IO_Append ) ) |
1072 | return false; | 1077 | return false; |
1073 | QString buf = "<event"; | 1078 | QString buf = "<event"; |
1074 | ev.save( buf ); | 1079 | ev.save( buf ); |
1075 | buf += " action="; | 1080 | buf += " action="; |
1076 | buf += "\"" + QString::number(action) + "\""; | 1081 | buf += "\"" + QString::number(action) + "\""; |
1077 | buf += " actionkey=\"" + QString::number(key) + "\""; | 1082 | buf += " actionkey=\"" + QString::number(key) + "\""; |
1078 | buf += " actionorig=\"" + QString::number(origHadRepeat) +"\""; | 1083 | buf += " actionorig=\"" + QString::number(origHadRepeat) +"\""; |
1079 | buf += " />\n"; | 1084 | buf += " />\n"; |
1080 | QString str = buf.utf8(); | 1085 | QString str = buf.utf8(); |
1081 | status = ( f.writeBlock( str.data(), str.length() ) == int(str.length()) ); | 1086 | status = ( f.writeBlock( str.data(), str.length() ) == int(str.length()) ); |
1082 | f.close(); | 1087 | f.close(); |
1083 | return status; | 1088 | return status; |
1084 | } | 1089 | } |
1085 | 1090 | ||
1086 | QValueList<Event> DateBookDB::getRawRepeats() const | 1091 | QValueList<Event> DateBookDB::getRawRepeats() const |
1087 | { | 1092 | { |
1088 | return repeatEvents; | 1093 | return repeatEvents; |
1089 | } | 1094 | } |
1090 | 1095 | ||
1091 | QValueList<Event> DateBookDB::getNonRepeatingEvents( const QDate &from, | 1096 | QValueList<Event> DateBookDB::getNonRepeatingEvents( const QDate &from, |
1092 | const QDate &to ) const | 1097 | const QDate &to ) const |
1093 | { | 1098 | { |
1094 | QValueListConstIterator<Event> it; | 1099 | QValueListConstIterator<Event> it; |
1095 | QDateTime dtTmp, dtEnd; | 1100 | QDateTime dtTmp, dtEnd; |
1096 | QValueList<Event> tmpList; | 1101 | QValueList<Event> tmpList; |
1097 | for (it = eventList.begin(); it != eventList.end(); ++it ) { | 1102 | for (it = eventList.begin(); it != eventList.end(); ++it ) { |
1098 | dtTmp = (*it).start(TRUE); | 1103 | dtTmp = (*it).start(TRUE); |
1099 | dtEnd = (*it).end(TRUE); | 1104 | dtEnd = (*it).end(TRUE); |
1100 | 1105 | ||