updates and additions
[webos-internals:modifications.git] / youtube / youtube-video-downloads.patch
1 diff --git a/usr/palm/applications/com.palm.app.youtube/app/controllers/video-assistant.js b/usr/palm/applications/com.palm.app.youtube/app/controllers/video-assistant.js
2 index 5c3be25..60dd52e 100644
3 --- a/usr/palm/applications/com.palm.app.youtube/app/controllers/video-assistant.js
4 +++ b/usr/palm/applications/com.palm.app.youtube/app/controllers/video-assistant.js
5 @@ -17,6 +17,8 @@ var VideoAssistant = Class.create({
6                 this.cmdMenuModel = {items: [{label: $L('More'), submenu:'more-menu'}, 
7                                                                         {label: $L('Share'), submenu:'share-menu'}]};
8                                                                         
9 +               this.moreMenuModel.items[2] = {label: $L('Download'), command:'download', disabled:false};
10 +               this.currProgress = -1;
11                 this.controller.setupWidget('more-menu', undefined, this.moreMenuModel);
12                 this.controller.setupWidget('share-menu', undefined, this.shareMenuModel);
13                 this.controller.setupWidget(Mojo.Menu.commandMenu, {menuClass: "no-fade"}, this.cmdMenuModel);
14 @@ -54,6 +56,53 @@ var VideoAssistant = Class.create({
15                                 }
16                                 event.stop(event);
17                         }
18 +                       else if (event.command == 'download') {
19 +                               this.moreMenuModel.items[2] = {label: $L('-Downloading-'), command:'download', disabled:true};
20 +                               this.controller.modelChanged(this.moreMenuModel);
21 +                               var filename = this.video.title;
22 +                               filename = filename.replace(/\s/ig, " ");
23 +                               filename = filename.replace(/[^a-z0-9_-]/ig, "");
24 +                               if(filename.length>30) {
25 +                                       filename = filename.substring(0, 29) + "...";
26 +                               }
27 +                               this.dlRequest = this.controller.serviceRequest('palm://com.palm.downloadmanager/', {
28 +                                       method: 'download',
29 +                                       parameters: {
30 +                                               target: this.video.link,
31 +                                               mime: "video/mp4",
32 +                                               targetFilename: filename + ".mp4",
33 +                                               subscribe: true
34 +                                       },
35 +                                       onSuccess: function(response) {
36 +                                               if(response.completed) {
37 +                                                       this.dlRequest.cancel();
38 +                                                       this.moreMenuModel.items[2] = {label: $L('Download'), command:'download', disabled:false};
39 +                                                       this.controller.modelChanged(this.moreMenuModel);
40 +                                                       this.controller.showBanner("Download finished!", "");
41 +
42 +                                               } else {
43 +                                                       if(response.amountReceived && response.amountTotal) {
44 +                                                               var percent = (response.amountReceived / response.amountTotal)*100;
45 +                                                               percent = Math.round(percent);
46 +                                                               if(percent!=NaN) {
47 +                                                                       if(this.currProgress != percent) {
48 +                                                                               this.currProgress = percent;
49 +                                                                               this.controller.showBanner("Downloading: " + percent + "%", "");
50 +                                                                       }
51 +                                                               }
52 +                                                       }
53 +                                               }
54 +                                       }.bind(this),
55 +                                       onFailure: function(fail) {
56 +                                               this.dlRequest.cancel();
57 +                                               Mojo.Controller.errorDialog(fail.errorText);
58 +                                               this.moreMenuModel.items[2] = {label: $L('Download'), command:'download', disabled:false};
59 +                                               this.controller.modelChanged(this.moreMenuModel);
60 +                                               this.currProgress = -1;
61 +                                       }.bind(this)
62 +                               });
63 +                               event.stop(event);
64 +                       }
65                         else if (event.command == 'related') {
66                                 if (this.video.related_url && this.video.related_url != "") {
67                                         Mojo.Controller.stageController.pushScene("search", this.video.related_url, "related");
68 @@ -87,7 +136,8 @@ var VideoAssistant = Class.create({
69         moreMenuModel: {label: $L('More'), 
70                                         items: [
71                                                 {label: $L('More from This Author'), command:'more'}, 
72 -                                               {label: $L('Related Videos'), command:'related' }
73 +                                               {label: $L('Related Videos'), command:'related' },
74 +                                               {label: $L('Download'), command:'download' }
75                                         ]},
76                                         
77         shareMenuModel: {label: $L('Share'),