2 * Copyright 2015 Canonical Ltd.
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.
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.
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/>.
17#include "windowstatestorage.h"
19#include <QtConcurrent>
21#include <QFutureSynchronizer>
26#include <unity/shell/application/ApplicationInfoInterface.h>
28QMutex WindowStateStorage::s_mutex;
30inline QString sanitiseString(QString string) {
31 return string.remove("\"").remove("'").remove("\\");
34WindowStateStorage::WindowStateStorage(QObject *parent):
37 QString dbPath = QDir::homePath() + "/.cache/unity8/";
38 m_db = QSqlDatabase::addDatabase(QStringLiteral("QSQLITE"));
41 m_db.setDatabaseName(dbPath + "windowstatestorage.sqlite");
45WindowStateStorage::~WindowStateStorage()
47 QFutureSynchronizer<void> futureSync;
48 for (int i = 0; i < m_asyncQueries.count(); ++i) {
49 futureSync.addFuture(m_asyncQueries[i]);
51 futureSync.waitForFinished();
55void WindowStateStorage::saveState(const QString &windowId, WindowStateStorage::WindowState state)
57 const QString queryString = QStringLiteral("INSERT OR REPLACE INTO state (windowId, state) values ('%1', '%2');")
58 .arg(sanitiseString(windowId))
61 saveValue(queryString);
64WindowStateStorage::WindowState WindowStateStorage::getState(const QString &windowId, WindowStateStorage::WindowState defaultValue) const
66 const QString queryString = QStringLiteral("SELECT * FROM state WHERE windowId = '%1';")
67 .arg(sanitiseString(windowId));
69 QSqlQuery query = getValue(queryString);
74 return (WindowState)query.value(QStringLiteral("state")).toInt();
77void WindowStateStorage::saveGeometry(const QString &windowId, const QRect rect)
79 const QString queryString = QStringLiteral("INSERT OR REPLACE INTO geometry (windowId, x, y, width, height) values ('%1', '%2', '%3', '%4', '%5');")
80 .arg(sanitiseString(windowId))
86 saveValue(queryString);
89void WindowStateStorage::saveStage(const QString &appId, int stage)
91 const QString queryString = QStringLiteral("INSERT OR REPLACE INTO stage (appId, stage) values ('%1', '%2');")
92 .arg(sanitiseString(appId))
95 saveValue(queryString);
98int WindowStateStorage::getStage(const QString &appId) const
100 const QString queryString = QStringLiteral("SELECT * FROM stage WHERE appId = '%1';")
101 .arg(sanitiseString(appId));
103 QSqlQuery query = getValue(queryString);
105 if (!query.first()) {
106 return unity::shell::application::ApplicationInfoInterface::MainStage;
108 return query.value("stage").toInt();
111void WindowStateStorage::executeAsyncQuery(const QString &queryString)
113 QMutexLocker l(&s_mutex);
116 bool ok = query.exec(queryString);
118 qWarning() << "Error executing query" << queryString
119 << "Driver error:" << query.lastError().driverText()
120 << "Database error:" << query.lastError().databaseText();
124QRect WindowStateStorage::getGeometry(const QString &windowId, const QRect defaultValue) const
126 QString queryString = QStringLiteral("SELECT * FROM geometry WHERE windowId = '%1';")
127 .arg(sanitiseString(windowId));
129 QSqlQuery query = getValue(queryString);
131 if (!query.first()) {
134 return QRect(query.value(QStringLiteral("x")).toInt(), query.value(QStringLiteral("y")).toInt(), query.value(QStringLiteral("width")).toInt(), query.value(QStringLiteral("height")).toInt());
137void WindowStateStorage::initdb()
141 qWarning() << "Error opening state database:" << m_db.lastError().driverText() << m_db.lastError().databaseText();
145 if (!m_db.tables().contains(QStringLiteral("geometry"))) {
147 query.exec(QStringLiteral("CREATE TABLE geometry(windowId TEXT UNIQUE, x INTEGER, y INTEGER, width INTEGER, height INTEGER);"));
150 if (!m_db.tables().contains(QStringLiteral("state"))) {
152 query.exec(QStringLiteral("CREATE TABLE state(windowId TEXT UNIQUE, state INTEGER);"));
155 if (!m_db.tables().contains(QStringLiteral("stage"))) {
157 query.exec(QStringLiteral("CREATE TABLE stage(appId TEXT UNIQUE, stage INTEGER);"));
161void WindowStateStorage::saveValue(const QString &queryString)
163 QMutexLocker mutexLocker(&s_mutex);
165 QFuture<void> future = QtConcurrent::run(executeAsyncQuery, queryString);
166 m_asyncQueries.append(future);
168 QFutureWatcher<void> *futureWatcher = new QFutureWatcher<void>();
169 futureWatcher->setFuture(future);
170 connect(futureWatcher, &QFutureWatcher<void>::finished,
172 [=](){ m_asyncQueries.removeAll(futureWatcher->future());
173 futureWatcher->deleteLater(); });
176QSqlQuery WindowStateStorage::getValue(const QString &queryString) const
178 QMutexLocker l(&s_mutex);
181 bool ok = query.exec(queryString);
183 qWarning() << "Error retrieving database query:" << queryString
184 << "Driver error:" << query.lastError().driverText()
185 << "Database error:" << query.lastError().databaseText();
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list