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