Source

vasm / src / vasm / ui / tui / modules / tui_skel.py

#!/usr/bin/env python

#    This file is part of VASM.
#
#    VASM is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License v3 as published by
#    the Free Software Foundation.
#
#    VASM 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 VASM.  If not, see <http://www.gnu.org/licenses/>.

""" tui_skel.py
    Text mode module to reset user settings """

__author__ = "Moises Henriquez"
__author_email__ = "moc.liamg@xnl.E0M"[::-1]

import urwid
import os
from vasm.backend import skel, useradmin
from vasm.ui.tui.support import dialogs, widgets
from vasm.backend.utils import _
import logging

logger = logging.getLogger('vasm')

VASM_CATEGORY = "Personal"
VASM_LABEL = "Reset Personal Settings"


class SettingsManage(dialogs.ModuleDialog):
    def __init__(self, parent):
        self.parent = parent
        msgx = _("Select which settings you would like to reset. "\
                 "WARNING: This cannot be undone.")
        acct = None
        for member in useradmin.UserModel().listUsers():
            if member.uid == os.geteuid():
                acct = member
                break
        assert acct is not None, "Unable to get user account from system for current user"
        self.skelmodel = skel.UserSkel(acct.login)
        settings = self.skelmodel.list_resettable_settings()
        self._settings_to_reset = []
        _body = []
        self.settings_boxes = []
        for _skel in settings:
            # the checkbox holds the file / dirname as the user_data argument
            desc = self.skelmodel._get_skel_description(_skel)
            cb = urwid.CheckBox(desc,
                                on_state_change = self.checkbox_toggle,
                                user_data = _skel)

            self.settings_boxes.append(cb)
            wrap = urwid.AttrWrap(cb, 'button', 'button sel')
            _body.append(wrap)
        #body = urwid.ListBox(_body)
        
        dialogs.ModuleDialog.__init__(
            self,
            header = _("Reset Personal Settings"),
            desc = [msgx],
            parent = self.parent,
            buttons = [("OK", self.on_ok_event),
                ("Cancel", self.close_module)],
            body = _body
        )
    
        self.body = urwid.LineBox(self.get_body())
        # Set the default focus to the interactive section of the frame
        self.set_focus('body')
    
    def checkbox_toggle(self, checkbox, newstate, fdname):
        """ Trigger changes when a checkbox is toggled.
        Each toggle box holds the file/dirname that it relates to in the user_data
        argument which is parsed to this method as fdname"""
        if newstate == False:
            if fdname in self._settings_to_reset:
                self._settings_to_reset.remove(fdname)
        else:
            if fdname not in self._settings_to_reset:
                self._settings_to_reset.append(fdname)
    
    def on_ok_event(self, widget):
        """ Define what happens when the ok button is pressed """
        # Check if we have to do anything at all
        if len(self._settings_to_reset) == 0:
            # nothing to do.
            dia = dialogs.Info(
                parent = self.parent,
                message = _("No settings have been seleted for resetting.  "\
                            "Select which settings to reset first."),
                buttons = [("OK", self.parent.return_to_module_stack)])
            return self.parent.pop_module_dialog(dia)

        txt = "%s \n\n%s"% (" | ".join(self._settings_to_reset),
                           _("Are you sure you want to reset these settings?"))
        dia = dialogs.Question(message = txt,
                           parent = self.parent,
                           buttons = [("Yes", self.do_reset_settings),
                            ("No", self.parent.return_to_module_stack)])
        self.parent.pop_module_dialog(dia)
    
    def do_reset_settings(self, widget=None):
        """ Perform the settings reset action"""
        # first we want to close the current dialog...
        self.parent.return_to_module_stack(widget)
        # Now perform the actions
        try:
            self.skelmodel.reset_settings(self._settings_to_reset)
            logger.debug('Running skel.')
        except AssertionError as e:
            dia = dialogs.Error(
                parent = self.parent,
                message = _("An error has occurred!") + "\n\n%s"% e,
                buttons = [("OK", self.return_home)]
                )
            logger.error('%s'%e)
            return self.parent.pop_module_dialog(dia)
        except Error as e:
            dia = dialogs.Error(
                parent = self.parent,
                message = _("Error") + "\n\n%s"% e,
                buttons = [("OK", self.return_home)]
                )
            logger.error('%s'%e)
        except OSError as e:
            dia = dialogs.Error(
                parent = self.parent,
                message = _("An error has occured!") + "\n\n%s"% e,
                buttons = [("OK", self.return_home)]
                )
            logger.error('%s'%e)
            return self.parent.pop_module_dialog(dia)

        dia = dialogs.Info(
            parent = self.parent,
            message = _("The settings you selected have been reverted to system defaults."),
            buttons = [("OK", self.return_home)]
            )
        logger.debug('Settings have been reverted to system defaults.')
        return self.parent.pop_module_dialog(dia)
    
    def return_home(self, widget):
        return self.parent.return_home(widget)


def __vasm_test__():
    """ Set of tests to anounce this module to vasm """
    return True


def __run__(parent_toplevel):
    mod = SettingsManage(parent_toplevel)
    parent_toplevel.pop_module(mod)