filters

wmlimport.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2002 Ariya Hidayat <ariyahidayat@yahoo.de>
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 <config.h>
21
22#ifdef HAVE_UNISTD_H
23#include <unistd.h>
24#endif
25
26#include <tqfileinfo.h>
27#include <tqvaluelist.h>
28#include <tqfont.h>
29
30#include <kdebug.h>
31#include <KoFilterChain.h>
32#include <kgenericfactory.h>
33#include <KoGlobal.h>
34
35#include <wmlimport.h>
36#include <wmlparser.h>
37
38typedef KGenericFactory<WMLImport, KoFilter> WMLImportFactory;
39K_EXPORT_COMPONENT_FACTORY( libwmlimport, WMLImportFactory( "kofficefilters" ) )
40
41WMLImport::WMLImport( KoFilter *, const char *, const TQStringList& ):
42 KoFilter()
43{
44}
45
46// converts WML to KWord document
47class WMLConverter: public WMLParser
48{
49 public:
50 TQString root;
51 TQString documentInfo;
52 WMLConverter();
53 virtual void parse( const char* filename );
54 virtual bool doOpenCard( TQString, TQString );
55 virtual bool doCloseCard();
56 virtual bool doParagraph( TQString text, WMLFormatList formatList,
57 WMLLayout layout );
58 private:
59 TQString m_title;
60};
61
62WMLConverter::WMLConverter()
63{
64 root = "";
65}
66
67static TQString WMLFormatAsXML( WMLFormat format )
68{
69 TQString result;
70
71 if( format.href.isEmpty() )
72 {
73 TQFont font = KoGlobal::defaultFont();
74 TQString fontFamily = font.family();
75 TQString fontSize = TQString::number(
76 format.fontsize == WMLFormat::Big ? font.pointSizeFloat()+3 :
77 format.fontsize == WMLFormat::Small ? font.pointSizeFloat()-3 : font.pointSizeFloat() );
78 TQString boldness = format.bold ? "75" : "50";
79 TQString italic = format.italic ? "1" : "0";
80 TQString underline = format.underline ? "1" : "0";
81
82 result = "<FORMAT id=\"1\" pos=\"" + TQString::number(format.pos) +
83 "\" len=\"" + TQString::number(format.len) + "\">\n";
84 result.append( " <FONT name=\"" + fontFamily + "\" />\n" );
85 result.append( " <SIZE value=\"" + fontSize + "\" />\n" );
86 result.append( " <WEIGHT value=\"" + boldness + "\" />\n" );
87 result.append( " <ITALIC value=\"" + italic + "\" />\n" );
88 result.append( " <UNDERLINE value=\"" + underline + "\" />\n" );
89 result.append( "</FORMAT>\n" );
90 }
91 else
92 {
93 // hyperlink
94 result.append( "<FORMAT id=\"4\" pos=\"" + TQString::number(format.pos) +
95 "\" len=\"" + TQString::number(format.len) + "\">\n");
96 result.append( "<VARIABLE>\n" );
97 result.append( " <TYPE key=\"STRING\" type=\"9\" text=\"" + format.link + "\" />\n" );
98 result.append( " <LINK linkName=\"" + format.link + "\" hrefName=\"" +
99 format.href + "\" />\n" );
100 result.append( "</VARIABLE>\n" );
101 result.append( "</FORMAT>\n" );
102 }
103
104
105 return result;
106}
107
108static TQString WMLLayoutAsXML( WMLLayout layout )
109{
110 TQString result;
111
112 TQString align = "left";
113 if( layout.align == WMLLayout::Center ) align = "center";
114 if( layout.align == WMLLayout::Right ) align = "right";
115
116 TQFont font = KoGlobal::defaultFont();
117 TQString fontFamily = font.family();
118 TQString fontSize = TQString::number( font.pointSizeFloat() );
119
120 result.append( "<LAYOUT>\n" );
121 result.append( " <NAME value=\"Standard\" />\n" );
122 result.append( " <FLOW align=\"" + align + "\" />\n" );
123 result.append( " <LINESPACING value=\"0\" />\n" );
124 result.append( " <LEFTBORDER width=\"0\" style=\"0\" />\n" );
125 result.append( " <RIGHTBORDER width=\"0\" style=\"0\" />\n" );
126 result.append( " <TOPBORDER width=\"0\" style=\"0\" />\n" );
127 result.append( " <BOTTOMBORDER width=\"0\" style=\"0\" />\n" );
128 result.append( " <INDENTS />\n" );
129 result.append( " <OFFSETS />\n" );
130 result.append( " <PAGEBREAKING />\n" );
131 result.append( " <COUNTER />\n" );
132 result.append( " <FORMAT id=\"1\">\n" );
133 result.append( " <WEIGHT value=\"50\" />\n" );
134 result.append( " <ITALIC value=\"0\" />\n" );
135 result.append( " <UNDERLINE value=\"0\" />\n" );
136 result.append( " <STRIKEOUT value=\"0\" />\n" );
137 result.append( " <CHARSET value=\"0\" />\n" );
138 result.append( " <VERTALIGN value=\"0\" />\n" );
139 result.append( " <FONT name=\"" + fontFamily + "\" />\n" );
140 result.append( " <SIZE value=\"" + fontSize + "\" />\n" );
141 result.append( " </FORMAT>\n" );
142 result.append( "</LAYOUT>\n" );
143
144 return result;
145}
146
147// use the first card title (or id) as document title
148bool WMLConverter::doOpenCard( TQString id, TQString title )
149{
150 if( m_title.isEmpty() )
151 m_title = ( !title.isEmpty() ) ? title : id;
152
153 return true;
154}
155
156// FIXME is this right ?
157bool WMLConverter::doCloseCard()
158{
159 // add extra paragraph between cards
160 return doParagraph( " ", WMLFormatList(), WMLLayout() );
161}
162
163bool WMLConverter::doParagraph( TQString atext, WMLFormatList formatList,
164 WMLLayout layout )
165{
166 TQString text, formats;
167
168 // encode the text for XML-ness
169 text = atext;
170 text.replace( '&', "&amp;" );
171 text.replace( '<', "&lt;" );
172 text.replace( '>', "&gt;" );
173
174 // formats, taken from formatList
175 WMLFormatList::iterator it;
176 for( it=formatList.begin(); it!=formatList.end(); ++it )
177 {
178 WMLFormat& format = *it;
179 formats.append( WMLFormatAsXML(format) );
180 }
181
182 // assemble
183 root.append( "<PARAGRAPH>\n" );
184 root.append( "<TEXT>" + text + "</TEXT>\n" );
185 root.append( "<FORMATS>" + formats + "</FORMATS>\n" );
186 root.append( WMLLayoutAsXML( layout) );
187 root.append( "</PARAGRAPH>\n" );
188
189 return true;
190}
191
192void WMLConverter::parse( const char* filename )
193{
194 WMLParser::parse( filename );
195
196 TQString prolog;
197 prolog += "<!DOCTYPE DOC>\n";
198 prolog += "<DOC mime=\"application/x-kword\" syntaxVersion=\"2\" editor=\"KWord\" >\n";
199 prolog += "<PAPER width=\"595\" spHeadBody=\"9\" format=\"1\" height=\"841\" fType=\"0\" orientation=\"0\" hType=\"0\" columnspacing=\"2\" spFootBody=\"9\" columns=\"1\" >\n";
200 prolog += "<PAPERBORDERS right=\"28\" left=\"28\" bottom=\"42\" top=\"42\" />\n";
201 prolog += "</PAPER>\n";
202 prolog += "<ATTRIBUTES hasTOC=\"0\" standardpage=\"1\" hasFooter=\"0\" hasHeader=\"0\" processing=\"0\" />\n";
203 prolog += "<FRAMESETS>\n";
204 prolog += "<FRAMESET frameType=\"1\" frameInfo=\"0\" name=\"Text Frameset 1\" visible=\"1\" >\n";
205 prolog += "<FRAME runaround=\"1\" copy=\"0\" right=\"567\" newFrameBehavior=\"0\" left=\"28\" bottom=\"799\" runaroundGap=\"2\" top=\"42\" />\n";
206
207 TQString epilog;
208 epilog = "</FRAMESET>\n";
209 epilog += "</FRAMESETS>\n";
210 epilog += "</DOC>\n";
211
212 root.prepend( prolog );
213 root.append( epilog );
214
215 // document information (only title though)
216 documentInfo = "<!DOCTYPE document-info>\n";
217 documentInfo += "<document-info>\n";
218 documentInfo += "<log><text></text></log>\n";
219 documentInfo += "<author>\n";
220 documentInfo += "<full-name></full-name>\n";
221 documentInfo += "<title></title>\n";
222 documentInfo += "<company></company>\n";
223 documentInfo += "<email></email>\n";
224 documentInfo += "<telephone></telephone>\n";
225 documentInfo += "</author>\n";
226 documentInfo += "<about>\n";
227 documentInfo += "<abstract></abstract>\n";
228 documentInfo += "<title>" + m_title + "</title>\n";
229 documentInfo += "</about>\n";
230 documentInfo += "</document-info>";
231
232}
233
234KoFilter::ConversionStatus WMLImport::convert( const TQCString& from, const TQCString& to )
235{
236 // check for proper conversion
237 if( to!= "application/x-kword" || from != "text/vnd.wap.wml" )
238 return KoFilter::NotImplemented;
239
240 // parse/convert input file
241 WMLConverter filter;
242 filter.parse( m_chain->inputFile().latin1() );
243
244 // check for error
245 // FIXME better error handling/reporting
246 if( filter.root.isEmpty() )
247 return KoFilter::StupidError;
248
249 TQString root = filter.root;
250
251 // prepare storage
252 KoStoreDevice* out=m_chain->storageFile( "root", KoStore::Write );
253
254 // store output document
255 if( out )
256 {
257 TQCString cstring = root.utf8();
258 cstring.prepend( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
259 out->writeBlock( (const char*) cstring, cstring.length() );
260 }
261
262
263 TQString documentInfo = filter.documentInfo;
264
265 // store document info
266 out = m_chain->storageFile( "documentinfo.xml", KoStore::Write );
267 if ( out )
268 {
269 TQCString cstring = documentInfo.utf8();
270 cstring.prepend( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
271
272 out->writeBlock( (const char*) cstring, cstring.length() );
273 }
274
275 return KoFilter::OK;
276}
277
278#include "wmlimport.moc"