ensure that the name used the control out IO for a route always uses the desired...
[ardour.git] / libs / ardour / audio_library.cc
1 /*
2     Copyright (C) 2003-2006 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 #include <sstream>
21
22 #include <libxml/uri.h>
23
24 #include <lrdf.h>
25 #include <glibmm/miscutils.h>
26
27 #include <pbd/compose.h>
28
29 #include <ardour/audio_library.h>
30 #include <ardour/utils.h>
31
32 #include "i18n.h"
33
34 using namespace std;
35 using namespace ARDOUR;
36
37 static const char* TAG = "http://ardour.org/ontology/Tag";
38
39 AudioLibrary::AudioLibrary ()
40 {
41         /* XXX URL - '/' is always the separator */
42
43         src = "file:" + get_user_ardour_path() + "/sfdb";
44
45         // workaround for possible bug in raptor that crashes when saving to a
46         // non-existant file.
47
48         touch_file(Glib::build_filename (get_user_ardour_path(), "sfdb"));
49
50         lrdf_read_file(src.c_str());
51 }
52
53 AudioLibrary::~AudioLibrary ()
54 {
55 }
56
57 void
58 AudioLibrary::save_changes ()
59 {
60         if (lrdf_export_by_source(src.c_str(), src.substr(5).c_str())) {
61                 PBD::warning << string_compose(_("Could not open %1.  Audio Library not saved"), src) << endmsg;
62         }
63 }
64
65 string
66 AudioLibrary::path2uri (string path)
67 {
68         xmlURI temp;
69         memset(&temp, 0, sizeof(temp));
70         
71         xmlChar *cal = xmlCanonicPath((xmlChar*) path.c_str());
72         temp.path = (char *) cal;
73         xmlChar *ret = xmlSaveUri(&temp);
74         xmlFree(cal);
75         
76         stringstream uri;
77         uri << "file:" << (const char*) ret;
78         
79         xmlFree (ret);
80         
81         return uri.str();
82 }
83
84 string
85 AudioLibrary::uri2path (string uri)
86 {
87         string path = xmlURIUnescapeString(uri.c_str(), 0, 0);
88         return path.substr(5);
89 }
90
91 void
92 AudioLibrary::set_tags (string member, vector<string> tags)
93 {
94         sort (tags.begin(), tags.end());
95         tags.erase (unique(tags.begin(), tags.end()), tags.end());
96         
97         string file_uri(path2uri(member));
98         
99         lrdf_remove_uri_matches (file_uri.c_str());
100         
101         for (vector<string>::iterator i = tags.begin(); i != tags.end(); ++i) {
102                 lrdf_add_triple (src.c_str(), file_uri.c_str(), TAG, (*i).c_str(), lrdf_literal);
103         }
104 }
105
106 vector<string>
107 AudioLibrary::get_tags (string member)
108 {
109         vector<string> tags;
110         
111         lrdf_statement pattern;
112         pattern.subject = strdup(path2uri(member).c_str());
113         pattern.predicate = (char*)TAG;
114         pattern.object = 0;
115         pattern.object_type = lrdf_literal;
116         
117         lrdf_statement* matches = lrdf_matches (&pattern);
118         free (pattern.subject);
119         
120         lrdf_statement* current = matches;
121         while (current != 0) {
122                 tags.push_back (current->object);
123                 
124                 current = current->next;
125         }
126         
127         lrdf_free_statements (matches);
128         
129         sort (tags.begin(), tags.end());
130         
131         return tags;
132 }
133
134 void
135 AudioLibrary::search_members_and (vector<string>& members, const vector<string> tags)
136 {
137         lrdf_statement **head;
138         lrdf_statement* pattern = 0;
139         lrdf_statement* old = 0;
140         head = &pattern;
141
142         vector<string>::const_iterator i;
143         for (i = tags.begin(); i != tags.end(); ++i){
144                 pattern = new lrdf_statement;
145                 pattern->subject = (char*)"?";
146                 pattern->predicate = (char*)TAG;
147                 pattern->object = strdup((*i).c_str());
148                 pattern->next = old;
149
150                 old = pattern;
151         }
152
153         if (*head != 0) {
154                 lrdf_uris* ulist = lrdf_match_multi(*head);
155                 for (uint32_t j = 0; ulist && j < ulist->count; ++j) {
156 //                      cerr << "AND: " << uri2path(ulist->items[j]) << endl;
157                         members.push_back(uri2path(ulist->items[j]));
158                 }
159                 lrdf_free_uris(ulist);
160
161             sort(members.begin(), members.end());
162             unique(members.begin(), members.end());
163         }
164
165         // memory clean up
166         pattern = *head;
167         while(pattern){
168                 free(pattern->object);
169                 old = pattern;
170                 pattern = pattern->next;
171                 delete old;
172         }
173 }