kexi

datatableaction.cpp
1/***************************************************************************
2 * This file is part of the KDE project
3 * copyright (C) 2006 by Sebastian Sauer (mail@dipe.org)
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 * This program 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 * You should have received a copy of the GNU Library General Public License
14 * along with this program; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 * Boston, MA 02110-1301, USA.
17 ***************************************************************************/
18
19#include "datatableaction.h"
20//#include "objectvariable.h"
21
22#include <core/kexi.h>
23#include <core/kexiproject.h>
24#include <core/kexipartmanager.h>
25#include <core/kexipartinfo.h>
26#include <core/kexipart.h>
27#include <core/keximainwindow.h>
28#include <core/kexiinternalpart.h>
29
30#include <tdelocale.h>
31
32using namespace KexiMacro;
33
34namespace KexiMacro {
35
36 //static const TQString OBJECT = "method";
37 //static const TQString OBJECT = "type";
38 //static const TQString OBJECT = "partitem";
39
40 template<class ACTIONIMPL>
41 class MethodVariable : public KexiVariable<ACTIONIMPL>
42 {
43 public:
44 MethodVariable(ACTIONIMPL* actionimpl)
45 : KexiVariable<ACTIONIMPL>(actionimpl, "method", i18n("Method"))
46 {
47 TQStringList list;
48 list << "import" << "export";
49 this->appendChild( TDESharedPtr<KoMacro::Variable>( new KoMacro::Variable(list, "@list") ) );
50
51 this->setVariant( list[0] );
52 }
53 };
54
55 template<class ACTIONIMPL>
56 class TypeVariable : public KexiVariable<ACTIONIMPL>
57 {
58 public:
59 TypeVariable(ACTIONIMPL* actionimpl)
60 : KexiVariable<ACTIONIMPL>(actionimpl, "type", i18n("Type"))
61 {
62 TQStringList list;
63 list << "file" << "clipboard";
64 this->appendChild( TDESharedPtr<KoMacro::Variable>( new KoMacro::Variable(list, "@list") ) );
65
66 this->setVariant( list[0] );
67 }
68 };
69
70 template<class ACTIONIMPL>
71 class PartItemVariable : public KexiVariable<ACTIONIMPL>
72 {
73 public:
74 PartItemVariable(ACTIONIMPL* actionimpl, const TQString& partitem = TQString())
75 : KexiVariable<ACTIONIMPL>(actionimpl, "partitem", i18n("Item"))
76 {
77 TQStringList namelist;
78 if(actionimpl->mainWin()->project()) {
79 KexiPart::PartInfoList* parts = Kexi::partManager().partInfoList();
80 for(KexiPart::PartInfoListIterator it(*parts); it.current(); ++it) {
81 KexiPart::Info* info = it.current();
82 if(! info->isDataExportSupported())
83 continue;
84 KexiPart::ItemDict* items = actionimpl->mainWin()->project()->items(info);
85 if(items)
86 for(KexiPart::ItemDictIterator item_it = *items; item_it.current(); ++item_it)
87 namelist << info->objectName() + "." + item_it.current()->name();
88 }
89 for(TQStringList::Iterator it = namelist.begin(); it != namelist.end(); ++it)
90 this->appendChild( TDESharedPtr<KoMacro::Variable>(new KoMacro::Variable(*it)) );
91
92 //const TQString name = info->objectName(); //info->groupName();
93 //this->appendChild( TDESharedPtr<KoMacro::Variable>(new KoMacro::Variable(name)) );
94 }
95 const TQString n =
96 namelist.contains(partitem)
97 ? partitem
98 : namelist.count() > 0 ? namelist[0] : "";
99 this->setVariant(n);
100 kdDebug()<<"##################### KexiActions::ObjectVariable() variant="<<this->variant()<<endl;
101 }
102 };
103
104}
105
107 : KexiAction("datatable", i18n("Data Table"))
108{
109 setVariable(TDESharedPtr<KoMacro::Variable>( new MethodVariable<DataTableAction>(this) ));
110 setVariable(TDESharedPtr<KoMacro::Variable>( new TypeVariable<DataTableAction>(this) ));
111 setVariable(TDESharedPtr<KoMacro::Variable>( new PartItemVariable<DataTableAction>(this) ));
112}
113
117
118bool DataTableAction::notifyUpdated(TDESharedPtr<KoMacro::MacroItem> macroitem, const TQString& name)
119{
120 kdDebug()<<"DataTableAction::notifyUpdated() name="<<name<<" macroitem.action="<<(macroitem->action() ? macroitem->action()->name() : "NOACTION")<<endl;
121 /*
122 TDESharedPtr<KoMacro::Variable> variable = macroitem->variable(name, false);
123 if(! variable) {
124 kdWarning()<<"DataTableAction::notifyUpdated() No such variable="<<name<<" in macroitem."<<endl;
125 return false;
126 }
127 variable->clearChildren();
128 if(name == "method") {
129 const int partitem = macroitem->variant(OBJECT, true).toString();
130 macroitem->variable(OBJECT, true)->setChildren(
131 KoMacro::Variable::List() << TDESharedPtr<KoMacro::Variable>(new ObjectVariable<ExecuteAction>(this, partitem)) );
132 }
133 */
134 return true;
135}
136
137void DataTableAction::activate(TDESharedPtr<KoMacro::Context> context)
138{
139 if(! mainWin()->project()) {
140 kdWarning() << "ExecuteAction::activate(TDESharedPtr<KoMacro::Context>) Invalid project" << endl;
141 return;
142 }
143
144 const TQString method = context->variable("method")->variant().toString();
145 const TQString type = context->variable("type")->variant().toString();
146
147 const TQString partitem = context->variable("partitem")->variant().toString();
148 TQString identifier;
149 if(! partitem.isEmpty()) {
150 TQStringList parts = TQStringList::split(".", partitem);
151 KexiPart::Part* part = Kexi::partManager().partForMimeType( TQString("kexi/%1").arg(parts[0]) );
152 KexiPart::Item* item = part ? mainWin()->project()->item(part->info(), parts[1]) : 0;
153 if(! item)
154 throw KoMacro::Exception(i18n("No such item \"%1\"").arg(partitem));
155 identifier = TQString::number(item->identifier());
156 }
157
158 TQMap<TQString,TQString> args;
159 if(! identifier.isNull())
160 args.insert("itemId", identifier);
161
162 if(method == "import") {
163 args.insert("sourceType", type);
165 "csv_importexport", "KexiCSVImportDialog", 0, mainWin(), 0, &args);
166 if (!dlg)
167 return; //error msg has been shown by KexiInternalPart
168 dlg->exec();
169 delete dlg;
170 }
171 else if(method == "export") {
172 args.insert("destinationType", type);
174 "csv_importexport", "KexiCSVExportWizard", 0, mainWin(), 0, &args);
175 if (!dlg)
176 return; //error msg has been shown by KexiInternalPart
177 dlg->exec();
178 delete dlg;
179 }
180 else {
181 throw KoMacro::Exception(i18n("No such method \"%1\"").arg(method));
182 }
183}
184
185//#include "executeaction.moc"
static TQDialog * createModalDialogInstance(const char *partName, const char *dialogClass, KexiDB::MessageHandler *msgHdr, KexiMainWindow *mainWin, const char *objName=0, TQMap< TQString, TQString > *args=0)
virtual bool notifyUpdated(TDESharedPtr< KoMacro::MacroItem > macroitem, const TQString &name)
This function is called, when the KoMacro::Variable with name name used within the KoMacro::MacroItem...
virtual void activate(TDESharedPtr< KoMacro::Context > context)
Called if the Action should be executed within the defined context .
virtual ~DataTableAction()
Destructor.
KexiMainWindow * mainWin() const
KexiAction(const TQString &name, const TQString &text)
Constructor.
Template class to offer common functionality needed by all KoMacro::Variable implementations Kexi pro...
KexiVariable(ACTIONIMPL *actionimpl, const TQString &name, const TQString &caption)
Constructor.
virtual KexiProject * project()=0
Project data of currently opened project or NULL if no project here yet.
Information about a single object that can be instantiated using Kexi Part.
Part * partForMimeType(const TQString &mimeTypt)
The main class for kexi frontend parts like tables, queries, forms and reports.
Definition kexipart.h:66
KexiPart::Item * item(KexiPart::Info *i, const TQString &name)
const TQString name() const
The name this Action has.
Definition action.cpp:95
void setVariable(TDESharedPtr< Variable > variable)
Append the Variable variable to list of variables this Action provides.
Definition action.cpp:145
Base Exception class.
Definition exception.h:35
void appendChild(TDESharedPtr< Variable > variable)
Append a Variable to the list of children this Variable has.
Definition variable.cpp:221
void setVariant(const TQVariant &variant, bool detecttype=true)
Set the TQVariant.
Definition variable.cpp:165
const TQVariant variant() const
Definition variable.cpp:157
KEXICORE_EXPORT KexiPart::Manager & partManager()
shared part manager
Definition kexi.cpp:97