From 7278d18e0124b21c18c8e47ca403cf1eb1bea2dc Mon Sep 17 00:00:00 2001
From: Hermann Meyer <brummer-@web.de>
Date: Tue, 5 Jun 2018 10:00:47 +0200
Subject: [PATCH 2/2] * fix bug #59 Guitarix crashes when click online for
presets, switch to use libcurl instead GIO
---
trunk/src/gx_head/gui/gx_preset_window.cpp | 142 ++++++-----------------------
trunk/src/gx_head/wscript | 2 +-
trunk/src/headers/guitarix.h | 1 +
trunk/src/headers/gx_preset_window.h | 24 +----
trunk/wscript | 1 +
5 files changed, 32 insertions(+), 138 deletions(-)
diff --git a/trunk/src/gx_head/gui/gx_preset_window.cpp b/trunk/src/gx_head/gui/gx_preset_window.cpp
index e09bf77f..00e358f7 100644
--- a/trunk/src/gx_head/gui/gx_preset_window.cpp
+++ b/trunk/src/gx_head/gui/gx_preset_window.cpp
@@ -170,9 +170,13 @@ PresetWindow::PresetWindow(Glib::RefPtr<gx_gui::GxBuilder> bld, gx_engine::GxMac
gtk_size_group_add_widget(lc, GTK_WIDGET(new_preset_bank->gobj()));
gtk_size_group_add_widget(lc, GTK_WIDGET(organize_presets->gobj()));
gtk_size_group_add_widget(lc, GTK_WIDGET(online_preset->gobj()));
+ curl_global_init(CURL_GLOBAL_DEFAULT);
+ curl = curl_easy_init();
}
PresetWindow::~PresetWindow() {
+ curl_easy_cleanup(curl);
+ curl_global_cleanup();
}
void PresetWindow::on_selection_changed() {
@@ -695,53 +699,6 @@ void PresetWindow::on_new_bank() {
// Online Preset Downloader
-void DownloadWatch::start () {
- cancellable = Gio::Cancellable::create ();
- file_state = sigc::slot<void, goffset, goffset>(&f_progress);
- watcher = Glib::Thread::create(sigc::mem_fun(*this, &DownloadWatch::watch), true);
-}
-
-void DownloadWatch::watch () {
- while(true) {
- {
- Glib::Mutex::Lock lock (w_mutex);
- if (stop) break;
- }
- sleep(25); // time out for the server response
- cancellable->cancel();
- if (!stop) timeout();
- break;
- }
-}
-
-void DownloadWatch::f_progress(goffset read, goffset total)
-{
- if(Gtk::Main::events_pending())
- Gtk::Main::iteration(false);
- //std::cout << read << "/" << total << std::endl;
-}
-
-DownloadWatch::~DownloadWatch() {
- {
- Glib::Mutex::Lock lock (w_mutex);
- stop = true;
- }
- if (watcher) watcher->join();
-}
-
-void PresetWindow::go_watch () {
- if(watch != NULL) return;
- watch = new DownloadWatch();
- watch->timeout.connect(sigc::mem_fun(*this, &PresetWindow::watch_done));
- watch->start();
-}
-
-void PresetWindow::watch_done() {
- if(watch == NULL) return;
- delete watch;
- watch = NULL;
-}
-
Glib::ustring PresetWindow::resolve_hostname() {
static Glib::ustring hostname = "localhost";
if (! machine.get_jack()) {
@@ -753,21 +710,31 @@ Glib::ustring PresetWindow::resolve_hostname() {
bool PresetWindow::download_file(Glib::ustring from_uri, Glib::ustring to_path) {
- go_watch();
- Glib::RefPtr<Gio::File> rem = Gio::File::create_for_uri(from_uri);
- Glib::RefPtr<Gio::File> dest = Gio::File::create_for_uri(Glib::filename_to_uri(to_path, resolve_hostname()));
- try {
- rem->copy(dest, watch->file_state, watch->cancellable,Gio::FILE_COPY_OVERWRITE);
- } catch (Gio::Error& e) {
- if (watch->cancellable->is_cancelled()) {
- gx_print_error( _("Time out, download cancelled"), _("the server on https://musical-artifacts.com/ takes to long to respond"));
+ CURLcode res;
+ FILE *out;
+ out = fopen(to_path.c_str(), "wb");
+
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, out);
+ curl_easy_setopt(curl, CURLOPT_URL, from_uri.c_str());
+ res = curl_easy_perform(curl);
+ if(CURLE_OK == res) {
+ char *ct = NULL;
+ res = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);
+ if (strstr(ct, "application/json")!= NULL ) {
+ gx_print_info( "download_file", from_uri);
+ } else if (strstr(ct, "application/octet-stream")!= NULL) {
+ gx_print_info( "download_preset", from_uri);
} else {
- gx_print_error(e.what().c_str(), Glib::ustring::compose("can't download '%1 ' from https://musical-artifacts.com/", from_uri));
- if (watch) watch_done();
+ res = CURLE_CONV_FAILED;
}
+ }
+ curl_easy_reset(curl);
+ fclose(out);
+ if(res != CURLE_OK) {
+ remove(to_path.c_str());
+ gx_print_error( "download_file", Glib::ustring::compose("curl_easy_perform() failed: %1", curl_easy_strerror(res)));
return false;
}
- if (watch) watch_done();
return true;
}
@@ -802,9 +769,6 @@ void PresetWindow::read_preset_menu() {
std::string INFO_;
std::string AUTHOR_;
std::string line;
- // bool set_name = false;
- // bool set_file = false;
- // bool set_info = false;
while ( in->read_line(line) )
{
std::istringstream is(line);
@@ -825,9 +789,7 @@ void PresetWindow::read_preset_menu() {
jp.read_kv("file", FILE_);
INFO_ += "Author : " + AUTHOR_;
olp.push_back(std::tuple<std::string,std::string,std::string>(NAME_,FILE_,INFO_));
- //os << "\n<<NAME>> \n" << NAME_ << "\n<<FILE>> \n" << FILE_ << "\n<<INFO>> \n" << INFO_ << "\n<<END>> \n" << "\n";
- } else {
- //gx_print_warning("read_online", "unknown key: " + jp.current_value());
+ } else {
jp.skip_object();
}
} while (jp.peek() == gx_system::JsonParser::value_key);
@@ -837,41 +799,6 @@ void PresetWindow::read_preset_menu() {
cerr << "JsonException: " << e.what() << ": '" << jp.current_value() << "'" << endl;
assert(false);
}
-
-/*
- if (line.find("<<NAME>>") != string::npos) {
- set_name = true;
- set_file = false;
- set_info = false;
- NAME_ = "";
- continue;
- } else if (line.find("<<FILE>>") != string::npos ) {
- set_name = false;
- set_file = true;
- set_info = false;
- FILE_ = "";
- continue;
- } else if (line.find("<<INFO>>") != string::npos ) {
- set_name = false;
- set_file = false;
- set_info = true;
- INFO_ = "";
- continue;
- } else if (line.find("<<END>>") != string::npos ) {
- set_name = false;
- set_file = false;
- set_info = false;
- olp.push_back(std::tuple<std::string,std::string,std::string>(NAME_,FILE_,INFO_));
- NAME_ = "";
- FILE_ = "";
- INFO_ = "";
- continue;
- }
- if ( set_name ) NAME_ += line;
- else if ( set_file ) FILE_ += line;
- else if ( set_info ) INFO_ += line+"\n";
-
- */
}
in->close ();
}
@@ -919,17 +846,6 @@ void PresetWindow::replace_inline(std::string& subject, const std::string& searc
void PresetWindow::show_online_preset() {
- char *dbus = getenv("DBUS_SESSION_BUS_ADDRESS");
- if (!dbus) {
- system("eval 'dbus-launch --sh-syntax --exit-with-session'");
- }
-
- dbus = getenv("DBUS_SESSION_BUS_ADDRESS");
- if (!dbus) {
- gx_print_error("downloadPreset", _("DBUS_SESSION_BUS_ADDRESS not detected, online prest download isn't supported!!"));
- return;
- }
-
Glib::RefPtr<Gio::File> dest = Gio::File::create_for_uri(Glib::filename_to_uri(options.get_online_config_filename(), resolve_hostname()));
static bool load_new = true;
static bool load = false;
@@ -939,16 +855,14 @@ void PresetWindow::show_online_preset() {
window->set_cursor(cursor);
if (dest->query_exists()) {
Gtk::MessageDialog *d = new Gtk::MessageDialog(*dynamic_cast<Gtk::Window*>(online_preset->get_toplevel()),
- "Do you wont to check for new presets from\n https://musical-artifacts.com ? \n Note, that may take a while",
+ "Do you want to check for new presets from\n https://musical-artifacts.com ? \n Note, that may take a while",
false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_YES_NO, true);
d->set_position(Gtk::WIN_POS_MOUSE);
if (d->run() == Gtk::RESPONSE_YES) load = true;
delete d;
}
if (load || ! dest->query_exists()) {
- if (download_file("https://musical-artifacts.com/artifacts.json?apps=guitarix", options.get_online_config_filename())) {
- // machine.load_online_presets();
- } else {
+ if (!download_file("https://musical-artifacts.com/artifacts.json?apps=guitarix", options.get_online_config_filename())) {
window->set_cursor();
return;
}
diff --git a/trunk/src/gx_head/wscript b/trunk/src/gx_head/wscript
index 45a5b249..df7c97b0 100644
--- a/trunk/src/gx_head/wscript
+++ b/trunk/src/gx_head/wscript
@@ -161,7 +161,7 @@ def build(bld):
lib = []
if sys.platform.startswith("linux"):
lib.append('dl')
- uselib = ['JACK', 'SNDFILE', 'GTHREAD', 'GMODULE_EXPORT',
+ uselib = ['JACK', 'SNDFILE', 'GTHREAD', 'GMODULE_EXPORT', 'CURL',
'GTK2', 'GTKMM', 'GIOMM', 'FFTW3', 'LRDF', 'LILV', 'BOOST_SYSTEM','BOOST_IOSTREAMS']
if bld.env["HAVE_AVAHI"]:
uselib += ['AVAHI_GOBJECT', 'AVAHI_GLIB', 'AVAHI_CLIENT']
diff --git a/trunk/src/headers/guitarix.h b/trunk/src/headers/guitarix.h
index 9c71dccd..686cb284 100644
--- a/trunk/src/headers/guitarix.h
+++ b/trunk/src/headers/guitarix.h
@@ -65,6 +65,7 @@
#endif
#include <gtkmm.h>
+#include <curl/curl.h>
#include "engine.h"
#include "jsonrpc.h"
diff --git a/trunk/src/headers/gx_preset_window.h b/trunk/src/headers/gx_preset_window.h
index 5d65d399..0b2afdd0 100644
--- a/trunk/src/headers/gx_preset_window.h
+++ b/trunk/src/headers/gx_preset_window.h
@@ -66,26 +66,6 @@ public:
using Gtk::TreeView::on_drag_motion;
};
-
-class DownloadWatch {
-public:
- DownloadWatch() : watcher(0), stop(false) {}
- Glib::RefPtr<Gio::Cancellable> cancellable;
- Gio::File::SlotFileProgress file_state;
- static void f_progress(goffset read, goffset total);
- void start ();
- ~DownloadWatch();
- Glib::Dispatcher timeout;
-
-protected:
- void watch ();
- void load ();
- Glib::Thread * watcher;
- Glib::Mutex w_mutex;
- bool stop;
-};
-
-
struct GxActions;
class PresetWindow: public sigc::trackable {
@@ -97,9 +77,7 @@ private:
};
gx_engine::GxMachineBase& machine;
GxActions& actions;
- DownloadWatch *watch;
- void go_watch ();
- void watch_done();
+ CURL *curl;
bool in_edit;
Gtk::TreeModel::iterator edit_iter;
Glib::RefPtr<Gdk::Pixbuf> pb_edit;
diff --git a/trunk/wscript b/trunk/wscript
index 2d54736a..7ebd6324 100644
--- a/trunk/wscript
+++ b/trunk/wscript
@@ -676,6 +676,7 @@ def configure(conf):
conf.check_cfg(package='glibmm-2.4', atleast_version='2.24', args='--cflags --libs', uselib_store='GLIBMM', mandatory=1)
conf.check(header_name='fftw3.h', mandatory=1)
conf.check_cfg(package='fftw3f', atleast_version='3.1.2', args='--cflags --libs', uselib_store='FFTW3', mandatory=1)
+ conf.check_cfg(package='libcurl', atleast_version='7.26.0', args='--cflags --libs', uselib_store='CURL', mandatory=1)
if (opt.standalone):
conf.env['STANDALONE'] = True
--
2.14.1
|