mackie: try to improve logic and management of device profiles
authorPaul Davis <paul@linuxaudiosystems.com>
Thu, 4 Feb 2016 16:23:54 +0000 (11:23 -0500)
committerPaul Davis <paul@linuxaudiosystems.com>
Thu, 4 Feb 2016 16:24:26 +0000 (11:24 -0500)
libs/surfaces/mackie/device_profile.cc
libs/surfaces/mackie/device_profile.h
libs/surfaces/mackie/mackie_control_protocol.cc
libs/surfaces/mackie/mackie_control_protocol.h

index 2b9cbd5edb2d462a933fd5d2d61cfabdd7d5db39..f67aa3cddd25ad491fea8d441a0dd884febc4dd7 100644 (file)
@@ -44,9 +44,12 @@ 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)
 {
 }
 
@@ -191,6 +194,8 @@ DeviceProfile::set_state (const XMLNode& node, int /* version */)
                }
        }
 
+       edited = false;
+
        return 0;
 }
 
@@ -200,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()) {
@@ -292,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
@@ -338,7 +361,7 @@ DeviceProfile::save ()
                return;
        }
 
-       fullpath = Glib::build_filename (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());
@@ -347,4 +370,3 @@ DeviceProfile::save ()
                error << string_compose ("MCP profile not saved to %1", fullpath) << endmsg;
        }
 }
-
index ab1e645e56f1ecbe06c319a9847a01dcccbbdfd1..3224b074ad1ef3b5b9a9f3cc7ee1f167d576cc3d 100644 (file)
@@ -41,11 +41,13 @@ class DeviceProfile
        std::string get_button_action (Button::ID, int modifier_state) const;
        void set_button_action (Button::ID, int modifier_state, const std::string&);
 
-       const std::string& name() const;
+       std::string name() const;
        void set_path (const std::string&);
 
        static void reload_device_profiles ();
        static std::map<std::string,DeviceProfile> device_profiles;
+       static std::string name_when_edited (std::string const& name);
+       static const std::string default_profile_name;
 
   private:
        struct ButtonActions {
@@ -62,6 +64,9 @@ class DeviceProfile
        std::string _name;
        std::string _path;
        ButtonActionMap _button_map;
+       bool edited;
+
+       static const std::string edited_indicator;
 
        int set_state (const XMLNode&, int version);
        XMLNode& get_state () const;
index d9fa3340c7e0e41415aa1112b489e09f18154ffe..a898746112b84cbaef976722d78fd85be8836a08 100644 (file)
@@ -1031,6 +1031,12 @@ MackieControlProtocol::get_state()
        return node;
 }
 
+bool
+MackieControlProtocol::profile_exists (string const & name) const
+{
+       return DeviceProfile::device_profiles.find (name) != DeviceProfile::device_profiles.end();
+}
+
 int
 MackieControlProtocol::set_state (const XMLNode & node, int version)
 {
@@ -1061,13 +1067,38 @@ MackieControlProtocol::set_state (const XMLNode & node, int version)
                if (prop->value().empty()) {
                        string default_profile_name;
 
-                       default_profile_name = Glib::get_user_name();
-                       default_profile_name += ' ';
-                       default_profile_name += _device_info.name();
+                       /* start by looking for a user-edited profile for the current device name */
+
+                       default_profile_name = DeviceProfile::name_when_edited (_device_info.name());
+
+                       if (!profile_exists (default_profile_name)) {
+
+                               /* no user-edited profile for this device name, so try the user-edited default profile */
+
+                               default_profile_name = DeviceProfile::name_when_edited (DeviceProfile::default_profile_name);
+
+                               if (!profile_exists (default_profile_name)) {
+
+                                       /* no user-edited version, so just try the device name */
+
+                                       default_profile_name = _device_info.name();
+
+                                       if (!profile_exists (default_profile_name)) {
+
+                                               /* no generic device specific profile, just try the fixed default */
+                                               default_profile_name = DeviceProfile::default_profile_name;
+                                       }
+                               }
+                       }
 
                        set_profile (default_profile_name);
+
                } else {
-                       set_profile (prop->value());
+                       if (profile_exists (prop->value())) {
+                               set_profile (prop->value());
+                       } else {
+                               set_profile (DeviceProfile::default_profile_name);
+                       }
                }
        }
 
index d3e17d0fcce46621404eb36d606f9367befcf527..667f0c36cfb0b98af1c9507ef813a6314abe571a 100644 (file)
@@ -314,6 +314,8 @@ class MackieControlProtocol
 
        static MackieControlProtocol* _instance;
 
+       bool profile_exists (std::string const&) const;
+
        Mackie::DeviceInfo       _device_info;
        Mackie::DeviceProfile    _device_profile;
        sigc::connection          periodic_connection;