| 1 |
#include <windows.h> |
| 2 |
#include <tchar.h> |
| 3 |
#include "Tombo.h" |
| 4 |
#include "resource.h" |
| 5 |
#include "UniConv.h" |
| 6 |
#include "SearchEngine.h" |
| 7 |
#include "SearchTree.h" |
| 8 |
#include "TString.h" |
| 9 |
#include "TomboURI.h" |
| 10 |
#include "DialogTemplate.h" |
| 11 |
#include "Message.h" |
| 12 |
#include "Property.h" |
| 13 |
#include "Repository.h" |
| 14 |
#include "VarBuffer.h" |
| 15 |
#include "URIScanner.h" |
| 16 |
|
| 17 |
//////////////////////////////// |
| 18 |
//////////////////////////////// |
| 19 |
|
| 20 |
class SearchTreeScanner : public URIScanner { |
| 21 |
public: |
| 22 |
BOOL bFound; |
| 23 |
TomboURI *pMatchedURI; |
| 24 |
|
| 25 |
SearchEngineA *pRegex; |
| 26 |
BOOL bSkipOne; |
| 27 |
|
| 28 |
BOOL Init(SearchEngineA *p, const TomboURI *pBase, BOOL bSkipOne, BOOL bSkipEncrypt); |
| 29 |
SearchTreeScanner(); |
| 30 |
|
| 31 |
void Node(); |
| 32 |
|
| 33 |
}; |
| 34 |
|
| 35 |
SearchTreeScanner::SearchTreeScanner() : bFound(FALSE), pMatchedURI(NULL) |
| 36 |
{ |
| 37 |
} |
| 38 |
|
| 39 |
BOOL SearchTreeScanner::Init(SearchEngineA *p, const TomboURI *pBase, BOOL bSOne, BOOL bSEncrypt) |
| 40 |
{ |
| 41 |
pRegex = p; |
| 42 |
bSkipOne = bSOne; |
| 43 |
return URIScanner::Init(&g_Repository, pBase, bSEncrypt); |
| 44 |
} |
| 45 |
|
| 46 |
void SearchTreeScanner::Node() |
| 47 |
{ |
| 48 |
if (bSkipOne) { |
| 49 |
bSkipOne = FALSE; |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
SearchResult result = pRegex->SearchFromURI(CurrentURI()); |
| 54 |
switch(result) { |
| 55 |
case SR_FOUND: |
| 56 |
bFound = TRUE; |
| 57 |
pMatchedURI = new TomboURI(*CurrentURI()); |
| 58 |
StopScan(); |
| 59 |
break; |
| 60 |
case SR_NOTFOUND: |
| 61 |
break; |
| 62 |
case SR_FAILED: |
| 63 |
StopScan(); |
| 64 |
break; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
//////////////////////////////// |
| 69 |
// ctor & dtor |
| 70 |
//////////////////////////////// |
| 71 |
|
| 72 |
BOOL SearchTree::Init(SearchEngineA *p, const TomboURI *pURI, BOOL bDForward, BOOL bSOne, BOOL bSkipEncrypt) |
| 73 |
{ |
| 74 |
pStartURI = new TomboURI(*pURI); |
| 75 |
if (pStartURI == NULL) { SetLastError(ERROR_NOT_ENOUGH_MEMORY); return FALSE; } |
| 76 |
pRegex = p; |
| 77 |
bSearchDirectionForward = bDForward; |
| 78 |
bSkipOne = bSOne; |
| 79 |
bSearchEncryptedMemo = !bSkipEncrypt; |
| 80 |
return TRUE; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
SearchTree::~SearchTree() |
| 85 |
{ |
| 86 |
delete pScanner; |
| 87 |
delete pStartURI; |
| 88 |
delete pMatchedURI; |
| 89 |
} |
| 90 |
|
| 91 |
//////////////////////////////// |
| 92 |
// Dialog procedure |
| 93 |
//////////////////////////////// |
| 94 |
|
| 95 |
static LRESULT CALLBACK SearchTreeDlgProc(HWND hDlg, UINT nMessage, WPARAM wParam, LPARAM lParam) |
| 96 |
{ |
| 97 |
SearchTree *pDlg; |
| 98 |
if (nMessage == WM_INITDIALOG) { |
| 99 |
SetWindowLong(hDlg, DWL_USER, lParam); |
| 100 |
pDlg = (SearchTree*)lParam; |
| 101 |
|
| 102 |
pDlg->InitDialog(hDlg); |
| 103 |
return TRUE; |
| 104 |
} |
| 105 |
|
| 106 |
pDlg = (SearchTree*)GetWindowLong(hDlg, DWL_USER); |
| 107 |
if (pDlg == NULL) return FALSE; |
| 108 |
|
| 109 |
switch(nMessage) { |
| 110 |
case WM_COMMAND: |
| 111 |
if (LOWORD(wParam) == IDOK) { |
| 112 |
pDlg->OnClose(hDlg, LOWORD(wParam)); |
| 113 |
return TRUE; |
| 114 |
} else if (LOWORD(wParam) == IDCANCEL) { |
| 115 |
pDlg->CancelRequest(); |
| 116 |
pDlg->OnClose(hDlg, LOWORD(wParam)); |
| 117 |
return TRUE; |
| 118 |
} |
| 119 |
} |
| 120 |
return FALSE; |
| 121 |
} |
| 122 |
|
| 123 |
//////////////////////////////// |
| 124 |
// Thread function |
| 125 |
//////////////////////////////// |
| 126 |
|
| 127 |
extern "C" static DWORD WINAPI SearchThreadFunc(LPVOID p) |
| 128 |
{ |
| 129 |
SearchTree *pSt = (SearchTree*)p; |
| 130 |
|
| 131 |
// Do search work |
| 132 |
pSt->SetResult(pSt->Search()); |
| 133 |
|
| 134 |
PostMessage(pSt->GetWnd(), WM_COMMAND, MAKEWPARAM(IDOK, 0), NULL); |
| 135 |
return 0; |
| 136 |
} |
| 137 |
|
| 138 |
///////////////////////////////////////// |
| 139 |
// Popup dialog and start searching |
| 140 |
///////////////////////////////////////// |
| 141 |
|
| 142 |
void SearchTree::Popup(HINSTANCE hInst, HWND hParent) |
| 143 |
{ |
| 144 |
DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_SEARCHING), hParent, (DLGPROC)SearchTreeDlgProc, (LONG)this); |
| 145 |
} |
| 146 |
|
| 147 |
///////////////////////////////////////// |
| 148 |
// Initialize |
| 149 |
///////////////////////////////////////// |
| 150 |
|
| 151 |
static DlgMsgRes aDlgMsgRes[] = { |
| 152 |
{ IDCANCEL, MSG_ID_DLG_CMN_CANCEL}, |
| 153 |
}; |
| 154 |
|
| 155 |
void SearchTree::InitDialog(HWND hDlg) |
| 156 |
{ |
| 157 |
OverrideDlgMsg(hDlg, -1, aDlgMsgRes, sizeof(aDlgMsgRes)/sizeof(DlgMsgRes)); |
| 158 |
|
| 159 |
DWORD nThreadId; |
| 160 |
|
| 161 |
hDlgWnd = hDlg; |
| 162 |
srResult = SR_NOTFOUND; |
| 163 |
hSearchThread = CreateThread(NULL, 0, SearchThreadFunc, (LPVOID)this, 0, &nThreadId); |
| 164 |
} |
| 165 |
|
| 166 |
///////////////////////////////////////// |
| 167 |
// Close dialog |
| 168 |
///////////////////////////////////////// |
| 169 |
|
| 170 |
void SearchTree::OnClose(HWND hDlg, WORD nId) |
| 171 |
{ |
| 172 |
if (hSearchThread) { |
| 173 |
WaitForSingleObject(hSearchThread, INFINITE); |
| 174 |
CloseHandle(hSearchThread); |
| 175 |
hSearchThread = NULL; |
| 176 |
} |
| 177 |
EndDialog(hDlg, nId); |
| 178 |
} |
| 179 |
|
| 180 |
///////////////////////////////////////// |
| 181 |
// request cancel |
| 182 |
///////////////////////////////////////// |
| 183 |
|
| 184 |
void SearchTree::CancelRequest() |
| 185 |
{ |
| 186 |
HWND hWnd = GetDlgItem(hDlgWnd, IDC_SEARCHMSG); |
| 187 |
SetWindowText(hWnd, MSG_SEARCH_CANCELING); |
| 188 |
if (pScanner) { |
| 189 |
pScanner->StopScan(); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
///////////////////////////////////////// |
| 194 |
// get current URI |
| 195 |
///////////////////////////////////////// |
| 196 |
|
| 197 |
const TomboURI* SearchTree::CurrentURI() |
| 198 |
{ |
| 199 |
if (pScanner) { |
| 200 |
return pScanner->CurrentURI(); |
| 201 |
} else { |
| 202 |
return NULL; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
///////////////////////////////////////// |
| 207 |
// search main |
| 208 |
///////////////////////////////////////// |
| 209 |
|
| 210 |
SearchResult SearchTree::Search() |
| 211 |
{ |
| 212 |
TomboURI sRoot; |
| 213 |
sRoot.Init(TEXT("tombo://default/")); |
| 214 |
|
| 215 |
pScanner = new SearchTreeScanner(); |
| 216 |
if (!pScanner->Init(pRegex, &sRoot, bSkipOne, !bSearchEncryptedMemo)) return SR_FAILED; |
| 217 |
if (!pScanner->Scan(pStartURI, !bSearchDirectionForward)) { |
| 218 |
return SR_FAILED; |
| 219 |
} |
| 220 |
|
| 221 |
if (pScanner->bFound) { |
| 222 |
pMatchedURI = new TomboURI(*(pScanner->pMatchedURI)); |
| 223 |
return SR_FOUND; |
| 224 |
} |
| 225 |
if (pScanner->IsStopScan()) { |
| 226 |
return SR_CANCELED; |
| 227 |
} |
| 228 |
return SR_NOTFOUND; |
| 229 |
} |