javacommons/fileutils/src.s60/filesystemutils.cpp
changeset 21 2a9601315dfc
child 64 0ea12c182930
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     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:  FileSystemUtilities - Wrapper to S60 Specific implementation
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include <memory>
       
    20 #include <f32file.h>
       
    21 #include "logger.h"
       
    22 #include "jstringutils.h"
       
    23 #include "s60commonutils.h"
       
    24 #include "fileutilities.h"
       
    25 #include "s60filesystemutilities.h"
       
    26 #include "javasymbianoslayer.h"
       
    27 
       
    28 using namespace std;
       
    29 using namespace java::util;
       
    30 using namespace java::fileutils;
       
    31 
       
    32 /*
       
    33  * Implementation of FileUtilities.h for C++ methods
       
    34  *
       
    35  */
       
    36 OS_EXPORT long long FileUtilities::availableSize(const std::wstring& aDrive)
       
    37 {
       
    38     JELOG2(EJavaFile);
       
    39     TInt64 size = -1;
       
    40 
       
    41     //In S60, first character is the drive letter.
       
    42     TChar driveChar(aDrive.at(0));
       
    43     TRAPD(err, S60FileSystemUtilities::GetAvailableSizeL(size, driveChar));
       
    44 
       
    45     if (KErrNone != err)
       
    46     {
       
    47         ELOG1(EJavaFile, "  FileUtilities::availableSize: Error: %d", err);
       
    48         return -1;
       
    49     }
       
    50     return (size);
       
    51 }
       
    52 
       
    53 OS_EXPORT long long FileUtilities::usedSize(const std::wstring& aDrive)
       
    54 {
       
    55     JELOG2(EJavaFile);
       
    56     TInt64 size = 0;
       
    57     TChar driveChar(aDrive.at(0));
       
    58     TRAPD(err, S60FileSystemUtilities::GetUsedSizeL(size, driveChar));
       
    59 
       
    60     if (KErrNone != err)
       
    61     {
       
    62         ELOG1(EJavaFile, "  FileUtilities::usedSize: Error: %d", err);
       
    63         return -1;
       
    64     }
       
    65     return (size);
       
    66 }
       
    67 
       
    68 OS_EXPORT long long FileUtilities::totalSize(const std::wstring& aDrive)
       
    69 {
       
    70     JELOG2(EJavaFile);
       
    71     TInt64 size = 0;
       
    72     TChar driveChar(aDrive.at(0));
       
    73     TRAPD(err, S60FileSystemUtilities::GetTotalSizeL(size, driveChar));
       
    74 
       
    75     if (KErrNone != err)
       
    76     {
       
    77         ELOG1(EJavaFile, "  FileUtilities::totalSize: Error: %d", err);
       
    78         return -1;
       
    79     }
       
    80     return (size);
       
    81 }
       
    82 
       
    83 OS_EXPORT bool FileUtilities::isHidden(const std::wstring& aFile)
       
    84 {
       
    85     JELOG2(EJavaFile);
       
    86     HBufC* name = S60CommonUtils::wstringToDes(aFile.c_str());
       
    87     //TDesC* str = &name;
       
    88     TBool hidden = EFalse;
       
    89     TRAP_IGNORE(S60FileSystemUtilities::IsHiddenL(hidden, name->Des()));
       
    90     delete name;
       
    91     return *reinterpret_cast<bool*>(&hidden);
       
    92 }
       
    93 
       
    94 OS_EXPORT std::wstring FileUtilities::listRoots()
       
    95 {
       
    96     JELOG2(EJavaFile);
       
    97     TDesC16 aDes(S60FileSystemUtilities::GetSupportedDrives());
       
    98     std::wstring retString((wchar_t*)aDes.Ptr());
       
    99     return retString;
       
   100 }
       
   101 
       
   102 OS_EXPORT int FileUtilities::setReadable(const std::wstring& /*aFile*/, bool /*aReadable*/)
       
   103 {
       
   104     // Note: In Symbian, we do not set the readable attribute to no access.
       
   105     // it is just ignored and canRead will always return true
       
   106     return 0;
       
   107 }
       
   108 
       
   109 OS_EXPORT int FileUtilities::setWritable(const std::wstring& aFile,
       
   110         bool aWritable)
       
   111 {
       
   112     JELOG2(EJavaFile);
       
   113     HBufC* name = S60CommonUtils::wstringToDes(aFile.c_str());
       
   114     TPtr uriBufPtr(name->Des());
       
   115     S60FileSystemUtilities::ReplaceCharacters(uriBufPtr, '/', '\\');
       
   116     RFs iRFs;
       
   117     int err = iRFs.Connect();
       
   118     if (KErrNone != err)
       
   119     {
       
   120         throw err;
       
   121         //return -1;
       
   122     }
       
   123 
       
   124     if (aWritable)
       
   125         err = iRFs.SetAtt(uriBufPtr, 0, KEntryAttReadOnly);
       
   126     else
       
   127         err = iRFs.SetAtt(uriBufPtr, KEntryAttReadOnly, 0);
       
   128 
       
   129     iRFs.Close();
       
   130     delete name;
       
   131     if (KErrNone != err)
       
   132     {
       
   133         throw err;
       
   134     }
       
   135     return 0;
       
   136 }
       
   137 
       
   138 
       
   139 OS_EXPORT std::wstring FileUtilities::getDirContents(const std::wstring& dName,
       
   140         const bool aIncludeHidden)
       
   141 {
       
   142     JELOG2(EJavaFile);
       
   143     LOG(EJavaFile, EInfo, "+FileUtilities::getDirContents()");
       
   144 
       
   145     std::wstring dirName = dName + L"/";
       
   146     auto_ptr<HBufC> name(0);
       
   147     name.reset(S60CommonUtils::wstringToDes(dirName.c_str()));
       
   148 
       
   149     TPtr uriBufPtr(name->Des());
       
   150     S60FileSystemUtilities::ReplaceCharacters(uriBufPtr, '/', '\\');
       
   151     std::wstring iFileListWrapper;
       
   152 
       
   153     RFs rFileServer;
       
   154     TInt error = rFileServer.Connect();
       
   155 
       
   156     LOG1(EJavaFile, EInfo, "  FileUtilities::getDirContents: RFs connect: %d",
       
   157          error);
       
   158 
       
   159     if (KErrNone == error)
       
   160     {
       
   161         CDir* fileAndDirList = NULL;
       
   162 
       
   163         // Include normal and system files
       
   164         TInt attributeList = KEntryAttNormal|KEntryAttSystem|KEntryAttDir;
       
   165         if (aIncludeHidden)
       
   166         {
       
   167             attributeList |= KEntryAttHidden;
       
   168         }
       
   169 
       
   170         error = rFileServer.GetDir(uriBufPtr, attributeList, ESortByName,
       
   171                                    fileAndDirList);
       
   172 
       
   173         LOG1(EJavaFile, EInfo, "  FileUtilities::getDirContents: RFs getdir: %d",
       
   174              error);
       
   175 
       
   176         if (KErrNone == error)
       
   177         {
       
   178             for (TInt index =0; index< fileAndDirList->Count(); index++)
       
   179             {
       
   180                 TEntry entry = (*fileAndDirList)[index];
       
   181                 TPtr16 TName = entry.iName.Des();
       
   182                 std::wstring retString((wchar_t*)desToWstring(TName));
       
   183                 LOG1(EJavaFile, EInfo, " FileUtilities::getDirContents:getDir: %S", retString.c_str());
       
   184 
       
   185                 iFileListWrapper += retString;
       
   186 
       
   187                 if (entry.IsDir())
       
   188                 {
       
   189                     // Append slash to indicate directory
       
   190                     iFileListWrapper += L"/*";
       
   191                 }
       
   192                 else
       
   193                 {
       
   194                     iFileListWrapper += L"*";
       
   195                 }
       
   196             }
       
   197             delete fileAndDirList;
       
   198         }
       
   199     }
       
   200     LOG(EJavaFile, EInfo, "-FileUtilities::getDirContents()");
       
   201     return iFileListWrapper;
       
   202 }