source: chevmsgr/trunk/msvc14/ChevMsgrClient_MFC/ChevMsgrClient_MFCDlg.cpp@ 32

Last change on this file since 32 was 32, checked in by cheese, 8 years ago

UI 검색기능 추가
클라이언트 수정

File size: 12.0 KB
Line 
1
2// ChevMsgrClient_MFCDlg.cpp : ±¸Çö ÆÄÀÏ
3//
4
5#include "stdafx.h"
6#include "ChevMsgrClient_MFC.h"
7#include "ChevMsgrClient_MFCDlg.h"
8#include "afxdialogex.h"
9#include <fstream>
10#include <algorithm>
11#include <time.h>
12
13#include "LoginDlg.h"
14#include "RegisterDlg.h"
15#include "SMSDlg.h"
16#include "AddFriendDlg.h"
17#include "AddGroupChat.h"
18
19#ifdef _DEBUG
20#define new DEBUG_NEW
21#endif
22
23#define SENSITIVE_TIME 10
24#define TIMER_EVENT WM_USER +1
25
26int cb_listen(SConversation & chat);
27int cb_opensession(SOpenSession & opensession);
28
29CChevMsgrClient_MFCDlg * gDlg = NULL;
30
31// ÀÀ¿ë ÇÁ·Î±×·¥ Á¤º¸¿¡ »ç¿ëµÇ´Â CAboutDlg ´ëÈ­ »óÀÚÀÔ´Ï´Ù.
32
33class CAboutDlg : public CDialogEx
34{
35public:
36 CAboutDlg();
37
38// ´ëÈ­ »óÀÚ µ¥ÀÌÅÍÀÔ´Ï´Ù.
39#ifdef AFX_DESIGN_TIME
40 enum { IDD = IDD_ABOUTBOX };
41#endif
42
43 protected:
44 virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV Áö¿øÀÔ´Ï´Ù.
45
46// ±¸ÇöÀÔ´Ï´Ù.
47protected:
48 DECLARE_MESSAGE_MAP()
49};
50
51CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
52{
53}
54
55void CAboutDlg::DoDataExchange(CDataExchange* pDX)
56{
57 CDialogEx::DoDataExchange(pDX);
58}
59
60BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
61END_MESSAGE_MAP()
62
63
64// CChevMsgrClient_MFCDlg ´ëÈ­ »óÀÚ
65
66
67
68CChevMsgrClient_MFCDlg::CChevMsgrClient_MFCDlg(CWnd* pParent /*=NULL*/)
69 : CDialogEx(IDD_CHEVMSGRCLIENT_MFC_DIALOG, pParent)
70 , mCheckedSensitive(FALSE)
71 , mSearch(_T(""))
72 , mSearchIndex(0)
73{
74 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
75 gDlg = this;
76}
77
78void CChevMsgrClient_MFCDlg::DoDataExchange(CDataExchange* pDX)
79{
80 CDialogEx::DoDataExchange(pDX);
81 DDX_Control(pDX, IDC_LIST_CONVERSATION, mChatListBox);
82 DDX_Control(pDX, IDC_LIST_FRIENDS, mFriendListBox);
83 DDX_Check(pDX, IDC_CHECK_SENSITIVE, mCheckedSensitive);
84 DDX_Text(pDX, IDC_EDIT_SEARCH, mSearch);
85}
86
87BEGIN_MESSAGE_MAP(CChevMsgrClient_MFCDlg, CDialogEx)
88 ON_WM_SYSCOMMAND()
89 ON_WM_PAINT()
90 ON_WM_QUERYDRAGICON()
91 ON_BN_CLICKED(IDC_BTN_SEND, &CChevMsgrClient_MFCDlg::OnBnClickedBtnSend)
92 ON_BN_CLICKED(IDC_BTN_ADD_FRIEND, &CChevMsgrClient_MFCDlg::OnBnClickedBtnAddFriend)
93 ON_BN_CLICKED(IDC_BTN_ADD_CHAT, &CChevMsgrClient_MFCDlg::OnBnClickedBtnAddChat)
94 ON_LBN_SELCHANGE(IDC_LIST_FRIENDS, &CChevMsgrClient_MFCDlg::OnLbnSelchangeListFriends)
95 ON_WM_TIMER()
96 ON_BN_CLICKED(IDC_BTN_SEARCH, &CChevMsgrClient_MFCDlg::OnBnClickedBtnSearch)
97END_MESSAGE_MAP()
98
99
100// CChevMsgrClient_MFCDlg ¸Þ½ÃÁö 󸮱â
101
102BOOL CChevMsgrClient_MFCDlg::OnInitDialog()
103{
104 CDialogEx::OnInitDialog();
105
106 // ½Ã½ºÅÛ ¸Þ´º¿¡ "Á¤º¸..." ¸Þ´º Ç׸ñÀ» Ãß°¡ÇÕ´Ï´Ù.
107
108 // IDM_ABOUTBOX´Â ½Ã½ºÅÛ ¸í·É ¹üÀ§¿¡ ÀÖ¾î¾ß ÇÕ´Ï´Ù.
109 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
110 ASSERT(IDM_ABOUTBOX < 0xF000);
111
112 CMenu* pSysMenu = GetSystemMenu(FALSE);
113 if (pSysMenu != NULL)
114 {
115 BOOL bNameValid;
116 CString strAboutMenu;
117 bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
118 ASSERT(bNameValid);
119 if (!strAboutMenu.IsEmpty())
120 {
121 pSysMenu->AppendMenu(MF_SEPARATOR);
122 pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
123 }
124 }
125
126 // ÀÌ ´ëÈ­ »óÀÚÀÇ ¾ÆÀÌÄÜÀ» ¼³Á¤ÇÕ´Ï´Ù. ÀÀ¿ë ÇÁ·Î±×·¥ÀÇ ÁÖ Ã¢ÀÌ ´ëÈ­ »óÀÚ°¡ ¾Æ´Ò °æ¿ì¿¡´Â
127 // ÇÁ·¹ÀÓ¿öÅ©°¡ ÀÌ ÀÛ¾÷À» ÀÚµ¿À¸·Î ¼öÇàÇÕ´Ï´Ù.
128 SetIcon(m_hIcon, TRUE); // Å« ¾ÆÀÌÄÜÀ» ¼³Á¤ÇÕ´Ï´Ù.
129 SetIcon(m_hIcon, FALSE); // ÀÛÀº ¾ÆÀÌÄÜÀ» ¼³Á¤ÇÕ´Ï´Ù.
130
131 // TODO: ¿©±â¿¡ Ãß°¡ ÃʱâÈ­ ÀÛ¾÷À» Ãß°¡ÇÕ´Ï´Ù.
132 this->SetTimer(TIMER_EVENT, 2000, NULL);
133
134 std::ifstream f("cnct2srv.ini");
135 if (!f)
136 {
137 AfxMessageBox(_T("¼­¹ö Á¤º¸ ÆÄÀÏÀ» ãÀ» ¼ö ¾øÀ½"));
138 ::SendMessage(GetSafeHwnd(), WM_QUIT, NULL, NULL);
139 }
140 std::string ip;
141 std::string port;
142
143 std::getline(f, ip);
144 std::getline(f, port);
145
146 if (!chev.connect(ip, atoi(port.c_str())))
147 {
148 AfxMessageBox(str2cstr(chev.getLastError()));
149 ::SendMessage(GetSafeHwnd(), WM_QUIT, NULL, NULL);
150 }
151
152 CLoginDlg loginDlg;
153 loginDlg.chev = &this->chev;
154 if (loginDlg.DoModal() != 0)
155 {
156 AfxMessageBox(_T("login failed"));
157 ::SendMessage(GetSafeHwnd(), WM_QUIT, NULL, NULL);
158 }
159
160 this->mId = loginDlg.mId;
161
162 SCallback cb;
163
164 cb.onListen = cb_listen;
165 cb.onOpenSession = cb_opensession;
166
167 chev.listen(cb);
168
169 UpdateFriendList();
170
171 CString cstrId;
172
173 for (size_t iter = 0; iter < idList.size(); iter++)
174 {
175 cstrId = str2cstr(idList[iter].id);
176 this->mFriendListBox.InsertString(this->mFriendListBox.GetCount(), cstrId);
177 }
178
179 scl.resize(idList.size());
180
181 return TRUE; // Æ÷Ä¿½º¸¦ ÄÁÆ®·Ñ¿¡ ¼³Á¤ÇÏÁö ¾ÊÀ¸¸é TRUE¸¦ ¹ÝȯÇÕ´Ï´Ù.
182}
183
184void CChevMsgrClient_MFCDlg::OnSysCommand(UINT nID, LPARAM lParam)
185{
186 if ((nID & 0xFFF0) == IDM_ABOUTBOX)
187 {
188 CAboutDlg dlgAbout;
189 dlgAbout.DoModal();
190 }
191 else
192 {
193 CDialogEx::OnSysCommand(nID, lParam);
194 }
195}
196
197// ´ëÈ­ »óÀÚ¿¡ ÃÖ¼ÒÈ­ ´ÜÃ߸¦ Ãß°¡ÇÒ °æ¿ì ¾ÆÀÌÄÜÀ» ±×¸®·Á¸é
198// ¾Æ·¡ Äڵ尡 ÇÊ¿äÇÕ´Ï´Ù. ¹®¼­/ºä ¸ðµ¨À» »ç¿ëÇÏ´Â MFC ÀÀ¿ë ÇÁ·Î±×·¥ÀÇ °æ¿ì¿¡´Â
199// ÇÁ·¹ÀÓ¿öÅ©¿¡¼­ ÀÌ ÀÛ¾÷À» ÀÚµ¿À¸·Î ¼öÇàÇÕ´Ï´Ù.
200
201void CChevMsgrClient_MFCDlg::OnPaint()
202{
203 if (IsIconic())
204 {
205 CPaintDC dc(this); // ±×¸®±â¸¦ À§ÇÑ µð¹ÙÀ̽º ÄÁÅؽºÆ®ÀÔ´Ï´Ù.
206
207 SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
208
209 // Ŭ¶óÀ̾ðÆ® »ç°¢Çü¿¡¼­ ¾ÆÀÌÄÜÀ» °¡¿îµ¥¿¡ ¸ÂÃä´Ï´Ù.
210 int cxIcon = GetSystemMetrics(SM_CXICON);
211 int cyIcon = GetSystemMetrics(SM_CYICON);
212 CRect rect;
213 GetClientRect(&rect);
214 int x = (rect.Width() - cxIcon + 1) / 2;
215 int y = (rect.Height() - cyIcon + 1) / 2;
216
217 // ¾ÆÀÌÄÜÀ» ±×¸³´Ï´Ù.
218 dc.DrawIcon(x, y, m_hIcon);
219 }
220 else
221 {
222 CDialogEx::OnPaint();
223 }
224}
225
226// »ç¿ëÀÚ°¡ ÃÖ¼ÒÈ­µÈ âÀ» ²ô´Â µ¿¾È¿¡ Ä¿¼­°¡ Ç¥½ÃµÇµµ·Ï ½Ã½ºÅÛ¿¡¼­
227// ÀÌ ÇÔ¼ö¸¦ È£ÃâÇÕ´Ï´Ù.
228HCURSOR CChevMsgrClient_MFCDlg::OnQueryDragIcon()
229{
230 return static_cast<HCURSOR>(m_hIcon);
231}
232
233int cb_listen(SConversation & chat)
234{
235 int idx = -1;
236
237 for (idx = 0; idx < gDlg->mFriendListBox.GetCount(); idx++)
238 {
239 if (chat.sessid == gDlg->scl[idx].sessionId)
240 {
241 if (chat.sensitive == true)
242 {
243 SensitiveCtrl sc;
244
245 sc.sensitiveIdx = gDlg->scl[idx].chatList.size();
246 sc.timestamp = (int)time(NULL);
247 gDlg->scl[idx].sensitiveCtrl.push_back(sc);
248 }
249
250 gDlg->scl[idx].chatList.push_back(chat);
251
252 break;
253 }
254 }
255
256 if (gDlg->mFriendListBox.GetCurSel() == idx)
257 {
258 gDlg->mChatListBox.InsertString(gDlg->mChatListBox.GetCount(), str2cstr("<" + chat.from + ">"));
259 gDlg->mChatListBox.InsertString(gDlg->mChatListBox.GetCount(), str2cstr(chat.message));
260 gDlg->mChatListBox.InsertString(gDlg->mChatListBox.GetCount(), _T(""));
261 }
262
263 return 0;
264}
265
266int cb_opensession(SOpenSession & opensession)
267{
268 CString cstrIdList = _T("");
269
270 for (size_t iter = 0; iter < opensession.idList.size(); iter++)
271 {
272 if (opensession.idList[iter] != gDlg->mId)
273 {
274 if (cstrIdList == "")
275 cstrIdList = str2cstr(opensession.idList[iter]);
276 else
277 cstrIdList = cstrIdList + "," + str2cstr(opensession.idList[iter]);
278 }
279 }
280
281 if (opensession.idList.size() > 2)
282 {
283 gDlg->mFriendListBox.InsertString(gDlg->mFriendListBox.GetCount(), cstrIdList);
284
285 Session s;
286
287 s.sessionId = opensession.sessid;
288
289 gDlg->scl.push_back(s);
290 }
291 else
292 {
293 CString cmpIdList;
294 for (int iter = 0; iter < gDlg->mFriendListBox.GetCount(); iter++)
295 {
296 gDlg->mFriendListBox.GetText(iter, cmpIdList);
297 if (cstrIdList == cmpIdList)
298 gDlg->scl[iter].sessionId = opensession.sessid;
299 }
300 }
301
302 return 0;
303}
304
305void CChevMsgrClient_MFCDlg::OnBnClickedBtnSend()
306{
307 // TODO: ¿©±â¿¡ ÄÁÆ®·Ñ ¾Ë¸² 󸮱â Äڵ带 Ãß°¡ÇÕ´Ï´Ù.
308 CString chat;
309 CString receiver;
310 SConversation conversation;
311 int idx = mFriendListBox.GetCurSel();
312
313 if (idx < 0)
314 return;
315
316 mFriendListBox.GetText(idx, receiver);
317 GetDlgItemText(IDC_EDIT_INPUT, chat);
318 UpdateData(TRUE);
319
320 if(scl[idx].sessionId == "")
321 {
322 std::vector<std::string> idList;
323 std::string comma;
324 mFriendListBox.GetText(idx, receiver);
325 std::istringstream is(cstr2str(receiver));
326 while (std::getline(is, comma, ','))
327 idList.push_back(comma);
328 std::sort(idList.begin(), idList.end());
329 scl[idx].sessionId = chev.getSessionID(idList);
330 }
331
332 conversation.sessid = scl[idx].sessionId;
333 conversation.from = this->mId;
334 conversation.message = cstr2str(chat);
335 conversation.sensitive = mCheckedSensitive ? true : false;
336
337 if (chev.tell(conversation))
338 {
339 if (conversation.sensitive == true)
340 {
341 SensitiveCtrl sc;
342
343 sc.sensitiveIdx = gDlg->scl[idx].chatList.size();
344 sc.timestamp = (int)time(NULL);
345 gDlg->scl[idx].sensitiveCtrl.push_back(sc);
346 }
347 gDlg->scl[idx].chatList.push_back(conversation);
348
349 mChatListBox.InsertString(mChatListBox.GetCount(), str2cstr("<" + mId + ">"));
350 mChatListBox.InsertString(mChatListBox.GetCount(), chat);
351 mChatListBox.InsertString(mChatListBox.GetCount(), _T(""));
352 }
353
354 SetDlgItemText(IDC_EDIT_INPUT, _T(""));
355}
356
357void CChevMsgrClient_MFCDlg::OnBnClickedBtnAddFriend()
358{
359 // TODO: ¿©±â¿¡ ÄÁÆ®·Ñ ¾Ë¸² 󸮱â Äڵ带 Ãß°¡ÇÕ´Ï´Ù.
360 CAddFriendDlg AFDlg;
361
362 AFDlg.DoModal();
363
364 CString existFriendId;
365 for (int iter = 0; iter < mFriendListBox.GetCount(); iter++)
366 {
367 mFriendListBox.GetText(iter, existFriendId);
368 if (AFDlg.friendId == existFriendId)
369 return;
370 }
371 if (!chev.addFriend(cstr2str(AFDlg.friendId)))
372 {
373 AfxMessageBox(_T("Add Friend Fail"));
374 return;
375 }
376 UpdateFriendList();
377 scl.resize(scl.size() + 1);
378 mFriendListBox.InsertString(mFriendListBox.GetCount(), AFDlg.friendId);
379}
380
381
382void CChevMsgrClient_MFCDlg::OnBnClickedBtnAddChat()
383{
384 // TODO: ¿©±â¿¡ ÄÁÆ®·Ñ ¾Ë¸² 󸮱â Äڵ带 Ãß°¡ÇÕ´Ï´Ù.
385 CAddGroupChatDlg AGCDlg;
386
387 AGCDlg.chev = &this->chev;
388 AGCDlg.mMsgrClntDlg = this;
389 AGCDlg.DoModal();
390
391 CString cmpCstr;
392 for (int iter = 0; iter < this->mFriendListBox.GetCount(); iter++)
393 {
394 this->mFriendListBox.GetText(iter, cmpCstr);
395 if (AGCDlg.groupChat == cmpCstr)
396 return;
397 }
398
399 mFriendListBox.InsertString(mFriendListBox.GetCount(), AGCDlg.groupChat);
400 gDlg->scl.resize(gDlg->scl.size() + 1);
401}
402
403void CChevMsgrClient_MFCDlg::UpdateFriendList()
404{
405 if (!chev.getFriendList(idList))
406 {
407 AfxMessageBox(_T("cannot get friend list"));
408 ::SendMessage(GetSafeHwnd(), WM_QUIT, NULL, NULL);
409 }
410}
411
412void CChevMsgrClient_MFCDlg::OnLbnSelchangeListFriends()
413{
414 // TODO: ¿©±â¿¡ ÄÁÆ®·Ñ ¾Ë¸² 󸮱â Äڵ带 Ãß°¡ÇÕ´Ï´Ù.
415 mChatListBox.ResetContent();
416 int idx = mFriendListBox.GetCurSel();
417
418 if (idx < 0)
419 return;
420
421 // vector out of range error
422 if (scl[idx].sessionId == "")
423 return;
424
425 std::vector<SConversation>::iterator iter = scl[idx].chatList.begin();
426 for ( ; iter != scl[idx].chatList.end(); iter++)
427 {
428 mChatListBox.InsertString(mChatListBox.GetCount(), str2cstr("<" + iter->from + ">"));
429 mChatListBox.InsertString(mChatListBox.GetCount(), str2cstr(iter->message));
430 mChatListBox.InsertString(mChatListBox.GetCount(), _T(""));
431 }
432}
433
434
435void CChevMsgrClient_MFCDlg::OnTimer(UINT_PTR nIDEvent)
436{
437 // TODO: ¿©±â¿¡ ¸Þ½ÃÁö 󸮱â Äڵ带 Ãß°¡ ¹×/¶Ç´Â ±âº»°ªÀ» È£ÃâÇÕ´Ï´Ù.
438 int timestamp = (int)time(NULL);
439
440 for (size_t iter = 0; iter < scl.size(); iter++)
441 {
442 if (scl[iter].sensitiveCtrl.size() == 0)
443 continue;
444
445 SensitiveCtrl &sc = scl[iter].sensitiveCtrl.front();
446 if ((timestamp - sc.timestamp) >= SENSITIVE_TIME)
447 {
448 scl[iter].chatList.erase(scl[iter].chatList.begin() + sc.sensitiveIdx);
449 if (iter == mFriendListBox.GetCurSel())
450 {
451 mChatListBox.DeleteString(sc.sensitiveIdx*3);
452 mChatListBox.DeleteString(sc.sensitiveIdx*3);
453 mChatListBox.DeleteString(sc.sensitiveIdx*3);
454 }
455 scl[iter].sensitiveCtrl.pop_front();
456
457 std::list<SensitiveCtrl>::iterator it = scl[iter].sensitiveCtrl.begin();
458 for (; it != scl[iter].sensitiveCtrl.end(); it++)
459 {
460 it->sensitiveIdx--;
461 }
462
463 }
464 }
465
466 CDialogEx::OnTimer(nIDEvent);
467}
468
469void CChevMsgrClient_MFCDlg::OnBnClickedBtnSearch()
470{
471 // TODO: ¿©±â¿¡ ÄÁÆ®·Ñ ¾Ë¸² 󸮱â Äڵ带 Ãß°¡ÇÕ´Ï´Ù.
472 UpdateData(TRUE);
473 CString search;
474 int test = mChatListBox.GetCount();
475
476 if (mSearch == "")
477 return;
478
479 for (int iter = mSearchIndex; ; iter++)
480 {
481 if ((iter * 3) == mChatListBox.GetCount())
482 {
483 mSearchIndex = -1;
484 break;
485 }
486 mChatListBox.GetText((iter * 3) + 1, search);
487
488 if (search.Find(mSearch, 0) != -1)
489 {
490 mChatListBox.SetCurSel((iter * 3) + 1);
491 mSearchIndex = iter;
492 break;
493 }
494 }
495 mSearchIndex++;
496}
Note: See TracBrowser for help on using the repository browser.