messagingfw/msgsrvnstore/server/src/CMsvAttachment.cpp
changeset 0 8e480a14352b
equal deleted inserted replaced
-1:000000000000 0:8e480a14352b
       
     1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include <cmsvattachment.h>
       
    17 #include "CMsvAttributeManager.h"
       
    18 
       
    19 const TInt KMsvAttachmentVersion = 2; // version number used for streaming
       
    20 
       
    21 /**
       
    22 Standard 2-phase construction, creates the attachment object.
       
    23 
       
    24 The object should then be initialised with information about the attachment.
       
    25 
       
    26 @param aType The attachment type.
       
    27 @return Pointer to the created attachment object.
       
    28 */
       
    29 EXPORT_C CMsvAttachment* CMsvAttachment::NewL(TMsvAttachmentType aType)
       
    30 	{
       
    31 	CMsvAttachment* self = new(ELeave) CMsvAttachment(aType);
       
    32 	CleanupStack::PushL(self);
       
    33 	self->ConstructL();
       
    34 	CleanupStack::Pop(self);
       
    35 	return self;
       
    36 	}
       
    37 
       
    38 /**
       
    39 Standard 2-phase construction, creates the attachment object. 
       
    40 
       
    41 The object should then be initialised with information about the attachment. This allows
       
    42 the caller to pass in commonly used attributes to initialise them on construction to
       
    43 save the caller from having to call the relavant Set... methods. The attributes that can
       
    44 be initialised are Mime-Type, attachment name and size. Callers should pass in NULL for
       
    45 any attributes that are not required to be set.
       
    46 
       
    47 @param aType The attachment type.
       
    48 @param aSize The size of the attachment. For file attachments this should be the file
       
    49 	   		 size and for message entry attachments this should be the message size.
       
    50 @param aMimeType The Mime-Type of the attachment.
       
    51 @param aAttachmentName The attachment name to identify the attachment.
       
    52 @return Pointer to the created attachment object.
       
    53 */
       
    54 EXPORT_C CMsvAttachment* CMsvAttachment::NewL(TMsvAttachmentType aType, TInt aSize, HBufC8* aMimeType, HBufC* aAttachmentName)
       
    55 	{
       
    56 	CMsvAttachment* self = new(ELeave) CMsvAttachment(aType, aSize, aMimeType, aAttachmentName);
       
    57 	CleanupStack::PushL(self);
       
    58 	self->ConstructL();
       
    59 	CleanupStack::Pop(self);
       
    60 	return self;
       
    61 	}
       
    62 
       
    63 EXPORT_C CMsvAttachment* CMsvAttachment::NewL(const CMsvAttachment& aAttachment)
       
    64 	{
       
    65 	CMsvAttachment* self = new(ELeave) CMsvAttachment(aAttachment.iType);
       
    66 	CleanupStack::PushL(self);
       
    67 	self->ConstructL(aAttachment);
       
    68 	CleanupStack::Pop(self);
       
    69 	return self;
       
    70 	}
       
    71 	
       
    72 CMsvAttachment::CMsvAttachment(TMsvAttachmentType aType)
       
    73 	: iType(aType)
       
    74 	{
       
    75 	}	
       
    76 
       
    77 CMsvAttachment::CMsvAttachment(TMsvAttachmentType aType, TInt aSize, HBufC8* aMimeType, HBufC* aAttachmentName)
       
    78 	: iType(aType), iSize(aSize), iMimeType(aMimeType), iAttachmentName(aAttachmentName)
       
    79 	{
       
    80 	}
       
    81 
       
    82 void CMsvAttachment::ConstructL()
       
    83 	{
       
    84 	iAttributeManager = CMsvAttributeManager::NewL(*this);
       
    85 	}
       
    86 
       
    87 void CMsvAttachment::ConstructL(const CMsvAttachment& aAttachment)
       
    88 	{
       
    89 	ConstructL();
       
    90 	
       
    91 	// Creates a deep copy of the attachment object passed in
       
    92 	iId = aAttachment.iId;
       
    93 	iSize = aAttachment.iSize;
       
    94 	if( aAttachment.iMimeType != NULL )
       
    95 		iMimeType = aAttachment.iMimeType->AllocL();
       
    96 	if( aAttachment.iFilePath != NULL )
       
    97 		iFilePath = aAttachment.iFilePath->AllocL();
       
    98 	iEntryId = aAttachment.iEntryId;
       
    99 	if( aAttachment.iAttachmentName != NULL )
       
   100 		iAttachmentName = aAttachment.iAttachmentName->AllocL();
       
   101 	iComplete = aAttachment.iComplete;
       
   102 	
       
   103 	// Create a copy of the attribute manager
       
   104 	iAttributeManager->CloneL(*aAttachment.iAttributeManager);
       
   105 	}
       
   106 
       
   107 /**
       
   108 Standard destructor.
       
   109 */
       
   110 EXPORT_C CMsvAttachment::~CMsvAttachment()
       
   111 	{
       
   112 	delete iAttributeManager;
       
   113 	delete iMimeType;
       
   114 	delete iFilePath;
       
   115 	delete iAttachmentName;
       
   116 	}
       
   117 
       
   118 /**
       
   119 Returns the attachment Id.
       
   120 
       
   121 The attachment Id is unique on a per message entry basis.
       
   122 
       
   123 @return The attachment Id.
       
   124 */
       
   125 EXPORT_C TMsvAttachmentId CMsvAttachment::Id() const
       
   126 	{
       
   127 	return iId;
       
   128 	}
       
   129 	
       
   130 /**
       
   131 Sets the attachment Id.
       
   132 
       
   133 The attachment Id is automatically set when passed to the Attachment Manager. This
       
   134 method allows the Id to be over-ridden or set by different implementations of
       
   135 attachment managers.
       
   136 @param aId The attachment Id to set.
       
   137 */
       
   138 EXPORT_C void CMsvAttachment::SetId(TMsvAttachmentId aId)
       
   139 	{
       
   140 	iId = aId;
       
   141 	}
       
   142 
       
   143 /**
       
   144 Returns the attachment type.
       
   145 
       
   146 The attachment types can be a file, a linked file or a message entry attachment.
       
   147 
       
   148 @return The attachment type.
       
   149 @see TMsvAttachmentType
       
   150 */
       
   151 EXPORT_C CMsvAttachment::TMsvAttachmentType CMsvAttachment::Type() const
       
   152 	{
       
   153 	return iType;
       
   154 	}
       
   155 
       
   156 /**
       
   157 Returns the message entry Id for message entry attachment. This is only valid if the attachment
       
   158 type is EMessageEntry.
       
   159 @return The message entry Id for the entry attachment.
       
   160 */
       
   161 EXPORT_C TMsvId CMsvAttachment::EntryAttachmentId() const
       
   162 	{
       
   163 	__ASSERT_DEBUG(iType==EMsvMessageEntry, User::Invariant());
       
   164 	
       
   165 	return iEntryId;
       
   166 	}
       
   167 	
       
   168 void CMsvAttachment::SetEntryAttachmentId(TMsvId aEntryId)
       
   169 	{
       
   170 	iEntryId = aEntryId;
       
   171 	}
       
   172 
       
   173 /**
       
   174 Returns the attachment name.
       
   175 
       
   176 The attachment name is a readable text attribute to identify the attachment.
       
   177 
       
   178 @return Descriptor containing attachment name. Zero-length descriptor if attachment 
       
   179 		name is not set. 
       
   180 */	
       
   181 EXPORT_C const TDesC& CMsvAttachment::AttachmentName() const
       
   182 	{
       
   183 	if( iAttachmentName==NULL )
       
   184 		return KNullDesC();
       
   185 	
       
   186 	return *iAttachmentName;
       
   187 	}
       
   188 	
       
   189 /*
       
   190 Sets the attachment name.
       
   191 
       
   192 The attachment name is a readable text attribute to identify the attachment.
       
   193 
       
   194 For example, this might be set as the filename for file based attachemnts.
       
   195 @param aAttachmentName Descriptor containing the attachment name.
       
   196 */
       
   197 EXPORT_C void CMsvAttachment::SetAttachmentNameL(const TDesC& aAttachmentName)
       
   198 	{
       
   199 	delete iAttachmentName;
       
   200 	iAttachmentName = NULL;
       
   201 	iAttachmentName = aAttachmentName.AllocL();
       
   202 	}
       
   203 
       
   204 /**
       
   205 Returns the full path specification for file attachments.
       
   206 
       
   207 This is only valid for file attachments where the attachment type is EMsvFile
       
   208 or EMsvLinkedFile.
       
   209 
       
   210 @return Descriptor containing full file path of the file attachment.
       
   211 */	
       
   212 EXPORT_C const TDesC& CMsvAttachment::FilePath() const
       
   213 	{
       
   214 	__ASSERT_ALWAYS(iType!=EMsvMessageEntry, User::Invariant());
       
   215 	
       
   216 	if (iFilePath!=NULL)
       
   217 		return *iFilePath;
       
   218 	else
       
   219 		return KNullDesC;
       
   220 	}
       
   221 	
       
   222 void CMsvAttachment::SetFilePathL(const TDesC& aFilePath)
       
   223 	{
       
   224 	__ASSERT_DEBUG(iType!=EMsvMessageEntry, User::Invariant());
       
   225 	
       
   226 	HBufC* newFilePath = aFilePath.AllocL();
       
   227 	delete iFilePath;
       
   228 	iFilePath = NULL;
       
   229 	iFilePath = newFilePath;
       
   230 	iNeedsPath = EFalse;
       
   231 	}
       
   232 	
       
   233 /**
       
   234 Size of the attachment.
       
   235 
       
   236 Return the size of the attachment in bytes.
       
   237 
       
   238 @return The size of the attachment.
       
   239 */
       
   240 EXPORT_C TInt CMsvAttachment::Size() const
       
   241 	{
       
   242 	return iSize;
       
   243 	}
       
   244 
       
   245 /**
       
   246 Sets the attachment size.
       
   247 
       
   248 When initialising or updating the attachment, this method should be used to set
       
   249 the size in bytes.
       
   250 
       
   251 @param aSize The size of the attachment in bytes.
       
   252 */
       
   253 EXPORT_C void CMsvAttachment::SetSize(TInt aSize)
       
   254 	{
       
   255 	iSize = aSize;
       
   256 	}
       
   257 
       
   258 /**
       
   259 The mime-type of the attachment.
       
   260 
       
   261 @return Descriptor containing mime-type. Zero-length descriptor if mime-type is not set.
       
   262 */
       
   263 EXPORT_C const TDesC8& CMsvAttachment::MimeType() const
       
   264 	{	
       
   265 	if(iMimeType==NULL)
       
   266 		return KNullDesC8();
       
   267 	
       
   268 	return *iMimeType;
       
   269 	}
       
   270 
       
   271 /**
       
   272 Sets the mime-type of the attachment.
       
   273 
       
   274 @param aMimeType Descriptor containing the mime-type.	
       
   275 */
       
   276 EXPORT_C void CMsvAttachment::SetMimeTypeL(const TDesC8& aMimeType)
       
   277 	{
       
   278 	delete iMimeType;
       
   279 	iMimeType = NULL;
       
   280 	iMimeType = aMimeType.AllocL();
       
   281 	}
       
   282 
       
   283 void CMsvAttachment::ExternalizeL(RWriteStream& aStream) const
       
   284 	{
       
   285 	// Stream out attachment....
       
   286 	
       
   287 	// 1. Start with version - TInt
       
   288 	aStream.WriteInt32L(KMsvAttachmentVersion);
       
   289 	
       
   290 	// 2. attachment id - TUint
       
   291 	aStream.WriteUint32L(iId);
       
   292 	
       
   293 	// 3. attachment type - TInt (enum)
       
   294 	aStream.WriteInt32L(iType);
       
   295 	
       
   296 	// 4. attachment size - TInt
       
   297 	aStream.WriteInt32L(iSize);
       
   298 	
       
   299 	// 5. mime-type - HBufC8
       
   300 	aStream << MimeType();
       
   301 			
       
   302 	// 6. message entry id - TInt (TMsvId)
       
   303 	aStream.WriteInt32L(iEntryId);
       
   304 	
       
   305 	// 7. file path - HBufC (if not message entry)
       
   306 	if(	iType != EMsvMessageEntry )
       
   307 		{
       
   308 		// Only stream out the filename for file attachments
       
   309 		if( iType == EMsvFile )
       
   310 			{
       
   311 			TParsePtrC filePath(FilePath());
       
   312 			aStream << filePath.NameAndExt();
       
   313 			}
       
   314 		else
       
   315 			{
       
   316 			// must be linked file, stream whole path
       
   317 			aStream << FilePath();
       
   318 			}
       
   319 		}
       
   320 	
       
   321 	// 8. attachment name - HBufC
       
   322 	aStream << AttachmentName();
       
   323 	
       
   324 	// 9. complete flag - TInt (TBool)
       
   325 	aStream.WriteInt32L(iComplete);
       
   326 	
       
   327 	// 10. the attributes (CMsvAttributeManager)
       
   328 	aStream << *iAttributeManager;
       
   329 	}
       
   330 
       
   331 void CMsvAttachment::InternalizeL(RReadStream& aStream)
       
   332 	{
       
   333 	// Stream in attachment info and populate data members...
       
   334 	// 1. Start with version - TInt
       
   335 	TInt version = aStream.ReadUint32L();
       
   336 	if( version>KMsvAttachmentVersion )
       
   337 		{
       
   338 		User::Leave(KErrNotSupported);
       
   339 		}
       
   340 	else
       
   341 		{
       
   342 		// 2. attachment id - TUint
       
   343 		iId = aStream.ReadUint32L();
       
   344 		
       
   345 		// 3. attachment type - TInt (enum)
       
   346 		iType = static_cast<TMsvAttachmentType>(aStream.ReadInt32L());
       
   347 		
       
   348 		// 4. attachment size - TInt
       
   349 		iSize = aStream.ReadInt32L();
       
   350 		
       
   351 		// 5. mime-type - HBufC8
       
   352 		iMimeType = HBufC8::NewL(aStream, KMaxTInt);
       
   353 				
       
   354 		// 6. message entry id - TInt (TMsvId)
       
   355 		iEntryId = aStream.ReadInt32L();
       
   356 		
       
   357 		// Versioning is required for file path, v1 simply stores the
       
   358 		// whole file path but v2 only stores the file name for EMsvFile
       
   359 		// types and requires that the attachment manager sets the path
       
   360 		// dynamically.		
       
   361 		if(	iType != EMsvMessageEntry )
       
   362 			{
       
   363 			// 7. file path - HBufC (if not message entry)
       
   364 			iFilePath = HBufC::NewL(aStream, KMaxTInt);
       
   365 			if( version==KMsvAttachmentVersion && iType==EMsvFile )
       
   366 				{
       
   367 				// Flag that the path still needs to be dynamically set
       
   368 				iNeedsPath = ETrue;
       
   369 				}
       
   370 			}
       
   371 		
       
   372 		// 8. attachment name - HBufC
       
   373 		iAttachmentName = HBufC::NewL(aStream, KMaxTInt);
       
   374 		
       
   375 		// 9. complete flag - TInt (TBool)
       
   376 		iComplete = aStream.ReadInt32L();
       
   377 		
       
   378 		// 10. the attributes (CMsvAttributeManager)
       
   379 		aStream >> *iAttributeManager;
       
   380 		}
       
   381 	}
       
   382 
       
   383 /**
       
   384 Indicates whether attachment is complete or not.
       
   385 
       
   386 This allows support for incomplete or pending attachments.
       
   387 
       
   388 @return ETrue if the attachment is complete, EFalse otherwise.
       
   389 */
       
   390 EXPORT_C TBool CMsvAttachment::Complete() const
       
   391 	{
       
   392 	return iComplete;
       
   393 	}
       
   394 
       
   395 /**
       
   396 Sets whether the attachment is complete or not.
       
   397 
       
   398 This allows support for incomplete or pending attachments.
       
   399 
       
   400 @param aComplete ETrue if the attachment is complete, EFalse otherwise.
       
   401 */
       
   402 EXPORT_C void CMsvAttachment::SetComplete(TBool aComplete)
       
   403 	{
       
   404 	iComplete = aComplete;
       
   405 	}
       
   406 
       
   407 /**
       
   408 Sets an 8-bit descriptor attribute for the attachment.
       
   409 
       
   410 A UID identifier uniquely identifies the attribute. If an attibute already exists with the same
       
   411 identifier, the old attribute is over-written.
       
   412 
       
   413 This can be used to store an attribute as an 8-bit descriptor or binary data.
       
   414 @param aAttributeId The unique identifier for the attribute.
       
   415 @param aAttribute The attribute data to store. The descriptor is copied internally.
       
   416 */
       
   417 EXPORT_C void CMsvAttachment::SetDesC8AttributeL(TUid aAttributeId, const TDesC8& aAttribute)
       
   418 	{
       
   419 	iAttributeManager->SetDesC8AttributeL(aAttributeId, aAttribute);
       
   420 	}
       
   421 
       
   422 /**
       
   423 Gets an 8-bit descriptor attribute.
       
   424 
       
   425 Gets the attribute uniquely identified by the UID identifer set using the SetDesC8AttributeL
       
   426 method.
       
   427 @param aAttributeId The unique identifier for the attribute.
       
   428 @param aAttribute If attribute is found, this will be set to point to the attribute data.
       
   429 @return KErrNone is successful, KErrNotFound if the attribute cannot be found.
       
   430 */	
       
   431 EXPORT_C TInt CMsvAttachment::GetDesC8Attribute(TUid aAttributeId, TPtrC8& aAttribute) const
       
   432 	{
       
   433 	return iAttributeManager->GetDesC8Attribute(aAttributeId, aAttribute);
       
   434 	}
       
   435 
       
   436 /**
       
   437 Removes an 8-bit descriptor attribute.
       
   438 
       
   439 Removes the attribute identified by its UID identifer. This methods has no effect if the
       
   440 attribute does not exist.
       
   441 @param aAttributeId The unique identifier for the attribute.
       
   442 */
       
   443 EXPORT_C void CMsvAttachment::RemoveDesC8Attribute(TUid aAttributeId)
       
   444 	{
       
   445 	iAttributeManager->RemoveDesC8Attribute(aAttributeId);
       
   446 	}
       
   447 
       
   448 /**
       
   449 Sets an integer attribute for the attachment.
       
   450 
       
   451 A UID identifier uniquely identifies the attribute. If an attibute already exists with the same
       
   452 identifier, the old attribute is over-written.
       
   453 @param aAttributeId The unique identifier for the attribute.
       
   454 @param aAttribute The attribute data to store.
       
   455 */
       
   456 EXPORT_C void CMsvAttachment::SetIntAttributeL(TUid aAttributeId, TInt aAttribute)
       
   457 	{
       
   458 	iAttributeManager->SetIntAttributeL(aAttributeId, aAttribute);
       
   459 	}
       
   460 
       
   461 /**
       
   462 Gets an integer attribute.
       
   463 
       
   464 Gets the attribute uniquely identified by the UID identifer set using the SetIntAttributeL method.
       
   465 @param aAttributeId The unique identifier for the attribute.
       
   466 @param aAttribute If attribute is found, this will be set to the attribute data.
       
   467 @return KErrNone is successful, KErrNotFound if the attribute cannot be found.
       
   468 */	
       
   469 EXPORT_C TInt CMsvAttachment::GetIntAttribute(TUid aAttributeId, TInt& aAttribute) const
       
   470 	{
       
   471 	return iAttributeManager->GetIntAttribute(aAttributeId, aAttribute);
       
   472 	}
       
   473 
       
   474 /**
       
   475 Removes an integer attribute.
       
   476 
       
   477 Removes the attribute identified by its UID identifer. This methods has no effect if the
       
   478 attribute does not exist.
       
   479 @param aAttributeId The unique identifier for the attribute.
       
   480 */	
       
   481 EXPORT_C void CMsvAttachment::RemoveIntAttribute(TUid aAttributeId)
       
   482 	{
       
   483 	iAttributeManager->RemoveIntAttribute(aAttributeId);
       
   484 	}
       
   485 	
       
   486 TBool CMsvAttachment::IsPathRequired() const
       
   487 	{
       
   488 	return iNeedsPath;
       
   489 	}
       
   490