1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
/***************************************************************************
* Copyright (C) 2005-2006 Nicolas Hadacek <[email protected]> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
#include "xml_data_file.h"
#include <tqfile.h>
#include <tqstringlist.h>
#include <ksimpleconfig.h>
#include <klocale.h>
#include "common/global/pfile.h"
XmlDataFile::XmlDataFile(const PURL::Url &url, const TQString &name)
: _url(url), _name(name), _document(name)
{
TQDomElement root = _document.createElement(name);
_document.appendChild(root);
}
bool XmlDataFile::load(TQString &error)
{
Log::StringView sview;
PURL::File file(_url, sview);
if ( !file.openForRead() ) {
error = i18n("Error opening file: %1").tqarg(sview.string());
return false;
}
if ( !_document.setContent(file.qfile(), false, &error) ) return false;
if ( _document.doctype().name()!=_name
|| _document.documentElement().nodeName()!=_name ) {
error = i18n("File is not of correct type.");
return false;
}
return true;
}
bool XmlDataFile::save(TQString &error) const
{
Log::StringView sview;
PURL::File file(_url, sview);
bool ok = file.openForWrite();
if (ok) {
TQString s = _document.toString(2);
file.appendText(s);
ok = file.close();
}
if ( !ok ) error = i18n("Error saving file: %1").tqarg(sview.string());
return ok;
}
TQDomElement XmlDataFile::findChildElement(TQDomElement parent, const TQString &name) const
{
TQDomNodeList list = parent.elementsByTagName(name);
return list.item(0).toElement();
}
TQDomElement XmlDataFile::createChildElement(TQDomElement parent, const TQString &name)
{
TQDomNodeList list = parent.elementsByTagName(name);
if ( list.count()==0 ) {
TQDomElement element = _document.createElement(name);
parent.appendChild(element);
return element;
}
return list.item(0).toElement();
}
void XmlDataFile::removeChilds(TQDomNode parent) const
{
TQDomNodeList list = parent.childNodes();
for (uint i=0; i<list.count(); i++)
parent.removeChild(list.item(i));
}
TQString XmlDataFile::value(const TQString &group, const TQString &key, const TQString &defValue) const
{
TQDomElement root = _document.documentElement();
TQDomElement groupElement = findChildElement(root, group);
if ( groupElement.isNull() ) return defValue;
TQDomElement element = findChildElement(groupElement, key);
if ( element.isNull() ) return defValue;
TQDomText text = element.firstChild().toText();
if ( text.isNull() ) return defValue;
return text.data();
}
void XmlDataFile::setValue(const TQString &group, const TQString &key, const TQString &value)
{
TQDomElement root = _document.documentElement();
TQDomElement groupElement = createChildElement(root, group);
TQDomElement element = createChildElement(groupElement, key);
removeChilds(element);
TQDomText text = _document.createTextNode(value);
element.appendChild(text);
}
TQStringList XmlDataFile::listValues(const TQString &group, const TQString &key, const TQStringList &defaultValues) const
{
TQStringList list;
TQDomElement root = _document.documentElement();
TQDomElement groupElement = findChildElement(root, group);
if ( groupElement.isNull() ) return defaultValues;
TQDomElement element = findChildElement(groupElement, key);
if ( element.isNull() ) return defaultValues;
TQDomNodeList childs = element.childNodes();
if ( childs.count()==1 ) { // legacy compatibility
TQDomText text = element.firstChild().toText();
if ( !text.isNull() ) return text.data();
}
for (uint i=0; i<childs.count(); i++) {
TQDomText text = childs.item(i).toElement().firstChild().toText();
if ( text.isNull() ) continue;
list.append(text.data());
}
return list;
}
void XmlDataFile::appendListValue(const TQString &group, const TQString &key, const TQString &value)
{
TQDomElement root = _document.documentElement();
TQDomElement groupElement = createChildElement(root, group);
TQDomElement element = createChildElement(groupElement, key);
TQDomElement item = _document.createElement("item");
element.appendChild(item);
TQDomText text = _document.createTextNode(value);
item.appendChild(text);
}
void XmlDataFile::removeListValue(const TQString &group, const TQString &key, const TQString &value)
{
TQDomElement root = _document.documentElement();
TQDomElement groupElement = createChildElement(root, group);
TQDomElement element = createChildElement(groupElement, key);
TQDomNodeList list = element.childNodes();
for (uint i=0; i<list.count(); i++) {
TQDomElement item = list.item(i).toElement();
TQDomText text = item.firstChild().toText();
if ( text.isNull() || text.data()!=value ) continue;
element.removeChild(item);
break;
}
}
void XmlDataFile::clearList(const TQString &group, const TQString &key)
{
TQDomElement root = _document.documentElement();
TQDomElement groupElement = createChildElement(root, group);
TQDomElement element = createChildElement(groupElement, key);
groupElement.removeChild(element);
}
void XmlDataFile::setListValues(const TQString &group, const TQString &key, const TQStringList &values)
{
clearList(group, key);
for (uint i=0; i<values.count(); i++) appendListValue(group, key, values[i]);
}
|