WinConfiguration.cpp 135 KB

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