WinConfiguration.cpp 135 KB

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