Beispiel #1
0
    def _session_started(self):

        categories_dict = {}
        default_group = e3.Group('Friends', 'Friends')
        group_group = e3.Group('Groups', 'Groups')
        categories_dict[0] = default_group
        categories_dict[-1] = group_group

        for index in self.res_manager.categories:
            key = index.key()
            group = e3.Group(self.res_manager.categories[key].name, key)
            categories_dict[key] = group

        for uin in self.res_manager.contacts:
            key = uin.key()
            #nick = self.res_manager.contacts[uin].nick
            _status = e3.status.OFFLINE
            try:
                if not self.res_manager.contacts[key].status == '':
                    _status = STATUS_MAP_REVERSE[
                        self.res_manager.contacts[key].status]
            except KeyError, e:
                _stauts = e3.status.ONLINE

            contact = e3.Contact(account=key,
                                 identifier=str(key),
                                 message=self.res_manager.contacts[key].lnick,
                                 _status=_status,
                                 blocked=False,
                                 cid=key)

            index = self.res_manager.contacts[key].cate_index
            group = categories_dict[index]
            self._add_contact_to_group(contact, group.name)
            self.session.contacts.contacts[key] = contact
Beispiel #2
0
    def _search_item(self, uid, parent):
        '''Searches na item, given its uid'''
        if parent == self:
            item_locator = parent.item
        else:
            item_locator = parent.child

        num_rows = parent.rowCount()
        for i in range(num_rows):
            found_item = item_locator(i, 0)
            found_uid = found_item.data(Role.UidRole).toString()
            if found_uid == QtCore.QString(str(uid)).trimmed():
                return found_item

        if uid in [self.NO_GRP_UID, self.ONL_GRP_UID, self.OFF_GRP_UID]:
            if uid == self.NO_GRP_UID:
                group = e3.Group(tr("No group"),
                                 identifier=uid,
                                 type_=e3.Group.NONE)
            elif uid == self.ONL_GRP_UID:
                group = e3.Group(tr("Online"),
                                 identifier=uid,
                                 type_=e3.Group.ONLINE)
                group.type = e3.Group.ONLINE
            elif uid == self.OFF_GRP_UID:
                group = e3.Group(tr("Offline"),
                                 identifier=uid,
                                 type_=e3.Group.OFFLINE)
            new_group_item = self.add_group(group, force=True)
            return new_group_item
Beispiel #3
0
    def test_add_groups(self):
        g1 = e3.Group("group 1", "g1")
        g2 = e3.Group("group 2", "g2")

        groups = {g1.identifier: g1, g2.identifier: g2}

        logger.add_groups(groups)
        # add twice to check that they are only added once
        logger.add_groups(groups)
        time.sleep(1)
        self.assertEquals(len(logger.logger.groups), 2)
Beispiel #4
0
    def handle_response(self, request, response):
        '''handle the response'''
        if response.status == 200:
            self.session.groups[self.gid] = e3.Group(self.name, self.gid)

            self.session.add_event(e3.Event.EVENT_GROUP_RENAME_SUCCEED,
                self.gid, self.name)
        else:
            log.debug(response.body + '\n' + request.body)
            self.session.add_event(e3.Event.EVENT_GROUP_RENAME_FAILED,
                self.gid, self.name)
Beispiel #5
0
    def handle_response(self, request, response):
        '''handle the response'''
        if response.status == 200:
            gid = common.get_value_between(response.body, '<guid>', '</guid>')
            self.session.groups[gid] = e3.Group(self.name, gid)

            self.session.add_event(e3.Event.EVENT_GROUP_ADD_SUCCEED,
                self.name, gid)
        else:
            log.debug(response.body + '\n' + request.body)
            self.session.add_event(e3.Event.EVENT_GROUP_ADD_FAILED, self.name)
Beispiel #6
0
    def _start_from_cache(self):
        '''try to send the adl with the data from cache'''
        logger = self.session.logger.logger

        if len(logger.accounts) < 2:
            return False

        # thread madness I should be careful to not modify accounts while
        # iterating
        for (account, contact) in logger.accounts.items():
            new_contact = e3.Contact(account, contact.cid, contact.nick,
                                     contact.message)
            new_contact.groups = contact.groups[:]
            self.session.contacts.contacts[account] = new_contact

        for (gid, group) in logger.groups.iteritems():
            new_group = e3.Group(group.name, gid)
            new_group.contacts = group.accounts[:]
            self.session.groups[gid] = new_group

        nick = my_account = self.session.account.account
        account_info = logger.accounts.get(my_account, None)

        if account_info:
            if account_info.nick:
                nick = account_info.nick
            else:
                nick = my_account

        nick = nick.decode('utf-8', 'replace').encode('utf-8')
        self.socket.send_command('PRP', ('MFN', urllib.quote(nick)))
        self.session.add_event(e3.Event.EVENT_NICK_CHANGE_SUCCEED, nick)
        self.socket.send_command('BLP', ('BL', ))

        for adl in self.session.contacts.get_adls():
            self.socket.send_command('ADL', payload=adl)

        self.session.add_event(e3.Event.EVENT_CONTACT_LIST_READY)

        return True
Beispiel #7
0
    def add_contact(self, contact, group=None):
        '''add a contact to the contact list, add it to the group if
        group is not None'''
        try:
            weight = int(self.session.config.d_weights.get(contact.account, 0))
        except ValueError:
            weight = 0

        self.session.config.d_weights[contact.account] = weight
        offline = contact.status == e3.status.OFFLINE
        is_online = not offline

        contact_data = (
            self._get_contact_pixbuf_or_default(contact), contact,
            self.format_nick(contact), True,
            utils.safe_gtk_pixbuf_load(
                gui.theme.image_theme.status_icons[contact.status]), weight,
            False, offline)

        # if group_offline is set and the contact is offline then put it on the
        # special offline group
        if self.group_offline and offline:
            duplicate = self._duplicate_check(contact)
            if duplicate is not None:
                return duplicate
            if not self.offline_group:
                self.session.config.d_weights['1'] = 0
                self.offline_group = e3.Group(_("Offline"),
                                              identifier='1',
                                              type_=e3.Group.OFFLINE)
                self.offline_group_iter = self.add_group(
                    self.offline_group, True)

            self.offline_group.contacts.append(contact.account)
            self.update_offline_group()

            return self._model.append(self.offline_group_iter, contact_data)

        # if we are in order by status mode and contact is online,
        # we add online contacts to their online group :)
        if self.order_by_status and is_online:
            duplicate = self._duplicate_check(contact)
            if duplicate is not None:
                return duplicate
            if not self.online_group:
                self.session.config.d_weights['0'] = 1
                self.online_group = e3.Group(_("Online"),
                                             identifier='0',
                                             type_=e3.Group.ONLINE)
                self.online_group_iter = self.add_group(
                    self.online_group, True)

            self.online_group.contacts.append(contact.account)
            self.update_online_group()

            return self._model.append(self.online_group_iter, contact_data)

        # if it has no group and we are in order by group then add it to the
        # special group "No group"
        if not group and not self.order_by_status:
            if self.no_group:
                self.no_group.contacts.append(contact.account)
                self.update_no_group()

                return self._model.append(self.no_group_iter, contact_data)
            else:
                self.no_group = e3.Group(_("No group"),
                                         identifier='0',
                                         type_=e3.Group.NONE)
                self.no_group_iter = self.add_group(self.no_group, True)
                self.no_group.contacts.append(contact.account)
                self.update_no_group()

                return self._model.append(self.no_group_iter, contact_data)

        # if no group add it to the root, but check that it's not on a group
        # or in the root already
        if not group or self.order_by_status:
            for row in self._model:
                obj = row[1]
                # check on group
                if isinstance(obj, e3.Group):
                    for contact_row in row.iterchildren():
                        con = contact_row[1]
                        if con.account == contact.account:
                            return contact_row.iter
                # check on the root
                elif isinstance(obj,
                                e3.Contact) and obj.account == contact.account:
                    return row.iter

            return self._model.append(None, contact_data)

        for row in self._model:
            obj = row[1]
            if isinstance(obj, e3.Group) and obj.name == group.name:
                # if the contact is already on the group, then dont add it
                for contact_row in row.iterchildren():
                    con = contact_row[1]
                    if con.account == contact.account:
                        return contact_row.iter

                return_iter = self._model.append(row.iter, contact_data)
                self.update_group(group)

                # search the use on the root to remove it if it's there
                # since we added him to a group
                for irow in self._model:
                    iobj = irow[1]
                    if isinstance(iobj, e3.Contact) and \
                            iobj.account == contact.account:
                        del self._model[irow.iter]

                return return_iter

        else:  #######WTF??? where does this belong???
            self.add_group(group)
            result = self.add_contact(contact, group)
            self.update_group(group)

            return result
Beispiel #8
0
 def _add_group(self, name):
     """
     method to add a group to the contact list
     """
     self.session.groups[name] = e3.Group(name, name)
Beispiel #9
0
    def handle_response(self, request, response):
        '''handle the response'''
        if response.status == 200:
            parser = XmlParser.DynamicParser(response.body)
            # Retrieve groups
            for group_dict in parser.groups:
                group_id = group_dict['groupId']
                group_name = group_dict['name']

                if group_id in self.session.groups:
                    self.session.groups[group_id].name = group_name
                else:
                    self.session.groups[group_id] = \
                        e3.Group(group_name, group_id)

            # Retrieve contacts
            for contact_dict in parser.contacts:
                if 'isMessengerUser' in contact_dict \
                  and 'passportName' in contact_dict \
                  and contact_dict['isMessengerUser'] == 'true':
                    # valid
                    email = contact_dict['passportName'].lower()
                    if email in self.session.contacts.contacts:
                        contact = self.session.contacts.contacts[email]
                    else:
                        contact = e3.Contact(email)
                        self.session.contacts.contacts[email] = contact
                else:
                    continue

                contact.identifier = contact_dict.get('contactId', '')
                contact.cid = contact_dict.get('CID', '')

                contact.groups = []
                for guid in contact_dict['groupIds']:
                    contact.groups.append(guid)
                    group = self.session.groups[guid]

                    if contact.account not in group.contacts:
                        group.contacts.append(contact.account)

                for ann in contact_dict['Annotations']:
                    if ann.get('Name', None) == 'AB.NickName':
                        contact.alias = urllib.unquote(ann['Value'])
                        break

                if not contact.nick:
                    contact.nick = urllib.unquote(contact_dict.get(
                        'displayName', contact.account))

                contact.attrs['mobile'] = \
                    contact_dict.get('isMobileIMEnabled', None) == 'true'

                contact.attrs['space'] = \
                    contact_dict.get('hasSpace', None) == 'true'

            log.debug('dynamic finished')

            self.session.contacts.me.identifier = \
                response.body.split('<contactType>Me</contactType>')\
                [1].split('</CID>')[0].split('<CID>')[1].strip()

            # get our nick
            try:
                nick = response.body.split('<contactType>Me</contactType>')\
                    [1].split('</displayName>')[0].split('<displayName>')[1]
                nick = common.unescape(nick)
            except IndexError:
                nick = self.session.contacts.me.account

            if not self.session.contacts.me.nick or \
                self.session.contacts.me != self.session.account.account:
                self.session.contacts.me.nick = nick

            if self.on_login:
                # set our nick
                self.command_queue.put(Command('PRP', params=('MFN',
                    urllib.quote(nick))))
                self.session.add_event(e3.Event.EVENT_NICK_CHANGE_SUCCEED, nick)

                if not self.started_from_cache:
                    self.command_queue.put(Command('BLP', params=('BL',)))

            accounts = self.session.contacts.pending.keys()
            for account in accounts:
                # account in pending that is already on some other role
                # (corrupted userlist)
                if account in self.session.contacts.contacts or account in self.session.contacts.reverse:
                    del self.session.contacts.pending[account]
                    # this line doen't work for accounts on corrupted userlists
                    # RemovePendingContact(self.session, account).start()

            self.session.add_event(e3.Event.EVENT_CONTACT_LIST_READY)
            self.session.logger.add_contact_by_group(
                self.session.contacts.contacts, self.session.groups)

            if not self.started_from_cache:
                for adl in self.session.contacts.get_adls():
                    self.command_queue.put(Command('ADL', payload=adl))

            GetProfile(self.session, self.session.contacts.me.identifier).start()

        else:
            log.debug('error requestion dynamic items')