javacommons/fileutils/src/driveobserverclient.cpp
branchRCL_3
changeset 19 04becd199f91
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     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:  DriveObserverImpl - platform S60 specific
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "logger.h"
       
    20 #include "comms.h"
       
    21 #include "commsmessage.h"
       
    22 #include "commsclientendpoint.h"
       
    23 
       
    24 #include "driveobserverclient.h"
       
    25 #include "driveobservermessages.h"
       
    26 
       
    27 using java::comms::CommsClientEndpoint;
       
    28 
       
    29 namespace java
       
    30 {
       
    31 namespace fileutils
       
    32 {
       
    33 
       
    34 DriveObserverClient::DriveObserverClient()
       
    35         :mEventsSubscribed(false), mComms(0), mCommsOwner(false)
       
    36 {
       
    37     JELOG2(EJavaFile);
       
    38 }
       
    39 
       
    40 DriveObserverClient::~DriveObserverClient()
       
    41 {
       
    42     JELOG2(EJavaFile);
       
    43 
       
    44     // Clear listeners collection
       
    45     mListeners.clear();
       
    46     disableEvents();
       
    47 }
       
    48 
       
    49 int DriveObserverClient::registerListener(DriveListenerInterface* aListener,
       
    50         const int& aServerCommsAddress)
       
    51 {
       
    52     JELOG2(EJavaFile);
       
    53     std::set<DriveListenerInterface*>::iterator iter = mListeners.find(aListener);
       
    54     if (iter == mListeners.end())
       
    55     {
       
    56         mListeners.insert(aListener);
       
    57     }
       
    58 
       
    59     enableEvents(aServerCommsAddress);
       
    60 
       
    61     return mListeners.size();
       
    62 }
       
    63 
       
    64 int DriveObserverClient::unregisterListener(DriveListenerInterface* aListener,
       
    65         const int& /*aServerCommsAddress*/)
       
    66 {
       
    67     JELOG2(EJavaFile);
       
    68     std::set<DriveListenerInterface*>::iterator iter = mListeners.find(aListener);
       
    69     if (iter != mListeners.end())
       
    70     {
       
    71         mListeners.erase(iter);
       
    72     }
       
    73     return mListeners.size();
       
    74 }
       
    75 
       
    76 void DriveObserverClient::enableEvents(const int& aServerCommsAddress)
       
    77 {
       
    78     if (!mEventsSubscribed)
       
    79     {
       
    80         int connecStatus = 0;
       
    81 
       
    82         // Create CommsEndpoint either use find or create a new one
       
    83         if (aServerCommsAddress == java::comms::IPC_ADDRESS_JAVA_CAPTAIN_C)
       
    84         {
       
    85             mComms = CommsClientEndpoint::find(L"javacaptain");
       
    86 
       
    87             if (mComms == 0)
       
    88             {
       
    89                 mComms = new CommsClientEndpoint;
       
    90                 mCommsOwner = true;
       
    91 
       
    92                 connecStatus = mComms->connect(java::comms::IPC_ADDRESS_JAVA_CAPTAIN_C);
       
    93             }
       
    94         }
       
    95         else
       
    96         {
       
    97             mComms = new CommsClientEndpoint;
       
    98             mCommsOwner = true;
       
    99             connecStatus = mComms->connect(aServerCommsAddress);
       
   100         }
       
   101 
       
   102         if (0 == connecStatus)
       
   103         {
       
   104             // Register moduleId
       
   105             mComms->registerListener(java::comms::PLUGIN_ID_DRIVE_OBSERVER_NATIVE_C, this);
       
   106 
       
   107             // Subscribe to events
       
   108             CommsMessage msg;
       
   109             setSubscribeParams(msg, java::comms::PLUGIN_ID_DRIVE_OBSERVER_NATIVE_C);
       
   110             if (0 == mComms->send(msg))
       
   111             {
       
   112                 mEventsSubscribed = true;
       
   113             }
       
   114         }
       
   115     }
       
   116 }
       
   117 
       
   118 void DriveObserverClient::disableEvents()
       
   119 {
       
   120     if (mEventsSubscribed)
       
   121     {
       
   122         // Unsubscribe to events
       
   123         CommsMessage msg;
       
   124         setUnsubscribeMessage(msg, java::comms::PLUGIN_ID_DRIVE_OBSERVER_NATIVE_C);
       
   125         mComms->send(msg);
       
   126 
       
   127         // Unregister moduleId
       
   128         mComms->unregisterListener(java::comms::PLUGIN_ID_DRIVE_OBSERVER_NATIVE_C, this);
       
   129 
       
   130         // Close and delete CommsEndpoint if our creation
       
   131         if (mCommsOwner)
       
   132         {
       
   133             mComms->disconnect();
       
   134             delete mComms;
       
   135             mComms = 0;
       
   136             mCommsOwner = false;
       
   137         }
       
   138         mEventsSubscribed = false;
       
   139     }
       
   140 }
       
   141 
       
   142 void DriveObserverClient::processMessage(java::comms::CommsMessage& aMessage)
       
   143 {
       
   144     JELOG2(EJavaFile);
       
   145     switch (aMessage.getMessageId())
       
   146     {
       
   147     case DRIVEOBSERVER_MSG_DRIVE_CHANGED_EVENT:
       
   148     {
       
   149         int operation;
       
   150         driveInfo di;
       
   151         getDriveChangedParams(aMessage, operation, di);
       
   152         std::set<DriveListenerInterface*>::iterator iter;
       
   153         for (iter = mListeners.begin(); iter != mListeners.end(); ++iter)
       
   154         {
       
   155             (*iter)->driveChanged(operation, di);
       
   156         }
       
   157     }
       
   158     break;
       
   159 
       
   160     default:
       
   161         ELOG1(EJavaFile, "Unknown message received %d", aMessage.getMessageId());
       
   162     }
       
   163 }
       
   164 
       
   165 } // end of namespace fileutils
       
   166 } // end of namespace java