fix crash when copy'ing latent plugins
[ardour.git] / libs / ardour / audio_library.cc
index 92069d929ff7c25ea3af2916256d8bc4234b76ec..2f09a37633062d55fc13f0be90c864c08a3be54c 100644 (file)
 
 */
 
+#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/compose.h"
+#include "pbd/error.h"
+#include "pbd/file_utils.h"
 
-#include <ardour/audio_library.h>
-#include <ardour/utils.h>
+#include "ardour/audio_library.h"
+#include "ardour/filesystem_paths.h"
 
-#include "i18n.h"
+#include "pbd/i18n.h"
 
 using namespace std;
+using namespace PBD;
 using namespace ARDOUR;
 
-static char* TAG = "http://ardour.org/ontology/Tag";
+namespace {
+       const char* const sfdb_file_name = "sfdb";
+} // anonymous namespace
+
+static const char* TAG = "http://ardour.org/ontology/Tag";
 
 AudioLibrary::AudioLibrary ()
 {
-       src = "file:" + get_user_ardour_path() + "sfdb";
+       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(get_user_ardour_path() + "sfdb");
 
+       touch_file(sfdb_file_path);
+
+#ifdef HAVE_LRDF
        lrdf_read_file(src.c_str());
+#endif
 }
 
 AudioLibrary::~AudioLibrary ()
@@ -53,76 +77,64 @@ 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;
        }
-}
-
-string
-AudioLibrary::path2uri (string path)
-{
-       xmlURI temp;
-       memset(&temp, 0, sizeof(temp));
-       
-       xmlChar *cal = xmlCanonicPath((xmlChar*) path.c_str());
-       temp.path = (char *) cal;
-       xmlChar *ret = xmlSaveUri(&temp);
-       xmlFree(cal);
-       
-       stringstream uri;
-       uri << "file:" << (const char*) ret;
-       
-       xmlFree (ret);
-       
-       return uri.str();
+#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());
-       
-       string file_uri(path2uri(member));
-       
+
+       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 = strdup(path2uri(member).c_str());
-       pattern.predicate = TAG;
+       pattern.subject = uri;
+       pattern.predicate = const_cast<char*>(TAG);
        pattern.object = 0;
        pattern.object_type = lrdf_literal;
-       
+
        lrdf_statement* matches = lrdf_matches (&pattern);
-       free (pattern.subject);
-       
+
        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)
+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;
@@ -131,8 +143,8 @@ AudioLibrary::search_members_and (vector<string>& members, const vector<string>
        vector<string>::const_iterator i;
        for (i = tags.begin(); i != tags.end(); ++i){
                pattern = new lrdf_statement;
-               pattern->subject = "?";
-               pattern->predicate = TAG;
+               pattern->subject = const_cast<char*>("?");
+               pattern->predicate = const_cast<char*>(TAG);
                pattern->object = strdup((*i).c_str());
                pattern->next = old;
 
@@ -142,8 +154,7 @@ AudioLibrary::search_members_and (vector<string>& members, const vector<string>
        if (*head != 0) {
                lrdf_uris* ulist = lrdf_match_multi(*head);
                for (uint32_t j = 0; ulist && j < ulist->count; ++j) {
-//                     printf("AND: %s\n", ulist->items[j]);
-                       members.push_back(ulist->items[j]);
+                       members.push_back(Glib::filename_from_uri(ulist->items[j]));
                }
                lrdf_free_uris(ulist);
 
@@ -154,33 +165,10 @@ AudioLibrary::search_members_and (vector<string>& members, const vector<string>
        // memory clean up
        pattern = *head;
        while(pattern){
-               free(pattern->predicate);
                free(pattern->object);
                old = pattern;
                pattern = pattern->next;
                delete old;
        }
-}
-
-bool
-AudioLibrary::safe_file_extension(string file)
-{
-       return !(file.rfind(".wav") == string::npos &&
-               file.rfind(".aiff")== string::npos &&
-               file.rfind(".aif") == string::npos &&
-               file.rfind(".snd") == string::npos &&
-               file.rfind(".au")  == string::npos &&
-               file.rfind(".raw") == string::npos &&
-               file.rfind(".sf")  == string::npos &&
-               file.rfind(".cdr") == string::npos &&
-               file.rfind(".smp") == string::npos &&
-               file.rfind(".maud")== string::npos &&
-               file.rfind(".vwe") == string::npos &&
-               file.rfind(".paf") == string::npos &&
-#ifdef HAVE_COREAUDIO
-               file.rfind(".mp3") == string::npos &&
-               file.rfind(".aac") == string::npos &&
-               file.rfind(".mp4") == string::npos &&
-#endif // HAVE_COREAUDIO
-               file.rfind(".voc") == string::npos);
+#endif
 }