From 7aa194b9f14d8f1520ee248b6dbc643bc57aefa9 Mon Sep 17 00:00:00 2001 From: Sergey Bolhovskoy Date: Fri, 6 Feb 2015 07:52:46 +0000 Subject: VKontakte: code cleanup and optimization git-svn-id: http://svn.miranda-ng.org/main/trunk@12016 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/VKontakte/src/misc.cpp | 47 +++++++++++++++++++++----------- protocols/VKontakte/src/vk.h | 4 ++- protocols/VKontakte/src/vk_chats.cpp | 3 +- protocols/VKontakte/src/vk_feed.cpp | 3 +- protocols/VKontakte/src/vk_history.cpp | 9 +++--- protocols/VKontakte/src/vk_proto.cpp | 7 ++--- protocols/VKontakte/src/vk_thread.cpp | 50 ++++++++++++++++------------------ 7 files changed, 68 insertions(+), 55 deletions(-) diff --git a/protocols/VKontakte/src/misc.cpp b/protocols/VKontakte/src/misc.cpp index 414d9fc65a..4d3560d128 100644 --- a/protocols/VKontakte/src/misc.cpp +++ b/protocols/VKontakte/src/misc.cpp @@ -19,6 +19,24 @@ along with this program. If not, see . static char* szImageTypes[] = { "photo_2560", "photo_1280", "photo_807", "photo_604", "photo_256", "photo_130", "photo_128", "photo_75", "photo_64" }; +bool IsEmpty(TCHAR *str) +{ + if (str == NULL) + return true; + if (str[0] == 0) + return true; + return false; +} + +bool IsEmpty(char *str) +{ + if (str == NULL) + return true; + if (str[0] == 0) + return true; + return false; +} + CMString json_as_CMString(JSONNODE* pNode) { ptrT pString(json_as_string(pNode)); @@ -345,8 +363,8 @@ bool CVkProto::CheckJsonResult(AsyncHttpRequest *pReq, JSONNODE *pNode) pReq->m_iRetry--; } else { - CMString msg, msgformat = TranslateT("Error %d. Data will not be sent or received."); - msg.AppendFormat(msgformat, iErrorCode); + CMString msg; + msg.AppendFormat(TranslateT("Error %d. Data will not be sent or received."), iErrorCode); MsgPopup(NULL, msg.GetBuffer(), TranslateT("Error"), true); debugLogA("CVkProto::CheckJsonResult SendError"); } @@ -518,26 +536,23 @@ void __cdecl CVkProto::DBAddAuthRequestThread(void *p) if (hContact == NULL || hContact == INVALID_CONTACT_ID || !IsOnline()) return; - for (int i = 0; i < MAX_RETRIES && CMString(ptrT(db_get_tsa(hContact, m_szModuleName, "Nick"))).IsEmpty(); i++) { + for (int i = 0; i < MAX_RETRIES && IsEmpty(ptrT(db_get_tsa(hContact, m_szModuleName, "Nick"))); i++) { Sleep(1500); if (!IsOnline()) return; } + DBAddAuthRequest(hContact); } void CVkProto::DBAddAuthRequest(const MCONTACT hContact) { debugLogA("CVkProto::DBAddAuthRequest"); - - CMString tszNick = ptrT(db_get_tsa(hContact, m_szModuleName, "Nick")); - CMString tszFirstName = ptrT(db_get_tsa(hContact, m_szModuleName, "FirstName")); - CMString tszLastName = ptrT(db_get_tsa(hContact, m_szModuleName, "LastName")); - - ptrA szNick(mir_utf8encodeT(tszNick.GetBuffer())); - ptrA szFirstName(mir_utf8encodeT(tszFirstName.GetBuffer())); - ptrA szLastName(mir_utf8encodeT(tszLastName.GetBuffer())); + + ptrA szNick(mir_utf8encodeT(ptrT(db_get_tsa(hContact, m_szModuleName, "Nick")))); + ptrA szFirstName(mir_utf8encodeT(ptrT(db_get_tsa(hContact, m_szModuleName, "FirstName")))); + ptrA szLastName(mir_utf8encodeT(ptrT(db_get_tsa(hContact, m_szModuleName, "LastName")))); //blob is: uin(DWORD), hContact(DWORD), nick(ASCIIZ), first(ASCIIZ), last(ASCIIZ), email(ASCIIZ), reason(ASCIIZ) //blob is: 0(DWORD), hContact(DWORD), nick(ASCIIZ), first(ASCIIZ), last(ASCIIZ), ""(ASCIIZ), ""(ASCIIZ) @@ -570,7 +585,7 @@ void CVkProto::DBAddAuthRequest(const MCONTACT hContact) *pCurBlob = '\0'; //reason db_event_add(NULL, &dbei); - debugLogA("CVkProto::DBAddAuthRequest %s", tszNick.IsEmpty() ? "" : szNick); + debugLogA("CVkProto::DBAddAuthRequest %s", szNick ? szNick : ""); } MCONTACT CVkProto::MContactFromDbEvent(MEVENT hDbEvent) @@ -1011,16 +1026,16 @@ CMString CVkProto::GetAttachmentDescr(JSONNODE *pAttachments, BBCSupport iBBC) ptrT ptszUrl(json_as_string(json_get(pLink, "url"))); ptrT ptszTitle(json_as_string(json_get(pLink, "title"))); ptrT ptszDescription(json_as_string(json_get(pLink, "description"))); - CMString tszImage(json_as_CMString(json_get(pLink, "image_src"))); + ptrT ptszImage(json_as_string(json_get(pLink, "image_src"))); res.AppendFormat(_T("%s: %s"), SetBBCString(TranslateT("Link"), iBBC, vkbbcB).GetBuffer(), SetBBCString(ptszTitle, iBBC, vkbbcUrl, ptszUrl).GetBuffer()); - if (!tszImage.IsEmpty()) + if (!IsEmpty(ptszImage)) if (m_iIMGBBCSupport) - res.AppendFormat(_T("\n\t%s: [img]%s[/img]"), TranslateT("Image"), tszImage.GetBuffer()); + res.AppendFormat(_T("\n\t%s: [img]%s[/img]"), TranslateT("Image"), ptszImage); else - res.AppendFormat(_T("\n\t%s: %s"), TranslateT("Image"), tszImage.GetBuffer()); + res.AppendFormat(_T("\n\t%s: %s"), TranslateT("Image"), ptszImage); if (ptszDescription) res.AppendFormat(_T("\n\t%s"), ptszDescription); diff --git a/protocols/VKontakte/src/vk.h b/protocols/VKontakte/src/vk.h index 8f0a871185..cde70d8614 100644 --- a/protocols/VKontakte/src/vk.h +++ b/protocols/VKontakte/src/vk.h @@ -88,4 +88,6 @@ bool tlstrstr(TCHAR* _s1, TCHAR* _s2); void InitIcons(void); HANDLE GetIconHandle(int iCommand); -char* ExpUrlEncode(const char *szUrl, bool strict = false); \ No newline at end of file +char* ExpUrlEncode(const char *szUrl, bool strict = false); +bool IsEmpty(TCHAR *str); +bool IsEmpty(char *str); \ No newline at end of file diff --git a/protocols/VKontakte/src/vk_chats.cpp b/protocols/VKontakte/src/vk_chats.cpp index 1ec4075495..8a2ddb51ed 100644 --- a/protocols/VKontakte/src/vk_chats.cpp +++ b/protocols/VKontakte/src/vk_chats.cpp @@ -282,7 +282,6 @@ void CVkProto::AppendChatMessage(CVkChatInfo *cc, int uid, int msgTime, LPCTSTR TCHAR tszId[20]; _itot(uid, tszId, 10); - CMString tszBody(ptszBody); GCDEST gcd = { m_szModuleName, cc->m_tszId, GC_EVENT_MESSAGE }; GCEVENT gce = { sizeof(GCEVENT), &gcd }; @@ -291,7 +290,7 @@ void CVkProto::AppendChatMessage(CVkChatInfo *cc, int uid, int msgTime, LPCTSTR gce.time = msgTime; gce.dwFlags = (bIsHistory) ? GCEF_NOTNOTIFY : GCEF_ADDTOLOG; gce.ptszNick = cu->m_tszNick ? mir_tstrdup(cu->m_tszNick) : mir_tstrdup(TranslateT("Unknown")); - gce.ptszText = tszBody.IsEmpty() ? mir_tstrdup(_T("...")) : mir_tstrdup(tszBody.GetBuffer()); + gce.ptszText = IsEmpty((TCHAR *)ptszBody) ? mir_tstrdup(_T("...")) : mir_tstrdup(ptszBody); CallServiceSync(MS_GC_EVENT, 0, (LPARAM)&gce); } diff --git a/protocols/VKontakte/src/vk_feed.cpp b/protocols/VKontakte/src/vk_feed.cpp index c0e651ed77..9240e56dd2 100644 --- a/protocols/VKontakte/src/vk_feed.cpp +++ b/protocols/VKontakte/src/vk_feed.cpp @@ -30,9 +30,8 @@ void CVkProto::AddFeedSpecialUser() if (!hContact) { hContact = FindUser(VK_FEED_USER, true); - CMString tszNick = TranslateT("VKontakte"); - setTString(hContact, "Nick", tszNick.GetBuffer()); + setTString(hContact, "Nick", TranslateT("VKontakte")); CMString tszUrl = _T("https://vk.com/press/Simple.png"); SetAvatarUrl(hContact, tszUrl); ReloadAvatarInfo(hContact); diff --git a/protocols/VKontakte/src/vk_history.cpp b/protocols/VKontakte/src/vk_history.cpp index 09715eac04..f2a6d53f05 100644 --- a/protocols/VKontakte/src/vk_history.cpp +++ b/protocols/VKontakte/src/vk_history.cpp @@ -126,14 +126,15 @@ void CVkProto::GetServerHistory(MCONTACT hContact, int iOffset, int iCount, int if (-1 == userID || userID == VK_FEED_USER) return; - CMStringA code, formatcode = "var iOffset=%d;var iReqCount=%d;var userID=%d;var iTime=%d;var lastMid=%d;" + CMStringA code; + + code.AppendFormat("var iOffset=%d;var iReqCount=%d;var userID=%d;var iTime=%d;var lastMid=%d;" "var Hist=API.messages.getHistory({\"user_id\":userID,\"count\":iReqCount,\"offset\":iOffset});" "var allCount=Hist.count;var ext=Hist.items.length;var index=0;" "while(ext!=0){if(Hist.items[index].date>iTime){if(Hist.items[index].id>lastMid)" "{index=index+1;ext=ext-1;}else ext=0;}else ext=0;};" - "var ret=Hist.items.slice(0,index); return{\"count\":index,\"datetime\":iTime,\"items\":ret,\"once\":%d,\"rcount\":iReqCount};"; - - code.AppendFormat(formatcode, iOffset, iCount, userID, iTime, iLastMsgId, (int)once); + "var ret=Hist.items.slice(0,index); return{\"count\":index,\"datetime\":iTime,\"items\":ret,\"once\":%d,\"rcount\":iReqCount};", + iOffset, iCount, userID, iTime, iLastMsgId, (int)once); Push(new AsyncHttpRequest(this, REQUEST_GET, "/method/execute.json", true, &CVkProto::OnReceiveHistoryMessages) << CHAR_PARAM("code", code) << VER_API)->pUserInfo = new CVkSendMsgParam(hContact, iLastMsgId, iOffset); diff --git a/protocols/VKontakte/src/vk_proto.cpp b/protocols/VKontakte/src/vk_proto.cpp index d8215f969e..49a3736e81 100644 --- a/protocols/VKontakte/src/vk_proto.cpp +++ b/protocols/VKontakte/src/vk_proto.cpp @@ -317,7 +317,7 @@ int CVkProto::OnPreBuildContactMenu(WPARAM hContact, LPARAM) { LONG userID = getDword(hContact, "ID", -1); bool bisFriend = (getByte(hContact, "Auth", -1) == 0); - bool bisBroadcast = !(CMString(ptrT(db_get_tsa(hContact, m_szModuleName, "AudioUrl"))).IsEmpty()); + bool bisBroadcast = !(IsEmpty(ptrT(db_get_tsa(hContact, m_szModuleName, "AudioUrl")))); Menu_ShowItem(g_hContactMenuItems[CMI_VISITPROFILE], !isChatRoom(hContact)); Menu_ShowItem(g_hContactMenuItems[CMI_ADDASFRIEND], !bisFriend && !isChatRoom(hContact) && userID != VK_FEED_USER); Menu_ShowItem(g_hContactMenuItems[CMI_DELETEFRIEND], bisFriend && userID != VK_FEED_USER); @@ -656,12 +656,11 @@ void CVkProto::OnReceiveAuthRequest(NETLIBHTTPREQUEST *reply, AsyncHttpRequest * int iRet = json_as_int(pResponse); setByte(param->hContact, "Auth", 0); if (iRet == 2) { - CMString msg, - msgformat = TranslateT("User %s added as friend"), + CMString msg, tszNick = ptrT(db_get_tsa(param->hContact, m_szModuleName, "Nick")); if (tszNick.IsEmpty()) tszNick = TranslateT("(Unknown contact)"); - msg.AppendFormat(msgformat, tszNick.GetBuffer()); + msg.AppendFormat(TranslateT("User %s added as friend"), tszNick.GetBuffer()); MsgPopup(param->hContact, msg.GetBuffer(), tszNick.GetBuffer()); } } diff --git a/protocols/VKontakte/src/vk_thread.cpp b/protocols/VKontakte/src/vk_thread.cpp index d088d03b19..a34e83d07e 100644 --- a/protocols/VKontakte/src/vk_thread.cpp +++ b/protocols/VKontakte/src/vk_thread.cpp @@ -110,11 +110,11 @@ void CVkProto::SetServerStatus(int iNewStatus) int iOldStatus = m_iStatus; CMString oldStatusMsg = ptrT(db_get_tsa(NULL, m_szModuleName, "OldStatusMsg")); - CMString ListeningToMsg = ptrT(db_get_tsa(NULL, m_szModuleName, "ListeningTo")); + ptrT ptszListeningToMsg(db_get_tsa(NULL, m_szModuleName, "ListeningTo")); if (iNewStatus == ID_STATUS_OFFLINE) { m_iStatus = ID_STATUS_OFFLINE; - if (!ListeningToMsg.IsEmpty()) + if (!IsEmpty(ptszListeningToMsg)) RetrieveStatusMsg(oldStatusMsg); if (iOldStatus != ID_STATUS_OFFLINE && iOldStatus != ID_STATUS_INVISIBLE) Push(new AsyncHttpRequest(this, REQUEST_GET, "/method/account.setOffline.json", true, &CVkProto::OnReceiveSmth) @@ -127,7 +127,7 @@ void CVkProto::SetServerStatus(int iNewStatus) } else { m_iStatus = ID_STATUS_INVISIBLE; - if (!ListeningToMsg.IsEmpty()) + if (!IsEmpty(ptszListeningToMsg)) RetrieveStatusMsg(oldStatusMsg); if (iOldStatus == ID_STATUS_ONLINE) Push(new AsyncHttpRequest(this, REQUEST_GET, "/method/account.setOffline.json", true, &CVkProto::OnReceiveSmth) @@ -386,10 +386,9 @@ void CVkProto::RetrieveUserInfo(LONG userID) CMString userIDs, code; userIDs.AppendFormat(_T("%i"), userID); - CMString codeformat("var userIDs=\"%s\";" - "return{\"freeoffline\":0,\"users\":API.users.get({\"user_ids\":userIDs,\"fields\":\"%s\",\"name_case\":\"nom\"})};"); - - code.AppendFormat(codeformat, userIDs.GetBuffer(), CMString(fieldsName).GetBuffer()); + + code.AppendFormat(_T("var userIDs=\"%s\";return{\"freeoffline\":0,\"users\":API.users.get({\"user_ids\":userIDs,\"fields\":\"%s\",\"name_case\":\"nom\"})};"), + userIDs.GetBuffer(), CMString(fieldsName).GetBuffer()); Push(new AsyncHttpRequest(this, REQUEST_POST, "/method/execute.json", true, &CVkProto::OnReceiveUserInfo) << TCHAR_PARAM("code", code) << VER_API); @@ -988,9 +987,9 @@ void CVkProto::OnReceiveStatus(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) JSONNODE *pAudio = json_get(pResponse, "audio"); if (pAudio == NULL) { - CMString StatusText = json_as_CMString(json_get(pResponse, "text")); - if (StatusText.GetBuffer()[0] != TCHAR(9835)) - setTString("OldStatusMsg", StatusText.GetBuffer()); + ptrT ptszStatusText(json_as_string(json_get(pResponse, "text"))); + if (ptszStatusText[0] != TCHAR(9835)) + setTString("OldStatusMsg", ptszStatusText); } } @@ -1012,13 +1011,13 @@ void CVkProto::RetrieveStatusMusic(const CMString &StatusMsg) return; CMString code; - CMString oldStatusMsg = ptrT(db_get_tsa(0, m_szModuleName, "OldStatusMsg")); + ptrT ptszOldStatusMsg(db_get_tsa(0, m_szModuleName, "OldStatusMsg")); if (StatusMsg.IsEmpty()) { if (m_iMusicSendMetod == sendBroadcastOnly) code = "API.audio.setBroadcast();return null;"; else { CMString codeformat("API.status.set({text:\"%s\"});return null;"); - code.AppendFormat(codeformat, oldStatusMsg); + code.AppendFormat(codeformat, ptszOldStatusMsg); } } else { @@ -1064,19 +1063,19 @@ INT_PTR __cdecl CVkProto::SvcSetListeningTo(WPARAM, LPARAM lParam) return 1; LISTENINGTOINFO *pliInfo = (LISTENINGTOINFO*)lParam; - CMStringW wszListeningTo; + CMString tszListeningTo; if (pliInfo == NULL || pliInfo->cbSize != sizeof(LISTENINGTOINFO)) db_unset(NULL, m_szModuleName, "ListeningTo"); else if (pliInfo->dwFlags & LTI_UNICODE) { if (ServiceExists(MS_LISTENINGTO_GETPARSEDTEXT)) - wszListeningTo = ptrT((LPWSTR)CallService(MS_LISTENINGTO_GETPARSEDTEXT, (WPARAM)_T("%artist% - %title%"), (LPARAM)pliInfo)); + tszListeningTo = ptrT((LPWSTR)CallService(MS_LISTENINGTO_GETPARSEDTEXT, (WPARAM)_T("%artist% - %title%"), (LPARAM)pliInfo)); else - wszListeningTo.Format(_T("%s - %s"), + tszListeningTo.Format(_T("%s - %s"), pliInfo->ptszArtist ? pliInfo->ptszArtist : _T(""), pliInfo->ptszTitle ? pliInfo->ptszTitle : _T("")); - setTString("ListeningTo", wszListeningTo); + setTString("ListeningTo", tszListeningTo); } - RetrieveStatusMusic(wszListeningTo); + RetrieveStatusMusic(tszListeningTo); return 0; } @@ -1099,11 +1098,11 @@ INT_PTR __cdecl CVkProto::SvcDeleteFriend(WPARAM hContact, LPARAM flag) if (!IsOnline() || userID == -1 || userID == VK_FEED_USER) return 1; - CMString formatstr = TranslateT("Are you sure to delete %s from your friend list?"), - tszNick = ptrT(db_get_tsa(hContact, m_szModuleName, "Nick")), - ptszMsg; + + ptrT ptszNick(db_get_tsa(hContact, m_szModuleName, "Nick")); + CMString ptszMsg; if (flag == 0) { - ptszMsg.AppendFormat(formatstr, tszNick.IsEmpty() ? TranslateT("(Unknown contact)") : tszNick); + ptszMsg.AppendFormat(TranslateT("Are you sure to delete %s from your friend list?"), IsEmpty(ptszNick) ? TranslateT("(Unknown contact)") : ptszNick); if (IDNO == MessageBox(NULL, ptszMsg.GetBuffer(), TranslateT("Attention!"), MB_ICONWARNING | MB_YESNO)) return 1; } @@ -1191,12 +1190,11 @@ INT_PTR __cdecl CVkProto::SvcBanUser(WPARAM hContact, LPARAM) tszVarWarning += ".\n"; code += "return 1;"; - CMString formatstr = TranslateT("Are you sure to ban %s? %s%sContinue?"), - tszNick = ptrT(db_get_tsa(hContact, m_szModuleName, "Nick")), - ptszMsg; + ptrT ptszNick(db_get_tsa(hContact, m_szModuleName, "Nick")); + CMString ptszMsg; - ptszMsg.AppendFormat(formatstr, - tszNick.IsEmpty() ? TranslateT("(Unknown contact)") : tszNick.GetBuffer(), + ptszMsg.AppendFormat(TranslateT("Are you sure to ban %s? %s%sContinue?"), + IsEmpty(ptszNick) ? TranslateT("(Unknown contact)") : ptszNick, tszVarWarning.IsEmpty() ? _T(" ") : TranslateT("\nIt will also"), tszVarWarning.IsEmpty() ? _T("\n") : tszVarWarning.GetBuffer()); -- cgit v1.2.3