Merge still/moving image classes.
[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/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         shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (_content.front ());
130         if (ic && !ic->still ()) {
131                 wxDirDialog* d = new wxDirDialog (0, _("Choose a folder"), wxT (""), wxDD_DIR_MUST_EXIST);
132                 int const r = d->ShowModal ();
133                 if (r == wxID_OK) {
134                         content.reset (new ImageContent (film, boost::filesystem::path (wx_to_std (d->GetPath ()))));
135                 }
136                 d->Destroy ();
137         } else {
138                 wxFileDialog* d = new wxFileDialog (0, _("Choose a file"), wxT (""), wxT (""), wxT ("*.*"), wxFD_MULTIPLE);
139                 int const r = d->ShowModal ();
140                 if (r == wxID_OK) {
141                         content = content_factory (film, wx_to_std (d->GetPath ()));
142                 }
143                 d->Destroy ();
144         }
145
146         if (!content) {
147                 return;
148         }
149
150         shared_ptr<Job> j (new ExamineContentJob (film, content));
151         
152         j->Finished.connect (
153                 bind (
154                         &ContentMenu::maybe_found_missing,
155                         this,
156                         boost::weak_ptr<Job> (j),
157                         boost::weak_ptr<Content> (_content.front ()),
158                         boost::weak_ptr<Content> (content)
159                         )
160                 );
161         
162         JobManager::instance()->add (j);
163 }
164
165 void
166 ContentMenu::maybe_found_missing (weak_ptr<Job> j, weak_ptr<Content> oc, weak_ptr<Content> nc)
167 {
168         shared_ptr<Job> job = j.lock ();
169         if (!job || !job->finished_ok ()) {
170                 return;
171         }
172
173         shared_ptr<Content> old_content = oc.lock ();
174         shared_ptr<Content> new_content = nc.lock ();
175         assert (old_content);
176         assert (new_content);
177
178         if (new_content->digest() != old_content->digest()) {
179                 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."));
180                 return;
181         }
182
183         old_content->set_path (new_content->path ());
184 }