From b2fc1ad387dfd2e294d9743fe568d0efa40baafc Mon Sep 17 00:00:00 2001 From: youdiansaodongxi Date: Wed, 17 May 2023 15:59:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AF=B9=E9=A2=84=E8=A3=85?= =?UTF-8?q?=E5=BA=94=E7=94=A8=E7=9A=84=E8=AF=86=E5=88=AB=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=EF=BC=8C=E9=87=8D=E8=A3=85=E4=B8=8D=E6=98=BE=E7=A4=BA=E8=93=9D?= =?UTF-8?q?=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/appdata/app-data-manager.cpp | 130 +++++++++++++++++++ src/appdata/app-data-manager.h | 3 + src/appdata/plugin/all-app-data-provider.cpp | 16 ++- 3 files changed, 142 insertions(+), 7 deletions(-) diff --git a/src/appdata/app-data-manager.cpp b/src/appdata/app-data-manager.cpp index e1d2c98..2a6a554 100644 --- a/src/appdata/app-data-manager.cpp +++ b/src/appdata/app-data-manager.cpp @@ -20,7 +20,17 @@ #include +#include +#include +#include +#include +#include +#include + #define APP_ICON_PREFIX "image://appicon/" +#define MENU_CONFIG_FILE_PATH ".config/ukui-menu/" +#define MENU_CONFIG_FILE_NAME "config.json" + namespace UkuiMenu { @@ -51,6 +61,8 @@ public Q_SLOTS: void setAppLaunched(const QString &path); private: + void loadPreInstallAppData(); + void removePreInstallAppData(const QStringList &infos); void updateFavoriteApps(); void removeApps(QStringList& appIdList, QStringList &removedIdList); void updateApps(const UkuiSearch::ApplicationInfoMap &infos, QList &apps); @@ -59,6 +71,7 @@ private: void addInfoToApp(const UkuiSearch::ApplicationPropertyMap &info, DataEntity &app); private: + QMutex m_mutex; AppDataManager *m_appManager{nullptr}; UkuiSearch::ApplicationInfo *m_applicationInfo{nullptr}; UkuiSearch::ApplicationProperties m_appProperties; @@ -75,6 +88,7 @@ AppDataWorker::AppDataWorker(AppDataManager *appManager) : QObject(nullptr), m_a } initAppData(); + loadPreInstallAppData(); connect(m_applicationInfo, &UkuiSearch::ApplicationInfo::appDBItems2BAdd, this, &AppDataWorker::onAppAdded); connect(m_applicationInfo, &UkuiSearch::ApplicationInfo::appDBItems2BUpdate, this, &AppDataWorker::onAppUpdated); @@ -296,6 +310,8 @@ void AppDataWorker::onAppDeleted(QStringList infos) Q_EMIT appDeleted(removedIdList); updateFavoriteApps(); + + removePreInstallAppData(infos); } void AppDataWorker::fixToFavoriteSlot(const QString &path, const int &num) @@ -326,6 +342,115 @@ void AppDataWorker::setAppLaunched(const QString &path) m_applicationInfo->setAppLaunchedState(path); } +void AppDataWorker::loadPreInstallAppData() +{ + QString configpath = QDir::homePath() + "/" + MENU_CONFIG_FILE_PATH + MENU_CONFIG_FILE_NAME; + if (!QFile::exists(configpath)) { + QDir dir; + QString menuConfigDir(QDir::homePath() + "/" + MENU_CONFIG_FILE_PATH); + + if (!dir.exists(menuConfigDir)) { + if (!dir.mkdir(menuConfigDir)) { + qWarning() << "Unable to create profile menu config."; + return; + } + } + + QFile file(configpath); + + if (!file.open(QFile::WriteOnly)) { + return; + } + + QJsonDocument jsonDocument; + QJsonArray jsonArray; + + { + QMutexLocker locker(&m_mutex); + QJsonObject object; + QJsonArray apps; + for (auto &app : m_appManager->m_normalApps) { + apps.append(app.id()); + m_appManager->m_preInstallAppData.insert(app.id()); + } + object.insert("preInstall", apps); + jsonArray.append(object); + } + + jsonDocument.setArray(jsonArray); + if (file.write(jsonDocument.toJson()) == -1) { + qWarning() << "Error saving configuration file."; + } + file.flush(); + file.close(); + return; + } + + QFile file(configpath); + if (!file.open(QFile::ReadOnly)) { + return; + } + + QByteArray byteArray = file.readAll(); + file.close(); + + QJsonDocument jsonDocument(QJsonDocument::fromJson(byteArray)); + if (jsonDocument.isNull() || jsonDocument.isEmpty() || !jsonDocument.isArray()) { + qWarning() << "AppDataManager: Incorrect configuration files are ignored."; + return; + } + + { + QMutexLocker locker(&m_mutex); + m_appManager->m_preInstallAppData.clear(); + } + + QJsonArray jsonArray = jsonDocument.array(); + for (const auto &value : jsonArray) { + QJsonObject object = value.toObject(); + if (object.contains("preInstall")) { + QJsonArray apps = object.value(QLatin1String("preInstall")).toArray(); + for (const auto &app : apps) { + m_appManager->m_preInstallAppData.insert(app.toString()); + } + } + } +} + +void AppDataWorker::removePreInstallAppData(const QStringList &infos) +{ + for (const auto &info : infos) { + if (m_appManager->isPreInstalled(info)) + m_appManager->m_preInstallAppData.remove(info); + } + QFile file(QDir::homePath() + "/" + MENU_CONFIG_FILE_PATH + MENU_CONFIG_FILE_NAME); + + if (!file.open(QFile::WriteOnly|QFile::Truncate)) { + return; + } + + QJsonDocument jsonDocument; + QJsonArray jsonArray; + + { + QMutexLocker locker(&m_mutex); + QJsonObject object; + QJsonArray apps; + for (auto &app : m_appManager->m_preInstallAppData) { + apps.append(app); + } + object.insert("preInstall", apps); + jsonArray.append(object); + } + + jsonDocument.setArray(jsonArray); + if (file.write(jsonDocument.toJson()) == -1) { + qWarning() << "Error saving configuration file."; + } + file.flush(); + file.close(); +} + void AppDataWorker::removeApps(QStringList &appIdList, QStringList &removedIdList) { QMutexLocker locker(&m_appManager->m_mutex); @@ -374,6 +499,11 @@ QList AppDataManager::normalApps() return m_normalApps.values(); } +bool AppDataManager::isPreInstalled(const QString &id) +{ + return m_preInstallAppData.contains(id); +} + QVector AppDataManager::favoriteApps() { QMutexLocker locker(&m_mutex); diff --git a/src/appdata/app-data-manager.h b/src/appdata/app-data-manager.h index 03625e5..c43ec2b 100644 --- a/src/appdata/app-data-manager.h +++ b/src/appdata/app-data-manager.h @@ -26,6 +26,7 @@ #include #include #include +#include #include "commons.h" @@ -47,6 +48,7 @@ public: bool getApp(const QString &appId, DataEntity &app); QList normalApps(); + bool isPreInstalled(const QString &id); QVector favoriteApps(); Q_SIGNALS: @@ -67,6 +69,7 @@ private: QMutex m_mutex; QThread m_workerThread; QMap m_normalApps; + QSet m_preInstallAppData; QVector m_favoriteApps; }; diff --git a/src/appdata/plugin/all-app-data-provider.cpp b/src/appdata/plugin/all-app-data-provider.cpp index bdf2986..736a1b8 100644 --- a/src/appdata/plugin/all-app-data-provider.cpp +++ b/src/appdata/plugin/all-app-data-provider.cpp @@ -184,13 +184,15 @@ bool AllAppDataProvider::appDataSort(const DataEntity &a, const DataEntity &b) void AllAppDataProvider::setRecentState(DataEntity &app) { - if (app.launched() == 0) { - QDateTime installTime = QDateTime::fromString(app.insertTime(), "yyyy-MM-dd hh:mm:ss"); - if (installTime.isValid()) { - int appTime = installTime.secsTo(QDateTime::currentDateTime()); - if ((appTime >= 0 ) && (appTime <= 3600*48)) { - app.setRecentInstall(true); - return; + if (!AppDataManager::instance()->isPreInstalled(app.id())) { + if (app.launched() == 0) { + QDateTime installTime = QDateTime::fromString(app.insertTime(), "yyyy-MM-dd hh:mm:ss"); + if (installTime.isValid()) { + int appTime = installTime.secsTo(QDateTime::currentDateTime()); + if ((appTime >= 0 ) && (appTime <= 3600*48)) { + app.setRecentInstall(true); + return; + } } } }