summaryrefslogtreecommitdiffstats
path: root/kexi/core/kexiproject.h
blob: 1128ffe42e47deb96ce46e96dd38a2d85502d812 (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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/* This file is part of the KDE project
   Copyright (C) 2003 Lucijan Busch <lucijan@kde.org>
   Copyright (C) 2003-2006 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.
*/

#ifndef KEXIPROJECT_H
#define KEXIPROJECT_H

#include <qobject.h>
#include <qintdict.h>
#include <qptrdict.h>
#include <qguardedptr.h>

#include <kexiutils/tristate.h>
#include <kexidb/object.h>
#include "kexiprojectdata.h"
#include "kexipartitem.h"
#include "kexi.h"

/*! KexiProject implementation version. 
 It is altered after every change: 
 - major number is increased after KexiProject storage format change, 
 - minor is increased after adding binary-incompatible change.
 Use KexiProject::versionMajor() and KexiProject::versionMinor() to get real project's version.
*/

#define KEXIPROJECT_VERSION_MAJOR 1
#define KEXIPROJECT_VERSION_MINOR 0

namespace KexiDB
{
	class DriverManager;
	class Driver;
	class Connection;
	class Parser;
}

namespace KexiPart
{
	class Part;
	class Info;
}

class KexiMainWindow;
class KexiDialogBase;

/**
 * @short A project's main controller. 
 * It also contains connection data,
 * current file state, etc.
 */
class KEXICORE_EXPORT KexiProject : public QObject, public KexiDB::Object
{
	Q_OBJECT

	public:
		/*! Constructor 1. Creates a new object using \a pdata. 
		 \a pdata which will be then owned by KexiProject object.
		 \a handler can be provided to receive error messages during 
		 entire KexiProject object's lifetime. */
		KexiProject(KexiProjectData* pdata, KexiDB::MessageHandler* handler = 0);

		/*! Constructor 2. Like above but sets predefined connections \a conn. 
		 The connection should be created using the same connection data 
		 as pdata->connectionData(). The connection will become owned by created KexiProject
		 object, so do not destroy it. */
		KexiProject(KexiProjectData *pdata, KexiDB::MessageHandler* handler, 
			KexiDB::Connection* conn);

//		KexiProject(KexiDB::ConnectionData *cdata);

		~KexiProject();

		/*! \return major version of KexiProject object. 
		 This information is retrieved from database when existing project is opened. */
		int versionMajor() const;

		/*! \return minor version of KexiProject object. 
		 @see versionMajor() */
		int versionMinor() const;

		/*! Opens existing project using project data. 
		 \return true on success */
		tristate open();

		/*! Like open(). 
		 \return true on success.
		 Additional \a incompatibleWithKexi, is set to false on failure when
		 connection for the project was successfully started bu the project 
		 is probably not compatible with Kexi - no valid "kexidb_major_ver" 
		 value in "kexi__db" table. 
		 This is often the case for native server-based databases. 
		 If so, Kexi application can propose importing the database
		 or linking it to parent project (the latter isn't yet implemented).
		 For other types of errors the variable is set to true. */
		tristate open(bool &incompatibleWithKexi);

		/*! Creates new, empty project using project data.
		 If \a forceOverwrite is true, existing database project is silently overwritten.
		 Connection is created (accessible then with KexiProject::dbConnection()).

		 Since KexiProject inherits KexiDB::Object, it is possible to get error message 
		 and other information on error.

		 \return true on success, false on failure, and cancelled when database exists 
		 but \a forceOverwrite is false. */
		tristate create(bool forceOverwrite = false);

		/*! \return true if there was error during last operation on the object. */
		bool error() const { return KexiDB::Object::error(); }

		/**
		 * @return true if a we are connected to a database
		 */
		bool isConnected();

		/**
		 * @return all items of a type \a i in this project
		 */
		KexiPart::ItemDict* items(KexiPart::Info *i);

		/**
		 * @return all items of a type \a mime in this project
		 * It is a convenience function.
		 */
		KexiPart::ItemDict* itemsForMimeType(const QCString &mimeType);

		/**
		 * Puts a list of items of a type \a i in this project into \a list.
		 * You can then sort this list using ItemList::sort().
		 */
		void getSortedItems(KexiPart::ItemList& list, KexiPart::Info *i);

		/**
		 * Puts a sorted list of items of a type \a mimeType in this project into \a list.
		 * You can then sort this list using ItemList::sort().
		 */
		void getSortedItemsForMimeType(KexiPart::ItemList& list, const QCString &mimeType);

		/**
		 * @return item of type \a mime and name \a name
		 */
		KexiPart::Item* itemForMimeType(const QCString &mimeType, const QString &name);

		/**
		 * @return item of type \a i and name \a name
		 */
		KexiPart::Item* item(KexiPart::Info *i, const QString &name);

		/**
		 * @return item for \a identifier
		 */
		KexiPart::Item* item(int identifier);

		/**
		 * @return the database connection associated with this project
		 */
		KexiDB::Connection *dbConnection() const;

		/**
		 * @return the project's data
		 */
		KexiProjectData *data() const;

		/*! Opens object pointed by \a item in a view \a viewMode.
		 \a staticObjectArgs can be passed for static object 
		 (only works when part for this item is of type KexiPart::StaticPart) */
		KexiDialogBase* openObject(KexiMainWindow *wnd, KexiPart::Item& item, 
			int viewMode = Kexi::DataViewMode, QMap<QString,QString>* staticObjectArgs = 0);

		//! For convenience
		KexiDialogBase* openObject(KexiMainWindow *wnd, const QCString &mimeType, 
			const QString& name, int viewMode = Kexi::DataViewMode);

		/*! Remove a part instance pointed by \a item.
		 \return true on success. */
		bool removeObject(KexiMainWindow *wnd, KexiPart::Item& item);

		/*! Renames a part instance pointed by \a item to a new name \a newName.
		 \return true on success. */
		bool renameObject(KexiMainWindow *wnd, KexiPart::Item& item, const QString& newName);

		/*! Creates part item for given part \a info. 
		 Newly item will not be saved to the backend but stored in memory only
		 (owned by project), and marked as "neverSaved" (see KexiPart::Item::neverSaved()).
		 The item will have assigned a new unique caption like e.g. "Table15",
		 and unique name like "table15", but no specific identifier 
		 (because id will be assigned on creation at the backend side).

		 If \a suggestedCaption is not empty, it will be set as a caption 
		 (with number suffix, to avoid duplicated, e.g. "employees7" 
		 for "employees" sugested name). Name will be then built based 
		 on this caption using KexiUtils::string2Identifier().

		 This method is used before creating new object.
		 \return newly created part item or NULL on any error. */
		KexiPart::Item* createPartItem(KexiPart::Info *info, 
			const QString& suggestedCaption = QString::null );

		//! Added for convenience.
		KexiPart::Item* createPartItem(KexiPart::Part *part, 
			const QString& suggestedCaption = QString::null);

		/*! Adds item \a item after it is succesfully stored as an instance of part
		 pointed by \a info. Also clears 'neverSaved' flag if \a item.
		 Used by KexiDialogBase::storeNewData(). 
		 @internal */
		void addStoredItem(KexiPart::Info *info, KexiPart::Item *item);

		/*! removes \a item from internal dictionaries. The item is destroyed 
		 after successful removal. 
		 Used to delete an unstored part item previusly created with createPartItem(). */
		void deleteUnstoredItem(KexiPart::Item *item);

#if 0 //remove?
		/*! Creates object using data provided by \a dlg dialog. 
		 Dialog's \a item (KexiDialog::partItem()) must not be stored 
		 (KexiPart::Item::neverStored()==false) and created 
		 by KexiProject::createPartItem().
		 Identifier of the item will be updated to a final value 
		 (stored in the backend), because previously there was temporary one set.
		 \return true for successfully created object or false on any error. */
		bool createObject(KexiDialogBase *dlg);
#endif

		KexiDB::Parser* sqlParser();

		/*! Shows dialog for creating new blank project,
		 ans creates one. Dialog is not shown if option for automatic creation 
		 is checked or Kexi::startupHandler().projectData() was provided from command line.
		 \a cancelled is set to true if creation has been cancelled (e.g. user answered 
		 no when asked for database overwriting, etc.
		 \return true if database was created, false on error or when cancel was pressed */
		static KexiProject* createBlankProject(bool &cancelled, KexiProjectData* data,
			KexiDB::MessageHandler* handler = 0);

		/*! Drops project described by \a data. \return true on success. 
		 Use with care: Any KexiProject objects allocated for this project will become invalid! */
		static tristate dropProject(KexiProjectData* data, 
			KexiDB::MessageHandler* handler, bool dontAsk = false);

		/*! @see KexiDB::Connection::setQuerySchemaObsolete( const QString& queryName ) */
//		void setQuerySchemaObsolete( const QString& queryName );

//		/** used to emit objectCreated() signal */
//		void emitObjectCreated(const QCString &mime, const QCString& name) { emit objectCreated(mime, name); }
//		void emitTableCreated(KexiDB::TableSchema& schema) { emit tableCreated(schema); }

	protected:
		/*! Creates connection using project data. 
		 The connection will be readonly if data()->isReadOnly().
		 \return true on success, otherwise false and appropriate error is set. */
		bool createConnection();
		
		bool closeConnection();

		bool initProject();

		//! Used in open() and open(bool&).
		tristate openInternal(bool *incompatibleWithKexi);

		/*! Kexi itself can define a number of internal database objects (mostly data structures), 
		 usually tables for it's own purposes.
		 Even while at KexiDB library level, such "system" tables, like "kexi__objects", "kexi__objectdata"
		 are created automatically on database project creation, this is not enough: there are objects
		 needed specifically for Kexi but not for other applications utilizing KexiDB library.
		 Example table created here for now is "kexi__blobs". 

		 This method is called on create() and open(): creates necessary objects 
		 if they are not yet existing. This especially allows to create to create these objects
		 (on open) within a project made with previous Kexi version not supporting 
		 all currently defined structurtes. We're trying to be here as much backward compatible as possible.
		 For this purpose, here's the complete list of currently created objects:
		 - "kexi__blobs" - a table containing BLOBs data that can be accessed globally at Kexi projects
		   from within any database-aware view (table views, forms, reports, etc.)
		 
		 @param insideTransaction Embed entire creation process inside a transaction

		 \return true on successful object's creation. Objects are created only once, they are not overwritten.
		*/
		bool createInternalStructures(bool insideTransaction);

		/*! \return Kexi part for \a item. */
		KexiPart::Part *findPartFor(KexiPart::Item& item);

	signals:
		/** signal emitted on error */
		void error(const QString &title, KexiDB::Object *obj);

		/** signal emitted on error (not KexiDB-related) */
		void error(const QString &msg, const QString &desc);

		/** New \a item has been stored. */
		void newItemStored(KexiPart::Item& item);

		/** instance pointed by \a item is removed */
		void itemRemoved(const KexiPart::Item &item);

		/** instance pointed by \a item is renamed */
		void itemRenamed(const KexiPart::Item &item, const QCString& oldName);

//		/** new table \a schema created */
//		void tableCreated(KexiDB::TableSchema& schema);
//		/** New object of mimetype \a mime and \a name has been created. */
//		void objectCreated(const QCString &mime, const QCString& name);

	protected:
		/*! Checks whether the project's connection is read-only. 
		 If so, error message is set and false is returned. */
		bool checkWritable();

		class Private;
		Private *d;

		friend class KexiMainWindowImpl;
};


#endif