-rw-r--r-- | libopie/tododb.cpp | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/libopie/tododb.cpp b/libopie/tododb.cpp index 6b10ec2..1e40c40 100644 --- a/libopie/tododb.cpp +++ b/libopie/tododb.cpp | |||
@@ -1,258 +1,266 @@ | |||
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 <opie/todoresource.h> |
6 | #include <qpe/palmtoprecord.h> | 6 | #include <qpe/palmtoprecord.h> |
7 | #include <qpe/global.h> | 7 | #include <qpe/global.h> |
8 | 8 | ||
9 | using namespace Opie; | 9 | using namespace Opie; |
10 | 10 | ||
11 | namespace { | 11 | namespace { |
12 | 12 | ||
13 | class FileToDoResource : public ToDoResource { | 13 | class FileToDoResource : public ToDoResource { |
14 | public: | 14 | public: |
15 | FileToDoResource() {}; | 15 | FileToDoResource() {}; |
16 | // FIXME better parsing | 16 | // FIXME better parsing |
17 | bool save(const QString &name, const QValueList<ToDoEvent> &m_todos ){ | 17 | bool save(const QString &name, const QValueList<ToDoEvent> &m_todos ){ |
18 | // prepare the XML | 18 | // prepare the XML |
19 | XMLElement *tasks = new XMLElement( ); | 19 | XMLElement *tasks = new XMLElement( ); |
20 | tasks->setTagName("Tasks" ); | 20 | tasks->setTagName("Tasks" ); |
21 | for( QValueList<ToDoEvent>::ConstIterator it = m_todos.begin(); it != m_todos.end(); ++it ){ | 21 | for( QValueList<ToDoEvent>::ConstIterator it = m_todos.begin(); it != m_todos.end(); ++it ){ |
22 | XMLElement::AttributeMap map; | 22 | XMLElement::AttributeMap map; |
23 | XMLElement *task = new XMLElement(); | 23 | XMLElement *task = new XMLElement(); |
24 | map.insert( "Completed", QString::number((int)(*it).isCompleted() ) ); | 24 | map.insert( "Completed", QString::number((int)(*it).isCompleted() ) ); |
25 | map.insert( "HasDate", QString::number((int)(*it).hasDate() ) ); | 25 | map.insert( "HasDate", QString::number((int)(*it).hasDate() ) ); |
26 | map.insert( "Priority", QString::number( (*it).priority() ) ); | 26 | map.insert( "Priority", QString::number( (*it).priority() ) ); |
27 | map.insert( "Progress", QString::number( (*it).progress() ) ); | ||
27 | map.insert( "Summary", (*it).summary() ); | 28 | map.insert( "Summary", (*it).summary() ); |
28 | QArray<int> arrat = (*it).categories(); | 29 | QArray<int> arrat = (*it).categories(); |
29 | QString attr; | 30 | QString attr; |
30 | for(uint i=0; i < arrat.count(); i++ ){ | 31 | for(uint i=0; i < arrat.count(); i++ ){ |
31 | attr.append(QString::number(arrat[i])+";" ); | 32 | attr.append(QString::number(arrat[i])+";" ); |
32 | } | 33 | } |
33 | if(!attr.isEmpty() ) // remove the last ; | 34 | if(!attr.isEmpty() ) // remove the last ; |
34 | attr.remove(attr.length()-1, 1 ); | 35 | attr.remove(attr.length()-1, 1 ); |
35 | map.insert( "Categories", attr ); | 36 | map.insert( "Categories", attr ); |
36 | //else | 37 | //else |
37 | //map.insert( "Categories", QString::null ); | 38 | //map.insert( "Categories", QString::null ); |
38 | map.insert( "Description", (*it).description() ); | 39 | map.insert( "Description", (*it).description() ); |
39 | if( (*it).hasDate() ){ | 40 | if( (*it).hasDate() ){ |
40 | map.insert("DateYear", QString::number( (*it).date().year() ) ); | 41 | map.insert("DateYear", QString::number( (*it).date().year() ) ); |
41 | map.insert("DateMonth", QString::number( (*it).date().month() ) ); | 42 | map.insert("DateMonth", QString::number( (*it).date().month() ) ); |
42 | map.insert("DateDay", QString::number( (*it).date().day() ) ); | 43 | map.insert("DateDay", QString::number( (*it).date().day() ) ); |
43 | } | 44 | } |
44 | map.insert("Uid", QString::number( (*it).uid() ) ); | 45 | map.insert("Uid", QString::number( (*it).uid() ) ); |
45 | task->setTagName("Task" ); | 46 | task->setTagName("Task" ); |
46 | task->setAttributes( map ); | 47 | task->setAttributes( map ); |
47 | tasks->appendChild(task); | 48 | tasks->appendChild(task); |
48 | } | 49 | } |
49 | QFile file( name); | 50 | QFile file( name); |
50 | if( file.open(IO_WriteOnly ) ){ | 51 | if( file.open(IO_WriteOnly ) ){ |
51 | QTextStream stream(&file ); | 52 | QTextStream stream(&file ); |
52 | stream.setEncoding( QTextStream::UnicodeUTF8 ); | 53 | stream.setEncoding( QTextStream::UnicodeUTF8 ); |
53 | stream << "<!DOCTYPE Tasks>" << endl; | 54 | stream << "<!DOCTYPE Tasks>" << endl; |
54 | tasks->save(stream ); | 55 | tasks->save(stream ); |
55 | delete tasks; | 56 | delete tasks; |
56 | stream << "</Tasks>" << endl; | 57 | stream << "</Tasks>" << endl; |
57 | file.close(); | 58 | file.close(); |
58 | return true; | 59 | return true; |
59 | } | 60 | } |
60 | return false; | 61 | return false; |
61 | } | 62 | } |
62 | QValueList<ToDoEvent> load( const QString &name ){ | 63 | QValueList<ToDoEvent> load( const QString &name ){ |
63 | qWarning("loading tododb" ); | 64 | qWarning("loading tododb" ); |
64 | QValueList<ToDoEvent> m_todos; | 65 | QValueList<ToDoEvent> m_todos; |
65 | XMLElement *root = XMLElement::load( name ); | 66 | XMLElement *root = XMLElement::load( name ); |
66 | if(root != 0l ){ // start parsing | 67 | if(root != 0l ){ // start parsing |
67 | qWarning("ToDoDB::load tagName(): %s", root->tagName().latin1() ); | 68 | qWarning("ToDoDB::load tagName(): %s", root->tagName().latin1() ); |
68 | //if( root->tagName() == QString::fromLatin1("Tasks" ) ){// Start | 69 | //if( root->tagName() == QString::fromLatin1("Tasks" ) ){// Start |
69 | XMLElement *element = root->firstChild(); | 70 | XMLElement *element = root->firstChild(); |
70 | element = element->firstChild(); | 71 | element = element->firstChild(); |
71 | while( element ){ | 72 | while( element ){ |
72 | if( element->tagName() != QString::fromLatin1("Task") ){ | 73 | if( element->tagName() != QString::fromLatin1("Task") ){ |
73 | element = element->nextChild(); | 74 | element = element->nextChild(); |
74 | continue; | 75 | continue; |
75 | } | 76 | } |
76 | qWarning("ToDoDB::load element tagName() : %s", element->tagName().latin1() ); | 77 | qWarning("ToDoDB::load element tagName() : %s", element->tagName().latin1() ); |
77 | QString dummy; | 78 | QString dummy; |
78 | ToDoEvent event; | 79 | ToDoEvent event; |
79 | bool ok; | 80 | bool ok; |
80 | int dumInt; | 81 | int dumInt; |
81 | // completed | 82 | // completed |
82 | dummy = element->attribute("Completed" ); | 83 | dummy = element->attribute("Completed" ); |
83 | dumInt = dummy.toInt(&ok ); | 84 | dumInt = dummy.toInt(&ok ); |
84 | if(ok ) event.setCompleted( dumInt == 0 ? false : true ); | 85 | if(ok ) event.setCompleted( dumInt == 0 ? false : true ); |
86 | // progress | ||
87 | dummy = element->attribute("Progress" ); | ||
88 | { | ||
89 | ushort dumShort = dummy.toUShort(&ok); | ||
90 | event.setProgress( dumShort ); | ||
91 | |||
92 | } | ||
85 | // hasDate | 93 | // hasDate |
86 | dummy = element->attribute("HasDate" ); | 94 | dummy = element->attribute("HasDate" ); |
87 | dumInt = dummy.toInt(&ok ); | 95 | dumInt = dummy.toInt(&ok ); |
88 | if(ok ) event.setHasDate( dumInt == 0 ? false: true ); | 96 | if(ok ) event.setHasDate( dumInt == 0 ? false: true ); |
89 | // set the date | 97 | // set the date |
90 | bool hasDa = dumInt; | 98 | bool hasDa = dumInt; |
91 | if ( hasDa ) { //parse the date | 99 | if ( hasDa ) { //parse the date |
92 | int year, day, month = 0; | 100 | int year, day, month = 0; |
93 | year = day = month; | 101 | year = day = month; |
94 | // year | 102 | // year |
95 | dummy = element->attribute("DateYear" ); | 103 | dummy = element->attribute("DateYear" ); |
96 | dumInt = dummy.toInt(&ok ); | 104 | dumInt = dummy.toInt(&ok ); |
97 | if( ok ) year = dumInt; | 105 | if( ok ) year = dumInt; |
98 | // month | 106 | // month |
99 | dummy = element->attribute("DateMonth" ); | 107 | dummy = element->attribute("DateMonth" ); |
100 | dumInt = dummy.toInt(&ok ); | 108 | dumInt = dummy.toInt(&ok ); |
101 | if(ok ) month = dumInt; | 109 | if(ok ) month = dumInt; |
102 | dummy = element->attribute("DateDay" ); | 110 | dummy = element->attribute("DateDay" ); |
103 | dumInt = dummy.toInt(&ok ); | 111 | dumInt = dummy.toInt(&ok ); |
104 | if(ok ) day = dumInt; | 112 | if(ok ) day = dumInt; |
105 | // set the date | 113 | // set the date |
106 | QDate date( year, month, day ); | 114 | QDate date( year, month, day ); |
107 | event.setDate( date); | 115 | event.setDate( date); |
108 | } | 116 | } |
109 | dummy = element->attribute("Priority" ); | 117 | dummy = element->attribute("Priority" ); |
110 | dumInt = dummy.toInt(&ok ); | 118 | dumInt = dummy.toInt(&ok ); |
111 | if(!ok ) dumInt = ToDoEvent::NORMAL; | 119 | if(!ok ) dumInt = ToDoEvent::NORMAL; |
112 | event.setPriority( dumInt ); | 120 | event.setPriority( dumInt ); |
113 | //description | 121 | //description |
114 | dummy = element->attribute("Description" ); | 122 | dummy = element->attribute("Description" ); |
115 | event.setDescription( dummy ); | 123 | event.setDescription( dummy ); |
116 | dummy = element->attribute("Summary" ); | 124 | dummy = element->attribute("Summary" ); |
117 | event.setSummary( dummy ); | 125 | event.setSummary( dummy ); |
118 | // category | 126 | // category |
119 | dummy = element->attribute("Categories" ); | 127 | dummy = element->attribute("Categories" ); |
120 | QStringList ids = QStringList::split(";", dummy ); | 128 | QStringList ids = QStringList::split(";", dummy ); |
121 | event.setCategories( ids ); | 129 | event.setCategories( ids ); |
122 | 130 | ||
123 | //uid | 131 | //uid |
124 | dummy = element->attribute("Uid" ); | 132 | dummy = element->attribute("Uid" ); |
125 | dumInt = dummy.toInt(&ok ); | 133 | dumInt = dummy.toInt(&ok ); |
126 | if(ok ) event.setUid( dumInt ); | 134 | if(ok ) event.setUid( dumInt ); |
127 | m_todos.append( event ); | 135 | m_todos.append( event ); |
128 | element = element->nextChild(); // next element | 136 | element = element->nextChild(); // next element |
129 | } | 137 | } |
130 | //} | 138 | //} |
131 | }else { | 139 | }else { |
132 | qWarning("could not load" ); | 140 | qWarning("could not load" ); |
133 | } | 141 | } |
134 | delete root; | 142 | delete root; |
135 | qWarning("returning" ); | 143 | qWarning("returning" ); |
136 | return m_todos; | 144 | return m_todos; |
137 | } | 145 | } |
138 | }; | 146 | }; |
139 | 147 | ||
140 | } | 148 | } |
141 | 149 | ||
142 | ToDoDB::ToDoDB(const QString &fileName, ToDoResource *res ){ | 150 | ToDoDB::ToDoDB(const QString &fileName, ToDoResource *res ){ |
143 | m_fileName = fileName; | 151 | m_fileName = fileName; |
144 | if( fileName.isEmpty() && res == 0 ){ | 152 | if( fileName.isEmpty() && res == 0 ){ |
145 | m_fileName = Global::applicationFileName("todolist","todolist.xml"); | 153 | m_fileName = Global::applicationFileName("todolist","todolist.xml"); |
146 | res = new FileToDoResource(); | 154 | res = new FileToDoResource(); |
147 | //qWarning("%s", m_fileName.latin1() ); | 155 | //qWarning("%s", m_fileName.latin1() ); |
148 | }else if(res == 0 ){ // let's create a ToDoResource for xml | 156 | }else if(res == 0 ){ // let's create a ToDoResource for xml |
149 | res = new FileToDoResource(); | 157 | res = new FileToDoResource(); |
150 | } | 158 | } |
151 | m_res = res; | 159 | m_res = res; |
152 | load(); | 160 | load(); |
153 | } | 161 | } |
154 | ToDoResource* ToDoDB::resource(){ | 162 | ToDoResource* ToDoDB::resource(){ |
155 | return m_res; | 163 | return m_res; |
156 | }; | 164 | }; |
157 | void ToDoDB::setResource( ToDoResource *res ) | 165 | void ToDoDB::setResource( ToDoResource *res ) |
158 | { | 166 | { |
159 | delete m_res; | 167 | delete m_res; |
160 | m_res = res; | 168 | m_res = res; |
161 | } | 169 | } |
162 | ToDoDB::~ToDoDB() | 170 | ToDoDB::~ToDoDB() |
163 | { | 171 | { |
164 | delete m_res; | 172 | delete m_res; |
165 | } | 173 | } |
166 | QValueList<ToDoEvent> ToDoDB::effectiveToDos(const QDate &from, const QDate &to, | 174 | QValueList<ToDoEvent> ToDoDB::effectiveToDos(const QDate &from, const QDate &to, |
167 | bool all ) | 175 | bool all ) |
168 | { | 176 | { |
169 | QValueList<ToDoEvent> events; | 177 | QValueList<ToDoEvent> events; |
170 | for( QValueList<ToDoEvent>::Iterator it = m_todos.begin(); it!= m_todos.end(); ++it ){ | 178 | for( QValueList<ToDoEvent>::Iterator it = m_todos.begin(); it!= m_todos.end(); ++it ){ |
171 | if( (*it).hasDate() ){ | 179 | if( (*it).hasDate() ){ |
172 | if( (*it).date() >= from && (*it).date() <= to ) | 180 | if( (*it).date() >= from && (*it).date() <= to ) |
173 | events.append( (*it) ); | 181 | events.append( (*it) ); |
174 | }else if( all ){ | 182 | }else if( all ){ |
175 | events.append( (*it) ); | 183 | events.append( (*it) ); |
176 | } | 184 | } |
177 | } | 185 | } |
178 | return events; | 186 | return events; |
179 | } | 187 | } |
180 | QValueList<ToDoEvent> ToDoDB::effectiveToDos(const QDate &from, | 188 | QValueList<ToDoEvent> ToDoDB::effectiveToDos(const QDate &from, |
181 | bool all) | 189 | bool all) |
182 | { | 190 | { |
183 | return effectiveToDos( from, QDate::currentDate(), all ); | 191 | return effectiveToDos( from, QDate::currentDate(), all ); |
184 | } | 192 | } |
185 | QValueList<ToDoEvent> ToDoDB::overDue() | 193 | QValueList<ToDoEvent> ToDoDB::overDue() |
186 | { | 194 | { |
187 | QValueList<ToDoEvent> events; | 195 | QValueList<ToDoEvent> events; |
188 | for( QValueList<ToDoEvent>::Iterator it = m_todos.begin(); it!= m_todos.end(); ++it ){ | 196 | for( QValueList<ToDoEvent>::Iterator it = m_todos.begin(); it!= m_todos.end(); ++it ){ |
189 | if( (*it).isOverdue() ) | 197 | if( (*it).isOverdue() ) |
190 | events.append((*it) ); | 198 | events.append((*it) ); |
191 | } | 199 | } |
192 | return events; | 200 | return events; |
193 | } | 201 | } |
194 | QValueList<ToDoEvent> ToDoDB::rawToDos() | 202 | QValueList<ToDoEvent> ToDoDB::rawToDos() |
195 | { | 203 | { |
196 | return m_todos; | 204 | return m_todos; |
197 | } | 205 | } |
198 | void ToDoDB::addEvent( const ToDoEvent &event ) | 206 | void ToDoDB::addEvent( const ToDoEvent &event ) |
199 | { | 207 | { |
200 | m_todos.append( event ); | 208 | m_todos.append( event ); |
201 | } | 209 | } |
202 | void ToDoDB::editEvent( const ToDoEvent &event ) | 210 | void ToDoDB::editEvent( const ToDoEvent &event ) |
203 | { | 211 | { |
204 | m_todos.remove( event ); | 212 | m_todos.remove( event ); |
205 | m_todos.append( event ); | 213 | m_todos.append( event ); |
206 | } | 214 | } |
207 | void ToDoDB::removeEvent( const ToDoEvent &event ) | 215 | void ToDoDB::removeEvent( const ToDoEvent &event ) |
208 | { | 216 | { |
209 | m_todos.remove( event ); | 217 | m_todos.remove( event ); |
210 | } | 218 | } |
211 | void ToDoDB::replaceEvent(const ToDoEvent &event ) | 219 | void ToDoDB::replaceEvent(const ToDoEvent &event ) |
212 | { | 220 | { |
213 | QValueList<ToDoEvent>::Iterator it; | 221 | QValueList<ToDoEvent>::Iterator it; |
214 | int uid = event.uid(); | 222 | int uid = event.uid(); |
215 | // == is not overloaded as we would like :( so let's search for the uid | 223 | // == is not overloaded as we would like :( so let's search for the uid |
216 | for(it = m_todos.begin(); it != m_todos.end(); ++it ){ | 224 | for(it = m_todos.begin(); it != m_todos.end(); ++it ){ |
217 | if( (*it).uid() == uid ){ | 225 | if( (*it).uid() == uid ){ |
218 | m_todos.remove( (*it) ); | 226 | m_todos.remove( (*it) ); |
219 | break; // should save us the iterate is now borked | 227 | break; // should save us the iterate is now borked |
220 | } | 228 | } |
221 | } | 229 | } |
222 | m_todos.append(event); | 230 | m_todos.append(event); |
223 | } | 231 | } |
224 | void ToDoDB::reload() | 232 | void ToDoDB::reload() |
225 | { | 233 | { |
226 | load(); | 234 | load(); |
227 | } | 235 | } |
228 | void ToDoDB::mergeWith(const QValueList<ToDoEvent>& events ) | 236 | void ToDoDB::mergeWith(const QValueList<ToDoEvent>& events ) |
229 | { | 237 | { |
230 | QValueList<ToDoEvent>::ConstIterator it; | 238 | QValueList<ToDoEvent>::ConstIterator it; |
231 | for( it = events.begin(); it != events.end(); ++it ){ | 239 | for( it = events.begin(); it != events.end(); ++it ){ |
232 | replaceEvent( (*it) ); | 240 | replaceEvent( (*it) ); |
233 | } | 241 | } |
234 | } | 242 | } |
235 | void ToDoDB::setFileName(const QString &file ) | 243 | void ToDoDB::setFileName(const QString &file ) |
236 | { | 244 | { |
237 | m_fileName =file; | 245 | m_fileName =file; |
238 | } | 246 | } |
239 | QString ToDoDB::fileName()const | 247 | QString ToDoDB::fileName()const |
240 | { | 248 | { |
241 | return m_fileName; | 249 | return m_fileName; |
242 | } | 250 | } |
243 | void ToDoDB::load() | 251 | void ToDoDB::load() |
244 | { | 252 | { |
245 | m_todos = m_res->load( m_fileName ); | 253 | m_todos = m_res->load( m_fileName ); |
246 | } | 254 | } |
247 | bool ToDoDB::save() | 255 | bool ToDoDB::save() |
248 | { | 256 | { |
249 | return m_res->save( m_fileName, m_todos ); | 257 | return m_res->save( m_fileName, m_todos ); |
250 | } | 258 | } |
251 | 259 | ||
252 | 260 | ||
253 | 261 | ||
254 | 262 | ||
255 | 263 | ||
256 | 264 | ||
257 | 265 | ||
258 | 266 | ||