emailuis/nmailui/src/nmmessagelistview.cpp
branchRCL_3
changeset 63 d189ee25cf9d
equal deleted inserted replaced
61:dcf0eedfc1a3 63:d189ee25cf9d
       
     1 /*
       
     2 * Copyright (c) 2009 - 2010 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 static const char *NMUI_MESSAGE_LIST_VIEW_XML = ":/docml/nmmessagelistview.docml";
       
    19 static const char *NMUI_MESSAGE_LIST_VIEW = "NmMessageListView";
       
    20 static const char *NMUI_MESSAGE_LIST_TREE_LIST = "MessageTreeList";
       
    21 static const char *NMUI_MESSAGE_LIST_NO_MESSAGES = "MessageListNoMessages";
       
    22 static const char *NMUI_MESSAGE_LIST_FOLDER_LABEL = "labelGroupBox";
       
    23 
       
    24 #include "nmuiheaders.h"
       
    25 
       
    26 /*!
       
    27 	\class NmMessageListView
       
    28 	\brief Message list view
       
    29 */
       
    30 
       
    31 /*!
       
    32     Constructor
       
    33 */
       
    34 NmMessageListView::NmMessageListView(
       
    35     NmApplication &application,
       
    36     NmUiStartParam* startParam,
       
    37     NmUiEngine &uiEngine,
       
    38     NmMailboxListModel &mailboxListModel,
       
    39     NmMessageListModel *messageListModel,
       
    40     HbDocumentLoader *documentLoader,
       
    41     QGraphicsItem *parent)
       
    42 : NmBaseView(startParam, application, parent),
       
    43 mApplication(application),
       
    44 mMessageListWidget(NULL),
       
    45 mUiEngine(uiEngine),
       
    46 mMailboxListModel(mailboxListModel),
       
    47 mMessageListModel(messageListModel),
       
    48 mDocumentLoader(documentLoader),
       
    49 mItemContextMenu(NULL),
       
    50 mLongPressedItem(NULL),
       
    51 mNoMessagesLabel(NULL),
       
    52 mFolderLabel(NULL),
       
    53 mViewReady(false),
       
    54 mCurrentFolderType(NmFolderInbox),
       
    55 mSettingsLaunched(false),
       
    56 mPreviousModelCount(0),
       
    57 mIsFirstSyncInMessageList(true)
       
    58 {
       
    59     NM_FUNCTION;
       
    60 
       
    61     loadViewLayout();
       
    62     createToolBar();
       
    63     initTreeView();
       
    64     setMailboxName();
       
    65     setFolderName();
       
    66 }
       
    67 
       
    68 /*!
       
    69     Destructor
       
    70 */
       
    71 NmMessageListView::~NmMessageListView()
       
    72 {
       
    73     NM_FUNCTION;
       
    74 
       
    75     delete mDocumentLoader;
       
    76     mWidgetList.clear();
       
    77     if (mItemContextMenu){
       
    78         mItemContextMenu->clearActions();
       
    79     }
       
    80     delete mItemContextMenu;
       
    81 }
       
    82 
       
    83 /*!
       
    84     View layout loading from XML
       
    85 */
       
    86 void NmMessageListView::loadViewLayout()
       
    87 {
       
    88     NM_FUNCTION;
       
    89 
       
    90     // Use document loader to load the view
       
    91     bool ok(false);
       
    92     setObjectName(QString(NMUI_MESSAGE_LIST_VIEW));
       
    93     QObjectList objectList;
       
    94     objectList.append(this);
       
    95     // Pass the view to documentloader. Document loader uses this view
       
    96     // when docml is parsed, instead of creating new view.
       
    97     if (mDocumentLoader) {
       
    98         mDocumentLoader->setObjectTree(objectList);
       
    99         mWidgetList = mDocumentLoader->load(NMUI_MESSAGE_LIST_VIEW_XML, &ok);
       
   100     }
       
   101 
       
   102     if (ok) {
       
   103         // Get message list widget
       
   104         mMessageListWidget = qobject_cast<HbTreeView *>
       
   105             (mDocumentLoader->findWidget(NMUI_MESSAGE_LIST_TREE_LIST));
       
   106 
       
   107         if (mMessageListWidget) {
       
   108             NM_COMMENT("nmailui: list object loaded");
       
   109 
       
   110             // Set item prototype.
       
   111             mMessageListWidget->setItemPrototype(new NmMessageListViewItem());
       
   112 
       
   113             // Set the list widget properties.
       
   114             mMessageListWidget->setItemRecycling(true);
       
   115             mMessageListWidget->contentWidget()->setProperty("indentation", 0);
       
   116             mMessageListWidget->setScrollDirections(Qt::Vertical);
       
   117             mMessageListWidget->setClampingStyle(HbScrollArea::BounceBackClamping);
       
   118             mMessageListWidget->setFrictionEnabled(true);
       
   119             mMessageListWidget->setItemPixmapCacheEnabled(true);
       
   120             
       
   121             // Enable animations to display an email as soon as it is added to
       
   122             // the list.
       
   123             mMessageListWidget->setEnabledAnimations(HbAbstractItemView::Appear &
       
   124                                                      HbAbstractItemView::Expand);
       
   125         }
       
   126         else {
       
   127             NM_ERROR(1,"nmailui: list object loading failed");
       
   128         }
       
   129 
       
   130         mNoMessagesLabel = qobject_cast<HbLabel *>
       
   131             (mDocumentLoader->findWidget(NMUI_MESSAGE_LIST_NO_MESSAGES));
       
   132         if (mNoMessagesLabel) {
       
   133             mNoMessagesLabel->hide();
       
   134         }
       
   135         else{
       
   136             NM_ERROR(1,"nmailui: (no messages) object loading failed");
       
   137         }
       
   138 
       
   139         mFolderLabel = qobject_cast<HbGroupBox *>(mDocumentLoader->findWidget(NMUI_MESSAGE_LIST_FOLDER_LABEL));
       
   140 
       
   141         // Connect options menu about to show to create options menu function
       
   142         QObject::connect(menu(), SIGNAL(aboutToShow()),
       
   143                          this, SLOT(createOptionsMenu()));
       
   144         QObject::connect(&mUiEngine, SIGNAL(syncStateEvent(NmSyncState, const NmId &)),
       
   145                          this, SLOT(handleSyncStateEvent(NmSyncState, const NmId &)));
       
   146 
       
   147         // Menu needs one dummy item so that aboutToShow signal is emitted.
       
   148         NmAction *dummy = new NmAction(0);
       
   149         menu()->addAction(dummy);
       
   150         // Set sync icon if needed
       
   151         updateSyncIcon();
       
   152     }
       
   153     else {
       
   154         NM_ERROR(1,"nmailui: resource loading failed");
       
   155     }
       
   156 }
       
   157 
       
   158 /*!
       
   159     Lazy loading when view layout has been loaded
       
   160 */
       
   161 void NmMessageListView::viewReady()
       
   162 {
       
   163     NM_FUNCTION;
       
   164 
       
   165     if (!mViewReady){
       
   166         // Refresh list
       
   167         QMetaObject::invokeMethod(this, "refreshList", Qt::QueuedConnection);
       
   168         mViewReady=true;
       
   169     }
       
   170     mSettingsLaunched = false;
       
   171 }
       
   172 
       
   173 /*!
       
   174     Getter for currently displayed folder type
       
   175 */
       
   176 NmFolderType NmMessageListView::folderType()
       
   177 {
       
   178     NM_FUNCTION;
       
   179 
       
   180     return mCurrentFolderType;
       
   181 }
       
   182 
       
   183 /*!
       
   184     okToExitView. Message list view determines whether it is
       
   185     ok to exit view and calls mapplication popview.
       
   186 */
       
   187 void NmMessageListView::okToExitView()
       
   188 {
       
   189     NM_FUNCTION;
       
   190 
       
   191     // Close view if current folder is inbox
       
   192     if (mCurrentFolderType==NmFolderInbox){
       
   193         mApplication.popView();
       
   194     }
       
   195     // Switch to inbox
       
   196     else{
       
   197         mSelectedMailboxId=mStartParam->mailboxId();
       
   198         mSelectedFolderId=mUiEngine.standardFolderId(mSelectedMailboxId,NmFolderInbox);
       
   199         QMetaObject::invokeMethod(this, "folderSelected", Qt::QueuedConnection);
       
   200     }
       
   201 }
       
   202 
       
   203 /*!
       
   204     initTreeView
       
   205 */
       
   206 void NmMessageListView::initTreeView()
       
   207 {
       
   208     NM_FUNCTION;
       
   209 
       
   210     // Get mailbox widget pointer and set parameters
       
   211     if (mMessageListWidget){
       
   212         QObject::connect(mMessageListWidget,
       
   213                 SIGNAL(activated(const QModelIndex &)), this,
       
   214                 SLOT(itemActivated(const QModelIndex &)));
       
   215         QObject::connect(mMessageListWidget,
       
   216                 SIGNAL(longPressed(HbAbstractViewItem*, QPointF)), this,
       
   217                 SLOT(showItemContextMenu(HbAbstractViewItem*, QPointF)));
       
   218         mMessageListWidget->setFocus();
       
   219     }
       
   220 }
       
   221 
       
   222 
       
   223 /*!
       
   224     Reload view contents with new start parameters
       
   225     Typically when view is already open and external view activation occurs
       
   226     for this same view. Startparam ownership is transferred to this view
       
   227 */
       
   228 void NmMessageListView::reloadViewContents(NmUiStartParam* startParam)
       
   229 {
       
   230     NM_FUNCTION;
       
   231 
       
   232     // Check start parameter validity, message view cannot
       
   233     // be updated if given parameter is zero.
       
   234     if (startParam&&startParam->viewId()==NmUiViewMessageList &&
       
   235         startParam->mailboxId() != 0 ) {
       
   236         // Delete existing start parameter data
       
   237         delete mStartParam;
       
   238         mStartParam = NULL;
       
   239         // Store new start parameter data
       
   240         mStartParam = startParam;
       
   241         // Disconnect signals from previous model
       
   242         QObject::disconnect(mMessageListModel, SIGNAL(rowsInserted(const QModelIndex&,int,int)),
       
   243                 this, SLOT(itemsAdded(const QModelIndex&,int,int)));
       
   244         QObject::disconnect(mMessageListModel, SIGNAL(rowsRemoved(const QModelIndex&,int,int)),
       
   245                 this, SLOT(itemsRemoved()));
       
   246         QObject::disconnect(mMessageListModel, SIGNAL(setNewParam(NmUiStartParam*)),
       
   247                 this, SLOT(reloadViewContents(NmUiStartParam*)));
       
   248         // Update model pointer and refresh mailbox with new model
       
   249         mMessageListModel = &mUiEngine.messageListModel(startParam->mailboxId(), startParam->folderId());
       
   250         
       
   251         refreshList();
       
   252         
       
   253         // Refresh the mailbox name
       
   254         setMailboxName();
       
   255         
       
   256         // Store active folder type
       
   257         mCurrentFolderType=NmFolderInbox;
       
   258         if (startParam->folderId()!=0){
       
   259             mCurrentFolderType = mUiEngine.folderTypeById(startParam->mailboxId(),startParam->folderId());        
       
   260         }
       
   261 
       
   262         // Update folder name
       
   263         setFolderName();
       
   264         
       
   265         // Set sync icon if needed
       
   266         updateSyncIcon();
       
   267         
       
   268         // Reconstruct the tool bar. This needed because, for example, the
       
   269         // search button needs the new mailbox ID.
       
   270         createToolBar();
       
   271     }
       
   272     else {
       
   273         NM_ERROR(1,"nmailui: invalid message list start parameter");
       
   274         // Unused start parameter needs to be deleted
       
   275         delete startParam;
       
   276     }
       
   277 }
       
   278 
       
   279 
       
   280 /*!
       
   281     Return view id
       
   282 */
       
   283 NmUiViewId NmMessageListView::nmailViewId() const
       
   284 {
       
   285     NM_FUNCTION;
       
   286 
       
   287     return NmUiViewMessageList;
       
   288 }
       
   289 
       
   290 
       
   291 /*!
       
   292     Refresh the message list.
       
   293 */
       
   294 void NmMessageListView::refreshList()
       
   295 {
       
   296     NM_FUNCTION;
       
   297 
       
   298     if (mMessageListModel) {
       
   299         NmId mailboxId = mStartParam->mailboxId();
       
   300 
       
   301         // In each refresh, e.g. in folder change the UI signals lower layer
       
   302         // about the folder that has been opened.
       
   303         if (mStartParam){
       
   304             mUiEngine.updateActiveFolder(mailboxId, mStartParam->folderId());
       
   305 
       
   306             NmFolderType folderType(NmFolderInbox);
       
   307             if (mStartParam->folderId()!= 0){
       
   308                 folderType = mUiEngine.folderTypeById(mStartParam->mailboxId(),
       
   309                              mStartParam->folderId());
       
   310             }
       
   311 
       
   312             // If the new folder is an inbox, first automatic sync should be shown
       
   313             if (folderType == NmFolderInbox) {
       
   314                 mIsFirstSyncInMessageList = true;
       
   315             }
       
   316         }
       
   317 
       
   318         // Set item model to message list widget
       
   319         if (mMessageListWidget) {
       
   320             mMessageListWidget->setModel(static_cast<QStandardItemModel*>(mMessageListModel));
       
   321 
       
   322             QObject::connect(mMessageListModel, SIGNAL(rowsInserted(const QModelIndex&,int,int)),
       
   323                 this, SLOT(itemsAdded(const QModelIndex&,int,int)),Qt::UniqueConnection);
       
   324             QObject::connect(mMessageListModel, SIGNAL(rowsRemoved(const QModelIndex&,int,int)),
       
   325                 this, SLOT(itemsRemoved()),Qt::UniqueConnection);
       
   326             QObject::connect(mMessageListModel, SIGNAL(setNewParam(NmUiStartParam*)),
       
   327                 this, SLOT(reloadViewContents(NmUiStartParam*)),Qt::UniqueConnection);
       
   328 
       
   329             mPreviousModelCount=mMessageListModel->rowCount();
       
   330 
       
   331             if (mPreviousModelCount == 0) {
       
   332                 showNoMessagesText();
       
   333             }
       
   334             else {
       
   335                 hideNoMessagesText();
       
   336             }
       
   337         }
       
   338 
       
   339         // Notify the mail agent. 
       
   340         NmUiEventsNotifier::notifyViewStateChanged(NmUiEventsNotifier::NmViewShownEvent,
       
   341                                                    NmUiViewMessageList,
       
   342                                                    mStartParam->mailboxId());
       
   343     }
       
   344 }
       
   345 
       
   346 
       
   347 /*!
       
   348     Sync state event handling
       
   349 */
       
   350 void NmMessageListView::handleSyncStateEvent(NmSyncState syncState, const NmId &mailboxId)
       
   351 {
       
   352     NM_FUNCTION;
       
   353     if (mMessageListModel && mailboxId == mMessageListModel->currentMailboxId()) {
       
   354         if (syncState == Synchronizing) {
       
   355              // before first sync inbox id might be zero
       
   356             if (mStartParam->folderId() == 0) {
       
   357                 // after sync inbox id should be updated to correct value
       
   358                 NmId folderId = mUiEngine.standardFolderId(
       
   359                     mStartParam->mailboxId(),
       
   360                     NmFolderInbox);
       
   361                 mStartParam->setFolderId(folderId);
       
   362             }
       
   363             // Show sync icon only for the first automatic sync after opening message list.
       
   364             // Sync icon for manual sync is shown in NmUiEngine::refreshMailbox, not here.
       
   365             if (mIsFirstSyncInMessageList) {
       
   366                 // Show the indicator only if the application is in the foreground
       
   367                 if (mApplication.isForeground()) {
       
   368                     mUiEngine.enableSyncIndicator(true);
       
   369                 }
       
   370                 mIsFirstSyncInMessageList = false;
       
   371             }
       
   372         }
       
   373     }
       
   374 }
       
   375 
       
   376 /*!
       
   377     folder selection handling within current mailbox
       
   378 */
       
   379 void NmMessageListView::folderSelected()
       
   380 {
       
   381     NM_FUNCTION;
       
   382 
       
   383     // Reload view contents with new startparams if mailbox or folder
       
   384     // id is different than current values.
       
   385     if (mStartParam && (mStartParam->mailboxId()!=mSelectedMailboxId||
       
   386                         mStartParam->folderId()!=mSelectedFolderId)){
       
   387         // Create start params
       
   388         NmUiStartParam* startParam = new NmUiStartParam(NmUiViewMessageList,
       
   389                                                         mSelectedMailboxId,
       
   390                                                         mSelectedFolderId);
       
   391         // Store active folder type
       
   392         mCurrentFolderType=NmFolderInbox;
       
   393         if (startParam->folderId()!=0){
       
   394             mCurrentFolderType = mUiEngine.folderTypeById(startParam->mailboxId(),startParam->folderId());        
       
   395         }
       
   396         // Reload view, ownership of the startparams is passed and old startparams
       
   397         // are deleted within reloadViewContents function
       
   398         reloadViewContents(startParam);
       
   399         // Set folder text to status bar
       
   400         setFolderName();
       
   401     }
       
   402 }
       
   403 
       
   404 
       
   405 /*!
       
   406     Long keypress handling
       
   407 */
       
   408 void NmMessageListView::showItemContextMenu(HbAbstractViewItem *listViewItem, const QPointF &coords)
       
   409 {
       
   410     NM_FUNCTION;
       
   411 
       
   412     if (listViewItem) {
       
   413         // Recreate item context menu each time it is called
       
   414         if (mItemContextMenu){
       
   415             mItemContextMenu->clearActions();
       
   416             delete mItemContextMenu;
       
   417             mItemContextMenu=NULL;
       
   418         }
       
   419         mItemContextMenu = new HbMenu();
       
   420         // Store long press item for later use with response
       
   421         mLongPressedItem = mMessageListModel->data(
       
   422                 listViewItem->modelIndex(), Qt::DisplayRole).value<NmMessageListModelItem*>();
       
   423         if (mItemContextMenu && mLongPressedItem && mLongPressedItem->itemType() ==
       
   424             NmMessageListModelItem::NmMessageItemMessage) {
       
   425 
       
   426             // Clear previous items from context menu
       
   427             mItemContextMenu->clearActions();
       
   428             NmUiExtensionManager &extMngr = mApplication.extManager();
       
   429             QList<NmAction*> list;
       
   430             // Fetch items from extension based on item
       
   431             NmMessageEnvelope *envelope = mLongPressedItem->envelopePtr();
       
   432             if (envelope){
       
   433                 NmActionRequest request(this, NmActionContextMenu, NmActionContextViewMessageList,
       
   434                         NmActionContextDataMessage, mStartParam->mailboxId(), mStartParam->folderId(),
       
   435                         envelope->messageId(),QVariant::fromValue(envelope));
       
   436                 extMngr.getActions(request, list);
       
   437                 for (int i=0;i<list.count();i++) {
       
   438                     mItemContextMenu->addAction(list[i]);
       
   439                 }
       
   440 			    mItemContextMenu->setPreferredPos(coords);
       
   441                 mItemContextMenu->open();
       
   442             }
       
   443         }
       
   444     }
       
   445 }
       
   446 
       
   447 /*!
       
   448     Item activation handling. Expand/collapse for title dividers
       
   449     opening for mail items
       
   450 */
       
   451 void NmMessageListView::itemActivated(const QModelIndex &index)
       
   452 {
       
   453     NM_FUNCTION;
       
   454 
       
   455     mActivatedIndex = index;
       
   456     QMetaObject::invokeMethod(this, "handleSelection", Qt::QueuedConnection);
       
   457 }
       
   458 
       
   459 /*!
       
   460     Item activation handling. Expand/collapse for title dividers
       
   461     opening for mail items
       
   462 */
       
   463 void NmMessageListView::handleSelection()
       
   464 {
       
   465     NM_FUNCTION;
       
   466 
       
   467     // Do expand/collapse for title divider items
       
   468     NmMessageListModelItem* modelItem = mMessageListModel->data(
       
   469             mActivatedIndex, Qt::DisplayRole).value<NmMessageListModelItem*>();
       
   470     if (modelItem && modelItem->itemType()==
       
   471         NmMessageListModelItem::NmMessageItemTitleDivider) {
       
   472         if (!mMessageListWidget->isExpanded(mActivatedIndex)) {
       
   473             mMessageListWidget->setExpanded(mActivatedIndex, true);
       
   474             modelItem->setExpanded(true);
       
   475         }
       
   476         else {
       
   477             mMessageListWidget->setExpanded(mActivatedIndex, false);
       
   478             modelItem->setExpanded(false);
       
   479         }
       
   480     }
       
   481     if (modelItem && modelItem->itemType() == NmMessageListModelItem::NmMessageItemMessage
       
   482         && !mSettingsLaunched)
       
   483     {
       
   484         NmFolderType folderType = mUiEngine.folderTypeById(mStartParam->mailboxId(),
       
   485                                   mStartParam->folderId());
       
   486         if (folderType==NmFolderDrafts){
       
   487             NmUiStartParam *startParam = new NmUiStartParam(NmUiViewMessageEditor,
       
   488                 mStartParam->mailboxId(), mStartParam->folderId(),
       
   489                 modelItem->envelope().messageId(),NmUiEditorFromDrafts);
       
   490             mApplication.enterNmUiView(startParam);
       
   491         }
       
   492         else if (folderType!=NmFolderOutbox){
       
   493             NmUiStartParam *startParam = new NmUiStartParam(NmUiViewMessageViewer,
       
   494                 mStartParam->mailboxId(), mStartParam->folderId(),
       
   495                 modelItem->envelope().messageId());
       
   496             mApplication.enterNmUiView(startParam);
       
   497         }
       
   498     }
       
   499 }
       
   500 
       
   501 
       
   502 
       
   503 /*!
       
   504     createOptionsMenu. Functions asks menu commands from extension
       
   505     to be added to options menu.
       
   506 */
       
   507 void NmMessageListView::createOptionsMenu()
       
   508 {
       
   509     NM_FUNCTION;
       
   510 
       
   511     menu()->clearActions();
       
   512     NmActionRequest request(this, NmActionOptionsMenu, NmActionContextViewMessageList,
       
   513     		NmActionContextDataNone, mStartParam->mailboxId(), mStartParam->folderId() );
       
   514     NmUiExtensionManager &extMngr = mApplication.extManager();
       
   515     QList<NmAction*> list;
       
   516     extMngr.getActions(request, list);
       
   517     for (int i=0;i<list.count();i++) {
       
   518         menu()->addAction(list[i]);
       
   519     }
       
   520 }
       
   521 
       
   522 /*!
       
   523     handleActionCommand. From NmMenuObserver, extension manager calls this
       
   524     call to handle menu command in the UI.
       
   525 */
       
   526 void NmMessageListView::handleActionCommand(NmActionResponse &actionResponse)
       
   527 {
       
   528     NM_FUNCTION;
       
   529 
       
   530     // Handle context menu commands here
       
   531     if (actionResponse.menuType() == NmActionOptionsMenu) {
       
   532         switch (actionResponse.responseCommand()) {
       
   533             case NmActionResponseCommandNewMail: {
       
   534                 // Check that given start response has mailbox and folder id's
       
   535                 if (actionResponse.mailboxId()!=0){
       
   536                     NmUiStartParam *startParam = new NmUiStartParam(NmUiViewMessageEditor,
       
   537                             actionResponse.mailboxId(), mStartParam->folderId());
       
   538                     // startParam ownerhips transfers
       
   539                     mApplication.enterNmUiView(startParam);
       
   540                 }
       
   541                 break;
       
   542             }
       
   543             case NmActionResponseCommandUpdateMailboxName: {
       
   544                 setMailboxName();
       
   545                 break;
       
   546             }
       
   547             case NmActionResponseCommandMailboxDeleted: {
       
   548                 mApplication.prepareForPopView();
       
   549 				break;
       
   550             }
       
   551             case NmActionResponseCommandSwitchFolder: {
       
   552                 mSelectedFolderId=actionResponse.folderId();
       
   553                 mSelectedMailboxId=actionResponse.mailboxId();
       
   554                 QMetaObject::invokeMethod(this, "folderSelected", Qt::QueuedConnection);
       
   555                 break;
       
   556             }
       
   557             case NmActionResponseCommandSettings: {
       
   558                 mSettingsLaunched = true;
       
   559                 break;
       
   560             }
       
   561             default: {
       
   562                 break;
       
   563             }
       
   564         }
       
   565     }
       
   566 
       
   567     // Handle context menu commands here
       
   568     else if (actionResponse.menuType()==NmActionContextMenu){
       
   569         switch (actionResponse.responseCommand()){
       
   570            case NmActionResponseCommandOpen:{
       
   571                if (mLongPressedItem){
       
   572                    NmUiStartParam *startParam = new NmUiStartParam(NmUiViewMessageViewer,
       
   573                        mStartParam->mailboxId(), mStartParam->folderId(),
       
   574                        mLongPressedItem->envelope().messageId());
       
   575                    mApplication.enterNmUiView(startParam);
       
   576                    mLongPressedItem=NULL;
       
   577                    }
       
   578                }
       
   579                break;
       
   580            default:
       
   581                break;
       
   582         }
       
   583     }
       
   584 
       
   585     // Handle toolbar commands here
       
   586     else if ( actionResponse.menuType() == NmActionToolbar ) {
       
   587         if ( actionResponse.responseCommand() == NmActionResponseCommandNewMail ) {
       
   588             // Check that given start response has mailbox and folder id's
       
   589             if (actionResponse.mailboxId()!=0){
       
   590                 NmUiStartParam *startParam = new NmUiStartParam(NmUiViewMessageEditor,
       
   591                         actionResponse.mailboxId(), mStartParam->folderId());
       
   592                 // startParam ownerhips transfers
       
   593                 mApplication.enterNmUiView(startParam);
       
   594             }
       
   595         }
       
   596         if (actionResponse.responseCommand() == NmActionResponseCommandSearch) {
       
   597             // Check that the given start response has mailbox and folder IDs.
       
   598             if (actionResponse.mailboxId() != 0) {
       
   599                 NmUiStartParam *startParam =
       
   600                     new NmUiStartParam(NmUiViewMessageSearchList,
       
   601                                        actionResponse.mailboxId(),
       
   602                                        mStartParam->folderId());
       
   603 
       
   604                 // startParam ownership transfers.
       
   605                 mApplication.enterNmUiView(startParam);
       
   606             }
       
   607         }
       
   608     }
       
   609 }
       
   610 
       
   611 
       
   612 /*!
       
   613     Set mailbox name to title
       
   614 */
       
   615 void NmMessageListView::setMailboxName()
       
   616 {
       
   617     NM_FUNCTION;
       
   618 
       
   619     if (mStartParam){
       
   620         NmMailboxMetaData *meta = mUiEngine.mailboxById(mStartParam->mailboxId());
       
   621         if (meta){
       
   622             setTitle(meta->name());
       
   623         }
       
   624     }
       
   625 }
       
   626 
       
   627 /*!
       
   628     createToolBar. Function asks menu commands from extension
       
   629     to be added to toolbar owned by the HbView. Also toolbar
       
   630     specific icons are added in this function.
       
   631 */
       
   632 void NmMessageListView::createToolBar()
       
   633 {
       
   634     NM_FUNCTION;
       
   635 
       
   636     HbToolBar *tb = toolBar();
       
   637     if (!tb) {
       
   638         return;
       
   639     }
       
   640     tb->clearActions();
       
   641     NmActionRequest request(this, NmActionToolbar, NmActionContextViewMessageList,
       
   642             NmActionContextDataNone, mStartParam->mailboxId(), mStartParam->folderId() );
       
   643     NmUiExtensionManager &extMngr = mApplication.extManager();
       
   644     if (!&extMngr) {
       
   645         return;
       
   646     }
       
   647     QList<NmAction *> list;
       
   648     extMngr.getActions(request, list);
       
   649     for (int i = 0; i < list.count(); i++) {
       
   650         tb->addAction(list[i]);
       
   651     }
       
   652 }
       
   653 
       
   654 /*!
       
   655     setFolderName. Function sets folder name to status bar
       
   656 */
       
   657 void NmMessageListView::setFolderName()
       
   658 {
       
   659     NM_FUNCTION;
       
   660 
       
   661     if (mStartParam && mFolderLabel){
       
   662         switch (mCurrentFolderType) {
       
   663         case NmFolderOutbox:
       
   664             {
       
   665             mFolderLabel->setHeading(hbTrId("txt_mail_subhead_outbox"));
       
   666             }
       
   667             break;
       
   668         case NmFolderDrafts:
       
   669             {
       
   670             mFolderLabel->setHeading(hbTrId("txt_mail_subhead_drafts"));
       
   671             }
       
   672             break;
       
   673         case NmFolderSent:
       
   674             {
       
   675             mFolderLabel->setHeading(hbTrId("txt_mail_subhead_sent_items"));
       
   676             }
       
   677             break;
       
   678         case NmFolderDeleted:
       
   679             {
       
   680             mFolderLabel->setHeading(hbTrId("txt_mail_subhead_deleted_items"));
       
   681             }
       
   682             break;
       
   683         case NmFolderInbox:
       
   684         default:
       
   685             {
       
   686             mFolderLabel->setHeading(hbTrId("txt_mail_subhead_inbox"));
       
   687             }
       
   688             break;
       
   689         }
       
   690     }
       
   691 }
       
   692 
       
   693 
       
   694 /*!
       
   695     Handles the addition of a new item. Makes sure the message list widget is
       
   696     visible and keeps the scroll position on the top of the list.
       
   697 
       
   698     \param parent Not used.
       
   699     \param start
       
   700     \param end Not used.
       
   701 */
       
   702 void NmMessageListView::itemsAdded(const QModelIndex &parent, int start, int end)
       
   703 {
       
   704     NM_FUNCTION;
       
   705 
       
   706     Q_UNUSED(parent);
       
   707     Q_UNUSED(end);
       
   708 
       
   709     // Hide no messages if previous model count has been zero
       
   710     // and new items have been added to the list
       
   711     if (mPreviousModelCount==0) {
       
   712         hideNoMessagesText();
       
   713     }
       
   714 
       
   715     // Make sure the top of the list is kept visible by scrolling back to the
       
   716     // top if necessary.
       
   717     if (start == 0 && mMessageListWidget) {
       
   718         QList<HbAbstractViewItem*> items = mMessageListWidget->visibleItems();
       
   719 
       
   720         if (items.count()) {
       
   721             QModelIndex index = items.at(0)->modelIndex();
       
   722 
       
   723             while (index.row() > 0) {
       
   724                 QModelIndex previous =
       
   725                     mMessageListWidget->modelIterator()->previousIndex(index);
       
   726 
       
   727                 if (previous.isValid()) {
       
   728                     mMessageListWidget->scrollTo(previous);
       
   729                 }
       
   730 
       
   731                 index = previous;
       
   732             }
       
   733         }
       
   734     }
       
   735     // Store model count
       
   736     if (mMessageListModel){
       
   737         mPreviousModelCount=mMessageListModel->rowCount();
       
   738     }
       
   739 }
       
   740 
       
   741 
       
   742 /*!
       
   743     Observe items removed
       
   744 */
       
   745 void NmMessageListView::itemsRemoved()
       
   746 {
       
   747     NM_FUNCTION;
       
   748     // Store model count
       
   749     if (mMessageListModel){
       
   750         mPreviousModelCount=mMessageListModel->rowCount();
       
   751     }
       
   752     if (mPreviousModelCount == 0){
       
   753         showNoMessagesText();
       
   754     }
       
   755 }
       
   756 
       
   757 /*!
       
   758     Show "(no messages)" text at the middle of the screen.
       
   759 */
       
   760 void NmMessageListView::showNoMessagesText()
       
   761 {
       
   762     NM_FUNCTION;
       
   763 
       
   764     if (mNoMessagesLabel&&mMessageListWidget) {
       
   765         mMessageListWidget->hide();
       
   766         mNoMessagesLabel->show();
       
   767     }
       
   768 }
       
   769 
       
   770 /*!
       
   771     Hide "(no messages)" text at the middle of the screen.
       
   772 */
       
   773 void NmMessageListView::hideNoMessagesText()
       
   774 {
       
   775     NM_FUNCTION;
       
   776 
       
   777     if (mNoMessagesLabel&&mMessageListWidget) {
       
   778         mNoMessagesLabel->hide();
       
   779         mMessageListWidget->show();
       
   780     }
       
   781 }
       
   782 
       
   783 /*!
       
   784     Updates sync icon based on sync status from the plugin.
       
   785 */
       
   786 void NmMessageListView::updateSyncIcon()
       
   787 {
       
   788     if (mStartParam) {
       
   789         NmSyncState syncState = mUiEngine.syncState(mStartParam->mailboxId());
       
   790         if (syncState==Synchronizing) {
       
   791             mUiEngine.enableSyncIndicator(true);
       
   792         } 
       
   793         else {
       
   794             mUiEngine.enableSyncIndicator(false);
       
   795         }
       
   796     }
       
   797 }
       
   798