26#include <dbaseimport.h>
27#include <dbaseimport.moc>
32#include <tqfontmetrics.h>
36#include <KoFilterChain.h>
39#include <kgenericfactory.h>
40#include <tdemessagebox.h>
42typedef KGenericFactory<DBaseImport, KoFilter> DBaseImportFactory;
43K_EXPORT_COMPONENT_FACTORY( libdbaseimport, DBaseImportFactory(
"kofficefilters" ) )
46DBaseImport::DBaseImport ( TQObject*, const
char*, const TQStringList& )
51KoFilter::ConversionStatus DBaseImport::convert(
const TQCString& from,
const TQCString& to )
53 if (to !=
"application/x-kspread" || from !=
"application/x-dbase")
54 return KoFilter::NotImplemented;
56 TQString inputFile = m_chain->inputFile();
59 bool result = dbase.load( inputFile );
61 if( dbase.version() !=3 )
63 KMessageBox::sorry( 0, i18n(
"File format is not supported.") );
64 return KoFilter::NotImplemented;
69 KMessageBox::sorry( 0, i18n(
"Could not read from file." ) );
70 return KoFilter::StupidError;
73 TQString root, documentInfo;
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";
82 root +=
"<map activeTable=\"Table1\" >\n";
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";
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";
99 TQFont font = KoGlobal::defaultFont();
102 TQFontMetrics fm( font );
103 for(
unsigned i=0; i<dbase.fields.count(); i++ )
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";
112 double h = POINT_TO_MM( 5 + fm.height() + fm.leading() );
113 for(
unsigned j=0; j<dbase.recordCount(); j++ )
115 root +=
"<row row=\"" + TQString::number(j+1) +
"\"";
116 root +=
" height=\"" + TQString::number( h ) +
"\" ><format/></row>\n";
120 for(
unsigned i=0; i<dbase.fields.count(); i++ )
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()) +
"\"" +
127 root +=
"</format>\n";
128 root +=
"<text>" + dbase.fields.at(i)->name +
"</text></cell>\n";
133 for(
unsigned j=0; j<dbase.recordCount(); j++ )
135 TQStringList rec = dbase.readRecord( j );
139 for(
unsigned i=0; i<rec.count(); i++ )
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()) +
"\"" +
147 root +=
"</format>\n";
148 root +=
"<text>" + rec[i] +
"</text></cell>\n";
155 root +=
"</table>\n";
157 root +=
"</spreadsheet>";
160 KoStoreDevice* out=m_chain->storageFile(
"root", KoStore::Write );
165 TQCString cstring = root.utf8();
166 cstring.prepend(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
167 out->writeBlock( (
const char*) cstring, cstring.length() );
171 out = m_chain->storageFile(
"documentinfo.xml", KoStore::Write );
174 TQCString cstring = documentInfo.utf8();
175 cstring.prepend(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
177 out->writeBlock( (
const char*) cstring, cstring.length() );