palmdocimport.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 <tqstringlist.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 <palmdb.h>
36#include <palmdoc.h>
37
38#include "palmdocimport.h"
39#include "palmdoc.h"
40
41typedef KGenericFactory<PalmDocImport, KoFilter> PalmDocImportFactory;
42K_EXPORT_COMPONENT_FACTORY( libpalmdocimport, PalmDocImportFactory( "kofficefilters" ) )
43
44PalmDocImport::PalmDocImport( KoFilter *, const char *, const TQStringList& ):
45 KoFilter()
46{
47}
48
49KoFilter::ConversionStatus PalmDocImport::convert( const TQCString& from, const TQCString& to )
50{
51 // check for proper conversion
52 if( to!= "application/x-kword" || from != "application/vnd.palm" )
53 return KoFilter::NotImplemented;
54
55 PalmDoc doc;
56 TQString inputFile( m_chain->inputFile() );
57 doc.load( inputFile.latin1() );
58
59 if( doc.result() == PalmDoc::InvalidFormat )
60 return KoFilter::NotImplemented;
61 if( doc.result() == PalmDoc::ReadError )
62 return KoFilter::FileNotFound;
63
64 TQString root = processPlainDocument( doc.text() );
65
66 // prepare storage
67 KoStoreDevice *out=m_chain->storageFile( "root", KoStore::Write );
68
69 if( out )
70 {
71 TQCString cstring = root.utf8();
72 cstring.prepend( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
73 out->writeBlock( (const char*) cstring, cstring.length() );
74 }
75
76 TQString docTitle = doc.name();
77 if( docTitle.isEmpty() )
78 {
79 TQFileInfo info( inputFile );
80 docTitle = info.baseName();
81 }
82
83 TQString documentInfo = processDocumentInfo( docTitle );
84
85 // store document info
86 out = m_chain->storageFile( "documentinfo.xml", KoStore::Write );
87 if( out )
88 {
89 TQCString cstring = documentInfo.utf8();
90 cstring.prepend( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
91 out->writeBlock( (const char*) cstring, cstring.length() );
92 }
93
94 return KoFilter::OK;
95}
96
97TQString PalmDocImport::processPlainParagraph( TQString text )
98{
99 TQString formats, layout, result;
100
101 // specify FORMAT (just empty element)
102 formats.append ( " <FORMAT id=\"1\" pos=\"0\" len=\"" +
103 TQString::number( text.length() )+ "\">\n" );
104 formats.append ( " </FORMAT>\n" );
105
106 TQFont font = KoGlobal::defaultFont();
107 TQString fontFamily = font.family();
108 double fontSize = font.pointSizeFloat();
109
110 // default LAYOUT
111 layout.append( "<LAYOUT>\n" );
112 layout.append( " <NAME value=\"Standard\" />\n" );
113 layout.append( " <FLOW align=\"left\" />\n" );
114 layout.append( " <LINESPACING value=\"0\" />\n" );
115 layout.append( " <LEFTBORDER width=\"0\" style=\"0\" />\n" );
116 layout.append( " <RIGHTBORDER width=\"0\" style=\"0\" />\n" );
117 layout.append( " <TOPBORDER width=\"0\" style=\"0\" />\n" );
118 layout.append( " <BOTTOMBORDER width=\"0\" style=\"0\" />\n" );
119 layout.append( " <INDENTS />\n" );
120 layout.append( " <OFFSETS after=\"9\" />\n" );
121 layout.append( " <PAGEBREAKING />\n" );
122 layout.append( " <COUNTER />\n" );
123 layout.append( " <FORMAT id=\"1\">\n" );
124 layout.append( " <SIZE value=\"" + TQString::number( fontSize ) + "\" />\n" );
125 layout.append( " <WEIGHT value=\"50\" />\n" );
126 layout.append( " <ITALIC value=\"0\" />\n" );
127 layout.append( " <UNDERLINE value=\"0\" />\n" );
128 layout.append( " <STRIKEOUT value=\"0\" />\n" );
129 layout.append( " <CHARSET value=\"0\" />\n" );
130 layout.append( " <VERTALIGN value=\"0\" />\n" );
131 layout.append( " <FONT name=\"" + fontFamily + "\" />\n" );
132 layout.append( " </FORMAT>\n" );
133 layout.append( "</LAYOUT>\n" );
134
135 // encode text for XML-ness
136 text.replace( '&', "&amp;" );
137 text.replace( '<', "&lt;" );
138 text.replace( '>', "&gt;" );
139 text.replace( '"', "&quot;" );
140 text.replace( '\'', "&apos;" );
141
142 // construct the <PARAGRAPH>
143 result.append( "<PARAGRAPH>\n" );
144 result.append( "<TEXT>" + text + "</TEXT>\n" );
145 result.append( "<FORMATS>\n");
146 result.append( formats );
147 result.append( "</FORMATS>\n");
148 result.append( layout );
149 result.append( "</PARAGRAPH>\n" );
150
151 return result;
152}
153
154TQString PalmDocImport::processPlainDocument( TQString plaindoc )
155{
156 TQString prolog, content, epilog;
157 TQStringList paragraphs;
158
159 paragraphs = TQStringList::split( "\n\n", plaindoc, true );
160 for( unsigned int i = 0; i < paragraphs.count(); i++ )
161 {
162 TQString text = paragraphs[i];
163 text.replace( '\n', ' ' );
164 content.append( processPlainParagraph( text ) );
165 }
166
167 prolog = "<!DOCTYPE DOC>\n";
168 prolog.append( "<DOC mime=\"application/x-kword\" syntaxVersion=\"2\" editor=\"KWord\">\n");
169
170 prolog.append( "<PAPER width=\"595\" height=\"841\" format=\"1\" fType=\"0\" orientation=\"0\" hType=\"0\" columns=\"1\">\n" );
171 prolog.append( " <PAPERBORDERS left=\"36\" right=\"36\" top=\"36\" bottom=\"36\" />\n" );
172 prolog.append( "</PAPER>\n" );
173
174 prolog.append( "<ATTRIBUTES standardpage=\"1\" hasFooter=\"0\" hasHeader=\"0\" processing=\"0\" />\n" );
175
176 prolog.append( "<FRAMESETS>\n" );
177 prolog.append( "<FRAMESET removable=\"0\" frameType=\"1\" frameInfo=\"0\" autoCreateNewFrame=\"1\">\n" );
178 prolog.append( "<FRAME right=\"567\" left=\"28\" top=\"42\" bottom=\"799\" />\n" );
179
180 epilog = "</FRAMESET>\n";
181 epilog.append( "</FRAMESETS>\n" );
182 epilog.append( "</DOC>\n" );
183
184 return prolog + content + epilog;
185}
186
187TQString PalmDocImport::processDocumentInfo( const TQString &title )
188{
189 TQString documentInfo;
190
191 documentInfo = "<!DOCTYPE document-info>\n";
192
193 documentInfo += "<document-info>\n";
194 documentInfo += "<log><text></text></log>\n";
195
196 documentInfo += "<author>\n";
197 documentInfo += "<full-name></full-name>\n";
198 documentInfo += "<title></title>\n";
199 documentInfo += "<company></company>\n";
200 documentInfo += "<email></email>\n";
201 documentInfo += "<telephone></telephone>\n";
202 documentInfo += "</author>\n";
203
204 documentInfo += "<about>\n";
205 documentInfo += "<abstract></abstract>\n";
206 documentInfo += "<title>" + title + "</title>\n";
207 documentInfo += "</about>\n";
208
209 documentInfo += "</document-info>";
210
211 return documentInfo;
212}
213
214#include "palmdocimport.moc"