kexi

executeaction.cpp
1/***************************************************************************
2 * This file is part of the KDE project
3 * copyright (C) 2006 by Sebastian Sauer (mail@dipe.org)
4 * copyright (C) 2006 by Bernd Steindorff (bernd@itii.de)
5 * copyright (C) 2006 by Sascha Kupper (kusato@kfnv.de)
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this program; see the file COPYING. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 ***************************************************************************/
20
21#include "executeaction.h"
22
23#include <core/kexi.h>
24#include <core/kexiproject.h>
25#include <core/kexipartmanager.h>
26#include <core/kexipartinfo.h>
27#include <core/kexipart.h>
28#include <core/keximainwindow.h>
29
30#include <tdelocale.h>
31
32using namespace KexiMacro;
33
34namespace KexiMacro {
35 static const TQString OBJECT = "object";
36 static const TQString NAME = "name";
37}
38
40 : KexiAction("execute", i18n("Execute"))
41{
42 int conditions = ObjectVariable<ExecuteAction>::VisibleInNav | ObjectVariable<ExecuteAction>::Executable;
43 TDESharedPtr<KoMacro::Variable> objvar = new ObjectVariable<ExecuteAction>(this, conditions);
44 setVariable(objvar);
45
46 setVariable(TDESharedPtr<KoMacro::Variable>( new ObjectNameVariable<ExecuteAction>(this, objvar->variant().toString()) ));
47}
48
52
53bool ExecuteAction::notifyUpdated(TDESharedPtr<KoMacro::MacroItem> macroitem, const TQString& name)
54{
55 kdDebug()<<"ExecuteAction::notifyUpdated() name="<<name<<" macroitem.action="<<(macroitem->action() ? macroitem->action()->name() : "NOACTION")<<endl;
56 TDESharedPtr<KoMacro::Variable> variable = macroitem->variable(name, false);
57 if(! variable) {
58 kdWarning()<<"ExecuteAction::notifyUpdated() No such variable="<<name<<" in macroitem."<<endl;
59 return false;
60 }
61
62 variable->clearChildren();
63 if(name == OBJECT) {
64 const TQString objectvalue = macroitem->variant(OBJECT, true).toString(); // e.g. "macro" or "script"
65 const TQString objectname = macroitem->variant(NAME, true).toString(); // e.g. "macro1" or "macro2" if objectvalue above is "macro"
66 macroitem->variable(NAME, true)->setChildren(
67 KoMacro::Variable::List() << TDESharedPtr<KoMacro::Variable>(new ObjectNameVariable<ExecuteAction>(this, objectvalue, objectname)) );
68 }
69
70 return true;
71}
72
73void ExecuteAction::activate(TDESharedPtr<KoMacro::Context> context)
74{
75 if(! mainWin()->project()) {
76 kdWarning() << "ExecuteAction::activate(TDESharedPtr<KoMacro::Context>) Invalid project" << endl;
77 return;
78 }
79
80 const TQString mimetype = TQString("kexi/%1").arg( context->variable("object")->variant().toString() );
81 const TQString name = context->variable("name")->variant().toString();
82
84 if(! part) {
85 throw KoMacro::Exception(i18n("No such mimetype \"%1\"").arg(mimetype));
86 }
87
88 KexiPart::Item* item = mainWin()->project()->item(part->info(), name);
89 if(! item) {
90 throw KoMacro::Exception(i18n("Failed to open part \"%1\" for mimetype \"%2\"").arg(name).arg(mimetype));
91 }
92
93 part->execute(item);
94}
95
96//#include "executeaction.moc"
virtual ~ExecuteAction()
Destructor.
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 .
KexiMainWindow * mainWin() const
KexiAction(const TQString &name, const TQString &text)
Constructor.
The ViewVariable class provide a list of KexiPart::PartItem's supported by a KexiPart::Part as KoMacr...
The ObjectVariable class implements KoMacro::Variable to provide a variable list of Kexi-objects.
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
virtual bool execute(KexiPart::Item *item, TQObject *sender=0)
Definition kexipart.h:85
KexiPart::Item * item(KexiPart::Info *i, const TQString &name)
TDESharedPtr< Variable > variable(const TQString &name) const
Definition action.cpp:130
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
TQValueList< TDESharedPtr< Variable > > List
Property to get and set a TQVariant as variable.
Definition variable.h:61
KEXICORE_EXPORT KexiPart::Manager & partManager()
shared part manager
Definition kexi.cpp:97