WinConfiguration.cpp 132 KB

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