WinConfiguration.cpp 134 KB

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