From 8ad4924b7f6d12037753e0a93b5f861b443614ae Mon Sep 17 00:00:00 2001 From: Colin Fletcher Date: Sun, 16 Jun 2013 15:43:43 +0100 Subject: Use connect(..., gui_thread()) rather than g_idle_add(). Use the proper functions to ensure things happen in the main gui thread, instead of fudging around with g_idle_add(). --- gtk2_ardour/sfdb_freesound_mootcher.cc | 43 ++++++++++++---------------------- gtk2_ardour/sfdb_freesound_mootcher.h | 13 +++++++++- 2 files changed, 27 insertions(+), 29 deletions(-) (limited to 'gtk2_ardour') diff --git a/gtk2_ardour/sfdb_freesound_mootcher.cc b/gtk2_ardour/sfdb_freesound_mootcher.cc index 38d53aaa70..e6861cd1f4 100644 --- a/gtk2_ardour/sfdb_freesound_mootcher.cc +++ b/gtk2_ardour/sfdb_freesound_mootcher.cc @@ -55,6 +55,7 @@ #include "ardour/audio_library.h" #include "ardour/rc_configuration.h" #include "pbd/pthread_utils.h" +#include "gui_thread.h" using namespace PBD; @@ -331,17 +332,15 @@ CURLcode res; return (void *) res; } - -static int -donewithMootcher(void *arg) + +void +Mootcher::doneWithMootcher() { - Mootcher *thisMootcher = (Mootcher *) arg; // update the sound info pane if the selection in the list box is still us - thisMootcher->sfb->refresh_display(thisMootcher->ID, thisMootcher->audioFileName); + sfb->refresh_display(ID, audioFileName); - delete(thisMootcher); - return 0; + delete this; // XXX is this a good idea? } static void * @@ -352,8 +351,8 @@ freesound_download_thread_func(void *arg) // std::cerr << "freesound_download_thread_func(" << arg << ")" << std::endl; res = thisMootcher->threadFunc(); - g_idle_add(donewithMootcher, thisMootcher); + thisMootcher->Finished(); /* EMIT SIGNAL */ return res; } @@ -421,6 +420,8 @@ bool Mootcher::fetchAudioFile(std::string originalFileName, std::string theID, s curl_easy_setopt (curl, CURLOPT_PROGRESSFUNCTION, progress_callback); curl_easy_setopt (curl, CURLOPT_PROGRESSDATA, this); + Progress.connect(*this, invalidator (*this), boost::bind(&Mootcher::updateProgress, this, _1, _2), gui_context()); + Finished.connect(*this, invalidator (*this), boost::bind(&Mootcher::doneWithMootcher, this), gui_context()); pthread_t freesound_download_thread; pthread_create_and_store("freesound_import", &freesound_download_thread, freesound_download_thread_func, this); @@ -428,29 +429,20 @@ bool Mootcher::fetchAudioFile(std::string originalFileName, std::string theID, s } //--------- -struct progressInfo { - Gtk::ProgressBar *bar; - double dltotal; - double dlnow; -}; -static int -updateProgress(void *arg) +void +Mootcher::updateProgress(double dlnow, double dltotal) { - struct progressInfo *progress = (struct progressInfo *) arg; - if (progress->dltotal > 0) { - double fraction = progress->dlnow / progress->dltotal; + if (dltotal > 0) { + double fraction = dlnow / dltotal; // std::cerr << "progress idle: " << progress->bar->get_text() << ". " << progress->dlnow << " / " << progress->dltotal << " = " << fraction << std::endl; if (fraction > 1.0) { fraction = 1.0; } else if (fraction < 0.0) { fraction = 0.0; } - progress->bar->set_fraction(fraction); + progress_bar.set_fraction(fraction); } - - delete progress; - return 0; } int @@ -466,12 +458,7 @@ Mootcher::progress_callback(void *caller, double dltotal, double dlnow, double / return -1; } - struct progressInfo *progress = new struct progressInfo; - progress->bar = &thisMootcher->progress_bar; - progress->dltotal = dltotal; - progress->dlnow = dlnow; - - g_idle_add(updateProgress, progress); + thisMootcher->Progress(dlnow, dltotal); /* EMIT SIGNAL */ return 0; } diff --git a/gtk2_ardour/sfdb_freesound_mootcher.h b/gtk2_ardour/sfdb_freesound_mootcher.h index ee65020021..48fb11b638 100644 --- a/gtk2_ardour/sfdb_freesound_mootcher.h +++ b/gtk2_ardour/sfdb_freesound_mootcher.h @@ -65,7 +65,7 @@ enum sortMethod { }; -class Mootcher +class Mootcher: public sigc::trackable, public PBD::ScopedConnectionList { public: Mootcher(); @@ -80,6 +80,14 @@ public: std::string audioFileName; std::string ID; + /** signal emitted when mootcher reports progress updates during download. + * The parameters are current and total numbers of bytes downloaded. + */ + PBD::Signal2 Progress; + /** signal emitted when the mootcher has finished downloading. */ + PBD::Signal0 Finished; + + private: void ensureWorkingDir(); @@ -97,6 +105,9 @@ private: FILE* theFile; + void updateProgress(double dlnow, double dltotal); + void doneWithMootcher(); + Gtk::HBox progress_hbox; Gtk::ProgressBar progress_bar; Gtk::Button cancel_download_btn; -- cgit v1.2.3