/** * Ensures that the given PIM list name is valid. * * @param pimListType CONTACT_LIST, EVENT_LIST or TODO_LIST * @param name name of the list * @throws PIMException if list with the specified name is not found */ private void validateName(int pimListType, String name) throws PIMException { String[] names = PIMHandler.getInstance().getListNames(pimListType); for (int i = 0; i < names.length; i++) { if (name.equals(names[i])) { return; } } throw new PIMException("PIM list does not exist: '" + name + "'"); }
/** * Opens the PIM list. * * @param pimListType CONTACT_LIST, EVENT_LIST or TODO_LIST * @param mode READ_ONLY, WRITE_ONLY or READ_WRITE * @return handle to opened PIM list * @throws PIMException if the list is not found */ public PIMList openPIMList(int pimListType, int mode) throws PIMException { validatePimListType(pimListType); validateMode(mode); checkPermissions(pimListType, mode); String listName = PIMHandler.getInstance().getDefaultListName(pimListType); if (listName == null) { throw new PIMException("List not available"); } return openPIMListImpl(pimListType, mode, listName); }
/** * Constructs a PIM list. * * @param pimList initial list * @param type type of list */ protected AbstractPIMItem(AbstractPIMList pimList, int type) { this.pimList = pimList; this.listType = type; pimHandler = PIMHandler.getInstance(); try { pimListHandle = pimList != null ? pimList.getHandle() : pimHandler.openList(type, null, PIM.READ_ONLY); dummyList = pimList == null; } catch (PIMException e) { e.printStackTrace(); throw new RuntimeException("Error while opening default list"); } }
/** * Does the same as openPIMList, without any validation * * @param pimListType CONTACT_LIST, EVENT_LIST or TODO_LIST * @param mode READ_ONLY, WRITE_ONLY or READ_WRITE * @param name name of the list * @return handle to opened PIM list * @throws PIMException if the list is not found */ private PIMList openPIMListImpl(int pimListType, int mode, String name) throws PIMException { AbstractPIMList list; PIMHandler handler = PIMHandler.getInstance(); Object listHandle = handler.openList(pimListType, name, mode); switch (pimListType) { case PIM.CONTACT_LIST: list = new ContactListImpl(name, mode, listHandle); break; case PIM.EVENT_LIST: list = new EventListImpl(name, mode, listHandle); break; case PIM.TODO_LIST: list = new ToDoListImpl(name, mode, listHandle); break; default: // pimListType has been verified throw new Error("Unreachable code"); } Object[] keys = handler.getListKeys(listHandle); for (int i = 0; i < keys.length; i++) { byte[] data = handler.getListElement(listHandle, keys[i]); String[] categories = handler.getListElementCategories(listHandle, keys[i]); try { PIMItem[] items = fromSerialFormat(new ByteArrayInputStream(data), "UTF-8", list); for (int j = 0; j < items.length; j++) { AbstractPIMItem item = (AbstractPIMItem) items[j]; item.setKey(keys[i]); list.addItem(item); item.setDefaultValues(); for (int index = 0; index < categories.length; index++) { item.addToCategory(categories[index]); } item.setModified(false); } } catch (UnsupportedEncodingException e) { throw new Error("UTF-8 not supported"); } catch (PIMException e) { // skip element } } return list; }
/** * Gets the current PIM lists. * * @param pimListType type of list to return * @return array of list names */ public String[] listPIMLists(int pimListType) { checkPermissions(pimListType, PIM.READ_ONLY); validatePimListType(pimListType); return PIMHandler.getInstance().getListNames(pimListType); }