GUITools.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //---------------------------------------------------------------------------
  2. #define NO_WIN32_LEAN_AND_MEAN
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include <shlobj.h>
  6. #include "GUITools.h"
  7. #include "GUIConfiguration.h"
  8. #include <TextsWin.h>
  9. #include <Common.h>
  10. #include <ScpMain.h>
  11. #include <SessionData.h>
  12. //---------------------------------------------------------------------------
  13. #pragma package(smart_init)
  14. //---------------------------------------------------------------------------
  15. bool __fastcall FindFile(AnsiString & Path)
  16. {
  17. bool Result = FileExists(Path);
  18. if (!Result)
  19. {
  20. int Len = GetEnvironmentVariable("PATH", NULL, 0);
  21. if (Len > 0)
  22. {
  23. AnsiString Paths;
  24. Paths.SetLength(Len - 1);
  25. GetEnvironmentVariable("PATH", Paths.c_str(), Len);
  26. AnsiString NewPath = FileSearch(ExtractFileName(Path), Paths);
  27. Result = !NewPath.IsEmpty();
  28. if (Result)
  29. {
  30. Path = NewPath;
  31. }
  32. }
  33. }
  34. return Result;
  35. }
  36. //---------------------------------------------------------------------------
  37. bool __fastcall FileExistsEx(AnsiString Path)
  38. {
  39. return FindFile(Path);
  40. }
  41. //---------------------------------------------------------------------------
  42. void __fastcall OpenSessionInPutty(TSessionData * SessionData)
  43. {
  44. AnsiString PuttyPath = GUIConfiguration->PuttyPath;
  45. if (FindFile(PuttyPath))
  46. {
  47. AnsiString SessionName;
  48. THierarchicalStorage * Storage = NULL;
  49. TSessionData * ExportData = NULL;
  50. try
  51. {
  52. Storage = new TRegistryStorage(Configuration->PuttySessionsKey);
  53. Storage->AccessMode = smReadWrite;
  54. if (Storage->OpenRootKey(true))
  55. {
  56. if (Storage->KeyExists(SessionData->StorageKey))
  57. {
  58. SessionName = SessionData->SessionName;
  59. }
  60. else
  61. {
  62. ExportData = new TSessionData("");
  63. ExportData->Assign(SessionData);
  64. ExportData->Modified = true;
  65. ExportData->Name = GUIConfiguration->PuttySession;
  66. ExportData->Password = "";
  67. ExportData->Save(Storage, true);
  68. SessionName = GUIConfiguration->PuttySession;
  69. }
  70. }
  71. }
  72. __finally
  73. {
  74. delete Storage;
  75. delete ExportData;
  76. }
  77. if (!ExecuteShell(PuttyPath, FORMAT("-load \"%s\"", (SessionName))))
  78. {
  79. throw Exception(FMTLOAD(EXECUTE_APP_ERROR, (PuttyPath)));
  80. }
  81. }
  82. else
  83. {
  84. throw Exception(FMTLOAD(FILE_NOT_FOUND, (GUIConfiguration->PuttyPath)));
  85. }
  86. }
  87. //---------------------------------------------------------------------------
  88. bool __fastcall ExecuteShell(const AnsiString Path, const AnsiString Params)
  89. {
  90. return ((int)ShellExecute(NULL, "open", (char*)Path.data(),
  91. (char*)Params.data(), NULL, SW_SHOWNORMAL) > 32);
  92. }
  93. //---------------------------------------------------------------------------
  94. bool __fastcall SpecialFolderLocation(int PathID, AnsiString & Path)
  95. {
  96. LPITEMIDLIST Pidl;
  97. char Buf[256];
  98. if (SHGetSpecialFolderLocation(NULL, PathID, &Pidl) == NO_ERROR &&
  99. SHGetPathFromIDList(Pidl, Buf))
  100. {
  101. Path = AnsiString(Buf);
  102. return true;
  103. }
  104. return false;
  105. }
  106. //---------------------------------------------------------------------------
  107. AnsiString __fastcall ItemsFormatString(const AnsiString SingleItemFormat,
  108. const AnsiString MultiItemsFormat, int Count, const AnsiString FirstItem)
  109. {
  110. AnsiString Result;
  111. if (Count == 1)
  112. {
  113. Result = FORMAT(SingleItemFormat, (FirstItem));
  114. }
  115. else
  116. {
  117. Result = FORMAT(MultiItemsFormat, (Count));
  118. }
  119. return Result;
  120. }
  121. //---------------------------------------------------------------------------
  122. AnsiString __fastcall ItemsFormatString(const AnsiString SingleItemFormat,
  123. const AnsiString MultiItemsFormat, TStrings * Items)
  124. {
  125. return ItemsFormatString(SingleItemFormat, MultiItemsFormat,
  126. Items->Count, (Items->Count > 0 ? Items->Strings[0] : AnsiString()));
  127. }
  128. //---------------------------------------------------------------------------
  129. AnsiString __fastcall FileNameFormatString(const AnsiString SingleFileFormat,
  130. const AnsiString MultiFilesFormat, TStrings * Files, bool Remote)
  131. {
  132. assert(Files != NULL);
  133. AnsiString Item;
  134. if (Files->Count > 0)
  135. {
  136. Item = Remote ? UnixExtractFileName(Files->Strings[0]) :
  137. ExtractFileName(Files->Strings[0]);
  138. }
  139. return ItemsFormatString(SingleFileFormat, MultiFilesFormat,
  140. Files->Count, Item);
  141. }
  142. //---------------------------------------------------------------------------