-
Notifications
You must be signed in to change notification settings - Fork 0
/
preferences.py
207 lines (177 loc) · 8.39 KB
/
preferences.py
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
#!/usr/bin/python3
from gi import require_version
require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk, Gio, GLib
import configparser
import subprocess
import os
config = configparser.ConfigParser()
configfile = os.path.expanduser('~/.config/chromecast_player')
default_vals = {'chromecast_player': {'automatic_connect': False, 'enable_web': False, 'enable_transcoding': False, 'local_port': "", 'transcoding_options':"", "preferred_transcoder":""}}
def get_config(section):
config.read(configfile)
dict1 = {}
for option in default_vals[section].keys():
try:
op = config.get(section, option)
if op == 'True' or op == 'False':
dict1[option] = (op == 'True')
else:
try:
dict1[option] = int(op)
except:
dict1[option] = op
except:
try:
config.add_section(section)
except:
pass
config.set(section, option, str(default_vals[section][option]))
f = open(configfile, 'w')
config.write(f)
f.close()
dict1[option] = default_vals[section][option]
return dict1
def set_config(section, option, value):
try:
config.set(section, option, str(value))
except:
config.add_section(section)
config.set(section, option, str(value))
f = open(configfile, 'w')
config.write(f)
f.close()
class Preferences(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
self.win = Gtk.Window(type=Gtk.WindowType.TOPLEVEL)
config_chromecast = get_config('chromecast_player')
vboxall = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
hboxbuttons = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
hboxradio = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
hboxport = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
hboxtrans = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
self.automatic_connect = Gtk.CheckButton(label=" Automatically connect to first available chromecast on startup")
self.automatic_connect.set_active(config_chromecast['automatic_connect'])
self.automatic_connect.connect("toggled", self.config_changed, "automatic_connect")
self.enable_web = Gtk.CheckButton(label=" Enable network streams")
self.enable_web.set_active(config_chromecast['enable_web'])
self.enable_web.connect("toggled", self.config_changed, "enable_web")
self.enable_transcoding = Gtk.CheckButton(label=" Enables on the fly transcoding of unsupported filetypes")
self.enable_transcoding.set_active(config_chromecast['enable_transcoding'])
self.enable_transcoding.connect("toggled", self.config_changed, "enable_transcoding")
radio_label = Gtk.Label()
radio_label.set_text("Preferred transcoder: ")
hboxradio.pack_start(radio_label, False, False, 0)
self.ffmpeg_button = Gtk.RadioButton.new_with_label_from_widget(None, "FFMPEG")
self.ffmpeg_button.connect("toggled", self.config_changed, "preferred_transcoder")
hboxradio.pack_start(self.ffmpeg_button, False, False, 10)
self.avconv_button = Gtk.RadioButton.new_with_label_from_widget(self.ffmpeg_button, "AVCONV")
self.avconv_button.connect("toggled", self.config_changed, "preferred_transcoder")
hboxradio.pack_start(self.avconv_button, False, False, 10)
port_label = Gtk.Label()
port_label.set_text("Port for local files: ")
self.port_entry = Gtk.Entry()
try:
po = int(config_chromecast['local_port'])
self.port_entry.set_text(str(po))
except:
self.port_entry.set_text("")
self.port_entry.connect("changed", self.config_changed, "local_port")
self.port_entry.set_max_width_chars(5)
self.port_entry.set_width_chars(5)
hboxport.pack_start(port_label, False, False, 0)
hboxport.pack_start(self.port_entry, False, False, 20)
transcoder_label = Gtk.Label()
transcoder_label.set_text("Options for transcoder: ")
self.transcoder_options = Gtk.Entry()
self.transcoder_options.set_max_width_chars(20)
self.transcoder_options.set_width_chars(20)
self.transcoder_options.connect("changed", self.config_changed, "transcoding_options")
self.transcoder_options.set_text(config_chromecast['transcoding_options'])
hboxtrans.pack_start(transcoder_label, False, False, 0)
hboxtrans.pack_start(self.transcoder_options, False, False, 20)
if not self.enable_transcoding.get_active():
self.ffmpeg_button.set_sensitive(False)
self.avconv_button.set_sensitive(False)
self.transcoder_options.set_sensitive(False)
vboxall.pack_start(self.automatic_connect, False, False, 20)
vboxall.pack_start(self.enable_web, False, False, 20)
vboxall.pack_start(hboxport, False, False, 20)
vboxall.pack_start(self.enable_transcoding, False, False, 20)
vboxall.pack_start(hboxradio, False, False, 20)
vboxall.pack_start(hboxtrans, False, False, 20)
#close = Gtk.Button("_Close", use_underline=True)
#close.get_style_context().add_class("destructive-action")
#close.connect("clicked", self.exit)
ok = Gtk.Button("_Apply", use_underline=True)
ok.get_style_context().add_class("suggested-action")
ok.connect("clicked", self.exit)
self.win.connect("delete-event", Gtk.main_quit)
hboxbuttons.pack_end(ok, False, False, 30)
vboxall.pack_end(hboxbuttons, False, False, 30)
self.win.set_icon_name('chromecast-player')
self.win.add(vboxall)
self.win.set_size_request(500,50)
self.automatic_connect.set_margin_left(30)
self.automatic_connect.set_margin_right(50)
self.enable_transcoding.set_margin_left(30)
self.enable_transcoding.set_margin_right(50)
hboxradio.set_margin_left(60)
hboxradio.set_margin_right(20)
hboxport.set_margin_left(30)
hboxport.set_margin_right(50)
hboxtrans.set_margin_left(60)
hboxtrans.set_margin_right(20)
self.enable_web.set_margin_left(30)
self.enable_web.set_margin_right(50)
self.win.set_title('Preferences')
def run(self):
self.win.show_all()
Gtk.main()
self.win.destroy()
def exit(self, *args):
#self.win.close()
Gtk.main_quit()
def config_changed(self, *args):
if args[1] in ["enable_web", "enable_transcoding", "automatic_connect"]:
state = args[0].get_active()
elif args[1] == "preferred_transcoder":
state = 0
elif args[1] in ["local_port", "transcoding_options"]:
state = args[0].get_text()
if args[1] == "enable_web" and state:
state = self.check_youtube_dl()
if not state:
message = ("Youtube-dl not installed on this machine, so stream fetching won't work!")
win = Gtk.Window(type=Gtk.WindowType.TOPLEVEL)
dialog = Gtk.MessageDialog(win, None, Gtk.MessageType.ERROR,
Gtk.ButtonsType.OK, message)
dialog.run()
dialog.destroy()
args[0].set_active(False)
elif args[1] == "enable_transcoding":
if state:
self.transcoder_options.set_sensitive(True)
self.ffmpeg_button.set_sensitive(True)
self.avconv_button.set_sensitive(True)
elif not state:
self.transcoder_options.set_sensitive(False)
self.ffmpeg_button.set_sensitive(False)
self.avconv_button.set_sensitive(False)
elif args[1] == "local_port":
try:
po = int(state)
self.port_entry.set_text(str(po))
except:
state = ""
self.port_entry.set_text(str(state))
elif args[1] == "preferred_transcoder":
if self.ffmpeg_button.get_active():
state = 'ffmpeg'
else:
state = 'avconv'
set_config('chromecast_player', args[1], str(state))
def check_youtube_dl(self):
rc = subprocess.call(['which', 'youtube-dl'])
return bool(not rc)