Diffstat (limited to 'libopie2/opiepim/backend/odatebookaccessbackend_xml.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | libopie2/opiepim/backend/odatebookaccessbackend_xml.cpp | 41 |
1 files changed, 37 insertions, 4 deletions
diff --git a/libopie2/opiepim/backend/odatebookaccessbackend_xml.cpp b/libopie2/opiepim/backend/odatebookaccessbackend_xml.cpp index 929d004..0ebda98 100644 --- a/libopie2/opiepim/backend/odatebookaccessbackend_xml.cpp +++ b/libopie2/opiepim/backend/odatebookaccessbackend_xml.cpp | |||
@@ -1,71 +1,101 @@ | |||
1 | /* | ||
2 | This file is part of the Opie Project | ||
3 | Copyright (C) Stefan Eilers (Eilers.Stefan@epost.de) | ||
4 | =. Copyright (C) The Opie Team <opie-devel@handhelds.org> | ||
5 | .=l. | ||
6 | .>+-= | ||
7 | _;:, .> :=|. This program is free software; you can | ||
8 | .> <`_, > . <= redistribute it and/or modify it under | ||
9 | :`=1 )Y*s>-.-- : the terms of the GNU Library General Public | ||
10 | .="- .-=="i, .._ License as published by the Free Software | ||
11 | - . .-<_> .<> Foundation; either version 2 of the License, | ||
12 | ._= =} : or (at your option) any later version. | ||
13 | .%`+i> _;_. | ||
14 | .i_,=:_. -<s. This program is distributed in the hope that | ||
15 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; | ||
16 | : .. .:, . . . without even the implied warranty of | ||
17 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A | ||
18 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU | ||
19 | ..}^=.= = ; Library General Public License for more | ||
20 | ++= -. .` .: details. | ||
21 | : = ...= . :.=- | ||
22 | -. .:....=;==+<; You should have received a copy of the GNU | ||
23 | -_. . . )=. = Library General Public License along with | ||
24 | -- :-=` this library; see the file COPYING.LIB. | ||
25 | If not, write to the Free Software Foundation, | ||
26 | Inc., 59 Temple Place - Suite 330, | ||
27 | Boston, MA 02111-1307, USA. | ||
28 | */ | ||
1 | #include <errno.h> | 29 | #include <errno.h> |
2 | #include <fcntl.h> | 30 | #include <fcntl.h> |
3 | 31 | ||
4 | #include <stdio.h> | 32 | #include <stdio.h> |
5 | #include <stdlib.h> | 33 | #include <stdlib.h> |
6 | 34 | ||
7 | #include <sys/types.h> | 35 | #include <sys/types.h> |
8 | #include <sys/mman.h> | 36 | #include <sys/mman.h> |
9 | #include <sys/stat.h> | 37 | #include <sys/stat.h> |
10 | 38 | ||
11 | #include <unistd.h> | 39 | #include <unistd.h> |
12 | 40 | ||
13 | #include <qasciidict.h> | 41 | #include <qasciidict.h> |
14 | #include <qfile.h> | 42 | #include <qfile.h> |
15 | 43 | ||
16 | #include <qtopia/global.h> | 44 | #include <qtopia/global.h> |
17 | #include <qtopia/stringutil.h> | 45 | #include <qtopia/stringutil.h> |
18 | #include <qtopia/timeconversion.h> | 46 | #include <qtopia/timeconversion.h> |
19 | 47 | ||
20 | #include "opimnotifymanager.h" | 48 | #include <opie2/opimnotifymanager.h> |
21 | #include "orecur.h" | 49 | #include <opie2/orecur.h> |
22 | #include "otimezone.h" | 50 | #include <opie2/otimezone.h> |
23 | #include "odatebookaccessbackend_xml.h" | 51 | #include <opie2/odatebookaccessbackend_xml.h> |
52 | |||
53 | using namespace Opie; | ||
24 | 54 | ||
25 | namespace { | 55 | namespace { |
26 | // FROM TT again | 56 | // FROM TT again |
27 | char *strstrlen(const char *haystack, int hLen, const char* needle, int nLen) | 57 | char *strstrlen(const char *haystack, int hLen, const char* needle, int nLen) |
28 | { | 58 | { |
29 | char needleChar; | 59 | char needleChar; |
30 | char haystackChar; | 60 | char haystackChar; |
31 | if (!needle || !haystack || !hLen || !nLen) | 61 | if (!needle || !haystack || !hLen || !nLen) |
32 | return 0; | 62 | return 0; |
33 | 63 | ||
34 | const char* hsearch = haystack; | 64 | const char* hsearch = haystack; |
35 | 65 | ||
36 | if ((needleChar = *needle++) != 0) { | 66 | if ((needleChar = *needle++) != 0) { |
37 | nLen--; //(to make up for needle++) | 67 | nLen--; //(to make up for needle++) |
38 | do { | 68 | do { |
39 | do { | 69 | do { |
40 | if ((haystackChar = *hsearch++) == 0) | 70 | if ((haystackChar = *hsearch++) == 0) |
41 | return (0); | 71 | return (0); |
42 | if (hsearch >= haystack + hLen) | 72 | if (hsearch >= haystack + hLen) |
43 | return (0); | 73 | return (0); |
44 | } while (haystackChar != needleChar); | 74 | } while (haystackChar != needleChar); |
45 | } while (strncmp(hsearch, needle, QMIN(hLen - (hsearch - haystack), nLen)) != 0); | 75 | } while (strncmp(hsearch, needle, QMIN(hLen - (hsearch - haystack), nLen)) != 0); |
46 | hsearch--; | 76 | hsearch--; |
47 | } | 77 | } |
48 | return ((char *)hsearch); | 78 | return ((char *)hsearch); |
49 | } | 79 | } |
50 | } | 80 | } |
51 | 81 | ||
52 | namespace { | 82 | namespace { |
53 | time_t start, end, created, rp_end; | 83 | time_t start, end, created, rp_end; |
54 | ORecur* rec; | 84 | ORecur* rec; |
55 | ORecur* recur() { | 85 | ORecur* recur() { |
56 | if (!rec) | 86 | if (!rec) |
57 | rec = new ORecur; | 87 | rec = new ORecur; |
58 | 88 | ||
59 | return rec; | 89 | return rec; |
60 | } | 90 | } |
61 | int alarmTime; | 91 | int alarmTime; |
62 | int snd; | 92 | int snd; |
63 | enum Attribute{ | 93 | enum Attribute{ |
64 | FDescription = 0, | 94 | FDescription = 0, |
65 | FLocation, | 95 | FLocation, |
66 | FCategories, | 96 | FCategories, |
67 | FUid, | 97 | FUid, |
68 | FType, | 98 | FType, |
69 | FAlarm, | 99 | FAlarm, |
70 | FSound, | 100 | FSound, |
71 | FRType, | 101 | FRType, |
@@ -121,96 +151,97 @@ namespace { | |||
121 | buf += " end=\"" + QString::number( zone.fromUTCDateTime( zone.toDateTime( ev.endDateTime() , OTimeZone::utc() ) ) ) + "\""; | 151 | buf += " end=\"" + QString::number( zone.fromUTCDateTime( zone.toDateTime( ev.endDateTime() , OTimeZone::utc() ) ) ) + "\""; |
122 | if (!ev.note().isEmpty() ) { | 152 | if (!ev.note().isEmpty() ) { |
123 | buf += " note=\"" + Qtopia::escapeString( ev.note() ) + "\""; | 153 | buf += " note=\"" + Qtopia::escapeString( ev.note() ) + "\""; |
124 | } | 154 | } |
125 | 155 | ||
126 | buf += " timezone=\""; | 156 | buf += " timezone=\""; |
127 | if ( ev.timeZone().isEmpty() ) | 157 | if ( ev.timeZone().isEmpty() ) |
128 | buf += "None"; | 158 | buf += "None"; |
129 | else | 159 | else |
130 | buf += ev.timeZone(); | 160 | buf += ev.timeZone(); |
131 | buf += "\""; | 161 | buf += "\""; |
132 | 162 | ||
133 | if (ev.parent() != 0 ) { | 163 | if (ev.parent() != 0 ) { |
134 | buf += " recparent=\""+QString::number(ev.parent() )+"\""; | 164 | buf += " recparent=\""+QString::number(ev.parent() )+"\""; |
135 | } | 165 | } |
136 | 166 | ||
137 | if (ev.children().count() != 0 ) { | 167 | if (ev.children().count() != 0 ) { |
138 | QArray<int> children = ev.children(); | 168 | QArray<int> children = ev.children(); |
139 | buf += " recchildren=\""; | 169 | buf += " recchildren=\""; |
140 | for ( uint i = 0; i < children.count(); i++ ) { | 170 | for ( uint i = 0; i < children.count(); i++ ) { |
141 | if ( i != 0 ) buf += " "; | 171 | if ( i != 0 ) buf += " "; |
142 | buf += QString::number( children[i] ); | 172 | buf += QString::number( children[i] ); |
143 | } | 173 | } |
144 | buf+= "\""; | 174 | buf+= "\""; |
145 | } | 175 | } |
146 | 176 | ||
147 | // skip custom writing | 177 | // skip custom writing |
148 | } | 178 | } |
149 | 179 | ||
150 | inline bool forAll( const QMap<int, OEvent>& list, QFile& file ) { | 180 | inline bool forAll( const QMap<int, OEvent>& list, QFile& file ) { |
151 | QMap<int, OEvent>::ConstIterator it; | 181 | QMap<int, OEvent>::ConstIterator it; |
152 | QString buf; | 182 | QString buf; |
153 | QCString str; | 183 | QCString str; |
154 | int total_written; | 184 | int total_written; |
155 | for ( it = list.begin(); it != list.end(); ++it ) { | 185 | for ( it = list.begin(); it != list.end(); ++it ) { |
156 | buf = "<event"; | 186 | buf = "<event"; |
157 | save( it.data(), buf ); | 187 | save( it.data(), buf ); |
158 | buf += " />\n"; | 188 | buf += " />\n"; |
159 | str = buf.utf8(); | 189 | str = buf.utf8(); |
160 | 190 | ||
161 | total_written = file.writeBlock(str.data(), str.length() ); | 191 | total_written = file.writeBlock(str.data(), str.length() ); |
162 | if ( total_written != int(str.length() ) ) | 192 | if ( total_written != int(str.length() ) ) |
163 | return false; | 193 | return false; |
164 | } | 194 | } |
165 | return true; | 195 | return true; |
166 | } | 196 | } |
167 | } | 197 | } |
168 | 198 | ||
199 | namespace Opie { | ||
169 | ODateBookAccessBackend_XML::ODateBookAccessBackend_XML( const QString& , | 200 | ODateBookAccessBackend_XML::ODateBookAccessBackend_XML( const QString& , |
170 | const QString& fileName ) | 201 | const QString& fileName ) |
171 | : ODateBookAccessBackend() { | 202 | : ODateBookAccessBackend() { |
172 | m_name = fileName.isEmpty() ? Global::applicationFileName( "datebook", "datebook.xml" ) : fileName; | 203 | m_name = fileName.isEmpty() ? Global::applicationFileName( "datebook", "datebook.xml" ) : fileName; |
173 | m_changed = false; | 204 | m_changed = false; |
174 | } | 205 | } |
175 | ODateBookAccessBackend_XML::~ODateBookAccessBackend_XML() { | 206 | ODateBookAccessBackend_XML::~ODateBookAccessBackend_XML() { |
176 | } | 207 | } |
177 | bool ODateBookAccessBackend_XML::load() { | 208 | bool ODateBookAccessBackend_XML::load() { |
178 | return loadFile(); | 209 | return loadFile(); |
179 | } | 210 | } |
180 | bool ODateBookAccessBackend_XML::reload() { | 211 | bool ODateBookAccessBackend_XML::reload() { |
181 | clear(); | 212 | clear(); |
182 | return load(); | 213 | return load(); |
183 | } | 214 | } |
184 | bool ODateBookAccessBackend_XML::save() { | 215 | bool ODateBookAccessBackend_XML::save() { |
185 | if (!m_changed) return true; | 216 | if (!m_changed) return true; |
186 | 217 | ||
187 | int total_written; | 218 | int total_written; |
188 | QString strFileNew = m_name + ".new"; | 219 | QString strFileNew = m_name + ".new"; |
189 | 220 | ||
190 | QFile f( strFileNew ); | 221 | QFile f( strFileNew ); |
191 | if (!f.open( IO_WriteOnly | IO_Raw ) ) return false; | 222 | if (!f.open( IO_WriteOnly | IO_Raw ) ) return false; |
192 | 223 | ||
193 | QString buf( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ); | 224 | QString buf( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ); |
194 | buf += "<!DOCTYPE DATEBOOK><DATEBOOK>\n"; | 225 | buf += "<!DOCTYPE DATEBOOK><DATEBOOK>\n"; |
195 | buf += "<events>\n"; | 226 | buf += "<events>\n"; |
196 | QCString str = buf.utf8(); | 227 | QCString str = buf.utf8(); |
197 | total_written = f.writeBlock( str.data(), str.length() ); | 228 | total_written = f.writeBlock( str.data(), str.length() ); |
198 | if ( total_written != int(str.length() ) ) { | 229 | if ( total_written != int(str.length() ) ) { |
199 | f.close(); | 230 | f.close(); |
200 | QFile::remove( strFileNew ); | 231 | QFile::remove( strFileNew ); |
201 | return false; | 232 | return false; |
202 | } | 233 | } |
203 | 234 | ||
204 | if (!forAll( m_raw, f ) ) { | 235 | if (!forAll( m_raw, f ) ) { |
205 | f.close(); | 236 | f.close(); |
206 | QFile::remove( strFileNew ); | 237 | QFile::remove( strFileNew ); |
207 | return false; | 238 | return false; |
208 | } | 239 | } |
209 | if (!forAll( m_rep, f ) ) { | 240 | if (!forAll( m_rep, f ) ) { |
210 | f.close(); | 241 | f.close(); |
211 | QFile::remove( strFileNew ); | 242 | QFile::remove( strFileNew ); |
212 | return false; | 243 | return false; |
213 | } | 244 | } |
214 | 245 | ||
215 | buf = "</events>\n</DATEBOOK>\n"; | 246 | buf = "</events>\n</DATEBOOK>\n"; |
216 | str = buf.utf8(); | 247 | str = buf.utf8(); |
@@ -565,48 +596,50 @@ void ODateBookAccessBackend_XML::setField( OEvent& e, int id, const QString& val | |||
565 | case FCreated: | 596 | case FCreated: |
566 | created = value.toInt(); | 597 | created = value.toInt(); |
567 | break; | 598 | break; |
568 | case FRecParent: | 599 | case FRecParent: |
569 | e.setParent( value.toInt() ); | 600 | e.setParent( value.toInt() ); |
570 | break; | 601 | break; |
571 | case FRecChildren:{ | 602 | case FRecChildren:{ |
572 | QStringList list = QStringList::split(' ', value ); | 603 | QStringList list = QStringList::split(' ', value ); |
573 | for ( QStringList::Iterator it = list.begin(); it != list.end(); ++it ) { | 604 | for ( QStringList::Iterator it = list.begin(); it != list.end(); ++it ) { |
574 | e.addChild( (*it).toInt() ); | 605 | e.addChild( (*it).toInt() ); |
575 | } | 606 | } |
576 | } | 607 | } |
577 | break; | 608 | break; |
578 | case FExceptions:{ | 609 | case FExceptions:{ |
579 | QStringList list = QStringList::split(' ', value ); | 610 | QStringList list = QStringList::split(' ', value ); |
580 | for (QStringList::Iterator it = list.begin(); it != list.end(); ++it ) { | 611 | for (QStringList::Iterator it = list.begin(); it != list.end(); ++it ) { |
581 | QDate date( (*it).left(4).toInt(), (*it).mid(4, 2).toInt(), (*it).right(2).toInt() ); | 612 | QDate date( (*it).left(4).toInt(), (*it).mid(4, 2).toInt(), (*it).right(2).toInt() ); |
582 | qWarning("adding exception %s", date.toString().latin1() ); | 613 | qWarning("adding exception %s", date.toString().latin1() ); |
583 | recur()->exceptions().append( date ); | 614 | recur()->exceptions().append( date ); |
584 | } | 615 | } |
585 | } | 616 | } |
586 | break; | 617 | break; |
587 | case FTimeZone: | 618 | case FTimeZone: |
588 | if ( value != "None" ) | 619 | if ( value != "None" ) |
589 | e.setTimeZone( value ); | 620 | e.setTimeZone( value ); |
590 | break; | 621 | break; |
591 | default: | 622 | default: |
592 | break; | 623 | break; |
593 | } | 624 | } |
594 | } | 625 | } |
595 | QArray<int> ODateBookAccessBackend_XML::matchRegexp( const QRegExp &r ) const | 626 | QArray<int> ODateBookAccessBackend_XML::matchRegexp( const QRegExp &r ) const |
596 | { | 627 | { |
597 | QArray<int> m_currentQuery( m_raw.count()+ m_rep.count() ); | 628 | QArray<int> m_currentQuery( m_raw.count()+ m_rep.count() ); |
598 | uint arraycounter = 0; | 629 | uint arraycounter = 0; |
599 | QMap<int, OEvent>::ConstIterator it; | 630 | QMap<int, OEvent>::ConstIterator it; |
600 | 631 | ||
601 | for ( it = m_raw.begin(); it != m_raw.end(); ++it ) | 632 | for ( it = m_raw.begin(); it != m_raw.end(); ++it ) |
602 | if ( it.data().match( r ) ) | 633 | if ( it.data().match( r ) ) |
603 | m_currentQuery[arraycounter++] = it.data().uid(); | 634 | m_currentQuery[arraycounter++] = it.data().uid(); |
604 | for ( it = m_rep.begin(); it != m_rep.end(); ++it ) | 635 | for ( it = m_rep.begin(); it != m_rep.end(); ++it ) |
605 | if ( it.data().match( r ) ) | 636 | if ( it.data().match( r ) ) |
606 | m_currentQuery[arraycounter++] = it.data().uid(); | 637 | m_currentQuery[arraycounter++] = it.data().uid(); |
607 | 638 | ||
608 | // Shrink to fit.. | 639 | // Shrink to fit.. |
609 | m_currentQuery.resize(arraycounter); | 640 | m_currentQuery.resize(arraycounter); |
610 | 641 | ||
611 | return m_currentQuery; | 642 | return m_currentQuery; |
612 | } | 643 | } |
644 | |||
645 | } | ||