diff --git a/CMakeLists.txt b/CMakeLists.txt index 78341fb..d0b61ef 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,7 @@ message(STATUS "BUILD: ${CMAKE_BUILD_TYPE}") ## ## INCLUDE ## +include(GNUInstallDirs) include(CompilerWarnings) include(EnableCcache) include(ClangTidy) @@ -70,3 +71,30 @@ link_directories(${GTK3_LIBRARY_DIRS}) add_definitions(${GTK3_CFLAGS_OTHER}) target_link_libraries(${PROJECT_NAME} ${GTK3_LIBRARIES} fmt::fmt) + +install( + TARGETS ${PROJECT_NAME} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} +) + +install( + FILES ${CMAKE_SOURCE_DIR}/cachyos-hello.desktop + DESTINATION ${CMAKE_INSTALL_DATADIR}/applications +) + +install( + DIRECTORY ${CMAKE_SOURCE_DIR}/data + DESTINATION ${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME} +) + +install( + DIRECTORY ${CMAKE_SOURCE_DIR}/ui + DESTINATION ${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME} +) + + +# uninstall +add_custom_target(uninstall + COMMAND cat ${PROJECT_BINARY_DIR}/install_manifest.txt | xargs rm + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} +) diff --git a/meson.build b/meson.build index 7706e54..a0f649a 100644 --- a/meson.build +++ b/meson.build @@ -68,6 +68,13 @@ executable( include_directories: [include_directories('src')], install: true) +install_data ( + meson.project_name () + '.desktop', + install_dir: join_paths(get_option('datadir'), 'applications') +) + +meson.add_install_script('postinstall.sh') + summary( { 'Build type': get_option('buildtype'), diff --git a/po/manjaro-hello.pot b/po/cachyos-hello.pot similarity index 100% rename from po/manjaro-hello.pot rename to po/cachyos-hello.pot diff --git a/postinstall.sh b/postinstall.sh new file mode 100755 index 0000000..c5dd731 --- /dev/null +++ b/postinstall.sh @@ -0,0 +1,12 @@ +#!/bin/sh + +mkdir -p "${DESTDIR}/${MESON_INSTALL_PREFIX}/share/cachyos-hello/" +cp -r "${MESON_SOURCE_ROOT}/data" "${DESTDIR}/${MESON_INSTALL_PREFIX}/share/cachyos-hello/" +cp -r "${MESON_SOURCE_ROOT}/ui" "${DESTDIR}/${MESON_INSTALL_PREFIX}/share/cachyos-hello/" + +cd "${MESON_SOURCE_ROOT}/po" +for lang in $(ls *.po); do + lang=${lang::-3} + mkdir -p ${DESTDIR}/usr/share/locale/${lang//_/-}/LC_MESSAGES + msgfmt -c -o ${DESTDIR}/usr/share/locale/${lang//_/-}/LC_MESSAGES/cachyos-hello.mo $lang.po +done diff --git a/src/hello.cpp b/src/hello.cpp index f532d67..668eb3e 100644 --- a/src/hello.cpp +++ b/src/hello.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -25,8 +26,12 @@ std::string fix_path(std::string&& path) noexcept { } nlohmann::json read_json(const std::string_view& path) { + const auto& buf = fix_path(path.data()); + if (!fs::exists(buf)) { + throw std::runtime_error(fmt::format("File does not exist: \"{}\"", buf)); + } // read a JSON file - std::ifstream i(fix_path(path.data())); + std::ifstream i(buf); nlohmann::json j; i >> j; @@ -63,6 +68,22 @@ std::array get_lsb_infos() { return {lsb["ID"], lsb["RELEASE"]}; } +void child_watch_cb(GPid pid, gint status, gpointer /*user_data*/) { +#if !defined(NDEBUG) + g_message("Child %" G_PID_FORMAT " exited %s", pid, + g_spawn_check_wait_status(status, nullptr) ? "normally" : "abnormally"); +#endif + + // Free any resources associated with the child here, such as I/O channels + // on its stdout and stderr FDs. If you have no code to put in the + // child_watch_cb() callback, you can remove it and the g_child_watch_add() + // call, but you must also remove the G_SPAWN_DO_NOT_REAP_CHILD flag, + // otherwise the child process will stay around as a zombie until this + // process exits. + + g_spawn_close_pid(pid); +} + void quick_message(Gtk::Window* parent, const std::string& message) { // Create the widgets const auto& flags = static_cast(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT); @@ -82,12 +103,27 @@ void quick_message(Gtk::Window* parent, const std::string& message) { gtk_widget_show_all(dialog); int result = gtk_dialog_run(GTK_DIALOG(dialog)); + std::vector argv{}; if (result == GTK_RESPONSE_NO) { - fmt::print("Offline\n"); + argv = {fix_path("~/.local/bin/calamares-offline.sh")}; } else { - fmt::print("Online\n"); + argv = {fix_path("~/.local/bin/calamares-online.sh")}; } + int child_stdout{}; + int child_stderr{}; + Glib::Pid child_pid; + + // Spawn child process. + try { + Glib::spawn_async_with_pipes(".", argv, Glib::SpawnFlags::SPAWN_DO_NOT_REAP_CHILD, Glib::SlotSpawnChildSetup(), &child_pid, nullptr, &child_stdout, &child_stderr); + } catch (Glib::Error& error) { + g_critical("%s", error.what().c_str()); + } + // Add a child watch function which will be called when the child process + // exits. + g_child_watch_add(child_pid, child_watch_cb, nullptr); + gtk_widget_destroy(dialog); } } // namespace @@ -221,7 +257,7 @@ Hello::Hello(int argc, char** argv) { languages->set_active_id(get_best_locale()); // Set autostart switcher state - m_autostart = fs::is_regular_file(fix_path(m_preferences["autostart_path"])); + m_autostart = fs::exists(fix_path(m_preferences["autostart_path"])); Gtk::Switch* autostart_switch; m_builder->get_widget("autostart", autostart_switch); autostart_switch->set_active(m_autostart); @@ -269,11 +305,13 @@ auto Hello::get_best_locale() const noexcept -> std::string { /// Sets locale of ui and pages. void Hello::set_locale(const std::string_view& use_locale) noexcept { +#if !defined(NDEBUG) fmt::print( "┌{0:─^{2}}┐\n" "│{1: ^{2}}│\n" "└{0:─^{2}}┘\n", "", fmt::format("Locale changed to {}", use_locale), 40); +#endif textdomain(m_app); Glib::setenv("LANGUAGE", use_locale.data()); diff --git a/src/helper.hpp b/src/helper.hpp index 6014372..bca67de 100644 --- a/src/helper.hpp +++ b/src/helper.hpp @@ -6,8 +6,6 @@ #include #include -#include - inline std::pair tokenize(std::string& str, const std::string_view& delim) { std::size_t start{}; std::size_t end = str.find(delim.data());