Source

vasm / src / vasm / backend / gdmhelper.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/>.


import gdmconfigparser
import os
import logging

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

log = logging.getLogger('vasm')

class SettingsModel(object):
    """ Model to work with the gdm settings """
    def __init__(self, tmpfile = '/tmp/gdmsettings', settingspath = '/etc/gdm/custom.conf'):
        self.settingspath = settingspath
        self._tmpfile = tmpfile
        self.config = gdmconfigparser.ConfigHandler()
        self.config.read(self.settingspath)

    def get_current_autologin_setting(self):
        """ Returns 'true' or 'false' """
        if self.config.has_option('daemon', 'AutomaticLoginEnable'):
            return self.config.get('daemon', 'AutomaticLoginEnable')
        return 'false'
    
    def get_current_autologin_user(self):
        """ return the value of the current autologin user.
        
        Returns None if nothing is available."""
        if self.config.has_option('daemon', 'AutomaticLogin'):
            return self.config.get('daemon', 'AutomaticLogin')
        return None
    
    def _save_to_file(self):
        """ Write the changes to the file """
        # start by saving the options on the tempfile first. This is because
        # Our helper module ignores all comments on the file, and we want to keep those
        # in there. They're really helpful.
        f = open(self.settingspath, 'r')
        data = f.readlines()
        f.close()
        for key in ('AutomaticLoginEnable', 'AutomaticLogin'):
            i = 0
            for line in data:
                if self._line_sets_setting(line, key):
                    value = self.config.get('daemon', key)
                    nline = "%s=%s\n"% (key, value)
                    data[i] = nline
                i += 1
        f = open(self.settingspath, 'w')
        f.writelines(''.join(data))
        f.close()
        return

    def _line_sets_setting(self, line, setting):
        """ Returns True if line sets setting"""
        if "=" not in line:
            return False
        key, value = line.split('=')
        return key.lower().strip() == setting.lower().strip()
    
    def save_settings(self, alsetting='false', aluser=''):
        """ save the settings to the configuration (not yet written to file) """
        self.config.set('daemon', 'AutomaticLoginEnable', alsetting)
        self.config.set('daemon', 'AutomaticLogin', aluser)
        f = open(self._tmpfile, 'w')
        f.writelines([alsetting,'\n', aluser])
        f.close()
        return self._save_to_file()