Working network status icon
parent
dc67470389
commit
1d6a111aa8
185
src/ipfs.cc
185
src/ipfs.cc
|
@ -25,114 +25,121 @@ IPFS::IPFS(std::string host, int port) : client(host, port, "6s") {}
|
|||
|
||||
std::size_t IPFS::getPeers()
|
||||
{
|
||||
ipfs::Json peers;
|
||||
client.SwarmPeers(&peers);
|
||||
return peers["Strings"].size();
|
||||
try
|
||||
{
|
||||
ipfs::Json peers;
|
||||
client.SwarmPeers(&peers);
|
||||
return peers["Peers"].size();
|
||||
}
|
||||
catch (const std::runtime_error &error)
|
||||
{
|
||||
// ignore connection issues
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* \brief Start IPFS Daemon
|
||||
* \return exit code
|
||||
*/
|
||||
int
|
||||
IPFS::startIPFSDaemon()
|
||||
int IPFS::startIPFSDaemon()
|
||||
{
|
||||
// Kill any running IPFS daemons if needed
|
||||
if (IPFS::shouldKillRunningProcess())
|
||||
{
|
||||
// Kill any running IPFS daemons if needed
|
||||
if (IPFS::shouldKillRunningProcess())
|
||||
std::cout << "INFO: Already running ipfs process will be terminated." << std::endl;
|
||||
int res = std::system("killall -w -q ipfs");
|
||||
if (res != 0)
|
||||
{
|
||||
std::cout << "INFO: Already running ipfs process will be terminated." << std::endl;
|
||||
int res = std::system("killall -w -q ipfs");
|
||||
if (res != 0)
|
||||
{
|
||||
// ignore
|
||||
}
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
// Find the IPFS binary
|
||||
std::string executable = IPFS::findIPFSBinary();
|
||||
std::cout << "INFO: Starting IPFS Daemon, using: " << executable << std::endl;
|
||||
if (n_fs::exists(executable))
|
||||
// Find the IPFS binary
|
||||
std::string executable = IPFS::findIPFSBinary();
|
||||
std::cout << "INFO: Starting IPFS Daemon, using: " << executable << std::endl;
|
||||
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
|
||||
|
||||
// Ready to call exec to start IPFS Daemon
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
bool IPFS::shouldKillRunningProcess()
|
||||
{
|
||||
FILE *cmd_pipe = popen("pidof -s ipfs", "r");
|
||||
if (cmd_pipe != NULL)
|
||||
{
|
||||
char pidbuf[512];
|
||||
memset(pidbuf, 0, sizeof(pidbuf));
|
||||
if (fgets(pidbuf, 512, cmd_pipe) == NULL)
|
||||
{
|
||||
/// 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
|
||||
//ignore
|
||||
}
|
||||
pclose(cmd_pipe);
|
||||
|
||||
// Ready to call exec to start IPFS Daemon
|
||||
const char *exe = executable.c_str();
|
||||
char *proc[] = {strdup(exe), strdup("daemon"), strdup("--init"), strdup("--migrate"), NULL};
|
||||
return execv(exe, proc);
|
||||
if (strlen(pidbuf) > 0)
|
||||
{
|
||||
pid_t pid = strtoul(pidbuf, NULL, 10);
|
||||
char pathbuf[1024];
|
||||
memset(pathbuf, 0, sizeof(pathbuf));
|
||||
std::string path = "/proc/" + std::to_string(pid) + "/exe";
|
||||
if (readlink(path.c_str(), pathbuf, sizeof(pathbuf) - 1) > 0)
|
||||
{
|
||||
// TODO: Compare version or file path location
|
||||
char beginPath[28] = "/usr/share/libreweb-browser";
|
||||
// If the begin path does not path (!= 0), return true,
|
||||
// meaning the process will be killed.
|
||||
return (strncmp(pathbuf, beginPath, strlen(beginPath)) != 0);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << "Error: IPFS Daemon is not found. IPFS will not work!" << std::endl;
|
||||
return -1;
|
||||
// No running IPFS process
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// Something went wrong, fallback is to kill (better safe then sorry)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool IPFS::shouldKillRunningProcess()
|
||||
std::string IPFS::findIPFSBinary()
|
||||
{
|
||||
// Try absolute path first
|
||||
for (std::string data_dir : Glib::get_system_data_dirs())
|
||||
{
|
||||
FILE *cmd_pipe = popen("pidof -s ipfs", "r");
|
||||
if (cmd_pipe != NULL)
|
||||
{
|
||||
char pidbuf[512];
|
||||
memset(pidbuf, 0, sizeof(pidbuf));
|
||||
if (fgets(pidbuf, 512, cmd_pipe) == NULL)
|
||||
{
|
||||
//ignore
|
||||
}
|
||||
pclose(cmd_pipe);
|
||||
|
||||
if (strlen(pidbuf) > 0)
|
||||
{
|
||||
pid_t pid = strtoul(pidbuf, NULL, 10);
|
||||
char pathbuf[1024];
|
||||
memset(pathbuf, 0, sizeof(pathbuf));
|
||||
std::string path = "/proc/" + std::to_string(pid) + "/exe";
|
||||
if (readlink(path.c_str(), pathbuf, sizeof(pathbuf) - 1) > 0)
|
||||
{
|
||||
// TODO: Compare version or file path location
|
||||
char beginPath[28] = "/usr/share/libreweb-browser";
|
||||
// If the begin path does not path (!= 0), return true,
|
||||
// meaning the process will be killed.
|
||||
return (strncmp(pathbuf, beginPath, strlen(beginPath)) != 0);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// No running IPFS process
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// Something went wrong, fallback is to kill (better safe then sorry)
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string IPFS::findIPFSBinary()
|
||||
{
|
||||
// Try absolute path first
|
||||
for (std::string data_dir : Glib::get_system_data_dirs())
|
||||
{
|
||||
std::vector<std::string> path_builder{data_dir, "libreweb-browser", "go-ipfs", "ipfs"};
|
||||
std::string ipfs_binary_path = Glib::build_path(G_DIR_SEPARATOR_S, path_builder);
|
||||
if (Glib::file_test(ipfs_binary_path, Glib::FileTest::FILE_TEST_IS_EXECUTABLE))
|
||||
{
|
||||
return ipfs_binary_path;
|
||||
}
|
||||
}
|
||||
|
||||
// Try local path if the images are not installed (yet)
|
||||
// When working directory is in the build/bin folder (relative path)
|
||||
std::string currentPath = n_fs::current_path().string();
|
||||
std::string ipfs_binary_path = Glib::build_filename(currentPath, "../..", "go-ipfs", "ipfs");
|
||||
std::vector<std::string> path_builder{data_dir, "libreweb-browser", "go-ipfs", "ipfs"};
|
||||
std::string ipfs_binary_path = Glib::build_path(G_DIR_SEPARATOR_S, path_builder);
|
||||
if (Glib::file_test(ipfs_binary_path, Glib::FileTest::FILE_TEST_IS_EXECUTABLE))
|
||||
{
|
||||
return ipfs_binary_path;
|
||||
}
|
||||
else
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Try local path if the images are not installed (yet)
|
||||
// When working directory is in the build/bin folder (relative path)
|
||||
std::string currentPath = n_fs::current_path().string();
|
||||
std::string ipfs_binary_path = Glib::build_filename(currentPath, "../..", "go-ipfs", "ipfs");
|
||||
if (Glib::file_test(ipfs_binary_path, Glib::FileTest::FILE_TEST_IS_EXECUTABLE))
|
||||
{
|
||||
return ipfs_binary_path;
|
||||
}
|
||||
else
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
|
@ -8,6 +8,7 @@
|
|||
#include <giomm/file.h>
|
||||
#include <glibmm/fileutils.h>
|
||||
#include <glibmm/miscutils.h>
|
||||
#include <glibmm/main.h>
|
||||
#include <gdk-pixbuf/gdk-pixbuf.h>
|
||||
#include <cmark-gfm.h>
|
||||
#include <pthread.h>
|
||||
|
@ -41,6 +42,9 @@ MainWindow::MainWindow()
|
|||
set_position(Gtk::WIN_POS_CENTER);
|
||||
add_accel_group(accelGroup);
|
||||
|
||||
// Timeouts
|
||||
this->statusTimerHandler = Glib::signal_timeout().connect(sigc::mem_fun(this, &MainWindow::update_connect_status), 3000);
|
||||
|
||||
// Connect signals
|
||||
m_menu.new_doc.connect(sigc::mem_fun(this, &MainWindow::new_doc)); /*!< Menu item for new document */
|
||||
m_menu.open.connect(sigc::mem_fun(this, &MainWindow::open)); /*!< Menu item for opening existing document */
|
||||
|
@ -238,14 +242,16 @@ MainWindow::MainWindow()
|
|||
m_statusButton.set_relief(Gtk::RELIEF_NONE);
|
||||
|
||||
// Add icons to the toolbar buttons
|
||||
m_statusOfflineIcon = Gdk::Pixbuf::create_from_file(this->getIconImageFromTheme("network_disconnected", "network"), m_iconSize, m_iconSize);
|
||||
m_statusOnlineIcon = Gdk::Pixbuf::create_from_file(this->getIconImageFromTheme("network_connected", "network"), m_iconSize, m_iconSize);
|
||||
|
||||
if (m_useCurrentGTKIconTheme)
|
||||
{
|
||||
m_backIcon.set_from_icon_name("go-previous", Gtk::IconSize(Gtk::ICON_SIZE_MENU));
|
||||
m_forwardIcon.set_from_icon_name("go-next", Gtk::IconSize(Gtk::ICON_SIZE_MENU));
|
||||
m_refreshIcon.set_from_icon_name("view-refresh", Gtk::IconSize(Gtk::ICON_SIZE_MENU));
|
||||
m_homeIcon.set_from_icon_name("go-home", Gtk::IconSize(Gtk::ICON_SIZE_MENU));
|
||||
m_statusOfflineIcon.set_from_icon_name("network-offline", Gtk::IconSize(Gtk::ICON_SIZE_MENU));
|
||||
m_statusOnlineIcon.set_from_icon_name("network-wired", Gtk::IconSize(Gtk::ICON_SIZE_MENU));
|
||||
m_statusIcon.set_from_icon_name("network-offline", Gtk::IconSize(Gtk::ICON_SIZE_MENU)); // fall-back
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -253,14 +259,14 @@ MainWindow::MainWindow()
|
|||
m_forwardIcon.set(Gdk::Pixbuf::create_from_file(this->getIconImageFromTheme("right_arrow_1", "arrows"), m_iconSize, m_iconSize));
|
||||
m_refreshIcon.set(Gdk::Pixbuf::create_from_file(this->getIconImageFromTheme("reload_2", "arrows"), m_iconSize, m_iconSize));
|
||||
m_homeIcon.set(Gdk::Pixbuf::create_from_file(this->getIconImageFromTheme("home", "basic"), m_iconSize, m_iconSize));
|
||||
m_statusOfflineIcon.set(Gdk::Pixbuf::create_from_file(this->getIconImageFromTheme("network_disconnected", "network"), m_iconSize, m_iconSize));
|
||||
m_statusOnlineIcon.set(Gdk::Pixbuf::create_from_file(this->getIconImageFromTheme("network_connected", "network"), m_iconSize, m_iconSize));
|
||||
m_statusIcon.set(m_statusOfflineIcon); // fall-back
|
||||
}
|
||||
m_backButton.add(m_backIcon);
|
||||
m_forwardButton.add(m_forwardIcon);
|
||||
m_refreshButton.add(m_refreshIcon);
|
||||
m_homeButton.add(m_homeIcon);
|
||||
m_statusButton.add(m_statusOfflineIcon);
|
||||
m_statusButton.add(m_statusIcon);
|
||||
|
||||
// Add tooltips to the toolbar buttons
|
||||
m_backButton.set_tooltip_text("Go back one page (Alt+Left arrow)");
|
||||
m_forwardButton.set_tooltip_text("Go forward one page (Alt+Right arrow)");
|
||||
|
@ -353,6 +359,10 @@ MainWindow::MainWindow()
|
|||
// Grap focus to input field by default
|
||||
m_addressBar.grab_focus();
|
||||
|
||||
// First time manually trigger the status update once,
|
||||
// timer will do the updates later
|
||||
this->update_connect_status();
|
||||
|
||||
#ifdef NDEBUG
|
||||
// Show start page by default
|
||||
go_home();
|
||||
|
@ -385,6 +395,40 @@ void MainWindow::doRequest(const std::string &path, bool setAddressBar, bool isH
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Timeout slot: Update the IPFS connection status every x seconds
|
||||
*/
|
||||
bool MainWindow::update_connect_status()
|
||||
{
|
||||
std::size_t peers = ipfs.getPeers();
|
||||
// TODO: Catch the pixbuf images, instead of reading from disk every time
|
||||
if (peers > 0)
|
||||
{
|
||||
if (m_useCurrentGTKIconTheme)
|
||||
{
|
||||
m_statusIcon.set_from_icon_name("network-wired", Gtk::IconSize(Gtk::ICON_SIZE_MENU));
|
||||
}
|
||||
else
|
||||
{
|
||||
m_statusIcon.set(m_statusOnlineIcon);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (m_useCurrentGTKIconTheme)
|
||||
{
|
||||
m_statusIcon.set_from_icon_name("network-offline", Gtk::IconSize(Gtk::ICON_SIZE_MENU));
|
||||
}
|
||||
else
|
||||
{
|
||||
m_statusIcon.set(m_statusOfflineIcon);
|
||||
}
|
||||
}
|
||||
|
||||
// Keep going (do not disconnect yet)
|
||||
return true;
|
||||
}
|
||||
|
||||
/***
|
||||
* Cut/copy/paste/delete/select all keybindings
|
||||
*/
|
||||
|
|
|
@ -31,6 +31,7 @@ public:
|
|||
|
||||
protected:
|
||||
// Signal handlers
|
||||
bool update_connect_status();
|
||||
void cut();
|
||||
void copy();
|
||||
void paste();
|
||||
|
@ -106,8 +107,9 @@ protected:
|
|||
Gtk::Image m_forwardIcon;
|
||||
Gtk::Image m_refreshIcon;
|
||||
Gtk::Image m_homeIcon;
|
||||
Gtk::Image m_statusOfflineIcon;
|
||||
Gtk::Image m_statusOnlineIcon;
|
||||
Gtk::Image m_statusIcon;
|
||||
Glib::RefPtr<Gdk::Pixbuf> m_statusOfflineIcon;
|
||||
Glib::RefPtr<Gdk::Pixbuf> m_statusOnlineIcon;
|
||||
Gtk::Image m_openIcon;
|
||||
Gtk::Image m_saveIcon;
|
||||
Gtk::Image m_publishIcon;
|
||||
|
@ -148,6 +150,7 @@ private:
|
|||
std::size_t currentHistoryIndex;
|
||||
std::vector<std::string> history;
|
||||
sigc::connection textChangedSignalHandler;
|
||||
sigc::connection statusTimerHandler;
|
||||
IPFS ipfs;
|
||||
|
||||
void enableEdit();
|
||||
|
|
Loading…
Reference in New Issue