summaryrefslogtreecommitdiff
path: root/hacks/python-playback/dvdomatic
blob: ce405f37e87334b0a6822f088bb711923de97332 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/python

import os
import operator
import traceback
import pygtk
pygtk.require('2.0')
import gtk
import glib
import gobject
import film
import film_view
import player
import screens
import thumbs
import ratio
import util

FILM_DIRECTORY = '/home/carl/DVD'

current_player = None
films = []
inhibit_selection_update = False

def find_films():
    global films
    films = []
    for root, dirs, files in os.walk(FILM_DIRECTORY):
        for name in files:
            if os.path.basename(name) == 'info':
                films.append(film.Film(os.path.join(root, os.path.dirname(name))))

    films.sort(key = operator.attrgetter('name'))

def update_film_store():
    global film_store
    global films
    global inhibit_selection_update
    inhibit_selection_update = True
    film_store.clear()
    for f in films:
        film_store.append([f.name])
    inhibit_selection_update = False

def update_screen_store(screen_store, screens):
    screen_store.clear()
    for s in screens.screens:
        screen_store.append([s.name])

def create_film_tree_view(film_store):
    view = gtk.TreeView(film_store)
    column = gtk.TreeViewColumn()
    view.append_column(column)
    cell = gtk.CellRendererText()
    column.pack_start(cell)
    column.add_attribute(cell, 'text', 0)
    view.get_selection().set_mode(gtk.SELECTION_SINGLE)
    return view

def create_screen_view(screen_store):
    view = gtk.TreeView(screen_store)
    column = gtk.TreeViewColumn()
    view.append_column(column)
    cell = gtk.CellRendererText()
    column.pack_start(cell)
    column.add_attribute(cell, 'text', 0)
    view.get_selection().set_mode(gtk.SELECTION_SINGLE)
    return view

def get_selected_film():
    (model, iter) = film_tree_view.get_selection().get_selected()

    for f in films:
        if f.name == model.get(iter, 0)[0]:
            return f

    return None

# @return Selected screen name
def get_selected_screen():
    (model, iter) = screen_view.get_selection().get_selected()
    return model.get(iter, 0)[0]

def film_selected(selection):
    if inhibit_selection_update:
        return

    film_view.set(get_selected_film())
    check_for_playability()

def screen_selected(selection):
    check_for_playability()

def check_for_playability():
    f = get_selected_film()
    if screens.get_format(get_selected_screen(), f.ratio) is not None:
        play_button.set_label("Play")
        play_button.set_sensitive(True)
    else:
        play_button.set_label("Cannot play: no setting for %s on screen %s" % (ratio.find(f.ratio).name(), get_selected_screen()))
        play_button.set_sensitive(False)

def update_status(s):
    global current_player
    if current_player is None:
        s.set_text("Not playing")
        return True

    position = current_player.time_pos
    if position is None:
        return True
    position_hms = util.s_to_hms(position)

    length = current_player.length
    if length is None:
        return True

    remaining = length - position
    remaining_hms = util.s_to_hms(remaining)
    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]))
    return True

def play_clicked(b):
    global current_player
    f = get_selected_film()
    current_player = player.get_player(f, screens.get_format(get_selected_screen(), f.ratio))
    print current_player.args

def stop_clicked(b):
    global current_player
    if current_player is not None:
        current_player.stop()
        current_player = None

def add_film_clicked(b):
    global films
    c = gtk.FileChooserDialog("New Film", main_window, gtk.FILE_CHOOSER_ACTION_CREATE_FOLDER, (("Add", gtk.RESPONSE_OK)))
    c.set_current_folder(FILM_DIRECTORY)
    if c.run() == gtk.RESPONSE_OK:
        f = film.Film()
        f.data = c.get_filename()
        f.name = os.path.basename(c.get_filename())
        f.write()
        find_films()
        update_film_store()
        c.hide()

        for i in range(0, len(films)):
            if films[i].name == f.name:
                film_tree_view.get_selection().select_path((i, ))

main_window = gtk.Window(gtk.WINDOW_TOPLEVEL)
main_window.set_title("DVD-o-matic")
main_window.maximize()

main_hbox = gtk.HBox()
main_hbox.set_spacing(12)
main_hbox.set_border_width(12)
main_window.add(main_hbox)

find_films()
film_view = film_view.FilmView(main_window)
screens = screens.Screens("screens")

left_vbox = gtk.VBox()
left_vbox.set_spacing(12)
main_hbox.pack_start(left_vbox, False, False)
right_vbox = gtk.VBox()
right_vbox.set_spacing(12)
main_hbox.pack_start(right_vbox)

film_store = gtk.ListStore(gobject.TYPE_STRING)
update_film_store()

film_tree_view = create_film_tree_view(film_store)
left_vbox.pack_start(film_tree_view, True, True)
film_tree_view.get_selection().select_path((0, ))
film_tree_view.get_selection().connect("changed", film_selected)

add_film_button = gtk.Button(stock = gtk.STOCK_ADD)
left_vbox.pack_start(add_film_button, False, False)
add_film_button.connect("clicked", add_film_clicked)

screen_store = gtk.ListStore(gobject.TYPE_STRING)
update_screen_store(screen_store, screens)

screen_view = create_screen_view(screen_store)
left_vbox.pack_start(screen_view, False, False)
screen_view.get_selection().select_path((0, ))
screen_view.get_selection().connect("changed", screen_selected)

right_vbox.pack_start(film_view, False, False)
film_view.set(films[0])

play_button = gtk.Button("Play")
right_vbox.pack_start(play_button)
play_button.connect("clicked", play_clicked)

stop_button = gtk.Button("Stop")
right_vbox.pack_start(stop_button)
stop_button.connect("clicked", stop_clicked)

status = gtk.Label()
right_vbox.pack_start(status, False, False)
glib.timeout_add_seconds(1, update_status, status)

check_for_playability()
main_window.show_all()
gtk.main()