<commit_before>/**
 * Appcelerator Titanium Mobile
 * Copyright (c) 2009-2012 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the Apache Public License
 * Please see the LICENSE included with this distribution for details.
 */

#include "NativeProxyObject.h"

#include "NativeLoggerObject.h"
#include "NativeMessageStrings.h"
#include "TiEvent.h"

const char* NativeProxyObject::tetACCEPTED = "accepted";
const char* NativeProxyObject::tetCHANGE = "change";
const char* NativeProxyObject::tetBEFORELOAD = "beforeload";
const char* NativeProxyObject::tetCLICK = "click";
const char* NativeProxyObject::tetCONNECTED = "connected";
const char* NativeProxyObject::tetERROR = "error";
const char* NativeProxyObject::tetLOAD = "load";
const char* NativeProxyObject::tetREADYSTATECHANGE = "readystatechange";
const char* NativeProxyObject::tetSTARTED = "started";
const char* NativeProxyObject::tetDATA = "data";


NativeProxyObject::NativeProxyObject(TiObject* tiObject)
    : NativeObject(tiObject)
{
}

NativeProxyObject::~NativeProxyObject()
{
}

int NativeProxyObject::fireEvent(const char* name, const TiObject* event) const
{
    EventPairSmartPtr ep = events_.value(name);
    if (ep.get() != NULL && ep->isValid())
    {
        if (event != NULL)
        {
            ep->container()->fireEvent(event);
        }
        else
        {
            ep->container()->fireEvent();
        }
        return NATIVE_ERROR_OK;
    }
    N_WARNING(Native::Msg::Unsupported_event_name_ << name);
    return NATIVE_ERROR_NOTSUPPORTED;
}

int NativeProxyObject::setEventHandler(const char* eventName, TiEvent* event)
{
    // TODO: support custom events
    if (events_.contains(eventName))
    {
        event->setId(getNextEventId());
        events_[eventName]->container()->addListener(event);
        return NATIVE_ERROR_OK;
    }
    return NATIVE_ERROR_NOTSUPPORTED;
}

int NativeProxyObject::removeEventHandler(const char* eventName, int eventId)
{
    if (events_.contains(eventName))
    {
        events_[eventName]->container()->removeListener(eventId);
        return NATIVE_ERROR_OK;
    }
    return NATIVE_ERROR_NOTSUPPORTED;
}

int NativeProxyObject::getNextEventId()
{
    static int s_nextEventId = 1;

    // Account for overflow.
    if (s_nextEventId < 1)
    {
        // This event id must start at 1 because 0 is reserved. Since
        // V8 will always cast a value of undefined to zero.
        s_nextEventId = 1;
    }
    return s_nextEventId++;<commit_msg>Support firing custom events.<commit_after>/**
 * Appcelerator Titanium Mobile
 * Copyright (c) 2009-2012 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the Apache Public License
 * Please see the LICENSE included with this distribution for details.
 */

#include "NativeProxyObject.h"

#include "NativeLoggerObject.h"
#include "NativeMessageStrings.h"
#include "TiEvent.h"
#include "TitaniumRuntime.h"

const char* NativeProxyObject::tetACCEPTED = "accepted";
const char* NativeProxyObject::tetCHANGE = "change";
const char* NativeProxyObject::tetBEFORELOAD = "beforeload";
const char* NativeProxyObject::tetCLICK = "click";
const char* NativeProxyObject::tetCONNECTED = "connected";
const char* NativeProxyObject::tetERROR = "error";
const char* NativeProxyObject::tetLOAD = "load";
const char* NativeProxyObject::tetREADYSTATECHANGE = "readystatechange";
const char* NativeProxyObject::tetSTARTED = "started";
const char* NativeProxyObject::tetDATA = "data";


NativeProxyObject::NativeProxyObject(TiObject* tiObject)
    : NativeObject(tiObject)
{
}

NativeProxyObject::~NativeProxyObject()
{
}

int NativeProxyObject::fireEvent(const char* name, const TiObject* event) const
{
    EventPairSmartPtr ep = events_.value(name);
    if (ep.get() != NULL && ep->container())
    {
        if (event != NULL)
        {
            ep->container()->fireEvent(event);
        }
        else
        {
            ep->container()->fireEvent();
        }
        return NATIVE_ERROR_OK;
    }
    N_WARNING(Native::Msg::Unsupported_event_name_ << name);
    return NATIVE_ERROR_NOTSUPPORTED;
}

int NativeProxyObject::setEventHandler(const char* eventName, TiEvent* event)
{
    TiEventContainer* container;
    if (events_.contains(eventName)) {
        container = events_[eventName]->container();
    } else {
        TiEventContainerFactory* factory = TitaniumRuntime::instance()->objectFactory()->getEventContainerFactory();
        container = factory->createEventContainer();
        events_.insert(eventName, EventPairSmartPtr(container, NULL));
    }

    event->setId(getNextEventId());
    container->addListener(event);
    return NATIVE_ERROR_OK;
}

int NativeProxyObject::removeEventHandler(const char* eventName, int eventId)
{
    if (events_.contains(eventName))
    {
        events_[eventName]->container()->removeListener(eventId);
        return NATIVE_ERROR_OK;
    }
    return NATIVE_ERROR_NOTSUPPORTED;
}

int NativeProxyObject::getNextEventId()
{
    static int s_nextEventId = 1;

    // Account for overflow.
    if (s_nextEventId < 1)
    {
        // This event id must start at 1 because 0 is reserved. Since
        // V8 will always cast a value of undefined to zero.
        s_nextEventId = 1;
    }
    return s_nextEventId++;
{'fraction_non_alphanumeric': 0.11306485157874835, 'fraction_numerical': 0.005294006428436377, 'mean_word_length': 3.9718045112781954, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4644941', 'n_tokens_mistral': 1580, 'n_tokens_neox': 1535, 'n_words': 371}