新增all-data-provider数据插件

This commit is contained in:
hewenfei 2023-01-16 15:17:54 +08:00
parent 920c9ad99e
commit 2efc02d651
3 changed files with 247 additions and 0 deletions

View File

@ -88,6 +88,7 @@ set(SOURCE_FILES
src/extension/menu-extension-iface.h
src/appdata/data-provider-plugin-iface.h
src/appdata/app-data-manager.cpp src/appdata/app-data-manager.h
src/appdata/plugin/all-app-data-provider.cpp src/appdata/plugin/all-app-data-provider.h
src/extension/extensions/folder-extension.cpp src/extension/extensions/folder-extension.h
src/extension/extensions/recent-file-extension.cpp src/extension/extensions/recent-file-extension.h
)

View File

@ -0,0 +1,182 @@
/*
* Copyright (C) 2023, 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 "all-app-data-provider.h"
#include "app-data-manager.h"
#include "app-folder-helper.h"
#include <QDebug>
namespace UkuiMenu {
AllAppDataProvider::AllAppDataProvider() : DataProviderPluginIFace()
{
reloadFolderData();
reloadAppData();
connect(AppDataManager::instance(), &AppDataManager::appAdded, this, &AllAppDataProvider::onAppAdded);
connect(AppDataManager::instance(), &AppDataManager::appDeleted, this, &AllAppDataProvider::onAppDeleted);
connect(AppDataManager::instance(), &AppDataManager::appUpdated, this, &AllAppDataProvider::onAppUpdated);
connect(AppFolderHelper::instance(), &AppFolderHelper::folderDataChanged, this, &AllAppDataProvider::onAppFolderChanged);
}
int AllAppDataProvider::index()
{
return 0;
}
QString AllAppDataProvider::name()
{
return tr("All");
}
QString AllAppDataProvider::icon()
{
return "text-plain";
}
QString AllAppDataProvider::title()
{
return "Recently installed";
}
PluginGroup::Group AllAppDataProvider::group()
{
return PluginGroup::SortMenuItem;
}
QVector<DataEntity> AllAppDataProvider::data()
{
QVector<DataEntity> data;
mergeData(data);
return data;
}
void AllAppDataProvider::forceUpdate()
{
reloadFolderData();
reloadAppData();
sendData();
}
void AllAppDataProvider::reloadAppData()
{
QMutexLocker locker(&m_mutex);
m_appData.clear();
// TODO 验证多线程间隐式共享数复制后导致迭代器实现问题
QList<DataEntity> apps = AppDataManager::instance()->normalApps();
if (apps.isEmpty()) {
return;
}
for (const auto &app : apps) {
if (!m_folderApps.contains(app.id())) {
m_appData.append(app);
}
}
std::sort(m_appData.begin(), m_appData.end(), [](const DataEntity &a, const DataEntity &b) {
return (a.top() > b.top()) || (a.launchTimes() > b.launchTimes());
});
}
void AllAppDataProvider::reloadFolderData()
{
QMutexLocker locker(&m_mutex);
m_folderApps.clear();
m_folderData.clear();
QVector<Folder> 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;
});
DataEntity folderItem;
for (const auto &folder : folders) {
folderItem.setType(DataType::Folder);
folderItem.setIcon("image://appicon/text-plain");
folderItem.setName(folder.name);
m_folderApps.unite(folder.apps.toSet());
m_folderData.append(folderItem);
}
}
void AllAppDataProvider::mergeData(QVector<DataEntity> &data)
{
QMutexLocker locker(&m_mutex);
data.append(m_folderData);
data.append(m_appData.toVector());
}
void AllAppDataProvider::sendData()
{
QVector<DataEntity> data;
mergeData(data);
Q_EMIT dataChanged(data);
}
void AllAppDataProvider::onAppFolderChanged()
{
reloadFolderData();
sendData();
}
void AllAppDataProvider::onAppAdded(const QList<DataEntity>& apps)
{
{
QMutexLocker locker(&m_mutex);
m_appData.append(apps);
}
sendData();
}
void AllAppDataProvider::onAppDeleted(QStringList idList)
{
removeApps(idList);
sendData();
}
void AllAppDataProvider::removeApps(QStringList &idList)
{
QMutexLocker locker(&m_mutex);
QList<DataEntity>::iterator iterator = m_appData.begin();
while (iterator != m_appData.end() && !idList.isEmpty()) {
if (idList.removeOne((*iterator).id())) {
iterator = m_appData.erase(iterator);
continue;
}
++iterator;
}
}
void AllAppDataProvider::onAppUpdated(const QList<DataEntity>& apps)
{
Q_UNUSED(apps);
reloadAppData();
sendData();
}
} // UkuiMenu

View File

@ -0,0 +1,64 @@
/*
* Copyright (C) 2023, 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_ALL_APP_DATA_PROVIDER_H
#define UKUI_MENU_ALL_APP_DATA_PROVIDER_H
#include "data-provider-plugin-iface.h"
#include <QSet>
#include <QList>
namespace UkuiMenu {
class AllAppDataProvider : public DataProviderPluginIFace
{
Q_OBJECT
public:
explicit AllAppDataProvider();
int index() override;
QString name() override;
QString icon() override;
QString title() override;
PluginGroup::Group group() override;
QVector<DataEntity> data() override;
void forceUpdate() override;
private Q_SLOTS:
void onAppAdded(const QList<DataEntity>& apps);
void onAppDeleted(QStringList idList);
void onAppUpdated(const QList<DataEntity>& apps);
// TODO 文件夹数据新增,删除信号处理
void onAppFolderChanged();
private:
inline void sendData();
void removeApps(QStringList& idList);
void reloadAppData();
void reloadFolderData();
void mergeData(QVector<DataEntity> &data);
private:
QMutex m_mutex;
QSet<QString> m_folderApps;
QList<DataEntity> m_appData;
QVector<DataEntity> m_folderData;
};
} // UkuiMenu
#endif //UKUI_MENU_ALL_APP_DATA_PROVIDER_H