summaryrefslogtreecommitdiffstats
path: root/kexi/plugins/macros/kexiactions/datatableaction.cpp
blob: 19922122b49f3890ac9679c2b42d8ccae222956a (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
176
177
178
179
180
181
182
183
184
185
/***************************************************************************
 * This file is part of the KDE project
 * copyright (C) 2006 by Sebastian Sauer (mail@dipe.org)
 *
 * This program 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 program 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 program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 ***************************************************************************/

#include "datatableaction.h"
//#include "objectvariable.h"

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

#include <klocale.h>

using namespace KexiMacro;

namespace KexiMacro {

	//static const TQString OBJECT = "method";
	//static const TQString OBJECT = "type";
	//static const TQString OBJECT = "partitem";

	template<class ACTIONIMPL>
	class MethodVariable : public KexiVariable<ACTIONIMPL>
	{
		public:
			MethodVariable(ACTIONIMPL* actionimpl)
				: KexiVariable<ACTIONIMPL>(actionimpl, "method", i18n("Method"))
			{
				TQStringList list;
				list << "import" << "export";
				this->appendChild( KSharedPtr<KoMacro::Variable>( new KoMacro::Variable(list, "@list") ) );

				this->setVariant( list[0] );
			}
	};

	template<class ACTIONIMPL>
	class TypeVariable : public KexiVariable<ACTIONIMPL>
	{
		public:
			TypeVariable(ACTIONIMPL* actionimpl)
				: KexiVariable<ACTIONIMPL>(actionimpl, "type", i18n("Type"))
			{
				TQStringList list;
				list << "file" << "clipboard";
				this->appendChild( KSharedPtr<KoMacro::Variable>( new KoMacro::Variable(list, "@list") ) );

				this->setVariant( list[0] );
			}
	};

	template<class ACTIONIMPL>
	class PartItemVariable : public KexiVariable<ACTIONIMPL>
	{
		public:
			PartItemVariable(ACTIONIMPL* actionimpl, const TQString& partitem = TQString())
				: KexiVariable<ACTIONIMPL>(actionimpl, "partitem", i18n("Item"))
			{
				TQStringList namelist;
				if(actionimpl->mainWin()->project()) {
					KexiPart::PartInfoList* parts = Kexi::partManager().partInfoList();
					for(KexiPart::PartInfoListIterator it(*parts); it.current(); ++it) {
						KexiPart::Info* info = it.current();
						if(! info->isDataExportSupported())
							continue;
						KexiPart::ItemDict* items = actionimpl->mainWin()->project()->items(info);
						if(items)
							for(KexiPart::ItemDictIterator item_it = *items; item_it.current(); ++item_it)
								namelist << info->objectName() + "." + item_it.current()->name();
					}
					for(TQStringList::Iterator it = namelist.begin(); it != namelist.end(); ++it)
						this->appendChild( KSharedPtr<KoMacro::Variable>(new KoMacro::Variable(*it)) );

					//const TQString name = info->objectName(); //info->groupName();
					//this->appendChild( KSharedPtr<KoMacro::Variable>(new KoMacro::Variable(name)) );
				}
				const TQString n =
					namelist.contains(partitem)
						? partitem
						: namelist.count() > 0 ? namelist[0] : "";
				this->setVariant(n);
				kdDebug()<<"##################### KexiActions::ObjectVariable() variant="<<this->variant()<<endl;
			}
	};

}

DataTableAction::DataTableAction()
	: KexiAction("datatable", i18n("Data Table"))
{
	setVariable(KSharedPtr<KoMacro::Variable>( new MethodVariable<DataTableAction>(this) ));
	setVariable(KSharedPtr<KoMacro::Variable>( new TypeVariable<DataTableAction>(this) ));
	setVariable(KSharedPtr<KoMacro::Variable>( new PartItemVariable<DataTableAction>(this) ));
}

DataTableAction::~DataTableAction() 
{
}

bool DataTableAction::notifyUpdated(KSharedPtr<KoMacro::MacroItem> macroitem, const TQString& name)
{
	kdDebug()<<"DataTableAction::notifyUpdated() name="<<name<<" macroitem.action="<<(macroitem->action() ? macroitem->action()->name() : "NOACTION")<<endl;
	/*
	KSharedPtr<KoMacro::Variable> variable = macroitem->variable(name, false);
	if(! variable) {
		kdWarning()<<"DataTableAction::notifyUpdated() No such variable="<<name<<" in macroitem."<<endl;
		return false;
	}
	variable->clearChildren();
	if(name == "method") {
		const int partitem = macroitem->variant(OBJECT, true).toString();
		macroitem->variable(OBJECT, true)->setChildren(
			KoMacro::Variable::List() << KSharedPtr<KoMacro::Variable>(new ObjectVariable<ExecuteAction>(this, partitem)) );
	}
	*/
	return true;
}

void DataTableAction::activate(KSharedPtr<KoMacro::Context> context)
{
	if(! mainWin()->project()) {
		kdWarning() << "ExecuteAction::activate(KSharedPtr<KoMacro::Context>) Invalid project" << endl;
		return;
	}

	const TQString method = context->variable("method")->variant().toString();
	const TQString type = context->variable("type")->variant().toString();

	const TQString partitem = context->variable("partitem")->variant().toString();
	TQString identifier;
	if(! partitem.isEmpty()) {
		TQStringList parts = TQStringList::split(".", partitem);
		KexiPart::Part* part = Kexi::partManager().partForMimeType( TQString("kexi/%1").arg(parts[0]) );
		KexiPart::Item* item = part ? mainWin()->project()->item(part->info(), parts[1]) : 0;
		if(! item)
			throw KoMacro::Exception(i18n("No such item \"%1\"").arg(partitem));
		identifier = TQString::number(item->identifier());
	}

	TQMap<TQString,TQString> args;
	if(! identifier.isNull())
		args.insert("itemId", identifier);

	if(method == "import") {
		args.insert("sourceType", type);
		TQDialog *dlg = KexiInternalPart::createModalDialogInstance(
			"csv_importexport", "KexiCSVImportDialog", 0, mainWin(), 0, &args);
		if (!dlg)
			return; //error msg has been shown by KexiInternalPart
		dlg->exec();
		delete dlg;
	}
	else if(method == "export") {
		args.insert("destinationType", type);
		TQDialog *dlg = KexiInternalPart::createModalDialogInstance(
			"csv_importexport", "KexiCSVExportWizard", 0, mainWin(), 0, &args);
		if (!dlg)
			return; //error msg has been shown by KexiInternalPart
		dlg->exec();
		delete dlg;
	}
	else {
		throw KoMacro::Exception(i18n("No such method \"%1\"").arg(method));
	}
}

//#include "executeaction.moc"