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