phonebookui/pbkcommonui/src/cntcollectionview.cpp
changeset 31 2a11b5b00470
parent 27 de1630741fbe
child 37 fd64c38c277d
equal deleted inserted replaced
27:de1630741fbe 31:2a11b5b00470
    14 * Description:
    14 * Description:
    15 *
    15 *
    16 */
    16 */
    17 
    17 
    18 #include "cntcollectionview.h"
    18 #include "cntcollectionview.h"
       
    19 #include "cntfetchcontactsview.h"
    19 #include "cntgroupselectionpopup.h"
    20 #include "cntgroupselectionpopup.h"
    20 #include "cntgroupdeletepopup.h"
    21 #include "cntgroupdeletepopup.h"
    21 #include "cntcollectionlistmodel.h"
    22 #include "cntcollectionlistmodel.h"
    22 #include "cntextensionmanager.h"
    23 #include "cntextensionmanager.h"
    23 #include "qtpbkglobal.h"
    24 #include "qtpbkglobal.h"
    34 #include <hbaction.h>
    35 #include <hbaction.h>
    35 #include <hbview.h>
    36 #include <hbview.h>
    36 #include <hbframebackground.h>
    37 #include <hbframebackground.h>
    37 #include <hbgroupbox.h>
    38 #include <hbgroupbox.h>
    38 #include <hbmessagebox.h>
    39 #include <hbmessagebox.h>
       
    40 #include <hbparameterlengthlimiter.h>
       
    41 
       
    42 #include <QList>
    39 
    43 
    40 const char *CNT_COLLECTIONVIEW_XML = ":/xml/contacts_collections.docml";
    44 const char *CNT_COLLECTIONVIEW_XML = ":/xml/contacts_collections.docml";
    41 
    45 
    42 /*!
    46 /*!
    43 
    47 
    51     mListView(NULL),
    55     mListView(NULL),
    52     mNamesAction(NULL),
    56     mNamesAction(NULL),
    53     mFindAction(NULL),
    57     mFindAction(NULL),
    54     mExtensionAction(NULL),
    58     mExtensionAction(NULL),
    55     mNewGroupAction(NULL),
    59     mNewGroupAction(NULL),
    56     mDeleteGroupsAction(NULL)
    60     mDeleteGroupsAction(NULL),
       
    61     mHandledContact(NULL),
       
    62     mFetchView(NULL)
    57 {
    63 {
    58     bool ok = false;
    64     bool ok = false;
    59     mDocumentLoader.load(CNT_COLLECTIONVIEW_XML, &ok);
    65     mDocumentLoader.load(CNT_COLLECTIONVIEW_XML, &ok);
    60 
    66 
    61     if (ok)
    67     if (ok)
    89 
    95 
    90 */
    96 */
    91 CntCollectionView::~CntCollectionView()
    97 CntCollectionView::~CntCollectionView()
    92 {
    98 {
    93     mView->deleteLater();
    99     mView->deleteLater();
       
   100     
       
   101     delete mHandledContact;
       
   102     mHandledContact = NULL;
       
   103     
       
   104     delete mFetchView;
       
   105     mFetchView = NULL;
    94 }
   106 }
    95 
   107 
    96 /*!
   108 /*!
    97 Called when activating the view
   109 Called when activating the view
    98 */
   110 */
   107 
   119 
   108     // disable delete group(s) button if only favorites group is present
   120     // disable delete group(s) button if only favorites group is present
   109     QContactDetailFilter groupFilter;
   121     QContactDetailFilter groupFilter;
   110     groupFilter.setDetailDefinitionName(QContactType::DefinitionName, QContactType::FieldType);
   122     groupFilter.setDetailDefinitionName(QContactType::DefinitionName, QContactType::FieldType);
   111     groupFilter.setValue(QLatin1String(QContactType::TypeGroup));
   123     groupFilter.setValue(QLatin1String(QContactType::TypeGroup));
   112     QList<QContactLocalId> groupContactIds = mViewManager->contactManager(SYMBIAN_BACKEND)->contactIds(groupFilter);
   124     QList<QContactLocalId> groupContactIds = getContactManager()->contactIds(groupFilter);
   113     if (groupContactIds.count() < 2)
   125     if (groupContactIds.count() < 2)
   114     {
   126     {
   115         mDeleteGroupsAction->setEnabled(false);
   127         mDeleteGroupsAction->setEnabled(false);
   116     }
   128     }
   117     
   129     
   129     frame.setFrameType(HbFrameDrawer::NinePieces);
   141     frame.setFrameType(HbFrameDrawer::NinePieces);
   130     mListView->itemPrototypes().first()->setDefaultFrame(frame);
   142     mListView->itemPrototypes().first()->setDefaultFrame(frame);
   131     
   143     
   132     mListView->listItemPrototype()->setGraphicsSize(HbListViewItem::LargeIcon);
   144     mListView->listItemPrototype()->setGraphicsSize(HbListViewItem::LargeIcon);
   133     mListView->listItemPrototype()->setStretchingStyle(HbListViewItem::StretchLandscape);
   145     mListView->listItemPrototype()->setStretchingStyle(HbListViewItem::StretchLandscape);
   134     mModel = new CntCollectionListModel(mViewManager->contactManager(SYMBIAN_BACKEND), mExtensionManager, this);
   146     mModel = new CntCollectionListModel(getContactManager(), mExtensionManager, this);
   135     mListView->setModel(mModel);
   147     mListView->setModel(mModel);
       
   148     
       
   149     mFetchView = new CntFetchContacts(mViewManager->contactManager( SYMBIAN_BACKEND ));
       
   150     connect(mFetchView, SIGNAL(clicked()), this, SLOT(handleNewGroupMembers()));
   136 }
   151 }
   137 
   152 
   138 void CntCollectionView::deactivate()
   153 void CntCollectionView::deactivate()
   139 {
   154 {
   140 
   155 
   167         int id = index.data(Qt::UserRole).toInt();
   182         int id = index.data(Qt::UserRole).toInt();
   168         int favoriteGrpId = mModel->favoriteGroupId();
   183         int favoriteGrpId = mModel->favoriteGroupId();
   169 
   184 
   170         if (id == favoriteGrpId )
   185         if (id == favoriteGrpId )
   171         {
   186         {
   172             QContact favoriteGroup = mViewManager->contactManager(SYMBIAN_BACKEND)->contact(favoriteGrpId);
   187             QContact favoriteGroup = getContactManager()->contact(favoriteGrpId);
   173             QContactRelationshipFilter rFilter;
   188             QContactRelationshipFilter rFilter;
   174             rFilter.setRelationshipType(QContactRelationship::HasMember);
   189             rFilter.setRelationshipType(QContactRelationship::HasMember);
   175             rFilter.setRelatedContactRole(QContactRelationship::First);
   190             rFilter.setRelatedContactRole(QContactRelationship::First);
   176             rFilter.setRelatedContactId(favoriteGroup.id());
   191             rFilter.setRelatedContactId(favoriteGroup.id());
   177             // group members and their count
   192             // group members and their count
   178             QList<QContactLocalId> groupMemberIds = mViewManager->contactManager(SYMBIAN_BACKEND)->contactIds(rFilter);
   193             QList<QContactLocalId> groupMemberIds = getContactManager()->contactIds(rFilter);
   179 
   194 
   180             if (groupMemberIds.isEmpty())
   195             if (groupMemberIds.isEmpty())
   181             {
   196             {
   182                 CntViewParameters viewParameters;
   197                 CntViewParameters viewParameters;
   183                 viewParameters.insert(EViewId, collectionFavoritesView);
   198                 viewParameters.insert(EViewId, collectionFavoritesView);
   196                 mViewManager->changeView(viewParameters);
   211                 mViewManager->changeView(viewParameters);
   197             }
   212             }
   198         }
   213         }
   199         else
   214         else
   200         {
   215         {
   201             QContact groupContact = mViewManager->contactManager(SYMBIAN_BACKEND)->contact(id);
   216             QContact groupContact = getContactManager()->contact(id);
   202 
   217 
   203             CntViewParameters viewParameters;
   218             CntViewParameters viewParameters;
   204             viewParameters.insert(EViewId, groupMemberView);
   219             viewParameters.insert(EViewId, groupMemberView);
   205             QVariant var;
   220             QVariant var;
   206             var.setValue(groupContact);
   221             var.setValue(groupContact);
   221         }
   236         }
   222     }
   237     }
   223     else
   238     else
   224     {
   239     {
   225         int id = item->modelIndex().data(Qt::UserRole).toInt();
   240         int id = item->modelIndex().data(Qt::UserRole).toInt();
       
   241         QVariant data( item->modelIndex().row() );
       
   242         
   226         int favoriteGrpId = mModel->favoriteGroupId();
   243         int favoriteGrpId = mModel->favoriteGroupId();
   227 
   244         
   228         HbMenu *menu = new HbMenu();
   245         HbMenu *menu = new HbMenu();
   229         HbAction *openAction = 0;
   246         menu->setAttribute(Qt::WA_DeleteOnClose);
   230         HbAction *deleteAction = 0;
   247         menu->setPreferredPos( coords );
   231 
   248         
   232         openAction = menu->addAction(hbTrId("txt_common_menu_open"));
   249         HbAction* openAction = menu->addAction(hbTrId("txt_common_menu_open"));
       
   250         openAction->setData( data );
   233 
   251 
   234         if (id != favoriteGrpId)
   252         if (id != favoriteGrpId)
   235         {
   253         {
   236             deleteAction = menu->addAction(hbTrId("txt_phob_menu_delete_group"));
   254             HbAction* deleteAction = menu->addAction(hbTrId("txt_phob_menu_delete_group"));
   237         }
   255             deleteAction->setData( data );
   238 
   256         }
   239         HbAction *selectedAction = menu->exec(coords);
   257         menu->open(this, SLOT(handleMenu(HbAction*)));
   240 
   258 
   241         if (selectedAction)
   259     }
   242         {
   260 }
   243             if (selectedAction == openAction)
   261 
   244             {
   262 void CntCollectionView::handleMenu(HbAction* action)
   245                 openGroup(item->modelIndex());
   263 {
   246             }
   264     int row = action->data().toInt();
   247             else if (selectedAction == deleteAction)
   265     HbMenu *menuItem = static_cast<HbMenu*>(sender());
   248             {
   266     QModelIndex index = mModel->index(row, 0);
   249                 QContact groupContact = mViewManager->contactManager(SYMBIAN_BACKEND)->contact(id);
   267     
   250                 deleteGroup(groupContact);
   268     int id = index.data(Qt::UserRole).toInt();
   251             }
   269     
   252         }
   270     if ( action == menuItem->actions().first() )
   253         menu->deleteLater();
   271         {
   254     }
   272         openGroup(index);
   255 }
   273         }
       
   274     else if (action == menuItem->actions().at(1))
       
   275         {
       
   276         
       
   277         QContact groupContact = getContactManager()->contact(id);
       
   278         deleteGroup(groupContact);
       
   279         }
       
   280 }
       
   281 
       
   282 
   256 
   283 
   257 void CntCollectionView::newGroup()
   284 void CntCollectionView::newGroup()
   258 {
   285 {
   259     QString mTextOfNewItem("");
   286     HbInputDialog *popup = new HbInputDialog();
   260     
   287     popup->setAttribute(Qt::WA_DeleteOnClose, true);
   261     HbInputDialog popup;
   288     
   262     
   289     popup->setPromptText(hbTrId("txt_phob_title_new_group_name"));
   263     HbGroupBox *headingLabel = new HbGroupBox(&popup);  
   290     popup->setPrimaryAction(new HbAction(hbTrId("txt_phob_button_create"), popup));
   264     headingLabel->setHeading("txt_phob_title_new_group_name");
   291     popup->setSecondaryAction(new HbAction(hbTrId("txt_common_button_cancel"), popup));
   265     popup.setHeadingWidget(headingLabel);
   292     popup->setInputMode(HbInputDialog::TextInput);
   266     popup.setPrimaryAction(new HbAction(hbTrId("txt_phob_button_create"),&popup));
   293 
   267     popup.setSecondaryAction(new HbAction(hbTrId("txt_common_button_cancel"),&popup));
   294     popup->open(this, SLOT(handleNewGroup(HbAction*)));
   268     popup.setInputMode(HbInputDialog::TextInput);
   295 }
   269     popup.setPromptText("");
   296 
   270     popup.setBackgroundFaded(true);
   297 void CntCollectionView::handleNewGroup(HbAction* action)
   271     HbAction* action = popup.exec();
   298 {
   272     QString text = popup.value().toString();
   299     HbInputDialog *popup = static_cast<HbInputDialog*>(sender());
   273     
   300     
   274     if (action == popup.primaryAction())
   301     if (popup && action == popup->actions().first())
   275     {
   302     {
   276         mTextOfNewItem = text;
   303         QString text = popup->value().toString();
   277         
   304         
   278         QContact groupContact;
   305         mHandledContact = new QContact();
   279         groupContact.setType(QContactType::TypeGroup);
   306         mHandledContact->setType(QContactType::TypeGroup);
   280         
   307         
   281         QContactName groupName;
   308         QContactName groupName;
   282         groupName.setCustomLabel(mTextOfNewItem);
   309         groupName.setCustomLabel(text);
   283         
   310         
   284         groupContact.saveDetail(&groupName);
   311         mHandledContact->saveDetail(&groupName);
   285         mViewManager->contactManager(SYMBIAN_BACKEND)->saveContact(&groupContact);
   312         getContactManager()->saveContact(mHandledContact);
   286         
   313         
   287         // call a dialog to display the contacts
   314         QContactDetailFilter filter;
   288         
   315         QList<QContactLocalId> contactsList = getContactManager()->contactIds(filter);
   289         CntGroupSelectionPopup *groupSelectionPopup = 
   316         QSet<QContactLocalId> contactsSet = contactsList.toSet();
   290             new CntGroupSelectionPopup(mViewManager->contactManager(SYMBIAN_BACKEND), &groupContact);
   317 
   291         groupSelectionPopup->populateListOfContact();
   318         // Select some contact(s) to add to the group
   292         HbAction* action = groupSelectionPopup->exec();
   319         QString groupNameCreated(mHandledContact->displayLabel());
   293         if (action == groupSelectionPopup->primaryAction())
   320         mFetchView->setDetails(HbParameterLengthLimiter(hbTrId("txt_phob_subtitle_1_group")).arg(groupNameCreated),
   294         {
   321                                hbTrId("Save"));
   295             groupSelectionPopup->saveNewGroup();
   322         mFetchView->displayContacts(CntFetchContacts::popup,
   296             
   323                                     HbAbstractItemView::MultiSelection,
   297             CntViewParameters viewParameters;
   324                                     contactsSet);
   298             viewParameters.insert(EViewId, groupMemberView);
   325     }
   299             QVariant var;
   326 }
   300             var.setValue(groupContact);
   327 
   301             viewParameters.insert(ESelectedContact, var);
   328 void CntCollectionView::handleNewGroupMembers()
   302             mViewManager->changeView(viewParameters);
   329 {
   303             
   330     mSelectedContactsSet = mFetchView->getSelectedContacts();
   304             delete groupSelectionPopup;
   331     if ( !mFetchView->wasCanceled() && mSelectedContactsSet.size() ) {
   305         }
   332         saveNewGroup(mHandledContact);
   306         else if (action == groupSelectionPopup->secondaryAction())
   333 
   307         {
   334         CntViewParameters viewParameters;
   308             delete groupSelectionPopup;
   335         viewParameters.insert(EViewId, groupMemberView);
   309             QString groupNameCreated(groupName.customLabel());
   336         QVariant var;
   310             HbNotificationDialog::launchDialog(hbTrId("txt_phob_dpophead_new_group_1_created").arg(groupNameCreated));
   337         var.setValue(*mHandledContact);
   311             //refresh the page 
   338         viewParameters.insert(ESelectedContact, var);
   312             refreshDataModel();
   339         mViewManager->changeView(viewParameters);
   313             mDeleteGroupsAction->setEnabled(true);
   340     }
   314         }
   341     
   315         
   342     QString groupNameCreated(mHandledContact->displayLabel());
   316     }
   343     HbNotificationDialog::launchDialog(HbParameterLengthLimiter(hbTrId("txt_phob_dpophead_new_group_1_created")).arg(groupNameCreated));
       
   344 
       
   345     // Refresh the page 
       
   346     refreshDataModel();
       
   347     mDeleteGroupsAction->setEnabled(true);
       
   348 
       
   349     delete mHandledContact;
       
   350     mHandledContact = NULL;
   317 }
   351 }
   318 
   352 
   319 void CntCollectionView::refreshDataModel()
   353 void CntCollectionView::refreshDataModel()
   320 {
   354 {
   321     mListView->setModel(0);
   355     mListView->setModel(0);
   322     delete mModel;
   356     delete mModel;
   323     mModel = 0;
   357     mModel = 0;
   324     mModel = new CntCollectionListModel(mViewManager->contactManager(SYMBIAN_BACKEND), mExtensionManager, this);
   358     mModel = new CntCollectionListModel(getContactManager(), mExtensionManager, this);
   325     mListView->setModel(mModel);
   359     mListView->setModel(mModel);
   326 }
   360 }
   327 
   361 
   328 void CntCollectionView::deleteGroup(QContact group)
   362 void CntCollectionView::deleteGroup(QContact group)
   329 {
   363 {
   330     QString name = group.displayLabel();
   364     mHandledContact = new QContact(group);
   331 
   365     QString name = mHandledContact->displayLabel();
   332     HbMessageBox *note = new HbMessageBox(hbTrId("txt_phob_info_delete_1").arg(name), HbMessageBox::MessageTypeQuestion);
   366 
   333     note->setPrimaryAction(new HbAction(hbTrId("txt_phob_button_delete"), note));
   367     HbMessageBox::question(HbParameterLengthLimiter(hbTrId("txt_phob_dialog_delete_1_group")).arg(name), this, SLOT(handleDeleteGroup(HbAction*)),
   334     note->setSecondaryAction(new HbAction(hbTrId("txt_common_button_cancel"), note));
   368             hbTrId("txt_phob_button_delete"), hbTrId("txt_common_button_cancel"));
   335     HbAction *selected = note->exec();
   369 }
   336     if (selected == note->primaryAction())
   370 
   337     {
   371 void CntCollectionView::handleDeleteGroup(HbAction* action)
   338         mViewManager->contactManager(SYMBIAN_BACKEND)->removeContact(group.localId());
   372 {
   339         mModel->removeGroup(group.localId());
   373     HbMessageBox *note = static_cast<HbMessageBox*>(sender());
       
   374     
       
   375     if (note && action == note->actions().first())
       
   376     {
       
   377         getContactManager()->removeContact(mHandledContact->localId());
       
   378         mModel->removeGroup(mHandledContact->localId());
   340         
   379         
   341         // disable delete group(s) button if only favorites group is present
   380         // disable delete group(s) button if only favorites group is present
   342         QContactDetailFilter groupFilter;
   381         QContactDetailFilter groupFilter;
   343         groupFilter.setDetailDefinitionName(QContactType::DefinitionName, QContactType::FieldType);
   382         groupFilter.setDetailDefinitionName(QContactType::DefinitionName, QContactType::FieldType);
   344         groupFilter.setValue(QLatin1String(QContactType::TypeGroup));
   383         groupFilter.setValue(QLatin1String(QContactType::TypeGroup));
   345         QList<QContactLocalId> groupContactIds = mViewManager->contactManager(SYMBIAN_BACKEND)->contactIds(groupFilter);
   384         QList<QContactLocalId> groupContactIds = getContactManager()->contactIds(groupFilter);
   346         if (groupContactIds.count() < 2)
   385         if (groupContactIds.count() < 2)
   347         {
   386         {
   348             mDeleteGroupsAction->setEnabled(false);
   387             mDeleteGroupsAction->setEnabled(false);
   349         }
   388         }
   350     }
   389     }
   351     delete note;
   390     
       
   391     delete mHandledContact;
       
   392     mHandledContact = NULL;
   352 }
   393 }
   353 
   394 
   354 void CntCollectionView::deleteGroups()
   395 void CntCollectionView::deleteGroups()
   355 {
   396 {
   356     // save the group here
   397     // save the group here
   357     CntGroupDeletePopup *groupDeletePopup = new CntGroupDeletePopup(mViewManager->contactManager(SYMBIAN_BACKEND));
   398     CntGroupDeletePopup *groupDeletePopup = new CntGroupDeletePopup(getContactManager());
   358     
   399     
   359     groupDeletePopup->populateListOfGroup();
   400     groupDeletePopup->populateListOfGroup();
   360     HbAction* action = groupDeletePopup->exec();
   401     groupDeletePopup->open(this, SLOT(handleDeleteGroups(HbAction*)));
   361     if (action == groupDeletePopup->primaryAction())
   402 
   362     {   
   403 }
   363         groupDeletePopup->deleteGroup();
   404 
   364     }
   405 void CntCollectionView::handleDeleteGroups(HbAction* action)
   365     
   406 {
   366     delete groupDeletePopup;
   407     CntGroupDeletePopup *groupDeletePopup = static_cast<CntGroupDeletePopup*>(sender());
   367     //refresh the page 
   408     
   368     refreshDataModel();
   409     if (groupDeletePopup && action == groupDeletePopup->actions().first())
   369     
   410     {
   370     // disable delete group(s) button if only favorites group is present
   411         QList<QContactLocalId> deletedList = groupDeletePopup->deleteGroup();
   371     QContactDetailFilter groupFilter;
   412         foreach (QContactLocalId id, deletedList)
   372     groupFilter.setDetailDefinitionName(QContactType::DefinitionName, QContactType::FieldType);
   413         {
   373     groupFilter.setValue(QLatin1String(QContactType::TypeGroup));
   414             mModel->removeGroup(id);
   374     QList<QContactLocalId> groupContactIds = mViewManager->contactManager(SYMBIAN_BACKEND)->contactIds(groupFilter);
   415         }
   375     if (groupContactIds.count() < 2)
   416         
   376     {
   417         // disable delete group(s) button if only favorites group is present
   377         mDeleteGroupsAction->setEnabled(false);
   418         QContactDetailFilter groupFilter;
   378     }
   419         groupFilter.setDetailDefinitionName(QContactType::DefinitionName, QContactType::FieldType);
   379 }
   420         groupFilter.setValue(QLatin1String(QContactType::TypeGroup));
       
   421         QList<QContactLocalId> groupContactIds = getContactManager()->contactIds(groupFilter);
       
   422         if (groupContactIds.count() < 2)
       
   423         {
       
   424             mDeleteGroupsAction->setEnabled(false);
       
   425         }
       
   426     }
       
   427 }
       
   428 
       
   429 QContactManager* CntCollectionView::getContactManager()
       
   430 {
       
   431     if (!mViewManager) return NULL;
       
   432 
       
   433     return mViewManager->contactManager(SYMBIAN_BACKEND);
       
   434 }
       
   435 
       
   436 void CntCollectionView::saveNewGroup(QContact* aContact)
       
   437 {
       
   438     if (!aContact) return;
       
   439     
       
   440     // Save the relationship from the selection model of the member selection list
       
   441     QList<QContactLocalId> selectedList = mSelectedContactsSet.toList();
       
   442     for (int i = 0; i < selectedList.size(); i++ ) {
       
   443         QContact contact = getContactManager()->contact(selectedList.at(i));
       
   444         QContactRelationship relationship;
       
   445         relationship.setRelationshipType(QContactRelationship::HasMember);
       
   446         relationship.setFirst(aContact->id());
       
   447         relationship.setSecond(contact.id());
       
   448 
       
   449         // Save relationship
       
   450         getContactManager()->saveRelationship(&relationship);
       
   451     }
       
   452 }
       
   453 
   380 // EOF
   454 // EOF