~alexlauni/tictactoe/trunk

« back to all changes in this revision

Viewing changes to tictactoe/contactselector.py

  • Committer: Alex Launi
  • Date: 2009-09-25 22:45:52 UTC
  • Revision ID: alex.launi@gmail.com-20090925224552-v2g2569t7tobjvos
Make tictactoe resizable and tabs->spaces

Show diffs side-by-side

added added

removed removed

Lines of Context:
42
42
    CONNECTION_PRESENCE_TYPE_BUSY,
43
43
    CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY,
44
44
    HANDLE_TYPE_LIST)
45
 
                
 
45
 
46
46
dbus.mainloop.glib.DBusGMainLoop(set_as_default = True)
47
47
theme = gtk.icon_theme_get_default()
48
48
logger = logging.root
58
58
 
59
59
def dbus_int_list_to_string(dbus_list):
60
60
    """Convert a dbus-list of integer to normal python one
61
 
    
 
61
 
62
62
    dbus_list can also be a normal python list; can't hurt"""
63
63
    return [int(x) for x in dbus_list]
64
 
        
 
64
 
65
65
class _Contact:
66
66
    """Help class for the contact to be stored in ContactListStore"""
67
67
 
81
81
        # dict of {handle=>Contact}
82
82
        # If ListStore supports searching, this _contact_list is not needed.
83
83
        self._contact_list = {}
84
 
                
 
84
 
85
85
        self._conn.call_when_ready(self._connection_ready_cb)
86
 
                
 
86
 
87
87
    def _connection_ready_cb(self, conn):
88
88
        logger.debug('Connection is ready: %s' % conn.service_name)
89
89
        # TODO react to connection status change
94
94
        if CONNECTION_INTERFACE_REQUESTS not in conn:
95
95
            logger.warning('REQUESTS interface not available on %s' % conn.service_name)
96
96
            return
97
 
                        
 
97
 
98
98
        if CONNECTION_INTERFACE_AVATARS not in conn:
99
99
            logger.warning('AVATARS interface not available on %s' % conn.service_name)
100
100
        else:
144
144
            False,
145
145
            reply_handler = self._get_contact_attributes_cb,
146
146
            error_handler = self._error_cb)
147
 
                        
 
147
 
148
148
        self._conn[CONNECTION_INTERFACE_AVATARS].RequestAvatars(handles)
149
 
        
 
149
 
150
150
    def _avatar_retrieved_cb(self, contact, token, avatar, type):
151
151
        path = os.path.join(avatar_cache_dir, token)
152
152
        if os.path.exists(path):
153
 
            return      
 
153
            return
154
154
        file = open(path, 'wb')
155
155
        file.write(avatar)
156
156
        file.close()
176
176
                avatar = value
177
177
            elif key == CONNECTION_INTERFACE_SIMPLE_PRESENCE + '/presence':
178
178
                presence = value
179
 
                                
 
179
 
180
180
        # TODO Can they be None?
181
181
        assert contact_id is not None
182
182
        assert presence is not None
205
205
 
206
206
class ContactListStore(gtk.ListStore):
207
207
    """
208
 
        ListStore that contains contact list from (all connections of) telepathy
 
208
    ListStore that contains contact list from (all connections of) telepathy
209
209
    """
210
210
 
211
211
    # indices of columns that are of interest to display
265
265
                return avatar
266
266
            except:
267
267
                pass
268
 
                                
 
268
 
269
269
        return self._load_default_avatar()
270
 
                
 
270
 
271
271
    def _load_default_avatar(self):
272
272
        return theme.load_icon("stock_people", 48, 0)
273
273
 
274
274
class ContactSelector(gtk.IconView):
275
275
    """An Icon view for selecting contacts"""
276
 
        
 
276
 
277
277
    COL_CONTACT = 2
278
278
    COL_NAME = 3
279
279
    COL_PIXBUF = 4
280
 
    
 
280
 
281
281
    def __init__(self):
282
282
        list_store = ContactListStore()
283
283
        list_store.set_sort_func(1, self._sort_contacts)
284
284
        list_store.set_sort_column_id(1, gtk.SORT_ASCENDING)
285
285
        online_model_filter = list_store.filter_new()
286
286
        online_model_filter.set_visible_func(self._filter_online_contacts)
287
 
        
 
287
 
288
288
        super(ContactSelector, self).__init__(online_model_filter)
289
 
        
 
289
 
290
290
        self.set_selection_mode(gtk.SELECTION_SINGLE)
291
291
        self.set_text_column(self.COL_NAME)
292
292
        self.set_pixbuf_column(self.COL_PIXBUF)
294
294
        self.set_column_spacing(2)
295
295
        self.set_row_spacing(2)
296
296
        self.set_columns(4)
297
 
                
 
297
 
298
298
    def _sort_contacts(self, model, iter1, iter2):
299
299
        contact1 = self._get_contact(model, iter1)
300
300
        contact2 = self._get_contact(model, iter2)
301
301
        if contact1 is not None and contact2 is not None:
302
302
            return contact1.alias.lower() > contact2.alias.lower()
303
303
        return 0
304
 
        
 
304
 
305
305
    def _filter_online_contacts(self, model, treeiter):
306
306
        online_statues = [
307
307
            CONNECTION_PRESENCE_TYPE_AVAILABLE,
313
313
            return contact.presence[0] in online_statues
314
314
        else:
315
315
            return False
316
 
                        
 
316
 
317
317
    def _get_contact(self, model, treeiter):
318
318
        contact_column = ContactListStore.COL_CONTACT
319
319
        return model[treeiter][contact_column] # TODO: why contact can be None?