1
0

WinConfiguration.cpp 136 KB

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