filters

qproimport.cc

00001 
00002 /* This file is part of the KDE project
00003    Copyright (C) 2001 Graham Short <grahshrt@netscape.net>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include <kdebug.h>
00022 #include <kmessagebox.h>
00023 #include <kgenericfactory.h>
00024 #include <KoFilterChain.h>
00025 
00026 #include <kspread_doc.h>
00027 #include <kspread_sheet.h>
00028 #include <kspread_cell.h>
00029 #include <kspread_map.h>
00030 
00031 #include <qproimport.h>
00032 
00033 #include <qproformula.h>
00034 #include <qpro/stream.h>
00035 #include <qpro/record_factory.h>
00036 #include <qfile.h>
00037 
00038 using namespace KSpread;
00039 
00040 typedef KGenericFactory<QpImport, KoFilter> QPROImportFactory;
00041 K_EXPORT_COMPONENT_FACTORY( libqproimport, QPROImportFactory( "kofficefilters" ) )
00042 
00043 // ---------------------------------------------------------------
00044 
00045 QpTableList::QpTableList()
00046 {
00047    for( int lIdx=0; lIdx<cNameCnt; ++lIdx )
00048    {
00049       cTable[lIdx] = 0;
00050    }
00051 }
00052 
00053 QpTableList::~QpTableList()
00054 {
00055    // don't delete the list of tables
00056 }
00057 
00058 
00059 void
00060 QpTableList::table(unsigned pIdx, Sheet* pTable)
00061 {
00062    if(pIdx < cNameCnt)
00063    {
00064       cTable[pIdx] = pTable;
00065    }
00066 }
00067 
00068 Sheet*
00069 QpTableList::table(unsigned pIdx)
00070 {
00071    return (pIdx < cNameCnt ? cTable[pIdx] : 0);
00072 }
00073 
00074 
00075 // ---------------------------------------------------------------
00076 
00077 QpImport::QpImport( KoFilter*, const char*, const QStringList& )
00078  : KoFilter()
00079 {
00080 //cout << "Hooray - in QpImport::QpImport" << endl; // ???
00081 }
00082 
00083 void
00084 QpImport::InitTableName(int pIdx, QString& pResult)
00085 {
00086    if( pIdx < 26 )
00087    {
00088       pResult = (char)('A' + pIdx);
00089    }
00090    else
00091    {
00092       pResult = (char)('A' -1 + pIdx / 26);
00093       pResult += (char)('A' + pIdx % 26);
00094    }
00095 }
00096 
00097 KoFilter::ConversionStatus QpImport::convert( const QCString& from, const QCString& to )
00098 {
00099     bool bSuccess=true;
00100 
00101     KoDocument* document = m_chain->outputDocument();
00102     if ( !document )
00103         return KoFilter::StupidError;
00104 
00105     kdDebug(30523) << "here we go... " << document->className() << endl;
00106 
00107     if( !::qt_cast<const KSpread::Doc *>( document ) )  // it's safer that way :)
00108     {
00109         kdWarning(30501) << "document isn't a KSpread::Doc but a " << document->className() << endl;
00110         return KoFilter::NotImplemented;
00111     }
00112     if(from!="application/x-quattropro" || to!="application/x-kspread")
00113     {
00114         kdWarning(30501) << "Invalid mimetypes " << from << " " << to << endl;
00115         return KoFilter::NotImplemented;
00116     }
00117 
00118     kdDebug(30523) << "...still here..." << endl;
00119 
00120     // No need for a dynamic cast here, since we use Qt's moc magic
00121     Doc *ksdoc=(Doc*)document;
00122 
00123     if(ksdoc->mimeType()!="application/x-kspread")
00124     {
00125         kdWarning(30501) << "Invalid document mimetype " << ksdoc->mimeType() << endl;
00126         return KoFilter::NotImplemented;
00127     }
00128 
00129     QpIStream lIn( QFile::encodeName(m_chain->inputFile()) );
00130 
00131     if( !lIn )
00132     {
00133         KMessageBox::sorry( 0L, i18n("QPRO filter cannot open input file - please report.") );
00134         return KoFilter::FileNotFound;
00135     }
00136 
00137     Sheet *table=0;
00138 
00139     QString field;
00140     int value=0;
00141     emit sigProgress(value);
00142 
00143    QpRecFactory            lFactory(lIn);
00144    QpTableList             lTableNames;
00145    QP_UINT8                lPageIdx = 0;
00146 
00147    QpRec*                  lRec = 0;
00148    QpRecBop*               lRecBop = 0;
00149    QpRecIntegerCell*       lRecInt = 0;
00150    QpRecFloatingPointCell* lRecFloat = 0;
00151    QpRecFormulaCell*       lRecFormula = 0;
00152    QpRecLabelCell*         lRecLabel = 0;
00153    QpRecPageName*          lRecPageName = 0;
00154 
00155    do
00156    {
00157       field = "";
00158       lRec  = lFactory.nextRecord();
00159 
00160       switch( lRec->type() )
00161       {
00162       case QpBop:
00163          lRecBop = (QpRecBop*)lRec;
00164          lPageIdx = lRecBop->pageIndex();
00165 
00166          // find out if we know about this table already, if not create it
00167          table=lTableNames.table(lPageIdx);
00168 
00169          if( table == 0 )
00170          {
00171             table=ksdoc->map()->addNewSheet();
00172             // set up a default name for the table
00173             table->setSheetName( lTableNames.name(lPageIdx)
00174                                , TRUE
00175                                );
00176             lTableNames.table(lPageIdx, table);
00177          }
00178          break;
00179 
00180       case QpIntegerCell:
00181          lRecInt = (QpRecIntegerCell*)lRec;
00182          field.setNum( lRecInt->integer() );
00183 //cout << "Setting R " << lRecInt->row()+1 << ", C " << ((unsigned)lRecInt->column()) << endl;
00184          table->setText( lRecInt->row()+1, ((unsigned)lRecInt->column())+1, field, false );
00185          break;
00186 
00187       case QpFormulaCell:
00188          lRecFormula = (QpRecFormulaCell*)lRec;
00189          {
00190            Formula lAnswer(*lRecFormula, lTableNames);
00191 
00192             char*     lFormula = lAnswer.formula();
00193 
00194             field = lFormula;
00195 
00196             delete [] lFormula;
00197          }
00198 
00199          // check for referenced tables that haven't been created yet
00200          for(unsigned lIdx=0; lIdx<lTableNames.cNameCnt; ++lIdx)
00201          {
00202             if(lTableNames.allocated(lIdx) && (lTableNames.table(lIdx) == 0) )
00203             {
00204                // we're about to reference a table that hasn't been created yet.
00205                // setText gets upset about this, so create a blank table
00206 
00207                Sheet* lNewTable=ksdoc->map()->addNewSheet();
00208 
00209                // set up a default name for the table
00210                lNewTable->setSheetName( lTableNames.name(lIdx)
00211                                       , TRUE
00212                                       );
00213                lTableNames.table(lIdx, lNewTable);
00214             }
00215          }
00216 
00217          table->setText( lRecFormula->row()+1, lRecFormula->column()+1, field, false );
00218          break;
00219 
00220       case QpFloatingPointCell:
00221          lRecFloat = (QpRecFloatingPointCell*)lRec;
00222          field.setNum( lRecFloat->value() );
00223          table->setText( lRecFloat->row()+1, lRecFloat->column()+1, field, false );
00224          break;
00225 
00226       case QpLabelCell:
00227          lRecLabel = (QpRecLabelCell*)lRec;
00228          field = "'";
00229          field += lRecLabel->label();
00230          table->setText( lRecLabel->row()+1, lRecLabel->column()+1, field, false );
00231          break;
00232 
00233       case QpPageName:
00234          lRecPageName = (QpRecPageName*)lRec;
00235 
00236          if( lTableNames.allocated(lPageIdx) && lTableNames.table(lPageIdx) )
00237          {
00238             lTableNames.table(lPageIdx)->setSheetName( lRecPageName->pageName()
00239 //                                                     , TRUE
00240                                                      );
00241             lTableNames.name(lPageIdx, lRecPageName->pageName());
00242          }
00243          break;
00244 
00245       case QpPassword:
00246         KMessageBox::sorry( 0L, i18n("Unable to open password protected files.\n"
00247                                      "The password algorithm has not been published")
00248                           );
00249         return KoFilter::NotImplemented;
00250       }
00251 
00252       delete lRec;
00253       lRec = 0;
00254    } while( lIn );
00255 
00256     emit sigProgress(100);
00257     if ( bSuccess )
00258         return KoFilter::OK;
00259     else
00260         return KoFilter::StupidError;
00261 }
00262 
00263 #include <qproimport.moc>
KDE Home | KDE Accessibility Home | Description of Access Keys