~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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/* IVLE - Informatics Virtual Learning Environment
 * Copyright (C) 2007-2008 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
 *
 * Module: Settings (Client-side JavaScript)
 * Author: Matt Giuca
 * Date: 25/2/2008
 */

var user_data;

/* Fetch the user's details from the server, and populate the page.
 * Returns false. */
function revert_settings()
{
    var callback = function(xhr)
        {
            user = JSON.parse(xhr.responseText);
            populate(user);
        }
    /* Just get details for the logged in user */
    ajax_call(callback, "userservice", "get_user", {}, "GET");
    return false;
}

/* Populate the page with the given user's account details */
function populate(user)
{
    user_data = user;
    /* Plain text elements (non-editable) */
    var login = document.getElementById("login");
    var role = document.getElementById("role");
    var changepassword = document.getElementById("changepassword");
    var notices = document.getElementById("notices");
    /* Textbox (input) elements */
    var nick = document.getElementById("nick");
    var email = document.getElementById("email");

    var text;
    var p;
    var b;
    var table;
    var tbody;
    var tr;
    var td;
    var inputbox;

    /* Clear things */
    dom_removechildren(login);
    dom_removechildren(role);
    dom_removechildren(changepassword);
    dom_removechildren(notices);

    /* Construct the page */

    /* "login" : Full Name (<b>login</b> / studentid) */
    text = user.fullname + " (";
    login.appendChild(document.createTextNode(text));
    text = user.login
    b = document.createElement("b");
    b.appendChild(document.createTextNode(text));
    login.appendChild(b);
    if (user.studentid != null)
        text = " / " + user.studentid + ")"
    else
        text = ")"
    login.appendChild(document.createTextNode(text));

    /* "role" : <p>Your privilege level is <b>rolenm</b>.</p>
     * Unless rolenm is "student"
     */
    if (user.rolenm != "student")
    {
        text = "Your privilege level is ";
        role.appendChild(document.createTextNode(text));
        b = document.createElement("b");
        text = user.rolenm;
        b.appendChild(document.createTextNode(text));
        role.appendChild(b);
        text = ".";
        role.appendChild(document.createTextNode(text));
    }

    /* "nick" and "email" boxes */
    nick.value = user.nick;
    email.value = user.email;

    /* Password change box */
    /* (Only if this user has a local password) */
    if (user.local_password)
    {
        p = document.createElement("h3");
        p.appendChild(document.createTextNode("Change password"))
        changepassword.appendChild(p);
        p = document.createElement("p");
        
        p.appendChild(document.createTextNode("Please type your old password, "
                + "and new password twice, for verification."));
        changepassword.appendChild(p);

        table = document.createElement("table");
        tbody = document.createElement("tbody");

        tr = document.createElement("tr");
        td = document.createElement("td");
        td.appendChild(document.createTextNode("Old password:"))
        tr.appendChild(td);
        td = document.createElement("td");
        inputbox = document.createElement("input");
        inputbox.setAttribute("type", "password");
        inputbox.setAttribute("name", "oldpass");
        inputbox.setAttribute("id", "oldpass");
        inputbox.setAttribute("size", "40");
        td.appendChild(inputbox)
        tr.appendChild(td);
        tbody.appendChild(tr);

        tr = document.createElement("tr");
        td = document.createElement("td");
        td.appendChild(document.createTextNode("New password:"))
        tr.appendChild(td);
        td = document.createElement("td");
        inputbox = document.createElement("input");
        inputbox.setAttribute("type", "password");
        inputbox.setAttribute("name", "newpass");
        inputbox.setAttribute("id", "newpass");
        inputbox.setAttribute("size", "40");
        td.appendChild(inputbox)
        tr.appendChild(td);
        tbody.appendChild(tr);

        tr = document.createElement("tr");
        td = document.createElement("td");
        td.appendChild(document.createTextNode("Retype password:"))
        tr.appendChild(td);
        td = document.createElement("td");
        inputbox = document.createElement("input");
        inputbox.setAttribute("type", "password");
        inputbox.setAttribute("name", "repeatpass");
        inputbox.setAttribute("id", "repeatpass");
        inputbox.setAttribute("size", "40");
        td.appendChild(inputbox)
        tr.appendChild(td);
        tbody.appendChild(tr);

        table.appendChild(tbody);
        changepassword.appendChild(table);
    }

    if (user.pass_exp != null || user.acct_exp != null)
    {
        p = document.createElement("h3");
        text = "Notices";
        p.appendChild(document.createTextNode(text));
        notices.appendChild(p);
        if (user.pass_exp != null)
        {
            p = document.createElement("p");
            /* TODO: Nice passexp */
            var pass_exp = user.pass_exp.toString()
            text = "Your password will expire on " + pass_exp
                + ". You should change it before then to avoid having your "
                + "account disabled.";
            p.appendChild(document.createTextNode(text));
            notices.appendChild(p);
        }
        if (user.acct_exp != null)
        {
            p = document.createElement("p");
            /* TODO: Nice acct_exp */
            var acct_exp = user.acct_exp.toString()
            text = "Your IVLE account will expire on " + acct_exp + ".";
            p.appendChild(document.createTextNode(text));
            notices.appendChild(p);
        }
    }
}

/* Sets the "result" text.
 * iserror (bool) determines the styling.
 */
function set_result(text, iserror)
{
    var p = document.getElementById("result");
    dom_removechildren(p);
    p.appendChild(document.createTextNode(text));
    if (iserror)
        p.setAttribute("class", "error");
    else
        p.removeAttribute("class");
}

/* Writes the settings to the server.
 * Returns false. */
function save_settings()
{
    /* Button (input) elements */
    var save = document.getElementById("save");
    /* Textbox (input) elements */
    try
    {
        var oldpass = document.getElementById("oldpass");
        var newpass = document.getElementById("newpass");
        var repeatpass = document.getElementById("repeatpass");
    }
    catch (e)
    {
        var newpass = null;
        var repeatpass = null;
    }
    var nick = document.getElementById("nick");
    var email = document.getElementById("email");

    /* Check */
    oldpassval = oldpass == null ? null : oldpass.value;
    newpassval = newpass == null ? null : newpass.value;
    repeatpassval = repeatpass == null ? null : repeatpass.value;
    nickval = nick.value;
    emailval = email.value;

    /* Clear the password boxes, even if there are errors later */
    try
    {
        newpass.value = "";
        repeatpass.value = "";
    }
    catch (e)
    {
    }

    if (nickval == "")
    {
        set_result("Display name is empty.", true);
        return false;
    }
    if (newpassval != repeatpassval)
    {
        set_result("Passwords do not match.", true);
        return false;
    }

    /* Disable the heavy-duty supercolliding super button */
    save.setAttribute("disabled", "disabled");
    save.setAttribute("value", "Saving...");
    var callback = function(xhr)
    {
        save.removeAttribute("disabled");
        save.setAttribute("value", "Save");

        if (xhr.status == 200)
        {
            set_result("Successfully updated details.");
            user_data.nick = nickval;
            user_data.email = emailval;
            /* Now a little hack - update the user's nick display
             * in the heading bar, so they are sure it has been changed.
             */
            var usernick = document.getElementById("usernick");
            dom_removechildren(usernick);
            usernick.appendChild(document.createTextNode(nickval));
        }
        else if (xhr.getResponseHeader("X-IVLE-Action-Error"))
        {
            set_result(decodeURIComponent(xhr.getResponseHeader(
                                     "X-IVLE-Action-Error").toString()), true);        
        }
        else
        {
            set_result("There was a problem updating the details."
                + " Your changes have not been saved.", true);
        }
    }
    data = {
        "login": user_data.login,
        "nick": nickval,
        "email": emailval,
        "oldpass": oldpassval,
    }
    if (newpassval != null && newpassval != "")
        data['password'] = newpassval;
    ajax_call(callback, "userservice", "update_user", data, "POST");
    return false;
}