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