phonebookui/pbkcommonui/src/cntcollectionview.cpp
branchRCL_3
changeset 62 5b6f26637ad3
equal deleted inserted replaced
58:d4f567ce2e7c 62:5b6f26637ad3
       
     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 "cntcollectionview.h"
       
    19 #include "cntfetchcontactpopup.h"
       
    20 #include "cntgroupdeletepopup.h"
       
    21 #include "cntcollectionlistmodel.h"
       
    22 #include "cntextensionmanager.h"
       
    23 #include "cntglobal.h"
       
    24 #include "cntfavourite.h"
       
    25 #include "cntdetailconst.h"
       
    26 #include "cntdebug.h"
       
    27 #include <cntuiextensionfactory.h>
       
    28 #include <cntuigroupsupplier.h>
       
    29 #include <hblabel.h>
       
    30 #include <hblistview.h>
       
    31 #include <hblistviewitem.h>
       
    32 #include <hbmenu.h>
       
    33 #include <hbdevicenotificationdialog.h>
       
    34 #include <hbinputdialog.h>
       
    35 #include <hbdialog.h>
       
    36 #include <hbaction.h>
       
    37 #include <hbview.h>
       
    38 #include <hbframebackground.h>
       
    39 #include <hbgroupbox.h>
       
    40 #include <hbmessagebox.h>
       
    41 #include <hbparameterlengthlimiter.h>
       
    42 
       
    43 #include <QActionGroup>
       
    44 #include <QApplication>
       
    45 #include <QList>
       
    46 
       
    47 const char *CNT_COLLECTIONVIEW_XML = ":/xml/contacts_collections.docml";
       
    48 
       
    49 /*!
       
    50 
       
    51 */
       
    52 CntCollectionView::CntCollectionView() :
       
    53     mView(NULL),
       
    54     mSoftkey(NULL),
       
    55     mViewManager(NULL),
       
    56     mModel(NULL),
       
    57     mListView(NULL),
       
    58     mNamesAction(NULL),
       
    59     mFindAction(NULL),
       
    60     mExtensionAction(NULL),
       
    61     mNewGroupAction(NULL),
       
    62     mDeleteGroupsAction(NULL),
       
    63     mHandledContact(NULL),
       
    64     mActionGroup(NULL)
       
    65 {
       
    66     bool ok = false;
       
    67     mDocumentLoader.load(CNT_COLLECTIONVIEW_XML, &ok);
       
    68 
       
    69     if (ok)
       
    70     {
       
    71         mView = static_cast<HbView*>(mDocumentLoader.findWidget(QString("view")));
       
    72     }
       
    73     else
       
    74     {
       
    75         qFatal("Unable to read :/xml/contacts_collections.docml");
       
    76     }
       
    77     
       
    78     //back button
       
    79     mSoftkey = new HbAction(Hb::BackNaviAction, mView);
       
    80     connect(mSoftkey, SIGNAL(triggered()), this, SLOT(showPreviousView()));
       
    81     
       
    82     // menu actions
       
    83     mNewGroupAction = static_cast<HbAction*>(mDocumentLoader.findObject("cnt:newgroup"));
       
    84     connect(mNewGroupAction, SIGNAL(triggered()), this, SLOT(newGroup()));
       
    85     mDeleteGroupsAction = static_cast<HbAction*>(mDocumentLoader.findObject("cnt:deletegroups"));
       
    86     connect(mDeleteGroupsAction, SIGNAL(triggered()), this, SLOT(deleteGroups()));
       
    87     
       
    88     // toolbar actions
       
    89     
       
    90     mNamesAction = static_cast<HbAction*>(mDocumentLoader.findObject("cnt:names"));
       
    91     connect(mNamesAction, SIGNAL(triggered()), this, SLOT(showPreviousView()));
       
    92     HbAction* groups = static_cast<HbAction*> (mDocumentLoader.findObject("cnt:groups"));
       
    93     
       
    94     mActionGroup = new QActionGroup(this);
       
    95     groups->setActionGroup(mActionGroup);
       
    96     mNamesAction->setActionGroup(mActionGroup);
       
    97     groups->setChecked(true);
       
    98     
       
    99     mFindAction = static_cast<HbAction*>(mDocumentLoader.findObject("cnt:find"));
       
   100     connect(mFindAction, SIGNAL(triggered()), this, SLOT(showNamesViewWithFinder()));
       
   101     mExtensionAction = static_cast<HbAction*> (mDocumentLoader.findObject("cnt:activity"));
       
   102     
       
   103     connect( qApp, SIGNAL(aboutToQuit()), this, SLOT(notifyNewGroup()));
       
   104 }
       
   105 
       
   106 /*!
       
   107 
       
   108 */
       
   109 CntCollectionView::~CntCollectionView()
       
   110 {
       
   111     mView->deleteLater();
       
   112     
       
   113     delete mHandledContact;
       
   114     mHandledContact = NULL;
       
   115 }
       
   116 
       
   117 /*!
       
   118 Called when activating the view
       
   119 */
       
   120 void CntCollectionView::activate( const CntViewParameters aArgs )
       
   121 {
       
   122     CNT_ENTRY
       
   123     
       
   124     Q_UNUSED(aArgs)
       
   125     
       
   126     if (mView->navigationAction() != mSoftkey)
       
   127         mView->setNavigationAction(mSoftkey);   
       
   128     
       
   129     mViewManager = &mEngine->viewManager();
       
   130     mExtensionManager = &mEngine->extensionManager();
       
   131     
       
   132     // disable delete group(s) button if only favorites group is present
       
   133     QContactDetailFilter groupFilter;
       
   134     groupFilter.setDetailDefinitionName(QContactType::DefinitionName, QContactType::FieldType);
       
   135     groupFilter.setValue(QLatin1String(QContactType::TypeGroup));
       
   136     QList<QContactLocalId> groupContactIds = getContactManager()->contactIds(groupFilter);
       
   137     if (groupContactIds.count() < 2)
       
   138     {
       
   139         mDeleteGroupsAction->setEnabled(false);
       
   140     }
       
   141     
       
   142     // set up the list
       
   143     mListView = static_cast<HbListView*>(mDocumentLoader.findWidget(QString("listView")));
       
   144     mListView->setUniformItemSizes(true);
       
   145     
       
   146     connect(mListView, SIGNAL(activated(const QModelIndex&)), this,
       
   147         SLOT(openGroup(const QModelIndex&)));
       
   148     connect(mListView, SIGNAL(longPressed(HbAbstractViewItem*,QPointF)), this,
       
   149         SLOT(showContextMenu(HbAbstractViewItem*,QPointF)));
       
   150     
       
   151     HbFrameBackground frame;
       
   152     frame.setFrameGraphicsName("qtg_fr_list_normal");
       
   153     frame.setFrameType(HbFrameDrawer::NinePieces);
       
   154     mListView->itemPrototypes().first()->setDefaultFrame(frame);
       
   155     
       
   156     mListView->listItemPrototype()->setGraphicsSize(HbListViewItem::LargeIcon);
       
   157     mListView->listItemPrototype()->setStretchingStyle(HbListViewItem::StretchLandscape);
       
   158     mModel = new CntCollectionListModel( mEngine, this);
       
   159     mListView->setModel(mModel);
       
   160     
       
   161     CNT_EXIT
       
   162 }
       
   163 
       
   164 void CntCollectionView::deactivate()
       
   165 {
       
   166 
       
   167 }
       
   168 
       
   169 /*!
       
   170 Handle view switching request from an extension group item 
       
   171 */
       
   172 void CntCollectionView::openView(CntViewParameters& viewParams)
       
   173 {
       
   174     mViewManager->changeView(viewParams);
       
   175 }
       
   176 
       
   177 /*!
       
   178 Go back to previous view
       
   179 */
       
   180 void CntCollectionView::showPreviousView()
       
   181 {
       
   182     CntViewParameters viewParameters;
       
   183     mViewManager->back(viewParameters);
       
   184 }
       
   185 
       
   186 /*!
       
   187 Go back to previous view
       
   188 */
       
   189 void CntCollectionView::showNamesViewWithFinder()
       
   190 {
       
   191     CntViewParameters viewParameters;
       
   192     viewParameters.insert(EExtraAction, CNT_FIND_ACTION);
       
   193     mViewManager->back(viewParameters);
       
   194 }
       
   195 
       
   196 /*!
       
   197 Called after user clicked on the listview.
       
   198 */
       
   199 void CntCollectionView::openGroup(const QModelIndex &index)
       
   200 {
       
   201     if (mModel->isExtensionGroup(index))
       
   202     {
       
   203         CntViewParameters params = mModel->extensionGroupActivated(index.row());
       
   204         if (params.count())
       
   205         {
       
   206             mViewManager->changeView(params);
       
   207         }
       
   208     }
       
   209     else
       
   210     {
       
   211         int id = index.data(Qt::UserRole).toInt();
       
   212         int favoriteGrpId = CntFavourite::favouriteGroupId(getContactManager());
       
   213 
       
   214         if (id == favoriteGrpId )
       
   215         {
       
   216             QContact favoriteGroup = getContactManager()->contact(favoriteGrpId);
       
   217             QContactRelationshipFilter rFilter;
       
   218             rFilter.setRelationshipType(QContactRelationship::HasMember);
       
   219             rFilter.setRelatedContactRole(QContactRelationship::First);
       
   220             rFilter.setRelatedContactId(favoriteGroup.id());
       
   221             // group members and their count
       
   222             QList<QContactLocalId> groupMemberIds = getContactManager()->contactIds(rFilter);
       
   223 
       
   224             if (groupMemberIds.isEmpty())
       
   225             {
       
   226                 CntViewParameters viewParameters;
       
   227                 viewParameters.insert(EViewId, collectionFavoritesView);
       
   228                 QVariant var;
       
   229                 var.setValue(favoriteGroup);
       
   230                 viewParameters.insert(ESelectedGroupContact, var);
       
   231                 mViewManager->changeView(viewParameters);
       
   232             }
       
   233             else
       
   234             {
       
   235                 CntViewParameters viewParameters;
       
   236                 viewParameters.insert(EViewId, favoritesMemberView);
       
   237                 QVariant var;
       
   238                 var.setValue(favoriteGroup);
       
   239                 viewParameters.insert(ESelectedGroupContact, var);
       
   240                 mViewManager->changeView(viewParameters);
       
   241             }
       
   242         }
       
   243         else
       
   244         {
       
   245             QContact groupContact = getContactManager()->contact(id);
       
   246 
       
   247             CntViewParameters viewParameters;
       
   248             viewParameters.insert(EViewId, groupMemberView);
       
   249             QVariant var;
       
   250             var.setValue(groupContact);
       
   251             viewParameters.insert(ESelectedGroupContact, var);
       
   252             mViewManager->changeView(viewParameters);
       
   253         }
       
   254     }
       
   255 }
       
   256 
       
   257 void CntCollectionView::showContextMenu(HbAbstractViewItem *item, const QPointF &coords)
       
   258 {
       
   259     if (mModel->isExtensionGroup(item->modelIndex()))
       
   260     {
       
   261         mModel->extensionGroupLongPressed(item->modelIndex().row(), coords, this);
       
   262     }
       
   263     else
       
   264     {
       
   265         int id = item->modelIndex().data(Qt::UserRole).toInt();
       
   266         QVariant data( id );
       
   267 
       
   268         int favoriteGrpId = CntFavourite::favouriteGroupId(getContactManager());
       
   269         
       
   270         HbMenu *menu = new HbMenu();
       
   271         menu->setAttribute(Qt::WA_DeleteOnClose);
       
   272         menu->setPreferredPos( coords );
       
   273         
       
   274         HbAction* openAction = menu->addAction(hbTrId("txt_common_menu_open"));
       
   275         openAction->setData( data );
       
   276 
       
   277         if (id != favoriteGrpId)
       
   278         {
       
   279             HbAction* deleteAction = menu->addAction(hbTrId("txt_phob_menu_delete_group"));
       
   280             deleteAction->setData( data );
       
   281         }
       
   282         menu->open(this, SLOT(handleMenu(HbAction*)));
       
   283 
       
   284     }
       
   285 }
       
   286 
       
   287 void CntCollectionView::handleMenu(HbAction* action)
       
   288 {
       
   289     HbMenu *menuItem = static_cast<HbMenu*>(sender());
       
   290     
       
   291     if ( action == menuItem->actions().first() )
       
   292     {
       
   293         int id = action->data().toInt();
       
   294         QModelIndex index = mModel->indexOfGroup(id);
       
   295         openGroup(index);
       
   296     }
       
   297     else if (action == menuItem->actions().at(1))
       
   298     {
       
   299         int id = action->data().toInt();
       
   300         QContact groupContact = getContactManager()->contact(id);
       
   301         deleteGroup(groupContact);
       
   302     }
       
   303 }
       
   304 
       
   305 
       
   306 
       
   307 void CntCollectionView::newGroup()
       
   308 {
       
   309     HbInputDialog *popup = new HbInputDialog();
       
   310     popup->setAttribute(Qt::WA_DeleteOnClose, true);
       
   311     
       
   312     HbLineEdit *lineEdit = popup->lineEdit();
       
   313     lineEdit->setInputMethodHints(Qt::ImhNoPredictiveText);
       
   314     lineEdit->setMaxLength( CNT_GROUPNAME_MAXLENGTH );
       
   315     
       
   316     popup->setPromptText(hbTrId("txt_phob_title_new_group_name"));
       
   317     popup->clearActions();
       
   318     HbAction* primaryAction = new HbAction(hbTrId("txt_phob_button_create"));
       
   319     popup->addAction(primaryAction);
       
   320     
       
   321     HbAction* secondaryAction = new HbAction(hbTrId("txt_common_button_cancel"));
       
   322     popup->addAction(secondaryAction);
       
   323            
       
   324     popup->setInputMode(HbInputDialog::TextInput);
       
   325 
       
   326     popup->open(this, SLOT(handleNewGroup(HbAction*)));
       
   327 }
       
   328 
       
   329 void CntCollectionView::handleNewGroup(HbAction* action)
       
   330 {
       
   331     HbInputDialog *popup = static_cast<HbInputDialog*>(sender());
       
   332     
       
   333     if (popup && action == popup->actions().first())
       
   334     {
       
   335         QString text = popup->value().toString();
       
   336         
       
   337         mHandledContact = new QContact();
       
   338         mHandledContact->setType(QContactType::TypeGroup);
       
   339         
       
   340         QContactName groupName;
       
   341         groupName.setCustomLabel(text);
       
   342         
       
   343         mHandledContact->saveDetail(&groupName);
       
   344         getContactManager()->saveContact(mHandledContact);
       
   345         
       
   346         // Select some contact(s) to add to the group
       
   347         QString groupNameCreated = mHandledContact->displayLabel();
       
   348         if (groupNameCreated.isEmpty())
       
   349         {
       
   350             groupNameCreated = hbTrId("txt_phob_list_unnamed");
       
   351         }
       
   352         
       
   353         // if no contacts are present, then dont call the fetch popup
       
   354        QContactDetailFilter gFilter;
       
   355        gFilter.setDetailDefinitionName(QContactType::DefinitionName, QContactType::FieldType);
       
   356        gFilter.setValue(QLatin1String(QContactType::TypeContact));
       
   357       
       
   358        QList<QContactLocalId> contactIds = getContactManager()->contactIds(gFilter);   
       
   359        if (contactIds.isEmpty())
       
   360        {
       
   361            mModel->addGroup(mHandledContact->localId());
       
   362            mDeleteGroupsAction->setEnabled(true);
       
   363                
       
   364            notifyNewGroup();
       
   365        }      
       
   366        else
       
   367        {
       
   368             CntFetchContactPopup* popup = CntFetchContactPopup::createMultiSelectionPopup(
       
   369                     HbParameterLengthLimiter(hbTrId("txt_phob_title_members_of_1_group")).arg(groupNameCreated),
       
   370                     hbTrId("txt_common_button_save"),
       
   371                     mEngine->contactManager(SYMBIAN_BACKEND));
       
   372             connect( popup, SIGNAL(fetchReady(QSet<QContactLocalId>)), this, SLOT(handleNewGroupMembers(QSet<QContactLocalId>)) );
       
   373             connect( popup, SIGNAL(fetchCancelled()), this, SLOT(handleCancelGroupMembers()) );
       
   374             
       
   375             QSet<QContactLocalId> ids;
       
   376             popup->setSelectedContacts(ids);
       
   377             popup->showPopup();
       
   378        }
       
   379     }
       
   380 }
       
   381 
       
   382 void CntCollectionView::handleNewGroupMembers( QSet<QContactLocalId> aIds )
       
   383 {
       
   384     mSelectedContactsSet = aIds;
       
   385 
       
   386     if ( aIds.size() > 0 )
       
   387     {
       
   388         saveNewGroup(mHandledContact);
       
   389         
       
   390         CntViewParameters viewParameters;
       
   391         viewParameters.insert(EViewId, groupMemberView);
       
   392         
       
   393         QVariant var;
       
   394         var.setValue(*mHandledContact);
       
   395         viewParameters.insert(ESelectedGroupContact, var);
       
   396         mViewManager->changeView(viewParameters);
       
   397     }
       
   398     else
       
   399     {
       
   400         mModel->addGroup(mHandledContact->localId());
       
   401         mDeleteGroupsAction->setEnabled(true);
       
   402     }
       
   403     notifyNewGroup();
       
   404 }
       
   405 
       
   406 void CntCollectionView::handleCancelGroupMembers()
       
   407 {
       
   408     mSelectedContactsSet.clear();
       
   409     mModel->addGroup(mHandledContact->localId());
       
   410     mDeleteGroupsAction->setEnabled(true);
       
   411     
       
   412     notifyNewGroup();
       
   413 }
       
   414 
       
   415 void CntCollectionView::notifyNewGroup()
       
   416 {
       
   417     if (mHandledContact != NULL)
       
   418     {
       
   419         QString groupNameCreated = mHandledContact->displayLabel();
       
   420         if (groupNameCreated.isEmpty())
       
   421         {
       
   422             groupNameCreated = hbTrId("txt_phob_list_unnamed");
       
   423         }
       
   424         HbDeviceNotificationDialog::notification(QString(), 
       
   425                 HbParameterLengthLimiter(hbTrId("txt_phob_dpophead_new_group_1_created")).arg(groupNameCreated));
       
   426 
       
   427         delete mHandledContact;
       
   428         mHandledContact = NULL;
       
   429     }
       
   430 }
       
   431 
       
   432 void CntCollectionView::refreshDataModel()
       
   433 {
       
   434     mListView->setModel(0);
       
   435     
       
   436     delete mModel;
       
   437     mModel = NULL;
       
   438     
       
   439     mModel = new CntCollectionListModel(mEngine, this);
       
   440     mListView->setModel(mModel);
       
   441 }
       
   442 
       
   443 void CntCollectionView::deleteGroup(QContact group)
       
   444 {
       
   445     mHandledContact = new QContact(group);
       
   446     QString name = mHandledContact->displayLabel();
       
   447     if (name.isEmpty())
       
   448     {
       
   449         name = hbTrId("txt_phob_list_unnamed");
       
   450     }
       
   451 
       
   452     HbLabel *headingLabel = new HbLabel();
       
   453     headingLabel->setPlainText(HbParameterLengthLimiter(hbTrId("txt_phob_dialog_delete_1_group")).arg(name));
       
   454           
       
   455     HbMessageBox::question(hbTrId("txt_phob_dialog_only_group_will_be_removed_contac")
       
   456             , this, SLOT(handleDeleteGroup(int)), HbMessageBox::Delete | HbMessageBox::Cancel,
       
   457                 headingLabel);
       
   458 }
       
   459 
       
   460 void CntCollectionView::handleDeleteGroup(int action)
       
   461 {
       
   462     if (action == HbMessageBox::Delete)
       
   463     {
       
   464         getContactManager()->removeContact(mHandledContact->localId());
       
   465         mModel->removeGroup(mHandledContact->localId());
       
   466         
       
   467         // disable delete group(s) button if only favorites group is present
       
   468         QContactDetailFilter groupFilter;
       
   469         groupFilter.setDetailDefinitionName(QContactType::DefinitionName, QContactType::FieldType);
       
   470         groupFilter.setValue(QLatin1String(QContactType::TypeGroup));
       
   471         QList<QContactLocalId> groupContactIds = getContactManager()->contactIds(groupFilter);
       
   472         if (groupContactIds.count() < 2)
       
   473         {
       
   474             mDeleteGroupsAction->setEnabled(false);
       
   475         }
       
   476     }
       
   477     
       
   478     delete mHandledContact;
       
   479     mHandledContact = NULL;
       
   480 }
       
   481 
       
   482 void CntCollectionView::deleteGroups()
       
   483 {
       
   484     // save the group here
       
   485     CntGroupDeletePopup *groupDeletePopup = new CntGroupDeletePopup( mEngine );
       
   486     
       
   487     groupDeletePopup->populateListOfGroup();
       
   488     groupDeletePopup->open(this, SLOT(handleDeleteGroups(HbAction*)));
       
   489 
       
   490 }
       
   491 
       
   492 void CntCollectionView::handleDeleteGroups(HbAction* action)
       
   493 {
       
   494     CntGroupDeletePopup *groupDeletePopup = static_cast<CntGroupDeletePopup*>(sender());
       
   495     
       
   496     if (groupDeletePopup && action == groupDeletePopup->actions().first())
       
   497     {
       
   498         QList<QContactLocalId> deletedList = groupDeletePopup->deleteGroup();
       
   499         foreach (QContactLocalId id, deletedList)
       
   500         {
       
   501             mModel->removeGroup(id);
       
   502         }
       
   503         
       
   504         // disable delete group(s) button if only favorites group is present
       
   505         QContactDetailFilter groupFilter;
       
   506         groupFilter.setDetailDefinitionName(QContactType::DefinitionName, QContactType::FieldType);
       
   507         groupFilter.setValue(QLatin1String(QContactType::TypeGroup));
       
   508         QList<QContactLocalId> groupContactIds = getContactManager()->contactIds(groupFilter);
       
   509         if (groupContactIds.count() < 2)
       
   510         {
       
   511             mDeleteGroupsAction->setEnabled(false);
       
   512         }
       
   513     }
       
   514 }
       
   515 
       
   516 QContactManager* CntCollectionView::getContactManager()
       
   517 {
       
   518     if (!mEngine) {
       
   519         return NULL;
       
   520     }
       
   521     return &mEngine->contactManager( SYMBIAN_BACKEND );
       
   522 }
       
   523 
       
   524 void CntCollectionView::saveNewGroup(QContact* aGroup)
       
   525 {
       
   526     if (!aGroup) {
       
   527         return;
       
   528     }
       
   529     
       
   530     // Save the relationship from the selection model of the member selection list
       
   531     QList<QContactRelationship> relationships;
       
   532     foreach (QContactLocalId localId, mSelectedContactsSet)
       
   533     {
       
   534         QContactRelationship relationship;
       
   535         QContactId id;
       
   536         id.setLocalId(localId);
       
   537         relationship.setRelationshipType(QContactRelationship::HasMember);
       
   538         relationship.setFirst(aGroup->id());
       
   539         relationship.setSecond(id);
       
   540         relationships.append(relationship);
       
   541     }
       
   542 
       
   543     // Save relationships
       
   544     if (!relationships.isEmpty()) {
       
   545         getContactManager()->saveRelationships(&relationships, NULL);
       
   546     }
       
   547 }
       
   548 
       
   549 // EOF