~ubuntu-branches/ubuntu/oneiric/emesene/oneiric-proposed

« back to all changes in this revision

Viewing changes to emesene/e3/base/Message.py

  • Committer: Bazaar Package Importer
  • Author(s): Devid Antonio Filoni
  • Date: 2011-03-03 14:49:13 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20110303144913-0adl9cmw2s35lvzo
Tags: 2.0~git20110303-0ubuntu1
* New upstream git revision (LP: #728469).
* Remove debian/watch, debian/emesene.xpm, debian/install and
  debian/README.source files.
* Remove 21_svn2451_fix_avatar and 20_dont_build_own_libmimic patches.
* debian/control: modify python to python (>= 2.5) in Build-Depends field.
* debian/control: remove python-libmimic from Recommends field.
* debian/control: modify python-gtk2 (>= 2.10) to python-gtk2 (>= 2.12) in
  Depends field.
* debian/control: add python-appindicator and python-xmpp to Recommends
  field.
* debian/control: add python-papyon (>= 0.5.4) and python-webkit to Depends
  field.
* debian/control: update Description field.
* debian/control: add python-setuptools to Build-Depends field.
* debian/control: move python-dbus and python-notify to Depends field.
* Update debian/copyright file.
* Update debian/links file.
* debian/menu: update description field.
* Bump Standards-Version to 3.9.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
 
 
3
#    This file is part of emesene.
 
4
#
 
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.
 
9
#
 
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.
 
14
#
 
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
 
18
 
 
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)
 
22
 
 
23
    def __init__(self, type_, body, account, style=None, timestamp=None):
 
24
        self.type = type_
 
25
        self.body = body
 
26
        self.account = account
 
27
        self.timestamp = timestamp
 
28
 
 
29
        if style is not None:
 
30
            self.style = style
 
31
        else:
 
32
            self.style = Style()
 
33
 
 
34
    def __str__(self):
 
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)
 
38
 
 
39
class Style(object):
 
40
    '''a class that represents the style of a message'''
 
41
 
 
42
    def __init__(self, font='Arial', color=None, bold=False, italic=False,
 
43
        underline=False, strike=False, size_=None):
 
44
        self.font = font
 
45
        self.size = size_
 
46
 
 
47
        if color is not None:
 
48
            self.color = color
 
49
        else:
 
50
            self.color = Color.from_hex('#000000')
 
51
 
 
52
        self.bold = bold
 
53
        self.italic = italic
 
54
        self.underline = underline
 
55
        self.strike = strike
 
56
 
 
57
    def __str__(self):
 
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)
 
62
 
 
63
    def to_css(self):
 
64
        '''return a string representing the current style as CSS rules'''
 
65
        style = ''
 
66
 
 
67
        if self.font is not None:
 
68
            style += 'font-family: %s; ' % self.font
 
69
 
 
70
        if self.size is not None:
 
71
            style += 'font-size: %spt; ' % str(self.size)
 
72
 
 
73
        if self.color is not None:
 
74
            style += 'color: #%s; ' % self.color.to_hex()
 
75
 
 
76
        if self.bold:
 
77
            style += 'font-weight: bold; '
 
78
 
 
79
        if self.italic:
 
80
            style += 'font-style: italic; '
 
81
 
 
82
        if self.underline:
 
83
            style += 'text-decoration: underline; '
 
84
 
 
85
        if self.strike:
 
86
            style += 'text-decoration: line-through;'
 
87
 
 
88
        return style
 
89
 
 
90
class Color(object):
 
91
    '''a class representing a RGBA color'''
 
92
 
 
93
    def __init__(self, red=0, green=0, blue=0, alpha=0):
 
94
        '''class contructor'''
 
95
 
 
96
        self.red = red
 
97
        self.green = green
 
98
        self.blue = blue
 
99
        self.alpha = alpha
 
100
 
 
101
    def to_hex(self):
 
102
        '''return a hexa representation of the color, usefull for pango'''
 
103
 
 
104
        red = self.red
 
105
        if red > 255:
 
106
            red /= 256
 
107
 
 
108
        green = self.green
 
109
        if green > 255:
 
110
            green /= 256
 
111
 
 
112
        blue = self.blue
 
113
        if blue > 255:
 
114
            blue /= 256
 
115
 
 
116
        red = hex(red)[2:][-2:]
 
117
        green = hex(green)[2:][-2:]
 
118
        blue = hex(blue)[2:][-2:]
 
119
 
 
120
        if len(red) == 1:
 
121
            red = '0' + red
 
122
 
 
123
        if len(green) == 1:
 
124
            green = '0' + green
 
125
 
 
126
        if len(blue) == 1:
 
127
            blue = '0' + blue
 
128
 
 
129
        return '%s%s%s' % (red, green, blue)
 
130
 
 
131
    def __str__(self):
 
132
        '''return a string representation of the object'''
 
133
 
 
134
        return '<Color red="%d" green="%d" blue="%d" alpha="%d">' % \
 
135
            (self.red, self.green, self.blue, self.alpha)
 
136
 
 
137
    def __iter__(self):
 
138
        '''return an iterator'''
 
139
        yield self.red
 
140
        yield self.green
 
141
        yield self.blue
 
142
 
 
143
    @classmethod
 
144
    def from_hex(cls, hex_str):
 
145
        '''return a color from an hexadecimal representation of type
 
146
        #rrggbb, rrggbb, #rgb, rgb'''
 
147
 
 
148
        if hex_str.startswith('#'):
 
149
            if hex_str == '#0': #some MSN clients define black as simply '#0'
 
150
                hex_str = '#000000'
 
151
 
 
152
            if len(hex_str) not in (4, 7):
 
153
                raise ValueError('Invalid color format', hex_str)
 
154
            else:
 
155
                hex_str = hex_str[1:]
 
156
        elif len(hex_str) not in (3, 6):
 
157
            raise ValueError('Invalid color format', hex_str)
 
158
 
 
159
        if len(hex_str) == 3:
 
160
            hex_str = hex_str[0] * 2 + hex_str[1] * 2 + hex_str[2] * 2
 
161
 
 
162
        red = int(hex_str[0:2], 16)
 
163
        green = int(hex_str[2:4], 16)
 
164
        blue = int(hex_str[4:6], 16)
 
165
 
 
166
        return Color(red, green, blue)
 
167