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