GUITools.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. //---------------------------------------------------------------------------
  2. #define NO_WIN32_LEAN_AND_MEAN
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include <Consts.hpp>
  6. #include <shlobj.h>
  7. #include <Common.h>
  8. #include "GUITools.h"
  9. #include "GUIConfiguration.h"
  10. #include <TextsWin.h>
  11. #include <TextsCore.h>
  12. #include <CoreMain.h>
  13. #include <SessionData.h>
  14. //---------------------------------------------------------------------------
  15. #pragma package(smart_init)
  16. //---------------------------------------------------------------------------
  17. bool __fastcall FindFile(AnsiString & Path)
  18. {
  19. bool Result = FileExists(Path);
  20. if (!Result)
  21. {
  22. int Len = GetEnvironmentVariable("PATH", NULL, 0);
  23. if (Len > 0)
  24. {
  25. AnsiString Paths;
  26. Paths.SetLength(Len - 1);
  27. GetEnvironmentVariable("PATH", Paths.c_str(), Len);
  28. AnsiString NewPath = FileSearch(ExtractFileName(Path), Paths);
  29. Result = !NewPath.IsEmpty();
  30. if (Result)
  31. {
  32. Path = NewPath;
  33. }
  34. }
  35. }
  36. return Result;
  37. }
  38. //---------------------------------------------------------------------------
  39. bool __fastcall FileExistsEx(AnsiString Path)
  40. {
  41. return FindFile(Path);
  42. }
  43. //---------------------------------------------------------------------------
  44. void __fastcall OpenSessionInPutty(const AnsiString PuttyPath,
  45. TSessionData * SessionData, const AnsiString Password)
  46. {
  47. AnsiString Program, Params, Dir;
  48. SplitCommand(PuttyPath, Program, Params, Dir);
  49. Program = ExpandEnvironmentVariables(Program);
  50. if (FindFile(Program))
  51. {
  52. AnsiString SessionName;
  53. THierarchicalStorage * Storage = NULL;
  54. TSessionData * ExportData = NULL;
  55. try
  56. {
  57. Storage = new TRegistryStorage(Configuration->PuttySessionsKey);
  58. Storage->AccessMode = smReadWrite;
  59. if (Storage->OpenRootKey(true))
  60. {
  61. if (Storage->KeyExists(SessionData->StorageKey))
  62. {
  63. SessionName = SessionData->SessionName;
  64. }
  65. else
  66. {
  67. ExportData = new TSessionData("");
  68. ExportData->Assign(SessionData);
  69. ExportData->Modified = true;
  70. ExportData->Name = GUIConfiguration->PuttySession;
  71. ExportData->Password = "";
  72. ExportData->Save(Storage, true);
  73. SessionName = GUIConfiguration->PuttySession;
  74. }
  75. }
  76. }
  77. __finally
  78. {
  79. delete Storage;
  80. delete ExportData;
  81. }
  82. if (!Params.IsEmpty())
  83. {
  84. Params += " ";
  85. }
  86. if (!Password.IsEmpty())
  87. {
  88. Params += FORMAT("-pw \"%s\" ", (Password));
  89. }
  90. Params += FORMAT("-load \"%s\"", (SessionName));
  91. if (!ExecuteShell(Program, Params))
  92. {
  93. throw Exception(FMTLOAD(EXECUTE_APP_ERROR, (Program)));
  94. }
  95. }
  96. else
  97. {
  98. throw Exception(FMTLOAD(FILE_NOT_FOUND, (Program)));
  99. }
  100. }
  101. //---------------------------------------------------------------------------
  102. bool __fastcall ExecuteShell(const AnsiString Path, const AnsiString Params)
  103. {
  104. return ((int)ShellExecute(NULL, "open", (char*)Path.data(),
  105. (char*)Params.data(), NULL, SW_SHOWNORMAL) > 32);
  106. }
  107. //---------------------------------------------------------------------------
  108. bool __fastcall ExecuteShell(const AnsiString Path, const AnsiString Params,
  109. HANDLE & Handle)
  110. {
  111. bool Result;
  112. TShellExecuteInfo ExecuteInfo;
  113. memset(&ExecuteInfo, 0, sizeof(ExecuteInfo));
  114. ExecuteInfo.cbSize = sizeof(ExecuteInfo);
  115. ExecuteInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
  116. ExecuteInfo.hwnd = Application->Handle;
  117. ExecuteInfo.lpFile = (char*)Path.data();
  118. ExecuteInfo.lpParameters = (char*)Params.data();
  119. ExecuteInfo.nShow = SW_SHOW;
  120. Result = (ShellExecuteEx(&ExecuteInfo) != 0);
  121. if (Result)
  122. {
  123. Handle = ExecuteInfo.hProcess;
  124. }
  125. return Result;
  126. }
  127. //---------------------------------------------------------------------------
  128. bool __fastcall ExecuteShellAndWait(HWND Handle, const AnsiString Path,
  129. const AnsiString Params, TProcessMessagesEvent ProcessMessages)
  130. {
  131. bool Result;
  132. TShellExecuteInfo ExecuteInfo;
  133. memset(&ExecuteInfo, 0, sizeof(ExecuteInfo));
  134. ExecuteInfo.cbSize = sizeof(ExecuteInfo);
  135. ExecuteInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
  136. ExecuteInfo.hwnd = Handle;
  137. ExecuteInfo.lpFile = (char*)Path.data();
  138. ExecuteInfo.lpParameters = (char*)Params.data();
  139. ExecuteInfo.nShow = SW_SHOW;
  140. Result = (ShellExecuteEx(&ExecuteInfo) != 0);
  141. if (Result)
  142. {
  143. if (ProcessMessages != NULL)
  144. {
  145. unsigned long WaitResult;
  146. do
  147. {
  148. WaitResult = WaitForSingleObject(ExecuteInfo.hProcess, 200);
  149. if (WaitResult == WAIT_FAILED)
  150. {
  151. throw Exception(LoadStr(DOCUMENT_WAIT_ERROR));
  152. }
  153. ProcessMessages();
  154. }
  155. while (WaitResult == WAIT_TIMEOUT);
  156. }
  157. else
  158. {
  159. WaitForSingleObject(ExecuteInfo.hProcess, INFINITE);
  160. }
  161. }
  162. return Result;
  163. }
  164. //---------------------------------------------------------------------------
  165. bool __fastcall ExecuteShellAndWait(HWND Handle, const AnsiString Command,
  166. TProcessMessagesEvent ProcessMessages)
  167. {
  168. AnsiString Program, Params, Dir;
  169. SplitCommand(Command, Program, Params, Dir);
  170. return ExecuteShellAndWait(Handle, Program, Params, ProcessMessages);
  171. }
  172. //---------------------------------------------------------------------------
  173. bool __fastcall SpecialFolderLocation(int PathID, AnsiString & Path)
  174. {
  175. LPITEMIDLIST Pidl;
  176. char Buf[256];
  177. if (SHGetSpecialFolderLocation(NULL, PathID, &Pidl) == NO_ERROR &&
  178. SHGetPathFromIDList(Pidl, Buf))
  179. {
  180. Path = AnsiString(Buf);
  181. return true;
  182. }
  183. return false;
  184. }
  185. //---------------------------------------------------------------------------
  186. AnsiString __fastcall ItemsFormatString(const AnsiString SingleItemFormat,
  187. const AnsiString MultiItemsFormat, int Count, const AnsiString FirstItem)
  188. {
  189. AnsiString Result;
  190. if (Count == 1)
  191. {
  192. Result = FORMAT(SingleItemFormat, (FirstItem));
  193. }
  194. else
  195. {
  196. Result = FORMAT(MultiItemsFormat, (Count));
  197. }
  198. return Result;
  199. }
  200. //---------------------------------------------------------------------------
  201. AnsiString __fastcall ItemsFormatString(const AnsiString SingleItemFormat,
  202. const AnsiString MultiItemsFormat, TStrings * Items)
  203. {
  204. return ItemsFormatString(SingleItemFormat, MultiItemsFormat,
  205. Items->Count, (Items->Count > 0 ? Items->Strings[0] : AnsiString()));
  206. }
  207. //---------------------------------------------------------------------------
  208. AnsiString __fastcall FileNameFormatString(const AnsiString SingleFileFormat,
  209. const AnsiString MultiFilesFormat, TStrings * Files, bool Remote)
  210. {
  211. assert(Files != NULL);
  212. AnsiString Item;
  213. if (Files->Count > 0)
  214. {
  215. Item = Remote ? UnixExtractFileName(Files->Strings[0]) :
  216. ExtractFileName(Files->Strings[0]);
  217. }
  218. return ItemsFormatString(SingleFileFormat, MultiFilesFormat,
  219. Files->Count, Item);
  220. }
  221. //---------------------------------------------------------------------
  222. AnsiString __fastcall FormatBytes(__int64 Bytes, bool UseOrders)
  223. {
  224. AnsiString Result;
  225. if (!UseOrders || (Bytes < __int64(100*1024)))
  226. {
  227. Result = FormatFloat("#,##0 \"B\"", Bytes);
  228. }
  229. else if (Bytes < __int64(100*1024*1024))
  230. {
  231. Result = FormatFloat("#,##0 \"KB\"", Bytes / 1024);
  232. }
  233. else
  234. {
  235. Result = FormatFloat("#,##0 \"MB\"", Bytes / (1024*1024));
  236. }
  237. return Result;
  238. }
  239. //---------------------------------------------------------------------------
  240. void __fastcall CopyToClipboard(AnsiString Text)
  241. {
  242. HANDLE Data;
  243. void * DataPtr;
  244. if (OpenClipboard(0))
  245. {
  246. try
  247. {
  248. Data = GlobalAlloc(GMEM_MOVEABLE + GMEM_DDESHARE, Text.Length() + 1);
  249. try
  250. {
  251. DataPtr = GlobalLock(Data);
  252. try
  253. {
  254. memcpy(DataPtr, Text.c_str(), Text.Length() + 1);
  255. EmptyClipboard();
  256. SetClipboardData(CF_TEXT, Data);
  257. }
  258. __finally
  259. {
  260. GlobalUnlock(Data);
  261. }
  262. }
  263. catch(...)
  264. {
  265. GlobalFree(Data);
  266. throw;
  267. }
  268. }
  269. __finally
  270. {
  271. CloseClipboard();
  272. }
  273. }
  274. else
  275. {
  276. throw Exception(Consts_SCannotOpenClipboard);
  277. }
  278. }
  279. //---------------------------------------------------------------------------
  280. void __fastcall CopyToClipboard(TStrings * Strings)
  281. {
  282. if (Strings->Count > 0)
  283. {
  284. if (Strings->Count == 1)
  285. {
  286. CopyToClipboard(Strings->Strings[0]);
  287. }
  288. else
  289. {
  290. CopyToClipboard(Strings->Text);
  291. }
  292. }
  293. }
  294. //---------------------------------------------------------------------------
  295. AnsiString __fastcall UniqTempDir(const AnsiString BaseDir, const AnsiString Identity,
  296. bool Mask)
  297. {
  298. AnsiString TempDir;
  299. do
  300. {
  301. TempDir = BaseDir.IsEmpty() ? SystemTemporaryDirectory() : BaseDir;
  302. TempDir = IncludeTrailingBackslash(TempDir) + Identity;
  303. if (Mask)
  304. {
  305. TempDir += "?????";
  306. }
  307. else
  308. {
  309. TempDir += IncludeTrailingBackslash(FormatDateTime("nnzzz", Now()));
  310. };
  311. }
  312. while (!Mask && DirectoryExists(TempDir));
  313. return TempDir;
  314. }
  315. //---------------------------------------------------------------------------
  316. bool __fastcall DeleteDirectory(const AnsiString DirName)
  317. {
  318. TSearchRec sr;
  319. bool retval = true;
  320. if (FindFirst(DirName + "\\*", faAnyFile, sr) == 0) // VCL Function
  321. {
  322. if (FLAGSET(sr.Attr, faDirectory))
  323. {
  324. if (sr.Name != "." && sr.Name != "..")
  325. retval = DeleteDirectory(DirName + "\\" + sr.Name);
  326. }
  327. else
  328. {
  329. retval = DeleteFile(DirName + "\\" + sr.Name);
  330. }
  331. if (retval)
  332. {
  333. while (FindNext(sr) == 0)
  334. { // VCL Function
  335. if (FLAGSET(sr.Attr, faDirectory))
  336. {
  337. if (sr.Name != "." && sr.Name != "..")
  338. retval = DeleteDirectory(DirName + "\\" + sr.Name);
  339. }
  340. else
  341. {
  342. retval = DeleteFile(DirName + "\\" + sr.Name);
  343. }
  344. if (!retval) break;
  345. }
  346. }
  347. }
  348. FindClose(sr);
  349. if (retval) retval = RemoveDir(DirName); // VCL function
  350. return retval;
  351. }
  352. //---------------------------------------------------------------------------
  353. AnsiString __fastcall TranslateExceptionMessage(const Exception * E)
  354. {
  355. if (dynamic_cast<const EAccessViolation*>(E) != NULL)
  356. {
  357. return LoadStr(ACCESS_VIOLATION_ERROR);
  358. }
  359. else
  360. {
  361. return E->Message;
  362. }
  363. }
  364. //---------------------------------------------------------------------------
  365. AnsiString __fastcall FormatDateTimeSpan(const AnsiString TimeFormat, TDateTime DateTime)
  366. {
  367. AnsiString Result;
  368. if (int(DateTime) > 0)
  369. {
  370. Result = IntToStr(int(DateTime)) + ", ";
  371. }
  372. // days are decremented, because when there is to many of them,
  373. // "integer overflow" error occures
  374. Result += FormatDateTime(TimeFormat, DateTime - int(DateTime));
  375. return Result;
  376. }
  377. //---------------------------------------------------------------------------
  378. TLocalCustomCommand::TLocalCustomCommand()
  379. {
  380. }
  381. //---------------------------------------------------------------------------
  382. TLocalCustomCommand::TLocalCustomCommand(const AnsiString & FileName,
  383. const AnsiString & LocalFileName, const AnsiString & FileList) :
  384. TFileCustomCommand(FileName, FileList)
  385. {
  386. FLocalFileName = LocalFileName;
  387. }
  388. //---------------------------------------------------------------------------
  389. int __fastcall TLocalCustomCommand::PatternLen(int Index, char PatternCmd)
  390. {
  391. int Len;
  392. if (PatternCmd == '^')
  393. {
  394. Len = 3;
  395. }
  396. else
  397. {
  398. Len = TFileCustomCommand::PatternLen(Index, PatternCmd);
  399. }
  400. return Len;
  401. }
  402. //---------------------------------------------------------------------------
  403. bool __fastcall TLocalCustomCommand::PatternReplacement(int Index,
  404. const AnsiString & Pattern, AnsiString & Replacement, bool & Delimit)
  405. {
  406. bool Result;
  407. if (Pattern == "!^!")
  408. {
  409. Replacement = FLocalFileName;
  410. Result = true;
  411. }
  412. else
  413. {
  414. Result = TFileCustomCommand::PatternReplacement(Index, Pattern, Replacement, Delimit);
  415. }
  416. return Result;
  417. }
  418. //---------------------------------------------------------------------------
  419. void __fastcall TLocalCustomCommand::DelimitReplacement(
  420. AnsiString & /*Replacement*/, char /*Quote*/)
  421. {
  422. // never delimit local commands
  423. }
  424. //---------------------------------------------------------------------------
  425. bool __fastcall TLocalCustomCommand::HasLocalFileName(const AnsiString & Command)
  426. {
  427. return FindPattern(Command, '^');
  428. }
  429. //---------------------------------------------------------------------------
  430. bool __fastcall TLocalCustomCommand::IsFileCommand(const AnsiString & Command)
  431. {
  432. return TFileCustomCommand::IsFileCommand(Command) || HasLocalFileName(Command);
  433. }