syncmlfw/dm/treedbhandler/src/nsmldmaclparser.cpp
changeset 0 b497e44ab2fc
equal deleted inserted replaced
-1:000000000000 0:b497e44ab2fc
       
     1 /*
       
     2 * Copyright (c) 2005 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:  source file for dm acl parser
       
    15 *
       
    16 */
       
    17 
       
    18 #include "nsmldmaclparser.h"
       
    19 
       
    20 
       
    21 // ===============================================================================================
       
    22 // class CNSmlDmACLParser
       
    23 // ===============================================================================================
       
    24 
       
    25 
       
    26 // ---------------------------------------------------------------------------
       
    27 // TInt CNSmlDmACLParser::NewLC()
       
    28 // Returns pointer to newly created CNSmlDmACLParser instance
       
    29 // ---------------------------------------------------------------------------
       
    30 CNSmlDmACLParser* CNSmlDmACLParser::NewLC()
       
    31 	{
       
    32 	CNSmlDmACLParser* self = new (ELeave) CNSmlDmACLParser();
       
    33 	CleanupStack::PushL( self );
       
    34 	return self;
       
    35 	}
       
    36 
       
    37 
       
    38 // ---------------------------------------------------------------------------
       
    39 // CNSmlDmACLParser::~CNSmlDmACLParser()
       
    40 // Destructor
       
    41 // ---------------------------------------------------------------------------
       
    42 CNSmlDmACLParser::~CNSmlDmACLParser()
       
    43 	{
       
    44 	Reset();
       
    45 	}
       
    46 
       
    47 // ---------------------------------------------------------------------------
       
    48 // CNSmlDmACLParser::ParseL()
       
    49 // Parses ACL data and keeps data until Reset() is called or instance
       
    50 // is destructed
       
    51 // ---------------------------------------------------------------------------
       
    52 TInt CNSmlDmACLParser::ParseL(const TDesC8& aACL)
       
    53 	{
       
    54 	Reset();
       
    55 	for(TInt i=EAclExecute;i>=EAclAdd;i--)
       
    56 		{
       
    57 		TInt aclStart = 0;
       
    58 		TBool found=EFalse;
       
    59 		switch(i)
       
    60 			{
       
    61 			case EAclAdd:
       
    62 				aclStart = aACL.Find(KNSmlDmAclAddEqual);
       
    63 				found = aclStart>=0;
       
    64 				aclStart=aclStart+KNSmlDmAclAddEqual().Length();
       
    65 				break;
       
    66 			case EAclReplace:
       
    67 				aclStart = aACL.Find(KNSmlDmAclReplaceEqual);
       
    68 				found = aclStart>=0;
       
    69 				aclStart=aclStart+KNSmlDmAclReplaceEqual().Length();
       
    70 				break;
       
    71 			case EAclDelete:
       
    72 				aclStart = aACL.Find(KNSmlDmAclDeleteEqual);
       
    73 				found = aclStart>=0;
       
    74 				aclStart=aclStart+KNSmlDmAclDeleteEqual().Length();
       
    75 				break;
       
    76 			case EAclGet:
       
    77 				aclStart = aACL.Find(KNSmlDmAclGetEqual);
       
    78 				found = aclStart>=0;
       
    79 				aclStart=aclStart+KNSmlDmAclGetEqual().Length();
       
    80 				break;
       
    81 			case EAclExecute:
       
    82 				aclStart = aACL.Find(KNSmlDmAclExecEqual);
       
    83 				found = aclStart>=0;
       
    84 				aclStart=aclStart+KNSmlDmAclExecEqual().Length();
       
    85 				break;
       
    86 			default:
       
    87 				User::Panic(KSmlDmTreeDbHandlerPanic,KErrArgument);
       
    88 				break;
       
    89 
       
    90 			}
       
    91 		if(found)
       
    92 			{
       
    93 			TInt aclStop = aACL.Right(aACL.Length()-aclStart).
       
    94 				Locate(KNSmlDMAclCommandSeparator);
       
    95 
       
    96 			if(aclStop<0)
       
    97 				{
       
    98 				aclStop = aACL.Length()-aclStart;
       
    99 				}
       
   100 
       
   101 			TPtrC8 commandAcl = aACL.Mid(aclStart,aclStop);
       
   102 
       
   103 			CNSmlAclElement* aclElement = new(ELeave) CNSmlAclElement();
       
   104 
       
   105 			aclElement->iCommandType = (TNSmlDmCmdType)i;
       
   106 			aclElement->iNext = iCommandAcls;
       
   107 			iCommandAcls=aclElement;
       
   108 
       
   109 			if(commandAcl.Compare(KNSmlDmAclAll)==0)
       
   110 				{
       
   111 				aclElement->iAllServers=ETrue;
       
   112 				}
       
   113 			else
       
   114 				{
       
   115 				TBool end = EFalse;
       
   116 
       
   117 				TInt serverIdStart=0;
       
   118 				while(!end)
       
   119 					{
       
   120 					TPtrC8 serverIdPtr =
       
   121 						commandAcl.Right(commandAcl.Length()-serverIdStart);
       
   122 						
       
   123 					TInt serverIdStop =
       
   124 						serverIdPtr.Locate(KNSmlDMAclSeparator);
       
   125 						
       
   126 					if(serverIdStop == KErrNotFound)
       
   127 						{
       
   128 						serverIdStop=commandAcl.Length();
       
   129 						end=ETrue;
       
   130 						}
       
   131 					HBufC8* serverId =
       
   132 						serverIdPtr.Left(serverIdStop).AllocL();
       
   133 						
       
   134 					aclElement->iServerIds.AppendL(serverId);
       
   135 					serverIdStart=serverIdStart+serverIdStop+1;
       
   136 					}
       
   137 				}
       
   138 			}
       
   139 		}
       
   140 	return KErrNone;
       
   141 	}
       
   142 
       
   143 // ---------------------------------------------------------------------------
       
   144 // CNSmlDmACLParser::GenerateL()
       
   145 // Generates ACL data string from the kept data.
       
   146 // ---------------------------------------------------------------------------
       
   147 HBufC8* CNSmlDmACLParser::GenerateL()
       
   148 	{
       
   149 	CBufBase *acl = CBufFlat::NewL(32);
       
   150 	CleanupStack::PushL(acl);
       
   151 
       
   152 	CNSmlAclElement* aclElement=iCommandAcls;
       
   153 
       
   154 	while(aclElement)
       
   155 		{
       
   156 		TBool anyServerIds=EFalse;
       
   157 		if(aclElement->iAllServers||aclElement->iServerIds.Count())
       
   158 			{
       
   159 			anyServerIds = ETrue;
       
   160 			switch(aclElement->iCommandType)
       
   161 				{
       
   162 				case EAclAdd:
       
   163 					acl->InsertL(acl->Size(),KNSmlDmAclAddEqual);
       
   164 					break;
       
   165 				case EAclReplace:
       
   166 					acl->InsertL(acl->Size(),KNSmlDmAclReplaceEqual);
       
   167 					break;
       
   168 				case EAclDelete:
       
   169 					acl->InsertL(acl->Size(),KNSmlDmAclDeleteEqual);
       
   170 					break;
       
   171 				case EAclGet:
       
   172 					acl->InsertL(acl->Size(),KNSmlDmAclGetEqual);
       
   173 					break;
       
   174 				case EAclExecute:
       
   175 					acl->InsertL(acl->Size(),KNSmlDmAclExecEqual);
       
   176 					break;
       
   177 				default:
       
   178 					User::Panic(KSmlDmTreeDbHandlerPanic,KErrArgument);
       
   179 					break;
       
   180 				}
       
   181 			if(aclElement->iAllServers)
       
   182 				{
       
   183 				acl->InsertL(acl->Size(),KNSmlDmAclAll);
       
   184 				}
       
   185 			else
       
   186 				{
       
   187 				for(TInt i=0;i<aclElement->iServerIds.Count();i++)
       
   188 					{
       
   189 					acl->InsertL(acl->Size(),*aclElement->iServerIds[i]);
       
   190 					if(i<aclElement->iServerIds.Count()-1)
       
   191 						{
       
   192 						acl->InsertL(acl->Size(),KNSmlDmAclServerIdSeparator);
       
   193 						}
       
   194 					}
       
   195 				}
       
   196 			}
       
   197 
       
   198 		aclElement=aclElement->iNext;
       
   199 		if(aclElement&&anyServerIds)
       
   200 			{
       
   201 			acl->InsertL(acl->Size(),KNSmlDmAclSeparator);
       
   202 			}
       
   203 		}
       
   204 	
       
   205 	HBufC8 *aclBuf = HBufC8::NewL(acl->Size());
       
   206 	TPtr8 bufPtr = aclBuf->Des();
       
   207 	acl->Read(0,bufPtr,acl->Size());
       
   208 	CleanupStack::PopAndDestroy(); //acl
       
   209 	return aclBuf;
       
   210 	}
       
   211 
       
   212 // ---------------------------------------------------------------------------
       
   213 // CNSmlDmACLParser::RemoveAllReferences()
       
   214 // Removes all references to aServerId
       
   215 // ---------------------------------------------------------------------------
       
   216 void CNSmlDmACLParser::RemoveAllReferences(const TDesC8& aServerId)
       
   217 	{
       
   218 	CNSmlAclElement* aclElement=iCommandAcls;
       
   219 
       
   220 
       
   221 	while(aclElement)
       
   222 		{
       
   223 		for(TInt i=0;i<aclElement->iServerIds.Count();i++)
       
   224 			{
       
   225 			if(aclElement->iServerIds[i]->Compare(aServerId)==0)
       
   226 				{
       
   227 				delete aclElement->iServerIds[i];
       
   228 				aclElement->iServerIds.Remove(i);
       
   229 				break;
       
   230 				}
       
   231 			}
       
   232 		aclElement=aclElement->iNext;
       
   233 		}
       
   234 	}
       
   235 
       
   236 // ---------------------------------------------------------------------------
       
   237 // CNSmlDmACLParser::ShouldDelete()
       
   238 // Check if the instance contains any data to generate acl string.
       
   239 // If not acl can be deleted
       
   240 // ---------------------------------------------------------------------------
       
   241 TBool CNSmlDmACLParser::ShouldDelete()
       
   242 	{
       
   243 	CNSmlAclElement* aclElement=iCommandAcls;
       
   244 
       
   245 	while(aclElement)
       
   246 		{
       
   247 		if(aclElement->iAllServers||aclElement->iServerIds.Count())
       
   248 			{
       
   249 			return EFalse;
       
   250 			}
       
   251 		aclElement=aclElement->iNext;
       
   252 		}
       
   253 	return ETrue;	
       
   254 	}
       
   255 
       
   256 // ---------------------------------------------------------------------------
       
   257 // CNSmlDmACLParser::HasRights()
       
   258 // Check if the aServerId server has rights for command aCommand type
       
   259 // ---------------------------------------------------------------------------
       
   260 TBool CNSmlDmACLParser::HasRights(const TDesC8& aServerId,
       
   261 	TNSmlDmCmdType aCommandType)
       
   262 	{
       
   263 	CNSmlAclElement* aclElement=iCommandAcls;
       
   264 	TBool ret = EFalse;
       
   265 
       
   266 	while(aclElement)
       
   267 		{
       
   268 		if(aclElement->iCommandType==aCommandType)
       
   269 			{
       
   270 			if(aclElement->iAllServers)
       
   271 				{
       
   272 				ret = ETrue;
       
   273 				}
       
   274 			else
       
   275 				{
       
   276 				for(TInt i=0;i<aclElement->iServerIds.Count();i++)
       
   277 					{
       
   278 					if(aclElement->iServerIds[i]->Compare(aServerId)==0)
       
   279 						{
       
   280 						ret = ETrue;
       
   281 						break;
       
   282 						}
       
   283 					}
       
   284 				}
       
   285 			if(ret)
       
   286 				{
       
   287 				break;
       
   288 				}
       
   289 			}
       
   290 		aclElement=aclElement->iNext;
       
   291 		}
       
   292 	return ret;	
       
   293 	}
       
   294 
       
   295 // ---------------------------------------------------------------------------
       
   296 // CNSmlDmACLParser::Reset()
       
   297 // Resets the instance data
       
   298 // ---------------------------------------------------------------------------
       
   299  void CNSmlDmACLParser::Reset()
       
   300 	{
       
   301 	CNSmlAclElement* aclElement=iCommandAcls;
       
   302 
       
   303 	while(iCommandAcls)
       
   304 		{
       
   305 		aclElement = iCommandAcls->iNext;
       
   306 		delete iCommandAcls;
       
   307 		iCommandAcls=aclElement;
       
   308 		}
       
   309 	}
       
   310 
       
   311 
       
   312 // ===============================================================================================
       
   313 // class CNSmlAclElement
       
   314 // ===============================================================================================
       
   315 
       
   316 // ---------------------------------------------------------------------------
       
   317 // CNSmlAclElement::CNSmlAclElement()
       
   318 // constructor
       
   319 // ---------------------------------------------------------------------------
       
   320 CNSmlAclElement::CNSmlAclElement()
       
   321 	{
       
   322 	}
       
   323 	
       
   324 // ---------------------------------------------------------------------------
       
   325 // CNSmlAclElement::~CNSmlAclElement()
       
   326 // destructor
       
   327 // ---------------------------------------------------------------------------
       
   328 CNSmlAclElement::~CNSmlAclElement()
       
   329 	{
       
   330 	iServerIds.ResetAndDestroy();
       
   331 	}