Translated using Weblate.
[wammu.git] / Wammu / MessageDisplay.py
blob08a4307ba575e23fc37e0a0993a7b120e84e8613
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 - 2010 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, doxml = False):
35 if txt is None:
36 return ''
37 if cfg.Read('/Message/Format') == 'yes':
38 parts = []
39 arr = txt.split(' ')
40 for a in arr:
41 if re.match('^([a-z]+[^ ]*)?[A-Z].*[a-z]{2,}[A-Z]{2,}.*$', a) != None:
42 prevtype = 'p'
43 if UnicodeConv(string.lowercase).find(a[0]) != -1:
44 curtype = 'l'
45 elif UnicodeConv(string.uppercase).find(a[0]) != -1:
46 curtype = 'u'
47 elif UnicodeConv(string.digits).find(a[0]) != -1:
48 curtype = 'd'
49 else:
50 curtype = 'p'
52 s = a[0]
54 for x in a[1:]:
55 if UnicodeConv(string.lowercase).find(x) != -1:
56 nexttype = 'l'
57 elif UnicodeConv(string.uppercase).find(x) != -1:
58 nexttype = 'u'
59 elif UnicodeConv(string.digits).find(x) != -1:
60 nexttype = 'd'
61 else:
62 nexttype = 'p'
64 if curtype == nexttype:
65 s += x
66 else:
67 if curtype == 'u' and nexttype == 'l' and prevtype == 'p' and len(s) == 1:
68 curtype = 'l'
69 prevtype = 'u'
70 s += x
71 continue
72 if curtype == 'p':
73 parts[-1] += s
74 elif curtype == 'u':
75 parts.append(s.lower())
76 else:
77 parts.append(s)
78 s = x
79 prevtype = curtype
80 curtype = nexttype
82 if curtype == 'p':
83 parts[-1] += s
84 elif curtype == 'u':
85 parts.append(s.lower())
86 else:
87 parts.append(s)
88 s = x
89 else:
90 parts.append(a)
91 ret = ' '.join(parts)
92 else:
93 ret = txt
94 if dohtml or doxml:
95 xmlsafe = xml.sax.saxutils.escape(ret)
96 if doxml:
97 return xmlsafe.replace('\n', '<br />')
98 else:
99 return xmlsafe.replace('\n', '<br>')
100 else:
101 return ret.replace('\n', ' ')
103 def SmsToHtml(cfg, v):
104 if v.has_key('SMSInfo'):
105 text = ''
106 ringno = 0
107 Wammu.Ringtone.ringtones = {}
108 for i in v['SMSInfo']['Entries']:
109 if i['ID'] in Wammu.Data.SMSIDs['PredefinedAnimation']:
110 if i['Number'] > len(Wammu.Data.PredefinedAnimations):
111 text = text + \
112 '<wxp module="Wammu.Image" class="Bitmap">' + \
113 '<param name="tooltip" value="' + (_('Predefined animation number %d') % i['Number']) + '">' + \
114 '<param name="image" value="' + "['" + string.join(Wammu.Data.UnknownPredefined, "', '") + "']" + '">' + \
115 '</wxp>'
116 else:
117 text = text + \
118 '<wxp module="Wammu.Image" class="Bitmap">' + \
119 '<param name="tooltip" value="' + Wammu.Data.PredefinedAnimations[i['Number']][0]+ '">' + \
120 '<param name="image" value="' + "['" + string.join(Wammu.Data.PredefinedAnimations[i['Number']][1], "', '") + "']" + '">' + \
121 '</wxp>'
123 if i['ID'] in Wammu.Data.SMSIDs['PredefinedSound']:
124 if i['Number'] >= len(Wammu.Data.PredefinedSounds):
125 desc = _('Unknown predefined sound #%d') % i['Number']
126 else:
127 desc = Wammu.Data.PredefinedSounds[i['Number']][0]
128 text = text + \
129 '[<wxp module="Wammu.Image" class="Bitmap">' + \
130 '<param name="image" value="' + "['" + string.join(Wammu.Data.Note, "', '") + "']" + '">' + \
131 '</wxp>' + desc + ']'
133 if i['ID'] in Wammu.Data.SMSIDs['Sound']:
134 Wammu.Ringtone.ringtones[ringno] = i['Ringtone']
135 text = text + \
136 '<wxp module="Wammu.Ringtone" class="Ringtone">' + \
137 '<param name="tooltip" value="' + i['Ringtone']['Name'] + '">' + \
138 '<param name="ringno" value="' + str(ringno) + '">' + \
139 '</wxp>'
140 ringno += 1
142 if i['ID'] in Wammu.Data.SMSIDs['Text']:
143 fmt = '%s'
144 for x in Wammu.Data.TextFormats:
145 for name, txt, style in x[1:]:
146 if i.has_key(name) and i[name]:
147 fmt = style % fmt
148 text = text + (fmt % SmsTextFormat(cfg, i['Buffer']))
150 if i['ID'] in Wammu.Data.SMSIDs['Bitmap']:
151 x = i['Bitmap'][0]
152 text = text + \
153 '<wxp module="Wammu.Image" class="Bitmap">' + \
154 '<param name="scale" value="(' + str(cfg.ReadInt('/Message/ScaleImage')) + ')">' + \
155 '<param name="image" value="' + "['" + string.join(x['XPM'], "', '") + "']" + '">' + \
156 '</wxp>'
158 if i['ID'] in Wammu.Data.SMSIDs['Animation']:
159 data = []
160 for x in i['Bitmap']:
161 data.append("['" + string.join(x['XPM'], "', '") + "']")
162 text = text + \
163 '<wxp module="Wammu.Image" class="Throbber">' + \
164 '<param name="scale" value="(' + str(cfg.ReadInt('/Message/ScaleImage')) + ')">' + \
165 '<param name="images" value="' + "[" + string.join(data, ", ") + "]" + '">' + \
166 '</wxp>'
167 if v['SMSInfo'].has_key('Unknown') and v['SMSInfo']['Unknown']:
168 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
169 else:
170 text = SmsTextFormat(cfg, v['Text'])
172 return HtmlStrConv(text)