Add message dialogs

master
Melroy van den Berg 2021-03-31 23:55:19 +02:00
parent 9967451f1d
commit 59a4be2528
5 changed files with 65 additions and 38 deletions

View File

@ -86,7 +86,7 @@ bool IPFSProcess::shouldKillRunningProcess()
// meaning the process will be killed.
return (strncmp(pathbuf, beginPath, strlen(beginPath)) != 0);
}
// TODO: Compare IPFS version as well, maybe?
// TODO: Compare IPFS version as well (via: "ipfs version" command), maybe?
}
else
{

View File

@ -63,32 +63,24 @@ std::string const IPFS::fetch(const std::string &path)
}
/**
* \brief Publish file to IPFS network (not thread-safe)
* \param filename Filename that gets stored in IPFS
* \brief Add a file to IPFS network (not thread-safe)
* \param path File path where the file could be stored in IPFS (like puting a file inside a directory within IPFS)
* \param content Content that needs to be written to the IPFS network
* \throw std::runtime_error when there is a connection-time/something goes wrong while trying to get the file
* \return IPFS content-addressed identifier (CID) hash
*/
std::string const IPFS::publish(const std::string &filename, const std::string &content)
std::string const IPFS::add(const std::string &path, const std::string &content)
{
try
ipfs::Json result;
// Publish a single file
client.FilesAdd({{path, ipfs::http::FileUpload::Type::kFileContents, content}}, &result);
if (result.is_array())
{
ipfs::Json result;
// Publish a single file
ipfs::http::FileUpload file = {filename, ipfs::http::FileUpload::Type::kFileContents, content};
client.FilesAdd({file}, &result);
if (result.is_array())
for (const auto &files : result.items())
{
for (const auto &files : result.items())
{
return files.value()["hash"];
}
return files.value()["hash"];
}
// something is wrong, fallback
return "";
}
catch (const std::runtime_error &error)
{
// ignore connection issues
}
return ""; // empty string, something went wrong
// something is wrong, fallback
return "";
}

View File

@ -15,7 +15,7 @@ public:
std::size_t getNrPeers();
std::map<std::string, float> getBandwidthRates();
static std::string const fetch(const std::string &path);
std::string const publish(const std::string &filename, const std::string &content);
std::string const add(const std::string &path, const std::string &content);
private:
ipfs::Client client;

View File

@ -917,22 +917,54 @@ void MainWindow::publish()
{
// TODO: If the currentContent is empty, give a warning about 'Are you sure you want to publish an empty document?'
std::string filename = "new_file.md";
std::string path = "new_file.md";
// Retrieve filename from saved file (if present)
if (!currentFileSavedPath.empty()) {
filename = File::getFilename(this->currentFileSavedPath);
} else {
// TODO: Ask for filename if it wasn't saved yet, but user pressed publish directly
if (!currentFileSavedPath.empty())
{
path = currentFileSavedPath;
}
// TODO: Inventigate: should we run this within a seperate thread? Is that necessary?
// Publish content to IPFS!
std::string cid = ipfs.publish(filename, this->currentContent);
else
{
// TODO: path is not defined yet. however, this may change anyway once we try to build more complex websites,
// needing to use directory structures.
}
// TODO: should we run this within a seperate thread? Looks fine until now without threading.
// TODO: Give pop-up or some other indication the data is stored in IPFS...
if (!cid.empty()) {
std::cout << "INFO: File is published successfully on IPFS! With hash: " << cid << std::endl;
} else {
std::cerr << "ERROR: File could not be published on IPFS." << std::endl;
// Add content to IPFS!
try {
std::string cid = ipfs.add(path, this->currentContent);
// TODO: Give pop-up or some other indication the data is stored in IPFS...
if (!cid.empty())
{
m_contentPublishedDialog.reset(new Gtk::MessageDialog(*this, "File is successfully added to IPFS!"));
m_contentPublishedDialog->set_secondary_text("The content is now available on the decentralized web, via:\n\nipfs://" + cid);
m_contentPublishedDialog->set_modal(true);
// m_contentPublishedDialog->set_hide_on_close(true); available in gtk-4.0
m_contentPublishedDialog->signal_response().connect(
sigc::hide(sigc::mem_fun(*m_contentPublishedDialog, &Gtk::Widget::hide)));
m_contentPublishedDialog->show();
}
else
{
m_contentPublishedDialog.reset(new Gtk::MessageDialog(*this, "File failed to be added added to IPFS", false,
Gtk::MESSAGE_ERROR));
m_contentPublishedDialog->set_modal(true);
// m_contentPublishedDialog->set_hide_on_close(true); available in gtk-4.0
m_contentPublishedDialog->signal_response().connect(
sigc::hide(sigc::mem_fun(*m_contentPublishedDialog, &Gtk::Widget::hide)));
m_contentPublishedDialog->show();
}
}
catch (const std::runtime_error &error)
{
m_contentPublishedDialog.reset(new Gtk::MessageDialog(*this, "File could not be added to IPFS", false,
Gtk::MESSAGE_ERROR));
m_contentPublishedDialog->set_secondary_text("Error message: " + std::string(error.what()));
m_contentPublishedDialog->set_modal(true);
// m_contentPublishedDialog->set_hide_on_close(true); available in gtk-4.0
m_contentPublishedDialog->signal_response().connect(
sigc::hide(sigc::mem_fun(*m_contentPublishedDialog, &Gtk::Widget::hide)));
m_contentPublishedDialog->show();
}
}
@ -1324,14 +1356,15 @@ void MainWindow::openFromDisk(bool isParseContent)
m_draw_main.setText(currentContent);
}
}
catch (const std::ios_base::failure &e)
catch (const std::ios_base::failure &error)
{
std::cerr << "ERROR: Could not read file: " << finalRequestPath << ". Error: " << e.what() << ".\nError code: " << e.code() << std::endl;
std::cerr << "ERROR: Could not read file: " << finalRequestPath << ". Error: " << error.what() << ".\nError code: " << error.code() << std::endl;
m_draw_main.showMessage("🎂 Could not read file", "Message: " + std::string(error.what()));
}
catch (const std::runtime_error &error)
{
std::cerr << "Error: File request failed, with message: " << error.what() << std::endl;
m_draw_main.showMessage("Page not found!", "Error message: " + std::string(error.what()));
m_draw_main.showMessage("🎂 File not found", "Message: " + std::string(error.what()));
}
}

View File

@ -16,6 +16,7 @@
#include <gtkmm/comboboxtext.h>
#include <gtkmm/popover.h>
#include <gtkmm/filechooserdialog.h>
#include <gtkmm/messagedialog.h>
#include <gtkmm/entry.h>
#include <gtkmm/searchbar.h>
#include <gtkmm/searchentry.h>
@ -144,6 +145,7 @@ protected:
Gtk::Image m_hightlightIcon;
Gtk::Popover m_statusPopover;
Gtk::Label m_statusLabel;
std::unique_ptr<Gtk::MessageDialog> m_contentPublishedDialog;
Gtk::ScrolledWindow m_scrolledWindowMain;
Gtk::ScrolledWindow m_scrolledWindowSecondary;
Gtk::Button m_exitBottomButton;