Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/misc.py
blob: c9fc1a7793b7e9ddf11b070d47afb40148e49b6e (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
135
136
137
138
139
# Copyright (C) 2007, One Laptop Per Child
#
# 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

from gettext import gettext as _
import logging
import time

import gtk

from sugar import activity
from sugar import mime
from sugar.bundle.activitybundle import ActivityBundle
from sugar.bundle.contentbundle import ContentBundle
from sugar.bundle.bundle import MalformedBundleException, \
     ZipExtractException, RegistrationException

def _get_icon_file_name(icon_name):
    icon_theme = gtk.icon_theme_get_default()
    info = icon_theme.lookup_icon(icon_name, gtk.ICON_SIZE_LARGE_TOOLBAR, 0)
    if not info:
        # display standard icon when icon for mime type is not found
        info = icon_theme.lookup_icon('application-octet-stream',
                                      gtk.ICON_SIZE_LARGE_TOOLBAR, 0)
    fname = info.get_filename()
    del info
    return fname

def get_icon_name(jobject):

    file_name = None

    if jobject.is_activity_bundle():
        try:
            bundle = ActivityBundle(jobject.file_path)
            file_name = bundle.get_icon()
        except IOError, e:
            logging.warning('Could not read bundle: %r' % e)
            return _get_icon_file_name('application-octet-stream')
        except MalformedBundleException, e:
            logging.warning('Incorrect bundle: %r' % e)
            return _get_icon_file_name('application-octet-stream')

    if jobject.metadata['activity']:
        service_name = jobject.metadata['activity']
        activity_info = activity.get_registry().get_activity(service_name)
        if activity_info:
            file_name = activity_info.icon

    mime_type = jobject.metadata['mime_type']
    if not file_name and mime_type:
        icon_name = mime.get_mime_icon(mime_type)
        if icon_name:
            file_name = _get_icon_file_name(icon_name)

    if not file_name:
        return _get_icon_file_name('application-octet-stream')

    return file_name

# TRANS: Relative dates (eg. 1 month and 5 days).
units = [[_('%d year'),   _('%d years'),   356 * 24 * 60 * 60],
            [_('%d month'),  _('%d months'),  30 * 24 * 60 * 60],
            [_('%d week'),   _('%d weeks'),   7 * 24 * 60 * 60],
            [_('%d day'),    _('%d days'),    24 * 60 * 60],
            [_('%d hour'),   _('%d hours'),   60 * 60],
            [_('%d minute'), _('%d minutes'), 60],
            [_('%d second'), _('%d seconds'), 1]]

AND = _(' and ')
COMMA = _(', ')
RIGHT_NOW = _('Right now')

def _get_elapsed_string(timestamp, max_levels=2):
    levels = 0
    result = ''
    elapsed_seconds = int(time.time() - timestamp)
    for name_singular, name_plural, factor in units:
        elapsed_units = elapsed_seconds / factor
        if elapsed_units > 0:

            if levels > 0:
                if max_levels - levels == 1:
                    result += AND
                else:
                    result += COMMA
                
            if elapsed_units == 1:
                result += name_singular % elapsed_units
            else:
                result += name_plural % elapsed_units
            elapsed_seconds -= elapsed_units * factor
            levels += 1
            
            if levels == max_levels:
                break

    if levels == 0:
        return RIGHT_NOW

    return result

def get_date(jobject):
    """ Convert from a string in iso format to a more human-like format. """
    if jobject.metadata.has_key('timestamp'):
        return _get_elapsed_string(jobject.metadata['timestamp'])
    elif jobject.metadata.has_key('mtime'):
        ti = time.strptime(jobject.metadata['mtime'], "%Y-%m-%dT%H:%M:%S")
        return _get_elapsed_string(time.mktime(ti))
    else:
        return _('No date')

def get_bundle(jobject):
    if not jobject.is_bundle():
        return None
    if jobject.file_path == '':
        # Probably a download-in-progress
        return None

    try:
        if jobject.is_activity_bundle():
            return ActivityBundle(jobject.file_path)
        else:
            return ContentBundle(jobject.file_path)
    except MalformedBundleException, e:
        logging.warning('Incorrect bundle: %r' % e)
        return None