swaroop: basic manipulation of content in playlist creator.
[dcpomatic.git] / src / wx / content_view.cc
1 /*
2     Copyright (C) 2018 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 "content_view.h"
22 #include "wx_util.h"
23 #include "lib/dcpomatic_assert.h"
24 #include "lib/config.h"
25 #include "lib/dcp_content.h"
26 #include "lib/content_factory.h"
27 #include "lib/examine_content_job.h"
28 #include "lib/job_manager.h"
29 #include "lib/cross.h"
30 #include <dcp/exceptions.h>
31 #include <boost/filesystem.hpp>
32 #include <boost/optional.hpp>
33 #include <wx/progdlg.h>
34
35 using boost::shared_ptr;
36 using boost::weak_ptr;
37 using boost::optional;
38 using boost::dynamic_pointer_cast;
39
40 ContentView::ContentView (wxWindow* parent, weak_ptr<Film> film)
41         : wxListCtrl (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER)
42         , _film (film)
43 {
44         AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 80);
45         /* type */
46         AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 80);
47         /* annotation text */
48         AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 580);
49 }
50
51 shared_ptr<Content>
52 ContentView::selected () const
53 {
54         long int s = GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
55         if (s == -1) {
56                 return shared_ptr<Content>();
57         }
58
59         DCPOMATIC_ASSERT (s < int(_content.size()));
60         return _content[s];
61 }
62
63 void
64 ContentView::update ()
65 {
66         if (!IsShown()) {
67                 return;
68         }
69
70         shared_ptr<Film> film = _film.lock ();
71         if (!film) {
72                 return;
73         }
74
75         using namespace boost::filesystem;
76
77         DeleteAllItems ();
78         _content.clear ();
79         optional<path> dir = Config::instance()->player_content_directory();
80         if (!dir) {
81                 return;
82         }
83
84         wxProgressDialog progress (_("DCP-o-matic"), _("Reading content directory"));
85         JobManager* jm = JobManager::instance ();
86
87         for (directory_iterator i = directory_iterator(*dir); i != directory_iterator(); ++i) {
88                 try {
89                         shared_ptr<Content> content;
90                         if (is_directory(*i) && (is_regular_file(*i / "ASSETMAP") || is_regular_file(*i / "ASSETMAP.xml"))) {
91                                 content.reset (new DCPContent(film, *i));
92                         } else if (i->path().extension() == ".mp4" || i->path().extension() == ".ecinema") {
93                                 content = content_factory(film, *i).front();
94                         }
95
96                         if (content) {
97                                 jm->add (shared_ptr<Job>(new ExamineContentJob(film, content)));
98                                 while (jm->work_to_do()) {
99                                         if (!progress.Pulse()) {
100                                                 /* user pressed cancel */
101                                                 BOOST_FOREACH (shared_ptr<Job> i, jm->get()) {
102                                                         i->cancel();
103                                                 }
104                                                 return;
105                                         }
106                                         dcpomatic_sleep (1);
107                                 }
108                                 if (report_errors_from_last_job (this)) {
109                                         add (content);
110                                         _content.push_back (content);
111                                 }
112                         }
113                 } catch (boost::filesystem::filesystem_error& e) {
114                         /* Never mind */
115                 } catch (dcp::DCPReadError& e) {
116                         /* Never mind */
117                 }
118         }
119 }
120
121 void
122 ContentView::add (shared_ptr<Content> content)
123 {
124         int const N = GetItemCount();
125
126         wxListItem it;
127         it.SetId(N);
128         it.SetColumn(0);
129         DCPTime length = content->length_after_trim ();
130         int h, m, s, f;
131         length.split (24, h, m, s, f);
132         it.SetText(wxString::Format("%02d:%02d:%02d", h, m, s));
133         InsertItem(it);
134
135         shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent>(content);
136         if (dcp && dcp->content_kind()) {
137                 it.SetId(N);
138                 it.SetColumn(1);
139                 it.SetText(std_to_wx(dcp::content_kind_to_string(*dcp->content_kind())));
140                 SetItem(it);
141         }
142
143         it.SetId(N);
144         it.SetColumn(2);
145         it.SetText(std_to_wx(content->summary()));
146         SetItem(it);
147 }