1
0

CoreMain.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. OnClick = NULL;
  24. GroupWith = -1;
  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. assert(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. #ifndef NO_FILEZILLA
  127. TFileZillaIntf::Initialize();
  128. #endif
  129. NeonInitialize();
  130. CoreLoad();
  131. }
  132. //---------------------------------------------------------------------------
  133. void CoreFinalize()
  134. {
  135. try
  136. {
  137. Configuration->Save();
  138. }
  139. catch(Exception & E)
  140. {
  141. ShowExtendedException(&E);
  142. }
  143. NeonFinalize();
  144. #ifndef NO_FILEZILLA
  145. TFileZillaIntf::Finalize();
  146. #endif
  147. PuttyFinalize();
  148. delete StoredSessions;
  149. StoredSessions = NULL;
  150. delete Configuration;
  151. Configuration = NULL;
  152. CryptographyFinalize();
  153. }
  154. //---------------------------------------------------------------------------
  155. void CoreSetResourceModule(void * ResourceHandle)
  156. {
  157. #ifndef NO_FILEZILLA
  158. TFileZillaIntf::SetResourceModule(ResourceHandle);
  159. #else
  160. USEDPARAM(ResourceHandle);
  161. #endif
  162. }
  163. //---------------------------------------------------------------------------
  164. void CoreMaintenanceTask()
  165. {
  166. DontSaveRandomSeed();
  167. }
  168. //---------------------------------------------------------------------------
  169. //---------------------------------------------------------------------------
  170. __fastcall TOperationVisualizer::TOperationVisualizer(bool UseBusyCursor) :
  171. FUseBusyCursor(UseBusyCursor)
  172. {
  173. if (FUseBusyCursor)
  174. {
  175. FToken = BusyStart();
  176. }
  177. }
  178. //---------------------------------------------------------------------------
  179. __fastcall TOperationVisualizer::~TOperationVisualizer()
  180. {
  181. if (FUseBusyCursor)
  182. {
  183. BusyEnd(FToken);
  184. }
  185. }
  186. //---------------------------------------------------------------------------
  187. //---------------------------------------------------------------------------
  188. __fastcall TInstantOperationVisualizer::TInstantOperationVisualizer() :
  189. FStart(Now())
  190. {
  191. }
  192. //---------------------------------------------------------------------------
  193. __fastcall TInstantOperationVisualizer::~TInstantOperationVisualizer()
  194. {
  195. TDateTime Time = Now();
  196. __int64 Duration = MilliSecondsBetween(Time, FStart);
  197. const __int64 MinDuration = 250;
  198. if (Duration < MinDuration)
  199. {
  200. Sleep(static_cast<unsigned int>(MinDuration - Duration));
  201. }
  202. }
  203. //---------------------------------------------------------------------------