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
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
00051
00052
00053 QDate* queryDate = 0l;
00054 QDate* checkDate = 0l;
00055 bool allcorrect = true;
00056 for ( int i = 0; i < Qtopia::Groups; i++ ) {
00057
00058
00059 switch ( i ){
00060 case Qtopia::Birthday:
00061 queryDate = new QDate( query.birthday() );
00062 checkDate = new QDate( find( uid_array[it] ).birthday() );
00063
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
00087
00088
00089 if ( !d.date().isValid() )
00090 current = QDate::currentDate();
00091 else
00092 current = d.date();
00093
00094
00095
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
00105
00106
00107 if ( current.daysTo( *queryDate ) >= 0 ){
00108 if ( !( ( *checkDate >= current ) &&
00109 ( *checkDate <= *queryDate ) ) ){
00110 allcorrect = false;
00111 }
00112 }
00113 }
00114 } else{
00115
00116 allcorrect = false;
00117 }
00118 }
00119
00120 delete queryDate;
00121 queryDate = 0l;
00122 delete checkDate;
00123 checkDate = 0l;
00124 break;
00125 default:
00126
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
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
00195
00196
00197
00198
00199
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
00213
00214
00215 if ( querySettings == OPimContactAccess::IgnoreCase )
00216 return false;
00217
00218
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:
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
00266
00267 for ( uint cat_nu = 0; cat_nu < cat_count; ++cat_nu ) {
00268 cat = categories[cat_nu];
00269
00270 if ( cat == -1 ) {
00271
00272
00273
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
00285
00286
00287 if ( !contactPassed )
00288 continue;
00289
00290 vector.insert(item++, contact );
00291 }
00292
00293 vector.resize( item );
00294
00295 vector.sort();
00296
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
00320
00321 }
00322
00323 return lst;
00324 }
00325 }