diff --git a/src/appdata/app-data-manager.cpp b/src/appdata/app-data-manager.cpp index d5d6ece..9ca2436 100644 --- a/src/appdata/app-data-manager.cpp +++ b/src/appdata/app-data-manager.cpp @@ -31,7 +31,9 @@ public: explicit AppDataWorker(AppDataManager *appManager = nullptr); Q_SIGNALS: - void appDataChanged(); + void appAdded(QList apps); + void appUpdated(QList apps); + void appDeleted(QStringList idList); void favoriteAppChanged(); void appDataBaseOpenFailed(); @@ -43,14 +45,19 @@ private Q_SLOTS: private: void updateFavoriteApps(); + void removeApps(QStringList& appIdList, QStringList &removedIdList); + void updateApps(QVector &infos, QList &apps); + void appendApps(const QVector &infos, QList &apps); private: AppDataManager *m_appManager{nullptr}; - UkuiSearch::AppInfoTable *m_appInfoTable = nullptr; + UkuiSearch::AppInfoTable *m_appInfoTable{nullptr}; }; AppDataWorker::AppDataWorker(AppDataManager *appManager) : QObject(nullptr), m_appManager(appManager) { + qRegisterMetaType >("QList"); + qRegisterMetaType >("QVector"); m_appInfoTable = new UkuiSearch::AppInfoTable(this); if (!m_appInfoTable || !m_appManager) { return; @@ -75,9 +82,6 @@ void AppDataWorker::initAppData() return; } - // parse app-info - QVector normalApps; - for (const auto &info : appInfos) { DataEntity app; app.setTop(info.top); @@ -91,12 +95,7 @@ void AppDataWorker::initAppData() app.setCategory(info.category); app.setFirstLetter(info.firstLetter); - normalApps.append(app); - } - - { - QMutexLocker locker(&m_appManager->m_mutex); - m_appManager->m_normalApps.swap(normalApps); + m_appManager->m_normalApps.append(app); } updateFavoriteApps(); @@ -134,25 +133,30 @@ void AppDataWorker::onAppAdded(const QVector &infos) return; } - { - QMutexLocker locker(&m_appManager->m_mutex); - for (const auto &info: infos) { - DataEntity app; - app.setTop(info.top); - app.setLock(info.lock == 1); - app.setFavorite(info.favorite); - app.setLaunchTimes(info.launchTimes); - app.setId(info.desktopPath); - app.setIcon(APP_ICON_PREFIX + info.iconName); - app.setName(info.appLocalName); - app.setCategory(info.category); - app.setFirstLetter(info.firstLetter); + QList apps; + appendApps(infos, apps); - m_appManager->m_normalApps.append(app); - } + Q_EMIT appAdded(apps); +} + +void AppDataWorker::appendApps(const QVector &infos, QList &apps) +{ + QMutexLocker locker(&m_appManager->m_mutex); + for (const auto &info : infos) { + DataEntity app; + app.setTop(info.top); + app.setLock(info.lock == 1); + app.setFavorite(info.favorite); + app.setLaunchTimes(info.launchTimes); + app.setId(info.desktopPath); + app.setIcon(APP_ICON_PREFIX + info.iconName); + app.setName(info.appLocalName); + app.setCategory(info.category); + app.setFirstLetter(info.firstLetter); + + m_appManager->m_normalApps.append(app); + apps.append(app); } - - Q_EMIT appDataChanged(); } void AppDataWorker::onAppUpdated(QVector infos) @@ -161,6 +165,16 @@ void AppDataWorker::onAppUpdated(QVector infos) return; } + QList apps; + updateApps(infos, apps); + Q_EMIT appUpdated(apps); + + updateFavoriteApps(); +} + +void AppDataWorker::updateApps(QVector &infos, QList &apps) +{ + QMutexLocker locker(&m_appManager->m_mutex); for (auto &app : m_appManager->m_normalApps) { if (infos.isEmpty()) { break; @@ -168,8 +182,6 @@ void AppDataWorker::onAppUpdated(QVector infos) for (int i = 0; i < infos.size(); ++i) { const UkuiSearch::AppInfoResult &info = infos.at(i); if (info.desktopPath == app.id()) { - QMutexLocker locker(&m_appManager->m_mutex); - app.setTop(info.top); app.setLock(info.lock == 1); app.setFavorite(info.favorite); @@ -179,14 +191,12 @@ void AppDataWorker::onAppUpdated(QVector infos) app.setCategory(info.category); app.setFirstLetter(info.firstLetter); + apps.append(app); infos.remove(i); break; } } } - - Q_EMIT appDataChanged(); - updateFavoriteApps(); } void AppDataWorker::onAppDeleted(QStringList infos) @@ -194,28 +204,29 @@ void AppDataWorker::onAppDeleted(QStringList infos) if (infos.isEmpty()) { return; } + QStringList removedIdList; + removeApps(infos, removedIdList); + Q_EMIT appDeleted(removedIdList); - QVector::iterator appIterator = m_appManager->m_normalApps.begin(); - for (; appIterator != m_appManager->m_normalApps.end(); ++appIterator) { - if (infos.isEmpty()) { - break; - } - - QStringList::iterator infoIterator = infos.begin(); - for (; infoIterator != infos.end(); ++infoIterator) { - if ((*appIterator).id() == *infoIterator) { - QMutexLocker locker(&m_appManager->m_mutex); - m_appManager->m_normalApps.erase(appIterator); - infos.erase(infoIterator); - break; - } - } - } - - Q_EMIT appDataChanged(); updateFavoriteApps(); } +void AppDataWorker::removeApps(QStringList &appIdList, QStringList &removedIdList) +{ + QMutexLocker locker(&m_appManager->m_mutex); + QList &normalApps = m_appManager->m_normalApps; + QList::iterator iterator = normalApps.begin(); + + while (iterator != normalApps.end() && !appIdList.isEmpty()) { + if (appIdList.removeOne((*iterator).id())) { + removedIdList.append((*iterator).id()); + iterator = normalApps.erase(iterator); + continue; + } + ++iterator; + } +} + // ===== AppDataManager ===== // AppDataManager *AppDataManager::instance() { @@ -229,7 +240,9 @@ AppDataManager::AppDataManager() appDataWorker->moveToThread(&m_workerThread); connect(&m_workerThread, &QThread::finished, appDataWorker, &QObject::deleteLater); - connect(appDataWorker, &AppDataWorker::appDataChanged, this, &AppDataManager::appDataChanged); + connect(appDataWorker, &AppDataWorker::appAdded, this, &AppDataManager::appAdded); + connect(appDataWorker, &AppDataWorker::appDeleted, this, &AppDataManager::appDeleted); + connect(appDataWorker, &AppDataWorker::appUpdated, this, &AppDataManager::appUpdated); connect(appDataWorker, &AppDataWorker::favoriteAppChanged, this, &AppDataManager::favoriteAppChanged); m_workerThread.start(); @@ -241,7 +254,7 @@ AppDataManager::~AppDataManager() m_workerThread.wait(); } -QVector AppDataManager::normalApps() +QList AppDataManager::normalApps() { QMutexLocker locker(&m_mutex); return m_normalApps; diff --git a/src/appdata/app-data-manager.h b/src/appdata/app-data-manager.h index 4d01933..bc2f2de 100644 --- a/src/appdata/app-data-manager.h +++ b/src/appdata/app-data-manager.h @@ -24,6 +24,7 @@ #include #include #include +#include #include "commons.h" @@ -43,10 +44,13 @@ public: AppDataManager(AppDataManager&& obj) = delete; AppDataManager &operator=(AppDataManager&& obj) = delete; - QVector normalApps(); + QList normalApps(); QVector favoriteApps(); Q_SIGNALS: + void appAdded(QList apps); + void appUpdated(QList apps); + void appDeleted(QStringList idList); void appDataChanged(); void favoriteAppChanged(); @@ -56,7 +60,7 @@ private: private: QMutex m_mutex; QThread m_workerThread; - QVector m_normalApps; + QList m_normalApps; QVector m_favoriteApps; }; diff --git a/src/appdata/app-folder-helper.cpp b/src/appdata/app-folder-helper.cpp index 276203e..80d73a1 100644 --- a/src/appdata/app-folder-helper.cpp +++ b/src/appdata/app-folder-helper.cpp @@ -69,9 +69,9 @@ void AppFolderHelper::insertFolder(const Folder &folder) m_folders.append(folder); } -void AppFolderHelper::addAppToFolder(const QString& appName, const QString& folderName) +void AppFolderHelper::addAppToFolder(const QString& appId, const QString& folderName) { - if (appName.isEmpty() || folderName.isEmpty()) { + if (appId.isEmpty() || folderName.isEmpty()) { return; } @@ -79,8 +79,8 @@ void AppFolderHelper::addAppToFolder(const QString& appName, const QString& fold QMutexLocker locker(&m_mutex); Folder *folder = findFolder(folderName); if (folder) { - if (!folder->apps.contains(appName)) { - folder->apps.append(appName); + if (!folder->apps.contains(appId)) { + folder->apps.append(appId); } return; } @@ -89,7 +89,7 @@ void AppFolderHelper::addAppToFolder(const QString& appName, const QString& fold // 新文件夹放最后 Folder folder; folder.name = folderName; - folder.apps.append(appName); + folder.apps.append(appId); if (m_folders.isEmpty()) { folder.index = 1; } else { @@ -99,9 +99,9 @@ void AppFolderHelper::addAppToFolder(const QString& appName, const QString& fold insertFolder(folder); } -void AppFolderHelper::removeAppFromFolder(const QString &appName, const QString &folderName) +void AppFolderHelper::removeAppFromFolder(const QString &appId, const QString &folderName) { - if (appName.isEmpty() || folderName.isEmpty()) { + if (appId.isEmpty() || folderName.isEmpty()) { return; } @@ -111,8 +111,8 @@ void AppFolderHelper::removeAppFromFolder(const QString &appName, const QString return; } - if (folder->apps.contains(appName)) { - folder->apps.removeOne(appName); + if (folder->apps.contains(appId)) { + folder->apps.removeOne(appId); } } @@ -140,16 +140,42 @@ QVector AppFolderHelper::folderData() return m_folders; } -const Folder *AppFolderHelper::searchFolder(const QString &folderName) +bool AppFolderHelper::searchFolder(const QString& folderName, Folder &folder) { + if (folderName.isEmpty()) { + return false; + } + QMutexLocker locker(&m_mutex); - return findFolder(folderName); + Folder *tmp = findFolder(folderName); + if (!tmp) { + return false; + } + + folder.name = tmp->name; + folder.index = tmp->index; + folder.apps = tmp->apps; + + return true; } -const Folder *AppFolderHelper::searchFolderByAppName(const QString &appName) +bool AppFolderHelper::searchFolderByAppName(const QString &appId, Folder &folder) { + if (appId.isEmpty()) { + return false; + } + QMutexLocker locker(&m_mutex); - return findFolderByAppName(appName); + Folder *tmp = findFolderByAppName(appId); + if (!tmp) { + return false; + } + + folder.name = tmp->name; + folder.index = tmp->index; + folder.apps = tmp->apps; + + return true; } Folder *AppFolderHelper::findFolder(const QString &folderName) @@ -171,7 +197,7 @@ Folder *AppFolderHelper::findFolder(const QString &folderName) return nullptr; } -Folder *AppFolderHelper::findFolderByAppName(const QString &appName) +Folder *AppFolderHelper::findFolderByAppName(const QString &appId) { if (m_folders.empty()) { return nullptr; @@ -179,7 +205,7 @@ Folder *AppFolderHelper::findFolderByAppName(const QString &appName) QVector::iterator iterator = m_folders.begin(); for (; iterator != m_folders.end(); ++iterator) { - if (iterator->apps.contains(appName)) { + if (iterator->apps.contains(appId)) { return iterator; } } diff --git a/src/appdata/app-folder-helper.h b/src/appdata/app-folder-helper.h index 775fd74..57165d4 100644 --- a/src/appdata/app-folder-helper.h +++ b/src/appdata/app-folder-helper.h @@ -31,13 +31,13 @@ namespace UkuiMenu { class Folder { Q_GADGET - Q_PROPERTY(QString index READ getIndex) + Q_PROPERTY(int index READ getIndex) Q_PROPERTY(QString name READ getName) Q_PROPERTY(QStringList apps READ getApps) public: - int getIndex() { return index; } - QString getName() { return name; } - QStringList getApps() { return apps; } + int getIndex() const { return index; } + QString getName() const { return name; } + QStringList getApps() const { return apps; } public: int index; @@ -59,12 +59,12 @@ public: // TODO 修改文件夹信息 // xxxx - const Folder *searchFolder(const QString& folderName); - const Folder *searchFolderByAppName(const QString& appName); + bool searchFolder(const QString& folderName, Folder &folder); + bool searchFolderByAppName(const QString& appId, Folder &folder); QVector folderData(); - void addAppToFolder(const QString& appName, const QString& folderName); - void removeAppFromFolder(const QString& appName, const QString& folderName); + void addAppToFolder(const QString& appId, const QString& folderName); + void removeAppFromFolder(const QString& appId, const QString& folderName); bool deleteFolder(const QString& folderName); void forceSync(); @@ -78,7 +78,7 @@ private: void saveData(); void insertFolder(const Folder& folder); Folder *findFolder(const QString& folderName); - Folder *findFolderByAppName(const QString& appName); + Folder *findFolderByAppName(const QString& appId); // TODO 配置文件监听 private: diff --git a/src/appdata/data-provider-plugin-iface.h b/src/appdata/data-provider-plugin-iface.h index 1bd54bd..87727e6 100644 --- a/src/appdata/data-provider-plugin-iface.h +++ b/src/appdata/data-provider-plugin-iface.h @@ -44,6 +44,7 @@ class DataProviderPluginIFace : public QObject { Q_OBJECT public: + explicit DataProviderPluginIFace() : QObject(nullptr) {} virtual int index() = 0; virtual QString name() = 0; virtual QString icon() = 0;