emailuis/nmsettingui/src/nmsettingsviewfactory.cpp
branchRCL_3
changeset 63 d189ee25cf9d
equal deleted inserted replaced
61:dcf0eedfc1a3 63:d189ee25cf9d
       
     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 <QProcess>
       
    19 #include <QTranslator>
       
    20 #include <QCoreApplication>
       
    21 #include <HbInstance>
       
    22 #include <HbAction>
       
    23 #include <HbMessageBox>
       
    24 
       
    25 #include "nmsettingsviewfactory.h"
       
    26 #include "nmmailboxsettingsmanager.h"
       
    27 #include "nmmailboxsettingview.h"
       
    28 #include "nmmailboxselectionview.h"
       
    29 #include "nmmailbox.h"
       
    30 #include "nmcommon.h"
       
    31 #include "nmsettingsviewlauncher.h"
       
    32 #include "nmsettingscommon.h"
       
    33 
       
    34 static const QString mailWizardStartExe = "mailwizard.exe";
       
    35 static const QString mailWizardStartArgs = "from:controlpanel";
       
    36 
       
    37 
       
    38 /*!
       
    39     \class NmSettingsViewFactory
       
    40     \brief Constructs and displays settings views.
       
    41 
       
    42 */
       
    43 
       
    44 
       
    45 // ======== MEMBER FUNCTIONS ========
       
    46 
       
    47 /*!
       
    48     Constructor of NmSettingsViewFactory.
       
    49 
       
    50     \param itemDataHelper
       
    51     \param text
       
    52     \param description
       
    53     \param icon
       
    54     \param parent
       
    55     \return A newly constructed settings view factory instance.
       
    56 */
       
    57 NmSettingsViewFactory::NmSettingsViewFactory(
       
    58     CpItemDataHelper &itemDataHelper,
       
    59     const QString &text,
       
    60     const QString &description,
       
    61     const HbIcon &icon,
       
    62     const HbDataFormModelItem *parent)
       
    63  : CpSettingFormEntryItemData(itemDataHelper, text, description, icon, parent),
       
    64    mSettingsManager(new NmMailboxSettingsManager()), mSettingsViewLauncher(0),mPrevView(0),
       
    65    mMessageBox(0)
       
    66 {
       
    67     NM_FUNCTION;
       
    68 
       
    69     createMessageBox();
       
    70 }
       
    71 
       
    72 
       
    73 /*!
       
    74     Constructor of NmSettingsViewFactory.
       
    75 
       
    76     \param viewLauncher
       
    77     \param itemDataHelper
       
    78     \param text
       
    79     \param description
       
    80     \param icon
       
    81     \param parent
       
    82     \return A newly constructed settings view factory instance.
       
    83 */
       
    84 NmSettingsViewFactory::NmSettingsViewFactory(
       
    85     const NmSettingsViewLauncher *viewLauncher,
       
    86     CpItemDataHelper &itemDataHelper,
       
    87     const QString &text,
       
    88     const QString &description,
       
    89     const HbIcon &icon,
       
    90     const HbDataFormModelItem *parent)
       
    91  : CpSettingFormEntryItemData(itemDataHelper, text, description, icon, parent),
       
    92      mSettingsManager(new NmMailboxSettingsManager()), mSettingsViewLauncher(viewLauncher),
       
    93      mPrevView(0), mMessageBox(0)
       
    94 {
       
    95     NM_FUNCTION;
       
    96 
       
    97     createMessageBox();
       
    98 }
       
    99 
       
   100 
       
   101 /*!
       
   102     Destructor of NmSettingsViewFactory.
       
   103 */
       
   104 NmSettingsViewFactory::~NmSettingsViewFactory()
       
   105 {
       
   106     NM_FUNCTION;
       
   107 
       
   108     delete mSettingsManager;
       
   109     delete mMessageBox;
       
   110 }
       
   111 
       
   112 
       
   113 /*!
       
   114     Constructs a setting view. The type of the view depends on the number of
       
   115     mailboxes present. If there are no mailboxes defined, displays a query
       
   116     asking whether to user wants to define a new mailbox or not.
       
   117 
       
   118     \return A newly constructed setting view instance.
       
   119 */
       
   120 CpBaseSettingView *NmSettingsViewFactory::createSettingView() const
       
   121 {
       
   122     NM_FUNCTION;
       
   123 
       
   124     CpBaseSettingView *view = 0;
       
   125     QList<NmMailbox *> mailboxList;
       
   126     mSettingsManager->listMailboxes(mailboxList);
       
   127     const int mailboxCount(mailboxList.count());
       
   128 
       
   129     // Log the number of mailboxes.
       
   130     NM_COMMENT(QString("NmSettingsViewFactory::createSettingView(): mailbox count is %1").arg(mailboxCount));
       
   131 
       
   132     switch(mailboxCount) {
       
   133         case 0: {
       
   134             // Query the user whether to launch the wizard or not.
       
   135             mMessageBox->open(const_cast<NmSettingsViewFactory *>(this),
       
   136                               SLOT(launchWizard(HbAction *)));
       
   137             break;
       
   138         }
       
   139         case 1: {
       
   140             // Construct setting view for the only available mailbox
       
   141             NmMailbox *mailbox = mailboxList.at(0);
       
   142             view = new NmMailboxSettingView(mailbox->id(),
       
   143                                             mailbox->name(),
       
   144                                             *mSettingsManager);
       
   145             break;
       
   146         }
       
   147         default: {
       
   148             // Construct mailbox selection view
       
   149             view = new NmMailboxSelectionView(*this, *mSettingsManager, mailboxList);
       
   150             break;
       
   151         }
       
   152 
       
   153     }
       
   154 
       
   155     if (view) {
       
   156         connect(mSettingsManager,
       
   157                 SIGNAL(mailboxListChanged(const NmId &, NmSettings::MailboxEventType)),
       
   158                 view, SLOT(mailboxListChanged(const NmId &, NmSettings::MailboxEventType)));
       
   159 
       
   160         connect(mSettingsManager,
       
   161                 SIGNAL(mailboxPropertyChanged(const NmId &, QVariant, QVariant)),
       
   162                 view, SLOT(mailboxPropertyChanged(const NmId &, QVariant, QVariant)));
       
   163     }
       
   164 
       
   165     return view;
       
   166 }
       
   167 
       
   168 
       
   169 /*!
       
   170     Constructs and launches, i.e. displays a setting view for the given mailbox.
       
   171 
       
   172     \param mailboxId The id of the mailbox.
       
   173     \param mailboxName The name of the mailbox.
       
   174 */
       
   175 void NmSettingsViewFactory::launchSettingView(const NmId &mailboxId,
       
   176                                               const QString &mailboxName) const
       
   177 {
       
   178     NM_FUNCTION;
       
   179 
       
   180     // There's always at least one valid main window available.
       
   181     HbMainWindow *mainWindow = hbInstance->allMainWindows().takeFirst();
       
   182 
       
   183     NmMailboxSettingView *currentSettingsView =
       
   184         qobject_cast<NmMailboxSettingView*>(mainWindow->currentView());
       
   185 
       
   186     // Let's have only one same settings view at a time open,
       
   187     // but if some other mailboxes settings view want's to open while
       
   188     // there is a settings view open let's open it also.
       
   189     if (!currentSettingsView || (currentSettingsView->mailboxId().id() != mailboxId.id())) {
       
   190         CpBaseSettingView *view =
       
   191              new NmMailboxSettingView(mailboxId, mailboxName, *mSettingsManager);
       
   192 
       
   193          connect(mSettingsManager,
       
   194                  SIGNAL(mailboxListChanged(const NmId &, NmSettings::MailboxEventType)),
       
   195                  view, SLOT(mailboxListChanged(const NmId &, NmSettings::MailboxEventType)));
       
   196 
       
   197          connect(mSettingsManager,
       
   198                  SIGNAL(mailboxPropertyChanged(const NmId &, QVariant, QVariant)),
       
   199                  view, SLOT(mailboxPropertyChanged(const NmId &, QVariant, QVariant)));
       
   200 
       
   201          // Disconnect mSettingsViewLauncher's previous connections to
       
   202          // be sure that signals which are offered out will be sent last.
       
   203          mSettingsManager->disconnect(
       
   204              SIGNAL(mailboxListChanged(const NmId &, NmSettings::MailboxEventType)),
       
   205              mSettingsViewLauncher,
       
   206              SIGNAL(mailboxListChanged(const NmId &, NmSettings::MailboxEventType)));
       
   207 
       
   208          mSettingsManager->disconnect(
       
   209              SIGNAL(mailboxPropertyChanged(const NmId &, QVariant, QVariant)),
       
   210              mSettingsViewLauncher,
       
   211              SIGNAL(mailboxPropertyChanged(const NmId &, QVariant, QVariant)));
       
   212 
       
   213          // Reconnect mSettingsViewLauncher.
       
   214          connect(mSettingsManager,
       
   215                  SIGNAL(mailboxListChanged(const NmId &, NmSettings::MailboxEventType)),
       
   216                  mSettingsViewLauncher,
       
   217                  SIGNAL(mailboxListChanged(const NmId &, NmSettings::MailboxEventType)));
       
   218 
       
   219          connect(mSettingsManager,
       
   220                  SIGNAL(mailboxPropertyChanged(const NmId &, QVariant, QVariant)),
       
   221                  mSettingsViewLauncher,
       
   222                  SIGNAL(mailboxPropertyChanged(const NmId &, QVariant, QVariant)));
       
   223 
       
   224          connect(mSettingsManager,
       
   225                  SIGNAL(goOnline(const NmId &)),
       
   226                  mSettingsViewLauncher,
       
   227                  SIGNAL(goOnline(const NmId &)), Qt::UniqueConnection);
       
   228 
       
   229          connect(mSettingsManager,
       
   230                  SIGNAL(goOffline(const NmId &)),
       
   231                  mSettingsViewLauncher,
       
   232                  SIGNAL(goOffline(const NmId &)), Qt::UniqueConnection);
       
   233 
       
   234          connect(this,
       
   235                  SIGNAL(aboutToClose()),
       
   236                  mSettingsManager,
       
   237                  SIGNAL(aboutToClose()), Qt::UniqueConnection);
       
   238 
       
   239          // Create back navigation action for a view.
       
   240          HbAction *action = new HbAction(Hb::BackNaviAction, view);
       
   241          connect(action, SIGNAL(triggered()), this, SLOT(backPress()));
       
   242          view->setNavigationAction(action);
       
   243          mPrevView = mainWindow->currentView();
       
   244          mainWindow->addView(view);
       
   245          mainWindow->setCurrentView(view);
       
   246     }
       
   247 }
       
   248 
       
   249 /*!
       
   250    Handels back button press.
       
   251    Removes the current view from the main window and activates the previous view.
       
   252    Destroys removed view.
       
   253 */
       
   254 void NmSettingsViewFactory::backPress()
       
   255 {
       
   256     NM_FUNCTION;
       
   257 
       
   258     emit aboutToClose();
       
   259     HbMainWindow *mainWindow = hbInstance->allMainWindows().takeFirst();
       
   260     QList<HbView *> views = mainWindow->views();
       
   261     if (views.count() > 1) {
       
   262         HbView *currentView = mainWindow->currentView();
       
   263         int indexOfView(views.indexOf(mPrevView));
       
   264         if (indexOfView > -1) {
       
   265             mainWindow->setCurrentView(views.at(indexOfView));
       
   266             mainWindow->removeView(currentView);
       
   267             currentView->deleteLater();
       
   268         }
       
   269         mPrevView = 0;
       
   270     }
       
   271 }
       
   272 
       
   273 /*!
       
   274     Handles user selection from "No mailboxes defined" dialog. Launches the Mail Wizard if \a action
       
   275     is the dialog's primary action ("Yes"). Otherwise does nothing.
       
   276     \param action. Action selected by the user.
       
   277 */
       
   278 void NmSettingsViewFactory::launchWizard(HbAction *action)
       
   279 {
       
   280     NM_FUNCTION;
       
   281 
       
   282     if (action == mMessageBox->primaryAction()) {
       
   283         // Launch mail wizard.
       
   284         NM_COMMENT(QString("NmSettingsViewFactory::launchWizard(): launching the mail wizard"));
       
   285         QStringList args;
       
   286         args << mailWizardStartArgs;
       
   287         QProcess::startDetached(mailWizardStartExe, args);
       
   288     }
       
   289 }
       
   290 
       
   291 /*!
       
   292     Creates the "No mailboxes defined" dialog. Called from the constructors.
       
   293 */
       
   294 void NmSettingsViewFactory::createMessageBox()
       
   295 {
       
   296     NM_FUNCTION;
       
   297 
       
   298     mMessageBox = new HbMessageBox(HbMessageBox::MessageTypeQuestion);
       
   299     mMessageBox->setText(hbTrId("txt_mail_dialog_no_mailboxes_create_new"));
       
   300     mMessageBox->setTimeout(HbMessageBox::NoTimeout);
       
   301     mMessageBox->setStandardButtons(HbMessageBox::Yes | HbMessageBox::No);
       
   302 }
       
   303 
       
   304 
       
   305 // End of file.