From: Carl Hetherington Date: Thu, 7 Mar 2024 22:33:03 +0000 (+0100) Subject: Fix screen name order in KDM emails. X-Git-Tag: v2.17.13~12^2 X-Git-Url: https://git.carlh.net/gitweb/?p=dcpomatic.git;a=commitdiff_plain;h=04b5957318df591f56e0a5d39720df143dc8230d Fix screen name order in KDM emails. --- diff --git a/src/lib/kdm_with_metadata.cc b/src/lib/kdm_with_metadata.cc index 971eeac76..f7ff84435 100644 --- a/src/lib/kdm_with_metadata.cc +++ b/src/lib/kdm_with_metadata.cc @@ -238,14 +238,13 @@ send_emails ( auto subject = substitute_variables(config->kdm_subject()); auto body = substitute_variables(config->kdm_email()); - string screens; + vector screens; for (auto kdm: kdms_for_cinema) { - auto screen_name = kdm->get('s'); - if (screen_name) { - screens += *screen_name + ", "; + if (auto screen_name = kdm->get('s')) { + screens.push_back(*screen_name); } } - boost::algorithm::replace_all (body, "$SCREENS", screens.substr (0, screens.length() - 2)); + boost::algorithm::replace_all(body, "$SCREENS", screen_names_to_string(screens)); auto emails = first->emails(); std::copy(extra_addresses.begin(), extra_addresses.end(), std::back_inserter(emails)); diff --git a/src/lib/util.cc b/src/lib/util.cc index 7f6e9da5a..fe6602de3 100644 --- a/src/lib/util.cc +++ b/src/lib/util.cc @@ -1123,3 +1123,31 @@ word_wrap(string input, int columns) return output; } + +string +screen_names_to_string(vector names) +{ + if (names.empty()) { + return {}; + } + + auto number = [](string const& s) { + return s.find_first_not_of("0123456789") == string::npos; + }; + + if (std::find_if(names.begin(), names.end(), [number](string const& s) { return !number(s); }) == names.end()) { + std::sort(names.begin(), names.end(), [](string const& a, string const& b) { + return dcp::raw_convert(a) < dcp::raw_convert(b); + }); + } else { + std::sort(names.begin(), names.end()); + } + + string result; + for (auto const& name: names) { + result += name + ", "; + } + + return result.substr(0, result.length() - 2); +} + diff --git a/src/lib/util.h b/src/lib/util.h index b92869b25..fd6fd6164 100644 --- a/src/lib/util.h +++ b/src/lib/util.h @@ -97,5 +97,6 @@ extern std::string error_details(boost::system::error_code ec); extern bool contains_assetmap(boost::filesystem::path dir); extern std::string word_wrap(std::string input, int columns); extern void capture_ffmpeg_logs(); +extern std::string screen_names_to_string(std::vector names); #endif diff --git a/test/util_test.cc b/test/util_test.cc index 49d0b3bc2..afcc4cfc9 100644 --- a/test/util_test.cc +++ b/test/util_test.cc @@ -155,3 +155,14 @@ BOOST_AUTO_TEST_CASE(word_wrap_test) BOOST_CHECK(word_wrap("hello this is a longer bit of text and it should be word-wrapped", 31) == string{"hello this is a longer bit of \ntext and it should be word-\nwrapped\n"}); BOOST_CHECK_EQUAL(word_wrap("hellocan'twrapthissadly", 5), "hello\ncan't\nwrapt\nhissa\ndly\n"); } + + +BOOST_AUTO_TEST_CASE(screen_names_to_string_test) +{ + BOOST_CHECK_EQUAL(screen_names_to_string({"1", "2", "3"}), "1, 2, 3"); + BOOST_CHECK_EQUAL(screen_names_to_string({"3", "2", "1"}), "1, 2, 3"); + BOOST_CHECK_EQUAL(screen_names_to_string({"39", "3", "10", "1", "2"}), "1, 2, 3, 10, 39"); + BOOST_CHECK_EQUAL(screen_names_to_string({"Sheila", "Fred", "Jim"}), "Fred, Jim, Sheila"); + BOOST_CHECK_EQUAL(screen_names_to_string({"Sheila", "Fred", "Jim", "1"}), "1, Fred, Jim, Sheila"); +} +