emailuis/nmailui/src/nmapplication.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 #include "nmuiheaders.h"
       
    19 #include <email_services_api.h>
       
    20 #include <e32base.h>
       
    21 
       
    22 static const QString NmSendServiceName = "nmail.com.nokia.symbian.IFileShare";
       
    23 
       
    24 static const QString NmActivityName = "EmailInboxView";
       
    25 
       
    26 /*!
       
    27     \class NmApplication
       
    28     \brief Application class, creates main window and handles view switching.
       
    29 */
       
    30 
       
    31 
       
    32 /*!
       
    33     Constructor.
       
    34 */
       
    35 NmApplication::NmApplication(QObject *parent, quint64 accountId)
       
    36 : QObject(parent),
       
    37   mMainWindow(NULL),
       
    38   mViewStack(NULL),
       
    39   mActiveViewId(NmUiViewNone),
       
    40   mUiEngine(NULL),
       
    41   mBackAction(NULL),
       
    42   mExtensionManager(NULL),
       
    43   mMbListModel(NULL),
       
    44   mServiceViewId(NmUiViewNone),
       
    45   mForegroundService(false),
       
    46   mEffects(NULL),
       
    47   mAttaManager(NULL),
       
    48   mSettingsViewLauncher(NULL),
       
    49   mViewReady(false),
       
    50   mQueryDialog(NULL),
       
    51   mBackButtonPressed(false),
       
    52   mApplicationHidden(false)
       
    53 {
       
    54     TRAP_IGNORE(mUiEngine = NmUiEngine::instance());
       
    55     
       
    56     // Create network access manager and cache for application use.
       
    57     mNetManager = new NmViewerViewNetManager(*mUiEngine);
       
    58     QNetworkDiskCache *cache = new QNetworkDiskCache();
       
    59     cache->setCacheDirectory(
       
    60         QDesktopServices::storageLocation(QDesktopServices::CacheLocation));
       
    61     mNetManager->setCache(cache);
       
    62     
       
    63     createMainWindow();
       
    64     
       
    65     // Attachment manager can be shared between viewer and editor.
       
    66     // The application class has the ownership.
       
    67     mAttaManager = new NmAttachmentManager(*mUiEngine);
       
    68     
       
    69     mSendServiceInterface =
       
    70         new NmSendServiceInterface(NmSendServiceName, NULL, *mUiEngine, this);
       
    71     mSendServiceInterface2 =
       
    72         new NmSendServiceInterface(emailFullServiceNameSend, NULL, *mUiEngine, this);
       
    73     mUriServiceInterface =
       
    74         new NmUriServiceInterface(NULL, *mUiEngine, this);
       
    75     mMailboxServiceInterface =
       
    76         new NmMailboxServiceInterface(NULL, *mUiEngine, this);
       
    77     mViewerServiceInterface =
       
    78         new NmViewerServiceInterface(NULL, this, *mUiEngine);
       
    79     
       
    80     if(accountId != 0) {
       
    81         QVariant mailbox;
       
    82         mailbox.setValue(accountId);
       
    83         mMailboxServiceInterface->displayInboxByMailboxId(mailbox);
       
    84     }
       
    85     
       
    86     mEffects = new NmUiEffects(*mMainWindow);
       
    87     
       
    88     QObject::connect(parent, SIGNAL(activate()), this, SLOT(activityActivated()));
       
    89 }
       
    90 
       
    91 
       
    92 /*!
       
    93     Destructor.
       
    94 */
       
    95 NmApplication::~NmApplication()
       
    96 {
       
    97     // Remove the event filter early since catching application activated/
       
    98     // deactivated events now may cause a crash.
       
    99     QCoreApplication::instance()->removeEventFilter(this);
       
   100 
       
   101     if (mQueryDialog) {
       
   102         delete mQueryDialog;
       
   103         mQueryDialog = NULL;
       
   104     }
       
   105     
       
   106     delete mSendServiceInterface;
       
   107     delete mSendServiceInterface2;
       
   108     delete mUriServiceInterface;
       
   109     delete mMailboxServiceInterface;
       
   110     delete mViewerServiceInterface;
       
   111     
       
   112     resetViewStack();
       
   113     delete mViewStack;
       
   114     
       
   115     NmIcons::freeIcons();
       
   116 
       
   117     NmUiEngine::releaseInstance(mUiEngine);
       
   118     mUiEngine = NULL;
       
   119     
       
   120     delete mBackAction;
       
   121     
       
   122     // Workaround: the main window has to be closed (hidden) before deleting
       
   123     // the extension manager in order to prevent the main window's title bar
       
   124     // from showing when the application is closed.
       
   125     if (mMainWindow) {
       
   126         // Workaround: assert failure may happen if an open main window is
       
   127         // deleted.
       
   128         mMainWindow->close();
       
   129     }
       
   130     
       
   131     delete mExtensionManager;
       
   132     
       
   133     if (mNetManager) {
       
   134         if (mNetManager->cache()) {
       
   135             mNetManager->cache()->clear();
       
   136         }
       
   137 
       
   138         delete mNetManager;
       
   139         mNetManager = NULL;
       
   140     }
       
   141     
       
   142     // Effects need to be deleted before MainWindow.
       
   143     delete mEffects;
       
   144     delete mMainWindow;
       
   145     delete mAttaManager;
       
   146     delete mSettingsViewLauncher;
       
   147 }
       
   148 
       
   149 
       
   150 /*!
       
   151     Main application window creation.
       
   152 */
       
   153 void NmApplication::createMainWindow()
       
   154 {
       
   155     NM_FUNCTION;
       
   156     
       
   157     bool service = XQServiceUtil::isService();
       
   158     
       
   159     // Register custom widget files.
       
   160     HbStyleLoader::registerFilePath(":nmmessagelistviewitem.widgetml");
       
   161     HbStyleLoader::registerFilePath(":nmmessagelistviewitem.css");
       
   162     HbStyleLoader::registerFilePath(":nmviewerheader.widgetml");
       
   163     HbStyleLoader::registerFilePath(":nmviewerheader.css");
       
   164     
       
   165     // Create main window.
       
   166     mMainWindow = new HbMainWindow();
       
   167     
       
   168     // Connect to lazy loading signal.
       
   169     QObject::connect(mMainWindow, SIGNAL(viewReady ()),
       
   170                      this, SLOT(viewReady()));
       
   171     
       
   172     // Create extension manager.
       
   173     mExtensionManager = new NmUiExtensionManager();
       
   174     
       
   175     // Create view stack.
       
   176     mViewStack = new QStack<NmBaseView*>;
       
   177     
       
   178     // Create back action and connect it to prepareForPopView().
       
   179     if (mMainWindow) {
       
   180         mBackAction = new HbAction(Hb::BackNaviAction,this);
       
   181         connect(mBackAction,
       
   182                 SIGNAL(triggered()),
       
   183                 this,
       
   184                 SLOT(prepareForPopView()));
       
   185         
       
   186         // Show mainwindow.
       
   187         // Services will active it when the view is ready.
       
   188         if (!service) {
       
   189             mMainWindow->show();
       
   190         }
       
   191     }
       
   192     
       
   193     // Asynchronous operation completion related notifications.
       
   194     connect(mUiEngine,
       
   195             SIGNAL(operationCompleted(const NmOperationCompletionEvent &)),
       
   196             this,
       
   197             SLOT(handleOperationCompleted(const NmOperationCompletionEvent &)));
       
   198     
       
   199     mMbListModel = &mUiEngine->mailboxListModel();
       
   200     
       
   201 
       
   202     // ----------------------------------------------------------------------
       
   203     
       
   204     if (mMainWindow) {
       
   205         // Start to filter main window events to get "end key" event in all
       
   206         // possible situations. Using event()is not enough to catch the event
       
   207         // as it is only called if the view widget has the focus. Note: if
       
   208         // key capturing (xqkeycapture.h) is required it is probably best to
       
   209         // implement an own QMainWindow class and do the capturing there, not
       
   210         // in the views.
       
   211         mMainWindow->installEventFilter(this);
       
   212         // Optimize the custom paint functions.
       
   213         // Currently effects to NmViewerHeader::paint() and
       
   214         // NmAttachmentListWidget::paint().
       
   215         mMainWindow->setOptimizationFlag(QGraphicsView::DontSavePainterState);    
       
   216     }
       
   217     
       
   218 	// Install the event filter in order to receive ApplicationActivate/Deactivate
       
   219     // events.
       
   220     QCoreApplication::instance()->installEventFilter(this);
       
   221 }
       
   222 
       
   223 
       
   224 /*!
       
   225     Slot. React to view ready signal and call current view method.
       
   226 */
       
   227 void NmApplication::viewReady()
       
   228 {
       
   229     mViewReady = true;
       
   230 
       
   231     if (mViewStack && !mViewStack->isEmpty()) {
       
   232     	NmBaseView *currentView = mViewStack->top();
       
   233 
       
   234         if (currentView) {
       
   235             currentView->viewReady();
       
   236             emit applicationReady();
       
   237         }
       
   238     }
       
   239 }
       
   240 
       
   241 
       
   242 /*!
       
   243     Event filter. End key is filtered from the main window and either the view
       
   244     takes case or the app is exited by default.
       
   245 */
       
   246 bool NmApplication::eventFilter(QObject *obj, QEvent *event)
       
   247 {
       
   248     bool consumed(false);
       
   249 
       
   250     if (obj && obj == mMainWindow && event && event->type() == QEvent::KeyPress) {
       
   251         QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
       
   252         if (keyEvent->key() == Qt::Key_No) {
       
   253             // End key, the "red" key.
       
   254             // Exit application if no pending operations are on-going.
       
   255         }
       
   256     }
       
   257     else if (event && event->type() == QEvent::ApplicationActivate) {
       
   258         NM_COMMENT("NmApplication::eventFilter ApplicationActivate");
       
   259         mApplicationHidden = false;
       
   260 		// Update task switcher name & screenshot, we could have activated into some other mailbox
       
   261         updateActivity();
       
   262     }
       
   263     else if (event && event->type() == QEvent::ApplicationDeactivate) {
       
   264         NM_COMMENT("NmApplication::eventFilter ApplicationDeactivate");
       
   265         // Update the screenshot in the taskswitcher to represent current state
       
   266         if (!mApplicationHidden) {
       
   267             updateActivity();
       
   268         }
       
   269         // hide the sync indicator when app goes to background
       
   270         mUiEngine->enableSyncIndicator(false);
       
   271     }
       
   272     
       
   273     if (!consumed) {
       
   274         consumed = QObject::eventFilter(obj, event);
       
   275     }
       
   276     
       
   277     return consumed;
       
   278 }
       
   279 
       
   280 
       
   281 /*!
       
   282     Pushes the given view into the view stack. The new view is also activated
       
   283     and shown. If the view stack contains other views, the one on the top is
       
   284     hidden.
       
   285 
       
   286     \param newView The view to push into the stack.
       
   287 */
       
   288 void NmApplication::pushView(NmBaseView *newView)
       
   289 {
       
   290     if (newView && mViewStack) {
       
   291         NM_COMMENT("NmApplication::pushView(): The given view is valid.");
       
   292         newView->setNavigationAction(mBackAction);
       
   293         
       
   294         // Store the view to be hidden.
       
   295         NmBaseView *viewToHide(NULL);
       
   296 
       
   297         if (!mViewStack->isEmpty()) {
       
   298             // The stack contains at least one other view.
       
   299             viewToHide = mViewStack->top();
       
   300         }
       
   301 		else {
       
   302 			// viewReady should be informed immediately.
       
   303 			if (mViewReady) {
       
   304 				newView->viewReady();
       
   305 			}
       
   306 		}
       
   307         
       
   308         // Activate the new view.
       
   309     	NM_COMMENT("NmApplication::pushView(): Adding the view into the stack.");
       
   310 
       
   311         mMainWindow->addView(newView);
       
   312         mViewStack->push(newView);
       
   313         mMainWindow->setCurrentView(newView);
       
   314         mActiveViewId = newView->nmailViewId();
       
   315         
       
   316         // Set the orientation of the toolbar.
       
   317         HbToolBar *tb = newView->toolBar();
       
   318 
       
   319         if (tb) {
       
   320             tb->setOrientation(Qt::Horizontal);
       
   321         }
       
   322         
       
   323         // Hide the old view.
       
   324         NM_COMMENT("NmApplication::pushView(): Removing the previous view.");
       
   325 
       
   326         if (viewToHide) {
       
   327             mMainWindow->removeView(viewToHide);
       
   328         }
       
   329     }
       
   330 }
       
   331 
       
   332 /*!
       
   333     Ask from view that is it ok to pop view. This kind of 2-phase popView is
       
   334     needed because view may show query dialog for user response.
       
   335  */
       
   336 void NmApplication::prepareForPopView()
       
   337 {
       
   338     if (mViewStack && mViewStack->size() > 0) {
       
   339         // Get view pointer.
       
   340         NmBaseView *view = mViewStack->top();
       
   341         if (view){
       
   342 			NmUiViewId topViewId = view->nmailViewId();
       
   343 			
       
   344 			// We must know in popView, are we coming with back button from
       
   345 			// message view to prevent the send animation.
       
   346 			if (topViewId == NmUiViewMessageEditor) {
       
   347 				mBackButtonPressed = true;
       
   348 			}
       
   349             
       
   350             // View will call/signal popView if exitting is ok.
       
   351             view->okToExitView();        
       
   352         }
       
   353     }
       
   354     // If the view stack is now empty quit the app. This happens also when
       
   355     // the app has been started as a service
       
   356     else if (mViewStack && mViewStack->size() == 0) {
       
   357         exitApplication();
       
   358     }
       
   359 }
       
   360 
       
   361 
       
   362 /*!
       
   363      Hide the application
       
   364 */
       
   365 void NmApplication::hideApplication()
       
   366 {
       
   367     mApplicationHidden = true;
       
   368     
       
   369     // Hide the application
       
   370     XQServiceUtil::toBackground(true);
       
   371 
       
   372     // hide the sync indicator as well
       
   373     mUiEngine->enableSyncIndicator(false);
       
   374     
       
   375     // Hide the mail from the task switcher 
       
   376     TsTaskSettings taskSettings;
       
   377     taskSettings.setVisibility(false);
       
   378     
       
   379     // Remove also the mailbox item from the task switcher
       
   380     HbApplication* hbApp = dynamic_cast<HbApplication*>(parent());
       
   381     if (hbApp) {
       
   382         hbApp->activityManager()->removeActivity(NmActivityName);
       
   383     }
       
   384 }
       
   385 
       
   386 
       
   387 /*!
       
   388     Pop view from view stack. View object is deleted.
       
   389 */
       
   390 void NmApplication::popView()
       
   391 {
       
   392     if (mViewStack && mViewStack->size() > 0) {
       
   393         NmBaseView *view = mViewStack->top();    
       
   394         if (view) {
       
   395             // Get top view id.
       
   396             NmUiViewId topViewId = view->nmailViewId();
       
   397             
       
   398             // Prepare for send animation if returing from editor and message
       
   399             // has been sent.
       
   400             if (topViewId == NmUiViewMessageEditor && mUiEngine->isSendingMessage()) {
       
   401 				// If we are coming from message editor with back button.
       
   402 				// do not prepare the send animation.
       
   403 				if (!mBackButtonPressed) {
       
   404 					mEffects->prepareEffect(NmUiEffects::NmEditorSendMessageAnimation);
       
   405 				}
       
   406 	        }
       
   407             mBackButtonPressed = false;
       
   408 
       
   409             // Move the application to background if closing the message list view
       
   410             if (topViewId == NmUiViewMessageList && mViewStack->size() == 1) {
       
   411                 hideApplication();
       
   412                 return;
       
   413             }
       
   414 
       
   415             mViewStack->pop();
       
   416             // Call custom exit function.
       
   417             view->aboutToExitView();
       
   418             // Remove view from stack.
       
   419             mMainWindow->removeView(view);
       
   420             
       
   421             // If we were in editor and sent a message, pop viewer from stack
       
   422             // first so we can go straight to mail list.
       
   423             if (!mViewStack->isEmpty() && topViewId == NmUiViewMessageEditor &&
       
   424                 mUiEngine->isSendingMessage() &&
       
   425                 mViewStack->top()->nmailViewId() == NmUiViewMessageViewer) {
       
   426                 NmBaseView *tmpView = mViewStack->pop();
       
   427                 mMainWindow->removeView(tmpView);
       
   428                 delete tmpView;
       
   429                 tmpView = NULL;
       
   430             }
       
   431             
       
   432             if (!mViewStack->isEmpty()) {
       
   433                 // Activate next view in stack.
       
   434                 NmBaseView *showView = mViewStack->top();
       
   435                 mMainWindow->addView(showView);
       
   436                 mMainWindow->setCurrentView(showView);
       
   437                 // Store activated view id.
       
   438                 mActiveViewId=showView->nmailViewId();
       
   439                 // Perform send animation if requested.
       
   440                 mEffects->startEffect(NmUiEffects::NmEditorSendMessageAnimation);
       
   441             }
       
   442             
       
   443             delete view;
       
   444             view = NULL;
       
   445             
       
   446             // If view was started as service, move the app now to the
       
   447             // background, unless it was started when the app was already in
       
   448             // foreground.
       
   449             if (mServiceViewId == topViewId) {
       
   450                 mServiceViewId = NmUiViewNone;
       
   451                 NM_COMMENT("NmApplication::popView() : returned from service view.");
       
   452                 // If started as embedded or while the app was in foreground,
       
   453                 // do not hide the app.
       
   454                 if (!XQServiceUtil::isEmbedded() &&
       
   455                     !mForegroundService) {
       
   456                     XQServiceUtil::toBackground(true);
       
   457                 }
       
   458             }
       
   459         }
       
   460     }
       
   461     
       
   462     // If the view stack is now empty quit the app. This happens also when
       
   463     // the app has been started as a service.
       
   464     if (mViewStack && mViewStack->size() == 0) {
       
   465         exitApplication();
       
   466     }
       
   467 }
       
   468 
       
   469 
       
   470 /*!
       
   471     Reset view stack. Remove and destroy view objects.
       
   472 */
       
   473 void NmApplication::resetViewStack()
       
   474 {
       
   475     if (mViewStack && !mViewStack->isEmpty()) {
       
   476 	    int viewCount = mViewStack->count();
       
   477         // Pop and destroy all views
       
   478 	    for (int i=0 ; i < viewCount ; i++) {
       
   479 		    NmBaseView *view = mViewStack->pop();
       
   480             mMainWindow->removeView(view);
       
   481             delete view;
       
   482         }
       
   483 	    mActiveViewId = NmUiViewNone;
       
   484     }
       
   485 }
       
   486 
       
   487 
       
   488 /*!
       
   489     Function activates view based on viewId parameter. If requested view is
       
   490     already open, it is requested to reload. Otherwise view object is created
       
   491     and pushed to view stack.
       
   492 */
       
   493 void NmApplication::enterNmUiView(NmUiStartParam *startParam)
       
   494 {
       
   495     NM_FUNCTION;
       
   496     
       
   497     // Check the validity of start parameter object.
       
   498     if (startParam) {
       
   499     
       
   500         mCurrentMailboxId = startParam->mailboxId();
       
   501         
       
   502         if (startParam->service() && mMainWindow) {
       
   503 			// When the message list is started as a service previous views
       
   504             // are removed from the stack. Open editors are closed. Also
       
   505             // if the view is same than the new one, keep it open (reload the
       
   506             // content).
       
   507             
       
   508 		    // Reset the foreground service flag while popping the views.
       
   509 		    bool previousForegroundService = mForegroundService;
       
   510 		    mForegroundService = true;
       
   511 		    
       
   512 		    // At least one view must remain in the stack.
       
   513 			while (mViewStack->count( )> 1) {
       
   514 			    NmUiViewId topId = mViewStack->top()->nmailViewId();
       
   515 			    if (topId != NmUiViewMessageEditor &&
       
   516 			        topId != NmUiViewMailboxList &&
       
   517 			        topId != startParam->viewId()) {
       
   518 			        prepareForPopView();
       
   519 			    }
       
   520 			    else {
       
   521 			        // Editor or mailbox list in the top. Stop the loop.
       
   522 			        break;
       
   523 			    }
       
   524 			}
       
   525 			mForegroundService = previousForegroundService;
       
   526         }
       
   527         
       
   528         // Check whether requested view is already active and if so, ask it
       
   529         // to reload contents with new start parameter data. Do not reuse the
       
   530         // view if started as service to editor view (ShareUI).
       
   531         if (mActiveViewId == startParam->viewId() &&
       
   532         	(!startParam->service() || mActiveViewId!=NmUiViewMessageEditor)) {
       
   533             mViewStack->top()->reloadViewContents(startParam);
       
   534         }
       
   535         else {
       
   536             switch (startParam->viewId()) {
       
   537                 case NmUiViewMailboxList:
       
   538                 {
       
   539                     NmMailboxListView *mbListView = new NmMailboxListView(
       
   540                     		*this, startParam, *mUiEngine,
       
   541                     		*mMbListModel, new HbDocumentLoader(mMainWindow));
       
   542                     pushView(mbListView);
       
   543                 }
       
   544                 break;
       
   545                 case NmUiViewMessageList:
       
   546                 {
       
   547                     // Check the topmost view. If it is an editor, save to draft and close it.
       
   548                     if (startParam->service() && !mViewStack->isEmpty() && 
       
   549                         mViewStack->top()->nmailViewId()==NmUiViewMessageEditor) {
       
   550                         QMetaObject::invokeMethod(mViewStack->top(),
       
   551                             "safeToDraft", Qt::DirectConnection);
       
   552                         popView();
       
   553                     }
       
   554 
       
   555                     NmMessageListModel *messageListModel =
       
   556                         &mUiEngine->messageListModel(startParam->mailboxId(),
       
   557                                                      startParam->folderId());
       
   558                     NmMessageListView *msgList =
       
   559                         new NmMessageListView(*this, startParam, *mUiEngine,
       
   560                                               *mMbListModel, messageListModel,
       
   561                                               new HbDocumentLoader(mMainWindow));
       
   562                     pushView(msgList);
       
   563                     
       
   564                     // Inform other processes about this event.
       
   565                     NmUiEventsNotifier::notifyViewStateChanged(NmUiEventsNotifier::NmViewShownEvent,
       
   566                                                                NmUiViewMessageList,
       
   567                                                                startParam->mailboxId());
       
   568                 }
       
   569                 break;
       
   570                 case NmUiViewMessageSearchList:
       
   571                 {
       
   572                     // Check the topmost view. If it is an editor, save to draft and close it.
       
   573                     if (startParam->service() && !mViewStack->isEmpty() && 
       
   574                         mViewStack->top()->nmailViewId()==NmUiViewMessageEditor) {
       
   575                         QMetaObject::invokeMethod(mViewStack->top(),
       
   576                             "safeToDraft", Qt::DirectConnection);
       
   577                         popView();
       
   578                     }
       
   579                     
       
   580                     NmMessageListModel &model =
       
   581                         mUiEngine->messageListModelForSearch(startParam->mailboxId());
       
   582                     
       
   583                     NmMessageSearchListView *searchListView = new NmMessageSearchListView(
       
   584                         *this, startParam, *mUiEngine, model,
       
   585                         new HbDocumentLoader(mMainWindow));
       
   586                     
       
   587                     pushView(searchListView);
       
   588                 }
       
   589                 break;
       
   590                 case NmUiViewMessageViewer:
       
   591                     pushView(new NmViewerView(*this, startParam, *mUiEngine,
       
   592                             mMainWindow, *mAttaManager));
       
   593                     break;
       
   594                 case NmUiViewMessageEditor:
       
   595                     // Check the topmost view. If it is an editor, save to draft and close it.
       
   596                     if (startParam->service() && !mViewStack->isEmpty() && 
       
   597                         mViewStack->top()->nmailViewId()==NmUiViewMessageEditor) {
       
   598                         QMetaObject::invokeMethod(mViewStack->top(),
       
   599                             "safeToDraft", Qt::DirectConnection);
       
   600                         popView();
       
   601                     }
       
   602                     pushView(new NmEditorView(*this, startParam, *mUiEngine, *mAttaManager));
       
   603                     break;
       
   604                 default:
       
   605                     // Reset view stack and exit application.
       
   606                     delete startParam;
       
   607                     startParam = NULL;
       
   608                     resetViewStack();
       
   609                     break;
       
   610             }
       
   611         }
       
   612         
       
   613         if (startParam && startParam->service()) {
       
   614             // Store the view id that was launched as service.
       
   615             mServiceViewId = mActiveViewId;
       
   616 		}
       
   617     }
       
   618 }
       
   619 
       
   620 
       
   621 /*!
       
   622     Function can be used from views to exit the application. View stack is
       
   623     cleared. Views can connect exit menu selection to this slot.
       
   624 */
       
   625 void NmApplication::exitApplication()
       
   626 {
       
   627     NM_FUNCTION;
       
   628     
       
   629     HbApplication* hbApp = dynamic_cast<HbApplication*>(parent());
       
   630     hbApp->activityManager()->removeActivity("EmailInboxView");
       
   631     
       
   632     delete mSendServiceInterface;
       
   633     mSendServiceInterface = NULL;
       
   634     delete mSendServiceInterface2;
       
   635     mSendServiceInterface2 = NULL;
       
   636     delete mUriServiceInterface;
       
   637     mUriServiceInterface = NULL;
       
   638     delete mMailboxServiceInterface;
       
   639     mMailboxServiceInterface = NULL;
       
   640     delete mViewerServiceInterface;
       
   641     mViewerServiceInterface = NULL;
       
   642     resetViewStack();
       
   643     qApp->quit();
       
   644 }
       
   645 
       
   646 
       
   647 /*!
       
   648    Exit the application in the next event loop.
       
   649 */
       
   650 void NmApplication::delayedExitApplication()
       
   651 {
       
   652     // Exit the application in the next event loop.
       
   653     QMetaObject::invokeMethod(this, "exitApplication", Qt::QueuedConnection);
       
   654 }
       
   655 
       
   656 
       
   657 /*!
       
   658     Getter for main window instance.
       
   659 */
       
   660 HbMainWindow *NmApplication::mainWindow()
       
   661 {
       
   662     return mMainWindow;
       
   663 }
       
   664 
       
   665 
       
   666 /*!
       
   667     Getter for main UI extension manager.
       
   668 */
       
   669 NmUiExtensionManager &NmApplication::extManager()
       
   670 {
       
   671     return *mExtensionManager;
       
   672 }
       
   673 
       
   674 
       
   675 /*!
       
   676     Getter for network access manager.
       
   677 */
       
   678 NmViewerViewNetManager &NmApplication::networkAccessManager()
       
   679 {
       
   680     return *mNetManager;
       
   681 }
       
   682 
       
   683 
       
   684 /*!
       
   685     Get the screen size. Function returns curtent screen size.
       
   686 */
       
   687 QSize NmApplication::screenSize()
       
   688 {
       
   689     QSize ret(0,0);
       
   690     if (mMainWindow){
       
   691         HbDeviceProfile currentP = HbDeviceProfile::current();
       
   692         HbDeviceProfile altP(currentP.alternateProfileName());
       
   693         QSize curPSize = currentP.logicalSize();
       
   694         QSize altPSize = altP.logicalSize();
       
   695         if (mMainWindow->orientation() == Qt::Horizontal) {
       
   696             // Get wide profile size in landscape.
       
   697             if (curPSize.width() > altPSize.width()) {
       
   698                 ret = curPSize;
       
   699             }
       
   700             else{
       
   701                 ret = altPSize;
       
   702             }
       
   703         }
       
   704         else {
       
   705             // Get narrow profile size in portrait.
       
   706             if (curPSize.width() < altPSize.width()) {
       
   707                 ret = curPSize;
       
   708             }
       
   709             else{
       
   710                 ret = altPSize;
       
   711             }
       
   712         }
       
   713     }
       
   714     return ret;
       
   715 }
       
   716 
       
   717 
       
   718 /*!
       
   719     Handles all asynchronous operation's completions at UI level.
       
   720 */
       
   721 void NmApplication::handleOperationCompleted(const NmOperationCompletionEvent &event)
       
   722 {
       
   723     if (event.mCompletionCode != NmNoError && event.mCompletionCode != NmCancelError) {
       
   724         if (event.mOperationType == Synch && event.mCompletionCode == NmAuthenticationError) {
       
   725             mLastOperationMailbox = event.mMailboxId;
       
   726             if (mQueryDialog) {
       
   727                 delete mQueryDialog;
       
   728                 mQueryDialog = NULL;
       
   729             }
       
   730             mQueryDialog = NmUtilities::displayQuestionNote(hbTrId("txt_mail_dialog_address_or_password_incorrect"),
       
   731                                                     this, SLOT(launchSettings(HbAction*)));
       
   732         }
       
   733         if (event.mOperationType == Synch && event.mCompletionCode == NmServerConnectionError) {
       
   734             mLastOperationMailbox=event.mMailboxId;
       
   735             if (mQueryDialog) {
       
   736                 delete mQueryDialog;
       
   737                 mQueryDialog = NULL;
       
   738             }
       
   739             mQueryDialog = NmUtilities::displayQuestionNote(hbTrId("txt_mail_dialog_server_settings_incorrect"),
       
   740                                                     this, SLOT(launchSettings(HbAction*)));
       
   741         }
       
   742         // Following applies to all operation/event types.
       
   743         if (event.mCompletionCode == NmConnectionError) {
       
   744             NmUtilities::displayWarningNote(hbTrId("txt_mail_dialog_mail_connection_error"));
       
   745         }
       
   746     }
       
   747 }
       
   748 
       
   749 
       
   750 /*!
       
   751     Launches settings view of the specified mailbox.
       
   752 */
       
   753 void NmApplication::launchSettings(HbAction* action)
       
   754 {
       
   755     // Check whether yes button was pressed.
       
   756     if (mQueryDialog&& action == mQueryDialog->actions().at(0)) {
       
   757         // Create settingslauncher if doesn't exist.
       
   758         if(!mSettingsViewLauncher) {
       
   759             mSettingsViewLauncher = new NmSettingsViewLauncher();
       
   760             }
       
   761         // Mailboxname required.
       
   762         NmMailboxMetaData *mailboxMetaData = mUiEngine->mailboxById(mLastOperationMailbox); // No ownership.
       
   763         if( mailboxMetaData ) {
       
   764             // Launch.
       
   765             mSettingsViewLauncher->launchSettingsView(mLastOperationMailbox, mailboxMetaData->name());
       
   766         }
       
   767     }
       
   768 }
       
   769 
       
   770 
       
   771 /*!
       
   772    Check the foreground status of the application 
       
   773    \return true if the application is in the foreground
       
   774 */
       
   775 bool NmApplication::isForeground() const
       
   776 {
       
   777     // At the moment there is no good way to check the foreground state.
       
   778     QWindowSurface *surface = mMainWindow->windowSurface();
       
   779     return (surface != 0);
       
   780 }
       
   781 
       
   782 /*!
       
   783 	Stores the visibility state, e.g. when the service was launched.
       
   784 	\return true if the app was visible.
       
   785 */
       
   786 bool NmApplication::updateVisibilityState()
       
   787 {
       
   788 	mForegroundService = isForeground();
       
   789 	NM_COMMENT(QString("NmApplication::updateVisibilityState() : mForegroundService == %1").arg(mForegroundService));
       
   790 	return mForegroundService;
       
   791 }
       
   792 
       
   793 
       
   794 /*!
       
   795     Update the thumbnail in the task switcher.
       
   796 */
       
   797 void NmApplication::updateActivity()
       
   798 {
       
   799     NmMailboxMetaData *meta = mUiEngine->mailboxById(mCurrentMailboxId);
       
   800     HbApplication* hbApp = dynamic_cast<HbApplication*>(parent());
       
   801 
       
   802     if (hbApp) {
       
   803         // This will ensure that when service is started as a embedded service and a mail 
       
   804         // process already exists the task activity will show the embedded service inside the 
       
   805         // calling processes activity and the already running mail process in its own activity.
       
   806         if(!XQServiceUtil::isService() || !XQServiceUtil::isEmbedded()) {
       
   807             if (meta) {
       
   808                 TsTaskSettings tasksettings;
       
   809                 tasksettings.setVisibility(false);
       
   810                 QVariantHash metadata;
       
   811                 metadata.insert(ActivityScreenshotKeyword, QPixmap::grabWidget(mainWindow(), mainWindow()->rect()));
       
   812                 metadata.insert(ActivityApplicationName, meta->name());
       
   813                 metadata.insert(ActivityVisibility, true);
       
   814                 hbApp->activityManager()->removeActivity(NmActivityName);
       
   815                 hbApp->activityManager()->addActivity(NmActivityName, QVariant(), metadata);
       
   816             }
       
   817             else {
       
   818                 hbApp->activityManager()->removeActivity(NmActivityName);
       
   819                 TsTaskSettings tasksettings;
       
   820                 tasksettings.setVisibility(true);
       
   821             }
       
   822         }
       
   823     }
       
   824 }
       
   825 
       
   826 /*!
       
   827 	Switch to activated mailbox
       
   828 */
       
   829 void NmApplication::activityActivated()
       
   830 {
       
   831     HbApplication* hbApp = dynamic_cast<HbApplication*>(parent());
       
   832     if (hbApp) {
       
   833         quint64 accountId(0);
       
   834         QString activateId = hbApp->activateId();
       
   835         if (hbApp->activateReason() == Hb::ActivationReasonActivity &&
       
   836                 activateId.startsWith(NmActivityName) ) {
       
   837             QString accountIdString = activateId.mid(NmActivityName.length());
       
   838             accountId = accountIdString.toULongLong();
       
   839             QVariant mailbox;
       
   840             mailbox.setValue(accountId);
       
   841             mMailboxServiceInterface->displayInboxByMailboxId(mailbox);
       
   842         }
       
   843     }
       
   844 }
       
   845