|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the QtCore module of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "cntdisplaylabel.h" |
|
43 #include <qtcontacts.h> |
|
44 |
|
45 /*! |
|
46 * Constructor |
|
47 */ |
|
48 |
|
49 CntDisplayLabel::CntDisplayLabel() : |
|
50 m_nameOrder(CntOrderFirstLast) |
|
51 { |
|
52 #ifdef SYMBIAN_BACKEND_USE_SQLITE |
|
53 m_settings = new CntCenrep(KCntNameOrdering, *this); |
|
54 m_nameOrder = m_settings->getValue(); |
|
55 #endif |
|
56 setDisplayLabelDetails(); |
|
57 } |
|
58 |
|
59 /*! |
|
60 * Destructor |
|
61 */ |
|
62 CntDisplayLabel::~CntDisplayLabel() |
|
63 { |
|
64 #ifdef SYMBIAN_BACKEND_USE_SQLITE |
|
65 delete m_settings; |
|
66 #endif |
|
67 } |
|
68 |
|
69 /*! |
|
70 * Sets the fields that should be used for displaylabel |
|
71 * |
|
72 * The order is defined in the QList of preferred options |
|
73 * Whereas the QPair includes the fields that should be used for |
|
74 * the specific detail |
|
75 */ |
|
76 void CntDisplayLabel::setDisplayLabelDetails() |
|
77 { |
|
78 //This will be variated in the future |
|
79 //adds the contact's details definition names + fields to be used for display name and filtering display label |
|
80 |
|
81 //Display label details |
|
82 //Contact |
|
83 //Preferred details |
|
84 m_contactDisplayLabelDetails.clear(); |
|
85 m_groupDisplayLabelDetails.clear(); |
|
86 QList<QPair<QLatin1String, QLatin1String> > contactPrefferedDisplayLabelDetails; |
|
87 QLatin1String firstLatin(QContactName::FieldFirstName); |
|
88 QLatin1String secondLatin(QContactName::FieldLastName); |
|
89 |
|
90 #ifdef SYMBIAN_BACKEND_USE_SQLITE |
|
91 if (m_nameOrder == CntOrderLastFirst || m_nameOrder == CntOrderLastCommaFirst) { |
|
92 firstLatin = QLatin1String(QContactName::FieldLastName); |
|
93 secondLatin = QLatin1String(QContactName::FieldFirstName); |
|
94 } |
|
95 #endif |
|
96 |
|
97 contactPrefferedDisplayLabelDetails.append(qMakePair(QLatin1String(QContactName::DefinitionName), firstLatin)); |
|
98 contactPrefferedDisplayLabelDetails.append(qMakePair(QLatin1String(QContactName::DefinitionName), secondLatin)); |
|
99 m_contactDisplayLabelDetails.append(contactPrefferedDisplayLabelDetails); |
|
100 |
|
101 //if preferred details doesn't exist use these |
|
102 QList<QPair<QLatin1String, QLatin1String> > contactSecondaryDisplayLabelDetails; |
|
103 contactSecondaryDisplayLabelDetails.append(qMakePair(QLatin1String(QContactOrganization::DefinitionName), QLatin1String(QContactOrganization::FieldName))); |
|
104 m_contactDisplayLabelDetails.append(contactSecondaryDisplayLabelDetails); |
|
105 |
|
106 //Group |
|
107 QList<QPair<QLatin1String, QLatin1String> > preferredGroupDisplayLabelDetails; |
|
108 preferredGroupDisplayLabelDetails.append(qMakePair(QLatin1String(QContactName::DefinitionName), QLatin1String(QContactName::FieldCustomLabel))); |
|
109 m_groupDisplayLabelDetails.append(preferredGroupDisplayLabelDetails); |
|
110 } |
|
111 |
|
112 /*! |
|
113 * Returns a synthesised display label of a contact |
|
114 * \a contact to read the data from . |
|
115 * \a error On return, contains the possible error. |
|
116 * \return synthesised display label |
|
117 */ |
|
118 QString CntDisplayLabel::synthesizedDisplayLabel(const QContact& contact, QContactManager::Error* error) |
|
119 { |
|
120 QString displayLabel; |
|
121 *error = QContactManager::NoError; |
|
122 |
|
123 #ifdef SYMBIAN_BACKEND_USE_SQLITE |
|
124 int value = m_settings->getValue(); |
|
125 if (value != -1 && value != m_nameOrder) { |
|
126 m_nameOrder = value; |
|
127 setDisplayLabelDetails(); |
|
128 } |
|
129 #endif |
|
130 //contact |
|
131 if(contact.type() == QContactType::TypeContact) { |
|
132 displayLabel = generateDisplayLabel(contact, m_contactDisplayLabelDetails); |
|
133 } |
|
134 |
|
135 //group |
|
136 else if (contact.type() == QContactType::TypeGroup) { |
|
137 displayLabel = generateDisplayLabel(contact, m_groupDisplayLabelDetails); |
|
138 } |
|
139 |
|
140 //invalid type |
|
141 else { |
|
142 *error = QContactManager::InvalidContactTypeError; |
|
143 } |
|
144 |
|
145 return displayLabel; |
|
146 } |
|
147 |
|
148 /*! |
|
149 * Generate the display label |
|
150 * \a contact to read the data from . |
|
151 * \a detailList contains the details to be read from the contact |
|
152 * \return synthesised display label |
|
153 */ |
|
154 QString CntDisplayLabel::generateDisplayLabel( const QContact &contact, const QList<QList<QPair<QLatin1String, QLatin1String> > > detailList) const |
|
155 { |
|
156 // Default to empty display label. It is up to the client to create a |
|
157 // localised presentation of a contact without a name. |
|
158 QString displayLabel(""); |
|
159 |
|
160 //loop through the details and create display label |
|
161 for(int i = 0; i < detailList.count() && displayLabel.isEmpty(); i++ ) |
|
162 { |
|
163 QList<QPair<QLatin1String, QLatin1String> > detailPairList = detailList.at(i); |
|
164 QContactDetail contactDetail; |
|
165 |
|
166 for(int j = 0; j < detailPairList.count(); j++) |
|
167 { |
|
168 contactDetail = contact.detail(detailPairList.at(j).first); |
|
169 |
|
170 if(displayLabel.isEmpty()){ //read the value and set it as display label |
|
171 displayLabel = contactDetail.value(detailPairList.at(j).second); |
|
172 } |
|
173 else{ //read the value and append it to the display label |
|
174 QString label = contactDetail.value(detailPairList.at(j).second); |
|
175 |
|
176 if(!label.isEmpty()) |
|
177 { |
|
178 // Inlcude a comma if needed in the display label |
|
179 if (m_nameOrder == CntOrderLastCommaFirst) |
|
180 displayLabel.append(comma()); |
|
181 displayLabel.append(delimiter()); |
|
182 displayLabel.append(label); |
|
183 } |
|
184 } |
|
185 } |
|
186 } |
|
187 |
|
188 return displayLabel; |
|
189 } |
|
190 |
|
191 /*! |
|
192 * delemiter to be used in display label |
|
193 * \return delemiter |
|
194 */ |
|
195 QString CntDisplayLabel::delimiter() const |
|
196 { |
|
197 //this will be variated in the future. |
|
198 return " "; |
|
199 } |
|
200 |
|
201 /*! |
|
202 * Returns the details to be used for contact filtering |
|
203 */ |
|
204 QList<QPair<QLatin1String, QLatin1String> > CntDisplayLabel::contactFilterDetails() const |
|
205 { |
|
206 return m_contactDisplayLabelDetails.at(0); |
|
207 } |
|
208 |
|
209 /*! |
|
210 * Returns the details to be used for group filtering |
|
211 */ |
|
212 QList<QPair<QLatin1String, QLatin1String> > CntDisplayLabel::groupFilterDetails() const |
|
213 { |
|
214 return m_groupDisplayLabelDetails.at(0); |
|
215 } |
|
216 |
|
217 /*! |
|
218 * Comma to be used in display label |
|
219 * \return comma |
|
220 */ |
|
221 QString CntDisplayLabel::comma() const |
|
222 { |
|
223 return ","; |
|
224 } |
|
225 |
|
226 #ifdef SYMBIAN_BACKEND_USE_SQLITE |
|
227 |
|
228 void CntDisplayLabel::updateNameOrdering() |
|
229 { |
|
230 emit displayLabelChanged(); |
|
231 } |
|
232 |
|
233 CntCenrep::CntCenrep(const TUint32 aKey, CntDisplayLabel& aDisplayLabel) : |
|
234 CActive(EPriorityStandard), |
|
235 iCenrep(NULL), |
|
236 iDisplayLabel(&aDisplayLabel), |
|
237 iKey(aKey) |
|
238 { |
|
239 TRAPD(error, iCenrep = CRepository::NewL(KCRCntSettings)); |
|
240 |
|
241 if ( error == KErrNone ) { |
|
242 CActiveScheduler::Add(this); |
|
243 |
|
244 // initial subscription and process current property value |
|
245 iCenrep->NotifyRequest(iKey, iStatus ); |
|
246 SetActive(); |
|
247 } |
|
248 |
|
249 iValue = getValue(); |
|
250 } |
|
251 |
|
252 CntCenrep::~CntCenrep() |
|
253 { |
|
254 Cancel(); |
|
255 delete iCenrep; |
|
256 } |
|
257 |
|
258 int CntCenrep::getValue() |
|
259 { |
|
260 TInt value; |
|
261 if (iCenrep && iCenrep->Get(iKey, value) == KErrNone) { |
|
262 return value; |
|
263 } else { |
|
264 return -1; |
|
265 } |
|
266 } |
|
267 |
|
268 void CntCenrep::DoCancel() |
|
269 { |
|
270 if (iCenrep) |
|
271 iCenrep->NotifyCancel(iKey); |
|
272 } |
|
273 |
|
274 TInt CntCenrep::RunError(TInt aError) |
|
275 { |
|
276 return aError; |
|
277 } |
|
278 |
|
279 void CntCenrep::RunL() |
|
280 { |
|
281 // resubscribe before processing new value to prevent missing updates |
|
282 iCenrep->NotifyRequest(iKey, iStatus ); |
|
283 SetActive(); |
|
284 |
|
285 int value = getValue(); |
|
286 if (value != -1 && value != iValue) { |
|
287 iValue = value; |
|
288 iDisplayLabel->updateNameOrdering(); |
|
289 } |
|
290 } |
|
291 |
|
292 #endif |