2016-10-15 06:11:27 +02:00
|
|
|
# Safe Eyes is a utility to remind you to take break frequently
|
|
|
|
# to protect your eyes from eye strain.
|
|
|
|
|
|
|
|
# Copyright (C) 2016 Gobinath
|
|
|
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import gi
|
|
|
|
gi.require_version('Gtk', '3.0')
|
2017-01-10 12:52:55 +01:00
|
|
|
from gi.repository import Gtk, Gdk, GdkX11, GObject
|
2017-04-11 01:32:43 +02:00
|
|
|
from safeeyes import Utility
|
2016-10-15 06:11:27 +02:00
|
|
|
|
|
|
|
class SettingsDialog:
|
2017-01-10 12:52:55 +01:00
|
|
|
"""
|
|
|
|
Create and initialize SettingsDialog instance.
|
|
|
|
"""
|
|
|
|
def __init__(self, config, language, languages, on_save_settings, glade_file):
|
2016-10-15 06:11:27 +02:00
|
|
|
self.config = config
|
|
|
|
self.on_save_settings = on_save_settings
|
2017-01-10 12:52:55 +01:00
|
|
|
self.languages = []
|
2016-10-15 06:11:27 +02:00
|
|
|
|
|
|
|
builder = Gtk.Builder()
|
|
|
|
builder.add_from_file(glade_file)
|
|
|
|
builder.connect_signals(self)
|
|
|
|
|
2017-04-09 02:29:51 +02:00
|
|
|
# Get the UI components
|
2017-01-10 12:52:55 +01:00
|
|
|
self.window = builder.get_object('window_settings')
|
|
|
|
self.spin_short_break_duration = builder.get_object('spin_short_break_duration')
|
|
|
|
self.spin_long_break_duration = builder.get_object('spin_long_break_duration')
|
|
|
|
self.spin_interval_between_two_breaks = builder.get_object('spin_interval_between_two_breaks')
|
|
|
|
self.spin_short_between_long = builder.get_object('spin_short_between_long')
|
|
|
|
self.spin_time_to_prepare = builder.get_object('spin_time_to_prepare')
|
2017-02-06 03:09:04 +01:00
|
|
|
self.spin_idle_time_to_pause = builder.get_object('spin_idle_time_to_pause')
|
2017-04-03 20:18:23 +02:00
|
|
|
self.spin_postpone_duration = builder.get_object('spin_postpone_duration')
|
2017-01-10 12:52:55 +01:00
|
|
|
self.switch_strict_break = builder.get_object('switch_strict_break')
|
2017-02-01 01:20:17 +01:00
|
|
|
self.switch_audible_alert = builder.get_object('switch_audible_alert')
|
2017-01-10 12:52:55 +01:00
|
|
|
self.cmb_language = builder.get_object('cmb_language')
|
2017-04-08 15:01:48 +02:00
|
|
|
self.switch_screen_lock = builder.get_object('switch_screen_lock')
|
2017-04-08 14:18:38 +02:00
|
|
|
self.spin_time_to_screen_lock = builder.get_object('spin_time_to_screen_lock')
|
2017-01-10 12:52:55 +01:00
|
|
|
|
2017-04-09 02:29:51 +02:00
|
|
|
# Translate the UI labels
|
2017-01-10 12:52:55 +01:00
|
|
|
builder.get_object('lbl_short_break').set_label(language['ui_controls']['short_break_duration'])
|
|
|
|
builder.get_object('lbl_long_break').set_label(language['ui_controls']['long_break_duration'])
|
|
|
|
builder.get_object('lbl_interval_bettween_breaks').set_label(language['ui_controls']['interval_between_two_breaks'])
|
|
|
|
builder.get_object('lbl_short_per_long').set_label(language['ui_controls']['no_of_short_breaks_between_two_long_breaks'])
|
|
|
|
builder.get_object('lbl_time_to_prepare').set_label(language['ui_controls']['time_to_prepare_for_break'])
|
2017-02-06 03:09:04 +01:00
|
|
|
builder.get_object('lbl_idle_time_to_pause').set_label(language['ui_controls']['idle_time'])
|
2017-04-03 20:18:23 +02:00
|
|
|
builder.get_object('lbl_postpone_duration').set_label(language['ui_controls']['postpone_duration'])
|
2017-01-10 12:52:55 +01:00
|
|
|
builder.get_object('lbl_strict_break').set_label(language['ui_controls']['strict_break'])
|
2017-02-01 01:20:17 +01:00
|
|
|
builder.get_object('lbl_audible_alert').set_label(language['ui_controls']['audible_alert'])
|
2017-01-10 12:52:55 +01:00
|
|
|
builder.get_object('lbl_language').set_label(language['ui_controls']['language'])
|
2017-04-08 15:01:48 +02:00
|
|
|
builder.get_object('lbl_enable_screen_lock').set_label(language['ui_controls']['enable_screen_lock'])
|
2017-04-08 14:18:38 +02:00
|
|
|
builder.get_object('lbl_lock_screen_after').set_label(language['ui_controls']['time_to_screen_lock'])
|
2017-01-10 12:52:55 +01:00
|
|
|
builder.get_object('btn_cancel').set_label(language['ui_controls']['cancel'])
|
|
|
|
builder.get_object('btn_save').set_label(language['ui_controls']['save'])
|
2016-11-04 09:47:21 +01:00
|
|
|
|
2017-04-09 02:29:51 +02:00
|
|
|
# Set the current values of input fields
|
2016-10-15 06:11:27 +02:00
|
|
|
self.spin_short_break_duration.set_value(config['short_break_duration'])
|
|
|
|
self.spin_long_break_duration.set_value(config['long_break_duration'])
|
|
|
|
self.spin_interval_between_two_breaks.set_value(config['break_interval'])
|
|
|
|
self.spin_short_between_long.set_value(config['no_of_short_breaks_per_long_break'])
|
|
|
|
self.spin_time_to_prepare.set_value(config['pre_break_warning_time'])
|
2017-02-06 03:09:04 +01:00
|
|
|
self.spin_idle_time_to_pause.set_value(config['idle_time'])
|
2017-04-03 20:18:23 +02:00
|
|
|
self.spin_postpone_duration.set_value(config['postpone_duration'])
|
2016-10-23 13:56:54 +02:00
|
|
|
self.switch_strict_break.set_active(config['strict_break'])
|
2017-02-01 01:20:17 +01:00
|
|
|
self.switch_audible_alert.set_active(config['audible_alert'])
|
2017-04-09 02:29:51 +02:00
|
|
|
self.spin_time_to_screen_lock.set_value(config['time_to_screen_lock'])
|
2017-04-12 23:46:30 +02:00
|
|
|
|
|
|
|
# Check lock screen command
|
|
|
|
able_to_lock_screen = False
|
|
|
|
if config['lock_screen_command'] or Utility.lock_screen_command():
|
|
|
|
able_to_lock_screen = True
|
|
|
|
|
|
|
|
self.switch_screen_lock.set_sensitive(able_to_lock_screen)
|
|
|
|
self.switch_screen_lock.set_active(able_to_lock_screen and config['enable_screen_lock'])
|
2017-04-08 15:01:48 +02:00
|
|
|
self.on_switch_screen_lock_activate(self.switch_screen_lock, self.switch_screen_lock.get_active())
|
2016-10-15 06:11:27 +02:00
|
|
|
|
2017-01-10 12:52:55 +01:00
|
|
|
# Initialize the language combobox
|
|
|
|
language_list_store = Gtk.ListStore(GObject.TYPE_STRING)
|
2017-03-13 14:30:44 +01:00
|
|
|
language_index = 2
|
|
|
|
lang_code = config['language']
|
|
|
|
|
2017-04-09 02:29:51 +02:00
|
|
|
# Add 'System Language' as the first option
|
2017-03-13 14:30:44 +01:00
|
|
|
language_list_store.append([language['ui_controls']['system_language']])
|
|
|
|
language_list_store.append(['-'])
|
|
|
|
self.languages.append('system')
|
|
|
|
self.languages.append('system') # Dummy record for row separator
|
|
|
|
if 'system' == lang_code:
|
|
|
|
self.cmb_language.set_active(0)
|
|
|
|
|
2017-01-10 12:52:55 +01:00
|
|
|
for key in sorted(languages.keys()):
|
|
|
|
language_list_store.append([languages[key]])
|
|
|
|
self.languages.append(key)
|
2017-03-13 14:30:44 +01:00
|
|
|
if key == lang_code:
|
2017-01-10 12:52:55 +01:00
|
|
|
self.cmb_language.set_active(language_index)
|
|
|
|
language_index += 1
|
|
|
|
|
|
|
|
self.cmb_language.set_model(language_list_store)
|
2017-03-13 14:30:44 +01:00
|
|
|
self.cmb_language.set_row_separator_func(lambda m,i: m.get_value(i, 0) == '-')
|
2017-01-10 12:52:55 +01:00
|
|
|
cell = Gtk.CellRendererText()
|
|
|
|
self.cmb_language.pack_start(cell, True)
|
|
|
|
self.cmb_language.add_attribute(cell, 'text', 0)
|
|
|
|
|
2017-04-09 02:29:51 +02:00
|
|
|
|
2016-10-15 06:11:27 +02:00
|
|
|
def show(self):
|
2017-04-09 02:29:51 +02:00
|
|
|
"""
|
|
|
|
Show the SettingsDialog.
|
|
|
|
"""
|
2016-10-15 06:11:27 +02:00
|
|
|
self.window.show_all()
|
|
|
|
|
2017-04-09 02:29:51 +02:00
|
|
|
|
2017-04-08 15:01:48 +02:00
|
|
|
def on_switch_screen_lock_activate(self, switch, state):
|
2017-04-09 02:29:51 +02:00
|
|
|
"""
|
|
|
|
Event handler to the state change of the screen_lock switch.
|
|
|
|
Enable or disable the self.spin_time_to_screen_lock based on the state of the screen_lock switch.
|
|
|
|
"""
|
2017-04-08 15:01:48 +02:00
|
|
|
self.spin_time_to_screen_lock.set_sensitive(self.switch_screen_lock.get_active())
|
|
|
|
|
2017-04-09 02:29:51 +02:00
|
|
|
|
2016-10-15 06:11:27 +02:00
|
|
|
def on_window_delete(self, *args):
|
2017-04-09 02:29:51 +02:00
|
|
|
"""
|
|
|
|
Event handler for Settings dialog close action.
|
|
|
|
"""
|
2016-10-15 06:11:27 +02:00
|
|
|
self.window.destroy()
|
|
|
|
|
2017-04-09 02:29:51 +02:00
|
|
|
|
2016-10-15 06:11:27 +02:00
|
|
|
def on_save_clicked(self, button):
|
2017-04-09 02:29:51 +02:00
|
|
|
"""
|
|
|
|
Event handler for Save button click.
|
|
|
|
"""
|
2016-10-15 06:11:27 +02:00
|
|
|
self.config['short_break_duration'] = self.spin_short_break_duration.get_value_as_int()
|
|
|
|
self.config['long_break_duration'] = self.spin_long_break_duration.get_value_as_int()
|
|
|
|
self.config['break_interval'] = self.spin_interval_between_two_breaks.get_value_as_int()
|
|
|
|
self.config['no_of_short_breaks_per_long_break'] = self.spin_short_between_long.get_value_as_int()
|
|
|
|
self.config['pre_break_warning_time'] = self.spin_time_to_prepare.get_value_as_int()
|
2017-02-06 03:09:04 +01:00
|
|
|
self.config['idle_time'] = self.spin_idle_time_to_pause.get_value_as_int()
|
2017-04-03 20:18:23 +02:00
|
|
|
self.config['postpone_duration'] = self.spin_postpone_duration.get_value_as_int()
|
2016-10-23 13:56:54 +02:00
|
|
|
self.config['strict_break'] = self.switch_strict_break.get_active()
|
2017-02-01 01:20:17 +01:00
|
|
|
self.config['audible_alert'] = self.switch_audible_alert.get_active()
|
2017-01-10 12:52:55 +01:00
|
|
|
self.config['language'] = self.languages[self.cmb_language.get_active()]
|
2017-04-08 14:18:38 +02:00
|
|
|
self.config['time_to_screen_lock'] = self.spin_time_to_screen_lock.get_value_as_int()
|
2017-04-08 15:01:48 +02:00
|
|
|
self.config['enable_screen_lock'] = self.switch_screen_lock.get_active()
|
2016-10-15 06:11:27 +02:00
|
|
|
|
|
|
|
self.on_save_settings(self.config) # Call the provided save method
|
|
|
|
self.window.destroy() # Close the settings window
|
|
|
|
|
2017-04-09 02:29:51 +02:00
|
|
|
|
2016-10-15 06:11:27 +02:00
|
|
|
def on_cancel_clicked(self, button):
|
2017-04-09 02:29:51 +02:00
|
|
|
"""
|
|
|
|
Event handler for Cancel button click.
|
|
|
|
"""
|
2016-10-15 06:11:27 +02:00
|
|
|
self.window.destroy()
|