calendarui/controller/src/calenattachmentui.cpp
changeset 0 f979ecb2b13e
child 5 42814f902fe6
equal deleted inserted replaced
-1:000000000000 0:f979ecb2b13e
       
     1 /*
       
     2 * Copyright (c) 2009 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 <aknlistquerydialog.h>
       
    19 #include <AiwGenericParam.hrh>
       
    20 #include <AiwGenericParam.h>
       
    21 #include <AiwVariant.h>
       
    22 #include <eikappui.h>
       
    23 #include <aknnotewrappers.h>
       
    24 #include <AknWaitDialog.h>
       
    25 #include <coemain.h>
       
    26 #include <calattachment.h>
       
    27 #include <CalenInterimUtils2.h>
       
    28 #include <calencommonui.rsg>
       
    29 #include <StringLoader.h>           // StringLoader
       
    30 #include <DRMHelper.h>
       
    31 #include <caf/data.h>
       
    32 #include <caf/caf.h>
       
    33 #include <calencommandhandler.h>
       
    34 #include <calencontext.h>
       
    35 #include <calentoolbar.h>
       
    36 
       
    37 #include "calenattachmentui.h"
       
    38 #include "calendarui_debug.h"
       
    39 #include "calenattachmentutils.h"
       
    40 #include "calencontroller.h"
       
    41 #include "calenviewattachmentsdialog.h"
       
    42 #include "calendar.hrh"
       
    43 #include "calenattachmentmodel.h"
       
    44 #include "calenattachmentinfo.h"
       
    45 #include "CleanupResetAndDestroy.h"
       
    46 #include "calenviewmanager.h"
       
    47 
       
    48 //_LIT16( KTextDataType, ".txt" );
       
    49 const TInt KCalenAttachmentOverhead = 1024;
       
    50 
       
    51 // ----------------------------------------------------------------------------
       
    52 // CCalenAttachmentUi::NewL
       
    53 // 1st phase of construction
       
    54 // ----------------------------------------------------------------------------
       
    55 //
       
    56 CCalenAttachmentUi* CCalenAttachmentUi::NewL(CCalenController& aController)
       
    57     {
       
    58     TRACE_ENTRY_POINT;
       
    59     
       
    60     CCalenAttachmentUi* self = new (ELeave) CCalenAttachmentUi(aController);
       
    61     CleanupStack::PushL(self);
       
    62     self->ConstructL();
       
    63     CleanupStack::Pop(self);
       
    64     
       
    65     TRACE_EXIT_POINT;
       
    66     return self;
       
    67     }
       
    68         
       
    69 // ----------------------------------------------------------------------------
       
    70 // CCalenAttachmentUi::~CCalenAttachmentUi
       
    71 // Destructor
       
    72 // ----------------------------------------------------------------------------
       
    73 //
       
    74 CCalenAttachmentUi::~CCalenAttachmentUi()
       
    75     {
       
    76     TRACE_ENTRY_POINT;
       
    77     delete iTitle;
       
    78     iController.CancelNotifications( this );
       
    79     TRACE_EXIT_POINT;
       
    80     }
       
    81 
       
    82 // ----------------------------------------------------------------------------
       
    83 // CCalenAttachmentUi::CCalenAttachmentUi
       
    84 // Default constructor
       
    85 // ----------------------------------------------------------------------------
       
    86 //        
       
    87 CCalenAttachmentUi::CCalenAttachmentUi(CCalenController& aController)
       
    88 : iController(aController), iAttachmentLoadingOn(EFalse), 
       
    89 iAddAttachmentFromViewer(EFalse),iViewerActive(EFalse)
       
    90     {
       
    91     TRACE_ENTRY_POINT;
       
    92     TRACE_EXIT_POINT;    
       
    93     }
       
    94 
       
    95 // ----------------------------------------------------------------------------
       
    96 // CCalenAttachmentUi::ConstructL
       
    97 // 2nd phase constructor
       
    98 // ----------------------------------------------------------------------------
       
    99 //        
       
   100 void CCalenAttachmentUi::ConstructL()
       
   101     {
       
   102     TRACE_ENTRY_POINT;
       
   103     
       
   104     RArray<TCalenNotification> notificationArray;
       
   105     notificationArray.Append(ECalenNotifyEntrySaved);
       
   106     notificationArray.Append(ECalenNotifyEntryDeleted);
       
   107     notificationArray.Append(ECalenNotifyEntryClosed);
       
   108     notificationArray.Append(ECalenNotifyInstanceDeleted);
       
   109     notificationArray.Append(ECalenNotifyMarkedEntryDeleted);
       
   110     notificationArray.Append(ECalenNotifyMultipleEntriesDeleted);
       
   111     notificationArray.Append(ECalenNotifyDialogClosed);
       
   112     notificationArray.Append(ECalenNotifyAttachmentViewerClosed);
       
   113     iController.RegisterForNotificationsL( this, notificationArray );
       
   114     notificationArray.Reset();
       
   115     
       
   116     iAttachmentModel = &iController.AttachmentData();
       
   117     iAttachmentModel->SetObserver(this);
       
   118     iAttachmentAlreadyExists = EFalse;
       
   119     iAttachmentInfoIntialized = EFalse;
       
   120     
       
   121     TRACE_EXIT_POINT;
       
   122     }
       
   123 
       
   124 // ----------------------------------------------------------------------------
       
   125 // CCalenAttachmentUi::HandleCommandL
       
   126 // Handles the attachement commands
       
   127 // ----------------------------------------------------------------------------
       
   128 //        
       
   129 TBool CCalenAttachmentUi::HandleCommandL(const TCalenCommand& aCommand)
       
   130     {
       
   131     TRACE_ENTRY_POINT;
       
   132     
       
   133     TInt continueCommand(EFalse);
       
   134     switch(aCommand.Command())
       
   135         {
       
   136         case ECalenAddAttachment:
       
   137             {
       
   138             HandleAddAttachmentL();
       
   139             }
       
   140             break;
       
   141         case ECalenAddAttachmentFromViewer:
       
   142             {
       
   143             
       
   144             iAddAttachmentFromViewer = ETrue;
       
   145             HandleAddAttachmentL();
       
   146             }
       
   147             break;
       
   148         case ECalenViewAttachmentList:
       
   149             {
       
   150             DoAttachmentL();
       
   151             }
       
   152             break;
       
   153         case ECalenRemoveAttachment:
       
   154             {
       
   155             CheckForExistingAttachmentsL();
       
   156             
       
   157             RPointerArray<CCalenAttachmentInfo> attachmentInfoList;
       
   158             iAttachmentModel->GetAttachmentListL(attachmentInfoList); 
       
   159             
       
   160             if(!iAddAttachmentFromViewer && (attachmentInfoList.Count()))
       
   161                 {                                
       
   162                 iAttachmentModel->Reset();
       
   163                 iController.BroadcastNotification(ECalenNotifyAttachmentRemoved);
       
   164                 }                    
       
   165             else
       
   166                 {
       
   167                 iAttachmentModel->DeleteAttachment(0);
       
   168                 }
       
   169             RemoveTemporaryFiles();
       
   170             attachmentInfoList.Close(); 
       
   171             }
       
   172             break;
       
   173         default:
       
   174             break;
       
   175         }
       
   176     
       
   177     TRACE_EXIT_POINT;
       
   178     return continueCommand;
       
   179     }
       
   180 
       
   181 // ----------------------------------------------------------------------------
       
   182 // CCalenAttachmentUi::CalenCommandHandlerExtensionL
       
   183 // Dummy implementation.
       
   184 // (other items were commented in a header).
       
   185 // ----------------------------------------------------------------------------
       
   186 //
       
   187 TAny* CCalenAttachmentUi::CalenCommandHandlerExtensionL( TUid /*aExtensionUid*/ )
       
   188     {
       
   189     TRACE_ENTRY_POINT;
       
   190     TRACE_EXIT_POINT;
       
   191     return NULL;
       
   192     }
       
   193 
       
   194 // ----------------------------------------------------------------------------
       
   195 // CCalenAttachmentUi::HandleNotification
       
   196 // Handle notifications
       
   197 // ----------------------------------------------------------------------------
       
   198 //        
       
   199 void CCalenAttachmentUi::HandleNotification(const TCalenNotification aNotification)
       
   200     {
       
   201     TRACE_ENTRY_POINT;
       
   202     
       
   203     TRAP_IGNORE(DoHandleNotificationL(aNotification));
       
   204 
       
   205     TRACE_EXIT_POINT;     
       
   206     }
       
   207 
       
   208 // ----------------------------------------------------------------------------
       
   209 // CCalenAttachmentUi::DoHandleNotificationL
       
   210 // Handle notifications
       
   211 // ----------------------------------------------------------------------------
       
   212 // 
       
   213 void CCalenAttachmentUi::DoHandleNotificationL(const TCalenNotification aNotification )
       
   214     {
       
   215     TRACE_ENTRY_POINT;
       
   216     switch(aNotification)
       
   217         {
       
   218         case ECalenNotifyEntrySaved:
       
   219         case ECalenNotifyEntryDeleted:
       
   220         case ECalenNotifyInstanceDeleted:            
       
   221             {
       
   222             if(!(iAddAttachmentFromViewer))
       
   223                 {
       
   224                 iAttachmentModel->Reset();
       
   225                 } 
       
   226             // clear calendar editor's folder
       
   227             RemoveTemporaryFiles();
       
   228             iAttachmentInfoIntialized = EFalse;
       
   229             }
       
   230             break;            
       
   231         case ECalenNotifyEntryClosed:
       
   232         case ECalenNotifyMarkedEntryDeleted:
       
   233         case ECalenNotifyMultipleEntriesDeleted:
       
   234         case ECalenNotifyDialogClosed:
       
   235             {
       
   236             if(!(iAddAttachmentFromViewer) && !(iAttachmentAlreadyExists))
       
   237                 {
       
   238                 iAttachmentModel->Reset();
       
   239                 }    
       
   240             iAttachmentAlreadyExists = EFalse;
       
   241             iAttachmentInfoIntialized = EFalse;
       
   242             }
       
   243             break;
       
   244         case ECalenNotifyAttachmentViewerClosed:
       
   245             {
       
   246             if(iAddAttachmentFromViewer)
       
   247                 {
       
   248                 // add attachments to the entry being viewed in event viewer
       
   249                 AddAttachmentsToEntryL();
       
   250                 
       
   251                 // Do not reset the model if the attachment list has been opened from the editor.
       
   252                 if(!iController.IsEditorActive())
       
   253                     {
       
   254                     // clear calendar editor's folder
       
   255                     RemoveTemporaryFiles();
       
   256                     
       
   257                     iAttachmentModel->Reset();
       
   258                     }
       
   259                 
       
   260                 iAddAttachmentFromViewer = EFalse;
       
   261                 
       
   262                 // refresh the event view
       
   263                 iController.ViewManager().StartActiveStepL();
       
   264                 }
       
   265             MCalenToolbar* toolbar = iController.Services().ToolbarOrNull();
       
   266             if(toolbar)
       
   267                 {
       
   268                 if(iShowToolbar)
       
   269                     {
       
   270                     toolbar->SetToolbarVisibilityL(ETrue);
       
   271                     }
       
   272                 iShowToolbar = EFalse;
       
   273                 }
       
   274             
       
   275             iAttachmentInfoIntialized = EFalse;            
       
   276             iViewerActive = EFalse;
       
   277             }
       
   278             break;
       
   279         default:
       
   280             break;
       
   281         }
       
   282     
       
   283     TRACE_EXIT_POINT;
       
   284     }
       
   285 
       
   286 // ----------------------------------------------------------------------------
       
   287 // CCalenAttachmentUi::HandleAddAttachmentL
       
   288 // Handles the command ECalenAddAttachment
       
   289 // ----------------------------------------------------------------------------
       
   290 // 
       
   291 void CCalenAttachmentUi::HandleAddAttachmentL()
       
   292     {
       
   293     TRACE_ENTRY_POINT;
       
   294     
       
   295     TInt selectedIndex(0);
       
   296     CAknListQueryDialog* dlg = new (ELeave) CAknListQueryDialog(&selectedIndex);
       
   297     dlg->PrepareLC( R_ATTACHMENT_LIST_QUERY );
       
   298     
       
   299     if(dlg->RunLD())
       
   300         {
       
   301         iSelectedCommand = selectedIndex;
       
   302         switch(selectedIndex)
       
   303             {
       
   304             case ECalenAddImage:
       
   305                 {
       
   306                 FetchFileL(CCalenAttachmentUtils::EImage);
       
   307                 }
       
   308                 break;
       
   309             case ECalenAddNote:
       
   310                 {
       
   311                 FetchFileL(CCalenAttachmentUtils::ENote);
       
   312                 }
       
   313                 break;
       
   314             case ECalenAddAudio:
       
   315                 {
       
   316                 FetchFileL(CCalenAttachmentUtils::EAudio);
       
   317                 }
       
   318                 break;
       
   319             case ECalenAddVideo:
       
   320                 {
       
   321                 FetchFileL(CCalenAttachmentUtils::EVideo);
       
   322                 }
       
   323                 break;
       
   324             case ECalenAddOtherFiles:
       
   325                 {
       
   326                 FetchFileL(CCalenAttachmentUtils::EUnknown);
       
   327                 }
       
   328                 break;
       
   329             default:
       
   330                 break;
       
   331             }
       
   332         }
       
   333     else
       
   334         {
       
   335         iController.BroadcastNotification(ECalenNotifyDialogClosed);
       
   336         }
       
   337     TRACE_EXIT_POINT;
       
   338     }
       
   339 
       
   340 // ----------------------------------------------------------------------------
       
   341 // CCalenAttachmentUi::FetchFileL
       
   342 // For fetching the file from other apps
       
   343 // ----------------------------------------------------------------------------
       
   344 //
       
   345 TBool CCalenAttachmentUi::FetchFileL( 
       
   346                     CCalenAttachmentUtils::TCalenAttachmentFileType aFetchType)
       
   347     {
       
   348     TRACE_ENTRY_POINT;       
       
   349     
       
   350     TAiwVariant sizeLimit( KCalenAttachmentOverhead );
       
   351     TAiwGenericParam sizeLimitParam( EGenericParamMMSSizeLimit, sizeLimit );
       
   352 
       
   353     CAiwGenericParamList* paramList = CAiwGenericParamList::NewLC();
       
   354     paramList->AppendL( sizeLimitParam );
       
   355     
       
   356     CDesCArrayFlat* files = new( ELeave ) CDesC16ArrayFlat(1);
       
   357     CleanupStack::PushL(files);
       
   358     
       
   359     TBool fetchOk(EFalse);
       
   360     if(aFetchType == CCalenAttachmentUtils::EUnknown)
       
   361         {
       
   362         TFileName fileName(KNullDesC);
       
   363         // used for fetching any file from the file system
       
   364         if(CCalenAttachmentUtils::FetchAnyFileL( fileName, this ))
       
   365             {
       
   366             fetchOk = ETrue;
       
   367             }
       
   368         }
       
   369     else
       
   370         {       
       
   371         // used for fetching media file and notes 
       
   372         fetchOk = CCalenAttachmentUtils::FetchFileL( aFetchType, *files,
       
   373                                         paramList, EFalse, EFalse, this );
       
   374     
       
   375         
       
   376         if(fetchOk && iSelectedCommand == ECalenAddNote)
       
   377             {         
       
   378             AddAttachmentL(files->MdcaPoint(0));
       
   379             }                
       
   380         }
       
   381     CleanupStack::PopAndDestroy( 2, paramList );
       
   382     
       
   383     if(!fetchOk)
       
   384         {
       
   385         iController.BroadcastNotification(ECalenNotifyDialogClosed);
       
   386         }
       
   387     
       
   388     TRACE_EXIT_POINT;
       
   389     return fetchOk;
       
   390     }
       
   391 
       
   392 // ----------------------------------------------------------------------------
       
   393 // CCalenAttachmentUi::VerifySelectionL
       
   394 // Interface for verifying fetched media files selection
       
   395 // ----------------------------------------------------------------------------
       
   396 //
       
   397 TBool CCalenAttachmentUi::VerifySelectionL(const MDesCArray* aSelectedFiles)
       
   398     {
       
   399     TRACE_ENTRY_POINT;
       
   400     
       
   401     TInt selectedFilesCount = aSelectedFiles->MdcaCount();
       
   402     for(TInt index=0;index<selectedFilesCount;index++)
       
   403         {
       
   404         RFile file;
       
   405         CEikonEnv* eikonEnv = CEikonEnv::Static();
       
   406         RFs& fs = eikonEnv->FsSession();
       
   407         User::LeaveIfError(fs.ShareProtected());
       
   408         TInt err = file.Open( fs, aSelectedFiles->MdcaPoint(index), EFileRead );
       
   409         file.Close();
       
   410         if( KErrNone != err )
       
   411             {
       
   412             // if any error in opening file then return EFalse
       
   413             return EFalse;
       
   414             }
       
   415         AddAttachmentL(aSelectedFiles->MdcaPoint(index));
       
   416         } 
       
   417     
       
   418     TRACE_EXIT_POINT; 
       
   419     
       
   420     return ETrue;
       
   421 	}
       
   422 
       
   423 // ----------------------------------------------------------------------------
       
   424 // CCalenAttachmentUi::OkToExitL
       
   425 // Get's called when user selects any file from file system through common
       
   426 // memory dialogs
       
   427 // ----------------------------------------------------------------------------
       
   428 //
       
   429 TBool CCalenAttachmentUi::OkToExitL( const TDesC& aDriveAndPath, const TEntry& aEntry )
       
   430     {
       
   431     TRACE_ENTRY_POINT;
       
   432     
       
   433     TInt length = aDriveAndPath.Length() + aEntry.iName.Length();
       
   434     if(length)
       
   435         {
       
   436         HBufC* filename = HBufC::NewLC( length );
       
   437         TPtr ptr = filename->Des();
       
   438         ptr.Append( aDriveAndPath );
       
   439         ptr.Append( aEntry.iName );
       
   440         
       
   441         RFile file;
       
   442         CEikonEnv* eikonEnv = CEikonEnv::Static();
       
   443         RFs& fs = eikonEnv->FsSession();
       
   444         User::LeaveIfError(fs.ShareProtected());
       
   445         TInt err = file.Open( fs, *filename, EFileRead );
       
   446         file.Close();
       
   447         if( KErrNone != err )
       
   448             {
       
   449             // if any error in opening file then return EFalse
       
   450             return EFalse;
       
   451             }
       
   452         
       
   453         AddAttachmentL(*filename);
       
   454 
       
   455         CleanupStack::PopAndDestroy(filename);
       
   456         }
       
   457     
       
   458     TRACE_EXIT_POINT;
       
   459     return ETrue;
       
   460     }
       
   461 
       
   462 // ----------------------------------------------------------------------------
       
   463 // CCalenAttachmentUi::CheckForExistingAttachmentsL
       
   464 // check for already existing attachments from the entry
       
   465 // ----------------------------------------------------------------------------
       
   466 //
       
   467 void CCalenAttachmentUi::CheckForExistingAttachmentsL()
       
   468     {
       
   469     TRACE_ENTRY_POINT;
       
   470     
       
   471     if( iAttachmentInfoIntialized )
       
   472         return;
       
   473     
       
   474     MCalenContext& context = iController.Services().Context();
       
   475     TCalLocalUid entryUid = context.InstanceId().iEntryLocalUid;
       
   476     
       
   477     if(entryUid)  // If it is not for new entry
       
   478 	    {
       
   479 	    CCalEntry* entry = iController.Services().EntryViewL(context.InstanceId().iColId)->FetchL(entryUid);
       
   480 	    CleanupStack::PushL(entry);
       
   481 	    if(entry)
       
   482 	        {
       
   483 	        iAttachmentModel->CheckForExistingAttachmentsL(entry);
       
   484 	        iAttachmentInfoIntialized = ETrue;
       
   485 	        }
       
   486 	    CleanupStack::PopAndDestroy(entry);
       
   487 	    }
       
   488     TRACE_EXIT_POINT;
       
   489     }
       
   490 
       
   491 // ----------------------------------------------------------------------------
       
   492 // CCalenAttachmentUi::NotifyChanges
       
   493 // Notify if attachment added or removed
       
   494 // ----------------------------------------------------------------------------
       
   495 //
       
   496 void CCalenAttachmentUi::NotifyChanges( TCalenAttachmentCommand aCommand, 
       
   497                                        TInt aAttachmentIndex )
       
   498     {
       
   499     TRACE_ENTRY_POINT;
       
   500     
       
   501     if(iViewerActive)
       
   502         {
       
   503         iController.ViewManager().SetRepopulation(ETrue);
       
   504         }
       
   505     
       
   506     if(aCommand == ECalenAttachmentAdded)
       
   507         {
       
   508         iController.BroadcastNotification(ECalenNotifyAttachmentAdded);
       
   509         }
       
   510     else if(aCommand == ECalenAttachmentRemoved)
       
   511         {
       
   512         TRAP_IGNORE(RemoveAttachmentFromEntryL(aAttachmentIndex));
       
   513         iController.BroadcastNotification(ECalenNotifyAttachmentRemoved);
       
   514         }
       
   515 
       
   516     TRACE_ENTRY_POINT;
       
   517     }
       
   518 
       
   519 // ----------------------------------------------------------------------------
       
   520 // CCalenAttachmentUi::GetAttachmentFileL
       
   521 // Notify if attachment added or removed
       
   522 // ----------------------------------------------------------------------------
       
   523 //
       
   524 RFile CCalenAttachmentUi::GetAttachmentFileL(TInt aIndex)
       
   525     {
       
   526     TRACE_ENTRY_POINT;
       
   527     
       
   528     MCalenContext& context = iController.Services().Context();
       
   529     TCalLocalUid entryUid = context.InstanceId().iEntryLocalUid;
       
   530     RFile fileHandle;
       
   531     CCalEntry* entry = iController.Services().EntryViewL(context.InstanceId().iColId)->FetchL(entryUid);
       
   532     CleanupStack::PushL(entry);
       
   533     if(entry)
       
   534         {
       
   535         TInt attachmentCount = entry->AttachmentCountL();
       
   536         if(attachmentCount)
       
   537             {
       
   538             CCalAttachment* attachment = entry->AttachmentL(aIndex);
       
   539             if(attachment->FileAttachment())
       
   540                 {
       
   541                 attachment->FileAttachment()->LoadBinaryDataL();
       
   542                 CleanupClosePushL(fileHandle);
       
   543                 attachment->FileAttachment()->FetchFileHandleL(fileHandle);
       
   544                 CleanupStack::Pop(&fileHandle);
       
   545                 }
       
   546             }
       
   547         }
       
   548     
       
   549     CleanupStack::PopAndDestroy(entry);
       
   550     
       
   551     TRACE_EXIT_POINT;
       
   552     return fileHandle;
       
   553     }
       
   554 
       
   555 // ----------------------------------------------------------------------------
       
   556 // CCalenAttachmentUi::AddAttachmentL
       
   557 // Adds attachments to the model
       
   558 // ----------------------------------------------------------------------------
       
   559 //
       
   560 void CCalenAttachmentUi::AddAttachmentL(const TDesC& aSourceFilePath)
       
   561     {
       
   562     TRACE_ENTRY_POINT;
       
   563     
       
   564     TParsePtrC fileNameParser(aSourceFilePath);
       
   565     TPtrC parsedFileName = fileNameParser.NameAndExt();
       
   566     TBool drmProtected;
       
   567     
       
   568     if(!iAttachmentModel->NumberOfItems())
       
   569         {
       
   570         CheckForExistingAttachmentsL();
       
   571         }
       
   572     
       
   573     TBool isAlreadyExists = IsDuplicateNameL(parsedFileName);
       
   574     CheckDRMStatus(aSourceFilePath,drmProtected);
       
   575     
       
   576     if(drmProtected || isAlreadyExists)
       
   577         {
       
   578         iAttachmentAlreadyExists = ETrue;
       
   579         iController.BroadcastNotification(ECalenNotifyDialogClosed);
       
   580         return;
       
   581         }
       
   582         
       
   583     TFileName destinationfileName;
       
   584     CopyFetchedFilesL( destinationfileName, aSourceFilePath );
       
   585     iAttachmentModel->AddAttachmentL(destinationfileName, destinationfileName);
       
   586     
       
   587     TRACE_EXIT_POINT;
       
   588     }
       
   589 
       
   590 // ----------------------------------------------------------------------------
       
   591 // CCalenAttachmentUi::RemoveAttachmentFromEntryL
       
   592 // Remove attachments from the entry
       
   593 // ----------------------------------------------------------------------------
       
   594 //
       
   595 void CCalenAttachmentUi::RemoveAttachmentFromEntryL(TInt aAttachmentIndex)
       
   596     {
       
   597     TRACE_ENTRY_POINT;
       
   598     
       
   599     // If user is deleting it from editors, then dont save it now as there is a chance that user discards these
       
   600     // changes. If user saves it, then editors will save these newly deleted attachments.
       
   601     if(iController.IsEditorActive())
       
   602         {
       
   603         return;
       
   604         }
       
   605         
       
   606     MCalenContext& context = iController.Services().Context();
       
   607     TCalLocalUid entryUid = context.InstanceId().iEntryLocalUid;
       
   608     
       
   609     CCalEntry* entry = iController.Services().EntryViewL(context.InstanceId().iColId)->FetchL(entryUid);
       
   610     CleanupStack::PushL(entry);
       
   611     if(entry)
       
   612         {
       
   613         if(entry->AttachmentCountL())
       
   614             {
       
   615             CCalAttachment* attachment = entry->AttachmentL(aAttachmentIndex);
       
   616             if(attachment)
       
   617                 {
       
   618                 entry->DeleteAttachmentL(*attachment);
       
   619                
       
   620                 CCalEntryView* entryView = iController.Services().EntryViewL(context.InstanceId().iColId);
       
   621                 CCalenInterimUtils2::StoreL( *entryView, *entry, ETrue );
       
   622                 }
       
   623             }
       
   624         }
       
   625     CleanupStack::PopAndDestroy(entry);
       
   626     
       
   627     TRACE_EXIT_POINT;
       
   628     }
       
   629 
       
   630 // ----------------------------------------------------------------------------
       
   631 // CCalenAttachmentUi::AddAttachmentsToEntryL
       
   632 // Adds attachments to the entry from event view
       
   633 // ----------------------------------------------------------------------------
       
   634 //
       
   635 void CCalenAttachmentUi::AddAttachmentsToEntryL()
       
   636     {
       
   637     TRACE_ENTRY_POINT;
       
   638         
       
   639     TInt attachmentCount = iAttachmentModel->NumberOfItems();
       
   640     if(attachmentCount)
       
   641         {
       
   642         MCalenContext& context = iController.Services().Context();
       
   643         TCalLocalUid entryUid = context.InstanceId().iEntryLocalUid;
       
   644             
       
   645         CCalEntry* entry = iController.Services().EntryViewL(context.InstanceId().iColId)->FetchL(entryUid);
       
   646         CleanupStack::PushL(entry);
       
   647         
       
   648         if(entry)
       
   649             {
       
   650             RPointerArray<CCalenAttachmentInfo> attachmentInfoList;           
       
   651              
       
   652             iAttachmentModel->GetAttachmentListL(attachmentInfoList);
       
   653             for(TInt index=0;index<attachmentCount;index++)
       
   654                 {
       
   655                 CCalenAttachmentInfo* attachmentInfo = attachmentInfoList[index];
       
   656                 // check if attachmentinfo created for new attachment,to avoid adding
       
   657                 // the attachments which already exists in the entry
       
   658                 if(attachmentInfo->StoreType() == CCalenAttachmentInfo::ECalenNewAttachment)
       
   659                     {
       
   660                     TParsePtrC fileNameParser(attachmentInfo->SystemFileName());
       
   661                     RFile fileHandle;    
       
   662                     CEikonEnv* eikonEnv = CEikonEnv::Static();
       
   663                     RFs& fs = eikonEnv->FsSession();
       
   664                     User::LeaveIfError(fs.ShareProtected());
       
   665                     TInt aSize;
       
   666                     TInt err = fileHandle.Open(fs, attachmentInfo->SystemFileName(), EFileWrite);
       
   667                     TInt error = fileHandle.Size(aSize);
       
   668                     HBufC8* data = HBufC8::NewLC(aSize);
       
   669                     TPtr8 fileData = data->Des();
       
   670                     if (error == KErrNone)
       
   671                         {                
       
   672                         TInt readingError = fileHandle.Read(fileData,aSize);  
       
   673                         if(readingError == KErrNone)
       
   674                             {
       
   675                             CCalAttachment* attachment = CCalAttachment::NewFileL(data);
       
   676                             CleanupStack::Pop(data);
       
   677                             CleanupStack::PushL(attachment);
       
   678                             
       
   679                             // sets the label for the attachment
       
   680                             attachment->SetLabelL(fileNameParser.NameAndExt());
       
   681                             // sets mime type for the attachment
       
   682                             attachment->SetMimeTypeL(attachmentInfo->DataType().Des8());
       
   683                             
       
   684                             CleanupClosePushL(fileHandle);
       
   685                             CleanupStack::PopAndDestroy(&fileHandle);
       
   686                             entry->AddAttachmentL(*attachment);
       
   687                             CleanupStack::Pop(attachment);
       
   688                             }                
       
   689                         }
       
   690                     
       
   691                     }
       
   692                 }
       
   693                         
       
   694             attachmentInfoList.Close();
       
   695             
       
   696             // If user is adding it from editors, then dont save it now as there is a chance that user discards these
       
   697             // changes. If user saves it, then editors will save these newly added attachments.
       
   698             if(iController.IsEditorActive())
       
   699                 {
       
   700                     CCalEntryView* entryView = iController.Services().EntryViewL(context.InstanceId().iColId);
       
   701                     CCalenInterimUtils2::StoreL( *entryView, *entry, ETrue );
       
   702                 }
       
   703             }
       
   704         CleanupStack::PopAndDestroy(entry);
       
   705         }
       
   706     TRACE_EXIT_POINT;
       
   707     }
       
   708 
       
   709 // ----------------------------------------------------------------------------
       
   710 // CCalenAttachmentUi::CopyFetchedFilesL
       
   711 // Copy fetched files in to temporary dir,adding the attachment to the entry 
       
   712 // will move the file from temporary to symbian calendar db
       
   713 // ----------------------------------------------------------------------------
       
   714 //
       
   715 void CCalenAttachmentUi::CopyFetchedFilesL( TFileName& aTempPath, 
       
   716                                             const TDesC& aSourceFilePath)
       
   717     {
       
   718     TRACE_ENTRY_POINT;
       
   719     
       
   720     TParsePtrC fileNameParser(aSourceFilePath);
       
   721     TFileName destinationfilePath;
       
   722     const TDesC& destinatiofileName = fileNameParser.NameAndExt();
       
   723     CCalenAttachmentUtils::GetCalenEditorTempPath( destinationfilePath,
       
   724                                                    &destinatiofileName );
       
   725     aTempPath.Append(destinationfilePath);
       
   726     
       
   727     CFileMan* fileManager = CFileMan::NewL(CEikonEnv::Static()->FsSession());
       
   728     CleanupStack::PushL(fileManager);
       
   729     fileManager->Copy( aSourceFilePath, destinationfilePath, CFileMan::ERecurse );
       
   730     CleanupStack::PopAndDestroy(fileManager);
       
   731     
       
   732     TRACE_EXIT_POINT;
       
   733     }
       
   734 
       
   735 // ------------------------------------------------------------------------------
       
   736 // void CCalenAttachmentUi::DoAttachmentL()
       
   737 // Opens an attachment dialog.
       
   738 // ------------------------------------------------------------------------------
       
   739 //
       
   740 void CCalenAttachmentUi::DoAttachmentL()
       
   741     {
       
   742     TRACE_ENTRY_POINT;
       
   743     
       
   744     CheckForExistingAttachmentsL();
       
   745     //Check if iTitle allready exists
       
   746     if(!iTitle)
       
   747         {        
       
   748         iTitle = CCoeEnv::Static()->AllocReadResourceLC(
       
   749         R_VIEWER_TITLE_ATTACHMENTS);
       
   750 
       
   751         CleanupStack::Pop(iTitle);
       
   752         }
       
   753     
       
   754     CCalenAttachmentModel* attmodel = iAttachmentModel;
       
   755         
       
   756     iAttachmentdlg = CCalenViewAttachmentsDialog::NewL(*iTitle,*attmodel,iController );    
       
   757 
       
   758     // Hide the toolbar before we display settings menu
       
   759     MCalenToolbar* toolbar = iController.Services().ToolbarOrNull();
       
   760     if(toolbar&&toolbar->IsVisible())
       
   761         {
       
   762         toolbar->SetToolbarVisibilityL(EFalse); 
       
   763         iShowToolbar = ETrue;
       
   764         }
       
   765     //To make sure we are viewer, so for delete attachment, notification will be issued 
       
   766     // only when viewer is closed (EFalse)
       
   767     iViewerActive = ETrue;
       
   768     
       
   769     // safe to call LD even as a member variable, since sets itself to NULL when deleting
       
   770     iAttachmentdlg->ExecuteLD(R_CALEN_ATTACHMENT_DIALOG); // CSI: 50 # see comment above
       
   771     
       
   772 
       
   773     if(iAttachmentLoadingOn)
       
   774         {
       
   775         ShowWaitNoteL( ETrue );
       
   776         if ( iWaitDialog )
       
   777             {
       
   778             iWaitDialog->SetCallback( this );
       
   779             }
       
   780         }
       
   781     else
       
   782         {
       
   783         //Update attachment models fetched-flags
       
   784         //if we are not currently loading attachments.
       
   785         if(iAttachmentdlg)
       
   786             {
       
   787             iAttachmentdlg->UpdateAttachmentsFetchFlags();
       
   788             }
       
   789 
       
   790         }
       
   791 
       
   792     // Dialer is always enabled in attachment dialog
       
   793    // SetDialerEnabled( ETrue );
       
   794     TRACE_EXIT_POINT;
       
   795     }
       
   796 
       
   797 // ------------------------------------------------------------------------------
       
   798 // CCalenAttachmentUi::ShowWaitNoteL()
       
   799 // Shows wait note
       
   800 // ------------------------------------------------------------------------------
       
   801 //
       
   802 void CCalenAttachmentUi::ShowWaitNoteL( TBool aCanCancel )
       
   803     {
       
   804     TRACE_ENTRY_POINT;
       
   805     
       
   806     if (!iWaitDialog)
       
   807         {
       
   808         // safe to call LD even as a member variable,
       
   809         // since sets itself to NULL when deleting
       
   810         iWaitDialog = new(ELeave)CAknWaitDialog(
       
   811                 (reinterpret_cast<CEikDialog**>( &iWaitDialog )), ETrue);
       
   812 
       
   813         // safe to call LD even as a member variable, since sets itself to NULL when deleting
       
   814         iWaitDialog->ExecuteLD(R_VIEWER_WAIT_OPEN_DIALOG_NOTE); // CSI: 50 # see comment above
       
   815         iWaitDialog->MakePanelButtonVisible( EAknSoftkeyCancel, aCanCancel );
       
   816         }
       
   817     TRACE_EXIT_POINT;
       
   818     }
       
   819 
       
   820 // ------------------------------------------------------------------------------
       
   821 // From MProgressDialogCallback called when wait note dismissed
       
   822 // called when dialog is dismissed
       
   823 // ------------------------------------------------------------------------------
       
   824 //
       
   825 void CCalenAttachmentUi::DialogDismissedL( TInt aButtonId )
       
   826    {
       
   827    TRACE_ENTRY_POINT;
       
   828    
       
   829     if ( aButtonId == EAknSoftkeyCancel )
       
   830         {
       
   831         delete iAttachmentdlg;
       
   832         iAttachmentdlg = NULL;
       
   833         iWaitDialog = NULL;
       
   834         }
       
   835     TRACE_EXIT_POINT;
       
   836     }
       
   837 
       
   838 // -----------------------------------------------------------------------------
       
   839 // CCalenAttachmentUi::CheckDRMStatus()
       
   840 // -----------------------------------------------------------------------------
       
   841 // 
       
   842 void CCalenAttachmentUi::CheckDRMStatus( const TDesC& aFileName,TBool& aProtection )
       
   843     {
       
   844     TRACE_ENTRY_POINT;
       
   845     
       
   846     RFile file; 
       
   847     file.Close();
       
   848     RFs& fs = CEikonEnv::Static()->FsSession();
       
   849     TInt err1 = file.Open( fs, aFileName, EFileRead | EFileShareReadersOnly );
       
   850     CleanupClosePushL( file );
       
   851     
       
   852     CContent* content = CContent::NewLC(file);
       
   853     TInt isDRMProtected = 0;
       
   854     
       
   855     //Check if file is drm protected. If not no need for further checks.    
       
   856     User::LeaveIfError(content->GetAttribute(EIsProtected, isDRMProtected));
       
   857     if (isDRMProtected)
       
   858         {
       
   859         aProtection = ETrue;
       
   860         
       
   861         //DRM protected file: can't attach the DRM protected file 
       
   862         CAknInformationNote* note = new ( ELeave ) CAknInformationNote(ETrue);
       
   863         HBufC* cannotAttach = StringLoader::LoadLC( 
       
   864                 R_QTN_CALEN_INFO_CANNOT_OPEN, CCoeEnv::Static() );
       
   865         note->ExecuteLD( *cannotAttach );
       
   866         CleanupStack::PopAndDestroy( cannotAttach );
       
   867         }
       
   868     else
       
   869         {
       
   870         aProtection = EFalse;
       
   871         }    
       
   872     CleanupStack::PopAndDestroy(content);
       
   873     CleanupStack::PopAndDestroy(&file);
       
   874     
       
   875     TRACE_EXIT_POINT;
       
   876     }
       
   877 
       
   878 // -----------------------------------------------------------------------------
       
   879 // CCalenAttachmentUi::IsDuplicateNameL()
       
   880 // Compares the name of the already attached and currently 
       
   881 // attached attachment names.
       
   882 // returns ETrue if match is found, EFalse otherwise.
       
   883 // -----------------------------------------------------------------------------
       
   884 // 
       
   885 TBool CCalenAttachmentUi::IsDuplicateNameL(const TDesC& aSelectedFile)
       
   886     {               
       
   887     TRACE_ENTRY_POINT;
       
   888     
       
   889     TInt attachmentCount = iAttachmentModel->NumberOfItems();
       
   890     TBool matchNotFound(ETrue);
       
   891     if(attachmentCount>0)
       
   892         {
       
   893         RPointerArray<CCalenAttachmentInfo> attachmentInfoList;
       
   894         iAttachmentModel->GetAttachmentListL(attachmentInfoList);    
       
   895         
       
   896         TParsePtrC fileNameParser(aSelectedFile);
       
   897         TPtrC parsedFileName = fileNameParser.Ext();
       
   898         for(TInt index=0; index<attachmentCount && matchNotFound; index++)
       
   899             {     
       
   900             TParsePtrC fileNameParser(attachmentInfoList[index]->FileName());
       
   901             TPtrC parsedFileName = fileNameParser.NameAndExt();
       
   902             matchNotFound = aSelectedFile.Compare(parsedFileName); 
       
   903             }
       
   904         attachmentInfoList.Close();
       
   905         }
       
   906     
       
   907       if(!matchNotFound)
       
   908           {
       
   909           //though attachment is aleady attached to this entry, show the info note for this info.
       
   910           CAknInformationNote* note = new ( ELeave ) CAknInformationNote(ETrue);
       
   911           HBufC* cannotAttach = StringLoader::LoadLC( 
       
   912                   R_QTN_CALEN_INFO_ALREADY_ATTACHED, CCoeEnv::Static() );
       
   913           note->ExecuteLD( *cannotAttach );
       
   914           CleanupStack::PopAndDestroy( cannotAttach );
       
   915           }
       
   916         
       
   917     
       
   918     TRACE_EXIT_POINT;
       
   919     return !matchNotFound;
       
   920     }
       
   921 
       
   922 // -----------------------------------------------------------------------------
       
   923 // CCalenAttachmentUi::CompareContentOfTextFiles()
       
   924 // Compares the binary data of already attached and currently selected text file.
       
   925 // -----------------------------------------------------------------------------
       
   926 // 
       
   927 TBool CCalenAttachmentUi::CompareContentOfTextFiles( const TDesC& aSelectedFile,
       
   928                                     const TDesC& aAlreadyAttachedFile)
       
   929     {
       
   930     TRACE_ENTRY_POINT;
       
   931     
       
   932     TBool matchFound(EFalse);
       
   933     //get the binary data of both the files and compare.       
       
   934     RFile fileHandle;    
       
   935     CEikonEnv* eikonEnv = CEikonEnv::Static();
       
   936     RFs& fs = eikonEnv->FsSession();
       
   937     User::LeaveIfError(fs.ShareProtected());
       
   938     
       
   939     //for newly selected file
       
   940     TInt aSelectedFileSize;
       
   941     TInt openErrForSelectedFile = fileHandle.Open(fs, aSelectedFile, EFileWrite);
       
   942     if( KErrNone != openErrForSelectedFile )
       
   943         {
       
   944         //Not Handling the Error case as of now.
       
   945         }
       
   946     
       
   947     TInt sizeErrorForSelectedFile = fileHandle.Size(aSelectedFileSize);
       
   948     HBufC8* aNewlySelectedFile = HBufC8::NewLC(aSelectedFileSize);
       
   949     TPtr8 aSelectedFileData = aNewlySelectedFile->Des();
       
   950     
       
   951     // create one more file handle, because for
       
   952     // the same file its coming as "Already in use".       
       
   953     RFile fileHandleForAlreadySelected;    
       
   954     
       
   955     // for already selected file
       
   956     TInt aAlreadySelectedFileSize;    
       
   957     TInt openErrorForAlreadyAttachedFile = 
       
   958     fileHandleForAlreadySelected.Open(fs, aAlreadyAttachedFile, EFileWrite);
       
   959        
       
   960     if( KErrNone != openErrorForAlreadyAttachedFile )
       
   961         {
       
   962         fileHandleForAlreadySelected.Close();
       
   963         openErrorForAlreadyAttachedFile = 
       
   964         fileHandleForAlreadySelected.Open(fs, aAlreadyAttachedFile, EFileWrite);
       
   965         }
       
   966     
       
   967     TInt sizeErrorForAlreadyAttachedFile = 
       
   968       fileHandleForAlreadySelected.Size(aAlreadySelectedFileSize);
       
   969     HBufC8* aAttachedFile = HBufC8::NewLC(aAlreadySelectedFileSize);
       
   970     TPtr8 aAttachedFileData = aAttachedFile->Des();
       
   971     
       
   972     if(!(aAttachedFileData.Compare(aSelectedFileData)))
       
   973         {
       
   974         matchFound=ETrue;
       
   975         }
       
   976    
       
   977     CleanupStack::Pop(2);
       
   978     CleanupClosePushL(fileHandle);
       
   979     CleanupStack::PopAndDestroy(&fileHandle);    
       
   980     
       
   981     TRACE_EXIT_POINT;
       
   982     return matchFound;
       
   983     }
       
   984 
       
   985 // -----------------------------------------------------------------------------
       
   986 // CCalenAttachmentUi::RemoveTemporaryFiles()
       
   987 // removes the temporary files, those we have added to temp path.
       
   988 // -----------------------------------------------------------------------------
       
   989 // 
       
   990 void CCalenAttachmentUi::RemoveTemporaryFiles()
       
   991     {
       
   992     TRACE_ENTRY_POINT;
       
   993     
       
   994     CEikonEnv* eikonEnv = CEikonEnv::Static();
       
   995     CFileMan* fileMan = CFileMan::NewL( eikonEnv->FsSession() );
       
   996     CleanupStack::PushL( fileMan );
       
   997     
       
   998     // delete files from temp directory.
       
   999     TFileName temppath;
       
  1000     CCalenAttachmentUtils::GetCalenEditorTempPath( temppath );
       
  1001     fileMan->Delete( temppath );
       
  1002     CleanupStack::PopAndDestroy( fileMan );
       
  1003     
       
  1004     TRACE_EXIT_POINT;
       
  1005     }
       
  1006 // end of file