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