'Retrieve a list of available XForms
@return: a list of tuples (url, title) of available XForms'

@staticmethod
def formlist():

    resources = current.deployment_settings.get_xforms_resources()
    xforms = []
    if resources:
        s3db = current.s3db
        for item in resources:
            options = {}
            if isinstance(item, (tuple, list)):
                if (len(item) == 2):
                    (title, tablename) = item
                    if isinstance(tablename, dict):
                        (tablename, options) = (title, tablename)
                        title = None
                elif (len(item) == 3):
                    (title, tablename, options) = item
                else:
                    continue
            else:
                (title, tablename) = (None, item)
            try:
                resource = s3db.resource(tablename)
            except AttributeError:
                current.log.warning(('XForms:    non-existent    resource    %s' % tablename))
                continue
            if (title is None):
                title = '    '.join((w.capitalize() for w in resource.name.split('_')))
            c = options.get('c', 'xforms')
            f = options.get('f', 'forms')
            url_vars = options.get('vars', {})
            config = resource.get_config('xform')
            if config:
                if isinstance(config, dict):
                    collection = config.get('collection')
                    if (not collection):
                        continue
                    table = resource.table
                    DELETED = current.xml.DELETED
                    if (DELETED in table):
                        query = (table[DELETED] != True)
                    else:
                        query = (table.id > 0)
                    public = options.get('check', 'public')
                    if (public in table):
                        query &= (table[public] == True)
                    title_field = options.get('title', 'name')
                    rows = current.db(query).select(table._id, table[title_field])
                    native = (c == 'xforms')
                    for row in rows:
                        if native:
                            args = [tablename, row[table._id]]
                        else:
                            args = [row[table._id], 'xform.xhtml']
                        url = URL(c=c, f=f, args=args, vars=url_vars, host=True, extension='')
                        xforms.append((url, row[title_field]))
                else:
                    continue
            else:
                if (c == 'xforms'):
                    args = [tablename]
                else:
                    args = ['xform.xhtml']
                url = URL(c=c, f=f, args=args, vars=url_vars, host=True, extension='')
                xforms.append((url, title))
    return xforms
