WinConfiguration.cpp 137 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "Common.h"
  5. #include "WinConfiguration.h"
  6. #include "Exceptions.h"
  7. #include "Bookmarks.h"
  8. #include "Terminal.h"
  9. #include "TextsWin.h"
  10. #include "WinInterface.h"
  11. #include "GUITools.h"
  12. #include "Tools.h"
  13. #include "Setup.h"
  14. #include "Security.h"
  15. #include "TerminalManager.h"
  16. #include "Cryptography.h"
  17. #include <VCLCommon.h>
  18. #include <InitGUID.h>
  19. #include <DragExt.h>
  20. #include <Math.hpp>
  21. #include <StrUtils.hpp>
  22. #include <Generics.Defaults.hpp>
  23. #include <OperationWithTimeout.hpp>
  24. #include "FileInfo.h"
  25. #include "CoreMain.h"
  26. #include "DriveView.hpp"
  27. //---------------------------------------------------------------------------
  28. #pragma package(smart_init)
  29. //---------------------------------------------------------------------------
  30. TWinConfiguration * WinConfiguration = NULL;
  31. //---------------------------------------------------------------------------
  32. static UnicodeString NotepadName(L"notepad.exe");
  33. static UnicodeString ToolbarsLayoutKey(L"ToolbarsLayout2");
  34. static UnicodeString ToolbarsLayoutOldKey(L"ToolbarsLayout");
  35. TDateTime DefaultUpdatesPeriod(7);
  36. // WORKAROUND (the semicolon, see TCustomListViewColProperties.GetParamsStr, and see other instances below)
  37. const UnicodeString ScpExplorerDirViewParamsDefault =
  38. L"0;1;0|150,1;70,1;150,1;79,1;62,1;55,0;20,0;150,0;125,0;@" + SaveDefaultPixelsPerInch() + L"|6;7;8;0;1;2;3;4;5";
  39. const UnicodeString ScpCommanderRemotePanelDirViewParamsDefault = ScpExplorerDirViewParamsDefault;
  40. const UnicodeString ScpCommanderLocalPanelDirViewParamsDefault =
  41. L"0;1;0|150,1;70,1;120,1;150,1;55,0;55,0;@" + SaveDefaultPixelsPerInch() + L"|5;0;1;2;3;4";
  42. UnicodeString QueueViewLayoutDefault;
  43. //---------------------------------------------------------------------------
  44. static const wchar_t FileColorDataSeparator = L':';
  45. TFileColorData::TFileColorData() :
  46. Color(TColor())
  47. {
  48. }
  49. //---------------------------------------------------------------------------
  50. void TFileColorData::Load(const UnicodeString & S)
  51. {
  52. UnicodeString Buf(S);
  53. Color = RestoreColor(CutToChar(Buf, FileColorDataSeparator, true));
  54. FileMask = Buf;
  55. }
  56. //---------------------------------------------------------------------------
  57. UnicodeString TFileColorData::Save() const
  58. {
  59. UnicodeString Result = StoreColor(Color) + FileColorDataSeparator + FileMask.Masks;
  60. return Result;
  61. }
  62. //---------------------------------------------------------------------------
  63. void TFileColorData::LoadList(const UnicodeString & S, TList & List)
  64. {
  65. std::unique_ptr<TStringList> Strings(CommaTextToStringList(S));
  66. List.clear();
  67. for (int Index = 0; Index < Strings->Count; Index++)
  68. {
  69. TFileColorData FileColorData;
  70. FileColorData.Load(Strings->Strings[Index]);
  71. List.push_back(FileColorData);
  72. }
  73. }
  74. //---------------------------------------------------------------------------
  75. UnicodeString TFileColorData::SaveList(const TList & List)
  76. {
  77. std::unique_ptr<TStringList> Strings(new TStringList());
  78. for (TFileColorData::TList::const_iterator Iter = List.begin(); Iter != List.end(); Iter++)
  79. {
  80. Strings->Add((*Iter).Save());
  81. }
  82. return Strings->CommaText;
  83. }
  84. //---------------------------------------------------------------------------
  85. //---------------------------------------------------------------------------
  86. __fastcall TEditorData::TEditorData() :
  87. FileMask(AnyMask),
  88. Editor(edInternal),
  89. ExternalEditor(L""),
  90. ExternalEditorText(false),
  91. SDIExternalEditor(false),
  92. DetectMDIExternalEditor(false)
  93. {
  94. }
  95. //---------------------------------------------------------------------------
  96. __fastcall TEditorData::TEditorData(const TEditorData & Source) :
  97. FileMask(Source.FileMask),
  98. Editor(Source.Editor),
  99. ExternalEditor(Source.ExternalEditor),
  100. ExternalEditorText(Source.ExternalEditorText),
  101. SDIExternalEditor(Source.SDIExternalEditor),
  102. DetectMDIExternalEditor(Source.DetectMDIExternalEditor)
  103. {
  104. }
  105. //---------------------------------------------------------------------------
  106. #define C(Property) (Property == rhd.Property)
  107. bool __fastcall TEditorData::operator==(const TEditorData & rhd) const
  108. {
  109. return
  110. C(FileMask) &&
  111. C(Editor) &&
  112. C(ExternalEditor) &&
  113. C(ExternalEditorText) &&
  114. C(SDIExternalEditor) &&
  115. C(DetectMDIExternalEditor) &&
  116. true;
  117. }
  118. #undef C
  119. //---------------------------------------------------------------------------
  120. void __fastcall TEditorData::ExternalEditorOptionsAutodetect()
  121. {
  122. UnicodeString Command = ExternalEditor;
  123. ReformatFileNameCommand(Command);
  124. UnicodeString ProgramName = ExtractProgramName(Command);
  125. // We explicitly do not use TEditorPreferences::GetDefaultExternalEditor(),
  126. // as we need to explicitly refer to the Notepad, even if the default external
  127. // editor ever changes
  128. UnicodeString NotepadProgramName = ExtractProgramName(NotepadName);
  129. if (SameText(ProgramName, NotepadProgramName))
  130. {
  131. // By default we use default transfer mode (binary),
  132. // as all reasonable 3rd party editors support all EOL styles.
  133. // A notable exception is Windows Notepad before Windows 10 1809, so here's an exception for it.
  134. ExternalEditorText = !IsWin10Build(17763);
  135. SDIExternalEditor = true;
  136. }
  137. }
  138. //---------------------------------------------------------------------------
  139. __fastcall TEditorPreferences::TEditorPreferences()
  140. {
  141. }
  142. //---------------------------------------------------------------------------
  143. __fastcall TEditorPreferences::TEditorPreferences(const TEditorData & Data) :
  144. FData(Data)
  145. {
  146. }
  147. //---------------------------------------------------------------------------
  148. bool __fastcall TEditorPreferences::operator==(const TEditorPreferences & rhp) const
  149. {
  150. return (FData == rhp.FData);
  151. }
  152. #undef C
  153. //---------------------------------------------------------------------------
  154. bool __fastcall TEditorPreferences::Matches(const UnicodeString FileName,
  155. bool Local, const TFileMasks::TParams & Params) const
  156. {
  157. return FData.FileMask.Matches(FileName, Local, false, &Params);
  158. }
  159. //---------------------------------------------------------------------------
  160. UnicodeString __fastcall TEditorPreferences::GetDefaultExternalEditor()
  161. {
  162. return NotepadName;
  163. }
  164. //---------------------------------------------------------------------------
  165. void __fastcall TEditorPreferences::LegacyDefaults()
  166. {
  167. FData.ExternalEditor = GetDefaultExternalEditor();
  168. FData.ExternalEditorOptionsAutodetect();
  169. }
  170. //---------------------------------------------------------------------------
  171. UnicodeString __fastcall TEditorPreferences::ExtractExternalEditorName() const
  172. {
  173. DebugAssert(FData.Editor == edExternal);
  174. UnicodeString ExternalEditor = FData.ExternalEditor;
  175. ReformatFileNameCommand(ExternalEditor);
  176. // Trim is a workaround for unknown problem with "notepad " (2 trailing spaces)
  177. return ExtractProgramName(ExternalEditor).Trim();
  178. }
  179. //---------------------------------------------------------------------------
  180. void __fastcall TEditorPreferences::Load(THierarchicalStorage * Storage, bool Legacy)
  181. {
  182. if (!Legacy)
  183. {
  184. FData.FileMask = Storage->ReadString(L"FileMask", FData.FileMask.Masks);
  185. }
  186. FData.Editor = (TEditor)Storage->ReadInteger(L"Editor", FData.Editor);
  187. FData.ExternalEditor = Storage->ReadString(L"ExternalEditor", FData.ExternalEditor);
  188. FData.ExternalEditorText = Storage->ReadBool(L"ExternalEditorText", FData.ExternalEditorText);
  189. FData.SDIExternalEditor = Storage->ReadBool(L"SDIExternalEditor", FData.SDIExternalEditor);
  190. FData.DetectMDIExternalEditor = Storage->ReadBool(L"DetectMDIExternalEditor", FData.DetectMDIExternalEditor);
  191. }
  192. //---------------------------------------------------------------------------
  193. void __fastcall TEditorPreferences::Save(THierarchicalStorage * Storage) const
  194. {
  195. Storage->WriteString(L"FileMask", FData.FileMask.Masks);
  196. Storage->WriteInteger(L"Editor", FData.Editor);
  197. Storage->WriteString(L"ExternalEditor", FData.ExternalEditor);
  198. Storage->WriteBool(L"ExternalEditorText", FData.ExternalEditorText);
  199. Storage->WriteBool(L"SDIExternalEditor", FData.SDIExternalEditor);
  200. Storage->WriteBool(L"DetectMDIExternalEditor", FData.DetectMDIExternalEditor);
  201. }
  202. //---------------------------------------------------------------------------
  203. TEditorData * __fastcall TEditorPreferences::GetData()
  204. {
  205. // returning non-const data, possible data change, invalidate cached name
  206. FName = L"";
  207. return &FData;
  208. };
  209. //---------------------------------------------------------------------------
  210. UnicodeString __fastcall TEditorPreferences::GetName() const
  211. {
  212. if (FName.IsEmpty())
  213. {
  214. if (FData.Editor == edInternal)
  215. {
  216. // StripHotkey is relic from times when INTERNAL_EDITOR_NAME was used
  217. // also for the menu item caption
  218. FName = StripHotkey(LoadStr(INTERNAL_EDITOR_NAME));
  219. }
  220. else if (FData.Editor == edOpen)
  221. {
  222. FName = StripHotkey(LoadStr(OPEN_EDITOR_NAME));
  223. }
  224. else
  225. {
  226. UnicodeString Program, Params, Dir;
  227. UnicodeString ExternalEditor = FData.ExternalEditor;
  228. ReformatFileNameCommand(ExternalEditor);
  229. SplitCommand(ExternalEditor, Program, Params, Dir);
  230. FName = ExtractFileName(Program);
  231. int P = FName.LastDelimiter(L".");
  232. if (P > 0)
  233. {
  234. FName.SetLength(P - 1);
  235. }
  236. if (FName.ByteType(1) == mbSingleByte)
  237. {
  238. if (FName.UpperCase() == FName)
  239. {
  240. FName = FName.LowerCase();
  241. }
  242. if (FName.LowerCase() == FName)
  243. {
  244. FName = FName.SubString(1, 1).UpperCase() +
  245. FName.SubString(2, FName.Length() - 1);
  246. }
  247. }
  248. }
  249. }
  250. return FName;
  251. }
  252. //---------------------------------------------------------------------------
  253. //---------------------------------------------------------------------------
  254. __fastcall TEditorList::TEditorList()
  255. {
  256. Init();
  257. }
  258. //---------------------------------------------------------------------------
  259. void __fastcall TEditorList::Init()
  260. {
  261. FEditors = new TList();
  262. FModified = false;
  263. }
  264. //---------------------------------------------------------------------------
  265. __fastcall TEditorList::~TEditorList()
  266. {
  267. Clear();
  268. delete FEditors;
  269. }
  270. //---------------------------------------------------------------------
  271. void __fastcall TEditorList::Modify()
  272. {
  273. FModified = true;
  274. }
  275. //---------------------------------------------------------------------------
  276. void __fastcall TEditorList::Saved()
  277. {
  278. FModified = false;
  279. }
  280. //---------------------------------------------------------------------------
  281. TEditorList & __fastcall TEditorList::operator=(const TEditorList & rhl)
  282. {
  283. Clear();
  284. for (int Index = 0; Index < rhl.Count; Index++)
  285. {
  286. Add(new TEditorPreferences(*rhl.Editors[Index]));
  287. }
  288. // there should be comparison of with the assigned list, but we rely on caller
  289. // to do it instead (TWinConfiguration::SetEditorList)
  290. Modify();
  291. return *this;
  292. }
  293. //---------------------------------------------------------------------------
  294. bool __fastcall TEditorList::operator==(const TEditorList & rhl) const
  295. {
  296. bool Result = (Count == rhl.Count);
  297. if (Result)
  298. {
  299. int i = 0;
  300. while ((i < Count) && Result)
  301. {
  302. Result = (*Editors[i]) == (*rhl.Editors[i]);
  303. i++;
  304. }
  305. }
  306. return Result;
  307. }
  308. //---------------------------------------------------------------------------
  309. void __fastcall TEditorList::Clear()
  310. {
  311. for (int i = 0; i < Count; i++)
  312. {
  313. delete Editors[i];
  314. }
  315. FEditors->Clear();
  316. }
  317. //---------------------------------------------------------------------------
  318. void __fastcall TEditorList::Add(TEditorPreferences * Editor)
  319. {
  320. Insert(Count, Editor);
  321. }
  322. //---------------------------------------------------------------------------
  323. void __fastcall TEditorList::Insert(int Index, TEditorPreferences * Editor)
  324. {
  325. FEditors->Insert(Index, reinterpret_cast<TObject *>(Editor));
  326. Modify();
  327. }
  328. //---------------------------------------------------------------------------
  329. void __fastcall TEditorList::Change(int Index, TEditorPreferences * Editor)
  330. {
  331. if (!((*Editors[Index]) == *Editor))
  332. {
  333. delete Editors[Index];
  334. FEditors->Items[Index] = (reinterpret_cast<TObject *>(Editor));
  335. Modify();
  336. }
  337. else
  338. {
  339. delete Editor;
  340. }
  341. }
  342. //---------------------------------------------------------------------------
  343. void __fastcall TEditorList::Move(int CurIndex, int NewIndex)
  344. {
  345. if (CurIndex != NewIndex)
  346. {
  347. FEditors->Move(CurIndex, NewIndex);
  348. Modify();
  349. }
  350. }
  351. //---------------------------------------------------------------------------
  352. void __fastcall TEditorList::Delete(int Index)
  353. {
  354. DebugAssert((Index >= 0) && (Index < Count));
  355. delete Editors[Index];
  356. FEditors->Delete(Index);
  357. Modify();
  358. }
  359. //---------------------------------------------------------------------------
  360. const TEditorPreferences * __fastcall TEditorList::Find(
  361. const UnicodeString FileName, bool Local, const TFileMasks::TParams & Params) const
  362. {
  363. const TEditorPreferences * Result = NULL;
  364. int i = 0;
  365. while ((i < FEditors->Count) && (Result == NULL))
  366. {
  367. Result = Editors[i];
  368. if (!Result->Matches(FileName, Local, Params))
  369. {
  370. Result = NULL;
  371. }
  372. i++;
  373. }
  374. return Result;
  375. }
  376. //---------------------------------------------------------------------------
  377. void __fastcall TEditorList::Load(THierarchicalStorage * Storage)
  378. {
  379. int Index = 0;
  380. bool Next;
  381. do
  382. {
  383. UnicodeString Name = IntToStr(Index);
  384. TEditorPreferences * Editor = NULL;
  385. try
  386. {
  387. Next = Storage->OpenSubKey(Name, false);
  388. if (Next)
  389. {
  390. try
  391. {
  392. Editor = new TEditorPreferences();
  393. Editor->Load(Storage, false);
  394. }
  395. __finally
  396. {
  397. Storage->CloseSubKey();
  398. }
  399. }
  400. }
  401. catch(...)
  402. {
  403. delete Editor;
  404. throw;
  405. }
  406. if (Editor != NULL)
  407. {
  408. FEditors->Add(reinterpret_cast<TObject *>(Editor));
  409. }
  410. Index++;
  411. }
  412. while (Next);
  413. FModified = false;
  414. }
  415. //---------------------------------------------------------------------------
  416. void __fastcall TEditorList::Save(THierarchicalStorage * Storage) const
  417. {
  418. Storage->ClearSubKeys();
  419. for (int Index = 0; Index < Count; Index++)
  420. {
  421. if (Storage->OpenSubKey(IntToStr(Index), true))
  422. {
  423. try
  424. {
  425. Editors[Index]->Save(Storage);
  426. }
  427. __finally
  428. {
  429. Storage->CloseSubKey();
  430. }
  431. }
  432. }
  433. }
  434. //---------------------------------------------------------------------------
  435. int __fastcall TEditorList::GetCount() const
  436. {
  437. int X = FEditors->Count;
  438. return X;
  439. }
  440. //---------------------------------------------------------------------------
  441. const TEditorPreferences * __fastcall TEditorList::GetEditor(int Index) const
  442. {
  443. return reinterpret_cast<TEditorPreferences *>(FEditors->Items[Index]);
  444. }
  445. //---------------------------------------------------------------------------
  446. bool __fastcall TEditorList::IsDefaultList() const
  447. {
  448. bool Result = true;
  449. for (int Index = 0; Result && (Index < Count); Index++)
  450. {
  451. const TEditorPreferences * Editor = GetEditor(Index);
  452. if (Editor->Data->Editor == edInternal)
  453. {
  454. // noop (keeps Result true)
  455. }
  456. else if (Editor->Data->Editor == edExternal)
  457. {
  458. UnicodeString ExternalEditor = Editor->ExtractExternalEditorName();
  459. UnicodeString DefaultExternalEditor = ExtractProgramName(TEditorPreferences::GetDefaultExternalEditor());
  460. Result = SameText(ExternalEditor, DefaultExternalEditor);
  461. }
  462. else
  463. {
  464. Result = false;
  465. }
  466. }
  467. return Result;
  468. }
  469. //---------------------------------------------------------------------------
  470. //---------------------------------------------------------------------------
  471. __fastcall TWinConfiguration::TWinConfiguration(): TCustomWinConfiguration()
  472. {
  473. FInvalidDefaultTranslationMessage = L"";
  474. FDDExtInstalled = -1;
  475. FBookmarks = new TBookmarks();
  476. FCustomCommandList = new TCustomCommandList();
  477. FExtensionList = new TCustomCommandList();
  478. FEditorList = new TEditorList();
  479. FDefaultUpdatesPeriod = 0;
  480. FDontDecryptPasswords = 0;
  481. FMasterPasswordSession = 0;
  482. FMasterPasswordSessionAsked = false;
  483. FCustomCommandOptions.reset(new TStringList());
  484. FCustomCommandOptionsModified = false;
  485. FExtensionTranslations.reset(new TStringList());
  486. Default();
  487. // This matters only if the translations are in the executable folder and auto-loaded by VCL (System.Pas - DelayLoadResourceModule)
  488. try
  489. {
  490. UnicodeString ResourceModuleName = GetResourceModuleName(Application->Name, ModuleFileName().c_str());
  491. CheckTranslationVersion(ResourceModuleName, true);
  492. }
  493. catch(Exception & E)
  494. {
  495. FInvalidDefaultTranslationMessage = E.Message;
  496. }
  497. // Load complete locale according to the UI language
  498. SetLocaleInternal(NULL, true, true);
  499. FDefaultLocale = AppliedLocale;
  500. }
  501. //---------------------------------------------------------------------------
  502. __fastcall TWinConfiguration::~TWinConfiguration()
  503. {
  504. if (!FTemporarySessionFile.IsEmpty()) DeleteFile(ApiPath(FTemporarySessionFile));
  505. ClearTemporaryLoginData();
  506. ReleaseExtensionTranslations();
  507. delete FBookmarks;
  508. delete FCustomCommandList;
  509. delete FExtensionList;
  510. delete FEditorList;
  511. }
  512. //---------------------------------------------------------------------------
  513. void __fastcall TWinConfiguration::Default()
  514. {
  515. FCustomCommandsDefaults = true;
  516. FCustomCommandOptionsModified = false;
  517. TCustomWinConfiguration::Default();
  518. int WorkAreaWidthScaled = DimensionToDefaultPixelsPerInch(Screen->WorkAreaWidth);
  519. int WorkAreaHeightScaled = DimensionToDefaultPixelsPerInch(Screen->WorkAreaHeight);
  520. UnicodeString PixelsPerInchToolbarValue = "PixelsPerInch=" + SaveDefaultPixelsPerInch();
  521. FDDDisableMove = false;
  522. FDDTransferConfirmation = asAuto;
  523. FDDTemporaryDirectory = L"";
  524. FDDDrives = L"";
  525. FDDWarnLackOfTempSpace = true;
  526. FDDWarnLackOfTempSpaceRatio = 1.1;
  527. FDDFakeFile = true;
  528. FDDExtTimeout = MSecsPerSec;
  529. FDeleteToRecycleBin = true;
  530. FSelectDirectories = false;
  531. FSelectMask = AnyMask;
  532. FShowHiddenFiles = false;
  533. FFormatSizeBytes = fbKilobytes;
  534. FPanelSearch = isNameStartOnly;
  535. FShowInaccesibleDirectories = true;
  536. FConfirmTransferring = true;
  537. FConfirmDeleting = true;
  538. FConfirmRecycling = true;
  539. FConfirmClosingSession = true;
  540. FDoubleClickAction = dcaEdit;
  541. FCopyOnDoubleClickConfirmation = false;
  542. FAlwaysRespectDoubleClickAction = false;
  543. FDimmHiddenFiles = true;
  544. FRenameWholeName = false;
  545. FAutoStartSession = L"";
  546. FExpertMode = true;
  547. FUseLocationProfiles = false;
  548. FUseSharedBookmarks = false;
  549. FDefaultDirIsHome = true;
  550. FDDDeleteDelay = 120;
  551. FTemporaryDirectoryAppendSession = false;
  552. FTemporaryDirectoryDeterministic = false;
  553. FTemporaryDirectoryAppendPath = true;
  554. FTemporaryDirectoryCleanup = true;
  555. FConfirmTemporaryDirectoryCleanup = true;
  556. FPreservePanelState = true;
  557. FDarkTheme = asAuto;
  558. FLastStoredSession = L"";
  559. // deliberately not being saved, so that when saving ad-hoc workspace,
  560. // we do not offer to overwrite the last saved workspace, what may be undesirable
  561. FLastWorkspace = L"";
  562. FAutoSaveWorkspace = false;
  563. FAutoSaveWorkspacePasswords = false;
  564. FAutoWorkspace = L"";
  565. FPathInCaption = picShort;
  566. FSessionTabNameFormat = stnfShortPathTrunc;
  567. FMinimizeToTray = false;
  568. FBalloonNotifications = true;
  569. FNotificationsTimeout = 10;
  570. FNotificationsStickTime = 2;
  571. FCopyParamAutoSelectNotice = true;
  572. FLockToolbars = false;
  573. FSelectiveToolbarText = true;
  574. FLargerToolbar = 1;
  575. FAutoOpenInPutty = false;
  576. FRefreshRemotePanel = false;
  577. FRefreshRemotePanelInterval = TDateTime(0, 1, 0, 0);
  578. FPanelFont.FontName = L"";
  579. FPanelFont.FontSize = 0;
  580. FPanelFont.FontStyle = 0;
  581. FPanelFont.FontCharset = DEFAULT_CHARSET;
  582. FNaturalOrderNumericalSorting = true;
  583. FAlwaysSortDirectoriesByName = false;
  584. FFullRowSelect = false;
  585. FOfferedEditorAutoConfig = false;
  586. FVersionHistory = L"";
  587. AddVersionToHistory();
  588. FUseMasterPassword = false;
  589. FPlainMasterPasswordEncrypt = L"";
  590. FPlainMasterPasswordDecrypt = L"";
  591. FMasterPasswordVerifier = L"";
  592. FOpenedStoredSessionFolders = L"";
  593. FAutoImportedFromPuttyOrFilezilla = false;
  594. FGenerateUrlComponents = -1;
  595. FGenerateUrlCodeTarget = guctUrl;
  596. FGenerateUrlScriptFormat = sfScriptFile;
  597. FGenerateUrlAssemblyLanguage = alCSharp;
  598. FExternalSessionInExistingInstance = true;
  599. FShowLoginWhenNoSession = true;
  600. FKeepOpenWhenNoSession = true;
  601. FDefaultToNewRemoteTab = true;
  602. FLocalIconsByExt = false;
  603. FFlashTaskbar = true;
  604. FMaxSessions = 100;
  605. FBidiModeOverride = lfoLanguageIfRecommended;
  606. FFlipChildrenOverride = lfoLanguageIfRecommended;
  607. FShowTips = true;
  608. FTipsSeen = L"";
  609. FTipsShown = Now();
  610. FFileColors = L"";
  611. FRunsSinceLastTip = 0;
  612. FExtensionsDeleted = L"";
  613. FLockedInterface = false;
  614. HonorDrivePolicy = true;
  615. UseABDrives = true;
  616. TimeoutShellOperations = true;
  617. TimeoutShellIconRetrieval = false;
  618. UseIconUpdateThread = true;
  619. AllowWindowPrint = false;
  620. StoreTransition = stInit;
  621. QueueTransferLimitMax = 9;
  622. HiContrast = false;
  623. EditorCheckNotModified = false;
  624. SessionTabCaptionTruncation = true;
  625. LoadingTooLongLimit = 15;
  626. FirstRun = StandardDatestamp();
  627. FEditor.Font.FontName = DefaultFixedWidthFontName;
  628. FEditor.Font.FontSize = DefaultFixedWidthFontSize;
  629. FEditor.Font.FontStyle = 0;
  630. FEditor.Font.FontCharset = DEFAULT_CHARSET;
  631. FEditor.FontColor = TColor(0);
  632. FEditor.BackgroundColor = TColor(0);
  633. FEditor.WordWrap = false;
  634. FEditor.FindText = L"";
  635. FEditor.ReplaceText = L"";
  636. FEditor.FindMatchCase = false;
  637. FEditor.FindWholeWord = false;
  638. FEditor.FindDown = true;
  639. FEditor.TabSize = 8;
  640. FEditor.MaxEditors = 500;
  641. FEditor.EarlyClose = 2; // seconds
  642. FEditor.SDIShellEditor = false;
  643. FEditor.WindowParams = L"";
  644. FEditor.Encoding = CP_ACP;
  645. FEditor.WarnOnEncodingFallback = true;
  646. FEditor.WarnOrLargeFileSize = true;
  647. FEditor.AutoFont = true;
  648. FEditor.DisableSmoothScroll = false;
  649. FQueueView.Height = 140;
  650. FQueueView.HeightPixelsPerInch = USER_DEFAULT_SCREEN_DPI;
  651. if (QueueViewLayoutDefault.IsEmpty())
  652. {
  653. // with 1000 pixels wide screen, both interfaces are wide enough to fit wider queue
  654. QueueViewLayoutDefault =
  655. UnicodeString((WorkAreaWidthScaled > 1000) ? L"70,250,250,80,80,80,100" : L"70,160,160,80,80,80,100") +
  656. // WORKAROUND (the comma), see GetListViewStr
  657. L",;" + SaveDefaultPixelsPerInch();
  658. }
  659. FQueueView.Layout = QueueViewLayoutDefault;
  660. FQueueView.Show = qvHideWhenEmpty;
  661. FQueueView.LastHideShow = qvHideWhenEmpty;
  662. FQueueView.ToolBar = true;
  663. FQueueView.Label = true;
  664. FQueueView.FileList = false;
  665. FQueueView.FileListHeight = 90;
  666. FQueueView.FileListHeightPixelsPerInch = USER_DEFAULT_SCREEN_DPI;
  667. FEnableQueueByDefault = true;
  668. FUpdates.Period = FDefaultUpdatesPeriod;
  669. FUpdates.LastCheck = 0;
  670. FUpdates.HaveResults = false;
  671. FUpdates.ShownResults = false;
  672. FUpdates.BetaVersions = asAuto;
  673. FUpdates.ShowOnStartup = true;
  674. FUpdates.AuthenticationEmail = L"";
  675. FUpdates.Mode = EmptyStr;
  676. // for backward compatibility the default is decided based on value of ProxyHost
  677. FUpdates.ConnectionType = (TConnectionType)-1;
  678. FUpdates.ProxyHost = L""; // keep empty (see above)
  679. FUpdates.ProxyPort = 8080;
  680. FUpdates.Results.Reset();
  681. FUpdates.DotNetVersion = L"";
  682. FUpdates.ConsoleVersion = L"";
  683. int ExplorerWidth = Min(WorkAreaWidthScaled - 40, 960);
  684. int ExplorerHeight = Min(WorkAreaHeightScaled - 30, 720);
  685. FScpExplorer.WindowParams = FormatDefaultWindowParams(ExplorerWidth, ExplorerHeight);
  686. FScpExplorer.DirViewParams = ScpExplorerDirViewParamsDefault;
  687. FScpExplorer.ToolbarsLayout =
  688. UnicodeString(
  689. L"Queue=1::0+-1,"
  690. "Menu=1:TopDock:0+0,"
  691. "Buttons=1:TopDock:2+0,"
  692. "Selection=0:TopDock:3+0,"
  693. "Session=0:TopDock:6+0,"
  694. "Preferences=1:TopDock:4+0,"
  695. "Sort=0:TopDock:5+0,"
  696. "Address=1:TopDock:1+0,"
  697. "Updates=1:TopDock:4+393,"
  698. "Transfer=1:TopDock:4+171,"
  699. "CustomCommands=0:TopDock:7+0,") +
  700. PixelsPerInchToolbarValue;
  701. FScpExplorer.ToolbarsButtons = UnicodeString();
  702. FScpExplorer.SessionsTabs = true;
  703. FScpExplorer.StatusBar = true;
  704. FScpExplorer.LastLocalTargetDirectory = GetPersonalFolder();
  705. FScpExplorer.ViewStyle = dvsIcon;
  706. FScpExplorer.ShowFullAddress = true;
  707. FScpExplorer.DriveView = true;
  708. FScpExplorer.DriveViewWidth = 180;
  709. FScpExplorer.DriveViewWidthPixelsPerInch = USER_DEFAULT_SCREEN_DPI;
  710. int CommanderWidth = Min(WorkAreaWidthScaled - 40, 1090);
  711. int CommanderHeight = Min(WorkAreaHeightScaled - 30, 700);
  712. FScpCommander.WindowParams = FormatDefaultWindowParams(CommanderWidth, CommanderHeight);
  713. FScpCommander.LocalPanelWidth = 0.5;
  714. FScpCommander.SwappedPanels = false;
  715. FScpCommander.SessionsTabs = true;
  716. FScpCommander.StatusBar = true;
  717. FScpCommander.NortonLikeMode = nlKeyboard;
  718. FScpCommander.PreserveLocalDirectory = false;
  719. FScpCommander.ToolbarsLayout =
  720. UnicodeString(
  721. L"Queue=1::0+-1,"
  722. "Menu=1:TopDock:0+0,"
  723. "Preferences=1:TopDock:1+228,"
  724. "Session=0:TopDock:1+602,"
  725. "Sort=0:TopDock:2+0,"
  726. "Commands=1:TopDock:1+0,"
  727. "Updates=1:TopDock:1+596,"
  728. "Transfer=1:TopDock:1+341,"
  729. "CustomCommands=0:TopDock:3+0,"
  730. "RemoteHistory=1:RemoteTopDock:0+172,"
  731. "RemoteNavigation=1:RemoteTopDock:0+252,"
  732. "RemotePath=1:RemoteTopDock:0+0,"
  733. "RemoteFile=1:RemoteTopDock:1+0,"
  734. "RemoteSelection=1:RemoteTopDock:1+345,"
  735. "LocalHistory=1:LocalTopDock:0+207,"
  736. "LocalNavigation=1:LocalTopDock:0+287,"
  737. "LocalPath=1:LocalTopDock:0+0,"
  738. "LocalFile=1:LocalTopDock:1+0,"
  739. "LocalSelection=1:LocalTopDock:1+329,"
  740. "Toolbar2=0:BottomDock:1+0,"
  741. "CommandLine=0:BottomDock:0+0,") +
  742. PixelsPerInchToolbarValue;
  743. FScpCommander.ToolbarsButtons = UnicodeString();
  744. FScpCommander.CurrentPanel = osLocal;
  745. FScpCommander.CompareByTime = true;
  746. FScpCommander.CompareBySize = false;
  747. FScpCommander.TreeOnLeft = false;
  748. FScpCommander.ExplorerKeyboardShortcuts = false;
  749. FScpCommander.SystemContextMenu = false;
  750. FScpCommander.RemotePanel.DirViewParams = ScpCommanderRemotePanelDirViewParamsDefault;
  751. FScpCommander.RemotePanel.ViewStyle = dvsReport;
  752. FScpCommander.RemotePanel.StatusBar = true;
  753. FScpCommander.RemotePanel.DriveView = false;
  754. FScpCommander.RemotePanel.DriveViewHeight = 100;
  755. FScpCommander.RemotePanel.DriveViewHeightPixelsPerInch = USER_DEFAULT_SCREEN_DPI;
  756. FScpCommander.RemotePanel.DriveViewWidth = 100;
  757. FScpCommander.RemotePanel.DriveViewWidthPixelsPerInch = USER_DEFAULT_SCREEN_DPI;
  758. FScpCommander.RemotePanel.LastPath = UnicodeString();
  759. FScpCommander.LocalPanel.DirViewParams = ScpCommanderLocalPanelDirViewParamsDefault;
  760. FScpCommander.LocalPanel.ViewStyle = dvsReport;
  761. FScpCommander.LocalPanel.StatusBar = true;
  762. FScpCommander.LocalPanel.DriveView = false;
  763. FScpCommander.LocalPanel.DriveViewHeight = 100;
  764. FScpCommander.LocalPanel.DriveViewHeightPixelsPerInch = USER_DEFAULT_SCREEN_DPI;
  765. FScpCommander.LocalPanel.DriveViewWidth = 100;
  766. FScpCommander.LocalPanel.DriveViewWidthPixelsPerInch = USER_DEFAULT_SCREEN_DPI;
  767. FScpCommander.LocalPanel.LastPath = UnicodeString();
  768. FScpCommander.OtherLocalPanelDirViewParams = FScpCommander.LocalPanel.DirViewParams;
  769. FScpCommander.OtherLocalPanelViewStyle = FScpCommander.LocalPanel.ViewStyle;
  770. FScpCommander.OtherLocalPanelLastPath = UnicodeString();
  771. FBookmarks->Clear();
  772. }
  773. //---------------------------------------------------------------------------
  774. void __fastcall TWinConfiguration::DefaultLocalized()
  775. {
  776. TGUIConfiguration::DefaultLocalized();
  777. if (FCustomCommandsDefaults)
  778. {
  779. FCustomCommandList->Clear();
  780. FCustomCommandList->Add(LoadStr(CUSTOM_COMMAND_EXECUTE), L"\"./!\"", 0);
  781. FCustomCommandList->Add(LoadStr(CUSTOM_COMMAND_TOUCH), L"touch \"!\"", ccApplyToDirectories | ccRecursive);
  782. FCustomCommandList->Add(LoadStr(CUSTOM_COMMAND_TAR),
  783. FORMAT(L"tar -cz -f \"!?%s?archive.tgz!\" !&",
  784. (LoadStr(CUSTOM_COMMAND_TAR_ARCHIVE))), ccApplyToDirectories);
  785. FCustomCommandList->Add(LoadStr(CUSTOM_COMMAND_UNTAR),
  786. FORMAT(L"tar -xz --directory=\"!?%s?.!\" -f \"!\"",
  787. (LoadStr(CUSTOM_COMMAND_UNTAR_DIRECTORY))), 0);
  788. FCustomCommandList->Add(LoadStr(CUSTOM_COMMAND_GREP),
  789. FORMAT(L"grep \"!?%s?!\" !&", (LoadStr(CUSTOM_COMMAND_GREP_PATTERN))),
  790. ccShowResults);
  791. FCustomCommandList->Add(LoadStr(CUSTOM_COMMAND_PRINT), L"notepad.exe /p \"!\"", ccLocal);
  792. FCustomCommandList->Reset();
  793. FCustomCommandsDefaults = true;
  794. }
  795. }
  796. //---------------------------------------------------------------------------
  797. bool __fastcall TWinConfiguration::DetectRegistryStorage(HKEY RootKey)
  798. {
  799. bool Result = false;
  800. TRegistryStorage * Storage = new TRegistryStorage(RegistryStorageKey, RootKey);
  801. try
  802. {
  803. if (Storage->OpenRootKey(false))
  804. {
  805. Result = true;
  806. Storage->CloseSubKey();
  807. }
  808. }
  809. __finally
  810. {
  811. delete Storage;
  812. }
  813. return Result;
  814. }
  815. //---------------------------------------------------------------------------
  816. bool __fastcall TWinConfiguration::CanWriteToStorage()
  817. {
  818. bool Result = false;
  819. try
  820. {
  821. THierarchicalStorage * Storage = CreateConfigStorage();
  822. try
  823. {
  824. Storage->AccessMode = smReadWrite;
  825. // This is actually not very good test, as we end up potentially with
  826. // the very same config, and TIniFileStorage file won't even try to
  827. // write the file then. Luckily, we use this for empty config only,
  828. // so we end up with at least an empty section.
  829. if (Storage->OpenSubKey(ConfigurationSubKey, true))
  830. {
  831. Storage->WriteBool(L"WriteTest", true);
  832. Storage->DeleteValue(L"WriteTest");
  833. }
  834. Storage->Flush();
  835. }
  836. __finally
  837. {
  838. delete Storage;
  839. }
  840. Result = true;
  841. }
  842. catch(...)
  843. {
  844. }
  845. return Result;
  846. }
  847. //---------------------------------------------------------------------------
  848. bool TWinConfiguration::DetectStorage(bool SafeOnly)
  849. {
  850. bool Result;
  851. UnicodeString IniFile = IniFileStorageNameForReading;
  852. if (FileExists(ApiPath(IniFile)))
  853. {
  854. Result = !SafeOnly;
  855. if (Result)
  856. {
  857. FStorage = stIniFile;
  858. AppLogFmt(L"Will use existing INI file %s for configuration", (IniFile));
  859. }
  860. }
  861. else
  862. {
  863. if (IsUWP() ||
  864. DetectRegistryStorage(HKEY_CURRENT_USER) ||
  865. DetectRegistryStorage(HKEY_LOCAL_MACHINE))
  866. {
  867. FStorage = stRegistry;
  868. if (IsUWP())
  869. {
  870. AppLog(L"Will use registry for configuration as this is store installation");
  871. }
  872. else
  873. {
  874. AppLog(L"Will use existing registry for configuration");
  875. }
  876. Result = true;
  877. }
  878. else
  879. {
  880. if (SafeOnly)
  881. {
  882. Result = false;
  883. AppLog(L"No configuration found");
  884. }
  885. else
  886. {
  887. FStorage = stIniFile;
  888. // As we fall back to user profile folder, when application folder
  889. // is not writtable, it is actually unlikely that the below test ever fails.
  890. if (!CanWriteToStorage())
  891. {
  892. FStorage = stRegistry;
  893. AppLog(L"Will use registry configuration as no configuration was found and default INI file location is not writtable");
  894. }
  895. else
  896. {
  897. AppLog(L"Will use (not yet existing) INI file for configuration");
  898. }
  899. // With !SafeOnly we always return true, so this result is actually never considered
  900. Result = true;
  901. }
  902. }
  903. }
  904. return Result;
  905. }
  906. //---------------------------------------------------------------------------
  907. TStorage __fastcall TWinConfiguration::GetStorage()
  908. {
  909. if (FStorage == stDetect)
  910. {
  911. if (FindResourceEx(NULL, RT_RCDATA, L"WINSCP_SESSION",
  912. MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL)))
  913. {
  914. FTemporarySessionFile =
  915. IncludeTrailingBackslash(SystemTemporaryDirectory()) + L"winscps.tmp";
  916. DumpResourceToFile(L"WINSCP_SESSION", FTemporarySessionFile);
  917. FEmbeddedSessions = true;
  918. FTemporaryKeyFile =
  919. IncludeTrailingBackslash(SystemTemporaryDirectory()) + L"winscpk.tmp";
  920. if (!DumpResourceToFile(L"WINSCP_KEY", FTemporaryKeyFile))
  921. {
  922. FTemporaryKeyFile = L"";
  923. }
  924. }
  925. DebugCheck(DetectStorage(false));
  926. }
  927. // Meaning any inherited autodetection basically does not happen, so the below call returns FStorage
  928. DebugAssert(FStorage != stDetect);
  929. TStorage Result = TCustomWinConfiguration::GetStorage();
  930. return Result;
  931. }
  932. //---------------------------------------------------------------------------
  933. bool TWinConfiguration::TrySetSafeStorage()
  934. {
  935. return DetectStorage(true);
  936. }
  937. //---------------------------------------------------------------------------
  938. void __fastcall TWinConfiguration::Saved()
  939. {
  940. TCustomWinConfiguration::Saved();
  941. FBookmarks->ModifyAll(false);
  942. FCustomCommandList->Reset();
  943. FEditorList->Saved();
  944. }
  945. //---------------------------------------------------------------------------
  946. void __fastcall TWinConfiguration::RecryptPasswords(TStrings * RecryptPasswordErrors)
  947. {
  948. TCustomWinConfiguration::RecryptPasswords(RecryptPasswordErrors);
  949. try
  950. {
  951. TTerminalManager * Manager = TTerminalManager::Instance(false);
  952. DebugAssert(Manager != NULL);
  953. if (Manager != NULL)
  954. {
  955. Manager->RecryptPasswords();
  956. }
  957. }
  958. catch (Exception & E)
  959. {
  960. UnicodeString Message;
  961. if (ExceptionMessage(&E, Message))
  962. {
  963. // we do not expect this really to happen,
  964. // so we do not bother providing context
  965. RecryptPasswordErrors->Add(Message);
  966. }
  967. }
  968. }
  969. //---------------------------------------------------------------------------
  970. bool __fastcall TWinConfiguration::GetUseMasterPassword()
  971. {
  972. return FUseMasterPassword;
  973. }
  974. //---------------------------------------------------------------------------
  975. THierarchicalStorage * TWinConfiguration::CreateScpStorage(bool & SessionList)
  976. {
  977. // Detect embedded session, if not checked yet
  978. GetStorage();
  979. THierarchicalStorage * Result;
  980. if (SessionList && !FTemporarySessionFile.IsEmpty())
  981. {
  982. Result = TIniFileStorage::CreateFromPath(FTemporarySessionFile);
  983. // This is session-list specific store, so the only instance,
  984. // we do not reset the SessionList argument
  985. // (compare TConfiguration::CreateScpStorage)
  986. }
  987. else
  988. {
  989. Result = TCustomWinConfiguration::CreateScpStorage(SessionList);
  990. }
  991. return Result;
  992. }
  993. //---------------------------------------------------------------------------
  994. // duplicated from core\configuration.cpp
  995. #define BLOCK(KEY, CANCREATE, BLOCK) \
  996. if (Storage->OpenSubKeyPath(KEY, CANCREATE)) try { BLOCK } __finally { Storage->CloseSubKeyPath(); }
  997. #define KEY(TYPE, VAR) KEYEX(TYPE, VAR, PropertyToKey(TEXT(#VAR)))
  998. #define REGCONFIG(CANCREATE) \
  999. BLOCK(L"Interface", CANCREATE, \
  1000. KEYEX(Integer,DoubleClickAction, L"CopyOnDoubleClick"); \
  1001. KEY(Bool, CopyOnDoubleClickConfirmation); \
  1002. KEY(Bool, AlwaysRespectDoubleClickAction); \
  1003. KEY(Bool, DDDisableMove); \
  1004. KEYEX(Integer, DDTransferConfirmation, L"DDTransferConfirmation2"); \
  1005. KEY(String, DDTemporaryDirectory); \
  1006. KEY(String, DDDrives); \
  1007. KEY(Bool, DDWarnLackOfTempSpace); \
  1008. KEY(Float, DDWarnLackOfTempSpaceRatio); \
  1009. KEY(Bool, DeleteToRecycleBin); \
  1010. KEY(Bool, DimmHiddenFiles); \
  1011. KEY(Bool, RenameWholeName); \
  1012. KEY(Bool, SelectDirectories); \
  1013. KEY(String, SelectMask); \
  1014. KEY(Bool, ShowHiddenFiles); \
  1015. KEY(Integer, FormatSizeBytes); \
  1016. KEY(Integer, PanelSearch); \
  1017. KEY(Bool, ShowInaccesibleDirectories); \
  1018. KEY(Bool, ConfirmTransferring); \
  1019. KEY(Bool, ConfirmDeleting); \
  1020. KEY(Bool, ConfirmRecycling); \
  1021. KEY(Bool, ConfirmClosingSession); \
  1022. KEY(String, AutoStartSession); \
  1023. KEY(Bool, UseLocationProfiles); \
  1024. KEY(Bool, UseSharedBookmarks); \
  1025. KEY(Integer, LocaleSafe); \
  1026. KEY(Bool, DDFakeFile); \
  1027. KEY(Integer, DDExtTimeout); \
  1028. KEY(Bool, DefaultDirIsHome); \
  1029. KEY(Bool, TemporaryDirectoryAppendSession); \
  1030. KEY(Bool, TemporaryDirectoryAppendPath); \
  1031. KEY(Bool, TemporaryDirectoryDeterministic); \
  1032. KEY(Bool, TemporaryDirectoryCleanup); \
  1033. KEY(Bool, ConfirmTemporaryDirectoryCleanup); \
  1034. KEY(Bool, PreservePanelState); \
  1035. KEY(Integer, DarkTheme); \
  1036. KEY(String, LastStoredSession); \
  1037. KEY(Bool, AutoSaveWorkspace); \
  1038. KEY(Bool, AutoSaveWorkspacePasswords); \
  1039. KEY(String, AutoWorkspace); \
  1040. KEY(Integer, PathInCaption); \
  1041. KEY(Integer, SessionTabNameFormat); \
  1042. KEY(Bool, MinimizeToTray); \
  1043. KEY(Bool, BalloonNotifications); \
  1044. KEY(Integer, NotificationsTimeout); \
  1045. KEY(Integer, NotificationsStickTime); \
  1046. KEY(Bool, CopyParamAutoSelectNotice); \
  1047. KEY(Bool, LockToolbars); \
  1048. KEY(Bool, SelectiveToolbarText); \
  1049. KEY(Integer, LargerToolbar); \
  1050. KEY(Bool, AutoOpenInPutty); \
  1051. KEY(Bool, RefreshRemotePanel); \
  1052. KEY(DateTime, RefreshRemotePanelInterval); \
  1053. KEYEX(String, PanelFont.FontName, L"PanelFontName"); \
  1054. KEYEX(Integer,PanelFont.FontSize, L"PanelFontSize"); \
  1055. KEYEX(Integer,PanelFont.FontStyle, L"PanelFontStyle"); \
  1056. KEYEX(Integer,PanelFont.FontCharset, L"PanelFontCharset"); \
  1057. KEY(Bool, NaturalOrderNumericalSorting); \
  1058. KEY(Bool, AlwaysSortDirectoriesByName); \
  1059. KEY(Bool, FullRowSelect); \
  1060. KEY(Bool, OfferedEditorAutoConfig); \
  1061. KEY(Integer, LastMonitor); \
  1062. KEY(String, VersionHistory); \
  1063. KEY(Bool, EnableQueueByDefault); \
  1064. KEY(String, OpenedStoredSessionFolders); \
  1065. KEY(Bool, AutoImportedFromPuttyOrFilezilla); \
  1066. KEY(Integer, GenerateUrlComponents); \
  1067. KEY(Integer, GenerateUrlCodeTarget); \
  1068. KEY(Integer, GenerateUrlScriptFormat); \
  1069. KEY(Integer, GenerateUrlAssemblyLanguage); \
  1070. KEY(Bool, ExternalSessionInExistingInstance); \
  1071. KEY(Bool, ShowLoginWhenNoSession); \
  1072. KEY(Bool, KeepOpenWhenNoSession); \
  1073. KEY(Bool, DefaultToNewRemoteTab); \
  1074. KEY(Bool, LocalIconsByExt); \
  1075. KEY(Bool, FlashTaskbar); \
  1076. KEY(Integer, MaxSessions); \
  1077. KEY(Integer, BidiModeOverride); \
  1078. KEY(Integer, FlipChildrenOverride); \
  1079. KEY(Bool, ShowTips); \
  1080. KEY(String, TipsSeen); \
  1081. KEY(DateTime, TipsShown); \
  1082. KEY(String, FileColors); \
  1083. KEY(Integer, RunsSinceLastTip); \
  1084. KEY(Bool, HonorDrivePolicy); \
  1085. KEY(Bool, UseABDrives); \
  1086. KEY(Integer, LastMachineInstallations); \
  1087. KEY(String, FExtensionsDeleted); \
  1088. KEY(String, FExtensionsOrder); \
  1089. KEY(String, FExtensionsShortCuts); \
  1090. KEY(Bool, TimeoutShellOperations); \
  1091. KEY(Bool, TimeoutShellIconRetrieval); \
  1092. KEY(Bool, UseIconUpdateThread); \
  1093. KEY(Bool, AllowWindowPrint); \
  1094. KEY(Integer, StoreTransition); \
  1095. KEY(Integer, QueueTransferLimitMax); \
  1096. KEY(Bool, HiContrast); \
  1097. KEY(Bool, EditorCheckNotModified); \
  1098. KEY(Bool, SessionTabCaptionTruncation); \
  1099. KEY(Integer, LoadingTooLongLimit); \
  1100. KEY(String, FirstRun); \
  1101. ); \
  1102. BLOCK(L"Interface\\Editor", CANCREATE, \
  1103. KEYEX(String, Editor.Font.FontName, L"FontName2"); \
  1104. KEY(Integer, Editor.Font.FontSize); \
  1105. KEY(Integer, Editor.Font.FontStyle); \
  1106. KEY(Integer, Editor.Font.FontCharset); \
  1107. KEY(Integer, Editor.FontColor); \
  1108. KEY(Integer, Editor.BackgroundColor); \
  1109. KEY(Bool, Editor.WordWrap); \
  1110. KEY(String, Editor.FindText); \
  1111. KEY(String, Editor.ReplaceText); \
  1112. KEY(Bool, Editor.FindMatchCase); \
  1113. KEY(Bool, Editor.FindWholeWord); \
  1114. KEY(Bool, Editor.FindDown); \
  1115. KEY(Integer, Editor.TabSize); \
  1116. KEY(Integer, Editor.MaxEditors); \
  1117. KEY(Integer, Editor.EarlyClose); \
  1118. KEY(Bool, Editor.SDIShellEditor); \
  1119. KEY(String, Editor.WindowParams); \
  1120. KEY(Integer, Editor.Encoding); \
  1121. KEY(Bool, Editor.WarnOnEncodingFallback); \
  1122. KEY(Bool, Editor.WarnOrLargeFileSize); \
  1123. KEY(Bool, Editor.AutoFont); \
  1124. KEY(Bool, Editor.DisableSmoothScroll); \
  1125. ); \
  1126. BLOCK(L"Interface\\QueueView", CANCREATE, \
  1127. KEY(Integer, QueueView.Height); \
  1128. KEY(Integer, QueueView.HeightPixelsPerInch); \
  1129. KEY(String, QueueView.Layout); \
  1130. KEY(Integer, QueueView.Show); \
  1131. KEY(Integer, QueueView.LastHideShow); \
  1132. KEY(Bool, QueueView.ToolBar); \
  1133. KEY(Bool, QueueView.Label); \
  1134. KEY(Bool, QueueView.FileList); \
  1135. KEY(Integer, QueueView.FileListHeight); \
  1136. KEY(Integer, QueueView.FileListHeightPixelsPerInch); \
  1137. ); \
  1138. BLOCK(L"Interface\\Updates", CANCREATE, \
  1139. KEY(Integer, FUpdates.Period); \
  1140. KEY(DateTime, FUpdates.LastCheck); \
  1141. KEY(Integer, FUpdates.HaveResults); \
  1142. KEY(Integer, FUpdates.ShownResults); \
  1143. KEY(Integer, FUpdates.BetaVersions); \
  1144. KEY(Bool, FUpdates.ShowOnStartup); \
  1145. KEY(String, FUpdates.AuthenticationEmail); \
  1146. KEY(String, FUpdates.Mode); \
  1147. KEY(Integer, FUpdates.ConnectionType); \
  1148. KEY(String, FUpdates.ProxyHost); \
  1149. KEY(Integer, FUpdates.ProxyPort); \
  1150. KEY(Integer, FUpdates.Results.ForVersion); \
  1151. KEY(Integer, FUpdates.Results.Version); \
  1152. KEY(String, FUpdates.Results.Message); \
  1153. KEY(Integer, FUpdates.Results.Critical); \
  1154. KEY(String, FUpdates.Results.Release); \
  1155. KEY(Bool, FUpdates.Results.Disabled); \
  1156. KEY(String, FUpdates.Results.Url); \
  1157. KEY(String, FUpdates.Results.UrlButton); \
  1158. KEY(String, FUpdates.Results.NewsUrl); \
  1159. KEYEX(Integer,FUpdates.Results.NewsSize.Width, L"NewsWidth"); \
  1160. KEYEX(Integer,FUpdates.Results.NewsSize.Height, L"NewsHeight"); \
  1161. KEY(String, FUpdates.Results.DownloadUrl); \
  1162. KEY(Int64, FUpdates.Results.DownloadSize); \
  1163. KEY(String, FUpdates.Results.DownloadSha256); \
  1164. KEY(String, FUpdates.Results.AuthenticationError); \
  1165. KEY(Bool, FUpdates.Results.OpenGettingStarted); \
  1166. KEY(String, FUpdates.Results.DownloadingUrl); \
  1167. KEYEX(Integer,FUpdates.Results.TipsSize.Width, L"TipsWidth"); \
  1168. KEYEX(Integer,FUpdates.Results.TipsSize.Height, L"TipsHeight"); \
  1169. KEY(String, FUpdates.Results.TipsUrl); \
  1170. KEY(String, FUpdates.Results.Tips); \
  1171. KEY(Integer, FUpdates.Results.TipsIntervalDays); \
  1172. KEY(Integer, FUpdates.Results.TipsIntervalRuns); \
  1173. KEY(String, FUpdates.DotNetVersion); \
  1174. KEY(String, FUpdates.ConsoleVersion); \
  1175. ); \
  1176. BLOCK(L"Interface\\Explorer", CANCREATE, \
  1177. KEYEX(String, ScpExplorer.ToolbarsLayout, ToolbarsLayoutKey); \
  1178. KEY(String, ScpExplorer.ToolbarsButtons); \
  1179. KEY(String, ScpExplorer.DirViewParams); \
  1180. KEY(String, ScpExplorer.LastLocalTargetDirectory); \
  1181. KEY(Bool, ScpExplorer.SessionsTabs); \
  1182. KEY(Bool, ScpExplorer.StatusBar); \
  1183. KEY(String, ScpExplorer.WindowParams); \
  1184. KEY(Integer, ScpExplorer.ViewStyle); \
  1185. KEY(Bool, ScpExplorer.ShowFullAddress); \
  1186. KEY(Bool, ScpExplorer.DriveView); \
  1187. KEY(Integer, ScpExplorer.DriveViewWidth); \
  1188. KEY(Integer, ScpExplorer.DriveViewWidthPixelsPerInch); \
  1189. ); \
  1190. BLOCK(L"Interface\\Commander", CANCREATE, \
  1191. KEYEX(String, ScpCommander.ToolbarsLayout, ToolbarsLayoutKey); \
  1192. KEY(String, ScpCommander.ToolbarsButtons); \
  1193. KEY(Integer, ScpCommander.CurrentPanel); \
  1194. KEY(Float, ScpCommander.LocalPanelWidth); \
  1195. KEY(Bool, ScpCommander.SwappedPanels); \
  1196. KEY(Bool, ScpCommander.SessionsTabs); \
  1197. KEY(Bool, ScpCommander.StatusBar); \
  1198. KEY(String, ScpCommander.WindowParams); \
  1199. KEYEX(Integer, ScpCommander.NortonLikeMode, L"ExplorerStyleSelection"); \
  1200. KEY(Bool, ScpCommander.PreserveLocalDirectory); \
  1201. KEY(Bool, ScpCommander.CompareByTime); \
  1202. KEY(Bool, ScpCommander.CompareBySize); \
  1203. KEY(Bool, ScpCommander.TreeOnLeft); \
  1204. KEY(Bool, ScpCommander.ExplorerKeyboardShortcuts); \
  1205. KEY(Bool, ScpCommander.SystemContextMenu); \
  1206. ); \
  1207. BLOCK(L"Interface\\Commander\\LocalPanel", CANCREATE, \
  1208. KEY(String, ScpCommander.LocalPanel.DirViewParams); \
  1209. KEY(Integer, ScpCommander.LocalPanel.ViewStyle); \
  1210. KEY(Bool, ScpCommander.LocalPanel.StatusBar); \
  1211. KEY(Bool, ScpCommander.LocalPanel.DriveView); \
  1212. KEY(Integer, ScpCommander.LocalPanel.DriveViewHeight); \
  1213. KEY(Integer, ScpCommander.LocalPanel.DriveViewHeightPixelsPerInch); \
  1214. KEY(Integer, ScpCommander.LocalPanel.DriveViewWidth); \
  1215. KEY(Integer, ScpCommander.LocalPanel.DriveViewWidthPixelsPerInch); \
  1216. KEY(String, ScpCommander.LocalPanel.LastPath); \
  1217. ); \
  1218. BLOCK(L"Interface\\Commander\\RemotePanel", CANCREATE, \
  1219. KEY(String, ScpCommander.RemotePanel.DirViewParams); \
  1220. KEY(Integer, ScpCommander.RemotePanel.ViewStyle); \
  1221. KEY(Bool, ScpCommander.RemotePanel.StatusBar); \
  1222. KEY(Bool, ScpCommander.RemotePanel.DriveView); \
  1223. KEY(Integer, ScpCommander.RemotePanel.DriveViewHeight); \
  1224. KEY(Integer, ScpCommander.RemotePanel.DriveViewHeightPixelsPerInch); \
  1225. KEY(Integer, ScpCommander.RemotePanel.DriveViewWidth); \
  1226. KEY(Integer, ScpCommander.RemotePanel.DriveViewWidthPixelsPerInch); \
  1227. KEY(String, ScpCommander.RemotePanel.LastPath); \
  1228. ); \
  1229. BLOCK(L"Interface\\Commander\\OtherLocalPanel", CANCREATE, \
  1230. KEYEX(String, ScpCommander.OtherLocalPanelDirViewParams, L"DirViewParams"); \
  1231. KEYEX(Integer, ScpCommander.OtherLocalPanelViewStyle, L"ViewStyle"); \
  1232. KEYEX(String, ScpCommander.OtherLocalPanelLastPath, L"LastPath"); \
  1233. ); \
  1234. BLOCK(L"Security", CANCREATE, \
  1235. KEY(Bool, FUseMasterPassword); \
  1236. KEY(String, FMasterPasswordVerifier); \
  1237. );
  1238. //---------------------------------------------------------------------------
  1239. void __fastcall TWinConfiguration::SaveData(THierarchicalStorage * Storage, bool All)
  1240. {
  1241. TCustomWinConfiguration::SaveData(Storage, All);
  1242. // duplicated from core\configuration.cpp
  1243. #define KEYEX(TYPE, VAR, NAME) Storage->Write ## TYPE(NAME, VAR)
  1244. REGCONFIG(true);
  1245. #undef KEYEX
  1246. if (Storage->OpenSubKey(L"Bookmarks", true))
  1247. {
  1248. FBookmarks->Save(Storage, All);
  1249. Storage->CloseSubKey();
  1250. }
  1251. if ((All && !FCustomCommandsDefaults) || FCustomCommandList->Modified)
  1252. {
  1253. FCustomCommandList->Save(Storage);
  1254. if (FCustomCommandList->Count == 0)
  1255. {
  1256. Storage->WriteBool(L"CustomCommandsNone", true);
  1257. }
  1258. else
  1259. {
  1260. Storage->DeleteValue(L"CustomCommandsNone");
  1261. }
  1262. }
  1263. if ((All || FCustomCommandOptionsModified) &&
  1264. Storage->OpenSubKey(L"CustomCommandOptions", true))
  1265. {
  1266. Storage->ClearValues();
  1267. Storage->WriteValues(FCustomCommandOptions.get(), true);
  1268. Storage->CloseSubKey();
  1269. FCustomCommandOptionsModified = false;
  1270. }
  1271. if ((All || FEditorList->Modified) &&
  1272. Storage->OpenSubKeyPath(L"Interface\\Editor", true))
  1273. try
  1274. {
  1275. FEditorList->Save(Storage);
  1276. }
  1277. __finally
  1278. {
  1279. Storage->CloseSubKeyPath();
  1280. }
  1281. }
  1282. //---------------------------------------------------------------------------
  1283. void __fastcall TWinConfiguration::LoadFrom(THierarchicalStorage * Storage)
  1284. {
  1285. FLegacyEditor = new TEditorPreferences();
  1286. try
  1287. {
  1288. FLegacyEditor->LegacyDefaults();
  1289. TCustomWinConfiguration::LoadFrom(Storage);
  1290. // Following needs to be done even if there's no Configuration key in the storage,
  1291. // so it cannot be in LoadData
  1292. int EditorCount = FEditorList->Count;
  1293. if (EditorCount == 0)
  1294. {
  1295. TEditorPreferences * AlternativeEditor = NULL;
  1296. try
  1297. {
  1298. if (FLegacyEditor->Data->Editor == edInternal)
  1299. {
  1300. if (!FLegacyEditor->Data->ExternalEditor.IsEmpty())
  1301. {
  1302. AlternativeEditor = new TEditorPreferences(*FLegacyEditor);
  1303. AlternativeEditor->GetData()->Editor = edExternal;
  1304. FLegacyEditor->GetData()->ExternalEditor = L"";
  1305. }
  1306. }
  1307. else
  1308. {
  1309. if (FLegacyEditor->Data->ExternalEditor.IsEmpty())
  1310. {
  1311. FLegacyEditor->GetData()->Editor = edInternal;
  1312. }
  1313. else
  1314. {
  1315. AlternativeEditor = new TEditorPreferences(*FLegacyEditor);
  1316. AlternativeEditor->GetData()->Editor = edInternal;
  1317. }
  1318. }
  1319. }
  1320. catch(...)
  1321. {
  1322. delete AlternativeEditor;
  1323. throw;
  1324. }
  1325. FEditorList->Add(FLegacyEditor);
  1326. FLegacyEditor = NULL;
  1327. if (AlternativeEditor != NULL)
  1328. {
  1329. FEditorList->Add(AlternativeEditor);
  1330. }
  1331. }
  1332. // Additionally, this needs to be after Locale is loaded
  1333. LoadExtensionTranslations();
  1334. // and this after the ExtensionsDeleted, ExtensionsOrder and ExtensionsShortCuts are loaded
  1335. LoadExtensionList();
  1336. }
  1337. __finally
  1338. {
  1339. delete FLegacyEditor;
  1340. FLegacyEditor = NULL;
  1341. }
  1342. if (FUpdates.ConnectionType == -1)
  1343. {
  1344. FUpdates.ConnectionType = (FUpdates.ProxyHost.IsEmpty() ? ctAuto : ctProxy);
  1345. }
  1346. AddVersionToHistory();
  1347. }
  1348. //---------------------------------------------------------------------------
  1349. void __fastcall TWinConfiguration::DoLoadExtensionList(
  1350. const UnicodeString & Path, const UnicodeString & PathId, TStringList * DeletedExtensions)
  1351. {
  1352. TSearchRecOwned SearchRec;
  1353. int FindAttrs = faReadOnly | faArchive;
  1354. if (FindFirstUnchecked(IncludeTrailingBackslash(Path) + L"*.*", FindAttrs, SearchRec) == 0)
  1355. {
  1356. do
  1357. {
  1358. UnicodeString Id = TCustomCommandType::GetExtensionId(SearchRec.Name);
  1359. if (!Id.IsEmpty())
  1360. {
  1361. Id = IncludeTrailingBackslash(PathId) + Id;
  1362. if (DeletedExtensions->IndexOf(Id) >= 0)
  1363. {
  1364. // reconstruct the list, so that we remove the commands that no longer exists
  1365. AddToList(FExtensionsDeleted, Id, L"|");
  1366. }
  1367. else
  1368. {
  1369. std::unique_ptr<TCustomCommandType> CustomCommand(new TCustomCommandType());
  1370. CustomCommand->Id = Id;
  1371. try
  1372. {
  1373. CustomCommand->LoadExtension(SearchRec.GetFilePath());
  1374. FExtensionList->Add(CustomCommand.release());
  1375. }
  1376. catch (...)
  1377. {
  1378. // skip invalid extension files
  1379. }
  1380. }
  1381. }
  1382. }
  1383. while (FindNextChecked(SearchRec) == 0);
  1384. }
  1385. }
  1386. //---------------------------------------------------------------------------
  1387. void __fastcall ParseExtensionList(TStrings * Strings, UnicodeString S)
  1388. {
  1389. while (!S.IsEmpty())
  1390. {
  1391. UnicodeString Extension = CutToChar(S, L'|', false);
  1392. Strings->Add(Extension);
  1393. }
  1394. }
  1395. //---------------------------------------------------------------------------
  1396. const UnicodeString ExtensionsSubFolder(L"Extensions");
  1397. const UnicodeString ExtensionsCommonPathId(L"common");
  1398. const UnicodeString ExtensionsCommonExtPathId(L"commonext");
  1399. const UnicodeString ExtensionsUserExtPathId(L"userext");
  1400. //---------------------------------------------------------------------------
  1401. static UnicodeString __fastcall ExtractExtensionBaseName(const UnicodeString & FileName)
  1402. {
  1403. UnicodeString S = ExtractFileName(FileName);
  1404. // ExtractFileNameOnly trims the last extension only, we want to trim all extensions
  1405. UnicodeString Result = CutToChar(S, L'.', true);
  1406. return Result;
  1407. }
  1408. //---------------------------------------------------------------------------
  1409. UnicodeString __fastcall TWinConfiguration::GetProvisionaryExtensionId(const UnicodeString & FileName)
  1410. {
  1411. return IncludeTrailingBackslash(ExtensionsUserExtPathId) + ExtractExtensionBaseName(FileName);
  1412. }
  1413. //---------------------------------------------------------------------------
  1414. UnicodeString __fastcall TWinConfiguration::GetUserExtensionsPath()
  1415. {
  1416. return IncludeTrailingBackslash(GetShellFolderPath(CSIDL_APPDATA)) + L"WinSCP\\" + ExtensionsSubFolder;
  1417. }
  1418. //---------------------------------------------------------------------------
  1419. TStrings * __fastcall TWinConfiguration::GetExtensionsPaths()
  1420. {
  1421. std::unique_ptr<TStrings> Result(new TStringList());
  1422. UnicodeString ExeParentPath = ExcludeTrailingBackslash(ExtractFilePath(Application->ExeName));
  1423. Result->Values[ExtensionsCommonPathId] = ExeParentPath;
  1424. UnicodeString CommonExtensions = IncludeTrailingBackslash(ExeParentPath) + ExtensionsSubFolder;
  1425. Result->Values[ExtensionsCommonExtPathId] = CommonExtensions;
  1426. Result->Values[ExtensionsUserExtPathId] = GetUserExtensionsPath();
  1427. return Result.release();
  1428. }
  1429. //---------------------------------------------------------------------------
  1430. UnicodeString __fastcall TWinConfiguration::GetExtensionId(const UnicodeString & ExtensionPath)
  1431. {
  1432. UnicodeString Path = ExcludeTrailingBackslash(ExtractFilePath(ExtensionPath));
  1433. UnicodeString NameId = TCustomCommandType::GetExtensionId(ExtractFileName(ExtensionPath));
  1434. if (!NameId.IsEmpty())
  1435. {
  1436. std::unique_ptr<TStrings> ExtensionsPaths(GetExtensionsPaths());
  1437. for (int Index = 0; Index < ExtensionsPaths->Count; Index++)
  1438. {
  1439. if (IsPathToSameFile(Path, ExtensionsPaths->ValueFromIndex[Index]))
  1440. {
  1441. return IncludeTrailingBackslash(ExtensionsPaths->Names[Index]) + NameId;
  1442. }
  1443. }
  1444. }
  1445. return L"";
  1446. }
  1447. //---------------------------------------------------------------------------
  1448. void __fastcall TWinConfiguration::ReleaseExtensionTranslations()
  1449. {
  1450. for (int Index = 0; Index < FExtensionTranslations->Count; Index++)
  1451. {
  1452. delete FExtensionTranslations->Objects[Index];
  1453. }
  1454. FExtensionTranslations->Clear();
  1455. }
  1456. //---------------------------------------------------------------------------
  1457. void __fastcall TWinConfiguration::LoadExtensionTranslations()
  1458. {
  1459. ReleaseExtensionTranslations();
  1460. FExtensionTranslations.reset(new TStringList());
  1461. int Index = EXTENSION_STRINGS;
  1462. UnicodeString S;
  1463. while (!((S = LoadStr(Index)).IsEmpty()))
  1464. {
  1465. UnicodeString ExtensionName = CutToChar(S, L'.', false);
  1466. UnicodeString Original = CutToChar(S, L'=', false);
  1467. UnicodeString Translation = S;
  1468. int ExtensionIndex = FExtensionTranslations->IndexOf(ExtensionName);
  1469. if (ExtensionIndex < 0)
  1470. {
  1471. ExtensionIndex = FExtensionTranslations->AddObject(ExtensionName, new TStringList());
  1472. }
  1473. TStringList * ExtensionTranslation = DebugNotNull(dynamic_cast<TStringList *>(FExtensionTranslations->Objects[ExtensionIndex]));
  1474. ExtensionTranslation->Values[Original] = Translation;
  1475. Index++;
  1476. }
  1477. }
  1478. //---------------------------------------------------------------------------
  1479. UnicodeString __fastcall TWinConfiguration::UniqueExtensionName(const UnicodeString & ExtensionName, int Counter)
  1480. {
  1481. // See how the digits are removed in the ExtensionStringTranslation
  1482. return ExtensionName + IntToStr(Counter);
  1483. }
  1484. //---------------------------------------------------------------------------
  1485. UnicodeString __fastcall TWinConfiguration::ExtensionStringTranslation(const UnicodeString & ExtensionId, const UnicodeString & S)
  1486. {
  1487. UnicodeString Result = S;
  1488. if (!ExtensionId.IsEmpty())
  1489. {
  1490. UnicodeString ExtensionName = ExtractFileName(ExtensionId);
  1491. int ExtensionIndex;
  1492. bool Retry;
  1493. do
  1494. {
  1495. ExtensionIndex = FExtensionTranslations->IndexOf(ExtensionName);
  1496. // Try without trailing digits, possibly added by UniqueExtensionName
  1497. Retry = (ExtensionIndex < 0) && !ExtensionName.IsEmpty() && IsDigit(*ExtensionName.LastChar());
  1498. if (Retry)
  1499. {
  1500. ExtensionName.SetLength(ExtensionName.Length() - 1);
  1501. }
  1502. }
  1503. while (Retry);
  1504. if (ExtensionIndex >= 0)
  1505. {
  1506. TStrings * ExtensionTranslation = DebugNotNull(dynamic_cast<TStrings *>(FExtensionTranslations->Objects[ExtensionIndex]));
  1507. int StringIndex = ExtensionTranslation->IndexOfName(S);
  1508. if (StringIndex >= 0)
  1509. {
  1510. Result = ExtensionTranslation->ValueFromIndex[StringIndex];
  1511. }
  1512. }
  1513. }
  1514. return Result;
  1515. }
  1516. //---------------------------------------------------------------------------
  1517. void __fastcall TWinConfiguration::LoadExtensionList()
  1518. {
  1519. FExtensionList->Clear();
  1520. std::unique_ptr<TStringList> DeletedExtensions(CreateSortedStringList());
  1521. ParseExtensionList(DeletedExtensions.get(), FExtensionsDeleted);
  1522. // will reconstruct the list in DoLoadExtensionList
  1523. FExtensionsDeleted = L"";
  1524. std::unique_ptr<TStrings> ExtensionsPaths(GetExtensionsPaths());
  1525. for (int Index = 0; Index < ExtensionsPaths->Count; Index++)
  1526. {
  1527. DoLoadExtensionList(ExtensionsPaths->ValueFromIndex[Index], ExtensionsPaths->Names[Index], DeletedExtensions.get());
  1528. }
  1529. std::unique_ptr<TStringList> OrderedExtensions(new TStringList());
  1530. ParseExtensionList(OrderedExtensions.get(), FExtensionsOrder);
  1531. FExtensionList->SortBy(OrderedExtensions.get());
  1532. UnicodeString ShortCuts = FExtensionsShortCuts;
  1533. while (!ShortCuts.IsEmpty())
  1534. {
  1535. UnicodeString S = CutToChar(ShortCuts, L'|', false);
  1536. TShortCut ShortCut = static_cast<TShortCut>(StrToInt(CutToChar(S, L'=', false)));
  1537. for (int Index = 0; Index < FExtensionList->Count; Index++)
  1538. {
  1539. if (FExtensionList->Commands[Index]->Id == S)
  1540. {
  1541. const_cast<TCustomCommandType *>(FExtensionList->Commands[Index])->ShortCut = ShortCut;
  1542. }
  1543. }
  1544. }
  1545. }
  1546. //---------------------------------------------------------------------------
  1547. static UnicodeString KeyName(THierarchicalStorage * Storage, const UnicodeString & Name)
  1548. {
  1549. UnicodeString Result = Name;
  1550. if ((Result == ToolbarsLayoutKey) && !Storage->KeyExists(Result) && Storage->KeyExists(ToolbarsLayoutOldKey))
  1551. {
  1552. Result = ToolbarsLayoutOldKey;
  1553. }
  1554. return Result;
  1555. }
  1556. //---------------------------------------------------------------------------
  1557. void __fastcall TWinConfiguration::LoadData(THierarchicalStorage * Storage)
  1558. {
  1559. TCustomWinConfiguration::LoadData(Storage);
  1560. // duplicated from core\configuration.cpp
  1561. #define KEYEX(TYPE, VAR, NAME) VAR = Storage->Read ## TYPE(KeyName(Storage, NAME), VAR)
  1562. #pragma warn -eas
  1563. REGCONFIG(false);
  1564. #pragma warn +eas
  1565. #undef KEYEX
  1566. // to reflect changes to PanelFont
  1567. UpdateIconFont();
  1568. if (Storage->OpenSubKey(L"Bookmarks", false))
  1569. {
  1570. FBookmarks->Load(Storage);
  1571. Storage->CloseSubKey();
  1572. }
  1573. if (Storage->KeyExists(L"CustomCommands") ||
  1574. Storage->ReadBool(L"CustomCommandsNone", false))
  1575. {
  1576. FCustomCommandList->Load(Storage);
  1577. FCustomCommandsDefaults = false;
  1578. }
  1579. else if (FCustomCommandList->Modified)
  1580. {
  1581. // can this (=reloading of configuration) even happen?
  1582. // if it does, shouldn't we reset default commands?
  1583. DebugFail();
  1584. FCustomCommandList->Clear();
  1585. FCustomCommandsDefaults = false;
  1586. }
  1587. FCustomCommandList->Reset();
  1588. if (Storage->OpenSubKey(L"CustomCommandOptions", false))
  1589. {
  1590. Storage->ReadValues(FCustomCommandOptions.get(), true);
  1591. Storage->CloseSubKey();
  1592. FCustomCommandOptionsModified = false;
  1593. }
  1594. if (Storage->OpenSubKeyPath(L"Interface\\Editor", false))
  1595. try
  1596. {
  1597. FEditorList->Clear();
  1598. FEditorList->Load(Storage);
  1599. }
  1600. __finally
  1601. {
  1602. Storage->CloseSubKeyPath();
  1603. }
  1604. // load legacy editor configuration
  1605. DebugAssert(FLegacyEditor != NULL);
  1606. if (Storage->OpenSubKeyPath(L"Interface\\Editor", false))
  1607. {
  1608. try
  1609. {
  1610. FLegacyEditor->Load(Storage, true);
  1611. }
  1612. __finally
  1613. {
  1614. Storage->CloseSubKeyPath();
  1615. }
  1616. }
  1617. }
  1618. //---------------------------------------------------------------------------
  1619. void __fastcall TWinConfiguration::LoadAdmin(THierarchicalStorage * Storage)
  1620. {
  1621. TCustomWinConfiguration::LoadAdmin(Storage);
  1622. FDisableOpenEdit = Storage->ReadBool(L"DisableOpenEdit", FDisableOpenEdit);
  1623. FDefaultUpdatesPeriod = Storage->ReadInteger(L"DefaultUpdatesPeriod", FDefaultUpdatesPeriod);
  1624. FMachineInstallations = Storage->ReadInteger(L"Installations", 0);
  1625. }
  1626. //---------------------------------------------------------------------------
  1627. void __fastcall TWinConfiguration::CopyData(THierarchicalStorage * Source, THierarchicalStorage * Target)
  1628. {
  1629. TCustomWinConfiguration::CopyData(Source, Target);
  1630. if (CopySubKey(Source, Target, ConfigurationSubKey))
  1631. {
  1632. Target->WriteString(L"JumpList", Source->ReadString(L"JumpList", L""));
  1633. Target->WriteString(L"JumpListWorkspaces", Source->ReadString(L"JumpListWorkspaces", L""));
  1634. Target->CloseSubKey();
  1635. Source->CloseSubKey();
  1636. }
  1637. }
  1638. //---------------------------------------------------------------------------
  1639. void __fastcall TWinConfiguration::ClearTemporaryLoginData()
  1640. {
  1641. if (!FTemporaryKeyFile.IsEmpty())
  1642. {
  1643. DeleteFile(ApiPath(FTemporaryKeyFile));
  1644. FTemporaryKeyFile = L"";
  1645. }
  1646. }
  1647. //---------------------------------------------------------------------------
  1648. void __fastcall TWinConfiguration::AddVersionToHistory()
  1649. {
  1650. int CurrentVersion = CompoundVersion;
  1651. DebugAssert(ZeroBuildNumber(CurrentVersion) == CurrentVersion);
  1652. int From = 1;
  1653. bool CurrentVersionPresent = false;
  1654. while (!CurrentVersionPresent && (From < FVersionHistory.Length()))
  1655. {
  1656. UnicodeString VersionInfo = CopyToChars(FVersionHistory, From, L";", true);
  1657. UnicodeString VersionStr = CutToChar(VersionInfo, L',', true);
  1658. int Version;
  1659. if (TryStrToInt(VersionStr, Version))
  1660. {
  1661. Version = ZeroBuildNumber(Version);
  1662. if (Version == CurrentVersion)
  1663. {
  1664. CurrentVersionPresent = true;
  1665. }
  1666. }
  1667. }
  1668. if (!CurrentVersionPresent)
  1669. {
  1670. UnicodeString CurrentVersionInfo =
  1671. IntToStr(CurrentVersion) + L"," + GetReleaseType();
  1672. AddToList(FVersionHistory, CurrentVersionInfo, L';');
  1673. }
  1674. Usage->Set(L"AnyBetaUsed", AnyBetaInVersionHistory);
  1675. }
  1676. //---------------------------------------------------------------------------
  1677. bool __fastcall TWinConfiguration::DoIsBeta(const UnicodeString & ReleaseType)
  1678. {
  1679. // What about "Development" release type?
  1680. return SameText(ReleaseType, L"beta") || SameText(ReleaseType, L"rc");
  1681. }
  1682. //---------------------------------------------------------------------------
  1683. bool __fastcall TWinConfiguration::GetIsBeta()
  1684. {
  1685. return DoIsBeta(GetReleaseType());
  1686. }
  1687. //---------------------------------------------------------------------------
  1688. bool __fastcall TWinConfiguration::GetAnyBetaInVersionHistory()
  1689. {
  1690. int From = 1;
  1691. bool AnyBeta = false;
  1692. while (!AnyBeta && (From < VersionHistory.Length()))
  1693. {
  1694. UnicodeString VersionInfo = CopyToChars(VersionHistory, From, L";", true);
  1695. CutToChar(VersionInfo, L',', true);
  1696. UnicodeString ReleaseType = CutToChar(VersionInfo, ',', true);
  1697. if (DoIsBeta(ReleaseType))
  1698. {
  1699. AnyBeta = true;
  1700. }
  1701. }
  1702. return AnyBeta;
  1703. }
  1704. //---------------------------------------------------------------------------
  1705. bool __fastcall TWinConfiguration::GetDDExtInstalled()
  1706. {
  1707. if (FDDExtInstalled < 0)
  1708. {
  1709. if (IsWin64())
  1710. {
  1711. // WORKAROUND
  1712. // We cannot load 64-bit COM class into 32-bit process,
  1713. // so we fallback to querying registration keys
  1714. #define CLSID_SIZE 39
  1715. wchar_t ClassID[CLSID_SIZE];
  1716. StringFromGUID2(CLSID_ShellExtension, ClassID, LENOF(ClassID));
  1717. NULL_TERMINATE(ClassID);
  1718. UnicodeString SubKey = UnicodeString(L"CLSID\\") + ClassID;
  1719. HKEY HKey;
  1720. LONG Result = RegOpenKeyEx(HKEY_CLASSES_ROOT, SubKey.c_str(), 0, KEY_READ | KEY_WOW64_64KEY, &HKey);
  1721. if (Result == ERROR_SUCCESS)
  1722. {
  1723. RegCloseKey(HKey);
  1724. FDDExtInstalled = 1;
  1725. }
  1726. else
  1727. {
  1728. FDDExtInstalled = 0;
  1729. }
  1730. }
  1731. else
  1732. {
  1733. void * DragExtRef;
  1734. int CreateResult =
  1735. CoCreateInstance(CLSID_ShellExtension, NULL,
  1736. CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, IID_IUnknown,
  1737. &DragExtRef);
  1738. bool Result = (CreateResult == S_OK);
  1739. FDDExtInstalled = (Result ? 1 : 0);
  1740. if (Result)
  1741. {
  1742. reinterpret_cast<IUnknown *>(DragExtRef)->Release();
  1743. CoFreeUnusedLibraries();
  1744. }
  1745. }
  1746. }
  1747. return (FDDExtInstalled > 0);
  1748. }
  1749. //---------------------------------------------------------------------------
  1750. bool __fastcall TWinConfiguration::IsDDExtRunning()
  1751. {
  1752. bool Result;
  1753. if (!DDExtInstalled)
  1754. {
  1755. Result = false;
  1756. }
  1757. else
  1758. {
  1759. HANDLE H = OpenMutex(SYNCHRONIZE, False, DRAG_EXT_RUNNING_MUTEX);
  1760. Result = (H != NULL);
  1761. CloseHandle(H);
  1762. }
  1763. return Result;
  1764. }
  1765. //---------------------------------------------------------------------------
  1766. bool __fastcall TWinConfiguration::IsDDExtBroken()
  1767. {
  1768. int Build = GetWindowsBuild();
  1769. return (Build >= 17134) && (Build < 17763);
  1770. }
  1771. //---------------------------------------------------------------------------
  1772. RawByteString __fastcall TWinConfiguration::StronglyRecryptPassword(RawByteString Password, UnicodeString Key)
  1773. {
  1774. RawByteString Dummy;
  1775. RawByteString Result;
  1776. if (GetExternalEncryptedPassword(Password, Dummy) ||
  1777. !FUseMasterPassword)
  1778. {
  1779. // already-strongly encrypted
  1780. // or no master password set, so we cannot strongly-encrypt it
  1781. Result = Password;
  1782. }
  1783. else
  1784. {
  1785. UnicodeString PasswordText =
  1786. TCustomWinConfiguration::DecryptPassword(Password, Key);
  1787. if (!PasswordText.IsEmpty())
  1788. {
  1789. // Can be not set for instance, when editing=>saving site with no prior password.
  1790. // Though it should not actually happen, as we call AskForMasterPasswordIfNotSetAndNeededToPersistSessionData in DoSaveSession.
  1791. AskForMasterPasswordIfNotSet();
  1792. Password = ScramblePassword(PasswordText);
  1793. AES256EncyptWithMAC(Password, FPlainMasterPasswordEncrypt, Result);
  1794. Result = SetExternalEncryptedPassword(Result);
  1795. }
  1796. }
  1797. return Result;
  1798. }
  1799. //---------------------------------------------------------------------------
  1800. UnicodeString __fastcall TWinConfiguration::DecryptPassword(RawByteString Password, UnicodeString Key)
  1801. {
  1802. UnicodeString Result;
  1803. RawByteString Buf;
  1804. if (GetExternalEncryptedPassword(Password, Buf))
  1805. {
  1806. if (FDontDecryptPasswords == 0)
  1807. {
  1808. // As opposite to AskForMasterPasswordIfNotSet, we test here
  1809. // for decrypt password. This is important while recrypting password,
  1810. // when clearing master password, when encrypt password is already empty.
  1811. if (FPlainMasterPasswordDecrypt.IsEmpty())
  1812. {
  1813. AskForMasterPassword();
  1814. }
  1815. if (!AES256DecryptWithMAC(Buf, FPlainMasterPasswordDecrypt, Buf) ||
  1816. !UnscramblePassword(Buf, Result))
  1817. {
  1818. throw Exception(LoadStr(DECRYPT_PASSWORD_ERROR));
  1819. }
  1820. }
  1821. else
  1822. {
  1823. Abort();
  1824. }
  1825. }
  1826. else
  1827. {
  1828. Result = TCustomWinConfiguration::DecryptPassword(Password, Key);
  1829. }
  1830. return Result;
  1831. }
  1832. //---------------------------------------------------------------------------
  1833. void __fastcall TWinConfiguration::SetMasterPassword(UnicodeString value)
  1834. {
  1835. // just stores the plain-text version of the password
  1836. // we can get here even if master password is off,
  1837. // when we encounter stray encrypted password (e.g. manually imported site),
  1838. // make sure we do not set encrypt password to avoid starting encrypting
  1839. // new passwords
  1840. // (this is bit of edge case, not really well tested)
  1841. if (!FUseMasterPassword)
  1842. {
  1843. FPlainMasterPasswordDecrypt = value;
  1844. }
  1845. else if (DebugAlwaysTrue(FUseMasterPassword) &&
  1846. DebugAlwaysTrue(ValidateMasterPassword(value)))
  1847. {
  1848. FPlainMasterPasswordEncrypt = value;
  1849. FPlainMasterPasswordDecrypt = value;
  1850. }
  1851. }
  1852. //---------------------------------------------------------------------------
  1853. void __fastcall TWinConfiguration::ChangeMasterPassword(
  1854. UnicodeString value, TStrings * RecryptPasswordErrors)
  1855. {
  1856. RawByteString Verifier;
  1857. AES256CreateVerifier(value, Verifier);
  1858. FMasterPasswordVerifier = BytesToHex(Verifier);
  1859. FPlainMasterPasswordEncrypt = value;
  1860. FUseMasterPassword = true;
  1861. try
  1862. {
  1863. RecryptPasswords(RecryptPasswordErrors);
  1864. }
  1865. __finally
  1866. {
  1867. FPlainMasterPasswordDecrypt = value;
  1868. }
  1869. }
  1870. //---------------------------------------------------------------------------
  1871. bool __fastcall TWinConfiguration::ValidateMasterPassword(UnicodeString value)
  1872. {
  1873. DebugAssert(UseMasterPassword);
  1874. DebugAssert(!FMasterPasswordVerifier.IsEmpty());
  1875. bool Result = AES256Verify(value, HexToBytes(FMasterPasswordVerifier));
  1876. return Result;
  1877. }
  1878. //---------------------------------------------------------------------------
  1879. void __fastcall TWinConfiguration::ClearMasterPassword(TStrings * RecryptPasswordErrors)
  1880. {
  1881. FMasterPasswordVerifier = L"";
  1882. FUseMasterPassword = false;
  1883. Shred(FPlainMasterPasswordEncrypt);
  1884. try
  1885. {
  1886. RecryptPasswords(RecryptPasswordErrors);
  1887. }
  1888. __finally
  1889. {
  1890. Shred(FPlainMasterPasswordDecrypt);
  1891. }
  1892. }
  1893. //---------------------------------------------------------------------------
  1894. void __fastcall TWinConfiguration::AskForMasterPassword()
  1895. {
  1896. if (FMasterPasswordSession > 0)
  1897. {
  1898. if (FMasterPasswordSessionAsked)
  1899. {
  1900. Abort();
  1901. }
  1902. // set before call to OnMasterPasswordPrompt as it may abort
  1903. FMasterPasswordSessionAsked = true;
  1904. }
  1905. if (FOnMasterPasswordPrompt == NULL)
  1906. {
  1907. throw Exception(L"Master password handler not set");
  1908. }
  1909. else
  1910. {
  1911. FOnMasterPasswordPrompt();
  1912. DebugAssert(!FPlainMasterPasswordDecrypt.IsEmpty());
  1913. }
  1914. }
  1915. //---------------------------------------------------------------------------
  1916. void __fastcall TWinConfiguration::AskForMasterPasswordIfNotSet()
  1917. {
  1918. if (FPlainMasterPasswordEncrypt.IsEmpty())
  1919. {
  1920. AskForMasterPassword();
  1921. }
  1922. }
  1923. //---------------------------------------------------------------------------
  1924. void __fastcall TWinConfiguration::BeginMasterPasswordSession()
  1925. {
  1926. // We do not expect nesting
  1927. DebugAssert(FMasterPasswordSession == 0);
  1928. DebugAssert(!FMasterPasswordSessionAsked || (FMasterPasswordSession > 0));
  1929. // This should better be thread-specific
  1930. FMasterPasswordSession++;
  1931. }
  1932. //---------------------------------------------------------------------------
  1933. void __fastcall TWinConfiguration::EndMasterPasswordSession()
  1934. {
  1935. if (DebugAlwaysTrue(FMasterPasswordSession > 0))
  1936. {
  1937. FMasterPasswordSession--;
  1938. }
  1939. FMasterPasswordSessionAsked = false;
  1940. }
  1941. //---------------------------------------------------------------------------
  1942. void __fastcall TWinConfiguration::SetDDDisableMove(bool value)
  1943. {
  1944. SET_CONFIG_PROPERTY(DDDisableMove);
  1945. }
  1946. //---------------------------------------------------------------------------
  1947. void __fastcall TWinConfiguration::SetDDTransferConfirmation(TAutoSwitch value)
  1948. {
  1949. SET_CONFIG_PROPERTY(DDTransferConfirmation);
  1950. }
  1951. //---------------------------------------------------------------------------
  1952. void __fastcall TWinConfiguration::SetDDTemporaryDirectory(UnicodeString value)
  1953. {
  1954. SET_CONFIG_PROPERTY(DDTemporaryDirectory);
  1955. }
  1956. //---------------------------------------------------------------------------
  1957. void __fastcall TWinConfiguration::SetDDDrives(UnicodeString value)
  1958. {
  1959. SET_CONFIG_PROPERTY(DDDrives);
  1960. }
  1961. //---------------------------------------------------------------------------
  1962. void __fastcall TWinConfiguration::SetDDFakeFile(bool value)
  1963. {
  1964. SET_CONFIG_PROPERTY(DDFakeFile);
  1965. }
  1966. //---------------------------------------------------------------------------
  1967. void __fastcall TWinConfiguration::SetDDExtTimeout(int value)
  1968. {
  1969. SET_CONFIG_PROPERTY(DDExtTimeout);
  1970. }
  1971. //---------------------------------------------------------------------------
  1972. void __fastcall TWinConfiguration::SetDDWarnLackOfTempSpace(bool value)
  1973. {
  1974. SET_CONFIG_PROPERTY(DDWarnLackOfTempSpace);
  1975. }
  1976. //---------------------------------------------------------------------------
  1977. void __fastcall TWinConfiguration::SetDDWarnLackOfTempSpaceRatio(double value)
  1978. {
  1979. SET_CONFIG_PROPERTY(DDWarnLackOfTempSpaceRatio);
  1980. }
  1981. //---------------------------------------------------------------------------
  1982. void __fastcall TWinConfiguration::SetScpExplorer(TScpExplorerConfiguration value)
  1983. {
  1984. SET_CONFIG_PROPERTY(ScpExplorer);
  1985. }
  1986. //---------------------------------------------------------------------------
  1987. void __fastcall TWinConfiguration::SetScpCommander(TScpCommanderConfiguration value)
  1988. {
  1989. SET_CONFIG_PROPERTY(ScpCommander);
  1990. }
  1991. //---------------------------------------------------------------------------
  1992. void __fastcall TWinConfiguration::SetEditor(TEditorConfiguration value)
  1993. {
  1994. SET_CONFIG_PROPERTY(Editor);
  1995. }
  1996. //---------------------------------------------------------------------------
  1997. void __fastcall TWinConfiguration::SetQueueView(TQueueViewConfiguration value)
  1998. {
  1999. SET_CONFIG_PROPERTY(QueueView);
  2000. }
  2001. //---------------------------------------------------------------------------
  2002. void __fastcall TWinConfiguration::SetEnableQueueByDefault(bool value)
  2003. {
  2004. SET_CONFIG_PROPERTY(EnableQueueByDefault);
  2005. }
  2006. //---------------------------------------------------------------------------
  2007. TUpdatesConfiguration __fastcall TWinConfiguration::GetUpdates()
  2008. {
  2009. TUpdatesConfiguration Result;
  2010. {
  2011. TGuard Guard(FCriticalSection);
  2012. Result = FUpdates;
  2013. }
  2014. return Result;
  2015. }
  2016. //---------------------------------------------------------------------------
  2017. void __fastcall TWinConfiguration::SetUpdates(TUpdatesConfiguration value)
  2018. {
  2019. TGuard Guard(FCriticalSection);
  2020. // do not use SET_CONFIG_PROPERTY to avoid OnChange handler call (not synchronized)
  2021. FUpdates = value;
  2022. }
  2023. //---------------------------------------------------------------------------
  2024. void __fastcall TWinConfiguration::SetVersionHistory(UnicodeString value)
  2025. {
  2026. SET_CONFIG_PROPERTY(VersionHistory);
  2027. }
  2028. //---------------------------------------------------------------------------
  2029. void __fastcall TWinConfiguration::SetDeleteToRecycleBin(bool value)
  2030. {
  2031. SET_CONFIG_PROPERTY(DeleteToRecycleBin);
  2032. }
  2033. //---------------------------------------------------------------------------
  2034. void __fastcall TWinConfiguration::SetSelectDirectories(bool value)
  2035. {
  2036. SET_CONFIG_PROPERTY(SelectDirectories);
  2037. }
  2038. //---------------------------------------------------------------------------
  2039. void __fastcall TWinConfiguration::SetShowHiddenFiles(bool value)
  2040. {
  2041. SET_CONFIG_PROPERTY(ShowHiddenFiles);
  2042. }
  2043. //---------------------------------------------------------------------------
  2044. void __fastcall TWinConfiguration::SetFormatSizeBytes(TFormatBytesStyle value)
  2045. {
  2046. SET_CONFIG_PROPERTY(FormatSizeBytes);
  2047. }
  2048. //---------------------------------------------------------------------------
  2049. void __fastcall TWinConfiguration::SetPanelSearch(TIncrementalSearch value)
  2050. {
  2051. SET_CONFIG_PROPERTY(PanelSearch);
  2052. }
  2053. //---------------------------------------------------------------------------
  2054. void __fastcall TWinConfiguration::SetShowInaccesibleDirectories(bool value)
  2055. {
  2056. SET_CONFIG_PROPERTY(ShowInaccesibleDirectories);
  2057. }
  2058. //---------------------------------------------------------------------------
  2059. void __fastcall TWinConfiguration::SetConfirmTransferring(bool value)
  2060. {
  2061. SET_CONFIG_PROPERTY(ConfirmTransferring);
  2062. }
  2063. //---------------------------------------------------------------------------
  2064. void __fastcall TWinConfiguration::SetConfirmDeleting(bool value)
  2065. {
  2066. SET_CONFIG_PROPERTY(ConfirmDeleting);
  2067. }
  2068. //---------------------------------------------------------------------------
  2069. void __fastcall TWinConfiguration::SetConfirmRecycling(bool value)
  2070. {
  2071. SET_CONFIG_PROPERTY(ConfirmRecycling);
  2072. }
  2073. //---------------------------------------------------------------------------
  2074. void __fastcall TWinConfiguration::SetUseLocationProfiles(bool value)
  2075. {
  2076. SET_CONFIG_PROPERTY(UseLocationProfiles);
  2077. }
  2078. //---------------------------------------------------------------------------
  2079. void __fastcall TWinConfiguration::SetUseSharedBookmarks(bool value)
  2080. {
  2081. SET_CONFIG_PROPERTY(UseSharedBookmarks);
  2082. }
  2083. //---------------------------------------------------------------------------
  2084. void __fastcall TWinConfiguration::SetConfirmClosingSession(bool value)
  2085. {
  2086. SET_CONFIG_PROPERTY(ConfirmClosingSession);
  2087. }
  2088. //---------------------------------------------------------------------------
  2089. void __fastcall TWinConfiguration::SetDoubleClickAction(TDoubleClickAction value)
  2090. {
  2091. SET_CONFIG_PROPERTY(DoubleClickAction);
  2092. }
  2093. //---------------------------------------------------------------------------
  2094. void __fastcall TWinConfiguration::SetCopyOnDoubleClickConfirmation(bool value)
  2095. {
  2096. SET_CONFIG_PROPERTY(CopyOnDoubleClickConfirmation);
  2097. }
  2098. //---------------------------------------------------------------------------
  2099. void __fastcall TWinConfiguration::SetAlwaysRespectDoubleClickAction(bool value)
  2100. {
  2101. SET_CONFIG_PROPERTY(AlwaysRespectDoubleClickAction);
  2102. }
  2103. //---------------------------------------------------------------------------
  2104. void __fastcall TWinConfiguration::SetDimmHiddenFiles(bool value)
  2105. {
  2106. SET_CONFIG_PROPERTY(DimmHiddenFiles);
  2107. }
  2108. //---------------------------------------------------------------------------
  2109. void __fastcall TWinConfiguration::SetRenameWholeName(bool value)
  2110. {
  2111. SET_CONFIG_PROPERTY(RenameWholeName);
  2112. }
  2113. //---------------------------------------------------------------------------
  2114. void __fastcall TWinConfiguration::SetAutoStartSession(UnicodeString value)
  2115. {
  2116. SET_CONFIG_PROPERTY(AutoStartSession);
  2117. }
  2118. //---------------------------------------------------------------------------
  2119. void __fastcall TWinConfiguration::SetExpertMode(bool value)
  2120. {
  2121. SET_CONFIG_PROPERTY(ExpertMode);
  2122. }
  2123. //---------------------------------------------------------------------------
  2124. void __fastcall TWinConfiguration::SetDefaultDirIsHome(bool value)
  2125. {
  2126. SET_CONFIG_PROPERTY(DefaultDirIsHome);
  2127. }
  2128. //---------------------------------------------------------------------------
  2129. void __fastcall TWinConfiguration::SetTemporaryDirectoryAppendSession(bool value)
  2130. {
  2131. SET_CONFIG_PROPERTY(TemporaryDirectoryAppendSession);
  2132. }
  2133. //---------------------------------------------------------------------------
  2134. void __fastcall TWinConfiguration::SetTemporaryDirectoryAppendPath(bool value)
  2135. {
  2136. SET_CONFIG_PROPERTY(TemporaryDirectoryAppendPath);
  2137. }
  2138. //---------------------------------------------------------------------------
  2139. void __fastcall TWinConfiguration::SetTemporaryDirectoryDeterministic(bool value)
  2140. {
  2141. SET_CONFIG_PROPERTY(TemporaryDirectoryDeterministic);
  2142. }
  2143. //---------------------------------------------------------------------------
  2144. void __fastcall TWinConfiguration::SetTemporaryDirectoryCleanup(bool value)
  2145. {
  2146. SET_CONFIG_PROPERTY(TemporaryDirectoryCleanup);
  2147. }
  2148. //---------------------------------------------------------------------------
  2149. void __fastcall TWinConfiguration::SetConfirmTemporaryDirectoryCleanup(bool value)
  2150. {
  2151. SET_CONFIG_PROPERTY(ConfirmTemporaryDirectoryCleanup);
  2152. }
  2153. //---------------------------------------------------------------------------
  2154. void __fastcall TWinConfiguration::SetPreservePanelState(bool value)
  2155. {
  2156. SET_CONFIG_PROPERTY(PreservePanelState);
  2157. }
  2158. //---------------------------------------------------------------------------
  2159. void __fastcall TWinConfiguration::SetDarkTheme(TAutoSwitch value)
  2160. {
  2161. SET_CONFIG_PROPERTY_EX(DarkTheme, ConfigureInterface());
  2162. }
  2163. //---------------------------------------------------------------------------
  2164. static int __fastcall SysDarkTheme(HKEY RootKey)
  2165. {
  2166. std::unique_ptr<TRegistry> Registry(new TRegistry());
  2167. Registry->RootKey = RootKey;
  2168. UnicodeString ThemesPersonalizeKey = L"Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize";
  2169. UnicodeString AppsUseLightThemeValue = L"AppsUseLightTheme";
  2170. int Result = -1;
  2171. if (Registry->OpenKeyReadOnly(ThemesPersonalizeKey) &&
  2172. Registry->ValueExists(AppsUseLightThemeValue))
  2173. {
  2174. Result = Registry->ReadBool(AppsUseLightThemeValue) ? 0 : 1;
  2175. }
  2176. return Result;
  2177. }
  2178. //---------------------------------------------------------------------------
  2179. bool __fastcall TWinConfiguration::UseDarkTheme()
  2180. {
  2181. switch (WinConfiguration->DarkTheme)
  2182. {
  2183. case asOn:
  2184. return true;
  2185. case asOff:
  2186. return false;
  2187. default:
  2188. return (GetSysDarkTheme() > 0);
  2189. }
  2190. }
  2191. //---------------------------------------------------------------------------
  2192. void __fastcall TWinConfiguration::SetLastStoredSession(UnicodeString value)
  2193. {
  2194. SET_CONFIG_PROPERTY(LastStoredSession);
  2195. }
  2196. //---------------------------------------------------------------------------
  2197. void __fastcall TWinConfiguration::SetAutoSaveWorkspace(bool value)
  2198. {
  2199. SET_CONFIG_PROPERTY(AutoSaveWorkspace);
  2200. }
  2201. //---------------------------------------------------------------------------
  2202. void __fastcall TWinConfiguration::SetAutoSaveWorkspacePasswords(bool value)
  2203. {
  2204. SET_CONFIG_PROPERTY(AutoSaveWorkspacePasswords);
  2205. }
  2206. //---------------------------------------------------------------------------
  2207. void __fastcall TWinConfiguration::SetAutoWorkspace(UnicodeString value)
  2208. {
  2209. SET_CONFIG_PROPERTY(AutoWorkspace);
  2210. }
  2211. //---------------------------------------------------------------------------
  2212. void __fastcall TWinConfiguration::SetPathInCaption(TPathInCaption value)
  2213. {
  2214. SET_CONFIG_PROPERTY(PathInCaption);
  2215. }
  2216. //---------------------------------------------------------------------------
  2217. void __fastcall TWinConfiguration::SetMinimizeToTray(bool value)
  2218. {
  2219. SET_CONFIG_PROPERTY(MinimizeToTray);
  2220. }
  2221. //---------------------------------------------------------------------------
  2222. void __fastcall TWinConfiguration::SetBalloonNotifications(bool value)
  2223. {
  2224. SET_CONFIG_PROPERTY(BalloonNotifications);
  2225. }
  2226. //---------------------------------------------------------------------------
  2227. void __fastcall TWinConfiguration::SetNotificationsTimeout(unsigned int value)
  2228. {
  2229. SET_CONFIG_PROPERTY(NotificationsTimeout);
  2230. }
  2231. //---------------------------------------------------------------------------
  2232. void __fastcall TWinConfiguration::SetNotificationsStickTime(unsigned int value)
  2233. {
  2234. SET_CONFIG_PROPERTY(NotificationsStickTime);
  2235. }
  2236. //---------------------------------------------------------------------------
  2237. void __fastcall TWinConfiguration::SetCopyParamAutoSelectNotice(bool value)
  2238. {
  2239. SET_CONFIG_PROPERTY(CopyParamAutoSelectNotice);
  2240. }
  2241. //---------------------------------------------------------------------------
  2242. void __fastcall TWinConfiguration::SetLockToolbars(bool value)
  2243. {
  2244. SET_CONFIG_PROPERTY(LockToolbars);
  2245. }
  2246. //---------------------------------------------------------------------------
  2247. void __fastcall TWinConfiguration::SetSelectiveToolbarText(bool value)
  2248. {
  2249. SET_CONFIG_PROPERTY(SelectiveToolbarText);
  2250. }
  2251. //---------------------------------------------------------------------------
  2252. void TWinConfiguration::SetLargerToolbar(int value)
  2253. {
  2254. SET_CONFIG_PROPERTY(LargerToolbar);
  2255. }
  2256. //---------------------------------------------------------------------------
  2257. void __fastcall TWinConfiguration::SetAutoOpenInPutty(bool value)
  2258. {
  2259. SET_CONFIG_PROPERTY(AutoOpenInPutty);
  2260. }
  2261. //---------------------------------------------------------------------------
  2262. void __fastcall TWinConfiguration::SetRefreshRemotePanel(bool value)
  2263. {
  2264. SET_CONFIG_PROPERTY(RefreshRemotePanel);
  2265. }
  2266. //---------------------------------------------------------------------------
  2267. void __fastcall TWinConfiguration::SetRefreshRemotePanelInterval(TDateTime value)
  2268. {
  2269. SET_CONFIG_PROPERTY(RefreshRemotePanelInterval);
  2270. }
  2271. //---------------------------------------------------------------------------
  2272. void __fastcall TWinConfiguration::UpdateIconFont()
  2273. {
  2274. UpdateDesktopFont();
  2275. }
  2276. //---------------------------------------------------------------------------
  2277. void __fastcall TWinConfiguration::SetPanelFont(const TFontConfiguration & value)
  2278. {
  2279. SET_CONFIG_PROPERTY_EX(PanelFont, UpdateIconFont());
  2280. }
  2281. //---------------------------------------------------------------------------
  2282. void __fastcall TWinConfiguration::SetNaturalOrderNumericalSorting(bool value)
  2283. {
  2284. SET_CONFIG_PROPERTY(NaturalOrderNumericalSorting);
  2285. }
  2286. //---------------------------------------------------------------------------
  2287. void __fastcall TWinConfiguration::SetAlwaysSortDirectoriesByName(bool value)
  2288. {
  2289. SET_CONFIG_PROPERTY(AlwaysSortDirectoriesByName);
  2290. }
  2291. //---------------------------------------------------------------------------
  2292. void __fastcall TWinConfiguration::SetFullRowSelect(bool value)
  2293. {
  2294. SET_CONFIG_PROPERTY(FullRowSelect);
  2295. }
  2296. //---------------------------------------------------------------------------
  2297. void __fastcall TWinConfiguration::SetOfferedEditorAutoConfig(bool value)
  2298. {
  2299. SET_CONFIG_PROPERTY(OfferedEditorAutoConfig);
  2300. }
  2301. //---------------------------------------------------------------------------
  2302. void __fastcall TWinConfiguration::SetOpenedStoredSessionFolders(UnicodeString value)
  2303. {
  2304. SET_CONFIG_PROPERTY(OpenedStoredSessionFolders);
  2305. }
  2306. //---------------------------------------------------------------------------
  2307. void __fastcall TWinConfiguration::SetAutoImportedFromPuttyOrFilezilla(bool value)
  2308. {
  2309. SET_CONFIG_PROPERTY(AutoImportedFromPuttyOrFilezilla);
  2310. }
  2311. //---------------------------------------------------------------------------
  2312. void __fastcall TWinConfiguration::SetGenerateUrlComponents(int value)
  2313. {
  2314. SET_CONFIG_PROPERTY(GenerateUrlComponents);
  2315. }
  2316. //---------------------------------------------------------------------------
  2317. void __fastcall TWinConfiguration::SetGenerateUrlCodeTarget(TGenerateUrlCodeTarget value)
  2318. {
  2319. SET_CONFIG_PROPERTY(GenerateUrlCodeTarget);
  2320. }
  2321. //---------------------------------------------------------------------------
  2322. void __fastcall TWinConfiguration::SetGenerateUrlScriptFormat(TScriptFormat value)
  2323. {
  2324. SET_CONFIG_PROPERTY(GenerateUrlScriptFormat);
  2325. }
  2326. //---------------------------------------------------------------------------
  2327. void __fastcall TWinConfiguration::SetGenerateUrlAssemblyLanguage(TAssemblyLanguage value)
  2328. {
  2329. SET_CONFIG_PROPERTY(GenerateUrlAssemblyLanguage);
  2330. }
  2331. //---------------------------------------------------------------------------
  2332. void __fastcall TWinConfiguration::SetExternalSessionInExistingInstance(bool value)
  2333. {
  2334. SET_CONFIG_PROPERTY(ExternalSessionInExistingInstance);
  2335. }
  2336. //---------------------------------------------------------------------------
  2337. void __fastcall TWinConfiguration::SetShowLoginWhenNoSession(bool value)
  2338. {
  2339. SET_CONFIG_PROPERTY(ShowLoginWhenNoSession);
  2340. }
  2341. //---------------------------------------------------------------------------
  2342. void __fastcall TWinConfiguration::SetKeepOpenWhenNoSession(bool value)
  2343. {
  2344. SET_CONFIG_PROPERTY(KeepOpenWhenNoSession);
  2345. }
  2346. //---------------------------------------------------------------------------
  2347. void __fastcall TWinConfiguration::SetDefaultToNewRemoteTab(bool value)
  2348. {
  2349. SET_CONFIG_PROPERTY(DefaultToNewRemoteTab);
  2350. }
  2351. //---------------------------------------------------------------------------
  2352. void __fastcall TWinConfiguration::SetLocalIconsByExt(bool value)
  2353. {
  2354. SET_CONFIG_PROPERTY(LocalIconsByExt);
  2355. }
  2356. //---------------------------------------------------------------------------
  2357. void __fastcall TWinConfiguration::SetFlashTaskbar(bool value)
  2358. {
  2359. SET_CONFIG_PROPERTY(FlashTaskbar);
  2360. }
  2361. //---------------------------------------------------------------------------
  2362. void __fastcall TWinConfiguration::SetBidiModeOverride(TLocaleFlagOverride value)
  2363. {
  2364. SET_CONFIG_PROPERTY(BidiModeOverride);
  2365. }
  2366. //---------------------------------------------------------------------------
  2367. void __fastcall TWinConfiguration::SetFlipChildrenOverride(TLocaleFlagOverride value)
  2368. {
  2369. SET_CONFIG_PROPERTY(FlipChildrenOverride);
  2370. }
  2371. //---------------------------------------------------------------------------
  2372. void __fastcall TWinConfiguration::SetShowTips(bool value)
  2373. {
  2374. SET_CONFIG_PROPERTY(ShowTips);
  2375. }
  2376. //---------------------------------------------------------------------------
  2377. void __fastcall TWinConfiguration::SetTipsSeen(UnicodeString value)
  2378. {
  2379. SET_CONFIG_PROPERTY(TipsSeen);
  2380. }
  2381. //---------------------------------------------------------------------------
  2382. void __fastcall TWinConfiguration::SetTipsShown(TDateTime value)
  2383. {
  2384. SET_CONFIG_PROPERTY(TipsShown);
  2385. }
  2386. //---------------------------------------------------------------------------
  2387. void __fastcall TWinConfiguration::SetFileColors(UnicodeString value)
  2388. {
  2389. SET_CONFIG_PROPERTY(FileColors);
  2390. }
  2391. //---------------------------------------------------------------------------
  2392. void __fastcall TWinConfiguration::SetRunsSinceLastTip(int value)
  2393. {
  2394. SET_CONFIG_PROPERTY(RunsSinceLastTip);
  2395. }
  2396. //---------------------------------------------------------------------------
  2397. bool __fastcall TWinConfiguration::GetHonorDrivePolicy()
  2398. {
  2399. return DriveInfo->HonorDrivePolicy;
  2400. }
  2401. //---------------------------------------------------------------------------
  2402. void __fastcall TWinConfiguration::SetHonorDrivePolicy(bool value)
  2403. {
  2404. if (HonorDrivePolicy != value)
  2405. {
  2406. DriveInfo->HonorDrivePolicy = value;
  2407. Changed();
  2408. }
  2409. }
  2410. //---------------------------------------------------------------------------
  2411. bool __fastcall TWinConfiguration::GetUseABDrives()
  2412. {
  2413. return DriveInfo->UseABDrives;
  2414. }
  2415. //---------------------------------------------------------------------------
  2416. void __fastcall TWinConfiguration::SetUseABDrives(bool value)
  2417. {
  2418. if (UseABDrives != value)
  2419. {
  2420. DriveInfo->UseABDrives = value;
  2421. Changed();
  2422. }
  2423. }
  2424. //---------------------------------------------------------------------------
  2425. void __fastcall TWinConfiguration::SetCustomCommandList(TCustomCommandList * value)
  2426. {
  2427. DebugAssert(FCustomCommandList);
  2428. if (!FCustomCommandList->Equals(value))
  2429. {
  2430. FCustomCommandList->Assign(value);
  2431. FCustomCommandsDefaults = false;
  2432. }
  2433. }
  2434. //---------------------------------------------------------------------------
  2435. void __fastcall TWinConfiguration::SetExtensionList(TCustomCommandList * value)
  2436. {
  2437. if (!ExtensionList->Equals(value))
  2438. {
  2439. std::unique_ptr<TStringList> DeletedExtensions(CreateSortedStringList());
  2440. ParseExtensionList(DeletedExtensions.get(), FExtensionsDeleted);
  2441. for (int Index = 0; Index < ExtensionList->Count; Index++)
  2442. {
  2443. const TCustomCommandType * CustomCommand = ExtensionList->Commands[Index];
  2444. int Index = value->FindIndexByFileName(CustomCommand->FileName);
  2445. if (Index < 0)
  2446. {
  2447. if (FileExists(CustomCommand->FileName) &&
  2448. !DeleteFile(CustomCommand->FileName))
  2449. {
  2450. DeletedExtensions->Add(CustomCommand->Id);
  2451. }
  2452. }
  2453. }
  2454. FExtensionsOrder = L"";
  2455. for (int Index = 0; Index < value->Count; Index++)
  2456. {
  2457. const TCustomCommandType * CustomCommand = value->Commands[Index];
  2458. AddToList(FExtensionsOrder, CustomCommand->Id, L"|");
  2459. int DeletedIndex = DeletedExtensions->IndexOf(CustomCommand->Id);
  2460. if (DeletedIndex >= 0)
  2461. {
  2462. DeletedExtensions->Delete(DeletedIndex);
  2463. }
  2464. }
  2465. FExtensionList->Assign(value);
  2466. FExtensionsDeleted = L"";
  2467. for (int Index = 0; Index < DeletedExtensions->Count; Index++)
  2468. {
  2469. AddToList(FExtensionsDeleted, DeletedExtensions->Strings[Index], L"|");
  2470. }
  2471. FExtensionsShortCuts = L"";
  2472. for (int Index = 0; Index < value->Count; Index++)
  2473. {
  2474. const TCustomCommandType * Extension = value->Commands[Index];
  2475. if (Extension->HasCustomShortCut())
  2476. {
  2477. AddToList(FExtensionsShortCuts, FORMAT(L"%d=%s", (Extension->ShortCut, Extension->Id)), L"|");
  2478. }
  2479. }
  2480. Changed();
  2481. }
  2482. }
  2483. //---------------------------------------------------------------------------
  2484. void __fastcall TWinConfiguration::CustomCommandShortCuts(TShortCuts & ShortCuts) const
  2485. {
  2486. CustomCommandList->ShortCuts(ShortCuts);
  2487. ExtensionList->ShortCuts(ShortCuts);
  2488. }
  2489. //---------------------------------------------------------------------------
  2490. void __fastcall TWinConfiguration::SetBookmarks(UnicodeString Key,
  2491. TBookmarkList * value)
  2492. {
  2493. FBookmarks->Bookmarks[Key] = value;
  2494. Changed();
  2495. }
  2496. //---------------------------------------------------------------------------
  2497. TBookmarkList * __fastcall TWinConfiguration::GetBookmarks(UnicodeString Key)
  2498. {
  2499. return FBookmarks->Bookmarks[Key];
  2500. }
  2501. //---------------------------------------------------------------------------
  2502. void __fastcall TWinConfiguration::SetSharedBookmarks(TBookmarkList * value)
  2503. {
  2504. FBookmarks->SharedBookmarks = value;
  2505. Changed();
  2506. }
  2507. //---------------------------------------------------------------------------
  2508. TBookmarkList * __fastcall TWinConfiguration::GetSharedBookmarks()
  2509. {
  2510. return FBookmarks->SharedBookmarks;
  2511. }
  2512. //---------------------------------------------------------------------------
  2513. UnicodeString __fastcall TWinConfiguration::GetDefaultKeyFile()
  2514. {
  2515. return (!FDefaultKeyFile.IsEmpty() ? FDefaultKeyFile : FTemporaryKeyFile);
  2516. }
  2517. //---------------------------------------------------------------------------
  2518. void __fastcall TWinConfiguration::SetLastMonitor(int value)
  2519. {
  2520. ::SetLastMonitor(value);
  2521. }
  2522. //---------------------------------------------------------------------------
  2523. int __fastcall TWinConfiguration::GetLastMonitor()
  2524. {
  2525. return ::GetLastMonitor();
  2526. }
  2527. //---------------------------------------------------------------------------
  2528. UnicodeString __fastcall TWinConfiguration::ExpandedTemporaryDirectory()
  2529. {
  2530. UnicodeString Result =
  2531. ExpandFileName(ExpandEnvironmentVariables(DDTemporaryDirectory));
  2532. if (Result.IsEmpty())
  2533. {
  2534. Result = SystemTemporaryDirectory();
  2535. }
  2536. return Result;
  2537. }
  2538. //---------------------------------------------------------------------------
  2539. UnicodeString TWinConfiguration::TemporaryDir(bool Mask)
  2540. {
  2541. return UniqTempDir(ExpandedTemporaryDirectory(), L"scp", Mask);
  2542. }
  2543. //---------------------------------------------------------------------------
  2544. TStrings * __fastcall TWinConfiguration::DoFindTemporaryFolders(bool OnlyFirst)
  2545. {
  2546. std::unique_ptr<TStrings> Result(new TStringList());
  2547. TSearchRecOwned SRec;
  2548. UnicodeString Mask = TemporaryDir(true);
  2549. if (FindFirstUnchecked(Mask, faDirectory | faHidden, SRec) == 0)
  2550. {
  2551. do
  2552. {
  2553. if (SRec.IsDirectory())
  2554. {
  2555. Result->Add(SRec.GetFilePath());
  2556. }
  2557. }
  2558. while ((FindNextChecked(SRec) == 0) && (!OnlyFirst || Result->Count == 0));
  2559. }
  2560. if (Result->Count == 0)
  2561. {
  2562. Result.reset(NULL);
  2563. }
  2564. return Result.release();
  2565. }
  2566. //---------------------------------------------------------------------------
  2567. TStrings * __fastcall TWinConfiguration::FindTemporaryFolders()
  2568. {
  2569. return DoFindTemporaryFolders(false);
  2570. }
  2571. //---------------------------------------------------------------------------
  2572. bool __fastcall TWinConfiguration::AnyTemporaryFolders()
  2573. {
  2574. std::unique_ptr<TStrings> Folders(DoFindTemporaryFolders(true));
  2575. return (Folders.get() != NULL);
  2576. }
  2577. //---------------------------------------------------------------------------
  2578. void __fastcall TWinConfiguration::CleanupTemporaryFolders()
  2579. {
  2580. std::unique_ptr<TStrings> Folders(FindTemporaryFolders());
  2581. if (Folders.get() != NULL)
  2582. {
  2583. CleanupTemporaryFolders(Folders.get());
  2584. }
  2585. }
  2586. //---------------------------------------------------------------------------
  2587. void __fastcall TWinConfiguration::CleanupTemporaryFolders(TStrings * Folders)
  2588. {
  2589. if (DebugAlwaysTrue(Folders->Count > 0))
  2590. {
  2591. Usage->Inc(L"TemporaryDirectoryCleanups");
  2592. }
  2593. UnicodeString ErrorList;
  2594. for (int i = 0; i < Folders->Count; i++)
  2595. {
  2596. if (!RecursiveDeleteFile(Folders->Strings[i]))
  2597. {
  2598. if (!ErrorList.IsEmpty())
  2599. {
  2600. ErrorList += L"\n";
  2601. }
  2602. ErrorList += Folders->Strings[i];
  2603. }
  2604. }
  2605. if (!ErrorList.IsEmpty())
  2606. {
  2607. throw ExtException(LoadStr(CLEANUP_TEMP_ERROR), ErrorList);
  2608. }
  2609. }
  2610. //---------------------------------------------------------------------------
  2611. int __fastcall TWinConfiguration::GetResourceModuleCompleteness(HINSTANCE Module)
  2612. {
  2613. UnicodeString CompletenessStr = LoadStrFrom(Module, TRANSLATION_COMPLETENESS);
  2614. return StrToIntDef(CompletenessStr, -1);
  2615. }
  2616. //---------------------------------------------------------------------------
  2617. int __fastcall TWinConfiguration::GetLocaleCompletenessTreshold()
  2618. {
  2619. return 80;
  2620. }
  2621. //---------------------------------------------------------------------------
  2622. bool __fastcall TWinConfiguration::IsTranslationComplete(HINSTANCE Module)
  2623. {
  2624. return (GetResourceModuleCompleteness(Module) >= LocaleCompletenessTreshold);
  2625. }
  2626. //---------------------------------------------------------------------------
  2627. HINSTANCE __fastcall TWinConfiguration::LoadNewResourceModule(LCID ALocale,
  2628. UnicodeString & FileName)
  2629. {
  2630. HINSTANCE Instance = TCustomWinConfiguration::LoadNewResourceModule(ALocale, FileName);
  2631. if (Instance != NULL)
  2632. {
  2633. try
  2634. {
  2635. CheckTranslationVersion(FileName, false);
  2636. }
  2637. catch(...)
  2638. {
  2639. FreeResourceModule(Instance);
  2640. throw;
  2641. }
  2642. }
  2643. return Instance;
  2644. }
  2645. //---------------------------------------------------------------------------
  2646. void __fastcall TWinConfiguration::CheckTranslationVersion(const UnicodeString FileName,
  2647. bool InternalLocaleOnError)
  2648. {
  2649. UnicodeString TranslationProductVersion = GetFileProductVersion(FileName);
  2650. UnicodeString TranslationProductName = GetFileProductName(FileName);
  2651. if ((ProductName != TranslationProductName) ||
  2652. (ProductVersion != TranslationProductVersion))
  2653. {
  2654. if (InternalLocaleOnError)
  2655. {
  2656. LocaleSafe = InternalLocale();
  2657. }
  2658. if (TranslationProductName.IsEmpty() || TranslationProductVersion.IsEmpty())
  2659. {
  2660. throw Exception(FMTLOAD(UNKNOWN_TRANSLATION, (FileName)));
  2661. }
  2662. else
  2663. {
  2664. throw Exception(FMTLOAD(INCOMPATIBLE_TRANSLATION,
  2665. (FileName, TranslationProductName, TranslationProductVersion)));
  2666. }
  2667. }
  2668. }
  2669. //---------------------------------------------------------------------------
  2670. void __fastcall TWinConfiguration::CheckDefaultTranslation()
  2671. {
  2672. if (!FInvalidDefaultTranslationMessage.IsEmpty())
  2673. {
  2674. MoreMessageDialog(FMTLOAD(INVALID_DEFAULT_TRANSLATION,
  2675. (FInvalidDefaultTranslationMessage)), NULL, qtWarning, qaOK, HELP_NONE);
  2676. }
  2677. }
  2678. //---------------------------------------------------------------------------
  2679. const TEditorPreferences * __fastcall TWinConfiguration::DefaultEditorForFile(
  2680. const UnicodeString FileName, bool Local, const TFileMasks::TParams & MaskParams)
  2681. {
  2682. return FEditorList->Find(FileName, Local, MaskParams);
  2683. }
  2684. //---------------------------------------------------------------------------
  2685. const TEditorList * __fastcall TWinConfiguration::GetEditorList()
  2686. {
  2687. return FEditorList;
  2688. }
  2689. //---------------------------------------------------------------------------
  2690. void __fastcall TWinConfiguration::SetEditorList(const TEditorList * value)
  2691. {
  2692. if (!(*FEditorList == *value))
  2693. {
  2694. *FEditorList = *value;
  2695. Changed();
  2696. }
  2697. }
  2698. //---------------------------------------------------------------------------
  2699. TStrings * __fastcall TWinConfiguration::GetCustomCommandOptions()
  2700. {
  2701. return FCustomCommandOptions.get();
  2702. }
  2703. //---------------------------------------------------------------------------
  2704. void __fastcall TWinConfiguration::SetCustomCommandOptions(TStrings * value)
  2705. {
  2706. if (!FCustomCommandOptions->Equals(value))
  2707. {
  2708. FCustomCommandOptions->Assign(value);
  2709. FCustomCommandOptionsModified = true;
  2710. }
  2711. }
  2712. //---------------------------------------------------------------------------
  2713. void __fastcall TWinConfiguration::SetLockedInterface(bool value)
  2714. {
  2715. SET_CONFIG_PROPERTY(LockedInterface);
  2716. }
  2717. //---------------------------------------------------------------------------
  2718. bool __fastcall TWinConfiguration::GetTimeoutShellOperations()
  2719. {
  2720. return ::TimeoutShellOperations;
  2721. }
  2722. //---------------------------------------------------------------------------
  2723. void __fastcall TWinConfiguration::SetTimeoutShellOperations(bool value)
  2724. {
  2725. ::TimeoutShellOperations = value;
  2726. }
  2727. //---------------------------------------------------------------------------
  2728. void __fastcall TWinConfiguration::SetTimeoutShellIconRetrieval(bool value)
  2729. {
  2730. SET_CONFIG_PROPERTY(TimeoutShellIconRetrieval);
  2731. }
  2732. //---------------------------------------------------------------------------
  2733. void __fastcall TWinConfiguration::SetUseIconUpdateThread(bool value)
  2734. {
  2735. SET_CONFIG_PROPERTY(UseIconUpdateThread);
  2736. }
  2737. //---------------------------------------------------------------------------
  2738. void __fastcall TWinConfiguration::SetAllowWindowPrint(bool value)
  2739. {
  2740. SET_CONFIG_PROPERTY(AllowWindowPrint);
  2741. }
  2742. //---------------------------------------------------------------------------
  2743. void TWinConfiguration::SetStoreTransition(TStoreTransition value)
  2744. {
  2745. SET_CONFIG_PROPERTY(StoreTransition);
  2746. }
  2747. //---------------------------------------------------------------------------
  2748. void TWinConfiguration::SetQueueTransferLimitMax(int value)
  2749. {
  2750. SET_CONFIG_PROPERTY(QueueTransferLimitMax);
  2751. }
  2752. //---------------------------------------------------------------------------
  2753. void TWinConfiguration::SetHiContrast(bool value)
  2754. {
  2755. SET_CONFIG_PROPERTY(HiContrast);
  2756. }
  2757. //---------------------------------------------------------------------------
  2758. void TWinConfiguration::SetEditorCheckNotModified(bool value)
  2759. {
  2760. SET_CONFIG_PROPERTY(EditorCheckNotModified);
  2761. }
  2762. //---------------------------------------------------------------------------
  2763. void TWinConfiguration::SetSessionTabCaptionTruncation(bool value)
  2764. {
  2765. SET_CONFIG_PROPERTY(SessionTabCaptionTruncation);
  2766. }
  2767. //---------------------------------------------------------------------------
  2768. void TWinConfiguration::SetLoadingTooLongLimit(int value)
  2769. {
  2770. DriveViewLoadingTooLongLimit = value;
  2771. }
  2772. //---------------------------------------------------------------------------
  2773. int TWinConfiguration::GetLoadingTooLongLimit()
  2774. {
  2775. return DriveViewLoadingTooLongLimit;
  2776. }
  2777. //---------------------------------------------------------------------------
  2778. void TWinConfiguration::SetFirstRun(const UnicodeString & value)
  2779. {
  2780. SET_CONFIG_PROPERTY(FirstRun);
  2781. }
  2782. //---------------------------------------------------------------------------
  2783. TStringList * __fastcall TWinConfiguration::LoadJumpList(
  2784. THierarchicalStorage * Storage, UnicodeString Name)
  2785. {
  2786. UnicodeString JumpList = Storage->ReadString(Name, L"");
  2787. std::unique_ptr<TStringList> List(new TStringList());
  2788. List->CaseSensitive = false;
  2789. List->CommaText = JumpList;
  2790. return List.release();
  2791. }
  2792. //---------------------------------------------------------------------------
  2793. void __fastcall TWinConfiguration::SaveJumpList(
  2794. THierarchicalStorage * Storage, UnicodeString Name, TStringList * List)
  2795. {
  2796. UnicodeString JumpList = Storage->ReadString(Name, L"");
  2797. UnicodeString NewJumpList = List->CommaText;
  2798. if (NewJumpList != JumpList)
  2799. {
  2800. Storage->WriteString(Name, NewJumpList);
  2801. }
  2802. }
  2803. //---------------------------------------------------------------------------
  2804. void __fastcall TWinConfiguration::TrimJumpList(TStringList * List)
  2805. {
  2806. while (List->Count > 30)
  2807. {
  2808. List->Delete(List->Count - 1);
  2809. }
  2810. }
  2811. //---------------------------------------------------------------------------
  2812. void __fastcall TWinConfiguration::UpdateEntryInJumpList(
  2813. bool Session, const UnicodeString & Name, bool Add)
  2814. {
  2815. try
  2816. {
  2817. std::unique_ptr<THierarchicalStorage> Storage(CreateConfigStorage());
  2818. TAutoNestingCounter DontDecryptPasswordsCounter(FDontDecryptPasswords);
  2819. Storage->AccessMode = smReadWrite;
  2820. // For initial call from UpdateJumpList, do not create the key if it does ot exist yet.
  2821. // To avoid creating the key if we are being started just for a maintenance task.
  2822. if (Storage->OpenSubKey(ConfigurationSubKey, !Name.IsEmpty()))
  2823. {
  2824. std::unique_ptr<TStringList> ListSessions(LoadJumpList(Storage.get(), L"JumpList"));
  2825. std::unique_ptr<TStringList> ListWorkspaces(LoadJumpList(Storage.get(), L"JumpListWorkspaces"));
  2826. if (!Name.IsEmpty())
  2827. {
  2828. TStringList * List = (Session ? ListSessions : ListWorkspaces).get();
  2829. int Index = List->IndexOf(Name);
  2830. if (Index >= 0)
  2831. {
  2832. List->Delete(Index);
  2833. }
  2834. if (Add)
  2835. {
  2836. List->Insert(0, Name);
  2837. }
  2838. }
  2839. TrimJumpList(ListSessions.get());
  2840. TrimJumpList(ListWorkspaces.get());
  2841. ::UpdateJumpList(ListSessions.get(), ListWorkspaces.get());
  2842. SaveJumpList(Storage.get(), L"JumpList", ListSessions.get());
  2843. SaveJumpList(Storage.get(), L"JumpListWorkspaces", ListWorkspaces.get());
  2844. }
  2845. }
  2846. catch (Exception & E)
  2847. {
  2848. throw ExtException(&E, MainInstructions(LoadStr(JUMPLIST_ERROR)));
  2849. }
  2850. }
  2851. //---------------------------------------------------------------------------
  2852. void __fastcall TWinConfiguration::AddSessionToJumpList(UnicodeString SessionName)
  2853. {
  2854. UpdateEntryInJumpList(true, SessionName, true);
  2855. }
  2856. //---------------------------------------------------------------------------
  2857. void __fastcall TWinConfiguration::DeleteSessionFromJumpList(UnicodeString SessionName)
  2858. {
  2859. UpdateEntryInJumpList(true, SessionName, false);
  2860. }
  2861. //---------------------------------------------------------------------------
  2862. void __fastcall TWinConfiguration::AddWorkspaceToJumpList(UnicodeString Workspace)
  2863. {
  2864. UpdateEntryInJumpList(false, Workspace, true);
  2865. }
  2866. //---------------------------------------------------------------------------
  2867. void __fastcall TWinConfiguration::DeleteWorkspaceFromJumpList(UnicodeString Workspace)
  2868. {
  2869. UpdateEntryInJumpList(false, Workspace, false);
  2870. }
  2871. //---------------------------------------------------------------------------
  2872. void __fastcall TWinConfiguration::UpdateJumpList()
  2873. {
  2874. AddSessionToJumpList(L"");
  2875. }
  2876. //---------------------------------------------------------------------------
  2877. void __fastcall TWinConfiguration::UpdateStaticUsage()
  2878. {
  2879. // This is here because of TStoredSessionList::UpdateStaticUsage() call
  2880. // from TConfiguration::UpdateStaticUsage()
  2881. TAutoNestingCounter DontDecryptPasswordsAutoCounter(FDontDecryptPasswords);
  2882. TCustomWinConfiguration::UpdateStaticUsage();
  2883. Usage->Set(L"Beta", IsBeta);
  2884. Usage->Set(L"Interface", Interface);
  2885. Usage->Set(L"ThemeDark", UseDarkTheme());
  2886. Usage->Set(L"CustomCommandsCount", (FCustomCommandsDefaults ? 0 : FCustomCommandList->Count));
  2887. Usage->Set(L"UsingLocationProfiles", UseLocationProfiles);
  2888. Usage->Set(L"UsingMasterPassword", UseMasterPassword);
  2889. Usage->Set(L"UsingAutoSaveWorkspace", AutoSaveWorkspace);
  2890. Usage->Set(L"TreeVisible",
  2891. (Interface == ifExplorer) ?
  2892. ScpExplorer.DriveView :
  2893. (ScpCommander.LocalPanel.DriveView || ScpCommander.RemotePanel.DriveView));
  2894. Usage->Set(L"MinimizeToTray", MinimizeToTray);
  2895. UnicodeString ToolbarsButtons = (Interface == ifExplorer) ? ScpExplorer.ToolbarsButtons : ScpCommander.ToolbarsButtons;
  2896. Usage->Set(L"AnyHiddenToolbarButtons", !ToolbarsButtons.IsEmpty());
  2897. Usage->Set(L"FileColors", !FileColors.IsEmpty());
  2898. Usage->Set(L"DragDropDrives", !DDDrives.IsEmpty());
  2899. Usage->Set(L"ShowingTips", ShowTips);
  2900. Usage->Set(L"KeepingOpenWhenNoSession", KeepOpenWhenNoSession);
  2901. Usage->Set(L"ShowingLoginWhenNoSession", ShowLoginWhenNoSession);
  2902. Usage->Set(L"DefaultingToNewRemoteTab", DefaultToNewRemoteTab);
  2903. TipsUpdateStaticUsage();
  2904. Usage->Set(L"CommanderNortonLikeMode", int(ScpCommander.NortonLikeMode));
  2905. Usage->Set(L"CommanderExplorerKeyboardShortcuts", ScpCommander.ExplorerKeyboardShortcuts);
  2906. Usage->Set(L"ExplorerViewStyle", ScpExplorer.ViewStyle);
  2907. Usage->Set(L"LastMonitor", LastMonitor);
  2908. UnicodeString ExternalEditors;
  2909. for (int Index = 0; Index < EditorList->Count; Index++)
  2910. {
  2911. const TEditorPreferences * Editor = EditorList->Editors[Index];
  2912. if (Editor->Data->Editor == edExternal)
  2913. {
  2914. AddToList(ExternalEditors, Editor->ExtractExternalEditorName(), ",");
  2915. }
  2916. }
  2917. Usage->Set(L"ExternalEditors", ExternalEditors);
  2918. if (LastMachineInstallations < FMachineInstallations)
  2919. {
  2920. Usage->Inc(L"InstallationsMachine", (FMachineInstallations - LastMachineInstallations));
  2921. LastMachineInstallations = FMachineInstallations;
  2922. }
  2923. int ExtensionsPortable = 0;
  2924. int ExtensionsInstalled = 0;
  2925. int ExtensionsUser = 0;
  2926. for (int Index = 0; Index < FExtensionList->Count; Index++)
  2927. {
  2928. const TCustomCommandType * CustomCommand = FExtensionList->Commands[Index];
  2929. UnicodeString PathId = ExcludeTrailingBackslash(ExtractFilePath(CustomCommand->Id));
  2930. if (SameText(PathId, ExtensionsCommonPathId))
  2931. {
  2932. ExtensionsPortable++;
  2933. }
  2934. else if (SameText(PathId, ExtensionsCommonExtPathId))
  2935. {
  2936. ExtensionsInstalled++;
  2937. }
  2938. else if (SameText(PathId, ExtensionsUserExtPathId))
  2939. {
  2940. ExtensionsUser++;
  2941. }
  2942. }
  2943. Usage->Set(L"ExtensionsPortableCount", (ExtensionsPortable));
  2944. Usage->Set(L"ExtensionsInstalledCount", (ExtensionsInstalled));
  2945. Usage->Set(L"ExtensionsUserCount", (ExtensionsUser));
  2946. std::unique_ptr<TStringList> DeletedExtensions(CreateSortedStringList());
  2947. ParseExtensionList(DeletedExtensions.get(), FExtensionsDeleted);
  2948. Usage->Set(L"ExtensionsDeleted", (DeletedExtensions->Count));
  2949. }
  2950. //---------------------------------------------------------------------------
  2951. void __fastcall TWinConfiguration::RestoreFont(
  2952. const TFontConfiguration & Configuration, TFont * Font)
  2953. {
  2954. Font->Name = Configuration.FontName;
  2955. Font->Size = Configuration.FontSize;
  2956. Font->Charset = Configuration.FontCharset;
  2957. Font->Style = IntToFontStyles(Configuration.FontStyle);
  2958. }
  2959. //---------------------------------------------------------------------------
  2960. void __fastcall TWinConfiguration::StoreFont(
  2961. TFont * Font, TFontConfiguration & Configuration)
  2962. {
  2963. Configuration.FontName = Font->Name;
  2964. Configuration.FontSize = Font->Size;
  2965. Configuration.FontCharset = Font->Charset;
  2966. Configuration.FontStyle = FontStylesToInt(Font->Style);
  2967. }
  2968. //---------------------------------------------------------------------------
  2969. TResolvedDoubleClickAction TWinConfiguration::ResolveDoubleClickAction(bool IsDirectory, TTerminal * Terminal)
  2970. {
  2971. TResolvedDoubleClickAction Result;
  2972. // Anything special is done on files only (not directories)
  2973. if (IsDirectory)
  2974. {
  2975. Result = rdcaChangeDir;
  2976. }
  2977. else
  2978. {
  2979. Result = rdcaNone;
  2980. if (Terminal != NULL)
  2981. {
  2982. if (!Terminal->ResolvingSymlinks && !Terminal->IsEncryptingFiles() && !AlwaysRespectDoubleClickAction)
  2983. {
  2984. Result = rdcaChangeDir;
  2985. }
  2986. }
  2987. if (Result == rdcaNone)
  2988. {
  2989. switch (DoubleClickAction)
  2990. {
  2991. case dcaOpen:
  2992. Result = rdcaOpen;
  2993. break;
  2994. case dcaCopy:
  2995. Result = rdcaCopy;
  2996. break;
  2997. case dcaEdit:
  2998. Result = rdcaEdit;
  2999. break;
  3000. default:
  3001. DebugFail();
  3002. Abort();
  3003. break;
  3004. }
  3005. }
  3006. }
  3007. return Result;
  3008. }
  3009. //---------------------------------------------------------------------------
  3010. //---------------------------------------------------------------------------
  3011. __fastcall TCustomCommandType::TCustomCommandType() :
  3012. FParams(0), FShortCut(0), FShortCutOriginal(0)
  3013. {
  3014. }
  3015. //---------------------------------------------------------------------------
  3016. __fastcall TCustomCommandType::TCustomCommandType(const TCustomCommandType & Other) :
  3017. FName(Other.FName),
  3018. FCommand(Other.FCommand),
  3019. FParams(Other.FParams),
  3020. FShortCut(Other.FShortCut),
  3021. FShortCutOriginal(Other.FShortCutOriginal),
  3022. FId(Other.FId),
  3023. FFileName(Other.FFileName),
  3024. FDescription(Other.FDescription),
  3025. FHomePage(Other.FHomePage),
  3026. FOptionsPage(Other.FOptionsPage),
  3027. FOptions(Other.FOptions)
  3028. {
  3029. }
  3030. //---------------------------------------------------------------------------
  3031. TCustomCommandType & TCustomCommandType::operator=(const TCustomCommandType & Other)
  3032. {
  3033. FName = Other.FName;
  3034. FCommand = Other.FCommand;
  3035. FParams = Other.FParams;
  3036. FShortCut = Other.FShortCut;
  3037. FShortCutOriginal = Other.FShortCutOriginal;
  3038. FId = Other.FId;
  3039. FFileName = Other.FFileName;
  3040. FDescription = Other.FDescription;
  3041. FHomePage = Other.FHomePage;
  3042. FOptionsPage = Other.FOptionsPage;
  3043. FOptions = Other.FOptions;
  3044. return *this;
  3045. }
  3046. //---------------------------------------------------------------------------
  3047. bool __fastcall TCustomCommandType::Equals(const TCustomCommandType * Other) const
  3048. {
  3049. return
  3050. (FName == Other->FName) &&
  3051. (FCommand == Other->FCommand) &&
  3052. (FParams == Other->FParams) &&
  3053. (FShortCut == Other->FShortCut) &&
  3054. (FShortCutOriginal == Other->FShortCutOriginal) &&
  3055. (FId == Other->FId) &&
  3056. (FFileName == Other->FFileName) &&
  3057. (FDescription == Other->FDescription) &&
  3058. (FHomePage == Other->FHomePage) &&
  3059. (FOptionsPage == Other->FOptionsPage) &&
  3060. (FOptions == Other->FOptions);
  3061. }
  3062. //---------------------------------------------------------------------------
  3063. const UnicodeString ExtensionNameDirective(L"name");
  3064. const UnicodeString ExtensionCommandDirective(L"command");
  3065. const wchar_t ExtensionMark = L'@';
  3066. const UnicodeString WinSCPExtensionExt(L".WinSCPextension");
  3067. //---------------------------------------------------------------------------
  3068. UnicodeString __fastcall TCustomCommandType::GetExtensionId(const UnicodeString & Name)
  3069. {
  3070. UnicodeString Result;
  3071. int P = Pos(UpperCase(WinSCPExtensionExt), UpperCase(Name));
  3072. // Ends with Ext or there's another extension after it
  3073. if ((P > 1) &&
  3074. ((Name.Length() == (P + WinSCPExtensionExt.Length() - 1)) || (Name[P + WinSCPExtensionExt.Length()] == L'.')))
  3075. {
  3076. Result = Name.SubString(1, P - 1);
  3077. }
  3078. return Result;
  3079. }
  3080. //---------------------------------------------------------------------------
  3081. void __fastcall TCustomCommandType::LoadExtension(const UnicodeString & Path)
  3082. {
  3083. std::unique_ptr<TStringList> Lines(new TStringList());
  3084. FileName = Path;
  3085. LoadScriptFromFile(Path, Lines.get());
  3086. LoadExtension(Lines.get(), Path);
  3087. Command = ReplaceStr(Command, L"%EXTENSION_PATH%", Path);
  3088. }
  3089. //---------------------------------------------------------------------------
  3090. void __fastcall TCustomCommandType::LoadExtension(TStrings * Lines, const UnicodeString & PathForBaseName)
  3091. {
  3092. Params = ccLocal;
  3093. bool AnythingFound = false;
  3094. std::set<UnicodeString> OptionIds;
  3095. UnicodeString ExtensionBaseName = ExtractExtensionBaseName(PathForBaseName);
  3096. UnicodeString ExtensionLine;
  3097. bool Break = false;
  3098. for (int Index = 0; !Break && (Index < Lines->Count); Index++)
  3099. {
  3100. UnicodeString Line = Lines->Strings[Index].Trim();
  3101. if (!Line.IsEmpty())
  3102. {
  3103. Line = ReplaceChar(Line, L'\t', L' ');
  3104. bool IsComment = false;
  3105. if (StartsText(ExtensionMark, Line))
  3106. {
  3107. IsComment = true;
  3108. }
  3109. else if (StartsText(L"rem ", Line))
  3110. {
  3111. IsComment = true;
  3112. Line.Delete(1, 4);
  3113. }
  3114. else if (StartsText(L"#", Line) || StartsText(L";", Line) || StartsText(L"'", Line))
  3115. {
  3116. IsComment = true;
  3117. Line.Delete(1, 1);
  3118. }
  3119. else if (StartsText(L"//", Line))
  3120. {
  3121. IsComment = true;
  3122. Line.Delete(1, 2);
  3123. }
  3124. if (!IsComment)
  3125. {
  3126. Break = true;
  3127. // ignore this and later lines, but finish processing the previous line with ^, if any
  3128. Line = L"";
  3129. }
  3130. else
  3131. {
  3132. Line = Line.Trim();
  3133. }
  3134. }
  3135. bool Continuation = (Line.Length() > 0) && (Line[Line.Length()] == L'^');
  3136. if (Continuation)
  3137. {
  3138. Line = Line.SubString(1, Line.Length() - 1).Trim();
  3139. }
  3140. AddToList(ExtensionLine, Line, L" ");
  3141. if (!Continuation)
  3142. {
  3143. int P;
  3144. if (!ExtensionLine.IsEmpty() && (ExtensionLine[1] == ExtensionMark) && ((P = Pos(L" ", ExtensionLine)) >= 2))
  3145. {
  3146. UnicodeString Key = ExtensionLine.SubString(2, P - 2).LowerCase();
  3147. UnicodeString Directive = UnicodeString(ExtensionMark) + Key;
  3148. UnicodeString Value = ExtensionLine.SubString(P + 1, ExtensionLine.Length() - P).Trim();
  3149. bool KnownKey = true;
  3150. if (Key == ExtensionNameDirective)
  3151. {
  3152. Name = WinConfiguration->ExtensionStringTranslation(Id, Value);
  3153. }
  3154. else if (Key == ExtensionCommandDirective)
  3155. {
  3156. Command = Value;
  3157. }
  3158. else if (Key == L"require")
  3159. {
  3160. UnicodeString DependencyVersion = Value;
  3161. UnicodeString Dependency = CutToChar(Value, L' ', true).LowerCase();
  3162. Value = Value.Trim();
  3163. bool Failed;
  3164. if (Dependency == L"winscp")
  3165. {
  3166. int Version = StrToCompoundVersion(Value);
  3167. Failed = (Version > WinConfiguration->CompoundVersion);
  3168. }
  3169. else if (Dependency == L".net")
  3170. {
  3171. Failed = (CompareVersion(Value, GetNetVersionStr()) > 0);
  3172. }
  3173. else if (Dependency == L".netcore")
  3174. {
  3175. Failed = (CompareVersion(Value, GetNetCoreVersionStr()) > 0);
  3176. }
  3177. else if (Dependency == L"powershell")
  3178. {
  3179. Failed = (CompareVersion(Value, GetPowerShellVersionStr()) > 0);
  3180. }
  3181. else if (Dependency == L"pwsh")
  3182. {
  3183. Failed = (CompareVersion(Value, GetPowerShellCoreVersionStr()) > 0);
  3184. }
  3185. else if (Dependency == L"windows")
  3186. {
  3187. Failed = (CompareVersion(Value, WindowsVersion()) > 0);
  3188. }
  3189. else
  3190. {
  3191. Failed = true;
  3192. }
  3193. if (Failed)
  3194. {
  3195. throw Exception(MainInstructions(FMTLOAD(EXTENSION_DEPENDENCY_ERROR, (DependencyVersion))));
  3196. }
  3197. }
  3198. else if (Key == L"side")
  3199. {
  3200. if (SameText(Value, L"Local"))
  3201. {
  3202. Params |= ccLocal;
  3203. }
  3204. else if (SameText(Value, L"Remote"))
  3205. {
  3206. Params &= ~ccLocal;
  3207. }
  3208. else
  3209. {
  3210. throw Exception(MainInstructions(FMTLOAD(EXTENSION_DIRECTIVE_ERROR, (Value, Directive))));
  3211. }
  3212. }
  3213. else if (Key == L"flag")
  3214. {
  3215. if (SameText(Value, L"ApplyToDirectories"))
  3216. {
  3217. Params |= ccApplyToDirectories;
  3218. }
  3219. else if (SameText(Value, L"Recursive"))
  3220. {
  3221. Params |= ccRecursive;
  3222. }
  3223. else if (SameText(Value, L"ShowResults"))
  3224. {
  3225. Params |= ccShowResults;
  3226. }
  3227. else if (SameText(Value, L"CopyResults"))
  3228. {
  3229. Params |= ccCopyResults;
  3230. }
  3231. else if (SameText(Value, L"RemoteFiles"))
  3232. {
  3233. Params |= ccRemoteFiles;
  3234. }
  3235. else if (SameText(Value, L"ShowResultsInMsgBox"))
  3236. {
  3237. Params |= ccShowResultsInMsgBox;
  3238. }
  3239. else
  3240. {
  3241. throw Exception(MainInstructions(FMTLOAD(EXTENSION_DIRECTIVE_ERROR, (Value, Directive))));
  3242. }
  3243. }
  3244. else if (Key == L"shortcut")
  3245. {
  3246. TShortCut AShortCut = TextToShortCut(Value);
  3247. if (IsCustomShortCut(AShortCut))
  3248. {
  3249. ShortCut = AShortCut;
  3250. FShortCutOriginal = AShortCut;
  3251. }
  3252. else
  3253. {
  3254. throw Exception(MainInstructions(FMTLOAD(EXTENSION_DIRECTIVE_ERROR, (Value, Directive))));
  3255. }
  3256. }
  3257. else if (Key == L"option")
  3258. {
  3259. TOption Option;
  3260. if (!ParseOption(Value, Option, ExtensionBaseName) ||
  3261. (Option.IsControl && (OptionIds.find(Option.Id.LowerCase()) != OptionIds.end())))
  3262. {
  3263. throw Exception(MainInstructions(FMTLOAD(EXTENSION_DIRECTIVE_ERROR, (Value, Directive))));
  3264. }
  3265. else
  3266. {
  3267. FOptions.push_back(Option);
  3268. if (!Option.IsControl)
  3269. {
  3270. OptionIds.insert(Option.Id.LowerCase());
  3271. }
  3272. }
  3273. }
  3274. else if (Key == L"description")
  3275. {
  3276. Description = WinConfiguration->ExtensionStringTranslation(Id, Value);
  3277. }
  3278. else if (Key == L"author")
  3279. {
  3280. // noop
  3281. }
  3282. else if (Key == L"version")
  3283. {
  3284. // noop
  3285. }
  3286. else if (Key == L"homepage")
  3287. {
  3288. HomePage = Value;
  3289. }
  3290. else if (Key == L"optionspage")
  3291. {
  3292. OptionsPage = Value;
  3293. }
  3294. else if (Key == L"source")
  3295. {
  3296. // noop
  3297. }
  3298. else
  3299. {
  3300. KnownKey = false;
  3301. }
  3302. if (KnownKey)
  3303. {
  3304. AnythingFound = true;
  3305. }
  3306. }
  3307. ExtensionLine = L"";
  3308. }
  3309. }
  3310. if (!AnythingFound)
  3311. {
  3312. throw Exception(MainInstructions(LoadStr(EXTENSION_NOT_FOUND)));
  3313. }
  3314. if (Name.IsEmpty())
  3315. {
  3316. throw Exception(MainInstructions(FMTLOAD(EXTENSION_DIRECTIVE_MISSING, (UnicodeString(ExtensionMark) + ExtensionNameDirective))));
  3317. }
  3318. if (Command.IsEmpty())
  3319. {
  3320. throw Exception(MainInstructions(FMTLOAD(EXTENSION_DIRECTIVE_MISSING, (UnicodeString(ExtensionMark) + ExtensionCommandDirective))));
  3321. }
  3322. }
  3323. //---------------------------------------------------------------------------
  3324. bool __fastcall TCustomCommandType::ParseOption(const UnicodeString & Value, TOption & Option, const UnicodeString & ExtensionBaseName)
  3325. {
  3326. UnicodeString Buf = Value;
  3327. UnicodeString KindName;
  3328. bool Result = CutTokenEx(Buf, Option.Id);
  3329. if (Result)
  3330. {
  3331. Option.Flags = 0;
  3332. UnicodeString FlagName;
  3333. while (CutTokenEx(Buf, FlagName) && (FlagName.SubString(1, 1) == L"-"))
  3334. {
  3335. FlagName = FlagName.LowerCase();
  3336. if (FlagName == L"-run")
  3337. {
  3338. Option.Flags |= ofRun;
  3339. }
  3340. else if (FlagName == L"-config")
  3341. {
  3342. Option.Flags |= ofConfig;
  3343. }
  3344. else if (FlagName == L"-site")
  3345. {
  3346. Option.Flags |= ofSite;
  3347. }
  3348. else
  3349. {
  3350. Result = false;
  3351. }
  3352. }
  3353. if ((Option.Flags & (ofRun | ofConfig)) == 0)
  3354. {
  3355. Option.Flags |= ofConfig;
  3356. }
  3357. KindName = FlagName;
  3358. if (Result)
  3359. {
  3360. UnicodeString DefaultCaption;
  3361. UnicodeString DefaultDefault;
  3362. TOption::TParams DefaultParams;
  3363. KindName = KindName.LowerCase();
  3364. if (KindName == L"label")
  3365. {
  3366. Option.Kind = okLabel;
  3367. Result = !Option.IsControl;
  3368. }
  3369. else if (KindName == L"link")
  3370. {
  3371. Option.Kind = okLink;
  3372. Result = !Option.IsControl;
  3373. }
  3374. else if (KindName == L"separator")
  3375. {
  3376. Option.Kind = okSeparator;
  3377. Result = !Option.IsControl;
  3378. }
  3379. else if (KindName == L"group")
  3380. {
  3381. Option.Kind = okGroup;
  3382. Result = !Option.IsControl;
  3383. }
  3384. else if (KindName == L"textbox")
  3385. {
  3386. Option.Kind = okTextBox;
  3387. Result = Option.IsControl;
  3388. }
  3389. else if (KindName == L"file")
  3390. {
  3391. Option.Kind = okFile;
  3392. Result = Option.IsControl;
  3393. }
  3394. else if (KindName == L"sessionlogfile")
  3395. {
  3396. Option.Kind = okFile;
  3397. Result = Option.IsControl;
  3398. DefaultCaption = LoadStr(EXTENSION_SESSIONLOG_FILE);
  3399. Option.FileCaption = LoadStr(EXTENSION_SESSIONLOG_CAPTION);
  3400. Option.FileFilter = LoadStr(EXTENSION_SESSIONLOG_FILTER);
  3401. // Similar to TConfiguration::GetDefaultLogFileName
  3402. Option.FileInitial = L"%TEMP%\\" + ExtensionBaseName + L".log";
  3403. Option.FileExt = L"log";
  3404. }
  3405. else if (KindName == L"dropdownlist")
  3406. {
  3407. Option.Kind = okDropDownList;
  3408. Result = Option.IsControl;
  3409. }
  3410. else if (KindName == L"combobox")
  3411. {
  3412. Option.Kind = okComboBox;
  3413. Result = Option.IsControl;
  3414. }
  3415. else if (KindName == L"checkbox")
  3416. {
  3417. Option.Kind = okCheckBox;
  3418. Result = Option.IsControl;
  3419. }
  3420. else if (KindName == L"pausecheckbox")
  3421. {
  3422. Option.Kind = okCheckBox;
  3423. Result = Option.IsControl;
  3424. DefaultCaption = LoadStr(EXTENSION_PAUSE_CHECKBOX);
  3425. DefaultDefault = L"-pause";
  3426. DefaultParams.push_back(L"-pause");
  3427. }
  3428. else
  3429. {
  3430. Option.Kind = okUnknown;
  3431. }
  3432. if ((Option.Kind != okUnknown) &&
  3433. (Option.Kind != okSeparator))
  3434. {
  3435. Result =
  3436. CutTokenEx(Buf, Option.Caption);
  3437. if (!Result && !DefaultCaption.IsEmpty())
  3438. {
  3439. Option.Caption = DefaultCaption;
  3440. Result = true;
  3441. }
  3442. if (Result)
  3443. {
  3444. Option.Caption = WinConfiguration->ExtensionStringTranslation(Id, Option.Caption);
  3445. if (CutTokenEx(Buf, Option.Default))
  3446. {
  3447. UnicodeString Param;
  3448. while (CutTokenEx(Buf, Param))
  3449. {
  3450. Option.Params.push_back(Param);
  3451. }
  3452. }
  3453. else
  3454. {
  3455. Option.Default = DefaultDefault;
  3456. }
  3457. if (Option.Params.size() == 0)
  3458. {
  3459. Option.Params = DefaultParams;
  3460. }
  3461. }
  3462. }
  3463. }
  3464. }
  3465. return Result;
  3466. }
  3467. //---------------------------------------------------------------------------
  3468. int __fastcall TCustomCommandType::GetOptionsCount() const
  3469. {
  3470. return FOptions.size();
  3471. }
  3472. //---------------------------------------------------------------------------
  3473. const TCustomCommandType::TOption & __fastcall TCustomCommandType::GetOption(int Index) const
  3474. {
  3475. return FOptions[Index];
  3476. }
  3477. //---------------------------------------------------------------------------
  3478. UnicodeString __fastcall TCustomCommandType::GetOptionKey(
  3479. const TCustomCommandType::TOption & Option, const UnicodeString & Site) const
  3480. {
  3481. UnicodeString Result = Id + L"\\" + Option.Id;
  3482. if (FLAGSET(Option.Flags, ofSite))
  3483. {
  3484. Result += L"\\" + Site;
  3485. }
  3486. return Result;
  3487. }
  3488. //---------------------------------------------------------------------------
  3489. bool __fastcall TCustomCommandType::AnyOptionWithFlag(unsigned int Flag) const
  3490. {
  3491. bool Result = false;
  3492. for (int Index = 0; !Result && (Index < OptionsCount); Index++)
  3493. {
  3494. const TCustomCommandType::TOption & Option = GetOption(Index);
  3495. Result = FLAGSET(Option.Flags, Flag);
  3496. }
  3497. return Result;
  3498. }
  3499. //---------------------------------------------------------------------------
  3500. UnicodeString __fastcall TCustomCommandType::GetCommandWithExpandedOptions(
  3501. TStrings * CustomCommandOptions, const UnicodeString & Site) const
  3502. {
  3503. UnicodeString Result = Command;
  3504. for (int Index = 0; Index < OptionsCount; Index++)
  3505. {
  3506. const TCustomCommandType::TOption & Option = GetOption(Index);
  3507. if (Option.IsControl)
  3508. {
  3509. UnicodeString OptionKey = GetOptionKey(Option, Site);
  3510. UnicodeString OptionValue;
  3511. bool NeedEscape;
  3512. if (CustomCommandOptions->IndexOfName(OptionKey) >= 0)
  3513. {
  3514. OptionValue = CustomCommandOptions->Values[OptionKey];
  3515. NeedEscape = true;
  3516. }
  3517. else
  3518. {
  3519. OptionValue = Option.Default;
  3520. NeedEscape = !Option.CanHavePatterns(); // approximation only?
  3521. }
  3522. UnicodeString OptionCommand = GetOptionCommand(Option, OptionValue);
  3523. if (NeedEscape)
  3524. {
  3525. OptionCommand = TCustomCommand::Escape(OptionCommand);
  3526. }
  3527. Result = ReplaceText(Result, FORMAT(L"%%%s%%", (Option.Id)), OptionCommand);
  3528. }
  3529. }
  3530. return Result;
  3531. }
  3532. //---------------------------------------------------------------------------
  3533. UnicodeString __fastcall TCustomCommandType::GetOptionCommand(const TOption & Option, const UnicodeString & Value) const
  3534. {
  3535. UnicodeString Result = Value;
  3536. switch (Option.Kind)
  3537. {
  3538. case okUnknown:
  3539. case okTextBox:
  3540. case okDropDownList:
  3541. case okComboBox:
  3542. case okCheckBox:
  3543. // noop
  3544. break;
  3545. case okFile:
  3546. Result = ExpandEnvironmentVariables(Result);
  3547. break;
  3548. case okLabel:
  3549. case okLink:
  3550. case okSeparator:
  3551. case okGroup:
  3552. default:
  3553. DebugFail();
  3554. }
  3555. return Result;
  3556. }
  3557. //---------------------------------------------------------------------------
  3558. bool __fastcall TCustomCommandType::HasCustomShortCut() const
  3559. {
  3560. return (ShortCut != FShortCutOriginal);
  3561. }
  3562. //---------------------------------------------------------------------------
  3563. //---------------------------------------------------------------------------
  3564. bool __fastcall TCustomCommandType::TOption::GetIsControl() const
  3565. {
  3566. return (Id != L"-");
  3567. }
  3568. //---------------------------------------------------------------------------
  3569. bool TCustomCommandType::TOption::CanHavePatterns() const
  3570. {
  3571. bool Result;
  3572. switch (Kind)
  3573. {
  3574. case okTextBox:
  3575. case okFile:
  3576. Result = true;
  3577. break;
  3578. default:
  3579. Result = false;
  3580. break;
  3581. }
  3582. return Result;
  3583. }
  3584. //---------------------------------------------------------------------------
  3585. bool TCustomCommandType::TOption::HasPatterns(TCustomCommand * CustomCommandForOptions) const
  3586. {
  3587. bool Result =
  3588. CanHavePatterns() &&
  3589. FLAGSET(Flags, TCustomCommandType::ofRun) &&
  3590. FLAGCLEAR(Flags, TCustomCommandType::ofConfig) &&
  3591. CustomCommandForOptions->HasAnyPatterns(Default);
  3592. return Result;
  3593. }
  3594. //---------------------------------------------------------------------------
  3595. bool TCustomCommandType::TOption::operator==(const TCustomCommandType::TOption & Other) const
  3596. {
  3597. // needed by vector<> but probably never used
  3598. return
  3599. (Id == Other.Id) &&
  3600. (Flags == Other.Flags) &&
  3601. (Kind == Other.Kind) &&
  3602. (Caption == Other.Caption) &&
  3603. (Default == Other.Default) &&
  3604. (Params == Other.Params) &&
  3605. (FileCaption == Other.FileCaption) &&
  3606. (FileFilter == Other.FileFilter) &&
  3607. (FileInitial == Other.FileInitial) &&
  3608. (FileExt == Other.FileExt);
  3609. }
  3610. //---------------------------------------------------------------------------
  3611. //---------------------------------------------------------------------------
  3612. __fastcall TCustomCommandList::TCustomCommandList()
  3613. {
  3614. FCommands = new TList();
  3615. FModified = false;
  3616. }
  3617. //---------------------------------------------------------------------------
  3618. __fastcall TCustomCommandList::~TCustomCommandList()
  3619. {
  3620. Clear();
  3621. delete FCommands;
  3622. }
  3623. //---------------------------------------------------------------------------
  3624. void __fastcall TCustomCommandList::Reset()
  3625. {
  3626. FModified = false;
  3627. }
  3628. //---------------------------------------------------------------------------
  3629. void __fastcall TCustomCommandList::Modify()
  3630. {
  3631. FModified = true;
  3632. }
  3633. //---------------------------------------------------------------------------
  3634. void __fastcall TCustomCommandList::Load(THierarchicalStorage * Storage)
  3635. {
  3636. Clear();
  3637. if (Storage->OpenSubKey(L"CustomCommands", false))
  3638. {
  3639. TStrings * Names = new TStringList();
  3640. try
  3641. {
  3642. Storage->ReadValues(Names, true);
  3643. for (int Index = 0; Index < Names->Count; Index++)
  3644. {
  3645. TCustomCommandType * Command = new TCustomCommandType();
  3646. Command->Name = Names->Names[Index];
  3647. Command->Command = Names->Values[Names->Names[Index]];
  3648. FCommands->Add(Command);
  3649. }
  3650. Storage->CloseSubKey();
  3651. }
  3652. __finally
  3653. {
  3654. delete Names;
  3655. }
  3656. }
  3657. if (Storage->OpenSubKey(L"CustomCommandsParams", false))
  3658. {
  3659. for (int Index = 0; Index < FCommands->Count; Index++)
  3660. {
  3661. TCustomCommandType * Command = GetCommand(Index);
  3662. Command->Params = Storage->ReadInteger(Command->Name, Command->Params);
  3663. }
  3664. Storage->CloseSubKey();
  3665. }
  3666. if (Storage->OpenSubKey(L"CustomCommandsShortCuts", false))
  3667. {
  3668. for (int Index = 0; Index < FCommands->Count; Index++)
  3669. {
  3670. TCustomCommandType * Command = GetCommand(Index);
  3671. Command->ShortCut = (Word)Storage->ReadInteger(Command->Name, Command->ShortCut);
  3672. }
  3673. Storage->CloseSubKey();
  3674. }
  3675. Reset();
  3676. }
  3677. //---------------------------------------------------------------------------
  3678. void __fastcall TCustomCommandList::Save(THierarchicalStorage * Storage)
  3679. {
  3680. if (Storage->OpenSubKey(L"CustomCommands", true))
  3681. {
  3682. Storage->ClearValues();
  3683. for (int Index = 0; Index < FCommands->Count; Index++)
  3684. {
  3685. const TCustomCommandType * Command = Commands[Index];
  3686. Storage->WriteString(Command->Name, Command->Command);
  3687. }
  3688. Storage->CloseSubKey();
  3689. }
  3690. if (Storage->OpenSubKey(L"CustomCommandsParams", true))
  3691. {
  3692. Storage->ClearValues();
  3693. for (int Index = 0; Index < FCommands->Count; Index++)
  3694. {
  3695. const TCustomCommandType * Command = Commands[Index];
  3696. Storage->WriteInteger(Command->Name, Command->Params);
  3697. }
  3698. Storage->CloseSubKey();
  3699. }
  3700. if (Storage->OpenSubKey(L"CustomCommandsShortCuts", true))
  3701. {
  3702. Storage->ClearValues();
  3703. for (int Index = 0; Index < FCommands->Count; Index++)
  3704. {
  3705. const TCustomCommandType * Command = Commands[Index];
  3706. if (Command->ShortCut != 0)
  3707. {
  3708. Storage->WriteInteger(Command->Name, Command->ShortCut);
  3709. }
  3710. }
  3711. Storage->CloseSubKey();
  3712. }
  3713. }
  3714. //---------------------------------------------------------------------------
  3715. void __fastcall TCustomCommandList::Clear()
  3716. {
  3717. for (int Index = 0; Index < FCommands->Count; Index++)
  3718. {
  3719. delete Commands[Index];
  3720. }
  3721. FCommands->Clear();
  3722. }
  3723. //---------------------------------------------------------------------------
  3724. void __fastcall TCustomCommandList::Add(const UnicodeString Name,
  3725. const UnicodeString ACommand, int Params)
  3726. {
  3727. TCustomCommandType * Command = new TCustomCommandType();
  3728. Command->Name = Name;
  3729. Command->Command = ACommand;
  3730. Command->Params = Params;
  3731. Add(Command);
  3732. }
  3733. //---------------------------------------------------------------------------
  3734. void __fastcall TCustomCommandList::Add(TCustomCommandType * Command)
  3735. {
  3736. Insert(Count, Command);
  3737. }
  3738. //---------------------------------------------------------------------------
  3739. void __fastcall TCustomCommandList::Insert(int Index, TCustomCommandType * Command)
  3740. {
  3741. FCommands->Insert(Index, Command);
  3742. Modify();
  3743. }
  3744. //---------------------------------------------------------------------------
  3745. void __fastcall TCustomCommandList::Change(int Index, TCustomCommandType * ACommand)
  3746. {
  3747. TCustomCommandType * Command = GetCommand(Index);
  3748. if (!Command->Equals(ACommand))
  3749. {
  3750. delete Command;
  3751. FCommands->Items[Index] = ACommand;
  3752. Modify();
  3753. }
  3754. else
  3755. {
  3756. delete ACommand;
  3757. }
  3758. }
  3759. //---------------------------------------------------------------------------
  3760. void __fastcall TCustomCommandList::Move(int CurIndex, int NewIndex)
  3761. {
  3762. if (CurIndex != NewIndex)
  3763. {
  3764. FCommands->Move(CurIndex, NewIndex);
  3765. Modify();
  3766. }
  3767. }
  3768. //---------------------------------------------------------------------------
  3769. void __fastcall TCustomCommandList::Delete(int Index)
  3770. {
  3771. DebugAssert((Index >= 0) && (Index < Count));
  3772. delete GetCommand(Index);
  3773. FCommands->Delete(Index);
  3774. Modify();
  3775. }
  3776. //---------------------------------------------------------------------------
  3777. class TCustomCommandCompareFunc : public TCppInterfacedObject<TListSortCompareFunc>
  3778. {
  3779. public:
  3780. TCustomCommandCompareFunc(TStrings * Ids)
  3781. {
  3782. FIds = Ids;
  3783. }
  3784. virtual int __fastcall Invoke(void * Item1, void * Item2)
  3785. {
  3786. TCustomCommandType * CustomCommand1 = static_cast<TCustomCommandType *>(Item1);
  3787. TCustomCommandType * CustomCommand2 = static_cast<TCustomCommandType *>(Item2);
  3788. int Index1 = FIds->IndexOf(CustomCommand1->Id);
  3789. int Index2 = FIds->IndexOf(CustomCommand2->Id);
  3790. int Result;
  3791. // new items to the end
  3792. if ((Index1 < 0) && (Index2 >= 0))
  3793. {
  3794. Result = 1;
  3795. }
  3796. else if ((Index2 < 0) && (Index1 >= 0))
  3797. {
  3798. Result = -1;
  3799. }
  3800. // fallback to comparing by name
  3801. else if ((Index1 < 0) && (Index2 < 0))
  3802. {
  3803. Result = TComparer__1<UnicodeString>::Default()->Compare(CustomCommand1->Name, CustomCommand2->Name);
  3804. }
  3805. else
  3806. {
  3807. Result = TComparer__1<int>::Default()->Compare(Index1, Index2);
  3808. }
  3809. return Result;
  3810. }
  3811. private:
  3812. TStrings * FIds;
  3813. };
  3814. //---------------------------------------------------------------------------
  3815. void __fastcall TCustomCommandList::SortBy(TStrings * Ids)
  3816. {
  3817. TCustomCommandCompareFunc * Func = new TCustomCommandCompareFunc(Ids);
  3818. FCommands->SortList(Func);
  3819. }
  3820. //---------------------------------------------------------------------------
  3821. int __fastcall TCustomCommandList::GetCount() const
  3822. {
  3823. return FCommands->Count;
  3824. }
  3825. //---------------------------------------------------------------------------
  3826. const TCustomCommandType * __fastcall TCustomCommandList::GetConstCommand(int Index) const
  3827. {
  3828. return static_cast<TCustomCommandType *>(FCommands->Items[Index]);
  3829. }
  3830. //---------------------------------------------------------------------------
  3831. TCustomCommandType * __fastcall TCustomCommandList::GetCommand(int Index)
  3832. {
  3833. return static_cast<TCustomCommandType *>(FCommands->Items[Index]);
  3834. }
  3835. //---------------------------------------------------------------------------
  3836. bool __fastcall TCustomCommandList::Equals(const TCustomCommandList * Other) const
  3837. {
  3838. bool Result = (Count == Other->Count);
  3839. for (int Index = 0; Result && (Index < Count); Index++)
  3840. {
  3841. Result = Commands[Index]->Equals(Other->Commands[Index]);
  3842. }
  3843. return Result;
  3844. }
  3845. //---------------------------------------------------------------------------
  3846. void __fastcall TCustomCommandList::Assign(const TCustomCommandList * Other)
  3847. {
  3848. Clear();
  3849. for (int Index = 0; Index < Other->Count; Index++)
  3850. {
  3851. Add(new TCustomCommandType(*Other->Commands[Index]));
  3852. }
  3853. // there should be comparison of with the assigned list, be we rely on caller
  3854. // to do it instead (TGUIConfiguration::SetCopyParamList)
  3855. Modify();
  3856. }
  3857. //---------------------------------------------------------------------------
  3858. const TCustomCommandType * TCustomCommandList::Find(const UnicodeString Name) const
  3859. {
  3860. for (int Index = 0; Index < FCommands->Count; Index++)
  3861. {
  3862. if (Commands[Index]->Name == Name)
  3863. {
  3864. return Commands[Index];
  3865. }
  3866. }
  3867. return NULL;
  3868. }
  3869. //---------------------------------------------------------------------------
  3870. const TCustomCommandType * TCustomCommandList::Find(TShortCut ShortCut) const
  3871. {
  3872. for (int Index = 0; Index < FCommands->Count; Index++)
  3873. {
  3874. if (Commands[Index]->ShortCut == ShortCut)
  3875. {
  3876. return Commands[Index];
  3877. }
  3878. }
  3879. return NULL;
  3880. }
  3881. //---------------------------------------------------------------------------
  3882. int TCustomCommandList::FindIndexByFileName(const UnicodeString & FileName) const
  3883. {
  3884. for (int Index = 0; Index < FCommands->Count; Index++)
  3885. {
  3886. if (IsPathToSameFile(Commands[Index]->FileName, FileName))
  3887. {
  3888. return Index;
  3889. }
  3890. }
  3891. return -1;
  3892. }
  3893. //---------------------------------------------------------------------------
  3894. void __fastcall TCustomCommandList::ShortCuts(TShortCuts & ShortCuts) const
  3895. {
  3896. for (int Index = 0; Index < FCommands->Count; Index++)
  3897. {
  3898. const TCustomCommandType * Command = Commands[Index];
  3899. if (Command->ShortCut != 0)
  3900. {
  3901. ShortCuts.Add(Command->ShortCut);
  3902. }
  3903. }
  3904. }