enough with umpteen "i18n.h" files. Consolidate on pbd/i18n.h
[ardour.git] / libs / surfaces / mackie / device_profile.cc
index 04975270abe41ea4a90875f86647c487ea6ac7f9..ca3f14680359cd07e9cea925a64a88610bd0be8e 100644 (file)
@@ -24,7 +24,8 @@
 
 #include "pbd/xml++.h"
 #include "pbd/error.h"
-#include "pbd/pathscanner.h"
+#include "pbd/file_utils.h"
+#include "pbd/stl_delete.h"
 #include "pbd/replace_all.h"
 
 #include "ardour/filesystem_paths.h"
 #include "mackie_control_protocol.h"
 #include "device_profile.h"
 
-#include "i18n.h"
+#include "pbd/i18n.h"
 
-using namespace Mackie;
 using namespace PBD;
 using namespace ARDOUR;
+using namespace ArdourSurface;
+using namespace Mackie;
+
 using std::string;
 using std::vector;
 
 std::map<std::string,DeviceProfile> DeviceProfile::device_profiles;
+const std::string DeviceProfile::edited_indicator (" (edited)");
+const std::string DeviceProfile::default_profile_name ("User");
 
 DeviceProfile::DeviceProfile (const string& n)
        : _name (n)
+       , edited (false)
 {
 }
 
@@ -55,38 +61,30 @@ static const char * const devprofile_env_variable_name = "ARDOUR_MCP_PATH";
 static const char* const devprofile_dir_name = "mcp";
 static const char* const devprofile_suffix = ".profile";
 
-static sys::path
-system_devprofile_search_path ()
+static Searchpath
+devprofile_search_path ()
 {
        bool devprofile_path_defined = false;
-        sys::path spath_env (Glib::getenv (devprofile_env_variable_name, devprofile_path_defined));
+        std::string spath_env (Glib::getenv (devprofile_env_variable_name, devprofile_path_defined));
 
        if (devprofile_path_defined) {
                return spath_env;
        }
 
-       SearchPath spath (system_data_search_path());
+       Searchpath spath (ardour_data_search_path());
        spath.add_subdirectory_to_paths(devprofile_dir_name);
 
-       // just return the first directory in the search path that exists
-       SearchPath::const_iterator i = std::find_if(spath.begin(), spath.end(), sys::exists);
-
-       if (i == spath.end()) return sys::path();
-
-       return *i;
+       return spath;
 }
 
-static sys::path
+static std::string
 user_devprofile_directory ()
 {
-       sys::path p(user_config_directory());
-       p /= devprofile_dir_name;
-
-       return p;
+       return Glib::build_filename (user_config_directory(), devprofile_dir_name);
 }
 
 static bool
-devprofile_filter (const string &str, void */*arg*/)
+devprofile_filter (const string &str, void/*arg*/)
 {
        return (str.length() > strlen(devprofile_suffix) &&
                str.find (devprofile_suffix) == (str.length() - strlen (devprofile_suffix)));
@@ -95,33 +93,24 @@ devprofile_filter (const string &str, void */*arg*/)
 void
 DeviceProfile::reload_device_profiles ()
 {
-       DeviceProfile dp;
        vector<string> s;
-       vector<string *> *devprofiles;
-       PathScanner scanner;
-       SearchPath spath (system_devprofile_search_path());
-       spath += user_devprofile_directory ();
+       vector<string> devprofiles;
+       Searchpath spath (devprofile_search_path());
 
-       devprofiles = scanner (spath.to_string(), devprofile_filter, 0, false, true);
+       find_files_matching_filter (devprofiles, spath, devprofile_filter, 0, false, true);
        device_profiles.clear ();
 
-       if (!devprofiles) {
+       if (devprofiles.empty()) {
                error << "No MCP device info files found using " << spath.to_string() << endmsg;
                return;
        }
 
-       if (devprofiles->empty()) {
-               error << "No MCP device info files found using " << spath.to_string() << endmsg;
-               return;
-       }
-
-       for (vector<string*>::iterator i = devprofiles->begin(); i != devprofiles->end(); ++i) {
-               string fullpath = *(*i);
+       for (vector<string>::iterator i = devprofiles.begin(); i != devprofiles.end(); ++i) {
+               string fullpath = *i;
+               DeviceProfile dp; // has to be initial every loop or info from last added.
 
                XMLTree tree;
 
-               std::cerr << "Loading " << fullpath << std::endl;
-               
                if (!tree.read (fullpath.c_str())) {
                        continue;
                }
@@ -136,8 +125,6 @@ DeviceProfile::reload_device_profiles ()
                        device_profiles[dp.name()] = dp;
                }
        }
-
-       delete devprofiles;
 }
 
 int
@@ -151,7 +138,7 @@ DeviceProfile::set_state (const XMLNode& node, int /* version */)
        }
 
        /* name is mandatory */
+
        if ((child = node.child ("Name")) == 0 || (prop = child->property ("value")) == 0) {
                return -1;
        } else {
@@ -173,7 +160,7 @@ DeviceProfile::set_state (const XMLNode& node, int /* version */)
 
                                int id = Button::name_to_id (prop->value());
                                if (id < 0) {
-                                       error << string_compose ("Unknow button ID \"%1\"", prop->value()) << endmsg;
+                                       error << string_compose ("Unknown button ID \"%1\"", prop->value()) << endmsg;
                                        continue;
                                }
 
@@ -207,6 +194,8 @@ DeviceProfile::set_state (const XMLNode& node, int /* version */)
                }
        }
 
+       edited = false;
+
        return 0;
 }
 
@@ -216,7 +205,7 @@ DeviceProfile::get_state () const
        XMLNode* node = new XMLNode ("MackieDeviceProfile");
        XMLNode* child = new XMLNode ("Name");
 
-       child->add_property ("value", _name);
+       child->add_property ("value", name());
        node->add_child_nocopy (*child);
 
        if (_button_map.empty()) {
@@ -308,13 +297,31 @@ DeviceProfile::set_button_action (Button::ID id, int modifier_state, const strin
                i->second.plain = action;
        }
 
+       edited = true;
+
        save ();
 }
 
-const string&
+string
+DeviceProfile::name_when_edited (string const& base)
+{
+       return string_compose ("%1 %2", base, edited_indicator);
+}
+
+string
 DeviceProfile::name() const
 {
-       return _name;
+       if (edited) {
+               if (_name.find (edited_indicator) == string::npos) {
+                       /* modify name to included edited indicator */
+                       return name_when_edited (_name);
+               } else {
+                       /* name already contains edited indicator */
+                       return _name;
+               }
+       } else {
+               return _name;
+       }
 }
 
 void
@@ -347,20 +354,19 @@ legalize_for_path (const string& str)
 void
 DeviceProfile::save ()
 {
-       sys::path fullpath = user_devprofile_directory();
+       std::string fullpath = user_devprofile_directory();
 
-       if (g_mkdir_with_parents (fullpath.to_string().c_str(), 0755) < 0) {
-               error << string_compose(_("Session: cannot create user MCP profile folder \"%1\" (%2)"), fullpath.to_string(), strerror (errno)) << endmsg;
+       if (g_mkdir_with_parents (fullpath.c_str(), 0755) < 0) {
+               error << string_compose(_("Session: cannot create user MCP profile folder \"%1\" (%2)"), fullpath, strerror (errno)) << endmsg;
                return;
        }
 
-       fullpath /= legalize_for_path (_name) + ".profile";
-       
+       fullpath = Glib::build_filename (fullpath, string_compose ("%1%2", legalize_for_path (name()), devprofile_suffix));
+
        XMLTree tree;
        tree.set_root (&get_state());
 
-       if (!tree.write (fullpath.to_string())) {
-               error << string_compose ("MCP profile not saved to %1", fullpath.to_string()) << endmsg;
+       if (!tree.write (fullpath)) {
+               error << string_compose ("MCP profile not saved to %1", fullpath) << endmsg;
        }
 }
-