kspread_kexiimportdialog.cpp
1/***************************************************************************
2 * Copyright (C) 2006 by Adam Pigg *
3 * adam@piggz.co.uk *
4 * *
5 * Based on insert calendar code: *
6 * Copyright (C) 2005 by Raphael Langerhorst *
7 * raphael-langerhorst@gmx.at *
8 * *
9 * Permission is hereby granted, free of charge, to any person obtaining *
10 * a copy of this software and associated documentation files (the *
11 * "Software"), to deal in the Software without restriction, including *
12 * without limitation the rights to use, copy, modify, merge, publish, *
13 * distribute, sublicense, and/or sell copies of the Software, and to *
14 * permit persons to whom the Software is furnished to do so, subject to *
15 * the following conditions: *
16 * *
17 * The above copyright notice and this permission notice shall be *
18 * included in all copies or substantial portions of the Software. *
19 * *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*
23 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR *
24 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 ***************************************************************************/
28
29#include "kspread_kexiimportdialog.h"
30
31//KDE Includes
32#include <kdebug.h>
33#include <tdeglobal.h>
34#include <kpushbutton.h>
35#include <tdelistview.h>
36#include <kcombobox.h>
37#include <tqradiobutton.h>
38#include <tqtextedit.h>
39#include <tqcheckbox.h>
40#include <tdelocale.h>
41
42//Kexi Includes
43#include <kexidb/global.h>
44#include <kexidb/kexidb_export.h>
45#include <kexidb/connectiondata.h>
46#include <kexidb/connection.h>
47#include <kexidb/drivermanager.h>
48#include <kexidb/driver.h>
49
50//KSpread includes
51#include <kspread_view.h>
52#include <kspread_doc.h>
53#include <kspread_map.h>
54
61 KSpreadKexiImportDialog::KSpreadKexiImportDialog(TQWidget* parent, const char* name)
62 : KSpreadKexiImportDialogBase(parent,name)
63{
64 connect(this->m_insertButton,TQ_SIGNAL(clicked()),this,TQ_SLOT(accept()));
65 connect(this->m_cancelButton,TQ_SIGNAL(clicked()),this,TQ_SLOT(reject()));
66}
67
72KSpreadKexiImportDialog::~KSpreadKexiImportDialog()
73{}
74
78void KSpreadKexiImportDialog::accept()
79{
80 kdDebug() << "insert kexi data dialog accepted (insert button clicked)" << endl;
81 done(TQDialog::Accepted);
82 emit insertKexi();
83}
84
88void KSpreadKexiImportDialog::reject()
89{
90 kdDebug() << "insert kexi data dialog rejected (cancel button clicked)" << endl;
91 done(TQDialog::Rejected);
92}
93
99void KSpreadKexiImportDialog::openDatabase( TQString fileName , KexiDB::ConnectionData *cdata)
100{
101 kdDebug() << "openDatabase" << endl;
102 KexiDB::Driver *dr;
103 KexiDB::DriverManager *dm;
104 KexiDB::ConnectionData cd;
105
106 //Open file db
107 dm = new KexiDB::DriverManager();
108 dr = dm->driver("sqlite3");
109 if (!dr)
110 {
111 kdDebug() << "Unable to create driver" << endl;
112 return;
113 }
114
115 if (cdata)
116 {
117 //Open server db
118 cd = *cdata;
119 }
120 else
121 {
122 if (!fileName.isEmpty())
123 {
124 cd.setFileName(fileName);
125 }
126 else
127 {
128 kdDebug() << "No file name" << endl;
129 KMessageBox::error(NULL, i18n("No file specified"), i18n("Error"));
130 return;
131 }
132 }
133
134 conn = dr->createConnection(cd);
135
136 if (!conn)
137 {
138 KMessageBox::error(NULL, i18n("Error creating connection"), i18n("Error"));
139 return;
140 }
141
142 if(!conn->connect())
143 {
144 KMessageBox::error(NULL, i18n("Error connecting to database"), i18n("Error"));
145 conn->debugError();
146 return;
147 }
148
149 if (!conn->useDatabase( fileName ))
150 {
151 KMessageBox::error(NULL, i18n("Error using database"), i18n("Error"));
152 conn->debugError();
153 return;
154 }
155
156 populateTables();
157
158}
159
164void KSpreadKexiImportDialog::populateTables()
165{
166 TQValueList<int> tids;
167 TQValueList<int> qids;
168
169 kdDebug() << "Getting Tables and Queries" << endl;
170 tids = conn->objectIds(KexiDB::TableObjectType);
171 qids = conn->objectIds(KexiDB::QueryObjectType);
172
173 kdDebug() << qids.count() << " queries " << tids.count() << " tables" << endl;
174
175 TQValueList<int>::iterator it;
176
177 for ( it = tids.begin(); it != tids.end(); ++it )
178 {
179 (void) new TDEListViewItem(m_sourceList,"Table", conn->tableSchema(*it)->name());
180 kdDebug() << "Table ID:" << (*it) << " " << conn->tableSchema(*it)->name()<< endl;
181 }
182
183 for ( it = qids.begin(); it != qids.end(); ++it )
184 {
185 (void) new TDEListViewItem(m_sourceList,"Query", conn->querySchema(*it)->name());
186 kdDebug() << "Query ID:" << (*it) << " " << conn->querySchema(*it)->name() << endl;
187 }
188
189 //Select the first item
190 if(m_sourceList->firstChild())
191 {
192 m_sourceList->setSelected(m_sourceList->firstChild(), true);
193 }
194
195}
196
201TQString KSpreadKexiImportDialog::customQueryString()
202{
203 return m_CustomQueryText->text();
204}
205
206TQPtrList<TQListViewItem> KSpreadKexiImportDialog::selectedItems()
207{
208 TQPtrList<TQListViewItem> lst;
209 TQListViewItemIterator it( m_sourceList );
210 while ( it.current() )
211 {
212 if ( it.current()->isSelected() )
213 lst.append( it.current() );
214 ++it;
215 }
216 return lst;
217}
218
219bool KSpreadKexiImportDialog::customQuery()
220{
221 return m_customQueryCheck->isChecked();
222}
227int KSpreadKexiImportDialog::conversion()
228{
229 if ( m_convKSRadio->isChecked())
230 return 1;
231 else if ( m_convTextRadio->isChecked())
232 return 2;
233 else
234 return -1;
235}
236