removed the following environment variables:
[ardour.git] / libs / ardour / audio_library.cc
1 /*
2     Copyright (C) 2003 Paul Davis 
3     Author: Taybin Rutkin
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19     $Id$
20 */
21
22 #include <cstdio> // Needed so that libraptor (included in lrdf) won't complain
23 #include <cerrno>
24 #include <iostream>
25 #include <sstream>
26 #include <cctype>
27
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fts.h>
31
32 #include <lrdf.h>
33
34 #include <pbd/compose.h>
35
36 #include <ardour/ardour.h>
37 #include <ardour/configuration.h>
38 #include <ardour/audio_library.h>
39 #include <ardour/utils.h>
40
41 #include "i18n.h"
42
43 using namespace std;
44 using namespace ARDOUR;
45
46 static char* SOUNDFILE = "http://ardour.org/ontology/Soundfile";
47
48 AudioLibrary::AudioLibrary ()
49 {
50 //      sfdb_paths.push_back("/Users/taybin/sounds");
51
52         src = "file:" + get_user_ardour_path() + "sfdb";
53
54         // workaround for possible bug in raptor that crashes when saving to a
55         // non-existant file.
56         touch_file(get_user_ardour_path() + "sfdb");
57
58         lrdf_read_file(src.c_str());
59
60         lrdf_statement pattern;
61
62         pattern.subject = SOUNDFILE;
63         pattern.predicate = RDF_TYPE;
64         pattern.object = RDFS_CLASS;
65         pattern.object_type = lrdf_uri;
66
67         lrdf_statement* matches = lrdf_matches(&pattern);
68
69         // if empty DB, create basic schema
70         if (matches == 0) {
71                 initialize_db ();
72                 save_changes();
73         } 
74
75         lrdf_free_statements(matches);
76
77         scan_paths();
78 }
79
80 AudioLibrary::~AudioLibrary ()
81 {
82 }
83
84 void
85 AudioLibrary::initialize_db ()
86 {
87         // define ardour:Soundfile
88         lrdf_add_triple(src.c_str(), SOUNDFILE, RDF_TYPE, RDFS_CLASS, lrdf_uri);
89
90         // add intergral fields
91         add_field(_("channels"));
92         add_field(_("samplerate"));
93         add_field(_("resolution"));
94         add_field(_("format"));
95 }
96
97 void
98 AudioLibrary::save_changes ()
99 {
100         if (lrdf_export_by_source(src.c_str(), src.substr(5).c_str())) {
101                 warning << string_compose(_("Could not open %1.  Audio Library not saved"), src) << endmsg;
102         }
103 }
104
105 void
106 AudioLibrary::add_member (string member)
107 {
108         string file_uri(string_compose("file:%1", member));
109
110         lrdf_add_triple(src.c_str(), file_uri.c_str(), RDF_TYPE, 
111                         SOUNDFILE, lrdf_uri);
112 }
113
114 void
115 AudioLibrary::remove_member (string uri)
116 {
117         lrdf_remove_uri_matches (uri.c_str());
118 }
119
120 void
121 AudioLibrary::search_members_and (vector<string>& members, 
122                                                                   const map<string,string>& fields)
123 {
124         lrdf_statement **head;
125         lrdf_statement* pattern = 0;
126         lrdf_statement* old = 0;
127         head = &pattern;
128
129         map<string,string>::const_iterator i;
130         for (i = fields.begin(); i != fields.end(); ++i){
131                 pattern = new lrdf_statement;
132                 pattern->subject = "?";
133                 pattern->predicate = strdup(field_uri(i->first).c_str());
134                 pattern->object = strdup((i->second).c_str());
135                 pattern->next = old;
136
137                 old = pattern;
138         }
139
140         if (*head != 0) {
141                 lrdf_uris* ulist = lrdf_match_multi(*head);
142                 for (uint32_t j = 0; ulist && j < ulist->count; ++j) {
143 //                      printf("AND: %s\n", ulist->items[j]);
144                         members.push_back(ulist->items[j]);
145                 }
146                 lrdf_free_uris(ulist);
147
148                 compact_vector(members);
149         }
150
151         // memory clean up
152         pattern = *head;
153         while(pattern){
154                 free(pattern->predicate);
155                 free(pattern->object);
156                 old = pattern;
157                 pattern = pattern->next;
158                 delete old;
159         }
160 }
161
162 void
163 AudioLibrary::search_members_or (vector<string>& members, 
164                                                                  const map<string,string>& fields)
165 {
166         map<string,string>::const_iterator i;
167
168         lrdf_statement pattern;
169         for (i = fields.begin(); i != fields.end(); ++i) {
170                 pattern.subject = 0;
171                 pattern.predicate = strdup(field_uri(i->first).c_str());
172                 pattern.object = strdup((i->second).c_str());
173                 pattern.object_type = lrdf_literal;
174
175                 lrdf_statement* matched = lrdf_matches(&pattern);
176
177                 lrdf_statement* old = matched;
178                 while(matched) {
179 //                      printf ("OR: %s\n", matched->subject);
180                         members.push_back(matched->subject);
181                         matched = matched->next;
182                 }
183
184                 free(pattern.predicate);
185                 free(pattern.object);
186                 lrdf_free_statements (old);
187         }
188
189         compact_vector(members);
190 }
191
192 void
193 AudioLibrary::add_field (string name)
194 {
195         string local_field = field_uri(name);
196         lrdf_statement pattern;
197         pattern.subject = strdup(local_field.c_str());
198         pattern.predicate = RDF_TYPE;
199         pattern.object = RDF_BASE "Property";
200         pattern.object_type = lrdf_uri;
201
202         if(lrdf_exists_match(&pattern)) {
203                 return;
204         }
205
206         // of type rdf:Property
207         lrdf_add_triple(src.c_str(), local_field.c_str(), RDF_TYPE, 
208                         RDF_BASE "Property", lrdf_uri);
209         // of range ardour:Soundfile
210         lrdf_add_triple(src.c_str(), local_field.c_str(), RDFS_BASE "range",
211                         SOUNDFILE, lrdf_uri);
212         // of domain rdf:Literal
213         lrdf_add_triple(src.c_str(), local_field.c_str(), RDFS_BASE "domain", 
214                                         RDF_BASE "Literal", lrdf_uri);
215
216         set_label (local_field, name);
217         
218         fields_changed(); /* EMIT SIGNAL */
219 }
220
221 void
222 AudioLibrary::get_fields (vector<string>& fields)
223 {
224         lrdf_statement pattern;
225
226         pattern.subject = 0;
227         pattern.predicate = RDFS_BASE "range";
228         pattern.object = SOUNDFILE;
229         pattern.object_type = lrdf_uri;
230
231         lrdf_statement* matches = lrdf_matches(&pattern);
232
233         lrdf_statement* current = matches;
234         while (current != 0) {
235                 fields.push_back(get_label(current->subject));
236
237                 current = current->next;
238         }
239
240         lrdf_free_statements(matches);
241
242         compact_vector(fields);
243 }
244
245 void
246 AudioLibrary::remove_field (string name)
247 {
248         lrdf_remove_uri_matches(field_uri(name).c_str());
249         fields_changed (); /* EMIT SIGNAL */
250 }
251
252 string 
253 AudioLibrary::get_field (string uri, string field)
254 {
255         lrdf_statement pattern;
256
257         pattern.subject = strdup(uri.c_str());
258
259         pattern.predicate = strdup(field_uri(field).c_str());
260
261         pattern.object = 0;
262         pattern.object_type = lrdf_literal;
263
264         lrdf_statement* matches = lrdf_matches(&pattern);
265         free(pattern.subject);
266         free(pattern.predicate);
267
268         stringstream object;
269         if (matches != 0){
270                 object << matches->object;
271         }
272
273         lrdf_free_statements(matches);
274         return object.str();
275 }
276
277 void 
278 AudioLibrary::set_field (string uri, string field, string literal)
279 {
280         lrdf_statement pattern;
281
282         pattern.subject = strdup(uri.c_str());
283
284         string local_field = field_uri(field);
285         pattern.predicate = strdup(local_field.c_str());
286
287         pattern.object = 0;
288         pattern.object_type = lrdf_literal;
289
290         lrdf_remove_matches(&pattern);
291         free(pattern.subject);
292         free(pattern.predicate);
293
294         lrdf_add_triple(src.c_str(), uri.c_str(), local_field.c_str(), 
295                         literal.c_str(), lrdf_literal);
296
297          fields_changed(); /* EMIT SIGNAL */
298 }
299
300 string
301 AudioLibrary::field_uri (string name)
302 {
303         stringstream local_field;
304         local_field << "file:sfdb/fields/" << name;
305
306         return local_field.str();
307 }
308
309 string
310 AudioLibrary::get_label (string uri)
311 {
312         lrdf_statement pattern;
313         pattern.subject = strdup(uri.c_str());
314         pattern.predicate = RDFS_BASE "label";
315         pattern.object = 0;
316         pattern.object_type = lrdf_literal;
317
318         lrdf_statement* matches = lrdf_matches (&pattern);
319         free(pattern.subject);
320
321         stringstream label;
322         if (matches != 0){
323                 label << matches->object;
324         }
325
326         lrdf_free_statements(matches);
327
328         return label.str();
329 }
330
331 void
332 AudioLibrary::set_label (string uri, string label)
333 {
334         lrdf_statement pattern;
335         pattern.subject = strdup(uri.c_str());
336         pattern.predicate = RDFS_BASE "label";
337         pattern.object = 0;
338         pattern.object_type = lrdf_literal;
339
340         lrdf_remove_matches(&pattern);
341         free(pattern.subject);
342
343         lrdf_add_triple(src.c_str(), uri.c_str(), RDFS_BASE "label", 
344                         label.c_str(), lrdf_literal);
345 }
346
347 void
348 AudioLibrary::compact_vector(vector<string>& vec)
349 {
350     sort(vec.begin(), vec.end());
351     unique(vec.begin(), vec.end());
352 }
353
354 void 
355 AudioLibrary::set_paths (vector<string> paths)
356 {
357         sfdb_paths = paths;
358 }
359
360 vector<string> 
361 AudioLibrary::get_paths ()
362 {
363         return sfdb_paths;
364 }
365
366 void
367 AudioLibrary::scan_paths ()
368 {
369         if (sfdb_paths.size() < 1) {
370                 return;
371         }
372
373         vector<char *> pathv(sfdb_paths.size());
374         unsigned int i;
375         for (i = 0; i < sfdb_paths.size(); ++i) {
376                 pathv[i] = new char[sfdb_paths[i].length() +1];
377                 sfdb_paths[i].copy(pathv[i], string::npos);
378                 pathv[i][sfdb_paths[i].length()] = 0;
379         }
380         pathv[i] = 0;
381
382         FTS* ft = fts_open(&pathv[0], FTS_LOGICAL|FTS_NOSTAT|FTS_PHYSICAL|FTS_XDEV, 0);
383         if (errno) {
384                 error << strerror(errno) << endmsg;
385                 return;
386         }
387
388         lrdf_statement s;
389         s.predicate = RDF_TYPE;
390         s.object = SOUNDFILE;
391         s.object_type = lrdf_uri;
392         string filename;
393         while (FTSENT* file = fts_read(ft)) {
394                 if ((file->fts_info & FTS_F) && (safe_file_extension(file->fts_name))) {
395                         filename = "file:";
396                         filename.append(file->fts_accpath);
397                         s.subject = strdup(filename.c_str());
398                         if (lrdf_exists_match(&s)) {
399                                 continue;
400                         } else {
401                                 add_member(file->fts_accpath);
402                                 cout << file->fts_accpath << endl;
403                         }
404                         free(s.subject);
405                 }
406         }
407         fts_close(ft);
408
409         for (i = 0; i < pathv.size(); ++i) {
410                 delete[] pathv[i];
411         }
412
413         save_changes();
414 }
415
416 bool
417 AudioLibrary::safe_file_extension(string file)
418 {
419         return !(file.rfind(".wav") == string::npos &&
420         file.rfind(".aiff")== string::npos &&
421         file.rfind(".aif") == string::npos &&
422         file.rfind(".snd") == string::npos &&
423         file.rfind(".au")  == string::npos &&
424         file.rfind(".raw") == string::npos &&
425         file.rfind(".sf")  == string::npos &&
426         file.rfind(".cdr") == string::npos &&
427         file.rfind(".smp") == string::npos &&
428         file.rfind(".maud")== string::npos &&
429         file.rfind(".vwe") == string::npos &&
430         file.rfind(".paf") == string::npos &&
431         file.rfind(".voc") == string::npos);
432 }