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 THEMES_URL = "http://www.fireinput.com/themes/themes.html";
37 const THEMES_URL = "http://www.fireinput.com/themes/themes.html";
38
39 const themeUI = [
40 {id: "fireinputThemeUser", strKey: "fireinput.theme.mime.label", attribute: "label"},
41 {id: "fireinputThemeMenu", strKey: "fireinput.theme.menu.label", attribute: "label"},
42 {id: "fireinputThemeDefault", strKey: "fireinput.theme.default.label", attribute: "label"}
43 ];
44
45
46 var FireinputThemes =
47 {
48 initialized: false,
49
50 userEmotionList: [],
51
52 load: function(forceLoad)
53 {
54 if(!this.initialized || forceLoad)
55 {
56 // get default language first
57 var defaultLanguage = fireinputPrefGetDefault("interfaceLanguage");
58 for(var i =0; i<themeUI.length; i++)
59 {
60 var id = themeUI[i].id;
61 var strKey = themeUI[i].strKey;
62 var attr = themeUI[i].attribute;
63
64 var value = FireinputUtils.getLocaleString(strKey + defaultLanguage);
65 var handle = document.getElementById(id);
66 if(!handle)
67 continue;
68 handle.setAttribute(attr, value);
69 }
70
71 this.initialized = true;
72 this.loadRemoteThemes();
73
74 // if forceLoad happens, don't register observer second time
75 if(forceLoad)
76 return;
77
78 // register an observer
79 var os = FireinputXPC.getService("@mozilla.org/observer-service;1", "nsIObserverService");
80 os.addObserver(this, "user-theme-changed", false);
81 }
82
83 },
84
85 refreshMenu: function()
86 {
87 // get default language first
88 var defaultLanguage = fireinputPrefGetDefault("interfaceLanguage");
89 for(var i =0; i<themeUI.length; i++)
90 {
91 var id = themeUI[i].id;
92 var strKey = themeUI[i].strKey;
93 var attr = themeUI[i].attribute;
94
95 var value = FireinputUtils.getLocaleString(strKey + defaultLanguage);
96 var handle = document.getElementById(id);
97 if(!handle)
98 continue;
99 handle.setAttribute(attr, value);
100 }
101
102 },
103
104 loadRemoteThemes: function()
105 {
106 var ajax = new Ajax();
107 if(!ajax)
108 return;
109
110 var self = this;
111 ajax.setOptions(
112 {
113 method: 'get',
114 onSuccess: function(p) { self.displayThemeMenu(p); },
115 onFailure: function(p) { self.displayThemeMenu(p); }
116 });
117 ajax.request(THEMES_URL);
118 },
119
120 displayThemeMenu: function(p)
121 {
122 if(!p)
123 return;
124 if(p.responseText.length <= 0)
125 return;
126
127 var jsonArray;
128 try {
129 jsonArray = eval('(' + p.responseText + ')');
130 }
131 catch(e) { };
132
133 if(typeof(jsonArray) == 'undefined')
134 return;
135
136 this.addGroup(jsonArray);
137 },
138
139 addGroup: function(jsonArray)
140 {
141 // get default language first
142 var defaultLanguage = fireinputPrefGetDefault("interfaceLanguage");
143 var openingSeparator = document.getElementById("themeOpenSeparator");
144 var closingSeparator = document.getElementById("themeCloseSeparator");
145 var themeMenu = document.getElementById("fireinputThemeMenus");
146
147 while(openingSeparator.nextSibling && openingSeparator.nextSibling != closingSeparator)
148 themeMenu.removeChild(openingSeparator.nextSibling);
149
150 // Check configured theme id
151 var currentTheme = fireinputPrefGetDefault("themeID");
152
153 for(var i=0; i < jsonArray.length; i++)
154 {
155 var data = jsonArray[i];
156
157 var id = "fireinput.theme." + data.id;
158 var menuID = document.getElementById(id);
159 var label = data.name;
160 if(!menuID)
161 {
162 var item = document.createElement("menuitem");
163 item.setAttribute("label", label);
164 item.setAttribute("class", "menuitem-iconic");
165 item.setAttribute("type", "checkbox");
166 item.setAttribute("autocheck", "false");
167 item.setAttribute("themeid", data.id);
168 item.setAttribute("fontcolor", data.fontcolor);
169 item.setAttribute("furl", data.furl);
170 item.setAttribute("iurl", data.iurl);
171 item.setAttribute("id", id);
172 item.setAttribute("oncommand", "FireinputThemes.applyTheme(event.target);");
173 item.setAttribute("checked", (currentTheme == data.id));
174
175 themeMenu.insertBefore(item, closingSeparator);
176 }
177
178 // enable the currentTheme
179 if(currentTheme == data.id)
180 this.applyThisTheme(data.fontcolor, data.furl, data.iurl);
181 }
182 },
183
184 userChooseTheme: function()
185 {
186 return false;
187 },
188
189 applyDefault: function()
190 {
191 /*
192 var containerBox = document.getElementById('fireinputIMEContainerBox');
193 containerBox.removeAttribute('fireinputtheme');
194 containerBox.style.removeProperty('background-image');
195 containerBox.style.removeProperty('color');
196 */
197 var pos = fireinputPrefGetDefault("IMEBarPosition");
198 var imeBar = document.getElementById('fireinputIMEBar_' + pos);
199 imeBar.removeAttribute('fireinputtheme');
200 imeBar.style.removeProperty('background-image');
201
202 // deselect the menu item
203 var currentTheme = fireinputPrefGetDefault("themeID");
204 var menuitemSelected = document.getElementById("fireinput.theme." + currentTheme);
205 if(menuitemSelected)
206 menuitemSelected.setAttribute("checked", false);
207
208 // update the pref
209 fireinputPrefSave('themeID', 'default');
210 },
211
212 applyTheme: function(target)
213 {
214 // deselect the menu first
215 var currentTheme = fireinputPrefGetDefault("themeID");
216 var menuitemSelected = document.getElementById("fireinput.theme." + currentTheme);
217 if(menuitemSelected)
218 menuitemSelected.setAttribute("checked", false);
219
220 // update theme
221 var fontcolor = target.getAttribute("fontcolor");
222 var furl = target.getAttribute("furl");
223 var iurl = target.getAttribute("iurl");
224 this.applyThisTheme(fontcolor, furl, iurl);
225
226 // select the menu
227 target.setAttribute("checked", true);
228 // update pref
229 fireinputPrefSave('themeID', target.getAttribute('themeid'));
230 },
231
232 applyThisTheme: function(fontcolor, furl, iurl)
233 {
234 /*
235 var containerBox = document.getElementById('fireinputIMEContainerBox');
236 containerBox.setAttribute('fireinputtheme', true);
237 // call setProperty because the traditional way won't work
238 containerBox.style.setProperty('background-image', "url('" + iurl + "')", "important");
239 containerBox.style.color = fontcolor;
240 */
241 var pos = fireinputPrefGetDefault("IMEBarPosition");
242 var imeBar = document.getElementById('fireinputIMEBar_' + pos);
243 imeBar.setAttribute('fireinputtheme', true);
244 imeBar.style.setProperty('background-image', "url('" + furl + "')", "important");
245 },
246
247 observe: function(subject, topic, data)
248 {
249 if(topic != 'fireinput-user-emotion-changed')
250 return;
251
252 // this.loadUserEmotionURL();
253 }
254 };
syntax highlighted by Code2HTML, v. 0.9.1