chxavfilestore.h
上传用户:dangjiwu
上传日期:2013-07-19
资源大小:42019k
文件大小:11k
- /************************************************************************
- * chxavfilestore.h
- * ----------------
- *
- * Synopsis:
- * simple file system abstraction for manipulating files and folders;
- * maintains 'root' (relative to true filesystem) 'current' folder
- * state
- *
- * root path is stored in full form, e.g.: c:realmedia
- * folder is stored in relative form, fully specified, e.g.:
- * = root
- * music = subfolder music
- *
- * the term 'item' (as used in this class) refers to a file or folder
- * that is in the current entry list; the entry list contains a list
- * of files and folders within the current folder
- *
- * Target:
- * Symbian OS
- *
- *
- * (c) 1995-2003 RealNetworks, Inc. Patents pending. All rights reserved.
- *
- ************************************************************************/
- #ifndef _chxavfilestore_h_
- #define _chxavfilestore_h_
- // Symbian includes...
- #include <apgcli.h>
- #include <apmrec.h>
- // Helix includes...
- #include "unkimp.h"
- #include "ihxpckts.h"
- #include "hxstring.h"
- #include "hxurl.h"
- #include "hxwintyp.h"
- #include "hxcom.h"
- #include "hxcomm.h"
- #include "hxmon.h"
- #include "hxfiles.h"
- #include "hxcore.h"
- #include "hxwin.h"
- // Includes from this project...
- #include "chxavcompositecommand.h"
- #include "chxavfilesystemwatcher.h"
- #include "chxavfileutil.h"
- #include "chxavvector.h"
- #include "chxavactivecompletiondispatch.h"
- #include "chxsmartptr.h"
- #include "chxavcleanupstack.h"
- #include "chxavmisc.h"
- #include "chxbody.h"
- //#define ASYNC_COPYMOVE //not fully implemented
- // class FileStoreOperationObserver
- class FileStoreOperationObserver
- {
- public:
- enum OperationType
- {
- otCopy,
- otMove,
- otWhoCares
- };
- //
- // these are called on separate thread if async fileman operations are used
- //
- virtual void OnFileOpStart(OperationType type, const TDesC& source, const TDesC& target) = 0;
- virtual bool OnFileOpTick(OperationType type, const TDesC& source, const TDesC& target, TInt64 cbCopied) = 0;
- virtual void OnFileOpEnd(OperationType type, TInt err, const TDesC& source, const TDesC& target) = 0;
- OperationType TranslateFileManAction(CFileMan::TAction action);
- };
- // forward decl
- class CFileMan;
- // class CHXAvFileStore
- class CHXAvFileStore
- : public CHXBody
- , public MFileManObserver
- , public FileStoreOperationObserver
- {
- public:
- typedef CHXAvVector<CHXAvFile::FileInfo> Entries;
- public:
- // ctor and dtor
- CHXAvFileStore();
- virtual ~CHXAvFileStore();
- void ConstructL(const TDesC& root, const TDesC& path = KPathSep,
- bool bAutoCreateRoot = false);
- void SetObserver(FileStoreOperationObserver* pObserver);
- // handler for filesystem events
- CHXAvCompositeCommand& GetEventHandler();
- // path set and get
- void SetRootL(const TDesC& path, bool bAutoCreateRoot = false);
- void SetCurrentPathL(const TDesC& path);
- const TDesC& GetRoot() const;
- const TDesC& GetCurrentPath() const;
- const TDesC& GetFullPath() const;
- // current folder actions
- void SwitchToParentFolderL();
- void SwitchToChildFolderL(const TDesC& folder);
- // item actions
- TInt CreateChildFolderL(const TDesC& folder, bool bAllowOverwrite);
- TInt DeleteItemL(TInt idxItem);
- TInt MoveItemL(TInt idxItem,
- const TDesC& targetPath,
- const TDesC& targetName,
- bool bAllowOverwrite, bool bRename = false);
- TInt MoveItemL(TInt idxItem, const TDesC& newName, bool bAllowOverwrite, bool bRename = false);
- // file actions; paths may be relative (to store) or absolute
- FILE* OpenFileL(const TDesC& path,
- const TDesC& fileName,
- const char* pMode);
- TInt CopyFileL(const TDesC& pathDest,
- const TDesC& fileNameDest,
- const TDesC& fullPathSource,
- bool bAllowOverwrite);
- TInt CopyFileAlternateL(const TDesC& pathDest,
- const TDesC& fileNameDest,
- const TDesC& fullPathSource,
- bool bAllowOverwrite);
- TInt DeleteFileL(const TDesC& pathDest, const TDesC& name, CHXAvFile::NameType type = CHXAvFile::ntUnspecified);
- TInt DeleteFileL(const TDesC& fullPath, CHXAvFile::NameType type = CHXAvFile::ntUnspecified);
- #if defined(ASYNC_COPYMOVE)
- // cancel async file operation (move, copy)
- void CancelAsyncFileOp();
- #endif
- // misc
- bool IsAtRoot() const;
- bool IsUpToDate() const;
- bool IsFolderEmpty() const;
- TInt GetCurrentFolderFileCount() const;
-
- bool PathExistsL(const TDesC& targetPath, const TDesC& name, CHXAvFile::NameType type = CHXAvFile::ntUnspecified);
- bool NameExistsInTargetPathL(const TDesC& targetPath, TInt idxItem, CHXAvFile::NameType type);
- bool NameExistsInTargetPathL(const TDesC& targetPath, TInt idxItem);
- bool NameExistsInTargetPathL(const TDesC& targetPath, const TDesC& name);
- bool IsSafeFileNameLengthL(const TDesC& path, TInt idxItem);
- bool IsSafeFileNameLengthL(const TDesC& path, const TDesC& name, CHXAvFile::NameType type = CHXAvFile::ntFile);
- bool IsSamePathL(const TDesC& pathDest, TInt idxItem);
- bool IsImmediateChild(const TDesC& fullPathChild);
- bool IsParent(const TDesC& fullPathParent);
- // entries
- void SetNeedsRefreshNextUpdate();
- //const Entries& RemoveEntries(const CHXAvVector<TInt>& removeIndexes);
- const Entries& UpdateEntriesL();
- const Entries& GetEntries() const;
- Entries CopyEntriesL() const;
-
- // callback for filesystem watcher
- void OnFileSystemEvent();
- TFileName* AllocFullPathL(TInt idxItem);
- TFileName* AllocFullPathL(const TDesC& path, const TDesC& name, CHXAvFile::NameType type = CHXAvFile::ntFile);
- public:
- // for use with SUSPEND_REFRESH macro
- class RefreshSuspenderL
- {
- public:
- RefreshSuspenderL(CHXAvFileStore* pStore)
- : m_pStore(pStore)
- {
- CleanupStack::PushL(TCleanupItem(CleanupSuspendRefresh, pStore));
- m_pStore->SuspendRefresh(true);
- }
- ~RefreshSuspenderL()
- {
- m_pStore->SuspendRefresh(false);
- CleanupStack::Pop();
- }
- private:
- CHXAvFileStore* m_pStore;
- }; //RefreshSuspenderL
- protected:
- // for RefreshSuspenderL
- friend class RefreshSuspenderL;
- static void CleanupSuspendRefresh(TAny* p);
- void SuspendRefresh(bool bSuspend);
- private:
- // MFileManObserver
- TControl NotifyFileManStarted();
- TControl NotifyFileManOperation();
- TControl NotifyFileManEnded();
- // FileStoreOperationObserver
- void OnFileOpStart(FileStoreOperationObserver::OperationType type, const TDesC& source, const TDesC& target);
- bool OnFileOpTick(FileStoreOperationObserver::OperationType type, const TDesC& source, const TDesC& target, TInt64 cbCopied);
- void OnFileOpEnd(FileStoreOperationObserver::OperationType type, TInt err, const TDesC& source, const TDesC& target);
- #if defined(ASYNC_COPYMOVE)
- // active object for handling async CFileMan ops
- void OnFileOpAsyncComplete(TInt status);
- void OnFileOpAsyncCancel(TInt status);
- #endif
- // disallow copy
- CHXAvFileStore& operator=(const CHXAvFileStore& other);
- CHXAvFileStore(const CHXAvFileStore& other);
- //
- // implementation helpers
- //
- const TDesC& UpdateFullPathL();
- void RefreshEntryInfoL();
- void RefreshEntryInfoHelper(const CDir* pFiles, bool bIncludeFolders);
-
- void EnsureEntryInfoIsUpToDate();
- TFileName* AllocFullTargetPathL(const TDesC& targetPath, TInt idxItem, const TDesC& targetName = KNullDesC);
- private:
-
- CFileMan* m_pFileMan;
- CHXAvVector<CHXAvFile::FileInfo> m_entryInfo;
- TParse m_root;
- TFileName m_curPath;
- TFileName* m_pFullPath;
- CHXAvFileSystemWatcher m_fsWatcher;
- bool m_bNeedRefresh;
- CHXAvCompositeCommand m_eventHandler;
- RApaLsSession m_apaLsSess;
- TInt64 m_cbFileOp;
- #if defined(ASYNC_COPYMOVE)
- CHXAvActiveCmplPtr m_pFileOpCompletion;
- #endif
- FileStoreOperationObserver* m_pObserver;
- TInt m_suspendRefreshSem;
- bool m_bHaveOutstandingFileSystemEvent;
- };
- // see SuspendRefresh()
- #define SUSPEND_REFRESH(p) CHXAvFileStore::RefreshSuspenderL makevar__(__LINE__) (p.raw_ptr());
- typedef CHXSmartPtr<CHXAvFileStore> CHXAvFileStorePtr;
- ////////////////////////////////////////////////////
- // force refresh next time entries are updated; theoretically
- // you should never need this because we watch the filesystem
- // for changes
- inline
- void CHXAvFileStore::SetNeedsRefreshNextUpdate()
- {
- m_bNeedRefresh = true;
- }
- ////////////////////////////////////////////////////
- // return event handler object so handlers may be
- // added/removed
- inline
- CHXAvCompositeCommand& CHXAvFileStore::GetEventHandler()
- {
- return m_eventHandler;
- }
- ////////////////////////////////////////////////////
- // true if showing contents for root level folder
- inline
- bool CHXAvFileStore::IsAtRoot() const
- {
- return 0 == m_curPath.Compare(KPathSep);
- }
- ////////////////////////////////////////////////////////////
- // return true if current entries are out of date (or we can't be sure)
- inline
- bool CHXAvFileStore::IsUpToDate() const
- {
- return !m_bNeedRefresh;
- }
- ////////////////////////////////////////////////////////////
- // return true if the current folder is empty
- inline
- bool CHXAvFileStore::IsFolderEmpty() const
- {
- return m_entryInfo.Nelements() == 0;
- }
- ////////////////////////////////////////////////////////////
- // return full path to current folder
- inline
- const TDesC& CHXAvFileStore::GetFullPath() const
- {
- return *m_pFullPath;
- }
- ////////////////////////////////////////////
- // return absolute path to root
- //
- // e.g.:
- //
- // 'c:realnetworksmedia'
- //
- inline
- const TDesC& CHXAvFileStore::GetRoot() const
- {
- return m_root.FullName();
- }
- ////////////////////////////////////////////
- // relative to the root; should start with ''
- //
- // e.g.:
- //
- // ''
- // 'foo'
- //
- inline
- const TDesC& CHXAvFileStore::GetCurrentPath() const
- {
- return m_curPath;
- }
- ///////////////////////////////////////////
- //
- inline
- void CHXAvFileStore::EnsureEntryInfoIsUpToDate()
- {
- if(m_bNeedRefresh)
- {
- RefreshEntryInfoL();
- HX_ASSERT(!m_bNeedRefresh); // impossible
- }
- }
- ////////////////////////////////////////////
- // refresh entries to match current state of
- // the current folder on disk
- //
- inline
- const CHXAvFileStore::Entries& CHXAvFileStore::UpdateEntriesL()
- {
- EnsureEntryInfoIsUpToDate();
- return m_entryInfo;
- }
- ////////////////////////////////////////////
- // get entries without refresh
- //
- // list is up-to-date (valid) until one of following occurs:
- //
- // a) external file system event
- // b) you call a method that changes the state of the folder contents
- // c) you call a method that changes the current root and/or folder
- //
- inline
- const CHXAvFileStore::Entries& CHXAvFileStore::GetEntries() const
- {
- return m_entryInfo;
- }
- inline
- CHXAvFileStore::Entries CHXAvFileStore::CopyEntriesL() const
- {
- Entries entries;
- entries.Copy(m_entryInfo);
- return entries;
- }
- inline
- void CHXAvFileStore::SetObserver(FileStoreOperationObserver* pObserver)
- {
- m_pObserver = pObserver;
- }
- #endif // _chxavfilestore_h_