kexi

datatableaction.cpp

00001 /***************************************************************************
00002  * This file is part of the KDE project
00003  * copyright (C) 2006 by Sebastian Sauer (mail@dipe.org)
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Library General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2 of the License, or (at your option) any later version.
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Library General Public License for more details.
00013  * You should have received a copy of the GNU Library General Public License
00014  * along with this program; see the file COPYING.  If not, write to
00015  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016  * Boston, MA 02110-1301, USA.
00017  ***************************************************************************/
00018 
00019 #include "datatableaction.h"
00020 //#include "objectvariable.h"
00021 
00022 #include <core/kexi.h>
00023 #include <core/kexiproject.h>
00024 #include <core/kexipartmanager.h>
00025 #include <core/kexipartinfo.h>
00026 #include <core/kexipart.h>
00027 #include <core/keximainwindow.h>
00028 #include <core/kexiinternalpart.h>
00029 
00030 #include <klocale.h>
00031 
00032 using namespace KexiMacro;
00033 
00034 namespace KexiMacro {
00035 
00036     //static const QString OBJECT = "method";
00037     //static const QString OBJECT = "type";
00038     //static const QString OBJECT = "partitem";
00039 
00040     template<class ACTIONIMPL>
00041     class MethodVariable : public KexiVariable<ACTIONIMPL>
00042     {
00043         public:
00044             MethodVariable(ACTIONIMPL* actionimpl)
00045                 : KexiVariable<ACTIONIMPL>(actionimpl, "method", i18n("Method"))
00046             {
00047                 QStringList list;
00048                 list << "import" << "export";
00049                 this->appendChild( KSharedPtr<KoMacro::Variable>( new KoMacro::Variable(list, "@list") ) );
00050 
00051                 this->setVariant( list[0] );
00052             }
00053     };
00054 
00055     template<class ACTIONIMPL>
00056     class TypeVariable : public KexiVariable<ACTIONIMPL>
00057     {
00058         public:
00059             TypeVariable(ACTIONIMPL* actionimpl)
00060                 : KexiVariable<ACTIONIMPL>(actionimpl, "type", i18n("Type"))
00061             {
00062                 QStringList list;
00063                 list << "file" << "clipboard";
00064                 this->appendChild( KSharedPtr<KoMacro::Variable>( new KoMacro::Variable(list, "@list") ) );
00065 
00066                 this->setVariant( list[0] );
00067             }
00068     };
00069 
00070     template<class ACTIONIMPL>
00071     class PartItemVariable : public KexiVariable<ACTIONIMPL>
00072     {
00073         public:
00074             PartItemVariable(ACTIONIMPL* actionimpl, const QString& partitem = QString::null)
00075                 : KexiVariable<ACTIONIMPL>(actionimpl, "partitem", i18n("Item"))
00076             {
00077                 QStringList namelist;
00078                 if(actionimpl->mainWin()->project()) {
00079                     KexiPart::PartInfoList* parts = Kexi::partManager().partInfoList();
00080                     for(KexiPart::PartInfoListIterator it(*parts); it.current(); ++it) {
00081                         KexiPart::Info* info = it.current();
00082                         if(! info->isDataExportSupported())
00083                             continue;
00084                         KexiPart::ItemDict* items = actionimpl->mainWin()->project()->items(info);
00085                         if(items)
00086                             for(KexiPart::ItemDictIterator item_it = *items; item_it.current(); ++item_it)
00087                                 namelist << info->objectName() + "." + item_it.current()->name();
00088                     }
00089                     for(QStringList::Iterator it = namelist.begin(); it != namelist.end(); ++it)
00090                         this->appendChild( KSharedPtr<KoMacro::Variable>(new KoMacro::Variable(*it)) );
00091 
00092                     //const QString name = info->objectName(); //info->groupName();
00093                     //this->appendChild( KSharedPtr<KoMacro::Variable>(new KoMacro::Variable(name)) );
00094                 }
00095                 const QString n =
00096                     namelist.contains(partitem)
00097                         ? partitem
00098                         : namelist.count() > 0 ? namelist[0] : "";
00099                 this->setVariant(n);
00100                 kdDebug()<<"##################### KexiActions::ObjectVariable() variant="<<this->variant()<<endl;
00101             }
00102     };
00103 
00104 }
00105 
00106 DataTableAction::DataTableAction()
00107     : KexiAction("datatable", i18n("Data Table"))
00108 {
00109     setVariable(KSharedPtr<KoMacro::Variable>( new MethodVariable<DataTableAction>(this) ));
00110     setVariable(KSharedPtr<KoMacro::Variable>( new TypeVariable<DataTableAction>(this) ));
00111     setVariable(KSharedPtr<KoMacro::Variable>( new PartItemVariable<DataTableAction>(this) ));
00112 }
00113 
00114 DataTableAction::~DataTableAction() 
00115 {
00116 }
00117 
00118 bool DataTableAction::notifyUpdated(KSharedPtr<KoMacro::MacroItem> macroitem, const QString& name)
00119 {
00120     kdDebug()<<"DataTableAction::notifyUpdated() name="<<name<<" macroitem.action="<<(macroitem->action() ? macroitem->action()->name() : "NOACTION")<<endl;
00121     /*
00122     KSharedPtr<KoMacro::Variable> variable = macroitem->variable(name, false);
00123     if(! variable) {
00124         kdWarning()<<"DataTableAction::notifyUpdated() No such variable="<<name<<" in macroitem."<<endl;
00125         return false;
00126     }
00127     variable->clearChildren();
00128     if(name == "method") {
00129         const int partitem = macroitem->variant(OBJECT, true).toString();
00130         macroitem->variable(OBJECT, true)->setChildren(
00131             KoMacro::Variable::List() << KSharedPtr<KoMacro::Variable>(new ObjectVariable<ExecuteAction>(this, partitem)) );
00132     }
00133     */
00134     return true;
00135 }
00136 
00137 void DataTableAction::activate(KSharedPtr<KoMacro::Context> context)
00138 {
00139     if(! mainWin()->project()) {
00140         kdWarning() << "ExecuteAction::activate(KSharedPtr<KoMacro::Context>) Invalid project" << endl;
00141         return;
00142     }
00143 
00144     const QString method = context->variable("method")->variant().toString();
00145     const QString type = context->variable("type")->variant().toString();
00146 
00147     const QString partitem = context->variable("partitem")->variant().toString();
00148     QString identifier;
00149     if(! partitem.isEmpty()) {
00150         QStringList parts = QStringList::split(".", partitem);
00151         KexiPart::Part* part = Kexi::partManager().partForMimeType( QString("kexi/%1").arg(parts[0]) );
00152         KexiPart::Item* item = part ? mainWin()->project()->item(part->info(), parts[1]) : 0;
00153         if(! item)
00154             throw KoMacro::Exception(i18n("No such item \"%1\"").arg(partitem));
00155         identifier = QString::number(item->identifier());
00156     }
00157 
00158     QMap<QString,QString> args;
00159     if(! identifier.isNull())
00160         args.insert("itemId", identifier);
00161 
00162     if(method == "import") {
00163         args.insert("sourceType", type);
00164         QDialog *dlg = KexiInternalPart::createModalDialogInstance(
00165             "csv_importexport", "KexiCSVImportDialog", 0, mainWin(), 0, &args);
00166         if (!dlg)
00167             return; //error msg has been shown by KexiInternalPart
00168         dlg->exec();
00169         delete dlg;
00170     }
00171     else if(method == "export") {
00172         args.insert("destinationType", type);
00173         QDialog *dlg = KexiInternalPart::createModalDialogInstance(
00174             "csv_importexport", "KexiCSVExportWizard", 0, mainWin(), 0, &args);
00175         if (!dlg)
00176             return; //error msg has been shown by KexiInternalPart
00177         dlg->exec();
00178         delete dlg;
00179     }
00180     else {
00181         throw KoMacro::Exception(i18n("No such method \"%1\"").arg(method));
00182     }
00183 }
00184 
00185 //#include "executeaction.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys