From 1d737c8f2a26a088763b4025efddad2a217b4262 Mon Sep 17 00:00:00 2001 From: jixiaoxu Date: Wed, 21 Dec 2022 11:04:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=9C=8D=E5=8A=A1=E5=BA=94?= =?UTF-8?q?=E7=94=A8=E6=95=B0=E6=8D=AE=E5=BA=93=E6=96=B0=E5=A2=9E=E8=B7=9F?= =?UTF-8?q?=E9=9A=8F=E7=89=88=E6=9C=AC=E6=9B=B4=E6=96=B0=E6=9C=BA=E5=88=B6?= =?UTF-8?q?=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app-db-manager.cpp | 161 ++++++++---------- ukui-search-app-data-service/app-db-manager.h | 10 +- 2 files changed, 80 insertions(+), 91 deletions(-) diff --git a/ukui-search-app-data-service/app-db-manager.cpp b/ukui-search-app-data-service/app-db-manager.cpp index e61e155..5012410 100644 --- a/ukui-search-app-data-service/app-db-manager.cpp +++ b/ukui-search-app-data-service/app-db-manager.cpp @@ -13,7 +13,8 @@ #define LAST_LOCALE_NAME QDir::homePath() + "/.config/org.ukui/ukui-search/appdata/last-locale-name.conf" #define LOCALE_NAME_VALUE "CurrentLocaleName" - +#define APP_DATABASE_VERSION_CONFIG QDir::homePath() + "/.config/org.ukui/ukui-search/appdata/app-database-version.conf" +#define APP_DATABASE_VERSION_VALUE "AppDatabaseVersion" using namespace UkuiSearch; static AppDBManager *global_instance; @@ -32,22 +33,47 @@ AppDBManager::AppDBManager(QObject *parent) : QThread(parent), m_database(QSqlDa //链接数据库 if (openDataBase()) { //监听系统语言变化 - m_qSettings = new QSettings(LAST_LOCALE_NAME, QSettings::IniFormat); + m_lastLocaleNameQsettings = new QSettings(LAST_LOCALE_NAME, QSettings::IniFormat); m_localeChanged = false; - m_qSettings->beginGroup(LOCALE_NAME_VALUE); - QString lastLocale = m_qSettings->value(LOCALE_NAME_VALUE).toString(); + m_lastLocaleNameQsettings->beginGroup(LOCALE_NAME_VALUE); + QString lastLocale = m_lastLocaleNameQsettings->value(LOCALE_NAME_VALUE).toString(); if (QLocale::system().name().compare(lastLocale)) { qDebug() << "I'm going to update the locale name in conf file."; if (!lastLocale.isEmpty()) { m_localeChanged = true; } - m_qSettings->setValue(LOCALE_NAME_VALUE, QLocale::system().name()); + m_lastLocaleNameQsettings->setValue(LOCALE_NAME_VALUE, QLocale::system().name()); + } + m_lastLocaleNameQsettings->endGroup(); + + //检查数据库版本 + m_dbVersionQsettings = new QSettings(APP_DATABASE_VERSION_CONFIG, QSettings::IniFormat); + m_dbVersionQsettings->beginGroup(APP_DATABASE_VERSION_VALUE); + QString dbVersion = m_dbVersionQsettings->value(APP_DATABASE_VERSION_VALUE).toString(); + if (dbVersion.isEmpty()) { + m_dbVersionNeedUpdate = true; + }else if (dbVersion != APP_DATABASE_VERSION) { + if (dbVersion.toDouble() < APP_DATABASE_VERSION.toDouble()) { + m_dbVersionNeedUpdate = true; + } else { + qDebug() << "app db version old version:" << dbVersion.toDouble() << "new version:" << APP_DATABASE_VERSION.toDouble(); + } + } + m_dbVersionQsettings->endGroup(); + + if (m_dbVersionNeedUpdate) { + qDebug() << "app db version need update! old version:" << dbVersion.toDouble() << "new version:" << APP_DATABASE_VERSION.toDouble(); } - m_qSettings->endGroup(); //初始化数据库 refreshAllData2DB(); -// refreshDataBase(); + + if (m_dbVersionNeedUpdate) { + m_dbVersionQsettings->beginGroup(APP_DATABASE_VERSION_VALUE); + m_dbVersionQsettings->setValue(APP_DATABASE_VERSION_VALUE, APP_DATABASE_VERSION); + m_dbVersionQsettings->endGroup(); + m_dbVersionNeedUpdate = false; + } //初始化FileSystemWatcher m_watcher = new FileSystemWatcher; @@ -266,85 +292,7 @@ void AppDBManager::buildAppInfoDB() void AppDBManager::updateAppInfoDB() { - /* - QMutexLocker locker(&s_mutex); - m_installAppMap.clear(); - this->getAllDesktopFilePath(GENERAL_APP_DESKTOP_PATH); - this->getAllDesktopFilePath(ANDROID_APP_DESKTOP_PATH); - this->getAllDesktopFilePath(SNAPD_APP_DESKTOP_PATH); - QStringList filePathList; - this->getFilePathList(filePathList); - QSqlQuery sql(m_database); - QString cmd = "SELECT COUNT(*) FROM APPINFO"; - if (!sql.exec(cmd)) { - this->buildAppInfoDB(); - for (auto &filePath : filePathList) { - this->addAppDesktopFile2DB(filePath); - } - } else { - if (sql.exec(cmd)) { - if (sql.next()) { - //删除多余项 - if (sql.value(0).toInt() > filePathList.size()) { - int size = sql.value(0).toInt(); - cmd = QString("SELECT DESKTOP_FILE_PATH FROM APPINFO"); - if (!sql.exec(cmd)) { - qWarning() << m_database.lastError() << cmd; - return; - } - QString path; - for (int i = 0; ideleteAppDesktopFile2DB(path); - break; - } - } - } - //添加新增项,根据md5判断desktop文件是否改变以更新对应项 - for (QString &filePath : filePathList) { - cmd = QString("SELECT COUNT(*) FROM APPINFO WHERE DESKTOP_FILE_PATH = '%0'").arg(filePath); - if (sql.exec(cmd)) { - if (sql.next()) { - if (sql.value(0).toInt() == 0) { - this->addAppDesktopFile2DB(filePath); - } else { - cmd = QString("SELECT MD5 FROM APPINFO WHERE DESKTOP_FILE_PATH = '%0'").arg(filePath); - if (!sql.exec(cmd)) { - qWarning() << m_database.lastError() << cmd; - return; - } - if (!sql.next()) { - qWarning() << m_database.lastError() << cmd; - return; - } - if (sql.value(0).toString() != getAppDesktopMd5(filePath)) { - this->updateAppDesktopFile2DB(filePath); - } - } - } else { - qWarning() << m_database.lastError() << cmd; - return; - } - } else { - qWarning() << m_database.lastError() << cmd; - return; - } - } - } else { - qWarning() << m_database.lastError() << cmd; - return; - } - } else { - qWarning() << m_database.lastError() << cmd; - return; - } - } - */ + } void AppDBManager::getFilePathList(QStringList &pathList) @@ -431,9 +379,43 @@ void AppDBManager::loadDesktopFilePaths(QString path, QFileInfoList &infolist) infolist.append(dir.entryInfoList()); } +bool AppDBManager::addItem2BackIfNotExist(QString itemName, QString itemDataType, QVariant defult) +{ + bool res(true); + QSqlQuery sql(m_database); + QString cmd = QString("SELECT * FROM sqlite_master WHERE name = 'appInfo' AND sql like '% %1 %' ").arg(itemName); + if (!sql.exec(cmd)) { + qWarning() << m_database.lastError() << cmd; + res = false; + return res; + } + if (sql.next()) { + qDebug() << cmd << "is exist!"; + return res; + } + + cmd = QString("ALTER TABLE appInfo ADD '%0' '%1' ").arg(itemName) + .arg(itemDataType); + if (defult != QVariant()) { + //TODO 根据数据类型将初始值转化为对应格式数据 + if (itemDataType == "INT(4)") { + cmd += QString("DEFAULT %1").arg(defult.toInt()); + } else if (itemDataType == "TEXT") { + cmd += QString("DEFAULT %1").arg(QString(defult.toByteArray())); + } + } + if (!sql.exec(cmd)) { + qWarning() << m_database.lastError() << cmd; + res = false; + } + if (!res) { + qDebug() << "Fail to addItem2Back :" << itemName; + } + return res; +} + void AppDBManager::refreshAllData2DB() { - m_dbChanged = false; bool firstExec = false; QSqlQuery sql(m_database); QString cmd = "SELECT DESKTOP_FILE_PATH,MD5 FROM APPINFO"; @@ -482,7 +464,8 @@ void AppDBManager::refreshAllData2DB() if (!firstExec) { //数据库有记录 if (dataMap.contains(path)) { - if (!QString::compare(dataMap.value(path), getAppDesktopMd5(path))) { + if (!QString::compare(dataMap.value(path), getAppDesktopMd5(path)) + and !m_dbVersionNeedUpdate) { //判断系统语言是否改变 if (m_localeChanged) { this->updateLocaleData(path); @@ -490,7 +473,7 @@ void AppDBManager::refreshAllData2DB() dataMap.remove(path); continue; } else { - //数据库有记录但md5值改变则update + //数据库有记录但md5值改变或数据库版本需要更新则update this->updateDBItem(path); dataMap.remove(path); continue; diff --git a/ukui-search-app-data-service/app-db-manager.h b/ukui-search-app-data-service/app-db-manager.h index 9699e33..f0ae476 100644 --- a/ukui-search-app-data-service/app-db-manager.h +++ b/ukui-search-app-data-service/app-db-manager.h @@ -20,6 +20,7 @@ //#include #define CONNECTION_NAME QLatin1String("ukss-appdb-connection") +static const QString APP_DATABASE_VERSION = QStringLiteral("1.0"); namespace UkuiSearch { /** @@ -110,6 +111,10 @@ private: //加载指定路径path中的所有desktop文件路径到infolist中 void loadDesktopFilePaths(QString path, QFileInfoList &infolist); + //数据库查找指定字段不存在则添加到最后并设置初始值 + bool addItem2BackIfNotExist(QString itemName, QString itemDataType, QVariant defult = QVariant()); + + //链接数据库 bool openDataBase(); //刷新数据库 @@ -129,9 +134,10 @@ private: private: static QMutex s_mutex; bool m_localeChanged; - bool m_dbChanged; + bool m_dbVersionNeedUpdate = false; - QSettings *m_qSettings = nullptr; + QSettings *m_lastLocaleNameQsettings = nullptr; + QSettings *m_dbVersionQsettings = nullptr; // QTimer *m_timer = nullptr; // QTimer *m_maxProcessTimer = nullptr;