diff --git a/src/extension/favorite/app-favorite-model.cpp b/src/extension/favorite/app-favorite-model.cpp index c06de8a..ac4edb5 100644 --- a/src/extension/favorite/app-favorite-model.cpp +++ b/src/extension/favorite/app-favorite-model.cpp @@ -1,6 +1,20 @@ -// -// Created by qiqi on 23-12-27. -// +/* + * Copyright (C) 2024, KylinSoft Co., Ltd. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ #include "favorites-config.h" #include "app-favorite-model.h" @@ -9,7 +23,6 @@ #include #include #include -#include namespace UkuiMenu { @@ -171,7 +184,7 @@ QVariant AppFavoritesModel::fileData(const QModelIndex &index, int role) const QStringList AppFavoritesModel::favoritesApps() const { QMap apps; - for (auto index : m_favoritesApps) { + 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); @@ -190,7 +203,7 @@ void AppFavoritesModel::changeAppState(const QString &id, const int &index) // 同步配置文件,更新应用顺序 if (index > 0) { - FavoritesConfig::instance().insertValue(id, index); + FavoritesConfig::instance().insertValue(APP_ID_SCHEME + id, index); } } @@ -219,7 +232,7 @@ void AppFavoritesModel::onAppRemoved(const QModelIndex &parent, int first, int l void AppFavoritesModel::addFavoriteApp(const QPersistentModelIndex &modelIndex) { - if (m_favoritesApps.contains(modelIndex)) { + if (m_favoritesApps.contains(modelIndex) || !modelIndex.isValid()) { return; } @@ -227,7 +240,7 @@ void AppFavoritesModel::addFavoriteApp(const QPersistentModelIndex &modelIndex) m_favoritesApps.append(modelIndex); endInsertRows(); - FavoritesConfig::instance().insertValue(modelIndex.data(DataEntity::Id).toString()); + FavoritesConfig::instance().insertValue(APP_ID_SCHEME + modelIndex.data(DataEntity::Id).toString()); } void AppFavoritesModel::removeFavoriteApp(const QPersistentModelIndex &modelIndex) @@ -240,7 +253,7 @@ void AppFavoritesModel::removeFavoriteApp(const QPersistentModelIndex &modelInde m_favoritesApps.removeOne(modelIndex); endRemoveRows(); - FavoritesConfig::instance().removeValueById(modelIndex.data(DataEntity::Id).toString()); + FavoritesConfig::instance().removeValueById(APP_ID_SCHEME + modelIndex.data(DataEntity::Id).toString()); } void AppFavoritesModel::onFolderAdded(const int &folderId, const int &order) @@ -256,7 +269,7 @@ void AppFavoritesModel::onFolderAdded(const int &folderId, const int &order) removeFavoriteApp(getIndexFromAppId(app)); } - FavoritesConfig::instance().insertValue(QString::number(folderId), std::max(0, order)); + FavoritesConfig::instance().insertValue(FOLDER_ID_SCHEME + QString::number(folderId), std::max(0, order)); } } @@ -272,7 +285,7 @@ void AppFavoritesModel::onFolderDeleted(const int &folderId, const QStringList & addFavoriteApp(modelIndex); } - FavoritesConfig::instance().removeValueById(QString::number(folderId)); + FavoritesConfig::instance().removeValueById(FOLDER_ID_SCHEME + QString::number(folderId)); } } @@ -313,4 +326,23 @@ QPersistentModelIndex AppFavoritesModel::getIndexFromAppId(const QString &id) co return *modelIndex; } + +void AppFavoritesModel::changeFileState(const QString &url, const bool &favorite) +{ + if (url.isEmpty()) { + return; + } + QString fileId; + if (url.startsWith(FILE_ID_SCHEME)) { + fileId = url; + } else { + fileId = FILE_ID_SCHEME + url; + } + + if (favorite) { + FavoritesConfig::instance().insertValue(fileId); + } else { + FavoritesConfig::instance().removeValueById(fileId); + } +} } // UkuiMenu diff --git a/src/extension/favorite/app-favorite-model.h b/src/extension/favorite/app-favorite-model.h index 26a9146..f9e03ad 100644 --- a/src/extension/favorite/app-favorite-model.h +++ b/src/extension/favorite/app-favorite-model.h @@ -1,6 +1,20 @@ -// -// Created by qiqi on 23-12-27. -// +/* + * Copyright (C) 2024, KylinSoft Co., Ltd. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ #ifndef UKUI_MENU_APP_FAVORITE_MODEL_H #define UKUI_MENU_APP_FAVORITE_MODEL_H @@ -26,6 +40,7 @@ public: QStringList favoritesApps() const; void changeAppState(const QString &id, const int &index); + void changeFileState(const QString &url, const bool &favorite); bool getApp(const QString &appid, DataEntity &app); private: diff --git a/src/extension/favorite/favorite-folder-helper.cpp b/src/extension/favorite/favorite-folder-helper.cpp index 9128f61..b0d2d7c 100644 --- a/src/extension/favorite/favorite-folder-helper.cpp +++ b/src/extension/favorite/favorite-folder-helper.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022, KylinSoft Co., Ltd. + * Copyright (C) 2024, KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -123,7 +123,7 @@ void FavoriteFolderHelper::addAppToNewFolder(const QString &appId, const QString insertFolder(folder); forceSync(); - Q_EMIT folderAdded(folder.id, FavoritesConfig::instance().getOrderById(appId)); + Q_EMIT folderAdded(folder.id, FavoritesConfig::instance().getOrderById(APP_ID_SCHEME + appId)); EventTrack::instance()->sendDefaultEvent("add_app_to_new_folder", "AppView"); } @@ -148,7 +148,7 @@ void FavoriteFolderHelper::addAppsToNewFolder(const QString &idFrom, const QStri folder.apps.append(idTo); insertFolder(folder); - Q_EMIT folderAdded(folder.id, FavoritesConfig::instance().getOrderById(idTo)); + Q_EMIT folderAdded(folder.id, FavoritesConfig::instance().getOrderById(APP_ID_SCHEME + idTo)); forceSync(); } diff --git a/src/extension/favorite/favorite-folder-helper.h b/src/extension/favorite/favorite-folder-helper.h index af4e236..24a632d 100644 --- a/src/extension/favorite/favorite-folder-helper.h +++ b/src/extension/favorite/favorite-folder-helper.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022, KylinSoft Co., Ltd. + * Copyright (C) 2024, KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/extension/favorite/favorites-config.cpp b/src/extension/favorite/favorites-config.cpp index fc65bc3..ee4a068 100644 --- a/src/extension/favorite/favorites-config.cpp +++ b/src/extension/favorite/favorites-config.cpp @@ -1,6 +1,21 @@ -// -// Created by qiqi on 23-12-27. -// +/* + * Copyright (C) 2024, KylinSoft Co., Ltd. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + #include #include #include @@ -61,16 +76,6 @@ void FavoritesConfig::removeValueById(const QString &id) setArrayToConfigFile(array); } -void FavoritesConfig::forceSync() -{ - -} - -QString FavoritesConfig::configFile() const -{ - return s_favoritesConfigFile; -} - void FavoritesConfig::initConfig(const QStringList &apps) { QJsonArray favoritesArray; @@ -159,4 +164,9 @@ void FavoritesConfig::changeOrder(const int &indexFrom, const int &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 eae8a8f..578a389 100644 --- a/src/extension/favorite/favorites-config.h +++ b/src/extension/favorite/favorites-config.h @@ -1,6 +1,20 @@ -// -// Created by qiqi on 23-12-27. -// +/* + * Copyright (C) 2024, KylinSoft Co., Ltd. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ #ifndef UKUI_MENU_FAVORITES_CONFIG_H #define UKUI_MENU_FAVORITES_CONFIG_H @@ -9,6 +23,10 @@ #include #include +static const QString APP_ID_SCHEME = "app://"; +static const QString FILE_ID_SCHEME = "file://"; +static const QString FOLDER_ID_SCHEME = "folder://"; + namespace UkuiMenu { class FavoritesConfig : public QObject @@ -18,13 +36,18 @@ public: static FavoritesConfig &instance(); QJsonObject getValue(const int &index) const; + /* + * 在配置文件中对应位置添加id + * 对应类型: 应用组(folder://)、应用(app://)、文件(file://) + */ void insertValue(const QString &id, const int &index = 0); void removeValueById(const QString &id); + /* + * 在配置文件中获取对应id的位置 + */ int getOrderById(const QString &id); void changeOrder(const int &indexFrom, const int &indexTo); - - void forceSync(); - QString configFile() const; + int configSize() const; void initConfig(const QStringList &apps); Q_SIGNALS: diff --git a/src/extension/favorite/favorites-model.cpp b/src/extension/favorite/favorites-model.cpp index d7426b7..24c472d 100644 --- a/src/extension/favorite/favorites-model.cpp +++ b/src/extension/favorite/favorites-model.cpp @@ -1,6 +1,21 @@ -// -// Created by qiqi on 23-12-28. -// +/* + * Copyright (C) 2024, KylinSoft Co., Ltd. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + #include #include "../context-menu-manager.h" @@ -23,28 +38,30 @@ FavoritesModel::FavoritesModel(QObject *parent) : QSortFilterProxyModel(parent) bool FavoritesModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const { - QVariant leftId = sourceModel()->data(source_left, DataEntity::Id); - QVariant rightId = sourceModel()->data(source_right, DataEntity::Id); + int index_left = -1; + int index_right = -1; - int leftIndex = -1; - int rightIndex = -1; - for (int i = 0; i < sourceModel()->rowCount(); i++) { - QJsonObject object = FavoritesConfig::instance().getValue(i); - if (object.contains("id")) { - if (object.value(QLatin1String("id")).toString() == leftId) { - leftIndex = i; - } else if (object.value(QLatin1String("id")).toString() == rightId) { - rightIndex = i; - } - } + QString id_left = urlFromModelIndex(source_left); + QString id_right = urlFromModelIndex(source_right); - if ((leftIndex != -1) && (rightIndex != -1)) { - return leftIndex < rightIndex; - } + index_left = FavoritesConfig::instance().getOrderById(id_left); + index_right = FavoritesConfig::instance().getOrderById(id_right); + + return index_left < index_right; +} + +QString FavoritesModel::urlFromModelIndex(const QModelIndex &modelIndex) const +{ + QString url; + if (modelIndex.data(DataEntity::Type).value() == DataType::Folder) { + url = FOLDER_ID_SCHEME + modelIndex.data(DataEntity::Id).toString(); + } else if (modelIndex.data(DataEntity::Type).value() == DataType::Files) { + url = FILE_ID_SCHEME + modelIndex.data(DataEntity::Id).toString(); + } else if (modelIndex.data(DataEntity::Type).value() == DataType::Normal) { + url = APP_ID_SCHEME + modelIndex.data(DataEntity::Id).toString(); } - // 对应Id没有在配置文件中找到 - return true; + return url; } void FavoritesModel::openMenu(const int &row) @@ -63,7 +80,13 @@ void FavoritesModel::openMenu(const int &row) void FavoritesModel::addAppToFavorites(const QString &id, const int &index) { - AppFavoritesModel::instance().changeAppState(id, index); + int appOrder; + if (index == -1) { + appOrder = FavoritesConfig::instance().configSize(); + } else { + appOrder = index; + } + AppFavoritesModel::instance().changeAppState(id, appOrder); } void FavoritesModel::removeAppFromFavorites(const QString &id) @@ -73,12 +96,22 @@ void FavoritesModel::removeAppFromFavorites(const QString &id) void FavoritesModel::exchangedAppsOrder(const int &indexFrom, const int &indexTo) { + if (indexFrom == indexTo) { + return; + } FavoritesConfig::instance().changeOrder(indexFrom, indexTo); } void FavoritesModel::addAppsToNewFolder(const QString &idFrom, const QString &idTo) { - FavoriteFolderHelper::instance()->addAppsToNewFolder(idFrom, idTo, ""); + if (idFrom == idTo) { + return; + } + + DataEntity app; + if (AppFavoritesModel::instance().getApp(idFrom, app) && AppFavoritesModel::instance().getApp(idTo, app)) { + FavoriteFolderHelper::instance()->addAppsToNewFolder(idFrom, idTo, ""); + } } void FavoritesModel::addAppToFolder(const QString &appId, const QString &folderId) @@ -91,5 +124,8 @@ void FavoritesModel::addAppToFolder(const QString &appId, const QString &folderI FavoriteFolderHelper::instance()->addAppToFolder(appId, folderId.toInt()); } - +void FavoritesModel::addFileToFavorites(const QString &url) +{ + AppFavoritesModel::instance().changeFileState(url, true); +} } // UkuiMenu diff --git a/src/extension/favorite/favorites-model.h b/src/extension/favorite/favorites-model.h index c14e421..09c29b4 100644 --- a/src/extension/favorite/favorites-model.h +++ b/src/extension/favorite/favorites-model.h @@ -1,6 +1,20 @@ -// -// Created by qiqi on 23-12-28. -// +/* + * Copyright (C) 2024, KylinSoft Co., Ltd. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ #ifndef UKUI_MENU_FAVORITES_MODEL_H #define UKUI_MENU_FAVORITES_MODEL_H @@ -18,7 +32,16 @@ public: static FavoritesModel &instance(); Q_INVOKABLE void openMenu(const int &row); - Q_INVOKABLE void addAppToFavorites(const QString &id, const int &index = 1); + /** + * 通过id添加应用到收藏 + * @param id + * @param index 默认为-1,添加到收藏最后一位;可以添加到收藏指定位置 + */ + Q_INVOKABLE void addAppToFavorites(const QString &id, const int &index = -1); + /** + * 通过id从收藏中移除 + * @param id + */ Q_INVOKABLE void removeAppFromFavorites(const QString &id); /** * 拖拽交换位置。 @@ -38,12 +61,19 @@ public: * @param folderId id为""时,新建应用组;id为数值时,添加至对应应用组 */ Q_INVOKABLE void addAppToFolder(const QString &appId, const QString& folderId); + /** + * 添加文件到收藏区域 + * @param url 文件对应的url路径 + */ + Q_INVOKABLE void addFileToFavorites(const QString &url); protected: bool lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const override; private: explicit FavoritesModel(QObject *parent = nullptr); + + QString urlFromModelIndex(const QModelIndex &modelIndex) const; }; } // UkuiMenu