summaryrefslogtreecommitdiff
authoreilers <eilers>2004-08-29 12:48:43 (UTC)
committer eilers <eilers>2004-08-29 12:48:43 (UTC)
commit45327ef3c0f093fc227682f79158632afc09e6d1 (patch) (side-by-side diff)
treebdfe4e602abcef87117528d2c9c4166820488d11
parentdd159675e6e3c361bc20eaa6994265e73b6599ef (diff)
downloadopie-45327ef3c0f093fc227682f79158632afc09e6d1.zip
opie-45327ef3c0f093fc227682f79158632afc09e6d1.tar.gz
opie-45327ef3c0f093fc227682f79158632afc09e6d1.tar.bz2
Fixed some quirks..
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opiepim/backend/otodoaccesssql.cpp11
1 files changed, 8 insertions, 3 deletions
diff --git a/libopie2/opiepim/backend/otodoaccesssql.cpp b/libopie2/opiepim/backend/otodoaccesssql.cpp
index 6f65c48..12a8bea 100644
--- a/libopie2/opiepim/backend/otodoaccesssql.cpp
+++ b/libopie2/opiepim/backend/otodoaccesssql.cpp
@@ -204,546 +204,551 @@ namespace {
}
/*
* converts from a OPimTodo to a query
* we leave out X-Ref + Maintainer
* FIXME: Implement/Finish toMap()/fromMap() into OpimTodo to move the encoding
* decoding stuff there.. (eilers)
*/
QString InsertQuery::query()const{
int year, month, day;
year = month = day = 0;
if (m_todo.hasDueDate() ) {
QDate date = m_todo.dueDate();
year = date.year();
month = date.month();
day = date.day();
}
int sYear = 0, sMonth = 0, sDay = 0;
if( m_todo.hasStartDate() ){
QDate sDate = m_todo.startDate();
sYear = sDate.year();
sMonth= sDate.month();
sDay = sDate.day();
}
int eYear = 0, eMonth = 0, eDay = 0;
if( m_todo.hasCompletedDate() ){
QDate eDate = m_todo.completedDate();
eYear = eDate.year();
eMonth= eDate.month();
eDay = eDate.day();
}
QString qu;
QMap<int, QString> recMap = m_todo.recurrence().toMap();
qu = "insert into todolist VALUES("
+ QString::number( m_todo.uid() ) + ","
+ "'" + m_todo.idsToString( m_todo.categories() ) + "'" + ","
+ QString::number( m_todo.isCompleted() ) + ","
+ "'" + m_todo.description() + "'" + ","
+ "'" + m_todo.summary() + "'" + ","
+ QString::number(m_todo.priority() ) + ","
+ "'" + QString::number(year).rightJustify( 4, '0' ) + "-"
+ QString::number(month).rightJustify( 2, '0' )
+ "-" + QString::number( day ).rightJustify( 2, '0' )+ "'" + ","
+ QString::number( m_todo.progress() ) + ","
+ QString::number( m_todo.state().state() ) + ","
+ "'" + recMap[ OPimRecurrence::RType ] + "'" + ","
+ "'" + recMap[ OPimRecurrence::RWeekdays ] + "'" + ","
+ "'" + recMap[ OPimRecurrence::RPosition ] + "'" + ","
+ "'" + recMap[ OPimRecurrence::RFreq ] + "'" + ","
+ "'" + recMap[ OPimRecurrence::RHasEndDate ] + "'" + ","
+ "'" + recMap[ OPimRecurrence::EndDate ] + "'" + ","
+ "'" + recMap[ OPimRecurrence::Created ] + "'" + ","
+ "'" + recMap[ OPimRecurrence::Exceptions ] + "'" + ",";
if ( m_todo.hasNotifiers() ) {
OPimNotifyManager manager = m_todo.notifiers();
qu += "'" + manager.remindersToString() + "'" + ","
+ "'" + manager.alarmsToString() + "'" + ",";
}
else{
qu += QString( "''" ) + ","
+ "''" + ",";
}
qu += QString( "''" ) + QString( "," ) // Maintainers (cur. not supported !)
+ "'" + QString::number(sYear).rightJustify( 4, '0' ) + "-"
+ QString::number(sMonth).rightJustify( 2, '0' )
+ "-" + QString::number(sDay).rightJustify( 2, '0' )+ "'" + ","
+ "'" + QString::number(eYear).rightJustify( 4, '0' ) + "-"
+ QString::number(eMonth).rightJustify( 2, '0' )
+ "-"+QString::number(eDay).rightJustify( 2, '0' ) + "'"
+ "); ";
// Save custom Entries:
int id = 0;
id = 0;
QMap<QString, QString> customMap = m_todo.toExtraMap();
for( QMap<QString, QString>::Iterator it = customMap.begin();
it != customMap.end(); ++it ){
qu += "insert into custom_data VALUES("
+ QString::number( m_todo.uid() )
+ ","
+ QString::number( id++ )
+ ",'"
+ it.key()
+ "',"
+ "0" // Priority for future enhancements
+ ",'"
+ it.data()
+ "');";
}
odebug << "add " << qu << "" << oendl;
return qu;
}
RemoveQuery::RemoveQuery(int uid )
: OSQLQuery(), m_uid( uid ) {}
RemoveQuery::~RemoveQuery() {}
QString RemoveQuery::query()const {
QString qu = "DELETE FROM todolist WHERE uid = " + QString::number(m_uid) + " ;";
qu += "DELETE FROM custom_data WHERE uid = " + QString::number(m_uid);
return qu;
}
ClearQuery::ClearQuery()
: OSQLQuery() {}
ClearQuery::~ClearQuery() {}
QString ClearQuery::query()const {
QString qu = "drop table todolist";
return qu;
}
FindQuery::FindQuery(int uid)
: OSQLQuery(), m_uid(uid ) {
}
FindQuery::FindQuery(const QArray<int>& ints)
: OSQLQuery(), m_uids(ints){
}
FindQuery::~FindQuery() {
}
QString FindQuery::query()const{
if (m_uids.count() == 0 )
return single();
else
return multi();
}
QString FindQuery::single()const{
QString qu = "select * from todolist where uid = " + QString::number(m_uid);
return qu;
}
QString FindQuery::multi()const {
QString qu = "select * from todolist where ";
for (uint i = 0; i < m_uids.count(); i++ ) {
qu += " UID = " + QString::number( m_uids[i] ) + " OR";
}
qu.remove( qu.length()-2, 2 );
return qu;
}
OverDueQuery::OverDueQuery(): OSQLQuery() {}
OverDueQuery::~OverDueQuery() {}
QString OverDueQuery::query()const {
QDate date = QDate::currentDate();
QString str;
str = QString("select uid from todolist where DueDate ='%1-%2-%3'")
.arg( QString::number( date.year() ).rightJustify( 4, '0' ) )
.arg( QString::number( date.month() ).rightJustify( 2, '0' ) )
.arg( QString::number( date.day() ) .rightJustify( 2, '0' ) );
return str;
}
EffQuery::EffQuery( const QDate& start, const QDate& end, bool inc )
: OSQLQuery(), m_start( start ), m_end( end ),m_inc(inc) {}
EffQuery::~EffQuery() {}
QString EffQuery::query()const {
return m_inc ? with() : out();
}
QString EffQuery::with()const {
QString str;
str = QString("select uid from todolist where ( DueDate >= '%1-%2-%3' AND DueDate <= '%4-%5-%6' ) OR DueDate = '0-0-0' ")
.arg( QString::number( m_start.year() ).rightJustify( 4, '0' ) )
.arg( QString::number( m_start.month() ).rightJustify( 2, '0' ) )
.arg( QString::number( m_start.day() ).rightJustify( 2, '0' ) )
.arg( QString::number( m_end.year() ).rightJustify( 4, '0' ) )
.arg( QString::number( m_end.month() ).rightJustify( 2, '0' ) )
.arg( QString::number( m_end.day() ).rightJustify( 2, '0' ) );
return str;
}
QString EffQuery::out()const {
QString str;
str = QString("select uid from todolist where DueDate >= '%1-%2-%3' AND DueDate <= '%4-%5-%6'")
.arg( QString::number( m_start.year() ).rightJustify( 4, '0' ) )
.arg( QString::number( m_start.month() ).rightJustify( 2, '0' ) )
.arg( QString::number( m_start.day() ).rightJustify( 2, '0' ) )
.arg( QString::number( m_end.year() ).rightJustify( 4, '0' ) )
.arg( QString::number( m_end.month() ).rightJustify( 2, '0' ) )
.arg( QString::number( m_end.day() ).rightJustify( 2, '0' ) );
return str;
}
FindCustomQuery::FindCustomQuery(int uid)
: OSQLQuery(), m_uid( uid ) {
}
FindCustomQuery::FindCustomQuery(const QArray<int>& ints)
: OSQLQuery(), m_uids( ints ){
}
FindCustomQuery::~FindCustomQuery() {
}
QString FindCustomQuery::query()const{
return single(); // Multiple requests not supported !
}
QString FindCustomQuery::single()const{
QString qu = "select uid, type, value from custom_data where uid = ";
qu += QString::number(m_uid);
return qu;
}
};
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) );
}
+
+// 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 0: // FIXME: Not a good style to use magic numbers here (eilers)
+ 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 1: // FIXME: Not a good style to use magic numbers here (eilers)
+ 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;
- return todo( res );
+ todo( res ); //FIXME: Don't like polymorphism here. It makes the code hard to read here..(eilers)
+ return cacheFind( uid );
}
+
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) {
EffQuery ef(s, t, u );
return uids (m_driver->query(&ef) );
}
/*
*
*/
QArray<int> OPimTodoAccessBackendSQL::sorted( bool asc, int sortOrder,
int sortFilter, int cat ) {
odebug << "sorted " << asc << ", " << sortOrder << "" << oendl;
QString query;
query = "select uid from todolist WHERE ";
/*
* Sort Filter stuff
* not that straight forward
* FIXME: Replace magic numbers
*
*/
/* Category */
if ( sortFilter & 1 ) {
QString str;
if (cat != 0 ) str = QString::number( cat );
query += " categories like '%" +str+"%' AND";
}
/* Show only overdue */
if ( sortFilter & 2 ) {
QDate date = QDate::currentDate();
QString due;
QString base;
base = QString("DueDate <= '%1-%2-%3' AND completed = 0")
.arg( QString::number( date.year() ).rightJustify( 4, '0' ) )
.arg( QString::number( date.month() ).rightJustify( 2, '0' ) )
.arg( QString::number( date.day() ).rightJustify( 2, '0' ) );
query += " " + base + " AND";
}
/* not show completed */
if ( sortFilter & 4 ) {
query += " completed = 0 AND";
}else{
query += " ( completed = 1 OR completed = 0) AND";
}
/* 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{
if ( res.state() == OSQLResult::Failure ) {
OPimTodo to;
return to;
}
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) ) );
}
return to;
}
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;
}
OPimTodo OPimTodoAccessBackendSQL::todo( int uid )const {
FindQuery find( uid );
return todo( 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;