emailuis/nmailui/src/nmmailboxselectiondialog.cpp
changeset 18 578830873419
child 23 2dc6caa42ec3
equal deleted inserted replaced
4:e7aa27f58ae1 18:578830873419
       
     1 /*
       
     2 * Copyright (c) 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 #include "nmuiheaders.h"
       
    19 
       
    20 static const char *NMUI_MAILBOX_SELECTION_DIALOG_XML = ":/docml/nmmailboxselectiondialog.docml";
       
    21 static const char *NMUI_MAILBOX_LIST_VIEW = "mailboxListView";
       
    22 static const char *NMUI_MAILBOX_SELECTION_DIALOG = "mailboxSelectionDialog";
       
    23 
       
    24 
       
    25 /*!
       
    26     \class NmMailboxSelectionDialog
       
    27     \brief Selection dialog for the mailboxes. Displays a dialog from which the
       
    28            user can select a mailbox. The dialog can be cancelled by pressing
       
    29            the cancel button.
       
    30 */
       
    31 
       
    32 
       
    33 /*!
       
    34      Class constructor.
       
    35 */
       
    36 NmMailboxSelectionDialog::NmMailboxSelectionDialog(
       
    37     NmMailboxListModel &mailboxListModel, QGraphicsItem *parent /* = 0 */)
       
    38     : mMailboxListView(NULL),
       
    39       mMailboxSelectionDialog(NULL),
       
    40       mContentItemModel(NULL),
       
    41       mMailboxListModel(mailboxListModel),
       
    42       mParent(parent),
       
    43       mMailboxId(0)
       
    44 {
       
    45     // No implementation required.
       
    46 }
       
    47 
       
    48 
       
    49 /*!
       
    50     Class destructor.
       
    51 */
       
    52 NmMailboxSelectionDialog::~NmMailboxSelectionDialog()
       
    53 {
       
    54     delete mContentItemModel;
       
    55     delete mMailboxListView;
       
    56     delete mMailboxSelectionDialog;
       
    57 }
       
    58 
       
    59 
       
    60 /*!
       
    61     Initializes and displays the dialog.
       
    62 
       
    63     \param mailboxId Where the ID of the selected mailbox is stored.
       
    64     \return True if the user selected a mailbox, false otherwise.
       
    65 */
       
    66 bool NmMailboxSelectionDialog::exec(NmId& mailboxId)
       
    67 {
       
    68     NMLOG("NmMailboxSelectionDialog::exec()");
       
    69     mMailboxId = 0;
       
    70 
       
    71     // Initialize the UI and fetch the mailbox items into the list.
       
    72     if (initializeUi() && populateListItems()) {
       
    73         // The UI is ready. Do display the dialog.
       
    74         mMailboxSelectionDialog->exec();
       
    75 
       
    76         // Store the ID of the selected mailbox into the given argument.
       
    77         mailboxId = mMailboxId;
       
    78     }
       
    79 
       
    80     return (mMailboxId != 0);
       
    81 }
       
    82 
       
    83 
       
    84 /*!
       
    85     Creates the view for the mailbox selection dialog.
       
    86 
       
    87     \return True if the widgets were loaded and set up successfully, false
       
    88             otherwise.
       
    89 */
       
    90 bool NmMailboxSelectionDialog::initializeUi()
       
    91 {
       
    92     NMLOG("NmMailboxSelectionDialog::initializeUi()");
       
    93 
       
    94     // Use the document loader to load the widgets.
       
    95     HbDocumentLoader documentLoader;
       
    96     bool documentLoaded = false;
       
    97 
       
    98     QObjectList objectList;
       
    99     objectList.append(this);
       
   100 
       
   101     documentLoader.setObjectTree(objectList);
       
   102     mWidgetList =
       
   103         documentLoader.load(NMUI_MAILBOX_SELECTION_DIALOG_XML, &documentLoaded);
       
   104 
       
   105     if (documentLoaded && mWidgetList.count()) {
       
   106         // Get the dialog and the list view widgets.
       
   107         mMailboxSelectionDialog =
       
   108             qobject_cast<HbDialog*>(documentLoader.findWidget(NMUI_MAILBOX_SELECTION_DIALOG));
       
   109         mMailboxListView =
       
   110             qobject_cast<HbListView*>(documentLoader.findWidget(NMUI_MAILBOX_LIST_VIEW));
       
   111     }
       
   112 
       
   113     if (!mMailboxSelectionDialog || !mMailboxListView) {
       
   114         NMLOG("NmMailboxSelectionDialog::initializeUi(): Failed to load widgets!");
       
   115         return false;
       
   116     }
       
   117 
       
   118     // Set up the loaded widgets.
       
   119     mMailboxSelectionDialog->setParentItem(mParent);
       
   120 
       
   121     // Disable timeout.
       
   122     mMailboxSelectionDialog->setTimeout(HbDialog::NoTimeout);
       
   123 
       
   124     // Connect the list view to the slot. When the user selects an item
       
   125     // from the list, the slot should be called.
       
   126     connect(mMailboxListView, SIGNAL(activated(QModelIndex)),
       
   127             this, SLOT(itemActivated(QModelIndex)));
       
   128 
       
   129     // Item model initialization, data will be populated later.
       
   130     mContentItemModel = new QStandardItemModel(this);
       
   131 
       
   132     // Set the model for the list view. Use default prototype for now.
       
   133     mMailboxListView->setModel(mContentItemModel);
       
   134 
       
   135     // Set the item prototype.
       
   136     mMailboxSelectionDialog->setContentWidget(mMailboxListView);
       
   137 
       
   138     return true;
       
   139 }
       
   140 
       
   141 
       
   142 /*!
       
   143     Populates the list items (mailboxes).
       
   144 
       
   145     \return True if success, false otherwise.
       
   146 */
       
   147 bool NmMailboxSelectionDialog::populateListItems()
       
   148 {
       
   149     NMLOG("NmMailboxSelectionDialog::populateListItems()");
       
   150     const int count = mMailboxListModel.rowCount();
       
   151 
       
   152     if (!mContentItemModel || count == 0) {
       
   153         // The UI skeleton was not set up successfully or no mailboxes exist!
       
   154         return false;
       
   155     }
       
   156 
       
   157     NmMailboxMetaData *metaData = NULL;
       
   158     QStandardItem *item = NULL;
       
   159 
       
   160     for (int i = 0; i < count; ++i) {
       
   161         metaData = mailboxMetaData(i);
       
   162 
       
   163         if (metaData) {
       
   164             // Implement the branded icons when possible.
       
   165             const HbIcon &mailboxIcon =
       
   166                 NmIcons::getIcon(NmIcons::NmIconDefaultMailbox);
       
   167 
       
   168             // Construct the item and append it into the list.
       
   169             item = new QStandardItem(mailboxIcon.qicon(), metaData->name());
       
   170             mContentItemModel->appendRow(item);
       
   171         }
       
   172     }
       
   173 
       
   174     return true;
       
   175 }
       
   176 
       
   177 
       
   178 /*!
       
   179     Retrieve the metadata for the given mailbox.
       
   180 
       
   181     \param index The index of the mailbox in the list model.
       
   182     \return A mailbox meta data instance.
       
   183 */
       
   184 NmMailboxMetaData *NmMailboxSelectionDialog::mailboxMetaData(int index) const
       
   185 {
       
   186     QVariant mailbox = mMailboxListModel.data(mMailboxListModel.index(index, 0));
       
   187     NmMailboxMetaData *mailboxMetaData = mailbox.value<NmMailboxMetaData*>();
       
   188     return mailboxMetaData;
       
   189 }
       
   190 
       
   191 
       
   192 /*!
       
   193     This slot is invoked when a list item is selected.
       
   194 
       
   195     \param index The index of the selected item.
       
   196 */
       
   197 void NmMailboxSelectionDialog::itemActivated(QModelIndex index)
       
   198 {
       
   199     const int rowIndex = index.row();
       
   200     NmMailboxMetaData *metaData = mailboxMetaData(rowIndex);
       
   201 
       
   202     if (metaData && mMailboxSelectionDialog) {
       
   203         mMailboxId = metaData->id();
       
   204         mMailboxSelectionDialog->close();
       
   205     }
       
   206 }
       
   207 
       
   208 
       
   209 // End of file.