Develop and Download Open Source Software

Browse CVS Repository

Contents of /tombo/Tombo/Src/VFStream.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.24 - (show annotations) (download) (as text)
Wed Aug 23 15:31:54 2006 UTC (17 years, 7 months ago) by hirami
Branch: MAIN
CVS Tags: Tombo_2_0b2, Tombo_2_0b3, Tombo_2_0b1, Tombo_2_0b4, B228, B229, B226, B227, B224, B225, B222, B223, B231, B230, HEAD
Changes since 1.23: +2 -2 lines
File MIME type: text/x-c++src
- Multi repository support(yet completed)
- Fixes to YAE
	* Hang up when *.tdt points invalid position.

1 #include <windows.h>
2 #include <tchar.h>
3 #if defined(PLATFORM_WIN32)
4 #include <wchar.h>
5 #endif
6 #include <commctrl.h>
7 #include "Tombo.h"
8 #include "Message.h"
9 #include "UniConv.h"
10 #include "TString.h"
11 #include "TomboURI.h"
12 #include "Repository.h"
13 #include "File.h"
14 #include "Property.h"
15 #include "MemoSelectView.h"
16 #include "DirectoryScanner.h"
17 #include "VFStream.h"
18 #include "SearchEngine.h"
19 #include "VarBuffer.h"
20 #include "AutoPtr.h"
21 #include "URIScanner.h"
22
23 #include "resource.h"
24 #include "DialogTemplate.h"
25 #include "FilterAddDlg.h"
26
27 #define STORE_INIT_SIZE 100
28 #define STORE_EXTEND_DELTA 50
29
30 ////////////////////////////////////
31 // VFNote
32 ////////////////////////////////////
33
34 VFNote::~VFNote()
35 {
36 delete [] pTitle;
37 delete pURI;
38 }
39
40 BOOL VFNote::Init(const TomboURI *pu, LPCTSTR title)
41 {
42 pURI = new TomboURI(*pu);
43 if (pURI == NULL) { SetLastError(ERROR_NOT_ENOUGH_MEMORY); return FALSE; }
44
45 pTitle = StringDup(title);
46 if (pTitle == NULL) return FALSE;
47
48 if (!g_Repository.GetNoteAttribute(pURI, &uLastUpdate, &uCreateDate, &uFileSize)) return FALSE;
49 return TRUE;
50 }
51
52 ////////////////////////////////////
53 ////////////////////////////////////
54
55 VFStream::VFStream() : pNext(NULL)
56 {
57 }
58
59 VFStream::~VFStream()
60 {
61 }
62
63 BOOL VFStream::SetNext(VFStream *p)
64 {
65 pNext = p;
66 return TRUE;
67 }
68
69 BOOL VFStream::Prepare()
70 {
71 if (pNext) return pNext->Prepare();
72 return TRUE;
73 }
74
75 void VFStream::FreeObject()
76 {
77 if (pNext) pNext->FreeObject();
78 delete pNext;
79 pNext = NULL;
80 }
81
82 BOOL VFStream::PostActivate()
83 {
84 if (pNext) return pNext->PostActivate();
85 return TRUE;
86 }
87
88 BOOL VFStream::NeedEncryptedNote()
89 {
90 return FALSE;
91 }
92
93 ////////////////////////////////////
94 // Traverse directory
95 ////////////////////////////////////
96 // VFDirectoryGenerator's helper class
97
98 class VFolderScanner : public URIScanner {
99 VFStream *pNext;
100 BOOL bCheckEncrypt;
101
102 DWORD nError;
103
104 void InitialScan() { nError = ERROR_SUCCESS; }
105 void Node();
106
107 public:
108
109 BOOL Init(const TomboURI *pURI, VFStream *pNext, BOOL bCheckEncrypt);
110 DWORD GetError() { return nError; }
111 };
112
113 BOOL VFolderScanner::Init(const TomboURI *pURI, VFStream *pN, BOOL bce)
114 {
115 bCheckEncrypt = bce;
116 pNext = pN;
117 return URIScanner::Init(&g_Repository, pURI, !bCheckEncrypt);
118 }
119
120 void VFolderScanner::Node()
121 {
122 const TomboURI *pCur = CurrentURI();
123 LPCTSTR pTitle = GetTitle();
124
125 if (!bCheckEncrypt && g_Repository.IsEncrypted(pCur)) return;
126
127 VFNote *pVF = new VFNote();
128 if (pVF == NULL) {
129 nError = ERROR_NOT_ENOUGH_MEMORY;
130 StopScan();
131 return;
132 }
133 if (!pVF->Init(pCur, pTitle)) {
134 nError = GetLastError();
135 StopScan();
136 delete pVF;
137 return;
138 }
139
140 // pass to the filter.
141 if (!pNext->Store(pVF)) {
142 nError = GetLastError();
143 StopScan();
144 delete pVF;
145 }
146 }
147
148 ////////////////////////////////////
149 // VFDirectoryGenerator
150 ////////////////////////////////////
151
152 VFDirectoryGenerator::VFDirectoryGenerator() : pURI(NULL)
153 {
154 }
155
156 VFDirectoryGenerator::~VFDirectoryGenerator()
157 {
158 delete pURI;
159 }
160
161 BOOL VFDirectoryGenerator::Init(LPCTSTR pDirPath, BOOL bCe)
162 {
163 bCheckEncrypt = bCe;
164 SetDirPath(pDirPath);
165 return TRUE;
166 }
167
168 BOOL VFDirectoryGenerator::Init(const TomboURI *pURI, BOOL bCe)
169 {
170 bCheckEncrypt = bCe;
171 return SetURI(pURI);
172 }
173
174 BOOL VFDirectoryGenerator::SetDirPath(LPCTSTR pPath)
175 {
176 delete pURI;
177 pURI = new TomboURI();
178 if (pURI == NULL) { SetLastError(ERROR_NOT_ENOUGH_MEMORY); return FALSE; }
179 // XXXX get repo name and set it
180 if (!pURI->InitByNotePath(TEXT("default"), pPath)) return FALSE;
181 return TRUE;
182 }
183
184 BOOL VFDirectoryGenerator::SetURI(const TomboURI *p)
185 {
186 delete pURI;
187 pURI = new TomboURI(*p);
188 if (pURI == NULL) { SetLastError(ERROR_NOT_ENOUGH_MEMORY); return FALSE; }
189 return TRUE;
190 }
191
192 BOOL VFDirectoryGenerator::Activate()
193 {
194 if (!pNext) return FALSE;
195
196 BOOL bCE;
197 if (bCheckEncrypt) {
198 bCE = TRUE;
199 } else {
200 bCE = FALSE;
201 VFStream *p = pNext;
202 while(p) {
203 if (p->NeedEncryptedNote()) {
204 bCE = TRUE;
205 }
206 p = p->GetNext();
207 }
208 }
209 VFolderScanner vfs;
210 if (!vfs.Init(pURI, pNext, bCE)) return FALSE;
211 return vfs.FullScan();
212 }
213
214 BOOL VFDirectoryGenerator::Store(VFNote *p)
215 {
216 // usually, this member is not called.
217 return FALSE;
218 }
219
220 VFStream *VFDirectoryGenerator::Clone(VFStore **ppTail)
221 {
222 VFDirectoryGenerator *p = new VFDirectoryGenerator();
223 if (p == NULL) return NULL;
224
225 p->bCheckEncrypt = bCheckEncrypt;
226 p->pNext = pNext->Clone(ppTail);
227 if (!p || p->pNext == NULL) {
228 delete p;
229 return NULL;
230 }
231 p->pURI = new TomboURI(*pURI);
232
233 return p;
234 }
235
236 BOOL VFDirectoryGenerator::GenerateXMLOpenTag(File *pFile)
237 {
238 pNext->GenerateXMLOpenTag(pFile);
239
240 TString sDirPath;
241 if (!pURI->GetFilePath(&sDirPath)) return FALSE;
242
243 if (!pFile->WriteUnicodeString(L"<src folder=\"")) return FALSE;
244 LPWSTR pDirW = ConvTCharToWChar(sDirPath.Get());
245 if (!pDirW) return FALSE;
246 if (!pFile->WriteUnicodeString(pDirW)) {
247 delete [] pDirW;
248 return FALSE;
249 }
250 delete [] pDirW;
251 if (!pFile->WriteUnicodeString(L"\"")) return FALSE;
252 if (bCheckEncrypt) {
253 if (!pFile->WriteUnicodeString(L" checkencrypt='True'")) return FALSE;
254 }
255 if (!pFile->WriteUnicodeString(L"/>\n")) return FALSE;
256
257 return TRUE;
258 }
259
260 BOOL VFDirectoryGenerator::GenerateXMLCloseTag(File *pFile)
261 {
262 return pNext->GenerateXMLCloseTag(pFile);
263 }
264
265
266 LPCTSTR VFDirectoryGenerator::GetFilterType()
267 {
268 return NULL;
269 }
270
271 BOOL VFDirectoryGenerator::ToString(TString *p)
272 {
273 return FALSE;
274 }
275
276
277 BOOL VFDirectoryGenerator::UpdateParamWithDialog(HINSTANCE hInst, HWND hParent)
278 {
279 return FALSE;
280 }
281
282 ////////////////////////////////////
283 // VFStore implimentation
284 ////////////////////////////////////
285
286 VFStore::VFStore()
287 {
288 }
289
290 VFStore::~VFStore()
291 {
292 }
293
294 BOOL VFStore::Init()
295 {
296 return vNotes.Init(STORE_INIT_SIZE, STORE_EXTEND_DELTA);
297 }
298
299 void VFStore::FreeObject()
300 {
301 /* NOP */
302 }
303
304 BOOL VFStore::Prepare()
305 {
306 if (!vNotes.Clear(TRUE)) return FALSE;
307 return TRUE;
308 }
309
310 BOOL VFStore::Store(VFNote *p)
311 {
312 return vNotes.Add(&p);
313 }
314
315 BOOL VFStore::PostActivate()
316 {
317 // TODO: sorting.
318 return TRUE;
319 }
320
321 void VFStore::FreeArray()
322 {
323 DWORD n = vNotes.NumItems();
324 for (DWORD i = 0; i < n; i++) {
325 delete (*vNotes.GetUnit(i));
326 }
327 vNotes.Clear(FALSE);
328 }
329
330 VFStream *VFStore::Clone(VFStore **ppTail)
331 {
332 VFStore *p = new VFStore();
333 if (!p || !p->Init()) return NULL;
334 *ppTail = p;
335 return p;
336 }
337
338 BOOL VFStore::GenerateXMLOpenTag(File *pFile)
339 {
340 return TRUE;
341 }
342
343 BOOL VFStore::GenerateXMLCloseTag(File *pFile)
344 {
345 return TRUE;
346 }
347
348 LPCTSTR VFStore::GetFilterType()
349 {
350 return NULL;
351 }
352
353 BOOL VFStore::ToString(TString *p)
354 {
355 return FALSE;
356 }
357
358 BOOL VFStore::UpdateParamWithDialog(HINSTANCE hInst, HWND hParent)
359 {
360 return FALSE;
361 }
362
363 ////////////////////////////////////
364 // VFRegexFilter
365 ////////////////////////////////////
366
367 VFRegexFilter::VFRegexFilter() : pRegex(NULL), pPassMgr(NULL)
368 {
369 }
370
371 VFRegexFilter::~VFRegexFilter()
372 {
373 delete pRegex;
374 }
375
376 BOOL VFRegexFilter::Init(LPCTSTR pPat, BOOL bCase, BOOL bEnc, BOOL bFileName, BOOL bNeg, PasswordManager *pPMgr)
377 {
378 pPattern = new TString();
379 if (!pPattern) {
380 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
381 return FALSE;
382 }
383 pPassMgr = pPMgr;
384 return Reset(pPat, bCase, bEnc, bFileName, bNeg);
385 }
386
387 BOOL VFRegexFilter::Reset(LPCTSTR pPat, BOOL bCase, BOOL bEnc, BOOL bFileName, BOOL bNeg)
388 {
389 if (!pPattern->Set(pPat)) return FALSE;
390
391 bCaseSensitive = bCase;
392 bFileNameOnly= bFileName;
393 bNegate = bNeg;
394
395 pRegex = new SearchEngineA();
396 const char *pReason;
397 if (!pRegex || !pRegex->Init(g_Property.GetCodePage(), bEnc, bFileName, pPassMgr)) return FALSE;
398 if (!pRegex->Prepare(pPat, bCase, &pReason)) return FALSE;
399 return TRUE;
400 }
401
402 BOOL VFRegexFilter::Store(VFNote *p)
403 {
404 switch(pRegex->SearchFromURI(p->GetURI())) {
405 case SR_NOTFOUND:
406 if (bNegate) {
407 return pNext->Store(p);
408 } else {
409 delete p; // when discarding, Store() must delete object p.
410 return TRUE;
411 }
412 case SR_FOUND:
413 if (bNegate) {
414 delete p; // when discarding, Store() must delete object p.
415 return TRUE;
416 } else {
417 return pNext->Store(p);
418 }
419 case SR_CANCELED:
420 case SR_FAILED:
421 default:
422 return FALSE;
423 }
424 }
425
426 VFStream *VFRegexFilter::Clone(VFStore **ppTail)
427 {
428 VFRegexFilter *p = new VFRegexFilter();
429 if (p == NULL) return NULL;
430
431 p->pPattern = new TString();
432 p->pPattern->Set(pPattern->Get());
433
434 p->pRegex = pRegex->Clone();
435 p->pNext = pNext->Clone(ppTail);
436
437 p->bCaseSensitive = bCaseSensitive;
438 p->bFileNameOnly = bFileNameOnly;
439 p->bNegate = bNegate;
440
441 if (!p || p->pRegex == NULL ||
442 p->pNext == NULL) {
443 delete p;
444 return NULL;
445 }
446 return p;
447 }
448
449 BOOL VFRegexFilter::GenerateXMLOpenTag(File *pFile)
450 {
451 if (!pNext->GenerateXMLOpenTag(pFile)) return FALSE;
452
453 if (!pFile->WriteUnicodeString(L"<grep pattern=\"")) return FALSE;
454 WString sPatW;
455 if (!sPatW.Set(pPattern)) return FALSE;
456 if (!pFile->WriteUnicodeString(sPatW.Get())) return FALSE;
457 if (!pFile->WriteUnicodeString(L"\"")) return FALSE;
458
459 if (bCaseSensitive) {
460 if (!pFile->WriteUnicodeString(L" casesensitive='True'")) return FALSE;
461 }
462 if (bFileNameOnly) {
463 if (!pFile->WriteUnicodeString(L" filenameonly='True'")) return FALSE;
464 }
465 if (bNegate) {
466 if (!pFile->WriteUnicodeString(L" not='True'")) return FALSE;
467 }
468
469 if (pRegex->IsSearchEncryptMemo()) {
470 if (!pFile->WriteUnicodeString(L" checkencrypt='True'")) return FALSE;
471 }
472
473 if (!pFile->WriteUnicodeString(L">\n")) return FALSE;
474
475 return TRUE;
476 }
477
478 BOOL VFRegexFilter::GenerateXMLCloseTag(File *pFile)
479 {
480 if (!pFile->WriteUnicodeString(L"</grep>\n")) return FALSE;
481 if (!pNext->GenerateXMLCloseTag(pFile)) return FALSE;
482 return TRUE;
483 }
484
485 LPCTSTR VFRegexFilter::GetFilterType()
486 {
487 return MSG_STREAM_NAME_REGEXP;
488 }
489
490 BOOL VFRegexFilter::ToString(TString *p)
491 {
492 return p->Set(pPattern->Get());
493 }
494
495 BOOL VFRegexFilter::UpdateParamWithDialog(HINSTANCE hInst, HWND hParent)
496 {
497 RegexFilterAddDlg ad;
498 if (!ad.Init(pPattern->Get(), bCaseSensitive, pRegex->IsSearchEncryptMemo(), bFileNameOnly, bNegate)) return FALSE;
499 if (ad.Popup(hInst, hParent) == IDOK) {
500 Reset(ad.GetMatchString()->Get(),
501 ad.IsCaseSensitive(), ad.IsCheckEncrypt(),
502 ad.IsCheckFileName(), ad.IsNegate());
503 return TRUE;
504 }
505 return FALSE;
506 }
507
508 BOOL VFRegexFilter::NeedEncryptedNote()
509 {
510 return pRegex->IsSearchEncryptMemo();
511 }
512
513 ////////////////////////////////////
514 // VFLimitFilter
515 ////////////////////////////////////
516
517 VFLimitFilter::VFLimitFilter() : nLimit(0)
518 {
519 }
520
521 VFLimitFilter::~VFLimitFilter()
522 {
523 }
524
525 BOOL VFLimitFilter::Init(DWORD n)
526 {
527 nLimit = n;
528 return TRUE;
529 }
530
531 BOOL VFLimitFilter::Prepare()
532 {
533 nCount = 0;
534 return VFStream::Prepare();
535 }
536
537 BOOL VFLimitFilter::Store(VFNote *p)
538 {
539 if (nCount >= nLimit) {
540 delete p;
541 return TRUE;
542 }
543 nCount++;
544 return pNext->Store(p);
545 }
546
547 VFStream *VFLimitFilter::Clone(VFStore **ppTail)
548 {
549 VFLimitFilter *p = new VFLimitFilter();
550 if (!p || !p->Init(nLimit)) {
551 delete p;
552 return NULL;
553 }
554 p->pNext = pNext->Clone(ppTail);
555 if (!p || p->pNext == NULL) {
556 delete p;
557 return NULL;
558 }
559 return p;
560 }
561
562
563 BOOL VFLimitFilter::GenerateXMLOpenTag(File *pFile)
564 {
565 if (!pNext->GenerateXMLOpenTag(pFile)) return FALSE;
566
567 if (!pFile->WriteUnicodeString(L"<limit number=\"")) return FALSE;
568 WCHAR buf[32];
569 swprintf(buf, L"%d", nLimit);
570 if (!pFile->WriteUnicodeString(buf)) return FALSE;
571
572 if (!pFile->WriteUnicodeString(L"\">\n")) return FALSE;
573 return TRUE;
574 }
575
576 BOOL VFLimitFilter::GenerateXMLCloseTag(File *pFile)
577 {
578 if (!pFile->WriteUnicodeString(L"</limit>\n")) return FALSE;
579 if (!pNext->GenerateXMLCloseTag(pFile)) return FALSE;
580 return TRUE;
581 }
582
583 LPCTSTR VFLimitFilter::GetFilterType()
584 {
585 return MSG_STREAM_NAME_LIMIT;
586 }
587
588 BOOL VFLimitFilter::ToString(TString *p)
589 {
590 if (!p->Alloc(_tcslen(MSG_STREAM_VALUE_LIMIT) + 10)) return FALSE;
591 wsprintf(p->Get(), MSG_STREAM_VALUE_LIMIT, nLimit);
592 return TRUE;
593 }
594
595 BOOL VFLimitFilter::UpdateParamWithDialog(HINSTANCE hInst, HWND hParent)
596 {
597 LimitFilterAddDlg ad;
598 if (!ad.Init(nLimit)) return FALSE;
599 if (ad.Popup(hInst, hParent) == IDOK) {
600 nLimit = ad.GetLimit();
601 return TRUE;
602 }
603 return FALSE;
604 }
605
606 ////////////////////////////////////
607 // VFTimestampFilter
608 ////////////////////////////////////
609
610 VFTimestampFilter::VFTimestampFilter()
611 {
612 }
613
614 VFTimestampFilter::~VFTimestampFilter()
615 {
616 }
617
618 BOOL VFTimestampFilter::Reset(DWORD nDelta, BOOL bNew)
619 {
620 SYSTEMTIME st;
621 GetLocalTime(&st);
622 FILETIME ft;
623 SystemTimeToFileTime(&st, &ft);
624
625 nDeltaDays = nDelta;
626
627 uBase = ((UINT64)ft.dwHighDateTime << 32) | (UINT64)ft.dwLowDateTime;
628 UINT64 d = 0xc92a69c000; // 1 day
629 uBase -= d * nDelta;
630 bNewer = bNew;
631
632 return TRUE;
633 }
634
635 BOOL VFTimestampFilter::Store(VFNote *pNote)
636 {
637 if (bNewer && pNote->GetLastUpdate() > uBase || (!bNewer && pNote->GetLastUpdate() < uBase)) {
638 return pNext->Store(pNote);
639 } else {
640 delete pNote;
641 return TRUE;
642 }
643 }
644
645 VFStream *VFTimestampFilter::Clone(VFStore **ppTail)
646 {
647 VFTimestampFilter *p = new VFTimestampFilter();
648 if (!p) return NULL;
649
650 p->bNewer = bNewer;
651 p->uBase = uBase;
652 p->nDeltaDays = nDeltaDays;
653 p->pNext = pNext->Clone(ppTail);
654 if (!p || p->pNext == NULL) {
655 delete p;
656 return NULL;
657 }
658 return p;
659 }
660
661 BOOL VFTimestampFilter::GenerateXMLOpenTag(File *pFile)
662 {
663 if (!pNext->GenerateXMLOpenTag(pFile)) return FALSE;
664
665 if (!pFile->WriteUnicodeString(L"<timestamp days=\"")) return FALSE;
666 WCHAR buf[32];
667 swprintf(buf, L"%d", nDeltaDays);
668 if (!pFile->WriteUnicodeString(buf)) return FALSE;
669 if (!pFile->WriteUnicodeString(L"\"")) return FALSE;
670
671 if (bNewer) {
672 if (!pFile->WriteUnicodeString(L" newer='True'")) return FALSE;
673 } else {
674 if (!pFile->WriteUnicodeString(L" older='True'")) return FALSE;
675 }
676
677 if (!pFile->WriteUnicodeString(L">\n")) return FALSE;
678 return TRUE;
679 }
680
681 BOOL VFTimestampFilter::GenerateXMLCloseTag(File *pFile)
682 {
683 if (!pFile->WriteUnicodeString(L"</timestamp>\n")) return FALSE;
684 if (!pNext->GenerateXMLCloseTag(pFile)) return FALSE;
685 return TRUE;
686 }
687
688 LPCTSTR VFTimestampFilter::GetFilterType()
689 {
690 return MSG_STREAM_NAME_TIMESTAMP;
691 }
692
693 BOOL VFTimestampFilter::ToString(TString *p)
694 {
695 LPCTSTR pTmpl;
696 if (bNewer) {
697 pTmpl = MSG_STREAM_VALUE_TIMESTAMP_NEWER;
698 } else {
699 pTmpl = MSG_STREAM_VALUE_TIMESTAMP_OLDER;
700 }
701 if (!p->Alloc(_tcslen(pTmpl) + 10)) return FALSE;
702 wsprintf(p->Get(), pTmpl, nDeltaDays);
703 return TRUE;
704 }
705
706 BOOL VFTimestampFilter::UpdateParamWithDialog(HINSTANCE hInst, HWND hParent)
707 {
708 TimestampFilterAddDlg ad;
709 if (!ad.Init(nDeltaDays, bNewer)) return FALSE;
710 if (ad.Popup(hInst, hParent) == IDOK) {
711 return Reset(ad.GetDeltaDay(), ad.IsNewer());
712 }
713 return FALSE;
714 }
715
716 ////////////////////////////////////
717 // VFSortFilter
718 ////////////////////////////////////
719
720 VFSortFilter::VFSortFilter()
721 {
722 }
723
724 VFSortFilter::~VFSortFilter()
725 {
726 }
727
728 BOOL VFSortFilter::Init(SortFuncType sf)
729 {
730 sfType = sf;
731 return vNotes.Init(STORE_INIT_SIZE, STORE_EXTEND_DELTA);
732 }
733
734 BOOL VFSortFilter::Prepare()
735 {
736 vNotes.Clear(TRUE);
737 return VFStream::Prepare();
738 }
739
740 BOOL VFSortFilter::Store(VFNote *p)
741 {
742 if (!vNotes.Add(&p)) return FALSE;
743 return TRUE;
744 }
745
746 extern "C" {
747 typedef int SortFunc(const void *e1, const void *e2);
748 };
749
750 extern "C" static int SortNotes_FileNameAsc(const void *e1, const void *e2)
751 {
752 VFNote *p1 = *(VFNote**)e1;
753 VFNote *p2 = *(VFNote**)e2;
754 return _tcsicmp(p1->GetTitle(), p2->GetTitle());
755 }
756
757 extern "C" static int SortNotes_FileNameDesc(const void *e1, const void *e2)
758 {
759 VFNote *p1 = *(VFNote**)e1;
760 VFNote *p2 = *(VFNote**)e2;
761 return _tcsicmp(p2->GetTitle(), p1->GetTitle());
762 }
763
764 extern "C" static int SortNotes_LastUpdateOlder(const void *e1, const void *e2)
765 {
766 VFNote *p1 = *(VFNote**)e1;
767 VFNote *p2 = *(VFNote**)e2;
768 if (p1->GetLastUpdate() == p2->GetLastUpdate()) return 0;
769 if (p1->GetLastUpdate() > p2->GetLastUpdate()) return 1;
770 else return -1;
771 }
772
773 extern "C" static int SortNotes_LastUpdateNewer(const void *e1, const void *e2)
774 {
775 VFNote *p1 = *(VFNote**)e1;
776 VFNote *p2 = *(VFNote**)e2;
777 if (p2->GetLastUpdate() == p1->GetLastUpdate()) return 0;
778 if (p2->GetLastUpdate() > p1->GetLastUpdate()) return 1;
779 else return -1;
780 }
781
782 extern "C" static int SortNotes_CreateDateOlder(const void *e1, const void *e2)
783 {
784 VFNote *p1 = *(VFNote**)e1;
785 VFNote *p2 = *(VFNote**)e2;
786 if (p1->GetCreateDate() == p2->GetCreateDate()) return 0;
787 if (p1->GetCreateDate() > p2->GetCreateDate()) return 1;
788 else return -1;
789 }
790
791 extern "C" static int SortNotes_CreateDateNewer(const void *e1, const void *e2)
792 {
793 VFNote *p1 = *(VFNote**)e1;
794 VFNote *p2 = *(VFNote**)e2;
795 if (p2->GetCreateDate() == p1->GetCreateDate()) return 0;
796 if (p2->GetCreateDate() > p1->GetCreateDate()) return 1;
797 else return -1;
798 }
799
800 extern "C" static int SortNotes_FileSizeAsc(const void *e1, const void *e2)
801 {
802 VFNote *p1 = *(VFNote**)e1;
803 VFNote *p2 = *(VFNote**)e2;
804
805 if (p1->GetFileSize() == p2->GetFileSize()) return 0;
806 if (p1->GetFileSize() > p2->GetFileSize()) return 1;
807 else return -1;
808 }
809
810 extern "C" static int SortNotes_FileSizeDsc(const void *e1, const void *e2)
811 {
812 VFNote *p1 = *(VFNote**)e1;
813 VFNote *p2 = *(VFNote**)e2;
814
815 if (p2->GetFileSize() == p1->GetFileSize()) return 0;
816 if (p2->GetFileSize() > p1->GetFileSize()) return 1;
817 else return -1;
818 }
819
820 BOOL VFSortFilter::PostActivate()
821 {
822 VFNote *p;
823 DWORD n = vNotes.NumItems();
824
825 SortFunc *pFunc;
826 switch(sfType) {
827 case SortFunc_FileNameAsc:
828 pFunc = SortNotes_FileNameAsc;
829 break;
830 case SortFunc_FileNameDsc:
831 pFunc = SortNotes_FileNameDesc;
832 break;
833 case SortFunc_LastUpdateAsc:
834 pFunc = SortNotes_LastUpdateOlder;
835 break;
836 case SortFunc_LastUpdateDsc:
837 pFunc = SortNotes_LastUpdateNewer;
838 break;
839 case SortFunc_CreateDateAsc:
840 pFunc = SortNotes_CreateDateOlder;
841 break;
842 case SortFunc_CreateDateDsc:
843 pFunc = SortNotes_CreateDateNewer;
844 break;
845 case SortFunc_FileSizeAsc:
846 pFunc = SortNotes_FileSizeAsc;
847 break;
848 case SortFunc_FileSizeDsc:
849 pFunc = SortNotes_FileSizeDsc;
850 break;
851 default:
852 return FALSE;
853 }
854 qsort((LPBYTE)vNotes.GetBuf(), n, sizeof(VFNote*), pFunc);
855 for (DWORD i = 0; i < n; i++) {
856 p = *vNotes.GetUnit(i);
857 if (!pNext->Store(p)) return FALSE;
858 }
859 return VFStream::PostActivate();
860 }
861
862 VFStream *VFSortFilter::Clone(VFStore **ppTail)
863 {
864 VFSortFilter *p = new VFSortFilter();
865 if (!p || !p->Init(sfType)) return NULL;
866
867 p->pNext = pNext->Clone(ppTail);
868 if (!p || p->pNext == NULL) {
869 delete p;
870 return NULL;
871 }
872 return p;
873 }
874
875 BOOL VFSortFilter::GenerateXMLOpenTag(File *pFile)
876 {
877 if (!pNext->GenerateXMLOpenTag(pFile)) return FALSE;
878
879 if (!pFile->WriteUnicodeString(L"<sort func=\"")) return FALSE;
880 LPCWSTR pType;
881 switch(sfType) {
882 case SortFunc_FileNameAsc:
883 pType = L"filename_asc";
884 break;
885 case SortFunc_FileNameDsc:
886 pType = L"filename_dsc";
887 break;
888 case SortFunc_LastUpdateAsc:
889 pType = L"lastupdate_asc";
890 break;
891 case SortFunc_LastUpdateDsc:
892 pType = L"lastupdate_dsc";
893 break;
894 case SortFunc_CreateDateAsc:
895 pType = L"createdate_asc";
896 break;
897 case SortFunc_CreateDateDsc:
898 pType = L"createdate_dsc";
899 break;
900 case SortFunc_FileSizeAsc:
901 pType = L"filesize_asc";
902 break;
903 case SortFunc_FileSizeDsc:
904 pType = L"filesize_dsc";
905 break;
906 default:
907 SetLastError(ERROR_INVALID_DATA);
908 return FALSE;
909 }
910 if (!pFile->WriteUnicodeString(pType)) return FALSE;
911 if (!pFile->WriteUnicodeString(L"\">\n")) return FALSE;
912 return TRUE;
913 }
914
915 BOOL VFSortFilter::GenerateXMLCloseTag(File *pFile)
916 {
917 if (!pFile->WriteUnicodeString(L"</sort>\n")) return FALSE;
918 if (!pNext->GenerateXMLCloseTag(pFile)) return FALSE;
919 return TRUE;
920 }
921
922 LPCTSTR VFSortFilter::GetFilterType()
923 {
924 return MSG_STREAM_NAME_SORT;
925 }
926
927 BOOL VFSortFilter::ToString(TString *p)
928 {
929 LPCTSTR pTmpl;
930 switch(sfType) {
931 case SortFunc_FileNameAsc:
932 pTmpl = MSG_STREAM_VALUE_SORT_FNAME_ASC;
933 break;
934 case SortFunc_FileNameDsc:
935 pTmpl = MSG_STREAM_VALUE_SORT_FNAME_DSC;
936 break;
937 case SortFunc_LastUpdateAsc:
938 pTmpl = MSG_STREAM_VALUE_SORT_LASTUPD_ASC;
939 break;
940 case SortFunc_LastUpdateDsc:
941 pTmpl = MSG_STREAM_VALUE_SORT_LASTUPD_DSC;
942 break;
943 case SortFunc_CreateDateAsc:
944 pTmpl = MSG_STREAM_VALUE_SORT_CREATE_ASC;
945 break;
946 case SortFunc_CreateDateDsc:
947 pTmpl = MSG_STREAM_VALUE_SORT_CREATE_DSC;
948 break;
949 case SortFunc_FileSizeAsc:
950 pTmpl = MSG_STREAM_VALUE_SORT_FILESIZE_ASC;
951 break;
952 case SortFunc_FileSizeDsc:
953 pTmpl = MSG_STREAM_VALUE_SORT_FILESIZE_DSC;
954 break;
955 default:
956 SetLastError(ERROR_INVALID_DATA);
957 return FALSE;
958 }
959 return p->Set(pTmpl);
960 }
961
962 BOOL VFSortFilter::UpdateParamWithDialog(HINSTANCE hInst, HWND hParent)
963 {
964 SortFilterAddDlg ad;
965 if (!ad.Init(sfType)) return FALSE;
966 if (ad.Popup(hInst, hParent) == IDOK) {
967 sfType = ad.GetType();
968 return TRUE;
969 }
970 return FALSE;
971 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26