~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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# 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 urllib

import genshi.template

from ivle.webapp.media import media_url
from ivle.webapp.core import Plugin as CorePlugin
from ivle.webapp.base.views import BaseView
from ivle.webapp.base.plugins import ViewPlugin, OverlayPlugin
from ivle.webapp.errors import HTTPError, Unauthorized
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 = {}
    allow_overlays = True
    overlay_blacklist = []

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

    def filter(self, stream, ctx):
        return stream

    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) 
        loader = genshi.template.TemplateLoader(".", auto_reload=True)
        tmpl = loader.load(app_template)
        app = self.filter(tmpl.generate(viewctx), 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()
        # XXX: Leave this here!! (Before req.styles is read)
        ctx['overlays'] = self.render_overlays(req) if req.user else []

        ctx['styles'] = [media_url(req, CorePlugin, 'ivle.css')]
        ctx['styles'] += req.styles

        ctx['scripts'] = [media_url(req, CorePlugin, path) for path in
                           ('util.js', 'json2.js', 'md5.js')]
        ctx['scripts'].append(media_url(req, '+external/jquery', 'jquery.js'))
        ctx['scripts'] += req.scripts

        ctx['scripts_init'] = req.scripts_init
        ctx['app_template'] = app
        ctx['title_img'] = media_url(req, CorePlugin,
                                     "images/chrome/title.png")
        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(self, req, ctx):
        raise NotImplementedError()

    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['logged_in'] = False
        ctx['publicmode'] = req.publicmode
        if hasattr(self, 'help'):
            ctx['help_path'] = self.help

        ctx['apps_in_tabs'] = []
        for plugin in req.config.plugin_index[ViewPlugin]:
            if not hasattr(plugin, 'tabs'):
                continue

            for tab in plugin.tabs:
                # tab is a tuple: name, title, desc, icon, path
                new_app = {}
                new_app['this_app'] = hasattr(self, 'tab') \
                                      and tab[0] == self.tab

                # Icon name
                if tab[3] is not None:
                    new_app['has_icon'] = True
                    icon_url = media_url(req, plugin, tab[3])
                    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(tab[4])
                new_app['desc'] = tab[2]
                new_app['name'] = tab[1]
                new_app['weight'] = tab[5]
                ctx['apps_in_tabs'].append(new_app)

        ctx['apps_in_tabs'].sort(key=lambda tab: tab['weight'])

    def render_overlays(self, req):
        """Generate XML streams for the overlays.
        
        Returns a list of streams. Populates the scripts, styles, and 
        scripts_init.
        """
        overlays = []
        if not self.allow_overlays:
            return overlays

        for plugin in req.config.plugin_index[OverlayPlugin]:
            for overclass in plugin.overlays:
                if overclass in self.overlay_blacklist:
                    continue
                overlay = overclass(req)
                #TODO: Re-factor this to look nicer
                for mplugin in overlay.plugin_scripts:
                    for path in overlay.plugin_scripts[mplugin]:
                        req.scripts.append(media_url(req, mplugin, path))

                for mplugin in overlay.plugin_styles:
                    for path in overlay.plugin_styles[mplugin]:
                        req.styles.append(media_url(req, mplugin, path))

                req.scripts_init += overlay.plugin_scripts_init

                overlays.append(overlay.render(req))
        return overlays

    @classmethod
    def get_error_view(cls, e):
        view_map = {HTTPError:    XHTMLErrorView,
                    Unauthorized: XHTMLUnauthorizedView}
        for exccls in inspect.getmro(type(e)):
            if exccls in view_map:
                return view_map[exccls]

class XHTMLErrorView(XHTMLView):
    template = 'xhtmlerror.html'

    def __init__(self, req, exception):
        self.context = exception

    def populate(self, req, ctx):
        ctx['exception'] = self.context

class XHTMLUnauthorizedView(XHTMLErrorView):
    template = 'xhtmlunauthorized.html'

    def __init__(self, req, exception):
        super(XHTMLUnauthorizedView, self).__init__(req, exception)

        if req.user is None:
            # Not logged in. Redirect to login page.
            if req.uri == '/':
                query_string = ''
            else:
                query_string = '?url=' + urllib.quote(req.uri, safe="/~")
            req.throw_redirect('/+login' + query_string)

        req.status = 403