summaryrefslogtreecommitdiff
path: root/src/config.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/config.cpp')
-rw-r--r--src/config.cpp157
1 files changed, 148 insertions, 9 deletions
diff --git a/src/config.cpp b/src/config.cpp
index 6405e2b..1ab2b93 100644
--- a/src/config.cpp
+++ b/src/config.cpp
@@ -1,6 +1,9 @@
#include "config.hpp"
+#include "luacxx.hpp"
+
#include <algorithm>
+#include <fcntl.h>
#include <vector>
namespace getsuyomi {
@@ -16,14 +19,27 @@ ShortcutSet shortcutSetFromList(const ShortcutList& shortcutList)
return shortcutSet;
}
+ShortcutList shortcutListFromSet(const ShortcutSet& shortcutSet)
+{
+ ShortcutList shortcutList{};
+ for (const auto& entry : shortcutSet) {
+ shortcutList.push_back(entry);
+ }
+ return shortcutList;
+}
+
Shortcuts::Shortcuts(ShortcutMap&& shortcutMap) :
m_actionToShortcuts{std::move(shortcutMap)}, m_shortcuts{}, m_shortcutToAction{}
{
for (const auto& [action, shortcuts] : m_actionToShortcuts) {
for (const auto& shortcut : shortcuts) {
qDebug("Working on action %s", actionTypeToString(action));
- if (bind(action, shortcut) != FudStatus::Success) {
+ auto bindResult = bind(action, shortcut);
+ if (bindResult != FudStatus::Success) {
+ qWarning("Error: %s", FudStatusToString(bindResult));
return;
+ } else {
+ qDebug("Bound %s to %s", qPrintable(shortcut.toString()), actionTypeToString(action));
}
}
}
@@ -40,7 +56,8 @@ bool Shortcuts::contains(ActionType actionType) const
return m_actionToShortcuts.contains(actionType);
}
-std::vector<ActionType> Shortcuts::actions() const {
+std::vector<ActionType> Shortcuts::actions() const
+{
std::vector<ActionType> actionList{};
actionList.reserve(m_actionToShortcuts.size());
for (const auto& [action, discarded] : m_actionToShortcuts) {
@@ -69,6 +86,7 @@ std::optional<ShortcutList> Shortcuts::shortcuts(ActionType action) const
}
return shortcutList;
} else {
+ qWarning("Action %s not found", actionTypeToString(action));
return std::nullopt;
}
}
@@ -122,10 +140,123 @@ fud::FudStatus Shortcuts::clear(ActionType action)
return FudStatus::Success;
}
-bool Shortcuts::valid() const {
+bool Shortcuts::valid() const
+{
return m_valid;
}
+const ShortcutMap& Shortcuts::shortcutMap() const
+{
+ return m_actionToShortcuts;
+}
+
+Shortcuts Shortcuts::fromLuaConfig(const std::filesystem::path& configFileName)
+{
+ Shortcuts shortcuts{};
+ shortcuts.m_valid = true;
+
+ LuaContext luaContext{};
+ if (!luaContext.valid()) {
+ qCritical("Failed to create lua context");
+ return shortcuts;
+ }
+
+ auto luaStatus = luaContext.loadFile(configFileName.c_str());
+ if (luaStatus != FudStatus::Success) {
+ qCritical("Failed to load file in lua %s", fud::FudStatusToString(luaStatus));
+ return shortcuts;
+ }
+
+ std::vector<ActionType> actions{
+ ActionType::OpenFile,
+ ActionType::OpenDirectory,
+ ActionType::Quit,
+ ActionType::Configure,
+ // ActionType::Help,
+ // ActionType::About
+ // ActionType::GotoFirst,
+ ActionType::Next,
+ ActionType::Back,
+ // ActionType::GotoLast,
+ ActionType::SinglePage,
+ ActionType::DualPage,
+ ActionType::MangaPage};
+
+ for (auto action : actions) {
+ auto result = luaContext.getGlobalStringArray(actionTypeToString(action));
+ if (result.isError()) {
+ qWarning(
+ "Failed to get variable %s from lua %s",
+ actionTypeToString(action),
+ fud::FudStatusToString(result.getError()));
+ // return RetType::error(result.getError());
+ continue;
+ }
+
+ auto actionBindings = result.getOkay();
+
+ for (const auto& shortcut : actionBindings) {
+ QKeySequence keySeq{shortcut.c_str()};
+ if (keySeq == Qt::Key_unknown) {
+ qCritical("Error: shortcut %s unknown", shortcut.c_str());
+ continue;
+ }
+ auto bindStatus = shortcuts.bind(action, keySeq);
+ if (bindStatus != FudStatus::Success) {
+ qCritical(
+ "Error: failure to bind to action %s keySeq %s",
+ actionTypeToString(action),
+ shortcut.c_str());
+ continue;
+ }
+ }
+ }
+
+ return shortcuts;
+}
+
+ShortcutMap Shortcuts::fromUserConfig(const std::filesystem::path& configFileName)
+{
+ constexpr mode_t configFileMode = 0600;
+ auto fdResult = open(configFileName.c_str(), O_CREAT, configFileMode);
+ if (fdResult == -1) {
+ if (errno != EEXIST) {
+ qCritical("Could not load or create user config file %s.", configFileName.c_str());
+ }
+ }
+ close(fdResult);
+
+ auto shortcuts = Shortcuts::fromLuaConfig(configFileName);
+
+ auto binder = [&](ActionType action, QKeySequence keySeq) {
+ auto bindDefaultStatus = shortcuts.bind(action, keySeq);
+ if (bindDefaultStatus != FudStatus::Success) {
+ qDebug("%s already bound", actionTypeToString(action));
+ }
+ };
+ binder(ActionType::OpenFile, QKeySequence(QKeySequence::Open));
+ binder(ActionType::OpenDirectory, QKeySequence(Qt::CTRL | Qt::ALT | Qt::Key_O));
+ binder(ActionType::Quit, QKeySequence(QKeySequence::Quit));
+
+ binder(ActionType::Configure, QKeySequence(Qt::CTRL | Qt::Key_P));
+
+ ShortcutList nextDefaults{QKeySequence{Qt::Key_L}, QKeySequence{Qt::Key_Right}, QKeySequence{Qt::Key_Down}};
+ for (const auto& binding : nextDefaults) {
+ binder(ActionType::Next, binding);
+ }
+
+ ShortcutList backDefaults{QKeySequence{Qt::Key_H}, QKeySequence{Qt::Key_Left}, QKeySequence{Qt::Key_Up}};
+ for (const auto& binding : backDefaults) {
+ binder(ActionType::Back, binding);
+ }
+
+ binder(ActionType::SinglePage, QKeySequence{Qt::Key_S});
+ binder(ActionType::DualPage, QKeySequence{Qt::Key_D});
+ binder(ActionType::MangaPage, QKeySequence{Qt::Key_M});
+
+ return shortcuts.shortcutMap();
+}
+
ShortcutMap GetsuyomiConfig::shortcuts() const
{
ShortcutMap shortcuts{};
@@ -133,10 +264,16 @@ ShortcutMap GetsuyomiConfig::shortcuts() const
shortcuts.insert({ActionType::OpenFile, shortcutSetFromList(openFileShortcuts)});
shortcuts.insert({ActionType::OpenDirectory, shortcutSetFromList(openDirectoryShortcuts)});
shortcuts.insert({ActionType::Quit, shortcutSetFromList(quitShortcuts)});
+
shortcuts.insert({ActionType::Configure, shortcutSetFromList(settingsShortcuts)});
+
shortcuts.insert({ActionType::Next, shortcutSetFromList(nextShortcuts)});
shortcuts.insert({ActionType::Back, shortcutSetFromList(backShortcuts)});
+ shortcuts.insert({ActionType::SinglePage, shortcutSetFromList(singlePageShortcuts)});
+ shortcuts.insert({ActionType::DualPage, shortcutSetFromList(dualPageShortcuts)});
+ shortcuts.insert({ActionType::MangaPage, shortcutSetFromList(mangaPageShortcuts)});
+
return shortcuts;
}
@@ -172,13 +309,13 @@ ShortcutCollector::ShortcutCollector(QWidget* parent, ActionType action, Shortcu
setLayout(m_layout);
}
-void ShortcutCollector::removeShortcut(int row, int) {
+void ShortcutCollector::removeShortcut(int row, int)
+{
auto keySequence = m_actionList[row];
auto result = m_shortcuts.remove(keySequence);
if (result == FudStatus::Success) {
m_actionList.removeAt(row);
- }
- else {
+ } else {
qCritical("What");
}
}
@@ -193,18 +330,20 @@ Settings::Settings(QWidget* parent, Shortcuts&& shortcuts) : QDialog{parent}, m_
}
for (const auto& action : m_shortcuts.actions()) {
- auto* collector = new ShortcutCollector(this, action, shortcuts);
+ auto* collector = new ShortcutCollector(this, action, m_shortcuts);
layout->addWidget(collector);
}
setLayout(layout);
}
-bool Settings::valid() const {
+bool Settings::valid() const
+{
return m_shortcuts.valid();
}
-const Shortcuts& Settings::shortcuts() const {
+const Shortcuts& Settings::shortcuts() const
+{
return m_shortcuts;
}