1
# -*- coding: utf-8 -*-
3
# This file is part of emesene.
5
# emesene is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
10
# emesene is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with emesene; if not, write to the Free Software
17
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
class Message(object):
20
'''a class that represent a msn message'''
21
(TYPE_MESSAGE, TYPE_TYPING, TYPE_NUDGE, TYPE_P2P, TYPE_UNK, TYPE_FLNMSG) = range(6)
23
def __init__(self, type_, body, account, style=None, timestamp=None):
26
self.account = account
27
self.timestamp = timestamp
35
'''return a string representation of a message'''
36
return '<message from="%s" style="%s" type="%i">' % (self.account,
37
str(self.style), self.type)
40
'''a class that represents the style of a message'''
42
def __init__(self, font='Arial', color=None, bold=False, italic=False,
43
underline=False, strike=False, size_=None):
50
self.color = Color.from_hex('#000000')
54
self.underline = underline
58
'''return a string representation of the object'''
59
return '<style font="%s" color="%s" b="%s" i="%s" u="%s" s="%s">' \
60
% (self.font, str(self.color), self.bold, self.italic,
61
self.underline, self.strike)
64
'''return a string representing the current style as CSS rules'''
67
if self.font is not None:
68
style += 'font-family: %s; ' % self.font
70
if self.size is not None:
71
style += 'font-size: %spt; ' % str(self.size)
73
if self.color is not None:
74
style += 'color: #%s; ' % self.color.to_hex()
77
style += 'font-weight: bold; '
80
style += 'font-style: italic; '
83
style += 'text-decoration: underline; '
86
style += 'text-decoration: line-through;'
91
'''a class representing a RGBA color'''
93
def __init__(self, red=0, green=0, blue=0, alpha=0):
94
'''class contructor'''
102
'''return a hexa representation of the color, usefull for pango'''
116
red = hex(red)[2:][-2:]
117
green = hex(green)[2:][-2:]
118
blue = hex(blue)[2:][-2:]
129
return '%s%s%s' % (red, green, blue)
132
'''return a string representation of the object'''
134
return '<Color red="%d" green="%d" blue="%d" alpha="%d">' % \
135
(self.red, self.green, self.blue, self.alpha)
138
'''return an iterator'''
144
def from_hex(cls, hex_str):
145
'''return a color from an hexadecimal representation of type
146
#rrggbb, rrggbb, #rgb, rgb'''
148
if hex_str.startswith('#'):
149
if hex_str == '#0': #some MSN clients define black as simply '#0'
152
if len(hex_str) not in (4, 7):
153
raise ValueError('Invalid color format', hex_str)
155
hex_str = hex_str[1:]
156
elif len(hex_str) not in (3, 6):
157
raise ValueError('Invalid color format', hex_str)
159
if len(hex_str) == 3:
160
hex_str = hex_str[0] * 2 + hex_str[1] * 2 + hex_str[2] * 2
162
red = int(hex_str[0:2], 16)
163
green = int(hex_str[2:4], 16)
164
blue = int(hex_str[4:6], 16)
166
return Color(red, green, blue)