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>
27QMutex WindowStateStorage::s_mutex;
29WindowStateStorage::WindowStateStorage(QObject *parent):
32 QString dbPath = QDir::homePath() + "/.cache/unity8/";
33 m_db = QSqlDatabase::addDatabase(QStringLiteral("QSQLITE"));
36 m_db.setDatabaseName(dbPath + "windowstatestorage.sqlite");
40WindowStateStorage::~WindowStateStorage()
42 QFutureSynchronizer<void> futureSync;
43 for (int i = 0; i < m_asyncQueries.count(); ++i) {
44 futureSync.addFuture(m_asyncQueries[i]);
46 futureSync.waitForFinished();
50void WindowStateStorage::saveState(const QString &windowId, WindowStateStorage::WindowState state)
52 const QString queryString = QStringLiteral("INSERT OR REPLACE INTO state (windowId, state) values ('%1', '%2');")
56 saveValue(queryString);
59WindowStateStorage::WindowState WindowStateStorage::getState(const QString &windowId, WindowStateStorage::WindowState defaultValue) const
61 const QString queryString = QStringLiteral("SELECT * FROM state WHERE windowId = '%1';")
64 QSqlQuery query = getValue(queryString);
69 return (WindowState)query.value(QStringLiteral("state")).toInt();
72void WindowStateStorage::saveGeometry(const QString &windowId, const QRect rect)
74 const QString queryString = QStringLiteral("INSERT OR REPLACE INTO geometry (windowId, x, y, width, height) values ('%1', '%2', '%3', '%4', '%5');")
81 saveValue(queryString);
84void WindowStateStorage::executeAsyncQuery(const QString &queryString)
86 QMutexLocker l(&s_mutex);
89 bool ok = query.exec(queryString);
91 qWarning() << "Error executing query" << queryString
92 << "Driver error:" << query.lastError().driverText()
93 << "Database error:" << query.lastError().databaseText();
97QRect WindowStateStorage::getGeometry(const QString &windowId, const QRect defaultValue) const
99 QString queryString = QStringLiteral("SELECT * FROM geometry WHERE windowId = '%1';")
102 QSqlQuery query = getValue(queryString);
104 if (!query.first()) {
107 return QRect(query.value(QStringLiteral("x")).toInt(), query.value(QStringLiteral("y")).toInt(), query.value(QStringLiteral("width")).toInt(), query.value(QStringLiteral("height")).toInt());
110void WindowStateStorage::initdb()
114 qWarning() << "Error opening state database:" << m_db.lastError().driverText() << m_db.lastError().databaseText();
118 if (!m_db.tables().contains(QStringLiteral("geometry"))) {
120 query.exec(QStringLiteral("CREATE TABLE geometry(windowId TEXT UNIQUE, x INTEGER, y INTEGER, width INTEGER, height INTEGER);"));
123 if (!m_db.tables().contains(QStringLiteral("state"))) {
125 query.exec(QStringLiteral("CREATE TABLE state(windowId TEXT UNIQUE, state INTEGER);"));
129void WindowStateStorage::saveValue(const QString &queryString)
131 QMutexLocker mutexLocker(&s_mutex);
133 QFuture<void> future = QtConcurrent::run(executeAsyncQuery, queryString);
134 m_asyncQueries.append(future);
136 QFutureWatcher<void> *futureWatcher = new QFutureWatcher<void>();
137 futureWatcher->setFuture(future);
138 connect(futureWatcher, &QFutureWatcher<void>::finished,
140 [=](){ m_asyncQueries.removeAll(futureWatcher->future());
141 futureWatcher->deleteLater(); });
144QSqlQuery WindowStateStorage::getValue(const QString &queryString) const
146 QMutexLocker l(&s_mutex);
149 bool ok = query.exec(queryString);
151 qWarning() << "Error retrieving database query:" << queryString
152 << "Driver error:" << query.lastError().driverText()
153 << "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