9 New Patches.
[webos-internals:skurriers-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..1cf589a 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,199 @@ 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 +                                                                               for (var i=0; i<eventList.days.length; i++) 
39 +                                                                               {
40 +                                                                                       var day = eventList.days[i];
41 +                                                                                       for (var k=0; k<day.allDayEvents.length; k++) 
42 +                                                                                       {
43 +                                                                                               var evnt = day.allDayEvents[k];
44 +                                                                                               var eventId = evnt.id;
45 +                                                                                               var subject = evnt.subject;
46 +                                                                                               if (evnt.note == "Created by birthday reminder patch from MetaViewSoft.")
47 +                                                                                               {
48 +                                                                                               Mojo.Controller.getAppController().showBanner("Delete event " + subject, "", "");
49 +                                                                                                       this.controller.serviceRequest('palm://com.palm.calendar', {      
50 +                                                                                                               method: 'deleteEvent',
51 +                                                                                                               parameters: {id: eventId, series: true, date: null},
52 +                                                                                                               onSuccess: function(eventList) {
53 +                                                                                                                       Mojo.Log.error("deleted");
54 +                                                                                                               }.bind(this),
55 +                                                                                                               onFailure: function(error) {
56 +                                                                                                                       Mojo.Log.error(subject, "* error:", error.errorText);
57 +                                                                                                               }.bind(this)
58 +                                                                                                       });
59 +                                                                                               }
60 +                                                                                       }
61 +                                                                               }
62 +                                                                       }.bind(this)
63 +                                                               });
64 +                                                       }
65 +                                               } catch (e) {Mojo.Log.error(Object.toJSON(e));}
66 +                                               }.bind(this)
67 +                                       });
68 +                               }.bind(this),
69 +                               this.controller
70 +                       );\r
71 +               }else if (event.type == Mojo.Event.command && event.command == "birthdayevent") {
72 +                       AppAssistant.contactsService.getDefaultAccount(
73 +                               function(account) {
74 +                                       var accountId = account.defaultAccountId;
75 +                                       this.controller.serviceRequest('palm://com.palm.calendar', {      
76 +                                               method: 'getCalendarsForSyncAccount',
77 +                                               parameters: {id: accountId},
78 +                                               onSuccess: function(calList) {
79 +                                                       //Mojo.Log.error(Object.toJSON(calList.calendars));
80 +                                                       if (calList.calendars.length > 0) {\r
81 +                                                               var calendarId = calList.calendars[0].id;\r
82 +                                                               AppAssistant.contactsService.count(this.controller, "",
83 +                                                                       function(event) {
84 +                                                                               //Mojo.Log.error(Object.toJSON(event));
85 +                                                                               //AppAssistant.contactsService.list(
86 +                                                                               this.controller.serviceRequest('palm://com.palm.contacts', {
87 +                                                                                       method: 'list',
88 +                                                                                       parameters: {"offset": 0, "limit": event.count, "subscribe": false},
89 +                                                                                       onSuccess: function(event) {
90 +                                                                                               var cntAllContacts = event.list.length;
91 +                                                                                               var cntCurContacts = 0;
92 +                                                                                               var sumBirthDayEvents = 0;
93 +                                                                                               var sumBirthDayCreated = 0;
94 +                                                                                               var sumBirthDayUpdated = 0;
95 +                                                                                               var sumBirthDayFailed = 0;
96 +                                                                                       Mojo.Controller.getAppController().showBanner("Browsing " + event.list.length + " contact records", "", "");
97 +                                                                                               for (var i=0; i<event.list.length; i++) {
98 +                                                                                                       var contactId = event.list[i].id;
99 +                                                                                                       AppAssistant.contactsService.details(contactId,
100 +                                                                                                               function(returnParams) {
101 +                                                                                                                       cntCurContacts++;
102 +                                                                                                                       if (returnParams.record.birthday !== undefined) {
103 +                                                                                                                               for (var k=0; k<returnParams.record.birthday.length; k++) {
104 +                                                                                                                                       var birthdate = new Date();
105 +                                                                                                                                       birthdate.setFullYear(returnParams.record.birthday[0].substr(0, 4));
106 +                                                                                                                                       birthdate.setMonth(returnParams.record.birthday[0].substr(4, 2) - 1);
107 +                                                                                                                                       birthdate.setDate(returnParams.record.birthday[0].substr(6, 2));
108 +                                                                                                                                       birthdate.setHours(0);
109 +                                                                                                                                       birthdate.setMinutes(0);
110 +                                                                                                                                       birthdate.setSeconds(0);
111 +                                                                                                                               Mojo.Controller.getAppController().showBanner("Birthday event for " + returnParams.record.firstName + " " + returnParams.record.lastName, "", "");
112 +                                                                                                                                       var birthday = {
113 +                                                                                                                                                                               "startTimestamp": birthdate.getTime(),\r
114 +                                                                                                                                                                               "endTimestamp": birthdate.getTime() + (24*60*60*1000) - 1,\r
115 +                                                                                                                                                                               "allDay": true,\r
116 +                                                                                                                                                                               "subject": returnParams.record.firstName + " " + returnParams.record.lastName + " Birthday (" + Mojo.Format.formatDate(birthdate, {"date": "short"}) + ")",\r
117 +                                                                                                                                                                               "location": "",\r
118 +                                                                                                                                                                               "note": "Created by birthday reminder patch from MetaViewSoft.",\r
119 +                                                                                                                                                                               "timeZoneId": "",\r
120 +                                                                                                                                                                               "rrule": "RRULE:FREQ=YEARLY;INTERVAL=1",\r
121 +                                                                                                                                                                               "endValidity": "Long.MAX_VALUE",\r
122 +                                                                                                                                                                               "alarm": "-P1D",\r
123 +                                                                                                                                                                               "alldayReservedStartTimestamp": birthdate.getTime(),\r
124 +                                                                                                                                                                               "alldayReservedEndTimestamp": birthdate.getTime() + (24*60*60*1000),\r
125 +                                                                                                                                                                               "calendarId": calendarId,\r
126 +                                                                                                                                                                               "parentId": 0,\r
127 +                                                                                                                                                               "externalId": "birthday_" + returnParams.record.id + "_" + k,\r
128 +                                                                                                                                                                               "rruleModel": {"count": -1, "interval": 1, "bymonthday": [], "frequency": "YEARLY", "bymonth": [], "until": "", "byhour": [], "byweekno": [], "bysetpos": [], "byyearday": [], "byday": [], "byminute": [], "bysecond": []},\r
129 +                                                                                                                                                                               "dirty": true,\r
130 +                                                                                                                                                                               "isRecurringForever": true
131 +                                                                                                                                                                       };
132 +                                                                                                                                       sumBirthDayEvents++;
133 +                                                                                                                                       // try to get event and update it or create a new one
134 +                                                                                                                                       Mojo.Log.error("accountId", accountId);
135 +                                                                                                                                       Mojo.Log.error("birthday.externalId", birthday.externalId);
136 +                                                                                                                                       this.controller.serviceRequest('palm://com.palm.calendar', {\r
137 +                                                                                                                                           method: 'getEvent',\r
138 +                                                                                                                                           parameters: {
139 +                                                                                                                                               accountId: accountId,\r
140 +                                                                                                                                               externalId: birthday.externalId,
141 +                                                                                                                                               subscribe: false\r
142 +                                                                                                                                           },\r
143 +                                                                                                                                           onSuccess: function(event) {
144 +                                                                                                                                               // copy EventId and try to update
145 +                                                                                                                                               birthday.eventId = event.eventId;
146 +                                                                                                                                                       this.controller.serviceRequest('palm://com.palm.calendar', {\r
147 +                                                                                                                                                           method: 'updateEvent',\r
148 +                                                                                                                                                           parameters: {
149 +                                                                                                                                                               event: birthday,\r
150 +                                                                                                                                                               subscribe: false\r
151 +                                                                                                                                                           },\r
152 +                                                                                                                                                           onSuccess: function() {
153 +                                                                                                                                                               Mojo.Log.error("Birthday event successfully updated.");
154 +                                                                                                                                                               sumBirthDayUpdated++;
155 +                                                                                                                                                               if ((cntCurContacts == cntAllContacts) && (sumBirthDayEvents == (sumBirthDayUpdated + sumBirthDayCreated + sumBirthDayFailed))) {
156 +                                                                                                                                                                       Mojo.Controller.getAppController().showBanner("Updated: " + sumBirthDayUpdated + " Created: " + sumBirthDayCreated + " Failed: " + sumBirthDayFailed, "", "");
157 +                                                                                                                                                               }
158 +                                                                                                                                                           },
159 +                                                                                                                                                           onFailure: function(error) {
160 +                                                                                                                                                               Mojo.Log.error("Update fails, giving up:", error.errorText);
161 +                                                                                                                                                               sumBirthDayFailed++;
162 +                                                                                                                                                               if ((cntCurContacts == cntAllContacts) && (sumBirthDayEvents == (sumBirthDayUpdated + sumBirthDayCreated + sumBirthDayFailed))) {
163 +                                                                                                                                                                       Mojo.Controller.getAppController().showBanner("Updated: " + sumBirthDayUpdated + " Created: " + sumBirthDayCreated + " Failed: " + sumBirthDayFailed, "", "");
164 +                                                                                                                                                               }
165 +                                                                                                                                                           }
166 +                                                                                                                                                       });
167 +                                                                                                                                           }.bind(this),\r
168 +                                                                                                                                           onFailure: function(error) {
169 +                                                                                                                                               // event wasn't found, try to create it
170 +                                                                                                                                               Mojo.Log.error("GetEvent fails, try create:", error.errorText);
171 +                                                                                                                                               // try to create a new event
172 +                                                                                                                                                       this.controller.serviceRequest('palm://com.palm.calendar', {\r
173 +                                                                                                                                                           method: 'setEvent',\r
174 +                                                                                                                                                           parameters: birthday,
175 +                                                                                                                                                           onSuccess: function() {
176 +                                                                                                                                                               Mojo.Log.error("Birthday event successfully created.");
177 +                                                                                                                                                               sumBirthDayCreated++;
178 +                                                                                                                                                               if ((cntCurContacts == cntAllContacts) && (sumBirthDayEvents == (sumBirthDayUpdated + sumBirthDayCreated + sumBirthDayFailed))) {
179 +                                                                                                                                                                       Mojo.Controller.getAppController().showBanner("Updated: " + sumBirthDayUpdated + " Created: " + sumBirthDayCreated + " Failed: " + sumBirthDayFailed, "", "");
180 +                                                                                                                                                               }
181 +                                                                                                                                                           },\r
182 +                                                                                                                                                           onFailure: function(error) {
183 +                                                                                                                                                               Mojo.Log.error("Error on create, giving up:", error.errorText);
184 +                                                                                                                                                               sumBirthDayFailed++;
185 +                                                                                                                                                               if ((cntCurContacts == cntAllContacts) && (sumBirthDayEvents == (sumBirthDayUpdated + sumBirthDayCreated + sumBirthDayFailed))) {
186 +                                                                                                                                                                       Mojo.Controller.getAppController().showBanner("Updated: " + sumBirthDayUpdated + " Created: " + sumBirthDayCreated + " Failed: " + sumBirthDayFailed, "", "");
187 +                                                                                                                                                               }
188 +                                                                                                                                                           }\r
189 +                                                                                                                                                       });
190 +                                                                                                                                       }.bind(this)\r
191 +                                                                                                                                       });
192 +                                                                                                                               }
193 +                                                                                                                       }
194 +                                                                                                               if ((cntCurContacts == cntAllContacts) && (sumBirthDayEvents == (sumBirthDayUpdated + sumBirthDayCreated + sumBirthDayFailed))) {
195 +                                                                                                                       Mojo.Controller.getAppController().showBanner("Updated: " + sumBirthDayUpdated + " Created: " + sumBirthDayCreated + " Failed: " + sumBirthDayFailed, "", "");
196 +                                                                                                               }
197 +                                                                                                                       //Mojo.Log.error(Object.toJSON(returnParams));
198 +                                                                                                               }.bind(this),
199 +                                                                                                               this.controller, null, function() {}
200 +                                                                                                       );
201 +                                                                                               }
202 +                                                                                       }.bind(this)
203 +                                                                               });
204 +                                                                       }.bind(this)
205 +                                                               );
206 +                                                       }
207 +                                               }.bind(this),
208 +                                           onFailure: function(error) {
209 +                                                       Mojo.Log.error("Calendarlist failed", error.errorText);
210 +                                           }.bind(this)\r
211 +                                       });
212 +                               }.bind(this),
213 +                               this.controller
214 +                       );
215                 }
216                 if (event.type == Mojo.Event.commandEnable && event.command == Mojo.Menu.prefsCmd) {
217                         // funkay!  this is apparently how you declare you want to activate the prefs menu item