WinConfiguration.cpp 135 KB

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