WinConfiguration.cpp 132 KB

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