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("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::saveGeometry(const QString &windowId, const QRect &rect)
52 QMutexLocker mutexLocker(&s_mutex);
54 QString queryString = QString("INSERT OR REPLACE INTO geometry (windowId, x, y, width, height) values ('%1', '%2', '%3', '%4', '%5');")
61 QFuture<void> future = QtConcurrent::run(executeAsyncQuery, queryString);
62 m_asyncQueries.append(future);
64 QFutureWatcher<void> *futureWatcher = new QFutureWatcher<void>();
65 futureWatcher->setFuture(future);
66 connect(futureWatcher, &QFutureWatcher<void>::finished,
68 [=](){ m_asyncQueries.removeAll(futureWatcher->future());
69 futureWatcher->deleteLater(); });
72void WindowStateStorage::executeAsyncQuery(const QString &queryString)
74 QMutexLocker l(&s_mutex);
77 bool ok = query.exec(queryString);
79 qWarning() << "Error executing query" << queryString
80 << "Driver error:" << query.lastError().driverText()
81 << "Database error:" << query.lastError().databaseText();
85QRect WindowStateStorage::getGeometry(const QString &windowId, const QRect &defaultValue)
87 QMutexLocker l(&s_mutex);
88 QString queryString = QString("SELECT * FROM geometry WHERE windowId = '%1';")
92 bool ok = query.exec(queryString);
94 qWarning() << "Error retrieving window state for" << windowId
95 << "Driver error:" << query.lastError().driverText()
96 << "Database error:" << query.lastError().databaseText();
102 return QRect(query.value("x").toInt(), query.value("y").toInt(), query.value("width").toInt(), query.value("height").toInt());
105void WindowStateStorage::initdb()
109 qWarning() << "Error opening state database:" << m_db.lastError().driverText() << m_db.lastError().databaseText();
113 if (!m_db.tables().contains("geometry")) {
115 query.exec("CREATE TABLE geometry(windowId TEXT UNIQUE, x INTEGER, y INTEGER, width INTEGER, height INTEGER);");
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