|
1 /* |
|
2 * Copyright (c) 2009 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: nmrecipientlineedit.cpp |
|
15 * |
|
16 */ |
|
17 |
|
18 #include "nmailuiwidgetsheaders.h" |
|
19 |
|
20 static const QString Semicolon(";"); |
|
21 static const QString Delimiter("; "); |
|
22 static const QRegExp CommaOrSemicolon("[,;]"); |
|
23 |
|
24 |
|
25 /*! |
|
26 Constructor |
|
27 */ |
|
28 NmRecipientLineEdit::NmRecipientLineEdit(QGraphicsItem *parent) |
|
29 : NmHtmlLineEdit(parent), |
|
30 mNeedToGenerateEmailAddressList(true) |
|
31 { |
|
32 NM_FUNCTION; |
|
33 |
|
34 connect(this, SIGNAL(textChanged(QString)), this, SLOT(handleTextChanged(QString))); |
|
35 } |
|
36 |
|
37 |
|
38 /*! |
|
39 Destructor |
|
40 */ |
|
41 NmRecipientLineEdit::~NmRecipientLineEdit() |
|
42 { |
|
43 NM_FUNCTION; |
|
44 } |
|
45 |
|
46 |
|
47 /*! |
|
48 Get the emailaddress list generated from the content of the lineedit. |
|
49 */ |
|
50 QList<NmAddress> NmRecipientLineEdit::emailAddressList() |
|
51 { |
|
52 NM_FUNCTION; |
|
53 |
|
54 if (mNeedToGenerateEmailAddressList) { |
|
55 // Empty mEmailAddressList. |
|
56 mEmailAddressList.clear(); |
|
57 // Generate mEmailAddressList from the lineedit content. |
|
58 generateEmailAddressList(); |
|
59 mNeedToGenerateEmailAddressList = false; |
|
60 } |
|
61 |
|
62 return mEmailAddressList; |
|
63 } |
|
64 |
|
65 |
|
66 #ifdef Q_OS_SYMBIAN |
|
67 /*! |
|
68 This Slot appends the selected contacts to the end of the lineedit content. |
|
69 */ |
|
70 void NmRecipientLineEdit::addSelectedContacts(const QVariant &selectedContacts) |
|
71 { |
|
72 NM_FUNCTION; |
|
73 |
|
74 // If user selected contact |
|
75 if (!selectedContacts.isNull()) { |
|
76 |
|
77 // If the lineedit is not empty and if there is no ";" or "; " at the end, |
|
78 // add a delimiter("; ") at the end. |
|
79 if (this->text().length() != 0 && !(this->text().endsWith(Semicolon)) && |
|
80 !(this->text().endsWith(Delimiter))){ |
|
81 |
|
82 // Move cursor to the end of the lineedit. |
|
83 this->setCursorPosition(this->text().length()); |
|
84 QTextCursor textCursor(this->textCursor()); |
|
85 // Append delimiter("; ") to the end of the lineedit |
|
86 textCursor.insertText(Delimiter); |
|
87 } |
|
88 |
|
89 CntServicesContactList contactList = qVariantValue<CntServicesContactList>(selectedContacts); |
|
90 |
|
91 // Loop through all the selected contacts. |
|
92 for (int i = 0; i < contactList.count(); ++i) { |
|
93 QString contactName = contactList[i].mDisplayName; |
|
94 QString contactEmailAddress = contactList[i].mEmailAddress; |
|
95 |
|
96 // If this contact has no name, use it's emailaddress as the display name |
|
97 if(contactName.isEmpty()) { |
|
98 // Move cursor to the end of the lineedit. |
|
99 this->setCursorPosition(this->text().length()); |
|
100 QTextCursor textCursor(this->textCursor()); |
|
101 // Append contactEmailAddress to the end of the lineedit |
|
102 textCursor.insertText(contactEmailAddress); |
|
103 } |
|
104 // If this contact has name, use the name as the display name |
|
105 else { |
|
106 // Handle a rare case: there are contacts has same name but different emailaddress. |
|
107 for (int i = 0; i != mRecipientsAddedFromContacts.count(); ++i) { |
|
108 if (mRecipientsAddedFromContacts.at(i).displayName() == contactName && |
|
109 mRecipientsAddedFromContacts.at(i).address() != contactEmailAddress) { |
|
110 // Differentiate this contact by supplying it's emailaddress |
|
111 contactName.append("<"); |
|
112 contactName.append(contactEmailAddress); |
|
113 contactName.append(">"); |
|
114 } |
|
115 } |
|
116 |
|
117 // Move cursor to the end of the lineedit. |
|
118 this->setCursorPosition(this->text().length()); |
|
119 QTextCursor textCursor(this->textCursor()); |
|
120 // Append contactName to the end of the lineedit |
|
121 textCursor.insertText(contactName); |
|
122 } |
|
123 |
|
124 QTextCursor textCursor(this->textCursor()); |
|
125 // Append delimiter("; ") |
|
126 textCursor.insertText(Delimiter); |
|
127 |
|
128 // Form the contact into Qmail NmAddress format. |
|
129 NmAddress contact; |
|
130 contact.setAddress(contactEmailAddress); |
|
131 contact.setDisplayName(contactName); |
|
132 |
|
133 // Add this NmAddress formated contact into mRecipientsAddedFromContacts. |
|
134 mRecipientsAddedFromContacts.append(contact); |
|
135 } |
|
136 } |
|
137 else { |
|
138 //Request returned NULL |
|
139 NM_COMMENT("ContactsPicker request returned NULL."); |
|
140 } |
|
141 |
|
142 } |
|
143 |
|
144 Q_IMPLEMENT_USER_METATYPE(CntServicesContact) |
|
145 Q_IMPLEMENT_USER_METATYPE_NO_OPERATORS(CntServicesContactList) |
|
146 #endif // Q_OS_SYMBIAN |
|
147 |
|
148 |
|
149 /*! |
|
150 keyPressEvent handles replacing user inputs "," or ";" from physical keyboard with "; " |
|
151 P.S. keyPressEvent can only catch QKeyEvent "," or ";" typed from physical keyboard, |
|
152 inputMethodEvent method handles user inputs "," or ";" from virtual keyboard. |
|
153 */ |
|
154 void NmRecipientLineEdit::keyPressEvent(QKeyEvent *keyEvent) |
|
155 { |
|
156 NM_FUNCTION; |
|
157 |
|
158 bool eventHandled = false; |
|
159 |
|
160 if (keyEvent) { |
|
161 switch (keyEvent->key()) { |
|
162 case Qt::Key_Comma: |
|
163 case Qt::Key_Semicolon: |
|
164 { |
|
165 QString textBeforeCursor = (this->text()).left(this->cursorPosition()); |
|
166 |
|
167 // No action when the lineedit is empty or cursor is after a Delimiter("; ") |
|
168 // or a Semicolon (";"). |
|
169 if ((this->text()).isEmpty() || textBeforeCursor.endsWith(Delimiter) |
|
170 || textBeforeCursor.endsWith(Semicolon)) { |
|
171 keyEvent->ignore(); |
|
172 eventHandled = true; |
|
173 } |
|
174 else { |
|
175 // Generate custom keyevent for Delimiter("; ") and |
|
176 // forward to the base class to handle. |
|
177 QKeyEvent delimiterKeyEvent(keyEvent->type(), keyEvent->key(), |
|
178 keyEvent->modifiers(), Delimiter); |
|
179 NmHtmlLineEdit::keyPressEvent(&delimiterKeyEvent); |
|
180 eventHandled = true; |
|
181 } |
|
182 } |
|
183 break; |
|
184 |
|
185 default: |
|
186 NmHtmlLineEdit::keyPressEvent(keyEvent); |
|
187 eventHandled = true; |
|
188 break; |
|
189 } |
|
190 } |
|
191 |
|
192 // If event is not handled, forward to the base class to handle. |
|
193 if (!eventHandled) { |
|
194 NmHtmlLineEdit::keyPressEvent(keyEvent); |
|
195 } |
|
196 } |
|
197 |
|
198 |
|
199 /*! |
|
200 inputMethodEvent handles replacing user inputs "," or ";" from virtual keyboard with "; ". |
|
201 P.S. keyPressEvent can only catch QKeyEvent "," or ";" typed from physical keyboard |
|
202 */ |
|
203 void NmRecipientLineEdit::inputMethodEvent(QInputMethodEvent *event) |
|
204 { |
|
205 NM_FUNCTION; |
|
206 |
|
207 bool eventHandled = false; |
|
208 |
|
209 if (event) { |
|
210 QString eventText = event->commitString(); |
|
211 |
|
212 if (!eventText.isEmpty() || event->replacementLength()) { |
|
213 // If typed charater from virtual keyboard is "," or ";" |
|
214 if (eventText.contains(CommaOrSemicolon)) { |
|
215 QString textBeforeCursor = (this->text()).left(this->cursorPosition()); |
|
216 |
|
217 // No action when the lineedit is empty or cursor is after a Delimiter("; ") |
|
218 // or Semicolon (";"). |
|
219 if ((this->text()).isEmpty() || textBeforeCursor.endsWith(Delimiter) |
|
220 || textBeforeCursor.endsWith(Semicolon)) { |
|
221 event->ignore(); |
|
222 eventHandled = true; |
|
223 } |
|
224 else { |
|
225 // Modify event with Delimiter("; ") and forward to the base class to handle. |
|
226 event->setCommitString(Delimiter, event->replacementStart(), |
|
227 event->replacementLength()); |
|
228 NmHtmlLineEdit::inputMethodEvent(event); |
|
229 eventHandled = true; |
|
230 } |
|
231 } |
|
232 } |
|
233 } |
|
234 |
|
235 // If event is not handled, forward to the base class to handle. |
|
236 if (!eventHandled) { |
|
237 NmHtmlLineEdit::inputMethodEvent(event); |
|
238 } |
|
239 } |
|
240 |
|
241 |
|
242 /*! |
|
243 Generate a list of all the email addresses from the content of the lineedit. |
|
244 */ |
|
245 void NmRecipientLineEdit::generateEmailAddressList() |
|
246 { |
|
247 NM_FUNCTION; |
|
248 |
|
249 // Remove whitespace from the start and the end of the lineedit content. |
|
250 QString contentOfLineedit = (this->text()).trimmed(); |
|
251 |
|
252 // Split the lineedit content by semicolon(";"). |
|
253 QStringList itemsOfLineeditContent = contentOfLineedit.split(Semicolon, QString::SkipEmptyParts); |
|
254 |
|
255 // Loop through all the items of the lineedit content. |
|
256 for (int i = 0; i != itemsOfLineeditContent.count(); ++i) { |
|
257 // Remove whitespace from the start and the end of the item. |
|
258 QString itemInLineedit = itemsOfLineeditContent.at(i).trimmed(); |
|
259 |
|
260 // Get the count of the recipients added from Contacts. |
|
261 int countOfRecipientsAddedFromContacts = mRecipientsAddedFromContacts.count(); |
|
262 |
|
263 // If there is recipient added from Contacts. |
|
264 if (countOfRecipientsAddedFromContacts > 0) { |
|
265 QStringList listOfAddedContactsName; |
|
266 QStringList listOfAddedContactsAddress; |
|
267 |
|
268 // Loop through all the recipients added from Contacts. |
|
269 for (int j = 0; j != countOfRecipientsAddedFromContacts; ++j) { |
|
270 NmAddress contact = mRecipientsAddedFromContacts.at(j); |
|
271 listOfAddedContactsName.append(contact.displayName()); |
|
272 listOfAddedContactsAddress.append(contact.address()); |
|
273 } |
|
274 |
|
275 int indexInAddedContactsName = listOfAddedContactsName.indexOf(itemInLineedit); |
|
276 int indexInAddedContactsAddress = listOfAddedContactsAddress.indexOf(itemInLineedit); |
|
277 |
|
278 // If this itemInLineedit matches the name of one added contact. |
|
279 if (indexInAddedContactsName >= 0) { |
|
280 // Add the recipient into mEmailAddressList. |
|
281 mEmailAddressList.append(mRecipientsAddedFromContacts.at(indexInAddedContactsName)); |
|
282 } |
|
283 // If this itemInLineedit matches the emailaddress of one added contact. |
|
284 else if (indexInAddedContactsAddress >= 0) { |
|
285 // Add the recipient into mEmailAddressList. |
|
286 mEmailAddressList.append(mRecipientsAddedFromContacts.at(indexInAddedContactsAddress)); |
|
287 } |
|
288 // This itemInLineedit is not added from Contacts |
|
289 else { |
|
290 // Form the item into NmAddress format. |
|
291 NmAddress recipient; |
|
292 recipient.setAddress(itemInLineedit); |
|
293 // There is no display name info available, so leave display name empty. |
|
294 recipient.setDisplayName(QString()); |
|
295 // Add this NmAddress formated lineedit item into mEmailAddressList. |
|
296 mEmailAddressList.append(recipient); |
|
297 } |
|
298 } |
|
299 else { // There is no recipient is added from Contacts |
|
300 // Form the item into NmAddress format. |
|
301 NmAddress recipient; |
|
302 recipient.setAddress(itemInLineedit); |
|
303 // There is no display name info available, so leave display name emapty. |
|
304 recipient.setDisplayName(QString()); |
|
305 // Add this NmAddress formated lineedit item into mEmailAddressList. |
|
306 mEmailAddressList.append(recipient); |
|
307 } |
|
308 } |
|
309 } |
|
310 |
|
311 |
|
312 /*! |
|
313 This Slot is called when the lineedit text changes. |
|
314 */ |
|
315 void NmRecipientLineEdit::handleTextChanged(const QString &text) |
|
316 { |
|
317 NM_FUNCTION; |
|
318 |
|
319 Q_UNUSED(text); |
|
320 mNeedToGenerateEmailAddressList = true; |
|
321 } |