WinConfiguration.cpp 138 KB

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