Ejemplo n.º 1
0
class SettingsWindow(Gtk.Window):
    def __init__(self, parent):
        self.parent = parent
        self.cfg = SettingsReader()
        self.notebook = Gtk.Notebook()
        self.auto_lock_time = Gtk.SpinButton()
        self.enable_switch = Gtk.CheckButton()
        self.auto_lock_switch = Gtk.CheckButton()
        self.password_button = Gtk.Button()
        self.generate_window()
        self.generate_components()

    def generate_window(self):
        Gtk.Window.__init__(self,
                            title=_("Settings"),
                            type=Gtk.WindowType.TOPLEVEL,
                            destroy_with_parent=True,
                            modal=True)
        self.connect("delete-event", self.close_window)
        self.resize(400, 300)
        self.set_size_request(400, 300)
        self.set_resizable(False)
        self.set_position(Gtk.WindowPosition.CENTER_ON_PARENT)
        self.set_transient_for(self.parent)
        self.connect("key_press_event", self.on_key_press)

    def show_window(self):
        self.show_all()

    def on_key_press(self, key, key_event):
        """
            Keyboard Listener handler
        """
        if Gdk.keyval_name(key_event.keyval) == "Escape":
            self.close_window()

    def generate_components(self):
        """
            Generate all the components
        """
        self.add(self.notebook)
        user_settings = self.generate_user_settings()
        user_settings.set_border_width(10)
        self.notebook.append_page(user_settings,
                                  Gtk.Label().new(_('Behavior')))

        login_settings = self.generate_login_settings()
        login_settings.set_border_width(10)
        self.notebook.append_page(login_settings,
                                  Gtk.Label().new(_('Account')))

    def generate_login_settings(self):
        """
            Create a box with login settings components
            :return (Gtk.Box): Box contains all the components
        """
        main_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
        password_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
        lock_enabled = bool(self.cfg.read("state", "login"))
        self.enable_switch.set_active(lock_enabled)
        self.enable_switch.connect("toggled", self.on_switch_activated)

        password_label = Gtk.Label()
        password_label.set_label(_("Password protection"))

        self.password_button.get_style_context().add_class("flat")
        self.password_button.get_style_context().add_class("text-button")
        self.password_button.set_label("******")
        self.password_button.connect("clicked", self.new_password_window)
        self.password_button.set_sensitive(lock_enabled)

        password_box.pack_start(self.enable_switch, False, True, 6)
        password_box.pack_start(password_label, False, True, 6)
        password_box.pack_start(self.password_button, False, True, 6)

        main_box.pack_start(password_box, False, True, 6)
        return main_box

    def generate_user_settings(self):
        """
            Create a box with user settings components
            :return (Gtk.Box): Box contains all the components
        """
        main_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)

        is_auto_lock_active = bool(self.cfg.read("auto-lock", "preferences"))
        auto_lock_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
        auto_lock_label = Gtk.Label().new(_("Auto-lock the application (m):"))
        self.auto_lock_switch.set_sensitive(self.cfg.read("state", "login"))
        self.auto_lock_switch.set_active(is_auto_lock_active)
        self.auto_lock_switch.connect("toggled", self.on_auto_lock_activated)

        default_value = self.cfg.read("auto-lock-time", "preferences")
        if default_value < 1 or default_value > 10:
            default_value = 3
        adjustment = Gtk.Adjustment(value=default_value,
                                    lower=1,
                                    upper=10,
                                    step_increment=1,
                                    page_increment=1,
                                    page_size=0)
        self.auto_lock_time.connect("value-changed",
                                    self.on_auto_lock_time_changed)
        self.auto_lock_time.set_adjustment(adjustment)
        self.auto_lock_time.set_sensitive(is_auto_lock_active)
        self.auto_lock_time.set_value(default_value)

        auto_lock_box.pack_start(self.auto_lock_switch, False, True, 6)
        auto_lock_box.pack_start(auto_lock_label, False, True, 6)
        auto_lock_box.pack_start(self.auto_lock_time, False, True, 12)

        main_box.pack_start(auto_lock_box, False, True, 6)
        return main_box

    def new_password_window(self, *args):
        """
            Show a new password window
        """
        pass_window = PasswordWindow(self)
        pass_window.show_window()

    def on_auto_lock_time_changed(self, spin_button):
        """
            Update auto lock time
        """
        self.cfg.update("auto-lock-time", spin_button.get_value_as_int(),
                        "preferences")
        logging.info("Auto lock time updated")

    def on_switch_activated(self, switch, *args):
        """
            Update password state : enabled/disabled
        """
        self.password_button.set_sensitive(switch.get_active())
        self.cfg.update("state", switch.get_active(), "login")
        if switch.get_active():
            password = self.cfg.read("password", "login")
            if len(password) == 0:
                self.new_password_window()
        self.auto_lock_switch.set_sensitive(switch.get_active())
        logging.info("Password enabled/disabled")
        self.parent.refresh_window()

    def on_auto_lock_activated(self, switch, *args):
        """
            Update auto-lock state : enabled/disabled
        """
        self.auto_lock_time.set_sensitive(switch.get_active())
        self.cfg.update("auto-lock", switch.get_active(), "preferences")
        logging.info("Auto lock state updated")

    def close_window(self, *args):
        """
            Close the window
        """
        logging.debug("SettingsWindow closed")
        self.destroy()
Ejemplo n.º 2
0
class SettingsWindow(Gtk.Window):

    def __init__(self, parent):
        self.parent = parent
        self.cfg = SettingsReader()
        self.notebook = Gtk.Notebook()
        self.auto_lock_time = Gtk.SpinButton()
        self.enable_switch = Gtk.CheckButton()
        self.auto_lock_switch = Gtk.CheckButton()
        self.password_button = Gtk.Button()
        self.generate_window()
        self.generate_components()

    def generate_window(self):
        Gtk.Window.__init__(self, title=_("Settings"), type=Gtk.WindowType.TOPLEVEL,
                            destroy_with_parent=True, modal=True)
        self.connect("delete-event", self.close_window)
        self.resize(400, 300)
        self.set_size_request(400, 300)
        self.set_resizable(False)
        self.set_position(Gtk.WindowPosition.CENTER_ON_PARENT)
        self.set_transient_for(self.parent)
        self.connect("key_press_event", self.on_key_press)

    def show_window(self):
        self.show_all()

    def on_key_press(self, key, key_event):
        """
            Keyboard Listener handler
        """
        if Gdk.keyval_name(key_event.keyval) == "Escape":
            self.close_window()

    def generate_components(self):
        """
            Generate all the components
        """
        self.add(self.notebook)
        user_settings = self.generate_user_settings()
        user_settings.set_border_width(10)
        self.notebook.append_page(
            user_settings, Gtk.Label().new(_('Behavior')))

        login_settings = self.generate_login_settings()
        login_settings.set_border_width(10)
        self.notebook.append_page(
            login_settings, Gtk.Label().new(_('Account')))

    def generate_login_settings(self):
        """
            Create a box with login settings components
            :return (Gtk.Box): Box contains all the components
        """
        main_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
        password_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
        lock_enabled = bool(self.cfg.read("state", "login"))
        self.enable_switch.set_active(lock_enabled)
        self.enable_switch.connect("toggled", self.on_switch_activated)

        password_label = Gtk.Label()
        password_label.set_label(_("Password protection"))

        self.password_button.get_style_context().add_class("flat")
        self.password_button.get_style_context().add_class("text-button")
        self.password_button.set_label("******")
        self.password_button.connect("clicked", self.new_password_window)
        self.password_button.set_sensitive(lock_enabled)

        password_box.pack_start(self.enable_switch, False, True, 6)
        password_box.pack_start(password_label, False, True, 6)
        password_box.pack_start(self.password_button, False, True, 6)

        main_box.pack_start(password_box, False, True, 6)
        return main_box

    def generate_user_settings(self):
        """
            Create a box with user settings components
            :return (Gtk.Box): Box contains all the components
        """
        main_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)

        is_auto_lock_active = bool(self.cfg.read("auto-lock", "preferences"))
        auto_lock_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
        auto_lock_label = Gtk.Label().new(_("Auto-lock the application (m):"))
        self.auto_lock_switch.set_sensitive(self.cfg.read("state", "login"))
        self.auto_lock_switch.set_active(is_auto_lock_active)
        self.auto_lock_switch.connect("toggled", self.on_auto_lock_activated)

        default_value = self.cfg.read("auto-lock-time", "preferences")
        if default_value < 1 or default_value > 10:
            default_value = 3
        adjustment = Gtk.Adjustment(value=default_value, lower=1, upper=10,
                                    step_increment=1, page_increment=1, page_size=0)
        self.auto_lock_time.connect(
            "value-changed", self.on_auto_lock_time_changed)
        self.auto_lock_time.set_adjustment(adjustment)
        self.auto_lock_time.set_sensitive(is_auto_lock_active)
        self.auto_lock_time.set_value(default_value)

        auto_lock_box.pack_start(self.auto_lock_switch, False, True, 6)
        auto_lock_box.pack_start(auto_lock_label, False, True, 6)
        auto_lock_box.pack_start(self.auto_lock_time, False, True, 12)

        main_box.pack_start(auto_lock_box, False, True, 6)
        return main_box

    def new_password_window(self, *args):
        """
            Show a new password window
        """
        pass_window = PasswordWindow(self)
        pass_window.show_window()

    def on_auto_lock_time_changed(self, spin_button):
        """
            Update auto lock time
        """
        self.cfg.update("auto-lock-time",
                        spin_button.get_value_as_int(), "preferences")
        logging.info("Auto lock time updated")

    def on_switch_activated(self, switch, *args):
        """
            Update password state : enabled/disabled
        """
        self.password_button.set_sensitive(switch.get_active())
        self.cfg.update("state", switch.get_active(), "login")
        if switch.get_active():
            password = self.cfg.read("password", "login")
            if len(password) == 0:
                self.new_password_window()
        self.auto_lock_switch.set_sensitive(switch.get_active())
        logging.info("Password enabled/disabled")
        self.parent.refresh_window()

    def on_auto_lock_activated(self, switch, *args):
        """
            Update auto-lock state : enabled/disabled
        """
        self.auto_lock_time.set_sensitive(switch.get_active())
        self.cfg.update("auto-lock", switch.get_active(), "preferences")
        logging.info("Auto lock state updated")

    def close_window(self, *args):
        """
            Close the window
        """
        logging.debug("SettingsWindow closed")
        self.destroy()
class PasswordWindow(Gtk.Window):

    def __init__(self, window):
        self.parent = window
        self.cfg = SettingsReader()

        self.hb = Gtk.HeaderBar()
        self.apply_button = Gtk.Button.new_with_label(_("Apply"))
        self.new_entry = Gtk.Entry()
        self.new2_entry = Gtk.Entry()
        self.old_entry = Gtk.Entry()

        self.generate_window()
        self.generate_components()
        self.generate_header_bar()

    def generate_window(self):
        Gtk.Window.__init__(self, type=Gtk.WindowType.TOPLEVEL, title=_("Change password"),
                            modal=True, destroy_with_parent=True)
        self.connect("delete-event", self.close_window)
        self.resize(300, 100)
        self.set_border_width(18)
        self.set_size_request(300, 100)
        self.set_position(Gtk.WindowPosition.CENTER_ON_PARENT)
        self.set_resizable(False)
        self.set_transient_for(self.parent)
        self.connect("key_press_event", self.on_key_press)

    def show_window(self):
        self.show_all()

    def on_key_press(self, key, key_event):
        """
            Keyboard listener handler
        """
        if Gdk.keyval_name(key_event.keyval) == "Escape":
            self.close_window()

    def generate_components(self):
        """
            Generate window components
        """
        main_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
        box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)

        if len(self.cfg.read("password", "login")) != 0:
            box_old = Gtk.Box(
                orientation=Gtk.Orientation.HORIZONTAL, spacing=18)
            old_label = Gtk.Label()
            old_label.set_text(_("Old password"))
            self.old_entry.connect("changed", self.on_type_password)
            self.old_entry.set_visibility(False)
            box_old.pack_end(self.old_entry, False, True, 0)
            box_old.pack_end(old_label, False, True, 0)
            box.add(box_old)

        box_new = Gtk.Box(
            orientation=Gtk.Orientation.HORIZONTAL, spacing=18)
        new_label = Gtk.Label()
        new_label.set_text(_("New password"))

        self.new_entry.connect("changed", self.on_type_password)
        self.new_entry.set_visibility(False)
        box_new.pack_end(self.new_entry, False, True, 0)
        box_new.pack_end(new_label, False, True, 0)

        box_new2 = Gtk.Box(
            orientation=Gtk.Orientation.HORIZONTAL, spacing=18)
        new2_label = Gtk.Label()
        new2_label.set_text(_("Repeat new password"))
        self.new2_entry.connect("changed", self.on_type_password)
        self.new2_entry.set_visibility(False)
        box_new2.pack_end(self.new2_entry, False, True, 0)
        box_new2.pack_end(new2_label, False, True, 0)

        box.add(box_new)
        box.add(box_new2)

        main_box.pack_start(box, False, True, 6)
        self.add(main_box)

    def update_password(self, *args):
        """
            Update user password
        """
        password = sha256(
            self.new_entry.get_text().encode("utf-8")).hexdigest()
        self.cfg.update("password", password, "login")
        logging.debug("Password changed successfully")
        self.close_window()

    def on_type_password(self, entry):
        """
            Validate the old & new password
        """
        pwd = self.cfg.read("password", "login")
        old_is_ok = True
        if self.new_entry.get_text() != self.new2_entry.get_text():
            self.new_entry.set_icon_from_icon_name(Gtk.EntryIconPosition.SECONDARY,
                                                   "dialog-error-symbolic")
            self.new2_entry.set_icon_from_icon_name(Gtk.EntryIconPosition.SECONDARY,
                                                    "dialog-error-symbolic")
            are_diff = True
        elif len(self.new_entry.get_text()) == 0:
            are_diff = True
        elif len(self.new_entry.get_text()) == 0:
            are_diff = True
        else:
            are_diff = False
        if len(pwd) != 0:
            if sha256(self.old_entry.get_text().encode('utf-8')).hexdigest() != pwd:
                self.old_entry.set_icon_from_icon_name(Gtk.EntryIconPosition.SECONDARY,
                                                       "dialog-error-symbolic")
                old_is_ok = False
            else:
                old_is_ok = True
            if old_is_ok:
                self.old_entry.set_icon_from_icon_name(
                    Gtk.EntryIconPosition.SECONDARY, None)
        if not are_diff:
            self.new_entry.set_icon_from_icon_name(
                Gtk.EntryIconPosition.SECONDARY, None)
            self.new2_entry.set_icon_from_icon_name(
                Gtk.EntryIconPosition.SECONDARY, None)
        self.apply_button.set_sensitive(not are_diff and old_is_ok)

    def generate_header_bar(self):
        """
            Generate header bar box
        """
        left_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
        right_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)

        cancel_button = Gtk.Button.new_with_label(_("Cancel"))
        cancel_button.connect("clicked", self.close_window)
        cancel_button.get_style_context().add_class("destructive-action")
        left_box.add(cancel_button)

        self.apply_button.get_style_context().add_class("suggested-action")
        self.apply_button.connect("clicked", self.update_password)
        self.apply_button.set_sensitive(False)
        right_box.add(self.apply_button)

        self.hb.pack_start(left_box)
        self.hb.pack_end(right_box)
        self.set_titlebar(self.hb)

    def close_window(self, *args):
        """
            Close the window
        """
        logging.debug("Closing PasswordWindow")
        self.destroy()
Ejemplo n.º 4
0
class PasswordWindow(Gtk.Window):
    def __init__(self, window):
        self.parent = window
        self.cfg = SettingsReader()

        self.hb = Gtk.HeaderBar()
        self.apply_button = Gtk.Button.new_with_label(_("Apply"))
        self.new_entry = Gtk.Entry()
        self.new2_entry = Gtk.Entry()
        self.old_entry = Gtk.Entry()

        self.generate_window()
        self.generate_components()
        self.generate_header_bar()

    def generate_window(self):
        Gtk.Window.__init__(self,
                            type=Gtk.WindowType.TOPLEVEL,
                            title=_("Change password"),
                            modal=True,
                            destroy_with_parent=True)
        self.connect("delete-event", self.close_window)
        self.resize(300, 100)
        self.set_border_width(18)
        self.set_size_request(300, 100)
        self.set_position(Gtk.WindowPosition.CENTER_ON_PARENT)
        self.set_resizable(False)
        self.set_transient_for(self.parent)
        self.connect("key_press_event", self.on_key_press)

    def show_window(self):
        self.show_all()

    def on_key_press(self, key, key_event):
        """
            Keyboard listener handler
        """
        if Gdk.keyval_name(key_event.keyval) == "Escape":
            self.close_window()

    def generate_components(self):
        """
            Generate window components
        """
        main_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
        box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)

        if len(self.cfg.read("password", "login")) != 0:
            box_old = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL,
                              spacing=18)
            old_label = Gtk.Label()
            old_label.set_text(_("Old password"))
            self.old_entry.connect("changed", self.on_type_password)
            self.old_entry.set_visibility(False)
            box_old.pack_end(self.old_entry, False, True, 0)
            box_old.pack_end(old_label, False, True, 0)
            box.add(box_old)

        box_new = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=18)
        new_label = Gtk.Label()
        new_label.set_text(_("New password"))

        self.new_entry.connect("changed", self.on_type_password)
        self.new_entry.set_visibility(False)
        box_new.pack_end(self.new_entry, False, True, 0)
        box_new.pack_end(new_label, False, True, 0)

        box_new2 = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=18)
        new2_label = Gtk.Label()
        new2_label.set_text(_("Repeat new password"))
        self.new2_entry.connect("changed", self.on_type_password)
        self.new2_entry.set_visibility(False)
        box_new2.pack_end(self.new2_entry, False, True, 0)
        box_new2.pack_end(new2_label, False, True, 0)

        box.add(box_new)
        box.add(box_new2)

        main_box.pack_start(box, False, True, 6)
        self.add(main_box)

    def update_password(self, *args):
        """
            Update user password
        """
        password = sha256(
            self.new_entry.get_text().encode("utf-8")).hexdigest()
        self.cfg.update("password", password, "login")
        logging.debug("Password changed successfully")
        self.close_window()

    def on_type_password(self, entry):
        """
            Validate the old & new password
        """
        pwd = self.cfg.read("password", "login")
        old_is_ok = True
        if self.new_entry.get_text() != self.new2_entry.get_text():
            self.new_entry.set_icon_from_icon_name(
                Gtk.EntryIconPosition.SECONDARY, "dialog-error-symbolic")
            self.new2_entry.set_icon_from_icon_name(
                Gtk.EntryIconPosition.SECONDARY, "dialog-error-symbolic")
            are_diff = True
        elif len(self.new_entry.get_text()) == 0:
            are_diff = True
        elif len(self.new_entry.get_text()) == 0:
            are_diff = True
        else:
            are_diff = False
        if len(pwd) != 0:
            if sha256(self.old_entry.get_text().encode(
                    'utf-8')).hexdigest() != pwd:
                self.old_entry.set_icon_from_icon_name(
                    Gtk.EntryIconPosition.SECONDARY, "dialog-error-symbolic")
                old_is_ok = False
            else:
                old_is_ok = True
            if old_is_ok:
                self.old_entry.set_icon_from_icon_name(
                    Gtk.EntryIconPosition.SECONDARY, None)
        if not are_diff:
            self.new_entry.set_icon_from_icon_name(
                Gtk.EntryIconPosition.SECONDARY, None)
            self.new2_entry.set_icon_from_icon_name(
                Gtk.EntryIconPosition.SECONDARY, None)
        self.apply_button.set_sensitive(not are_diff and old_is_ok)

    def generate_header_bar(self):
        """
            Generate header bar box
        """
        left_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
        right_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)

        cancel_button = Gtk.Button.new_with_label(_("Cancel"))
        cancel_button.connect("clicked", self.close_window)
        cancel_button.get_style_context().add_class("destructive-action")
        left_box.add(cancel_button)

        self.apply_button.get_style_context().add_class("suggested-action")
        self.apply_button.connect("clicked", self.update_password)
        self.apply_button.set_sensitive(False)
        right_box.add(self.apply_button)

        self.hb.pack_start(left_box)
        self.hb.pack_end(right_box)
        self.set_titlebar(self.hb)

    def close_window(self, *args):
        """
            Close the window
        """
        logging.debug("Closing PasswordWindow")
        self.destroy()