Merge 1.0.
[dcpomatic.git] / src / wx / content_menu.cc
1 /*
2     Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <wx/wx.h>
21 #include <wx/dirdlg.h>
22 #include "lib/playlist.h"
23 #include "lib/film.h"
24 #include "lib/moving_image_content.h"
25 #include "lib/content_factory.h"
26 #include "lib/examine_content_job.h"
27 #include "lib/job_manager.h"
28 #include "content_menu.h"
29 #include "repeat_dialog.h"
30 #include "wx_util.h"
31
32 using std::cout;
33 using boost::shared_ptr;
34 using boost::weak_ptr;
35 using boost::dynamic_pointer_cast;
36
37 enum {
38         ID_repeat = 1,
39         ID_find_missing,
40         ID_remove
41 };
42
43 ContentMenu::ContentMenu (shared_ptr<Film> f, wxWindow* p)
44         : _menu (new wxMenu)
45         , _film (f)
46         , _parent (p)
47 {
48         _repeat = _menu->Append (ID_repeat, _("Repeat..."));
49         _find_missing = _menu->Append (ID_find_missing, _("Find missing..."));
50         _menu->AppendSeparator ();
51         _remove = _menu->Append (ID_remove, _("Remove"));
52
53         _parent->Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&ContentMenu::repeat, this), ID_repeat);
54         _parent->Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&ContentMenu::find_missing, this), ID_find_missing);
55         _parent->Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&ContentMenu::remove, this), ID_remove);
56 }
57
58 ContentMenu::~ContentMenu ()
59 {
60         delete _menu;
61 }
62
63 void
64 ContentMenu::popup (ContentList c, wxPoint p)
65 {
66         _content = c;
67         _repeat->Enable (!_content.empty ());
68         _find_missing->Enable (_content.size() == 1 && !_content.front()->path_valid ());
69         _remove->Enable (!_content.empty ());
70         _parent->PopupMenu (_menu, p);
71 }
72
73 void
74 ContentMenu::repeat ()
75 {
76         if (_content.empty ()) {
77                 return;
78         }
79                 
80         RepeatDialog* d = new RepeatDialog (_parent);
81         if (d->ShowModal() != wxID_OK) {
82                 d->Destroy ();
83                 return;
84         }
85
86         shared_ptr<const Film> film = _film.lock ();
87         if (!film) {
88                 return;
89         }
90
91         film->playlist()->repeat (_content, d->number ());
92         d->Destroy ();
93
94         _content.clear ();
95 }
96
97 void
98 ContentMenu::remove ()
99 {
100         if (_content.empty ()) {
101                 return;
102         }
103
104         shared_ptr<const Film> film = _film.lock ();
105         if (!film) {
106                 return;
107         }
108
109         film->playlist()->remove (_content);
110
111         _content.clear ();
112 }
113
114 void
115 ContentMenu::find_missing ()
116 {
117         if (_content.size() != 1) {
118                 return;
119         }
120
121         shared_ptr<const Film> film = _film.lock ();
122         if (!film) {
123                 return;
124         }
125         
126         shared_ptr<Content> content;
127
128         /* XXX: a bit nasty */
129         if (dynamic_pointer_cast<MovingImageContent> (_content.front ())) {
130                 wxDirDialog* d = new wxDirDialog (0, _("Choose a folder"), wxT (""), wxDD_DIR_MUST_EXIST);
131                 int const r = d->ShowModal ();
132                 if (r == wxID_OK) {
133                         content.reset (new MovingImageContent (film, boost::filesystem::path (wx_to_std (d->GetPath ()))));
134                 }
135                 d->Destroy ();
136         } else {
137                 wxFileDialog* d = new wxFileDialog (0, _("Choose a file"), wxT (""), wxT (""), wxT ("*.*"), wxFD_MULTIPLE);
138                 int const r = d->ShowModal ();
139                 if (r == wxID_OK) {
140                         content = content_factory (film, wx_to_std (d->GetPath ()));
141                 }
142                 d->Destroy ();
143         }
144
145         if (!content) {
146                 return;
147         }
148
149         shared_ptr<Job> j (new ExamineContentJob (film, content));
150         
151         j->Finished.connect (
152                 bind (
153                         &ContentMenu::maybe_found_missing,
154                         this,
155                         boost::weak_ptr<Job> (j),
156                         boost::weak_ptr<Content> (_content.front ()),
157                         boost::weak_ptr<Content> (content)
158                         )
159                 );
160         
161         JobManager::instance()->add (j);
162 }
163
164 void
165 ContentMenu::maybe_found_missing (weak_ptr<Job> j, weak_ptr<Content> oc, weak_ptr<Content> nc)
166 {
167         shared_ptr<Job> job = j.lock ();
168         if (!job || !job->finished_ok ()) {
169                 return;
170         }
171
172         shared_ptr<Content> old_content = oc.lock ();
173         shared_ptr<Content> new_content = nc.lock ();
174         assert (old_content);
175         assert (new_content);
176
177         if (new_content->digest() != old_content->digest()) {
178                 error_dialog (0, _("The content file(s) you specified are not the same as those that are missing.  Either try again with the correct content file or remove the missing content."));
179                 return;
180         }
181
182         old_content->set_path (new_content->path ());
183 }