diff --git a/README.adoc b/README.adoc index d88f776..a1477f7 100644 --- a/README.adoc +++ b/README.adoc @@ -11,6 +11,7 @@ :uri-clang: https://clang.llvm.org/ :uri-cmake: https://cmake.org/ :uri-mastodonpp: https://schlomp.space/tastytea/mastodonpp +:uri-nlohmann-json: https://github.com/nlohmann/json :uri-doxygen: http://www.doxygen.nl/ :uri-catch: https://github.com/catchorg/Catch2 :uri-dpkg: https://packages.qa.debian.org/dpkg @@ -75,6 +76,7 @@ link:{uri-clang}[clang] 6/7) * link:{uri-cmake}[CMake] (at least: 3.9) * link:{uri-mastodonpp}[mastodonpp] (at least: 0.5) +* link:{uri-nlohmann-json}[nlohmann-json] (tested: 3.6) * link:{uri-qt}[Qt] (tested: 5.13) * Optional ** Library documentation: link:{uri-doxygen}[Doxygen] (tested: 1.8) diff --git a/cmake/FindFilesystem.cmake b/cmake/FindFilesystem.cmake new file mode 100644 index 0000000..e71dd37 --- /dev/null +++ b/cmake/FindFilesystem.cmake @@ -0,0 +1,227 @@ +# Distributed under the OSI-approved BSD 3-Clause License. See accompanying +# file Copyright.txt or https://cmake.org/licensing for details. + +#[=======================================================================[.rst: + +FindFilesystem +############## + +This module supports the C++17 standard library's filesystem utilities. Use the +:imp-target:`std::filesystem` imported target to + +Options +******* + +The ``COMPONENTS`` argument to this module supports the following values: + +.. find-component:: Experimental + :name: fs.Experimental + + Allows the module to find the "experimental" Filesystem TS version of the + Filesystem library. This is the library that should be used with the + ``std::experimental::filesystem`` namespace. + +.. find-component:: Final + :name: fs.Final + + Finds the final C++17 standard version of the filesystem library. + +If no components are provided, behaves as if the +:find-component:`fs.Final` component was specified. + +If both :find-component:`fs.Experimental` and :find-component:`fs.Final` are +provided, first looks for ``Final``, and falls back to ``Experimental`` in case +of failure. If ``Final`` is found, :imp-target:`std::filesystem` and all +:ref:`variables ` will refer to the ``Final`` version. + + +Imported Targets +**************** + +.. imp-target:: std::filesystem + + The ``std::filesystem`` imported target is defined when any requested + version of the C++ filesystem library has been found, whether it is + *Experimental* or *Final*. + + If no version of the filesystem library is available, this target will not + be defined. + + .. note:: + This target has ``cxx_std_17`` as an ``INTERFACE`` + :ref:`compile language standard feature `. Linking + to this target will automatically enable C++17 if no later standard + version is already required on the linking target. + + +.. _fs.variables: + +Variables +********* + +.. variable:: CXX_FILESYSTEM_IS_EXPERIMENTAL + + Set to ``TRUE`` when the :find-component:`fs.Experimental` version of C++ + filesystem library was found, otherwise ``FALSE``. + +.. variable:: CXX_FILESYSTEM_HAVE_FS + + Set to ``TRUE`` when a filesystem header was found. + +.. variable:: CXX_FILESYSTEM_HEADER + + Set to either ``filesystem`` or ``experimental/filesystem`` depending on + whether :find-component:`fs.Final` or :find-component:`fs.Experimental` was + found. + +.. variable:: CXX_FILESYSTEM_NAMESPACE + + Set to either ``std::filesystem`` or ``std::experimental::filesystem`` + depending on whether :find-component:`fs.Final` or + :find-component:`fs.Experimental` was found. + + +Examples +******** + +Using `find_package(Filesystem)` with no component arguments: + +.. code-block:: cmake + + find_package(Filesystem REQUIRED) + + add_executable(my-program main.cpp) + target_link_libraries(my-program PRIVATE std::filesystem) + + +#]=======================================================================] + + +if(TARGET std::filesystem) + # This module has already been processed. Don't do it again. + return() +endif() + +include(CMakePushCheckState) +include(CheckIncludeFileCXX) +include(CheckCXXSourceCompiles) + +cmake_push_check_state() + +set(CMAKE_REQUIRED_QUIET ${Filesystem_FIND_QUIETLY}) + +# All of our tests required C++17 or later +set(CMAKE_CXX_STANDARD 17) + +# Normalize and check the component list we were given +set(want_components ${Filesystem_FIND_COMPONENTS}) +if(Filesystem_FIND_COMPONENTS STREQUAL "") + set(want_components Final) +endif() + +# Warn on any unrecognized components +set(extra_components ${want_components}) +list(REMOVE_ITEM extra_components Final Experimental) +foreach(component IN LISTS extra_components) + message(WARNING "Extraneous find_package component for Filesystem: ${component}") +endforeach() + +# Detect which of Experimental and Final we should look for +set(find_experimental TRUE) +set(find_final TRUE) +if(NOT "Final" IN_LIST want_components) + set(find_final FALSE) +endif() +if(NOT "Experimental" IN_LIST want_components) + set(find_experimental FALSE) +endif() + +if(find_final) + check_include_file_cxx("filesystem" _CXX_FILESYSTEM_HAVE_HEADER) + mark_as_advanced(_CXX_FILESYSTEM_HAVE_HEADER) + if(_CXX_FILESYSTEM_HAVE_HEADER) + # We found the non-experimental header. Don't bother looking for the + # experimental one. + set(find_experimental FALSE) + endif() +else() + set(_CXX_FILESYSTEM_HAVE_HEADER FALSE) +endif() + +if(find_experimental) + check_include_file_cxx("experimental/filesystem" _CXX_FILESYSTEM_HAVE_EXPERIMENTAL_HEADER) + mark_as_advanced(_CXX_FILESYSTEM_HAVE_EXPERIMENTAL_HEADER) +else() + set(_CXX_FILESYSTEM_HAVE_EXPERIMENTAL_HEADER FALSE) +endif() + +if(_CXX_FILESYSTEM_HAVE_HEADER) + set(_have_fs TRUE) + set(_fs_header filesystem) + set(_fs_namespace std::filesystem) +elseif(_CXX_FILESYSTEM_HAVE_EXPERIMENTAL_HEADER) + set(_have_fs TRUE) + set(_fs_header experimental/filesystem) + set(_fs_namespace std::experimental::filesystem) +else() + set(_have_fs FALSE) +endif() + +set(CXX_FILESYSTEM_HAVE_FS ${_have_fs} CACHE BOOL "TRUE if we have the C++ filesystem headers") +set(CXX_FILESYSTEM_HEADER ${_fs_header} CACHE STRING "The header that should be included to obtain the filesystem APIs") +set(CXX_FILESYSTEM_NAMESPACE ${_fs_namespace} CACHE STRING "The C++ namespace that contains the filesystem APIs") + +set(_found FALSE) + +if(CXX_FILESYSTEM_HAVE_FS) + # We have some filesystem library available. Do link checks + string(CONFIGURE [[ + #include <@CXX_FILESYSTEM_HEADER@> + + int main() { + auto cwd = @CXX_FILESYSTEM_NAMESPACE@::current_path(); + return static_cast(cwd.string().size()); + } + ]] code @ONLY) + + # Try to compile a simple filesystem program without any linker flags + check_cxx_source_compiles("${code}" CXX_FILESYSTEM_NO_LINK_NEEDED) + + set(can_link ${CXX_FILESYSTEM_NO_LINK_NEEDED}) + + if(NOT CXX_FILESYSTEM_NO_LINK_NEEDED) + set(prev_libraries ${CMAKE_REQUIRED_LIBRARIES}) + # Add the libstdc++ flag + set(CMAKE_REQUIRED_LIBRARIES ${prev_libraries} -lstdc++fs) + check_cxx_source_compiles("${code}" CXX_FILESYSTEM_STDCPPFS_NEEDED) + set(can_link ${CXX_FILESYSTEM_STDCPPFS_NEEDED}) + if(NOT CXX_FILESYSTEM_STDCPPFS_NEEDED) + # Try the libc++ flag + set(CMAKE_REQUIRED_LIBRARIES ${prev_libraries} -lc++fs) + check_cxx_source_compiles("${code}" CXX_FILESYSTEM_CPPFS_NEEDED) + set(can_link ${CXX_FILESYSTEM_CPPFS_NEEDED}) + endif() + endif() + + if(can_link) + add_library(std::filesystem INTERFACE IMPORTED) + target_compile_features(std::filesystem INTERFACE cxx_std_17) + set(_found TRUE) + + if(CXX_FILESYSTEM_NO_LINK_NEEDED) + # Nothing to add... + elseif(CXX_FILESYSTEM_STDCPPFS_NEEDED) + target_link_libraries(std::filesystem INTERFACE -lstdc++fs) + elseif(CXX_FILESYSTEM_CPPFS_NEEDED) + target_link_libraries(std::filesystem INTERFACE -lc++fs) + endif() + endif() +endif() + +cmake_pop_check_state() + +set(Filesystem_FOUND ${_found} CACHE BOOL "TRUE if we can compile and link a program using std::filesystem" FORCE) + +if(Filesystem_FIND_REQUIRED AND NOT Filesystem_FOUND) + message(FATAL_ERROR "Cannot Compile simple program using std::filesystem") +endif() diff --git a/gui/src/window_main.cpp b/gui/src/window_main.cpp index b8e73a0..fa75d96 100644 --- a/gui/src/window_main.cpp +++ b/gui/src/window_main.cpp @@ -18,6 +18,7 @@ #include "channel.hpp" #include "dialog_about.hpp" #include "widget_post.hpp" +#include "fedipotato.hpp" #include #include @@ -114,6 +115,8 @@ void MainWindow::add_account() QFont font_channel{treeview_channel->font()}; font_channel.setPointSize(font_channel.pointSize() + 2); treeview_channel->setFont(font_channel); + + FediPotato acc("FediPotato"); } void MainWindow::show_about() diff --git a/lib/include/config.hpp b/lib/include/config.hpp new file mode 100644 index 0000000..5c8d11d --- /dev/null +++ b/lib/include/config.hpp @@ -0,0 +1,99 @@ +/* This file is part of FediPotato. + * Copyright © 2020 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#ifndef FEDIPOTATO_CONFIG_HPP +#define FEDIPOTATO_CONFIG_HPP + +#include + +#if __has_include() +# include +#else +# include +#endif +#include +#include + +namespace FediPotato +{ + +using json = nlohmann::json; +namespace fs = std::filesystem; +using std::string; +using std::string_view; + +/*! + * @brief Read and write the configuration. + * + * @since 0.1.0 + * + * @headerfile config.hpp FediPotato/config.hpp + */ +class Config +{ +public: + /*! + * @brief Construct a Config object. + * + * @param application_name The name of your application. + * @param filename The filename to open. + * + * @since 0.1.0 + */ + explicit Config(string_view application_name, string_view filename); + + //! Copy constructor + Config(const Config &other) = default; + + //! Move constructor + Config(Config &&other) noexcept = default; + + //! Destructor + virtual ~Config() noexcept = default; + + //! Copy assignment operator + Config& operator=(const Config &other) = delete; + + //! Move assignment operator + Config& operator=(Config &&other) noexcept = delete; + +public: + /*! + * @brief Read and parse the configuration file. + * + * @since 0.1.0 + */ + void read(); + + /*! + * @brief Set a configuration setting. + * + * @param key The key of the setting. + * @param value The value of the setting. + * + * @since 0.1.0 + */ + void set(string_view key, string_view value); + +private: + const string _filename; + const fs::path _filepath; + json _configfile; +}; + +} // namespace FediPotato + +#endif // FEDIPOTATO_CONFIG_HPP diff --git a/lib/include/directories.hpp b/lib/include/directories.hpp new file mode 100644 index 0000000..dde5d9d --- /dev/null +++ b/lib/include/directories.hpp @@ -0,0 +1,89 @@ +/* This file is part of FediPotato. + * Copyright © 2020 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#ifndef FEDIPOTATO_DIRECTORIES_HPP +#define FEDIPOTATO_DIRECTORIES_HPP + +#if __has_include() +# include +#else +# include +#endif +#include +#include + +namespace FediPotato +{ + +using std::string; +using std::string_view; + + +namespace fs = std::filesystem; + +/*! + * @brief Find the proper directories. + * + * @since 0.1.0 + * + * @headerfile directories.hpp FediPotato/directories.hpp + */ +class Directories +{ +public: + /*! + * @brief Construct a Directories object. + * + * @param application_name The name of your application. Is appended to + * all returned directories. + * + * @since 0.1.0 + */ + explicit Directories(const string_view application_name) + : _application_name{application_name} + {} + + //! Copy constructor + Directories(const Directories &other) = default; + + //! Move constructor + Directories(Directories &&other) noexcept = default; + + //! Destructor + virtual ~Directories() noexcept = default; + + //! Copy assignment operator + Directories& operator=(const Directories &other) = delete; + + //! Move assignment operator + Directories& operator=(Directories &&other) noexcept = delete; + +public: + /*! + * @brief Return the directory for config files. + * + * @since 0.1.0 + */ + [[nodiscard]] + fs::path config() const; + +private: + const string _application_name; +}; + +} // namespace FediPotato + +#endif // FEDIPOTATO_DIRECTORIES_HPP diff --git a/lib/include/fedipotato.hpp b/lib/include/fedipotato.hpp index ad6aa17..858a659 100644 --- a/lib/include/fedipotato.hpp +++ b/lib/include/fedipotato.hpp @@ -17,11 +17,16 @@ #ifndef FEDIPOTATO_HPP #define FEDIPOTATO_HPP +#include "config.hpp" +#include "version.hpp" + +#include #include namespace FediPotato { +using std::string; using std::string_view; /*! @@ -34,8 +39,25 @@ using std::string_view; class FediPotato { public: - //! Default constructor - FediPotato() = default; + /*! + * @brief Construct a FediPotato object. + * + * These exception could occur: + * * `std::runtime_error` (Directory not found) + * * `std::filesystem::filesystem_error` (Could not create directory) + * * `std::ifstream::failure` (Could not read from file) + * * `nlohmann::detail::parse_error` (Could not parse file) + * + * @param application_name The name of your application. Influences the + * file paths for all the data that is stored. + * + * @since 0.1.0 + */ + explicit FediPotato(const string_view application_name) + : _application_name{application_name} + , _config{application_name, "general.json"} + { + } //! Copy constructor FediPotato(const FediPotato &other) = default; @@ -47,14 +69,14 @@ public: virtual ~FediPotato() noexcept = default; //! Copy assignment operator - FediPotato& operator=(const FediPotato &other) = default; + FediPotato& operator=(const FediPotato &other) = delete; //! Move assignment operator - FediPotato& operator=(FediPotato &&other) noexcept = default; + FediPotato& operator=(FediPotato &&other) noexcept = delete; public: /*! - * @brief Return the version. + * @brief Return the version of the library. * * @since 0.1.0 */ @@ -62,6 +84,29 @@ public: { return version; } + + /*! + * @brief Set the proxy server. + * + * Example: + * @code + * FediPotato account("example"); + * account.set_proxy("socks4a://user:password@localhost:9050"); + * @endcode + * + * @param proxy curl-compatible proxy string. See [CURLOPT_PROXY(3)] + * (https://curl.haxx.se/libcurl/c/CURLOPT_PROXY.html). + * + * @since 0.1.0 + */ + void set_proxy(const string_view proxy) + { + _config.set("proxy", proxy); + } + +private: + const string _application_name; + Config _config; }; } // namespace FediPotato diff --git a/lib/src/CMakeLists.txt b/lib/src/CMakeLists.txt index 92231cb..b34fe97 100644 --- a/lib/src/CMakeLists.txt +++ b/lib/src/CMakeLists.txt @@ -1,6 +1,8 @@ include(GNUInstallDirs) find_package(mastodonpp REQUIRED CONFIG) +find_package(Filesystem) +find_package(nlohmann_json REQUIRED CONFIG) add_library(${PROJECT_NAME}) @@ -14,7 +16,8 @@ set_target_properties(${PROJECT_NAME} PROPERTIES SOVERSION ${${PROJECT_NAME}_VERSION_MAJOR}) target_link_libraries(${PROJECT_NAME} - PRIVATE mastodonpp::mastodonpp) + PRIVATE mastodonpp::mastodonpp + PUBLIC std::filesystem nlohmann_json::nlohmann_json) install(TARGETS ${PROJECT_NAME} EXPORT "${PROJECT_NAME}Targets" diff --git a/lib/src/config.cpp b/lib/src/config.cpp new file mode 100644 index 0000000..cc7bfbe --- /dev/null +++ b/lib/src/config.cpp @@ -0,0 +1,56 @@ +/* This file is part of FediPotato. + * Copyright © 2020 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include "config.hpp" +#include "directories.hpp" + +#include + +namespace FediPotato +{ + +using std::ifstream; +using std::ofstream; + +Config::Config(const string_view application_name, const string_view filename) + : _filename{filename} + , _filepath{Directories(application_name).config() /= filename} +{ + read(); +} + +void Config::read() +{ + if (!fs::exists(_filepath)) + { + ofstream out{_filepath}; + out.exceptions(ofstream::failbit | ofstream::badbit); + out << "{}"; + out.close(); + } + + ifstream file(_filepath); + // Can't use file.exceptions() because of nlohmann::json's noexceptness. + // + if (file.fail() || file.bad()) + { + throw std::ifstream::failure{ + string("Could not read ") += _filepath, std::io_errc::stream}; + } + file >> _configfile; +} + +} // namespace FediPotato diff --git a/lib/src/directories.cpp b/lib/src/directories.cpp new file mode 100644 index 0000000..3113080 --- /dev/null +++ b/lib/src/directories.cpp @@ -0,0 +1,54 @@ +/* This file is part of FediPotato. + * Copyright © 2020 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include + +#include "directories.hpp" + +namespace FediPotato +{ + +using std::runtime_error; + +fs::path Directories::config() const +{ + fs::path dir; + + char *envdir = getenv("XDG_CONFIG_HOME"); + if (envdir != nullptr) + { + dir = envdir; + } + else + { + envdir = getenv("HOME"); + if (envdir != nullptr) + { + dir = fs::path{envdir} /= ".config"; + } + else + { + throw runtime_error{"Couldn't find configuration directory."}; + } + } + + dir /= _application_name; + fs::create_directories(dir); + + return dir; +} + +} // namespace FediPotato