amiproexport.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 <amiproexport.h>
41
42typedef KGenericFactory<AmiProExport, KoFilter> AmiProExportFactory;
43K_EXPORT_COMPONENT_FACTORY( libamiproexport, AmiProExportFactory( "kofficefilters" ) )
44class AmiProWorker : public KWEFBaseWorker
45{
46 public:
47 AmiProWorker(void) { }
48 virtual ~AmiProWorker(void) { }
49 virtual bool doOpenFile(const TQString& filenameOut, const TQString& to);
50 virtual bool doCloseFile(void);
51 virtual bool doOpenDocument(void);
52 virtual bool doCloseDocument(void);
53 virtual bool doFullParagraph(const TQString& paraText, const LayoutData& layout,
54 const ValueListFormatData& paraFormatDataList);
55 private:
56 TQString filename;
57 TQString result;
58 bool m_bold, m_italic, m_underline, m_underlineDouble;
59 bool m_strike, m_subscript, m_superscript;
60};
61
62bool AmiProWorker::doOpenFile(const TQString& filenameOut, const TQString& /*to*/)
63{
64 filename = filenameOut;
65
66 return true;
67}
68
69bool AmiProWorker::doCloseFile(void)
70{
71 TQFile out( filename );
72 if( !out.open( IO_WriteOnly ) )
73 return false;
74 TQTextStream stream;
75 stream.setDevice( &out );
76 stream << result;
77 return true;
78}
79
80bool AmiProWorker::doOpenDocument(void)
81{
82 result = "[ver]\n\t4\n";
83 result += "[sty]\n\t\n";
84 result += "[lay]\n";
85 result += "\tStandard\n";
86 result += "\t516\n";
87 result += "\t[rght]\n";
88
89 // don't ask me, I have no idea what these numbers are
90 int magic[] = { 16833, 11908, 1, 1440, 1440, 1, 1440, 1440,
91 0, 1, 0, 1, 0, 2, 1, 1440, 10465, 12, 1, 720, 1, 1440,
92 1, 2160, 1, 2880, 1, 3600, 1, 4320, 1, 5040, 1, 5760,
93 1, 6480, 1, 7200, 1, 7920, 1, 8640 };
94 for( uint i=0; i<sizeof(magic)/sizeof(magic[0]); i++ )
95 result += "\t\t" + TQString::number(magic[i]) + "\n";
96
97 result += "[elay]\n";
98 result += "[edoc]\n";
99
100 m_bold = m_italic = m_underline = m_underlineDouble =
101 m_strike = m_subscript = m_superscript = false;
102
103 return true;
104}
105
106bool AmiProWorker::doCloseDocument(void)
107{
108 result += ">\n\n";
109 return true;
110}
111
112static TQString AmiProEscape( const TQString& text )
113{
114 TQString result;
115
116 for( unsigned i=0; i<text.length(); i++ )
117 {
118 TQChar ch = text[i];
119 switch( ch.unicode() )
120 {
121 case '<': result += "<<"; break;
122 case '>': result += "<;>"; break;
123 case '[': result += "<[>"; break;
124 case '@': result += "@@"; break;
125 case '\'': result += "</R>"; break;
126 default: result += ch; break;
127 }
128 }
129
130 return result;
131}
132
133bool AmiProWorker::doFullParagraph(const TQString& paraText,
134 const LayoutData& /*layout*/, const ValueListFormatData& paraFormatDataList)
135{
136 TQString amiproText = "";
137 TQString text = paraText;
138
139 ValueListFormatData::ConstIterator it;
140 for( it = paraFormatDataList.begin(); it!=paraFormatDataList.end(); ++it )
141 {
142 const FormatData& formatData = *it;
143
144 // only if the format is for text (id==1)
145 if( formatData.id == 1 )
146 {
147 TQString partialText;
148 partialText = text.mid( formatData.pos, formatData.len );
149
150 partialText = AmiProEscape( partialText );
151
152 // apply formatting
153 m_bold = formatData.text.weight >= 75;
154 m_italic = formatData.text.italic;
155 m_underline = formatData.text.underline;
156 m_underlineDouble = formatData.text.underlineValue == "double";
157 m_subscript = formatData.text.verticalAlignment == 1;
158 m_superscript = formatData.text.verticalAlignment == 2;
159 m_strike = formatData.text.strikeout;
160
161 if( m_bold ) partialText = "<+!>" + partialText + "<-!>";
162 if( m_italic ) partialText = "<+\">" + partialText + "<-\">";
163 if( m_underline && !m_underlineDouble ) partialText = "<+#>" + partialText + "<-#>";
164 if( m_underlineDouble ) partialText = "<+)>" + partialText + "<-)>";
165 if( m_subscript ) partialText = "<+'>" + partialText + "<-'>";
166 if( m_superscript ) partialText = "<+&>" + partialText + "<-&>";
167 if( m_strike) partialText = "<+%>" + partialText + "<-%>";
168
169 amiproText += partialText;
170 }
171 }
172
173 result += amiproText + "\n\n";
174
175 return true;
176}
177
178AmiProExport::AmiProExport( KoFilter *, const char *, const TQStringList& ):
179 KoFilter()
180{
181}
182
183KoFilter::ConversionStatus
184AmiProExport::convert( const TQCString& from,
185 const TQCString& to )
186{
187 // check for proper conversion
188 if( to!= "application/x-amipro" || from != "application/x-kword" )
189 return KoFilter::NotImplemented;
190
191 AmiProWorker* worker = new AmiProWorker();
192 KWEFKWordLeader* leader = new KWEFKWordLeader( worker );
193
194 KoFilter::ConversionStatus result;
195 result = leader->convert( m_chain, from, to );
196
197 delete worker;
198 delete leader;
199
200 return result;
201}
202
203#include "amiproexport.moc"
Base worker of the libexport system.
Paragraph layout.