WinConfiguration.cpp 131 KB

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