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