From 2a45f57386480d9369108fce388692e342015bc6 Mon Sep 17 00:00:00 2001 From: Felix Geyer Date: Wed, 18 Jul 2012 20:54:26 +0200 Subject: [PATCH] Support proper plugin finding. So we don't have to hardcode the location anymore. --- src/autotype/AutoType.cpp | 25 ++++++++++++++++--------- src/autotype/AutoType.h | 1 + src/core/DatabaseIcons.cpp | 2 +- src/core/FilePath.cpp | 29 +++++++++++++++++++++++++++++ src/core/FilePath.h | 1 + src/core/Tools.cpp | 13 +++++++++++++ src/core/Tools.h | 1 + 7 files changed, 62 insertions(+), 10 deletions(-) diff --git a/src/autotype/AutoType.cpp b/src/autotype/AutoType.cpp index b60629441..e704aa296 100644 --- a/src/autotype/AutoType.cpp +++ b/src/autotype/AutoType.cpp @@ -23,6 +23,7 @@ #include "autotype/AutoTypePlatformPlugin.h" #include "core/Database.h" #include "core/Entry.h" +#include "core/FilePath.h" #include "core/Group.h" #include "core/ListDeleter.h" #include "core/Tools.h" @@ -39,10 +40,21 @@ AutoType::AutoType(QObject* parent) // prevent crash when the plugin has unresolved symbols m_pluginLoader->setLoadHints(QLibrary::ResolveAllSymbolsHint); - // TODO: scan in proper paths -#if defined(Q_OS_UNIX) && !defined(Q_OS_MAC) - m_pluginLoader->setFileName(QCoreApplication::applicationDirPath() + "/autotype/x11/libkeepassx-autotype-x11.so"); -#endif + QString pluginPath = filePath()->pluginPath("keepassx-autotype-" + Tools::platform()); + + if (!pluginPath.isEmpty()) { + loadPlugin(pluginPath); + } +} + +AutoType::~AutoType() +{ + delete m_executor; +} + +void AutoType::loadPlugin(const QString& pluginPath) +{ + m_pluginLoader->setFileName(pluginPath); QObject* pluginInstance = m_pluginLoader->instance(); if (pluginInstance) { @@ -58,11 +70,6 @@ AutoType::AutoType(QObject* parent) } } -AutoType::~AutoType() -{ - delete m_executor; -} - AutoType* AutoType::instance() { if (!m_instance) { diff --git a/src/autotype/AutoType.h b/src/autotype/AutoType.h index 3ca53984e..3c2d729c5 100644 --- a/src/autotype/AutoType.h +++ b/src/autotype/AutoType.h @@ -57,6 +57,7 @@ Q_SIGNALS: private: explicit AutoType(QObject* parent = Q_NULLPTR); ~AutoType(); + void loadPlugin(const QString& pluginPath); bool parseActions(const QString& sequence, const Entry* entry, QList& actions); QList createActionFromTemplate(const QString& tmpl, const Entry* entry); diff --git a/src/core/DatabaseIcons.cpp b/src/core/DatabaseIcons.cpp index 46fb4812d..a6667270a 100644 --- a/src/core/DatabaseIcons.cpp +++ b/src/core/DatabaseIcons.cpp @@ -106,7 +106,7 @@ QImage DatabaseIcons::icon(int index) } else { QString iconPath = QString("icons/database/").append(m_indexToName[index]); - QImage icon(filePath()->path(iconPath)); + QImage icon(filePath()->dataPath(iconPath)); m_iconCache[index] = icon; return icon; diff --git a/src/core/FilePath.cpp b/src/core/FilePath.cpp index 2649c704b..231d146af 100644 --- a/src/core/FilePath.cpp +++ b/src/core/FilePath.cpp @@ -19,6 +19,7 @@ #include #include +#include #include "config-keepassx.h" @@ -29,6 +30,34 @@ QString FilePath::dataPath(const QString& name) return m_basePath + name; } +QString FilePath::pluginPath(const QString& name) +{ + QStringList pluginPaths; + QDir buildDir(QCoreApplication::applicationDirPath() + "/autotype"); + Q_FOREACH (const QString& dir, buildDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) { + pluginPaths << QCoreApplication::applicationDirPath() + "/autotype/" + dir; + } + pluginPaths << QCoreApplication::applicationDirPath(); + pluginPaths << QCoreApplication::applicationDirPath() + "/../lib/keepassx"; + + QStringList dirFilter; + dirFilter << QString("*%1*").arg(name); + + Q_FOREACH (const QString& path, pluginPaths) { + QStringList fileCandidates = QDir(path).entryList(dirFilter, QDir::Files); + + Q_FOREACH (const QString& file, fileCandidates) { + QString filePath = path + "/" + file; + + if (QLibrary::isLibrary(filePath)) { + return filePath; + } + } + } + + return QString(); +} + QIcon FilePath::applicationIcon() { return icon("apps", "keepassx"); diff --git a/src/core/FilePath.h b/src/core/FilePath.h index 7af881deb..3ecb7af95 100644 --- a/src/core/FilePath.h +++ b/src/core/FilePath.h @@ -27,6 +27,7 @@ class FilePath { public: QString dataPath(const QString& name); + QString pluginPath(const QString& name); QIcon applicationIcon(); QIcon icon(const QString& category, const QString& name, bool fromTheme = true); diff --git a/src/core/Tools.cpp b/src/core/Tools.cpp index 019ff8a92..74c801754 100644 --- a/src/core/Tools.cpp +++ b/src/core/Tools.cpp @@ -181,4 +181,17 @@ void wait(int ms) } } +QString platform() +{ +#if defined(Q_WS_X11) + return "x11"; +#elif defined(Q_WS_MAC) + return "mac"; +#elif defined(Q_WS_WIN) + return "win"; +#else + return QString(); +#endif +} + } // namespace Tools diff --git a/src/core/Tools.h b/src/core/Tools.h index aacbbe12f..f696e38ce 100644 --- a/src/core/Tools.h +++ b/src/core/Tools.h @@ -37,6 +37,7 @@ QString imageReaderFilter(); bool isHex(const QByteArray& ba); void sleep(int ms); void wait(int ms); +QString platform(); } // namespace Tools