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