summaryrefslogtreecommitdiffstats
path: root/filters/kspread/kexi/kspread_kexiimport.cc
blob: 6100dabe386a88ff4277ef2e3dadb278ed7cbad6 (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
237
238
239
240
241
242
243
244
245
246
247
248
249
/***************************************************************************
 *   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_kexiimport.h"

#include "kspread_kexiimportdialog.h"

//KSpread Includes
#include <kspread_view.h>
#include <kspread_doc.h>
//#include <selection.h>
#include <kspread_sheet.h>
#include <kspread_map.h>
#include <kspread_style.h>

//Koffice Includes
#include <KoFilterChain.h>
#include <KoFilterManager.h>
#include <kofficeversion.h>

//Kexi Includes
#include <kexidb/cursor.h>
#include <kexidb/connection.h>
#include <kexidb/parser/parser.h>

//KDE Includes
#include <kgenericfactory.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <tdelistview.h>
#include <tqcolor.h>

typedef KGenericFactory<KSpreadKexiImport, KoFilter> KSpreadKexiImportFactory;
K_EXPORT_COMPONENT_FACTORY( libkspreadkexiimport, KSpreadKexiImportFactory( "kofficefilters" ) )


//=============================================================================

KSpreadKexiImport::KSpreadKexiImport(KoFilter *parent, const char *name, const TQStringList&)
{
}

//=============================================================================

KSpreadKexiImport::~KSpreadKexiImport()
{
    delete m_dialog;
}

//=============================================================================

KoFilter::ConversionStatus KSpreadKexiImport::convert( const TQCString& from, const TQCString& to )
{
    TQPtrList<TQListViewItem> objects;
    TQString file( m_chain->inputFile() );

    //Create dialog
    m_dialog = new KSpreadKexiImportDialog();

    m_dialog->openDatabase( file, 0);
    if (!m_dialog->exec())
    {
        return KoFilter::UserCancelled;
    }

    objects = m_dialog->selectedItems();

    kdDebug() << "Getting Documents..." << endl;

    KoDocument* document = m_chain->outputDocument();

    if ( !document )
        return KoFilter::StupidError;

    kdDebug() << "here we go... " << document->className() << endl;

    if ( !::tqqt_cast<const KSpread::Doc *>( document ) )
    {
        kdWarning() << "document isn't a KSpread::Doc but a " << document->className() << endl;
        return KoFilter::NotImplemented;
    }
    if(from!="application/x-kexiproject-sqlite3" || to!="application/x-kspread")
    {
        kdWarning() << "Invalid mimetypes " << from << " " << to << endl;
        return KoFilter::NotImplemented;
    }

    ksdoc = static_cast<KSpread::Doc *>( document ); // type checked above

    if(ksdoc->mimeType()!="application/x-kspread")
    {
        kdWarning() << "Invalid document mimetype " << ksdoc->mimeType() << endl;
        return KoFilter::NotImplemented;
    }
    
    document->emitBeginOperation();
    
    TQListViewItem *itm;
    for(itm = objects.first(); itm ; itm = objects.next())
    {
        if (!insertObject(itm->text(1), itm->text(0)))
        {
            return KoFilter::StupidError;
        }
    }
    if (m_dialog->customQuery())
    {
        if (!insertObject(m_dialog->customQueryString(), "Custom"))
        {
            return KoFilter::StupidError;
        }
    }

    document->emitEndOperation();
    kdDebug() << "inserting kexi data completed" << endl;
    return KoFilter::OK;
}

bool KSpreadKexiImport::insertObject(const TQString& object, const TQString& type)
{
    TQStringList fieldNames;
    KSpread::Sheet *sheet;
    KexiDB::Parser *parser;
    KexiDB::QuerySchema *query;
    
    sheet = ksdoc->map()->addNewSheet();
    if (!sheet)
    {
        KMessageBox::error(NULL, i18n("Cant find sheet"), i18n("Error"));
        return false;
    }

    if (type == "Custom")
    {
        sheet->setSheetName("Custom");
    }
    else
    {
        sheet->setSheetName(object);
    }

    //Get the field names
    if (type == "Table")
    {
        fieldNames = m_dialog->connection()->tableSchema(object)->names();
    }
    else if (type == "Query")
    {
        //Note....for queries such as select * from....field names are not available
        fieldNames = m_dialog->connection()->querySchema(object)->names();
    }
    else if (type == "Custom") //Custom Query
    {
        parser = new KexiDB::Parser(m_dialog->connection());
        parser->parse( object );
        
        if (parser->operation() != KexiDB::Parser::OP_Select)
        {
            KMessageBox::error(NULL, i18n("Your query was invalid or not a SELECT query"), i18n("Error"));
            return false;
        }
        query = parser->query();
        fieldNames = query->names();
    }

    //Insert the field headings
    TQStringList::iterator it;
    int i = 1;
    for (it = fieldNames.begin(); it != fieldNames.end(); ++it, ++i)
    {
        KSpread::Cell *c = sheet->nonDefaultCell(i ,1);
        c->setCellText(*it, true);
        c->format()->setBgColor(TQColor(200,200,200));
        c->format()->setTextFontBold(true);
    }

    //Insert the data
    KexiDB::Cursor *cur;
    if (type == "Table") //Table
    {
        cur = m_dialog->connection()->executeQuery( *(m_dialog->connection()->tableSchema(object)));
    }
    else if (type == "Query") //Query
    {
        cur = m_dialog->connection()->executeQuery( *(m_dialog->connection()->querySchema(object)));
    }
    else if (type == "Custom") //Custom Query
    {
        cur = m_dialog->connection()->executeQuery( *query );
    }
    else
    {
        cur = 0;
    }

    if (!cur)
    {
        KMessageBox::error(NULL, i18n("Error executing query"), i18n("Error"));
        return false;
    }

    cur->moveFirst();
    int j = cur->fieldCount();

    unsigned int k = 2; //start on row 2
    bool asText = false;

    if(m_dialog->conversion() == 2)
        asText = true;

    while (!cur->eof())
    {
        for (i = 0; i < j; ++i)
        {
            sheet->nonDefaultCell(i+1 ,k)->setCellText(cur->value(i).toString(), asText);
        }

        kdDebug() << k << endl;
        cur->moveNext();
        ++k;
    }
    return true;
}
#include "kspread_kexiimport.moc"