-rw-r--r-- | libopie/tododb.cpp | 196 |
1 files changed, 196 insertions, 0 deletions
diff --git a/libopie/tododb.cpp b/libopie/tododb.cpp new file mode 100644 index 0000000..eb17674 --- a/dev/null +++ b/libopie/tododb.cpp | |||
@@ -0,0 +1,196 @@ | |||
1 | |||
2 | #include <qdir.h> | ||
3 | #include <opie/tododb.h> | ||
4 | #include <opie/xmltree.h> | ||
5 | #include <qpe/palmtoprecord.h> | ||
6 | #include <qpe/global.h> | ||
7 | |||
8 | ToDoDB::ToDoDB(const QString &fileName = QString::null ){ | ||
9 | m_fileName = fileName; | ||
10 | if( fileName.isEmpty() ){ | ||
11 | m_fileName = Global::applicationFileName("todolist","todolist.xml");; | ||
12 | qWarning("%s", m_fileName.latin1() ); | ||
13 | } | ||
14 | |||
15 | load(); | ||
16 | } | ||
17 | ToDoDB::~ToDoDB() | ||
18 | { | ||
19 | |||
20 | } | ||
21 | QValueList<ToDoEvent> ToDoDB::effectiveToDos(const QDate &from, const QDate &to, | ||
22 | bool all ) | ||
23 | { | ||
24 | QValueList<ToDoEvent> events; | ||
25 | for( QValueList<ToDoEvent>::Iterator it = m_todos.begin(); it!= m_todos.end(); ++it ){ | ||
26 | if( (*it).hasDate() ){ | ||
27 | if( (*it).date() >= from && (*it).date() <= to ) | ||
28 | events.append( (*it) ); | ||
29 | }else if( all ){ | ||
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 | } | ||
66 | void ToDoDB::reload() | ||
67 | { | ||
68 | load(); | ||
69 | } | ||
70 | void ToDoDB::setFileName(const QString &file ) | ||
71 | { | ||
72 | m_fileName =file; | ||
73 | } | ||
74 | QString ToDoDB::fileName()const | ||
75 | { | ||
76 | return m_fileName; | ||
77 | } | ||
78 | void ToDoDB::load() | ||
79 | { | ||
80 | qWarning("loading tododb" ); | ||
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 | } | ||
150 | bool ToDoDB::save() | ||
151 | { | ||
152 | // prepare the XML | ||
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 | } | ||
189 | |||
190 | |||
191 | |||
192 | |||
193 | |||
194 | |||
195 | |||
196 | |||