Cleanup.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //---------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include <VCLCommon.h>
  6. #include <CoreMain.h>
  7. #include <WinConfiguration.h>
  8. #include <TextsWin.h>
  9. #include <HelpWin.h>
  10. #include <PuttyTools.h>
  11. #include "Cleanup.h"
  12. //---------------------------------------------------------------------
  13. #ifndef NO_RESOURCES
  14. #pragma resource "*.dfm"
  15. #endif
  16. //---------------------------------------------------------------------
  17. bool __fastcall DoCleanupDialog()
  18. {
  19. std::unique_ptr<TCleanupDialog> CleanupDialog(SafeFormCreate<TCleanupDialog>());
  20. return CleanupDialog->Execute();
  21. }
  22. //---------------------------------------------------------------------
  23. void __fastcall DoCleanupDialogIfAnyDataAndWanted()
  24. {
  25. std::unique_ptr<TCleanupDialog> CleanupDialog(SafeFormCreate<TCleanupDialog>());
  26. if (CleanupDialog->AnyData() &&
  27. (MessageDialog(LoadStr(UNINSTALL_CLEANUP), qtConfirmation, qaYes | qaNo, HELP_UNINSTALL_CLEANUP) == qaYes))
  28. {
  29. CleanupDialog->Execute();
  30. }
  31. }
  32. //---------------------------------------------------------------------
  33. __fastcall TCleanupDialog::TCleanupDialog(TComponent* AOwner)
  34. : TForm(AOwner)
  35. {
  36. FAnyData = false;
  37. FindData();
  38. UseSystemSettings(this);
  39. }
  40. //---------------------------------------------------------------------
  41. void __fastcall TCleanupDialog::AddLocation(int CaptionId, const UnicodeString & Location, TCleanupEvent Event)
  42. {
  43. FCaptions.push_back(LoadStr(CaptionId));
  44. FLocations.push_back(Location);
  45. FCleanupEvents.push_back(Event);
  46. FAnyData = true;
  47. }
  48. //---------------------------------------------------------------------
  49. void __fastcall TCleanupDialog::AddRegistryLocation(int CaptionId, const UnicodeString & Location, TCleanupEvent Event)
  50. {
  51. AddLocation(CaptionId, Configuration->RootKeyStr + L'\\' + Configuration->RegistryStorageKey + L'\\' + Location, Event);
  52. }
  53. //---------------------------------------------------------------------
  54. bool __fastcall TCleanupDialog::AnyData()
  55. {
  56. return FAnyData;
  57. }
  58. //---------------------------------------------------------------------
  59. void __fastcall TCleanupDialog::FindData()
  60. {
  61. // Add unconditionally (as it has a side effect of not saving the configuration)
  62. AddRegistryLocation(CLEANUP_CONFIG, Configuration->ConfigurationSubKey, Configuration->CleanupConfiguration);
  63. // But count as real data, only if it really exists
  64. FAnyData = Configuration->RegistryPathExists(Configuration->ConfigurationSubKey);
  65. if (Configuration->RegistryPathExists(Configuration->StoredSessionsSubKey))
  66. {
  67. AddRegistryLocation(CLEANUP_SESSIONS, Configuration->StoredSessionsSubKey, StoredSessions->Cleanup);
  68. }
  69. if (Configuration->HasAnyCache())
  70. {
  71. AddRegistryLocation(CLEANUP_HOSTKEYS, L"...", Configuration->CleanupCaches);
  72. }
  73. UnicodeString IniFilePath = ExpandEnvironmentVariables(Configuration->IniFileStorageNameForReading);
  74. if (FileExists(IniFilePath))
  75. {
  76. AddLocation(CLEANUP_INIFILE, IniFilePath, Configuration->CleanupIniFile);
  77. }
  78. if (RandomSeedExists())
  79. {
  80. AddLocation(CLEANUP_SEEDFILE, Configuration->RandomSeedFileName, Configuration->CleanupRandomSeedFile);
  81. }
  82. if (WinConfiguration->AnyTemporaryFolders())
  83. {
  84. AddLocation(CLEANUP_TEMP_FOLDERS, WinConfiguration->TemporaryDir(true), WinConfiguration->CleanupTemporaryFolders);
  85. }
  86. }
  87. //---------------------------------------------------------------------
  88. void __fastcall TCleanupDialog::InitControls()
  89. {
  90. DebugAssert(FCaptions.size() == FLocations.size());
  91. DebugAssert(FCaptions.size() == FCleanupEvents.size());
  92. // Particularly in response to WM_DPICHANGED, the form may re-show
  93. DataListView->Items->Clear();
  94. for (size_t Index = 0; Index < FCaptions.size(); Index++)
  95. {
  96. TListItem * Item = DataListView->Items->Add();
  97. Item->Caption = FCaptions[Index];
  98. Item->SubItems->Add(FLocations[Index]);
  99. }
  100. AutoSizeListColumnsWidth(DataListView);
  101. }
  102. //---------------------------------------------------------------------
  103. void __fastcall TCleanupDialog::UpdateControls()
  104. {
  105. EnableControl(OKButton, ListViewAnyChecked(DataListView));
  106. }
  107. //---------------------------------------------------------------------------
  108. void __fastcall TCleanupDialog::DataListViewMouseDown(
  109. TObject * /*Sender*/, TMouseButton /*Button*/, TShiftState /*Shift*/,
  110. int /*X*/, int /*Y*/)
  111. {
  112. UpdateControls();
  113. }
  114. //---------------------------------------------------------------------------
  115. void __fastcall TCleanupDialog::DataListViewKeyUp(
  116. TObject * /*Sender*/, WORD & /*Key*/, TShiftState /*Shift*/)
  117. {
  118. UpdateControls();
  119. }
  120. //---------------------------------------------------------------------------
  121. void __fastcall TCleanupDialog::FormShow(TObject * /*Sender*/)
  122. {
  123. InitControls();
  124. UpdateControls();
  125. }
  126. //---------------------------------------------------------------------------
  127. void __fastcall TCleanupDialog::CheckAllButtonClick(TObject * /*Sender*/)
  128. {
  129. ListViewCheckAll(DataListView, caToggle);
  130. UpdateControls();
  131. }
  132. //---------------------------------------------------------------------------
  133. void __fastcall TCleanupDialog::DataListViewInfoTip(TObject * /*Sender*/,
  134. TListItem * Item, UnicodeString & InfoTip)
  135. {
  136. InfoTip = Format(L"%s\nLocation: %s",
  137. ARRAYOFCONST((Item->Caption, Item->SubItems->Strings[0])));
  138. }
  139. //---------------------------------------------------------------------------
  140. void __fastcall TCleanupDialog::HelpButtonClick(TObject * /*Sender*/)
  141. {
  142. FormHelp(this);
  143. }
  144. //---------------------------------------------------------------------------
  145. bool __fastcall TCleanupDialog::Execute()
  146. {
  147. bool Result = (ShowModal() == DefaultResult(this));
  148. if (Result)
  149. {
  150. Configuration->Usage->Inc(L"Cleanups");
  151. for (int Index = 0; Index < DataListView->Items->Count; Index++)
  152. {
  153. if (DataListView->Items->Item[Index]->Checked)
  154. {
  155. try
  156. {
  157. FCleanupEvents[Index]();
  158. }
  159. catch (Exception & E)
  160. {
  161. ShowExtendedException(&E);
  162. }
  163. }
  164. }
  165. }
  166. return Result;
  167. }
  168. //---------------------------------------------------------------------------