import os

from quixote import get_session, get_session_manager, get_publisher, get_request, get_response
from quixote.directory import Directory, AccessControlled

import hosts
#import users
import settings
import logger

from menu import html_top

from larpe import errors

def gpl [html] ():
    """<p>This program 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.</p>

    <p>This program 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.</p>

    <p>You should have received a copy of the GNU General Public License along with
    this program; if not, write to the Free Software Foundation, Inc., 59 Temple
    Place - Suite 330, Boston, MA  02111-1307, USA.</p>
    """


class RootDirectory(AccessControlled, Directory):
    _q_exports = ['', 'hosts', 'settings', 'logger']

    hosts = hosts.HostsDirectory()
#    roles = roles.RolesDirectory()
#    users = users.UsersDirectory()
    settings = settings.SettingsDirectory()
    logger = logger.LoggerDirectory()

    def _q_access(self):
        return
        # Ignore access checks at the moment
        # FIXME : implement a user management for admin access
        
        get_response().breadcrumb.append( ('admin/', _('Administration')) )

        if os.path.exists(os.path.join(get_publisher().app_dir, 'ADMIN_FOR_ALL')):
            return
        session = get_session()

        req = get_request()
        if req.user:
            if users.User.count() == 0:
                # this means user logged in anonymously
                pass
            elif not req.user.is_admin:
                raise errors.AccessForbiddenError()
        else:
            if users.User.count() > 0:
                raise errors.AccessUnauthorizedError()

        return


    def _q_index [html] (self):
        html_top('/')
        gpl()
