2 Copyright (C) 2015-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
22 #include "recipients_panel.h"
24 #include "recipient_dialog.h"
25 #include "dcpomatic_button.h"
26 #include "lib/config.h"
36 using std::shared_ptr;
38 using boost::optional;
39 using namespace dcpomatic;
42 RecipientsPanel::RecipientsPanel (wxWindow* parent)
43 : wxPanel (parent, wxID_ANY)
44 , _ignore_selection_change (false)
46 auto sizer = new wxBoxSizer (wxVERTICAL);
49 int const height = 30;
51 int const height = -1;
54 _search = new wxSearchCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (200, height));
56 /* The cancel button seems to be strangely broken in GTK3; clicking on it twice sometimes works */
57 _search->ShowCancelButton (true);
59 sizer->Add (_search, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP);
61 auto targets = new wxBoxSizer (wxHORIZONTAL);
62 _targets = new wxTreeCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTR_HIDE_ROOT | wxTR_MULTIPLE | wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT);
63 targets->Add (_targets, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
67 auto target_buttons = new wxBoxSizer (wxVERTICAL);
69 _add_recipient = new Button (this, _("Add..."));
70 target_buttons->Add (_add_recipient, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
71 _edit_recipient = new Button (this, _("Edit..."));
72 target_buttons->Add (_edit_recipient, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
73 _remove_recipient = new Button (this, _("Remove"));
74 target_buttons->Add (_remove_recipient, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
76 targets->Add (target_buttons, 0, 0);
78 sizer->Add (targets, 1, wxEXPAND);
80 _search->Bind (wxEVT_TEXT, boost::bind (&RecipientsPanel::search_changed, this));
81 _targets->Bind (wxEVT_TREE_SEL_CHANGED, &RecipientsPanel::selection_changed_shim, this);
83 _add_recipient->Bind (wxEVT_BUTTON, boost::bind (&RecipientsPanel::add_recipient_clicked, this));
84 _edit_recipient->Bind (wxEVT_BUTTON, boost::bind (&RecipientsPanel::edit_recipient_clicked, this));
85 _remove_recipient->Bind (wxEVT_BUTTON, boost::bind (&RecipientsPanel::remove_recipient_clicked, this));
91 RecipientsPanel::~RecipientsPanel ()
93 _targets->Unbind (wxEVT_TREE_SEL_CHANGED, &RecipientsPanel::selection_changed_shim, this);
98 RecipientsPanel::setup_sensitivity ()
100 _edit_recipient->Enable (_selected.size() == 1);
101 _remove_recipient->Enable (_selected.size() >= 1);
106 RecipientsPanel::add_recipient (shared_ptr<DKDMRecipient> r)
108 string search = wx_to_std (_search->GetValue());
109 transform (search.begin(), search.end(), search.begin(), ::tolower);
111 if (!search.empty()) {
112 string name = r->name;
113 transform (name.begin(), name.end(), name.begin(), ::tolower);
114 if (name.find(search) == string::npos) {
119 _recipients[_targets->AppendItem(_root, std_to_wx(r->name))] = r;
121 _targets->SortChildren (_root);
126 RecipientsPanel::add_recipient_clicked ()
128 auto d = new RecipientDialog (GetParent(), _("Add recipient"));
129 if (d->ShowModal() == wxID_OK) {
130 auto r = std::make_shared<DKDMRecipient>(d->name(), d->notes(), d->recipient(), d->emails(), d->utc_offset_hour(), d->utc_offset_minute());
131 Config::instance()->add_dkdm_recipient (r);
140 RecipientsPanel::edit_recipient_clicked ()
142 if (_selected.size() != 1) {
146 auto c = *_selected.begin();
148 auto d = new RecipientDialog (
149 GetParent(), _("Edit recipient"), c.second->name, c.second->notes, c.second->emails, c.second->utc_offset_hour, c.second->utc_offset_minute, c.second->recipient
152 if (d->ShowModal () == wxID_OK) {
153 c.second->name = d->name ();
154 c.second->emails = d->emails ();
155 c.second->notes = d->notes ();
156 c.second->utc_offset_hour = d->utc_offset_hour ();
157 c.second->utc_offset_minute = d->utc_offset_minute ();
158 _targets->SetItemText (c.first, std_to_wx (d->name()));
159 Config::instance()->changed (Config::DKDM_RECIPIENTS);
167 RecipientsPanel::remove_recipient_clicked ()
169 for (auto const& i: _selected) {
170 Config::instance()->remove_dkdm_recipient (i.second);
171 _targets->Delete (i.first);
174 selection_changed ();
178 list<shared_ptr<DKDMRecipient>>
179 RecipientsPanel::recipients () const
181 list<shared_ptr<DKDMRecipient>> r;
183 for (auto const& i: _selected) {
184 r.push_back (i.second);
195 RecipientsPanel::selection_changed_shim (wxTreeEvent &)
197 selection_changed ();
202 RecipientsPanel::selection_changed ()
204 if (_ignore_selection_change) {
208 wxArrayTreeItemIds s;
209 _targets->GetSelections (s);
213 for (size_t i = 0; i < s.GetCount(); ++i) {
214 RecipientMap::const_iterator j = _recipients.find (s[i]);
215 if (j != _recipients.end ()) {
216 _selected[j->first] = j->second;
220 setup_sensitivity ();
221 RecipientsChanged ();
226 RecipientsPanel::add_recipients ()
228 _root = _targets->AddRoot ("Foo");
230 for (auto i: Config::instance()->dkdm_recipients()) {
237 RecipientsPanel::search_changed ()
239 _targets->DeleteAllItems ();
240 _recipients.clear ();
244 _ignore_selection_change = true;
246 for (auto const& i: _selected) {
247 /* The wxTreeItemIds will now be different, so we must search by recipient */
248 auto j = _recipients.begin ();
249 while (j != _recipients.end() && j->second != i.second) {
253 if (j != _recipients.end ()) {
254 _targets->SelectItem (j->first);
258 _ignore_selection_change = false;