Use connect(..., gui_thread()) rather than g_idle_add().
authorColin Fletcher <colin.m.fletcher@googlemail.com>
Sun, 16 Jun 2013 14:43:43 +0000 (15:43 +0100)
committerColin Fletcher <colin.m.fletcher@googlemail.com>
Fri, 21 Jun 2013 20:30:52 +0000 (21:30 +0100)
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
gtk2_ardour/sfdb_freesound_mootcher.h

index 38d53aaa70b44bb9017f695a9d971553f443c815..e6861cd1f4b842d19ed115cdd18c237f7ce63ad4 100644 (file)
@@ -55,6 +55,7 @@
 #include "ardour/audio_library.h"\r
 #include "ardour/rc_configuration.h"\r
 #include "pbd/pthread_utils.h"\r
+#include "gui_thread.h"\r
 \r
 using namespace PBD;\r
 \r
@@ -331,17 +332,15 @@ CURLcode res;
 \r
        return (void *) res;\r
 }\r
-\r
-static int \r
-donewithMootcher(void *arg) \r
\r
+void\r
+Mootcher::doneWithMootcher()\r
 {\r
-       Mootcher *thisMootcher = (Mootcher *) arg;\r
 \r
        // update the sound info pane if the selection in the list box is still us \r
-       thisMootcher->sfb->refresh_display(thisMootcher->ID, thisMootcher->audioFileName);\r
+       sfb->refresh_display(ID, audioFileName);\r
 \r
-       delete(thisMootcher);\r
-       return 0;\r
+       delete this; // XXX is this a good idea?\r
 }\r
 \r
 static void *\r
@@ -352,8 +351,8 @@ freesound_download_thread_func(void *arg)
 \r
        // std::cerr << "freesound_download_thread_func(" << arg << ")" << std::endl;\r
        res = thisMootcher->threadFunc();\r
-       g_idle_add(donewithMootcher, thisMootcher);\r
 \r
+       thisMootcher->Finished(); /* EMIT SIGNAL */\r
        return res;\r
 }\r
 \r
@@ -421,6 +420,8 @@ bool Mootcher::fetchAudioFile(std::string originalFileName, std::string theID, s
        curl_easy_setopt (curl, CURLOPT_PROGRESSFUNCTION, progress_callback);\r
        curl_easy_setopt (curl, CURLOPT_PROGRESSDATA, this);\r
 \r
+       Progress.connect(*this, invalidator (*this), boost::bind(&Mootcher::updateProgress, this, _1, _2), gui_context());\r
+       Finished.connect(*this, invalidator (*this), boost::bind(&Mootcher::doneWithMootcher, this), gui_context());\r
        pthread_t freesound_download_thread;\r
        pthread_create_and_store("freesound_import", &freesound_download_thread, freesound_download_thread_func, this);\r
 \r
@@ -428,29 +429,20 @@ bool Mootcher::fetchAudioFile(std::string originalFileName, std::string theID, s
 }\r
 \r
 //---------\r
-struct progressInfo {\r
-       Gtk::ProgressBar *bar;\r
-       double dltotal;\r
-       double dlnow;\r
-};\r
 \r
-static int \r
-updateProgress(void *arg\r
+void \r
+Mootcher::updateProgress(double dlnow, double dltotal\r
 {\r
-       struct progressInfo *progress = (struct progressInfo *) arg;\r
-       if (progress->dltotal > 0) {\r
-               double fraction = progress->dlnow / progress->dltotal;\r
+       if (dltotal > 0) {\r
+               double fraction = dlnow / dltotal;\r
                // std::cerr << "progress idle: " << progress->bar->get_text() << ". " << progress->dlnow << " / " << progress->dltotal << " = " << fraction << std::endl;\r
                if (fraction > 1.0) {\r
                        fraction = 1.0;\r
                } else if (fraction < 0.0) {\r
                        fraction = 0.0;\r
                }\r
-               progress->bar->set_fraction(fraction);\r
+               progress_bar.set_fraction(fraction);\r
        }\r
-\r
-       delete progress;\r
-       return 0;\r
 }\r
 \r
 int \r
@@ -466,12 +458,7 @@ Mootcher::progress_callback(void *caller, double dltotal, double dlnow, double /
                return -1;\r
        }\r
 \r
-       struct progressInfo *progress = new struct progressInfo;\r
-       progress->bar = &thisMootcher->progress_bar;\r
-       progress->dltotal = dltotal;\r
-       progress->dlnow = dlnow;\r
-\r
-       g_idle_add(updateProgress, progress);\r
+       thisMootcher->Progress(dlnow, dltotal); /* EMIT SIGNAL */\r
        return 0;\r
 }\r
 \r
index ee650200210a1ca4e9cfe1f7386f67b2d3dcb233..48fb11b638fa4a311ea49cdb440df99481db83be 100644 (file)
@@ -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<void, double, double> Progress;
+       /** signal emitted when the mootcher has finished downloading. */
+       PBD::Signal0<void> 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;