qproimport.cpp
1
2/* This file is part of the KDE project
3 Copyright (C) 2001 Graham Short <grahshrt@netscape.net>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21#include <kdebug.h>
22#include <tdemessagebox.h>
23#include <kgenericfactory.h>
24#include <KoFilterChain.h>
25
26#include <kspread_doc.h>
27#include <kspread_sheet.h>
28#include <kspread_cell.h>
29#include <kspread_map.h>
30
31#include <qproimport.h>
32
33#include <qproformula.h>
34#include <qpro/stream.h>
35#include <qpro/record_factory.h>
36#include <tqfile.h>
37
38using namespace KSpread;
39
40typedef KGenericFactory<QpImport, KoFilter> TQPROImportFactory;
41K_EXPORT_COMPONENT_FACTORY( libqproimport, TQPROImportFactory( "kofficefilters" ) )
42
43// ---------------------------------------------------------------
44
45QpTableList::QpTableList()
46{
47 for( int lIdx=0; lIdx<cNameCnt; ++lIdx )
48 {
49 cTable[lIdx] = 0;
50 }
51}
52
53QpTableList::~QpTableList()
54{
55 // don't delete the list of tables
56}
57
58
59void
60QpTableList::table(unsigned pIdx, Sheet* pTable)
61{
62 if(pIdx < cNameCnt)
63 {
64 cTable[pIdx] = pTable;
65 }
66}
67
68Sheet*
69QpTableList::table(unsigned pIdx)
70{
71 return (pIdx < cNameCnt ? cTable[pIdx] : 0);
72}
73
74
75// ---------------------------------------------------------------
76
77QpImport::QpImport( KoFilter*, const char*, const TQStringList& )
78 : KoFilter()
79{
80//cout << "Hooray - in QpImport::QpImport" << endl; // ???
81}
82
83void
84QpImport::InitTableName(int pIdx, TQString& pResult)
85{
86 if( pIdx < 26 )
87 {
88 pResult = (char)('A' + pIdx);
89 }
90 else
91 {
92 pResult = (char)('A' -1 + pIdx / 26);
93 pResult += (char)('A' + pIdx % 26);
94 }
95}
96
97KoFilter::ConversionStatus QpImport::convert( const TQCString& from, const TQCString& to )
98{
99 bool bSuccess=true;
100
101 KoDocument* document = m_chain->outputDocument();
102 if ( !document )
103 return KoFilter::StupidError;
104
105 kdDebug(30523) << "here we go... " << document->className() << endl;
106
107 if( !::tqt_cast<const KSpread::Doc *>( document ) ) // it's safer that way :)
108 {
109 kdWarning(30501) << "document isn't a KSpread::Doc but a " << document->className() << endl;
110 return KoFilter::NotImplemented;
111 }
112 if(from!="application/x-quattropro" || to!="application/x-kspread")
113 {
114 kdWarning(30501) << "Invalid mimetypes " << from << " " << to << endl;
115 return KoFilter::NotImplemented;
116 }
117
118 kdDebug(30523) << "...still here..." << endl;
119
120 // No need for a dynamic cast here, since we use TQt's moc magic
121 Doc *ksdoc=(Doc*)document;
122
123 if(ksdoc->mimeType()!="application/x-kspread")
124 {
125 kdWarning(30501) << "Invalid document mimetype " << ksdoc->mimeType() << endl;
126 return KoFilter::NotImplemented;
127 }
128
129 QpIStream lIn( TQFile::encodeName(m_chain->inputFile()) );
130
131 if( !lIn )
132 {
133 KMessageBox::sorry( 0L, i18n("QPRO filter cannot open input file - please report.") );
134 return KoFilter::FileNotFound;
135 }
136
137 Sheet *table=0;
138
139 TQString field;
140 int value=0;
141 emit sigProgress(value);
142
143 QpRecFactory lFactory(lIn);
144 QpTableList lTableNames;
145 TQP_UINT8 lPageIdx = 0;
146
147 QpRec* lRec = 0;
148 QpRecBop* lRecBop = 0;
149 QpRecIntegerCell* lRecInt = 0;
150 QpRecFloatingPointCell* lRecFloat = 0;
151 QpRecFormulaCell* lRecFormula = 0;
152 QpRecLabelCell* lRecLabel = 0;
153 QpRecPageName* lRecPageName = 0;
154
155 do
156 {
157 field = "";
158 lRec = lFactory.nextRecord();
159
160 switch( lRec->type() )
161 {
162 case QpBop:
163 lRecBop = (QpRecBop*)lRec;
164 lPageIdx = lRecBop->pageIndex();
165
166 // find out if we know about this table already, if not create it
167 table=lTableNames.table(lPageIdx);
168
169 if( table == 0 )
170 {
171 table=ksdoc->map()->addNewSheet();
172 // set up a default name for the table
173 table->setSheetName( lTableNames.name(lPageIdx)
174 , true
175 );
176 lTableNames.table(lPageIdx, table);
177 }
178 break;
179
180 case QpIntegerCell:
181 lRecInt = (QpRecIntegerCell*)lRec;
182 field.setNum( lRecInt->integer() );
183//cout << "Setting R " << lRecInt->row()+1 << ", C " << ((unsigned)lRecInt->column()) << endl;
184 table->setText( lRecInt->row()+1, ((unsigned)lRecInt->column())+1, field, false );
185 break;
186
187 case QpFormulaCell:
188 lRecFormula = (QpRecFormulaCell*)lRec;
189 {
190 Formula lAnswer(*lRecFormula, lTableNames);
191
192 char* lFormula = lAnswer.formula();
193
194 field = lFormula;
195
196 delete [] lFormula;
197 }
198
199 // check for referenced tables that haven't been created yet
200 for(unsigned lIdx=0; lIdx<lTableNames.cNameCnt; ++lIdx)
201 {
202 if(lTableNames.allocated(lIdx) && (lTableNames.table(lIdx) == 0) )
203 {
204 // we're about to reference a table that hasn't been created yet.
205 // setText gets upset about this, so create a blank table
206
207 Sheet* lNewTable=ksdoc->map()->addNewSheet();
208
209 // set up a default name for the table
210 lNewTable->setSheetName( lTableNames.name(lIdx)
211 , true
212 );
213 lTableNames.table(lIdx, lNewTable);
214 }
215 }
216
217 table->setText( lRecFormula->row()+1, lRecFormula->column()+1, field, false );
218 break;
219
220 case QpFloatingPointCell:
221 lRecFloat = (QpRecFloatingPointCell*)lRec;
222 field.setNum( lRecFloat->value() );
223 table->setText( lRecFloat->row()+1, lRecFloat->column()+1, field, false );
224 break;
225
226 case QpLabelCell:
227 lRecLabel = (QpRecLabelCell*)lRec;
228 field = "'";
229 field += lRecLabel->label();
230 table->setText( lRecLabel->row()+1, lRecLabel->column()+1, field, false );
231 break;
232
233 case QpPageName:
234 lRecPageName = (QpRecPageName*)lRec;
235
236 if( lTableNames.allocated(lPageIdx) && lTableNames.table(lPageIdx) )
237 {
238 lTableNames.table(lPageIdx)->setSheetName( lRecPageName->pageName()
239// , true
240 );
241 lTableNames.name(lPageIdx, lRecPageName->pageName());
242 }
243 break;
244
245 case QpPassword:
246 KMessageBox::sorry( 0L, i18n("Unable to open password protected files.\n"
247 "The password algorithm has not been published")
248 );
249 return KoFilter::NotImplemented;
250 }
251
252 delete lRec;
253 lRec = 0;
254 } while( lIn );
255
256 emit sigProgress(100);
257 if ( bSuccess )
258 return KoFilter::OK;
259 else
260 return KoFilter::StupidError;
261}
262
263#include <qproimport.moc>
This class hold a formula.