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 tableManagmentUI = [
38 {id: "fireinputTableManagement", strKey: "fireinput.table.management.label", attribute: "label"}
39 ];
40
41 var FireinputTable =
42 {
43 debug: 1,
44
45 updateTimer: null,
46 isUpdating: false,
47 initialized: false,
48
49 load: function(forceLoad)
50 {
51 if(this.initialized && !forceLoad)
52 return;
53
54 this.refreshMenu();
55
56 // register an observer
57 var os = FireinputXPC.getService("@mozilla.org/observer-service;1", "nsIObserverService");
58 os.addObserver(this, "fireinput-table-update-request", false);
59
60 // check new table words
61 this.checkTableUpdate();
62 },
63
64 observe: function(subject, topic, data)
65 {
66
67 if(topic != 'fireinput-table-update-request')
68 return;
69
70 this.checkTableUpdate(true);
71 },
72
73
74 refreshMenu: function()
75 {
76 // get default language first
77 var defaultLanguage = fireinputPrefGetDefault("interfaceLanguage");
78 // update UI
79 for(var i =0; i<tableManagmentUI.length; i++)
80 {
81 var id = tableManagmentUI[i].id;
82 var handle = document.getElementById(id);
83 if(!handle)
84 continue;
85
86 var strKey = tableManagmentUI[i].strKey;
87 var attr = tableManagmentUI[i].attribute;
88
89 var value = FireinputUtils.getLocaleString(strKey + defaultLanguage);
90 // to check whether the shortcut keystring exists
91 var found =value.match(/%(.+)%/i);
92 if(found)
93 {
94 var keystring = FireinputKeyBinding.getKeyString(found[1]);
95 value = value.replace(found[0], keystring);
96 }
97
98 handle.setAttribute(attr, value);
99 }
100 },
101
102 showDialog: function()
103 {
104 FireinputUtils.loadURI("chrome://fireinput/content/tablemgr/tablemgr.html");
105 },
106
107 isBeingUpdated: function()
108 {
109 return this.isUpdating;
110 },
111
112 showUpdatingProgress: function(showflag)
113 {
114 var handle = document.getElementById('fireinputTableUpdatePanel');
115 if(showflag)
116 {
117 this.isUpdating = true;
118 if(handle)
119 handle.style.display = ""; // don't put block here as it will not align with other menu well
120
121 var defaultLanguage = fireinputPrefGetDefault("interfaceLanguage");
122 var value = FireinputUtils.getLocaleString('fireinput.table.updating.label' + defaultLanguage);
123 var h = document.getElementById('fireinputTableUpdate');
124 if(h)
125 h.setAttribute('label', value);
126 }
127 else
128 {
129 this.isUpdating = false;
130 if(handle)
131 handle.style.display = "none";
132 var h = document.getElementById('fireinputTableUpdate');
133 if(h)
134 h.setAttribute('label', '');
135 }
136
137 },
138
139 checkTableUpdate: function(force)
140 {
141 if(this.isBeingUpdated())
142 return;
143
144 var lastupdate = fireinputPrefGetDefault("lastTableUpdate");
145 var intervalInHour = fireinputPrefGetDefault("tableUpdateInterval");
146 if(lastupdate.length <= 0)
147 {
148
149 // find out the fireinput installation time
150 var installpath = FireinputUtils.getAppRootPath() + "/extensions/fireinput@software.fireinput.com";
151 var pathUrl = FireinputXPC.getIOService().newURI(installpath, null, null).QueryInterface(Components.interfaces.nsIFileURL);
152 var path = pathUrl.file;
153 lastupdate = path.lastModifiedTime / 1000;
154 }
155
156 if(this.updateTimer)
157 clearTimeout(this.updateTimer);
158
159 if(!force && intervalInHour <= 0)
160 {
161 // perodically loop to see if the interval has been changed.
162 var timeout = 30 * 60 * 1000;
163 this.updateTimer = setTimeout(function() { FireinputTable.checkTableUpdate(); }, timeout);
164 }
165 else
166 {
167 var last = new Date(lastupdate).getTime() - 10000000;
168 var now = new Date().getTime();
169 var timeout = intervalInHour * 3600 * 1000 - (now - last);
170 timeout = (!force && timeout > 0) ? timeout: 2000; // give 2 seconds window
171 lastupdatetime = last / 1000;
172
173 FireinputLog.debug(this, "lastupdatetime: " + lastupdatetime + ", timeout: " + timeout);
174 this.updateTimer = setTimeout(function() { FireinputTable.startTableUpdate(lastupdatetime); }, timeout);
175 }
176 },
177
178 startTableUpdate: function(lastupdatetime)
179 {
180 var ajax = new Ajax();
181 if(!ajax)
182 return;
183
184 var self = this;
185
186 ajax.setOptions(
187 {
188 method: 'get',
189 onSuccess: function(p) { self.processLatestTableUpdate(p); self.checkTableUpdate(); },
190 onFailure: function(p) { self.processLatestTableUpdate(p, true); self.checkTableUpdate();}
191 });
192
193 this.showUpdatingProgress(true);
194
195 var ime = Fireinput.getCurrentIME();
196 var params = "imetype=" + encodeURIComponent(ime.getIMEType()) +
197 "&lastupdate=" + encodeURIComponent(lastupdatetime);
198
199 ajax.request(SERVER_URL + "/table/getlatest.php?" + params);
200 },
201
202 processLatestTableUpdate: function(p, error)
203 {
204 if(!p || p.responseText.length <= 0)
205 {
206 this.showUpdatingProgress(false);
207 // if not error, set lastUpdateTime
208 if(!error)
209 fireinputPrefSave('lastTableUpdate', (new Date()).toString());
210
211 return;
212 }
213
214 // the input is UTF-8, we need to convert as we save raw bytes in memory
215 var words = FireinputUnicode.convertFromUnicode(p.responseText);
216 var jsonArray;
217 try {
218 jsonArray = eval('(' + words + ')');
219 }
220 catch(e) { alert(e); };
221
222 if(typeof(jsonArray) == 'undefined')
223 {
224 this.showUpdatingProgress(false);
225 return;
226 }
227
228 FireinputLog.debug(this, FireinputUnicode.getUnicodeString(jsonArray.toString()));
229 this.processLatestTable(jsonArray);
230 this.showUpdatingProgress(false);
231 },
232
233 processLatestTable: function(tableArray)
234 {
235
236 var current = new Date();
237 try {
238
239 var ime = Fireinput.getCurrentIME();
240 ime.storeUpdatePhrases(tableArray);
241 fireinputPrefSave('lastTableUpdate', current.toString());
242
243 } catch(e) {};
244 }
245
246 };
syntax highlighted by Code2HTML, v. 0.9.1