~ubuntu-branches/ubuntu/hardy/emesene/hardy-backports

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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# -*- 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 2 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 sys

import Login
import TrayIcon
import MainMenu
import UserList
import UserPanel
import FilterEntry
import emesenelib.common

try:
    import gtk
    import gobject
except:
    print 'you need pyGTK to run emesene'
    sys.exit(-1)

class MainWindow(gtk.Window):
    '''
    This class represent the main window of emesene, 
    it inherit from gtk.Window
    and basically it is a container for other classes.
    '''

    __gsignals__ = {
        'gui-build-done' : 
            (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
                (gobject.TYPE_STRING,)),
    }

    def __init__(self, controller):
        '''Constructor'''
        gtk.Window.__init__(self)
        
        self.controller = controller
        self.config = controller.config
        # accelerators
        accelGroup = gtk.AccelGroup()
        self.add_accel_group(accelGroup)
        self.accelGroup = accelGroup
        accelGroup.connect_group(ord('M'), gtk.gdk.CONTROL_MASK, \
            gtk.ACCEL_LOCKED, self.on_toggle_menu)

        self.set_title('emesene')
        self.set_role('main') 
        
        self.x = self.config.glob['mainWindowX']
        self.y = self.config.glob['mainWindowY']

        self.set_geometry_hints(self, -1, -1)
        self.width = self.config.glob['mainWindowWidth']
        self.height = self.config.glob['mainWindowHeight']
        self.set_default_size(self.width , self.height)
       
        self.connect('size-allocate', self.on_size_alloc)

        theme = controller.theme
        gtk.window_set_default_icon_list(theme.getImage('icon16'),
                                          theme.getImage('icon32'),
                                          theme.getImage('icon48'),
                                          theme.getImage('icon96'))

        self.vbox = None
        self.login = None
        self.userList = None
        
        self.currentInterface = 'login'
        self.buildInterface('login')
        
        self.itemSelectedId = 0
        
        self.signals = []
        sap = self.signals.append
        sap(self.config.connect('change::showUserPanel', 
            self.updateConfig))
        sap(self.config.connect('change::showSearchEntry', 
            self.updateConfig))
        sap(self.config.connect('change::showStatusCombo', 
            self.updateConfig))
        sap(self.config.connect('change::showMenu', 
            self.updateConfig))
        sap(self.config.connect('change::userListAvatarSize', 
            self.updateSize))
        sap(self.config.connect('change::smallIcons', self.updateSize))
        # TODO: do we need disconnecting these signals?
        
    def on_size_alloc(self, widget, allocation):
        self.width = allocation.width
        self.height = allocation.height
    
    def on_toggle_menu(self, *args):
        self.config.user['showMenu'] = not self.config.user['showMenu']
        
    def saveToQuit(self):
        '''Saves configuration parametes and everything needed ir order
        to safely quit'''

        if self.userList:
            try:
                self.userList.saveGroupState()
            except:
                pass
        
        self.controller.config.glob['mainWindowWidth'] = self.width
        self.controller.config.glob['mainWindowHeight'] = self.height

        self.controller.config.glob['mainWindowX'] = self.x
        self.controller.config.glob['mainWindowY'] = self.y

    def quit(self, status = 0):
        '''close the window, and do all the things needed...'''     
        self.controller.quit(status)
        
    def show(self):
        '''Shows itself'''
        if not (self.x <= 0 or self.y <= 0):
            self.move(self.x, self.y)
        gtk.Window.show(self)
        
    def hide(self):
        '''Hides itself and any other window'''

        # saves position
        if self.get_property('visible'):
            self.x, self.y = self.get_position()
        
        if self.userList:
            self.userList.tooltips.hideTooltip()
        
        # ------------ let the window hide itself the last ---------
        gtk.Window.hide(self)

    def hideOrClose(self, *args):
        '''hide or close depending if we have trayicon'''

        if TrayIcon.disabled:
            self.quit(0)
        else:
            self.hide()

        return True

    def buildInterface(self , guiType = 'login'):
        '''build the interface depending on the guiType parameter, by
        default build the login window.'''
        
        # save old login values, we may need them in new interface
        if self.login:
            user = self.login.getUser()
            passwd = self.login.getPass()
            status = self.login.getStatus()
        else:
            user = passwd = status = ''

        if self.get_child() != None:
            self.remove(self.vbox)
        
        if self.userList:
            self.userList.tooltips.hideTooltip()
            self.userList.disconnect(self.itemSelectedId)

        # if i dont add this if we disconnect then the image isnt shown
        if guiType == 'userlist' and self.login:
            try:
                self.login.remove(self.login.loginImage)
            except:
                emesenelib.common.debug('Error when removing loginImage')

        self.currentInterface = guiType
                    
        #widgets

        self.vbox = gtk.VBox(spacing=2)
        self.scroll = gtk.ScrolledWindow()
        self.scroll.set_policy(gtk.POLICY_NEVER , gtk.POLICY_AUTOMATIC)
        self.scroll.set_shadow_type(gtk.SHADOW_IN)
        self.menu = MainMenu.MainMenu(self.controller, \
                                       guiType, self.accelGroup)

        if guiType == 'login':
            self.vbox.pack_start(self.menu, False, False)
            self.login = Login.Login(self.controller, 'login')
            self.vbox.pack_start(self.login)
        elif guiType == 'userlist':
            self.login = None
            self.vbox.pack_start(self.menu, False, False)
            self.userList = UserList.UserList(self.controller, \
                self.controller.theme, self.controller.config)
            self.itemSelectedId = self.userList.connect('item-selected',
                self.onItemSelected)
            
            self.userPanel = UserPanel.UserPanel(self.controller)
            self.vbox.pack_start(self.userPanel, False, False)
            self.filterEntry = FilterEntry.FilterEntry(
                self.userList.setFilterText)
            self.vbox.pack_start(self.filterEntry, False, False)
            self.scroll.add(self.userList)
            vbox2 = gtk.VBox()
            vbox2.pack_start(self.scroll)
            self.statusCombo = StatusCombo(self.controller)
            vbox2.pack_start(self.statusCombo, False, False)
            vbox2.set_border_width(2)
            self.vbox.pack_start(vbox2)
            
            vbox2.show_all()
            self.controller.connect('preferences-changed', 
                self.updateConfig)

        elif guiType == 'loading':
            self.menu.set_sensitive(False)

            self.login = Login.Login(self.controller, 'loading')
            self.login.setFieldValues(user, passwd, status)
            self.vbox.pack_start(self.menu, False, False)
            self.vbox.pack_start(self.login)
            self.menu.show_all()

        elif guiType == 'reconnect':
            self.menu.set_sensitive(False)
            self.login = Login.Login(self.controller, 'reconnect')
            self.vbox.pack_start(self.menu, False, False)
            self.vbox.pack_start(self.login)
            self.menu.show_all()
        
        self.menu.show_all()
        self.add(self.vbox)
        self.vbox.show()
        self.update(self.controller)

        self.connect('delete-event' , self.hideOrClose)
        self.emit('gui-build-done', guiType)

    def updateConfig(self, *args):
        self.update(self.controller, False)

    def updateSize(self, config, value, oldvalue):
        if value != oldvalue and self.userList:
            self.userList.fill()

    def update(self, controller, refresUserList=True):
        if not controller or self.currentInterface != 'userlist':
            return
        
        if not self.config.user['showUserPanel']:
            self.userPanel.hide()
        else:
            self.userPanel.show()
            
        if not self.config.user['showSearchEntry']:
            self.filterEntry.hide()
        else:
            self.filterEntry.show()
                
        if not self.config.user['showStatusCombo']:
            self.statusCombo.hide()
        else:
            self.statusCombo.show()
        
        if not self.config.user['showMenu']:
            self.menu.hide();
        else:
            self.menu.show();
        
        # update UserPanel Nick and PM
        if self.config.user['showUserPanel']:
            self.userPanel.personalMessageRefresh()
            self.userPanel.nickRefresh()

        if refresUserList:
            self.refreshUserList()

    def rebuild(self):
        '''repaint the currentinteface'''

        self.buildInterface(self.currentInterface)

    def refreshUserList(self, force=False):
        '''refresh the userlist :D
        (if we are in userlist mode)'''

        if self.currentInterface == 'userlist':
            groups = self.controller.msn.contactManager.groups 
            groups[_('no group')] = \
                self.controller.msn.contactManager.noGroup
            self.userList.fill(groups, force)

    def setAvatar(self, pixbuf):
        if self.currentInterface == 'userlist':
            self.userPanel.setAvatar(pixbuf)

    def onItemSelected(self, userlist, objType, obj, path):
        if objType == 'user':
            self.controller.newConversation(self.controller.msn,
                                            obj.email, None, True)
        elif objType == 'group':
            if self.userList.row_expanded(path):
                self.userList.collapse_row(path)
            else:
                self.userList.expand_row(path, False)
    

class StatusCombo(gtk.ComboBox):
    '''this class represent the combo where you set the status'''

    def __init__(self, controller):
        '''Constructor'''

        self.statusListStore = gtk.ListStore(gtk.gdk.Pixbuf, \
                      gobject.TYPE_STRING, gobject.TYPE_STRING)

        gtk.ComboBox.__init__(self, self.statusListStore)

        self.controller = controller

        self.statusPixbufCell = gtk.CellRendererPixbuf()
        self.statusTextCell = gtk.CellRendererText()
        self.pack_start(self.statusPixbufCell, False)
        self.pack_start(self.statusTextCell, False)
        self.statusPixbufCell.set_property('xalign', 0.0)
        self.statusPixbufCell.set_property('xpad', 5)
        self.statusTextCell.set_property('xalign', 0.0)
        self.statusTextCell.set_property('xpad', 5)
        self.statusTextCell.set_property('width', 195)
        self.add_attribute(self.statusPixbufCell, 'pixbuf', 0)
        self.add_attribute(self.statusTextCell, 'text', 2)
        self.set_resize_mode(0)
        self.set_wrap_width(1)

        counter = 0
        flag = False
        j = 0
        for i in controller.status_ordered[0]:
            if self.controller.contacts.get_status() == i:
                self.set_active(j)
                
            if i != 'FLN':
                self.statusListStore.append([
                    self.controller.theme.statusToPixbuf(i), i,
                    _(self.controller.status_ordered[2][j])]) # re-gettext-it
            j += 1

        # flag needed to avoid the double-changing of status when
        # user changes it from another place
        self.changeStatusFlag = True 
        
        self.connect('changed', self.on_status_changed, self.changeStatusFlag)
        self.controller.msn.connect('self-status-changed', 
            self.selfStatusChanged)

    def selfStatusChanged(self, msnp, status):
        self.changeStatusFlag = False
        statusOrdered = self.controller.status_ordered[1]
        
        if emesenelib.common.reverse_status[status] in statusOrdered:
            self.set_active(statusOrdered.index(\
                emesenelib.common.reverse_status[status]))
                
        self.changeStatusFlag = True

    def on_status_changed(self , *args):
        if self.changeStatusFlag:
            asd = self.statusListStore.get(self.get_active_iter(), 1)
            print "on_status_changed", asd
            self.controller.contacts.set_status(asd[0])