WinConfiguration.cpp 138 KB

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