WinConfiguration.cpp 138 KB

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