javamanager/javacaptain/extensionplugins/boot/src/booteventprovider.cpp
branchRCL_3
changeset 19 04becd199f91
child 25 9ac0a0a7da70
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:  BootEventProvider
       
    15 *
       
    16 */
       
    17 
       
    18 #include <errno.h>
       
    19 #include <fcntl.h>
       
    20 #include <unistd.h>
       
    21 #include <string.h>
       
    22 
       
    23 #include "logger.h"
       
    24 
       
    25 #include "coreinterface.h"
       
    26 #include "eventconsumerinterface.h"
       
    27 
       
    28 #include "booteventprovider.h"
       
    29 #include "booteventprovidermessages.h"
       
    30 
       
    31 namespace java
       
    32 {
       
    33 namespace captain
       
    34 {
       
    35 
       
    36 // In S60 working directory is initalized to C:\private\<UID> by OpenC
       
    37 const char IAD_BOOT_FLAG[]   = "iadboot.dat";
       
    38 const char FIRST_BOOT_FLAG[] = "firstbootdone.dat";
       
    39 
       
    40 BootEventProvider::BootEventProvider() : mCore(0)
       
    41 {
       
    42     JELOG2(EJavaCaptain);
       
    43 }
       
    44 
       
    45 BootEventProvider::~BootEventProvider()
       
    46 {
       
    47     JELOG2(EJavaCaptain);
       
    48 }
       
    49 
       
    50 void BootEventProvider::startPlugin(CoreInterface* aCore)
       
    51 {
       
    52     JELOG2(EJavaCaptain);
       
    53     mCore = aCore;
       
    54 
       
    55     int bootType = NORMAL_BOOT_C;
       
    56     if (isIadBoot())
       
    57     {
       
    58         bootType = IAD_BOOT_C;
       
    59         setBootFlag(IAD_BOOT_FLAG, false);
       
    60     }
       
    61     else if (isFirstDeviceBoot())
       
    62     {
       
    63         bootType = FIRST_DEVICE_BOOT_C;
       
    64         setBootFlag(FIRST_BOOT_FLAG, true);
       
    65     }
       
    66 
       
    67     dispatchEvent(BOOT_EVENT_PROVIDER, bootType);
       
    68 }
       
    69 
       
    70 void BootEventProvider::stopPlugin()
       
    71 {
       
    72     JELOG2(EJavaCaptain);
       
    73     mCore = 0;
       
    74 }
       
    75 
       
    76 void BootEventProvider::dispatchEvent(const std::string& aEvent, int aType)
       
    77 {
       
    78     LOG2(EJavaCaptain, EInfo, "BootEventProvider dispatching event=%s type=%d", aEvent.c_str(), aType);
       
    79 
       
    80     CommsMessage eventMsg;
       
    81     setBootMessageParams(eventMsg, aType);
       
    82     mCore->getEventDispatcher()->event(aEvent, eventMsg);
       
    83 }
       
    84 
       
    85 bool BootEventProvider::isFirstDeviceBoot() const
       
    86 {
       
    87     JELOG2(EJavaCaptain);
       
    88     bool firstBoot = !fileExists(FIRST_BOOT_FLAG);
       
    89     return firstBoot;
       
    90 }
       
    91 
       
    92 bool BootEventProvider::isIadBoot() const
       
    93 {
       
    94     JELOG2(EJavaCaptain);
       
    95     bool iadBoot = fileExists(IAD_BOOT_FLAG);
       
    96     return iadBoot;
       
    97 }
       
    98 
       
    99 void BootEventProvider::setBootFlag(const std::string& aPath, bool aCreate) const
       
   100 {
       
   101     JELOG2(EJavaCaptain);
       
   102     if (aCreate)
       
   103     {
       
   104         createFile(aPath);
       
   105     }
       
   106     else
       
   107     {
       
   108         removeFile(aPath);
       
   109     }
       
   110 }
       
   111 
       
   112 bool BootEventProvider::fileExists(const std::string& aPath) const
       
   113 {
       
   114     JELOG2(EJavaCaptain);
       
   115 
       
   116     bool exists = false;
       
   117     struct stat fileStatBuf;
       
   118     if (stat(aPath.c_str(), &fileStatBuf) == 0)
       
   119     {
       
   120         exists = true;
       
   121     }
       
   122     return exists;
       
   123 }
       
   124 void BootEventProvider::createFile(const std::string& aPath) const
       
   125 {
       
   126     JELOG2(EJavaCaptain);
       
   127     int fd = open(aPath.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
       
   128     if (fd < 0)
       
   129     {
       
   130         ELOG1(EJavaCaptain, "createFile() failed: %s", strerror(errno));
       
   131     }
       
   132     else
       
   133     {
       
   134         close(fd);
       
   135     }
       
   136 }
       
   137 
       
   138 void BootEventProvider::removeFile(const std::string& aPath) const
       
   139 {
       
   140     JELOG2(EJavaCaptain);
       
   141     int rc = unlink(aPath.c_str());
       
   142     if (rc < 0)
       
   143     {
       
   144         ELOG1(EJavaCaptain, "removeFile() failed: %s", strerror(errno));
       
   145     }
       
   146 }
       
   147 
       
   148 } // namespace captain
       
   149 } // namespace java
       
   150