2 * Copyright (c) 2002 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
15 * Represents a connection to the Phonebook contact database
20 #ifndef __CPBKCONTACTENGINE_H__
21 #define __CPBKCONTACTENGINE_H__
24 #include <e32base.h> // CBase
25 #include <cntdef.h> // TContactItemId
26 #include <cntdbobs.h> // MContactDbObserver
27 #include <f32file.h> // RFs
28 #include "PbkFields.hrh" // TPbkFieldId
31 // FORWARD DECLARATIONS
32 class CContactDatabase;
33 class MIdleFindObserver;
34 class MPbkContactDbObserver;
37 class CPbkContactItem;
38 class CPbkContactIter;
39 class CPbkContactChangeNotifier;
43 class MPbkContactNameFormat;
44 class MPbkFieldDataArray;
45 class CContactViewBase;
47 class RSharedDataClient;
48 class TResourceReader;
49 class CPbkEngineExtension;
50 class CPbkSortOrderManager;
51 class CPbkSINDHandlerInterface;
57 * The main entrypoint to the Phonebook contact engine. The Phonebook
58 * contact engine builds on top of the Symbian Contacts model and
59 * implements platform specific functionality and policies that can be
60 * reused and followed by clients to implement functionality that is
61 * better integrated to the platform functionality. Functionality like
62 * contact field typing is implemented by this engine.
64 class CPbkContactEngine :
66 private MContactDbObserver
68 public: // Constructors and destructor
70 * Creates a new Phonebook engine object and connects to the default
71 * contact database. If the default database does not exist it is
74 * @param aFs An open file server connection. If !=NULL aFs is used
75 * instead of a locally created RFs connection. aFs must
76 * remain connected for the lifetime of the returned
78 * @return A new instance of this class.
79 * @exception KErrCorrupt if the database is corrupted.
81 IMPORT_C static CPbkContactEngine* NewL(RFs* aFs=NULL);
86 * Creates a new engine object and connects it to a specified
87 * database file. If the database file does not exist it is created.
89 * @param aFileName The database filename.
90 * @param aReplace If ETrue replaces exisiting file with an empty one.
91 * PLEASE NOTE: all data in the existing file
93 * @param aFs An open file server connection. If !=NULL aFs is used
94 * instead of a locally created RFs connection. aFs must
95 * remain connected for the lifetime of the returned
97 * @return A new instance of this class.
98 * @exception KErrCorrupt if the database is corrupted.
101 IMPORT_C static CPbkContactEngine* NewL
102 (const TDesC& aFileName, TBool aReplace=EFalse, RFs* aFs=NULL);
105 * Replaces the default contact database and connects to it. See
106 * Symbian Contacs model documentation for CContactDatabase::ReplaceL
107 * for possible leave codes.
108 * PLEASE NOTE: all data in the existing database will be lost!
110 * @param aFs An open file server connection. If !=NULL aFs is used
111 * instead of a locally created RFs connection. aFs must
112 * remain connected for the lifetime of the returned
114 * @return A new instance of this class.
115 * @see CContactDatabase::ReplaceL
117 IMPORT_C static CPbkContactEngine* ReplaceL(RFs* aFs=NULL);
120 * Destructor. Destroys this object and closes the contact database
121 * connection. REComSession::FinalClose() is called. Notice that
122 * FinalClose -call releases resource handles of already destroyed ECom
123 * plugins also outside CPbkContactEngine. See REComSession
126 ~CPbkContactEngine();
130 * Returns the global Phonebook engine instance, NULL if no instance
132 * Note1: Uses thread local storage (TLS), which is slow. Cache the
133 * returned pointer if it is used more than one time!
134 * Note2: Only the first engine instance created in calling thread can
135 * be accessed with this function.
136 * @return Global Phonebook engine instance.
138 IMPORT_C static CPbkContactEngine* Static();
141 * Returns the underlying CContactDatabase object. Use only if this class's API
142 * is not enough for your purposes.
144 * @return The underlying Symbian Contacts model CContactDatabase instance.
145 * @see CContactDatabase
147 IMPORT_C CContactDatabase& Database();
150 * Returns the field type info array.
151 * @return All the Phonebook field types.
152 * @see CPbkFieldsInfo
154 IMPORT_C const CPbkFieldsInfo& FieldsInfo();
157 * Returns an open file server session.
158 * @return A handle to a file server session.
160 IMPORT_C RFs& FsSession() const;
162 public: // Creating contacts
164 * Returns a new contact card with default fields.
165 * @return A new empty contact item object.
167 IMPORT_C CPbkContactItem* CreateEmptyContactL();
170 * Adds a new contact to the contact database.
172 * @param aContact The new contact item to add.
173 * @param aImmediateNotify Send notification to observers immediately.
174 * NOTE: send immediately will result in
175 * observers (MPbkContactDbObserver) receiving
177 * @return The id of the new contact item.
178 * @see CContactDatabase::AddNewContactL
180 IMPORT_C TContactItemId AddNewContactL
181 (CPbkContactItem& aContact, TBool aImmediateNotify=EFalse);
184 * Duplicates a contact in the database.
186 * @param aId Id of the contact to duplicate.
187 * @param aImmediateNotify Send notification to observers immediately.
188 * NOTE: send immediately will result in
189 * observers (MPbkContactDbObserver) receiving
191 * @return Contact id of the new duplicate.
193 IMPORT_C TContactItemId DuplicateContactL
194 (TContactItemId aId, TBool aImmediateNotify=EFalse);
196 public: // Reading contacts
198 * Reads a contact and returns a phonebook contact item.
200 * @param aContactId Contact item id to be read.
201 * @param aFieldTypes Array of types of fields to read in, all
202 * fields are read if NULL (which is default).
203 * NOTE: when using this parameter ReadContactL
204 * may return more fields than expected; for example
205 * if aFieldTypes contains any phonenumber the
206 * method may return all phone number fields
208 * @return A new Phonebook contact item.
209 * @see CContactDatabase::ReadContactL
211 IMPORT_C CPbkContactItem* ReadContactL
212 (TContactItemId aContactId, const CPbkFieldIdArray* aFieldTypes=NULL);
215 * Same as ReadContactL, but leaves the returned contact item
216 * on the cleanup stack.
218 * @param aContactId Contact item id to be read.
219 * @param aFieldTypes Array of types of fields to read in, all
220 * fields are read if NULL (which is default).
221 * NOTE: when using this parameter ReadContactL
222 * may return more fields than expected; for example
223 * if aFieldTypes contains any phonenumber the
224 * method may return all phone number fields
226 * @return A new Phonebook contact item.
228 IMPORT_C CPbkContactItem* ReadContactLC
229 (TContactItemId aContactId, const CPbkFieldIdArray* aFieldTypes=NULL);
232 * Same as ReadContactLC but reads only minimal information. See
233 * Symbian Contacts Model documentation for definition of "minimal" in this
236 * @param aContactId Contact item id to be read.
237 * @return A new phonebook contact item. Leaves the item on the cleanup stack.
238 * @see CContactDatabase::ReadMinimalContactL
240 IMPORT_C CPbkContactItem* ReadMinimalContactLC(TContactItemId aContactId);
243 * Returns a Phonebook contact iterator.
244 * @param aUseMinimalRead If ETrue the iterator will use the Symbian
245 * Contacts model CContactDatabase::ReadMinimalContactL, EFalse will read
246 * all the contact fields. Default behaviour is to read all fields.
247 * @see CPbkContactIter::NewL.
248 * @see CContactDatabase::ReadMinimalContactL
250 IMPORT_C CPbkContactIter* CreateContactIteratorLC
251 (TBool aUseMinimalRead=EFalse);
253 public: // Modifying contacts
255 * Opens a contact and returns a phonebook contact item.
256 * @param aContactId Contact item id to be opened.
257 * @return A Phonebook contact item that is marked as open in the database.
258 * @see CContactDatabase::OpenContactL
260 IMPORT_C CPbkContactItem* OpenContactL(TContactItemId aContactId);
263 * Same as OpenContactL, but leaves a lock record AND the opened
264 * contact object on the cleanup stack.
265 * Use CleanupStack::PopAndDestroy(2) to close the contact and destroy
266 * the returned object. First pop pops the contact object and second
268 * @param aContactId Contact item id to open.
269 * @return A Phonebook contact item that is marked as open in the database.
270 * @see CContactDatabase::OpenContactLX
272 IMPORT_C CPbkContactItem* OpenContactLCX(TContactItemId aContactId);
275 * Commit changes to a previously opened contact item into the contact
278 * @param aContact Contact item to be updated in the database.
279 * @param aImmediateNotify Send notification to observers immediately.
280 * NOTE: send immediately will result in
281 * observers (MPbkContactDbObserver) receiving
283 * @see CContactDatabase::CommitContactL
285 IMPORT_C void CommitContactL
286 (CPbkContactItem& aContact, TBool aImmediateNotify=EFalse);
289 * Closes a previously opened contact item without saving changes.
290 * @param aContactId Contact item to be closed.
291 * @see CContactDatabase::CloseContactL
293 IMPORT_C void CloseContactL(TContactItemId aContactId);
295 public: // Deleting contacts
297 * Deletes a contact item from the database.
298 * @param aContactId Contact item to be deleted.
299 * @param aImmediateNotify Send notification to observers immediately.
300 * NOTE: send immediately will result in
301 * observers (MPbkContactDbObserver) receiving
303 * @see CContactDatabase::DeleteContactL
305 IMPORT_C void DeleteContactL
306 (TContactItemId aContactId, TBool aImmediateNotify=EFalse);
309 * Deletes multiple contact items from the database.
310 * @param aContactIds Contact items to be deleted.
311 * @param aImmediateNotify Send notification to observers immediately.
312 * NOTE: send immediately will result in
313 * observers (MPbkContactDbObserver) receiving
315 * @see CContactDatabase::DeleteContactsL
317 IMPORT_C void DeleteContactsL
318 (const CContactIdArray& aContactIds, TBool aImmediateNotify=EFalse);
321 * Deletes multiple contacts from the database in an active object
322 * -driven asynchronous background process.
324 * @param aContactIds Contacts to delete.
326 IMPORT_C void DeleteContactsOnBackgroundL
327 (const CContactIdArray& aContactIds);
329 public: // Contact groups
331 * Creates a new contact group.
333 * @param aGroupLabel Group name.
334 * @param aInTransaction See Symbian Contacts Model documentation
335 * for CContactDatabase::CreateContactGroupL documentation.
336 * @see CContactDatabase::CreateContactGroupL
338 IMPORT_C CContactGroup* CreateContactGroupL
339 (const TDesC& aGroupLabel,TBool aInTransaction=EFalse);
342 * Adds a Contact to a group.
344 * @param aItemId Contact to be added to group.
345 * @param aGroupId Group where the contact will be added to.
346 * @see CContactDatabase::AddContactToGroupL
348 IMPORT_C void AddContactToGroupL
349 (TContactItemId aItemId, TContactItemId aGroupId);
352 * Removes a contact from a group
354 * @param aItemId Contact to be removed from group.
355 * @param aGroupId Group where the contact will be removed from.
356 * @see CContactDatabase::RemoveContactFromGroupL
358 IMPORT_C void RemoveContactFromGroupL
359 (TContactItemId aItemId, TContactItemId aGroupId);
362 * Reads a Contact group.
364 * @param aId Group id.
365 * @return Contact group object.
366 * @see CContactDatabase::ReadContactL
368 IMPORT_C CContactGroup* ReadContactGroupL(TContactItemId aId);
371 * Opens a Contact group for modification.
373 * @param aId Groups id
374 * @return Contact group object.
375 * @see CContactDatabase::OpenContactL
377 IMPORT_C CContactGroup* OpenContactGroupL(TContactItemId aId);
380 * Opens a Contact group for modification. Pushes the returned
381 * contact group object and a lock item on the cleanup stack.
383 * @param aId Groups id
384 * @return Contact group object.
385 * @see CContactDatabase::OpenContactLX
387 IMPORT_C CContactGroup* OpenContactGroupLCX(TContactItemId aId);
390 * Commits changes to a contact group to the database.
392 * @param aId Groups id
393 * @param aImmediateNotify Send notification to observers immediately.
394 * NOTE: send immediately will result in
395 * observers (MPbkContactDbObserver) receiving
397 * @see CContactDatabase::CommitContactL
399 IMPORT_C void CommitContactGroupL(CContactGroup& aGroup, TBool aImmediateNotify=EFalse);
402 * Deletes a contact group from the database.
403 * @param aContactId Contact group to be deleted.
404 * @param aImmediateNotify Send notification to observers immediately.
405 * NOTE: send immediately will result in
406 * observers (MPbkContactDbObserver) receiving
408 * @see CContactDatabase::DeleteContactL
410 IMPORT_C void DeleteContactGroupL
411 (TContactItemId aContactId, TBool aImmediateNotify=EFalse);
413 public: // Speed dials
415 * Sets a speed dial to a contact field.
417 * @param aItem Contact item to add speed dial to.
418 * @param aFieldIndex Field index to add Speed dial to.
419 * @param aSpeedDialPosition Speed dial position number to set to
421 * @see CContactDatabase::SetFieldAsSpeedDialL
423 IMPORT_C void SetFieldAsSpeedDialL
424 (CPbkContactItem& aItem, TInt aFieldIndex, TInt aSpeedDialPosition);
427 * Returns a speed dial contact.
429 * @param aSpeedDialPosition Speed dial position number.
430 * @param aPhoneNumber Phone number
431 * @see CContactDatabase::GetSpeedDialFieldL.
433 IMPORT_C TContactItemId GetSpeedDialFieldL
434 (TInt aSpeedDialPosition, TDes& aPhoneNumber) const;
437 * Removes a speed dial from a contact.
439 * @param aContactId Contact item to remove the speed dial from.
440 * @param aSpeedDialPosition Speed dial position number to remove.
441 * @see CContactDatabase::RemoveSpeedDialFieldL.
443 IMPORT_C void RemoveSpeedDialFieldL
444 (TContactItemId aContactId, TInt aSpeedDialPosition);
447 * Returns ETrue if this field has been assigned a speed dial position.
449 * @param aItem contact Item to check for speed dial.
450 * @param aFieldIndex Index of the field to check. This is an index
451 * into aItem.CardFields().
452 * @return ETrue if Speed dial exitst, EFalse if not.
454 IMPORT_C TBool IsSpeedDialAssigned
455 (const CPbkContactItem& aItem, TInt aFieldIndex) const;
457 public: // Contact views
459 * Returns a contact view object containing all the contacts in the
462 * @return Contact view containing all the contacts in the database.
463 * @see CContactViewBase
465 IMPORT_C CContactViewBase& AllContactsView();
468 * Returns a contact view object containing all the groups in the
471 * @return Contact view containing all the groups in the database.
472 * @see CContactViewBase
474 IMPORT_C CContactViewBase& AllGroupsViewL();
477 * Returns a contact view object containing all the contacts in the
478 * database that match a filter.
480 * @param aFilter Use CContactDatabase::TContactViewFilter.
481 * @see CContactViewBase
483 IMPORT_C CContactViewBase& FilteredContactsViewL(TInt aFilter);
487 * Creates and returns a new CPbkContactChangeNotifier for the
488 * default contact database. The returned object attaches aObserver
489 * to this engine instance as long as the object exists.
491 * @param aObserver Observer to attach to this object.
492 * @return New CPbkContactChangeNotifier object. Caller is responsible
494 * @see CPbkContactChangeNotifier
495 * @see MPbkContactDbObserver
497 IMPORT_C CPbkContactChangeNotifier* CreateContactChangeNotifierL
498 (MPbkContactDbObserver* aObserver);
500 public: // Contact name formatting
502 * Gets a title text for a contact or localised text for unnamed
503 * contact if contact contains no title.
505 * @param aItem Contact item for which to make the title.
506 * @return A buffer containing the title or unnamed text if no title
507 * can be generated. Caller is responsible of deleting the
510 IMPORT_C HBufC* GetContactTitleL(const CPbkContactItem& aItem) const;
513 * Similar to GetContactTitleL but returns NULL if contact contains no
514 * title. Uses the MPbkFieldDataArray interface to access the field
516 * @param aContactData The contact field data array.
517 * @return Contact title, NULL if field array did not contain any fields
518 * used to for constructing contact titles.
519 * @see CPbkContactEngine::GetContactTitleL
521 IMPORT_C HBufC* GetContactTitleOrNullL
522 (const MPbkFieldDataArray& aContactData);
525 * Returns ETrue if field is one of the fields used in building the
528 * @param aFieldId The id of the field.
529 * @return ETrue if aFieldId is type of a field used to build contact
531 * @see GetContactTitleL
532 * @see GetContactTitleOrNullL
534 IMPORT_C TBool IsTitleField(TPbkFieldId aFieldId) const;
538 * Returns the contact name formatter.
540 * @see MPbkContactNameFormat
543 IMPORT_C MPbkContactNameFormat& ContactNameFormat() const;
546 * Returns the localised title text to use for unnamed contacts.
547 * @return Localised title text to use for unnamed contacts.
549 IMPORT_C const TDesC& UnnamedTitle() const;
553 * Call-through for new Phone number matching function in 6.2 version
554 * of class CContactDatabase. If you don't need any other functionality
555 * from CPbkContactEngine than this consider using the CContactDatabase
556 * API directly. See Symbian Contacts Model documentation for
557 * CContactDatabase::MatchPhoneNumberL use.
559 * @see CContactDatabase::MatchPhoneNumberL(const TDesC&,const TInt)
561 IMPORT_C CContactIdArray* MatchPhoneNumberL
562 (const TDesC& aNumber, const TInt aMatchLengthFromRight);
565 * Searches all contacts in the database for aText.
567 * @param aText Text to search.
568 * @param aFieldTypes Phonebook fields types to <i>at least</i>
569 * include in the search. If NULL searches all
571 * <b>PLEASE NOTE:</b> The find matches in most
572 * cases also other fields than those specified
573 * in aFieldTypes. Always loop through the
574 * returned contacts to check match in the
576 * @return Array of matching contact IDs.
577 * @see CContactDatabase::FindLC.
579 IMPORT_C CContactIdArray* FindLC
580 (const TDesC& aText, const CPbkFieldIdArray* aFieldTypes=NULL);
583 * Searches all contacts in the database for aText asynchronously.
585 * @param aText Text to search.
586 * @param aFieldTypes Phonebook fields types to <i>at least</i>
587 * include in the search. If NULL searches all
589 * <b>PLEASE NOTE:</b> The find matches in most
590 * cases also other fields than those specified
591 * in aFieldTypes. Always loop through the
592 * returned contacts to check match in the
594 * @param aObserver Observer for this operation.
595 * @return CPbkIdleFinder object which is used to access the find
596 * results when complete. Deleting this object will cancel
597 * this asyncronous find operation.
598 * @see CContactDatabase::FindAsyncL.
599 * @see CPbkIdleFinder
601 IMPORT_C CPbkIdleFinder* FindAsyncL(
603 const CPbkFieldIdArray* aFieldTypes=NULL,
604 MIdleFindObserver *aObserver=NULL);
606 public: // Phonebook internal API
609 * Sets the compression UI for this engine.
610 * To be called by Phonebook only!
612 * @see MPbkCompressUi
614 * @see CancelCompress
617 IMPORT_C void SetCompressUi(MPbkCompressUi* aCompressiUi);
621 * Checks if the contact database needs a compression.
622 * To be called by Phonebook only!
626 * @see CancelCompress
629 IMPORT_C TBool CheckCompress();
633 * Compresses the database if necessary. To be called by Phonebook only.
636 * @see CancelCompress
639 IMPORT_C void CompressL();
643 * Call to cancel any ongoing database compression started with
644 * CheckCompressL(). Calls PbkCompressCanceled() for any registered
645 * compression UI. To be called by Phonebook only.
651 IMPORT_C void CancelCompress();
655 * Checks current file system space and amount of wasted space in
656 * Phonebook's database. Compresses the database synchronously if
657 * Phonebook's database can be made smaller by compression. If file
658 * system space is still under critical level after compression,
659 * leaves with KErrDiskFull.
661 * @exception KErrDiskFull if file system is out of space.
664 IMPORT_C void CheckFileSystemSpaceAndCompressL();
668 * Phonebook name ordering information.
673 EPbkNameOrderLastNameFirstName = 0, /// Last name then first name order
674 EPbkNameOrderFirstNameLastName, /// First name then last name order
675 EPbkNameOrderNotDefined /// undefined name order
680 * Sets name display order.
681 * @param aNameOrder The ordering to use
684 IMPORT_C void SetNameDisplayOrderL(TPbkNameOrder aNameOrder);
688 * Gets name display order.
689 * @return The name ordering in use
692 IMPORT_C TPbkNameOrder NameDisplayOrderL();
696 * Returns the phonebook constants object.
697 * @return Phonebook constants manager
700 IMPORT_C CPbkConstants* Constants();
704 * Returns the sort order manager of the engine.
706 const CPbkSortOrderManager& SortOrderManager() const;
711 * Sets whether a separator character is shown between
712 * last and first names.
713 * @param aSeparator This parameter should contain the character to be
714 * used as separator. If value is 0 then separator will not be
715 * used between names.
717 IMPORT_C void SetNameSeparatorL(TChar aSeparator);
721 * Return info on whether separator character is used between last
723 * @return The separator character. Empty (value 0) if there is no
726 IMPORT_C TChar NameSeparator() const;
730 private: // from MContactDbObserver
731 void HandleDatabaseEventL(TContactDbObserverEvent aEvent);
733 private: // Interface for CPbkContactChangeNotifier
734 friend class CPbkContactChangeNotifier;
735 void AddObserverL(MPbkContactDbObserver* aObserver);
736 void RemoveObserver(MPbkContactDbObserver* aObserver);
737 void SendEventToAllObservers(const TContactDbObserverEvent& aEvent);
739 private: // Implementation
741 void ConstructL(const TDesC* aFileName, TBool aReplace, RFs* aFs);
742 void ConnectFsL(RFs* aRfs);
743 void ReadResourcesL(TBool& aSettingsVisibility);
744 void CreateDbConnectionL(const TDesC* aFileName,
747 TBool aSettingsVisible);
748 void SendImmidiateEventToAllObservers(
749 TContactDbObserverEventType aEventType,
750 TContactItemId aContactId, TBool aSendEvent);
751 void doDeleteContactL(TContactItemId aContactId);
752 class CContactDbConnection;
753 friend class CContactDbConnection;
754 friend class CPbkSharedDataObserver;
757 /// Ref: file server connection
759 /// Own: file server connection
761 /// Own: Contact database connection object.
762 CContactDbConnection* iDbConnection;
763 /// Own: Observer array
764 CArrayPtr<MPbkContactDbObserver>* iObservers;
765 /// Own: fields info array.
766 CPbkFieldsInfo* iPbkFieldsInfo;
767 /// Own: Phonebook constants
768 CPbkConstants* iPbkConstants;
769 /// Own: shared data client
770 RSharedDataClient* iSharedDataClient;
771 /// Own: maximum free space required to delete a contact from the DB
772 TInt iFreeSpaceRequiredToDelete;
773 /// Own: Engine extension interface
774 CPbkEngineExtension* iExtension;
775 /// Is separator used between first- and last-names of contacts
777 /// The separator to be used between first- and last-names of contacts
779 /// CPbkSINDHandler instance identifier key.
781 /// Own: SIND Handler
782 CPbkSINDHandlerInterface* iSINDHandler;
788 #endif // __CPBKCONTACTENGINE_H__