WinConfiguration.cpp 135 KB

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