iIgnore folders which can not be properly converted to unicode (bug #602).
[wammu.git] / Wammu / MessageDisplay.py
blob01c5a2c1cf19cfcd6178153ff1131b36546ddafc
1 # -*- coding: UTF-8 -*-
2 # vim: expandtab sw=4 ts=4 sts=4:
3 '''
4 Wammu - Phone manager
5 Message to HTML conversion
6 '''
7 __author__ = 'Michal Čihař'
8 __email__ = 'michal@cihar.com'
9 __license__ = '''
10 Copyright © 2003 - 2008 Michal Čihař
12 This program is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License version 2 as published by
14 the Free Software Foundation.
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 more details.
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 '''
26 import Wammu
27 import Wammu.Data
28 import Wammu.Ringtone
29 import string
30 import re
31 import xml.sax.saxutils
32 from Wammu.Locales import UnicodeConv, HtmlStrConv, hgettext as _
34 def SmsTextFormat(cfg, txt, dohtml = True):
35 if cfg.Read('/Message/Format') == 'yes':
36 ret = ''
37 arr = txt.split(' ')
38 for a in arr:
39 if re.match('^([a-z]+[^ ]*)?[A-Z].*[a-z]{2,}[A-Z]{2,}.*$', a) != None:
40 prevtype = 'p'
41 if UnicodeConv(string.lowercase).find(a[0]) != -1:
42 curtype = 'l'
43 elif UnicodeConv(string.uppercase).find(a[0]) != -1:
44 curtype = 'u'
45 elif UnicodeConv(string.digits).find(a[0]) != -1:
46 curtype = 'd'
47 else:
48 curtype = 'p'
50 s = a[0]
52 for x in a[1:]:
53 if UnicodeConv(string.lowercase).find(x) != -1:
54 nexttype = 'l'
55 elif UnicodeConv(string.uppercase).find(x) != -1:
56 nexttype = 'u'
57 elif UnicodeConv(string.digits).find(x) != -1:
58 nexttype = 'd'
59 else:
60 nexttype = 'p'
62 if curtype == nexttype:
63 s += x
64 else:
65 if curtype == 'u' and nexttype == 'l' and prevtype == 'p' and len(s) == 1:
66 curtype = 'l'
67 prevtype = 'u'
68 s += x
69 continue
70 if curtype == 'p':
71 ret = ret.rstrip() + s + ' '
72 elif curtype == 'u':
73 ret += s.lower() + ' '
74 else:
75 ret += s + ' '
76 s = x
77 prevtype = curtype
78 curtype = nexttype
80 if curtype == 'p':
81 ret = ret.rstrip() + s + ' '
82 elif curtype == 'u':
83 ret += s.lower() + ' '
84 else:
85 ret += s + ' '
86 s = x
87 else:
88 ret += a + ' '
89 else:
90 ret = txt
91 if dohtml:
92 xmlsafe = xml.sax.saxutils.escape(ret)
93 return xmlsafe.replace('\n', '<br>')
94 else:
95 return ret.replace('\n', ' ')
97 def SmsToHtml(cfg, v):
98 if v.has_key('SMSInfo'):
99 text = ''
100 ringno = 0
101 Wammu.Ringtone.ringtones = {}
102 for i in v['SMSInfo']['Entries']:
103 if i['ID'] in Wammu.Data.SMSIDs['PredefinedAnimation']:
104 if i['Number'] > len(Wammu.Data.PredefinedAnimations):
105 text = text + \
106 '<wxp module="Wammu.Image" class="Bitmap">' + \
107 '<param name="tooltip" value="' + (_('Predefined animation number %d') % i['Number']) + '">' + \
108 '<param name="image" value="' + "['" + string.join(Wammu.Data.UnknownPredefined, "', '") + "']" + '">' + \
109 '</wxp>'
110 else:
111 text = text + \
112 '<wxp module="Wammu.Image" class="Bitmap">' + \
113 '<param name="tooltip" value="' + Wammu.Data.PredefinedAnimations[i['Number']][0]+ '">' + \
114 '<param name="image" value="' + "['" + string.join(Wammu.Data.PredefinedAnimations[i['Number']][1], "', '") + "']" + '">' + \
115 '</wxp>'
117 if i['ID'] in Wammu.Data.SMSIDs['PredefinedSound']:
118 if i['Number'] >= len(Wammu.Data.PredefinedSounds):
119 desc = _('Unknown predefined sound #%d') % i['Number']
120 else:
121 desc = Wammu.Data.PredefinedSounds[i['Number']][0]
122 text = text + \
123 '[<wxp module="Wammu.Image" class="Bitmap">' + \
124 '<param name="image" value="' + "['" + string.join(Wammu.Data.Note, "', '") + "']" + '">' + \
125 '</wxp>' + desc + ']'
127 if i['ID'] in Wammu.Data.SMSIDs['Sound']:
128 Wammu.Ringtone.ringtones[ringno] = i['Ringtone']
129 text = text + \
130 '<wxp module="Wammu.Ringtone" class="Ringtone">' + \
131 '<param name="tooltip" value="' + i['Ringtone']['Name'] + '">' + \
132 '<param name="ringno" value="' + str(ringno) + '">' + \
133 '</wxp>'
134 ringno += 1
136 if i['ID'] in Wammu.Data.SMSIDs['Text']:
137 fmt = '%s'
138 for x in Wammu.Data.TextFormats:
139 for name, txt, style in x[1:]:
140 if i.has_key(name) and i[name]:
141 fmt = style % fmt
142 text = text + (fmt % SmsTextFormat(cfg, i['Buffer']))
144 if i['ID'] in Wammu.Data.SMSIDs['Bitmap']:
145 x = i['Bitmap'][0]
146 text = text + \
147 '<wxp module="Wammu.Image" class="Bitmap">' + \
148 '<param name="scale" value="(' + str(cfg.ReadInt('/Message/ScaleImage')) + ')">' + \
149 '<param name="image" value="' + "['" + string.join(x['XPM'], "', '") + "']" + '">' + \
150 '</wxp>'
152 if i['ID'] in Wammu.Data.SMSIDs['Animation']:
153 data = []
154 for x in i['Bitmap']:
155 data.append("['" + string.join(x['XPM'], "', '") + "']")
156 text = text + \
157 '<wxp module="Wammu.Image" class="Throbber">' + \
158 '<param name="scale" value="(' + str(cfg.ReadInt('/Message/ScaleImage')) + ')">' + \
159 '<param name="images" value="' + "['" + string.join(data, "', '") + "']" + '">' + \
160 '</wxp>'
161 if v['SMSInfo'].has_key('Unknown') and v['SMSInfo']['Unknown']:
162 text = ('<table border="1" bgcolor="#dd7777" color="#000000"><tr><td>%s</td></tr></table>' % _('Some parts of this message were not decoded correctly, probably due to missing support for it in Gammu.')) + text
163 else:
164 text = SmsTextFormat(cfg, v['Text'])
166 return HtmlStrConv(text)