phonebookui/pbkcommonui/src/cntgroupmemberview_p.cpp
changeset 54 47627ab5d7a4
child 61 d30183af6ca6
equal deleted inserted replaced
53:e6aff7b69165 54:47627ab5d7a4
       
     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 "cntgroupmemberview_p.h"
       
    19 #include "cntfetchcontactpopup.h"
       
    20 #include "cntcontactcardheadingitem.h"
       
    21 #include "cntglobal.h"
       
    22 #include "cntimagelabel.h"
       
    23 #include "cntimageutility.h"
       
    24 #include <hbnotificationdialog.h>
       
    25 #include <hbmessagebox.h>
       
    26 #include <hbmenu.h>
       
    27 #include <hblistview.h>
       
    28 #include <hblistviewitem.h>
       
    29 #include <hbframebackground.h>
       
    30 #include <hbindexfeedback.h>
       
    31 #include <hbscrollbar.h>
       
    32 #include <hbview.h>
       
    33 #include <hbaction.h>
       
    34 #include <hblabel.h>
       
    35 #include <hbparameterlengthlimiter.h>
       
    36 #include <thumbnailmanager_qt.h>
       
    37 #include <cntlistmodel.h>
       
    38 #include <hbmainwindow.h>
       
    39 #include <xqservicerequest.h>
       
    40 
       
    41 #include "cntdocumentloader.h"
       
    42 
       
    43 const char *CNT_GROUPMEMBERVIEW_XML = ":/xml/contacts_groupmembers.docml";
       
    44 
       
    45 /*!
       
    46 \class CntGroupMemberViewPrivate
       
    47 \brief
       
    48 
       
    49 This is the group members view class that shows list of contacts for a user group. View contains a listview that shows actual contacts that
       
    50 have been added to a particular group. There is also toolbar and menu for navigating between different views. Instance of this class is
       
    51 created by our viewmanager but view itself is owned by the mainwindow which will also delete it in the end.
       
    52 
       
    53 */
       
    54 
       
    55 /*!
       
    56 Constructor, initialize member variables.
       
    57 \a viewManager is the parent that creates this view. \a parent is a pointer to parent QGraphicsItem (by default this is 0)
       
    58 
       
    59 */
       
    60 CntGroupMemberViewPrivate::CntGroupMemberViewPrivate() :
       
    61     mGroupContact(NULL),
       
    62     mViewManager(NULL),
       
    63     mHeadingItem(NULL),
       
    64     mModel(NULL),
       
    65     mImageLabel(NULL), 
       
    66     mListView(NULL),
       
    67     mAvatar(NULL)
       
    68 {
       
    69     mDocument = new CntDocumentLoader;
       
    70     
       
    71     bool ok;
       
    72     mDocument->load( CNT_GROUPMEMBERVIEW_XML, &ok );
       
    73     if ( !ok ){
       
    74         qFatal( "Unable to load %S", CNT_GROUPMEMBERVIEW_XML );
       
    75     }
       
    76 
       
    77     mView = static_cast<HbView*>( mDocument->findWidget("view") );
       
    78     
       
    79     //back button
       
    80     mSoftkey = new HbAction(Hb::BackNaviAction, mView);
       
    81     connect(mSoftkey, SIGNAL(triggered()), this, SLOT(showPreviousView()));
       
    82     
       
    83     mImageLabel = static_cast<CntImageLabel*>(mDocument->findWidget("editViewImage"));
       
    84     connect( mImageLabel, SIGNAL(iconClicked()), this, SLOT(openImageEditor()) );
       
    85     connect( mImageLabel, SIGNAL(iconLongPressed(const QPointF&)), this, SLOT(drawImageMenu(const QPointF&)) );
       
    86     
       
    87     mListView = static_cast<HbListView*>( mDocument->findWidget("listView") );
       
    88     connect(mListView, SIGNAL(longPressed(HbAbstractViewItem*,QPointF)), this,
       
    89         SLOT(showContextMenu(HbAbstractViewItem*,QPointF)));
       
    90     connect(mListView, SIGNAL(activated (const QModelIndex&)), this,
       
    91         SLOT(showContactView(const QModelIndex&)));
       
    92     
       
    93     mHeadingItem = static_cast<CntContactCardHeadingItem*>( mDocument->findWidget("editViewHeading") );
       
    94     connect(mHeadingItem, SIGNAL(passShortPressed(const QPointF&)), this, SLOT(openImageEditor()) );
       
    95     connect(mHeadingItem, SIGNAL(passLongPressed(const QPointF&)), this, SLOT(drawImageMenu(const QPointF&)));
       
    96 
       
    97     // menu actions
       
    98     mEditGroupAction = static_cast<HbAction*>( mDocument->findObject("cnt:editgroupdetails"));
       
    99     connect(mEditGroupAction, SIGNAL(triggered()), this, SLOT(editGroup()));
       
   100     
       
   101     // toolbar actions
       
   102     mManageAction = static_cast<HbAction*>( mDocument->findObject("cnt:managemembers"));
       
   103     connect(mManageAction, SIGNAL(triggered()), this, SLOT(manageMembers()));
       
   104     mDeleteAction = static_cast<HbAction*>( mDocument->findObject("cnt:deletegroup"));
       
   105     connect(mDeleteAction, SIGNAL(triggered()), this, SLOT(deleteGroup()));
       
   106     mShowActionsAction = static_cast<HbAction*>( mDocument->findObject("cnt:groupactions"));
       
   107     connect(mShowActionsAction, SIGNAL(triggered()), this, SLOT(openGroupActions()));
       
   108     
       
   109     // thumbnail manager
       
   110     mThumbnailManager = new ThumbnailManager(this);
       
   111     mThumbnailManager->setMode(ThumbnailManager::Default);
       
   112     mThumbnailManager->setQualityPreference(ThumbnailManager::OptimizeForQuality);
       
   113     mThumbnailManager->setThumbnailSize(ThumbnailManager::ThumbnailLarge);
       
   114    
       
   115     connect(mThumbnailManager, SIGNAL(thumbnailReady(QPixmap, void*, int, int)),
       
   116                this, SLOT(thumbnailReady(QPixmap, void*, int, int)));
       
   117 }
       
   118 
       
   119 /*!
       
   120 Destructor
       
   121 */
       
   122 CntGroupMemberViewPrivate::~CntGroupMemberViewPrivate()
       
   123 {
       
   124     mView->deleteLater();
       
   125     
       
   126     delete mGroupContact;
       
   127     mGroupContact = NULL;
       
   128     
       
   129     delete mModel;
       
   130     mModel = NULL;
       
   131     
       
   132     delete mAvatar;
       
   133     mAvatar = NULL;
       
   134 }
       
   135 
       
   136 void CntGroupMemberViewPrivate::setOrientation(Qt::Orientation orientation)
       
   137 {
       
   138     if (orientation == Qt::Vertical) 
       
   139     {
       
   140         // reading "portrait" section
       
   141         mDocument->load( CNT_GROUPMEMBERVIEW_XML, "portrait" );
       
   142     } 
       
   143     else 
       
   144     {
       
   145         // reading "landscape" section
       
   146         mDocument->load( CNT_GROUPMEMBERVIEW_XML, "landscape" );
       
   147     }
       
   148 }
       
   149 
       
   150 void CntGroupMemberViewPrivate::activate( CntAbstractViewManager* aMgr, const CntViewParameters aArgs )
       
   151 {
       
   152     mViewManager = aMgr;
       
   153     mArgs = aArgs;
       
   154     
       
   155     if (mView->navigationAction() != mSoftkey)
       
   156         {
       
   157         mView->setNavigationAction(mSoftkey);   
       
   158         }
       
   159         
       
   160     QVariant contact = mArgs.value( ESelectedGroupContact );
       
   161     mGroupContact = new QContact( contact.value<QContact>() );
       
   162     
       
   163     HbMainWindow* window = mView->mainWindow();
       
   164     if ( window )
       
   165     {
       
   166         connect(window, SIGNAL(orientationChanged(Qt::Orientation)), this, SLOT(setOrientation(Qt::Orientation)));
       
   167         setOrientation(window->orientation());
       
   168     }
       
   169     
       
   170     mHeadingItem->setGroupDetails(mGroupContact);
       
   171 
       
   172     // avatar
       
   173     QList<QContactAvatar> details = mGroupContact->details<QContactAvatar>();
       
   174     for (int i = 0;i < details.count();i++)
       
   175     {
       
   176         if (details.at(i).imageUrl().isValid())
       
   177             {
       
   178             mAvatar = new QContactAvatar(details.at(i));
       
   179             mThumbnailManager->getThumbnail(mAvatar->imageUrl().toString());
       
   180             break;
       
   181             }
       
   182     }
       
   183     
       
   184     // create list & model
       
   185     mListView->setFrictionEnabled(true);
       
   186     mListView->setScrollingStyle(HbScrollArea::PanWithFollowOn);
       
   187     mListView->verticalScrollBar()->setInteractive(true);
       
   188     mListView->setUniformItemSizes(true);
       
   189     mListView->listItemPrototype()->setGraphicsSize(HbListViewItem::Thumbnail);
       
   190     HbIndexFeedback *indexFeedback = new HbIndexFeedback(mView);
       
   191     indexFeedback->setIndexFeedbackPolicy(HbIndexFeedback::IndexFeedbackSingleCharacter);
       
   192     indexFeedback->setItemView(mListView);
       
   193 
       
   194     HbFrameBackground frame;
       
   195     frame.setFrameGraphicsName("qtg_fr_list_normal");
       
   196     frame.setFrameType(HbFrameDrawer::NinePieces);
       
   197     mListView->itemPrototypes().first()->setDefaultFrame(frame);
       
   198     
       
   199     createModel();
       
   200     
       
   201     if (mArgs.value(ESelectedAction).toString() == CNT_SAVE_ACTION)
       
   202     {
       
   203         QString name = getContactManager()->synthesizedContactDisplayLabel(*mGroupContact);
       
   204         HbNotificationDialog::launchDialog(HbParameterLengthLimiter(hbTrId("txt_phob_dpophead_new_group_1_created").arg(name)));
       
   205     }
       
   206 }
       
   207 
       
   208 void CntGroupMemberViewPrivate::deactivate()
       
   209 {
       
   210 }
       
   211 
       
   212 void CntGroupMemberViewPrivate::showPreviousView()
       
   213 {
       
   214     Q_Q(CntGroupMemberView);
       
   215     
       
   216     emit q->backPressed();
       
   217     
       
   218     //save the contact if avatar has been changed.
       
   219     QContact contact = mViewManager->contactManager( SYMBIAN_BACKEND )->contact(mGroupContact->localId());
       
   220     if ( contact != *mGroupContact )
       
   221     {
       
   222         getContactManager()->saveContact(mGroupContact);
       
   223     }
       
   224     mViewManager->back(mArgs);
       
   225 }
       
   226 
       
   227 void CntGroupMemberViewPrivate::openGroupActions()
       
   228 {
       
   229     QVariant var;
       
   230     var.setValue(*mGroupContact);
       
   231     
       
   232     mArgs.insert(EViewId, groupActionsView);
       
   233     mArgs.insert(ESelectedGroupContact, var);
       
   234     mViewManager->changeView(mArgs);
       
   235 }
       
   236 
       
   237 void CntGroupMemberViewPrivate::manageMembers()
       
   238 {
       
   239     QContactRelationshipFilter membersFilter;
       
   240     membersFilter.setRelationshipType(QContactRelationship::HasMember);
       
   241     membersFilter.setRelatedContactRole(QContactRelationship::First);
       
   242     membersFilter.setRelatedContactId(mGroupContact->id());   
       
   243     
       
   244     mOriginalGroupMembers = getContactManager()->contactIds(membersFilter);
       
   245     
       
   246     QContactName contactName = mGroupContact->detail( QContactName::DefinitionName );
       
   247     QString groupName = contactName.value( QContactName::FieldCustomLabel );
       
   248     if (groupName.isEmpty())
       
   249     {
       
   250         groupName = hbTrId("txt_phob_list_unnamed");
       
   251     }
       
   252     
       
   253     CntFetchContactPopup* popup = CntFetchContactPopup::createMultiSelectionPopup(
       
   254             HbParameterLengthLimiter(hbTrId("txt_phob_title_members_of_1_group")).arg(groupName),
       
   255             hbTrId("txt_common_button_save"),
       
   256             *mViewManager->contactManager(SYMBIAN_BACKEND));
       
   257     connect( popup, SIGNAL(fetchReady(QSet<QContactLocalId>)),this, SLOT(handleManageMembers(QSet<QContactLocalId>)) );
       
   258     popup->setSelectedContacts( mOriginalGroupMembers.toSet() );
       
   259     popup->showPopup();
       
   260 }
       
   261 
       
   262 void CntGroupMemberViewPrivate::handleManageMembers( QSet<QContactLocalId> aIds )
       
   263 {
       
   264     QList<QContactRelationship> removedMemberships;
       
   265     QList<QContactRelationship> addedMemberships;
       
   266 
       
   267     QSet<QContactLocalId> removedMembers = mOriginalGroupMembers.toSet() - aIds;
       
   268     setRelationship(removedMembers, removedMemberships);
       
   269 
       
   270     QSet<QContactLocalId> addedMembers = aIds - mOriginalGroupMembers.toSet();
       
   271     setRelationship(addedMembers, addedMemberships);
       
   272     
       
   273     QMap<int, QContactManager::Error> errors;
       
   274     if (!addedMemberships.isEmpty()) 
       
   275     {
       
   276         getContactManager()->saveRelationships(&addedMemberships, &errors);
       
   277     }
       
   278     
       
   279     if (!removedMemberships.isEmpty()) 
       
   280     {
       
   281         getContactManager()->removeRelationships(removedMemberships, &errors);
       
   282     }
       
   283 }
       
   284 
       
   285 void CntGroupMemberViewPrivate::createModel()
       
   286 {
       
   287     QContactRelationshipFilter rFilter;
       
   288     rFilter.setRelationshipType(QContactRelationship::HasMember);
       
   289     rFilter.setRelatedContactRole(QContactRelationship::First);
       
   290     rFilter.setRelatedContactId(mGroupContact->id());
       
   291 
       
   292     mModel = new CntListModel(getContactManager(), rFilter, false);
       
   293     mListView->setModel(mModel);
       
   294 }
       
   295 
       
   296 void CntGroupMemberViewPrivate::editGroup()
       
   297 {
       
   298     mArgs.insert(EViewId, groupEditorView);
       
   299     QVariant var;
       
   300     var.setValue(*mGroupContact);
       
   301     mArgs.insert(ESelectedGroupContact, var);
       
   302     mViewManager->changeView(mArgs);
       
   303 }
       
   304 
       
   305 void CntGroupMemberViewPrivate::deleteGroup()
       
   306 {
       
   307     QContactName contactName = mGroupContact->detail( QContactName::DefinitionName );
       
   308     QString groupName = contactName.value( QContactName::FieldCustomLabel );
       
   309     if (groupName.isNull())
       
   310     {
       
   311         groupName = hbTrId("txt_phob_list_unnamed");
       
   312     }
       
   313     
       
   314     HbLabel *headingLabel = new HbLabel();
       
   315     headingLabel->setPlainText(HbParameterLengthLimiter(hbTrId("txt_phob_dialog_delete_1_group")).arg(groupName));
       
   316     
       
   317     HbMessageBox::question(hbTrId("txt_phob_dialog_only_group_will_be_removed_contac"), this, SLOT(handleDeleteGroup(int)),
       
   318             HbMessageBox::Delete | HbMessageBox::Cancel, headingLabel);
       
   319 }
       
   320 
       
   321 void CntGroupMemberViewPrivate::handleDeleteGroup(int action)
       
   322 {
       
   323     if (action == HbMessageBox::Delete)
       
   324     {
       
   325         getContactManager()->removeContact(mGroupContact->localId());
       
   326         showPreviousView();
       
   327     }
       
   328 }
       
   329 
       
   330 /*!
       
   331 Called when a list item is longpressed
       
   332 */
       
   333 void CntGroupMemberViewPrivate::showContextMenu(HbAbstractViewItem *aItem, const QPointF &aCoords)
       
   334 {
       
   335     QVariant data( aItem->modelIndex().row() );
       
   336    
       
   337     QModelIndex index = aItem->modelIndex();
       
   338 
       
   339     HbMenu *menu = new HbMenu();
       
   340     menu->setAttribute(Qt::WA_DeleteOnClose);
       
   341     menu->setPreferredPos( aCoords );
       
   342     
       
   343     HbAction *removeFromGroupAction = 0;
       
   344     HbAction *openContactAction = 0;
       
   345     HbAction *editContactAction = 0;
       
   346     HbAction *sendToHsAction = 0;
       
   347 
       
   348     openContactAction = menu->addAction(hbTrId("txt_common_menu_open"));
       
   349     editContactAction = menu->addAction(hbTrId("txt_common_menu_edit"));
       
   350     removeFromGroupAction = menu->addAction(hbTrId("txt_phob_menu_remove_from_group"));
       
   351     sendToHsAction = menu->addAction(hbTrId("txt_phob_menu_send_to_homescreen"));
       
   352     
       
   353     openContactAction->setData( data );
       
   354     editContactAction->setData( data );
       
   355     removeFromGroupAction->setData( data );
       
   356     sendToHsAction->setData( data );
       
   357 
       
   358     menu->open(this, SLOT(handleMenu(HbAction*)));
       
   359 }
       
   360 
       
   361 void CntGroupMemberViewPrivate::handleMenu(HbAction* action)
       
   362 {
       
   363     int row = action->data().toInt();
       
   364     HbMenu *menuItem = static_cast<HbMenu*>(sender());
       
   365     QModelIndex index = mModel->index(row);
       
   366     
       
   367     if ( action == menuItem->actions().first() )
       
   368         {
       
   369         showContactView(index);
       
   370         }
       
   371     else if (action == menuItem->actions().at(1))
       
   372         {
       
   373         editContact(index);
       
   374         }
       
   375     else if (action == menuItem->actions().at(2))
       
   376         {
       
   377         removeFromGroup(index);
       
   378         }
       
   379     else if (action == menuItem->actions().at(3))
       
   380         {
       
   381         sendToHs(index);
       
   382         }
       
   383 }
       
   384 
       
   385 /*!
       
   386 Called after user clicked on the listview.
       
   387 */
       
   388 void CntGroupMemberViewPrivate::sendToHs(const QModelIndex &index)
       
   389 {
       
   390     QVariantHash preferences;
       
   391     preferences["contactId"] = mModel->contact(index).id().localId();
       
   392     
       
   393     XQServiceRequest snd("com.nokia.symbian.IHomeScreenClient",
       
   394                          "addWidget(QString,QVariantHash)",
       
   395                          false);
       
   396     snd << QString("contactwidgethsplugin");
       
   397     snd << preferences;
       
   398     snd.send();
       
   399 }
       
   400 
       
   401 /*!
       
   402 Called after user clicked on the listview.
       
   403 */
       
   404 void CntGroupMemberViewPrivate::showContactView(const QModelIndex &index)
       
   405 {
       
   406     mArgs.insert(EViewId, contactCardView);
       
   407     QVariant var;
       
   408     var.setValue(mModel->contact(index));
       
   409     mArgs.insert(ESelectedContact, var);
       
   410     QVariant varGroup;
       
   411     varGroup.setValue(*mGroupContact);
       
   412     mArgs.insert(ESelectedGroupContact, varGroup);
       
   413     mViewManager->changeView(mArgs);
       
   414         
       
   415 }
       
   416 
       
   417 void CntGroupMemberViewPrivate::removeFromGroup(const QModelIndex &index)
       
   418 {
       
   419     // get contact id using index
       
   420     QContact selectedContact = mModel->contact(index);
       
   421     QContactRelationship relationship;
       
   422     relationship.setRelationshipType(QContactRelationship::HasMember);
       
   423     relationship.setFirst(mGroupContact->id());
       
   424     relationship.setSecond(selectedContact.id());
       
   425     getContactManager()->removeRelationship(relationship);
       
   426 }
       
   427 
       
   428 void CntGroupMemberViewPrivate::editContact(const QModelIndex &index)
       
   429 {
       
   430 
       
   431     mArgs.insert(EViewId, editView);
       
   432     QVariant var;
       
   433     var.setValue(mModel->contact(index));
       
   434     mArgs.insert(ESelectedContact, var);
       
   435     mViewManager->changeView(mArgs);
       
   436 }
       
   437 
       
   438 void CntGroupMemberViewPrivate::thumbnailReady(const QPixmap& pixmap, void *data, int id, int error)
       
   439 {
       
   440     Q_UNUSED(data);
       
   441     Q_UNUSED(id);
       
   442     Q_UNUSED(error);
       
   443     if (!error)
       
   444     {
       
   445         HbIcon icon(pixmap);
       
   446         mHeadingItem->setIcon(icon);
       
   447         
       
   448         mImageLabel->clear();
       
   449         mImageLabel->setIcon(pixmap);
       
   450     }
       
   451 }
       
   452 
       
   453 void CntGroupMemberViewPrivate::openImageEditor()
       
   454 {
       
   455     QVariant var;
       
   456     var.setValue(*mGroupContact);
       
   457        
       
   458     // only group contact is assigned since we want to only
       
   459     // change the groups image
       
   460     mArgs.insert(ESelectedGroupContact, var);
       
   461     
       
   462     mArgs.insert(EViewId, imageEditorView );
       
   463         
       
   464     mViewManager->changeView( mArgs );
       
   465 }
       
   466 
       
   467 QContactManager* CntGroupMemberViewPrivate::getContactManager()
       
   468 {
       
   469     return mViewManager->contactManager(SYMBIAN_BACKEND);
       
   470 }
       
   471 
       
   472 void CntGroupMemberViewPrivate::setRelationship(QSet<QContactLocalId>        &aLocalId,
       
   473                                          QList<QContactRelationship>  &aRelationshipList)
       
   474 {
       
   475     foreach (QContactLocalId id, aLocalId) {
       
   476         QContact contact = getContactManager()->contact(id);
       
   477 
       
   478         QContactRelationship membership;
       
   479         membership.setRelationshipType(QContactRelationship::HasMember);
       
   480         membership.setFirst(mGroupContact->id());
       
   481         membership.setSecond(contact.id());
       
   482         aRelationshipList.append(membership);
       
   483     }
       
   484 }
       
   485 
       
   486 /*!
       
   487 Draw the image specific content menu
       
   488 */
       
   489 void CntGroupMemberViewPrivate::drawImageMenu(const QPointF &aCoords)
       
   490 {
       
   491     HbMenu *menu = new HbMenu();
       
   492     HbAction *changeImageAction = menu->addAction(hbTrId("txt_phob_menu_change_picture"), this, SLOT(openImageEditor()));
       
   493     if (mAvatar && !mAvatar->imageUrl().isEmpty())
       
   494     {
       
   495         HbAction *removeAction = menu->addAction(hbTrId("txt_phob_menu_remove_image"), this, SLOT(removeImage()));
       
   496     }
       
   497     menu->setPreferredPos(aCoords);
       
   498     menu->open();
       
   499 }
       
   500 
       
   501 
       
   502 void CntGroupMemberViewPrivate::removeImage()
       
   503 {
       
   504     if (mAvatar) 
       
   505     {
       
   506         if (!mAvatar->imageUrl().isEmpty())
       
   507         {
       
   508             bool success = mGroupContact->removeDetail(mAvatar);
       
   509             // Check if image removable.
       
   510             CntImageUtility imageUtility;
       
   511             if(imageUtility.isImageRemovable(mAvatar->imageUrl().toString()))
       
   512             {
       
   513                 imageUtility.removeImage(mAvatar->imageUrl().toString());
       
   514             }
       
   515             mAvatar->setImageUrl(QUrl());
       
   516             mImageLabel->clear();
       
   517             mImageLabel->setAvatarIcon(HbIcon("qtg_large_add_group_picture"));
       
   518             mHeadingItem->setIcon(HbIcon("qtg_large_add_group_picture"));
       
   519             getContactManager()->saveContact(mGroupContact);
       
   520        }
       
   521     }
       
   522 }
       
   523 
       
   524 
       
   525 
       
   526 // end of file