Preferences.cpp 105 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051
  1. //---------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <StrUtils.hpp>
  5. #include <Common.h>
  6. #include <math.h>
  7. #include "Preferences.h"
  8. #include "Custom.h"
  9. #include <CoreMain.h>
  10. #include <Terminal.h>
  11. #include <Bookmarks.h>
  12. #include "VCLCommon.h"
  13. #include "GUITools.h"
  14. #include "Tools.h"
  15. #include "TextsCore.h"
  16. #include "TextsWin.h"
  17. #include "HelpWin.h"
  18. #include "WinInterface.h"
  19. #include "WinConfiguration.h"
  20. #include "Setup.h"
  21. #include "ProgParams.h"
  22. #include "Http.h"
  23. //---------------------------------------------------------------------
  24. #pragma link "CopyParams"
  25. #pragma link "UpDownEdit"
  26. #pragma link "ComboEdit"
  27. #ifndef NO_RESOURCES
  28. #pragma link "HistoryComboBox"
  29. #pragma resource "*.dfm"
  30. #endif
  31. //---------------------------------------------------------------------
  32. bool __fastcall DoPreferencesDialog(TPreferencesMode APreferencesMode,
  33. TPreferencesDialogData * DialogData)
  34. {
  35. bool Result;
  36. TPreferencesDialog * PreferencesDialog =
  37. new TPreferencesDialog(GetFormOwner(), APreferencesMode);
  38. try
  39. {
  40. Result = PreferencesDialog->Execute(DialogData);
  41. if (Result)
  42. {
  43. Configuration->SaveExplicit();
  44. }
  45. }
  46. __finally
  47. {
  48. delete PreferencesDialog;
  49. }
  50. return Result;
  51. }
  52. //---------------------------------------------------------------------
  53. __fastcall TPreferencesDialog::TPreferencesDialog(
  54. TComponent* AOwner, TPreferencesMode PreferencesMode)
  55. : TForm(AOwner)
  56. {
  57. SetCorrectFormParent(this);
  58. FNoUpdate = 0;
  59. FPreferencesMode = PreferencesMode;
  60. FEditorFont.reset(new TFont());
  61. FEditorFont->Color = clWindowText;
  62. FPanelFont.reset(new TFont());
  63. FPanelFont->Color = clWindowText;
  64. // currently useless
  65. FAfterFilenameEditDialog = false;
  66. FCustomCommandList = new TCustomCommandList();
  67. FCustomCommandChanging = false;
  68. FExtensionList = new TCustomCommandList();
  69. FListViewDragDest = -1;
  70. FCopyParamList = new TCopyParamList();
  71. FEditorList = new TEditorList();
  72. FAutomaticUpdatesPossible = IsInstalled();
  73. FCustomCommandsHintItem = NULL;
  74. FAddedExtensions.reset(CreateSortedStringList());
  75. FCustomCommandOptions.reset(new TStringList());
  76. UseSystemSettings(this);
  77. FCustomCommandsScrollOnDragOver = new TListViewScrollOnDragOver(CustomCommandsView, true);
  78. FixListColumnWidth(CustomCommandsView, -1);
  79. FCopyParamScrollOnDragOver = new TListViewScrollOnDragOver(CopyParamListView, true);
  80. FixListColumnWidth(CopyParamListView, -1);
  81. FEditorScrollOnDragOver = new TListViewScrollOnDragOver(EditorListView3, true);
  82. FixListColumnWidth(EditorListView3, -1);
  83. FOrigCustomCommandsViewWindowProc = CustomCommandsView->WindowProc;
  84. CustomCommandsView->WindowProc = CustomCommandsViewWindowProc;
  85. ComboAutoSwitchInitialize(UpdatesBetaVersionsCombo);
  86. EnableControl(UpdatesBetaVersionsCombo, !WinConfiguration->IsBeta);
  87. EnableControl(UpdatesBetaVersionsLabel, UpdatesBetaVersionsCombo->Enabled);
  88. HintLabel(LogFileNameHintText, LoadStr(LOG_FILE_HINT3));
  89. HintLabel(ActionsLogFileNameHintText, LoadStr(LOG_FILE_HINT3));
  90. HintLabel(ShellIconsText2);
  91. HotTrackLabel(CopyParamLabel);
  92. HintLabel(PuttyPathHintText, LoadStr(PUTTY_PATTERNS_HINT2));
  93. EditorEncodingCombo->Items->Add(DefaultEncodingName());
  94. EditorEncodingCombo->Items->Add(LoadStr(UTF8_NAME));
  95. std::unique_ptr<TStrings> Workspaces(StoredSessions->GetWorkspaces());
  96. AutoWorkspaceCombo->Items->AddStrings(Workspaces.get());
  97. AutoSaveWorkspacePasswordsCheck->Caption = LoadStr(SAVE_WORKSPACE_PASSWORDS);
  98. PuttyRegistryStorageKeyEdit->Items->Add(OriginalPuttyRegistryStorageKey);
  99. PuttyRegistryStorageKeyEdit->Items->Add(KittyRegistryStorageKey);
  100. MenuButton(RegisterAsUrlHandlersButton);
  101. MenuButton(EditorFontColorButton);
  102. MenuButton(EditorBackgroundColorButton);
  103. LoadDialogImage(CommanderInterfacePicture, L"Commander");
  104. LoadDialogImage(ExplorerInterfacePicture, L"Explorer");
  105. LinkLabel(UpdatesLink);
  106. LinkAppLabel(BackgroundConfirmationsLink);
  107. HideComponentsPanel(this);
  108. }
  109. //---------------------------------------------------------------------------
  110. __fastcall TPreferencesDialog::~TPreferencesDialog()
  111. {
  112. SAFE_DESTROY(FEditorScrollOnDragOver);
  113. SAFE_DESTROY(FCopyParamScrollOnDragOver);
  114. SAFE_DESTROY(FCustomCommandsScrollOnDragOver);
  115. delete FCustomCommandList;
  116. FCustomCommandList = NULL;
  117. delete FExtensionList;
  118. FExtensionList = NULL;
  119. delete FCopyParamList;
  120. FCopyParamList = NULL;
  121. delete FEditorList;
  122. FEditorList = NULL;
  123. }
  124. //---------------------------------------------------------------------
  125. bool __fastcall TPreferencesDialog::Execute(TPreferencesDialogData * DialogData)
  126. {
  127. PuttyPathEdit->Items = CustomWinConfiguration->History[L"PuttyPath"];
  128. FDialogData = DialogData;
  129. bool Result = (ShowModal() == DefaultResult(this));
  130. if (Result)
  131. {
  132. SaveConfiguration();
  133. CustomWinConfiguration->History[L"PuttyPath"] = PuttyPathEdit->Items;
  134. }
  135. else
  136. {
  137. for (int Index = 0; Index < FAddedExtensions->Count; Index++)
  138. {
  139. DeleteFile(ApiPath(FAddedExtensions->Strings[Index]));
  140. }
  141. }
  142. return Result;
  143. }
  144. //---------------------------------------------------------------------------
  145. void __fastcall TPreferencesDialog::LoadLanguages()
  146. {
  147. if (!FLanguagesLoaded)
  148. {
  149. LanguagesView->Items->Clear();
  150. TStrings * Locales = GUIConfiguration->Locales;
  151. for (int Index = 0; Index < Locales->Count; Index++)
  152. {
  153. TListItem * Item = LanguagesView->Items->Add();
  154. Item->Caption = Locales->Strings[Index];
  155. Item->Data = Locales->Objects[Index];
  156. Item->Focused =
  157. (reinterpret_cast<LCID>(Locales->Objects[Index]) == GUIConfiguration->Locale);
  158. Item->Selected = Item->Focused;
  159. }
  160. FLanguagesLoaded = false;
  161. }
  162. }
  163. //---------------------------------------------------------------------------
  164. TTabSheet * __fastcall TPreferencesDialog::FindPageForTreeNode(TTreeNode * Node)
  165. {
  166. for (int pi = 0; pi < PageControl->PageCount; pi++)
  167. {
  168. TTabSheet * Sheet = PageControl->Pages[pi];
  169. if (Sheet->Tag == Node->SelectedIndex)
  170. {
  171. return Sheet;
  172. }
  173. }
  174. DebugFail();
  175. return NULL;
  176. }
  177. //---------------------------------------------------------------------------
  178. void __fastcall TPreferencesDialog::PrepareNavigationTree(TTreeView * Tree)
  179. {
  180. Tree->FullExpand();
  181. int i = 0;
  182. while (i < Tree->Items->Count)
  183. {
  184. TTreeNode * Node = Tree->Items->Item[i];
  185. TTabSheet * Sheet = FindPageForTreeNode(Node);
  186. if (DebugNotNull(Sheet))
  187. {
  188. if (Sheet->Enabled)
  189. {
  190. // gets called multiple times occasionally
  191. // (e.g. when called from upload dialog invoked by /upload)
  192. if (!Sheet->Caption.IsEmpty())
  193. {
  194. Node->Text = Sheet->Caption;
  195. Sheet->Caption = L"";
  196. }
  197. }
  198. else
  199. {
  200. Tree->Items->Delete(Node);
  201. i--;
  202. }
  203. }
  204. i++;
  205. }
  206. }
  207. //---------------------------------------------------------------------------
  208. void __fastcall TPreferencesDialog::LoadConfiguration()
  209. {
  210. FNoUpdate++;
  211. try
  212. {
  213. #define BOOLPROP(PROP) PROP ## Check->Checked = WinConfiguration->PROP;
  214. BOOLPROP(DefaultDirIsHome);
  215. BOOLPROP(PreservePanelState);
  216. BOOLPROP(DeleteToRecycleBin);
  217. BOOLPROP(DDTransferConfirmation);
  218. BOOLPROP(DDWarnLackOfTempSpace);
  219. BOOLPROP(ShowHiddenFiles);
  220. BOOLPROP(RenameWholeName);
  221. BOOLPROP(ShowInaccesibleDirectories);
  222. BOOLPROP(CopyOnDoubleClickConfirmation);
  223. BOOLPROP(ConfirmTransferring);
  224. BOOLPROP(ConfirmOverwriting);
  225. BOOLPROP(ConfirmResume);
  226. BOOLPROP(ConfirmDeleting);
  227. BOOLPROP(ConfirmRecycling);
  228. BOOLPROP(ConfirmExitOnCompletion);
  229. BOOLPROP(ConfirmCommandSession);
  230. BOOLPROP(ContinueOnError);
  231. BOOLPROP(DDAllowMoveInit);
  232. BOOLPROP(BeepOnFinish);
  233. BOOLPROP(TemporaryDirectoryAppendSession);
  234. BOOLPROP(TemporaryDirectoryAppendPath);
  235. BOOLPROP(TemporaryDirectoryDeterministic);
  236. BOOLPROP(TemporaryDirectoryCleanup);
  237. BOOLPROP(ConfirmTemporaryDirectoryCleanup);
  238. BOOLPROP(FullRowSelect);
  239. ConfirmClosingSessionCheck2->Checked = WinConfiguration->ConfirmClosingSession;
  240. if (WinConfiguration->DDTransferConfirmation == asAuto)
  241. {
  242. // allow grayed state only initially,
  243. // once the off state is confirmed, never allow returning
  244. // to the undefined state
  245. DDTransferConfirmationCheck->AllowGrayed = true;
  246. }
  247. CheckBoxAutoSwitchLoad(DDTransferConfirmationCheck, WinConfiguration->DDTransferConfirmation);
  248. BeepOnFinishAfterEdit->AsInteger =
  249. int(static_cast<double>(GUIConfiguration->BeepOnFinishAfter) * SecsPerDay);
  250. BOOLPROP(BalloonNotifications);
  251. DDExtEnabledButton->Checked = WinConfiguration->DDExtEnabled;
  252. DDExtDisabledButton->Checked = !DDExtEnabledButton->Checked;
  253. DDWarnOnMoveCheck->Checked = !WinConfiguration->DDAllowMove;
  254. if (WinConfiguration->DDTemporaryDirectory.IsEmpty())
  255. {
  256. DDSystemTemporaryDirectoryButton->Checked = true;
  257. DDTemporaryDirectoryEdit->Text = SystemTemporaryDirectory();
  258. }
  259. else
  260. {
  261. DDCustomTemporaryDirectoryButton->Checked = true;
  262. DDTemporaryDirectoryEdit->Text = WinConfiguration->DDTemporaryDirectory;
  263. }
  264. // Commander
  265. if (WinConfiguration->ScpCommander.NortonLikeMode == nlOff)
  266. {
  267. NortonLikeModeCombo->ItemIndex = 2;
  268. }
  269. else if (WinConfiguration->ScpCommander.NortonLikeMode == nlKeyboard)
  270. {
  271. NortonLikeModeCombo->ItemIndex = 1;
  272. }
  273. else
  274. {
  275. NortonLikeModeCombo->ItemIndex = 0;
  276. }
  277. SwappedPanelsCheck->Checked =
  278. WinConfiguration->ScpCommander.SwappedPanels;
  279. TreeOnLeftCheck->Checked = WinConfiguration->ScpCommander.TreeOnLeft;
  280. ExplorerKeyboardShortcutsCombo->ItemIndex =
  281. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts ? 1 : 0;
  282. BOOLPROP(UseLocationProfiles);
  283. CompareByTimeCheck->Checked = WinConfiguration->ScpCommander.CompareByTime;
  284. CompareBySizeCheck->Checked = WinConfiguration->ScpCommander.CompareBySize;
  285. // Local panel
  286. PreserveLocalDirectoryCheck->Checked =
  287. WinConfiguration->ScpCommander.PreserveLocalDirectory;
  288. SystemContextMenuCheck->Checked =
  289. WinConfiguration->ScpCommander.SystemContextMenu;
  290. // Explorer
  291. ShowFullAddressCheck->Checked =
  292. WinConfiguration->ScpExplorer.ShowFullAddress;
  293. // select none when stNul
  294. RegistryStorageButton->Checked = (Configuration->Storage == stRegistry);
  295. IniFileStorageButton2->Checked = (Configuration->Storage == stIniFile);
  296. RandomSeedFileEdit->Text = Configuration->RandomSeedFile;
  297. // editor
  298. EditorWordWrapCheck->Checked = WinConfiguration->Editor.WordWrap;
  299. EditorTabSizeEdit->AsInteger = WinConfiguration->Editor.TabSize;
  300. if (WinConfiguration->Editor.Encoding == CP_UTF8)
  301. {
  302. EditorEncodingCombo->ItemIndex = 1;
  303. }
  304. else
  305. {
  306. EditorEncodingCombo->ItemIndex = 0;
  307. }
  308. TWinConfiguration::RestoreFont(WinConfiguration->Editor.Font, FEditorFont.get());
  309. FEditorFont->Color = WinConfiguration->Editor.FontColor;
  310. FEditorBackgroundColor = WinConfiguration->Editor.BackgroundColor;
  311. (*FEditorList) = *WinConfiguration->EditorList;
  312. UpdateEditorListView();
  313. FCopyParams = GUIConfiguration->DefaultCopyParam;
  314. ResumeOnButton->Checked = GUIConfiguration->DefaultCopyParam.ResumeSupport == rsOn;
  315. ResumeSmartButton->Checked = GUIConfiguration->DefaultCopyParam.ResumeSupport == rsSmart;
  316. ResumeOffButton->Checked = GUIConfiguration->DefaultCopyParam.ResumeSupport == rsOff;
  317. ResumeThresholdEdit->Value = GUIConfiguration->DefaultCopyParam.ResumeThreshold / 1024;
  318. SessionReopenAutoCheck->Checked = (Configuration->SessionReopenAuto > 0);
  319. SessionReopenAutoEdit->Value = (Configuration->SessionReopenAuto > 0 ?
  320. (Configuration->SessionReopenAuto / MSecsPerSec) : 5);
  321. SessionReopenAutoIdleCheck->Checked = (GUIConfiguration->SessionReopenAutoIdle > 0);
  322. SessionReopenAutoIdleEdit->Value = (GUIConfiguration->SessionReopenAutoIdle > 0 ?
  323. (GUIConfiguration->SessionReopenAutoIdle / MSecsPerSec) : 5);
  324. SessionReopenAutoStallCheck->Checked = (Configuration->SessionReopenAutoStall > 0);
  325. SessionReopenAutoStallEdit->Value = (Configuration->SessionReopenAutoStall > 0 ?
  326. (Configuration->SessionReopenAutoStall / MSecsPerSec) : SecsPerMin);
  327. SessionReopenTimeoutEdit->Value = (Configuration->SessionReopenTimeout / MSecsPerSec);
  328. GeneralSheet->Enabled = WinConfiguration->ExpertMode;
  329. ExplorerSheet->Enabled = WinConfiguration->ExpertMode;
  330. CommanderSheet->Enabled = WinConfiguration->ExpertMode;
  331. EditorSheet->Enabled = WinConfiguration->ExpertMode && !WinConfiguration->DisableOpenEdit;
  332. StorageGroup->Visible = WinConfiguration->ExpertMode;
  333. RandomSeedFileLabel->Visible = WinConfiguration->ExpertMode;
  334. RandomSeedFileEdit->Visible = WinConfiguration->ExpertMode;
  335. FCustomCommandList->Assign(WinConfiguration->CustomCommandList);
  336. FExtensionList->Assign(WinConfiguration->ExtensionList);
  337. UpdateCustomCommandsView();
  338. FCustomCommandOptions->Assign(WinConfiguration->CustomCommandOptions);
  339. PuttyPathEdit->Text = GUIConfiguration->PuttyPath;
  340. PuttyPasswordCheck2->Checked = GUIConfiguration->PuttyPassword;
  341. AutoOpenInPuttyCheck->Checked = WinConfiguration->AutoOpenInPutty;
  342. TelnetForFtpInPuttyCheck->Checked = WinConfiguration->TelnetForFtpInPutty;
  343. SelectPuttyRegistryStorageKey(GUIConfiguration->PuttyRegistryStorageKey);
  344. // Queue
  345. QueueTransferLimitEdit->AsInteger = GUIConfiguration->QueueTransfersLimit;
  346. EnableQueueByDefaultCheck->Checked = WinConfiguration->EnableQueueByDefault;
  347. QueueAutoPopupCheck->Checked = GUIConfiguration->QueueAutoPopup;
  348. QueueCheck->Checked = GUIConfiguration->DefaultCopyParam.Queue;
  349. QueueIndividuallyCheck->Checked = GUIConfiguration->DefaultCopyParam.QueueIndividually;
  350. QueueNoConfirmationCheck->Checked = GUIConfiguration->DefaultCopyParam.QueueNoConfirmation;
  351. if (!GUIConfiguration->QueueKeepDoneItems)
  352. {
  353. QueueKeepDoneItemsForCombo->ItemIndex = 0;
  354. }
  355. else if (GUIConfiguration->QueueKeepDoneItemsFor < 0)
  356. {
  357. QueueKeepDoneItemsForCombo->ItemIndex = 5;
  358. }
  359. else if (GUIConfiguration->QueueKeepDoneItemsFor <= 15)
  360. {
  361. QueueKeepDoneItemsForCombo->ItemIndex = 1;
  362. }
  363. else if (GUIConfiguration->QueueKeepDoneItemsFor <= 60)
  364. {
  365. QueueKeepDoneItemsForCombo->ItemIndex = 2;
  366. }
  367. else if (GUIConfiguration->QueueKeepDoneItemsFor <= 15 * 60)
  368. {
  369. QueueKeepDoneItemsForCombo->ItemIndex = 3;
  370. }
  371. else if (GUIConfiguration->QueueKeepDoneItemsFor <= 60 * 60)
  372. {
  373. QueueKeepDoneItemsForCombo->ItemIndex = 4;
  374. }
  375. if (WinConfiguration->QueueView.Show == qvShow)
  376. {
  377. QueueViewShowButton->Checked = true;
  378. }
  379. else if (WinConfiguration->QueueView.Show == qvHideWhenEmpty)
  380. {
  381. QueueViewHideWhenEmptyButton->Checked = true;
  382. }
  383. else
  384. {
  385. QueueViewHideButton->Checked = true;
  386. }
  387. // window
  388. AutoSaveWorkspaceCheck->Checked = WinConfiguration->AutoSaveWorkspace;
  389. AutoWorkspaceCombo->Text =
  390. DefaultStr(WinConfiguration->AutoWorkspace,
  391. // It will rarely happen that LastWorkspace is set, while AutoWorkspace not.
  392. // It can happen only when user saved workspace before opening the Preferences
  393. // dialog for the first time
  394. DefaultStr(WinConfiguration->LastWorkspace, LoadStr(NEW_WORKSPACE)));
  395. AutoSaveWorkspacePasswordsCheck->Checked = WinConfiguration->AutoSaveWorkspacePasswords;
  396. if (WinConfiguration->PathInCaption == picFull)
  397. {
  398. PathInCaptionFullButton->Checked = true;
  399. }
  400. else if (WinConfiguration->PathInCaption == picShort)
  401. {
  402. PathInCaptionShortButton->Checked = true;
  403. }
  404. else
  405. {
  406. PathInCaptionNoneButton->Checked = true;
  407. }
  408. BOOLPROP(MinimizeToTray);
  409. BOOLPROP(ExternalSessionInExistingInstance);
  410. BOOLPROP(KeepOpenWhenNoSession);
  411. BOOLPROP(ShowTips);
  412. // panels
  413. DoubleClickActionCombo->ItemIndex = WinConfiguration->DoubleClickAction;
  414. BOOLPROP(AutoReadDirectoryAfterOp);
  415. BOOLPROP(RefreshRemotePanel);
  416. RefreshRemotePanelIntervalEdit->Value =
  417. int(static_cast<double>(WinConfiguration->RefreshRemotePanelInterval) * SecsPerDay);
  418. switch (WinConfiguration->FormatSizeBytes)
  419. {
  420. case fbNone:
  421. FormatSizeBytesCombo->ItemIndex = 0;
  422. break;
  423. case fbKilobytes:
  424. FormatSizeBytesCombo->ItemIndex = 1;
  425. break;
  426. case fbShort:
  427. FormatSizeBytesCombo->ItemIndex = 2;
  428. break;
  429. default:
  430. DebugFail();
  431. }
  432. bool CustomPanelFont = !WinConfiguration->PanelFont.FontName.IsEmpty();
  433. PanelFontCheck->Checked = CustomPanelFont;
  434. if (CustomPanelFont)
  435. {
  436. TWinConfiguration::RestoreFont(WinConfiguration->PanelFont, FPanelFont.get());
  437. }
  438. else
  439. {
  440. FPanelFont->Assign(WinConfiguration->SystemIconFont);
  441. }
  442. // updates
  443. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  444. if (int(Updates.Period) <= 0)
  445. {
  446. UpdatesPeriodCombo->ItemIndex = 0;
  447. }
  448. else if (int(Updates.Period) <= 1)
  449. {
  450. UpdatesPeriodCombo->ItemIndex = 1;
  451. }
  452. else if (int(Updates.Period) <= 7)
  453. {
  454. UpdatesPeriodCombo->ItemIndex = 2;
  455. }
  456. else
  457. {
  458. UpdatesPeriodCombo->ItemIndex = 3;
  459. }
  460. UpdatesShowOnStartup->Checked = Updates.ShowOnStartup;
  461. UpdatesAuthenticationEmailEdit->Text = Updates.AuthenticationEmail;
  462. FVerifiedUpdatesAuthenticationEmail = UpdatesAuthenticationEmailEdit->Text;
  463. CollectUsageCheck->Checked = Configuration->CollectUsage;
  464. ComboAutoSwitchLoad(UpdatesBetaVersionsCombo, Updates.BetaVersions);
  465. switch (Updates.ConnectionType)
  466. {
  467. case ctDirect:
  468. default:
  469. UpdatesDirectCheck->Checked = true;
  470. break;
  471. case ctAuto:
  472. UpdatesAutoCheck->Checked = true;
  473. break;
  474. case ctProxy:
  475. UpdatesProxyCheck->Checked = true;
  476. break;
  477. }
  478. UpdatesProxyHostEdit->Text = Updates.ProxyHost;
  479. UpdatesProxyPortEdit->AsInteger = Updates.ProxyPort;
  480. // presets
  481. (*FCopyParamList) = *WinConfiguration->CopyParamList;
  482. UpdateCopyParamListView();
  483. CopyParamListView->ItemIndex = 0;
  484. BOOLPROP(CopyParamAutoSelectNotice);
  485. // interface
  486. switch (CustomWinConfiguration->Interface)
  487. {
  488. case ifCommander:
  489. CommanderInterfaceButton2->Checked = true;
  490. break;
  491. case ifExplorer:
  492. ExplorerInterfaceButton2->Checked = true;
  493. break;
  494. default:
  495. DebugFail();
  496. break;
  497. }
  498. // security
  499. UseMasterPasswordCheck->Checked = WinConfiguration->UseMasterPassword;
  500. SessionRememberPasswordCheck->Checked = GUIConfiguration->SessionRememberPassword;
  501. // network
  502. RetrieveExternalIpAddressButton->Checked = Configuration->ExternalIpAddress.IsEmpty();
  503. CustomExternalIpAddressButton->Checked = !RetrieveExternalIpAddressButton->Checked;
  504. CustomExternalIpAddressEdit->Text = Configuration->ExternalIpAddress;
  505. TryFtpWhenSshFailsCheck->Checked = Configuration->TryFtpWhenSshFails;
  506. // logging
  507. EnableLoggingCheck->Checked = Configuration->Logging;
  508. LogProtocolCombo->ItemIndex = Configuration->LogProtocol;
  509. LogToFileCheck->Checked = Configuration->LogToFile;
  510. LogFileNameEdit3->Text =
  511. !Configuration->LogFileName.IsEmpty() ? Configuration->LogFileName : Configuration->DefaultLogFileName;
  512. if (Configuration->LogFileAppend)
  513. {
  514. LogFileAppendButton->Checked = true;
  515. }
  516. else
  517. {
  518. LogFileOverwriteButton->Checked = true;
  519. }
  520. LogShowWindowCheck->Checked = (CustomWinConfiguration->LogView == lvWindow);
  521. if (Configuration->LogWindowComplete)
  522. {
  523. LogWindowCompleteButton->Checked = true;
  524. }
  525. else
  526. {
  527. LogWindowLinesButton->Checked = true;
  528. }
  529. if (!Configuration->LogWindowComplete)
  530. {
  531. LogWindowLinesEdit->AsInteger = Configuration->LogWindowLines;
  532. }
  533. else
  534. {
  535. LogWindowLinesEdit->AsInteger = 500;
  536. }
  537. BOOLPROP(LogSensitive);
  538. EnableActionsLoggingCheck->Checked = Configuration->LogActions;
  539. ActionsLogFileNameEdit->Text = Configuration->ActionsLogFileName;
  540. #undef BOOLPROP
  541. }
  542. __finally
  543. {
  544. FNoUpdate--;
  545. }
  546. UpdateControls();
  547. }
  548. //---------------------------------------------------------------------------
  549. void __fastcall TPreferencesDialog::SaveConfiguration()
  550. {
  551. Configuration->BeginUpdate();
  552. try
  553. {
  554. TGUICopyParamType CopyParam = GUIConfiguration->DefaultCopyParam;
  555. #define BOOLPROP(PROP) WinConfiguration->PROP = PROP ## Check->Checked
  556. BOOLPROP(DefaultDirIsHome);
  557. BOOLPROP(PreservePanelState);
  558. BOOLPROP(DeleteToRecycleBin);
  559. BOOLPROP(DDWarnLackOfTempSpace);
  560. BOOLPROP(ShowHiddenFiles);
  561. BOOLPROP(RenameWholeName);
  562. BOOLPROP(ShowInaccesibleDirectories);
  563. BOOLPROP(CopyOnDoubleClickConfirmation);
  564. BOOLPROP(ConfirmTransferring);
  565. BOOLPROP(ConfirmOverwriting);
  566. BOOLPROP(ConfirmResume);
  567. BOOLPROP(ConfirmDeleting);
  568. BOOLPROP(ConfirmRecycling);
  569. BOOLPROP(ConfirmExitOnCompletion);
  570. BOOLPROP(ConfirmCommandSession);
  571. BOOLPROP(ContinueOnError);
  572. BOOLPROP(DDAllowMoveInit);
  573. BOOLPROP(BeepOnFinish);
  574. BOOLPROP(TemporaryDirectoryAppendSession);
  575. BOOLPROP(TemporaryDirectoryAppendPath);
  576. BOOLPROP(TemporaryDirectoryDeterministic);
  577. BOOLPROP(TemporaryDirectoryCleanup);
  578. BOOLPROP(ConfirmTemporaryDirectoryCleanup);
  579. BOOLPROP(FullRowSelect);
  580. WinConfiguration->ConfirmClosingSession = ConfirmClosingSessionCheck2->Checked;
  581. WinConfiguration->DDTransferConfirmation =
  582. CheckBoxAutoSwitchSave(DDTransferConfirmationCheck);
  583. GUIConfiguration->BeepOnFinishAfter =
  584. static_cast<double>(BeepOnFinishAfterEdit->Value / SecsPerDay);
  585. BOOLPROP(BalloonNotifications);
  586. WinConfiguration->DDAllowMove = !DDWarnOnMoveCheck->Checked;
  587. WinConfiguration->DDExtEnabled = DDExtEnabledButton->Checked;
  588. if (DDSystemTemporaryDirectoryButton->Checked)
  589. {
  590. WinConfiguration->DDTemporaryDirectory = L"";
  591. }
  592. else
  593. {
  594. WinConfiguration->DDTemporaryDirectory = DDTemporaryDirectoryEdit->Text;
  595. }
  596. // Commander
  597. TScpCommanderConfiguration ScpCommander = WinConfiguration->ScpCommander;
  598. if (NortonLikeModeCombo->ItemIndex == 2)
  599. {
  600. ScpCommander.NortonLikeMode = nlOff;
  601. }
  602. else if (NortonLikeModeCombo->ItemIndex == 1)
  603. {
  604. ScpCommander.NortonLikeMode = nlKeyboard;
  605. }
  606. else
  607. {
  608. ScpCommander.NortonLikeMode = nlOn;
  609. }
  610. ScpCommander.SwappedPanels = SwappedPanelsCheck->Checked;
  611. ScpCommander.TreeOnLeft = TreeOnLeftCheck->Checked;
  612. ScpCommander.ExplorerKeyboardShortcuts =
  613. (ExplorerKeyboardShortcutsCombo->ItemIndex != 0);
  614. BOOLPROP(UseLocationProfiles);
  615. ScpCommander.CompareByTime = CompareByTimeCheck->Checked;
  616. ScpCommander.CompareBySize = CompareBySizeCheck->Checked;
  617. // Local panel
  618. ScpCommander.PreserveLocalDirectory = PreserveLocalDirectoryCheck->Checked;
  619. ScpCommander.SystemContextMenu = SystemContextMenuCheck->Checked;
  620. WinConfiguration->ScpCommander = ScpCommander;
  621. // Explorer
  622. TScpExplorerConfiguration ScpExplorer = WinConfiguration->ScpExplorer;
  623. ScpExplorer.ShowFullAddress = ShowFullAddressCheck->Checked;
  624. WinConfiguration->ScpExplorer = ScpExplorer;
  625. Configuration->RandomSeedFile = RandomSeedFileEdit->Text;
  626. // editor
  627. WinConfiguration->Editor.WordWrap = EditorWordWrapCheck->Checked;
  628. WinConfiguration->Editor.TabSize = EditorTabSizeEdit->AsInteger;
  629. switch (EditorEncodingCombo->ItemIndex)
  630. {
  631. case 1:
  632. WinConfiguration->Editor.Encoding = CP_UTF8;
  633. break;
  634. default:
  635. WinConfiguration->Editor.Encoding = CP_ACP;
  636. break;
  637. }
  638. TWinConfiguration::StoreFont(FEditorFont.get(), WinConfiguration->Editor.Font);
  639. WinConfiguration->Editor.FontColor = FEditorFont->Color;
  640. WinConfiguration->Editor.BackgroundColor = FEditorBackgroundColor;
  641. WinConfiguration->EditorList = FEditorList;
  642. // overwrites only TCopyParamType fields
  643. CopyParam = FCopyParams;
  644. if (ResumeOnButton->Checked) CopyParam.ResumeSupport = rsOn;
  645. if (ResumeSmartButton->Checked) CopyParam.ResumeSupport = rsSmart;
  646. if (ResumeOffButton->Checked) CopyParam.ResumeSupport = rsOff;
  647. CopyParam.ResumeThreshold = ResumeThresholdEdit->AsInteger * 1024;
  648. Configuration->SessionReopenAuto =
  649. (SessionReopenAutoCheck->Checked ? (SessionReopenAutoEdit->AsInteger * MSecsPerSec) : 0);
  650. GUIConfiguration->SessionReopenAutoIdle =
  651. (SessionReopenAutoIdleCheck->Checked ? (SessionReopenAutoIdleEdit->AsInteger * MSecsPerSec) : 0);
  652. Configuration->SessionReopenAutoStall =
  653. (SessionReopenAutoStallCheck->Checked ? (SessionReopenAutoStallEdit->AsInteger * MSecsPerSec) : 0);
  654. Configuration->SessionReopenTimeout = (SessionReopenTimeoutEdit->AsInteger * MSecsPerSec);
  655. WinConfiguration->CustomCommandList = FCustomCommandList;
  656. WinConfiguration->ExtensionList = FExtensionList;
  657. WinConfiguration->CustomCommandOptions = FCustomCommandOptions.get();
  658. GUIConfiguration->PuttyPath = PuttyPathEdit->Text;
  659. GUIConfiguration->PuttyPassword = PuttyPasswordCheck2->Checked;
  660. WinConfiguration->AutoOpenInPutty = AutoOpenInPuttyCheck->Checked;
  661. WinConfiguration->TelnetForFtpInPutty = TelnetForFtpInPuttyCheck->Checked;
  662. // do not overwrite custom keys
  663. if (PuttyRegistryStorageKeyEdit->ItemIndex >= 0)
  664. {
  665. GUIConfiguration->PuttyRegistryStorageKey = PuttyRegistryStorageKeyEdit->Text;
  666. }
  667. // Queue
  668. GUIConfiguration->QueueTransfersLimit = QueueTransferLimitEdit->AsInteger;
  669. WinConfiguration->EnableQueueByDefault = EnableQueueByDefaultCheck->Checked;
  670. GUIConfiguration->QueueAutoPopup = QueueAutoPopupCheck->Checked;
  671. CopyParam.Queue = QueueCheck->Checked;
  672. CopyParam.QueueIndividually = QueueIndividuallyCheck->Checked;
  673. CopyParam.QueueNoConfirmation = QueueNoConfirmationCheck->Checked;
  674. GUIConfiguration->QueueKeepDoneItems = (QueueKeepDoneItemsForCombo->ItemIndex != 0);
  675. switch (QueueKeepDoneItemsForCombo->ItemIndex)
  676. {
  677. case 0:
  678. GUIConfiguration->QueueKeepDoneItemsFor = 0;
  679. break;
  680. case 1:
  681. GUIConfiguration->QueueKeepDoneItemsFor = 15;
  682. break;
  683. case 2:
  684. GUIConfiguration->QueueKeepDoneItemsFor = 60;
  685. break;
  686. case 3:
  687. GUIConfiguration->QueueKeepDoneItemsFor = 15 * 60;
  688. break;
  689. case 4:
  690. GUIConfiguration->QueueKeepDoneItemsFor = 60 * 60;
  691. break;
  692. default:
  693. GUIConfiguration->QueueKeepDoneItemsFor = -1;
  694. break;
  695. }
  696. if (QueueViewShowButton->Checked)
  697. {
  698. WinConfiguration->QueueView.Show = qvShow;
  699. }
  700. else if (QueueViewHideWhenEmptyButton->Checked)
  701. {
  702. WinConfiguration->QueueView.Show = qvHideWhenEmpty;
  703. }
  704. else
  705. {
  706. WinConfiguration->QueueView.Show = qvHide;
  707. }
  708. GUIConfiguration->DefaultCopyParam = CopyParam;
  709. // window
  710. WinConfiguration->AutoSaveWorkspace =
  711. !AutoWorkspaceCombo->Text.IsEmpty() &&
  712. AutoSaveWorkspaceCheck->Checked;
  713. if (!AutoWorkspaceCombo->Text.IsEmpty())
  714. {
  715. WinConfiguration->AutoWorkspace = AutoWorkspaceCombo->Text;
  716. }
  717. WinConfiguration->AutoSaveWorkspacePasswords = AutoSaveWorkspacePasswordsCheck->Checked;
  718. if (PathInCaptionFullButton->Checked)
  719. {
  720. WinConfiguration->PathInCaption = picFull;
  721. }
  722. else if (PathInCaptionShortButton->Checked)
  723. {
  724. WinConfiguration->PathInCaption = picShort;
  725. }
  726. else
  727. {
  728. WinConfiguration->PathInCaption = picNone;
  729. }
  730. BOOLPROP(MinimizeToTray);
  731. BOOLPROP(ExternalSessionInExistingInstance);
  732. BOOLPROP(KeepOpenWhenNoSession);
  733. BOOLPROP(ShowTips);
  734. // panels
  735. WinConfiguration->DoubleClickAction = (TDoubleClickAction)DoubleClickActionCombo->ItemIndex;
  736. BOOLPROP(AutoReadDirectoryAfterOp);
  737. BOOLPROP(RefreshRemotePanel);
  738. WinConfiguration->RefreshRemotePanelInterval =
  739. static_cast<double>(RefreshRemotePanelIntervalEdit->Value / SecsPerDay);
  740. switch (FormatSizeBytesCombo->ItemIndex)
  741. {
  742. case 0:
  743. WinConfiguration->FormatSizeBytes = fbNone;
  744. break;
  745. case 1:
  746. WinConfiguration->FormatSizeBytes = fbKilobytes;
  747. break;
  748. case 2:
  749. WinConfiguration->FormatSizeBytes = fbShort;
  750. break;
  751. default:
  752. DebugFail();
  753. }
  754. TFontConfiguration PanelFontConfiguration;
  755. if (PanelFontCheck->Checked)
  756. {
  757. TWinConfiguration::StoreFont(FPanelFont.get(), PanelFontConfiguration);
  758. }
  759. WinConfiguration->PanelFont = PanelFontConfiguration;
  760. // updates
  761. WinConfiguration->Updates = SaveUpdates();
  762. Configuration->CollectUsage = CollectUsageCheck->Checked;
  763. // presets
  764. WinConfiguration->CopyParamList = FCopyParamList;
  765. BOOLPROP(CopyParamAutoSelectNotice);
  766. // interface
  767. if (GetInterface() != CustomWinConfiguration->Interface)
  768. {
  769. Configuration->Usage->Inc(L"InterfaceChanges");
  770. }
  771. CustomWinConfiguration->Interface = GetInterface();
  772. // network
  773. Configuration->ExternalIpAddress =
  774. (CustomExternalIpAddressButton->Checked ? CustomExternalIpAddressEdit->Text : UnicodeString());
  775. Configuration->TryFtpWhenSshFails = TryFtpWhenSshFailsCheck->Checked;
  776. // security
  777. GUIConfiguration->SessionRememberPassword = SessionRememberPasswordCheck->Checked;
  778. // logging
  779. Configuration->Logging = EnableLoggingCheck->Checked;
  780. Configuration->LogProtocol = LogProtocolCombo->ItemIndex;
  781. Configuration->LogFileName = LogToFileCheck->Checked ? LogFileNameEdit3->Text : UnicodeString();
  782. Configuration->LogFileAppend = LogFileAppendButton->Checked;
  783. CustomWinConfiguration->LogView = LogShowWindowCheck->Checked ? lvWindow : lvNone;
  784. Configuration->LogWindowComplete = LogWindowCompleteButton->Checked;
  785. if (!LogWindowCompleteButton->Checked)
  786. {
  787. Configuration->LogWindowLines = LogWindowLinesEdit->AsInteger;
  788. }
  789. BOOLPROP(LogSensitive);
  790. Configuration->LogActions = EnableActionsLoggingCheck->Checked;
  791. Configuration->ActionsLogFileName = ActionsLogFileNameEdit->Text;
  792. // languages
  793. // As this dialog does not explicitly support run-time locale changing,
  794. // make this last, otherwise we lose some settings (or even worse
  795. // we end-up saving default text box values=control names as our configuration)
  796. if (LanguagesView->ItemFocused != NULL)
  797. {
  798. GUIConfiguration->Locale =
  799. reinterpret_cast<LCID>(LanguagesView->ItemFocused->Data);
  800. }
  801. // This possibly fails, make it last, so that the other settings are preserved.
  802. // Do nothing when no option is selected (i.e. storage is stNul).
  803. if (RegistryStorageButton->Checked)
  804. {
  805. Configuration->Storage = stRegistry;
  806. }
  807. else if (IniFileStorageButton2->Checked)
  808. {
  809. Configuration->Storage = stIniFile;
  810. }
  811. #undef BOOLPROP
  812. }
  813. __finally
  814. {
  815. Configuration->EndUpdate();
  816. }
  817. }
  818. //---------------------------------------------------------------------------
  819. TUpdatesConfiguration __fastcall TPreferencesDialog::SaveUpdates()
  820. {
  821. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  822. if (UpdatesPeriodCombo->ItemIndex == 0)
  823. {
  824. Updates.Period = 0;
  825. }
  826. else if (UpdatesPeriodCombo->ItemIndex == 1)
  827. {
  828. Updates.Period = 1;
  829. }
  830. else if (UpdatesPeriodCombo->ItemIndex == 2)
  831. {
  832. Updates.Period = 7;
  833. }
  834. else
  835. {
  836. Updates.Period = 30;
  837. }
  838. Updates.ShowOnStartup = UpdatesShowOnStartup->Checked;
  839. Updates.AuthenticationEmail = UpdatesAuthenticationEmailEdit->Text;
  840. Updates.BetaVersions = ComboAutoSwitchSave(UpdatesBetaVersionsCombo);
  841. if (UpdatesDirectCheck->Checked)
  842. {
  843. Updates.ConnectionType = ctDirect;
  844. }
  845. else if (UpdatesAutoCheck->Checked)
  846. {
  847. Updates.ConnectionType = ctAuto;
  848. }
  849. else if (UpdatesProxyCheck->Checked)
  850. {
  851. if (!UpdatesProxyHostEdit->Text.IsEmpty())
  852. {
  853. Updates.ConnectionType = ctProxy;
  854. }
  855. else
  856. {
  857. Updates.ConnectionType = ctDirect;
  858. }
  859. }
  860. Updates.ProxyHost = UpdatesProxyHostEdit->Text;
  861. Updates.ProxyPort = UpdatesProxyPortEdit->AsInteger;
  862. return Updates;
  863. }
  864. //---------------------------------------------------------------------------
  865. TInterface __fastcall TPreferencesDialog::GetInterface()
  866. {
  867. return CommanderInterfaceButton2->Checked ? ifCommander : ifExplorer;
  868. }
  869. //---------------------------------------------------------------------------
  870. void __fastcall TPreferencesDialog::FormShow(TObject * /*Sender*/)
  871. {
  872. // Load only now, particularly so that we have handles allocated already.
  873. // If called before ShowModal, we end up recreating the handles,
  874. // what causes flicker of the currently focused window
  875. LoadConfiguration();
  876. InstallPathWordBreakProc(RandomSeedFileEdit);
  877. InstallPathWordBreakProc(DDTemporaryDirectoryEdit);
  878. InstallPathWordBreakProc(PuttyPathEdit);
  879. InstallPathWordBreakProc(LogFileNameEdit3);
  880. InstallPathWordBreakProc(ActionsLogFileNameEdit);
  881. ListView_SetExtendedListViewStyle(CustomCommandsView->Handle, (ListView_GetExtendedListViewStyle(CustomCommandsView->Handle) & ~LVS_EX_INFOTIP));
  882. PrepareNavigationTree(NavigationTree);
  883. switch (FPreferencesMode) {
  884. case pmEditor: PageControl->ActivePage = EditorSheet; break;
  885. case pmCustomCommands: PageControl->ActivePage = CustomCommandsSheet; break;
  886. case pmQueue: PageControl->ActivePage = QueueSheet; break;
  887. case pmLogging: PageControl->ActivePage = LogSheet; break;
  888. case pmUpdates: PageControl->ActivePage = UpdatesSheet; break;
  889. case pmPresets: PageControl->ActivePage = CopyParamListSheet; break;
  890. case pmEditors: PageControl->ActivePage = EditorSheet; break;
  891. case pmCommander: PageControl->ActivePage = CommanderSheet; break;
  892. case pmEditorInternal: PageControl->ActivePage = EditorInternalSheet; break;
  893. default: PageControl->ActivePage = PreferencesSheet; break;
  894. }
  895. PageControlChange(NULL);
  896. ActiveControl = NavigationTree;
  897. }
  898. //---------------------------------------------------------------------------
  899. void __fastcall TPreferencesDialog::ControlChange(TObject * /*Sender*/)
  900. {
  901. UpdateControls();
  902. }
  903. //---------------------------------------------------------------------------
  904. UnicodeString __fastcall TPreferencesDialog::TabSample(UnicodeString Values)
  905. {
  906. UnicodeString Result;
  907. for (int Index = 1; Index <= Values.Length(); Index++)
  908. {
  909. if (Index > 1)
  910. {
  911. Result += L' ';
  912. if (EditorTabSizeEdit->AsInteger > 2)
  913. {
  914. Result += UnicodeString::StringOfChar(L' ', EditorTabSizeEdit->AsInteger - 2);
  915. }
  916. }
  917. Result += Values[Index];
  918. }
  919. return Result;
  920. }
  921. //---------------------------------------------------------------------------
  922. TCustomCommandList * __fastcall TPreferencesDialog::GetCommandList(int Index)
  923. {
  924. if (Index < FCustomCommandList->Count)
  925. {
  926. return FCustomCommandList;
  927. }
  928. else
  929. {
  930. return FExtensionList;
  931. }
  932. }
  933. //---------------------------------------------------------------------------
  934. int __fastcall TPreferencesDialog::GetCommandIndex(int Index)
  935. {
  936. if (Index >= FCustomCommandList->Count)
  937. {
  938. Index -= FCustomCommandList->Count;
  939. }
  940. return Index;
  941. }
  942. //---------------------------------------------------------------------------
  943. int __fastcall TPreferencesDialog::GetListCommandIndex(TCustomCommandList * List)
  944. {
  945. int Index;
  946. if (GetCommandList(CustomCommandsView->ItemIndex) == List)
  947. {
  948. Index = GetCommandIndex(CustomCommandsView->ItemIndex);
  949. }
  950. else
  951. {
  952. Index = -1;
  953. }
  954. return Index;
  955. }
  956. //---------------------------------------------------------------------------
  957. int __fastcall TPreferencesDialog::GetCommandListIndex(TCustomCommandList * List, int Index)
  958. {
  959. if (List == FExtensionList)
  960. {
  961. Index += FCustomCommandList->Count;
  962. }
  963. return Index;
  964. }
  965. //---------------------------------------------------------------------------
  966. void __fastcall TPreferencesDialog::UpdateControls()
  967. {
  968. if (FNoUpdate == 0)
  969. {
  970. EnableControl(BeepOnFinishAfterEdit, BeepOnFinishCheck->Checked);
  971. EnableControl(BeepOnFinishAfterText, BeepOnFinishCheck->Checked);
  972. EnableControl(ResumeThresholdEdit, ResumeSmartButton->Checked);
  973. EnableControl(ResumeThresholdUnitLabel2, ResumeThresholdEdit->Enabled);
  974. EnableControl(SessionReopenAutoEdit, SessionReopenAutoCheck->Checked);
  975. EnableControl(SessionReopenAutoLabel, SessionReopenAutoEdit->Enabled);
  976. EnableControl(SessionReopenAutoSecLabel, SessionReopenAutoEdit->Enabled);
  977. EnableControl(SessionReopenAutoIdleEdit, SessionReopenAutoIdleCheck->Checked);
  978. EnableControl(SessionReopenAutoIdleLabel, SessionReopenAutoIdleEdit->Enabled);
  979. EnableControl(SessionReopenAutoIdleSecLabel, SessionReopenAutoIdleEdit->Enabled);
  980. EnableControl(SessionReopenAutoStallEdit, SessionReopenAutoStallCheck->Checked);
  981. EnableControl(SessionReopenAutoStallLabel, SessionReopenAutoStallEdit->Enabled);
  982. EnableControl(SessionReopenAutoStallSecLabel, SessionReopenAutoStallEdit->Enabled);
  983. EnableControl(SessionReopenTimeoutEdit,
  984. SessionReopenAutoEdit->Enabled || SessionReopenAutoStallCheck->Checked);
  985. EnableControl(SessionReopenTimeoutLabel, SessionReopenTimeoutEdit->Enabled);
  986. EnableControl(SessionReopenTimeoutSecLabel,SessionReopenTimeoutEdit->Enabled);
  987. EnableControl(CopyOnDoubleClickConfirmationCheck,
  988. (DoubleClickActionCombo->ItemIndex == 1) && ConfirmTransferringCheck->Checked);
  989. TFont * ActualPanelFont = PanelFontCheck->Checked ? FPanelFont.get() : WinConfiguration->SystemIconFont;
  990. UnicodeString PanelFontLabelText;
  991. PanelFontLabelText = FMTLOAD(EDITOR_FONT_FMT,
  992. (ActualPanelFont->Name, ActualPanelFont->Size));
  993. PanelFontLabel->Caption = PanelFontLabelText;
  994. if (!SameFont(PanelFontLabel->Font, ActualPanelFont))
  995. {
  996. PanelFontLabel->Font = ActualPanelFont;
  997. }
  998. EnableControl(RefreshRemotePanelIntervalEdit, RefreshRemotePanelCheck->Checked);
  999. EnableControl(RefreshRemoteDirectoryUnitLabel, RefreshRemotePanelCheck->Checked);
  1000. UnicodeString EditorFontLabelText;
  1001. EditorFontLabelText = FMTLOAD(EDITOR_FONT_FMT,
  1002. (FEditorFont->Name, FEditorFont->Size)) + L"\n\n";
  1003. EditorFontLabelText += TabSample(L"ABCD") + L"\n";
  1004. EditorFontLabelText += TabSample(L"1234");
  1005. EditorFontLabel->Caption = EditorFontLabelText;
  1006. TColor EditorFontColor = GetWindowTextColor(FEditorFont->Color);
  1007. if (!SameFont(EditorFontLabel->Font, FEditorFont.get()) ||
  1008. (EditorFontLabel->Font->Color != EditorFontColor))
  1009. {
  1010. std::unique_ptr<TFont> Font(new TFont);
  1011. Font->Assign(FEditorFont.get());
  1012. Font->Color = EditorFontColor;
  1013. EditorFontLabel->Font = Font.get();
  1014. }
  1015. EditorFontLabel->Color = GetWindowColor(FEditorBackgroundColor);
  1016. TCustomCommandList * CommandList = GetCommandList(CustomCommandsView->ItemIndex);
  1017. int CommandIndex = GetCommandIndex(CustomCommandsView->ItemIndex);
  1018. bool CommandSelected = (CustomCommandsView->Selected != NULL);
  1019. bool CustomCommandSelected = CommandSelected && (CommandList == FCustomCommandList);
  1020. bool ExtensionSelected = CommandSelected && (CommandList == FExtensionList);
  1021. EnableControl(EditCommandButton, CustomCommandSelected);
  1022. EditCommandButton->Visible = !ExtensionSelected;
  1023. EnableControl(ConfigureCommandButton, ExtensionSelected && (CommandList->Commands[CommandIndex]->OptionsCount > 0));
  1024. ConfigureCommandButton->Visible = ExtensionSelected;
  1025. EnableControl(RemoveCommandButton, CommandSelected);
  1026. EnableControl(UpCommandButton, CommandSelected && (CommandIndex > 0));
  1027. EnableControl(DownCommandButton, CommandSelected && (CommandIndex < CommandList->Count - 1));
  1028. bool CopyParamSelected = (CopyParamListView->Selected != NULL);
  1029. EnableControl(EditCopyParamButton, CopyParamSelected);
  1030. EnableControl(DuplicateCopyParamButton, CopyParamSelected);
  1031. EnableControl(RemoveCopyParamButton,
  1032. CopyParamSelected && (CopyParamListView->ItemIndex >= 1));
  1033. EnableControl(UpCopyParamButton,
  1034. CopyParamSelected && (CopyParamListView->ItemIndex > 1));
  1035. EnableControl(DownCopyParamButton,
  1036. CopyParamSelected &&
  1037. (CopyParamListView->ItemIndex >= 1) &&
  1038. (CopyParamListView->ItemIndex < CopyParamListView->Items->Count - 1));
  1039. EnableControl(CopyParamAutoSelectNoticeCheck, FCopyParamList->AnyRule);
  1040. UnicodeString InfoStr;
  1041. if (CopyParamSelected)
  1042. {
  1043. const TCopyParamType * SelectedCopyParam = GetCopyParam(CopyParamListView->ItemIndex);
  1044. InfoStr = SelectedCopyParam->GetInfoStr(L"; ", 0);
  1045. if (CopyParamListView->ItemIndex >= 1)
  1046. {
  1047. const TCopyParamRule * Rule = FCopyParamList->Rules[CopyParamListView->ItemIndex - 1];
  1048. if (Rule != NULL)
  1049. {
  1050. InfoStr += L"\n" + FORMAT(ReplaceStr(LoadStr(COPY_PARAM_RULE), L"\n", L" "), (Rule->GetInfoStr(L"; ")));
  1051. }
  1052. }
  1053. }
  1054. SetLabelHintPopup(CopyParamLabel, InfoStr);
  1055. EnableControl(DDExtEnabledButton, WinConfiguration->DDExtInstalled);
  1056. EnableControl(DDExtEnabledLabel, WinConfiguration->DDExtInstalled);
  1057. EnableControl(DDExtDisabledPanel, DDExtDisabledButton->Checked);
  1058. EnableControl(DDTemporaryDirectoryEdit, DDCustomTemporaryDirectoryButton->Enabled &&
  1059. DDCustomTemporaryDirectoryButton->Checked);
  1060. EnableControl(DDWarnOnMoveCheck, DDExtDisabledButton->Checked &&
  1061. DDAllowMoveInitCheck->Checked);
  1062. EnableControl(ConfirmTemporaryDirectoryCleanupCheck,
  1063. TemporaryDirectoryCleanupCheck->Checked);
  1064. // allow only when some of the known storages is selected,
  1065. // and particularly do not allow switching storage, when we start with stNul,
  1066. // as that would destroy the stored configuration
  1067. EnableControl(StorageGroup, RegistryStorageButton->Checked || IniFileStorageButton2->Checked);
  1068. IniFileStorageButton2->Caption =
  1069. AnsiReplaceStr(IniFileStorageButton2->Caption, L"(winscp.ini)",
  1070. FORMAT(L"(%s)", (ExpandEnvironmentVariables(Configuration->IniFileStorageName))));
  1071. EditorFontLabel->WordWrap = EditorWordWrapCheck->Checked;
  1072. bool EditorSelected = (EditorListView3->Selected != NULL);
  1073. EnableControl(EditEditorButton, EditorSelected);
  1074. EnableControl(RemoveEditorButton, EditorSelected);
  1075. EnableControl(UpEditorButton, EditorSelected &&
  1076. (EditorListView3->ItemIndex > 0));
  1077. EnableControl(DownEditorButton, EditorSelected &&
  1078. (EditorListView3->ItemIndex < EditorListView3->Items->Count - 1));
  1079. // updates
  1080. EnableControl(UpdatesAuthenticationEmailEdit, FAutomaticUpdatesPossible);
  1081. EnableControl(UpdatesAuthenticationEmailLabel, UpdatesAuthenticationEmailEdit->Enabled);
  1082. EnableControl(UsageViewButton, CollectUsageCheck->Checked);
  1083. EnableControl(UpdatesProxyHostEdit, UpdatesProxyCheck->Checked);
  1084. EnableControl(UpdatesProxyHostLabel, UpdatesProxyHostEdit->Enabled);
  1085. EnableControl(UpdatesProxyPortEdit, UpdatesProxyCheck->Checked);
  1086. EnableControl(UpdatesProxyPortLabel, UpdatesProxyPortEdit->Enabled);
  1087. bool IsSiteCommand = false;
  1088. try
  1089. {
  1090. TRemoteCustomCommand RemoteCustomCommand;
  1091. TInteractiveCustomCommand InteractiveCustomCommand(&RemoteCustomCommand);
  1092. UnicodeString PuttyPath = PuttyPathEdit->Text;
  1093. PuttyPath = InteractiveCustomCommand.Complete(PuttyPath, false);
  1094. IsSiteCommand = RemoteCustomCommand.IsSiteCommand(PuttyPath);
  1095. }
  1096. catch (...)
  1097. {
  1098. // noop
  1099. }
  1100. EnableControl(PuttyPasswordCheck2, !PuttyPathEdit->Text.IsEmpty());
  1101. EnableControl(AutoOpenInPuttyCheck, PuttyPasswordCheck2->Enabled);
  1102. EnableControl(TelnetForFtpInPuttyCheck,
  1103. PuttyPasswordCheck2->Enabled && !IsSiteCommand);
  1104. EnableControl(PuttyRegistryStorageKeyEdit,
  1105. PuttyPasswordCheck2->Enabled && !IsSiteCommand);
  1106. EnableControl(PuttyRegistryStorageKeyLabel, PuttyRegistryStorageKeyEdit->Enabled);
  1107. EnableControl(SetMasterPasswordButton, WinConfiguration->UseMasterPassword);
  1108. // network
  1109. EnableControl(CustomExternalIpAddressEdit, CustomExternalIpAddressButton->Checked);
  1110. // window
  1111. EnableControl(AutoWorkspaceCombo, AutoSaveWorkspaceCheck->Checked);
  1112. EnableControl(AutoWorkspaceLabel, AutoWorkspaceCombo->Enabled);
  1113. EnableControl(AutoSaveWorkspacePasswordsCheck,
  1114. !Configuration->DisablePasswordStoring &&
  1115. AutoWorkspaceCombo->Enabled);
  1116. // integration
  1117. // There's no quick launch in Windows 7
  1118. EnableControl(QuickLaunchIconButton, !IsWin7());
  1119. MakeDefaultHandlerItem->Visible = IsWinVista();
  1120. // languages
  1121. LanguageChangeLabel->Visible =
  1122. DebugAlwaysTrue(!GUIConfiguration->CanApplyLocaleImmediately) &&
  1123. (LanguagesView->ItemFocused != NULL) &&
  1124. (reinterpret_cast<LCID>(LanguagesView->ItemFocused->Data) != GUIConfiguration->AppliedLocale);
  1125. // logging
  1126. EnableControl(LogProtocolCombo, EnableLoggingCheck->Checked);
  1127. EnableControl(LogToFileCheck, LogProtocolCombo->Enabled);
  1128. EnableControl(LogFileNameEdit3, LogToFileCheck->Enabled && LogToFileCheck->Checked);
  1129. EnableControl(LogFileNameHintText, LogFileNameEdit3->Enabled);
  1130. EnableControl(LogFileAppendButton, LogFileNameEdit3->Enabled);
  1131. EnableControl(LogFileOverwriteButton, LogFileNameEdit3->Enabled);
  1132. EnableControl(LogShowWindowCheck, LogProtocolCombo->Enabled);
  1133. EnableControl(LogWindowCompleteButton, LogShowWindowCheck->Enabled && LogShowWindowCheck->Checked);
  1134. EnableControl(LogWindowLinesButton, LogWindowCompleteButton->Enabled);
  1135. EnableControl(LogWindowLinesEdit, LogWindowLinesButton->Enabled && LogWindowLinesButton->Checked);
  1136. EnableControl(LogWindowLinesText, LogWindowLinesEdit->Enabled);
  1137. EnableControl(LogSensitiveCheck, LogProtocolCombo->Enabled);
  1138. EnableControl(ActionsLogFileNameEdit, EnableActionsLoggingCheck->Checked);
  1139. EnableControl(ActionsLogFileNameHintText, ActionsLogFileNameEdit->Enabled);
  1140. // interface
  1141. InterfaceChangeLabel->Visible =
  1142. !CustomWinConfiguration->CanApplyInterfaceImmediately &&
  1143. (GetInterface() != CustomWinConfiguration->AppliedInterface);
  1144. }
  1145. }
  1146. //---------------------------------------------------------------------------
  1147. void __fastcall TPreferencesDialog::EditorFontButtonClick(TObject * /*Sender*/)
  1148. {
  1149. if (FontDialog(FEditorFont.get()))
  1150. {
  1151. UpdateControls();
  1152. }
  1153. }
  1154. //---------------------------------------------------------------------------
  1155. void __fastcall TPreferencesDialog::EditorFontColorChange(TColor Color)
  1156. {
  1157. FEditorFont->Color = Color;
  1158. UpdateControls();
  1159. }
  1160. //---------------------------------------------------------------------------
  1161. void __fastcall TPreferencesDialog::EditorFontColorButtonClick(TObject * /*Sender*/)
  1162. {
  1163. // WORKAROUND: Compiler keeps crashing randomly (but frequently) with
  1164. // "internal error" when passing menu directly to unique_ptr.
  1165. // Splitting it to two statements seems to help.
  1166. // The same hack exists in TSiteAdvancedDialog::ColorButtonClick
  1167. TPopupMenu * Menu = CreateColorPopupMenu(FEditorFont->Color, EditorFontColorChange);
  1168. // Popup menu has to survive the popup as TBX calls click handler asynchronously (post).
  1169. FColorPopupMenu.reset(Menu);
  1170. MenuPopup(Menu, EditorFontColorButton);
  1171. }
  1172. //---------------------------------------------------------------------------
  1173. void __fastcall TPreferencesDialog::EditorBackgroundColorChange(TColor Color)
  1174. {
  1175. FEditorBackgroundColor = Color;
  1176. UpdateControls();
  1177. }
  1178. //---------------------------------------------------------------------------
  1179. void __fastcall TPreferencesDialog::EditorBackgroundColorButtonClick(TObject * /*Sender*/)
  1180. {
  1181. // See comment in EditorFontColorButtonClick.
  1182. // We are using session color (contrary to editor text color) for background
  1183. // for a consistency with color selection menu on editor toolbar.
  1184. TTBXPopupMenu * PopupMenu = new TTBXPopupMenu(Application);
  1185. FColorPopupMenu.reset(PopupMenu);
  1186. CreateEditorBackgroundColorMenu(PopupMenu->Items, FEditorBackgroundColor, EditorBackgroundColorChange);
  1187. MenuPopup(FColorPopupMenu.get(), EditorBackgroundColorButton);
  1188. }
  1189. //---------------------------------------------------------------------------
  1190. void __fastcall TPreferencesDialog::PanelFontButtonClick(TObject * /*Sender*/)
  1191. {
  1192. if (FontDialog(FPanelFont.get()))
  1193. {
  1194. PanelFontCheck->Checked = true;
  1195. UpdateControls();
  1196. }
  1197. }
  1198. //---------------------------------------------------------------------------
  1199. void __fastcall TPreferencesDialog::FormCloseQuery(TObject * /*Sender*/,
  1200. bool & /*CanClose*/)
  1201. {
  1202. if (ModalResult == DefaultResult(this))
  1203. {
  1204. ExitActiveControl(this);
  1205. }
  1206. }
  1207. //---------------------------------------------------------------------------
  1208. void __fastcall TPreferencesDialog::IconButtonClick(TObject *Sender)
  1209. {
  1210. UnicodeString IconName, Params;
  1211. int SpecialFolder;
  1212. if (Sender == DesktopIconButton)
  1213. {
  1214. IconName = AppName;
  1215. int Result =
  1216. MessageDialog(LoadStr(CREATE_DESKTOP_ICON2), qtConfirmation,
  1217. qaYes | qaNo | qaCancel, HELP_CREATE_ICON);
  1218. switch (Result)
  1219. {
  1220. case qaYes:
  1221. SpecialFolder = CSIDL_COMMON_DESKTOPDIRECTORY;
  1222. break;
  1223. case qaNo:
  1224. SpecialFolder = CSIDL_DESKTOPDIRECTORY;
  1225. break;
  1226. default:
  1227. Abort();
  1228. break;
  1229. }
  1230. }
  1231. else
  1232. {
  1233. if (MessageDialog(MainInstructions(LoadStr(CONFIRM_CREATE_ICON)),
  1234. qtConfirmation, qaYes | qaNo, HELP_CREATE_ICON) == qaYes)
  1235. {
  1236. if (Sender == SendToHookButton)
  1237. {
  1238. IconName = FMTLOAD(SENDTO_HOOK_NAME2, (AppName));
  1239. SpecialFolder = CSIDL_SENDTO;
  1240. Params = TProgramParams::FormatSwitch(UPLOAD_SWITCH);
  1241. }
  1242. else if (Sender == QuickLaunchIconButton)
  1243. {
  1244. IconName = L"Microsoft\\Internet Explorer\\Quick Launch\\" +
  1245. AppName;
  1246. SpecialFolder = CSIDL_APPDATA;
  1247. }
  1248. }
  1249. else
  1250. {
  1251. Abort();
  1252. }
  1253. }
  1254. TInstantOperationVisualizer Visualizer;
  1255. CreateDesktopShortCut(IconName,
  1256. Application->ExeName, Params, L"", SpecialFolder);
  1257. }
  1258. //---------------------------------------------------------------------------
  1259. void __fastcall TPreferencesDialog::CustomCommandsViewData(TObject * /*Sender*/,
  1260. TListItem * Item)
  1261. {
  1262. // WORKAROUND We get here on Wine after destructor is called
  1263. if ((FCustomCommandList != NULL) && (FExtensionList != NULL))
  1264. {
  1265. int Index = Item->Index;
  1266. const TCustomCommandType * Command = GetCommandList(Index)->Commands[GetCommandIndex(Index)];
  1267. UnicodeString Caption = StripHotkey(Command->Name);
  1268. Item->Caption = Caption;
  1269. DebugAssert(!Item->SubItems->Count);
  1270. Item->SubItems->Add(Command->Command);
  1271. Item->SubItems->Add(LoadStr(
  1272. FLAGSET(Command->Params, ccLocal) ? CUSTOM_COMMAND_LOCAL : CUSTOM_COMMAND_REMOTE));
  1273. }
  1274. }
  1275. //---------------------------------------------------------------------------
  1276. void __fastcall TPreferencesDialog::ListViewSelectItem(
  1277. TObject * /*Sender*/, TListItem * /*Item*/, bool /*Selected*/)
  1278. {
  1279. UpdateControls();
  1280. }
  1281. //---------------------------------------------------------------------------
  1282. void __fastcall TPreferencesDialog::UpdateCustomCommandsView()
  1283. {
  1284. CustomCommandsView->Items->Count = FCustomCommandList->Count + FExtensionList->Count;
  1285. AutoSizeListColumnsWidth(CustomCommandsView, 1);
  1286. CustomCommandsView->Invalidate();
  1287. // particularly after command is edited/configured, make sure the hint is updated,
  1288. // even if we manage to display a hint for the same command as before the change
  1289. FCustomCommandsHintItem = NULL;
  1290. }
  1291. //---------------------------------------------------------------------------
  1292. void __fastcall TPreferencesDialog::CustomCommandsViewKeyDown(
  1293. TObject * /*Sender*/, WORD & Key, TShiftState /*Shift*/)
  1294. {
  1295. if (RemoveCommandButton->Enabled && (Key == VK_DELETE))
  1296. {
  1297. RemoveCommandButtonClick(NULL);
  1298. }
  1299. if (AddCommandButton->Enabled && (Key == VK_INSERT))
  1300. {
  1301. AddEditCommand(false);
  1302. }
  1303. }
  1304. //---------------------------------------------------------------------------
  1305. void __fastcall TPreferencesDialog::CustomCommandsViewDblClick(
  1306. TObject * /*Sender*/)
  1307. {
  1308. if (EditCommandButton->Enabled)
  1309. {
  1310. AddEditCommand(true);
  1311. }
  1312. else if (ConfigureCommandButton->Enabled)
  1313. {
  1314. ConfigureCommand();
  1315. }
  1316. }
  1317. //---------------------------------------------------------------------------
  1318. static int __fastcall AddCommandToList(TCustomCommandList * List, int Index, TCustomCommandType * Command)
  1319. {
  1320. if (Index >= 0)
  1321. {
  1322. List->Insert(Index, Command);
  1323. }
  1324. else
  1325. {
  1326. List->Add(Command);
  1327. Index = List->Count - 1;
  1328. }
  1329. return Index;
  1330. }
  1331. //---------------------------------------------------------------------------
  1332. void __fastcall TPreferencesDialog::AddEditCommand(bool Edit)
  1333. {
  1334. TCustomCommandType Command;
  1335. if (Edit)
  1336. {
  1337. int Index = CustomCommandsView->ItemIndex;
  1338. DebugAssert(GetCommandList(Index) == FCustomCommandList);
  1339. Command = *FCustomCommandList->Commands[GetCommandIndex(Index)];
  1340. }
  1341. TShortCuts ShortCuts;
  1342. if (WinConfiguration->SharedBookmarks != NULL)
  1343. {
  1344. WinConfiguration->SharedBookmarks->ShortCuts(ShortCuts);
  1345. }
  1346. FCustomCommandList->ShortCuts(ShortCuts);
  1347. FExtensionList->ShortCuts(ShortCuts);
  1348. if (DoCustomCommandDialog(Command, FCustomCommandList,
  1349. (Edit ? ccmEdit : ccmAdd), 0, NULL, &ShortCuts))
  1350. {
  1351. int Index = GetListCommandIndex(FCustomCommandList);
  1352. TCustomCommandType * ACommand = new TCustomCommandType(Command);
  1353. if (Edit)
  1354. {
  1355. DebugAssert(Index < FCustomCommandList->Count);
  1356. FCustomCommandList->Change(Index, ACommand);
  1357. }
  1358. else
  1359. {
  1360. Index = AddCommandToList(FCustomCommandList, Index, ACommand);
  1361. }
  1362. UpdateCustomCommandsView();
  1363. CustomCommandsView->ItemIndex = GetCommandListIndex(FCustomCommandList, Index);
  1364. CustomCommandsView->ItemFocused->MakeVisible(false);
  1365. UpdateControls();
  1366. }
  1367. }
  1368. //---------------------------------------------------------------------------
  1369. void __fastcall TPreferencesDialog::RemoveCommandButtonClick(
  1370. TObject * /*Sender*/)
  1371. {
  1372. TCustomCommandList * List = GetCommandList(CustomCommandsView->ItemIndex);
  1373. int Index = GetCommandIndex(CustomCommandsView->ItemIndex);
  1374. if (List == FExtensionList)
  1375. {
  1376. const TCustomCommandType * CustomComand = List->Commands[Index];
  1377. // If the extension was added in this "preferences session", remove the file
  1378. int PathIndex = FAddedExtensions->IndexOf(CustomComand->FileName);
  1379. if (PathIndex >= 0)
  1380. {
  1381. FAddedExtensions->Delete(PathIndex);
  1382. DeleteFile(ApiPath(CustomComand->FileName));
  1383. }
  1384. }
  1385. List->Delete(Index);
  1386. UpdateCustomCommandsView();
  1387. UpdateControls();
  1388. }
  1389. //---------------------------------------------------------------------------
  1390. void __fastcall TPreferencesDialog::CustomCommandMove(int Source, int Dest)
  1391. {
  1392. TCustomCommandList * List = GetCommandList(CustomCommandsView->ItemIndex);
  1393. int SourceIndex = GetCommandIndex(Source);
  1394. int DestIndex = GetCommandIndex(Dest);
  1395. List->Move(SourceIndex, DestIndex);
  1396. // workaround for bug in VCL
  1397. CustomCommandsView->ItemIndex = -1;
  1398. CustomCommandsView->ItemFocused = CustomCommandsView->Selected;
  1399. CustomCommandsView->ItemIndex = Dest;
  1400. UpdateCustomCommandsView();
  1401. UpdateControls();
  1402. }
  1403. //---------------------------------------------------------------------------
  1404. void __fastcall TPreferencesDialog::UpDownCommandButtonClick(TObject * Sender)
  1405. {
  1406. CustomCommandMove(CustomCommandsView->ItemIndex,
  1407. CustomCommandsView->ItemIndex + (Sender == UpCommandButton ? -1 : 1));
  1408. }
  1409. //---------------------------------------------------------------------------
  1410. TListViewScrollOnDragOver * __fastcall TPreferencesDialog::ScrollOnDragOver(TObject * ListView)
  1411. {
  1412. if (ListView == CopyParamListView)
  1413. {
  1414. return FCopyParamScrollOnDragOver;
  1415. }
  1416. else if (ListView == CustomCommandsView)
  1417. {
  1418. return FCustomCommandsScrollOnDragOver;
  1419. }
  1420. else if (ListView == EditorListView3)
  1421. {
  1422. return FEditorScrollOnDragOver;
  1423. }
  1424. else
  1425. {
  1426. DebugFail();
  1427. return NULL;
  1428. }
  1429. }
  1430. //---------------------------------------------------------------------------
  1431. void __fastcall TPreferencesDialog::ListViewStartDrag(
  1432. TObject * Sender, TDragObject *& /*DragObject*/)
  1433. {
  1434. FListViewDragSource = dynamic_cast<TListView*>(Sender)->ItemIndex;
  1435. FListViewDragDest = -1;
  1436. ScrollOnDragOver(Sender)->StartDrag();
  1437. }
  1438. //---------------------------------------------------------------------------
  1439. static int __fastcall PointToListViewIndex(TObject * Sender, int X, int Y)
  1440. {
  1441. TListItem * Item = dynamic_cast<TListView*>(Sender)->GetItemAt(X, Y);
  1442. return Item ? Item->Index : -1;
  1443. }
  1444. //---------------------------------------------------------------------------
  1445. bool __fastcall TPreferencesDialog::AllowListViewDrag(TObject * Sender, int X, int Y)
  1446. {
  1447. FListViewDragDest = PointToListViewIndex(Sender, X, Y);
  1448. return (FListViewDragDest >= 0) && (FListViewDragDest != FListViewDragSource);
  1449. }
  1450. //---------------------------------------------------------------------------
  1451. void __fastcall TPreferencesDialog::CustomCommandsViewDragDrop(
  1452. TObject * Sender, TObject * Source, int X, int Y)
  1453. {
  1454. if (Source == CustomCommandsView)
  1455. {
  1456. if (AllowListViewDrag(Sender, X, Y) &&
  1457. (GetCommandList(FListViewDragSource) == GetCommandList(FListViewDragDest)))
  1458. {
  1459. CustomCommandMove(FListViewDragSource, FListViewDragDest);
  1460. }
  1461. }
  1462. }
  1463. //---------------------------------------------------------------------------
  1464. void __fastcall TPreferencesDialog::CustomCommandsViewDragOver(
  1465. TObject *Sender, TObject *Source, int X, int Y, TDragState State, bool & Accept)
  1466. {
  1467. ListViewDragOver(Sender, Source, X, Y, State, Accept);
  1468. if (Source == Sender)
  1469. {
  1470. int Dest = PointToListViewIndex(Sender, X, Y);
  1471. if (GetCommandList(FListViewDragSource) != GetCommandList(Dest))
  1472. {
  1473. Accept = false;
  1474. }
  1475. }
  1476. }
  1477. //---------------------------------------------------------------------------
  1478. void __fastcall TPreferencesDialog::ListViewDragOver(
  1479. TObject * Sender, TObject * Source, int X, int Y,
  1480. TDragState /*State*/, bool & Accept)
  1481. {
  1482. if (Source == Sender)
  1483. {
  1484. // cannot use AllowListViewDrag(X, Y) because of bug in VCL
  1485. // (when dropped on item itself, when it was dragged over another item before,
  1486. // that another item remains highlighted forever)
  1487. Accept = true;
  1488. ScrollOnDragOver(Source)->DragOver(TPoint(X, Y));
  1489. }
  1490. }
  1491. //---------------------------------------------------------------------------
  1492. const TCopyParamType * TPreferencesDialog::GetCopyParam(int Index)
  1493. {
  1494. if (Index == 0)
  1495. {
  1496. return &FCopyParams;
  1497. }
  1498. else
  1499. {
  1500. return FCopyParamList->CopyParams[Index - 1];
  1501. }
  1502. }
  1503. //---------------------------------------------------------------------------
  1504. void __fastcall TPreferencesDialog::CopyParamMove(int Source, int Dest)
  1505. {
  1506. if (Source >= 1 && Source < (1 + FCopyParamList->Count) &&
  1507. Dest >= 0 && Dest < (1 + FCopyParamList->Count))
  1508. {
  1509. if (Dest == 0)
  1510. {
  1511. Dest = 1;
  1512. }
  1513. FCopyParamList->Move(Source - 1, Dest - 1);
  1514. // workaround for bug in VCL
  1515. CopyParamListView->ItemIndex = -1;
  1516. CopyParamListView->ItemFocused = CopyParamListView->Selected;
  1517. CopyParamListView->ItemIndex = Dest;
  1518. UpdateCopyParamListView();
  1519. UpdateControls();
  1520. }
  1521. }
  1522. //---------------------------------------------------------------------------
  1523. void __fastcall TPreferencesDialog::CopyParamListViewDragDrop(
  1524. TObject * Sender, TObject * Source, int X, int Y)
  1525. {
  1526. if (Source == CopyParamListView)
  1527. {
  1528. if (AllowListViewDrag(Sender, X, Y))
  1529. {
  1530. CopyParamMove(FListViewDragSource, FListViewDragDest);
  1531. }
  1532. }
  1533. }
  1534. //---------------------------------------------------------------------------
  1535. void __fastcall TPreferencesDialog::UpDownCopyParamButtonClick(TObject * Sender)
  1536. {
  1537. CopyParamMove(CopyParamListView->ItemIndex,
  1538. CopyParamListView->ItemIndex + (Sender == UpCopyParamButton ? -1 : 1));
  1539. }
  1540. //---------------------------------------------------------------------------
  1541. void __fastcall TPreferencesDialog::RemoveCopyParamButtonClick(
  1542. TObject * /*Sender*/)
  1543. {
  1544. DebugAssert(CopyParamListView->ItemIndex >= 1 &&
  1545. CopyParamListView->ItemIndex < (1 + FCopyParamList->Count));
  1546. FCopyParamList->Delete(CopyParamListView->ItemIndex - 1);
  1547. UpdateCopyParamListView();
  1548. UpdateControls();
  1549. }
  1550. //---------------------------------------------------------------------------
  1551. void __fastcall TPreferencesDialog::AddEditCopyParam(TCopyParamPresetMode Mode)
  1552. {
  1553. int Index = CopyParamListView->ItemIndex;
  1554. bool Result;
  1555. if ((Index == 0) && (Mode == cpmEdit))
  1556. {
  1557. Result = DoCopyParamCustomDialog(FCopyParams, 0);
  1558. }
  1559. else
  1560. {
  1561. TCopyParamRuleData * CopyParamRuleData =
  1562. (FDialogData != NULL ? FDialogData->CopyParamRuleData : NULL);
  1563. // negative (when default is selected) means add to the end
  1564. Index--;
  1565. Result = DoCopyParamPresetDialog(FCopyParamList, Index, Mode, CopyParamRuleData, FCopyParams);
  1566. if (Result)
  1567. {
  1568. UpdateCopyParamListView();
  1569. CopyParamListView->ItemIndex = Index + 1;
  1570. // when using duplicate button, focus remains on original item
  1571. CopyParamListView->ItemFocused = CopyParamListView->Selected;
  1572. }
  1573. }
  1574. if (Result)
  1575. {
  1576. UpdateControls();
  1577. }
  1578. }
  1579. //---------------------------------------------------------------------------
  1580. void __fastcall TPreferencesDialog::AddCopyParamButtonClick(TObject * /*Sender*/)
  1581. {
  1582. AddEditCopyParam(cpmAdd);
  1583. }
  1584. //---------------------------------------------------------------------------
  1585. void __fastcall TPreferencesDialog::EditCopyParamButtonClick(TObject * /*Sender*/)
  1586. {
  1587. AddEditCopyParam(cpmEdit);
  1588. }
  1589. //---------------------------------------------------------------------------
  1590. void __fastcall TPreferencesDialog::DuplicateCopyParamButtonClick(TObject * /*Sender*/)
  1591. {
  1592. AddEditCopyParam(cpmDuplicate);
  1593. }
  1594. //---------------------------------------------------------------------------
  1595. void __fastcall TPreferencesDialog::CopyParamListViewDblClick(
  1596. TObject * /*Sender*/)
  1597. {
  1598. if (EditCopyParamButton->Enabled)
  1599. {
  1600. AddEditCopyParam(cpmEdit);
  1601. }
  1602. }
  1603. //---------------------------------------------------------------------------
  1604. void __fastcall TPreferencesDialog::CopyParamListViewKeyDown(
  1605. TObject * /*Sender*/, WORD & Key, TShiftState /*Shift*/)
  1606. {
  1607. if (RemoveCopyParamButton->Enabled && (Key == VK_DELETE))
  1608. {
  1609. RemoveCopyParamButtonClick(NULL);
  1610. }
  1611. if (AddCopyParamButton->Enabled && (Key == VK_INSERT))
  1612. {
  1613. AddEditCopyParam(cpmAdd);
  1614. }
  1615. }
  1616. //---------------------------------------------------------------------------
  1617. void __fastcall TPreferencesDialog::EditorMove(int Source, int Dest)
  1618. {
  1619. if (Source >= 0 && Source < FEditorList->Count &&
  1620. Dest >= 0 && Dest < FEditorList->Count)
  1621. {
  1622. FEditorList->Move(Source, Dest);
  1623. // workaround for bug in VCL
  1624. EditorListView3->ItemIndex = -1;
  1625. EditorListView3->ItemFocused = EditorListView3->Selected;
  1626. EditorListView3->ItemIndex = Dest;
  1627. UpdateEditorListView();
  1628. UpdateControls();
  1629. }
  1630. }
  1631. //---------------------------------------------------------------------------
  1632. void __fastcall TPreferencesDialog::EditorListView3DragDrop(TObject * Sender,
  1633. TObject * Source, int X, int Y)
  1634. {
  1635. if (Source == EditorListView3)
  1636. {
  1637. if (AllowListViewDrag(Sender, X, Y))
  1638. {
  1639. EditorMove(FListViewDragSource, FListViewDragDest);
  1640. }
  1641. }
  1642. }
  1643. //---------------------------------------------------------------------------
  1644. void __fastcall TPreferencesDialog::UpDownEditorButtonClick(TObject *Sender)
  1645. {
  1646. EditorMove(EditorListView3->ItemIndex,
  1647. EditorListView3->ItemIndex + (Sender == UpEditorButton ? -1 : 1));
  1648. }
  1649. //---------------------------------------------------------------------------
  1650. void __fastcall TPreferencesDialog::RemoveEditorButtonClick(
  1651. TObject * /*Sender*/)
  1652. {
  1653. DebugAssert(EditorListView3->ItemIndex >= 0 &&
  1654. EditorListView3->ItemIndex < FEditorList->Count);
  1655. FEditorList->Delete(EditorListView3->ItemIndex);
  1656. UpdateEditorListView();
  1657. UpdateControls();
  1658. }
  1659. //---------------------------------------------------------------------------
  1660. void __fastcall TPreferencesDialog::AddEditEditorButtonClick(TObject * Sender)
  1661. {
  1662. TEditorPreferencesMode Mode = (Sender == EditEditorButton ? epmEdit : epmAdd);
  1663. int Index = EditorListView3->ItemIndex;
  1664. TEditorPreferences * Editor;
  1665. if (Mode == epmEdit)
  1666. {
  1667. Editor = new TEditorPreferences(*FEditorList->Editors[Index]);
  1668. }
  1669. else
  1670. {
  1671. Editor = new TEditorPreferences();
  1672. }
  1673. try
  1674. {
  1675. bool DummyRemember = false;
  1676. if (DoEditorPreferencesDialog(Editor->GetData(), DummyRemember, Mode, true))
  1677. {
  1678. if (Mode == epmEdit)
  1679. {
  1680. FEditorList->Change(Index, Editor);
  1681. }
  1682. else
  1683. {
  1684. if (Index < 0)
  1685. {
  1686. Index = FEditorList->Count;
  1687. FEditorList->Add(Editor);
  1688. }
  1689. else
  1690. {
  1691. FEditorList->Insert(Index, Editor);
  1692. }
  1693. }
  1694. // ownership of the object lost
  1695. Editor = NULL;
  1696. UpdateEditorListView();
  1697. EditorListView3->ItemIndex = Index;
  1698. UpdateControls();
  1699. }
  1700. }
  1701. __finally
  1702. {
  1703. delete Editor;
  1704. }
  1705. }
  1706. //---------------------------------------------------------------------------
  1707. void __fastcall TPreferencesDialog::EditorListView3DblClick(TObject * /*Sender*/)
  1708. {
  1709. if (EditEditorButton->Enabled)
  1710. {
  1711. AddEditEditorButtonClick(EditEditorButton);
  1712. }
  1713. }
  1714. //---------------------------------------------------------------------------
  1715. void __fastcall TPreferencesDialog::EditorListView3KeyDown(TObject * /*Sender*/,
  1716. WORD & Key, TShiftState /*Shift*/)
  1717. {
  1718. if (RemoveEditorButton->Enabled && (Key == VK_DELETE))
  1719. {
  1720. RemoveEditorButtonClick(NULL);
  1721. }
  1722. if (AddEditorButton->Enabled && (Key == VK_INSERT))
  1723. {
  1724. AddEditEditorButtonClick(AddEditorButton);
  1725. }
  1726. }
  1727. //---------------------------------------------------------------------------
  1728. void __fastcall TPreferencesDialog::UpdateEditorListView()
  1729. {
  1730. EditorListView3->Items->Count = FEditorList->Count;
  1731. AutoSizeListColumnsWidth(EditorListView3);
  1732. EditorListView3->Invalidate();
  1733. }
  1734. //---------------------------------------------------------------------------
  1735. void __fastcall TPreferencesDialog::EditorListView3Data(TObject * /*Sender*/,
  1736. TListItem * Item)
  1737. {
  1738. // WORKAROUND We get here on Wine after destructor is called
  1739. if (FEditorList != NULL)
  1740. {
  1741. int Index = Item->Index;
  1742. DebugAssert(Index >= 0 && Index <= FEditorList->Count);
  1743. const TEditorPreferences * Editor = FEditorList->Editors[Index];
  1744. Item->Caption = Editor->Name;
  1745. Item->SubItems->Add(Editor->Data->FileMask.Masks);
  1746. if (Editor->Data->Editor == edExternal)
  1747. {
  1748. Item->SubItems->Add(BooleanToStr(Editor->Data->ExternalEditorText));
  1749. }
  1750. }
  1751. }
  1752. //---------------------------------------------------------------------------
  1753. void __fastcall TPreferencesDialog::NavigationTreeChange(TObject * /*Sender*/,
  1754. TTreeNode * Node)
  1755. {
  1756. if (DebugAlwaysTrue(Node->SelectedIndex > 0))
  1757. {
  1758. PageControl->ActivePage = DebugNotNull(FindPageForTreeNode(Node));
  1759. // reshow the accelerators, etc
  1760. ResetSystemSettings(this);
  1761. // This is particularly here to enable EditCopyParamButton,
  1762. // as for some reason CopyParamListView->Selected is NULL until
  1763. // its page is shown for the first time
  1764. UpdateControls();
  1765. }
  1766. }
  1767. //---------------------------------------------------------------------------
  1768. void __fastcall TPreferencesDialog::PageControlChange(TObject * /*Sender*/)
  1769. {
  1770. // this is probably only ever called from FormShow (explicitly)
  1771. bool Found = false;
  1772. if (DebugAlwaysTrue(PageControl->ActivePage->Tag > 0))
  1773. {
  1774. for (int Index = 0; Index < NavigationTree->Items->Count; Index++)
  1775. {
  1776. if (NavigationTree->Items->Item[Index]->SelectedIndex ==
  1777. PageControl->ActivePage->Tag)
  1778. {
  1779. NavigationTree->Items->Item[Index]->Selected = true;
  1780. Found = true;
  1781. }
  1782. }
  1783. }
  1784. if (DebugAlwaysTrue(Found))
  1785. {
  1786. UpdateControls();
  1787. }
  1788. }
  1789. //---------------------------------------------------------------------------
  1790. void __fastcall TPreferencesDialog::CMDialogKey(TWMKeyDown & Message)
  1791. {
  1792. if (Message.CharCode == VK_TAB)
  1793. {
  1794. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  1795. if (Shift.Contains(ssCtrl))
  1796. {
  1797. TTreeNode * Node = NavigationTree->Selected;
  1798. if (!Shift.Contains(ssShift))
  1799. {
  1800. Node = Node->GetNext();
  1801. if (!Node) Node = NavigationTree->Items->GetFirstNode();
  1802. }
  1803. else
  1804. {
  1805. if (Node->GetPrev()) Node = Node->GetPrev();
  1806. else
  1807. while (Node->GetNext()) Node = Node->GetNext();
  1808. }
  1809. Node->Selected = True;
  1810. Message.Result = 1;
  1811. return;
  1812. }
  1813. }
  1814. TForm::Dispatch(&Message);
  1815. }
  1816. //---------------------------------------------------------------------------
  1817. void __fastcall TPreferencesDialog::WMHelp(TWMHelp & Message)
  1818. {
  1819. DebugAssert(Message.HelpInfo != NULL);
  1820. if (Message.HelpInfo->iContextType == HELPINFO_WINDOW)
  1821. {
  1822. // invoke help for active page (not for whole form), regardless of focus
  1823. // (e.g. even if focus is on control outside pagecontrol)
  1824. Message.HelpInfo->hItemHandle = PageControl->ActivePage->Handle;
  1825. }
  1826. TForm::Dispatch(&Message);
  1827. }
  1828. //---------------------------------------------------------------------------
  1829. void __fastcall TPreferencesDialog::Dispatch(void *Message)
  1830. {
  1831. TMessage * M = reinterpret_cast<TMessage*>(Message);
  1832. DebugAssert(M);
  1833. if (M->Msg == CM_DIALOGKEY)
  1834. {
  1835. CMDialogKey(*((TWMKeyDown *)Message));
  1836. }
  1837. else if (M->Msg == WM_HELP)
  1838. {
  1839. WMHelp(*((TWMHelp *)Message));
  1840. }
  1841. else
  1842. {
  1843. TForm::Dispatch(Message);
  1844. }
  1845. }
  1846. //---------------------------------------------------------------------------
  1847. void __fastcall TPreferencesDialog::RegisterAsUrlHandlersButtonClick(
  1848. TObject * /*Sender*/)
  1849. {
  1850. MenuPopup(RegisterAsUrlHandlerMenu, RegisterAsUrlHandlersButton);
  1851. }
  1852. //---------------------------------------------------------------------------
  1853. void __fastcall TPreferencesDialog::RegisterAsUrlHandlerItemClick(TObject * /*Sender*/)
  1854. {
  1855. unsigned int Result =
  1856. MessageDialog(MainInstructions(LoadStr(CONFIRM_REGISTER_URL2)),
  1857. qtConfirmation, qaYes | qaNo, HELP_REGISTER_URL);
  1858. if (Result == qaYes)
  1859. {
  1860. TInstantOperationVisualizer Visualizer;
  1861. RegisterForDefaultProtocols();
  1862. }
  1863. }
  1864. //---------------------------------------------------------------------------
  1865. void __fastcall TPreferencesDialog::UnregisterForDefaultProtocolsItemClick(TObject * /*Sender*/)
  1866. {
  1867. unsigned int Result =
  1868. MessageDialog(MainInstructions(LoadStr(CONFIRM_UNREGISTER_URL)),
  1869. qtConfirmation, qaYes | qaNo, HELP_REGISTER_URL);
  1870. if (Result == qaYes)
  1871. {
  1872. TInstantOperationVisualizer Visualizer;
  1873. UnregisterForProtocols();
  1874. }
  1875. }
  1876. //---------------------------------------------------------------------------
  1877. void __fastcall TPreferencesDialog::MakeDefaultHandlerItemClick(TObject * /*Sender*/)
  1878. {
  1879. TOperationVisualizer Visualizer;
  1880. LaunchAdvancedAssociationUI();
  1881. }
  1882. //---------------------------------------------------------------------------
  1883. void __fastcall TPreferencesDialog::DDExtLabelClick(TObject * Sender)
  1884. {
  1885. ((Sender == DDExtEnabledLabel) ? DDExtEnabledButton : DDExtDisabledButton)->
  1886. SetFocus();
  1887. }
  1888. //---------------------------------------------------------------------------
  1889. void __fastcall TPreferencesDialog::AddSearchPathButtonClick(
  1890. TObject * /*Sender*/)
  1891. {
  1892. UnicodeString AppPath = ExtractFilePath(Application->ExeName);
  1893. if (MessageDialog(MainInstructions(FMTLOAD(CONFIRM_ADD_SEARCH_PATH, (AppPath))),
  1894. qtConfirmation, qaYes | qaNo, HELP_ADD_SEARCH_PATH) == qaYes)
  1895. {
  1896. TInstantOperationVisualizer Visualizer;
  1897. AddSearchPath(AppPath);
  1898. }
  1899. }
  1900. //---------------------------------------------------------------------------
  1901. void __fastcall TPreferencesDialog::EditorFontLabelDblClick(
  1902. TObject * Sender)
  1903. {
  1904. EditorFontButtonClick(Sender);
  1905. }
  1906. //---------------------------------------------------------------------------
  1907. void __fastcall TPreferencesDialog::UpdateCopyParamListView()
  1908. {
  1909. CopyParamListView->Items->Count = 1 + FCopyParamList->Count;
  1910. AutoSizeListColumnsWidth(CopyParamListView);
  1911. CopyParamListView->Invalidate();
  1912. }
  1913. //---------------------------------------------------------------------------
  1914. void __fastcall TPreferencesDialog::CopyParamListViewData(TObject * /*Sender*/,
  1915. TListItem * Item)
  1916. {
  1917. // WORKAROUND We get here on Wine after destructor is called
  1918. if (FCopyParamList != NULL)
  1919. {
  1920. UnicodeString Name;
  1921. UnicodeString Rule;
  1922. int Index = Item->Index;
  1923. if (Index == 0)
  1924. {
  1925. Name = StripHotkey(LoadStr(COPY_PARAM_DEFAULT));
  1926. }
  1927. else
  1928. {
  1929. DebugAssert(Index >= 1 && Index <= 1 + FCopyParamList->Count);
  1930. Name = StripHotkey(FCopyParamList->Names[Index - 1]);
  1931. Rule = BooleanToStr(FCopyParamList->Rules[Index - 1] != NULL);
  1932. }
  1933. Item->Caption = Name;
  1934. Item->SubItems->Add(Rule);
  1935. }
  1936. }
  1937. //---------------------------------------------------------------------------
  1938. void __fastcall TPreferencesDialog::HelpButtonClick(TObject * /*Sender*/)
  1939. {
  1940. FormHelp(this);
  1941. }
  1942. //---------------------------------------------------------------------------
  1943. void __fastcall TPreferencesDialog::PuttyPathBrowseButtonClick(
  1944. TObject * /*Sender*/)
  1945. {
  1946. UnicodeString Executables = FORMAT("%s;%s", (OriginalPuttyExecutable, KittyExecutable));
  1947. BrowseForExecutable(PuttyPathEdit, LoadStr(PREFERENCES_SELECT_PUTTY2),
  1948. FMTLOAD(PREFERENCES_PUTTY_FILTER2, (Executables)), false, false);
  1949. }
  1950. //---------------------------------------------------------------------------
  1951. void __fastcall TPreferencesDialog::PathEditBeforeDialog(
  1952. TObject * /*Sender*/, UnicodeString & Name, bool & /*Action*/)
  1953. {
  1954. FBeforeDialogPath = Name;
  1955. Name = ExpandEnvironmentVariables(Name);
  1956. }
  1957. //---------------------------------------------------------------------------
  1958. void __fastcall TPreferencesDialog::PathEditAfterDialog(
  1959. TObject * /*Sender*/, UnicodeString & Name, bool & /*Action*/)
  1960. {
  1961. if (CompareFileName(Name, ExpandEnvironmentVariables(FBeforeDialogPath)))
  1962. {
  1963. Name = FBeforeDialogPath;
  1964. }
  1965. }
  1966. //---------------------------------------------------------------------------
  1967. void __fastcall TPreferencesDialog::NavigationTreeCollapsing(
  1968. TObject * /*Sender*/, TTreeNode * /*Node*/, bool & AllowCollapse)
  1969. {
  1970. AllowCollapse = false;
  1971. }
  1972. //---------------------------------------------------------------------------
  1973. void __fastcall TPreferencesDialog::ListViewEndDrag(
  1974. TObject * Sender, TObject * /*Target*/, int /*X*/, int /*Y*/)
  1975. {
  1976. ScrollOnDragOver(Sender)->EndDrag();
  1977. }
  1978. //---------------------------------------------------------------------------
  1979. void __fastcall TPreferencesDialog::SessionReopenTimeoutEditSetValue(
  1980. TObject * /*Sender*/, Extended Value, UnicodeString & Text, bool & Handled)
  1981. {
  1982. if (Value == 0)
  1983. {
  1984. Text = LoadStr(PREFERENCES_RECONNECT_TIMEOUT_UNLIMITED);
  1985. Handled = true;
  1986. }
  1987. }
  1988. //---------------------------------------------------------------------------
  1989. void __fastcall TPreferencesDialog::SessionReopenTimeoutEditGetValue(
  1990. TObject * /*Sender*/, UnicodeString Text, Extended & Value, bool & Handled)
  1991. {
  1992. if (AnsiSameText(Text, LoadStr(PREFERENCES_RECONNECT_TIMEOUT_UNLIMITED)))
  1993. {
  1994. Value = 0;
  1995. Handled = true;
  1996. }
  1997. }
  1998. //---------------------------------------------------------------------------
  1999. bool __fastcall TPreferencesDialog::CanSetMasterPassword()
  2000. {
  2001. bool Result;
  2002. bool Retry;
  2003. do
  2004. {
  2005. Retry = false;
  2006. Result = !AnyOtherInstanceOfSelf();
  2007. if (!Result)
  2008. {
  2009. unsigned int Answer =
  2010. MessageDialog(
  2011. LoadStr(MASTER_PASSWORD_OTHER_INSTANCE),
  2012. qtConfirmation, qaRetry | qaIgnore | qaCancel,
  2013. HELP_MASTER_PASSWORD);
  2014. switch (Answer)
  2015. {
  2016. case qaRetry:
  2017. Retry = true;
  2018. break;
  2019. case qaIgnore:
  2020. Result = true;
  2021. break;
  2022. case qaCancel:
  2023. default:
  2024. // noop
  2025. break;
  2026. }
  2027. }
  2028. }
  2029. while (Retry && !Result);
  2030. return Result;
  2031. }
  2032. //---------------------------------------------------------------------------
  2033. void __fastcall TPreferencesDialog::MasterPasswordChanged(
  2034. UnicodeString Message, TStrings * RecryptPasswordErrors)
  2035. {
  2036. // Save master password.
  2037. // This is unlikely to fail, as storage is written explicitly already
  2038. // when writing the recrypted passwords
  2039. Configuration->SaveExplicit();
  2040. TQueryType QueryType = qtInformation;
  2041. if (RecryptPasswordErrors->Count > 0)
  2042. {
  2043. Message = FMTLOAD(MASTER_PASSWORD_RECRYPT_ERRORS, (Message));
  2044. QueryType = qtWarning;
  2045. }
  2046. MoreMessageDialog(
  2047. Message, RecryptPasswordErrors, QueryType, qaOK, HELP_MASTER_PASSWORD);
  2048. }
  2049. //---------------------------------------------------------------------------
  2050. void __fastcall TPreferencesDialog::ChangeMasterPassword(UnicodeString Message)
  2051. {
  2052. UnicodeString NewPassword;
  2053. if (DoChangeMasterPasswordDialog(NewPassword))
  2054. {
  2055. std::unique_ptr<TStrings> RecryptPasswordErrors(new TStringList());
  2056. WinConfiguration->ChangeMasterPassword(NewPassword, RecryptPasswordErrors.get());
  2057. MasterPasswordChanged(Message, RecryptPasswordErrors.get());
  2058. }
  2059. }
  2060. //---------------------------------------------------------------------------
  2061. void __fastcall TPreferencesDialog::UseMasterPasswordCheckClick(
  2062. TObject * /*Sender*/)
  2063. {
  2064. if (UseMasterPasswordCheck->Checked != WinConfiguration->UseMasterPassword)
  2065. {
  2066. try
  2067. {
  2068. if (CanSetMasterPassword())
  2069. {
  2070. if (UseMasterPasswordCheck->Checked)
  2071. {
  2072. ChangeMasterPassword(LoadStr(MASTER_PASSWORD_SET2));
  2073. }
  2074. else
  2075. {
  2076. if (DoMasterPasswordDialog())
  2077. {
  2078. std::unique_ptr<TStrings> RecryptPasswordErrors(new TStringList());
  2079. WinConfiguration->ClearMasterPassword(RecryptPasswordErrors.get());
  2080. MasterPasswordChanged(LoadStr(MASTER_PASSWORD_CLEARED2), RecryptPasswordErrors.get());
  2081. }
  2082. }
  2083. }
  2084. }
  2085. __finally
  2086. {
  2087. UseMasterPasswordCheck->Checked = WinConfiguration->UseMasterPassword;
  2088. UpdateControls();
  2089. }
  2090. }
  2091. }
  2092. //---------------------------------------------------------------------------
  2093. void __fastcall TPreferencesDialog::SetMasterPasswordButtonClick(
  2094. TObject * /*Sender*/)
  2095. {
  2096. if (CanSetMasterPassword())
  2097. {
  2098. ChangeMasterPassword(MainInstructions(LoadStr(MASTER_PASSWORD_CHANGED)));
  2099. }
  2100. }
  2101. //---------------------------------------------------------------------------
  2102. void __fastcall TPreferencesDialog::UsageViewButtonClick(TObject * /*Sender*/)
  2103. {
  2104. std::unique_ptr<TStrings> Data(TextToStringList(GetUsageData()));
  2105. UnicodeString Message =
  2106. Data->Text.IsEmpty() ? MainInstructions(LoadStr(USAGE_DATA_NONE)) : LoadStr(USAGE_DATA2);
  2107. MoreMessageDialog(Message, Data.get(), qtInformation, qaOK, HELP_USAGE);
  2108. }
  2109. //---------------------------------------------------------------------------
  2110. void __fastcall TPreferencesDialog::CopyParamLabelClick(TObject * /*Sender*/)
  2111. {
  2112. if (EditCopyParamButton->Enabled)
  2113. {
  2114. AddEditCopyParam(cpmEdit);
  2115. }
  2116. }
  2117. //---------------------------------------------------------------------------
  2118. void __fastcall TPreferencesDialog::CopyParamListViewCustomDrawItem(
  2119. TCustomListView * Sender, TListItem * Item,
  2120. TCustomDrawState /*State*/, bool & /*DefaultDraw*/)
  2121. {
  2122. if (Item->Index == 0)
  2123. {
  2124. Sender->Canvas->Font->Style = Sender->Canvas->Font->Style << fsBold;
  2125. }
  2126. }
  2127. //---------------------------------------------------------------------------
  2128. void __fastcall TPreferencesDialog::SelectPuttyRegistryStorageKey(const UnicodeString & Key)
  2129. {
  2130. PuttyRegistryStorageKeyEdit->ItemIndex =
  2131. PuttyRegistryStorageKeyEdit->Items->IndexOf(Key);
  2132. }
  2133. //---------------------------------------------------------------------------
  2134. void __fastcall TPreferencesDialog::PuttyPathEditChange(TObject * /*Sender*/)
  2135. {
  2136. UnicodeString PuttyPath = PuttyPathEdit->Text;
  2137. if (ContainsText(PuttyPath, OriginalPuttyExecutable))
  2138. {
  2139. SelectPuttyRegistryStorageKey(OriginalPuttyRegistryStorageKey);
  2140. }
  2141. else if (ContainsText(PuttyPath, KittyExecutable))
  2142. {
  2143. SelectPuttyRegistryStorageKey(KittyRegistryStorageKey);
  2144. }
  2145. UpdateControls();
  2146. }
  2147. //---------------------------------------------------------------------------
  2148. void __fastcall TPreferencesDialog::NavigationTreeChanging(TObject * /*Sender*/,
  2149. TTreeNode * Node, bool & /*AllowChange*/)
  2150. {
  2151. TTabSheet * Sheet = FindPageForTreeNode(Node);
  2152. // delay load as this can be time consuming
  2153. if (Sheet == LanguagesSheet)
  2154. {
  2155. LoadLanguages();
  2156. }
  2157. }
  2158. //---------------------------------------------------------------------------
  2159. void __fastcall TPreferencesDialog::LanguagesGetMoreButtonClick(TObject * /*Sender*/)
  2160. {
  2161. OpenBrowser(ProgramUrl(LoadStr(LOCALES_URL)));
  2162. }
  2163. //---------------------------------------------------------------------------
  2164. void __fastcall TPreferencesDialog::CommanderClick(TObject * /*Sender*/)
  2165. {
  2166. CommanderInterfaceButton2->SetFocus();
  2167. }
  2168. //---------------------------------------------------------------------------
  2169. void __fastcall TPreferencesDialog::ExplorerClick(TObject * /*Sender*/)
  2170. {
  2171. ExplorerInterfaceButton2->SetFocus();
  2172. }
  2173. //---------------------------------------------------------------------------
  2174. void __fastcall TPreferencesDialog::PanelFontLabelDblClick(TObject * Sender)
  2175. {
  2176. PanelFontButtonClick(Sender);
  2177. }
  2178. //---------------------------------------------------------------------------
  2179. void __fastcall TPreferencesDialog::UpdatesAuthenticationEmailEditExit(TObject * /*Sender*/)
  2180. {
  2181. if (FVerifiedUpdatesAuthenticationEmail != UpdatesAuthenticationEmailEdit->Text)
  2182. {
  2183. FVerifiedUpdatesAuthenticationEmail = UpdatesAuthenticationEmailEdit->Text;
  2184. if (!FVerifiedUpdatesAuthenticationEmail.IsEmpty())
  2185. {
  2186. TUpdatesConfiguration Updates = SaveUpdates();
  2187. {
  2188. TInstantOperationVisualizer Visualizer;
  2189. QueryUpdates(Updates);
  2190. }
  2191. UnicodeString AuthenticationError = Updates.Results.AuthenticationError;
  2192. if (!AuthenticationError.IsEmpty())
  2193. {
  2194. AuthenticationError = FormatUpdatesMessage(AuthenticationError);
  2195. if (HasParagraphs(AuthenticationError))
  2196. {
  2197. AuthenticationError = MainInstructionsFirstParagraph(AuthenticationError);
  2198. }
  2199. else
  2200. {
  2201. AuthenticationError = MainInstructions(AuthenticationError);
  2202. }
  2203. UnicodeString HelpUrl = GetEnableAutomaticUpdatesUrl();
  2204. unsigned int Result =
  2205. MoreMessageDialog(AuthenticationError, NULL, qtError, qaIgnore | qaAbort, HelpUrl);
  2206. if (Result == qaAbort)
  2207. {
  2208. Abort();
  2209. }
  2210. }
  2211. }
  2212. }
  2213. }
  2214. //---------------------------------------------------------------------------
  2215. void __fastcall TPreferencesDialog::UpdatesLinkClick(TObject * /*Sender*/)
  2216. {
  2217. EnableAutomaticUpdates();
  2218. }
  2219. //---------------------------------------------------------------------------
  2220. void __fastcall TPreferencesDialog::CustomCommandsViewWindowProc(TMessage & Message)
  2221. {
  2222. FOrigCustomCommandsViewWindowProc(Message);
  2223. if (Message.Msg == CN_NOTIFY)
  2224. {
  2225. TWMNotify & NotifyMessage = reinterpret_cast<TWMNotify &>(Message);
  2226. if (NotifyMessage.NMHdr->code == NM_CUSTOMDRAW)
  2227. {
  2228. // request CDDS_ITEMPOSTPAINT notification
  2229. Message.Result |= CDRF_NOTIFYPOSTPAINT | CDRF_NOTIFYSUBITEMDRAW;
  2230. TNMLVCustomDraw * CustomDraw = reinterpret_cast<TNMLVCustomDraw *>(NotifyMessage.NMHdr);
  2231. int Index = CustomDraw->nmcd.dwItemSpec;
  2232. int CommandIndex = GetCommandIndex(Index);
  2233. TCustomCommandList * List = GetCommandList(Index);
  2234. // after end of every list, except for the last last list
  2235. if ((CommandIndex == List->Count - 1) && (Index < CustomCommandsView->Items->Count - 1) &&
  2236. FLAGSET(CustomDraw->nmcd.dwDrawStage, CDDS_ITEMPOSTPAINT))
  2237. {
  2238. TRect Rect;
  2239. Rect.Top = CustomDraw->iSubItem;
  2240. Rect.Left = LVIR_BOUNDS;
  2241. CustomCommandsView->Perform(LVM_GETSUBITEMRECT, CustomDraw->nmcd.dwItemSpec, reinterpret_cast<LPARAM>(&Rect));
  2242. HDC DC = CustomDraw->nmcd.hdc;
  2243. SelectObject(DC, GetStockObject(DC_PEN));
  2244. SetDCPenColor(DC, ColorToRGB(clWindowFrame));
  2245. MoveToEx(DC, Rect.Left, Rect.Bottom - 1, NULL);
  2246. LineTo(DC, Rect.Right, Rect.Bottom - 1);
  2247. }
  2248. }
  2249. }
  2250. }
  2251. //---------------------------------------------------------------------------
  2252. void __fastcall TPreferencesDialog::AddExtension()
  2253. {
  2254. const UnicodeString HistoryKey(L"ExtensionPath");
  2255. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[HistoryKey]));
  2256. UnicodeString Path;
  2257. if (InputDialog(LoadStr(ADD_EXTENSION_CAPTION), LoadStr(ADD_EXTENSION_PROMPT), Path,
  2258. HELP_NONE, History.get(), true) &&
  2259. !Path.IsEmpty())
  2260. {
  2261. CustomWinConfiguration->History[HistoryKey] = History.get();
  2262. bool Trusted;
  2263. bool Latest;
  2264. UnicodeString FileName;
  2265. UnicodeString ExtensionPath;
  2266. std::unique_ptr<TStringList> Lines(new TStringList());
  2267. std::unique_ptr<TCustomCommandType> CustomCommand;
  2268. bool IsUrl = IsHttpOrHttpsUrl(Path);
  2269. try
  2270. {
  2271. if (IsUrl)
  2272. {
  2273. UnicodeString Url = Path;
  2274. Url = SecureUrl(Url);
  2275. bool WinSCPURL = IsWinSCPUrl(Url);
  2276. if (WinSCPURL)
  2277. {
  2278. Url = CampaignUrl(ProgramUrl(Url));
  2279. }
  2280. TOperationVisualizer Visualizer;
  2281. std::unique_ptr<THttp> Http(CreateHttp());
  2282. Http->URL = Url;
  2283. std::unique_ptr<TStrings> Headers(new TStringList());
  2284. Headers->Values[L"Accept"] = L"text/winscpextension,text/plain";
  2285. Http->RequestHeaders = Headers.get();
  2286. Http->Get();
  2287. UnicodeString TrustedStr = Http->ResponseHeaders->Values["WinSCP-Extension-Trusted"];
  2288. Trusted = WinSCPURL && (StrToIntDef(TrustedStr, 0) != 0);
  2289. FileName = MakeValidFileName(Http->ResponseHeaders->Values["WinSCP-Extension-Id"]);
  2290. if (FileName.IsEmpty())
  2291. {
  2292. FileName = MakeValidFileName(ExtractFileNameFromUrl(Path));
  2293. }
  2294. Lines->Text = Http->Response;
  2295. Latest = Http->ResponseHeaders->Values["WinSCP-Extension-Skipped"].Trim().IsEmpty();
  2296. }
  2297. else
  2298. {
  2299. if (!FileExists(ApiPath(Path)))
  2300. {
  2301. throw Exception(MainInstructions(FMTLOAD(FILE_NOT_EXISTS, (Path))));
  2302. }
  2303. Trusted = true;
  2304. Latest = true;
  2305. UnicodeString Id = WinConfiguration->GetExtensionId(Path);
  2306. FileName = ExtractFileName(Path);
  2307. if (!Id.IsEmpty())
  2308. {
  2309. ExtensionPath = Path;
  2310. }
  2311. LoadScriptFromFile(Path, Lines.get());
  2312. }
  2313. // validate syntax
  2314. CustomCommand.reset(new TCustomCommandType());
  2315. CustomCommand->LoadExtension(Lines.get());
  2316. }
  2317. catch (Exception & E)
  2318. {
  2319. throw ExtException(&E, MainInstructions(FMTLOAD(EXTENSION_LOAD_ERROR, (Path))));
  2320. }
  2321. if (!ExtensionPath.IsEmpty())
  2322. {
  2323. int Index = FExtensionList->FindIndexByFileName(Path);
  2324. if (Index > 0)
  2325. {
  2326. CustomCommandsView->ItemIndex = GetCommandListIndex(FExtensionList, Index);
  2327. CustomCommandsView->ItemFocused->MakeVisible(false);
  2328. CustomCommandsView->SetFocus();
  2329. throw Exception(MainInstructions(LoadStr(EXTENSION_INSTALLED_ALREADY)));
  2330. }
  2331. }
  2332. if (FExtensionList->Find(CustomCommand->Name) != NULL)
  2333. {
  2334. throw Exception(MainInstructions(FMTLOAD(EXTENSION_DUPLICATE, (StripHotkey(CustomCommand->Name)))));
  2335. }
  2336. if (ExtensionPath.IsEmpty())
  2337. {
  2338. if (TCustomCommandType::GetExtensionId(FileName).IsEmpty())
  2339. {
  2340. UnicodeString FileNameOnly = ExtractFileNameOnly(FileName);
  2341. if (FileNameOnly.IsEmpty())
  2342. {
  2343. FileName = MakeValidFileName(StripHotkey(CustomCommand->Name)) + WinSCPExtensionExt;
  2344. }
  2345. else
  2346. {
  2347. FileName = ExtractFileNameOnly(FileName) + WinSCPExtensionExt + ExtractFileExt(FileName);
  2348. }
  2349. }
  2350. }
  2351. if (Trusted ||
  2352. (MessageDialog(MainInstructions(LoadStr(EXTENSION_UNTRUSTED)), qtWarning, qaOK | qaCancel) == qaOK))
  2353. {
  2354. if (ExtensionPath.IsEmpty())
  2355. {
  2356. UnicodeString UserExtensionsPath = WinConfiguration->GetUserExtensionsPath();
  2357. if (!DirectoryExists(UserExtensionsPath) &&
  2358. !ForceDirectories(UserExtensionsPath))
  2359. {
  2360. throw EOSExtException(MainInstructions(FMTLOAD(CREATE_LOCAL_DIR_ERROR, (UserExtensionsPath))));
  2361. }
  2362. ExtensionPath = IncludeTrailingBackslash(UserExtensionsPath) + FileName;
  2363. int Counter = 1;
  2364. UnicodeString OriginalExtensionPath = ExtensionPath;
  2365. int P = Pos(UpperCase(WinSCPExtensionExt), UpperCase(OriginalExtensionPath));
  2366. while (FileExists(ApiPath(ExtensionPath)))
  2367. {
  2368. Counter++;
  2369. ExtensionPath = LeftStr(OriginalExtensionPath, P - 1) + IntToStr(Counter) + RightStr(OriginalExtensionPath, OriginalExtensionPath.Length() - P + 1);
  2370. }
  2371. Lines->SaveToFile(ApiPath(ExtensionPath));
  2372. FAddedExtensions->Add(ExtensionPath);
  2373. }
  2374. int Index = GetListCommandIndex(FExtensionList);
  2375. std::unique_ptr<TCustomCommandType> CustomCommand(new TCustomCommandType());
  2376. CustomCommand->Id = WinConfiguration->GetExtensionId(ExtensionPath);
  2377. CustomCommand->LoadExtension(ExtensionPath);
  2378. Index = AddCommandToList(FExtensionList, Index, CustomCommand.release());
  2379. UpdateCustomCommandsView();
  2380. CustomCommandsView->ItemIndex = GetCommandListIndex(FExtensionList, Index);
  2381. CustomCommandsView->ItemFocused->MakeVisible(false);
  2382. UpdateControls();
  2383. if (!Latest)
  2384. {
  2385. MessageDialog(LoadStr(EXTENSION_NOT_LATEST), qtInformation, qaOK);
  2386. }
  2387. if (IsUrl)
  2388. {
  2389. Configuration->Usage->Inc(L"ExtensionAddsFromUrl");
  2390. }
  2391. else
  2392. {
  2393. Configuration->Usage->Inc(L"ExtensionAddsFromFile");
  2394. }
  2395. }
  2396. }
  2397. }
  2398. //---------------------------------------------------------------------------
  2399. void __fastcall TPreferencesDialog::AddCommandButtonClick(TObject * /*Sender*/)
  2400. {
  2401. if (GetCommandList(CustomCommandsView->ItemIndex) == FCustomCommandList)
  2402. {
  2403. AddEditCommand(false);
  2404. }
  2405. else
  2406. {
  2407. AddExtension();
  2408. }
  2409. }
  2410. //---------------------------------------------------------------------------
  2411. void __fastcall TPreferencesDialog::AddCustomCommandMenuItemClick(TObject * /*Sender*/)
  2412. {
  2413. AddEditCommand(false);
  2414. }
  2415. //---------------------------------------------------------------------------
  2416. void __fastcall TPreferencesDialog::AddExtensionMenuItemClick(TObject * /*Sender*/)
  2417. {
  2418. AddExtension();
  2419. }
  2420. //---------------------------------------------------------------------------
  2421. void __fastcall TPreferencesDialog::EditCommandButtonClick(TObject * /*Sender*/)
  2422. {
  2423. AddEditCommand(true);
  2424. }
  2425. //---------------------------------------------------------------------------
  2426. void __fastcall TPreferencesDialog::AddCommandButtonDropDownClick(TObject * /*Sender*/)
  2427. {
  2428. AddCustomCommandMenuItem->Default = (GetCommandList(CustomCommandsView->ItemIndex) == FCustomCommandList);
  2429. AddExtensionMenuItem->Default = (GetCommandList(CustomCommandsView->ItemIndex) == FExtensionList);
  2430. MenuPopup(AddCommandMenu, AddCommandButton);
  2431. }
  2432. //---------------------------------------------------------------------------
  2433. void __fastcall TPreferencesDialog::CustomCommandsViewMouseMove(TObject * /*Sender*/, TShiftState /*Shift*/, int X, int Y)
  2434. {
  2435. TListItem * Item = CustomCommandsView->GetItemAt(X, Y);
  2436. int Index = (Item != NULL) ? Item->Index : -1;
  2437. if (Index != FCustomCommandsHintItem)
  2438. {
  2439. Application->CancelHint();
  2440. UnicodeString Hint;
  2441. if (Index >= 0)
  2442. {
  2443. TCustomCommandList * List = GetCommandList(Index);
  2444. const TCustomCommandType * Command = List->Commands[GetCommandIndex(Index)];
  2445. Hint = StripHotkey(Command->Name);
  2446. if (Command->ShortCut != 0)
  2447. {
  2448. Hint = FORMAT(L"%s (%s)", (Hint, ShortCutToText(Command->ShortCut)));
  2449. }
  2450. if (!Command->Description.IsEmpty())
  2451. {
  2452. Hint += L"\n" + Command->Description;
  2453. }
  2454. Hint += L"\n" + Command->GetCommandWithExpandedOptions(FCustomCommandOptions.get());
  2455. if (List == FExtensionList)
  2456. {
  2457. Hint += L"\n" + Command->FileName;
  2458. }
  2459. }
  2460. CustomCommandsView->Hint = Hint;
  2461. FCustomCommandsHintItem = Index;
  2462. }
  2463. }
  2464. //---------------------------------------------------------------------------
  2465. void __fastcall TPreferencesDialog::BackgroundConfirmationsLinkClick(TObject * /*Sender*/)
  2466. {
  2467. PageControl->ActivePage = QueueSheet;
  2468. PageControlChange(NULL);
  2469. QueueNoConfirmationCheck->SetFocus();
  2470. QueueNoConfirmationCheck->Perform(WM_CHANGEUISTATE, MAKEWPARAM(UIS_CLEAR, UISF_HIDEFOCUS), 0);
  2471. }
  2472. //---------------------------------------------------------------------------
  2473. class TCustomCommandOptionsDialog : public TCustomDialog
  2474. {
  2475. public:
  2476. __fastcall TCustomCommandOptionsDialog(const TCustomCommandType * Command, TStrings * CustomCommandOptions);
  2477. bool __fastcall Execute();
  2478. protected:
  2479. virtual void __fastcall DoHelp();
  2480. private:
  2481. const TCustomCommandType * FCommand;
  2482. TStrings * FCustomCommandOptions;
  2483. std::vector<TControl *> FControls;
  2484. std::vector<std::vector<UnicodeString> > FValues;
  2485. UnicodeString __fastcall HistoryKey(const TCustomCommandType::TOption & Option);
  2486. THistoryComboBox * __fastcall CreateHistoryComboBox(const TCustomCommandType::TOption & Option, const UnicodeString & Value);
  2487. void __fastcall BrowseButtonClick(TObject * Sender);
  2488. void __fastcall LinkLabelClick(TObject * Sender);
  2489. UnicodeString __fastcall SaveHistoryComboBoxValue(TControl * Control, const TCustomCommandType::TOption & Option);
  2490. void __fastcall AddOptionComboBox(
  2491. TComboBox * ComboBox, const UnicodeString & Value, const TCustomCommandType::TOption & Option,
  2492. std::vector<UnicodeString> & Values);
  2493. UnicodeString __fastcall GetComboBoxValue(TControl * Control, const UnicodeString & Default);
  2494. };
  2495. //---------------------------------------------------------------------------
  2496. __fastcall TCustomCommandOptionsDialog::TCustomCommandOptionsDialog(
  2497. const TCustomCommandType * Command, TStrings * CustomCommandOptions) :
  2498. TCustomDialog(HELP_EXTENSION_OPTIONS)
  2499. {
  2500. FCommand = Command;
  2501. FCustomCommandOptions = CustomCommandOptions;
  2502. Caption = FMTLOAD(EXTENSION_OPTIONS_CAPTION, (StripHotkey(FCommand->Name)));
  2503. Width = ScaleByTextHeight(this, 400);
  2504. for (int Index = 0; Index < FCommand->OptionsCount; Index++)
  2505. {
  2506. const TCustomCommandType::TOption & Option = FCommand->GetOption(Index);
  2507. UnicodeString OptionKey = FCommand->GetOptionKey(Option);
  2508. UnicodeString Value;
  2509. if (FCustomCommandOptions->IndexOfName(OptionKey) >= 0)
  2510. {
  2511. Value = FCustomCommandOptions->Values[OptionKey];
  2512. }
  2513. else
  2514. {
  2515. Value = Option.Default;
  2516. }
  2517. TControl * Control = NULL;
  2518. std::vector<UnicodeString> Values;
  2519. if (Option.Kind == TCustomCommandType::okUnknown)
  2520. {
  2521. Control = NULL;
  2522. }
  2523. else if (Option.Kind == TCustomCommandType::okLabel)
  2524. {
  2525. TLabel * Label = CreateLabel(Option.Caption);
  2526. AddText(Label);
  2527. Control = Label;
  2528. }
  2529. else if (Option.Kind == TCustomCommandType::okLink)
  2530. {
  2531. TStaticText * Label = new TStaticText(this);
  2532. Label->Caption = Option.Caption;
  2533. if (IsHttpOrHttpsUrl(Label->Caption))
  2534. {
  2535. Label->Caption = SecureUrl(Label->Caption);
  2536. LinkLabel(Label);
  2537. Label->TabStop = true;
  2538. }
  2539. else if (!Option.Default.IsEmpty() && IsHttpOrHttpsUrl(Option.Default))
  2540. {
  2541. Label->OnClick = LinkLabelClick;
  2542. LinkLabel(Label);
  2543. Label->TabStop = true;
  2544. }
  2545. else
  2546. {
  2547. // keep it plain text, as we have no URL
  2548. }
  2549. AddText(Label);
  2550. Control = Label;
  2551. }
  2552. else if (Option.Kind == TCustomCommandType::okTextBox)
  2553. {
  2554. Control = CreateHistoryComboBox(Option, Value);
  2555. }
  2556. else if (Option.Kind == TCustomCommandType::okFile)
  2557. {
  2558. THistoryComboBox * ComboBox = CreateHistoryComboBox(Option, Value);
  2559. TButton * Button = new TButton(this);
  2560. Button->Parent = this;
  2561. Button->Width = HelpButton->Width;
  2562. Button->Left = HelpButton->Left;
  2563. ComboBox->Width = HelpButton->Left - ComboBox->Left - ScaleByTextHeight(this, 6);
  2564. Button->Top = ComboBox->Top - ScaleByTextHeight(this, 2);
  2565. Button->Tag = Index;
  2566. Button->Caption = LoadStr(EXTENSION_OPTIONS_BROWSE);
  2567. Button->OnClick = BrowseButtonClick;
  2568. ScaleButtonControl(Button);
  2569. AddWinControl(Button);
  2570. Control = ComboBox;
  2571. }
  2572. else if (Option.Kind == TCustomCommandType::okDropDownList)
  2573. {
  2574. TComboBox * ComboBox = new TComboBox(this);
  2575. ComboBox->Style = csDropDownList;
  2576. AddOptionComboBox(ComboBox, Value, Option, Values);
  2577. Control = ComboBox;
  2578. }
  2579. else if (Option.Kind == TCustomCommandType::okComboBox)
  2580. {
  2581. TComboBox * ComboBox = new TComboBox(this);
  2582. ComboBox->Style = csDropDown;
  2583. AddOptionComboBox(ComboBox, Value, Option, Values);
  2584. if (ComboBox->ItemIndex < 0)
  2585. {
  2586. ComboBox->Text = Value;
  2587. }
  2588. Control = ComboBox;
  2589. }
  2590. else if (Option.Kind == TCustomCommandType::okCheckBox)
  2591. {
  2592. TCheckBox * CheckBox = CreateAndAddCheckBox(Option.Caption);
  2593. CheckBox->Checked =
  2594. (Option.Params.size() >= 1) &&
  2595. (Value == Option.Params[0]);
  2596. Control = CheckBox;
  2597. }
  2598. else
  2599. {
  2600. DebugFail();
  2601. }
  2602. if (Control != NULL)
  2603. {
  2604. Control->Tag = Index;
  2605. }
  2606. FControls.push_back(Control);
  2607. FValues.push_back(Values);
  2608. }
  2609. DebugAssert(FCommand->OptionsCount == static_cast<int>(FControls.size()));
  2610. }
  2611. //---------------------------------------------------------------------------
  2612. void __fastcall TCustomCommandOptionsDialog::AddOptionComboBox(
  2613. TComboBox * ComboBox, const UnicodeString & Value, const TCustomCommandType::TOption & Option, std::vector<UnicodeString> & Values)
  2614. {
  2615. std::unique_ptr<TStringList> Items(new TStringList());
  2616. int ItemIndex = -1;
  2617. TCustomCommandType::TOption::TParams::const_iterator ParamI = Option.Params.begin();
  2618. while (ParamI != Option.Params.end())
  2619. {
  2620. UnicodeString Item = (*ParamI);
  2621. int P = Item.Pos(L"=");
  2622. UnicodeString ParamValue;
  2623. if (P > 0)
  2624. {
  2625. ParamValue = Item.SubString(1, P - 1);
  2626. Item.Delete(1, P);
  2627. }
  2628. else
  2629. {
  2630. ParamValue = Item;
  2631. }
  2632. Items->Add(Item);
  2633. if (Value == ParamValue)
  2634. {
  2635. ItemIndex = Items->Count - 1;
  2636. }
  2637. Values.push_back(ParamValue);
  2638. ParamI++;
  2639. }
  2640. AddComboBox(ComboBox, CreateLabel(Option.Caption), Items.get(), true);
  2641. ComboBox->ItemIndex = ItemIndex;
  2642. }
  2643. //---------------------------------------------------------------------------
  2644. void __fastcall TCustomCommandOptionsDialog::LinkLabelClick(TObject * Sender)
  2645. {
  2646. TStaticText * Label = DebugNotNull(dynamic_cast<TStaticText *>(Sender));
  2647. const TCustomCommandType::TOption & Option = FCommand->GetOption(Label->Tag);
  2648. OpenBrowser(SecureUrl(Option.Default));
  2649. }
  2650. //---------------------------------------------------------------------------
  2651. void __fastcall TCustomCommandOptionsDialog::BrowseButtonClick(TObject * Sender)
  2652. {
  2653. TButton * Button = DebugNotNull(dynamic_cast<TButton *>(Sender));
  2654. int Index = Button->Tag;
  2655. THistoryComboBox * ComboBox = dynamic_cast<THistoryComboBox *>(FControls[Index]);
  2656. std::unique_ptr<TOpenDialog> OpenDialog(new TOpenDialog(Application));
  2657. UnicodeString Caption = FCommand->GetOption(Index).Caption;
  2658. Caption = StripHotkey(Caption);
  2659. if (!Caption.IsEmpty() && (Caption[Caption.Length()] == L':'))
  2660. {
  2661. Caption.SetLength(Caption.Length() - 1);
  2662. }
  2663. OpenDialog->Title = FMTLOAD(EXTENSION_OPTIONS_BROWSE_TITLE, (Caption));
  2664. UnicodeString ExpandedValue = ExpandEnvironmentVariables(ComboBox->Text);
  2665. OpenDialog->FileName = ExpandedValue;
  2666. UnicodeString InitialDir = ExtractFilePath(ExpandedValue);
  2667. if (!InitialDir.IsEmpty())
  2668. {
  2669. OpenDialog->InitialDir = InitialDir;
  2670. }
  2671. if (OpenDialog->Execute())
  2672. {
  2673. if (OpenDialog->FileName != ExpandedValue)
  2674. {
  2675. ComboBox->Text = OpenDialog->FileName;
  2676. }
  2677. }
  2678. }
  2679. //---------------------------------------------------------------------------
  2680. THistoryComboBox * __fastcall TCustomCommandOptionsDialog::CreateHistoryComboBox(
  2681. const TCustomCommandType::TOption & Option, const UnicodeString & Value)
  2682. {
  2683. THistoryComboBox * ComboBox = new THistoryComboBox(this);
  2684. ComboBox->AutoComplete = false;
  2685. AddComboBox(ComboBox, CreateLabel(Option.Caption));
  2686. ComboBox->Items = CustomWinConfiguration->History[HistoryKey(Option)];
  2687. ComboBox->Text = Value;
  2688. return ComboBox;
  2689. }
  2690. //---------------------------------------------------------------------------
  2691. UnicodeString __fastcall TCustomCommandOptionsDialog::HistoryKey(const TCustomCommandType::TOption & Option)
  2692. {
  2693. UnicodeString Result = FCommand->GetOptionKey(Option);
  2694. Result = CustomWinConfiguration->GetValidHistoryKey(Result);
  2695. return L"CustomCommandOption_" + Result;
  2696. }
  2697. //---------------------------------------------------------------------------
  2698. bool __fastcall TCustomCommandOptionsDialog::Execute()
  2699. {
  2700. bool Result = TCustomDialog::Execute();
  2701. if (Result)
  2702. {
  2703. DebugAssert(FCommand->OptionsCount == static_cast<int>(FControls.size()));
  2704. for (int Index = 0; Index < FCommand->OptionsCount; Index++)
  2705. {
  2706. const TCustomCommandType::TOption & Option = FCommand->GetOption(Index);
  2707. if ((Option.Kind != TCustomCommandType::okUnknown) &&
  2708. Option.IsControl)
  2709. {
  2710. UnicodeString OptionKey = FCommand->GetOptionKey(Option);
  2711. TControl * Control = FControls[Index];
  2712. UnicodeString Value;
  2713. if (Option.Kind == TCustomCommandType::okTextBox)
  2714. {
  2715. Value = SaveHistoryComboBoxValue(Control, Option);
  2716. }
  2717. else if (Option.Kind == TCustomCommandType::okFile)
  2718. {
  2719. Value = SaveHistoryComboBoxValue(Control, Option);
  2720. }
  2721. else if (Option.Kind == TCustomCommandType::okDropDownList)
  2722. {
  2723. Value = GetComboBoxValue(Control, Option.Default);
  2724. }
  2725. else if (Option.Kind == TCustomCommandType::okComboBox)
  2726. {
  2727. TComboBox * ComboBox = DebugNotNull(dynamic_cast<TComboBox *>(Control));
  2728. Value = GetComboBoxValue(Control, ComboBox->Text);
  2729. }
  2730. else if (Option.Kind == TCustomCommandType::okCheckBox)
  2731. {
  2732. TCheckBox * CheckBox = DebugNotNull(dynamic_cast<TCheckBox *>(Control));
  2733. int Index = (CheckBox->Checked ? 0 : 1);
  2734. Value = (Index < static_cast<int>(Option.Params.size())) ? Option.Params[Index] : UnicodeString();
  2735. }
  2736. else
  2737. {
  2738. DebugFail();
  2739. }
  2740. // The default value setter deletes the "name" when the value is empty.
  2741. // It would cause us to fall back to the default value, but we want to remember the empty value.
  2742. if (Value.IsEmpty())
  2743. {
  2744. int Index = FCustomCommandOptions->IndexOfName(OptionKey);
  2745. if (Index < 0)
  2746. {
  2747. Index = FCustomCommandOptions->Add(L"");
  2748. }
  2749. UnicodeString Line = OptionKey + FCustomCommandOptions->NameValueSeparator;
  2750. FCustomCommandOptions->Strings[Index] = Line;
  2751. }
  2752. else
  2753. {
  2754. FCustomCommandOptions->Values[OptionKey] = Value;
  2755. }
  2756. }
  2757. }
  2758. }
  2759. return Result;
  2760. }
  2761. //---------------------------------------------------------------------------
  2762. UnicodeString __fastcall TCustomCommandOptionsDialog::GetComboBoxValue(
  2763. TControl * Control, const UnicodeString & Default)
  2764. {
  2765. TComboBox * ComboBox = DebugNotNull(dynamic_cast<TComboBox *>(Control));
  2766. UnicodeString Result;
  2767. if (ComboBox->ItemIndex < 0)
  2768. {
  2769. Result = Default;
  2770. }
  2771. else
  2772. {
  2773. Result = FValues[Control->Tag][ComboBox->ItemIndex];
  2774. }
  2775. return Result;
  2776. }
  2777. //---------------------------------------------------------------------------
  2778. UnicodeString __fastcall TCustomCommandOptionsDialog::SaveHistoryComboBoxValue(
  2779. TControl * Control, const TCustomCommandType::TOption & Option)
  2780. {
  2781. THistoryComboBox * ComboBox = DebugNotNull(dynamic_cast<THistoryComboBox *>(Control));
  2782. ComboBox->SaveToHistory();
  2783. CustomWinConfiguration->History[HistoryKey(Option)] = ComboBox->Items;
  2784. return ComboBox->Text;
  2785. }
  2786. //---------------------------------------------------------------------------
  2787. void __fastcall TCustomCommandOptionsDialog::DoHelp()
  2788. {
  2789. UnicodeString HelpPage;
  2790. if (!FCommand->OptionsPage.IsEmpty())
  2791. {
  2792. HelpPage = FCommand->OptionsPage;
  2793. }
  2794. else
  2795. {
  2796. HelpPage = FCommand->HomePage;
  2797. }
  2798. if (!HelpPage.IsEmpty())
  2799. {
  2800. OpenBrowser(HelpPage);
  2801. }
  2802. else
  2803. {
  2804. TCustomDialog::DoHelp();
  2805. }
  2806. }
  2807. //---------------------------------------------------------------------------
  2808. void __fastcall TPreferencesDialog::ConfigureCommandButtonClick(TObject * /*Sender*/)
  2809. {
  2810. ConfigureCommand();
  2811. }
  2812. //---------------------------------------------------------------------------
  2813. void __fastcall TPreferencesDialog::ConfigureCommand()
  2814. {
  2815. int Index = CustomCommandsView->ItemIndex;
  2816. const TCustomCommandType * Command = GetCommandList(Index)->Commands[GetCommandIndex(Index)];
  2817. std::unique_ptr<TCustomCommandOptionsDialog> Dialog(new TCustomCommandOptionsDialog(Command, FCustomCommandOptions.get()));
  2818. Dialog->Execute();
  2819. UpdateCustomCommandsView();
  2820. }
  2821. //---------------------------------------------------------------------------