uiservicetab/vimpstui/src/cvimpstuiavatarselectionhandler.cpp
changeset 0 5e5d6b214f4f
child 14 9fdee5e1da30
equal deleted inserted replaced
-1:000000000000 0:5e5d6b214f4f
       
     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:  avatar selection handler
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 #include    "cvimpstuiavatarselectionhandler.h"
       
    23 #include    "vimpstui.hrh"
       
    24 #include    "vimpstutilsdialog.h"
       
    25 #include    "tvimpstconsts.h"
       
    26 
       
    27 #include    <StringLoader.h>
       
    28 #include   <vimpstuires.rsg>
       
    29 #include    <NewFileServiceClient.h>
       
    30 #include    <AiwGenericParam.h>
       
    31 #include    <MGFetch.h>
       
    32 #include    <DocumentHandler.h>
       
    33 #include    <eikenv.h>
       
    34 #include    <imageconversion.h>
       
    35 #include	<aknnavi.h>
       
    36 #include	<aknappui.h>
       
    37 #include    <avkon.rsg>
       
    38 #include    <caf/content.h>
       
    39 #include    <caf/manager.h> // content access
       
    40 
       
    41 
       
    42 // ============================ MEMBER FUNCTIONS ===============================
       
    43 
       
    44 // -----------------------------------------------------------------------------
       
    45 // CAvatarSelectionHandler::CAvatarSelectionHandler
       
    46 // C++ default constructor can NOT contain any code, that
       
    47 // might leave.
       
    48 // -----------------------------------------------------------------------------
       
    49 //
       
    50 CAvatarSelectionHandler::CAvatarSelectionHandler()
       
    51 : CActive( CActive::EPriorityLow )
       
    52             {
       
    53             CActiveScheduler::Add( this );
       
    54             }
       
    55 
       
    56 // -----------------------------------------------------------------------------
       
    57 // CAvatarSelectionHandler::ConstructL
       
    58 // Symbian 2nd phase constructor can leave.
       
    59 // -----------------------------------------------------------------------------
       
    60 //
       
    61 void CAvatarSelectionHandler::ConstructL()
       
    62     {
       
    63     User::LeaveIfError(iApaSession.Connect() );
       
    64 
       
    65     // create dochandler
       
    66     iDocHandler = CDocumentHandler::NewL();
       
    67     iDocHandler->SetExitObserver( this);
       
    68     }
       
    69 
       
    70 // -----------------------------------------------------------------------------
       
    71 // CAvatarSelectionHandler::NewL
       
    72 // Two-phased constructor.
       
    73 // -----------------------------------------------------------------------------
       
    74 //
       
    75 CAvatarSelectionHandler* CAvatarSelectionHandler::NewLC()
       
    76 
       
    77     {
       
    78     CAvatarSelectionHandler* self = new( ELeave ) CAvatarSelectionHandler();
       
    79 
       
    80     CleanupStack::PushL(self);
       
    81     self->ConstructL();
       
    82 
       
    83     return self;
       
    84     }
       
    85 
       
    86 // Destructor
       
    87 CAvatarSelectionHandler::~CAvatarSelectionHandler()
       
    88     {
       
    89     Cancel();
       
    90 
       
    91     delete iDecoder;
       
    92     iDecoder = NULL;
       
    93 
       
    94     delete iDocHandler;
       
    95     iDocHandler = NULL;
       
    96 
       
    97     iApaSession.Close();
       
    98     if (iWait.IsStarted() )
       
    99         {
       
   100         iWait.AsyncStop();
       
   101         }
       
   102     delete iMimeType;
       
   103     }
       
   104 
       
   105 //-----------------------------------------------------------------------------
       
   106 // CAvatarSelectionHandler::RunL
       
   107 // ( Other items commented in header )
       
   108 //-----------------------------------------------------------------------------
       
   109 void CAvatarSelectionHandler::RunL()
       
   110     {
       
   111     if (iWait.IsStarted() )
       
   112         {
       
   113         iWait.AsyncStop();
       
   114         }
       
   115     }
       
   116 
       
   117 //-----------------------------------------------------------------------------
       
   118 // CAvatarSelectionHandler::DoCancel
       
   119 // ( Other items commented in header )
       
   120 //-----------------------------------------------------------------------------
       
   121 void CAvatarSelectionHandler::DoCancel()
       
   122     {
       
   123     iDecoder->Cancel();
       
   124     }
       
   125 
       
   126 //-----------------------------------------------------------------------------
       
   127 // CAvatarSelectionHandler::RunError
       
   128 // ( Other items commented in header )
       
   129 //-----------------------------------------------------------------------------
       
   130 TInt CAvatarSelectionHandler::RunError(TInt aError)
       
   131     {
       
   132     if (IsActive() )
       
   133         {
       
   134         iStatus = aError;
       
   135         Cancel();
       
   136         }
       
   137 
       
   138     if (aError == KErrNoMemory)
       
   139         {
       
   140         CActiveScheduler::Current()->Error(KErrNoMemory);
       
   141         }
       
   142 
       
   143     return KErrNone;
       
   144     }
       
   145 
       
   146 // ---------------------------------------------------------
       
   147 // CAvatarSelectionHandler::HandleServerAppExit
       
   148 // (other items were commented in a header).
       
   149 // ---------------------------------------------------------
       
   150 //
       
   151 void CAvatarSelectionHandler::HandleServerAppExit(TInt aReason)
       
   152     {
       
   153     iServerAppExitReason = aReason;
       
   154     if (iWait.IsStarted() )
       
   155         {
       
   156         iWait.AsyncStop();
       
   157         }
       
   158     }
       
   159 
       
   160 // ---------------------------------------------------------
       
   161 // CAvatarSelectionHandler::VerifySelectedFilesL
       
   162 // (other items were commented in a header).
       
   163 // ---------------------------------------------------------
       
   164 //
       
   165 TBool CAvatarSelectionHandler::VerifySelectedFilesL( const MDesCArray* aSelectedFiles )
       
   166 
       
   167     {
       
   168     if( aSelectedFiles && aSelectedFiles->MdcaCount() )
       
   169         {
       
   170         TPtrC filename( aSelectedFiles->MdcaPoint(0) ); // since muliselection is not allowed
       
   171         //take the only file in the array 
       
   172         TInt value( KErrNone ); 
       
   173         ContentAccess::CContent* content = ContentAccess::CContent::NewL( filename );
       
   174         // check for  protetected image 
       
   175         TInt err( content->GetAttribute( ContentAccess::EIsProtected, value ) );
       
   176         delete content;
       
   177 
       
   178         if( err == KErrNone && value ) // protected 
       
   179             {
       
   180             // show note
       
   181             ShowErrorL(EProtected);
       
   182             }
       
   183         else  // not protected 
       
   184             {
       
   185             delete iDecoder; // delete decoder if there is any 
       
   186             iDecoder = NULL;
       
   187 
       
   188             TInt params(CImageDecoder::EPreferFastDecode);
       
   189             TRAPD( err, iDecoder = CImageDecoder::FileNewL(
       
   190                     CEikonEnv::Static()->FsSession(),filename, 
       
   191                     static_cast<CImageDecoder::TOptions>(params) ));
       
   192             if( err == KErrNone )
       
   193                 {
       
   194                 // file seems ok, try to decode image
       
   195                 CFbsBitmap* bitmap = new (ELeave) CFbsBitmap();
       
   196                 CleanupStack::PushL( bitmap );
       
   197 
       
   198                 TFrameInfo frame =  iDecoder->FrameInfo();
       
   199                 TSize decodeSize( 60,60 );
       
   200 
       
   201                 TInt bitmaperr = bitmap->Create( decodeSize, frame.iFrameDisplayMode );      
       
   202 
       
   203                 if( bitmaperr )
       
   204                     { // any bit mapp error throw error and return EFalse
       
   205                     CActiveScheduler::Current()->Error( bitmaperr );
       
   206                     CleanupStack::PopAndDestroy( bitmap );
       
   207                     return EFalse;
       
   208                     }
       
   209                 // start converting
       
   210                 iDecoder->Convert( &iStatus, *bitmap );
       
   211                 if( !IsActive() )
       
   212                     {
       
   213                     SetActive();
       
   214                     }
       
   215 
       
   216                 // and wait until it's finished
       
   217                 if( !iWait.IsStarted() )
       
   218                     {
       
   219                     iWait.Start();  //  iWait is not an active object
       
   220                     }
       
   221 
       
   222                 TInt statuscode = iStatus.Int();
       
   223                 CleanupStack::PopAndDestroy( bitmap );
       
   224 
       
   225                 if( statuscode == KErrCorrupt ) // image corrupted 
       
   226                     {
       
   227                     // show note
       
   228                     ShowErrorL( ECorrupted );
       
   229                     return  EFalse; 
       
   230                     }
       
   231 
       
   232                 return ETrue; // selected file is ok ,return ETrue 
       
   233                 }
       
   234             else
       
   235                 {
       
   236                 ShowErrorL( ECorrupted );
       
   237                 }
       
   238 
       
   239             }
       
   240 
       
   241         }
       
   242     return EFalse;  // no files or protected file return EFalse
       
   243     }
       
   244 
       
   245 // ---------------------------------------------------------
       
   246 // CAvatarSelectionHandler::DecodeSize
       
   247 // (other items were commented in a header).
       
   248 // ---------------------------------------------------------
       
   249 //
       
   250 TSize CAvatarSelectionHandler::DecodeSize(const TSize& aSize)
       
   251     {
       
   252     // 1:1 is always valid ratio for decode scaling
       
   253     TInt lastValidRatio( 1);
       
   254     for (TInt ratio(KDecodeScaleRatioMin); ratio <= KDecodeScaleRatioMax; ratio<<= 1)
       
   255         {
       
   256         if (aSize.iWidth % ratio + aSize.iHeight % ratio == 0)
       
   257             {
       
   258             // this ratio is valid
       
   259             lastValidRatio = ratio;
       
   260             }
       
   261         }
       
   262 
       
   263     // return the size scaled with correct ratio
       
   264     return TSize(aSize.iWidth / lastValidRatio, aSize.iHeight
       
   265             / lastValidRatio);
       
   266     }
       
   267 
       
   268 // ---------------------------------------------------------
       
   269 // CAvatarSelectionHandler::HandleAvatarSelectionL()
       
   270 // (other items were commented in a header).
       
   271 // ---------------------------------------------------------
       
   272 //
       
   273 HBufC*  CAvatarSelectionHandler::HandleAvatarSelectionL(TInt aCommand )
       
   274     {
       
   275     HBufC* selectedFile = NULL;
       
   276     switch (aCommand)
       
   277         {
       
   278         case ECmdAvatarGallery:
       
   279             {
       
   280             CDesCArray* files = new( ELeave )CDesCArrayFlat(1);
       
   281             CleanupStack::PushL(files);
       
   282             CDesCArray* mimeTypes = new ( ELeave ) CDesCArrayFlat(1 );                                           
       
   283             CleanupStack::PushL( mimeTypes );
       
   284 
       
   285             mimeTypes->AppendL( KJPegType );
       
   286             mimeTypes->AppendL( KGifType );
       
   287             mimeTypes->AppendL( KPngType );
       
   288 
       
   289             HBufC* softkey = CCoeEnv::Static()->AllocReadResourceLC(
       
   290                     R_QTN_SERVTAB_GALLERY_LSK_SELECT);
       
   291 
       
   292             HBufC* title = CCoeEnv::Static()->AllocReadResourceLC(
       
   293                     R_QTN_SERVTAB_GALLERY_TITLE_AVATAR);
       
   294             // This is to clear the NaviPane(Pane where Service Tabs are displayed) 
       
   295             // before launching the avatar selection dialogue from photos fetcher.
       
   296             // since photos fetcher does't take care of clearing the Navi pane.
       
   297             CEikStatusPane* statusPane = CEikonEnv::Static()->AppUiFactory()->StatusPane(); // Get the StatusPane    
       
   298             CAknNavigationControlContainer* naviPane = static_cast<CAknNavigationControlContainer*>(statusPane->ControlL( TUid::Uid( EEikStatusPaneUidNavi ) ) );            
       
   299             CAknNavigationDecorator* naviDecoration = naviPane->Top();  // Get the current NaviPane
       
   300             naviPane->PushDefaultL( EFalse ); // Clear the NaviPane
       
   301             // Launching Avatar Selection 
       
   302             if (MGFetch::RunL( *files, EImageFile, EFalse, *softkey,
       
   303                     *title, mimeTypes , NULL) )
       
   304                 {
       
   305                 if(VerifySelectedFilesL(files))
       
   306                     {
       
   307                     selectedFile =  (files->MdcaPoint(0)).AllocL(); // alloclate for first filename
       
   308                     StoreMimeTypeL(*selectedFile); // get the mime type and store
       
   309                     }
       
   310                 }
       
   311             CleanupStack::PopAndDestroy( 4); //  title, softkey ,mimeTypes ,files
       
   312             naviPane->PushL(*naviDecoration); // Restoring the previous Navipane 
       
   313             break;
       
   314             }
       
   315         case ECmdAvatarNewPhoto:
       
   316             {
       
   317             CDesCArray* fileNames = new( ELeave )CDesCArrayFlat(1);
       
   318             CleanupStack::PushL(fileNames);
       
   319             CNewFileServiceClient* newFile = NewFileServiceFactory::NewClientL();
       
   320             CleanupStack::PushL(newFile);
       
   321 
       
   322             CAiwGenericParamList* params = CAiwGenericParamList::NewLC();
       
   323 
       
   324 
       
   325             TBool okToSend = EFalse;
       
   326             TRAPD( err, okToSend =
       
   327             newFile->NewFileL( *fileNames, params, ENewFileServiceImage, EFalse ) );
       
   328 
       
   329             if (err)
       
   330                 {
       
   331                 // restore state
       
   332                 User::LeaveIfError(err);
       
   333                 }
       
   334 
       
   335             if (okToSend)
       
   336                 {
       
   337                 selectedFile = ( fileNames->MdcaPoint(0)).AllocL(); // alloclate for first filename
       
   338                 StoreMimeTypeL(*selectedFile); // get the mime type and store
       
   339                 }
       
   340 
       
   341             CleanupStack::PopAndDestroy( 3, fileNames); // params, newFile, fileNames
       
   342 
       
   343             break;
       
   344             }
       
   345         }
       
   346     return selectedFile;
       
   347     }
       
   348 // ---------------------------------------------------------
       
   349 // CAvatarSelectionHandler::ShowErrorL
       
   350 // (other items were commented in a header).
       
   351 // ---------------------------------------------------------
       
   352 //
       
   353 void CAvatarSelectionHandler::ShowErrorL(TErrorType aErrorType)
       
   354     {
       
   355 
       
   356     switch( aErrorType )
       
   357         {  
       
   358         case EProtected:
       
   359             {
       
   360             HBufC* prompt = StringLoader::LoadLC( R_QTN_SERVTAB_GALLERY_ERROR_DRMFILE );
       
   361             VIMPSTUtilsDialog::DisplayErrorNoteL( *prompt ) ;
       
   362             CleanupStack::PopAndDestroy(prompt);
       
   363             break;
       
   364             }
       
   365         case ECorrupted:
       
   366             {
       
   367             HBufC* prompt = StringLoader::LoadLC( R_QTN_SERVTAB_GALLERY_ERROR_BADFILE );
       
   368             VIMPSTUtilsDialog::DisplayErrorNoteL( *prompt ) ;
       
   369             CleanupStack::PopAndDestroy(prompt);
       
   370             break;
       
   371             }    
       
   372         }
       
   373 
       
   374     }
       
   375 
       
   376 // ---------------------------------------------------------
       
   377 // CAvatarSelectionHandler::MimeTypeL
       
   378 // (other items were commented in a header).
       
   379 // ---------------------------------------------------------
       
   380 //
       
   381 void CAvatarSelectionHandler::StoreMimeTypeL(const TDesC& aFilename)
       
   382     {
       
   383     TUid dummyUid( KNullUid );
       
   384     TDataType dataType;
       
   385     User::LeaveIfError( iApaSession.AppForDocument(
       
   386             aFilename,
       
   387             dummyUid, dataType ) );
       
   388 
       
   389     iMimeType = dataType.Des8().AllocL();
       
   390     }
       
   391 
       
   392 // ---------------------------------------------------------
       
   393 // CAvatarSelectionHandler::MimeTypeL
       
   394 // (other items were commented in a header).
       
   395 // ---------------------------------------------------------
       
   396 //
       
   397 const TDesC8& CAvatarSelectionHandler::MimeTypeL()
       
   398     {
       
   399     return *iMimeType;
       
   400     }
       
   401 //  End of File