|
1 /* |
|
2 * Copyright (c) 2002-2007 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: The virtual phonebook find operation |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 #include "CFindOperation.h" |
|
21 #include "CContactLink.h" |
|
22 #include "CContactStore.h" |
|
23 #include "CFieldFactory.h" |
|
24 #include <cntfield.h> |
|
25 |
|
26 #include <MVPbkFieldType.h> |
|
27 #include <MVPbkContactFindObserver.h> |
|
28 |
|
29 namespace VPbkCntModel { |
|
30 |
|
31 CFindOperation::CFindOperation( |
|
32 CContactStore& aContactStore, |
|
33 MVPbkContactFindObserver& aObserver ) : |
|
34 CActive( CActive::EPriorityIdle ), |
|
35 iContactStore( aContactStore), |
|
36 iObserver( aObserver ) |
|
37 { |
|
38 CActiveScheduler::Add( this ); |
|
39 } |
|
40 |
|
41 inline void CFindOperation::ConstructL( |
|
42 const TDesC& aSearchString, |
|
43 const MVPbkFieldTypeList& aFieldTypes ) |
|
44 { |
|
45 iSearchString = aSearchString.AllocL(); |
|
46 iFieldDef = new ( ELeave ) CContactItemFieldDef; |
|
47 |
|
48 const TInt fieldCount = aFieldTypes.FieldTypeCount(); |
|
49 iFieldDef->SetReserveL( fieldCount ); |
|
50 for ( TInt i = 0; i < fieldCount; ++i ) |
|
51 { |
|
52 const CContactItemField* field = |
|
53 iContactStore.FieldFactory().FindField( aFieldTypes.FieldTypeAt(i) ); |
|
54 if ( field ) |
|
55 { |
|
56 iFieldDef->AppendL( field->ContentType().FieldType(0) ); |
|
57 } |
|
58 } |
|
59 iResults = CVPbkContactLinkArray::NewL(); |
|
60 } |
|
61 |
|
62 CFindOperation* CFindOperation::NewL( |
|
63 CContactStore& aContactStore, |
|
64 const TDesC& aSearchString, |
|
65 const MVPbkFieldTypeList& aFieldTypes, |
|
66 MVPbkContactFindObserver& aObserver ) |
|
67 { |
|
68 CFindOperation* self = new ( ELeave ) CFindOperation( |
|
69 aContactStore, aObserver ); |
|
70 CleanupStack::PushL( self ); |
|
71 self->ConstructL( aSearchString, aFieldTypes ); |
|
72 CleanupStack::Pop( self ); |
|
73 return self; |
|
74 } |
|
75 |
|
76 CFindOperation::~CFindOperation() |
|
77 { |
|
78 Cancel(); |
|
79 |
|
80 delete iSearchString; |
|
81 delete iResults; |
|
82 delete iAsyncFinder; |
|
83 delete iContactIds; |
|
84 delete iFieldDef; |
|
85 } |
|
86 |
|
87 void CFindOperation::IssueRequest() |
|
88 { |
|
89 TRequestStatus* status = &iStatus; |
|
90 User::RequestComplete( status, KErrNone ); |
|
91 SetActive(); |
|
92 } |
|
93 |
|
94 void CFindOperation::DoCancel() |
|
95 { |
|
96 iState = ECancelled; |
|
97 if ( !IsActive() ) |
|
98 { |
|
99 IssueRequest(); |
|
100 } |
|
101 } |
|
102 |
|
103 void CFindOperation::RunL() |
|
104 { |
|
105 switch ( iState ) |
|
106 { |
|
107 case EBuildLinks: |
|
108 { |
|
109 if ( iContactIds->Count() > 0 ) |
|
110 { |
|
111 CContactLink* link = CContactLink::NewLC( iContactStore, |
|
112 (*iContactIds)[0] ); |
|
113 iContactIds->Remove( 0 ); |
|
114 iResults->AppendL( link ); |
|
115 CleanupStack::Pop( link ); |
|
116 IssueRequest(); |
|
117 } |
|
118 else |
|
119 { |
|
120 delete iContactIds; |
|
121 iContactIds = NULL; |
|
122 iState = EComplete; |
|
123 IssueRequest(); |
|
124 } |
|
125 break; |
|
126 } |
|
127 case EComplete: |
|
128 { |
|
129 // gives ownership to observer |
|
130 CVPbkContactLinkArray* results = iResults; |
|
131 iResults = NULL; |
|
132 iObserver.FindCompleteL( results ); |
|
133 break; |
|
134 } |
|
135 case ECancelled: |
|
136 { |
|
137 iObserver.FindFailed( KErrCancel ); |
|
138 break; |
|
139 } |
|
140 } |
|
141 |
|
142 } |
|
143 |
|
144 TInt CFindOperation::RunError( TInt aError ) |
|
145 { |
|
146 iObserver.FindFailed( aError ); |
|
147 return KErrNone; |
|
148 } |
|
149 |
|
150 void CFindOperation::StartL() |
|
151 { |
|
152 iAsyncFinder = iContactStore.NativeDatabase().FindAsyncL( |
|
153 *iSearchString, iFieldDef, this ); |
|
154 } |
|
155 |
|
156 void CFindOperation::IdleFindCallback() |
|
157 { |
|
158 if ( iAsyncFinder->IsComplete() ) |
|
159 { |
|
160 iState = EBuildLinks; |
|
161 iContactIds = iAsyncFinder->TakeContactIds(); |
|
162 IssueRequest(); |
|
163 } |
|
164 } |
|
165 |
|
166 void CFindOperation::Cancel() |
|
167 { |
|
168 CActive::Cancel(); |
|
169 } |
|
170 |
|
171 } // namespace VPbkCntModel |