javacommons/gcfprotocols/file/javasrc.s60/com/nokia/mj/impl/file/FileSystemUtils.java
branchRCL_3
changeset 19 04becd199f91
child 23 98ccebc37403
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 package com.nokia.mj.impl.file;
       
    19 
       
    20 import java.util.Vector;
       
    21 import java.util.Hashtable;
       
    22 
       
    23 import com.nokia.mj.impl.fileutils.DriveInfo;
       
    24 import com.nokia.mj.impl.fileutils.DriveUtilities;
       
    25 import com.nokia.mj.impl.rt.support.Jvm;
       
    26 import com.nokia.mj.impl.utils.Logger;
       
    27 import com.nokia.mj.impl.utils.Tokenizer;
       
    28 import com.nokia.mj.impl.rt.support.ApplicationInfo;
       
    29 
       
    30 /**
       
    31  * Provides methods to retrieve information that are platform specific. These
       
    32  * would include list of roots present and their localized names based on the
       
    33  * needs of the platform.
       
    34  */
       
    35 public final class FileSystemUtils
       
    36 {
       
    37     private static String iDefaultRoot;
       
    38 
       
    39     private static String iDefaultMemoryCard;
       
    40 
       
    41     private static String iDefaultTemporaryDrive;
       
    42 
       
    43     private static String iDefaultRomDrive;
       
    44 
       
    45     private static String iAppDomain;
       
    46 
       
    47     private static ApplicationInfo iAppInfo;
       
    48 
       
    49     static
       
    50     {
       
    51         try
       
    52         {
       
    53             Jvm.loadSystemLibrary("javafile");
       
    54         }
       
    55         catch (Exception e)
       
    56         {
       
    57             FileLogger.ELog("Unable to load javafile native library");
       
    58         }
       
    59 
       
    60         iDefaultRoot = getDefaultDrive();
       
    61         iDefaultTemporaryDrive = getTemporaryDrive();
       
    62         iDefaultRomDrive = getRomDrive();
       
    63         iDefaultMemoryCard = getMemoryCardDrive();
       
    64 
       
    65         // Load application information at startup
       
    66         iAppInfo = ApplicationInfo.getInstance();
       
    67 
       
    68         // Load application domain
       
    69         iAppDomain = iAppInfo.getProtectionDomain();
       
    70     }
       
    71 
       
    72     public static String getProtectionDomain()
       
    73     {
       
    74         return iAppDomain;
       
    75     }
       
    76 
       
    77     /**
       
    78      * Lists the currently available drives on the device.
       
    79      *
       
    80      * @return vector of drives.
       
    81      */
       
    82     public static Vector listRoots()
       
    83     {
       
    84         DriveInfo[] drives = DriveUtilities.getAccessibleDrives();
       
    85 
       
    86         Vector roots = new Vector();
       
    87         for (int index = 0; index < drives.length; index++)
       
    88         {
       
    89             String toAdd = drives[index].iRootPath.replace('\\', '/');
       
    90             if (FileAccessHelper.accessAllowed(toAdd,
       
    91                                                FileConstants.INTENT_READ, getProtectionDomain(), false))
       
    92             {
       
    93                 roots.addElement(toAdd);
       
    94             }
       
    95         }
       
    96         return roots;
       
    97     }
       
    98 
       
    99     public static String getDefaultRoot()
       
   100     {
       
   101         return iDefaultRoot;
       
   102     }
       
   103 
       
   104     private static String getDefaultDrive()
       
   105     {
       
   106         String drive = _getDefaultRootPath();
       
   107         drive = drive.replace('\\', '/');
       
   108         return drive;
       
   109     }
       
   110 
       
   111     public static String getTemporaryDrive()
       
   112     {
       
   113         String drive = _getTemporaryDrivePath();
       
   114         drive = drive.replace('\\', '/');
       
   115         return drive;
       
   116     }
       
   117 
       
   118     public static String getRomDrive()
       
   119     {
       
   120         String drive = _getRomDrivePath();
       
   121         drive = drive.replace('\\', '/');
       
   122         return drive;
       
   123     }
       
   124 
       
   125     public static String getMemoryCardDrive()
       
   126     {
       
   127         String drive = _getMemoryCardDrivePath();
       
   128         drive = drive.replace('\\', '/');
       
   129         return drive;
       
   130     }
       
   131 
       
   132     private static boolean isDefaultRoot(String aRoot)
       
   133     {
       
   134         if (aRoot.equalsIgnoreCase(iDefaultRoot))
       
   135         {
       
   136             return true;
       
   137         }
       
   138         return false;
       
   139     }
       
   140 
       
   141     private static boolean isTemporaryRoot(String aRoot)
       
   142     {
       
   143         if (aRoot.equalsIgnoreCase(iDefaultTemporaryDrive))
       
   144         {
       
   145             return true;
       
   146         }
       
   147         return false;
       
   148     }
       
   149 
       
   150     private static boolean isRom(String aRoot)
       
   151     {
       
   152         if (aRoot.equalsIgnoreCase(iDefaultRomDrive))
       
   153         {
       
   154             return true;
       
   155         }
       
   156         return false;
       
   157     }
       
   158 
       
   159     private static boolean isDefaultMemoryCardDrive(String aRoot)
       
   160     {
       
   161         if (aRoot.equalsIgnoreCase(iDefaultMemoryCard))
       
   162         {
       
   163             return true;
       
   164         }
       
   165         return false;
       
   166     }
       
   167 
       
   168     private static String getRemoteDriveName(final DriveInfo aDrive)
       
   169     {
       
   170         return FileConnectionTexts.get(FileConnectionTexts.FILE_REMOTEDRIVE_N,
       
   171                                        new String[] { "" + aDrive.iRootPath.charAt(0) });
       
   172     }
       
   173 
       
   174     private static String getMemoryCardDriveName(final DriveInfo aDrive)
       
   175     {
       
   176         return FileConnectionTexts.get(FileConnectionTexts.FILE_MEMORYCARD_N,
       
   177                                        new String[] { "" + aDrive.iRootPath.charAt(0) });
       
   178     }
       
   179 
       
   180     private static String getInternalDriveName(final DriveInfo aDrive)
       
   181     {
       
   182         return FileConnectionTexts.get(FileConnectionTexts.FILE_INTERNAL_N,
       
   183                                        new String[] { "" + aDrive.iRootPath.charAt(0) });
       
   184     }
       
   185 
       
   186     /**
       
   187      * Gives localized name of a given drive. It is done only in case the drive
       
   188      * is one of the default roots present. <br/> C:/ - Internal; D:/ -
       
   189      * Temporary; Z: - ROM
       
   190      *
       
   191      * @param aInfo
       
   192      * @return
       
   193      */
       
   194     private static String getLocalDriveName(final DriveInfo aInfo)
       
   195     {
       
   196         String driveName = aInfo.iRootPath.replace('\\', '/');
       
   197         if (isDefaultRoot(driveName))
       
   198         {
       
   199             return FileConnectionTexts.get(FileConnectionTexts.FILE_C_DRIVE);
       
   200         }
       
   201 
       
   202         if (isTemporaryRoot(driveName))
       
   203         {
       
   204             return FileConnectionTexts.get(FileConnectionTexts.FILE_D_DRIVE);
       
   205         }
       
   206 
       
   207         if (isRom(driveName))
       
   208         {
       
   209             return FileConnectionTexts.get(FileConnectionTexts.FILE_Z_DRIVE);
       
   210         }
       
   211         return null;
       
   212     }
       
   213 
       
   214     public static Vector getRootNames()
       
   215     {
       
   216         DriveInfo[] drives = DriveUtilities.getAccessibleDrives();
       
   217 
       
   218         // Vector will contain an array of drives that are present and that can
       
   219         // be viewed by the application.
       
   220         Vector roots = new Vector();
       
   221 
       
   222         for (int index = 0; index < drives.length; index++)
       
   223         {
       
   224             String toAdd = drives[index].iRootPath.replace('\\', '/');
       
   225             if (FileAccessHelper.accessAllowed(toAdd,
       
   226                                                FileConstants.INTENT_READ, getProtectionDomain(), false))
       
   227             {
       
   228                 roots.addElement(drives[index]);
       
   229             }
       
   230         }
       
   231 
       
   232         Vector rootNames = new Vector();
       
   233         for (int index = 0; index < roots.size(); index++)
       
   234         {
       
   235             DriveInfo info = (DriveInfo) roots.elementAt(index);
       
   236             String driveName = info.iRootPath.replace('\\', '/');
       
   237             String localName = null;
       
   238 
       
   239             // If Remote drive
       
   240             if (!info.iIsLocal)
       
   241             {
       
   242                 localName = getRemoteDriveName(info);
       
   243             }
       
   244             else if (info.iIsRemovable)
       
   245             {
       
   246                 if (isDefaultMemoryCardDrive(driveName))
       
   247                 {
       
   248                     localName = FileConnectionTexts
       
   249                                 .get(FileConnectionTexts.FILE_MEMORYCARD);
       
   250                 }
       
   251                 else
       
   252                 {
       
   253                     localName = getMemoryCardDriveName(info);
       
   254                 }
       
   255             }
       
   256             else
       
   257                 // It is internal drive
       
   258             {
       
   259                 localName = getLocalDriveName(info);
       
   260                 if (localName == null)
       
   261                 {
       
   262                     localName = getInternalDriveName(info);
       
   263                 }
       
   264             }
       
   265             rootNames.addElement(localName);
       
   266         }
       
   267         return rootNames;
       
   268     }
       
   269 
       
   270     public static String getPathOfProperty(int aValue)
       
   271     {
       
   272         return "file:///" + getPathOfProperty(aValue, true);
       
   273     }
       
   274 
       
   275     public static String getPathOfProperty(int aValue, boolean addRoot)
       
   276     {
       
   277         String retValue = null;
       
   278         int value = aValue;
       
   279 
       
   280         retValue = _getPathOfProperty(value, true);
       
   281 
       
   282         FileLogger.Log("FileSystemUtils: GetPathOfProperty returned: "
       
   283                        + retValue);
       
   284         retValue = retValue.replace('\\', '/');
       
   285 
       
   286         if (addRoot == false)
       
   287         {
       
   288             retValue = retValue.substring(2);
       
   289         }
       
   290         return retValue;
       
   291     }
       
   292 
       
   293     public static String getLocalizedName(int value)
       
   294     {
       
   295         String retValue = null;
       
   296         String propKey = null;
       
   297 
       
   298         switch (value)
       
   299         {
       
   300         case FileConstants.GRAPHICS_PROPERTY:
       
   301             propKey = FileConnectionTexts.FILE_GRAPHICS;
       
   302             break;
       
   303         case FileConstants.MUSIC_PROPERTY:
       
   304             propKey = FileConnectionTexts.FILE_MUSIC;
       
   305             break;
       
   306         case FileConstants.PHOTOS_PROPERTY:
       
   307             propKey = FileConnectionTexts.FILE_IMAGES;
       
   308             break;
       
   309         case FileConstants.RECORDINGS_PROPERTY:
       
   310             propKey = FileConnectionTexts.FILE_RECORDINGS;
       
   311             break;
       
   312         case FileConstants.TONES_PROPERTY:
       
   313             propKey = FileConnectionTexts.FILE_TONES;
       
   314             break;
       
   315         case FileConstants.VIDEOS_PROPERTY:
       
   316             propKey = FileConnectionTexts.FILE_VIDEOS;
       
   317             break;
       
   318         case FileConstants.MEMORY_CARD_PROPERTY:
       
   319             propKey = FileConnectionTexts.FILE_MEMORYCARD;
       
   320             break;
       
   321         case FileConstants.PRIVATE_PROPERTY:
       
   322             propKey = FileConnectionTexts.FILE_PRIVATE;
       
   323             break;
       
   324         default:
       
   325             retValue = null;
       
   326         }
       
   327         return FileConnectionTexts.get(propKey);
       
   328     }
       
   329 
       
   330     public static Vector getForbiddenPaths()
       
   331     {
       
   332         String pathString = _getForbiddenPaths();
       
   333         String[] pathArray = Tokenizer.split(pathString, "*");
       
   334         Vector paths = new Vector();
       
   335 
       
   336         for (int index = 0; index < pathArray.length - 1; index++)
       
   337         {
       
   338             String toAdd = pathArray[index];
       
   339             toAdd = toAdd.replace('\\', '/');
       
   340             paths.addElement(toAdd);
       
   341         }
       
   342         return paths;
       
   343     }
       
   344 
       
   345     public static Vector getRestrictedPaths()
       
   346     {
       
   347         String pathString = _getRestrictedPaths();
       
   348         String[] pathArray = Tokenizer.split(pathString, "*");
       
   349         Vector paths = new Vector();
       
   350 
       
   351         for (int index = 0; index < pathArray.length - 1; index++)
       
   352         {
       
   353             String toAdd = pathArray[index];
       
   354             toAdd = toAdd.replace('\\', '/');
       
   355             paths.addElement(toAdd);
       
   356         }
       
   357         return paths;
       
   358     }
       
   359 
       
   360     public static String getAppPrivateDir()
       
   361     {
       
   362         String appPrivateDir = iAppInfo.getRootPath();
       
   363         appPrivateDir = appPrivateDir.replace('\\', '/');
       
   364         appPrivateDir += "scratch/";
       
   365         return appPrivateDir;
       
   366     }
       
   367 
       
   368     private static native String _getMemoryCardDrivePath();
       
   369 
       
   370     private static native String _getTemporaryDrivePath();
       
   371 
       
   372     private static native String _getRomDrivePath();
       
   373 
       
   374     private static native String _getDefaultRootPath();
       
   375 
       
   376     private static native String _getPathOfProperty(int value, boolean addRoot);
       
   377 
       
   378     private static native String _getForbiddenPaths();
       
   379 
       
   380     private static native String _getRestrictedPaths();
       
   381 }