WinConfiguration.cpp 133 KB

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