OSDN Git Service

NCVのリスナー情報同期処理
[nlite/nlite.git] / nlite / nlite_chatData.cpp
1 #include "stdafx.h"
2 #include "nlite_include.h"
3
4 namespace nlite{
5
6
7
8
9 //\83\8a\83X\83i\81[\8c\9f\8dõ\83I\83u\83W\83F\83N\83g
10 struct ListenerPre{
11
12         ListenerData &user;
13
14         ListenerPre(ListenerData &in_user):
15                 user(in_user)
16         {}
17
18         bool operator()(CListenerList::reference listenerData){
19
20                 
21                 return (user.user_id == listenerData.user_id) && (user.community == listenerData.community);
22         }
23
24 };
25
26
27 //
28 //\83\8a\83X\83i\81[\83f\81[\83^
29 /////////////////////////////////////////////////////////////////////////////////////////////////
30
31
32 ListenerData::ListenerData():
33 user_id(TEXT("")),
34 name(TEXT("")),
35 community(TEXT("")),
36 bgcolor(INIT_COLOR),
37 //originBkFlag(FALSE),
38 time(0)
39 {}
40
41 VOID ListenerData::SetBkColor(COLORREF bkColor_in){
42         bgcolor = bkColor_in;
43         return;
44 }
45
46
47 CChatData::ViewData::ViewData():
48                         sellLineColor(INIT_COLOR),
49                         stringColor(INIT_COLOR),
50                         backColor(INIT_COLOR),
51                   selectFlag(FALSE),
52                   height(0)
53                 {}
54
55 VOID CChatData::SetChat(NicoLiveChat &chat_in,ListenerData &listener){
56         
57         
58         this->anonymity = _ttol(chat_in.anonymity);
59         this->premium = _ttol(chat_in.premium);
60         this->chatBuf = chat_in.chatBuf;
61         this->no = _ttol(chat_in.no);
62
63         if(_tcscmp(chat_in.locale,TEXT("jp")) == 0){
64                 this->locale = cuntry::jp;
65         } else {
66                 this->locale = cuntry::unknown;
67         }
68         this->mail.reserve(chat_in.mailCount);
69         for(UINT_PTR index = 0;index < chat_in.mailCount ;index++){
70
71                 this->mail.push_back(chat_in.mail[index]);
72         }
73
74         this->name = chat_in.name;
75
76
77         
78         this->listenerData = &listenerList.Register(listener,FALSE);
79
80         
81         this->vpos = _ttol(chat_in.vpos);
82         this->thread = _ttol(chat_in.thread);
83         this->date = _ttol(chat_in.date);
84         
85
86
87         return;
88 }
89
90
91 //
92 //
93 //\83R\83\81\83\93\83g\83\8a\83X\83g\83N\83\89\83X
94 //////////////////////////////////////////////////////////////////////////
95
96 //\91I\91ð\8ds\8eæ\93¾\97p\83t\83@\83\93\83N\83V\83\87\83i\83\8b
97 struct GetCurSelFunc_t{
98
99         INT_PTR count;
100         BOOL findFlag;
101         GetCurSelFunc_t():
102                 count(0)
103                 {}
104
105         BOOL operator() (const CChatData &target){
106                 count++;
107                 return  target.viewData.selectFlag;
108         }
109
110 };
111
112 //\91}\93ü\88Ê\92u\8c\9f\8dõ\83t\83@\83\93\83N\83V\83\87\83i\83\8b
113 struct SeartchSetPos_t{
114         time_t time;
115
116         SeartchSetPos_t(time_t in_time):time(in_time){}
117
118         bool operator()(const CChatData &target){
119
120                 return time > target.date;
121         }
122 };
123
124 CCommentList::CCommentList(){}
125
126 CCommentList::~CCommentList(){}
127
128 CCommentList::iterator CCommentList::GetCurSel(){
129         auto end = this->end();
130         auto ite = this->begin();
131
132         for(;ite != end && ite->viewData.selectFlag == FALSE;++ite);
133
134         return ite ;
135 }
136
137
138 INT_PTR CCommentList::GetCurSelNo(){
139
140         auto end = this->end();
141         auto ite = this->begin();
142         INT_PTR no = 0;
143         for(;ite != end && ite->viewData.selectFlag == FALSE;++ite,++no);
144
145         return ite != end ? no : -1;
146
147 }
148
149
150 CCommentList::iterator CCommentList::OnChatReceve(NicoLiveChat_P chatData_in,LPCTSTR communityID, UINT_PTR commnetCountSum,CCommentListWindow &listWindow){
151
152         CChatData chatData;
153         ListenerData listenerData;
154         listenerData.user_id = chatData_in->user_id;
155         listenerData.time = time(NULL);
156         listenerData.community = communityID;
157         chatData.SetChat(*chatData_in,listenerData);
158         iterator rslt;
159         if(empty() == TRUE || back().date <= chatData.date){
160                 chatList.push_back(chatData);
161                 rslt = (--chatList.end());
162         }else {
163
164                 rslt = chatList.insert(std::find_if(chatList.begin(),chatList.end(),SeartchSetPos_t(chatData.date)),chatData);
165         }
166         
167         if(shortcut ==chatList.end())shortcut = chatList.begin();
168
169         return rslt;
170 }
171
172 CCommentList::iterator CCommentList::GetCurSelItem(){
173         auto rslt = std::find_if(chatList.begin(),chatList.end(),GetCurSelFunc_t());
174
175         return rslt;
176 }
177
178
179 VOID CCommentList::SetCurSel(CCommentList::iterator cursel){
180         
181         auto end = this->end();
182         if(cursel == end)goto end;
183         {
184                 auto curselItem = this->GetCurSelItem();
185         
186                 if(curselItem != end){
187                         curselItem->viewData.selectFlag = FALSE;
188                 }
189         
190                 
191                 cursel->viewData.selectFlag = TRUE;
192         }
193 end:
194
195         return;
196 }
197
198
199 CCommentList::reference CCommentList::GetChatAt(UINT_PTR no){
200
201         iterator data = chatList.begin();
202         std::advance(data,no);
203         return *data;
204 }
205
206
207 UINT_PTR CCommentList::Size(){
208
209         return chatList.size();
210 }
211
212 BOOL CCommentList::empty(){
213
214         return chatList.empty();
215 }
216
217
218
219 CCommentList::iterator CCommentList::begin(){
220
221         return chatList.begin();
222 }
223
224 CCommentList::iterator CCommentList::end(){
225         
226         return chatList.end();
227 }
228
229
230 CCommentList::reverse_iterator CCommentList::rbegein(){
231
232         
233         return chatList.rbegin();
234 }
235
236
237
238                 
239 CCommentList::reverse_iterator CCommentList::rend(){
240
241         return chatList.rend();
242 }
243
244 VOID CCommentList::OnDestroy(){
245         
246         chatList.clear();
247         
248 }
249
250 VOID CCommentList::OnConnect(){
251         OnDestroy();
252         shortcut = chatList.begin();
253 }
254
255 CCommentList::reference CCommentList::back(){
256
257         return chatList.back();
258 }
259
260 ///
261 ///\83V\83\87\81[\83g\83J\83b\83g\83C\83e\83\8c\81[\83^\82ð\8eæ\93¾
262 ///
263 CCommentList::iterator CCommentList::GetShortCut(){
264
265         return shortcut;
266 }
267                 
268 ///
269 ///\83V\83\87\81[\83g\83J\83b\83g\82ð\90Ý\92è
270 ///
271 BOOL CCommentList::SetShortCut(UINT_PTR no){
272
273
274         if(static_cast<INT_PTR>(chatList.size()) <= no)return FALSE;
275
276         shortcut = chatList.begin();
277         std::advance(shortcut,no);
278
279         return TRUE;
280
281 }
282
283
284
285 VOID CListenerList::BkColorCollect(CListenerColorCollector &target){
286
287         iterator end = m_listenerList.end();
288         std::for_each(m_listenerList.begin(), end,target);
289 }
290
291 CListenerList::reference CListenerList::Register(ListenerData &listener,BOOL bUpdate){
292
293         ListenerPre listenerPre(listener);
294
295         iterator begin = m_listenerList.begin();
296         iterator end = m_listenerList.end();
297
298         iterator rslt = std::find_if(begin,end,listenerPre);
299
300         
301
302         if(rslt == end){
303
304                 
305                 m_listenerList.push_back(listener);
306
307                 return m_listenerList.back();
308
309         } else if(bUpdate == TRUE){
310
311                 *rslt = listener;
312
313         }
314         
315         return *rslt;
316
317 }
318
319 BOOL CListenerList::ReadProperty(LPCTSTR fileName){
320         BOOL rslt = FALSE;
321         CAtlFile        userSettingFile;
322         ULONGLONG userSettingFileSize;
323         
324         
325         if(SUCCEEDED(userSettingFile.Create(fileName,GENERIC_READ,FILE_SHARE_READ,OPEN_ALWAYS)) == FALSE || SUCCEEDED(userSettingFile.GetSize(userSettingFileSize)) == FALSE){
326
327                 throw Exception(TEXT("\83\8a\83X\83i\81[\90Ý\92è\83t\83@\83C\83\8b\82ª\8aJ\82¯\82Ü\82¹\82ñ\82Å\82µ\82½\81B\82±\82Ì\83G\83\89\81[\82ª\91±\82­\82æ\82¤\82Å\82 \82ê\82Î\81A\88ê\93x\90Ý\92è\83t\83@\83C\83\8b\82ð\8dí\8f\9c\82µ\82Ä\8dÄ\93x\8e\8e\82µ\82Ä\82Ý\82Ä\82­\82¾\82³\82¢"),__LINE__,TEXT(__FILE__),TEXT(__FUNCTION__));
328         }
329         std::vector<char> userSettingBuf(static_cast<UINT_PTR>(userSettingFileSize / sizeof(char) + (1 * sizeof(char))));
330         userSettingFile.Read(&userSettingBuf[0],userSettingBuf.size());
331         LPSTR userSettingStart = strstr(&userSettingBuf[0],"<");
332         if(userSettingStart != NULL){
333                 ReadUserSettingXML rusx(nlite::listenerList);
334                 rusx.Parse(userSettingStart);
335         }
336
337
338         return rslt;
339 }
340
341
342 ReadUserSettingXML::ReadUserSettingXML(CListenerList &in_listenerList):m_listenerList(in_listenerList),userNodeFlag(FALSE){}
343
344 #define NLITE_READ_USER_STRING(at,l,t) if(_tcscmp(at[0],TEXT(#t)) == 0)l.t = at[1]
345 #define NLITE_READ_USER_INT(at,l,t)             if(_tcscmp(at[0],TEXT(#t))==0)l.t = _tcstol((const wchar_t*)at[1],NULL,10)
346
347         void ReadUserSettingXML::OnStartElement (const XML_Char *pszName, const XML_Char **papszAttrs){
348
349                 if(_tcscmp(pszName,TEXT("user")) == 0){
350
351                         this->userNodeFlag = TRUE;
352
353
354                         for(;papszAttrs[0] != NULL;papszAttrs += 2){
355
356                                 NLITE_READ_USER_STRING(papszAttrs,listenerBuff,name);
357                                 NLITE_READ_USER_STRING(papszAttrs,listenerBuff,community);
358                                 NLITE_READ_USER_INT(papszAttrs,listenerBuff,bgcolor);
359                                 NLITE_READ_USER_INT(papszAttrs,listenerBuff,time);
360                                 
361                         }
362
363                 }
364
365                 return;
366         }
367
368         void ReadUserSettingXML::OnEndElement (const XML_Char *pszName){
369
370                 if(_tcscmp(pszName,TEXT("user")) == 0){
371
372                         listenerList.Register(listenerBuff,TRUE);
373                         this->userNodeFlag = FALSE;
374                         this->listenerBuff.~ListenerData();
375                         new(&listenerBuff) ListenerData();
376
377                 }
378
379                 return;
380         }
381
382         void ReadUserSettingXML::OnCharacterData (const XML_Char *pszData, int nLength){
383
384                 if(this->userNodeFlag == TRUE){
385
386
387                         listenerBuff.user_id.Append(pszData,nLength);
388
389
390
391
392                 }
393
394                 
395
396                 return;
397         }
398
399 }