Bump version
[dcpomatic.git] / hacks / python-playback / dvdomatic
1 #!/usr/bin/python
2
3 import os
4 import operator
5 import traceback
6 import pygtk
7 pygtk.require('2.0')
8 import gtk
9 import glib
10 import gobject
11 import film
12 import film_view
13 import player
14 import screens
15 import thumbs
16 import ratio
17 import util
18
19 FILM_DIRECTORY = '/home/carl/DVD'
20
21 current_player = None
22 films = []
23 inhibit_selection_update = False
24
25 def find_films():
26     global films
27     films = []
28     for root, dirs, files in os.walk(FILM_DIRECTORY):
29         for name in files:
30             if os.path.basename(name) == 'info':
31                 films.append(film.Film(os.path.join(root, os.path.dirname(name))))
32
33     films.sort(key = operator.attrgetter('name'))
34
35 def update_film_store():
36     global film_store
37     global films
38     global inhibit_selection_update
39     inhibit_selection_update = True
40     film_store.clear()
41     for f in films:
42         film_store.append([f.name])
43     inhibit_selection_update = False
44
45 def update_screen_store(screen_store, screens):
46     screen_store.clear()
47     for s in screens.screens:
48         screen_store.append([s.name])
49
50 def create_film_tree_view(film_store):
51     view = gtk.TreeView(film_store)
52     column = gtk.TreeViewColumn()
53     view.append_column(column)
54     cell = gtk.CellRendererText()
55     column.pack_start(cell)
56     column.add_attribute(cell, 'text', 0)
57     view.get_selection().set_mode(gtk.SELECTION_SINGLE)
58     return view
59
60 def create_screen_view(screen_store):
61     view = gtk.TreeView(screen_store)
62     column = gtk.TreeViewColumn()
63     view.append_column(column)
64     cell = gtk.CellRendererText()
65     column.pack_start(cell)
66     column.add_attribute(cell, 'text', 0)
67     view.get_selection().set_mode(gtk.SELECTION_SINGLE)
68     return view
69
70 def get_selected_film():
71     (model, iter) = film_tree_view.get_selection().get_selected()
72
73     for f in films:
74         if f.name == model.get(iter, 0)[0]:
75             return f
76
77     return None
78
79 # @return Selected screen name
80 def get_selected_screen():
81     (model, iter) = screen_view.get_selection().get_selected()
82     return model.get(iter, 0)[0]
83
84 def film_selected(selection):
85     if inhibit_selection_update:
86         return
87
88     film_view.set(get_selected_film())
89     check_for_playability()
90
91 def screen_selected(selection):
92     check_for_playability()
93
94 def check_for_playability():
95     f = get_selected_film()
96     if screens.get_format(get_selected_screen(), f.ratio) is not None:
97         play_button.set_label("Play")
98         play_button.set_sensitive(True)
99     else:
100         play_button.set_label("Cannot play: no setting for %s on screen %s" % (ratio.find(f.ratio).name(), get_selected_screen()))
101         play_button.set_sensitive(False)
102
103 def update_status(s):
104     global current_player
105     if current_player is None:
106         s.set_text("Not playing")
107         return True
108
109     position = current_player.time_pos
110     if position is None:
111         return True
112     position_hms = util.s_to_hms(position)
113
114     length = current_player.length
115     if length is None:
116         return True
117
118     remaining = length - position
119     remaining_hms = util.s_to_hms(remaining)
120     s.set_text("Playing: %d:%02d:%02d, %d:%02d:%02d remaining" % (position_hms[0], position_hms[1], position_hms[2], remaining_hms[0], remaining_hms[1], remaining_hms[2]))
121     return True
122
123 def play_clicked(b):
124     global current_player
125     f = get_selected_film()
126     current_player = player.get_player(f, screens.get_format(get_selected_screen(), f.ratio))
127     print current_player.args
128
129 def stop_clicked(b):
130     global current_player
131     if current_player is not None:
132         current_player.stop()
133         current_player = None
134
135 def add_film_clicked(b):
136     global films
137     c = gtk.FileChooserDialog("New Film", main_window, gtk.FILE_CHOOSER_ACTION_CREATE_FOLDER, (("Add", gtk.RESPONSE_OK)))
138     c.set_current_folder(FILM_DIRECTORY)
139     if c.run() == gtk.RESPONSE_OK:
140         f = film.Film()
141         f.data = c.get_filename()
142         f.name = os.path.basename(c.get_filename())
143         f.write()
144         find_films()
145         update_film_store()
146         c.hide()
147
148         for i in range(0, len(films)):
149             if films[i].name == f.name:
150                 film_tree_view.get_selection().select_path((i, ))
151
152 main_window = gtk.Window(gtk.WINDOW_TOPLEVEL)
153 main_window.set_title("DVD-o-matic")
154 main_window.maximize()
155
156 main_hbox = gtk.HBox()
157 main_hbox.set_spacing(12)
158 main_hbox.set_border_width(12)
159 main_window.add(main_hbox)
160
161 find_films()
162 film_view = film_view.FilmView(main_window)
163 screens = screens.Screens("screens")
164
165 left_vbox = gtk.VBox()
166 left_vbox.set_spacing(12)
167 main_hbox.pack_start(left_vbox, False, False)
168 right_vbox = gtk.VBox()
169 right_vbox.set_spacing(12)
170 main_hbox.pack_start(right_vbox)
171
172 film_store = gtk.ListStore(gobject.TYPE_STRING)
173 update_film_store()
174
175 film_tree_view = create_film_tree_view(film_store)
176 left_vbox.pack_start(film_tree_view, True, True)
177 film_tree_view.get_selection().select_path((0, ))
178 film_tree_view.get_selection().connect("changed", film_selected)
179
180 add_film_button = gtk.Button(stock = gtk.STOCK_ADD)
181 left_vbox.pack_start(add_film_button, False, False)
182 add_film_button.connect("clicked", add_film_clicked)
183
184 screen_store = gtk.ListStore(gobject.TYPE_STRING)
185 update_screen_store(screen_store, screens)
186
187 screen_view = create_screen_view(screen_store)
188 left_vbox.pack_start(screen_view, False, False)
189 screen_view.get_selection().select_path((0, ))
190 screen_view.get_selection().connect("changed", screen_selected)
191
192 right_vbox.pack_start(film_view, False, False)
193 film_view.set(films[0])
194
195 play_button = gtk.Button("Play")
196 right_vbox.pack_start(play_button)
197 play_button.connect("clicked", play_clicked)
198
199 stop_button = gtk.Button("Stop")
200 right_vbox.pack_start(stop_button)
201 stop_button.connect("clicked", stop_clicked)
202
203 status = gtk.Label()
204 right_vbox.pack_start(status, False, False)
205 glib.timeout_add_seconds(1, update_status, status)
206
207 check_for_playability()
208 main_window.show_all()
209 gtk.main()