Mercurial > tinyurl_creator
comparison chrome/content/saved.js @ 0:c14d52a3b2fe
initial import
author | Yoshiki Yazawa <yaz@honeyplanet.jp> |
---|---|
date | Mon, 11 Aug 2008 20:34:21 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c14d52a3b2fe |
---|---|
1 | |
2 var oProps = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties); | |
3 var sUserChrome = (oProps.get("UChrm", Components.interfaces.nsIFile)).path; | |
4 var sPathSeperator = "\\"; | |
5 if(sUserChrome[0].indexOf("/") > -1) sPathSeperator = "/"; | |
6 | |
7 var sTinyRdfName = "tinyurl.rdf"; | |
8 var sTinyRdfPath = sUserChrome + sPathSeperator + sTinyRdfName; | |
9 | |
10 // RDF Vars | |
11 var gTinyRdf = null; | |
12 var gTinyDs = null; | |
13 var gTinyRdfUtil = null; | |
14 var gTinyRdfC = null; | |
15 var gTinyRdfRoot = "http://jgillick.nettripper.com/tinyurl"; | |
16 | |
17 // Create RDF File if it does not exist | |
18 function tinyurl_saved_rdfCreate(){ | |
19 try{ | |
20 | |
21 var oFileLocal = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile); | |
22 oFileLocal.initWithPath(sTinyRdfPath); | |
23 | |
24 // Doesn't Exist? | |
25 if(!oFileLocal.exists()){ | |
26 | |
27 // Read RDF | |
28 var oRequest = new XMLHttpRequest(); | |
29 oRequest.open("GET", "chrome://tinyurl/content/"+ sTinyRdfName, false); | |
30 oRequest.send(""); | |
31 var sContent = oRequest.responseText; | |
32 | |
33 // Write | |
34 oFileLocal.create(0x00, 0666); | |
35 var oFileOut = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream); | |
36 oFileOut.init(oFileLocal, 0x20 | 0x02, 00666, null); | |
37 oFileOut.write(sContent, sContent.length); | |
38 oFileOut.flush(); | |
39 oFileOut.close(); | |
40 } | |
41 | |
42 }catch(err){ } | |
43 | |
44 } | |
45 | |
46 // Enable Buttons | |
47 function tinyurl_saved_disable(dis){ | |
48 document.getElementById('go-saved-button').disabled = dis; | |
49 document.getElementById('copy-saved-button').disabled = dis; | |
50 document.getElementById('edit-saved-button').disabled = dis; | |
51 document.getElementById('remove-saved-button').disabled = dis; | |
52 | |
53 if(dis){ | |
54 document.getElementById('go-saved-button').image = "chrome://tinyurl/content/icons/go_off.png"; | |
55 document.getElementById('copy-saved-button').image = "chrome://tinyurl/content/icons/copy_off.png"; | |
56 document.getElementById('edit-saved-button').image = "chrome://tinyurl/content/icons/edit_off.png"; | |
57 document.getElementById('remove-saved-button').image = "chrome://tinyurl/content/icons/remove_off.png"; | |
58 } | |
59 else{ | |
60 document.getElementById('go-saved-button').image = "chrome://tinyurl/content/icons/go.png"; | |
61 document.getElementById('copy-saved-button').image = "chrome://tinyurl/content/icons/copy.png"; | |
62 document.getElementById('edit-saved-button').image = "chrome://tinyurl/content/icons/edit.png"; | |
63 document.getElementById('remove-saved-button').image = "chrome://tinyurl/content/icons/remove.png"; | |
64 } | |
65 } | |
66 | |
67 // Goto Saved Url | |
68 function tinyurl_saved_go(){ | |
69 var sUri = oSavedTree.contentView.getItemAtIndex(oSavedTree.currentIndex).getAttribute("uri"); | |
70 | |
71 // Get Real Url | |
72 try{ | |
73 var aProps = tinyurl_saved_getEntry(sUri); | |
74 | |
75 if(opener){ | |
76 opener.gBrowser.selectedTab = opener.gBrowser.addTab(aProps["realurl"]); | |
77 } | |
78 else{ | |
79 window.open(aProps["realurl"]); | |
80 } | |
81 | |
82 setTimeout("self.focus()", 100); | |
83 | |
84 }catch(err) { alert("Error: 560\n Could not access that URL\n"+ err); } | |
85 } | |
86 | |
87 // Copy TinyUrl | |
88 function tinyurl_saved_copy(){ | |
89 if(oSavedTree.currentIndex > -1){ | |
90 var sUri = oSavedTree.contentView.getItemAtIndex(oSavedTree.currentIndex).getAttribute("uri"); | |
91 try{ | |
92 var aProps = tinyurl_saved_getEntry(sUri); | |
93 tinyurl_copyText(aProps["tinyurl"]); | |
94 }catch(err){ alert("Error: 580\n Could not copy that URL\n"+ err); } | |
95 } | |
96 } | |
97 | |
98 // Open Edit Dialog | |
99 function tinyurl_saved_openEdit(){ | |
100 var sUri = oSavedTree.contentView.getItemAtIndex(oSavedTree.currentIndex).getAttribute("uri"); | |
101 | |
102 if(sUri != null && sUri != "") | |
103 window.openDialog('chrome://tinyurl/content/tinyurl_edit.xul','TinyUrlEditDialog','centerscreen, chrome, modal, resizable', sUri).focus() | |
104 } | |
105 | |
106 // Read RDF Entries | |
107 function tinyurl_saved_read(){ | |
108 if(oSavedTreeItems == null) | |
109 return; | |
110 | |
111 try{ | |
112 tinyurl_saved_initRdf(); | |
113 | |
114 // Remove All Existing | |
115 while(oSavedTreeItems.childNodes.length > 0) | |
116 oSavedTreeItems.removeChild(oSavedTreeItems.firstChild); | |
117 | |
118 // Read From RDF | |
119 var aProps = null; | |
120 var oTrgt = null; | |
121 var oTreeItem = null; | |
122 var oTreeRow = null; | |
123 var oTreeCell = null; | |
124 var aElements = gTinyRdfC.GetElements(); | |
125 while(aElements.hasMoreElements()){ | |
126 try{ | |
127 var oRes = aElements.getNext(); | |
128 oRes = oRes.QueryInterface(Components.interfaces.nsIRDFResource); | |
129 | |
130 oTreeItem = document.createElement("treeitem"); | |
131 oTreeItem.setAttribute("uri", oRes.Value); | |
132 oTreeRow = oTreeItem.appendChild(document.createElement("treerow")); | |
133 | |
134 aProps = tinyurl_saved_getEntry(oRes.Value); | |
135 | |
136 // Title | |
137 oTreeRow.appendChild(document.createElement("treecell")).setAttribute("label", aProps["title"]) | |
138 // TinyUrl | |
139 oTreeRow.appendChild(document.createElement("treecell")).setAttribute("label", aProps["tinyurl"]) | |
140 // Real Url | |
141 oTreeRow.appendChild(document.createElement("treecell")).setAttribute("label", aProps["realurl"]) | |
142 | |
143 oSavedTreeItems.appendChild(oTreeItem); | |
144 | |
145 }catch(err){ } | |
146 } | |
147 | |
148 tinyurl_saved_disable(true); | |
149 | |
150 }catch(err) { alert("Error 500\nAn unknown error occurred loading saved data\n"+ err); } | |
151 } | |
152 | |
153 // Remvoe RDF Entry | |
154 function tinyurl_saved_remove(){ | |
155 | |
156 var sUri = oSavedTree.contentView.getItemAtIndex(oSavedTree.currentIndex).getAttribute("uri"); | |
157 var iSelect = oSavedTree.currentIndex; | |
158 | |
159 if(sUri != null && sUri != "" && confirm("Are you SURE you want to remove this entry?")){ | |
160 try{ | |
161 tinyurl_saved_initRdf(); | |
162 | |
163 var oRes = gTinyRdf.GetResource(sUri); | |
164 | |
165 // Remove All Archs | |
166 var aArchs = null; | |
167 var hasArchs = true; | |
168 while(hasArchs){ | |
169 aArchs = gTinyDs.ArcLabelsOut(oRes); | |
170 hasArchs = aArchs.hasMoreElements(); | |
171 while(aArchs.hasMoreElements()){ | |
172 oArch = aArchs.getNext().QueryInterface(Components.interfaces.nsIRDFResource); | |
173 | |
174 //Remove | |
175 gTinyDs.Unassert(oRes, oArch, gTinyDs.GetTarget(oRes, oArch, true)); | |
176 } | |
177 } | |
178 | |
179 // Remove Element | |
180 gTinyRdfC.RemoveElement(oRes, true); | |
181 | |
182 // Rewrite Tree | |
183 tinyurl_saved_read() | |
184 | |
185 // Select Row | |
186 if(iSelect > oSavedTree.view.rowCount - 1){ | |
187 oSavedTree.view.selection.select(oSavedTree.view.rowCount - 1); | |
188 } | |
189 else{ | |
190 oSavedTree.view.selection.select(iSelect); | |
191 } | |
192 | |
193 }catch(err){ alert("Error 520\nCould not remove that entry.\n"+ err); } | |
194 } | |
195 } | |
196 | |
197 // Add RDF Entry | |
198 function tinyurl_saved_add(tinyUrl, realUrl, title){ | |
199 var sTitle = ""; | |
200 var sTimestamp = (new Date()).getTime(); | |
201 var sTiny = tinyUrl; | |
202 var sReal = realUrl; | |
203 var sPageTitle = (title == null) ? "" : title; | |
204 var sUri = "http://jgillick.nettripper.com/tinyurl/"; | |
205 var oRes = null; | |
206 | |
207 tinyurl_saved_rdfCreate(); | |
208 | |
209 // Get Uri | |
210 if(sTiny.length > "http://tinyurl.com/".length){ | |
211 sUri += sTiny.substring(("http://tinyurl.com/".length)); | |
212 } | |
213 else{ sUri = null; } | |
214 | |
215 // Is this TinyUrl already saved? | |
216 try{ | |
217 tinyurl_saved_initRdf(); | |
218 oRes = gTinyRdf.GetResource(sUri); | |
219 | |
220 if(gTinyRdfC.IndexOf(oRes) > -1){ | |
221 alert("You have already saved a TinyUrl entry for that URL.") | |
222 return false; | |
223 } | |
224 }catch(err){ } | |
225 | |
226 if(!sTiny || !sReal) | |
227 return; | |
228 | |
229 // Check Uri | |
230 if(sUri == null){ | |
231 alert("Error 540\nCould not save that TinyUrl.\n"+ tinyUrl); | |
232 return false; | |
233 } | |
234 | |
235 // Get Title | |
236 while(true){ | |
237 sTitle = prompt("Title to save it under:", sPageTitle); | |
238 | |
239 // Empty | |
240 if(sTitle == ""){ | |
241 alert("You must enter a title."); | |
242 } | |
243 // null (cancel) | |
244 else if(sTitle == null){ | |
245 break; | |
246 } | |
247 // Duplicate | |
248 else if(tinyurl_saved_getUriForTitle(sTitle) != null){ | |
249 alert("A saved TinyUrl entry already exists with that title."); | |
250 sTitle | |
251 } | |
252 else{ | |
253 break; | |
254 } | |
255 } | |
256 | |
257 if(sTitle != null){ | |
258 try{ | |
259 tinyurl_saved_initRdf() | |
260 var oRes = gTinyRdf.GetResource(sUri); | |
261 gTinyRdfC.AppendElement(oRes); | |
262 gTinyDs.Assert(oRes, gTinyRdf.GetResource("http://jgillick.nettripper.com/tinyurl/rdf#title"), gTinyRdf.GetLiteral(sTitle), true); | |
263 gTinyDs.Assert(oRes, gTinyRdf.GetResource("http://jgillick.nettripper.com/tinyurl/rdf#tinyurl"), gTinyRdf.GetLiteral(sTiny), true); | |
264 gTinyDs.Assert(oRes, gTinyRdf.GetResource("http://jgillick.nettripper.com/tinyurl/rdf#realurl"), gTinyRdf.GetLiteral(sReal), true); | |
265 gTinyDs.Assert(oRes, gTinyRdf.GetResource("http://jgillick.nettripper.com/tinyurl/rdf#created"), gTinyRdf.GetLiteral(sTimestamp), true); | |
266 | |
267 | |
268 // Rewrite Tree | |
269 tinyurl_saved_read(); | |
270 | |
271 return true; | |
272 }catch(err){ | |
273 alert("Error 541\nCould not save that TinyUrl.\n"+ err); | |
274 } | |
275 } | |
276 | |
277 return false; | |
278 } | |
279 | |
280 // Get favicon.ico for url | |
281 function tinyurl_saved_getFavIcon(sUrl){ | |
282 if(opener){ | |
283 | |
284 // Get Url for favicon.ico | |
285 var oUri = Components.classes["@mozilla.org/network/standard-url;1"].createInstance(Components.interfaces.nsIURI); | |
286 oUri.spec = sUrl; | |
287 var sFavUri = opener.gBrowser.buildFavIconString(oUri); | |
288 | |
289 // Verify Exists | |
290 try{ | |
291 var oRequest = new XMLHttpRequest(); | |
292 oRequest.open("GET", sFavUri, false); | |
293 oRequest.send(""); | |
294 | |
295 if(oRequest.status == 200) | |
296 return sFavUri; | |
297 | |
298 }catch(err) { } | |
299 } | |
300 | |
301 return ""; | |
302 } | |
303 | |
304 | |
305 // Get Data From Entry | |
306 function tinyurl_saved_getEntry(sUri){ | |
307 var aProps = new Array(); | |
308 | |
309 try{ | |
310 tinyurl_saved_initRdf(); | |
311 | |
312 var oRes = gTinyRdf.GetResource(sUri); | |
313 | |
314 // Get Resources | |
315 aProps["title"] = gTinyDs.GetTarget(oRes, gTinyRdf.GetResource("http://jgillick.nettripper.com/tinyurl/rdf#title"), true); | |
316 aProps["tinyurl"] = gTinyDs.GetTarget(oRes, gTinyRdf.GetResource("http://jgillick.nettripper.com/tinyurl/rdf#tinyurl"), true); | |
317 aProps["realurl"] = gTinyDs.GetTarget(oRes, gTinyRdf.GetResource("http://jgillick.nettripper.com/tinyurl/rdf#realurl"), true); | |
318 aProps["created"] = gTinyDs.GetTarget(oRes, gTinyRdf.GetResource("http://jgillick.nettripper.com/tinyurl/rdf#created"), true); | |
319 | |
320 // Get Values | |
321 (aProps["title"] instanceof Components.interfaces.nsIRDFLiteral) ? aProps["title"] = aProps["title"].Value : aProps["title"] = ""; | |
322 (aProps["tinyurl"] instanceof Components.interfaces.nsIRDFLiteral) ? aProps["tinyurl"] = aProps["tinyurl"].Value : aProps["tinyurl"] = ""; | |
323 (aProps["realurl"] instanceof Components.interfaces.nsIRDFLiteral) ? aProps["realurl"] = aProps["realurl"].Value : aProps["realurl"] = ""; | |
324 (aProps["created"] instanceof Components.interfaces.nsIRDFLiteral) ? aProps["created"] = aProps["created"].Value : aProps["created"] = ""; | |
325 | |
326 }catch(err){ throw err; } | |
327 | |
328 return aProps; | |
329 } | |
330 | |
331 // Get Uri for title | |
332 function tinyurl_saved_getUriForTitle(sTitle){ | |
333 try{ | |
334 tinyurl_saved_initRdf(); | |
335 | |
336 var oSubject = gTinyDs.GetSource(gTinyRdf.GetResource("http://jgillick.nettripper.com/tinyurl/rdf#title"), gTinyRdf.GetLiteral(sTitle), true); | |
337 if(oSubject instanceof Components.interfaces.nsIRDFResource) | |
338 return oSubject.Value | |
339 | |
340 }catch(err) { throw err; } | |
341 | |
342 return null; | |
343 } | |
344 | |
345 // Change Title to sTitle for sUri | |
346 function tinyurl_saved_changeTitle(sUri, sTitle){ | |
347 try{ | |
348 tinyurl_saved_initRdf(); | |
349 | |
350 var oRes = gTinyRdf.GetResource(sUri); | |
351 var oProp = gTinyRdf.GetResource("http://jgillick.nettripper.com/tinyurl/rdf#title"); | |
352 var sOld = gTinyDs.GetTarget(oRes, oProp, true); // Old Value | |
353 | |
354 //Change | |
355 gTinyDs.Change(oRes, oProp, sOld, gTinyRdf.GetLiteral(sTitle)); | |
356 | |
357 }catch(err) { throw err; } | |
358 | |
359 return null; | |
360 } | |
361 | |
362 | |
363 // Init RDF Objects | |
364 function tinyurl_saved_initRdf(){ | |
365 try{ | |
366 if(gTinyRdf == null) | |
367 gTinyRdf = Components.classes["@mozilla.org/rdf/rdf-service;1"].getService(Components.interfaces.nsIRDFService); | |
368 if(gTinyDs == null) | |
369 gTinyDs = gTinyRdf.GetDataSourceBlocking("file:///"+ sTinyRdfPath); | |
370 if(gTinyRdfUtil == null) | |
371 gTinyRdfUtil = Components.classes["@mozilla.org/rdf/container-utils;1"].getService(Components.interfaces.nsIRDFContainerUtils); | |
372 if(gTinyRdfC == null){ | |
373 gTinyRdfC = Components.classes["@mozilla.org/rdf/container;1"].createInstance(Components.interfaces.nsIRDFContainer); | |
374 | |
375 //Get or Add Sequence | |
376 if(gTinyRdfUtil.IsSeq(gTinyDs, gTinyRdf.GetResource(gTinyRdfRoot))){ | |
377 gTinyRdfC.Init(gTinyDs, gTinyRdf.GetResource(gTinyRdfRoot)); | |
378 }else{ | |
379 gTinyRdfC = gTinyRdfUtil.MakeSeq(gTinyDs, gTinyRdf.GetResource(gTinyRdfRoot)); //Create | |
380 } | |
381 } | |
382 | |
383 }catch(err){throw err} | |
384 } |