wip: add files for policy bundle test

This commit is contained in:
august-alt 2021-07-02 19:01:27 +04:00
parent fc234c75f4
commit 2ad20714d3
12 changed files with 117 additions and 25 deletions

View File

@ -57,3 +57,4 @@ add_definitions(
add_gpui_library(gpui-model ${SOURCES})
target_link_libraries(gpui-model Qt5::Core)
target_link_libraries(gpui-model gpui-io)

View File

@ -20,8 +20,16 @@
#include "policybundle.h"
#include "../pluginstorage.h"
#include "../../plugins/admx/admxformat.h"
#include "../../plugins/adml/admlformat.h"
#include "../../io/genericfile.h"
#include <QDir>
#include <fstream>
namespace model {
namespace bundle {
@ -35,7 +43,7 @@ std::shared_ptr<PolicyTreeModel> PolicyBundle::loadFolder(const std::string& pat
const std::string& fallbackLanguage)
{
const QDir dir(path.c_str());
const QFileInfoList files = dir.entryInfoList();
const QFileInfoList files = dir.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot);
for (const QFileInfo& file : files) {
if (file.completeBaseName().toLower().endsWith(".admx")) {
@ -46,6 +54,33 @@ std::shared_ptr<PolicyTreeModel> PolicyBundle::loadFolder(const std::string& pat
return std::shared_ptr<PolicyTreeModel>(nullptr);
}
template<typename TPolicies, typename TFormat>
std::unique_ptr<TPolicies> loadPolicies(const QString& pluginName, const QFileInfo& admxFileName)
{
std::unique_ptr<TPolicies> policies;
TFormat* format = gpui::PluginStorage::instance()->createPluginClass<TFormat>(pluginName);
if (!format)
{
return policies;
}
std::ifstream file;
file.open(admxFileName.absoluteFilePath().toStdString(), std::ifstream::in);
if (file.good()) {
policies = std::make_unique<TPolicies>();
reinterpret_cast<io::PolicyFileFormat<TPolicies>*>(format)->read(file, policies.get());
}
file.close();
return policies;
}
bool PolicyBundle::loadAdmxAndAdml(const QFileInfo& admxFileName, const std::string& language,
const std::string& fallbackLanguage)
{
@ -53,6 +88,15 @@ bool PolicyBundle::loadAdmxAndAdml(const QFileInfo& admxFileName, const std::str
Q_UNUSED(language);
Q_UNUSED(fallbackLanguage);
auto policyDefinitions = loadPolicies<io::PolicyDefinitionsFile, gpui::AdmxFormat>("admx", admxFileName);
QString admlFilePath = admxFileName.absolutePath();
admlFilePath.replace(admlFilePath.length() - 4, 4, "adml");
qWarning() << admlFilePath;
//auto policyResources = loadPolicies<io::PolicyResourcesFile, gpui::AdmlFormat>()
// TODO: Load admx and adml files.
return false;
}

View File

@ -30,7 +30,7 @@ namespace gpui {
public:
QString name;
std::unique_ptr<QLibrary> library;
std::map<QString, std::function<void()> > pluginClasses;
std::map<QString, std::function<void*()> > pluginClasses;
};
Plugin::~Plugin()
@ -55,7 +55,7 @@ namespace gpui {
return d->library.get();
}
const std::map<QString, std::function<void ()> >& Plugin::getPluginClasses() const
const std::map<QString, std::function<void*()> >& Plugin::getPluginClasses() const
{
return d->pluginClasses;
}
@ -72,7 +72,7 @@ namespace gpui {
}
void Plugin::registerPluginClass(const QString &name, std::function<void ()> constructor)
void Plugin::registerPluginClass(const QString &name, std::function<void*()> constructor)
{
d->pluginClasses[name] = constructor;
}

View File

@ -46,13 +46,13 @@ namespace gpui {
void setLibrary(std::unique_ptr<QLibrary> library);
QLibrary* getLibrary() const;
const std::map<QString, std::function<void()>>& getPluginClasses() const;
const std::map<QString, std::function<void *()> > &getPluginClasses() const;
protected:
explicit Plugin(const QString& name);
explicit Plugin(const char* name);
void registerPluginClass(const QString& name, std::function<void()> constructor);
void registerPluginClass(const QString& name, std::function<void*()> constructor);
private:
PluginPrivate* d;

View File

@ -33,7 +33,7 @@ class PluginStoragePrivate
{
public:
std::map<QString, std::unique_ptr<Plugin> > pluginMap;
std::map<QString, std::map<QString, std::function<void()> > > classMap;
std::map<QString, std::map<QString, std::function<void*()> > > classMap;
};
PluginStorage::PluginStorage()
@ -144,26 +144,14 @@ void PluginStorage::loadDefaultPlugins()
loadPluginDirectory("/lib64/gpui/plugins/");
}
template<typename T>
T* PluginStorage::createPluginClass(const QString& pluginName)
{
auto search = d->classMap.find(pluginName);
if (search != d->classMap.end())
{
return search->second[typeid(T).name()]();
}
return nullptr;
}
void PluginStorage::registerPluginClass(const QString& pluginName, const QString& className, std::function<void()> constructor)
void PluginStorage::registerPluginClass(const QString& pluginName, const QString& className, std::function<void*()> constructor)
{
auto search = d->classMap.find(pluginName);
if (search == d->classMap.end())
{
d->classMap[pluginName] = std::map<QString, std::function<void()> >();
d->classMap[pluginName] = std::map<QString, std::function<void*()> >();
}
std::map<QString, std::function<void()> >& pluginConstructors = d->classMap[pluginName];
std::map<QString, std::function<void*()> >& pluginConstructors = d->classMap[pluginName];
pluginConstructors[className] = constructor;
}
@ -182,4 +170,15 @@ bool PluginStorage::unregisterPluginClass(const QString& pluginName, const QStri
return false;
}
void* PluginStorage::createPluginClass(const QString &className, const QString &pluginName)
{
auto search = d->classMap.find(pluginName);
if (search != d->classMap.end())
{
return search->second[className]();
}
return nullptr;
}
}

View File

@ -87,14 +87,19 @@ public:
* \brief createPluginClass
*/
template<typename T>
T* createPluginClass(const QString& pluginName);
T* createPluginClass(const QString& pluginName)
{
return reinterpret_cast<T*>(createPluginClass(typeid(T).name(), pluginName));
}
static PluginStorage* instance();
private:
void registerPluginClass(const QString& pluginName, const QString& className, std::function<void()> constructor);
void registerPluginClass(const QString& pluginName, const QString& className, std::function<void*()> constructor);
bool unregisterPluginClass(const QString& pluginName, const QString& className);
void *createPluginClass(const QString& className, const QString& pluginName);
PluginStorage();
~PluginStorage();

View File

@ -1,3 +1,4 @@
add_subdirectory(gui)
add_subdirectory(io)
add_subdirectory(model)
add_subdirectory(plugins)

View File

@ -0,0 +1,7 @@
find_package(GPUI COMPONENTS model REQUIRED)
include_directories(${GPUI_INCLUDE_DIRS})
find_package(Qt5 COMPONENTS Core Test REQUIRED)
set(QT_USE_QTTEST TRUE)
add_subdirectory(policybundle)

View File

@ -0,0 +1,4 @@
qt5_wrap_cpp(MOC_SOURCES policybundletest.h)
add_executable(policybundletest policybundletest.cpp ${MOC_SOURCES})
target_link_libraries(policybundletest gpui-model Qt5::Core Qt5::Test)
add_gpui_test(model.policybundletest policybundletest)

View File

@ -0,0 +1,30 @@
/***********************************************************************************************************************
**
** Copyright (C) 2021 BaseALT 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 2
** 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, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
***********************************************************************************************************************/
#include "policybundletest.h"
#include "../../../../src/io/policydefinitionsfile.h"
#include "../../../../src/model/bundle/policybundle.h"
using namespace io;
using namespace model::admx;
namespace tests {
}

View File

@ -43,7 +43,8 @@ void AdmlTest::read()
std::ifstream file;
file.open (dataPath + "example.adml", std::ifstream::in);
// file.open (dataPath + "example.adml", std::ifstream::in);
file.open("/home/august/Downloads/ADMX/Program Files/Microsoft Group Policy/Windows 10 October 2020 Update (20H2)/PolicyDefinitions/ru-ru/ActiveXInstallService.adml", std::fstream::in);
if (file.good())
{