kexi

openaction.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 "openaction.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 
00036     static const QString DATAVIEW = "data";
00037     static const QString DESIGNVIEW = "design";
00038     static const QString TEXTVIEW = "text";
00039     
00040     static const QString OBJECT = "object";
00041     static const QString NAME = "name";
00042     static const QString VIEW = "view";
00043 
00048     template<class ACTIONIMPL>
00049     class ViewVariable : public KexiVariable<ACTIONIMPL>
00050     {
00051         public:
00052             ViewVariable(ACTIONIMPL* actionimpl, const QString& objectname = QString::null, const QString& viewname = QString::null)
00053                 : KexiVariable<ACTIONIMPL>(actionimpl, VIEW, i18n("View"))
00054             {
00055                 QStringList namelist;
00056                 KexiPart::Part* part = Kexi::partManager().partForMimeType( QString("kexi/%1").arg(objectname) );
00057                 if(part) {
00058                     int viewmodes = part->supportedViewModes();
00059                     if(viewmodes & Kexi::DataViewMode)
00060                         namelist << DATAVIEW;
00061                     if(viewmodes & Kexi::DesignViewMode)
00062                         namelist << DESIGNVIEW;
00063                     if(viewmodes & Kexi::TextViewMode)
00064                         namelist << TEXTVIEW;
00065                     for(QStringList::Iterator it = namelist.begin(); it != namelist.end(); ++it)
00066                         this->children().append( KSharedPtr<KoMacro::Variable>(new KoMacro::Variable(*it)) );
00067                 }
00068                 const QString n =
00069                     namelist.contains(viewname)
00070                         ? viewname
00071                         : namelist.count() > 0 ? namelist[0] : "";
00072 
00073                 this->setVariant(n);
00074             }
00075     };
00076 
00077 }
00078 
00079 OpenAction::OpenAction()
00080     : KexiAction("open", i18n("Open"))
00081 {
00082     const int conditions = ObjectVariable<OpenAction>::VisibleInNav;
00083     
00084     KSharedPtr<KoMacro::Variable> objvar = new ObjectVariable<OpenAction>(this, conditions);
00085     setVariable(objvar);
00086 
00087     setVariable(KSharedPtr<KoMacro::Variable>( new ObjectNameVariable<OpenAction>(this, objvar->variant().toString()) ));
00088     setVariable(KSharedPtr<KoMacro::Variable>( new ViewVariable<OpenAction>(this, objvar->variant().toString()) ));
00089 }
00090 
00091 OpenAction::~OpenAction()
00092 {
00093 }
00094 
00095 bool OpenAction::notifyUpdated(KSharedPtr<KoMacro::MacroItem> macroitem, const QString& name)
00096 {
00097     kdDebug()<<"OpenAction::notifyUpdated() name="<<name<<" macroitem.action="<<(macroitem->action() ? macroitem->action()->name() : "NOACTION")<<endl;
00098     KSharedPtr<KoMacro::Variable> variable = macroitem->variable(name, false);
00099     if(! variable) {
00100         kdWarning()<<"OpenAction::notifyUpdated() No such variable="<<name<<" in macroitem."<<endl;
00101         return false;
00102     }
00103 
00104     variable->clearChildren();
00105     if(name == OBJECT) {
00106         const QString objectvalue = macroitem->variant(OBJECT, true).toString(); // e.g. "table" or "query"
00107         const QString objectname = macroitem->variant(NAME, true).toString(); // e.g. "table1" or "table2" if objectvalue above is "table"
00108         const QString viewname = macroitem->variant(VIEW, true).toString(); // "data", "design" or "text"
00109 
00110         macroitem->variable(NAME, true)->setChildren(
00111             KoMacro::Variable::List() << KSharedPtr<KoMacro::Variable>(new ObjectNameVariable<OpenAction>(this, objectvalue, objectname)) );
00112         macroitem->variable(VIEW, true)->setChildren(
00113             KoMacro::Variable::List() << KSharedPtr<KoMacro::Variable>(new ViewVariable<OpenAction>(this, objectvalue, viewname)) );
00114     }
00115 
00116     return true;
00117 }
00118 
00119 void OpenAction::activate(KSharedPtr<KoMacro::Context> context)
00120 {
00121     if(! mainWin()->project()) {
00122         throw KoMacro::Exception(i18n("No project loaded."));
00123     }
00124 
00125     const QString objectname = context->variable(OBJECT)->variant().toString();
00126     const QString name = context->variable(NAME)->variant().toString();
00127     KexiPart::Item* item = mainWin()->project()->itemForMimeType( QString("kexi/%1").arg(objectname).latin1(), name );
00128     if(! item) {
00129         throw KoMacro::Exception(i18n("No such object \"%1.%2\".").arg(objectname).arg(name));
00130     }
00131 
00132     // Determinate the viewmode.
00133     const QString view = context->variable(VIEW)->variant().toString();
00134     int viewmode;
00135     if(view == DATAVIEW)
00136         viewmode = Kexi::DataViewMode;
00137     else if(view == DESIGNVIEW)
00138         viewmode = Kexi::DesignViewMode;
00139     else if(view == TEXTVIEW)
00140         viewmode = Kexi::TextViewMode;
00141     else {
00142         throw KoMacro::Exception(i18n("No such viewmode \"%1\" in object \"%2.%3\".").arg(view).arg(objectname).arg(name));
00143     }
00144 
00145     // Try to open the object now.
00146     bool openingCancelled;
00147     if(! mainWin()->openObject(item, viewmode, openingCancelled)) {
00148         if(! openingCancelled) {
00149             throw KoMacro::Exception(i18n("Failed to open object \"%1.%2\".").arg(objectname).arg(name));
00150         }
00151     }
00152 }
00153 
00154 //#include "openaction.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys