summaryrefslogtreecommitdiff
path: root/libopie2/opiepim/backend/otodoaccesssql.cpp
Side-by-side diff
Diffstat (limited to 'libopie2/opiepim/backend/otodoaccesssql.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opiepim/backend/otodoaccesssql.cpp20
1 files changed, 13 insertions, 7 deletions
diff --git a/libopie2/opiepim/backend/otodoaccesssql.cpp b/libopie2/opiepim/backend/otodoaccesssql.cpp
index 12a8bea..4e3e47b 100644
--- a/libopie2/opiepim/backend/otodoaccesssql.cpp
+++ b/libopie2/opiepim/backend/otodoaccesssql.cpp
@@ -409,136 +409,135 @@ namespace {
namespace Opie {
OPimTodoAccessBackendSQL::OPimTodoAccessBackendSQL( const QString& file )
: OPimTodoAccessBackend(),/* m_dict(15),*/ m_driver(NULL), m_dirty(true)
{
QString fi = file;
if ( fi.isEmpty() )
fi = Global::applicationFileName( "todolist", "todolist.db" );
OSQLManager man;
m_driver = man.standard();
m_driver->setUrl(fi);
// fillDict();
}
OPimTodoAccessBackendSQL::~OPimTodoAccessBackendSQL(){
if( m_driver )
delete m_driver;
}
bool OPimTodoAccessBackendSQL::load(){
if (!m_driver->open() )
return false;
CreateQuery creat;
OSQLResult res = m_driver->query(&creat );
m_dirty = true;
return true;
}
bool OPimTodoAccessBackendSQL::reload(){
return load();
}
bool OPimTodoAccessBackendSQL::save(){
return m_driver->close(); // Shouldn't m_driver->sync be better than close ? (eilers)
}
QArray<int> OPimTodoAccessBackendSQL::allRecords()const {
if (m_dirty )
update();
return m_uids;
}
QArray<int> OPimTodoAccessBackendSQL::queryByExample( const OPimTodo& , int, const QDateTime& ){
QArray<int> ints(0);
return ints;
}
OPimTodo OPimTodoAccessBackendSQL::find(int uid ) const{
FindQuery query( uid );
- return todo( m_driver->query(&query) );
+ return parseResultAndCache( uid, m_driver->query(&query) );
}
// Remember: uid is already in the list of uids, called ints !
OPimTodo OPimTodoAccessBackendSQL::find( int uid, const QArray<int>& ints,
uint cur, Frontend::CacheDirection dir ) const{
uint CACHE = readAhead();
odebug << "searching for " << uid << "" << oendl;
QArray<int> search( CACHE );
uint size =0;
OPimTodo to;
// we try to cache CACHE items
switch( dir ) {
/* forward */
case Frontend::Forward:
for (uint i = cur; i < ints.count() && size < CACHE; i++ ) {
odebug << "size " << size << " " << ints[i] << "" << oendl;
search[size] = ints[i];
size++;
}
break;
/* reverse */
case Frontend::Reverse:
for (uint i = cur; i != 0 && size < CACHE; i-- ) {
search[size] = ints[i];
size++;
}
break;
}
search.resize( size );
FindQuery query( search );
OSQLResult res = m_driver->query( &query );
if ( res.state() != OSQLResult::Success )
return to;
- todo( res ); //FIXME: Don't like polymorphism here. It makes the code hard to read here..(eilers)
- return cacheFind( uid );
+ return parseResultAndCache( uid, res );
}
void OPimTodoAccessBackendSQL::clear() {
ClearQuery cle;
OSQLResult res = m_driver->query( &cle );
CreateQuery qu;
res = m_driver->query(&qu);
}
bool OPimTodoAccessBackendSQL::add( const OPimTodo& t) {
InsertQuery ins( t );
OSQLResult res = m_driver->query( &ins );
if ( res.state() == OSQLResult::Failure )
return false;
int c = m_uids.count();
m_uids.resize( c+1 );
m_uids[c] = t.uid();
return true;
}
bool OPimTodoAccessBackendSQL::remove( int uid ) {
RemoveQuery rem( uid );
OSQLResult res = m_driver->query(&rem );
if ( res.state() == OSQLResult::Failure )
return false;
m_dirty = true;
return true;
}
/*
* FIXME better set query
* but we need the cache for that
* now we remove
*/
bool OPimTodoAccessBackendSQL::replace( const OPimTodo& t) {
remove( t.uid() );
bool b= add(t);
m_dirty = false; // we changed some stuff but the UID stayed the same
return b;
}
QArray<int> OPimTodoAccessBackendSQL::overDue() {
OverDueQuery qu;
return uids( m_driver->query(&qu ) );
}
QArray<int> OPimTodoAccessBackendSQL::effectiveToDos( const QDate& s,
const QDate& t,
bool u) {
@@ -585,176 +584,183 @@ QArray<int> OPimTodoAccessBackendSQL::sorted( bool asc, int sortOrder,
}
/* strip the end */
query = query.remove( query.length()-3, 3 );
/*
* sort order stuff
* quite straight forward
*/
query += "ORDER BY ";
switch( sortOrder ) {
/* completed */
case 0:
query += "completed";
break;
case 1:
query += "priority";
break;
case 2:
query += "summary";
break;
case 3:
query += "DueDate";
break;
}
if ( !asc ) {
odebug << "not ascending!" << oendl;
query += " DESC";
}
odebug << query << oendl;
OSQLRawQuery raw(query );
return uids( m_driver->query(&raw) );
}
bool OPimTodoAccessBackendSQL::date( QDate& da, const QString& str ) const{
if ( str == "0-0-0" )
return false;
else{
int day, year, month;
QStringList list = QStringList::split("-", str );
year = list[0].toInt();
month = list[1].toInt();
day = list[2].toInt();
da.setYMD( year, month, day );
return true;
}
}
-OPimTodo OPimTodoAccessBackendSQL::todo( const OSQLResult& res ) const{
+OPimTodo OPimTodoAccessBackendSQL::parseResultAndCache( int uid, const OSQLResult& res ) const{
if ( res.state() == OSQLResult::Failure ) {
OPimTodo to;
return to;
}
+ OPimTodo retTodo;
+
OSQLResultItem::ValueList list = res.results();
OSQLResultItem::ValueList::Iterator it = list.begin();
odebug << "todo1" << oendl;
OPimTodo to = todo( (*it) );
cache( to );
++it;
for ( ; it != list.end(); ++it ) {
odebug << "caching" << oendl;
- cache( todo( (*it) ) );
+ OPimTodo newTodo = todo( (*it) );
+ cache( newTodo );
+ if ( newTodo.uid() == uid )
+ retTodo = newTodo;
}
- return to;
+ return retTodo;
}
OPimTodo OPimTodoAccessBackendSQL::todo( OSQLResultItem& item )const {
odebug << "todo(ResultItem)" << oendl;
// Request information from addressbook table and create the OPimTodo-object.
bool hasDueDate = false; QDate dueDate = QDate::currentDate();
hasDueDate = date( dueDate, item.data("DueDate") );
QStringList cats = QStringList::split(";", item.data("categories") );
odebug << "Item is completed: " << item.data("completed").toInt() << "" << oendl;
OPimTodo to( (bool)item.data("completed").toInt(), item.data("priority").toInt(),
cats, item.data("summary"), item.data("description"),
item.data("progress").toUShort(), hasDueDate, dueDate,
item.data("uid").toInt() );
bool isOk;
int prioInt = QString( item.data("priority") ).toInt( &isOk );
if ( isOk )
to.setPriority( prioInt );
bool hasStartDate = false; QDate startDate = QDate::currentDate();
hasStartDate = date( startDate, item.data("startdate") );
bool hasCompletedDate = false; QDate completedDate = QDate::currentDate();
hasCompletedDate = date( completedDate, item.data("completeddate") );
if ( hasStartDate )
to.setStartDate( startDate );
if ( hasCompletedDate )
to.setCompletedDate( completedDate );
OPimNotifyManager& manager = to.notifiers();
manager.alarmsFromString( item.data("alarms") );
manager.remindersFromString( item.data("reminders") );
OPimState pimState;
pimState.setState( QString( item.data("state") ).toInt() );
to.setState( pimState );
QMap<int, QString> recMap;
recMap.insert( OPimRecurrence::RType , item.data("RType") );
recMap.insert( OPimRecurrence::RWeekdays , item.data("RWeekdays") );
recMap.insert( OPimRecurrence::RPosition , item.data("RPosition") );
recMap.insert( OPimRecurrence::RFreq , item.data("RFreq") );
recMap.insert( OPimRecurrence::RHasEndDate, item.data("RHasEndDate") );
recMap.insert( OPimRecurrence::EndDate , item.data("EndDate") );
recMap.insert( OPimRecurrence::Created , item.data("Created") );
recMap.insert( OPimRecurrence::Exceptions , item.data("Exceptions") );
OPimRecurrence recur;
recur.fromMap( recMap );
to.setRecurrence( recur );
// Finally load the custom-entries for this UID and put it into the created object
to.setExtraMap( requestCustom( to.uid() ) );
return to;
}
+
+// FIXME: Where is the difference to "find" ? (eilers)
OPimTodo OPimTodoAccessBackendSQL::todo( int uid )const {
FindQuery find( uid );
- return todo( m_driver->query(&find) );
+ return parseResultAndCache( uid, m_driver->query(&find) );
}
/*
* update the dict
*/
void OPimTodoAccessBackendSQL::fillDict() {
#if 0
/* initialize dict */
/*
* UPDATE dict if you change anything!!!
* FIXME: Isn't this dict obsolete ? (eilers)
*/
m_dict.setAutoDelete( TRUE );
m_dict.insert("Categories" , new int(OPimTodo::Category) );
m_dict.insert("Uid" , new int(OPimTodo::Uid) );
m_dict.insert("HasDate" , new int(OPimTodo::HasDate) );
m_dict.insert("Completed" , new int(OPimTodo::Completed) );
m_dict.insert("Description" , new int(OPimTodo::Description) );
m_dict.insert("Summary" , new int(OPimTodo::Summary) );
m_dict.insert("Priority" , new int(OPimTodo::Priority) );
m_dict.insert("DateDay" , new int(OPimTodo::DateDay) );
m_dict.insert("DateMonth" , new int(OPimTodo::DateMonth) );
m_dict.insert("DateYear" , new int(OPimTodo::DateYear) );
m_dict.insert("Progress" , new int(OPimTodo::Progress) );
m_dict.insert("Completed", new int(OPimTodo::Completed) ); // Why twice ? (eilers)
m_dict.insert("CrossReference", new int(OPimTodo::CrossReference) );
// m_dict.insert("HasAlarmDateTime",new int(OPimTodo::HasAlarmDateTime) ); // old stuff (eilers)
// m_dict.insert("AlarmDateTime", new int(OPimTodo::AlarmDateTime) ); // old stuff (eilers)
#endif
}
/*
* need to be const so let's fool the
* compiler :(
*/
void OPimTodoAccessBackendSQL::update()const {
((OPimTodoAccessBackendSQL*)this)->m_dirty = false;
LoadQuery lo;
OSQLResult res = m_driver->query(&lo);
if ( res.state() != OSQLResult::Success )
return;
((OPimTodoAccessBackendSQL*)this)->m_uids = uids( res );
}
QArray<int> OPimTodoAccessBackendSQL::uids( const OSQLResult& res) const{
OSQLResultItem::ValueList list = res.results();
OSQLResultItem::ValueList::Iterator it;