OSDN Git Service

import source-tree based svn r84.
[bluegriffon/BlueGriffon.git] / sidebars / projectManager / content / url-loader.js
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 Original Code is Nvu.
15  *
16  * The Initial Developer of the Original Code is
17  * Linspire Inc..
18  * Portions created by the Initial Developer are Copyright (C) 2003-2005
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  *   Daniel Glazman (glazman@disruptive-innovations.com), original author
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either the GNU General Public License Version 2 or later (the "GPL"), or
26  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */
37
38
39 const IOSERVICE_CTRID           = "@mozilla.org/network/io-service;1";
40 const cnsIIOService             = Components.interfaces.nsIIOService;
41 const SIS_CTRID                 = "@mozilla.org/scriptableinputstream;1"
42 const cnsIScriptableInputStream = Components.interfaces.nsIScriptableInputStream;
43 const cnsIChannel               = Components.interfaces.nsIChannel;
44 const cnsIInputStream           = Components.interfaces.nsIInputStream;
45 const cnsIRequest               = Components.interfaces.nsIRequest;
46 const cnsIDirectoryListing      = Components.interfaces.nsIDirectoryListing;
47
48 var gError;
49 var gCurrentChannel;
50
51 function _getChannelForURL (url)
52 {
53     var serv = Components.classes[IOSERVICE_CTRID].getService(cnsIIOService);
54     if (!serv)
55         return null;
56     
57     return serv.newChannel(url, "UTF-8", null);
58
59 }
60
61 function StreamListener(channel, url, observer)
62 {
63     this.channel = channel;
64     this.data = "";
65     this.url = url;
66     this.observer = observer;
67 }
68
69 StreamListener.prototype.onStartRequest =
70 function (request, context)
71 {
72 }
73
74 StreamListener.prototype.onStopRequest =
75 function (request, context, status)
76 {
77     // close the channel
78     this.channel.cancel(0x804b0002); // NS_BINDING_ABORTED
79
80     if (this.observer &&
81         typeof this.observer.onComplete == "function")
82         this.observer.onComplete (this.data, this.url, status);
83 }
84
85 StreamListener.prototype.onDataAvailable =
86 function (request, context, inStr, sourceOffset, count)
87 {
88     // dump ("onDataAvailable(): " + count + "\n");
89     // sometimes the inStr changes between onDataAvailable calls, so we
90     // can't cache it.
91     /*var sis = 
92         Components.classes[SIS_CTRID].createInstance(cnsIScriptableInputStream);
93     sis.init(inStr);
94     var str = sis.read(count);*/
95     var sis = Components.classes['@mozilla.org/binaryinputstream;1']
96                             .createInstance(Components.interfaces.nsIBinaryInputStream);
97     sis.setInputStream(inStr);
98     var str = sis.readBytes(count);
99
100     this.data += str;
101 }
102
103
104
105 function loadURLAsync (url, observer)
106 {
107   var chan = _getChannelForURL (url);
108   chan.loadFlags |= cnsIRequest.LOAD_BYPASS_CACHE;
109   
110   chan.contentType = "application/http-index-format";
111   gCurrentChannel = chan;
112
113   return chan.asyncOpen (new StreamListener (chan, url, observer), null);
114 }
115
116 function newFileInputStream(file, buffered)
117 {
118   const PR_RDONLY = 0x1;
119   var stream =
120       Components.classes["@mozilla.org/network/file-input-stream;1"].
121       createInstance(Components.interfaces.nsIFileInputStream);
122   stream.init(file, PR_RDONLY, 0, 0);
123   if (!buffered)
124     return stream;
125
126   var buffer =
127       Components.classes["@mozilla.org/network/buffered-input-stream;1"].
128       createInstance(Components.interfaces.nsIBufferedInputStream);
129   buffer.init(stream, 4096);
130   return buffer;
131 }
132
133 function StoreURLAsync (aURLSpec, aFile, observer)
134 {
135   var chan = _getChannelForURL (aURLSpec);
136   chan.loadFlags |= cnsIRequest.LOAD_BYPASS_CACHE;
137   
138   // chan.contentType = "application/http-index-format";
139   gCurrentChannel = chan;
140
141   var uploadstream = newFileInputStream(aFile, true);
142
143   chan.QueryInterface(Components.interfaces.nsIUploadChannel);
144   chan.setUploadStream(uploadstream, "", aFile.fileSize);
145
146   return chan.asyncOpen (new StreamListener (chan, aURLSpec, observer), null);
147 }
148
149 function loadURL(url, caller)
150 {
151   function onComplete(data, url, status)
152     {
153       if (status == Components.results.NS_OK)
154       {
155         if (caller.parseData)
156           caller.parseData(data, caller.rqData);
157         if (caller.endCallback)
158           caller.endCallback(caller.rqData);
159       }
160       else if (caller.errorCallback)
161         caller.errorCallback(url, status);
162     };
163
164   loadURLAsync (url, { onComplete: onComplete });
165 }
166
167 /*******************************/
168
169 function deleteURLAsync (url, treeitem)
170 {
171   var chan = _getChannelForURL (url);
172   chan.loadFlags |= cnsIRequest.LOAD_BYPASS_CACHE;
173
174   chan instanceof Components.interfaces.nsIFTPChannel;
175   var p = new progressListener(chan, FTP_DEL, url, null, treeitem)
176   chan.notificationCallbacks = p.QueryInterface(Components.interfaces.nsIInterfaceRequestor) ;
177   chan.deleteFile();
178   gError = false;
179
180   gCurrentChannel = chan;
181
182   return chan.asyncOpen (new StreamListener (chan, url, null), null);
183 }
184
185 function removeDirURLAsync (url, treeitem)
186 {
187   var chan = _getChannelForURL (url);
188   chan.loadFlags |= cnsIRequest.LOAD_BYPASS_CACHE;
189
190   chan instanceof Components.interfaces.nsIFTPChannel;
191   var p = new progressListener(chan, FTP_RMDIR, url, null, treeitem)
192   chan.notificationCallbacks = p.QueryInterface(Components.interfaces.nsIInterfaceRequestor) ;
193   chan.removeDirectory();
194   gError = false;
195
196   gCurrentChannel = chan;
197
198   return chan.asyncOpen (new StreamListener (chan, url, null), null);
199 }
200
201 function createDirURLAsync (url, aDirName, aRequestData, aEnableNotifs)
202 {
203   var chan = _getChannelForURL (url);
204   chan.loadFlags |= cnsIRequest.LOAD_BYPASS_CACHE;
205
206   chan instanceof Components.interfaces.nsIFTPChannel;
207   if (aEnableNotifs)
208   {
209           var p = new progressListener(chan, FTP_MKDIR, url, aDirName, aRequestData)
210           chan.notificationCallbacks = p.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
211   }
212   chan.createDirectory();
213   gError = false;
214
215   gCurrentChannel = chan;
216
217   return chan.asyncOpen (new StreamListener (chan, url, null), null);
218 }
219
220 function renameURLAsync(url, aNewURI, aRequestData)
221 {
222   var chan = _getChannelForURL(url);
223   chan.loadFlags |= cnsIRequest.LOAD_BYPASS_CACHE;
224
225   chan instanceof Components.interfaces.nsIFTPChannel;
226   var p = new progressListener(chan, FTP_RENAME, url, aNewURI, aRequestData)
227   chan.notificationCallbacks = p.QueryInterface(Components.interfaces.nsIInterfaceRequestor) ;
228   chan.renameTo(aNewURI);
229   gError = false;
230
231   gCurrentChannel = chan;
232
233   return chan.asyncOpen (new StreamListener (chan, url, null), null);
234 }
235
236 /**************************************/
237
238 function DropFtpConnection()
239 {
240   if (gCurrentChannel)
241     gCurrentChannel.cancel(0x804b0002); // NS_BINDING_ABORTED
242 }
243
244 function ForgetAboutLastFtpRequest()
245 {
246   gCurrentChannel = null;
247 }
248