libraries/qr3/src/propertywatcher.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // propertywatcher.cpp
       
     2 // 
       
     3 // Copyright (c) 2010 Accenture. All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of the "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 // Accenture - Initial contribution
       
    11 //
       
    12 #include <fshell/qr3dll.h>
       
    13 
       
    14 EXPORT_C CPropertyManager* CPropertyManager::NewL(TCallBack aCallback)
       
    15 	{
       
    16 	CPropertyManager* self = new(ELeave) CPropertyManager(aCallback);
       
    17 	return self;
       
    18 	}
       
    19 
       
    20 EXPORT_C void CPropertyManager::SetPropertyFromStringL(const TDesC& /*aString*/)
       
    21 	{
       
    22 	}
       
    23 
       
    24 EXPORT_C HBufC* CPropertyManager::GetValueL()
       
    25 	{
       
    26 	if (!iProperty.Handle()) User::Leave(KErrNotReady);
       
    27 
       
    28 	TInt err = KErrNone;
       
    29 	if (iPropertyType == RProperty::ETypeLimit)
       
    30 		{
       
    31 		// First time called, need to figure out what type the property is
       
    32 		TInt propInt;
       
    33 		//TBuf8<1> propBuf8;
       
    34 		
       
    35 		err = iProperty.Get(propInt);
       
    36 		if (err == KErrNotFound)
       
    37 			{
       
    38 			return NULL;
       
    39 			}
       
    40 		if (err == KErrPermissionDenied)
       
    41 			{
       
    42 			User::Leave(err);
       
    43 			}
       
    44 
       
    45 		if (err != KErrArgument)
       
    46 			{
       
    47 			iPropertyType = RProperty::EInt;
       
    48 			}
       
    49 		else
       
    50 			{
       
    51 			//err = iProperty.Get(propBuf8);
       
    52 			//if (err == 
       
    53 			// Just assume it's binary, for now
       
    54 			iPropertyType = RProperty::EByteArray;
       
    55 			}
       
    56 		}
       
    57 
       
    58 	switch (iPropertyType)
       
    59 		{
       
    60 		case RProperty::EInt:
       
    61 			{
       
    62 			TInt val;
       
    63 			TInt err = iProperty.Get(val);
       
    64 			if (err == KErrNone)
       
    65 				{
       
    66 				HBufC* res = HBufC::NewL(15);
       
    67 				res->Des().Format(_L("%i"), val);
       
    68 				return res;
       
    69 				}
       
    70 			else if (err == KErrArgument)
       
    71 				{
       
    72 				// Someone's redefined the property as a different type
       
    73 				iPropertyType = RProperty::ETypeLimit;
       
    74 				return GetValueL();
       
    75 				}
       
    76 			else if (err == KErrNotFound)
       
    77 				{
       
    78 				// property deleted or undefined
       
    79 				return NULL;
       
    80 				}
       
    81 			else
       
    82 				{
       
    83 				User::Leave(err);
       
    84 				}
       
    85 			break;
       
    86 			}
       
    87 		case RProperty::EByteArray:
       
    88 			{
       
    89 			for (TInt size = 512; /*Nothing*/; size *= 2)
       
    90 				{
       
    91 				HBufC8* val = HBufC8::NewL(size);
       
    92 				TPtr8 ptr = val->Des();
       
    93 				TInt err = iProperty.Get(ptr);
       
    94 				if (err == KErrNone)
       
    95 					{
       
    96 
       
    97 					_LIT(KBinary, "Binary:");
       
    98 					CleanupStack::PushL(val);
       
    99 					HBufC* res = HBufC::NewL(val->Length() * 4 + KBinary().Length());
       
   100 					TPtr ptr = res->Des();
       
   101 					//ptr.Append(KBinary);
       
   102 					for (TInt i = 0; i < val->Length(); i++)
       
   103 						{
       
   104 						if (i % 8 == 0) ptr.Append('\n');
       
   105 						if (i % 4 == 0 && i != 0) ptr.Append(' ');
       
   106 						TBuf<2> numBuf;
       
   107 						numBuf.NumFixedWidthUC((*val)[i], EHex, 2);
       
   108 						ptr.Append(numBuf);
       
   109 						//ptr.AppendFormat(_L("%02x"), (TUint)(*val)[i]);
       
   110 						}
       
   111 					CleanupStack::PopAndDestroy(val);
       
   112 					return res;
       
   113 					}
       
   114 				else
       
   115 					{
       
   116 					delete val;
       
   117 					}
       
   118 
       
   119 				if (err == KErrOverflow)
       
   120 					{
       
   121 					// Try with bigger buffer
       
   122 					continue;
       
   123 					}
       
   124 				else if (err == KErrArgument)
       
   125 					{
       
   126 					// Someone's redefined the property as a different type
       
   127 					iPropertyType = RProperty::ETypeLimit;
       
   128 					return GetValueL();
       
   129 					}
       
   130 				else if (err == KErrNotFound)
       
   131 					{
       
   132 					// property deleted or undefined
       
   133 					return NULL;
       
   134 					}
       
   135 				else
       
   136 					{
       
   137 					User::Leave(err);
       
   138 					}
       
   139 				}
       
   140 			}
       
   141 		default:
       
   142 			break;
       
   143 		}
       
   144 	return NULL; // To satisfy compiler
       
   145 	}
       
   146 
       
   147 EXPORT_C TInt CPropertyManager::ChangeProperty(TUint aCategory, TUint aKey)
       
   148 	{
       
   149 	Cancel();
       
   150 	iPropertyType = RProperty::ETypeLimit;
       
   151 	iCat = aCategory;
       
   152 	iKey = aKey;
       
   153 
       
   154 	iProperty.Close();
       
   155 	TInt err = iProperty.Attach(TUid::Uid(aCategory), aKey);
       
   156 	if (!err)
       
   157 		{
       
   158 		Watch();
       
   159 		}
       
   160 	return err;
       
   161 	}
       
   162 
       
   163 void CPropertyManager::Watch()
       
   164 	{
       
   165 	if (!IsActive())
       
   166 		{
       
   167 		iProperty.Subscribe(iStatus);
       
   168 		SetActive();
       
   169 		}
       
   170 	}
       
   171 
       
   172 void CPropertyManager::RunL()
       
   173 	{
       
   174 	if (!iStatus.Int())
       
   175 		{
       
   176 		Watch();
       
   177 		}
       
   178 	iChangedFn.CallBack();
       
   179 	}
       
   180 
       
   181 void CPropertyManager::DoCancel()
       
   182 	{
       
   183 	iProperty.Cancel();
       
   184 	}
       
   185 
       
   186 CPropertyManager::CPropertyManager(TCallBack aCallback)
       
   187 	: CActive(CActive::EPriorityStandard), iPropertyType(RProperty::ETypeLimit), iChangedFn(aCallback)
       
   188 	{
       
   189 	CActiveScheduler::Add(this);
       
   190 	}
       
   191 
       
   192 CPropertyManager::~CPropertyManager()
       
   193 	{
       
   194 	Cancel();
       
   195 	iProperty.Close();
       
   196 	}