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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# IVLE - Informatics Virtual Learning Environment
# Copyright (C) 2007-2009 The University of Melbourne
#
# 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.
#
# 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.
#
# 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

# Author: Nick Chadwick

import inspect
import os.path

import genshi.template

from ivle.webapp.media import media_url
from ivle.webapp.base.views import BaseView
import ivle.conf
import ivle.util

class XHTMLView(BaseView):
    """
    A view which provides a base class for views which need to return XHTML
    It is expected that apps which use this view will be written using Genshi
    templates.
    """

    template = 'template.html'
    plugin_scripts = {}
    plugin_styles = {}

    def __init__(self, req, **kwargs):
        for key in kwargs:
            setattr(self, key, kwargs[key])

    def render(self, req):
        req.content_type = 'text/html' # TODO: Detect application/xhtml+xml

        # View template
        viewctx = genshi.template.Context()
        self.populate(req, viewctx)

        # The template is found in the directory of the module containing the
        # view.
        app_template = os.path.join(os.path.dirname(
                        inspect.getmodule(self).__file__), self.template) 
        req.write_html_head_foot = False
        loader = genshi.template.TemplateLoader(".", auto_reload=True)
        tmpl = loader.load(app_template)
        app = tmpl.generate(viewctx)

        for plugin in self.plugin_scripts:
            for path in self.plugin_scripts[plugin]:
                req.scripts.append(media_url(req, plugin, path))

        for plugin in self.plugin_styles:
            for path in self.plugin_styles[plugin]:
                req.styles.append(media_url(req, plugin, path))

        # Global template
        ctx = genshi.template.Context()
        ctx['app_styles'] = req.styles
        ctx['scripts'] = req.scripts
        ctx['scripts_init'] = req.scripts_init
        ctx['app_template'] = app
        self.populate_headings(req, ctx)
        tmpl = loader.load(os.path.join(os.path.dirname(__file__), 
                                                        'ivle-headings.html'))
        req.write(tmpl.generate(ctx).render('xhtml', doctype='xhtml'))

    def populate_headings(self, req, ctx):
        ctx['favicon'] = None
        ctx['root_dir'] = ivle.conf.root_dir
        ctx['public_host'] = ivle.conf.public_host
        ctx['write_javascript_settings'] = req.write_javascript_settings
        if req.user:
            ctx['login'] = req.user.login
            ctx['logged_in'] = True
            ctx['nick'] = req.user.nick
        else:
            ctx['login'] = None
        ctx['publicmode'] = req.publicmode
        ctx['apps_in_tabs'] = []
        for urlname in ivle.conf.apps.apps_in_tabs:
            new_app = {}
            app = ivle.conf.apps.app_url[urlname]
            new_app['this_app'] = hasattr(self, 'appname') \
                                  and urlname == self.appname
            if app.icon:
                new_app['has_icon'] = True
                icon_dir = ivle.conf.apps.app_icon_dir
                icon_url = ivle.util.make_path(os.path.join(icon_dir, app.icon))
                new_app['icon_url'] = icon_url
                if new_app['this_app']:
                    ctx['favicon'] = icon_url
            else:
                new_app['has_icon'] = False
            new_app['path'] = ivle.util.make_path(urlname)
            new_app['desc'] = app.desc
            new_app['name'] = app.name
            ctx['apps_in_tabs'].append(new_app)