emailuis/nmsettingui/src/nmmailboxselectionview.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 <QScopedPointer>
       
    19 #include <QSignalMapper>
       
    20 #include <hbdataform.h>
       
    21 #include <hbdataformmodel.h>
       
    22 #include <hbpushbutton.h>
       
    23 #include <hbdataformviewitem.h>
       
    24 #include <cpitemdatahelper.h>
       
    25 #include <nmmailbox.h>
       
    26 
       
    27 #include "nmmailboxentryitem.h"
       
    28 #include "nmmailboxselectionview.h"
       
    29 #include "nmsettingsviewfactory.h"
       
    30 #include "nmmailboxsettingsmanager.h"
       
    31 
       
    32 /*!
       
    33     \class NmMailboxSelectionView
       
    34     \brief Selection view for the mailbox accounts.
       
    35 */
       
    36 
       
    37 // ======== MEMBER FUNCTIONS ========
       
    38 
       
    39 /*!
       
    40     Constructor of NmMailboxSelectionView.
       
    41 
       
    42     Constructs the model and populates it with the mailbox items.
       
    43 */
       
    44 NmMailboxSelectionView::NmMailboxSelectionView(
       
    45     const NmSettingsViewFactory &settingsFactory,
       
    46     NmMailboxSettingsManager &settingsManager,
       
    47     const QList<NmMailbox *> &mailboxList,
       
    48     QGraphicsItem *parent)
       
    49 : CpBaseSettingView(0, parent),
       
    50   mSettingsManager(settingsManager),
       
    51   mSettingsFactory(settingsFactory),
       
    52   mSignalMapperConnected(false),
       
    53   mRefreshForm(false)
       
    54 {
       
    55     QScopedPointer<QSignalMapper> signalMapper(new QSignalMapper());
       
    56 
       
    57     // Connect the form activated signal.
       
    58     connect(settingForm(), SIGNAL(activated(QModelIndex)),
       
    59         this, SLOT(formActivate(QModelIndex)));
       
    60 
       
    61     QScopedPointer<CpItemDataHelper> itemHelper(new CpItemDataHelper());
       
    62     QScopedPointer<HbDataFormModel> model(new HbDataFormModel());
       
    63 
       
    64     // Ownership is not transferred yet.
       
    65     mItemDataHelper = itemHelper.data();
       
    66     mModel = model.data();
       
    67 
       
    68     // populate data.
       
    69     populateDataModel(mailboxList);
       
    70 
       
    71     (void)model.take();
       
    72     (void)itemHelper.take();
       
    73     mSignalMapper = signalMapper.take();
       
    74 
       
    75     // Set the localized title string.
       
    76     setTitle(hbTrId("txt_mail_title_mail"));
       
    77 }
       
    78 
       
    79 /*!
       
    80     Destructor of NmMailboxSelectionView.
       
    81 */
       
    82 NmMailboxSelectionView::~NmMailboxSelectionView()
       
    83 {
       
    84     delete mItemDataHelper;
       
    85     delete mModel;
       
    86     delete mSignalMapper;
       
    87 }
       
    88 
       
    89 /*!
       
    90     Handles the mailbox button click signal. Calls the SettingsViewFactory
       
    91     to construct the setting view for the selected mailbox account.
       
    92 
       
    93     \param item NmMailBoxEntryItem mapped for the button.
       
    94 
       
    95 */
       
    96 void NmMailboxSelectionView::buttonClick(QObject *item)
       
    97 {
       
    98     NMLOG("NmMailboxSelectionView::buttonClick");
       
    99     NmMailboxEntryItem *entryItem = static_cast<NmMailboxEntryItem *>(item);
       
   100     const NmId &id = entryItem->id();
       
   101     const QString &name = entryItem->text();
       
   102     mSettingsFactory.launchSettingView(id, name);
       
   103 }
       
   104 
       
   105 /*!
       
   106     Handles the form activate signal. Maps the signal/mailbox item for the
       
   107     buttons.
       
   108 
       
   109     \param index Data model index.
       
   110 
       
   111 */
       
   112 void NmMailboxSelectionView::formActivate(const QModelIndex &index)
       
   113 {
       
   114     NMLOG("NmMailboxSelectionView::formActivate");
       
   115     HbWidget *widget = settingForm()->dataFormViewItem(index)->dataItemContentWidget();
       
   116     HbDataFormModel *model = static_cast<HbDataFormModel *>(settingForm()->model());
       
   117     NmMailboxEntryItem *item = static_cast<NmMailboxEntryItem *>(model->itemFromIndex(index));
       
   118     HbPushButton *button = static_cast<HbPushButton *>(widget);
       
   119 
       
   120     if (!mSignalMapper->mapping(item)) {
       
   121         connect(button, SIGNAL(pressed()), mSignalMapper, SLOT(map()));
       
   122 
       
   123         mSignalMapper->setMapping(button, item);
       
   124 
       
   125         // Form activate is called as many times as there are items in the view.
       
   126         // This prevents multiple connections, so that the button click
       
   127         // will be called only once.
       
   128         if (!mSignalMapperConnected) {
       
   129             connect(mSignalMapper, SIGNAL(mapped(QObject *)), this, SLOT(buttonClick(QObject *)));
       
   130             mSignalMapperConnected = true;
       
   131         }
       
   132     }
       
   133 }
       
   134 
       
   135 /*!
       
   136     Populates the data model for the view.
       
   137 
       
   138     \param mailboxList List of all the available mailbox accounts.
       
   139 
       
   140 */
       
   141 void NmMailboxSelectionView::populateDataModel(const QList<NmMailbox *> &mailboxList)
       
   142 {
       
   143     NMLOG("NmMailboxSelectionView::populateDataModel");
       
   144     mModel->clear();
       
   145     foreach (NmMailbox *mailbox, mailboxList) {
       
   146         NmId id = mailbox->id();
       
   147         QScopedPointer<NmMailboxEntryItem> item(
       
   148             new NmMailboxEntryItem(*mItemDataHelper, id, mailbox->name(),
       
   149                 mSettingsManager.mailboxIcon(id)));
       
   150         mModel->appendDataFormItem(item.data());
       
   151         item.take();
       
   152     }
       
   153     settingForm()->setModel(mModel);
       
   154 }
       
   155 
       
   156 /*!
       
   157     Handles mailbox list changes.
       
   158 
       
   159     \param mailboxId mailbox id
       
   160     \param type type of change \sa nmsettingscommon.h
       
   161 */
       
   162 void NmMailboxSelectionView::mailboxListChanged(const NmId &mailboxId,
       
   163     NmSettings::MailboxEventType type)
       
   164 {
       
   165     NMLOG("NmMailboxSelectionView::mailboxListChanged");
       
   166 
       
   167     switch (type) {
       
   168         case NmSettings::MailboxDeleted: {
       
   169             // Search correct item.
       
   170             QModelIndex itemModelIndex;
       
   171             bool found(false);
       
   172             const int rowCount(mModel->rowCount());
       
   173             for (int rowIndex(0); rowIndex < rowCount && !found; ++rowIndex) {
       
   174                 itemModelIndex = mModel->index(rowIndex, 0);
       
   175                 NmMailboxEntryItem *entryItem = static_cast<NmMailboxEntryItem *>(
       
   176                     mModel->itemFromIndex(itemModelIndex));
       
   177                 if (entryItem && (entryItem->id() == mailboxId)) {
       
   178                     found = true;
       
   179                 }
       
   180             }
       
   181 
       
   182             if (found) {
       
   183                 // There is nothing that we can do if removing fails.
       
   184                 (void)mModel->removeItem(itemModelIndex);
       
   185             }
       
   186             break;
       
   187         }
       
   188         default: {
       
   189             break;
       
   190         }
       
   191     }
       
   192     NMLOG("NmMailboxSelectionView::mailboxListChanged - OK");
       
   193 }
       
   194 
       
   195 /*!
       
   196     Handles mailbox property changes.
       
   197 
       
   198     \param mailboxId mailbox id
       
   199     \param property changed property \sa nmsettingscommon.h
       
   200     \param value changed value
       
   201 */
       
   202 void NmMailboxSelectionView::mailboxPropertyChanged(const NmId &mailboxId,
       
   203     QVariant property, QVariant value)
       
   204 {
       
   205     NMLOG("NmMailboxSelectionView::mailboxPropertyChanged");
       
   206 
       
   207     // Search correct item.
       
   208     NmMailboxEntryItem *entryItem = 0;
       
   209     const int itemCount(mModel->rowCount());
       
   210     for (int itemIndex(0); itemIndex < itemCount; ++itemIndex) {
       
   211         entryItem = static_cast<NmMailboxEntryItem *>(
       
   212             mModel->itemFromIndex(mModel->index(itemIndex, 0)));
       
   213         if (entryItem && (entryItem->id() == mailboxId)) {
       
   214             break;
       
   215         }
       
   216         entryItem = 0;
       
   217     }
       
   218 
       
   219     switch (property.toInt()) {
       
   220         case NmSettings::MailboxName:
       
   221             if (entryItem) {
       
   222                 entryItem->setText(value.toString());
       
   223                 mRefreshForm = true;
       
   224             }
       
   225             break;
       
   226         default:
       
   227             break;
       
   228     }
       
   229 
       
   230     NMLOG("NmMailboxSelectionView::mailboxPropertyChanged - OK");
       
   231 }