examples/Base/FileServer/Notify/Notify.cpp

00001 /*
00002 Copyright (c) 2000-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:  
00028 */
00029 
00030 
00031 #include "Notify.h"
00032 
00033 _LIT(KStarted,"Copy started\n");
00034 _LIT(KComplete,"Copy complete\n");
00035 _LIT(KProgress,"%d bytes copied\n");
00036 
00037 // 
00038 // TFileCopyProgressMonitor
00039 // 
00040 
00041 TFileCopyProgressMonitor::TFileCopyProgressMonitor(CFileMan& aFileMan)
00042 :iFileMan(aFileMan)
00043         {       
00044         }
00045 
00046 // Called when copy operation started
00047 MFileManObserver::TControl TFileCopyProgressMonitor::NotifyFileManStarted()
00048         {
00049         console->Printf(KStarted);
00050         return EContinue;
00051         }
00052 
00053 // Called when copy operation is in progress
00054 MFileManObserver::TControl TFileCopyProgressMonitor::NotifyFileManOperation()
00055         {
00056         console->Printf(KProgress,iFileMan.BytesTransferredByCopyStep());
00057         return EContinue;
00058         }
00059 
00060 // Called when copy operation is complete
00061 MFileManObserver::TControl TFileCopyProgressMonitor::NotifyFileManEnded()
00062         {
00063         console->Printf(KComplete);
00064         return EContinue;
00065         }
00066         
00067 
00068 //
00069 // Do the example
00070 //
00071 
00072 LOCAL_C void doExampleL()
00073     {
00074         _LIT(KSourcePath,"dataFile\\");
00075         _LIT(KDestinationPath,"dataFileCopy\\");
00076         
00077         TInt err;
00078 
00079           // Connect session
00080         RFs fsSession;
00081         User::LeaveIfError(fsSession.Connect());
00082         
00083           // create the private directory
00084         User::LeaveIfError(fsSession.CreatePrivatePath(RFs::GetSystemDrive()));
00085         
00086           // Set the session path to the private directory
00087     User::LeaveIfError(fsSession.SetSessionToPrivate(RFs::GetSystemDrive()));
00088         
00089           // Get the private directory name
00090         TFileName path;
00091         fsSession.PrivatePath(path);
00092   
00093       // Create a source and a target directory
00094         RBuf pathSource;
00095         RBuf pathDestination;
00096         
00097         pathSource.CreateL(sizeof(TFileName));
00098         pathSource.CleanupClosePushL();
00099         pathSource.Append(path);
00100         pathSource.Append(KSourcePath);
00101         
00102         pathDestination.CreateL(sizeof(TFileName));
00103         pathDestination.CleanupClosePushL();
00104         pathDestination.Append(path);
00105         pathDestination.Append(KDestinationPath);
00106         
00107         err=fsSession.MkDir(pathSource);
00108         if (err!=KErrAlreadyExists)
00109                 User::LeaveIfError(err);
00110         
00111         err=fsSession.MkDir(pathDestination);
00112         if (err!=KErrAlreadyExists)
00113                 User::LeaveIfError(err);
00114         
00115           // Create 2 source files in the source directory
00116           // so that we have something to copy.
00117     _LIT(KFile1,"dataFile1.txt");
00118     _LIT(KFile2,"dataFile2.txt");
00119     _LIT8(KFile1Data,"File data qwertyu");
00120     _LIT8(KFile2Data,"File data iop");
00121     
00122     fsSession.SetSessionPath(pathSource);
00123     RFile file;
00124     
00125     User::LeaveIfError(file.Replace(fsSession,KFile1,EFileWrite));
00126         User::LeaveIfError(file.Write(KFile1Data));
00127         User::LeaveIfError(file.Flush()); // Commit data
00128         file.Close(); // close file having finished with it
00129         
00130         User::LeaveIfError(file.Replace(fsSession,KFile2,EFileWrite));
00131         User::LeaveIfError(file.Write(KFile2Data));
00132         User::LeaveIfError(file.Flush()); // Commit data
00133         file.Close(); // close file having finished with it
00134     
00135         // Create file management object
00136         CFileMan* fileMan = CFileMan::NewL(fsSession);
00137         CleanupStack::PushL(fileMan); 
00138 
00139            // Create file management notification object and set to observe
00140         TFileCopyProgressMonitor fileCopyProgressMonitor(*fileMan);
00141         fileMan->SetObserver(&fileCopyProgressMonitor);
00142         
00143           // Do copy (here synchronously)
00144         fileMan->Copy(pathSource,pathDestination);
00145         
00146           // Clean up 3 items 
00147         CleanupStack::PopAndDestroy(3);
00148         
00149           // Close file server session and note that we have not deleted
00150           // any files or directories created here.
00151         fsSession.Close();
00152         }
00153 

Generated by  doxygen 1.6.2