summaryrefslogtreecommitdiffstats
path: root/kexi/plugins/tables/kexitablepart.cpp
blob: 55c8be5dfb995798e140dacd45822fdbd764c1ba (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* This file is part of the KDE project
   Copyright (C) 2003 Lucijan Busch <lucijan@kde.org>
   Copyright (C) 2002, 2003 Joseph Wenninger <jowenn@kde.org>
   Copyright (C) 2004 Jaroslaw Staniek <js@iidea.pl>

   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 "kexitablepart.h"

#include <kdebug.h>
#include <kgenericfactory.h>
#include <kmessagebox.h>
#include <ktabwidget.h>
#include <kiconloader.h>

#include "keximainwindow.h"
#include "kexiproject.h"
#include "kexipartinfo.h"
#include "widget/kexidatatable.h"
#include "widget/tableview/kexidatatableview.h"
#include "kexitabledesignerview.h"
#include "kexitabledesigner_dataview.h"
#include "kexilookupcolumnpage.h"

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

//! @internal
class KexiTablePart::Private
{
	public:
		Private()
		{
		}
		~Private()
		{
			delete static_cast<KexiLookupColumnPage*>(lookupColumnPage);
		}
		TQGuardedPtr<KexiLookupColumnPage> lookupColumnPage;
};

KexiTablePart::KexiTablePart(TQObject *parent, const char *name, const TQStringList &l)
 : KexiPart::Part(parent, name, l)
 , d(new Private())
{
	// REGISTERED ID:
	m_registeredPartID = (int)KexiPart::TableObjectType;

	kdDebug() << "KexiTablePart::KexiTablePart()" << endl;
	m_names["instanceName"] 
		= i18n("Translate this word using only lowercase alphanumeric characters (a..z, 0..9). "
		"Use '_' character instead of spaces. First character should be a..z character. "
		"If you cannot use latin characters in your language, use english word.", 
		"table");
	m_names["instanceCaption"] = i18n("Table");
	m_supportedViewModes = Kexi::DataViewMode | Kexi::DesignViewMode;
//js TODO: also add Kexi::TextViewMode when we'll have SQL ALTER TABLE EDITOR!!!
}

KexiTablePart::~KexiTablePart()
{
	delete d;
}

void KexiTablePart::initPartActions()
{
}

void KexiTablePart::initInstanceActions()
{
//moved to main window	createSharedAction(Kexi::DataViewMode, i18n("Filter"), "filter", 0, "tablepart_filter");

	KAction *a = createSharedToggleAction(
		Kexi::DesignViewMode, i18n("Primary Key"), "key", 0, "tablepart_toggle_pkey");
//		Kexi::DesignViewMode, i18n("Toggle Primary Key"), "key", 0, "tablepart_toggle_pkey");
	a->setWhatsThis(i18n("Sets or removes primary key for currently selected field."));
}

KexiDialogTempData* KexiTablePart::createTempData(KexiDialogBase* dialog)
{
	return new KexiTablePart::TempData(TQT_TQOBJECT(dialog));
}

KexiViewBase* KexiTablePart::createView(TQWidget *parent, KexiDialogBase* dialog, 
	KexiPart::Item &item, int viewMode, TQMap<TQString,TQString>*)
{
	KexiMainWindow *win = dialog->mainWin();
	if (!win || !win->project() || !win->project()->dbConnection())
		return 0;


	KexiTablePart::TempData *temp = static_cast<KexiTablePart::TempData*>(dialog->tempData());
	if (!temp->table) {
		temp->table = win->project()->dbConnection()->tableSchema(item.name());
		kdDebug() << "KexiTablePart::execute(): schema is " << temp->table << endl;
	}

	if (viewMode == Kexi::DesignViewMode) {
		KexiTableDesignerView *t = new KexiTableDesignerView(win, parent);
		return t;
	}
	else if (viewMode == Kexi::DataViewMode) {
		if(!temp->table)
			return 0; //todo: message
		//we're not setting table schema here -it will be forced to set 
		// in KexiTableDesigner_DataView::afterSwitchFrom()
		KexiTableDesigner_DataView *t = new KexiTableDesigner_DataView(win, parent);
		return t;
	}
	return 0;
}

bool KexiTablePart::remove(KexiMainWindow *win, KexiPart::Item &item)
{
	if (!win || !win->project() || !win->project()->dbConnection())
		return false;

	KexiDB::Connection *conn = win->project()->dbConnection();
	KexiDB::TableSchema *sch = conn->tableSchema(item.identifier());

	if (sch) {
		tristate res = KexiTablePart::askForClosingObjectsUsingTableSchema(
			win, *conn, *sch, 
			i18n("You are about to remove table \"%1\" but following objects using this table are opened:")
			.arg(sch->name()));
		return true == conn->dropTable( sch );
	}
	//last chance: just remove item
	return conn->removeObject( item.identifier() );
}

tristate KexiTablePart::rename(KexiMainWindow *win, KexiPart::Item & item, 
	const TQString& newName)
{
//TODO: what about objects (queries/forms) that use old name?
	KexiDB::Connection *conn = win->project()->dbConnection();
	KexiDB::TableSchema *sch = conn->tableSchema(item.identifier());
	if (!sch)
		return false;
	return conn->alterTableName(*sch, newName);
}

KexiDB::SchemaData*
KexiTablePart::loadSchemaData(KexiDialogBase *dlg, const KexiDB::SchemaData& sdata, int viewMode)
{
	Q_UNUSED( viewMode );

	return dlg->mainWin()->project()->dbConnection()->tableSchema( sdata.name() );
}

#if 0
KexiPart::DataSource *
KexiTablePart::dataSource()
{
	return new KexiTableDataSource(this);
}
#endif

tristate KexiTablePart::askForClosingObjectsUsingTableSchema(TQWidget *parent, KexiDB::Connection& conn, 
	KexiDB::TableSchema& table, const TQString& msg)
{
	TQPtrList<KexiDB::Connection::TableSchemaChangeListenerInterface>* listeners
		= conn.tableSchemaChangeListeners(table);
	if (!listeners || listeners->isEmpty())
		return true;
	
	TQString openedObjectsStr = "<ul>";
	for (TQPtrListIterator<KexiDB::Connection::TableSchemaChangeListenerInterface> it(*listeners);
		it.current(); ++it)	{
			openedObjectsStr += TQString("<li>%1</li>").arg(it.current()->listenerInfoString);
	}
	openedObjectsStr += "</ul>";
	int r = KMessageBox::questionYesNo(parent, 
		"<p>"+msg+"</p><p>"+openedObjectsStr+"</p><p>"
		+i18n("Do you want to close all windows for these objects?"), 
		TQString(), KGuiItem(i18n("Close windows"),"fileclose"), KStdGuiItem::cancel());
	tristate res;
	if (r == KMessageBox::Yes) {
		//try to close every window
		res = conn.closeAllTableSchemaChangeListeners(table);
		if (res!=true) //do not expose closing errors twice; just cancel
			res = cancelled;
	}
	else
		res = cancelled;

	return res;
}

TQString
KexiTablePart::i18nMessage(const TQCString& englishMessage, KexiDialogBase* dlg) const
{
	if (englishMessage=="Design of object \"%1\" has been modified.")
		return i18n("Design of table \"%1\" has been modified.");

	if (englishMessage=="Object \"%1\" already exists.")
		return i18n("Table \"%1\" already exists.");

	if (dlg->currentViewMode()==Kexi::DesignViewMode && !dlg->neverSaved()
		&& englishMessage==":additional message before saving design")
		return i18n("Warning! Any data in this table will be removed upon design's saving!");

	return englishMessage;
}

void KexiTablePart::setupCustomPropertyPanelTabs(KTabWidget *tab, KexiMainWindow* mainWin)
{
	if (!d->lookupColumnPage) {
		d->lookupColumnPage = new KexiLookupColumnPage(0);
		connect(d->lookupColumnPage, TQT_SIGNAL(jumpToObjectRequested(const TQCString&, const TQCString&)),
			mainWin, TQT_SLOT(highlightObject(const TQCString&, const TQCString&)));

//! @todo add "Table" tab

	/*
		connect(d->dataSourcePage, TQT_SIGNAL(formDataSourceChanged(const TQCString&, const TQCString&)),
			KFormDesigner::FormManager::self(), TQT_SLOT(setFormDataSource(const TQCString&, const TQCString&)));
		connect(d->dataSourcePage, TQT_SIGNAL(dataSourceFieldOrExpressionChanged(const TQString&, const TQString&, KexiDB::Field::Type)),
			KFormDesigner::FormManager::self(), TQT_SLOT(setDataSourceFieldOrExpression(const TQString&, const TQString&, KexiDB::Field::Type)));
		connect(d->dataSourcePage, TQT_SIGNAL(insertAutoFields(const TQString&, const TQString&, const TQStringList&)),
			KFormDesigner::FormManager::self(), TQT_SLOT(insertAutoFields(const TQString&, const TQString&, const TQStringList&)));*/
	}

	KexiProject *prj = mainWin->project();
	d->lookupColumnPage->setProject(prj);

//! @todo add lookup field icon
	tab->addTab( d->lookupColumnPage, SmallIconSet("combo"), "");
	tab->setTabToolTip( d->lookupColumnPage, i18n("Lookup column"));
}

KexiLookupColumnPage* KexiTablePart::lookupColumnPage() const
{
	return d->lookupColumnPage;
}

//----------------

#if 0
KexiTableDataSource::KexiTableDataSource(KexiPart::Part *part)
 : KexiPart::DataSource(part)
{
}

KexiTableDataSource::~KexiTableDataSource()
{
}

KexiDB::FieldList *
KexiTableDataSource::fields(KexiProject *project, const KexiPart::Item &it)
{
	kdDebug() << "KexiTableDataSource::fields(): " << it.name() << endl;
	return project->dbConnection()->tableSchema(it.name());
}

KexiDB::Cursor *
KexiTableDataSource::cursor(KexiProject * /*project*/, 
	const KexiPart::Item &/*it*/, bool /*buffer*/)
{
	return 0;
}
#endif

//----------------

KexiTablePart::TempData::TempData(TQObject* parent)
 : KexiDialogTempData(parent)
 , table(0)
 , tableSchemaChangedInPreviousView(true /*to force reloading on startup*/ )
{
}

//----------------

/**
TODO
*/
/*
AboutData( const char *programName,
	const char *version,
	const char *i18nShortDescription = 0,
	int licenseType = License_Unknown,
	const char *i18nCopyrightStatement = 0,
	const char *i18nText = 0,
	const char *homePageAddress = 0,
	const char *bugsEmailAddress = "submit@bugs.kde.org"
);

#define KEXIPART_EXPORT_FACTORY( libname, partClass, aboutData ) \
	static KexiPart::AboutData * libname ## updateAD(KexiPart::AboutData *ad) \
	{ ad->setAppName( #libname ); return ad; } \
	K_EXPORT_COMPONENT_FACTORY( libname, KGenericFactory<partClass>(libname ## updateAD(#libname)) )
*/

K_EXPORT_COMPONENT_FACTORY( kexihandler_table, KGenericFactory<KexiTablePart>("kexihandler_table") )

#include "kexitablepart.moc"