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 <Generics.Defaults.hpp>
  23. #include <OperationWithTimeout.hpp>
  24. #include "FileInfo.h"
  25. #include "CoreMain.h"
  26. #include "DriveView.hpp"
  27. //---------------------------------------------------------------------------
  28. #pragma package(smart_init)
  29. //---------------------------------------------------------------------------
  30. TWinConfiguration * WinConfiguration = NULL;
  31. //---------------------------------------------------------------------------
  32. static UnicodeString NotepadName(L"notepad.exe");
  33. static UnicodeString ToolbarsLayoutKey(L"ToolbarsLayout2");
  34. static UnicodeString ToolbarsLayoutOldKey(L"ToolbarsLayout");
  35. TDateTime DefaultUpdatesPeriod(7);
  36. // WORKAROUND (the semicolon, see TCustomListViewColProperties.GetParamsStr, and see other instances below)
  37. const UnicodeString ScpExplorerDirViewParamsDefault =
  38. 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";
  39. const UnicodeString ScpCommanderRemotePanelDirViewParamsDefault = ScpExplorerDirViewParamsDefault;
  40. const UnicodeString ScpCommanderLocalPanelDirViewParamsDefault =
  41. L"0;1;0|150,1;70,1;120,1;150,1;55,0;55,0;@" + SaveDefaultPixelsPerInch() + L"|5;0;1;2;3;4";
  42. UnicodeString QueueViewLayoutDefault;
  43. //---------------------------------------------------------------------------
  44. static const wchar_t FileColorDataSeparator = L':';
  45. TFileColorData::TFileColorData() :
  46. Color(TColor())
  47. {
  48. }
  49. //---------------------------------------------------------------------------
  50. void TFileColorData::Load(const UnicodeString & S)
  51. {
  52. UnicodeString Buf(S);
  53. Color = RestoreColor(CutToChar(Buf, FileColorDataSeparator, true));
  54. FileMask = Buf;
  55. }
  56. //---------------------------------------------------------------------------
  57. UnicodeString TFileColorData::Save() const
  58. {
  59. UnicodeString Result = StoreColor(Color) + FileColorDataSeparator + FileMask.Masks;
  60. return Result;
  61. }
  62. //---------------------------------------------------------------------------
  63. void TFileColorData::LoadList(const UnicodeString & S, TList & List)
  64. {
  65. std::unique_ptr<TStringList> Strings(CommaTextToStringList(S));
  66. List.clear();
  67. for (int Index = 0; Index < Strings->Count; Index++)
  68. {
  69. TFileColorData FileColorData;
  70. FileColorData.Load(Strings->Strings[Index]);
  71. List.push_back(FileColorData);
  72. }
  73. }
  74. //---------------------------------------------------------------------------
  75. UnicodeString TFileColorData::SaveList(const TList & List)
  76. {
  77. std::unique_ptr<TStringList> Strings(new TStringList());
  78. for (TFileColorData::TList::const_iterator Iter = List.begin(); Iter != List.end(); Iter++)
  79. {
  80. Strings->Add((*Iter).Save());
  81. }
  82. return Strings->CommaText;
  83. }
  84. //---------------------------------------------------------------------------
  85. //---------------------------------------------------------------------------
  86. __fastcall TEditorData::TEditorData() :
  87. FileMask(AnyMask),
  88. Editor(edInternal),
  89. ExternalEditor(L""),
  90. ExternalEditorText(false),
  91. SDIExternalEditor(false),
  92. DetectMDIExternalEditor(false)
  93. {
  94. }
  95. //---------------------------------------------------------------------------
  96. __fastcall TEditorData::TEditorData(const TEditorData & Source) :
  97. FileMask(Source.FileMask),
  98. Editor(Source.Editor),
  99. ExternalEditor(Source.ExternalEditor),
  100. ExternalEditorText(Source.ExternalEditorText),
  101. SDIExternalEditor(Source.SDIExternalEditor),
  102. DetectMDIExternalEditor(Source.DetectMDIExternalEditor)
  103. {
  104. }
  105. //---------------------------------------------------------------------------
  106. #define C(Property) (Property == rhd.Property)
  107. bool __fastcall TEditorData::operator==(const TEditorData & rhd) const
  108. {
  109. return
  110. C(FileMask) &&
  111. C(Editor) &&
  112. C(ExternalEditor) &&
  113. C(ExternalEditorText) &&
  114. C(SDIExternalEditor) &&
  115. C(DetectMDIExternalEditor) &&
  116. true;
  117. }
  118. #undef C
  119. //---------------------------------------------------------------------------
  120. void __fastcall TEditorData::ExternalEditorOptionsAutodetect()
  121. {
  122. UnicodeString Command = ExternalEditor;
  123. ReformatFileNameCommand(Command);
  124. UnicodeString ProgramName = ExtractProgramName(Command);
  125. // We explicitly do not use TEditorPreferences::GetDefaultExternalEditor(),
  126. // as we need to explicitly refer to the Notepad, even if the default external
  127. // editor ever changes
  128. UnicodeString NotepadProgramName = ExtractProgramName(NotepadName);
  129. if (SameText(ProgramName, NotepadProgramName))
  130. {
  131. // By default we use default transfer mode (binary),
  132. // as all reasonable 3rd party editors support all EOL styles.
  133. // A notable exception is Windows Notepad before Windows 10 1809, so here's an exception for it.
  134. ExternalEditorText = !IsWin10Build(17763);
  135. SDIExternalEditor = true;
  136. }
  137. }
  138. //---------------------------------------------------------------------------
  139. __fastcall TEditorPreferences::TEditorPreferences()
  140. {
  141. }
  142. //---------------------------------------------------------------------------
  143. __fastcall TEditorPreferences::TEditorPreferences(const TEditorData & Data) :
  144. FData(Data)
  145. {
  146. }
  147. //---------------------------------------------------------------------------
  148. bool __fastcall TEditorPreferences::operator==(const TEditorPreferences & rhp) const
  149. {
  150. return (FData == rhp.FData);
  151. }
  152. #undef C
  153. //---------------------------------------------------------------------------
  154. bool __fastcall TEditorPreferences::Matches(const UnicodeString FileName,
  155. bool Local, const TFileMasks::TParams & Params) const
  156. {
  157. return FData.FileMask.Matches(FileName, Local, false, &Params);
  158. }
  159. //---------------------------------------------------------------------------
  160. UnicodeString __fastcall TEditorPreferences::GetDefaultExternalEditor()
  161. {
  162. return NotepadName;
  163. }
  164. //---------------------------------------------------------------------------
  165. void __fastcall TEditorPreferences::LegacyDefaults()
  166. {
  167. FData.ExternalEditor = GetDefaultExternalEditor();
  168. FData.ExternalEditorOptionsAutodetect();
  169. }
  170. //---------------------------------------------------------------------------
  171. UnicodeString __fastcall TEditorPreferences::ExtractExternalEditorName() const
  172. {
  173. DebugAssert(FData.Editor == edExternal);
  174. UnicodeString ExternalEditor = FData.ExternalEditor;
  175. ReformatFileNameCommand(ExternalEditor);
  176. // Trim is a workaround for unknown problem with "notepad " (2 trailing spaces)
  177. return ExtractProgramName(ExternalEditor).Trim();
  178. }
  179. //---------------------------------------------------------------------------
  180. void __fastcall TEditorPreferences::Load(THierarchicalStorage * Storage, bool Legacy)
  181. {
  182. if (!Legacy)
  183. {
  184. FData.FileMask = Storage->ReadString(L"FileMask", FData.FileMask.Masks);
  185. }
  186. FData.Editor = (TEditor)Storage->ReadInteger(L"Editor", FData.Editor);
  187. FData.ExternalEditor = Storage->ReadString(L"ExternalEditor", FData.ExternalEditor);
  188. FData.ExternalEditorText = Storage->ReadBool(L"ExternalEditorText", FData.ExternalEditorText);
  189. FData.SDIExternalEditor = Storage->ReadBool(L"SDIExternalEditor", FData.SDIExternalEditor);
  190. FData.DetectMDIExternalEditor = Storage->ReadBool(L"DetectMDIExternalEditor", FData.DetectMDIExternalEditor);
  191. }
  192. //---------------------------------------------------------------------------
  193. void __fastcall TEditorPreferences::Save(THierarchicalStorage * Storage) const
  194. {
  195. Storage->WriteString(L"FileMask", FData.FileMask.Masks);
  196. Storage->WriteInteger(L"Editor", FData.Editor);
  197. Storage->WriteString(L"ExternalEditor", FData.ExternalEditor);
  198. Storage->WriteBool(L"ExternalEditorText", FData.ExternalEditorText);
  199. Storage->WriteBool(L"SDIExternalEditor", FData.SDIExternalEditor);
  200. Storage->WriteBool(L"DetectMDIExternalEditor", FData.DetectMDIExternalEditor);
  201. }
  202. //---------------------------------------------------------------------------
  203. TEditorData * __fastcall TEditorPreferences::GetData()
  204. {
  205. // returning non-const data, possible data change, invalidate cached name
  206. FName = L"";
  207. return &FData;
  208. };
  209. //---------------------------------------------------------------------------
  210. UnicodeString __fastcall TEditorPreferences::GetName() const
  211. {
  212. if (FName.IsEmpty())
  213. {
  214. if (FData.Editor == edInternal)
  215. {
  216. // StripHotkey is relic from times when INTERNAL_EDITOR_NAME was used
  217. // also for the menu item caption
  218. FName = StripHotkey(LoadStr(INTERNAL_EDITOR_NAME));
  219. }
  220. else if (FData.Editor == edOpen)
  221. {
  222. FName = StripHotkey(LoadStr(OPEN_EDITOR_NAME));
  223. }
  224. else
  225. {
  226. UnicodeString Program, Params, Dir;
  227. UnicodeString ExternalEditor = FData.ExternalEditor;
  228. ReformatFileNameCommand(ExternalEditor);
  229. SplitCommand(ExternalEditor, Program, Params, Dir);
  230. FName = ExtractFileName(Program);
  231. int P = FName.LastDelimiter(L".");
  232. if (P > 0)
  233. {
  234. FName.SetLength(P - 1);
  235. }
  236. if (FName.ByteType(1) == mbSingleByte)
  237. {
  238. if (FName.UpperCase() == FName)
  239. {
  240. FName = FName.LowerCase();
  241. }
  242. if (FName.LowerCase() == FName)
  243. {
  244. FName = FName.SubString(1, 1).UpperCase() +
  245. FName.SubString(2, FName.Length() - 1);
  246. }
  247. }
  248. }
  249. }
  250. return FName;
  251. }
  252. //---------------------------------------------------------------------------
  253. //---------------------------------------------------------------------------
  254. __fastcall TEditorList::TEditorList()
  255. {
  256. Init();
  257. }
  258. //---------------------------------------------------------------------------
  259. void __fastcall TEditorList::Init()
  260. {
  261. FEditors = new TList();
  262. FModified = false;
  263. }
  264. //---------------------------------------------------------------------------
  265. __fastcall TEditorList::~TEditorList()
  266. {
  267. Clear();
  268. delete FEditors;
  269. }
  270. //---------------------------------------------------------------------
  271. void __fastcall TEditorList::Modify()
  272. {
  273. FModified = true;
  274. }
  275. //---------------------------------------------------------------------------
  276. void __fastcall TEditorList::Saved()
  277. {
  278. FModified = false;
  279. }
  280. //---------------------------------------------------------------------------
  281. TEditorList & __fastcall TEditorList::operator=(const TEditorList & rhl)
  282. {
  283. Clear();
  284. for (int Index = 0; Index < rhl.Count; Index++)
  285. {
  286. Add(new TEditorPreferences(*rhl.Editors[Index]));
  287. }
  288. // there should be comparison of with the assigned list, but we rely on caller
  289. // to do it instead (TWinConfiguration::SetEditorList)
  290. Modify();
  291. return *this;
  292. }
  293. //---------------------------------------------------------------------------
  294. bool __fastcall TEditorList::operator==(const TEditorList & rhl) const
  295. {
  296. bool Result = (Count == rhl.Count);
  297. if (Result)
  298. {
  299. int i = 0;
  300. while ((i < Count) && Result)
  301. {
  302. Result = (*Editors[i]) == (*rhl.Editors[i]);
  303. i++;
  304. }
  305. }
  306. return Result;
  307. }
  308. //---------------------------------------------------------------------------
  309. void __fastcall TEditorList::Clear()
  310. {
  311. for (int i = 0; i < Count; i++)
  312. {
  313. delete Editors[i];
  314. }
  315. FEditors->Clear();
  316. }
  317. //---------------------------------------------------------------------------
  318. void __fastcall TEditorList::Add(TEditorPreferences * Editor)
  319. {
  320. Insert(Count, Editor);
  321. }
  322. //---------------------------------------------------------------------------
  323. void __fastcall TEditorList::Insert(int Index, TEditorPreferences * Editor)
  324. {
  325. FEditors->Insert(Index, reinterpret_cast<TObject *>(Editor));
  326. Modify();
  327. }
  328. //---------------------------------------------------------------------------
  329. void __fastcall TEditorList::Change(int Index, TEditorPreferences * Editor)
  330. {
  331. if (!((*Editors[Index]) == *Editor))
  332. {
  333. delete Editors[Index];
  334. FEditors->Items[Index] = (reinterpret_cast<TObject *>(Editor));
  335. Modify();
  336. }
  337. else
  338. {
  339. delete Editor;
  340. }
  341. }
  342. //---------------------------------------------------------------------------
  343. void __fastcall TEditorList::Move(int CurIndex, int NewIndex)
  344. {
  345. if (CurIndex != NewIndex)
  346. {
  347. FEditors->Move(CurIndex, NewIndex);
  348. Modify();
  349. }
  350. }
  351. //---------------------------------------------------------------------------
  352. void __fastcall TEditorList::Delete(int Index)
  353. {
  354. DebugAssert((Index >= 0) && (Index < Count));
  355. delete Editors[Index];
  356. FEditors->Delete(Index);
  357. Modify();
  358. }
  359. //---------------------------------------------------------------------------
  360. const TEditorPreferences * __fastcall TEditorList::Find(
  361. const UnicodeString FileName, bool Local, const TFileMasks::TParams & Params) const
  362. {
  363. const TEditorPreferences * Result = NULL;
  364. int i = 0;
  365. while ((i < FEditors->Count) && (Result == NULL))
  366. {
  367. Result = Editors[i];
  368. if (!Result->Matches(FileName, Local, Params))
  369. {
  370. Result = NULL;
  371. }
  372. i++;
  373. }
  374. return Result;
  375. }
  376. //---------------------------------------------------------------------------
  377. void __fastcall TEditorList::Load(THierarchicalStorage * Storage)
  378. {
  379. int Index = 0;
  380. bool Next;
  381. do
  382. {
  383. UnicodeString Name = IntToStr(Index);
  384. TEditorPreferences * Editor = NULL;
  385. try
  386. {
  387. Next = Storage->OpenSubKey(Name, false);
  388. if (Next)
  389. {
  390. try
  391. {
  392. Editor = new TEditorPreferences();
  393. Editor->Load(Storage, false);
  394. }
  395. __finally
  396. {
  397. Storage->CloseSubKey();
  398. }
  399. }
  400. }
  401. catch(...)
  402. {
  403. delete Editor;
  404. throw;
  405. }
  406. if (Editor != NULL)
  407. {
  408. FEditors->Add(reinterpret_cast<TObject *>(Editor));
  409. }
  410. Index++;
  411. }
  412. while (Next);
  413. FModified = false;
  414. }
  415. //---------------------------------------------------------------------------
  416. void __fastcall TEditorList::Save(THierarchicalStorage * Storage) const
  417. {
  418. Storage->ClearSubKeys();
  419. for (int Index = 0; Index < Count; Index++)
  420. {
  421. if (Storage->OpenSubKey(IntToStr(Index), true))
  422. {
  423. try
  424. {
  425. Editors[Index]->Save(Storage);
  426. }
  427. __finally
  428. {
  429. Storage->CloseSubKey();
  430. }
  431. }
  432. }
  433. }
  434. //---------------------------------------------------------------------------
  435. int __fastcall TEditorList::GetCount() const
  436. {
  437. int X = FEditors->Count;
  438. return X;
  439. }
  440. //---------------------------------------------------------------------------
  441. const TEditorPreferences * __fastcall TEditorList::GetEditor(int Index) const
  442. {
  443. return reinterpret_cast<TEditorPreferences *>(FEditors->Items[Index]);
  444. }
  445. //---------------------------------------------------------------------------
  446. bool __fastcall TEditorList::IsDefaultList() const
  447. {
  448. bool Result = true;
  449. for (int Index = 0; Result && (Index < Count); Index++)
  450. {
  451. const TEditorPreferences * Editor = GetEditor(Index);
  452. if (Editor->Data->Editor == edInternal)
  453. {
  454. // noop (keeps Result true)
  455. }
  456. else if (Editor->Data->Editor == edExternal)
  457. {
  458. UnicodeString ExternalEditor = Editor->ExtractExternalEditorName();
  459. UnicodeString DefaultExternalEditor = ExtractProgramName(TEditorPreferences::GetDefaultExternalEditor());
  460. Result = SameText(ExternalEditor, DefaultExternalEditor);
  461. }
  462. else
  463. {
  464. Result = false;
  465. }
  466. }
  467. return Result;
  468. }
  469. //---------------------------------------------------------------------------
  470. //---------------------------------------------------------------------------
  471. __fastcall TWinConfiguration::TWinConfiguration(): TCustomWinConfiguration()
  472. {
  473. FInvalidDefaultTranslationMessage = L"";
  474. FDDExtInstalled = -1;
  475. FBookmarks = new TBookmarks();
  476. FCustomCommandList = new TCustomCommandList();
  477. FExtensionList = new TCustomCommandList();
  478. FEditorList = new TEditorList();
  479. FDefaultUpdatesPeriod = 0;
  480. FDontDecryptPasswords = 0;
  481. FMasterPasswordSession = 0;
  482. FMasterPasswordSessionAsked = false;
  483. FCustomCommandOptions.reset(new TStringList());
  484. FCustomCommandOptionsModified = false;
  485. FExtensionTranslations.reset(new TStringList());
  486. Default();
  487. // This matters only if the translations are in the executable folder and auto-loaded by VCL (System.Pas - DelayLoadResourceModule)
  488. try
  489. {
  490. UnicodeString ResourceModuleName = GetResourceModuleName(Application->Name, ModuleFileName().c_str());
  491. CheckTranslationVersion(ResourceModuleName, true);
  492. }
  493. catch(Exception & E)
  494. {
  495. FInvalidDefaultTranslationMessage = E.Message;
  496. }
  497. // Load complete locale according to the UI language
  498. SetLocaleInternal(NULL, true, true);
  499. FDefaultLocale = AppliedLocale;
  500. }
  501. //---------------------------------------------------------------------------
  502. __fastcall TWinConfiguration::~TWinConfiguration()
  503. {
  504. if (!FTemporarySessionFile.IsEmpty()) DeleteFile(ApiPath(FTemporarySessionFile));
  505. ClearTemporaryLoginData();
  506. ReleaseExtensionTranslations();
  507. delete FBookmarks;
  508. delete FCustomCommandList;
  509. delete FExtensionList;
  510. delete FEditorList;
  511. }
  512. //---------------------------------------------------------------------------
  513. void __fastcall TWinConfiguration::Default()
  514. {
  515. FCustomCommandsDefaults = true;
  516. FCustomCommandOptionsModified = false;
  517. TCustomWinConfiguration::Default();
  518. int WorkAreaWidthScaled = DimensionToDefaultPixelsPerInch(Screen->WorkAreaWidth);
  519. int WorkAreaHeightScaled = DimensionToDefaultPixelsPerInch(Screen->WorkAreaHeight);
  520. UnicodeString PixelsPerInchToolbarValue = "PixelsPerInch=" + SaveDefaultPixelsPerInch();
  521. FDDDisableMove = false;
  522. FDDTransferConfirmation = asAuto;
  523. FDDTemporaryDirectory = L"";
  524. FDDDrives = L"";
  525. FDDWarnLackOfTempSpace = true;
  526. FDDWarnLackOfTempSpaceRatio = 1.1;
  527. FDDFakeFile = true;
  528. FDDExtTimeout = MSecsPerSec;
  529. FDeleteToRecycleBin = true;
  530. FSelectDirectories = false;
  531. FSelectMask = AnyMask;
  532. FShowHiddenFiles = false;
  533. FFormatSizeBytes = fbKilobytes;
  534. FPanelSearch = isNameStartOnly;
  535. FShowInaccesibleDirectories = true;
  536. FConfirmTransferring = true;
  537. FConfirmDeleting = true;
  538. FConfirmRecycling = true;
  539. FConfirmClosingSession = true;
  540. FDoubleClickAction = dcaEdit;
  541. FCopyOnDoubleClickConfirmation = false;
  542. FAlwaysRespectDoubleClickAction = false;
  543. FDimmHiddenFiles = true;
  544. FRenameWholeName = false;
  545. FAutoStartSession = L"";
  546. FExpertMode = true;
  547. FUseLocationProfiles = false;
  548. FUseSharedBookmarks = false;
  549. FDefaultDirIsHome = true;
  550. FDDDeleteDelay = 120;
  551. FTemporaryDirectoryAppendSession = false;
  552. FTemporaryDirectoryDeterministic = false;
  553. FTemporaryDirectoryAppendPath = true;
  554. FTemporaryDirectoryCleanup = true;
  555. FConfirmTemporaryDirectoryCleanup = true;
  556. FPreservePanelState = true;
  557. FDarkTheme = asAuto;
  558. FLastStoredSession = L"";
  559. // deliberately not being saved, so that when saving ad-hoc workspace,
  560. // we do not offer to overwrite the last saved workspace, what may be undesirable
  561. FLastWorkspace = L"";
  562. FAutoSaveWorkspace = false;
  563. FAutoSaveWorkspacePasswords = false;
  564. FAutoWorkspace = L"";
  565. FPathInCaption = picShort;
  566. FSessionTabNameFormat = stnfShortPathTrunc;
  567. FMinimizeToTray = false;
  568. FBalloonNotifications = true;
  569. FNotificationsTimeout = 10;
  570. FNotificationsStickTime = 2;
  571. FCopyParamAutoSelectNotice = true;
  572. FLockToolbars = false;
  573. FSelectiveToolbarText = true;
  574. FLargerToolbar = 1;
  575. FAutoOpenInPutty = false;
  576. FRefreshRemotePanel = false;
  577. FRefreshRemotePanelInterval = TDateTime(0, 1, 0, 0);
  578. FPanelFont.FontName = L"";
  579. FPanelFont.FontSize = 0;
  580. FPanelFont.FontStyle = 0;
  581. FPanelFont.FontCharset = DEFAULT_CHARSET;
  582. FNaturalOrderNumericalSorting = true;
  583. FAlwaysSortDirectoriesByName = false;
  584. FFullRowSelect = false;
  585. FOfferedEditorAutoConfig = false;
  586. FVersionHistory = L"";
  587. AddVersionToHistory();
  588. FUseMasterPassword = false;
  589. FPlainMasterPasswordEncrypt = L"";
  590. FPlainMasterPasswordDecrypt = L"";
  591. FMasterPasswordVerifier = L"";
  592. FOpenedStoredSessionFolders = L"";
  593. FAutoImportedFromPuttyOrFilezilla = false;
  594. FGenerateUrlComponents = -1;
  595. FGenerateUrlCodeTarget = guctUrl;
  596. FGenerateUrlScriptFormat = sfScriptFile;
  597. FGenerateUrlAssemblyLanguage = alCSharp;
  598. FExternalSessionInExistingInstance = true;
  599. FShowLoginWhenNoSession = true;
  600. FKeepOpenWhenNoSession = true;
  601. FDefaultToNewRemoteTab = true;
  602. FLocalIconsByExt = false;
  603. FFlashTaskbar = true;
  604. FMaxSessions = 100;
  605. FBidiModeOverride = lfoLanguageIfRecommended;
  606. FFlipChildrenOverride = lfoLanguageIfRecommended;
  607. FShowTips = true;
  608. FTipsSeen = L"";
  609. FTipsShown = Now();
  610. FFileColors = L"";
  611. FRunsSinceLastTip = 0;
  612. FExtensionsDeleted = L"";
  613. FLockedInterface = false;
  614. HonorDrivePolicy = true;
  615. UseABDrives = true;
  616. TimeoutShellOperations = true;
  617. TimeoutShellIconRetrieval = false;
  618. UseIconUpdateThread = true;
  619. AllowWindowPrint = false;
  620. StoreTransition = stInit;
  621. QueueTransferLimitMax = 9;
  622. HiContrast = false;
  623. EditorCheckNotModified = false;
  624. SessionTabCaptionTruncation = true;
  625. LoadingTooLongLimit = 15;
  626. RemoteThumbnailMask = EmptyStr;
  627. RemoteThumbnailSizeLimit = 50 * 1024;
  628. FirstRun = StandardDatestamp();
  629. FEditor.Font.FontName = DefaultFixedWidthFontName;
  630. FEditor.Font.FontSize = DefaultFixedWidthFontSize;
  631. FEditor.Font.FontStyle = 0;
  632. FEditor.Font.FontCharset = DEFAULT_CHARSET;
  633. FEditor.FontColor = TColor(0);
  634. FEditor.BackgroundColor = TColor(0);
  635. FEditor.WordWrap = false;
  636. FEditor.FindText = L"";
  637. FEditor.ReplaceText = L"";
  638. FEditor.FindMatchCase = false;
  639. FEditor.FindWholeWord = false;
  640. FEditor.FindDown = true;
  641. FEditor.TabSize = 8;
  642. FEditor.MaxEditors = 500;
  643. FEditor.EarlyClose = 2; // seconds
  644. FEditor.SDIShellEditor = false;
  645. FEditor.WindowParams = L"";
  646. FEditor.Encoding = CP_ACP;
  647. FEditor.WarnOnEncodingFallback = true;
  648. FEditor.WarnOrLargeFileSize = true;
  649. FEditor.AutoFont = true;
  650. FEditor.DisableSmoothScroll = false;
  651. FQueueView.Height = 140;
  652. FQueueView.HeightPixelsPerInch = USER_DEFAULT_SCREEN_DPI;
  653. if (QueueViewLayoutDefault.IsEmpty())
  654. {
  655. // with 1000 pixels wide screen, both interfaces are wide enough to fit wider queue
  656. QueueViewLayoutDefault =
  657. UnicodeString((WorkAreaWidthScaled > 1000) ? L"70,250,250,80,80,80,100" : L"70,160,160,80,80,80,100") +
  658. // WORKAROUND (the comma), see GetListViewStr
  659. L",;" + SaveDefaultPixelsPerInch();
  660. }
  661. FQueueView.Layout = QueueViewLayoutDefault;
  662. FQueueView.Show = qvHideWhenEmpty;
  663. FQueueView.LastHideShow = qvHideWhenEmpty;
  664. FQueueView.ToolBar = true;
  665. FQueueView.Label = true;
  666. FQueueView.FileList = false;
  667. FQueueView.FileListHeight = 90;
  668. FQueueView.FileListHeightPixelsPerInch = USER_DEFAULT_SCREEN_DPI;
  669. FEnableQueueByDefault = true;
  670. FUpdates.Period = FDefaultUpdatesPeriod;
  671. FUpdates.LastCheck = 0;
  672. FUpdates.HaveResults = false;
  673. FUpdates.ShownResults = false;
  674. FUpdates.BetaVersions = asAuto;
  675. FUpdates.ShowOnStartup = true;
  676. FUpdates.AuthenticationEmail = L"";
  677. FUpdates.Mode = EmptyStr;
  678. // for backward compatibility the default is decided based on value of ProxyHost
  679. FUpdates.ConnectionType = (TConnectionType)-1;
  680. FUpdates.ProxyHost = L""; // keep empty (see above)
  681. FUpdates.ProxyPort = 8080;
  682. FUpdates.Results.Reset();
  683. FUpdates.DotNetVersion = L"";
  684. FUpdates.ConsoleVersion = L"";
  685. int ExplorerWidth = Min(WorkAreaWidthScaled - 40, 960);
  686. int ExplorerHeight = Min(WorkAreaHeightScaled - 30, 720);
  687. FScpExplorer.WindowParams = FormatDefaultWindowParams(ExplorerWidth, ExplorerHeight);
  688. FScpExplorer.DirViewParams = ScpExplorerDirViewParamsDefault;
  689. FScpExplorer.ToolbarsLayout =
  690. UnicodeString(
  691. L"Queue=1::0+-1,"
  692. "Menu=1:TopDock:0+0,"
  693. "Buttons=1:TopDock:2+0,"
  694. "Selection=0:TopDock:3+0,"
  695. "Session=0:TopDock:6+0,"
  696. "Preferences=1:TopDock:4+0,"
  697. "Sort=0:TopDock:5+0,"
  698. "Address=1:TopDock:1+0,"
  699. "Updates=1:TopDock:4+393,"
  700. "Transfer=1:TopDock:4+171,"
  701. "CustomCommands=0:TopDock:7+0,") +
  702. PixelsPerInchToolbarValue;
  703. FScpExplorer.ToolbarsButtons = UnicodeString();
  704. FScpExplorer.SessionsTabs = true;
  705. FScpExplorer.StatusBar = true;
  706. FScpExplorer.LastLocalTargetDirectory = GetPersonalFolder();
  707. FScpExplorer.ViewStyle = dvsIcon;
  708. FScpExplorer.ShowFullAddress = true;
  709. FScpExplorer.DriveView = true;
  710. FScpExplorer.DriveViewWidth = 180;
  711. FScpExplorer.DriveViewWidthPixelsPerInch = USER_DEFAULT_SCREEN_DPI;
  712. int CommanderWidth = Min(WorkAreaWidthScaled - 40, 1090);
  713. int CommanderHeight = Min(WorkAreaHeightScaled - 30, 700);
  714. FScpCommander.WindowParams = FormatDefaultWindowParams(CommanderWidth, CommanderHeight);
  715. FScpCommander.LocalPanelWidth = 0.5;
  716. FScpCommander.SwappedPanels = false;
  717. FScpCommander.SessionsTabs = true;
  718. FScpCommander.StatusBar = true;
  719. FScpCommander.NortonLikeMode = nlKeyboard;
  720. FScpCommander.PreserveLocalDirectory = false;
  721. FScpCommander.ToolbarsLayout =
  722. UnicodeString(
  723. L"Queue=1::0+-1,"
  724. "Menu=1:TopDock:0+0,"
  725. "Preferences=1:TopDock:1+228,"
  726. "Session=0:TopDock:1+602,"
  727. "Sort=0:TopDock:2+0,"
  728. "Commands=1:TopDock:1+0,"
  729. "Updates=1:TopDock:1+596,"
  730. "Transfer=1:TopDock:1+341,"
  731. "CustomCommands=0:TopDock:3+0,"
  732. "RemoteHistory=1:RemoteTopDock:0+172,"
  733. "RemoteNavigation=1:RemoteTopDock:0+252,"
  734. "RemotePath=1:RemoteTopDock:0+0,"
  735. "RemoteFile=1:RemoteTopDock:1+0,"
  736. "RemoteSelection=1:RemoteTopDock:1+345,"
  737. "LocalHistory=1:LocalTopDock:0+207,"
  738. "LocalNavigation=1:LocalTopDock:0+287,"
  739. "LocalPath=1:LocalTopDock:0+0,"
  740. "LocalFile=1:LocalTopDock:1+0,"
  741. "LocalSelection=1:LocalTopDock:1+329,"
  742. "Toolbar2=0:BottomDock:1+0,"
  743. "CommandLine=0:BottomDock:0+0,") +
  744. PixelsPerInchToolbarValue;
  745. FScpCommander.ToolbarsButtons = UnicodeString();
  746. FScpCommander.CurrentPanel = osLocal;
  747. FScpCommander.CompareByTime = true;
  748. FScpCommander.CompareBySize = false;
  749. FScpCommander.TreeOnLeft = false;
  750. FScpCommander.ExplorerKeyboardShortcuts = false;
  751. FScpCommander.SystemContextMenu = false;
  752. FScpCommander.RemotePanel.DirViewParams = ScpCommanderRemotePanelDirViewParamsDefault;
  753. FScpCommander.RemotePanel.ViewStyle = dvsReport;
  754. FScpCommander.RemotePanel.StatusBar = true;
  755. FScpCommander.RemotePanel.DriveView = false;
  756. FScpCommander.RemotePanel.DriveViewHeight = 100;
  757. FScpCommander.RemotePanel.DriveViewHeightPixelsPerInch = USER_DEFAULT_SCREEN_DPI;
  758. FScpCommander.RemotePanel.DriveViewWidth = 100;
  759. FScpCommander.RemotePanel.DriveViewWidthPixelsPerInch = USER_DEFAULT_SCREEN_DPI;
  760. FScpCommander.RemotePanel.LastPath = UnicodeString();
  761. FScpCommander.LocalPanel.DirViewParams = ScpCommanderLocalPanelDirViewParamsDefault;
  762. FScpCommander.LocalPanel.ViewStyle = dvsReport;
  763. FScpCommander.LocalPanel.StatusBar = true;
  764. FScpCommander.LocalPanel.DriveView = false;
  765. FScpCommander.LocalPanel.DriveViewHeight = 100;
  766. FScpCommander.LocalPanel.DriveViewHeightPixelsPerInch = USER_DEFAULT_SCREEN_DPI;
  767. FScpCommander.LocalPanel.DriveViewWidth = 100;
  768. FScpCommander.LocalPanel.DriveViewWidthPixelsPerInch = USER_DEFAULT_SCREEN_DPI;
  769. FScpCommander.LocalPanel.LastPath = UnicodeString();
  770. FScpCommander.OtherLocalPanelDirViewParams = FScpCommander.LocalPanel.DirViewParams;
  771. FScpCommander.OtherLocalPanelViewStyle = FScpCommander.LocalPanel.ViewStyle;
  772. FScpCommander.OtherLocalPanelLastPath = UnicodeString();
  773. FBookmarks->Clear();
  774. }
  775. //---------------------------------------------------------------------------
  776. void __fastcall TWinConfiguration::DefaultLocalized()
  777. {
  778. TGUIConfiguration::DefaultLocalized();
  779. if (FCustomCommandsDefaults)
  780. {
  781. FCustomCommandList->Clear();
  782. FCustomCommandList->Add(LoadStr(CUSTOM_COMMAND_EXECUTE), L"\"./!\"", 0);
  783. FCustomCommandList->Add(LoadStr(CUSTOM_COMMAND_TOUCH), L"touch \"!\"", ccApplyToDirectories | ccRecursive);
  784. FCustomCommandList->Add(LoadStr(CUSTOM_COMMAND_TAR),
  785. FORMAT(L"tar -cz -f \"!?%s?archive.tgz!\" !&",
  786. (LoadStr(CUSTOM_COMMAND_TAR_ARCHIVE))), ccApplyToDirectories);
  787. FCustomCommandList->Add(LoadStr(CUSTOM_COMMAND_UNTAR),
  788. FORMAT(L"tar -xz --directory=\"!?%s?.!\" -f \"!\"",
  789. (LoadStr(CUSTOM_COMMAND_UNTAR_DIRECTORY))), 0);
  790. FCustomCommandList->Add(LoadStr(CUSTOM_COMMAND_GREP),
  791. FORMAT(L"grep \"!?%s?!\" !&", (LoadStr(CUSTOM_COMMAND_GREP_PATTERN))),
  792. ccShowResults);
  793. FCustomCommandList->Add(LoadStr(CUSTOM_COMMAND_PRINT), L"notepad.exe /p \"!\"", ccLocal);
  794. FCustomCommandList->Reset();
  795. FCustomCommandsDefaults = true;
  796. }
  797. }
  798. //---------------------------------------------------------------------------
  799. bool __fastcall TWinConfiguration::DetectRegistryStorage(HKEY RootKey)
  800. {
  801. bool Result = false;
  802. TRegistryStorage * Storage = new TRegistryStorage(RegistryStorageKey, RootKey);
  803. try
  804. {
  805. if (Storage->OpenRootKey(false))
  806. {
  807. Result = true;
  808. Storage->CloseSubKey();
  809. }
  810. }
  811. __finally
  812. {
  813. delete Storage;
  814. }
  815. return Result;
  816. }
  817. //---------------------------------------------------------------------------
  818. bool __fastcall TWinConfiguration::CanWriteToStorage()
  819. {
  820. bool Result = false;
  821. try
  822. {
  823. THierarchicalStorage * Storage = CreateConfigStorage();
  824. try
  825. {
  826. Storage->AccessMode = smReadWrite;
  827. // This is actually not very good test, as we end up potentially with
  828. // the very same config, and TIniFileStorage file won't even try to
  829. // write the file then. Luckily, we use this for empty config only,
  830. // so we end up with at least an empty section.
  831. if (Storage->OpenSubKey(ConfigurationSubKey, true))
  832. {
  833. Storage->WriteBool(L"WriteTest", true);
  834. Storage->DeleteValue(L"WriteTest");
  835. }
  836. Storage->Flush();
  837. }
  838. __finally
  839. {
  840. delete Storage;
  841. }
  842. Result = true;
  843. }
  844. catch(...)
  845. {
  846. }
  847. return Result;
  848. }
  849. //---------------------------------------------------------------------------
  850. bool TWinConfiguration::DetectStorage(bool SafeOnly)
  851. {
  852. bool Result;
  853. UnicodeString IniFile = IniFileStorageNameForReading;
  854. if (FileExists(ApiPath(IniFile)))
  855. {
  856. Result = !SafeOnly;
  857. if (Result)
  858. {
  859. FStorage = stIniFile;
  860. AppLogFmt(L"Will use existing INI file %s for configuration", (IniFile));
  861. }
  862. }
  863. else
  864. {
  865. if (IsUWP() ||
  866. DetectRegistryStorage(HKEY_CURRENT_USER) ||
  867. DetectRegistryStorage(HKEY_LOCAL_MACHINE))
  868. {
  869. FStorage = stRegistry;
  870. if (IsUWP())
  871. {
  872. AppLog(L"Will use registry for configuration as this is store installation");
  873. }
  874. else
  875. {
  876. AppLog(L"Will use existing registry for configuration");
  877. }
  878. Result = true;
  879. }
  880. else
  881. {
  882. if (SafeOnly)
  883. {
  884. Result = false;
  885. AppLog(L"No configuration found");
  886. }
  887. else
  888. {
  889. FStorage = stIniFile;
  890. // As we fall back to user profile folder, when application folder
  891. // is not writtable, it is actually unlikely that the below test ever fails.
  892. if (!CanWriteToStorage())
  893. {
  894. FStorage = stRegistry;
  895. AppLog(L"Will use registry configuration as no configuration was found and default INI file location is not writtable");
  896. }
  897. else
  898. {
  899. AppLog(L"Will use (not yet existing) INI file for configuration");
  900. }
  901. // With !SafeOnly we always return true, so this result is actually never considered
  902. Result = true;
  903. }
  904. }
  905. }
  906. return Result;
  907. }
  908. //---------------------------------------------------------------------------
  909. TStorage __fastcall TWinConfiguration::GetStorage()
  910. {
  911. if (FStorage == stDetect)
  912. {
  913. if (FindResourceEx(NULL, RT_RCDATA, L"WINSCP_SESSION",
  914. MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL)))
  915. {
  916. FTemporarySessionFile =
  917. IncludeTrailingBackslash(SystemTemporaryDirectory()) + L"winscps.tmp";
  918. DumpResourceToFile(L"WINSCP_SESSION", FTemporarySessionFile);
  919. FEmbeddedSessions = true;
  920. FTemporaryKeyFile =
  921. IncludeTrailingBackslash(SystemTemporaryDirectory()) + L"winscpk.tmp";
  922. if (!DumpResourceToFile(L"WINSCP_KEY", FTemporaryKeyFile))
  923. {
  924. FTemporaryKeyFile = L"";
  925. }
  926. }
  927. DebugCheck(DetectStorage(false));
  928. }
  929. // Meaning any inherited autodetection basically does not happen, so the below call returns FStorage
  930. DebugAssert(FStorage != stDetect);
  931. TStorage Result = TCustomWinConfiguration::GetStorage();
  932. return Result;
  933. }
  934. //---------------------------------------------------------------------------
  935. bool TWinConfiguration::TrySetSafeStorage()
  936. {
  937. return DetectStorage(true);
  938. }
  939. //---------------------------------------------------------------------------
  940. void __fastcall TWinConfiguration::Saved()
  941. {
  942. TCustomWinConfiguration::Saved();
  943. FBookmarks->ModifyAll(false);
  944. FCustomCommandList->Reset();
  945. FEditorList->Saved();
  946. }
  947. //---------------------------------------------------------------------------
  948. void __fastcall TWinConfiguration::RecryptPasswords(TStrings * RecryptPasswordErrors)
  949. {
  950. TCustomWinConfiguration::RecryptPasswords(RecryptPasswordErrors);
  951. try
  952. {
  953. TTerminalManager * Manager = TTerminalManager::Instance(false);
  954. DebugAssert(Manager != NULL);
  955. if (Manager != NULL)
  956. {
  957. Manager->RecryptPasswords();
  958. }
  959. }
  960. catch (Exception & E)
  961. {
  962. UnicodeString Message;
  963. if (ExceptionMessage(&E, Message))
  964. {
  965. // we do not expect this really to happen,
  966. // so we do not bother providing context
  967. RecryptPasswordErrors->Add(Message);
  968. }
  969. }
  970. }
  971. //---------------------------------------------------------------------------
  972. bool __fastcall TWinConfiguration::GetUseMasterPassword()
  973. {
  974. return FUseMasterPassword;
  975. }
  976. //---------------------------------------------------------------------------
  977. THierarchicalStorage * TWinConfiguration::CreateScpStorage(bool & SessionList)
  978. {
  979. // Detect embedded session, if not checked yet
  980. GetStorage();
  981. THierarchicalStorage * Result;
  982. if (SessionList && !FTemporarySessionFile.IsEmpty())
  983. {
  984. Result = TIniFileStorage::CreateFromPath(FTemporarySessionFile);
  985. // This is session-list specific store, so the only instance,
  986. // we do not reset the SessionList argument
  987. // (compare TConfiguration::CreateScpStorage)
  988. }
  989. else
  990. {
  991. Result = TCustomWinConfiguration::CreateScpStorage(SessionList);
  992. }
  993. return Result;
  994. }
  995. //---------------------------------------------------------------------------
  996. // duplicated from core\configuration.cpp
  997. #define BLOCK(KEY, CANCREATE, BLOCK) \
  998. if (Storage->OpenSubKeyPath(KEY, CANCREATE)) try { BLOCK } __finally { Storage->CloseSubKeyPath(); }
  999. #define KEY(TYPE, VAR) KEYEX(TYPE, VAR, PropertyToKey(TEXT(#VAR)))
  1000. #define REGCONFIG(CANCREATE) \
  1001. BLOCK(L"Interface", CANCREATE, \
  1002. KEYEX(Integer,DoubleClickAction, L"CopyOnDoubleClick"); \
  1003. KEY(Bool, CopyOnDoubleClickConfirmation); \
  1004. KEY(Bool, AlwaysRespectDoubleClickAction); \
  1005. KEY(Bool, DDDisableMove); \
  1006. KEYEX(Integer, DDTransferConfirmation, L"DDTransferConfirmation2"); \
  1007. KEY(String, DDTemporaryDirectory); \
  1008. KEY(String, DDDrives); \
  1009. KEY(Bool, DDWarnLackOfTempSpace); \
  1010. KEY(Float, DDWarnLackOfTempSpaceRatio); \
  1011. KEY(Bool, DeleteToRecycleBin); \
  1012. KEY(Bool, DimmHiddenFiles); \
  1013. KEY(Bool, RenameWholeName); \
  1014. KEY(Bool, SelectDirectories); \
  1015. KEY(String, SelectMask); \
  1016. KEY(Bool, ShowHiddenFiles); \
  1017. KEY(Integer, FormatSizeBytes); \
  1018. KEY(Integer, PanelSearch); \
  1019. KEY(Bool, ShowInaccesibleDirectories); \
  1020. KEY(Bool, ConfirmTransferring); \
  1021. KEY(Bool, ConfirmDeleting); \
  1022. KEY(Bool, ConfirmRecycling); \
  1023. KEY(Bool, ConfirmClosingSession); \
  1024. KEY(String, AutoStartSession); \
  1025. KEY(Bool, UseLocationProfiles); \
  1026. KEY(Bool, UseSharedBookmarks); \
  1027. KEY(Integer, LocaleSafe); \
  1028. KEY(Bool, DDFakeFile); \
  1029. KEY(Integer, DDExtTimeout); \
  1030. KEY(Bool, DefaultDirIsHome); \
  1031. KEY(Bool, TemporaryDirectoryAppendSession); \
  1032. KEY(Bool, TemporaryDirectoryAppendPath); \
  1033. KEY(Bool, TemporaryDirectoryDeterministic); \
  1034. KEY(Bool, TemporaryDirectoryCleanup); \
  1035. KEY(Bool, ConfirmTemporaryDirectoryCleanup); \
  1036. KEY(Bool, PreservePanelState); \
  1037. KEY(Integer, DarkTheme); \
  1038. KEY(String, LastStoredSession); \
  1039. KEY(Bool, AutoSaveWorkspace); \
  1040. KEY(Bool, AutoSaveWorkspacePasswords); \
  1041. KEY(String, AutoWorkspace); \
  1042. KEY(Integer, PathInCaption); \
  1043. KEY(Integer, SessionTabNameFormat); \
  1044. KEY(Bool, MinimizeToTray); \
  1045. KEY(Bool, BalloonNotifications); \
  1046. KEY(Integer, NotificationsTimeout); \
  1047. KEY(Integer, NotificationsStickTime); \
  1048. KEY(Bool, CopyParamAutoSelectNotice); \
  1049. KEY(Bool, LockToolbars); \
  1050. KEY(Bool, SelectiveToolbarText); \
  1051. KEY(Integer, LargerToolbar); \
  1052. KEY(Bool, AutoOpenInPutty); \
  1053. KEY(Bool, RefreshRemotePanel); \
  1054. KEY(DateTime, RefreshRemotePanelInterval); \
  1055. KEYEX(String, PanelFont.FontName, L"PanelFontName"); \
  1056. KEYEX(Integer,PanelFont.FontSize, L"PanelFontSize"); \
  1057. KEYEX(Integer,PanelFont.FontStyle, L"PanelFontStyle"); \
  1058. KEYEX(Integer,PanelFont.FontCharset, L"PanelFontCharset"); \
  1059. KEY(Bool, NaturalOrderNumericalSorting); \
  1060. KEY(Bool, AlwaysSortDirectoriesByName); \
  1061. KEY(Bool, FullRowSelect); \
  1062. KEY(Bool, OfferedEditorAutoConfig); \
  1063. KEY(Integer, LastMonitor); \
  1064. KEY(String, VersionHistory); \
  1065. KEY(Bool, EnableQueueByDefault); \
  1066. KEY(String, OpenedStoredSessionFolders); \
  1067. KEY(Bool, AutoImportedFromPuttyOrFilezilla); \
  1068. KEY(Integer, GenerateUrlComponents); \
  1069. KEY(Integer, GenerateUrlCodeTarget); \
  1070. KEY(Integer, GenerateUrlScriptFormat); \
  1071. KEY(Integer, GenerateUrlAssemblyLanguage); \
  1072. KEY(Bool, ExternalSessionInExistingInstance); \
  1073. KEY(Bool, ShowLoginWhenNoSession); \
  1074. KEY(Bool, KeepOpenWhenNoSession); \
  1075. KEY(Bool, DefaultToNewRemoteTab); \
  1076. KEY(Bool, LocalIconsByExt); \
  1077. KEY(Bool, FlashTaskbar); \
  1078. KEY(Integer, MaxSessions); \
  1079. KEY(Integer, BidiModeOverride); \
  1080. KEY(Integer, FlipChildrenOverride); \
  1081. KEY(Bool, ShowTips); \
  1082. KEY(String, TipsSeen); \
  1083. KEY(DateTime, TipsShown); \
  1084. KEY(String, FileColors); \
  1085. KEY(Integer, RunsSinceLastTip); \
  1086. KEY(Bool, HonorDrivePolicy); \
  1087. KEY(Bool, UseABDrives); \
  1088. KEY(Integer, LastMachineInstallations); \
  1089. KEY(String, FExtensionsDeleted); \
  1090. KEY(String, FExtensionsOrder); \
  1091. KEY(String, FExtensionsShortCuts); \
  1092. KEY(Bool, TimeoutShellOperations); \
  1093. KEY(Bool, TimeoutShellIconRetrieval); \
  1094. KEY(Bool, UseIconUpdateThread); \
  1095. KEY(Bool, AllowWindowPrint); \
  1096. KEY(Integer, StoreTransition); \
  1097. KEY(Integer, QueueTransferLimitMax); \
  1098. KEY(Bool, HiContrast); \
  1099. KEY(Bool, EditorCheckNotModified); \
  1100. KEY(Bool, SessionTabCaptionTruncation); \
  1101. KEY(Integer, LoadingTooLongLimit); \
  1102. KEY(String, RemoteThumbnailMask); \
  1103. KEY(Integer, RemoteThumbnailSizeLimit); \
  1104. KEY(String, FirstRun); \
  1105. ); \
  1106. BLOCK(L"Interface\\Editor", CANCREATE, \
  1107. KEYEX(String, Editor.Font.FontName, L"FontName2"); \
  1108. KEY(Integer, Editor.Font.FontSize); \
  1109. KEY(Integer, Editor.Font.FontStyle); \
  1110. KEY(Integer, Editor.Font.FontCharset); \
  1111. KEY(Integer, Editor.FontColor); \
  1112. KEY(Integer, Editor.BackgroundColor); \
  1113. KEY(Bool, Editor.WordWrap); \
  1114. KEY(String, Editor.FindText); \
  1115. KEY(String, Editor.ReplaceText); \
  1116. KEY(Bool, Editor.FindMatchCase); \
  1117. KEY(Bool, Editor.FindWholeWord); \
  1118. KEY(Bool, Editor.FindDown); \
  1119. KEY(Integer, Editor.TabSize); \
  1120. KEY(Integer, Editor.MaxEditors); \
  1121. KEY(Integer, Editor.EarlyClose); \
  1122. KEY(Bool, Editor.SDIShellEditor); \
  1123. KEY(String, Editor.WindowParams); \
  1124. KEY(Integer, Editor.Encoding); \
  1125. KEY(Bool, Editor.WarnOnEncodingFallback); \
  1126. KEY(Bool, Editor.WarnOrLargeFileSize); \
  1127. KEY(Bool, Editor.AutoFont); \
  1128. KEY(Bool, Editor.DisableSmoothScroll); \
  1129. ); \
  1130. BLOCK(L"Interface\\QueueView", CANCREATE, \
  1131. KEY(Integer, QueueView.Height); \
  1132. KEY(Integer, QueueView.HeightPixelsPerInch); \
  1133. KEY(String, QueueView.Layout); \
  1134. KEY(Integer, QueueView.Show); \
  1135. KEY(Integer, QueueView.LastHideShow); \
  1136. KEY(Bool, QueueView.ToolBar); \
  1137. KEY(Bool, QueueView.Label); \
  1138. KEY(Bool, QueueView.FileList); \
  1139. KEY(Integer, QueueView.FileListHeight); \
  1140. KEY(Integer, QueueView.FileListHeightPixelsPerInch); \
  1141. ); \
  1142. BLOCK(L"Interface\\Updates", CANCREATE, \
  1143. KEY(Integer, FUpdates.Period); \
  1144. KEY(DateTime, FUpdates.LastCheck); \
  1145. KEY(Integer, FUpdates.HaveResults); \
  1146. KEY(Integer, FUpdates.ShownResults); \
  1147. KEY(Integer, FUpdates.BetaVersions); \
  1148. KEY(Bool, FUpdates.ShowOnStartup); \
  1149. KEY(String, FUpdates.AuthenticationEmail); \
  1150. KEY(String, FUpdates.Mode); \
  1151. KEY(Integer, FUpdates.ConnectionType); \
  1152. KEY(String, FUpdates.ProxyHost); \
  1153. KEY(Integer, FUpdates.ProxyPort); \
  1154. KEY(Integer, FUpdates.Results.ForVersion); \
  1155. KEY(Integer, FUpdates.Results.Version); \
  1156. KEY(String, FUpdates.Results.Message); \
  1157. KEY(Integer, FUpdates.Results.Critical); \
  1158. KEY(String, FUpdates.Results.Release); \
  1159. KEY(Bool, FUpdates.Results.Disabled); \
  1160. KEY(String, FUpdates.Results.Url); \
  1161. KEY(String, FUpdates.Results.UrlButton); \
  1162. KEY(String, FUpdates.Results.NewsUrl); \
  1163. KEYEX(Integer,FUpdates.Results.NewsSize.Width, L"NewsWidth"); \
  1164. KEYEX(Integer,FUpdates.Results.NewsSize.Height, L"NewsHeight"); \
  1165. KEY(String, FUpdates.Results.DownloadUrl); \
  1166. KEY(Int64, FUpdates.Results.DownloadSize); \
  1167. KEY(String, FUpdates.Results.DownloadSha256); \
  1168. KEY(String, FUpdates.Results.AuthenticationError); \
  1169. KEY(Bool, FUpdates.Results.OpenGettingStarted); \
  1170. KEY(String, FUpdates.Results.DownloadingUrl); \
  1171. KEYEX(Integer,FUpdates.Results.TipsSize.Width, L"TipsWidth"); \
  1172. KEYEX(Integer,FUpdates.Results.TipsSize.Height, L"TipsHeight"); \
  1173. KEY(String, FUpdates.Results.TipsUrl); \
  1174. KEY(String, FUpdates.Results.Tips); \
  1175. KEY(Integer, FUpdates.Results.TipsIntervalDays); \
  1176. KEY(Integer, FUpdates.Results.TipsIntervalRuns); \
  1177. KEY(String, FUpdates.DotNetVersion); \
  1178. KEY(String, FUpdates.ConsoleVersion); \
  1179. ); \
  1180. BLOCK(L"Interface\\Explorer", CANCREATE, \
  1181. KEYEX(String, ScpExplorer.ToolbarsLayout, ToolbarsLayoutKey); \
  1182. KEY(String, ScpExplorer.ToolbarsButtons); \
  1183. KEY(String, ScpExplorer.DirViewParams); \
  1184. KEY(String, ScpExplorer.LastLocalTargetDirectory); \
  1185. KEY(Bool, ScpExplorer.SessionsTabs); \
  1186. KEY(Bool, ScpExplorer.StatusBar); \
  1187. KEY(String, ScpExplorer.WindowParams); \
  1188. KEY(Integer, ScpExplorer.ViewStyle); \
  1189. KEY(Bool, ScpExplorer.ShowFullAddress); \
  1190. KEY(Bool, ScpExplorer.DriveView); \
  1191. KEY(Integer, ScpExplorer.DriveViewWidth); \
  1192. KEY(Integer, ScpExplorer.DriveViewWidthPixelsPerInch); \
  1193. ); \
  1194. BLOCK(L"Interface\\Commander", CANCREATE, \
  1195. KEYEX(String, ScpCommander.ToolbarsLayout, ToolbarsLayoutKey); \
  1196. KEY(String, ScpCommander.ToolbarsButtons); \
  1197. KEY(Integer, ScpCommander.CurrentPanel); \
  1198. KEY(Float, ScpCommander.LocalPanelWidth); \
  1199. KEY(Bool, ScpCommander.SwappedPanels); \
  1200. KEY(Bool, ScpCommander.SessionsTabs); \
  1201. KEY(Bool, ScpCommander.StatusBar); \
  1202. KEY(String, ScpCommander.WindowParams); \
  1203. KEYEX(Integer, ScpCommander.NortonLikeMode, L"ExplorerStyleSelection"); \
  1204. KEY(Bool, ScpCommander.PreserveLocalDirectory); \
  1205. KEY(Bool, ScpCommander.CompareByTime); \
  1206. KEY(Bool, ScpCommander.CompareBySize); \
  1207. KEY(Bool, ScpCommander.TreeOnLeft); \
  1208. KEY(Bool, ScpCommander.ExplorerKeyboardShortcuts); \
  1209. KEY(Bool, ScpCommander.SystemContextMenu); \
  1210. ); \
  1211. BLOCK(L"Interface\\Commander\\LocalPanel", CANCREATE, \
  1212. KEY(String, ScpCommander.LocalPanel.DirViewParams); \
  1213. KEY(Integer, ScpCommander.LocalPanel.ViewStyle); \
  1214. KEY(Bool, ScpCommander.LocalPanel.StatusBar); \
  1215. KEY(Bool, ScpCommander.LocalPanel.DriveView); \
  1216. KEY(Integer, ScpCommander.LocalPanel.DriveViewHeight); \
  1217. KEY(Integer, ScpCommander.LocalPanel.DriveViewHeightPixelsPerInch); \
  1218. KEY(Integer, ScpCommander.LocalPanel.DriveViewWidth); \
  1219. KEY(Integer, ScpCommander.LocalPanel.DriveViewWidthPixelsPerInch); \
  1220. KEY(String, ScpCommander.LocalPanel.LastPath); \
  1221. ); \
  1222. BLOCK(L"Interface\\Commander\\RemotePanel", CANCREATE, \
  1223. KEY(String, ScpCommander.RemotePanel.DirViewParams); \
  1224. KEY(Integer, ScpCommander.RemotePanel.ViewStyle); \
  1225. KEY(Bool, ScpCommander.RemotePanel.StatusBar); \
  1226. KEY(Bool, ScpCommander.RemotePanel.DriveView); \
  1227. KEY(Integer, ScpCommander.RemotePanel.DriveViewHeight); \
  1228. KEY(Integer, ScpCommander.RemotePanel.DriveViewHeightPixelsPerInch); \
  1229. KEY(Integer, ScpCommander.RemotePanel.DriveViewWidth); \
  1230. KEY(Integer, ScpCommander.RemotePanel.DriveViewWidthPixelsPerInch); \
  1231. KEY(String, ScpCommander.RemotePanel.LastPath); \
  1232. ); \
  1233. BLOCK(L"Interface\\Commander\\OtherLocalPanel", CANCREATE, \
  1234. KEYEX(String, ScpCommander.OtherLocalPanelDirViewParams, L"DirViewParams"); \
  1235. KEYEX(Integer, ScpCommander.OtherLocalPanelViewStyle, L"ViewStyle"); \
  1236. KEYEX(String, ScpCommander.OtherLocalPanelLastPath, L"LastPath"); \
  1237. ); \
  1238. BLOCK(L"Security", CANCREATE, \
  1239. KEY(Bool, FUseMasterPassword); \
  1240. KEY(String, FMasterPasswordVerifier); \
  1241. );
  1242. //---------------------------------------------------------------------------
  1243. void __fastcall TWinConfiguration::SaveData(THierarchicalStorage * Storage, bool All)
  1244. {
  1245. TCustomWinConfiguration::SaveData(Storage, All);
  1246. // duplicated from core\configuration.cpp
  1247. #define KEYEX(TYPE, VAR, NAME) Storage->Write ## TYPE(NAME, VAR)
  1248. REGCONFIG(true);
  1249. #undef KEYEX
  1250. if (Storage->OpenSubKey(L"Bookmarks", true))
  1251. {
  1252. FBookmarks->Save(Storage, All);
  1253. Storage->CloseSubKey();
  1254. }
  1255. if ((All && !FCustomCommandsDefaults) || FCustomCommandList->Modified)
  1256. {
  1257. FCustomCommandList->Save(Storage);
  1258. if (FCustomCommandList->Count == 0)
  1259. {
  1260. Storage->WriteBool(L"CustomCommandsNone", true);
  1261. }
  1262. else
  1263. {
  1264. Storage->DeleteValue(L"CustomCommandsNone");
  1265. }
  1266. }
  1267. if ((All || FCustomCommandOptionsModified) &&
  1268. Storage->OpenSubKey(L"CustomCommandOptions", true))
  1269. {
  1270. Storage->ClearValues();
  1271. Storage->WriteValues(FCustomCommandOptions.get(), true);
  1272. Storage->CloseSubKey();
  1273. FCustomCommandOptionsModified = false;
  1274. }
  1275. if ((All || FEditorList->Modified) &&
  1276. Storage->OpenSubKeyPath(L"Interface\\Editor", true))
  1277. try
  1278. {
  1279. FEditorList->Save(Storage);
  1280. }
  1281. __finally
  1282. {
  1283. Storage->CloseSubKeyPath();
  1284. }
  1285. }
  1286. //---------------------------------------------------------------------------
  1287. void __fastcall TWinConfiguration::LoadFrom(THierarchicalStorage * Storage)
  1288. {
  1289. FLegacyEditor = new TEditorPreferences();
  1290. try
  1291. {
  1292. FLegacyEditor->LegacyDefaults();
  1293. TCustomWinConfiguration::LoadFrom(Storage);
  1294. // Following needs to be done even if there's no Configuration key in the storage,
  1295. // so it cannot be in LoadData
  1296. int EditorCount = FEditorList->Count;
  1297. if (EditorCount == 0)
  1298. {
  1299. TEditorPreferences * AlternativeEditor = NULL;
  1300. try
  1301. {
  1302. if (FLegacyEditor->Data->Editor == edInternal)
  1303. {
  1304. if (!FLegacyEditor->Data->ExternalEditor.IsEmpty())
  1305. {
  1306. AlternativeEditor = new TEditorPreferences(*FLegacyEditor);
  1307. AlternativeEditor->GetData()->Editor = edExternal;
  1308. FLegacyEditor->GetData()->ExternalEditor = L"";
  1309. }
  1310. }
  1311. else
  1312. {
  1313. if (FLegacyEditor->Data->ExternalEditor.IsEmpty())
  1314. {
  1315. FLegacyEditor->GetData()->Editor = edInternal;
  1316. }
  1317. else
  1318. {
  1319. AlternativeEditor = new TEditorPreferences(*FLegacyEditor);
  1320. AlternativeEditor->GetData()->Editor = edInternal;
  1321. }
  1322. }
  1323. }
  1324. catch(...)
  1325. {
  1326. delete AlternativeEditor;
  1327. throw;
  1328. }
  1329. FEditorList->Add(FLegacyEditor);
  1330. FLegacyEditor = NULL;
  1331. if (AlternativeEditor != NULL)
  1332. {
  1333. FEditorList->Add(AlternativeEditor);
  1334. }
  1335. }
  1336. // Additionally, this needs to be after Locale is loaded
  1337. LoadExtensionTranslations();
  1338. // and this after the ExtensionsDeleted, ExtensionsOrder and ExtensionsShortCuts are loaded
  1339. LoadExtensionList();
  1340. }
  1341. __finally
  1342. {
  1343. delete FLegacyEditor;
  1344. FLegacyEditor = NULL;
  1345. }
  1346. if (FUpdates.ConnectionType == -1)
  1347. {
  1348. FUpdates.ConnectionType = (FUpdates.ProxyHost.IsEmpty() ? ctAuto : ctProxy);
  1349. }
  1350. AddVersionToHistory();
  1351. }
  1352. //---------------------------------------------------------------------------
  1353. void __fastcall TWinConfiguration::DoLoadExtensionList(
  1354. const UnicodeString & Path, const UnicodeString & PathId, TStringList * DeletedExtensions)
  1355. {
  1356. TSearchRecOwned SearchRec;
  1357. int FindAttrs = faReadOnly | faArchive;
  1358. if (FindFirstUnchecked(IncludeTrailingBackslash(Path) + L"*.*", FindAttrs, SearchRec) == 0)
  1359. {
  1360. do
  1361. {
  1362. UnicodeString Id = TCustomCommandType::GetExtensionId(SearchRec.Name);
  1363. if (!Id.IsEmpty())
  1364. {
  1365. Id = IncludeTrailingBackslash(PathId) + Id;
  1366. if (DeletedExtensions->IndexOf(Id) >= 0)
  1367. {
  1368. // reconstruct the list, so that we remove the commands that no longer exists
  1369. AddToList(FExtensionsDeleted, Id, L"|");
  1370. }
  1371. else
  1372. {
  1373. std::unique_ptr<TCustomCommandType> CustomCommand(new TCustomCommandType());
  1374. CustomCommand->Id = Id;
  1375. try
  1376. {
  1377. CustomCommand->LoadExtension(SearchRec.GetFilePath());
  1378. FExtensionList->Add(CustomCommand.release());
  1379. }
  1380. catch (...)
  1381. {
  1382. // skip invalid extension files
  1383. }
  1384. }
  1385. }
  1386. }
  1387. while (FindNextChecked(SearchRec) == 0);
  1388. }
  1389. }
  1390. //---------------------------------------------------------------------------
  1391. void __fastcall ParseExtensionList(TStrings * Strings, UnicodeString S)
  1392. {
  1393. while (!S.IsEmpty())
  1394. {
  1395. UnicodeString Extension = CutToChar(S, L'|', false);
  1396. Strings->Add(Extension);
  1397. }
  1398. }
  1399. //---------------------------------------------------------------------------
  1400. const UnicodeString ExtensionsSubFolder(L"Extensions");
  1401. const UnicodeString ExtensionsCommonPathId(L"common");
  1402. const UnicodeString ExtensionsCommonExtPathId(L"commonext");
  1403. const UnicodeString ExtensionsUserExtPathId(L"userext");
  1404. //---------------------------------------------------------------------------
  1405. static UnicodeString __fastcall ExtractExtensionBaseName(const UnicodeString & FileName)
  1406. {
  1407. UnicodeString S = ExtractFileName(FileName);
  1408. // ExtractFileNameOnly trims the last extension only, we want to trim all extensions
  1409. UnicodeString Result = CutToChar(S, L'.', true);
  1410. return Result;
  1411. }
  1412. //---------------------------------------------------------------------------
  1413. UnicodeString __fastcall TWinConfiguration::GetProvisionaryExtensionId(const UnicodeString & FileName)
  1414. {
  1415. return IncludeTrailingBackslash(ExtensionsUserExtPathId) + ExtractExtensionBaseName(FileName);
  1416. }
  1417. //---------------------------------------------------------------------------
  1418. UnicodeString __fastcall TWinConfiguration::GetUserExtensionsPath()
  1419. {
  1420. return IncludeTrailingBackslash(GetShellFolderPath(CSIDL_APPDATA)) + L"WinSCP\\" + ExtensionsSubFolder;
  1421. }
  1422. //---------------------------------------------------------------------------
  1423. TStrings * __fastcall TWinConfiguration::GetExtensionsPaths()
  1424. {
  1425. std::unique_ptr<TStrings> Result(new TStringList());
  1426. UnicodeString ExeParentPath = ExcludeTrailingBackslash(ExtractFilePath(Application->ExeName));
  1427. Result->Values[ExtensionsCommonPathId] = ExeParentPath;
  1428. UnicodeString CommonExtensions = IncludeTrailingBackslash(ExeParentPath) + ExtensionsSubFolder;
  1429. Result->Values[ExtensionsCommonExtPathId] = CommonExtensions;
  1430. Result->Values[ExtensionsUserExtPathId] = GetUserExtensionsPath();
  1431. return Result.release();
  1432. }
  1433. //---------------------------------------------------------------------------
  1434. UnicodeString __fastcall TWinConfiguration::GetExtensionId(const UnicodeString & ExtensionPath)
  1435. {
  1436. UnicodeString Path = ExcludeTrailingBackslash(ExtractFilePath(ExtensionPath));
  1437. UnicodeString NameId = TCustomCommandType::GetExtensionId(ExtractFileName(ExtensionPath));
  1438. if (!NameId.IsEmpty())
  1439. {
  1440. std::unique_ptr<TStrings> ExtensionsPaths(GetExtensionsPaths());
  1441. for (int Index = 0; Index < ExtensionsPaths->Count; Index++)
  1442. {
  1443. if (IsPathToSameFile(Path, ExtensionsPaths->ValueFromIndex[Index]))
  1444. {
  1445. return IncludeTrailingBackslash(ExtensionsPaths->Names[Index]) + NameId;
  1446. }
  1447. }
  1448. }
  1449. return L"";
  1450. }
  1451. //---------------------------------------------------------------------------
  1452. void __fastcall TWinConfiguration::ReleaseExtensionTranslations()
  1453. {
  1454. for (int Index = 0; Index < FExtensionTranslations->Count; Index++)
  1455. {
  1456. delete FExtensionTranslations->Objects[Index];
  1457. }
  1458. FExtensionTranslations->Clear();
  1459. }
  1460. //---------------------------------------------------------------------------
  1461. void __fastcall TWinConfiguration::LoadExtensionTranslations()
  1462. {
  1463. ReleaseExtensionTranslations();
  1464. FExtensionTranslations.reset(new TStringList());
  1465. int Index = EXTENSION_STRINGS;
  1466. UnicodeString S;
  1467. while (!((S = LoadStr(Index)).IsEmpty()))
  1468. {
  1469. UnicodeString ExtensionName = CutToChar(S, L'.', false);
  1470. UnicodeString Original = CutToChar(S, L'=', false);
  1471. UnicodeString Translation = S;
  1472. int ExtensionIndex = FExtensionTranslations->IndexOf(ExtensionName);
  1473. if (ExtensionIndex < 0)
  1474. {
  1475. ExtensionIndex = FExtensionTranslations->AddObject(ExtensionName, new TStringList());
  1476. }
  1477. TStringList * ExtensionTranslation = DebugNotNull(dynamic_cast<TStringList *>(FExtensionTranslations->Objects[ExtensionIndex]));
  1478. ExtensionTranslation->Values[Original] = Translation;
  1479. Index++;
  1480. }
  1481. }
  1482. //---------------------------------------------------------------------------
  1483. UnicodeString __fastcall TWinConfiguration::UniqueExtensionName(const UnicodeString & ExtensionName, int Counter)
  1484. {
  1485. // See how the digits are removed in the ExtensionStringTranslation
  1486. return ExtensionName + IntToStr(Counter);
  1487. }
  1488. //---------------------------------------------------------------------------
  1489. UnicodeString __fastcall TWinConfiguration::ExtensionStringTranslation(const UnicodeString & ExtensionId, const UnicodeString & S)
  1490. {
  1491. UnicodeString Result = S;
  1492. if (!ExtensionId.IsEmpty())
  1493. {
  1494. UnicodeString ExtensionName = ExtractFileName(ExtensionId);
  1495. int ExtensionIndex;
  1496. bool Retry;
  1497. do
  1498. {
  1499. ExtensionIndex = FExtensionTranslations->IndexOf(ExtensionName);
  1500. // Try without trailing digits, possibly added by UniqueExtensionName
  1501. Retry = (ExtensionIndex < 0) && !ExtensionName.IsEmpty() && IsDigit(*ExtensionName.LastChar());
  1502. if (Retry)
  1503. {
  1504. ExtensionName.SetLength(ExtensionName.Length() - 1);
  1505. }
  1506. }
  1507. while (Retry);
  1508. if (ExtensionIndex >= 0)
  1509. {
  1510. TStrings * ExtensionTranslation = DebugNotNull(dynamic_cast<TStrings *>(FExtensionTranslations->Objects[ExtensionIndex]));
  1511. int StringIndex = ExtensionTranslation->IndexOfName(S);
  1512. if (StringIndex >= 0)
  1513. {
  1514. Result = ExtensionTranslation->ValueFromIndex[StringIndex];
  1515. }
  1516. }
  1517. }
  1518. return Result;
  1519. }
  1520. //---------------------------------------------------------------------------
  1521. void __fastcall TWinConfiguration::LoadExtensionList()
  1522. {
  1523. FExtensionList->Clear();
  1524. std::unique_ptr<TStringList> DeletedExtensions(CreateSortedStringList());
  1525. ParseExtensionList(DeletedExtensions.get(), FExtensionsDeleted);
  1526. // will reconstruct the list in DoLoadExtensionList
  1527. FExtensionsDeleted = L"";
  1528. std::unique_ptr<TStrings> ExtensionsPaths(GetExtensionsPaths());
  1529. for (int Index = 0; Index < ExtensionsPaths->Count; Index++)
  1530. {
  1531. DoLoadExtensionList(ExtensionsPaths->ValueFromIndex[Index], ExtensionsPaths->Names[Index], DeletedExtensions.get());
  1532. }
  1533. std::unique_ptr<TStringList> OrderedExtensions(new TStringList());
  1534. ParseExtensionList(OrderedExtensions.get(), FExtensionsOrder);
  1535. FExtensionList->SortBy(OrderedExtensions.get());
  1536. UnicodeString ShortCuts = FExtensionsShortCuts;
  1537. while (!ShortCuts.IsEmpty())
  1538. {
  1539. UnicodeString S = CutToChar(ShortCuts, L'|', false);
  1540. TShortCut ShortCut = static_cast<TShortCut>(StrToInt(CutToChar(S, L'=', false)));
  1541. for (int Index = 0; Index < FExtensionList->Count; Index++)
  1542. {
  1543. if (FExtensionList->Commands[Index]->Id == S)
  1544. {
  1545. const_cast<TCustomCommandType *>(FExtensionList->Commands[Index])->ShortCut = ShortCut;
  1546. }
  1547. }
  1548. }
  1549. }
  1550. //---------------------------------------------------------------------------
  1551. static UnicodeString KeyName(THierarchicalStorage * Storage, const UnicodeString & Name)
  1552. {
  1553. UnicodeString Result = Name;
  1554. if ((Result == ToolbarsLayoutKey) && !Storage->KeyExists(Result) && Storage->KeyExists(ToolbarsLayoutOldKey))
  1555. {
  1556. Result = ToolbarsLayoutOldKey;
  1557. }
  1558. return Result;
  1559. }
  1560. //---------------------------------------------------------------------------
  1561. void __fastcall TWinConfiguration::LoadData(THierarchicalStorage * Storage)
  1562. {
  1563. TCustomWinConfiguration::LoadData(Storage);
  1564. // duplicated from core\configuration.cpp
  1565. #define KEYEX(TYPE, VAR, NAME) VAR = Storage->Read ## TYPE(KeyName(Storage, NAME), VAR)
  1566. #pragma warn -eas
  1567. REGCONFIG(false);
  1568. #pragma warn +eas
  1569. #undef KEYEX
  1570. // to reflect changes to PanelFont
  1571. UpdateIconFont();
  1572. if (Storage->OpenSubKey(L"Bookmarks", false))
  1573. {
  1574. FBookmarks->Load(Storage);
  1575. Storage->CloseSubKey();
  1576. }
  1577. if (Storage->KeyExists(L"CustomCommands") ||
  1578. Storage->ReadBool(L"CustomCommandsNone", false))
  1579. {
  1580. FCustomCommandList->Load(Storage);
  1581. FCustomCommandsDefaults = false;
  1582. }
  1583. else if (FCustomCommandList->Modified)
  1584. {
  1585. // can this (=reloading of configuration) even happen?
  1586. // if it does, shouldn't we reset default commands?
  1587. DebugFail();
  1588. FCustomCommandList->Clear();
  1589. FCustomCommandsDefaults = false;
  1590. }
  1591. FCustomCommandList->Reset();
  1592. if (Storage->OpenSubKey(L"CustomCommandOptions", false))
  1593. {
  1594. Storage->ReadValues(FCustomCommandOptions.get(), true);
  1595. Storage->CloseSubKey();
  1596. FCustomCommandOptionsModified = false;
  1597. }
  1598. if (Storage->OpenSubKeyPath(L"Interface\\Editor", false))
  1599. try
  1600. {
  1601. FEditorList->Clear();
  1602. FEditorList->Load(Storage);
  1603. }
  1604. __finally
  1605. {
  1606. Storage->CloseSubKeyPath();
  1607. }
  1608. // load legacy editor configuration
  1609. DebugAssert(FLegacyEditor != NULL);
  1610. if (Storage->OpenSubKeyPath(L"Interface\\Editor", false))
  1611. {
  1612. try
  1613. {
  1614. FLegacyEditor->Load(Storage, true);
  1615. }
  1616. __finally
  1617. {
  1618. Storage->CloseSubKeyPath();
  1619. }
  1620. }
  1621. }
  1622. //---------------------------------------------------------------------------
  1623. void __fastcall TWinConfiguration::LoadAdmin(THierarchicalStorage * Storage)
  1624. {
  1625. TCustomWinConfiguration::LoadAdmin(Storage);
  1626. FDisableOpenEdit = Storage->ReadBool(L"DisableOpenEdit", FDisableOpenEdit);
  1627. FDefaultUpdatesPeriod = Storage->ReadInteger(L"DefaultUpdatesPeriod", FDefaultUpdatesPeriod);
  1628. FMachineInstallations = Storage->ReadInteger(L"Installations", 0);
  1629. }
  1630. //---------------------------------------------------------------------------
  1631. void __fastcall TWinConfiguration::CopyData(THierarchicalStorage * Source, THierarchicalStorage * Target)
  1632. {
  1633. TCustomWinConfiguration::CopyData(Source, Target);
  1634. if (CopySubKey(Source, Target, ConfigurationSubKey))
  1635. {
  1636. Target->WriteString(L"JumpList", Source->ReadString(L"JumpList", L""));
  1637. Target->WriteString(L"JumpListWorkspaces", Source->ReadString(L"JumpListWorkspaces", L""));
  1638. Target->CloseSubKey();
  1639. Source->CloseSubKey();
  1640. }
  1641. }
  1642. //---------------------------------------------------------------------------
  1643. void __fastcall TWinConfiguration::ClearTemporaryLoginData()
  1644. {
  1645. if (!FTemporaryKeyFile.IsEmpty())
  1646. {
  1647. DeleteFile(ApiPath(FTemporaryKeyFile));
  1648. FTemporaryKeyFile = L"";
  1649. }
  1650. }
  1651. //---------------------------------------------------------------------------
  1652. void __fastcall TWinConfiguration::AddVersionToHistory()
  1653. {
  1654. int CurrentVersion = CompoundVersion;
  1655. DebugAssert(ZeroBuildNumber(CurrentVersion) == CurrentVersion);
  1656. int From = 1;
  1657. bool CurrentVersionPresent = false;
  1658. while (!CurrentVersionPresent && (From < FVersionHistory.Length()))
  1659. {
  1660. UnicodeString VersionInfo = CopyToChars(FVersionHistory, From, L";", true);
  1661. UnicodeString VersionStr = CutToChar(VersionInfo, L',', true);
  1662. int Version;
  1663. if (TryStrToInt(VersionStr, Version))
  1664. {
  1665. Version = ZeroBuildNumber(Version);
  1666. if (Version == CurrentVersion)
  1667. {
  1668. CurrentVersionPresent = true;
  1669. }
  1670. }
  1671. }
  1672. if (!CurrentVersionPresent)
  1673. {
  1674. UnicodeString CurrentVersionInfo =
  1675. IntToStr(CurrentVersion) + L"," + GetReleaseType();
  1676. AddToList(FVersionHistory, CurrentVersionInfo, L';');
  1677. }
  1678. Usage->Set(L"AnyBetaUsed", AnyBetaInVersionHistory);
  1679. }
  1680. //---------------------------------------------------------------------------
  1681. bool __fastcall TWinConfiguration::DoIsBeta(const UnicodeString & ReleaseType)
  1682. {
  1683. // What about "Development" release type?
  1684. return SameText(ReleaseType, L"beta") || SameText(ReleaseType, L"rc");
  1685. }
  1686. //---------------------------------------------------------------------------
  1687. bool __fastcall TWinConfiguration::GetIsBeta()
  1688. {
  1689. return DoIsBeta(GetReleaseType());
  1690. }
  1691. //---------------------------------------------------------------------------
  1692. bool __fastcall TWinConfiguration::GetAnyBetaInVersionHistory()
  1693. {
  1694. int From = 1;
  1695. bool AnyBeta = false;
  1696. while (!AnyBeta && (From < VersionHistory.Length()))
  1697. {
  1698. UnicodeString VersionInfo = CopyToChars(VersionHistory, From, L";", true);
  1699. CutToChar(VersionInfo, L',', true);
  1700. UnicodeString ReleaseType = CutToChar(VersionInfo, ',', true);
  1701. if (DoIsBeta(ReleaseType))
  1702. {
  1703. AnyBeta = true;
  1704. }
  1705. }
  1706. return AnyBeta;
  1707. }
  1708. //---------------------------------------------------------------------------
  1709. bool __fastcall TWinConfiguration::GetDDExtInstalled()
  1710. {
  1711. if (FDDExtInstalled < 0)
  1712. {
  1713. if (IsWin64())
  1714. {
  1715. // WORKAROUND
  1716. // We cannot load 64-bit COM class into 32-bit process,
  1717. // so we fallback to querying registration keys
  1718. #define CLSID_SIZE 39
  1719. wchar_t ClassID[CLSID_SIZE];
  1720. StringFromGUID2(CLSID_ShellExtension, ClassID, LENOF(ClassID));
  1721. NULL_TERMINATE(ClassID);
  1722. UnicodeString SubKey = UnicodeString(L"CLSID\\") + ClassID;
  1723. HKEY HKey;
  1724. LONG Result = RegOpenKeyEx(HKEY_CLASSES_ROOT, SubKey.c_str(), 0, KEY_READ | KEY_WOW64_64KEY, &HKey);
  1725. if (Result == ERROR_SUCCESS)
  1726. {
  1727. RegCloseKey(HKey);
  1728. FDDExtInstalled = 1;
  1729. }
  1730. else
  1731. {
  1732. FDDExtInstalled = 0;
  1733. }
  1734. }
  1735. else
  1736. {
  1737. void * DragExtRef;
  1738. int CreateResult =
  1739. CoCreateInstance(CLSID_ShellExtension, NULL,
  1740. CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, IID_IUnknown,
  1741. &DragExtRef);
  1742. bool Result = (CreateResult == S_OK);
  1743. FDDExtInstalled = (Result ? 1 : 0);
  1744. if (Result)
  1745. {
  1746. reinterpret_cast<IUnknown *>(DragExtRef)->Release();
  1747. CoFreeUnusedLibraries();
  1748. }
  1749. }
  1750. }
  1751. return (FDDExtInstalled > 0);
  1752. }
  1753. //---------------------------------------------------------------------------
  1754. bool __fastcall TWinConfiguration::IsDDExtRunning()
  1755. {
  1756. bool Result;
  1757. if (!DDExtInstalled)
  1758. {
  1759. Result = false;
  1760. }
  1761. else
  1762. {
  1763. HANDLE H = OpenMutex(SYNCHRONIZE, False, DRAG_EXT_RUNNING_MUTEX);
  1764. Result = (H != NULL);
  1765. CloseHandle(H);
  1766. }
  1767. return Result;
  1768. }
  1769. //---------------------------------------------------------------------------
  1770. bool __fastcall TWinConfiguration::IsDDExtBroken()
  1771. {
  1772. int Build = GetWindowsBuild();
  1773. return (Build >= 17134) && (Build < 17763);
  1774. }
  1775. //---------------------------------------------------------------------------
  1776. RawByteString __fastcall TWinConfiguration::StronglyRecryptPassword(RawByteString Password, UnicodeString Key)
  1777. {
  1778. RawByteString Dummy;
  1779. RawByteString Result;
  1780. if (GetExternalEncryptedPassword(Password, Dummy) ||
  1781. !FUseMasterPassword)
  1782. {
  1783. // already-strongly encrypted
  1784. // or no master password set, so we cannot strongly-encrypt it
  1785. Result = Password;
  1786. }
  1787. else
  1788. {
  1789. UnicodeString PasswordText =
  1790. TCustomWinConfiguration::DecryptPassword(Password, Key);
  1791. if (!PasswordText.IsEmpty())
  1792. {
  1793. // Can be not set for instance, when editing=>saving site with no prior password.
  1794. // Though it should not actually happen, as we call AskForMasterPasswordIfNotSetAndNeededToPersistSessionData in DoSaveSession.
  1795. AskForMasterPasswordIfNotSet();
  1796. Password = ScramblePassword(PasswordText);
  1797. AES256EncyptWithMAC(Password, FPlainMasterPasswordEncrypt, Result);
  1798. Result = SetExternalEncryptedPassword(Result);
  1799. }
  1800. }
  1801. return Result;
  1802. }
  1803. //---------------------------------------------------------------------------
  1804. UnicodeString __fastcall TWinConfiguration::DecryptPassword(RawByteString Password, UnicodeString Key)
  1805. {
  1806. UnicodeString Result;
  1807. RawByteString Buf;
  1808. if (GetExternalEncryptedPassword(Password, Buf))
  1809. {
  1810. if (FDontDecryptPasswords == 0)
  1811. {
  1812. // As opposite to AskForMasterPasswordIfNotSet, we test here
  1813. // for decrypt password. This is important while recrypting password,
  1814. // when clearing master password, when encrypt password is already empty.
  1815. if (FPlainMasterPasswordDecrypt.IsEmpty())
  1816. {
  1817. AskForMasterPassword();
  1818. }
  1819. if (!AES256DecryptWithMAC(Buf, FPlainMasterPasswordDecrypt, Buf) ||
  1820. !UnscramblePassword(Buf, Result))
  1821. {
  1822. throw Exception(LoadStr(DECRYPT_PASSWORD_ERROR));
  1823. }
  1824. }
  1825. else
  1826. {
  1827. Abort();
  1828. }
  1829. }
  1830. else
  1831. {
  1832. Result = TCustomWinConfiguration::DecryptPassword(Password, Key);
  1833. }
  1834. return Result;
  1835. }
  1836. //---------------------------------------------------------------------------
  1837. void __fastcall TWinConfiguration::SetMasterPassword(UnicodeString value)
  1838. {
  1839. // just stores the plain-text version of the password
  1840. // we can get here even if master password is off,
  1841. // when we encounter stray encrypted password (e.g. manually imported site),
  1842. // make sure we do not set encrypt password to avoid starting encrypting
  1843. // new passwords
  1844. // (this is bit of edge case, not really well tested)
  1845. if (!FUseMasterPassword)
  1846. {
  1847. FPlainMasterPasswordDecrypt = value;
  1848. }
  1849. else if (DebugAlwaysTrue(FUseMasterPassword) &&
  1850. DebugAlwaysTrue(ValidateMasterPassword(value)))
  1851. {
  1852. FPlainMasterPasswordEncrypt = value;
  1853. FPlainMasterPasswordDecrypt = value;
  1854. }
  1855. }
  1856. //---------------------------------------------------------------------------
  1857. void __fastcall TWinConfiguration::ChangeMasterPassword(
  1858. UnicodeString value, TStrings * RecryptPasswordErrors)
  1859. {
  1860. RawByteString Verifier;
  1861. AES256CreateVerifier(value, Verifier);
  1862. FMasterPasswordVerifier = BytesToHex(Verifier);
  1863. FPlainMasterPasswordEncrypt = value;
  1864. FUseMasterPassword = true;
  1865. try
  1866. {
  1867. RecryptPasswords(RecryptPasswordErrors);
  1868. }
  1869. __finally
  1870. {
  1871. FPlainMasterPasswordDecrypt = value;
  1872. }
  1873. }
  1874. //---------------------------------------------------------------------------
  1875. bool __fastcall TWinConfiguration::ValidateMasterPassword(UnicodeString value)
  1876. {
  1877. DebugAssert(UseMasterPassword);
  1878. DebugAssert(!FMasterPasswordVerifier.IsEmpty());
  1879. bool Result = AES256Verify(value, HexToBytes(FMasterPasswordVerifier));
  1880. return Result;
  1881. }
  1882. //---------------------------------------------------------------------------
  1883. void __fastcall TWinConfiguration::ClearMasterPassword(TStrings * RecryptPasswordErrors)
  1884. {
  1885. FMasterPasswordVerifier = L"";
  1886. FUseMasterPassword = false;
  1887. Shred(FPlainMasterPasswordEncrypt);
  1888. try
  1889. {
  1890. RecryptPasswords(RecryptPasswordErrors);
  1891. }
  1892. __finally
  1893. {
  1894. Shred(FPlainMasterPasswordDecrypt);
  1895. }
  1896. }
  1897. //---------------------------------------------------------------------------
  1898. void __fastcall TWinConfiguration::AskForMasterPassword()
  1899. {
  1900. if (FMasterPasswordSession > 0)
  1901. {
  1902. if (FMasterPasswordSessionAsked)
  1903. {
  1904. Abort();
  1905. }
  1906. // set before call to OnMasterPasswordPrompt as it may abort
  1907. FMasterPasswordSessionAsked = true;
  1908. }
  1909. if (FOnMasterPasswordPrompt == NULL)
  1910. {
  1911. throw Exception(L"Master password handler not set");
  1912. }
  1913. else
  1914. {
  1915. FOnMasterPasswordPrompt();
  1916. DebugAssert(!FPlainMasterPasswordDecrypt.IsEmpty());
  1917. }
  1918. }
  1919. //---------------------------------------------------------------------------
  1920. void __fastcall TWinConfiguration::AskForMasterPasswordIfNotSet()
  1921. {
  1922. if (FPlainMasterPasswordEncrypt.IsEmpty())
  1923. {
  1924. AskForMasterPassword();
  1925. }
  1926. }
  1927. //---------------------------------------------------------------------------
  1928. void __fastcall TWinConfiguration::BeginMasterPasswordSession()
  1929. {
  1930. // We do not expect nesting
  1931. DebugAssert(FMasterPasswordSession == 0);
  1932. DebugAssert(!FMasterPasswordSessionAsked || (FMasterPasswordSession > 0));
  1933. // This should better be thread-specific
  1934. FMasterPasswordSession++;
  1935. }
  1936. //---------------------------------------------------------------------------
  1937. void __fastcall TWinConfiguration::EndMasterPasswordSession()
  1938. {
  1939. if (DebugAlwaysTrue(FMasterPasswordSession > 0))
  1940. {
  1941. FMasterPasswordSession--;
  1942. }
  1943. FMasterPasswordSessionAsked = false;
  1944. }
  1945. //---------------------------------------------------------------------------
  1946. void __fastcall TWinConfiguration::SetDDDisableMove(bool value)
  1947. {
  1948. SET_CONFIG_PROPERTY(DDDisableMove);
  1949. }
  1950. //---------------------------------------------------------------------------
  1951. void __fastcall TWinConfiguration::SetDDTransferConfirmation(TAutoSwitch value)
  1952. {
  1953. SET_CONFIG_PROPERTY(DDTransferConfirmation);
  1954. }
  1955. //---------------------------------------------------------------------------
  1956. void __fastcall TWinConfiguration::SetDDTemporaryDirectory(UnicodeString value)
  1957. {
  1958. SET_CONFIG_PROPERTY(DDTemporaryDirectory);
  1959. }
  1960. //---------------------------------------------------------------------------
  1961. void __fastcall TWinConfiguration::SetDDDrives(UnicodeString value)
  1962. {
  1963. SET_CONFIG_PROPERTY(DDDrives);
  1964. }
  1965. //---------------------------------------------------------------------------
  1966. void __fastcall TWinConfiguration::SetDDFakeFile(bool value)
  1967. {
  1968. SET_CONFIG_PROPERTY(DDFakeFile);
  1969. }
  1970. //---------------------------------------------------------------------------
  1971. void __fastcall TWinConfiguration::SetDDExtTimeout(int value)
  1972. {
  1973. SET_CONFIG_PROPERTY(DDExtTimeout);
  1974. }
  1975. //---------------------------------------------------------------------------
  1976. void __fastcall TWinConfiguration::SetDDWarnLackOfTempSpace(bool value)
  1977. {
  1978. SET_CONFIG_PROPERTY(DDWarnLackOfTempSpace);
  1979. }
  1980. //---------------------------------------------------------------------------
  1981. void __fastcall TWinConfiguration::SetDDWarnLackOfTempSpaceRatio(double value)
  1982. {
  1983. SET_CONFIG_PROPERTY(DDWarnLackOfTempSpaceRatio);
  1984. }
  1985. //---------------------------------------------------------------------------
  1986. void __fastcall TWinConfiguration::SetScpExplorer(TScpExplorerConfiguration value)
  1987. {
  1988. SET_CONFIG_PROPERTY(ScpExplorer);
  1989. }
  1990. //---------------------------------------------------------------------------
  1991. void __fastcall TWinConfiguration::SetScpCommander(TScpCommanderConfiguration value)
  1992. {
  1993. SET_CONFIG_PROPERTY(ScpCommander);
  1994. }
  1995. //---------------------------------------------------------------------------
  1996. void __fastcall TWinConfiguration::SetEditor(TEditorConfiguration value)
  1997. {
  1998. SET_CONFIG_PROPERTY(Editor);
  1999. }
  2000. //---------------------------------------------------------------------------
  2001. void __fastcall TWinConfiguration::SetQueueView(TQueueViewConfiguration value)
  2002. {
  2003. SET_CONFIG_PROPERTY(QueueView);
  2004. }
  2005. //---------------------------------------------------------------------------
  2006. void __fastcall TWinConfiguration::SetEnableQueueByDefault(bool value)
  2007. {
  2008. SET_CONFIG_PROPERTY(EnableQueueByDefault);
  2009. }
  2010. //---------------------------------------------------------------------------
  2011. TUpdatesConfiguration __fastcall TWinConfiguration::GetUpdates()
  2012. {
  2013. TUpdatesConfiguration Result;
  2014. {
  2015. TGuard Guard(FCriticalSection);
  2016. Result = FUpdates;
  2017. }
  2018. return Result;
  2019. }
  2020. //---------------------------------------------------------------------------
  2021. void __fastcall TWinConfiguration::SetUpdates(TUpdatesConfiguration value)
  2022. {
  2023. TGuard Guard(FCriticalSection);
  2024. // do not use SET_CONFIG_PROPERTY to avoid OnChange handler call (not synchronized)
  2025. FUpdates = value;
  2026. }
  2027. //---------------------------------------------------------------------------
  2028. void __fastcall TWinConfiguration::SetVersionHistory(UnicodeString value)
  2029. {
  2030. SET_CONFIG_PROPERTY(VersionHistory);
  2031. }
  2032. //---------------------------------------------------------------------------
  2033. void __fastcall TWinConfiguration::SetDeleteToRecycleBin(bool value)
  2034. {
  2035. SET_CONFIG_PROPERTY(DeleteToRecycleBin);
  2036. }
  2037. //---------------------------------------------------------------------------
  2038. void __fastcall TWinConfiguration::SetSelectDirectories(bool value)
  2039. {
  2040. SET_CONFIG_PROPERTY(SelectDirectories);
  2041. }
  2042. //---------------------------------------------------------------------------
  2043. void __fastcall TWinConfiguration::SetShowHiddenFiles(bool value)
  2044. {
  2045. SET_CONFIG_PROPERTY(ShowHiddenFiles);
  2046. }
  2047. //---------------------------------------------------------------------------
  2048. void __fastcall TWinConfiguration::SetFormatSizeBytes(TFormatBytesStyle value)
  2049. {
  2050. SET_CONFIG_PROPERTY(FormatSizeBytes);
  2051. }
  2052. //---------------------------------------------------------------------------
  2053. void __fastcall TWinConfiguration::SetPanelSearch(TIncrementalSearch value)
  2054. {
  2055. SET_CONFIG_PROPERTY(PanelSearch);
  2056. }
  2057. //---------------------------------------------------------------------------
  2058. void __fastcall TWinConfiguration::SetShowInaccesibleDirectories(bool value)
  2059. {
  2060. SET_CONFIG_PROPERTY(ShowInaccesibleDirectories);
  2061. }
  2062. //---------------------------------------------------------------------------
  2063. void __fastcall TWinConfiguration::SetConfirmTransferring(bool value)
  2064. {
  2065. SET_CONFIG_PROPERTY(ConfirmTransferring);
  2066. }
  2067. //---------------------------------------------------------------------------
  2068. void __fastcall TWinConfiguration::SetConfirmDeleting(bool value)
  2069. {
  2070. SET_CONFIG_PROPERTY(ConfirmDeleting);
  2071. }
  2072. //---------------------------------------------------------------------------
  2073. void __fastcall TWinConfiguration::SetConfirmRecycling(bool value)
  2074. {
  2075. SET_CONFIG_PROPERTY(ConfirmRecycling);
  2076. }
  2077. //---------------------------------------------------------------------------
  2078. void __fastcall TWinConfiguration::SetUseLocationProfiles(bool value)
  2079. {
  2080. SET_CONFIG_PROPERTY(UseLocationProfiles);
  2081. }
  2082. //---------------------------------------------------------------------------
  2083. void __fastcall TWinConfiguration::SetUseSharedBookmarks(bool value)
  2084. {
  2085. SET_CONFIG_PROPERTY(UseSharedBookmarks);
  2086. }
  2087. //---------------------------------------------------------------------------
  2088. void __fastcall TWinConfiguration::SetConfirmClosingSession(bool value)
  2089. {
  2090. SET_CONFIG_PROPERTY(ConfirmClosingSession);
  2091. }
  2092. //---------------------------------------------------------------------------
  2093. void __fastcall TWinConfiguration::SetDoubleClickAction(TDoubleClickAction value)
  2094. {
  2095. SET_CONFIG_PROPERTY(DoubleClickAction);
  2096. }
  2097. //---------------------------------------------------------------------------
  2098. void __fastcall TWinConfiguration::SetCopyOnDoubleClickConfirmation(bool value)
  2099. {
  2100. SET_CONFIG_PROPERTY(CopyOnDoubleClickConfirmation);
  2101. }
  2102. //---------------------------------------------------------------------------
  2103. void __fastcall TWinConfiguration::SetAlwaysRespectDoubleClickAction(bool value)
  2104. {
  2105. SET_CONFIG_PROPERTY(AlwaysRespectDoubleClickAction);
  2106. }
  2107. //---------------------------------------------------------------------------
  2108. void __fastcall TWinConfiguration::SetDimmHiddenFiles(bool value)
  2109. {
  2110. SET_CONFIG_PROPERTY(DimmHiddenFiles);
  2111. }
  2112. //---------------------------------------------------------------------------
  2113. void __fastcall TWinConfiguration::SetRenameWholeName(bool value)
  2114. {
  2115. SET_CONFIG_PROPERTY(RenameWholeName);
  2116. }
  2117. //---------------------------------------------------------------------------
  2118. void __fastcall TWinConfiguration::SetAutoStartSession(UnicodeString value)
  2119. {
  2120. SET_CONFIG_PROPERTY(AutoStartSession);
  2121. }
  2122. //---------------------------------------------------------------------------
  2123. void __fastcall TWinConfiguration::SetExpertMode(bool value)
  2124. {
  2125. SET_CONFIG_PROPERTY(ExpertMode);
  2126. }
  2127. //---------------------------------------------------------------------------
  2128. void __fastcall TWinConfiguration::SetDefaultDirIsHome(bool value)
  2129. {
  2130. SET_CONFIG_PROPERTY(DefaultDirIsHome);
  2131. }
  2132. //---------------------------------------------------------------------------
  2133. void __fastcall TWinConfiguration::SetTemporaryDirectoryAppendSession(bool value)
  2134. {
  2135. SET_CONFIG_PROPERTY(TemporaryDirectoryAppendSession);
  2136. }
  2137. //---------------------------------------------------------------------------
  2138. void __fastcall TWinConfiguration::SetTemporaryDirectoryAppendPath(bool value)
  2139. {
  2140. SET_CONFIG_PROPERTY(TemporaryDirectoryAppendPath);
  2141. }
  2142. //---------------------------------------------------------------------------
  2143. void __fastcall TWinConfiguration::SetTemporaryDirectoryDeterministic(bool value)
  2144. {
  2145. SET_CONFIG_PROPERTY(TemporaryDirectoryDeterministic);
  2146. }
  2147. //---------------------------------------------------------------------------
  2148. void __fastcall TWinConfiguration::SetTemporaryDirectoryCleanup(bool value)
  2149. {
  2150. SET_CONFIG_PROPERTY(TemporaryDirectoryCleanup);
  2151. }
  2152. //---------------------------------------------------------------------------
  2153. void __fastcall TWinConfiguration::SetConfirmTemporaryDirectoryCleanup(bool value)
  2154. {
  2155. SET_CONFIG_PROPERTY(ConfirmTemporaryDirectoryCleanup);
  2156. }
  2157. //---------------------------------------------------------------------------
  2158. void __fastcall TWinConfiguration::SetPreservePanelState(bool value)
  2159. {
  2160. SET_CONFIG_PROPERTY(PreservePanelState);
  2161. }
  2162. //---------------------------------------------------------------------------
  2163. void __fastcall TWinConfiguration::SetDarkTheme(TAutoSwitch value)
  2164. {
  2165. SET_CONFIG_PROPERTY_EX(DarkTheme, ConfigureInterface());
  2166. }
  2167. //---------------------------------------------------------------------------
  2168. static int __fastcall SysDarkTheme(HKEY RootKey)
  2169. {
  2170. std::unique_ptr<TRegistry> Registry(new TRegistry());
  2171. Registry->RootKey = RootKey;
  2172. UnicodeString ThemesPersonalizeKey = L"Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize";
  2173. UnicodeString AppsUseLightThemeValue = L"AppsUseLightTheme";
  2174. int Result = -1;
  2175. if (Registry->OpenKeyReadOnly(ThemesPersonalizeKey) &&
  2176. Registry->ValueExists(AppsUseLightThemeValue))
  2177. {
  2178. Result = Registry->ReadBool(AppsUseLightThemeValue) ? 0 : 1;
  2179. }
  2180. return Result;
  2181. }
  2182. //---------------------------------------------------------------------------
  2183. bool __fastcall TWinConfiguration::UseDarkTheme()
  2184. {
  2185. switch (WinConfiguration->DarkTheme)
  2186. {
  2187. case asOn:
  2188. return true;
  2189. case asOff:
  2190. return false;
  2191. default:
  2192. return (GetSysDarkTheme() > 0);
  2193. }
  2194. }
  2195. //---------------------------------------------------------------------------
  2196. void __fastcall TWinConfiguration::SetLastStoredSession(UnicodeString value)
  2197. {
  2198. SET_CONFIG_PROPERTY(LastStoredSession);
  2199. }
  2200. //---------------------------------------------------------------------------
  2201. void __fastcall TWinConfiguration::SetAutoSaveWorkspace(bool value)
  2202. {
  2203. SET_CONFIG_PROPERTY(AutoSaveWorkspace);
  2204. }
  2205. //---------------------------------------------------------------------------
  2206. void __fastcall TWinConfiguration::SetAutoSaveWorkspacePasswords(bool value)
  2207. {
  2208. SET_CONFIG_PROPERTY(AutoSaveWorkspacePasswords);
  2209. }
  2210. //---------------------------------------------------------------------------
  2211. void __fastcall TWinConfiguration::SetAutoWorkspace(UnicodeString value)
  2212. {
  2213. SET_CONFIG_PROPERTY(AutoWorkspace);
  2214. }
  2215. //---------------------------------------------------------------------------
  2216. void __fastcall TWinConfiguration::SetPathInCaption(TPathInCaption value)
  2217. {
  2218. SET_CONFIG_PROPERTY(PathInCaption);
  2219. }
  2220. //---------------------------------------------------------------------------
  2221. void __fastcall TWinConfiguration::SetMinimizeToTray(bool value)
  2222. {
  2223. SET_CONFIG_PROPERTY(MinimizeToTray);
  2224. }
  2225. //---------------------------------------------------------------------------
  2226. void __fastcall TWinConfiguration::SetBalloonNotifications(bool value)
  2227. {
  2228. SET_CONFIG_PROPERTY(BalloonNotifications);
  2229. }
  2230. //---------------------------------------------------------------------------
  2231. void __fastcall TWinConfiguration::SetNotificationsTimeout(unsigned int value)
  2232. {
  2233. SET_CONFIG_PROPERTY(NotificationsTimeout);
  2234. }
  2235. //---------------------------------------------------------------------------
  2236. void __fastcall TWinConfiguration::SetNotificationsStickTime(unsigned int value)
  2237. {
  2238. SET_CONFIG_PROPERTY(NotificationsStickTime);
  2239. }
  2240. //---------------------------------------------------------------------------
  2241. void __fastcall TWinConfiguration::SetCopyParamAutoSelectNotice(bool value)
  2242. {
  2243. SET_CONFIG_PROPERTY(CopyParamAutoSelectNotice);
  2244. }
  2245. //---------------------------------------------------------------------------
  2246. void __fastcall TWinConfiguration::SetLockToolbars(bool value)
  2247. {
  2248. SET_CONFIG_PROPERTY(LockToolbars);
  2249. }
  2250. //---------------------------------------------------------------------------
  2251. void __fastcall TWinConfiguration::SetSelectiveToolbarText(bool value)
  2252. {
  2253. SET_CONFIG_PROPERTY(SelectiveToolbarText);
  2254. }
  2255. //---------------------------------------------------------------------------
  2256. void TWinConfiguration::SetLargerToolbar(int value)
  2257. {
  2258. SET_CONFIG_PROPERTY(LargerToolbar);
  2259. }
  2260. //---------------------------------------------------------------------------
  2261. void __fastcall TWinConfiguration::SetAutoOpenInPutty(bool value)
  2262. {
  2263. SET_CONFIG_PROPERTY(AutoOpenInPutty);
  2264. }
  2265. //---------------------------------------------------------------------------
  2266. void __fastcall TWinConfiguration::SetRefreshRemotePanel(bool value)
  2267. {
  2268. SET_CONFIG_PROPERTY(RefreshRemotePanel);
  2269. }
  2270. //---------------------------------------------------------------------------
  2271. void __fastcall TWinConfiguration::SetRefreshRemotePanelInterval(TDateTime value)
  2272. {
  2273. SET_CONFIG_PROPERTY(RefreshRemotePanelInterval);
  2274. }
  2275. //---------------------------------------------------------------------------
  2276. void __fastcall TWinConfiguration::UpdateIconFont()
  2277. {
  2278. UpdateDesktopFont();
  2279. }
  2280. //---------------------------------------------------------------------------
  2281. void __fastcall TWinConfiguration::SetPanelFont(const TFontConfiguration & value)
  2282. {
  2283. SET_CONFIG_PROPERTY_EX(PanelFont, UpdateIconFont());
  2284. }
  2285. //---------------------------------------------------------------------------
  2286. void __fastcall TWinConfiguration::SetNaturalOrderNumericalSorting(bool value)
  2287. {
  2288. SET_CONFIG_PROPERTY(NaturalOrderNumericalSorting);
  2289. }
  2290. //---------------------------------------------------------------------------
  2291. void __fastcall TWinConfiguration::SetAlwaysSortDirectoriesByName(bool value)
  2292. {
  2293. SET_CONFIG_PROPERTY(AlwaysSortDirectoriesByName);
  2294. }
  2295. //---------------------------------------------------------------------------
  2296. void __fastcall TWinConfiguration::SetFullRowSelect(bool value)
  2297. {
  2298. SET_CONFIG_PROPERTY(FullRowSelect);
  2299. }
  2300. //---------------------------------------------------------------------------
  2301. void __fastcall TWinConfiguration::SetOfferedEditorAutoConfig(bool value)
  2302. {
  2303. SET_CONFIG_PROPERTY(OfferedEditorAutoConfig);
  2304. }
  2305. //---------------------------------------------------------------------------
  2306. void __fastcall TWinConfiguration::SetOpenedStoredSessionFolders(UnicodeString value)
  2307. {
  2308. SET_CONFIG_PROPERTY(OpenedStoredSessionFolders);
  2309. }
  2310. //---------------------------------------------------------------------------
  2311. void __fastcall TWinConfiguration::SetAutoImportedFromPuttyOrFilezilla(bool value)
  2312. {
  2313. SET_CONFIG_PROPERTY(AutoImportedFromPuttyOrFilezilla);
  2314. }
  2315. //---------------------------------------------------------------------------
  2316. void __fastcall TWinConfiguration::SetGenerateUrlComponents(int value)
  2317. {
  2318. SET_CONFIG_PROPERTY(GenerateUrlComponents);
  2319. }
  2320. //---------------------------------------------------------------------------
  2321. void __fastcall TWinConfiguration::SetGenerateUrlCodeTarget(TGenerateUrlCodeTarget value)
  2322. {
  2323. SET_CONFIG_PROPERTY(GenerateUrlCodeTarget);
  2324. }
  2325. //---------------------------------------------------------------------------
  2326. void __fastcall TWinConfiguration::SetGenerateUrlScriptFormat(TScriptFormat value)
  2327. {
  2328. SET_CONFIG_PROPERTY(GenerateUrlScriptFormat);
  2329. }
  2330. //---------------------------------------------------------------------------
  2331. void __fastcall TWinConfiguration::SetGenerateUrlAssemblyLanguage(TAssemblyLanguage value)
  2332. {
  2333. SET_CONFIG_PROPERTY(GenerateUrlAssemblyLanguage);
  2334. }
  2335. //---------------------------------------------------------------------------
  2336. void __fastcall TWinConfiguration::SetExternalSessionInExistingInstance(bool value)
  2337. {
  2338. SET_CONFIG_PROPERTY(ExternalSessionInExistingInstance);
  2339. }
  2340. //---------------------------------------------------------------------------
  2341. void __fastcall TWinConfiguration::SetShowLoginWhenNoSession(bool value)
  2342. {
  2343. SET_CONFIG_PROPERTY(ShowLoginWhenNoSession);
  2344. }
  2345. //---------------------------------------------------------------------------
  2346. void __fastcall TWinConfiguration::SetKeepOpenWhenNoSession(bool value)
  2347. {
  2348. SET_CONFIG_PROPERTY(KeepOpenWhenNoSession);
  2349. }
  2350. //---------------------------------------------------------------------------
  2351. void __fastcall TWinConfiguration::SetDefaultToNewRemoteTab(bool value)
  2352. {
  2353. SET_CONFIG_PROPERTY(DefaultToNewRemoteTab);
  2354. }
  2355. //---------------------------------------------------------------------------
  2356. void __fastcall TWinConfiguration::SetLocalIconsByExt(bool value)
  2357. {
  2358. SET_CONFIG_PROPERTY(LocalIconsByExt);
  2359. }
  2360. //---------------------------------------------------------------------------
  2361. void __fastcall TWinConfiguration::SetFlashTaskbar(bool value)
  2362. {
  2363. SET_CONFIG_PROPERTY(FlashTaskbar);
  2364. }
  2365. //---------------------------------------------------------------------------
  2366. void __fastcall TWinConfiguration::SetBidiModeOverride(TLocaleFlagOverride value)
  2367. {
  2368. SET_CONFIG_PROPERTY(BidiModeOverride);
  2369. }
  2370. //---------------------------------------------------------------------------
  2371. void __fastcall TWinConfiguration::SetFlipChildrenOverride(TLocaleFlagOverride value)
  2372. {
  2373. SET_CONFIG_PROPERTY(FlipChildrenOverride);
  2374. }
  2375. //---------------------------------------------------------------------------
  2376. void __fastcall TWinConfiguration::SetShowTips(bool value)
  2377. {
  2378. SET_CONFIG_PROPERTY(ShowTips);
  2379. }
  2380. //---------------------------------------------------------------------------
  2381. void __fastcall TWinConfiguration::SetTipsSeen(UnicodeString value)
  2382. {
  2383. SET_CONFIG_PROPERTY(TipsSeen);
  2384. }
  2385. //---------------------------------------------------------------------------
  2386. void __fastcall TWinConfiguration::SetTipsShown(TDateTime value)
  2387. {
  2388. SET_CONFIG_PROPERTY(TipsShown);
  2389. }
  2390. //---------------------------------------------------------------------------
  2391. void __fastcall TWinConfiguration::SetFileColors(UnicodeString value)
  2392. {
  2393. SET_CONFIG_PROPERTY(FileColors);
  2394. }
  2395. //---------------------------------------------------------------------------
  2396. void __fastcall TWinConfiguration::SetRunsSinceLastTip(int value)
  2397. {
  2398. SET_CONFIG_PROPERTY(RunsSinceLastTip);
  2399. }
  2400. //---------------------------------------------------------------------------
  2401. bool __fastcall TWinConfiguration::GetHonorDrivePolicy()
  2402. {
  2403. return DriveInfo->HonorDrivePolicy;
  2404. }
  2405. //---------------------------------------------------------------------------
  2406. void __fastcall TWinConfiguration::SetHonorDrivePolicy(bool value)
  2407. {
  2408. if (HonorDrivePolicy != value)
  2409. {
  2410. DriveInfo->HonorDrivePolicy = value;
  2411. Changed();
  2412. }
  2413. }
  2414. //---------------------------------------------------------------------------
  2415. bool __fastcall TWinConfiguration::GetUseABDrives()
  2416. {
  2417. return DriveInfo->UseABDrives;
  2418. }
  2419. //---------------------------------------------------------------------------
  2420. void __fastcall TWinConfiguration::SetUseABDrives(bool value)
  2421. {
  2422. if (UseABDrives != value)
  2423. {
  2424. DriveInfo->UseABDrives = value;
  2425. Changed();
  2426. }
  2427. }
  2428. //---------------------------------------------------------------------------
  2429. void __fastcall TWinConfiguration::SetCustomCommandList(TCustomCommandList * value)
  2430. {
  2431. DebugAssert(FCustomCommandList);
  2432. if (!FCustomCommandList->Equals(value))
  2433. {
  2434. FCustomCommandList->Assign(value);
  2435. FCustomCommandsDefaults = false;
  2436. }
  2437. }
  2438. //---------------------------------------------------------------------------
  2439. void __fastcall TWinConfiguration::SetExtensionList(TCustomCommandList * value)
  2440. {
  2441. if (!ExtensionList->Equals(value))
  2442. {
  2443. std::unique_ptr<TStringList> DeletedExtensions(CreateSortedStringList());
  2444. ParseExtensionList(DeletedExtensions.get(), FExtensionsDeleted);
  2445. for (int Index = 0; Index < ExtensionList->Count; Index++)
  2446. {
  2447. const TCustomCommandType * CustomCommand = ExtensionList->Commands[Index];
  2448. int Index = value->FindIndexByFileName(CustomCommand->FileName);
  2449. if (Index < 0)
  2450. {
  2451. if (FileExists(CustomCommand->FileName) &&
  2452. !DeleteFile(CustomCommand->FileName))
  2453. {
  2454. DeletedExtensions->Add(CustomCommand->Id);
  2455. }
  2456. }
  2457. }
  2458. FExtensionsOrder = L"";
  2459. for (int Index = 0; Index < value->Count; Index++)
  2460. {
  2461. const TCustomCommandType * CustomCommand = value->Commands[Index];
  2462. AddToList(FExtensionsOrder, CustomCommand->Id, L"|");
  2463. int DeletedIndex = DeletedExtensions->IndexOf(CustomCommand->Id);
  2464. if (DeletedIndex >= 0)
  2465. {
  2466. DeletedExtensions->Delete(DeletedIndex);
  2467. }
  2468. }
  2469. FExtensionList->Assign(value);
  2470. FExtensionsDeleted = L"";
  2471. for (int Index = 0; Index < DeletedExtensions->Count; Index++)
  2472. {
  2473. AddToList(FExtensionsDeleted, DeletedExtensions->Strings[Index], L"|");
  2474. }
  2475. FExtensionsShortCuts = L"";
  2476. for (int Index = 0; Index < value->Count; Index++)
  2477. {
  2478. const TCustomCommandType * Extension = value->Commands[Index];
  2479. if (Extension->HasCustomShortCut())
  2480. {
  2481. AddToList(FExtensionsShortCuts, FORMAT(L"%d=%s", (Extension->ShortCut, Extension->Id)), L"|");
  2482. }
  2483. }
  2484. Changed();
  2485. }
  2486. }
  2487. //---------------------------------------------------------------------------
  2488. void __fastcall TWinConfiguration::CustomCommandShortCuts(TShortCuts & ShortCuts) const
  2489. {
  2490. CustomCommandList->ShortCuts(ShortCuts);
  2491. ExtensionList->ShortCuts(ShortCuts);
  2492. }
  2493. //---------------------------------------------------------------------------
  2494. void __fastcall TWinConfiguration::SetBookmarks(UnicodeString Key,
  2495. TBookmarkList * value)
  2496. {
  2497. FBookmarks->Bookmarks[Key] = value;
  2498. Changed();
  2499. }
  2500. //---------------------------------------------------------------------------
  2501. TBookmarkList * __fastcall TWinConfiguration::GetBookmarks(UnicodeString Key)
  2502. {
  2503. return FBookmarks->Bookmarks[Key];
  2504. }
  2505. //---------------------------------------------------------------------------
  2506. void __fastcall TWinConfiguration::SetSharedBookmarks(TBookmarkList * value)
  2507. {
  2508. FBookmarks->SharedBookmarks = value;
  2509. Changed();
  2510. }
  2511. //---------------------------------------------------------------------------
  2512. TBookmarkList * __fastcall TWinConfiguration::GetSharedBookmarks()
  2513. {
  2514. return FBookmarks->SharedBookmarks;
  2515. }
  2516. //---------------------------------------------------------------------------
  2517. UnicodeString __fastcall TWinConfiguration::GetDefaultKeyFile()
  2518. {
  2519. return (!FDefaultKeyFile.IsEmpty() ? FDefaultKeyFile : FTemporaryKeyFile);
  2520. }
  2521. //---------------------------------------------------------------------------
  2522. void __fastcall TWinConfiguration::SetLastMonitor(int value)
  2523. {
  2524. ::SetLastMonitor(value);
  2525. }
  2526. //---------------------------------------------------------------------------
  2527. int __fastcall TWinConfiguration::GetLastMonitor()
  2528. {
  2529. return ::GetLastMonitor();
  2530. }
  2531. //---------------------------------------------------------------------------
  2532. UnicodeString __fastcall TWinConfiguration::ExpandedTemporaryDirectory()
  2533. {
  2534. UnicodeString Result =
  2535. ExpandFileName(ExpandEnvironmentVariables(DDTemporaryDirectory));
  2536. if (Result.IsEmpty())
  2537. {
  2538. Result = SystemTemporaryDirectory();
  2539. }
  2540. return Result;
  2541. }
  2542. //---------------------------------------------------------------------------
  2543. UnicodeString TWinConfiguration::TemporaryDir(bool Mask)
  2544. {
  2545. return UniqTempDir(ExpandedTemporaryDirectory(), L"scp", Mask);
  2546. }
  2547. //---------------------------------------------------------------------------
  2548. TStrings * __fastcall TWinConfiguration::DoFindTemporaryFolders(bool OnlyFirst)
  2549. {
  2550. std::unique_ptr<TStrings> Result(new TStringList());
  2551. TSearchRecOwned SRec;
  2552. UnicodeString Mask = TemporaryDir(true);
  2553. if (FindFirstUnchecked(Mask, faDirectory | faHidden, SRec) == 0)
  2554. {
  2555. do
  2556. {
  2557. if (SRec.IsDirectory())
  2558. {
  2559. Result->Add(SRec.GetFilePath());
  2560. }
  2561. }
  2562. while ((FindNextChecked(SRec) == 0) && (!OnlyFirst || Result->Count == 0));
  2563. }
  2564. if (Result->Count == 0)
  2565. {
  2566. Result.reset(NULL);
  2567. }
  2568. return Result.release();
  2569. }
  2570. //---------------------------------------------------------------------------
  2571. TStrings * __fastcall TWinConfiguration::FindTemporaryFolders()
  2572. {
  2573. return DoFindTemporaryFolders(false);
  2574. }
  2575. //---------------------------------------------------------------------------
  2576. bool __fastcall TWinConfiguration::AnyTemporaryFolders()
  2577. {
  2578. std::unique_ptr<TStrings> Folders(DoFindTemporaryFolders(true));
  2579. return (Folders.get() != NULL);
  2580. }
  2581. //---------------------------------------------------------------------------
  2582. void __fastcall TWinConfiguration::CleanupTemporaryFolders()
  2583. {
  2584. std::unique_ptr<TStrings> Folders(FindTemporaryFolders());
  2585. if (Folders.get() != NULL)
  2586. {
  2587. CleanupTemporaryFolders(Folders.get());
  2588. }
  2589. }
  2590. //---------------------------------------------------------------------------
  2591. void __fastcall TWinConfiguration::CleanupTemporaryFolders(TStrings * Folders)
  2592. {
  2593. if (DebugAlwaysTrue(Folders->Count > 0))
  2594. {
  2595. Usage->Inc(L"TemporaryDirectoryCleanups");
  2596. }
  2597. UnicodeString ErrorList;
  2598. for (int i = 0; i < Folders->Count; i++)
  2599. {
  2600. if (!RecursiveDeleteFile(Folders->Strings[i]))
  2601. {
  2602. if (!ErrorList.IsEmpty())
  2603. {
  2604. ErrorList += L"\n";
  2605. }
  2606. ErrorList += Folders->Strings[i];
  2607. }
  2608. }
  2609. if (!ErrorList.IsEmpty())
  2610. {
  2611. throw ExtException(LoadStr(CLEANUP_TEMP_ERROR), ErrorList);
  2612. }
  2613. }
  2614. //---------------------------------------------------------------------------
  2615. int __fastcall TWinConfiguration::GetResourceModuleCompleteness(HINSTANCE Module)
  2616. {
  2617. UnicodeString CompletenessStr = LoadStrFrom(Module, TRANSLATION_COMPLETENESS);
  2618. return StrToIntDef(CompletenessStr, -1);
  2619. }
  2620. //---------------------------------------------------------------------------
  2621. int __fastcall TWinConfiguration::GetLocaleCompletenessTreshold()
  2622. {
  2623. return 80;
  2624. }
  2625. //---------------------------------------------------------------------------
  2626. bool __fastcall TWinConfiguration::IsTranslationComplete(HINSTANCE Module)
  2627. {
  2628. return (GetResourceModuleCompleteness(Module) >= LocaleCompletenessTreshold);
  2629. }
  2630. //---------------------------------------------------------------------------
  2631. HINSTANCE __fastcall TWinConfiguration::LoadNewResourceModule(LCID ALocale,
  2632. UnicodeString & FileName)
  2633. {
  2634. HINSTANCE Instance = TCustomWinConfiguration::LoadNewResourceModule(ALocale, FileName);
  2635. if (Instance != NULL)
  2636. {
  2637. try
  2638. {
  2639. CheckTranslationVersion(FileName, false);
  2640. }
  2641. catch(...)
  2642. {
  2643. FreeResourceModule(Instance);
  2644. throw;
  2645. }
  2646. }
  2647. return Instance;
  2648. }
  2649. //---------------------------------------------------------------------------
  2650. void __fastcall TWinConfiguration::CheckTranslationVersion(const UnicodeString FileName,
  2651. bool InternalLocaleOnError)
  2652. {
  2653. UnicodeString TranslationProductVersion = GetFileProductVersion(FileName);
  2654. UnicodeString TranslationProductName = GetFileProductName(FileName);
  2655. if ((ProductName != TranslationProductName) ||
  2656. (ProductVersion != TranslationProductVersion))
  2657. {
  2658. if (InternalLocaleOnError)
  2659. {
  2660. LocaleSafe = InternalLocale();
  2661. }
  2662. if (TranslationProductName.IsEmpty() || TranslationProductVersion.IsEmpty())
  2663. {
  2664. throw Exception(FMTLOAD(UNKNOWN_TRANSLATION, (FileName)));
  2665. }
  2666. else
  2667. {
  2668. throw Exception(FMTLOAD(INCOMPATIBLE_TRANSLATION,
  2669. (FileName, TranslationProductName, TranslationProductVersion)));
  2670. }
  2671. }
  2672. }
  2673. //---------------------------------------------------------------------------
  2674. void __fastcall TWinConfiguration::CheckDefaultTranslation()
  2675. {
  2676. if (!FInvalidDefaultTranslationMessage.IsEmpty())
  2677. {
  2678. MoreMessageDialog(FMTLOAD(INVALID_DEFAULT_TRANSLATION,
  2679. (FInvalidDefaultTranslationMessage)), NULL, qtWarning, qaOK, HELP_NONE);
  2680. }
  2681. }
  2682. //---------------------------------------------------------------------------
  2683. const TEditorPreferences * __fastcall TWinConfiguration::DefaultEditorForFile(
  2684. const UnicodeString FileName, bool Local, const TFileMasks::TParams & MaskParams)
  2685. {
  2686. return FEditorList->Find(FileName, Local, MaskParams);
  2687. }
  2688. //---------------------------------------------------------------------------
  2689. const TEditorList * __fastcall TWinConfiguration::GetEditorList()
  2690. {
  2691. return FEditorList;
  2692. }
  2693. //---------------------------------------------------------------------------
  2694. void __fastcall TWinConfiguration::SetEditorList(const TEditorList * value)
  2695. {
  2696. if (!(*FEditorList == *value))
  2697. {
  2698. *FEditorList = *value;
  2699. Changed();
  2700. }
  2701. }
  2702. //---------------------------------------------------------------------------
  2703. TStrings * __fastcall TWinConfiguration::GetCustomCommandOptions()
  2704. {
  2705. return FCustomCommandOptions.get();
  2706. }
  2707. //---------------------------------------------------------------------------
  2708. void __fastcall TWinConfiguration::SetCustomCommandOptions(TStrings * value)
  2709. {
  2710. if (!FCustomCommandOptions->Equals(value))
  2711. {
  2712. FCustomCommandOptions->Assign(value);
  2713. FCustomCommandOptionsModified = true;
  2714. }
  2715. }
  2716. //---------------------------------------------------------------------------
  2717. void __fastcall TWinConfiguration::SetLockedInterface(bool value)
  2718. {
  2719. SET_CONFIG_PROPERTY(LockedInterface);
  2720. }
  2721. //---------------------------------------------------------------------------
  2722. bool __fastcall TWinConfiguration::GetTimeoutShellOperations()
  2723. {
  2724. return ::TimeoutShellOperations;
  2725. }
  2726. //---------------------------------------------------------------------------
  2727. void __fastcall TWinConfiguration::SetTimeoutShellOperations(bool value)
  2728. {
  2729. ::TimeoutShellOperations = value;
  2730. }
  2731. //---------------------------------------------------------------------------
  2732. void __fastcall TWinConfiguration::SetTimeoutShellIconRetrieval(bool value)
  2733. {
  2734. SET_CONFIG_PROPERTY(TimeoutShellIconRetrieval);
  2735. }
  2736. //---------------------------------------------------------------------------
  2737. void __fastcall TWinConfiguration::SetUseIconUpdateThread(bool value)
  2738. {
  2739. SET_CONFIG_PROPERTY(UseIconUpdateThread);
  2740. }
  2741. //---------------------------------------------------------------------------
  2742. void __fastcall TWinConfiguration::SetAllowWindowPrint(bool value)
  2743. {
  2744. SET_CONFIG_PROPERTY(AllowWindowPrint);
  2745. }
  2746. //---------------------------------------------------------------------------
  2747. void TWinConfiguration::SetStoreTransition(TStoreTransition value)
  2748. {
  2749. SET_CONFIG_PROPERTY(StoreTransition);
  2750. }
  2751. //---------------------------------------------------------------------------
  2752. void TWinConfiguration::SetQueueTransferLimitMax(int value)
  2753. {
  2754. SET_CONFIG_PROPERTY(QueueTransferLimitMax);
  2755. }
  2756. //---------------------------------------------------------------------------
  2757. void TWinConfiguration::SetHiContrast(bool value)
  2758. {
  2759. SET_CONFIG_PROPERTY(HiContrast);
  2760. }
  2761. //---------------------------------------------------------------------------
  2762. void TWinConfiguration::SetEditorCheckNotModified(bool value)
  2763. {
  2764. SET_CONFIG_PROPERTY(EditorCheckNotModified);
  2765. }
  2766. //---------------------------------------------------------------------------
  2767. void TWinConfiguration::SetSessionTabCaptionTruncation(bool value)
  2768. {
  2769. SET_CONFIG_PROPERTY(SessionTabCaptionTruncation);
  2770. }
  2771. //---------------------------------------------------------------------------
  2772. void TWinConfiguration::SetLoadingTooLongLimit(int value)
  2773. {
  2774. DriveViewLoadingTooLongLimit = value;
  2775. }
  2776. //---------------------------------------------------------------------------
  2777. int TWinConfiguration::GetLoadingTooLongLimit()
  2778. {
  2779. return DriveViewLoadingTooLongLimit;
  2780. }
  2781. //---------------------------------------------------------------------------
  2782. void TWinConfiguration::SetFirstRun(const UnicodeString & value)
  2783. {
  2784. SET_CONFIG_PROPERTY(FirstRun);
  2785. }
  2786. //---------------------------------------------------------------------------
  2787. TStringList * __fastcall TWinConfiguration::LoadJumpList(
  2788. THierarchicalStorage * Storage, UnicodeString Name)
  2789. {
  2790. UnicodeString JumpList = Storage->ReadString(Name, L"");
  2791. std::unique_ptr<TStringList> List(new TStringList());
  2792. List->CaseSensitive = false;
  2793. List->CommaText = JumpList;
  2794. return List.release();
  2795. }
  2796. //---------------------------------------------------------------------------
  2797. void __fastcall TWinConfiguration::SaveJumpList(
  2798. THierarchicalStorage * Storage, UnicodeString Name, TStringList * List)
  2799. {
  2800. UnicodeString JumpList = Storage->ReadString(Name, L"");
  2801. UnicodeString NewJumpList = List->CommaText;
  2802. if (NewJumpList != JumpList)
  2803. {
  2804. Storage->WriteString(Name, NewJumpList);
  2805. }
  2806. }
  2807. //---------------------------------------------------------------------------
  2808. void __fastcall TWinConfiguration::TrimJumpList(TStringList * List)
  2809. {
  2810. while (List->Count > 30)
  2811. {
  2812. List->Delete(List->Count - 1);
  2813. }
  2814. }
  2815. //---------------------------------------------------------------------------
  2816. void __fastcall TWinConfiguration::UpdateEntryInJumpList(
  2817. bool Session, const UnicodeString & Name, bool Add)
  2818. {
  2819. try
  2820. {
  2821. std::unique_ptr<THierarchicalStorage> Storage(CreateConfigStorage());
  2822. TAutoNestingCounter DontDecryptPasswordsCounter(FDontDecryptPasswords);
  2823. Storage->AccessMode = smReadWrite;
  2824. // For initial call from UpdateJumpList, do not create the key if it does ot exist yet.
  2825. // To avoid creating the key if we are being started just for a maintenance task.
  2826. if (Storage->OpenSubKey(ConfigurationSubKey, !Name.IsEmpty()))
  2827. {
  2828. std::unique_ptr<TStringList> ListSessions(LoadJumpList(Storage.get(), L"JumpList"));
  2829. std::unique_ptr<TStringList> ListWorkspaces(LoadJumpList(Storage.get(), L"JumpListWorkspaces"));
  2830. if (!Name.IsEmpty())
  2831. {
  2832. TStringList * List = (Session ? ListSessions : ListWorkspaces).get();
  2833. int Index = List->IndexOf(Name);
  2834. if (Index >= 0)
  2835. {
  2836. List->Delete(Index);
  2837. }
  2838. if (Add)
  2839. {
  2840. List->Insert(0, Name);
  2841. }
  2842. }
  2843. TrimJumpList(ListSessions.get());
  2844. TrimJumpList(ListWorkspaces.get());
  2845. ::UpdateJumpList(ListSessions.get(), ListWorkspaces.get());
  2846. SaveJumpList(Storage.get(), L"JumpList", ListSessions.get());
  2847. SaveJumpList(Storage.get(), L"JumpListWorkspaces", ListWorkspaces.get());
  2848. }
  2849. }
  2850. catch (Exception & E)
  2851. {
  2852. throw ExtException(&E, MainInstructions(LoadStr(JUMPLIST_ERROR)));
  2853. }
  2854. }
  2855. //---------------------------------------------------------------------------
  2856. void __fastcall TWinConfiguration::AddSessionToJumpList(UnicodeString SessionName)
  2857. {
  2858. UpdateEntryInJumpList(true, SessionName, true);
  2859. }
  2860. //---------------------------------------------------------------------------
  2861. void __fastcall TWinConfiguration::DeleteSessionFromJumpList(UnicodeString SessionName)
  2862. {
  2863. UpdateEntryInJumpList(true, SessionName, false);
  2864. }
  2865. //---------------------------------------------------------------------------
  2866. void __fastcall TWinConfiguration::AddWorkspaceToJumpList(UnicodeString Workspace)
  2867. {
  2868. UpdateEntryInJumpList(false, Workspace, true);
  2869. }
  2870. //---------------------------------------------------------------------------
  2871. void __fastcall TWinConfiguration::DeleteWorkspaceFromJumpList(UnicodeString Workspace)
  2872. {
  2873. UpdateEntryInJumpList(false, Workspace, false);
  2874. }
  2875. //---------------------------------------------------------------------------
  2876. void __fastcall TWinConfiguration::UpdateJumpList()
  2877. {
  2878. AddSessionToJumpList(L"");
  2879. }
  2880. //---------------------------------------------------------------------------
  2881. void __fastcall TWinConfiguration::UpdateStaticUsage()
  2882. {
  2883. // This is here because of TStoredSessionList::UpdateStaticUsage() call
  2884. // from TConfiguration::UpdateStaticUsage()
  2885. TAutoNestingCounter DontDecryptPasswordsAutoCounter(FDontDecryptPasswords);
  2886. TCustomWinConfiguration::UpdateStaticUsage();
  2887. Usage->Set(L"Beta", IsBeta);
  2888. Usage->Set(L"Interface", Interface);
  2889. Usage->Set(L"ThemeDark", UseDarkTheme());
  2890. Usage->Set(L"CustomCommandsCount", (FCustomCommandsDefaults ? 0 : FCustomCommandList->Count));
  2891. Usage->Set(L"UsingLocationProfiles", UseLocationProfiles);
  2892. Usage->Set(L"UsingMasterPassword", UseMasterPassword);
  2893. Usage->Set(L"UsingAutoSaveWorkspace", AutoSaveWorkspace);
  2894. Usage->Set(L"TreeVisible",
  2895. (Interface == ifExplorer) ?
  2896. ScpExplorer.DriveView :
  2897. (ScpCommander.LocalPanel.DriveView || ScpCommander.RemotePanel.DriveView));
  2898. Usage->Set(L"MinimizeToTray", MinimizeToTray);
  2899. UnicodeString ToolbarsButtons = (Interface == ifExplorer) ? ScpExplorer.ToolbarsButtons : ScpCommander.ToolbarsButtons;
  2900. Usage->Set(L"AnyHiddenToolbarButtons", !ToolbarsButtons.IsEmpty());
  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. }