新增provider管理器,为model提供应用数据

This commit is contained in:
hewenfei 2023-02-03 16:03:35 +08:00
parent f276cbe74b
commit 78a544a357
3 changed files with 174 additions and 6 deletions

View File

@ -75,20 +75,21 @@ set(SOURCE_FILES
src/main.cpp src/main.cpp
src/commons.h src/commons.cpp src/commons.h src/commons.cpp
src/menu-dbus-service.cpp src/menu-dbus-service.h src/menu-dbus-service.cpp src/menu-dbus-service.h
src/ukui-menu-application.cpp src/ukui-menu-application.h
src/model/model.cpp src/model/model.h src/model/model.cpp src/model/model.h
src/model/model-manager.cpp src/model/model-manager.h
src/settings/settings.cpp src/settings/settings.h src/settings/settings.cpp src/settings/settings.h
src/uiconfig/color-helper.cpp src/uiconfig/color-helper.h src/uiconfig/color-helper.cpp src/uiconfig/color-helper.h
src/uiconfig/theme-palette.cpp src/uiconfig/theme-palette.h src/uiconfig/theme-palette.cpp src/uiconfig/theme-palette.h
src/ukui-menu-application.cpp src/ukui-menu-application.h
src/model/model-manager.cpp src/model/model-manager.h
src/appdata/app-icon-provider.cpp src/appdata/app-icon-provider.h
src/windows/menu-main-window.cpp src/windows/menu-main-window.h src/windows/menu-main-window.cpp src/windows/menu-main-window.h
src/appdata/app-folder-helper.cpp src/appdata/app-folder-helper.h
src/extension/menu-extension.cpp src/extension/menu-extension.h
src/extension/menu-extension-iface.h
src/appdata/data-provider-plugin-iface.h src/appdata/data-provider-plugin-iface.h
src/appdata/app-data-manager.cpp src/appdata/app-data-manager.h src/appdata/app-data-manager.cpp src/appdata/app-data-manager.h
src/appdata/app-folder-helper.cpp src/appdata/app-folder-helper.h
src/appdata/app-icon-provider.cpp src/appdata/app-icon-provider.h
src/appdata/data-provider-manager.cpp src/appdata/data-provider-manager.h
src/appdata/plugin/all-app-data-provider.cpp src/appdata/plugin/all-app-data-provider.h src/appdata/plugin/all-app-data-provider.cpp src/appdata/plugin/all-app-data-provider.h
src/extension/menu-extension-iface.h
src/extension/menu-extension.cpp src/extension/menu-extension.h
src/extension/extensions/folder-extension.cpp src/extension/extensions/folder-extension.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 src/extension/extensions/recent-file-extension.cpp src/extension/extensions/recent-file-extension.h
) )

View File

@ -0,0 +1,106 @@
/*
* 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 "data-provider-manager.h"
#include "plugin/all-app-data-provider.h"
namespace UkuiMenu {
/**
* 1.model显示相同的数据
* 2.model隐藏插件
*/
DataProviderManager *DataProviderManager::instance()
{
static DataProviderManager dataProviderManager;
return &dataProviderManager;
}
DataProviderManager::DataProviderManager()
{
m_worker.start();
initProviders();
}
void DataProviderManager::initProviders()
{
auto *allProvider = new AllAppDataProvider;
registerProvider(allProvider);
activateProvider(allProvider->name());
allProvider->moveToThread(&m_worker);
}
void DataProviderManager::registerProvider(DataProviderPluginIFace *provider)
{
// if nullptr
if (!provider) {
return;
}
if (m_providers.contains(provider->name())) {
return;
}
m_providers.insert(provider->name(), provider);
connect(provider, &AllAppDataProvider::dataChanged, this, [this, provider](const QVector<DataEntity> &data) {
//qDebug() << "==DataProviderManager dataChanged:" << provider->name() << QThread::currentThreadId();
if (m_activatedPlugin != provider->name()) {
return;
}
Q_EMIT dataChanged(data);
});
}
QVector<DataEntity> DataProviderManager::data() const
{
return m_providers.value(m_activatedPlugin)->data();
}
QString DataProviderManager::activatedProvider() const
{
return m_activatedPlugin;
}
QStringList DataProviderManager::providers() const
{
return m_providers.keys();
}
void DataProviderManager::activateProvider(const QString &name)
{
if (!m_providers.contains(name)) {
return;
}
m_activatedPlugin = name;
Q_EMIT pluginChanged();
}
DataProviderManager::~DataProviderManager()
{
m_worker.quit();
m_worker.wait();
for (auto &provider : m_providers) {
provider->deleteLater();
}
}
} // UkuiMenu

View File

@ -0,0 +1,61 @@
/*
* 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_DATA_PROVIDER_MANAGER_H
#define UKUI_MENU_DATA_PROVIDER_MANAGER_H
#include <QObject>
#include <QThread>
#include <QVariant>
#include "data-provider-plugin-iface.h"
namespace UkuiMenu {
class DataProviderManager : public QObject
{
Q_OBJECT
public:
static DataProviderManager *instance();
~DataProviderManager() override;
QStringList providers() const;
QString activatedProvider() const;
void activateProvider(const QString &name);
QVector<DataEntity> data() const;
Q_SIGNALS:
void pluginChanged();
void dataChanged(QVector<DataEntity> data);
private:
DataProviderManager();
Q_DISABLE_COPY(DataProviderManager);
void initProviders();
void registerProvider(DataProviderPluginIFace *provider);
private:
QThread m_worker;
QString m_activatedPlugin{""};
QHash<QString, DataProviderPluginIFace*> m_providers;
};
} // UkuiMenu
#endif //UKUI_MENU_DATA_PROVIDER_MANAGER_H