author | zecke <zecke> | 2002-03-19 19:14:32 (UTC) |
---|---|---|
committer | zecke <zecke> | 2002-03-19 19:14:32 (UTC) |
commit | c23db0c35b77cc1656ac8822ca948e73c50e8ee6 (patch) (unidiff) | |
tree | f1f61cd24074a9ce60561e13681c386e89088dc9 | |
parent | 51762a579019d09fc1af83ef4838260493b534a7 (diff) | |
download | opie-c23db0c35b77cc1656ac8822ca948e73c50e8ee6.zip opie-c23db0c35b77cc1656ac8822ca948e73c50e8ee6.tar.gz opie-c23db0c35b77cc1656ac8822ca948e73c50e8ee6.tar.bz2 |
Make multiple backends possible
-rw-r--r-- | include/opie/tododb.h | 6 | ||||
-rw-r--r-- | include/opie/todoevent.h | 1 | ||||
-rw-r--r-- | include/opie/todoresource.h | 14 | ||||
-rw-r--r-- | libopie/tododb.cpp | 221 | ||||
-rw-r--r-- | libopie/tododb.h | 6 | ||||
-rw-r--r-- | libopie/todoevent.h | 1 | ||||
-rw-r--r-- | libopie/todoresource.h | 14 |
7 files changed, 160 insertions, 103 deletions
diff --git a/include/opie/tododb.h b/include/opie/tododb.h index 6478363..945f343 100644 --- a/include/opie/tododb.h +++ b/include/opie/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/include/opie/todoevent.h b/include/opie/todoevent.h index dd8c0c9..79522b2 100644 --- a/include/opie/todoevent.h +++ b/include/opie/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/include/opie/todoresource.h b/include/opie/todoresource.h new file mode 100644 index 0000000..34edb04 --- a/dev/null +++ b/include/opie/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 | ||
diff --git a/libopie/tododb.cpp b/libopie/tododb.cpp index eb17674..91331d0 100644 --- a/libopie/tododb.cpp +++ b/libopie/tododb.cpp | |||
@@ -2,84 +2,57 @@ | |||
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 { |
9 | m_fileName = fileName; | ||
10 | if( fileName.isEmpty() ){ | ||
11 | m_fileName = Global::applicationFileName("todolist","todolist.xml");; | ||
12 | qWarning("%s", m_fileName.latin1() ); | ||
13 | } | ||
14 | 10 | ||
15 | load(); | 11 | class FileToDoResource : public ToDoResource { |
16 | } | 12 | public: |
17 | ToDoDB::~ToDoDB() | 13 | FileToDoResource() {}; |
18 | { | 14 | bool save(const QString &name, const QValueList<ToDoEvent> &m_todos ){ |
19 | 15 | // prepare the XML | |
20 | } | 16 | XMLElement *tasks = new XMLElement( ); |
21 | QValueList<ToDoEvent> ToDoDB::effectiveToDos(const QDate &from, const QDate &to, | 17 | tasks->setTagName("Tasks" ); |
22 | bool all ) | 18 | for( QValueList<ToDoEvent>::ConstIterator it = m_todos.begin(); it != m_todos.end(); ++it ){ |
23 | { | 19 | XMLElement::AttributeMap map; |
24 | QValueList<ToDoEvent> events; | 20 | XMLElement *task = new XMLElement(); |
25 | for( QValueList<ToDoEvent>::Iterator it = m_todos.begin(); it!= m_todos.end(); ++it ){ | 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() ); | ||
26 | if( (*it).hasDate() ){ | 31 | if( (*it).hasDate() ){ |
27 | if( (*it).date() >= from && (*it).date() <= to ) | 32 | map.insert("DateYear", QString::number( (*it).date().year() ) ); |
28 | events.append( (*it) ); | 33 | map.insert("DateMonth", QString::number( (*it).date().month() ) ); |
29 | }else if( all ){ | 34 | map.insert("DateDay", QString::number( (*it).date().day() ) ); |
30 | events.append( (*it) ); | ||
31 | } | ||
32 | } | ||
33 | return events; | ||
34 | } | ||
35 | QValueList<ToDoEvent> ToDoDB::effectiveToDos(const QDate &from, | ||
36 | bool all) | ||
37 | { | ||
38 | return effectiveToDos( from, QDate::currentDate(), all ); | ||
39 | } | ||
40 | QValueList<ToDoEvent> ToDoDB::overDue() | ||
41 | { | ||
42 | QValueList<ToDoEvent> events; | ||
43 | for( QValueList<ToDoEvent>::Iterator it = m_todos.begin(); it!= m_todos.end(); ++it ){ | ||
44 | if( (*it).isOverdue() ) | ||
45 | events.append((*it) ); | ||
46 | } | ||
47 | return events; | ||
48 | } | ||
49 | QValueList<ToDoEvent> ToDoDB::rawToDos() | ||
50 | { | ||
51 | return m_todos; | ||
52 | } | ||
53 | void ToDoDB::addEvent( const ToDoEvent &event ) | ||
54 | { | ||
55 | m_todos.append( event ); | ||
56 | } | ||
57 | void ToDoDB::editEvent( const ToDoEvent &event ) | ||
58 | { | ||
59 | m_todos.remove( event ); | ||
60 | m_todos.append( event ); | ||
61 | } | ||
62 | void ToDoDB::removeEvent( const ToDoEvent &event ) | ||
63 | { | ||
64 | m_todos.remove( event ); | ||
65 | } | 35 | } |
66 | void ToDoDB::reload() | 36 | map.insert("Uid", QString::number( (*it).uid() ) ); |
67 | { | 37 | task->setTagName("Task" ); |
68 | load(); | 38 | task->setAttributes( map ); |
39 | tasks->appendChild(task); | ||
69 | } | 40 | } |
70 | void ToDoDB::setFileName(const QString &file ) | 41 | QFile file( name); |
71 | { | 42 | if( file.open(IO_WriteOnly ) ){ |
72 | m_fileName =file; | 43 | QTextStream stream(&file ); |
44 | stream << "<!DOCTYPE Tasks>" << endl; | ||
45 | tasks->save(stream ); | ||
46 | delete tasks; | ||
47 | file.close(); | ||
48 | return true; | ||
73 | } | 49 | } |
74 | QString ToDoDB::fileName()const | 50 | return false; |
75 | { | ||
76 | return m_fileName; | ||
77 | } | 51 | } |
78 | void ToDoDB::load() | 52 | QValueList<ToDoEvent> load( const QString &name ){ |
79 | { | ||
80 | qWarning("loading tododb" ); | 53 | qWarning("loading tododb" ); |
81 | m_todos.clear(); | 54 | QValueList<ToDoEvent> m_todos; |
82 | XMLElement *root = XMLElement::load( m_fileName ); | 55 | XMLElement *root = XMLElement::load( name ); |
83 | if(root != 0l ){ // start parsing | 56 | if(root != 0l ){ // start parsing |
84 | qWarning("ToDoDB::load tagName(): %s", root->tagName().latin1() ); | 57 | qWarning("ToDoDB::load tagName(): %s", root->tagName().latin1() ); |
85 | //if( root->tagName() == QString::fromLatin1("Tasks" ) ){// Start | 58 | //if( root->tagName() == QString::fromLatin1("Tasks" ) ){// Start |
@@ -137,7 +110,7 @@ void ToDoDB::load() | |||
137 | //uid | 110 | //uid |
138 | dummy = element->attribute("Uid" ); | 111 | dummy = element->attribute("Uid" ); |
139 | dumInt = dummy.toInt(&ok ); | 112 | dumInt = dummy.toInt(&ok ); |
140 | if(ok ) event.m_uid = dumInt; | 113 | if(ok ) event.setUid( dumInt ); |
141 | m_todos.append( event ); | 114 | m_todos.append( event ); |
142 | element = element->nextChild(); // next element | 115 | element = element->nextChild(); // next element |
143 | } | 116 | } |
@@ -146,45 +119,91 @@ void ToDoDB::load() | |||
146 | qWarning("could not load" ); | 119 | qWarning("could not load" ); |
147 | } | 120 | } |
148 | delete root; | 121 | delete root; |
122 | return m_todos; | ||
149 | } | 123 | } |
150 | bool ToDoDB::save() | 124 | }; |
125 | |||
126 | } | ||
127 | |||
128 | ToDoDB::ToDoDB(const QString &fileName = QString::null, ToDoResource *res ){ | ||
129 | m_fileName = fileName; | ||
130 | if( fileName.isEmpty() && res == 0 ){ | ||
131 | m_fileName = Global::applicationFileName("todolist","todolist.xml");; | ||
132 | //qWarning("%s", m_fileName.latin1() ); | ||
133 | }else if(res == 0 ){ // let's create a ToDoResource for xml | ||
134 | res = new FileToDoResource(); | ||
135 | } | ||
136 | m_res = res; | ||
137 | load(); | ||
138 | } | ||
139 | ToDoDB::~ToDoDB() | ||
151 | { | 140 | { |
152 | // prepare the XML | 141 | |
153 | XMLElement *tasks = new XMLElement( ); | 142 | } |
154 | tasks->setTagName("Tasks" ); | 143 | QValueList<ToDoEvent> ToDoDB::effectiveToDos(const QDate &from, const QDate &to, |
144 | bool all ) | ||
145 | { | ||
146 | QValueList<ToDoEvent> events; | ||
155 | for( QValueList<ToDoEvent>::Iterator it = m_todos.begin(); it != m_todos.end(); ++it ){ | 147 | 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() ){ | 148 | if( (*it).hasDate() ){ |
169 | map.insert("DateYear", QString::number( (*it).date().year() ) ); | 149 | if( (*it).date() >= from && (*it).date() <= to ) |
170 | map.insert("DateMonth", QString::number( (*it).date().month() ) ); | 150 | events.append( (*it) ); |
171 | map.insert("DateDay", QString::number( (*it).date().day() ) ); | 151 | }else if( all ){ |
152 | events.append( (*it) ); | ||
172 | } | 153 | } |
173 | map.insert("Uid", QString::number( (*it).uid() ) ); | ||
174 | task->setTagName("Task" ); | ||
175 | task->setAttributes( map ); | ||
176 | tasks->appendChild(task); | ||
177 | } | 154 | } |
178 | QFile file( m_fileName); | 155 | return events; |
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 | } | 156 | } |
187 | return false; | 157 | QValueList<ToDoEvent> ToDoDB::effectiveToDos(const QDate &from, |
158 | bool all) | ||
159 | { | ||
160 | return effectiveToDos( from, QDate::currentDate(), all ); | ||
161 | } | ||
162 | QValueList<ToDoEvent> ToDoDB::overDue() | ||
163 | { | ||
164 | QValueList<ToDoEvent> events; | ||
165 | for( QValueList<ToDoEvent>::Iterator it = m_todos.begin(); it!= m_todos.end(); ++it ){ | ||
166 | if( (*it).isOverdue() ) | ||
167 | events.append((*it) ); | ||
168 | } | ||
169 | return events; | ||
170 | } | ||
171 | QValueList<ToDoEvent> ToDoDB::rawToDos() | ||
172 | { | ||
173 | return m_todos; | ||
174 | } | ||
175 | void ToDoDB::addEvent( const ToDoEvent &event ) | ||
176 | { | ||
177 | m_todos.append( event ); | ||
178 | } | ||
179 | void ToDoDB::editEvent( const ToDoEvent &event ) | ||
180 | { | ||
181 | m_todos.remove( event ); | ||
182 | m_todos.append( event ); | ||
183 | } | ||
184 | void ToDoDB::removeEvent( const ToDoEvent &event ) | ||
185 | { | ||
186 | m_todos.remove( event ); | ||
187 | } | ||
188 | void ToDoDB::reload() | ||
189 | { | ||
190 | load(); | ||
191 | } | ||
192 | void ToDoDB::setFileName(const QString &file ) | ||
193 | { | ||
194 | m_fileName =file; | ||
195 | } | ||
196 | QString ToDoDB::fileName()const | ||
197 | { | ||
198 | return m_fileName; | ||
199 | } | ||
200 | void ToDoDB::load() | ||
201 | { | ||
202 | |||
203 | } | ||
204 | bool ToDoDB::save() | ||
205 | { | ||
206 | return m_res->save( m_fileName, m_todos ); | ||
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 | ||