filters
kspread_kexiimport.cc00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "kspread_kexiimport.h"
00030
00031 #include "kspread_kexiimportdialog.h"
00032
00033
00034 #include <kspread_view.h>
00035 #include <kspread_doc.h>
00036
00037 #include <kspread_sheet.h>
00038 #include <kspread_map.h>
00039 #include <kspread_style.h>
00040
00041
00042 #include <KoFilterChain.h>
00043 #include <KoFilterManager.h>
00044 #include <kofficeversion.h>
00045
00046
00047 #include <kexidb/cursor.h>
00048 #include <kexidb/connection.h>
00049 #include <kexidb/parser/parser.h>
00050
00051
00052 #include <kgenericfactory.h>
00053 #include <klocale.h>
00054 #include <kmessagebox.h>
00055 #include <klistview.h>
00056 #include <qcolor.h>
00057
00058 typedef KGenericFactory<KSpreadKexiImport, KoFilter> KSpreadKexiImportFactory;
00059 K_EXPORT_COMPONENT_FACTORY( libkspreadkexiimport, KSpreadKexiImportFactory( "kofficefilters" ) )
00060
00061
00062
00063
00064 KSpreadKexiImport::KSpreadKexiImport(KoFilter *parent, const char *name, const QStringList&)
00065 {
00066 }
00067
00068
00069
00070 KSpreadKexiImport::~KSpreadKexiImport()
00071 {
00072 delete m_dialog;
00073 }
00074
00075
00076
00077 KoFilter::ConversionStatus KSpreadKexiImport::convert( const QCString& from, const QCString& to )
00078 {
00079 QPtrList<QListViewItem> objects;
00080 QString file( m_chain->inputFile() );
00081
00082
00083 m_dialog = new KSpreadKexiImportDialog();
00084
00085 m_dialog->openDatabase( file, 0);
00086 if (!m_dialog->exec())
00087 {
00088 return KoFilter::UserCancelled;
00089 }
00090
00091 objects = m_dialog->selectedItems();
00092
00093 kdDebug() << "Getting Documents..." << endl;
00094
00095 KoDocument* document = m_chain->outputDocument();
00096
00097 if ( !document )
00098 return KoFilter::StupidError;
00099
00100 kdDebug() << "here we go... " << document->className() << endl;
00101
00102 if ( !::qt_cast<const KSpread::Doc *>( document ) )
00103 {
00104 kdWarning() << "document isn't a KSpread::Doc but a " << document->className() << endl;
00105 return KoFilter::NotImplemented;
00106 }
00107 if(from!="application/x-kexiproject-sqlite3" || to!="application/x-kspread")
00108 {
00109 kdWarning() << "Invalid mimetypes " << from << " " << to << endl;
00110 return KoFilter::NotImplemented;
00111 }
00112
00113 ksdoc = static_cast<KSpread::Doc *>( document );
00114
00115 if(ksdoc->mimeType()!="application/x-kspread")
00116 {
00117 kdWarning() << "Invalid document mimetype " << ksdoc->mimeType() << endl;
00118 return KoFilter::NotImplemented;
00119 }
00120
00121 document->emitBeginOperation();
00122
00123 QListViewItem *itm;
00124 for(itm = objects.first(); itm ; itm = objects.next())
00125 {
00126 if (!insertObject(itm->text(1), itm->text(0)))
00127 {
00128 return KoFilter::StupidError;
00129 }
00130 }
00131 if (m_dialog->customQuery())
00132 {
00133 if (!insertObject(m_dialog->customQueryString(), "Custom"))
00134 {
00135 return KoFilter::StupidError;
00136 }
00137 }
00138
00139 document->emitEndOperation();
00140 kdDebug() << "inserting kexi data completed" << endl;
00141 return KoFilter::OK;
00142 }
00143
00144 bool KSpreadKexiImport::insertObject(const QString& object, const QString& type)
00145 {
00146 QStringList fieldNames;
00147 KSpread::Sheet *sheet;
00148 KexiDB::Parser *parser;
00149 KexiDB::QuerySchema *query;
00150
00151 sheet = ksdoc->map()->addNewSheet();
00152 if (!sheet)
00153 {
00154 KMessageBox::error(NULL, i18n("Cant find sheet"), i18n("Error"));
00155 return false;
00156 }
00157
00158 if (type == "Custom")
00159 {
00160 sheet->setSheetName("Custom");
00161 }
00162 else
00163 {
00164 sheet->setSheetName(object);
00165 }
00166
00167
00168 if (type == "Table")
00169 {
00170 fieldNames = m_dialog->connection()->tableSchema(object)->names();
00171 }
00172 else if (type == "Query")
00173 {
00174
00175 fieldNames = m_dialog->connection()->querySchema(object)->names();
00176 }
00177 else if (type == "Custom")
00178 {
00179 parser = new KexiDB::Parser(m_dialog->connection());
00180 parser->parse( object );
00181
00182 if (parser->operation() != KexiDB::Parser::OP_Select)
00183 {
00184 KMessageBox::error(NULL, i18n("Your query was invalid or not a SELECT query"), i18n("Error"));
00185 return false;
00186 }
00187 query = parser->query();
00188 fieldNames = query->names();
00189 }
00190
00191
00192 QStringList::iterator it;
00193 int i = 1;
00194 for (it = fieldNames.begin(); it != fieldNames.end(); ++it, ++i)
00195 {
00196 KSpread::Cell *c = sheet->nonDefaultCell(i ,1);
00197 c->setCellText(*it, true);
00198 c->format()->setBgColor(QColor(200,200,200));
00199 c->format()->setTextFontBold(true);
00200 }
00201
00202
00203 KexiDB::Cursor *cur;
00204 if (type == "Table")
00205 {
00206 cur = m_dialog->connection()->executeQuery( *(m_dialog->connection()->tableSchema(object)));
00207 }
00208 else if (type == "Query")
00209 {
00210 cur = m_dialog->connection()->executeQuery( *(m_dialog->connection()->querySchema(object)));
00211 }
00212 else if (type == "Custom")
00213 {
00214 cur = m_dialog->connection()->executeQuery( *query );
00215 }
00216 else
00217 {
00218 cur = 0;
00219 }
00220
00221 if (!cur)
00222 {
00223 KMessageBox::error(NULL, i18n("Error executing query"), i18n("Error"));
00224 return false;
00225 }
00226
00227 cur->moveFirst();
00228 int j = cur->fieldCount();
00229
00230 unsigned int k = 2;
00231 bool asText = false;
00232
00233 if(m_dialog->conversion() == 2)
00234 asText = true;
00235
00236 while (!cur->eof())
00237 {
00238 for (i = 0; i < j; ++i)
00239 {
00240 sheet->nonDefaultCell(i+1 ,k)->setCellText(cur->value(i).toString(), asText);
00241 }
00242
00243 kdDebug() << k << endl;
00244 cur->moveNext();
00245 ++k;
00246 }
00247 return true;
00248 }
00249 #include "kspread_kexiimport.moc"
|