|
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:Custom widget derived from HbTextEdit which provides rich text |
|
15 * processing |
|
16 * |
|
17 */ |
|
18 |
|
19 #include "univieweraddresswidget.h" |
|
20 |
|
21 // SYSTEM INCLUDES |
|
22 #include <QTextCursor> |
|
23 #include <HbMenu> |
|
24 #include <QGraphicsSceneMouseEvent> |
|
25 #include <HbAction> |
|
26 #include <HbFrameItem> |
|
27 #include <QTextBlock> |
|
28 #include <QApplication> |
|
29 #include <QClipBoard> |
|
30 #include <xqservicerequest.h> |
|
31 |
|
32 #include <xqappmgr.h> |
|
33 #include <cntservicescontact.h> |
|
34 #include <qtcontacts.h> |
|
35 #include <XQServiceRequest.h> |
|
36 #include <xqaiwrequest.h> |
|
37 #include "msgcontacthandler.h" |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 // LOCAL CONSTANTS |
|
44 const QString ADDRESS_SEPARATOR("; "); |
|
45 const QString ADDRESS_OPEN(" ("); |
|
46 const QString ADDRESS_CLOSE(")"); |
|
47 const QString SPACE(" "); |
|
48 |
|
49 //localization |
|
50 #define LOC_OPEN_CONTACT_INFO hbTrId("txt_messaging_menu_open_contact_info") |
|
51 #define LOC_CALL hbTrId("txt_common_menu_call_verb") |
|
52 #define LOC_SEND_MESSAGE hbTrId("txt_common_menu_send_message") |
|
53 #define LOC_SAVE_TO_CONTACTS hbTrId("txt_common_menu_save_to_contacts") |
|
54 #define LOC_COPY hbTrId("txt_common_menu_copy") |
|
55 |
|
56 const QString BG_FRAME_GRAPHICS("qtg_fr_lineedit_normal"); |
|
57 |
|
58 //--------------------------------------------------------------- |
|
59 // UniViewerAddressWidget::UniViewerAddressWidget |
|
60 // @see header file |
|
61 //--------------------------------------------------------------- |
|
62 UniViewerAddressWidget::UniViewerAddressWidget(QGraphicsItem * parent) : |
|
63 HbTextEdit(parent), |
|
64 mCursorPos(-1) |
|
65 { |
|
66 this->setReadOnly(true); |
|
67 this->setCursorVisibility(Hb::TextCursorHidden); |
|
68 this->setScrollable(false); |
|
69 HbFrameItem *noBackground = new HbFrameItem(this); |
|
70 this->setBackgroundItem(noBackground); |
|
71 |
|
72 HbFontSpec fontSpec(HbFontSpec::Secondary); |
|
73 QFont font = fontSpec.font(); |
|
74 this->setFont(font); |
|
75 |
|
76 mFormatNormal.setForeground(palette().link()); |
|
77 mFormatNormal.setBackground(Qt::transparent); |
|
78 |
|
79 mFormatHighlight.setBackground(palette().highlight()); |
|
80 mFormatHighlight.setForeground(palette().highlightedText()); |
|
81 |
|
82 connect(this, SIGNAL(aboutToShowContextMenu(HbMenu*,QPointF)), |
|
83 this, SLOT(aboutToShowContextMenu(HbMenu*,QPointF))); |
|
84 } |
|
85 |
|
86 //--------------------------------------------------------------- |
|
87 // UniViewerAddressWidget::~UniViewerAddressWidget |
|
88 // @see header file |
|
89 //--------------------------------------------------------------- |
|
90 UniViewerAddressWidget::~UniViewerAddressWidget() |
|
91 { |
|
92 } |
|
93 |
|
94 //--------------------------------------------------------------- |
|
95 //UniViewerAddressWidget :: mousePressEvent |
|
96 // @see header file |
|
97 //--------------------------------------------------------------- |
|
98 void UniViewerAddressWidget::mousePressEvent(QGraphicsSceneMouseEvent *event) |
|
99 { |
|
100 HbTextEdit::mousePressEvent(event); |
|
101 |
|
102 QTextDocument* doc = this->document(); |
|
103 |
|
104 mCursorPos = doc->documentLayout()->hitTest(event->pos(), Qt::ExactHit); |
|
105 |
|
106 highlightText(true); |
|
107 } |
|
108 |
|
109 //--------------------------------------------------------------- |
|
110 //UniViewerAddressWidget :: mouseReleaseEvent |
|
111 // @see header file |
|
112 //--------------------------------------------------------------- |
|
113 void UniViewerAddressWidget::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) |
|
114 { |
|
115 HbTextEdit::mouseReleaseEvent(event); |
|
116 |
|
117 highlightText(false); |
|
118 |
|
119 QString anchor = this->anchorAt(event->pos()); |
|
120 |
|
121 if(!anchor.isEmpty() && !this->textCursor().hasSelection()) |
|
122 { |
|
123 shortTapAction(anchor); |
|
124 } |
|
125 } |
|
126 |
|
127 //---------------------------------------------------------------------------- |
|
128 // UniViewerAddressWidget::populate |
|
129 // @see header file |
|
130 //---------------------------------------------------------------------------- |
|
131 void UniViewerAddressWidget::populate(const QString &label, |
|
132 const QString &address, |
|
133 const QString &alias) |
|
134 { |
|
135 QString labelText = label; |
|
136 labelText.trimmed(); |
|
137 labelText += SPACE; |
|
138 |
|
139 //Font. |
|
140 HbFontSpec fontSpec(HbFontSpec::Secondary); |
|
141 qreal fontHeight = 0.0; |
|
142 style()->parameter("hb-param-text-height-tiny", fontHeight); |
|
143 fontSpec.setTextHeight(fontHeight); |
|
144 QFont font = fontSpec.font(); |
|
145 |
|
146 QTextCharFormat labelFormat; |
|
147 labelFormat.setFont(font); |
|
148 |
|
149 QTextCharFormat addressFormat; |
|
150 addressFormat.setForeground(palette().link()); |
|
151 addressFormat.setFontUnderline(true); |
|
152 |
|
153 // Insert the label then the addresses |
|
154 QTextCursor cursor(this->textCursor()); |
|
155 cursor.insertText(labelText,labelFormat); |
|
156 |
|
157 QString address1 = QString(); |
|
158 if (!(alias.isEmpty())) |
|
159 { |
|
160 address1.append(alias); |
|
161 QString alias1 = QString(); |
|
162 |
|
163 int totalNumbers = 0; |
|
164 MsgContactHandler::resolveContactDisplayName( |
|
165 address, |
|
166 alias1, |
|
167 totalNumbers); |
|
168 if (totalNumbers > 1) |
|
169 { |
|
170 address1.append(ADDRESS_OPEN); |
|
171 address1.append(address); |
|
172 address1.append(ADDRESS_CLOSE); |
|
173 } |
|
174 } |
|
175 else |
|
176 { |
|
177 address1.append(address); |
|
178 } |
|
179 addressFormat.setAnchorHref(address); |
|
180 cursor.insertText(address1, addressFormat); |
|
181 } |
|
182 |
|
183 //---------------------------------------------------------------------------- |
|
184 // UniViewerAddressWidget::populate |
|
185 // @see header file |
|
186 //---------------------------------------------------------------------------- |
|
187 void UniViewerAddressWidget::populate(const QString &label, |
|
188 ConvergedMessageAddressList addressList) |
|
189 { |
|
190 QString labelText = label; |
|
191 labelText.trimmed(); |
|
192 labelText += SPACE; |
|
193 |
|
194 //Font. |
|
195 HbFontSpec fontSpec(HbFontSpec::Secondary); |
|
196 qreal fontHeight = 0.0; |
|
197 style()->parameter("hb-param-text-height-tiny", fontHeight); |
|
198 fontSpec.setTextHeight(fontHeight); |
|
199 QFont font = fontSpec.font(); |
|
200 |
|
201 QTextCharFormat labelFormat; |
|
202 labelFormat.setFont(font); |
|
203 |
|
204 QTextCharFormat defaultFormat; |
|
205 defaultFormat.setForeground(palette().link()); |
|
206 |
|
207 QTextCharFormat addressFormat; |
|
208 addressFormat.setForeground(palette().link()); |
|
209 addressFormat.setFontUnderline(true); |
|
210 |
|
211 // Insert the label then the addresses |
|
212 QTextCursor cursor(this->document()); |
|
213 cursor.insertText(labelText,labelFormat); |
|
214 |
|
215 int addressCount = addressList.count(); |
|
216 |
|
217 for (int i = 0; i < addressCount; ++i) |
|
218 { |
|
219 |
|
220 QString address = QString(); |
|
221 if (! (addressList[i]->alias().isEmpty())) |
|
222 { |
|
223 address.append(addressList[i]->alias()); |
|
224 QString alias = QString(); |
|
225 |
|
226 int totalNumbers = 0; |
|
227 MsgContactHandler::resolveContactDisplayName( |
|
228 addressList[i]->address(), |
|
229 alias, |
|
230 totalNumbers); |
|
231 if (totalNumbers > 1) |
|
232 { |
|
233 address.append(ADDRESS_OPEN); |
|
234 address.append(addressList[i]->address()); |
|
235 address.append(ADDRESS_CLOSE); |
|
236 } |
|
237 } |
|
238 else |
|
239 { |
|
240 address.append(addressList[i]->address()); |
|
241 } |
|
242 |
|
243 addressFormat.setAnchorHref(addressList[i]->address()); |
|
244 cursor.insertText(address, addressFormat); |
|
245 |
|
246 if (addressCount - 1 != i) |
|
247 { |
|
248 cursor.insertText(ADDRESS_SEPARATOR, defaultFormat); |
|
249 } |
|
250 |
|
251 } |
|
252 } |
|
253 |
|
254 //---------------------------------------------------------------------------- |
|
255 // UniViewerAddressWidget::clearContent |
|
256 // @see header file |
|
257 //---------------------------------------------------------------------------- |
|
258 void UniViewerAddressWidget::clearContent() |
|
259 { |
|
260 this->document()->clear(); |
|
261 } |
|
262 |
|
263 //---------------------------------------------------------------------------- |
|
264 // UniViewerAddressWidget::menuClosed |
|
265 // @see header file |
|
266 //---------------------------------------------------------------------------- |
|
267 void UniViewerAddressWidget::menuClosed() |
|
268 { |
|
269 highlightText(false); |
|
270 } |
|
271 |
|
272 //---------------------------------------------------------------------------- |
|
273 // UniViewerAddressWidget::highlightText |
|
274 // @see header file |
|
275 //---------------------------------------------------------------------------- |
|
276 void UniViewerAddressWidget::highlightText(bool highlight) |
|
277 { |
|
278 QTextBlock textBlock = this->document()->findBlock(mCursorPos); |
|
279 |
|
280 QTextBlock::iterator it; |
|
281 |
|
282 for (it = textBlock.begin(); !(it.atEnd()); ++it) |
|
283 { |
|
284 QTextFragment currentFragment = it.fragment(); |
|
285 |
|
286 if (currentFragment.isValid() && currentFragment.contains(mCursorPos) |
|
287 && currentFragment.charFormat().fontUnderline()) |
|
288 { |
|
289 int start = currentFragment.position(); |
|
290 int length = currentFragment.length(); |
|
291 |
|
292 QTextCursor cursor = this->textCursor(); |
|
293 cursor.clearSelection(); |
|
294 cursor.setPosition(start); |
|
295 cursor.setPosition(start + length,QTextCursor::KeepAnchor); |
|
296 |
|
297 if(highlight) |
|
298 { |
|
299 cursor.mergeCharFormat(mFormatHighlight); |
|
300 } |
|
301 else |
|
302 { |
|
303 cursor.mergeCharFormat(mFormatNormal); |
|
304 } |
|
305 |
|
306 cursor.clearSelection(); |
|
307 break; |
|
308 } |
|
309 } |
|
310 } |
|
311 |
|
312 void UniViewerAddressWidget::aboutToShowContextMenu(HbMenu *contextMenu, const QPointF &pos) |
|
313 { |
|
314 //remove default actions. |
|
315 contextMenu->clearActions(); |
|
316 |
|
317 // Check if there is an anchor at this pos |
|
318 QString anchor = this->anchorAt(pos); |
|
319 |
|
320 if(!anchor.isEmpty() && !this->textCursor().hasSelection()) |
|
321 { |
|
322 |
|
323 HbAction* action = NULL; |
|
324 |
|
325 action = contextMenu->addAction(LOC_OPEN_CONTACT_INFO, this, SLOT(openContactInfo())); |
|
326 action->setData(anchor); |
|
327 |
|
328 action = contextMenu->addAction(LOC_CALL, this, SLOT(call())); |
|
329 action->setData(anchor); |
|
330 |
|
331 action = contextMenu->addAction(LOC_SEND_MESSAGE, this, SLOT(sendMessage())); |
|
332 action->setData(anchor); |
|
333 |
|
334 action = contextMenu->addAction(LOC_SAVE_TO_CONTACTS, this, SLOT(saveToContacts())); |
|
335 action->setData(anchor); |
|
336 |
|
337 action = contextMenu->addAction(LOC_COPY, this, SLOT(copyToClipboard())); |
|
338 action->setData(anchor); |
|
339 |
|
340 } |
|
341 |
|
342 connect(contextMenu,SIGNAL(aboutToClose()),this,SLOT(menuClosed())); |
|
343 } |
|
344 |
|
345 void UniViewerAddressWidget::shortTapAction(QString anchor) |
|
346 { |
|
347 HbAction action; |
|
348 action.setData(anchor); |
|
349 connect(&action,SIGNAL(triggered()),this,SLOT(openContactInfo())); |
|
350 action.trigger(); |
|
351 } |
|
352 |
|
353 void UniViewerAddressWidget::copyToClipboard() |
|
354 { |
|
355 HbAction* action = qobject_cast<HbAction*>(sender()); |
|
356 |
|
357 if(action) |
|
358 { |
|
359 QMimeData* data = new QMimeData(); |
|
360 QString str = action->data().toString(); |
|
361 data->setText(str); |
|
362 QApplication::clipboard()->setMimeData(data); |
|
363 } |
|
364 } |
|
365 |
|
366 void UniViewerAddressWidget::call() |
|
367 { |
|
368 HbAction* action = qobject_cast<HbAction*>(sender()); |
|
369 |
|
370 if(action) |
|
371 { |
|
372 QString phoneNumber = action->data().toString(); |
|
373 |
|
374 //invoke dialer service and pass phoneNumber. |
|
375 QString serviceName("com.nokia.services.telephony"); |
|
376 QString operation("dial(QString)"); |
|
377 |
|
378 XQServiceRequest* serviceRequest = new XQServiceRequest(serviceName,operation,false); |
|
379 |
|
380 connect(serviceRequest, SIGNAL(requestCompleted(QVariant)), |
|
381 this, SLOT(onServiceRequestCompleted())); |
|
382 |
|
383 connect(serviceRequest, SIGNAL(requestError(int)), |
|
384 this, SLOT(onServiceRequestCompleted())); |
|
385 |
|
386 *serviceRequest << phoneNumber; |
|
387 serviceRequest->send(); |
|
388 } |
|
389 } |
|
390 |
|
391 void UniViewerAddressWidget::onServiceRequestCompleted() |
|
392 { |
|
393 //service request is now complete. delete it. |
|
394 XQServiceRequest* request = qobject_cast<XQServiceRequest*>(sender()); |
|
395 |
|
396 if(request) |
|
397 { |
|
398 delete request; |
|
399 } |
|
400 } |
|
401 |
|
402 |
|
403 void UniViewerAddressWidget::openContactInfo() |
|
404 { |
|
405 HbAction* action = qobject_cast<HbAction*>(sender()); |
|
406 |
|
407 if(action) |
|
408 { |
|
409 QList<QVariant> args; |
|
410 QString operation; |
|
411 |
|
412 QString data = action->data().toString(); |
|
413 |
|
414 int contactId = MsgContactHandler::resolveContactDisplayName( |
|
415 data, |
|
416 QContactPhoneNumber::DefinitionName, |
|
417 QContactPhoneNumber::FieldNumber); |
|
418 |
|
419 if(contactId > 0) |
|
420 { |
|
421 //open contact card |
|
422 operation = QString("open(int)"); |
|
423 args << contactId; |
|
424 } |
|
425 else |
|
426 { |
|
427 //save to contacts with phone number field prefilled. |
|
428 |
|
429 operation = QString("editCreateNew(QString,QString)"); |
|
430 QString type = QContactPhoneNumber::DefinitionName; |
|
431 |
|
432 args << type; |
|
433 args << data; |
|
434 } |
|
435 |
|
436 //service stuff. |
|
437 QString serviceName("com.nokia.services.phonebookservices"); |
|
438 |
|
439 XQAiwRequest* request; |
|
440 XQApplicationManager appManager; |
|
441 request = appManager.create(serviceName, "Fetch", operation, true); // embedded |
|
442 if ( request == NULL ) |
|
443 { |
|
444 return; |
|
445 } |
|
446 |
|
447 // Result handlers |
|
448 connect (request, SIGNAL(requestOk(const QVariant&)), |
|
449 this, SLOT(handleOk(const QVariant&))); |
|
450 connect (request, SIGNAL(requestError(const QVariant&)), |
|
451 this, SLOT(handleError(const QVariant&))); |
|
452 |
|
453 request->setArguments(args); |
|
454 request->send(); |
|
455 delete request; |
|
456 } |
|
457 } |
|
458 |
|
459 void UniViewerAddressWidget::handleOk(const QVariant& result) |
|
460 { |
|
461 Q_UNUSED(result) |
|
462 } |
|
463 |
|
464 void UniViewerAddressWidget::handleError(int errorCode, const QString& errorMessage) |
|
465 { |
|
466 Q_UNUSED(errorMessage) |
|
467 Q_UNUSED(errorCode) |
|
468 } |
|
469 |
|
470 void UniViewerAddressWidget::saveToContacts() |
|
471 { |
|
472 //handler for save to contacts. |
|
473 } |
|
474 |
|
475 void UniViewerAddressWidget::sendMessage() |
|
476 { |
|
477 HbAction* action = qobject_cast<HbAction*>(sender()); |
|
478 |
|
479 if(action) |
|
480 { |
|
481 QString phoneNumber = action->data().toString(); |
|
482 QString alias; |
|
483 |
|
484 QTextBlock textBlock = this->document()->findBlock(mCursorPos); |
|
485 |
|
486 QTextBlock::iterator it; |
|
487 |
|
488 for (it = textBlock.begin(); !(it.atEnd()); ++it) |
|
489 { |
|
490 QTextFragment currentFragment = it.fragment(); |
|
491 |
|
492 if (currentFragment.isValid() && currentFragment.contains(mCursorPos) |
|
493 && currentFragment.charFormat().fontUnderline()) |
|
494 { |
|
495 QString txt = currentFragment.text(); |
|
496 if(txt != phoneNumber) |
|
497 { |
|
498 alias = txt; |
|
499 } |
|
500 break; |
|
501 } |
|
502 } |
|
503 |
|
504 |
|
505 //invoke editor & pass phoneNumber. |
|
506 emit sendMessage(phoneNumber,alias); |
|
507 } |
|
508 } |
|
509 |
|
510 // EOF |