kexi

executeaction.cpp

00001 /***************************************************************************
00002  * This file is part of the KDE project
00003  * copyright (C) 2006 by Sebastian Sauer (mail@dipe.org)
00004  * copyright (C) 2006 by Bernd Steindorff (bernd@itii.de)
00005  * copyright (C) 2006 by Sascha Kupper (kusato@kfnv.de)
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Library General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2 of the License, or (at your option) any later version.
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  * You should have received a copy of the GNU Library General Public License
00016  * along with this program; see the file COPYING.  If not, write to
00017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019  ***************************************************************************/
00020 
00021 #include "executeaction.h"
00022 
00023 #include <core/kexi.h>
00024 #include <core/kexiproject.h>
00025 #include <core/kexipartmanager.h>
00026 #include <core/kexipartinfo.h>
00027 #include <core/kexipart.h>
00028 #include <core/keximainwindow.h>
00029 
00030 #include <klocale.h>
00031 
00032 using namespace KexiMacro;
00033 
00034 namespace KexiMacro {
00035     static const QString OBJECT = "object";
00036     static const QString NAME = "name";
00037 }
00038 
00039 ExecuteAction::ExecuteAction()
00040     : KexiAction("execute", i18n("Execute"))
00041 {
00042     int conditions = ObjectVariable<ExecuteAction>::VisibleInNav | ObjectVariable<ExecuteAction>::Executable;
00043     KSharedPtr<KoMacro::Variable> objvar = new ObjectVariable<ExecuteAction>(this, conditions);
00044     setVariable(objvar);
00045 
00046     setVariable(KSharedPtr<KoMacro::Variable>( new ObjectNameVariable<ExecuteAction>(this, objvar->variant().toString()) ));
00047 }
00048 
00049 ExecuteAction::~ExecuteAction() 
00050 {
00051 }
00052 
00053 bool ExecuteAction::notifyUpdated(KSharedPtr<KoMacro::MacroItem> macroitem, const QString& name)
00054 {
00055     kdDebug()<<"ExecuteAction::notifyUpdated() name="<<name<<" macroitem.action="<<(macroitem->action() ? macroitem->action()->name() : "NOACTION")<<endl;
00056     KSharedPtr<KoMacro::Variable> variable = macroitem->variable(name, false);
00057     if(! variable) {
00058         kdWarning()<<"ExecuteAction::notifyUpdated() No such variable="<<name<<" in macroitem."<<endl;
00059         return false;
00060     }
00061 
00062     variable->clearChildren();
00063     if(name == OBJECT) {
00064         const QString objectvalue = macroitem->variant(OBJECT, true).toString(); // e.g. "macro" or "script"
00065         const QString objectname = macroitem->variant(NAME, true).toString(); // e.g. "macro1" or "macro2" if objectvalue above is "macro"
00066         macroitem->variable(NAME, true)->setChildren(
00067             KoMacro::Variable::List() << KSharedPtr<KoMacro::Variable>(new ObjectNameVariable<ExecuteAction>(this, objectvalue, objectname)) );
00068     }
00069 
00070     return true;
00071 }
00072 
00073 void ExecuteAction::activate(KSharedPtr<KoMacro::Context> context)
00074 {
00075     if(! mainWin()->project()) {
00076         kdWarning() << "ExecuteAction::activate(KSharedPtr<KoMacro::Context>) Invalid project" << endl;
00077         return;
00078     }
00079 
00080     const QString mimetype = QString("kexi/%1").arg( context->variable("object")->variant().toString() );
00081     const QString name = context->variable("name")->variant().toString();
00082 
00083     KexiPart::Part* part = Kexi::partManager().partForMimeType(mimetype);
00084     if(! part) {
00085         throw KoMacro::Exception(i18n("No such mimetype \"%1\"").arg(mimetype));
00086     }
00087 
00088     KexiPart::Item* item = mainWin()->project()->item(part->info(), name);
00089     if(! item) {
00090         throw KoMacro::Exception(i18n("Failed to open part \"%1\" for mimetype \"%2\"").arg(name).arg(mimetype));
00091     }
00092 
00093     part->execute(item);
00094 }
00095 
00096 //#include "executeaction.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys