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

ocontactaccessbackend.cpp

Go to the documentation of this file.
00001 /*
00002                              This file is part of the Opie Project
00003                              Copyright (C) Holger Freyther <freyther@handhelds.org>
00004                              Copyright (C) Stefan Eilers <stefan@eilers-online.net> 
00005               =.             Copyright (C) The Opie Team <opie-devel@handhelds.org>
00006             .=l.
00007            .>+-=
00008  _;:,     .>    :=|.         This program is free software; you can
00009 .> <`_,   >  .   <=          redistribute it and/or  modify it under
00010 :`=1 )Y*s>-.--   :           the terms of the GNU Library General Public
00011 .="- .-=="i,     .._         License as published by the Free Software
00012  - .   .-<_>     .<>         Foundation; either version 2 of the License,
00013      ._= =}       :          or (at your option) any later version.
00014     .%`+i>       _;_.
00015     .i_,=:_.      -<s.       This program is distributed in the hope that
00016      +  .  -:.       =       it will be useful,  but WITHOUT ANY WARRANTY;
00017     : ..    .:,     . . .    without even the implied warranty of
00018     =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
00019   _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU
00020 ..}^=.=       =       ;      Library General Public License for more
00021 ++=   -.     .`     .:       details.
00022  :     =  ...= . :.=-
00023  -.   .:....=;==+<;          You should have received a copy of the GNU
00024   -_. . .   )=.  =           Library General Public License along with
00025     --        :-=`           this library; see the file COPYING.LIB.
00026                              If not, write to the Free Software Foundation,
00027                              Inc., 59 Temple Place - Suite 330,
00028                              Boston, MA 02111-1307, USA.
00029 */
00030 
00031 #include "ocontactaccessbackend.h"
00032 #include <opie2/private/opimcontactsortvector.h>
00033 #include <opie2/ocontactaccess.h>
00034 
00035 #include <opie2/odebug.h>
00036 
00037 #include <qdatetime.h>
00038 
00039 namespace Opie {
00040 OPimContactAccessBackend::OPimContactAccessBackend() {}
00041 
00042 UIDArray OPimContactAccessBackend::queryByExample( const UIDArray& uid_array, const OPimContact& query, int settings,
00043                                                    const QDateTime& d )const {
00044         odebug << "Using Unaccelerated OPimContactAccessBackend implementation of queryByExample!" << oendl;
00045 
00046         UIDArray m_currentQuery( uid_array.count() );
00047         uint arraycounter = 0;
00048 
00049         for( uint it = 0; it < uid_array.count(); ++it ){
00050                 /* Search all fields and compare them with query object. Store them into list
00051                  * if all fields matches.
00052                  */
00053                 QDate* queryDate = 0l;
00054                 QDate* checkDate = 0l;
00055                 bool allcorrect = true;
00056                 for ( int i = 0; i < Qtopia::Groups; i++ ) {
00057                         // Birthday and anniversary are special nonstring fields and should
00058                         // be handled specially
00059                         switch ( i ){
00060                         case Qtopia::Birthday:
00061                                 queryDate = new QDate( query.birthday() );
00062                                 checkDate = new QDate( find( uid_array[it] ).birthday() );
00063                                 // fall through
00064                         case Qtopia::Anniversary:
00065                                 if ( queryDate == 0l ){
00066                                         queryDate = new QDate( query.anniversary() );
00067                                         checkDate = new QDate( find( uid_array[it] ).anniversary() );
00068                                 }
00069 
00070                                 if ( queryDate->isValid() ){
00071                                         if(  checkDate->isValid() ){
00072                                                 if ( settings & OPimContactAccess::DateYear ){
00073                                                         if ( queryDate->year() != checkDate->year() )
00074                                                                 allcorrect = false;
00075                                                 }
00076                                                 if ( settings & OPimContactAccess::DateMonth ){
00077                                                         if ( queryDate->month() != checkDate->month() )
00078                                                                 allcorrect = false;
00079                                                 }
00080                                                 if ( settings & OPimContactAccess::DateDay ){
00081                                                         if ( queryDate->day() != checkDate->day() )
00082                                                                 allcorrect = false;
00083                                                 }
00084                                                 if ( settings & OPimContactAccess::DateDiff ) {
00085                                                         QDate current;
00086                                                         // If we get an additional date, we
00087                                                         // will take this date instead of
00088                                                         // the current one..
00089                                                         if ( !d.date().isValid() )
00090                                                                 current = QDate::currentDate();
00091                                                         else
00092                                                                 current = d.date();
00093 
00094                                                         // We have to equalize the year, otherwise
00095                                                         // the search will fail..
00096                                                         checkDate->setYMD( current.year(),
00097                                                                            checkDate->month(),
00098                                                                            checkDate->day() );
00099                                                         if ( *checkDate < current )
00100                                                                 checkDate->setYMD( current.year()+1,
00101                                                                                    checkDate->month(),
00102                                                                                    checkDate->day() );
00103 
00104                                                         // Check whether the birthday/anniversary date is between
00105                                                         // the current/given date and the maximum date
00106                                                         // ( maximum time range ) !
00107                                                         if ( current.daysTo( *queryDate ) >= 0 ){
00108                                                                 if ( !( ( *checkDate >= current ) &&
00109                                                                         ( *checkDate <= *queryDate ) ) ){
00110                                                                         allcorrect = false;
00111                                                                 }
00112                                                         }
00113                                                 }
00114                                         } else{
00115                                                 // checkDate is invalid. Therefore this entry is always rejected
00116                                                 allcorrect = false;
00117                                         }
00118                                 }
00119 
00120                                 delete queryDate;
00121                                 queryDate = 0l;
00122                                 delete checkDate;
00123                                 checkDate = 0l;
00124                                 break;
00125                         default:
00126                                 /* Just compare fields which are not empty in the query object */
00127                                 if ( !query.field(i).isEmpty() ){
00128                                         switch ( settings & ~( OPimContactAccess::IgnoreCase
00129                                                                | OPimContactAccess::DateDiff
00130                                                                | OPimContactAccess::DateYear
00131                                                                | OPimContactAccess::DateMonth
00132                                                                | OPimContactAccess::DateDay
00133                                                                | OPimContactAccess::MatchOne
00134                                                                ) ){
00135 
00136                                         case OPimContactAccess::RegExp:{
00137                                                 QRegExp expr ( query.field(i),
00138                                                                !(settings & OPimContactAccess::IgnoreCase),
00139                                                                false );
00140                                                 if ( expr.find ( find( uid_array[it] ).field(i), 0 ) == -1 )
00141                                                         allcorrect = false;
00142                                         }
00143                                                 break;
00144                                         case OPimContactAccess::WildCards:{
00145                                                 QRegExp expr ( query.field(i),
00146                                                                !(settings & OPimContactAccess::IgnoreCase),
00147                                                                true );
00148                                                 if ( expr.find ( find( uid_array[it] ).field(i), 0 ) == -1 )
00149                                                         allcorrect = false;
00150                                         }
00151                                                 break;
00152                                         case OPimContactAccess::ExactMatch:{
00153                                                 if (settings & OPimContactAccess::IgnoreCase){
00154                                                         if ( query.field(i).upper() !=
00155                                                              find( uid_array[it] ).field(i).upper() )
00156                                                                 allcorrect = false;
00157                                                 }else{
00158                                                         if ( query.field(i) != find( uid_array[it] ).field(i) )
00159                                                                 allcorrect = false;
00160                                                 }
00161                                         }
00162                                                 break;
00163                                         }
00164                                 }
00165                         }
00166                 }
00167                 if ( allcorrect ){
00168                         m_currentQuery[arraycounter++] = uid_array[it];
00169                 }
00170         }
00171 
00172         // Shrink to fit..
00173         m_currentQuery.resize(arraycounter);
00174 
00175         return m_currentQuery;
00176 
00177 }
00178 
00179 const uint OPimContactAccessBackend::querySettings() const
00180 {
00181     return ( OPimContactAccess::WildCards
00182          | OPimContactAccess::IgnoreCase
00183          | OPimContactAccess::RegExp
00184          | OPimContactAccess::ExactMatch
00185          | OPimContactAccess::DateDiff
00186          | OPimContactAccess::DateYear
00187          | OPimContactAccess::DateMonth
00188          | OPimContactAccess::DateDay
00189          );
00190 }
00191 
00192 bool OPimContactAccessBackend::hasQuerySettings (uint querySettings) const
00193 {
00194     /* OPimContactAccess::IgnoreCase, DateDiff, DateYear, DateMonth, DateDay
00195      * may be added with any of the other settings. IgnoreCase should never used alone.
00196      * Wildcards, RegExp, ExactMatch should never used at the same time...
00197      */
00198 
00199     // Step 1: Check whether the given settings are supported by this backend
00200     if ( ( querySettings & (
00201                 OPimContactAccess::IgnoreCase
00202                 | OPimContactAccess::WildCards
00203                 | OPimContactAccess::DateDiff
00204                 | OPimContactAccess::DateYear
00205                 | OPimContactAccess::DateMonth
00206                 | OPimContactAccess::DateDay
00207                 | OPimContactAccess::RegExp
00208                 | OPimContactAccess::ExactMatch
00209                    ) ) != querySettings )
00210         return false;
00211 
00212     // Step 2: Check whether the given combinations are ok..
00213 
00214     // IngoreCase alone is invalid
00215     if ( querySettings == OPimContactAccess::IgnoreCase )
00216         return false;
00217 
00218     // WildCards, RegExp and ExactMatch should never used at the same time
00219     switch ( querySettings & ~( OPimContactAccess::IgnoreCase
00220                     | OPimContactAccess::DateDiff
00221                     | OPimContactAccess::DateYear
00222                     | OPimContactAccess::DateMonth
00223                     | OPimContactAccess::DateDay
00224                     )
00225          ){
00226     case OPimContactAccess::RegExp:
00227         return ( true );
00228     case OPimContactAccess::WildCards:
00229         return ( true );
00230     case OPimContactAccess::ExactMatch:
00231         return ( true );
00232     case 0: // one of the upper removed bits were set..
00233         return ( true );
00234     default:
00235         return ( false );
00236     }
00237 }
00238 
00239 
00240 UIDArray OPimContactAccessBackend::sorted( const UIDArray& ar, bool asc, int sortOrder,
00241                                   int filter, const QArray<int>& categories )const {
00242     odebug << "Using Unaccelerated OPimContactAccessBackend sorted Implementation" << oendl;
00243 
00244     Internal::OPimContactSortVector vector(ar.count(), asc, sortOrder );
00245 
00246     int item = 0;
00247     uint cat_count = categories.count();
00248     uint eve_count = ar.count();
00249     bool contactPassed = false;
00250     int cat;
00251 
00252     for ( uint i = 0; i < eve_count; ++i ) {
00253         OPimContact contact = find( ar[i], ar, i, Frontend::Forward );
00254         if ( contact.isEmpty() )
00255             continue;
00256 
00257         contactPassed = true;
00258 
00259         if ( (filter & OPimContactAccess::DoNotShowWithCategory) ? true : false ){
00260                 if ( !contact.categories().isEmpty() )
00261                         continue;
00262         } else {
00263 
00264                 if ( (filter  & OPimContactAccess::FilterCategory) ? true : false ){
00265                         /* show category */
00266                         /* -1 == unfiled */
00267                         for ( uint cat_nu = 0; cat_nu < cat_count; ++cat_nu ) {
00268                                 cat = categories[cat_nu];
00269                         
00270                                 if ( cat == -1 ) { 
00271                                         // We should search unfiled contacts. 
00272                                         // Unfiled categories have no category set, thus continue if 
00273                                         // this contact has no empty category.
00274                                         if( !contact.categories().isEmpty() )
00275                                                 contactPassed = false;
00276                                 } else if ( cat != 0 )
00277                                         if ( !contact.categories().contains( cat ) )
00278                                                 contactPassed = false;
00279                         }
00280                         
00281                 }
00282         }
00283         /*
00284          * If none of the Categories matched
00285          * continue
00286          */
00287         if ( !contactPassed )
00288             continue;
00289 
00290         vector.insert(item++, contact );
00291     }
00292 
00293     vector.resize( item );
00294     /* sort it now */
00295     vector.sort();
00296     /* now get the uids */
00297     UIDArray array( vector.count() );
00298     for (uint i= 0; i < vector.count(); i++ )
00299         array[i] = vector.uidAt( i );
00300 
00301     return array;
00302 }
00303 
00304 OPimBackendOccurrence::List OPimContactAccessBackend::occurrences( const QDate& start,
00305                                                                    const QDate& end)const {
00306     OPimBackendOccurrence::List lst;
00307 
00308     UIDArray records = allRecords();
00309     const uint count = records.count();
00310     int uid;
00311 
00312     for ( uint i = 0; i < count; ++i ) {
00313         uid = records[i];
00314         OPimContact contact = find(uid, records, i, Frontend::Forward );
00315 
00316         QDate date = contact.anniversary();
00317         date = QDate( start.year(), date.month(),date.day() );
00318 
00319 //        if ( date.isValid() && date.) {
00320 //        }
00321     }
00322 
00323     return lst;
00324 }
00325 }

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