1/* This file is part of the KDE project
2 Copyright (C) 2005, 2007, 2011 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 "kptcontext.h"
21#include "kptview.h"
22
23#include <KoXmlReader.h>
24
25#include <QDomDocument>
26
27#include <kdebug.h>
28
29namespace KPlato
30{
31
32Context::Context()
33 : currentEstimateType(0),
34 currentSchedule(0),
35 m_contextLoaded( false )
36{
37 ganttview.ganttviewsize = -1;
38 ganttview.taskviewsize = -1;
39
40 accountsview.accountsviewsize = -1;
41 accountsview.periodviewsize = -1;
42
43
44}
45
46Context::~Context() {
47}
48
49const KoXmlElement &Context::context() const
50{
51 return m_context;
52}
53
54bool Context::setContent( const QString &str )
55{
56 KoXmlDocument doc;
57 if ( doc.setContent( str ) ) {
58 return load( doc );
59 }
60 return false;
61}
62
63bool Context::load( const KoXmlDocument &document ) {
64 m_document = document; // create a copy, document is deleted under our feet
65
66 // Check if this is the right app
67 KoXmlElement elm = m_document.documentElement();
68 QString value = elm.attribute( "mime", QString() );
69 if ( value.isEmpty() ) {
70 kError() << "No mime type specified!";
71// setErrorMessage( i18n( "Invalid document. No mimetype specified." ) );
72 return false;
73 } else if ( value != "application/x-vnd.kde.plan" ) {
74 if ( value == "application/x-vnd.kde.kplato" ) {
75 // accept, since we forgot to change kplato to plan for so long...
76 } else {
77 kError() << "Unknown mime type " << value;
78// setErrorMessage( i18n( "Invalid document. Expected mimetype application/x-vnd.kde.kplato, got %1", value ) );
79 return false;
80 }
81 }
82/* QString m_syntaxVersion = elm.attribute( "version", "0.0" );
83 if ( m_syntaxVersion > "0.0" ) {
84 int ret = KMessageBox::warningContinueCancel(
85 0, i18n( "This document was created with a newer version of Plan (syntax version: %1)\n"
86 "Opening it in this version of Plan will lose some information.", m_syntaxVersion ),
87 i18n( "File-Format Mismatch" ), KGuiItem( i18n( "Continue" ) ) );
88 if ( ret == KMessageBox::Cancel ) {
89 setErrorMessage( "USER_CANCELED" );
90 return false;
91 }
92 }
93*/
94/*
95#ifdef KOXML_USE_QDOM
96 int numNodes = elm.childNodes().count();
97#else
98 int numNodes = elm.childNodesCount();
99#endif
100*/
101 KoXmlNode n = elm.firstChild();
102 for ( ; ! n.isNull(); n = n.nextSibling() ) {
103 if ( ! n.isElement() ) {
104 continue;
105 }
106 KoXmlElement element = n.toElement();
107 if ( element.tagName() == "context" ) {
108 m_context = element;
109 m_contextLoaded = true;
110 }
111 }
112 return true;
113}
114
115QDomDocument Context::save( const View *view ) const {
116 QDomDocument document( "plan.context" );
117
118 document.appendChild( document.createProcessingInstruction(
119 "xml",
120 "version=\"1.0\" encoding=\"UTF-8\"" ) );
121
122 QDomElement doc = document.createElement( "context" );
123 doc.setAttribute( "editor", "Plan" );
124 doc.setAttribute( "mime", "application/x-vnd.kde.plan" );
125 doc.setAttribute( "version", 0.0 );
126 document.appendChild( doc );
127
128 QDomElement e = doc.ownerDocument().createElement("context");
129 doc.appendChild( e );
130 view->saveContext( e );
131
132 return document;
133}
134
135} //KPlato namespace
136