wmix: fix multiple definitions of global variables.
[dockapps.git] / wmradio / config / wmradio-config.py
blob58da8e21fa2c3b8bf27d873623a5f36aa03e998d
1 #!/usr/bin/env python
3 import gobject, gtk, time, os, string
4 from gettext import gettext as _
5 from gettext import bindtextdomain, textdomain
6 from locale import setlocale, LC_ALL
7 import os
9 PREFIX = "/usr"
10 LOCALEDIR = "%s/share/locale" % (PREFIX)
11 PACKAGE = "wmradio-config.py"
12 SKINDIR = "%s/lib/wmradio" % (PREFIX)
13 FREQMIN=87.5
14 FREQMAX=108.0
15 XOSD = "undef " == "define"
17 class inifile:
18 def __init__(self,afilename):
19 self.sections = {};
20 try:
21 self.load(afilename)
22 except IOError:
23 pass
24 def load(self,afilename):
25 self.filename = afilename
26 f = open(afilename,'r');
27 lines = f.readlines();
28 section = "config";
29 self.sections = {};
30 self.sections[section] = {}
31 for line in lines:
32 if line[:1] == ';' or line[:1] == '#' : line = '';
33 if len(line)> 0:
34 if line[0] == '[':
35 e = string.find(line,']');
36 section = string.lower(line[1:e])
37 self.sections[section] = {}
38 else:
39 eq = string.find(line,"=")
40 if eq >= 0 :
41 variable = string.strip(string.lower(line[:eq]))
42 value = string.strip(line[eq+1:])
43 e = string.find(value,'\n')
44 if e>=0 :
45 value = value[0:e]
46 self.sections[section][variable] = value
47 f.close();
48 def save(self):
49 f = file(self.filename,"w+")
50 for section in self.sections.keys():
51 f.write('[%s]\n' % (section))
52 for value in self.sections[section].keys():
53 f.write("%s=%s\n" % (value,self.sections[section][value]))
54 f.close()
55 def save_as(self,newfile):
56 self.filename = newfile
57 self.save()
58 def get_variable(self,sec,var,default=""):
59 sec = string.lower(sec)
60 var = string.lower(var)
61 if not self.sections.has_key(sec) : return default
62 if not self.sections[sec].has_key(var) : return default
63 return self.sections[sec][var]
64 def get_int_variable(self,sec,var,default=0):
65 try:
66 val = self.get_variable(sec,var)
67 if val == "" : return default
68 return string.atoi(val)
69 except: return 0;
70 def set_variable(self,sec,var,val):
71 if not self.sections.has_key(string.lower(sec)):
72 self.sections[string.lower(sec)] = {}
73 self.sections[string.lower(sec)][string.lower(var)] = val
74 def set_int_variable(self,sec,var,val):
75 self.set_variable(sec,var,"%i" % (val))
77 # --------------------------------------------------------------------
79 class WmRadioConfig:
80 def __init__(self):
81 self.g_window = None;
82 self.g_quit_button = None;
83 self.g_device = None;
84 self.g_ok_button = None;
85 self.g_cancel_button = None;
86 self.g_pressed = []
87 self.g_new_freq = None;
88 self.g_new_name = None;
89 self.g_delete_button = None;
90 self.g_osd = None;
91 self.g_osd_font = None;
92 self.g_osd_color = None;
93 self.g_osd_position = None;
94 self.g_osd_shadow = None;
95 self.g_osd_timeout = None;
96 self.g_start_muted = None;
97 self.g_dont_quit = None;
98 def quit(self, *args):
99 self.g_window.destroy()
100 def one_station_to_ini(self,model,path,iter,data):
101 line = self.g_jobs.get_value(iter,0)
102 i = string.find(line,"=")
103 if i > -1:
104 name = line[:i]
105 freq = line[i+1:]
106 self.cfg.set_variable("names", name, freq)
107 def station_to_ini(self):
108 if self.cfg.sections.has_key("names") : del self.cfg.sections["names"]
109 self.g_jobs.foreach(self.one_station_to_ini,0)
110 def on_osd_changed(self, *args):
111 sensitive = self.g_osd.get_active()
112 self.g_osd_font.set_sensitive(sensitive)
113 self.g_osd_color.set_sensitive(sensitive)
114 self.g_osd_position.set_sensitive(sensitive)
115 self.g_osd_shadow.set_sensitive(sensitive)
116 self.g_osd_timeout.set_sensitive(sensitive)
117 def on_apply_click(self, *args):
118 self.store_config()
119 def on_ok_click(self, *args):
120 self.store_config()
121 self.g_window.destroy()
122 def store_config(self):
123 self.cfg.set_variable("config","device",self.g_device.get_text())
124 self.cfg.set_variable("config","skin",self.g_skin.entry.get_text())
125 for i in range(6):
126 self.cfg.set_variable("config",
127 "preset%i" % i ,
128 "%.2f" % self.g_pressed[i].get_value())
129 self.station_to_ini()
130 self.cfg.set_int_variable("config","osd", self.g_osd.get_active())
131 self.cfg.set_variable("config","osd-font",self.g_osd_font.get_text())
132 self.cfg.set_variable("config","osd-color", self.g_osd_color.get_text())
133 self.cfg.set_int_variable("config","osd-position",self.g_osd_position.get_value())
134 self.cfg.set_int_variable("config","osd-shadow-offset", self.g_osd_shadow.get_value())
135 self.cfg.set_int_variable("config","osd-timeout",self.g_osd_timeout.get_value())
136 self.cfg.set_int_variable("config","start-muted",self.g_start_muted.get_active())
137 self.cfg.set_int_variable("config","dont-quit-mode",self.g_dont_quit.get_active())
138 self.cfg.save()
139 self.refresh_running_radio()
140 def destroy(self, *args):
141 gtk.main_quit()
142 def load_config(self):
143 self.cfg = inifile("%s/.wmradio" % (os.environ['HOME']))
144 self.g_device.set_text(self.cfg.get_variable("config","device"))
145 self.g_skin.entry.set_text(self.cfg.get_variable("config","skin"))
146 if self.cfg.get_variable("config","start-muted") == "1":
147 self.g_start_muted.set_active(gtk.TRUE)
148 if self.cfg.get_variable("config","dont-quit-mode") == "1":
149 self.g_dont_quit.set_active(gtk.TRUE)
150 for i in range(6):
151 try:
152 self.g_pressed[i].set_value(string.atof(self.cfg.get_variable("config","preset%i" % i )))
153 except:
154 pass
155 if self.cfg.sections.has_key("names"):
156 for name in self.cfg.sections["names"].keys():
157 item = self.g_jobs.append()
158 self.g_jobs.set(item,0,
159 "%s=%s" %
160 (name,self.cfg.get_variable("names",name))
162 if self.cfg.get_variable("config","osd") == "1":
163 self.g_osd.set_active(gtk.TRUE)
164 self.g_osd_font.set_text(self.cfg.get_variable("config",
165 "osd-font"))
166 self.g_osd_color.set_text(self.cfg.get_variable("config",
167 "osd-color"))
168 self.g_osd_position.set_value(self.cfg.get_int_variable("config",
169 "osd-position"))
170 self.g_osd_shadow.set_value(self.cfg.get_int_variable("config",
171 "osd-shadow-offset"))
172 self.g_osd_timeout.set_value(self.cfg.get_int_variable("config",
173 "osd-timeout"))
174 self.on_osd_changed(None)
175 def delete_selected_name(self,model,path,iter,data):
176 model.remove(iter)
177 def on_delete(self,widget):
178 self.g_tree_view.get_selection().selected_foreach(self.delete_selected_name,0)
179 self.on_selection_changed(widget)
180 def on_add_name(self,widget):
181 self.station_alredy_exist = 0
182 self.g_jobs.foreach(self.stations_exist,self.g_new_name.get_text())
183 if not self.station_alredy_exist:
184 item = self.g_jobs.append()
185 self.g_jobs.set(item,
187 "%s=%.2f" %
188 (self.g_new_name.get_text(),
189 self.g_new_freq.get_value()))
190 def activate_delete_button(self,model,path,iter,data):
191 self.g_delete_button.set_sensitive(gtk.TRUE)
192 def on_selection_changed(self,widget=None):
193 self.g_delete_button.set_sensitive(gtk.FALSE)
194 self.g_tree_view.get_selection().selected_foreach(self.activate_delete_button,0)
195 def on_name_changed(self,widget):
196 self.g_add_button.set_sensitive( len(string.strip(self.g_new_name.get_text())) > 0 )
197 def refresh_running_radio(self):
198 pipe = "/tmp/wmradio_%i" % os.getuid()
199 if os.path.exists(pipe):
200 f = open(pipe,"w+")
201 f.write("READ_CONFIG\n")
202 def stations_exist(self,model,path,iter,text):
203 line = self.g_jobs.get_value(iter,0)
204 i = string.find(line,"=")
205 if i > -1:
206 name = line[:i]
207 if string.lower(name) == string.lower(text):
208 self.g_jobs.set(iter,
210 "%s=%.2f" %
211 (self.g_new_name.get_text(),
212 self.g_new_freq.get_value()))
213 self.station_alredy_exist = 1
214 def check_that_station_exist():
215 self.station_alredy_exist = 0
216 self.g_jobs.foreach(self.stations_exist,0)
217 def fill_combo(self):
218 files = os.listdir(SKINDIR)
219 skins = []
220 for file in files:
221 if file[-5:] == ".skin":
222 skins.append(file)
223 if len(skins): self.g_skin.set_popdown_strings(skins)
224 def frame1(self):
225 frame = gtk.Frame(_("Base config"))
226 frame.set_border_width(5)
227 table = gtk.Table(rows = 2, columns = 5, homogeneous=gtk.FALSE)
228 table.set_border_width(5)
229 table.set_col_spacings(5)
230 frame.add(table)
231 lbl = gtk.Label(_("Device"))
232 lbl.set_alignment(xalign=0.0,yalign=0.5)
233 table.attach(lbl,0,1,0,1)
234 self.g_device = gtk.Entry()
235 table.attach(self.g_device,1,2,0,1)
237 lbl = gtk.Label(_("Skin"))
238 lbl.set_alignment(xalign=0.0,yalign=0.5)
239 table.attach(lbl,0,1,1,2)
240 self.g_skin = gtk.Combo()
241 self.fill_combo()
242 table.attach(self.g_skin,1,2,1,2)
244 self.g_start_muted = gtk.CheckButton(_("Start muted"))
245 table.attach(self.g_start_muted,0,2,2,3)
247 self.g_dont_quit = gtk.CheckButton(_("Don't quit mode"))
248 table.attach(self.g_dont_quit,0,2,3,4)
250 return frame
251 def frame2(self):
252 frame = gtk.Frame(_("Stations"))
253 frame.set_border_width(5)
254 table = gtk.Table(rows = 2, columns = 6, homogeneous=gtk.FALSE)
255 table.set_border_width(5)
256 table.set_col_spacings(5)
257 frame.add(table)
258 for i in range(6):
259 lbl = gtk.Label(_("Station %i") % (i+1))
260 lbl.set_alignment(xalign=0.0,yalign=0.5)
261 table.attach(lbl,0,1,i,i+1)
262 self.g_pressed.append( gtk.SpinButton(gtk.Adjustment(FREQMIN,
263 FREQMIN,
264 FREQMAX,
265 0.1,
269 0.0,
270 2) );
271 table.attach(self.g_pressed[i],1,2,i,i+1)
272 return frame
273 def frame3(self):
274 frame = gtk.Frame(_("Stations names"))
275 frame.set_border_width(5)
276 table = gtk.Table(rows = 2, columns = 3, homogeneous=gtk.FALSE)
277 table.set_border_width(5)
278 table.set_col_spacings(5)
279 frame.add(table)
280 box = gtk.HBox(gtk.FALSE,2)
281 table.attach(box,0,1,0,1,yoptions=0)
282 lbl = gtk.Label(_("Name"))
283 box.pack_start(lbl,gtk.TRUE,gtk.TRUE,0)
284 self.g_new_name = gtk.Entry(max=3);
285 self.g_new_name.connect("changed",self.on_name_changed)
286 box.pack_start(self.g_new_name,gtk.TRUE,gtk.TRUE,0)
287 lbl = gtk.Label(_("Frequency"))
288 box.pack_start(lbl,gtk.TRUE,gtk.TRUE,0)
289 self.g_new_freq = gtk.SpinButton(gtk.Adjustment(FREQMIN,
290 FREQMIN,
291 FREQMAX,
292 0.1,
295 0.0,
297 box.pack_start(self.g_new_freq,gtk.TRUE,gtk.TRUE,0)
298 self.g_add_button = gtk.Button(_("Add"))
299 box.pack_start(self.g_add_button,gtk.TRUE,gtk.TRUE,0)
300 self.g_add_button.connect("clicked",self.on_add_name)
301 self.g_add_button.set_sensitive(gtk.FALSE)
303 scrolled_window = gtk.ScrolledWindow()
304 scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
305 self.g_jobs = gtk.ListStore(gobject.TYPE_STRING)
306 self.g_tree_view = gtk.TreeView(self.g_jobs)
307 scrolled_window.add_with_viewport (self.g_tree_view)
308 cell = gtk.CellRendererText()
309 column = gtk.TreeViewColumn(_("Name=Frequency"), cell, text=0)
310 self.g_tree_view.append_column(column)
311 table.attach(scrolled_window,0,1,1,2,yoptions=gtk.EXPAND|gtk.FILL)
312 self.g_tree_view.get_selection().connect("changed",self.on_selection_changed)
314 self.g_delete_button = gtk.Button(_("Delete"))
315 self.g_delete_button.connect("clicked",self.on_delete)
316 self.g_delete_button.set_sensitive(gtk.FALSE)
318 table.attach(self.g_delete_button,0,1,2,3,yoptions=0)
319 return frame
320 def frame4(self):
321 vbox = gtk.VBox(gtk.FALSE,2)
322 frame = gtk.Frame(_("osd"))
323 frame.set_border_width(5)
324 vbox.pack_start(frame, gtk.FALSE,gtk.FALSE)
325 #vbox.pack_start(gtk.VBox(gtk.FALSE), gtk.TRUE,gtk.TRUE)
326 table = gtk.Table(rows = 2, columns = 3, homogeneous=gtk.FALSE)
327 table.set_border_width(5)
328 table.set_col_spacings(5)
329 frame.add(table)
331 self.g_osd = gtk.CheckButton(_("Use osd"))
332 table.attach(self.g_osd,0,2,0,1)
333 self.g_osd.connect("clicked",self.on_osd_changed,None)
334 # osd font
335 lbl = gtk.Label(_("Font"))
336 lbl.set_alignment(xalign=0.0,yalign=0.5)
337 table.attach(lbl,0,1,1,2)
338 self.g_osd_font = gtk.Entry()
339 table.attach(self.g_osd_font,1,2,1,2)
340 # osd color
341 lbl = gtk.Label(_("Color"))
342 lbl.set_alignment(xalign=0.0,yalign=0.5)
343 table.attach(lbl,0,1,2,3)
344 self.g_osd_color = gtk.Entry()
345 table.attach(self.g_osd_color,1,2,2,3)
346 # position
347 lbl = gtk.Label(_("Position"))
348 lbl.set_alignment(xalign=0.0,yalign=0.5)
349 table.attach(lbl,0,1,3,4)
350 self.g_osd_position = gtk.SpinButton(gtk.Adjustment(0,0,5000,1,5,0))
351 table.attach(self.g_osd_position,1,2,3,4)
352 # shadow offset
353 lbl = gtk.Label(_("Shadow offset"))
354 lbl.set_alignment(xalign=0.0,yalign=0.5)
355 table.attach(lbl,0,1,4,5)
356 self.g_osd_shadow = gtk.SpinButton(gtk.Adjustment(0,0,5000,1,5,0))
357 table.attach(self.g_osd_shadow,1,2,4,5)
358 # timeout
359 lbl = gtk.Label(_("Timeout"))
360 lbl.set_alignment(xalign=0.0,yalign=0.5)
361 table.attach(lbl,0,1,5,6)
362 self.g_osd_timeout = gtk.SpinButton(gtk.Adjustment(0,0,5000,1,5,0))
363 table.attach(self.g_osd_timeout,1,2,5,6)
365 return vbox
366 def create_gui(self):
367 setlocale(LC_ALL,"")
368 bindtextdomain( PACKAGE, LOCALEDIR )
369 textdomain( PACKAGE )
370 self.g_window = gobject.new(gtk.Window,
371 type=gtk.WINDOW_TOPLEVEL,
372 title="WmRadio Config",
373 allow_grow=gtk.FALSE,
374 allow_shrink=gtk.FALSE,
375 border_width=10)
376 self.g_window.connect("destroy", self.destroy)
377 vbox = gtk.VBox(gtk.FALSE,2)
378 self.g_window.add(vbox)
379 self.g_notebook = gtk.Notebook()
380 vbox.pack_start(self.g_notebook,gtk.TRUE,gtk.TRUE)
382 pagevbox = gtk.VBox(gtk.FALSE,2)
383 pagevbox.pack_start(self.frame1(),gtk.TRUE,gtk.TRUE)
384 pagevbox.pack_start(self.frame2(),gtk.TRUE,gtk.TRUE)
385 self.g_notebook.append_page(pagevbox,
386 gtk.Label(_("Config")));
387 self.g_notebook.append_page(self.frame3(),
388 gtk.Label(_("Names")));
389 lbl = gtk.Label(_("osd"))
390 self.g_notebook.append_page(self.frame4(),lbl);
391 if not XOSD:
392 lbl.set_sensitive(gtk.FALSE)
393 self.g_notebook.get_nth_page(2).set_sensitive(gtk.FALSE)
394 box = gtk.HBox(gtk.TRUE,2)
395 vbox.pack_start(box,gtk.FALSE,gtk.FALSE)
396 self.g_ok_button = gtk.Button(_("Ok"))
397 self.g_ok_button.connect("clicked",self.on_ok_click)
398 box.pack_start(self.g_ok_button,gtk.TRUE,gtk.TRUE,0)
399 self.g_apply_button = gtk.Button(_("Apply"))
400 self.g_apply_button.connect("clicked",self.on_apply_click)
401 box.pack_start(self.g_apply_button,gtk.TRUE,gtk.TRUE,0)
402 self.g_cancel_button = gtk.Button(_("Cancel"))
403 self.g_cancel_button.connect("clicked",self.quit)
404 box.pack_start(self.g_cancel_button,gtk.TRUE,gtk.TRUE,0)
405 self.g_window.show_all()
406 def run(self):
407 self.create_gui()
408 self.load_config()
409 gtk.main()
412 WmRadioConfig().run()