Add API to dispatch keyboard events to VST Plugins
[ardour.git] / gtk2_ardour / sfdb_freesound_mootcher.cc
1 /* sfdb_freesound_mootcher.cpp **********************************************************************
2
3         Adapted for Ardour by Ben Loftis, March 2008
4         Updated to new Freesound API by Colin Fletcher, November 2011
5
6         Mootcher 23-8-2005
7
8         Mootcher Online Access to thefreesoundproject website
9         http://freesound.iua.upf.edu/
10
11         GPL 2005 Jorn Lemon
12         mail for questions/remarks: mootcher@twistedlemon.nl
13         or go to the freesound website forum
14
15         -----------------------------------------------------------------
16
17         Includes:
18                 curl.h    (version 7.14.0)
19         Librarys:
20                 libcurl.lib
21
22         -----------------------------------------------------------------
23         Licence GPL:
24
25         This program is free software; you can redistribute it and/or
26         modify it under the terms of the GNU General Public License
27         as published by the Free Software Foundation; either version 2
28         of the License, or (at your option) any later version.
29
30         This program is distributed in the hope that it will be useful,
31         but WITHOUT ANY WARRANTY; without even the implied warranty of
32         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33         GNU General Public License for more details.
34
35         You should have received a copy of the GNU General Public License
36         along with this program; if not, write to the Free Software
37         Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
38
39
40 *************************************************************************************/
41 #include "sfdb_freesound_mootcher.h"
42
43 #include "pbd/xml++.h"
44 #include "pbd/error.h"
45
46 #include <sys/stat.h>
47 #include <sys/types.h>
48 #include <iostream>
49
50 #include <glib.h>
51 #include "pbd/gstdio_compat.h"
52
53 #include "pbd/i18n.h"
54
55 #include "ardour/audio_library.h"
56 #include "ardour/rc_configuration.h"
57 #include "pbd/pthread_utils.h"
58 #include "gui_thread.h"
59
60 using namespace PBD;
61
62 static const std::string base_url = "http://www.freesound.org/api";
63 static const std::string api_key = "9d77cb8d841b4bcfa960e1aae62224eb"; // ardour3
64
65 //------------------------------------------------------------------------
66 Mootcher::Mootcher()
67         : curl(curl_easy_init())
68 {
69         cancel_download_btn.set_label (_("Cancel"));
70         progress_hbox.pack_start (progress_bar, true, true);
71         progress_hbox.pack_end (cancel_download_btn, false, false);
72         progress_bar.show();
73         cancel_download_btn.show();
74         cancel_download_btn.signal_clicked().connect(sigc::mem_fun (*this, &Mootcher::cancelDownload));
75 };
76 //------------------------------------------------------------------------
77 Mootcher:: ~Mootcher()
78 {
79         curl_easy_cleanup(curl);
80 }
81
82 //------------------------------------------------------------------------
83
84 void Mootcher::ensureWorkingDir ()
85 {
86         std::string p = ARDOUR::Config->get_freesound_download_dir();
87
88         if (!Glib::file_test (p, Glib::FILE_TEST_IS_DIR)) {
89                 if (g_mkdir_with_parents (p.c_str(), 0775) != 0) {
90                         PBD::error << "Unable to create Mootcher working dir" << endmsg;
91                 }
92         }
93         basePath = p;
94 #ifdef PLATFORM_WINDOWS
95         std::string replace = "/";
96         size_t pos = basePath.find("\\");
97         while( pos != std::string::npos ){
98                 basePath.replace(pos, 1, replace);
99                 pos = basePath.find("\\");
100         }
101 #endif
102 }
103
104
105 //------------------------------------------------------------------------
106 size_t Mootcher::WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
107 {
108         int realsize = (int)(size * nmemb);
109         struct SfdbMemoryStruct *mem = (struct SfdbMemoryStruct *)data;
110
111         mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
112
113         if (mem->memory) {
114                 memcpy(&(mem->memory[mem->size]), ptr, realsize);
115                 mem->size += realsize;
116                 mem->memory[mem->size] = 0;
117         }
118         return realsize;
119 }
120
121
122 //------------------------------------------------------------------------
123
124 std::string Mootcher::sortMethodString(enum sortMethod sort)
125 {
126 // given a sort type, returns the string value to be passed to the API to
127 // sort the results in the requested way.
128
129         switch (sort) {
130                 case sort_duration_desc:        return "duration_desc";
131                 case sort_duration_asc:         return "duration_asc";
132                 case sort_created_desc:         return "created_desc";
133                 case sort_created_asc:          return "created_asc";
134                 case sort_downloads_desc:       return "downloads_desc";
135                 case sort_downloads_asc:        return "downloads_asc";
136                 case sort_rating_desc:          return "rating_desc";
137                 case sort_rating_asc:           return "rating_asc";
138                 default:                        return "";
139         }
140 }
141
142 //------------------------------------------------------------------------
143 void Mootcher::setcUrlOptions()
144 {
145         // some servers don't like requests that are made without a user-agent field, so we provide one
146         curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
147         // setup curl error buffer
148         curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
149         // Allow redirection
150         curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
151
152         // Allow connections to time out (without using signals)
153         curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
154         curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 30);
155
156
157 }
158
159 std::string Mootcher::doRequest(std::string uri, std::string params)
160 {
161         std::string result;
162         struct SfdbMemoryStruct xml_page;
163         xml_page.memory = NULL;
164         xml_page.size = 0;
165
166         setcUrlOptions();
167         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
168         curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) &xml_page);
169
170         // curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
171         // curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postMessage.c_str());
172         // curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, -1);
173
174         // the url to get
175         std::string url = base_url + uri + "?";
176         if (params != "") {
177                 url += params + "&api_key=" + api_key + "&format=xml";
178         } else {
179                 url += "api_key=" + api_key + "&format=xml";
180         }
181
182         curl_easy_setopt(curl, CURLOPT_URL, url.c_str() );
183
184         // perform online request
185         CURLcode res = curl_easy_perform(curl);
186         if( res != 0 ) {
187                 error << string_compose (_("curl error %1 (%2)"), res, curl_easy_strerror(res)) << endmsg;
188                 return "";
189         }
190
191         // free the memory
192         if (xml_page.memory) {
193                 result = xml_page.memory;
194         }
195
196         free (xml_page.memory);
197         xml_page.memory = NULL;
198         xml_page.size = 0;
199
200         return result;
201 }
202
203
204 std::string Mootcher::searchSimilar(std::string id)
205 {
206         std::string params = "";
207
208         params += "&fields=id,original_filename,duration,filesize,samplerate,license,serve";
209         params += "&num_results=100";
210
211         return doRequest("/sounds/" + id + "/similar", params);
212 }
213
214 //------------------------------------------------------------------------
215
216 std::string Mootcher::searchText(std::string query, int page, std::string filter, enum sortMethod sort)
217 {
218         std::string params = "";
219         char buf[24];
220
221         if (page > 1) {
222                 snprintf(buf, 23, "p=%d&", page);
223                 params += buf;
224         }
225
226         char *eq = curl_easy_escape(curl, query.c_str(), query.length());
227         params += "q=\"" + std::string(eq) + "\"";
228         free(eq);
229
230         if (filter != "") {
231                 char *ef = curl_easy_escape(curl, filter.c_str(), filter.length());
232                 params += "&f=" + std::string(ef);
233                 free(ef);
234         }
235
236         if (sort)
237                 params += "&s=" + sortMethodString(sort);
238
239         params += "&fields=id,original_filename,duration,filesize,samplerate,license,serve";
240         params += "&sounds_per_page=100";
241
242         return doRequest("/sounds/search", params);
243 }
244
245 //------------------------------------------------------------------------
246
247 std::string Mootcher::getSoundResourceFile(std::string ID)
248 {
249
250         std::string originalSoundURI;
251         std::string audioFileName;
252         std::string xml;
253
254
255         // download the xmlfile into xml_page
256         xml = doRequest("/sounds/" + ID, "");
257
258         XMLTree doc;
259         doc.read_buffer( xml.c_str() );
260         XMLNode *freesound = doc.root();
261
262         // if the page is not a valid xml document with a 'freesound' root
263         if (freesound == NULL) {
264                 error << _("getSoundResourceFile: There is no valid root in the xml file") << endmsg;
265                 return "";
266         }
267
268         if (strcmp(doc.root()->name().c_str(), "response") != 0) {
269                 error << string_compose (_("getSoundResourceFile: root = %1, != response"), doc.root()->name()) << endmsg;
270                 return "";
271         }
272
273         XMLNode *name = freesound->child("original_filename");
274
275         // get the file name and size from xml file
276         if (name) {
277
278                 audioFileName = Glib::build_filename (basePath, ID + "-" + name->child("text")->content());
279
280                 //store all the tags in the database
281                 XMLNode *tags = freesound->child("tags");
282                 if (tags) {
283                         XMLNodeList children = tags->children();
284                         XMLNodeConstIterator niter;
285                         std::vector<std::string> strings;
286                         for (niter = children.begin(); niter != children.end(); ++niter) {
287                                 XMLNode *node = *niter;
288                                 if( strcmp( node->name().c_str(), "resource") == 0 ) {
289                                         XMLNode *text = node->child("text");
290                                         if (text) {
291                                                 // std::cerr << "tag: " << text->content() << std::endl;
292                                                 strings.push_back(text->content());
293                                         }
294                                 }
295                         }
296                         ARDOUR::Library->set_tags (std::string("//")+audioFileName, strings);
297                         ARDOUR::Library->save_changes ();
298                 }
299         }
300
301         return audioFileName;
302 }
303
304 int audioFileWrite(void *buffer, size_t size, size_t nmemb, void *file)
305 {
306         return (int)fwrite(buffer, size, nmemb, (FILE*) file);
307 };
308
309 //------------------------------------------------------------------------
310
311 void *
312 Mootcher::threadFunc() {
313 CURLcode res;
314
315         res = curl_easy_perform (curl);
316         fclose (theFile);
317         curl_easy_setopt (curl, CURLOPT_NOPROGRESS, 1); // turn off the progress bar
318
319         if (res != CURLE_OK) {
320                 /* it's not an error if the user pressed the stop button */
321                 if (res != CURLE_ABORTED_BY_CALLBACK) {
322                         error <<  string_compose (_("curl error %1 (%2)"), res, curl_easy_strerror(res)) << endmsg;
323                 }
324                 remove ( (audioFileName+".part").c_str() );
325         } else {
326                 rename ( (audioFileName+".part").c_str(), audioFileName.c_str() );
327                 // now download the tags &c.
328                 getSoundResourceFile(ID);
329         }
330
331         return (void *) res;
332 }
333
334 void
335 Mootcher::doneWithMootcher()
336 {
337
338         // update the sound info pane if the selection in the list box is still us
339         sfb->refresh_display(ID, audioFileName);
340
341         delete this; // this should be OK to do as long as Progress and Finished signals are always received in the order in which they are emitted
342 }
343
344 static void *
345 freesound_download_thread_func(void *arg)
346 {
347         Mootcher *thisMootcher = (Mootcher *) arg;
348         void *res;
349
350         // std::cerr << "freesound_download_thread_func(" << arg << ")" << std::endl;
351         res = thisMootcher->threadFunc();
352
353         thisMootcher->Finished(); /* EMIT SIGNAL */
354         return res;
355 }
356
357
358 //------------------------------------------------------------------------
359
360 bool Mootcher::checkAudioFile(std::string originalFileName, std::string theID)
361 {
362         ensureWorkingDir();
363         ID = theID;
364         audioFileName = Glib::build_filename (basePath, ID + "-" + originalFileName);
365
366         // check to see if audio file already exists
367         FILE *testFile = g_fopen(audioFileName.c_str(), "r");
368         if (testFile) {
369                 fseek (testFile , 0 , SEEK_END);
370                 if (ftell (testFile) > 256) {
371                         fclose (testFile);
372                         return true;
373                 }
374
375                 // else file was small, probably an error, delete it
376                 fclose(testFile);
377                 remove( audioFileName.c_str() );
378         }
379         return false;
380 }
381
382
383 bool Mootcher::fetchAudioFile(std::string originalFileName, std::string theID, std::string audioURL, SoundFileBrowser *caller)
384 {
385         ensureWorkingDir();
386         ID = theID;
387         audioFileName = Glib::build_filename (basePath, ID + "-" + originalFileName);
388
389         if (!curl) {
390                 return false;
391         }
392         // now download the actual file
393         theFile = g_fopen( (audioFileName + ".part").c_str(), "wb" );
394
395         if (!theFile) {
396                 return false;
397         }
398
399         // create the download url
400         audioURL += "?api_key=" + api_key;
401
402         setcUrlOptions();
403         curl_easy_setopt(curl, CURLOPT_URL, audioURL.c_str() );
404         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, audioFileWrite);
405         curl_easy_setopt(curl, CURLOPT_WRITEDATA, theFile);
406
407         std::string prog;
408         prog = string_compose (_("%1"), originalFileName);
409         progress_bar.set_text(prog);
410
411         Gtk::VBox *freesound_vbox = dynamic_cast<Gtk::VBox *> (caller->notebook.get_nth_page(2));
412         freesound_vbox->pack_start(progress_hbox, Gtk::PACK_SHRINK);
413         progress_hbox.show();
414         cancel_download = false;
415         sfb = caller;
416
417         curl_easy_setopt (curl, CURLOPT_NOPROGRESS, 0); // turn on the progress bar
418         curl_easy_setopt (curl, CURLOPT_PROGRESSFUNCTION, progress_callback);
419         curl_easy_setopt (curl, CURLOPT_PROGRESSDATA, this);
420
421         Progress.connect(*this, invalidator (*this), boost::bind(&Mootcher::updateProgress, this, _1, _2), gui_context());
422         Finished.connect(*this, invalidator (*this), boost::bind(&Mootcher::doneWithMootcher, this), gui_context());
423         pthread_t freesound_download_thread;
424         pthread_create_and_store("freesound_import", &freesound_download_thread, freesound_download_thread_func, this);
425
426         return true;
427 }
428
429 //---------
430
431 void
432 Mootcher::updateProgress(double dlnow, double dltotal)
433 {
434         if (dltotal > 0) {
435                 double fraction = dlnow / dltotal;
436                 // std::cerr << "progress idle: " << progress->bar->get_text() << ". " << progress->dlnow << " / " << progress->dltotal << " = " << fraction << std::endl;
437                 if (fraction > 1.0) {
438                         fraction = 1.0;
439                 } else if (fraction < 0.0) {
440                         fraction = 0.0;
441                 }
442                 progress_bar.set_fraction(fraction);
443         }
444 }
445
446 int
447 Mootcher::progress_callback(void *caller, double dltotal, double dlnow, double /*ultotal*/, double /*ulnow*/)
448 {
449         // It may seem curious to pass a pointer to an instance of an object to a static
450         // member function, but we can't use a normal member function as a curl progress callback,
451         // and we want access to some private members of Mootcher.
452
453         Mootcher *thisMootcher = (Mootcher *) caller;
454
455         if (thisMootcher->cancel_download) {
456                 return -1;
457         }
458
459         thisMootcher->Progress(dlnow, dltotal); /* EMIT SIGNAL */
460         return 0;
461 }
462