CoreMain.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "CoreMain.h"
  5. #include "Common.h"
  6. #include "Interface.h"
  7. #include "Configuration.h"
  8. #include "PuttyIntf.h"
  9. #include "Cryptography.h"
  10. #include <DateUtils.hpp>
  11. #ifndef NO_FILEZILLA
  12. #include "FileZillaIntf.h"
  13. #endif
  14. #include "WebDAVFileSystem.h"
  15. //---------------------------------------------------------------------------
  16. #pragma package(smart_init)
  17. //---------------------------------------------------------------------------
  18. TConfiguration * Configuration = NULL;
  19. TStoredSessionList * StoredSessions = NULL;
  20. //---------------------------------------------------------------------------
  21. TQueryButtonAlias::TQueryButtonAlias()
  22. {
  23. OnSubmit = NULL;
  24. GroupWith = -1;
  25. ElevationRequired = false;
  26. MenuButton = false;
  27. }
  28. //---------------------------------------------------------------------------
  29. TQueryParams::TQueryParams(unsigned int AParams, UnicodeString AHelpKeyword)
  30. {
  31. Params = AParams;
  32. Aliases = NULL;
  33. AliasesCount = 0;
  34. Timer = 0;
  35. TimerEvent = NULL;
  36. TimerMessage = L"";
  37. TimerAnswers = 0;
  38. TimerQueryType = static_cast<TQueryType>(-1);
  39. Timeout = 0;
  40. TimeoutAnswer = 0;
  41. NoBatchAnswers = 0;
  42. HelpKeyword = AHelpKeyword;
  43. }
  44. //---------------------------------------------------------------------------
  45. TQueryParams::TQueryParams(const TQueryParams & Source)
  46. {
  47. Assign(Source);
  48. }
  49. //---------------------------------------------------------------------------
  50. void TQueryParams::Assign(const TQueryParams & Source)
  51. {
  52. *this = Source;
  53. }
  54. //---------------------------------------------------------------------------
  55. bool __fastcall IsAuthenticationPrompt(TPromptKind Kind)
  56. {
  57. return
  58. (Kind == pkUserName) || (Kind == pkPassphrase) || (Kind == pkTIS) ||
  59. (Kind == pkCryptoCard) || (Kind == pkKeybInteractive) ||
  60. (Kind == pkPassword) || (Kind == pkNewPassword);
  61. }
  62. //---------------------------------------------------------------------------
  63. bool __fastcall IsPasswordOrPassphrasePrompt(TPromptKind Kind, TStrings * Prompts)
  64. {
  65. return
  66. (Prompts->Count == 1) && FLAGCLEAR(int(Prompts->Objects[0]), pupEcho) &&
  67. ((Kind == pkPassword) || (Kind == pkPassphrase) || (Kind == pkKeybInteractive) ||
  68. (Kind == pkTIS) || (Kind == pkCryptoCard));
  69. }
  70. //---------------------------------------------------------------------------
  71. bool __fastcall IsPasswordPrompt(TPromptKind Kind, TStrings * Prompts)
  72. {
  73. return
  74. IsPasswordOrPassphrasePrompt(Kind, Prompts) &&
  75. (Kind != pkPassphrase);
  76. }
  77. //---------------------------------------------------------------------------
  78. void CoreLoad()
  79. {
  80. bool SessionList = true;
  81. std::unique_ptr<THierarchicalStorage> SessionsStorage(Configuration->CreateScpStorage(SessionList));
  82. THierarchicalStorage * ConfigStorage;
  83. std::unique_ptr<THierarchicalStorage> ConfigStorageAuto;
  84. if (!SessionList)
  85. {
  86. // can reuse this for configuration
  87. ConfigStorage = SessionsStorage.get();
  88. }
  89. else
  90. {
  91. ConfigStorageAuto.reset(Configuration->CreateConfigStorage());
  92. ConfigStorage = ConfigStorageAuto.get();
  93. }
  94. try
  95. {
  96. Configuration->Load(ConfigStorage);
  97. }
  98. catch (Exception & E)
  99. {
  100. ShowExtendedException(&E);
  101. }
  102. // should be noop, unless exception occured above
  103. ConfigStorage->CloseAll();
  104. StoredSessions = new TStoredSessionList();
  105. try
  106. {
  107. if (SessionsStorage->OpenSubKey(Configuration->StoredSessionsSubKey, false))
  108. {
  109. StoredSessions->Load(SessionsStorage.get());
  110. }
  111. }
  112. catch (Exception & E)
  113. {
  114. ShowExtendedException(&E);
  115. }
  116. }
  117. //---------------------------------------------------------------------------
  118. void CoreInitialize()
  119. {
  120. Randomize();
  121. CryptographyInitialize();
  122. // we do not expect configuration re-creation
  123. DebugAssert(Configuration == NULL);
  124. // configuration needs to be created and loaded before putty is initialized,
  125. // so that random seed path is known
  126. Configuration = CreateConfiguration();
  127. PuttyInitialize();
  128. #ifndef NO_FILEZILLA
  129. TFileZillaIntf::Initialize();
  130. #endif
  131. NeonInitialize();
  132. CoreLoad();
  133. }
  134. //---------------------------------------------------------------------------
  135. void CoreFinalize()
  136. {
  137. try
  138. {
  139. Configuration->Save();
  140. }
  141. catch(Exception & E)
  142. {
  143. ShowExtendedException(&E);
  144. }
  145. NeonFinalize();
  146. #ifndef NO_FILEZILLA
  147. TFileZillaIntf::Finalize();
  148. #endif
  149. PuttyFinalize();
  150. delete StoredSessions;
  151. StoredSessions = NULL;
  152. delete Configuration;
  153. Configuration = NULL;
  154. CryptographyFinalize();
  155. }
  156. //---------------------------------------------------------------------------
  157. void CoreSetResourceModule(void * ResourceHandle)
  158. {
  159. #ifndef NO_FILEZILLA
  160. TFileZillaIntf::SetResourceModule(ResourceHandle);
  161. #else
  162. DebugUsedParam(ResourceHandle);
  163. #endif
  164. }
  165. //---------------------------------------------------------------------------
  166. void CoreMaintenanceTask()
  167. {
  168. DontSaveRandomSeed();
  169. }
  170. //---------------------------------------------------------------------------
  171. //---------------------------------------------------------------------------
  172. __fastcall TOperationVisualizer::TOperationVisualizer(bool UseBusyCursor) :
  173. FUseBusyCursor(UseBusyCursor)
  174. {
  175. if (FUseBusyCursor)
  176. {
  177. FToken = BusyStart();
  178. }
  179. }
  180. //---------------------------------------------------------------------------
  181. __fastcall TOperationVisualizer::~TOperationVisualizer()
  182. {
  183. if (FUseBusyCursor)
  184. {
  185. BusyEnd(FToken);
  186. }
  187. }
  188. //---------------------------------------------------------------------------
  189. //---------------------------------------------------------------------------
  190. __fastcall TInstantOperationVisualizer::TInstantOperationVisualizer() :
  191. FStart(Now())
  192. {
  193. }
  194. //---------------------------------------------------------------------------
  195. __fastcall TInstantOperationVisualizer::~TInstantOperationVisualizer()
  196. {
  197. TDateTime Time = Now();
  198. __int64 Duration = MilliSecondsBetween(Time, FStart);
  199. const __int64 MinDuration = 250;
  200. if (Duration < MinDuration)
  201. {
  202. Sleep(static_cast<unsigned int>(MinDuration - Duration));
  203. }
  204. }
  205. //---------------------------------------------------------------------------