openaction.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 "openaction.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
36 static const TQString DATAVIEW = "data";
37 static const TQString DESIGNVIEW = "design";
38 static const TQString TEXTVIEW = "text";
39
40 static const TQString OBJECT = "object";
41 static const TQString NAME = "name";
42 static const TQString VIEW = "view";
43
48 template<class ACTIONIMPL>
49 class ViewVariable : public KexiVariable<ACTIONIMPL>
50 {
51 public:
52 ViewVariable(ACTIONIMPL* actionimpl, const TQString& objectname = TQString(), const TQString& viewname = TQString())
53 : KexiVariable<ACTIONIMPL>(actionimpl, VIEW, i18n("View"))
54 {
55 TQStringList namelist;
56 KexiPart::Part* part = Kexi::partManager().partForMimeType( TQString("kexi/%1").arg(objectname) );
57 if(part) {
58 int viewmodes = part->supportedViewModes();
59 if(viewmodes & Kexi::DataViewMode)
60 namelist << DATAVIEW;
61 if(viewmodes & Kexi::DesignViewMode)
62 namelist << DESIGNVIEW;
63 if(viewmodes & Kexi::TextViewMode)
64 namelist << TEXTVIEW;
65 for(TQStringList::Iterator it = namelist.begin(); it != namelist.end(); ++it)
66 this->children().append( TDESharedPtr<KoMacro::Variable>(new KoMacro::Variable(*it)) );
67 }
68 const TQString n =
69 namelist.contains(viewname)
70 ? viewname
71 : namelist.count() > 0 ? namelist[0] : "";
72
73 this->setVariant(n);
74 }
75 };
76
77}
78
80 : KexiAction("open", i18n("Open"))
81{
82 const int conditions = ObjectVariable<OpenAction>::VisibleInNav;
83
84 TDESharedPtr<KoMacro::Variable> objvar = new ObjectVariable<OpenAction>(this, conditions);
85 setVariable(objvar);
86
87 setVariable(TDESharedPtr<KoMacro::Variable>( new ObjectNameVariable<OpenAction>(this, objvar->variant().toString()) ));
88 setVariable(TDESharedPtr<KoMacro::Variable>( new ViewVariable<OpenAction>(this, objvar->variant().toString()) ));
89}
90
94
95bool OpenAction::notifyUpdated(TDESharedPtr<KoMacro::MacroItem> macroitem, const TQString& name)
96{
97 kdDebug()<<"OpenAction::notifyUpdated() name="<<name<<" macroitem.action="<<(macroitem->action() ? macroitem->action()->name() : "NOACTION")<<endl;
98 TDESharedPtr<KoMacro::Variable> variable = macroitem->variable(name, false);
99 if(! variable) {
100 kdWarning()<<"OpenAction::notifyUpdated() No such variable="<<name<<" in macroitem."<<endl;
101 return false;
102 }
103
104 variable->clearChildren();
105 if(name == OBJECT) {
106 const TQString objectvalue = macroitem->variant(OBJECT, true).toString(); // e.g. "table" or "query"
107 const TQString objectname = macroitem->variant(NAME, true).toString(); // e.g. "table1" or "table2" if objectvalue above is "table"
108 const TQString viewname = macroitem->variant(VIEW, true).toString(); // "data", "design" or "text"
109
110 macroitem->variable(NAME, true)->setChildren(
111 KoMacro::Variable::List() << TDESharedPtr<KoMacro::Variable>(new ObjectNameVariable<OpenAction>(this, objectvalue, objectname)) );
112 macroitem->variable(VIEW, true)->setChildren(
113 KoMacro::Variable::List() << TDESharedPtr<KoMacro::Variable>(new ViewVariable<OpenAction>(this, objectvalue, viewname)) );
114 }
115
116 return true;
117}
118
119void OpenAction::activate(TDESharedPtr<KoMacro::Context> context)
120{
121 if(! mainWin()->project()) {
122 throw KoMacro::Exception(i18n("No project loaded."));
123 }
124
125 const TQString objectname = context->variable(OBJECT)->variant().toString();
126 const TQString name = context->variable(NAME)->variant().toString();
127 KexiPart::Item* item = mainWin()->project()->itemForMimeType( TQString("kexi/%1").arg(objectname).latin1(), name );
128 if(! item) {
129 throw KoMacro::Exception(i18n("No such object \"%1.%2\".").arg(objectname).arg(name));
130 }
131
132 // Determinate the viewmode.
133 const TQString view = context->variable(VIEW)->variant().toString();
134 int viewmode;
135 if(view == DATAVIEW)
136 viewmode = Kexi::DataViewMode;
137 else if(view == DESIGNVIEW)
138 viewmode = Kexi::DesignViewMode;
139 else if(view == TEXTVIEW)
140 viewmode = Kexi::TextViewMode;
141 else {
142 throw KoMacro::Exception(i18n("No such viewmode \"%1\" in object \"%2.%3\".").arg(view).arg(objectname).arg(name));
143 }
144
145 // Try to open the object now.
146 bool openingCancelled;
147 if(! mainWin()->openObject(item, viewmode, openingCancelled)) {
148 if(! openingCancelled) {
149 throw KoMacro::Exception(i18n("Failed to open object \"%1.%2\".").arg(objectname).arg(name));
150 }
151 }
152}
153
154//#include "openaction.moc"
Template class to offer common functionality needed by all KoMacro::Action implementations Kexi provi...
Definition kexiaction.h:44
KexiMainWindow * mainWin() const
Template class to offer common functionality needed by all KoMacro::Variable implementations Kexi pro...
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 void activate(TDESharedPtr< KoMacro::Context > context)
Called if the Action should be executed within the defined context .
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...
OpenAction()
Constructor.
virtual ~OpenAction()
Destructor.
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
int supportedViewModes() const
Definition kexipart.h:98
KexiPart::Item * itemForMimeType(const TQCString &mimeType, 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
A variable value used to provide abstract access to variables.
Definition variable.h:39
void setVariant(const TQVariant &variant, bool detecttype=true)
Set the TQVariant.
Definition variable.cpp:165
TQValueList< TDESharedPtr< Variable > > List
Property to get and set a TQVariant as variable.
Definition variable.h:61
List children() const
Definition variable.cpp:216
KEXICORE_EXPORT KexiPart::Manager & partManager()
shared part manager
Definition kexi.cpp:97
@ TextViewMode
Also known as SQL View Mode.
Definition kexi.h:48