phonebookui/pbkcommonui/src/cntmycardview.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 "cntmycardview.h"
       
    19 #include "cntfetchcontactpopup.h"
       
    20 #include "cntglobal.h"
       
    21 
       
    22 #include <qtcontacts.h>
       
    23 #include <hbpushbutton.h>
       
    24 #include <hbaction.h>
       
    25 #include <hbview.h>
       
    26 #include <hbmainwindow.h>
       
    27 
       
    28 const char *CNT_MYCARD_UI_XML = ":/xml/contacts_mc.docml";
       
    29 
       
    30 CntMyCardView::CntMyCardView() :
       
    31     mContact(NULL),
       
    32     mViewManager(NULL)
       
    33 {
       
    34     bool ok = false;
       
    35     mDocumentLoader.load(CNT_MYCARD_UI_XML, &ok);
       
    36 
       
    37     if (ok)
       
    38     {
       
    39         mView = static_cast<HbView*>(mDocumentLoader.findWidget(QString("view")));
       
    40     }
       
    41     else
       
    42     {
       
    43         qFatal("Unable to read :/xml/contacts_mc.docml");
       
    44     }
       
    45 
       
    46     //back button
       
    47     mSoftkey = new HbAction(Hb::BackNaviAction, mView);
       
    48     connect(mSoftkey, SIGNAL(triggered()), this, SLOT(showPreviousView()));
       
    49 }
       
    50 
       
    51 CntMyCardView::~CntMyCardView()
       
    52 {
       
    53     mView->deleteLater();
       
    54     
       
    55     delete mContact;
       
    56     mContact = NULL;
       
    57 }
       
    58 
       
    59 /*!
       
    60 Activates a previous view
       
    61 */
       
    62 void CntMyCardView::showPreviousView()
       
    63 {
       
    64 	CntViewParameters args;
       
    65     mViewManager->back(args);
       
    66 }
       
    67 
       
    68 /*
       
    69 Activates a default view
       
    70 */
       
    71 void CntMyCardView::activate(const CntViewParameters aArgs)
       
    72 {
       
    73     mViewManager = &mEngine->viewManager();
       
    74     
       
    75     if (mView->navigationAction() != mSoftkey) {
       
    76         mView->setNavigationAction(mSoftkey);
       
    77     }
       
    78     
       
    79     HbMainWindow* window = mView->mainWindow();
       
    80     connect(window, SIGNAL(orientationChanged(Qt::Orientation)), this, SLOT(setOrientation(Qt::Orientation)));
       
    81     setOrientation(window->orientation());
       
    82     
       
    83     mContact = new QContact(aArgs.value(ESelectedContact).value<QContact>());
       
    84     HbPushButton *newButton = static_cast<HbPushButton*>(mDocumentLoader.findWidget(QString("cnt_button_new")));
       
    85     connect(newButton, SIGNAL(clicked()), this, SLOT(openNameEditor()));
       
    86 
       
    87     HbPushButton *chooseButton = static_cast<HbPushButton*>(mDocumentLoader.findWidget(QString("cnt_button_choose")));
       
    88     connect(chooseButton, SIGNAL(clicked()), this, SLOT(openMyCardSelectionView()));
       
    89 
       
    90     QContactDetailFilter filter;
       
    91     filter.setDetailDefinitionName(QContactType::DefinitionName, QContactType::FieldType);
       
    92     filter.setValue(QLatin1String(QContactType::TypeContact));
       
    93 
       
    94     if (mEngine->contactManager( SYMBIAN_BACKEND ).contactIds(filter).isEmpty())
       
    95     {
       
    96         chooseButton->setEnabled(false);
       
    97     }
       
    98 }
       
    99 
       
   100 void CntMyCardView::deactivate()
       
   101 {
       
   102 }
       
   103 
       
   104 void CntMyCardView::setOrientation(Qt::Orientation orientation)
       
   105 {
       
   106     if (orientation == Qt::Vertical) 
       
   107     {
       
   108         // reading "portrait" section
       
   109         mDocumentLoader.load(CNT_MYCARD_UI_XML, "portrait");
       
   110     } 
       
   111     else 
       
   112     {
       
   113         // reading "landscape" section
       
   114         mDocumentLoader.load(CNT_MYCARD_UI_XML, "landscape");
       
   115     }
       
   116 }
       
   117 
       
   118 /*!
       
   119 Opens the name detail editor view
       
   120 */
       
   121 void CntMyCardView::openNameEditor()
       
   122 {
       
   123     CntViewParameters viewParameters;
       
   124     viewParameters.insert(EViewId, editView);
       
   125     viewParameters.insert(EMyCard, "myCard" );
       
   126     
       
   127     QVariant var;
       
   128     var.setValue(*mContact);
       
   129     viewParameters.insert(ESelectedContact, var);
       
   130     viewParameters.insert(EExtraAction, CNT_ROOT_ACTION);
       
   131     mViewManager->changeView(viewParameters);
       
   132 }
       
   133 
       
   134 /*!
       
   135 Opens the my card selection view
       
   136 */
       
   137 void CntMyCardView::openMyCardSelectionView()
       
   138 {
       
   139     CntFetchContactPopup* popup = CntFetchContactPopup::createSingleSelectionPopup(
       
   140             hbTrId("txt_phob_title_select_contact"),
       
   141             mEngine->contactManager(SYMBIAN_BACKEND));
       
   142     connect( popup, SIGNAL(fetchReady(QSet<QContactLocalId>)), this, SLOT(handleMultiCardSelection(QSet<QContactLocalId>)));
       
   143     QSet<QContactLocalId> ids;
       
   144     popup->setSelectedContacts(ids);
       
   145     popup->showPopup();
       
   146 }
       
   147 
       
   148 void CntMyCardView::handleMultiCardSelection( QSet<QContactLocalId> aIds )
       
   149 {
       
   150     QContactManager& manager = mEngine->contactManager( SYMBIAN_BACKEND );
       
   151 
       
   152     if ( !aIds.isEmpty() ) {
       
   153         QList<QContactLocalId> selectedContactsList = aIds.values();
       
   154         QContact contact = manager.contact(selectedContactsList.front());
       
   155         removeFromGroup(&contact);
       
   156         
       
   157         manager.setSelfContactId( contact.localId() );
       
   158         showPreviousView();
       
   159     }
       
   160 }
       
   161 
       
   162 void CntMyCardView::removeFromGroup(const QContact* aContact)
       
   163 {
       
   164     QContactDetailFilter groupFilter;
       
   165     groupFilter.setDetailDefinitionName(QContactType::DefinitionName, QContactType::FieldType);
       
   166     groupFilter.setValue(QLatin1String(QContactType::TypeGroup));
       
   167 
       
   168     QContactManager& mgr = mEngine->contactManager(SYMBIAN_BACKEND);
       
   169     QList<QContactLocalId> groupContactIds = mgr.contactIds(groupFilter);
       
   170     if (!groupContactIds.isEmpty()) {
       
   171         for(int i = 0;i < groupContactIds.count();i++) {
       
   172             QContact groupContact = mgr.contact(groupContactIds.at(i));
       
   173             QContactRelationship relationship;
       
   174             relationship.setRelationshipType(QContactRelationship::HasMember);
       
   175             relationship.setFirst(groupContact.id());
       
   176             relationship.setSecond(aContact->id());
       
   177             mgr.removeRelationship(relationship);  
       
   178         }
       
   179     }
       
   180 }
       
   181 
       
   182 // EOF