Merge branch 'radiosrb' of https://github.com/adiknoth/ardour
[ardour.git] / gtk2_ardour / sfdb_freesound_mootcher.h
1 /*
2     Copyright (C) 2012 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /*sfdb_freesound_mootcher.h****************************************************************************
21
22         Adapted for Ardour by Ben Loftis, March 2008
23         Updated to new Freesound API by Colin Fletcher, November 2011
24
25         Mootcher Online Access to thefreesoundproject website
26         http://freesound.iua.upf.edu/
27
28         GPL 2005 Jorn Lemon
29         mail for questions/remarks: mootcher@twistedlemon.nl
30         or go to the freesound website forum
31
32 *****************************************************************************/
33
34 #include <string>
35 #include <fstream>
36 #include <iostream>
37 #include <stdio.h>
38 #include <cstring>
39 #include <string>
40 #include <sstream>
41 #include <vector>
42 #include <gtkmm/progressbar.h>
43 //#include <ctime>
44
45 #include "sfdb_ui.h"
46
47 #include "curl/curl.h"
48
49 //--- struct to store XML file
50 struct MemoryStruct {
51         char *memory;
52         size_t size;
53 };
54
55 enum sortMethod {
56         sort_none,              // no sort
57         sort_duration_desc,     // Sort by the duration of the sounds, longest sounds first.
58         sort_duration_asc,      // Same as above, but shortest sounds first.
59         sort_created_desc,      // Sort by the date of when the sound was added. newest sounds first.
60         sort_created_asc,       // Same as above, but oldest sounds first.
61         sort_downloads_desc,    // Sort by the number of downloads, most downloaded sounds first.
62         sort_downloads_asc,     // Same as above, but least downloaded sounds first.
63         sort_rating_desc,       // Sort by the average rating given to the sounds, highest rated first.
64         sort_rating_asc         // Same as above, but lowest rated sounds first.
65 };
66
67
68 class Mootcher: public sigc::trackable, public PBD::ScopedConnectionList
69 {
70 public:
71         Mootcher();
72         ~Mootcher();
73
74         bool            checkAudioFile(std::string originalFileName, std::string ID);
75         bool            fetchAudioFile(std::string originalFileName, std::string ID, std::string audioURL, SoundFileBrowser *caller);
76         std::string     searchText(std::string query, int page, std::string filter, enum sortMethod sort);
77         std::string     searchSimilar(std::string id);
78         void *          threadFunc();
79         SoundFileBrowser *sfb; 
80         std::string     audioFileName;
81         std::string     ID;
82
83         /** signal emitted when mootcher reports progress updates during download.
84          * The parameters are current and total numbers of bytes downloaded.
85          */
86         PBD::Signal2<void, double, double> Progress;
87         /** signal emitted when the mootcher has finished downloading. */
88         PBD::Signal0<void> Finished;
89
90
91 private:
92
93         void            ensureWorkingDir();
94
95         std::string     doRequest(std::string uri, std::string params);
96         void            setcUrlOptions();
97
98         static size_t   WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data);
99         static int      progress_callback(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow);
100         std::string     sortMethodString(enum sortMethod sort);
101         std::string     getSoundResourceFile(std::string ID);
102
103         CURL *curl;
104         char errorBuffer[CURL_ERROR_SIZE];      // storage for cUrl error message
105
106         FILE* theFile;
107
108         void updateProgress(double dlnow, double dltotal);
109         void doneWithMootcher();
110
111         Gtk::HBox progress_hbox;
112         Gtk::ProgressBar progress_bar;
113         Gtk::Button cancel_download_btn;
114
115         bool cancel_download;
116         void cancelDownload() { 
117                 cancel_download = true;
118                 progress_hbox.hide();
119         }
120
121         std::string basePath;
122         std::string xmlLocation;
123 };
124