filters

dbaseimport.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2002 Ariya Hidayat <ariyahidayat@yahoo.de>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
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   // KOffice default font
00099   QFont font = KoGlobal::defaultFont();
00100 
00101   // define columns
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   // define rows
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   // field names come as first row
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   // process all records
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   // prepare storage
00160   KoStoreDevice* out=m_chain->storageFile( "root", KoStore::Write );
00161 
00162   // store output document
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   // store document info
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 }
KDE Home | KDE Accessibility Home | Description of Access Keys