phonebookui/pbkcommonui/src/cntnamesview_p.cpp
changeset 25 76a2435edfd4
child 27 de1630741fbe
equal deleted inserted replaced
24:0ba2181d7c28 25:76a2435edfd4
       
     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 "cntnamesview_p.h"
       
    19 #include "cntaction.h"
       
    20 #include "qtpbkglobal.h"
       
    21 
       
    22 #include <qapplication.h>
       
    23 #include <hbabstractviewitem.h>
       
    24 #include <hbaction.h>
       
    25 #include <hbnotificationdialog.h>
       
    26 #include <hbmenu.h>
       
    27 #include <hbtoolbar.h>
       
    28 #include <hbmainwindow.h>
       
    29 #include <hbview.h>
       
    30 #include <hbtextitem.h>
       
    31 #include <hbdocumentloader.h>
       
    32 #include <hblistview.h>
       
    33 #include <hblistviewitem.h>
       
    34 #include <hbgroupbox.h>
       
    35 #include <hbsearchpanel.h>
       
    36 #include <hbtoolbar.h>
       
    37 #include <hbframebackground.h>
       
    38 #include <hbstaticvkbhost.h>
       
    39 #include <hbmessagebox.h>
       
    40 
       
    41 #include <QGraphicsLinearLayout>
       
    42 #include <QList>
       
    43 
       
    44 const char *CNT_CONTACTLIST_XML = ":/xml/contacts_namelist.docml";
       
    45 
       
    46 CntNamesViewPrivate::CntNamesViewPrivate() :
       
    47     QObject(),
       
    48     mViewManager(0),
       
    49     mListModel(0),
       
    50     mView(0),
       
    51     mListView(0),
       
    52     mEmptyList(0),
       
    53     mBanner(0),
       
    54     mSearchPanel(0),
       
    55     mLoader(0),
       
    56     mLayout(0),
       
    57     mVirtualKeyboard(0),
       
    58     mMenuBuilder(0),
       
    59     mIsDefault(true),
       
    60     mId( CntViewParameters::namesView )
       
    61 {
       
    62     bool ok;
       
    63     document()->load( CNT_CONTACTLIST_XML, &ok);
       
    64     if (!ok) {
       
    65         qFatal("Unable to read %S", CNT_CONTACTLIST_XML);
       
    66     }
       
    67     mView = static_cast<HbView*> (document()->findWidget("view"));
       
    68 
       
    69     mVirtualKeyboard = new HbStaticVkbHost(mView);
       
    70     connect(mVirtualKeyboard, SIGNAL(keypadOpened()), this, SLOT(handleKeypadOpen()));
       
    71     connect(mVirtualKeyboard, SIGNAL(keypadClosed()), this, SLOT(handleKeypadClose()));
       
    72     
       
    73     mSoftkey = new HbAction(Hb::BackNaviAction, mView);
       
    74        
       
    75     HbAction* newContact = static_cast<HbAction*> (document()->findObject("cnt:newcontact"));
       
    76     HbAction* findContacts = static_cast<HbAction*> (document()->findObject("cnt:find"));
       
    77     HbAction* groups = static_cast<HbAction*> (document()->findObject("cnt:groups"));
       
    78     HbAction* importSim = static_cast<HbAction*> (document()->findObject("cnt:importsim"));
       
    79        
       
    80     connect(mSoftkey, SIGNAL(triggered()), this, SLOT(showPreviousView()));
       
    81     connect(newContact, SIGNAL(triggered()), this, SLOT(createNewContact()));
       
    82     connect(findContacts, SIGNAL(triggered()), this, SLOT(showFinder()));
       
    83     connect(groups, SIGNAL(triggered()), this, SLOT(showCollectionView()));
       
    84     connect( importSim, SIGNAL(triggered()), this, SLOT(importSim()) );
       
    85     connect(list(), SIGNAL(longPressed(HbAbstractViewItem*,QPointF)), this,
       
    86            SLOT(showContextMenu(HbAbstractViewItem*,QPointF)));
       
    87     connect(list(), SIGNAL(activated (const QModelIndex&)), this,
       
    88            SLOT(showContactView(const QModelIndex&)));
       
    89        
       
    90 }
       
    91 
       
    92 CntNamesViewPrivate::~CntNamesViewPrivate()
       
    93 {
       
    94     mView->deleteLater();
       
    95 
       
    96     delete mListModel;
       
    97     mListModel = 0;
       
    98 
       
    99     delete mListView;
       
   100     mListView = 0;
       
   101 
       
   102     delete mSearchPanel;
       
   103     mSearchPanel = 0;
       
   104 
       
   105     delete mEmptyList;
       
   106     mEmptyList = 0;
       
   107 
       
   108     delete mBanner;
       
   109     mBanner = 0;
       
   110 
       
   111     delete mLoader;
       
   112     mLoader = 0;
       
   113 
       
   114     delete mVirtualKeyboard;
       
   115     mVirtualKeyboard = 0;
       
   116     
       
   117     delete mMenuBuilder;
       
   118     mMenuBuilder = 0;
       
   119 }
       
   120 
       
   121 void CntNamesViewPrivate::activate(CntAbstractViewManager* aMgr, const CntViewParameters& aArgs)
       
   122 {
       
   123     mViewManager = aMgr;
       
   124     if (!mListModel) {
       
   125         QContactSortOrder sortOrderFirstName;
       
   126         sortOrderFirstName.setDetailDefinitionName(QContactName::DefinitionName,
       
   127             QContactName::FieldFirst);
       
   128 
       
   129         QContactSortOrder sortOrderLastName;
       
   130         sortOrderLastName.setDetailDefinitionName(QContactName::DefinitionName,
       
   131             QContactName::FieldLast);
       
   132 
       
   133         QList<QContactSortOrder> sortOrders;
       
   134         sortOrders.append(sortOrderFirstName);
       
   135         sortOrders.append(sortOrderLastName);
       
   136 
       
   137         mListModel = new MobCntModel(mViewManager->contactManager(SYMBIAN_BACKEND), QContactFilter(), sortOrders);
       
   138 
       
   139         QContactDetailFilter filter;
       
   140         filter.setDetailDefinitionName(QContactType::DefinitionName, QContactType::FieldType);
       
   141 
       
   142         QString typeContact = QContactType::TypeContact;
       
   143         filter.setValue(typeContact);
       
   144 
       
   145         mListModel->setFilterAndSortOrder(filter);
       
   146         mListModel->showMyCard(true);
       
   147     }
       
   148 
       
   149     list()->setModel(mListModel);
       
   150 
       
   151     mMenuBuilder = new CntActionMenuBuilder( mListModel->myCardId() );
       
   152     connect( mMenuBuilder, SIGNAL(deleteContact(QContact&)), this, SLOT(deleteContact(QContact&)) );
       
   153     connect( mMenuBuilder, SIGNAL(editContact(QContact&)), this, SLOT(showContactEditorView(QContact&)) );
       
   154     connect( mMenuBuilder, SIGNAL(openContact(QContact&)), this, SLOT(showContactView(QContact&)) );
       
   155     connect( mMenuBuilder, SIGNAL(performContactAction(QContact&,QString)), this, SLOT(executeAction(QContact&,QString)));
       
   156         
       
   157     if ( mView->navigationAction() != mSoftkey)
       
   158     {
       
   159         mView->setNavigationAction(mSoftkey);
       
   160     }
       
   161     
       
   162     QContactManager* contactManager = aMgr->contactManager( SYMBIAN_BACKEND );
       
   163     QString action = aArgs.selectedAction();
       
   164     if ( action == "save" )
       
   165     {
       
   166         QString name = contactManager->synthesizedDisplayLabel( aArgs.selectedContact() );
       
   167         HbNotificationDialog::launchDialog(qtTrId("Contact \"%1\" saved").arg(name));
       
   168 
       
   169         scrollTo( aArgs.selectedContact() );
       
   170     }
       
   171            
       
   172     else if ( aArgs.selectedAction() == "delete" )
       
   173     {
       
   174         QString name = contactManager->synthesizedDisplayLabel( aArgs.selectedContact() );
       
   175         HbNotificationDialog::launchDialog(qtTrId("%1 deleted").arg(name));
       
   176     }    
       
   177     else if ( aArgs.selectedAction() == "failed")
       
   178     {
       
   179         HbNotificationDialog::launchDialog(qtTrId("SAVING FAILED!"));
       
   180     }
       
   181 }
       
   182 
       
   183 void CntNamesViewPrivate::deactivate()
       
   184 {
       
   185     hideFinder();
       
   186 
       
   187     // Don't delete the model, default views will retain it between
       
   188     // activate/deactivate. do it in destructor.
       
   189     list()->setModel(NULL);
       
   190     
       
   191     delete mMenuBuilder;
       
   192     mMenuBuilder = 0;
       
   193     
       
   194     // delete the hbsearch since we can not empty text from outside.
       
   195     delete mSearchPanel;
       
   196     mSearchPanel = 0;
       
   197 }
       
   198 
       
   199 void CntNamesViewPrivate::scrollTo(const QContact &aContact)
       
   200 {
       
   201     if (mListModel) 
       
   202     {
       
   203         QModelIndex index = mListModel->indexOfContact(aContact);
       
   204 
       
   205         if (index.isValid()) 
       
   206         {
       
   207             list()->scrollTo(index);
       
   208         }
       
   209     }
       
   210 }
       
   211 void CntNamesViewPrivate::setFilter(const QString &filterString)
       
   212 {
       
   213     QStringList searchList = filterString.split(QRegExp("\\s+"), QString::SkipEmptyParts);
       
   214     QContactDetailFilter filter;
       
   215     filter.setDetailDefinitionName(QContactDisplayLabel::DefinitionName,
       
   216         QContactDisplayLabel::FieldLabel);
       
   217     filter.setMatchFlags(QContactFilter::MatchStartsWith);
       
   218     filter.setValue(searchList);
       
   219 
       
   220     mListModel->setFilterAndSortOrder(filter);
       
   221 
       
   222     if (mListModel->rowCount() == 0) {
       
   223         layout()->removeItem(list());
       
   224         layout()->insertItem(1, emptyLabel());
       
   225         list()->setVisible(false);
       
   226         emptyLabel()->setVisible(true);
       
   227     }
       
   228     else {
       
   229         layout()->removeItem(emptyLabel());
       
   230         layout()->insertItem(1, list());
       
   231         emptyLabel()->setVisible(false);
       
   232         list()->setVisible(true);
       
   233     }
       
   234     mListModel->showMyCard(false);
       
   235 }
       
   236 
       
   237 void CntNamesViewPrivate::handleKeypadOpen()
       
   238 {
       
   239     qreal searchHeight = search()->size().height();
       
   240     qreal bannerHeight = groupBox()->size().height();
       
   241     qreal heightToSet = mView->size().height() - mVirtualKeyboard->keyboardArea().height()
       
   242         - searchHeight - bannerHeight;
       
   243 
       
   244     list()->setMaximumHeight(heightToSet);
       
   245     emptyLabel()->setMaximumHeight(heightToSet);
       
   246 }
       
   247 
       
   248 void CntNamesViewPrivate::handleKeypadClose()
       
   249 {
       
   250     list()->setMaximumHeight(mView->size().height());
       
   251     emptyLabel()->setMaximumHeight(mView->size().height());
       
   252 }
       
   253 
       
   254 void CntNamesViewPrivate::showBanner(const QString aText)
       
   255 {
       
   256     layout()->insertItem(0, groupBox());
       
   257     groupBox()->setHeading(aText);
       
   258     groupBox()->setVisible(true);
       
   259 }
       
   260 
       
   261 void CntNamesViewPrivate::hideBanner()
       
   262 {
       
   263     layout()->removeItem(groupBox());
       
   264     groupBox()->setVisible(false);
       
   265 }
       
   266 
       
   267 void CntNamesViewPrivate::showFinder()
       
   268 {
       
   269     showBanner(hbTrId("txt_phob_subtitle_find_all_contacts"));
       
   270 
       
   271     mView->toolBar()->hide();
       
   272     setFilter(QString());
       
   273 
       
   274     list()->setVisible(true);
       
   275     layout()->addItem(search());
       
   276     search()->setVisible(true);
       
   277 
       
   278     mListModel->showMyCard(false);
       
   279 }
       
   280 
       
   281 void CntNamesViewPrivate::hideFinder()
       
   282 {
       
   283     layout()->removeItem(emptyLabel());
       
   284     layout()->removeItem(search());
       
   285     emptyLabel()->setVisible(false);
       
   286     search()->setVisible(false);
       
   287 
       
   288     hideBanner();
       
   289 
       
   290     layout()->addItem(list());
       
   291     list()->setVisible(true);
       
   292 
       
   293     QContactDetailFilter filter;
       
   294     filter.setDetailDefinitionName(QContactType::DefinitionName, QContactType::FieldType);
       
   295     QString typeContact = QContactType::TypeContact;
       
   296     filter.setValue(typeContact);
       
   297 
       
   298     mListModel->setFilterAndSortOrder(filter);
       
   299     mListModel->showMyCard(true);
       
   300 
       
   301     mView->toolBar()->show();
       
   302 }
       
   303 
       
   304 bool CntNamesViewPrivate::isFinderVisible()
       
   305 {
       
   306     return search()->isVisible();
       
   307 }
       
   308 
       
   309 void CntNamesViewPrivate::showPreviousView()
       
   310 {
       
   311     if ( !isFinderVisible() )
       
   312     {
       
   313         qApp->quit();
       
   314     }
       
   315     hideFinder();
       
   316 }
       
   317 
       
   318 void CntNamesViewPrivate::createNewContact()
       
   319 {
       
   320     QContact newContact;
       
   321     showContactEditorView(newContact);
       
   322 }
       
   323 
       
   324 void CntNamesViewPrivate::showContactView( const QModelIndex& aIndex )
       
   325 {
       
   326     QContact c = mListModel->contact(aIndex);
       
   327     showContactView( c );
       
   328 }
       
   329 
       
   330 void CntNamesViewPrivate::showContactView( QContact& aContact )
       
   331 {
       
   332     CntViewParameters args(CntViewParameters::commLauncherView);
       
   333     if (aContact.localId() == mListModel->myCardId() && aContact.details().count() <= 4)
       
   334     {
       
   335         args.setNextViewId(CntViewParameters::myCardView);
       
   336     }
       
   337 
       
   338     args.setSelectedContact(aContact);
       
   339     mViewManager->changeView(args);
       
   340 }
       
   341 
       
   342 void CntNamesViewPrivate::showContextMenu(HbAbstractViewItem* aItem, QPointF aPoint)
       
   343 {
       
   344     QContact contact = mListModel->contact(aItem->modelIndex());
       
   345     mMenuBuilder->execActionMenu( contact, aPoint );
       
   346 }
       
   347 
       
   348 void CntNamesViewPrivate::executeAction( QContact& aContact, QString aAction )
       
   349 {
       
   350     CntAction* other = new CntAction( aAction );
       
   351     connect(other, SIGNAL(actionExecuted(CntAction*)), this, SLOT(actionExecuted(CntAction*)));
       
   352     other->execute(aContact, QContactDetail());
       
   353 }
       
   354 
       
   355 void CntNamesViewPrivate::actionExecuted(CntAction* aAction)
       
   356 {
       
   357     aAction->deleteLater();
       
   358 }
       
   359 
       
   360 void CntNamesViewPrivate::deleteContact(QContact& aContact)
       
   361 {
       
   362     QContactManager* manager = mViewManager->contactManager( SYMBIAN_BACKEND );
       
   363     QString name = manager->synthesizedDisplayLabel(aContact);
       
   364 
       
   365     HbMessageBox *note = new HbMessageBox(
       
   366             hbTrId("txt_phob_info_delete_1").arg(name),
       
   367             HbMessageBox::MessageTypeQuestion);
       
   368 
       
   369     note->setPrimaryAction(new HbAction(hbTrId("txt_phob_button_delete"), note));
       
   370     note->setSecondaryAction(new HbAction(hbTrId("txt_common_button_cancel"), note));
       
   371 
       
   372     HbAction *selected = note->exec();
       
   373     if (selected == note->primaryAction())
       
   374     {
       
   375         manager->removeContact(aContact.localId());
       
   376         HbNotificationDialog::launchDialog(hbTrId("txt_phob_dpophead_1_deleted").arg(name));
       
   377     }
       
   378     delete note;
       
   379 }
       
   380 
       
   381 void CntNamesViewPrivate::showContactEditorView(QContact& aContact)
       
   382 {
       
   383     CntViewParameters args(CntViewParameters::editView);
       
   384     args.setSelectedContact(aContact);
       
   385 
       
   386     mViewManager->changeView(args);
       
   387 }
       
   388 
       
   389 void CntNamesViewPrivate::showCollectionView()
       
   390 {
       
   391     CntViewParameters args(CntViewParameters::collectionView);
       
   392     mViewManager->changeView(args);
       
   393 }
       
   394 
       
   395 void CntNamesViewPrivate::importSim()
       
   396 {
       
   397     int copied(0);
       
   398     int failed(0);
       
   399     QContactManager* simManager = mViewManager->contactManager( SIM_BACKEND );
       
   400     QContactManager* symbianManager = mViewManager->contactManager( SYMBIAN_BACKEND );
       
   401     
       
   402     QList<QContactLocalId> contactIds = simManager->contactIds();
       
   403     if (contactIds.count() == 0) {
       
   404         HbMessageBox::information("Nothing to copy: SIM card is empty or not accessible.");
       
   405         return;
       
   406     }
       
   407         
       
   408     foreach(QContactLocalId id, contactIds) {
       
   409         QContact contact = simManager->contact(id);
       
   410         if (contact.localId() > 0) {
       
   411             //delete local id before saving to different storage
       
   412             QScopedPointer<QContactId> contactId(new QContactId());
       
   413             contactId->setLocalId(0);
       
   414             contactId->setManagerUri(QString());
       
   415             contact.setId(*contactId);
       
   416 
       
   417             // custom label contains name information, save it to the first name 
       
   418             QList<QContactDetail> names = contact.details(QContactName::DefinitionName);
       
   419             if (names.count() > 0) {
       
   420                 QContactName name = static_cast<QContactName>(names.at(0));
       
   421                 name.setFirstName(name.customLabel());
       
   422                 name.setCustomLabel(QString());
       
   423                 contact.saveDetail(&name);
       
   424             }
       
   425                 
       
   426             if (symbianManager->saveContact(&contact)) {
       
   427                 copied++;
       
   428             }
       
   429             else {
       
   430                 failed++;
       
   431             }
       
   432         }
       
   433         else {
       
   434             failed++;
       
   435         }
       
   436     }
       
   437     
       
   438     QString resultMessage;
       
   439     resultMessage.setNum(copied);
       
   440     resultMessage.append(" contact copied, ");
       
   441     resultMessage.append(QString().setNum(failed));
       
   442     resultMessage.append(" failed.");
       
   443         
       
   444     HbMessageBox::information(resultMessage);
       
   445 }
       
   446 
       
   447 
       
   448 //// lazy accessors
       
   449 HbListView* CntNamesViewPrivate::list()
       
   450 {
       
   451     if (!mListView) {
       
   452         mListView = static_cast<HbListView*> (mLoader->findWidget("listView"));
       
   453         HbListViewItem *prototype = mListView->listItemPrototype();
       
   454         prototype->setGraphicsSize(HbListViewItem::Thumbnail);
       
   455 
       
   456         mListView->setFrictionEnabled(true);
       
   457         mListView->setScrollingStyle(HbScrollArea::PanOrFlick);
       
   458         mListView->listItemPrototype()->setGraphicsSize(HbListViewItem::Thumbnail);
       
   459 
       
   460         HbFrameBackground frame;
       
   461         frame.setFrameGraphicsName("qtg_fr_list_normal");
       
   462         frame.setFrameType(HbFrameDrawer::NinePieces);
       
   463         mListView->itemPrototypes().first()->setDefaultFrame(frame);
       
   464     }
       
   465     return mListView;
       
   466 }
       
   467 
       
   468 HbTextItem* CntNamesViewPrivate::emptyLabel()
       
   469 {
       
   470     if (!mEmptyList) {
       
   471         mEmptyList = new HbTextItem(hbTrId("(no matching contacts)"));
       
   472         mEmptyList->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
       
   473         mEmptyList->setFontSpec(HbFontSpec(HbFontSpec::Primary));
       
   474         mEmptyList->setAlignment(Qt::AlignCenter);
       
   475     }
       
   476     return mEmptyList;
       
   477 }
       
   478 HbGroupBox* CntNamesViewPrivate::groupBox()
       
   479 {
       
   480     if (!mBanner)
       
   481         mBanner = new HbGroupBox();
       
   482     return mBanner;
       
   483 }
       
   484 
       
   485 HbSearchPanel* CntNamesViewPrivate::search()
       
   486 {
       
   487     if (!mSearchPanel) {
       
   488         mSearchPanel = new HbSearchPanel();
       
   489         connect(mSearchPanel, SIGNAL(exitClicked()), this, SLOT(hideFinder()));
       
   490         connect(mSearchPanel, SIGNAL(criteriaChanged(QString)), this, SLOT(setFilter(QString)));
       
   491     }
       
   492     return mSearchPanel;
       
   493 }
       
   494 QGraphicsLinearLayout* CntNamesViewPrivate::layout()
       
   495 {
       
   496     if (!mLayout) {
       
   497         QGraphicsWidget *w = mLoader->findWidget(QString("content"));
       
   498         mLayout = static_cast<QGraphicsLinearLayout*> (w->layout());
       
   499     }
       
   500     return mLayout;
       
   501 }
       
   502 
       
   503 HbDocumentLoader* CntNamesViewPrivate::document()
       
   504 {
       
   505     if (!mLoader) {
       
   506         mLoader = new HbDocumentLoader();
       
   507     }
       
   508     return mLoader;
       
   509 }
       
   510 
       
   511 // End of File