OSDN Git Service

f2ad2847b65d59fa4cdecbb7b7f401888ab374d0
[nlite/nlite.git] / nlite / nlite_chatData.cpp
1 #include "stdafx.h"
2 #include "nlite_include.h"
3 using namespace std::tr1;
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         CListenerData &user;
13
14         ListenerPre(CListenerData &in_user):
15                 user(in_user)
16         {}
17
18         bool operator()(CListenerList::reference listenerData){
19
20                 
21                 return (user.GetUserID() == listenerData.GetUserID())/* && (user.community == listenerData.community)*/;
22         }
23
24 };
25
26
27 //
28 //\83\8a\83X\83i\81[\83f\81[\83^
29 /////////////////////////////////////////////////////////////////////////////////////////////////
30
31
32 CListenerData::CListenerData():
33 user_id(TEXT("")),
34 name(TEXT("")),
35 community(TEXT("")),
36 bgcolor(INIT_COLOR),
37 time(0),
38 onlyCommunityFlag(FALSE),
39 nameColor(INIT_COLOR)
40 {}
41
42 VOID CListenerData::SetNameColor(COLORREF nameColor_in){
43         
44         nameColor = nameColor_in;
45
46         return;
47 }
48
49 VOID CListenerData::SetBkColor(COLORREF bkColor_in){
50         bgcolor = bkColor_in;
51
52         return;
53 }
54
55 VOID CListenerData::SetUserName(LPCTSTR name){
56
57         this->name = name;
58
59
60 }
61
62 COLORREF CListenerData::GetBackColor()const {
63
64         return this->bgcolor;
65
66 }
67
68 COLORREF CListenerData::GetNameColor()const{
69
70         return this->nameColor;
71 }
72
73
74 const CNLiteString &CListenerData::GetUserID()const{
75
76         return this->user_id;
77
78 }
79
80
81
82 const CNLiteString &CListenerData::GetName()const{
83
84         return this->name;
85
86 }
87
88 CChatData::ViewData::ViewData():
89                         sellLineColor(INIT_COLOR),
90                         stringColor(INIT_COLOR),
91                         backColor(INIT_COLOR),
92                   selectFlag(FALSE),
93                   height(0)
94                 {}
95
96 VOID CChatData::SetChat(NicoLiveChat &chat_in,CListenerData &listener){
97         
98         
99         this->anonymity = _ttol(chat_in.anonymity);
100         this->premium = _ttol(chat_in.premium);
101         this->chatBuf = chat_in.chatBuf;
102         this->no = _ttol(chat_in.no);
103
104         if(_tcscmp(chat_in.locale,TEXT("jp")) == 0){
105                 this->locale = cuntry::jp;
106         } else {
107                 this->locale = cuntry::unknown;
108         }
109         this->mail.reserve(chat_in.mailCount);
110         for(UINT_PTR index = 0;index < chat_in.mailCount ;index++){
111
112                 this->mail.push_back(chat_in.mail[index]);
113         }
114
115         this->name = chat_in.name;
116
117
118         
119         this->listenerData = &listenerList.Register(listener,FALSE);
120         tstring target(chat_in.chatBuf);
121         
122         match_results<tstring::iterator> results;
123         if(regex_match(target.begin(),target.end(),results,nliteregex::NAMESERTCHREGEX) == TRUE){
124                 
125                 const tstring &name = results.str(results.size() - 1).c_str();
126                 
127                 if(nliteProperty.GetGeneralProperty().IsNumberName() == TRUE || _tcstol(name.c_str(),NULL,10) == 0){
128                         this->listenerData->SetUserName(name.c_str());
129                 }
130                 
131         }
132         
133         this->vpos = _ttol(chat_in.vpos);
134         this->thread = _ttol(chat_in.thread);
135         this->date = _ttol(chat_in.date);
136         
137
138
139         return;
140 }
141
142
143 VOID CChatData::SetHeight(UINT_PTR height){
144
145         this->viewData.height = height;
146         
147 }
148
149 CListenerData &CChatData::GetListnerData() {
150
151
152         return *this->listenerData;
153 }
154
155 const CNLiteString &CChatData::GetChatBuff()const{
156
157         return this->chatBuf;
158 }
159
160 BOOL CChatData::IsSelect()const{
161
162         return this->viewData.selectFlag;
163 }
164
165 COLORREF CChatData::GetBackColor()const{
166
167         return this->viewData.backColor;
168 }
169
170 UINT_PTR CChatData::GetPremium()const{
171
172         return this->premium;
173
174 }
175
176 time_t CChatData::GetDate()const{
177
178         return this->date;
179
180 }
181
182 UINT_PTR CChatData::GetNo()const{
183
184         return this->no;
185 }
186
187 COLORREF CChatData::GetStringColor()const{
188
189         return this->viewData.stringColor;
190
191 }
192
193 COLORREF CChatData::GetSellLineColor()const{
194
195         return this->viewData.sellLineColor;
196 }
197
198 UINT_PTR CChatData::GetHeight()const{
199
200         return this->viewData.height;
201 }
202
203 //
204 //
205 //\83R\83\81\83\93\83g\83\8a\83X\83g\83N\83\89\83X
206 //////////////////////////////////////////////////////////////////////////
207
208 //\91I\91ð\8ds\8eæ\93¾\97p\83t\83@\83\93\83N\83V\83\87\83i\83\8b
209 struct GetCurSelFunc_t{
210
211         INT_PTR count;
212         BOOL findFlag;
213         GetCurSelFunc_t():
214                 count(0)
215                 {}
216
217         BOOL operator() (const CChatData &target){
218                 count++;
219                 return  target.IsSelect();
220         }
221
222 };
223
224 //\91}\93ü\88Ê\92u\8c\9f\8dõ\83t\83@\83\93\83N\83V\83\87\83i\83\8b
225 struct SeartchSetPos_t{
226         time_t time;
227
228         SeartchSetPos_t(time_t in_time):time(in_time){}
229
230         bool operator()(const CChatData &target){
231
232                 return time > target.GetDate();
233         }
234 };
235
236 CCommentList::CCommentList(){}
237
238 CCommentList::~CCommentList(){}
239
240 CCommentList::iterator CCommentList::GetCurSel(){
241         auto end = this->end();
242         auto ite = this->begin();
243
244         for(;ite != end && ite->viewData.selectFlag == FALSE;++ite);
245
246         return ite ;
247 }
248
249
250 INT_PTR CCommentList::GetCurSelNo(){
251
252         auto end = this->end();
253         auto ite = this->begin();
254         INT_PTR no = 0;
255         for(;ite != end && ite->viewData.selectFlag == FALSE;++ite,++no);
256
257         return ite != end ? no : -1;
258
259 }
260
261
262 CCommentList::iterator CCommentList::OnChatReceve(NicoLiveChat_P chatData_in,LPCTSTR communityID, UINT_PTR commnetCountSum,CCommentListWindow &listWindow){
263
264         CChatData chatData;
265         CListenerData listenerData;
266         listenerData.user_id = chatData_in->user_id;
267         listenerData.time = time(NULL);
268         listenerData.community = communityID;
269         chatData.SetChat(*chatData_in,listenerData);
270         iterator rslt;
271         if(empty() == TRUE || back().date <= chatData.date){
272                 chatList.push_back(chatData);
273                 rslt = (--chatList.end());
274                 if(nlite::nliteProperty.GetCommentReadProperty().IsCommentRead() == TRUE){
275                         
276                         g_speaker.SynthesisAsync((const speaker::TChar_P)(LPCTSTR)rslt->chatBuf);
277                 }
278         }else {
279
280                 rslt = chatList.insert(std::find_if(chatList.begin(),chatList.end(),SeartchSetPos_t(chatData.date)),chatData);
281         }
282         
283         if(shortcut ==chatList.end())shortcut = chatList.begin();
284
285         return rslt;
286 }
287
288 CCommentList::iterator CCommentList::GetCurSelItem(){
289         auto rslt = std::find_if(chatList.begin(),chatList.end(),GetCurSelFunc_t());
290
291         return rslt;
292 }
293
294
295 VOID CCommentList::SetCurSel(CCommentList::iterator cursel){
296         
297         auto end = this->end();
298         if(cursel == end)goto end;
299         {
300                 auto curselItem = this->GetCurSelItem();
301         
302                 if(curselItem != end){
303                         curselItem->viewData.selectFlag = FALSE;
304                 }
305         
306                 
307                 cursel->viewData.selectFlag = TRUE;
308         }
309 end:
310
311         return;
312 }
313
314
315 CCommentList::reference CCommentList::GetChatAt(UINT_PTR no){
316
317         iterator data = chatList.begin();
318         std::advance(data,no);
319         return *data;
320 }
321
322
323 UINT_PTR CCommentList::Size(){
324
325         return chatList.size();
326 }
327
328 BOOL CCommentList::empty(){
329
330         return chatList.empty();
331 }
332
333
334
335 CCommentList::iterator CCommentList::begin(){
336
337         return chatList.begin();
338 }
339
340 CCommentList::iterator CCommentList::end(){
341         
342         return chatList.end();
343 }
344
345
346 CCommentList::reverse_iterator CCommentList::rbegein(){
347
348         
349         return chatList.rbegin();
350 }
351
352
353
354                 
355 CCommentList::reverse_iterator CCommentList::rend(){
356
357         return chatList.rend();
358 }
359
360 VOID CCommentList::OnDestroy(){
361         
362         chatList.clear();
363         
364 }
365
366 VOID CCommentList::OnConnect(){
367         OnDestroy();
368         shortcut = chatList.begin();
369 }
370
371 CCommentList::reference CCommentList::back(){
372
373         return chatList.back();
374 }
375
376 ///
377 ///\83V\83\87\81[\83g\83J\83b\83g\83C\83e\83\8c\81[\83^\82ð\8eæ\93¾
378 ///
379 CCommentList::iterator CCommentList::GetShortCut(){
380
381         return shortcut;
382 }
383                 
384 ///
385 ///\83V\83\87\81[\83g\83J\83b\83g\82ð\90Ý\92è
386 ///
387 BOOL CCommentList::SetShortCut(UINT_PTR no){
388
389
390         if(static_cast<INT_PTR>(chatList.size()) <= no)return FALSE;
391
392         shortcut = chatList.begin();
393         std::advance(shortcut,no);
394
395         return TRUE;
396
397 }
398
399
400
401 VOID CListenerList::BkColorCollect(CListenerColorCollector &target){
402
403         iterator end = m_listenerList.end();
404         std::for_each(m_listenerList.begin(), end,target);
405 }
406
407 CListenerList::reference CListenerList::Register(CListenerData &listener,BOOL bUpdate){
408
409         //\8aÖ\90\94\83I\83u\83W\83F\83N\83g\90\90¬
410         ListenerPre listenerPre(listener);
411
412
413         iterator begin = m_listenerList.begin();
414         iterator end = m_listenerList.end();
415
416         //\93o\98^\97\\92è\82Ì\83\8a\83X\83i\81[\83f\81[\83^\82ª\82·\82Å\82É\93o\98^\82³\82ê\82Ä\82¢\82é\82©\8c\9f\8dõ
417         iterator rslt = std::find_if(begin,end,listenerPre);
418
419
420         //\83\8a\83X\83i\81[\83f\81[\83^\82ª\93o\98^\82³\82ê\82Ä\82¢\82È\82¢\8fê\8d\87
421         if(rslt == end){
422
423                 //\83\8a\83X\83i\81[\83f\81[\83^\82ð\93o\98^
424                 m_listenerList.push_back(listener);
425                 return m_listenerList.back();
426
427
428
429                 //\83\8a\83X\83i\81[\83f\81[\83^\82ª\93o\98^\82³\82ê\82Ä\82¢\82é\8fê\8d\87\82Å\81A\8dX\90V\83t\83\89\83O\82ªtrue\82Ì\8fê\8d\87
430         } else if(bUpdate == TRUE){
431
432                 //\83\8a\83X\83i\81[\83f\81[\83^\82ð\8dX\90V
433                 *rslt = listener;
434
435                 
436         }
437         
438         return *rslt;
439
440 }
441
442 BOOL CListenerList::ReadProperty(LPCTSTR fileName){
443         BOOL rslt = FALSE;
444         CAtlFile        userSettingFile;
445         ULONGLONG userSettingFileSize;
446         std::vector<char> userSettingBuf;
447         
448         if(SUCCEEDED(userSettingFile.Create(fileName,GENERIC_READ,FILE_SHARE_READ,OPEN_ALWAYS)) == FALSE || userSettingFile.m_h == NULL || SUCCEEDED(userSettingFile.GetSize(userSettingFileSize)) == FALSE){
449
450                 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__));
451                 
452         }
453         userSettingBuf.resize(static_cast<UINT_PTR>(userSettingFileSize / sizeof(char) + (1 * sizeof(char))));
454         userSettingFile.Read(&userSettingBuf[0],userSettingBuf.size());
455         LPSTR userSettingStart = strstr(&userSettingBuf[0],"<");
456         if(userSettingStart != NULL){
457                 CReadUserSettingXML rusx(nlite::listenerList);
458                 rusx.Parse(userSettingStart);
459         }
460
461
462         return rslt;
463 }
464 #define NLITE_ATTRIBUTE_WRITE_STR(s,n)  s->WriteAttributeString(NULL, _tcsstr(TEXT(#n),TEXT("."))  != NULL ? _tcsstr(TEXT(#n),TEXT(".")) + 1 : TEXT(#n), NULL, n)
465
466 #define NLITE_ATTRIBUTE_WRITE_INT(s,n)  \
467         {\
468         TCHAR buffer[sizeof(n) * 8];\
469         _stprintf(buffer,TEXT("%d"),static_cast<INT>(n));\
470         s->WriteAttributeString(NULL, _tcsstr(TEXT(#n),TEXT(".")) + 1, NULL,buffer );\
471 }
472 BOOL CListenerList::WriteProperty(LPCTSTR fileName){
473         BOOL rslt = FALSE;
474
475
476         CComPtr<IXmlWriter> pWriter;
477         CreateXmlWriter(__uuidof(IXmlWriter), reinterpret_cast<void**>(&pWriter), 0);
478         CComPtr<IStream> pStream;
479         SHCreateStreamOnFile(fileName, STGM_CREATE | STGM_WRITE, &pStream);
480
481         pWriter->SetOutput(pStream);
482
483         // \83C\83\93\83f\83\93\83g\97L\8cø\89»
484         pWriter->SetProperty(XmlWriterProperty_Indent, TRUE);
485
486         // <?xml version="1.0" encoding="UTF-8"?>
487         pWriter->WriteStartDocument(XmlStandalone_Omit);
488
489         pWriter->WriteStartElement(NULL,TEXT("listenerData"),NULL);
490
491
492         auto begin = m_listenerList.begin();
493         auto end = m_listenerList.end();
494
495         //\83\8a\83X\83i\81[\83\8a\83X\83g\82©\82ç\95Û\91\91Î\8fÛ\82Ì\83f\81[\83^\82Ì\82Ýxml\8c`\8e®\82Å\95Û\91
496         for(;begin != end;++begin){
497                 if(begin->name.Length() > 0 || begin->nameColor != CListenerData::INIT_COLOR || begin->bgcolor != CListenerData::INIT_COLOR){
498                         pWriter->WriteStartElement(NULL,TEXT("user"),NULL);
499                         if(begin->name.Length() > 0) NLITE_ATTRIBUTE_WRITE_STR(pWriter,(*begin).name);
500                         if(begin->community.Length()> 0) NLITE_ATTRIBUTE_WRITE_STR(pWriter,(*begin).community);
501                         NLITE_ATTRIBUTE_WRITE_INT(pWriter,(*begin).time);
502                         if(begin->bgcolor != CListenerData::INIT_COLOR)NLITE_ATTRIBUTE_WRITE_INT(pWriter,(*begin).bgcolor);
503                         if(begin->nameColor != CListenerData::INIT_COLOR)NLITE_ATTRIBUTE_WRITE_INT(pWriter,(*begin).nameColor);
504                         pWriter->WriteString(begin->user_id);
505                         pWriter->WriteEndElement();
506                 }
507         }
508         pWriter->WriteEndDocument();
509         rslt = TRUE;
510         return rslt;
511 }
512
513
514 CReadUserSettingXML::CReadUserSettingXML(CListenerList &in_listenerList):m_listenerList(in_listenerList),userNodeFlag(FALSE){}
515
516 #define NLITE_READ_USER_STRING(at,l,t) if(_tcscmp(at[0],TEXT(#t)) == 0)l.t = at[1]
517 #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)
518
519         void CReadUserSettingXML::OnStartElement (const XML_Char *pszName, const XML_Char **papszAttrs){
520
521                 if(_tcscmp(pszName,TEXT("user")) == 0){
522
523                         this->userNodeFlag = TRUE;
524
525
526                         for(;papszAttrs[0] != NULL;papszAttrs += 2){
527
528                                 NLITE_READ_USER_STRING(papszAttrs,listenerBuff,name);
529                                 NLITE_READ_USER_STRING(papszAttrs,listenerBuff,community);
530                                 NLITE_READ_USER_INT(papszAttrs,listenerBuff,bgcolor);
531                                 NLITE_READ_USER_INT(papszAttrs,listenerBuff,nameColor);
532                                 NLITE_READ_USER_INT(papszAttrs,listenerBuff,time);
533                                 
534                         }
535
536                 }
537
538                 return;
539         }
540
541         void CReadUserSettingXML::OnEndElement (const XML_Char *pszName){
542
543                 if(_tcscmp(pszName,TEXT("user")) == 0){
544
545                         listenerList.Register(listenerBuff,TRUE);
546                         this->userNodeFlag = FALSE;
547                         this->listenerBuff.~CListenerData();
548                         new(&listenerBuff) CListenerData();
549
550                 }
551
552                 return;
553         }
554
555         void CReadUserSettingXML::OnCharacterData (const XML_Char *pszData, int nLength){
556
557                 if(this->userNodeFlag == TRUE){
558
559                         
560                         if(_tcstol(listenerBuff.user_id,NULL,10) != 0 || ((difftime(time(NULL),604800)) <listenerBuff.time)){
561                                 listenerBuff.user_id.Append(pszData,nLength);
562                         }
563
564
565
566                 }
567
568                 
569
570                 return;
571         }
572
573 }