filters

wmlexport.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 <tqtextcodec.h>
27#include <tqfile.h>
28#include <tqfileinfo.h>
29#include <tqtextstream.h>
30
31#include <kdebug.h>
32#include <KoFilterChain.h>
33#include <kgenericfactory.h>
34
35#include <KWEFStructures.h>
36#include <KWEFBaseWorker.h>
37#include <KWEFKWordLeader.h>
38#include <KWEFUtil.h>
39
40#include "wmlexport.h"
41
42typedef KGenericFactory<WMLExport, KoFilter> WMLExportFactory;
43K_EXPORT_COMPONENT_FACTORY( libwmlexport, WMLExportFactory( "kofficefilters" ) )
44
45class WMLWorker : public KWEFBaseWorker
46{
47 public:
48 WMLWorker(void) { }
49 virtual ~WMLWorker(void) { }
50 virtual bool doOpenFile(const TQString& filenameOut, const TQString& to);
51 virtual bool doCloseFile(void);
52 virtual bool doOpenDocument(void);
53 virtual bool doCloseDocument(void);
54 virtual bool doFullParagraph(const TQString& paraText, const LayoutData& layout,
55 const ValueListFormatData& paraFormatDataList);
56 private:
57 TQString filename;
58 TQString result;
59 bool m_bold, m_italic, m_underline;
60};
61
62bool WMLWorker::doOpenFile(const TQString& filenameOut, const TQString& /*to*/)
63{
64 filename = filenameOut;
65 return true;
66}
67
68bool WMLWorker::doCloseFile(void)
69{
70 TQFile out( filename );
71 if( !out.open( IO_WriteOnly ) )
72 return false;
73 TQTextStream stream;
74 stream.setDevice( &out );
75 stream << result;
76 return true;
77}
78
79bool WMLWorker::doOpenDocument(void)
80{
81 result = "<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\"\n";
82 result += " \"http://www.wapforum.org/DTD/wml_1.1.xml\" >\n";
83 result += "<!-- Created using KWord, see www.trinitydesktop.org -->\n";
84 result += "<wml>\n";
85 result += "<card>\n";
86
87 m_bold = m_italic = m_underline = false;
88
89 return true;
90}
91
92bool WMLWorker::doCloseDocument(void)
93{
94 result += "</card>\n";
95 result += "</wml>";
96 return true;
97}
98
99bool WMLWorker::doFullParagraph(const TQString& paraText,
100 const LayoutData& layout, const ValueListFormatData& paraFormatDataList)
101{
102 TQString wmlText;
103 TQString text = paraText;
104
105 ValueListFormatData::ConstIterator it;
106 for( it = paraFormatDataList.begin(); it!=paraFormatDataList.end(); ++it )
107 {
108 const FormatData& formatData = *it;
109
110 // only if the format is for text (id==1)
111 if( formatData.id == 1 )
112 {
113 TQString partialText;
114 partialText = text.mid( formatData.pos, formatData.len );
115
116 // escape the text
117 partialText = KWEFUtil::EscapeSgmlText( NULL, partialText, true, true );
118
119 // apply formatting
120 m_bold = formatData.text.weight >= 75;
121 m_italic = formatData.text.italic;
122 m_underline = formatData.text.underline;
123
124 if( m_bold ) partialText = "<b>" + partialText + "</b>";
125 if( m_italic ) partialText = "<i>" + partialText + "</i>";
126 if( m_underline ) partialText = "<u>" + partialText + "</u>";
127
128
129 wmlText += partialText;
130 }
131 }
132
133 // sentinel check
134 TQString align = layout.alignment.lower();
135 if( ( align!="left" ) && ( align!="right" ) && ( align!="center" ) )
136 align = "left";
137
138 result += "<p align=\"" + align + "\">" + wmlText + "</p>\n";
139
140 return true;
141}
142
143WMLExport::WMLExport( KoFilter *, const char *, const TQStringList& ):
144 KoFilter()
145{
146}
147
148KoFilter::ConversionStatus WMLExport::convert( const TQCString& from,
149 const TQCString& to )
150{
151 // check for proper conversion
152 if( to!= "text/vnd.wap.wml" || from != "application/x-kword" )
153 return KoFilter::NotImplemented;
154
155 WMLWorker* worker = new WMLWorker();
156 KWEFKWordLeader* leader = new KWEFKWordLeader( worker );
157
158 KoFilter::ConversionStatus result;
159 result = leader->convert( m_chain, from, to );
160
161 delete worker;
162 delete leader;
163
164 return result;
165}
166
167#include "wmlexport.moc"
Base worker of the libexport system.
Paragraph layout.
TQString alignment
left, right, center, justify