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