kword13document.cpp
1/*
2 This file is part of the KDE project
3 Copyright (C) 2004 Nicolas GOUTTE <goutte@kde.org>
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21#include <tqiodevice.h>
22
23#include <kdebug.h>
24#include <tdetempfile.h>
25
26#include "kword13utils.h"
27#include "kword13layout.h"
28#include "kword13picture.h"
29#include "kword13document.h"
30
31KWord13Document::KWord13Document( void ) : m_previewFile( 0 )
32{
33 m_normalTextFramesetList.setAutoDelete( true );
34 m_tableFramesetList.setAutoDelete( true );
35 m_otherFramesetList.setAutoDelete( true );
36 m_headerFooterFramesetList.setAutoDelete( true );
37 m_footEndNoteFramesetList.setAutoDelete( true );
38 m_pictureFramesetList.setAutoDelete( true );
39 m_pictureDict.setAutoDelete( true );
40}
41
42KWord13Document::~KWord13Document( void )
43{
44 delete m_previewFile;
45}
46
47void KWord13Document::xmldump( TQIODevice* io )
48{
49 TQTextStream iostream( io );
50 iostream.setEncoding( TQTextStream::UnicodeUTF8 );
51
52 iostream << "<?xml encoding='UTF-8'?>\n";
53 iostream << "<kworddocument>\n";
54
55 for ( TQMap<TQString,TQString>::ConstIterator it = m_documentProperties.begin();
56 it != m_documentProperties.end();
57 ++it)
58 {
59 iostream << " <param key=\"" << it.key() << "\" data=\"" << EscapeXmlDump( it.data() ) << "\"/>\n";
60 }
61
62 iostream << " <documentinfo>\n";
63 for ( TQMap<TQString,TQString>::ConstIterator it11 = m_documentInfo.begin();
64 it11 != m_documentInfo.end();
65 ++it11)
66 {
67 iostream << " <param key=\"" << it11.key() << "\" data=\"" << EscapeXmlDump( it11.data() ) << "\"/>\n";
68 }
69 iostream << " </documentinfo>\n";
70
71 iostream << " <normalframesets>\n";
72 for ( KWordTextFrameset* item = m_normalTextFramesetList.first();
73 item;
74 item = m_normalTextFramesetList.next() )
75 {
76 item->xmldump( iostream );
77 }
78 iostream << " </normalframesets>\n";
79
80 iostream << " <tableframesets>\n";
81 for ( KWordTextFrameset* item12 = m_tableFramesetList.first();
82 item12;
83 item12 = m_tableFramesetList.next() )
84 {
85 item12->xmldump( iostream );
86 }
87 iostream << " </tableframesets>\n";
88
89 iostream << " <headerfooterframesets>\n";
90 for ( KWordTextFrameset* item2 = m_headerFooterFramesetList.first();
91 item2;
92 item2 = m_headerFooterFramesetList.next() )
93 {
94 item2->xmldump( iostream );
95 }
96 iostream << " </headerfooterframesets>\n";
97
98 iostream << " <footendnoteframesets>\n";
99 for ( KWordTextFrameset* item3 = m_footEndNoteFramesetList.first();
100 item3;
101 item3 = m_footEndNoteFramesetList.next() )
102 {
103 item3->xmldump( iostream );
104 }
105 iostream << " </footendnoteframesets>\n";
106
107 iostream << " <otherframesets>\n";
108 for ( KWord13Frameset* item4 = m_otherFramesetList.first();
109 item4;
110 item4 = m_otherFramesetList.next() )
111 {
112 item4->xmldump( iostream );
113 }
114 iostream << " </otherframesets>\n";
115
116 iostream << " <pictureframesets>\n";
117 for ( KWord13Frameset* item5 = m_pictureFramesetList.first();
118 item5;
119 item5 = m_pictureFramesetList.next() )
120 {
121 item5->xmldump( iostream );
122 }
123 iostream << " </pictureframesets>\n";
124
125 iostream << " <styles>\n";
126
127 for ( TQValueList<KWord13Layout>::Iterator it2 = m_styles.begin();
128 it2 != m_styles.end();
129 ++it2)
130 {
131 (*it2).xmldump( iostream );
132 }
133
134 iostream << " </styles>\n";
135
136 iostream << " <pictures>\n";
137
138 for ( TQDictIterator<KWord13Picture> it3( m_pictureDict ) ; it3.current(); ++it3 )
139 {
140 iostream << " <key>" << it3.currentKey() << "</key>" << endl;
141 }
142
143 iostream << " </pictures>\n";
144
145 iostream << "</kworddocument>\n";
146}
147
148TQString KWord13Document::getDocumentInfo( const TQString& name ) const
149{
150 TQMap<TQString,TQString>::ConstIterator it ( m_documentInfo.find( name ) );
151 if ( it == m_documentInfo.end() )
152 {
153 // Property does not exist
154 return TQString();
155 }
156 else
157 {
158 return it.data();
159 }
160}
161
162TQString KWord13Document::getProperty( const TQString& name, const TQString& oldName ) const
163{
164 const TQString result ( getPropertyInternal( name ) );
165
166 if ( result.isEmpty() && !oldName.isEmpty() )
167 {
168 // The result is empty result and we have an old name, so try the old name
169 return getPropertyInternal( oldName );
170 }
171 else
172 {
173 return result;
174 }
175}
176
177TQString KWord13Document::getPropertyInternal( const TQString& name ) const
178{
179 TQMap<TQString,TQString>::ConstIterator it ( m_documentProperties.find( name ) );
180 if ( it == m_documentProperties.end() )
181 {
182 // Property does not exist
183 return TQString();
184 }
185 else
186 {
187 return it.data();
188 }
189}
190
191TQDateTime KWord13Document::lastPrintingDate( void ) const
192{
193 const TQString strDate( getPropertyInternal( "VARIABLESETTINGS:lastPrintingDate" ) );
194
195 TQDateTime dt;
196
197 if ( strDate.isEmpty() )
198 {
199 // The printing date only exists in syntax 3, so we have no fallback.
200 kdDebug(30520) << "No syntax 3 printing date!" << endl;
201 }
202 else
203 {
204 dt = TQDateTime::fromString( strDate, TQt::ISODate );
205 }
206 return dt;
207}
208
209TQDateTime KWord13Document::creationDate( void ) const
210{
211 const TQString strDate( getPropertyInternal( "VARIABLESETTINGS:creationDate" ) );
212
213 TQDateTime dt;
214
215 if ( strDate.isEmpty() )
216 {
217 kdDebug(30520) << "No syntax 3 creation date!" << endl;
218 const int year = getPropertyInternal( "VARIABLESETTINGS:createFileYear" ).toInt();
219 const int month = getPropertyInternal( "VARIABLESETTINGS:createFileMonth" ).toInt();
220 const int day = getPropertyInternal( "VARIABLESETTINGS:createFileDay" ).toInt();
221
222 if ( TQDate::isValid( year, month, day) )
223 {
224 dt.setDate( TQDate ( year, month, day) );
225 }
226 else
227 {
228 kdDebug(30520) << "No syntax 2 creation date!" << endl;
229 }
230 }
231 else
232 {
233 dt = TQDateTime::fromString( strDate, TQt::ISODate );
234 }
235 return dt;
236}
237
238TQDateTime KWord13Document::modificationDate( void ) const
239{
240 const TQString strDate( getPropertyInternal( "VARIABLESETTINGS:modificationDate" ) );
241
242 TQDateTime dt;
243
244 if ( strDate.isEmpty() )
245 {
246 kdDebug(30520) << "No syntax 3 modification date!" << endl;
247 const int year = getPropertyInternal( "VARIABLESETTINGS:modifyFileYear" ).toInt();
248 const int month = getPropertyInternal( "VARIABLESETTINGS:modifyFileMonth" ).toInt();
249 const int day = getPropertyInternal( "VARIABLESETTINGS:modifyFileDay" ).toInt();
250 if ( TQDate::isValid( year, month, day) )
251 {
252 dt.setDate( TQDate ( year, month, day) );
253 }
254 else
255 {
256 kdDebug(30520) << "No syntax 2 modification date!" << endl;
257 }
258 }
259 else
260 {
261 dt = TQDateTime::fromString( strDate, TQt::ISODate );
262 }
263 return dt;
264}