2 Copyright (C) 2003 Paul Davis
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.
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.
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.
22 #include <cstdio> // Needed so that libraptor (included in lrdf) won't complain
29 #include <pbd/compose.h>
31 #include <ardour/ardour.h>
32 #include <ardour/configuration.h>
33 #include <ardour/audio_library.h>
34 #include <ardour/utils.h>
39 using namespace ARDOUR;
43 bool operator() (string a, string b) const {
44 return cmp_nocase(Library->get_label(a), Library->get_label(b)) == -1;
49 static char* GROUP = "http://ardour.org/ontology/Group";
50 static char* SOUNDFILE = "http://ardour.org/ontology/Soundfile";
51 static char* hasFile = "http://ardour.org/ontology/hasFile";
52 static char* memberOf = "http://ardour.org/ontology/memberOf";
53 static char* subGroupOf = "http://ardour.org/ontology/subGroupOf";
55 AudioLibrary::AudioLibrary ()
57 src = "file:" + Config->get_user_ardour_path() + "sfdb";
59 // workaround for possible bug in raptor that crashes when saving to a
61 touch_file(Config->get_user_ardour_path() + "sfdb");
63 lrdf_read_file(src.c_str());
65 lrdf_statement pattern;
67 pattern.subject = GROUP;
68 pattern.predicate = RDF_TYPE;
69 pattern.object = RDFS_CLASS;
70 pattern.object_type = lrdf_uri;
72 lrdf_statement* matches = lrdf_matches(&pattern);
74 // if empty DB, create basic schema
80 lrdf_free_statements(matches);
83 AudioLibrary::~AudioLibrary ()
88 AudioLibrary::initialize_db ()
90 // define ardour:Group
91 lrdf_add_triple(src.c_str(), GROUP, RDF_TYPE, RDFS_CLASS, lrdf_uri);
92 // define ardour:Soundfile
93 lrdf_add_triple(src.c_str(), SOUNDFILE, RDF_TYPE, RDFS_CLASS, lrdf_uri);
95 // add intergral fields
96 add_field("channels");
97 add_field("samplerate");
98 add_field("resolution");
103 AudioLibrary::save_changes ()
105 if (lrdf_export_by_source(src.c_str(), src.substr(5).c_str())) {
106 warning << compose(_("Could not open %1. Audio Library not saved"), src) << endmsg;
111 AudioLibrary::add_group (string group, string parent_uri)
113 string local_group(compose("file:sfbd/group/%1", get_uid()));
115 lrdf_add_triple(src.c_str(), local_group.c_str(),
116 RDFS_BASE "label", group.c_str(), lrdf_literal);
118 if (parent_uri == ""){
119 lrdf_add_triple(src.c_str(), local_group.c_str(),
120 subGroupOf, GROUP, lrdf_uri);
122 lrdf_add_triple(src.c_str(), local_group.c_str(),
123 subGroupOf, parent_uri.c_str(), lrdf_uri);
126 added_group(local_group, parent_uri); /* EMIT SIGNAL */
132 AudioLibrary::remove_group (string uri)
135 list<string>::iterator i;
137 get_members(items, uri);
138 for (i = items.begin(); i != items.end(); ++i) {
144 get_groups(items, uri);
145 for (i = items.begin(); i != items.end(); ++i) {
149 lrdf_remove_uri_matches(uri.c_str());
152 removed_group(uri); /* EMIT SIGNAL */
156 AudioLibrary::get_groups (list<string>& groups, string parent_uri)
158 lrdf_statement pattern;
161 pattern.predicate = subGroupOf;
162 if (parent_uri == ""){
163 pattern.object = strdup(GROUP);
165 pattern.object = strdup(parent_uri.c_str());
168 lrdf_statement* matches = lrdf_matches(&pattern);
170 lrdf_statement* current = matches;
171 while (current != 0) {
172 groups.push_back(current->subject);
173 current = current->next;
176 lrdf_free_statements(matches);
177 free (pattern.object);
185 AudioLibrary::add_member (string member, string parent_uri)
187 string local_member(compose("file:sfdb/soundfile/%1", get_uid()));
188 string file_uri(compose("file:%1", member));
190 lrdf_add_triple(src.c_str(), local_member.c_str(), RDF_TYPE,
191 SOUNDFILE, lrdf_uri);
192 lrdf_add_triple(src.c_str(), local_member.c_str(), hasFile,
193 file_uri.c_str(), lrdf_uri);
195 string::size_type size = member.find_last_of('/');
196 string label = member.substr(++size);
198 lrdf_add_triple(src.c_str(), local_member.c_str(), RDFS_BASE "label",
199 label.c_str(), lrdf_literal);
201 if (parent_uri == ""){
202 lrdf_add_triple(src.c_str(), local_member.c_str(), memberOf,
205 lrdf_add_triple(src.c_str(), local_member.c_str(), memberOf,
206 parent_uri.c_str(), lrdf_uri);
211 added_member (local_member, parent_uri); /* EMIT SIGNAL */
217 AudioLibrary::remove_member (string uri)
219 lrdf_remove_uri_matches (uri.c_str());
223 removed_member(uri); /* EMIT SIGNAL */
227 AudioLibrary::get_members (list<string>& members, string parent_uri)
229 lrdf_statement pattern;
232 pattern.predicate = memberOf;
233 if (parent_uri == ""){
234 pattern.object = strdup(GROUP);
236 pattern.object = strdup(parent_uri.c_str());
239 lrdf_statement* matches = lrdf_matches(&pattern);
241 lrdf_statement* current = matches;
242 while (current != 0) {
243 members.push_back(current->subject);
244 current = current->next;
247 lrdf_free_statements(matches);
248 free (pattern.object);
256 AudioLibrary::search_members_and (list<string>& members,
257 const map<string,string>& fields)
259 lrdf_statement **head;
260 lrdf_statement* pattern = 0;
261 lrdf_statement* old = 0;
264 map<string,string>::const_iterator i;
265 for (i = fields.begin(); i != fields.end(); ++i){
266 pattern = new lrdf_statement;
267 pattern->subject = "?";
268 pattern->predicate = strdup(field_uri(i->first).c_str());
269 pattern->object = strdup((i->second).c_str());
276 lrdf_uris* ulist = lrdf_match_multi(*head);
277 for (uint32_t j = 0; ulist && j < ulist->count; ++j) {
278 // printf("AND: %s\n", ulist->items[j]);
279 members.push_back(ulist->items[j]);
281 lrdf_free_uris(ulist);
291 free(pattern->predicate);
292 free(pattern->object);
294 pattern = pattern->next;
300 AudioLibrary::search_members_or (list<string>& members,
301 const map<string,string>& fields)
303 map<string,string>::const_iterator i;
305 lrdf_statement pattern;
306 for (i = fields.begin(); i != fields.end(); ++i) {
308 pattern.predicate = strdup(field_uri(i->first).c_str());
309 pattern.object = strdup((i->second).c_str());
310 pattern.object_type = lrdf_literal;
312 lrdf_statement* matched = lrdf_matches(&pattern);
314 lrdf_statement* old = matched;
316 // printf ("OR: %s\n", matched->subject);
317 members.push_back(matched->subject);
318 matched = matched->next;
321 free(pattern.predicate);
322 free(pattern.object);
323 lrdf_free_statements (old);
332 AudioLibrary::get_member_filename (string uri)
334 lrdf_statement pattern;
335 pattern.subject = strdup(uri.c_str());
336 pattern.predicate = hasFile;
338 pattern.object_type = lrdf_uri;
340 lrdf_statement* matches = lrdf_matches(&pattern);
342 string file = matches->object;
343 lrdf_free_statements(matches);
345 string::size_type pos = file.find(":");
346 return file.substr(++pos);
348 warning << _("Could not find member filename") << endmsg;
354 AudioLibrary::add_field (string name)
356 string local_field = field_uri(name);
357 lrdf_statement pattern;
358 pattern.subject = strdup(local_field.c_str());
359 pattern.predicate = RDF_TYPE;
360 pattern.object = RDF_BASE "Property";
361 pattern.object_type = lrdf_uri;
363 if(lrdf_exists_match(&pattern)) {
367 // of type rdf:Property
368 lrdf_add_triple(src.c_str(), local_field.c_str(), RDF_TYPE,
369 RDF_BASE "Property", lrdf_uri);
370 // of range ardour:Soundfile
371 lrdf_add_triple(src.c_str(), local_field.c_str(), RDFS_BASE "range",
372 SOUNDFILE, lrdf_uri);
373 // of domain rdf:Literal
374 lrdf_add_triple(src.c_str(), local_field.c_str(), RDFS_BASE "domain",
375 RDF_BASE "Literal", lrdf_uri);
377 set_label (local_field, name);
381 fields_changed(); /* EMIT SIGNAL */
385 AudioLibrary::get_fields (list<string>& fields)
387 lrdf_statement pattern;
390 pattern.predicate = RDFS_BASE "range";
391 pattern.object = SOUNDFILE;
392 pattern.object_type = lrdf_uri;
394 lrdf_statement* matches = lrdf_matches(&pattern);
396 lrdf_statement* current = matches;
397 while (current != 0) {
398 fields.push_back(get_label(current->subject));
400 current = current->next;
403 lrdf_free_statements(matches);
410 AudioLibrary::remove_field (string name)
412 lrdf_remove_uri_matches(field_uri(name).c_str());
414 fields_changed (); /* EMIT SIGNAL */
418 AudioLibrary::get_field (string uri, string field)
420 lrdf_statement pattern;
422 pattern.subject = strdup(uri.c_str());
424 pattern.predicate = strdup(field_uri(field).c_str());
427 pattern.object_type = lrdf_literal;
429 lrdf_statement* matches = lrdf_matches(&pattern);
430 free(pattern.subject);
431 free(pattern.predicate);
435 object << matches->object;
438 lrdf_free_statements(matches);
443 AudioLibrary::set_field (string uri, string field, string literal)
445 lrdf_statement pattern;
447 pattern.subject = strdup(uri.c_str());
449 string local_field = field_uri(field);
450 pattern.predicate = strdup(local_field.c_str());
453 pattern.object_type = lrdf_literal;
455 lrdf_remove_matches(&pattern);
456 free(pattern.subject);
457 free(pattern.predicate);
459 lrdf_add_triple(src.c_str(), uri.c_str(), local_field.c_str(),
460 literal.c_str(), lrdf_literal);
464 fields_changed(); /* EMIT SIGNAL */
468 AudioLibrary::field_uri (string name)
470 stringstream local_field;
471 local_field << "file:sfdb/fields/" << name;
473 return local_field.str();
477 AudioLibrary::get_label (string uri)
479 lrdf_statement pattern;
480 pattern.subject = strdup(uri.c_str());
481 pattern.predicate = RDFS_BASE "label";
483 pattern.object_type = lrdf_literal;
485 lrdf_statement* matches = lrdf_matches (&pattern);
486 free(pattern.subject);
490 label << matches->object;
493 lrdf_free_statements(matches);
499 AudioLibrary::set_label (string uri, string label)
501 lrdf_statement pattern;
502 pattern.subject = strdup(uri.c_str());
503 pattern.predicate = RDFS_BASE "label";
505 pattern.object_type = lrdf_literal;
507 lrdf_remove_matches(&pattern);
508 free(pattern.subject);
510 lrdf_add_triple(src.c_str(), uri.c_str(), RDFS_BASE "label",
511 label.c_str(), lrdf_literal);