Commit from One Laptop Per Child: Translation System by user HoboPrimate. 31 of 31...
[journal-activity.git] / misc.py
blob77fc21877d7ec270600264c2ae953205c4b431b2
1 # Copyright (C) 2007, One Laptop Per Child
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 import logging
18 import time
19 import traceback
20 import sys
21 import os
22 from gettext import gettext as _
24 import gtk
26 from sugar import activity
27 from sugar import mime
28 from sugar.bundle.activitybundle import ActivityBundle
29 from sugar.bundle.contentbundle import ContentBundle
30 from sugar.bundle.bundle import MalformedBundleException, \
31 ZipExtractException, RegistrationException
32 from sugar import util
34 from journalentrybundle import JournalEntryBundle
36 def _get_icon_file_name(icon_name):
37 icon_theme = gtk.icon_theme_get_default()
38 info = icon_theme.lookup_icon(icon_name, gtk.ICON_SIZE_LARGE_TOOLBAR, 0)
39 if not info:
40 # display standard icon when icon for mime type is not found
41 info = icon_theme.lookup_icon('application-octet-stream',
42 gtk.ICON_SIZE_LARGE_TOOLBAR, 0)
43 fname = info.get_filename()
44 del info
45 return fname
47 _icon_cache = util.LRU(50)
49 def get_icon_name(jobject):
51 cache_key = (jobject.object_id, jobject.metadata.get('timestamp', None))
52 if cache_key in _icon_cache:
53 return _icon_cache[cache_key]
55 file_name = None
57 if jobject.is_activity_bundle() and jobject.file_path:
58 try:
59 bundle = ActivityBundle(jobject.file_path)
60 file_name = bundle.get_icon()
61 except:
62 logging.warning('Could not read bundle:\n' + \
63 ''.join(traceback.format_exception(*sys.exc_info())))
64 file_name = _get_icon_file_name('application-octet-stream')
66 if not file_name and jobject.metadata['activity']:
67 service_name = jobject.metadata['activity']
68 activity_info = activity.get_registry().get_activity(service_name)
69 if activity_info:
70 file_name = activity_info.icon
72 mime_type = jobject.metadata['mime_type']
73 if not file_name and mime_type:
74 icon_name = mime.get_mime_icon(mime_type)
75 if icon_name:
76 file_name = _get_icon_file_name(icon_name)
78 if not file_name or not os.path.exists(file_name):
79 file_name = _get_icon_file_name('application-octet-stream')
81 _icon_cache[cache_key] = file_name
83 return file_name
85 def get_date(jobject):
86 """ Convert from a string in iso format to a more human-like format. """
87 if jobject.metadata.has_key('timestamp'):
88 return util.timestamp_to_elapsed_string(jobject.metadata['timestamp'])
89 elif jobject.metadata.has_key('mtime'):
90 ti = time.strptime(jobject.metadata['mtime'], "%Y-%m-%dT%H:%M:%S")
91 return util.timestamp_to_elapsed_string(time.mktime(ti))
92 else:
93 return _('No date')
95 def get_bundle(jobject):
96 try:
97 if jobject.is_activity_bundle() and jobject.file_path:
98 return ActivityBundle(jobject.file_path)
99 elif jobject.is_content_bundle() and jobject.file_path:
100 return ContentBundle(jobject.file_path)
101 elif jobject.metadata['mime_type'] == JournalEntryBundle.MIME_TYPE \
102 and jobject.file_path:
103 return JournalEntryBundle(jobject.file_path)
104 else:
105 return None
106 except MalformedBundleException, e:
107 logging.warning('Incorrect bundle: %r' % e)
108 return None