Changeset View
Changeset View
Standalone View
Standalone View
src/plugins/queries/kexiquerypart.cpp
1 | /* This file is part of the KDE project | 1 | /* This file is part of the KDE project | ||
---|---|---|---|---|---|
2 | Copyright (C) 2004 Lucijan Busch <lucijan@kde.org> | 2 | Copyright (C) 2004 Lucijan Busch <lucijan@kde.org> | ||
3 | Copyright (C) 2004-2016 Jarosław Staniek <staniek@kde.org> | 3 | Copyright (C) 2004-2017 Jarosław Staniek <staniek@kde.org> | ||
4 | 4 | | |||
5 | This program is free software; you can redistribute it and/or | 5 | This program is free software; you can redistribute it and/or | ||
6 | modify it under the terms of the GNU Library General Public | 6 | modify it under the terms of the GNU Library General Public | ||
7 | License as published by the Free Software Foundation; either | 7 | License as published by the Free Software Foundation; either | ||
8 | version 2 of the License, or (at your option) any later version. | 8 | version 2 of the License, or (at your option) any later version. | ||
9 | 9 | | |||
10 | This program is distributed in the hope that it will be useful, | 10 | This program is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
Show All 16 Lines | |||||
28 | #include <kexipartinfo.h> | 28 | #include <kexipartinfo.h> | ||
29 | #include <kexiutils/utils.h> | 29 | #include <kexiutils/utils.h> | ||
30 | 30 | | |||
31 | #include <KDbConnection> | 31 | #include <KDbConnection> | ||
32 | #include <KDbCursor> | 32 | #include <KDbCursor> | ||
33 | #include <KDbParser> | 33 | #include <KDbParser> | ||
34 | #include <KDbQuerySchema> | 34 | #include <KDbQuerySchema> | ||
35 | 35 | | |||
36 | #include <KStandardGuiItem> | ||||
37 | #include <KMessageBox> | ||||
38 | | ||||
36 | #include <QDebug> | 39 | #include <QDebug> | ||
37 | 40 | | |||
38 | KEXI_PLUGIN_FACTORY(KexiQueryPart, "kexi_queryplugin.json") | 41 | KEXI_PLUGIN_FACTORY(KexiQueryPart, "kexi_queryplugin.json") | ||
39 | 42 | | |||
40 | KexiQueryPart::KexiQueryPart(QObject *parent, const QVariantList &l) | 43 | KexiQueryPart::KexiQueryPart(QObject *parent, const QVariantList &l) | ||
41 | : KexiPart::Part(parent, | 44 | : KexiPart::Part(parent, | ||
42 | xi18nc("Translate this word using only lowercase alphanumeric characters (a..z, 0..9). " | 45 | xi18nc("Translate this word using only lowercase alphanumeric characters (a..z, 0..9). " | ||
43 | "Use '_' character instead of spaces. First character should be a..z character. " | 46 | "Use '_' character instead of spaces. First character should be a..z character. " | ||
▲ Show 20 Lines • Show All 50 Lines • ▼ Show 20 Line(s) | |||||
94 | 97 | | |||
95 | tristate KexiQueryPart::remove(KexiPart::Item *item) | 98 | tristate KexiQueryPart::remove(KexiPart::Item *item) | ||
96 | { | 99 | { | ||
97 | if (!KexiMainWindowIface::global()->project() | 100 | if (!KexiMainWindowIface::global()->project() | ||
98 | || !KexiMainWindowIface::global()->project()->dbConnection()) | 101 | || !KexiMainWindowIface::global()->project()->dbConnection()) | ||
99 | return false; | 102 | return false; | ||
100 | KDbConnection *conn = KexiMainWindowIface::global()->project()->dbConnection(); | 103 | KDbConnection *conn = KexiMainWindowIface::global()->project()->dbConnection(); | ||
101 | KDbQuerySchema *sch = conn->querySchema(item->identifier()); | 104 | KDbQuerySchema *sch = conn->querySchema(item->identifier()); | ||
102 | if (sch) | 105 | if (sch) { | ||
106 | const tristate res = KexiQueryPart::askForClosingObjectsUsingQuerySchema( | ||||
107 | KexiMainWindowIface::global()->openedWindowFor(item->identifier()), conn, sch, | ||||
108 | kxi18n("<para>You are about to delete query <resource>%1</resource> but it is used by " | ||||
109 | "following opened windows:</para>") | ||||
110 | .subs(sch->name())); | ||||
111 | if (res != true) { | ||||
112 | return res; | ||||
113 | } | ||||
103 | return conn->dropQuery(sch); | 114 | return conn->dropQuery(sch); | ||
115 | } | ||||
104 | //last chance: just remove item | 116 | //last chance: just remove item | ||
105 | return conn->removeObject(item->identifier()); | 117 | return conn->removeObject(item->identifier()); | ||
106 | } | 118 | } | ||
107 | 119 | | |||
108 | void KexiQueryPart::initPartActions() | 120 | void KexiQueryPart::initPartActions() | ||
109 | { | 121 | { | ||
110 | } | 122 | } | ||
111 | 123 | | |||
▲ Show 20 Lines • Show All 67 Lines • ▼ Show 20 Line(s) | |||||
179 | } | 191 | } | ||
180 | 192 | | |||
181 | tristate KexiQueryPart::rename(KexiPart::Item *item, const QString& newName) | 193 | tristate KexiQueryPart::rename(KexiPart::Item *item, const QString& newName) | ||
182 | { | 194 | { | ||
183 | Q_ASSERT(item); | 195 | Q_ASSERT(item); | ||
184 | Q_UNUSED(newName); | 196 | Q_UNUSED(newName); | ||
185 | if (!KexiMainWindowIface::global()->project()->dbConnection()) | 197 | if (!KexiMainWindowIface::global()->project()->dbConnection()) | ||
186 | return false; | 198 | return false; | ||
199 | // Note: unlike in KexiTablePart::rename here we just mark the query obsolete here so no need | ||||
200 | // to call KexiQueryPart::askForClosingObjectsUsingQuerySchema(). | ||||
187 | KexiMainWindowIface::global()->project()->dbConnection() | 201 | KexiMainWindowIface::global()->project()->dbConnection() | ||
188 | ->setQuerySchemaObsolete(item->name()); | 202 | ->setQuerySchemaObsolete(item->name()); | ||
189 | return true; | 203 | return true; | ||
190 | } | 204 | } | ||
191 | 205 | | |||
206 | // static | ||||
207 | tristate KexiQueryPart::askForClosingObjectsUsingQuerySchema(KexiWindow *window, | ||||
208 | KDbConnection *conn, | ||||
209 | KDbQuerySchema *query, | ||||
210 | const KLocalizedString &msg) | ||||
211 | { | ||||
212 | Q_ASSERT(conn); | ||||
213 | Q_ASSERT(query); | ||||
214 | if (!window) { | ||||
215 | return true; | ||||
216 | } | ||||
217 | QList<KDbTableSchemaChangeListener*> listeners | ||||
218 | = KDbTableSchemaChangeListener::listeners(conn, query); | ||||
219 | KexiQueryPartTempData *temp = static_cast<KexiQueryPartTempData*>(window->data()); | ||||
220 | // Special case: listener that is equal to window->data() will be silently closed | ||||
221 | // without asking for confirmation. It is not counted when looking for objects that | ||||
222 | // are "blocking" changes of the query. | ||||
223 | const bool tempListenerExists = listeners.removeAll(temp) > 0; | ||||
224 | // Immediate success if there's no temp-data's listener to close nor other listeners to close | ||||
225 | if (!tempListenerExists && listeners.isEmpty()) { | ||||
226 | return true; | ||||
227 | } | ||||
228 | | ||||
229 | if (!listeners.isEmpty()) { | ||||
230 | QString openedObjectsStr = "<p><ul>"; | ||||
231 | for(const KDbTableSchemaChangeListener* listener : listeners) { | ||||
232 | openedObjectsStr += QString("<li>%1</li>").arg(listener->name()); | ||||
233 | } | ||||
234 | openedObjectsStr += "</ul></p>"; | ||||
235 | QString message = "<html>" | ||||
236 | + i18nc("@info/plain Sentence1 Sentence2 Sentence3", "%1%2%3", | ||||
237 | KexiUtils::localizedStringToHtmlSubstring(msg), openedObjectsStr, | ||||
238 | KexiUtils::localizedStringToHtmlSubstring( | ||||
239 | kxi18nc("@info", "<para>Do you want to close these windows and save the " | ||||
240 | "design or cancel saving?</para>"))) | ||||
241 | + "</html>"; | ||||
242 | KGuiItem closeAndSaveItem(KStandardGuiItem::save()); | ||||
243 | closeAndSaveItem.setText( | ||||
244 | xi18nc("@action:button Close all windows and save", "Close Windows and Save")); | ||||
245 | closeAndSaveItem.setToolTip(xi18nc("@info:tooltip Close all windows and save design", | ||||
246 | "Close all windows and save design")); | ||||
247 | const int r = KMessageBox::questionYesNo(window, message, QString(), closeAndSaveItem, | ||||
248 | KStandardGuiItem::cancel(), QString(), | ||||
249 | KMessageBox::Notify | KMessageBox::Dangerous); | ||||
250 | if (r != KMessageBox::Yes) { | ||||
251 | return cancelled; | ||||
252 | } | ||||
253 | } | ||||
254 | // try to close every window depending on the query (if present) and also the temp-data's | ||||
255 | // listener (if present) | ||||
256 | const tristate res = KDbTableSchemaChangeListener::closeListeners(conn, query, { temp }); | ||||
257 | if (res != true) { //do not expose closing errors twice; just cancel | ||||
258 | return cancelled; | ||||
259 | } | ||||
260 | return true; | ||||
261 | } | ||||
262 | | ||||
192 | //---------------- | 263 | //---------------- | ||
193 | 264 | | |||
194 | KexiQueryPartTempData::KexiQueryPartTempData(KexiWindow* window, KDbConnection *conn) | 265 | KexiQueryPartTempData::KexiQueryPartTempData(KexiWindow* window, KDbConnection *conn) | ||
195 | : KexiWindowData(window) | 266 | : KexiWindowData(window) | ||
196 | , KDbTableSchemaChangeListener() | 267 | , KDbTableSchemaChangeListener() | ||
197 | , m_query(0) | 268 | , m_query(0) | ||
198 | , m_queryChangedInView(Kexi::NoViewMode) | 269 | , m_queryChangedInView(Kexi::NoViewMode) | ||
199 | { | 270 | { | ||
Show All 19 Lines | |||||
219 | { | 290 | { | ||
220 | KDbTableSchemaChangeListener::unregisterForChanges(conn, this); | 291 | KDbTableSchemaChangeListener::unregisterForChanges(conn, this); | ||
221 | } | 292 | } | ||
222 | 293 | | |||
223 | void KexiQueryPartTempData::registerTableSchemaChanges(KDbQuerySchema *q) | 294 | void KexiQueryPartTempData::registerTableSchemaChanges(KDbQuerySchema *q) | ||
224 | { | 295 | { | ||
225 | if (!q) | 296 | if (!q) | ||
226 | return; | 297 | return; | ||
227 | foreach(const KDbTableSchema* table, *q->tables()) { | 298 | KDbTableSchemaChangeListener::registerForChanges(conn, this, q); | ||
228 | KDbTableSchemaChangeListener::registerForChanges(conn, this, table); | | |||
229 | } | | |||
230 | } | 299 | } | ||
231 | 300 | | |||
232 | tristate KexiQueryPartTempData::closeListener() | 301 | tristate KexiQueryPartTempData::closeListener() | ||
233 | { | 302 | { | ||
234 | KexiWindow* window = static_cast<KexiWindow*>(parent()); | 303 | KexiWindow* window = static_cast<KexiWindow*>(parent()); | ||
304 | qDebug() << window->partItem()->name(); | ||||
235 | return KexiMainWindowIface::global()->closeWindow(window); | 305 | return KexiMainWindowIface::global()->closeWindow(window); | ||
236 | } | 306 | } | ||
237 | 307 | | |||
238 | KDbQuerySchema *KexiQueryPartTempData::takeQuery() | 308 | KDbQuerySchema *KexiQueryPartTempData::takeQuery() | ||
239 | { | 309 | { | ||
240 | KDbQuerySchema *query = m_query; | 310 | KDbQuerySchema *query = m_query; | ||
241 | m_query = 0; | 311 | m_query = 0; | ||
242 | return query; | 312 | return query; | ||
Show All 27 Lines |