diff --git a/src/extension/favorite/app-favorite-model.cpp b/src/extension/favorite/app-favorite-model.cpp index ef3ddb8..1df03b2 100644 --- a/src/extension/favorite/app-favorite-model.cpp +++ b/src/extension/favorite/app-favorite-model.cpp @@ -219,9 +219,15 @@ void AppFavoritesModel::clearFavorites() FavoritesConfig::instance().clear(); } -bool AppFavoritesModel::getApp(const QString &appid, DataEntity &app) +bool AppFavoritesModel::isAppIncluded(const QString &appid) { - return m_sourceModel->databaseInterface()->getApp(appid, app); + QModelIndex sourceIndex = m_sourceModel->index(m_sourceModel->indexOfApp(appid), 0, {}); + if (!sourceIndex.isValid()) { + return false; + } + + QPersistentModelIndex index(sourceIndex); + return m_favoritesApps.contains(index); } void AppFavoritesModel::onAppRemoved(const QModelIndex &parent, int first, int last) diff --git a/src/extension/favorite/app-favorite-model.h b/src/extension/favorite/app-favorite-model.h index f5be780..a9ab31e 100644 --- a/src/extension/favorite/app-favorite-model.h +++ b/src/extension/favorite/app-favorite-model.h @@ -42,7 +42,7 @@ public: void addAppToFavorites(const QString &id, const int &index); void removeAppFromFavorites(const QString &id); void changeFileState(const QString &url, const bool &favorite); - bool getApp(const QString &appid, DataEntity &app); + bool isAppIncluded(const QString &appid); private: explicit AppFavoritesModel(QObject *parent = nullptr); diff --git a/src/extension/favorite/favorites-config.cpp b/src/extension/favorite/favorites-config.cpp index 0403a94..9e419ca 100644 --- a/src/extension/favorite/favorites-config.cpp +++ b/src/extension/favorite/favorites-config.cpp @@ -22,6 +22,7 @@ #include #include #include "favorites-config.h" +#include "favorite-folder-helper.h" #define FOLDER_FILE_PATH ".config/ukui-menu/" #define FOLDER_FILE_NAME "favorite.json" @@ -138,19 +139,24 @@ void FavoritesConfig::initConfig() m_favoritesList.clear(); QJsonArray array = jsonDocument.array(); - QJsonArray newArray; + bool needSync = false; for (int i = 0; i < array.size(); i++) { if (array.at(i).isString()) { - m_favoritesList.append(array.at(i).toString()); - newArray.append(array.at(i)); + QString id = array.at(i).toString(); + // 配置文件出现错误, 某一个应用 同时出现在了 应用组的内部和外部 + if (id.startsWith(APP_ID_SCHEME) && FavoriteFolderHelper::instance()->containApp(id.mid(APP_ID_SCHEME.length()))) { + needSync = true; + continue; + } + m_favoritesList.append(id); + } else { + needSync = true; } } - file.open(QFile::WriteOnly); - jsonDocument.setArray(newArray); - file.write(jsonDocument.toJson()); - file.flush(); - file.close(); + if (needSync) { + sync(); + } } } // UkuiMenu diff --git a/src/extension/favorite/favorites-model.cpp b/src/extension/favorite/favorites-model.cpp index 5cba398..cf890a4 100644 --- a/src/extension/favorite/favorites-model.cpp +++ b/src/extension/favorite/favorites-model.cpp @@ -80,6 +80,11 @@ void FavoritesModel::openMenu(const int &row) void FavoritesModel::addAppToFavorites(const QString &id, const int &index) { + if (AppFavoritesModel::instance().isAppIncluded(id) || FavoriteFolderHelper::instance()->containApp(id)) { + qWarning() << "This application is already included in the favorite apps!"; + return; + } + int appOrder; if (index == -1) { appOrder = FavoritesConfig::instance().configSize(); @@ -108,8 +113,7 @@ void FavoritesModel::addAppsToNewFolder(const QString &idFrom, const QString &id return; } - DataEntity app; - if (AppFavoritesModel::instance().getApp(idFrom, app) && AppFavoritesModel::instance().getApp(idTo, app)) { + if (AppFavoritesModel::instance().isAppIncluded(idFrom) && AppFavoritesModel::instance().isAppIncluded(idTo)) { FavoriteFolderHelper::instance()->addAppsToNewFolder(idFrom, idTo, ""); } }