-rw-r--r-- | libopie/libopie.pro | 2 | ||||
-rw-r--r-- | libopie/tododb.cpp | 196 | ||||
-rw-r--r-- | libopie/tododb.h | 40 | ||||
-rw-r--r-- | libopie/todoevent.cpp | 150 | ||||
-rw-r--r-- | libopie/todoevent.h | 52 |
5 files changed, 439 insertions, 1 deletions
diff --git a/libopie/libopie.pro b/libopie/libopie.pro index 95ec145..fb00422 100644 --- a/libopie/libopie.pro +++ b/libopie/libopie.pro | |||
@@ -1,7 +1,7 @@ | |||
1 | TEMPLATE = lib | 1 | TEMPLATE = lib |
2 | CONFIG += qte warn_on release | 2 | CONFIG += qte warn_on release |
3 | HEADERS = $(OPIEDIR)/include/opie/xmltree.h | 3 | HEADERS = $(OPIEDIR)/include/opie/xmltree.h |
4 | SOURCES = xmltree.cc | 4 | SOURCES = xmltree.cc tododb.cpp todoevent.cpp |
5 | TARGET = opie | 5 | TARGET = opie |
6 | INCLUDEPATH += $(OPIEDIR)/include | 6 | INCLUDEPATH += $(OPIEDIR)/include |
7 | DESTDIR = $(QTDIR)/lib$(PROJMAK) | 7 | DESTDIR = $(QTDIR)/lib$(PROJMAK) |
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 | |||
diff --git a/libopie/tododb.h b/libopie/tododb.h new file mode 100644 index 0000000..6478363 --- a/dev/null +++ b/libopie/tododb.h | |||
@@ -0,0 +1,40 @@ | |||
1 | |||
2 | #ifndef tododb_h | ||
3 | #define tododb_h | ||
4 | |||
5 | #include <qvaluelist.h> | ||
6 | |||
7 | #include <opie/todoevent.h> | ||
8 | |||
9 | class ToDoDB | ||
10 | { | ||
11 | public: | ||
12 | // if no argument is supplied pick the default book | ||
13 | ToDoDB(const QString &fileName = QString::null ); | ||
14 | ~ToDoDB(); | ||
15 | QValueList<ToDoEvent> effectiveToDos(const QDate &from, | ||
16 | const QDate &to, | ||
17 | bool includeNoDates = true); | ||
18 | QValueList<ToDoEvent> effectiveToDos(const QDate &start, bool includeNoDates = true ); | ||
19 | QValueList<ToDoEvent> rawToDos(); // all events | ||
20 | QValueList<ToDoEvent> overDue(); | ||
21 | |||
22 | void addEvent(const ToDoEvent &event ); | ||
23 | void editEvent(const ToDoEvent &editEvent ); | ||
24 | void removeEvent(const ToDoEvent &event); | ||
25 | |||
26 | void reload(); | ||
27 | void setFileName(const QString & ); | ||
28 | QString fileName()const; | ||
29 | bool save(); | ||
30 | |||
31 | private: | ||
32 | class ToDoDBPrivate; | ||
33 | ToDoDBPrivate *d; | ||
34 | QString m_fileName; | ||
35 | QValueList<ToDoEvent> m_todos; | ||
36 | void load(); | ||
37 | }; | ||
38 | |||
39 | |||
40 | #endif | ||
diff --git a/libopie/todoevent.cpp b/libopie/todoevent.cpp new file mode 100644 index 0000000..4cfe1c0 --- a/dev/null +++ b/libopie/todoevent.cpp | |||
@@ -0,0 +1,150 @@ | |||
1 | |||
2 | #include <opie/todoevent.h> | ||
3 | #include <qpe/palmtopuidgen.h> | ||
4 | |||
5 | ToDoEvent::ToDoEvent(bool completed, int priority, const QString &category, | ||
6 | const QString &description, bool hasDate, QDate date, int uid ) | ||
7 | { | ||
8 | qWarning("todoEvent c'tor" ); | ||
9 | m_date = date; | ||
10 | m_isCompleted = completed; | ||
11 | m_hasDate = hasDate; | ||
12 | m_priority = priority; | ||
13 | m_category = category; | ||
14 | m_desc = description; | ||
15 | if (uid == -1 ) { | ||
16 | Qtopia::UidGen *uidgen = new Qtopia::UidGen(); | ||
17 | uid = uidgen->generate(); | ||
18 | delete uidgen; | ||
19 | }// generate the ids | ||
20 | m_uid = uid; | ||
21 | } | ||
22 | bool ToDoEvent::isCompleted() const | ||
23 | { | ||
24 | return m_isCompleted; | ||
25 | } | ||
26 | bool ToDoEvent::hasDate() const | ||
27 | { | ||
28 | return m_hasDate; | ||
29 | } | ||
30 | int ToDoEvent::priority()const | ||
31 | { | ||
32 | return m_priority; | ||
33 | } | ||
34 | QString ToDoEvent::category()const | ||
35 | { | ||
36 | return m_category; | ||
37 | } | ||
38 | QDate ToDoEvent::date()const | ||
39 | { | ||
40 | return m_date; | ||
41 | } | ||
42 | QString ToDoEvent::description()const | ||
43 | { | ||
44 | return m_desc; | ||
45 | } | ||
46 | void ToDoEvent::setCompleted( bool completed ) | ||
47 | { | ||
48 | m_isCompleted = completed; | ||
49 | } | ||
50 | void ToDoEvent::setHasDate( bool hasDate ) | ||
51 | { | ||
52 | m_hasDate = hasDate; | ||
53 | } | ||
54 | void ToDoEvent::setDescription(const QString &desc ) | ||
55 | { | ||
56 | m_desc = desc; | ||
57 | } | ||
58 | void ToDoEvent::setCategory( const QString &cat ) | ||
59 | { | ||
60 | m_category = cat; | ||
61 | } | ||
62 | void ToDoEvent::setPriority(int prio ) | ||
63 | { | ||
64 | m_priority = prio; | ||
65 | } | ||
66 | void ToDoEvent::setDate( QDate date ) | ||
67 | { | ||
68 | m_date = date; | ||
69 | } | ||
70 | bool ToDoEvent::isOverdue( ) | ||
71 | { | ||
72 | if( m_hasDate ) | ||
73 | return QDate::currentDate() > m_date; | ||
74 | return false; | ||
75 | } | ||
76 | bool ToDoEvent::operator<( const ToDoEvent &toDoEvent )const{ | ||
77 | if( !hasDate() && !toDoEvent.hasDate() ) return true; | ||
78 | if( !hasDate() && toDoEvent.hasDate() ) return true; | ||
79 | if( hasDate() && toDoEvent.hasDate() ){ | ||
80 | if( date() == toDoEvent.date() ){ // let's the priority decide | ||
81 | return priority() < toDoEvent.priority(); | ||
82 | }else{ | ||
83 | return date() < toDoEvent.date(); | ||
84 | } | ||
85 | } | ||
86 | return false; | ||
87 | } | ||
88 | bool ToDoEvent::operator<=(const ToDoEvent &toDoEvent )const | ||
89 | { | ||
90 | if( !hasDate() && !toDoEvent.hasDate() ) return true; | ||
91 | if( !hasDate() && toDoEvent.hasDate() ) return true; | ||
92 | if( hasDate() && toDoEvent.hasDate() ){ | ||
93 | if( date() == toDoEvent.date() ){ // let's the priority decide | ||
94 | return priority() <= toDoEvent.priority(); | ||
95 | }else{ | ||
96 | return date() <= toDoEvent.date(); | ||
97 | } | ||
98 | } | ||
99 | return true; | ||
100 | } | ||
101 | bool ToDoEvent::operator>(const ToDoEvent &toDoEvent )const | ||
102 | { | ||
103 | if( !hasDate() && !toDoEvent.hasDate() ) return false; | ||
104 | if( !hasDate() && toDoEvent.hasDate() ) return false; | ||
105 | if( hasDate() && toDoEvent.hasDate() ){ | ||
106 | if( date() == toDoEvent.date() ){ // let's the priority decide | ||
107 | return priority() > toDoEvent.priority(); | ||
108 | }else{ | ||
109 | return date() > toDoEvent.date(); | ||
110 | } | ||
111 | } | ||
112 | return false; | ||
113 | } | ||
114 | bool ToDoEvent::operator>=(const ToDoEvent &toDoEvent )const | ||
115 | { | ||
116 | if( !hasDate() && !toDoEvent.hasDate() ) return true; | ||
117 | if( !hasDate() && toDoEvent.hasDate() ) return false; | ||
118 | if( hasDate() && toDoEvent.hasDate() ){ | ||
119 | if( date() == toDoEvent.date() ){ // let's the priority decide | ||
120 | return priority() > toDoEvent.priority(); | ||
121 | }else{ | ||
122 | return date() > toDoEvent.date(); | ||
123 | } | ||
124 | } | ||
125 | return true; | ||
126 | } | ||
127 | bool ToDoEvent::operator==(const ToDoEvent &toDoEvent )const | ||
128 | { | ||
129 | if( m_date == toDoEvent.m_date && m_isCompleted == toDoEvent.m_isCompleted && m_hasDate == toDoEvent.m_hasDate && m_priority == toDoEvent.m_priority && m_category == toDoEvent.m_category && m_desc == toDoEvent.m_category ) | ||
130 | return true; | ||
131 | return false; | ||
132 | } | ||
133 | ToDoEvent &ToDoEvent::operator=(const ToDoEvent &item ) | ||
134 | { | ||
135 | m_date = item.m_date; | ||
136 | m_isCompleted = item.m_isCompleted; | ||
137 | m_hasDate = item.m_hasDate; | ||
138 | m_priority = item.m_priority; | ||
139 | m_category = item.m_category; | ||
140 | m_desc = item.m_desc; | ||
141 | return *this; | ||
142 | } | ||
143 | |||
144 | |||
145 | |||
146 | |||
147 | |||
148 | |||
149 | |||
150 | |||
diff --git a/libopie/todoevent.h b/libopie/todoevent.h new file mode 100644 index 0000000..dd8c0c9 --- a/dev/null +++ b/libopie/todoevent.h | |||
@@ -0,0 +1,52 @@ | |||
1 | |||
2 | #ifndef todoevent_h | ||
3 | #define todoevent_h | ||
4 | |||
5 | #include <qdatetime.h> | ||
6 | |||
7 | class ToDoEvent { | ||
8 | friend class ToDoDB; | ||
9 | public: | ||
10 | enum Priority { VERYHIGH=1, HIGH, NORMAL, LOW, VERYLOW }; | ||
11 | ToDoEvent( bool completed = false, int priority = NORMAL, | ||
12 | const QString &category = QString::null, | ||
13 | const QString &description = QString::null , | ||
14 | bool hasDate = false, QDate date = QDate::currentDate(), int uid = -1 ); | ||
15 | bool isCompleted() const; | ||
16 | bool hasDate() const; | ||
17 | int priority()const ; | ||
18 | QString category()const; | ||
19 | QDate date()const; | ||
20 | QString description()const; | ||
21 | |||
22 | int uid()const { return m_uid;}; | ||
23 | void setCompleted(bool completed ); | ||
24 | void setHasDate( bool hasDate ); | ||
25 | // if the category doesn't exist we will create it | ||
26 | void setCategory( const QString &category ); | ||
27 | void setPriority(int priority ); | ||
28 | void setDate( QDate date ); | ||
29 | void setDescription(const QString& ); | ||
30 | bool isOverdue(); | ||
31 | |||
32 | bool operator<(const ToDoEvent &toDoEvent )const; | ||
33 | bool operator<=(const ToDoEvent &toDoEvent )const; | ||
34 | bool operator!=(const ToDoEvent &toDoEvent )const { return !(*this == toDoEvent); }; | ||
35 | bool operator>(const ToDoEvent &toDoEvent )const; | ||
36 | bool operator>=(const ToDoEvent &toDoEvent)const; | ||
37 | bool operator==(const ToDoEvent &toDoEvent )const; | ||
38 | ToDoEvent &operator=(const ToDoEvent &toDoEvent ); | ||
39 | private: | ||
40 | class ToDoEventPrivate; | ||
41 | ToDoEventPrivate *d; | ||
42 | QDate m_date; | ||
43 | bool m_isCompleted:1; | ||
44 | bool m_hasDate:1; | ||
45 | int m_priority; | ||
46 | QString m_category; | ||
47 | QString m_desc; | ||
48 | int m_uid; | ||
49 | }; | ||
50 | |||
51 | |||
52 | #endif | ||