00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <qtl.h>
00030
00031 #include <opie2/odebug.h>
00032 #include <opie2/opimrecurrence.h>
00033 #include <opie2/odatebookaccess.h>
00034 #include <opie2/odatebookaccessbackend.h>
00035 #include <opie2/private/opimeventsortvector.h>
00036
00037 using namespace Opie;
00038
00039 namespace {
00040
00041 void events( OPimBackendOccurrence::List& tmpList,
00042 const OPimEvent::ValueList& events,
00043 const QDate& from, const QDate& to ) {
00044 QDateTime dtStart, dtEnd;
00045
00046 for ( OPimEvent::ValueList::ConstIterator it = events.begin(); it != events.end(); ++it ) {
00047 dtStart = (*it).startDateTime();
00048 dtEnd = (*it).endDateTime();
00049
00050
00051
00052
00053 if (dtStart.date() >= from && dtEnd.date() <= to ) {
00054 OPimBackendOccurrence eff( dtStart, dtEnd, (*it).uid() );;
00055 tmpList.append( eff );
00056 }
00057 }
00058 }
00059
00060 void repeat( OPimBackendOccurrence::List& tmpList, const OPimEvent::ValueList& list,
00061 const QDate& from, const QDate& to ) {
00062 QDate repeat;
00063 for ( OPimEvent::ValueList::ConstIterator it = list.begin(); it != list.end(); ++it ) {
00064 int dur = (*it).startDateTime().date().daysTo( (*it).endDateTime().date() );
00065 QDate itDate = from.addDays(-dur );
00066 OPimRecurrence rec = (*it).recurrence();
00067 if ( !rec.hasEndDate() || rec.endDate() > to ) {
00068 rec.setEndDate( to );
00069 rec.setHasEndDate( true );
00070 }
00071
00072 QDateTime start, end;
00073 while (rec.nextOcurrence(itDate, repeat ) ) {
00074 if (repeat > to ) break;
00075
00076 OPimEvent event = *it;
00077 start = QDateTime( repeat, event.startDateTime().time() );
00078 end = QDateTime( repeat.addDays(dur), event.endDateTime().time() );
00079 OPimBackendOccurrence eff(start, end, event.uid() );
00080 tmpList.append( eff );
00081 }
00082 }
00083 }
00084 }
00085
00086 namespace Opie {
00087
00088 ODateBookAccessBackend::ODateBookAccessBackend()
00089 : OPimAccessBackend<OPimEvent>()
00090 {
00091
00092 }
00093 ODateBookAccessBackend::~ODateBookAccessBackend() {
00094
00095 }
00096 OPimBackendOccurrence::List ODateBookAccessBackend::occurrences( const QDate& from,
00097 const QDate& to )const {
00098 OPimBackendOccurrence::List tmpList;
00099
00100 events( tmpList, directNonRepeats(), from, to );
00101 repeat( tmpList, directRawRepeats(),from,to );
00102
00103 return tmpList;
00104 }
00105
00106 OPimBackendOccurrence::List ODateBookAccessBackend::occurrences( const QDateTime& dt )const
00107 {
00108 OPimBackendOccurrence::List day = occurrences( dt.date(), dt.date() );
00109
00110 return filterOccurrences( day, dt );
00111 }
00112
00113 OPimBackendOccurrence::List ODateBookAccessBackend::effectiveNonRepeatingEvents( const QDate& from,
00114 const QDate& to )const
00115 {
00116 OPimBackendOccurrence::List tmpList;
00117 OPimEvent::ValueList list = directNonRepeats();
00118
00119 events( tmpList, list, from, to );
00120
00121 return tmpList;
00122 }
00123
00124 OPimBackendOccurrence::List ODateBookAccessBackend::effectiveNonRepeatingEvents( const QDateTime& dt )const
00125 {
00126 OPimBackendOccurrence::List day = effectiveNonRepeatingEvents( dt.date(), dt.date() );
00127 return filterOccurrences( day,dt );
00128 }
00129
00130 const uint ODateBookAccessBackend::querySettings() const
00131 {
00132 return 0;
00133 }
00134
00135 bool ODateBookAccessBackend::hasQuerySettings (uint querySettings) const
00136 {
00137 return false;
00138 }
00139
00140
00141
00142 UIDArray ODateBookAccessBackend::queryByExample( const UIDArray& uidlist, const OPimEvent&, int settings,
00143 const QDateTime& d )const
00144 {
00145 qDebug( "Accessing ODateBookAccessBackend::queryByExample() which is not implemented!" );
00146 return UIDArray();
00147 }
00148
00149 UIDArray ODateBookAccessBackend::sorted( const UIDArray& ar, bool asc, int sortOrder, int filter, const QArray<int>& categories )const {
00150 odebug << "Using Unaccelerated ODateBookAccessBackend sorted Implementation" << oendl;
00151
00152 Internal::OPimEventSortVector vector( ar.count(), asc, sortOrder );
00153 int item = 0;
00154
00155 for ( uint i = 0; i < ar.count(); ++i ){
00156 OPimEvent event = find( ar[i], ar, i, Frontend::Forward );
00157 if ( event.isEmpty() )
00158 continue;
00159
00160 bool catPassed = true;
00161 if ( filter & ODateBookAccess::FilterCategory ){
00162 catPassed = false;
00163
00164 for ( uint cat_idx = 0; cat_idx < categories.count(); ++cat_idx ){
00165 int cat = categories[cat_idx];
00166 if ( cat == -1 || cat == 0 ){
00167
00168
00169 if ( !event.categories().isEmpty() )
00170 continue;
00171 else
00172 catPassed = true;
00173 } else {
00174 if ( !event.categories().contains( cat ) )
00175 continue;
00176 else{
00177 catPassed = true;
00178 break;
00179 }
00180 }
00181 }
00182 }
00183
00184
00185 if ( !catPassed )
00186 continue;
00187
00188 vector.insert( item++, event );
00189 }
00190
00191
00192 vector.resize( item );
00193 vector.sort();
00194
00195 UIDArray array( vector.count() );
00196 for ( uint i= 0; i < vector.count(); i++ )
00197 array[i] = vector.uidAt( i );
00198
00199 return array;
00200
00201 }
00202
00203 OPimBackendOccurrence::List ODateBookAccessBackend::filterOccurrences( const OPimBackendOccurrence::List dayList,
00204 const QDateTime& dt ) {
00205 OPimBackendOccurrence::List tmpList;
00206 OPimBackendOccurrence::List::ConstIterator it;
00207
00208 for ( it = dayList.begin(); it != dayList.end(); ++it ) {
00209 OPimBackendOccurrence occ = *it;
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220 if ( dt.date() == occ.startDateTime().date() ) {
00221 if ( QABS( dt.time().secsTo( occ.startDateTime().time() ) ) < 60 )
00222 tmpList.append( occ );
00223 }else if ( dt.date() == occ.endDateTime().date() ) {
00224 if ( QABS( dt.time().secsTo( occ.endDateTime().time() ) ) < 60 )
00225 tmpList.append( occ );
00226 }else if ( dt.date() >= occ.startDateTime().date() &&
00227 dt.date() >= occ.endDateTime().date() )
00228 tmpList.append( occ );
00229 }
00230
00231 return tmpList;
00232 }
00233 }