1/* This file is part of the KDE project
2 Copyright (C) 2010 Dag Andersen <danders@get2net.dk>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19
20#include "KPlatoXmlLoader.h"
21
22#include "kptconfig.h"
23#include "kptpackage.h"
24#include "kptxmlloaderobject.h"
25#include "kptproject.h"
26
27#include "KoXmlReader.h"
28
29#include <kmessagebox.h>
30#include <ktimezone.h>
31#include <ksystemtimezone.h>
32#include <kdebug.h>
33#include <kdeversion.h>
34
35#include <QDateTime>
36
37extern int kplatoXmlDebugArea();
38
39namespace KPlato
40{
41
42KPlatoXmlLoader::KPlatoXmlLoader( XMLLoaderObject &loader, Project* project )
43 : KPlatoXmlLoaderBase(),
44 m_loader( loader ),
45 m_project( project )
46{
47}
48
49QString KPlatoXmlLoader::errorMessage() const
50{
51 return m_message;
52}
53
54Package *KPlatoXmlLoader::package() const
55{
56 return m_package;
57}
58
59QString KPlatoXmlLoader::timeTag() const
60{
61 return m_timeTag;
62}
63
64bool KPlatoXmlLoader::load( const KoXmlElement& plan )
65{
66 kDebug(kplatoXmlDebugArea())<<"plan";
67 QString syntaxVersion = plan.attribute( "version" );
68 m_loader.setVersion( syntaxVersion );
69 if ( syntaxVersion.isEmpty() ) {
70 int ret = KMessageBox::warningContinueCancel(
71 0, i18n( "This document has no syntax version.\n"
72 "Opening it in Plan may lose information." ),
73 i18n( "File-Format Error" ), KGuiItem( i18n( "Continue" ) ) );
74 if ( ret == KMessageBox::Cancel ) {
75 m_message = "USER_CANCELED";
76 return false;
77 }
78 // set to max version and hope for the best
79 m_loader.setVersion( KPLATO_MAX_FILE_SYNTAX_VERSION );
80 } else if ( syntaxVersion > KPLATO_MAX_FILE_SYNTAX_VERSION ) {
81 int ret = KMessageBox::warningContinueCancel(
82 0, i18n( "This document was created with a newer version of KPlato than Plan can load.\n"
83 "Syntax version: %1\n"
84 "Opening it in this version of Plan may lose some information.", syntaxVersion ),
85 i18n( "File-Format Mismatch" ), KGuiItem( i18n( "Continue" ) ) );
86 if ( ret == KMessageBox::Cancel ) {
87 m_message = "USER_CANCELED";
88 return false;
89 }
90 }
91 m_loader.startLoad();
92 bool result = false;
93 KoXmlNode n = plan.firstChild();
94 for ( ; ! n.isNull(); n = n.nextSibling() ) {
95 if ( ! n.isElement() ) {
96 continue;
97 }
98 KoXmlElement e = n.toElement();
99 if ( e.tagName() == "project" ) {
100 m_loader.setProject( m_project );
101 result = load( m_project, e, m_loader );
102 if ( result ) {
103 if ( m_project->id().isEmpty() ) {
104 m_project->setId( m_project->uniqueNodeId() );
105 m_project->registerNodeId( m_project );
106 }
107 } else {
108 m_loader.addMsg( XMLLoaderObject::Errors, "Loading of project failed" );
109 kError()<<"Loading of project failed";
110 //TODO add some ui here
111 }
112 }
113 }
114 m_loader.stopLoad();
115 return result;
116}
117
118
119bool KPlatoXmlLoader::loadWorkpackage( const KoXmlElement& plan )
120{
121 kDebug(kplatoXmlDebugArea());
122 bool ok = false;
123 if ( m_loader.workVersion() > KPLATOWORK_MAX_FILE_SYNTAX_VERSION ) {
124 int ret = KMessageBox::warningContinueCancel(
125 0, i18n( "This document was created with a newer version of KPlatoWork (syntax version: %1)\n"
126 "Opening it in this version of PlanWork will lose some information.", m_loader.workVersion() ),
127 i18n( "File-Format Mismatch" ), KGuiItem( i18n( "Continue" ) ) );
128 if ( ret == KMessageBox::Cancel ) {
129 m_message = "USER_CANCELED";
130 return false;
131 }
132 }
133 m_loader.startLoad();
134 Project *proj = new Project();
135 Package *package = new Package();
136 package->project = proj;
137 KoXmlNode n = plan.firstChild();
138 for ( ; ! n.isNull(); n = n.nextSibling() ) {
139 if ( ! n.isElement() ) {
140 continue;
141 }
142 KoXmlElement e = n.toElement();
143 if ( e.tagName() == "project" ) {
144 m_loader.setProject( proj );
145 ok = load( proj, e, m_loader );
146 if ( ! ok ) {
147 m_loader.addMsg( XMLLoaderObject::Errors, "Loading of work package failed" );
148 //TODO add some ui here
149 }
150 } else if ( e.tagName() == "workpackage" ) {
151 m_timeTag = e.attribute( "time-tag" );
152 package->ownerId = e.attribute( "owner-id" );
153 package->ownerName = e.attribute( "owner" );
154 KoXmlElement elem;
155 forEachElement( elem, e ) {
156 if ( elem.tagName() != "settings" ) {
157 continue;
158 }
159 package->settings.usedEffort = (bool)elem.attribute( "used-effort" ).toInt();
160 package->settings.progress = (bool)elem.attribute( "progress" ).toInt();
161 package->settings.documents = (bool)elem.attribute( "documents" ).toInt();
162 }
163 }
164 }
165 if ( proj->numChildren() > 0 ) {
166 WorkPackage &wp = static_cast<Task*>( proj->childNode( 0 ) )->workPackage();
167 if ( wp.ownerId().isEmpty() ) {
168 wp.setOwnerId( package->ownerId );
169 wp.setOwnerName( package->ownerName );
170 }
171 }
172 return ok;
173}
174
175} // namespace KPlato
176
177#include "KPlatoXmlLoader.moc"
178