Skip to content
Snippets Groups Projects
Commit f625036d authored by Moritz Maxeiner's avatar Moritz Maxeiner
Browse files

Cleanup whitespace

parent e231c66f
Branches
No related tags found
No related merge requests found
...@@ -16,8 +16,8 @@ ...@@ -16,8 +16,8 @@
std::vector<std::string> QueryKey(HKEY hKey, std::string path) std::vector<std::string> QueryKey(HKEY hKey, std::string path)
{ {
//See https://docs.microsoft.com/en-us/windows/desktop/sysinfo/enumerating-registry-subkeys //See https://docs.microsoft.com/en-us/windows/desktop/sysinfo/enumerating-registry-subkeys
std::vector<std::string> list; std::vector<std::string> list;
TCHAR achClass[MAX_PATH] = TEXT(""); // buffer for class name TCHAR achClass[MAX_PATH] = TEXT(""); // buffer for class name
DWORD cchClassName = MAX_PATH; // size of class string DWORD cchClassName = MAX_PATH; // size of class string
DWORD cSubKeys=0; // number of subkeys DWORD cSubKeys=0; // number of subkeys
...@@ -52,7 +52,7 @@ std::vector<std::string> QueryKey(HKEY hKey, std::string path) ...@@ -52,7 +52,7 @@ std::vector<std::string> QueryKey(HKEY hKey, std::string path)
// Enumerate the key values. // Enumerate the key values.
if (cValues) if (cValues)
{ {
//printf( "\nNumber of values: %d\n", cValues); //printf( "\nNumber of values: %d\n", cValues);
for (i=0, retCode=ERROR_SUCCESS; i<cValues; i++) for (i=0, retCode=ERROR_SUCCESS; i<cValues; i++)
{ {
...@@ -68,51 +68,51 @@ std::vector<std::string> QueryKey(HKEY hKey, std::string path) ...@@ -68,51 +68,51 @@ std::vector<std::string> QueryKey(HKEY hKey, std::string path)
if (retCode == ERROR_SUCCESS ) if (retCode == ERROR_SUCCESS )
{ {
CRegKey regKey; CRegKey regKey;
CHAR szBuffer[512]; CHAR szBuffer[512];
ULONG dwBufferSize = sizeof(szBuffer); ULONG dwBufferSize = sizeof(szBuffer);
if(ERROR_SUCCESS != regKey.Open(HKEY_LOCAL_MACHINE, path.c_str())) if(ERROR_SUCCESS != regKey.Open(HKEY_LOCAL_MACHINE, path.c_str()))
{ {
qWarning() << "Error opening registry path " << path.c_str(); qWarning() << "Error opening registry path " << path.c_str();
regKey.Close(); regKey.Close();
} }
if( ERROR_SUCCESS != regKey.QueryStringValue(achValue,szBuffer,&dwBufferSize)) if( ERROR_SUCCESS != regKey.QueryStringValue(achValue,szBuffer,&dwBufferSize))
{ {
qWarning() << "Error opening registry value " << achValue; qWarning() << "Error opening registry value " << achValue;
regKey.Close(); regKey.Close();
} }
std::string fp = szBuffer; std::string fp = szBuffer;
std::replace( fp.begin(), fp.end(), '\\', '/'); std::replace( fp.begin(), fp.end(), '\\', '/');
list.push_back(fp); list.push_back(fp);
} }
} }
} }
return list; return list;
} }
#endif #endif
std::vector<std::string> PluginLoader::queryRegistryBehaviors(std::string path) std::vector<std::string> PluginLoader::queryRegistryBehaviors(std::string path)
{ {
std::vector<std::string> list; std::vector<std::string> list;
#ifdef _WIN32 #ifdef _WIN32
HKEY hTestKey; HKEY hTestKey;
if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, if( RegOpenKeyEx( HKEY_LOCAL_MACHINE,
TEXT(path.c_str()), TEXT(path.c_str()),
0, 0,
KEY_READ, KEY_READ,
&hTestKey) == ERROR_SUCCESS) &hTestKey) == ERROR_SUCCESS)
{ {
list = QueryKey(hTestKey, path); list = QueryKey(hTestKey, path);
} }
RegCloseKey(hTestKey); RegCloseKey(hTestKey);
#endif #endif
return list; return list;
} }
#ifdef _WIN32 #ifdef _WIN32
...@@ -130,7 +130,7 @@ const char * WinGetEnv(const char * name) ...@@ -130,7 +130,7 @@ const char * WinGetEnv(const char * name)
} }
} }
bool WinSetEnv(const char* name, const char* toWhat){ bool WinSetEnv(const char* name, const char* toWhat){
return SetEnvironmentVariableA(name, toWhat); return SetEnvironmentVariableA(name, toWhat);
} }
#endif #endif
...@@ -142,198 +142,198 @@ bool is_shared_library(std::filesystem::path const& p) ...@@ -142,198 +142,198 @@ bool is_shared_library(std::filesystem::path const& p)
const char* PluginLoader::addDllPath(std::string f) const char* PluginLoader::addDllPath(std::string f)
{ {
//Get the directory of the DLL/*.so and add it to the PATH env variable. //Get the directory of the DLL/*.so and add it to the PATH env variable.
//This way dependencies can be shipped in the same directory //This way dependencies can be shipped in the same directory
#ifdef _WIN32 #ifdef _WIN32
QFileInfo finf(f.c_str()); QFileInfo finf(f.c_str());
//rather than the buggy _getenv: https://docs.microsoft.com/de-de/windows/desktop/api/winbase/nf-winbase-getenvironmentvariable //rather than the buggy _getenv: https://docs.microsoft.com/de-de/windows/desktop/api/winbase/nf-winbase-getenvironmentvariable
auto old_path = WinGetEnv("PATH"); auto old_path = WinGetEnv("PATH");
auto path = std::ostringstream(); auto path = std::ostringstream();
if(old_path){ if(old_path){
path << old_path << ";" << finf.absolutePath().toStdString().c_str(); path << old_path << ";" << finf.absolutePath().toStdString().c_str();
WinSetEnv("PATH", path.str().c_str()); WinSetEnv("PATH", path.str().c_str());
}else{ }else{
qWarning() << "Failed to get and modify PATH enviromental variable."; qWarning() << "Failed to get and modify PATH enviromental variable.";
} }
return old_path; return old_path;
#endif #endif
return ""; return "";
} }
void PluginLoader::delDllPath(const char* oldPath){ void PluginLoader::delDllPath(const char* oldPath){
//reset path. We don't want some weird cross-effects //reset path. We don't want some weird cross-effects
#ifdef _WIN32 #ifdef _WIN32
if(oldPath){ if(oldPath){
WinSetEnv("PATH", oldPath); WinSetEnv("PATH", oldPath);
} }
#endif #endif
} }
bool endsWith(std::string value, std::string ending) bool endsWith(std::string value, std::string ending)
{ {
std::transform(value.begin(), value.end(), value.begin(), ::tolower); std::transform(value.begin(), value.end(), value.begin(), ::tolower);
std::transform(ending.begin(), ending.end(), ending.begin(), ::tolower); std::transform(ending.begin(), ending.end(), ending.begin(), ::tolower);
if (ending.size() > value.size()) return false; if (ending.size() > value.size()) return false;
return std::equal(ending.rbegin(), ending.rend(), value.rbegin()); return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
} }
bool validSuffix(std::string f, std::string suffix){ bool validSuffix(std::string f, std::string suffix){
return (endsWith(f,suffix+".dll") || endsWith(f,suffix+".so")); return (endsWith(f,suffix+".dll") || endsWith(f,suffix+".so"));
} }
std::vector<std::string> PluginLoader::searchDirectoriesForPlugins(std::vector<std::string> list, std::string suffix){ std::vector<std::string> PluginLoader::searchDirectoriesForPlugins(std::vector<std::string> list, std::string suffix){
//Search directories //Search directories
std::vector<std::string> filesFromFolders; std::vector<std::string> filesFromFolders;
for (auto f: list) for (auto f: list)
{ {
auto path = std::filesystem::path(f); auto path = std::filesystem::path(f);
if (path.empty()) if (path.empty())
{ {
continue; continue;
} }
if (std::filesystem::is_directory(path)) if (std::filesystem::is_directory(path))
{ {
try try
{ {
for (auto& e : std::filesystem::directory_iterator(path)) for (auto& e : std::filesystem::directory_iterator(path))
{ {
auto p = e.path(); auto p = e.path();
if (is_shared_library(p) && p.replace_extension().extension() == ".robo_tracker") if (is_shared_library(p) && p.replace_extension().extension() == ".robo_tracker")
{ {
filesFromFolders.push_back(p.string()); filesFromFolders.push_back(p.string());
} }
} }
} }
catch (std::filesystem::filesystem_error const& e) catch (std::filesystem::filesystem_error const& e)
{ {
qWarning() << e.what(); qWarning() << e.what();
} }
} }
else if (is_shared_library(path)) else if (is_shared_library(path))
{ {
filesFromFolders.push_back(f); filesFromFolders.push_back(f);
} }
else else
{ {
qWarning() << "Neither a directory, nor a shared library:" << f.data(); qWarning() << "Neither a directory, nor a shared library:" << f.data();
} }
} }
return filesFromFolders; return filesFromFolders;
} }
PluginLoader::PluginLoader(QObject *parent) PluginLoader::PluginLoader(QObject *parent)
{ {
m_MetaData = nullptr; m_MetaData = nullptr;
m_PluginLoader = new QPluginLoader(this); m_PluginLoader = new QPluginLoader(this);
m_PluginListModel = new QStringListModel(); m_PluginListModel = new QStringListModel();
} }
PluginLoader::~PluginLoader() PluginLoader::~PluginLoader()
{ {
delete m_PluginLoader; delete m_PluginLoader;
delete m_PluginListModel; delete m_PluginListModel;
} }
bool PluginLoader::loadPluginFromFilename(QString const& filename) bool PluginLoader::loadPluginFromFilename(QString const& filename)
{ {
bool retval = false; bool retval = false;
if (m_PluginLoader->isLoaded()) { if (m_PluginLoader->isLoaded()) {
m_PluginLoader->unload(); m_PluginLoader->unload();
} }
bool isLib = QLibrary::isLibrary(filename); bool isLib = QLibrary::isLibrary(filename);
if (isLib) { if (isLib) {
auto oldPath = PluginLoader::addDllPath(filename.toStdString()); auto oldPath = PluginLoader::addDllPath(filename.toStdString());
m_PluginLoader->setFileName(filename); m_PluginLoader->setFileName(filename);
readMetaDataFromPlugin(); readMetaDataFromPlugin();
retval = m_PluginLoader->load(); retval = m_PluginLoader->load();
QString s = m_PluginLoader->errorString(); QString s = m_PluginLoader->errorString();
std::string ss = s.toStdString(); std::string ss = s.toStdString();
addPluginnameToLists(getCurrentPluginName(), filename); addPluginnameToLists(getCurrentPluginName(), filename);
if (!m_PluginLoader->isLoaded()) if (!m_PluginLoader->isLoaded())
{ {
qWarning() << ss.c_str(); qWarning() << ss.c_str();
retval = false; retval = false;
} }
PluginLoader::delDllPath(oldPath); PluginLoader::delDllPath(oldPath);
} }
else { else {
retval = false; retval = false;
} }
return retval; return retval;
} }
void PluginLoader::addPluginnameToLists(QString mstring, QString filename) void PluginLoader::addPluginnameToLists(QString mstring, QString filename)
{ {
if (!m_PluginList.contains(mstring)) if (!m_PluginList.contains(mstring))
m_PluginList.append(mstring); m_PluginList.append(mstring);
m_PluginListModel->setStringList(m_PluginList); m_PluginListModel->setStringList(m_PluginList);
m_PluginMap.insert(std::pair<QString, QString>(mstring, filename)); m_PluginMap.insert(std::pair<QString, QString>(mstring, filename));
} }
bool PluginLoader::loadPluginFromName(QString name) { bool PluginLoader::loadPluginFromName(QString name) {
QString filename = m_PluginMap.find(name)->second; QString filename = m_PluginMap.find(name)->second;
return loadPluginFromFilename(filename); return loadPluginFromFilename(filename);
} }
int PluginLoader::addToPluginList(QString filename, QString suffix) { int PluginLoader::addToPluginList(QString filename, QString suffix) {
if (!validSuffix(filename.toStdString(), suffix.toStdString())) if (!validSuffix(filename.toStdString(), suffix.toStdString()))
return 1; return 1;
bool isLib = QLibrary::isLibrary(filename); bool isLib = QLibrary::isLibrary(filename);
if (isLib) { if (isLib) {
QPluginLoader loader; QPluginLoader loader;
loader.setFileName(filename); loader.setFileName(filename);
QJsonValue pluginMeda(loader.metaData().value("MetaData")); QJsonValue pluginMeda(loader.metaData().value("MetaData"));
QJsonObject metaObj = pluginMeda.toObject(); QJsonObject metaObj = pluginMeda.toObject();
QString mstring = metaObj.value("name").toString(); QString mstring = metaObj.value("name").toString();
addPluginnameToLists(mstring, filename); addPluginnameToLists(mstring, filename);
} }
else { else {
return 2; return 2;
qWarning() << "Error reading plugin: " << filename; qWarning() << "Error reading plugin: " << filename;
} }
return 0; return 0;
} }
QStringListModel* PluginLoader::getPluginList() { QStringListModel* PluginLoader::getPluginList() {
return m_PluginListModel; return m_PluginListModel;
} }
QObject* PluginLoader::getPluginInstance() { QObject* PluginLoader::getPluginInstance() {
return (m_PluginLoader->instance()); return (m_PluginLoader->instance());
} }
QJsonObject PluginLoader::getPluginMetaData() const QJsonObject PluginLoader::getPluginMetaData() const
{ {
if (m_MetaData == nullptr) if (m_MetaData == nullptr)
qFatal("(getPluginMetaData) No plugin loaded"); qFatal("(getPluginMetaData) No plugin loaded");
return *m_MetaData; return *m_MetaData;
} }
void PluginLoader::readMetaDataFromPlugin() void PluginLoader::readMetaDataFromPlugin()
{ {
m_MetaData = std::make_shared<QJsonObject>(m_PluginLoader->metaData().value("MetaData").toObject()); m_MetaData = std::make_shared<QJsonObject>(m_PluginLoader->metaData().value("MetaData").toObject());
} }
bool PluginLoader::getIsPluginLoaded() { bool PluginLoader::getIsPluginLoaded() {
return m_isPluginLoaded; return m_isPluginLoaded;
} }
QString PluginLoader::getCurrentPluginName() { QString PluginLoader::getCurrentPluginName() {
if (m_MetaData == nullptr) if (m_MetaData == nullptr)
return "Error name"; return "Error name";
return m_MetaData->value("name").toString(); return m_MetaData->value("name").toString();
} }
const std::map<QString, QString> &PluginLoader::getPluginMap() const const std::map<QString, QString> &PluginLoader::getPluginMap() const
{ {
......
...@@ -16,90 +16,90 @@ ...@@ -16,90 +16,90 @@
class PluginLoader : QObject class PluginLoader : QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit PluginLoader(QObject *parent = 0); explicit PluginLoader(QObject *parent = 0);
~PluginLoader(); ~PluginLoader();
/** /**
* Loads a file as a QT plugin and reads it's name from the metadata. * Loads a file as a QT plugin and reads it's name from the metadata.
* It will then be added to the stringlist and is selectable via "loadPluginFromName". * It will then be added to the stringlist and is selectable via "loadPluginFromName".
* This function does not actually set the plugin instance. * This function does not actually set the plugin instance.
*/ */
int addToPluginList(QString filename, QString suffix); int addToPluginList(QString filename, QString suffix);
/** /**
* Returns a QStrinListModel with the names of all seen Plugins. * Returns a QStrinListModel with the names of all seen Plugins.
*/ */
QStringListModel* getPluginList(); QStringListModel* getPluginList();
/** /**
* Loads a BioTracker Plugin from a file path. It returns true if the Plugin could be loaded, otherwise false. * Loads a BioTracker Plugin from a file path. It returns true if the Plugin could be loaded, otherwise false.
* The loaded plugin acts as singleton! * The loaded plugin acts as singleton!
*/ */
bool loadPluginFromFilename(QString const& filename); bool loadPluginFromFilename(QString const& filename);
/** /**
* Loads a plugin from it's name (given in the metadata) instead of the filename. * Loads a plugin from it's name (given in the metadata) instead of the filename.
* Only works if it is already known to the pluginloader, i.e. was loaded manually or automatically before or * Only works if it is already known to the pluginloader, i.e. was loaded manually or automatically before or
* or has been added to the pluginlist via "addToPluginList" * or has been added to the pluginlist via "addToPluginList"
*/ */
bool loadPluginFromName(QString name); bool loadPluginFromName(QString name);
QJsonObject getPluginMetaData() const; QJsonObject getPluginMetaData() const;
static std::vector<std::string> queryRegistryBehaviors(std::string path); static std::vector<std::string> queryRegistryBehaviors(std::string path);
static std::vector<std::string> searchDirectoriesForPlugins(std::vector<std::string> list, std::string suffix); static std::vector<std::string> searchDirectoriesForPlugins(std::vector<std::string> list, std::string suffix);
//return oldPath //return oldPath
static const char* addDllPath(std::string file); static const char* addDllPath(std::string file);
static void delDllPath(const char* oldPath); static void delDllPath(const char* oldPath);
// Is a plugin loaded or not? // Is a plugin loaded or not?
bool getIsPluginLoaded(); bool getIsPluginLoaded();
// Gets the name of the currently loaded plugin // Gets the name of the currently loaded plugin
QString getCurrentPluginName(); QString getCurrentPluginName();
/** /**
* Returns the Instance of the BioTracker Plugin. * Returns the Instance of the BioTracker Plugin.
*/ */
QObject *getPluginInstance(); QObject *getPluginInstance();
/** /**
* Returns a map containing the mapping "plugin name -> filename" * Returns a map containing the mapping "plugin name -> filename"
*/ */
std::map<QString, QString> const& getPluginMap() const; std::map<QString, QString> const& getPluginMap() const;
private: private:
void addPluginnameToLists(QString name, QString filename); void addPluginnameToLists(QString name, QString filename);
//a map containing the mapping "plugin name -> filename" //a map containing the mapping "plugin name -> filename"
std::map<QString, QString> m_PluginMap; std::map<QString, QString> m_PluginMap;
/** /**
* Gets the metadata from the currently loaded plugin. * Gets the metadata from the currently loaded plugin.
* Currently this is nothing but the advertised name of the plugin * Currently this is nothing but the advertised name of the plugin
*/ */
void readMetaDataFromPlugin(); void readMetaDataFromPlugin();
// The QT object to actually load the plugins // The QT object to actually load the plugins
QPluginLoader *m_PluginLoader; QPluginLoader *m_PluginLoader;
std::shared_ptr<QJsonObject> m_MetaData; std::shared_ptr<QJsonObject> m_MetaData;
//nomen est omen //nomen est omen
bool m_isPluginLoaded; bool m_isPluginLoaded;
// List of all available plugins // List of all available plugins
QStringList m_PluginList; QStringList m_PluginList;
// Entire ListModel of the metadata (actually containing all metadata, not only name) // Entire ListModel of the metadata (actually containing all metadata, not only name)
QStringListModel* m_PluginListModel; QStringListModel* m_PluginListModel;
}; };
#endif // PLUGINLOADER_H #endif // PLUGINLOADER_H
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment