fix(extension):完善收藏插件细节(解散应用组到指定位置),兼容原有配置文件

This commit is contained in:
qiqi49 2024-01-24 11:31:57 +08:00 committed by hewenfei
parent 970dc89f26
commit 379b07fc7a
16 changed files with 178 additions and 139 deletions

View File

@ -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

View File

@ -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;

View File

@ -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)
}

View File

@ -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<int, QString> 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

View File

@ -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<QPersistentModelIndex> m_favoritesApps;
/**

View File

@ -15,13 +15,6 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include "favorite-folder-helper.h"
#include "app-data-manager.h"
#include "event-track.h"
#include "favorites-config.h"
#include "basic-app-model.h"
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
@ -30,13 +23,21 @@
#include <QDir>
#include <QMenu>
#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,8 +281,10 @@ void FavoriteFolderHelper::readData()
m_folders.clear();
}
// 遍历json数据节点
QJsonArray jsonArray = jsonDocument.array();
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")) {
@ -296,6 +302,12 @@ void FavoriteFolderHelper::readData()
}
}
}
}
} else {
saveData();
return;
}
}
void FavoriteFolderHelper::saveData()
@ -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

View File

@ -61,6 +61,7 @@ public:
bool getFolderFromId(const int& folderId, FavoritesFolder& folder);
bool containApp(const QString& appId);
bool deleteFolder(const int& folderId);
QList<FavoritesFolder> 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);

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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;

View File

@ -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 {};
}

View File

@ -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 <QQmlEngine>
namespace UkuiMenu {