summaryrefslogtreecommitdiffstats
path: root/pyanaconda/ui/tui/spokes/time.py
blob: 2cea1b8cf745adcf118f5e81f9304289a427c04b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# Timezone text spoke
#
# Copyright (C) 2012  Red Hat, Inc.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions of
# the GNU General Public License v.2, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY expressed or implied, including the implied warranties 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 this program; if not, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
# source code or documentation are not subject to the GNU General Public
# License and may only be used or replicated with the express permission of
# Red Hat, Inc.
#
# Red Hat Author(s): Martin Sivak <msivak@redhat.com>
#

from pyanaconda.ui.tui.spokes import NormalTUISpoke
from pyanaconda.ui.tui.simpleline import TextWidget, ColumnWidget
from pyanaconda import timezone

import gettext
_ = lambda x: gettext.ldgettext("anaconda", x)

class TimeZoneSpoke(NormalTUISpoke):
    title = _("Timezone settings")
    category = "localization"

    def __init__(self, app, data, storage, payload, instclass):
        NormalTUISpoke.__init__(self, app, data, storage, payload, instclass)

    def initialize(self):
        self._timezones = dict([(k, sorted(v)) for k,v in timezone.get_all_regions_and_timezones().iteritems()])
        self._regions = [r for r in self._timezones]
        self._lower_regions = [r.lower() for r in self._timezones]

        self._zones = ["%s/%s" % (region, z) for region in self._timezones for z in self._timezones[region]]
        self._lower_zones = [z.lower() for region in self._timezones for z in self._timezones[region]] # for lowercase lookup

        self._selection = ""

    @property
    def completed(self):
        return bool(self.data.timezone.timezone or self._selection)

    @property
    def status(self):
        if self.data.timezone.timezone:
            return _("%s timezone") % self.data.timezone.timezone
        elif self._selection:
            return _("%s timezone") % self._selection
        else:
            return _("Timezone is not set.")


    def refresh(self, args = None):
        """args is None if we want a list of zones or "zone" to show all timezones in that zone."""
        NormalTUISpoke.refresh(self, args)

        if args and args in self._timezones:
            self._window += [TextWidget(_("Available timezones in region %s") % args)]
            displayed = [TextWidget(z) for z in self._timezones[args]]
        else:
            self._window += [TextWidget(_("Available regions"))]
            displayed = [TextWidget(z) for z in self._regions]

        def _prep(i, w):
            number = TextWidget("%2d)" % i)
            return ColumnWidget([(4, [number]), (None, [w])], 1)

        # split zones to three columns
        middle = len(displayed) / 3
        left = [_prep(i, w) for i,w in enumerate(displayed) if i <= middle]
        center = [_prep(i, w) for i,w in enumerate(displayed) if i > middle and i <= 2*middle]
        right = [_prep(i, w) for i,w in enumerate(displayed) if i > 2*middle]

        c = ColumnWidget([(24, left), (24, center), (24, right)], 3)
        self._window.append(c)

        return True

    def input(self, args, key):
        try:
            keyid = int(key)
            if args:
                self._selection = "%s/%s" % (args, self._timezones[args][keyid])
                self.apply()
                self.close()
            else:
                if len(self._timezones[self._regions[keyid]]) == 1:
                    self._selection = "%s/%s" % (self._regions[keyid],
                                                 self._timezones[self._regions[keyid]][0])
                    self.apply()
                    self.close()
                else:
                    self.app.switch_screen(self, self._regions[keyid])
            return True
        except (ValueError, IndexError):
            pass

        if key.lower() in self._lower_zones:
            id = self._lower_zones.index(key.lower())
            self._selection = self._zones[id]
            self.apply()
            self.close()
            return True

        elif key.lower() in self._lower_regions:
            id = self._lower_regions.index(key.lower())
            if len(self._timezones[self._regions[id]]) == 1:
                self._selection = "%s/%s" % (self._regions[id],
                                             self._timezones[self._regions[id]][0])
                self.apply()
                self.close()
            else:
                self.app.switch_screen(self, self._regions[id])
            return True

        elif key.lower() == "b":
            self.app.switch_screen(self, None)
            return True

        else:
            return key

    def prompt(self, args):
        return _("Please select the timezone.\nUse numbers or type names directly [b to region list, q to quit]: ")

    def apply(self):
        self.data.timezone.timezone = self._selection