summaryrefslogtreecommitdiffstats
path: root/filters/kword/palmdoc/palmdocimport.cpp
blob: a1e8107bcc603c7f87e12f44c655792ebaa3163b (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
/* This file is part of the KDE project
   Copyright (C) 2002 Ariya Hidayat <ariyahidayat@yahoo.de>

   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 <tqfileinfo.h>
#include <tqstringlist.h>
#include <tqfont.h>

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

#include <palmdb.h>
#include <palmdoc.h>

#include "palmdocimport.h"
#include "palmdoc.h"

typedef KGenericFactory<PalmDocImport, KoFilter> PalmDocImportFactory;
K_EXPORT_COMPONENT_FACTORY( libpalmdocimport, PalmDocImportFactory( "kofficefilters" ) )

PalmDocImport::PalmDocImport( KoFilter *, const char *, const TQStringList& ):
                     KoFilter()
{
}

KoFilter::ConversionStatus PalmDocImport::convert( const TQCString& from, const TQCString& to )
{
  // check for proper conversion
  if( to!= "application/x-kword" || from != "application/vnd.palm" )
     return KoFilter::NotImplemented;

  PalmDoc doc;
  TQString inputFile( m_chain->inputFile() );
  doc.load( inputFile.latin1() );

  if( doc.result() == PalmDoc::InvalidFormat )
    return KoFilter::NotImplemented;
  if( doc.result() == PalmDoc::ReadError )
    return KoFilter::FileNotFound;

  TQString root = processPlainDocument( doc.text() );

  // prepare storage
  KoStoreDevice *out=m_chain->storageFile( "root", KoStore::Write );

  if( out )
    {
      TQCString cstring = root.utf8();
      cstring.prepend( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
      out->writeBlock( (const char*) cstring, cstring.length() );
    }

  TQString docTitle = doc.name();
  if( docTitle.isEmpty() )
  {
    TQFileInfo info( inputFile );
    docTitle = info.baseName();
  }

  TQString documentInfo = processDocumentInfo( docTitle );

  // store document info
  out = m_chain->storageFile( "documentinfo.xml", KoStore::Write );
  if( out )
    {
       TQCString cstring = documentInfo.utf8();
       cstring.prepend( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
       out->writeBlock( (const char*) cstring, cstring.length() );
     }

  return KoFilter::OK;
}

TQString PalmDocImport::processPlainParagraph( TQString text )
{
  TQString formats, layout, result;

  // specify FORMAT (just empty element)
  formats.append ( "  <FORMAT id=\"1\" pos=\"0\" len=\"" +
    TQString::number( text.length() )+ "\">\n" );
  formats.append ( "  </FORMAT>\n" );

  TQFont font = KoGlobal::defaultFont();
  TQString fontFamily = font.family();
  double fontSize = font.pointSizeFloat();

  // default LAYOUT
  layout.append( "<LAYOUT>\n" );
  layout.append( "  <NAME value=\"Standard\" />\n" );
  layout.append( "  <FLOW align=\"left\" />\n" );
  layout.append( "  <LINESPACING value=\"0\" />\n" );
  layout.append( "  <LEFTBORDER width=\"0\" style=\"0\" />\n" );
  layout.append( "  <RIGHTBORDER width=\"0\" style=\"0\" />\n" );
  layout.append( "  <TOPBORDER width=\"0\" style=\"0\" />\n" );
  layout.append( "  <BOTTOMBORDER width=\"0\" style=\"0\" />\n" );
  layout.append( "  <INDENTS />\n" );
  layout.append( "  <OFFSETS after=\"9\" />\n" );
  layout.append( "  <PAGEBREAKING />\n" );
  layout.append( "  <COUNTER />\n" );
  layout.append( "  <FORMAT id=\"1\">\n" );
  layout.append( "    <SIZE value=\"" + TQString::number( fontSize ) + "\" />\n" );
  layout.append( "    <WEIGHT value=\"50\" />\n" );
  layout.append( "    <ITALIC value=\"0\" />\n" );
  layout.append( "    <UNDERLINE value=\"0\" />\n" );
  layout.append( "    <STRIKEOUT value=\"0\" />\n" );
  layout.append( "    <CHARSET value=\"0\" />\n" );
  layout.append( "    <VERTALIGN value=\"0\" />\n" );
  layout.append( "    <FONT name=\"" + fontFamily + "\" />\n" );
  layout.append( "  </FORMAT>\n" );
  layout.append( "</LAYOUT>\n" );

  // encode text for XML-ness
  text.replace( '&', "&amp;" );
  text.replace( '<', "&lt;" );
  text.replace( '>', "&gt;" );
  text.replace( '"', "&quot;" );
  text.replace( '\'', "&apos;" );

  // construct the <PARAGRAPH>
  result.append( "<PARAGRAPH>\n" );
  result.append( "<TEXT>" + text + "</TEXT>\n" );
  result.append( "<FORMATS>\n");
  result.append( formats );
  result.append( "</FORMATS>\n");
  result.append( layout );
  result.append( "</PARAGRAPH>\n" );

  return result;
}

TQString PalmDocImport::processPlainDocument( TQString plaindoc )
{
  TQString prolog, content, epilog;
  TQStringList paragraphs;

  paragraphs = TQStringList::split( "\n\n", plaindoc, TRUE );
  for( unsigned int i = 0; i < paragraphs.count(); i++ )
  {
      TQString text = paragraphs[i];
      text.replace( '\n', ' ' );
      content.append( processPlainParagraph( text ) );
  }

  prolog = "<!DOCTYPE DOC>\n";
  prolog.append( "<DOC mime=\"application/x-kword\" syntaxVersion=\"2\" editor=\"KWord\">\n");

  prolog.append( "<PAPER width=\"595\" height=\"841\" format=\"1\" fType=\"0\" orientation=\"0\" hType=\"0\" columns=\"1\">\n" );
  prolog.append( " <PAPERBORDERS left=\"36\" right=\"36\" top=\"36\" bottom=\"36\" />\n" );
  prolog.append( "</PAPER>\n" );

  prolog.append( "<ATTRIBUTES standardpage=\"1\" hasFooter=\"0\" hasHeader=\"0\" processing=\"0\" />\n" );

  prolog.append( "<FRAMESETS>\n" );
  prolog.append( "<FRAMESET removable=\"0\" frameType=\"1\" frameInfo=\"0\" autoCreateNewFrame=\"1\">\n" );
  prolog.append( "<FRAME right=\"567\" left=\"28\" top=\"42\" bottom=\"799\" />\n" );

  epilog = "</FRAMESET>\n";
  epilog.append( "</FRAMESETS>\n" );
  epilog.append( "</DOC>\n" );

  return prolog + content + epilog;
}

TQString PalmDocImport::processDocumentInfo( const TQString &title )
{
  TQString documentInfo;

  documentInfo = "<!DOCTYPE document-info>\n";

  documentInfo += "<document-info>\n";
  documentInfo += "<log><text></text></log>\n";

  documentInfo += "<author>\n";
  documentInfo += "<full-name></full-name>\n";
  documentInfo += "<title></title>\n";
  documentInfo += "<company></company>\n";
  documentInfo += "<email></email>\n";
  documentInfo += "<telephone></telephone>\n";
  documentInfo += "</author>\n";

  documentInfo += "<about>\n";
  documentInfo += "<abstract></abstract>\n";
  documentInfo += "<title>" + title + "</title>\n";
  documentInfo += "</about>\n";

  documentInfo += "</document-info>";

  return documentInfo;
}

#include "palmdocimport.moc"