添加对预装应用的识别功能,重装不显示蓝点

This commit is contained in:
youdiansaodongxi 2023-05-17 15:59:29 +08:00 committed by He Sir
parent d28fbb0487
commit b2fc1ad387
3 changed files with 142 additions and 7 deletions

View File

@ -20,7 +20,17 @@
#include <application-info.h> #include <application-info.h>
#include <QFile>
#include <QDir>
#include <QDebug>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#define APP_ICON_PREFIX "image://appicon/" #define APP_ICON_PREFIX "image://appicon/"
#define MENU_CONFIG_FILE_PATH ".config/ukui-menu/"
#define MENU_CONFIG_FILE_NAME "config.json"
namespace UkuiMenu { namespace UkuiMenu {
@ -51,6 +61,8 @@ public Q_SLOTS:
void setAppLaunched(const QString &path); void setAppLaunched(const QString &path);
private: private:
void loadPreInstallAppData();
void removePreInstallAppData(const QStringList &infos);
void updateFavoriteApps(); void updateFavoriteApps();
void removeApps(QStringList& appIdList, QStringList &removedIdList); void removeApps(QStringList& appIdList, QStringList &removedIdList);
void updateApps(const UkuiSearch::ApplicationInfoMap &infos, QList<DataEntity> &apps); void updateApps(const UkuiSearch::ApplicationInfoMap &infos, QList<DataEntity> &apps);
@ -59,6 +71,7 @@ private:
void addInfoToApp(const UkuiSearch::ApplicationPropertyMap &info, DataEntity &app); void addInfoToApp(const UkuiSearch::ApplicationPropertyMap &info, DataEntity &app);
private: private:
QMutex m_mutex;
AppDataManager *m_appManager{nullptr}; AppDataManager *m_appManager{nullptr};
UkuiSearch::ApplicationInfo *m_applicationInfo{nullptr}; UkuiSearch::ApplicationInfo *m_applicationInfo{nullptr};
UkuiSearch::ApplicationProperties m_appProperties; UkuiSearch::ApplicationProperties m_appProperties;
@ -75,6 +88,7 @@ AppDataWorker::AppDataWorker(AppDataManager *appManager) : QObject(nullptr), m_a
} }
initAppData(); initAppData();
loadPreInstallAppData();
connect(m_applicationInfo, &UkuiSearch::ApplicationInfo::appDBItems2BAdd, this, &AppDataWorker::onAppAdded); connect(m_applicationInfo, &UkuiSearch::ApplicationInfo::appDBItems2BAdd, this, &AppDataWorker::onAppAdded);
connect(m_applicationInfo, &UkuiSearch::ApplicationInfo::appDBItems2BUpdate, this, &AppDataWorker::onAppUpdated); connect(m_applicationInfo, &UkuiSearch::ApplicationInfo::appDBItems2BUpdate, this, &AppDataWorker::onAppUpdated);
@ -296,6 +310,8 @@ void AppDataWorker::onAppDeleted(QStringList infos)
Q_EMIT appDeleted(removedIdList); Q_EMIT appDeleted(removedIdList);
updateFavoriteApps(); updateFavoriteApps();
removePreInstallAppData(infos);
} }
void AppDataWorker::fixToFavoriteSlot(const QString &path, const int &num) void AppDataWorker::fixToFavoriteSlot(const QString &path, const int &num)
@ -326,6 +342,115 @@ void AppDataWorker::setAppLaunched(const QString &path)
m_applicationInfo->setAppLaunchedState(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) void AppDataWorker::removeApps(QStringList &appIdList, QStringList &removedIdList)
{ {
QMutexLocker locker(&m_appManager->m_mutex); QMutexLocker locker(&m_appManager->m_mutex);
@ -374,6 +499,11 @@ QList<DataEntity> AppDataManager::normalApps()
return m_normalApps.values(); return m_normalApps.values();
} }
bool AppDataManager::isPreInstalled(const QString &id)
{
return m_preInstallAppData.contains(id);
}
QVector<DataEntity> AppDataManager::favoriteApps() QVector<DataEntity> AppDataManager::favoriteApps()
{ {
QMutexLocker locker(&m_mutex); QMutexLocker locker(&m_mutex);

View File

@ -26,6 +26,7 @@
#include <QMutexLocker> #include <QMutexLocker>
#include <QList> #include <QList>
#include <QMap> #include <QMap>
#include <QSet>
#include "commons.h" #include "commons.h"
@ -47,6 +48,7 @@ public:
bool getApp(const QString &appId, DataEntity &app); bool getApp(const QString &appId, DataEntity &app);
QList<DataEntity> normalApps(); QList<DataEntity> normalApps();
bool isPreInstalled(const QString &id);
QVector<DataEntity> favoriteApps(); QVector<DataEntity> favoriteApps();
Q_SIGNALS: Q_SIGNALS:
@ -67,6 +69,7 @@ private:
QMutex m_mutex; QMutex m_mutex;
QThread m_workerThread; QThread m_workerThread;
QMap <QString, DataEntity> m_normalApps; QMap <QString, DataEntity> m_normalApps;
QSet<QString> m_preInstallAppData;
QVector<DataEntity> m_favoriteApps; QVector<DataEntity> m_favoriteApps;
}; };

View File

@ -184,6 +184,7 @@ bool AllAppDataProvider::appDataSort(const DataEntity &a, const DataEntity &b)
void AllAppDataProvider::setRecentState(DataEntity &app) void AllAppDataProvider::setRecentState(DataEntity &app)
{ {
if (!AppDataManager::instance()->isPreInstalled(app.id())) {
if (app.launched() == 0) { if (app.launched() == 0) {
QDateTime installTime = QDateTime::fromString(app.insertTime(), "yyyy-MM-dd hh:mm:ss"); QDateTime installTime = QDateTime::fromString(app.insertTime(), "yyyy-MM-dd hh:mm:ss");
if (installTime.isValid()) { if (installTime.isValid()) {
@ -194,6 +195,7 @@ void AllAppDataProvider::setRecentState(DataEntity &app)
} }
} }
} }
}
app.setRecentInstall(false); app.setRecentInstall(false);
} }