feat(extension):收藏插件增加收藏文件接口

This commit is contained in:
qiqi49 2024-01-16 16:42:49 +08:00 committed by hewenfei
parent 898b61d312
commit 83b4d14306
8 changed files with 211 additions and 65 deletions

View File

@ -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 <https://www.gnu.org/licenses/>.
*
*/
#include "favorites-config.h"
#include "app-favorite-model.h"
@ -9,7 +23,6 @@
#include <QMimeDatabase>
#include <QFile>
#include <QUrl>
#include <QDebug>
namespace UkuiMenu {
@ -171,7 +184,7 @@ QVariant AppFavoritesModel::fileData(const QModelIndex &index, int role) const
QStringList AppFavoritesModel::favoritesApps() const
{
QMap<int, QString> 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

View File

@ -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 <https://www.gnu.org/licenses/>.
*
*/
#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:

View File

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

View File

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

View File

@ -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 <https://www.gnu.org/licenses/>.
*
*/
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
@ -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

View File

@ -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 <https://www.gnu.org/licenses/>.
*
*/
#ifndef UKUI_MENU_FAVORITES_CONFIG_H
#define UKUI_MENU_FAVORITES_CONFIG_H
@ -9,6 +23,10 @@
#include <QVariant>
#include <QJsonObject>
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:

View File

@ -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 <https://www.gnu.org/licenses/>.
*
*/
#include <QDir>
#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);
index_left = FavoritesConfig::instance().getOrderById(id_left);
index_right = FavoritesConfig::instance().getOrderById(id_right);
return index_left < index_right;
}
if ((leftIndex != -1) && (rightIndex != -1)) {
return leftIndex < rightIndex;
}
QString FavoritesModel::urlFromModelIndex(const QModelIndex &modelIndex) const
{
QString url;
if (modelIndex.data(DataEntity::Type).value<DataType::Type>() == DataType::Folder) {
url = FOLDER_ID_SCHEME + modelIndex.data(DataEntity::Id).toString();
} else if (modelIndex.data(DataEntity::Type).value<DataType::Type>() == DataType::Files) {
url = FILE_ID_SCHEME + modelIndex.data(DataEntity::Id).toString();
} else if (modelIndex.data(DataEntity::Type).value<DataType::Type>() == 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,13 +96,23 @@ 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)
{
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

View File

@ -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 <https://www.gnu.org/licenses/>.
*
*/
#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