messagingappbase/obexmtms/obexmtm/obexutil/source/obexpasswordexternaliser.cpp
changeset 31 ebfee66fde93
child 47 5b14749788d7
equal deleted inserted replaced
30:6a20128ce557 31:ebfee66fde93
       
     1 // Copyright (c) 2001-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 // $Workfile: obexpasswordexternaliser.cpp $
       
    15 // $Author: Stevep $
       
    16 // $Revision: 13 $
       
    17 // $Date: 26/03/02 11:34 $
       
    18 // 
       
    19 //
       
    20 
       
    21 //class include
       
    22 #include "obexpasswordexternaliser.h"
       
    23 //system includes
       
    24 #include <msvstore.h>   //CMsvStore, RMsvWriteStream, RMsvReadStream
       
    25 #include <msvids.h>
       
    26 #include <msvuids.h>
       
    27 
       
    28 //user includes
       
    29 #include "obexinternalutils.h"
       
    30 
       
    31 const TUid KObexPasswordStreamUid = {0x1000AAC6};
       
    32 
       
    33 
       
    34 void ObexPasswordExternaliser::SetPasswordFromServiceEntryL(CMsvEntry& aServiceEntry, const TDesC& aPassword)
       
    35 /**
       
    36 * Static setter function to set the ObexPassword associated with the given entry. If the length of the password is
       
    37 * zero, then the password is cleared. Calling this function causes the entry to be changed, too, which allows the watcher
       
    38 * to detect the change to the password.
       
    39 *
       
    40 * @param aServiceEntry The service entry to set the password into
       
    41 * @param aPassword the password to set
       
    42 * @leave KErrXXX system-wide error codes
       
    43 */
       
    44 	{
       
    45  	CMsvStore* serviceEntryStore = aServiceEntry.EditStoreL();
       
    46 	CleanupStack::PushL(serviceEntryStore);
       
    47 
       
    48 	if (!aPassword.Length())
       
    49 		{
       
    50 		//No password, so remove the password stream
       
    51 		serviceEntryStore->Remove(KObexPasswordStreamUid);
       
    52 		}
       
    53 	else
       
    54 		{
       
    55 		//Get the stream
       
    56 		RMsvWriteStream passwordWriteStream;
       
    57 		passwordWriteStream.AssignL(*serviceEntryStore, KObexPasswordStreamUid);
       
    58 
       
    59 		//Externalise the password 
       
    60 		HBufC* passwordBuf = aPassword.AllocLC();
       
    61 		ObexInternalUtils::ExternalizeL(passwordBuf, passwordWriteStream);
       
    62 		CleanupStack::PopAndDestroy();		//passwordBuf
       
    63 		passwordBuf = 0;
       
    64 
       
    65 		//Commit the stream
       
    66 		passwordWriteStream.CommitL();
       
    67 		passwordWriteStream.Close();
       
    68 		}
       
    69 
       
    70 	//"Change" the entry, so that the watcher will pick up the change if it's installed
       
    71 	aServiceEntry.ChangeL(aServiceEntry.Entry());
       
    72 
       
    73 	//Commit the store
       
    74 	serviceEntryStore->CommitL();
       
    75 
       
    76 	//Cleanup
       
    77 	CleanupStack::PopAndDestroy();		//serviceEntryStore
       
    78 	}
       
    79 
       
    80 
       
    81 EXPORT_C void ObexPasswordExternaliser::SetPasswordL(CMsvEntry& aEntry, const TDesC& aPassword)
       
    82 /**
       
    83 * Static setter function to set the ObexPassword associated with the given entry. If the length of the password is
       
    84 * zero, then the password is cleared
       
    85 *
       
    86 * @param aEntry CMsvEntry whose owning service entry's password is to be set
       
    87 * @param aPassword the password to set
       
    88 * @leave KErrXXX system-wide error codes
       
    89 */
       
    90 	{
       
    91 	TMsvId originalEntry = aEntry.EntryId();
       
    92 	TMsvId serviceEntry = aEntry.Entry().iServiceId;
       
    93 
       
    94 	//Switch to the service entry
       
    95 	aEntry.SetEntryL(serviceEntry);
       
    96 
       
    97 	SetPasswordFromServiceEntryL(aEntry, aPassword);
       
    98 
       
    99 	//Switch back
       
   100 	aEntry.SetEntryL(originalEntry);
       
   101 	}
       
   102 
       
   103 
       
   104 EXPORT_C void ObexPasswordExternaliser::SetPasswordL(CMsvSession& aSession, const TUid& aMtmUid, const TDesC& aPassword)
       
   105 /**
       
   106 * Static setter function to set the ObexPassword associated with the given entry. If the length of the password is
       
   107 * zero, then the password is cleared
       
   108 *
       
   109 * @param aEntry aSessionObserver A session observer
       
   110 * @param aMtmUid Uid of the MTM whose password is to be set
       
   111 * @param aPassword the password to set
       
   112 * @leave KErrXXX system-wide error codes
       
   113 */
       
   114 	{
       
   115 	CMsvEntry* serviceEntry = GetServiceEntryLC(aSession, aMtmUid);
       
   116 
       
   117 	SetPasswordFromServiceEntryL(*serviceEntry, aPassword);
       
   118 
       
   119 	CleanupStack::PopAndDestroy();		//serviceEntry
       
   120 	}
       
   121 
       
   122 
       
   123 HBufC* ObexPasswordExternaliser::GetPasswordFromServiceEntryLC(CMsvEntry& aServiceEntry)
       
   124 /**
       
   125 * Static getter function to get the ObexPassword from the given service entry. If the password stream doesn't exist,
       
   126 * a zero-length HBufC is returned. Ownership of the returned descriptor is passed to the caller, and is pushed onto the
       
   127 * cleanup stack.
       
   128 *
       
   129 * @param aServiceEntry CMsvEntry set to the service entry containing the password
       
   130 * @return HBufC containing the password (zero length if not set of cannot be read). Ownership is passed to the caller, and left on the cleanup stack
       
   131 * @leave KErrXXX system-wide error codes
       
   132 */
       
   133 	{
       
   134 	HBufC* returnDescriptor = 0;
       
   135 
       
   136 	CMsvStore* serviceEntryStore = 0;
       
   137 	TRAPD(editStoreErr, serviceEntryStore = aServiceEntry.ReadStoreL());
       
   138 	if (editStoreErr != KErrNone)
       
   139 		{
       
   140 		//Couldn't read the store--probably doesn't exist
       
   141 		returnDescriptor = HBufC::NewLC(0);
       
   142 		return returnDescriptor;
       
   143 		}
       
   144 	CleanupStack::PushL(serviceEntryStore);
       
   145 
       
   146 	//Get the stream
       
   147 	RMsvReadStream passwordReadStream;
       
   148 	TRAPD(assignErr, passwordReadStream.OpenL(*serviceEntryStore, KObexPasswordStreamUid));
       
   149 	if (assignErr == KErrNone)
       
   150 		{
       
   151 		//Stream opened OK
       
   152 		passwordReadStream.PushL();
       
   153 		ObexInternalUtils::InternalizeL(returnDescriptor, passwordReadStream);
       
   154 		CleanupStack::PopAndDestroy(2);		//passwordReadStream, serviceEntryStore
       
   155 		}
       
   156 	else
       
   157 		{
       
   158 		//Couldn't read from stream--probably doesn't exist
       
   159 		CleanupStack::PopAndDestroy();		//serviceEntryStore
       
   160 		returnDescriptor = HBufC::NewL(0);
       
   161 		}
       
   162 
       
   163 	CleanupStack::PushL(returnDescriptor);
       
   164 	return returnDescriptor;
       
   165 	}
       
   166 
       
   167 
       
   168 EXPORT_C HBufC* ObexPasswordExternaliser::GetPasswordLC(CMsvEntry& aEntry)
       
   169 /**
       
   170  * Static getter function to get the ObexPassword associated with the given entry. If the password stream doesn't exist,
       
   171  * a zero-length HBufC is returned. Ownership of the returned descriptor is passed to the caller, and is pushed onto the
       
   172  * cleanup stack.
       
   173  *
       
   174  * @param aEntry CMsvEntry whose owning service entry's password is to be set
       
   175  * @return HBufC containing the password (zero length if not set). Ownership is passed to the caller, and left on the cleanup stack
       
   176  * @leave KErrXXX system-wide error codes
       
   177  */
       
   178 	{
       
   179 	TMsvId originalEntry = aEntry.EntryId();
       
   180 	TMsvId serviceEntry = aEntry.Entry().iServiceId;
       
   181 
       
   182 	//Switch to the service entry, and get the store
       
   183 	aEntry.SetEntryL(serviceEntry);
       
   184 
       
   185 	HBufC* returnDescriptor = GetPasswordFromServiceEntryLC(aEntry);
       
   186 
       
   187 	//Switch back
       
   188 	aEntry.SetEntryL(originalEntry);
       
   189 
       
   190 	return returnDescriptor;
       
   191 	}
       
   192 
       
   193 
       
   194 EXPORT_C HBufC* ObexPasswordExternaliser::GetPasswordLC(CMsvSession& aSession, const TUid& aMtmUid)
       
   195 /**
       
   196  * Static getter function to get the ObexPassword associated with the given MTM UID. The zeroth service entry is assumed.
       
   197  * If the password stream doesn't exist,a zero-length HBufC is returned. Ownership of the returned descriptor is passed
       
   198  * to the caller, and is pushed onto the cleanup stack.
       
   199  *
       
   200  * @param aSessionObserver a session observer
       
   201  * @param aMtmUid UID of the MTM whose password is to be set
       
   202  * @return HBufC containing the password (zero length if not set). Ownership is passed to the caller, and left on the cleanup stack
       
   203  * @leave KErrXXX system-wide error codes
       
   204  */
       
   205 	{
       
   206 	CMsvEntry* serviceEntry = GetServiceEntryLC(aSession, aMtmUid);
       
   207 
       
   208 	HBufC* returnDescriptor = GetPasswordFromServiceEntryLC(*serviceEntry);
       
   209 
       
   210 	CleanupStack::Pop();			//returnDescriptor
       
   211 	CleanupStack::PopAndDestroy();	//serviceEntry
       
   212 	CleanupStack::PushL(returnDescriptor);
       
   213 
       
   214 	return returnDescriptor;
       
   215 	}
       
   216 
       
   217 
       
   218 EXPORT_C void ObexPasswordExternaliser::ResetPasswordL(CMsvEntry& aEntry)
       
   219 /**
       
   220 * Resets the password associated with the given entry. Does nothing if there is no password.
       
   221 *
       
   222 * @param aEntry CMsvEntry whose owning service entry's password is to be reset
       
   223 * @leave KErrXXX system-wide error codes
       
   224 */	
       
   225 	{
       
   226 	ObexPasswordExternaliser::SetPasswordL(aEntry, KNullDesC);
       
   227 	}
       
   228 
       
   229 
       
   230 EXPORT_C void ObexPasswordExternaliser::ResetPasswordL(CMsvSession& aSession, const TUid& aMtmUid)
       
   231 /**
       
   232 * Resets the password associated with the given entry. Does nothing if there is no password.
       
   233 *
       
   234 * @param aEntry aSessionObserver A session observer
       
   235 * @param aMtmUid Uid of the MTM whose password is to be set
       
   236 * @leave KErrXXX system-wide error codes
       
   237 */	
       
   238 	{
       
   239 	ObexPasswordExternaliser::SetPasswordL(aSession, aMtmUid, KNullDesC);
       
   240 	}
       
   241 
       
   242 
       
   243 CMsvEntry* ObexPasswordExternaliser::GetServiceEntryLC(CMsvSession& aSession, const TUid& aMtmUid)
       
   244 /**
       
   245 * Returns a CMsvEntry pointing to the zeroth service entry associated with aMtmUid. Ownership of the returned CMsvEntry
       
   246 * is passed to the caller, and it is left on the cleanup stack.
       
   247 *
       
   248 * @param aSession a session
       
   249 * @param aSessionObserver A session observer
       
   250 * @param aMtmUid Uid of the MTM whose service entry is to be found
       
   251 * @return A newly created CMsvEntry
       
   252 */
       
   253 	{
       
   254 	//Create the entry
       
   255 	TMsvSelectionOrdering order;
       
   256 	order.SetShowInvisibleEntries(ETrue);
       
   257 	CMsvEntry* returnEntry = CMsvEntry::NewL(aSession, KMsvRootIndexEntryId, order);
       
   258 	CleanupStack::PushL(returnEntry);
       
   259 
       
   260 	//Find the service entry
       
   261 	CMsvEntrySelection* serviceEntrySelection = returnEntry->ChildrenWithTypeL(KUidMsvServiceEntry);
       
   262 	CleanupStack::PushL(serviceEntrySelection);
       
   263 
       
   264 	TInt current;
       
   265 	for	(current = 0; current < serviceEntrySelection->Count(); current++)
       
   266 		{
       
   267 		returnEntry->SetEntryL(serviceEntrySelection->At(current));
       
   268 		if (returnEntry->Entry().iMtm == aMtmUid) break;
       
   269 		}
       
   270 
       
   271 	if (current == serviceEntrySelection->Count())
       
   272 		{
       
   273 		//No entry found--leave
       
   274 		User::Leave(KErrNotFound);
       
   275 		}
       
   276 	CleanupStack::PopAndDestroy(serviceEntrySelection);	
       
   277 
       
   278 	return returnEntry;
       
   279 	}