kexi

keximacroview.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2006 Sebastian Sauer <mail@dipe.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012    You should have received a copy of the GNU Library General Public License
00013    along with this library; see the file COPYING.LIB.  If not, write to
00014    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00015    Boston, MA 02110-1301, USA.
00016 */
00017 
00018 #include "keximacroview.h"
00019 
00020 #include <qdom.h>
00021 #include <kdebug.h>
00022 
00023 #include <kexidialogbase.h>
00024 #include <kexidb/connection.h>
00025 #include <kexidb/error.h>
00026 
00027 #include <core/kexi.h>
00028 #include <core/kexiproject.h>
00029 #include <core/kexipartmanager.h>
00030 #include <core/kexipartinfo.h>
00031 
00032 #include "../lib/macro.h"
00033 #include "../lib/xmlhandler.h"
00034 #include "../lib/exception.h"
00035 
00036 #include "keximacroerror.h"
00037 
00042 class KexiMacroView::Private
00043 {
00044     public:
00045 
00050         KSharedPtr<KoMacro::Macro> macro;
00051 
00058         Private(KoMacro::Macro* const m)
00059             : macro(m)
00060         {
00061         }
00062 
00063 };
00064 
00065 KexiMacroView::KexiMacroView(KexiMainWindow *mainwin, QWidget *parent, KoMacro::Macro* const macro, const char* name)
00066     : KexiViewBase(mainwin, parent, (name ? name : "KexiMacroView"))
00067     , d( new Private(macro) )
00068 {
00069     //kdDebug() << "KexiMacroView::KexiMacroView() Ctor" << endl;
00070     plugSharedAction( "data_execute", this, SLOT(execute()) );
00071 }
00072 
00073 KexiMacroView::~KexiMacroView()
00074 {
00075     //kdDebug() << "KexiMacroView::~KexiMacroView() Dtor" << endl;
00076     delete d;
00077 }
00078 
00079 KSharedPtr<KoMacro::Macro> KexiMacroView::macro() const
00080 {
00081     return d->macro;
00082 }
00083 
00084 tristate KexiMacroView::beforeSwitchTo(int mode, bool& dontstore)
00085 {
00086     kexipluginsdbg << "KexiMacroView::beforeSwitchTo mode=" << mode << " dontstore=" << dontstore << endl;
00087     return true;
00088 }
00089 
00090 tristate KexiMacroView::afterSwitchFrom(int mode)
00091 {
00092     kexipluginsdbg << "KexiMacroView::afterSwitchFrom mode=" << mode << endl;
00093     loadData(); // reload the data
00094     return true;
00095 }
00096 
00097 bool KexiMacroView::loadData()
00098 {
00099     d->macro->clearItems();
00100 
00101     QString data;
00102     if(! loadDataBlock(data)) {
00103         kexipluginsdbg << "KexiMacroView::loadData(): no DataBlock" << endl;
00104         return false;
00105     }
00106 
00107     QString errmsg;
00108     int errline, errcol;
00109 
00110     QDomDocument domdoc;
00111     bool parsed = domdoc.setContent(data, false, &errmsg, &errline, &errcol);
00112 
00113     if(! parsed) {
00114         kexipluginsdbg << "KexiMacroView::loadData() XML parsing error line: " << errline << " col: " << errcol << " message: " << errmsg << endl;
00115         return false;
00116     }
00117 
00118     kexipluginsdbg << QString("KexiMacroView::loadData()\n%1").arg(domdoc.toString()) << endl;
00119     QDomElement macroelem = domdoc.namedItem("macro").toElement();
00120     if(macroelem.isNull()) {
00121         kexipluginsdbg << "KexiMacroView::loadData() Macro domelement is null" << endl;
00122         return false;
00123     }
00124 
00125     //kexipluginsdbg << "KexiMacroView::loadData()" << endl;
00126     return d->macro->parseXML(macroelem);
00127 }
00128 
00129 KexiDB::SchemaData* KexiMacroView::storeNewData(const KexiDB::SchemaData& sdata, bool &cancel)
00130 {
00131     KexiDB::SchemaData *schema = KexiViewBase::storeNewData(sdata, cancel);
00132     kexipluginsdbg << "KexiMacroView::storeNewData() new id:" << schema->id() << endl;
00133 
00134     if(!schema || cancel) {
00135         delete schema;
00136         return 0;
00137     }
00138 
00139     if(! storeData()) {
00140         kexipluginsdbg << "KexiMacroView::storeNewData() Failed to store the data." << endl;
00141         //failure: remove object's schema data to avoid garbage
00142         KexiDB::Connection *conn = parentDialog()->mainWin()->project()->dbConnection();
00143         conn->removeObject( schema->id() );
00144         delete schema;
00145         return 0;
00146     }
00147 
00148     return schema;
00149 }
00150 
00151 tristate KexiMacroView::storeData(bool /*dontAsk*/)
00152 {
00153     QDomDocument domdoc("macros");
00154     QDomElement macroelem = d->macro->toXML();
00155     domdoc.appendChild(macroelem);
00156     const QString xml = domdoc.toString(2);
00157     const QString name = QString("%1 [%2]").arg(parentDialog()->partItem()->name()).arg(parentDialog()->id());
00158     kexipluginsdbg << QString("KexiMacroView::storeData %1\n%2").arg(name).arg(xml) << endl;
00159     return storeDataBlock(xml);
00160 }
00161 
00162 void KexiMacroView::execute(QObject* sender)
00163 {
00164     KSharedPtr<KoMacro::Context> context = d->macro->execute(sender);
00165     if(context->hadException()) {
00166         KexiMacroError* error = new KexiMacroError(
00167             mainWin(), // The parent KexiMainWindow
00168             context // The KoMacro::Context where the error occured.
00169         );
00170         error->exec();
00171     }
00172 }
00173 
00174 #include "keximacroview.moc"
00175 
KDE Home | KDE Accessibility Home | Description of Access Keys