From 1fdc150116cad39aae5c5da407c3312b47a59e3a Mon Sep 17 00:00:00 2001 From: Unit 193 Date: Fri, 17 Dec 2021 07:06:30 -0500 Subject: New upstream version 3.3.3+dfsg1. --- .../coreSQLiteStudio/services/impl/configimpl.cpp | 145 ++++++++++++++++----- .../coreSQLiteStudio/services/impl/configimpl.h | 5 + .../services/impl/dbmanagerimpl.cpp | 2 +- .../services/impl/pluginmanagerimpl.cpp | 18 ++- 4 files changed, 128 insertions(+), 42 deletions(-) (limited to 'SQLiteStudio3/coreSQLiteStudio/services/impl') diff --git a/SQLiteStudio3/coreSQLiteStudio/services/impl/configimpl.cpp b/SQLiteStudio3/coreSQLiteStudio/services/impl/configimpl.cpp index 860e828..570395a 100644 --- a/SQLiteStudio3/coreSQLiteStudio/services/impl/configimpl.cpp +++ b/SQLiteStudio3/coreSQLiteStudio/services/impl/configimpl.cpp @@ -15,10 +15,15 @@ #include #include #include +#include +#include #include +#include static_qstring(DB_FILE_NAME, "settings3"); +static_qstring(CONFIG_DIR_SETTING, "SQLiteStudioConfigDir"); qint64 ConfigImpl::sqlHistoryId = -1; +QString ConfigImpl::memoryDbName = QStringLiteral(":memory:"); ConfigImpl::~ConfigImpl() { @@ -43,7 +48,7 @@ void ConfigImpl::cleanUp() if (db->isOpen()) db->close(); - safe_delete(db); + safe_delete(db) } const QString &ConfigImpl::getConfigDir() const @@ -59,6 +64,11 @@ QString ConfigImpl::getConfigFilePath() const return db->getPath(); } +bool ConfigImpl::isInMemory() const +{ + return db->getPath() == memoryDbName; +} + void ConfigImpl::beginMassSave() { if (isMassSaving()) @@ -243,8 +253,8 @@ void ConfigImpl::storeGroup(const ConfigImpl::DbGroupPtr &group, qint64 parentId if (parentId > -1) parent = parentId; - SqlQueryPtr results = db->exec("INSERT INTO groups (name, [order], parent, open, dbname) VALUES (?, ?, ?, ?, ?)", - {group->name, group->order, parent, group->open, group->referencedDbName}); + SqlQueryPtr results = db->exec("INSERT INTO groups (name, [order], parent, open, dbname, db_expanded) VALUES (?, ?, ?, ?, ?, ?)", + {group->name, group->order, parent, group->open, group->referencedDbName, group->dbExpanded}); qint64 newParentId = results->getRegularInsertRowId(); for (const DbGroupPtr& childGroup : group->childs) @@ -261,7 +271,7 @@ QList ConfigImpl::getGroups() ConfigImpl::DbGroupPtr ConfigImpl::getDbGroup(const QString& dbName) { - SqlQueryPtr results = db->exec("SELECT id, name, [order], open, dbname FROM groups WHERE dbname = ? LIMIT 1", {dbName}); + SqlQueryPtr results = db->exec("SELECT id, name, [order], open, dbname, db_expanded FROM groups WHERE dbname = ? LIMIT 1", {dbName}); DbGroupPtr group = DbGroupPtr::create(); group->referencedDbName = dbName; @@ -274,6 +284,7 @@ ConfigImpl::DbGroupPtr ConfigImpl::getDbGroup(const QString& dbName) group->name = row->value("name").toString(); group->order = row->value("order").toInt(); group->open = row->value("open").toBool(); + group->dbExpanded = row->value("db_expanded").toBool(); return group; } @@ -553,9 +564,9 @@ void ConfigImpl::readGroupRecursively(ConfigImpl::DbGroupPtr group) { SqlQueryPtr results; if (group->id < 0) - results = db->exec("SELECT id, name, [order], open, dbname FROM groups WHERE parent IS NULL ORDER BY [order]"); + results = db->exec("SELECT id, name, [order], open, dbname, db_expanded FROM groups WHERE parent IS NULL ORDER BY [order]"); else - results = db->exec("SELECT id, name, [order], open, dbname FROM groups WHERE parent = ? ORDER BY [order]", {group->id}); + results = db->exec("SELECT id, name, [order], open, dbname, db_expanded FROM groups WHERE parent = ? ORDER BY [order]", {group->id}); DbGroupPtr childGroup; SqlResultsRowPtr row; @@ -568,6 +579,7 @@ void ConfigImpl::readGroupRecursively(ConfigImpl::DbGroupPtr group) childGroup->order = row->value("order").toInt(); childGroup->open = row->value("open").toBool(); childGroup->referencedDbName = row->value("dbname").toString(); + childGroup->dbExpanded = row->value("db_expanded").toBool(); group->childs += childGroup; } @@ -591,6 +603,11 @@ void ConfigImpl::rollback() } QString ConfigImpl::getConfigPath() +{ + return QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/" + DB_FILE_NAME; +} + +QString ConfigImpl::getLegacyConfigPath() { #ifdef Q_OS_WIN if (QSysInfo::windowsVersion() & QSysInfo::WV_NT_based) @@ -669,7 +686,7 @@ void ConfigImpl::initTables() if (!tables.contains("groups")) db->exec("CREATE TABLE groups (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, parent INTEGER REFERENCES groups(id), " "[order] INTEGER, open INTEGER DEFAULT 0, dbname TEXT UNIQUE REFERENCES dblist(name) ON UPDATE CASCADE ON DELETE CASCADE, " - "UNIQUE(name, parent))"); + "db_expanded INTEGER DEFAULT 0, UNIQUE(name, parent))"); if (!tables.contains("ddl_history")) db->exec("CREATE TABLE ddl_history (id INTEGER PRIMARY KEY AUTOINCREMENT, dbname TEXT, file TEXT, timestamp INTEGER, " @@ -712,43 +729,29 @@ void ConfigImpl::initTables() void ConfigImpl::initDbFile() { - // Determinate global config location and portable one - QString globalPath = getConfigPath(); - QString portablePath = getPortableConfigPath(); - QList> paths; - if (!globalPath.isNull() && !portablePath.isNull()) - { - if (QFileInfo(portablePath).exists()) - { - paths << QPair(portablePath+"/"+DB_FILE_NAME, false); - paths << QPair(globalPath+"/"+DB_FILE_NAME, true); - } - else - { - paths << QPair(globalPath+"/"+DB_FILE_NAME, true); - paths << QPair(portablePath+"/"+DB_FILE_NAME, false); - } - } - else if (!globalPath.isNull()) + + // Do we have path selected by user? (because app was unable to find writable location before) + QSettings sett; + QString customPath = sett.value(CONFIG_DIR_SETTING).toString(); + if (!customPath.isEmpty()) { - paths << QPair(globalPath+"/"+DB_FILE_NAME, true); - } - else if (!portablePath.isNull()) - { - paths << QPair(portablePath+"/"+DB_FILE_NAME, false); + paths << QPair(customPath + "/" + DB_FILE_NAME, false); + qDebug() << "Using custom configuration directory. The location is stored in" << sett.fileName(); } + else + paths.append(getStdDbPaths()); // A fallback to in-memory db - paths << QPair(":memory:", false); + paths << QPair(memoryDbName, false); // Go through all candidates and pick one QDir dir; for (const QPair& path : paths) { dir = QDir(path.first); - if (path.first != ":memory:") - dir.cdUp(); + if (path.first != memoryDbName) + dir = QFileInfo(path.first).dir(); if (tryInitDbFile(path)) { @@ -757,8 +760,25 @@ void ConfigImpl::initDbFile() } } + // Failed to use any of predefined directories. Let's ask the user. + while (configDir == memoryDbName) + { + QString path = askUserForConfigDirFunc(); + if (path.isNull()) + break; + + dir = QDir(path); + if (tryInitDbFile(QPair(path + "/" + DB_FILE_NAME, false))) + { + configDir = dir.absolutePath(); + QSettings sett; + sett.setValue(CONFIG_DIR_SETTING, configDir); + qDebug() << "Using custom configuration directory. The location is stored in" << sett.fileName(); + } + } + // We ended up with in-memory one? That's not good. - if (configDir == ":memory:") + if (configDir == memoryDbName) { paths.removeLast(); QStringList pathStrings; @@ -766,13 +786,38 @@ void ConfigImpl::initDbFile() pathStrings << path.first; notifyError(QObject::tr("Could not initialize configuration file. Any configuration changes and queries history will be lost after application restart." - " Tried to initialize the file at following localizations: %1.").arg(pathStrings.join(", "))); + " Unable to create a file at following locations: %1.").arg(pathStrings.join(", "))); } qDebug() << "Using configuration directory:" << configDir; db->exec("PRAGMA foreign_keys = 1;"); } +QList> ConfigImpl::getStdDbPaths() +{ + QList> paths; + + // Portable dir location has always precedense - comes first + QString portablePath = getPortableConfigPath(); + if (!portablePath.isNull()) + paths << QPair(portablePath+"/"+DB_FILE_NAME, false); + + // Determinate global config location + QString globalPath = getConfigPath(); + paths << QPair(globalPath, true); + + // If needed, migrate configuration from legacy location (pre-3.3) to new location (3.3 and later) + QString legacyGlobalPath = getLegacyConfigPath(); + if (!legacyGlobalPath.isNull()) + { + paths << QPair(legacyGlobalPath+"/"+DB_FILE_NAME, true); + if (!QFile::exists(globalPath)) + tryToMigrateOldGlobalPath(legacyGlobalPath, globalPath); + } + + return paths; +} + bool ConfigImpl::tryInitDbFile(const QPair &dbPath) { // Create global config directory if not existing @@ -1118,6 +1163,12 @@ void ConfigImpl::updateConfigDb() // 1->2 db->exec("UPDATE settings SET [key] = 'DataUncommittedError' WHERE [key] = 'DataUncommitedError'"); db->exec("UPDATE settings SET [key] = 'DataUncommitted' WHERE [key] = 'DataUncommited'"); + __attribute__((__fallthrough__)); + } + case 2: + { + // 2->3 + db->exec("ALTER TABLE groups ADD db_expanded INTEGER DEFAULT 0"); } // Add cases here for next versions, // without a "break" instruction, @@ -1129,6 +1180,30 @@ void ConfigImpl::updateConfigDb() db->commit(); } +bool ConfigImpl::tryToMigrateOldGlobalPath(const QString& oldPath, const QString& newPath) +{ + if (!QFileInfo::exists(oldPath)) + return false; + + qDebug() << "Attempting to migrate legacy config location" << oldPath << "to new location" << newPath; + QDir dir = QFileInfo(newPath).dir(); + if (!dir.exists()) + QDir::root().mkpath(dir.absolutePath()); + + if (QFile::copy(oldPath, dir.absoluteFilePath(DB_FILE_NAME))) + { + qDebug() << "Migration successful. Renaming old location file so it has '.old' suffix."; + if (QFile::rename(oldPath, oldPath+".old")) + qDebug() << "Renaming successful."; + else + qDebug() << "Renaming did not work, but it's okay. It will just remain with original name there."; + } + else + qDebug() << "Migration (copying) failed."; + + return true; +} + void ConfigImpl::refreshSqlHistory() { if (sqlHistoryModel) diff --git a/SQLiteStudio3/coreSQLiteStudio/services/impl/configimpl.h b/SQLiteStudio3/coreSQLiteStudio/services/impl/configimpl.h index 561aab4..710fbb3 100644 --- a/SQLiteStudio3/coreSQLiteStudio/services/impl/configimpl.h +++ b/SQLiteStudio3/coreSQLiteStudio/services/impl/configimpl.h @@ -22,6 +22,7 @@ class API_EXPORT ConfigImpl : public Config void cleanUp(); const QString& getConfigDir() const; QString getConfigFilePath() const; + bool isInMemory() const; void beginMassSave(); void commitMassSave(); @@ -104,6 +105,7 @@ class API_EXPORT ConfigImpl : public Config QString getPortableConfigPath(); void initTables(); void initDbFile(); + QList > getStdDbPaths(); bool tryInitDbFile(const QPair& dbPath); QVariant deserializeValue(const QVariant& value) const; @@ -131,9 +133,12 @@ class API_EXPORT ConfigImpl : public Config void mergeMasterConfig(); void updateConfigDb(); + bool tryToMigrateOldGlobalPath(const QString& oldPath, const QString& newPath); + QString getLegacyConfigPath(); static Config* instance; static qint64 sqlHistoryId; + static QString memoryDbName; Db* db = nullptr; QString configDir; diff --git a/SQLiteStudio3/coreSQLiteStudio/services/impl/dbmanagerimpl.cpp b/SQLiteStudio3/coreSQLiteStudio/services/impl/dbmanagerimpl.cpp index 217c2b7..9086257 100644 --- a/SQLiteStudio3/coreSQLiteStudio/services/impl/dbmanagerimpl.cpp +++ b/SQLiteStudio3/coreSQLiteStudio/services/impl/dbmanagerimpl.cpp @@ -284,7 +284,7 @@ QString DbManagerImpl::quickAddDb(const QString& path, const QHashgetDbNames()); if (!DBLIST->addDb(newName, path, options, false)) - return QString::null; + return QString(); return newName; } diff --git a/SQLiteStudio3/coreSQLiteStudio/services/impl/pluginmanagerimpl.cpp b/SQLiteStudio3/coreSQLiteStudio/services/impl/pluginmanagerimpl.cpp index c67156c..324f549 100644 --- a/SQLiteStudio3/coreSQLiteStudio/services/impl/pluginmanagerimpl.cpp +++ b/SQLiteStudio3/coreSQLiteStudio/services/impl/pluginmanagerimpl.cpp @@ -87,7 +87,7 @@ QStringList PluginManagerImpl::getPluginDirs() const QString PluginManagerImpl::getFilePath(Plugin* plugin) const { if (!pluginContainer.contains(plugin->getName())) - return QString::null; + return QString(); return pluginContainer[plugin->getName()]->filePath; } @@ -339,7 +339,13 @@ bool PluginManagerImpl::initPlugin(Plugin* plugin) bool PluginManagerImpl::shouldAutoLoad(const QString& pluginName) { - QStringList loadedPlugins = CFG_CORE.General.LoadedPlugins.get().split(",", QString::SkipEmptyParts); + QStringList loadedPlugins = CFG_CORE.General.LoadedPlugins.get().split(",", +#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) + Qt::SkipEmptyParts +#else + QString::SkipEmptyParts +#endif + ); QStringList pair; for (const QString& loadedPlugin : loadedPlugins) { @@ -385,7 +391,7 @@ PluginType* PluginManagerImpl::getPluginType(const QString& pluginName) const QString PluginManagerImpl::getAuthor(const QString& pluginName) const { if (!pluginContainer.contains(pluginName)) - return QString::null; + return QString(); return pluginContainer[pluginName]->author; } @@ -393,7 +399,7 @@ QString PluginManagerImpl::getAuthor(const QString& pluginName) const QString PluginManagerImpl::getTitle(const QString& pluginName) const { if (!pluginContainer.contains(pluginName)) - return QString::null; + return QString(); return pluginContainer[pluginName]->title; } @@ -401,7 +407,7 @@ QString PluginManagerImpl::getTitle(const QString& pluginName) const QString PluginManagerImpl::getPrintableVersion(const QString& pluginName) const { if (!pluginContainer.contains(pluginName)) - return QString::null; + return QString(); return pluginContainer[pluginName]->printableVersion; } @@ -417,7 +423,7 @@ int PluginManagerImpl::getVersion(const QString& pluginName) const QString PluginManagerImpl::getDescription(const QString& pluginName) const { if (!pluginContainer.contains(pluginName)) - return QString::null; + return QString(); return pluginContainer[pluginName]->description; } -- cgit v1.2.3