|
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: |
|
15 * |
|
16 */ |
|
17 |
|
18 #include "cntcontactcardview.h" |
|
19 |
|
20 #include <QGraphicsLinearLayout> |
|
21 #include <QGraphicsSceneResizeEvent> |
|
22 #include <qtcontacts.h> |
|
23 #include <hbdocumentloader.h> |
|
24 #include <hbscrollarea.h> |
|
25 |
|
26 #include <hbmenu.h> |
|
27 #include <hbgroupbox.h> |
|
28 #include <thumbnailmanager_qt.h> |
|
29 |
|
30 #include "cntcontactcarddatacontainer.h" |
|
31 #include "cntcontactcarddetailitem.h" |
|
32 #include "cntcontactcardheadingitem.h" |
|
33 #include "cntmainwindow.h" |
|
34 #include "cntcommands.h" |
|
35 |
|
36 const char *CNT_COMMLAUNCERVIEW_XML = ":/xml/contacts_cc.docml"; |
|
37 |
|
38 /*! |
|
39 Constructor, initialize member variables. |
|
40 \a viewManager is the parent that creates this view. \a parent is a pointer to parent QGraphicsItem (by default this is 0) |
|
41 */ |
|
42 CntContactCardView::CntContactCardView(CntViewManager *viewManager, QGraphicsItem *parent) : |
|
43 CntBaseView(viewManager, parent), |
|
44 mScrollArea(0), |
|
45 mContainerWidget(0), |
|
46 mContainerLayout(0), |
|
47 mContact(0), |
|
48 mDetailsWidget(0), |
|
49 mDataContainer(0), |
|
50 mHeadingItem(0), |
|
51 mThumbnailManager(0), |
|
52 mGroupContact(0), |
|
53 mIsGroupMember(false) |
|
54 { |
|
55 bool ok = false; |
|
56 ok = loadDocument(CNT_COMMLAUNCERVIEW_XML); |
|
57 |
|
58 if (ok) |
|
59 { |
|
60 QGraphicsWidget *content = findWidget(QString("content")); |
|
61 setWidget(content); |
|
62 } |
|
63 else |
|
64 { |
|
65 qFatal("Unable to read :/xml/contacts_cc.docml"); |
|
66 } |
|
67 |
|
68 mThumbnailManager = new ThumbnailManager(this); |
|
69 mThumbnailManager->setMode(ThumbnailManager::Default); |
|
70 mThumbnailManager->setQualityPreference(ThumbnailManager::OptimizeForQuality); |
|
71 mThumbnailManager->setThumbnailSize(ThumbnailManager::ThumbnailMedium); |
|
72 |
|
73 connect(mThumbnailManager, SIGNAL(thumbnailReady(QPixmap, void*, int, int)), |
|
74 this, SLOT(thumbnailReady(QPixmap, void*, int, int))); |
|
75 } |
|
76 |
|
77 /*! |
|
78 Destructor |
|
79 */ |
|
80 CntContactCardView::~CntContactCardView() |
|
81 { |
|
82 delete mContact; |
|
83 delete mDataContainer; |
|
84 delete mGroupContact; |
|
85 } |
|
86 |
|
87 void CntContactCardView::thumbnailReady(const QPixmap& pixmap, void *data, int id, int error) |
|
88 { |
|
89 Q_UNUSED(data); |
|
90 Q_UNUSED(id); |
|
91 Q_UNUSED(error); |
|
92 QIcon qicon(pixmap); |
|
93 HbIcon icon(qicon); |
|
94 mHeadingItem->setIcon(icon); |
|
95 } |
|
96 |
|
97 /*! |
|
98 Add actions to menu |
|
99 */ |
|
100 void CntContactCardView::addMenuItems() |
|
101 { |
|
102 actions()->clearActionList(); |
|
103 actions()->actionList() << actions()->baseAction("cnt:sendbusinesscard") << actions()->baseAction("cnt:editcontact") << |
|
104 actions()->baseAction("cnt:addtogroup") << actions()->baseAction("cnt:deletecontact"); |
|
105 actions()->addActionsToMenu(menu()); |
|
106 |
|
107 connect(actions()->baseAction("cnt:sendbusinesscard"), SIGNAL(triggered()), |
|
108 this, SLOT (sendBusinessCard())); |
|
109 |
|
110 connect(actions()->baseAction("cnt:editcontact"), SIGNAL(triggered()), |
|
111 this, SLOT (editContact())); |
|
112 |
|
113 connect(actions()->baseAction("cnt:addtogroup"), SIGNAL(triggered()), |
|
114 this, SLOT (addToGroup())); |
|
115 |
|
116 connect(actions()->baseAction("cnt:deletecontact"), SIGNAL(triggered()), |
|
117 this, SLOT (deleteContact())); |
|
118 |
|
119 // to be enabled after implementation |
|
120 actions()->baseAction("cnt:sendbusinesscard")->setEnabled(false); |
|
121 actions()->baseAction("cnt:addtogroup")->setEnabled(false); |
|
122 |
|
123 } |
|
124 |
|
125 /*! |
|
126 Add actions also to toolbar |
|
127 */ |
|
128 void CntContactCardView::addActionsToToolBar() |
|
129 { |
|
130 //Add Action to the toolbar |
|
131 actions()->clearActionList(); |
|
132 actions()->actionList() << actions()->baseAction("cnt:edit") << actions()->baseAction("cnt:history") << actions()->baseAction("cnt:activitystream"); |
|
133 actions()->addActionsToToolBar(toolBar()); |
|
134 |
|
135 connect(actions()->baseAction("cnt:edit"), SIGNAL(triggered()), |
|
136 this, SLOT(editContact())); |
|
137 connect(actions()->baseAction("cnt:history"), SIGNAL(triggered()), |
|
138 this, SLOT(viewHistory())); |
|
139 } |
|
140 |
|
141 /*! |
|
142 Launch contact editor |
|
143 */ |
|
144 void CntContactCardView::editContact() |
|
145 { |
|
146 CntViewParameters viewParameters(CntViewParameters::editView); |
|
147 viewParameters.setSelectedContact(*mContact); |
|
148 viewManager()->onActivateView(viewParameters); |
|
149 } |
|
150 |
|
151 void CntContactCardView::sendBusinessCard() |
|
152 { |
|
153 } |
|
154 |
|
155 void CntContactCardView::addToGroup() |
|
156 { |
|
157 } |
|
158 |
|
159 /*! |
|
160 Delete contact |
|
161 */ |
|
162 void CntContactCardView::deleteContact() |
|
163 { |
|
164 // the delete command |
|
165 connect(commands(), SIGNAL(commandExecuted(QString, QContact)), this, |
|
166 SLOT(handleExecutedCommand(QString, QContact))); |
|
167 commands()->deleteContact(*mContact); |
|
168 } |
|
169 |
|
170 |
|
171 /*! |
|
172 Launch history view |
|
173 */ |
|
174 void CntContactCardView::viewHistory() |
|
175 { |
|
176 commands()->viewHistory(*mContact); |
|
177 } |
|
178 |
|
179 /*! |
|
180 Activates a previous view |
|
181 */ |
|
182 void CntContactCardView::aboutToCloseView() |
|
183 { |
|
184 if(mIsGroupMember) |
|
185 { |
|
186 CntViewParameters viewParameters(CntViewParameters::groupMemberView); |
|
187 viewParameters.setSelectedContact(*mGroupContact); |
|
188 viewManager()->onActivateView(viewParameters); |
|
189 } |
|
190 else |
|
191 { |
|
192 viewManager()->onActivateView(CntViewParameters::namesView); |
|
193 } |
|
194 |
|
195 } |
|
196 |
|
197 void CntContactCardView::resizeEvent(QGraphicsSceneResizeEvent *event) |
|
198 { |
|
199 if (mScrollArea) |
|
200 { |
|
201 mContainerWidget->resize(mScrollArea->size().width(), 0); |
|
202 } |
|
203 CntBaseView::resizeEvent(event); |
|
204 } |
|
205 |
|
206 void CntContactCardView::handleExecutedCommand(QString command, QContact /*contact*/) |
|
207 { |
|
208 if (command == "delete") |
|
209 { |
|
210 CntViewParameters viewParameters(CntViewParameters::namesView); |
|
211 viewManager()->onActivateView(viewParameters); |
|
212 } |
|
213 } |
|
214 |
|
215 /* |
|
216 Activates a default view and setup name label texts |
|
217 */ |
|
218 void CntContactCardView::activateView(const CntViewParameters &viewParameters) |
|
219 { |
|
220 QContact contact = viewParameters.selectedContact(); |
|
221 mContact = new QContact(contact); |
|
222 |
|
223 if (viewParameters.selectedAction() == "FromGroupMemberView") |
|
224 { |
|
225 mIsGroupMember = true; |
|
226 QContact groupContact = viewParameters.selectedGroupContact(); |
|
227 mGroupContact = new QContact(groupContact); |
|
228 } |
|
229 // add heading widget to the content |
|
230 QGraphicsWidget *c = findWidget(QString("content")); |
|
231 QGraphicsLinearLayout* l = static_cast<QGraphicsLinearLayout*>(c->layout()); |
|
232 |
|
233 mHeadingItem = new CntContactCardHeadingItem(c); |
|
234 mHeadingItem->setDetails(mContact); |
|
235 |
|
236 l->insertItem(0, mHeadingItem); |
|
237 |
|
238 // avatar |
|
239 QList<QContactAvatar> details = mContact->details<QContactAvatar>(); |
|
240 if (details.count() > 0) |
|
241 { |
|
242 for (int i = 0;i < details.count();i++) |
|
243 { |
|
244 if (details.at(i).subType() == QContactAvatar::SubTypeImage) |
|
245 { |
|
246 mThumbnailManager->getThumbnail(details.at(i).avatar()); |
|
247 break; |
|
248 } |
|
249 } |
|
250 } |
|
251 |
|
252 // data |
|
253 mDataContainer = new CntContactCardDataContainer(mContact); |
|
254 |
|
255 mScrollArea = static_cast<HbScrollArea*>(findWidget(QString("scrollArea"))); |
|
256 mScrollArea->setScrollDirections(Qt::Vertical); |
|
257 |
|
258 mContainerWidget = new QGraphicsWidget(mScrollArea); |
|
259 mContainerWidget->setPreferredWidth(mScrollArea->size().width()); |
|
260 mScrollArea->setContentWidget(mContainerWidget); |
|
261 |
|
262 mContainerLayout = new QGraphicsLinearLayout(Qt::Vertical); |
|
263 mContainerLayout->setContentsMargins(0, 0, 0, 0); |
|
264 mContainerLayout->setSpacing(0); |
|
265 mContainerWidget->setLayout(mContainerLayout); |
|
266 |
|
267 for (int index = 0; index < mDataContainer->rowCount(); index++) |
|
268 { |
|
269 // communication methods |
|
270 if (mDataContainer->separatorIndex() == -1 || index < mDataContainer->separatorIndex()) |
|
271 { |
|
272 CntContactCardDetailItem* item = new CntContactCardDetailItem(index, mContainerWidget); |
|
273 |
|
274 connect(item, SIGNAL(clicked()), this, SLOT(onItemActivated())); |
|
275 connect(item, SIGNAL(longPressed(const QPointF&)), this, SLOT(onLongPressed(const QPointF&))); |
|
276 |
|
277 HbIcon icon(""); |
|
278 QString text; |
|
279 QString valueText; |
|
280 |
|
281 // DecorationRole |
|
282 QVariant decorationRole = mDataContainer->data(index, Qt::DecorationRole); |
|
283 QVariantList variantList; |
|
284 if (decorationRole.canConvert<HbIcon>()) |
|
285 { |
|
286 icon = decorationRole.value<HbIcon>(); |
|
287 } |
|
288 else if (decorationRole.canConvert< QList<QVariant> >()) |
|
289 { |
|
290 variantList = decorationRole.toList(); |
|
291 for (int j = 0; j < variantList.count(); j++) |
|
292 { |
|
293 if (j==0 && variantList.at(0).canConvert<HbIcon>()) |
|
294 { |
|
295 icon = variantList.at(0).value<HbIcon>(); |
|
296 } |
|
297 } |
|
298 } |
|
299 |
|
300 |
|
301 // DisplayRole |
|
302 QVariant displayRole = mDataContainer->data(index, Qt::DisplayRole); |
|
303 QStringList stringList; |
|
304 |
|
305 if (displayRole.canConvert<QString>()) |
|
306 { |
|
307 stringList.append(displayRole.toString()); |
|
308 } |
|
309 else if (displayRole.canConvert<QStringList>()) |
|
310 { |
|
311 stringList = displayRole.toStringList(); |
|
312 } |
|
313 |
|
314 for (int j = 0; j < stringList.count(); j++) |
|
315 { |
|
316 if (j==0) |
|
317 { |
|
318 text = stringList.at(0); |
|
319 } |
|
320 else if (j==1) |
|
321 { |
|
322 valueText = stringList.at(1); |
|
323 } |
|
324 } |
|
325 |
|
326 QVariant variant = mDataContainer->data(index, Qt::UserRole+1); |
|
327 const QMap<QString, QVariant> map = variant.toMap(); |
|
328 QString action = map.value("action").toString(); |
|
329 bool isUnderLine = mContact->isPreferredDetail(action, map.value("detail").value<QContactDetail>()); |
|
330 if (isUnderLine) |
|
331 { |
|
332 mPreferredItems.insert(action, item); |
|
333 } |
|
334 if (action == "call" || action == "message") |
|
335 { |
|
336 item->setDetails(icon, text, valueText, Qt::ElideLeft, isUnderLine); |
|
337 } |
|
338 else |
|
339 { |
|
340 item->setDetails(icon, text, valueText, Qt::ElideRight, isUnderLine); |
|
341 } |
|
342 |
|
343 mContainerLayout->addItem(item); |
|
344 } |
|
345 |
|
346 // separator |
|
347 else if (index == mDataContainer->separatorIndex()) |
|
348 { |
|
349 QVariant displayRole = mDataContainer->data(index, Qt::DisplayRole); |
|
350 if (displayRole.isValid()) |
|
351 { |
|
352 if (displayRole.canConvert<QString>()) |
|
353 { |
|
354 HbGroupBox* details = new HbGroupBox(this); |
|
355 details->setHeading(displayRole.toString()); |
|
356 mContainerLayout->addItem(details); |
|
357 } |
|
358 } |
|
359 } |
|
360 |
|
361 // details |
|
362 else |
|
363 { |
|
364 CntContactCardDetailItem* item = new CntContactCardDetailItem(index, mContainerWidget, false); |
|
365 |
|
366 HbIcon icon(""); |
|
367 QString text; |
|
368 QString valueText; |
|
369 |
|
370 QVariant displayRole = mDataContainer->data(index, Qt::DisplayRole); |
|
371 QStringList stringList; |
|
372 if (displayRole.canConvert<QString>()) |
|
373 { |
|
374 stringList.append(displayRole.toString()); |
|
375 } |
|
376 else if (displayRole.canConvert<QStringList>()) |
|
377 { |
|
378 stringList = displayRole.toStringList(); |
|
379 } |
|
380 |
|
381 for (int j = 0; j < stringList.count(); j++) |
|
382 { |
|
383 if (j==0) |
|
384 { |
|
385 text = stringList.at(0); |
|
386 } |
|
387 else if (j==1) |
|
388 { |
|
389 valueText = stringList.at(1); |
|
390 } |
|
391 } |
|
392 |
|
393 item->setDetails(icon, text, valueText); |
|
394 mContainerLayout->addItem(item); |
|
395 } |
|
396 } |
|
397 } |
|
398 |
|
399 /*! |
|
400 Called after user clicked on the listview. |
|
401 */ |
|
402 void CntContactCardView::onItemActivated() |
|
403 { |
|
404 CntContactCardDetailItem *item = qobject_cast<CntContactCardDetailItem*>(sender()); |
|
405 int index = item->index(); |
|
406 QVariant variant = mDataContainer->data(index, Qt::UserRole+1); |
|
407 const QMap<QString, QVariant> map = variant.toMap(); |
|
408 QString action = map.value("action").toString(); |
|
409 QContactDetail detail = map.value("detail").value<QContactDetail>(); |
|
410 |
|
411 commands()->launchAction(*mContact, detail, action); |
|
412 } |
|
413 |
|
414 /*! |
|
415 Set selected detail as preferred for selected action |
|
416 */ |
|
417 void CntContactCardView::setPreferredAction(const QString &aAction, const QContactDetail &aDetail) |
|
418 { |
|
419 mContact->setPreferredDetail(aAction, aDetail); |
|
420 contactManager()->saveContact(mContact); |
|
421 emit preferredUpdated(); |
|
422 } |
|
423 |
|
424 /*! |
|
425 Called after user longtaps the listview |
|
426 */ |
|
427 void CntContactCardView::onLongPressed(const QPointF &aCoords) |
|
428 { |
|
429 CntContactCardDetailItem *item = qobject_cast<CntContactCardDetailItem*>(sender()); |
|
430 int index = item->index(); |
|
431 QVariant variant = mDataContainer->data(index, Qt::UserRole+1); |
|
432 const QMap<QString, QVariant> map = variant.toMap(); |
|
433 |
|
434 HbMenu *menu = new HbMenu(); |
|
435 HbAction *communicationAction = 0; |
|
436 HbAction *preferredAction = 0; |
|
437 |
|
438 QString action = map.value("action").toString(); |
|
439 |
|
440 if (action.compare("call", Qt::CaseInsensitive) == 0) |
|
441 { |
|
442 communicationAction = menu->addAction(map.value("name").toString()); |
|
443 } |
|
444 else if (action.compare("message", Qt::CaseInsensitive) == 0) |
|
445 { |
|
446 communicationAction = menu->addAction(hbTrId("txt_phob_dblist_send_message")); |
|
447 } |
|
448 else if (action.compare("email", Qt::CaseInsensitive) == 0) |
|
449 { |
|
450 communicationAction = menu->addAction(hbTrId("Send email")); |
|
451 } |
|
452 menu->addSeparator(); |
|
453 |
|
454 preferredAction = menu->addAction(hbTrId("Set as preferred")); |
|
455 if (mContact->isPreferredDetail(action, map.value("detail").value<QContactDetail>())) |
|
456 { |
|
457 preferredAction->setEnabled(false); |
|
458 } |
|
459 |
|
460 //favoriteAction = menu->addAction(hbTrId("Make favorite")); |
|
461 //homeScreenAction = menu->addAction(hbTrId("Place to homescreen")); |
|
462 HbAction *selectedAction = menu->exec(aCoords); |
|
463 |
|
464 if (selectedAction) |
|
465 { |
|
466 if (selectedAction == communicationAction) |
|
467 { |
|
468 commands()->launchAction(*mContact, map.value("detail").value<QContactDetail>(), action); |
|
469 } |
|
470 else if (selectedAction == preferredAction) |
|
471 { |
|
472 setPreferredAction(action, map.value("detail").value<QContactDetail>()); |
|
473 item->setUnderLine(true); |
|
474 |
|
475 if (mPreferredItems.contains(action)) |
|
476 { |
|
477 mPreferredItems.value(action)->setUnderLine(false); |
|
478 } |
|
479 mPreferredItems.insert(action, item); |
|
480 } |
|
481 } |
|
482 menu->deleteLater(); |
|
483 } |
|
484 |
|
485 void CntContactCardView::keyPressEvent(QKeyEvent *event) |
|
486 { |
|
487 if (this == viewManager()->mainWindow()->currentView() && event->key() == Qt::Key_Yes) |
|
488 { |
|
489 event->accept(); |
|
490 QList<QContactActionDescriptor> actionDescriptors = mContact->availableActions(); |
|
491 QStringList availableActions; |
|
492 for (int i = 0;i < actionDescriptors.count();i++) |
|
493 { |
|
494 availableActions << actionDescriptors.at(i).actionName(); |
|
495 } |
|
496 if (availableActions.contains("call", Qt::CaseInsensitive)) |
|
497 { |
|
498 commands()->launchAction(*mContact, QContactDetail(), "call"); |
|
499 } |
|
500 } |
|
501 else |
|
502 { |
|
503 CntBaseView::keyPressEvent(event); |
|
504 } |
|
505 } |
|
506 |
|
507 // end of file |