timezones.py (5834B)
1 # coding: utf-8 2 3 import subprocess 4 import gi 5 gi.require_version('TimezoneMap', '1.0') 6 from gi.repository import Gtk, Gdk, GLib, TimezoneMap 7 from collections import defaultdict, namedtuple 8 from datetime import datetime 9 from zoneinfo import ZoneInfo, ZoneInfoNotFoundError 10 11 TZDATA_FILE = '/usr/share/zoneinfo/tzdata.zi' 12 # The map stretches to whatever it's allocated, this keeps its ratio at the window's default width 13 MAP_HEIGHT = 385 14 15 timezones = [] 16 region_menus = {} 17 zone_links = {} 18 19 Timezone = namedtuple('Timezone', 'name ccode'.split()) 20 21 def build_timezones(_installer): 22 global installer, time_label, date_label, tz_map 23 installer = _installer 24 25 cssProvider = Gtk.CssProvider() 26 cssProvider.load_from_path('/usr/share/live-installer/style.css') 27 screen = Gdk.Screen.get_default() 28 styleContext = Gtk.StyleContext() 29 styleContext.add_provider_for_screen(screen, cssProvider, Gtk.STYLE_PROVIDER_PRIORITY_USER) 30 31 time_label = installer.builder.get_object("label_time") 32 date_label = installer.builder.get_object("label_date") 33 34 GLib.timeout_add_seconds(1, update_local_time_label) 35 36 tz_map = TimezoneMap.TimezoneMap.new() 37 tz_map.set_size_request(-1, MAP_HEIGHT) 38 tz_map.connect('location-changed', map_location_changed) 39 installer.builder.get_object("aspectframe_timezones").add(tz_map) 40 tz_map.show() 41 42 # The map's city data can use legacy zone names (Europe/Kiev...), tzdata.zi lists them as links 43 try: 44 with open(TZDATA_FILE) as f: 45 for line in f: 46 parts = line.split() 47 if len(parts) == 3 and parts[0] == 'L': 48 zone_links[parts[2]] = parts[1] 49 except OSError as e: 50 print("Could not read %s: %s" % (TZDATA_FILE, e)) 51 52 # Populate timezones model 53 def autovivified(): 54 return defaultdict(autovivified) 55 hierarchy = autovivified() 56 57 for line in subprocess.getoutput("awk '/^[^#]/{ print $1,$3 }' /usr/share/zoneinfo/zone.tab | sort -k2").split('\n'): 58 ccode, name = line.split() 59 tup = Timezone(name, ccode) 60 submenu = hierarchy 61 parts = name.split('/') 62 for i, part in enumerate(parts, 1): 63 if i != len(parts): submenu = submenu[part] 64 else: submenu[part] = tup 65 timezones.append(tup) 66 67 def _build_tz_menu(d): 68 menu = Gtk.Menu() 69 for k in sorted(d): 70 v = d[k] 71 item = Gtk.MenuItem(k.replace("_", " ")) 72 item.show() 73 if isinstance(v, dict): 74 item.set_submenu(_build_tz_menu(v)) 75 else: 76 item.connect('activate', tz_menu_selected, v) 77 menu.append(item) 78 menu.show() 79 return menu 80 81 def _build_cont_menu(d): 82 menu = Gtk.Menu() 83 for k in sorted(d): 84 v = d[k] 85 item = Gtk.MenuItem(k.replace("_", " ")) 86 item.show() 87 if isinstance(v, dict): 88 region_menus[k] = _build_tz_menu(v) 89 region_menus[k].show_all() 90 91 item.connect('activate', cont_menu_selected, k) 92 menu.append(item) 93 menu.show() 94 return menu 95 96 cont_menu = _build_cont_menu(hierarchy) 97 cont_menu.show_all() 98 99 installer.builder.get_object('cont_button').connect('event', button_callback) 100 installer.builder.get_object('cont_button').menu = cont_menu 101 installer.builder.get_object('tz_button').connect('event', button_callback) 102 103 current_zone = None 104 current_country = None 105 syncing_map = False 106 map_updated = False 107 108 def button_callback(button, event): 109 menu = button.menu 110 111 if event.type == Gdk.EventType.BUTTON_PRESS: 112 menu.popup(None, None, None, None, 0, event.time) 113 return True 114 return False 115 116 def update_local_time_label(): 117 if current_zone is None: 118 return True 119 try: 120 now = datetime.now(ZoneInfo(current_zone)) 121 except ZoneInfoNotFoundError as e: 122 print("Could not load timezone %s: %s" % (current_zone, e)) 123 return True 124 time_label.set_markup(now.strftime('<span size="x-large" weight="bold">%H:%M</span> (%-I:%M %p)')) 125 date_format = '%A, %B %-d, %Y' if current_country == 'US' else '%A, %-d %B %Y' 126 date_label.set_label(now.strftime(date_format)) 127 return True 128 129 def cont_menu_selected(widget, cont): 130 installer.builder.get_object("cont_button").set_label(cont) 131 installer.builder.get_object("tz_button").set_label(_('Select timezone')) 132 installer.builder.get_object("tz_button").menu = region_menus[cont] 133 134 def tz_menu_selected(widget, tz): 135 select_timezone(tz) 136 137 def map_location_changed(widget, location): 138 global map_updated 139 map_updated = True 140 if syncing_map or location is None: 141 return 142 name = zone_links.get(location.props.zone, location.props.zone) 143 tz = next((tz for tz in timezones if tz.name == name), None) 144 if tz is None: 145 tz = Timezone(name, location.props.country) 146 select_timezone(tz, from_map=True) 147 148 def show_on_map(name): 149 global syncing_map, map_updated 150 syncing_map = True 151 map_updated = False 152 tz_map.set_timezone(name) 153 if not map_updated: 154 # The map has no city for this zone, don't leave a stale pin on it 155 tz_map.clear_location() 156 syncing_map = False 157 158 def select_timezone(tz, from_map=False): 159 global current_zone, current_country 160 current_zone = tz.name 161 current_country = tz.ccode 162 163 installer.setup.timezone = tz.name 164 cont, separator, tz_str = tz.name.partition("/") 165 166 installer.builder.get_object("cont_button").set_label(cont) 167 installer.builder.get_object("tz_button").set_label(tz_str.replace("_", " ")) 168 if cont in region_menus: 169 installer.builder.get_object("tz_button").menu = region_menus[cont] 170 171 update_local_time_label() 172 173 if not from_map: 174 show_on_map(tz.name)