Added significant performance improvement to delete all.
[webos-internals:modifications.git] / email / email-read-delete-all.patch
1 Adds delete all and read buttons to email folders
2 Index: /usr/palm/applications/com.palm.app.email/app/controllers/list-assistant.js
3 ===================================================================
4 --- .orig/usr/palm/applications/com.palm.app.email/app/controllers/list-assistant.js
5 +++ /usr/palm/applications/com.palm.app.email/app/controllers/list-assistant.js
6 @@ -34,7 +34,8 @@ var ListAssistant = Class.create(App.Sce
7                         visible:true,
8                         items: [
9                                 {label:$L('Compose'), icon:'compose', command:'compose'},
10 -                               {},
11 +                               {label:$L('Delete All'), icon:'delete', command:'deleteall'},
12 +                               {label:$L('All Read'), icon:'priority', command:'readall'},
13                                 {label:$L('Update'), icon:'sync', command:'sync'}
14                         ]};
15         this.controller.setupWidget(Mojo.Menu.commandMenu, undefined, this.cmdMenuModel);
16 @@ -179,6 +180,14 @@ var ListAssistant = Class.create(App.Sce
17    handleCommand: function(event) {
18         if (event.type == Mojo.Event.command) {
19                 switch (event.command) {
20 +                       case 'deleteall':
21 +                               this.handleDeleteAll();
22 +                               break;
23 +
24 +                       case 'readall':
25 +                               this.handleReadAll();
26 +                               break;
27 +
28                         case 'compose':
29                                 this.handleSend();
30                                 break;
31 @@ -479,6 +488,153 @@ var ListAssistant = Class.create(App.Sce
32         }
33    },
34  
35 +  handleDeleteAll: function (event) {
36 +       
37 +       var totalCount = 0;
38 +                       
39 +       totalCount = this.emailListElement.mojo.getLength();
40 +                                       
41 +       this.controller.showAlertDialog({
42 +               onChoose: function(value) {
43 +                       if(value == 'yes') {
44 +                               //Delete all items in this folder
45 +                               this.deleteAll();
46 +                       }
47 +               },
48 +               title: '<b>' + $L('Delete All') + '</b>',
49 +               message: $L('Are you sure you want to delete all ') + "<b>" + totalCount + "</b>" + $L(' items in this folder?'),
50 +               choices: [
51 +                       {label:$L('Yes'), value:'yes', type:'affirmative'},
52 +                       {label:$L('No'), value:'no', type:'alert'}
53 +                       ]
54 +       });
55 +  },
56 +
57 +  handleDeleteAllResponse: function (event) {
58 +        //check to see if there are more items to delete.
59 +       this.deleteAll();
60 +  },
61 +
62 +  deleteAll: function(){
63 +    
64 +       var count = this.emailListElement.mojo.getLength();
65 +       
66 +       var id;
67 +               
68 +       if(count > 0)
69 +       {
70 +               var i;
71 +
72 +               //Since the list is loaded dynamically we may have the count of all emails but not all of the data
73 +               //So start with count and work backward with error handling
74 +               //When reach 0 trigger at least one more pass after data has had a chance to refresh
75 +               for(i=count-1; i>=0; i--)
76 +               {
77 +                       var item = this.emailListElement.mojo.getNodeByIndex(i);
78 +
79 +                       if(item !== undefined)
80 +                       {
81 +                               id = item.id;
82 +
83 +                               if(id)
84 +                               {
85 +                                       if(i === 0)
86 +                                       {
87 +                                               this.controller.serviceRequest(Email.identifier, {
88 +                                                       method: 'setDeleted',
89 +                                                       parameters: {'message':id, 'value': true },
90 +                                                       onSuccess: this.handleDeleteAllResponse.bind(this),
91 +                                                       onFailure: this.handleDeleteAllResponse.bind(this)
92 +                                                       });
93 +                                       }
94 +                                       else 
95 +                                       {
96 +                                               this.controller.serviceRequest(Email.identifier, {
97 +                                                       method: 'setDeleted',
98 +                                                       parameters: {'message':id, 'value': true },
99 +                                                       onSuccess: undefined,
100 +                                                       onFailure: undefined
101 +                                                       });
102 +                                       }
103 +                               }//if(id)
104 +                       }//if item !== undefined
105 +                       else
106 +                       {
107 +                               if(i === 0)
108 +                               {
109 +                                       //item was undefined probably because it is currently marked for delete
110 +                                       //this.deleteAll();
111 +                                       
112 +                                       //send msg to delete a bogus msg id. It will fail but will also give the list a chance to update
113 +                                       //so that when the deleteall gets called again we can proceed
114 +                                       //just calling deleteall causes an endless loop because the list doesnt get a chance to update if
115 +                                       //the 0 index item does not happen to be part of the currently loaded set of data
116 +                                       //need to find a better way to get the list to refresh but this should work for now.
117 +                                       this.controller.serviceRequest(Email.identifier, {
118 +                                               method: 'setDeleted',
119 +                                               parameters: {'message':-1, 'value': true },
120 +                                               onSuccess: this.handleDeleteAllResponse.bind(this),
121 +                                               onFailure: this.handleDeleteAllResponse.bind(this)
122 +                                               });
123 +                               }
124 +                       }
125 +               }//for
126 +       }//count > 0
127 +  },
128 +
129 +  handleReadAllResponse: function (event) {
130 +  },
131 +
132 +  readAll: function(tleft){
133 +       var count = this.emailListElement.mojo.getLength();
134 +       var id;
135 +       while(tleft > 0) 
136 +       {       
137 +               var item = this.emailListElement.mojo.getNodeByIndex((tleft - 1));      
138 +               if(item !== undefined)
139 +               {
140 +                       id = item.id;
141 +                       if(id)
142 +                       {
143 +                               this.controller.serviceRequest(Email.identifier, {
144 +                                       method: 'setRead',
145 +                                       parameters: {'message':id, 'value': true },
146 +                                       onSuccess: this.handleReadAllResponse.bind(this),
147 +                                       onFailure: this.handleReadAllResponse.bind(this)
148 +                                       });
149 +                       }
150 +               
151 +                       tleft = tleft - 1;
152 +               }
153 +               else
154 +               {
155 +                       tleft = tleft - 1;
156 +               }
157 +       }       
158 +  },
159 +  
160 +
161 +  handleReadAll: function (event) {
162 +               var totalCount = 0;
163 +               totalCount = this.emailListElement.mojo.getLength();
164 +               if (totalCount > 0) {
165 +                       this.controller.showAlertDialog({
166 +                   onChoose: function(value) {
167 +                               if(value == 'yes') {
168 +                                               var tleft = totalCount;
169 +                                                       this.readAll(tleft);
170 +                       }
171 +                               },
172 +                       title: '<b>' + $L('All Read') + '</b>',
173 +                       message: $L('Are you sure you want to mark ') + "<b>" + totalCount + "</b>" + $L(' items in this folder read?'),
174 +                    choices: [
175 +                       {label:$L('Yes'), value:'yes', type:'affirmative'},
176 +                        {label:$L('No'), value:'no', type:'alert'}
177 +                    ]
178 +                       });
179 +               }
180 +  },
181 +
182    updateFolderStatusModel: function(response) {
183         if (!response.folders) {
184                 Mojo.Log.warn("updateFolderStatusModel got unexpected response ", Object.toJSON(response))