WWW update
[ubi] / qml / ubi / u1.js
1 // u1.js
2 .pragma library
3
4 Qt.include("oauth/oauth.js")
5 Qt.include("oauth/sha1.js")
6
7 function getToken(user,pass,root)
8 {
9     var url = "https://login.ubuntu.com/api/1.0/authentications?ws.op=authenticate&token_name=Ubuntu%20One%20@%20Ubi"
10     var xhr = new XMLHttpRequest();
11     if(user && pass) {
12         xhr.open("GET",url,true,user,pass);
13         xhr.onreadystatechange = function() {
14                     if(xhr.readyState===4) {
15                         //console.log(xhr.status);
16                         if(xhr.status>=400) {
17                             root.onErr(xhr.status);
18                         } else {
19                             //console.log(xhr.responseText);
20                             var resp = eval('('+xhr.responseText+')');
21                             var secrets = {
22                                 token: resp.token,
23                                 secret: resp.token_secret,
24                                 consumer_key : resp.consumer_key,
25                                 consumer_secret: resp.consumer_secret,
26                             };
27                             registerToken(secrets,user,root);
28                         }
29                     }
30                 }
31         xhr.send();
32     } else {
33         root.onErr(0);
34     }
35 }
36
37 function oAuthRequest(url,secrets,method,range)
38 {
39     var accessor = {
40         consumerKey: secrets.consumer_key,
41         consumerSecret: secrets.consumer_secret,
42         token: secrets.token,
43         tokenSecret: secrets.secret
44     };
45     if(!method) method = "GET";
46     var message = {
47         action: url,
48         method: method,
49         parameters: [
50             ["oauth_consumer_key",accessor.consumerKey],
51             ["oauth_token",accessor.token],
52             ["oauth_version","1.0"]
53         ]
54     };
55     OAuth.setTimestampAndNonce(message);
56     OAuth.SignatureMethod.sign(message,accessor);
57     var xhr = new XMLHttpRequest();
58     xhr.open(method,url,true);
59     var auth = OAuth.getAuthorizationHeader("",message.parameters);
60     xhr.setRequestHeader("Authorization",auth);
61     if(range) xhr.setRequestHeader("Range",range);
62     return xhr;
63 }
64
65 function oAuthHeader(url,secrets,method)
66 {
67     var accessor = {
68         consumerKey: secrets.consumer_key,
69         consumerSecret: secrets.consumer_secret,
70         token: secrets.token,
71         tokenSecret: secrets.secret
72     };
73     if(!method) method = "GET";
74     var message = {
75         action: url,
76         method: method,
77         parameters: [
78             ["oauth_consumer_key",accessor.consumerKey],
79             ["oauth_token",accessor.token],
80             ["oauth_version","1.0"]
81         ]
82     };
83     OAuth.setTimestampAndNonce(message);
84     OAuth.SignatureMethod.sign(message,accessor);
85     return OAuth.getAuthorizationHeader("",message.parameters);
86 }
87
88 function registerToken(secrets,user,root)
89 {
90     var url = "https://one.ubuntu.com/oauth/sso-finished-so-get-tokens/"+user;
91     var xhr = oAuthRequest(url,secrets);
92     xhr.onreadystatechange = function() {
93                 if(xhr.readyState===4) {
94                     if(xhr.status>=400) {
95                         //console.log(xhr.responseText);
96                         root.onErr(xhr.status);
97                     } else {
98                         //root.onResp(secrets);
99                         //console.log(xhr.responseText);
100                         getAccount(secrets,root);
101                     }
102                 }
103             }
104     xhr.send();
105 }
106
107 function getAccount(secrets,root)
108 {
109     var url = "https://one.ubuntu.com/api/account/";
110     var xhr = oAuthRequest(url,secrets);
111     //console.log("getAccount");
112     xhr.onreadystatechange = function() {
113                 if(xhr.readyState===4) {
114                     if(xhr.status>=400||xhr.status===0) {
115                         //console.log(xhr.status);
116                         //console.log(xhr.responseText);
117                         //console.log("getAccount: err");
118                         root.onErr(xhr.status);
119                     } else {
120                         //console.log(xhr.responseText);
121                         //console.log("getAccount: ok");
122                         var account = eval('('+xhr.responseText+')');
123                         root.onResp(secrets,account);
124                     }
125                 }
126             }
127     xhr.send();
128 }
129
130 function getFiles(secrets,rootNode,root)
131 {
132     var url = encodeURI2("https://one.ubuntu.com/api/file_storage/v1"
133             +rootNode+"/?include_children=true");
134     //console.log("url: "+url);
135     var xhr = oAuthRequest(url,secrets);
136     xhr.onreadystatechange = function() {
137                 if(xhr.readyState===4) {
138                     if(xhr.status>=400||xhr.status===0) {
139                         root.onErr(xhr.status);
140                     } else {
141                         //console.log(xhr.responseText);
142                         //console.log("");
143                         //console.log(xhr.getAllResponseHeaders());
144                         var resp = eval('('+xhr.responseText+')');
145                         var nodes = resp.children;
146                         root.onResp(nodes);
147                     }
148                 }
149             }
150     xhr.send();
151 }
152
153 function getRootNode(secrets,root)
154 {
155     var url = "https://one.ubuntu.com/api/file_storage/v1";
156     var xhr = oAuthRequest(url,secrets);
157     xhr.onreadystatechange = function() {
158                 if(xhr.readyState===4) {
159                     if(xhr.status>=400||xhr.status===0) {
160                         //console.log(xhr.status);
161                         root.onErr(xhr.status);
162                     } else {
163                         //console.log(xhr.responseText);
164                         var resp = eval('('+xhr.responseText+')');
165                         root.onRespRootNode(resp);
166                     }
167                 }
168             }
169     xhr.send();
170 }
171
172 function getFileTree(secrets,root)
173 {
174     var url = "https://one.ubuntu.com/api/file_storage/v1";
175     var xhr = oAuthRequest(url,secrets);
176     xhr.onreadystatechange = function() {
177                 if(xhr.readyState===4) {
178                     if(xhr.status>=400||xhr.status===0) {
179                         //console.log(xhr.status);
180                         root.onErr(xhr.status);
181                     } else {
182                         //console.log(xhr.responseText);
183                         var resp = eval('('+xhr.responseText+')');
184                         var rootNode = resp.root_node_path;
185                         getFiles(secrets,rootNode,root);
186                     }
187                 }
188             }
189     xhr.send();
190 }
191
192 function renameFile(secrets,resourcePath,targetPath,root)
193 {
194     var url = encodeURI2("https://one.ubuntu.com/api/file_storage/v1"+resourcePath);
195     console.log("url: "+url);
196     //console.log("target: "+encodeURI(targetPath));
197     //console.log("target: "+targetPath);
198     var xhr = oAuthRequest(url,secrets,"PUT");
199     xhr.setRequestHeader("Content-Type","application/json");
200     //var body = '{"path":"'+encodeURI(targetPath)+'"}';
201     var body = '{"path":"'+targetPath+'"}';
202     //console.log("body: "+body);
203     xhr.onreadystatechange = function() {
204                 if(xhr.readyState===4) {
205                     if(xhr.status>=400||xhr.status===0) {
206                         console.log("status: "+xhr.status);
207                         //console.log(xhr.responseText);
208                         root.onErrRename(xhr.status);
209                     } else {
210                         //console.log("status: "+xhr.status);
211                         //console.log(xhr.responseText);
212                         var resp = eval('('+xhr.responseText+')');
213                         //console.log(resp);
214                         root.onRespRename(resp);
215                     }
216                 }
217             }
218
219     xhr.send(body);
220 }
221
222 function stopPublishing(secrets,resourcePath,root)
223 {
224     var url = encodeURI2("https://one.ubuntu.com/api/file_storage/v1"+resourcePath);
225     var xhr = oAuthRequest(url,secrets,"PUT");
226     xhr.setRequestHeader("Content-Type","application/json");
227     var body = '{"is_public":false}';
228     xhr.onreadystatechange = function() {
229                 if(xhr.readyState===4) {
230                     if(xhr.status>=400||xhr.status===0) {
231                         //console.log("status: "+xhr.status);
232                         //console.log(xhr.responseText);
233                         root.onErrStopPublishing(xhr.status);
234                     } else {
235                         //console.log("status: "+xhr.status);
236                         //console.log(xhr.responseText);
237                         var resp = eval('('+xhr.responseText+')');
238                         //console.log(resp);
239                         root.onRespStopPublishing(resp);
240                     }
241                 }
242             }
243     xhr.send(body);
244 }
245
246 function startPublishing(secrets,resourcePath,root)
247 {
248     var url = encodeURI2("https://one.ubuntu.com/api/file_storage/v1"+resourcePath);
249     var xhr = oAuthRequest(url,secrets,"PUT");
250     xhr.setRequestHeader("Content-Type","application/json");
251     var body = '{"is_public":true}';
252     xhr.onreadystatechange = function() {
253                 if(xhr.readyState===4) {
254                     if(xhr.status>=400||xhr.status===0) {
255                         //console.log("status: "+xhr.status);
256                         //console.log(xhr.responseText);
257                         root.onErrStartPublishing(xhr.status);
258                     } else {
259                         //console.log("status: "+xhr.status);
260                         //console.log(xhr.responseText);
261                         var resp = eval('('+xhr.responseText+')');
262                         //console.log(resp);
263                         root.onRespStartPublishing(resp);
264                     }
265                 }
266             }
267     xhr.send(body);
268 }
269
270 function newFolder(secrets,resourcePath,root)
271 {
272     var url = encodeURI2("https://one.ubuntu.com/api/file_storage/v1"+resourcePath);
273     //console.log("url: "+url);
274     var xhr = oAuthRequest(url,secrets,"PUT");
275     xhr.setRequestHeader("Content-Type","application/json");
276     var body = '{"kind": "directory"}';
277     //console.log("body: "+body);
278     xhr.onreadystatechange = function() {
279                 if(xhr.readyState===4) {
280                     if(xhr.status>=400||xhr.status===0) {
281                         //console.log("status: "+xhr.status);
282                         //console.log(xhr.responseText);
283                         root.onErrNew(xhr.status);
284                     } else {
285                         //console.log("status: "+xhr.status);
286                         //console.log(xhr.responseText);
287                         root.onRespNew();
288                     }
289                 }
290             }
291
292     xhr.send(body);
293 }
294
295 function deleteFile(secrets,resourcePath,root,utils)
296 {
297     //var urlA = "https://one.ubuntu.com/api/file_storage/v1"+encodeURI(resourcePath);
298     var url = "https://one.ubuntu.com/api/file_storage/v1"+resourcePath;
299     var urlA = encodeURI2(url);
300
301     //console.log("u1.js:delete url="+url);
302     var auth = oAuthHeader(urlA,secrets,"DELETE");
303     utils.deleteFile(url,auth);
304 }
305
306 function getFileContentType(secrets,root,path)
307 {
308     //var url = "https://one.ubuntu.com/api/file_storage/v1"+encodeURI(path);
309     var url = encodeURI2("https://files.one.ubuntu.com"+path);
310     //console.log("url: "+url);
311     var xhr = oAuthRequest(url,secrets,"GET","bytes=0-10");
312     xhr.onreadystatechange = function() {
313                 console.log("readyState: "+xhr.readyState);
314                 if(xhr.readyState===4) {
315                     if(xhr.status>=400||xhr.status===0) {
316                     } else {
317                         var filename;
318                         var ind = path.lastIndexOf("/");
319                         if(ind>=0) {
320                             filename = path.substr(ind+1);
321                         }  else {
322                             filename = path;
323                         }
324                         var type = xhr.getResponseHeader("content-type");
325                         //console.log("filename: "+filename+" type: "+type);
326                         root.setContentType(type);
327                     }
328                 }
329             }
330     xhr.send();
331 }
332
333 function fixFilename(path) {
334     path = path.toString();
335     //console.log(path);
336     var ind = path.lastIndexOf("/");
337     if(ind>=0) {
338         path = path.substr(ind+1);
339     }
340     if(path=="") path = "/";
341
342     return path;
343 }
344
345 function fixFolder(path) {
346     path = path.toString();
347     //console.log(path);
348     var ind = path.lastIndexOf("file://");
349     if(ind>=0) {
350         path = path.substr(ind+7);
351     }
352     if(path=="") path = "/";
353
354     return path;
355 }
356
357 function getFileContent(secrets,root,path,folder,size,utils)
358 {
359     //var url = "https://one.ubuntu.com/api/file_storage/v1"+encodeURI(path);
360     var url = "https://files.one.ubuntu.com"+path;
361     var urlA = encodeURI2(url);
362
363     var filename = fixFilename(path);
364     var ffolder = fixFolder(folder);
365     //console.log("url: "+url);
366     //console.log("ffolder: "+ffolder);
367     var auth = oAuthHeader(urlA,secrets,"GET");
368     //console.log("auth: "+auth);
369     utils.downloadFile(ffolder,filename,url,size,auth);
370 }
371
372 function uploadFile(secrets,root,path,filename,folder,utils)
373 {
374     var url = "https://files.one.ubuntu.com"+path;
375     var urlA = encodeURI2(url);
376
377     //console.log("u1.js:uploadFile url=" + url);
378     //console.log("u1.js:uploadFile urAl=" + urlA);
379
380     var ffolder = fixFolder(folder);
381     var auth = oAuthHeader(urlA,secrets,"PUT");
382     utils.uploadFile(ffolder,filename,url,auth);
383 }
384
385 function encodeURI2(uri)
386 {
387     var uri2 = encodeURI(uri);
388     uri2 = uri2.replace(/\%5B/g, "[");
389     uri2 = uri2.replace(/\%5D/g, "]");
390     return uri2;
391 }