filters

kspread_kexiimportdialog.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_kexiimportdialog.h"
00030 
00031 //KDE Includes
00032 #include <kdebug.h>
00033 #include <kglobal.h>
00034 #include <kpushbutton.h>
00035 #include <klistview.h>
00036 #include <kcombobox.h>
00037 #include <qradiobutton.h>
00038 #include <qtextedit.h>
00039 #include <qcheckbox.h>
00040 #include <klocale.h>
00041 
00042 //Kexi Includes
00043 #include <kexidb/global.h>
00044 #include <kexidb/kexidb_export.h>
00045 #include <kexidb/connectiondata.h>
00046 #include <kexidb/connection.h>
00047 #include <kexidb/drivermanager.h>
00048 #include <kexidb/driver.h>
00049 
00050 //KSpread includes
00051 #include <kspread_view.h>
00052 #include <kspread_doc.h>
00053 #include <kspread_map.h>
00054 
00061     KSpreadKexiImportDialog::KSpreadKexiImportDialog(QWidget* parent, const char* name)
00062     : KSpreadKexiImportDialogBase(parent,name)
00063 {
00064     connect(this->m_insertButton,SIGNAL(clicked()),this,SLOT(accept()));
00065     connect(this->m_cancelButton,SIGNAL(clicked()),this,SLOT(reject()));
00066 }
00067 
00072 KSpreadKexiImportDialog::~KSpreadKexiImportDialog()
00073 {}
00074 
00078 void KSpreadKexiImportDialog::accept()
00079 {
00080     kdDebug() << "insert kexi data dialog accepted (insert button clicked)" << endl;
00081     done(QDialog::Accepted);
00082     emit insertKexi();
00083 }
00084 
00088 void KSpreadKexiImportDialog::reject()
00089 {
00090     kdDebug() << "insert kexi data dialog rejected (cancel button clicked)" << endl;
00091     done(QDialog::Rejected);
00092 }
00093 
00099 void KSpreadKexiImportDialog::openDatabase( QString fileName , KexiDB::ConnectionData *cdata)
00100 {
00101     kdDebug() << "openDatabase" << endl;
00102     KexiDB::Driver *dr;
00103     KexiDB::DriverManager *dm;
00104     KexiDB::ConnectionData cd;
00105     
00106     //Open file db
00107     dm = new KexiDB::DriverManager();
00108     dr = dm->driver("sqlite3");
00109     if (!dr)
00110     {
00111         kdDebug() << "Unable to create driver" << endl;
00112         return;
00113     }
00114     
00115     if (cdata)
00116     {
00117         //Open server db
00118         cd = *cdata;
00119     }
00120     else
00121     {
00122         if (!fileName.isEmpty())
00123         {
00124             cd.setFileName(fileName); 
00125         }
00126         else
00127         {
00128             kdDebug() << "No file name" << endl;
00129             KMessageBox::error(NULL, i18n("No file specified"), i18n("Error"));
00130             return;
00131         }
00132     }
00133 
00134     conn = dr->createConnection(cd);
00135 
00136     if (!conn)
00137     {
00138         KMessageBox::error(NULL, i18n("Error creating connection"), i18n("Error"));
00139         return;
00140     }
00141 
00142     if(!conn->connect())
00143     {
00144         KMessageBox::error(NULL, i18n("Error connecting to database"), i18n("Error"));
00145         conn->debugError();
00146         return;
00147     }
00148 
00149     if (!conn->useDatabase( fileName ))
00150     {
00151         KMessageBox::error(NULL, i18n("Error using database"), i18n("Error"));
00152         conn->debugError();
00153         return;
00154     }
00155 
00156     populateTables();
00157     
00158 }
00159 
00164 void KSpreadKexiImportDialog::populateTables()
00165 {
00166     QValueList<int> tids;
00167     QValueList<int> qids;
00168 
00169     kdDebug() << "Getting Tables and Queries" << endl;
00170     tids = conn->objectIds(KexiDB::TableObjectType);
00171     qids = conn->objectIds(KexiDB::QueryObjectType);
00172 
00173     kdDebug() << qids.count() << " queries " << tids.count() << " tables" << endl;
00174 
00175     QValueList<int>::iterator it;
00176 
00177     for ( it = tids.begin(); it != tids.end(); ++it )
00178     {
00179         (void) new KListViewItem(m_sourceList,"Table", conn->tableSchema(*it)->name());
00180         kdDebug() << "Table ID:" << (*it) << " " << conn->tableSchema(*it)->name()<< endl;
00181     }
00182 
00183     for ( it = qids.begin(); it != qids.end(); ++it )
00184     {
00185         (void) new KListViewItem(m_sourceList,"Query", conn->querySchema(*it)->name());
00186         kdDebug() << "Query ID:" << (*it) << " " << conn->querySchema(*it)->name() << endl;
00187     }
00188 
00189     //Select the first item
00190     if(m_sourceList->firstChild())
00191     {
00192         m_sourceList->setSelected(m_sourceList->firstChild(), true);
00193     }
00194 
00195 }
00196 
00201 QString KSpreadKexiImportDialog::customQueryString()
00202 {
00203     return m_CustomQueryText->text();
00204 }
00205 
00206 QPtrList<QListViewItem> KSpreadKexiImportDialog::selectedItems()
00207 {
00208     QPtrList<QListViewItem> lst;
00209     QListViewItemIterator it( m_sourceList );
00210     while ( it.current() ) 
00211     {
00212         if ( it.current()->isSelected() )
00213             lst.append( it.current() );
00214         ++it;
00215     }
00216     return lst;
00217 }
00218 
00219 bool KSpreadKexiImportDialog::customQuery()
00220 {
00221     return m_customQueryCheck->isChecked();
00222 }
00227 int KSpreadKexiImportDialog::conversion()
00228 {
00229     if ( m_convKSRadio->isChecked())
00230         return 1;
00231     else if ( m_convTextRadio->isChecked())
00232         return 2;
00233     else
00234         return -1;
00235 }
00236 
KDE Home | KDE Accessibility Home | Description of Access Keys