Update to the new library 'json2'
[euphorik.git] / js / client.js
1 // coding: utf-8
2 // Copyright 2008 Grégory Burri
3 //
4 // This file is part of Euphorik.
5 //
6 // Euphorik is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // Euphorik is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with Euphorik. If not, see <http://www.gnu.org/licenses/>.
18
19 /*jslint laxbreak:true */
20
21 /**
22 * Object that represents the user.
23 * It carries all the user data like
24 * - ID
25 * - Nick
26 * - E-Mail
27 * - etc..
28 * It can access directly to the DOM if needed. Some examples :
29 * - Changes the menu if the user is logged or not
30 * - Changes the logo if the user is an ek master
31 * - etc..
32 * @util See util.js.
33 * @communication See communication.js.
34 */
35 euphorik.Client = function(util, communication) {
36 this.util = util;
37 this.communication = communication;
38
39 this.cookie = null;
40 this.regexCookie = /cookie=([^;]*)/;
41
42 this.resetPersonalData();
43
44 this.setStatus(euphorik.Client.statusType.disconnected);
45
46 // If true then each data change is flushed to the server.
47 // Active only for opera which doesn't support the unload event.
48 this.autoflush = $.browser.opera;
49 };
50
51 // The three status of a client.
52 euphorik.Client.statusType = {
53 // Authentified and registered : The user can post messages and can modify his profile.
54 auth_registered : 0,
55 // Authentified but not registered : The user can only post messages.
56 auth_not_registered : 1,
57 // Disconnected (the initial state) : The user cannot post message.
58 disconnected : 2
59 };
60
61 /**
62 * Reset all the local personal data. Does not erase the remote data.
63 * This function is used during disconnecting.
64 */
65 euphorik.Client.prototype.resetPersonalData = function() {
66 this.id = 0;
67 this.nick = euphorik.conf.defaultNick;
68 this.login = "";
69 this.password = "";
70 this.email = "";
71 this.css = $("link#mainCss").attr("href");
72 this.chatOrder = "reverse";
73 this.nickFormat = "nick";
74 this.viewTimes = true;
75 this.viewTooltips = true;
76 this.cookie = undefined;
77
78 this.mainConversationPage = 1;
79 this.ekMaster = false;
80 this.ostentatiousMaster = "light";
81
82 // The user opened conversations.
83 // Each conversation object owns theses properties :
84 // - root (integer)
85 // - page (integer)
86 // - isCollapsed (bool)
87 this.conversations = [];
88 };
89
90 /**
91 * Set the CSS. Dynamically change the href to the CSS in the DOM.
92 * @css The relative path to the CSS file, for example :
93 * "styles/1/euphorik.css".
94 */
95 euphorik.Client.prototype.setCss = function(css) {
96 if (this.css === css || !css) {
97 return;
98 }
99
100 this.css = css;
101 $("link#mainCss").attr("href", this.css);
102
103 if (this.autoflush) {
104 this.flush(true);
105 }
106 };
107
108 /**
109 * Change the current page to the next one for the given conversation.
110 * @numConv The number of the conversation.
111 */
112 euphorik.Client.prototype.nextPage = function(numConv) {
113 if (numConv < 0 && this.mainConversationPage > 1) {
114 this.mainConversationPage -= 1;
115 } else if (this.conversations[numConv].page > 1) {
116 this.conversations[numConv].page -= 1;
117 }
118 };
119
120 /**
121 * Change the current page to the previous one for the given conversation.
122 * @numConv The number of the conversation.
123 */
124 euphorik.Client.prototype.previousPage = function(numConv) {
125 if (numConv < 0) {
126 this.mainConversationPage += 1;
127 } else {
128 this.conversations[numConv].page += 1;
129 }
130 };
131
132 /**
133 * Définit la première page pour la conversation donnée.
134 * @return true si la page a changé sinon false
135 */
136 euphorik.Client.prototype.goFirstPage = function(numConv) {
137 if (numConv < 0) {
138 if (this.mainConversationPage === 1) {
139 return false;
140 }
141 this.mainConversationPage = 1;
142 } else {
143 if (this.conversations[numConv].page === 1) {
144 return false;
145 }
146 this.conversations[numConv].page = 1;
147 }
148 return true;
149 };
150
151 /**
152 * Ajoute une conversation à la vue de l'utilisateur.
153 * Le profile de l'utilisateur est directement sauvegardé sur le serveur.
154 * @param racines la racine de la conversation (integer)
155 * @return true si la conversation a été créée sinon false (par exemple si la conv existe déjà)
156 */
157 euphorik.Client.prototype.ajouterConversation = function(racine) {
158 // vérification s'il elle n'existe pas déjà
159 var existe = false;
160 this.conversations.each(function(i, conv) {
161 if (conv.root === racine) {
162 existe = true;
163 }
164 });
165 if (existe) {
166 return false;
167 }
168
169 this.conversations.push({root : racine, page : 1, isCollapsed : false});
170 if (this.autoflush) {
171 this.flush(true);
172 }
173
174 return true;
175 };
176
177 euphorik.Client.prototype.supprimerConversation = function(num) {
178 if (num < 0 || num >= this.conversations.length) {
179 return;
180 }
181
182 // décalage TODO : supprimer le dernier élément
183 for (var i = num; i < this.conversations.length - 1; i++) {
184 this.conversations[i] = this.conversations[i+1];
185 }
186 this.conversations.pop();
187
188 if (this.autoflush) {
189 this.flush(true);
190 }
191 };
192
193 euphorik.Client.prototype.getJSONConversations = function() {
194 var conversations = [];
195 this.conversations.each(function(i, conv) {
196 conversations.push({ "root" : conv.root, "minimized" : conv.isCollapsed });
197 });
198 return conversations;
199 };
200
201 euphorik.Client.prototype.getJSONProfile = function() {
202 return {
203 "cookie" : this.cookie,
204 "login" : this.login,
205 "password" : this.password,
206 "profile" : this.getJSONProfileInfos()
207 };
208 };
209
210 euphorik.Client.prototype.getJSONProfileInfos = function() {
211 return {
212 "nick" : this.nick,
213 "email" : this.email,
214 "css" : this.css,
215 "chat_order" : this.chatOrder,
216 "nick_format" : this.nickFormat,
217 "view_times" : this.viewTimes,
218 "view_tooltips" : this.viewTooltips,
219 "conversations" : this.getJSONConversations(),
220 "ostentatious_master" : this.ostentatiousMaster
221 };
222 };
223
224 /**
225 * .
226 */
227 euphorik.Client.prototype.getCookie = function() {
228 var cookie = this.regexCookie.exec(document.cookie);
229 if (cookie) {
230 this.cookie = cookie[1];
231 } else {
232 this.cookie = undefined;
233 }
234 };
235
236 euphorik.Client.prototype.delCookie = function() {
237 document.cookie = "cookie=; max-age=0";
238 this.cookie = undefined;
239 };
240
241 euphorik.Client.prototype.setCookie = function() {
242 if (!this.cookie) {
243 return;
244 }
245
246 // doesn't work under IE....
247 /*document.cookie = "cookie=" + this.cookie + "; max-age=" + (60 * 60 * 24 * 365) */
248
249 document.cookie =
250 "cookie="+this.cookie+"; expires=" + new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 365).toUTCString();
251 };
252
253 euphorik.Client.prototype.authentifie = function() {
254 return this.statut === euphorik.Client.statusType.auth_registered || this.statut === euphorik.Client.statusType.auth_not_registered;
255 };
256
257 euphorik.Client.prototype.setStatus = function(statut)
258 {
259 // conversation en "enum" si en "string"
260 if (typeof(statut) === "string") {
261 statut =
262 statut === "auth_registered" ?
263 euphorik.Client.statusType.auth_registered :
264 (statut === "auth_not_registered" ? euphorik.Client.statusType.auth_not_registered : euphorik.Client.statusType.disconnected);
265 }
266
267 if (statut === this.statut) {
268 return;
269 }
270
271 this.statut = statut;
272
273 this.majMenu();
274 this.majLogo();
275 };
276
277 /**
278 * Try to authentify the client with the cookie information.
279 * Do nothing if there is no cookie.
280 */
281 euphorik.Client.prototype.connectionCookie = function() {
282 this.getCookie();
283 if (!this.cookie) {
284 return false;
285 }
286 return this.connexion("authentification", { "cookie" : this.cookie });
287 };
288
289 euphorik.Client.prototype.connexionLogin = function(login, password) {
290 return this.connexion("authentification", {"login" : login, "password" : password });
291 };
292
293 euphorik.Client.prototype.enregistrement = function(login, password) {
294 if (this.authentifie()) {
295 this.login = login;
296 this.password = password;
297 if(this.flush()) {
298 this.setStatus(euphorik.Client.statusType.auth_registered);
299 return true;
300 }
301 return false;
302 } else {
303 return this.connexion("register", this.getJSONEnregistrement(login, password));
304 }
305 };
306
307 /**
308 * le couple (login, password) est facultatif. S'il n'est pas fournit alors il ne sera pas possible
309 * de s'autentifier avec (login, password).
310 */
311 euphorik.Client.prototype.getJSONEnregistrement = function(login, password) {
312 var mess = {};
313
314 if (login && password) {
315 mess.login = login;
316 mess.password = password;
317 }
318
319 mess.profile = this.getJSONProfileInfos();
320
321 return mess;
322 };
323
324 /**
325 * Connexion. Réalisé de manière synchrone.
326 */
327 euphorik.Client.prototype.connexion = function(action, messageJson) {
328 var thisClient = this;
329
330 this.communication.requete(
331 action,
332 messageJson,
333 function(data) {
334 thisClient.chargerDonnees(data);
335 },
336 function(data) {
337 thisClient.util.messageDialog(data.error_message);
338 thisClient.delCookie(); // suppression du cookie actuel, cas où le cookie du client ne permet pas une authentification
339 },
340 false
341 );
342 return this.authentifie();
343 };
344
345 euphorik.Client.prototype.disconnect = function() {
346 this.flush(true);
347 this.delCookie();
348 this.resetPersonalData();
349 this.setStatus(euphorik.Client.statusType.disconnected);
350 };
351
352 euphorik.Client.prototype.chargerDonnees = function(data) {
353 // la modification du statut qui suit met à jour le menu, le menu dépend (page admin)
354 // de l'état ekMaster
355 this.ekMaster = data.ek_master ? data.ek_master : false;
356
357 this.setStatus(data.status);
358
359 if (this.authentifie()) {
360 this.cookie = data.cookie;
361 this.setCookie();
362
363 this.id = data.id;
364 this.login = data.login;
365 this.nick = data.profile.nick;
366 this.email = data.profile.email;
367 this.setCss(data.profile.css);
368 this.chatOrder = data.profile.chat_order;
369 this.nickFormat = data.profile.nick_format;
370 this.viewTimes = data.profile.view_times;
371 this.viewTooltips = data.profile.view_tooltips;
372 this.ostentatiousMaster = data.profile.ostentatious_master;
373
374 // la page de la conversation principale
375 this.mainConversationPage = 1;
376
377 // les conversations
378 this.conversations = data.profile.conversations;
379 this.conversations.map(function(conv) {
380 return { root : conv.root, page : 1, isCollapsed : conv.minimized };
381 });
382
383 this.majBulle();
384 this.majCssSelectionee();
385 }
386 };
387
388 /**
389 * Met à jour les données personne sur serveur.
390 * @param async de manière asynchrone ? défaut = true
391 * @return false si le flush n'a pas pû se faire sinon true
392 */
393 euphorik.Client.prototype.flush = function(async) {
394 async = async || false;
395
396 if (!this.authentifie()) {
397 return false;
398 }
399
400 var thisClient = this;
401 var ok = true;
402
403 this.communication.requete(
404 "set_profile",
405 this.getJSONProfile(),
406 function(data) {
407 thisClient.majBulle();
408 },
409 function(data) {
410 thisClient.util.messageDialog(data.error_message);
411 ok = false;
412 },
413 async
414 );
415
416 return ok;
417 };
418
419 euphorik.Client.prototype.majMenu = function() {
420 var displayType = "block";
421
422 $("#menu .admin").css("display", this.ekMaster ? displayType : "none");
423
424 // met à jour le menu
425 if (this.statut === euphorik.Client.statusType.auth_registered) {
426 $("#menu .profile").css("display", displayType).text("profile");
427 $("#menu .logout").css("display", displayType);
428 $("#menu .register").css("display", "none");
429 } else if (this.statut === euphorik.Client.statusType.auth_not_registered) {
430 $("#menu .profile").css("display", "none");
431 $("#menu .logout").css("display", displayType);
432 $("#menu .register").css("display", displayType);
433 } else {
434 $("#menu .profile").css("display", displayType).text("login");
435 $("#menu .logout").css("display", "none");
436 $("#menu .register").css("display", displayType);
437 }
438 };
439
440 /**
441 * Met à jour l'affichage ou non des infos bulles en fonction du profile.
442 */
443 euphorik.Client.prototype.majBulle = function() {
444 this.util.bulleActive = this.viewTooltips;
445 };
446
447 /**
448 * Met à jour la css sélectionnée, lors du chargement des données.
449 */
450 euphorik.Client.prototype.majCssSelectionee = function() {
451 // extraction du numéro de la css courante
452 var numCssCourante = this.css.match(/^.*?\/(\d)\/.*$/);
453 if (numCssCourante && numCssCourante[1]) {
454 $("#menuCss option").removeAttr("selected");
455 $("#menuCss option[value=" + numCssCourante[1]+ "]").attr("selected", "selected");
456 }
457 };
458
459 /**
460 * Change la "class" du logo en fonction du statut de ekMaster.
461 */
462 euphorik.Client.prototype.majLogo = function() {
463 if (this.ekMaster) {
464 $("#logo").addClass("ekMaster");
465 } else {
466 $("#logo").removeClass("ekMaster");
467 }
468 };
469
470 euphorik.Client.prototype.slap = function(userId, raison) {
471 var thisClient = this;
472 this.communication.requete("slap", { "cookie" : thisClient.cookie, "user_id" : userId, "reason" : raison });
473 };
474
475 euphorik.Client.prototype.ban = function(userId, raison, minutes) {
476 var thisClient = this;
477
478 // par défaut un ban correspond à 3 jours
479 minutes = minutes || euphorik.conf.tempsBan;
480 this.communication.requete("ban", { "cookie" : thisClient.cookie, "duration" : minutes, "user_id" : userId, "reason" : raison });
481 };
482
483 euphorik.Client.prototype.kick = function(userId, raison) {
484 this.ban(userId, raison, euphorik.conf.tempsKick);
485 };