Add a crazy amount of missed files from the previous commit.
[dcpomatic.git] / src / wx / recipients_panel.cc
1 /*
2     Copyright (C) 2015-2020 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 #include "recipients_panel.h"
22 #include "wx_util.h"
23 #include "recipient_dialog.h"
24 #include "dcpomatic_button.h"
25 #include "lib/config.h"
26 #include <boost/foreach.hpp>
27 #include <list>
28 #include <iostream>
29
30 using std::list;
31 using std::pair;
32 using std::cout;
33 using std::map;
34 using std::string;
35 using std::make_pair;
36 using boost::shared_ptr;
37 using boost::optional;
38 using namespace dcpomatic;
39
40 RecipientsPanel::RecipientsPanel (wxWindow* parent)
41         : wxPanel (parent, wxID_ANY)
42         , _ignore_selection_change (false)
43 {
44         wxBoxSizer* sizer = new wxBoxSizer (wxVERTICAL);
45
46         _search = new wxSearchCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (200, -1));
47         _search->ShowCancelButton (true);
48         sizer->Add (_search, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP);
49
50         wxBoxSizer* targets = new wxBoxSizer (wxHORIZONTAL);
51         _targets = new wxTreeCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTR_HIDE_ROOT | wxTR_MULTIPLE | wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT);
52         targets->Add (_targets, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
53
54         add_recipients ();
55
56         wxBoxSizer* target_buttons = new wxBoxSizer (wxVERTICAL);
57
58         _add_recipient = new Button (this, _("Add..."));
59         target_buttons->Add (_add_recipient, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
60         _edit_recipient = new Button (this, _("Edit..."));
61         target_buttons->Add (_edit_recipient, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
62         _remove_recipient = new Button (this, _("Remove"));
63         target_buttons->Add (_remove_recipient, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
64
65         targets->Add (target_buttons, 0, 0);
66
67         sizer->Add (targets, 1, wxEXPAND);
68
69         _search->Bind  (wxEVT_TEXT, boost::bind (&RecipientsPanel::search_changed, this));
70         _targets->Bind (wxEVT_TREE_SEL_CHANGED, &RecipientsPanel::selection_changed_shim, this);
71
72         _add_recipient->Bind    (wxEVT_BUTTON, boost::bind (&RecipientsPanel::add_recipient_clicked, this));
73         _edit_recipient->Bind   (wxEVT_BUTTON, boost::bind (&RecipientsPanel::edit_recipient_clicked, this));
74         _remove_recipient->Bind (wxEVT_BUTTON, boost::bind (&RecipientsPanel::remove_recipient_clicked, this));
75
76         SetSizer (sizer);
77 }
78
79
80 RecipientsPanel::~RecipientsPanel ()
81 {
82         _targets->Unbind (wxEVT_TREE_SEL_CHANGED, &RecipientsPanel::selection_changed_shim, this);
83 }
84
85
86 void
87 RecipientsPanel::setup_sensitivity ()
88 {
89         _edit_recipient->Enable (_selected.size() == 1);
90         _remove_recipient->Enable (_selected.size() >= 1);
91 }
92
93
94 void
95 RecipientsPanel::add_recipient (shared_ptr<DKDMRecipient> r)
96 {
97         string search = wx_to_std (_search->GetValue());
98         transform (search.begin(), search.end(), search.begin(), ::tolower);
99
100         if (!search.empty()) {
101                 string name = r->name;
102                 transform (name.begin(), name.end(), name.begin(), ::tolower);
103                 if (name.find(search) == string::npos) {
104                         return;
105                 }
106         }
107
108         _recipients[_targets->AppendItem(_root, std_to_wx(r->name))] = r;
109
110         _targets->SortChildren (_root);
111 }
112
113
114 void
115 RecipientsPanel::add_recipient_clicked ()
116 {
117         RecipientDialog* d = new RecipientDialog (GetParent(), _("Add recipient"));
118         if (d->ShowModal() == wxID_OK) {
119                 shared_ptr<DKDMRecipient> r (new DKDMRecipient(d->name(), d->notes(), d->recipient(), d->emails(), d->utc_offset_hour(), d->utc_offset_minute()));
120                 Config::instance()->add_dkdm_recipient (r);
121                 add_recipient (r);
122         }
123
124         d->Destroy ();
125 }
126
127
128 void
129 RecipientsPanel::edit_recipient_clicked ()
130 {
131         if (_selected.size() != 1) {
132                 return;
133         }
134
135         pair<wxTreeItemId, shared_ptr<DKDMRecipient> > c = *_selected.begin();
136
137         RecipientDialog* d = new RecipientDialog (
138                 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
139                 );
140
141         if (d->ShowModal () == wxID_OK) {
142                 c.second->name = d->name ();
143                 c.second->emails = d->emails ();
144                 c.second->notes = d->notes ();
145                 c.second->utc_offset_hour = d->utc_offset_hour ();
146                 c.second->utc_offset_minute = d->utc_offset_minute ();
147                 _targets->SetItemText (c.first, std_to_wx (d->name()));
148                 Config::instance()->changed (Config::DKDM_RECIPIENTS);
149         }
150
151         d->Destroy ();
152 }
153
154
155 void
156 RecipientsPanel::remove_recipient_clicked ()
157 {
158         for (RecipientMap::iterator i = _selected.begin(); i != _selected.end(); ++i) {
159                 Config::instance()->remove_dkdm_recipient (i->second);
160                 _targets->Delete (i->first);
161         }
162
163         selection_changed ();
164 }
165
166
167 list<shared_ptr<DKDMRecipient> >
168 RecipientsPanel::recipients () const
169 {
170         list<shared_ptr<DKDMRecipient> > r;
171
172         for (RecipientMap::const_iterator i = _selected.begin(); i != _selected.end(); ++i) {
173                 r.push_back (i->second);
174         }
175
176         r.sort ();
177         r.unique ();
178
179         return r;
180 }
181
182
183 void
184 RecipientsPanel::selection_changed_shim (wxTreeEvent &)
185 {
186         selection_changed ();
187 }
188
189
190 void
191 RecipientsPanel::selection_changed ()
192 {
193         if (_ignore_selection_change) {
194                 return;
195         }
196
197         wxArrayTreeItemIds s;
198         _targets->GetSelections (s);
199
200         _selected.clear ();
201
202         for (size_t i = 0; i < s.GetCount(); ++i) {
203                 RecipientMap::const_iterator j = _recipients.find (s[i]);
204                 if (j != _recipients.end ()) {
205                         _selected[j->first] = j->second;
206                 }
207         }
208
209         setup_sensitivity ();
210         RecipientsChanged ();
211 }
212
213
214 void
215 RecipientsPanel::add_recipients ()
216 {
217         _root = _targets->AddRoot ("Foo");
218
219         BOOST_FOREACH (shared_ptr<DKDMRecipient> i, Config::instance()->dkdm_recipients()) {
220                 add_recipient (i);
221         }
222 }
223
224
225 void
226 RecipientsPanel::search_changed ()
227 {
228         _targets->DeleteAllItems ();
229         _recipients.clear ();
230
231         add_recipients ();
232
233         _ignore_selection_change = true;
234
235         for (RecipientMap::const_iterator i = _selected.begin(); i != _selected.end(); ++i) {
236                 /* The wxTreeItemIds will now be different, so we must search by recipient */
237                 RecipientMap::const_iterator j = _recipients.begin ();
238                 while (j != _recipients.end() && j->second != i->second) {
239                         ++j;
240                 }
241
242                 if (j != _recipients.end ()) {
243                         _targets->SelectItem (j->first);
244                 }
245         }
246
247         _ignore_selection_change = false;
248 }