Source

vasm / src / vasm / ui / gtk2 / modules / 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/>.

import gtk
import sys
import os
from vasm.ui.gtk2.support import widgets, dialogs
from vasm.backend import skel, useradmin
from vasm.backend.utils import _
import logging

logger = logging.getLogger('vasm')

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

# vasm flags
VASM_CATEGORY = _("Personal")
VASM_ICON=gtk.STOCK_PREFERENCES
VASM_LABEL=_("Reset Personalization")

def __vasm_test__():
    """ run any tests required for this module to be shown in the mian vasm UI
    
    Must return True or False"""
    return os.path.isdir('/etc/skel')

class gtkSkel(widgets.VasmModule):
    def __init__(self, parent):
        self._parent = parent
        self.title = VASM_LABEL
        widgets.VasmModule.__init__(self, parent=parent, module_title=self.title,
                                    module_desc = _("Reset personal data"))
        tx = (
            _("Resetting personal settings can fix application errors when applications have been misconfigured."),
            " ",
            _("These are minor changes and have no system-wide effect."), " ",
            _("Select which settings you would like to reset back to factory defaults."),
            "\n<b>%s</b>"% _("WARNING: This cannot be undone.")
        )
        # The skel model
        wusername = None
        # get the username
        for acct in useradmin.UserModel().listUsers():
            if acct.uid == os.geteuid():
                wusername = acct.login
        if not wusername:
            return dialogs.error(
                _("Unable to find your user name.  This is bad!"),
                parent = self._parent
                )
        self.skelmodel = skel.UserSkel(wusername)
        self.set_description(' '.join(tx))
        self.scroll = gtk.ScrolledWindow()
        self.scroll.set_policy(
            gtk.POLICY_AUTOMATIC,
            gtk.POLICY_AUTOMATIC
        )
        self.datamodel = gtk.ListStore(bool, bool, str, str)
        settings = self.skelmodel.list_resettable_settings()
        skeldir = self.skelmodel.skeldir

        for item in settings:
            desc = self.skelmodel._get_skel_description(item)
            self.datamodel.append([False, True, item, desc])
        
        self.treeview = gtk.TreeView(model = self.datamodel)
        self.treeview.set_headers_visible(False)
        
        # renderers
        crt = gtk.CellRendererToggle()
        crt.connect('toggled', self.toggled_callback, self.datamodel)
        crfname = gtk.CellRendererText()
        crfdesc = gtk.CellRendererText()
        
        #columns
        col0 = gtk.TreeViewColumn('value', crt, active=0, activatable=1)
        col1 = gtk.TreeViewColumn(_("File"), crfname, text=2)
        col2 = gtk.TreeViewColumn(_("Description"), crfdesc, text=3)
        
        for column in col0, col1, col2:
            self.treeview.append_column(column)
        
        # add the treeview to the scroll
        self.scroll.add(self.treeview)
        #self.pack_start(self.treeview, True, True, 0)
        hb = gtk.HBox()
        hb.pack_start(self.scroll, True, True, 4)
        self.pack_start(hb, True, True, 2)
    
        # Add the buttons
        btclose = gtk.Button(stock=gtk.STOCK_CANCEL)
        btclose.connect('clicked', self.close_module)
        self.add_button(btclose)
        self.btapply = gtk.Button(stock=gtk.STOCK_APPLY)
        self.btapply.connect('clicked', self.do_apply_changes)
        self.add_button(self.btapply)
        self.btapply.set_property('sensitive', len(self._get_reset_list()) > 0)
        
    def toggled_callback(self, cell, path, model):
        iten = model.get_iter(path)
        model.set_value(iten, 0, not cell.get_active())
        self.btapply.set_property('sensitive', len(self._get_reset_list()) > 0)
    
    def _get_reset_list(self):
        """ Return a list of settings to be wiped """
        model = self.treeview.get_model()
        ret = []
        for item in model:
            if item[0] is True:
               ret.append(item[2])
        
        return ret

    def do_apply_changes(self, widget=None):
        """ Make the call to the backend to apply the changes. """
        reset = self._get_reset_list()
        try:
            self.skelmodel.reset_settings(reset)
            logger.debug("Personal settings for %s have been wiped"% self.skelmodel.login)
        except AssertionError as e:
            logger.error("Error resetting personal settigs. %s"% e)
            return dialogs.error(
                _("An error has occurred!") + "  \n\n%s"% e,
                parent = self._parent
                )
        except OSError as e:
            logger.error("Error resetting personal settigs. %s"% e)
            return dialogs.error(
                _("An error has occured while restoring original configuration") + "\n\n%s"% e,
                parent = self._parent)
        
        return dialogs.info(
            _("The settings you selected have been reset back to system defaults."),
            parent = self._parent
            )

def __run__(parent=None):
    skeller = gtkSkel(parent=parent)
    skeller.launch()