javaextensions/pim/javasrc/com/nokia/mj/impl/pim/PIMManager.java
changeset 76 4ad59aaee882
parent 21 2a9601315dfc
child 72 1f0034e370aa
child 83 26b2b12093af
--- a/javaextensions/pim/javasrc/com/nokia/mj/impl/pim/PIMManager.java	Thu Sep 02 20:20:40 2010 +0300
+++ b/javaextensions/pim/javasrc/com/nokia/mj/impl/pim/PIMManager.java	Fri Sep 17 08:28:21 2010 +0300
@@ -20,12 +20,13 @@
 package com.nokia.mj.impl.pim;
 
 // IMPORTS
-
 import javax.microedition.pim.PIM;
 import javax.microedition.pim.PIMException;
 import javax.microedition.pim.PIMItem;
 import javax.microedition.pim.PIMList;
 import java.io.UnsupportedEncodingException;
+import java.util.Enumeration;
+import java.util.Vector;
 import com.nokia.mj.impl.pim.ErrorString;
 import com.nokia.mj.impl.pim.GenericException;
 import com.nokia.mj.impl.rt.support.ApplicationUtils;
@@ -33,7 +34,8 @@
 import com.nokia.mj.impl.rt.support.ShutdownListener;
 import com.nokia.mj.impl.security.utils.SecurityPromptMessage;
 import com.nokia.mj.impl.pim.utils.NativeError;
-
+import com.nokia.mj.impl.utils.Tokenizer;
+import com.nokia.mj.impl.pim.Calendar;
 
 // CLASS DEFINITION
 /**
@@ -72,6 +74,7 @@
 
     /** Serializer. */
     private Serializer iSerializer;
+    private Vector iCalInfo;
 
     // Methods
 
@@ -98,7 +101,7 @@
      * Creates PIMManager.
      * Direct creation of a PIMManager is prohibited.
      */
-    private PIMManager()
+    public PIMManager()
     {
         super();
         setShutdownListener();
@@ -111,6 +114,7 @@
         }
 
         iSerializer = new Serializer(iManagerHandle);
+        iCalInfo = new Vector();
     }
 
     /**
@@ -164,7 +168,6 @@
                     iManagerHandle = 0;
                 }
             }
-
         });
     }
 
@@ -178,13 +181,18 @@
     }
 
     // Methods from PIM
-
     public synchronized PIMList openPIMList(int aPimListType, int aMode)
     throws PIMException
     {
-        return doOpenPIMList(aPimListType, aMode, null);
+        return doOpenPIMList(aPimListType, aMode, null, null);
     }
 
+
+    /**
+     * this method is used to open existsing calendar, create new calendar and delete existsing calendar by passing string  as name.
+     * function will parse the string and do the operation.
+     * user as to pass the string in given format only
+     */
     public synchronized PIMList openPIMList(int aPimListType, int aMode,
                                             String aName) throws PIMException
     {
@@ -193,17 +201,130 @@
             throw new NullPointerException(ErrorString.OPENING_LISTS_FAILED_COLON +
                                            ErrorString.LIST_NAME_IS_NULL);
         }
+        //Check if the aName is as per the MultipleCalendar Parameter definition
+        //aName = [calendarname "/"] listname ["?operation=" ["create" | "delete"]]
+        if (isMultiCalendarParam(aName))
+        {
 
-        return doOpenPIMList(aPimListType, aMode, aName);
+            String calendarName = null;
+            String operation = null;
+            String listName = null;
+
+            listName = getListName(aName);
+            operation = getOperation(aName);
+            calendarName = aName.substring(0, aName.indexOf("/"));
+            if (isListNameValid(listName))
+            {
+                if (operation == null)
+                {
+                    if (calendarName != null)
+                    {
+                        //This is the case of opening an existing calendar
+                        return doOpenPIMList(aPimListType, aMode, listName, "C:" + calendarName);
+                    }
+                    else
+                    {
+                        //This is the case with IllegalArgumentException
+                        throw new IllegalArgumentException("Calendar Name is NULL");
+                    }
+                }
+                else if (operation.equals("create"))
+                {
+                    createCalendar(calendarName);
+                    int[] error = new int[1];
+                    int listHandle = _openPIMList(iManagerHandle, aPimListType, listName, calendarName, error);
+                    PIMListImpl pimList = new EventListImpl(listHandle, aMode);
+                    return pimList;
+                }
+                else if (operation.equals("delete"))
+                {
+                    deleteCalendar(calendarName);
+                    int listHandle = 0;
+                    PIMListImpl pimList = new EventListImpl(listHandle, aMode);
+                    return pimList;
+                }
+                else
+                {
+                    throw new IllegalArgumentException("Invalid operation");
+                }
+            } // if isListNameValid(listName) block ends here
+            else
+            {
+                throw new IllegalArgumentException("Invalid List name");
+            }
+        } // if isMultiCalendarParam(aName) block ends here
+        else
+        {
+            //This is not the case of Multiple Calendar, so follow the default calendar path
+            return doOpenPIMList(aPimListType, aMode, aName);
+        }
+    }
+
+    private boolean isMultiCalendarParam(String aListTypeName)
+    {
+        //If there is "?" and/or "=" is present in aListTypeName
+        //return true
+        boolean ret = false;
+        if ((aListTypeName.indexOf("/") != -1) || (aListTypeName.indexOf("?") != -1) || (aListTypeName.indexOf("=") != -1))
+        {
+            ret = true;
+        }
+        return ret;
+    }
+
+    private String getListName(String aName)
+    {
+        // Get Operation
+        String operation = "?operation=";
+        int operationIndex = aName.indexOf(operation);
+
+        if (operationIndex == -1)
+        {
+            operationIndex = aName.length();
+        }
+
+        return aName.substring(aName.indexOf("/") + 1, operationIndex);
+    }
+
+    private boolean isListNameValid(String aListName)
+    {
+        String lists[] = listPIMLists(PIM.EVENT_LIST);
+        boolean listExist = false;
+
+        for (int i = 0; i < lists.length; i++)
+        {
+            if (aListName.trim().equalsIgnoreCase(lists[i]))
+            {
+                listExist = true;
+                break;
+            }
+        }
+
+        return listExist;
+    }
+
+    private String getOperation(String aName)
+    {
+        String aOperation = null;
+        String operation = "?operation=";
+        int operationIndex = aName.indexOf(operation);
+
+        if (operationIndex != -1)
+        {
+            aOperation = aName.substring(operationIndex + operation.length());
+        }
+
+
+        return aOperation;
+
     }
 
     public synchronized String[] listPIMLists(int aPimListType)
     {
-        if (aPimListType != PIM.CONTACT_LIST && aPimListType != PIM.EVENT_LIST
-                && aPimListType != PIM.TODO_LIST)
+
+        if (aPimListType != PIM.CONTACT_LIST && aPimListType != PIM.EVENT_LIST && aPimListType != PIM.TODO_LIST)
         {
-            throw new java.lang.IllegalArgumentException(ErrorString.LISTING_FAILED_DOT +
-                    ErrorString.INVALID_LIST_TYPE_COLON + aPimListType);
+            throw new java.lang.IllegalArgumentException(ErrorString.LISTING_FAILED_DOT + ErrorString.INVALID_LIST_TYPE_COLON + aPimListType);
         }
         // Ensure permission
         getPermission(aPimListType, PIM.READ_ONLY);
@@ -233,7 +354,121 @@
     {
         return iSerializer.supportedSerialFormats(aPimListType);
     }
+    /**
+        * Enumerates the calendars currently present in the device.
+        *
+        * @return A list of Calendar names
+        * @throws java.lang.SecurityException
+        *             if the application is not given permission to read PIM lists
+        */
+    public synchronized Calendar[] listCalendars()
+    {
+        // security check
+        ApplicationUtils appUtils = ApplicationUtils.getInstance();
+        PIMPermissionImpl per = new PIMPermissionImpl("pim://*", PIMPermissionImpl.ACTION_READ_EVENTS + "," + PIMPermissionImpl.ACTION_READ_TODOS);
+        appUtils.checkPermission(per);
+        int[] error = new int[1];
 
+        String[] calendarFileLists = _listCalendars(iManagerHandle, error);
+        String[] calendarNameLists = _listCalendarNames(iManagerHandle, error);
+        if (!NativeError.checkSuccess(error[0]))
+        {
+            throw new GenericException(ErrorString.GENERAL_ERROR_COLON + error[0]);
+        }
+        Vector tokens = new Vector();
+        Vector tokenNames = new Vector();
+        int length = calendarFileLists.length;
+        for (int i = 0; i < length; i++)
+        {
+            String str[] = Tokenizer.split(calendarFileLists[i], ":");
+            String strname[] = Tokenizer.split(calendarNameLists[i], ":");
+
+
+            if (str[0].equals("C"))
+            {
+                tokens.addElement(str[1]);
+
+            }
+            if (strname[0].equals("C"))
+            {
+
+                tokenNames.addElement(strname[1]);
+            }
+            else
+            {
+
+                tokenNames.addElement(strname[0]);
+            }
+        }
+        String[] calendarLists = new String[tokens.size()];
+        String[] calendarNames = new String[tokenNames.size()];
+        tokens.copyInto(calendarLists);
+        tokenNames.copyInto(calendarNames);
+        Calendar[] calendarobjlist = new Calendar[calendarLists.length];
+        for (int i = 0; i < calendarLists.length; i++)
+        {
+            Calendar cal = new Calendar(calendarLists[i], calendarNames[i]);
+            calendarobjlist[i] = cal;
+        }
+        //return calendarLists;
+        return calendarobjlist;
+    }
+
+
+    private synchronized void createCalendar(String aCalName)
+    {
+
+        String displayName = aCalName;
+        // security check
+        ApplicationUtils appUtils = ApplicationUtils.getInstance();
+        PIMPermissionImpl per = new PIMPermissionImpl("pim://*", PIMPermissionImpl.ACTION_WRITE_EVENTS + "," +  PIMPermissionImpl.ACTION_WRITE_TODOS);
+        appUtils.checkPermission(per);
+        String fileName = "C:" + aCalName;
+        int error = _createCalendar(iManagerHandle, fileName,displayName);
+        NativeError.handleCreateCalendarError(error, aCalName);
+    }
+
+    private synchronized void deleteCalendar(String aCalName)
+    {
+        // security check
+        ApplicationUtils appUtils = ApplicationUtils.getInstance();
+        PIMPermissionImpl per = new PIMPermissionImpl(PIMPermissionImpl.ACTION_WRITE_EVENTS + "," + PIMPermissionImpl.ACTION_WRITE_TODOS, aCalName, null, -1);
+        appUtils.checkPermission(per);
+
+        boolean isCalPresent = false;
+        String fileName = "C:" + aCalName;
+        int index;
+        for (index = 0; index < iCalInfo.size(); index++)
+        {
+            CalendarListInfo calList = (CalendarListInfo) iCalInfo.elementAt(index);
+            String calName = calList.iCalName;
+            if (calName.equals(fileName))
+            {
+                Enumeration e = calList.iList.elements();
+                while (e.hasMoreElements())
+                {
+                    PIMList list = (PIMList) e.nextElement();
+                    try
+                    {
+                        list.close();
+                    }
+                    catch (PIMException ex)
+                    {
+                        //throw new GenericException("Delete entry failed. " + ex.toString());
+                    }
+                }
+                calList.iList.removeAllElements();
+                isCalPresent = true;
+                break;
+            }
+        }
+        int error = _deleteCalendar(iManagerHandle, fileName);
+        NativeError.handleDeleteCalendarError(error, aCalName);
+        if (isCalPresent)
+        {
+            iCalInfo.removeElementAt(index);
+        }
+    }
 
     /**
      * getPermission
@@ -297,27 +532,17 @@
         appUtils.checkPermission(per);
     }
 
-
-    // New private methods
-    /**
-     * Common implementation of the list opening. Arguments and permissions are
-     * pre-checked.
-     *
-     * @param aName
-     *            If null, default list is opened.
-     */
     private PIMList doOpenPIMList(int aPimListType, int aMode, String aName)
     throws PIMException
     {
-        if (aPimListType != PIM.CONTACT_LIST && aPimListType != PIM.EVENT_LIST
-                && aPimListType != PIM.TODO_LIST)
+
+        if (aPimListType != PIM.CONTACT_LIST && aPimListType != PIM.EVENT_LIST && aPimListType != PIM.TODO_LIST)
         {
             throw new java.lang.IllegalArgumentException(
                 ErrorString.INVALID_LIST_TYPE_COLON + aPimListType);
         }
 
-        if (aMode != PIM.READ_ONLY && aMode != PIM.WRITE_ONLY
-                && aMode != PIM.READ_WRITE)
+        if (aMode != PIM.READ_ONLY && aMode != PIM.WRITE_ONLY && aMode != PIM.READ_WRITE)
         {
             throw new java.lang.IllegalArgumentException(
                 ErrorString.INVALID_MODE_COLON + aMode);
@@ -339,8 +564,7 @@
         }
         int[] error = new int[1];
         int listHandle = _openPIMList(
-
-                             iManagerHandle, aPimListType, aName, error); // if null, open default
+                             iManagerHandle, aPimListType, aName, null, error); // if null, open default
         // list
         NativeError.handleOpenPIMListError(error[0], aPimListType, aName);
 
@@ -378,6 +602,86 @@
 
         return pimList;
     }
+///////////////////////
+    // New private methods
+
+    /**
+     * Common implementation of the list opening. Arguments and permissions are
+     * pre-checked.
+     *
+     * @param aName
+     *            If null, default list is opened.
+     */
+    private PIMList doOpenPIMList(int aPimListType, int aMode, String aName, String aCalName)
+    throws PIMException
+    {
+
+        if (aPimListType != PIM.CONTACT_LIST && aPimListType != PIM.EVENT_LIST && aPimListType != PIM.TODO_LIST)
+        {
+            throw new java.lang.IllegalArgumentException(
+                ErrorString.INVALID_LIST_TYPE_COLON + aPimListType);
+        }
+
+        if (aMode != PIM.READ_ONLY && aMode != PIM.WRITE_ONLY && aMode != PIM.READ_WRITE)
+        {
+            throw new java.lang.IllegalArgumentException(
+                ErrorString.INVALID_MODE_COLON + aMode);
+        }
+
+        // Both permissions must be checked separately if aMode is
+        // PIM.READ_WRITE
+        if (aMode == PIM.READ_WRITE)
+        {
+            // First ensure read access permission
+            // Get localized text info for the security dialog
+            getPermission(aPimListType, PIM.READ_ONLY);
+            getPermission(aPimListType, PIM.WRITE_ONLY);
+
+        }
+        else
+        {
+            getPermission(aPimListType, aMode);
+        }
+        int[] error = new int[1];
+
+        int listHandle = _openPIMList(
+                             iManagerHandle, aPimListType, aName, aCalName, error);
+        // list
+        NativeError.handleOpenPIMListError(error[0], aPimListType, aName);
+
+        // Create new pim list of right type
+        PIMListImpl pimList = null;
+
+        switch (aPimListType)
+        {
+        case PIM.CONTACT_LIST:
+        {
+            pimList = new ContactListImpl(listHandle, aMode);
+            break;
+        }
+
+        case PIM.EVENT_LIST:
+        {
+            pimList = new EventListImpl(listHandle, aMode);
+            break;
+        }
+
+        case PIM.TODO_LIST:
+        {
+            pimList = new ToDoListImpl(listHandle, aMode);
+            break;
+        }
+
+        default:
+        {
+            // We should never end up here
+            throw new PIMException(ErrorString.GENERAL_ERROR,
+                                   PIMException.GENERAL_ERROR);
+        }
+        }
+
+        return pimList;
+    }
 
 
     // Native operations
@@ -398,11 +702,33 @@
      *         value on error.
      */
     private native int _openPIMList(int aManagerHandle, int aPimListType,
-                                    String aPimListName, int[] aError);
+                                    String aPimListName, String aCalName,
+                                    int[] aError);
 
     private native String[] _listPIMLists(int aManagerHandle, int aPimListType,
                                           int[] aError);
 
+    private native String[] _listCalendars(int aManagerHandle, int[] aError);
+
+    private native String[] _listCalendarNames(int aManagerHandle, int[] aError);
+
+    private native int _createCalendar(int aManagerHandle, String aCalName, String aDisplayName);
+
+    private native int _deleteCalendar(int aManagerHandle, String aCalName);
+
+    class CalendarListInfo
+    {
+
+        String iCalName;
+        Vector iList;
+
+        CalendarListInfo(String aCalName, PIMList aPIMList)
+        {
+            iCalName = aCalName;
+            iList = new Vector();
+            iList.addElement(aPIMList);
+        }
+    }
 }
 
 // End of file