18 |
18 |
19 // PACKAGE |
19 // PACKAGE |
20 package com.nokia.mj.impl.pim; |
20 package com.nokia.mj.impl.pim; |
21 |
21 |
22 // IMPORTS |
22 // IMPORTS |
|
23 |
23 import javax.microedition.pim.PIM; |
24 import javax.microedition.pim.PIM; |
24 import javax.microedition.pim.PIMException; |
25 import javax.microedition.pim.PIMException; |
25 import javax.microedition.pim.PIMItem; |
26 import javax.microedition.pim.PIMItem; |
26 import javax.microedition.pim.PIMList; |
27 import javax.microedition.pim.PIMList; |
27 import java.io.UnsupportedEncodingException; |
28 import java.io.UnsupportedEncodingException; |
28 import java.util.Enumeration; |
|
29 import java.util.Vector; |
|
30 import com.nokia.mj.impl.pim.ErrorString; |
29 import com.nokia.mj.impl.pim.ErrorString; |
31 import com.nokia.mj.impl.pim.GenericException; |
30 import com.nokia.mj.impl.pim.GenericException; |
32 import com.nokia.mj.impl.rt.support.ApplicationUtils; |
31 import com.nokia.mj.impl.rt.support.ApplicationUtils; |
33 import com.nokia.mj.impl.rt.support.Finalizer; |
32 import com.nokia.mj.impl.rt.support.Finalizer; |
34 import com.nokia.mj.impl.rt.support.ShutdownListener; |
33 import com.nokia.mj.impl.rt.support.ShutdownListener; |
35 import com.nokia.mj.impl.security.utils.SecurityPromptMessage; |
34 import com.nokia.mj.impl.security.utils.SecurityPromptMessage; |
36 import com.nokia.mj.impl.pim.utils.NativeError; |
35 import com.nokia.mj.impl.pim.utils.NativeError; |
37 import com.nokia.mj.impl.utils.Tokenizer; |
36 |
38 import com.nokia.mj.impl.pim.Calendar; |
|
39 |
37 |
40 // CLASS DEFINITION |
38 // CLASS DEFINITION |
41 /** |
39 /** |
42 * PIM singleton implementation. PIM class acts as a starting point to the PIM |
40 * PIM singleton implementation. PIM class acts as a starting point to the PIM |
43 * API. Only single instance of the class is created and that instance is |
41 * API. Only single instance of the class is created and that instance is |
179 { |
176 { |
180 return iManagerHandle; |
177 return iManagerHandle; |
181 } |
178 } |
182 |
179 |
183 // Methods from PIM |
180 // Methods from PIM |
|
181 |
184 public synchronized PIMList openPIMList(int aPimListType, int aMode) |
182 public synchronized PIMList openPIMList(int aPimListType, int aMode) |
185 throws PIMException |
183 throws PIMException |
186 { |
184 { |
187 return doOpenPIMList(aPimListType, aMode, null, null); |
185 return doOpenPIMList(aPimListType, aMode, null); |
188 } |
186 } |
189 |
187 |
190 |
|
191 /** |
|
192 * this method is used to open existsing calendar, create new calendar and delete existsing calendar by passing string as name. |
|
193 * function will parse the string and do the operation. |
|
194 * user as to pass the string in given format only |
|
195 */ |
|
196 public synchronized PIMList openPIMList(int aPimListType, int aMode, |
188 public synchronized PIMList openPIMList(int aPimListType, int aMode, |
197 String aName) throws PIMException |
189 String aName) throws PIMException |
198 { |
190 { |
199 if (aName == null) |
191 if (aName == null) |
200 { |
192 { |
201 throw new NullPointerException(ErrorString.OPENING_LISTS_FAILED_COLON + |
193 throw new NullPointerException(ErrorString.OPENING_LISTS_FAILED_COLON + |
202 ErrorString.LIST_NAME_IS_NULL); |
194 ErrorString.LIST_NAME_IS_NULL); |
203 } |
195 } |
204 //Check if the aName is as per the MultipleCalendar Parameter definition |
196 |
205 //aName = [calendarname "/"] listname ["?operation=" ["create" | "delete"]] |
197 return doOpenPIMList(aPimListType, aMode, aName); |
206 if (isMultiCalendarParam(aName)) |
|
207 { |
|
208 |
|
209 String calendarName = null; |
|
210 String operation = null; |
|
211 String listName = null; |
|
212 |
|
213 listName = getListName(aName); |
|
214 operation = getOperation(aName); |
|
215 calendarName = aName.substring(0, aName.indexOf("/")); |
|
216 if (isListNameValid(listName)) |
|
217 { |
|
218 if (operation == null) |
|
219 { |
|
220 if (calendarName != null) |
|
221 { |
|
222 //This is the case of opening an existing calendar |
|
223 return doOpenPIMList(aPimListType, aMode, listName, "C:" + calendarName); |
|
224 } |
|
225 else |
|
226 { |
|
227 //This is the case with IllegalArgumentException |
|
228 throw new IllegalArgumentException("Calendar Name is NULL"); |
|
229 } |
|
230 } |
|
231 else if (operation.equals("create")) |
|
232 { |
|
233 createCalendar(calendarName); |
|
234 int[] error = new int[1]; |
|
235 int listHandle = _openPIMList(iManagerHandle, aPimListType, listName, calendarName, error); |
|
236 PIMListImpl pimList = new EventListImpl(listHandle, aMode); |
|
237 return pimList; |
|
238 } |
|
239 else if (operation.equals("delete")) |
|
240 { |
|
241 deleteCalendar(calendarName); |
|
242 int listHandle = 0; |
|
243 PIMListImpl pimList = new EventListImpl(listHandle, aMode); |
|
244 return pimList; |
|
245 } |
|
246 else |
|
247 { |
|
248 throw new IllegalArgumentException("Invalid operation"); |
|
249 } |
|
250 } // if isListNameValid(listName) block ends here |
|
251 else |
|
252 { |
|
253 throw new IllegalArgumentException("Invalid List name"); |
|
254 } |
|
255 } // if isMultiCalendarParam(aName) block ends here |
|
256 else |
|
257 { |
|
258 //This is not the case of Multiple Calendar, so follow the default calendar path |
|
259 return doOpenPIMList(aPimListType, aMode, aName); |
|
260 } |
|
261 } |
|
262 |
|
263 private boolean isMultiCalendarParam(String aListTypeName) |
|
264 { |
|
265 //If there is "?" and/or "=" is present in aListTypeName |
|
266 //return true |
|
267 boolean ret = false; |
|
268 if ((aListTypeName.indexOf("/") != -1) || (aListTypeName.indexOf("?") != -1) || (aListTypeName.indexOf("=") != -1)) |
|
269 { |
|
270 ret = true; |
|
271 } |
|
272 return ret; |
|
273 } |
|
274 |
|
275 private String getListName(String aName) |
|
276 { |
|
277 // Get Operation |
|
278 String operation = "?operation="; |
|
279 int operationIndex = aName.indexOf(operation); |
|
280 |
|
281 if (operationIndex == -1) |
|
282 { |
|
283 operationIndex = aName.length(); |
|
284 } |
|
285 |
|
286 return aName.substring(aName.indexOf("/") + 1, operationIndex); |
|
287 } |
|
288 |
|
289 private boolean isListNameValid(String aListName) |
|
290 { |
|
291 String lists[] = listPIMLists(PIM.EVENT_LIST); |
|
292 boolean listExist = false; |
|
293 |
|
294 for (int i = 0; i < lists.length; i++) |
|
295 { |
|
296 if (aListName.trim().equalsIgnoreCase(lists[i])) |
|
297 { |
|
298 listExist = true; |
|
299 break; |
|
300 } |
|
301 } |
|
302 |
|
303 return listExist; |
|
304 } |
|
305 |
|
306 private String getOperation(String aName) |
|
307 { |
|
308 String aOperation = null; |
|
309 String operation = "?operation="; |
|
310 int operationIndex = aName.indexOf(operation); |
|
311 |
|
312 if (operationIndex != -1) |
|
313 { |
|
314 aOperation = aName.substring(operationIndex + operation.length()); |
|
315 } |
|
316 |
|
317 |
|
318 return aOperation; |
|
319 |
|
320 } |
198 } |
321 |
199 |
322 public synchronized String[] listPIMLists(int aPimListType) |
200 public synchronized String[] listPIMLists(int aPimListType) |
323 { |
201 { |
324 |
202 if (aPimListType != PIM.CONTACT_LIST && aPimListType != PIM.EVENT_LIST |
325 if (aPimListType != PIM.CONTACT_LIST && aPimListType != PIM.EVENT_LIST && aPimListType != PIM.TODO_LIST) |
203 && aPimListType != PIM.TODO_LIST) |
326 { |
204 { |
327 throw new java.lang.IllegalArgumentException(ErrorString.LISTING_FAILED_DOT + ErrorString.INVALID_LIST_TYPE_COLON + aPimListType); |
205 throw new java.lang.IllegalArgumentException(ErrorString.LISTING_FAILED_DOT + |
|
206 ErrorString.INVALID_LIST_TYPE_COLON + aPimListType); |
328 } |
207 } |
329 // Ensure permission |
208 // Ensure permission |
330 getPermission(aPimListType, PIM.READ_ONLY); |
209 getPermission(aPimListType, PIM.READ_ONLY); |
331 |
210 |
332 int[] error = new int[1]; |
211 int[] error = new int[1]; |
352 |
231 |
353 public synchronized String[] supportedSerialFormats(int aPimListType) |
232 public synchronized String[] supportedSerialFormats(int aPimListType) |
354 { |
233 { |
355 return iSerializer.supportedSerialFormats(aPimListType); |
234 return iSerializer.supportedSerialFormats(aPimListType); |
356 } |
235 } |
357 /** |
236 |
358 * Enumerates the calendars currently present in the device. |
|
359 * |
|
360 * @return A list of Calendar names |
|
361 * @throws java.lang.SecurityException |
|
362 * if the application is not given permission to read PIM lists |
|
363 */ |
|
364 public synchronized Calendar[] listCalendars() |
|
365 { |
|
366 // security check |
|
367 ApplicationUtils appUtils = ApplicationUtils.getInstance(); |
|
368 PIMPermissionImpl per = new PIMPermissionImpl("pim://*", PIMPermissionImpl.ACTION_READ_EVENTS + "," + PIMPermissionImpl.ACTION_READ_TODOS); |
|
369 appUtils.checkPermission(per); |
|
370 int[] error = new int[1]; |
|
371 |
|
372 String[] calendarFileLists = _listCalendars(iManagerHandle, error); |
|
373 String[] calendarNameLists = _listCalendarNames(iManagerHandle, error); |
|
374 if (!NativeError.checkSuccess(error[0])) |
|
375 { |
|
376 throw new GenericException(ErrorString.GENERAL_ERROR_COLON + error[0]); |
|
377 } |
|
378 Vector tokens = new Vector(); |
|
379 Vector tokenNames = new Vector(); |
|
380 int length = calendarFileLists.length; |
|
381 for (int i = 0; i < length; i++) |
|
382 { |
|
383 String str[] = Tokenizer.split(calendarFileLists[i], ":"); |
|
384 String strname[] = Tokenizer.split(calendarNameLists[i], ":"); |
|
385 |
|
386 |
|
387 if (str[0].equals("C")) |
|
388 { |
|
389 tokens.addElement(str[1]); |
|
390 |
|
391 } |
|
392 if (strname[0].equals("C")) |
|
393 { |
|
394 |
|
395 tokenNames.addElement(strname[1]); |
|
396 } |
|
397 else |
|
398 { |
|
399 |
|
400 tokenNames.addElement(strname[0]); |
|
401 } |
|
402 } |
|
403 String[] calendarLists = new String[tokens.size()]; |
|
404 String[] calendarNames = new String[tokenNames.size()]; |
|
405 tokens.copyInto(calendarLists); |
|
406 tokenNames.copyInto(calendarNames); |
|
407 Calendar[] calendarobjlist = new Calendar[calendarLists.length]; |
|
408 for (int i = 0; i < calendarLists.length; i++) |
|
409 { |
|
410 Calendar cal = new Calendar(calendarLists[i], calendarNames[i]); |
|
411 calendarobjlist[i] = cal; |
|
412 } |
|
413 //return calendarLists; |
|
414 return calendarobjlist; |
|
415 } |
|
416 |
|
417 |
|
418 private synchronized void createCalendar(String aCalName) |
|
419 { |
|
420 |
|
421 String displayName = aCalName; |
|
422 // security check |
|
423 ApplicationUtils appUtils = ApplicationUtils.getInstance(); |
|
424 PIMPermissionImpl per = new PIMPermissionImpl("pim://*", PIMPermissionImpl.ACTION_WRITE_EVENTS + "," + PIMPermissionImpl.ACTION_WRITE_TODOS); |
|
425 appUtils.checkPermission(per); |
|
426 String fileName = "C:" + aCalName; |
|
427 int error = _createCalendar(iManagerHandle, fileName,displayName); |
|
428 NativeError.handleCreateCalendarError(error, aCalName); |
|
429 } |
|
430 |
|
431 private synchronized void deleteCalendar(String aCalName) |
|
432 { |
|
433 // security check |
|
434 ApplicationUtils appUtils = ApplicationUtils.getInstance(); |
|
435 PIMPermissionImpl per = new PIMPermissionImpl(PIMPermissionImpl.ACTION_WRITE_EVENTS + "," + PIMPermissionImpl.ACTION_WRITE_TODOS, aCalName, null, -1); |
|
436 appUtils.checkPermission(per); |
|
437 |
|
438 boolean isCalPresent = false; |
|
439 String fileName = "C:" + aCalName; |
|
440 int index; |
|
441 for (index = 0; index < iCalInfo.size(); index++) |
|
442 { |
|
443 CalendarListInfo calList = (CalendarListInfo) iCalInfo.elementAt(index); |
|
444 String calName = calList.iCalName; |
|
445 if (calName.equals(fileName)) |
|
446 { |
|
447 Enumeration e = calList.iList.elements(); |
|
448 while (e.hasMoreElements()) |
|
449 { |
|
450 PIMList list = (PIMList) e.nextElement(); |
|
451 try |
|
452 { |
|
453 list.close(); |
|
454 } |
|
455 catch (PIMException ex) |
|
456 { |
|
457 //throw new GenericException("Delete entry failed. " + ex.toString()); |
|
458 } |
|
459 } |
|
460 calList.iList.removeAllElements(); |
|
461 isCalPresent = true; |
|
462 break; |
|
463 } |
|
464 } |
|
465 int error = _deleteCalendar(iManagerHandle, fileName); |
|
466 NativeError.handleDeleteCalendarError(error, aCalName); |
|
467 if (isCalPresent) |
|
468 { |
|
469 iCalInfo.removeElementAt(index); |
|
470 } |
|
471 } |
|
472 |
237 |
473 /** |
238 /** |
474 * getPermission |
239 * getPermission |
475 * Ensures permissions for PIM operations. |
240 * Ensures permissions for PIM operations. |
476 * The possible operations in this case are reading from |
241 * The possible operations in this case are reading from |
530 PIMPermissionImpl per = new PIMPermissionImpl("pim://*", action); |
295 PIMPermissionImpl per = new PIMPermissionImpl("pim://*", action); |
531 // Ensure permission from PIM API security |
296 // Ensure permission from PIM API security |
532 appUtils.checkPermission(per); |
297 appUtils.checkPermission(per); |
533 } |
298 } |
534 |
299 |
535 private PIMList doOpenPIMList(int aPimListType, int aMode, String aName) |
300 |
536 throws PIMException |
|
537 { |
|
538 |
|
539 if (aPimListType != PIM.CONTACT_LIST && aPimListType != PIM.EVENT_LIST && aPimListType != PIM.TODO_LIST) |
|
540 { |
|
541 throw new java.lang.IllegalArgumentException( |
|
542 ErrorString.INVALID_LIST_TYPE_COLON + aPimListType); |
|
543 } |
|
544 |
|
545 if (aMode != PIM.READ_ONLY && aMode != PIM.WRITE_ONLY && aMode != PIM.READ_WRITE) |
|
546 { |
|
547 throw new java.lang.IllegalArgumentException( |
|
548 ErrorString.INVALID_MODE_COLON + aMode); |
|
549 } |
|
550 |
|
551 // Both permissions must be checked separately if aMode is |
|
552 // PIM.READ_WRITE |
|
553 if (aMode == PIM.READ_WRITE) |
|
554 { |
|
555 // First ensure read access permission |
|
556 // Get localized text info for the security dialog |
|
557 getPermission(aPimListType, PIM.READ_ONLY); |
|
558 getPermission(aPimListType, PIM.WRITE_ONLY); |
|
559 |
|
560 } |
|
561 else |
|
562 { |
|
563 getPermission(aPimListType, aMode); |
|
564 } |
|
565 int[] error = new int[1]; |
|
566 int listHandle = _openPIMList( |
|
567 iManagerHandle, aPimListType, aName, null, error); // if null, open default |
|
568 // list |
|
569 NativeError.handleOpenPIMListError(error[0], aPimListType, aName); |
|
570 |
|
571 |
|
572 // Create new pim list of right type |
|
573 PIMListImpl pimList = null; |
|
574 |
|
575 switch (aPimListType) |
|
576 { |
|
577 case PIM.CONTACT_LIST: |
|
578 { |
|
579 pimList = new ContactListImpl(listHandle, aMode); |
|
580 break; |
|
581 } |
|
582 |
|
583 case PIM.EVENT_LIST: |
|
584 { |
|
585 pimList = new EventListImpl(listHandle, aMode); |
|
586 break; |
|
587 } |
|
588 |
|
589 case PIM.TODO_LIST: |
|
590 { |
|
591 pimList = new ToDoListImpl(listHandle, aMode); |
|
592 break; |
|
593 } |
|
594 |
|
595 default: |
|
596 { |
|
597 // We should never end up here |
|
598 throw new PIMException(ErrorString.GENERAL_ERROR, |
|
599 PIMException.GENERAL_ERROR); |
|
600 } |
|
601 } |
|
602 |
|
603 return pimList; |
|
604 } |
|
605 /////////////////////// |
|
606 // New private methods |
301 // New private methods |
607 |
|
608 /** |
302 /** |
609 * Common implementation of the list opening. Arguments and permissions are |
303 * Common implementation of the list opening. Arguments and permissions are |
610 * pre-checked. |
304 * pre-checked. |
611 * |
305 * |
612 * @param aName |
306 * @param aName |
613 * If null, default list is opened. |
307 * If null, default list is opened. |
614 */ |
308 */ |
615 private PIMList doOpenPIMList(int aPimListType, int aMode, String aName, String aCalName) |
309 private PIMList doOpenPIMList(int aPimListType, int aMode, String aName) |
616 throws PIMException |
310 throws PIMException |
617 { |
311 { |
618 |
312 if (aPimListType != PIM.CONTACT_LIST && aPimListType != PIM.EVENT_LIST |
619 if (aPimListType != PIM.CONTACT_LIST && aPimListType != PIM.EVENT_LIST && aPimListType != PIM.TODO_LIST) |
313 && aPimListType != PIM.TODO_LIST) |
620 { |
314 { |
621 throw new java.lang.IllegalArgumentException( |
315 throw new java.lang.IllegalArgumentException( |
622 ErrorString.INVALID_LIST_TYPE_COLON + aPimListType); |
316 ErrorString.INVALID_LIST_TYPE_COLON + aPimListType); |
623 } |
317 } |
624 |
318 |
625 if (aMode != PIM.READ_ONLY && aMode != PIM.WRITE_ONLY && aMode != PIM.READ_WRITE) |
319 if (aMode != PIM.READ_ONLY && aMode != PIM.WRITE_ONLY |
|
320 && aMode != PIM.READ_WRITE) |
626 { |
321 { |
627 throw new java.lang.IllegalArgumentException( |
322 throw new java.lang.IllegalArgumentException( |
628 ErrorString.INVALID_MODE_COLON + aMode); |
323 ErrorString.INVALID_MODE_COLON + aMode); |
629 } |
324 } |
630 |
325 |
641 else |
336 else |
642 { |
337 { |
643 getPermission(aPimListType, aMode); |
338 getPermission(aPimListType, aMode); |
644 } |
339 } |
645 int[] error = new int[1]; |
340 int[] error = new int[1]; |
646 |
|
647 int listHandle = _openPIMList( |
341 int listHandle = _openPIMList( |
648 iManagerHandle, aPimListType, aName, aCalName, error); |
342 |
|
343 iManagerHandle, aPimListType, aName, error); // if null, open default |
649 // list |
344 // list |
650 NativeError.handleOpenPIMListError(error[0], aPimListType, aName); |
345 NativeError.handleOpenPIMListError(error[0], aPimListType, aName); |
|
346 |
651 |
347 |
652 // Create new pim list of right type |
348 // Create new pim list of right type |
653 PIMListImpl pimList = null; |
349 PIMListImpl pimList = null; |
654 |
350 |
655 switch (aPimListType) |
351 switch (aPimListType) |
700 * |
396 * |
701 * @return Handle to new native side PIM list of given type or negative |
397 * @return Handle to new native side PIM list of given type or negative |
702 * value on error. |
398 * value on error. |
703 */ |
399 */ |
704 private native int _openPIMList(int aManagerHandle, int aPimListType, |
400 private native int _openPIMList(int aManagerHandle, int aPimListType, |
705 String aPimListName, String aCalName, |
401 String aPimListName, int[] aError); |
706 int[] aError); |
|
707 |
402 |
708 private native String[] _listPIMLists(int aManagerHandle, int aPimListType, |
403 private native String[] _listPIMLists(int aManagerHandle, int aPimListType, |
709 int[] aError); |
404 int[] aError); |
710 |
405 |
711 private native String[] _listCalendars(int aManagerHandle, int[] aError); |
|
712 |
|
713 private native String[] _listCalendarNames(int aManagerHandle, int[] aError); |
|
714 |
|
715 private native int _createCalendar(int aManagerHandle, String aCalName, String aDisplayName); |
|
716 |
|
717 private native int _deleteCalendar(int aManagerHandle, String aCalName); |
|
718 |
|
719 class CalendarListInfo |
|
720 { |
|
721 |
|
722 String iCalName; |
|
723 Vector iList; |
|
724 |
|
725 CalendarListInfo(String aCalName, PIMList aPIMList) |
|
726 { |
|
727 iCalName = aCalName; |
|
728 iList = new Vector(); |
|
729 iList.addElement(aPIMList); |
|
730 } |
|
731 } |
|
732 } |
406 } |
733 |
407 |
734 // End of file |
408 // End of file |