javacommons/fileutils/src.s60/drivechangedeventgenerator.cpp
branchRCL_3
changeset 14 04becd199f91
equal deleted inserted replaced
13:f5050f1da672 14:04becd199f91
       
     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:  DriveChangedEventGenerator - S60 specific
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "logger.h"
       
    20 
       
    21 #include "drivechangedeventgenerator.h"
       
    22 #include "driveutilities.h"
       
    23 
       
    24 namespace java
       
    25 {
       
    26 namespace fileutils
       
    27 {
       
    28 
       
    29 DriveChangedEventGenerator::DriveChangedEventGenerator(DriveListenerInterface* aEventDispatcher)
       
    30         :iEventDispatcher(aEventDispatcher)
       
    31 {
       
    32     JELOG2(EJavaFile);
       
    33 }
       
    34 
       
    35 DriveChangedEventGenerator::~DriveChangedEventGenerator()
       
    36 {
       
    37     JELOG2(EJavaFile);
       
    38 }
       
    39 
       
    40 void DriveChangedEventGenerator::startEvents()
       
    41 {
       
    42     JELOG2(EJavaFile);
       
    43 
       
    44     driveInfos drives;
       
    45     DriveUtilities::getAllDrives(drives);
       
    46 
       
    47     if (KErrNone == iFs.Connect())
       
    48     {
       
    49         for (driveInfos::const_iterator iter = drives.begin() ;
       
    50                 iter != drives.end() ; ++iter)
       
    51         {
       
    52             if (iter->iIsRemovable)
       
    53             {
       
    54                 iDriveObjects.push_back(new DriveChangedActive(*iter, this, &iFs));
       
    55             }
       
    56         }
       
    57     }
       
    58 }
       
    59 void DriveChangedEventGenerator::stopEvents()
       
    60 {
       
    61     JELOG2(EJavaFile);
       
    62     iFs.Close();
       
    63 }
       
    64 
       
    65 
       
    66 // DriveListenerInterface
       
    67 void DriveChangedEventGenerator::driveChanged(const int& aOperation, const driveInfo& aDriveInfo)
       
    68 {
       
    69     JELOG2(EJavaFile);
       
    70 
       
    71     iEventDispatcher->driveChanged(aOperation, aDriveInfo);
       
    72 }
       
    73 // change notification will be generated for this directory
       
    74 _LIT(KNotificationDirectory,"mediachange\\");
       
    75 
       
    76 DriveChangedEventGenerator::DriveChangedActive::DriveChangedActive(driveInfo aDriveInfo,
       
    77         DriveListenerInterface* aEventDispatcher, RFs* aFsSession)
       
    78         :CActive(EPriorityNormal),
       
    79         iDrive(0),
       
    80         iDriveInfo(aDriveInfo),
       
    81         iEventDispatcher(aEventDispatcher),
       
    82         iFs(aFsSession)
       
    83 {
       
    84     JELOG2(EJavaFile);
       
    85 
       
    86     CActiveScheduler::Add(this);
       
    87 
       
    88     TChar driveChar = (TChar) iDriveInfo.iRootPath[0];
       
    89     iFs->CharToDrive(driveChar, iDrive);
       
    90 
       
    91     TPath privatePath;
       
    92     iFs->PrivatePath(privatePath);
       
    93 
       
    94     // obtain notification path
       
    95     iNotificationPath.Append(driveChar);
       
    96     iNotificationPath.Append(KDriveDelimiter);
       
    97     iNotificationPath.Append(privatePath);
       
    98     iNotificationPath.Append(KNotificationDirectory);
       
    99 
       
   100     iFs->NotifyChange(ENotifyEntry, iStatus, iNotificationPath);
       
   101     SetActive();
       
   102 }
       
   103 
       
   104 DriveChangedEventGenerator::DriveChangedActive::~DriveChangedActive()
       
   105 {
       
   106     JELOG2(EJavaFile);
       
   107 
       
   108     Cancel();
       
   109 }
       
   110 
       
   111 void DriveChangedEventGenerator::DriveChangedActive::RunL()
       
   112 {
       
   113     JELOG2(EJavaFile);
       
   114 
       
   115     TDriveInfo driveInfo;
       
   116     TInt rc = iFs->Drive(driveInfo, iDrive);
       
   117     if (KErrNone == rc)
       
   118     {
       
   119         LOG(EJavaFile, EInfo, "iFs->Drive() returns KErrNone");
       
   120         LOG1(EJavaFile, EInfo, "driveInfo.iType = %d", driveInfo.iType);
       
   121     }
       
   122 
       
   123     TVolumeInfo volumeInfo;
       
   124     int operation = 0;
       
   125     rc = iFs->Volume(volumeInfo, iDrive);
       
   126     if (KErrNone == rc)
       
   127     {
       
   128         LOG(EJavaFile, EInfo, "iFs->Volume() returns KErrNone");
       
   129         operation = DriveListenerInterface::REMOVABLE_MEDIA_INSERTED_C;
       
   130         iDriveInfo.iIsPresent = true;
       
   131         iDriveInfo.iId = volumeInfo.iUniqueID;
       
   132     }
       
   133     else
       
   134     {
       
   135         LOG1(EJavaFile, EInfo, "iFs->Volume() returns %d", rc);
       
   136         operation = DriveListenerInterface::REMOVABLE_MEDIA_REMOVED_C;
       
   137         iDriveInfo.iIsPresent = false;
       
   138     }
       
   139 
       
   140     iEventDispatcher->driveChanged(operation, iDriveInfo);
       
   141 
       
   142     iFs->NotifyChange(ENotifyEntry, iStatus, iNotificationPath);
       
   143     SetActive();
       
   144 }
       
   145 
       
   146 void DriveChangedEventGenerator::DriveChangedActive::DoCancel()
       
   147 {
       
   148     JELOG2(EJavaFile);
       
   149 
       
   150     iFs->NotifyChangeCancel();
       
   151 }
       
   152 } // namespace fileutils
       
   153 } // namespace java