+
+void
+VSTPlugin::find_presets ()
+{
+ /* Built-in presets */
+
+ int const vst_version = _plugin->dispatcher (_plugin, effGetVstVersion, 0, 0, NULL, 0);
+ for (int i = 0; i < _plugin->numPrograms; ++i) {
+ PresetRecord r (string_compose (X_("VST:%1:%2"), unique_id (), i), "", -1, false);
+
+ if (vst_version >= 2) {
+ char buf[256];
+ if (_plugin->dispatcher (_plugin, 29, i, 0, buf, 0) == 1) {
+ r.label = buf;
+ } else {
+ r.label = string_compose (_("Preset %1"), i);
+ }
+ } else {
+ r.label = string_compose (_("Preset %1"), i);
+ }
+
+ _presets.insert (make_pair (r.uri, r));
+ }
+
+ /* User presets from our XML file */
+
+ boost::shared_ptr<XMLTree> t (presets_tree ());
+
+ if (t) {
+ XMLNode* root = t->root ();
+ for (XMLNodeList::const_iterator i = root->children().begin(); i != root->children().end(); ++i) {
+
+ XMLProperty* uri = (*i)->property (X_("uri"));
+ XMLProperty* label = (*i)->property (X_("label"));
+
+ assert (uri);
+ assert (label);
+
+ PresetRecord r (uri->value(), label->value(), -1, true);
+ _presets.insert (make_pair (r.uri, r));
+ }
+ }
+
+}
+
+/** @return XMLTree with our user presets; could be a new one if no existing
+ * one was found, or 0 if one was present but badly-formatted.
+ */
+XMLTree *
+VSTPlugin::presets_tree () const
+{
+ XMLTree* t = new XMLTree;
+
+ std::string p = Glib::build_filename (ARDOUR::user_config_directory (), "presets");
+
+ if (!Glib::file_test (p, Glib::FILE_TEST_IS_DIR)) {
+ if (g_mkdir_with_parents (p.c_str(), 0755) != 0) {
+ error << _("Unable to make VST presets directory") << endmsg;
+ };
+ }
+
+ p = Glib::build_filename (p, presets_file ());
+
+ if (!Glib::file_test (p, Glib::FILE_TEST_EXISTS)) {
+ t->set_root (new XMLNode (X_("VSTPresets")));
+ return t;
+ }
+
+ t->set_filename (p);
+ if (!t->read ()) {
+ delete t;
+ return 0;
+ }
+
+ return t;
+}
+
+/** @return Index of the first user preset in our lists */
+int
+VSTPlugin::first_user_preset_index () const
+{
+ return _plugin->numPrograms;
+}
+
+string
+VSTPlugin::presets_file () const
+{
+ return string_compose ("vst-%1", unique_id ());
+}
+