btobexprofiles/obexserviceman/utils/src/obexutilslaunchwaiter.cpp
changeset 42 b72428996822
parent 32 19bd632b5100
child 43 7d241e669870
equal deleted inserted replaced
32:19bd632b5100 42:b72428996822
     1 /*
       
     2 * Copyright (c) 2002 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 
       
    19 // INCLUDE FILES
       
    20 #include <eikenv.h>
       
    21 #include <DocumentHandler.h>
       
    22 #include <mmsvattachmentmanager.h>
       
    23 #include <msvids.h>
       
    24 #include <bautils.h>
       
    25 #include <AknCommonDialogsDynMem.h>     // for memory and file selection dialogs
       
    26 #include <CommonDialogs.rsg>
       
    27 #include <pathinfo.h>                   // for getting drive root path
       
    28 #include <Obexutils.rsg>
       
    29 #include <AknGlobalNote.h>
       
    30 #include <StringLoader.h>
       
    31 #include <AiwGenericParam.h>
       
    32 #include "obexutilslaunchwaiter.h"
       
    33 #include "obexutilsdebug.h"
       
    34 #include "obexutilsuilayer.h"            // For launching file manager
       
    35 #include "obexutilsmessagehandler.h"            // For updating an entry
       
    36 
       
    37 // ============================ MEMBER FUNCTIONS ===============================
       
    38 
       
    39 // -----------------------------------------------------------------------------
       
    40 // CObexUtilsLaunchWaiter::NewLC
       
    41 // -----------------------------------------------------------------------------
       
    42 CObexUtilsLaunchWaiter* CObexUtilsLaunchWaiter::NewLC( 
       
    43 	CMsvSession& aMsvSession,
       
    44 	CMsvEntry* aMessage,
       
    45     TRequestStatus& aObserverRequestStatus )
       
    46     {
       
    47     CObexUtilsLaunchWaiter* self = new( ELeave )CObexUtilsLaunchWaiter( 
       
    48         aMsvSession,
       
    49         aMessage, 
       
    50         aObserverRequestStatus );
       
    51     CleanupStack::PushL( self );
       
    52     self->ConstructL( aMessage );
       
    53     return self;
       
    54     }
       
    55 
       
    56 // -----------------------------------------------------------------------------
       
    57 // CObexUtilsLaunchWaiter::NewL
       
    58 // -----------------------------------------------------------------------------
       
    59 CObexUtilsLaunchWaiter* CObexUtilsLaunchWaiter::NewL(
       
    60 	CMsvSession& aMsvSession,
       
    61 	CMsvEntry* aMessage,
       
    62     TRequestStatus& aObserverRequestStatus )
       
    63     {
       
    64     CObexUtilsLaunchWaiter* self = CObexUtilsLaunchWaiter::NewLC(
       
    65         aMsvSession,
       
    66         aMessage, 
       
    67         aObserverRequestStatus );
       
    68     CleanupStack::Pop( self );
       
    69     return self;
       
    70     }
       
    71 
       
    72 // -----------------------------------------------------------------------------
       
    73 // CObexUtilsLaunchWaiter::ConstructL
       
    74 // -----------------------------------------------------------------------------
       
    75 void CObexUtilsLaunchWaiter::ConstructL( CMsvEntry* aMessage )
       
    76     {
       
    77     if (aMessage->Count() < 1)
       
    78         {
       
    79         User::Leave(KErrOverflow);
       
    80         }
       
    81     
       
    82     CMsvEntry* attachEntry = iMsvSession.GetEntryL(((*aMessage)[0]).Id());
       
    83     CleanupStack::PushL(attachEntry);  // 1st push
       
    84     CMsvStore* store = attachEntry->ReadStoreL();
       
    85     CleanupStack::PushL(store);  // 2nd push
       
    86     
       
    87     CMsvAttachment* attachInfo = store->AttachmentManagerL().GetAttachmentInfoL(0);
       
    88     CleanupStack::PushL(attachInfo); // 3rd push
       
    89           
       
    90     TDataType dataType = attachInfo->MimeType();
       
    91     TFileName filePath;
       
    92     filePath = attachInfo->FilePath();
       
    93   
       
    94     TInt error = KErrNone;
       
    95     TBool isCompleteSelf = EFalse;      
       
    96     CEikonEnv* eikEnv = CEikonEnv::Static();
       
    97 
       
    98     if ( attachInfo->Type() == CMsvAttachment::EMsvFile )
       
    99         {
       
   100         RFile attachFile;        
       
   101         TRAP( error, attachFile = store->AttachmentManagerL().GetAttachmentFileL(0));
       
   102         if ( error == KErrNone )
       
   103             {
       
   104             CleanupClosePushL(attachFile);  // 4th push          
       
   105             CAiwGenericParamList* paramList = CAiwGenericParamList::NewLC();  // 5th push
       
   106             TAiwGenericParam paramSave(EGenericParamAllowSave, ETrue);
       
   107             paramList->AppendL( paramSave );          
       
   108             
       
   109             if ( eikEnv )
       
   110                 {               
       
   111                 iDocumentHandler = CDocumentHandler::NewL( eikEnv->Process() );
       
   112                 iDocumentHandler->SetExitObserver( this );
       
   113                 TRAP( error, iDocumentHandler->OpenFileEmbeddedL(attachFile, dataType, *paramList));               
       
   114                 }// eikEnv        
       
   115             CleanupStack::PopAndDestroy(2); // paramList, attachFile
       
   116             }
       
   117         }// EMsvFile
       
   118     
       
   119     if ( attachInfo->Type() == CMsvAttachment::EMsvLinkedFile )
       
   120         {
       
   121         CAiwGenericParamList* paramList = CAiwGenericParamList::NewLC();  // 4th push
       
   122         TAiwGenericParam paramSave(EGenericParamFileSaved, ETrue);
       
   123         paramList->AppendL( paramSave );
       
   124         
       
   125         if ( eikEnv )
       
   126             {            
       
   127             iDocumentHandler = CDocumentHandler::NewL( eikEnv->Process() );
       
   128             iDocumentHandler->SetExitObserver( this );
       
   129             RFs rfs;
       
   130             User::LeaveIfError( rfs.Connect() );
       
   131             if ( BaflUtils::FileExists( rfs, filePath ) )                                 
       
   132                 {
       
   133                 RFile64 shareableFile;
       
   134                 TRAP( error, iDocumentHandler->OpenTempFileL(filePath,shareableFile));
       
   135                 if ( error == KErrNone)
       
   136                     {
       
   137                     TRAP( error, iDocumentHandler->OpenFileEmbeddedL( shareableFile, dataType, *paramList));
       
   138                     }
       
   139                 shareableFile.Close();
       
   140                 
       
   141                 if ( error == KErrNotSupported )  
       
   142                     {                    
       
   143                     delete iDocumentHandler;
       
   144                     iDocumentHandler = NULL;
       
   145                     
       
   146                     const TInt sortMethod = 2;  // 0 = 'By name', 1 = 'By type', 
       
   147                                                 // 2 = 'Most recent first' and 3 = 'Largest first'
       
   148                     TRAP (error, TObexUtilsUiLayer::LaunchFileManagerL( filePath, 
       
   149                                                                         sortMethod, 
       
   150                                                                         ETrue )); // ETrue -> launch file manager in embedded mode.
       
   151                     isCompleteSelf = ETrue;
       
   152                     }  // KErrNotSupported
       
   153                 }            
       
   154             else 
       
   155                 {
       
   156                 error = KErrNone;
       
   157                 TFileName fileName;
       
   158                 if (LocateFileL(fileName, filePath))
       
   159                     {
       
   160                     // Update the entry
       
   161                     TRAP(error, TObexUtilsMessageHandler::UpdateEntryAttachmentL(fileName,aMessage));
       
   162                     if ( error == KErrNone )
       
   163                         {
       
   164                         // Show a confirmation note
       
   165                         CAknGlobalNote* note = CAknGlobalNote::NewLC();
       
   166                         HBufC* stringholder  = StringLoader::LoadLC( R_BT_SAVED_LINK_UPDATED );
       
   167                         note->ShowNoteL(EAknGlobalConfirmationNote, *stringholder);
       
   168                         CleanupStack::PopAndDestroy(2); //note and stringholder
       
   169                         }            
       
   170                     }    
       
   171                 isCompleteSelf = ETrue;
       
   172                 }  
       
   173            
       
   174             rfs.Close();
       
   175             } // eikEnv
       
   176         
       
   177         CleanupStack::PopAndDestroy(); // paramList                                     
       
   178         } // EMsvLinkedFile
       
   179      
       
   180     
       
   181     // Set message to READ     
       
   182     TMsvEntry entry = aMessage->Entry();
       
   183     entry.SetUnread( EFalse );
       
   184     aMessage->ChangeL( entry );
       
   185     
       
   186     User::LeaveIfError ( error );
       
   187     CleanupStack::PopAndDestroy(3); //  attachInfo, store, attachEntry        
       
   188     
       
   189     iObserverRequestStatus = KRequestPending;  // CMsvOperation (observer)
       
   190     iStatus = KRequestPending;  // CMsvOperation
       
   191     SetActive();
       
   192 
       
   193     if ( isCompleteSelf )
       
   194         {
       
   195         HandleServerAppExit( error );
       
   196         }
       
   197     }
       
   198 
       
   199 // -----------------------------------------------------------------------------
       
   200 // Destructor
       
   201 // -----------------------------------------------------------------------------
       
   202 CObexUtilsLaunchWaiter::~CObexUtilsLaunchWaiter()
       
   203     {
       
   204     Cancel();
       
   205     if (iDocumentHandler)
       
   206         {
       
   207         delete iDocumentHandler;
       
   208         iDocumentHandler = NULL;
       
   209         }
       
   210     }
       
   211 
       
   212 // -----------------------------------------------------------------------------
       
   213 // CObexUtilsLaunchWaiter::RunL
       
   214 // -----------------------------------------------------------------------------
       
   215 void CObexUtilsLaunchWaiter::RunL()
       
   216     {
       
   217     FLOG(_L("[OBEXUTILS]\t CObexUtilsLaunchWaiter::RunL()"));
       
   218     
       
   219     TRequestStatus* status = &iObserverRequestStatus;
       
   220     User::RequestComplete( status, KErrNone );
       
   221 
       
   222     FLOG(_L("[OBEXUTILS]\t CObexUtilsLaunchWaiter::RunL() completed"));
       
   223     }
       
   224     
       
   225 // -----------------------------------------------------------------------------
       
   226 // CObexUtilsLaunchWaiter::DoCancel
       
   227 // -----------------------------------------------------------------------------
       
   228 void CObexUtilsLaunchWaiter::DoCancel()
       
   229     {
       
   230     FLOG(_L("[OBEXUTILS]\t CObexUtilsLaunchWaiter::DoCancel()"));
       
   231    
       
   232     if ( iStatus == KRequestPending )
       
   233         {
       
   234         TRequestStatus* pstat = &iStatus;
       
   235         User::RequestComplete( pstat, KErrCancel );
       
   236         }
       
   237       
       
   238     if( iObserverRequestStatus == KRequestPending )
       
   239         {
       
   240         TRequestStatus* observerStatus = &iObserverRequestStatus;
       
   241         User::RequestComplete( observerStatus, KErrCancel );
       
   242         }
       
   243 
       
   244     
       
   245     FLOG(_L("[OBEXUTILS]\t CObexUtilsLaunchWaiter::DoCancel() completed"));
       
   246     }
       
   247 
       
   248 // -----------------------------------------------------------------------------
       
   249 // CObexUtilsLaunchWaiter::CObexUtilsLaunchWaiter
       
   250 // -----------------------------------------------------------------------------
       
   251 CObexUtilsLaunchWaiter::CObexUtilsLaunchWaiter( 
       
   252 	CMsvSession& aMsvSession,
       
   253 	CMsvEntry* /*aMessage*/,
       
   254     TRequestStatus& aObserverRequestStatus )
       
   255     :
       
   256     CMsvOperation(aMsvSession, EPriorityStandard, aObserverRequestStatus),
       
   257     iDocumentHandler(NULL)
       
   258     {
       
   259     FLOG(_L("[OBEXUTILS]\t CObexUtilsLaunchWaiter::CObexUtilsLaunchWaiter()"));
       
   260 
       
   261     CActiveScheduler::Add( this );
       
   262 
       
   263     FLOG(_L("[OBEXUTILS]\t CObexUtilsLaunchWaiter::CObexUtilsLaunchWaiter() completed"));
       
   264     }
       
   265 
       
   266 // -----------------------------------------------------------------------------
       
   267 // CObexUtilsLaunchWaiter::HandleServerAppExit
       
   268 // -----------------------------------------------------------------------------
       
   269 void CObexUtilsLaunchWaiter::HandleServerAppExit(TInt aReason)
       
   270     {
       
   271     FLOG(_L("[OBEXUTILS]\t CObexUtilsLaunchWaiter::HandleServerAppExit()"));
       
   272 
       
   273     if( iStatus == KRequestPending )
       
   274         {
       
   275         // Complete self
       
   276         //
       
   277         TRequestStatus* status = &iStatus;
       
   278         User::RequestComplete( status, aReason );
       
   279         }
       
   280 
       
   281 	MAknServerAppExitObserver::HandleServerAppExit( aReason );	
       
   282     FLOG(_L("[OBEXUTILS]\t CObexUtilsLaunchWaiter::HandleServerAppExit() completed"));
       
   283     }
       
   284 
       
   285 // -----------------------------------------------------------------------------
       
   286 // CObexUtilsLaunchWaiter::ProgressL
       
   287 // -----------------------------------------------------------------------------
       
   288 const TDesC8& CObexUtilsLaunchWaiter::ProgressL()
       
   289     {
       
   290     return KNullDesC8;
       
   291     }
       
   292 
       
   293 // -----------------------------------------------------------------------------
       
   294 // CObexUtilsLaunchWaiter::LocateFile
       
   295 // -----------------------------------------------------------------------------
       
   296 TBool CObexUtilsLaunchWaiter::LocateFileL(TFileName& aFileName, const TFileName& anOldFileName)
       
   297     {
       
   298     FLOG(_L("[OBEXUTILS]\t CObexUtilsLaunchWaiter::LocateFile()"));
       
   299     
       
   300     TBuf<200> buf;
       
   301     TRequestStatus status = KRequestPending;
       
   302     
       
   303     TParse fileParseOld;
       
   304     fileParseOld.Set(anOldFileName, NULL, NULL);
       
   305     TFileName oldName = fileParseOld.NameAndExt();
       
   306             
       
   307     // check old link if the file saved in mmc. If so, check if mmc available. 
       
   308     // if unavailable, show "is File not found as memory card is not present"
       
   309     //
       
   310     TInt err = CheckIfSaveInMMC( anOldFileName );
       
   311     TBool showMMCOut = EFalse;
       
   312     if( err == EDriveF)
       
   313         {
       
   314         if( CheckDriveL(EDriveF) )
       
   315             showMMCOut = ETrue;
       
   316         }
       
   317     else if( err == EDriveE )
       
   318         {
       
   319         if( CheckDriveL(EDriveE) )
       
   320             showMMCOut = ETrue;
       
   321         }
       
   322 
       
   323     TBool answer = EFalse;
       
   324     if( showMMCOut )
       
   325         {
       
   326         answer = TObexUtilsUiLayer::ShowGlobalConfirmationQueryPlainL(R_BT_SAVED_NO_MEMORY_CARD);
       
   327         }
       
   328     else
       
   329         {
       
   330         answer = TObexUtilsUiLayer::ShowGlobalConfirmationQueryPlainL(R_BT_SAVED_SEARCH);
       
   331         }
       
   332    
       
   333     TBool updateLink = EFalse;
       
   334     if ( answer )
       
   335         {
       
   336         updateLink = LaunchFileSelectionDialogL(aFileName, oldName);               
       
   337         }  
       
   338     
       
   339     FLOG(_L("[OBEXUTILS]\t CObexUtilsLaunchWaiter::LocateFile() completed"));
       
   340     return updateLink;
       
   341     }
       
   342     
       
   343 // -----------------------------------------------------------------------------
       
   344 // CObexUtilsLaunchWaiter::LaunchFileSelectionDialogL
       
   345 // -----------------------------------------------------------------------------
       
   346 TBool CObexUtilsLaunchWaiter::LaunchFileSelectionDialogL(
       
   347     TFileName& aFileName, 
       
   348     const TFileName& anOldName)
       
   349     {
       
   350     TBuf<200> buf;
       
   351     TParse fileParseNew;
       
   352     TFileName nameNew;
       
   353     TBool updateLink = EFalse;
       
   354    
       
   355     while ( !updateLink ) 
       
   356         {
       
   357         TBool isSelected =  AknCommonDialogsDynMem::RunSelectDlgLD( AknCommonDialogsDynMem::EMemoryTypeMMCExternal|
       
   358                                                                     AknCommonDialogsDynMem::EMemoryTypeMMC|
       
   359                                                                     AknCommonDialogsDynMem::EMemoryTypeInternalMassStorage|
       
   360                                                                     AknCommonDialogsDynMem::EMemoryTypePhone,
       
   361                                                                     aFileName,
       
   362                                                                     R_CFD_DEFAULT_SELECT_MEMORY_SELECTION,
       
   363                                                                     R_CFD_DEFAULT_SELECT_FILE_SELECTION );
       
   364         
       
   365         if ( isSelected )
       
   366             {
       
   367             fileParseNew.Set(aFileName, NULL, NULL);
       
   368             nameNew = fileParseNew.NameAndExt();
       
   369             
       
   370             if ( nameNew.Compare(anOldName)) // names do not match
       
   371                 {
       
   372                 updateLink =  TObexUtilsUiLayer::ShowGlobalConfirmationQueryPlainL(R_BT_SAVED_SEARCH_UPDATE);                        
       
   373                 }
       
   374             else
       
   375                 {
       
   376                 updateLink = ETrue;
       
   377                 }       
       
   378             }
       
   379         else
       
   380             {
       
   381             break;  // Exit from while loop.
       
   382             }
       
   383         }
       
   384   
       
   385     return updateLink;
       
   386     }
       
   387 
       
   388 // -----------------------------------------------------------------------------
       
   389 // CObexUtilsLaunchWaiter::CheckDriveL
       
   390 // -----------------------------------------------------------------------------
       
   391 TInt CObexUtilsLaunchWaiter::CheckDriveL(TDriveNumber aDriveNumber)
       
   392     {
       
   393     RFs rfs;
       
   394     User::LeaveIfError(rfs.Connect());
       
   395     CleanupClosePushL( rfs ) ;
       
   396     TVolumeInfo volumeInfo;
       
   397     TInt err = rfs.Volume(volumeInfo, aDriveNumber);
       
   398     CleanupStack::PopAndDestroy();    // rfs
       
   399     
       
   400     return err;
       
   401     }
       
   402 // -----------------------------------------------------------------------------
       
   403 // CObexUtilsLaunchWaiter::CheckIfSaveInMMC
       
   404 // -----------------------------------------------------------------------------
       
   405 TInt CObexUtilsLaunchWaiter::CheckIfSaveInMMC(const TFileName& aFileName)
       
   406     {
       
   407     if(aFileName.Find(_L("F:")) != KErrNotFound)
       
   408         return EDriveF;
       
   409     if(aFileName.Find(_L("E:")) != KErrNotFound)
       
   410         return EDriveE;
       
   411 
       
   412     return KErrNotFound;
       
   413     }
       
   414 //  End of File