Working network status icon

master
Melroy van den Berg 2021-03-27 19:36:00 +01:00
parent dc67470389
commit 1d6a111aa8
3 changed files with 150 additions and 96 deletions

View File

@ -25,18 +25,25 @@ IPFS::IPFS(std::string host, int port) : client(host, port, "6s") {}
std::size_t IPFS::getPeers()
{
try
{
ipfs::Json peers;
client.SwarmPeers(&peers);
return peers["Strings"].size();
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())
{
@ -70,10 +77,10 @@ std::size_t IPFS::getPeers()
std::cerr << "Error: IPFS Daemon is not found. IPFS will not work!" << std::endl;
return -1;
}
}
}
bool IPFS::shouldKillRunningProcess()
{
bool IPFS::shouldKillRunningProcess()
{
FILE *cmd_pipe = popen("pidof -s ipfs", "r");
if (cmd_pipe != NULL)
{
@ -108,10 +115,10 @@ std::size_t IPFS::getPeers()
}
// Something went wrong, fallback is to kill (better safe then sorry)
return true;
}
}
std::string IPFS::findIPFSBinary()
{
std::string IPFS::findIPFSBinary()
{
// Try absolute path first
for (std::string data_dir : Glib::get_system_data_dirs())
{
@ -135,4 +142,4 @@ std::size_t IPFS::getPeers()
{
return "";
}
}
}

View File

@ -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
*/

View File

@ -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();