⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filelistengine.cpp

📁 一个简单的资源管理器
💻 CPP
字号:
/*
 * Copyright (c) 2009 Nokia Corporation.
 */

// INCLUDE FILES


#include "FileListEngine.h"
#include "Filelist.hrh"     // enumerations
#include <documenthandler.h>// for launching
#include "FileList.pan"
#include <bautils.h>
#include <PathInfo.h>

// ================= Constants =======================
// Number, name and file size
_LIT(KStringSize,"%d\t%S\t%d bytes");
// Number, name and date modified
_LIT(KStringDate,"%d\t%S\t%S");

#define KDirSounds    PathInfo::DigitalSoundsPath()
#define KDirPictures  PathInfo::ImagesPath()
#define KDirVideos    PathInfo::VideosPath()

_LIT(KSlash, "\\");

const TInt KDateLength( 30 );

// ================= MEMBER FUNCTIONS =======================

// ---------------------------------------------------------
// CFileListEngine::ConstructL(const TRect& aRect)
// Symbian two phased constructor
// ---------------------------------------------------------
//
void CFileListEngine::ConstructL(CEikProcess* aProcess /*= NULL*/)
    {
    aProcess = NULL;
    iDocHandler = CDocumentHandler::NewL();
    }

  // Destructor
CFileListEngine::~CFileListEngine()
    {
    delete iDocHandler;
    iFileList.Close();
    iDirList.Close();
    }

// ---------------------------------------------------------
// CFileListEngine::StartFileList()
// This Method gets the specific directory by using PathInfo.
// ---------------------------------------------------------
//
TInt CFileListEngine::StartFileList()
  {
  iFileList.Reset();
  iDirList.Reset();

  // Connect to fileserver
  TInt error = iFsSession.Connect();
  if(error!=KErrNone)
    {
    return error;
    }

  TFileName path1 =  PathInfo::PhoneMemoryRootPath();
  TFileName path2 = PathInfo::MemoryCardRootPath();
  switch (iDirectory)
    {
    case EFileListPictures:
      path1.Append(KDirPictures);
      path2.Append(KDirPictures);
      break;
    case EFileListVideos:
      path1.Append(KDirVideos);
      path2.Append(KDirVideos);
      break;
    case EFileListSounds:
    default:
      path1.Append(KDirSounds);
      path2.Append(KDirSounds);
      break;
    }

    TRAPD( err, GetResourceFilesL( path1 ));
    TRAPD( err2, GetResourceFilesL( path2 ));
    return err;
  }

// ---------------------------------------------------------
// CFileListEngine::GetResourceFilesL ( TFileName &aBasePath )
// The method get all files including files into subfolders.
// ---------------------------------------------------------
//
void CFileListEngine::GetResourceFilesL ( TFileName &aBasePath )
  {
    CDir* dirs = 0;
    CDir* files = 0;

    if(! BaflUtils::PathExists(iFsSession, aBasePath))
      {
      return;
      }

  // Get dir. KEntryAttNormal means that no hidden files or directories are included
    User::LeaveIfError(iFsSession.GetDir(aBasePath,KEntryAttNormal,ESortByName,files, dirs));

  CleanupStack::PushL(dirs);
  CleanupStack::PushL(files);

  //get files in base path
  for( TInt i=0; i<files->Count(); i++ )
    {
    iFileList.Append( (*files)[i] );
    iDirList.Append( aBasePath );
    }

  //get subfolders
  for( TInt i=0; i<dirs->Count(); i++ )
    {
    // Exclude directories beginning with '_' as they can
    // contain system-generated thumbnails etc.
    if( (*dirs)[i].iName[0] != '_')
      {
      TFileName nextDir = aBasePath;
      nextDir.Append( (*dirs)[i].iName );
      nextDir.Append( KSlash );
      GetResourceFilesL( nextDir );
      }
    }
    // Number 2 cannot be considered a "magic number" in this case,
    // and PopAndDestroy does get a parameter. Thus, the CSIs.
    CleanupStack::PopAndDestroy( 2, dirs );  // CSI: 47,12 #
  }

// ---------------------------------------------------------
// CFileListEngine::GetFileListItems(CDesCArray* aItems)
// This Method constructs the listbox items with directory
// information
// ---------------------------------------------------------
//
void CFileListEngine::GetFileListItemsL(CDesCArray* aItems)
    {

    for (TInt i=0;i<iFileList.Count();i++)
        {
        TFileName filename(KNullDesC);
        if(iSizeDate==EFileListSize)
            {
            // Show file size
            filename.Format(KStringSize,i+1,&iFileList[i].iName,iFileList[i].iSize);
            }
        else
            {
            // Fix the date and time string of last modification
            TBuf<KDateLength> dateString;
            _LIT(KDateString,"%D%M%Y%/0%1%/1%2%/2%3%/3 %-B%:0%J%:1%T%:2%S%:3%+B");
            iFileList[i].iModified.FormatL(dateString,KDateString);

            // Show date modified
            filename.Format(KStringDate,i+1,&iFileList[i].iName,&dateString);
            }
        aItems->AppendL(filename);
        }
    }

// ---------------------------------------------------------
// CFileListEngine::SetDirectory(TInt aDirectory)
// This Method sets which directory to list.
// ---------------------------------------------------------
//
void CFileListEngine::SetDirectory(TInt aDirectory)
    {
    if (aDirectory!=EFileListDirNoChange)
        iDirectory=aDirectory;
    }

// ---------------------------------------------------------
// CFileListEngine::LaunchCurrent(TInt aPosition)
// This Method launches selected item with DocumentHandler.
// DocumentHandler will launch correct application depending
// on the file type.
// Note that all the extensions do not work on emulator.
// ---------------------------------------------------------
//
void CFileListEngine::LaunchCurrentL(TInt aPosition)
    {
    TFileName descr = iDirList[aPosition];

    if(aPosition <= iFileList.Count())
      {
      // Add filename to be launched
      descr.Append(iFileList[aPosition].iName);
      }
    else
      {
      Panic(EFileListIvalidIndex);
      }

    // Create nullType. This makes the DocumentHandler to figure out the posfix for us.
    TDataType nullType;
    // Launch the appropriate application for this file
    iDocHandler->OpenFileEmbeddedL(descr,nullType);
    iDocHandler->SetExitObserver(this);
    iDocEmbedded = ETrue;
    };

// ---------------------------------------------------------
// CFileListEngine::RemoveItems(CDesCArray* aItems)
// This Method removes all listbox items when a new list
// needs to be shown.
// ---------------------------------------------------------
//
TBool CFileListEngine::RemoveItems(CDesCArray* aItems)
    {
    if (iFileList.Count())
        {
        aItems->Delete(0,(iFileList.Count()));
        return ETrue;
        }
    return EFalse;
    };

// ---------------------------------------------------------
// CFileListEngine::IsDirListEmpty()
// ---------------------------------------------------------
//
TBool CFileListEngine::IsDirListEmpty()
    {

    if(iFileList.Count())
      {
      return EFalse;

      }
    return ETrue;
    };

// ---------------------------------------------------------
// CFileListEngine::SetSizeDate(TInt aSizeDate)
// This Method sets whether modification date or file size
// is shown. There is also option for toggling the status.
// ---------------------------------------------------------
//
void CFileListEngine::SetSizeDate(TInt aSizeDate)
    {
    if (aSizeDate==EFileListToggle)
        if (iSizeDate==EFileListSize)
            iSizeDate=EFileListDate;
        else
            iSizeDate=EFileListSize;
    else
        if (aSizeDate!=EFileListSizeDateNoChange)
            iSizeDate=aSizeDate;
    };

// ---------------------------------------------------------
// CFileListEngine::EndFileList()
// This Method ends the FileList session
// ---------------------------------------------------------
//
void CFileListEngine::EndFileList()
    {
    // Close the file server session
    iFsSession.Close();
    };

void CFileListEngine::HandleServerAppExit(TInt aReason)
{
  iDocEmbedded = EFalse;
  MAknServerAppExitObserver::HandleServerAppExit(aReason);
}

//EOF


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -