From 860d0cc17dfcc5d16bd754b0aa016c3217173785 Mon Sep 17 00:00:00 2001 From: Melroy van den Berg Date: Mon, 1 Mar 2021 22:28:58 +0100 Subject: [PATCH] Fix icon loading, show error when ipfs is not found and add plantuml example. --- plantuml.md | 6 ++++ src/ipfs.cc | 46 +++++++++++++----------------- src/main.cc | 6 +--- src/mainwindow.cc | 72 +++++++++++++++++++++++++++++++++-------------- src/mainwindow.h | 2 ++ 5 files changed, 80 insertions(+), 52 deletions(-) create mode 100644 plantuml.md diff --git a/plantuml.md b/plantuml.md new file mode 100644 index 0000000..22fbb26 --- /dev/null +++ b/plantuml.md @@ -0,0 +1,6 @@ +# PlantUML + +```plantuml +Bob -> Alice : hello +Alice -> Bob : ok +``` diff --git a/src/ipfs.cc b/src/ipfs.cc index d2fe1bb..a2e0e84 100644 --- a/src/ipfs.cc +++ b/src/ipfs.cc @@ -18,36 +18,30 @@ int IPFS::startIPFSDaemon() { // Be sure to kill any running daemons int res = std::system("killall -q ipfs"); - if (res != 0) { + if (res != 0) + { // ignore } - /// open /dev/null for writing - int fd = open("/dev/null", O_WRONLY); - - dup2(fd, 1); // make stdout a copy of fd (> /dev/null) - dup2(fd, 2); // ..and same with stderr - close(fd); // close fd - - // stdout and stderr now write to /dev/null // Ready to call exec to start IPFS Daemon std::string currentPath = n_fs::current_path().string(); std::string executable = currentPath.append("/../../go-ipfs/ipfs"); - const char *exe = executable.c_str(); - std::cout << "Info: Starting IPFS Daemon from: " << exe << std::endl; - char *proc[] = {strdup(exe), strdup("daemon"), strdup("--init"), strdup("--migrate"), NULL}; - return execv(exe, proc); -} + if (n_fs::exists(executable)) + { + /// open /dev/null for writing + int fd = open("/dev/null", O_WRONLY); + dup2(fd, 1); // make stdout a copy of fd (> /dev/null) + dup2(fd, 2); // ..and same with stderr + close(fd); // close fd + // stdout and stderr now write to /dev/null -/* -std::string IPFS::getExecutablePath() { - char buff[PATH_MAX]; - ssize_t len = ::readlink("/proc/self/exe", buff, sizeof(buff)-1); - if (len != -1) { - buff[len] = '\0'; - return std::string(buff); - } - else { - return ""; - } -}*/ + const char *exe = executable.c_str(); + char *proc[] = {strdup(exe), strdup("daemon"), strdup("--init"), strdup("--migrate"), NULL}; + return execv(exe, proc); + } + else + { + std::cerr << "Error: IPFS Daemon is not found. IPFS will not work!" << std::endl; + return -1; + } +} diff --git a/src/main.cc b/src/main.cc index 8bcd49e..1c8e19d 100644 --- a/src/main.cc +++ b/src/main.cc @@ -31,11 +31,7 @@ int main(int argc, char *argv[]) { // Run by child process printf("INFO: Starting IPFS daemon.\n"); - int res = IPFS::startIPFSDaemon(); - if (res != 0) { - printf("ERROR: IPFS daemon failed to start.\n"); - } - return res; + return IPFS::startIPFSDaemon(); } else if (child_pid > 0) { diff --git a/src/mainwindow.cc b/src/mainwindow.cc index e5bd2c9..3d166a5 100644 --- a/src/mainwindow.cc +++ b/src/mainwindow.cc @@ -1,17 +1,18 @@ #include "mainwindow.h" +#include "md-parser.h" +#include "menu.h" #include #include -#include +#include +#include +#include #include #include #include #include #include -#include "md-parser.h" -#include "menu.h" - MainWindow::MainWindow() : accelGroup(Gtk::AccelGroup::create()), m_menu(accelGroup), @@ -21,6 +22,7 @@ MainWindow::MainWindow() m_hboxToolbar(Gtk::ORIENTATION_HORIZONTAL, 0), m_hboxBottom(Gtk::ORIENTATION_HORIZONTAL, 0), m_appName("DWeb Browser"), + m_iconTheme("flat"), // filled or flat m_requestThread(nullptr), requestPath(""), finalRequestPath(""), @@ -76,60 +78,61 @@ MainWindow::MainWindow() m_numberedListButton.signal_clicked().connect(sigc::mem_fun(m_draw_main, &Draw::insert_numbered_list)); m_highlightButton.signal_clicked().connect(sigc::mem_fun(m_draw_main, &Draw::make_highlight)); - // Add icons to the editor buttons - try { + try + { + // Add icons to the editor buttons int iconSize = 16; - std::string iconTheme = "flat"; // filled or flat - m_boldIcon.set(Gdk::Pixbuf::create_from_file("../../images/icons/" + iconTheme + "/bold.svg", iconSize, iconSize)); + m_boldIcon.set(Gdk::Pixbuf::create_from_file(this->getIconImage("bold.svg"), iconSize, iconSize)); m_boldButton.set_tooltip_text("Add bold text"); m_boldButton.add(m_boldIcon); m_boldButton.set_relief(Gtk::RELIEF_NONE); - m_italicIcon.set(Gdk::Pixbuf::create_from_file("../../images/icons/" + iconTheme + "/italic.svg", iconSize, iconSize)); + m_italicIcon.set(Gdk::Pixbuf::create_from_file(this->getIconImage("italic.svg"), iconSize, iconSize)); m_italicButton.set_tooltip_text("Add italic text"); m_italicButton.add(m_italicIcon); m_italicButton.set_relief(Gtk::RELIEF_NONE); - m_strikethroughIcon.set(Gdk::Pixbuf::create_from_file("../../images/icons/" + iconTheme + "/strikethrough.svg", iconSize, iconSize)); + m_strikethroughIcon.set(Gdk::Pixbuf::create_from_file(this->getIconImage("strikethrough.svg"), iconSize, iconSize)); m_strikethroughButton.set_tooltip_text("Add strikethrough text"); m_strikethroughButton.add(m_strikethroughIcon); m_strikethroughButton.set_relief(Gtk::RELIEF_NONE); - m_superIcon.set(Gdk::Pixbuf::create_from_file("../../images/icons/" + iconTheme + "/superscript.svg", iconSize, iconSize)); + m_superIcon.set(Gdk::Pixbuf::create_from_file(this->getIconImage("superscript.svg"), iconSize, iconSize)); m_superButton.set_tooltip_text("Add superscript text"); m_superButton.add(m_superIcon); m_superButton.set_relief(Gtk::RELIEF_NONE); - m_subIcon.set(Gdk::Pixbuf::create_from_file("../../images/icons/" + iconTheme + "/subscript.svg", iconSize, iconSize)); + m_subIcon.set(Gdk::Pixbuf::create_from_file(this->getIconImage("subscript.svg"), iconSize, iconSize)); m_subButton.set_tooltip_text("Add subscript text"); m_subButton.add(m_subIcon); m_subButton.set_relief(Gtk::RELIEF_NONE); - m_quoteIcon.set(Gdk::Pixbuf::create_from_file("../../images/icons/" + iconTheme + "/quote.svg", iconSize, iconSize)); + m_quoteIcon.set(Gdk::Pixbuf::create_from_file(this->getIconImage("quote.svg"), iconSize, iconSize)); m_quoteButton.set_tooltip_text("Insert a quote"); m_quoteButton.add(m_quoteIcon); m_quoteButton.set_relief(Gtk::RELIEF_NONE); - m_codeIcon.set(Gdk::Pixbuf::create_from_file("../../images/icons/" + iconTheme + "/code.svg", iconSize, iconSize)); + m_codeIcon.set(Gdk::Pixbuf::create_from_file(this->getIconImage("code.svg"), iconSize, iconSize)); m_codeButton.set_tooltip_text("Insert code"); m_codeButton.add(m_codeIcon); m_codeButton.set_relief(Gtk::RELIEF_NONE); - m_linkIcon.set(Gdk::Pixbuf::create_from_file("../../images/icons/" + iconTheme + "/link.svg", iconSize, iconSize)); + m_linkIcon.set(Gdk::Pixbuf::create_from_file(this->getIconImage("link.svg"), iconSize, iconSize)); m_linkButton.set_tooltip_text("Add a link"); m_linkButton.add(m_linkIcon); m_linkButton.set_relief(Gtk::RELIEF_NONE); - m_imageIcon.set(Gdk::Pixbuf::create_from_file("../../images/icons/" + iconTheme + "/shapes.svg", iconSize, iconSize)); + m_imageIcon.set(Gdk::Pixbuf::create_from_file(this->getIconImage("shapes.svg"), iconSize, iconSize)); m_imageButton.set_tooltip_text("Add a image"); m_imageButton.add(m_imageIcon); m_imageButton.set_relief(Gtk::RELIEF_NONE); - m_bulletListIcon.set(Gdk::Pixbuf::create_from_file("../../images/icons/" + iconTheme + "/bullet_list.svg", iconSize, iconSize)); + m_bulletListIcon.set(Gdk::Pixbuf::create_from_file(this->getIconImage("bullet_list.svg"), iconSize, iconSize)); m_bulletListButton.set_tooltip_text("Add a bullet list"); m_bulletListButton.add(m_bulletListIcon); m_bulletListButton.set_relief(Gtk::RELIEF_NONE); - m_numberedListIcon.set(Gdk::Pixbuf::create_from_file("../../images/icons/" + iconTheme + "/number_list.svg", iconSize, iconSize)); + m_numberedListIcon.set(Gdk::Pixbuf::create_from_file(this->getIconImage("number_list.svg"), iconSize, iconSize)); m_numberedListButton.set_tooltip_text("Add a numbered list"); m_numberedListButton.add(m_numberedListIcon); m_numberedListButton.set_relief(Gtk::RELIEF_NONE); - m_hightlightIcon.set(Gdk::Pixbuf::create_from_file("../../images/icons/" + iconTheme + "/highlighter.svg", iconSize, iconSize)); + m_hightlightIcon.set(Gdk::Pixbuf::create_from_file(this->getIconImage("highlighter.svg"), iconSize, iconSize)); m_highlightButton.set_tooltip_text("Add highlight text"); m_highlightButton.add(m_hightlightIcon); m_highlightButton.set_relief(Gtk::RELIEF_NONE); } - catch(const Glib::FileError &e) { + catch (const Glib::FileError &e) + { std::cerr << "Error: Icons could not be loaded: " << e.what() << std::endl; } @@ -385,7 +388,7 @@ void MainWindow::enableEdit() // Disable "view source" menu item this->m_draw_main.setViewSourceMenuItem(false); // Connect changed signal - this->textChangedSignalHandler = m_draw_main.get_buffer().get()->signal_changed().connect(sigc::mem_fun(this, &MainWindow::editor_changed_text)); + this->textChangedSignalHandler = m_draw_main.get_buffer().get()->signal_changed().connect(sigc::mem_fun(this, &MainWindow::editor_changed_text)); // Set new title set_title("Untitled * - " + m_appName); } @@ -509,6 +512,33 @@ void MainWindow::openFromDisk() } } +std::string MainWindow::getIconImage(const std::string &iconFilename) +{ + // Try absolute path first + for (std::string data_dir : Glib::get_system_data_dirs()) + { + std::vector path_builder{data_dir, "dweb-browser", "images", "icons", m_iconTheme, iconFilename}; + std::string file_path = Glib::build_path(G_DIR_SEPARATOR_S, path_builder); + if (Glib::file_test(file_path, Glib::FileTest::FILE_TEST_IS_REGULAR)) + { + return file_path; + } + } + + // Try local path if the images are not installed (yet) + // When working directory is in the build/bin folder (relative path) + std::string file_path = Glib::build_filename("../../images/icons", m_iconTheme, iconFilename); + std::cout << file_path << std::endl; + if (Glib::file_test(file_path, Glib::FileTest::FILE_TEST_IS_REGULAR)) + { + return file_path; + } + else + { + return ""; + } +} + void MainWindow::editor_changed_text() { // Retrieve text from text editor diff --git a/src/mainwindow.h b/src/mainwindow.h index 9b2d8e7..192c894 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -100,6 +100,7 @@ protected: private: std::string m_appName; + std::string m_iconTheme; File m_file; std::thread *m_requestThread; std::string requestPath; @@ -115,6 +116,7 @@ private: void processRequest(const std::string &path); void fetchFromIPFS(); void openFromDisk(); + std::string getIconImage(const std::string &iconFilename); }; #endif