merge with master and fix 4 conflicts by hand
[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 #ifdef WAF_BUILD
21 #include "libardour-config.h"
22 #endif
23
24 #include <sstream>
25
26 #include <libxml/uri.h>
27
28 #ifdef HAVE_LRDF
29 #include <lrdf.h>
30 #endif
31
32 #include <glibmm/miscutils.h>
33
34 #include <glibmm/convert.h>
35
36 #include "pbd/compose.h"
37
38 #include "ardour/audio_library.h"
39 #include "ardour/utils.h"
40 #include "ardour/filesystem_paths.h"
41
42 #include "i18n.h"
43
44 using namespace std;
45 using namespace PBD;
46 using namespace ARDOUR;
47
48 namespace {
49         const char* const sfdb_file_name = "sfdb";
50 } // anonymous namespace
51
52 static const char* TAG = "http://ardour.org/ontology/Tag";
53
54 AudioLibrary::AudioLibrary ()
55 {
56         std::string sfdb_file_path(user_config_directory ());
57
58         sfdb_file_path = Glib::build_filename (sfdb_file_path, sfdb_file_name);
59
60         src = Glib::filename_to_uri (sfdb_file_path);
61
62         // workaround for possible bug in raptor that crashes when saving to a
63         // non-existant file.
64
65         touch_file(sfdb_file_path);
66
67 #ifdef HAVE_LRDF
68         lrdf_read_file(src.c_str());
69 #endif
70 }
71
72 AudioLibrary::~AudioLibrary ()
73 {
74 }
75
76 void
77 AudioLibrary::save_changes ()
78 {
79 #ifdef HAVE_LRDF
80         if (lrdf_export_by_source(src.c_str(), src.substr(5).c_str())) {
81                 PBD::warning << string_compose(_("Could not open %1.  Audio Library not saved"), src) << endmsg;
82         }
83 #endif
84 }
85
86 void
87 AudioLibrary::set_tags (string member, vector<string> tags)
88 {
89 #ifdef HAVE_LRDF
90         sort (tags.begin(), tags.end());
91         tags.erase (unique(tags.begin(), tags.end()), tags.end());
92
93         const string file_uri(Glib::filename_to_uri (member));
94
95         lrdf_remove_uri_matches (file_uri.c_str());
96
97         for (vector<string>::iterator i = tags.begin(); i != tags.end(); ++i) {
98                 lrdf_add_triple (src.c_str(), file_uri.c_str(), TAG, (*i).c_str(), lrdf_literal);
99         }
100 #endif
101 }
102
103 vector<string>
104 AudioLibrary::get_tags (string member)
105 {
106         vector<string> tags;
107 #ifdef HAVE_LRDF
108         char * uri = strdup(Glib::filename_to_uri(member).c_str());
109
110         lrdf_statement pattern;
111         pattern.subject = uri;
112         pattern.predicate = const_cast<char*>(TAG);
113         pattern.object = 0;
114         pattern.object_type = lrdf_literal;
115
116         lrdf_statement* matches = lrdf_matches (&pattern);
117
118         lrdf_statement* current = matches;
119         while (current != 0) {
120                 tags.push_back (current->object);
121
122                 current = current->next;
123         }
124
125         lrdf_free_statements (matches);
126
127         sort (tags.begin(), tags.end());
128         free (uri);
129 #endif
130         return tags;
131 }
132
133 void
134 AudioLibrary::search_members_and (vector<string>& members, const vector<string>& tags)
135 {
136 #ifdef HAVE_LRDF
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 = const_cast<char*>("?");
146                 pattern->predicate = const_cast<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                         members.push_back(Glib::filename_from_uri(ulist->items[j]));
157                 }
158                 lrdf_free_uris(ulist);
159
160             sort(members.begin(), members.end());
161             unique(members.begin(), members.end());
162         }
163
164         // memory clean up
165         pattern = *head;
166         while(pattern){
167                 free(pattern->object);
168                 old = pattern;
169                 pattern = pattern->next;
170                 delete old;
171         }
172 #endif
173 }