summaryrefslogtreecommitdiff
path: root/libopie
Unidiff
Diffstat (limited to 'libopie') (more/less context) (ignore whitespace changes)
-rw-r--r--libopie/tododb.cpp4
1 files changed, 4 insertions, 0 deletions
diff --git a/libopie/tododb.cpp b/libopie/tododb.cpp
index 4d6711d..c486723 100644
--- a/libopie/tododb.cpp
+++ b/libopie/tododb.cpp
@@ -1,162 +1,166 @@
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
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 if( element->tagName() != QString::fromLatin1("Task") ){
68 element = element->nextChild();
69 continue;
70 }
67 qWarning("ToDoDB::load element tagName() : %s", element->tagName().latin1() ); 71 qWarning("ToDoDB::load element tagName() : %s", element->tagName().latin1() );
68 QString dummy; 72 QString dummy;
69 ToDoEvent event; 73 ToDoEvent event;
70 bool ok; 74 bool ok;
71 int dumInt; 75 int dumInt;
72 // completed 76 // completed
73 dummy = element->attribute("Completed" ); 77 dummy = element->attribute("Completed" );
74 dumInt = dummy.toInt(&ok ); 78 dumInt = dummy.toInt(&ok );
75 if(ok ) event.setCompleted( dumInt == 0 ? false : true ); 79 if(ok ) event.setCompleted( dumInt == 0 ? false : true );
76 // hasDate 80 // hasDate
77 dummy = element->attribute("HasDate" ); 81 dummy = element->attribute("HasDate" );
78 dumInt = dummy.toInt(&ok ); 82 dumInt = dummy.toInt(&ok );
79 if(ok ) event.setHasDate( dumInt == 0 ? false: true ); 83 if(ok ) event.setHasDate( dumInt == 0 ? false: true );
80 // set the date 84 // set the date
81 bool hasDa = dumInt; 85 bool hasDa = dumInt;
82 if ( hasDa ) { //parse the date 86 if ( hasDa ) { //parse the date
83 int year, day, month = 0; 87 int year, day, month = 0;
84 year = day = month; 88 year = day = month;
85 // year 89 // year
86 dummy = element->attribute("DateYear" ); 90 dummy = element->attribute("DateYear" );
87 dumInt = dummy.toInt(&ok ); 91 dumInt = dummy.toInt(&ok );
88 if( ok ) year = dumInt; 92 if( ok ) year = dumInt;
89 // month 93 // month
90 dummy = element->attribute("DateMonth" ); 94 dummy = element->attribute("DateMonth" );
91 dumInt = dummy.toInt(&ok ); 95 dumInt = dummy.toInt(&ok );
92 if(ok ) month = dumInt; 96 if(ok ) month = dumInt;
93 dummy = element->attribute("DateDay" ); 97 dummy = element->attribute("DateDay" );
94 dumInt = dummy.toInt(&ok ); 98 dumInt = dummy.toInt(&ok );
95 if(ok ) day = dumInt; 99 if(ok ) day = dumInt;
96 // set the date 100 // set the date
97 QDate date( year, month, day ); 101 QDate date( year, month, day );
98 event.setDate( date); 102 event.setDate( date);
99 } 103 }
100 dummy = element->attribute("Priority" ); 104 dummy = element->attribute("Priority" );
101 dumInt = dummy.toInt(&ok ); 105 dumInt = dummy.toInt(&ok );
102 if(!ok ) dumInt = ToDoEvent::NORMAL; 106 if(!ok ) dumInt = ToDoEvent::NORMAL;
103 event.setPriority( dumInt ); 107 event.setPriority( dumInt );
104 //description 108 //description
105 dummy = element->attribute("Description" ); 109 dummy = element->attribute("Description" );
106 event.setDescription( dummy ); 110 event.setDescription( dummy );
107 // category 111 // category
108 dummy = element->attribute("Categories" ); 112 dummy = element->attribute("Categories" );
109 QStringList ids = QStringList::split(";", dummy ); 113 QStringList ids = QStringList::split(";", dummy );
110 event.setCategories( ids ); 114 event.setCategories( ids );
111 115
112 //uid 116 //uid
113 dummy = element->attribute("Uid" ); 117 dummy = element->attribute("Uid" );
114 dumInt = dummy.toInt(&ok ); 118 dumInt = dummy.toInt(&ok );
115 if(ok ) event.setUid( dumInt ); 119 if(ok ) event.setUid( dumInt );
116 m_todos.append( event ); 120 m_todos.append( event );
117 element = element->nextChild(); // next element 121 element = element->nextChild(); // next element
118 } 122 }
119 //} 123 //}
120 }else { 124 }else {
121 qWarning("could not load" ); 125 qWarning("could not load" );
122 } 126 }
123 delete root; 127 delete root;
124 qWarning("returning" ); 128 qWarning("returning" );
125 return m_todos; 129 return m_todos;
126 } 130 }
127}; 131};
128 132
129} 133}
130 134
131ToDoDB::ToDoDB(const QString &fileName, ToDoResource *res ){ 135ToDoDB::ToDoDB(const QString &fileName, ToDoResource *res ){
132 m_fileName = fileName; 136 m_fileName = fileName;
133 if( fileName.isEmpty() && res == 0 ){ 137 if( fileName.isEmpty() && res == 0 ){
134 m_fileName = Global::applicationFileName("todolist","todolist.xml"); 138 m_fileName = Global::applicationFileName("todolist","todolist.xml");
135 res = new FileToDoResource(); 139 res = new FileToDoResource();
136 //qWarning("%s", m_fileName.latin1() ); 140 //qWarning("%s", m_fileName.latin1() );
137 }else if(res == 0 ){ // let's create a ToDoResource for xml 141 }else if(res == 0 ){ // let's create a ToDoResource for xml
138 res = new FileToDoResource(); 142 res = new FileToDoResource();
139 } 143 }
140 m_res = res; 144 m_res = res;
141 load(); 145 load();
142} 146}
143ToDoResource* ToDoDB::resource(){ 147ToDoResource* ToDoDB::resource(){
144 return m_res; 148 return m_res;
145}; 149};
146void ToDoDB::setResource( ToDoResource *res ) 150void ToDoDB::setResource( ToDoResource *res )
147{ 151{
148 delete m_res; 152 delete m_res;
149 m_res = res; 153 m_res = res;
150} 154}
151ToDoDB::~ToDoDB() 155ToDoDB::~ToDoDB()
152{ 156{
153 delete m_res; 157 delete m_res;
154} 158}
155QValueList<ToDoEvent> ToDoDB::effectiveToDos(const QDate &from, const QDate &to, 159QValueList<ToDoEvent> ToDoDB::effectiveToDos(const QDate &from, const QDate &to,
156 bool all ) 160 bool all )
157{ 161{
158 QValueList<ToDoEvent> events; 162 QValueList<ToDoEvent> events;
159 for( QValueList<ToDoEvent>::Iterator it = m_todos.begin(); it!= m_todos.end(); ++it ){ 163 for( QValueList<ToDoEvent>::Iterator it = m_todos.begin(); it!= m_todos.end(); ++it ){
160 if( (*it).hasDate() ){ 164 if( (*it).hasDate() ){
161 if( (*it).date() >= from && (*it).date() <= to ) 165 if( (*it).date() >= from && (*it).date() <= to )
162 events.append( (*it) ); 166 events.append( (*it) );