OSDN Git Service

2012/01/27 15:13:39
[nlite/nlite.git] / nlite / nlite_property.cpp
1 #include "stdafx.h"
2 #include "nlite_include.h"
3
4 namespace nlite{
5
6
7 #define NLITE_GET_TYPENAME(n)                   (_tcsstr(CComBSTR(typeid(n).name()),TEXT("::")) + 2)
8
9
10
11 #define NLITE_XMLELLMENT_WRITE(s,p)             s->WriteStartElement(NULL, NLITE_GET_TYPENAME(p), NULL)
12
13 #define NLITE_ATTRIBUTE_WRITE_STR(s,n)  s->WriteElementString(NULL, _tcsstr(TEXT(#n),TEXT("."))  != NULL ? _tcsstr(TEXT(#n),TEXT(".")) + 1 : TEXT(#n), NULL, n)
14
15
16 #define NLITE_ATTRIBUTE_WRITE_INT(s,n)  \
17         {\
18         TCHAR buffer[sizeof(n) * 8];\
19         _stprintf(buffer,TEXT("%d"),static_cast<INT>(n));\
20         s->WriteElementString(NULL, _tcsstr(TEXT(#n),TEXT(".")) + 1, NULL,buffer );\
21 }
22
23 #define NLITE_XML_WRITE_FOR_INT(s,n)\
24         {\
25                 TCHAR nbuffer[LENGTH_256];\
26                 TCHAR buffer[sizeof(n[0]) * 8];\
27                 for(UINT_PTR index = 0;index < ARRAY_LENGTH(n);index++){\
28                         _stprintf(nbuffer,TEXT("%s%d"),_tcsstr(TEXT(#n),TEXT(".")) + 1,index);\
29                         _stprintf(buffer,TEXT("%d"),n[index]);\
30                         s->WriteElementString(NULL, nbuffer, NULL,buffer );\
31                 }\
32 }
33
34
35
36 static LPCTSTR headerFormat = TEXT("headerholSize%u");
37 static LPCTSTR headerOrderFormat = TEXT("headerOrderArray%u");
38 static const LPCTSTR bt_none = TEXT("none");
39 static const LPCTSTR bt_ie = TEXT("ie");
40 static const LPCTSTR bt_chrome = TEXT("chrome");
41 static const LPCTSTR bt_firefox = TEXT("firefox");
42 static const LPCTSTR bt_iecm = TEXT("iecm");
43
44
45
46 static
47 BOOL
48 WINAPI
49 WritePrivateProfileInt(
50   LPCTSTR lpAppName,
51   LPCTSTR lpKeyName,
52   LONG lData,
53   LPCTSTR lpFileName
54 )
55 {
56   TCHAR value[LENGTH_256];
57
58   _stprintf(value, TEXT("%d"), lData);
59   return WritePrivateProfileString(
60            lpAppName,
61            lpKeyName,
62            value,
63            lpFileName);
64 }
65
66
67 PropertyBase::PropertyBase(LPCTSTR in_sectionName):sectionName(in_sectionName){}
68
69
70 LPCTSTR PropertyBase::GetSectionName(){
71
72         return sectionName;
73
74 }
75
76 BOOL CCookieSettingWindow::OnInitDialog(CWindow wndFocus, LPARAM lInitParam){
77         // \83X\83N\83\8a\81[\83\93\82Ì\92\86\89\9b\82É\94z\92u
78         CenterWindow();
79
80
81         CButton radio;
82
83         switch(this->browserType){
84
85         case BT_INTERNET_EXPLORER:
86
87                 radio = GetDlgItem(IDC_RADIO_IE);
88
89                 break;
90
91         case BT_FIRE_FOX:
92
93                 radio = GetDlgItem(IDC_RADIO_FIREFOX);
94
95                 break;
96
97
98         case BT_GOOGLE_CHROME:
99
100                 radio = GetDlgItem(IDC_RADIO_CHROME);
101
102                 break;
103
104         case BT_IE_COMPONENT:
105
106                 radio = GetDlgItem(IDC_RADIO_IECM);
107
108                 break;
109
110         default:
111                 goto skip;
112         }
113         radio.SetCheck(BST_CHECKED);
114                         
115 skip:
116                         
117         return TRUE;
118 }
119
120 VOID CCookieSettingWindow::OnOK(UINT uNotifyCode, int nID, CWindow wndCtl){
121         CButton ieRadio = GetDlgItem(IDC_RADIO_IE);
122         CButton firefoxRadio = GetDlgItem(IDC_RADIO_FIREFOX);
123         CButton chromeRadio = GetDlgItem(IDC_RADIO_CHROME);
124         CButton iecmRadio = GetDlgItem(IDC_RADIO_IECM);
125
126         if(ieRadio.GetCheck() == BST_CHECKED){
127
128                 this->browserType = BT_INTERNET_EXPLORER;
129
130         }
131
132         if(firefoxRadio.GetCheck() == BST_CHECKED){
133
134                 this->browserType = BT_FIRE_FOX;
135
136         }
137
138         if(chromeRadio.GetCheck() == BST_CHECKED){
139
140                 this->browserType = BT_GOOGLE_CHROME;
141
142         }
143
144         if(iecmRadio.GetCheck() == BST_CHECKED){
145
146                 this->browserType = BT_IE_COMPONENT;
147
148         }
149
150
151         EndDialog(nID);
152 }
153
154 VOID CCookieSettingWindow::OnCancel(UINT uNotifyCode, int nID, CWindow wndCtl){
155         EndDialog(nID);
156 }
157
158 VOID CCookieSettingWindow::SetBrowserType(BROWSERTYPE browserType){
159
160         this->browserType = browserType;
161
162
163         return;
164 }
165
166 BROWSERTYPE CCookieSettingWindow::GetBrowserType(){
167         return this->browserType;
168 }
169
170
171
172 CommentViewProperty::CommentViewProperty():
173         commentFoldingFlag(TRUE),
174         commentTimeType(StreamingTimeType),
175         PropertyBase(TEXT("COMMENTVIEW")),
176         baseStringColor(PALETTERGB(0,0,0)),
177         backColor(PALETTERGB(255,255,255)),
178         selectBackColor(PALETTERGB(255,240,240)),                       
179         selectSellLineColor(PALETTERGB(255,0,0)),                                               
180         normalSellLineColor( PALETTERGB(0,0,0)),
181         colLineColor(PALETTERGB(224,224,224))
182
183 {
184
185         memset(headerholSize,-1,sizeof(headerholSize));
186 }
187
188
189
190
191 CommentReadProperty::CommentReadProperty():
192         commentReadFlag(FALSE),
193         ngWordReadFlag(TRUE),
194         ngUserReadFlag(TRUE),
195         administratorCommentReadFlag(TRUE),
196         bspReadFlag(TRUE),
197         userCommentReadFlag(TRUE),
198         teropReadFlag(TRUE),
199         PropertyBase(TEXT("COMMENTREAD"))
200 {}
201
202
203
204
205 GeneralProperty::GeneralProperty():
206         userCountUpdateFlag(TRUE),
207         commentLogAutoSaveFlag(FALSE),
208         defaultBrowserFlag(TRUE),
209         autoNameRegisterFlag(FALSE),
210         autoNameUpdateFlag(FALSE),
211         numberNameFlag(FALSE),
212         autoColorRegisterFlag(FALSE),
213         activeCountTimeFlag(TRUE),
214         PropertyBase(TEXT("GENERAL")),
215         browserType(BT_NOSETTING)
216 {}
217
218
219
220 MainFrameProperty::MainFrameProperty():
221 windowFrontFlag(TRUE),
222 PropertyBase(TEXT("MAINFRAME"))
223 {
224
225         ZeroMemory(&windowSize,sizeof(windowSize));
226
227 }
228
229
230
231
232 Property::Property()
233 {
234         TCHAR localAppPath[_MAX_PATH];
235         SHGetFolderPathW(0,CSIDL_LOCAL_APPDATA,0,SHGFP_TYPE_CURRENT,localAppPath);
236
237         this->appLocalPath = localAppPath;
238 }
239
240 Property::~Property()
241 {
242                         
243                 
244 }
245
246 VOID Property::Initialize(){
247         this->~Property();
248         new(this) Property;
249
250 }
251
252 BROWSERTYPE Property::CookieSetting(){
253
254         CCookieSettingWindow cookieSettingwindow;
255
256         cookieSettingwindow.SetBrowserType(gp.browserType);
257
258         cookieSettingwindow.DoModal();
259
260         this->gp.browserType = cookieSettingwindow.GetBrowserType();
261
262         return this->gp.browserType;
263
264 }
265
266 VOID Property::ReadProperty(){
267
268         CreatePropertyFile();
269         CAtlFile        propertyFile;
270         ULONGLONG nLen;
271
272         check(SUCCEEDED(propertyFile.Create(propertyPath,GENERIC_READ,FILE_SHARE_READ,OPEN_ALWAYS)),TEXT("\90Ý\92è\83t\83@\83C\83\8b\82ª\8aJ\82¯\82Ü\82¹\82ñ"));
273         check(SUCCEEDED(propertyFile.GetSize(nLen)),TEXT("\90Ý\92è\83t\83@\83C\83\8b\90Ý\92è\8eæ\93¾\82É\8e¸\94s\82µ\82Ü\82µ\82½"));
274         std::vector<char> buf(static_cast<UINT_PTR>(nLen / sizeof(char) + 1));
275         buf[buf.size() -1 ] = TEXT('\0'); 
276         propertyFile.Read(&buf[0],buf.size());
277         ReadPropertyXML reader(*this);
278         reader.Parse(&buf[0],buf.size());
279         
280         return;
281 }
282
283
284 VOID Property::CreatePropertyFile(){
285
286         propertyPath = appLocalPath;
287
288         propertyPath += TEXT("\\qwerty_nico");
289
290         if(PathIsDirectory(propertyPath) == FALSE){
291
292                 if(CreateDirectory(propertyPath,NULL) == 0){
293                         throw Exception(TEXT("\90Ý\92è\83t\83@\83C\83\8b\8dì\90¬\82É\8e¸\94s\82µ\82Ü\82µ\82½"),__LINE__,TEXT(__FILE__),TEXT(__FUNCTION__));
294                 }
295
296         }
297
298         propertyPath += TEXT("\\nlite");
299
300         if(PathIsDirectory(propertyPath) == FALSE){
301
302                 if(CreateDirectory(propertyPath,NULL) == 0){
303                         throw Exception(TEXT("\90Ý\92è\83t\83@\83C\83\8b\8dì\90¬\82É\8e¸\94s\82µ\82Ü\82µ\82½"),__LINE__,TEXT(__FILE__),TEXT(__FUNCTION__));
304                 }
305
306         }
307
308         propertyPath += TEXT("\\property.xml");
309
310                 
311         if(PathFileExists(propertyPath)== FALSE || PathIsDirectory(propertyPath) == TRUE){
312                 
313                 HANDLE hPropertyxml =   CreateFile(propertyPath,GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
314
315                 if(hPropertyxml == INVALID_HANDLE_VALUE ){
316
317                         throw Exception(TEXT("\90Ý\92è\83t\83@\83C\83\8b\8dì\90¬\82É\8e¸\94s\82µ\82Ü\82µ\82½"),__LINE__,TEXT(__FILE__),TEXT(__FUNCTION__));
318
319                 }
320
321
322                 CloseHandle(hPropertyxml);
323                 
324         }
325
326         return;
327 }
328
329
330 VOID Property::WriteProperty(){
331
332         CreatePropertyFile();
333         CComPtr<IXmlWriter> pWriter;
334     CreateXmlWriter(__uuidof(IXmlWriter), reinterpret_cast<void**>(&pWriter), 0);
335
336     // XML\83t\83@\83C\83\8b\83p\83X\8dì\90¬
337     TCHAR xml[MAX_PATH];
338         _tcscpy(xml,this->propertyPath);
339
340         
341         
342          // \83t\83@\83C\83\8b\83X\83g\83\8a\81[\83\80\8dì\90¬
343     CComPtr<IStream> pStream;
344     SHCreateStreamOnFile(xml, STGM_CREATE | STGM_WRITE, &pStream);
345
346         pWriter->SetOutput(pStream);
347         
348         // \83C\83\93\83f\83\93\83g\97L\8cø\89»
349     pWriter->SetProperty(XmlWriterProperty_Indent, TRUE);
350
351         // <?xml version="1.0" encoding="UTF-8"?>
352     pWriter->WriteStartDocument(XmlStandalone_Omit);
353
354         pWriter->WriteStartElement(NULL,TEXT("nliteconfig"),NULL);
355         //\83R\83\81\83\93\83g\93Ç\82Ý\8fã\82°\90Ý\92è\8f\91\82«\8d\9e\82Ý
356         NLITE_XMLELLMENT_WRITE(pWriter,crp);
357         NLITE_ATTRIBUTE_WRITE_STR(pWriter,crp.outString);
358         NLITE_ATTRIBUTE_WRITE_INT(pWriter,crp.administratorCommentReadFlag);
359         NLITE_ATTRIBUTE_WRITE_INT(pWriter,crp.besideLengthFlag);
360         NLITE_ATTRIBUTE_WRITE_INT(pWriter,crp.bspReadFlag);
361         NLITE_ATTRIBUTE_WRITE_INT(pWriter,crp.commentReadFlag);
362         NLITE_ATTRIBUTE_WRITE_INT(pWriter,crp.maxCommentLength);
363         NLITE_ATTRIBUTE_WRITE_INT(pWriter,crp.newLineReadFlag);
364         NLITE_ATTRIBUTE_WRITE_INT(pWriter,crp.ngUserReadFlag);
365         NLITE_ATTRIBUTE_WRITE_INT(pWriter,crp.ngWordReadFlag);
366         NLITE_ATTRIBUTE_WRITE_STR(pWriter,crp.shortComment);
367         NLITE_ATTRIBUTE_WRITE_INT(pWriter,crp.teropReadFlag);
368         NLITE_ATTRIBUTE_WRITE_INT(pWriter,crp.userCommentReadFlag);
369         pWriter->WriteEndElement();
370         //commentview\90Ý\92è\92l\8f\91\82«\8d\9e\82Ý
371         NLITE_XMLELLMENT_WRITE(pWriter,cvp);
372         NLITE_XML_WRITE_FOR_INT(pWriter,cvp.headerholSize);
373         NLITE_ATTRIBUTE_WRITE_INT(pWriter,cvp.anonymousIDLinkColor);
374         NLITE_ATTRIBUTE_WRITE_INT(pWriter,cvp.backColor);
375         NLITE_ATTRIBUTE_WRITE_INT(pWriter,cvp.baseStringColor);
376         NLITE_ATTRIBUTE_WRITE_INT(pWriter,cvp.commentFoldingFlag);
377         NLITE_ATTRIBUTE_WRITE_INT(pWriter,cvp.commentTimeType);
378         NLITE_XML_WRITE_FOR_INT(pWriter,cvp.headerOraderArray);
379         NLITE_ATTRIBUTE_WRITE_INT(pWriter,cvp.IDLinkColor);
380         NLITE_ATTRIBUTE_WRITE_INT(pWriter,cvp.selectBackColor);
381         NLITE_ATTRIBUTE_WRITE_INT(pWriter,cvp.selectSellLineColor);
382         NLITE_ATTRIBUTE_WRITE_INT(pWriter,cvp.colLineColor);
383         NLITE_ATTRIBUTE_WRITE_INT(pWriter,cvp.normalSellLineColor);
384
385         
386         pWriter->WriteEndElement();
387
388
389         NLITE_XMLELLMENT_WRITE(pWriter,mfp);
390         NLITE_ATTRIBUTE_WRITE_INT(pWriter,mfp.windowSize.cx);
391         NLITE_ATTRIBUTE_WRITE_INT(pWriter,mfp.windowSize.cy);
392         NLITE_ATTRIBUTE_WRITE_INT(pWriter,mfp.windowFrontFlag);
393         pWriter->WriteEndElement();
394
395         NLITE_XMLELLMENT_WRITE(pWriter,gp);
396         NLITE_ATTRIBUTE_WRITE_INT(pWriter,gp.activeCountTimeFlag);
397         NLITE_ATTRIBUTE_WRITE_INT(pWriter,gp.autoColorRegisterFlag);
398         NLITE_ATTRIBUTE_WRITE_INT(pWriter,gp.autoNameUpdateFlag);
399         NLITE_ATTRIBUTE_WRITE_INT(pWriter,gp.autoNameRegisterFlag);
400         NLITE_ATTRIBUTE_WRITE_STR(pWriter,gp.browserPath);
401         NLITE_ATTRIBUTE_WRITE_INT(pWriter,gp.browserType);
402         NLITE_ATTRIBUTE_WRITE_INT(pWriter,gp.commentLogAutoSaveFlag);
403         NLITE_ATTRIBUTE_WRITE_STR(pWriter,gp.commentPath);
404         NLITE_ATTRIBUTE_WRITE_INT(pWriter,gp.defaultBrowserFlag);
405         NLITE_ATTRIBUTE_WRITE_INT(pWriter,gp.numberNameFlag);
406         NLITE_ATTRIBUTE_WRITE_INT(pWriter,gp.userCountUpdateFlag);
407         pWriter->WriteEndElement();
408         pWriter->WriteEndDocument();
409
410         pWriter->Flush();
411
412         return;
413 }
414
415
416 ///
417 ///\83R\83\93\83X\83g\83\89\83N\83^
418 ///
419 ReadPropertyXML::ReadPropertyXML(Property &in_property):nliteProperty(in_property),propertyType(Node::None){
420
421         return;
422 }
423
424
425 #define NLITE_PSZNAME_CMP(t,p) (_tcscmp(NLITE_GET_TYPENAME(t),(LPCTSTR)(p)) == 0)
426 void ReadPropertyXML::OnStartElement (const XML_Char *pszName, const XML_Char **papszAttrs){
427
428         switch(propertyType){
429
430         case Node::None:
431
432                 if(NLITE_PSZNAME_CMP(nliteProperty.crp,pszName)){
433
434                         propertyType = Node::CommentReadProperty;
435
436                 } else if(NLITE_PSZNAME_CMP(nliteProperty.cvp,pszName)){
437
438                         propertyType = Node::CommentViewProperty;
439
440                 } else if(NLITE_PSZNAME_CMP(nliteProperty.mfp,pszName)){
441
442                         propertyType = Node::MainFrameProperty;
443
444                 } else if(NLITE_PSZNAME_CMP(nliteProperty.gp,pszName)){
445
446                         propertyType = Node::GeneralProperty;
447
448                 }
449
450                 break;
451
452
453         case Node::CommentReadProperty:
454         case Node::CommentViewProperty:
455         case Node::GeneralProperty:
456         case Node::MainFrameProperty:
457
458                 nodeName = (LPCTSTR)pszName;
459                 break;
460
461         default:
462
463                 break;
464         }
465
466         return;
467 }
468
469
470
471
472 void ReadPropertyXML::OnEndElement (const XML_Char *pszName){
473
474         if(NLITE_PSZNAME_CMP(nliteProperty.crp,pszName) || 
475                 NLITE_PSZNAME_CMP(nliteProperty.cvp,pszName)||
476                 NLITE_PSZNAME_CMP(nliteProperty.gp,pszName) || 
477                 NLITE_PSZNAME_CMP(nliteProperty.mfp,pszName)){
478
479                         propertyType = Node::None;
480                         
481         }
482         nodeName = TEXT("");
483
484         return;
485 }
486
487 #define NLITE_R_X_P_S(p,p2,v) \
488         if(_tcscmp(TEXT(#v),nodeName.c_str()) == 0){\
489                 _tcsncpy(p.p2.v,(LPCTSTR)pszData,nLength);\
490                 break;\
491         }
492
493 #define NLITE_R_X_P_I(p,p2,v) \
494         if(_tcscmp(TEXT(#v),nodeName.c_str()) == 0){\
495                 p.p2.v = 0;\
496                 strToIntN(p.p2.v,pszData,nLength);\
497                 break;\
498         }
499
500 #define NLITE_RXP_AI(p,p2,v) {\
501         TCHAR name[LENGTH_256];\
502         for(INT_PTR index = 0;index < ARRAY_LENGTH(p.p2.v);index++){\
503                 _stprintf(name,TEXT("%s%d"),TEXT(#v),index);\
504                 if(_tcscmp(name,nodeName.c_str()) == 0){\
505                         p.p2.v[index] = 0;\
506                         strToIntN(p.p2.v[index],pszData,nLength);\
507                         goto end;\
508                 }\
509         }\
510 }
511
512 #define NLITE_NAME(n) (#n)
513
514 void ReadPropertyXML::OnCharacterData (const XML_Char *pszData, int nLength){
515
516         if (nodeName.empty() == true)return;
517
518         switch(propertyType){
519
520
521
522         case Node::CommentReadProperty:
523                 
524                 NLITE_R_X_P_S(nliteProperty,crp,shortComment);
525                 NLITE_R_X_P_I(nliteProperty,crp,administratorCommentReadFlag);
526                 NLITE_R_X_P_I(nliteProperty,crp,besideLengthFlag);
527                 NLITE_R_X_P_I(nliteProperty,crp,bspReadFlag);
528                 NLITE_R_X_P_I(nliteProperty,crp,commentReadFlag);
529                 NLITE_R_X_P_I(nliteProperty,crp,maxCommentLength);
530                 NLITE_R_X_P_I(nliteProperty,crp,newLineReadFlag);
531                 NLITE_R_X_P_I(nliteProperty,crp,ngUserReadFlag);
532                 NLITE_R_X_P_I(nliteProperty,crp,ngWordReadFlag);
533                 NLITE_R_X_P_S(nliteProperty,crp,outString);
534                 NLITE_R_X_P_S(nliteProperty,crp,shortComment);
535                 NLITE_R_X_P_I(nliteProperty,crp,teropReadFlag);
536                 NLITE_R_X_P_I(nliteProperty,crp,userCommentReadFlag);
537
538                 break;
539         case Node::CommentViewProperty:
540                 NLITE_R_X_P_I(nliteProperty,cvp,anonymousIDLinkColor);
541                 NLITE_R_X_P_I(nliteProperty,cvp,backColor);
542                 NLITE_R_X_P_I(nliteProperty,cvp,baseStringColor);
543                 NLITE_R_X_P_I(nliteProperty,cvp,commentFoldingFlag);
544                 NLITE_R_X_P_I(nliteProperty,cvp,commentTimeType);
545                 NLITE_R_X_P_I(nliteProperty,cvp,IDLinkColor);
546                 NLITE_RXP_AI(nliteProperty,cvp,headerholSize);
547                 NLITE_RXP_AI(nliteProperty,cvp,headerOraderArray);
548                 NLITE_R_X_P_I(nliteProperty,cvp,selectBackColor);
549                 NLITE_R_X_P_I(nliteProperty,cvp,selectSellLineColor);
550                 NLITE_R_X_P_I(nliteProperty,cvp,normalSellLineColor);
551                 NLITE_R_X_P_I(nliteProperty,cvp,colLineColor);
552                 break;
553         case Node::GeneralProperty:
554                 NLITE_R_X_P_I(nliteProperty,gp,activeCountTimeFlag);
555                 NLITE_R_X_P_I(nliteProperty,gp,autoColorRegisterFlag);
556                 NLITE_R_X_P_I(nliteProperty,gp,autoNameRegisterFlag);
557                 NLITE_R_X_P_I(nliteProperty,gp,autoNameUpdateFlag);
558                 NLITE_R_X_P_S(nliteProperty,gp,browserPath);
559                 NLITE_R_X_P_I(nliteProperty,gp,browserType);
560                 NLITE_R_X_P_I(nliteProperty,gp,commentLogAutoSaveFlag);
561                 NLITE_R_X_P_S(nliteProperty,gp,commentPath);
562                 NLITE_R_X_P_I(nliteProperty,gp,defaultBrowserFlag);
563                 NLITE_R_X_P_I(nliteProperty,gp,numberNameFlag);
564                 NLITE_R_X_P_I(nliteProperty,gp,userCountUpdateFlag);
565                 break;
566         case Node::MainFrameProperty:
567                 NLITE_R_X_P_I(nliteProperty,mfp,windowSize.cx);
568                 NLITE_R_X_P_I(nliteProperty,mfp,windowSize.cy);
569                 NLITE_R_X_P_I(nliteProperty,mfp,windowFrontFlag);
570                 break;
571
572
573         default:
574
575                 break;
576
577
578         }
579
580 end:
581
582         return;
583 }
584
585 }