author | zecke <zecke> | 2002-03-19 19:14:32 (UTC) |
---|---|---|
committer | zecke <zecke> | 2002-03-19 19:14:32 (UTC) |
commit | c23db0c35b77cc1656ac8822ca948e73c50e8ee6 (patch) (unidiff) | |
tree | f1f61cd24074a9ce60561e13681c386e89088dc9 | |
parent | 51762a579019d09fc1af83ef4838260493b534a7 (diff) | |
download | opie-c23db0c35b77cc1656ac8822ca948e73c50e8ee6.zip opie-c23db0c35b77cc1656ac8822ca948e73c50e8ee6.tar.gz opie-c23db0c35b77cc1656ac8822ca948e73c50e8ee6.tar.bz2 |
Make multiple backends possible
-rw-r--r-- | include/opie/tododb.h | 6 | ||||
-rw-r--r-- | include/opie/todoevent.h | 1 | ||||
-rw-r--r-- | include/opie/todoresource.h | 14 | ||||
-rw-r--r-- | libopie/tododb.cpp | 237 | ||||
-rw-r--r-- | libopie/tododb.h | 6 | ||||
-rw-r--r-- | libopie/todoevent.h | 1 | ||||
-rw-r--r-- | libopie/todoresource.h | 14 |
7 files changed, 168 insertions, 111 deletions
diff --git a/include/opie/tododb.h b/include/opie/tododb.h index 6478363..945f343 100644 --- a/include/opie/tododb.h +++ b/include/opie/tododb.h | |||
@@ -1,40 +1,44 @@ | |||
1 | 1 | ||
2 | #ifndef tododb_h | 2 | #ifndef tododb_h |
3 | #define tododb_h | 3 | #define tododb_h |
4 | 4 | ||
5 | #include <qvaluelist.h> | 5 | #include <qvaluelist.h> |
6 | 6 | ||
7 | #include <opie/todoevent.h> | 7 | #include <opie/todoevent.h> |
8 | 8 | ||
9 | class ToDoResource; | ||
9 | class ToDoDB | 10 | class ToDoDB |
10 | { | 11 | { |
11 | public: | 12 | public: |
12 | // if no argument is supplied pick the default book | 13 | // if no argument is supplied pick the default book |
13 | ToDoDB(const QString &fileName = QString::null ); | 14 | ToDoDB(const QString &fileName = QString::null, ToDoResource* resource= 0 ); |
14 | ~ToDoDB(); | 15 | ~ToDoDB(); |
15 | QValueList<ToDoEvent> effectiveToDos(const QDate &from, | 16 | QValueList<ToDoEvent> effectiveToDos(const QDate &from, |
16 | const QDate &to, | 17 | const QDate &to, |
17 | bool includeNoDates = true); | 18 | bool includeNoDates = true); |
18 | QValueList<ToDoEvent> effectiveToDos(const QDate &start, bool includeNoDates = true ); | 19 | QValueList<ToDoEvent> effectiveToDos(const QDate &start, bool includeNoDates = true ); |
19 | QValueList<ToDoEvent> rawToDos(); // all events | 20 | QValueList<ToDoEvent> rawToDos(); // all events |
20 | QValueList<ToDoEvent> overDue(); | 21 | QValueList<ToDoEvent> overDue(); |
21 | 22 | ||
22 | void addEvent(const ToDoEvent &event ); | 23 | void addEvent(const ToDoEvent &event ); |
23 | void editEvent(const ToDoEvent &editEvent ); | 24 | void editEvent(const ToDoEvent &editEvent ); |
24 | void removeEvent(const ToDoEvent &event); | 25 | void removeEvent(const ToDoEvent &event); |
25 | 26 | ||
26 | void reload(); | 27 | void reload(); |
27 | void setFileName(const QString & ); | 28 | void setFileName(const QString & ); |
28 | QString fileName()const; | 29 | QString fileName()const; |
29 | bool save(); | 30 | bool save(); |
31 | ToDoResource *resource(); | ||
32 | void setResource(ToDoResource* res); | ||
30 | 33 | ||
31 | private: | 34 | private: |
32 | class ToDoDBPrivate; | 35 | class ToDoDBPrivate; |
33 | ToDoDBPrivate *d; | 36 | ToDoDBPrivate *d; |
34 | QString m_fileName; | 37 | QString m_fileName; |
38 | ToDoResource *m_res; | ||
35 | QValueList<ToDoEvent> m_todos; | 39 | QValueList<ToDoEvent> m_todos; |
36 | void load(); | 40 | void load(); |
37 | }; | 41 | }; |
38 | 42 | ||
39 | 43 | ||
40 | #endif | 44 | #endif |
diff --git a/include/opie/todoevent.h b/include/opie/todoevent.h index dd8c0c9..79522b2 100644 --- a/include/opie/todoevent.h +++ b/include/opie/todoevent.h | |||
@@ -1,52 +1,53 @@ | |||
1 | 1 | ||
2 | #ifndef todoevent_h | 2 | #ifndef todoevent_h |
3 | #define todoevent_h | 3 | #define todoevent_h |
4 | 4 | ||
5 | #include <qdatetime.h> | 5 | #include <qdatetime.h> |
6 | 6 | ||
7 | class ToDoEvent { | 7 | class ToDoEvent { |
8 | friend class ToDoDB; | 8 | friend class ToDoDB; |
9 | public: | 9 | public: |
10 | enum Priority { VERYHIGH=1, HIGH, NORMAL, LOW, VERYLOW }; | 10 | enum Priority { VERYHIGH=1, HIGH, NORMAL, LOW, VERYLOW }; |
11 | ToDoEvent( bool completed = false, int priority = NORMAL, | 11 | ToDoEvent( bool completed = false, int priority = NORMAL, |
12 | const QString &category = QString::null, | 12 | const QString &category = QString::null, |
13 | const QString &description = QString::null , | 13 | const QString &description = QString::null , |
14 | bool hasDate = false, QDate date = QDate::currentDate(), int uid = -1 ); | 14 | bool hasDate = false, QDate date = QDate::currentDate(), int uid = -1 ); |
15 | bool isCompleted() const; | 15 | bool isCompleted() const; |
16 | bool hasDate() const; | 16 | bool hasDate() const; |
17 | int priority()const ; | 17 | int priority()const ; |
18 | QString category()const; | 18 | QString category()const; |
19 | QDate date()const; | 19 | QDate date()const; |
20 | QString description()const; | 20 | QString description()const; |
21 | 21 | ||
22 | int uid()const { return m_uid;}; | 22 | int uid()const { return m_uid;}; |
23 | void setCompleted(bool completed ); | 23 | void setCompleted(bool completed ); |
24 | void setHasDate( bool hasDate ); | 24 | void setHasDate( bool hasDate ); |
25 | // if the category doesn't exist we will create it | 25 | // if the category doesn't exist we will create it |
26 | void setCategory( const QString &category ); | 26 | void setCategory( const QString &category ); |
27 | void setPriority(int priority ); | 27 | void setPriority(int priority ); |
28 | void setDate( QDate date ); | 28 | void setDate( QDate date ); |
29 | void setDescription(const QString& ); | 29 | void setDescription(const QString& ); |
30 | bool isOverdue(); | 30 | bool isOverdue(); |
31 | 31 | ||
32 | void setUid(int id) {m_uid = id; }; | ||
32 | bool operator<(const ToDoEvent &toDoEvent )const; | 33 | bool operator<(const ToDoEvent &toDoEvent )const; |
33 | bool operator<=(const ToDoEvent &toDoEvent )const; | 34 | bool operator<=(const ToDoEvent &toDoEvent )const; |
34 | bool operator!=(const ToDoEvent &toDoEvent )const { return !(*this == toDoEvent); }; | 35 | bool operator!=(const ToDoEvent &toDoEvent )const { return !(*this == toDoEvent); }; |
35 | bool operator>(const ToDoEvent &toDoEvent )const; | 36 | bool operator>(const ToDoEvent &toDoEvent )const; |
36 | bool operator>=(const ToDoEvent &toDoEvent)const; | 37 | bool operator>=(const ToDoEvent &toDoEvent)const; |
37 | bool operator==(const ToDoEvent &toDoEvent )const; | 38 | bool operator==(const ToDoEvent &toDoEvent )const; |
38 | ToDoEvent &operator=(const ToDoEvent &toDoEvent ); | 39 | ToDoEvent &operator=(const ToDoEvent &toDoEvent ); |
39 | private: | 40 | private: |
40 | class ToDoEventPrivate; | 41 | class ToDoEventPrivate; |
41 | ToDoEventPrivate *d; | 42 | ToDoEventPrivate *d; |
42 | QDate m_date; | 43 | QDate m_date; |
43 | bool m_isCompleted:1; | 44 | bool m_isCompleted:1; |
44 | bool m_hasDate:1; | 45 | bool m_hasDate:1; |
45 | int m_priority; | 46 | int m_priority; |
46 | QString m_category; | 47 | QString m_category; |
47 | QString m_desc; | 48 | QString m_desc; |
48 | int m_uid; | 49 | int m_uid; |
49 | }; | 50 | }; |
50 | 51 | ||
51 | 52 | ||
52 | #endif | 53 | #endif |
diff --git a/include/opie/todoresource.h b/include/opie/todoresource.h new file mode 100644 index 0000000..34edb04 --- a/dev/null +++ b/include/opie/todoresource.h | |||
@@ -0,0 +1,14 @@ | |||
1 | |||
2 | |||
3 | #ifndef opietodoresource_h | ||
4 | #define opietodoresource_h | ||
5 | |||
6 | class ToDoEvent; | ||
7 | class ToDoResource { | ||
8 | public: | ||
9 | ToDoResource( ) {}; | ||
10 | virtual QValueList<ToDoEvent> load(const QString &file ) = 0; | ||
11 | virtual bool save( const QString &file, const QValueList<ToDoEvent> & ) = 0; | ||
12 | }; | ||
13 | |||
14 | #endif | ||
diff --git a/libopie/tododb.cpp b/libopie/tododb.cpp index eb17674..91331d0 100644 --- a/libopie/tododb.cpp +++ b/libopie/tododb.cpp | |||
@@ -1,196 +1,215 @@ | |||
1 | 1 | ||
2 | #include <qdir.h> | 2 | #include <qdir.h> |
3 | #include <opie/tododb.h> | 3 | #include <opie/tododb.h> |
4 | #include <opie/xmltree.h> | 4 | #include <opie/xmltree.h> |
5 | #include <opie/todoresource.h> | ||
5 | #include <qpe/palmtoprecord.h> | 6 | #include <qpe/palmtoprecord.h> |
6 | #include <qpe/global.h> | 7 | #include <qpe/global.h> |
7 | 8 | ||
8 | ToDoDB::ToDoDB(const QString &fileName = QString::null ){ | 9 | namespace { |
10 | |||
11 | class FileToDoResource : public ToDoResource { | ||
12 | public: | ||
13 | FileToDoResource() {}; | ||
14 | bool save(const QString &name, const QValueList<ToDoEvent> &m_todos ){ | ||
15 | // prepare the XML | ||
16 | XMLElement *tasks = new XMLElement( ); | ||
17 | tasks->setTagName("Tasks" ); | ||
18 | for( QValueList<ToDoEvent>::ConstIterator it = m_todos.begin(); it != m_todos.end(); ++it ){ | ||
19 | XMLElement::AttributeMap map; | ||
20 | XMLElement *task = new XMLElement(); | ||
21 | map.insert( "Completed", QString::number((int)(*it).isCompleted() ) ); | ||
22 | map.insert( "HasDate", QString::number((int)(*it).hasDate() ) ); | ||
23 | map.insert( "Priority", QString::number( (*it).priority() ) ); | ||
24 | if(!(*it).category().isEmpty() ){ | ||
25 | QArray<int> arrat(1); | ||
26 | arrat = Qtopia::Record::idsFromString( (*it).category() ); | ||
27 | map.insert( "Categories", QString::number( arrat[0] ) ); | ||
28 | }else | ||
29 | map.insert( "Categories", QString::null ); | ||
30 | map.insert( "Description", (*it).description() ); | ||
31 | if( (*it).hasDate() ){ | ||
32 | map.insert("DateYear", QString::number( (*it).date().year() ) ); | ||
33 | map.insert("DateMonth", QString::number( (*it).date().month() ) ); | ||
34 | map.insert("DateDay", QString::number( (*it).date().day() ) ); | ||
35 | } | ||
36 | map.insert("Uid", QString::number( (*it).uid() ) ); | ||
37 | task->setTagName("Task" ); | ||
38 | task->setAttributes( map ); | ||
39 | tasks->appendChild(task); | ||
40 | } | ||
41 | QFile file( name); | ||
42 | if( file.open(IO_WriteOnly ) ){ | ||
43 | QTextStream stream(&file ); | ||
44 | stream << "<!DOCTYPE Tasks>" << endl; | ||
45 | tasks->save(stream ); | ||
46 | delete tasks; | ||
47 | file.close(); | ||
48 | return true; | ||
49 | } | ||
50 | return false; | ||
51 | } | ||
52 | QValueList<ToDoEvent> load( const QString &name ){ | ||
53 | qWarning("loading tododb" ); | ||
54 | QValueList<ToDoEvent> m_todos; | ||
55 | XMLElement *root = XMLElement::load( name ); | ||
56 | if(root != 0l ){ // start parsing | ||
57 | qWarning("ToDoDB::load tagName(): %s", root->tagName().latin1() ); | ||
58 | //if( root->tagName() == QString::fromLatin1("Tasks" ) ){// Start | ||
59 | XMLElement *element = root->firstChild(); | ||
60 | element = element->firstChild(); | ||
61 | while( element ){ | ||
62 | qWarning("ToDoDB::load element tagName() : %s", element->tagName().latin1() ); | ||
63 | QString dummy; | ||
64 | ToDoEvent event; | ||
65 | bool ok; | ||
66 | int dumInt; | ||
67 | // completed | ||
68 | dummy = element->attribute("Completed" ); | ||
69 | dumInt = dummy.toInt(&ok ); | ||
70 | if(ok ) event.setCompleted( dumInt == 0 ? false : true ); | ||
71 | // hasDate | ||
72 | dummy = element->attribute("HasDate" ); | ||
73 | dumInt = dummy.toInt(&ok ); | ||
74 | if(ok ) event.setHasDate( dumInt == 0 ? false: true ); | ||
75 | // set the date | ||
76 | bool hasDa = dumInt; | ||
77 | if ( hasDa ) { //parse the date | ||
78 | int year, day, month = 0; | ||
79 | year = day = month; | ||
80 | // year | ||
81 | dummy = element->attribute("DateYear" ); | ||
82 | dumInt = dummy.toInt(&ok ); | ||
83 | if( ok ) year = dumInt; | ||
84 | // month | ||
85 | dummy = element->attribute("DateMonth" ); | ||
86 | dumInt = dummy.toInt(&ok ); | ||
87 | if(ok ) month = dumInt; | ||
88 | dummy = element->attribute("DateDay" ); | ||
89 | dumInt = dummy.toInt(&ok ); | ||
90 | if(ok ) day = dumInt; | ||
91 | // set the date | ||
92 | QDate date( year, month, day ); | ||
93 | event.setDate( date); | ||
94 | } | ||
95 | dummy = element->attribute("Priority" ); | ||
96 | dumInt = dummy.toInt(&ok ); | ||
97 | if(!ok ) dumInt = ToDoEvent::NORMAL; | ||
98 | event.setPriority( dumInt ); | ||
99 | //description | ||
100 | dummy = element->attribute("Description" ); | ||
101 | event.setDescription( dummy ); | ||
102 | // category | ||
103 | dummy = element->attribute("Categories" ); | ||
104 | dumInt = dummy.toInt(&ok ); | ||
105 | if(ok ) { | ||
106 | QArray<int> arrat(1); | ||
107 | arrat[0] = dumInt; | ||
108 | event.setCategory( Qtopia::Record::idsToString( arrat ) ); | ||
109 | } | ||
110 | //uid | ||
111 | dummy = element->attribute("Uid" ); | ||
112 | dumInt = dummy.toInt(&ok ); | ||
113 | if(ok ) event.setUid( dumInt ); | ||
114 | m_todos.append( event ); | ||
115 | element = element->nextChild(); // next element | ||
116 | } | ||
117 | //} | ||
118 | }else { | ||
119 | qWarning("could not load" ); | ||
120 | } | ||
121 | delete root; | ||
122 | return m_todos; | ||
123 | } | ||
124 | }; | ||
125 | |||
126 | } | ||
127 | |||
128 | ToDoDB::ToDoDB(const QString &fileName = QString::null, ToDoResource *res ){ | ||
9 | m_fileName = fileName; | 129 | m_fileName = fileName; |
10 | if( fileName.isEmpty() ){ | 130 | if( fileName.isEmpty() && res == 0 ){ |
11 | m_fileName = Global::applicationFileName("todolist","todolist.xml");; | 131 | m_fileName = Global::applicationFileName("todolist","todolist.xml");; |
12 | qWarning("%s", m_fileName.latin1() ); | 132 | //qWarning("%s", m_fileName.latin1() ); |
133 | }else if(res == 0 ){ // let's create a ToDoResource for xml | ||
134 | res = new FileToDoResource(); | ||
13 | } | 135 | } |
14 | 136 | m_res = res; | |
15 | load(); | 137 | load(); |
16 | } | 138 | } |
17 | ToDoDB::~ToDoDB() | 139 | ToDoDB::~ToDoDB() |
18 | { | 140 | { |
19 | 141 | ||
20 | } | 142 | } |
21 | QValueList<ToDoEvent> ToDoDB::effectiveToDos(const QDate &from, const QDate &to, | 143 | QValueList<ToDoEvent> ToDoDB::effectiveToDos(const QDate &from, const QDate &to, |
22 | bool all ) | 144 | bool all ) |
23 | { | 145 | { |
24 | QValueList<ToDoEvent> events; | 146 | QValueList<ToDoEvent> events; |
25 | for( QValueList<ToDoEvent>::Iterator it = m_todos.begin(); it!= m_todos.end(); ++it ){ | 147 | for( QValueList<ToDoEvent>::Iterator it = m_todos.begin(); it!= m_todos.end(); ++it ){ |
26 | if( (*it).hasDate() ){ | 148 | if( (*it).hasDate() ){ |
27 | if( (*it).date() >= from && (*it).date() <= to ) | 149 | if( (*it).date() >= from && (*it).date() <= to ) |
28 | events.append( (*it) ); | 150 | events.append( (*it) ); |
29 | }else if( all ){ | 151 | }else if( all ){ |
30 | events.append( (*it) ); | 152 | events.append( (*it) ); |
31 | } | 153 | } |
32 | } | 154 | } |
33 | return events; | 155 | return events; |
34 | } | 156 | } |
35 | QValueList<ToDoEvent> ToDoDB::effectiveToDos(const QDate &from, | 157 | QValueList<ToDoEvent> ToDoDB::effectiveToDos(const QDate &from, |
36 | bool all) | 158 | bool all) |
37 | { | 159 | { |
38 | return effectiveToDos( from, QDate::currentDate(), all ); | 160 | return effectiveToDos( from, QDate::currentDate(), all ); |
39 | } | 161 | } |
40 | QValueList<ToDoEvent> ToDoDB::overDue() | 162 | QValueList<ToDoEvent> ToDoDB::overDue() |
41 | { | 163 | { |
42 | QValueList<ToDoEvent> events; | 164 | QValueList<ToDoEvent> events; |
43 | for( QValueList<ToDoEvent>::Iterator it = m_todos.begin(); it!= m_todos.end(); ++it ){ | 165 | for( QValueList<ToDoEvent>::Iterator it = m_todos.begin(); it!= m_todos.end(); ++it ){ |
44 | if( (*it).isOverdue() ) | 166 | if( (*it).isOverdue() ) |
45 | events.append((*it) ); | 167 | events.append((*it) ); |
46 | } | 168 | } |
47 | return events; | 169 | return events; |
48 | } | 170 | } |
49 | QValueList<ToDoEvent> ToDoDB::rawToDos() | 171 | QValueList<ToDoEvent> ToDoDB::rawToDos() |
50 | { | 172 | { |
51 | return m_todos; | 173 | return m_todos; |
52 | } | 174 | } |
53 | void ToDoDB::addEvent( const ToDoEvent &event ) | 175 | void ToDoDB::addEvent( const ToDoEvent &event ) |
54 | { | 176 | { |
55 | m_todos.append( event ); | 177 | m_todos.append( event ); |
56 | } | 178 | } |
57 | void ToDoDB::editEvent( const ToDoEvent &event ) | 179 | void ToDoDB::editEvent( const ToDoEvent &event ) |
58 | { | 180 | { |
59 | m_todos.remove( event ); | 181 | m_todos.remove( event ); |
60 | m_todos.append( event ); | 182 | m_todos.append( event ); |
61 | } | 183 | } |
62 | void ToDoDB::removeEvent( const ToDoEvent &event ) | 184 | void ToDoDB::removeEvent( const ToDoEvent &event ) |
63 | { | 185 | { |
64 | m_todos.remove( event ); | 186 | m_todos.remove( event ); |
65 | } | 187 | } |
66 | void ToDoDB::reload() | 188 | void ToDoDB::reload() |
67 | { | 189 | { |
68 | load(); | 190 | load(); |
69 | } | 191 | } |
70 | void ToDoDB::setFileName(const QString &file ) | 192 | void ToDoDB::setFileName(const QString &file ) |
71 | { | 193 | { |
72 | m_fileName =file; | 194 | m_fileName =file; |
73 | } | 195 | } |
74 | QString ToDoDB::fileName()const | 196 | QString ToDoDB::fileName()const |
75 | { | 197 | { |
76 | return m_fileName; | 198 | return m_fileName; |
77 | } | 199 | } |
78 | void ToDoDB::load() | 200 | void ToDoDB::load() |
79 | { | 201 | { |
80 | qWarning("loading tododb" ); | 202 | |
81 | m_todos.clear(); | ||
82 | XMLElement *root = XMLElement::load( m_fileName ); | ||
83 | if(root != 0l ){ // start parsing | ||
84 | qWarning("ToDoDB::load tagName(): %s", root->tagName().latin1() ); | ||
85 | //if( root->tagName() == QString::fromLatin1("Tasks" ) ){// Start | ||
86 | XMLElement *element = root->firstChild(); | ||
87 | element = element->firstChild(); | ||
88 | while( element ){ | ||
89 | qWarning("ToDoDB::load element tagName() : %s", element->tagName().latin1() ); | ||
90 | QString dummy; | ||
91 | ToDoEvent event; | ||
92 | bool ok; | ||
93 | int dumInt; | ||
94 | // completed | ||
95 | dummy = element->attribute("Completed" ); | ||
96 | dumInt = dummy.toInt(&ok ); | ||
97 | if(ok ) event.setCompleted( dumInt == 0 ? false : true ); | ||
98 | // hasDate | ||
99 | dummy = element->attribute("HasDate" ); | ||
100 | dumInt = dummy.toInt(&ok ); | ||
101 | if(ok ) event.setHasDate( dumInt == 0 ? false: true ); | ||
102 | // set the date | ||
103 | bool hasDa = dumInt; | ||
104 | if ( hasDa ) { //parse the date | ||
105 | int year, day, month = 0; | ||
106 | year = day = month; | ||
107 | // year | ||
108 | dummy = element->attribute("DateYear" ); | ||
109 | dumInt = dummy.toInt(&ok ); | ||
110 | if( ok ) year = dumInt; | ||
111 | // month | ||
112 | dummy = element->attribute("DateMonth" ); | ||
113 | dumInt = dummy.toInt(&ok ); | ||
114 | if(ok ) month = dumInt; | ||
115 | dummy = element->attribute("DateDay" ); | ||
116 | dumInt = dummy.toInt(&ok ); | ||
117 | if(ok ) day = dumInt; | ||
118 | // set the date | ||
119 | QDate date( year, month, day ); | ||
120 | event.setDate( date); | ||
121 | } | ||
122 | dummy = element->attribute("Priority" ); | ||
123 | dumInt = dummy.toInt(&ok ); | ||
124 | if(!ok ) dumInt = ToDoEvent::NORMAL; | ||
125 | event.setPriority( dumInt ); | ||
126 | //description | ||
127 | dummy = element->attribute("Description" ); | ||
128 | event.setDescription( dummy ); | ||
129 | // category | ||
130 | dummy = element->attribute("Categories" ); | ||
131 | dumInt = dummy.toInt(&ok ); | ||
132 | if(ok ) { | ||
133 | QArray<int> arrat(1); | ||
134 | arrat[0] = dumInt; | ||
135 | event.setCategory( Qtopia::Record::idsToString( arrat ) ); | ||
136 | } | ||
137 | //uid | ||
138 | dummy = element->attribute("Uid" ); | ||
139 | dumInt = dummy.toInt(&ok ); | ||
140 | if(ok ) event.m_uid = dumInt; | ||
141 | m_todos.append( event ); | ||
142 | element = element->nextChild(); // next element | ||
143 | } | ||
144 | //} | ||
145 | }else { | ||
146 | qWarning("could not load" ); | ||
147 | } | ||
148 | delete root; | ||
149 | } | 203 | } |
150 | bool ToDoDB::save() | 204 | bool ToDoDB::save() |
151 | { | 205 | { |
152 | // prepare the XML | 206 | return m_res->save( m_fileName, m_todos ); |
153 | XMLElement *tasks = new XMLElement( ); | ||
154 | tasks->setTagName("Tasks" ); | ||
155 | for( QValueList<ToDoEvent>::Iterator it = m_todos.begin(); it != m_todos.end(); ++it ){ | ||
156 | XMLElement::AttributeMap map; | ||
157 | XMLElement *task = new XMLElement(); | ||
158 | map.insert( "Completed", QString::number((int)(*it).isCompleted() ) ); | ||
159 | map.insert( "HasDate", QString::number((int)(*it).hasDate() ) ); | ||
160 | map.insert( "Priority", QString::number( (*it).priority() ) ); | ||
161 | if(!(*it).category().isEmpty() ){ | ||
162 | QArray<int> arrat(1); | ||
163 | arrat = Qtopia::Record::idsFromString( (*it).category() ); | ||
164 | map.insert( "Categories", QString::number( arrat[0] ) ); | ||
165 | }else | ||
166 | map.insert( "Categories", QString::null ); | ||
167 | map.insert( "Description", (*it).description() ); | ||
168 | if( (*it).hasDate() ){ | ||
169 | map.insert("DateYear", QString::number( (*it).date().year() ) ); | ||
170 | map.insert("DateMonth", QString::number( (*it).date().month() ) ); | ||
171 | map.insert("DateDay", QString::number( (*it).date().day() ) ); | ||
172 | } | ||
173 | map.insert("Uid", QString::number( (*it).uid() ) ); | ||
174 | task->setTagName("Task" ); | ||
175 | task->setAttributes( map ); | ||
176 | tasks->appendChild(task); | ||
177 | } | ||
178 | QFile file( m_fileName); | ||
179 | if( file.open(IO_WriteOnly ) ){ | ||
180 | QTextStream stream(&file ); | ||
181 | stream << "<!DOCTYPE Tasks>" << endl; | ||
182 | tasks->save(stream ); | ||
183 | delete tasks; | ||
184 | file.close(); | ||
185 | return true; | ||
186 | } | ||
187 | return false; | ||
188 | } | 207 | } |
189 | 208 | ||
190 | 209 | ||
191 | 210 | ||
192 | 211 | ||
193 | 212 | ||
194 | 213 | ||
195 | 214 | ||
196 | 215 | ||
diff --git a/libopie/tododb.h b/libopie/tododb.h index 6478363..945f343 100644 --- a/libopie/tododb.h +++ b/libopie/tododb.h | |||
@@ -1,40 +1,44 @@ | |||
1 | 1 | ||
2 | #ifndef tododb_h | 2 | #ifndef tododb_h |
3 | #define tododb_h | 3 | #define tododb_h |
4 | 4 | ||
5 | #include <qvaluelist.h> | 5 | #include <qvaluelist.h> |
6 | 6 | ||
7 | #include <opie/todoevent.h> | 7 | #include <opie/todoevent.h> |
8 | 8 | ||
9 | class ToDoResource; | ||
9 | class ToDoDB | 10 | class ToDoDB |
10 | { | 11 | { |
11 | public: | 12 | public: |
12 | // if no argument is supplied pick the default book | 13 | // if no argument is supplied pick the default book |
13 | ToDoDB(const QString &fileName = QString::null ); | 14 | ToDoDB(const QString &fileName = QString::null, ToDoResource* resource= 0 ); |
14 | ~ToDoDB(); | 15 | ~ToDoDB(); |
15 | QValueList<ToDoEvent> effectiveToDos(const QDate &from, | 16 | QValueList<ToDoEvent> effectiveToDos(const QDate &from, |
16 | const QDate &to, | 17 | const QDate &to, |
17 | bool includeNoDates = true); | 18 | bool includeNoDates = true); |
18 | QValueList<ToDoEvent> effectiveToDos(const QDate &start, bool includeNoDates = true ); | 19 | QValueList<ToDoEvent> effectiveToDos(const QDate &start, bool includeNoDates = true ); |
19 | QValueList<ToDoEvent> rawToDos(); // all events | 20 | QValueList<ToDoEvent> rawToDos(); // all events |
20 | QValueList<ToDoEvent> overDue(); | 21 | QValueList<ToDoEvent> overDue(); |
21 | 22 | ||
22 | void addEvent(const ToDoEvent &event ); | 23 | void addEvent(const ToDoEvent &event ); |
23 | void editEvent(const ToDoEvent &editEvent ); | 24 | void editEvent(const ToDoEvent &editEvent ); |
24 | void removeEvent(const ToDoEvent &event); | 25 | void removeEvent(const ToDoEvent &event); |
25 | 26 | ||
26 | void reload(); | 27 | void reload(); |
27 | void setFileName(const QString & ); | 28 | void setFileName(const QString & ); |
28 | QString fileName()const; | 29 | QString fileName()const; |
29 | bool save(); | 30 | bool save(); |
31 | ToDoResource *resource(); | ||
32 | void setResource(ToDoResource* res); | ||
30 | 33 | ||
31 | private: | 34 | private: |
32 | class ToDoDBPrivate; | 35 | class ToDoDBPrivate; |
33 | ToDoDBPrivate *d; | 36 | ToDoDBPrivate *d; |
34 | QString m_fileName; | 37 | QString m_fileName; |
38 | ToDoResource *m_res; | ||
35 | QValueList<ToDoEvent> m_todos; | 39 | QValueList<ToDoEvent> m_todos; |
36 | void load(); | 40 | void load(); |
37 | }; | 41 | }; |
38 | 42 | ||
39 | 43 | ||
40 | #endif | 44 | #endif |
diff --git a/libopie/todoevent.h b/libopie/todoevent.h index dd8c0c9..79522b2 100644 --- a/libopie/todoevent.h +++ b/libopie/todoevent.h | |||
@@ -1,52 +1,53 @@ | |||
1 | 1 | ||
2 | #ifndef todoevent_h | 2 | #ifndef todoevent_h |
3 | #define todoevent_h | 3 | #define todoevent_h |
4 | 4 | ||
5 | #include <qdatetime.h> | 5 | #include <qdatetime.h> |
6 | 6 | ||
7 | class ToDoEvent { | 7 | class ToDoEvent { |
8 | friend class ToDoDB; | 8 | friend class ToDoDB; |
9 | public: | 9 | public: |
10 | enum Priority { VERYHIGH=1, HIGH, NORMAL, LOW, VERYLOW }; | 10 | enum Priority { VERYHIGH=1, HIGH, NORMAL, LOW, VERYLOW }; |
11 | ToDoEvent( bool completed = false, int priority = NORMAL, | 11 | ToDoEvent( bool completed = false, int priority = NORMAL, |
12 | const QString &category = QString::null, | 12 | const QString &category = QString::null, |
13 | const QString &description = QString::null , | 13 | const QString &description = QString::null , |
14 | bool hasDate = false, QDate date = QDate::currentDate(), int uid = -1 ); | 14 | bool hasDate = false, QDate date = QDate::currentDate(), int uid = -1 ); |
15 | bool isCompleted() const; | 15 | bool isCompleted() const; |
16 | bool hasDate() const; | 16 | bool hasDate() const; |
17 | int priority()const ; | 17 | int priority()const ; |
18 | QString category()const; | 18 | QString category()const; |
19 | QDate date()const; | 19 | QDate date()const; |
20 | QString description()const; | 20 | QString description()const; |
21 | 21 | ||
22 | int uid()const { return m_uid;}; | 22 | int uid()const { return m_uid;}; |
23 | void setCompleted(bool completed ); | 23 | void setCompleted(bool completed ); |
24 | void setHasDate( bool hasDate ); | 24 | void setHasDate( bool hasDate ); |
25 | // if the category doesn't exist we will create it | 25 | // if the category doesn't exist we will create it |
26 | void setCategory( const QString &category ); | 26 | void setCategory( const QString &category ); |
27 | void setPriority(int priority ); | 27 | void setPriority(int priority ); |
28 | void setDate( QDate date ); | 28 | void setDate( QDate date ); |
29 | void setDescription(const QString& ); | 29 | void setDescription(const QString& ); |
30 | bool isOverdue(); | 30 | bool isOverdue(); |
31 | 31 | ||
32 | void setUid(int id) {m_uid = id; }; | ||
32 | bool operator<(const ToDoEvent &toDoEvent )const; | 33 | bool operator<(const ToDoEvent &toDoEvent )const; |
33 | bool operator<=(const ToDoEvent &toDoEvent )const; | 34 | bool operator<=(const ToDoEvent &toDoEvent )const; |
34 | bool operator!=(const ToDoEvent &toDoEvent )const { return !(*this == toDoEvent); }; | 35 | bool operator!=(const ToDoEvent &toDoEvent )const { return !(*this == toDoEvent); }; |
35 | bool operator>(const ToDoEvent &toDoEvent )const; | 36 | bool operator>(const ToDoEvent &toDoEvent )const; |
36 | bool operator>=(const ToDoEvent &toDoEvent)const; | 37 | bool operator>=(const ToDoEvent &toDoEvent)const; |
37 | bool operator==(const ToDoEvent &toDoEvent )const; | 38 | bool operator==(const ToDoEvent &toDoEvent )const; |
38 | ToDoEvent &operator=(const ToDoEvent &toDoEvent ); | 39 | ToDoEvent &operator=(const ToDoEvent &toDoEvent ); |
39 | private: | 40 | private: |
40 | class ToDoEventPrivate; | 41 | class ToDoEventPrivate; |
41 | ToDoEventPrivate *d; | 42 | ToDoEventPrivate *d; |
42 | QDate m_date; | 43 | QDate m_date; |
43 | bool m_isCompleted:1; | 44 | bool m_isCompleted:1; |
44 | bool m_hasDate:1; | 45 | bool m_hasDate:1; |
45 | int m_priority; | 46 | int m_priority; |
46 | QString m_category; | 47 | QString m_category; |
47 | QString m_desc; | 48 | QString m_desc; |
48 | int m_uid; | 49 | int m_uid; |
49 | }; | 50 | }; |
50 | 51 | ||
51 | 52 | ||
52 | #endif | 53 | #endif |
diff --git a/libopie/todoresource.h b/libopie/todoresource.h new file mode 100644 index 0000000..34edb04 --- a/dev/null +++ b/libopie/todoresource.h | |||
@@ -0,0 +1,14 @@ | |||
1 | |||
2 | |||
3 | #ifndef opietodoresource_h | ||
4 | #define opietodoresource_h | ||
5 | |||
6 | class ToDoEvent; | ||
7 | class ToDoResource { | ||
8 | public: | ||
9 | ToDoResource( ) {}; | ||
10 | virtual QValueList<ToDoEvent> load(const QString &file ) = 0; | ||
11 | virtual bool save( const QString &file, const QValueList<ToDoEvent> & ) = 0; | ||
12 | }; | ||
13 | |||
14 | #endif | ||