1. Moises Henriquez
  2. vasm


vasm / src / vasm / ui / tui / modules / tui_dateset.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
#    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_dateset.py
    Text mode module set the system date and time."""

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

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

logger = logging.getLogger('vasm')

VASM_LABEL = "Date and Time"

class DateSet(dialogs.ModuleDialog):
    def __init__(self, parent):
        self.parent = parent
        # create the models to work with the dateset module
        self.dtmodel = dateset.DateTime()
        self.tzmodel = dateset.TimeZone()
        self.hwcmodel = dateset.HWClock()
        msg = _("System date and time.")
        desc = _("Set the system date and time using the values below.") + "\n" + \
        _("Press +/- to navigate months, space or enter to select current date")
        now = datetime.datetime.now()
        self.calendar = widgets.Calendar()
        cal = urwid.LineBox(self.calendar)
        self.clock = widgets.TimePicker()

        # Create a label that explains how the timezone and hwclock work
        tx = (
            _("Linux uses the hardware clock setting and timezone to properly set the system time."),
            " ",
            _("To get the proper results, select the value that matches your hardware (BIOS) clock setting."),
            " ",
            _("If you are unsure about this, 'Localtime' is the common value for most PC's."),'\n'
        warntext = urwid.Text('\n'+' '.join(tx))

        # the timezone picker
        self.tzselector = widgets.SensibleLabel(
            markup = self.tzmodel.get_zone(),
            actuator = self._pick_timezone
        selector = urwid.AttrWrap(self.tzselector, 'button', 'button sel')
        tzarea = urwid.Columns([
            ('fixed', 20, urwid.Text("Select Timezone")) ,
        # the hwclock selector
        self.hwcselector = widgets.HChoiceBox(
            "Hardware clock setting:",
            [("UTC","UTC"), ("Localtime", "Localtime")]
        lb = urwid.ListBox([warntext, self.tzselector, self.hwcselector])
        piled_bottom = [
            ('flow', warntext),
        bottom = urwid.Pile(piled_bottom)

        top = urwid.Columns([cal, ('fixed', 2, widgets.blank), self.clock])

        _widgets = [top, urwid.Divider('_'), bottom]

            parent = self.parent,
            header = msg,
            desc = ["", desc, ""],
            buttons = [("OK", self.save_settings),
                ("Cancel", self.close_module)],
            body = _widgets

    def _pick_timezone(self, widget=None):
        """ Pop a dialog to select the timezone """
        dia = dialogs.VChoiceBox(
            title  = _("Select your timezone"),
            options = self.tzmodel.list_zones(),
            parent = self.parent,
            on_select = self._update_tz_selection)
        # Set the initial value to the current setting. 
        dia._selection = self.tzmodel.get_zone()
        return self.parent.pop_module_dialog(dia, 3)

    def _update_tz_selection(self, value):
        return self.tzselector.set_value(value)
    def _on_timezonepicker_enter(self, widget=None):

    def _write_date_settings(self):
        """ Write the date settings to the system based on what the user picked from the calendar widget."""
        _year, _month, _day = self.calendar.get_date()
        self.dtmodel.year = _year
        self.dtmodel.month = _month
        self.dtmodel.day = _day
        return self.dtmodel.set_date()

    def _write_time_settings(self):
        """ Write the time settings for based on the current values of the clock widget """
        _hr, _min, _sec = self.clock.get_time()
        self.dtmodel.hour = _hr
        self.dtmodel.minute = _min
        self.dtmodel.second = _sec
        return self.dtmodel.set_time()

    def _write_timezone_setting(self):
        tz = self.tzselector.get_value()
        return self.tzmodel.set_zone(tz)

    def _write_hwclock_setting(self):
        setting = self.hwcselector.get_value()
        return self.hwcmodel.set_value(setting)

    def save_settings(self, widget=None):
        """ Trigger the sequence to save the changes to the system."""
        # Save the date settings
            logger.debug('Saved system date.')
        except dateset.DateTimeError as e:
            dia = dialogs.Error(
                parent = self.parent,
                message = _("Failed to set the system date.") + "\n\n%s"% e,
                buttons = [("OK", self.return_to_module)]
            logger.error("Error while writing system date settings: %s"% e)
            return self.pop_dialog(dia)
        # Save the time settings
            logger.debug('Saved time settings.')
        except dateset.DateTimeError as e:
            dia = dialogs.Error(
                parent = self.parent,
                message = _("Failed to save the system time settings.") + "\n\n%s"% e,
                buttons = [("OK", self.return_to_module)]
            logger.error('Error while saving system time settings: %s'% e)
            return self.pop_dialog(dia)

        # Save the timezone settings
            logger.debug('Saved timezone settings')
        except IOError as e:
            return dialogs.Error(
                parent = self.parent,
                message = _("Failed to set timezone setting.") + "\n\n%s"% e,
                buttons = [("OK", self.return_to_module)]
            logger.error('Unable to save timezone settings %s'% e)
            return self.pop_dialog(dia)
        # Save the hwclock settings
            logger.debug('Saved hardware clock settings.')
        except IOError as e:
            dia = dialogs.Error(
                parent = self.parent,
                message = _("Faled to save hardware clock setting.") + "\n\n%s"% e,
                buttons = [("OK", self.return_to_module)]
            logger.error('Faied to save hardware clock settings: %s'%e)
            return self.pop_dialog(dia)
        except AssertionError as e:
            dia = dialogs.Error(
                parent = self.parent,
                buttons = [("OK", self.return_to_module)],
                message = _("Failed to save hardware clock setting.") + "\n\n%s"% e            
            logger.error('Faied to save hardware clock settings: %s'%e)
            return self.pop_dialog(dia)

        dia = dialogs.Info(
            parent = self.parent,
            message = ("System date and time settings have been updated."),
            buttons = [("OK", self.close_module)]
        logger.debug("Successfully saved Date, Time, Timezone, and Hardware clock settings.")
        return self.pop_dialog(dia)

def __vasm_test__():
    # only root can use this
    return os.geteuid() == 0

def __run__(parent):
    dia = DateSet(parent)
    return parent.pop_module(dia)