File indexing completed on 2024-04-28 16:24:35

0001 /* This file is part of the KDE project
0002    Copyright (C) 2005, 2007, 2011 Dag Andersen <danders@get2net.dk>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 // clazy:excludeall=qstring-arg
0021 #include "kptcontext.h"
0022 #include "kptview.h"
0023 #include "kptdebug.h"
0024 
0025 #include <QDomDocument>
0026 
0027 namespace KPlato
0028 {
0029 
0030 Context::Context()
0031     : currentEstimateType(0),
0032       currentSchedule(0),
0033       m_contextLoaded(false)
0034 {
0035     ganttview.ganttviewsize = -1;
0036     ganttview.taskviewsize = -1;
0037 
0038     accountsview.accountsviewsize = -1;
0039     accountsview.periodviewsize = -1;
0040 
0041 
0042 }
0043 
0044 Context::~Context() {
0045 }
0046 
0047 const KoXmlElement &Context::context() const
0048 {
0049     return m_context;
0050 }
0051 
0052 bool Context::setContent(const QString &str)
0053 {
0054     KoXmlDocument doc;
0055     if (doc.setContent(str)) {
0056         return load(doc);
0057     }
0058     return false;
0059 }
0060 
0061 QDomDocument Context::document() const
0062 {
0063     QDomDocument document("plan.context");
0064 
0065     document.appendChild(document.createProcessingInstruction(
0066         "xml",
0067         "version=\"1.0\" encoding=\"UTF-8\""));
0068 
0069     KoXml::asQDomElement(document, m_document.documentElement());
0070     return document;
0071 }
0072 
0073 bool Context::load(const KoXmlDocument &document) {
0074     m_document = document; // create a copy, document is deleted under our feet
0075 
0076     // Check if this is the right app
0077     KoXmlElement elm = m_document.documentElement();
0078     QString value = elm.attribute("mime", QString());
0079     if (value.isEmpty()) {
0080         errorPlan << "No mime type specified!";
0081 //        setErrorMessage(i18n("Invalid document. No mimetype specified."));
0082         return false;
0083     } else if (value != "application/x-vnd.kde.plan") {
0084         if (value == "application/x-vnd.kde.kplato") {
0085             // accept, since we forgot to change kplato to plan for so long...
0086         } else {
0087             errorPlan << "Unknown mime type " << value;
0088 //        setErrorMessage(i18n("Invalid document. Expected mimetype application/x-vnd.kde.kplato, got %1", value));
0089             return false;
0090         }
0091     }
0092 /*    QString m_syntaxVersion = elm.attribute("version", "0.0");
0093     if (m_syntaxVersion > "0.0") {
0094         KMessageBox::ButtonCode ret = KMessageBox::warningContinueCancel(
0095                       0, i18n("This document was created with a newer version of Plan (syntax version: %1)\n"
0096                                "Opening it in this version of Plan will lose some information.", m_syntaxVersion),
0097                       i18n("File-Format Mismatch"), KGuiItem(i18n("Continue")));
0098         if (ret == KMessageBox::Cancel) {
0099             setErrorMessage("USER_CANCELED");
0100             return false;
0101         }
0102     }
0103 */
0104 /*
0105 #ifdef KOXML_USE_QDOM
0106     int numNodes = elm.childNodes().count();
0107 #else
0108     int numNodes = elm.childNodesCount();
0109 #endif
0110 */
0111     KoXmlNode n = elm.firstChild();
0112     for (; ! n.isNull(); n = n.nextSibling()) {
0113         if (! n.isElement()) {
0114             continue;
0115         }
0116         KoXmlElement element = n.toElement();
0117         if (element.tagName() == "context") {
0118             m_context = element;
0119             m_contextLoaded = true;
0120         }
0121     }
0122     return true;
0123 }
0124 
0125 QDomDocument Context::save(const View *view) const {
0126     QDomDocument document("plan.context");
0127 
0128     document.appendChild(document.createProcessingInstruction(
0129                               "xml",
0130                               "version=\"1.0\" encoding=\"UTF-8\""));
0131 
0132     QDomElement doc = document.createElement("context");
0133     doc.setAttribute("editor", "Plan");
0134     doc.setAttribute("mime", "application/x-vnd.kde.plan");
0135     doc.setAttribute("version", QString::number(0.0));
0136     document.appendChild(doc);
0137 
0138     QDomElement e = doc.ownerDocument().createElement("context");
0139     doc.appendChild(e);
0140     view->saveContext(e);
0141 
0142     return document;
0143 }
0144 
0145 }  //KPlato namespace