-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathOswClient.java
395 lines (313 loc) · 9.91 KB
/
OswClient.java
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
* Copyright 2010 Vodafone Group Services Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.onesocialweb.gwt.client;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.onesocialweb.gwt.client.handler.LoginHandler;
import org.onesocialweb.gwt.client.i18n.UserInterfaceText;
import org.onesocialweb.gwt.client.ui.application.AbstractApplication;
import org.onesocialweb.gwt.client.ui.application.ActivityApplication;
import org.onesocialweb.gwt.client.ui.application.ContactsApplication;
import org.onesocialweb.gwt.client.ui.application.PreferencesApplication;
import org.onesocialweb.gwt.client.ui.dialog.LoginDialog;
import org.onesocialweb.gwt.client.ui.menu.MainMenu;
import org.onesocialweb.gwt.client.ui.menu.MenuCommand;
import org.onesocialweb.gwt.client.util.OSWUrlBuilder;
import org.onesocialweb.gwt.service.OswService;
import org.onesocialweb.gwt.service.OswServiceFactory;
import org.onesocialweb.gwt.service.RequestCallback;
import com.google.code.gwt.storage.client.Storage;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.i18n.client.Dictionary;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.Window.Location;
public class OswClient {
// internationalization
private UserInterfaceText uiText = (UserInterfaceText) GWT.create(UserInterfaceText.class);
private static OswClient instance;
private Map<String, String> preferences = new HashMap<String, String>();
private OswService service;
private MainMenu mainMenu;
private AbstractApplication currentApplication;
private HashMap<String, MenuCommand> applicationCommands = new HashMap<String, MenuCommand>();
private HashMap<String, String> OSWLocales = new HashMap<String, String>();
private boolean sessionActive;
public static OswClient getInstance() {
if (instance == null) {
instance = new OswClient();
}
return instance;
}
public void start() {
// set the locale if defined
setLocale();
// Prepare the environment
this.service = OswServiceFactory.getService();
loadPreferences();
// Start the XMPP connection
service.setup(getPreference("bosh_path"), getPreference("bosh_host"),
getPreference("xmpp_domain"));
// Prepare the history manager (for browser history support)
History.addValueChangeHandler(new HistoryEventHandler());
History.fireCurrentHistoryState();
// Check local storage for username & password
if (Storage.isSupported()) {
Storage localStorage = Storage.getLocalStorage();
String username = localStorage.getItem("username");
String password = localStorage.getItem("password");
if (username != null && password != null) {
// if present automatically login
login(username, password);
return;
}
}
// Could not login based on stored credentials,
// so we show the login dialog
if (!sessionActive) {
showLogin();
}
}
public OswService getService() {
return service;
}
public String getPreference(String key) {
return preferences.get(key);
}
public String getPreference(String key, String defaultValue) {
String result = getPreference(key);
if (result != null) {
return result;
} else {
return defaultValue;
}
}
public void showApplication(String application) {
// remove the current application
if (currentApplication != null) {
currentApplication.destroy();
currentApplication = null;
}
// show the application
// TODO Will be refactored with a factory or other form of dependency
// injection
if (application.equals("ActivityApplication")) {
ActivityApplication activityApplication = new ActivityApplication();
currentApplication = activityApplication;
} else if (application.equals("PreferencesApplication")) {
PreferencesApplication preferencesApplication = new PreferencesApplication();
currentApplication = preferencesApplication;
} else if (application.equals("ContactsApplication")) {
ContactsApplication contactsApplication = new ContactsApplication();
currentApplication = contactsApplication;
}
// Initialized and show the application
currentApplication.init();
currentApplication.show();
// Inform the menu that we changed
if (applicationCommands.containsKey(application)) {
mainMenu.highlight(applicationCommands.get(application));
}
// set the application history
History.newItem(application, false);
}
public AbstractApplication getCurrentApplication() {
return currentApplication;
}
public void logout() {
service.logout(new RequestCallback<Object>() {
@Override
public void onFailure() {
destroySession();
}
@Override
public void onSuccess(Object result) {
destroySession();
}
});
}
private void showLogin() {
// show login box and add a handler for success
LoginDialog.getInstance().showDialog(new LoginHandler() {
public void handleLogin(boolean success) {
if (success) {
LoginDialog.getInstance().hide();
createSession();
}
}
});
}
private void login(String username, String password) {
OswServiceFactory.getService().login(username, password,
new RequestCallback<Object>() {
@Override
public void onFailure() {
showLogin();
}
@Override
public void onSuccess(Object result) {
createSession();
}
});
}
private void setLocale() {
// set locale based on the presence of a stored value in the LocalStorage
// Set the available OSW locale values
initLocales();
// get the locale parameter from the url
String localeUrl = Location.getParameter("locale");
// get the locale from the storage
String localeStored = "";
if (Storage.isSupported()) {
Storage localStorage = Storage.getLocalStorage();
localeStored = localStorage.getItem("locale");
}
// if the locale is not present
if (localeUrl == null && localeStored != null) {
if (OSWLocales.containsKey(localeStored)) {
OSWUrlBuilder urlBuilder = new OSWUrlBuilder();
urlBuilder.setParameter("locale", localeStored);
Window.Location.replace(urlBuilder.buildString());
} else {
OSWUrlBuilder urlBuilder = new OSWUrlBuilder();
urlBuilder.removeParameter("locale");
Window.Location.replace(urlBuilder.buildString());
}
}
}
private void createSession() {
sessionActive = true;
// create a new mainmenu
initMenu();
// fire the first application
// if there is an application in the URL use that
// get the locale parameter from the url
String hash = Location.getHash();
String application = "";
// remove the hash sign
if (hash != null && hash.length() > 0) {
application = hash.substring(1);
}
if (application.length() > 0) {
showApplication(application);
} else {
showApplication("ActivityApplication");
}
}
private void destroySession() {
sessionActive = false;
// make sure to kill all apps
killApplications();
// hide the menu
mainMenu.hideMenu();
// clear the local storage
Storage.getLocalStorage().clear();
// show login dialog
showLogin();
}
private void killApplications() {
currentApplication.destroy();
currentApplication = null;
}
private void initLocales() {
OSWLocales.put("default", "English");
OSWLocales.put("nl", "Nederlands");
OSWLocales.put("fr", "Français");
OSWLocales.put("it", "Italiano");
}
private void initMenu() {
List<MenuCommand> commands = new ArrayList<MenuCommand>();
MenuCommand command = new ActivityAppCommand();
applicationCommands.put("ActivityApplication", command);
commands.add(command);
command = new ContactsAppCommand();
applicationCommands.put("ContactsApplication", command);
commands.add(command);
command = new PreferencesAppCommand();
applicationCommands.put("PreferencesApplication", command);
commands.add(command);
command = new SignOutCommand();
commands.add(command);
mainMenu = new MainMenu(commands);
mainMenu.showMenu();
}
public HashMap<String, String> getOSWLocales() {
return OSWLocales;
}
private void loadPreferences() {
Dictionary prefs = Dictionary.getDictionary("preferences");
for (String key : prefs.keySet()) {
preferences.put(key, prefs.get(key));
}
}
// Private constructor to enforce the singleton
private OswClient() {
//
}
private class HistoryEventHandler implements ValueChangeHandler<String> {
@Override
public void onValueChange(ValueChangeEvent<String> event) {
if (sessionActive) {
showApplication(event.getValue());
}
}
}
private class ActivityAppCommand implements MenuCommand {
@Override
public String getLabel() {
return uiText.Activities();
}
@Override
public void execute() {
showApplication("ActivityApplication");
}
}
private class ContactsAppCommand implements MenuCommand {
@Override
public String getLabel() {
return uiText.Contacts();
}
@Override
public void execute() {
showApplication("ContactsApplication");
}
}
private class PreferencesAppCommand implements MenuCommand {
@Override
public String getLabel() {
return uiText.Preferences();
}
@Override
public void execute() {
showApplication("PreferencesApplication");
}
}
private class SignOutCommand implements MenuCommand {
@Override
public String getLabel() {
return uiText.Logout();
}
@Override
public void execute() {
logout();
}
}
}