on xenial help sign in
replace WindowStateStorage with a threaded c++ implementation
commitec57fb3fa7444b27f9ae36e125c0bf4d8e07a782 authorMichael Zanetti <michael.zanetti@canonical.com> dateTue 10 Feb 2015, 18:03 (12 years ago) parent9032632
7 files changed+145−68
plugins/Utils/CMakeLists.txt+2−1 plugins/Utils/plugin.cpp+9 plugins/Utils/windowstatestorage.cpp+93 plugins/Utils/windowstatestorage.h+38 qml/Components/WindowStateStorage/WindowStateStorage.qml−63 qml/Components/WindowStateStorage/qmldir−1 qml/Stages/WindowMoveResizeArea.qml+3−3
plugins/Utils/CMakeLists.txt+2−1
@@ -18,6 +18,7 @@ set(QMLPLUGIN_SRC
1818 unitymenumodelpaths.cpp
1919 windowkeysfilter.cpp
2020 easingcurve.cpp
21+ windowstatestorage.cpp
2122 plugin.cpp
2223 )
2324
@@ -33,6 +34,6 @@ target_link_libraries(Utils-qml ${GIO_LDFLAGS})
3334# files directly in targets.
3435set_target_properties(Utils-qml PROPERTIES COMPILE_FLAGS -fvisibility=default)
3536
36−qt5_use_modules(Utils-qml Qml Quick DBus Network Gui)
37+qt5_use_modules(Utils-qml Qml Quick DBus Network Gui Sql Concurrent)
3738
3839add_unity8_plugin(Utils 0.1 Utils TARGETS Utils-qml)
plugins/Utils/plugin.cpp+9
@@ -33,6 +33,14 @@
3333#include "unitymenumodelpaths.h"
3434#include "windowkeysfilter.h"
3535#include "easingcurve.h"
36+#include "windowstatestorage.h"
37+
38+static QObject *createWindowStateStorage(QQmlEngine *engine, QJSEngine *scriptEngine)
39+{
40+ Q_UNUSED(engine)
41+ Q_UNUSED(scriptEngine)
42+ return new WindowStateStorage();
43+}
3644
3745void UtilsPlugin::registerTypes(const char *uri)
3846{
@@ -46,6 +54,7 @@ void UtilsPlugin::registerTypes(const char *uri)
4654 qmlRegisterType<GDateTimeFormatter>(uri, 0, 1, "GDateTimeFormatter");
4755 qmlRegisterType<EasingCurve>(uri, 0, 1, "EasingCurve");
4856 qmlRegisterType<RelativeTimeFormatter>(uri, 0, 1, "RelativeTimeFormatter");
57+ qmlRegisterSingletonType<WindowStateStorage>(uri, 0, 1, "WindowStateStorage", createWindowStateStorage);
4958}
5059
5160void UtilsPlugin::initializeEngine(QQmlEngine *engine, const char *uri)
plugins/Utils/windowstatestorage.cppnew file+93
@@ -0,0 +1,93 @@
1+/*
2+ * Copyright 2015 Canonical Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU Lesser General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU Lesser General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU Lesser General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+#include "windowstatestorage.h"
18+
19+#include <QtConcurrent>
20+#include <QDebug>
21+#include <QSqlQuery>
22+#include <QSqlError>
23+#include <QSqlResult>
24+#include <QRect>
25+
26+QMutex WindowStateStorage::s_mutex;
27+
28+WindowStateStorage::WindowStateStorage(QObject *parent):
29+ QObject(parent)
30+{
31+ m_db = QSqlDatabase::addDatabase("QSQLITE");
32+ m_db.setDatabaseName(QDir::homePath() + "/.cache/unity8/windowstatestorage.sqlite");
33+ initdb();
34+}
35+
36+void WindowStateStorage::saveGeometry(const QString &windowId, const QRect &rect)
37+{
38+ QString queryString = QString("INSERT OR REPLACE INTO geometry (windowId, x, y, width, height) values ('%1', '%2', '%3', '%4', '%5');")
39+ .arg(windowId)
40+ .arg(rect.x())
41+ .arg(rect.y())
42+ .arg(rect.width())
43+ .arg(rect.height());
44+
45+ QtConcurrent::run(executeAsyncQuery, queryString);
46+}
47+
48+void WindowStateStorage::executeAsyncQuery(const QString &queryString)
49+{
50+ QMutexLocker l(&s_mutex);
51+ QSqlQuery query;
52+
53+ bool ok = query.exec(queryString);
54+ if (!ok) {
55+ qWarning() << "Error saving window state."
56+ << "Driver error:" << query.lastError().driverText()
57+ << "Database error:" << query.lastError().databaseText();
58+ }
59+}
60+
61+QRect WindowStateStorage::getGeometry(const QString &windowId, const QRect &defaultValue)
62+{
63+ QMutexLocker l(&s_mutex);
64+ QString queryString = QString("SELECT * FROM geometry WHERE windowId = '%1';")
65+ .arg(windowId);
66+ QSqlQuery query;
67+
68+ bool ok = query.exec(queryString);
69+ if (!ok) {
70+ qWarning() << "Error retrieving window state for" << windowId
71+ << "Driver error:" << query.lastError().driverText()
72+ << "Database error:" << query.lastError().databaseText();
73+ return defaultValue;
74+ }
75+ if (!query.first()) {
76+ return defaultValue;
77+ }
78+ return QRect(query.value("x").toInt(), query.value("y").toInt(), query.value("width").toInt(), query.value("height").toInt());
79+}
80+
81+void WindowStateStorage::initdb()
82+{
83+ m_db.open();
84+ if (!m_db.open()) {
85+ qWarning() << "Error opening state database:" << m_db.lastError().driverText() << m_db.lastError().databaseText();
86+ return;
87+ }
88+
89+ if (!m_db.tables().contains("windowproperties")) {
90+ QSqlQuery query;
91+ query.exec("CREATE TABLE IF NOT EXISTS geometry(windowId TEXT UNIQUE, x INTEGER, y INTEGER, width INTEGER, height INTEGER);");
92+ }
93+}
plugins/Utils/windowstatestorage.hnew file+38
@@ -0,0 +1,38 @@
1+/*
2+ * Copyright 2015 Canonical Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU Lesser General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU Lesser General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU Lesser General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+#include <QObject>
18+#include <QSqlDatabase>
19+#include <QMutex>
20+
21+class WindowStateStorage: public QObject
22+{
23+ Q_OBJECT
24+public:
25+ WindowStateStorage(QObject *parent = 0);
26+
27+ Q_INVOKABLE void saveGeometry(const QString &windowId, const QRect &rect);
28+ Q_INVOKABLE QRect getGeometry(const QString &windowId, const QRect &defaultValue);
29+
30+private:
31+ void initdb();
32+
33+ static void executeAsyncQuery(const QString &queryString);
34+ static QMutex s_mutex;
35+
36+ // NB: This is accessed from threads. Make sure to mutex it.
37+ QSqlDatabase m_db;
38+};
qml/Components/WindowStateStorage/WindowStateStorage.qmldeleted−63
@@ -1,63 +0,0 @@
1−/*
2− * Copyright (C) 2015 Canonical, Ltd.
3− *
4− * This program is free software; you can redistribute it and/or modify
5− * it under the terms of the GNU General Public License as published by
6− * the Free Software Foundation; version 3.
7− *
8− * This program is distributed in the hope that it will be useful,
9− * but WITHOUT ANY WARRANTY; without even the implied warranty of
10− * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11− * GNU General Public License for more details.
12− *
13− * You should have received a copy of the GNU General Public License
14− * along with this program. If not, see <http://www.gnu.org/licenses/>.
15− */
16−
17−pragma Singleton
18−import QtQuick 2.2
19−import QtQuick.LocalStorage 2.0
20−
21−QtObject {
22−
23− property var priv: QtObject {
24− property var db: null
25−
26− function openDB() {
27− if (db !== null) return;
28−
29− db = LocalStorage.openDatabaseSync("unity8", "0.1", "", 100000);
30− try {
31− db.transaction(function(tx) {
32− tx.executeSql('CREATE TABLE IF NOT EXISTS windowproperties(windowId TEXT UNIQUE, x INTEGER, y INTEGER, width INTEGER, height INTEGER)');
33− });
34− } catch (err) {
35− console.log("Error creating table in database: " + err);
36− };
37− }
38− }
39−
40− function saveState(windowId, x, y, width, height) {
41− priv.openDB();
42− priv.db.transaction( function(tx){
43− tx.executeSql('INSERT OR REPLACE INTO windowproperties VALUES(?, ?, ?, ?, ?)', [windowId, x, y, width, height]);
44− });
45− }
46−
47− function getState(windowId) {
48− priv.openDB();
49− var res = new Object();
50− priv.db.transaction(function(tx) {
51− var rs = tx.executeSql('SELECT x, y, width, height FROM windowproperties WHERE windowId=?;', [windowId]);
52− if (rs.rows.length === 0) {
53− res = undefined;
54− } else {
55− res.x = rs.rows.item(0).x
56− res.y = rs.rows.item(0).y
57− res.width = rs.rows.item(0).width
58− res.height = rs.rows.item(0).height
59− }
60− });
61− return res;
62− }
63−}
qml/Components/WindowStateStorage/qmldirdeleted−1
@@ -1 +0,0 @@
1−singleton WindowStateStorage 1.0 WindowStateStorage.qml
qml/Stages/WindowMoveResizeArea.qml+3−3
@@ -18,7 +18,7 @@
1818
1919import QtQuick 2.3
2020import Ubuntu.Components 1.1
21−import "../Components/WindowStateStorage"
21+import Utils 0.1
2222
2323MouseArea {
2424 id: root
@@ -50,7 +50,7 @@ MouseArea {
5050 }
5151
5252 Component.onCompleted: {
53− var windowState = WindowStateStorage.getState(root.windowId)
53+ var windowState = WindowStateStorage.getGeometry(root.windowId, Qt.rect(target.x, target.y, target.width, target.height))
5454 if (windowState !== undefined) {
5555 target.x = windowState.x
5656 target.y = windowState.y
@@ -109,6 +109,6 @@ MouseArea {
109109 }
110110
111111 onReleased: {
112− WindowStateStorage.saveState(root.windowId,target.x, target.y, target.width, target.height)
112+ WindowStateStorage.saveGeometry(root.windowId, Qt.rect(target.x, target.y, target.width, target.height))
113113 }
114114}