Symbian
Symbian Developer Library

SYMBIAN OS V9.4

Feedback

[Index] [Previous] [Next]


Notify: CFileMan observer

[Top]


Example code

Found in: examples\Base\FileServer\Notify

The files reproduced here are the main files contained in the examples directory. Some extra files may be needed to run the examples, and these will be found in the appropriate examples directory.

// Notify.h
//
// Copyright (C) Symbian Software Ltd 2000-2005.  All rights reserved.
#ifndef __NOTIFY_H
#define __NOTIFY_H

#include "CommonFramework.h"
#include <f32file.h>
    
/** 
    Monitors file copy events, and prints out the progress.
*/
class TFileCopyProgressMonitor : public MFileManObserver
    {
public:
    /** 
        Constructor.
        @param aFileMan Used to obtain the number of bytes currently copied 
    */
    TFileCopyProgressMonitor(CFileMan& aFileMan);
private:
    TControl NotifyFileManStarted();
    TControl NotifyFileManOperation();
    TControl NotifyFileManEnded(); 
private:
    CFileMan& iFileMan;
    };

#endif
// Notify.cpp
//
// Copyright (C) Symbian Software Ltd 2000-2005.  All rights reserved.

#include "Notify.h"

_LIT(KStarted,"Copy started\n");
_LIT(KComplete,"Copy complete\n");
_LIT(KProgress,"%d bytes copied\n");

// 
// TFileCopyProgressMonitor
// 

TFileCopyProgressMonitor::TFileCopyProgressMonitor(CFileMan& aFileMan)
:iFileMan(aFileMan)
    {   
    }

// Called when copy operation started
MFileManObserver::TControl TFileCopyProgressMonitor::NotifyFileManStarted()
    {
    console->Printf(KStarted);
    return EContinue;
    }

// Called when copy operation is in progress
MFileManObserver::TControl TFileCopyProgressMonitor::NotifyFileManOperation()
    {
    console->Printf(KProgress,iFileMan.BytesTransferredByCopyStep());
    return EContinue;
    }

// Called when copy operation is complete
MFileManObserver::TControl TFileCopyProgressMonitor::NotifyFileManEnded()
    {
    console->Printf(KComplete);
    return EContinue;
    }
    

//
// Do the example
//

LOCAL_C void doExampleL()
    {
    _LIT(KSourcePath,"dataFile\\");
    _LIT(KDestinationPath,"dataFileCopy\\");
    
    TInt err;

      // Connect session
    RFs fsSession;
    User::LeaveIfError(fsSession.Connect());
    
      // create the private directory
    User::LeaveIfError(fsSession.CreatePrivatePath(RFs::GetSystemDrive()));
    
      // Set the session path to the private directory
    User::LeaveIfError(fsSession.SetSessionToPrivate(RFs::GetSystemDrive()));
    
      // Get the private directory name
    TFileName path;
    fsSession.PrivatePath(path);
  
      // Create a source and a target directory
    RBuf pathSource;
    RBuf pathDestination;
    
    pathSource.CreateL(sizeof(TFileName));
    pathSource.CleanupClosePushL();
    pathSource.Append(path);
    pathSource.Append(KSourcePath);
    
    pathDestination.CreateL(sizeof(TFileName));
    pathDestination.CleanupClosePushL();
    pathDestination.Append(path);
    pathDestination.Append(KDestinationPath);
    
    err=fsSession.MkDir(pathSource);
    if (err!=KErrAlreadyExists)
        User::LeaveIfError(err);
    
    err=fsSession.MkDir(pathDestination);
    if (err!=KErrAlreadyExists)
        User::LeaveIfError(err);
    
      // Create 2 source files in the source directory
      // so that we have something to copy.
    _LIT(KFile1,"dataFile1.txt");
    _LIT(KFile2,"dataFile2.txt");
    _LIT8(KFile1Data,"File data qwertyu");
    _LIT8(KFile2Data,"File data iop");
    
    fsSession.SetSessionPath(pathSource);
    RFile file;
    
    User::LeaveIfError(file.Replace(fsSession,KFile1,EFileWrite));
    User::LeaveIfError(file.Write(KFile1Data));
    User::LeaveIfError(file.Flush()); // Commit data
    file.Close(); // close file having finished with it
    
    User::LeaveIfError(file.Replace(fsSession,KFile2,EFileWrite));
    User::LeaveIfError(file.Write(KFile2Data));
    User::LeaveIfError(file.Flush()); // Commit data
    file.Close(); // close file having finished with it
    
        // Create file management object
    CFileMan* fileMan = CFileMan::NewL(fsSession);
    CleanupStack::PushL(fileMan); 

       // Create file management notification object and set to observe
    TFileCopyProgressMonitor fileCopyProgressMonitor(*fileMan);
    fileMan->SetObserver(&fileCopyProgressMonitor);
    
      // Do copy (here synchronously)
    fileMan->Copy(pathSource,pathDestination);
    
      // Clean up 3 items 
    CleanupStack::PopAndDestroy(3);
    
      // Close file server session and note that we have not deleted
      // any files or directories created here.
    fsSession.Close();
    }
// Notify.mmp
//
// Copyright (C) Symbian Software Ltd 2000-2005.  All rights reserved.

// using relative paths for source and userinclude directories

// No explicit capabilities required to run this.

// Please note that the 2nd UID listed here has not been
// allocated from the central pool of UI's and is not 
// guaranteed to be unique.
// The value is used for demonstration purposes only.

TARGET        Notify.exe
TARGETTYPE    exe
UID           0 0x0FFFFF06
VENDORID      0x70000001

SOURCEPATH    .
SOURCE        Notify.cpp

USERINCLUDE   .
USERINCLUDE   ..\..\CommonFramework
SYSTEMINCLUDE \Epoc32\include

LIBRARY       euser.lib efsrv.lib

CAPABILITY    None
// BLD.INF
// Component description file
//
// Copyright (C) Symbian Software Ltd 2000-2005.  All rights reserved.

PRJ_MMPFILES

Notify.mmp

PRJ_EXPORTS
dataFile ..\wins\c\dataFile
dataFile ..\winscw\c\dataFile

[Top]


Description

Notify demonstrates using MFileManObserver to observe a file copy operation carried out through the file management class CFileMan. The build process puts a file, dateFile, on the C drive. The program copies this to a new file, dateFileCopy, using MFileManObserver to report the progress of the operation.

[Top]


Classes used