int BBMBPS::WaitForEvents() { MUTEX_LOCK(); int status = bbmsp_request_events(0); bps_event_t *event = NULL; bbmsp_event_t *bbmEvent; bbmsp_profile_t *bbmProfile; for (;;) { MUTEX_UNLOCK(); bps_get_event(&event, -1); MUTEX_LOCK(); if (event) { int event_domain = bps_event_get_domain(event); if (event_domain == bbmsp_get_domain()) { int eventCategory = 0; int eventType = 0; bbmsp_event_get(event, &bbmEvent); if (bbmsp_event_get_category(event, &eventCategory) == BBMSP_SUCCESS) { switch (eventCategory) { case BBMSP_REGISTRATION: { if (bbmsp_event_get_type(event, &eventType) == BBMSP_SUCCESS) { switch (eventType) { case BBMSP_SP_EVENT_ACCESS_CHANGED: processAccessCode(bbmsp_event_access_changed_get_access_error_code(bbmEvent)); break; } } break; } case BBMSP_USER_PROFILE: { if (bbmsp_event_get_type(event, &eventType) == BBMSP_SUCCESS) { switch (eventType) { case BBMSP_SP_EVENT_PROFILE_CHANGED: { bbmsp_presence_update_types_t profileUpdateType; bbmsp_event_profile_changed_get_profile(bbmEvent, &bbmProfile); if (bbmsp_event_profile_changed_get_presence_update_type(bbmEvent, &profileUpdateType) == BBMSP_SUCCESS) { if (profileUpdateType == BBMSP_DISPLAY_PICTURE) { bbmsp_image_t *avatar; char *imgData = NULL; char *output = NULL; bbmsp_image_create_empty(&avatar); if (bbmsp_profile_get_display_picture(bbmProfile, avatar) == BBMSP_SUCCESS) { imgData = bbmsp_image_get_data(avatar); int size = bbmsp_image_get_data_size(avatar); output = new char[size*4]; int bufferSize = b64_ntop(reinterpret_cast<unsigned char *>(imgData), bbmsp_image_get_data_size(avatar), output, size*4); output[bufferSize] = 0; m_pParent->NotifyEvent(std::string("self.getDisplayPicture ").append(output)); delete output; } bbmsp_image_destroy(&avatar); } else { processProfileUpdate(bbmEvent); } } break; } } } break; } case BBMSP_CONTACT_LIST: { if (bbmsp_event_get_type(event, &eventType) == BBMSP_SUCCESS) { switch (eventType) { case BBMSP_SP_EVENT_CONTACT_CHANGED: { processContactUpdate(bbmEvent); } } } } } } } else if (event_domain == m_BBMInternalDomain) { int code = bps_event_get_code(event); if (code == INTERNAL_EVENT_REGISTER) { bps_event_payload_t *payload = bps_event_get_payload(event); char *uuid = reinterpret_cast<char *>(payload->data1); bbmsp_register(uuid); delete uuid; } else if (code == INTERNAL_EVENT_CONTACT_EVENTS) { bbmsp_event_contact_list_register_event(); contactEventsEnabled = true; } else if (code == INTERNAL_EVENT_STOP) { break; } } } } return (status == BPS_SUCCESS) ? 0 : 1; }
/** * Called when a new contact event is received from the BBMSP library. */ void handle_contact_event( bbmsp_event_t* bbmsp_event, int event_type ) { trace( "processing contact event" ); bbmsp_result_t result = BBMSP_FAILURE; //Full contact list has been retrieved. if( event_type == BBMSP_SP_EVENT_CONTACT_LIST_FULL ) { if( bbmsp_event_contact_list_register_event() == BBMSP_FAILURE ) { trace( "bbmsp_event_contact_list_register_event() failure" ); } //we got the entire contact list bbmsp_contact_list_t *list; result = bbmsp_event_contact_list_get_full_contact_list( bbmsp_event, &list ); trace( "bbmsp_event_contact_list_get_full_contact_list %i", result ); contact_size = bbmsp_contact_list_get_size( list ); trace( "bbmsp_contact_list_get_size %i", contact_size ); contact_list = (bbmsp_contact_t**) malloc(sizeof(bbmsp_contact_t*) *contact_size); result = bbmsp_contact_list_get_all_contacts( list, contact_list ); trace( "bbmsp_contact_list_get_all_contacts %i", result ); pthread_mutex_lock( &amutex ); contactsCreated = false; //Dispatch event to ActionScript, which then calls getContactList() to retrieve the contact list. FREDispatchStatusEventAsync(context, (const uint8_t *)"contact", (const uint8_t *)"contact_list"); while( !contactsCreated ) { pthread_cond_wait( &acond, &amutex ); } pthread_mutex_unlock( &amutex ); free( contact_list ); } else if( event_type == BBMSP_SP_EVENT_CONTACT_CHANGED ) { //contact was changed bbmsp_contact_t* contact = 0; bbmsp_contact_create(&contact); result = bbmsp_event_contact_changed_get_contact(bbmsp_event, &contact); trace( "bbmsp_event_contact_changed_get_contact %i", result ); bbmsp_presence_update_types_t updateType; if(bbmsp_event_contact_changed_get_presence_update_type(bbmsp_event, &updateType) == BBMSP_SUCCESS) { trace( "updateType %i", updateType ); if( updateType & BBMSP_INSTALL_APP ) { trace( "adding contact" ); pthread_mutex_lock( &amutex ); bbmsp_contact_create( &contactToAdd ); bbmsp_contact_copy( contactToAdd, contact ); contactAdded = false; //Dispatch event to ActionScript, which then calls getAddedContact() to get the added contact. FREDispatchStatusEventAsync(context, (const uint8_t *)"contact", (const uint8_t *)"contact_added"); while( !contactAdded ) { pthread_cond_wait( &acond, &amutex ); } bbmsp_contact_destroy( &contactToAdd ); pthread_mutex_unlock( &amutex ); } if( updateType & BBMSP_DISPLAY_NAME ) { addContactUpdate( contact, BBMSP_DISPLAY_NAME ); FREDispatchStatusEventAsync(context, ( uint8_t* )"contact", (const uint8_t*)"contact_update"); } if( updateType & BBMSP_DISPLAY_PICTURE ) { addContactUpdate( contact, BBMSP_DISPLAY_PICTURE ); FREDispatchStatusEventAsync(context, ( uint8_t* )"contact", (const uint8_t*)"contact_update"); } if( updateType & BBMSP_PERSONAL_MESSAGE ) { addContactUpdate( contact, BBMSP_PERSONAL_MESSAGE ); FREDispatchStatusEventAsync(context, ( uint8_t* )"contact", (const uint8_t*)"contact_update"); } if( updateType & BBMSP_STATUS ) { addContactUpdate( contact, BBMSP_STATUS ); FREDispatchStatusEventAsync(context, ( uint8_t* )"contact", (const uint8_t*)"contact_update"); } if( updateType & BBMSP_UNINSTALL_APP ) { addContactUpdate( contact, BBMSP_UNINSTALL_APP ); FREDispatchStatusEventAsync(context, ( uint8_t* )"contact", (const uint8_t*)"contact_update"); } } } }