context.cpp
1/***************************************************************************
2 * This file is part of the KDE project
3 * copyright (C) 2005 by Sebastian Sauer (mail@dipe.org)
4 * copyright (C) 2005 by Tobi Krebs (tobi.krebs@gmail.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this program; see the file COPYING. 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 "context.h"
21#include "action.h"
22#include "macro.h"
23#include "macroitem.h"
24#include "exception.h"
25
26//#include <tqtimer.h>
27#include <kdebug.h>
28
29using namespace KoMacro;
30
31namespace KoMacro {
32
37 class Context::Private
38 {
39 public:
40
44 TDESharedPtr<Macro> macro;
45
50 TQValueList<TDESharedPtr<MacroItem > > items;
51
56 TDESharedPtr<MacroItem> macroitem;
57
62 TQMap<TQString, TDESharedPtr<Variable > > variables;
63
68 Exception* exception;
69
71 explicit Private(TDESharedPtr<Macro> m)
72 : macro(m) // remember the macro
73 , items(m->items()) // set d-pointer children to macro children
74 , exception(0) // no exception yet.
75 {
76 }
77
79 ~Private()
80 {
81 delete exception;
82 }
83 };
84
85}
86//Constructor with initialization of our Private.object (d-pointer)
87Context::Context(TDESharedPtr<Macro> macro)
88 : TQObject()
89 , d( new Private(macro) ) // create the private d-pointer instance.
90{
91}
92
93//Destructor.
95{
96 delete d;
97}
98
99//return if we have (d-pointer) variables
100bool Context::hasVariable(const TQString& name) const
101{
102 //Use TQMap?s contains to check if a variable with name exists
103 return d->variables.contains(name);
104}
105
106//return variable with name or throw an exception if none is found in variables
107TDESharedPtr<Variable> Context::variable(const TQString& name) const
108{
109 //Use TQMap?s contains to check if a variable with name exists in context
110 if (d->variables.contains(name)) {
111 //return it
112 return d->variables[name];
113 }
114 //if there is a macroitem try to get variable from it
115 if(d->macroitem.data()) {
116 TDESharedPtr<Variable> v = d->macroitem->variable(name, true);
117 if(v.data()) {
118 return v;
119 }
120 }
121 //none found throw exception
122 throw Exception(TQString("Variable name='%1' does not exist.").arg(name));
123}
124
125//return a map of our (d-pointer) variables
127{
128 return d->variables;
129}
130
131//set a variable
132void Context::setVariable(const TQString& name, TDESharedPtr<Variable> variable)
133{
134 //debuging infos
135 kdDebug() << TQString("KoMacro::Context::setVariable name='%1' variable='%2'").arg(name).arg(variable->toString()) << endl;
136 //Use TQMap?s replace to set/replace the variable named name
137 d->variables.replace(name, variable);
138}
139
140//return the associated Macro
141TDESharedPtr<Macro> Context::macro() const
142{
143 return d->macro;
144}
145
146//return the currently selected MacroItem
147TDESharedPtr<MacroItem> Context::macroItem() const
148{
149 return d->macroitem;
150}
151
152//return if this context had an exception
154{
155 return d->exception != 0;
156}
157
158//return the (d-pointer) exception
160{
161 return d->exception;
162}
163
164//try to activate all action?s in this context
165void Context::activate(TQValueList<TDESharedPtr<MacroItem > >::ConstIterator it)
166{
167 //debuging infos
168 kdDebug() << "Context::activate()" << endl;
169 //TQ_ASSIGN(d->macro);
170
171 //set end to constEnd
172 TQValueList<TDESharedPtr<MacroItem > >::ConstIterator end(d->items.constEnd());
173 //loop through actions
174 for(;it != end; ++it) {
175 // fetch the MacroItem we are currently pointing to.
176 d->macroitem = TDESharedPtr<MacroItem>(*it);
177 //skip empty macroitems
178 if(! d->macroitem.data()) {
179 kdDebug() << "Context::activate() Skipping empty MacroItem" << endl;
180 continue;
181 }
182
183 // fetch the Action, the MacroItem points to.
184 TDESharedPtr<Action> action = d->macroitem->action();
185 //skip macroitems without an action
186 if(! action.data()) {
187 kdDebug() << "Context::activate() Skipping MacroItem with no action" << endl;
188 continue;
189 }
190
191 try {
192 // activate the action
193 action->activate(this);
194 }
195 //catch exceptions
196 catch(Exception& e) {
197 //create a new exception from caugth one and set internal exception
198 d->exception = new Exception(e);
199 //add new tracemessages
200 //the macro name
201 d->exception->addTraceMessage( TQString("macro=%1").arg(d->macro->name()) );
202 //the action name
203 d->exception->addTraceMessage( TQString("action=%1").arg(action->name()) );
204 //and all variables wich belong to the action/macro
205 TQStringList variables = action->variableNames();
206 for(TQStringList::Iterator vit = variables.begin(); vit != variables.end(); ++vit) {
207 TDESharedPtr<Variable> v = d->macroitem->variable(*vit, true);
208 d->exception->addTraceMessage( TQString("%1=%2").arg(*vit).arg(v->toString()) );
209 }
210 return; // abort execution
211 }
212 }
213
214 // The run is done. So, let's remove the currently selected item to
215 // outline, that we did the job and there stays no dangling item.
216 d->macroitem = TDESharedPtr<MacroItem>(0);
217}
218
219//try to activated an context
220void Context::activate(TDESharedPtr<Context> context)
221{
222 //setup context
223 delete d->exception; d->exception = 0;
224
225 if(context->hadException()) {
226 // if the context in which this context should run in already had an exception,
227 // we adopt this exception and abort the execution.
228 d->exception = new Exception( *context->exception() );
229 return;
230 }
231
232 // Merge the passed context into this context
233 Variable::Map variables = context->variables();
234 //copy variables
235 Variable::Map::ConstIterator it, end( variables.constEnd() );
236 for( it = variables.constBegin(); it != end; ++it)
237 setVariable(it.key(), it.data());
238
239 //activate copied context.
240 activate(d->items.constBegin());
241}
242
243//try to continue activation of a context
245{
246 //setup/clear context,
247 //allows us to continue activation even when an exception happend before
248 delete d->exception; d->exception = 0;
249
250 if(! d->macroitem) { // if no MacroItem is defined, we don't need to try to continue execution
251 return;
252 }
253
254 //find the macroitem from which to continue
255 TQValueList<TDESharedPtr<MacroItem > >::ConstIterator it = d->items.find(d->macroitem);
256 if (it != d->items.constEnd()) {
257 activate(++it); // try to continue the execution.
258 }
259}
260
261#include "context.moc"
Variable::Map variables() const
Definition context.cpp:126
TDESharedPtr< Variable > variable(const TQString &name) const
Definition context.cpp:107
~Context()
Destructor.
Definition context.cpp:94
virtual void activateNext()
This slot continues execution.
Definition context.cpp:244
bool hadException() const
Definition context.cpp:153
TDESharedPtr< Macro > macro() const
Definition context.cpp:141
Exception * exception() const
Definition context.cpp:159
bool hasVariable(const TQString &name) const
Definition context.cpp:100
TDESharedPtr< MacroItem > macroItem() const
Definition context.cpp:147
Context(TDESharedPtr< Macro > macro)
Constructor.
Definition context.cpp:87
void setVariable(const TQString &name, TDESharedPtr< Variable > variable)
Set the variable variable defined with name name .
Definition context.cpp:132
Base Exception class.
Definition exception.h:35
TQMap< TQString, TDESharedPtr< Variable > > Map
A map of variables.
Definition variable.h:66