summaryrefslogtreecommitdiffstats
path: root/src/common/global/xml_data_file.cpp
blob: 2464b34f5a16fb14bbfffc5cd4b6bc2626bc236b (plain)
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 <hadacek@kde.org>             *
 *                                                                         *
 *   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 <qfile.h>
#include <qstringlist.h>
#include <ksimpleconfig.h>
#include <klocale.h>
#include "common/global/pfile.h"

XmlDataFile::XmlDataFile(const PURL::Url &url, const QString &name)
  : _url(url), _name(name), _document(name)
{
  QDomElement root = _document.createElement(name);
  _document.appendChild(root);
}

bool XmlDataFile::load(QString &error)
{
  Log::StringView sview;
  PURL::File file(_url, sview);
  if ( !file.openForRead() ) {
    error = i18n("Error opening file: %1").arg(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(QString &error) const
{
  Log::StringView sview;
  PURL::File file(_url, sview);
  bool ok = file.openForWrite();
  if (ok) {
    QString s = _document.toString(2);
    file.appendText(s);
    ok = file.close();
  }
  if ( !ok ) error = i18n("Error saving file: %1").arg(sview.string());
  return ok;
}

QDomElement XmlDataFile::findChildElement(QDomElement parent, const QString &name) const
{
  QDomNodeList list = parent.elementsByTagName(name);
  return list.item(0).toElement();
}

QDomElement XmlDataFile::createChildElement(QDomElement parent, const QString &name)
{
  QDomNodeList list = parent.elementsByTagName(name);
  if ( list.count()==0 ) {
    QDomElement element = _document.createElement(name);
    parent.appendChild(element);
    return element;
  }
  return list.item(0).toElement();
}

void XmlDataFile::removeChilds(QDomNode parent) const
{
  QDomNodeList list = parent.childNodes();
  for (uint i=0; i<list.count(); i++)
    parent.removeChild(list.item(i));
}

QString XmlDataFile::value(const QString &group, const QString &key, const QString &defValue) const
{
  QDomElement root = _document.documentElement();
  QDomElement groupElement = findChildElement(root, group);
  if ( groupElement.isNull() ) return defValue;
  QDomElement element = findChildElement(groupElement, key);
  if ( element.isNull() ) return defValue;
  QDomText text = element.firstChild().toText();
  if ( text.isNull() ) return defValue;
  return text.data();
}

void XmlDataFile::setValue(const QString &group, const QString &key, const QString &value)
{
  QDomElement root = _document.documentElement();
  QDomElement groupElement = createChildElement(root, group);
  QDomElement element = createChildElement(groupElement, key);
  removeChilds(element);
  QDomText text = _document.createTextNode(value);
  element.appendChild(text);
}

QStringList XmlDataFile::listValues(const QString &group, const QString &key, const QStringList &defaultValues) const
{
  QStringList list;
  QDomElement root = _document.documentElement();
  QDomElement groupElement = findChildElement(root, group);
  if ( groupElement.isNull() ) return defaultValues;
  QDomElement element = findChildElement(groupElement, key);
  if ( element.isNull() ) return defaultValues;
  QDomNodeList childs = element.childNodes();
  if ( childs.count()==1 ) { // legacy compatibility
    QDomText text = element.firstChild().toText();
    if ( !text.isNull() ) return text.data();
  }
  for (uint i=0; i<childs.count(); i++) {
    QDomText text = childs.item(i).toElement().firstChild().toText();
    if ( text.isNull() ) continue;
    list.append(text.data());
  }
  return list;
}

void XmlDataFile::appendListValue(const QString &group, const QString &key, const QString &value)
{
  QDomElement root = _document.documentElement();
  QDomElement groupElement = createChildElement(root, group);
  QDomElement element = createChildElement(groupElement, key);
  QDomElement item = _document.createElement("item");
  element.appendChild(item);
  QDomText text = _document.createTextNode(value);
  item.appendChild(text);
}

void XmlDataFile::removeListValue(const QString &group, const QString &key, const QString &value)
{
  QDomElement root = _document.documentElement();
  QDomElement groupElement = createChildElement(root, group);
  QDomElement element = createChildElement(groupElement, key);
  QDomNodeList list = element.childNodes();
  for (uint i=0; i<list.count(); i++) {
    QDomElement item = list.item(i).toElement();
    QDomText text = item.firstChild().toText();
    if ( text.isNull() || text.data()!=value ) continue;
    element.removeChild(item);
    break;
  }
}

void XmlDataFile::clearList(const QString &group, const QString &key)
{
  QDomElement root = _document.documentElement();
  QDomElement groupElement = createChildElement(root, group);
  QDomElement element = createChildElement(groupElement, key);
  groupElement.removeChild(element);
}

void XmlDataFile::setListValues(const QString &group, const QString &key, const QStringList &values)
{
  clearList(group, key);
  for (uint i=0; i<values.count(); i++) appendListValue(group, key, values[i]);
}