kexi
keximacroview.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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
00070 plugSharedAction( "data_execute", this, SLOT(execute()) );
00071 }
00072
00073 KexiMacroView::~KexiMacroView()
00074 {
00075
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();
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
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
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 )
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(),
00168 context
00169 );
00170 error->exec();
00171 }
00172 }
00173
00174 #include "keximacroview.moc"
00175
|