Example #1
0
def get_state_choices(curstatus = None, account = None):
    'Return state choices for all accounts, or just for one.'

    # Status choices come from connected accounts, unless there are none.
    _profile = profile()
    conn  = list(_profile.account_manager.connected_accounts)
    accts = conn
    if accts != [_profile] and _profile in accts:
        accts.remove(_profile)

    # When no accounts are connected, just use sensible defaults.
    if not accts:
        return DEFAULT_STATUS_CHOICES

    # Sort status messages by "category" in the order they appear in the status
    # message lists in protocolmeta.py
    statuses = []
    for acct in (accts if account in (None, False) else [account]):
        if hasattr(acct, 'protocol_info'):
            proto_statuses = acct.protocol_info().get('statuses', [])
            invis = [StatusMessage.Invisible.title]
            if acct.protocol_info().get('has_invisible', False) and invis not in proto_statuses:
                proto_statuses.append(invis)
        else:
            proto_statuses = []

        for cat_i, cat in enumerate(proto_statuses):
            for status_id, status in enumerate(cat):
                for st in cat:
                    statuses += [(cat_i, status_id, st)]
    statuses.sort()

    statuses = removedupes([(s[0], s[2]) for s in statuses])
    status_strings = [(s[1], _(s[1])) for s in statuses]

    # If the status string itself in our accumulated list of statuses, that means
    # it belongs to another protocol. Search for any protocols which have the
    # status, and append an extra status message to the list.

    if curstatus is not None and curstatus not in [c[0] for c in status_strings]:
        # Accumulate Status -> [Acct1, Acct2, ...]
        from common.protocolmeta import protocols
        status_acct_map = defaultdict(list)
        for k, protocol in protocols.iteritems():
            for cat in protocol.get('statuses', []):
                for st in cat:
                    status_acct_map[st].append(protocol.name)

            if protocol.get('has_invisible', False):
                status_acct_map[StatusMessage.Invisible.title].append(protocol.name)

        # add a string like (MSN/ICQ only) to the status
        accounts = sorted(status_acct_map.get(curstatus, []))

        #Translators: Separator when listing multiple accounts, ex: MSN/ICQ only
        account_types = _('/').join(accounts)
        status_strings.append((curstatus, _('{status} ({account_types} Only)').format(status=curstatus, account_types=account_types)))

    return status_strings or DEFAULT_STATUS_CHOICES
Example #2
0
    def _reconfig_sorter(self, rebuild = True):
        if not hasattr(self, '_rebuild_sorter_count'):
            self._rebuild_sorter_count = 0
        log.debug('rebuilding sorter %d', self._rebuild_sorter_count)
        self._rebuild_sorter_count += 1

        sorts = pref('buddylist.sortby')
        assert isinstance(sorts, basestring)
        sorts = sorts.split()

        search = getattr(self, '_search_by', '')

        s = self.new_sorter
        s.clearSorters()

        show_offline = pref('buddylist.show_offline')
        group_offline = pref('buddylist.group_offline')
        show_mobile = pref('buddylist.show_mobile')
        hide_offline_groups = pref('buddylist.hide_offline_groups')

        if search:
            # search by
            show_offline = True
            s.addSorter(blist.ByGroup(False, 2))
            s.addSorter(blist.BySearch(search, SEARCH_CONTACTS_GROUPNAME))
        else:
            if not sorts[0].startswith('*'):
                s.addSorter(blist.ByFakeRoot(pref('buddylist.fakeroot_name', default=_('Contacts'))))
            s.addSorter(blist.ByGroup(not sorts[0].startswith('*'), 2))


        #until status grouper can do it, always add a mobile filter.
        #mobile needs to happen before the status grouper (otherwise you may see a mobile group for now)
        if not search and not show_mobile:
            s.addSorter(blist.ByMobile(show_mobile))

        # Add any necessary groupers
        added_status_grouper = False
        if not search and sorts[0].startswith('*'):
            show_groups = True
            sorter = sorts[0][1:]
            grouper = self.Groupers.get(sorter)
            if grouper is not None:
                if sorter == 'status':
                    # pass showOffline flag to ByStatus grouper
                    args = (show_offline, )
                    added_status_grouper = True
                else:
                    args = ()

                grouper_obj = getattr(blist, grouper)(show_groups, *args)

                if sorter == 'service':
                    # Set group names on the ByService grouper
                    for service, protocolinfo in protocols.iteritems():
                        grouper_obj.setGroupName(service, protocolinfo['name'])

                s.addSorter(grouper_obj)

        # Comparators
        sorters = [blist.CustomOrder]

        if search:
            # move offline buddies to the bottom when searching, and sort alphabetically
            sorts = ['online', 'name']
        else:
            # If we're grouping offline buddies, or filtering them out,
            # and we didn't add a ByStatus grouper, then we need to add a simpler
            # ByOnline grouper that accomplish the same things.
            # And, btw, we're "always" grouping offline buddies, we need the counts.
            if not added_status_grouper:
                s.addSorter(blist.ByOnline(group_offline, show_offline))

        # Always sort by user order.
        if sorts[-1] != 'none':
            sorts.append('none')

        self.attrs = set(self.base_attrs)
        cmpnames = []
        for sort in sorts:
            if sort.startswith('*'):
                #continue
                sort = sort[1:]
            cmpname = self.Sorters[sort]
            cmpnames.append(cmpname)

            # make sure we rebuild when buddy attributes change that are important
            # to the sorter
            sort_attr = self.BuddySortAttrs.get(sort, None)
            if sort_attr is not None:
                self.attrs.add(sort_attr)

            sorters.append(getattr(blist, cmpname))

        log.debug('comparators are: %s', ', '.join(cmpnames))

        self.comparators = sorters
        s.setComparators(sorters)
        if pref('buddylist.hide_offline_dependant', False, bool):
            s.setPruneEmpty(not pref('buddylist.show_offline') and pref('buddylist.hide_offline_groups'))
        else:
            s.setPruneEmpty(pref('buddylist.hide_offline_groups'))

        if rebuild:
            self.rebuild_threaded()