~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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# 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
from ivle.webapp.publisher import NoPath
from ivle.webapp.breadcrumbs import Breadcrumber

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'
    allow_overlays = True
    breadcrumb_text = None

    def __init__(self, *args, **kwargs):
        super(XHTMLView, self).__init__(*args, **kwargs)

        self.overlay_blacklist = []

        self.plugin_scripts = {}
        self.plugin_styles = {}
        self.scripts_init = []

        self.extra_breadcrumbs = []
        self.overlay_blacklist = []

    def get_context_ancestry(self, req):
        return req.publisher.get_ancestors(self.context)

    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)

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

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

        # Global template
        ctx = genshi.template.Context()

        overlay_bits = self.render_overlays(req) if req.user else [[]]*4
        ctx['overlays'] = overlay_bits[0]

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

        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'] += view_scripts
        ctx['scripts'] += overlay_bits[2]

        ctx['scripts_init'] = self.scripts_init + overlay_bits[3]
        ctx['app_template'] = app
        ctx['title_img'] = media_url(req, CorePlugin,
                                     "images/chrome/root-breadcrumb.png")
        try:
            ancestry = self.get_context_ancestry(req)
        except NoPath:
            ancestry = []

        crumber = Breadcrumber(req)

        ctx['breadcrumbs'] = []
        for ancestor in ancestry:
            crumb = crumber.crumb(ancestor)
            if crumb is None:
                continue

            if hasattr(crumb, 'extra_breadcrumbs_before'):
                ctx['breadcrumbs'].extend(crumb.extra_breadcrumbs_before)
            ctx['breadcrumbs'].append(crumb)
            if hasattr(crumb, 'extra_breadcrumbs_after'):
                ctx['breadcrumbs'].extend(crumb.extra_breadcrumbs_after)

        # If the view has specified text for a breadcrumb, add one.
        if self.breadcrumb_text:
            ctx['breadcrumbs'].append(ViewBreadcrumb(req, self))

        # Allow the view to add its own fake breadcrumbs.
        ctx['breadcrumbs'].extend(self.extra_breadcrumbs)

        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'] = req.config['urls']['root']
        ctx['public_host'] = req.config['urls']['public_host']
        ctx['svn_base'] = req.config['urls']['svn_addr']
        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, weight, admin
                # (Admin is optional, defaults to false)
                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
                # The following check is here, so it is AFTER setting the
                # icon, but BEFORE actually installing the tab in the menu
                if len(tab) > 6 and tab[6]:
                    # Admin-only tab
                    if not (req.user and req.user.admin):
                        break
                new_app['path'] = req.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 = []
        styles = []
        scripts = []
        scripts_init = []
        if not self.allow_overlays:
            return (overlays, styles, scripts, scripts_init)

        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]:
                        scripts.append(media_url(req, mplugin, path))

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

                scripts_init += overlay.plugin_scripts_init

                overlays.append(overlay.render(req))
        return (overlays, styles, scripts, scripts_init)

    @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, context, lastobj):
        super(XHTMLErrorView, self).__init__(req, context)
        self.lastobj = lastobj

    def get_context_ancestry(self, req):
        return req.publisher.get_ancestors(self.lastobj)

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

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

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

        if not req.publicmode and 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

class ViewBreadcrumb(object):
    def __init__(self, req, context):
        self.req = req
        self.context = context

    @property
    def text(self):
        return self.context.breadcrumb_text