emailuis/nmailui/src/nmapplication.cpp
changeset 49 00c7ae862740
parent 47 f83bd4ae1fe3
child 51 d845db10c0d4
equal deleted inserted replaced
48:10eaf342f539 49:00c7ae862740
     1 /*
     1 /*
     2 * Copyright (c) 2009 - 2010 Nokia Corporation and/or its subsidiary(-ies).
     2  * Copyright (c) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3  * All rights reserved.
     4 * This component and the accompanying materials are made available
     4  * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     5  * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     6  * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     8  *
     9 * Initial Contributors:
     9  * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    10  * Nokia Corporation - initial contribution.
    11 *
    11  *
    12 * Contributors:
    12  * Contributors:
    13 *
    13  *
    14 * Description:
    14  * Description:
    15 *
    15  *
    16 */
    16  */
    17 
    17 
    18 #include "nmuiheaders.h"
    18 #include "nmuiheaders.h"
    19 #include <email_services_api.h>
    19 #include <email_services_api.h>
    20 #include <e32base.h>
    20 #include <e32base.h>
    21 
    21 
    22 static const QString NmSendServiceName = "nmail.com.nokia.symbian.IFileShare";
    22 static const QString NmSendServiceName = "nmail.com.nokia.symbian.IFileShare";
    23 
    23 
    24 /*!
    24 /*!
    25 	\class NmApplication
    25     \class NmApplication
    26 	\brief Application class, creates main window and handles view switching
    26     \brief Application class, creates main window and handles view switching.
    27 */
    27 */
    28 
    28 
    29 /*!
    29 /*!
    30     Constructor
    30     Constructor.
    31 */
    31 */
    32 NmApplication::NmApplication(QObject *parent, quint32 accountId)
    32 NmApplication::NmApplication(QObject *parent, quint32 accountId)
    33 :QObject(parent),
    33 : QObject(parent),
    34 mMainWindow(NULL),
    34   mMainWindow(NULL),
    35 mViewStack(NULL),
    35   mViewStack(NULL),
    36 mActiveViewId(NmUiViewNone),
    36   mActiveViewId(NmUiViewNone),
    37 mUiEngine(NULL),
    37   mUiEngine(NULL),
    38 mBackAction(NULL),
    38   mBackAction(NULL),
    39 mExtensionManager(NULL),
    39   mExtensionManager(NULL),
    40 mMbListModel(NULL),
    40   mMbListModel(NULL),
    41 mServiceViewId(NmUiViewNone),
    41   mServiceViewId(NmUiViewNone),
    42 mForegroundService(false),
    42   mForegroundService(false),
    43 mEffects(NULL),
    43   mEffects(NULL),
    44 mAttaManager(NULL),
    44   mAttaManager(NULL),
    45 mSettingsViewLauncher(NULL),
    45   mSettingsViewLauncher(NULL),
    46 mViewReady(false),
    46   mViewReady(false),
    47 mQueryDialog(NULL),
    47   mQueryDialog(NULL),
    48 mBackButtonPressed(false)
    48   mBackButtonPressed(false)
    49 {
    49 {
    50     NM_FUNCTION;
       
    51 
       
    52     TRAP_IGNORE(mUiEngine = NmUiEngine::instance());
    50     TRAP_IGNORE(mUiEngine = NmUiEngine::instance());
    53 
    51     
    54     // Create network access manager and cache for application use.
    52     // Create network access manager and cache for application use.
    55     mNetManager = new NmViewerViewNetManager(*mUiEngine);
    53     mNetManager = new NmViewerViewNetManager(*mUiEngine);
    56     QNetworkDiskCache *cache = new QNetworkDiskCache();
    54     QNetworkDiskCache *cache = new QNetworkDiskCache();
    57     cache->setCacheDirectory(QDesktopServices::storageLocation(QDesktopServices::CacheLocation));
    55     cache->setCacheDirectory(
       
    56         QDesktopServices::storageLocation(QDesktopServices::CacheLocation));
    58     mNetManager->setCache(cache);
    57     mNetManager->setCache(cache);
    59 
    58     
    60     createMainWindow();
    59     createMainWindow();
    61 
    60     
    62     // attachment manager can be shared between viewer and editor, ownership in application class
    61     // Attachment manager can be shared between viewer and editor.
       
    62     // The application class has the ownership.
    63     mAttaManager = new NmAttachmentManager(*mUiEngine);
    63     mAttaManager = new NmAttachmentManager(*mUiEngine);
    64 
    64     
    65     mSendServiceInterface =
    65     mSendServiceInterface =
    66             new NmSendServiceInterface(NmSendServiceName, NULL, *mUiEngine, this);
    66         new NmSendServiceInterface(NmSendServiceName, NULL, *mUiEngine, this);
    67     mSendServiceInterface2 =
    67     mSendServiceInterface2 =
    68             new NmSendServiceInterface(emailFullServiceNameSend, NULL, *mUiEngine, this);
    68         new NmSendServiceInterface(emailFullServiceNameSend, NULL, *mUiEngine, this);
    69     mUriServiceInterface =
    69     mUriServiceInterface =
    70         new NmUriServiceInterface(NULL, *mUiEngine, this);
    70         new NmUriServiceInterface(NULL, *mUiEngine, this);
    71     mMailboxServiceInterface =
    71     mMailboxServiceInterface =
    72             new NmMailboxServiceInterface(NULL, *mUiEngine, this);
    72         new NmMailboxServiceInterface(NULL, *mUiEngine, this);
    73     mViewerServiceInterface =
    73     mViewerServiceInterface =
    74             new NmViewerServiceInterface(NULL, this, *mUiEngine);
    74         new NmViewerServiceInterface(NULL, this, *mUiEngine);
    75 
    75     
    76     if(accountId != 0) {
    76     if(accountId != 0) {
    77         QVariant mailbox;
    77         QVariant mailbox;
    78         mailbox.setValue(mUiEngine->getPluginIdByMailboxId(accountId).id());
    78         mailbox.setValue(mUiEngine->getPluginIdByMailboxId(accountId).id());
    79         mMailboxServiceInterface->displayInboxByMailboxId(mailbox);
    79         mMailboxServiceInterface->displayInboxByMailboxId(mailbox);
    80     }
    80     }
    81 
    81     
    82     mEffects = new NmUiEffects(*mMainWindow);
    82     mEffects = new NmUiEffects(*mMainWindow);
    83 }
    83 }
    84 
    84 
    85 /*!
    85 /*!
    86     Destructor
    86     Destructor.
    87 */
    87 */
    88 NmApplication::~NmApplication()
    88 NmApplication::~NmApplication()
    89 {
    89 {
    90     NM_FUNCTION;
       
    91 
       
    92     if (mQueryDialog) {
    90     if (mQueryDialog) {
    93         delete mQueryDialog;
    91         delete mQueryDialog;
    94         mQueryDialog=NULL;
    92         mQueryDialog = NULL;
    95     }
    93     }
    96 
    94     
    97 	delete mSendServiceInterface;
    95     delete mSendServiceInterface;
    98 	delete mSendServiceInterface2;
    96     delete mSendServiceInterface2;
    99 	delete mUriServiceInterface;
    97     delete mUriServiceInterface;
   100 	delete mMailboxServiceInterface;
    98     delete mMailboxServiceInterface;
   101 	delete mViewerServiceInterface;
    99     delete mViewerServiceInterface;
   102 
   100     
   103 	resetViewStack();
   101     resetViewStack();
   104     delete mViewStack;
   102     delete mViewStack;
       
   103     
   105     NmIcons::freeIcons();
   104     NmIcons::freeIcons();
   106     NmUiEngine::releaseInstance(mUiEngine);
   105     NmUiEngine::releaseInstance(mUiEngine);
   107     mUiEngine = NULL;
   106     mUiEngine = NULL;
       
   107     
   108     delete mBackAction;
   108     delete mBackAction;
       
   109     
       
   110     // Workaround: the main window has to be closed (hidden) before deleting
       
   111     // the extension manager in order to prevent the main window's title bar
       
   112     // from showing when the application is closed.
       
   113     if (mMainWindow) {
       
   114         // Workaround: assert failure may happen if an open main window is
       
   115         // deleted.
       
   116         mMainWindow->close();
       
   117     }
       
   118     
   109     delete mExtensionManager;
   119     delete mExtensionManager;
       
   120     
   110     if (mNetManager) {
   121     if (mNetManager) {
   111         if (mNetManager->cache()) {
   122         if (mNetManager->cache()) {
   112             mNetManager->cache()->clear();
   123             mNetManager->cache()->clear();
   113         }
   124         }
   114     delete mNetManager;
   125     delete mNetManager;
   115     mNetManager = NULL;
   126     mNetManager = NULL;
   116     }
   127     }
   117     // Effects needs to be deleted before MainWindow.
   128     
       
   129     // Effects need to be deleted before MainWindow.
   118     delete mEffects;
   130     delete mEffects;
   119     if (mMainWindow) {
       
   120         // workaround: assert failure may happen otherwise
       
   121         mMainWindow->close();
       
   122     }
       
   123     delete mMainWindow;
   131     delete mMainWindow;
   124     delete mAttaManager;
   132     delete mAttaManager;
   125     delete mSettingsViewLauncher;
   133     delete mSettingsViewLauncher;
   126 }
   134 }
   127 
   135 
   128 /*!
   136 /*!
   129     Main application window creation
   137     Main application window creation.
   130 */
   138 */
   131 void NmApplication::createMainWindow()
   139 void NmApplication::createMainWindow()
   132 {
   140 {
   133     NM_FUNCTION;
   141     NM_FUNCTION;
   134 
   142     
   135     bool service = XQServiceUtil::isService();
   143     bool service = XQServiceUtil::isService();
   136 
   144     
   137     // Register custom widget files
   145     // Register custom widget files.
   138     HbStyleLoader::registerFilePath(":nmmessagelistviewitem.widgetml");
   146     HbStyleLoader::registerFilePath(":nmmessagelistviewitem.widgetml");
   139     HbStyleLoader::registerFilePath(":nmmessagelistviewitem.css");
   147     HbStyleLoader::registerFilePath(":nmmessagelistviewitem.css");
   140     HbStyleLoader::registerFilePath(":nmviewerheader.widgetml");
   148     HbStyleLoader::registerFilePath(":nmviewerheader.widgetml");
   141     HbStyleLoader::registerFilePath(":nmviewerheader.css");
   149     HbStyleLoader::registerFilePath(":nmviewerheader.css");
   142 
   150     
   143     // Create main window
   151     // Create main window.
   144     mMainWindow = new HbMainWindow();
   152     mMainWindow = new HbMainWindow();
   145 
   153     
   146     // Connect to lazy loading signal
   154     // Connect to lazy loading signal.
   147     QObject::connect(mMainWindow, SIGNAL(viewReady ()),
   155     QObject::connect(mMainWindow, SIGNAL(viewReady ()),
   148                      this, SLOT(viewReady()));
   156                      this, SLOT(viewReady()));
   149 
   157     
   150     // Create extension manager
   158     // Create extension manager.
   151     mExtensionManager = new NmUiExtensionManager();
   159     mExtensionManager = new NmUiExtensionManager();
   152 
   160     
   153     // Create view stack
   161     // Create view stack.
   154     mViewStack = new QStack<NmBaseView*>;
   162     mViewStack = new QStack<NmBaseView*>;
   155 
   163     
   156     // Create back action and connect it to prepareForPopView()
   164     // Create back action and connect it to prepareForPopView().
   157     if (mMainWindow) {
   165     if (mMainWindow) {
   158         mBackAction = new HbAction(Hb::BackNaviAction,this);
   166         mBackAction = new HbAction(Hb::BackNaviAction,this);
   159         connect(mBackAction, SIGNAL(triggered()), this, SLOT(prepareForPopView()));
   167         connect(mBackAction,
   160 
   168                 SIGNAL(triggered()),
   161         // Show mainwindow
   169                 this,
   162         // Services will active it when the view is ready
   170                 SLOT(prepareForPopView()));
       
   171         
       
   172         // Show mainwindow.
       
   173         // Services will active it when the view is ready.
   163         if (!service) {
   174         if (!service) {
   164             mMainWindow->show();
   175             mMainWindow->show();
   165         }
   176         }
   166     }
   177     }
   167 
   178     
   168     // async operation completion related notifications
   179     // Asynchronous operation completion related notifications.
   169     connect(
   180     connect(mUiEngine,
   170         mUiEngine, SIGNAL(operationCompleted(const NmOperationCompletionEvent &)),
   181             SIGNAL(operationCompleted(const NmOperationCompletionEvent &)),
   171         this, SLOT(handleOperationCompleted(const NmOperationCompletionEvent &)));
   182             this,
   172 
   183             SLOT(handleOperationCompleted(const NmOperationCompletionEvent &)));
       
   184     
   173     mMbListModel = &mUiEngine->mailboxListModel();
   185     mMbListModel = &mUiEngine->mailboxListModel();
   174 
   186     
   175     // Start application to mailbox view
   187     // ----------------------------------------------------------------------
   176     // If started as service, there is no need to create views
   188     // TODO: Remove or comment out the following code block when the mail
       
   189     //       wizard starts to work with Mfe.
       
   190     // ----------------------------------------------------------------------
       
   191     // Start application to mailbox view. If started as service, there is no
       
   192     // need to create views.
   177     if (!service) {
   193     if (!service) {
   178         NmUiStartParam *startParam = new NmUiStartParam(NmUiViewMailboxList);
   194         NmUiStartParam *startParam = new NmUiStartParam(NmUiViewMailboxList);
   179         enterNmUiView(startParam);
   195         enterNmUiView(startParam);
   180     }
   196     }
   181 
   197     // ----------------------------------------------------------------------
       
   198     
   182     if (mMainWindow) {
   199     if (mMainWindow) {
   183         // Start to filter main window events to get "end key" event in all possible situations. Using
   200         // Start to filter main window events to get "end key" event in all
   184         // event() is not enough to catch the event as it is only called if the view widget has the
   201         // possible situations. Using event()is not enough to catch the event
   185         // focus. Note: if key capturing (xqkeycapture.h) is required it is probably best to implement
   202         // as it is only called if the view widget has the focus. Note: if
   186         // an own QMainWindow class and do the capturing there, not in the views.
   203         // key capturing (xqkeycapture.h) is required it is probably best to
       
   204         // implement an own QMainWindow class and do the capturing there, not
       
   205         // in the views.
   187         mMainWindow->installEventFilter(this);
   206         mMainWindow->installEventFilter(this);
   188     
   207         // Optimize the custom paint functions.
   189         // Optimize the custom paint functions. 
   208         // Currently effects to NmViewerHeader::paint() and
   190         // Currently effects to NmViewerHeader::paint & NmAttachmentListWidget::paint
   209         // NmAttachmentListWidget::paint().
   191         mMainWindow->setOptimizationFlag(QGraphicsView::DontSavePainterState);    
   210         mMainWindow->setOptimizationFlag(QGraphicsView::DontSavePainterState);    
   192     }
   211     }
   193 }
   212 }
   194 
   213 
   195 /*!
   214 /*!
   196     Slot. React to view ready signal and call
   215     Slot. React to view ready signal and call current view method.
   197     current view method.
       
   198 */
   216 */
   199 void NmApplication::viewReady()
   217 void NmApplication::viewReady()
   200 {
   218 {
   201     NM_FUNCTION;
       
   202 
       
   203     mViewReady = true;
   219     mViewReady = true;
   204     if (mViewStack && !mViewStack->isEmpty()) {
   220     if (mViewStack && !mViewStack->isEmpty()) {
   205     	NmBaseView *currentView = mViewStack->top();
   221     	NmBaseView *currentView = mViewStack->top();
   206         if (currentView) {
   222         if (currentView) {
   207             currentView->viewReady();
   223             currentView->viewReady();
   208         }
   224         }
   209     }
   225     }
   210 }
   226 }
   211 
   227 
   212 /*!
   228 /*!
   213     Event filter. End key is filtered from the main window and either the view takes case of the
   229     Event filter. End key is filtered from the main window and either the
   214 	or the app is exited by default.
   230     view takes case of the or the app is exited by default.
   215 */
   231 */
   216 bool NmApplication::eventFilter(QObject *obj, QEvent *event)
   232 bool NmApplication::eventFilter(QObject *obj, QEvent *event)
   217 {
   233 {
   218     NM_FUNCTION;
       
   219 
       
   220     bool consumed(false);
   234     bool consumed(false);
   221 
   235     
   222     if (obj && obj == mMainWindow && event && event->type() == QEvent::KeyPress) {
   236     if (obj && obj == mMainWindow && event && event->type() == QEvent::KeyPress) {
   223         QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
   237         QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
   224 
   238         if (keyEvent->key() == Qt::Key_No) {
   225         if (keyEvent->key() == Qt::Key_No) { // end key, the "red" key
   239             // End key, the "red" key.
   226 
   240             // Exit application if no pending operations are on-going.
   227             // exit application if no pending operations are on-going
   241         }
   228         }
   242     }
   229     }
   243     
   230 
       
   231     if (!consumed) {
   244     if (!consumed) {
   232         consumed = QObject::eventFilter(obj, event);
   245         consumed = QObject::eventFilter(obj, event);
   233     }
   246     }
   234 
   247     
   235     return consumed;
   248     return consumed;
   236 }
   249 }
   237 
   250 
   238 /*!
   251 /*!
   239     Push view to view stack
   252     Push view to view stack.
   240 */
   253 */
   241 void NmApplication::pushView(NmBaseView *newView)
   254 void NmApplication::pushView(NmBaseView *newView)
   242 {
   255 {
   243     NM_FUNCTION;
       
   244 
       
   245     if (newView && mViewStack) {
   256     if (newView && mViewStack) {
   246         NM_COMMENT("nmailui: view exists");
   257         NM_COMMENT("NmApplication::pushView() : view exists.");
   247         newView->setNavigationAction(mBackAction);
   258         newView->setNavigationAction(mBackAction);
   248 
   259         
   249         // Store view to be hidden
   260         // Store view to be hidden.
   250         NmBaseView *hideView(NULL);
   261         NmBaseView *hideView(NULL);
   251         if (!mViewStack->isEmpty()) {
   262         if (!mViewStack->isEmpty()) {
   252             hideView = mViewStack->top();
   263             hideView = mViewStack->top();
   253         }
   264         }
   254 		else {
   265 		else {
   255 			// viewReady should be informed immediatelly
   266 			// viewReady should be informed immediately.
   256 			if (mViewReady) {
   267 			if (mViewReady) {
   257 				newView->viewReady();
   268 				newView->viewReady();
   258 			}
   269 			}
   259 		}
   270 		}
   260 
   271         
   261         // activate new view
   272         // Activate new view.
   262     	NM_COMMENT("nmailui: addView");
   273     	NM_COMMENT("NmApplication::pushView() : add view.");
   263         mMainWindow->addView(newView);
   274         mMainWindow->addView(newView);
   264         mViewStack->push(newView);
   275         mViewStack->push(newView);
   265         mMainWindow->setCurrentView(newView);
   276         mMainWindow->setCurrentView(newView);
   266         mActiveViewId=newView->nmailViewId();
   277         mActiveViewId=newView->nmailViewId();
   267 
   278         
   268         // Set toolbars orientation
   279         // Set toolbars orientation.
   269         HbToolBar *tb = newView->toolBar();
   280         HbToolBar *tb = newView->toolBar();
   270         if (tb) {
   281         if (tb) {
   271             tb->setOrientation(Qt::Horizontal);
   282             tb->setOrientation(Qt::Horizontal);
   272         }
   283         }
   273 
   284         
   274         // hide old view
   285         // Hide old view.
   275         NM_COMMENT("nmailui: removeView");
   286         NM_COMMENT("NmApplication::pushView() : remove view.");
   276         if (hideView) {
   287         if (hideView) {
   277             mMainWindow->removeView(hideView);
   288             mMainWindow->removeView(hideView);
   278         }
   289         }
   279 
   290     }
   280         NM_COMMENT("nmailui: pushView done");
   291 }
   281     }
   292 
   282 }
   293 /*!
   283 
   294     Ask from view that is it ok to pop view. This kind of 2-phase popView is
   284 /*!
   295     needed because view may show query dialog for user response.
   285     Ask from view that is it ok to pop view. This kind of 2-phase popView is needed
       
   286     because view may show query dialog for user response.
       
   287  */
   296  */
   288 void NmApplication::prepareForPopView()
   297 void NmApplication::prepareForPopView()
   289 {
   298 {
   290     if (mViewStack && mViewStack->size() > 0) {
   299     if (mViewStack && mViewStack->size() > 0) {
   291         // Get view pointer
   300         // Get view pointer.
   292         NmBaseView *view = mViewStack->top();
   301         NmBaseView *view = mViewStack->top();
   293         if (view){
   302         if (view){
   294 			NmUiViewId topViewId = view->nmailViewId();
   303 			NmUiViewId topViewId = view->nmailViewId();
   295 			
   304 			
   296 			// We must know in popView, are we coming with back button from message view
   305 			// We must know in popView, are we coming with back button from
   297 			// to prevent the send animation.
   306 			// message view to prevent the send animation.
   298 			if (topViewId == NmUiViewMessageEditor) {
   307 			if (topViewId == NmUiViewMessageEditor) {
   299 				mBackButtonPressed = true;
   308 				mBackButtonPressed = true;
   300 			}
   309 			}
   301             
   310             
   302             // View will call/signal popView if exitting is ok.
   311             // View will call/signal popView if exitting is ok.
   303             view->okToExitView();        
   312             view->okToExitView();        
   304         }
   313         }
   305     }
   314     }
   306     // If the view stack is now empty quit the app
   315     // If the view stack is now empty quit the app. This happens also when
   307     // This happens also when the app has been started as a service
   316     // the app has been started as a service
   308     else if (mViewStack && mViewStack->size() == 0) {
   317     else if (mViewStack && mViewStack->size() == 0) {
   309         exitApplication();
   318         exitApplication();
   310     }
   319     }
   311 }
   320 }
   312 
   321 
   313 /*!
   322 /*!
   314     Pop view from view stack. View object is deleted.
   323     Pop view from view stack. View object is deleted.
   315 */
   324 */
   316 void NmApplication::popView()
   325 void NmApplication::popView()
   317 {
   326 {
   318     NM_FUNCTION;
       
   319     if (mViewStack && mViewStack->size() > 0) {
   327     if (mViewStack && mViewStack->size() > 0) {
   320         NmBaseView *view = mViewStack->top();    
   328         NmBaseView *view = mViewStack->top();    
   321         if (view) {
   329         if (view) {
   322             // Get top view id
   330             // Get top view id.
   323             NmUiViewId topViewId = view->nmailViewId();
   331             NmUiViewId topViewId = view->nmailViewId();
   324     
   332             
   325             // Prepare for send animation if returing from editor and message has been sent.
   333             // Prepare for send animation if returing from editor and message
       
   334             // has been sent.
   326             if (topViewId == NmUiViewMessageEditor && mUiEngine->isSendingMessage()) {
   335             if (topViewId == NmUiViewMessageEditor && mUiEngine->isSendingMessage()) {
   327 				// If we are coming from message editor with back button
   336 				// If we are coming from message editor with back button.
   328 				// do not prepare the send animation.
   337 				// do not prepare the send animation.
   329 				if (!mBackButtonPressed) {
   338 				if (!mBackButtonPressed) {
   330 					mEffects->prepareEffect(NmUiEffects::NmEditorSendMessageAnimation);
   339 					mEffects->prepareEffect(NmUiEffects::NmEditorSendMessageAnimation);
   331 				}
   340 				}
   332 	        }
   341 	        }
   333             mBackButtonPressed = false;
   342             mBackButtonPressed = false;
   334             mViewStack->pop();
   343             mViewStack->pop();
   335             // Call custom exit function
   344             // Call custom exit function.
   336             view->aboutToExitView();
   345             view->aboutToExitView();
   337             // Remove view from stack.
   346             // Remove view from stack.
   338             mMainWindow->removeView(view);
   347             mMainWindow->removeView(view);
   339     
   348             
   340             // if we were in editor and sent a message, pop viewer from stack first
   349             // If we were in editor and sent a message, pop viewer from stack
   341             // so we can go straight to mail list
   350             // first so we can go straight to mail list.
   342             if (!mViewStack->isEmpty() && topViewId == NmUiViewMessageEditor &&
   351             if (!mViewStack->isEmpty() && topViewId == NmUiViewMessageEditor &&
   343                 mUiEngine->isSendingMessage() &&
   352                 mUiEngine->isSendingMessage() &&
   344                 mViewStack->top()->nmailViewId() == NmUiViewMessageViewer) {
   353                 mViewStack->top()->nmailViewId() == NmUiViewMessageViewer) {
   345                 NmBaseView *tmpView = mViewStack->pop();
   354                 NmBaseView *tmpView = mViewStack->pop();
   346                 mMainWindow->removeView(tmpView);
   355                 mMainWindow->removeView(tmpView);
   347                 delete tmpView;
   356                 delete tmpView;
   348                 tmpView = NULL;
   357                 tmpView = NULL;
   349             }
   358             }
   350     
   359             
   351             if (!mViewStack->isEmpty()) {
   360             if (!mViewStack->isEmpty()) {
   352                 // Activate next view in stack
   361                 // Activate next view in stack.
   353                 NmBaseView *showView = mViewStack->top();
   362                 NmBaseView *showView = mViewStack->top();
   354                 mMainWindow->addView(showView);
   363                 mMainWindow->addView(showView);
   355                 mMainWindow->setCurrentView(showView);
   364                 mMainWindow->setCurrentView(showView);
   356                 // Store activated view id
   365                 // Store activated view id.
   357                 mActiveViewId=showView->nmailViewId();
   366                 mActiveViewId=showView->nmailViewId();
   358                 // Perform send animation if requested.
   367                 // Perform send animation if requested.
   359                 mEffects->startEffect(NmUiEffects::NmEditorSendMessageAnimation);
   368                 mEffects->startEffect(NmUiEffects::NmEditorSendMessageAnimation);
   360             }
   369             }
   361     
   370             
   362             delete view;
   371             delete view;
   363             view = NULL;
   372             view = NULL;
   364     
   373             
   365             // If view was started as service, move the app now
   374             // If view was started as service, move the app now to the
   366             // to the background, unless it was started when the app
   375             // background, unless it was started when the app was already in
   367             // was already in foreground..
   376             // foreground.
   368             if (mServiceViewId == topViewId) {
   377             if (mServiceViewId == topViewId) {
   369                 mServiceViewId = NmUiViewNone;
   378                 mServiceViewId = NmUiViewNone;
   370                 NM_COMMENT("Returned from service view");
   379                 NM_COMMENT("NmApplication::popView() : returned from service view.");
   371     
   380                 // If started as embedded or while the app was in foreground,
   372                 // if started as embedded or while the app was in foreground,
   381                 // do not hide the app.
   373                 // do not hide the app
       
   374                 if (!XQServiceUtil::isEmbedded() &&
   382                 if (!XQServiceUtil::isEmbedded() &&
   375                     !mForegroundService) {
   383                     !mForegroundService) {
   376                     XQServiceUtil::toBackground(true);
   384                     XQServiceUtil::toBackground(true);
   377                 }
   385                 }
   378             }
   386             }
   379         }
   387         }
   380     }
   388     }
   381 
   389     
   382     // If the view stack is now empty quit the app
   390     // If the view stack is now empty quit the app. This happens also when
   383     // This happens also when the app has been started as a service
   391     // the app has been started as a service.
   384     if (mViewStack && mViewStack->size() == 0) {
   392     if (mViewStack && mViewStack->size() == 0) {
   385         exitApplication();
   393         exitApplication();
   386     }
   394     }
   387 }
   395 }
   388 
   396 
   389 /*!
   397 /*!
   390     Reset view stack. Remove and destroy view objects.
   398     Reset view stack. Remove and destroy view objects.
   391 */
   399 */
   392 void NmApplication::resetViewStack()
   400 void NmApplication::resetViewStack()
   393 {
   401 {
   394     NM_FUNCTION;
       
   395 
       
   396     if (mViewStack && !mViewStack->isEmpty()) {
   402     if (mViewStack && !mViewStack->isEmpty()) {
   397 	    int viewCount = mViewStack->count();
   403 	    int viewCount = mViewStack->count();
   398         // Pop and destroy all views
   404         // Pop and destroy all views
   399 	    for (int i=0 ; i < viewCount ; i++) {
   405 	    for (int i=0 ; i < viewCount ; i++) {
   400 		    NmBaseView *view = mViewStack->pop();
   406 		    NmBaseView *view = mViewStack->pop();
   404 	    mActiveViewId = NmUiViewNone;
   410 	    mActiveViewId = NmUiViewNone;
   405     }
   411     }
   406 }
   412 }
   407 
   413 
   408 /*!
   414 /*!
   409     Function activates view based on viewId parameter.
   415     Function activates view based on viewId parameter. If requested view is
   410     If requested view is already open, it is requested to reload.
   416     already open, it is requested to reload. Otherwise view object is created
   411     Otherwise view object is created and pushed to view stack
   417     and pushed to view stack.
   412 */
   418 */
   413 void NmApplication::enterNmUiView(NmUiStartParam *startParam)
   419 void NmApplication::enterNmUiView(NmUiStartParam *startParam)
   414 {
   420 {
   415     NM_FUNCTION;
   421     NM_FUNCTION;
   416 
   422     
   417     // Check the validity of start parameter object
   423     // Check the validity of start parameter object.
   418     if (startParam) {
   424     if (startParam) {
   419 
   425         
   420         if (startParam->service() && mMainWindow) {
   426         if (startParam->service() && mMainWindow) {
   421 			// When the message list is started as a service previous views are removed
   427 			// When the message list is started as a service previous views
   422 			// from the stack. Open editors are not closed.
   428             // are removed from the stack. Open editors are not closed. Also
   423 			// Also if the view is same than the new one, keep it open (reload the content).
   429             // if the view is same than the new one, keep it open (reload the
   424 
   430             // content).
   425 
   431             
   426 		    // reset the foreground service flag while popping the views
   432 		    // Reset the foreground service flag while popping the views.
   427 		    bool previousForegroundService = mForegroundService;
   433 		    bool previousForegroundService = mForegroundService;
   428 		    mForegroundService = true;
   434 		    mForegroundService = true;
   429 
   435 		    
   430 		    // at least one view must remain in the stack
   436 		    // At least one view must remain in the stack.
   431 			while (mViewStack->count( )> 1) {
   437 			while (mViewStack->count( )> 1) {
   432 			    NmUiViewId topId = mViewStack->top()->nmailViewId();
   438 			    NmUiViewId topId = mViewStack->top()->nmailViewId();
   433 			    if (topId != NmUiViewMessageEditor &&
   439 			    if (topId != NmUiViewMessageEditor &&
   434 			        topId != NmUiViewMailboxList &&
   440 			        topId != NmUiViewMailboxList &&
   435 			        topId != startParam->viewId()) {
   441 			        topId != startParam->viewId()) {
   440 			        break;
   446 			        break;
   441 			    }
   447 			    }
   442 			}
   448 			}
   443 			mForegroundService = previousForegroundService;
   449 			mForegroundService = previousForegroundService;
   444         }
   450         }
   445 
   451         
   446         // Check whether requested view is already active
   452         // Check whether requested view is already active and if so, ask it
   447         // and if so, ask it to reload contents with new start parameter data
   453         // to reload contents with new start parameter data. Do not reuse the
   448         // Do not reuse the view if started as service to editor view (ShareUI)
   454         // view if started as service to editor view (ShareUI).
   449         if (mActiveViewId == startParam->viewId() &&
   455         if (mActiveViewId == startParam->viewId() &&
   450         	(!startParam->service() || mActiveViewId!=NmUiViewMessageEditor)) {
   456         	(!startParam->service() || mActiveViewId!=NmUiViewMessageEditor)) {
   451             mViewStack->top()->reloadViewContents(startParam);
   457             mViewStack->top()->reloadViewContents(startParam);
   452         }
   458         }
   453         else {
   459         else {
   461                 }
   467                 }
   462                 break;
   468                 break;
   463                 case NmUiViewMessageList:
   469                 case NmUiViewMessageList:
   464                 {
   470                 {
   465                     // Check the topmost view. If it is an editor, do not open
   471                     // Check the topmost view. If it is an editor, do not open
   466                     // a new mail list view
   472                     // a new mail list view.
   467                     if (startParam->service() && !mViewStack->isEmpty() &&
   473                     if (startParam->service() && !mViewStack->isEmpty() &&
   468                         mViewStack->top()->nmailViewId()==NmUiViewMessageEditor) {
   474                         mViewStack->top()->nmailViewId()==NmUiViewMessageEditor) {
   469                         break;
   475                         break;
   470                     }
   476                     }
   471                     NmMessageListModel *messageListModel = &mUiEngine->messageListModel(
   477                     NmMessageListModel *messageListModel = &mUiEngine->messageListModel(
   482                     // a new mail search list view.
   488                     // a new mail search list view.
   483                     if (startParam->service() && !mViewStack->isEmpty() &&
   489                     if (startParam->service() && !mViewStack->isEmpty() &&
   484                         mViewStack->top()->nmailViewId() == NmUiViewMessageEditor) {
   490                         mViewStack->top()->nmailViewId() == NmUiViewMessageEditor) {
   485                         break;
   491                         break;
   486                     }
   492                     }
   487 
   493                     
   488                     NmMessageListModel &model =
   494                     NmMessageListModel &model =
   489                         mUiEngine->messageListModelForSearch(startParam->mailboxId());
   495                         mUiEngine->messageListModelForSearch(startParam->mailboxId());
   490 
   496                     
   491                     NmMessageSearchListView *searchListView = new NmMessageSearchListView(
   497                     NmMessageSearchListView *searchListView = new NmMessageSearchListView(
   492                         *this, startParam, *mUiEngine, model,
   498                         *this, startParam, *mUiEngine, model,
   493                         new HbDocumentLoader(mMainWindow));
   499                         new HbDocumentLoader(mMainWindow));
   494 
   500                     
   495                     pushView(searchListView);
   501                     pushView(searchListView);
   496                 }
   502                 }
   497                 break;
   503                 break;
   498                 case NmUiViewMessageViewer:
   504                 case NmUiViewMessageViewer:
   499                     pushView(new NmViewerView(*this, startParam, *mUiEngine,
   505                     pushView(new NmViewerView(*this, startParam, *mUiEngine,
   501                     break;
   507                     break;
   502                 case NmUiViewMessageEditor:
   508                 case NmUiViewMessageEditor:
   503                     pushView(new NmEditorView(*this, startParam, *mUiEngine, *mAttaManager));
   509                     pushView(new NmEditorView(*this, startParam, *mUiEngine, *mAttaManager));
   504                     break;
   510                     break;
   505                 default:
   511                 default:
   506                     // Reset view stack and exit application
   512                     // Reset view stack and exit application.
   507                     delete startParam;
   513                     delete startParam;
   508                     startParam = NULL;
   514                     startParam = NULL;
   509                     resetViewStack();
   515                     resetViewStack();
   510                     break;
   516                     break;
   511             }
   517             }
   512         }
   518         }
   513 
   519         
   514         if (startParam && startParam->service()) {
   520         if (startParam && startParam->service()) {
   515             // Store the view id that was launched as service
   521             // Store the view id that was launched as service.
   516             mServiceViewId = mActiveViewId;
   522             mServiceViewId = mActiveViewId;
   517 		}
   523 		}
   518     }
   524     }
   519 }
   525 }
   520 
   526 
   521 /*!
   527 /*!
   522     Function can be used from views to exit the application
   528     Function can be used from views to exit the application. View stack is
   523     View stack is cleared. Views can connect exit menu
   529     cleared. Views can connect exit menu selection to this slot.
   524     selection to this slot.
       
   525 */
   530 */
   526 void NmApplication::exitApplication()
   531 void NmApplication::exitApplication()
   527 {
   532 {
   528     NM_FUNCTION;
   533     NM_FUNCTION;
   529 
   534     
   530     delete mSendServiceInterface;
   535     delete mSendServiceInterface;
   531     mSendServiceInterface = NULL;
   536     mSendServiceInterface = NULL;
   532     delete mSendServiceInterface2;
   537     delete mSendServiceInterface2;
   533     mSendServiceInterface2 = NULL;
   538     mSendServiceInterface2 = NULL;
   534     delete mUriServiceInterface;
   539     delete mUriServiceInterface;
   540     resetViewStack();
   545     resetViewStack();
   541     qApp->quit();
   546     qApp->quit();
   542 }
   547 }
   543 
   548 
   544 /*!
   549 /*!
   545    Exit the application in the next event loop
   550    Exit the application in the next event loop.
   546 */
   551 */
   547 void NmApplication::delayedExitApplication()
   552 void NmApplication::delayedExitApplication()
   548 {
   553 {
   549     NM_FUNCTION;
   554     // Exit the application in the next event loop.
   550 
       
   551     // Exit the application in the next event loop
       
   552     QMetaObject::invokeMethod(this, "exitApplication", Qt::QueuedConnection);
   555     QMetaObject::invokeMethod(this, "exitApplication", Qt::QueuedConnection);
   553 }
   556 }
   554 
   557 
   555 /*!
   558 /*!
   556     Getter for main window instance.
   559     Getter for main window instance.
   557 */
   560 */
   558 HbMainWindow *NmApplication::mainWindow()
   561 HbMainWindow *NmApplication::mainWindow()
   559 {
   562 {
   560     NM_FUNCTION;
       
   561 
       
   562     return mMainWindow;
   563     return mMainWindow;
   563 }
   564 }
   564 
   565 
   565 /*!
   566 /*!
   566     Getter for main UI extension manager
   567     Getter for main UI extension manager.
   567 */
   568 */
   568 NmUiExtensionManager &NmApplication::extManager()
   569 NmUiExtensionManager &NmApplication::extManager()
   569 {
   570 {
   570     NM_FUNCTION;
       
   571 
       
   572     return *mExtensionManager;
   571     return *mExtensionManager;
   573 }
   572 }
   574 
   573 
   575 /*!
   574 /*!
   576     Getter for network access manager
   575     Getter for network access manager.
   577 */
   576 */
   578 NmViewerViewNetManager &NmApplication::networkAccessManager()
   577 NmViewerViewNetManager &NmApplication::networkAccessManager()
   579 {
   578 {
   580     NM_FUNCTION;
       
   581 
       
   582     return *mNetManager;
   579     return *mNetManager;
   583 }
   580 }
   584 
   581 
   585 /*!
   582 /*!
   586     screen size. Function returns curtent screen size
   583     Get the screen size. Function returns curtent screen size.
   587 */
   584 */
   588 QSize NmApplication::screenSize()
   585 QSize NmApplication::screenSize()
   589 {
   586 {
   590     NM_FUNCTION;
       
   591 
       
   592     QSize ret(0,0);
   587     QSize ret(0,0);
   593     if (mMainWindow){
   588     if (mMainWindow){
   594         HbDeviceProfile currentP = HbDeviceProfile::current();
   589         HbDeviceProfile currentP = HbDeviceProfile::current();
   595         HbDeviceProfile altP(currentP.alternateProfileName());
   590         HbDeviceProfile altP(currentP.alternateProfileName());
   596         QSize curPSize = currentP.logicalSize();
   591         QSize curPSize = currentP.logicalSize();
   597         QSize altPSize = altP.logicalSize();
   592         QSize altPSize = altP.logicalSize();
   598         if (mMainWindow->orientation() == Qt::Horizontal) {
   593         if (mMainWindow->orientation() == Qt::Horizontal) {
   599             // Get wide profile size in landscape
   594             // Get wide profile size in landscape.
   600             if (curPSize.width() > altPSize.width()) {
   595             if (curPSize.width() > altPSize.width()) {
   601                 ret = curPSize;
   596                 ret = curPSize;
   602             }
   597             }
   603             else{
   598             else{
   604                 ret = altPSize;
   599                 ret = altPSize;
   605             }
   600             }
   606         }
   601         }
   607         else {
   602         else {
   608             // Get narrow profile size in portrait
   603             // Get narrow profile size in portrait.
   609             if (curPSize.width() < altPSize.width()) {
   604             if (curPSize.width() < altPSize.width()) {
   610                 ret = curPSize;
   605                 ret = curPSize;
   611             }
   606             }
   612             else{
   607             else{
   613                 ret = altPSize;
   608                 ret = altPSize;
   616     }
   611     }
   617     return ret;
   612     return ret;
   618 }
   613 }
   619 
   614 
   620 /*!
   615 /*!
   621     handles all asynchronous operation's completions at UI level
   616     Handles all asynchronous operation's completions at UI level.
   622 */
   617 */
   623 void NmApplication::handleOperationCompleted(const NmOperationCompletionEvent &event)
   618 void NmApplication::handleOperationCompleted(const NmOperationCompletionEvent &event)
   624 {
   619 {
   625     NM_FUNCTION;
       
   626 
       
   627     if (event.mCompletionCode != NmNoError && event.mCompletionCode != NmCancelError) {
   620     if (event.mCompletionCode != NmNoError && event.mCompletionCode != NmCancelError) {
   628         if (event.mOperationType == Synch && event.mCompletionCode == NmAuthenticationError) {
   621         if (event.mOperationType == Synch && event.mCompletionCode == NmAuthenticationError) {
   629             mLastOperationMailbox = event.mMailboxId;
   622             mLastOperationMailbox = event.mMailboxId;
   630             if (mQueryDialog) {
   623             if (mQueryDialog) {
   631                 delete mQueryDialog;
   624                 delete mQueryDialog;
   641                 mQueryDialog = NULL;
   634                 mQueryDialog = NULL;
   642             }
   635             }
   643             mQueryDialog = NmUtilities::displayQuestionNote(hbTrId("txt_mail_dialog_server_settings_incorrect"),
   636             mQueryDialog = NmUtilities::displayQuestionNote(hbTrId("txt_mail_dialog_server_settings_incorrect"),
   644                                                     this, SLOT(launchSettings(HbAction*)));
   637                                                     this, SLOT(launchSettings(HbAction*)));
   645         }
   638         }
   646         // following applies to all operation/event types
   639         // Following applies to all operation/event types.
   647         if (event.mCompletionCode == NmConnectionError) {
   640         if (event.mCompletionCode == NmConnectionError) {
   648             NmUtilities::displayWarningNote(hbTrId("txt_mail_dialog_mail_connection_error"));
   641             NmUtilities::displayWarningNote(hbTrId("txt_mail_dialog_mail_connection_error"));
   649         }
   642         }
   650     }
   643     }
   651 }
   644 }
   652 
   645 
   653 /*!
   646 /*!
   654     launches settings view of the specified mailbox
   647     Launches settings view of the specified mailbox.
   655 */
   648 */
   656 void NmApplication::launchSettings(HbAction* action)
   649 void NmApplication::launchSettings(HbAction* action)
   657 {
   650 {
   658     NM_FUNCTION;
   651     // Check whether yes button was pressed.
   659 
       
   660     // Check whether yes button was pressed
       
   661     if (mQueryDialog&& action == mQueryDialog->actions().at(0)) {
   652     if (mQueryDialog&& action == mQueryDialog->actions().at(0)) {
   662         // create settingslauncher if doesn't exist
   653         // Create settingslauncher if doesn't exist.
   663         if(!mSettingsViewLauncher) {
   654         if(!mSettingsViewLauncher) {
   664             mSettingsViewLauncher = new NmSettingsViewLauncher();
   655             mSettingsViewLauncher = new NmSettingsViewLauncher();
   665             }
   656             }
   666         // mailboxname required
   657         // Mailboxname required.
   667         NmMailboxMetaData *mailboxMetaData = mUiEngine->mailboxById(mLastOperationMailbox); // no ownership
   658         NmMailboxMetaData *mailboxMetaData = mUiEngine->mailboxById(mLastOperationMailbox); // No ownership.
   668         if( mailboxMetaData ) {
   659         if( mailboxMetaData ) {
   669             // launch
   660             // Launch.
   670             mSettingsViewLauncher->launchSettingsView(mLastOperationMailbox, mailboxMetaData->name());
   661             mSettingsViewLauncher->launchSettingsView(mLastOperationMailbox, mailboxMetaData->name());
   671         }
   662         }
   672     }
   663     }
   673 }
   664 }
   674 
   665 
   675 /*!
   666 /*!
   676 	Stores the visibility state, e.g. when the service was launched.
   667 	Stores the visibility state, e.g. when the service was launched.
   677 	\return true if the app was visible
   668 	\return true if the app was visible.
   678 */
   669 */
   679 bool NmApplication::updateVisibilityState()
   670 bool NmApplication::updateVisibilityState()
   680 {
   671 {
   681     // At the moment there is no good way to check the foreground state
   672     // At the moment there is no good way to check the foreground state.
   682     QWindowSurface *surface = mMainWindow->windowSurface();
   673     QWindowSurface *surface = mMainWindow->windowSurface();
   683 	mForegroundService = (surface != 0);
   674 	mForegroundService = (surface != 0);
   684 	NM_COMMENT(QString("NmApplication::updateVisibilityState fg=%1").arg(mForegroundService));
   675 	NM_COMMENT(QString("NmApplication::updateVisibilityState() : mForegroundService == %1").arg(mForegroundService));
   685 	return mForegroundService;
   676 	return mForegroundService;
   686 }
   677 }