on ab01b51 help sign in
unity8/plugins/Utils/windowstatestorage.cpp
93 lines 3.1 kB history raw
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 */
17#include "windowstatestorage.h"
19#include <QtConcurrent>
20#include <QDebug>
21#include <QSqlQuery>
22#include <QSqlError>
23#include <QSqlResult>
24#include <QRect>
26QMutex WindowStateStorage::s_mutex;
28WindowStateStorage::WindowStateStorage(QObject *parent):
29 QObject(parent)
31 m_db = QSqlDatabase::addDatabase("QSQLITE");
32 m_db.setDatabaseName(QDir::homePath() + "/.cache/unity8/windowstatestorage.sqlite");
33 initdb();
36void WindowStateStorage::saveGeometry(const QString &windowId, const QRect &rect)
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());
45 QtConcurrent::run(executeAsyncQuery, queryString);
48void WindowStateStorage::executeAsyncQuery(const QString &queryString)
50 QMutexLocker l(&s_mutex);
51 QSqlQuery query;
53 bool ok = query.exec(queryString);
54 if (!ok) {
55 qWarning() << "Error esecuting query" << queryString
56 << "Driver error:" << query.lastError().driverText()
57 << "Database error:" << query.lastError().databaseText();
58 }
61QRect WindowStateStorage::getGeometry(const QString &windowId, const QRect &defaultValue)
63 QMutexLocker l(&s_mutex);
64 QString queryString = QString("SELECT * FROM geometry WHERE windowId = '%1';")
65 .arg(windowId);
66 QSqlQuery query;
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());
81void WindowStateStorage::initdb()
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 }
89 if (!m_db.tables().contains("geometry")) {
90 QSqlQuery query;
91 query.exec("CREATE TABLE geometry(windowId TEXT UNIQUE, x INTEGER, y INTEGER, width INTEGER, height INTEGER);");
92 }