diff options
author | Stefano Karapetsas <[email protected]> | 2012-06-12 17:49:56 +0200 |
---|---|---|
committer | Stefano Karapetsas <[email protected]> | 2012-06-12 17:49:56 +0200 |
commit | 4bbd16182dab69da9ca7ad13309962af40529469 (patch) | |
tree | c77202017ea3739f137a86f817bdb2ecef381f80 /timer-applet/src/timerapplet/utils.py | |
parent | c9b9454068f090a7132c6bea6ac07c91843ffb6e (diff) | |
download | mate-applets-4bbd16182dab69da9ca7ad13309962af40529469.tar.bz2 mate-applets-4bbd16182dab69da9ca7ad13309962af40529469.tar.xz |
add timer-applet
Diffstat (limited to 'timer-applet/src/timerapplet/utils.py')
-rw-r--r-- | timer-applet/src/timerapplet/utils.py | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/timer-applet/src/timerapplet/utils.py b/timer-applet/src/timerapplet/utils.py new file mode 100644 index 00000000..53bfdc21 --- /dev/null +++ b/timer-applet/src/timerapplet/utils.py @@ -0,0 +1,79 @@ +# Copyright (C) 2008 Jimmy Do <[email protected]> +# Copyright (C) 2010 Kenny Meyer <[email protected]> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program 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 this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +from gettext import gettext as _ + +def is_valid_preset_name(name_str, preset_store, allowed_names=()): + if len(name_str) == 0: + return False + + name_str = name_str.lower() + for allowed in allowed_names: + if name_str == allowed.lower(): + return True + + return not preset_store.preset_name_exists_case_insensitive(name_str) + +def seconds_to_hms(total_seconds): + (hours, remaining_seconds) = divmod(total_seconds, 3600) + (minutes, seconds) = divmod(remaining_seconds, 60) + return (hours, minutes, seconds) + +def hms_to_seconds(hours, minutes, seconds): + return hours * 3600 + minutes * 60 + seconds + +def construct_time_str(remaining_seconds, show_all=True): + """Return a user-friendly representation of remaining time based on the given number of seconds. + + show_all specifies whether the returned string should show all time components. + If show_all is True (default), the returned string is in HH:MM:SS format. + If show_all is False, the returned string is in either HH:MM or MM:SS format, + depending on how much time is remaining. This avoids showing the user more + information than necessary. + + """ + hours, minutes, seconds = seconds_to_hms(remaining_seconds) + if show_all: + # HH:MM:SS + return _('%02d:%02d:%02d') % (hours, minutes, seconds) + else: + if hours > 0 or minutes > 14: + # HH:MM + return _('%02d:%02d') % (hours, minutes) + else: + # MM:SS + return _('%02d:%02d') % (minutes, seconds) + +def get_display_text_from_datetime(date_time): + return date_time.strftime('%X') + +def get_preset_display_text(presets_store, row_iter): + (name, hours, minutes, seconds, command, next_timer, auto_start) = \ + presets_store.get_preset(row_iter) + + # <preset name> (HH:MM:SS) + return _('%s (%02d:%02d:%02d)') % (name, hours, minutes, seconds) + +def serialize_bool(boolean): + if boolean: + return "1" + return "0" + +def deserialize_bool(string): + if string == "1": + return True + return False |