FileFind.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include <WinInterface.h>
  6. #include <VCLCommon.h>
  7. #include <TextsWin.h>
  8. #include <WinConfiguration.h>
  9. #include <CoreMain.h>
  10. #include <Tools.h>
  11. #include "FileFind.h"
  12. //---------------------------------------------------------------------------
  13. #pragma package(smart_init)
  14. #pragma link "HistoryComboBox"
  15. #pragma link "IEListView"
  16. #pragma link "NortonLikeListView"
  17. #ifndef NO_RESOURCES
  18. #pragma resource "*.dfm"
  19. #endif
  20. //---------------------------------------------------------------------------
  21. bool __fastcall DoFileFindDialog(UnicodeString Directory,
  22. TFindEvent OnFind, UnicodeString & Path)
  23. {
  24. bool Result;
  25. TFileFindDialog * Dialog = new TFileFindDialog(Application, OnFind);
  26. try
  27. {
  28. Result = Dialog->Execute(Directory, Path);
  29. }
  30. __finally
  31. {
  32. delete Dialog;
  33. }
  34. return Result;
  35. }
  36. //---------------------------------------------------------------------------
  37. __fastcall TFileFindDialog::TFileFindDialog(TComponent * Owner, TFindEvent OnFind)
  38. : TForm(Owner)
  39. {
  40. UseSystemSettings(this);
  41. FOnFind = OnFind;
  42. FState = ffInit;
  43. FMinimizedByMe = false;
  44. InstallPathWordBreakProc(MaskEdit);
  45. InstallPathWordBreakProc(RemoteDirectoryEdit);
  46. FixComboBoxResizeBug(MaskEdit);
  47. FixComboBoxResizeBug(RemoteDirectoryEdit);
  48. HintLabel(MaskHintText,
  49. FORMAT(L"%s\n \n%s\n \n%s\n \n%s\n \n%s\n \n%s", (LoadStr(MASK_HINT2),
  50. LoadStr(FILE_MASK_EX_HINT), LoadStr(COMBINING_MASKS_HINT),
  51. LoadStr(PATH_MASK_HINT2), LoadStr(DIRECTORY_MASK_HINT),
  52. LoadStr(MASK_HELP))));
  53. FSystemImageList = SharedSystemImageList(false);
  54. FileView->SmallImages = FSystemImageList;
  55. if (!IsGlobalMinimizeHandler())
  56. {
  57. SetGlobalMinimizeHandler(GlobalMinimize);
  58. };
  59. }
  60. //---------------------------------------------------------------------------
  61. __fastcall TFileFindDialog::~TFileFindDialog()
  62. {
  63. if (GetGlobalMinimizeHandler() == GlobalMinimize)
  64. {
  65. SetGlobalMinimizeHandler(NULL);
  66. }
  67. Clear();
  68. delete FSystemImageList;
  69. }
  70. //---------------------------------------------------------------------------
  71. bool __fastcall TFileFindDialog::IsFinding()
  72. {
  73. return (FState == ffFinding) || (FState == ffAborting);
  74. }
  75. //---------------------------------------------------------------------------
  76. void __fastcall TFileFindDialog::UpdateControls()
  77. {
  78. bool Finding = IsFinding();
  79. Caption = LoadStr(Finding ? FIND_FILE_FINDING : FIND_FILE_TITLE);
  80. UnicodeString StartStopCaption;
  81. if (Finding)
  82. {
  83. EnableControl(StartStopButton, true);
  84. StartStopCaption = LoadStr(FIND_FILE_STOP);
  85. }
  86. else
  87. {
  88. EnableControl(StartStopButton, !RemoteDirectoryEdit->Text.IsEmpty());
  89. StartStopCaption = LoadStr(FIND_FILE_START);
  90. }
  91. StartStopButton->Caption = StartStopCaption;
  92. CancelButton->Visible = !Finding;
  93. EnableControl(FilterGroup, !Finding);
  94. MinimizeButton->Visible = Finding;
  95. EnableControl(FocusButton, (FileView->ItemFocused != NULL));
  96. switch (FState)
  97. {
  98. case ffInit:
  99. StatusBar->SimpleText = L"";
  100. case ffFinding:
  101. case ffAborting:
  102. if (!FFindingInDirectory.IsEmpty())
  103. {
  104. StatusBar->SimpleText = FMTLOAD(FIND_FILE_IN_DIRECTORY, (FFindingInDirectory));
  105. }
  106. else
  107. {
  108. StatusBar->SimpleText = L"";
  109. }
  110. break;
  111. case ffAborted:
  112. StatusBar->SimpleText = LoadStr(FIND_FILE_ABORTED);
  113. break;
  114. case ffDone:
  115. StatusBar->SimpleText = LoadStr(FIND_FILE_DONE);
  116. break;
  117. default:
  118. assert(false);
  119. break;
  120. }
  121. }
  122. //---------------------------------------------------------------------------
  123. void __fastcall TFileFindDialog::ControlChange(TObject * /*Sender*/)
  124. {
  125. UpdateControls();
  126. }
  127. //---------------------------------------------------------------------------
  128. bool __fastcall TFileFindDialog::Execute(UnicodeString Directory, UnicodeString & Path)
  129. {
  130. MaskEdit->Text = WinConfiguration->SelectMask;
  131. RemoteDirectoryEdit->Text = UnixExcludeTrailingBackslash(Directory);
  132. // have to set history after value, to prevent autocompletition
  133. MaskEdit->Items = WinConfiguration->History[L"Mask"];
  134. RemoteDirectoryEdit->Items = CustomWinConfiguration->History[L"RemoteDirectory"];
  135. bool Result = (ShowModal() != mrCancel);
  136. if (Result)
  137. {
  138. Path = static_cast<TRemoteFile *>(FileView->ItemFocused->Data)->FullFileName;
  139. }
  140. TFindFileConfiguration FormConfiguration = CustomWinConfiguration->FindFile;
  141. FormConfiguration.ListParams = FileView->ColProperties->ParamsStr;
  142. FormConfiguration.WindowParams = StoreFormSize(this);
  143. CustomWinConfiguration->FindFile = FormConfiguration;
  144. return Result;
  145. }
  146. //---------------------------------------------------------------------------
  147. void __fastcall TFileFindDialog::Clear()
  148. {
  149. for (int Index = 0; Index < FileView->Items->Count; Index++)
  150. {
  151. TListItem * Item = FileView->Items->Item[Index];
  152. TRemoteFile * File = static_cast<TRemoteFile *>(Item->Data);
  153. Item->Data = NULL;
  154. delete File;
  155. }
  156. FileView->Items->Clear();
  157. }
  158. //---------------------------------------------------------------------------
  159. void __fastcall TFileFindDialog::Start()
  160. {
  161. if (MaskEdit->Focused())
  162. {
  163. MaskEditExit(NULL);
  164. }
  165. RemoteDirectoryEdit->SaveToHistory();
  166. CustomWinConfiguration->History[L"RemoteDirectory"] = RemoteDirectoryEdit->Items;
  167. MaskEdit->SaveToHistory();
  168. WinConfiguration->History[L"Mask"] = MaskEdit->Items;
  169. WinConfiguration->SelectMask = MaskEdit->Text;
  170. assert(FState != ffFinding);
  171. FState = ffFinding;
  172. try
  173. {
  174. UpdateControls();
  175. Repaint();
  176. Busy(true);
  177. assert(FOnFind != NULL);
  178. FDirectory = UnixExcludeTrailingBackslash(RemoteDirectoryEdit->Text);
  179. FOnFind(FDirectory, MaskEdit->Text, FileFound, FindingFile);
  180. }
  181. __finally
  182. {
  183. Busy(false);
  184. FFindingInDirectory = L"";
  185. if (FState == ffFinding)
  186. {
  187. FState = ffDone;
  188. }
  189. if (FState == ffAborting)
  190. {
  191. FState = ffAborted;
  192. }
  193. if (IsApplicationMinimized() && FMinimizedByMe)
  194. {
  195. ShowNotification(NULL, LoadStr(BALLOON_OPERATION_COMPLETE), qtInformation);
  196. FMinimizedByMe = false;
  197. }
  198. UpdateControls();
  199. }
  200. }
  201. //---------------------------------------------------------------------------
  202. void __fastcall TFileFindDialog::FileFound(TTerminal * /*Terminal*/,
  203. const UnicodeString FileName, const TRemoteFile * AFile, bool & Cancel)
  204. {
  205. TListItem * Item = FileView->Items->Add();
  206. TRemoteFile * File = AFile->Duplicate(true);
  207. Item->Data = File;
  208. Item->ImageIndex = File->IconIndex;
  209. UnicodeString Caption = File->FileName;
  210. if (File->IsDirectory)
  211. {
  212. Caption = UnixIncludeTrailingBackslash(Caption);
  213. }
  214. Item->Caption = Caption;
  215. UnicodeString Directory = UnixExtractFilePath(File->FullFileName);
  216. if (AnsiSameText(FDirectory, Directory.SubString(1, FDirectory.Length())))
  217. {
  218. Directory[1] = L'.';
  219. Directory.Delete(2, FDirectory.Length() - 1);
  220. }
  221. else
  222. {
  223. assert(false);
  224. }
  225. Item->SubItems->Add(Directory);
  226. if (File->IsDirectory)
  227. {
  228. Item->SubItems->Add(L"");
  229. }
  230. else
  231. {
  232. Item->SubItems->Add(FormatFloat(L"#,##0", File->Size));
  233. }
  234. Item->SubItems->Add(UserModificationStr(File->Modification, File->ModificationFmt));
  235. UpdateControls();
  236. Cancel = (FState == ffAborting);
  237. Application->ProcessMessages();
  238. }
  239. //---------------------------------------------------------------------------
  240. void __fastcall TFileFindDialog::FindingFile(TTerminal * /*Terminal*/,
  241. const UnicodeString Directory, bool & Cancel)
  242. {
  243. if (!Directory.IsEmpty() && (FFindingInDirectory != Directory))
  244. {
  245. FFindingInDirectory = Directory;
  246. UpdateControls();
  247. }
  248. Cancel = (FState == ffAborting);
  249. Application->ProcessMessages();
  250. }
  251. //---------------------------------------------------------------------------
  252. void __fastcall TFileFindDialog::StartStopButtonClick(TObject * /*Sender*/)
  253. {
  254. if (IsFinding())
  255. {
  256. Stop();
  257. }
  258. else
  259. {
  260. Clear();
  261. if (ActiveControl->Parent == FilterGroup)
  262. {
  263. FileView->SetFocus();
  264. }
  265. Start();
  266. }
  267. }
  268. //---------------------------------------------------------------------------
  269. void __fastcall TFileFindDialog::StopButtonClick(TObject * /*Sender*/)
  270. {
  271. Stop();
  272. }
  273. //---------------------------------------------------------------------------
  274. void __fastcall TFileFindDialog::Stop()
  275. {
  276. FState = ffAborting;
  277. UpdateControls();
  278. }
  279. //---------------------------------------------------------------------------
  280. void __fastcall TFileFindDialog::MinimizeButtonClick(TObject * /*Sender*/)
  281. {
  282. MinimizeApp();
  283. }
  284. //---------------------------------------------------------------------------
  285. void __fastcall TFileFindDialog::GlobalMinimize(TObject * /*Sender*/)
  286. {
  287. MinimizeApp();
  288. }
  289. //---------------------------------------------------------------------------
  290. void __fastcall TFileFindDialog::MinimizeApp()
  291. {
  292. Application->Minimize();
  293. FMinimizedByMe = true;
  294. }
  295. //---------------------------------------------------------------------------
  296. void __fastcall TFileFindDialog::FormShow(TObject * /*Sender*/)
  297. {
  298. UpdateFormPosition(this, poOwnerFormCenter);
  299. RestoreFormSize(CustomWinConfiguration->FindFile.WindowParams, this);
  300. FileView->ColProperties->ParamsStr = CustomWinConfiguration->FindFile.ListParams;
  301. UpdateControls();
  302. }
  303. //---------------------------------------------------------------------------
  304. bool __fastcall TFileFindDialog::StopIfFinding()
  305. {
  306. bool Result = IsFinding();
  307. if (Result)
  308. {
  309. Stop();
  310. }
  311. return Result;
  312. }
  313. //---------------------------------------------------------------------------
  314. void __fastcall TFileFindDialog::FormCloseQuery(TObject * /*Sender*/,
  315. bool & /*CanClose*/)
  316. {
  317. StopIfFinding();
  318. }
  319. //---------------------------------------------------------------------------
  320. void __fastcall TFileFindDialog::HelpButtonClick(TObject * /*Sender*/)
  321. {
  322. FormHelp(this);
  323. }
  324. //---------------------------------------------------------------------------
  325. void __fastcall TFileFindDialog::FormKeyDown(TObject * /*Sender*/, WORD & Key,
  326. TShiftState /*Shift*/)
  327. {
  328. if ((Key == VK_ESCAPE) && StopIfFinding())
  329. {
  330. Key = 0;
  331. }
  332. }
  333. //---------------------------------------------------------------------------
  334. void __fastcall TFileFindDialog::MaskEditExit(TObject * /*Sender*/)
  335. {
  336. ValidateMaskEdit(MaskEdit);
  337. }
  338. //---------------------------------------------------------------------------
  339. void __fastcall TFileFindDialog::FileViewDblClick(TObject * /*Sender*/)
  340. {
  341. if (FileView->ItemFocused != NULL)
  342. {
  343. StopIfFinding();
  344. ModalResult = FocusButton->ModalResult;
  345. }
  346. }
  347. //---------------------------------------------------------------------------
  348. void __fastcall TFileFindDialog::FocusButtonClick(TObject * /*Sender*/)
  349. {
  350. StopIfFinding();
  351. }
  352. //---------------------------------------------------------------------------
  353. void __fastcall TFileFindDialog::FileViewSelectItem(TObject * /*Sender*/,
  354. TListItem * /*Item*/, bool /*Selected*/)
  355. {
  356. UpdateControls();
  357. }
  358. //---------------------------------------------------------------------------
  359. void __fastcall TFileFindDialog::MaskButtonClick(TObject * /*Sender*/)
  360. {
  361. TFileMasks Masks = MaskEdit->Text;
  362. if (DoEditMaskDialog(Masks))
  363. {
  364. MaskEdit->Text = Masks.Masks;
  365. }
  366. }
  367. //---------------------------------------------------------------------------