summaryrefslogtreecommitdiffabout
path: root/libkcal/recurrence.cpp
Unidiff
Diffstat (limited to 'libkcal/recurrence.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--libkcal/recurrence.cpp34
1 files changed, 11 insertions, 23 deletions
diff --git a/libkcal/recurrence.cpp b/libkcal/recurrence.cpp
index 8a175c9..6ee5499 100644
--- a/libkcal/recurrence.cpp
+++ b/libkcal/recurrence.cpp
@@ -723,825 +723,822 @@ void Recurrence::setYearly(int type, int _rFreq, int _rDuration)
723 return; 723 return;
724 if (mCompatVersion < 310) 724 if (mCompatVersion < 310)
725 mCompatDuration = (_rDuration > 0) ? _rDuration : 0; 725 mCompatDuration = (_rDuration > 0) ? _rDuration : 0;
726 setYearly_(type, mFeb29YearlyDefaultType, _rFreq, _rDuration); 726 setYearly_(type, mFeb29YearlyDefaultType, _rFreq, _rDuration);
727} 727}
728 728
729void Recurrence::setYearly(int type, int _rFreq, const QDate &_rEndDate) 729void Recurrence::setYearly(int type, int _rFreq, const QDate &_rEndDate)
730{ 730{
731 if (mRecurReadOnly) return; 731 if (mRecurReadOnly) return;
732 rEndDateTime.setDate(_rEndDate); 732 rEndDateTime.setDate(_rEndDate);
733 rEndDateTime.setTime(mRecurStart.time()); 733 rEndDateTime.setTime(mRecurStart.time());
734 mCompatDuration = 0; 734 mCompatDuration = 0;
735 setYearly_(type, mFeb29YearlyDefaultType, _rFreq, 0); 735 setYearly_(type, mFeb29YearlyDefaultType, _rFreq, 0);
736} 736}
737 737
738void Recurrence::setYearlyByDate(Feb29Type type, int _rFreq, int _rDuration) 738void Recurrence::setYearlyByDate(Feb29Type type, int _rFreq, int _rDuration)
739{ 739{
740 if (mRecurReadOnly || _rDuration == 0 || _rDuration < -1) 740 if (mRecurReadOnly || _rDuration == 0 || _rDuration < -1)
741 return; 741 return;
742 if (mCompatVersion < 310) 742 if (mCompatVersion < 310)
743 mCompatDuration = (_rDuration > 0) ? _rDuration : 0; 743 mCompatDuration = (_rDuration > 0) ? _rDuration : 0;
744 setYearly_(rYearlyMonth, type, _rFreq, _rDuration); 744 setYearly_(rYearlyMonth, type, _rFreq, _rDuration);
745} 745}
746 746
747void Recurrence::setYearlyByDate(Feb29Type type, int _rFreq, const QDate &_rEndDate) 747void Recurrence::setYearlyByDate(Feb29Type type, int _rFreq, const QDate &_rEndDate)
748{ 748{
749 if (mRecurReadOnly) return; 749 if (mRecurReadOnly) return;
750 rEndDateTime.setDate(_rEndDate); 750 rEndDateTime.setDate(_rEndDate);
751 rEndDateTime.setTime(mRecurStart.time()); 751 rEndDateTime.setTime(mRecurStart.time());
752 mCompatDuration = 0; 752 mCompatDuration = 0;
753 setYearly_(rYearlyMonth, type, _rFreq, 0); 753 setYearly_(rYearlyMonth, type, _rFreq, 0);
754} 754}
755 755
756void Recurrence::addYearlyMonthPos(short _rPos, const QBitArray &_rDays) 756void Recurrence::addYearlyMonthPos(short _rPos, const QBitArray &_rDays)
757{ 757{
758 if (recurs == rYearlyPos) 758 if (recurs == rYearlyPos)
759 addMonthlyPos_(_rPos, _rDays); 759 addMonthlyPos_(_rPos, _rDays);
760} 760}
761 761
762const QPtrList<int> &Recurrence::yearNums() const 762const QPtrList<int> &Recurrence::yearNums() const
763{ 763{
764 return rYearNums; 764 return rYearNums;
765} 765}
766void Recurrence::addYearlyMonth(short _rPos ) 766void Recurrence::addYearlyMonth(short _rPos )
767{ 767{
768 if (mRecurReadOnly || recurs != rYearlyMonth) // invalid day/month number 768 if (mRecurReadOnly || recurs != rYearlyMonth) // invalid day/month number
769 return; 769 return;
770 rMonthPos *tmpPos = new rMonthPos; 770 rMonthPos *tmpPos = new rMonthPos;
771 if ( _rPos > 0) { 771 if ( _rPos > 0) {
772 tmpPos->rPos = _rPos; 772 tmpPos->rPos = _rPos;
773 tmpPos->negative = false; 773 tmpPos->negative = false;
774 } else { 774 } else {
775 tmpPos->rPos = -_rPos; // take abs() 775 tmpPos->rPos = -_rPos; // take abs()
776 tmpPos->negative = true; 776 tmpPos->negative = true;
777 } 777 }
778 rMonthPositions.append(tmpPos); 778 rMonthPositions.append(tmpPos);
779} 779}
780void Recurrence::addYearlyNum(short _rNum) 780void Recurrence::addYearlyNum(short _rNum)
781{ 781{
782 if (mRecurReadOnly 782 if (mRecurReadOnly
783 || (recurs != rYearlyMonth && recurs != rYearlyDay && recurs != rYearlyPos) 783 || (recurs != rYearlyMonth && recurs != rYearlyDay && recurs != rYearlyPos)
784 || _rNum <= 0) // invalid day/month number 784 || _rNum <= 0) // invalid day/month number
785 return; 785 return;
786 786
787 if (mCompatVersion < 310 && mCompatRecurs == rYearlyDay) { 787 if (mCompatVersion < 310 && mCompatRecurs == rYearlyDay) {
788 // Backwards compatibility for KDE < 3.1. 788 // Backwards compatibility for KDE < 3.1.
789 // Dates were stored as day numbers, with a fiddle to take account of leap years. 789 // Dates were stored as day numbers, with a fiddle to take account of leap years.
790 // Convert the day number to a month. 790 // Convert the day number to a month.
791 if (_rNum <= 0 || _rNum > 366 || (_rNum == 366 && mRecurStart.date().daysInYear() < 366)) 791 if (_rNum <= 0 || _rNum > 366 || (_rNum == 366 && mRecurStart.date().daysInYear() < 366))
792 return; // invalid day number 792 return; // invalid day number
793 _rNum = QDate(mRecurStart.date().year(), 1, 1).addDays(_rNum - 1).month(); 793 _rNum = QDate(mRecurStart.date().year(), 1, 1).addDays(_rNum - 1).month();
794 } else 794 } else
795 if ((recurs == rYearlyMonth || recurs == rYearlyPos) && _rNum > 12 795 if ((recurs == rYearlyMonth || recurs == rYearlyPos) && _rNum > 12
796 || recurs == rYearlyDay && _rNum > 366) 796 || recurs == rYearlyDay && _rNum > 366)
797 return; // invalid day number 797 return; // invalid day number
798 798
799 uint i = 0; 799 uint i = 0;
800 for (int* it = rYearNums.first(); it && _rNum >= *it; it = rYearNums.next()) { 800 for (int* it = rYearNums.first(); it && _rNum >= *it; it = rYearNums.next()) {
801 if (_rNum == *it) 801 if (_rNum == *it)
802 return; // this day/month is already in the list - avoid duplication 802 return; // this day/month is already in the list - avoid duplication
803 ++i; 803 ++i;
804 } 804 }
805 805
806 int *tmpNum = new int; 806 int *tmpNum = new int;
807 *tmpNum = _rNum; 807 *tmpNum = _rNum;
808 rYearNums.insert(i, tmpNum); // insert the day/month in a sorted position 808 rYearNums.insert(i, tmpNum); // insert the day/month in a sorted position
809 809
810 if (mCompatVersion < 310 && mCompatDuration > 0) { 810 if (mCompatVersion < 310 && mCompatDuration > 0) {
811 // Backwards compatibility for KDE < 3.1. 811 // Backwards compatibility for KDE < 3.1.
812 // rDuration was set to the number of time periods to recur. 812 // rDuration was set to the number of time periods to recur.
813 // Convert this to the number of occurrences. 813 // Convert this to the number of occurrences.
814 QDate end(mRecurStart.date().year() + (mCompatDuration-1+mRecurExDatesCount)*rFreq, 12, 31); 814 QDate end(mRecurStart.date().year() + (mCompatDuration-1+mRecurExDatesCount)*rFreq, 12, 31);
815 rDuration = INT_MAX; // ensure that recurCalc() does its job correctly 815 rDuration = INT_MAX; // ensure that recurCalc() does its job correctly
816 rDuration = recurCalc(COUNT_TO_DATE, end); 816 rDuration = recurCalc(COUNT_TO_DATE, end);
817 } 817 }
818 818
819 if (mParent) mParent->updated(); 819 if (mParent) mParent->updated();
820} 820}
821 821
822 822
823QDateTime Recurrence::getNextDateTime(const QDateTime &preDateTime, bool *last) const 823QDateTime Recurrence::getNextDateTime(const QDateTime &preDateTime, bool *last) const
824{ 824{
825 if (last) 825 if (last)
826 *last = false; 826 *last = false;
827 int freq; 827 int freq;
828 switch (recurs) 828 switch (recurs)
829 { 829 {
830 case rMinutely: 830 case rMinutely:
831 freq = rFreq * 60; 831 freq = rFreq * 60;
832 break; 832 break;
833 case rHourly: 833 case rHourly:
834 freq = rFreq * 3600; 834 freq = rFreq * 3600;
835 break; 835 break;
836 case rDaily: 836 case rDaily:
837 case rWeekly: 837 case rWeekly:
838 case rMonthlyPos: 838 case rMonthlyPos:
839 case rMonthlyDay: 839 case rMonthlyDay:
840 case rYearlyMonth: 840 case rYearlyMonth:
841 case rYearlyDay: 841 case rYearlyDay:
842 case rYearlyPos: { 842 case rYearlyPos: {
843 QDate preDate = preDateTime.date(); 843 QDate preDate = preDateTime.date();
844 if (!mFloats && mRecurStart.time() > preDateTime.time()) 844 if (!mFloats && mRecurStart.time() > preDateTime.time())
845 preDate = preDate.addDays(-1); 845 preDate = preDate.addDays(-1);
846 return QDateTime(getNextDateNoTime(preDate, last), mRecurStart.time()); 846 return QDateTime(getNextDateNoTime(preDate, last), mRecurStart.time());
847 } 847 }
848 default: 848 default:
849 return QDateTime(); 849 return QDateTime();
850 } 850 }
851 851
852 // It's a sub-daily recurrence 852 // It's a sub-daily recurrence
853 if (preDateTime < mRecurStart) 853 if (preDateTime < mRecurStart)
854 return mRecurStart; 854 return mRecurStart;
855 int count = mRecurStart.secsTo(preDateTime) / freq + 2; 855 int count = mRecurStart.secsTo(preDateTime) / freq + 2;
856 if (rDuration > 0) { 856 if (rDuration > 0) {
857 if (count > rDuration) 857 if (count > rDuration)
858 return QDateTime(); 858 return QDateTime();
859 if (last && count == rDuration) 859 if (last && count == rDuration)
860 *last = true; 860 *last = true;
861 } 861 }
862 QDateTime endtime = mRecurStart.addSecs((count - 1)*freq); 862 QDateTime endtime = mRecurStart.addSecs((count - 1)*freq);
863 if (rDuration == 0) { 863 if (rDuration == 0) {
864 if (endtime > rEndDateTime) 864 if (endtime > rEndDateTime)
865 return QDateTime(); 865 return QDateTime();
866 if (last && endtime == rEndDateTime) 866 if (last && endtime == rEndDateTime)
867 *last = true; 867 *last = true;
868 } 868 }
869 return endtime; 869 return endtime;
870} 870}
871 871
872QDate Recurrence::getNextDate(const QDate &preDate, bool *last) const 872QDate Recurrence::getNextDate(const QDate &preDate, bool *last) const
873{ 873{
874 if (last) 874 if (last)
875 *last = false; 875 *last = false;
876 switch (recurs) 876 switch (recurs)
877 { 877 {
878 case rMinutely: 878 case rMinutely:
879 case rHourly: 879 case rHourly:
880 return getNextDateTime(QDateTime(preDate, QTime(23,59,59)), last).date(); 880 return getNextDateTime(QDateTime(preDate, QTime(23,59,59)), last).date();
881 case rDaily: 881 case rDaily:
882 case rWeekly: 882 case rWeekly:
883 case rMonthlyPos: 883 case rMonthlyPos:
884 case rMonthlyDay: 884 case rMonthlyDay:
885 case rYearlyMonth: 885 case rYearlyMonth:
886 case rYearlyDay: 886 case rYearlyDay:
887 case rYearlyPos: 887 case rYearlyPos:
888 qDebug("Recurrence::getNextDate: MAY BE BROKEN "); 888 qDebug("Recurrence::getNextDate: MAY BE BROKEN ");
889 return getNextDateNoTime(preDate, last); 889 return getNextDateNoTime(preDate, last);
890 default: 890 default:
891 return QDate(); 891 return QDate();
892 } 892 }
893} 893}
894 894
895 895
896QDateTime Recurrence::getPreviousDateTime(const QDateTime &afterDateTime, bool *last) const 896QDateTime Recurrence::getPreviousDateTime(const QDateTime &afterDateTime, bool *last) const
897{ 897{
898 if (last) 898 if (last)
899 *last = false; 899 *last = false;
900 int freq; 900 int freq;
901 switch (recurs) 901 switch (recurs)
902 { 902 {
903 case rMinutely: 903 case rMinutely:
904 freq = rFreq * 60; 904 freq = rFreq * 60;
905 break; 905 break;
906 case rHourly: 906 case rHourly:
907 freq = rFreq * 3600; 907 freq = rFreq * 3600;
908 break; 908 break;
909 case rDaily: 909 case rDaily:
910 case rWeekly: 910 case rWeekly:
911 case rMonthlyPos: 911 case rMonthlyPos:
912 case rMonthlyDay: 912 case rMonthlyDay:
913 case rYearlyMonth: 913 case rYearlyMonth:
914 case rYearlyDay: 914 case rYearlyDay:
915 case rYearlyPos: { 915 case rYearlyPos: {
916 QDate afterDate = afterDateTime.date(); 916 QDate afterDate = afterDateTime.date();
917 if (!mFloats && mRecurStart.time() < afterDateTime.time()) 917 if (!mFloats && mRecurStart.time() < afterDateTime.time())
918 afterDate = afterDate.addDays(1); 918 afterDate = afterDate.addDays(1);
919 return QDateTime(getPreviousDateNoTime(afterDate, last), mRecurStart.time()); 919 return QDateTime(getPreviousDateNoTime(afterDate, last), mRecurStart.time());
920 } 920 }
921 default: 921 default:
922 return QDateTime(); 922 return QDateTime();
923 } 923 }
924 924
925 // It's a sub-daily recurrence 925 // It's a sub-daily recurrence
926 if (afterDateTime <= mRecurStart) 926 if (afterDateTime <= mRecurStart)
927 return QDateTime(); 927 return QDateTime();
928 int count = (mRecurStart.secsTo(afterDateTime) - 1) / freq + 1; 928 int count = (mRecurStart.secsTo(afterDateTime) - 1) / freq + 1;
929 if (rDuration > 0) { 929 if (rDuration > 0) {
930 if (count > rDuration) 930 if (count > rDuration)
931 count = rDuration; 931 count = rDuration;
932 if (last && count == rDuration) 932 if (last && count == rDuration)
933 *last = true; 933 *last = true;
934 } 934 }
935 QDateTime endtime = mRecurStart.addSecs((count - 1)*freq); 935 QDateTime endtime = mRecurStart.addSecs((count - 1)*freq);
936 if (rDuration == 0) { 936 if (rDuration == 0) {
937 if (endtime > rEndDateTime) 937 if (endtime > rEndDateTime)
938 endtime = rEndDateTime; 938 endtime = rEndDateTime;
939 if (last && endtime == rEndDateTime) 939 if (last && endtime == rEndDateTime)
940 *last = true; 940 *last = true;
941 } 941 }
942 return endtime; 942 return endtime;
943} 943}
944 944
945QDate Recurrence::getPreviousDate(const QDate &afterDate, bool *last) const 945QDate Recurrence::getPreviousDate(const QDate &afterDate, bool *last) const
946{ 946{
947 if (last) 947 if (last)
948 *last = false; 948 *last = false;
949 switch (recurs) 949 switch (recurs)
950 { 950 {
951 case rMinutely: 951 case rMinutely:
952 case rHourly: 952 case rHourly:
953 return getPreviousDateTime(QDateTime(afterDate, QTime(0,0,0)), last).date(); 953 return getPreviousDateTime(QDateTime(afterDate, QTime(0,0,0)), last).date();
954 case rDaily: 954 case rDaily:
955 case rWeekly: 955 case rWeekly:
956 case rMonthlyPos: 956 case rMonthlyPos:
957 case rMonthlyDay: 957 case rMonthlyDay:
958 case rYearlyMonth: 958 case rYearlyMonth:
959 case rYearlyDay: 959 case rYearlyDay:
960 case rYearlyPos: 960 case rYearlyPos:
961 return getPreviousDateNoTime(afterDate, last); 961 return getPreviousDateNoTime(afterDate, last);
962 default: 962 default:
963 return QDate(); 963 return QDate();
964 } 964 }
965} 965}
966 966
967 967
968/***************************** PROTECTED FUNCTIONS ***************************/ 968/***************************** PROTECTED FUNCTIONS ***************************/
969 969
970bool Recurrence::recursSecondly(const QDate &qd, int secondFreq) const 970bool Recurrence::recursSecondly(const QDate &qd, int secondFreq) const
971{ 971{
972 if ((qd >= mRecurStart.date()) && 972 if ((qd >= mRecurStart.date()) &&
973 ((rDuration > 0) && (qd <= endDate()) || 973 ((rDuration > 0) && (qd <= endDate()) ||
974 ((rDuration == 0) && (qd <= rEndDateTime.date())) || 974 ((rDuration == 0) && (qd <= rEndDateTime.date())) ||
975 (rDuration == -1))) { 975 (rDuration == -1))) {
976 // The date queried falls within the range of the event. 976 // The date queried falls within the range of the event.
977 if (secondFreq < 24*3600) 977 if (secondFreq < 24*3600)
978 return true; // the event recurs at least once each day 978 return true; // the event recurs at least once each day
979 int after = mRecurStart.secsTo(QDateTime(qd)); 979 int after = mRecurStart.secsTo(QDateTime(qd)) - 1;
980 if (after / secondFreq != (after + 24*3600) / secondFreq) 980 if (after / secondFreq != (after + 24*3600) / secondFreq)
981 return true; 981 return true;
982 } 982 }
983 return false; 983 return false;
984} 984}
985 985
986bool Recurrence::recursMinutelyAt(const QDateTime &dt, int minuteFreq) const 986bool Recurrence::recursMinutelyAt(const QDateTime &dt, int minuteFreq) const
987{ 987{
988 if ((dt >= mRecurStart) && 988 if ((dt >= mRecurStart) &&
989 ((rDuration > 0) && (dt <= endDateTime()) || 989 ((rDuration > 0) && (dt <= endDateTime()) ||
990 ((rDuration == 0) && (dt <= rEndDateTime)) || 990 ((rDuration == 0) && (dt <= rEndDateTime)) ||
991 (rDuration == -1))) { 991 (rDuration == -1))) {
992 // The time queried falls within the range of the event. 992 // The time queried falls within the range of the event.
993 if (((mRecurStart.secsTo(dt) / 60) % minuteFreq) == 0) 993 if (((mRecurStart.secsTo(dt) / 60) % minuteFreq) == 0)
994 return true; 994 return true;
995 } 995 }
996 return false; 996 return false;
997} 997}
998 998
999bool Recurrence::recursDaily(const QDate &qd) const 999bool Recurrence::recursDaily(const QDate &qd) const
1000{ 1000{
1001 QDate dStart = mRecurStart.date(); 1001 QDate dStart = mRecurStart.date();
1002 if ((dStart.daysTo(qd) % rFreq) == 0) { 1002 if ((dStart.daysTo(qd) % rFreq) == 0) {
1003 // The date is a day which recurs 1003 // The date is a day which recurs
1004 if (qd >= dStart 1004 if (qd >= dStart
1005 && ((rDuration > 0 && qd <= endDate()) || 1005 && ((rDuration > 0 && qd <= endDate()) ||
1006 (rDuration == 0 && qd <= rEndDateTime.date()) || 1006 (rDuration == 0 && qd <= rEndDateTime.date()) ||
1007 rDuration == -1)) { 1007 rDuration == -1)) {
1008 // The date queried falls within the range of the event. 1008 // The date queried falls within the range of the event.
1009 return true; 1009 return true;
1010 } 1010 }
1011 } 1011 }
1012 return false; 1012 return false;
1013} 1013}
1014 1014
1015bool Recurrence::recursWeekly(const QDate &qd) const 1015bool Recurrence::recursWeekly(const QDate &qd) const
1016{ 1016{
1017 QDate dStart = mRecurStart.date(); 1017 QDate dStart = mRecurStart.date();
1018 if ((dStart.daysTo(qd)/7) % rFreq == 0) { 1018 if ((dStart.daysTo(qd)/7) % rFreq == 0) {
1019 // The date is in a week which recurs 1019 // The date is in a week which recurs
1020 if (qd >= dStart 1020 if (qd >= dStart
1021 && ((rDuration > 0 && qd <= endDate()) || 1021 && ((rDuration > 0 && qd <= endDate()) ||
1022 (rDuration == 0 && qd <= rEndDateTime.date()) || 1022 (rDuration == 0 && qd <= rEndDateTime.date()) ||
1023 rDuration == -1)) { 1023 rDuration == -1)) {
1024 // The date queried falls within the range of the event. 1024 // The date queried falls within the range of the event.
1025 // check if the bits set match today. 1025 // check if the bits set match today.
1026 int i = qd.dayOfWeek()-1; 1026 int i = qd.dayOfWeek()-1;
1027 if (rDays.testBit((uint) i)) 1027 if (rDays.testBit((uint) i))
1028 return true; 1028 return true;
1029 } 1029 }
1030 } 1030 }
1031 return false; 1031 return false;
1032} 1032}
1033 1033
1034bool Recurrence::recursMonthly(const QDate &qd) const 1034bool Recurrence::recursMonthly(const QDate &qd) const
1035{ 1035{
1036 QDate dStart = mRecurStart.date(); 1036 QDate dStart = mRecurStart.date();
1037 int year = qd.year(); 1037 int year = qd.year();
1038 int month = qd.month(); 1038 int month = qd.month();
1039 int day = qd.day(); 1039 int day = qd.day();
1040 // calculate how many months ahead this date is from the original 1040 // calculate how many months ahead this date is from the original
1041 // event's date 1041 // event's date
1042 int monthsAhead = (year - dStart.year()) * 12 + (month - dStart.month()); 1042 int monthsAhead = (year - dStart.year()) * 12 + (month - dStart.month());
1043 if ((monthsAhead % rFreq) == 0) { 1043 if ((monthsAhead % rFreq) == 0) {
1044 // The date is in a month which recurs 1044 // The date is in a month which recurs
1045 if (qd >= dStart 1045 if (qd >= dStart
1046 && ((rDuration > 0 && qd <= endDate()) || 1046 && ((rDuration > 0 && qd <= endDate()) ||
1047 (rDuration == 0 && qd <= rEndDateTime.date()) || 1047 (rDuration == 0 && qd <= rEndDateTime.date()) ||
1048 rDuration == -1)) { 1048 rDuration == -1)) {
1049 // The date queried falls within the range of the event. 1049 // The date queried falls within the range of the event.
1050 QValueList<int> days; 1050 QValueList<int> days;
1051 int daysInMonth = qd.daysInMonth(); 1051 int daysInMonth = qd.daysInMonth();
1052 if (recurs == rMonthlyDay) 1052 if (recurs == rMonthlyDay)
1053 getMonthlyDayDays(days, daysInMonth); 1053 getMonthlyDayDays(days, daysInMonth);
1054 else if (recurs == rMonthlyPos) 1054 else if (recurs == rMonthlyPos)
1055 getMonthlyPosDays(days, daysInMonth, QDate(year, month, 1).dayOfWeek()); 1055 getMonthlyPosDays(days, daysInMonth, QDate(year, month, 1).dayOfWeek());
1056 for (QValueList<int>::Iterator it = days.begin(); it != days.end(); ++it) { 1056 for (QValueList<int>::Iterator it = days.begin(); it != days.end(); ++it) {
1057 if (*it == day) 1057 if (*it == day)
1058 return true; 1058 return true;
1059 } 1059 }
1060 // no dates matched 1060 // no dates matched
1061 } 1061 }
1062 } 1062 }
1063 return false; 1063 return false;
1064} 1064}
1065 1065
1066bool Recurrence::recursYearlyByMonth(const QDate &qd) const 1066bool Recurrence::recursYearlyByMonth(const QDate &qd) const
1067{ 1067{
1068 QDate dStart = mRecurStart.date(); 1068 QDate dStart = mRecurStart.date();
1069 int startDay = dStart.day(); 1069 int startDay = dStart.day();
1070 int qday = qd.day(); 1070 int qday = qd.day();
1071 int qmonth = qd.month(); 1071 int qmonth = qd.month();
1072 int qyear = qd.year(); 1072 int qyear = qd.year();
1073 bool match = (qday == startDay); 1073 bool match = (qday == startDay);
1074 if (!match && startDay == 29 && dStart.month() == 2) { 1074 if (!match && startDay == 29 && dStart.month() == 2) {
1075 // It's a recurrence on February 29th 1075 // It's a recurrence on February 29th
1076 switch (mFeb29YearlyType) { 1076 switch (mFeb29YearlyType) {
1077 case rFeb28: 1077 case rFeb28:
1078 if (qday == 28 && qmonth == 2 && !QDate::leapYear(qyear)) 1078 if (qday == 28 && qmonth == 2 && !QDate::leapYear(qyear))
1079 match = true; 1079 match = true;
1080 break; 1080 break;
1081 case rMar1: 1081 case rMar1:
1082 if (qday == 1 && qmonth == 3 && !QDate::leapYear(qyear)) { 1082 if (qday == 1 && qmonth == 3 && !QDate::leapYear(qyear)) {
1083 qmonth = 2; 1083 qmonth = 2;
1084 match = true; 1084 match = true;
1085 } 1085 }
1086 break; 1086 break;
1087 case rFeb29: 1087 case rFeb29:
1088 break; 1088 break;
1089 } 1089 }
1090 } 1090 }
1091 1091
1092 if (match) { 1092 if (match) {
1093 // The day of the month matches. Calculate how many years ahead 1093 // The day of the month matches. Calculate how many years ahead
1094 // this date is from the original event's date. 1094 // this date is from the original event's date.
1095 int yearsAhead = (qyear - dStart.year()); 1095 int yearsAhead = (qyear - dStart.year());
1096 if (yearsAhead % rFreq == 0) { 1096 if (yearsAhead % rFreq == 0) {
1097 // The date is in a year which recurs 1097 // The date is in a year which recurs
1098 if (qd >= dStart 1098 if (qd >= dStart
1099 && ((rDuration > 0 && qd <= endDate()) || 1099 && ((rDuration > 0 && qd <= endDate()) ||
1100 (rDuration == 0 && qd <= rEndDateTime.date()) || 1100 (rDuration == 0 && qd <= rEndDateTime.date()) ||
1101 rDuration == -1)) { 1101 rDuration == -1)) {
1102 // The date queried falls within the range of the event. 1102 // The date queried falls within the range of the event.
1103 int i = qmonth; 1103 int i = qmonth;
1104 for (QPtrListIterator<int> qlin(rYearNums); qlin.current(); ++qlin) { 1104 for (QPtrListIterator<int> qlin(rYearNums); qlin.current(); ++qlin) {
1105 if (i == *qlin.current()) 1105 if (i == *qlin.current())
1106 return true; 1106 return true;
1107 } 1107 }
1108 } 1108 }
1109 } 1109 }
1110 } 1110 }
1111 return false; 1111 return false;
1112} 1112}
1113 1113
1114bool Recurrence::recursYearlyByPos(const QDate &qd) const 1114bool Recurrence::recursYearlyByPos(const QDate &qd) const
1115{ 1115{
1116 QDate dStart = mRecurStart.date(); 1116 QDate dStart = mRecurStart.date();
1117 int year = qd.year(); 1117 int year = qd.year();
1118 int month = qd.month(); 1118 int month = qd.month();
1119 int day = qd.day(); 1119 int day = qd.day();
1120 // calculate how many years ahead this date is from the original 1120 // calculate how many years ahead this date is from the original
1121 // event's date 1121 // event's date
1122 int yearsAhead = (year - dStart.year()); 1122 int yearsAhead = (year - dStart.year());
1123 if (yearsAhead % rFreq == 0) { 1123 if (yearsAhead % rFreq == 0) {
1124 // The date is in a year which recurs 1124 // The date is in a year which recurs
1125 if (qd >= dStart 1125 if (qd >= dStart
1126 && ((rDuration > 0 && qd <= endDate()) || 1126 && ((rDuration > 0 && qd <= endDate()) ||
1127 (rDuration == 0 && qd <= rEndDateTime.date()) || 1127 (rDuration == 0 && qd <= rEndDateTime.date()) ||
1128 rDuration == -1)) { 1128 rDuration == -1)) {
1129 // The date queried falls within the range of the event. 1129 // The date queried falls within the range of the event.
1130 for (QPtrListIterator<int> qlin(rYearNums); qlin.current(); ++qlin) { 1130 for (QPtrListIterator<int> qlin(rYearNums); qlin.current(); ++qlin) {
1131 if (month == *qlin.current()) { 1131 if (month == *qlin.current()) {
1132 // The month recurs 1132 // The month recurs
1133 QValueList<int> days; 1133 QValueList<int> days;
1134 getMonthlyPosDays(days, qd.daysInMonth(), QDate(year, month, 1).dayOfWeek()); 1134 getMonthlyPosDays(days, qd.daysInMonth(), QDate(year, month, 1).dayOfWeek());
1135 for (QValueList<int>::Iterator it = days.begin(); it != days.end(); ++it) { 1135 for (QValueList<int>::Iterator it = days.begin(); it != days.end(); ++it) {
1136 if (*it == day) 1136 if (*it == day)
1137 return true; 1137 return true;
1138 } 1138 }
1139 } 1139 }
1140 } 1140 }
1141 } 1141 }
1142 } 1142 }
1143 return false; 1143 return false;
1144} 1144}
1145 1145
1146bool Recurrence::recursYearlyByDay(const QDate &qd) const 1146bool Recurrence::recursYearlyByDay(const QDate &qd) const
1147{ 1147{
1148 QDate dStart = mRecurStart.date(); 1148 QDate dStart = mRecurStart.date();
1149 // calculate how many years ahead this date is from the original 1149 // calculate how many years ahead this date is from the original
1150 // event's date 1150 // event's date
1151 int yearsAhead = (qd.year() - dStart.year()); 1151 int yearsAhead = (qd.year() - dStart.year());
1152 if (yearsAhead % rFreq == 0) { 1152 if (yearsAhead % rFreq == 0) {
1153 // The date is in a year which recurs 1153 // The date is in a year which recurs
1154 if (qd >= dStart 1154 if (qd >= dStart
1155 && ((rDuration > 0 && qd <= endDate()) || 1155 && ((rDuration > 0 && qd <= endDate()) ||
1156 (rDuration == 0 && qd <= rEndDateTime.date()) || 1156 (rDuration == 0 && qd <= rEndDateTime.date()) ||
1157 rDuration == -1)) { 1157 rDuration == -1)) {
1158 // The date queried falls within the range of the event. 1158 // The date queried falls within the range of the event.
1159 int i = qd.dayOfYear(); 1159 int i = qd.dayOfYear();
1160 for (QPtrListIterator<int> qlin(rYearNums); qlin.current(); ++qlin) { 1160 for (QPtrListIterator<int> qlin(rYearNums); qlin.current(); ++qlin) {
1161 if (i == *qlin.current()) 1161 if (i == *qlin.current())
1162 return true; 1162 return true;
1163 } 1163 }
1164 } 1164 }
1165 } 1165 }
1166 return false; 1166 return false;
1167} 1167}
1168 1168
1169/* Get the date of the next recurrence, after the specified date. 1169/* Get the date of the next recurrence, after the specified date.
1170 * If 'last' is non-null, '*last' is set to true if the next recurrence is the 1170 * If 'last' is non-null, '*last' is set to true if the next recurrence is the
1171 * last recurrence, else false. 1171 * last recurrence, else false.
1172 * Reply = date of next recurrence, or invalid date if none. 1172 * Reply = date of next recurrence, or invalid date if none.
1173 */ 1173 */
1174QDate Recurrence::getNextDateNoTime(const QDate &preDate, bool *last) const 1174QDate Recurrence::getNextDateNoTime(const QDate &preDate, bool *last) const
1175{ 1175{
1176 1176
1177 if (last) 1177 if (last)
1178 *last = false; 1178 *last = false;
1179 QDate dStart = mRecurStart.date(); 1179 QDate dStart = mRecurStart.date();
1180 if (preDate < dStart) 1180 if (preDate < dStart)
1181 return dStart; 1181 return dStart;
1182 QDate earliestDate = preDate.addDays(1); 1182 QDate earliestDate = preDate.addDays(1);
1183 QDate nextDate; 1183 QDate nextDate;
1184 1184
1185 switch (recurs) { 1185 switch (recurs) {
1186 case rDaily: 1186 case rDaily:
1187 nextDate = dStart.addDays((dStart.daysTo(preDate)/rFreq + 1) * rFreq); 1187 nextDate = dStart.addDays((dStart.daysTo(preDate)/rFreq + 1) * rFreq);
1188 break; 1188 break;
1189 1189
1190 case rWeekly: { 1190 case rWeekly: {
1191 QDate start = dStart.addDays(1 - dStart.dayOfWeek()); // start of week for dStart 1191 QDate start = dStart.addDays(-((dStart.dayOfWeek() - rWeekStart + 7)%7)); // start of week for dStart
1192 int earliestDayOfWeek = earliestDate.dayOfWeek(); 1192 int earliestDayOfWeek = earliestDate.dayOfWeek();
1193 int weeksAhead = start.daysTo(earliestDate) / 7; 1193 int weeksAhead = start.daysTo(earliestDate) / 7;
1194 int notThisWeek = weeksAhead % rFreq; // zero if this week is a recurring week 1194 int notThisWeek = weeksAhead % rFreq; // zero if this week is a recurring week
1195 weeksAhead -= notThisWeek; // latest week which recurred 1195 weeksAhead -= notThisWeek; // latest week which recurred
1196 int weekday = 0; 1196 int weekday = 0;
1197 // First check for any remaining day this week, if this week is a recurring week 1197 // First check for any remaining day this week, if this week is a recurring week
1198 if (!notThisWeek) 1198 if (!notThisWeek)
1199 weekday = getFirstDayInWeek(earliestDayOfWeek); 1199 weekday = getFirstDayInWeek(earliestDayOfWeek);
1200 // Check for a day in the next scheduled week 1200 // Check for a day in the next scheduled week
1201 if (!weekday && earliestDayOfWeek > 1) 1201 if (!weekday )
1202 weekday = getFirstDayInWeek(rWeekStart) + rFreq*7; 1202 weekday = getFirstDayInWeek(rWeekStart) + rFreq*7;
1203 if (weekday) 1203 nextDate = start.addDays(weeksAhead*7 + weekday - 1);
1204 nextDate = start.addDays(weeksAhead*7 + weekday - 1);
1205 break; 1204 break;
1206 } 1205 }
1207 case rMonthlyDay: 1206 case rMonthlyDay:
1208 case rMonthlyPos: { 1207 case rMonthlyPos: {
1209 int startYear = dStart.year(); 1208 int startYear = dStart.year();
1210 int startMonth = dStart.month(); // 1..12 1209 int startMonth = dStart.month(); // 1..12
1211 int earliestYear = earliestDate.year(); 1210 int earliestYear = earliestDate.year();
1212 int monthsAhead = (earliestYear - startYear)*12 + earliestDate.month() - startMonth; 1211 int monthsAhead = (earliestYear - startYear)*12 + earliestDate.month() - startMonth;
1213 int notThisMonth = monthsAhead % rFreq; // zero if this month is a recurring month 1212 int notThisMonth = monthsAhead % rFreq; // zero if this month is a recurring month
1214 monthsAhead -= notThisMonth; // latest month which recurred 1213 monthsAhead -= notThisMonth; // latest month which recurred
1215 // Check for the first later day in the current month 1214 // Check for the first later day in the current month
1216 if (!notThisMonth) 1215 if (!notThisMonth)
1217 nextDate = getFirstDateInMonth(earliestDate); 1216 nextDate = getFirstDateInMonth(earliestDate);
1218 if (!nextDate.isValid() && earliestDate.day() > 1) { 1217 if (!nextDate.isValid() ) {
1219 // Check for a day in the next scheduled month 1218 // Check for a day in the next scheduled month
1220 int months = startMonth - 1 + monthsAhead + rFreq; 1219 int months = startMonth - 1 + monthsAhead + rFreq;
1221 nextDate = getFirstDateInMonth(QDate(startYear + months/12, months%12 + 1, 1)); 1220 nextDate = getFirstDateInMonth(QDate(startYear + months/12, months%12 + 1, 1));
1222 } 1221 }
1223 break; 1222 break;
1224 } 1223 }
1225 case rYearlyMonth: 1224 case rYearlyMonth:
1226 case rYearlyPos: 1225 case rYearlyPos:
1227 case rYearlyDay: { 1226 case rYearlyDay: {
1228 int startYear = dStart.year(); 1227 int startYear = dStart.year();
1229 int yearsAhead = earliestDate.year() - startYear; 1228 int yearsAhead = earliestDate.year() - startYear;
1230 int notThisYear = yearsAhead % rFreq; // zero if this year is a recurring year 1229 int notThisYear = yearsAhead % rFreq; // zero if this year is a recurring year
1231 yearsAhead -= notThisYear; // latest year which recurred 1230 yearsAhead -= notThisYear; // latest year which recurred
1232 // Check for the first later date in the current year 1231 // Check for the first later date in the current year
1233 if (!notThisYear) 1232 if (!notThisYear)
1234 nextDate = getFirstDateInYear(earliestDate); 1233 nextDate = getFirstDateInYear(earliestDate);
1235 // Check for a date in the next scheduled year 1234 // Check for a date in the next scheduled year
1236 if (!nextDate.isValid() && earliestDate.dayOfYear() > 1) 1235 if (!nextDate.isValid() && earliestDate.dayOfYear() > 1)
1237 nextDate = getFirstDateInYear(QDate(startYear + yearsAhead + rFreq, 1, 1)); 1236 nextDate = getFirstDateInYear(QDate(startYear + yearsAhead + rFreq, 1, 1));
1238 break; 1237 break;
1239 } 1238 }
1240 case rNone: 1239 case rNone:
1241 default: 1240 default:
1242 return QDate(); 1241 return QDate();
1243 } 1242 }
1244 1243
1245 if (rDuration >= 0 && nextDate.isValid()) { 1244 if (rDuration >= 0 && nextDate.isValid()) {
1246 // Check that the date found is within the range of the recurrence 1245 // Check that the date found is within the range of the recurrence
1247 QDate end = endDate(); 1246 QDate end = endDate();
1248 if (nextDate > end) 1247 if (nextDate > end)
1249 return QDate(); 1248 return QDate();
1250 if (last && nextDate == end) 1249 if (last && nextDate == end)
1251 *last = true; 1250 *last = true;
1252 } 1251 }
1253 return nextDate; 1252 return nextDate;
1254} 1253}
1255 1254
1256/* Get the date of the last previous recurrence, before the specified date. 1255/* Get the date of the last previous recurrence, before the specified date.
1257 * Reply = date of previous recurrence, or invalid date if none. 1256 * Reply = date of previous recurrence, or invalid date if none.
1258 */ 1257 */
1259QDate Recurrence::getPreviousDateNoTime(const QDate &afterDate, bool *last) const 1258QDate Recurrence::getPreviousDateNoTime(const QDate &afterDate, bool *last) const
1260{ 1259{
1261 if (last) 1260 if (last)
1262 *last = false; 1261 *last = false;
1263 QDate dStart = mRecurStart.date(); 1262 QDate dStart = mRecurStart.date();
1264 QDate latestDate = afterDate.addDays(-1); 1263 QDate latestDate = afterDate.addDays(-1);
1265 if (latestDate < dStart) 1264 if (latestDate < dStart)
1266 return QDate(); 1265 return QDate();
1267 QDate prevDate; 1266 QDate prevDate;
1268 1267
1269 switch (recurs) { 1268 switch (recurs) {
1270 case rDaily: 1269 case rDaily:
1271 prevDate = dStart.addDays((dStart.daysTo(latestDate) / rFreq) * rFreq); 1270 prevDate = dStart.addDays((dStart.daysTo(latestDate) / rFreq) * rFreq);
1272 break; 1271 break;
1273 1272
1274 case rWeekly: { 1273 case rWeekly: {
1275 QDate start = dStart.addDays(1 - dStart.dayOfWeek()); // start of week for dStart 1274 QDate start = dStart.addDays(-((dStart.dayOfWeek() - rWeekStart + 7)%7)); // start of week for dStart
1276 int latestDayOfWeek = latestDate.dayOfWeek(); 1275 int latestDayOfWeek = latestDate.dayOfWeek();
1277 int weeksAhead = start.daysTo(latestDate) / 7; 1276 int weeksAhead = start.daysTo(latestDate) / 7;
1278 int notThisWeek = weeksAhead % rFreq; // zero if this week is a recurring week 1277 int notThisWeek = weeksAhead % rFreq; // zero if this week is a recurring week
1279 weeksAhead -= notThisWeek; // latest week which recurred 1278 weeksAhead -= notThisWeek; // latest week which recurred
1280 int weekday = 0; 1279 int weekday = 0;
1281 // First check for any previous day this week, if this week is a recurring week 1280 // First check for any previous day this week, if this week is a recurring week
1282 if (!notThisWeek) 1281 if (!notThisWeek)
1283 weekday = getLastDayInWeek(latestDayOfWeek); 1282 weekday = getLastDayInWeek(latestDayOfWeek);
1284 // Check for a day in the previous scheduled week 1283 // Check for a day in the previous scheduled week
1285 if (!weekday) { 1284 if (!weekday) {
1285 if (!notThisWeek)
1286 weeksAhead -= rFreq;
1286 int weekEnd = (rWeekStart + 5)%7 + 1; 1287 int weekEnd = (rWeekStart + 5)%7 + 1;
1287 if (latestDayOfWeek < weekEnd) { 1288 weekday = getLastDayInWeek(weekEnd);
1288 if (!notThisWeek)
1289 weeksAhead -= rFreq;
1290 weekday = getLastDayInWeek(weekEnd);
1291 }
1292 } 1289 }
1293 if (weekday) 1290 if (weekday)
1294 prevDate = start.addDays(weeksAhead*7 + weekday - 1); 1291 prevDate = start.addDays(weeksAhead*7 + weekday - 1);
1295 break; 1292 break;
1296 } 1293 }
1297 case rMonthlyDay: 1294 case rMonthlyDay:
1298 case rMonthlyPos: { 1295 case rMonthlyPos: {
1299 int startYear = dStart.year(); 1296 int startYear = dStart.year();
1300 int startMonth = dStart.month(); // 1..12 1297 int startMonth = dStart.month(); // 1..12
1301 int latestYear = latestDate.year(); 1298 int latestYear = latestDate.year();
1302 int monthsAhead = (latestYear - startYear)*12 + latestDate.month() - startMonth; 1299 int monthsAhead = (latestYear - startYear)*12 + latestDate.month() - startMonth;
1303 int notThisMonth = monthsAhead % rFreq; // zero if this month is a recurring month 1300 int notThisMonth = monthsAhead % rFreq; // zero if this month is a recurring month
1304 monthsAhead -= notThisMonth; // latest month which recurred 1301 monthsAhead -= notThisMonth; // latest month which recurred
1305 // Check for the last earlier day in the current month 1302 // Check for the last earlier day in the current month
1306 if (!notThisMonth) 1303 if (!notThisMonth)
1307 prevDate = getLastDateInMonth(latestDate); 1304 prevDate = getLastDateInMonth(latestDate);
1308 if (!prevDate.isValid() && latestDate.day() < latestDate.daysInMonth()) { 1305 if (!prevDate.isValid() && latestDate.day() < latestDate.daysInMonth()) {
1309 // Check for a day in the previous scheduled month 1306 // Check for a day in the previous scheduled month
1310 if (!notThisMonth) 1307 if (!notThisMonth)
1311 monthsAhead -= rFreq; 1308 monthsAhead -= rFreq;
1312 int months = startMonth + monthsAhead; // get the month after the one that recurs 1309 int months = startMonth + monthsAhead; // get the month after the one that recurs
1313 prevDate = getLastDateInMonth(QDate(startYear + months/12, months%12 + 1, 1).addDays(-1)); 1310 prevDate = getLastDateInMonth(QDate(startYear + months/12, months%12 + 1, 1).addDays(-1));
1314 } 1311 }
1315 break; 1312 break;
1316 } 1313 }
1317 case rYearlyMonth: 1314 case rYearlyMonth:
1318 case rYearlyPos: 1315 case rYearlyPos:
1319 case rYearlyDay: { 1316 case rYearlyDay: {
1320 int startYear = dStart.year(); 1317 int startYear = dStart.year();
1321 int yearsAhead = latestDate.year() - startYear; 1318 int yearsAhead = latestDate.year() - startYear;
1322 int notThisYear = yearsAhead % rFreq; // zero if this year is a recurring year 1319 int notThisYear = yearsAhead % rFreq; // zero if this year is a recurring year
1323 yearsAhead -= notThisYear; // latest year which recurred 1320 yearsAhead -= notThisYear; // latest year which recurred
1324 // Check for the first later date in the current year 1321 // Check for the first later date in the current year
1325 if (!notThisYear) 1322 if (!notThisYear)
1326 prevDate = getLastDateInYear(latestDate); 1323 prevDate = getLastDateInYear(latestDate);
1327 if (!prevDate.isValid() && latestDate.dayOfYear() < latestDate.daysInYear()) { 1324 if (!prevDate.isValid() && latestDate.dayOfYear() < latestDate.daysInYear()) {
1328 // Check for a date in the next scheduled year 1325 // Check for a date in the next scheduled year
1329 if (!notThisYear) 1326 if (!notThisYear)
1330 yearsAhead -= rFreq; 1327 yearsAhead -= rFreq;
1331 prevDate = getLastDateInYear(QDate(startYear + yearsAhead, 12, 31)); 1328 prevDate = getLastDateInYear(QDate(startYear + yearsAhead, 12, 31));
1332 } 1329 }
1333 break; 1330 break;
1334 } 1331 }
1335 case rNone: 1332 case rNone:
1336 default: 1333 default:
1337 return QDate(); 1334 return QDate();
1338 } 1335 }
1339 1336
1340 if (prevDate.isValid()) { 1337 if (prevDate.isValid()) {
1341 // Check that the date found is within the range of the recurrence 1338 // Check that the date found is within the range of the recurrence
1342 if (prevDate < dStart) 1339 if (prevDate < dStart)
1343 return QDate(); 1340 return QDate();
1344 if (rDuration >= 0) { 1341 if (rDuration >= 0) {
1345 QDate end = endDate(); 1342 QDate end = endDate();
1346 if (prevDate >= end) { 1343 if (prevDate >= end) {
1347 if (last) 1344 if (last)
1348 *last = true; 1345 *last = true;
1349 return end; 1346 return end;
1350 } 1347 }
1351 } 1348 }
1352 } 1349 }
1353 return prevDate; 1350 return prevDate;
1354} 1351}
1355 1352
1356void Recurrence::setDailySub(short type, int freq, int duration) 1353void Recurrence::setDailySub(short type, int freq, int duration)
1357{ 1354{
1358 recurs = type; 1355 recurs = type;
1359 rFreq = freq; 1356 rFreq = freq;
1360 rDuration = duration; 1357 rDuration = duration;
1361 rMonthPositions.clear(); 1358 rMonthPositions.clear();
1362 rMonthDays.clear(); 1359 rMonthDays.clear();
1363 rYearNums.clear(); 1360 rYearNums.clear();
1364 if (type != rDaily) 1361 if (type != rDaily)
1365 mFloats = false; // sub-daily types can't be floating 1362 mFloats = false; // sub-daily types can't be floating
1366 1363
1367 if (mParent) mParent->updated(); 1364 if (mParent) mParent->updated();
1368} 1365}
1369 1366
1370void Recurrence::setYearly_(short type, Feb29Type feb29type, int freq, int duration) 1367void Recurrence::setYearly_(short type, Feb29Type feb29type, int freq, int duration)
1371{ 1368{
1372 recurs = type; 1369 recurs = type;
1373 if (mCompatVersion < 310 && type == rYearlyDay) { 1370 if (mCompatVersion < 310 && type == rYearlyDay) {
1374 mCompatRecurs = rYearlyDay; 1371 mCompatRecurs = rYearlyDay;
1375 recurs = rYearlyMonth; // convert old yearly-by-day to yearly-by-month 1372 recurs = rYearlyMonth; // convert old yearly-by-day to yearly-by-month
1376 feb29type = rMar1; // retain the same day number in the year 1373 feb29type = rMar1; // retain the same day number in the year
1377 } 1374 }
1378 1375
1379 mFeb29YearlyType = feb29type; 1376 mFeb29YearlyType = feb29type;
1380 rFreq = freq; 1377 rFreq = freq;
1381 rDuration = duration; 1378 rDuration = duration;
1382 if (type != rYearlyPos) 1379 if (type != rYearlyPos)
1383 rMonthPositions.clear(); 1380 rMonthPositions.clear();
1384 rMonthDays.clear(); 1381 rMonthDays.clear();
1385 if (mParent) mParent->updated(); 1382 if (mParent) mParent->updated();
1386} 1383}
1387 1384
1388int Recurrence::recurCalc(PeriodFunc func, QDateTime &endtime) const 1385int Recurrence::recurCalc(PeriodFunc func, QDateTime &endtime) const
1389{ 1386{
1390 QDate enddate = endtime.date(); 1387 QDate enddate = endtime.date();
1391 switch (func) { 1388 switch (func) {
1392 case END_DATE_AND_COUNT: 1389 case END_DATE_AND_COUNT:
1393 if (rDuration < 0) { 1390 if (rDuration < 0) {
1394 endtime = QDateTime(); 1391 endtime = QDateTime();
1395 return 0; // infinite recurrence 1392 return 0; // infinite recurrence
1396 } 1393 }
1397 if (rDuration == 0) { 1394 if (rDuration == 0) {
1398 endtime = rEndDateTime; 1395 endtime = rEndDateTime;
1399 func = COUNT_TO_DATE; 1396 func = COUNT_TO_DATE;
1400 } 1397 }
1401 break; 1398 break;
1402 case COUNT_TO_DATE: 1399 case COUNT_TO_DATE:
1403 // Count recurrences up to and including the specified date/time. 1400 // Count recurrences up to and including the specified date/time.
1404 if (endtime < mRecurStart) 1401 if (endtime < mRecurStart)
1405 return 0; 1402 return 0;
1406 if (rDuration == 0 && endtime > rEndDateTime) 1403 if (rDuration == 0 && endtime > rEndDateTime)
1407 enddate = rEndDateTime.date(); 1404 enddate = rEndDateTime.date();
1408 else if (!mFloats && mRecurStart.time() > endtime.time()) 1405 else if (!mFloats && mRecurStart.time() > endtime.time())
1409 enddate = enddate.addDays(-1); 1406 enddate = enddate.addDays(-1);
1410 break; 1407 break;
1411 case NEXT_AFTER_DATE: 1408 case NEXT_AFTER_DATE:
1412 // Find next recurrence AFTER endtime 1409 // Find next recurrence AFTER endtime
1413 if (endtime < mRecurStart) { 1410 if (endtime < mRecurStart) {
1414 endtime = mRecurStart; 1411 endtime = mRecurStart;
1415 return 1; 1412 return 1;
1416 } 1413 }
1417 if (rDuration == 0 && endtime >= rEndDateTime) { 1414 if (rDuration == 0 && endtime >= rEndDateTime) {
1418 endtime = QDateTime(); 1415 endtime = QDateTime();
1419 return 0; 1416 return 0;
1420 } 1417 }
1421 if (!mFloats && mRecurStart.time() > endtime.time()) 1418 if (!mFloats && mRecurStart.time() > endtime.time())
1422 enddate = enddate.addDays(-1); 1419 enddate = enddate.addDays(-1);
1423 break; 1420 break;
1424 default: 1421 default:
1425 endtime = QDateTime(); 1422 endtime = QDateTime();
1426 return 0; 1423 return 0;
1427 } 1424 }
1428 1425
1429 int count = 0; // default = error 1426 int count = 0; // default = error
1430 bool timed = false; 1427 bool timed = false;
1431 switch (recurs) { 1428 switch (recurs) {
1432 case rMinutely: 1429 case rMinutely:
1433 timed = true; 1430 timed = true;
1434 count = secondlyCalc(func, endtime, rFreq*60); 1431 count = secondlyCalc(func, endtime, rFreq*60);
1435 break; 1432 break;
1436 case rHourly: 1433 case rHourly:
1437 timed = true; 1434 timed = true;
1438 count = secondlyCalc(func, endtime, rFreq*3600); 1435 count = secondlyCalc(func, endtime, rFreq*3600);
1439 break; 1436 break;
1440 case rDaily: 1437 case rDaily:
1441 count = dailyCalc(func, enddate); 1438 count = dailyCalc(func, enddate);
1442 break; 1439 break;
1443 case rWeekly: 1440 case rWeekly:
1444 count = weeklyCalc(func, enddate); 1441 count = weeklyCalc(func, enddate);
1445 break; 1442 break;
1446 case rMonthlyPos: 1443 case rMonthlyPos:
1447 case rMonthlyDay: 1444 case rMonthlyDay:
1448 count = monthlyCalc(func, enddate); 1445 count = monthlyCalc(func, enddate);
1449 break; 1446 break;
1450 case rYearlyMonth: 1447 case rYearlyMonth:
1451 count = yearlyMonthCalc(func, enddate); 1448 count = yearlyMonthCalc(func, enddate);
1452 break; 1449 break;
1453 case rYearlyPos: 1450 case rYearlyPos:
1454 count = yearlyPosCalc(func, enddate); 1451 count = yearlyPosCalc(func, enddate);
1455 break; 1452 break;
1456 case rYearlyDay: 1453 case rYearlyDay:
1457 count = yearlyDayCalc(func, enddate); 1454 count = yearlyDayCalc(func, enddate);
1458 break; 1455 break;
1459 default: 1456 default:
1460 break; 1457 break;
1461 } 1458 }
1462 1459
1463 switch (func) { 1460 switch (func) {
1464 case END_DATE_AND_COUNT: 1461 case END_DATE_AND_COUNT:
1465 case NEXT_AFTER_DATE: 1462 case NEXT_AFTER_DATE:
1466 if (count == 0) 1463 if (count == 0)
1467 endtime = QDateTime(); 1464 endtime = QDateTime();
1468 else if (!timed) { 1465 else if (!timed) {
1469 endtime.setDate(enddate); 1466 endtime.setDate(enddate);
1470 endtime.setTime(mRecurStart.time()); 1467 endtime.setTime(mRecurStart.time());
1471 } 1468 }
1472 break; 1469 break;
1473 case COUNT_TO_DATE: 1470 case COUNT_TO_DATE:
1474 break; 1471 break;
1475 } 1472 }
1476 return count; 1473 return count;
1477} 1474}
1478 1475
1479int Recurrence::recurCalc(PeriodFunc func, QDate &enddate) const 1476int Recurrence::recurCalc(PeriodFunc func, QDate &enddate) const
1480{ 1477{
1481 QDateTime endtime(enddate, QTime(23,59,59)); 1478 QDateTime endtime(enddate, QTime(23,59,59));
1482 switch (func) { 1479 switch (func) {
1483 case END_DATE_AND_COUNT: 1480 case END_DATE_AND_COUNT:
1484 if (rDuration < 0) { 1481 if (rDuration < 0) {
1485 enddate = QDate(); 1482 enddate = QDate();
1486 return 0; // infinite recurrence 1483 return 0; // infinite recurrence
1487 } 1484 }
1488 if (rDuration == 0) { 1485 if (rDuration == 0) {
1489 enddate = rEndDateTime.date(); 1486 enddate = rEndDateTime.date();
1490 func = COUNT_TO_DATE; 1487 func = COUNT_TO_DATE;
1491 } 1488 }
1492 break; 1489 break;
1493 case COUNT_TO_DATE: 1490 case COUNT_TO_DATE:
1494 // Count recurrences up to and including the specified date. 1491 // Count recurrences up to and including the specified date.
1495 if (enddate < mRecurStart.date()) 1492 if (enddate < mRecurStart.date())
1496 return 0; 1493 return 0;
1497 if (rDuration == 0 && enddate > rEndDateTime.date()) { 1494 if (rDuration == 0 && enddate > rEndDateTime.date()) {
1498 enddate = rEndDateTime.date(); 1495 enddate = rEndDateTime.date();
1499 endtime.setDate(enddate); 1496 endtime.setDate(enddate);
1500 } 1497 }
1501 break; 1498 break;
1502 case NEXT_AFTER_DATE: 1499 case NEXT_AFTER_DATE:
1503 if (enddate < mRecurStart.date()) { 1500 if (enddate < mRecurStart.date()) {
1504 enddate = mRecurStart.date(); 1501 enddate = mRecurStart.date();
1505 return 1; 1502 return 1;
1506 } 1503 }
1507 if (rDuration == 0 && enddate >= rEndDateTime.date()) { 1504 if (rDuration == 0 && enddate >= rEndDateTime.date()) {
1508 enddate = QDate(); 1505 enddate = QDate();
1509 return 0; 1506 return 0;
1510 } 1507 }
1511 break; 1508 break;
1512 default: 1509 default:
1513 enddate = QDate(); 1510 enddate = QDate();
1514 return 0; 1511 return 0;
1515 } 1512 }
1516 1513
1517 int count = 0; // default = error 1514 int count = 0; // default = error
1518 bool timed = false; 1515 bool timed = false;
1519 switch (recurs) { 1516 switch (recurs) {
1520 case rMinutely: 1517 case rMinutely:
1521 timed = true; 1518 timed = true;
1522 count = secondlyCalc(func, endtime, rFreq*60); 1519 count = secondlyCalc(func, endtime, rFreq*60);
1523 break; 1520 break;
1524 case rHourly: 1521 case rHourly:
1525 timed = true; 1522 timed = true;
1526 count = secondlyCalc(func, endtime, rFreq*3600); 1523 count = secondlyCalc(func, endtime, rFreq*3600);
1527 break; 1524 break;
1528 case rDaily: 1525 case rDaily:
1529 count = dailyCalc(func, enddate); 1526 count = dailyCalc(func, enddate);
1530 break; 1527 break;
1531 case rWeekly: 1528 case rWeekly:
1532 count = weeklyCalc(func, enddate); 1529 count = weeklyCalc(func, enddate);
1533 break; 1530 break;
1534 case rMonthlyPos: 1531 case rMonthlyPos:
1535 case rMonthlyDay: 1532 case rMonthlyDay:
1536 count = monthlyCalc(func, enddate); 1533 count = monthlyCalc(func, enddate);
1537 break; 1534 break;
1538 case rYearlyMonth: 1535 case rYearlyMonth:
1539 count = yearlyMonthCalc(func, enddate); 1536 count = yearlyMonthCalc(func, enddate);
1540 break; 1537 break;
1541 case rYearlyPos: 1538 case rYearlyPos:
1542 count = yearlyPosCalc(func, enddate); 1539 count = yearlyPosCalc(func, enddate);
1543 break; 1540 break;
1544 case rYearlyDay: 1541 case rYearlyDay:
1545 count = yearlyDayCalc(func, enddate); 1542 count = yearlyDayCalc(func, enddate);
1546 break; 1543 break;
1547 default: 1544 default:
@@ -3147,267 +3144,258 @@ bool Recurrence::getYearlyMonthMonths(int day, QValueList<int> &list, QValueList
3147 * specified week which is >= the startDay. 3144 * specified week which is >= the startDay.
3148 * Parameters: startDay = 1..7 (Monday..Sunday) 3145 * Parameters: startDay = 1..7 (Monday..Sunday)
3149 * useWeekStart = true to end search at day before next rWeekStart 3146 * useWeekStart = true to end search at day before next rWeekStart
3150 * = false to search for a full 7 days 3147 * = false to search for a full 7 days
3151 * Reply = day of the week (1..7), or 0 if none found. 3148 * Reply = day of the week (1..7), or 0 if none found.
3152 */ 3149 */
3153int Recurrence::getFirstDayInWeek(int startDay, bool useWeekStart) const 3150int Recurrence::getFirstDayInWeek(int startDay, bool useWeekStart) const
3154{ 3151{
3155 int last = ((useWeekStart ? rWeekStart : startDay) + 5)%7; 3152 int last = ((useWeekStart ? rWeekStart : startDay) + 5)%7;
3156 for (int i = startDay - 1; ; i = (i + 1)%7) { 3153 for (int i = startDay - 1; ; i = (i + 1)%7) {
3157 if (rDays.testBit(i)) 3154 if (rDays.testBit(i))
3158 return i + 1; 3155 return i + 1;
3159 if (i == last) 3156 if (i == last)
3160 return 0; 3157 return 0;
3161 } 3158 }
3162} 3159}
3163 3160
3164/* From the recurrence day of the week list, get the latest day in the 3161/* From the recurrence day of the week list, get the latest day in the
3165 * specified week which is <= the endDay. 3162 * specified week which is <= the endDay.
3166 * Parameters: endDay = 1..7 (Monday..Sunday) 3163 * Parameters: endDay = 1..7 (Monday..Sunday)
3167 * useWeekStart = true to end search at rWeekStart 3164 * useWeekStart = true to end search at rWeekStart
3168 * = false to search for a full 7 days 3165 * = false to search for a full 7 days
3169 * Reply = day of the week (1..7), or 0 if none found. 3166 * Reply = day of the week (1..7), or 0 if none found.
3170 */ 3167 */
3171int Recurrence::getLastDayInWeek(int endDay, bool useWeekStart) const 3168int Recurrence::getLastDayInWeek(int endDay, bool useWeekStart) const
3172{ 3169{
3173 int last = useWeekStart ? rWeekStart - 1 : endDay%7; 3170 int last = useWeekStart ? rWeekStart - 1 : endDay%7;
3174 for (int i = endDay - 1; ; i = (i + 6)%7) { 3171 for (int i = endDay - 1; ; i = (i + 6)%7) {
3175 if (rDays.testBit(i)) 3172 if (rDays.testBit(i))
3176 return i + 1; 3173 return i + 1;
3177 if (i == last) 3174 if (i == last)
3178 return 0; 3175 return 0;
3179 } 3176 }
3180} 3177}
3181 3178
3182/* From the recurrence monthly day number list or monthly day of week/week of 3179/* From the recurrence monthly day number list or monthly day of week/week of
3183 * month list, get the earliest day in the specified month which is >= the 3180 * month list, get the earliest day in the specified month which is >= the
3184 * earliestDate. 3181 * earliestDate.
3185 */ 3182 */
3186QDate Recurrence::getFirstDateInMonth(const QDate &earliestDate) const 3183QDate Recurrence::getFirstDateInMonth(const QDate &earliestDate) const
3187{ 3184{
3188 int earliestDay = earliestDate.day(); 3185 int earliestDay = earliestDate.day();
3189 int daysInMonth = earliestDate.daysInMonth(); 3186 int daysInMonth = earliestDate.daysInMonth();
3190 switch (recurs) { 3187 switch (recurs) {
3191 case rMonthlyDay: { 3188 case rMonthlyDay: {
3192 int minday = daysInMonth + 1; 3189 int minday = daysInMonth + 1;
3193 for (QPtrListIterator<int> it(rMonthDays); it.current(); ++it) { 3190 for (QPtrListIterator<int> it(rMonthDays); it.current(); ++it) {
3194 int day = *it.current(); 3191 int day = *it.current();
3195 if (day < 0) 3192 if (day < 0)
3196 day = daysInMonth + day + 1; 3193 day = daysInMonth + day + 1;
3197 if (day >= earliestDay && day < minday) 3194 if (day >= earliestDay && day < minday)
3198 minday = day; 3195 minday = day;
3199 } 3196 }
3200 if (minday <= daysInMonth) 3197 if (minday <= daysInMonth)
3201 return earliestDate.addDays(minday - earliestDay); 3198 return earliestDate.addDays(minday - earliestDay);
3202 break; 3199 break;
3203 } 3200 }
3204 case rMonthlyPos: 3201 case rMonthlyPos:
3205 case rYearlyPos: { 3202 case rYearlyPos: {
3206 QDate monthBegin(earliestDate.addDays(1 - earliestDay)); 3203 QDate monthBegin(earliestDate.addDays(1 - earliestDay));
3207 QValueList<int> dayList; 3204 QValueList<int> dayList;
3208 getMonthlyPosDays(dayList, daysInMonth, monthBegin.dayOfWeek()); 3205 getMonthlyPosDays(dayList, daysInMonth, monthBegin.dayOfWeek());
3209 for (QValueList<int>::ConstIterator id = dayList.begin(); id != dayList.end(); ++id) { 3206 for (QValueList<int>::ConstIterator id = dayList.begin(); id != dayList.end(); ++id) {
3210 if (*id >= earliestDay) 3207 if (*id >= earliestDay)
3211 return monthBegin.addDays(*id - 1); 3208 return monthBegin.addDays(*id - 1);
3212 } 3209 }
3213 break; 3210 break;
3214 } 3211 }
3215 } 3212 }
3216 return QDate(); 3213 return QDate();
3217} 3214}
3218 3215
3219/* From the recurrence monthly day number list or monthly day of week/week of 3216/* From the recurrence monthly day number list or monthly day of week/week of
3220 * month list, get the latest day in the specified month which is <= the 3217 * month list, get the latest day in the specified month which is <= the
3221 * latestDate. 3218 * latestDate.
3222 */ 3219 */
3223QDate Recurrence::getLastDateInMonth(const QDate &latestDate) const 3220QDate Recurrence::getLastDateInMonth(const QDate &latestDate) const
3224{ 3221{
3225 int latestDay = latestDate.day(); 3222 int latestDay = latestDate.day();
3226 int daysInMonth = latestDate.daysInMonth(); 3223 int daysInMonth = latestDate.daysInMonth();
3227 switch (recurs) { 3224 switch (recurs) {
3228 case rMonthlyDay: { 3225 case rMonthlyDay: {
3229 int maxday = -1; 3226 int maxday = -1;
3230 for (QPtrListIterator<int> it(rMonthDays); it.current(); ++it) { 3227 for (QPtrListIterator<int> it(rMonthDays); it.current(); ++it) {
3231 int day = *it.current(); 3228 int day = *it.current();
3232 if (day < 0) 3229 if (day < 0)
3233 day = daysInMonth + day + 1; 3230 day = daysInMonth + day + 1;
3234 if (day <= latestDay && day > maxday) 3231 if (day <= latestDay && day > maxday)
3235 maxday = day; 3232 maxday = day;
3236 } 3233 }
3237 if (maxday > 0) 3234 if (maxday > 0)
3238 return QDate(latestDate.year(), latestDate.month(), maxday); 3235 return QDate(latestDate.year(), latestDate.month(), maxday);
3239 break; 3236 break;
3240 } 3237 }
3241 case rMonthlyPos: 3238 case rMonthlyPos:
3242 case rYearlyPos: { 3239 case rYearlyPos: {
3243 QDate monthBegin(latestDate.addDays(1 - latestDay)); 3240 QDate monthBegin(latestDate.addDays(1 - latestDay));
3244 QValueList<int> dayList; 3241 QValueList<int> dayList;
3245 getMonthlyPosDays(dayList, daysInMonth, monthBegin.dayOfWeek()); 3242 getMonthlyPosDays(dayList, daysInMonth, monthBegin.dayOfWeek());
3246 for (QValueList<int>::ConstIterator id = dayList.fromLast(); id != dayList.end(); --id) { 3243 for (QValueList<int>::ConstIterator id = dayList.fromLast(); id != dayList.end(); --id) {
3247 if (*id <= latestDay) 3244 if (*id <= latestDay)
3248 return monthBegin.addDays(*id - 1); 3245 return monthBegin.addDays(*id - 1);
3249 } 3246 }
3250 break; 3247 break;
3251 } 3248 }
3252 } 3249 }
3253 return QDate(); 3250 return QDate();
3254} 3251}
3255 3252
3256/* From the recurrence yearly month list or yearly day list, get the earliest 3253/* From the recurrence yearly month list or yearly day list, get the earliest
3257 * month or day in the specified year which is >= the earliestDate. 3254 * month or day in the specified year which is >= the earliestDate.
3258 * Note that rYearNums is sorted in numerical order. 3255 * Note that rYearNums is sorted in numerical order.
3259 */ 3256 */
3260QDate Recurrence::getFirstDateInYear(const QDate &earliestDate) const 3257QDate Recurrence::getFirstDateInYear(const QDate &earliestDate) const
3261{ 3258{
3262 QPtrListIterator<int> it(rYearNums); 3259 QPtrListIterator<int> it(rYearNums);
3263 switch (recurs) { 3260 switch (recurs) {
3264 case rYearlyMonth: { 3261 case rYearlyMonth: {
3265 int day = recurStart().date().day(); 3262 int day = recurStart().date().day();
3266 int earliestYear = earliestDate.year(); 3263 int earliestYear = earliestDate.year();
3267 int earliestMonth = earliestDate.month(); 3264 int earliestMonth = earliestDate.month();
3268 int earliestDay = earliestDate.day(); 3265 int earliestDay = earliestDate.day();
3269 if (earliestDay > day) { 3266 if (earliestDay > day) {
3270 // The earliest date is later in the month than the recurrence date, 3267 // The earliest date is later in the month than the recurrence date,
3271 // so skip to the next month before starting to check 3268 // so skip to the next month before starting to check
3272 if (++earliestMonth > 12) 3269 if (++earliestMonth > 12)
3273 return QDate(); 3270 return QDate();
3274 } 3271 }
3275 for ( ; it.current(); ++it) { 3272 for ( ; it.current(); ++it) {
3276 int month = *it.current(); 3273 int month = *it.current();
3277 if (month >= earliestMonth) { 3274 if (month >= earliestMonth) {
3278 if (day <= 28 || QDate::isValid(earliestYear, month, day)) 3275 if (day <= 28 || QDate::isValid(earliestYear, month, day))
3279 return QDate(earliestYear, month, day); 3276 return QDate(earliestYear, month, day);
3280 if (day == 29 && month == 2) { 3277 if (day == 29 && month == 2) {
3281 // It's a recurrence on February 29th, in a non-leap year 3278 // It's a recurrence on February 29th, in a non-leap year
3282 switch (mFeb29YearlyType) { 3279 switch (mFeb29YearlyType) {
3283 case rMar1: 3280 case rMar1:
3284 return QDate(earliestYear, 3, 1); 3281 return QDate(earliestYear, 3, 1);
3285 case rFeb28: 3282 case rFeb28:
3286 if (earliestDay <= 28) 3283 if (earliestDay <= 28)
3287 return QDate(earliestYear, 2, 28); 3284 return QDate(earliestYear, 2, 28);
3288 break; 3285 break;
3289 case rFeb29: 3286 case rFeb29:
3290 break; 3287 break;
3291 } 3288 }
3292 } 3289 }
3293 } 3290 }
3294 } 3291 }
3295 break; 3292 break;
3296 } 3293 }
3297 case rYearlyPos: { 3294 case rYearlyPos: {
3298 QValueList<int> dayList; 3295 QValueList<int> dayList;
3299 int earliestYear = earliestDate.year(); 3296 int earliestYear = earliestDate.year();
3300 int earliestMonth = earliestDate.month(); 3297 int earliestMonth = earliestDate.month();
3301 int earliestDay = earliestDate.day(); 3298 int earliestDay = earliestDate.day();
3302 for ( ; it.current(); ++it) { 3299 for ( ; it.current(); ++it) {
3303 int month = *it.current(); 3300 int month = *it.current();
3304 if (month >= earliestMonth) { 3301 if (month >= earliestMonth) {
3305 QDate monthBegin(earliestYear, month, 1); 3302 QDate monthBegin(earliestYear, month, 1);
3306 getMonthlyPosDays(dayList, monthBegin.daysInMonth(), monthBegin.dayOfWeek()); 3303 getMonthlyPosDays(dayList, monthBegin.daysInMonth(), monthBegin.dayOfWeek());
3307 for (QValueList<int>::ConstIterator id = dayList.begin(); id != dayList.end(); ++id) { 3304 for (QValueList<int>::ConstIterator id = dayList.begin(); id != dayList.end(); ++id) {
3308 if (*id >= earliestDay) 3305 if (*id >= earliestDay)
3309 return monthBegin.addDays(*id - 1); 3306 return monthBegin.addDays(*id - 1);
3310 } 3307 }
3311 earliestDay = 1; 3308 earliestDay = 1;
3312 } 3309 }
3313 } 3310 }
3314 break; 3311 break;
3315 } 3312 }
3316 case rYearlyDay: { 3313 case rYearlyDay: {
3317 int earliestDay = earliestDate.dayOfYear(); 3314 int earliestDay = earliestDate.dayOfYear();
3318 for ( ; it.current(); ++it) { 3315 for ( ; it.current(); ++it) {
3319 int day = *it.current(); 3316 int day = *it.current();
3320 if (day >= earliestDay && (day <= 365 || day <= earliestDate.daysInYear())) 3317 if (day >= earliestDay && (day <= 365 || day <= earliestDate.daysInYear()))
3321 return earliestDate.addDays(day - earliestDay); 3318 return earliestDate.addDays(day - earliestDay);
3322 } 3319 }
3323 break; 3320 break;
3324 } 3321 }
3325 } 3322 }
3326 return QDate(); 3323 return QDate();
3327} 3324}
3328 3325
3329/* From the recurrence yearly month list or yearly day list, get the latest 3326/* From the recurrence yearly month list or yearly day list, get the latest
3330 * month or day in the specified year which is <= the latestDate. 3327 * month or day in the specified year which is <= the latestDate.
3331 * Note that rYearNums is sorted in numerical order. 3328 * Note that rYearNums is sorted in numerical order.
3332 */ 3329 */
3333QDate Recurrence::getLastDateInYear(const QDate &latestDate) const 3330QDate Recurrence::getLastDateInYear(const QDate &latestDate) const
3334{ 3331{
3335 QPtrListIterator<int> it(rYearNums); 3332 QPtrListIterator<int> it(rYearNums);
3336 switch (recurs) { 3333 switch (recurs) {
3337 case rYearlyMonth: { 3334 case rYearlyMonth: {
3338 int day = recurStart().date().day(); 3335 int day = recurStart().date().day();
3339 int latestYear = latestDate.year(); 3336 int latestYear = latestDate.year();
3340 int latestMonth = latestDate.month(); 3337 int latestMonth = latestDate.month();
3341 if (latestDate.day() > day) { 3338 if (latestDate.day() > day) {
3342 // The latest date is earlier in the month than the recurrence date, 3339 // The latest date is earlier in the month than the recurrence date,
3343 // so skip to the previous month before starting to check 3340 // so skip to the previous month before starting to check
3344 if (--latestMonth <= 0) 3341 if (--latestMonth <= 0)
3345 return QDate(); 3342 return QDate();
3346 } 3343 }
3347 for (it.toLast(); it.current(); --it) { 3344 for (it.toLast(); it.current(); --it) {
3348 int month = *it.current(); 3345 int month = *it.current();
3349 if (month <= latestMonth) { 3346 if (month <= latestMonth) {
3350 if (day <= 28 || QDate::isValid(latestYear, month, day)) 3347 if (day <= 28 || QDate::isValid(latestYear, month, day))
3351 return QDate(latestYear, month, day); 3348 return QDate(latestYear, month, day);
3352 if (day == 29 && month == 2) { 3349 if (day == 29 && month == 2) {
3353 // It's a recurrence on February 29th, in a non-leap year 3350 // It's a recurrence on February 29th, in a non-leap year
3354 switch (mFeb29YearlyType) { 3351 switch (mFeb29YearlyType) {
3355 case rMar1: 3352 case rMar1:
3356 if (latestMonth >= 3) 3353 if (latestMonth >= 3)
3357 return QDate(latestYear, 3, 1); 3354 return QDate(latestYear, 3, 1);
3358 break; 3355 break;
3359 case rFeb28: 3356 case rFeb28:
3360 return QDate(latestYear, 2, 28); 3357 return QDate(latestYear, 2, 28);
3361 case rFeb29: 3358 case rFeb29:
3362 break; 3359 break;
3363 } 3360 }
3364 } 3361 }
3365 } 3362 }
3366 } 3363 }
3367 break; 3364 break;
3368 } 3365 }
3369 case rYearlyPos: { 3366 case rYearlyPos: {
3370 QValueList<int> dayList; 3367 QValueList<int> dayList;
3371 int latestYear = latestDate.year(); 3368 int latestYear = latestDate.year();
3372 int latestMonth = latestDate.month(); 3369 int latestMonth = latestDate.month();
3373 int latestDay = latestDate.day(); 3370 int latestDay = latestDate.day();
3374 for (it.toLast(); it.current(); --it) { 3371 for (it.toLast(); it.current(); --it) {
3375 int month = *it.current(); 3372 int month = *it.current();
3376 if (month <= latestMonth) { 3373 if (month <= latestMonth) {
3377 QDate monthBegin(latestYear, month, 1); 3374 QDate monthBegin(latestYear, month, 1);
3378 getMonthlyPosDays(dayList, monthBegin.daysInMonth(), monthBegin.dayOfWeek()); 3375 getMonthlyPosDays(dayList, monthBegin.daysInMonth(), monthBegin.dayOfWeek());
3379 for (QValueList<int>::ConstIterator id = dayList.fromLast(); id != dayList.end(); --id) { 3376 for (QValueList<int>::ConstIterator id = dayList.fromLast(); id != dayList.end(); --id) {
3380 if (*id <= latestDay) 3377 if (*id <= latestDay)
3381 return monthBegin.addDays(*id - 1); 3378 return monthBegin.addDays(*id - 1);
3382 } 3379 }
3383 latestDay = 31; 3380 latestDay = 31;
3384 } 3381 }
3385 } 3382 }
3386 break; 3383 break;
3387 } 3384 }
3388 case rYearlyDay: { 3385 case rYearlyDay: {
3389 int latestDay = latestDate.dayOfYear(); 3386 int latestDay = latestDate.dayOfYear();
3390 for (it.toLast(); it.current(); --it) { 3387 for (it.toLast(); it.current(); --it) {
3391 int day = *it.current(); 3388 int day = *it.current();
3392 if (day <= latestDay) 3389 if (day <= latestDay)
3393 return latestDate.addDays(day - latestDay); 3390 return latestDate.addDays(day - latestDay);
3394 } 3391 }
3395 break; 3392 break;
3396 } 3393 }
3397 } 3394 }
3398 return QDate(); 3395 return QDate();
3399} 3396}
3400 3397
3401void Recurrence::dump() const 3398void Recurrence::dump() const
3402{ 3399{
3403 kdDebug() << "Recurrence::dump():" << endl; 3400 ;
3404
3405 kdDebug() << " type: " << recurs << endl;
3406
3407 kdDebug() << " rDays: " << endl;
3408 int i;
3409 for( i = 0; i < 7; ++i ) {
3410 kdDebug() << " " << i << ": "
3411 << ( rDays.testBit( i ) ? "true" : "false" ) << endl;
3412 }
3413} 3401}