WinConfiguration.cpp 137 KB

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