35c399adca90450aa365f1ac5f828f844142f789
[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         <title>Guacamole ${project.version}</title>
28     </head>
29
30     <body>
31
32         <div id="login-ui" style="display: none">
33             <div id="login-dialog-middle">
34
35                 <div id="login-dialog">
36
37                     <p id="login-error"></p>
38
39                     <form id="login-form" action="#" method="post">
40
41                         <div id="login-fields">
42                             <table>
43                                 <tr>
44                                     <th>Username</th>
45                                     <td><input type="text" name="username" id="username" autofocus="autofocus"/></td>
46                                 </tr>
47                                 <tr>
48                                     <th>Password</th>
49                                     <td><input type="password" name="password" id="password"/></td>
50                                 </tr>
51                             </table>
52
53                             <img class="logo" src="images/guacamole-logo-64.png" alt=""/>
54                         </div>
55
56                         <div id="buttons">
57                             <input type="submit" name="login" id="login" value="Login"/>
58                         </div>
59
60                     </form>
61                 </div>
62
63             </div>
64         </div>
65
66         <!-- Connection list UI -->
67         <div id="connection-list-ui" style="display: none">
68
69             <div id="logout-panel">
70                 <button id="logout">Logout</button>
71             </div>
72             
73             <h1>
74                 <img class="logo" src="images/guacamole-logo-64.png" alt=""/>
75                 Available Connections
76             </h1>
77             
78             <table class="connections">
79                 <thead>
80                     <tr>
81                         <th class="protocol"> </th>
82                         <th class="name">Name</th>
83                     </tr>
84                 </thead>
85                 <tbody id="connections-tbody">
86                 </tbody>
87             </table>
88
89         </div>
90
91         <div id="version-dialog">
92             Guacamole ${project.version}
93         </div>
94
95         <script type="text/javascript" src="scripts/connections.js"></script>
96
97         <!-- Init -->
98         <script type="text/javascript"> /* <![CDATA[ */
99
100             function resetUI() {
101
102                 var configs;
103                 try {
104                     configs = getConfigList();
105                 }
106                 catch (e) {
107
108                     // Show login UI if unable to get configs
109                     loginUI.style.display = "";
110                     connectionListUI.style.display = "none";
111
112                     return;
113
114                 }
115
116                 // If only one connection, redirect to that.
117                 if (configs.length == 1) {
118                     window.location.href = "client.xhtml?" + encodeURIComponent(configs[0].id);
119                     return;
120                 }
121
122                 // Remove all rows from connections list
123                 var tbody = document.getElementById("connections-tbody");
124                 tbody.innerHTML = "";
125                 
126                 // Add one row per connection
127                 for (var i=0; i<configs.length; i++) {
128
129                     // Create row and cells
130                     var tr = document.createElement("tr");
131                     var protocol = document.createElement("td");
132                     var id = document.createElement("td");
133
134                     var protocolIcon = document.createElement("div");
135                     protocolIcon.className = "protocol icon " + configs[i].protocol;
136
137                     // Set CSS
138                     protocol.className = "protocol";
139                     id.className = "name";
140
141                     // Create link to client
142                     var clientLink = document.createElement("a");
143                     clientLink.setAttribute("href",
144                         "client.xhtml?" + encodeURIComponent(configs[i].id));
145
146                     // Set cell contents
147                     protocol.appendChild(protocolIcon);
148                     //protocol.textContent   = configs[i].protocol;
149                     clientLink.textContent = configs[i].id;
150                     id.appendChild(clientLink);
151
152                     // Add cells
153                     tr.appendChild(protocol);
154                     tr.appendChild(id);
155
156                     // Add row
157                     tbody.appendChild(tr);
158
159                 }
160
161                 // If configs could be retrieved, display list
162                 loginUI.style.display = "none";
163                 connectionListUI.style.display = "";
164
165             }
166
167             var loginForm = document.getElementById("login-form");
168             var loginUI = document.getElementById("login-ui");
169             var connectionListUI = document.getElementById("connection-list-ui");
170             var logout = document.getElementById("logout");
171
172             logout.onclick = function() {
173                 window.location.href = "logout";
174             };
175
176             // TODO: Get connection list
177             // On no-auth fail, show login UI 
178
179             loginForm.onsubmit = function() {
180
181                 var username = document.getElementById("username");
182                 var password = document.getElementById("password");
183
184                 var data =
185                        "username=" + encodeURIComponent(username.value)
186                     + "&password=" + encodeURIComponent(password.value)
187
188                 try {
189
190                     // Log in
191                     var xhr = new XMLHttpRequest();
192                     xhr.open("POST", "login", false);
193                     xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
194                     xhr.send(data);
195
196                     // Handle failures
197                     if (xhr.status != 200)
198                         throw new Error("Invalid login");
199
200                     resetUI();
201
202                 }
203                 catch (e) {
204
205                     var loginError = document.getElementById("login-error");
206
207                     // Display error, reset and refocus password field
208                     loginError.textContent = e.message;
209                     password.value = "";
210                     password.focus();
211
212                     return false;
213
214                 }
215
216                 // On success, hide loginUI, get and show connection list.
217                 return false;
218
219             }
220
221             resetUI();
222
223             /* ]]> */ </script>
224
225     </body>
226
227 </html>