summaryrefslogtreecommitdiffstats
path: root/filters/kword/hancomword/hancomwordimport.cpp
blob: 561d0cc42bed3b773b912b7bf5ecee869f882cee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/* This file is part of the KDE project
   Copyright (C) 2002,2006 Ariya Hidayat <ariya@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include <config.h>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include <hancomwordimport.h>
#include <hancomwordimport.moc>

#include <tqbuffer.h>
#include <tqcstring.h>
#include <tqfile.h>
#include <tqstring.h>
#include <tqstringlist.h>
#include <tqtextstream.h>

#include <kdebug.h>
#include <KoFilterChain.h>
#include <KoGlobal.h>
#include <KoUnit.h>
#include <kgenericfactory.h>

#include <KoXmlWriter.h>

#include <iostream>
#include "pole.h"

typedef KGenericFactory<HancomWordImport, KoFilter> HancomWordImportFactory;
K_EXPORT_COMPONENT_FACTORY( libhancomwordimport, HancomWordImportFactory( "kofficefilters" ) )

class HancomWordImport::Private
{
public:
  TQString inputFile;
  TQString outputFile;
  
  TQStringList paragraphs;
  
  TQByteArray createStyles();
  TQByteArray createContent();
  TQByteArray createManifest();
};

HancomWordImport::HancomWordImport ( TQObject*, const char*, const TQStringList& )
    : KoFilter()
{
  d = new Private;
}

HancomWordImport::~HancomWordImport()
{
  delete d;
}

namespace
{

static inline unsigned long readU16( const void* p )
{
  const unsigned char* ptr = (const unsigned char*) p;
  return ptr[0]+(ptr[1]<<8);
}

}

KoFilter::ConversionStatus HancomWordImport::convert( const TQCString& from, const TQCString& to )
{
  if ( from != "application/x-hancomword" )
    return KoFilter::NotImplemented; 

  if ( to != "application/vnd.oasis.opendocument.text" )
    return KoFilter::NotImplemented;

  d->inputFile = m_chain->inputFile();
  d->outputFile = m_chain->outputFile();
  d->paragraphs.clear();

  POLE::Storage storage( d->inputFile.latin1() );
  if( !storage.open() )
    return KoFilter::WrongFormat;
  
  POLE::Stream* stream;
  stream = new POLE::Stream( &storage, "/PrvText" );
  if( stream->fail() || (stream->size() == 0) )
  {
    delete stream;
    return KoFilter::WrongFormat;
  }
  
  int len = stream->size() / 2;
  TQString plaindoc;
  plaindoc.reserve( len );
  
  unsigned char* buf = new unsigned char [stream->size()];
  stream->read( buf, stream->size());
  for(int i = 0; i < len; i++ )
    plaindoc.append( TQChar((int)readU16(buf + i*2) ) );
  delete[] buf;
  delete stream;

  // split into paragraphs
  d->paragraphs = TQStringList::split( "\n", plaindoc, true );
    
  // create output store
  KoStore* storeout;
  storeout = KoStore::createStore( d->outputFile, KoStore::Write, 
    "application/vnd.oasis.opendocument.text", KoStore::Zip );

  if ( !storeout )
  {
    kdWarning() << "Couldn't open the requested file." << endl;
    return KoFilter::FileNotFound;
  }

  if ( !storeout->open( "styles.xml" ) )
  {
    kdWarning() << "Couldn't open the file 'styles.xml'." << endl;
    return KoFilter::CreationError;
  }
  storeout->write( d->createStyles() );
  storeout->close();

  if ( !storeout->open( "content.xml" ) )
  {
    kdWarning() << "Couldn't open the file 'content.xml'." << endl;
    return KoFilter::CreationError;
  }
  storeout->write( d->createContent() );
  storeout->close();

  // store document manifest
  storeout->enterDirectory( "META-INF" );
  if ( !storeout->open( "manifest.xml" ) )
  {
     kdWarning() << "Couldn't open the file 'META-INF/manifest.xml'." << endl;
     return KoFilter::CreationError;
  }
  storeout->write( d->createManifest() );
  storeout->close();

  // we are done!
  d->inputFile = TQString();
  d->outputFile = TQString();
  delete storeout;

  return KoFilter::OK;
}

TQByteArray HancomWordImport::Private::createContent()
{
  KoXmlWriter* contentWriter;
  TQByteArray contentData;
  TQBuffer contentBuffer( contentData );

  contentBuffer.open( IO_WriteOnly );
  contentWriter = new KoXmlWriter( &contentBuffer );

  contentWriter->startDocument( "office:document-content" );
  contentWriter->startElement( "office:document-content" );
  
  contentWriter->addAttribute( "xmlns:office", "urn:oasis:names:tc:opendocument:xmlns:office:1.0" );
  contentWriter->addAttribute( "xmlns:style", "urn:oasis:names:tc:opendocument:xmlns:style:1.0" );
  contentWriter->addAttribute( "xmlns:text", "urn:oasis:names:tc:opendocument:xmlns:text:1.0" );
  contentWriter->addAttribute( "xmlns:table", "urn:oasis:names:tc:opendocument:xmlns:table:1.0" );
  contentWriter->addAttribute( "xmlns:draw", "urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" );
  contentWriter->addAttribute( "xmlns:fo", "urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" );
  contentWriter->addAttribute( "xmlns:svg","urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" );
  contentWriter->addAttribute( "office:version","1.0" );

  contentWriter->startElement( "office:automatic-styles" );
  contentWriter->endElement(); // office:automatic-style

  // office:body
  contentWriter->startElement( "office:body" );
  
  contentWriter->startElement( "office:text" );
  
  contentWriter->startElement( "text:sequence-decls" );
  contentWriter->endElement();  // text:sequence-decls

  for( uint i = 0; i < paragraphs.count(); i++ )
  {
    TQString text = paragraphs[i];
    text.replace( '\r', ' ' );
    contentWriter->startElement( "text:p" );
    contentWriter->addTextNode( text );
    contentWriter->endElement();  // text:p
  }
  
  contentWriter->endElement();  //office:text
  contentWriter->endElement();  // office:body
  
  contentWriter->endElement();  // office:document-content
  contentWriter->endDocument();
  
  delete contentWriter;

  return contentData;
}

TQByteArray HancomWordImport::Private::createStyles()
{
  KoXmlWriter* stylesWriter;
  TQByteArray stylesData;
  TQBuffer stylesBuffer( stylesData );

  stylesBuffer.open( IO_WriteOnly );
  stylesWriter = new KoXmlWriter( &stylesBuffer );

  stylesWriter->startDocument( "office:document-styles" );
  stylesWriter->startElement( "office:document-styles" );
  stylesWriter->addAttribute( "xmlns:office", "urn:oasis:names:tc:opendocument:xmlns:office:1.0" );
  stylesWriter->addAttribute( "xmlns:style", "urn:oasis:names:tc:opendocument:xmlns:style:1.0" );
  stylesWriter->addAttribute( "xmlns:text", "urn:oasis:names:tc:opendocument:xmlns:text:1.0" );
  stylesWriter->addAttribute( "xmlns:table", "urn:oasis:names:tc:opendocument:xmlns:table:1.0" );
  stylesWriter->addAttribute( "xmlns:draw", "urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" );
  stylesWriter->addAttribute( "xmlns:fo", "urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" );
  stylesWriter->addAttribute( "xmlns:svg","urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" );
  stylesWriter->addAttribute( "office:version","1.0" );
  stylesWriter->startElement( "office:styles" );
  
  // dummy default paragraph style
  stylesWriter->startElement( "style:default-style" );
  stylesWriter->addAttribute( "style:family", "paragraph" );
  
  stylesWriter->startElement( "style:paragraph-properties" );
  stylesWriter->addAttribute( "fo:hyphenation-ladder-count", "no-limit" );
  stylesWriter->addAttribute( "style:text-autospace", "ideograph-alpha" );
  stylesWriter->addAttribute( "style:punctuation-wrap", "hanging" );
  stylesWriter->addAttribute( "style:line-break", "strict" );
  stylesWriter->addAttribute( "tyle:tab-stop-distance", "0.5in" );
  stylesWriter->addAttribute( "style:writing-mode", "page" );
  stylesWriter->endElement(); // style:paragraph-properties

  stylesWriter->startElement( "style:text-properties" );
  stylesWriter->addAttribute( "style:use-window-font-color", "true" );
  stylesWriter->addAttribute( "style:font-name", "Sans Serif" );
  stylesWriter->addAttribute( "fo:font-size", "12pt" );
  stylesWriter->addAttribute( "fo:hyphenate", "false" );
  stylesWriter->endElement(); // style:text-properties
  
  stylesWriter->endElement(); // style:default-style
  stylesWriter->endElement(); // office:styles
  
  // office:automatic-styles
  stylesWriter->startElement( "office:automatic-styles" );
  stylesWriter->endElement(); // office:automatic-styles

  stylesWriter->endElement();  // office:document-styles
  stylesWriter->endDocument();
  
  delete stylesWriter;

  return stylesData;
}

TQByteArray HancomWordImport::Private::createManifest()
{
  KoXmlWriter* manifestWriter;
  TQByteArray manifestData;
  TQBuffer manifestBuffer( manifestData );

  manifestBuffer.open( IO_WriteOnly );
  manifestWriter = new KoXmlWriter( &manifestBuffer );
  
  manifestWriter->startDocument( "manifest:manifest" );
  manifestWriter->startElement( "manifest:manifest" );
  manifestWriter->addAttribute( "xmlns:manifest", "urn:oasis:names:tc:openoffice:xmlns:manifest:1.0" );
  manifestWriter->addManifestEntry( "/", "application/vnd.oasis.opendocument.text" );
  manifestWriter->addManifestEntry( "styles.xml", "text/xml" );
  manifestWriter->addManifestEntry( "content.xml", "text/xml" );
  manifestWriter->endElement();
  manifestWriter->endDocument();
  delete manifestWriter;

  return manifestData;
}