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 var EmotionMgr = 
 38 {
 39     imageFileValid: false, 
 40 
 41     userEmotionList:  [], 
 42 
 43     showMyEmotions: false,
 44 
 45     limitnum: 10,
 46 
 47     startindex: 0,
 48 
 49     selectedpage: 0, 
 50 
 51     initTab: function(tabid)
 52     {
 53        var handle = document.getElementById("addFileCommand"); 
 54        if(tabid == 1)
 55           handle.style.display = ""; 
 56        else    
 57           handle.style.display = "none"; 
 58        // command for browserFile panel 
 59        handle = document.getElementById("browseFileCommand");
 60        if(tabid == 2)
 61        {
 62           handle.style.display = ""; 
 63        }
 64        else    
 65           handle.style.display = "none"; 
 66     }, 
 67 
 68     notify: function()
 69     {
 70        var os = FireinputXPC.getService("@mozilla.org/observer-service;1", "nsIObserverService");
 71        os.notifyObservers(null, "fireinput-user-emotion-changed", null);
 72        return true;
 73     },
 74    
 75     clearAddFileMessage: function()
 76     {
 77        var handle = document.getElementById("addFileMessage");
 78        handle.innerHTML = "";
 79 
 80     },
 81 
 82     showAddFileMessage: function(flag, message)
 83     {
 84        var handle = document.getElementById("addFileMessage");
 85        handle.style.color = "green";
 86        if(!flag)
 87        {
 88           handle.style.color = "red";
 89        }
 90 
 91        handle.innerHTML = message; 
 92     }, 
 93           
 94     addFileOnFocus: function(event)
 95     {
 96        this.clearAddFileMessage(); 
 97     },
 98 
 99     addFileOnInput: function(event)
100     {
101        var handle = document.getElementById("uploadForm");
102        var imageUrl = "";
103        if(handle.style.display != "none")
104        {
105           var id = document.getElementById("uploadFileValue");
106           imageUrl = "file://" + id.value;
107        }
108        else
109        {
110           var id = document.getElementById("addFileValue");
111           imageUrl = id.value; 
112        }
113 
114        this.clearAddFileMessage(); 
115 
116        var imgHandle = document.getElementById("addFileShowImage");
117        imgHandle.style.display = "none";
118        this.imageFileValid = false; 
119        imgHandle.onload = bind(function(event) 
120                                {
121                                   this.imageFileValid = true; 
122                                   imgHandle.style.display = "";
123                                }, this); 
124 
125        imgHandle.src = imageUrl;
126     },
127 
128     addFileIntoList: function()
129     {
130        if(!this.imageFileValid)
131        {
132           this.showAddFileMessage(false, "图案还没有成功显示或不合法的链接"); 
133           return; 
134        }
135 
136        var id = document.getElementById("addFileValue");
137        var imageUrl = id.value;
138        if(FireinputEmotionUpdater.save(imageUrl))
139        {
140           this.notify(); 
141           this.showAddFileMessage(true, "图案成功加入网络图案菜单");
142        }
143        else
144           this.showAddFileMessage(false, "图案加入失败-不应该发生,请到火输网站报告错误");
145     },
146 
147     showAddFileForm: function(nothidden)
148     {
149        var handle = document.getElementById("addFileHelp"); 
150        if(nothidden)
151           handle.style.display = ""; 
152        else 
153           handle.style.display = "none"; 
154        handle = document.getElementById("addForm");
155        if(nothidden)
156           handle.style.display = ""; 
157        else 
158           handle.style.display = "none"; 
159     }, 
160 
161     toggleUploadForm: function()
162     {
163        this.cleanUploadForm(); 
164 
165        var handle = document.getElementById("uploadLinkBox");
166        if(handle.style.display != "none")
167        {
168          handle.style.display = "none";
169          handle = document.getElementById("uploadForm");
170          handle.style.display = "none";
171          handle = document.getElementById("uploadLink");
172          handle.innerHTML = "上传"; 
173 
174          this.showAddFileForm(true);     
175          return;
176        }
177 
178        handle.style.display = "";
179        handle = document.getElementById("uploadLink");
180        handle.innerHTML = "取消上传"; 
181 
182        handle = document.getElementById("needLogonHelp");
183        handle.style.display = document.getElementById("logonUserBox").style.display == "none" ? "":"none";
184 
185        handle = document.getElementById("uploadForm");
186        handle.style.display = "";
187        this.showAddFileForm(false);     
188     },
189 
190     cleanUploadForm: function()
191     {
192        this.imageFileValid = false; 
193        var handle = document.getElementById("uploadFileValue"); 
194        handle.value = "";
195 
196        this.clearAddFileMessage(); 
197 
198        var imgHandle = document.getElementById("addFileShowImage");
199        imgHandle.style.display = "none";
200        imgHandle.setAttribute("src", "");
201     }, 
202 
203     goToPage: function(url)
204     {
205        var mainWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
206                    .getInterface(Components.interfaces.nsIWebNavigation)
207                    .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
208                    .rootTreeItem
209                    .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
210                    .getInterface(Components.interfaces.nsIDOMWindow); 
211        var gBrowser = mainWindow.top.document.getElementById("content");
212        if(gBrowser)
213            gBrowser.selectedTab = gBrowser.addTab(url); 
214        else
215            window.open(url); 
216     }, 
217 
218    
219     uploadFileCheckImage: function()
220     {
221        if(!this.imageFileValid)
222        { 
223           this.showAddFileMessage(false, "图案还没有成功显示或不合法的图像文件"); 
224           return false; 
225        }
226        
227        return true; 
228 
229     }, 
230 
231     uploadFileComplete: function(response)
232     {
233        /* the reponse should be valud URL */
234        if(!response || response.length <= 0 || !/^http:\/\//.test(response))
235        {
236           this.showAddFileMessage(false, "图案没有成功上传,火输网站现在可能不能够处理您的上传,请稍后再试"); 
237           return; 
238        }    
239 
240        if(FireinputEmotionUpdater.save(response))
241        {
242           this.notify();
243           this.showAddFileMessage(true, "图案成功上传并已加入到网络图案菜单");
244        }
245        else
246           this.showAddFileMessage(false, "图案加入失败-不应该发生,请到火输网站报告错误");
247 
248     },
249 
250     uploadFrame: function(cb) 
251     {
252        var n = 'f' + Math.floor(Math.random() * 99999);
253        var d = document.createElement('div');
254        d.innerHTML = '<iframe style="display:none" src="about:blank" id="'+n+'" name="'+n+'" onload="EmotionMgr.uploadLoaded(\''+n+'\')"></iframe>';
255        document.body.appendChild(d);
256        var i = document.getElementById(n);
257        if (cb && typeof(cb.onComplete) == 'function') {
258            i.onComplete = cb.onComplete;
259        }
260 
261        return n;
262     },
263 
264     uploadForm: function(targetForm, name)
265     {
266        targetForm.setAttribute('target', name); 
267     }, 
268 
269     uploadFile: function(targetForm)
270     {
271        if(!this.imageFileValid)
272        { 
273           this.showAddFileMessage(false, "图案还没有成功显示或不合法的图像文件"); 
274           return false; 
275        }
276 
277        var self = this; 
278 
279        var cb = { 
280            onStart: function() { self.uploadFileCheckImage(); }, 
281            onComplete: function(p) { self.uploadFileComplete(p); }
282        }; 
283 
284        this.uploadForm(targetForm, this.uploadFrame(cb)); 
285        if (cb && typeof(cb.onStart) == 'function') {
286             return cb.onStart();
287         } else {
288             return true;
289         } 
290     }, 
291 
292     uploadLoaded : function(id) 
293     {
294         var i = document.getElementById(id);
295         if (i.contentDocument) {
296             var d = i.contentDocument;
297         } else if (i.contentWindow) {
298             var d = i.contentWindow.document;
299         } else {
300             var d = window.frames[id].document;
301         }
302         if (d.location.href == "about:blank") {
303             return;
304         }
305 
306         if (typeof(i.onComplete) == 'function') {
307             i.onComplete(d.body.innerHTML);
308         }
309     },
310 
311 
312     updateUserEmotionList: function(checkbox, emotionurl)
313     {
314        // clean up the save file message 
315        this.showSaveFileMessage(true, "<font color='#E76539'>您所作的更改还未保存</font>");
316 
317        for (var i=this.userEmotionList.length-1; i>=0; i--)
318        {
319           if(this.userEmotionList[i].url == emotionurl)
320           {
321              if(checkbox.checked)
322                this.userEmotionList[i].saved = true; 
323              else 
324                this.userEmotionList[i].saved = false; 
325 
326              return; 
327           }
328        }
329 
330        if(checkbox.checked)
331           this.userEmotionList[this.userEmotionList.length] = {url: emotionurl, saved: true}; 
332     }, 
333 
334 
335     getUserEmotionURL: function(str)
336     {
337        this.userEmotionList[this.userEmotionList.length] = { url: str, saved: true};
338     },
339 
340     loadCurrentEmotions: function()
341     {
342        // load local list 
343        var ios = FireinputXPC.getIOService(); 
344        var fileHandler = ios.getProtocolHandler("file")
345                          .QueryInterface(Components.interfaces.nsIFileProtocolHandler);
346 
347        var path = FireinputUtils.getAppRootPath();
348        var datafile = fileHandler.getFileFromURLSpec(path + "/useremotion.fireinput");
349        this.userEmotionList.length = 0;
350        if(!datafile.exists())
351        {
352           // initialize the pagination 
353           this.showCurrentEmotions(); 
354           return;
355        }
356 
357        var options = {
358           caller: this,
359           oncomplete: this.showCurrentEmotions,
360           onavailable: this.getUserEmotionURL
361        };
362        FireinputStream.loadDataAsync(datafile, options);
363     },
364 
365     showCurrentEmotions: function()
366     {
367        var handle = document.getElementById("browseFileList"); 
368        var pageinfo = {}; 
369        pageinfo.page = this.userEmotionList; 
370        pageinfo.electedpage=0; 
371 
372        if(this.userEmotionList.length <= 0)
373        {
374           this.showBrowseFileMessage(true, "您还没有加入图案");
375           this.disableBrowseSaveButton(true);
376        }
377        else
378        {
379           this.showBrowseFileMessage(true, "");
380           this.disableBrowseSaveButton(false);
381        }
382 
383        this.showSaveFileMessage(true, "");
384        Pagination.buildPages(pageinfo, 'pagecontent', 'paginatioplink', this.limitnum); 
385 
386     }, 
387    
388     getCheckedStatus: function(emotionurl)
389     {
390        for (var i=this.userEmotionList.length-1; i>=0; i--)
391        {
392           if(this.userEmotionList[i].url == emotionurl)
393           {
394              return this.userEmotionList[i].saved; 
395           }
396        }
397 
398        return false; 
399     }, 
400 
401     showMimeOrAll: function(flag)
402     {
403        this.showMyEmotions = flag;
404        // reload  
405        this.loadServerEmotions(); 
406     }, 
407 
408     loadServerNextPages: function(sindex,spage)
409     {
410        this.startindex = sindex; 
411        this.selectedpage = spage; 
412        // reload  
413        this.loadServerEmotions(); 
414     }, 
415 
416     loadServerEmotions: function()
417     {
418        var ajax = new Ajax();
419        if(!ajax)
420           return;
421 
422        var self = this;
423 
424        var url = SERVER_URL + "/emotions/viewlist.php?"; 
425        url += "startindex=" + this.startindex + "&limitnum=" + this.limitnum; 
426        if(this.showMyEmotions)
427           url += "&showmine=true"; 
428        ajax.setOptions(
429           {
430              method: 'get',
431              onSuccess: function(p) { self.loadServerEmotionSuccess(p); },
432              onFailure: function(p) { self.loadServerEmotionFailure(p); }
433           });
434        ajax.request(url);
435     }, 
436 
437     loadServerEmotionSuccess: function(p)
438     {
439        if(!p)
440           return;
441        if(p.responseText.length <= 0)
442           return;
443 
444        var jsonArray;
445        try {
446           jsonArray = eval('(' + p.responseText + ')');
447        }
448        catch(e) { };
449 
450        if(typeof(jsonArray) == 'undefined')
451           return;
452 
453        this.showBrowseFileMessage(true, "");
454        this.showSaveFileMessage(true, "");
455        this.disableBrowseSaveButton(false);
456        Pagination.buildRemotePages(jsonArray.totalcount, jsonArray.urllist, this.selectedpage, 'pagecontent', 'paginatioplink', this.limitnum); 
457     }, 
458 
459     loadServerEmotionFailure: function(p)
460     {
461        this.showBrowseFileMessage(false, "连接火输网站失败");
462        this.showSaveFileMessage(true, "");
463        this.disableBrowseSaveButton(true);
464        Pagination.buildRemotePages(0, null, 0, 'pagecontent', 'paginatioplink', this.limitnum); 
465        return; 
466     },
467  
468     showBrowseFileMessage: function(flag, message)
469     {
470        var handle = document.getElementById("browseFileMessage");
471        handle.style.color = "green";
472        if(!flag)
473        {
474           handle.style.color = "red";
475        }
476 
477        handle.innerHTML = message;
478     },
479 
480     showSaveFileMessage: function(flag, message)
481     {
482        var handle = document.getElementById("saveFileMessage");
483        handle.style.color = "green";
484        if(!flag)
485        {
486           handle.style.color = "red";
487        }
488 
489        handle.innerHTML = message;
490     },
491 
492     saveFileToList: function()
493     {
494        if(FireinputEmotionUpdater.save(this.userEmotionList, 'overwrite'))
495        {
496           this.notify();
497           this.showSaveFileMessage(true, "您所作的更改已成功保存");
498           // refresh the page 
499           this.loadCurrentEmotions();      
500        }
501        else
502           this.showSaveFileMessage(false, "保存更改-不应该发生,请到火输网站报告错误");
503     },
504 
505     disableBrowseSaveButton: function(flag)
506     {
507        var handle = document.getElementById("updateButton");
508        handle.disabled = flag; 
509     }
510 
511 
512 }; 


syntax highlighted by Code2HTML, v. 0.9.1