kexi

kexipartmanager.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2003 Lucijan Busch <lucijan@kde.org>
3 Copyright (C) 2003-2005 Jaroslaw Staniek <js@iidea.pl>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21#include <klibloader.h>
22#include <ktrader.h>
23#include <kdebug.h>
24#include <tdeconfig.h>
25#include <tdeparts/componentfactory.h>
26
27#include "kexipartmanager.h"
28#include "kexipart.h"
29#include "kexipartinfo.h"
30#include "kexistaticpart.h"
31#include "kexi_version.h"
32
33#include <kexidb/connection.h>
34#include <kexidb/cursor.h>
35
36using namespace KexiPart;
37
38Manager::Manager(TQObject *parent)
39 : TQObject(parent)
40{
41 m_lookupDone = false;
42 m_partlist.setAutoDelete(true);
43 m_partsByMime.setAutoDelete(false);
44 m_parts.setAutoDelete(false);//KApp will remove parts
45 m_nextTempProjectPartID = -1;
46}
47
48void
50{
51//js: TODO: allow refreshing!!!! (will need calling removeClient() by Part objects)
52 if (m_lookupDone)
53 return;
54 m_lookupDone = true;
55 m_partlist.clear();
56 m_partsByMime.clear();
57 m_parts.clear();
58 TDETrader::OfferList tlist = TDETrader::self()->query("Kexi/Handler",
59 "[X-Kexi-PartVersion] == " + TQString::number(KEXI_PART_VERSION));
60
61 TDEConfig conf("kexirc", true);
62 conf.setGroup("Parts");
63 TQStringList sl_order = TQStringList::split( ",", conf.readEntry("Order") );//we'll set parts in defined order
64 const int size = TQMAX( tlist.count(), sl_order.count() );
65 TQPtrVector<KService> ordered( size*2 );
66 int offset = size; //we will insert not described parts from #offset
67
68 //compute order
69 for(TDETrader::OfferList::ConstIterator it(tlist.constBegin()); it != tlist.constEnd(); ++it)
70 {
71 KService::Ptr ptr = (*it);
72 TQCString mime = ptr->property("X-Kexi-TypeMime").toCString();
73 kdDebug() << "Manager::lookup(): " << mime << endl;
74//<TEMP>: disable some parts if needed
75 if (!Kexi::tempShowForms() && mime=="kexi/form")
76 continue;
77 if (!Kexi::tempShowReports() && mime=="kexi/report")
78 continue;
79 if (!Kexi::tempShowMacros() && mime=="kexi/macro")
80 continue;
81 if (!Kexi::tempShowScripts() && mime=="kexi/script")
82 continue;
83//</TEMP>
84 int idx = sl_order.findIndex( ptr->library() );
85 if (idx!=-1)
86 ordered.insert(idx, ptr);
87 else //add to end
88 ordered.insert(offset++, ptr);
89 }
90 //fill final list using computed order
91 for (int i = 0; i< (int)ordered.size(); i++) {
92 KService::Ptr ptr = ordered[i];
93 if (ptr) {
94 Info *info = new Info(ptr);
95 info->setProjectPartID(m_nextTempProjectPartID--); // temp. part id are -1, -2, and so on,
96 // to avoid duplicates
97 if (!info->mimeType().isEmpty()) {
98 m_partsByMime.insert(info->mimeType(), info);
99 kdDebug() << "Manager::lookup(): inserting info to " << info->mimeType() << endl;
100 }
101 m_partlist.append(info);
102 }
103 }
104}
105
106Manager::~Manager()
107{
108}
109
110Part *
112{
113 clearError();
114 if(!i)
115 return 0;
116
117// kdDebug() << "Manager::part( id = " << i->projectPartID() << " )" << endl;
118
119 if (i->isBroken()) {
121 return 0;
122 }
123
124 Part *p = m_parts[i->projectPartID()];
125
126 if(!p) {
127// kdDebug() << "Manager::part().." << endl;
128 int error=0;
129 p = KParts::ComponentFactory::createInstanceFromService<Part>(i->ptr(), this,
130 TQString(i->objectName()+"_part").latin1(), TQStringList(), &error);
131 if(!p) {
132 kdDebug() << "Manager::part(): failed :( (ERROR #" << error << ")" << endl;
133 kdDebug() << " " << KLibLoader::self()->lastErrorMessage() << endl;
134 i->setBroken(true, i18n("Error while loading plugin \"%1\"").arg(i->objectName()));
136 return 0;
137 }
138 if (p->m_registeredPartID>0) {
140 }
141
142 p->setInfo(i);
143 m_parts.insert(i->projectPartID(),p);
144 emit partLoaded(p);
145 }
146 else {
147// kdDebug() << "Manager::part(): cached: " << i->groupName() << endl;
148 }
149
150// kdDebug() << "Manager::part(): fine!" << endl;
151 return p;
152}
153
154#if 0
155void
156Manager::unloadPart(Info *i)
157{
158 m_parts.setAutoDelete(true);
159 m_parts.remove(i->projectPartID());
160 m_parts.setAutoDelete(false);
161/* if (!p)
162 return;
163 m_partsByMime.take(i->mime());
164 m_partlist.removeRef(p);*/
165}
166
167void
168Manager::unloadAllParts()
169{
170// m_partsByMime.clear();
171 m_parts.setAutoDelete(true);
172 m_parts.clear();
173 m_parts.setAutoDelete(false);
174// m_partlist.clear();
175}
176#endif
177
178/*void
179Manager::removeClients( KexiMainWindow *win )
180{
181 if (!win)
182 return;
183 TQIntDictIterator<Part> it(m_parts);
184 for (;i.current();++it) {
185 i.current()->removeClient(win->guiFactory());
186 }
187}*/
188
189Part *
190Manager::partForMimeType(const TQString &mimeType)
191{
192 return mimeType.isEmpty() ? 0 : part(m_partsByMime[mimeType.latin1()]);
193}
194
195Info *
196Manager::infoForMimeType(const TQString &mimeType)
197{
198 Info *i = mimeType.isEmpty() ? 0 : m_partsByMime[mimeType.latin1()];
199 if (i)
200 return i;
201 setError(i18n("No plugin for mime type \"%1\"").arg(mimeType));
202 return 0;
203}
204
205
206bool
208{
209 clearError();
210// TQString errmsg = i18n("Invalid project contents.");
211
212//TODO: catch errors!
213 if(!conn->isDatabaseUsed()) {
214 setError(conn);
215 return false;
216 }
217
218 KexiDB::Cursor *cursor = conn->executeQuery("SELECT * FROM kexi__parts");//, KexiDB::Cursor::Buffered);
219 if(!cursor) {
220 setError(conn);
221 return false;
222 }
223
224// int id=0;
225// TQStringList parts_found;
226 for(cursor->moveFirst(); !cursor->eof(); cursor->moveNext())
227 {
228// id++;
229 Info *i = infoForMimeType(cursor->value(2).toCString());
230 if(!i)
231 {
232 Missing m;
233 m.name = cursor->value(1).toString();
234 m.mime = cursor->value(2).toCString();
235 m.url = cursor->value(3).toString();
236
237 m_missing.append(m);
238 }
239 else
240 {
241 i->setProjectPartID(cursor->value(0).toInt());
242 i->setIdStoredInPartDatabase(true);
243// parts_found+=cursor->value(2).toString();
244 }
245 }
246
247 conn->deleteCursor(cursor);
248
249#if 0 //js: moved to Connection::createDatabase()
250 //add missing default part entries
251 KexiDB::TableSchema *ts = conn->tableSchema("kexi__parts");
252 if (!ts)
253 return false;
254 KexiDB::FieldList *fl = ts->subList("p_id", "p_name", "p_mime", "p_url");
255 if (!fl)
256 return false;
257 if (!parts_found.contains("kexi/table")) {
258 if (!conn->insertRecord(*fl, TQVariant(1), TQVariant("Tables"), TQVariant("kexi/table"), TQVariant("http://")))
259 return false;
260 }
261 if (!parts_found.contains("kexi/query")) {
262 if (!conn->insertRecord(*fl, TQVariant(2), TQVariant("Queries"), TQVariant("kexi/query"), TQVariant("http://")))
263 return false;
264 }
265#endif
266 return true;
267}
268
270{
271 if (!part)
272 return;
273 part->info()->setProjectPartID(m_nextTempProjectPartID--); // temp. part id are -1, -2, and so on,
274 m_partlist.append(part->info());
275 if (!part->info()->mimeType().isEmpty())
276 m_partsByMime.insert(part->info()->mimeType(), part->info());
277 m_parts.insert(part->info()->projectPartID(), part);
278}
279
280#include "kexipartmanager.moc"
Provides database connection, allowing queries and data modification.
Definition connection.h:62
TableSchema * tableSchema(int tableId)
bool deleteCursor(Cursor *cursor)
bool isDatabaseUsed() const
Cursor * executeQuery(const TQString &statement, uint cursor_options=0)
Provides database cursor functionality.
Definition cursor.h:67
virtual bool moveNext()
Definition cursor.cpp:292
virtual TQVariant value(uint i)=0
bool eof() const
Definition cursor.cpp:339
bool moveFirst()
Definition cursor.cpp:205
FieldList * subList(const TQString &n1, const TQString &n2=TQString(), const TQString &n3=TQString(), const TQString &n4=TQString(), const TQString &n5=TQString(), const TQString &n6=TQString(), const TQString &n7=TQString(), const TQString &n8=TQString(), const TQString &n9=TQString(), const TQString &n10=TQString(), const TQString &n11=TQString(), const TQString &n12=TQString(), const TQString &n13=TQString(), const TQString &n14=TQString(), const TQString &n15=TQString(), const TQString &n16=TQString(), const TQString &n17=TQString(), const TQString &n18=TQString())
bool error() const
Definition object.h:38
virtual void clearError()
Definition object.cpp:131
virtual void setError(int code=ERR_OTHER, const TQString &msg=TQString())
Definition object.cpp:53
Information about a Kexi Part (plugin).
void setBroken(bool broken, const TQString &errorMessage)
Sets the broken flag and error message.
int projectPartID() const
TQString errorMessage() const
KService::Ptr ptr() const
TQCString mimeType() const
bool isBroken() const
TQString objectName() const
void setProjectPartID(int id)
Sets the project-part-id.
bool checkProject(KexiDB::Connection *conn)
checks project's kexi__part table and checks if all parts used in a project are available locally
void insertStaticPart(KexiPart::StaticPart *part)
Used by StaticPart.
Part * partForMimeType(const TQString &mimeTypt)
Manager(TQObject *parent=0)
creates an empty instance
Info * infoForMimeType(const TQString &mimeType)
void lookup()
queries ktrader and creates a list of available parts
The main class for kexi frontend parts like tables, queries, forms and reports.
Definition kexipart.h:66
int m_registeredPartID
Definition kexipart.h:318
Static Kexi Part (plugin).
KEXICORE_EXPORT bool & tempShowScripts()
false by default, flag loaded on main window startup
Definition kexi.cpp:134
KEXICORE_EXPORT bool & tempShowForms()
false by default, flag loaded on main window startup
Definition kexi.cpp:110
KEXICORE_EXPORT bool & tempShowMacros()
false by default, flag loaded on main window startup
Definition kexi.cpp:126
KEXICORE_EXPORT bool & tempShowReports()
false by default, flag loaded on main window startup
Definition kexi.cpp:118