phonebookengines/contactsmodel/src/cntviewstore.cpp
branchRCL_3
changeset 63 f4a778e096c2
parent 0 e686773b3f54
child 24 0ba2181d7c28
equal deleted inserted replaced
62:5b6f26637ad3 63:f4a778e096c2
       
     1 // Copyright (c) 2005-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 <e32base.h>
       
    17 #include <centralrepository.h>
       
    18 #include <cntdb.h>
       
    19 #include "CNTSTD.H"
       
    20 #include "rcntmodel.h"
       
    21 #include <cntviewbase.h>
       
    22 #include <cntviewstore.h>
       
    23 #include "cntviewstoreprivate.h"
       
    24 
       
    25 
       
    26 /*
       
    27   Constant values used to access the view definition data 
       
    28   in the central repository.
       
    29 */
       
    30 //const TUint32 KTableMask = 0x00FF0000;
       
    31 const TUint32 KRowMask = 0x0000FF00;
       
    32 const TUint32 KColumnMask = 0x000000FF;
       
    33 
       
    34 const TUint32 KViewDefinitionsTable = 0x00010000;
       
    35 const TUint32 KViewSortOrderTable = 0x00020000;
       
    36 const TUint32 KTypeCol = 0x00000000;
       
    37 const TUint32 KNameCol = 0x00000001;
       
    38 const TUint32 KSortPluginCol = 0x00000002;
       
    39 const TUint32 KPreferencesCol = 0x00000003;
       
    40 const TUint32 KSortOrderIdxCol = 0x00000004;
       
    41 
       
    42 const TUid KContactsRepositoryUid = { 0x10003A73 };
       
    43 
       
    44 /** 
       
    45 	Stores a list of default remote view definitions in the contacts servers' 
       
    46     persistent data store. The view definitions are assumed to relate to 
       
    47     the default contacts database although this is not enforced.
       
    48 
       
    49 @param aDefaultViews Array of view definitions to store
       
    50 
       
    51 @publishedPartner
       
    52 @released
       
    53 */
       
    54 EXPORT_C void TContactViewStore::SetDefaultViewsL(const RPointerArray<CContactDefaultViewDefinition> &aDefaultViews)
       
    55 	{
       
    56 	CRepository* rep = CRepository::NewLC(KContactsRepositoryUid);
       
    57 	
       
    58 	// Attempt to store in one transaction; if it fails, pass
       
    59 	// the error on as a leave.
       
    60 	rep->StartTransaction(CRepository::EConcurrentReadWriteTransaction);
       
    61 
       
    62 	// clear out any existing entries
       
    63 	TUint32 errorKey;
       
    64 	rep->Delete(KViewDefinitionsTable, ~(KRowMask | KColumnMask), errorKey);
       
    65 	rep->Delete(KViewSortOrderTable, ~(KRowMask | KColumnMask), errorKey);
       
    66 	
       
    67 	// for each view definition
       
    68 	TInt count = aDefaultViews.Count();
       
    69 	for (TInt i = 0; i < count; i++)
       
    70 		{
       
    71 		CContactDefaultViewDefinition* def = aDefaultViews[i];
       
    72 
       
    73 		// View Type
       
    74 		TUint32 key = (KViewDefinitionsTable | (i << 8) | KTypeCol);
       
    75 		rep->Set(key, def->ViewType());
       
    76 				
       
    77 		// View Name
       
    78 		key = (KViewDefinitionsTable | (i << 8) | KNameCol);
       
    79 		rep->Set(key, def->ViewNameL());
       
    80 		
       
    81 		// Sort Plugin Name
       
    82 		key = (KViewDefinitionsTable | (i << 8) | KSortPluginCol);
       
    83 		rep->Set(key, def->SortPluginNameL());
       
    84 
       
    85 		// View Preferences
       
    86 		key = (KViewDefinitionsTable | (i << 8) | KPreferencesCol);
       
    87 		rep->Set(key, def->ViewPreferences());
       
    88 		
       
    89 		// Sort Order Row Index
       
    90 		key = (KViewDefinitionsTable | (i << 8) | KSortOrderIdxCol);
       
    91 		rep->Set(key, i);
       
    92 		
       
    93 		// Write the entries for the sort order in the sort order table
       
    94 		// Need Assert that Count() <= 256 (i.e. 0xFF)
       
    95 		TInt fieldCount = def->SortOrder().Count();
       
    96 		for (TInt j = 0; j < fieldCount; j++)
       
    97 			{
       
    98 			key = (KViewSortOrderTable | (i << 8) | j);
       
    99 			rep->Set(key, static_cast<TInt>(def->SortOrder()[j].iUid));
       
   100 			}
       
   101 		}
       
   102 
       
   103 	User::LeaveIfError(rep->CommitTransaction(errorKey));
       
   104 		
       
   105 	CleanupStack::PopAndDestroy(rep);	
       
   106 	}
       
   107 
       
   108 /** 
       
   109 	Retrieves the list of default view definitions stored in the contacts
       
   110     servers' persistent data storage.
       
   111     
       
   112 @publishedPartner
       
   113 @released
       
   114 */	
       
   115 EXPORT_C void TContactViewStore::GetDefaultViewsL(RPointerArray<CContactDefaultViewDefinition> &aDefaultViews)
       
   116 	{
       
   117 	__ASSERT_ALWAYS(aDefaultViews.Count() == 0, User::Leave(KErrArgument));
       
   118 	
       
   119 	CRepository* rep = CRepository::NewLC(KContactsRepositoryUid);
       
   120 	
       
   121 	TInt type(1);
       
   122 	TInt prefs(0);
       
   123 	TInt sortOrderRow;
       
   124 	RContactViewSortOrder viewSortOrder;
       
   125 	CleanupClosePushL(viewSortOrder);
       
   126 	TInt actualLength;
       
   127 	RBuf name;
       
   128 	name.CleanupClosePushL();
       
   129 	RBuf8 pluginName;
       
   130 	pluginName.CleanupClosePushL();	
       
   131 	
       
   132 	// Attempt to store in one transaction; if it fails, pass
       
   133 	// the error on as a leave.
       
   134 	rep->StartTransaction(CRepository::EConcurrentReadWriteTransaction);
       
   135 
       
   136 	TInt row(0);
       
   137 	const TUint32 KMask = ~KColumnMask;
       
   138 	TUint32 partialKey = (KViewDefinitionsTable | (row << 8));
       
   139 	RArray<TUint32> foundKeys;
       
   140 	CleanupClosePushL(foundKeys);
       
   141 	CContactDefaultViewDefinition* viewDef;
       
   142 		
       
   143 	TInt err = rep->FindL(partialKey, KMask, foundKeys);
       
   144 	while (err != KErrNotFound)
       
   145 		{
       
   146 		TInt count = foundKeys.Count();		
       
   147 		for (TInt col = 0; col < count; col++)
       
   148 			{
       
   149 			switch (foundKeys[col] & KColumnMask)
       
   150 				{
       
   151 				case KTypeCol:
       
   152 					{
       
   153 					err = rep->Get(foundKeys[col], type);
       
   154 					break;
       
   155 					}				
       
   156 				case KNameCol:
       
   157 					{
       
   158 					err = rep->Get(foundKeys[col], name, actualLength);
       
   159 					if (err == KErrOverflow)
       
   160 						{
       
   161 						name.ReAllocL(actualLength);
       
   162 						rep->Get(foundKeys[col], name);
       
   163 						}
       
   164 					break;
       
   165 					}
       
   166 				case KSortPluginCol:
       
   167 					{
       
   168 					err = rep->Get(foundKeys[col], pluginName, actualLength);
       
   169 					if (err == KErrOverflow)
       
   170 						{
       
   171 						pluginName.ReAllocL(actualLength);
       
   172 						rep->Get(foundKeys[col], pluginName);
       
   173 						}							
       
   174 					break;
       
   175 					}
       
   176 				case KPreferencesCol:
       
   177 					{
       
   178 					rep->Get(foundKeys[col], prefs);
       
   179 					break;
       
   180 					}
       
   181 				case KSortOrderIdxCol:
       
   182 					{
       
   183 					rep->Get(foundKeys[col], sortOrderRow);
       
   184 					
       
   185 					// read in the sort order from the sort order table
       
   186 					RArray<TUint32> foundFieldKeys;
       
   187 					partialKey = (KViewSortOrderTable | (sortOrderRow << 8));
       
   188 					err = rep->FindL(partialKey, KMask, foundFieldKeys);
       
   189 					CleanupClosePushL(foundFieldKeys);
       
   190 					for (TInt field = 0; field < foundFieldKeys.Count(); field++)
       
   191 						{
       
   192 						TInt fieldUid;
       
   193 						rep->Get(foundFieldKeys[field], fieldUid);
       
   194 						viewSortOrder.AppendL(TUid().Uid(fieldUid));
       
   195 						}
       
   196 					CleanupStack::PopAndDestroy(&foundFieldKeys);
       
   197 					break;
       
   198 					}
       
   199 				default:
       
   200 					{
       
   201 					break;
       
   202 					}
       
   203 				}
       
   204 			}
       
   205 													   
       
   206 		viewDef = CContactDefaultViewDefinition::NewLC(static_cast<CContactDefaultViewDefinition::TViewType>(type),
       
   207 													   name, viewSortOrder, 
       
   208 													   static_cast<TContactViewPreferences>(prefs), 
       
   209 													   pluginName);													   
       
   210 			
       
   211 		aDefaultViews.AppendL(viewDef);
       
   212 		CleanupStack::Pop(viewDef);	
       
   213 		viewSortOrder.Close();
       
   214 
       
   215 		row++;
       
   216 		partialKey = (KViewDefinitionsTable | (row << 8));
       
   217 		err = rep->FindL(partialKey, KMask, foundKeys);
       
   218 		}
       
   219 
       
   220 	TUint32 errorKey;
       
   221 	User::LeaveIfError(rep->CommitTransaction(errorKey));
       
   222 
       
   223 	CleanupStack::PopAndDestroy(5, rep); // foundKeys, pluginName, name, viewSortOrder, rep
       
   224 	}
       
   225 	
       
   226 /**
       
   227   Returns an array of definitions describing the remote views that exist 
       
   228   inside the server for the given contact database.
       
   229   Note: The order of views in the array is not guaranteed to be the order 
       
   230   that they were created in.
       
   231 
       
   232 @param aDbName Name of the contacts database to query for open views.
       
   233 @param aViewDefs An empty array that will be filled with any exisiting
       
   234                  open views for the given contact database.
       
   235 
       
   236 @internalTechnology
       
   237 @test
       
   238 */
       
   239 #if defined(_DEBUG)
       
   240 EXPORT_C void TContactViewStorePrivate::GetDefinitionsOfExistingViewsL(const TDesC& aDbName, RPointerArray<CContactDefaultViewDefinition>& aViewDefs)
       
   241 	{
       
   242 	RCntModel session;
       
   243 	CleanupClosePushL(session);
       
   244 	session.ConnectL();
       
   245 	session.GetDefinitionsOfExistingViewsL(aDbName, aViewDefs);
       
   246 	CleanupStack::PopAndDestroy(); // session
       
   247 	}
       
   248 #else
       
   249 EXPORT_C void TContactViewStorePrivate::GetDefinitionsOfExistingViewsL(const TDesC& , RPointerArray<CContactDefaultViewDefinition>& )
       
   250 	{
       
   251 	}
       
   252 #endif // _DEBUG
       
   253 	
       
   254 /**  Static constructor. Returns a default remote view definition.
       
   255 
       
   256 @param	aViewType	Type of the view. 
       
   257 @param	aViewName	The name of the view. Ignored if aViewType = ERemoteView; 
       
   258 @param 	aSortOrder	Specifies the fields to use to sort the items in the view.
       
   259 @param 	aContactTypes	Specifies which types of contact items should be included in the
       
   260 view and the behaviour for items that do not have content in any of the fields specified in the sort order.
       
   261 @param 	aSortPluginName	Specifies a plug-in that will be used to compare view contacts
       
   262 when the the view is sorted. This name is used by ECOM to select the plugin, and is matched
       
   263 with the "default_data" of all ECOM plugins that support the required interface.
       
   264 @return The newly constructed object.
       
   265 
       
   266 @publishedPartner
       
   267 @released
       
   268 */
       
   269 EXPORT_C  CContactDefaultViewDefinition* CContactDefaultViewDefinition::NewLC(TViewType aViewType, const TDesC &aViewName, const RContactViewSortOrder &aSortOrder, TContactViewPreferences aContactTypes, const TDesC8 &aSortPluginName)
       
   270 	{
       
   271 	CContactDefaultViewDefinition* self = new (ELeave) CContactDefaultViewDefinition(aViewType, aContactTypes);
       
   272 	CleanupStack::PushL(self);
       
   273 	self->ConstructL(aViewName, aSortOrder, aSortPluginName);
       
   274 	return self;	
       
   275 	}
       
   276 
       
   277 /**  Static constructor. Returns a default remote view definition created from the contents
       
   278      of the given stream.
       
   279 
       
   280 @param aStream Stream which the parmameters for the view definition are read from
       
   281 
       
   282 @return The newly constructed object.
       
   283 
       
   284 @publishedPartner
       
   285 @released
       
   286 */
       
   287 EXPORT_C  CContactDefaultViewDefinition* CContactDefaultViewDefinition::NewLC(RReadStream& aStream)
       
   288 	{
       
   289 	CContactDefaultViewDefinition* self = new (ELeave) CContactDefaultViewDefinition;
       
   290 	CleanupStack::PushL(self);
       
   291 	aStream >> *self;
       
   292 	return self;	
       
   293 	}
       
   294 	
       
   295 CContactDefaultViewDefinition::CContactDefaultViewDefinition(TViewType aViewType, TContactViewPreferences aContactTypes):
       
   296 	iContactTypes(aContactTypes),
       
   297 	iViewType(aViewType)
       
   298 	{
       
   299 	}
       
   300 	
       
   301 CContactDefaultViewDefinition::CContactDefaultViewDefinition()
       
   302 	{
       
   303 	}
       
   304 	
       
   305 void CContactDefaultViewDefinition::ConstructL(const TDesC &aViewName, const RContactViewSortOrder &aSortOrder, const TDesC8 &aSortPluginName)
       
   306 	{
       
   307 	iViewName = aViewName.AllocL();
       
   308 	iSortPluginName = aSortPluginName.AllocL();
       
   309 	iSortOrder.CopyL(aSortOrder);
       
   310 	}	
       
   311 
       
   312 /**
       
   313 @publishedPartner
       
   314 @released
       
   315 */
       
   316 EXPORT_C CContactDefaultViewDefinition::~CContactDefaultViewDefinition()
       
   317 	{
       
   318 	Close();
       
   319 	}
       
   320 	
       
   321 void CContactDefaultViewDefinition::Close()
       
   322 	{
       
   323 	iSortOrder.Close();
       
   324 	delete iSortPluginName;
       
   325 	iSortPluginName = NULL;
       
   326 	delete iViewName;	
       
   327 	iViewName = NULL;
       
   328 	}
       
   329 
       
   330 /** Gets the view name.
       
   331 @return The view name.
       
   332 
       
   333 @publishedPartner
       
   334 @released
       
   335 */
       
   336 EXPORT_C const TDesC& CContactDefaultViewDefinition::ViewNameL() const
       
   337 	{ return *iViewName; }
       
   338 
       
   339 /** Gets the sort plugin name.
       
   340 @return The sort plugin name.
       
   341 
       
   342 @publishedPartner
       
   343 @released
       
   344 */
       
   345 EXPORT_C const TDesC8& CContactDefaultViewDefinition::SortPluginNameL() const
       
   346 	{ return *iSortPluginName; }
       
   347 
       
   348 /** Gets the sort order.
       
   349 @return The sort order.
       
   350 
       
   351 @publishedPartner
       
   352 @released
       
   353 */
       
   354 EXPORT_C const RContactViewSortOrder& CContactDefaultViewDefinition::SortOrder() const
       
   355 	{ return iSortOrder; }
       
   356 
       
   357 /** Gets the view preferences.
       
   358 @return The view preferences.
       
   359 
       
   360 @publishedPartner
       
   361 @released
       
   362 */
       
   363 EXPORT_C TContactViewPreferences CContactDefaultViewDefinition::ViewPreferences() const
       
   364 	{ return iContactTypes; }
       
   365 
       
   366 /** Gets the view type.
       
   367 @return The view type.
       
   368 
       
   369 @publishedPartner
       
   370 @released
       
   371 */
       
   372 EXPORT_C CContactDefaultViewDefinition::TViewType CContactDefaultViewDefinition::ViewType() const
       
   373 	{ return iViewType; }
       
   374 
       
   375 /** Standard object internalization function.
       
   376 @param aStream Stream from which the object is internalized.
       
   377 
       
   378 @publishedPartner
       
   379 @released
       
   380 */
       
   381 EXPORT_C void CContactDefaultViewDefinition::InternalizeL(RReadStream &aStream)
       
   382 	{
       
   383 	// free any existing data
       
   384 	Close();
       
   385 	
       
   386 	// read in the new data
       
   387 	const TInt KMaxDescriptorLength(KMaxTInt);
       
   388 	iViewName = HBufC::NewL(aStream, KMaxDescriptorLength);
       
   389 	iSortPluginName = HBufC8::NewL(aStream, KMaxDescriptorLength);
       
   390 	aStream >> iSortOrder;
       
   391 	iContactTypes = static_cast<TContactViewPreferences>(aStream.ReadUint32L());
       
   392 	iViewType = static_cast<TViewType>(aStream.ReadUint32L());
       
   393 	}
       
   394 
       
   395 /** Standard object externalization function.
       
   396 @param aStream Stream that the object is externalized to.
       
   397 
       
   398 @publishedPartner
       
   399 @released
       
   400 */
       
   401 EXPORT_C void CContactDefaultViewDefinition::ExternalizeL(RWriteStream &aStream) const
       
   402 	{
       
   403 	aStream << *iViewName;
       
   404 	aStream << *iSortPluginName;
       
   405 	aStream << iSortOrder;
       
   406 	aStream.WriteUint32L(static_cast<TUint32>(iContactTypes));
       
   407 	aStream.WriteUint32L(static_cast<TUint32>(iViewType));
       
   408 	}