+ dev_info = get_device_info(dev);
+
+ // If present, vendor and model always have to match.
+ if (dev_info.count("vendor") > 0 && search_info.count("vendor") > 0)
+ if (dev_info.at("vendor") != search_info.at("vendor")) continue;
+
+ if (dev_info.count("model") > 0 && search_info.count("model") > 0)
+ if (dev_info.at("model") != search_info.at("model")) continue;
+
+ // Most unique match: vendor/model/serial_num (but don't match a S/N of 0)
+ if ((dev_info.count("serial_num") > 0) && (dev_info.at("serial_num") != "0")
+ && search_info.count("serial_num") > 0)
+ if (dev_info.at("serial_num") == search_info.at("serial_num") &&
+ dev_info.at("serial_num") != "0")
+ return dev;
+
+ // Second best match: vendor/model/connection_id
+ if (dev_info.count("connection_id") > 0 &&
+ search_info.count("connection_id") > 0)
+ if (dev_info.at("connection_id") == search_info.at("connection_id"))
+ return dev;
+
+ // Last resort: vendor/model/version
+ if (dev_info.count("version") > 0 &&
+ search_info.count("version") > 0)
+ if (dev_info.at("version") == search_info.at("version") &&
+ dev_info.at("version") != "0")
+ return dev;
+
+ // For this device, we merely have a vendor/model match.
+ last_resort_dev = dev;
+ }
+
+ // If there wasn't even a vendor/model/version match, we end up here.
+ // This is usually the case for devices with only vendor/model data.
+ // The selected device may be wrong with multiple such devices attached
+ // but it is the best we can do at this point. After all, there may be
+ // only one such device and we do want to select it in this case.
+ return last_resort_dev;
+}
+
+void DeviceManager::build_display_name(shared_ptr<Device> device)
+{
+ auto session_device = dynamic_pointer_cast<SessionDevice>(device);
+ auto hardware_device = dynamic_pointer_cast<HardwareDevice>(device);
+
+ if (session_device) {
+ full_names_[device] = display_names_[device] =
+ boost::filesystem::path(
+ session_device->parent()->filename()).filename().string();
+ return;
+ }
+
+ // First, build the device's full name. It always contains all
+ // possible information.
+ vector<string> parts = {device->vendor(), device->model(),
+ device->version(), device->serial_number()};
+
+ if (device->connection_id().length() > 0)
+ parts.push_back("("+device->connection_id()+")");
+
+ full_names_[device] = join(parts, " ");
+
+ // Next, build the display name. It only contains fields as required.
+
+ // If we can find another device with the same model/vendor then
+ // we have at least two such devices and need to distinguish them.
+ const bool multiple_dev = hardware_device && any_of(
+ devices_.begin(), devices_.end(),
+ [&](shared_ptr<HardwareDevice> dev) {
+ return (dev->vendor() == hardware_device->vendor() &&
+ dev->model() == hardware_device->model()) &&
+ dev != hardware_device;
+ } );
+
+ parts = {device->vendor(), device->model()};
+
+ if (multiple_dev) {
+ parts.push_back(device->version());
+ parts.push_back(device->serial_number());
+
+ if ((device->serial_number().length() == 0) &&
+ (device->connection_id().length() > 0))
+ parts.push_back("("+device->connection_id()+")");