-rw-r--r-- | libopie/tododb.cpp | 237 | ||||
-rw-r--r-- | libopie/tododb.h | 6 | ||||
-rw-r--r-- | libopie/todoevent.h | 1 | ||||
-rw-r--r-- | libopie/todoresource.h | 14 |
4 files changed, 148 insertions, 110 deletions
diff --git a/libopie/tododb.cpp b/libopie/tododb.cpp index eb17674..91331d0 100644 --- a/libopie/tododb.cpp +++ b/libopie/tododb.cpp | |||
@@ -2,16 +2,138 @@ | |||
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 <qpe/palmtoprecord.h> | 6 | #include <qpe/palmtoprecord.h> |
6 | #include <qpe/global.h> | 7 | #include <qpe/global.h> |
7 | 8 | ||
8 | ToDoDB::ToDoDB(const QString &fileName = QString::null ){ | 9 | namespace { |
10 | |||
11 | class FileToDoResource : public ToDoResource { | ||
12 | public: | ||
13 | FileToDoResource() {}; | ||
14 | bool save(const QString &name, const QValueList<ToDoEvent> &m_todos ){ | ||
15 | // prepare the XML | ||
16 | XMLElement *tasks = new XMLElement( ); | ||
17 | tasks->setTagName("Tasks" ); | ||
18 | for( QValueList<ToDoEvent>::ConstIterator it = m_todos.begin(); it != m_todos.end(); ++it ){ | ||
19 | XMLElement::AttributeMap map; | ||
20 | XMLElement *task = new XMLElement(); | ||
21 | map.insert( "Completed", QString::number((int)(*it).isCompleted() ) ); | ||
22 | map.insert( "HasDate", QString::number((int)(*it).hasDate() ) ); | ||
23 | map.insert( "Priority", QString::number( (*it).priority() ) ); | ||
24 | if(!(*it).category().isEmpty() ){ | ||
25 | QArray<int> arrat(1); | ||
26 | arrat = Qtopia::Record::idsFromString( (*it).category() ); | ||
27 | map.insert( "Categories", QString::number( arrat[0] ) ); | ||
28 | }else | ||
29 | map.insert( "Categories", QString::null ); | ||
30 | map.insert( "Description", (*it).description() ); | ||
31 | if( (*it).hasDate() ){ | ||
32 | map.insert("DateYear", QString::number( (*it).date().year() ) ); | ||
33 | map.insert("DateMonth", QString::number( (*it).date().month() ) ); | ||
34 | map.insert("DateDay", QString::number( (*it).date().day() ) ); | ||
35 | } | ||
36 | map.insert("Uid", QString::number( (*it).uid() ) ); | ||
37 | task->setTagName("Task" ); | ||
38 | task->setAttributes( map ); | ||
39 | tasks->appendChild(task); | ||
40 | } | ||
41 | QFile file( name); | ||
42 | if( file.open(IO_WriteOnly ) ){ | ||
43 | QTextStream stream(&file ); | ||
44 | stream << "<!DOCTYPE Tasks>" << endl; | ||
45 | tasks->save(stream ); | ||
46 | delete tasks; | ||
47 | file.close(); | ||
48 | return true; | ||
49 | } | ||
50 | return false; | ||
51 | } | ||
52 | QValueList<ToDoEvent> load( const QString &name ){ | ||
53 | qWarning("loading tododb" ); | ||
54 | QValueList<ToDoEvent> m_todos; | ||
55 | XMLElement *root = XMLElement::load( name ); | ||
56 | if(root != 0l ){ // start parsing | ||
57 | qWarning("ToDoDB::load tagName(): %s", root->tagName().latin1() ); | ||
58 | //if( root->tagName() == QString::fromLatin1("Tasks" ) ){// Start | ||
59 | XMLElement *element = root->firstChild(); | ||
60 | element = element->firstChild(); | ||
61 | while( element ){ | ||
62 | qWarning("ToDoDB::load element tagName() : %s", element->tagName().latin1() ); | ||
63 | QString dummy; | ||
64 | ToDoEvent event; | ||
65 | bool ok; | ||
66 | int dumInt; | ||
67 | // completed | ||
68 | dummy = element->attribute("Completed" ); | ||
69 | dumInt = dummy.toInt(&ok ); | ||
70 | if(ok ) event.setCompleted( dumInt == 0 ? false : true ); | ||
71 | // hasDate | ||
72 | dummy = element->attribute("HasDate" ); | ||
73 | dumInt = dummy.toInt(&ok ); | ||
74 | if(ok ) event.setHasDate( dumInt == 0 ? false: true ); | ||
75 | // set the date | ||
76 | bool hasDa = dumInt; | ||
77 | if ( hasDa ) { //parse the date | ||
78 | int year, day, month = 0; | ||
79 | year = day = month; | ||
80 | // year | ||
81 | dummy = element->attribute("DateYear" ); | ||
82 | dumInt = dummy.toInt(&ok ); | ||
83 | if( ok ) year = dumInt; | ||
84 | // month | ||
85 | dummy = element->attribute("DateMonth" ); | ||
86 | dumInt = dummy.toInt(&ok ); | ||
87 | if(ok ) month = dumInt; | ||
88 | dummy = element->attribute("DateDay" ); | ||
89 | dumInt = dummy.toInt(&ok ); | ||
90 | if(ok ) day = dumInt; | ||
91 | // set the date | ||
92 | QDate date( year, month, day ); | ||
93 | event.setDate( date); | ||
94 | } | ||
95 | dummy = element->attribute("Priority" ); | ||
96 | dumInt = dummy.toInt(&ok ); | ||
97 | if(!ok ) dumInt = ToDoEvent::NORMAL; | ||
98 | event.setPriority( dumInt ); | ||
99 | //description | ||
100 | dummy = element->attribute("Description" ); | ||
101 | event.setDescription( dummy ); | ||
102 | // category | ||
103 | dummy = element->attribute("Categories" ); | ||
104 | dumInt = dummy.toInt(&ok ); | ||
105 | if(ok ) { | ||
106 | QArray<int> arrat(1); | ||
107 | arrat[0] = dumInt; | ||
108 | event.setCategory( Qtopia::Record::idsToString( arrat ) ); | ||
109 | } | ||
110 | //uid | ||
111 | dummy = element->attribute("Uid" ); | ||
112 | dumInt = dummy.toInt(&ok ); | ||
113 | if(ok ) event.setUid( dumInt ); | ||
114 | m_todos.append( event ); | ||
115 | element = element->nextChild(); // next element | ||
116 | } | ||
117 | //} | ||
118 | }else { | ||
119 | qWarning("could not load" ); | ||
120 | } | ||
121 | delete root; | ||
122 | return m_todos; | ||
123 | } | ||
124 | }; | ||
125 | |||
126 | } | ||
127 | |||
128 | ToDoDB::ToDoDB(const QString &fileName = QString::null, ToDoResource *res ){ | ||
9 | m_fileName = fileName; | 129 | m_fileName = fileName; |
10 | if( fileName.isEmpty() ){ | 130 | if( fileName.isEmpty() && res == 0 ){ |
11 | m_fileName = Global::applicationFileName("todolist","todolist.xml");; | 131 | m_fileName = Global::applicationFileName("todolist","todolist.xml");; |
12 | qWarning("%s", m_fileName.latin1() ); | 132 | //qWarning("%s", m_fileName.latin1() ); |
133 | }else if(res == 0 ){ // let's create a ToDoResource for xml | ||
134 | res = new FileToDoResource(); | ||
13 | } | 135 | } |
14 | 136 | m_res = res; | |
15 | load(); | 137 | load(); |
16 | } | 138 | } |
17 | ToDoDB::~ToDoDB() | 139 | ToDoDB::~ToDoDB() |
@@ -77,114 +199,11 @@ QString ToDoDB::fileName()const | |||
77 | } | 199 | } |
78 | void ToDoDB::load() | 200 | void ToDoDB::load() |
79 | { | 201 | { |
80 | qWarning("loading tododb" ); | 202 | |
81 | m_todos.clear(); | ||
82 | XMLElement *root = XMLElement::load( m_fileName ); | ||
83 | if(root != 0l ){ // start parsing | ||
84 | qWarning("ToDoDB::load tagName(): %s", root->tagName().latin1() ); | ||
85 | //if( root->tagName() == QString::fromLatin1("Tasks" ) ){// Start | ||
86 | XMLElement *element = root->firstChild(); | ||
87 | element = element->firstChild(); | ||
88 | while( element ){ | ||
89 | qWarning("ToDoDB::load element tagName() : %s", element->tagName().latin1() ); | ||
90 | QString dummy; | ||
91 | ToDoEvent event; | ||
92 | bool ok; | ||
93 | int dumInt; | ||
94 | // completed | ||
95 | dummy = element->attribute("Completed" ); | ||
96 | dumInt = dummy.toInt(&ok ); | ||
97 | if(ok ) event.setCompleted( dumInt == 0 ? false : true ); | ||
98 | // hasDate | ||
99 | dummy = element->attribute("HasDate" ); | ||
100 | dumInt = dummy.toInt(&ok ); | ||
101 | if(ok ) event.setHasDate( dumInt == 0 ? false: true ); | ||
102 | // set the date | ||
103 | bool hasDa = dumInt; | ||
104 | if ( hasDa ) { //parse the date | ||
105 | int year, day, month = 0; | ||
106 | year = day = month; | ||
107 | // year | ||
108 | dummy = element->attribute("DateYear" ); | ||
109 | dumInt = dummy.toInt(&ok ); | ||
110 | if( ok ) year = dumInt; | ||
111 | // month | ||
112 | dummy = element->attribute("DateMonth" ); | ||
113 | dumInt = dummy.toInt(&ok ); | ||
114 | if(ok ) month = dumInt; | ||
115 | dummy = element->attribute("DateDay" ); | ||
116 | dumInt = dummy.toInt(&ok ); | ||
117 | if(ok ) day = dumInt; | ||
118 | // set the date | ||
119 | QDate date( year, month, day ); | ||
120 | event.setDate( date); | ||
121 | } | ||
122 | dummy = element->attribute("Priority" ); | ||
123 | dumInt = dummy.toInt(&ok ); | ||
124 | if(!ok ) dumInt = ToDoEvent::NORMAL; | ||
125 | event.setPriority( dumInt ); | ||
126 | //description | ||
127 | dummy = element->attribute("Description" ); | ||
128 | event.setDescription( dummy ); | ||
129 | // category | ||
130 | dummy = element->attribute("Categories" ); | ||
131 | dumInt = dummy.toInt(&ok ); | ||
132 | if(ok ) { | ||
133 | QArray<int> arrat(1); | ||
134 | arrat[0] = dumInt; | ||
135 | event.setCategory( Qtopia::Record::idsToString( arrat ) ); | ||
136 | } | ||
137 | //uid | ||
138 | dummy = element->attribute("Uid" ); | ||
139 | dumInt = dummy.toInt(&ok ); | ||
140 | if(ok ) event.m_uid = dumInt; | ||
141 | m_todos.append( event ); | ||
142 | element = element->nextChild(); // next element | ||
143 | } | ||
144 | //} | ||
145 | }else { | ||
146 | qWarning("could not load" ); | ||
147 | } | ||
148 | delete root; | ||
149 | } | 203 | } |
150 | bool ToDoDB::save() | 204 | bool ToDoDB::save() |
151 | { | 205 | { |
152 | // prepare the XML | 206 | return m_res->save( m_fileName, m_todos ); |
153 | XMLElement *tasks = new XMLElement( ); | ||
154 | tasks->setTagName("Tasks" ); | ||
155 | for( QValueList<ToDoEvent>::Iterator it = m_todos.begin(); it != m_todos.end(); ++it ){ | ||
156 | XMLElement::AttributeMap map; | ||
157 | XMLElement *task = new XMLElement(); | ||
158 | map.insert( "Completed", QString::number((int)(*it).isCompleted() ) ); | ||
159 | map.insert( "HasDate", QString::number((int)(*it).hasDate() ) ); | ||
160 | map.insert( "Priority", QString::number( (*it).priority() ) ); | ||
161 | if(!(*it).category().isEmpty() ){ | ||
162 | QArray<int> arrat(1); | ||
163 | arrat = Qtopia::Record::idsFromString( (*it).category() ); | ||
164 | map.insert( "Categories", QString::number( arrat[0] ) ); | ||
165 | }else | ||
166 | map.insert( "Categories", QString::null ); | ||
167 | map.insert( "Description", (*it).description() ); | ||
168 | if( (*it).hasDate() ){ | ||
169 | map.insert("DateYear", QString::number( (*it).date().year() ) ); | ||
170 | map.insert("DateMonth", QString::number( (*it).date().month() ) ); | ||
171 | map.insert("DateDay", QString::number( (*it).date().day() ) ); | ||
172 | } | ||
173 | map.insert("Uid", QString::number( (*it).uid() ) ); | ||
174 | task->setTagName("Task" ); | ||
175 | task->setAttributes( map ); | ||
176 | tasks->appendChild(task); | ||
177 | } | ||
178 | QFile file( m_fileName); | ||
179 | if( file.open(IO_WriteOnly ) ){ | ||
180 | QTextStream stream(&file ); | ||
181 | stream << "<!DOCTYPE Tasks>" << endl; | ||
182 | tasks->save(stream ); | ||
183 | delete tasks; | ||
184 | file.close(); | ||
185 | return true; | ||
186 | } | ||
187 | return false; | ||
188 | } | 207 | } |
189 | 208 | ||
190 | 209 | ||
diff --git a/libopie/tododb.h b/libopie/tododb.h index 6478363..945f343 100644 --- a/libopie/tododb.h +++ b/libopie/tododb.h | |||
@@ -6,11 +6,12 @@ | |||
6 | 6 | ||
7 | #include <opie/todoevent.h> | 7 | #include <opie/todoevent.h> |
8 | 8 | ||
9 | class ToDoResource; | ||
9 | class ToDoDB | 10 | class ToDoDB |
10 | { | 11 | { |
11 | public: | 12 | public: |
12 | // if no argument is supplied pick the default book | 13 | // if no argument is supplied pick the default book |
13 | ToDoDB(const QString &fileName = QString::null ); | 14 | ToDoDB(const QString &fileName = QString::null, ToDoResource* resource= 0 ); |
14 | ~ToDoDB(); | 15 | ~ToDoDB(); |
15 | QValueList<ToDoEvent> effectiveToDos(const QDate &from, | 16 | QValueList<ToDoEvent> effectiveToDos(const QDate &from, |
16 | const QDate &to, | 17 | const QDate &to, |
@@ -27,11 +28,14 @@ class ToDoDB | |||
27 | void setFileName(const QString & ); | 28 | void setFileName(const QString & ); |
28 | QString fileName()const; | 29 | QString fileName()const; |
29 | bool save(); | 30 | bool save(); |
31 | ToDoResource *resource(); | ||
32 | void setResource(ToDoResource* res); | ||
30 | 33 | ||
31 | private: | 34 | private: |
32 | class ToDoDBPrivate; | 35 | class ToDoDBPrivate; |
33 | ToDoDBPrivate *d; | 36 | ToDoDBPrivate *d; |
34 | QString m_fileName; | 37 | QString m_fileName; |
38 | ToDoResource *m_res; | ||
35 | QValueList<ToDoEvent> m_todos; | 39 | QValueList<ToDoEvent> m_todos; |
36 | void load(); | 40 | void load(); |
37 | }; | 41 | }; |
diff --git a/libopie/todoevent.h b/libopie/todoevent.h index dd8c0c9..79522b2 100644 --- a/libopie/todoevent.h +++ b/libopie/todoevent.h | |||
@@ -29,6 +29,7 @@ class ToDoEvent { | |||
29 | void setDescription(const QString& ); | 29 | void setDescription(const QString& ); |
30 | bool isOverdue(); | 30 | bool isOverdue(); |
31 | 31 | ||
32 | void setUid(int id) {m_uid = id; }; | ||
32 | bool operator<(const ToDoEvent &toDoEvent )const; | 33 | bool operator<(const ToDoEvent &toDoEvent )const; |
33 | bool operator<=(const ToDoEvent &toDoEvent )const; | 34 | bool operator<=(const ToDoEvent &toDoEvent )const; |
34 | bool operator!=(const ToDoEvent &toDoEvent )const { return !(*this == toDoEvent); }; | 35 | bool operator!=(const ToDoEvent &toDoEvent )const { return !(*this == toDoEvent); }; |
diff --git a/libopie/todoresource.h b/libopie/todoresource.h new file mode 100644 index 0000000..34edb04 --- a/dev/null +++ b/libopie/todoresource.h | |||
@@ -0,0 +1,14 @@ | |||
1 | |||
2 | |||
3 | #ifndef opietodoresource_h | ||
4 | #define opietodoresource_h | ||
5 | |||
6 | class ToDoEvent; | ||
7 | class ToDoResource { | ||
8 | public: | ||
9 | ToDoResource( ) {}; | ||
10 | virtual QValueList<ToDoEvent> load(const QString &file ) = 0; | ||
11 | virtual bool save( const QString &file, const QValueList<ToDoEvent> & ) = 0; | ||
12 | }; | ||
13 | |||
14 | #endif | ||