kexi
context.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "context.h"
00021 #include "action.h"
00022 #include "macro.h"
00023 #include "macroitem.h"
00024 #include "exception.h"
00025
00026
00027 #include <kdebug.h>
00028
00029 using namespace KoMacro;
00030
00031 namespace KoMacro {
00032
00037 class Context::Private
00038 {
00039 public:
00040
00044 KSharedPtr<Macro> macro;
00045
00050 QValueList<KSharedPtr<MacroItem > > items;
00051
00056 KSharedPtr<MacroItem> macroitem;
00057
00062 QMap<QString, KSharedPtr<Variable > > variables;
00063
00068 Exception* exception;
00069
00071 explicit Private(KSharedPtr<Macro> m)
00072 : macro(m)
00073 , items(m->items())
00074 , exception(0)
00075 {
00076 }
00077
00079 ~Private()
00080 {
00081 delete exception;
00082 }
00083 };
00084
00085 }
00086
00087 Context::Context(KSharedPtr<Macro> macro)
00088 : QObject()
00089 , d( new Private(macro) )
00090 {
00091 }
00092
00093
00094 Context::~Context()
00095 {
00096 delete d;
00097 }
00098
00099
00100 bool Context::hasVariable(const QString& name) const
00101 {
00102
00103 return d->variables.contains(name);
00104 }
00105
00106
00107 KSharedPtr<Variable> Context::variable(const QString& name) const
00108 {
00109
00110 if (d->variables.contains(name)) {
00111
00112 return d->variables[name];
00113 }
00114
00115 if(d->macroitem.data()) {
00116 KSharedPtr<Variable> v = d->macroitem->variable(name, true);
00117 if(v.data()) {
00118 return v;
00119 }
00120 }
00121
00122 throw Exception(QString("Variable name='%1' does not exist.").arg(name));
00123 }
00124
00125
00126 Variable::Map Context::variables() const
00127 {
00128 return d->variables;
00129 }
00130
00131
00132 void Context::setVariable(const QString& name, KSharedPtr<Variable> variable)
00133 {
00134
00135 kdDebug() << QString("KoMacro::Context::setVariable name='%1' variable='%2'").arg(name).arg(variable->toString()) << endl;
00136
00137 d->variables.replace(name, variable);
00138 }
00139
00140
00141 KSharedPtr<Macro> Context::macro() const
00142 {
00143 return d->macro;
00144 }
00145
00146
00147 KSharedPtr<MacroItem> Context::macroItem() const
00148 {
00149 return d->macroitem;
00150 }
00151
00152
00153 bool Context::hadException() const
00154 {
00155 return d->exception != 0;
00156 }
00157
00158
00159 Exception* Context::exception() const
00160 {
00161 return d->exception;
00162 }
00163
00164
00165 void Context::activate(QValueList<KSharedPtr<MacroItem > >::ConstIterator it)
00166 {
00167
00168 kdDebug() << "Context::activate()" << endl;
00169
00170
00171
00172 QValueList<KSharedPtr<MacroItem > >::ConstIterator end(d->items.constEnd());
00173
00174 for(;it != end; ++it) {
00175
00176 d->macroitem = KSharedPtr<MacroItem>(*it);
00177
00178 if(! d->macroitem.data()) {
00179 kdDebug() << "Context::activate() Skipping empty MacroItem" << endl;
00180 continue;
00181 }
00182
00183
00184 KSharedPtr<Action> action = d->macroitem->action();
00185
00186 if(! action.data()) {
00187 kdDebug() << "Context::activate() Skipping MacroItem with no action" << endl;
00188 continue;
00189 }
00190
00191 try {
00192
00193 action->activate(this);
00194 }
00195
00196 catch(Exception& e) {
00197
00198 d->exception = new Exception(e);
00199
00200
00201 d->exception->addTraceMessage( QString("macro=%1").arg(d->macro->name()) );
00202
00203 d->exception->addTraceMessage( QString("action=%1").arg(action->name()) );
00204
00205 QStringList variables = action->variableNames();
00206 for(QStringList::Iterator vit = variables.begin(); vit != variables.end(); ++vit) {
00207 KSharedPtr<Variable> v = d->macroitem->variable(*vit, true);
00208 d->exception->addTraceMessage( QString("%1=%2").arg(*vit).arg(v->toString()) );
00209 }
00210 return;
00211 }
00212 }
00213
00214
00215
00216 d->macroitem = KSharedPtr<MacroItem>(0);
00217 }
00218
00219
00220 void Context::activate(KSharedPtr<Context> context)
00221 {
00222
00223 delete d->exception; d->exception = 0;
00224
00225 if(context->hadException()) {
00226
00227
00228 d->exception = new Exception( *context->exception() );
00229 return;
00230 }
00231
00232
00233 Variable::Map variables = context->variables();
00234
00235 Variable::Map::ConstIterator it, end( variables.constEnd() );
00236 for( it = variables.constBegin(); it != end; ++it)
00237 setVariable(it.key(), it.data());
00238
00239
00240 activate(d->items.constBegin());
00241 }
00242
00243
00244 void Context::activateNext()
00245 {
00246
00247
00248 delete d->exception; d->exception = 0;
00249
00250 if(! d->macroitem) {
00251 return;
00252 }
00253
00254
00255 QValueList<KSharedPtr<MacroItem > >::ConstIterator it = d->items.find(d->macroitem);
00256 if (it != d->items.constEnd()) {
00257 activate(++it);
00258 }
00259 }
00260
00261 #include "context.moc"
|