13
0

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().
This commit is contained in:
Colin Fletcher 2013-06-16 15:43:43 +01:00
parent 0483803186
commit 8ad4924b7f
2 changed files with 27 additions and 29 deletions

View File

@ -55,6 +55,7 @@
#include "ardour/audio_library.h" #include "ardour/audio_library.h"
#include "ardour/rc_configuration.h" #include "ardour/rc_configuration.h"
#include "pbd/pthread_utils.h" #include "pbd/pthread_utils.h"
#include "gui_thread.h"
using namespace PBD; using namespace PBD;
@ -332,16 +333,14 @@ CURLcode res;
return (void *) res; return (void *) res;
} }
static int void
donewithMootcher(void *arg) Mootcher::doneWithMootcher()
{ {
Mootcher *thisMootcher = (Mootcher *) arg;
// update the sound info pane if the selection in the list box is still us // 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); delete this; // XXX is this a good idea?
return 0;
} }
static void * static void *
@ -352,8 +351,8 @@ freesound_download_thread_func(void *arg)
// std::cerr << "freesound_download_thread_func(" << arg << ")" << std::endl; // std::cerr << "freesound_download_thread_func(" << arg << ")" << std::endl;
res = thisMootcher->threadFunc(); res = thisMootcher->threadFunc();
g_idle_add(donewithMootcher, thisMootcher);
thisMootcher->Finished(); /* EMIT SIGNAL */
return res; 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_PROGRESSFUNCTION, progress_callback);
curl_easy_setopt (curl, CURLOPT_PROGRESSDATA, this); 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_t freesound_download_thread;
pthread_create_and_store("freesound_import", &freesound_download_thread, freesound_download_thread_func, this); 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 void
updateProgress(void *arg) Mootcher::updateProgress(double dlnow, double dltotal)
{ {
struct progressInfo *progress = (struct progressInfo *) arg; if (dltotal > 0) {
if (progress->dltotal > 0) { double fraction = dlnow / dltotal;
double fraction = progress->dlnow / progress->dltotal;
// std::cerr << "progress idle: " << progress->bar->get_text() << ". " << progress->dlnow << " / " << progress->dltotal << " = " << fraction << std::endl; // std::cerr << "progress idle: " << progress->bar->get_text() << ". " << progress->dlnow << " / " << progress->dltotal << " = " << fraction << std::endl;
if (fraction > 1.0) { if (fraction > 1.0) {
fraction = 1.0; fraction = 1.0;
} else if (fraction < 0.0) { } else if (fraction < 0.0) {
fraction = 0.0; fraction = 0.0;
} }
progress->bar->set_fraction(fraction); progress_bar.set_fraction(fraction);
} }
delete progress;
return 0;
} }
int int
@ -466,12 +458,7 @@ Mootcher::progress_callback(void *caller, double dltotal, double dlnow, double /
return -1; return -1;
} }
struct progressInfo *progress = new struct progressInfo; thisMootcher->Progress(dlnow, dltotal); /* EMIT SIGNAL */
progress->bar = &thisMootcher->progress_bar;
progress->dltotal = dltotal;
progress->dlnow = dlnow;
g_idle_add(updateProgress, progress);
return 0; return 0;
} }

View File

@ -65,7 +65,7 @@ enum sortMethod {
}; };
class Mootcher class Mootcher: public sigc::trackable, public PBD::ScopedConnectionList
{ {
public: public:
Mootcher(); Mootcher();
@ -80,6 +80,14 @@ public:
std::string audioFileName; std::string audioFileName;
std::string ID; std::string ID;
/** signal emitted when mootcher reports progress updates during download.
* The parameters are current and total numbers of bytes downloaded.
*/
PBD::Signal2<void, double, double> Progress;
/** signal emitted when the mootcher has finished downloading. */
PBD::Signal0<void> Finished;
private: private:
void ensureWorkingDir(); void ensureWorkingDir();
@ -97,6 +105,9 @@ private:
FILE* theFile; FILE* theFile;
void updateProgress(double dlnow, double dltotal);
void doneWithMootcher();
Gtk::HBox progress_hbox; Gtk::HBox progress_hbox;
Gtk::ProgressBar progress_bar; Gtk::ProgressBar progress_bar;
Gtk::Button cancel_download_btn; Gtk::Button cancel_download_btn;