Example #1
0
void CVkProto::OnReceiveChatInfo(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq)
{
	debugLogA("CVkProto::OnReceiveChatInfo %d", reply->resultCode);
	if (reply->resultCode != 200)
		return;

	JSONROOT pRoot;
	JSONNODE *pResponse = CheckJsonResponse(pReq, reply, pRoot);
	if (pResponse == NULL)
		return;

	CVkChatInfo *cc = (CVkChatInfo*)pReq->pUserInfo;
	if (m_chats.indexOf(cc) == -1)
		return;

	JSONNODE *info = json_get(pResponse, "info");
	if (info != NULL) {
		ptrT tszTitle(json_as_string(json_get(info, "title")));
		if (lstrcmp(tszTitle, cc->m_tszTopic)) {
			cc->m_tszTopic = mir_tstrdup(tszTitle);
			setTString(cc->m_hContact, "Nick", tszTitle);

			GCDEST gcd = { m_szModuleName, cc->m_tszId, GC_EVENT_CHANGESESSIONAME };
			GCEVENT gce = { sizeof(GCEVENT), &gcd };
			gce.ptszText = tszTitle;
			CallServiceSync(MS_GC_EVENT, 0, (LPARAM)&gce);
		}
		
		cc->m_admin_id = json_as_int(json_get(info, "admin_id"));
	}

	JSONNODE *users = json_as_array(json_get(pResponse, "users"));
	if (users != NULL) {
		for (int i = 0; i < cc->m_users.getCount(); i++)
			cc->m_users[i].m_bDel = true;

		for (int i = 0;; i++) {
			JSONNODE *pUser = json_at(users, i);
			if (pUser == NULL)
				break;

			int uid = json_as_int(json_get(pUser, "uid"));
			TCHAR tszId[20];
			_itot(uid, tszId, 10);

			bool bNew;
			CVkChatUser *cu = cc->m_users.find((CVkChatUser*)&uid);
			if (cu == NULL) {
				cc->m_users.insert(cu = new CVkChatUser(uid));
				bNew = true;
			}
			else bNew = cu->m_bUnknown;
			cu->m_bDel = false;

			ptrT fName(json_as_string(json_get(pUser, "first_name")));
			ptrT lName(json_as_string(json_get(pUser, "last_name")));
			CMString tszNick = CMString(fName).Trim() + _T(" ") + CMString(lName).Trim();
			cu->m_tszNick = mir_tstrdup(tszNick);
			cu->m_bUnknown = false;
			
			if (bNew) {
				GCDEST gcd = { m_szModuleName, cc->m_tszId, GC_EVENT_JOIN };
				GCEVENT gce = { sizeof(GCEVENT), &gcd };
				gce.bIsMe = uid == m_myUserId;
				gce.ptszUID = tszId;
				gce.ptszNick = tszNick;
				gce.ptszStatus = TranslateTS(sttStatuses[uid == cc->m_admin_id]);
				gce.dwItemData = (INT_PTR)cu;
				CallServiceSync(MS_GC_EVENT, 0, (LPARAM)&gce);
			}
		}

		for (int i = cc->m_users.getCount() - 1; i >= 0; i--) {
			CVkChatUser &cu = cc->m_users[i];
			if (!cu.m_bDel)
				continue;

			TCHAR tszId[20];
			_itot(cu.m_uid, tszId, 10);

			GCDEST gcd = { m_szModuleName, cc->m_tszId, GC_EVENT_PART };
			GCEVENT gce = { sizeof(GCEVENT), &gcd };
			gce.ptszUID = tszId;
			CallServiceSync(MS_GC_EVENT, 0, (LPARAM)&gce);

			cc->m_users.remove(i);
		}
	}

	JSONNODE *msgs = json_as_array(json_get(pResponse, "msgs"));
	if (msgs != NULL) {
		for (int i = 1;; i++) {
			JSONNODE *pMsg = json_at(msgs, i);
			if (pMsg == NULL)
				break;

			AppendChatMessage(cc->m_chatid, pMsg, true);
		}
		cc->m_bHistoryRead = true;
	}

	for (int j = 0; j < cc->m_msgs.getCount(); j++) {
		CVkChatMessage &p = cc->m_msgs[j];
		AppendChatMessage(cc, p.m_mid, p.m_uid, p.m_date, p.m_tszBody, p.m_bHistory);
	}
	cc->m_msgs.destroy();
}
int main(){
	Label lTitle(20);
	lTitle.setValue("    Student Form");
	lTitle.setBorder(BorderType::Double);
	lTitle.setForeground(Color::Purple);
	Label lName(10);
	lName.setValue("Name:");
	lName.setBorder(BorderType::Single);
	Label lAddress(10);
	lAddress.setValue("Address:");
	lAddress.setBorder(BorderType::Single);
	Label lCountry(18);
	lCountry.setValue("Counrty:");
	lCountry.setBorder(BorderType::Single);
	Label lSex(10);
	lSex.setValue("Gender:");
	lSex.setBorder(BorderType::Single);
	Label lInterests(10);
	lInterests.setValue("Interests:");
	lInterests.setBorder(BorderType::Single);
	Label lAge(15);
	lAge.setValue("Age:");
	lAge.setBorder(BorderType::Single);
	TextBox tName(30);
	tName.setValue("Sherlock Holmes");
	tName.setBorder(BorderType::Double);
	TextBox tAddress(30);
	tAddress.setValue("221B Baker Street, London");
	tAddress.setBorder(BorderType::Double);
	Combox cCountry(18, { "Israel", "Great Britain", "United States" });
	cCountry.setSelectedIndex(1);
	cCountry.setBorder(BorderType::Double);
	RadioList rSex(2, 10, { "Male", "Female" });
	rSex.setBorder(BorderType::Double);
	CheckList clInterests(3, 10, { "Sports", "Books", "Movies" });
	clInterests.selectIndex(1);
	clInterests.selectIndex(2);
	auto v = clInterests.getSelectedIndices();
	cCountry.getSelectedIndex();
	clInterests.setBorder(BorderType::Double);
	NumericBox nAge(15, 18, 120);
	nAge.setValue(23);
	nAge.setBorder(BorderType::Double);
	Button bSubmit(13);
	bSubmit.setValue("   Submit");
	bSubmit.setBorder(BorderType::Double);
	Button bExit(13);
	bExit.setValue("    Exit");
	bExit.setBorder(BorderType::Double);
	Messagebox msg(10, 35);
	Panel main(26,75);
	main.setBackground(Color::Cyan);
	main.setForeground(Color::White);
	main.setBorder(BorderType::Double);
	main.addControl(lTitle, (main.getWidth() - lTitle.getWidth()) / 2, 2);
	main.addControl(lName, 13, 6);
	main.addControl(lAddress, 13, 10);
	main.addControl(lCountry, 3, 14);
	main.addControl(lSex, 26, 14);
	main.addControl(lInterests, 41, 14);
	main.addControl(lAge, 57, 14);
	main.addControl(tName, 27, 6);
	main.addControl(tAddress, 27, 10);
	main.addControl(cCountry, 3, 18);
	main.addControl(rSex, 26, 18);
	main.addControl(clInterests, 41, 18);
	main.addControl(nAge, 57, 18);
	main.addControl(bSubmit, (main.getWidth()*1/4) , 24);
	main.addControl(bExit, (main.getWidth() * 3 / 5), 24);
	main.addControl(msg, (main.getWidth()-msg.getWidth())/2, 10);
	bSubmit.addListener(e, &msg);
	Control::setFocus(tName);
	auto engine = EventEngine::getEngine();
	auto eStop = [&](Control* c) {engine.stop(); };
	bExit.addListener(eStop, nullptr);
	engine.run(main);
	return 0;
}