WinConfiguration.cpp 134 KB

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