1 /*
2 * Copyright (C) 2005-2007 Taybin Rutkin <taybin@taybin.com>
3 * Copyright (C) 2005-2016 Paul Davis <paul@linuxaudiosystems.com>
4 * Copyright (C) 2006-2015 Tim Mayberry <mojofunk@gmail.com>
5 * Copyright (C) 2007-2010 David Robillard <d@drobilla.net>
6 * Copyright (C) 2014-2019 Robin Gareus <robin@gareus.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #ifdef WAF_BUILD
24 #include "libardour-config.h"
25 #endif
26
27 #include <sstream>
28
29 #include <libxml/uri.h>
30
31 #ifdef HAVE_LRDF
32 #include <lrdf.h>
33 #endif
34
35 #include <glibmm/miscutils.h>
36
37 #include <glibmm/convert.h>
38
39 #include "pbd/compose.h"
40 #include "pbd/error.h"
41 #include "pbd/file_utils.h"
42
43 #include "ardour/audio_library.h"
44 #include "ardour/filesystem_paths.h"
45
46 #include "pbd/i18n.h"
47
48 using namespace std;
49 using namespace PBD;
50 using namespace ARDOUR;
51
52 namespace {
53 const char* const sfdb_file_name = "sfdb";
54 } // anonymous namespace
55
56 static const char* TAG = "http://ardour.org/ontology/Tag";
57
AudioLibrary()58 AudioLibrary::AudioLibrary ()
59 {
60 std::string sfdb_file_path(user_config_directory ());
61
62 sfdb_file_path = Glib::build_filename (sfdb_file_path, sfdb_file_name);
63
64 src = Glib::filename_to_uri (sfdb_file_path);
65
66 // workaround for possible bug in raptor that crashes when saving to a
67 // non-existant file.
68
69 touch_file(sfdb_file_path);
70
71 #ifdef HAVE_LRDF
72 lrdf_read_file(src.c_str());
73 #endif
74 }
75
~AudioLibrary()76 AudioLibrary::~AudioLibrary ()
77 {
78 }
79
80 void
save_changes()81 AudioLibrary::save_changes ()
82 {
83 #ifdef HAVE_LRDF
84 #ifdef PLATFORM_WINDOWS
85 string path = Glib::locale_from_utf8 (Glib::filename_from_uri(src));
86 #else
87 string path = Glib::filename_from_uri(src);
88 #endif
89 if (lrdf_export_by_source(src.c_str(), path.c_str())) {
90 PBD::warning << string_compose(_("Could not open %1. Audio Library not saved"), path) << endmsg;
91 }
92 #endif
93 }
94
95 void
set_tags(string member,vector<string> tags)96 AudioLibrary::set_tags (string member, vector<string> tags)
97 {
98 #ifdef HAVE_LRDF
99 sort (tags.begin(), tags.end());
100 tags.erase (unique(tags.begin(), tags.end()), tags.end());
101
102 const string file_uri(Glib::filename_to_uri (member));
103
104 lrdf_remove_uri_matches (file_uri.c_str());
105
106 for (vector<string>::iterator i = tags.begin(); i != tags.end(); ++i) {
107 lrdf_add_triple (src.c_str(), file_uri.c_str(), TAG, (*i).c_str(), lrdf_literal);
108 }
109 #endif
110 }
111
112 vector<string>
get_tags(string member)113 AudioLibrary::get_tags (string member)
114 {
115 vector<string> tags;
116 #ifdef HAVE_LRDF
117 char * uri = strdup(Glib::filename_to_uri(member).c_str());
118
119 lrdf_statement pattern;
120 pattern.subject = uri;
121 pattern.predicate = const_cast<char*>(TAG);
122 pattern.object = 0;
123 pattern.object_type = lrdf_literal;
124
125 lrdf_statement* matches = lrdf_matches (&pattern);
126
127 lrdf_statement* current = matches;
128 while (current != 0) {
129 tags.push_back (current->object);
130
131 current = current->next;
132 }
133
134 lrdf_free_statements (matches);
135
136 sort (tags.begin(), tags.end());
137 free (uri);
138 #endif
139 return tags;
140 }
141
142 void
search_members_and(vector<string> & members,const vector<string> & tags)143 AudioLibrary::search_members_and (vector<string>& members, const vector<string>& tags)
144 {
145 #ifdef HAVE_LRDF
146 lrdf_statement **head;
147 lrdf_statement* pattern = 0;
148 lrdf_statement* old = 0;
149 head = &pattern;
150
151 vector<string>::const_iterator i;
152 for (i = tags.begin(); i != tags.end(); ++i){
153 pattern = new lrdf_statement;
154 pattern->subject = const_cast<char*>("?");
155 pattern->predicate = const_cast<char*>(TAG);
156 pattern->object = strdup((*i).c_str());
157 pattern->next = old;
158
159 old = pattern;
160 }
161
162 if (*head != 0) {
163 lrdf_uris* ulist = lrdf_match_multi(*head);
164 for (uint32_t j = 0; ulist && j < ulist->count; ++j) {
165 members.push_back(Glib::filename_from_uri(ulist->items[j]));
166 }
167 lrdf_free_uris(ulist);
168
169 sort (members.begin(), members.end());
170 members.erase (unique (members.begin(), members.end()), members.end());
171 }
172
173 // memory clean up
174 pattern = *head;
175 while(pattern){
176 free(pattern->object);
177 old = pattern;
178 pattern = pattern->next;
179 delete old;
180 }
181 #endif
182 }
183