summaryrefslogtreecommitdiffstats
path: root/filters/kspread/kexi/kspread_kexiimportdialog.cc
blob: 0e15acbe719ac81d86472d0f4e4036cc5a93b72b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/***************************************************************************
 *   Copyright (C) 2006 by Adam Pigg                                       *
 *   adam@piggz.co.uk                                                      *
 *                                                                         *
 *   Based on insert calendar code:                                        *
 *   Copyright (C) 2005 by Raphael Langerhorst                             *
 *   raphael-langerhorst@gmx.at                                            *
 *                                                                         *
 *   Permission is hereby granted, free of charge, to any person obtaining *
 *   a copy of this software and associated documentation files (the       *
 *   "Software"), to deal in the Software without restriction, including   *
 *   without limitation the rights to use, copy, modify, merge, publish,   *
 *   distribute, sublicense, and/or sell copies of the Software, and to    *
 *   permit persons to whom the Software is furnished to do so, subject to *
 *   the following conditions:                                             *
 *                                                                         *
 *   The above copyright notice and this permission notice shall be        *
 *   included in all copies or substantial portions of the Software.       *
 *                                                                         *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       *
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    *
 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*
 *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR     *
 *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
 *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
 *   OTHER DEALINGS IN THE SOFTWARE.                                       *
 ***************************************************************************/

#include "kspread_kexiimportdialog.h"

//KDE Includes
#include <kdebug.h>
#include <tdeglobal.h>
#include <kpushbutton.h>
#include <tdelistview.h>
#include <kcombobox.h>
#include <tqradiobutton.h>
#include <tqtextedit.h>
#include <tqcheckbox.h>
#include <tdelocale.h>

//Kexi Includes
#include <kexidb/global.h>
#include <kexidb/kexidb_export.h>
#include <kexidb/connectiondata.h>
#include <kexidb/connection.h>
#include <kexidb/drivermanager.h>
#include <kexidb/driver.h>

//KSpread includes
#include <kspread_view.h>
#include <kspread_doc.h>
#include <kspread_map.h>

/**
 * Constructor
 * @param parent Pointer to kspread view
 * @param name Name of the dialog
 * @return None
 */
    KSpreadKexiImportDialog::KSpreadKexiImportDialog(TQWidget* parent, const char* name)
    : KSpreadKexiImportDialogBase(parent,name)
{
    connect(this->m_insertButton,TQT_SIGNAL(clicked()),this,TQT_SLOT(accept()));
    connect(this->m_cancelButton,TQT_SIGNAL(clicked()),this,TQT_SLOT(reject()));
}

/**
 * Destructor
 * @return None
 */
KSpreadKexiImportDialog::~KSpreadKexiImportDialog()
{}

/**
 * Accepts the dialog and start the import process
 */
void KSpreadKexiImportDialog::accept()
{
    kdDebug() << "insert kexi data dialog accepted (insert button clicked)" << endl;
    done(TQDialog::Accepted);
    emit insertKexi();
}

/**
 * Cancels the dialog
 */
void KSpreadKexiImportDialog::reject()
{
    kdDebug() << "insert kexi data dialog rejected (cancel button clicked)" << endl;
    done(TQDialog::Rejected);
}

/**
 * Opens a database given either a filename or connection data
 * @param fileName name of file to open if cdata is null
 * @param cdata connection data to server based database...not implemented though
 */
void KSpreadKexiImportDialog::openDatabase( TQString fileName , KexiDB::ConnectionData *cdata)
{
    kdDebug() << "openDatabase" << endl;
    KexiDB::Driver *dr;
    KexiDB::DriverManager *dm;
    KexiDB::ConnectionData cd;
    
    //Open file db
    dm = new KexiDB::DriverManager();
    dr = dm->driver("sqlite3");
    if (!dr)
    {
        kdDebug() << "Unable to create driver" << endl;
        return;
    }
    
    if (cdata)
    {
        //Open server db
        cd = *cdata;
    }
    else
    {
        if (!fileName.isEmpty())
        {
            cd.setFileName(fileName); 
        }
        else
        {
            kdDebug() << "No file name" << endl;
            KMessageBox::error(NULL, i18n("No file specified"), i18n("Error"));
            return;
        }
    }

    conn = dr->createConnection(cd);

    if (!conn)
    {
        KMessageBox::error(NULL, i18n("Error creating connection"), i18n("Error"));
        return;
    }

    if(!conn->connect())
    {
        KMessageBox::error(NULL, i18n("Error connecting to database"), i18n("Error"));
        conn->debugError();
        return;
    }

    if (!conn->useDatabase( fileName ))
    {
        KMessageBox::error(NULL, i18n("Error using database"), i18n("Error"));
        conn->debugError();
        return;
    }

    populateTables();
    
}

/**
 * Gets all the tables and queries from the database
 * and adds them to m_sourceList
 */
void KSpreadKexiImportDialog::populateTables()
{
    TQValueList<int> tids;
    TQValueList<int> qids;

    kdDebug() << "Getting Tables and Queries" << endl;
    tids = conn->objectIds(KexiDB::TableObjectType);
    qids = conn->objectIds(KexiDB::QueryObjectType);

    kdDebug() << qids.count() << " queries " << tids.count() << " tables" << endl;

    TQValueList<int>::iterator it;

    for ( it = tids.begin(); it != tids.end(); ++it )
    {
        (void) new TDEListViewItem(m_sourceList,"Table", conn->tableSchema(*it)->name());
        kdDebug() << "Table ID:" << (*it) << " " << conn->tableSchema(*it)->name()<< endl;
    }

    for ( it = qids.begin(); it != qids.end(); ++it )
    {
        (void) new TDEListViewItem(m_sourceList,"Query", conn->querySchema(*it)->name());
        kdDebug() << "Query ID:" << (*it) << " " << conn->querySchema(*it)->name() << endl;
    }

    //Select the first item
    if(m_sourceList->firstChild())
    {
        m_sourceList->setSelected(m_sourceList->firstChild(), true);
    }

}

/**
 * 
 * @return the custom query that has been entered
 */
TQString KSpreadKexiImportDialog::customQueryString()
{
    return m_CustomQueryText->text();
}

TQPtrList<TQListViewItem> KSpreadKexiImportDialog::selectedItems()
{
    TQPtrList<TQListViewItem> lst;
    TQListViewItemIterator it( m_sourceList );
    while ( it.current() ) 
    {
        if ( it.current()->isSelected() )
            lst.append( it.current() );
        ++it;
    }
    return lst;
}

bool KSpreadKexiImportDialog::customQuery()
{
    return m_customQueryCheck->isChecked();
}
/**
 * Returns the user specified option for data conversion
 * @return integer stating the type of cenverion to make
 */
int KSpreadKexiImportDialog::conversion()
{
    if ( m_convKSRadio->isChecked())
        return 1;
    else if ( m_convTextRadio->isChecked())
        return 2;
    else
        return -1;
}