diff options
Diffstat (limited to 'src/main_window.cpp')
-rw-r--r-- | src/main_window.cpp | 159 |
1 files changed, 145 insertions, 14 deletions
diff --git a/src/main_window.cpp b/src/main_window.cpp index b05507c..5c0c401 100644 --- a/src/main_window.cpp +++ b/src/main_window.cpp @@ -1,22 +1,43 @@ #include "main_window.hpp" +#include "config.hpp" + #include <QString> +#include <filesystem> +#include <fud_result.hpp> +#include <fud_status.hpp> +#include <string> +#include <vector> #include <zip.h> namespace getsuyomi { -GetsuyomiApp::GetsuyomiApp() : m_getsuyomi{new Getsuyomi()} +using fud::FudStatus; +using GetEnvResult = fud::Result<GetsuyomiConfig, FudStatus>; + +constexpr const char* HOME{"HOME"}; + +std::optional<std::string> getEnvVar(const char* envVar); +void getEnvVar(const std::string envVar, std::string& envValue, const char* backup); +GetEnvResult getEnvironment(); + +GetsuyomiApp::GetsuyomiApp() : m_getsuyomi{new Getsuyomi()}, m_qtSettings{new QSettings(AppVendor, AppName)} { - setCentralWidget(m_getsuyomi); - setup(); } -void GetsuyomiApp::setup() +FudStatus GetsuyomiApp::setup() { + setCentralWidget(m_getsuyomi); + QCoreApplication::setApplicationName(AppName); QCoreApplication::setApplicationVersion(AppVersionString); setWindowTitle(AppName); + auto envResult = getEnvironment(); + if (envResult.isError()) { + return envResult.getError(); + } + createActions(); createMenus(); createToolBar(); @@ -25,7 +46,11 @@ void GetsuyomiApp::setup() constexpr int minimumHeight = 480; setMinimumSize(minimumWidth, minimumHeight); + readSettings(); + show(); + + return FudStatus::Success; } void GetsuyomiApp::createActions() @@ -81,18 +106,35 @@ void GetsuyomiApp::createToolBar() void GetsuyomiApp::openFile() { - auto filename = QFileDialog::getOpenFileName( + auto dialog = QFileDialog( this, tr("Open Archive"), - QDir::homePath(), + m_lastOpenedDirectory, tr("Archive types (*.zip *.cbz *.cbr *.gz)")); + dialog.setFileMode(QFileDialog::ExistingFile); + QString filename; + if (dialog.exec()) { + auto filenames = dialog.selectedFiles(); + if (filenames.length() == 0) { + qWarning("No files selected."); + return; + } else if (filenames.length() > 1) { + qWarning("Too many files selected %llu.", filenames.length()); + return; + } + filename = filenames[0]; + m_lastOpenedDirectory = dialog.directory().absolutePath(); + } else { + qWarning("File dialog did not execute"); + return; + } if (filename.endsWith(".zip")) { - try { - auto* archive = new ZipArchive(filename); - m_getsuyomi->setArchive(archive); - } catch (std::runtime_error& exc) { + auto* archive = new ZipArchive(filename); + if (!archive->valid()) { qCritical("Failed to change archive"); + } else { + m_getsuyomi->setArchive(archive); } } else { qCritical("Unsupported file extension"); @@ -101,12 +143,28 @@ void GetsuyomiApp::openFile() void GetsuyomiApp::openDirectory() { - QString directory = QFileDialog::getExistingDirectory( + auto dialog = QFileDialog( this, tr("Open Directory"), - QDir::homePath(), - QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); - qDebug("Open directory %s", qPrintable(directory)); + m_lastOpenedDirectory); + dialog.setFileMode(QFileDialog::Directory); + QString directoryName; + if (dialog.exec()) { + auto filenames = dialog.selectedFiles(); + if (filenames.length() == 0) { + qWarning("No files selected."); + return; + } else if (filenames.length() > 1) { + qWarning("Too many files selected %llu.", filenames.length()); + return; + } + directoryName = filenames[0]; + m_lastOpenedDirectory = dialog.directory().absolutePath(); + } else { + qWarning("File dialog did not execute"); + return; + } + qDebug("Opened directory %s", qPrintable(directoryName)); } void GetsuyomiApp::quit() @@ -126,4 +184,77 @@ void GetsuyomiApp::back() m_getsuyomi->back(); } +void GetsuyomiApp::closeEvent(QCloseEvent* event) +{ + QMainWindow::closeEvent(event); +} + +void GetsuyomiApp::readSettings() +{ + restoreGeometry(m_qtSettings->value("geometry").toByteArray()); + restoreState(m_qtSettings->value("windowState").toByteArray()); + m_lastOpenedDirectory = m_qtSettings->value("lastOpenedDirectory", QDir::homePath()).toString(); + qDebug("ReadSettings - last directory is %s", qPrintable(m_lastOpenedDirectory)); +} + +void GetsuyomiApp::writeSettings() +{ + m_qtSettings->setValue("geometry", saveGeometry()); + m_qtSettings->setValue("windowState", saveState()); + m_qtSettings->setValue("lastOpenedDirectory", m_lastOpenedDirectory); +} + +std::optional<std::string> 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 envVar, std::string& envValue, const char* backup) +{ + auto envValueOpt = getEnvVar(envVar.c_str()); + if (envValueOpt == std::nullopt || envValueOpt->length() == 0) { + std::filesystem::path envValuePath{HOME}; + envValuePath.append(backup); + envValue = envValuePath; + } else { + envValue = *envValueOpt; + } + 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(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(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(XdgStateHome, config.stateHome, ".local/state"); + + return GetEnvResult::okay(config); +} + } // namespace getsuyomi |