/*************************************************************************** * 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