943d55c6c08a7191157bcbc2827acc7cf395bf25
[dcpomatic.git] / src / wx / recipients_panel.cc
1 /*
2     Copyright (C) 2015-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21
22 #include "recipients_panel.h"
23 #include "wx_util.h"
24 #include "recipient_dialog.h"
25 #include "dcpomatic_button.h"
26 #include "lib/config.h"
27 #include <list>
28 #include <iostream>
29
30
31 using std::cout;
32 using std::list;
33 using std::make_pair;
34 using std::map;
35 using std::pair;
36 using std::shared_ptr;
37 using std::string;
38 using boost::optional;
39 using namespace dcpomatic;
40
41
42 RecipientsPanel::RecipientsPanel (wxWindow* parent)
43         : wxPanel (parent, wxID_ANY)
44         , _ignore_selection_change (false)
45 {
46         auto sizer = new wxBoxSizer (wxVERTICAL);
47
48 #ifdef __WXGTK3__
49         int const height = 30;
50 #else
51         int const height = -1;
52 #endif
53
54         _search = new wxSearchCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (200, height));
55 #ifndef __WXGTK3__
56         /* The cancel button seems to be strangely broken in GTK3; clicking on it twice sometimes works */
57         _search->ShowCancelButton (true);
58 #endif
59         sizer->Add (_search, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP);
60
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);
64
65         add_recipients ();
66
67         auto target_buttons = new wxBoxSizer (wxVERTICAL);
68
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);
75
76         targets->Add (target_buttons, 0, 0);
77
78         sizer->Add (targets, 1, wxEXPAND);
79
80         _search->Bind  (wxEVT_TEXT, boost::bind (&RecipientsPanel::search_changed, this));
81         _targets->Bind (wxEVT_TREE_SEL_CHANGED, &RecipientsPanel::selection_changed_shim, this);
82
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));
86
87         SetSizer (sizer);
88 }
89
90
91 RecipientsPanel::~RecipientsPanel ()
92 {
93         _targets->Unbind (wxEVT_TREE_SEL_CHANGED, &RecipientsPanel::selection_changed_shim, this);
94 }
95
96
97 void
98 RecipientsPanel::setup_sensitivity ()
99 {
100         _edit_recipient->Enable (_selected.size() == 1);
101         _remove_recipient->Enable (_selected.size() >= 1);
102 }
103
104
105 void
106 RecipientsPanel::add_recipient (shared_ptr<DKDMRecipient> r)
107 {
108         string search = wx_to_std (_search->GetValue());
109         transform (search.begin(), search.end(), search.begin(), ::tolower);
110
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) {
115                         return;
116                 }
117         }
118
119         _recipients[_targets->AppendItem(_root, std_to_wx(r->name))] = r;
120
121         _targets->SortChildren (_root);
122 }
123
124
125 void
126 RecipientsPanel::add_recipient_clicked ()
127 {
128         RecipientDialog dialog(GetParent(), _("Add recipient"));
129         if (dialog.ShowModal() == wxID_OK) {
130                 auto r = std::make_shared<DKDMRecipient>(dialog.name(), dialog.notes(), dialog.recipient(), dialog.emails(), dialog.utc_offset_hour(), dialog.utc_offset_minute());
131                 Config::instance()->add_dkdm_recipient (r);
132                 add_recipient (r);
133         }
134 }
135
136
137 void
138 RecipientsPanel::edit_recipient_clicked ()
139 {
140         if (_selected.size() != 1) {
141                 return;
142         }
143
144         auto c = *_selected.begin();
145
146         RecipientDialog dialog(
147                 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
148                 );
149
150         if (dialog.ShowModal() == wxID_OK) {
151                 c.second->name = dialog.name();
152                 c.second->emails = dialog.emails();
153                 c.second->notes = dialog.notes();
154                 c.second->utc_offset_hour = dialog.utc_offset_hour();
155                 c.second->utc_offset_minute = dialog.utc_offset_minute();
156                 _targets->SetItemText(c.first, std_to_wx(dialog.name()));
157                 Config::instance()->changed (Config::DKDM_RECIPIENTS);
158         }
159 }
160
161
162 void
163 RecipientsPanel::remove_recipient_clicked ()
164 {
165         for (auto const& i: _selected) {
166                 Config::instance()->remove_dkdm_recipient (i.second);
167                 _targets->Delete (i.first);
168         }
169
170         selection_changed ();
171 }
172
173
174 list<shared_ptr<DKDMRecipient>>
175 RecipientsPanel::recipients () const
176 {
177         list<shared_ptr<DKDMRecipient>> r;
178
179         for (auto const& i: _selected) {
180                 r.push_back (i.second);
181         }
182
183         r.sort ();
184         r.unique ();
185
186         return r;
187 }
188
189
190 void
191 RecipientsPanel::selection_changed_shim (wxTreeEvent &)
192 {
193         selection_changed ();
194 }
195
196
197 void
198 RecipientsPanel::selection_changed ()
199 {
200         if (_ignore_selection_change) {
201                 return;
202         }
203
204         wxArrayTreeItemIds s;
205         _targets->GetSelections (s);
206
207         _selected.clear ();
208
209         for (size_t i = 0; i < s.GetCount(); ++i) {
210                 RecipientMap::const_iterator j = _recipients.find (s[i]);
211                 if (j != _recipients.end ()) {
212                         _selected[j->first] = j->second;
213                 }
214         }
215
216         setup_sensitivity ();
217         RecipientsChanged ();
218 }
219
220
221 void
222 RecipientsPanel::add_recipients ()
223 {
224         _root = _targets->AddRoot ("Foo");
225
226         for (auto i: Config::instance()->dkdm_recipients()) {
227                 add_recipient (i);
228         }
229 }
230
231
232 void
233 RecipientsPanel::search_changed ()
234 {
235         _targets->DeleteAllItems ();
236         _recipients.clear ();
237
238         add_recipients ();
239
240         _ignore_selection_change = true;
241
242         for (auto const& i: _selected) {
243                 /* The wxTreeItemIds will now be different, so we must search by recipient */
244                 auto j = _recipients.begin ();
245                 while (j != _recipients.end() && j->second != i.second) {
246                         ++j;
247                 }
248
249                 if (j != _recipients.end ()) {
250                         _targets->SelectItem (j->first);
251                 }
252         }
253
254         _ignore_selection_change = false;
255 }