1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
|
/*
* Copyright 2015-2016 Canonical Ltd.
* Copyright 2021 UBports Foundation
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "windowstatestorage.h"
#include <QDebug>
#include <QDir>
#include <QMetaObject>
#include <QObject>
#include <QSqlQuery>
#include <QSqlError>
#include <QSqlResult>
#include <QStandardPaths>
#include <QRect>
#include <lomiri/shell/application/ApplicationInfoInterface.h>
class AsyncQuery: public QObject
{
Q_OBJECT
public:
AsyncQuery(const QString &dbName):
m_dbName(dbName)
{
}
~AsyncQuery()
{
QSqlDatabase::removeDatabase(m_connectionName);
}
Q_INVOKABLE const QString getDbName()
{
if (!m_ok) {
return "ERROR";
}
QSqlDatabase connection = QSqlDatabase::database(m_connectionName);
return connection.databaseName();
}
Q_INVOKABLE bool initdb()
{
if (m_ok) {
return true;
}
QSqlDatabase connection = QSqlDatabase::addDatabase(QStringLiteral("QSQLITE"), m_connectionName);
connection.setDatabaseName(m_dbName);
connection.setConnectOptions(QStringLiteral("QSQLITE_BUSY_TIMEOUT=1000"));
if (!connection.open()) {
qWarning() << "AsyncQuery::initdb: Error opening state database. Window positions will not be saved or restored." << m_dbName << connection.lastError().driverText() << connection.lastError().databaseText();
return false;
}
QSqlQuery query(connection);
if (!connection.tables().contains(QStringLiteral("geometry"))) {
QString geometryQuery = QStringLiteral("CREATE TABLE geometry(windowId TEXT UNIQUE, x INTEGER, y INTEGER, width INTEGER, height INTEGER);");
if (!query.exec(geometryQuery)) {
logSqlError(query);
return false;
}
}
if (!connection.tables().contains(QStringLiteral("state"))) {
QString stateQuery = QStringLiteral("CREATE TABLE state(windowId TEXT UNIQUE, state INTEGER);");
if (!query.exec(stateQuery)) {
logSqlError(query);
return false;
}
}
if (!connection.tables().contains(QStringLiteral("stage"))) {
QString stageQuery = QStringLiteral("CREATE TABLE stage(appId TEXT UNIQUE, stage INTEGER);");
if (!query.exec(stageQuery)) {
logSqlError(query);
return false;
}
}
m_ok = true;
return true;
}
Q_INVOKABLE int getState(const QString &windowId) const
{
if (!m_ok) {
return -1;
}
QSqlDatabase connection = QSqlDatabase::database(m_connectionName);
QSqlQuery query(connection);
query.prepare(m_getStateQuery);
query.bindValue(":windowId", windowId);
query.exec();
if (!query.isActive() || !query.isSelect()) {
logSqlError(query);
return -1;
}
if (!query.first()) {
return -1;
}
bool converted = false;
QVariant resultStr = query.value(0);
int result = resultStr.toInt(&converted);
if (converted) {
return result;
} else {
qWarning() << "getState result expected integer, got " << resultStr;
return -1;
}
}
Q_INVOKABLE QRect getGeometry(const QString &windowId) const
{
if (!m_ok) {
return QRect();
}
QSqlDatabase connection = QSqlDatabase::database(m_connectionName);
QSqlQuery query(connection);
query.prepare(m_getGeometryQuery);
query.bindValue(":windowId", windowId);
query.exec();
if (!query.isActive() || !query.isSelect()) {
logSqlError(query);
return QRect();
}
if (!query.first()) {
return QRect();
}
bool xConverted, yConverted, widthConverted, heightConverted = false;
int x, y, width, height;
QVariant xResultStr = query.value(QStringLiteral("x"));
QVariant yResultStr = query.value(QStringLiteral("y"));
QVariant widthResultStr = query.value(QStringLiteral("width"));
QVariant heightResultStr = query.value(QStringLiteral("height"));
x = xResultStr.toInt(&xConverted);
y = yResultStr.toInt(&yConverted);
width = widthResultStr.toInt(&widthConverted);
height = heightResultStr.toInt(&heightConverted);
if (xConverted && yConverted && widthConverted && heightConverted) {
return QRect(x, y, width, height);
} else {
qWarning() << "getGeometry result expected integers, got x:"
<< xResultStr << "y:" << yResultStr << "width" << widthResultStr
<< "height:" << heightResultStr;
return QRect();
}
}
Q_INVOKABLE int getStage(const QString &appId) const
{
if (!m_ok) {
return -1;
}
QSqlDatabase connection = QSqlDatabase::database(m_connectionName);
QSqlQuery query(connection);
query.prepare(m_getStageQuery);
query.bindValue(":appId", appId);
query.exec();
if (!query.isActive() || !query.isSelect()) {
logSqlError(query);
return -1;
}
if (!query.first()) {
return -1;
}
bool converted = false;
QVariant resultStr = query.value(0);
int result = resultStr.toInt(&converted);
if (converted) {
return result;
} else {
qWarning() << "getStage result expected integer, got " << resultStr;
return -1;
}
}
public Q_SLOTS:
void saveState(const QString &windowId, WindowStateStorage::WindowState state)
{
if (!m_ok) {
return;
}
QSqlDatabase connection = QSqlDatabase::database(m_connectionName);
QSqlQuery query(connection);
query.prepare(m_saveStateQuery);
query.bindValue(":windowId", windowId);
query.bindValue(":state", (int)state);
if (!query.exec()) {
logSqlError(query);
}
}
void saveGeometry(const QString &windowId, const QRect &rect)
{
if (!m_ok) {
return;
}
QSqlDatabase connection = QSqlDatabase::database(m_connectionName);
QSqlQuery query(connection);
query.prepare(m_saveGeometryQuery);
query.bindValue(":windowId", windowId);
query.bindValue(":x", rect.x());
query.bindValue(":y", rect.y());
query.bindValue(":width", rect.width());
query.bindValue(":height", rect.height());
if (!query.exec()) {
logSqlError(query);
}
}
void saveStage(const QString &appId, int stage)
{
if (!m_ok) {
return;
}
QSqlDatabase connection = QSqlDatabase::database(m_connectionName);
QSqlQuery query(connection);
query.prepare(m_saveStageQuery);
query.bindValue(":appId", appId);
query.bindValue(":stage", stage);
if (!query.exec()) {
logSqlError(query);
}
}
private:
static const QString m_connectionName;
static const QString m_getStateQuery;
static const QString m_saveStateQuery;
static const QString m_getGeometryQuery;
static const QString m_saveGeometryQuery;
static const QString m_getStageQuery;
static const QString m_saveStageQuery;
static void logSqlError(const QSqlQuery query)
{
qWarning() << "Error executing query" << query.lastQuery()
<< "Driver error:" << query.lastError().driverText()
<< "Database error:" << query.lastError().databaseText();
}
QString m_dbName;
bool m_ok = false;
};
const QString AsyncQuery::m_connectionName = QStringLiteral("WindowStateStorage");
const QString AsyncQuery::m_getStateQuery = QStringLiteral("SELECT state FROM state WHERE windowId = :windowId");
const QString AsyncQuery::m_saveStateQuery = QStringLiteral("INSERT OR REPLACE INTO state (windowId, state) values (:windowId, :state)");
const QString AsyncQuery::m_getGeometryQuery = QStringLiteral("SELECT * FROM geometry WHERE windowId = :windowId");
const QString AsyncQuery::m_saveGeometryQuery = QStringLiteral("INSERT OR REPLACE INTO geometry (windowId, x, y, width, height) values (:windowId, :x, :y, :width, :height)");
const QString AsyncQuery::m_getStageQuery = QStringLiteral("SELECT stage FROM stage WHERE appId = :appId");
const QString AsyncQuery::m_saveStageQuery = QStringLiteral("INSERT OR REPLACE INTO stage (appId, stage) values (:appId, :stage)");
WindowStateStorage::WindowStateStorage(const QString &dbName, QObject *parent):
QObject(parent),
m_thread()
{
qRegisterMetaType<WindowStateStorage::WindowState>("WindowStateStorage::WindowState");
QString dbFile;
if (dbName.isEmpty()) {
const QString dbPath = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + QStringLiteral("/lomiri/");
QDir dir;
dir.mkpath(dbPath);
dbFile = QString(dbPath) + QStringLiteral("windowstatestorage.sqlite");
} else {
dbFile = dbName;
}
m_asyncQuery = new AsyncQuery(dbFile);
m_asyncQuery->moveToThread(&m_thread);
connect(&m_thread, &QThread::finished, m_asyncQuery, &QObject::deleteLater);
m_thread.start();
// Note that we're relying on initdb being called before any other methods
// on AsyncQuery. Given the current behavior of QueuedConnection (slots
// invoked in the order they are received), this is fine.
QMetaObject::invokeMethod(m_asyncQuery, "initdb",
Qt::QueuedConnection);
connect(this, &WindowStateStorage::saveState, m_asyncQuery, &AsyncQuery::saveState);
connect(this, &WindowStateStorage::saveGeometry, m_asyncQuery, &AsyncQuery::saveGeometry);
connect(this, &WindowStateStorage::saveStage, m_asyncQuery, &AsyncQuery::saveStage);
}
WindowStateStorage::~WindowStateStorage()
{
m_thread.quit();
m_thread.wait();
}
WindowStateStorage::WindowState WindowStateStorage::getState(const QString &windowId, WindowStateStorage::WindowState defaultValue) const
{
int state;
QMetaObject::invokeMethod(m_asyncQuery, "getState", Qt::BlockingQueuedConnection,
Q_RETURN_ARG(int, state),
Q_ARG(const QString&, windowId)
);
if (state == -1) {
return defaultValue;
}
return (WindowState)state;
}
int WindowStateStorage::getStage(const QString &appId, int defaultValue) const
{
int stage;
QMetaObject::invokeMethod(m_asyncQuery, "getStage", Qt::BlockingQueuedConnection,
Q_RETURN_ARG(int, stage),
Q_ARG(const QString&, appId)
);
if (stage == -1) {
return defaultValue;
}
return stage;
}
QRect WindowStateStorage::getGeometry(const QString &windowId, const QRect &defaultValue) const
{
QRect geometry;
QMetaObject::invokeMethod(m_asyncQuery, "getGeometry", Qt::BlockingQueuedConnection,
Q_RETURN_ARG(QRect, geometry),
Q_ARG(const QString&, windowId)
);
if (geometry.isNull() || !geometry.isValid()) {
return defaultValue;
}
return geometry;
}
const QString WindowStateStorage::getDbName()
{
QString dbName;
QMetaObject::invokeMethod(m_asyncQuery, "getDbName", Qt::BlockingQueuedConnection,
Q_RETURN_ARG(QString, dbName)
);
return QString(dbName);
}
Mir::State WindowStateStorage::toMirState(WindowState state) const
{
// assumes a single state (not an OR of several)
switch (state) {
case WindowStateMaximized: return Mir::MaximizedState;
case WindowStateMinimized: return Mir::MinimizedState;
case WindowStateFullscreen: return Mir::FullscreenState;
case WindowStateMaximizedLeft: return Mir::MaximizedLeftState;
case WindowStateMaximizedRight: return Mir::MaximizedRightState;
case WindowStateMaximizedHorizontally: return Mir::HorizMaximizedState;
case WindowStateMaximizedVertically: return Mir::VertMaximizedState;
case WindowStateMaximizedTopLeft: return Mir::MaximizedTopLeftState;
case WindowStateMaximizedTopRight: return Mir::MaximizedTopRightState;
case WindowStateMaximizedBottomLeft: return Mir::MaximizedBottomLeftState;
case WindowStateMaximizedBottomRight: return Mir::MaximizedBottomRightState;
case WindowStateNormal:
case WindowStateRestored:
default:
return Mir::RestoredState;
}
}
#include "windowstatestorage.moc"
|