Fix URLs in connection list, add getClientURL() function for constructing the URL...
[guacamole.git] / src / main / webapp / index.xhtml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE html>
3
4 <!--
5     Guacamole - Clientless Remote Desktop
6     Copyright (C) 2010  Michael Jumper
7
8     This program is free software: you can redistribute it and/or modify
9     it under the terms of the GNU Affero General Public License as published by
10     the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU Affero General Public License for more details.
17
18     You should have received a copy of the GNU Affero General Public License
19     along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 -->
21
22 <html xmlns="http://www.w3.org/1999/xhtml">
23
24     <head>
25         <link rel="icon" type="image/png" href="images/guacamole-logo-64.png"/>
26         <link rel="stylesheet" type="text/css" href="styles/login.css"/>
27         <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no, target-densitydpi=device-dpi"/>
28         <title>Guacamole ${project.version}</title>
29     </head>
30
31     <body>
32
33         <div id="login-ui" style="display: none">
34             <div id="login-dialog-middle">
35
36                 <div id="login-dialog">
37
38                     <p id="login-error"></p>
39
40                     <form id="login-form" action="#" method="post">
41
42                         <div id="login-fields">
43                             <table>
44                                 <tr>
45                                     <th>Username</th>
46                                     <td><input type="text" name="username" id="username" autofocus="autofocus"/></td>
47                                 </tr>
48                                 <tr>
49                                     <th>Password</th>
50                                     <td><input type="password" name="password" id="password"/></td>
51                                 </tr>
52                             </table>
53
54                             <img class="logo" src="images/guacamole-logo-64.png" alt=""/>
55                         </div>
56
57                         <div id="buttons">
58                             <input type="submit" name="login" id="login" value="Login"/>
59                         </div>
60
61                     </form>
62                 </div>
63
64             </div>
65         </div>
66
67         <!-- Connection list UI -->
68         <div id="connection-list-ui" style="display: none">
69
70             <div id="logout-panel">
71                 <button id="logout">Logout</button>
72             </div>
73             
74             <h1>
75                 <img class="logo" src="images/guacamole-logo-64.png" alt=""/>
76                 Available Connections
77             </h1>
78             
79             <table class="connections">
80                 <thead>
81                     <tr>
82                         <th class="protocol"> </th>
83                         <th class="name">Name</th>
84                     </tr>
85                 </thead>
86                 <tbody id="connections-tbody">
87                 </tbody>
88             </table>
89
90         </div>
91
92         <div id="version-dialog">
93             Guacamole ${project.version}
94         </div>
95
96         <script type="text/javascript" src="scripts/connections.js"></script>
97
98         <!-- Init -->
99         <script type="text/javascript"> /* <![CDATA[ */
100
101             // Constructs the URL for a client which connects to the connection
102             // with the given id.
103             function getClientURL(id) {
104                 
105                 // Get parameters from query string
106                 var parameters = window.location.search.substring(1);
107
108                 // Construct URL for client, including any additional
109                 // parameters from the query string
110                 var client_url = "client.xhtml?id=" + encodeURIComponent(id);
111                 if (parameters) client_url += "&" + parameters;
112
113                 return client_url;
114                 
115             }
116
117             // Resets the interface such that the login UI is displayed if
118             // the user is not authenticated (or authentication fails) and
119             // the connection list UI (or the client for the only available
120             // connection, if there is only one) is displayed if the user is
121             // authenticated.
122             function resetUI() {
123
124                 var configs;
125                 try {
126                     configs = getConfigList();
127                 }
128                 catch (e) {
129
130                     // Show login UI if unable to get configs
131                     loginUI.style.display = "";
132                     connectionListUI.style.display = "none";
133
134                     return;
135
136                 }
137
138                 // If only one connection, redirect to that.
139                 if (configs.length == 1) {
140                     window.location.href = getClientURL(configs[0].id);
141                     return;
142                 }
143
144                 // Remove all rows from connections list
145                 var tbody = document.getElementById("connections-tbody");
146                 tbody.innerHTML = "";
147                 
148                 // Add one row per connection
149                 for (var i=0; i<configs.length; i++) {
150
151                     // Create row and cells
152                     var tr = document.createElement("tr");
153                     var protocol = document.createElement("td");
154                     var id = document.createElement("td");
155
156                     var protocolIcon = document.createElement("div");
157                     protocolIcon.className = "protocol icon " + configs[i].protocol;
158
159                     // Set CSS
160                     protocol.className = "protocol";
161                     id.className = "name";
162
163                     // Create link to client
164                     var clientLink = document.createElement("a");
165                     clientLink.setAttribute("href", getClientURL(configs[i].id));
166
167                     // Set cell contents
168                     protocol.appendChild(protocolIcon);
169                     //protocol.textContent   = configs[i].protocol;
170                     clientLink.textContent = configs[i].id;
171                     id.appendChild(clientLink);
172
173                     // Add cells
174                     tr.appendChild(protocol);
175                     tr.appendChild(id);
176
177                     // Add row
178                     tbody.appendChild(tr);
179
180                 }
181
182                 // If configs could be retrieved, display list
183                 loginUI.style.display = "none";
184                 connectionListUI.style.display = "";
185
186             }
187
188             var loginForm = document.getElementById("login-form");
189             var loginUI = document.getElementById("login-ui");
190             var connectionListUI = document.getElementById("connection-list-ui");
191             var logout = document.getElementById("logout");
192
193             logout.onclick = function() {
194                 window.location.href = "logout";
195             };
196
197             loginForm.onsubmit = function() {
198
199                 var username = document.getElementById("username");
200                 var password = document.getElementById("password");
201
202                 var data =
203                        "username=" + encodeURIComponent(username.value)
204                     + "&password=" + encodeURIComponent(password.value)
205
206                 try {
207
208                     // Log in
209                     var xhr = new XMLHttpRequest();
210                     xhr.open("POST", "login", false);
211                     xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
212                     xhr.send(data);
213
214                     // Handle failures
215                     if (xhr.status != 200)
216                         throw new Error("Invalid login");
217
218                     resetUI();
219
220                 }
221                 catch (e) {
222
223                     var loginError = document.getElementById("login-error");
224
225                     // Display error, reset and refocus password field
226                     loginError.textContent = e.message;
227                     password.value = "";
228                     password.focus();
229
230                     return false;
231
232                 }
233
234                 // On success, hide loginUI, get and show connection list.
235                 return false;
236
237             }
238
239             resetUI();
240
241             /* ]]> */ </script>
242
243     </body>
244
245 </html>