From 47e0ff88edd4660513f1d4f3d731008461532a13 Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Wed, 2 Oct 2024 15:07:24 -0500 Subject: Get and save user keybinds. --- src/main_window.cpp | 162 +++++++--------------------------------------------- 1 file changed, 22 insertions(+), 140 deletions(-) (limited to 'src/main_window.cpp') diff --git a/src/main_window.cpp b/src/main_window.cpp index 82e6ef2..52c14ef 100644 --- a/src/main_window.cpp +++ b/src/main_window.cpp @@ -1,7 +1,7 @@ #include "main_window.hpp" #include "config.hpp" -#include "luacxx.hpp" +#include "settings.hpp" #include #include @@ -10,94 +10,11 @@ #include #include #include -#include #include namespace getsuyomi { using fud::FudStatus; -using GetEnvResult = fud::Result; -using GetConfigResult = fud::Result; - -constexpr const char* HOME{"HOME"}; - -std::optional getEnvVar(const char* envVar); -void getEnvVar(const std::string& homeVar, const std::string& envVar, std::string& envValue, const char* backup); -GetEnvResult getEnvironment(); - -FudStatus createXdgDirectory(const std::string& directoryName) -{ - constexpr mode_t xdgMode = 0700; - auto dirStatus = mkdir(directoryName.c_str(), xdgMode); - if (dirStatus == 0) { - return FudStatus::Success; - } - - if (errno != EEXIST) { - return FudStatus::Failure; - } - struct stat statBuffer {}; - dirStatus = stat(directoryName.c_str(), &statBuffer); - - if (dirStatus != 0) { - return FudStatus::Failure; - } - - if ((statBuffer.st_mode & S_IFMT) != S_IFDIR) { - return FudStatus::Failure; - } - - return FudStatus::Success; -} - -void setUserConfig(GetsuyomiConfig& config, const ShortcutMap& shortcutMap) -{ - auto binder = [&](ShortcutList& shortcuts, ActionType action) { - shortcuts = shortcutListFromSet(shortcutMap.at(action)); - }; - - binder(config.openFileShortcuts, ActionType::OpenFile); - binder(config.openDirectoryShortcuts, ActionType::OpenDirectory); - binder(config.quitShortcuts, ActionType::Quit); - - binder(config.settingsShortcuts, ActionType::Configure); - - binder(config.nextShortcuts, ActionType::Next); - binder(config.backShortcuts, ActionType::Back); - - binder(config.singlePageShortcuts, ActionType::SinglePage); - binder(config.dualPageShortcuts, ActionType::DualPage); - binder(config.mangaPageShortcuts, ActionType::MangaPage); -} - -GetConfigResult getUserConfig() -{ - auto configResult = getEnvironment(); - if (configResult.isError()) { - return GetConfigResult::error(configResult.getError()); - } - auto config = configResult.getOkay(); - - auto dirStatus = createXdgDirectory(config.dataHome); - if (dirStatus != FudStatus::Success) { - return GetConfigResult::error(dirStatus); - } - dirStatus = createXdgDirectory(config.configHome); - if (dirStatus != FudStatus::Success) { - return GetConfigResult::error(dirStatus); - } - dirStatus = createXdgDirectory(config.stateHome); - if (dirStatus != FudStatus::Success) { - return GetConfigResult::error(dirStatus); - } - - auto configFileName = std::filesystem::path(config.configHome).append("config.lua"); - auto shortcutMap = Shortcuts::fromUserConfig(configFileName); - - setUserConfig(config, shortcutMap); - - return GetConfigResult::okay(config); -} GetsuyomiApp::GetsuyomiApp() : QMainWindow(nullptr), m_getsuyomi{new Getsuyomi(this)} { @@ -127,6 +44,9 @@ FudStatus GetsuyomiApp::setup() constexpr int minimumHeight = 480; setMinimumSize(minimumWidth, minimumHeight); + /* TODO: Wire up a proper status bar */ + statusBar()->addWidget(new QLabel("Status bar")); + show(); return FudStatus::Success; @@ -146,6 +66,9 @@ void GetsuyomiApp::createActions() m_settingsAction = new QAction(QIcon("resources/gear.svg"), tr("&Settings"), this); connect(m_settingsAction, &QAction::triggered, this, &GetsuyomiApp::configure); + m_aboutApp = new QAction(QIcon::fromTheme(QIcon::ThemeIcon::HelpAbout), tr("&About"), this); + connect(m_aboutApp, &QAction::triggered, this, &GetsuyomiApp::aboutApp); + m_nextAction = new QAction(QIcon::fromTheme(QIcon::ThemeIcon::GoNext), tr("Next"), this); connect(m_nextAction, &QAction::triggered, this, &GetsuyomiApp::next); @@ -207,8 +130,11 @@ void GetsuyomiApp::createMenus() m_fileMenu->addAction(m_openDirectory); m_fileMenu->addAction(m_quitAction); - m_SettingsMenu = menuBar()->addMenu(tr("&Settings")); - m_SettingsMenu->addAction(m_settingsAction); + m_settingsMenu = menuBar()->addMenu(tr("&Settings")); + m_settingsMenu->addAction(m_settingsAction); + + m_helpMenu = menuBar()->addMenu(tr("&Help")); + m_helpMenu->addAction(m_aboutApp); } void GetsuyomiApp::createToolBar() @@ -305,9 +231,19 @@ void GetsuyomiApp::configure() const auto& shortcuts = settings.shortcuts(); setUserConfig(m_config, shortcuts.shortcutMap()); bindShortcuts(); + auto saveStatus = shortcuts.save(m_config.configFilename); + if (saveStatus != FudStatus::Success) { + qWarning("Failed to save shortcuts."); + } } } +void GetsuyomiApp::aboutApp() +{ + /* TODO: proper implementation */ + QMessageBox::about(this, "About Getsuyomi", "A comic book reading app."); +} + void GetsuyomiApp::next() { m_getsuyomi->next(); @@ -366,58 +302,4 @@ void GetsuyomiApp::writeSettings() settings.sync(); } -std::optional getEnvVar(const char* envVar) -{ - const QByteArray defaultArray{}; - if (envVar == nullptr) { - return std::nullopt; - } - QByteArray varArray = qgetenv(envVar); - if (varArray == defaultArray) { - return std::nullopt; - } - return varArray.toStdString(); -} - -void getEnvVar(const std::string& homeVar, const std::string& envVar, std::string& envValue, const char* backup) -{ - auto envValueOpt = getEnvVar(envVar.c_str()); - std::filesystem::path envValuePath{homeVar}; - if (envValueOpt == std::nullopt || envValueOpt->length() == 0) { - envValuePath.append(backup); - } else { - envValuePath = *envValueOpt; - } - envValuePath.append(AppName); - envValue = envValuePath; - // qDebug("%s is %s", envVar.c_str(), envValue.c_str()); -} - -GetEnvResult getEnvironment() -{ - GetsuyomiConfig config{}; - - auto homeOpt = getEnvVar(HOME); - if (homeOpt == std::nullopt || homeOpt->length() == 0) { - qCritical("Error getting home"); - return GetEnvResult::error(FudStatus::Failure); - } - config.home = *homeOpt; - // qDebug("Home is %s", config.home.c_str()); - - /* If $XDG_DATA_HOME is either not set or empty, a default equal to $HOME/.local/share should be used. */ - const std::string XdgDataHome{"XDG_DATA_HOME"}; - getEnvVar(config.home, XdgDataHome, config.dataHome, ".local/share"); - - /* If $XDG_CONFIG_HOME is either not set or empty, a default equal to $HOME/.config should be used. */ - const std::string XdgConfigHome{"XDG_CONFIG_HOME"}; - getEnvVar(config.home, XdgConfigHome, config.configHome, ".config"); - - /* If $XDG_STATE_HOME is either not set or empty, a default equal to $HOME/.local/state should be used. */ - const std::string XdgStateHome{"XDG_STATE_HOME"}; - getEnvVar(config.home, XdgStateHome, config.stateHome, ".local/state"); - - return GetEnvResult::okay(config); -} - } // namespace getsuyomi -- cgit v1.2.3