修改文件夹数据接口
This commit is contained in:
parent
0f0170801b
commit
71a3e79930
|
@ -40,6 +40,7 @@ AppFolderHelper *AppFolderHelper::instance()
|
||||||
|
|
||||||
AppFolderHelper::AppFolderHelper()
|
AppFolderHelper::AppFolderHelper()
|
||||||
{
|
{
|
||||||
|
qRegisterMetaType<Folder>("Folder");
|
||||||
if (!QFile::exists(s_folderConfigFile)) {
|
if (!QFile::exists(s_folderConfigFile)) {
|
||||||
QDir dir;
|
QDir dir;
|
||||||
QString folderConfigDir(QDir::homePath() + "/" + FOLDER_FILE_PATH);
|
QString folderConfigDir(QDir::homePath() + "/" + FOLDER_FILE_PATH);
|
||||||
|
@ -66,95 +67,104 @@ AppFolderHelper::~AppFolderHelper()
|
||||||
void AppFolderHelper::insertFolder(const Folder &folder)
|
void AppFolderHelper::insertFolder(const Folder &folder)
|
||||||
{
|
{
|
||||||
QMutexLocker locker(&m_mutex);
|
QMutexLocker locker(&m_mutex);
|
||||||
m_folders.append(folder);
|
m_folders.insert(folder.id, folder);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AppFolderHelper::addAppToFolder(const QString& appId, const QString& folderName)
|
void AppFolderHelper::addAppToFolder(const QString &appId, const int &folderId)
|
||||||
{
|
{
|
||||||
if (appId.isEmpty() || folderName.isEmpty()) {
|
if (appId.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
QMutexLocker locker(&m_mutex);
|
QMutexLocker locker(&m_mutex);
|
||||||
Folder *folder = findFolder(folderName);
|
if (!m_folders.contains(folderId)) {
|
||||||
if (folder) {
|
|
||||||
if (!folder->apps.contains(appId)) {
|
|
||||||
folder->apps.append(appId);
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Folder &folder = m_folders[folderId];
|
||||||
|
if (folder.apps.contains(appId)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
folder.apps.append(appId);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 新文件夹放最后
|
Q_EMIT folderDataChanged();
|
||||||
Folder folder;
|
}
|
||||||
folder.name = folderName;
|
|
||||||
folder.apps.append(appId);
|
void AppFolderHelper::addAppToNewFolder(const QString &appId, const QString &folderName)
|
||||||
if (m_folders.isEmpty()) {
|
{
|
||||||
folder.index = 1;
|
if (appId.isEmpty()) {
|
||||||
} else {
|
return;
|
||||||
folder.index = m_folders.last().index + 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: max越界处理
|
||||||
|
int max = m_folders.isEmpty() ? -1 : m_folders.lastKey();
|
||||||
|
QString name = folderName;
|
||||||
|
if (name.isEmpty()) {
|
||||||
|
name = tr("New Folder %1").arg(m_folders.size() + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
Folder folder;
|
||||||
|
folder.id = ++max;
|
||||||
|
folder.name = name;
|
||||||
|
folder.apps.append(appId);
|
||||||
|
|
||||||
insertFolder(folder);
|
insertFolder(folder);
|
||||||
|
Q_EMIT folderDataChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
void AppFolderHelper::removeAppFromFolder(const QString &appId, const QString &folderName)
|
void AppFolderHelper::removeAppFromFolder(const QString &appId, const int &folderId)
|
||||||
{
|
{
|
||||||
if (appId.isEmpty() || folderName.isEmpty()) {
|
if (appId.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
QMutexLocker locker(&m_mutex);
|
||||||
|
if (!m_folders.contains(folderId)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Folder &folder = m_folders[folderId];
|
||||||
|
if (!folder.apps.contains(appId)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
folder.apps.removeOne(appId);
|
||||||
|
}
|
||||||
|
|
||||||
|
Q_EMIT folderDataChanged();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool AppFolderHelper::deleteFolder(const int& folderId)
|
||||||
|
{
|
||||||
QMutexLocker locker(&m_mutex);
|
QMutexLocker locker(&m_mutex);
|
||||||
Folder *folder = findFolder(folderName);
|
if (!m_folders.contains(folderId)) {
|
||||||
if (!folder) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (folder->apps.contains(appId)) {
|
|
||||||
folder->apps.removeOne(appId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool AppFolderHelper::deleteFolder(const QString &folderName)
|
|
||||||
{
|
|
||||||
if (folderName.isEmpty()) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
QMutexLocker locker(&m_mutex);
|
if (m_folders.remove(folderId)) {
|
||||||
QVector<Folder>::iterator iterator = m_folders.begin();
|
return true;
|
||||||
for (; iterator != m_folders.end(); ++iterator) {
|
|
||||||
if (folderName == iterator->name) {
|
|
||||||
m_folders.erase(iterator);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
QVector<Folder> AppFolderHelper::folderData()
|
QList<Folder> AppFolderHelper::folderData()
|
||||||
{
|
{
|
||||||
QMutexLocker locker(&m_mutex);
|
QMutexLocker locker(&m_mutex);
|
||||||
return m_folders;
|
return m_folders.values();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AppFolderHelper::searchFolder(const QString& folderName, Folder &folder)
|
bool AppFolderHelper::searchFolder(const int &folderId, Folder &folder)
|
||||||
{
|
{
|
||||||
if (folderName.isEmpty()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
QMutexLocker locker(&m_mutex);
|
QMutexLocker locker(&m_mutex);
|
||||||
Folder *tmp = findFolder(folderName);
|
if (!m_folders.contains(folderId)) {
|
||||||
if (!tmp) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
folder.name = tmp->name;
|
const Folder &tmp = m_folders[folderId];
|
||||||
folder.index = tmp->index;
|
folder = tmp;
|
||||||
folder.apps = tmp->apps;
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -166,51 +176,14 @@ bool AppFolderHelper::searchFolderByAppName(const QString &appId, Folder &folder
|
||||||
}
|
}
|
||||||
|
|
||||||
QMutexLocker locker(&m_mutex);
|
QMutexLocker locker(&m_mutex);
|
||||||
Folder *tmp = findFolderByAppName(appId);
|
for (const auto &tmp: m_folders) {
|
||||||
if (!tmp) {
|
if (tmp.apps.contains(appId)) {
|
||||||
return false;
|
folder = tmp;
|
||||||
}
|
return true;
|
||||||
|
|
||||||
folder.name = tmp->name;
|
|
||||||
folder.index = tmp->index;
|
|
||||||
folder.apps = tmp->apps;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
Folder *AppFolderHelper::findFolder(const QString &folderName)
|
|
||||||
{
|
|
||||||
if (m_folders.empty()) {
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
QVector<Folder>::iterator iterator = m_folders.begin();
|
|
||||||
for (; iterator != m_folders.end(); ++iterator) {
|
|
||||||
if (folderName == iterator->name) {
|
|
||||||
// 如果定义了 QT_STRICT_ITERATORS 宏,那么迭代器为:class iterator; 否则迭代器为:typedef T* iterator;
|
|
||||||
// kylin上的qt库没有定义QT_STRICT_ITERATORS宏,需要注意该处可能出现的编译错误,参见QVector源码。
|
|
||||||
// return &*iterator;
|
|
||||||
return iterator;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nullptr;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
Folder *AppFolderHelper::findFolderByAppName(const QString &appId)
|
|
||||||
{
|
|
||||||
if (m_folders.empty()) {
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
QVector<Folder>::iterator iterator = m_folders.begin();
|
|
||||||
for (; iterator != m_folders.end(); ++iterator) {
|
|
||||||
if (iterator->apps.contains(appId)) {
|
|
||||||
return iterator;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void AppFolderHelper::forceSync()
|
void AppFolderHelper::forceSync()
|
||||||
|
@ -243,15 +216,12 @@ void AppFolderHelper::readData()
|
||||||
|
|
||||||
// 遍历json数据节点
|
// 遍历json数据节点
|
||||||
QJsonArray jsonArray = jsonDocument.array();
|
QJsonArray jsonArray = jsonDocument.array();
|
||||||
QJsonArray::iterator iterator = jsonArray.begin();
|
for (const auto &value : jsonArray) {
|
||||||
while (iterator != jsonArray.end()) {
|
QJsonObject object = value.toObject();
|
||||||
QJsonObject object = (*iterator).toObject();
|
if (object.contains("name") && object.contains("id") && object.contains("apps")) {
|
||||||
|
|
||||||
if (object.contains("name") && object.contains("index") && object.contains("apps")) {
|
|
||||||
Folder folder;
|
Folder folder;
|
||||||
|
|
||||||
folder.name = object.value(QLatin1String("name")).toString();
|
folder.name = object.value(QLatin1String("name")).toString();
|
||||||
folder.index = object.value(QLatin1String("index")).toInt();
|
folder.id = object.value(QLatin1String("id")).toInt();
|
||||||
|
|
||||||
QJsonArray apps = object.value(QLatin1String("apps")).toArray();
|
QJsonArray apps = object.value(QLatin1String("apps")).toArray();
|
||||||
for (const auto &app : apps) {
|
for (const auto &app : apps) {
|
||||||
|
@ -262,8 +232,6 @@ void AppFolderHelper::readData()
|
||||||
insertFolder(folder);
|
insertFolder(folder);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
++iterator;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -274,7 +242,6 @@ void AppFolderHelper::saveData()
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 读取json数据
|
|
||||||
QJsonDocument jsonDocument;
|
QJsonDocument jsonDocument;
|
||||||
QJsonArray folderArray;
|
QJsonArray folderArray;
|
||||||
|
|
||||||
|
@ -289,7 +256,7 @@ void AppFolderHelper::saveData()
|
||||||
}
|
}
|
||||||
|
|
||||||
object.insert("name", folder.name);
|
object.insert("name", folder.name);
|
||||||
object.insert("index", folder.index);
|
object.insert("id", folder.id);
|
||||||
object.insert("apps", apps);
|
object.insert("apps", apps);
|
||||||
|
|
||||||
folderArray.append(object);
|
folderArray.append(object);
|
||||||
|
|
|
@ -22,27 +22,30 @@
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QMutex>
|
#include <QMutex>
|
||||||
#include <QMutexLocker>
|
#include <QMutexLocker>
|
||||||
#include <QVector>
|
#include <QMap>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QStringList>
|
#include <QStringList>
|
||||||
|
|
||||||
namespace UkuiMenu {
|
namespace UkuiMenu {
|
||||||
|
|
||||||
|
class AppFolderHelper;
|
||||||
|
|
||||||
class Folder
|
class Folder
|
||||||
{
|
{
|
||||||
Q_GADGET
|
Q_GADGET
|
||||||
Q_PROPERTY(int index READ getIndex)
|
Q_PROPERTY(int id READ getId)
|
||||||
Q_PROPERTY(QString name READ getName)
|
Q_PROPERTY(QString name READ getName)
|
||||||
Q_PROPERTY(QStringList apps READ getApps)
|
Q_PROPERTY(QStringList apps READ getApps)
|
||||||
|
friend class AppFolderHelper;
|
||||||
public:
|
public:
|
||||||
int getIndex() const { return index; }
|
int getId() const { return id; }
|
||||||
QString getName() const { return name; }
|
QString getName() const { return name; }
|
||||||
QStringList getApps() const { return apps; }
|
QStringList getApps() const { return apps; }
|
||||||
|
|
||||||
public:
|
private:
|
||||||
int index;
|
int id; // 文件夹唯一Id,文件夹排序值
|
||||||
QString name;
|
QString name; // 名称
|
||||||
QStringList apps;
|
QStringList apps; // 应用列表
|
||||||
};
|
};
|
||||||
|
|
||||||
class AppFolderHelper : public QObject
|
class AppFolderHelper : public QObject
|
||||||
|
@ -59,13 +62,14 @@ public:
|
||||||
|
|
||||||
// TODO 修改文件夹信息
|
// TODO 修改文件夹信息
|
||||||
// xxxx
|
// xxxx
|
||||||
bool searchFolder(const QString& folderName, Folder &folder);
|
bool searchFolder(const int& folderId, Folder &folder);
|
||||||
bool searchFolderByAppName(const QString& appId, Folder &folder);
|
bool searchFolderByAppName(const QString& appId, Folder &folder);
|
||||||
QVector<Folder> folderData();
|
QList<Folder> folderData();
|
||||||
|
|
||||||
void addAppToFolder(const QString& appId, const QString& folderName);
|
void addAppToFolder(const QString& appId, const int& folderId);
|
||||||
void removeAppFromFolder(const QString& appId, const QString& folderName);
|
void addAppToNewFolder(const QString& appId, const QString& folderName);
|
||||||
bool deleteFolder(const QString& folderName);
|
void removeAppFromFolder(const QString& appId, const int& folderId);
|
||||||
|
bool deleteFolder(const int& folderId);
|
||||||
|
|
||||||
void forceSync();
|
void forceSync();
|
||||||
|
|
||||||
|
@ -77,13 +81,11 @@ private:
|
||||||
void readData();
|
void readData();
|
||||||
void saveData();
|
void saveData();
|
||||||
void insertFolder(const Folder& folder);
|
void insertFolder(const Folder& folder);
|
||||||
Folder *findFolder(const QString& folderName);
|
|
||||||
Folder *findFolderByAppName(const QString& appId);
|
|
||||||
// TODO 配置文件监听
|
// TODO 配置文件监听
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QMutex m_mutex;
|
QMutex m_mutex;
|
||||||
QVector<Folder> m_folders;
|
QMap<int, Folder> m_folders;
|
||||||
static QString s_folderConfigFile;
|
static QString s_folderConfigFile;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -108,22 +108,22 @@ void AllAppDataProvider::reloadFolderData()
|
||||||
m_folderApps.clear();
|
m_folderApps.clear();
|
||||||
m_folderData.clear();
|
m_folderData.clear();
|
||||||
|
|
||||||
QVector<Folder> folders = AppFolderHelper::instance()->folderData();
|
QList<Folder> folders = AppFolderHelper::instance()->folderData();
|
||||||
if (folders.isEmpty()) {
|
if (folders.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::sort(folders.begin(), folders.end(), [](const Folder &a, const Folder &b) {
|
std::sort(folders.begin(), folders.end(), [](const Folder &a, const Folder &b) {
|
||||||
return a.index < b.index;
|
return a.getId() < b.getId();
|
||||||
});
|
});
|
||||||
|
|
||||||
DataEntity folderItem;
|
DataEntity folderItem;
|
||||||
for (const auto &folder : folders) {
|
for (const auto &folder : folders) {
|
||||||
folderItem.setType(DataType::Folder);
|
folderItem.setType(DataType::Folder);
|
||||||
folderItem.setIcon("image://appicon/text-plain");
|
folderItem.setIcon("image://appicon/text-plain");
|
||||||
folderItem.setName(folder.name);
|
folderItem.setName(folder.getName());
|
||||||
|
|
||||||
m_folderApps.unite(folder.apps.toSet());
|
m_folderApps.unite(folder.getApps().toSet());
|
||||||
m_folderData.append(folderItem);
|
m_folderData.append(folderItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue