keximacroview.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2006 Sebastian Sauer <mail@dipe.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
12 You should have received a copy of the GNU Library General Public License
13 along with this library; see the file COPYING.LIB. If not, write to
14 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
15 Boston, MA 02110-1301, USA.
16*/
17
18#include "keximacroview.h"
19
20#include <tqdom.h>
21#include <kdebug.h>
22
23#include <kexidialogbase.h>
24#include <kexidb/connection.h>
25#include <kexidb/error.h>
26
27#include <core/kexi.h>
28#include <core/kexiproject.h>
29#include <core/kexipartmanager.h>
30#include <core/kexipartinfo.h>
31
32#include "../lib/macro.h"
33#include "../lib/xmlhandler.h"
34#include "../lib/exception.h"
35
36#include "keximacroerror.h"
37
42class KexiMacroView::Private
43{
44 public:
45
50 TDESharedPtr<KoMacro::Macro> macro;
51
58 Private(KoMacro::Macro* const m)
59 : macro(m)
60 {
61 }
62
63};
64
65KexiMacroView::KexiMacroView(KexiMainWindow *mainwin, TQWidget *parent, KoMacro::Macro* const macro, const char* name)
66 : KexiViewBase(mainwin, parent, (name ? name : "KexiMacroView"))
67 , d( new Private(macro) )
68{
69 //kdDebug() << "KexiMacroView::KexiMacroView() Ctor" << endl;
70 plugSharedAction( "data_execute", this, TQ_SLOT(execute()) );
71}
72
74{
75 //kdDebug() << "KexiMacroView::~KexiMacroView() Dtor" << endl;
76 delete d;
77}
78
79TDESharedPtr<KoMacro::Macro> KexiMacroView::macro() const
80{
81 return d->macro;
82}
83
84tristate KexiMacroView::beforeSwitchTo(int mode, bool& dontstore)
85{
86 kexipluginsdbg << "KexiMacroView::beforeSwitchTo mode=" << mode << " dontstore=" << dontstore << endl;
87 return true;
88}
89
91{
92 kexipluginsdbg << "KexiMacroView::afterSwitchFrom mode=" << mode << endl;
93 loadData(); // reload the data
94 return true;
95}
96
98{
99 d->macro->clearItems();
100
101 TQString data;
102 if(! loadDataBlock(data)) {
103 kexipluginsdbg << "KexiMacroView::loadData(): no DataBlock" << endl;
104 return false;
105 }
106
107 TQString errmsg;
108 int errline, errcol;
109
110 TQDomDocument domdoc;
111 bool parsed = domdoc.setContent(data, false, &errmsg, &errline, &errcol);
112
113 if(! parsed) {
114 kexipluginsdbg << "KexiMacroView::loadData() XML parsing error line: " << errline << " col: " << errcol << " message: " << errmsg << endl;
115 return false;
116 }
117
118 kexipluginsdbg << TQString("KexiMacroView::loadData()\n%1").arg(domdoc.toString()) << endl;
119 TQDomElement macroelem = domdoc.namedItem("macro").toElement();
120 if(macroelem.isNull()) {
121 kexipluginsdbg << "KexiMacroView::loadData() Macro domelement is null" << endl;
122 return false;
123 }
124
125 //kexipluginsdbg << "KexiMacroView::loadData()" << endl;
126 return d->macro->parseXML(macroelem);
127}
128
130{
131 KexiDB::SchemaData *schema = KexiViewBase::storeNewData(sdata, cancel);
132 kexipluginsdbg << "KexiMacroView::storeNewData() new id:" << schema->id() << endl;
133
134 if(!schema || cancel) {
135 delete schema;
136 return 0;
137 }
138
139 if(! storeData()) {
140 kexipluginsdbg << "KexiMacroView::storeNewData() Failed to store the data." << endl;
141 //failure: remove object's schema data to avoid garbage
142 KexiDB::Connection *conn = parentDialog()->mainWin()->project()->dbConnection();
143 conn->removeObject( schema->id() );
144 delete schema;
145 return 0;
146 }
147
148 return schema;
149}
150
152{
153 TQDomDocument domdoc("macros");
154 TQDomElement macroelem = d->macro->toXML();
155 domdoc.appendChild(macroelem);
156 const TQString xml = domdoc.toString(2);
157 const TQString name = TQString("%1 [%2]").arg(parentDialog()->partItem()->name()).arg(parentDialog()->id());
158 kexipluginsdbg << TQString("KexiMacroView::storeData %1\n%2").arg(name).arg(xml) << endl;
159 return storeDataBlock(xml);
160}
161
162void KexiMacroView::execute(TQObject* sender)
163{
164 TDESharedPtr<KoMacro::Context> context = d->macro->execute(sender);
165 if(context->hadException()) {
166 KexiMacroError* error = new KexiMacroError(
167 mainWin(), // The parent KexiMainWindow
168 context // The KoMacro::Context where the error occured.
169 );
170 error->exec();
171 }
172}
173
174#include "keximacroview.moc"
175
void plugSharedAction(const char *action_name, TQObject *receiver, const char *slot)
Provides database connection, allowing queries and data modification.
Definition connection.h:62
bool removeObject(uint objId)
An error dialog used to displayed more detailed informations about a KoMacro::Exception that got thro...
void execute(TQObject *sender=0)
This slot will be invoked if Kexi's menuitem Data=>Execute got activated and will execute the Macro.
virtual tristate beforeSwitchTo(int mode, bool &dontstore)
Called by KexiDialogBase::switchToViewMode() right before dialog is switched to new mode.
TDESharedPtr< KoMacro::Macro > macro() const
virtual KexiDB::SchemaData * storeNewData(const KexiDB::SchemaData &sdata, bool &cancel)
Try to call storeData with new data we like to store.
KexiMacroView(KexiMainWindow *mainwin, TQWidget *parent, ::KoMacro::Macro *const macro, const char *name=0)
Constructor.
virtual bool loadData()
Load the data from XML source and fill the internally used KoMacro::Macro instance.
virtual tristate storeData(bool dontAsk=false)
Try to store the modified data in the already opened and currently used KexiDB::SchemaData instance.
virtual ~KexiMacroView()
Destructor.
virtual tristate afterSwitchFrom(int mode)
Called by KexiDialogBase::switchToViewMode() right after dialog is switched to new mode.
Kexi's main window interface This interface is implemented by KexiMainWindowImpl class.
virtual KexiProject * project()=0
Project data of currently opened project or NULL if no project here yet.
KexiDB::Connection * dbConnection() const
Base class for single view embeddable of in KexiDialogBase.
virtual KexiDB::SchemaData * storeNewData(const KexiDB::SchemaData &sdata, bool &cancel)
bool storeDataBlock(const TQString &dataString, const TQString &dataID=TQString())
bool loadDataBlock(TQString &dataString, const TQString &dataID=TQString(), bool canBeEmpty=false)
KexiDialogBase * parentDialog() const
KexiMainWindow * mainWin() const
The Macro class implements all the action-handling.
Definition macro.h:45
3-state logical type with three values: true, false and cancelled and convenient operators.
Definition tristate.h:100