Source

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

Full commit
#!/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_loginmanager.py
    Text mode module to set the system login manager"""

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

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

logger = logging.getLogger('vasm')

VASM_CATEGORY = "Graphical Environment"
VASM_LABEL = "System Login Manager"


class Manager(widgets.TuiModule):
    """ Text interface to set the login manager. """
    def __init__(self, parent):
        self.parent = parent
        _managers = []
        xdmgrp = []
        self.datamodel = loginmanagers.LoginManagers()
        self.selected_loginmanager = self.datamodel.get_current().lower()
        # get the list of existing login managers.
        existing = self.datamodel.list_existing()
        for manager in existing:
            rb = urwid.RadioButton(
                group = xdmgrp,
                label = manager.upper(),
                user_data = manager.lower(),
                on_state_change = self.loginmanager_toggle,
                state = manager.lower() == self.selected_loginmanager.lower())
            _managers.append(urwid.AttrWrap(rb, 'button'))
            _managers.append(urwid.Text(" " * 4 + self.datamodel._describe_manager(manager.lower())))
        _body = [widgets.blank, urwid.Pile(_managers), widgets.blank]
        _desc = _("Selected your preferred login manager.  The login manager provides") + " " + \
                _("user authentication to the system and launches your preferred desktop environment.")
        widgets.TuiModule.__init__(
            self,
            parent = self.parent,
            header = _("Login Manager Settings"),
            desc = [widgets.blank, urwid.Text(_desc)],
            body = _body,
            buttons = [("Save", self.save_login_manager),
                ("Back", self.close_module)]
            
        )
        self.body = urwid.LineBox(self.get_body())
        self.set_focus('body')
    
    
    def save_login_manager(self, widget=None):
        """ Save the changes to the system.  The selected value is stored in
        self.selected_loginmanager"""
        if self.datamodel.set_manager(self.selected_loginmanager):
            # if that method returns True, then we're OK.
            dia = dialogs.Info(
                parent = self.parent,
                buttons = [("OK", self.close_module)],
                message = _("Loginmanager settings have been saved.")
            )
            logger.info("Login manager settings saved.")
        else:
            # There was an error
            dia = dialogs.Error(
                parent = self.parent,
                message = _("Unable to save loginmanager settings.") + "\n" +
                _("Make sure you are doing this as root and that /etc/rc.d/rc.X is fine"),
                buttons = [("OK", self.return_to_module)]
            )
            logger.error("Unable to save login manager settings.  Check /etc/rc.d/rc.X")
        return self.parent.pop_module_dialog(dia)
    
    def loginmanager_toggle(self, radio, newvalue, user_data):
        """ This gets triggered every time a radio button is selected.  user_data contains the
        name of the loginmanager that was selected. """
        if newvalue is True:
            self.selected_loginmanager = user_data.lower()

def __vasm_test__():
    return os.geteuid() == 0

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