messagingfw/sendas/client/src/csendasaccounts.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 <s32strm.h>
       
    17 #include <csendasaccounts.h>
       
    18 
       
    19 const TInt KMessageAccountArrayGranularity = 8;
       
    20 const TInt16 KStreamVersionSendAs2Account = 1;
       
    21 const TInt KMaxSendAs2AccountNameLength = 1024;
       
    22 
       
    23 /**
       
    24 Create a new CSendAsAccount.
       
    25 
       
    26 @return
       
    27 If the function succeeds, this is a pointer to a newly allocated and initialised
       
    28 object.
       
    29 */
       
    30 EXPORT_C CSendAsAccounts* CSendAsAccounts::NewL()
       
    31 	{
       
    32 	CSendAsAccounts *self = new (ELeave) CSendAsAccounts;
       
    33 	CleanupStack::PushL(self);
       
    34 	self->ConstructL();
       
    35 	CleanupStack::Pop(self);
       
    36 	return self;
       
    37 	}
       
    38 
       
    39 /**
       
    40 Destructor.
       
    41 */	
       
    42 EXPORT_C CSendAsAccounts::~CSendAsAccounts()
       
    43 	{
       
    44 	delete iAccountNames;
       
    45 	iAccounts.Close();
       
    46 	}
       
    47 
       
    48 /**
       
    49 The UID of the message type.
       
    50 
       
    51 @return
       
    52 The UID of the message type.
       
    53 */
       
    54 EXPORT_C TUid CSendAsAccounts::MessageType() const
       
    55 	{
       
    56 	return iMessageType;
       
    57 	}
       
    58 	
       
    59 /**
       
    60 The array of names of accounts for this message type.
       
    61 
       
    62 @return
       
    63 An array of name of accounts.
       
    64 */
       
    65 EXPORT_C const MDesCArray& CSendAsAccounts::AccountNames() const
       
    66 	{
       
    67 	return (*iAccountNames);
       
    68 	}
       
    69 	
       
    70 /**
       
    71 The account specified by the index argument.
       
    72 
       
    73 @param	aIndex
       
    74 The index of the specified account.
       
    75 
       
    76 @return
       
    77 The account specified by the index argument.
       
    78 */
       
    79 EXPORT_C TSendAsAccount CSendAsAccounts::Account(TInt aIndex) const
       
    80 	{
       
    81 	return iAccounts[aIndex];
       
    82 	}
       
    83 	
       
    84 /**
       
    85 The number of accounts for this message type.
       
    86 
       
    87 @return
       
    88 The number of accounts.
       
    89 */
       
    90 EXPORT_C TInt CSendAsAccounts::Count() const
       
    91 	{
       
    92 	return iAccountNames->Count();
       
    93 	}
       
    94 
       
    95 /**
       
    96 Deletes all elements from the array.
       
    97 */
       
    98 EXPORT_C void CSendAsAccounts::Reset()
       
    99 	{
       
   100 	iAccountNames->Reset();
       
   101 	iAccounts.Reset();
       
   102 	}
       
   103 	
       
   104 /**
       
   105 The name associated with the given account.
       
   106 
       
   107 @param aAccount
       
   108 The account whose associated name is to be returned.
       
   109 
       
   110 @return
       
   111 A descriptor holding the associated name.
       
   112 */
       
   113 EXPORT_C TPtrC CSendAsAccounts::NameFromAccountL(const TSendAsAccount aAccount) const
       
   114 	{
       
   115 	TInt nameIndex = User::LeaveIfError(iAccounts.Find(aAccount));
       
   116 	return (*iAccountNames)[nameIndex];
       
   117 	}
       
   118 	
       
   119 /**
       
   120 The account associated with the given name.
       
   121 
       
   122 @param aName
       
   123 The associated name for the account to be returned.
       
   124 
       
   125 @return
       
   126 An account for the associated name.
       
   127 
       
   128 @leave  KErrNotFound
       
   129 An account with the given name cannot be found.
       
   130 */
       
   131 EXPORT_C TSendAsAccount CSendAsAccounts::AccountFromNameL(const TDesC& aName) const
       
   132 	{
       
   133 	TInt acctIndex;
       
   134 	if (iAccountNames->Find(aName, acctIndex) != 0)
       
   135 		{
       
   136 		User::Leave(KErrNotFound);
       
   137 		}
       
   138 	return iAccounts[acctIndex];
       
   139 	}
       
   140 
       
   141 /**
       
   142 Internalizes the object from the specified stream.
       
   143 
       
   144 @see
       
   145 RMsvReadStream
       
   146 
       
   147 @param aReadStream
       
   148 The stream to read from.
       
   149 
       
   150 @internalComponent
       
   151 */
       
   152 EXPORT_C void CSendAsAccounts::InternalizeL(RReadStream& aReadStream)
       
   153 	{
       
   154 	aReadStream.ReadInt16L(); // version - not used yet
       
   155 
       
   156 	aReadStream >> iMessageType;
       
   157 
       
   158 	TInt count = aReadStream.ReadInt32L();
       
   159 
       
   160 	Reset();
       
   161 
       
   162 	TSendAsAccount sendAsAccount;
       
   163 	for (TInt i = 0; i < count; ++i)
       
   164 		{
       
   165 		HBufC* buf = HBufC::NewLC(aReadStream, KMaxSendAs2AccountNameLength);
       
   166 		aReadStream >> sendAsAccount;
       
   167 		AppendAccountL(*buf, sendAsAccount);
       
   168 		
       
   169 		CleanupStack::PopAndDestroy(buf);
       
   170 		}
       
   171 	}
       
   172 
       
   173 
       
   174 /**
       
   175 Externalizes the object from the specified stream.
       
   176 
       
   177 @see
       
   178 RMsvWriteStream
       
   179 
       
   180 @param aWriteStream
       
   181 The stream to write out to.
       
   182 
       
   183 @internalComponent
       
   184 */
       
   185 EXPORT_C void CSendAsAccounts::ExternalizeL(RWriteStream& aWriteStream) const
       
   186 	{
       
   187 	__ASSERT_ALWAYS((iAccountNames->Count() == iAccounts.Count()), User::Invariant());
       
   188 	
       
   189 	aWriteStream.WriteInt16L(KStreamVersionSendAs2Account); // Version number
       
   190 	aWriteStream << iMessageType; // Message type
       
   191 
       
   192 	TInt count = iAccountNames->Count();	
       
   193 	aWriteStream.WriteInt32L(count); // The count of name, uid object pairs to write out.
       
   194 
       
   195 	for (TInt i = 0; i < count; ++i)
       
   196 		{
       
   197 		if ((*iAccountNames)[i].Length() > KMaxSendAs2AccountNameLength)
       
   198 			{
       
   199 			// Limit the string length if necessary.
       
   200 			aWriteStream.WriteL((*iAccountNames)[i], KMaxSendAs2AccountNameLength);
       
   201 			}
       
   202 		else
       
   203 			{
       
   204 			aWriteStream << (*iAccountNames)[i];
       
   205 			}
       
   206 		aWriteStream << iAccounts[i];
       
   207 		}
       
   208 	}
       
   209 
       
   210 /**
       
   211 Sets UID of the message type.
       
   212 
       
   213 @param aMessageType
       
   214 The UID of the message type.
       
   215 */
       
   216 EXPORT_C void CSendAsAccounts::SetMessageType(TUid aMessageType)
       
   217 	{
       
   218 	iMessageType = aMessageType;
       
   219 	}
       
   220 
       
   221 /**
       
   222 Appends a new account and associated name pair.
       
   223 
       
   224 @param aAccountName
       
   225 The human readable name with which to associate with the given account.
       
   226 
       
   227 @param aAccount
       
   228 The account to add.
       
   229 */	
       
   230 EXPORT_C void CSendAsAccounts::AppendAccountL(const TDesC& aAccountName, TSendAsAccount aAccount)
       
   231 	{
       
   232 	iAccounts.AppendL(aAccount);
       
   233 	TRAPD(err, iAccountNames->AppendL(aAccountName));
       
   234 	if (err != KErrNone)
       
   235 		{
       
   236 		iAccounts.Remove(iAccounts.Count() - 1);
       
   237 		User::Leave(err);
       
   238 		}
       
   239 	}	
       
   240 
       
   241 /**
       
   242 Removes an existing account and associated name pair.
       
   243 
       
   244 @param aIndex
       
   245 The index of the entry to remove.
       
   246 */
       
   247 EXPORT_C void CSendAsAccounts::RemoveAccount(TInt aIndex)
       
   248 	{
       
   249 	iAccountNames->Delete(aIndex);
       
   250 	iAccounts.Remove(aIndex);
       
   251 	}
       
   252 
       
   253 /**
       
   254 Gives the size of the accounts collection.
       
   255 
       
   256 @return
       
   257 The size of the accounts collection.
       
   258 */
       
   259 EXPORT_C TInt CSendAsAccounts::Size() const
       
   260 	{
       
   261 	const TInt acctSize(sizeof(TSendAsAccount));
       
   262 	const TInt tIntSize(sizeof(TInt));
       
   263 	TInt totalSize = sizeof(TInt16) + sizeof(TUid); // The version and message type.
       
   264 	TInt count = iAccountNames->Count();
       
   265 
       
   266 	totalSize += tIntSize; // Size of the number of accounts
       
   267 
       
   268 	for (TInt i = 0; i < count; ++i)
       
   269 		{
       
   270 		// Each account consists of an integer which tells us the size of the
       
   271 		// account name, the account name, and the the TSendAsAccount data. We must
       
   272 		// add the size of each of these items to the total.
       
   273 		// Note that when externalizing an account, we don't explicitly write out an
       
   274 		// integer containing the size of the account name, but the externalize
       
   275 		// routine for the descriptor will write one out, so we must include it in
       
   276 		// our calculation here.
       
   277 		totalSize += tIntSize + (*iAccountNames)[i].Size() + acctSize;
       
   278 		}
       
   279 
       
   280 	return totalSize;
       
   281 	}
       
   282 	
       
   283 CSendAsAccounts::CSendAsAccounts()
       
   284 : iMessageType(KNullUid), iAccounts(KMessageAccountArrayGranularity)
       
   285 	{
       
   286 	}
       
   287 	
       
   288 void CSendAsAccounts::ConstructL()
       
   289 	{
       
   290 	iAccountNames = new (ELeave) CDesCArrayFlat(KMessageAccountArrayGranularity);
       
   291 	}