examples/Base/FileServer/Notify/Notify.cpp

00001 // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
00002 // All rights reserved.
00003 // This component and the accompanying materials are made available
00004 // under the terms of "Eclipse Public License v1.0"
00005 // which accompanies this distribution, and is available
00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00007 //
00008 // Initial Contributors:
00009 // Nokia Corporation - initial contribution.
00010 //
00011 // Contributors:
00012 //
00013 // Description:
00014 //
00015 
00016 #include "Notify.h"
00017 
00018 _LIT(KStarted,"Copy started\n");
00019 _LIT(KComplete,"Copy complete\n");
00020 _LIT(KProgress,"%d bytes copied\n");
00021 
00022 // 
00023 // TFileCopyProgressMonitor
00024 // 
00025 
00026 TFileCopyProgressMonitor::TFileCopyProgressMonitor(CFileMan& aFileMan)
00027 :iFileMan(aFileMan)
00028         {       
00029         }
00030 
00031 // Called when copy operation started
00032 MFileManObserver::TControl TFileCopyProgressMonitor::NotifyFileManStarted()
00033         {
00034         console->Printf(KStarted);
00035         return EContinue;
00036         }
00037 
00038 // Called when copy operation is in progress
00039 MFileManObserver::TControl TFileCopyProgressMonitor::NotifyFileManOperation()
00040         {
00041         console->Printf(KProgress,iFileMan.BytesTransferredByCopyStep());
00042         return EContinue;
00043         }
00044 
00045 // Called when copy operation is complete
00046 MFileManObserver::TControl TFileCopyProgressMonitor::NotifyFileManEnded()
00047         {
00048         console->Printf(KComplete);
00049         return EContinue;
00050         }
00051         
00052 
00053 //
00054 // Do the example
00055 //
00056 
00057 LOCAL_C void doExampleL()
00058     {
00059         _LIT(KSourcePath,"dataFile\\");
00060         _LIT(KDestinationPath,"dataFileCopy\\");
00061         
00062         TInt err;
00063 
00064           // Connect session
00065         RFs fsSession;
00066         User::LeaveIfError(fsSession.Connect());
00067         
00068           // create the private directory
00069         User::LeaveIfError(fsSession.CreatePrivatePath(RFs::GetSystemDrive()));
00070         
00071           // Set the session path to the private directory
00072     User::LeaveIfError(fsSession.SetSessionToPrivate(RFs::GetSystemDrive()));
00073         
00074           // Get the private directory name
00075         TFileName path;
00076         fsSession.PrivatePath(path);
00077   
00078       // Create a source and a target directory
00079         RBuf pathSource;
00080         RBuf pathDestination;
00081         
00082         pathSource.CreateL(sizeof(TFileName));
00083         pathSource.CleanupClosePushL();
00084         pathSource.Append(path);
00085         pathSource.Append(KSourcePath);
00086         
00087         pathDestination.CreateL(sizeof(TFileName));
00088         pathDestination.CleanupClosePushL();
00089         pathDestination.Append(path);
00090         pathDestination.Append(KDestinationPath);
00091         
00092         err=fsSession.MkDir(pathSource);
00093         if (err!=KErrAlreadyExists)
00094                 User::LeaveIfError(err);
00095         
00096         err=fsSession.MkDir(pathDestination);
00097         if (err!=KErrAlreadyExists)
00098                 User::LeaveIfError(err);
00099         
00100           // Create 2 source files in the source directory
00101           // so that we have something to copy.
00102     _LIT(KFile1,"dataFile1.txt");
00103     _LIT(KFile2,"dataFile2.txt");
00104     _LIT8(KFile1Data,"File data qwertyu");
00105     _LIT8(KFile2Data,"File data iop");
00106     
00107     fsSession.SetSessionPath(pathSource);
00108     RFile file;
00109     
00110     User::LeaveIfError(file.Replace(fsSession,KFile1,EFileWrite));
00111         User::LeaveIfError(file.Write(KFile1Data));
00112         User::LeaveIfError(file.Flush()); // Commit data
00113         file.Close(); // close file having finished with it
00114         
00115         User::LeaveIfError(file.Replace(fsSession,KFile2,EFileWrite));
00116         User::LeaveIfError(file.Write(KFile2Data));
00117         User::LeaveIfError(file.Flush()); // Commit data
00118         file.Close(); // close file having finished with it
00119     
00120         // Create file management object
00121         CFileMan* fileMan = CFileMan::NewL(fsSession);
00122         CleanupStack::PushL(fileMan); 
00123 
00124            // Create file management notification object and set to observe
00125         TFileCopyProgressMonitor fileCopyProgressMonitor(*fileMan);
00126         fileMan->SetObserver(&fileCopyProgressMonitor);
00127         
00128           // Do copy (here synchronously)
00129         fileMan->Copy(pathSource,pathDestination);
00130         
00131           // Clean up 3 items 
00132         CleanupStack::PopAndDestroy(3);
00133         
00134           // Close file server session and note that we have not deleted
00135           // any files or directories created here.
00136         fsSession.Close();
00137         }
00138 

Generated by  doxygen 1.6.2