WinConfiguration.cpp 134 KB

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