diff --git a/src/appdata/app-folder-helper.cpp b/src/appdata/app-folder-helper.cpp index 80d73a1..d0f5aa6 100644 --- a/src/appdata/app-folder-helper.cpp +++ b/src/appdata/app-folder-helper.cpp @@ -40,6 +40,7 @@ AppFolderHelper *AppFolderHelper::instance() AppFolderHelper::AppFolderHelper() { + qRegisterMetaType("Folder"); if (!QFile::exists(s_folderConfigFile)) { QDir dir; QString folderConfigDir(QDir::homePath() + "/" + FOLDER_FILE_PATH); @@ -66,95 +67,104 @@ AppFolderHelper::~AppFolderHelper() void AppFolderHelper::insertFolder(const Folder &folder) { 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; } { QMutexLocker locker(&m_mutex); - Folder *folder = findFolder(folderName); - if (folder) { - if (!folder->apps.contains(appId)) { - folder->apps.append(appId); - } + if (!m_folders.contains(folderId)) { return; } + + Folder &folder = m_folders[folderId]; + if (folder.apps.contains(appId)) { + return; + } + + folder.apps.append(appId); } - // 新文件夹放最后 - Folder folder; - folder.name = folderName; - folder.apps.append(appId); - if (m_folders.isEmpty()) { - folder.index = 1; - } else { - folder.index = m_folders.last().index + 1; + Q_EMIT folderDataChanged(); +} + +void AppFolderHelper::addAppToNewFolder(const QString &appId, const QString &folderName) +{ + if (appId.isEmpty()) { + return; } + // 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); + 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; } + { + 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); - Folder *folder = findFolder(folderName); - if (!folder) { - return; - } - - if (folder->apps.contains(appId)) { - folder->apps.removeOne(appId); - } -} - -bool AppFolderHelper::deleteFolder(const QString &folderName) -{ - if (folderName.isEmpty()) { + if (!m_folders.contains(folderId)) { return false; } - QMutexLocker locker(&m_mutex); - QVector::iterator iterator = m_folders.begin(); - for (; iterator != m_folders.end(); ++iterator) { - if (folderName == iterator->name) { - m_folders.erase(iterator); - return true; - } + if (m_folders.remove(folderId)) { + return true; } return false; } -QVector AppFolderHelper::folderData() +QList AppFolderHelper::folderData() { 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); - Folder *tmp = findFolder(folderName); - if (!tmp) { + if (!m_folders.contains(folderId)) { return false; } - folder.name = tmp->name; - folder.index = tmp->index; - folder.apps = tmp->apps; + const Folder &tmp = m_folders[folderId]; + folder = tmp; return true; } @@ -166,51 +176,14 @@ bool AppFolderHelper::searchFolderByAppName(const QString &appId, Folder &folder } QMutexLocker locker(&m_mutex); - Folder *tmp = findFolderByAppName(appId); - if (!tmp) { - return false; - } - - 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::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; + for (const auto &tmp: m_folders) { + if (tmp.apps.contains(appId)) { + folder = tmp; + return true; } } - return nullptr; -} - -Folder *AppFolderHelper::findFolderByAppName(const QString &appId) -{ - if (m_folders.empty()) { - return nullptr; - } - - QVector::iterator iterator = m_folders.begin(); - for (; iterator != m_folders.end(); ++iterator) { - if (iterator->apps.contains(appId)) { - return iterator; - } - } - - return nullptr; + return false; } void AppFolderHelper::forceSync() @@ -243,15 +216,12 @@ void AppFolderHelper::readData() // 遍历json数据节点 QJsonArray jsonArray = jsonDocument.array(); - QJsonArray::iterator iterator = jsonArray.begin(); - while (iterator != jsonArray.end()) { - QJsonObject object = (*iterator).toObject(); - - if (object.contains("name") && object.contains("index") && object.contains("apps")) { + for (const auto &value : jsonArray) { + QJsonObject object = value.toObject(); + if (object.contains("name") && object.contains("id") && object.contains("apps")) { Folder folder; - 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(); for (const auto &app : apps) { @@ -262,8 +232,6 @@ void AppFolderHelper::readData() insertFolder(folder); } } - - ++iterator; } } @@ -274,7 +242,6 @@ void AppFolderHelper::saveData() return; } - // 读取json数据 QJsonDocument jsonDocument; QJsonArray folderArray; @@ -289,7 +256,7 @@ void AppFolderHelper::saveData() } object.insert("name", folder.name); - object.insert("index", folder.index); + object.insert("id", folder.id); object.insert("apps", apps); folderArray.append(object); diff --git a/src/appdata/app-folder-helper.h b/src/appdata/app-folder-helper.h index 57165d4..bc0a7b5 100644 --- a/src/appdata/app-folder-helper.h +++ b/src/appdata/app-folder-helper.h @@ -22,27 +22,30 @@ #include #include #include -#include +#include #include #include namespace UkuiMenu { +class AppFolderHelper; + class Folder { Q_GADGET - Q_PROPERTY(int index READ getIndex) + Q_PROPERTY(int id READ getId) Q_PROPERTY(QString name READ getName) Q_PROPERTY(QStringList apps READ getApps) + friend class AppFolderHelper; public: - int getIndex() const { return index; } + int getId() const { return id; } QString getName() const { return name; } QStringList getApps() const { return apps; } -public: - int index; - QString name; - QStringList apps; +private: + int id; // 文件夹唯一Id,文件夹排序值 + QString name; // 名称 + QStringList apps; // 应用列表 }; class AppFolderHelper : public QObject @@ -59,13 +62,14 @@ public: // TODO 修改文件夹信息 // xxxx - bool searchFolder(const QString& folderName, Folder &folder); + bool searchFolder(const int& folderId, Folder &folder); bool searchFolderByAppName(const QString& appId, Folder &folder); - QVector folderData(); + QList folderData(); - void addAppToFolder(const QString& appId, const QString& folderName); - void removeAppFromFolder(const QString& appId, const QString& folderName); - bool deleteFolder(const QString& folderName); + void addAppToFolder(const QString& appId, const int& folderId); + void addAppToNewFolder(const QString& appId, const QString& folderName); + void removeAppFromFolder(const QString& appId, const int& folderId); + bool deleteFolder(const int& folderId); void forceSync(); @@ -77,13 +81,11 @@ private: void readData(); void saveData(); void insertFolder(const Folder& folder); - Folder *findFolder(const QString& folderName); - Folder *findFolderByAppName(const QString& appId); // TODO 配置文件监听 private: QMutex m_mutex; - QVector m_folders; + QMap m_folders; static QString s_folderConfigFile; }; diff --git a/src/appdata/plugin/all-app-data-provider.cpp b/src/appdata/plugin/all-app-data-provider.cpp index f8d53fc..aa0dcdf 100644 --- a/src/appdata/plugin/all-app-data-provider.cpp +++ b/src/appdata/plugin/all-app-data-provider.cpp @@ -108,22 +108,22 @@ void AllAppDataProvider::reloadFolderData() m_folderApps.clear(); m_folderData.clear(); - QVector folders = AppFolderHelper::instance()->folderData(); + QList folders = AppFolderHelper::instance()->folderData(); if (folders.isEmpty()) { return; } std::sort(folders.begin(), folders.end(), [](const Folder &a, const Folder &b) { - return a.index < b.index; + return a.getId() < b.getId(); }); DataEntity folderItem; for (const auto &folder : folders) { folderItem.setType(DataType::Folder); 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); } }