filters

kspread_kexiimport.cc

00001 /***************************************************************************
00002  *   Copyright (C) 2006 by Adam Pigg                                       *
00003  *   adam@piggz.co.uk                                                      *
00004  *                                                                         *
00005  *   Based on insert calendar code:                                        *
00006  *   Copyright (C) 2005 by Raphael Langerhorst                             *
00007  *   raphael-langerhorst@gmx.at                                            *
00008  *                                                                         *
00009  *   Permission is hereby granted, free of charge, to any person obtaining *
00010  *   a copy of this software and associated documentation files (the       *
00011  *   "Software"), to deal in the Software without restriction, including   *
00012  *   without limitation the rights to use, copy, modify, merge, publish,   *
00013  *   distribute, sublicense, and/or sell copies of the Software, and to    *
00014  *   permit persons to whom the Software is furnished to do so, subject to *
00015  *   the following conditions:                                             *
00016  *                                                                         *
00017  *   The above copyright notice and this permission notice shall be        *
00018  *   included in all copies or substantial portions of the Software.       *
00019  *                                                                         *
00020  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       *
00021  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    *
00022  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*
00023  *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR     *
00024  *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
00025  *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
00026  *   OTHER DEALINGS IN THE SOFTWARE.                                       *
00027  ***************************************************************************/
00028 
00029 #include "kspread_kexiimport.h"
00030 
00031 #include "kspread_kexiimportdialog.h"
00032 
00033 //KSpread Includes
00034 #include <kspread_view.h>
00035 #include <kspread_doc.h>
00036 //#include <selection.h>
00037 #include <kspread_sheet.h>
00038 #include <kspread_map.h>
00039 #include <kspread_style.h>
00040 
00041 //Koffice Includes
00042 #include <KoFilterChain.h>
00043 #include <KoFilterManager.h>
00044 #include <kofficeversion.h>
00045 
00046 //Kexi Includes
00047 #include <kexidb/cursor.h>
00048 #include <kexidb/connection.h>
00049 #include <kexidb/parser/parser.h>
00050 
00051 //KDE Includes
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     //Create dialog
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 ); // type checked above
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     //Get the field names
00168     if (type == "Table")
00169     {
00170         fieldNames = m_dialog->connection()->tableSchema(object)->names();
00171     }
00172     else if (type == "Query")
00173     {
00174         //Note....for queries such as select * from....field names are not available
00175         fieldNames = m_dialog->connection()->querySchema(object)->names();
00176     }
00177     else if (type == "Custom") //Custom Query
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     //Insert the field headings
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     //Insert the data
00203     KexiDB::Cursor *cur;
00204     if (type == "Table") //Table
00205     {
00206         cur = m_dialog->connection()->executeQuery( *(m_dialog->connection()->tableSchema(object)));
00207     }
00208     else if (type == "Query") //Query
00209     {
00210         cur = m_dialog->connection()->executeQuery( *(m_dialog->connection()->querySchema(object)));
00211     }
00212     else if (type == "Custom") //Custom Query
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; //start on row 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"
KDE Home | KDE Accessibility Home | Description of Access Keys