summaryrefslogtreecommitdiff
path: root/libs/ardour/audio_library.cc
blob: 2f09a37633062d55fc13f0be90c864c08a3be54c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
    Copyright (C) 2003-2006 Paul Davis

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#ifdef WAF_BUILD
#include "libardour-config.h"
#endif

#include <sstream>

#include <libxml/uri.h>

#ifdef HAVE_LRDF
#include <lrdf.h>
#endif

#include <glibmm/miscutils.h>

#include <glibmm/convert.h>

#include "pbd/compose.h"
#include "pbd/error.h"
#include "pbd/file_utils.h"

#include "ardour/audio_library.h"
#include "ardour/filesystem_paths.h"

#include "pbd/i18n.h"

using namespace std;
using namespace PBD;
using namespace ARDOUR;

namespace {
	const char* const sfdb_file_name = "sfdb";
} // anonymous namespace

static const char* TAG = "http://ardour.org/ontology/Tag";

AudioLibrary::AudioLibrary ()
{
	std::string sfdb_file_path(user_config_directory ());

	sfdb_file_path = Glib::build_filename (sfdb_file_path, sfdb_file_name);

	src = Glib::filename_to_uri (sfdb_file_path);

	// workaround for possible bug in raptor that crashes when saving to a
	// non-existant file.

	touch_file(sfdb_file_path);

#ifdef HAVE_LRDF
	lrdf_read_file(src.c_str());
#endif
}

AudioLibrary::~AudioLibrary ()
{
}

void
AudioLibrary::save_changes ()
{
#ifdef HAVE_LRDF
	if (lrdf_export_by_source(src.c_str(), src.substr(5).c_str())) {
		PBD::warning << string_compose(_("Could not open %1.  Audio Library not saved"), src) << endmsg;
	}
#endif
}

void
AudioLibrary::set_tags (string member, vector<string> tags)
{
#ifdef HAVE_LRDF
	sort (tags.begin(), tags.end());
	tags.erase (unique(tags.begin(), tags.end()), tags.end());

	const string file_uri(Glib::filename_to_uri (member));

	lrdf_remove_uri_matches (file_uri.c_str());

	for (vector<string>::iterator i = tags.begin(); i != tags.end(); ++i) {
		lrdf_add_triple (src.c_str(), file_uri.c_str(), TAG, (*i).c_str(), lrdf_literal);
	}
#endif
}

vector<string>
AudioLibrary::get_tags (string member)
{
	vector<string> tags;
#ifdef HAVE_LRDF
	char * uri = strdup(Glib::filename_to_uri(member).c_str());

	lrdf_statement pattern;
	pattern.subject = uri;
	pattern.predicate = const_cast<char*>(TAG);
	pattern.object = 0;
	pattern.object_type = lrdf_literal;

	lrdf_statement* matches = lrdf_matches (&pattern);

	lrdf_statement* current = matches;
	while (current != 0) {
		tags.push_back (current->object);

		current = current->next;
	}

	lrdf_free_statements (matches);

	sort (tags.begin(), tags.end());
	free (uri);
#endif
	return tags;
}

void
AudioLibrary::search_members_and (vector<string>& members, const vector<string>& tags)
{
#ifdef HAVE_LRDF
	lrdf_statement **head;
	lrdf_statement* pattern = 0;
	lrdf_statement* old = 0;
	head = &pattern;

	vector<string>::const_iterator i;
	for (i = tags.begin(); i != tags.end(); ++i){
		pattern = new lrdf_statement;
		pattern->subject = const_cast<char*>("?");
		pattern->predicate = const_cast<char*>(TAG);
		pattern->object = strdup((*i).c_str());
		pattern->next = old;

		old = pattern;
	}

	if (*head != 0) {
		lrdf_uris* ulist = lrdf_match_multi(*head);
		for (uint32_t j = 0; ulist && j < ulist->count; ++j) {
			members.push_back(Glib::filename_from_uri(ulist->items[j]));
		}
		lrdf_free_uris(ulist);

	    sort(members.begin(), members.end());
	    unique(members.begin(), members.end());
	}

	// memory clean up
	pattern = *head;
	while(pattern){
		free(pattern->object);
		old = pattern;
		pattern = pattern->next;
		delete old;
	}
#endif
}