Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

odatebookaccessbackend.cpp

Go to the documentation of this file.
00001 /*
00002                              This file is part of the Opie Project
00003                              Copyright (C) Stefan Eilers (Eilers.Stefan@epost.de)
00004               =.             Copyright (C) The Opie Team <opie-devel@handhelds.org>
00005             .=l.
00006            .>+-=
00007  _;:,     .>    :=|.         This program is free software; you can
00008 .> <`_,   >  .   <=          redistribute it and/or  modify it under
00009 :`=1 )Y*s>-.--   :           the terms of the GNU Library General Public
00010 .="- .-=="i,     .._         License as published by the Free Software
00011  - .   .-<_>     .<>         Foundation; either version 2 of the License,
00012      ._= =}       :          or (at your option) any later version.
00013     .%`+i>       _;_.
00014     .i_,=:_.      -<s.       This program is distributed in the hope that
00015      +  .  -:.       =       it will be useful,  but WITHOUT ANY WARRANTY;
00016     : ..    .:,     . . .    without even the implied warranty of
00017     =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
00018   _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU
00019 ..}^=.=       =       ;      Library General Public License for more
00020 ++=   -.     .`     .:       details.
00021  :     =  ...= . :.=-
00022  -.   .:....=;==+<;          You should have received a copy of the GNU
00023   -_. . .   )=.  =           Library General Public License along with
00024     --        :-=`           this library; see the file COPYING.LIB.
00025                              If not, write to the Free Software Foundation,
00026                              Inc., 59 Temple Place - Suite 330,
00027                              Boston, MA 02111-1307, USA.
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 /* a small helper to get all NonRepeating events for a range of time */
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          * If in range
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                     // Filter Categories
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                                     // Unfiled. Check next category if list is not empty.
00168                                     // Else: take it as we will not filter unfiled events..
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             // Continue to next event if the category filter removed this item
00185             if ( !catPassed )
00186                     continue;
00187             
00188             vector.insert( item++, event );
00189     }
00190 
00191     // Now sort the vector and return the list of UID's
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          * Let us find occurrences that are 'now'!
00213          * If the dt.date() is on the same day as start or end of the Occurrence
00214          * check how near start/end are.
00215          * If it is in the middle of a multiday occurrence list it.
00216          *
00217          * We might want to 'lose' the sixty second offset and list
00218          * all Events which are active at that time.
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 }

Generated on Sat Nov 5 16:16:15 2005 for OPIE by  doxygen 1.4.2