meetingrequest/mrbcplugin/bcmrevent/src/cmrbcplugincreatenewmrcmd.cpp
branchRCL_3
changeset 12 4ce476e64c59
child 16 b5fbb9b25d57
equal deleted inserted replaced
11:0396474f30f5 12:4ce476e64c59
       
     1 /*
       
     2 * Copyright (c) 2008 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:  MBUtils ECOM implementation
       
    15 *
       
    16 */
       
    17 
       
    18 #include "cmrbcplugincreatenewmrcmd.h"
       
    19 #include "cesmrviewerctrl.h"
       
    20 #include "esmrcommands.h"
       
    21 #include "tmroutputparams.h"
       
    22 #include "esmrconfig.hrh"
       
    23 
       
    24 #include <calenservices.h>
       
    25 #include <calencommands.hrh>
       
    26 #include <caleninterimutils2.h>
       
    27 #include <cmrviewers.h>
       
    28 #include <calentry.h>
       
    29 #include <caluser.h>
       
    30 #include <cmrmailboxutils.h>
       
    31 #include <ct/rcpointerarray.h>
       
    32 #include <aknlists.h>
       
    33 #include <aknpopup.h>
       
    34 #include <utf.h>
       
    35 #include <bldvariant.hrh>
       
    36 
       
    37 #include "emailtrace.h"
       
    38 
       
    39 namespace { // codescanner::namespace
       
    40 
       
    41 /**
       
    42  * Literal for BC viewer / editor
       
    43  */
       
    44 _LIT8( KBCViewer, "[2001F406]" );
       
    45 
       
    46 /**
       
    47  * Default sequence number
       
    48  */
       
    49 const TInt KDefaultSeqNo( 0 );
       
    50 
       
    51 /**
       
    52  * Constant for default duration in minutes
       
    53  */
       
    54 const TInt KDefaultDurationInMinutes( 60 );
       
    55 
       
    56 #ifdef _DEBUG
       
    57 
       
    58 /**
       
    59  * Panic literal
       
    60  */
       
    61 _LIT( KMRBCPluginCreateNewMRCmd, "MRBCPluginCreateNewMRCmd" );
       
    62 
       
    63 /**
       
    64  * Panic codes
       
    65  */
       
    66 enum TMRBCPluginCreateNewMRCmdPanic
       
    67 	{
       
    68 	EInvalidCommand // Invalid command
       
    69 	};
       
    70 
       
    71 void Panic( TMRBCPluginCreateNewMRCmdPanic aPanic )
       
    72 	{
       
    73 	User::Panic( KMRBCPluginCreateNewMRCmd, aPanic );
       
    74 	}
       
    75 
       
    76 #endif // _DEBUG
       
    77 
       
    78 /**
       
    79  * Fetches the supported mailboxes.
       
    80  * @param aMBUtils utils class
       
    81  * @param aMailBoxes to test
       
    82  */
       
    83 void SupportedMailboxesL(
       
    84         CMRMailboxUtils& aMBUtils,
       
    85         RArray<CMRMailboxUtils::TMailboxInfo>& aMailBoxes )
       
    86     {
       
    87     FUNC_LOG;
       
    88     aMBUtils.ListMailBoxesL( aMailBoxes );
       
    89 
       
    90     RCPointerArray<CImplementationInformation> implArray;
       
    91     CleanupClosePushL( implArray );
       
    92 
       
    93     //Get all MRViewers Implementation
       
    94     const TUid mrViewersIface = TUid::Uid(KMRViewersInterfaceUID);
       
    95     REComSession::ListImplementationsL(mrViewersIface, implArray );
       
    96     TInt mrviewerCount( implArray.Count() );
       
    97 
       
    98     TInt index(0);
       
    99     TInt mailboxCount( aMailBoxes.Count() );
       
   100     while ( index < mailboxCount )
       
   101          {
       
   102          TBool supported( EFalse );
       
   103 
       
   104          for ( TInt i(0); (i < mrviewerCount) && !supported; ++ i )
       
   105              {
       
   106              TBuf16<KMaxUidName> mbName;
       
   107              CnvUtfConverter::ConvertToUnicodeFromUtf8(
       
   108                      mbName,
       
   109                      implArray[i]->DataType() );
       
   110 
       
   111              if( aMailBoxes[index].iMtmUid.Name().CompareF(mbName) == 0)
       
   112                   {
       
   113                   supported = ETrue;
       
   114                  }
       
   115              }
       
   116 
       
   117          if ( supported )
       
   118              {
       
   119              index++;
       
   120              }
       
   121          else
       
   122              {
       
   123              aMailBoxes.Remove( index );
       
   124              mailboxCount = aMailBoxes.Count();
       
   125              }
       
   126          }
       
   127     CleanupStack::PopAndDestroy( &implArray );
       
   128     }
       
   129 
       
   130 /**
       
   131  * Prompts user to select default mailbox.
       
   132  * @param aMailBoxes Reference to mailbox list
       
   133  */
       
   134 TInt PromptForDefaultMailboxL(
       
   135         RArray<CMRMailboxUtils::TMailboxInfo>& aMailBoxes )
       
   136     {
       
   137     FUNC_LOG;
       
   138     TInt selected( KErrCancel );
       
   139 
       
   140     TInt mbCount = aMailBoxes.Count();
       
   141     if( mbCount > 0)
       
   142         {
       
   143         CAknSinglePopupMenuStyleListBox* list =
       
   144             new (ELeave) CAknSinglePopupMenuStyleListBox;
       
   145         CleanupStack::PushL(list);
       
   146 
       
   147         CAknPopupList* popupList = CAknPopupList::NewL(
       
   148                                             list,
       
   149                                             R_AVKON_SOFTKEYS_OK_CANCEL);
       
   150         CleanupStack::PushL(popupList);
       
   151 
       
   152         list->ConstructL(popupList, CEikListBox::ELeftDownInViewRect);
       
   153         list->CreateScrollBarFrameL(ETrue);
       
   154         list->ScrollBarFrame()->SetScrollBarVisibilityL(
       
   155             CEikScrollBarFrame::EOff, CEikScrollBarFrame::EAuto);
       
   156 
       
   157         CEikonEnv* eikEnv = CEikonEnv::Static();// codescanner::eikonenvstatic
       
   158 
       
   159         CDesCArrayFlat* items = new (ELeave)CDesCArrayFlat(mbCount);
       
   160         CleanupStack::PushL(items);
       
   161         for(TInt i=0; i<mbCount; ++i)
       
   162             {
       
   163             items->AppendL( aMailBoxes[i].iName );
       
   164             }
       
   165         CleanupStack::Pop(items);
       
   166         CTextListBoxModel* model = list->Model();
       
   167 
       
   168         //Pass ownersip of items to model
       
   169         model->SetItemTextArray(items);
       
   170 
       
   171         HBufC* title = KNullDesC().AllocLC();
       
   172         popupList->SetTitleL(*title);
       
   173         CleanupStack::PopAndDestroy(title);
       
   174 
       
   175         TBool accepted = popupList->ExecuteLD();
       
   176         CleanupStack::Pop( popupList );
       
   177 
       
   178         if(accepted)
       
   179             {
       
   180             selected = list->CurrentItemIndex();
       
   181             }
       
   182         else
       
   183             {
       
   184             selected = KErrCancel;
       
   185             }
       
   186 
       
   187         CleanupStack::PopAndDestroy( list );
       
   188         }
       
   189     else
       
   190         {
       
   191         //No mailboxes defined.  Could prompt user to define one here.
       
   192         selected = KErrCancel;
       
   193         }
       
   194 
       
   195     return selected;
       
   196     }
       
   197 
       
   198 /**
       
   199  * Test if this is valid command to be executed by this object.
       
   200  * @param aCommand Reference to command object
       
   201  * @return ETrue if this is valid command, EFalse otherwise
       
   202  */
       
   203 TBool IsValidCommand(
       
   204         const TCalenCommand& aCommand )
       
   205     {
       
   206     TBool retValue( EFalse );
       
   207 
       
   208     TInt aCommandId( aCommand.Command() );
       
   209 
       
   210     if ( ECalenNewMeetingRequest == aCommandId ||
       
   211             ECalenNewMeetingTimeSpan == aCommandId )
       
   212         {
       
   213         retValue = ETrue;
       
   214         }
       
   215 
       
   216     return retValue;
       
   217     }
       
   218 
       
   219 }
       
   220 
       
   221 // -----------------------------------------------------------------------------
       
   222 // CMRBCPluginCreateNewMRCmd::CMRBCPluginCreateNewMRCmd
       
   223 // -----------------------------------------------------------------------------
       
   224 //
       
   225 CMRBCPluginCreateNewMRCmd::CMRBCPluginCreateNewMRCmd(
       
   226 		MCalenServices& aServices )
       
   227 :	iServices( aServices )
       
   228 	{
       
   229 	FUNC_LOG;
       
   230 	}
       
   231 
       
   232 // -----------------------------------------------------------------------------
       
   233 // CMRBCPluginCreateNewMRCmd::~CMRBCPluginCreateNewMRCmd
       
   234 // -----------------------------------------------------------------------------
       
   235 //
       
   236 CMRBCPluginCreateNewMRCmd::~CMRBCPluginCreateNewMRCmd()
       
   237 	{
       
   238 	FUNC_LOG;
       
   239 	delete iEntry;
       
   240 	delete iEditor;
       
   241 	}
       
   242 
       
   243 // -----------------------------------------------------------------------------
       
   244 // CMRBCPluginCreateNewMRCmd::ConstructL
       
   245 // -----------------------------------------------------------------------------
       
   246 //
       
   247 CMRBCPluginCreateNewMRCmd* CMRBCPluginCreateNewMRCmd::NewL(
       
   248 		MCalenServices& aServices )
       
   249 	{
       
   250 	FUNC_LOG;
       
   251 
       
   252 	CMRBCPluginCreateNewMRCmd* self =
       
   253 			new (ELeave) CMRBCPluginCreateNewMRCmd( aServices );
       
   254 	CleanupStack::PushL( self );
       
   255 	self->ConstructL();
       
   256 	CleanupStack::Pop( self );
       
   257 	return self;
       
   258 	}
       
   259 
       
   260 // -----------------------------------------------------------------------------
       
   261 // CMRBCPluginCreateNewMRCmd::ConstructL
       
   262 // -----------------------------------------------------------------------------
       
   263 //
       
   264 void CMRBCPluginCreateNewMRCmd::ConstructL()
       
   265 	{
       
   266 	FUNC_LOG;
       
   267 	}
       
   268 
       
   269 // -----------------------------------------------------------------------------
       
   270 // CMRBCPluginCreateNewMRCmd::ExecuteCommandL
       
   271 // -----------------------------------------------------------------------------
       
   272 //
       
   273 void CMRBCPluginCreateNewMRCmd::ExecuteCommandL(
       
   274 		const TCalenCommand& aCommand )
       
   275 	{
       
   276 	FUNC_LOG;
       
   277 
       
   278 	TBool validCommand( IsValidCommand(aCommand) );
       
   279 
       
   280 	__ASSERT_DEBUG(
       
   281 	        validCommand,
       
   282 			Panic( EInvalidCommand ) );
       
   283 
       
   284 	if ( !validCommand )
       
   285 		{
       
   286 		ERROR( KErrArgument, "Invalid command" );
       
   287 		User::Leave( KErrArgument );
       
   288 		}
       
   289 
       
   290 
       
   291 	CreateEntryL( aCommand );
       
   292     LaunchMREditorL( aCommand );
       
   293 	}
       
   294 
       
   295 // ---------------------------------------------------------------------------
       
   296 // CMRBCPluginCreateNewMRCmd::ProcessCommandL
       
   297 // ---------------------------------------------------------------------------
       
   298 //
       
   299 void CMRBCPluginCreateNewMRCmd::ProcessCommandL(
       
   300 		TInt /*aCommandId*/ )
       
   301 	{
       
   302 	FUNC_LOG;
       
   303 	}
       
   304 
       
   305 // ---------------------------------------------------------------------------
       
   306 // CMRBCPluginCreateNewMRCmd::ProcessCommandWithResultL
       
   307 // ---------------------------------------------------------------------------
       
   308 //
       
   309 TInt CMRBCPluginCreateNewMRCmd::ProcessCommandWithResultL( // codescanner::intleaves
       
   310 		TInt /*aCommandId*/ )
       
   311 	{
       
   312 	FUNC_LOG;
       
   313 	return KErrNone;
       
   314 	}
       
   315 
       
   316 // ---------------------------------------------------------------------------
       
   317 // CMRBCPluginCreateNewMRCmd::LaunchMREditorL
       
   318 // ---------------------------------------------------------------------------
       
   319 //
       
   320 void CMRBCPluginCreateNewMRCmd::LaunchMREditorL(
       
   321         const TCalenCommand& aCommand )
       
   322     {
       
   323     FUNC_LOG;
       
   324 
       
   325     const TUid KCalendarUid = { 0x10005901 };
       
   326 
       
   327     // Constructing input parameters
       
   328     INFO( "Constructing input parameters" );
       
   329     MAgnEntryUi::TAgnEntryUiInParams inParams(
       
   330             KCalendarUid,
       
   331             iServices.SessionL(),
       
   332             MAgnEntryUi::ECreateNewEntry );
       
   333 
       
   334     // Set time for input parameters
       
   335     INFO( "Set time for input parameters" );
       
   336     TCalTime startTime = iEntry->StartTimeL();
       
   337     TCalTime::TTimeMode timemode = startTime.TimeMode();
       
   338     if (timemode == TCalTime::EFloating )
       
   339         {
       
   340         inParams.iInstanceDate.SetTimeLocalFloatingL (
       
   341               startTime.TimeLocalL() );
       
   342         }
       
   343     else
       
   344         {
       
   345         inParams.iInstanceDate.SetTimeUtcL (
       
   346                 startTime.TimeUtcL() );
       
   347         }
       
   348     inParams.iMsgSession = NULL;
       
   349 
       
   350     // Output parameters
       
   351     MAgnEntryUi::TAgnEntryUiOutParams outParams;
       
   352     TMROutputParams outputParams;
       
   353     outParams.iSpare = reinterpret_cast< TInt >( &outputParams );
       
   354 
       
   355     // Launch Entry UI
       
   356     INFO( "Launch Entry UI" );
       
   357     RPointerArray<CCalEntry> entries;
       
   358     CleanupClosePushL(entries);
       
   359     entries.AppendL(iEntry);
       
   360 
       
   361     AddOrganizerL();
       
   362 
       
   363     CCalenInterimUtils2& utils = iServices.InterimUtilsL();
       
   364     if ( utils.MRViewersEnabledL( ETrue ) )
       
   365         {
       
   366         iEditor = CESMRViewerController::NewL(
       
   367                 KBCViewer(),
       
   368                 entries,
       
   369                 inParams,
       
   370                 outParams,
       
   371                 *this );
       
   372         }
       
   373     else
       
   374         {
       
   375         // MR is not supported --> Leave
       
   376         User::Leave (KErrNotSupported );
       
   377         }
       
   378 
       
   379     iEditor->ExecuteL();
       
   380 
       
   381     TCalenNotification notification( ECalenNotifyEntryClosed );
       
   382 
       
   383     if ( MAgnEntryUi::ENoAction != outParams.iAction )
       
   384         {
       
   385         // Update context and issue notification before confirmation dialog
       
   386         // to avoid delay of updating title pane
       
   387         MCalenContext& context = iServices.Context();
       
   388         aCommand.GetContextL( context );
       
   389 
       
   390         // Update context and issue notification before confirmation dialog
       
   391         // to avoid delay of updating title pane
       
   392         TCalenInstanceId id = TCalenInstanceId::CreateL(
       
   393                 *iEntry,
       
   394                 outParams.iNewInstanceDate );
       
   395 
       
   396         context.SetInstanceIdL( id, context.ViewId() );
       
   397         notification = ECalenNotifyEntrySaved;
       
   398         }
       
   399     else
       
   400         {
       
   401         TMROutputParams* params =
       
   402             ( TMROutputParams* )( outParams.iSpare );
       
   403 
       
   404         // If viewer options menu exit is desired
       
   405         if( params->iCommand == EMRDialogOptionsMenuExit )
       
   406             {
       
   407             notification = ECalenNotifyRealExit;
       
   408             }
       
   409         }
       
   410 
       
   411     iServices.IssueNotificationL( notification );
       
   412     IssueCommandL( outParams );
       
   413     CleanupStack::PopAndDestroy( &entries );
       
   414     iEntry = NULL;
       
   415     }
       
   416 
       
   417 // ---------------------------------------------------------------------------
       
   418 // CMRBCPluginCreateNewMRCmd::CreateEntryL
       
   419 // ---------------------------------------------------------------------------
       
   420 //
       
   421 void CMRBCPluginCreateNewMRCmd::CreateEntryL(
       
   422 		const TCalenCommand& aCommand )
       
   423     {
       
   424     FUNC_LOG;
       
   425 
       
   426     CCalenInterimUtils2& utils = iServices.InterimUtilsL();
       
   427 
       
   428     // Create unique ID.
       
   429     HBufC8* guid = utils.GlobalUidL();
       
   430     CleanupStack::PushL(guid);
       
   431     iEntry = CCalEntry::NewL(
       
   432     		CCalEntry::EAppt,
       
   433     		guid, CCalEntry::EMethodRequest,
       
   434     		KDefaultSeqNo );
       
   435     CleanupStack::Pop( guid );
       
   436 
       
   437     MCalenContext& context = iServices.Context();
       
   438     aCommand.GetContextL( context );
       
   439 
       
   440     TCalTime startTime, stopTime;
       
   441 
       
   442 #ifdef RD_USE_PS2_APIS
       
   443     // Business Calendar day view with visual selection
       
   444     TTime start( 0 );
       
   445     TTime end( 0 );
       
   446 
       
   447     context.GetStartAndEndTimeForNewInstance( start, end );
       
   448 
       
   449     const TTime zeroTime(0);
       
   450     if ( start != zeroTime && end != zeroTime &&
       
   451             ECalenNewMeetingTimeSpan == aCommand.Command() )
       
   452         {
       
   453         startTime.SetTimeLocalL( start );
       
   454         stopTime.SetTimeLocalL( end );
       
   455 
       
   456         context.SetStartAndEndTimeForNewInstance( zeroTime, zeroTime ); //clean the start/end time in Context
       
   457 
       
   458         }
       
   459     else
       
   460 #endif // RD_USE_PS2_APIS
       
   461         {
       
   462         startTime = context.FocusDateAndTimeL();
       
   463 
       
   464         TTimeIntervalMinutes interval = KDefaultDurationInMinutes;
       
   465 
       
   466         stopTime.SetTimeLocalL(
       
   467                startTime.TimeLocalL() +
       
   468                interval );
       
   469         }
       
   470 
       
   471     iEntry->SetStartAndEndTimeL( startTime, stopTime );
       
   472 
       
   473     iEntry->SetPriorityL( EFSCalenMRPriorityNormal );
       
   474     }
       
   475 
       
   476 // ---------------------------------------------------------------------------
       
   477 // CMRBCPluginCreateNewMRCmd::AddOrganizerL
       
   478 // ---------------------------------------------------------------------------
       
   479 //
       
   480 void CMRBCPluginCreateNewMRCmd::AddOrganizerL()
       
   481     {
       
   482     FUNC_LOG;
       
   483     CMRMailboxUtils* mbUtils = CMRMailboxUtils::NewL( NULL );
       
   484     CleanupStack::PushL( mbUtils );
       
   485 
       
   486     CMRMailboxUtils::TMailboxInfo defaultMailBox;
       
   487     TInt err = mbUtils->GetDefaultMRMailBoxL( defaultMailBox );
       
   488 
       
   489     if ( KErrNone != err )
       
   490         {
       
   491         RArray<CMRMailboxUtils::TMailboxInfo> mailBoxes;
       
   492         CleanupClosePushL( mailBoxes );
       
   493 
       
   494         SupportedMailboxesL( *mbUtils, mailBoxes );
       
   495 
       
   496         TInt selectedMailbox( PromptForDefaultMailboxL(mailBoxes) );
       
   497 
       
   498         if ( KErrCancel != selectedMailbox )
       
   499             {
       
   500             mbUtils->SetDefaultMRMailBoxL(
       
   501                     mailBoxes[selectedMailbox].iEntryId ); // codescanner::accessArrayElementWithoutCheck2
       
   502             mbUtils->GetDefaultMRMailBoxL(defaultMailBox);
       
   503             }
       
   504         CleanupStack::PopAndDestroy( &mailBoxes );
       
   505 
       
   506         // This will leave if user cancelled the mailbox selection
       
   507         User::LeaveIfError( selectedMailbox );
       
   508         }
       
   509 
       
   510     //Set the organizer from the selected mailbox
       
   511     CCalUser* organizer = CCalUser::NewL( defaultMailBox.iEmailAddress );
       
   512     CleanupStack::PushL(organizer );
       
   513     iEntry->SetOrganizerL(organizer );
       
   514     CleanupStack::Pop( organizer ); // Ownership trasferred
       
   515 
       
   516     CleanupStack::PopAndDestroy( mbUtils );
       
   517     }
       
   518 
       
   519 // ---------------------------------------------------------------------------
       
   520 // CMRBCPluginCreateNewMRCmd::IssueCommandL
       
   521 // Issue command for calendar to handle
       
   522 // ---------------------------------------------------------------------------
       
   523 //
       
   524 void CMRBCPluginCreateNewMRCmd::IssueCommandL(
       
   525         MAgnEntryUi::TAgnEntryUiOutParams aOutParams )
       
   526     {
       
   527     FUNC_LOG;
       
   528     TInt command( KErrNotFound );
       
   529 
       
   530     TMROutputParams* params = ( TMROutputParams* )( aOutParams.iSpare );
       
   531 
       
   532     switch ( params->iCommand )
       
   533         {
       
   534         case EMRDialogOptionsMenuExit:
       
   535             {
       
   536             // Calendar app is not supposed to issue EAknCmdExit command.
       
   537             // It is always soft exit and faster app FW takes the call, and
       
   538             // decides the correct action.
       
   539             //
       
   540             // This is why we use command EAknSoftkeyExit, to exit
       
   541             // application from editor options menu.
       
   542 
       
   543             command = EAknSoftkeyExit;
       
   544             }
       
   545             break;
       
   546 
       
   547         default:
       
   548             break;
       
   549         }
       
   550 
       
   551     // Issue command for RECAL
       
   552     if( command != KErrNotFound )
       
   553         {
       
   554         iServices.IssueCommandL( command );
       
   555         }
       
   556     }
       
   557 
       
   558 // EOF