javacommons/fileutils/src.s60/filemanager.cpp
branchRCL_3
changeset 19 04becd199f91
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:  FileManager - Provides file management facility
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include <f32file.h>
       
    20 
       
    21 #include "logger.h"
       
    22 #include "s60commonutils.h"
       
    23 
       
    24 #include "filemanager.h"
       
    25 
       
    26 using namespace java::fileutils;
       
    27 using namespace java::util;
       
    28 
       
    29 void ReplaceCharacters(TPtr& aPtr, TUint8 aReplacedChar, TUint8 aNewChar,
       
    30                        TBool aOnlyFirstMatch)
       
    31 {
       
    32     for (TInt i = 0; i < aPtr.Length(); ++i)
       
    33     {
       
    34         if (aReplacedChar == aPtr[i])
       
    35         {
       
    36             aPtr[i] = aNewChar;
       
    37             if (aOnlyFirstMatch)
       
    38                 return;
       
    39         }
       
    40     }
       
    41 }
       
    42 
       
    43 void ReplaceCharacters(TPtr& aPtr, TUint8 aReplacedChar, TUint8 aNewChar)
       
    44 {
       
    45     ReplaceCharacters(aPtr, aReplacedChar, aNewChar, EFalse);
       
    46 }
       
    47 
       
    48 bool FileManager::copy(std::wstring aSource, std::wstring aDest, bool override)
       
    49 {
       
    50     JELOG2(EJavaFile);
       
    51 
       
    52     LOG1(EJavaFile, EInfo, "FileManager::copy: Source     : %S",
       
    53          aSource.c_str());
       
    54     LOG1(EJavaFile, EInfo, "FileManager::copy: Destination: %S", aDest.c_str());
       
    55 
       
    56     HBufC* source = S60CommonUtils::wstringToDes(aSource.c_str());
       
    57     HBufC* dest = S60CommonUtils::wstringToDes(aDest.c_str());
       
    58 
       
    59     TPtr sourcePtr(source->Des());
       
    60     TPtr destPtr(dest->Des());
       
    61 
       
    62     ReplaceCharacters(sourcePtr, '/', '\\');
       
    63     ReplaceCharacters(destPtr, '/', '\\');
       
    64 
       
    65     RFs fsSession;
       
    66     int error = fsSession.Connect();
       
    67     bool retVal = false;
       
    68 
       
    69     if (KErrNone == error)
       
    70     {
       
    71         // Create file management object
       
    72         CFileMan* fileMan = 0;
       
    73         TRAP(error, fileMan = CFileMan::NewL(fsSession));
       
    74 
       
    75         if (KErrNone == error)
       
    76         {
       
    77             TRAP(error,
       
    78                  CleanupStack::PushL(fileMan);
       
    79 
       
    80                  // Do copy (here synchronously)
       
    81                  TInt option = CFileMan::ENone;
       
    82                  if (override)
       
    83         {
       
    84             option = CFileMan::EOverWrite;
       
    85         }
       
    86 
       
    87         TInt success = fileMan->Copy(sourcePtr, destPtr, option);
       
    88 
       
    89         if (KErrNone == success)
       
    90         {
       
    91             retVal = true;
       
    92 
       
    93         }
       
    94         else
       
    95         {
       
    96             WLOG1(EJavaFile, "Copy Failed: Error: %d", success);
       
    97             }
       
    98             // Clean up
       
    99             CleanupStack::PopAndDestroy();
       
   100             // close file server session
       
   101             fsSession.Close();
       
   102                 );
       
   103         }
       
   104     }
       
   105     delete source;
       
   106     delete dest;
       
   107     return retVal;
       
   108 }
       
   109 
       
   110 bool FileManager::move(std::wstring aSource, std::wstring aDest, bool override)
       
   111 {
       
   112     JELOG2(EJavaFile);
       
   113 
       
   114     LOG1(EJavaFile, EInfo, "FileManager::move: Source     : %S",
       
   115          aSource.c_str());
       
   116     LOG1(EJavaFile, EInfo, "FileManager::move: Destination: %S", aDest.c_str());
       
   117 
       
   118     HBufC* source = S60CommonUtils::wstringToDes(aSource.c_str());
       
   119     HBufC* dest = S60CommonUtils::wstringToDes(aDest.c_str());
       
   120 
       
   121     TPtr sourcePtr(source->Des());
       
   122     TPtr destPtr(dest->Des());
       
   123 
       
   124     ReplaceCharacters(sourcePtr, '/', '\\');
       
   125     ReplaceCharacters(destPtr, '/', '\\');
       
   126 
       
   127     RFs fsSession;
       
   128     int error = fsSession.Connect();
       
   129     bool retVal = false;
       
   130 
       
   131     if (KErrNone == error)
       
   132     {
       
   133         // Create file management object
       
   134         CFileMan* fileMan = 0;
       
   135         TRAP(error, fileMan = CFileMan::NewL(fsSession));
       
   136 
       
   137         if (KErrNone == error)
       
   138         {
       
   139             TRAP(error,
       
   140                  CleanupStack::PushL(fileMan);
       
   141 
       
   142                  // Do copy (here synchronously)
       
   143                  TInt option = CFileMan::ENone;
       
   144                  if (override)
       
   145         {
       
   146             option = CFileMan::EOverWrite;
       
   147         }
       
   148 
       
   149         TInt success = fileMan->Move(sourcePtr, destPtr, option);
       
   150         if (KErrNone == success)
       
   151         {
       
   152             retVal = true;
       
   153 
       
   154         }
       
   155         else
       
   156         {
       
   157             WLOG1(EJavaFile, "Move Failed: Error: %d", success);
       
   158             }
       
   159             // Clean up
       
   160             CleanupStack::PopAndDestroy();
       
   161             // close file server session
       
   162             fsSession.Close();
       
   163                 );
       
   164         }
       
   165     }
       
   166     delete source;
       
   167     delete dest;
       
   168     return retVal;
       
   169 }
       
   170 
       
   171 bool FileManager::copyAll(std::wstring aSource, std::wstring aDest,
       
   172                           bool override)
       
   173 {
       
   174     JELOG2(EJavaFile);
       
   175 
       
   176     LOG1(EJavaFile, EInfo, "FileManager::copyAll: Source     : %S",
       
   177          aSource.c_str());
       
   178     LOG1(EJavaFile, EInfo, "FileManager::copyAll: Destination: %S",
       
   179          aDest.c_str());
       
   180 
       
   181     HBufC* source = S60CommonUtils::wstringToDes(aSource.c_str());
       
   182     HBufC* dest = S60CommonUtils::wstringToDes(aDest.c_str());
       
   183 
       
   184     TPtr sourcePtr(source->Des());
       
   185     TPtr destPtr(dest->Des());
       
   186 
       
   187     ReplaceCharacters(sourcePtr, '/', '\\');
       
   188     ReplaceCharacters(destPtr, '/', '\\');
       
   189 
       
   190     RFs fsSession;
       
   191     int error = fsSession.Connect();
       
   192     bool retVal = false;
       
   193 
       
   194     if (KErrNone == error)
       
   195     {
       
   196         // Create file management object
       
   197         CFileMan* fileMan = 0;
       
   198         TRAP(error, fileMan = CFileMan::NewL(fsSession));
       
   199 
       
   200         if (KErrNone == error)
       
   201         {
       
   202             LOG(EJavaFile, EInfo, "FileManager::copyAll Creating FileMan ok");
       
   203             TRAP(error,
       
   204                  CleanupStack::PushL(fileMan);
       
   205 
       
   206                  // Do copy (here synchronously)
       
   207                  TInt option = CFileMan::ERecurse;
       
   208                  // First recurse through sub directories.
       
   209                  TInt success = fileMan->Copy(sourcePtr, destPtr, option);
       
   210                  LOG1(EJavaFile, EInfo,
       
   211                       "FileManager::copyAll First Copy: %d", success);
       
   212 
       
   213                  if (override)
       
   214         {
       
   215             LOG(EJavaFile, EInfo,
       
   216                 "FileManager::copyAll Setting option to overwrite");
       
   217                 option = CFileMan::EOverWrite;
       
   218 
       
   219                 // now overwrite.
       
   220                 success = fileMan->Copy(sourcePtr, destPtr, option);
       
   221                 LOG1(EJavaFile, EInfo,
       
   222                      "FileManager::copyAll Second Copy: %d", success);
       
   223             }
       
   224 
       
   225             // It will already be ensured that source and destination
       
   226             // exist. So, KErrNotFound will only be returned if the
       
   227             // folders are empty. In which case, it is still a success.
       
   228             if ((KErrNone == success) || (KErrNotFound == success))
       
   229         {
       
   230             retVal = true;
       
   231         }
       
   232         else
       
   233         {
       
   234             WLOG1(EJavaFile,
       
   235                   "FileManager::copyAll Failed: Error: %d",
       
   236                   success);
       
   237                 retVal = false;
       
   238             }
       
   239 
       
   240             // Clean up
       
   241             CleanupStack::PopAndDestroy();
       
   242             // close file server session
       
   243             fsSession.Close();
       
   244                 );
       
   245         }
       
   246     }
       
   247     delete source;
       
   248     delete dest;
       
   249     LOG1(EJavaFile, EInfo, "FileManager::copyAll: returns: %d", retVal);
       
   250     return retVal;
       
   251 }
       
   252 
       
   253 bool FileManager::deleteAll(std::wstring aSource)
       
   254 {
       
   255     JELOG2(EJavaFile);
       
   256 
       
   257     LOG1(EJavaFile, EInfo, "FileManager::deleteAll: Source     : %S",
       
   258          aSource.c_str());
       
   259 
       
   260     HBufC* source = S60CommonUtils::wstringToDes(aSource.c_str());
       
   261     TPtr sourcePtr(source->Des());
       
   262     ReplaceCharacters(sourcePtr, '/', '\\');
       
   263 
       
   264     RFs fsSession;
       
   265     int error = fsSession.Connect();
       
   266     bool retVal = false;
       
   267 
       
   268     if (KErrNone == error)
       
   269     {
       
   270         // Create file management object
       
   271         CFileMan* fileMan = 0;
       
   272         TRAP(error, fileMan = CFileMan::NewL(fsSession));
       
   273 
       
   274         if (KErrNone == error)
       
   275         {
       
   276             TRAP(error,
       
   277                  CleanupStack::PushL(fileMan);
       
   278 
       
   279                  LOG(EJavaFile, EInfo,
       
   280                      "FileManager::deleteAll Creating FileMan ok");
       
   281                  // First, lets clear all read-only attributes from files in
       
   282                  // the folder
       
   283                  TInt success = fileMan->Attribs(sourcePtr, 0,
       
   284                                                  KEntryAttReadOnly,
       
   285                                                  0, CFileMan::ERecurse);
       
   286 
       
   287                  // Ignoring return value of attribs()
       
   288                  LOG1(EJavaFile, EInfo,
       
   289                       "FileManager::deleteAll() Changing attribs: Returns %d",
       
   290                       success);
       
   291 
       
   292                  success = fileMan->RmDir(sourcePtr);
       
   293 
       
   294                  if (KErrNone == success)
       
   295         {
       
   296             retVal = true;
       
   297         }
       
   298         else
       
   299         {
       
   300             WLOG1(EJavaFile, "Delete All Failed: Error: %d",
       
   301                   success);
       
   302             }
       
   303             // Clean up
       
   304             CleanupStack::PopAndDestroy();
       
   305             // close file server session
       
   306             fsSession.Close();
       
   307                 );
       
   308         }
       
   309     }
       
   310     delete source;
       
   311     return retVal;
       
   312 }