~azzar1/unity/add-show-desktop-key

1099.1.34 by William Grant
Split up ivle.webapp.base.views into ivle.webapp.base.{rest,xhtml}, as it was
1
# IVLE - Informatics Virtual Learning Environment
2
# Copyright (C) 2007-2009 The University of Melbourne
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18
# Author: Nick Chadwick
19
20
import inspect
21
import os.path
22
23
import genshi.template
24
1099.1.60 by William Grant
Add support in XHTMLView for plugin styles and scripts.
25
from ivle.webapp.media import media_url
1099.1.34 by William Grant
Split up ivle.webapp.base.views into ivle.webapp.base.{rest,xhtml}, as it was
26
from ivle.webapp.base.views import BaseView
1099.1.74 by Nick Chadwick
Added overlay system and console overlay. Note that the console overlay
27
from ivle.webapp.base.plugins import OverlayPlugin
1099.1.34 by William Grant
Split up ivle.webapp.base.views into ivle.webapp.base.{rest,xhtml}, as it was
28
import ivle.conf
29
import ivle.util
30
31
class XHTMLView(BaseView):
32
    """
33
    A view which provides a base class for views which need to return XHTML
34
    It is expected that apps which use this view will be written using Genshi
35
    templates.
36
    """
1099.1.60 by William Grant
Add support in XHTMLView for plugin styles and scripts.
37
38
    template = 'template.html'
39
    plugin_scripts = {}
40
    plugin_styles = {}
1099.1.74 by Nick Chadwick
Added overlay system and console overlay. Note that the console overlay
41
    overlay_blacklist = []
1099.1.60 by William Grant
Add support in XHTMLView for plugin styles and scripts.
42
1099.1.34 by William Grant
Split up ivle.webapp.base.views into ivle.webapp.base.{rest,xhtml}, as it was
43
    def __init__(self, req, **kwargs):
44
        for key in kwargs:
1099.1.46 by Nick Chadwick
Fixed a slight issue in the indentation of xhtml.py
45
            setattr(self, key, kwargs[key])
1099.1.38 by William Grant
ivle.webapp.base.xhtml.XHTMLView: Split the global and view contexts.
46
1099.1.34 by William Grant
Split up ivle.webapp.base.views into ivle.webapp.base.{rest,xhtml}, as it was
47
    def render(self, req):
48
        req.content_type = 'text/html' # TODO: Detect application/xhtml+xml
1099.1.38 by William Grant
ivle.webapp.base.xhtml.XHTMLView: Split the global and view contexts.
49
50
        # View template
51
        viewctx = genshi.template.Context()
52
        self.populate(req, viewctx)
53
54
        # The template is found in the directory of the module containing the
55
        # view.
1099.1.34 by William Grant
Split up ivle.webapp.base.views into ivle.webapp.base.{rest,xhtml}, as it was
56
        app_template = os.path.join(os.path.dirname(
1099.1.35 by William Grant
ivle.webapp.base.xhtml#XHTMLView: Rename app_template to template (the things
57
                        inspect.getmodule(self).__file__), self.template) 
1099.1.34 by William Grant
Split up ivle.webapp.base.views into ivle.webapp.base.{rest,xhtml}, as it was
58
        req.write_html_head_foot = False
59
        loader = genshi.template.TemplateLoader(".", auto_reload=True)
60
        tmpl = loader.load(app_template)
1099.1.38 by William Grant
ivle.webapp.base.xhtml.XHTMLView: Split the global and view contexts.
61
        app = tmpl.generate(viewctx)
62
1099.1.60 by William Grant
Add support in XHTMLView for plugin styles and scripts.
63
        for plugin in self.plugin_scripts:
64
            for path in self.plugin_scripts[plugin]:
65
                req.scripts.append(media_url(req, plugin, path))
66
67
        for plugin in self.plugin_styles:
68
            for path in self.plugin_styles[plugin]:
69
                req.styles.append(media_url(req, plugin, path))
70
1099.1.38 by William Grant
ivle.webapp.base.xhtml.XHTMLView: Split the global and view contexts.
71
        # Global template
72
        ctx = genshi.template.Context()
1099.1.74 by Nick Chadwick
Added overlay system and console overlay. Note that the console overlay
73
        # XXX: Leave this here!! (Before req.styles is read)
74
        ctx['overlays'] = self.render_overlays(req)
1099.1.38 by William Grant
ivle.webapp.base.xhtml.XHTMLView: Split the global and view contexts.
75
        ctx['app_styles'] = req.styles
76
        ctx['scripts'] = req.scripts
77
        ctx['scripts_init'] = req.scripts_init
1099.1.34 by William Grant
Split up ivle.webapp.base.views into ivle.webapp.base.{rest,xhtml}, as it was
78
        ctx['app_template'] = app
1099.1.38 by William Grant
ivle.webapp.base.xhtml.XHTMLView: Split the global and view contexts.
79
        self.populate_headings(req, ctx)
1099.1.34 by William Grant
Split up ivle.webapp.base.views into ivle.webapp.base.{rest,xhtml}, as it was
80
        tmpl = loader.load(os.path.join(os.path.dirname(__file__), 
81
                                                        'ivle-headings.html'))
82
        req.write(tmpl.generate(ctx).render('xhtml', doctype='xhtml'))
1099.1.74 by Nick Chadwick
Added overlay system and console overlay. Note that the console overlay
83
        
84
    def populate(self, req, ctx):
85
        raise NotImplementedError()
1099.1.34 by William Grant
Split up ivle.webapp.base.views into ivle.webapp.base.{rest,xhtml}, as it was
86
87
    def populate_headings(self, req, ctx):
88
        ctx['favicon'] = None
89
        ctx['root_dir'] = ivle.conf.root_dir
90
        ctx['public_host'] = ivle.conf.public_host
91
        ctx['write_javascript_settings'] = req.write_javascript_settings
92
        if req.user:
93
            ctx['login'] = req.user.login
94
            ctx['logged_in'] = True
95
            ctx['nick'] = req.user.nick
96
        else:
97
            ctx['login'] = None
98
        ctx['publicmode'] = req.publicmode
99
        ctx['apps_in_tabs'] = []
100
        for urlname in ivle.conf.apps.apps_in_tabs:
101
            new_app = {}
102
            app = ivle.conf.apps.app_url[urlname]
1099.1.35 by William Grant
ivle.webapp.base.xhtml#XHTMLView: Rename app_template to template (the things
103
            new_app['this_app'] = hasattr(self, 'appname') \
104
                                  and urlname == self.appname
1099.1.34 by William Grant
Split up ivle.webapp.base.views into ivle.webapp.base.{rest,xhtml}, as it was
105
            if app.icon:
106
                new_app['has_icon'] = True
107
                icon_dir = ivle.conf.apps.app_icon_dir
108
                icon_url = ivle.util.make_path(os.path.join(icon_dir, app.icon))
109
                new_app['icon_url'] = icon_url
110
                if new_app['this_app']:
111
                    ctx['favicon'] = icon_url
112
            else:
113
                new_app['has_icon'] = False
114
            new_app['path'] = ivle.util.make_path(urlname)
115
            new_app['desc'] = app.desc
116
            new_app['name'] = app.name
117
            ctx['apps_in_tabs'].append(new_app)
1099.1.74 by Nick Chadwick
Added overlay system and console overlay. Note that the console overlay
118
            
119
    def render_overlays(self, req):
120
        """Generate XML streams for the overlays.
121
        
122
        Returns a list of streams. Populates the scripts, styles, and 
123
        scripts_init.
124
        """
125
        overlays = []
126
        for plugin in req.plugin_index[OverlayPlugin]:
127
            for overclass in plugin.overlays:
128
                if overclass in self.overlay_blacklist:
129
                    continue
130
                overlay = overclass(req)
131
                #TODO: Re-factor this to look nicer
132
                for mplugin in overlay.plugin_scripts:
133
                    for path in overlay.plugin_scripts[mplugin]:
134
                        req.scripts.append(media_url(req, mplugin, path))
135
136
                for mplugin in overlay.plugin_styles:
137
                    for path in overlay.plugin_styles[mplugin]:
138
                        req.styles.append(media_url(req, mplugin, path))
139
                
140
                req.scripts_init += overlay.plugin_scripts_init
141
                
142
                overlays.append(overlay.render(req))
143
        return overlays