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

otodoaccesssql.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) Holger Freyther (zecke@handhelds.org)
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 /* OPIE */
00032 #include <opie2/osqldriver.h>
00033 #include <opie2/osqlresult.h>
00034 #include <opie2/osqlmanager.h>
00035 #include <opie2/osqlquery.h>
00036 
00037 #include <opie2/otodoaccesssql.h>
00038 #include <opie2/opimstate.h>
00039 #include <opie2/opimnotifymanager.h>
00040 #include <opie2/opimrecurrence.h>
00041 #include <opie2/odebug.h>
00042 
00043 #include <qpe/global.h>
00044 
00045 /* QT */
00046 #include <qdatetime.h>
00047 #include <qmap.h>
00048 #include <qstring.h>
00049 
00050 
00051 using namespace Opie::DB;
00052 
00053 using namespace Opie;
00054 /*
00055  * first some query
00056  * CREATE query
00057  * LOAD query
00058  * INSERT
00059  * REMOVE
00060  * CLEAR
00061  */
00062 namespace {
00066     class CreateQuery : public OSQLQuery {
00067     public:
00068         CreateQuery();
00069         ~CreateQuery();
00070         QString query()const;
00071     };
00072 
00077     class LoadQuery : public OSQLQuery {
00078     public:
00079         LoadQuery();
00080         ~LoadQuery();
00081         QString query()const;
00082     };
00083 
00087     class InsertQuery : public OSQLQuery {
00088     public:
00089         InsertQuery(const OPimTodo& );
00090         ~InsertQuery();
00091         QString query()const;
00092     private:
00093         OPimTodo m_todo;
00094     };
00095 
00099     class RemoveQuery : public OSQLQuery {
00100     public:
00101         RemoveQuery(int uid );
00102         ~RemoveQuery();
00103         QString query()const;
00104     private:
00105         int m_uid;
00106     };
00107 
00111     class ClearQuery : public OSQLQuery {
00112     public:
00113         ClearQuery();
00114         ~ClearQuery();
00115         QString query()const;
00116 
00117     };
00118 
00122     class FindQuery : public OSQLQuery {
00123     public:
00124         FindQuery(int uid);
00125         FindQuery(const QArray<int>& );
00126         ~FindQuery();
00127         QString query()const;
00128     private:
00129         QString single()const;
00130         QString multi()const;
00131         QArray<int> m_uids;
00132         int m_uid;
00133     };
00134 
00138     class OverDueQuery : public OSQLQuery {
00139     public:
00140         OverDueQuery();
00141         ~OverDueQuery();
00142         QString query()const;
00143     };
00144     class EffQuery : public OSQLQuery {
00145     public:
00146         EffQuery( const QDate&, const QDate&, bool inc );
00147         ~EffQuery();
00148         QString query()const;
00149     private:
00150         QString with()const;
00151         QString out()const;
00152         QDate m_start;
00153         QDate m_end;
00154         bool m_inc :1;
00155     };
00156 
00157 
00161     class FindCustomQuery : public OSQLQuery {
00162     public:
00163         FindCustomQuery(int uid);
00164         FindCustomQuery(const QArray<int>& );
00165         ~FindCustomQuery();
00166         QString query()const;
00167     private:
00168         QString single()const;
00169         QString multi()const;
00170         QArray<int> m_uids;
00171         int m_uid;
00172     };
00173 
00174 
00175 
00176     CreateQuery::CreateQuery() : OSQLQuery() {}
00177     CreateQuery::~CreateQuery() {}
00178     QString CreateQuery::query()const {
00179         QString qu;
00180         qu += "create table todolist( uid PRIMARY KEY, categories, completed, ";
00181         qu += "description, summary, priority, DueDate, progress ,  state, ";
00182     // This is the recurrance-stuff .. Exceptions are currently not supported (see OPimRecurrence.cpp) ! (eilers)
00183     qu += "RType, RWeekdays, RPosition, RFreq, RHasEndDate, EndDate, Created, Exceptions, ";
00184     qu += "reminders, alarms, maintainer, startdate, completeddate);";
00185     qu += "create table custom_data( uid INTEGER, id INTEGER, type VARCHAR(10), priority INTEGER, value VARCHAR(10), PRIMARY KEY /* identifier */ (uid, id) );";
00186         return qu;
00187     }
00188 
00189     LoadQuery::LoadQuery() : OSQLQuery() {}
00190     LoadQuery::~LoadQuery() {}
00191     QString LoadQuery::query()const {
00192         QString qu;
00193     // We do not need "distinct" here. The primary key is always unique..
00194         //qu += "select distinct uid from todolist";
00195         qu += "select uid from todolist";
00196 
00197         return qu;
00198     }
00199 
00200     InsertQuery::InsertQuery( const OPimTodo& todo )
00201         : OSQLQuery(), m_todo( todo ) {
00202     }
00203     InsertQuery::~InsertQuery() {
00204     }
00205     /*
00206      * converts from a OPimTodo to a query
00207      * we leave out X-Ref + Maintainer
00208      * FIXME: Implement/Finish toMap()/fromMap() into OpimTodo to move the encoding
00209      *        decoding stuff there.. (eilers)
00210      */
00211     QString InsertQuery::query()const{
00212 
00213         int year, month, day;
00214         year = month = day = 0;
00215         if (m_todo.hasDueDate() ) {
00216             QDate date = m_todo.dueDate();
00217             year = date.year();
00218             month = date.month();
00219             day = date.day();
00220     }
00221     int sYear = 0, sMonth = 0, sDay = 0;
00222     if( m_todo.hasStartDate() ){
00223         QDate sDate = m_todo.startDate();
00224         sYear = sDate.year();
00225         sMonth= sDate.month();
00226         sDay  = sDate.day();
00227     }
00228 
00229     int eYear = 0, eMonth = 0, eDay = 0;
00230     if( m_todo.hasCompletedDate() ){
00231         QDate eDate = m_todo.completedDate();
00232         eYear = eDate.year();
00233         eMonth= eDate.month();
00234         eDay  = eDate.day();
00235     }
00236         QString qu;
00237     QMap<int, QString> recMap = m_todo.recurrence().toMap();
00238         qu  = "insert into todolist VALUES("
00239         +  QString::number( m_todo.uid() ) + ","
00240         + "'" + m_todo.idsToString( m_todo.categories() ) + "'" + ","
00241         +       QString::number( m_todo.isCompleted() )         + ","
00242         + "'" + m_todo.description()                      + "'" + ","
00243         + "'" + m_todo.summary()                          + "'" + ","
00244         +       QString::number(m_todo.priority() )             + ","
00245         + "'" + QString::number(year).rightJustify( 4, '0' ) + "-"
00246         +       QString::number(month).rightJustify( 2, '0' )
00247         +       "-" + QString::number( day ).rightJustify( 2, '0' )+ "'" + ","
00248         +       QString::number( m_todo.progress() )            + ","
00249         +       QString::number( m_todo.state().state() )       + ","
00250         + "'" + recMap[ OPimRecurrence::RType ]                   + "'" + ","
00251         + "'" + recMap[ OPimRecurrence::RWeekdays ]               + "'" + ","
00252         + "'" + recMap[ OPimRecurrence::RPosition ]               + "'" + ","
00253         + "'" + recMap[ OPimRecurrence::RFreq ]                   + "'" + ","
00254         + "'" + recMap[ OPimRecurrence::RHasEndDate ]             + "'" + ","
00255         + "'" + recMap[ OPimRecurrence::EndDate ]                 + "'" + ","
00256         + "'" + recMap[ OPimRecurrence::Created ]                 + "'" + ","
00257         + "'" + recMap[ OPimRecurrence::Exceptions ]              + "'" + ",";
00258 
00259     if ( m_todo.hasNotifiers() ) {
00260         OPimNotifyManager manager = m_todo.notifiers();
00261         qu += "'" + manager.remindersToString()           + "'" + ","
00262             + "'" + manager.alarmsToString()          + "'" + ",";
00263     }
00264     else{
00265         qu += QString( "''" )                                   + ","
00266             + "''"                                          + ",";
00267     }
00268 
00269     qu +=   QString( "''" )                             + QString( "," ) // Maintainers (cur. not supported !)
00270         + "'" + QString::number(sYear).rightJustify( 4, '0' ) + "-"
00271         + QString::number(sMonth).rightJustify( 2, '0' )
00272         + "-" + QString::number(sDay).rightJustify( 2, '0' )+ "'" + ","
00273         + "'" + QString::number(eYear).rightJustify( 4, '0' ) + "-"
00274         +       QString::number(eMonth).rightJustify( 2, '0' )
00275         +       "-"+QString::number(eDay).rightJustify( 2, '0' ) + "'"
00276         + "); ";
00277 
00278     // Save custom Entries:
00279     int id = 0;
00280     id = 0;
00281     QMap<QString, QString> customMap = m_todo.toExtraMap();
00282     for( QMap<QString, QString>::Iterator it = customMap.begin();
00283          it != customMap.end(); ++it ){
00284         qu  += "insert into custom_data VALUES("
00285             +  QString::number( m_todo.uid() )
00286             + ","
00287             +  QString::number( id++ )
00288             + ",'"
00289             + it.key()
00290             + "',"
00291             + "0" // Priority for future enhancements
00292             + ",'"
00293             + it.data()
00294             + "');";
00295     }
00296 
00297 
00298         odebug << "add " << qu << "" << oendl;
00299         return qu;
00300     }
00301 
00302     RemoveQuery::RemoveQuery(int uid )
00303         : OSQLQuery(), m_uid( uid ) {}
00304 
00305     RemoveQuery::~RemoveQuery() {}
00306 
00307     QString RemoveQuery::query()const {
00308         QString qu = "DELETE FROM todolist WHERE uid = " + QString::number(m_uid) + " ;";
00309         qu += "DELETE FROM custom_data WHERE uid = " + QString::number(m_uid);
00310         return qu;
00311     }
00312 
00313 
00314     ClearQuery::ClearQuery()
00315         : OSQLQuery() {}
00316     ClearQuery::~ClearQuery() {}
00317     QString ClearQuery::query()const 
00318     {
00319             QString qu = "drop table todolist";
00320             return qu;
00321     }
00322 
00323     FindQuery::FindQuery(int uid)
00324         : OSQLQuery(), m_uid(uid ) 
00325     {
00326     }
00327     
00328     FindQuery::FindQuery(const QArray<int>& ints)
00329             : OSQLQuery(), m_uids(ints)
00330     {
00331     }
00332 
00333     FindQuery::~FindQuery() 
00334     {
00335     }
00336 
00337     QString FindQuery::query()const{
00338         if (m_uids.count() == 0 )
00339             return single();
00340         else
00341             return multi();
00342     }
00343     QString FindQuery::single()const{
00344         QString qu = "select * from todolist where uid = " + QString::number(m_uid);
00345         return qu;
00346     }
00347     QString FindQuery::multi()const {
00348         QString qu = "select * from todolist where ";
00349         for (uint i = 0; i < m_uids.count(); i++ ) {
00350             qu += " UID = " + QString::number( m_uids[i] ) + " OR";
00351         }
00352         qu.remove( qu.length()-2, 2 );
00353         return qu;
00354     }
00355 
00356     OverDueQuery::OverDueQuery(): OSQLQuery() {}
00357     OverDueQuery::~OverDueQuery() {}
00358     QString OverDueQuery::query()const {
00359         QDate date = QDate::currentDate();
00360         QString str;
00361         str = QString("select uid from todolist where DueDate ='%1-%2-%3'")
00362         .arg( QString::number( date.year() ).rightJustify( 4, '0' ) )
00363         .arg( QString::number( date.month() ).rightJustify( 2, '0' ) )
00364         .arg( QString::number( date.day() ) .rightJustify( 2, '0' ) );
00365 
00366         return str;
00367     }
00368 
00369 
00370     EffQuery::EffQuery( const QDate& start, const QDate& end, bool inc )
00371         : OSQLQuery(), m_start( start ), m_end( end ),m_inc(inc) {}
00372     EffQuery::~EffQuery() {}
00373     QString EffQuery::query()const {
00374         return m_inc ? with() : out();
00375     }
00376     QString EffQuery::with()const {
00377         QString str;
00378         str = QString("select uid from todolist where ( DueDate >= '%1-%2-%3' AND DueDate <= '%4-%5-%6' ) OR DueDate = '0-0-0' ")
00379         .arg( QString::number( m_start.year() ).rightJustify( 4, '0' ) )
00380         .arg( QString::number( m_start.month() ).rightJustify( 2, '0' ) )
00381         .arg( QString::number( m_start.day() ).rightJustify( 2, '0' ) )
00382         .arg( QString::number( m_end.year() ).rightJustify( 4, '0' ) )
00383         .arg( QString::number( m_end.month() ).rightJustify( 2, '0' ) )
00384         .arg( QString::number( m_end.day() ).rightJustify( 2, '0' ) );
00385         return str;
00386     }
00387     QString EffQuery::out()const {
00388         QString str;
00389         str = QString("select uid from todolist where DueDate >= '%1-%2-%3' AND  DueDate <= '%4-%5-%6'")
00390         .arg( QString::number( m_start.year() ).rightJustify( 4, '0' ) )
00391         .arg( QString::number( m_start.month() ).rightJustify( 2, '0' ) )
00392         .arg( QString::number( m_start.day() ).rightJustify( 2, '0' ) )
00393         .arg( QString::number( m_end.year() ).rightJustify( 4, '0' ) )
00394         .arg( QString::number( m_end.month() ).rightJustify( 2, '0' ) )
00395         .arg( QString::number( m_end.day() ).rightJustify( 2, '0' ) );
00396 
00397         return str;
00398     }
00399 
00400     FindCustomQuery::FindCustomQuery(int uid)
00401         : OSQLQuery(), m_uid( uid ) {
00402     }
00403     FindCustomQuery::FindCustomQuery(const QArray<int>& ints)
00404         : OSQLQuery(), m_uids( ints ){
00405     }
00406     FindCustomQuery::~FindCustomQuery() {
00407     }
00408     QString FindCustomQuery::query()const{
00409         return single(); // Multiple requests not supported !
00410     }
00411     QString FindCustomQuery::single()const{
00412         QString qu = "select uid, type, value from custom_data where uid = ";
00413         qu += QString::number(m_uid);
00414         return qu;
00415     }
00416 
00417 };
00418 
00419 
00420 namespace Opie {
00421 OPimTodoAccessBackendSQL::OPimTodoAccessBackendSQL( const QString& file )
00422     : OPimTodoAccessBackend(),/* m_dict(15),*/ m_driver(NULL), m_dirty(true)
00423 {
00424     QString fi = file;
00425     if ( fi.isEmpty() )
00426         fi = Global::applicationFileName( "todolist", "todolist.db" );
00427     OSQLManager man;
00428     m_driver = man.standard();
00429     m_driver->setUrl(fi);
00430     // fillDict();
00431 }
00432 
00433 OPimTodoAccessBackendSQL::~OPimTodoAccessBackendSQL(){
00434     if( m_driver )
00435         delete m_driver;
00436 }
00437 
00438 bool OPimTodoAccessBackendSQL::load(){
00439     if (!m_driver->open() )
00440         return false;
00441 
00442     CreateQuery creat;
00443     OSQLResult res = m_driver->query(&creat );
00444 
00445     m_dirty = true;
00446     return true;
00447 }
00448 bool OPimTodoAccessBackendSQL::reload(){
00449     return load();
00450 }
00451 
00452 bool OPimTodoAccessBackendSQL::save(){
00453     return m_driver->close();  // Shouldn't m_driver->sync be better than close ? (eilers)
00454 }
00455 QArray<int> OPimTodoAccessBackendSQL::allRecords()const {
00456     if (m_dirty )
00457         update();
00458 
00459     return m_uids;
00460 }
00461 // QArray<int> OPimTodoAccessBackendSQL::queryByExample( const UIDArray& uidlist, const OPimTodo& , int, const QDateTime& ){
00462 //     QArray<int> ints(0);
00463 //     return ints;
00464 // }
00465 OPimTodo OPimTodoAccessBackendSQL::find(int uid ) const{
00466     FindQuery query( uid );
00467     return parseResultAndCache( uid, m_driver->query(&query) );
00468 }
00469 
00470 // Remember: uid is already in the list of uids, called ints !
00471 OPimTodo OPimTodoAccessBackendSQL::find( int uid, const QArray<int>& ints,
00472                                    uint cur, Frontend::CacheDirection dir ) const{
00473     uint CACHE = readAhead();
00474     odebug << "searching for " << uid << "" << oendl;
00475     QArray<int> search( CACHE );
00476     uint size =0;
00477 
00478     // we try to cache CACHE items
00479     switch( dir ) {
00480     /* forward */
00481     case Frontend::Forward:
00482         for (uint i = cur; i < ints.count() && size < CACHE; i++ ) {
00483             search[size] = ints[i];
00484             size++;
00485         }
00486         break;
00487     /* reverse */
00488     case Frontend::Reverse:
00489         for (uint i = cur; i != 0 && size <  CACHE; i-- ) {
00490             search[size] = ints[i];
00491             size++;
00492         }
00493         break;
00494     }
00495 
00496     search.resize( size );
00497     FindQuery query( search );
00498     OSQLResult res = m_driver->query( &query  );
00499     if ( res.state() != OSQLResult::Success )
00500         return OPimTodo();
00501 
00502     return parseResultAndCache( uid, res );
00503 }
00504 
00505 void OPimTodoAccessBackendSQL::clear() {
00506     ClearQuery cle;
00507     OSQLResult res = m_driver->query( &cle );
00508     CreateQuery qu;
00509     res = m_driver->query(&qu);
00510 }
00511 bool OPimTodoAccessBackendSQL::add( const OPimTodo& t) {
00512     InsertQuery ins( t );
00513     OSQLResult res = m_driver->query( &ins );
00514 
00515     if ( res.state() == OSQLResult::Failure )
00516         return false;
00517 
00518     int c = m_uids.count();
00519     m_uids.resize( c+1 );
00520     m_uids[c] = t.uid();
00521 
00522     return true;
00523 }
00524 bool OPimTodoAccessBackendSQL::remove( int uid ) {
00525     RemoveQuery rem( uid );
00526     OSQLResult res = m_driver->query(&rem );
00527 
00528     if ( res.state() == OSQLResult::Failure )
00529         return false;
00530 
00531     m_dirty = true;
00532     return true;
00533 }
00534 /*
00535  * FIXME better set query
00536  * but we need the cache for that
00537  * now we remove
00538  */
00539 bool OPimTodoAccessBackendSQL::replace( const OPimTodo& t) {
00540     remove( t.uid() );
00541     bool b= add(t);
00542     m_dirty = false; // we changed some stuff but the UID stayed the same
00543     return b;
00544 }
00545 QArray<int> OPimTodoAccessBackendSQL::overDue()const {
00546     OverDueQuery qu;
00547     return uids( m_driver->query(&qu ) );
00548 }
00549 QArray<int> OPimTodoAccessBackendSQL::effectiveToDos( const QDate& s,
00550                                                    const QDate& t,
00551                                                    bool u)const {
00552     EffQuery ef(s, t, u );
00553     return uids (m_driver->query(&ef) );
00554 }
00555 
00556 #if 0
00557 /*
00558  *
00559  */
00560 QArray<int> OPimTodoAccessBackendSQL::sorted( bool asc, int sortOrder,
00561                                            int sortFilter, int cat ) {
00562     odebug << "sorted " << asc << ", " << sortOrder << "" << oendl;
00563     QString query;
00564     query = "select uid from todolist WHERE ";
00565 
00566     /*
00567      * Sort Filter stuff
00568      * not that straight forward
00569      * FIXME: Replace magic numbers
00570      *
00571      */
00572     /* Category */
00573     if ( sortFilter & OPimTodoAccess::FilterCategory ) {
00574         QString str;
00575         if (cat != 0 ) str = QString::number( cat );
00576         query += " categories like '%" +str+"%' AND";
00577     }
00578     /* Show only overdue */
00579     if ( sortFilter & OPimTodoAccess::OnlyOverDue ) {
00580         QDate date = QDate::currentDate();
00581         QString due;
00582         QString base;
00583         base = QString("DueDate <= '%1-%2-%3' AND completed = 0")
00584         .arg( QString::number( date.year() ).rightJustify( 4, '0' ) )
00585         .arg( QString::number( date.month() ).rightJustify( 2, '0' ) )
00586         .arg( QString::number( date.day() ).rightJustify( 2, '0' ) );
00587         query += " " + base + " AND";
00588     }
00589     /* not show completed */
00590     if ( sortFilter & OPimTodoAccess::DoNotShowCompleted ) {
00591         query += " completed = 0 AND";
00592     }else{
00593        query += " ( completed = 1 OR  completed = 0) AND";
00594     }
00595     /* strip the end */
00596     query = query.remove( query.length()-3, 3 );
00597 
00598 
00599     /*
00600      * sort order stuff
00601      * quite straight forward
00602      */
00603     query += "ORDER BY ";
00604     switch( sortOrder ) {
00605         /* completed */
00606     case OPimTodoAccess::Completed:
00607         query += "completed";
00608         break;
00609     case OPimTodoAccess::Priority:
00610         query += "priority";
00611         break;
00612     case OPimTodoAccess::SortSummary:
00613         query += "summary";
00614         break;
00615     case OPimTodoAccess::Deadline:
00616         query += "DueDate";
00617         break;
00618     }
00619 
00620     if ( !asc )
00621         query += " DESC";
00622 
00623 
00624     odebug << query << oendl;
00625     OSQLRawQuery raw(query );
00626     return uids( m_driver->query(&raw) );
00627 }
00628 #endif
00629 
00630 
00631 bool OPimTodoAccessBackendSQL::date( QDate& da, const QString& str ) const{
00632     if ( str == "0000-00-00" )
00633         return false;
00634     else{
00635         int day, year, month;
00636         QStringList list = QStringList::split("-", str );
00637         year = list[0].toInt();
00638         month = list[1].toInt();
00639         day = list[2].toInt();
00640         da.setYMD( year, month, day );
00641         return true;
00642     }
00643 }
00644 
00645 
00646 OPimTodo OPimTodoAccessBackendSQL::parseResultAndCache( int uid, const OSQLResult& res ) const{
00647     if ( res.state() == OSQLResult::Failure ) {
00648         OPimTodo to;
00649         return to;
00650     }
00651 
00652     OPimTodo retTodo;
00653 
00654     OSQLResultItem::ValueList list = res.results();
00655     OSQLResultItem::ValueList::Iterator it = list.begin();
00656     OPimTodo to, tmp;
00657 
00658     for ( ; it != list.end(); ++it ) {
00659         OPimTodo newTodo = parse( (*it) );
00660         cache( newTodo );
00661         if ( newTodo.uid() == uid )
00662                 retTodo = newTodo;
00663     }
00664     return retTodo;
00665 }
00666 OPimTodo OPimTodoAccessBackendSQL::parse( OSQLResultItem& item )const {
00667 
00668     // Request information from addressbook table and create the OPimTodo-object.
00669 
00670     bool hasDueDate = false; QDate dueDate = QDate::currentDate();
00671     hasDueDate = date( dueDate, item.data("DueDate") );
00672     QStringList cats = QStringList::split(";", item.data("categories") );
00673 
00674     OPimTodo to( (bool)item.data("completed").toInt(), item.data("priority").toInt(),
00675               cats, item.data("summary"), item.data("description"),
00676               item.data("progress").toUShort(), hasDueDate, dueDate,
00677               item.data("uid").toInt() );
00678 
00679     bool isOk;
00680     int prioInt = QString( item.data("priority") ).toInt( &isOk );
00681     if ( isOk )
00682         to.setPriority( prioInt );
00683 
00684     bool hasStartDate = false; QDate startDate = QDate::currentDate();
00685     hasStartDate = date( startDate, item.data("startdate") );
00686     bool hasCompletedDate = false; QDate completedDate = QDate::currentDate();
00687     hasCompletedDate = date( completedDate, item.data("completeddate") );
00688 
00689     if ( hasStartDate )
00690         to.setStartDate( startDate );
00691     if ( hasCompletedDate )
00692         to.setCompletedDate( completedDate );
00693 
00694     OPimNotifyManager& manager = to.notifiers();
00695     manager.alarmsFromString( item.data("alarms") );
00696     manager.remindersFromString( item.data("reminders") );
00697 
00698     OPimState pimState;
00699     pimState.setState( QString( item.data("state") ).toInt() );
00700     to.setState( pimState );
00701 
00702     QMap<int, QString> recMap;
00703     recMap.insert( OPimRecurrence::RType      , item.data("RType") );
00704     recMap.insert( OPimRecurrence::RWeekdays  , item.data("RWeekdays") );
00705     recMap.insert( OPimRecurrence::RPosition  , item.data("RPosition") );
00706     recMap.insert( OPimRecurrence::RFreq      , item.data("RFreq") );
00707     recMap.insert( OPimRecurrence::RHasEndDate, item.data("RHasEndDate") );
00708     recMap.insert( OPimRecurrence::EndDate    , item.data("EndDate") );
00709     recMap.insert( OPimRecurrence::Created    , item.data("Created") );
00710     recMap.insert( OPimRecurrence::Exceptions , item.data("Exceptions") );
00711 
00712     OPimRecurrence recur;
00713     recur.fromMap( recMap );
00714     to.setRecurrence( recur );
00715 
00716     // Finally load the custom-entries for this UID and put it into the created object
00717     to.setExtraMap( requestCustom( to.uid() ) );
00718 
00719     return to;
00720 }
00721 
00722 // FIXME: Where is the difference to "find" ? (eilers)
00723 OPimTodo OPimTodoAccessBackendSQL::todo( int uid )const {
00724     FindQuery find( uid );
00725     return parseResultAndCache( uid, m_driver->query(&find) );
00726 }
00727 
00728 
00729 /*
00730  * need to be const so let's fool the
00731  * compiler :(
00732  */
00733 void OPimTodoAccessBackendSQL::update()const {
00734     ((OPimTodoAccessBackendSQL*)this)->m_dirty = false;
00735     LoadQuery lo;
00736     OSQLResult res = m_driver->query(&lo);
00737     if ( res.state() != OSQLResult::Success )
00738         return;
00739 
00740     ((OPimTodoAccessBackendSQL*)this)->m_uids = uids( res );
00741 }
00742 QArray<int> OPimTodoAccessBackendSQL::uids( const OSQLResult& res) const{
00743 
00744     OSQLResultItem::ValueList list = res.results();
00745     OSQLResultItem::ValueList::Iterator it;
00746     QArray<int> ints(list.count() );
00747 
00748     int i = 0;
00749     for (it = list.begin(); it != list.end(); ++it ) {
00750         ints[i] =  (*it).data("uid").toInt();
00751         i++;
00752     }
00753     return ints;
00754 }
00755 
00756 QArray<int> OPimTodoAccessBackendSQL::matchRegexp(  const QRegExp &r ) const
00757 {
00758     QString qu = "SELECT uid FROM todolist WHERE (";
00759 
00760     // Does it make sense to search other fields, too ?
00761     qu += " rlike(\""+ r.pattern() + "\",\"description\") OR";
00762     qu += " rlike(\""+ r.pattern() + "\",\"summary\")";
00763 
00764     qu += ")";
00765 
00766     OSQLRawQuery raw( qu );
00767     OSQLResult res = m_driver->query( &raw );
00768 
00769     return uids( res );
00770 }
00771 
00772 void OPimTodoAccessBackendSQL::removeAllCompleted(){
00773     // First we need the uids from all entries which are
00774     // completed. Then, we just have to remove them...
00775 
00776     QString qu = "SELECT uid FROM todolist WHERE completed = 1";
00777 
00778     OSQLRawQuery raw( qu );
00779     OSQLResult res = m_driver->query( &raw );
00780 
00781     QArray<int> completed_uids = uids( res );
00782 
00783     if ( completed_uids.size() == 0 )
00784         return;
00785 
00786     qu = "DELETE FROM todolist WHERE (";
00787     QString query;
00788 
00789     for ( uint i = 0; i < completed_uids.size(); i++ ){
00790         if ( !query.isEmpty() )
00791             query += " OR ";
00792         query += QString( "uid = %1" ).arg( completed_uids[i] );
00793     }
00794     qu += query + " );";
00795 
00796     // Put remove of custom entries in this query to speed up..
00797     qu += "DELETE FORM custom_data WHERE (";
00798     query = "";
00799 
00800     for ( uint i = 0; i < completed_uids.size(); i++ ){
00801         if ( !query.isEmpty() )
00802             query += " OR ";
00803         query += QString( "uid = %1" ).arg( completed_uids[i] );
00804     }
00805     qu += query + " );";
00806 
00807     OSQLRawQuery raw2( qu );
00808     res = m_driver->query( &raw2 );
00809 
00810     if ( res.state() == OSQLResult::Failure )
00811         owarn << "OPimTodoAccessBackendSQL::removeAllCompleted():Failure in query: " << qu << "" << oendl;
00812 
00813 }
00814 
00815 
00816 QMap<QString, QString>  OPimTodoAccessBackendSQL::requestCustom( int uid ) const
00817 {
00818     QMap<QString, QString> customMap;
00819 
00820     FindCustomQuery query( uid );
00821     OSQLResult res_custom = m_driver->query( &query );
00822 
00823     if ( res_custom.state() == OSQLResult::Failure ) {
00824         owarn << "OSQLResult::Failure in find query !!" << oendl;
00825         return QMap<QString, QString>();
00826     }
00827 
00828     OSQLResultItem::ValueList list = res_custom.results();
00829     OSQLResultItem::ValueList::Iterator it = list.begin();
00830     for ( ; it != list.end(); ++it )
00831         customMap.insert( (*it).data( "type" ), (*it).data( "value" ) );
00832 
00833 
00834     return customMap;
00835 }
00836 
00837 
00838 
00839 
00840 }

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