LibreWeb-Browser/src/mainwindow.cc

145 lines
4.8 KiB
C++
Raw Normal View History

2020-11-12 14:27:47 -08:00
#include "mainwindow.h"
2020-11-30 15:43:52 -08:00
#include <gtkmm/menuitem.h>
2020-12-08 19:44:26 -08:00
#include <gtkmm/image.h>
#ifdef LEGACY_CXX
#include <experimental/filesystem>
namespace n_fs = ::std::experimental::filesystem;
#else
#include <filesystem>
namespace n_fs = ::std::filesystem;
#endif
2020-11-12 14:27:47 -08:00
2020-12-08 19:44:26 -08:00
MainWindow::MainWindow()
: m_vbox(Gtk::ORIENTATION_VERTICAL, 0),
2020-12-10 10:01:11 -08:00
m_hbox_bar(Gtk::ORIENTATION_HORIZONTAL, 0),
currentRequestPath()
2020-11-12 14:27:47 -08:00
{
2020-12-10 10:53:05 -08:00
set_title("DBrowser");
2020-11-29 20:13:48 -08:00
set_default_size(1000, 800);
2020-11-30 16:02:29 -08:00
set_position(Gtk::WIN_POS_CENTER);
2020-11-14 14:13:00 -08:00
2020-11-30 15:43:52 -08:00
// Connect signals
m_menu.quit.connect(sigc::mem_fun(this, &MainWindow::hide)); /*!< hide main window and therefor closes the app */
2020-12-10 10:01:11 -08:00
m_menu.reload.connect(sigc::mem_fun(this, &MainWindow::refresh)); /*!< Menu item for reloading the page */
2020-12-08 19:44:26 -08:00
m_menu.about.connect(sigc::mem_fun(m_about, &About::show_about)); /*!< Display about dialog */
m_about.signal_response().connect(sigc::mem_fun(m_about, &About::hide_about)); /*!< Close about dialog */
2020-12-10 10:01:11 -08:00
m_refreshButton.signal_clicked().connect(sigc::mem_fun(this, &MainWindow::refresh)); /*!< Button for reloading the page */
m_homeButton.signal_clicked().connect(sigc::mem_fun(this, &MainWindow::go_home)); /*!< Button for home page */
m_inputField.signal_activate().connect(sigc::mem_fun(this, &MainWindow::input_activate)); /*!< User pressed enter in the input */
2020-11-30 15:43:52 -08:00
m_vbox.pack_start(m_menu, false, false, 0);
2020-11-14 14:13:00 -08:00
2020-12-08 19:44:26 -08:00
// Horizontal bar
auto styleBack = m_backButton.get_style_context();
styleBack->add_class("circular");
auto styleForward = m_forwardButton.get_style_context();
styleForward->add_class("circular");
auto styleRefresh = m_refreshButton.get_style_context();
styleRefresh->add_class("circular");
m_backButton.set_relief(Gtk::RELIEF_NONE);
m_forwardButton.set_relief(Gtk::RELIEF_NONE);
m_refreshButton.set_relief(Gtk::RELIEF_NONE);
m_homeButton.set_relief(Gtk::RELIEF_NONE);
// Add icons to buttons
backIcon.set_from_icon_name("go-previous", Gtk::IconSize(Gtk::ICON_SIZE_MENU));
m_backButton.add(backIcon);
forwardIcon.set_from_icon_name("go-next", Gtk::IconSize(Gtk::ICON_SIZE_MENU));
m_forwardButton.add(forwardIcon);
refreshIcon.set_from_icon_name("view-refresh", Gtk::IconSize(Gtk::ICON_SIZE_MENU));
m_refreshButton.add(refreshIcon);
homeIcon.set_from_icon_name("go-home", Gtk::IconSize(Gtk::ICON_SIZE_MENU));
m_homeButton.add(homeIcon);
m_hbox_bar.pack_start(m_backButton, false, false , 0);
m_hbox_bar.pack_start(m_forwardButton, false, false , 0);
m_hbox_bar.pack_start(m_refreshButton, false, false , 0);
m_hbox_bar.pack_start(m_homeButton, false, false , 0);
m_hbox_bar.pack_start(m_inputField, true, true , 8);
m_vbox.pack_start(m_hbox_bar, false, false, 6);
// Main browser rendering area
2020-11-28 14:47:34 -08:00
m_scrolledWindow.add(m_renderArea);
m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
2020-11-30 15:43:52 -08:00
m_vbox.pack_end(m_scrolledWindow, true, true, 0);
add(m_vbox);
2020-11-28 20:00:32 -08:00
show_all_children();
2020-12-08 19:44:26 -08:00
// Grap focus to input field by default
m_inputField.grab_focus();
2020-12-07 16:13:21 -08:00
2020-12-10 10:01:11 -08:00
// Show start page by default
go_home();
}
2020-12-10 10:01:11 -08:00
void MainWindow::go_home()
{
2020-12-10 10:01:11 -08:00
this->currentRequestPath = "";
m_renderArea.showStartPage();
}
void MainWindow::input_activate()
{
// doRequest("QmQzhn6hEfbYdCfwzYFsSt3eWpubVKA1dNqsgUwci5vHwq");
doRequest(m_inputField.get_text());
}
void MainWindow::doRequest(const std::string &path)
{
this->currentRequestPath = path;
// TODO: Strip protocol from currentRequestPath
if (path.rfind("ipfs://", 0) == 0) {
fetchFromIPFS();
} else if (path.rfind("file://", 0) == 0) {
openFromDisk();
} else {
// IPFS as fallback
fetchFromIPFS();
2020-12-04 17:51:40 -08:00
}
2020-12-10 10:01:11 -08:00
}
/**
* Refresh page
*/
void MainWindow::refresh()
{
if (!this->currentRequestPath.empty())
doRequest(this->currentRequestPath);
}
/**
* Display markdown file from IPFS network
*/
void MainWindow::fetchFromIPFS()
{
try {
2020-12-10 10:01:11 -08:00
cmark_node *fetchDoc = m_file.fetch(currentRequestPath);
2020-12-04 17:51:40 -08:00
m_renderArea.processDocument(fetchDoc);
m_file.free(fetchDoc);
} catch (const std::runtime_error &error) {
std::cerr << "IPFS Deamon is most likely down: " << error.what() << std::endl;
// Not found (or any other issue)
m_renderArea.showMessage("Page not found!", "Detailed error message: " + std::string(error.what()));
}
2020-11-12 20:27:47 -08:00
}
2020-12-07 16:13:21 -08:00
2020-12-10 10:01:11 -08:00
/**
* Display markdown file from disk
*/
void MainWindow::openFromDisk()
{
// std::string exePath = n_fs::current_path().string();
// std::string filePath = exePath.append("/../../test.md");
try {
cmark_node *readDoc = m_file.read(currentRequestPath);
m_renderArea.processDocument(readDoc);
m_file.free(readDoc);
} catch (const std::runtime_error &error) {
m_renderArea.showMessage("Page not found!", "Detailed error message: " + std::string(error.what()));
}
}