~emesene-team/emesene/master

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# -*- coding: utf-8 -*-

#    This file is part of emesene.
#
#    emesene is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 3 of the License, or
#    (at your option) any later version.
#
#    emesene is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with emesene; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import gtk
import e3
import gui

import TextField
import StatusButton

import extension

class UserPanel(gtk.VBox):
    '''a panel to display and manipulate the user information'''
    NAME = 'User Panel'
    DESCRIPTION = 'A widget to display/modify the account information on the main window'
    AUTHOR = 'Mariano Guerra'
    WEBSITE = 'www.emesene.org'

    def __init__(self, session, main_window):
        '''constructor'''
        gtk.VBox.__init__(self)

        self.main_window = main_window
        self.session = session
        self._enabled = True

        Avatar = extension.get_default('avatar')

        self.avatar = Avatar(cell_dimension=48)

        self.avatarBox = gtk.EventBox()
        self.avatarBox.set_events(gtk.gdk.BUTTON_PRESS_MASK)
        if session.session_has_service(e3.Session.SERVICE_PROFILE_PICTURE):
            self.avatarBox.connect('button-press-event', self.on_avatar_click)
            self.avatarBox.set_tooltip_text(_('Click here to set your avatar'))
        self.avatarBox.add(self.avatar)
        self.avatarBox.set_border_width(4)
        self.avatar.set_from_file(session.config_dir.get_path("last_avatar"))

        self.nick = TextField.TextField(session.contacts.me.display_name, session.contacts.me.account, False)
        self.nick.set_tooltip_text(_('Click here to set your nick name'))
        self.nick.connect('text-changed', self._on_nick_changed)

        self.status = StatusButton.StatusButton(session)
        self.status.set_tooltip_text(_('Click here to change your status'))
        self.status.set_status(session.contacts.me.status)

        self.search = gtk.ToggleButton()
        self.search.set_tooltip_text(_('Search (Ctrl+F)'))
        self.search.set_image(gtk.image_new_from_stock(gtk.STOCK_FIND,
            gtk.ICON_SIZE_MENU))
        self.search.set_relief(gtk.RELIEF_NONE)

        self.mail = gtk.Button(label="(0)")
        self.mail.set_tooltip_text(_('Click here to access your mail'))
        self.mail.get_settings().set_property( "gtk-button-images", True )
        self.mail.set_image(gtk.image_new_from_file(gui.theme.image_theme.mailbox))
        self.mail.set_relief(gtk.RELIEF_NONE)
        self.mail.connect('clicked', lambda *args : main_window.on_mail_click())

        empty_message_text = _("Click here to set your message")
        self.message = TextField.TextField(session.contacts.me.message,
            '[I]' + empty_message_text + '[/I]',
            True)
        self.message.set_tooltip_text(empty_message_text)
        self.message.connect('text-changed', self._on_message_changed)
        self.toolbar = gtk.HBox()

        hbox = gtk.HBox()
        hbox.set_border_width(1)
        hbox.pack_start(self.avatarBox, False)

        vbox = gtk.VBox()
        nick_hbox = gtk.HBox()
        nick_hbox.pack_start(self.nick, True, True)
        nick_hbox.pack_start(self.mail, False)
        nick_hbox.pack_start(self.search, False)

        # enable this code and you'll get a nice button which fires a signal
        #def do_something_weird(item):
        #    from e3.base.Event import Event
        #    session.add_event(Event.EVENT_DISCONNECTED, 'Tested disconnection', 0)
        #test_btn=gtk.Button(label="(!)")
        #test_btn.connect('clicked', do_something_weird)
        #test_btn.show()
        #nick_hbox.pack_start(test_btn, False)
        self.userpanel_button = None
        vbox.pack_start(nick_hbox, False)
        self.message_hbox = gtk.HBox()
        self.message_hbox.pack_start(self.message, True, True)
        self.message_hbox.pack_start(self.status, False)
        vbox.pack_start(self.message_hbox, False)

        hbox.pack_start(vbox, True, True)

        self.pack_start(hbox, True, True)
        self.pack_start(self.toolbar, False)

        hbox.show()
        nick_hbox.show()
        self.message_hbox.show()
        vbox.show()
        self._add_subscriptions()

    def _on_userpanel_button_changed(self, newvalue):
        if type(self.userpanel_button) != newvalue:
            #first remove current button
            if not self.userpanel_button is None:
                self.message_hbox.remove(self.userpanel_button)
                self.userpanel_button = None

            if newvalue is not None:
                #add new button
                self.userpanel_button = newvalue(self, self.main_window)
                self.message_hbox.pack_start(self.userpanel_button, False)
                self.message_hbox.reorder_child(self.userpanel_button, 1)
                self.userpanel_button.show()

    def show(self):
        '''override show'''
        gtk.VBox.show(self)
        self.avatar.show()
        self.avatarBox.show()
        self.nick.show()
        self.message.show()
        if self.session.session_has_service(e3.Session.SERVICE_STATUS):
            self.status.show()
        else:
            self.status.hide()

        self.search.show()
        if self.userpanel_button:
            self.userpanel_button.show()
        if self.session.config.b_show_mail_inbox:
            self.mail.show()
        self.toolbar.show()

    def show_all(self):
        '''override show_all'''
        self.show()

    def _set_enabled(self, value):
        '''set the value of enabled and modify the widgets to reflect the status
        '''
        self.nick.enabled = value
        self.message.enabled = value
        self.status.set_sensitive(value)
        self.search.set_sensitive(value)
        if self.userpanel_button:
            self.userpanel_button.set_sensitive(value)
        self._enabled = value

    def _get_enabled(self):
        '''return the value of the enabled property
        '''
        return self._enabled

    enabled = property(fget=_get_enabled, fset=_set_enabled)

    def on_status_change_succeed(self, stat):
        '''callback called when the status has been changed successfully'''
        self.status.set_status(stat)

    def on_message_change_succeed(self, message):
        '''callback called when the message has been changed successfully'''
        if self.session.contacts.me.media is None or self.session.contacts.me.media is "":
            self.message.text = message
        else:
            self.message.text = '♫ ' + self.session.contacts.me.media

    def on_media_change_succeed(self, message):
        '''callback called when the message has been changed successfully'''
        if not message is None:
            self.message.text = message

    def on_contact_list_ready(self):
        '''callback called when the contact list is ready to be used'''
        self.enabled = True

    def on_picture_change_succeed(self, account, path):
        '''callback called when the picture of an account is changed'''
        # out account
        if account == self.session.account.account:
            self.avatar.set_from_file(path)

    def on_profile_update_succeed(self, nick, message):
        '''method called when information about our profile is obtained
        '''
        self.nick.text = nick
        if message is not '':
            self.message.text = message

    def on_avatar_click(self, widget, data):
        '''method called when user click on his avatar
        '''
        av_chooser = extension.get_and_instantiate('avatar chooser',  self.session)
        av_chooser.set_modal(True)
        av_chooser.show()

    def _on_nick_changed(self, textfield, old_text, new_text):
        '''method called when the nick is changed'''
        self.session.set_nick(new_text)

    def _on_message_changed(self, textfield, old_text, new_text):
        '''method called when the nick is changed'''
        self.session.set_message(new_text)

    def _on_show_mail_inbox_changed(self, value):
        '''callback called when config.b_show_mail_inbox changes'''
        self.mail.set_visible(value)

    def set_mail_count(self, count):
        self.mail.set_label("(%d)" % count)

    def _add_subscriptions(self):
        '''subscribe all signals'''
        self.session.signals.message_change_succeed.subscribe(
            self.on_message_change_succeed)
        self.session.signals.media_change_succeed.subscribe(
            self.on_media_change_succeed)
        if self.session.session_has_service(e3.Session.SERVICE_STATUS):
            self.session.signals.status_change_succeed.subscribe(
                self.on_status_change_succeed)
        self.session.signals.contact_list_ready.subscribe(
            self.on_contact_list_ready)
        self.session.signals.picture_change_succeed.subscribe(
            self.on_picture_change_succeed)
        self.session.signals.profile_get_succeed.subscribe(
            self.on_profile_update_succeed)
        self.session.signals.profile_set_succeed.subscribe(
            self.on_profile_update_succeed)
        self.session.config.subscribe(self._on_show_mail_inbox_changed,
            'b_show_mail_inbox')

        extension.subscribe(
            self._on_userpanel_button_changed, "userpanel button")

    def remove_subscriptions(self):
        '''unsubscribe all signals'''
        self.session.signals.message_change_succeed.unsubscribe(
            self.on_message_change_succeed)
        self.session.signals.media_change_succeed.unsubscribe(
            self.on_media_change_succeed)
        if self.session.session_has_service(e3.Session.SERVICE_STATUS):
            self.session.signals.status_change_succeed.unsubscribe(
                self.on_status_change_succeed)
        self.session.signals.contact_list_ready.unsubscribe(
            self.on_contact_list_ready)
        self.session.signals.picture_change_succeed.unsubscribe(
            self.on_picture_change_succeed)
        self.session.signals.profile_get_succeed.unsubscribe(
            self.on_profile_update_succeed)
        self.session.signals.profile_set_succeed.unsubscribe(
            self.on_profile_update_succeed)
        self.session.config.unsubscribe(self._on_show_mail_inbox_changed,
            'b_show_mail_inbox')

        extension.unsubscribe(
            self._on_userpanel_button_changed, "userpanel button")