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

0001 /* This file is part of the KDE project
0002  Copyright (C) 2010 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 "KPlatoXmlLoader.h"
0022 
0023 #include "kptconfig.h"
0024 #include "kptpackage.h"
0025 #include "kptxmlloaderobject.h"
0026 #include "kptproject.h"
0027 
0028 #include "KoXmlReader.h"
0029 
0030 #include <KLocalizedString>
0031 
0032 #include <kmessagebox.h>
0033 
0034 
0035 extern int kplatoXmlDebugArea();
0036 
0037 namespace KPlato
0038 {
0039 
0040 KPlatoXmlLoader::KPlatoXmlLoader(XMLLoaderObject &loader, Project* project)
0041     : KPlatoXmlLoaderBase(),
0042     m_loader(loader),
0043     m_project(project)
0044 {
0045 }
0046 
0047 QString KPlatoXmlLoader::errorMessage() const
0048 {
0049     return m_message;
0050 }
0051 
0052 Package *KPlatoXmlLoader::package() const
0053 {
0054     return m_package;
0055 }
0056 
0057 QString KPlatoXmlLoader::timeTag() const
0058 {
0059     return m_timeTag;
0060 }
0061 
0062 bool KPlatoXmlLoader::load(const KoXmlElement& plan)
0063 {
0064     debugPlanXml<<"plan";
0065     QString syntaxVersion = plan.attribute("version");
0066     m_loader.setVersion(syntaxVersion);
0067     if (syntaxVersion.isEmpty()) {
0068         KMessageBox::ButtonCode ret = KMessageBox::warningContinueCancel(
0069                       0, i18n("This document has no syntax version.\n"
0070                                "Opening it in Plan may lose information."),
0071                       i18n("File-Format Error"), KGuiItem(i18n("Continue")));
0072         if (ret == KMessageBox::Cancel) {
0073             m_message = "USER_CANCELED";
0074             return false;
0075         }
0076         // set to max version and hope for the best
0077         m_loader.setVersion(KPLATO_MAX_FILE_SYNTAX_VERSION);
0078     } else if (syntaxVersion > KPLATO_MAX_FILE_SYNTAX_VERSION) {
0079         KMessageBox::ButtonCode ret = KMessageBox::warningContinueCancel(
0080                       0, i18n("This document was created with a newer version of KPlato than Plan can load.\n"
0081                                "Syntax version: %1\n"
0082                                "Opening it in this version of Plan may lose some information.", syntaxVersion),
0083                       i18n("File-Format Mismatch"), KGuiItem(i18n("Continue")));
0084         if (ret == KMessageBox::Cancel) {
0085             m_message = "USER_CANCELED";
0086             return false;
0087         }
0088     }
0089     m_loader.startLoad();
0090     bool result = false;
0091     KoXmlNode n = plan.firstChild();
0092     for (; ! n.isNull(); n = n.nextSibling()) {
0093         if (! n.isElement()) {
0094             continue;
0095         }
0096         KoXmlElement e = n.toElement();
0097         if (e.tagName() == "project") {
0098             m_loader.setProject(m_project);
0099             result = load(m_project, e, m_loader);
0100             if (result) {
0101                 if (m_project->id().isEmpty()) {
0102                     m_project->setId(m_project->uniqueNodeId());
0103                     m_project->registerNodeId(m_project);
0104                 }
0105             } else {
0106                 m_loader.addMsg(XMLLoaderObject::Errors, "Loading of project failed");
0107                 errorPlanXml <<"Loading of project failed";
0108                 //TODO add some ui here
0109             }
0110         }
0111     }
0112     m_loader.stopLoad();
0113     return result;
0114 }
0115 
0116 
0117 bool KPlatoXmlLoader::loadWorkpackage(const KoXmlElement& plan)
0118 {
0119     debugPlanXml;
0120     bool ok = false;
0121     if (m_loader.workVersion() > KPLATOWORK_MAX_FILE_SYNTAX_VERSION) {
0122         KMessageBox::ButtonCode ret = KMessageBox::warningContinueCancel(
0123                 0, i18n("This document was created with a newer version of KPlatoWork (syntax version: %1)\n"
0124                 "Opening it in this version of PlanWork will lose some information.", m_loader.workVersion()),
0125                 i18n("File-Format Mismatch"), KGuiItem(i18n("Continue")));
0126         if (ret == KMessageBox::Cancel) {
0127             m_message = "USER_CANCELED";
0128             return false;
0129         }
0130     }
0131     m_loader.startLoad();
0132     Project *proj = new Project();
0133     Package *package = new Package();
0134     package->project = proj;
0135     KoXmlNode n = plan.firstChild();
0136     for (; ! n.isNull(); n = n.nextSibling()) {
0137         if (! n.isElement()) {
0138             continue;
0139         }
0140         KoXmlElement e = n.toElement();
0141         if (e.tagName() == "project") {
0142             m_loader.setProject(proj);
0143             ok = load(proj, e, m_loader);
0144             if (! ok) {
0145                 m_loader.addMsg(XMLLoaderObject::Errors, "Loading of work package failed");
0146                 //TODO add some ui here
0147             }
0148         } else if (e.tagName() == "workpackage") {
0149             m_timeTag = e.attribute("time-tag");
0150             package->ownerId = e.attribute("owner-id");
0151             package->ownerName = e.attribute("owner");
0152             KoXmlElement elem;
0153             forEachElement(elem, e) {
0154                 if (elem.tagName() != "settings") {
0155                     continue;
0156                 }
0157                 package->settings.usedEffort = (bool)elem.attribute("used-effort").toInt();
0158                 package->settings.progress = (bool)elem.attribute("progress").toInt();
0159                 package->settings.documents = (bool)elem.attribute("documents").toInt();
0160             }
0161         }
0162     }
0163     if (proj->numChildren() > 0) {
0164         WorkPackage &wp = static_cast<Task*>(proj->childNode(0))->workPackage();
0165         if (wp.ownerId().isEmpty()) {
0166             wp.setOwnerId(package->ownerId);
0167             wp.setOwnerName(package->ownerName);
0168         }
0169     }
0170     return ok;
0171 }
0172 
0173 } // namespace KPlato