Processed all outstanding update submissions
[webos-internals:modifications.git] / calendar / calendar-create-birthday-events.patch
1 diff --git a/usr/palm/applications/com.palm.app.contacts/app/controllers/list-assistant.js b/usr/palm/applications/com.palm.app.contacts/app/controllers/list-assistant.js
2 index fbb9529..e940659 100644
3 --- a/usr/palm/applications/com.palm.app.contacts/app/controllers/list-assistant.js
4 +++ b/usr/palm/applications/com.palm.app.contacts/app/controllers/list-assistant.js
5 @@ -287,6 +287,12 @@ ListAssistant = Class.create(App.Scene,{
6                                 items: [Mojo.Menu.editItem,{
7                                         label:$L("Send All to Car Kit"),
8                                         command:"sendcontacts"
9 +                               }, {
10 +                                       label: $L('Create Birthday Events'),
11 +                                       command: "birthdayevent"
12 +                               }, {
13 +                                       label: $L('Remove Birthday Events'),
14 +                                       command: "removeevents"
15                                 }, prefsItem, {
16                                         label: $LL('Help'),
17                                         command: Mojo.Menu.helpCmd
18 @@ -466,6 +472,217 @@ ListAssistant = Class.create(App.Scene,{
19                         this.controller.stageController.pushScene('prefs');
20                 }else if (event.type == Mojo.Event.command && event.command == "sendcontacts") {
21                         this.sendContacts();
22 +               }else if (event.type == Mojo.Event.command && event.command == "removeevents") {
23 +                       AppAssistant.contactsService.getDefaultAccount(
24 +                               function(account) {
25 +                                       var accountId = account.defaultAccountId;
26 +                                       this.controller.serviceRequest('palm://com.palm.calendar', {      
27 +                                               method: 'getCalendarsForSyncAccount',
28 +                                               parameters: {id: accountId},
29 +                                               onSuccess: function(calList) {
30 +                                                       try {
31 +                                                       //Mojo.Log.error(Object.toJSON(calList.calendars));
32 +                                                       if (calList.calendars.length > 0) {\r
33 +                                                               var calendarId = calList.calendars[0].id;
34 +                                                               this.controller.serviceRequest('palm://com.palm.calendar', {      
35 +                                                                       method: 'getEvents',
36 +                                                                       parameters: {calendarId: calendarId, startDate: 1262304000000, endDate: 1293839999999, subscribe: false},
37 +                                                                       onSuccess: function(eventList) {
38 +                                                                       Mojo.Controller.getAppController().showBanner("Browsing " + eventList.days.length + " All-Day events", "", "");
39 +                                                                               for (var i=0; i<eventList.days.length; i++) 
40 +                                                                               {
41 +                                                                                       var day = eventList.days[i];
42 +                                                                                       for (var k=0; k<day.allDayEvents.length; k++) 
43 +                                                                                       {
44 +                                                                                               var evnt = day.allDayEvents[k];
45 +                                                                                               var eventId = evnt.id;
46 +                                                                                               var subject = evnt.subject;
47 +                                                                                               if (evnt.note.indexOf("Created by birthday reminder patch from MetaViewSoft") > -1)
48 +                                                                                               {
49 +                                                                                               Mojo.Controller.getAppController().showBanner("Delete event " + subject, "", "");
50 +                                                                                                       this.controller.serviceRequest('palm://com.palm.calendar', {      
51 +                                                                                                               method: 'deleteEvent',
52 +                                                                                                               parameters: {id: eventId, series: true, date: null},
53 +                                                                                                               onSuccess: function(eventList) {
54 +                                                                                                                       Mojo.Log.error("deleted");
55 +                                                                                                               }.bind(this),
56 +                                                                                                               onFailure: function(error) {
57 +                                                                                                                       Mojo.Log.error(subject, "* error:", error.errorText);
58 +                                                                                                               }.bind(this)
59 +                                                                                                       });
60 +                                                                                               }
61 +                                                                                       }
62 +                                                                               }
63 +                                                                       }.bind(this)
64 +                                                               });
65 +                                                       }
66 +                                               } catch (e) {Mojo.Log.error(Object.toJSON(e));}
67 +                                               }.bind(this)
68 +                                       });
69 +                               }.bind(this),
70 +                               this.controller
71 +                       );\r
72 +               }else if (event.type == Mojo.Event.command && event.command == "birthdayevent") {
73 +                       AppAssistant.contactsService.getDefaultAccount(
74 +                               function(account) {
75 +                                       var accountId = account.defaultAccountId;
76 +                                       this.controller.serviceRequest('palm://com.palm.calendar', {      
77 +                                               method: 'getCalendarsForSyncAccount',
78 +                                               parameters: {id: accountId},
79 +                                               onSuccess: function(calList) {
80 +                                                       //Mojo.Log.error(Object.toJSON(calList.calendars));
81 +                                                       if (calList.calendars.length == 0) {
82 +                                                               this.controller.showAlertDialog({onChoose: function(value) {}, title: "Error!", message: "No calendar assigned to your default profile.", choices:[{label: 'OK', value:"OK", type:'affirmative'}]});\r
83 +                                                       }else if (calList.calendars.length > 0) {\r
84 +                                                               var calendarId = calList.calendars[0].id;\r
85 +                                                               this.controller.showAlertDialog({title: "Reminder?", message: "Reminder settings:", choices:[{label: 'No reminder', value:"0"}, {label: '1 day before', value:"12"}, {label: '2 days before', value:"36"}, {label: '3 days before', value:"60"}, {label: '1 week before', value:"156"}],
86 +                                                                       onChoose: function(reminder) {\r
87 +                                                                               this.controller.showAlertDialog({title: "Repeat?", message: "Do you want repeated events or just for this year?", choices:[{label: 'Repeat forever', value:"0"}, {label: 'Only this year', value:"1"}],
88 +                                                                                       onChoose: function(repeat) {\r
89 +                                                                                               AppAssistant.contactsService.count(this.controller, "",
90 +                                                                                                       function(event) {
91 +                                                                                                               //Mojo.Log.error(Object.toJSON(event));
92 +                                                                                                               //AppAssistant.contactsService.list(
93 +                                                                                                               this.controller.serviceRequest('palm://com.palm.contacts', {
94 +                                                                                                                       method: 'list',
95 +                                                                                                                       parameters: {"offset": 0, "limit": event.count, "subscribe": false},
96 +                                                                                                                       onSuccess: function(event) {
97 +                                                                                                                               var cntAllContacts = event.list.length;
98 +                                                                                                                               var cntCurContacts = 0;
99 +                                                                                                                               var sumBirthDayEvents = 0;
100 +                                                                                                                               var sumBirthDayCreated = 0;
101 +                                                                                                                               var sumBirthDayUpdated = 0;
102 +                                                                                                                               var sumBirthDayFailed = 0;
103 +                                                                                                                       Mojo.Controller.getAppController().showBanner("Browsing " + event.list.length + " contact records", "", "");
104 +                                                                                                                               for (var i=0; i<event.list.length; i++) {
105 +                                                                                                                                       var contactId = event.list[i].id;
106 +                                                                                                                                       AppAssistant.contactsService.details(contactId,
107 +                                                                                                                                               function(returnParams) {
108 +                                                                                                                                                       cntCurContacts++;
109 +                                                                                                                                                       if (returnParams.record.birthday !== undefined) {
110 +                                                                                                                                                               for (var k=0; k<returnParams.record.birthday.length; k++) {
111 +                                                                                                                                                                       var birthdate = new Date();
112 +                                                                                                                                                                       Mojo.Log.error(returnParams.record.birthday[k]);
113 +                                                                                                                                                                       birthdate.setFullYear(2010);
114 +                                                                                                                                                                       if (returnParams.record.birthday[k].length == 4) {
115 +                                                                                                                                                                               birthdate.setMonth(returnParams.record.birthday[k].substr(0, 2) - 1);
116 +                                                                                                                                                                               birthdate.setDate(returnParams.record.birthday[k].substr(2, 2));
117 +                                                                                                                                                                       } else {
118 +                                                                                                                                                                               birthdate.setMonth(returnParams.record.birthday[k].substr(4, 2) - 1);
119 +                                                                                                                                                                               birthdate.setDate(returnParams.record.birthday[k].substr(6, 2));
120 +                                                                                                                                                                       }
121 +                                                                                                                                                                       birthdate.setHours(0);
122 +                                                                                                                                                                       birthdate.setMinutes(0);
123 +                                                                                                                                                                       birthdate.setSeconds(0);
124 +                                                                                                                                                               Mojo.Controller.getAppController().showBanner("Birthday event for " + returnParams.record.firstName + " " + returnParams.record.lastName, "", "");
125 +                                                                                                                                                                       var birthday = {
126 +                                                                                                                                                                                                               "startTimestamp": birthdate.getTime(),\r
127 +                                                                                                                                                                                                               "endTimestamp": birthdate.getTime() + (24*60*60*1000) - 1000,\r
128 +                                                                                                                                                                                                               "allDay": true,\r
129 +                                                                                                                                                                                                               "subject": returnParams.record.firstName + " " + returnParams.record.lastName + "'s Birthday (" + Mojo.Format.formatDate(birthdate, {"date": "short"}) + ")",\r
130 +                                                                                                                                                                                                               "location": "",\r
131 +                                                                                                                                                                                                               "note": "Created by birthday reminder patch from MetaViewSoft. ICON=birthday;",\r
132 +                                                                                                                                                                                                               "timeZoneId": "",\r
133 +                                                                                                                                                                                                               "rrule": (repeat == 1) ? "" : "RRULE:FREQ=YEARLY;INTERVAL=1",\r
134 +                                                                                                                                                                                                               "endValidity": "Long.MAX_VALUE",\r
135 +                                                                                                                                                                                                               "alarm": (reminder == 0) ? "" : "-pt" + reminder + "h",\r
136 +                                                                                                                                                                                                               //"alldayReservedStartTimestamp": birthdate.getTime(),\r
137 +                                                                                                                                                                                                               //"alldayReservedEndTimestamp": birthdate.getTime() + (24*60*60*1000) - 1,\r
138 +                                                                                                                                                                                                               "calendarId": calendarId,\r
139 +                                                                                                                                                                                                               "parentId": 0,\r
140 +                                                                                                                                                                                               "externalId": "birthday_" + returnParams.record.id + "_" + k,\r
141 +                                                                                                                                                                                                               "rruleModel": {"count": -1, "interval": 1, "bymonthday": [], "frequency": "YEARLY", "bymonth": [], "until": "", "byhour": [], "byweekno": [], "bysetpos": [], "byyearday": [], "byday": [], "byminute": [], "bysecond": []},\r
142 +                                                                                                                                                                                                               "dirty": true,\r
143 +                                                                                                                                                                                                               "isRecurringForever": (repeat == 1) ? false : true
144 +                                                                                                                                                                                                       };
145 +                                                                                                                                                                       //Mojo.Log.error(Object.toJSON(birthday));
146 +                                                                                                                                                                       sumBirthDayEvents++;
147 +                                                                                                                                                                       // try to get event and update it or create a new one
148 +                                                                                                                                                                       Mojo.Log.error("accountId", accountId);
149 +                                                                                                                                                                       Mojo.Log.error("birthday.externalId", birthday.externalId);
150 +                                                                                                                                                                       this.controller.serviceRequest('palm://com.palm.calendar', {\r
151 +                                                                                                                                                                           method: 'getEvent',\r
152 +                                                                                                                                                                           parameters: {
153 +                                                                                                                                                                               accountId: accountId,\r
154 +                                                                                                                                                                               externalId: birthday.externalId,
155 +                                                                                                                                                                               subscribe: false\r
156 +                                                                                                                                                                           },\r
157 +                                                                                                                                                                           onSuccess: function(event) {
158 +                                                                                                                                                                               // copy EventId and try to update
159 +                                                                                                                                                                               birthday.eventId = event.eventId;
160 +                                                                                                                                                                                       this.controller.serviceRequest('palm://com.palm.calendar', {\r
161 +                                                                                                                                                                                           method: 'updateEvent',\r
162 +                                                                                                                                                                                           parameters: {
163 +                                                                                                                                                                                               event: birthday,\r
164 +                                                                                                                                                                                               subscribe: false\r
165 +                                                                                                                                                                                           },\r
166 +                                                                                                                                                                                           onSuccess: function() {
167 +                                                                                                                                                                                               Mojo.Log.error("Birthday event successfully updated.");
168 +                                                                                                                                                                                               sumBirthDayUpdated++;
169 +                                                                                                                                                                                               if ((cntCurContacts == cntAllContacts) && (sumBirthDayEvents == (sumBirthDayUpdated + sumBirthDayCreated + sumBirthDayFailed))) {
170 +                                                                                                                                                                                                       Mojo.Controller.getAppController().showBanner("Updated: " + sumBirthDayUpdated + " Created: " + sumBirthDayCreated + " Failed: " + sumBirthDayFailed, "", "");
171 +                                                                                                                                                                                               }
172 +                                                                                                                                                                                           },
173 +                                                                                                                                                                                           onFailure: function(error) {
174 +                                                                                                                                                                                               Mojo.Log.error("Update fails, giving up:", error.errorText);
175 +                                                                                                                                                                                               sumBirthDayFailed++;
176 +                                                                                                                                                                                               if ((cntCurContacts == cntAllContacts) && (sumBirthDayEvents == (sumBirthDayUpdated + sumBirthDayCreated + sumBirthDayFailed))) {
177 +                                                                                                                                                                                                       Mojo.Controller.getAppController().showBanner("Updated: " + sumBirthDayUpdated + " Created: " + sumBirthDayCreated + " Failed: " + sumBirthDayFailed, "", "");
178 +                                                                                                                                                                                               }
179 +                                                                                                                                                                                           }
180 +                                                                                                                                                                                       });
181 +                                                                                                                                                                           }.bind(this),\r
182 +                                                                                                                                                                           onFailure: function(error) {
183 +                                                                                                                                                                               // event wasn't found, try to create it
184 +                                                                                                                                                                               Mojo.Log.error("GetEvent fails, try create:", error.errorText);
185 +                                                                                                                                                                               // try to create a new event
186 +                                                                                                                                                                                       this.controller.serviceRequest('palm://com.palm.calendar', {\r
187 +                                                                                                                                                                                           method: 'setEvent',\r
188 +                                                                                                                                                                                           parameters: birthday,
189 +                                                                                                                                                                                           onSuccess: function() {
190 +                                                                                                                                                                                               Mojo.Log.error("Birthday event successfully created.");
191 +                                                                                                                                                                                               sumBirthDayCreated++;
192 +                                                                                                                                                                                               if ((cntCurContacts == cntAllContacts) && (sumBirthDayEvents == (sumBirthDayUpdated + sumBirthDayCreated + sumBirthDayFailed))) {
193 +                                                                                                                                                                                                       Mojo.Controller.getAppController().showBanner("Updated: " + sumBirthDayUpdated + " Created: " + sumBirthDayCreated + " Failed: " + sumBirthDayFailed, "", "");
194 +                                                                                                                                                                                               }
195 +                                                                                                                                                                                           },\r
196 +                                                                                                                                                                                           onFailure: function(error) {
197 +                                                                                                                                                                                               Mojo.Log.error("Error on create, giving up:", error.errorText);
198 +                                                                                                                                                                                               sumBirthDayFailed++;
199 +                                                                                                                                                                                               if ((cntCurContacts == cntAllContacts) && (sumBirthDayEvents == (sumBirthDayUpdated + sumBirthDayCreated + sumBirthDayFailed))) {
200 +                                                                                                                                                                                                       Mojo.Controller.getAppController().showBanner("Updated: " + sumBirthDayUpdated + " Created: " + sumBirthDayCreated + " Failed: " + sumBirthDayFailed, "", "");
201 +                                                                                                                                                                                               }
202 +                                                                                                                                                                                           }\r
203 +                                                                                                                                                                                       });
204 +                                                                                                                                                                       }.bind(this)\r
205 +                                                                                                                                                                       });
206 +                                                                                                                                                               }
207 +                                                                                                                                                       }
208 +                                                                                                                                               if ((cntCurContacts == cntAllContacts) && (sumBirthDayEvents == (sumBirthDayUpdated + sumBirthDayCreated + sumBirthDayFailed))) {
209 +                                                                                                                                                       Mojo.Controller.getAppController().showBanner("Updated: " + sumBirthDayUpdated + " Created: " + sumBirthDayCreated + " Failed: " + sumBirthDayFailed, "", "");
210 +                                                                                                                                               }
211 +                                                                                                                                                       //Mojo.Log.error(Object.toJSON(returnParams));
212 +                                                                                                                                               }.bind(this),
213 +                                                                                                                                               this.controller, null, function() {}
214 +                                                                                                                                       );
215 +                                                                                                                               }
216 +                                                                                                                       }.bind(this)
217 +                                                                                                               });
218 +                                                                                                       }.bind(this)
219 +                                                                                               );
220 +                                                                                       }.bind(this)
221 +                                                                               });
222 +                                                                       }.bind(this)
223 +                                                               });
224 +                                                       }
225 +                                               }.bind(this),
226 +                                           onFailure: function(error) {
227 +                                                       Mojo.Log.error("Calendarlist failed", error.errorText);
228 +                                           }.bind(this)\r
229 +                                       });
230 +                               }.bind(this),
231 +                               this.controller
232 +                       );
233                 }
234                 if (event.type == Mojo.Event.commandEnable && event.command == Mojo.Menu.prefsCmd) {
235                         // funkay!  this is apparently how you declare you want to activate the prefs menu item