CoreMain.cpp 6.0 KB

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