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 const EMOTION_URL = "http://www.fireinput.com/emotions/emotion.html"; 
 37 
 38 var FireinputEmotions = 
 39 {
 40     initialized: false, 
 41     
 42     initializeRemote: false, 
 43 
 44     mouseTooltips: "", 
 45 
 46     userEmotionList: [], 
 47 
 48     addContextSelectedImage: function()
 49     {
 50        if(FireinputEmotionUpdater.save(gContextMenu.imageURL))
 51        {
 52           this.loadUserEmotionURL();
 53        }
 54     }, 
 55 
 56     load: function()
 57     {
 58        if(!this.initialized) 
 59        {
 60           // get default language first 
 61           var defaultLanguage = FireinputPrefDefault.getInterfaceLanguage();
 62           this.mouseTooltip = FireinputUtils.getLocaleString("fireinput.emotion.mouse.tooltips" + defaultLanguage); 
 63 
 64           var element = document.getElementById("fireinputEmotionMenu"); 
 65           var label = FireinputUtils.getLocaleString("fireinput.emotion.label" + defaultLanguage);
 66           element.setAttribute("label", label);
 67           this.initialized = true; 
 68           this.loadUserEmotionURL(); 
 69 
 70           // register an observer 
 71           var os = Components.classes["@mozilla.org/observer-service;1"]
 72                        .getService(Components.interfaces.nsIObserverService);
 73           os.addObserver(this, "user-emotion-changed", false);
 74        }
 75 
 76     },
 77 
 78     addUserEmotionMenu: function()
 79     {
 80        var defaultLanguage = FireinputPrefDefault.getInterfaceLanguage();
 81        var menuElement = document.getElementById("fireinputEmotionMenuItems");
 82 
 83        // user action menu 
 84        var id = "fireinput.emotion.user.action"; 
 85        var label = FireinputUtils.getLocaleString(id + defaultLanguage); 
 86        var menuID = document.getElementById(id); 
 87        if(!menuID)
 88        {
 89           var subMenu = document.createElement("menuitem");
 90           subMenu.setAttribute("label", label);
 91           subMenu.setAttribute("id", id);
 92           subMenu.onclick=bind(function(event)
 93                                 { 
 94                                     this.showAddEmotionDialog(event);
 95                                 }, this);
 96 
 97           menuElement.appendChild(subMenu);
 98 
 99        }
100 
101        // user img list 
102        id = "fireinput.emotion.user.list"; 
103        menuID = document.getElementById(id);
104        label = FireinputUtils.getLocaleString(id + defaultLanguage); 
105        if(!menuID)
106        {
107           var subMenu = document.createElement("menu");
108           var subMenupopup = document.createElement("menupopup");
109           subMenu.setAttribute("label", label);
110           subMenu.setAttribute("id", id);
111 
112           this.addGroupEmotion(subMenupopup, this.userEmotionList);
113           subMenupopup.setAttribute("id", id + "_popupmenu");
114 
115           subMenu.appendChild(subMenupopup);
116           menuElement.appendChild(subMenu);
117        }
118        else 
119        {
120           while(menuID.hasChildNodes())
121           {
122              menuID.removeChild(menuID.lastChild);
123           }
124 
125           var subMenupopup = document.createElement("menupopup");
126 
127           this.addGroupEmotion(subMenupopup, this.userEmotionList);
128           subMenupopup.setAttribute("id", id + "_popupmenu");
129           menuID.appendChild(subMenupopup);
130 
131        }
132 
133        // remote emotion 
134        if(!this.initializeRemote)
135          this.loadRemoteEmotions(); 
136     }, 
137 
138     loadUserEmotionURL: function()
139     {
140        var ios = IOService.getService(Components.interfaces.nsIIOService);
141        var fileHandler = ios.getProtocolHandler("file")
142                          .QueryInterface(Components.interfaces.nsIFileProtocolHandler);
143 
144        var path = FireinputUtils.getAppRootPath();
145        var datafile = fileHandler.getFileFromURLSpec(path + "/useremotion.fireinput"); 
146        this.userEmotionList.length = 0; 
147        if(!datafile.exists())
148        {
149           // add user's other menu 
150           this.addUserEmotionMenu(); 
151           return;
152        }
153  
154        var options = {
155           caller: this,
156           oncomplete: this.addUserEmotionMenu, 
157           onavailable: this.getUserEmotionURL
158        };
159        FireinputStream.loadDataAsync(datafile, options);
160     }, 
161 
162     getUserEmotionURL: function(str)
163     {
164        this.userEmotionList[this.userEmotionList.length] = str; 
165     }, 
166 
167     showAddEmotionDialog: function()
168     {
169         gBrowser.selectedTab = gBrowser.addTab("chrome://fireinput/content/emotionmgr/emotionmgr.html");
170     },
171 
172     loadRemoteEmotions: function()
173     {
174        var ajax = new Ajax(); 
175        if(!ajax)
176           return; 
177 
178        var self = this; 
179 
180        ajax.setOptions(
181           { 
182              method: 'get',
183              onSuccess: function(p) { self.displayEmotionMenu(p); },
184              onFailure: function(p) { self.displayEmotionMenu(p); }
185           }); 
186        ajax.request(EMOTION_URL); 
187     }, 
188 
189     displayEmotionMenu: function(p)
190     {
191        if(!p)
192           return; 
193        if(p.responseText.length <= 0)
194           return; 
195 
196        var jsonArray; 
197        try {
198           jsonArray = eval('(' + p.responseText + ')'); 
199        }
200        catch(e) { }; 
201 
202        if(typeof(jsonArray) == 'undefined')
203           return; 
204 
205        this.initializeRemote = true; 
206        this.addGroup(jsonArray);
207     },
208 
209     refreshMenu: function()
210     {
211        if(!this.initialized)
212           return; 
213 
214        var defaultLanguage = FireinputPrefDefault.getInterfaceLanguage();
215 
216        this.mouseTooltip = FireinputUtils.getLocaleString("fireinput.emotion.mouse.tooltips" + defaultLanguage); 
217 
218        var myMenuID = document.getElementById("fireinput.emotion.user.list");
219        var myLabel = FireinputUtils.getLocaleString("fireinput.emotion.user.list" + defaultLanguage);
220        myMenuID.setAttribute("label", myLabel);
221 
222        var actionMenuID = document.getElementById("fireinput.emotion.user.action");
223        var aLabel = FireinputUtils.getLocaleString("fireinput.emotion.user.action" + defaultLanguage);
224        actionMenuID.setAttribute("label", aLabel);
225 
226        var element = document.getElementById("fireinputEmotionMenu"); 
227        var label = FireinputUtils.getLocaleString("fireinput.emotion.label" + defaultLanguage);
228        element.setAttribute("label", label);
229 
230        element = document.getElementById("fireinputEmotionMenuItems");
231 
232        for (var child = element.firstChild; child; child = child.nextSibling)
233        {
234           var label = ""; 
235           if(defaultLanguage != "")
236              label = child.getAttribute("categoryname"); 
237           else
238              label = child.getAttribute("category"); 
239 
240           if(label && label.length > 0)
241              child.setAttribute("label", label); 
242 
243           var images = child.getElementsByTagName("image"); 
244           if(!images)
245              continue; 
246 
247           for(var i=images.length-1; i>=0; i--)
248           {
249              images[i].setAttribute("tooltiptext", this.mouseTooltip); 
250           }
251        }
252     },
253 
254 
255     addGroup: function(jsonArray)
256     {
257        // get default language first 
258        var defaultLanguage = FireinputPrefDefault.getLanguage();
259        var menuElement = document.getElementById("fireinputEmotionMenuItems");
260 
261        for(var i=0; i < jsonArray.length; i++)
262        {
263           var data = jsonArray[i]; 
264           var groupName = data.name; 
265  
266           if(defaultLanguage != LANGUAGE_ZH)
267              groupName = data.category; 
268 
269           var id = "fireinput.emotion." + data.category; 
270           var menuID = document.getElementById(id); 
271           var label = groupName; 
272           if(!menuID)
273           { 
274              var subMenu = document.createElement("menu"); 
275              var subMenupopup = document.createElement("menupopup"); 
276              subMenu.setAttribute("label", label); 
277              subMenu.setAttribute("categoryname", data.name); 
278              subMenu.setAttribute("category", data.category); 
279              subMenu.setAttribute("id", id); 
280 
281              this.addGroupEmotion(subMenupopup, data.urllist); 
282              subMenupopup.setAttribute("id", id +"_menupopup"); 
283 
284              subMenu.appendChild(subMenupopup); 
285              menuElement.appendChild(subMenu); 
286 
287           }
288           else 
289           {
290              menuID.setAttribute("label", label); 
291           }
292        }
293     },
294 
295     addGroupEmotion: function(menuItem, urllist)
296     {
297        var num = 5; 
298 
299        if(urllist.length > 50)
300            num = 10; 
301 
302        for(var i=urllist.length-1; i>=0;)
303        {
304  
305           var toolbar = document.createElement("toolbar"); 
306           toolbar.setAttribute("class", "specialcharbar"); 
307           var j=0; 
308           for(j=0; j<num && (i-j) >=0; j++)
309           {
310              var label = document.createElement("label"); 
311              label.setAttribute("class", "specialcharlabel");
312              label.setAttribute("hiddenvalue", urllist[i-j]);
313                   
314              label.onclick=bind(function(event) 
315                                 { if(event.button == 2) 
316                                     this.copyIntoClipboard(event); 
317                                   else 
318                                     Fireinput.insertSpecialCharAt(event, IMAGE_SOURCE_TYPE); 
319                                 }, this); 
320              var img = document.createElement("image");
321              img.setAttribute("src", urllist[i-j]);
322              img.setAttribute("height", 32);
323              img.setAttribute("width", 32);
324              img.setAttribute("value", urllist[i-j]);
325              img.setAttribute("tooltiptext", this.mouseTooltip); 
326              label.appendChild(img);
327              toolbar.appendChild(label); 
328           }
329           i-= j; 
330           
331           menuItem.appendChild(toolbar); 
332        }
333     },
334 
335     copyIntoClipboard: function(event)
336     {
337        var clickTarget = event.target;
338        var value = clickTarget.getAttribute("hiddenvalue");
339        if(!value)
340           value = clickTarget.getAttribute("value");
341 
342        try 
343        {
344           var clipboard = Components.classes["@mozilla.org/widget/clipboardhelper;1"]
345                                     .getService(Components.interfaces.nsIClipboardHelper);
346           clipboard.copyString(value); 
347        } 
348        catch(e) {}
349     },
350 
351     observe: function(subject, topic, data)
352     {
353        if(topic != 'user-emotion-changed')
354           return; 
355 
356        this.loadUserEmotionURL(); 
357     }
358 };              


syntax highlighted by Code2HTML, v. 0.9.1