From 379b07fc7ac39fc8ebb5a078f05617bbd3672a03 Mon Sep 17 00:00:00 2001 From: qiqi49 Date: Wed, 24 Jan 2024 11:31:57 +0800 Subject: [PATCH] =?UTF-8?q?fix(extension):=E5=AE=8C=E5=96=84=E6=94=B6?= =?UTF-8?q?=E8=97=8F=E6=8F=92=E4=BB=B6=E7=BB=86=E8=8A=82=EF=BC=88=E8=A7=A3?= =?UTF-8?q?=E6=95=A3=E5=BA=94=E7=94=A8=E7=BB=84=E5=88=B0=E6=8C=87=E5=AE=9A?= =?UTF-8?q?=E4=BD=8D=E7=BD=AE=EF=BC=89=EF=BC=8C=E5=85=BC=E5=AE=B9=E5=8E=9F?= =?UTF-8?q?=E6=9C=89=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 11 +- qml/AppUI/Folder.qml | 4 +- qml/extensions/FavoriteExtension.qml | 1 + src/extension/favorite/app-favorite-model.cpp | 64 +++++++---- src/extension/favorite/app-favorite-model.h | 7 +- .../favorite/favorite-folder-helper.cpp | 81 ++++++++----- .../favorite/favorite-folder-helper.h | 3 +- src/extension/favorite/favorite-widget.cpp | 2 + src/extension/favorite/favorite-widget.h | 2 + src/extension/favorite/favorites-config.cpp | 108 +++++++----------- src/extension/favorite/favorites-config.h | 15 ++- src/extension/favorite/favorites-model.cpp | 9 +- src/extension/favorite/favorites-model.h | 4 + .../favorite}/folder-model.cpp | 4 +- .../favorite}/folder-model.h | 0 src/model/model-manager.cpp | 2 +- 16 files changed, 178 insertions(+), 139 deletions(-) rename src/{model => extension/favorite}/folder-model.cpp (96%) rename src/{model => extension/favorite}/folder-model.h (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3e37549..52c6b05 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -110,13 +110,14 @@ set(SOURCE_FILES src/extension/widget-extension-model.cpp src/extension/widget-extension-model.h src/extension/widget-model.cpp src/extension/widget-model.h src/extension/menu/app-menu-plugin.cpp src/extension/menu/app-menu-plugin.h - src/extension/favorite/favorite-extension-plugin.cpp src/extension/favorite/favorite-extension-plugin.h - src/extension/favorite/favorite-context-menu.cpp src/extension/favorite/favorite-context-menu.h - src/extension/favorite/favorite-widget.cpp src/extension/favorite/favorite-widget.h - src/extension/favorite/app-favorite-model.cpp src/extension/favorite/app-favorite-model.h - src/extension/favorite/favorites-config.cpp src/extension/favorite/favorites-config.h + src/extension/favorite/folder-model.cpp src/extension/favorite/folder-model.h src/extension/favorite/favorites-model.cpp src/extension/favorite/favorites-model.h + src/extension/favorite/favorite-widget.cpp src/extension/favorite/favorite-widget.h + src/extension/favorite/favorites-config.cpp src/extension/favorite/favorites-config.h + src/extension/favorite/app-favorite-model.cpp src/extension/favorite/app-favorite-model.h + src/extension/favorite/favorite-context-menu.cpp src/extension/favorite/favorite-context-menu.h src/extension/favorite/favorite-folder-helper.cpp src/extension/favorite/favorite-folder-helper.h + src/extension/favorite/favorite-extension-plugin.cpp src/extension/favorite/favorite-extension-plugin.h src/libappdata/basic-app-model.cpp src/libappdata/basic-app-model.h src/libappdata/app-database-interface.cpp src/libappdata/app-database-interface.h src/libappdata/app-category-model.cpp src/libappdata/app-category-model.h diff --git a/qml/AppUI/Folder.qml b/qml/AppUI/Folder.qml index 01affd4..d682bb3 100644 --- a/qml/AppUI/Folder.qml +++ b/qml/AppUI/Folder.qml @@ -28,7 +28,7 @@ import org.ukui.quick.items 1.0 as UkuiItems Loader { id: root active: false - property var folderModel: modelManager.getFolderModel() + property var folderModel property string folderName: "" property int folderX: 0 property int folderY: 0 @@ -42,7 +42,7 @@ Loader { signal turnPageFinished() function initFolder(id, name, x, y) { - folderModel.setFolderId(id); + extensionData.folderModel.setFolderId(id); folderName = name; folderX = x; folderY = y; diff --git a/qml/extensions/FavoriteExtension.qml b/qml/extensions/FavoriteExtension.qml index 6cd6b09..c0acd96 100644 --- a/qml/extensions/FavoriteExtension.qml +++ b/qml/extensions/FavoriteExtension.qml @@ -53,6 +53,7 @@ UkuiMenuExtension { anchors.fill: parent isFullScreen: false + folderModel: extensionData.folderModel Component.onCompleted: favoriteView.openFolderSignal.connect(initFolder) Component.onDestruction: favoriteView.openFolderSignal.disconnect(initFolder) } diff --git a/src/extension/favorite/app-favorite-model.cpp b/src/extension/favorite/app-favorite-model.cpp index bc28c06..02437fd 100644 --- a/src/extension/favorite/app-favorite-model.cpp +++ b/src/extension/favorite/app-favorite-model.cpp @@ -120,6 +120,7 @@ void AppFavoritesModel::updateFavoritesApps(const DataEntity &app, const QModelI if (app.favorite() > 0 && !FavoriteFolderHelper::instance()->containApp(app.id())) { QPersistentModelIndex index(sourceIndex); addFavoriteApp(index); + FavoritesConfig::instance().insertValue(APP_ID_SCHEME + index.data(DataEntity::Id).toString()); } else if (app.favorite() == 0) { if (FavoriteFolderHelper::instance()->containApp(app.id())) { @@ -185,30 +186,23 @@ QVariant AppFavoritesModel::fileData(const QModelIndex &index, int role) const return {}; } -QStringList AppFavoritesModel::favoritesApps() const +void AppFavoritesModel::clearFavorites() { - QMap apps; - for (const auto &index : m_favoritesApps) { - QString id = m_sourceModel->data(index, DataEntity::Id).toString(); - int favorite = m_sourceModel->data(index, DataEntity::Favorite).toInt(); - apps.insert(favorite, id); + for(const auto &appIndex : m_favoritesApps) { + removeAppFromFavorites(appIndex.data(DataEntity::Id).toString()); } - return apps.values(); -} - -void AppFavoritesModel::changeAppState(const QString &id, const int &index) -{ - if (id.isEmpty()) { - return; + for (const auto &id : FavoriteFolderHelper::instance()->appsInFolders()) { + removeAppFromFavorites(id); } - m_sourceModel->databaseInterface()->fixAppToFavorite(id, index); + beginRemoveRows(QModelIndex(), 0, rowCount()); + m_favoritesApps.clear(); + m_folders.clear(); + m_favoritesFiles.clear(); + endRemoveRows(); - // 同步配置文件,更新应用顺序 - if (index > 0) { - FavoritesConfig::instance().insertValue(APP_ID_SCHEME + id, index); - } + FavoritesConfig::instance().clear(); } bool AppFavoritesModel::getApp(const QString &appid, DataEntity &app) @@ -243,8 +237,6 @@ void AppFavoritesModel::addFavoriteApp(const QPersistentModelIndex &modelIndex) beginInsertRows(QModelIndex(), m_favoritesApps.count(), m_favoritesApps.count()); m_favoritesApps.append(modelIndex); endInsertRows(); - - FavoritesConfig::instance().insertValue(APP_ID_SCHEME + modelIndex.data(DataEntity::Id).toString()); } void AppFavoritesModel::removeFavoriteApp(const QPersistentModelIndex &modelIndex) @@ -284,9 +276,11 @@ void AppFavoritesModel::onFolderDeleted(const int &folderId, const QStringList & m_folders.removeOne(folderId); endRemoveRows(); - for (auto app : apps) { - QPersistentModelIndex modelIndex(m_sourceModel->index(m_sourceModel->indexOfApp(app))); + int index = FavoritesConfig::instance().getOrderById(FOLDER_ID_SCHEME +QString::number(folderId)); + for (int i = 0; i < apps.count(); i++) { + QPersistentModelIndex modelIndex(m_sourceModel->index(m_sourceModel->indexOfApp(apps.at(i)))); addFavoriteApp(modelIndex); + FavoritesConfig::instance().insertValue(APP_ID_SCHEME + modelIndex.data(DataEntity::Id).toString(), index + i); } FavoritesConfig::instance().removeValueById(FOLDER_ID_SCHEME + QString::number(folderId)); @@ -310,6 +304,7 @@ void AppFavoritesModel::onFolderChanged(const int &folderId, const QString &appI } else { QPersistentModelIndex modelIndex(m_sourceModel->index(m_sourceModel->indexOfApp(appId))); addFavoriteApp(modelIndex); + FavoritesConfig::instance().insertValue(APP_ID_SCHEME + modelIndex.data(DataEntity::Id).toString()); } roles.append(DataEntity::Icon); @@ -344,9 +339,34 @@ void AppFavoritesModel::changeFileState(const QString &url, const bool &favorite } if (favorite) { + m_favoritesFiles.append(url); FavoritesConfig::instance().insertValue(fileId); } else { + m_favoritesFiles.removeAll(url); FavoritesConfig::instance().removeValueById(fileId); } } + +void AppFavoritesModel::addAppToFavorites(const QString &id, const int &index) +{ + if (id.isEmpty()) { + return; + } + + m_sourceModel->databaseInterface()->fixAppToFavorite(id, 1); + + // 同步配置文件,更新应用顺序 + if (index > 0) { + FavoritesConfig::instance().insertValue(APP_ID_SCHEME + id, index); + } +} + +void AppFavoritesModel::removeAppFromFavorites(const QString &id) +{ + if (id.isEmpty()) { + return; + } + + m_sourceModel->databaseInterface()->fixAppToFavorite(id, 0); +} } // UkuiMenu diff --git a/src/extension/favorite/app-favorite-model.h b/src/extension/favorite/app-favorite-model.h index f9e03ad..f5be780 100644 --- a/src/extension/favorite/app-favorite-model.h +++ b/src/extension/favorite/app-favorite-model.h @@ -38,8 +38,9 @@ public: int rowCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role) const override; - QStringList favoritesApps() const; - void changeAppState(const QString &id, const int &index); + void clearFavorites(); + 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); @@ -67,7 +68,7 @@ private: private: /** - *不在应用组的收藏应用在baseModel的对应index + *不在应用组 的收藏应用在baseModel的对应index */ QVector m_favoritesApps; /** diff --git a/src/extension/favorite/favorite-folder-helper.cpp b/src/extension/favorite/favorite-folder-helper.cpp index c3a2ad7..4feff87 100644 --- a/src/extension/favorite/favorite-folder-helper.cpp +++ b/src/extension/favorite/favorite-folder-helper.cpp @@ -15,13 +15,6 @@ * along with this program. If not, see . * */ - -#include "favorite-folder-helper.h" -#include "app-data-manager.h" -#include "event-track.h" -#include "favorites-config.h" -#include "basic-app-model.h" - #include #include #include @@ -30,13 +23,21 @@ #include #include +#include "favorite-folder-helper.h" +#include "event-track.h" +#include "favorites-config.h" +#include "libappdata/basic-app-model.h" + #define FOLDER_FILE_PATH ".config/ukui-menu/" -#define FOLDER_FILE_NAME "folder0.json" +#define FOLDER_FILE_NAME "folder.json" namespace UkuiMenu { QString FavoriteFolderHelper::s_folderConfigFile = QDir::homePath() + "/" + FOLDER_FILE_PATH + FOLDER_FILE_NAME; - +/** + * changelog 1.0.0 添加版本号,区分旧文件内容 + */ +static const QString FOLDER_CONFIG_VERSION = QStringLiteral("1.0.0"); FavoriteFolderHelper *FavoriteFolderHelper::instance() { static FavoriteFolderHelper FavoriteFolderHelper; @@ -263,12 +264,15 @@ void FavoriteFolderHelper::readData() } // 读取json数据 - QByteArray byteArray = file.readAll(); + QTextStream stream(&file); + stream.setCodec("UTF-8"); + QString str = stream.readAll(); file.close(); - QJsonDocument jsonDocument(QJsonDocument::fromJson(byteArray)); - if (jsonDocument.isNull() || jsonDocument.isEmpty() || !jsonDocument.isArray()) { - qWarning() << "FavoriteFolderHelper: Incorrect configuration files are ignored."; + QJsonParseError jsonError; + QJsonDocument jsonDocument(QJsonDocument::fromJson(str.toUtf8(),&jsonError)); + if (jsonDocument.isNull() || jsonDocument.isEmpty()) { + qWarning() << "FavoriteFolderHelper: Incorrect configuration files are ignored." << jsonError.error; return; } @@ -277,24 +281,32 @@ void FavoriteFolderHelper::readData() m_folders.clear(); } - // 遍历json数据节点 - QJsonArray jsonArray = jsonDocument.array(); - for (const auto &value : jsonArray) { - QJsonObject object = value.toObject(); - if (object.contains("name") && object.contains("id") && object.contains("apps")) { - FavoritesFolder folder; - folder.name = object.value(QLatin1String("name")).toString(); - folder.id = object.value(QLatin1String("id")).toInt(); + QJsonObject fileObject = jsonDocument.object(); + if (fileObject.value("version").toString() == FOLDER_CONFIG_VERSION) { + if (fileObject.contains("folders")) { + QJsonArray jsonArray = fileObject.value(QLatin1String("folders")).toArray(); + for (const auto &value : jsonArray) { + QJsonObject object = value.toObject(); + if (object.contains("name") && object.contains("id") && object.contains("apps")) { + FavoritesFolder folder; + folder.name = object.value(QLatin1String("name")).toString(); + folder.id = object.value(QLatin1String("id")).toInt(); - QJsonArray apps = object.value(QLatin1String("apps")).toArray(); - for (const auto &app : apps) { - folder.apps.append(app.toString()); - } + QJsonArray apps = object.value(QLatin1String("apps")).toArray(); + for (const auto &app : apps) { + folder.apps.append(app.toString()); + } - if (!folder.apps.isEmpty()) { - insertFolder(folder); + if (!folder.apps.isEmpty()) { + insertFolder(folder); + } + } } } + + } else { + saveData(); + return; } } @@ -306,6 +318,7 @@ void FavoriteFolderHelper::saveData() } QJsonDocument jsonDocument; + QJsonObject fileObject; QJsonArray folderArray; { @@ -326,7 +339,9 @@ void FavoriteFolderHelper::saveData() } } - jsonDocument.setArray(folderArray); + fileObject.insert("version", FOLDER_CONFIG_VERSION); + fileObject.insert("folders", folderArray); + jsonDocument.setObject(fileObject); if (file.write(jsonDocument.toJson()) == -1) { qWarning() << "Error saving configuration file."; @@ -350,4 +365,14 @@ QStringList FavoriteFolderHelper::folderIcon(const FavoritesFolder &folder) return icons; } + +QStringList FavoriteFolderHelper::appsInFolders() +{ + QStringList apps; + for (const auto &folder : m_folders) { + apps.append(folder.apps); + } + + return apps; +} } // UkuiMenu diff --git a/src/extension/favorite/favorite-folder-helper.h b/src/extension/favorite/favorite-folder-helper.h index 24a632d..89a5a65 100644 --- a/src/extension/favorite/favorite-folder-helper.h +++ b/src/extension/favorite/favorite-folder-helper.h @@ -61,6 +61,7 @@ public: bool getFolderFromId(const int& folderId, FavoritesFolder& folder); bool containApp(const QString& appId); + bool deleteFolder(const int& folderId); QList folderData(); @@ -68,10 +69,10 @@ public: void addAppToNewFolder(const QString& appId, const QString& folderName); void addAppsToNewFolder(const QString& idFrom, const QString& idTo, const QString& folderName); void removeAppFromFolder(const QString& appId); - bool deleteFolder(const int& folderId); void renameFolder(const int& folderId, const QString& folderName); void forceSync(); + QStringList appsInFolders(); Q_SIGNALS: void folderAdded(int folderId, const int& order); diff --git a/src/extension/favorite/favorite-widget.cpp b/src/extension/favorite/favorite-widget.cpp index 19bd7a6..ceeab8e 100644 --- a/src/extension/favorite/favorite-widget.cpp +++ b/src/extension/favorite/favorite-widget.cpp @@ -36,7 +36,9 @@ FavoriteWidget::FavoriteWidget(QObject *parent) : WidgetExtension(parent) FavoritesModel::instance().setSourceModel(&AppFavoritesModel::instance()); FavoritesModel::instance().sort(0); + m_folderModel = new FolderModel; m_data.insert("favoriteAppsModel", QVariant::fromValue(&FavoritesModel::instance())); + m_data.insert("folderModel", QVariant::fromValue(m_folderModel)); } int FavoriteWidget::index() const diff --git a/src/extension/favorite/favorite-widget.h b/src/extension/favorite/favorite-widget.h index 17d8497..ba24214 100644 --- a/src/extension/favorite/favorite-widget.h +++ b/src/extension/favorite/favorite-widget.h @@ -20,6 +20,7 @@ #define UKUI_MENU_FAVORITE_WIDGET_H #include "../widget-extension.h" +#include "folder-model.h" namespace UkuiMenu { @@ -37,6 +38,7 @@ public: private: MetadataMap m_metadata; QVariantMap m_data; + FolderModel *m_folderModel; }; } // UkuiMenu diff --git a/src/extension/favorite/favorites-config.cpp b/src/extension/favorite/favorites-config.cpp index ee4a068..efcb5d0 100644 --- a/src/extension/favorite/favorites-config.cpp +++ b/src/extension/favorite/favorites-config.cpp @@ -38,64 +38,62 @@ FavoritesConfig &FavoritesConfig::instance() FavoritesConfig::FavoritesConfig(QObject *parent) { - + initConfig(); } -QJsonObject FavoritesConfig::getValue(const int &index) const +QString FavoritesConfig::getValue(const int &index) const { - if (index < 0 || index > arrayFromConfigFile().count()) { + if (index < 0 || index > m_favoritesList.count()) { return {}; } - return arrayFromConfigFile().at(index).toObject(); + return m_favoritesList.at(index); } void FavoritesConfig::insertValue(const QString &id, const int &index) { - QJsonArray array = arrayFromConfigFile(); - if (indexOfObject(id, array) > -1) { + if (m_favoritesList.indexOf(id) > -1) { return; } - QJsonObject object; - object.insert("id", id); - array.insert(index, object); - - setArrayToConfigFile(array); + m_favoritesList.insert(index, id); + sync(); } void FavoritesConfig::removeValueById(const QString &id) { - QJsonArray array = arrayFromConfigFile(); - int index = indexOfObject(id, array); + int index = m_favoritesList.indexOf(id); - if (index > -1 && index < array.size()) { - array.removeAt(index); + if (index > -1 && index < m_favoritesList.count()) { + m_favoritesList.removeAt(index); } - setArrayToConfigFile(array); + sync(); } -void FavoritesConfig::initConfig(const QStringList &apps) +int FavoritesConfig::getOrderById(const QString &id) { - QJsonArray favoritesArray; - - for (const auto &appId : apps) { - QJsonObject object; - object.insert("id", appId); - favoritesArray.append(object); - } - - setArrayToConfigFile(favoritesArray); + return m_favoritesList.indexOf(id); } -void FavoritesConfig::setArrayToConfigFile(const QJsonArray &array) +void FavoritesConfig::changeOrder(const int &indexFrom, const int &indexTo) +{ + m_favoritesList.move(indexFrom, indexTo); + sync(); +} + +int FavoritesConfig::configSize() const +{ + return m_favoritesList.size(); +} + +void FavoritesConfig::sync() { QDir dir; QString folderConfigDir(QDir::homePath() + "/" + FOLDER_FILE_PATH); if (!dir.exists(folderConfigDir)) { if (!dir.mkdir(folderConfigDir)) { - qWarning() << "Unable to create profile favorites."; + qWarning() << "FavoritesConfig::Unable to create configuration file."; return; } } @@ -104,10 +102,10 @@ void FavoritesConfig::setArrayToConfigFile(const QJsonArray &array) file.open(QFile::WriteOnly); QJsonDocument jsonDocument; - jsonDocument.setArray(array); + jsonDocument.setArray(QJsonArray::fromStringList(m_favoritesList)); if (file.write(jsonDocument.toJson()) == -1) { - qWarning() << "Error saving configuration file."; + qWarning() << "FavoritesConfig::Error saving configuration file."; } file.flush(); @@ -116,57 +114,35 @@ void FavoritesConfig::setArrayToConfigFile(const QJsonArray &array) Q_EMIT configChanged(); } -QJsonArray FavoritesConfig::arrayFromConfigFile() const +void FavoritesConfig::clear() +{ + m_favoritesList.clear(); + sync(); +} + +void FavoritesConfig::initConfig() { QFile file(s_favoritesConfigFile); if (!file.open(QFile::ReadOnly)) { - return {}; + qWarning() << "FavoritesConfig: Incorrect configuration files open failed."; + return; } QJsonDocument jsonDocument(QJsonDocument::fromJson(file.readAll())); file.close(); if (jsonDocument.isNull() || jsonDocument.isEmpty() || !jsonDocument.isArray()) { - qWarning() << "AppFolderHelper: Incorrect configuration files are ignored."; - return {}; - } - - return jsonDocument.array(); -} - -int FavoritesConfig::indexOfObject(const QString &id, const QJsonArray &array) -{ - if (id.isEmpty()) { - qWarning() << "id is unavailable!"; - return -1; + qWarning() << "FavoritesConfig: Incorrect configuration files are ignored."; + return; } + m_favoritesList.clear(); + QJsonArray array = jsonDocument.array(); for (int i = 0; i < array.size(); i++) { if (array.at(i).toObject().contains("id")) { - if (array.at(i).toObject().value("id").toString() == id) { - return i; - } + m_favoritesList.append(array.at(i).toString()); } } - - return -1; -} - -int FavoritesConfig::getOrderById(const QString &id) -{ - return indexOfObject(id, arrayFromConfigFile()); -} - -void FavoritesConfig::changeOrder(const int &indexFrom, const int &indexTo) -{ - QVariantList list = arrayFromConfigFile().toVariantList(); - list.move(indexFrom, indexTo); - setArrayToConfigFile(QJsonArray::fromVariantList(list)); -} - -int FavoritesConfig::configSize() const -{ - return arrayFromConfigFile().size(); } } // UkuiMenu diff --git a/src/extension/favorite/favorites-config.h b/src/extension/favorite/favorites-config.h index 578a389..847e157 100644 --- a/src/extension/favorite/favorites-config.h +++ b/src/extension/favorite/favorites-config.h @@ -34,33 +34,32 @@ class FavoritesConfig : public QObject Q_OBJECT public: static FavoritesConfig &instance(); - - QJsonObject getValue(const int &index) const; + void changeOrder(const int &indexFrom, const int &indexTo); /* * 在配置文件中对应位置添加id * 对应类型: 应用组(folder://)、应用(app://)、文件(file://) */ void insertValue(const QString &id, const int &index = 0); void removeValueById(const QString &id); + void clear(); + int configSize() const; /* * 在配置文件中获取对应id的位置 */ int getOrderById(const QString &id); - void changeOrder(const int &indexFrom, const int &indexTo); - int configSize() const; - void initConfig(const QStringList &apps); + QString getValue(const int &index) const; Q_SIGNALS: void configChanged(); private: explicit FavoritesConfig(QObject *parent = nullptr); - void setArrayToConfigFile(const QJsonArray &array); - QJsonArray arrayFromConfigFile() const; - int indexOfObject(const QString &id, const QJsonArray &array); + void initConfig(); + void sync(); private: static QString s_favoritesConfigFile; + QStringList m_favoritesList; }; } // UkuiMenu diff --git a/src/extension/favorite/favorites-model.cpp b/src/extension/favorite/favorites-model.cpp index 24c472d..5cba398 100644 --- a/src/extension/favorite/favorites-model.cpp +++ b/src/extension/favorite/favorites-model.cpp @@ -86,12 +86,12 @@ void FavoritesModel::addAppToFavorites(const QString &id, const int &index) } else { appOrder = index; } - AppFavoritesModel::instance().changeAppState(id, appOrder); + AppFavoritesModel::instance().addAppToFavorites(id, appOrder); } void FavoritesModel::removeAppFromFavorites(const QString &id) { - AppFavoritesModel::instance().changeAppState(id, 0); + AppFavoritesModel::instance().removeAppFromFavorites(id); } void FavoritesModel::exchangedAppsOrder(const int &indexFrom, const int &indexTo) @@ -128,4 +128,9 @@ void FavoritesModel::addFileToFavorites(const QString &url) { AppFavoritesModel::instance().changeFileState(url, true); } + +void FavoritesModel::clearFavorites() +{ + AppFavoritesModel::instance().clearFavorites(); +} } // UkuiMenu diff --git a/src/extension/favorite/favorites-model.h b/src/extension/favorite/favorites-model.h index 09c29b4..4e1b9c0 100644 --- a/src/extension/favorite/favorites-model.h +++ b/src/extension/favorite/favorites-model.h @@ -66,6 +66,10 @@ public: * @param url 文件对应的url路径 */ Q_INVOKABLE void addFileToFavorites(const QString &url); + /** + * 清空所有已收藏应用 + */ + Q_INVOKABLE void clearFavorites(); protected: bool lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const override; diff --git a/src/model/folder-model.cpp b/src/extension/favorite/folder-model.cpp similarity index 96% rename from src/model/folder-model.cpp rename to src/extension/favorite/folder-model.cpp index 523676b..2d0b111 100644 --- a/src/model/folder-model.cpp +++ b/src/extension/favorite/folder-model.cpp @@ -21,6 +21,7 @@ #include "app-data-manager.h" #include "favorite/favorite-folder-helper.h" #include "context-menu-manager.h" +#include "libappdata/basic-app-model.h" namespace UkuiMenu { @@ -81,7 +82,8 @@ QVariant FolderModel::data(const QModelIndex &index, int role) const } DataEntity app; - if (!AppDataManager::instance()->getApp(m_apps.at(i), app)) { + + if (!BasicAppModel::instance()->getAppById(m_apps.at(i), app)) { return {}; } diff --git a/src/model/folder-model.h b/src/extension/favorite/folder-model.h similarity index 100% rename from src/model/folder-model.h rename to src/extension/favorite/folder-model.h diff --git a/src/model/model-manager.cpp b/src/model/model-manager.cpp index 03b37a9..51860ad 100644 --- a/src/model/model-manager.cpp +++ b/src/model/model-manager.cpp @@ -20,7 +20,7 @@ #include "label-model.h" #include "app-model.h" #include "app-group-model.h" -#include "folder-model.h" +#include "favorite/folder-model.h" #include namespace UkuiMenu {