phonebookui/pbkcommonui/src/cntfavoritesview.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 "cntfavoritesview.h"
       
    19 #include "cntfetchcontactpopup.h"
       
    20 #include "cntglobal.h"
       
    21 #include <hbpushbutton.h>
       
    22 #include <hbaction.h>
       
    23 #include <hbview.h>
       
    24 #include <hbmainwindow.h>
       
    25 #include <QSet>
       
    26 
       
    27 const char *CNT_FAVORITE_UI_XML = ":/xml/contacts_favorite.docml";
       
    28 
       
    29 CntFavoritesView::CntFavoritesView() :
       
    30     mContact(NULL),
       
    31     mView(NULL),
       
    32     mSoftkey(NULL),
       
    33     mViewManager(NULL)
       
    34 {
       
    35     bool ok = false;
       
    36     mDocumentLoader.load(CNT_FAVORITE_UI_XML, &ok);
       
    37 
       
    38     if (ok)
       
    39     {
       
    40         mView = static_cast<HbView*>(mDocumentLoader.findWidget(QString("favoritesView")));
       
    41     }
       
    42     else
       
    43     {
       
    44         qFatal("Unable to read :/xml/contacts_favorite.docml");
       
    45     }
       
    46     
       
    47     //back button
       
    48     mSoftkey = new HbAction(Hb::BackNaviAction, mView);
       
    49     connect(mSoftkey, SIGNAL(triggered()), this, SLOT(showPreviousView()));
       
    50 }
       
    51 /*!
       
    52 Destructor
       
    53 */
       
    54 CntFavoritesView::~CntFavoritesView()
       
    55 {
       
    56     mView->deleteLater();
       
    57     
       
    58     delete mContact;
       
    59     mContact = NULL;
       
    60 }
       
    61 
       
    62 void CntFavoritesView::activate( const CntViewParameters aArgs )
       
    63 {
       
    64     if (mView->navigationAction() != mSoftkey)
       
    65         mView->setNavigationAction(mSoftkey);
       
    66     
       
    67     HbMainWindow* window = mView->mainWindow();
       
    68     connect(window, SIGNAL(orientationChanged(Qt::Orientation)), this, SLOT(setOrientation(Qt::Orientation)));
       
    69     setOrientation(window->orientation());
       
    70     
       
    71     mContact = new QContact(aArgs.value(ESelectedGroupContact).value<QContact>());
       
    72     mViewManager = &mEngine->viewManager();
       
    73 
       
    74     HbPushButton *addButton = static_cast<HbPushButton*>(mDocumentLoader.findWidget(QString("cnt_button_add")));
       
    75     connect(addButton, SIGNAL(clicked()), this, SLOT(openSelectionPopup()));
       
    76     
       
    77     // If no contacts are present, then disable the button 
       
    78     QContactDetailFilter filter;
       
    79     filter.setDetailDefinitionName(QContactType::DefinitionName, QContactType::FieldType);
       
    80     filter.setValue(QLatin1String(QContactType::TypeContact));
       
    81    
       
    82     QList<QContactLocalId> contactIds = getContactManager()->contactIds(filter);   
       
    83     if (contactIds.isEmpty())
       
    84     {
       
    85         addButton->setEnabled(false); 
       
    86     }
       
    87     
       
    88 }
       
    89 
       
    90 void CntFavoritesView::deactivate()
       
    91 {
       
    92 }
       
    93 
       
    94 void CntFavoritesView::openSelectionPopup()
       
    95 {
       
    96     CntFetchContactPopup* popup = CntFetchContactPopup::createMultiSelectionPopup(
       
    97             hbTrId("txt_phob_subtitle_favorites"), 
       
    98             hbTrId("txt_common_button_save"),
       
    99             *getContactManager());
       
   100     connect(popup, SIGNAL(fetchReady(QSet<QContactLocalId>)), 
       
   101             this, SLOT(handleMemberSelection(QSet<QContactLocalId>)));
       
   102     QSet<QContactLocalId> ids;
       
   103     popup->setSelectedContacts(ids);
       
   104     popup->showPopup();
       
   105 }
       
   106 
       
   107 void CntFavoritesView::handleMemberSelection( QSet<QContactLocalId> aIds )
       
   108 {
       
   109     if ( aIds.isEmpty() )
       
   110     {
       
   111         showPreviousView();
       
   112     }
       
   113     else
       
   114     {
       
   115         QList<QContactRelationship> memberships;
       
   116         foreach (QContactLocalId id, aIds) {
       
   117             QContactId contactId;
       
   118             contactId.setLocalId(id);
       
   119             QContactRelationship membership;
       
   120             membership.setRelationshipType(QContactRelationship::HasMember);
       
   121             membership.setFirst(mContact->id());
       
   122             membership.setSecond(contactId);
       
   123             memberships.append(membership);
       
   124         }
       
   125     
       
   126         if (!memberships.isEmpty()) {
       
   127             getContactManager()->saveRelationships(&memberships, NULL);
       
   128         }
       
   129     
       
   130         CntViewParameters viewParameters;
       
   131         viewParameters.insert(EViewId, favoritesMemberView);
       
   132         QVariant var;
       
   133         var.setValue(*mContact);
       
   134         viewParameters.insert(ESelectedGroupContact, var);
       
   135         mViewManager->changeView(viewParameters);
       
   136     }
       
   137 }
       
   138 
       
   139 void CntFavoritesView::setOrientation(Qt::Orientation orientation)
       
   140 {
       
   141     if (orientation == Qt::Vertical) 
       
   142     {
       
   143         // reading "portrait" section
       
   144         mDocumentLoader.load(CNT_FAVORITE_UI_XML, "portrait");
       
   145     } 
       
   146     else 
       
   147     {
       
   148         // reading "landscape" section
       
   149         mDocumentLoader.load(CNT_FAVORITE_UI_XML, "landscape");
       
   150     }
       
   151 }
       
   152 
       
   153 void CntFavoritesView::showPreviousView()
       
   154 {
       
   155     CntViewParameters args;
       
   156     mViewManager->back(args);
       
   157 }
       
   158 
       
   159 QContactManager* CntFavoritesView::getContactManager()
       
   160 {
       
   161     if (!mViewManager) {
       
   162         return NULL;
       
   163     }
       
   164 
       
   165     return &mEngine->contactManager(SYMBIAN_BACKEND);
       
   166 }
       
   167 
       
   168 // end of file