Properly encode file URIs in the sfdb.
[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
26 #include <pbd/compose.h>
27
28 #include <ardour/audio_library.h>
29 #include <ardour/utils.h>
30
31 #include "i18n.h"
32
33 using namespace std;
34 using namespace ARDOUR;
35
36 static char* TAG = "http://ardour.org/ontology/Tag";
37
38 AudioLibrary::AudioLibrary ()
39 {
40         src = "file:" + get_user_ardour_path() + "sfdb";
41
42         // workaround for possible bug in raptor that crashes when saving to a
43         // non-existant file.
44         touch_file(get_user_ardour_path() + "sfdb");
45
46         lrdf_read_file(src.c_str());
47 }
48
49 AudioLibrary::~AudioLibrary ()
50 {
51 }
52
53 void
54 AudioLibrary::save_changes ()
55 {
56         if (lrdf_export_by_source(src.c_str(), src.substr(5).c_str())) {
57                 PBD::warning << string_compose(_("Could not open %1.  Audio Library not saved"), src) << endmsg;
58         }
59 }
60
61 string
62 AudioLibrary::path2uri (string path)
63 {
64         xmlURI temp;
65         memset(&temp, 0, sizeof(temp));
66         
67         xmlChar *cal = xmlCanonicPath((xmlChar*) path.c_str());
68         temp.path = (char *) cal;
69         xmlChar *ret = xmlSaveUri(&temp);
70         xmlFree(cal);
71         
72         stringstream uri;
73         uri << "file:" << (const char*) ret;
74         
75         xmlFree (ret);
76         
77         return uri.str();
78 }
79
80 void
81 AudioLibrary::set_tags (string member, vector<string> tags)
82 {
83         sort (tags.begin(), tags.end());
84         tags.erase (unique(tags.begin(), tags.end()), tags.end());
85         
86         string file_uri(path2uri(member));
87         
88         lrdf_remove_uri_matches (file_uri.c_str());
89         
90         for (vector<string>::iterator i = tags.begin(); i != tags.end(); ++i) {
91                 lrdf_add_triple (src.c_str(), file_uri.c_str(), TAG, (*i).c_str(), lrdf_literal);
92         }
93 }
94
95 vector<string>
96 AudioLibrary::get_tags (string member)
97 {
98         vector<string> tags;
99         
100         lrdf_statement pattern;
101         pattern.subject = strdup(path2uri(member).c_str());
102         pattern.predicate = TAG;
103         pattern.object = 0;
104         pattern.object_type = lrdf_literal;
105         
106         lrdf_statement* matches = lrdf_matches (&pattern);
107         free (pattern.subject);
108         
109         lrdf_statement* current = matches;
110         while (current != 0) {
111                 tags.push_back (current->object);
112                 
113                 current = current->next;
114         }
115         
116         lrdf_free_statements (matches);
117         
118         sort (tags.begin(), tags.end());
119         
120         return tags;
121 }
122
123 void
124 AudioLibrary::search_members_and (vector<string>& members, const vector<string> tags)
125 {
126         lrdf_statement **head;
127         lrdf_statement* pattern = 0;
128         lrdf_statement* old = 0;
129         head = &pattern;
130
131         vector<string>::const_iterator i;
132         for (i = tags.begin(); i != tags.end(); ++i){
133                 pattern = new lrdf_statement;
134                 pattern->subject = "?";
135                 pattern->predicate = TAG;
136                 pattern->object = strdup((*i).c_str());
137                 pattern->next = old;
138
139                 old = pattern;
140         }
141
142         if (*head != 0) {
143                 lrdf_uris* ulist = lrdf_match_multi(*head);
144                 for (uint32_t j = 0; ulist && j < ulist->count; ++j) {
145 //                      printf("AND: %s\n", ulist->items[j]);
146                         members.push_back(ulist->items[j]);
147                 }
148                 lrdf_free_uris(ulist);
149
150             sort(members.begin(), members.end());
151             unique(members.begin(), members.end());
152         }
153
154         // memory clean up
155         pattern = *head;
156         while(pattern){
157                 free(pattern->predicate);
158                 free(pattern->object);
159                 old = pattern;
160                 pattern = pattern->next;
161                 delete old;
162         }
163 }
164
165 bool
166 AudioLibrary::safe_file_extension(string file)
167 {
168         return !(file.rfind(".wav") == string::npos &&
169                 file.rfind(".aiff")== string::npos &&
170                 file.rfind(".aif") == string::npos &&
171                 file.rfind(".snd") == string::npos &&
172                 file.rfind(".au")  == string::npos &&
173                 file.rfind(".raw") == string::npos &&
174                 file.rfind(".sf")  == string::npos &&
175                 file.rfind(".cdr") == string::npos &&
176                 file.rfind(".smp") == string::npos &&
177                 file.rfind(".maud")== string::npos &&
178                 file.rfind(".vwe") == string::npos &&
179                 file.rfind(".paf") == string::npos &&
180 #ifdef HAVE_COREAUDIO
181                 file.rfind(".mp3") == string::npos &&
182                 file.rfind(".aac") == string::npos &&
183                 file.rfind(".mp4") == string::npos &&
184 #endif // HAVE_COREAUDIO
185                 file.rfind(".voc") == string::npos);
186 }