Improve app config (#237)

* Provide A Default Values method

* Fix #222 Add Defaults for gnome, kde, and lxde Screenshot tools

---------

Co-authored-by: Chris Rizzitello <crizzitello@ics.com>
main
crizzitello 2023-10-02 14:05:58 -04:00 committed by GitHub
parent a1ef9bdbe7
commit 96e8a9a290
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 76 additions and 11 deletions

View File

@ -6,6 +6,7 @@
#include <QIODevice>
#include <QJsonParseError>
#include <QJsonObject>
#include <QFile>
AppConfig::AppConfig(QObject *parent)
: QObject(parent)
@ -17,24 +18,16 @@ AppConfig::AppConfig(QObject *parent)
void AppConfig::validateConfig()
{
//Remove Any invalid Keys and set or fix any keys needed.
//Remove Any invalid Keys and set or set any empty to their default.
for (const auto &key : appConfig->allKeys()) {
if(!_appConfigValidKeys.contains(key))
appConfig->remove(key);
}
if (appConfig->value(CONFIG::EVIDENCEREPO).isNull())
appConfig->setValue(CONFIG::EVIDENCEREPO, QStringLiteral("%1/ashirt/evidence").arg(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)));
#ifdef Q_OS_MACOS
if (appConfig->value(CONFIG::COMMAND_SCREENSHOT).isNull())
appConfig->setValue(CONFIG::COMMAND_SCREENSHOT, QStringLiteral("screencapture -s %file"));
if (appConfig->value(CONFIG::COMMAND_CAPTUREWINDOW).isNull())
appConfig->setValue(CONFIG::COMMAND_CAPTUREWINDOW, QStringLiteral("screencapture -w %file"));
#endif
}
QString AppConfig::value(const QString &key)
{
return get()->appConfig->value(key, QString()).toString();
return get()->appConfig->value(key, defaultValue(key)).toString();
}
void AppConfig::setValue(const QString &key, const QString &value)
@ -100,6 +93,64 @@ void AppConfig::setLastUsedTags(QList<model::Tag> lastTags)
get()->appSettings->setValue(_lastUsedTagsSetting, QVariant::fromValue(writeTags));
}
QString AppConfig::defaultValue(const QString &key)
{
if (key.isEmpty() || key == CONFIG::ACCESSKEY || key == CONFIG::SECRETKEY )
return QString();
if (key == CONFIG::EVIDENCEREPO)
return QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).at(0).mid(0).append("/evidence");
if (key == CONFIG::APIURL)
return QStringLiteral("http://localhost:8080");
if (key == CONFIG::SHORTCUT_CAPTURECLIPBOARD)
#ifdef Q_OS_LINUX
return QStringLiteral("Meta+Alt+v");
#elif defined Q_OS_WIN
return QStringLiteral("Alt+v");
#elif defined Q_OS_MAC
return QStringLiteral("Option+v");
#endif
if(key == CONFIG::SHORTCUT_CAPTUREWINDOW)
#ifdef Q_OS_LINUX
return QStringLiteral("Meta+Alt+4");
#elif defined Q_OS_WIN
return QStringLiteral("Alt+4");
#elif defined Q_OS_MAC
return QStringLiteral("Option+shift+4");
#endif
if(key == CONFIG::SHORTCUT_SCREENSHOT)
#ifdef Q_OS_LINUX
return QStringLiteral("Meta+Alt+3");
#elif defined Q_OS_WIN
return QStringLiteral("Alt+3");
#elif defined Q_OS_MAC
return QStringLiteral("Option+shift+3");
#endif
if(key == CONFIG::COMMAND_SCREENSHOT)
#ifdef Q_OS_LINUX
return QFile::exists(gnomeSS) ? gnomeAreaCommand : QFile::exists(kdeSS) ? kdeAreaCommand : QFile::exists(xfceSS) ? xfceAreaCommand : QString();
#elif defined Q_OS_WIN
return QStringLiteral("C:\\Program Files\\IrfanView\\i_view64.exe /capture=4 convert=%file");
#elif defined Q_OS_MAC
return QStringLiteral("screencapture -s %file");
#endif
if(key == CONFIG::COMMAND_CAPTUREWINDOW)
#ifdef Q_OS_LINUX
return QFile::exists(gnomeSS) ? gnomeWindowCommand : QFile::exists(kdeSS) ? kdeWindowCommand : QFile::exists(xfceSS) ? xfceWindowCommand : QString();
#elif defined Q_OS_WIN
return QStringLiteral("C:\\Program Files\\IrfanView\\i_view64.exe /capture=0 convert=%file");
#elif defined Q_OS_MAC
return QStringLiteral("screencapture -w %file");
#endif
return QString();
}
QList<model::Tag> AppConfig::getLastUsedTags()
{
QList<model::Tag> rtn;

View File

@ -53,7 +53,8 @@ public:
static QList<model::Tag> getLastUsedTags();
/// Set the last used Tags
static void setLastUsedTags(QList<model::Tag> lastTags);
/// Return the Default Value for a given key
static QString defaultValue(const QString &key = QString());
signals:
void operationChanged(QString operationSlug, QString operationName);
@ -74,6 +75,19 @@ private:
inline static const auto _configFile = QStringLiteral("%1/ashirt/config.json").arg(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation));
#else
inline static const QString _configFile = QStringLiteral("%1/ashirt/config.json").arg(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation));
#endif
#ifdef Q_OS_LINUX
inline static const auto gnomeSS = QStringLiteral("/usr/bin/gnome-screenshot");
inline static const auto gnomeWindowCommand = QStringLiteral("gnome-screenshot -w -f %file");
inline static const auto gnomeAreaCommand = QStringLiteral("gnome-screenshot -a -f %file");
inline static const auto kdeSS = QStringLiteral("/usr/bin/spectacle");
inline static const auto kdeWindowCommand = QStringLiteral("spectacle -a -bno %file");
inline static const auto kdeAreaCommand = QStringLiteral("spectacle -r -bno %file");
inline static const auto xfceSS = QStringLiteral("/usr/bin/xfce4-screenshooter");
inline static const auto xfceWindowCommand = QStringLiteral("xfce4-screenshooter -w -s %file");
inline static const auto xfceAreaCommand = QStringLiteral("xfce4-screenshooter -r -s %file");
#endif
inline static const auto _opSlugSetting = QStringLiteral("operation/slug");
inline static const auto _opNameSetting = QStringLiteral("operation/name");