summaryrefslogtreecommitdiffstats
path: root/kexi/plugins/macros/kexipart/keximacroview.cpp
blob: 507764a77cad647751258a06642b70f330a955a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* This file is part of the KDE project
   Copyright (C) 2006 Sebastian Sauer <mail@dipe.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.
   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.
   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "keximacroview.h"

#include <tqdom.h>
#include <kdebug.h>

#include <kexidialogbase.h>
#include <kexidb/connection.h>
#include <kexidb/error.h>

#include <core/kexi.h>
#include <core/kexiproject.h>
#include <core/kexipartmanager.h>
#include <core/kexipartinfo.h>

#include "../lib/macro.h"
#include "../lib/xmlhandler.h"
#include "../lib/exception.h"

#include "keximacroerror.h"

/**
* \internal d-pointer class to be more flexible on future extension of the
* functionality without to much risk to break the binary compatibility.
*/
class KexiMacroView::Private
{
	public:

		/**
		* The \a KoMacro::Manager instance used to access the
		* Macro Framework.
		*/
		KSharedPtr<KoMacro::Macro> macro;

		/**
		* Constructor.
		*
		* \param m The passed \a KoMacro::Manager instance our
		*        \a manager points to.
		*/
		Private(KoMacro::Macro* const m)
			: macro(m)
		{
		}

};

KexiMacroView::KexiMacroView(KexiMainWindow *mainwin, TQWidget *tqparent, KoMacro::Macro* const macro, const char* name)
	: KexiViewBase(mainwin, tqparent, (name ? name : "KexiMacroView"))
	, d( new Private(macro) )
{
	//kdDebug() << "KexiMacroView::KexiMacroView() Ctor" << endl;
	plugSharedAction( "data_execute", this, TQT_SLOT(execute()) );
}

KexiMacroView::~KexiMacroView()
{
	//kdDebug() << "KexiMacroView::~KexiMacroView() Dtor" << endl;
	delete d;
}

KSharedPtr<KoMacro::Macro> KexiMacroView::macro() const
{
	return d->macro;
}

tristate KexiMacroView::beforeSwitchTo(int mode, bool& dontstore)
{
	kexipluginsdbg << "KexiMacroView::beforeSwitchTo mode=" << mode << " dontstore=" << dontstore << endl;
	return true;
}

tristate KexiMacroView::afterSwitchFrom(int mode)
{
	kexipluginsdbg << "KexiMacroView::afterSwitchFrom mode=" << mode << endl;
	loadData(); // reload the data
	return true;
}

bool KexiMacroView::loadData()
{
	d->macro->clearItems();

	TQString data;
	if(! loadDataBlock(data)) {
		kexipluginsdbg << "KexiMacroView::loadData(): no DataBlock" << endl;
		return false;
	}

	TQString errmsg;
	int errline, errcol;

	TQDomDocument domdoc;
	bool parsed = domdoc.setContent(data, false, &errmsg, &errline, &errcol);

	if(! parsed) {
		kexipluginsdbg << "KexiMacroView::loadData() XML parsing error line: " << errline << " col: " << errcol << " message: " << errmsg << endl;
		return false;
	}

	kexipluginsdbg << TQString("KexiMacroView::loadData()\n%1").tqarg(domdoc.toString()) << endl;
	TQDomElement macroelem = domdoc.namedItem("macro").toElement();
	if(macroelem.isNull()) {
		kexipluginsdbg << "KexiMacroView::loadData() Macro domelement is null" << endl;
		return false;
	}

	//kexipluginsdbg << "KexiMacroView::loadData()" << endl;
	return d->macro->parseXML(macroelem);
}

KexiDB::SchemaData* KexiMacroView::storeNewData(const KexiDB::SchemaData& sdata, bool &cancel)
{
	KexiDB::SchemaData *schema = KexiViewBase::storeNewData(sdata, cancel);
	kexipluginsdbg << "KexiMacroView::storeNewData() new id:" << schema->id() << endl;

	if(!schema || cancel) {
		delete schema;
		return 0;
	}

	if(! storeData()) {
		kexipluginsdbg << "KexiMacroView::storeNewData() Failed to store the data." << endl;
		//failure: remove object's schema data to avoid garbage
		KexiDB::Connection *conn = tqparentDialog()->mainWin()->project()->dbConnection();
		conn->removeObject( schema->id() );
		delete schema;
		return 0;
	}

	return schema;
}

tristate KexiMacroView::storeData(bool /*dontAsk*/)
{
	TQDomDocument domdoc("macros");
	TQDomElement macroelem = d->macro->toXML();
	domdoc.appendChild(macroelem);
	const TQString xml = domdoc.toString(2);
	const TQString name = TQString("%1 [%2]").tqarg(tqparentDialog()->partItem()->name()).tqarg(tqparentDialog()->id());
	kexipluginsdbg << TQString("KexiMacroView::storeData %1\n%2").tqarg(name).tqarg(xml) << endl;
	return storeDataBlock(xml);
}

void KexiMacroView::execute(TQObject* sender)
{
	KSharedPtr<KoMacro::Context> context = d->macro->execute(sender);
	if(context->hadException()) {
		KexiMacroError* error = new KexiMacroError(
			mainWin(), // The tqparent KexiMainWindow
			context // The KoMacro::Context where the error occured.
		);
		error->exec();
	}
}

#include "keximacroview.moc"