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