dbaseimport.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 <dbaseimport.h>
27#include <dbaseimport.moc>
28#include <dbase.h>
29
30#include <tqfile.h>
31#include <tqfont.h>
32#include <tqfontmetrics.h>
33#include <tqstring.h>
34
35#include <kdebug.h>
36#include <KoFilterChain.h>
37#include <KoGlobal.h>
38#include <KoUnit.h>
39#include <kgenericfactory.h>
40#include <tdemessagebox.h>
41
42typedef KGenericFactory<DBaseImport, KoFilter> DBaseImportFactory;
43K_EXPORT_COMPONENT_FACTORY( libdbaseimport, DBaseImportFactory( "kofficefilters" ) )
44
45
46DBaseImport::DBaseImport ( TQObject*, const char*, const TQStringList& )
47 : KoFilter()
48{
49}
50
51KoFilter::ConversionStatus DBaseImport::convert( const TQCString& from, const TQCString& to )
52{
53 if (to != "application/x-kspread" || from != "application/x-dbase")
54 return KoFilter::NotImplemented;
55
56 TQString inputFile = m_chain->inputFile();
57
58 DBase dbase;
59 bool result = dbase.load( inputFile );
60
61 if( dbase.version() !=3 )
62 {
63 KMessageBox::sorry( 0, i18n("File format is not supported.") );
64 return KoFilter::NotImplemented;
65 }
66
67 if( !result )
68 {
69 KMessageBox::sorry( 0, i18n("Could not read from file." ) );
70 return KoFilter::StupidError;
71 }
72
73 TQString root, documentInfo;
74
75 root = "<!DOCTYPE spreadsheet >\n";
76 root += "<spreadsheet mime=\"application/x-kspread\" editor=\"KSpread\" >\n";
77 root += "<paper format=\"A4\" orientation=\"Portrait\" >\n";
78 root += "<borders right=\"20\" left=\"20\" bottom=\"20\" top=\"20\" />\n";
79 root += "<head/>\n";
80 root += "<foot/>\n";
81 root += "</paper>\n";
82 root += "<map activeTable=\"Table1\" >\n";
83
84 root += "<locale positivePrefixCurrencySymbol=\"True\"";
85 root += " negativeMonetarySignPosition=\"0\"";
86 root += " negativePrefixCurrencySymbol=\"True\" fracDigits=\"2\"";
87 root += " thousandsSeparator=\",\" dateFormat=\"%A %d %B %Y\"";
88 root += " timeFormat=\"%H:%M:%S\" monetaryDecimalSymbol=\".\"";
89 root += " weekStartsMonday=\"True\" currencySymbol=\"$\"";
90 root += " negativeSign=\"-\" positiveSign=\"\"";
91 root += " positiveMonetarySignPosition=\"1\" decimalSymbol=\".\"";
92 root += " monetaryThousandsSeparator=\",\" dateFormatShort=\"%Y-%m-%d\" />\n";
93
94 root += "<table name=\"Table1\" columnnumber=\"0\" borders=\"0\"";
95 root += " hide=\"0\" hidezero=\"0\" firstletterupper=\"0\" grid=\"1\"";
96 root += " formular=\"0\" lcmode=\"0\" >\n";
97
98 // KOffice default font
99 TQFont font = KoGlobal::defaultFont();
100
101 // define columns
102 TQFontMetrics fm( font );
103 for( unsigned i=0; i<dbase.fields.count(); i++ )
104 {
105 int mw = TQMAX( dbase.fields.at(i)->length, dbase.fields.at(i)->name.length());
106 double w = POINT_TO_MM( fm.maxWidth() * mw );
107 root += "<column column=\"" + TQString::number(i+1) + "\"";
108 root += " width=\"" + TQString::number( w ) + "\"><format/></column>\n";
109 }
110
111 // define rows
112 double h = POINT_TO_MM( 5 + fm.height() + fm.leading() );
113 for( unsigned j=0; j<dbase.recordCount(); j++ )
114 {
115 root += "<row row=\"" + TQString::number(j+1) + "\"";
116 root += " height=\"" + TQString::number( h ) + "\" ><format/></row>\n";
117 }
118
119 // field names come as first row
120 for( unsigned i=0; i<dbase.fields.count(); i++ )
121 {
122 root += "<cell row=\"1\" column=\"" + TQString::number(i+1) + "\" >\n";
123 root += "<format><pen width=\"0\" style=\"1\" color=\"#000000\" />";
124 root += "<font family=\"" + font.family() + "\"" +
125 " size=\"" + TQString::number(font.pointSizeFloat()) + "\"" +
126 " weight=\"50\" />";
127 root += "</format>\n";
128 root += "<text>" + dbase.fields.at(i)->name + "</text></cell>\n";
129 }
130
131 // process all records
132 unsigned row = 1;
133 for( unsigned j=0; j<dbase.recordCount(); j++ )
134 {
135 TQStringList rec = dbase.readRecord( j );
136 if( rec.count() )
137 {
138 row++;
139 for( unsigned i=0; i<rec.count(); i++ )
140 {
141 root += "<cell row=\"" + TQString::number(row) + "\"" +
142 "column=\"" + TQString::number(i+1) + "\" >\n";
143 root += "<format><pen width=\"0\" style=\"1\" color=\"#000000\" />";
144 root += "<font family=\"" + font.family() + "\"" +
145 " size=\"" + TQString::number(font.pointSizeFloat()) + "\"" +
146 " weight=\"50\" />";
147 root += "</format>\n";
148 root += "<text>" + rec[i] + "</text></cell>\n";
149 }
150 }
151 }
152
153 dbase.close();
154
155 root += "</table>\n";
156 root += "</map>\n";
157 root += "</spreadsheet>";
158
159 // prepare storage
160 KoStoreDevice* out=m_chain->storageFile( "root", KoStore::Write );
161
162 // store output document
163 if( out )
164 {
165 TQCString cstring = root.utf8();
166 cstring.prepend( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
167 out->writeBlock( (const char*) cstring, cstring.length() );
168 }
169
170 // store document info
171 out = m_chain->storageFile( "documentinfo.xml", KoStore::Write );
172 if ( out )
173 {
174 TQCString cstring = documentInfo.utf8();
175 cstring.prepend( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
176
177 out->writeBlock( (const char*) cstring, cstring.length() );
178 }
179
180 return KoFilter::OK;
181}