filters
dbaseimport.cc00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <config.h>
00021
00022 #ifdef HAVE_UNISTD_H
00023 #include <unistd.h>
00024 #endif
00025
00026 #include <dbaseimport.h>
00027 #include <dbaseimport.moc>
00028 #include <dbase.h>
00029
00030 #include <qfile.h>
00031 #include <qfont.h>
00032 #include <qfontmetrics.h>
00033 #include <qstring.h>
00034
00035 #include <kdebug.h>
00036 #include <KoFilterChain.h>
00037 #include <KoGlobal.h>
00038 #include <KoUnit.h>
00039 #include <kgenericfactory.h>
00040 #include <kmessagebox.h>
00041
00042 typedef KGenericFactory<DBaseImport, KoFilter> DBaseImportFactory;
00043 K_EXPORT_COMPONENT_FACTORY( libdbaseimport, DBaseImportFactory( "kofficefilters" ) )
00044
00045
00046 DBaseImport::DBaseImport ( QObject*, const char*, const QStringList& )
00047 : KoFilter()
00048 {
00049 }
00050
00051 KoFilter::ConversionStatus DBaseImport::convert( const QCString& from, const QCString& to )
00052 {
00053 if (to != "application/x-kspread" || from != "application/x-dbase")
00054 return KoFilter::NotImplemented;
00055
00056 QString inputFile = m_chain->inputFile();
00057
00058 DBase dbase;
00059 bool result = dbase.load( inputFile );
00060
00061 if( dbase.version() !=3 )
00062 {
00063 KMessageBox::sorry( 0, i18n("File format is not supported.") );
00064 return KoFilter::NotImplemented;
00065 }
00066
00067 if( !result )
00068 {
00069 KMessageBox::sorry( 0, i18n("Could not read from file." ) );
00070 return KoFilter::StupidError;
00071 }
00072
00073 QString root, documentInfo;
00074
00075 root = "<!DOCTYPE spreadsheet >\n";
00076 root += "<spreadsheet mime=\"application/x-kspread\" editor=\"KSpread\" >\n";
00077 root += "<paper format=\"A4\" orientation=\"Portrait\" >\n";
00078 root += "<borders right=\"20\" left=\"20\" bottom=\"20\" top=\"20\" />\n";
00079 root += "<head/>\n";
00080 root += "<foot/>\n";
00081 root += "</paper>\n";
00082 root += "<map activeTable=\"Table1\" >\n";
00083
00084 root += "<locale positivePrefixCurrencySymbol=\"True\"";
00085 root += " negativeMonetarySignPosition=\"0\"";
00086 root += " negativePrefixCurrencySymbol=\"True\" fracDigits=\"2\"";
00087 root += " thousandsSeparator=\",\" dateFormat=\"%A %d %B %Y\"";
00088 root += " timeFormat=\"%H:%M:%S\" monetaryDecimalSymbol=\".\"";
00089 root += " weekStartsMonday=\"True\" currencySymbol=\"$\"";
00090 root += " negativeSign=\"-\" positiveSign=\"\"";
00091 root += " positiveMonetarySignPosition=\"1\" decimalSymbol=\".\"";
00092 root += " monetaryThousandsSeparator=\",\" dateFormatShort=\"%Y-%m-%d\" />\n";
00093
00094 root += "<table name=\"Table1\" columnnumber=\"0\" borders=\"0\"";
00095 root += " hide=\"0\" hidezero=\"0\" firstletterupper=\"0\" grid=\"1\"";
00096 root += " formular=\"0\" lcmode=\"0\" >\n";
00097
00098
00099 QFont font = KoGlobal::defaultFont();
00100
00101
00102 QFontMetrics fm( font );
00103 for( unsigned i=0; i<dbase.fields.count(); i++ )
00104 {
00105 int mw = QMAX( dbase.fields.at(i)->length, dbase.fields.at(i)->name.length());
00106 double w = POINT_TO_MM( fm.maxWidth() * mw );
00107 root += "<column column=\"" + QString::number(i+1) + "\"";
00108 root += " width=\"" + QString::number( w ) + "\"><format/></column>\n";
00109 }
00110
00111
00112 double h = POINT_TO_MM( 5 + fm.height() + fm.leading() );
00113 for( unsigned j=0; j<dbase.recordCount(); j++ )
00114 {
00115 root += "<row row=\"" + QString::number(j+1) + "\"";
00116 root += " height=\"" + QString::number( h ) + "\" ><format/></row>\n";
00117 }
00118
00119
00120 for( unsigned i=0; i<dbase.fields.count(); i++ )
00121 {
00122 root += "<cell row=\"1\" column=\"" + QString::number(i+1) + "\" >\n";
00123 root += "<format><pen width=\"0\" style=\"1\" color=\"#000000\" />";
00124 root += "<font family=\"" + font.family() + "\"" +
00125 " size=\"" + QString::number(font.pointSizeFloat()) + "\"" +
00126 " weight=\"50\" />";
00127 root += "</format>\n";
00128 root += "<text>" + dbase.fields.at(i)->name + "</text></cell>\n";
00129 }
00130
00131
00132 unsigned row = 1;
00133 for( unsigned j=0; j<dbase.recordCount(); j++ )
00134 {
00135 QStringList rec = dbase.readRecord( j );
00136 if( rec.count() )
00137 {
00138 row++;
00139 for( unsigned i=0; i<rec.count(); i++ )
00140 {
00141 root += "<cell row=\"" + QString::number(row) + "\"" +
00142 "column=\"" + QString::number(i+1) + "\" >\n";
00143 root += "<format><pen width=\"0\" style=\"1\" color=\"#000000\" />";
00144 root += "<font family=\"" + font.family() + "\"" +
00145 " size=\"" + QString::number(font.pointSizeFloat()) + "\"" +
00146 " weight=\"50\" />";
00147 root += "</format>\n";
00148 root += "<text>" + rec[i] + "</text></cell>\n";
00149 }
00150 }
00151 }
00152
00153 dbase.close();
00154
00155 root += "</table>\n";
00156 root += "</map>\n";
00157 root += "</spreadsheet>";
00158
00159
00160 KoStoreDevice* out=m_chain->storageFile( "root", KoStore::Write );
00161
00162
00163 if( out )
00164 {
00165 QCString cstring = root.utf8();
00166 cstring.prepend( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
00167 out->writeBlock( (const char*) cstring, cstring.length() );
00168 }
00169
00170
00171 out = m_chain->storageFile( "documentinfo.xml", KoStore::Write );
00172 if ( out )
00173 {
00174 QCString cstring = documentInfo.utf8();
00175 cstring.prepend( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
00176
00177 out->writeBlock( (const char*) cstring, cstring.length() );
00178 }
00179
00180 return KoFilter::OK;
00181 }
|