Unity 8
windowstatestorage.cpp
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 <QFutureSynchronizer>
22 #include <QSqlQuery>
23 #include <QSqlError>
24 #include <QSqlResult>
25 #include <QRect>
26 
27 QMutex WindowStateStorage::s_mutex;
28 
29 WindowStateStorage::WindowStateStorage(QObject *parent):
30  QObject(parent)
31 {
32  QString dbPath = QDir::homePath() + "/.cache/unity8/";
33  m_db = QSqlDatabase::addDatabase("QSQLITE");
34  QDir dir;
35  dir.mkpath(dbPath);
36  m_db.setDatabaseName(dbPath + "windowstatestorage.sqlite");
37  initdb();
38 }
39 
40 WindowStateStorage::~WindowStateStorage()
41 {
42  QFutureSynchronizer<void> futureSync;
43  for (int i = 0; i < m_asyncQueries.count(); ++i) {
44  futureSync.addFuture(m_asyncQueries[i]);
45  }
46  futureSync.waitForFinished();
47  m_db.close();
48 }
49 
50 void WindowStateStorage::saveGeometry(const QString &windowId, const QRect &rect)
51 {
52  QMutexLocker mutexLocker(&s_mutex);
53 
54  QString queryString = QString("INSERT OR REPLACE INTO geometry (windowId, x, y, width, height) values ('%1', '%2', '%3', '%4', '%5');")
55  .arg(windowId)
56  .arg(rect.x())
57  .arg(rect.y())
58  .arg(rect.width())
59  .arg(rect.height());
60 
61  QFuture<void> future = QtConcurrent::run(executeAsyncQuery, queryString);
62  m_asyncQueries.append(future);
63 
64  QFutureWatcher<void> *futureWatcher = new QFutureWatcher<void>();
65  futureWatcher->setFuture(future);
66  connect(futureWatcher, &QFutureWatcher<void>::finished,
67  this,
68  [=](){ m_asyncQueries.removeAll(futureWatcher->future());
69  futureWatcher->deleteLater(); });
70 }
71 
72 void WindowStateStorage::executeAsyncQuery(const QString &queryString)
73 {
74  QMutexLocker l(&s_mutex);
75  QSqlQuery query;
76 
77  bool ok = query.exec(queryString);
78  if (!ok) {
79  qWarning() << "Error executing query" << queryString
80  << "Driver error:" << query.lastError().driverText()
81  << "Database error:" << query.lastError().databaseText();
82  }
83 }
84 
85 QRect WindowStateStorage::getGeometry(const QString &windowId, const QRect &defaultValue)
86 {
87  QMutexLocker l(&s_mutex);
88  QString queryString = QString("SELECT * FROM geometry WHERE windowId = '%1';")
89  .arg(windowId);
90  QSqlQuery query;
91 
92  bool ok = query.exec(queryString);
93  if (!ok) {
94  qWarning() << "Error retrieving window state for" << windowId
95  << "Driver error:" << query.lastError().driverText()
96  << "Database error:" << query.lastError().databaseText();
97  return defaultValue;
98  }
99  if (!query.first()) {
100  return defaultValue;
101  }
102  return QRect(query.value("x").toInt(), query.value("y").toInt(), query.value("width").toInt(), query.value("height").toInt());
103 }
104 
105 void WindowStateStorage::initdb()
106 {
107  m_db.open();
108  if (!m_db.open()) {
109  qWarning() << "Error opening state database:" << m_db.lastError().driverText() << m_db.lastError().databaseText();
110  return;
111  }
112 
113  if (!m_db.tables().contains("geometry")) {
114  QSqlQuery query;
115  query.exec("CREATE TABLE geometry(windowId TEXT UNIQUE, x INTEGER, y INTEGER, width INTEGER, height INTEGER);");
116  }
117 }