result VKUMessagesListItemProvider::ProcessChatUsers(const JsonObject * chatJson) {
	AppLog("VKUMessagesListItemProvider::ProcessChatUsers");
	result r = E_SUCCESS;

	JsonArray *chatUsers;
	JsonParseUtils::GetArray(chatJson, L"users", chatUsers);

	_pUserIdAvatarMap = new HashMap(SingleObjectDeleter);
	_pUserIdAvatarMap->Construct(chatUsers->GetCount(), 1);

	for (int i=0; i<chatUsers->GetCount(); i++) {
		JsonObject *userJson;
		JsonParseUtils::GetObject(chatUsers, i, userJson);

		int userId;
		String avatarUrl;

		JsonParseUtils::GetInteger(*userJson, L"id", userId);
		JsonParseUtils::GetString(*userJson, L"photo_100", avatarUrl);

		AppLog("Adding avatar for %d : %ls", userId, avatarUrl.GetPointer());
		_pUserIdAvatarMap->Add(new Integer(userId), new String(avatarUrl));
	}

	return r;
}
void VKUMessagesListItemProvider::OnResponseN(RequestId requestId, JsonObject *object) {
	result r = E_SUCCESS;
	String cacheFile(VKUApp::GetInstance()->GetCacheDir() + "dialog" + Integer::ToString(_peerId) + ".json");
	JsonObject *response;
	JsonArray *items;

	AppLog("processing messages json");

	r = JsonParseUtils::GetObject(object, L"response", response);
	TryCatch(r == E_SUCCESS, , "failed get response from object");

	r = JsonParseUtils::GetArray(response, L"items", items);
	TryCatch(r == E_SUCCESS, , "failed get items from response");


	if(requestId == REQUEST_GET_NEW_MESSAGE) {

		if(_messagesJson == null) {
			AppLog("Assigned %d items", items->GetCount());
			_messagesJson = items->CloneN();
		} else {
			AppLog("added %d items", items->GetCount());
			for(int i = 0; i < items->GetCount(); i++) {
				IJsonValue *value;
				items->GetAt(i, value);
				_messagesJson->InsertAt(static_cast<JsonObject *>(value)->CloneN(), 0);
			}
		}

		_tableView->UpdateTableView();
		TryCatch(GetLastResult() == E_SUCCESS, r = GetLastResult() , "Failed pTableView->UpdateTableView");

		_tableView->RequestRedraw(true);
		TryCatch(GetLastResult() == E_SUCCESS, r = GetLastResult() , "Failed pTableView->RequestRedraw");

		r = _tableView->ScrollToItem(_tableView->GetItemCount() - 1);
		TryCatch(r == E_SUCCESS, , "Failed pTableView->ScrollToItem");

		JsonWriter::Compose(_messagesJson, cacheFile);
	} else if(requestId == REQUEST_UPDATE_HISTORY) {
void GHPlayerController::OnTransactionReadyToRead(Tizen::Base::String apiCode, Tizen::Base::String statusCode, Tizen::Web::Json::IJsonValue* data)
{
	AppLogDebug("[DEBUG] apiCode : %S", apiCode.GetPointer() );
	AppLogDebug("[DEBUG] statusCode : %S", statusCode.GetPointer());

	if(pProgressPopup->IsVisible()) {
		pProgressPopup->SetShowState(false);
	}
	if(apiCode.Equals(PLAYER_LOGIN)) {
		// 정상적으로 결과를 반환했을 때
		if(statusCode != "0") {

			// Save AppRegistry Data(Email, Password)----------
			appReg.put(appReg.email, playerEmail);
			appReg.put(appReg.pwd, playerPwd);
			//------------------------------------------------

			GHSharedAuthData & sharedInstance = GHSharedAuthData::getSharedInstance();
			sharedInstance.setPlayerId(statusCode);

			if(this->currentListener != null) this->currentListener->loginPlayerFinished(statusCode);
		}
		else { // 에러가 발생했을 때
		}


	}
	else if(apiCode.Equals(PLAYER_PLAYERDATA)) {	// PLAYER_PLAYERDATA
			GHPlayer *player;

			// 정상적으로 결과를 반환했을 때
			if(statusCode == "1") {

				JsonObject *pJsonOject 	= static_cast<JsonObject*>(data);

				AppLogDebug("---------------------------2-----------------------");

				String* pkeyId		= new String(L"player_id");
				String* pkeyEmail 	= new String(L"email");
				String* pkeyName 	= new String(L"name");
				String* pkeyImgUrl 	= new String(L"img_url");
				String* pkeyIsFriend= new String(L"is_friend");

				String  sId 		= getStringByKey(pJsonOject, pkeyId);
				String  sEmail 		= getStringByKey(pJsonOject, pkeyEmail);
				String  sName		= getStringByKey(pJsonOject, pkeyName);
				String  sImgUrl 	= getStringByKey(pJsonOject, pkeyImgUrl);
				bool  	bIsFriend 	= getBoolByKey(pJsonOject, pkeyIsFriend);

				AppLogDebug("--------------------------------------------------");

				player = new GHPlayer(sId, sEmail, sName, sImgUrl);
				player->setIsFriend(bIsFriend);

				// KEY NAME DELETE
				delete pkeyId;		delete pkeyEmail;
				delete pkeyName;	delete pkeyImgUrl;
				delete pkeyIsFriend;

			}
			else { // 에러가 발생했을 때
				player = null;
			}

			if(this->currentListener != null) this->currentListener->loadPlayerDataFinished(player);
	}
	else if(apiCode.Equals(PLAYER_GAMELIST))
	{
		ArrayList *gameList;

		// 정상적으로 결과를 반환했을 때
		if(statusCode == "1") {
			JsonArray* 	pJsonArray 	= static_cast<JsonArray*>(data);
			int 		arrNum 		= pJsonArray->GetCount();
			gameList = new ArrayList();

			// KEY NAME
			String* pKeyGameId 		= new String(L"game_id");
			String* pKeyGameTitle	= new String(L"title");
			String* pkeyGameImgUrl	= new String(L"img_url");
			String* pKeyGameDesc	= new String(L"description");

			AppLogDebug("[DEBUG] arrNum : %d", arrNum );

			for(int i=0; i<arrNum; i++) {
				JsonObject *pJsonOject 	= getJsonObjectByIndex(pJsonArray, i);

				// 데이터 파싱
				String  sGameId			= getStringByKey(pJsonOject, pKeyGameId);
				String  sGameTitle		= getStringByKey(pJsonOject, pKeyGameTitle);
				String  sGameImgUrl 	= getStringByKey(pJsonOject, pkeyGameImgUrl);
				String  sGameDesc	 	= getStringByKey(pJsonOject, pKeyGameDesc);

				// 리스트에 추가
				gameList->Add( new GHGame(sGameId, "", sGameTitle, sGameDesc, sGameImgUrl, 0, 0, 0, false, false) );
			}

			// KEY NAME DELETE
			delete pKeyGameId;		delete pKeyGameTitle;	 delete pkeyGameImgUrl;
		}
		else { // 에러가 발생했을 때
			gameList = null;
		}

		if(this->currentListener != null) this->currentListener->loadPlayerGamesFinished(gameList);


	}
	else if(apiCode.Equals(PLAYER_SEARCHFRIEND))
	{
		ArrayList *friendList;

		if(statusCode == "1") {
			JsonObject *pJsonOject 	= static_cast<JsonObject*>(data);
			friendList = new ArrayList();

			// KEY NAME
			String* pKeyPlayerId	= new String(L"player_id");
			String* pKeyPlayerEmail	= new String(L"email");
			String* pkeyPlayerName	= new String(L"name");
			String* pkeyPlayerImgUrl= new String(L"img_url");

			// 데이터 파싱
			String  sPlayerId		= getStringByKey(pJsonOject, pKeyPlayerId);
			String  sPlayerEmail	= getStringByKey(pJsonOject, pKeyPlayerEmail);
			String  sPlayerName		= getStringByKey(pJsonOject, pkeyPlayerName);
			String  sPlayerImgUrl 	= getStringByKey(pJsonOject, pkeyPlayerImgUrl);

			// 리스트에 추가
			friendList->Add( new GHPlayer(sPlayerId, sPlayerEmail, sPlayerName, sPlayerImgUrl) );

			// KEY NAME DELETE
			delete pKeyPlayerId;		delete pKeyPlayerEmail;
			delete pkeyPlayerName;		delete pkeyPlayerImgUrl;
		}
		else { // 에러가 발생했을 때
			friendList = null;
		}

		if(this->currentListener != null) this->currentListener->searchFriendFinished(friendList);
	}
	else if(apiCode.Equals(PLAYER_ADDFRIEND))
	{
		if(this->currentListener != null) this->currentListener->addFriendFinished(statusCode);
	}
	else if(apiCode.Equals(PLAYER_LOADFRIENDS))	// 친구 목록 불러오기
	{
		ArrayList *friendList;

		if(statusCode == "1") {
				JsonArray* 	pJsonArray 	= static_cast<JsonArray*>(data);
				int 		arrNum 		= pJsonArray->GetCount();
			friendList = new ArrayList();

			// KEY NAME
				String* pKeyPlayer		= new String(L"player_id");
				String* pKeyPlayerName	= new String(L"name");
				String* pkeyPlayerImgUrl= new String(L"img_url");

				AppLogDebug("[DEBUG] arrNum : %d", arrNum );

				for(int i=0; i<arrNum; i++) {
					JsonObject *pJsonOject 	= getJsonObjectByIndex(pJsonArray, i);

					// 데이터 파싱
					String  sPlayerId		= getStringByKey(pJsonOject, pKeyPlayer);
					String  sPlayerName		= getStringByKey(pJsonOject, pKeyPlayerName);
					String  sPlayerImgUrl 	= getStringByKey(pJsonOject, pkeyPlayerImgUrl);

					// 리스트에 추가
					friendList->Add( new GHPlayer(sPlayerId, sPlayerName, sPlayerImgUrl) );
				}

				// KEY NAME DELETE
				delete pKeyPlayer;		delete pKeyPlayerName;	 delete pkeyPlayerImgUrl;
		}
		else { // 에러가 발생했을 때
			friendList = null;
		}

		if(this->currentListener != null) this->currentListener->loadPlayerFriendsFinished(friendList);

	}
	else //PLAYER_LOGIN ,PLAYER_GAMEJOIN
	{
//		if(this->currentListener != null) this->currentListener->doPlayerFinished(statusCode);
	}
}
//Recursively traverse IJsonValue obtained by parser (using ParseN)
void
ProjectGiraffeTab1::TraverseFunction(IJsonValue* pValue)
{
	TryReturnVoid(_tableView, "tableView is null");

	TryReturnVoid(pValue, "input jsonvalue pointer is null");

	User *dummyUser = new User();
	dummyUser->setUsername(L"CS210 Student");

	Graffiti *lastCreatedGraffiti = NULL;

	switch (pValue->GetType())
	{
	case JSON_TYPE_OBJECT:
	{
		JsonObject* pObject = static_cast< JsonObject* >(pValue);
		IMapEnumeratorT< const String*, IJsonValue* >* pMapEnum = pObject->GetMapEnumeratorN();
		while (pMapEnum->MoveNext() == E_SUCCESS)
		{
			AppLog("IN JSON OBJECT");
			const String* key = null;
			IJsonValue* value = null;
			pMapEnum->GetKey(key);
			pMapEnum->GetValue(value);
			String* pListKey = new (std::nothrow) String(*key);
			_pJsonKeyList->Add(*pListKey);
			AppLog("Key: %ls", pListKey->GetPointer());

			if(pListKey->Equals("message",true)){
				AppLog("Message received");
				lastCreatedGraffiti = new Graffiti();
				lastCreatedGraffiti->setUser(dummyUser);
				JsonString* pVal = static_cast< JsonString* >(value);
				String* pListValue = new (std::nothrow) String(*pVal);
				lastCreatedGraffiti->setText(String(*pListValue));
				_items->Add(lastCreatedGraffiti);
				AppLog("message : %ls", lastCreatedGraffiti->text().GetPointer());
			} else if (value && pListKey->Equals("latitude", true)) {
				JsonNumber *jsonNum = static_cast<JsonNumber *>(value);
				Double *latValue = static_cast<Double *>(jsonNum);
				if (latValue && lastCreatedGraffiti) {
					lastCreatedGraffiti->setLatitude(latValue->ToFloat());
					AppLog("latitude : %f", lastCreatedGraffiti->longitude());
				}
			} else if (value != NULL && pListKey->Equals("longitude", true)) {
				JsonNumber *jsonNum = static_cast<JsonNumber *>(value);
				Double *lonValue = static_cast<Double *>(jsonNum);
				if (lonValue && lastCreatedGraffiti) {
					lastCreatedGraffiti->setLongitude(lonValue->ToFloat());
					AppLog("longitude : %f", lastCreatedGraffiti->longitude());
				}
			}
		}

		delete pMapEnum;
	}
	break;

	case JSON_TYPE_STRING:
	{
		JsonString* pVal = static_cast< JsonString* >(pValue);
		if (_isArray == 0)
		{
			String* pStr = new (std::nothrow) String(*pVal);
			_pValueList->Add(*pStr);
		}
	}
	break;

	case JSON_TYPE_ARRAY:
	{
		JsonArray* pJsonArray = static_cast< JsonArray* >(pValue);
		pJsonArray->GetCount();
		IEnumeratorT< IJsonValue* >* pEnum = pJsonArray->GetEnumeratorN();
		while (pEnum->MoveNext() == E_SUCCESS)
		{
			IJsonValue* pValue = null;
			pEnum->GetCurrent(pValue);
			if ((pValue->GetType() == JSON_TYPE_STRING) ||
					(pValue->GetType() == JSON_TYPE_NUMBER) ||
					(pValue->GetType() == JSON_TYPE_BOOL)   ||
					(pValue->GetType() == JSON_TYPE_NULL)) {
				_isArray = 1;
			}
			TraverseFunction(pValue);
		}
		delete pEnum;
	}
	break;

	case JSON_TYPE_NUMBER:
	{
		JsonNumber* pVal = static_cast< JsonNumber* >(pValue);
		String* pStr = new (std::nothrow) String((pVal->ToString()));
		if (_isArray == 0)
			_pValueList->Add(*pStr);
	}
	break;

	case JSON_TYPE_BOOL:
	{
		String* pStr = null;
		JsonBool* pVal = static_cast< JsonBool* >(pValue);
		if (*pVal == true)
			pStr = new (std::nothrow) String("true");
		else
			pStr = new (std::nothrow) String("false");
		if (_isArray == 0)
			_pValueList->Add(*pStr);
	}
	break;

	case JSON_TYPE_NULL:
	{
		String* pStr = null;
		pStr = new (std::nothrow) String("null");
		if (_isArray == 0)
			_pValueList->Add(*pStr);
	}
	break;

	default:
		break;
	}
	_tableView->UpdateTableView();
}
ArrayList * VKUMessagesListItemProvider::GetMessageElementsN(const JsonObject *pMessageJson, int itemWidth) {
	AppLog("enter VKUMessagesListItemProvider::GetMessageElementsN");
	result r;

	// general
	ArrayList* pResultArray;

	// body stuff
	String messageText;
	MessageTextElement *pMessageTextElement;

	// attachs stuff
	IJsonValue *attachs;
	JsonArray * pAttachArray;
	int out;
	int emoji = 0;

	pResultArray = new ArrayList(SingleObjectDeleter);
	r = pResultArray->Construct(1);
	TryCatch(r == E_SUCCESS, , "pResultArray->Construct");
	JsonParseUtils::GetInteger(*pMessageJson, L"out", out);

	JsonObject * geoObject;
	r = JsonParseUtils::GetObject(pMessageJson, L"geo", geoObject);

	if (r == E_SUCCESS) {
		AppLog("Message has geo entry, receiving");
		MessageLocationElement * pLocationElement = new MessageLocationElement();
		pLocationElement->Construct(Rectangle(0, 0, 400, 400), geoObject);

		pResultArray->Add(pLocationElement);
	}

	r = JsonParseUtils::GetString(*pMessageJson, L"body", messageText);
	TryCatch(r == E_SUCCESS, , "JsonParseUtils::GetString body");

	JsonParseUtils::GetInteger(*pMessageJson, L"emoji", emoji);

	if (messageText.GetLength() != 0) {
		AppLog("Message has text entry, receiving");
		pMessageTextElement = new MessageTextElement();
		pMessageTextElement->Construct(Rectangle(0, 0, itemWidth-200, 10000));
		pMessageTextElement->SetText(messageText, emoji);

		pResultArray->Add(pMessageTextElement);
	}


	static const String attachConst(L"attachments");
	r = pMessageJson->GetValue(&attachConst, attachs);

	if (r == E_SUCCESS)
		pAttachArray = static_cast<JsonArray *>(attachs);

	for (int i=0; r == E_SUCCESS && i<pAttachArray->GetCount(); i++) {
		AppLog("Message has %d attachments, receiving %d", pAttachArray->GetCount(), i);

		IJsonValue *pAttachValue;
		JsonObject *pAttachObject;
		String attachType;

		MessageElement *pMessageElement;

		pAttachArray->GetAt(i, pAttachValue);

		pAttachObject = static_cast<JsonObject *>(pAttachValue);

		JsonParseUtils::GetString(*pAttachObject, L"type", attachType);

		if (attachType == L"photo") {
			AppLog("Message has photo, receiving");
			String imageUrl;

			IJsonValue *pPhotoValue;
			JsonObject *pPhotoObject;

			Rectangle thumbSize;

			int width = 0, height = 0;

			static const String photoConst(L"photo");

			pAttachObject->GetValue(&photoConst, pPhotoValue);
			pPhotoObject = static_cast<JsonObject *>(pPhotoValue);

			JsonParseUtils::GetString(*pPhotoObject, L"photo_604", imageUrl);
			JsonParseUtils::GetInteger(*pPhotoObject, L"width", width);
			JsonParseUtils::GetInteger(*pPhotoObject, L"height", height);

			if (width != 0 && height != 0) {
				thumbSize = ImageUtils::ScaleTo(320, Rectangle(0, 0, width, height));
			} else {
				thumbSize = Rectangle(0, 0, 320, 240);
			}

			MessagePhotoElement * pPhotoElement = new MessagePhotoElement();
			pPhotoElement->Construct(thumbSize, imageUrl);

			pMessageElement = static_cast<MessageElement *>(pPhotoElement);;

		} else if (attachType == L"video") {
			AppLog("Message has video, receiving");

//			IJsonValue *pVideoValue;
//			JsonObject *pVideoObject;
//
//			static const String videoConst(L"video");
//
//			pAttachObject->GetValue(&videoConst, pVideoValue);
//			pVideoObject = static_cast<JsonObject *>(pVideoValue);

			JsonObject *pVideoObject;
			JsonParseUtils::GetObject(pAttachObject, L"video", pVideoObject);

			MessageVideoElement *pVideoElement = new MessageVideoElement();
			pVideoElement->Construct(Rectangle(0, 0, 320, 240), pVideoObject);

			pMessageElement = static_cast<MessageElement *>(pVideoElement);

		} else if (attachType == L"audio") {
			AppLog("Message has audio, receiving");

			JsonObject *pAudioObject;
			JsonParseUtils::GetObject(pAttachObject, L"audio", pAudioObject);

			MessageAudioElement *pMessageAudioEleemnt = new MessageAudioElement();
			pMessageAudioEleemnt->Construct(Rectangle(0, 0, 520, 240), pAudioObject, out);

			pMessageElement = dynamic_cast<MessageElement *>(pMessageAudioEleemnt);

		} else if (attachType == L"doc") {
			AppLog("Message has doc, receiving");

			JsonObject *pDocObject;
			JsonParseUtils::GetObject(pAttachObject, L"doc", pDocObject);

			MessageDocElement * pDocElement = new MessageDocElement();
			pDocElement->Construct(Rectangle(0, 0, 520, 90), pDocObject, out);

			pMessageElement = static_cast<MessageElement * >(pDocElement);
		} else if (attachType == L"wall") {
			AppLog("Message has wall, receiving");

			JsonObject *pWallObject;
			JsonParseUtils::GetObject(pAttachObject, L"wall", pWallObject);

			MessageWallElement * pWallElement = new MessageWallElement();
			pWallElement->Construct(Rectangle(0, 0, 320, 240), pWallObject, out);

			pMessageElement = dynamic_cast<MessageElement *>(pWallElement);
		}

		pResultArray->Add(pMessageElement);
	}

	JsonArray *forwardedMessages;
	r = JsonParseUtils::GetArray(pMessageJson, L"fwd_messages", forwardedMessages);
	AppLog("Message has forwardedMessages?");
	for (int i=0; r == E_SUCCESS && i<forwardedMessages->GetCount(); i++) {
		AppLog("Message has forwardedMessages, receiving %d", i);

		JsonObject *fwdMessage;

		JsonParseUtils::GetObject(forwardedMessages, i, fwdMessage);

		MessageForwardedElement *pForwardedElement = new MessageForwardedElement();
		pForwardedElement->Construct(Rectangle(0, 0, 500, 40000), fwdMessage, out);

		MessageElement *pMessageElement;
		pMessageElement = static_cast<MessageForwardedElement *>(pForwardedElement);

		pResultArray->Add(pMessageElement);
	}

	AppLog("Message has done");

	return pResultArray;

CATCH:
	AppLogException("VKUMessagesListItemProvider::GetMessageElementsN is failed. %s", GetErrorMessage(r));
	return pResultArray;
}