1 | #!/usr/bin/env python3 |
---|
2 | |
---|
3 | |
---|
4 | import gi |
---|
5 | gi.require_version('Gtk', '3.0') |
---|
6 | from gi.repository import Gtk, Pango, GdkPixbuf, Gdk, Gio, GObject,GLib,Gdk |
---|
7 | |
---|
8 | from . import settings |
---|
9 | import gettext |
---|
10 | gettext.textdomain(settings.TEXT_DOMAIN) |
---|
11 | _ = gettext.gettext |
---|
12 | |
---|
13 | import os |
---|
14 | import json |
---|
15 | import codecs |
---|
16 | import io |
---|
17 | import glob |
---|
18 | import threading |
---|
19 | |
---|
20 | BANNERS_PATH="/usr/share/bell-scheduler/banners/" |
---|
21 | |
---|
22 | class EditBox(Gtk.VBox): |
---|
23 | |
---|
24 | def __init__(self): |
---|
25 | |
---|
26 | Gtk.VBox.__init__(self) |
---|
27 | |
---|
28 | self.core=Core.Core.get_core() |
---|
29 | |
---|
30 | #def __init__ |
---|
31 | |
---|
32 | def init_form(self): |
---|
33 | |
---|
34 | try: |
---|
35 | self.editBox.remove(self.editBox.main_box) |
---|
36 | except: |
---|
37 | pass |
---|
38 | |
---|
39 | #def init_form |
---|
40 | |
---|
41 | def render_form(self): |
---|
42 | |
---|
43 | builder=Gtk.Builder() |
---|
44 | builder.set_translation_domain(settings.TEXT_DOMAIN) |
---|
45 | |
---|
46 | ui_path=self.core.ui_path |
---|
47 | builder.add_from_file(ui_path) |
---|
48 | |
---|
49 | self.css_file=self.core.rsrc_dir+"bell-scheduler.css" |
---|
50 | |
---|
51 | self.main_box=builder.get_object("bell_edit_box") |
---|
52 | self.cron_frame=builder.get_object("cron_frame") |
---|
53 | self.hour_spinbutton=builder.get_object("hour_spinbutton") |
---|
54 | self.minute_spinbutton=builder.get_object("minute_spinbutton") |
---|
55 | self.monday_tb=builder.get_object("monday_togglebutton") |
---|
56 | self.tuesday_tb=builder.get_object("tuesday_togglebutton") |
---|
57 | self.wednesday_tb=builder.get_object("wednesday_togglebutton") |
---|
58 | self.thursday_tb=builder.get_object("thursday_togglebutton") |
---|
59 | self.friday_tb=builder.get_object("friday_togglebutton") |
---|
60 | |
---|
61 | self.data_frame=builder.get_object("data_frame") |
---|
62 | self.name_label=builder.get_object("name_label") |
---|
63 | self.name_entry=builder.get_object("name_entry") |
---|
64 | self.image_label=builder.get_object("image_label") |
---|
65 | self.stock_rb=builder.get_object("stock_radiobutton") |
---|
66 | self.custom_rb=builder.get_object("custom_radiobutton") |
---|
67 | self.image_fc=builder.get_object("image_filechosser") |
---|
68 | self.sound_label=builder.get_object("sound_label") |
---|
69 | self.random_rb=builder.get_object("random_radiobutton") |
---|
70 | self.file_rb=builder.get_object("file_radiobutton") |
---|
71 | self.url_rb=builder.get_object("url_radiobutton") |
---|
72 | self.sound_dc=builder.get_object("sound_folderchosser") |
---|
73 | self.sound_fc=builder.get_object("sound_filechosser") |
---|
74 | self.sound_url=builder.get_object("url_entry") |
---|
75 | |
---|
76 | self.duration_label=builder.get_object("duration_label") |
---|
77 | self.duration_entry=builder.get_object("duration_entry") |
---|
78 | self.note_label=builder.get_object("note_label") |
---|
79 | |
---|
80 | self.weekdays=[] |
---|
81 | self.weekdays.append(self.monday_tb) |
---|
82 | self.weekdays.append(self.tuesday_tb) |
---|
83 | self.weekdays.append(self.wednesday_tb) |
---|
84 | self.weekdays.append(self.thursday_tb) |
---|
85 | self.weekdays.append(self.friday_tb) |
---|
86 | |
---|
87 | self.image_cb=builder.get_object("image_combobox") |
---|
88 | self.image_store=Gtk.ListStore(GdkPixbuf.Pixbuf,str) |
---|
89 | |
---|
90 | for x in glob.glob(BANNERS_PATH+"*.png"): |
---|
91 | f_name=x.replace(BANNERS_PATH,"").split(".png")[0] |
---|
92 | image=Gtk.Image() |
---|
93 | image.set_from_file(x) |
---|
94 | pixbuf=image.get_pixbuf() |
---|
95 | pixbuf=pixbuf.scale_simple(64,64,GdkPixbuf.InterpType.BILINEAR) |
---|
96 | self.image_store.append([pixbuf,f_name]) |
---|
97 | |
---|
98 | |
---|
99 | self.image_cb.set_model(self.image_store) |
---|
100 | pixbuf_renderer=Gtk.CellRendererPixbuf() |
---|
101 | |
---|
102 | self.image_cb.pack_start(pixbuf_renderer,True) |
---|
103 | self.image_cb.add_attribute(pixbuf_renderer,"pixbuf",0) |
---|
104 | |
---|
105 | self.edit=False |
---|
106 | self.bell_to_edit="" |
---|
107 | self.pack_start(self.main_box,True,True,0) |
---|
108 | self.set_css_info() |
---|
109 | self.connect_signals() |
---|
110 | self.init_data_form() |
---|
111 | |
---|
112 | #def render_form_ |
---|
113 | |
---|
114 | def set_css_info(self): |
---|
115 | |
---|
116 | self.style_provider=Gtk.CssProvider() |
---|
117 | |
---|
118 | f=Gio.File.new_for_path(self.css_file) |
---|
119 | self.style_provider.load_from_file(f) |
---|
120 | |
---|
121 | Gtk.StyleContext.add_provider_for_screen(Gdk.Screen.get_default(),self.style_provider,Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION) |
---|
122 | self.name_label.set_name("EDIT_LABEL") |
---|
123 | self.image_label.set_name("EDIT_LABEL") |
---|
124 | self.sound_label.set_name("EDIT_LABEL") |
---|
125 | self.duration_label.set_name("EDIT_LABEL") |
---|
126 | self.note_label.set_name("NOTE_LABEL") |
---|
127 | |
---|
128 | #def set-css_info |
---|
129 | |
---|
130 | def connect_signals(self): |
---|
131 | |
---|
132 | self.stock_rb.connect("toggled",self.image_toggled_button,"stock") |
---|
133 | self.custom_rb.connect("toggled",self.image_toggled_button,"custom") |
---|
134 | self.random_rb.connect("toggled",self.sound_toggled_button,"random") |
---|
135 | self.file_rb.connect("toggled",self.sound_toggled_button,"file") |
---|
136 | self.url_rb.connect("toggled",self.sound_toggled_button,"url") |
---|
137 | self.image_fc.connect("file-set",self.check_mimetype_image) |
---|
138 | self.sound_fc.connect("file-set",self.check_mimetype_sound) |
---|
139 | |
---|
140 | #def connect_signals |
---|
141 | |
---|
142 | def init_threads(self): |
---|
143 | |
---|
144 | self.checking_data_t=threading.Thread(target=self.checking_data) |
---|
145 | |
---|
146 | self.checking_data_t.daemon=True |
---|
147 | |
---|
148 | self.checking_data_t.launched=False |
---|
149 | |
---|
150 | GObject.threads_init() |
---|
151 | |
---|
152 | #def init_threads |
---|
153 | |
---|
154 | def init_data_form(self): |
---|
155 | |
---|
156 | self.image_op="stock" |
---|
157 | self.image_fc.set_sensitive(False) |
---|
158 | self.sound_op="file" |
---|
159 | self.sound_dc.set_sensitive(False) |
---|
160 | self.sound_url.set_sensitive(False) |
---|
161 | self.duration_entry.set_value(30) |
---|
162 | self.init_threads() |
---|
163 | |
---|
164 | #def init_data_form |
---|
165 | |
---|
166 | |
---|
167 | def image_toggled_button(self,button,name): |
---|
168 | |
---|
169 | if button.get_active(): |
---|
170 | if name=="stock": |
---|
171 | self.image_cb.set_sensitive(True) |
---|
172 | self.image_fc.set_sensitive(False) |
---|
173 | self.image_op="stock" |
---|
174 | |
---|
175 | else: |
---|
176 | self.image_cb.set_sensitive(False) |
---|
177 | self.image_fc.set_sensitive(True) |
---|
178 | self.image_op="custom" |
---|
179 | |
---|
180 | #def image_toggled_button |
---|
181 | |
---|
182 | |
---|
183 | def sound_toggled_button(self,button,name): |
---|
184 | |
---|
185 | if button.get_active(): |
---|
186 | if name=="random": |
---|
187 | self.sound_dc.set_sensitive(True) |
---|
188 | self.sound_fc.set_sensitive(False) |
---|
189 | self.sound_url.set_sensitive(False) |
---|
190 | self.sound_op="random" |
---|
191 | |
---|
192 | else: |
---|
193 | if name=="file": |
---|
194 | self.sound_dc.set_sensitive(False) |
---|
195 | self.sound_fc.set_sensitive(True) |
---|
196 | self.sound_url.set_sensitive(False) |
---|
197 | self.sound_op="file" |
---|
198 | else: |
---|
199 | self.sound_dc.set_sensitive(False) |
---|
200 | self.sound_fc.set_sensitive(False) |
---|
201 | self.sound_url.set_sensitive(True) |
---|
202 | self.sound_op="url" |
---|
203 | |
---|
204 | #def sound_toggled_button |
---|
205 | |
---|
206 | |
---|
207 | def load_values(self,bell): |
---|
208 | |
---|
209 | bell_to_edit=self.core.mainWindow.bells_info[bell] |
---|
210 | |
---|
211 | self.hour_spinbutton.set_value(bell_to_edit["hour"]) |
---|
212 | self.minute_spinbutton.set_value(bell_to_edit["minute"]) |
---|
213 | self.name_entry.set_text(bell_to_edit["name"]) |
---|
214 | |
---|
215 | weekdays=bell_to_edit["weekdays"] |
---|
216 | if weekdays["0"]: |
---|
217 | self.monday_tb.set_active(True) |
---|
218 | if weekdays["1"]: |
---|
219 | self.tuesday_tb.set_active(True) |
---|
220 | if weekdays["2"]: |
---|
221 | self.wednesday_tb.set_active(True) |
---|
222 | if weekdays["3"]: |
---|
223 | self.thursday_tb.set_active(True) |
---|
224 | if weekdays["4"]: |
---|
225 | self.friday_tb.set_active(True) |
---|
226 | |
---|
227 | image_op=bell_to_edit["image"]["option"] |
---|
228 | |
---|
229 | if image_op=="stock": |
---|
230 | self.stock_rb.set_active(True) |
---|
231 | if os.path.exists(bell_to_edit["image"]["path"]): |
---|
232 | c=0 |
---|
233 | for i in self.image_store: |
---|
234 | if os.path.basename(bell_to_edit["image"]["path"]).split(".")[0]==i[1]: |
---|
235 | break |
---|
236 | c+=1 |
---|
237 | self.image_cb.set_active(c) |
---|
238 | else: |
---|
239 | self.custom_rb.set_active(True) |
---|
240 | if os.path.exists(bell_to_edit["image"]["path"]): |
---|
241 | self.image_fc.set_filename(bell_to_edit["image"]["path"]) |
---|
242 | |
---|
243 | sound_op=bell_to_edit["sound"]["option"] |
---|
244 | |
---|
245 | if sound_op=="random": |
---|
246 | self.random_rb.set_active(True) |
---|
247 | if os.path.exists(bell_to_edit["sound"]["path"]): |
---|
248 | self.sound_dc.set_filename(bell_to_edit["sound"]["path"]) |
---|
249 | elif sound_op=="file": |
---|
250 | self.file_rb.set_active(True) |
---|
251 | if os.path.exists(bell_to_edit["sound"]["path"]): |
---|
252 | self.sound_fc.set_filename(bell_to_edit["sound"]["path"]) |
---|
253 | else: |
---|
254 | self.url_rb.set_active(True) |
---|
255 | self.sound_url.set_text(bell_to_edit["sound"]["path"]) |
---|
256 | |
---|
257 | self.duration_entry.set_value(bell_to_edit["play"]["duration"]) |
---|
258 | |
---|
259 | self.edit=True |
---|
260 | self.bell_to_edit=bell |
---|
261 | self.active_bell=bell_to_edit["active"] |
---|
262 | |
---|
263 | #def load_values |
---|
264 | |
---|
265 | def gather_values(self,widget): |
---|
266 | |
---|
267 | self.files_tocheck={} |
---|
268 | self.files_tocheck["sound"]="" |
---|
269 | self.files_tocheck["image"]="" |
---|
270 | self.files_tocheck["url"]=None |
---|
271 | self.files_tocheck["directory"]="" |
---|
272 | |
---|
273 | |
---|
274 | if self.image_op=="stock": |
---|
275 | self.image_path=BANNERS_PATH+self.image_store[self.image_cb.get_active()][1]+".png" |
---|
276 | else: |
---|
277 | self.image_path=self.image_fc.get_filename() |
---|
278 | self.files_tocheck["image"]=self.image_path |
---|
279 | |
---|
280 | if self.sound_op=="random": |
---|
281 | self.sound_path=self.sound_dc.get_filename() |
---|
282 | self.files_tocheck["directory"]=self.sound_path |
---|
283 | elif self.sound_op=="file": |
---|
284 | self.sound_path=self.sound_fc.get_filename() |
---|
285 | self.files_tocheck["sound"]=self.sound_path |
---|
286 | else: |
---|
287 | self.sound_path=self.sound_url.get_text() |
---|
288 | self.files_tocheck["url"]=self.sound_path |
---|
289 | |
---|
290 | self.duration=self.duration_entry.get_value_as_int() |
---|
291 | |
---|
292 | self.core.mainWindow.waiting_label.set_text(self.core.mainWindow.get_msg(30)) |
---|
293 | self.core.mainWindow.waiting_window.show_all() |
---|
294 | self.init_threads() |
---|
295 | self.checking_data_t.start() |
---|
296 | GLib.timeout_add(100,self.pulsate_checking_data) |
---|
297 | |
---|
298 | #def gather_values |
---|
299 | |
---|
300 | def pulsate_checking_data(self): |
---|
301 | |
---|
302 | if self.checking_data_t.is_alive(): |
---|
303 | self.core.mainWindow.waiting_pbar.pulse() |
---|
304 | return True |
---|
305 | |
---|
306 | else: |
---|
307 | self.core.mainWindow.waiting_window.hide() |
---|
308 | if not self.check["result"]: |
---|
309 | self.core.mainWindow.manage_message(True,self.check["code"]) |
---|
310 | else: |
---|
311 | self.save_values() |
---|
312 | |
---|
313 | return False |
---|
314 | |
---|
315 | #def pulsate_checking_data |
---|
316 | |
---|
317 | def checking_data(self): |
---|
318 | |
---|
319 | self.check=self.core.bellmanager.check_data(self.name_entry.get_text(),self.files_tocheck) |
---|
320 | |
---|
321 | #def checking_data |
---|
322 | |
---|
323 | def save_values(self): |
---|
324 | |
---|
325 | bell=self.core.mainWindow.bells_info.copy() |
---|
326 | |
---|
327 | |
---|
328 | if self.edit: |
---|
329 | order=self.bell_to_edit |
---|
330 | action="edit" |
---|
331 | else: |
---|
332 | if len(bell)>0: |
---|
333 | keys=bell.keys() |
---|
334 | order=str(int(max(keys))+1) |
---|
335 | else: |
---|
336 | order="1" |
---|
337 | action="add" |
---|
338 | |
---|
339 | minute=self.minute_spinbutton.get_value_as_int() |
---|
340 | hour=self.hour_spinbutton.get_value_as_int() |
---|
341 | |
---|
342 | |
---|
343 | bell[order]={} |
---|
344 | bell[order]["hour"]=hour |
---|
345 | bell[order]["minute"]=minute |
---|
346 | bell[order]["weekdays"]={} |
---|
347 | bell[order]["weekdays"]["0"]=self.monday_tb.get_active() |
---|
348 | bell[order]["weekdays"]["1"]=self.tuesday_tb.get_active() |
---|
349 | bell[order]["weekdays"]["2"]=self.wednesday_tb.get_active() |
---|
350 | bell[order]["weekdays"]["3"]=self.thursday_tb.get_active() |
---|
351 | bell[order]["weekdays"]["4"]=self.friday_tb.get_active() |
---|
352 | |
---|
353 | count=0 |
---|
354 | if self.monday_tb.get_active(): |
---|
355 | count=count+1 |
---|
356 | if self.tuesday_tb.get_active(): |
---|
357 | count+=count+1 |
---|
358 | if self.wednesday_tb.get_active(): |
---|
359 | count+=count+1 |
---|
360 | if self.thursday_tb.get_active(): |
---|
361 | count+=count+1 |
---|
362 | if self.friday_tb.get_active(): |
---|
363 | count+=count+1 |
---|
364 | |
---|
365 | |
---|
366 | if count>0: |
---|
367 | if self.edit: |
---|
368 | active_bell=self.active_bell |
---|
369 | else: |
---|
370 | active_bell=True |
---|
371 | else: |
---|
372 | active_bell=False |
---|
373 | |
---|
374 | |
---|
375 | bell[order]["name"]=self.name_entry.get_text() |
---|
376 | |
---|
377 | bell[order]["image"]={} |
---|
378 | bell[order]["image"]["option"]=self.image_op |
---|
379 | if self.image_op=="custom": |
---|
380 | orig_image_path=self.image_path |
---|
381 | dest_image_path=os.path.join(self.core.images_path,os.path.basename(orig_image_path)) |
---|
382 | else: |
---|
383 | orig_image_path="" |
---|
384 | dest_image_path=self.image_path |
---|
385 | |
---|
386 | bell[order]["image"]["path"]=dest_image_path |
---|
387 | |
---|
388 | bell[order]["sound"]={} |
---|
389 | bell[order]["sound"]["option"]=self.sound_op |
---|
390 | if self.sound_op=="file": |
---|
391 | orig_sound_path=self.sound_path |
---|
392 | dest_sound_path=os.path.join(self.core.sounds_path,os.path.basename(orig_sound_path)) |
---|
393 | else: |
---|
394 | orig_sound_path="" |
---|
395 | dest_sound_path=self.sound_path |
---|
396 | |
---|
397 | bell[order]["sound"]["path"]=dest_sound_path |
---|
398 | |
---|
399 | |
---|
400 | bell[order]["play"]={} |
---|
401 | bell[order]["play"]["duration"]=self.duration |
---|
402 | |
---|
403 | |
---|
404 | bell[order]["active"]=active_bell |
---|
405 | |
---|
406 | |
---|
407 | result_copy=self.core.bellmanager.copy_media_files(orig_image_path,orig_sound_path) |
---|
408 | if result_copy['status']: |
---|
409 | result=self.core.bellmanager.save_conf(bell,order,action) |
---|
410 | if result['status']: |
---|
411 | self.core.mainWindow.load_info() |
---|
412 | self.core.bellBox.draw_bell(False,order) |
---|
413 | |
---|
414 | else: |
---|
415 | self.core.bellBox.draw_bell(False) |
---|
416 | |
---|
417 | |
---|
418 | self.core.mainWindow.search_entry.set_text("") |
---|
419 | self.core.mainWindow.manage_menubar(True) |
---|
420 | self.core.mainWindow.manage_down_buttons(False) |
---|
421 | self.core.mainWindow.stack_opt.set_transition_type(Gtk.StackTransitionType.SLIDE_LEFT) |
---|
422 | self.core.mainWindow.stack_opt.set_visible_child_name("bellBox") |
---|
423 | self.core.editBox.remove(self.core.editBox.main_box) |
---|
424 | |
---|
425 | if result_copy['status']: |
---|
426 | if result['status']: |
---|
427 | if self.edit: |
---|
428 | self.core.mainWindow.manage_message(False,15) |
---|
429 | else: |
---|
430 | self.core.mainWindow.manage_message(False,18) |
---|
431 | else: |
---|
432 | self.core.mainWindow.manage_message(True,result['code']) |
---|
433 | else: |
---|
434 | self.core.mainWindow.manage_message(True,result['code']) |
---|
435 | |
---|
436 | |
---|
437 | #def save_values |
---|
438 | |
---|
439 | |
---|
440 | def check_mimetype_image(self,widget): |
---|
441 | |
---|
442 | check=self.core.bellmanager.check_mimetypes(self.image_fc.get_filename(),"image") |
---|
443 | if check !=None: |
---|
444 | self.core.mainWindow.manage_message(True,check["code"]) |
---|
445 | else: |
---|
446 | self.core.mainWindow.msg_label.hide() |
---|
447 | |
---|
448 | #def check_mimetype_image |
---|
449 | |
---|
450 | def check_mimetype_sound(self,widget): |
---|
451 | |
---|
452 | check=self.core.bellmanager.check_mimetypes(self.sound_fc.get_filename(),"audio") |
---|
453 | if check !=None: |
---|
454 | self.core.mainWindow.manage_message(True,check["code"]) |
---|
455 | else: |
---|
456 | self.core.mainWindow.msg_label.hide() |
---|
457 | |
---|
458 | #def check_mimetype_sound |
---|
459 | |
---|
460 | |
---|
461 | |
---|
462 | #class EditBox |
---|
463 | |
---|
464 | from . import Core |
---|