1 /* This file is part of the KDE project
2    Copyright (C) 2005 Jarosław Staniek <staniek@kde.org>
3 
4    This library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Library General Public
6    License as published by the Free Software Foundation; either
7    version 2 of the License, or (at your option) any later version.
8 
9    This library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Library General Public License for more details.
13 
14    You should have received a copy of the GNU Library General Public License
15    along with this library; see the file COPYING.LIB.  If not, write to
16    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18 */
19 
20 #include "KexiCsvImportExportPlugin.h"
21 #include "kexicsvimportdialog.h"
22 #include "kexicsvexportwizard.h"
23 #include <core/KexiMainWindowIface.h>
24 #include <core/kexiproject.h>
25 #include <core/kexipart.h>
26 #include <kexiutils/utils.h>
27 
28 #include <KDbTableOrQuerySchema>
29 
30 KEXI_PLUGIN_FACTORY(KexiCsvImportExportPlugin, "kexi_csvimportexportplugin.json")
31 
KexiCsvImportExportPlugin(QObject * parent,const QVariantList & args)32 KexiCsvImportExportPlugin::KexiCsvImportExportPlugin(QObject *parent, const QVariantList &args)
33         : KexiInternalPart(parent, args)
34 {
35 }
36 
~KexiCsvImportExportPlugin()37 KexiCsvImportExportPlugin::~KexiCsvImportExportPlugin()
38 {
39 }
40 
createWidget(const char * widgetClass,QWidget * parent,const char * objName,QMap<QString,QString> * args)41 QWidget *KexiCsvImportExportPlugin::createWidget(const char* widgetClass,
42         QWidget *parent, const char *objName, QMap<QString, QString>* args)
43 {
44     if (0 == qstrcmp(widgetClass, "KexiCSVImportDialog")) {
45         KexiCSVImportDialog::Mode mode = (args && (*args)["sourceType"] == "file")
46                                          ? KexiCSVImportDialog::File : KexiCSVImportDialog::Clipboard;
47         KexiCSVImportDialog *dlg = new KexiCSVImportDialog(mode, parent);
48         dlg->setObjectName(objName);
49         KexiInternalPart::setCancelled(dlg->canceled());
50         if (KexiInternalPart::cancelled()) {
51             delete dlg;
52             return 0;
53         }
54         return dlg;
55     } else if (0 == qstrcmp(widgetClass, "KexiCSVExportWizard")) {
56         if (!args)
57             return 0;
58         KexiCSVExport::Options options;
59         if (!options.assign(args))
60             return 0;
61         KexiCSVExportWizard *dlg = new KexiCSVExportWizard(options, parent);
62         dlg->setObjectName(objName);
63         KexiInternalPart::setCancelled(dlg->canceled());
64         if (KexiInternalPart::cancelled()) {
65             delete dlg;
66             return 0;
67         }
68         return dlg;
69     }
70     return 0;
71 }
72 
executeCommand(const char * commandName,QMap<QString,QString> * args)73 bool KexiCsvImportExportPlugin::executeCommand(const char* commandName,
74         QMap<QString, QString>* args)
75 {
76     if (0 == qstrcmp(commandName, "KexiCSVExport")) {
77         KexiCSVExport::Options options;
78         if (!options.assign(args))
79             return false;
80         KDbConnection *conn = KexiMainWindowIface::global()->project()->dbConnection();
81         KDbTableOrQuerySchema tableOrQuery(conn, options.itemId);
82         QTextStream *stream = 0;
83         if (args->contains("textStream")) {
84             stream = KDbUtils::stringToPointer<QTextStream>(args->value("textStream"));
85         }
86         return KexiCSVExport::exportData(conn, &tableOrQuery, options, -1, stream);
87     }
88     return false;
89 }
90 
91 #include "KexiCsvImportExportPlugin.moc"
92