author | zecke <zecke> | 2004-03-14 17:00:05 (UTC) |
---|---|---|
committer | zecke <zecke> | 2004-03-14 17:00:05 (UTC) |
commit | 59106c8ffcf25181925281c519a2b49d6835ef9e (patch) (unidiff) | |
tree | 810ff3af82ffb109ad662bf86e2d669b27ef9a32 /libopie2/opiepim | |
parent | 5bf53ecde573239a07faaa713f2a6f8392fa364a (diff) | |
download | opie-59106c8ffcf25181925281c519a2b49d6835ef9e.zip opie-59106c8ffcf25181925281c519a2b49d6835ef9e.tar.gz opie-59106c8ffcf25181925281c519a2b49d6835ef9e.tar.bz2 |
but XML Parser back to OPie Core as libtinykate uses it as well
-rw-r--r-- | libopie2/opiepim/backend/backends.pro | 6 | ||||
-rw-r--r-- | libopie2/opiepim/backend/ocontactaccessbackend_xml.cpp | 5 | ||||
-rw-r--r-- | libopie2/opiepim/core/backends/private/xmltree.cc | 323 | ||||
-rw-r--r-- | libopie2/opiepim/core/backends/private/xmltree.h | 122 |
4 files changed, 4 insertions, 452 deletions
diff --git a/libopie2/opiepim/backend/backends.pro b/libopie2/opiepim/backend/backends.pro index d4867ba..953d928 100644 --- a/libopie2/opiepim/backend/backends.pro +++ b/libopie2/opiepim/backend/backends.pro | |||
@@ -1,33 +1,31 @@ | |||
1 | SOURCES += core/backends/ocontactaccessbackend_sql.cpp \ | 1 | SOURCES += core/backends/ocontactaccessbackend_sql.cpp \ |
2 | core/backends/ocontactaccessbackend_vcard.cpp \ | 2 | core/backends/ocontactaccessbackend_vcard.cpp \ |
3 | core/backends/ocontactaccessbackend_xml.cpp \ | 3 | core/backends/ocontactaccessbackend_xml.cpp \ |
4 | core/backends/ocontactaccess.cpp \ | 4 | core/backends/ocontactaccess.cpp \ |
5 | core/backends/odatebookaccessbackend.cpp \ | 5 | core/backends/odatebookaccessbackend.cpp \ |
6 | core/backends/odatebookaccessbackend_xml.cpp \ | 6 | core/backends/odatebookaccessbackend_xml.cpp \ |
7 | core/backends/otodoaccessbackend.cpp \ | 7 | core/backends/otodoaccessbackend.cpp \ |
8 | core/backends/otodoaccess.cpp \ | 8 | core/backends/otodoaccess.cpp \ |
9 | core/backends/otodoaccesssql.cpp \ | 9 | core/backends/otodoaccesssql.cpp \ |
10 | core/backends/otodoaccessvcal.cpp \ | 10 | core/backends/otodoaccessvcal.cpp \ |
11 | core/backends/otodoaccessxml.cpp \ | 11 | core/backends/otodoaccessxml.cpp \ |
12 | core/backends/odatebookaccess.cpp \ | 12 | core/backends/odatebookaccess.cpp \ |
13 | core/backends/odatebookaccessbackend_sql.cpp \ | 13 | core/backends/odatebookaccessbackend_sql.cpp |
14 | core/backends/private/xmltree.cc | ||
15 | 14 | ||
16 | HEADERS += core/backends/obackendfactory.h \ | 15 | HEADERS += core/backends/obackendfactory.h \ |
17 | core/backends/ocontactaccessbackend.h \ | 16 | core/backends/ocontactaccessbackend.h \ |
18 | core/backends/ocontactaccessbackend_sql.h \ | 17 | core/backends/ocontactaccessbackend_sql.h \ |
19 | core/backends/ocontactaccessbackend_vcard.h \ | 18 | core/backends/ocontactaccessbackend_vcard.h \ |
20 | core/backends/ocontactaccessbackend_xml.h \ | 19 | core/backends/ocontactaccessbackend_xml.h \ |
21 | core/backends/ocontactaccess.h \ | 20 | core/backends/ocontactaccess.h \ |
22 | core/backends/odatebookaccessbackend.h \ | 21 | core/backends/odatebookaccessbackend.h \ |
23 | core/backends/odatebookaccessbackend_sql.h \ | 22 | core/backends/odatebookaccessbackend_sql.h \ |
24 | core/backends/odatebookaccessbackend_xml.h \ | 23 | core/backends/odatebookaccessbackend_xml.h \ |
25 | core/backends/opimaccessbackend.h \ | 24 | core/backends/opimaccessbackend.h \ |
26 | core/backends/opimaccesstemplate.h \ | 25 | core/backends/opimaccesstemplate.h \ |
27 | core/backends/otodoaccessbackend.h \ | 26 | core/backends/otodoaccessbackend.h \ |
28 | core/backends/otodoaccess.h \ | 27 | core/backends/otodoaccess.h \ |
29 | core/backends/otodoaccesssql.h \ | 28 | core/backends/otodoaccesssql.h \ |
30 | core/backends/otodoaccessvcal.h \ | 29 | core/backends/otodoaccessvcal.h \ |
31 | core/backends/otodoaccessxml.h \ | 30 | core/backends/otodoaccessxml.h \ |
32 | core/backends/odatebookaccess.h \ | 31 | core/backends/odatebookaccess.h |
33 | core/backends/private/xmltree.h | ||
diff --git a/libopie2/opiepim/backend/ocontactaccessbackend_xml.cpp b/libopie2/opiepim/backend/ocontactaccessbackend_xml.cpp index f5e76d5..2b467c3 100644 --- a/libopie2/opiepim/backend/ocontactaccessbackend_xml.cpp +++ b/libopie2/opiepim/backend/ocontactaccessbackend_xml.cpp | |||
@@ -1,180 +1,179 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of the Opie Project | 2 | This file is part of the Opie Project |
3 | Copyright (C) Stefan Eilers (Eilers.Stefan@epost.de) | 3 | Copyright (C) Stefan Eilers (Eilers.Stefan@epost.de) |
4 | =. Copyright (C) The Opie Team <opie-devel@handhelds.org> | 4 | =. Copyright (C) The Opie Team <opie-devel@handhelds.org> |
5 | .=l. | 5 | .=l. |
6 | .>+-= | 6 | .>+-= |
7 | _;:, .> :=|. This program is free software; you can | 7 | _;:, .> :=|. This program is free software; you can |
8 | .> <`_, > . <= redistribute it and/or modify it under | 8 | .> <`_, > . <= redistribute it and/or modify it under |
9 | :`=1 )Y*s>-.-- : the terms of the GNU Library General Public | 9 | :`=1 )Y*s>-.-- : the terms of the GNU Library General Public |
10 | .="- .-=="i, .._ License as published by the Free Software | 10 | .="- .-=="i, .._ License as published by the Free Software |
11 | - . .-<_> .<> Foundation; either version 2 of the License, | 11 | - . .-<_> .<> Foundation; either version 2 of the License, |
12 | ._= =} : or (at your option) any later version. | 12 | ._= =} : or (at your option) any later version. |
13 | .%`+i> _;_. | 13 | .%`+i> _;_. |
14 | .i_,=:_. -<s. This program is distributed in the hope that | 14 | .i_,=:_. -<s. This program is distributed in the hope that |
15 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; | 15 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; |
16 | : .. .:, . . . without even the implied warranty of | 16 | : .. .:, . . . without even the implied warranty of |
17 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A | 17 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A |
18 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU | 18 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU |
19 | ..}^=.= = ; Library General Public License for more | 19 | ..}^=.= = ; Library General Public License for more |
20 | ++= -. .` .: details. | 20 | ++= -. .` .: details. |
21 | : = ...= . :.=- | 21 | : = ...= . :.=- |
22 | -. .:....=;==+<; You should have received a copy of the GNU | 22 | -. .:....=;==+<; You should have received a copy of the GNU |
23 | -_. . . )=. = Library General Public License along with | 23 | -_. . . )=. = Library General Public License along with |
24 | -- :-=` this library; see the file COPYING.LIB. | 24 | -- :-=` this library; see the file COPYING.LIB. |
25 | If not, write to the Free Software Foundation, | 25 | If not, write to the Free Software Foundation, |
26 | Inc., 59 Temple Place - Suite 330, | 26 | Inc., 59 Temple Place - Suite 330, |
27 | Boston, MA 02111-1307, USA. | 27 | Boston, MA 02111-1307, USA. |
28 | */ | 28 | */ |
29 | /* | 29 | /* |
30 | * XML Backend for the OPIE-Contact Database. | 30 | * XML Backend for the OPIE-Contact Database. |
31 | */ | 31 | */ |
32 | 32 | ||
33 | #include <opie2/ocontactaccessbackend_xml.h> | 33 | #include <opie2/ocontactaccessbackend_xml.h> |
34 | 34 | ||
35 | #include <qasciidict.h> | 35 | #include <qasciidict.h> |
36 | #include <qfile.h> | 36 | #include <qfile.h> |
37 | #include <qfileinfo.h> | 37 | #include <qfileinfo.h> |
38 | #include <qregexp.h> | 38 | #include <qregexp.h> |
39 | #include <qarray.h> | 39 | #include <qarray.h> |
40 | #include <qmap.h> | 40 | #include <qmap.h> |
41 | 41 | ||
42 | #include <qpe/global.h> | 42 | #include <qpe/global.h> |
43 | 43 | ||
44 | #include "private/xmltree.h" | 44 | #include <opie2/xmltree.h> |
45 | #include <opie2/ocontactaccessbackend.h> | 45 | #include <opie2/ocontactaccessbackend.h> |
46 | #include <opie2/ocontactaccess.h> | 46 | #include <opie2/ocontactaccess.h> |
47 | 47 | ||
48 | #include <stdlib.h> | 48 | #include <stdlib.h> |
49 | #include <errno.h> | 49 | #include <errno.h> |
50 | 50 | ||
51 | using namespace Opie; | 51 | using namespace Opie::Core; |
52 | using namespace Opie::Pim::Private; | ||
53 | 52 | ||
54 | 53 | ||
55 | namespace Opie { | 54 | namespace Opie { |
56 | OPimContactAccessBackend_XML::OPimContactAccessBackend_XML ( const QString& appname, const QString& filename ): | 55 | OPimContactAccessBackend_XML::OPimContactAccessBackend_XML ( const QString& appname, const QString& filename ): |
57 | m_changed( false ) | 56 | m_changed( false ) |
58 | { | 57 | { |
59 | // Just m_contactlist should call delete if an entry | 58 | // Just m_contactlist should call delete if an entry |
60 | // is removed. | 59 | // is removed. |
61 | m_contactList.setAutoDelete( true ); | 60 | m_contactList.setAutoDelete( true ); |
62 | m_uidToContact.setAutoDelete( false ); | 61 | m_uidToContact.setAutoDelete( false ); |
63 | 62 | ||
64 | m_appName = appname; | 63 | m_appName = appname; |
65 | 64 | ||
66 | /* Set journalfile name ... */ | 65 | /* Set journalfile name ... */ |
67 | m_journalName = getenv("HOME"); | 66 | m_journalName = getenv("HOME"); |
68 | m_journalName +="/.abjournal" + appname; | 67 | m_journalName +="/.abjournal" + appname; |
69 | 68 | ||
70 | /* Expecting to access the default filename if nothing else is set */ | 69 | /* Expecting to access the default filename if nothing else is set */ |
71 | if ( filename.isEmpty() ){ | 70 | if ( filename.isEmpty() ){ |
72 | m_fileName = Global::applicationFileName( "addressbook","addressbook.xml" ); | 71 | m_fileName = Global::applicationFileName( "addressbook","addressbook.xml" ); |
73 | } else | 72 | } else |
74 | m_fileName = filename; | 73 | m_fileName = filename; |
75 | 74 | ||
76 | /* Load Database now */ | 75 | /* Load Database now */ |
77 | load (); | 76 | load (); |
78 | } | 77 | } |
79 | 78 | ||
80 | bool OPimContactAccessBackend_XML::save() | 79 | bool OPimContactAccessBackend_XML::save() |
81 | { | 80 | { |
82 | 81 | ||
83 | if ( !m_changed ) | 82 | if ( !m_changed ) |
84 | return true; | 83 | return true; |
85 | 84 | ||
86 | QString strNewFile = m_fileName + ".new"; | 85 | QString strNewFile = m_fileName + ".new"; |
87 | QFile f( strNewFile ); | 86 | QFile f( strNewFile ); |
88 | if ( !f.open( IO_WriteOnly|IO_Raw ) ) | 87 | if ( !f.open( IO_WriteOnly|IO_Raw ) ) |
89 | return false; | 88 | return false; |
90 | 89 | ||
91 | int total_written; | 90 | int total_written; |
92 | int idx_offset = 0; | 91 | int idx_offset = 0; |
93 | QString out; | 92 | QString out; |
94 | 93 | ||
95 | // Write Header | 94 | // Write Header |
96 | out = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE Addressbook ><AddressBook>\n" | 95 | out = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE Addressbook ><AddressBook>\n" |
97 | " <Groups>\n" | 96 | " <Groups>\n" |
98 | " </Groups>\n" | 97 | " </Groups>\n" |
99 | " <Contacts>\n"; | 98 | " <Contacts>\n"; |
100 | QCString cstr = out.utf8(); | 99 | QCString cstr = out.utf8(); |
101 | f.writeBlock( cstr.data(), cstr.length() ); | 100 | f.writeBlock( cstr.data(), cstr.length() ); |
102 | idx_offset += cstr.length(); | 101 | idx_offset += cstr.length(); |
103 | out = ""; | 102 | out = ""; |
104 | 103 | ||
105 | // Write all contacts | 104 | // Write all contacts |
106 | QListIterator<OPimContact> it( m_contactList ); | 105 | QListIterator<OPimContact> it( m_contactList ); |
107 | for ( ; it.current(); ++it ) { | 106 | for ( ; it.current(); ++it ) { |
108 | // qWarning(" Uid %d at Offset: %x", (*it)->uid(), idx_offset ); | 107 | // qWarning(" Uid %d at Offset: %x", (*it)->uid(), idx_offset ); |
109 | out += "<Contact "; | 108 | out += "<Contact "; |
110 | (*it)->save( out ); | 109 | (*it)->save( out ); |
111 | out += "/>\n"; | 110 | out += "/>\n"; |
112 | cstr = out.utf8(); | 111 | cstr = out.utf8(); |
113 | total_written = f.writeBlock( cstr.data(), cstr.length() ); | 112 | total_written = f.writeBlock( cstr.data(), cstr.length() ); |
114 | idx_offset += cstr.length(); | 113 | idx_offset += cstr.length(); |
115 | if ( total_written != int(cstr.length()) ) { | 114 | if ( total_written != int(cstr.length()) ) { |
116 | f.close(); | 115 | f.close(); |
117 | QFile::remove( strNewFile ); | 116 | QFile::remove( strNewFile ); |
118 | return false; | 117 | return false; |
119 | } | 118 | } |
120 | out = ""; | 119 | out = ""; |
121 | } | 120 | } |
122 | out += " </Contacts>\n</AddressBook>\n"; | 121 | out += " </Contacts>\n</AddressBook>\n"; |
123 | 122 | ||
124 | // Write Footer | 123 | // Write Footer |
125 | cstr = out.utf8(); | 124 | cstr = out.utf8(); |
126 | total_written = f.writeBlock( cstr.data(), cstr.length() ); | 125 | total_written = f.writeBlock( cstr.data(), cstr.length() ); |
127 | if ( total_written != int( cstr.length() ) ) { | 126 | if ( total_written != int( cstr.length() ) ) { |
128 | f.close(); | 127 | f.close(); |
129 | QFile::remove( strNewFile ); | 128 | QFile::remove( strNewFile ); |
130 | return false; | 129 | return false; |
131 | } | 130 | } |
132 | f.close(); | 131 | f.close(); |
133 | 132 | ||
134 | // move the file over, I'm just going to use the system call | 133 | // move the file over, I'm just going to use the system call |
135 | // because, I don't feel like using QDir. | 134 | // because, I don't feel like using QDir. |
136 | if ( ::rename( strNewFile.latin1(), m_fileName.latin1() ) < 0 ) { | 135 | if ( ::rename( strNewFile.latin1(), m_fileName.latin1() ) < 0 ) { |
137 | qWarning( "problem renaming file %s to %s, errno: %d", | 136 | qWarning( "problem renaming file %s to %s, errno: %d", |
138 | strNewFile.latin1(), m_journalName.latin1(), errno ); | 137 | strNewFile.latin1(), m_journalName.latin1(), errno ); |
139 | // remove the tmp file... | 138 | // remove the tmp file... |
140 | QFile::remove( strNewFile ); | 139 | QFile::remove( strNewFile ); |
141 | } | 140 | } |
142 | 141 | ||
143 | /* The journalfile should be removed now... */ | 142 | /* The journalfile should be removed now... */ |
144 | removeJournal(); | 143 | removeJournal(); |
145 | 144 | ||
146 | m_changed = false; | 145 | m_changed = false; |
147 | return true; | 146 | return true; |
148 | } | 147 | } |
149 | 148 | ||
150 | bool OPimContactAccessBackend_XML::load () | 149 | bool OPimContactAccessBackend_XML::load () |
151 | { | 150 | { |
152 | m_contactList.clear(); | 151 | m_contactList.clear(); |
153 | m_uidToContact.clear(); | 152 | m_uidToContact.clear(); |
154 | 153 | ||
155 | /* Load XML-File and journal if it exists */ | 154 | /* Load XML-File and journal if it exists */ |
156 | if ( !load ( m_fileName, false ) ) | 155 | if ( !load ( m_fileName, false ) ) |
157 | return false; | 156 | return false; |
158 | /* The returncode of the journalfile is ignored due to the | 157 | /* The returncode of the journalfile is ignored due to the |
159 | * fact that it does not exist when this class is instantiated ! | 158 | * fact that it does not exist when this class is instantiated ! |
160 | * But there may such a file exist, if the application crashed. | 159 | * But there may such a file exist, if the application crashed. |
161 | * Therefore we try to load it to get the changes before the # | 160 | * Therefore we try to load it to get the changes before the # |
162 | * crash happened... | 161 | * crash happened... |
163 | */ | 162 | */ |
164 | load (m_journalName, true); | 163 | load (m_journalName, true); |
165 | 164 | ||
166 | return true; | 165 | return true; |
167 | } | 166 | } |
168 | 167 | ||
169 | void OPimContactAccessBackend_XML::clear () | 168 | void OPimContactAccessBackend_XML::clear () |
170 | { | 169 | { |
171 | m_contactList.clear(); | 170 | m_contactList.clear(); |
172 | m_uidToContact.clear(); | 171 | m_uidToContact.clear(); |
173 | 172 | ||
174 | m_changed = false; | 173 | m_changed = false; |
175 | } | 174 | } |
176 | 175 | ||
177 | bool OPimContactAccessBackend_XML::wasChangedExternally() | 176 | bool OPimContactAccessBackend_XML::wasChangedExternally() |
178 | { | 177 | { |
179 | QFileInfo fi( m_fileName ); | 178 | QFileInfo fi( m_fileName ); |
180 | 179 | ||
diff --git a/libopie2/opiepim/core/backends/private/xmltree.cc b/libopie2/opiepim/core/backends/private/xmltree.cc deleted file mode 100644 index 40749ca..0000000 --- a/libopie2/opiepim/core/backends/private/xmltree.cc +++ b/dev/null | |||
@@ -1,323 +0,0 @@ | |||
1 | /* This file is part of the KDE project | ||
2 | Copyright (C) 2001 Simon Hausmann <hausmann@kde.org> | ||
3 | |||
4 | This library is free software; you can redistribute it and/or | ||
5 | modify it under the terms of the GNU Library General Public | ||
6 | License as published by the Free Software Foundation; either | ||
7 | version 2 of the License, or (at your option) any later version. | ||
8 | |||
9 | This library is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
12 | Library General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Library General Public License | ||
15 | along with this library; see the file COPYING.LIB. If not, write to | ||
16 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
17 | Boston, MA 02111-1307, USA. | ||
18 | */ | ||
19 | |||
20 | #include "xmltree.h" | ||
21 | |||
22 | #include <qpe/stringutil.h> | ||
23 | |||
24 | #include <qxml.h> | ||
25 | |||
26 | #include <assert.h> | ||
27 | |||
28 | using namespace Opie::Pim::Private; | ||
29 | |||
30 | XMLElement::XMLElement() | ||
31 | : m_parent( 0 ), m_next( 0 ), m_prev( 0 ), m_first( 0 ), m_last( 0 ) | ||
32 | { | ||
33 | } | ||
34 | |||
35 | XMLElement::~XMLElement() | ||
36 | { | ||
37 | XMLElement *n = m_first; | ||
38 | |||
39 | while ( n ) | ||
40 | { | ||
41 | XMLElement *tmp = n; | ||
42 | n = n->m_next; | ||
43 | delete tmp; | ||
44 | } | ||
45 | } | ||
46 | |||
47 | void XMLElement::appendChild( XMLElement *child ) | ||
48 | { | ||
49 | if ( child->m_parent ) | ||
50 | child->m_parent->removeChild( child ); | ||
51 | |||
52 | child->m_parent = this; | ||
53 | |||
54 | if ( m_last ) | ||
55 | m_last->m_next = child; | ||
56 | |||
57 | child->m_prev = m_last; | ||
58 | |||
59 | if ( !m_first ) | ||
60 | m_first = child; | ||
61 | |||
62 | m_last = child; | ||
63 | } | ||
64 | |||
65 | void XMLElement::insertAfter( XMLElement *newChild, XMLElement *refChild ) | ||
66 | { | ||
67 | assert( newChild != refChild ); | ||
68 | |||
69 | if ( refChild == m_last ) | ||
70 | { | ||
71 | appendChild( newChild ); | ||
72 | return; | ||
73 | } | ||
74 | |||
75 | assert( refChild ); | ||
76 | assert( refChild->m_parent ); | ||
77 | assert( refChild->m_parent == this ); | ||
78 | |||
79 | if ( newChild->m_parent && newChild != refChild ) | ||
80 | newChild->m_parent->removeChild( newChild ); | ||
81 | |||
82 | newChild->m_parent = this; | ||
83 | |||
84 | XMLElement *next = refChild->m_next; | ||
85 | |||
86 | refChild->m_next = newChild; | ||
87 | |||
88 | newChild->m_prev = refChild; | ||
89 | newChild->m_next = next; | ||
90 | |||
91 | if ( next ) | ||
92 | next->m_prev = newChild; | ||
93 | } | ||
94 | |||
95 | QString XMLElement::attribute( const QString &attr ) const | ||
96 | { | ||
97 | AttributeMap::ConstIterator it = m_attributes.find( attr ); | ||
98 | if ( it == m_attributes.end() ) | ||
99 | return QString::null; | ||
100 | return it.data(); | ||
101 | } | ||
102 | |||
103 | void XMLElement::setAttribute( const QString &attr, const QString &value ) | ||
104 | { | ||
105 | m_attributes.replace( attr, value ); | ||
106 | } | ||
107 | |||
108 | void XMLElement::insertBefore( XMLElement *newChild, XMLElement *refChild ) | ||
109 | { | ||
110 | assert( refChild ); | ||
111 | assert( refChild->m_parent ); | ||
112 | assert( refChild->m_parent == this ); | ||
113 | assert( newChild != refChild ); | ||
114 | |||
115 | if ( newChild->m_parent && newChild != refChild ) | ||
116 | newChild->m_parent->removeChild( newChild ); | ||
117 | |||
118 | newChild->m_parent = this; | ||
119 | |||
120 | XMLElement *prev = refChild->m_prev; | ||
121 | |||
122 | refChild->m_prev = newChild; | ||
123 | |||
124 | newChild->m_prev = prev; | ||
125 | newChild->m_next = refChild; | ||
126 | |||
127 | if ( prev ) | ||
128 | prev->m_next = newChild; | ||
129 | |||
130 | if ( refChild == m_first ) | ||
131 | m_first = newChild; | ||
132 | } | ||
133 | |||
134 | void XMLElement::removeChild( XMLElement *child ) | ||
135 | { | ||
136 | if ( child->m_parent != this ) | ||
137 | return; | ||
138 | |||
139 | if ( m_first == child ) | ||
140 | m_first = child->m_next; | ||
141 | |||
142 | if ( m_last == child ) | ||
143 | m_last = child->m_prev; | ||
144 | |||
145 | if ( child->m_prev ) | ||
146 | child->m_prev->m_next = child->m_next; | ||
147 | |||
148 | if ( child->m_next ) | ||
149 | child->m_next->m_prev = child->m_prev; | ||
150 | |||
151 | child->m_parent = 0; | ||
152 | child->m_prev = 0; | ||
153 | child->m_next = 0; | ||
154 | } | ||
155 | |||
156 | void XMLElement::save( QTextStream &s, uint indent ) | ||
157 | { | ||
158 | if ( !m_value.isEmpty() ) | ||
159 | { | ||
160 | s << Qtopia::escapeString( m_value ); | ||
161 | return; | ||
162 | } | ||
163 | |||
164 | for ( uint i = 0; i < indent; ++i ) | ||
165 | s << " "; | ||
166 | |||
167 | s << "<" << m_tag; | ||
168 | |||
169 | if ( !m_attributes.isEmpty() ) | ||
170 | { | ||
171 | s << " "; | ||
172 | AttributeMap::ConstIterator it = m_attributes.begin(); | ||
173 | AttributeMap::ConstIterator end = m_attributes.end(); | ||
174 | for (; it != end; ++it ) | ||
175 | { | ||
176 | s << it.key() << "=\"" << Qtopia::escapeString( it.data() ) << "\""; | ||
177 | s << " "; | ||
178 | } | ||
179 | } | ||
180 | |||
181 | if ( m_last ) | ||
182 | { | ||
183 | if ( ( m_first && !m_first->value().isEmpty() ) || !m_parent ) | ||
184 | s << ">"; | ||
185 | else | ||
186 | s << ">" << endl; | ||
187 | |||
188 | int newIndent = indent; | ||
189 | if ( m_parent ) | ||
190 | newIndent++; | ||
191 | |||
192 | XMLElement *n = m_first; | ||
193 | while ( n ) | ||
194 | { | ||
195 | n->save( s, newIndent ); | ||
196 | n = n->nextChild(); | ||
197 | } | ||
198 | |||
199 | if ( m_last && m_last->value().isEmpty() && m_parent ) | ||
200 | for ( uint i = 0; i < indent; ++i ) | ||
201 | s << " "; | ||
202 | |||
203 | if ( m_parent ) | ||
204 | s << "</" << m_tag << ">" << endl; | ||
205 | } | ||
206 | else | ||
207 | s << "/>" << endl; | ||
208 | } | ||
209 | |||
210 | class Handler : public QXmlDefaultHandler | ||
211 | { | ||
212 | public: | ||
213 | Handler() : m_node( 0 ), m_root( 0 ) {} | ||
214 | |||
215 | XMLElement *root() const { return m_root; } | ||
216 | |||
217 | virtual bool startDocument(); | ||
218 | virtual bool endDocument(); | ||
219 | virtual bool startElement( const QString &ns, const QString &ln, const QString &qName, | ||
220 | const QXmlAttributes &attr ); | ||
221 | virtual bool endElement( const QString &ns, const QString &ln, const QString &qName ); | ||
222 | virtual bool characters( const QString &ch ); | ||
223 | |||
224 | private: | ||
225 | XMLElement *m_node; | ||
226 | XMLElement *m_root; | ||
227 | }; | ||
228 | |||
229 | bool Handler::startDocument() | ||
230 | { | ||
231 | m_root = m_node = new XMLElement; | ||
232 | |||
233 | return true; | ||
234 | } | ||
235 | |||
236 | bool Handler::endDocument() | ||
237 | { | ||
238 | return m_root == m_node; | ||
239 | } | ||
240 | |||
241 | bool Handler::startElement( const QString &, const QString &, const QString &qName, | ||
242 | const QXmlAttributes &attr ) | ||
243 | { | ||
244 | XMLElement *bm = new XMLElement; | ||
245 | |||
246 | XMLElement::AttributeMap attributes; | ||
247 | for ( int i = 0; i < attr.length(); ++i ) | ||
248 | attributes[ attr.qName( i ) ] = attr.value( i ); | ||
249 | |||
250 | bm->setAttributes( attributes ); | ||
251 | |||
252 | bm->setTagName( qName ); | ||
253 | |||
254 | m_node->appendChild( bm ); | ||
255 | m_node = bm; | ||
256 | |||
257 | return true; | ||
258 | } | ||
259 | |||
260 | bool Handler::endElement( const QString &, const QString &, const QString & ) | ||
261 | { | ||
262 | if ( m_node == m_root ) | ||
263 | return false; | ||
264 | |||
265 | m_node = m_node->parent(); | ||
266 | return true; | ||
267 | } | ||
268 | |||
269 | bool Handler::characters( const QString &ch ) | ||
270 | { | ||
271 | XMLElement *textNode = new XMLElement; | ||
272 | textNode->setValue( ch ); | ||
273 | m_node->appendChild( textNode ); | ||
274 | return true; | ||
275 | } | ||
276 | |||
277 | XMLElement *XMLElement::namedItem( const QString &name ) | ||
278 | { | ||
279 | XMLElement *e = m_first; | ||
280 | |||
281 | for (; e; e = e->nextChild() ) | ||
282 | if ( e->tagName() == name ) | ||
283 | return e; | ||
284 | |||
285 | return 0; | ||
286 | } | ||
287 | |||
288 | XMLElement *XMLElement::clone() const | ||
289 | { | ||
290 | XMLElement *res = new XMLElement; | ||
291 | |||
292 | res->setTagName( m_tag ); | ||
293 | res->setValue( m_value ); | ||
294 | res->setAttributes( m_attributes ); | ||
295 | |||
296 | XMLElement *e = m_first; | ||
297 | for (; e; e = e->m_next ) | ||
298 | res->appendChild( e->clone() ); | ||
299 | |||
300 | return res; | ||
301 | } | ||
302 | |||
303 | XMLElement *XMLElement::load( const QString &fileName ) | ||
304 | { | ||
305 | QFile f( fileName ); | ||
306 | if ( !f.open( IO_ReadOnly ) ) | ||
307 | return 0; | ||
308 | |||
309 | QTextStream stream( &f ); | ||
310 | stream.setEncoding( QTextStream::UnicodeUTF8 ); | ||
311 | QXmlInputSource src( stream ); | ||
312 | QXmlSimpleReader reader; | ||
313 | Handler handler; | ||
314 | |||
315 | reader.setFeature( "http://trolltech.com/xml/features/report-whitespace-only-CharData", false ); | ||
316 | reader.setContentHandler( &handler ); | ||
317 | reader.parse( src ); | ||
318 | |||
319 | return handler.root();; | ||
320 | } | ||
321 | |||
322 | /* vim: et sw=4 | ||
323 | */ | ||
diff --git a/libopie2/opiepim/core/backends/private/xmltree.h b/libopie2/opiepim/core/backends/private/xmltree.h deleted file mode 100644 index 9817a02..0000000 --- a/libopie2/opiepim/core/backends/private/xmltree.h +++ b/dev/null | |||
@@ -1,122 +0,0 @@ | |||
1 | /* This file is part of the KDE project | ||
2 | Copyright (C) 2000,2001 Simon Hausmann <hausmann@kde.org> | ||
3 | |||
4 | This library is free software; you can redistribute it and/or | ||
5 | modify it under the terms of the GNU Library General Public | ||
6 | License as published by the Free Software Foundation; either | ||
7 | version 2 of the License, or (at your option) any later version. | ||
8 | |||
9 | This library is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
12 | Library General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Library General Public License | ||
15 | along with this library; see the file COPYING.LIB. If not, write to | ||
16 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
17 | Boston, MA 02111-1307, USA. | ||
18 | */ | ||
19 | |||
20 | |||
21 | #ifndef __bookmarks_h__ | ||
22 | #define __bookmarks_h__ | ||
23 | |||
24 | #include <qstring.h> | ||
25 | #include <qmap.h> | ||
26 | #include <qtextstream.h> | ||
27 | |||
28 | namespace Opie { | ||
29 | namespace Pim { | ||
30 | namespace Private{ | ||
31 | |||
32 | /** | ||
33 | * A small xml lib written by Simon Hausmann. | ||
34 | */ | ||
35 | class XMLElement | ||
36 | { | ||
37 | public: | ||
38 | typedef QMap<QString, QString> AttributeMap; | ||
39 | |||
40 | /** | ||
41 | * The constructor of XMLElement | ||
42 | */ | ||
43 | XMLElement(); | ||
44 | ~XMLElement(); | ||
45 | |||
46 | /** appendChild appends a child to the XMLElement behind the last element. | ||
47 | * The ownership of the child get's transfered to the | ||
48 | * this XMLElement. | ||
49 | * If child is already the child of another parent | ||
50 | * it's get removed from the other parent first. | ||
51 | */ | ||
52 | void appendChild( XMLElement *child ); | ||
53 | |||
54 | /** inserts newChild after refChild. If newChild is the child | ||
55 | * of another parent the child will get removed. | ||
56 | * The ownership of child gets transfered. | ||
57 | * | ||
58 | */ | ||
59 | void insertAfter( XMLElement *newChild, XMLElement *refChild ); | ||
60 | |||
61 | /** same as insertAfter but the element get's inserted before refChild. | ||
62 | * | ||
63 | */ | ||
64 | void insertBefore( XMLElement *newChild, XMLElement *refChild ); | ||
65 | |||
66 | /** removeChild removes the child from the XMLElement. | ||
67 | * The ownership gets dropped. You need to delete the | ||
68 | * child yourself. | ||
69 | */ | ||
70 | void removeChild( XMLElement *child ); | ||
71 | |||
72 | /** parent() returns the parent of this XMLElement | ||
73 | * If there is no parent 0l gets returned | ||
74 | */ | ||
75 | XMLElement *parent() const { return m_parent; } | ||
76 | XMLElement *firstChild() const { return m_first; } | ||
77 | XMLElement *nextChild() const { return m_next; } | ||
78 | XMLElement *prevChild() const { return m_prev; } | ||
79 | XMLElement *lastChild() const { return m_last; } | ||
80 | |||
81 | void setTagName( const QString &tag ) { m_tag = tag; } | ||
82 | QString tagName() const { return m_tag; } | ||
83 | |||
84 | void setValue( const QString &val ) { m_value = val; } | ||
85 | QString value() const { return m_value; } | ||
86 | |||
87 | void setAttributes( const AttributeMap &attrs ) { m_attributes = attrs; } | ||
88 | AttributeMap attributes() const { return m_attributes; } | ||
89 | AttributeMap &attributes() { return m_attributes; } | ||
90 | |||
91 | QString attribute( const QString & ) const; | ||
92 | void setAttribute( const QString &attr, const QString &value ); | ||
93 | void save( QTextStream &stream, uint indent = 0 ); | ||
94 | |||
95 | XMLElement *namedItem( const QString &name ); | ||
96 | |||
97 | XMLElement *clone() const; | ||
98 | |||
99 | static XMLElement *load( const QString &fileName ); | ||
100 | |||
101 | private: | ||
102 | QString m_tag; | ||
103 | QString m_value; | ||
104 | AttributeMap m_attributes; | ||
105 | |||
106 | XMLElement *m_parent; | ||
107 | XMLElement *m_next; | ||
108 | XMLElement *m_prev; | ||
109 | XMLElement *m_first; | ||
110 | XMLElement *m_last; | ||
111 | |||
112 | XMLElement( const XMLElement &rhs ); | ||
113 | XMLElement &operator=( const XMLElement &rhs ); | ||
114 | class Private; | ||
115 | Private* d; | ||
116 | }; | ||
117 | |||
118 | } | ||
119 | } | ||
120 | } // namespace Opie | ||
121 | |||
122 | #endif | ||