1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 *
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Initial Developer of the Original Code is Fireinput Inc.
15 *
16 * Portions created by the Initial Developer are Copyright (C) 2007
17 * the Initial Developer. All Rights Reserved.
18 *
19 * Contributor(s):
20 * Olly Ja <ollyja@gmail.com>
21 *
22 * Alternatively, the contents of this file may be used under the terms of
23 * either the GNU General Public License Version 2 or later (the "GPL"), or
24 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
25 * in which case the provisions of the GPL or the LGPL are applicable instead
26 * of those above. If you wish to allow use of your version of this file only
27 * under the terms of either the GPL or the LGPL, and not to allow others to
28 * use your version of this file under the terms of the MPL, indicate your
29 * decision by deleting the provisions above and replace them with the notice
30 * and other provisions required by the GPL or the LGPL. If you do not delete
31 * the provisions above, a recipient may use your version of this file under
32 * the terms of any one of the MPL, the GPL or the LGPL.
33 *
34 * ***** END LICENSE BLOCK *****
35 */
36
37 const SERVER_URL = "http://www.fireinput.com/";
38 var EmotionMgr =
39 {
40 imageFileValid: false,
41
42 userEmotionList: [],
43
44 showMyEmotions: false,
45
46 limitnum: 10,
47
48 startindex: 0,
49
50 selectedpage: 0,
51
52 initTab: function(tabid)
53 {
54 var handle = document.getElementById("addFileCommand");
55 if(tabid == 1)
56 handle.style.display = "";
57 else
58 handle.style.display = "none";
59 // command for browserFile panel
60 handle = document.getElementById("browseFileCommand");
61 if(tabid == 2)
62 {
63 handle.style.display = "";
64 }
65 else
66 handle.style.display = "none";
67 },
68
69 notify: function()
70 {
71 var os = Components.classes["@mozilla.org/observer-service;1"]
72 .getService(Components.interfaces.nsIObserverService);
73 os.notifyObservers(null, "fireinput-user-emotion-changed", null);
74 return true;
75 },
76
77 clearAddFileMessage: function()
78 {
79 var handle = document.getElementById("addFileMessage");
80 handle.innerHTML = "";
81
82 },
83
84 showAddFileMessage: function(flag, message)
85 {
86 var handle = document.getElementById("addFileMessage");
87 handle.style.color = "green";
88 if(!flag)
89 {
90 handle.style.color = "red";
91 }
92
93 handle.innerHTML = message;
94 },
95
96 addFileOnFocus: function(event)
97 {
98 this.clearAddFileMessage();
99 },
100
101 addFileOnInput: function(event)
102 {
103 var handle = document.getElementById("uploadForm");
104 var imageUrl = "";
105 if(handle.style.display != "none")
106 {
107 var id = document.getElementById("uploadFileValue");
108 imageUrl = "file://" + id.value;
109 }
110 else
111 {
112 var id = document.getElementById("addFileValue");
113 imageUrl = id.value;
114 }
115
116 this.clearAddFileMessage();
117
118 var imgHandle = document.getElementById("addFileShowImage");
119 imgHandle.style.display = "none";
120 this.imageFileValid = false;
121 imgHandle.onload = bind(function(event)
122 {
123 this.imageFileValid = true;
124 imgHandle.style.display = "";
125 }, this);
126
127 imgHandle.src = imageUrl;
128 },
129
130 addFileIntoList: function()
131 {
132 if(!this.imageFileValid)
133 {
134 this.showAddFileMessage(false, "图案还没有成功显示或不合法的链接");
135 return;
136 }
137
138 var id = document.getElementById("addFileValue");
139 var imageUrl = id.value;
140 if(FireinputEmotionUpdater.save(imageUrl))
141 {
142 this.notify();
143 this.showAddFileMessage(true, "图案成功加入网络图案菜单");
144 }
145 else
146 this.showAddFileMessage(false, "图案加入失败-不应该发生,请到火输网站报告错误");
147 },
148
149 checkUserLogon: function()
150 {
151 var ajax = new Ajax();
152 if(!ajax)
153 return;
154 var self = this;
155
156 ajax.setOptions(
157 {
158 method: 'get',
159 onSuccess: function(p) { self.checkUserLogonSuccess(p); },
160 onFailure: function(p) { self.checkUserLogonFailure(p); }
161 });
162 ajax.request(SERVER_URL + "/account/logon_info.php");
163 },
164
165 checkUserLogonSuccess: function(p)
166 {
167 if(!p || p.responseText.length <= 0)
168 {
169 this.checkUserLogonFailure(p);
170 return;
171 }
172
173 var jsonResp;
174 try {
175 jsonResp = eval('(' + p.responseText + ')');
176 }
177 catch(e)
178 {
179 this.checkUserLogonFailure(p);
180 return;
181 };
182
183 if(typeof(jsonResp) == 'undefined')
184 {
185 this.checkUserLogonFailure(p);
186 return;
187 }
188
189 var handle = document.getElementById("logonUser");
190 handle.innerHTML = "您好! " + jsonResp.name;
191 handle = document.getElementById("logonUserBox");
192 handle.style.display = "";
193
194 handle = document.getElementById("logonForm");
195 handle.style.display = "none";
196
197 handle = document.getElementById("logonButton");
198 handle.setAttribute("image", "");
199
200 handle = document.getElementById("logonLink");
201 handle.style.display = "none";
202
203 // my upload
204 handle = document.getElementById("showMyUpload");
205 handle.style.display = "";
206 },
207
208 checkUserLogonFailure: function(p)
209 {
210
211 var handle = document.getElementById("logonUser");
212 handle.innerHTML = "";
213
214 handle = document.getElementById("logonUserBox");
215 handle.style.display = "none";
216
217 handle = document.getElementById("showMyUpload");
218 handle.style.display = "none";
219 },
220
221 showAddFileForm: function(nothidden)
222 {
223 var handle = document.getElementById("addFileHelp");
224 if(nothidden)
225 handle.style.display = "";
226 else
227 handle.style.display = "none";
228 handle = document.getElementById("addForm");
229 if(nothidden)
230 handle.style.display = "";
231 else
232 handle.style.display = "none";
233 },
234
235 cleanUploadForm: function()
236 {
237 this.imageFileValid = false;
238 var handle = document.getElementById("uploadFileValue");
239 handle.value = "";
240
241 this.clearAddFileMessage();
242
243 var imgHandle = document.getElementById("addFileShowImage");
244 imgHandle.style.display = "none";
245 imgHandle.setAttribute("src", "");
246 },
247
248 toggleUploadForm: function()
249 {
250 this.cleanUploadForm();
251
252 var handle = document.getElementById("uploadLinkBox");
253 if(handle.style.display != "none")
254 {
255 handle.style.display = "none";
256 handle = document.getElementById("uploadForm");
257 handle.style.display = "none";
258 handle = document.getElementById("uploadLink");
259 handle.innerHTML = "上传";
260
261 this.showAddFileForm(true);
262 return;
263 }
264
265 handle.style.display = "";
266 handle = document.getElementById("uploadLink");
267 handle.innerHTML = "取消上传";
268
269 handle = document.getElementById("needLogonHelp");
270 handle.style.display = document.getElementById("logonUserBox").style.display == "none" ? "":"none";
271
272 handle = document.getElementById("uploadForm");
273 handle.style.display = "";
274 this.showAddFileForm(false);
275 },
276
277
278 toggleLogonForm: function(event)
279 {
280 var handle = document.getElementById("logonForm");
281 if(handle.style.display != "none")
282 {
283 handle.style.display = "none";
284 return;
285 }
286 var ajax = new Ajax();
287 if(!ajax)
288 return;
289
290 var self = this;
291 var px = event.pageX - 400;
292 var py = event.pageY+1;
293 ajax.setOptions(
294 {
295 method: 'get',
296 onSuccess: function(p) { self.showLogonFormSuccess(p, px, py); },
297 onFailure: function(p) { self.showLogonFormFailure(p, px, py); }
298 });
299 ajax.request(SERVER_URL + "/account/logon_form_simple.php");
300 },
301
302 showLogonFormSuccess: function(p, px, py)
303 {
304 var handle = document.getElementById("logonSeed");
305 handle.value = p.responseText;
306 $("#logonForm").css("left", px);
307 $("#logonForm").css("top", py);
308 $("#logonForm").css("height", 150);
309 $("#logonForm").show();
310 },
311
312 showLogonFormFailure: function(p, px, py)
313 {
314 $("#logonForm").css("left", px);
315 $("#logonForm").css("top", py);
316 $("#logonForm").css("height", 50);
317 $("#logonForm").html("<div style='margin: 10px; color:red'>无法显示登录网页,连接火输网站失败</div>");
318 $("#logonForm").show();
319 },
320
321 logon: function(event)
322 {
323 // check enter key
324 if(event && event.keyCode!=13)
325 return true;
326 var handle = document.getElementById("logonMessage");
327 handle.value="";
328
329 handle = document.getElementById("logonButton");
330 handle.type = "image";
331 handle.src = "chrome://fireinput/skin/loading.gif";
332 var self = this;
333 setTimeout(function(){self.logonServer(); }, 1000);
334 return false;
335 },
336
337 logonServer: function()
338 {
339
340 var email = document.getElementById("logonEmail").value;
341 var password = document.getElementById("logonPasswd").value;
342 var seed = document.getElementById("logonSeed").value;
343 var salt = password.substr(Math.floor(password.length/2),password.length);
344 var md5hex1 = hex_hmac_md5(password, salt);
345 var md5hex2 = hex_hmac_md5(md5hex1, seed);
346 var url = "/account/logon_user.php";
347 var params = "email="+email + "&password="+md5hex2;
348
349 var ajax = new Ajax();
350 var self = this;
351 ajax.setOptions(
352 {
353 method: 'post',
354 postBody: params,
355 contentType: 'application/x-www-form-urlencoded',
356 onSuccess: function(p) { self.logonServerSuccess(p); },
357 onFailure: function(p) { self.logonServerFailure(p); }
358 });
359 ajax.request(SERVER_URL + url);
360 },
361
362 logonServerSuccess: function(p)
363 {
364 var handle = document.getElementById("logonButton");
365 handle.type = "button";
366 handle.src = "";
367 this.checkUserLogon();
368 },
369
370 logonServerFailure: function(p)
371 {
372 var handle = document.getElementById("logonMessage");
373 handle.innerHTML="登录失败,您输入的邮箱或密码不正确";
374
375 handle = document.getElementById("logonButton");
376 handle.type = "button";
377 handle.src = "";
378 },
379
380 goToPage: function(url)
381 {
382 var mainWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
383 .getInterface(Components.interfaces.nsIWebNavigation)
384 .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
385 .rootTreeItem
386 .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
387 .getInterface(Components.interfaces.nsIDOMWindow);
388 var gBrowser = mainWindow.top.document.getElementById("content");
389 if(gBrowser)
390 gBrowser.selectedTab = gBrowser.addTab(url);
391 else
392 window.open(url);
393 },
394
395
396 uploadFileCheckImage: function()
397 {
398 if(!this.imageFileValid)
399 {
400 this.showAddFileMessage(false, "图案还没有成功显示或不合法的图像文件");
401 return false;
402 }
403
404 return true;
405
406 },
407
408 uploadFileComplete: function(response)
409 {
410 /* the reponse should be valud URL */
411 if(!response || response.length <= 0 || !/^http:\/\//.test(response))
412 {
413 this.showAddFileMessage(false, "图案没有成功上传,火输网站现在可能不能够处理您的上传,请稍后再试");
414 return;
415 }
416
417 if(FireinputEmotionUpdater.save(response))
418 {
419 this.notify();
420 this.showAddFileMessage(true, "图案成功上传并已加入到网络图案菜单");
421 }
422 else
423 this.showAddFileMessage(false, "图案加入失败-不应该发生,请到火输网站报告错误");
424
425 },
426
427 uploadFrame: function(cb)
428 {
429 var n = 'f' + Math.floor(Math.random() * 99999);
430 var d = document.createElement('div');
431 d.innerHTML = '<iframe style="display:none" src="about:blank" id="'+n+'" name="'+n+'" onload="EmotionMgr.uploadLoaded(\''+n+'\')"></iframe>';
432 document.body.appendChild(d);
433 var i = document.getElementById(n);
434 if (cb && typeof(cb.onComplete) == 'function') {
435 i.onComplete = cb.onComplete;
436 }
437
438 return n;
439 },
440
441 uploadForm: function(targetForm, name)
442 {
443 targetForm.setAttribute('target', name);
444 },
445
446 uploadFile: function(targetForm)
447 {
448 if(!this.imageFileValid)
449 {
450 this.showAddFileMessage(false, "图案还没有成功显示或不合法的图像文件");
451 return false;
452 }
453
454 var self = this;
455
456 var cb = {
457 onStart: function() { self.uploadFileCheckImage(); },
458 onComplete: function(p) { self.uploadFileComplete(p); }
459 };
460
461 this.uploadForm(targetForm, this.uploadFrame(cb));
462 if (cb && typeof(cb.onStart) == 'function') {
463 return cb.onStart();
464 } else {
465 return true;
466 }
467 },
468
469 uploadLoaded : function(id)
470 {
471 var i = document.getElementById(id);
472 if (i.contentDocument) {
473 var d = i.contentDocument;
474 } else if (i.contentWindow) {
475 var d = i.contentWindow.document;
476 } else {
477 var d = window.frames[id].document;
478 }
479 if (d.location.href == "about:blank") {
480 return;
481 }
482
483 if (typeof(i.onComplete) == 'function') {
484 i.onComplete(d.body.innerHTML);
485 }
486 },
487
488
489 updateUserEmotionList: function(checkbox, emotionurl)
490 {
491 // clean up the save file message
492 this.showSaveFileMessage(true, "<font color='#E76539'>您所作的更改还未保存</font>");
493
494 for (var i=this.userEmotionList.length-1; i>=0; i--)
495 {
496 if(this.userEmotionList[i].url == emotionurl)
497 {
498 if(checkbox.checked)
499 this.userEmotionList[i].saved = true;
500 else
501 this.userEmotionList[i].saved = false;
502
503 return;
504 }
505 }
506
507 if(checkbox.checked)
508 this.userEmotionList[this.userEmotionList.length] = {url: emotionurl, saved: true};
509 },
510
511
512 getUserEmotionURL: function(str)
513 {
514 this.userEmotionList[this.userEmotionList.length] = { url: str, saved: true};
515 },
516
517 loadCurrentEmotions: function()
518 {
519 // load local list
520 var ios = FireinputXPC.getIOService();
521 var fileHandler = ios.getProtocolHandler("file")
522 .QueryInterface(Components.interfaces.nsIFileProtocolHandler);
523
524 var path = FireinputUtils.getAppRootPath();
525 var datafile = fileHandler.getFileFromURLSpec(path + "/useremotion.fireinput");
526 this.userEmotionList.length = 0;
527 if(!datafile.exists())
528 {
529 // initialize the pagination
530 this.showCurrentEmotions();
531 return;
532 }
533
534 var options = {
535 caller: this,
536 oncomplete: this.showCurrentEmotions,
537 onavailable: this.getUserEmotionURL
538 };
539 FireinputStream.loadDataAsync(datafile, options);
540 },
541
542 showCurrentEmotions: function()
543 {
544 var handle = document.getElementById("browseFileList");
545 var pageinfo = {};
546 pageinfo.page = this.userEmotionList;
547 pageinfo.electedpage=0;
548
549 if(this.userEmotionList.length <= 0)
550 {
551 this.showBrowseFileMessage(true, "您还没有加入图案");
552 this.disableBrowseSaveButton(true);
553 }
554 else
555 {
556 this.showBrowseFileMessage(true, "");
557 this.disableBrowseSaveButton(false);
558 }
559
560 this.showSaveFileMessage(true, "");
561 Pagination.buildPages(pageinfo, 'pagecontent', 'paginatioplink', this.limitnum);
562
563 },
564
565 getCheckedStatus: function(emotionurl)
566 {
567 for (var i=this.userEmotionList.length-1; i>=0; i--)
568 {
569 if(this.userEmotionList[i].url == emotionurl)
570 {
571 return this.userEmotionList[i].saved;
572 }
573 }
574
575 return false;
576 },
577
578 showMimeOrAll: function(flag)
579 {
580 this.showMyEmotions = flag;
581 // reload
582 this.loadServerEmotions();
583 },
584
585 loadServerNextPages: function(sindex,spage)
586 {
587 this.startindex = sindex;
588 this.selectedpage = spage;
589 // reload
590 this.loadServerEmotions();
591 },
592
593 loadServerEmotions: function()
594 {
595 var ajax = new Ajax();
596 if(!ajax)
597 return;
598
599 var self = this;
600
601 var url = SERVER_URL + "/emotions/viewlist.php?";
602 url += "startindex=" + this.startindex + "&limitnum=" + this.limitnum;
603 if(this.showMyEmotions)
604 url += "&showmine=true";
605 ajax.setOptions(
606 {
607 method: 'get',
608 onSuccess: function(p) { self.loadServerEmotionSuccess(p); },
609 onFailure: function(p) { self.loadServerEmotionFailure(p); }
610 });
611 ajax.request(url);
612 },
613
614 loadServerEmotionSuccess: function(p)
615 {
616 if(!p)
617 return;
618 if(p.responseText.length <= 0)
619 return;
620
621 var jsonArray;
622 try {
623 jsonArray = eval('(' + p.responseText + ')');
624 }
625 catch(e) { };
626
627 if(typeof(jsonArray) == 'undefined')
628 return;
629
630 this.showBrowseFileMessage(true, "");
631 this.showSaveFileMessage(true, "");
632 this.disableBrowseSaveButton(false);
633 Pagination.buildRemotePages(jsonArray.totalcount, jsonArray.urllist, this.selectedpage, 'pagecontent', 'paginatioplink', this.limitnum);
634 },
635
636 loadServerEmotionFailure: function(p)
637 {
638 this.showBrowseFileMessage(false, "连接火输网站失败");
639 this.showSaveFileMessage(true, "");
640 this.disableBrowseSaveButton(true);
641 Pagination.buildRemotePages(0, null, 0, 'pagecontent', 'paginatioplink', this.limitnum);
642 return;
643 },
644
645 showBrowseFileMessage: function(flag, message)
646 {
647 var handle = document.getElementById("browseFileMessage");
648 handle.style.color = "green";
649 if(!flag)
650 {
651 handle.style.color = "red";
652 }
653
654 handle.innerHTML = message;
655 },
656
657 showSaveFileMessage: function(flag, message)
658 {
659 var handle = document.getElementById("saveFileMessage");
660 handle.style.color = "green";
661 if(!flag)
662 {
663 handle.style.color = "red";
664 }
665
666 handle.innerHTML = message;
667 },
668
669 saveFileToList: function()
670 {
671 if(FireinputEmotionUpdater.save(this.userEmotionList, 'overwrite'))
672 {
673 this.notify();
674 this.showSaveFileMessage(true, "您所作的更改已成功保存");
675 // refresh the page
676 this.loadCurrentEmotions();
677 }
678 else
679 this.showSaveFileMessage(false, "保存更改-不应该发生,请到火输网站报告错误");
680 },
681
682 disableBrowseSaveButton: function(flag)
683 {
684 var handle = document.getElementById("updateButton");
685 handle.disabled = flag;
686 }
687
688
689 };
syntax highlighted by Code2HTML, v. 0.9.1