summaryrefslogtreecommitdiff
path: root/libopie2/opiepim/backend/otodoaccessxml.cpp
Unidiff
Diffstat (limited to 'libopie2/opiepim/backend/otodoaccessxml.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opiepim/backend/otodoaccessxml.cpp20
1 files changed, 12 insertions, 8 deletions
diff --git a/libopie2/opiepim/backend/otodoaccessxml.cpp b/libopie2/opiepim/backend/otodoaccessxml.cpp
index 3a72881..5fe733c 100644
--- a/libopie2/opiepim/backend/otodoaccessxml.cpp
+++ b/libopie2/opiepim/backend/otodoaccessxml.cpp
@@ -12,25 +12,25 @@ OTodoAccessXML::OTodoAccessXML( const QString& appName,
12 const QString& fileName ) 12 const QString& fileName )
13 : OTodoAccessBackend(), m_app( appName ), m_opened( false ), m_changed( false ) 13 : OTodoAccessBackend(), m_app( appName ), m_opened( false ), m_changed( false )
14{ 14{
15 if (!fileName.isEmpty() ) 15 if (!fileName.isEmpty() )
16 m_file = fileName; 16 m_file = fileName;
17 else 17 else
18 m_file = Global::applicationFileName( "todolist", "todolist.xml" ); 18 m_file = Global::applicationFileName( "todolist", "todolist.xml" );
19} 19}
20OTodoAccessXML::~OTodoAccessXML() { 20OTodoAccessXML::~OTodoAccessXML() {
21 21
22} 22}
23bool OTodoAccessXML::load() { 23bool OTodoAccessXML::load() {
24 m_opened = false; 24 m_opened = true;
25 m_changed = false; 25 m_changed = false;
26 /* initialize dict */ 26 /* initialize dict */
27 /* 27 /*
28 * UPDATE dict if you change anything!!! 28 * UPDATE dict if you change anything!!!
29 */ 29 */
30 QAsciiDict<int> dict(15); 30 QAsciiDict<int> dict(15);
31 dict.setAutoDelete( TRUE ); 31 dict.setAutoDelete( TRUE );
32 dict.insert("Categories" , new int(OTodo::Category) ); 32 dict.insert("Categories" , new int(OTodo::Category) );
33 dict.insert("Uid" , new int(OTodo::Uid) ); 33 dict.insert("Uid" , new int(OTodo::Uid) );
34 dict.insert("HasDate" , new int(OTodo::HasDate) ); 34 dict.insert("HasDate" , new int(OTodo::HasDate) );
35 dict.insert("Completed" , new int(OTodo::Completed) ); 35 dict.insert("Completed" , new int(OTodo::Completed) );
36 dict.insert("Description" , new int(OTodo::Description) ); 36 dict.insert("Description" , new int(OTodo::Description) );
@@ -60,39 +60,41 @@ bool OTodoAccessXML::load() {
60 while ( element ) { 60 while ( element ) {
61 if ( element->tagName() != QString::fromLatin1("Task") ) { 61 if ( element->tagName() != QString::fromLatin1("Task") ) {
62 element = element->nextChild(); 62 element = element->nextChild();
63 continue; 63 continue;
64 } 64 }
65 /* here is the right element for a task */ 65 /* here is the right element for a task */
66 OTodo ev = todo( &dict, element ); 66 OTodo ev = todo( &dict, element );
67 m_events.insert( ev.uid(), ev ); 67 m_events.insert( ev.uid(), ev );
68 68
69 element = element->nextChild(); 69 element = element->nextChild();
70 } 70 }
71 }else { 71 }else {
72 qWarning("could not parse"); 72// qWarning("could not parse");
73 return false;; 73 return false;;
74 } 74 }
75 delete root; 75 delete root;
76 76
77 m_opened = true; 77// qWarning("Access %d" + m_events.count() );
78 qWarning("Access %d" + m_events.count() );
79 return true; 78 return true;
80} 79}
81bool OTodoAccessXML::reload() { 80bool OTodoAccessXML::reload() {
82 return load(); 81 return load();
83} 82}
84bool OTodoAccessXML::save() { 83bool OTodoAccessXML::save() {
85 if (!m_opened || !m_changed ) 84// qWarning("saving");
85 if (!m_opened || !m_changed ) {
86// qWarning("not saving");
86 return true; 87 return true;
88 }
87 QString strNewFile = m_file + ".new"; 89 QString strNewFile = m_file + ".new";
88 QFile f( strNewFile ); 90 QFile f( strNewFile );
89 if (!f.open( IO_WriteOnly|IO_Raw ) ) 91 if (!f.open( IO_WriteOnly|IO_Raw ) )
90 return false; 92 return false;
91 93
92 int written; 94 int written;
93 QString out; 95 QString out;
94 out = "<!DOCTYPE Tasks>\n<Tasks>\n"; 96 out = "<!DOCTYPE Tasks>\n<Tasks>\n";
95 97
96 // for all todos 98 // for all todos
97 QMap<int, OTodo>::Iterator it; 99 QMap<int, OTodo>::Iterator it;
98 for (it = m_events.begin(); it != m_events.end(); ++it ) { 100 for (it = m_events.begin(); it != m_events.end(); ++it ) {
@@ -113,25 +115,25 @@ bool OTodoAccessXML::save() {
113 QCString cstr = out.utf8(); 115 QCString cstr = out.utf8();
114 written = f.writeBlock( cstr.data(), cstr.length() ); 116 written = f.writeBlock( cstr.data(), cstr.length() );
115 117
116 if ( written != (int)cstr.length() ) { 118 if ( written != (int)cstr.length() ) {
117 f.close(); 119 f.close();
118 QFile::remove( strNewFile ); 120 QFile::remove( strNewFile );
119 return false; 121 return false;
120 } 122 }
121 /* flush before renaming */ 123 /* flush before renaming */
122 f.close(); 124 f.close();
123 125
124 if( ::rename( strNewFile.latin1(), m_file.latin1() ) < 0 ) { 126 if( ::rename( strNewFile.latin1(), m_file.latin1() ) < 0 ) {
125 qWarning("error renaming"); 127// qWarning("error renaming");
126 QFile::remove( strNewFile ); 128 QFile::remove( strNewFile );
127 } 129 }
128 130
129 m_changed = false; 131 m_changed = false;
130 return true; 132 return true;
131} 133}
132QArray<int> OTodoAccessXML::allRecords()const { 134QArray<int> OTodoAccessXML::allRecords()const {
133 QArray<int> ids( m_events.count() ); 135 QArray<int> ids( m_events.count() );
134 QMap<int, OTodo>::ConstIterator it; 136 QMap<int, OTodo>::ConstIterator it;
135 int i = 0; 137 int i = 0;
136 138
137 for ( it = m_events.begin(); it != m_events.end(); ++it ) { 139 for ( it = m_events.begin(); it != m_events.end(); ++it ) {
@@ -151,24 +153,25 @@ OTodo OTodoAccessXML::find( int uid )const {
151 if ( it != m_events.end() ) 153 if ( it != m_events.end() )
152 todo = it.data(); 154 todo = it.data();
153 155
154 return todo; 156 return todo;
155} 157}
156void OTodoAccessXML::clear() { 158void OTodoAccessXML::clear() {
157 if (m_opened ) 159 if (m_opened )
158 m_changed = true; 160 m_changed = true;
159 161
160 m_events.clear(); 162 m_events.clear();
161} 163}
162bool OTodoAccessXML::add( const OTodo& todo ) { 164bool OTodoAccessXML::add( const OTodo& todo ) {
165// qWarning("add");
163 m_changed = true; 166 m_changed = true;
164 m_events.insert( todo.uid(), todo ); 167 m_events.insert( todo.uid(), todo );
165 168
166 return true; 169 return true;
167} 170}
168bool OTodoAccessXML::remove( int uid ) { 171bool OTodoAccessXML::remove( int uid ) {
169 m_changed = true; 172 m_changed = true;
170 m_events.remove( uid ); 173 m_events.remove( uid );
171 174
172 return true; 175 return true;
173} 176}
174bool OTodoAccessXML::replace( const OTodo& todo) { 177bool OTodoAccessXML::replace( const OTodo& todo) {
@@ -208,36 +211,36 @@ QArray<int> OTodoAccessXML::overDue() {
208 if ( it.data().isOverdue() ) { 211 if ( it.data().isOverdue() ) {
209 ids[i] = it.key(); 212 ids[i] = it.key();
210 i++; 213 i++;
211 } 214 }
212 } 215 }
213 ids.resize( i ); 216 ids.resize( i );
214 return ids; 217 return ids;
215} 218}
216 219
217 220
218/* private */ 221/* private */
219OTodo OTodoAccessXML::todo( QAsciiDict<int>* dict, Opie::XMLElement* element)const { 222OTodo OTodoAccessXML::todo( QAsciiDict<int>* dict, Opie::XMLElement* element)const {
220 qWarning("parse to do from XMLElement" ); 223// qWarning("parse to do from XMLElement" );
221 OTodo ev; 224 OTodo ev;
222 QMap<QString, QString> attributes = element->attributes(); 225 QMap<QString, QString> attributes = element->attributes();
223 QMap<QString, QString>::Iterator it; 226 QMap<QString, QString>::Iterator it;
224 227
225 int *find=0; 228 int *find=0;
226 int day, month, year; 229 int day, month, year;
227 day = month = year = -1; 230 day = month = year = -1;
228 for ( it = attributes.begin(); it != attributes.end(); ++it ) { 231 for ( it = attributes.begin(); it != attributes.end(); ++it ) {
229 find = (*dict)[ it.key() ]; 232 find = (*dict)[ it.key() ];
230 if (!find ) { 233 if (!find ) {
231 qWarning("Unknown option" + it.key() ); 234// qWarning("Unknown option" + it.key() );
232 ev.setCustomField( it.key(), it.data() ); 235 ev.setCustomField( it.key(), it.data() );
233 continue; 236 continue;
234 } 237 }
235 238
236 switch( *find ) { 239 switch( *find ) {
237 case OTodo::Uid: 240 case OTodo::Uid:
238 ev.setUid( it.data().toInt() ); 241 ev.setUid( it.data().toInt() );
239 break; 242 break;
240 case OTodo::Category: 243 case OTodo::Category:
241 ev.setCategories( ev.idsFromString( it.data() ) ); 244 ev.setCategories( ev.idsFromString( it.data() ) );
242 break; 245 break;
243 case OTodo::HasDate: 246 case OTodo::HasDate:
@@ -311,24 +314,25 @@ QString OTodoAccessXML::toString( const OTodo& ev )const {
311 str += "Priority=\"" + QString::number( ev.priority() ) + "\" "; 314 str += "Priority=\"" + QString::number( ev.priority() ) + "\" ";
312 str += "Progress=\"" + QString::number(ev.progress() ) + "\" "; 315 str += "Progress=\"" + QString::number(ev.progress() ) + "\" ";
313 316
314 str += "Categories=\"" + toString( ev.categories() ) + "\" "; 317 str += "Categories=\"" + toString( ev.categories() ) + "\" ";
315 str += "Description=\"" + Qtopia::escapeString( ev.description() ) + "\" "; 318 str += "Description=\"" + Qtopia::escapeString( ev.description() ) + "\" ";
316 str += "Summary=\"" + Qtopia::escapeString( ev.summary() ) + "\" "; 319 str += "Summary=\"" + Qtopia::escapeString( ev.summary() ) + "\" ";
317 320
318 if ( ev.hasDueDate() ) { 321 if ( ev.hasDueDate() ) {
319 str += "DateYear=\"" + QString::number( ev.dueDate().year() ) + "\" "; 322 str += "DateYear=\"" + QString::number( ev.dueDate().year() ) + "\" ";
320 str += "DateMonth=\"" + QString::number( ev.dueDate().month() ) + "\" "; 323 str += "DateMonth=\"" + QString::number( ev.dueDate().month() ) + "\" ";
321 str += "DateDay=\"" + QString::number( ev.dueDate().day() ) + "\" "; 324 str += "DateDay=\"" + QString::number( ev.dueDate().day() ) + "\" ";
322 } 325 }
326// qWarning( "Uid %d", ev.uid() );
323 str += "Uid=\"" + QString::number( ev.uid() ) + "\" "; 327 str += "Uid=\"" + QString::number( ev.uid() ) + "\" ";
324 328
325// append the extra options 329// append the extra options
326 /* FIXME Qtopia::Record this is currently not 330 /* FIXME Qtopia::Record this is currently not
327 * possible you can set custom fields 331 * possible you can set custom fields
328 * but don' iterate over the list 332 * but don' iterate over the list
329 * I may do #define private protected 333 * I may do #define private protected
330 * for this case - cough --zecke 334 * for this case - cough --zecke
331 */ 335 */
332 /* 336 /*
333 QMap<QString, QString> extras = ev.extras(); 337 QMap<QString, QString> extras = ev.extras();
334 QMap<QString, QString>::Iterator extIt; 338 QMap<QString, QString>::Iterator extIt;