From f508189682b6fba62e08feeb1596f682bad5fff9 Mon Sep 17 00:00:00 2001 From: tpearson Date: Wed, 24 Feb 2010 18:42:24 +0000 Subject: Added KDE3 version of PikLab git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/applications/piklab@1095639 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- src/common/global/xml_data_file.cpp | 160 ++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 src/common/global/xml_data_file.cpp (limited to 'src/common/global/xml_data_file.cpp') diff --git a/src/common/global/xml_data_file.cpp b/src/common/global/xml_data_file.cpp new file mode 100644 index 0000000..2464b34 --- /dev/null +++ b/src/common/global/xml_data_file.cpp @@ -0,0 +1,160 @@ +/*************************************************************************** + * Copyright (C) 2005-2006 Nicolas Hadacek * + * * + * 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 +#include +#include +#include +#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