Source

vasm / src / vasm / ui / tui / modules / tui_bootmode.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_bootmode.py
    Text mode module to set the system boot mode """

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

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

logger = logging.getLogger('vasm')

VASM_CATEGORY = "System"
VASM_LABEL = "System Startup Mode"

class Interface(dialogs.ModuleDialog):
    """ Dialog to set the default system boot mode. """
    def __init__(self, parent):
        self.parent = parent
        self.datamodel = bootmode.InitTab()
        modes = {2: _("Command line single user mode."),
                 3: _("Commandline with services enabled for a multi user environment."),
                 4: _("Graphical desktop.  Recomended for most home users."),
                 5: _("Graphical desktop with services enaled for multiple users.")}
        msg = _("Select your default system startup mode.")
        _body = []#widgets.blank]
        _radios = []
        radios = []
        current_boot_mode = self.datamodel.getMode()
        self.selected_mode = current_boot_mode
        mode2= urwid.AttrWrap(
            urwid.RadioButton(
                radios,
                state = current_boot_mode == 2,
                label="[ %s %s ]"% (_("Level"), "2") + " "*2 + \
                _("Text user interface desktop."),
                user_data = 2,
                on_state_change = self.bootmode_toggle), 'button')       
        mode3 = urwid.AttrWrap(
            urwid.RadioButton(
                radios,
                label = "[ %s %s ]"% (_("Level"), "3") + " "*2 + \
                _("Text user interface server."),
                user_data = 3,
                state = current_boot_mode == 3,
                on_state_change = self.bootmode_toggle), 'button')
        mode4 = urwid.AttrWrap(
            urwid.RadioButton(
                radios,
                label="[ %s %s ]"% (_("Level"), "4") + " "*2 + \
                _("Graphical single user desktop."),
                user_data = 4,
                state = current_boot_mode == 4,
                on_state_change = self.bootmode_toggle), 'button')
        mode5 = urwid.AttrWrap(
            urwid.RadioButton(
                radios,
                label = "[ %s %s ]"% (_("Level"), "5") + " "*2 + \
                _("Graphical multi user server."),
                user_data = 5,
                state = current_boot_mode == 5,
                on_state_change = self.bootmode_toggle), 'button')

        for mode, modeno in ((mode2, 2), (mode3, 3),
            (mode4, 4), (mode5, 5)):
            _radios.append(mode)
            _radios.append(urwid.Text(" "*4 + modes[modeno]))
        
        _body.append(urwid.Pile(_radios))

        dialogs.ModuleDialog.__init__(
            self,
            header = _("System Startup Settings"),
            desc = ["\n",msg],
            parent = self.parent,
            body = _body,
            buttons = [("Save", self.save_boot_mode),
                ("Cancel", self.parent.return_home)])
        
        self.body = urwid.LineBox(self.get_body())
        # set the focus to the body
        self.set_focus('body')
    
    def save_boot_mode(self, widget):
        """ Save the changes in boot mode """
        try:
            self.datamodel.setMode(self.selected_mode)
            logger.info("Boot mode was set to %s"% self.selected_mode)
        except:
            logger.error("Unable to save boot mode settings.")
            dia = dialogs.Error(
            parent = self.parent,
            message = _("Unable to set boot mode.  No changes were applied.") + "\n" + \
            _("Make sure you are logged in as root and that /etc/inittab is ok."),
            buttons = [("OK", self.parent.return_to_module_stack)]
            )
            return self.parent.pop_module_dialog(dia)
        nmode = str(self.datamodel.getMode())
        dia = dialogs.Info(
            parent = self.parent,
            message = _("Boot mode has been been set to" + " "+ nmode),
            buttons = [("OK", self.parent.return_to_module_stack)]
            )
        return self.parent.pop_module_dialog(dia)

    def bootmode_toggle(self, radio, newstate, user_data):
        """ user_data provides an integer (2-5) which is the bootmode for the toggled radio"""
        if newstate is True:
            self.selected_mode = user_data


def __vasm_test__():
    """ Set of tests to determine who can interact with this module
    """
    return os.geteuid() == 0

def __run__(parent):
    mod = Interface(parent)
    parent.pop_module(mod)