-rw-r--r-- | libopie/tododb.cpp | 25 | ||||
-rw-r--r-- | libopie/todoevent.cpp | 27 | ||||
-rw-r--r-- | libopie/todoevent.h | 13 | ||||
-rw-r--r-- | libopie/todovcalresource.cpp | 2 |
4 files changed, 45 insertions, 22 deletions
diff --git a/libopie/tododb.cpp b/libopie/tododb.cpp index 10ea2f0..7814c4f 100644 --- a/libopie/tododb.cpp +++ b/libopie/tododb.cpp @@ -1,225 +1,226 @@ #include <qdir.h> #include <opie/tododb.h> #include <opie/xmltree.h> #include <opie/todoresource.h> #include <qpe/palmtoprecord.h> #include <qpe/global.h> namespace { class FileToDoResource : public ToDoResource { public: FileToDoResource() {}; bool save(const QString &name, const QValueList<ToDoEvent> &m_todos ){ // prepare the XML XMLElement *tasks = new XMLElement( ); tasks->setTagName("Tasks" ); for( QValueList<ToDoEvent>::ConstIterator it = m_todos.begin(); it != m_todos.end(); ++it ){ XMLElement::AttributeMap map; XMLElement *task = new XMLElement(); map.insert( "Completed", QString::number((int)(*it).isCompleted() ) ); map.insert( "HasDate", QString::number((int)(*it).hasDate() ) ); map.insert( "Priority", QString::number( (*it).priority() ) ); - if(!(*it).category().isEmpty() ){ - QArray<int> arrat(1); - arrat = Qtopia::Record::idsFromString( (*it).category() ); - map.insert( "Categories", QString::number( arrat[0] ) ); - }else - map.insert( "Categories", QString::null ); + QArray<int> arrat = (*it).categories(); + QString attr; + for(uint i=0; i < arrat.count(); i++ ){ + attr.append(QString::number(arrat[i])+";" ); + } + if(!attr.isEmpty() ) // remove the last ; + attr.remove(attr.length()-1, 1 ); + map.insert( "Categories", attr ); + //else + //map.insert( "Categories", QString::null ); map.insert( "Description", (*it).description() ); if( (*it).hasDate() ){ map.insert("DateYear", QString::number( (*it).date().year() ) ); map.insert("DateMonth", QString::number( (*it).date().month() ) ); map.insert("DateDay", QString::number( (*it).date().day() ) ); } map.insert("Uid", QString::number( (*it).uid() ) ); task->setTagName("Task" ); task->setAttributes( map ); tasks->appendChild(task); } QFile file( name); if( file.open(IO_WriteOnly ) ){ QTextStream stream(&file ); stream << "<!DOCTYPE Tasks>" << endl; tasks->save(stream ); delete tasks; file.close(); return true; } return false; } QValueList<ToDoEvent> load( const QString &name ){ qWarning("loading tododb" ); QValueList<ToDoEvent> m_todos; XMLElement *root = XMLElement::load( name ); if(root != 0l ){ // start parsing qWarning("ToDoDB::load tagName(): %s", root->tagName().latin1() ); //if( root->tagName() == QString::fromLatin1("Tasks" ) ){// Start XMLElement *element = root->firstChild(); element = element->firstChild(); while( element ){ qWarning("ToDoDB::load element tagName() : %s", element->tagName().latin1() ); QString dummy; ToDoEvent event; bool ok; int dumInt; // completed dummy = element->attribute("Completed" ); dumInt = dummy.toInt(&ok ); if(ok ) event.setCompleted( dumInt == 0 ? false : true ); // hasDate dummy = element->attribute("HasDate" ); dumInt = dummy.toInt(&ok ); if(ok ) event.setHasDate( dumInt == 0 ? false: true ); // set the date bool hasDa = dumInt; if ( hasDa ) { //parse the date int year, day, month = 0; year = day = month; // year dummy = element->attribute("DateYear" ); dumInt = dummy.toInt(&ok ); if( ok ) year = dumInt; // month dummy = element->attribute("DateMonth" ); dumInt = dummy.toInt(&ok ); if(ok ) month = dumInt; dummy = element->attribute("DateDay" ); dumInt = dummy.toInt(&ok ); if(ok ) day = dumInt; // set the date QDate date( year, month, day ); event.setDate( date); } dummy = element->attribute("Priority" ); dumInt = dummy.toInt(&ok ); if(!ok ) dumInt = ToDoEvent::NORMAL; event.setPriority( dumInt ); //description dummy = element->attribute("Description" ); event.setDescription( dummy ); // category dummy = element->attribute("Categories" ); - dumInt = dummy.toInt(&ok ); - if(ok ) { - QArray<int> arrat(1); - arrat[0] = dumInt; - event.setCategory( Qtopia::Record::idsToString( arrat ) ); - } + QStringList ids = QStringList::split(";", dummy ); + event.setCategories( ids ); + //uid dummy = element->attribute("Uid" ); dumInt = dummy.toInt(&ok ); if(ok ) event.setUid( dumInt ); m_todos.append( event ); element = element->nextChild(); // next element } //} }else { qWarning("could not load" ); } delete root; qWarning("returning" ); return m_todos; } }; } ToDoDB::ToDoDB(const QString &fileName, ToDoResource *res ){ m_fileName = fileName; if( fileName.isEmpty() && res == 0 ){ m_fileName = Global::applicationFileName("todolist","todolist.xml"); res = new FileToDoResource(); //qWarning("%s", m_fileName.latin1() ); }else if(res == 0 ){ // let's create a ToDoResource for xml res = new FileToDoResource(); } m_res = res; load(); } ToDoResource* ToDoDB::resource(){ return m_res; }; void ToDoDB::setResource( ToDoResource *res ) { delete m_res; m_res = res; } ToDoDB::~ToDoDB() { delete m_res; } QValueList<ToDoEvent> ToDoDB::effectiveToDos(const QDate &from, const QDate &to, bool all ) { QValueList<ToDoEvent> events; for( QValueList<ToDoEvent>::Iterator it = m_todos.begin(); it!= m_todos.end(); ++it ){ if( (*it).hasDate() ){ if( (*it).date() >= from && (*it).date() <= to ) events.append( (*it) ); }else if( all ){ events.append( (*it) ); } } return events; } QValueList<ToDoEvent> ToDoDB::effectiveToDos(const QDate &from, bool all) { return effectiveToDos( from, QDate::currentDate(), all ); } QValueList<ToDoEvent> ToDoDB::overDue() { QValueList<ToDoEvent> events; for( QValueList<ToDoEvent>::Iterator it = m_todos.begin(); it!= m_todos.end(); ++it ){ if( (*it).isOverdue() ) events.append((*it) ); } return events; } QValueList<ToDoEvent> ToDoDB::rawToDos() { return m_todos; } void ToDoDB::addEvent( const ToDoEvent &event ) { m_todos.append( event ); } void ToDoDB::editEvent( const ToDoEvent &event ) { m_todos.remove( event ); m_todos.append( event ); } void ToDoDB::removeEvent( const ToDoEvent &event ) { m_todos.remove( event ); } void ToDoDB::reload() { load(); } void ToDoDB::setFileName(const QString &file ) { m_fileName =file; } QString ToDoDB::fileName()const { return m_fileName; } void ToDoDB::load() { m_todos = m_res->load( m_fileName ); } bool ToDoDB::save() { return m_res->save( m_fileName, m_todos ); } diff --git a/libopie/todoevent.cpp b/libopie/todoevent.cpp index 7dbf907..28b2e98 100644 --- a/libopie/todoevent.cpp +++ b/libopie/todoevent.cpp @@ -1,176 +1,191 @@ #include <opie/todoevent.h> #include <qpe/palmtopuidgen.h> #include <qpe/stringutil.h> #include <qpe/palmtoprecord.h> ToDoEvent::ToDoEvent(const ToDoEvent &event ) { *this = event; } -ToDoEvent::ToDoEvent(bool completed, int priority, const QString &category, +ToDoEvent::ToDoEvent(bool completed, int priority, const QStringList &category, const QString &description, bool hasDate, QDate date, int uid ) { qWarning("todoEvent c'tor" ); m_date = date; m_isCompleted = completed; m_hasDate = hasDate; m_priority = priority; m_category = category; m_desc = Qtopia::simplifyMultiLineSpace(description ); if (uid == -1 ) { Qtopia::UidGen *uidgen = new Qtopia::UidGen(); uid = uidgen->generate(); delete uidgen; }// generate the ids m_uid = uid; } QArray<int> ToDoEvent::categories()const { - QArray<int> array(1); // currently the datebook can be only in one category - array = Qtopia::Record::idsFromString( category() ); + QArray<int> array(m_category.count() ); // currently the datebook can be only in one category + array = Qtopia::Record::idsFromString( m_category.join(";") ); return array; } bool ToDoEvent::match( const QRegExp ®Exp )const { if( QString::number( m_priority ).find( regExp ) != -1 ){ return true; }else if( m_hasDate && m_date.toString().find( regExp) != -1 ){ return true; }else if(m_desc.find( regExp ) != -1 ){ return true; } return false; } bool ToDoEvent::isCompleted() const { return m_isCompleted; } bool ToDoEvent::hasDate() const { return m_hasDate; } int ToDoEvent::priority()const { return m_priority; } -QString ToDoEvent::category()const +QStringList ToDoEvent::allCategories()const { return m_category; } +void ToDoEvent::insertCategory(const QString &str ) +{ + m_category.append( str ); +} +void ToDoEvent::clearCategories() +{ + m_category.clear(); +} +void ToDoEvent::setCategories(const QStringList &list ) +{ + m_category = list; + qWarning("todoevent: %s", list.join(";" ).latin1() ); +} QDate ToDoEvent::date()const { return m_date; } + QString ToDoEvent::description()const { return m_desc; } void ToDoEvent::setCompleted( bool completed ) { m_isCompleted = completed; } void ToDoEvent::setHasDate( bool hasDate ) { m_hasDate = hasDate; } void ToDoEvent::setDescription(const QString &desc ) { m_desc = Qtopia::simplifyMultiLineSpace(desc ); } void ToDoEvent::setCategory( const QString &cat ) { qWarning("setCategory %s", cat.latin1() ); - m_category = cat; + m_category.clear(); + m_category << cat; } void ToDoEvent::setPriority(int prio ) { m_priority = prio; } void ToDoEvent::setDate( QDate date ) { m_date = date; } bool ToDoEvent::isOverdue( ) { if( m_hasDate ) return QDate::currentDate() > m_date; return false; } bool ToDoEvent::operator<( const ToDoEvent &toDoEvent )const{ if( !hasDate() && !toDoEvent.hasDate() ) return true; if( !hasDate() && toDoEvent.hasDate() ) return true; if( hasDate() && toDoEvent.hasDate() ){ if( date() == toDoEvent.date() ){ // let's the priority decide return priority() < toDoEvent.priority(); }else{ return date() < toDoEvent.date(); } } return false; } bool ToDoEvent::operator<=(const ToDoEvent &toDoEvent )const { if( !hasDate() && !toDoEvent.hasDate() ) return true; if( !hasDate() && toDoEvent.hasDate() ) return true; if( hasDate() && toDoEvent.hasDate() ){ if( date() == toDoEvent.date() ){ // let's the priority decide return priority() <= toDoEvent.priority(); }else{ return date() <= toDoEvent.date(); } } return true; } bool ToDoEvent::operator>(const ToDoEvent &toDoEvent )const { if( !hasDate() && !toDoEvent.hasDate() ) return false; if( !hasDate() && toDoEvent.hasDate() ) return false; if( hasDate() && toDoEvent.hasDate() ){ if( date() == toDoEvent.date() ){ // let's the priority decide return priority() > toDoEvent.priority(); }else{ return date() > toDoEvent.date(); } } return false; } bool ToDoEvent::operator>=(const ToDoEvent &toDoEvent )const { if( !hasDate() && !toDoEvent.hasDate() ) return true; if( !hasDate() && toDoEvent.hasDate() ) return false; if( hasDate() && toDoEvent.hasDate() ){ if( date() == toDoEvent.date() ){ // let's the priority decide return priority() > toDoEvent.priority(); }else{ return date() > toDoEvent.date(); } } return true; } bool ToDoEvent::operator==(const ToDoEvent &toDoEvent )const { - if( m_date == toDoEvent.m_date && m_isCompleted == toDoEvent.m_isCompleted && m_hasDate == toDoEvent.m_hasDate && m_priority == toDoEvent.m_priority && m_category == toDoEvent.m_category && m_desc == toDoEvent.m_category ) + if( m_date == toDoEvent.m_date && m_isCompleted == toDoEvent.m_isCompleted && m_hasDate == toDoEvent.m_hasDate && m_priority == toDoEvent.m_priority && m_category == toDoEvent.m_category && m_desc == toDoEvent.m_desc ) return true; return false; } ToDoEvent &ToDoEvent::operator=(const ToDoEvent &item ) { m_date = item.m_date; m_isCompleted = item.m_isCompleted; m_hasDate = item.m_hasDate; m_priority = item.m_priority; m_category = item.m_category; m_desc = item.m_desc; m_uid = item.m_uid; return *this; } diff --git a/libopie/todoevent.h b/libopie/todoevent.h index ac996a1..0d477fd 100644 --- a/libopie/todoevent.h +++ b/libopie/todoevent.h @@ -1,57 +1,64 @@ #ifndef todoevent_h #define todoevent_h +#include <qstringlist.h> #include <qdatetime.h> class ToDoEvent { friend class ToDoDB; public: enum Priority { VERYHIGH=1, HIGH, NORMAL, LOW, VERYLOW }; ToDoEvent( bool completed = false, int priority = NORMAL, - const QString &category = QString::null, + const QStringList &category = QStringList(), const QString &description = QString::null , bool hasDate = false, QDate date = QDate::currentDate(), int uid = -1 ); ToDoEvent(const ToDoEvent & ); bool isCompleted() const; bool hasDate() const; int priority()const ; - QString category()const; + QStringList allCategories()const; QArray<int> categories() const; QDate date()const; QString description()const; int uid()const { return m_uid;}; void setCompleted(bool completed ); void setHasDate( bool hasDate ); // if the category doesn't exist we will create it + // this sets the the Category after this call category will be the only category void setCategory( const QString &category ); + // adds a category to the Categories of this event + void insertCategory(const QString &category ); + void clearCategories(); + void setCategories(const QStringList& ); + void setPriority(int priority ); void setDate( QDate date ); void setDescription(const QString& ); bool isOverdue(); bool match( const QRegExp &r )const; void setUid(int id) {m_uid = id; }; bool operator<(const ToDoEvent &toDoEvent )const; bool operator<=(const ToDoEvent &toDoEvent )const; bool operator!=(const ToDoEvent &toDoEvent )const { return !(*this == toDoEvent); }; bool operator>(const ToDoEvent &toDoEvent )const; bool operator>=(const ToDoEvent &toDoEvent)const; bool operator==(const ToDoEvent &toDoEvent )const; ToDoEvent &operator=(const ToDoEvent &toDoEvent ); private: class ToDoEventPrivate; ToDoEventPrivate *d; QDate m_date; bool m_isCompleted:1; bool m_hasDate:1; int m_priority; - QString m_category; + QStringList m_category; QString m_desc; int m_uid; }; #endif diff --git a/libopie/todovcalresource.cpp b/libopie/todovcalresource.cpp index a6afe68..75f2197 100644 --- a/libopie/todovcalresource.cpp +++ b/libopie/todovcalresource.cpp @@ -1,153 +1,153 @@ /* =. This file is part of the OPIE Project .=l. Copyright (c) 2002 Holger Freyther <freyther@kde.org> .>+-= the use of vobject was inspired by libkcal _;:, .> :=|. This library is free software; you can .> <`_, > . <= redistribute it and/or modify it under :`=1 )Y*s>-.-- : the terms of the GNU Library General Public .="- .-=="i, .._ License as published by the Free Software - . .-<_> .<> Foundation; either version 2 of the License, ._= =} : or (at your option) any later version. .%`+i> _;_. .i_,=:_. -<s. This library is distributed in the hope that + . -:. = it will be useful, but WITHOUT ANY WARRANTY; : .. .:, . . . without even the implied warranty of =_ + =;=|` MERCHANTABILITY or FITNESS FOR A _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU ..}^=.= = ; Library General Public License for more ++= -. .` .: details. : = ...= . :.=- -. .:....=;==+<; You should have received a copy of the GNU -_. . . )=. = Library General Public License along with -- :-=` this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include <qfile.h> #include <qvaluelist.h> #include <opie/todoevent.h> #include <opie/todovcalresource.h> #include "../library/backend/vobject_p.h" #include "../library/backend/timeconversion.h" #include "../library/backend/qfiledirect_p.h" static VObject *vobjByEvent( const ToDoEvent &event ) { VObject *task = newVObject( VCTodoProp ); if( task == 0 ) return 0l; if( event.hasDate() ) addPropValue( task, VCDueProp, TimeConversion::toISO8601( event.date() ) ); if( event.isCompleted() ) addPropValue( task, VCStatusProp, "COMPLETED"); QString string = QString::number(event.priority() ); addPropValue( task, VCPriorityProp, string.local8Bit() ); - addPropValue( task, VCCategoriesProp, event.category().local8Bit() ); + addPropValue( task, VCCategoriesProp, event.allCategories().join(";").local8Bit() ); addPropValue( task, VCDescriptionProp, event.description().local8Bit() ); addPropValue( task, VCSummaryProp, event.description().left(15).local8Bit() ); return task; }; static ToDoEvent eventByVObj( VObject *obj ){ ToDoEvent event; VObject *ob; QCString name; // no uid, attendees, ... and no fun // description if( ( ob = isAPropertyOf( obj, VCDescriptionProp )) != 0 ){ name = vObjectStringZValue( ob ); event.setDescription( name ); } // completed if( ( ob = isAPropertyOf( obj, VCStatusProp )) != 0 ){ name = vObjectStringZValue( ob ); if( name == "COMPLETED" ){ event.setCompleted( true ); }else{ event.setCompleted( false ); } }else event.setCompleted( false ); // priority if ((ob = isAPropertyOf(obj, VCPriorityProp))) { name = vObjectStringZValue( ob ); bool ok; event.setPriority(name.toInt(&ok) ); } //due date if((ob = isAPropertyOf(obj, VCDueProp)) ){ event.setHasDate( true ); name = vObjectStringZValue( ob ); event.setDate( TimeConversion::fromISO8601( name).date() ); } // categories if((ob = isAPropertyOf( obj, VCCategoriesProp )) != 0 ){ name = vObjectStringZValue( ob ); qWarning("Categories:%s", name.data() ); } return event; }; QValueList<ToDoEvent> ToDoVCalResource::load(const QString &file) { QValueList<ToDoEvent> events; VObject *vcal = 0l; vcal = Parse_MIME_FromFileName( (char *)file.utf8().data() ); // from vobject if(!vcal ) return events; // start parsing VObjectIterator it; VObject *vobj; initPropIterator(&it, vcal); while( moreIteration( &it ) ) { vobj = ::nextVObject( &it ); QCString name = ::vObjectName( vobj ); //QCString objVal = ::vObjectStringZValue( vobj ); // let's find out the type if( name == VCTodoProp ){ events.append( eventByVObj( vobj ) ); } // parse the value } return events; } bool ToDoVCalResource::save(const QString &fileName, const QValueList<ToDoEvent>&list ) { QFileDirect file ( fileName ); if(!file.open(IO_WriteOnly ) ) return false; // obj VObject *obj; obj = newVObject( VCCalProp ); addPropValue( obj, VCVersionProp, "1.0" ); VObject *vo; for(QValueList<ToDoEvent>::ConstIterator it = list.begin(); it != list.end(); ++it ){ vo = vobjByEvent( (*it) ); addVObjectProp(obj, vo ); } writeVObject( file.directHandle(), obj ); cleanVObject( obj ); cleanStrTbl(); return true; } |