Warning, file /office/calligra/filters/sheets/qpro/qproimport.cc was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 
0002 /* This file is part of the KDE project
0003    SPDX-FileCopyrightText: 2001 Graham Short <grahshrt@netscape.net>
0004 
0005    SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include <qproimport.h>
0009 #include <qproformula.h>
0010 #include <qpro/stream.h>
0011 #include <qpro/record_factory.h>
0012 
0013 #include <KLocalizedString>
0014 #include <kmessagebox.h>
0015 #include <kpluginfactory.h>
0016 
0017 #include <KoFilterChain.h>
0018 
0019 #include <sheets/engine/CellBase.h>
0020 #include <sheets/engine/SheetBase.h>
0021 #include <sheets/engine/Value.h>
0022 #include <sheets/core/DocBase.h>
0023 #include <sheets/core/LoadingInfo.h>
0024 #include <sheets/core/Map.h>
0025 
0026 #include <QFile>
0027 #include <QDebug>
0028 
0029 using namespace Calligra::Sheets;
0030 
0031 Q_LOGGING_CATEGORY(lcQPro, "calligra.filter.qpro")
0032 
0033 K_PLUGIN_FACTORY_WITH_JSON(QPROImportFactory, "calligra_filter_qpro2sheets.json",
0034                            registerPlugin<QpImport>();)
0035 
0036 // ---------------------------------------------------------------
0037 
0038 QpTableList::QpTableList()
0039 {
0040     for (int lIdx = 0; lIdx < cNameCnt; ++lIdx) {
0041         cTable[lIdx] = 0;
0042     }
0043 }
0044 
0045 QpTableList::~QpTableList()
0046 {
0047     // don't delete the list of tables
0048 }
0049 
0050 
0051 void
0052 QpTableList::table(unsigned pIdx, SheetBase* pTable)
0053 {
0054     if (pIdx < cNameCnt) {
0055         cTable[pIdx] = pTable;
0056     }
0057 }
0058 
0059 SheetBase*
0060 QpTableList::table(unsigned pIdx)
0061 {
0062     return (pIdx < cNameCnt ? cTable[pIdx] : 0);
0063 }
0064 
0065 
0066 // ---------------------------------------------------------------
0067 
0068 QpImport::QpImport(QObject* parent, const QVariantList&)
0069         : KoFilter(parent)
0070 {
0071 }
0072 
0073 void
0074 QpImport::InitTableName(int pIdx, QString& pResult)
0075 {
0076     if (pIdx < 26) {
0077         pResult = (char)('A' + pIdx);
0078     } else {
0079         pResult = (char)('A' - 1 + pIdx / 26);
0080         pResult += (char)('A' + pIdx % 26);
0081     }
0082 }
0083 
0084 KoFilter::ConversionStatus QpImport::convert(const QByteArray& from, const QByteArray& to)
0085 {
0086     bool bSuccess = true;
0087 
0088     KoDocument* document = m_chain->outputDocument();
0089     if (!document)
0090         return KoFilter::StupidError;
0091 
0092     qDebug(lcQPro) << "here we go..." << document->metaObject()->className();
0093 
0094     if (!::qobject_cast<const Calligra::Sheets::DocBase *>(document)) {   // it's safer that way :)
0095         qWarning(lcQPro) << "document isn't a Calligra::Sheets::DocBase but a " << document->metaObject()->className();
0096         return KoFilter::NotImplemented;
0097     }
0098     if (from != "application/x-quattropro" || to != "application/x-kspread") {
0099         qWarning(lcQPro) << "Invalid mimetypes " << from << " " << to;
0100         return KoFilter::NotImplemented;
0101     }
0102 
0103     qDebug(lcQPro) << "...still here...";
0104     DocBase *ksdoc = dynamic_cast<DocBase *>(document);
0105 
0106     if (ksdoc->mimeType() != "application/x-ole-storage") {
0107         qWarning(lcQPro) << "Invalid document mimetype " << ksdoc->mimeType();
0108         return KoFilter::NotImplemented;
0109     }
0110 
0111     QpIStream lIn(QFile::encodeName(m_chain->inputFile()));
0112 
0113     if (!lIn) {
0114         KMessageBox::sorry(0L, i18n("QPRO filter cannot open input file - please report."));
0115         return KoFilter::FileNotFound;
0116     }
0117 
0118     SheetBase *table = 0;
0119 
0120     QString field;
0121     int value = 0;
0122     emit sigProgress(value);
0123 
0124     QpRecFactory            lFactory(lIn);
0125     QpTableList             lTableNames;
0126     QP_UINT8                lPageIdx = 0;
0127 
0128     QpRec*                  lRec = 0;
0129     QpRecBop*               lRecBop = 0;
0130     QpRecIntegerCell*       lRecInt = 0;
0131     QpRecFloatingPointCell* lRecFloat = 0;
0132     QpRecFormulaCell*       lRecFormula = 0;
0133     QpRecLabelCell*         lRecLabel = 0;
0134     QpRecPageName*          lRecPageName = 0;
0135 
0136     do {
0137         field.clear();
0138         lRec  = lFactory.nextRecord();
0139 
0140         switch (lRec->type()) {
0141         case QpBop:
0142             lRecBop = (QpRecBop*)lRec;
0143             lPageIdx = lRecBop->pageIndex();
0144 
0145             // find out if we know about this table already, if not create it
0146             table = lTableNames.table(lPageIdx);
0147 
0148             if (table == 0) {
0149                 table = ksdoc->map()->addNewSheet();
0150                 // set up a default name for the table
0151                 table->setSheetName(lTableNames.name(lPageIdx));
0152                 lTableNames.table(lPageIdx, table);
0153             }
0154             break;
0155 
0156         case QpIntegerCell:
0157             lRecInt = (QpRecIntegerCell*)lRec;
0158             field.setNum(lRecInt->integer());
0159 //cout << "Setting R " << lRecInt->row()+1 << ", C " << ((unsigned)lRecInt->column()) << endl;
0160             if (table)
0161                 setText(table, lRecInt->row() + 1, ((unsigned)lRecInt->column()) + 1, field, false);
0162             break;
0163 
0164         case QpFormulaCell:
0165             lRecFormula = (QpRecFormulaCell*)lRec;
0166             {
0167                 QuattroPro::Formula lAnswer(*lRecFormula, lTableNames);
0168 
0169                 char*     lFormula = lAnswer.formula();
0170 
0171                 field = lFormula;
0172 
0173                 delete [] lFormula;
0174             }
0175 
0176             // check for referenced tables that haven't been created yet
0177             for (unsigned lIdx = 0; lIdx < lTableNames.cNameCnt; ++lIdx) {
0178                 if (lTableNames.allocated(lIdx) && (lTableNames.table(lIdx) == 0)) {
0179                     // we're about to reference a table that hasn't been created yet.
0180                     // setText gets upset about this, so create a blank table
0181 
0182                     SheetBase* lNewTable = ksdoc->map()->addNewSheet();
0183 
0184                     // set up a default name for the table
0185                     lNewTable->setSheetName(lTableNames.name(lIdx));
0186                     lTableNames.table(lIdx, lNewTable);
0187                 }
0188             }
0189 
0190             if (table)
0191                 setText(table, lRecFormula->row() + 1, lRecFormula->column() + 1, field, false);
0192             break;
0193 
0194         case QpFloatingPointCell:
0195             lRecFloat = (QpRecFloatingPointCell*)lRec;
0196             field.setNum(lRecFloat->value());
0197             if (table)
0198                 setText(table, lRecFloat->row() + 1, lRecFloat->column() + 1, field, false);
0199             break;
0200 
0201         case QpLabelCell:
0202             lRecLabel = (QpRecLabelCell*)lRec;
0203             field = "'";
0204             field += lRecLabel->label();
0205             if (table)
0206                 setText(table, lRecLabel->row() + 1, lRecLabel->column() + 1, field, false);
0207             break;
0208 
0209         case QpPageName:
0210             lRecPageName = (QpRecPageName*)lRec;
0211 
0212             if (lTableNames.allocated(lPageIdx) && lTableNames.table(lPageIdx)) {
0213                 lTableNames.table(lPageIdx)->setSheetName(lRecPageName->pageName());
0214                 lTableNames.name(lPageIdx, lRecPageName->pageName());
0215             }
0216             break;
0217 
0218         case QpPassword:
0219             KMessageBox::sorry(0L, i18n("Unable to open password protected files.\n"
0220                                         "The password algorithm has not been published")
0221                               );
0222             delete lRec;
0223             return KoFilter::NotImplemented;
0224         }
0225 
0226         delete lRec;
0227         lRec = 0;
0228     } while (lIn);
0229 
0230     ksdoc->map()->loadingInfo()->setInitialActiveSheet(table);
0231 
0232     emit sigProgress(100);
0233     if (bSuccess)
0234         return KoFilter::OK;
0235     else
0236         return KoFilter::StupidError;
0237 }
0238 
0239 void QpImport::setText(SheetBase* sheet, int _row, int _column, const QString& _text, bool asString)
0240 {
0241     CellBase cell(sheet, _column, _row);
0242     if (asString) {
0243         cell.setUserInput(_text);
0244         cell.setValue(Value(_text));
0245     } else {
0246         cell.parseUserInput(_text);
0247     }
0248 }
0249 
0250 #include <qproimport.moc>