WinConfiguration.cpp 137 KB

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