WinConfiguration.cpp 137 KB

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