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