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
10 LOCALEDIR
= "%s/share/locale" % (PREFIX
)
11 PACKAGE
= "wmradio-config.py"
12 SKINDIR
= "%s/lib/wmradio" % (PREFIX
)
15 XOSD
= "@HAVE_XOSD@" == "define"
18 def __init__(self
,afilename
):
24 def load(self
,afilename
):
25 self
.filename
= afilename
26 f
= open(afilename
,'r');
27 lines
= f
.readlines();
30 self
.sections
[section
] = {}
32 if line
[:1] == ';' or line
[:1] == '#' : line
= '';
35 e
= string
.find(line
,']');
36 section
= string
.lower(line
[1:e
])
37 self
.sections
[section
] = {}
39 eq
= string
.find(line
,"=")
41 variable
= string
.strip(string
.lower(line
[:eq
]))
42 value
= string
.strip(line
[eq
+1:])
43 e
= string
.find(value
,'\n')
46 self
.sections
[section
][variable
] = value
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
]))
55 def save_as(self
,newfile
):
56 self
.filename
= newfile
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):
66 val
= self
.get_variable(sec
,var
)
67 if val
== "" : return default
68 return string
.atoi(val
)
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 # --------------------------------------------------------------------
82 self
.g_quit_button
= None;
84 self
.g_ok_button
= None;
85 self
.g_cancel_button
= None;
87 self
.g_new_freq
= None;
88 self
.g_new_name
= None;
89 self
.g_delete_button
= 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
,"=")
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
):
119 def on_ok_click(self
, *args
):
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())
126 self
.cfg
.set_variable("config",
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())
139 self
.refresh_running_radio()
140 def destroy(self
, *args
):
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
)
152 self
.g_pressed
[i
].set_value(string
.atof(self
.cfg
.get_variable("config","preset%i" % i
)))
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,
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",
166 self
.g_osd_color
.set_text(self
.cfg
.get_variable("config",
168 self
.g_osd_position
.set_value(self
.cfg
.get_int_variable("config",
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",
174 self
.on_osd_changed(None)
175 def delete_selected_name(self
,model
,path
,iter,data
):
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
,
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
):
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
,"=")
207 if string
.lower(name
) == string
.lower(text
):
208 self
.g_jobs
.set(iter,
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
)
221 if file[-5:] == ".skin":
223 if len(skins
): self
.g_skin
.set_popdown_strings(skins
)
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)
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()
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)
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)
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
,
271 table
.attach(self
.g_pressed
[i
],1,2,i
,i
+1)
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)
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
,
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)
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)
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)
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)
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)
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)
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)
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)
366 def create_gui(self
):
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
,
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
);
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()
412 WmRadioConfig().run()