upnp/upnpstack/upnphttptransfer/src/httpfile.cpp
changeset 0 f5a58ecadc66
child 9 5c72fd91570d
equal deleted inserted replaced
-1:000000000000 0:f5a58ecadc66
       
     1 /** @file
       
     2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies  this distribution, and is available 
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Represents one downloadable file
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // User include files
       
    20 #include "httpfile.h"
       
    21 
       
    22 // Constants
       
    23 _LIT(KUnderScore, "_");
       
    24 const TUint KMaxSameFileNames = 1000;
       
    25 const TUint KSpaceForNumber = 5; // underscore + 4 digits
       
    26 const TUint KFinishFileCreationLoop = 1001;
       
    27 
       
    28 // ======== MEMBER FUNCTIONS ========
       
    29 
       
    30 // --------------------------------------------------------------------------
       
    31 // CHttpFile::CHttpFile()
       
    32 // --------------------------------------------------------------------------
       
    33 //
       
    34 CHttpFile::CHttpFile( TAny* aKey )
       
    35     {
       
    36     iKey = aKey;
       
    37     iPath = NULL;
       
    38     }
       
    39 
       
    40 // --------------------------------------------------------------------------
       
    41 // CHttpFile::CHttpFile()
       
    42 // --------------------------------------------------------------------------
       
    43 //
       
    44 CHttpFile::CHttpFile( TAny* aKey, const RFile& aFileHandle )
       
    45     {
       
    46     iKey = aKey;
       
    47     iPath = NULL;
       
    48     iFile.Duplicate( aFileHandle );
       
    49     }
       
    50 
       
    51 // --------------------------------------------------------------------------
       
    52 // CHttpFile::ConstructL()
       
    53 // (See comments in header file)
       
    54 // --------------------------------------------------------------------------
       
    55 //
       
    56 void CHttpFile::ConstructL( const TDesC8& aUri, const TDesC& aPath )
       
    57     {
       
    58     SetUriL( aUri );
       
    59 
       
    60     iPath = aPath.AllocL();
       
    61     
       
    62     User::LeaveIfError( iFsSession.Connect() );
       
    63     }
       
    64 
       
    65 // --------------------------------------------------------------------------
       
    66 // CHttpFile::ConstructL()
       
    67 // (See comments in header file)
       
    68 // --------------------------------------------------------------------------
       
    69 //
       
    70 void CHttpFile::ConstructL( const TDesC8& aUri )
       
    71     {
       
    72     SetUriL( aUri );
       
    73     }
       
    74 // --------------------------------------------------------------------------
       
    75 // CHttpFile::NewL()
       
    76 // (See comments in header file)
       
    77 // --------------------------------------------------------------------------
       
    78 //
       
    79 CHttpFile* CHttpFile::NewL( TAny* aKey, 
       
    80                             const TDesC8& aUri, 
       
    81                             const TDesC& aPath )
       
    82     {
       
    83     CHttpFile* self = CHttpFile::NewLC( aKey, aUri, aPath );
       
    84     CleanupStack::Pop( self );
       
    85     return self;
       
    86     }
       
    87 
       
    88 // --------------------------------------------------------------------------
       
    89 // CHttpFile::NewLC()
       
    90 // (See comments in header file)
       
    91 // --------------------------------------------------------------------------
       
    92 //
       
    93 CHttpFile* CHttpFile::NewLC( TAny* aKey, 
       
    94                              const TDesC8& aUri, 
       
    95                              const TDesC& aPath )
       
    96     {
       
    97     CHttpFile* self = new( ELeave ) CHttpFile( aKey );
       
    98     CleanupStack::PushL( self );
       
    99     self->ConstructL( aUri, aPath );
       
   100     return self;
       
   101     }
       
   102 
       
   103 // --------------------------------------------------------------------------
       
   104 // CHttpFile::NewL()
       
   105 // (See comments in header file)
       
   106 // --------------------------------------------------------------------------
       
   107 //
       
   108 CHttpFile* CHttpFile::NewL( TAny* aKey, const TDesC8& aUri,
       
   109                             const RFile& aFileHandle )
       
   110     {
       
   111     CHttpFile* self = CHttpFile::NewLC( aKey, aUri, aFileHandle );
       
   112     CleanupStack::Pop( self );
       
   113     return self;
       
   114     }
       
   115 
       
   116 // --------------------------------------------------------------------------
       
   117 // CHttpFile::NewLC()
       
   118 // (See comments in header file)
       
   119 // --------------------------------------------------------------------------
       
   120 //
       
   121 CHttpFile* CHttpFile::NewLC( TAny* aKey, const TDesC8& aUri, 
       
   122                              const RFile& aFileHandle )
       
   123     {
       
   124     CHttpFile* self = new( ELeave ) CHttpFile( aKey, aFileHandle );
       
   125     CleanupStack::PushL( self );
       
   126     self->ConstructL( aUri );
       
   127     return self;
       
   128     }
       
   129 
       
   130 // --------------------------------------------------------------------------
       
   131 // CHttpFile::~CHttpFile()
       
   132 // (See comments in header file)
       
   133 // --------------------------------------------------------------------------
       
   134 //
       
   135 CHttpFile::~CHttpFile()
       
   136     {
       
   137     iFile.Close();
       
   138     delete iUri;
       
   139     delete iPath;
       
   140     iHeaderArray.ResetAndDestroy();
       
   141 
       
   142     iFsSession.Close();
       
   143     }
       
   144 
       
   145 // --------------------------------------------------------------------------
       
   146 // CHttpFile::TrackProgress()
       
   147 // (See comments in header file)
       
   148 // --------------------------------------------------------------------------
       
   149 //
       
   150 void CHttpFile::TrackProgress( TBool aValue ) 
       
   151     {
       
   152     iTrackProgress = aValue;
       
   153     }
       
   154 
       
   155 // --------------------------------------------------------------------------
       
   156 // CHttpFile::TrackingOn()
       
   157 // (See comments in header file)
       
   158 // --------------------------------------------------------------------------
       
   159 //
       
   160 TBool CHttpFile::TrackingOn() const
       
   161     {
       
   162     return iTrackProgress;    
       
   163     }
       
   164 
       
   165 // --------------------------------------------------------------------------
       
   166 // CHttpFile::Key()
       
   167 // (See comments in header file)
       
   168 // --------------------------------------------------------------------------
       
   169 //
       
   170 TAny* CHttpFile::Key() const
       
   171     {
       
   172     return iKey;
       
   173     }
       
   174 
       
   175 // --------------------------------------------------------------------------
       
   176 // CHttpFile::Uri()
       
   177 // (See comments in header file)
       
   178 // --------------------------------------------------------------------------
       
   179 //
       
   180 const HBufC8* CHttpFile::Uri() const
       
   181     {
       
   182     return iUri;
       
   183     }
       
   184 
       
   185 // --------------------------------------------------------------------------
       
   186 // CHttpFile::SetUriL()
       
   187 // (See comments in header file)
       
   188 // --------------------------------------------------------------------------
       
   189 //
       
   190 void CHttpFile::SetUriL( const TDesC8& aUri )
       
   191     {
       
   192     HBufC8* uri = aUri.AllocL();
       
   193     delete iUri;
       
   194     iUri = uri;
       
   195     }
       
   196 
       
   197 // --------------------------------------------------------------------------
       
   198 // CHttpFile::Path()
       
   199 // (See comments in header file)
       
   200 // --------------------------------------------------------------------------
       
   201 //
       
   202 const HBufC* CHttpFile::Path() const
       
   203     {
       
   204     return iPath;
       
   205     }
       
   206 
       
   207 // --------------------------------------------------------------------------
       
   208 // CHttpFile::FileHandle()
       
   209 // (See comments in header file)
       
   210 // --------------------------------------------------------------------------
       
   211 //
       
   212 RFile CHttpFile::FileHandle() const
       
   213     {
       
   214     return iFile;
       
   215     }
       
   216 
       
   217 // --------------------------------------------------------------------------
       
   218 // CHttpFile::SetHeaderL()
       
   219 // (See comments in header file)
       
   220 // --------------------------------------------------------------------------
       
   221 //
       
   222 void CHttpFile::SetHeaderL( const TDesC8& aFieldName, 
       
   223                             const TDesC8& aFieldValue )
       
   224     {
       
   225     // if the fieldname already exists ( header already set ) -> leave
       
   226     for ( TInt i = 0; i < iHeaderArray.Count(); i++ )
       
   227         {
       
   228         if ( iHeaderArray[i]->FieldName().CompareF( aFieldName ) == 0 )
       
   229             {
       
   230             User::Leave( KErrAlreadyExists );
       
   231             }
       
   232         }
       
   233 
       
   234     iHeaderArray.Append( CHttpHeader::NewL( aFieldName, aFieldValue ) );
       
   235     }
       
   236 
       
   237 // --------------------------------------------------------------------------
       
   238 // CHttpFile::Headers()
       
   239 // (See comments in header file)
       
   240 // --------------------------------------------------------------------------
       
   241 //
       
   242 RPointerArray<CHttpHeader>& CHttpFile::Headers()
       
   243     {
       
   244     return iHeaderArray;
       
   245     }
       
   246 
       
   247 // --------------------------------------------------------------------------
       
   248 // CHttpFile::CreateFileInFileSystem()
       
   249 // (See comments in header file)
       
   250 // --------------------------------------------------------------------------
       
   251 //
       
   252 void CHttpFile::CreateFileInFileSystemL()
       
   253     {
       
   254     TInt ret = KErrNone;
       
   255 
       
   256     if ( iPath )
       
   257         {
       
   258         // Check first that the drive exists
       
   259         TInt driveNum;
       
   260         TParse p;
       
   261 		p.Set( *iPath, NULL, NULL ); 
       
   262 		TPtrC pointer = p.Drive(); 
       
   263 		TLex lineParser( pointer.Ptr() ); 
       
   264 		TChar ch = lineParser.Get(); 
       
   265 		
       
   266 		User::LeaveIfError( iFsSession.CharToDrive( ch, driveNum) );
       
   267 
       
   268 		TVolumeInfo volumeInfo;
       
   269         User::LeaveIfError( iFsSession.Volume( volumeInfo, driveNum ) );
       
   270 
       
   271         // Create the file in the file system
       
   272         ret = iFile.Create( iFsSession, *iPath, EFileWrite );
       
   273 
       
   274         // Directory not available => create one
       
   275         if( KErrPathNotFound == ret )
       
   276             {
       
   277             // Make directory
       
   278             // Creates the directory and ignores the filename
       
   279             iFsSession.MkDirAll( *iPath );
       
   280             ret = iFile.Create( iFsSession, *iPath, EFileWrite );
       
   281             }
       
   282 
       
   283         // File already exists
       
   284         // File has to be renamed to 
       
   285         // <oldfilename>_<number>.<old_file_extension>
       
   286         // number will increase if same file is loaded many times
       
   287         else if( KErrAlreadyExists  == ret )
       
   288             {
       
   289             TParse parseFilename;
       
   290 
       
   291             parseFilename.Set( *iPath, NULL, NULL );
       
   292 
       
   293             HBufC* name = NULL;
       
   294 
       
   295             for ( TInt count = 1; count < KMaxSameFileNames; count++ )
       
   296                 {
       
   297                 // KSpaceForNumber is meant for underscore and number
       
   298                 name = HBufC::NewLC( iPath->Length() + 
       
   299                                      KSpaceForNumber );
       
   300 
       
   301                 TBuf<KSpaceForNumber> value;
       
   302                 value.Num( count );
       
   303 
       
   304                 name->Des().Append( parseFilename.DriveAndPath() );
       
   305                 name->Des().Append( parseFilename.Name() );    
       
   306                 name->Des().Append( KUnderScore() );
       
   307                 name->Des().Append( value );
       
   308                 name->Des().Append( parseFilename.Ext() );
       
   309 
       
   310                 ret = iFile.Create( iFsSession, *name, EFileWrite );
       
   311 
       
   312                 // If the file already exists make a new loop
       
   313                 if ( ret == KErrAlreadyExists )
       
   314                     {
       
   315                     CleanupStack::PopAndDestroy( name );
       
   316                     name = NULL;
       
   317                     }
       
   318                 // if file creation succeeded
       
   319                 else if ( ret == KErrNone )
       
   320                     {
       
   321                     delete iPath;
       
   322                     iPath = name;
       
   323                     CleanupStack::Pop( name );
       
   324                     
       
   325                     count = KFinishFileCreationLoop;
       
   326                     }
       
   327                 else
       
   328                     {
       
   329                     CleanupStack::PopAndDestroy( name );
       
   330                     
       
   331                     count = KFinishFileCreationLoop;
       
   332                     }
       
   333                 }
       
   334             }
       
   335         }
       
   336 
       
   337     if( ret != KErrNone ) 
       
   338         {
       
   339         User::Leave( ret );
       
   340         }
       
   341 
       
   342     }
       
   343 
       
   344 // --------------------------------------------------------------------------
       
   345 // CHttpFile::DeleteFileFromFileSystemL()
       
   346 // (See comments in header file)
       
   347 // --------------------------------------------------------------------------
       
   348 //
       
   349 void CHttpFile::DeleteFileFromFileSystemL()
       
   350     {
       
   351     // Close filehandle before delete
       
   352     iFile.Close();
       
   353 
       
   354     // Delete the file
       
   355     if ( iPath )
       
   356         {
       
   357         CFileMan* fileMan = CFileMan::NewL( iFsSession );
       
   358         CleanupStack::PushL( fileMan );
       
   359         User::LeaveIfError( fileMan->Delete( iPath->Des() ) );
       
   360         CleanupStack::PopAndDestroy( fileMan );
       
   361         }
       
   362     }
       
   363 
       
   364 // --------------------------------------------------------------------------
       
   365 // CHttpFile::CloseFile()
       
   366 // (See comments in header file)
       
   367 // --------------------------------------------------------------------------
       
   368 //
       
   369 void CHttpFile::CloseFile()
       
   370     {
       
   371     iFile.Close();
       
   372     }
       
   373 
       
   374 // end of file