-rw-r--r-- | libopie/tododb.cpp | 2 | ||||
-rw-r--r-- | libopie/xmltree.cc | 1 | ||||
-rw-r--r-- | libopie/xmltree.h | 5 | ||||
-rw-r--r-- | libopie2/opiecore/xmltree.cc | 1 | ||||
-rw-r--r-- | libopie2/opiecore/xmltree.h | 5 |
5 files changed, 14 insertions, 0 deletions
diff --git a/libopie/tododb.cpp b/libopie/tododb.cpp index 33fa177..fe8b8bf 100644 --- a/libopie/tododb.cpp +++ b/libopie/tododb.cpp | |||
@@ -1,252 +1,254 @@ | |||
1 | 1 | ||
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 <opie/todoresource.h> |
6 | #include <qpe/palmtoprecord.h> | 6 | #include <qpe/palmtoprecord.h> |
7 | #include <qpe/global.h> | 7 | #include <qpe/global.h> |
8 | 8 | ||
9 | using namespace Opie; | ||
10 | |||
9 | namespace { | 11 | namespace { |
10 | 12 | ||
11 | class FileToDoResource : public ToDoResource { | 13 | class FileToDoResource : public ToDoResource { |
12 | public: | 14 | public: |
13 | FileToDoResource() {}; | 15 | FileToDoResource() {}; |
14 | bool save(const QString &name, const QValueList<ToDoEvent> &m_todos ){ | 16 | bool save(const QString &name, const QValueList<ToDoEvent> &m_todos ){ |
15 | // prepare the XML | 17 | // prepare the XML |
16 | XMLElement *tasks = new XMLElement( ); | 18 | XMLElement *tasks = new XMLElement( ); |
17 | tasks->setTagName("Tasks" ); | 19 | tasks->setTagName("Tasks" ); |
18 | for( QValueList<ToDoEvent>::ConstIterator it = m_todos.begin(); it != m_todos.end(); ++it ){ | 20 | for( QValueList<ToDoEvent>::ConstIterator it = m_todos.begin(); it != m_todos.end(); ++it ){ |
19 | XMLElement::AttributeMap map; | 21 | XMLElement::AttributeMap map; |
20 | XMLElement *task = new XMLElement(); | 22 | XMLElement *task = new XMLElement(); |
21 | map.insert( "Completed", QString::number((int)(*it).isCompleted() ) ); | 23 | map.insert( "Completed", QString::number((int)(*it).isCompleted() ) ); |
22 | map.insert( "HasDate", QString::number((int)(*it).hasDate() ) ); | 24 | map.insert( "HasDate", QString::number((int)(*it).hasDate() ) ); |
23 | map.insert( "Priority", QString::number( (*it).priority() ) ); | 25 | map.insert( "Priority", QString::number( (*it).priority() ) ); |
24 | QArray<int> arrat = (*it).categories(); | 26 | QArray<int> arrat = (*it).categories(); |
25 | QString attr; | 27 | QString attr; |
26 | for(uint i=0; i < arrat.count(); i++ ){ | 28 | for(uint i=0; i < arrat.count(); i++ ){ |
27 | attr.append(QString::number(arrat[i])+";" ); | 29 | attr.append(QString::number(arrat[i])+";" ); |
28 | } | 30 | } |
29 | if(!attr.isEmpty() ) // remove the last ; | 31 | if(!attr.isEmpty() ) // remove the last ; |
30 | attr.remove(attr.length()-1, 1 ); | 32 | attr.remove(attr.length()-1, 1 ); |
31 | map.insert( "Categories", attr ); | 33 | map.insert( "Categories", attr ); |
32 | //else | 34 | //else |
33 | //map.insert( "Categories", QString::null ); | 35 | //map.insert( "Categories", QString::null ); |
34 | map.insert( "Description", (*it).description() ); | 36 | map.insert( "Description", (*it).description() ); |
35 | if( (*it).hasDate() ){ | 37 | if( (*it).hasDate() ){ |
36 | map.insert("DateYear", QString::number( (*it).date().year() ) ); | 38 | map.insert("DateYear", QString::number( (*it).date().year() ) ); |
37 | map.insert("DateMonth", QString::number( (*it).date().month() ) ); | 39 | map.insert("DateMonth", QString::number( (*it).date().month() ) ); |
38 | map.insert("DateDay", QString::number( (*it).date().day() ) ); | 40 | map.insert("DateDay", QString::number( (*it).date().day() ) ); |
39 | } | 41 | } |
40 | map.insert("Uid", QString::number( (*it).uid() ) ); | 42 | map.insert("Uid", QString::number( (*it).uid() ) ); |
41 | task->setTagName("Task" ); | 43 | task->setTagName("Task" ); |
42 | task->setAttributes( map ); | 44 | task->setAttributes( map ); |
43 | tasks->appendChild(task); | 45 | tasks->appendChild(task); |
44 | } | 46 | } |
45 | QFile file( name); | 47 | QFile file( name); |
46 | if( file.open(IO_WriteOnly ) ){ | 48 | if( file.open(IO_WriteOnly ) ){ |
47 | QTextStream stream(&file ); | 49 | QTextStream stream(&file ); |
48 | stream.setEncoding( QTextStream::UnicodeUTF8 ); | 50 | stream.setEncoding( QTextStream::UnicodeUTF8 ); |
49 | stream << "<!DOCTYPE Tasks>" << endl; | 51 | stream << "<!DOCTYPE Tasks>" << endl; |
50 | tasks->save(stream ); | 52 | tasks->save(stream ); |
51 | delete tasks; | 53 | delete tasks; |
52 | stream << "</Tasks>" << endl; | 54 | stream << "</Tasks>" << endl; |
53 | file.close(); | 55 | file.close(); |
54 | return true; | 56 | return true; |
55 | } | 57 | } |
56 | return false; | 58 | return false; |
57 | } | 59 | } |
58 | QValueList<ToDoEvent> load( const QString &name ){ | 60 | QValueList<ToDoEvent> load( const QString &name ){ |
59 | qWarning("loading tododb" ); | 61 | qWarning("loading tododb" ); |
60 | QValueList<ToDoEvent> m_todos; | 62 | QValueList<ToDoEvent> m_todos; |
61 | XMLElement *root = XMLElement::load( name ); | 63 | XMLElement *root = XMLElement::load( name ); |
62 | if(root != 0l ){ // start parsing | 64 | if(root != 0l ){ // start parsing |
63 | qWarning("ToDoDB::load tagName(): %s", root->tagName().latin1() ); | 65 | qWarning("ToDoDB::load tagName(): %s", root->tagName().latin1() ); |
64 | //if( root->tagName() == QString::fromLatin1("Tasks" ) ){// Start | 66 | //if( root->tagName() == QString::fromLatin1("Tasks" ) ){// Start |
65 | XMLElement *element = root->firstChild(); | 67 | XMLElement *element = root->firstChild(); |
66 | element = element->firstChild(); | 68 | element = element->firstChild(); |
67 | while( element ){ | 69 | while( element ){ |
68 | if( element->tagName() != QString::fromLatin1("Task") ){ | 70 | if( element->tagName() != QString::fromLatin1("Task") ){ |
69 | element = element->nextChild(); | 71 | element = element->nextChild(); |
70 | continue; | 72 | continue; |
71 | } | 73 | } |
72 | qWarning("ToDoDB::load element tagName() : %s", element->tagName().latin1() ); | 74 | qWarning("ToDoDB::load element tagName() : %s", element->tagName().latin1() ); |
73 | QString dummy; | 75 | QString dummy; |
74 | ToDoEvent event; | 76 | ToDoEvent event; |
75 | bool ok; | 77 | bool ok; |
76 | int dumInt; | 78 | int dumInt; |
77 | // completed | 79 | // completed |
78 | dummy = element->attribute("Completed" ); | 80 | dummy = element->attribute("Completed" ); |
79 | dumInt = dummy.toInt(&ok ); | 81 | dumInt = dummy.toInt(&ok ); |
80 | if(ok ) event.setCompleted( dumInt == 0 ? false : true ); | 82 | if(ok ) event.setCompleted( dumInt == 0 ? false : true ); |
81 | // hasDate | 83 | // hasDate |
82 | dummy = element->attribute("HasDate" ); | 84 | dummy = element->attribute("HasDate" ); |
83 | dumInt = dummy.toInt(&ok ); | 85 | dumInt = dummy.toInt(&ok ); |
84 | if(ok ) event.setHasDate( dumInt == 0 ? false: true ); | 86 | if(ok ) event.setHasDate( dumInt == 0 ? false: true ); |
85 | // set the date | 87 | // set the date |
86 | bool hasDa = dumInt; | 88 | bool hasDa = dumInt; |
87 | if ( hasDa ) { //parse the date | 89 | if ( hasDa ) { //parse the date |
88 | int year, day, month = 0; | 90 | int year, day, month = 0; |
89 | year = day = month; | 91 | year = day = month; |
90 | // year | 92 | // year |
91 | dummy = element->attribute("DateYear" ); | 93 | dummy = element->attribute("DateYear" ); |
92 | dumInt = dummy.toInt(&ok ); | 94 | dumInt = dummy.toInt(&ok ); |
93 | if( ok ) year = dumInt; | 95 | if( ok ) year = dumInt; |
94 | // month | 96 | // month |
95 | dummy = element->attribute("DateMonth" ); | 97 | dummy = element->attribute("DateMonth" ); |
96 | dumInt = dummy.toInt(&ok ); | 98 | dumInt = dummy.toInt(&ok ); |
97 | if(ok ) month = dumInt; | 99 | if(ok ) month = dumInt; |
98 | dummy = element->attribute("DateDay" ); | 100 | dummy = element->attribute("DateDay" ); |
99 | dumInt = dummy.toInt(&ok ); | 101 | dumInt = dummy.toInt(&ok ); |
100 | if(ok ) day = dumInt; | 102 | if(ok ) day = dumInt; |
101 | // set the date | 103 | // set the date |
102 | QDate date( year, month, day ); | 104 | QDate date( year, month, day ); |
103 | event.setDate( date); | 105 | event.setDate( date); |
104 | } | 106 | } |
105 | dummy = element->attribute("Priority" ); | 107 | dummy = element->attribute("Priority" ); |
106 | dumInt = dummy.toInt(&ok ); | 108 | dumInt = dummy.toInt(&ok ); |
107 | if(!ok ) dumInt = ToDoEvent::NORMAL; | 109 | if(!ok ) dumInt = ToDoEvent::NORMAL; |
108 | event.setPriority( dumInt ); | 110 | event.setPriority( dumInt ); |
109 | //description | 111 | //description |
110 | dummy = element->attribute("Description" ); | 112 | dummy = element->attribute("Description" ); |
111 | event.setDescription( dummy ); | 113 | event.setDescription( dummy ); |
112 | // category | 114 | // category |
113 | dummy = element->attribute("Categories" ); | 115 | dummy = element->attribute("Categories" ); |
114 | QStringList ids = QStringList::split(";", dummy ); | 116 | QStringList ids = QStringList::split(";", dummy ); |
115 | event.setCategories( ids ); | 117 | event.setCategories( ids ); |
116 | 118 | ||
117 | //uid | 119 | //uid |
118 | dummy = element->attribute("Uid" ); | 120 | dummy = element->attribute("Uid" ); |
119 | dumInt = dummy.toInt(&ok ); | 121 | dumInt = dummy.toInt(&ok ); |
120 | if(ok ) event.setUid( dumInt ); | 122 | if(ok ) event.setUid( dumInt ); |
121 | m_todos.append( event ); | 123 | m_todos.append( event ); |
122 | element = element->nextChild(); // next element | 124 | element = element->nextChild(); // next element |
123 | } | 125 | } |
124 | //} | 126 | //} |
125 | }else { | 127 | }else { |
126 | qWarning("could not load" ); | 128 | qWarning("could not load" ); |
127 | } | 129 | } |
128 | delete root; | 130 | delete root; |
129 | qWarning("returning" ); | 131 | qWarning("returning" ); |
130 | return m_todos; | 132 | return m_todos; |
131 | } | 133 | } |
132 | }; | 134 | }; |
133 | 135 | ||
134 | } | 136 | } |
135 | 137 | ||
136 | ToDoDB::ToDoDB(const QString &fileName, ToDoResource *res ){ | 138 | ToDoDB::ToDoDB(const QString &fileName, ToDoResource *res ){ |
137 | m_fileName = fileName; | 139 | m_fileName = fileName; |
138 | if( fileName.isEmpty() && res == 0 ){ | 140 | if( fileName.isEmpty() && res == 0 ){ |
139 | m_fileName = Global::applicationFileName("todolist","todolist.xml"); | 141 | m_fileName = Global::applicationFileName("todolist","todolist.xml"); |
140 | res = new FileToDoResource(); | 142 | res = new FileToDoResource(); |
141 | //qWarning("%s", m_fileName.latin1() ); | 143 | //qWarning("%s", m_fileName.latin1() ); |
142 | }else if(res == 0 ){ // let's create a ToDoResource for xml | 144 | }else if(res == 0 ){ // let's create a ToDoResource for xml |
143 | res = new FileToDoResource(); | 145 | res = new FileToDoResource(); |
144 | } | 146 | } |
145 | m_res = res; | 147 | m_res = res; |
146 | load(); | 148 | load(); |
147 | } | 149 | } |
148 | ToDoResource* ToDoDB::resource(){ | 150 | ToDoResource* ToDoDB::resource(){ |
149 | return m_res; | 151 | return m_res; |
150 | }; | 152 | }; |
151 | void ToDoDB::setResource( ToDoResource *res ) | 153 | void ToDoDB::setResource( ToDoResource *res ) |
152 | { | 154 | { |
153 | delete m_res; | 155 | delete m_res; |
154 | m_res = res; | 156 | m_res = res; |
155 | } | 157 | } |
156 | ToDoDB::~ToDoDB() | 158 | ToDoDB::~ToDoDB() |
157 | { | 159 | { |
158 | delete m_res; | 160 | delete m_res; |
159 | } | 161 | } |
160 | QValueList<ToDoEvent> ToDoDB::effectiveToDos(const QDate &from, const QDate &to, | 162 | QValueList<ToDoEvent> ToDoDB::effectiveToDos(const QDate &from, const QDate &to, |
161 | bool all ) | 163 | bool all ) |
162 | { | 164 | { |
163 | QValueList<ToDoEvent> events; | 165 | QValueList<ToDoEvent> events; |
164 | for( QValueList<ToDoEvent>::Iterator it = m_todos.begin(); it!= m_todos.end(); ++it ){ | 166 | for( QValueList<ToDoEvent>::Iterator it = m_todos.begin(); it!= m_todos.end(); ++it ){ |
165 | if( (*it).hasDate() ){ | 167 | if( (*it).hasDate() ){ |
166 | if( (*it).date() >= from && (*it).date() <= to ) | 168 | if( (*it).date() >= from && (*it).date() <= to ) |
167 | events.append( (*it) ); | 169 | events.append( (*it) ); |
168 | }else if( all ){ | 170 | }else if( all ){ |
169 | events.append( (*it) ); | 171 | events.append( (*it) ); |
170 | } | 172 | } |
171 | } | 173 | } |
172 | return events; | 174 | return events; |
173 | } | 175 | } |
174 | QValueList<ToDoEvent> ToDoDB::effectiveToDos(const QDate &from, | 176 | QValueList<ToDoEvent> ToDoDB::effectiveToDos(const QDate &from, |
175 | bool all) | 177 | bool all) |
176 | { | 178 | { |
177 | return effectiveToDos( from, QDate::currentDate(), all ); | 179 | return effectiveToDos( from, QDate::currentDate(), all ); |
178 | } | 180 | } |
179 | QValueList<ToDoEvent> ToDoDB::overDue() | 181 | QValueList<ToDoEvent> ToDoDB::overDue() |
180 | { | 182 | { |
181 | QValueList<ToDoEvent> events; | 183 | QValueList<ToDoEvent> events; |
182 | for( QValueList<ToDoEvent>::Iterator it = m_todos.begin(); it!= m_todos.end(); ++it ){ | 184 | for( QValueList<ToDoEvent>::Iterator it = m_todos.begin(); it!= m_todos.end(); ++it ){ |
183 | if( (*it).isOverdue() ) | 185 | if( (*it).isOverdue() ) |
184 | events.append((*it) ); | 186 | events.append((*it) ); |
185 | } | 187 | } |
186 | return events; | 188 | return events; |
187 | } | 189 | } |
188 | QValueList<ToDoEvent> ToDoDB::rawToDos() | 190 | QValueList<ToDoEvent> ToDoDB::rawToDos() |
189 | { | 191 | { |
190 | return m_todos; | 192 | return m_todos; |
191 | } | 193 | } |
192 | void ToDoDB::addEvent( const ToDoEvent &event ) | 194 | void ToDoDB::addEvent( const ToDoEvent &event ) |
193 | { | 195 | { |
194 | m_todos.append( event ); | 196 | m_todos.append( event ); |
195 | } | 197 | } |
196 | void ToDoDB::editEvent( const ToDoEvent &event ) | 198 | void ToDoDB::editEvent( const ToDoEvent &event ) |
197 | { | 199 | { |
198 | m_todos.remove( event ); | 200 | m_todos.remove( event ); |
199 | m_todos.append( event ); | 201 | m_todos.append( event ); |
200 | } | 202 | } |
201 | void ToDoDB::removeEvent( const ToDoEvent &event ) | 203 | void ToDoDB::removeEvent( const ToDoEvent &event ) |
202 | { | 204 | { |
203 | m_todos.remove( event ); | 205 | m_todos.remove( event ); |
204 | } | 206 | } |
205 | void ToDoDB::replaceEvent(const ToDoEvent &event ) | 207 | void ToDoDB::replaceEvent(const ToDoEvent &event ) |
206 | { | 208 | { |
207 | QValueList<ToDoEvent>::Iterator it; | 209 | QValueList<ToDoEvent>::Iterator it; |
208 | int uid = event.uid(); | 210 | int uid = event.uid(); |
209 | // == is not overloaded as we would like :( so let's search for the uid | 211 | // == is not overloaded as we would like :( so let's search for the uid |
210 | for(it = m_todos.begin(); it != m_todos.end(); ++it ){ | 212 | for(it = m_todos.begin(); it != m_todos.end(); ++it ){ |
211 | if( (*it).uid() == uid ){ | 213 | if( (*it).uid() == uid ){ |
212 | m_todos.remove( (*it) ); | 214 | m_todos.remove( (*it) ); |
213 | break; // should save us the iterate is now borked | 215 | break; // should save us the iterate is now borked |
214 | } | 216 | } |
215 | } | 217 | } |
216 | m_todos.append(event); | 218 | m_todos.append(event); |
217 | } | 219 | } |
218 | void ToDoDB::reload() | 220 | void ToDoDB::reload() |
219 | { | 221 | { |
220 | load(); | 222 | load(); |
221 | } | 223 | } |
222 | void ToDoDB::mergeWith(const QValueList<ToDoEvent>& events ) | 224 | void ToDoDB::mergeWith(const QValueList<ToDoEvent>& events ) |
223 | { | 225 | { |
224 | QValueList<ToDoEvent>::ConstIterator it; | 226 | QValueList<ToDoEvent>::ConstIterator it; |
225 | for( it = events.begin(); it != events.end(); ++it ){ | 227 | for( it = events.begin(); it != events.end(); ++it ){ |
226 | replaceEvent( (*it) ); | 228 | replaceEvent( (*it) ); |
227 | } | 229 | } |
228 | } | 230 | } |
229 | void ToDoDB::setFileName(const QString &file ) | 231 | void ToDoDB::setFileName(const QString &file ) |
230 | { | 232 | { |
231 | m_fileName =file; | 233 | m_fileName =file; |
232 | } | 234 | } |
233 | QString ToDoDB::fileName()const | 235 | QString ToDoDB::fileName()const |
234 | { | 236 | { |
235 | return m_fileName; | 237 | return m_fileName; |
236 | } | 238 | } |
237 | void ToDoDB::load() | 239 | void ToDoDB::load() |
238 | { | 240 | { |
239 | m_todos = m_res->load( m_fileName ); | 241 | m_todos = m_res->load( m_fileName ); |
240 | } | 242 | } |
241 | bool ToDoDB::save() | 243 | bool ToDoDB::save() |
242 | { | 244 | { |
243 | return m_res->save( m_fileName, m_todos ); | 245 | return m_res->save( m_fileName, m_todos ); |
244 | } | 246 | } |
245 | 247 | ||
246 | 248 | ||
247 | 249 | ||
248 | 250 | ||
249 | 251 | ||
250 | 252 | ||
251 | 253 | ||
252 | 254 | ||
diff --git a/libopie/xmltree.cc b/libopie/xmltree.cc index 408e3c6..27db5b3 100644 --- a/libopie/xmltree.cc +++ b/libopie/xmltree.cc | |||
@@ -1,321 +1,322 @@ | |||
1 | /* This file is part of the KDE project | 1 | /* This file is part of the KDE project |
2 | Copyright (C) 2001 Simon Hausmann <hausmann@kde.org> | 2 | Copyright (C) 2001 Simon Hausmann <hausmann@kde.org> |
3 | 3 | ||
4 | This library is free software; you can redistribute it and/or | 4 | This library is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU Library General Public | 5 | modify it under the terms of the GNU Library General Public |
6 | License as published by the Free Software Foundation; either | 6 | License as published by the Free Software Foundation; either |
7 | version 2 of the License, or (at your option) any later version. | 7 | version 2 of the License, or (at your option) any later version. |
8 | 8 | ||
9 | This library is distributed in the hope that it will be useful, | 9 | This library is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | Library General Public License for more details. | 12 | Library General Public License for more details. |
13 | 13 | ||
14 | You should have received a copy of the GNU Library General Public License | 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 | 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, | 16 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
17 | Boston, MA 02111-1307, USA. | 17 | Boston, MA 02111-1307, USA. |
18 | */ | 18 | */ |
19 | 19 | ||
20 | #include <qpe/stringutil.h> | 20 | #include <qpe/stringutil.h> |
21 | #include <opie/xmltree.h> | 21 | #include <opie/xmltree.h> |
22 | 22 | ||
23 | #include <qxml.h> | 23 | #include <qxml.h> |
24 | 24 | ||
25 | #include <assert.h> | 25 | #include <assert.h> |
26 | 26 | ||
27 | using namespace Opie; | ||
27 | 28 | ||
28 | XMLElement::XMLElement() | 29 | XMLElement::XMLElement() |
29 | : m_parent( 0 ), m_next( 0 ), m_prev( 0 ), m_first( 0 ), m_last( 0 ) | 30 | : m_parent( 0 ), m_next( 0 ), m_prev( 0 ), m_first( 0 ), m_last( 0 ) |
30 | { | 31 | { |
31 | } | 32 | } |
32 | 33 | ||
33 | XMLElement::~XMLElement() | 34 | XMLElement::~XMLElement() |
34 | { | 35 | { |
35 | XMLElement *n = m_first; | 36 | XMLElement *n = m_first; |
36 | 37 | ||
37 | while ( n ) | 38 | while ( n ) |
38 | { | 39 | { |
39 | XMLElement *tmp = n; | 40 | XMLElement *tmp = n; |
40 | n = n->m_next; | 41 | n = n->m_next; |
41 | delete tmp; | 42 | delete tmp; |
42 | } | 43 | } |
43 | } | 44 | } |
44 | 45 | ||
45 | void XMLElement::appendChild( XMLElement *child ) | 46 | void XMLElement::appendChild( XMLElement *child ) |
46 | { | 47 | { |
47 | if ( child->m_parent ) | 48 | if ( child->m_parent ) |
48 | child->m_parent->removeChild( child ); | 49 | child->m_parent->removeChild( child ); |
49 | 50 | ||
50 | child->m_parent = this; | 51 | child->m_parent = this; |
51 | 52 | ||
52 | if ( m_last ) | 53 | if ( m_last ) |
53 | m_last->m_next = child; | 54 | m_last->m_next = child; |
54 | 55 | ||
55 | child->m_prev = m_last; | 56 | child->m_prev = m_last; |
56 | 57 | ||
57 | if ( !m_first ) | 58 | if ( !m_first ) |
58 | m_first = child; | 59 | m_first = child; |
59 | 60 | ||
60 | m_last = child; | 61 | m_last = child; |
61 | } | 62 | } |
62 | 63 | ||
63 | void XMLElement::insertAfter( XMLElement *newChild, XMLElement *refChild ) | 64 | void XMLElement::insertAfter( XMLElement *newChild, XMLElement *refChild ) |
64 | { | 65 | { |
65 | assert( newChild != refChild ); | 66 | assert( newChild != refChild ); |
66 | 67 | ||
67 | if ( refChild == m_last ) | 68 | if ( refChild == m_last ) |
68 | { | 69 | { |
69 | appendChild( newChild ); | 70 | appendChild( newChild ); |
70 | return; | 71 | return; |
71 | } | 72 | } |
72 | 73 | ||
73 | assert( refChild ); | 74 | assert( refChild ); |
74 | assert( refChild->m_parent ); | 75 | assert( refChild->m_parent ); |
75 | assert( refChild->m_parent == this ); | 76 | assert( refChild->m_parent == this ); |
76 | 77 | ||
77 | if ( newChild->m_parent && newChild != refChild ) | 78 | if ( newChild->m_parent && newChild != refChild ) |
78 | newChild->m_parent->removeChild( newChild ); | 79 | newChild->m_parent->removeChild( newChild ); |
79 | 80 | ||
80 | newChild->m_parent = this; | 81 | newChild->m_parent = this; |
81 | 82 | ||
82 | XMLElement *next = refChild->m_next; | 83 | XMLElement *next = refChild->m_next; |
83 | 84 | ||
84 | refChild->m_next = newChild; | 85 | refChild->m_next = newChild; |
85 | 86 | ||
86 | newChild->m_prev = refChild; | 87 | newChild->m_prev = refChild; |
87 | newChild->m_next = next; | 88 | newChild->m_next = next; |
88 | 89 | ||
89 | if ( next ) | 90 | if ( next ) |
90 | next->m_prev = newChild; | 91 | next->m_prev = newChild; |
91 | } | 92 | } |
92 | 93 | ||
93 | QString XMLElement::attribute( const QString &attr ) const | 94 | QString XMLElement::attribute( const QString &attr ) const |
94 | { | 95 | { |
95 | AttributeMap::ConstIterator it = m_attributes.find( attr ); | 96 | AttributeMap::ConstIterator it = m_attributes.find( attr ); |
96 | if ( it == m_attributes.end() ) | 97 | if ( it == m_attributes.end() ) |
97 | return QString::null; | 98 | return QString::null; |
98 | return it.data(); | 99 | return it.data(); |
99 | } | 100 | } |
100 | 101 | ||
101 | void XMLElement::setAttribute( const QString &attr, const QString &value ) | 102 | void XMLElement::setAttribute( const QString &attr, const QString &value ) |
102 | { | 103 | { |
103 | m_attributes.replace( attr, value ); | 104 | m_attributes.replace( attr, value ); |
104 | } | 105 | } |
105 | 106 | ||
106 | void XMLElement::insertBefore( XMLElement *newChild, XMLElement *refChild ) | 107 | void XMLElement::insertBefore( XMLElement *newChild, XMLElement *refChild ) |
107 | { | 108 | { |
108 | assert( refChild ); | 109 | assert( refChild ); |
109 | assert( refChild->m_parent ); | 110 | assert( refChild->m_parent ); |
110 | assert( refChild->m_parent == this ); | 111 | assert( refChild->m_parent == this ); |
111 | assert( newChild != refChild ); | 112 | assert( newChild != refChild ); |
112 | 113 | ||
113 | if ( newChild->m_parent && newChild != refChild ) | 114 | if ( newChild->m_parent && newChild != refChild ) |
114 | newChild->m_parent->removeChild( newChild ); | 115 | newChild->m_parent->removeChild( newChild ); |
115 | 116 | ||
116 | newChild->m_parent = this; | 117 | newChild->m_parent = this; |
117 | 118 | ||
118 | XMLElement *prev = refChild->m_prev; | 119 | XMLElement *prev = refChild->m_prev; |
119 | 120 | ||
120 | refChild->m_prev = newChild; | 121 | refChild->m_prev = newChild; |
121 | 122 | ||
122 | newChild->m_prev = prev; | 123 | newChild->m_prev = prev; |
123 | newChild->m_next = refChild; | 124 | newChild->m_next = refChild; |
124 | 125 | ||
125 | if ( prev ) | 126 | if ( prev ) |
126 | prev->m_next = newChild; | 127 | prev->m_next = newChild; |
127 | 128 | ||
128 | if ( refChild == m_first ) | 129 | if ( refChild == m_first ) |
129 | m_first = newChild; | 130 | m_first = newChild; |
130 | } | 131 | } |
131 | 132 | ||
132 | void XMLElement::removeChild( XMLElement *child ) | 133 | void XMLElement::removeChild( XMLElement *child ) |
133 | { | 134 | { |
134 | if ( child->m_parent != this ) | 135 | if ( child->m_parent != this ) |
135 | return; | 136 | return; |
136 | 137 | ||
137 | if ( m_first == child ) | 138 | if ( m_first == child ) |
138 | m_first = child->m_next; | 139 | m_first = child->m_next; |
139 | 140 | ||
140 | if ( m_last == child ) | 141 | if ( m_last == child ) |
141 | m_last = child->m_prev; | 142 | m_last = child->m_prev; |
142 | 143 | ||
143 | if ( child->m_prev ) | 144 | if ( child->m_prev ) |
144 | child->m_prev->m_next = child->m_next; | 145 | child->m_prev->m_next = child->m_next; |
145 | 146 | ||
146 | if ( child->m_next ) | 147 | if ( child->m_next ) |
147 | child->m_next->m_prev = child->m_prev; | 148 | child->m_next->m_prev = child->m_prev; |
148 | 149 | ||
149 | child->m_parent = 0; | 150 | child->m_parent = 0; |
150 | child->m_prev = 0; | 151 | child->m_prev = 0; |
151 | child->m_next = 0; | 152 | child->m_next = 0; |
152 | } | 153 | } |
153 | 154 | ||
154 | void XMLElement::save( QTextStream &s, uint indent ) | 155 | void XMLElement::save( QTextStream &s, uint indent ) |
155 | { | 156 | { |
156 | if ( !m_value.isEmpty() ) | 157 | if ( !m_value.isEmpty() ) |
157 | { | 158 | { |
158 | s << Qtopia::escapeString( m_value ); | 159 | s << Qtopia::escapeString( m_value ); |
159 | return; | 160 | return; |
160 | } | 161 | } |
161 | 162 | ||
162 | for ( uint i = 0; i < indent; ++i ) | 163 | for ( uint i = 0; i < indent; ++i ) |
163 | s << " "; | 164 | s << " "; |
164 | 165 | ||
165 | s << "<" << m_tag; | 166 | s << "<" << m_tag; |
166 | 167 | ||
167 | if ( !m_attributes.isEmpty() ) | 168 | if ( !m_attributes.isEmpty() ) |
168 | { | 169 | { |
169 | s << " "; | 170 | s << " "; |
170 | AttributeMap::ConstIterator it = m_attributes.begin(); | 171 | AttributeMap::ConstIterator it = m_attributes.begin(); |
171 | AttributeMap::ConstIterator end = m_attributes.end(); | 172 | AttributeMap::ConstIterator end = m_attributes.end(); |
172 | for (; it != end; ++it ) | 173 | for (; it != end; ++it ) |
173 | { | 174 | { |
174 | s << it.key() << "=\"" << Qtopia::escapeString( it.data() ) << "\""; | 175 | s << it.key() << "=\"" << Qtopia::escapeString( it.data() ) << "\""; |
175 | s << " "; | 176 | s << " "; |
176 | } | 177 | } |
177 | } | 178 | } |
178 | 179 | ||
179 | if ( m_last ) | 180 | if ( m_last ) |
180 | { | 181 | { |
181 | if ( ( m_first && !m_first->value().isEmpty() ) || !m_parent ) | 182 | if ( ( m_first && !m_first->value().isEmpty() ) || !m_parent ) |
182 | s << ">"; | 183 | s << ">"; |
183 | else | 184 | else |
184 | s << ">" << endl; | 185 | s << ">" << endl; |
185 | 186 | ||
186 | int newIndent = indent; | 187 | int newIndent = indent; |
187 | if ( m_parent ) | 188 | if ( m_parent ) |
188 | newIndent++; | 189 | newIndent++; |
189 | 190 | ||
190 | XMLElement *n = m_first; | 191 | XMLElement *n = m_first; |
191 | while ( n ) | 192 | while ( n ) |
192 | { | 193 | { |
193 | n->save( s, newIndent ); | 194 | n->save( s, newIndent ); |
194 | n = n->nextChild(); | 195 | n = n->nextChild(); |
195 | } | 196 | } |
196 | 197 | ||
197 | if ( m_last && m_last->value().isEmpty() && m_parent ) | 198 | if ( m_last && m_last->value().isEmpty() && m_parent ) |
198 | for ( uint i = 0; i < indent; ++i ) | 199 | for ( uint i = 0; i < indent; ++i ) |
199 | s << " "; | 200 | s << " "; |
200 | 201 | ||
201 | if ( m_parent ) | 202 | if ( m_parent ) |
202 | s << "</" << m_tag << ">" << endl; | 203 | s << "</" << m_tag << ">" << endl; |
203 | } | 204 | } |
204 | else | 205 | else |
205 | s << "/>" << endl; | 206 | s << "/>" << endl; |
206 | } | 207 | } |
207 | 208 | ||
208 | class Handler : public QXmlDefaultHandler | 209 | class Handler : public QXmlDefaultHandler |
209 | { | 210 | { |
210 | public: | 211 | public: |
211 | Handler() : m_node( 0 ), m_root( 0 ) {} | 212 | Handler() : m_node( 0 ), m_root( 0 ) {} |
212 | 213 | ||
213 | XMLElement *root() const { return m_root; } | 214 | XMLElement *root() const { return m_root; } |
214 | 215 | ||
215 | virtual bool startDocument(); | 216 | virtual bool startDocument(); |
216 | virtual bool endDocument(); | 217 | virtual bool endDocument(); |
217 | virtual bool startElement( const QString &ns, const QString &ln, const QString &qName, | 218 | virtual bool startElement( const QString &ns, const QString &ln, const QString &qName, |
218 | const QXmlAttributes &attr ); | 219 | const QXmlAttributes &attr ); |
219 | virtual bool endElement( const QString &ns, const QString &ln, const QString &qName ); | 220 | virtual bool endElement( const QString &ns, const QString &ln, const QString &qName ); |
220 | virtual bool characters( const QString &ch ); | 221 | virtual bool characters( const QString &ch ); |
221 | 222 | ||
222 | private: | 223 | private: |
223 | XMLElement *m_node; | 224 | XMLElement *m_node; |
224 | XMLElement *m_root; | 225 | XMLElement *m_root; |
225 | }; | 226 | }; |
226 | 227 | ||
227 | bool Handler::startDocument() | 228 | bool Handler::startDocument() |
228 | { | 229 | { |
229 | m_root = m_node = new XMLElement; | 230 | m_root = m_node = new XMLElement; |
230 | 231 | ||
231 | return true; | 232 | return true; |
232 | } | 233 | } |
233 | 234 | ||
234 | bool Handler::endDocument() | 235 | bool Handler::endDocument() |
235 | { | 236 | { |
236 | return m_root == m_node; | 237 | return m_root == m_node; |
237 | } | 238 | } |
238 | 239 | ||
239 | bool Handler::startElement( const QString &, const QString &, const QString &qName, | 240 | bool Handler::startElement( const QString &, const QString &, const QString &qName, |
240 | const QXmlAttributes &attr ) | 241 | const QXmlAttributes &attr ) |
241 | { | 242 | { |
242 | XMLElement *bm = new XMLElement; | 243 | XMLElement *bm = new XMLElement; |
243 | 244 | ||
244 | XMLElement::AttributeMap attributes; | 245 | XMLElement::AttributeMap attributes; |
245 | for ( int i = 0; i < attr.length(); ++i ) | 246 | for ( int i = 0; i < attr.length(); ++i ) |
246 | attributes[ attr.qName( i ) ] = attr.value( i ); | 247 | attributes[ attr.qName( i ) ] = attr.value( i ); |
247 | 248 | ||
248 | bm->setAttributes( attributes ); | 249 | bm->setAttributes( attributes ); |
249 | 250 | ||
250 | bm->setTagName( qName ); | 251 | bm->setTagName( qName ); |
251 | 252 | ||
252 | m_node->appendChild( bm ); | 253 | m_node->appendChild( bm ); |
253 | m_node = bm; | 254 | m_node = bm; |
254 | 255 | ||
255 | return true; | 256 | return true; |
256 | } | 257 | } |
257 | 258 | ||
258 | bool Handler::endElement( const QString &, const QString &, const QString & ) | 259 | bool Handler::endElement( const QString &, const QString &, const QString & ) |
259 | { | 260 | { |
260 | if ( m_node == m_root ) | 261 | if ( m_node == m_root ) |
261 | return false; | 262 | return false; |
262 | 263 | ||
263 | m_node = m_node->parent(); | 264 | m_node = m_node->parent(); |
264 | return true; | 265 | return true; |
265 | } | 266 | } |
266 | 267 | ||
267 | bool Handler::characters( const QString &ch ) | 268 | bool Handler::characters( const QString &ch ) |
268 | { | 269 | { |
269 | XMLElement *textNode = new XMLElement; | 270 | XMLElement *textNode = new XMLElement; |
270 | textNode->setValue( ch ); | 271 | textNode->setValue( ch ); |
271 | m_node->appendChild( textNode ); | 272 | m_node->appendChild( textNode ); |
272 | return true; | 273 | return true; |
273 | } | 274 | } |
274 | 275 | ||
275 | XMLElement *XMLElement::namedItem( const QString &name ) | 276 | XMLElement *XMLElement::namedItem( const QString &name ) |
276 | { | 277 | { |
277 | XMLElement *e = m_first; | 278 | XMLElement *e = m_first; |
278 | 279 | ||
279 | for (; e; e = e->nextChild() ) | 280 | for (; e; e = e->nextChild() ) |
280 | if ( e->tagName() == name ) | 281 | if ( e->tagName() == name ) |
281 | return e; | 282 | return e; |
282 | 283 | ||
283 | return 0; | 284 | return 0; |
284 | } | 285 | } |
285 | 286 | ||
286 | XMLElement *XMLElement::clone() const | 287 | XMLElement *XMLElement::clone() const |
287 | { | 288 | { |
288 | XMLElement *res = new XMLElement; | 289 | XMLElement *res = new XMLElement; |
289 | 290 | ||
290 | res->setTagName( m_tag ); | 291 | res->setTagName( m_tag ); |
291 | res->setValue( m_value ); | 292 | res->setValue( m_value ); |
292 | res->setAttributes( m_attributes ); | 293 | res->setAttributes( m_attributes ); |
293 | 294 | ||
294 | XMLElement *e = m_first; | 295 | XMLElement *e = m_first; |
295 | for (; e; e = e->m_next ) | 296 | for (; e; e = e->m_next ) |
296 | res->appendChild( e->clone() ); | 297 | res->appendChild( e->clone() ); |
297 | 298 | ||
298 | return res; | 299 | return res; |
299 | } | 300 | } |
300 | 301 | ||
301 | XMLElement *XMLElement::load( const QString &fileName ) | 302 | XMLElement *XMLElement::load( const QString &fileName ) |
302 | { | 303 | { |
303 | QFile f( fileName ); | 304 | QFile f( fileName ); |
304 | if ( !f.open( IO_ReadOnly ) ) | 305 | if ( !f.open( IO_ReadOnly ) ) |
305 | return 0; | 306 | return 0; |
306 | 307 | ||
307 | QTextStream stream( &f ); | 308 | QTextStream stream( &f ); |
308 | stream.setEncoding( QTextStream::UnicodeUTF8 ); | 309 | stream.setEncoding( QTextStream::UnicodeUTF8 ); |
309 | QXmlInputSource src( stream ); | 310 | QXmlInputSource src( stream ); |
310 | QXmlSimpleReader reader; | 311 | QXmlSimpleReader reader; |
311 | Handler handler; | 312 | Handler handler; |
312 | 313 | ||
313 | reader.setFeature( "http://trolltech.com/xml/features/report-whitespace-only-CharData", false ); | 314 | reader.setFeature( "http://trolltech.com/xml/features/report-whitespace-only-CharData", false ); |
314 | reader.setContentHandler( &handler ); | 315 | reader.setContentHandler( &handler ); |
315 | reader.parse( src ); | 316 | reader.parse( src ); |
316 | 317 | ||
317 | return handler.root();; | 318 | return handler.root();; |
318 | } | 319 | } |
319 | 320 | ||
320 | /* vim: et sw=4 | 321 | /* vim: et sw=4 |
321 | */ | 322 | */ |
diff --git a/libopie/xmltree.h b/libopie/xmltree.h index ed93c23..4a6b6d9 100644 --- a/libopie/xmltree.h +++ b/libopie/xmltree.h | |||
@@ -1,112 +1,117 @@ | |||
1 | /* This file is part of the KDE project | 1 | /* This file is part of the KDE project |
2 | Copyright (C) 2000,2001 Simon Hausmann <hausmann@kde.org> | 2 | Copyright (C) 2000,2001 Simon Hausmann <hausmann@kde.org> |
3 | 3 | ||
4 | This library is free software; you can redistribute it and/or | 4 | This library is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU Library General Public | 5 | modify it under the terms of the GNU Library General Public |
6 | License as published by the Free Software Foundation; either | 6 | License as published by the Free Software Foundation; either |
7 | version 2 of the License, or (at your option) any later version. | 7 | version 2 of the License, or (at your option) any later version. |
8 | 8 | ||
9 | This library is distributed in the hope that it will be useful, | 9 | This library is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | Library General Public License for more details. | 12 | Library General Public License for more details. |
13 | 13 | ||
14 | You should have received a copy of the GNU Library General Public License | 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 | 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, | 16 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
17 | Boston, MA 02111-1307, USA. | 17 | Boston, MA 02111-1307, USA. |
18 | */ | 18 | */ |
19 | 19 | ||
20 | 20 | ||
21 | #ifndef __bookmarks_h__ | 21 | #ifndef __bookmarks_h__ |
22 | #define __bookmarks_h__ | 22 | #define __bookmarks_h__ |
23 | 23 | ||
24 | #include <qstring.h> | 24 | #include <qstring.h> |
25 | #include <qmap.h> | 25 | #include <qmap.h> |
26 | #include <qtextstream.h> | 26 | #include <qtextstream.h> |
27 | 27 | ||
28 | namespace Opie | ||
29 | { | ||
30 | |||
28 | /** | 31 | /** |
29 | * A small xml lib written by Simon Hausmann. | 32 | * A small xml lib written by Simon Hausmann. |
30 | */ | 33 | */ |
31 | class XMLElement | 34 | class XMLElement |
32 | { | 35 | { |
33 | public: | 36 | public: |
34 | typedef QMap<QString, QString> AttributeMap; | 37 | typedef QMap<QString, QString> AttributeMap; |
35 | 38 | ||
36 | /** | 39 | /** |
37 | * The constructor of XMLElement | 40 | * The constructor of XMLElement |
38 | */ | 41 | */ |
39 | XMLElement(); | 42 | XMLElement(); |
40 | ~XMLElement(); | 43 | ~XMLElement(); |
41 | 44 | ||
42 | /** appendChild appends a child to the XMLElement behind the last element. | 45 | /** appendChild appends a child to the XMLElement behind the last element. |
43 | * The ownership of the child get's transfered to the | 46 | * The ownership of the child get's transfered to the |
44 | * this XMLElement. | 47 | * this XMLElement. |
45 | * If child is already the child of another parent | 48 | * If child is already the child of another parent |
46 | * it's get removed from the other parent first. | 49 | * it's get removed from the other parent first. |
47 | */ | 50 | */ |
48 | void appendChild( XMLElement *child ); | 51 | void appendChild( XMLElement *child ); |
49 | 52 | ||
50 | /** inserts newChild after refChild. If newChild is the child | 53 | /** inserts newChild after refChild. If newChild is the child |
51 | * of another parent the child will get removed. | 54 | * of another parent the child will get removed. |
52 | * The ownership of child gets transfered. | 55 | * The ownership of child gets transfered. |
53 | * | 56 | * |
54 | */ | 57 | */ |
55 | void insertAfter( XMLElement *newChild, XMLElement *refChild ); | 58 | void insertAfter( XMLElement *newChild, XMLElement *refChild ); |
56 | 59 | ||
57 | /** same as insertAfter but the element get's inserted before refChild. | 60 | /** same as insertAfter but the element get's inserted before refChild. |
58 | * | 61 | * |
59 | */ | 62 | */ |
60 | void insertBefore( XMLElement *newChild, XMLElement *refChild ); | 63 | void insertBefore( XMLElement *newChild, XMLElement *refChild ); |
61 | 64 | ||
62 | /** removeChild removes the child from the XMLElement. | 65 | /** removeChild removes the child from the XMLElement. |
63 | * The ownership gets dropped. You need to delete the | 66 | * The ownership gets dropped. You need to delete the |
64 | * child yourself. | 67 | * child yourself. |
65 | */ | 68 | */ |
66 | void removeChild( XMLElement *child ); | 69 | void removeChild( XMLElement *child ); |
67 | 70 | ||
68 | /** parent() returns the parent of this XMLElement | 71 | /** parent() returns the parent of this XMLElement |
69 | * If there is no parent 0l gets returned | 72 | * If there is no parent 0l gets returned |
70 | */ | 73 | */ |
71 | XMLElement *parent() const { return m_parent; } | 74 | XMLElement *parent() const { return m_parent; } |
72 | XMLElement *firstChild() const { return m_first; } | 75 | XMLElement *firstChild() const { return m_first; } |
73 | XMLElement *nextChild() const { return m_next; } | 76 | XMLElement *nextChild() const { return m_next; } |
74 | XMLElement *prevChild() const { return m_prev; } | 77 | XMLElement *prevChild() const { return m_prev; } |
75 | XMLElement *lastChild() const { return m_last; } | 78 | XMLElement *lastChild() const { return m_last; } |
76 | 79 | ||
77 | void setTagName( const QString &tag ) { m_tag = tag; } | 80 | void setTagName( const QString &tag ) { m_tag = tag; } |
78 | QString tagName() const { return m_tag; } | 81 | QString tagName() const { return m_tag; } |
79 | 82 | ||
80 | void setValue( const QString &val ) { m_value = val; } | 83 | void setValue( const QString &val ) { m_value = val; } |
81 | QString value() const { return m_value; } | 84 | QString value() const { return m_value; } |
82 | 85 | ||
83 | void setAttributes( const AttributeMap &attrs ) { m_attributes = attrs; } | 86 | void setAttributes( const AttributeMap &attrs ) { m_attributes = attrs; } |
84 | AttributeMap attributes() const { return m_attributes; } | 87 | AttributeMap attributes() const { return m_attributes; } |
85 | AttributeMap &attributes() { return m_attributes; } | 88 | AttributeMap &attributes() { return m_attributes; } |
86 | 89 | ||
87 | QString attribute( const QString & ) const; | 90 | QString attribute( const QString & ) const; |
88 | void setAttribute( const QString &attr, const QString &value ); | 91 | void setAttribute( const QString &attr, const QString &value ); |
89 | void save( QTextStream &stream, uint indent = 0 ); | 92 | void save( QTextStream &stream, uint indent = 0 ); |
90 | 93 | ||
91 | XMLElement *namedItem( const QString &name ); | 94 | XMLElement *namedItem( const QString &name ); |
92 | 95 | ||
93 | XMLElement *clone() const; | 96 | XMLElement *clone() const; |
94 | 97 | ||
95 | static XMLElement *load( const QString &fileName ); | 98 | static XMLElement *load( const QString &fileName ); |
96 | 99 | ||
97 | private: | 100 | private: |
98 | QString m_tag; | 101 | QString m_tag; |
99 | QString m_value; | 102 | QString m_value; |
100 | AttributeMap m_attributes; | 103 | AttributeMap m_attributes; |
101 | 104 | ||
102 | XMLElement *m_parent; | 105 | XMLElement *m_parent; |
103 | XMLElement *m_next; | 106 | XMLElement *m_next; |
104 | XMLElement *m_prev; | 107 | XMLElement *m_prev; |
105 | XMLElement *m_first; | 108 | XMLElement *m_first; |
106 | XMLElement *m_last; | 109 | XMLElement *m_last; |
107 | 110 | ||
108 | XMLElement( const XMLElement &rhs ); | 111 | XMLElement( const XMLElement &rhs ); |
109 | XMLElement &operator=( const XMLElement &rhs ); | 112 | XMLElement &operator=( const XMLElement &rhs ); |
110 | }; | 113 | }; |
111 | 114 | ||
115 | } // namespace Opie | ||
116 | |||
112 | #endif | 117 | #endif |
diff --git a/libopie2/opiecore/xmltree.cc b/libopie2/opiecore/xmltree.cc index 408e3c6..27db5b3 100644 --- a/libopie2/opiecore/xmltree.cc +++ b/libopie2/opiecore/xmltree.cc | |||
@@ -1,321 +1,322 @@ | |||
1 | /* This file is part of the KDE project | 1 | /* This file is part of the KDE project |
2 | Copyright (C) 2001 Simon Hausmann <hausmann@kde.org> | 2 | Copyright (C) 2001 Simon Hausmann <hausmann@kde.org> |
3 | 3 | ||
4 | This library is free software; you can redistribute it and/or | 4 | This library is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU Library General Public | 5 | modify it under the terms of the GNU Library General Public |
6 | License as published by the Free Software Foundation; either | 6 | License as published by the Free Software Foundation; either |
7 | version 2 of the License, or (at your option) any later version. | 7 | version 2 of the License, or (at your option) any later version. |
8 | 8 | ||
9 | This library is distributed in the hope that it will be useful, | 9 | This library is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | Library General Public License for more details. | 12 | Library General Public License for more details. |
13 | 13 | ||
14 | You should have received a copy of the GNU Library General Public License | 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 | 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, | 16 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
17 | Boston, MA 02111-1307, USA. | 17 | Boston, MA 02111-1307, USA. |
18 | */ | 18 | */ |
19 | 19 | ||
20 | #include <qpe/stringutil.h> | 20 | #include <qpe/stringutil.h> |
21 | #include <opie/xmltree.h> | 21 | #include <opie/xmltree.h> |
22 | 22 | ||
23 | #include <qxml.h> | 23 | #include <qxml.h> |
24 | 24 | ||
25 | #include <assert.h> | 25 | #include <assert.h> |
26 | 26 | ||
27 | using namespace Opie; | ||
27 | 28 | ||
28 | XMLElement::XMLElement() | 29 | XMLElement::XMLElement() |
29 | : m_parent( 0 ), m_next( 0 ), m_prev( 0 ), m_first( 0 ), m_last( 0 ) | 30 | : m_parent( 0 ), m_next( 0 ), m_prev( 0 ), m_first( 0 ), m_last( 0 ) |
30 | { | 31 | { |
31 | } | 32 | } |
32 | 33 | ||
33 | XMLElement::~XMLElement() | 34 | XMLElement::~XMLElement() |
34 | { | 35 | { |
35 | XMLElement *n = m_first; | 36 | XMLElement *n = m_first; |
36 | 37 | ||
37 | while ( n ) | 38 | while ( n ) |
38 | { | 39 | { |
39 | XMLElement *tmp = n; | 40 | XMLElement *tmp = n; |
40 | n = n->m_next; | 41 | n = n->m_next; |
41 | delete tmp; | 42 | delete tmp; |
42 | } | 43 | } |
43 | } | 44 | } |
44 | 45 | ||
45 | void XMLElement::appendChild( XMLElement *child ) | 46 | void XMLElement::appendChild( XMLElement *child ) |
46 | { | 47 | { |
47 | if ( child->m_parent ) | 48 | if ( child->m_parent ) |
48 | child->m_parent->removeChild( child ); | 49 | child->m_parent->removeChild( child ); |
49 | 50 | ||
50 | child->m_parent = this; | 51 | child->m_parent = this; |
51 | 52 | ||
52 | if ( m_last ) | 53 | if ( m_last ) |
53 | m_last->m_next = child; | 54 | m_last->m_next = child; |
54 | 55 | ||
55 | child->m_prev = m_last; | 56 | child->m_prev = m_last; |
56 | 57 | ||
57 | if ( !m_first ) | 58 | if ( !m_first ) |
58 | m_first = child; | 59 | m_first = child; |
59 | 60 | ||
60 | m_last = child; | 61 | m_last = child; |
61 | } | 62 | } |
62 | 63 | ||
63 | void XMLElement::insertAfter( XMLElement *newChild, XMLElement *refChild ) | 64 | void XMLElement::insertAfter( XMLElement *newChild, XMLElement *refChild ) |
64 | { | 65 | { |
65 | assert( newChild != refChild ); | 66 | assert( newChild != refChild ); |
66 | 67 | ||
67 | if ( refChild == m_last ) | 68 | if ( refChild == m_last ) |
68 | { | 69 | { |
69 | appendChild( newChild ); | 70 | appendChild( newChild ); |
70 | return; | 71 | return; |
71 | } | 72 | } |
72 | 73 | ||
73 | assert( refChild ); | 74 | assert( refChild ); |
74 | assert( refChild->m_parent ); | 75 | assert( refChild->m_parent ); |
75 | assert( refChild->m_parent == this ); | 76 | assert( refChild->m_parent == this ); |
76 | 77 | ||
77 | if ( newChild->m_parent && newChild != refChild ) | 78 | if ( newChild->m_parent && newChild != refChild ) |
78 | newChild->m_parent->removeChild( newChild ); | 79 | newChild->m_parent->removeChild( newChild ); |
79 | 80 | ||
80 | newChild->m_parent = this; | 81 | newChild->m_parent = this; |
81 | 82 | ||
82 | XMLElement *next = refChild->m_next; | 83 | XMLElement *next = refChild->m_next; |
83 | 84 | ||
84 | refChild->m_next = newChild; | 85 | refChild->m_next = newChild; |
85 | 86 | ||
86 | newChild->m_prev = refChild; | 87 | newChild->m_prev = refChild; |
87 | newChild->m_next = next; | 88 | newChild->m_next = next; |
88 | 89 | ||
89 | if ( next ) | 90 | if ( next ) |
90 | next->m_prev = newChild; | 91 | next->m_prev = newChild; |
91 | } | 92 | } |
92 | 93 | ||
93 | QString XMLElement::attribute( const QString &attr ) const | 94 | QString XMLElement::attribute( const QString &attr ) const |
94 | { | 95 | { |
95 | AttributeMap::ConstIterator it = m_attributes.find( attr ); | 96 | AttributeMap::ConstIterator it = m_attributes.find( attr ); |
96 | if ( it == m_attributes.end() ) | 97 | if ( it == m_attributes.end() ) |
97 | return QString::null; | 98 | return QString::null; |
98 | return it.data(); | 99 | return it.data(); |
99 | } | 100 | } |
100 | 101 | ||
101 | void XMLElement::setAttribute( const QString &attr, const QString &value ) | 102 | void XMLElement::setAttribute( const QString &attr, const QString &value ) |
102 | { | 103 | { |
103 | m_attributes.replace( attr, value ); | 104 | m_attributes.replace( attr, value ); |
104 | } | 105 | } |
105 | 106 | ||
106 | void XMLElement::insertBefore( XMLElement *newChild, XMLElement *refChild ) | 107 | void XMLElement::insertBefore( XMLElement *newChild, XMLElement *refChild ) |
107 | { | 108 | { |
108 | assert( refChild ); | 109 | assert( refChild ); |
109 | assert( refChild->m_parent ); | 110 | assert( refChild->m_parent ); |
110 | assert( refChild->m_parent == this ); | 111 | assert( refChild->m_parent == this ); |
111 | assert( newChild != refChild ); | 112 | assert( newChild != refChild ); |
112 | 113 | ||
113 | if ( newChild->m_parent && newChild != refChild ) | 114 | if ( newChild->m_parent && newChild != refChild ) |
114 | newChild->m_parent->removeChild( newChild ); | 115 | newChild->m_parent->removeChild( newChild ); |
115 | 116 | ||
116 | newChild->m_parent = this; | 117 | newChild->m_parent = this; |
117 | 118 | ||
118 | XMLElement *prev = refChild->m_prev; | 119 | XMLElement *prev = refChild->m_prev; |
119 | 120 | ||
120 | refChild->m_prev = newChild; | 121 | refChild->m_prev = newChild; |
121 | 122 | ||
122 | newChild->m_prev = prev; | 123 | newChild->m_prev = prev; |
123 | newChild->m_next = refChild; | 124 | newChild->m_next = refChild; |
124 | 125 | ||
125 | if ( prev ) | 126 | if ( prev ) |
126 | prev->m_next = newChild; | 127 | prev->m_next = newChild; |
127 | 128 | ||
128 | if ( refChild == m_first ) | 129 | if ( refChild == m_first ) |
129 | m_first = newChild; | 130 | m_first = newChild; |
130 | } | 131 | } |
131 | 132 | ||
132 | void XMLElement::removeChild( XMLElement *child ) | 133 | void XMLElement::removeChild( XMLElement *child ) |
133 | { | 134 | { |
134 | if ( child->m_parent != this ) | 135 | if ( child->m_parent != this ) |
135 | return; | 136 | return; |
136 | 137 | ||
137 | if ( m_first == child ) | 138 | if ( m_first == child ) |
138 | m_first = child->m_next; | 139 | m_first = child->m_next; |
139 | 140 | ||
140 | if ( m_last == child ) | 141 | if ( m_last == child ) |
141 | m_last = child->m_prev; | 142 | m_last = child->m_prev; |
142 | 143 | ||
143 | if ( child->m_prev ) | 144 | if ( child->m_prev ) |
144 | child->m_prev->m_next = child->m_next; | 145 | child->m_prev->m_next = child->m_next; |
145 | 146 | ||
146 | if ( child->m_next ) | 147 | if ( child->m_next ) |
147 | child->m_next->m_prev = child->m_prev; | 148 | child->m_next->m_prev = child->m_prev; |
148 | 149 | ||
149 | child->m_parent = 0; | 150 | child->m_parent = 0; |
150 | child->m_prev = 0; | 151 | child->m_prev = 0; |
151 | child->m_next = 0; | 152 | child->m_next = 0; |
152 | } | 153 | } |
153 | 154 | ||
154 | void XMLElement::save( QTextStream &s, uint indent ) | 155 | void XMLElement::save( QTextStream &s, uint indent ) |
155 | { | 156 | { |
156 | if ( !m_value.isEmpty() ) | 157 | if ( !m_value.isEmpty() ) |
157 | { | 158 | { |
158 | s << Qtopia::escapeString( m_value ); | 159 | s << Qtopia::escapeString( m_value ); |
159 | return; | 160 | return; |
160 | } | 161 | } |
161 | 162 | ||
162 | for ( uint i = 0; i < indent; ++i ) | 163 | for ( uint i = 0; i < indent; ++i ) |
163 | s << " "; | 164 | s << " "; |
164 | 165 | ||
165 | s << "<" << m_tag; | 166 | s << "<" << m_tag; |
166 | 167 | ||
167 | if ( !m_attributes.isEmpty() ) | 168 | if ( !m_attributes.isEmpty() ) |
168 | { | 169 | { |
169 | s << " "; | 170 | s << " "; |
170 | AttributeMap::ConstIterator it = m_attributes.begin(); | 171 | AttributeMap::ConstIterator it = m_attributes.begin(); |
171 | AttributeMap::ConstIterator end = m_attributes.end(); | 172 | AttributeMap::ConstIterator end = m_attributes.end(); |
172 | for (; it != end; ++it ) | 173 | for (; it != end; ++it ) |
173 | { | 174 | { |
174 | s << it.key() << "=\"" << Qtopia::escapeString( it.data() ) << "\""; | 175 | s << it.key() << "=\"" << Qtopia::escapeString( it.data() ) << "\""; |
175 | s << " "; | 176 | s << " "; |
176 | } | 177 | } |
177 | } | 178 | } |
178 | 179 | ||
179 | if ( m_last ) | 180 | if ( m_last ) |
180 | { | 181 | { |
181 | if ( ( m_first && !m_first->value().isEmpty() ) || !m_parent ) | 182 | if ( ( m_first && !m_first->value().isEmpty() ) || !m_parent ) |
182 | s << ">"; | 183 | s << ">"; |
183 | else | 184 | else |
184 | s << ">" << endl; | 185 | s << ">" << endl; |
185 | 186 | ||
186 | int newIndent = indent; | 187 | int newIndent = indent; |
187 | if ( m_parent ) | 188 | if ( m_parent ) |
188 | newIndent++; | 189 | newIndent++; |
189 | 190 | ||
190 | XMLElement *n = m_first; | 191 | XMLElement *n = m_first; |
191 | while ( n ) | 192 | while ( n ) |
192 | { | 193 | { |
193 | n->save( s, newIndent ); | 194 | n->save( s, newIndent ); |
194 | n = n->nextChild(); | 195 | n = n->nextChild(); |
195 | } | 196 | } |
196 | 197 | ||
197 | if ( m_last && m_last->value().isEmpty() && m_parent ) | 198 | if ( m_last && m_last->value().isEmpty() && m_parent ) |
198 | for ( uint i = 0; i < indent; ++i ) | 199 | for ( uint i = 0; i < indent; ++i ) |
199 | s << " "; | 200 | s << " "; |
200 | 201 | ||
201 | if ( m_parent ) | 202 | if ( m_parent ) |
202 | s << "</" << m_tag << ">" << endl; | 203 | s << "</" << m_tag << ">" << endl; |
203 | } | 204 | } |
204 | else | 205 | else |
205 | s << "/>" << endl; | 206 | s << "/>" << endl; |
206 | } | 207 | } |
207 | 208 | ||
208 | class Handler : public QXmlDefaultHandler | 209 | class Handler : public QXmlDefaultHandler |
209 | { | 210 | { |
210 | public: | 211 | public: |
211 | Handler() : m_node( 0 ), m_root( 0 ) {} | 212 | Handler() : m_node( 0 ), m_root( 0 ) {} |
212 | 213 | ||
213 | XMLElement *root() const { return m_root; } | 214 | XMLElement *root() const { return m_root; } |
214 | 215 | ||
215 | virtual bool startDocument(); | 216 | virtual bool startDocument(); |
216 | virtual bool endDocument(); | 217 | virtual bool endDocument(); |
217 | virtual bool startElement( const QString &ns, const QString &ln, const QString &qName, | 218 | virtual bool startElement( const QString &ns, const QString &ln, const QString &qName, |
218 | const QXmlAttributes &attr ); | 219 | const QXmlAttributes &attr ); |
219 | virtual bool endElement( const QString &ns, const QString &ln, const QString &qName ); | 220 | virtual bool endElement( const QString &ns, const QString &ln, const QString &qName ); |
220 | virtual bool characters( const QString &ch ); | 221 | virtual bool characters( const QString &ch ); |
221 | 222 | ||
222 | private: | 223 | private: |
223 | XMLElement *m_node; | 224 | XMLElement *m_node; |
224 | XMLElement *m_root; | 225 | XMLElement *m_root; |
225 | }; | 226 | }; |
226 | 227 | ||
227 | bool Handler::startDocument() | 228 | bool Handler::startDocument() |
228 | { | 229 | { |
229 | m_root = m_node = new XMLElement; | 230 | m_root = m_node = new XMLElement; |
230 | 231 | ||
231 | return true; | 232 | return true; |
232 | } | 233 | } |
233 | 234 | ||
234 | bool Handler::endDocument() | 235 | bool Handler::endDocument() |
235 | { | 236 | { |
236 | return m_root == m_node; | 237 | return m_root == m_node; |
237 | } | 238 | } |
238 | 239 | ||
239 | bool Handler::startElement( const QString &, const QString &, const QString &qName, | 240 | bool Handler::startElement( const QString &, const QString &, const QString &qName, |
240 | const QXmlAttributes &attr ) | 241 | const QXmlAttributes &attr ) |
241 | { | 242 | { |
242 | XMLElement *bm = new XMLElement; | 243 | XMLElement *bm = new XMLElement; |
243 | 244 | ||
244 | XMLElement::AttributeMap attributes; | 245 | XMLElement::AttributeMap attributes; |
245 | for ( int i = 0; i < attr.length(); ++i ) | 246 | for ( int i = 0; i < attr.length(); ++i ) |
246 | attributes[ attr.qName( i ) ] = attr.value( i ); | 247 | attributes[ attr.qName( i ) ] = attr.value( i ); |
247 | 248 | ||
248 | bm->setAttributes( attributes ); | 249 | bm->setAttributes( attributes ); |
249 | 250 | ||
250 | bm->setTagName( qName ); | 251 | bm->setTagName( qName ); |
251 | 252 | ||
252 | m_node->appendChild( bm ); | 253 | m_node->appendChild( bm ); |
253 | m_node = bm; | 254 | m_node = bm; |
254 | 255 | ||
255 | return true; | 256 | return true; |
256 | } | 257 | } |
257 | 258 | ||
258 | bool Handler::endElement( const QString &, const QString &, const QString & ) | 259 | bool Handler::endElement( const QString &, const QString &, const QString & ) |
259 | { | 260 | { |
260 | if ( m_node == m_root ) | 261 | if ( m_node == m_root ) |
261 | return false; | 262 | return false; |
262 | 263 | ||
263 | m_node = m_node->parent(); | 264 | m_node = m_node->parent(); |
264 | return true; | 265 | return true; |
265 | } | 266 | } |
266 | 267 | ||
267 | bool Handler::characters( const QString &ch ) | 268 | bool Handler::characters( const QString &ch ) |
268 | { | 269 | { |
269 | XMLElement *textNode = new XMLElement; | 270 | XMLElement *textNode = new XMLElement; |
270 | textNode->setValue( ch ); | 271 | textNode->setValue( ch ); |
271 | m_node->appendChild( textNode ); | 272 | m_node->appendChild( textNode ); |
272 | return true; | 273 | return true; |
273 | } | 274 | } |
274 | 275 | ||
275 | XMLElement *XMLElement::namedItem( const QString &name ) | 276 | XMLElement *XMLElement::namedItem( const QString &name ) |
276 | { | 277 | { |
277 | XMLElement *e = m_first; | 278 | XMLElement *e = m_first; |
278 | 279 | ||
279 | for (; e; e = e->nextChild() ) | 280 | for (; e; e = e->nextChild() ) |
280 | if ( e->tagName() == name ) | 281 | if ( e->tagName() == name ) |
281 | return e; | 282 | return e; |
282 | 283 | ||
283 | return 0; | 284 | return 0; |
284 | } | 285 | } |
285 | 286 | ||
286 | XMLElement *XMLElement::clone() const | 287 | XMLElement *XMLElement::clone() const |
287 | { | 288 | { |
288 | XMLElement *res = new XMLElement; | 289 | XMLElement *res = new XMLElement; |
289 | 290 | ||
290 | res->setTagName( m_tag ); | 291 | res->setTagName( m_tag ); |
291 | res->setValue( m_value ); | 292 | res->setValue( m_value ); |
292 | res->setAttributes( m_attributes ); | 293 | res->setAttributes( m_attributes ); |
293 | 294 | ||
294 | XMLElement *e = m_first; | 295 | XMLElement *e = m_first; |
295 | for (; e; e = e->m_next ) | 296 | for (; e; e = e->m_next ) |
296 | res->appendChild( e->clone() ); | 297 | res->appendChild( e->clone() ); |
297 | 298 | ||
298 | return res; | 299 | return res; |
299 | } | 300 | } |
300 | 301 | ||
301 | XMLElement *XMLElement::load( const QString &fileName ) | 302 | XMLElement *XMLElement::load( const QString &fileName ) |
302 | { | 303 | { |
303 | QFile f( fileName ); | 304 | QFile f( fileName ); |
304 | if ( !f.open( IO_ReadOnly ) ) | 305 | if ( !f.open( IO_ReadOnly ) ) |
305 | return 0; | 306 | return 0; |
306 | 307 | ||
307 | QTextStream stream( &f ); | 308 | QTextStream stream( &f ); |
308 | stream.setEncoding( QTextStream::UnicodeUTF8 ); | 309 | stream.setEncoding( QTextStream::UnicodeUTF8 ); |
309 | QXmlInputSource src( stream ); | 310 | QXmlInputSource src( stream ); |
310 | QXmlSimpleReader reader; | 311 | QXmlSimpleReader reader; |
311 | Handler handler; | 312 | Handler handler; |
312 | 313 | ||
313 | reader.setFeature( "http://trolltech.com/xml/features/report-whitespace-only-CharData", false ); | 314 | reader.setFeature( "http://trolltech.com/xml/features/report-whitespace-only-CharData", false ); |
314 | reader.setContentHandler( &handler ); | 315 | reader.setContentHandler( &handler ); |
315 | reader.parse( src ); | 316 | reader.parse( src ); |
316 | 317 | ||
317 | return handler.root();; | 318 | return handler.root();; |
318 | } | 319 | } |
319 | 320 | ||
320 | /* vim: et sw=4 | 321 | /* vim: et sw=4 |
321 | */ | 322 | */ |
diff --git a/libopie2/opiecore/xmltree.h b/libopie2/opiecore/xmltree.h index ed93c23..4a6b6d9 100644 --- a/libopie2/opiecore/xmltree.h +++ b/libopie2/opiecore/xmltree.h | |||
@@ -1,112 +1,117 @@ | |||
1 | /* This file is part of the KDE project | 1 | /* This file is part of the KDE project |
2 | Copyright (C) 2000,2001 Simon Hausmann <hausmann@kde.org> | 2 | Copyright (C) 2000,2001 Simon Hausmann <hausmann@kde.org> |
3 | 3 | ||
4 | This library is free software; you can redistribute it and/or | 4 | This library is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU Library General Public | 5 | modify it under the terms of the GNU Library General Public |
6 | License as published by the Free Software Foundation; either | 6 | License as published by the Free Software Foundation; either |
7 | version 2 of the License, or (at your option) any later version. | 7 | version 2 of the License, or (at your option) any later version. |
8 | 8 | ||
9 | This library is distributed in the hope that it will be useful, | 9 | This library is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | Library General Public License for more details. | 12 | Library General Public License for more details. |
13 | 13 | ||
14 | You should have received a copy of the GNU Library General Public License | 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 | 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, | 16 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
17 | Boston, MA 02111-1307, USA. | 17 | Boston, MA 02111-1307, USA. |
18 | */ | 18 | */ |
19 | 19 | ||
20 | 20 | ||
21 | #ifndef __bookmarks_h__ | 21 | #ifndef __bookmarks_h__ |
22 | #define __bookmarks_h__ | 22 | #define __bookmarks_h__ |
23 | 23 | ||
24 | #include <qstring.h> | 24 | #include <qstring.h> |
25 | #include <qmap.h> | 25 | #include <qmap.h> |
26 | #include <qtextstream.h> | 26 | #include <qtextstream.h> |
27 | 27 | ||
28 | namespace Opie | ||
29 | { | ||
30 | |||
28 | /** | 31 | /** |
29 | * A small xml lib written by Simon Hausmann. | 32 | * A small xml lib written by Simon Hausmann. |
30 | */ | 33 | */ |
31 | class XMLElement | 34 | class XMLElement |
32 | { | 35 | { |
33 | public: | 36 | public: |
34 | typedef QMap<QString, QString> AttributeMap; | 37 | typedef QMap<QString, QString> AttributeMap; |
35 | 38 | ||
36 | /** | 39 | /** |
37 | * The constructor of XMLElement | 40 | * The constructor of XMLElement |
38 | */ | 41 | */ |
39 | XMLElement(); | 42 | XMLElement(); |
40 | ~XMLElement(); | 43 | ~XMLElement(); |
41 | 44 | ||
42 | /** appendChild appends a child to the XMLElement behind the last element. | 45 | /** appendChild appends a child to the XMLElement behind the last element. |
43 | * The ownership of the child get's transfered to the | 46 | * The ownership of the child get's transfered to the |
44 | * this XMLElement. | 47 | * this XMLElement. |
45 | * If child is already the child of another parent | 48 | * If child is already the child of another parent |
46 | * it's get removed from the other parent first. | 49 | * it's get removed from the other parent first. |
47 | */ | 50 | */ |
48 | void appendChild( XMLElement *child ); | 51 | void appendChild( XMLElement *child ); |
49 | 52 | ||
50 | /** inserts newChild after refChild. If newChild is the child | 53 | /** inserts newChild after refChild. If newChild is the child |
51 | * of another parent the child will get removed. | 54 | * of another parent the child will get removed. |
52 | * The ownership of child gets transfered. | 55 | * The ownership of child gets transfered. |
53 | * | 56 | * |
54 | */ | 57 | */ |
55 | void insertAfter( XMLElement *newChild, XMLElement *refChild ); | 58 | void insertAfter( XMLElement *newChild, XMLElement *refChild ); |
56 | 59 | ||
57 | /** same as insertAfter but the element get's inserted before refChild. | 60 | /** same as insertAfter but the element get's inserted before refChild. |
58 | * | 61 | * |
59 | */ | 62 | */ |
60 | void insertBefore( XMLElement *newChild, XMLElement *refChild ); | 63 | void insertBefore( XMLElement *newChild, XMLElement *refChild ); |
61 | 64 | ||
62 | /** removeChild removes the child from the XMLElement. | 65 | /** removeChild removes the child from the XMLElement. |
63 | * The ownership gets dropped. You need to delete the | 66 | * The ownership gets dropped. You need to delete the |
64 | * child yourself. | 67 | * child yourself. |
65 | */ | 68 | */ |
66 | void removeChild( XMLElement *child ); | 69 | void removeChild( XMLElement *child ); |
67 | 70 | ||
68 | /** parent() returns the parent of this XMLElement | 71 | /** parent() returns the parent of this XMLElement |
69 | * If there is no parent 0l gets returned | 72 | * If there is no parent 0l gets returned |
70 | */ | 73 | */ |
71 | XMLElement *parent() const { return m_parent; } | 74 | XMLElement *parent() const { return m_parent; } |
72 | XMLElement *firstChild() const { return m_first; } | 75 | XMLElement *firstChild() const { return m_first; } |
73 | XMLElement *nextChild() const { return m_next; } | 76 | XMLElement *nextChild() const { return m_next; } |
74 | XMLElement *prevChild() const { return m_prev; } | 77 | XMLElement *prevChild() const { return m_prev; } |
75 | XMLElement *lastChild() const { return m_last; } | 78 | XMLElement *lastChild() const { return m_last; } |
76 | 79 | ||
77 | void setTagName( const QString &tag ) { m_tag = tag; } | 80 | void setTagName( const QString &tag ) { m_tag = tag; } |
78 | QString tagName() const { return m_tag; } | 81 | QString tagName() const { return m_tag; } |
79 | 82 | ||
80 | void setValue( const QString &val ) { m_value = val; } | 83 | void setValue( const QString &val ) { m_value = val; } |
81 | QString value() const { return m_value; } | 84 | QString value() const { return m_value; } |
82 | 85 | ||
83 | void setAttributes( const AttributeMap &attrs ) { m_attributes = attrs; } | 86 | void setAttributes( const AttributeMap &attrs ) { m_attributes = attrs; } |
84 | AttributeMap attributes() const { return m_attributes; } | 87 | AttributeMap attributes() const { return m_attributes; } |
85 | AttributeMap &attributes() { return m_attributes; } | 88 | AttributeMap &attributes() { return m_attributes; } |
86 | 89 | ||
87 | QString attribute( const QString & ) const; | 90 | QString attribute( const QString & ) const; |
88 | void setAttribute( const QString &attr, const QString &value ); | 91 | void setAttribute( const QString &attr, const QString &value ); |
89 | void save( QTextStream &stream, uint indent = 0 ); | 92 | void save( QTextStream &stream, uint indent = 0 ); |
90 | 93 | ||
91 | XMLElement *namedItem( const QString &name ); | 94 | XMLElement *namedItem( const QString &name ); |
92 | 95 | ||
93 | XMLElement *clone() const; | 96 | XMLElement *clone() const; |
94 | 97 | ||
95 | static XMLElement *load( const QString &fileName ); | 98 | static XMLElement *load( const QString &fileName ); |
96 | 99 | ||
97 | private: | 100 | private: |
98 | QString m_tag; | 101 | QString m_tag; |
99 | QString m_value; | 102 | QString m_value; |
100 | AttributeMap m_attributes; | 103 | AttributeMap m_attributes; |
101 | 104 | ||
102 | XMLElement *m_parent; | 105 | XMLElement *m_parent; |
103 | XMLElement *m_next; | 106 | XMLElement *m_next; |
104 | XMLElement *m_prev; | 107 | XMLElement *m_prev; |
105 | XMLElement *m_first; | 108 | XMLElement *m_first; |
106 | XMLElement *m_last; | 109 | XMLElement *m_last; |
107 | 110 | ||
108 | XMLElement( const XMLElement &rhs ); | 111 | XMLElement( const XMLElement &rhs ); |
109 | XMLElement &operator=( const XMLElement &rhs ); | 112 | XMLElement &operator=( const XMLElement &rhs ); |
110 | }; | 113 | }; |
111 | 114 | ||
115 | } // namespace Opie | ||
116 | |||
112 | #endif | 117 | #endif |