1
0

CustomScpExplorer.cpp 365 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079
  1. //---------------------------------------------------------------------------
  2. #define NO_WIN32_LEAN_AND_MEAN
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include <Common.h>
  6. #include "CustomScpExplorer.h"
  7. #include <Bookmarks.h>
  8. #include <Interface.h>
  9. #include <Exceptions.h>
  10. #include <CoreMain.h>
  11. #include <FileSystems.h>
  12. #include <TextsCore.h>
  13. #include <TextsWin.h>
  14. #include <HelpWin.h>
  15. #include <VCLCommon.h>
  16. #include <Progress.h>
  17. #include <SynchronizeProgress.h>
  18. #include <DragExt.h>
  19. #include <WinApi.h>
  20. #include "GUITools.h"
  21. #include "NonVisual.h"
  22. #include "Glyphs.h"
  23. #include "Tools.h"
  24. #include "WinConfiguration.h"
  25. #include "TerminalManager.h"
  26. #include "EditorManager.h"
  27. #include "ProgParams.h"
  28. #include "Setup.h"
  29. #include <Consts.hpp>
  30. #include <DateUtils.hpp>
  31. #include <TB2Common.hpp>
  32. #include <DirectoryMonitor.hpp>
  33. #include <System.IOUtils.hpp>
  34. //---------------------------------------------------------------------------
  35. #pragma package(smart_init)
  36. #pragma link "CustomDirView"
  37. #pragma link "CustomUnixDirView"
  38. #pragma link "IEListView"
  39. #pragma link "NortonLikeListView"
  40. #pragma link "UnixDirView"
  41. #pragma link "CustomDriveView"
  42. #pragma link "UnixDriveView"
  43. #pragma link "CustomDriveView"
  44. #pragma link "TB2Dock"
  45. #pragma link "TBXStatusBars"
  46. #pragma link "TB2Item"
  47. #pragma link "TB2Toolbar"
  48. #pragma link "ThemePageControl"
  49. #pragma link "PathLabel"
  50. #pragma resource "*.dfm"
  51. //---------------------------------------------------------------------------
  52. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  53. static const int SessionPanelCount = 4;
  54. //---------------------------------------------------------------------------
  55. class TMutexGuard
  56. {
  57. public:
  58. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  59. int Timeout = 5000)
  60. {
  61. FMutex = NULL;
  62. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  63. if (WaitResult == WAIT_TIMEOUT)
  64. {
  65. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  66. }
  67. else
  68. {
  69. FMutex = AMutex;
  70. }
  71. }
  72. ~TMutexGuard()
  73. {
  74. if (FMutex != NULL)
  75. {
  76. ReleaseMutex(FMutex);
  77. }
  78. }
  79. private:
  80. HANDLE FMutex;
  81. };
  82. //---------------------------------------------------------------------------
  83. class TWindowLock
  84. {
  85. public:
  86. TWindowLock(TCustomScpExplorerForm * Form) :
  87. FForm(Form)
  88. {
  89. FForm->LockWindow();
  90. }
  91. ~TWindowLock()
  92. {
  93. FForm->UnlockWindow();
  94. }
  95. private:
  96. TCustomScpExplorerForm * FForm;
  97. };
  98. //---------------------------------------------------------------------------
  99. class TAutoBatch
  100. {
  101. public:
  102. TAutoBatch(TCustomScpExplorerForm * Form) :
  103. FForm(Form)
  104. {
  105. FForm->BatchStart(FBatchStorage);
  106. }
  107. ~TAutoBatch()
  108. {
  109. FForm->BatchEnd(FBatchStorage);
  110. }
  111. private:
  112. TCustomScpExplorerForm * FForm;
  113. void * FBatchStorage;
  114. };
  115. //---------------------------------------------------------------------------
  116. struct TTransferOperationParam
  117. {
  118. TTransferOperationParam();
  119. UnicodeString TargetDirectory;
  120. bool Temp;
  121. bool DragDrop;
  122. int Options;
  123. TAutoSwitch Queue;
  124. };
  125. //---------------------------------------------------------------------------
  126. TTransferOperationParam::TTransferOperationParam()
  127. {
  128. Temp = false;
  129. DragDrop = false;
  130. Options = 0;
  131. Queue = asAuto;
  132. }
  133. //---------------------------------------------------------------------------
  134. class TTransferPresetNoteData : public TObject
  135. {
  136. public:
  137. UnicodeString Message;
  138. };
  139. //---------------------------------------------------------------------------
  140. class TTerminalNoteData : public TObject
  141. {
  142. public:
  143. TManagedTerminal * Terminal;
  144. };
  145. //---------------------------------------------------------------------------
  146. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  147. FFormRestored(false),
  148. TForm(Owner)
  149. {
  150. FInvalid = false;
  151. FCurrentSide = osRemote;
  152. FEverShown = false;
  153. FDocks = new TList();
  154. InitControls();
  155. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  156. // So that UpdateCustomCommandsToolbar called from RestoreParams works
  157. NonVisualDataModule->ScpExplorer = this;
  158. RestoreParams();
  159. ConfigurationChanged();
  160. RemoteDirView->Invalidate();
  161. FAutoOperation = false;
  162. FOnFileOperationFinished = NULL;
  163. FForceExecution = false;
  164. FIgnoreNextDialogChar = 0;
  165. FErrorList = NULL;
  166. FSynchronizeProgressForm = NULL;
  167. FProgressForm = NULL;
  168. FRefreshLocalDirectory = false;
  169. FRefreshRemoteDirectory = false;
  170. FDDExtMapFile = NULL;
  171. // CreateMutexW keeps failing with ERROR_NOACCESS
  172. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  173. DebugAssert(FDDExtMutex != NULL);
  174. FDDTargetControl = NULL;
  175. FDelayedDeletionTimer = NULL;
  176. FDelayedDeletionList = new TStringList();
  177. FDDFileList = NULL;
  178. FPendingTempSpaceWarn = false;
  179. FCapturedLog = NULL;
  180. FDragDropOperation = false;
  181. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  182. FAllowTransferPresetAutoSelect = true;
  183. FCopyParamDefault = L"";
  184. FSynchronizeController = NULL;
  185. FPendingQueueActionItem = NULL;
  186. FLockLevel = 0;
  187. FLockSuspendLevel = 0;
  188. FDisabledOnLockSuspend = false;
  189. FAlternativeDelete = false;
  190. FTrayIcon = new ::TTrayIcon(0);
  191. FTrayIcon->OnClick = TrayIconClick;
  192. FMaxQueueLength = 0;
  193. FLastContextPopupScreenPoint = TPoint(-1, -1);
  194. FTransferResumeList = NULL;
  195. FMoveToQueue = false;
  196. StandaloneOperation = false;
  197. FOnFeedSynchronizeError = NULL;
  198. FOnSynchronizeAbort = NULL;
  199. FSynchronizeTerminal = NULL;
  200. FNeedSession = false;
  201. FDoNotIdleCurrentTerminal = 0;
  202. FIncrementalSearching = 0;
  203. FIncrementalSearchHaveNext = false;
  204. FQueueFileList.reset(new TQueueFileList());
  205. FDownloadingFromClipboard = false;
  206. FClipboardFakeMonitorsPendingReset = false;
  207. FEditorManager = new TEditorManager();
  208. FEditorManager->OnFileChange = ExecutedFileChanged;
  209. FEditorManager->OnFileReload = ExecutedFileReload;
  210. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  211. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  212. FLocalEditors = new TList();
  213. FQueueStatus = NULL;
  214. FQueueStatusSection = new TCriticalSection();
  215. FQueueStatusInvalidated = false;
  216. FQueueItemInvalidated = false;
  217. FQueueStatusUpdating = false;
  218. FQueueActedItem = NULL;
  219. FQueueController = new TQueueController(QueueView3);
  220. FUserActionTimer = new TTimer(this);
  221. FUserActionTimer->Enabled = false;
  222. FUserActionTimer->Interval = 10;
  223. FUserActionTimer->OnTimer = UserActionTimer;
  224. FNotes = new TStringList();
  225. FNoteTimer = new TTimer(this);
  226. FNoteTimer->Enabled = false;
  227. FNoteTimer->OnTimer = NoteTimer;
  228. FOnNoteClick = NULL;
  229. FOle32Library = LoadLibrary(L"Ole32.dll");
  230. FDragMoveCursor = FOle32Library != NULL ?
  231. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  232. UseSystemSettings(this);
  233. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  234. static_cast<TObject*>(GetComponent(fcTransferList)));
  235. DebugAssert(TransferList != NULL);
  236. FTransferListHoverIndex = -1;
  237. TransferList->OnChange = TransferListChange;
  238. TransferList->OnDrawItem = TransferListDrawItem;
  239. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))));
  240. SetSubmenu(NonVisualDataModule->ColorMenuItem);
  241. UseDesktopFont(SessionsPageControl);
  242. UpdateSessionsPageControlHeight();
  243. UseDesktopFont(RemoteDirView);
  244. UseDesktopFont(RemoteDriveView);
  245. UseDesktopFont(QueueView3);
  246. UseDesktopFont(QueueFileList);
  247. UseDesktopFont(QueueLabel);
  248. UseDesktopFont(RemoteStatusBar);
  249. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  250. QueueSplitter->ShowHint = true;
  251. reinterpret_cast<TLabel*>(QueueFileListSplitter)->OnDblClick = QueueFileListSplitterDblClick;
  252. QueueFileListSplitter->ShowHint = true;
  253. RemotePanelSplitter->ShowHint = true;
  254. UpdateImages();
  255. FCustomCommandMenu = CreateTBXPopupMenu(this);
  256. FCustomCommandLocalFileList = NULL;
  257. FCustomCommandRemoteFileList = NULL;
  258. FSessionColors = new TPngImageList(this);
  259. FSessionColors->ColorDepth = cd32Bit;
  260. AddFixedSessionImages();
  261. SessionsPageControl->Images = FSessionColors;
  262. UpdateQueueLabel();
  263. FRemoteDirViewWasFocused = true;
  264. CreateHiddenWindow();
  265. StartUpdates();
  266. }
  267. //---------------------------------------------------------------------------
  268. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  269. {
  270. FInvalid = true;
  271. // this has to be one of the very first things to do
  272. StopUpdateThread();
  273. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  274. TTerminalManager::Instance()->ScpExplorer = NULL;
  275. delete FCustomCommandLocalFileList;
  276. delete FCustomCommandRemoteFileList;
  277. delete FCustomCommandMenu;
  278. delete FTrayIcon;
  279. FTrayIcon = NULL;
  280. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  281. delete FEditorManager;
  282. ForceCloseLocalEditors();
  283. delete FLocalEditors;
  284. HideFileFindDialog();
  285. if (FDelayedDeletionTimer)
  286. {
  287. DoDelayedDeletion(NULL);
  288. SAFE_DESTROY(FDelayedDeletionTimer);
  289. }
  290. SAFE_DESTROY(FDelayedDeletionList);
  291. // sometimes we do not get DDEnd so the list is not released
  292. SAFE_DESTROY(FDDFileList);
  293. DebugAssert(FSynchronizeController == NULL);
  294. CloseHandle(FDDExtMutex);
  295. FDDExtMutex = NULL;
  296. FreeLibrary(FOle32Library);
  297. FOle32Library = NULL;
  298. FDragMoveCursor = NULL;
  299. DebugAssert(!FErrorList);
  300. if (FEverShown)
  301. {
  302. // when window is never shown (like when running command-line operation),
  303. // particularly window site is not restored correctly (BoundsRect value set
  304. // in RestoreForm gets lost during handle allocation), so we do not want
  305. // it to be stored
  306. StoreParams();
  307. }
  308. Terminal = NULL;
  309. Queue = NULL;
  310. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  311. NonVisualDataModule->ScpExplorer = NULL;
  312. delete FQueueController;
  313. FQueueController = NULL;
  314. delete FQueueStatusSection;
  315. FQueueStatusSection = NULL;
  316. delete FQueueStatus;
  317. FQueueStatus = NULL;
  318. delete FUserActionTimer;
  319. FUserActionTimer = NULL;
  320. delete FNoteTimer;
  321. delete FNotes;
  322. delete FNoteData;
  323. SAFE_DESTROY(FDocks);
  324. SessionsPageControl->Images = NULL;
  325. // only after clearing Terminal (after DoTerminalListChanged => ... => AddSessionColor is called)
  326. SAFE_DESTROY(FSessionColors);
  327. SAFE_DESTROY(FSessionsDragDropFilesEx);
  328. SAFE_DESTROY(FHistoryMenu[0][0]);
  329. SAFE_DESTROY(FHistoryMenu[0][1]);
  330. SAFE_DESTROY(FHistoryMenu[1][0]);
  331. SAFE_DESTROY(FHistoryMenu[1][1]);
  332. if (FHiddenWindow != NULL)
  333. {
  334. DestroyWindow(FHiddenWindow);
  335. FHiddenWindow = NULL;
  336. }
  337. }
  338. //---------------------------------------------------------------------------
  339. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  340. {
  341. std::unique_ptr<TSessionData> Data;
  342. if (!Session.IsEmpty())
  343. {
  344. bool DefaultsOnly;
  345. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  346. }
  347. TTerminalManager * Manager = TTerminalManager::Instance();
  348. for (int Index = 0; Index < Manager->Count; Index++)
  349. {
  350. TTerminal * Terminal = Manager->Terminals[Index];
  351. if (Session.IsEmpty() ||
  352. Manager->IsActiveTerminalForSite(Terminal, Data.get()))
  353. {
  354. if (Path.IsEmpty())
  355. {
  356. Terminal->ClearCaches();
  357. }
  358. else
  359. {
  360. Terminal->DirectoryModified(Path, true);
  361. }
  362. }
  363. }
  364. // We should flag a pending refresh for the background terminals or busy foreground terminals
  365. if (HasActiveTerminal() &&
  366. CanCommandLineFromAnotherInstance() &&
  367. (Session.IsEmpty() || Manager->IsActiveTerminalForSite(Terminal, Data.get())) &&
  368. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  369. {
  370. Terminal->ReloadDirectory();
  371. }
  372. }
  373. //---------------------------------------------------------------------------
  374. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  375. {
  376. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  377. size_t MessageSize = sizeof(TCopyDataMessage);
  378. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  379. if (Result)
  380. {
  381. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  382. Result = (Message.Version == TCopyDataMessage::Version1);
  383. if (Result)
  384. {
  385. switch (Message.Command)
  386. {
  387. case TCopyDataMessage::CommandCanCommandLine:
  388. Result = CanCommandLineFromAnotherInstance();
  389. break;
  390. case TCopyDataMessage::CommandCommandLine:
  391. {
  392. UnicodeString CommandLine(Message.CommandLine);
  393. Result = CommandLineFromAnotherInstance(CommandLine);
  394. }
  395. break;
  396. case TCopyDataMessage::RefreshPanel:
  397. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  398. break;
  399. case TCopyDataMessage::MainWindowCheck:
  400. Result = true;
  401. break;
  402. default:
  403. Result = false;
  404. break;
  405. }
  406. }
  407. }
  408. Message.Result = Result ? 1 : 0;
  409. }
  410. //---------------------------------------------------------------------------
  411. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  412. {
  413. WNDCLASS WindowClass = {0};
  414. WindowClass.lpfnWndProc = DefWindowProc;
  415. WindowClass.hInstance = HInstance;
  416. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  417. FHiddenWindow = NULL;
  418. if (RegisterClass(&WindowClass))
  419. {
  420. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  421. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  422. }
  423. }
  424. //---------------------------------------------------------------------------
  425. bool __fastcall TCustomScpExplorerForm::CanConsole()
  426. {
  427. return
  428. HasActiveTerminal() &&
  429. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  430. }
  431. //---------------------------------------------------------------------------
  432. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  433. {
  434. bool Result = Visible && !NonVisualDataModule->Busy;
  435. return Result;
  436. }
  437. //---------------------------------------------------------------------------
  438. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  439. const UnicodeString & CommandLine)
  440. {
  441. TProgramParams Params(CommandLine);
  442. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  443. if (Result)
  444. {
  445. NonVisualDataModule->StartBusy();
  446. try
  447. {
  448. // this action is initiated from another process,
  449. // so it's likely that our window is not visible,
  450. // and user won't see what is going on
  451. Application->BringToFront();
  452. // reload sessions as we may be asked to open a session
  453. // just stored by another instance
  454. StoredSessions->Reload();
  455. UnicodeString SessionName = Params.Param[1];
  456. std::unique_ptr<TObjectList> DataList(new TObjectList());
  457. try
  458. {
  459. UnicodeString DownloadFile; // unused
  460. int Flags = GetCommandLineParseUrlFlags(&Params);
  461. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this, Flags);
  462. }
  463. catch (EAbort &)
  464. {
  465. // Happens when opening session in PuTTY and in other situations,
  466. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  467. // to process the command-line on its own (or to pass it to yet another instance)
  468. }
  469. if (DataList->Count > 0)
  470. {
  471. TTerminalManager * Manager = TTerminalManager::Instance();
  472. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  473. }
  474. }
  475. __finally
  476. {
  477. NonVisualDataModule->EndBusy();
  478. }
  479. }
  480. return Result;
  481. }
  482. //---------------------------------------------------------------------------
  483. void __fastcall TCustomScpExplorerForm::SetTerminal(TManagedTerminal * value)
  484. {
  485. if (FTerminal != value)
  486. {
  487. TerminalChanging();
  488. DoSetTerminal(value, false);
  489. }
  490. }
  491. //---------------------------------------------------------------------------
  492. void __fastcall TCustomScpExplorerForm::DoSetTerminal(TManagedTerminal * value, bool Replace)
  493. {
  494. FTerminal = value;
  495. bool PrevAllowTransferPresetAutoSelect = FAllowTransferPresetAutoSelect;
  496. FAllowTransferPresetAutoSelect = false;
  497. try
  498. {
  499. TerminalChanged(Replace);
  500. }
  501. __finally
  502. {
  503. FAllowTransferPresetAutoSelect = PrevAllowTransferPresetAutoSelect;
  504. }
  505. if (Terminal != NULL)
  506. {
  507. TransferPresetAutoSelect();
  508. }
  509. }
  510. //---------------------------------------------------------------------------
  511. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  512. {
  513. DoSetTerminal(value, true);
  514. }
  515. //---------------------------------------------------------------------------
  516. void __fastcall TCustomScpExplorerForm::TerminalChanging()
  517. {
  518. if (FTerminal != NULL)
  519. {
  520. UpdateTerminal(Terminal);
  521. }
  522. }
  523. //---------------------------------------------------------------------------
  524. void __fastcall TCustomScpExplorerForm::TerminalChanged(bool Replaced)
  525. {
  526. if (Terminal != NULL)
  527. {
  528. UpdateSessionColor((TColor)Terminal->StateData->Color);
  529. }
  530. DoTerminalListChanged();
  531. DebugAssert(!IsLocalBrowserMode());
  532. if (Replaced)
  533. {
  534. RemoteDirView->ReplaceTerminal(Terminal);
  535. }
  536. else
  537. {
  538. RemoteDirView->Terminal = Terminal;
  539. }
  540. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  541. if (Terminal != NULL)
  542. {
  543. if (Terminal->Active)
  544. {
  545. Terminal->RefreshDirectory();
  546. }
  547. if (WinConfiguration->PreservePanelState)
  548. {
  549. if (Terminal->RemoteExplorerState != NULL)
  550. {
  551. DirView(osRemote)->RestoreState(Terminal->RemoteExplorerState);
  552. }
  553. else
  554. {
  555. DirView(osRemote)->ClearState();
  556. }
  557. }
  558. if (!Replaced && Terminal->Active)
  559. {
  560. InitStatusBar();
  561. }
  562. }
  563. UpdateTransferList();
  564. // Update panels Enable state before refreshing the labels
  565. UpdateControls();
  566. }
  567. //---------------------------------------------------------------------------
  568. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  569. {
  570. if (Queue != value)
  571. {
  572. if (FQueue != NULL)
  573. {
  574. FQueue->OnListUpdate = NULL;
  575. FQueue->OnQueueItemUpdate = NULL;
  576. }
  577. FQueue = value;
  578. if (FQueue != NULL)
  579. {
  580. DebugAssert(FQueue->OnListUpdate == NULL);
  581. FQueue->OnListUpdate = QueueListUpdate;
  582. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  583. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  584. }
  585. QueueChanged();
  586. }
  587. }
  588. //---------------------------------------------------------------------------
  589. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  590. TListItem * Item)
  591. {
  592. if (FQueueActedItem == Item)
  593. {
  594. FQueueActedItem = NULL;
  595. if ((QueueView3->PopupMenu != NULL) &&
  596. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  597. {
  598. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  599. ReleaseCapture();
  600. }
  601. }
  602. if (Item->Data == FPendingQueueActionItem)
  603. {
  604. FPendingQueueActionItem = NULL;
  605. }
  606. }
  607. //---------------------------------------------------------------------------
  608. bool TCustomScpExplorerForm::IsAnythingQueued()
  609. {
  610. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  611. }
  612. //---------------------------------------------------------------------------
  613. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  614. {
  615. {
  616. TGuard Guard(FQueueStatusSection);
  617. FQueueStatusInvalidated = false;
  618. if (FQueue != NULL)
  619. {
  620. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  621. }
  622. }
  623. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  624. {
  625. FMaxQueueLength = FQueueStatus->Count;
  626. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  627. }
  628. {
  629. TAutoFlag Flag(FQueueStatusUpdating);
  630. FQueueController->UpdateQueueStatus(FQueueStatus);
  631. }
  632. UpdateQueueFileList();
  633. SetQueueProgress();
  634. UpdateQueueView();
  635. bool IsEmpty = !IsAnythingQueued();
  636. if (IsEmpty && (Terminal != NULL))
  637. {
  638. TOnceDoneOperation OnceDoneOperation =
  639. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  640. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  641. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  642. {
  643. FQueue->Enabled = false;
  644. }
  645. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  646. {
  647. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  648. }
  649. }
  650. }
  651. //---------------------------------------------------------------------------
  652. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  653. {
  654. UnicodeString Result;
  655. if (FQueueStatus != NULL)
  656. {
  657. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  658. {
  659. TFileOperationProgressType * ProgressData =
  660. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  661. if ((ProgressData != NULL) && ProgressData->InProgress)
  662. {
  663. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  664. }
  665. }
  666. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  667. {
  668. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  669. }
  670. }
  671. return Result;
  672. }
  673. //---------------------------------------------------------------------------
  674. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  675. {
  676. ComponentVisible[fcQueueView] =
  677. (WinConfiguration->QueueView.Show == qvShow) ||
  678. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  679. (FQueueStatus != NULL) && (FQueueStatus->Count > 0));
  680. }
  681. //---------------------------------------------------------------------------
  682. void __fastcall TCustomScpExplorerForm::QueueChanged()
  683. {
  684. if (FQueueStatus != NULL)
  685. {
  686. // UpdateFileList implementation relies on the status being recreated when session changes
  687. delete FQueueStatus;
  688. FQueueStatus = NULL;
  689. }
  690. UpdateQueueStatus(true);
  691. }
  692. //---------------------------------------------------------------------------
  693. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  694. {
  695. if (FQueue == Queue)
  696. {
  697. FQueueStatusInvalidated = true;
  698. }
  699. }
  700. //---------------------------------------------------------------------------
  701. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  702. TQueueItem * Item)
  703. {
  704. if (FQueue == Queue)
  705. {
  706. TGuard Guard(FQueueStatusSection);
  707. // this may be running in parallel with QueueChanged
  708. if (FQueueStatus != NULL)
  709. {
  710. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  711. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  712. {
  713. FRefreshLocalDirectory = (QueueItem == NULL) ||
  714. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  715. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  716. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  717. }
  718. if (QueueItem != NULL)
  719. {
  720. QueueItem->UserData = (void*)true;
  721. FQueueItemInvalidated = true;
  722. }
  723. }
  724. }
  725. }
  726. //---------------------------------------------------------------------------
  727. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  728. {
  729. // during standalone editing, we have no way to see/control queue,
  730. // so we have to always popup prompts automatically
  731. return StandaloneOperation || GUIConfiguration->QueueAutoPopup;
  732. }
  733. //---------------------------------------------------------------------------
  734. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  735. {
  736. if (FQueueStatus != NULL)
  737. {
  738. bool QueueAutoPopup = IsQueueAutoPopup();
  739. bool NeedRefresh = FQueueController->NeedRefresh();
  740. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  741. FQueueItemInvalidated = false;
  742. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  743. bool Updated = false;
  744. TQueueItemProxy * QueueItem;
  745. bool WasUserAction;
  746. for (int Index = 0; Index < Limit; Index++)
  747. {
  748. bool Update = false;
  749. QueueItem = FQueueStatus->Items[Index];
  750. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  751. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  752. (FPendingQueueActionItem == NULL))
  753. {
  754. FPendingQueueActionItem = QueueItem;
  755. FUserActionTimer->Enabled = true;
  756. }
  757. if ((bool)QueueItem->UserData)
  758. {
  759. QueueItem->UserData = (void*)false;
  760. QueueItem->Update();
  761. Updated = true;
  762. Update = true;
  763. }
  764. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  765. {
  766. Update = true;
  767. }
  768. if (Update)
  769. {
  770. FQueueController->RefreshQueueItem(QueueItem);
  771. }
  772. }
  773. if (Updated)
  774. {
  775. NonVisualDataModule->UpdateNonVisibleActions();
  776. SetQueueProgress();
  777. UpdateQueueFileList();
  778. }
  779. }
  780. }
  781. //---------------------------------------------------------------------------
  782. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  783. {
  784. if (FTaskbarList != NULL)
  785. {
  786. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  787. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  788. }
  789. }
  790. //---------------------------------------------------------------------------
  791. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  792. {
  793. if (Progress >= 0)
  794. {
  795. if (FTaskbarList != NULL)
  796. {
  797. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  798. }
  799. }
  800. else
  801. {
  802. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  803. }
  804. }
  805. //---------------------------------------------------------------------------
  806. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  807. {
  808. int OverallProgress;
  809. // FProgressForm is null when this is called from SetQueueProgress
  810. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  811. {
  812. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  813. }
  814. else if (!ProgressData->IsIndeterminate())
  815. {
  816. OverallProgress = ProgressData->OverallProgress();
  817. }
  818. else
  819. {
  820. OverallProgress = -1;
  821. }
  822. SetTaskbarListProgressValue(OverallProgress);
  823. }
  824. //---------------------------------------------------------------------------
  825. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  826. {
  827. TTerminalManager::Instance()->QueueStatusUpdated();
  828. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  829. {
  830. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  831. {
  832. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  833. {
  834. TFileOperationProgressType * ProgressData = NULL;
  835. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  836. {
  837. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  838. }
  839. if ((ProgressData != NULL) &&
  840. ProgressData->InProgress)
  841. {
  842. SetTaskbarListProgressValue(ProgressData);
  843. }
  844. else
  845. {
  846. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  847. }
  848. }
  849. else
  850. {
  851. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  852. }
  853. }
  854. else
  855. {
  856. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  857. }
  858. }
  859. UpdateQueueLabel();
  860. }
  861. //---------------------------------------------------------------------------
  862. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  863. {
  864. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  865. if (FQueueStatus != NULL)
  866. {
  867. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  868. if (ActiveAndPendingPrimaryCount > 0)
  869. {
  870. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  871. }
  872. }
  873. QueueLabel->Caption = Caption;
  874. }
  875. //---------------------------------------------------------------------------
  876. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  877. {
  878. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  879. static_cast<TComponent*>(GetComponent(fcTransferList)));
  880. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  881. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  882. TransferList->Strings->BeginUpdate();
  883. try
  884. {
  885. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  886. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  887. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  888. if (FTransferDropDownHint.IsEmpty())
  889. {
  890. FTransferDropDownHint = TransferDropDown->Hint;
  891. }
  892. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  893. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  894. TransferDropDown->Text = StripHotkey(Name);
  895. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  896. (FTransferDropDownHint, StripHotkey(Name),
  897. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  898. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General))));
  899. // update the label, otherwise when it is updated only on the first draw
  900. // of the list, it is drawn "bold" for some reason
  901. FTransferListHoverIndex = TransferList->ItemIndex;
  902. UpdateTransferLabel();
  903. }
  904. __finally
  905. {
  906. TransferList->Strings->EndUpdate();
  907. }
  908. }
  909. //---------------------------------------------------------------------------
  910. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  911. {
  912. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  913. DebugAssert(Toolbar != NULL);
  914. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  915. }
  916. //---------------------------------------------------------------------------
  917. void __fastcall TCustomScpExplorerForm::UpdateActions()
  918. {
  919. TForm::UpdateActions();
  920. if (ComponentVisible[fcCustomCommandsBand])
  921. {
  922. UpdateCustomCommandsToolbar();
  923. }
  924. }
  925. //---------------------------------------------------------------------------
  926. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  927. {
  928. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  929. }
  930. //---------------------------------------------------------------------------
  931. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  932. {
  933. Color = GetBtnFaceColor();
  934. DebugAssert(Configuration && RemoteDirView);
  935. RemoteDirView->DDAllowMove = !WinConfiguration->DDDisableMove;
  936. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  937. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  938. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  939. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  940. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  941. if (RemoteDirView->RowSelect != WinConfiguration->FullRowSelect)
  942. {
  943. RemoteDirView->RowSelect = WinConfiguration->FullRowSelect;
  944. // Without this, the panel turns gray and unused part of header turns black
  945. RemoteDirView->Perform(CM_COLORCHANGED, 0, 0);
  946. }
  947. RemoteDriveView->DDAllowMove = !WinConfiguration->DDDisableMove;
  948. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  949. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  950. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  951. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  952. UpdateSessionsPageControlHeight();
  953. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  954. UpdateToolbarDisplayMode();
  955. UpdateTransferList();
  956. if (Terminal != NULL)
  957. {
  958. TransferPresetAutoSelect();
  959. }
  960. UpdateQueueView();
  961. UpdateControls();
  962. // this can be called even before constuctor finishes.
  963. if (FEditorManager != NULL)
  964. {
  965. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  966. }
  967. // this can be called even before constuctor finishes.
  968. if (FLocalEditors != NULL)
  969. {
  970. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  971. {
  972. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  973. }
  974. }
  975. if (ComponentVisible[fcCustomCommandsBand])
  976. {
  977. UpdateCustomCommandsToolbar();
  978. }
  979. // show only when keeping queue items forever,
  980. // otherwise, it's enough to have it in the context menu
  981. QueueDeleteAllDoneQueueToolbarItem->Visible =
  982. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  983. if (FFileColorsCurrent != WinConfiguration->FileColors)
  984. {
  985. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  986. FFileColorsCurrent = WinConfiguration->FileColors;
  987. FileColorsChanged();
  988. }
  989. }
  990. //---------------------------------------------------------------------------
  991. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  992. {
  993. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  994. DirView->Invalidate();
  995. }
  996. //---------------------------------------------------------------------------
  997. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  998. {
  999. DoFileColorsChanged(RemoteDirView);
  1000. }
  1001. //---------------------------------------------------------------------------
  1002. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  1003. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  1004. void * /*Arg*/)
  1005. {
  1006. if (Token != NULL)
  1007. {
  1008. TForm * Editor = dynamic_cast<TForm*>(Token);
  1009. DebugAssert(Editor != NULL);
  1010. ReconfigureEditorForm(Editor);
  1011. }
  1012. }
  1013. //---------------------------------------------------------------------------
  1014. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  1015. {
  1016. WinConfiguration->DDTransferConfirmation = asOn;
  1017. }
  1018. //---------------------------------------------------------------------------
  1019. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1020. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1021. {
  1022. // noop
  1023. }
  1024. //---------------------------------------------------------------------------
  1025. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1026. TTransferDirection Direction, TTransferType Type, bool Temp,
  1027. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1028. bool Confirm, bool DragDrop, int Options)
  1029. {
  1030. bool Result = true;
  1031. DebugAssert(HasActiveTerminal());
  1032. // these parameters are known in advance
  1033. int Params =
  1034. FLAGMASK(Type == ttMove, cpDelete);
  1035. bool ToTemp = (Temp && (Direction == tdToLocal));
  1036. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1037. {
  1038. bool DisableNewerOnly =
  1039. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1040. ToTemp;
  1041. Options |=
  1042. FLAGMASK(ToTemp, coTemp) |
  1043. FLAGMASK(!Terminal->IsCapable[fcBackgroundTransfers], coDisableQueue) |
  1044. coDoNotShowAgain;
  1045. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1046. int CopyParamAttrs = (Direction == tdToRemote ?
  1047. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1048. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1049. int OutputOptions =
  1050. FLAGMASK(DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto),
  1051. cooDoNotShowAgain);
  1052. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1053. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1054. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1055. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1056. if (Result)
  1057. {
  1058. if (FLAGSET(OutputOptions, cooDoNotShowAgain))
  1059. {
  1060. if (DragDrop)
  1061. {
  1062. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1063. {
  1064. PopupTrayBalloon(NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation,
  1065. NULL, 0, EnableDDTransferConfirmation, NULL);
  1066. }
  1067. WinConfiguration->DDTransferConfirmation = asOff;
  1068. }
  1069. else
  1070. {
  1071. WinConfiguration->ConfirmTransferring = false;
  1072. }
  1073. }
  1074. else
  1075. {
  1076. // User explicitly unchecked "do not show again",
  1077. // so show him the dialog the next time
  1078. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1079. {
  1080. WinConfiguration->DDTransferConfirmation = asOn;
  1081. }
  1082. }
  1083. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1084. }
  1085. }
  1086. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fcBackgroundTransfers])
  1087. {
  1088. Configuration->Usage->Inc(L"TransfersOnBackground");
  1089. // these parameter are known only after transfer dialog
  1090. Params |=
  1091. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1092. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1093. Result = false;
  1094. ClearTransferSourceSelection(Direction);
  1095. }
  1096. return Result;
  1097. }
  1098. //---------------------------------------------------------------------------
  1099. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1100. {
  1101. if (FOnFileOperationFinished != NULL)
  1102. {
  1103. FOnFileOperationFinished(UnicodeString(), true);
  1104. }
  1105. else
  1106. {
  1107. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1108. if (HasDirView[Side])
  1109. {
  1110. DirView(Side)->SelectAll(smNone);
  1111. }
  1112. }
  1113. }
  1114. //---------------------------------------------------------------------------
  1115. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1116. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1117. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1118. int Params)
  1119. {
  1120. DebugAssert(Queue != NULL);
  1121. bool SingleFile = false;
  1122. if (FileList->Count == 1)
  1123. {
  1124. if (Direction == tdToRemote)
  1125. {
  1126. UnicodeString FileName = FileList->Strings[0];
  1127. SingleFile = FileExists(ApiPath(FileName));
  1128. }
  1129. else
  1130. {
  1131. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1132. SingleFile = !File->IsDirectory;
  1133. }
  1134. }
  1135. TQueueItem * QueueItem;
  1136. if (Direction == tdToRemote)
  1137. {
  1138. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1139. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1140. }
  1141. else
  1142. {
  1143. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1144. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1145. }
  1146. AddQueueItem(Queue, QueueItem, Terminal);
  1147. }
  1148. //---------------------------------------------------------------------------
  1149. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1150. {
  1151. if (Queue->IsEmpty)
  1152. {
  1153. Terminal->QueueOperationStart = Now();
  1154. }
  1155. Queue->AddItem(QueueItem);
  1156. }
  1157. //---------------------------------------------------------------------------
  1158. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1159. {
  1160. }
  1161. //---------------------------------------------------------------------------
  1162. void __fastcall TCustomScpExplorerForm::InitControls()
  1163. {
  1164. DebugAssert(FDocks != NULL);
  1165. for (int Index = 0; Index < ComponentCount; Index++)
  1166. {
  1167. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1168. if ((Dock != NULL) && (Dock->Tag == 0))
  1169. {
  1170. FDocks->Add(Dock);
  1171. }
  1172. }
  1173. CollectItemsWithTextDisplayMode(this);
  1174. }
  1175. //---------------------------------------------------------------------------
  1176. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1177. {
  1178. ConfigurationChanged();
  1179. QueuePanel->Height =
  1180. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1181. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1182. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1183. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1184. QueueFileList->Visible = WinConfiguration->QueueView.FileList;
  1185. QueueFileList->Height =
  1186. LoadDimension(WinConfiguration->QueueView.FileListHeight, WinConfiguration->QueueView.FileListHeightPixelsPerInch, this);
  1187. if (QueueFileList->Visible)
  1188. {
  1189. AdjustQueueLayout();
  1190. }
  1191. UpdateCustomCommandsToolbar();
  1192. }
  1193. //---------------------------------------------------------------------------
  1194. void __fastcall TCustomScpExplorerForm::StoreParams()
  1195. {
  1196. int APixelsPerInch = GetControlPixelsPerInch(this);
  1197. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1198. WinConfiguration->QueueView.HeightPixelsPerInch = APixelsPerInch;
  1199. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1200. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1201. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1202. WinConfiguration->QueueView.FileList = QueueFileList->Visible;
  1203. WinConfiguration->QueueView.FileListHeight = QueueFileList->Height;
  1204. WinConfiguration->QueueView.FileListHeightPixelsPerInch = APixelsPerInch;
  1205. }
  1206. //---------------------------------------------------------------------------
  1207. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1208. {
  1209. if (!FFormRestored)
  1210. {
  1211. FFormRestored = true;
  1212. RestoreFormParams();
  1213. }
  1214. TForm::CreateParams(Params);
  1215. }
  1216. //---------------------------------------------------------------------------
  1217. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1218. {
  1219. DebugAssert(FDocks != NULL);
  1220. for (int Index = 0; Index < FDocks->Count; Index++)
  1221. {
  1222. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1223. }
  1224. }
  1225. //---------------------------------------------------------------------------
  1226. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1227. {
  1228. SetDockAllowDrag(true);
  1229. while (!ButtonsStr.IsEmpty())
  1230. {
  1231. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1232. UnicodeString Name = CutToChar(S, L'=', true);
  1233. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1234. {
  1235. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1236. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1237. {
  1238. while (!S.IsEmpty())
  1239. {
  1240. UnicodeString S2 = CutToChar(S, L',', true);
  1241. UnicodeString Name = CutToChar(S2, L':', true);
  1242. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1243. {
  1244. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1245. if (SameText(GetToolbarItemName(Item), Name))
  1246. {
  1247. // forward compatibility
  1248. UnicodeString S3 = CutToChar(S2, L':', true);
  1249. bool Visible = StrToIntDef(S3, 0);
  1250. Item->Visible = Visible;
  1251. }
  1252. }
  1253. }
  1254. }
  1255. }
  1256. }
  1257. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1258. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1259. }
  1260. //---------------------------------------------------------------------------
  1261. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1262. {
  1263. UnicodeString Result;
  1264. SetDockAllowDrag(true);
  1265. Result = ::GetToolbarsLayoutStr(this);
  1266. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1267. return Result;
  1268. }
  1269. //---------------------------------------------------------------------------
  1270. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1271. {
  1272. UnicodeString Result;
  1273. if (Item->Action != NULL)
  1274. {
  1275. Result = Item->Action->Name;
  1276. Result = RemoveSuffix(Result, L"Action", true);
  1277. }
  1278. else
  1279. {
  1280. Result = Item->Name;
  1281. Result = RemoveSuffix(Result, L"SubmenuItem", true);
  1282. Result = RemoveSuffix(Result, L"Item", true);
  1283. }
  1284. return Result;
  1285. }
  1286. //---------------------------------------------------------------------------
  1287. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1288. {
  1289. UnicodeString Result;
  1290. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1291. {
  1292. UnicodeString ToolbarStr;
  1293. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1294. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1295. {
  1296. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1297. {
  1298. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1299. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1300. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1301. // and save the changes here only.
  1302. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1303. {
  1304. UnicodeString Name = GetToolbarItemName(Item);
  1305. DebugAssert(Name.Pos(L"TBX") == 0);
  1306. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1307. }
  1308. }
  1309. }
  1310. if (!ToolbarStr.IsEmpty())
  1311. {
  1312. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1313. }
  1314. }
  1315. return Result;
  1316. }
  1317. //---------------------------------------------------------------------------
  1318. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1319. {
  1320. DebugAssert(FProgressForm == NULL);
  1321. bool AllowSkip = (Terminal != NULL) ? Terminal->IsCapable[fcSkipTransfer] : false;
  1322. FProgressForm = new TProgressForm(Application, (FTransferResumeList != NULL), AllowSkip, SynchronizeProgress);
  1323. FProgressForm->DeleteLocalToRecycleBin =
  1324. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1325. FProgressForm->DeleteRemoteToRecycleBin =
  1326. (Terminal != NULL) &&
  1327. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1328. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1329. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1330. LockWindow();
  1331. // Actually, do not know what hides the progress once the operation finishes
  1332. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1333. SetTaskbarListProgressState(TBPF_NORMAL);
  1334. }
  1335. //---------------------------------------------------------------------------
  1336. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1337. {
  1338. UnlockWindow();
  1339. SAFE_DESTROY(FProgressForm);
  1340. SetQueueProgress();
  1341. }
  1342. //---------------------------------------------------------------------------
  1343. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1344. TFileOperationProgressType & ProgressData)
  1345. {
  1346. // Download to temporary local directory
  1347. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1348. {
  1349. bool Continue = true;
  1350. FPendingTempSpaceWarn = false;
  1351. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1352. if (!Continue)
  1353. {
  1354. Abort();
  1355. }
  1356. }
  1357. // operation is being executed and we still didn't show up progress form
  1358. if (ProgressData.InProgress && !FProgressForm)
  1359. {
  1360. CreateProgressForm(NULL);
  1361. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1362. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1363. }
  1364. // operation is finished (or terminated), so we hide progress form
  1365. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1366. {
  1367. DestroyProgressForm();
  1368. if (ProgressData.Operation == foCalculateSize)
  1369. {
  1370. // When calculating size before transfer, the abort caused by
  1371. // cancel flag set due to "MoveToQueue" below,
  1372. // is not propagated back to us in ExecuteFileOperation,
  1373. // as it is silently swallowed.
  1374. // So we explicitly (re)throw it here.
  1375. if (FMoveToQueue)
  1376. {
  1377. Abort();
  1378. }
  1379. }
  1380. else
  1381. {
  1382. if ((ProgressData.Cancel == csContinue) &&
  1383. !FAutoOperation)
  1384. {
  1385. OperationComplete(ProgressData.StartTime);
  1386. }
  1387. }
  1388. }
  1389. if (FProgressForm &&
  1390. // Can happen only during synchronization, when ending one of the operations
  1391. ProgressData.InProgress)
  1392. {
  1393. FProgressForm->SetProgressData(ProgressData);
  1394. DebugAssert(ProgressData.InProgress);
  1395. SetTaskbarListProgressValue(&ProgressData);
  1396. if (FProgressForm->Cancel > csContinue)
  1397. {
  1398. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1399. if (FProgressForm->Cancel == csCancelFile)
  1400. {
  1401. FProgressForm->ClearCancel();
  1402. }
  1403. // cancel cancels even the move
  1404. FMoveToQueue = false;
  1405. }
  1406. else if (FProgressForm->MoveToQueue)
  1407. {
  1408. FMoveToQueue = true;
  1409. ProgressData.SetCancelAtLeast(csCancel);
  1410. }
  1411. if ((FTransferResumeList != NULL) &&
  1412. ProgressData.InProgress &&
  1413. ProgressData.IsTransfer() &&
  1414. !ProgressData.FullFileName.IsEmpty())
  1415. {
  1416. if ((FTransferResumeList->Count == 0) ||
  1417. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1418. {
  1419. // note that we do not recognize directories from files here
  1420. FTransferResumeList->Add(ProgressData.FullFileName);
  1421. }
  1422. }
  1423. }
  1424. }
  1425. //---------------------------------------------------------------------------
  1426. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1427. const TDateTime & StartTime)
  1428. {
  1429. if (GUIConfiguration->BeepOnFinish &&
  1430. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1431. {
  1432. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1433. DWORD Sound;
  1434. if (!ExtractFileExt(BeepSound).IsEmpty())
  1435. {
  1436. Sound = SND_FILENAME;
  1437. }
  1438. else
  1439. {
  1440. Sound = SND_ALIAS;
  1441. }
  1442. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1443. }
  1444. }
  1445. //---------------------------------------------------------------------------
  1446. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1447. TFileOperationProgressType & ProgressData)
  1448. {
  1449. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1450. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1451. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1452. FileOperationProgress(ProgressData);
  1453. }
  1454. //---------------------------------------------------------------------------
  1455. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1456. {
  1457. UnicodeString Result;
  1458. if (FProgressForm != NULL)
  1459. {
  1460. Result = FProgressForm->ProgressStr();
  1461. }
  1462. return Result;
  1463. }
  1464. //---------------------------------------------------------------------------
  1465. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1466. bool DragDrop)
  1467. {
  1468. return (!DragDrop && (DropSourceControl == NULL)) ||
  1469. (DropSourceControl == DirView(osOther));
  1470. }
  1471. //---------------------------------------------------------------------------
  1472. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1473. TFileOperation Operation, TOperationSide Side,
  1474. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1475. TOnceDoneOperation & OnceDoneOperation)
  1476. {
  1477. if (!FAutoOperation)
  1478. {
  1479. // no selection on "/upload", form serves only as event handler
  1480. // (it is not displayed)
  1481. if (PanelOperation(Side, FDragDropOperation) &&
  1482. Visible && (Operation != foCalculateSize) &&
  1483. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1484. {
  1485. if (FOnFileOperationFinished != NULL)
  1486. {
  1487. FOnFileOperationFinished(FileName, Success);
  1488. }
  1489. else
  1490. {
  1491. TCustomDirView * DView = DirView(Side);
  1492. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1493. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1494. // this can happen when local drive is unplugged in the middle of the operation
  1495. if (Item != NULL)
  1496. {
  1497. if (Success) Item->Selected = false;
  1498. DView->MakeProgressVisible(Item);
  1499. }
  1500. }
  1501. }
  1502. if (TFileOperationProgressType::IsTransferOperation(Operation))
  1503. {
  1504. DebugAssert(!IsLocalBrowserMode());
  1505. if (Side == osLocal)
  1506. {
  1507. Configuration->Usage->Inc(L"UploadedFiles");
  1508. }
  1509. else
  1510. {
  1511. Configuration->Usage->Inc(L"DownloadedFiles");
  1512. }
  1513. }
  1514. }
  1515. if (Success && (FSynchronizeController != NULL))
  1516. {
  1517. DebugAssert(!IsSideLocalBrowser(osRemote));
  1518. if (Operation == foCopy)
  1519. {
  1520. DebugAssert(Side == osLocal);
  1521. FSynchronizeController->LogOperation(soUpload, FileName);
  1522. }
  1523. else if (Operation == foDelete)
  1524. {
  1525. DebugAssert(Side == osRemote);
  1526. FSynchronizeController->LogOperation(soDelete, FileName);
  1527. }
  1528. }
  1529. if (FProgressForm)
  1530. {
  1531. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1532. }
  1533. }
  1534. //---------------------------------------------------------------------------
  1535. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1536. TFileOperation Operation, TOperationSide Side,
  1537. bool Temp, const UnicodeString & FileName, Boolean Success,
  1538. TOnceDoneOperation & OnceDoneOperation)
  1539. {
  1540. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1541. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1542. OnceDoneOperation);
  1543. }
  1544. //---------------------------------------------------------------------------
  1545. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1546. {
  1547. return false;
  1548. }
  1549. //---------------------------------------------------------------------------
  1550. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1551. {
  1552. return false;
  1553. }
  1554. //---------------------------------------------------------------------------
  1555. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1556. {
  1557. return NULL;
  1558. }
  1559. //---------------------------------------------------------------------------
  1560. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1561. {
  1562. DebugAssert(GetSide(Side) == osRemote);
  1563. DebugUsedParam(Side);
  1564. return RemoteDirView;
  1565. }
  1566. //---------------------------------------------------------------------------
  1567. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1568. {
  1569. DebugAssert(GetSide(Side) == osRemote);
  1570. DebugUsedParam(Side);
  1571. return RemoteDriveView;
  1572. }
  1573. //---------------------------------------------------------------------------
  1574. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1575. {
  1576. DebugAssert(GetSide(Side) == osRemote);
  1577. DebugUsedParam(Side);
  1578. return HasActiveTerminal();
  1579. }
  1580. //---------------------------------------------------------------------------
  1581. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1582. TOperationSide Side, int FilesOnly)
  1583. {
  1584. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1585. }
  1586. //---------------------------------------------------------------------------
  1587. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1588. TOperationSide Side, int FilesOnly)
  1589. {
  1590. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1591. }
  1592. //---------------------------------------------------------------------------
  1593. struct THistoryItemData
  1594. {
  1595. short int Side;
  1596. short int Index;
  1597. };
  1598. //---------------------------------------------------------------------------
  1599. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1600. {
  1601. DirView(Side)->HistoryGo(Index);
  1602. }
  1603. //---------------------------------------------------------------------------
  1604. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1605. {
  1606. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1607. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1608. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1609. }
  1610. //---------------------------------------------------------------------------
  1611. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1612. bool Back)
  1613. {
  1614. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1615. {
  1616. TCustomDirView * DView = DirView(Side);
  1617. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1618. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1619. if (ICount > 10)
  1620. {
  1621. ICount = 10;
  1622. }
  1623. Menu->Items->Clear();
  1624. THistoryItemData Data;
  1625. Data.Side = Side;
  1626. for (short int i = 1; i <= ICount; i++)
  1627. {
  1628. TTBCustomItem * Item = new TTBXItem(Menu);
  1629. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1630. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1631. Item->Hint = DView->HistoryPath[Data.Index];
  1632. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1633. Item->Tag = *reinterpret_cast<int*>(&Data);
  1634. Item->OnClick = HistoryItemClick;
  1635. Menu->Items->Add(Item);
  1636. }
  1637. }
  1638. }
  1639. //---------------------------------------------------------------------------
  1640. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1641. TOperationSide Side, bool Back)
  1642. {
  1643. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1644. {
  1645. // workaround
  1646. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1647. // operator new allocates memory in Pascal code, but calls inline
  1648. // contructor in C++, leading in problems, the function does
  1649. // both in Pascal code
  1650. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1651. UpdateHistoryMenu(Side, Back);
  1652. }
  1653. return FHistoryMenu[Side == osLocal][Back];
  1654. }
  1655. //---------------------------------------------------------------------------
  1656. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1657. TCustomDirView *Sender)
  1658. {
  1659. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1660. UpdateHistoryMenu(Side, true);
  1661. UpdateHistoryMenu(Side, false);
  1662. }
  1663. //---------------------------------------------------------------------------
  1664. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1665. {
  1666. // remote custom commands can be executed only if the server supports shell commands
  1667. // or have secondary shell
  1668. return HasActiveTerminal() && (FTerminal->IsCapable[fcSecondaryShell] || FTerminal->IsCapable[fcShellAnyCommand]);
  1669. }
  1670. //---------------------------------------------------------------------------
  1671. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1672. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1673. {
  1674. // -1 = hidden, 0 = disabled, 1 = enabled
  1675. int Result;
  1676. TFileCustomCommand RemoteCustomCommand;
  1677. TLocalCustomCommand LocalCustomCommand;
  1678. TFileCustomCommand * NonInteractiveCustomCommand =
  1679. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1680. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1681. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1682. if (FLAGCLEAR(Command.Params, ccLocal))
  1683. {
  1684. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1685. // custom command that does not operate with files can be executed anytime ...
  1686. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1687. {
  1688. // ... but do not show such command in remote file menu
  1689. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1690. {
  1691. Result = AllowedState;
  1692. }
  1693. else
  1694. {
  1695. Result = -1;
  1696. }
  1697. }
  1698. else
  1699. {
  1700. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1701. {
  1702. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1703. }
  1704. else
  1705. {
  1706. Result = -1;
  1707. }
  1708. }
  1709. }
  1710. else
  1711. {
  1712. // custom command that does not operate with files can be executed anytime
  1713. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1714. {
  1715. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1716. }
  1717. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1718. {
  1719. if ((ListType == ccltAll) || (ListType == ccltFile))
  1720. {
  1721. // special case is "diff"-style command that can be executed over any side,
  1722. // if we have both sides
  1723. Result =
  1724. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1725. // directly (instead of EnableSelectedOperation) to pass
  1726. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1727. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1728. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1729. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1730. }
  1731. else if (ListType == ccltBoth)
  1732. {
  1733. DebugAssert(!IsLocalBrowserMode());
  1734. Result = 1;
  1735. }
  1736. else
  1737. {
  1738. Result = -1;
  1739. }
  1740. }
  1741. else
  1742. {
  1743. if ((ListType == ccltAll) ||
  1744. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1745. {
  1746. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1747. }
  1748. else
  1749. {
  1750. Result = -1;
  1751. }
  1752. }
  1753. if ((Result > 0) &&
  1754. LocalCustomCommand.IsSessionCommand(Cmd) &&
  1755. !HasActiveTerminal())
  1756. {
  1757. Result = 0;
  1758. }
  1759. }
  1760. return Result;
  1761. }
  1762. //---------------------------------------------------------------------------
  1763. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1764. TStrings * FileList, const TCustomCommandType & ACommand,
  1765. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1766. {
  1767. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1768. {
  1769. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1770. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1771. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1772. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1773. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1774. bool Capture =
  1775. FLAGSET(ACommand.Params, ccShowResults) ||
  1776. FLAGSET(ACommand.Params, ccCopyResults) ||
  1777. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1778. TCaptureOutputEvent OutputEvent = NULL;
  1779. DebugAssert(FCapturedLog == NULL);
  1780. if (Capture)
  1781. {
  1782. FCapturedLog = new TStringList();
  1783. OutputEvent = TerminalCaptureLog;
  1784. }
  1785. try
  1786. {
  1787. if (!RemoteCustomCommand.IsFileCommand(Command))
  1788. {
  1789. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1790. OutputEvent);
  1791. }
  1792. else
  1793. {
  1794. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1795. }
  1796. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1797. {
  1798. if (FLAGSET(ACommand.Params, ccCopyResults))
  1799. {
  1800. CopyToClipboard(FCapturedLog);
  1801. }
  1802. if (FLAGSET(ACommand.Params, ccShowResults))
  1803. {
  1804. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1805. }
  1806. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1807. {
  1808. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1809. }
  1810. }
  1811. }
  1812. __finally
  1813. {
  1814. SAFE_DESTROY(FCapturedLog);
  1815. }
  1816. }
  1817. }
  1818. //---------------------------------------------------------------------------
  1819. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1820. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1821. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1822. {
  1823. DebugAssert(!IsLocalBrowserMode());
  1824. TStrings * LocalFileList = NULL;
  1825. TStrings * RemoteFileList = NULL;
  1826. TStrings * RemoteFileListFull = NULL;
  1827. try
  1828. {
  1829. if (LocalFileCommand)
  1830. {
  1831. if (ALocalFileList == NULL)
  1832. {
  1833. DebugAssert(HasDirView[osLocal]);
  1834. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1835. // directly (instead of EnableSelectedOperation) to pass
  1836. // false to FocusedFileOnlyWhenFocused
  1837. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1838. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1839. }
  1840. else
  1841. {
  1842. LocalFileList = ALocalFileList;
  1843. }
  1844. if (FileListCommand)
  1845. {
  1846. if (LocalFileList->Count != 1)
  1847. {
  1848. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1849. }
  1850. }
  1851. else
  1852. {
  1853. if ((LocalFileList->Count != 1) &&
  1854. (FileList->Count != 1) &&
  1855. (LocalFileList->Count != FileList->Count))
  1856. {
  1857. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1858. }
  1859. }
  1860. }
  1861. UnicodeString RootTempDir;
  1862. UnicodeString TempDir;
  1863. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1864. if (!RemoteFiles)
  1865. {
  1866. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1867. }
  1868. try
  1869. {
  1870. TDateTimes RemoteFileTimes;
  1871. if (RemoteFiles)
  1872. {
  1873. RemoteFileList = FileList;
  1874. }
  1875. else
  1876. {
  1877. RemoteFileList = new TStringList();
  1878. TMakeLocalFileListParams MakeFileListParam;
  1879. MakeFileListParam.FileList = RemoteFileList;
  1880. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1881. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1882. MakeFileListParam.Recursive =
  1883. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1884. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1885. if (!MakeFileListParam.Recursive)
  1886. {
  1887. RemoteFileTimes.clear();
  1888. RemoteFileListFull = new TStringList();
  1889. MakeFileListParam.FileList = RemoteFileListFull;
  1890. MakeFileListParam.Recursive = true;
  1891. MakeFileListParam.IncludeDirs = false;
  1892. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1893. }
  1894. else
  1895. {
  1896. RemoteFileListFull = RemoteFileList;
  1897. }
  1898. }
  1899. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1900. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1901. if (!NonBlocking)
  1902. {
  1903. int Count;
  1904. if (FileListCommand)
  1905. {
  1906. Count = 1;
  1907. }
  1908. else if (LocalFileCommand)
  1909. {
  1910. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  1911. }
  1912. else
  1913. {
  1914. Count = RemoteFileList->Count;
  1915. }
  1916. Progress.Start(foCustomCommand, osRemote, Count);
  1917. DebugAssert(FProgressForm != NULL);
  1918. FProgressForm->ReadOnly = true;
  1919. }
  1920. try
  1921. {
  1922. if (FileListCommand)
  1923. {
  1924. UnicodeString LocalFile;
  1925. // MakeFileList does not delimit filenames
  1926. UnicodeString FileList = MakeFileList(RemoteFileList);
  1927. if (LocalFileCommand)
  1928. {
  1929. DebugAssert(LocalFileList->Count == 1);
  1930. LocalFile = LocalFileList->Strings[0];
  1931. }
  1932. TLocalCustomCommand CustomCommand(Data,
  1933. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1934. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1935. if (NonBlocking)
  1936. {
  1937. DebugAssert(!POutput);
  1938. ExecuteShellChecked(ShellCommand);
  1939. }
  1940. else
  1941. {
  1942. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1943. }
  1944. }
  1945. else if (LocalFileCommand)
  1946. {
  1947. if (LocalFileList->Count == 1)
  1948. {
  1949. UnicodeString LocalFile = LocalFileList->Strings[0];
  1950. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1951. {
  1952. UnicodeString FileName = RemoteFileList->Strings[Index];
  1953. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1954. Progress.SetFile(FileNameForProgress);
  1955. TLocalCustomCommand CustomCommand(Data,
  1956. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  1957. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1958. TOnceDoneOperation OnceDoneOperation;
  1959. // Do not unselect anything, as it won't work with two panels anyway
  1960. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1961. }
  1962. }
  1963. else if (RemoteFileList->Count == 1)
  1964. {
  1965. UnicodeString FileName = RemoteFileList->Strings[0];
  1966. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1967. {
  1968. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1969. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1970. Progress.SetFile(LocalFileNameOnly);
  1971. TLocalCustomCommand CustomCommand(
  1972. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1973. FileName, LocalFileName, L"");
  1974. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1975. TOnceDoneOperation OnceDoneOperation;
  1976. // Do not unselect anything, as it won't work with two panels anyway
  1977. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1978. }
  1979. }
  1980. else
  1981. {
  1982. if (LocalFileList->Count != RemoteFileList->Count)
  1983. {
  1984. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  1985. }
  1986. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1987. {
  1988. UnicodeString FileName = RemoteFileList->Strings[Index];
  1989. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1990. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1991. Progress.SetFile(LocalFileNameOnly); // sic
  1992. TLocalCustomCommand CustomCommand(
  1993. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1994. FileName, LocalFileName, L"");
  1995. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1996. TOnceDoneOperation OnceDoneOperation;
  1997. // Do not unselect anything, as it won't work with two panels anyway
  1998. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1999. }
  2000. }
  2001. }
  2002. else
  2003. {
  2004. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2005. {
  2006. UnicodeString FileName = RemoteFileList->Strings[Index];
  2007. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2008. Progress.SetFile(FileNameForProgress);
  2009. TLocalCustomCommand CustomCommand(Data,
  2010. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2011. FileName, L"", L"");
  2012. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2013. TOnceDoneOperation OnceDoneOperation;
  2014. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  2015. }
  2016. }
  2017. }
  2018. __finally
  2019. {
  2020. if (!NonBlocking)
  2021. {
  2022. Progress.Stop();
  2023. }
  2024. }
  2025. DebugAssert(!FAutoOperation);
  2026. if (!RemoteFiles)
  2027. {
  2028. TempDir = IncludeTrailingBackslash(TempDir);
  2029. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  2030. {
  2031. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  2032. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  2033. // Skip directories, process only nested files.
  2034. // The check is redundant as FileAge fails for directories anyway.
  2035. !DirectoryExists(FileName))
  2036. {
  2037. UnicodeString RemoteDir =
  2038. UnixExtractFileDir(
  2039. UnixIncludeTrailingBackslash(FTerminal->CurrentDirectory) +
  2040. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2041. TDateTime NewTime;
  2042. if (FileAge(FileName, NewTime) &&
  2043. (NewTime != RemoteFileTimes[Index]))
  2044. {
  2045. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2046. TemporaryFileCopyParam(CopyParam);
  2047. CopyParam.FileMask = L"";
  2048. FAutoOperation = true;
  2049. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2050. TemporaryFilesList->Add(FileName);
  2051. FTerminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2052. }
  2053. }
  2054. }
  2055. }
  2056. }
  2057. __finally
  2058. {
  2059. FAutoOperation = false;
  2060. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2061. {
  2062. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2063. }
  2064. }
  2065. }
  2066. __finally
  2067. {
  2068. if (RemoteFileList != FileList)
  2069. {
  2070. delete RemoteFileList;
  2071. }
  2072. if (LocalFileList != ALocalFileList)
  2073. {
  2074. delete LocalFileList;
  2075. }
  2076. if (RemoteFileListFull != RemoteFileList)
  2077. {
  2078. delete RemoteFileListFull;
  2079. }
  2080. }
  2081. }
  2082. //---------------------------------------------------------------------------
  2083. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2084. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2085. bool FileListCommand, UnicodeString * POutput)
  2086. {
  2087. std::unique_ptr<TStrings> SelectedFileList(DirView(osLocal)->CreateFileList(false, true, NULL));
  2088. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2089. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2090. {
  2091. UnicodeString FileName = SelectedFileList->Strings[Index];
  2092. if (DirectoryExists(FileName))
  2093. {
  2094. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2095. {
  2096. LocalFileList->Add(FileName);
  2097. }
  2098. if (FLAGSET(ACommand.Params, ccRecursive))
  2099. {
  2100. TMakeLocalFileListParams MakeFileListParam;
  2101. MakeFileListParam.FileList = LocalFileList.get();
  2102. MakeFileListParam.FileTimes = NULL;
  2103. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2104. MakeFileListParam.Recursive = true;
  2105. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2106. }
  2107. }
  2108. else
  2109. {
  2110. LocalFileList->Add(FileName);
  2111. }
  2112. }
  2113. UnicodeString RemotePath;
  2114. if (Terminal != NULL)
  2115. {
  2116. RemotePath = Terminal->CurrentDirectory;
  2117. }
  2118. if (FileListCommand)
  2119. {
  2120. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2121. TLocalCustomCommand CustomCommand(
  2122. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2123. L"", L"", FileList);
  2124. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2125. }
  2126. else
  2127. {
  2128. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2129. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2130. DebugAssert(FProgressForm != NULL);
  2131. FProgressForm->ReadOnly = true;
  2132. try
  2133. {
  2134. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2135. {
  2136. UnicodeString FileName = LocalFileList->Strings[Index];
  2137. Progress.SetFile(FileName);
  2138. TLocalCustomCommand CustomCommand(
  2139. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2140. FileName, L"", L"");
  2141. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2142. TOnceDoneOperation OnceDoneOperation;
  2143. Progress.Finish(FileName, true, OnceDoneOperation);
  2144. }
  2145. }
  2146. __finally
  2147. {
  2148. Progress.Stop();
  2149. }
  2150. }
  2151. }
  2152. //---------------------------------------------------------------------------
  2153. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2154. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2155. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2156. {
  2157. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2158. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2159. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2160. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2161. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2162. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2163. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2164. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2165. std::unique_ptr<UnicodeString> POutput;
  2166. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2167. FLAGSET(ACommand.Params, ccCopyResults))
  2168. {
  2169. POutput.reset(new UnicodeString());
  2170. }
  2171. if (!LocalCustomCommand.IsFileCommand(Command))
  2172. {
  2173. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2174. }
  2175. // remote files?
  2176. else if ((FCurrentSide == osRemote) || LocalFileCommand)
  2177. {
  2178. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2179. }
  2180. // local files
  2181. else
  2182. {
  2183. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2184. }
  2185. if (POutput.get() != NULL)
  2186. {
  2187. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2188. int P = POutput->Pos(sLineBreak);
  2189. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2190. {
  2191. POutput->SetLength(P - 1);
  2192. }
  2193. // Copy even empty output to clipboard
  2194. if (FLAGSET(ACommand.Params, ccCopyResults))
  2195. {
  2196. CopyToClipboard(*POutput);
  2197. }
  2198. // But do not show an empty message box.
  2199. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2200. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2201. !POutput->IsEmpty())
  2202. {
  2203. TClipboardHandler ClipboardHandler;
  2204. ClipboardHandler.Text = *POutput;
  2205. TMessageParams Params;
  2206. TQueryButtonAlias Aliases[1];
  2207. Aliases[0].Button = qaRetry;
  2208. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2209. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2210. Params.Aliases = Aliases;
  2211. Params.AliasesCount = LENOF(Aliases);
  2212. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2213. }
  2214. }
  2215. }
  2216. //---------------------------------------------------------------------------
  2217. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2218. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2219. {
  2220. TCustomCommandData Data;
  2221. UnicodeString Site;
  2222. UnicodeString RemotePath;
  2223. if (HasActiveTerminal())
  2224. {
  2225. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  2226. Data = TCustomCommandData(SessionData.get());
  2227. Site = Terminal->SessionData->SessionKey;
  2228. RemotePath = Terminal->CurrentDirectory;
  2229. }
  2230. UnicodeString HelpKeyword = ACommand.HomePage;
  2231. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2232. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2233. {
  2234. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2235. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2236. {
  2237. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2238. }
  2239. else
  2240. {
  2241. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2242. }
  2243. if (!DoCustomCommandOptionsDialog(
  2244. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2245. {
  2246. Abort();
  2247. }
  2248. }
  2249. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2250. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2251. {
  2252. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2253. }
  2254. else
  2255. {
  2256. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2257. }
  2258. }
  2259. //---------------------------------------------------------------------------
  2260. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2261. const TCustomCommandType & Command)
  2262. {
  2263. DebugAssert(FCustomCommandLocalFileList != NULL);
  2264. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2265. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2266. TStrings * LocalFileList = new TStringList();
  2267. TStrings * RemoteFileList = new TStringList();
  2268. try
  2269. {
  2270. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2271. {
  2272. LocalFileList->Clear();
  2273. LocalFileList->AddObject(
  2274. FCustomCommandLocalFileList->Strings[Index],
  2275. FCustomCommandLocalFileList->Objects[Index]);
  2276. RemoteFileList->Clear();
  2277. RemoteFileList->AddObject(
  2278. FCustomCommandRemoteFileList->Strings[Index],
  2279. FCustomCommandRemoteFileList->Objects[Index]);
  2280. CustomCommand(RemoteFileList, Command, LocalFileList);
  2281. }
  2282. }
  2283. __finally
  2284. {
  2285. delete LocalFileList;
  2286. delete RemoteFileList;
  2287. }
  2288. }
  2289. //---------------------------------------------------------------------------
  2290. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2291. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2292. {
  2293. delete FCustomCommandLocalFileList;
  2294. delete FCustomCommandRemoteFileList;
  2295. // takeover ownership,
  2296. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2297. FCustomCommandLocalFileList = LocalFileList;
  2298. FCustomCommandRemoteFileList = RemoteFileList;
  2299. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2300. if (Button != NULL)
  2301. {
  2302. FCustomCommandMenu->Items->Clear();
  2303. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2304. MenuPopup(FCustomCommandMenu, Button);
  2305. }
  2306. else
  2307. {
  2308. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2309. }
  2310. }
  2311. //---------------------------------------------------------------------------
  2312. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2313. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2314. {
  2315. DebugAssert(FCapturedLog != NULL);
  2316. if ((OutputType == cotOutput) || (OutputType == cotError))
  2317. {
  2318. FCapturedLog->Add(AddedLine);
  2319. }
  2320. }
  2321. //---------------------------------------------------------------------------
  2322. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2323. TOperationSide Side)
  2324. {
  2325. return
  2326. (Side == osOther) &&
  2327. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2328. }
  2329. //---------------------------------------------------------------------------
  2330. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2331. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2332. {
  2333. TTBItemOptions O;
  2334. O = Item->Options;
  2335. TCustomDirView * DView = DirView(Side);
  2336. if ((DView->ItemFocused != NULL) &&
  2337. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2338. // when resolving links is disabled, default action is to enter the directory,
  2339. // no matter what DoubleClickAction is configured to
  2340. (IsSideLocalBrowser(Side) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2341. // Can only Edit files, but can Open/Copy even directories
  2342. ((DoubleClickAction != dcaEdit) ||
  2343. !DView->ItemIsDirectory(DView->ItemFocused)))
  2344. {
  2345. Item->Options = O << tboDefault;
  2346. }
  2347. else
  2348. {
  2349. Item->Options = O >> tboDefault;
  2350. }
  2351. }
  2352. //---------------------------------------------------------------------------
  2353. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2354. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2355. {
  2356. TCustomDirView * DView = DirView(Side);
  2357. TListItem * Item = DView->ItemFocused;
  2358. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2359. EnableFocusedOperation[Side])
  2360. {
  2361. TPoint ClientPoint;
  2362. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2363. TPoint(0, 0) : MousePos;
  2364. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2365. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2366. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2367. }
  2368. }
  2369. //---------------------------------------------------------------------------
  2370. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2371. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2372. {
  2373. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2374. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2375. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2376. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2377. Handled = true;
  2378. }
  2379. //---------------------------------------------------------------------------
  2380. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2381. {
  2382. }
  2383. //---------------------------------------------------------------------------
  2384. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2385. {
  2386. DebugAssert(FErrorList == NULL);
  2387. if (WinConfiguration->ContinueOnError)
  2388. {
  2389. FErrorList = new TStringList();
  2390. Configuration->Usage->Inc(L"ContinuationsOnError");
  2391. }
  2392. NonVisualDataModule->StartBusy();
  2393. }
  2394. //---------------------------------------------------------------------------
  2395. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2396. {
  2397. NonVisualDataModule->EndBusy();
  2398. if (FErrorList)
  2399. {
  2400. HandleErrorList(FErrorList);
  2401. }
  2402. }
  2403. //---------------------------------------------------------------------------
  2404. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2405. const TCopyParamType & CopyParam)
  2406. {
  2407. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2408. {
  2409. Configuration->Usage->Inc(L"FileMaskUses");
  2410. }
  2411. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2412. {
  2413. Configuration->Usage->Inc(L"OperationMaskUses");
  2414. }
  2415. }
  2416. //---------------------------------------------------------------------------
  2417. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2418. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2419. {
  2420. DebugAssert(!IsLocalBrowserMode());
  2421. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2422. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2423. TTransferOperationParam DefaultParam;
  2424. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2425. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2426. switch (Param.Queue)
  2427. {
  2428. case asOn:
  2429. CopyParam.Queue = true;
  2430. break;
  2431. case asOff:
  2432. CopyParam.Queue = false;
  2433. break;
  2434. case asAuto:
  2435. default:
  2436. // keep default
  2437. break;
  2438. }
  2439. bool Result =
  2440. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2441. if (Result)
  2442. {
  2443. DebugAssert(Terminal);
  2444. bool SelectionRestored = false;
  2445. TCustomDirView * DView = NULL;
  2446. if (HasDirView[Side])
  2447. {
  2448. DView = DirView(Side);
  2449. DView->SaveSelection();
  2450. DView->SaveSelectedNames();
  2451. }
  2452. UpdateCopyParamCounters(CopyParam);
  2453. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2454. DebugAssert(FTransferResumeList == NULL);
  2455. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2456. FMoveToQueue = false;
  2457. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2458. try
  2459. {
  2460. TStrings * PermanentFileList;
  2461. std::unique_ptr<TStrings> PermanentFileListOwner;
  2462. try
  2463. {
  2464. if (Side == osLocal)
  2465. {
  2466. PermanentFileList = FileList;
  2467. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2468. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2469. if (Operation == foMove)
  2470. {
  2471. ReloadLocalDirectory();
  2472. if (DView != NULL)
  2473. {
  2474. DView->RestoreSelection();
  2475. }
  2476. SelectionRestored = true;
  2477. }
  2478. }
  2479. else
  2480. {
  2481. // Clone the file list as it may refer to current directory files,
  2482. // which get destroyed, when the source directory is reloaded after foMove operation.
  2483. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2484. // But for a hotfix, we are not going to do such a big change.
  2485. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2486. PermanentFileList = PermanentFileListOwner.get();
  2487. try
  2488. {
  2489. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2490. }
  2491. __finally
  2492. {
  2493. if (Operation == foMove)
  2494. {
  2495. if (DView != NULL)
  2496. {
  2497. DView->RestoreSelection();
  2498. }
  2499. SelectionRestored = true;
  2500. }
  2501. ReloadLocalDirectory(Param.TargetDirectory);
  2502. }
  2503. }
  2504. // When the transfer batch is aborted between individual file transfers, the Abort is not called.
  2505. if (FMoveToQueue)
  2506. {
  2507. Abort();
  2508. }
  2509. }
  2510. catch (EAbort &)
  2511. {
  2512. if (FMoveToQueue)
  2513. {
  2514. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2515. DebugAssert(CopyParam.TransferSkipList == NULL);
  2516. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2517. if (TransferResumeList->Count > 0)
  2518. {
  2519. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2520. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2521. }
  2522. CopyParam.TransferSkipList = TransferResumeList.release();
  2523. // not really needed, just to keep it consistent with TransferResumeList
  2524. FTransferResumeList = NULL;
  2525. FMoveToQueue = false;
  2526. Configuration->Usage->Inc("MovesToBackground");
  2527. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2528. ClearTransferSourceSelection(Direction);
  2529. }
  2530. throw;
  2531. }
  2532. }
  2533. __finally
  2534. {
  2535. if (!SelectionRestored && (DView != NULL))
  2536. {
  2537. DView->DiscardSavedSelection();
  2538. }
  2539. FTransferResumeList = NULL;
  2540. }
  2541. }
  2542. return Result;
  2543. }
  2544. //---------------------------------------------------------------------------
  2545. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2546. TOperationSide Side, TStrings * FileList, void * Param)
  2547. {
  2548. DebugAssert(FileList->Count);
  2549. // We deliberately do not toggle alternative flag (Param), but use OR,
  2550. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2551. // shortcut of CurrentDeleteAlternativeAction
  2552. bool Alternative =
  2553. bool(Param) || UseAlternativeFunction();
  2554. bool Recycle;
  2555. if (IsSideLocalBrowser(Side))
  2556. {
  2557. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2558. }
  2559. else
  2560. {
  2561. Recycle =
  2562. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2563. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2564. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2565. }
  2566. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2567. if (!Result)
  2568. {
  2569. UnicodeString Query;
  2570. if (FileList->Count == 1)
  2571. {
  2572. if (IsSideLocalBrowser(Side))
  2573. {
  2574. Query = ExtractFileName(FileList->Strings[0]);
  2575. }
  2576. else
  2577. {
  2578. Query = UnixExtractFileName(FileList->Strings[0]);
  2579. }
  2580. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2581. }
  2582. else
  2583. {
  2584. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2585. }
  2586. TMessageParams Params(mpNeverAskAgainCheck);
  2587. Params.ImageName = L"Delete file";
  2588. unsigned int Answer =
  2589. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2590. if (Answer == qaNeverAskAgain)
  2591. {
  2592. Result = true;
  2593. if (Recycle)
  2594. {
  2595. WinConfiguration->ConfirmRecycling = false;
  2596. }
  2597. else
  2598. {
  2599. WinConfiguration->ConfirmDeleting = false;
  2600. }
  2601. }
  2602. else
  2603. {
  2604. Result = (Answer == qaOK);
  2605. }
  2606. }
  2607. if (Result)
  2608. {
  2609. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2610. }
  2611. return Result;
  2612. }
  2613. //---------------------------------------------------------------------------
  2614. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2615. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2616. {
  2617. TAutoBatch AutoBatch(this);
  2618. bool Result;
  2619. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2620. {
  2621. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2622. }
  2623. else if (Operation == foRename)
  2624. {
  2625. DebugAssert(DirView(Side)->ItemFocused);
  2626. DirView(Side)->ItemFocused->EditCaption();
  2627. Result = true;
  2628. }
  2629. else if (Operation == foDelete)
  2630. {
  2631. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2632. }
  2633. else if (Operation == foSetProperties)
  2634. {
  2635. DirView(osRemote)->SaveSelectedNames();
  2636. Result = SetProperties(Side, FileList);
  2637. }
  2638. else if (Operation == foCustomCommand)
  2639. {
  2640. DebugAssert(Param);
  2641. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2642. DirView(osRemote)->SaveSelectedNames();
  2643. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2644. CustomCommand(FileList, *Command, NULL);
  2645. Result = true;
  2646. }
  2647. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2648. {
  2649. DebugAssert(!IsSideLocalBrowser(Side));
  2650. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2651. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2652. }
  2653. else if (Operation == foLock)
  2654. {
  2655. DebugAssert(!IsSideLocalBrowser(Side));
  2656. LockFiles(FileList, true);
  2657. Result = true;
  2658. }
  2659. else if (Operation == foUnlock)
  2660. {
  2661. DebugAssert(!IsSideLocalBrowser(Side));
  2662. LockFiles(FileList, false);
  2663. Result = true;
  2664. }
  2665. else
  2666. {
  2667. DebugFail();
  2668. }
  2669. return Result;
  2670. }
  2671. //---------------------------------------------------------------------------
  2672. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2673. {
  2674. if (Side == osCurrent)
  2675. {
  2676. Side = FCurrentSide;
  2677. }
  2678. return Side;
  2679. }
  2680. //---------------------------------------------------------------------------
  2681. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2682. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2683. {
  2684. Side = GetSide(Side);
  2685. bool Result;
  2686. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, IsSideLocalBrowser(Side), NULL);
  2687. try
  2688. {
  2689. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2690. }
  2691. __finally
  2692. {
  2693. delete FileList;
  2694. }
  2695. return Result;
  2696. }
  2697. //---------------------------------------------------------------------------
  2698. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2699. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2700. bool NoConfirmation, void * Param)
  2701. {
  2702. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2703. {
  2704. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2705. {
  2706. DebugAssert(!IsLocalBrowserMode());
  2707. if (GetSide(Side) == osLocal)
  2708. {
  2709. Configuration->Usage->Inc(L"UploadsCommand");
  2710. }
  2711. else
  2712. {
  2713. Configuration->Usage->Inc(L"DownloadsCommand");
  2714. }
  2715. }
  2716. }
  2717. }
  2718. //---------------------------------------------------------------------------
  2719. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2720. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2721. {
  2722. TTransferOperationParam Param;
  2723. if ((WinConfiguration->Interface != ifCommander) ||
  2724. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2725. {
  2726. Flags &= ~cocShortCutHint;
  2727. }
  2728. TCustomDirView * DView = DirView(Side);
  2729. Param.Options =
  2730. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2731. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2732. if (FLAGSET(Flags, cocQueue))
  2733. {
  2734. Param.Queue = asOn;
  2735. }
  2736. else if (FLAGSET(Flags, cocNonQueue))
  2737. {
  2738. Param.Queue = asOff;
  2739. }
  2740. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2741. }
  2742. //---------------------------------------------------------------------------
  2743. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2744. {
  2745. int Count = ErrorList->Count;
  2746. try
  2747. {
  2748. if (ErrorList->Count)
  2749. {
  2750. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2751. if (MessageDialog(Message, qtError,
  2752. qaOK | qaCancel, HELP_NONE) == qaOK)
  2753. {
  2754. unsigned int Answer;
  2755. int Index = 0;
  2756. do
  2757. {
  2758. DebugAssert(Index >= 0 && Index < Count);
  2759. TQueryButtonAlias Aliases[2];
  2760. Aliases[0].Button = qaYes;
  2761. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2762. Aliases[1].Button = qaNo;
  2763. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2764. TMessageParams Params;
  2765. Params.Aliases = Aliases;
  2766. Params.AliasesCount = LENOF(Aliases);
  2767. int No = Index + 1;
  2768. UnicodeString Message = ErrorList->Strings[Index];
  2769. UnicodeString AMainInstructions;
  2770. UnicodeString Details;
  2771. if (ExtractMainInstructions(Message, AMainInstructions))
  2772. {
  2773. Details = Message;
  2774. Message = AMainInstructions;
  2775. }
  2776. Message = MainInstructions(FMTLOAD(ERROR_LIST_NUMBER, (No, Count, Message))) + Details;
  2777. Answer = MoreMessageDialog(
  2778. Message,
  2779. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2780. (Index ? qaYes : 0) | (Index < Count - 1 ? qaNo : 0) |
  2781. qaOK, HELP_NONE, &Params);
  2782. if (Answer == qaNo)
  2783. {
  2784. Index++;
  2785. }
  2786. if (Answer == qaYes)
  2787. {
  2788. Index--;
  2789. }
  2790. }
  2791. while (Answer != qaOK);
  2792. }
  2793. }
  2794. }
  2795. __finally
  2796. {
  2797. TStrings * List = ErrorList;
  2798. ErrorList = NULL;
  2799. for (int i = 0; i < Count; i++)
  2800. {
  2801. delete List->Objects[i];
  2802. }
  2803. delete List;
  2804. }
  2805. }
  2806. //---------------------------------------------------------------------------
  2807. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2808. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2809. {
  2810. // needed for checking filemasks, as there's no directory object
  2811. // associated with the file object
  2812. File->FullFileName = FullFileName;
  2813. TFileMasks::TParams MaskParams;
  2814. MaskParams.Size = File->Size;
  2815. MaskParams.Modification = File->Modification;
  2816. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2817. }
  2818. //---------------------------------------------------------------------------
  2819. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2820. {
  2821. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2822. Side = GetSide(Side);
  2823. TCustomDirView * DView = DirView(Side);
  2824. TListItem * FocusedItem = DView->ItemFocused;
  2825. UnicodeString Name;
  2826. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2827. {
  2828. Name = DView->ItemFileName(FocusedItem);
  2829. }
  2830. else
  2831. {
  2832. Name = LoadStr(NEW_FILE);
  2833. }
  2834. UnicodeString Names = Name;
  2835. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2836. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2837. HELP_EDIT_NEW, History.get(), true, NULL, true, 400))
  2838. {
  2839. while (!Names.IsEmpty())
  2840. {
  2841. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2842. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2843. UnicodeString TargetFileName;
  2844. UnicodeString LocalFileName;
  2845. UnicodeString RootTempDir;
  2846. UnicodeString TempDir;
  2847. UnicodeString RemoteDirectory;
  2848. bool ExistingFile = false;
  2849. if (!IsSideLocalBrowser(Side))
  2850. {
  2851. Name = AbsolutePath(FTerminal->CurrentDirectory, Name);
  2852. TRemoteFile * File = NULL;
  2853. if (FTerminal->FileExists(Name, &File) &&
  2854. !File->IsDirectory)
  2855. {
  2856. try
  2857. {
  2858. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2859. ExistingFile = true;
  2860. }
  2861. __finally
  2862. {
  2863. delete File;
  2864. }
  2865. }
  2866. if (!ExistingFile)
  2867. {
  2868. RemoteDirectory = UnixExtractFilePath(Name);
  2869. TemporaryDirectoryForRemoteFiles(
  2870. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2871. TargetFileName = UnixExtractFileName(Name);
  2872. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2873. LocalFileName = TempDir +
  2874. // We probably do not want to trim the VMS version here
  2875. FTerminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2876. }
  2877. }
  2878. else
  2879. {
  2880. if (ExtractFilePath(Name).IsEmpty())
  2881. {
  2882. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2883. }
  2884. else
  2885. {
  2886. LocalFileName = ExpandFileName(Name);
  2887. }
  2888. TargetFileName = ExtractFileName(Name);
  2889. }
  2890. if (!ExistingFile)
  2891. {
  2892. bool NewFile = !FileExists(ApiPath(LocalFileName));
  2893. if (NewFile)
  2894. {
  2895. int File = FileCreate(ApiPath(LocalFileName));
  2896. if (File < 0)
  2897. {
  2898. if (!RootTempDir.IsEmpty())
  2899. {
  2900. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2901. }
  2902. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2903. }
  2904. else
  2905. {
  2906. FileClose(File);
  2907. }
  2908. }
  2909. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2910. const TEditorData * ExternalEditor = NULL;
  2911. TFileMasks::TParams MaskParams; // size not known
  2912. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2913. false, MaskParams);
  2914. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2915. ExternalEditor, RootTempDir, RemoteDirectory, NewFile);
  2916. }
  2917. }
  2918. }
  2919. }
  2920. //---------------------------------------------------------------------------
  2921. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2922. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2923. {
  2924. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2925. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2926. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2927. return
  2928. ((ExecuteFileBy == efInternalEditor)) ||
  2929. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2930. }
  2931. //---------------------------------------------------------------------------
  2932. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2933. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2934. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2935. UnicodeString RemoteDirectory, bool NewFile)
  2936. {
  2937. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2938. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2939. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2940. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2941. Side = GetSide(Side);
  2942. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2943. if (!IsSideLocalBrowser(Side))
  2944. {
  2945. Data->Terminal = Terminal;
  2946. Data->Queue = Queue;
  2947. Data->SessionData = CloneCurrentSessionData();
  2948. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2949. Data->RemoteDirectory = RemoteDirectory;
  2950. Data->SessionName = Terminal->SessionData->SessionName;
  2951. Data->LocalRootDirectory = LocalRootDirectory;
  2952. Data->OriginalFileName = OriginalFileName;
  2953. Data->Command = L""; // will be changed later for external editor
  2954. }
  2955. if (ExecuteFileBy == efInternalEditor)
  2956. {
  2957. if (!IsSideLocalBrowser(Side))
  2958. {
  2959. UnicodeString Caption = UnixIncludeTrailingBackslash(RemoteDirectory) + OriginalFileName +
  2960. L" - " + Terminal->SessionData->SessionName;
  2961. TForm * Editor;
  2962. try
  2963. {
  2964. TNotifyEvent OnFileChanged = NULL;
  2965. TNotifyEvent OnSaveAll = NULL;
  2966. TAnyModifiedEvent OnAnyModified = NULL;
  2967. // Edited files are uploaded in background queue, what we do not support with encrypted files
  2968. if (Terminal->IsCapable[fcBackgroundTransfers])
  2969. {
  2970. OnFileChanged = FEditorManager->FileChanged;
  2971. OnSaveAll = SaveAllInternalEditors;
  2972. OnAnyModified = AnyInternalEditorModified;
  2973. }
  2974. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  2975. FEditorManager->FileReload, FEditorManager->FileClosed,
  2976. OnSaveAll, OnAnyModified,
  2977. Caption, StandaloneOperation, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  2978. }
  2979. catch(...)
  2980. {
  2981. if (!LocalRootDirectory.IsEmpty())
  2982. {
  2983. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  2984. }
  2985. throw;
  2986. }
  2987. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  2988. }
  2989. else
  2990. {
  2991. DebugAssert(!StandaloneOperation);
  2992. TForm * Editor =
  2993. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  2994. SaveAllInternalEditors, AnyInternalEditorModified,
  2995. L"", false, SessionColor, -1, NewFile);
  2996. FLocalEditors->Add(Editor);
  2997. }
  2998. }
  2999. else
  3000. {
  3001. HANDLE Process;
  3002. if (ExecuteFileBy == efExternalEditor)
  3003. {
  3004. UnicodeString Program, Params, Dir;
  3005. Data->Command = ExternalEditor->ExternalEditor;
  3006. ReformatFileNameCommand(Data->Command);
  3007. SplitCommand(Data->Command, Program, Params, Dir);
  3008. Params = ExpandFileNameCommand(Params, FileName);
  3009. Program = ExpandEnvironmentVariables(Program);
  3010. if (!ExecuteShell(Program, Params, Process))
  3011. {
  3012. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  3013. }
  3014. }
  3015. else
  3016. {
  3017. DebugAssert(!IsSideLocalBrowser(Side));
  3018. if (!ExecuteShell(FileName, L"", Process))
  3019. {
  3020. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  3021. }
  3022. }
  3023. if (IsSideLocalBrowser(Side) ||
  3024. ((ExecuteFileBy == efShell) &&
  3025. !WinConfiguration->Editor.SDIShellEditor) ||
  3026. ((ExecuteFileBy == efExternalEditor) &&
  3027. !ExternalEditor->SDIExternalEditor))
  3028. {
  3029. // no need for handle
  3030. if (Process != NULL)
  3031. {
  3032. DebugCheck(CloseHandle(Process));
  3033. }
  3034. Process = INVALID_HANDLE_VALUE;
  3035. }
  3036. else
  3037. {
  3038. if (Process == NULL)
  3039. {
  3040. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  3041. }
  3042. }
  3043. if (!IsSideLocalBrowser(Side))
  3044. {
  3045. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  3046. }
  3047. }
  3048. }
  3049. //---------------------------------------------------------------------------
  3050. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3051. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3052. void * /*Arg*/)
  3053. {
  3054. if (Token != NULL)
  3055. {
  3056. EditorFormFileSave(static_cast<TForm *>(Token));
  3057. }
  3058. }
  3059. //---------------------------------------------------------------------------
  3060. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3061. {
  3062. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3063. {
  3064. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3065. }
  3066. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3067. }
  3068. //---------------------------------------------------------------------------
  3069. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3070. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3071. void * Arg)
  3072. {
  3073. if ((Token != NULL) &&
  3074. IsEditorFormModified(static_cast<TForm *>(Token)))
  3075. {
  3076. *static_cast<bool *>(Arg) = true;
  3077. }
  3078. }
  3079. //---------------------------------------------------------------------------
  3080. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3081. TObject * /*Sender*/, bool & Modified)
  3082. {
  3083. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3084. {
  3085. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3086. {
  3087. Modified = true;
  3088. }
  3089. }
  3090. if (!Modified)
  3091. {
  3092. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3093. }
  3094. }
  3095. //---------------------------------------------------------------------------
  3096. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3097. {
  3098. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3099. }
  3100. //---------------------------------------------------------------------------
  3101. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3102. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3103. UnicodeString & Result, UnicodeString & RootDirectory)
  3104. {
  3105. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3106. {
  3107. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3108. Result = RootDirectory;
  3109. }
  3110. else
  3111. {
  3112. RootDirectory = L"";
  3113. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3114. Result = IncludeTrailingBackslash(Result);
  3115. }
  3116. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3117. {
  3118. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3119. }
  3120. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3121. {
  3122. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3123. {
  3124. RemoteDirectory.Delete(1, 1);
  3125. }
  3126. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3127. }
  3128. if (!ForceDirectories(ApiPath(Result)))
  3129. {
  3130. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3131. }
  3132. }
  3133. //---------------------------------------------------------------------------
  3134. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3135. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3136. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  3137. {
  3138. DebugAssert(!IsLocalBrowserMode());
  3139. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3140. if (ForceText)
  3141. {
  3142. CopyParam.TransferMode = tmAscii;
  3143. }
  3144. TemporaryFileCopyParam(CopyParam);
  3145. if (AllFiles)
  3146. {
  3147. CopyParam.IncludeFileMask = TFileMasks();
  3148. }
  3149. if (TempDir.IsEmpty())
  3150. {
  3151. TemporaryDirectoryForRemoteFiles(FTerminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3152. }
  3153. DebugAssert(!FAutoOperation);
  3154. FAutoOperation = AutoOperation;
  3155. Terminal->ExceptionOnFail = true;
  3156. try
  3157. {
  3158. try
  3159. {
  3160. // turn off confirmations, as for MDI editors we may possibly download
  3161. // the same file over
  3162. Terminal->CopyToLocal(
  3163. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3164. if (GetTargetNames)
  3165. {
  3166. for (int i = 0; i < FileList->Count; i++)
  3167. {
  3168. FileList->Strings[i] =
  3169. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3170. }
  3171. }
  3172. }
  3173. catch(...)
  3174. {
  3175. if (!RootTempDir.IsEmpty())
  3176. {
  3177. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3178. }
  3179. throw;
  3180. }
  3181. }
  3182. __finally
  3183. {
  3184. FAutoOperation = false;
  3185. Terminal->ExceptionOnFail = false;
  3186. }
  3187. }
  3188. //---------------------------------------------------------------------------
  3189. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3190. {
  3191. // Do not waste time checking for default editor list the next time,
  3192. // or testing if default editor is not notepad.
  3193. bool TryNextTime = false;
  3194. UnicodeString UsageState;
  3195. if (!WinConfiguration->EditorList->IsDefaultList())
  3196. {
  3197. UsageState = "H";
  3198. }
  3199. else
  3200. {
  3201. UnicodeString Executable;
  3202. UnicodeString ExecutableDescription;
  3203. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3204. {
  3205. UnicodeString Message =
  3206. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3207. unsigned int Answer =
  3208. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3209. if (Answer != qaOK)
  3210. {
  3211. UsageState = "R";
  3212. }
  3213. else
  3214. {
  3215. UsageState = "A";
  3216. TEditorData EditorData;
  3217. EditorData.Editor = edExternal;
  3218. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3219. EditorData.ExternalEditorOptionsAutodetect();
  3220. TEditorList EditorList;
  3221. EditorList = *WinConfiguration->EditorList;
  3222. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3223. WinConfiguration->EditorList = &EditorList;
  3224. }
  3225. }
  3226. }
  3227. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3228. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3229. }
  3230. //---------------------------------------------------------------------------
  3231. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3232. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3233. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3234. {
  3235. if (ExecuteFileBy == efDefaultEditor)
  3236. {
  3237. if (!WinConfiguration->OfferedEditorAutoConfig)
  3238. {
  3239. EditorAutoConfig();
  3240. }
  3241. const TEditorPreferences * Editor =
  3242. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3243. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3244. {
  3245. ExecuteFileBy = efInternalEditor;
  3246. ExternalEditor = NULL;
  3247. }
  3248. else if (Editor->Data->Editor == edOpen)
  3249. {
  3250. ExecuteFileBy = efShell;
  3251. ExternalEditor = NULL;
  3252. }
  3253. else
  3254. {
  3255. ExecuteFileBy = efExternalEditor;
  3256. ExternalEditor = Editor->Data;
  3257. }
  3258. }
  3259. }
  3260. //---------------------------------------------------------------------------
  3261. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3262. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3263. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3264. {
  3265. UnicodeString OriginalFileName;
  3266. UnicodeString LocalRootDirectory;
  3267. UnicodeString RemoteDirectory;
  3268. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3269. IsSideLocalBrowser(Side), MaskParams);
  3270. UnicodeString Counter;
  3271. UnicodeString LocalFileName;
  3272. bool Handled = false;
  3273. if (!IsSideLocalBrowser(Side))
  3274. {
  3275. // We need to trim VMS version here, so that we use name without version
  3276. // when uploading back to create a new version of the file
  3277. OriginalFileName = FTerminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3278. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3279. TObject * Token = NULL;
  3280. UnicodeString LocalDirectory;
  3281. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3282. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3283. LocalDirectory))
  3284. {
  3285. if (Token != NULL)
  3286. {
  3287. TForm * Form = dynamic_cast<TForm *>(Token);
  3288. if (Form->WindowState == wsMinimized)
  3289. {
  3290. ShowWindow(Form->Handle, SW_RESTORE);
  3291. }
  3292. else
  3293. {
  3294. Form->SetFocus();
  3295. }
  3296. Handled = true;
  3297. }
  3298. else
  3299. {
  3300. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3301. }
  3302. }
  3303. if (!Handled)
  3304. {
  3305. TStringList * FileList1 = new TStringList();
  3306. try
  3307. {
  3308. FileList1->AddObject(FullFileName, Object);
  3309. TemporarilyDownloadFiles(FileList1,
  3310. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3311. LocalRootDirectory, LocalDirectory, true, true, true);
  3312. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3313. }
  3314. __finally
  3315. {
  3316. delete FileList1;
  3317. }
  3318. switch (ExecuteFileBy)
  3319. {
  3320. case efShell:
  3321. Counter = "RemoteFilesExecuted";
  3322. break;
  3323. case efInternalEditor:
  3324. Counter = "RemoteFilesOpenedInInternalEditor";
  3325. break;
  3326. case efExternalEditor:
  3327. Counter = "RemoteFilesOpenedInExternalEditor";
  3328. break;
  3329. default:
  3330. DebugFail();
  3331. }
  3332. }
  3333. }
  3334. else
  3335. {
  3336. LocalFileName = FullFileName;
  3337. OriginalFileName = ExtractFileName(FullFileName);
  3338. switch (ExecuteFileBy)
  3339. {
  3340. case efShell:
  3341. Counter = "LocalFilesExecuted";
  3342. break;
  3343. case efInternalEditor:
  3344. Counter = "LocalFilesOpenedInInternalEditor";
  3345. break;
  3346. case efExternalEditor:
  3347. Counter = "LocalFilesOpenedInExternalEditor";
  3348. break;
  3349. default:
  3350. DebugFail();
  3351. }
  3352. }
  3353. if (!Handled)
  3354. {
  3355. Configuration->Usage->Inc(Counter);
  3356. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3357. ExternalEditor, LocalRootDirectory, RemoteDirectory, false);
  3358. }
  3359. }
  3360. //---------------------------------------------------------------------------
  3361. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3362. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3363. bool AllSelected, bool OnFocused)
  3364. {
  3365. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3366. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3367. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3368. Side = GetSide(Side);
  3369. TCustomDirView * DView = DirView(Side);
  3370. TStrings * FileList =
  3371. AllSelected ?
  3372. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3373. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3374. try
  3375. {
  3376. DebugAssert(AllSelected || (FileList->Count == 1));
  3377. for (int i = 0; i < FileList->Count; i++)
  3378. {
  3379. UnicodeString ListFileName = FileList->Strings[i];
  3380. UnicodeString FileNameOnly =
  3381. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3382. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3383. if (!DView->ItemIsDirectory(Item))
  3384. {
  3385. UnicodeString FullFileName;
  3386. if (!IsSideLocalBrowser(Side))
  3387. {
  3388. FullFileName = DirView(Side)->Path + ListFileName;
  3389. }
  3390. else
  3391. {
  3392. FullFileName = ListFileName;
  3393. }
  3394. TFileMasks::TParams MaskParams;
  3395. MaskParams.Size = DView->ItemFileSize(Item);
  3396. TDateTimePrecision Precision;
  3397. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3398. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3399. FileList->Objects[i], MaskParams);
  3400. }
  3401. }
  3402. }
  3403. __finally
  3404. {
  3405. delete FileList;
  3406. }
  3407. }
  3408. //---------------------------------------------------------------------------
  3409. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3410. {
  3411. CopyParam.FileNameCase = ncNoChange;
  3412. CopyParam.PreserveRights = false;
  3413. CopyParam.PreserveReadOnly = false;
  3414. CopyParam.ReplaceInvalidChars = true;
  3415. CopyParam.IncludeFileMask = TFileMasks();
  3416. CopyParam.NewerOnly = false;
  3417. CopyParam.FileMask = L"";
  3418. }
  3419. //---------------------------------------------------------------------------
  3420. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3421. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3422. {
  3423. TTerminalManager * Manager = TTerminalManager::Instance();
  3424. if (!IsActiveTerminal(Data->Terminal))
  3425. {
  3426. if (!NonVisualDataModule->Busy)
  3427. {
  3428. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3429. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3430. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3431. if (Data->Terminal == NULL)
  3432. {
  3433. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3434. if (SameSiteTerminal != NULL)
  3435. {
  3436. UnicodeString Message =
  3437. FMTLOAD(EDIT_SESSION_REATTACH,
  3438. (FileNameOnly, Data->SessionName, FileNameOnly));
  3439. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3440. {
  3441. Data->Terminal = Terminal;
  3442. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3443. Data->SessionName = Terminal->SessionData->SessionName;
  3444. // We might also overwrite session data
  3445. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3446. }
  3447. else
  3448. {
  3449. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3450. Abort();
  3451. }
  3452. }
  3453. }
  3454. // foreground session should reconnect itself
  3455. else if (Terminal != Data->Terminal)
  3456. {
  3457. UnicodeString Message =
  3458. MainInstructions(
  3459. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3460. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3461. {
  3462. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3463. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3464. }
  3465. else
  3466. {
  3467. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3468. Abort();
  3469. }
  3470. }
  3471. }
  3472. if (!IsActiveTerminal(Data->Terminal))
  3473. {
  3474. Configuration->Usage->Inc(L"EditInactiveSession");
  3475. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3476. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3477. (ExtractFileName(FileName), Data->SessionName)));
  3478. }
  3479. }
  3480. TStrings * FileList = new TStringList();
  3481. try
  3482. {
  3483. FileList->Add(FileName);
  3484. // consider using the same settings (preset) as when the file was downloaded
  3485. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3486. TemporaryFileCopyParam(CopyParam);
  3487. if (Data->ForceText)
  3488. {
  3489. CopyParam.TransferMode = tmAscii;
  3490. }
  3491. // so i do not need to worry if masking algorithm works in all cases
  3492. // ("" means "copy file name", no masking is actually done)
  3493. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3494. {
  3495. CopyParam.FileMask = L"";
  3496. }
  3497. else
  3498. {
  3499. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3500. }
  3501. DebugAssert(Data->Queue != NULL);
  3502. int Params = cpNoConfirmation | cpTemporary;
  3503. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3504. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3505. QueueItem->CompleteEvent = UploadCompleteEvent;
  3506. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3507. }
  3508. __finally
  3509. {
  3510. delete FileList;
  3511. }
  3512. }
  3513. //---------------------------------------------------------------------------
  3514. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3515. const UnicodeString FileName, const TEditedFileData * Data)
  3516. {
  3517. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3518. DebugAssert(!NonVisualDataModule->Busy);
  3519. if (!IsActiveTerminal(Data->Terminal))
  3520. {
  3521. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3522. (ExtractFileName(FileName), Data->SessionName)));
  3523. }
  3524. TManagedTerminal * PrevTerminal = TTerminalManager::Instance()->ActiveTerminal;
  3525. TTerminalManager::Instance()->ActiveTerminal = Data->Terminal;
  3526. NonVisualDataModule->StartBusy();
  3527. try
  3528. {
  3529. std::unique_ptr<TRemoteFile> File;
  3530. UnicodeString RemoteFileName =
  3531. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3532. FTerminal->ExceptionOnFail = true;
  3533. try
  3534. {
  3535. TRemoteFile * AFile = NULL;
  3536. FTerminal->ReadFile(RemoteFileName, AFile);
  3537. File.reset(AFile);
  3538. if (!File->HaveFullFileName)
  3539. {
  3540. File->FullFileName = RemoteFileName;
  3541. }
  3542. }
  3543. __finally
  3544. {
  3545. FTerminal->ExceptionOnFail = false;
  3546. }
  3547. std::unique_ptr<TStrings> FileList(new TStringList());
  3548. FileList->AddObject(RemoteFileName, File.get());
  3549. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3550. UnicodeString TempDir = ExtractFilePath(FileName);
  3551. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3552. TempDir, true, true, true);
  3553. // sanity check, the target file name should be still the same
  3554. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3555. }
  3556. __finally
  3557. {
  3558. NonVisualDataModule->EndBusy();
  3559. // it actually may not exist anymore...
  3560. TTerminalManager::Instance()->ActiveTerminal = PrevTerminal;
  3561. }
  3562. }
  3563. //---------------------------------------------------------------------------
  3564. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3565. const TEditedFileData * Data, bool & KeepOpen)
  3566. {
  3567. // Command is set for external editors only (not for "shell" open).
  3568. if (!Data->Command.IsEmpty())
  3569. {
  3570. bool AnyFound = false;
  3571. bool AnyMDI = false;
  3572. bool AnyNonMDI = false;
  3573. bool AnyDetect = false;
  3574. TEditorList * EditorList = new TEditorList();
  3575. try
  3576. {
  3577. *EditorList = *WinConfiguration->EditorList;
  3578. for (int i = 0; i < EditorList->Count; i++)
  3579. {
  3580. const TEditorPreferences * Editor = EditorList->Editors[i];
  3581. if ((Editor->Data->Editor == edExternal) &&
  3582. (Editor->Data->ExternalEditor == Data->Command))
  3583. {
  3584. AnyFound = true;
  3585. if (Editor->Data->SDIExternalEditor)
  3586. {
  3587. AnyNonMDI = true;
  3588. if (Editor->Data->DetectMDIExternalEditor)
  3589. {
  3590. AnyDetect = true;
  3591. }
  3592. }
  3593. else
  3594. {
  3595. AnyMDI = true;
  3596. }
  3597. }
  3598. }
  3599. bool EnableMDI = false;
  3600. bool DisableDetect = false;
  3601. if (AnyMDI)
  3602. {
  3603. KeepOpen = true;
  3604. if (AnyNonMDI)
  3605. {
  3606. // there is at least one instance of the editor with MDI support enabled,
  3607. // and one with disabled, enable it for all instances
  3608. EnableMDI = true;
  3609. }
  3610. }
  3611. else if (AnyFound && !AnyDetect)
  3612. {
  3613. // at least once instance found but all have MDI autodetection disabled
  3614. // => close the file (default action)
  3615. }
  3616. else
  3617. {
  3618. // no instance of the editor has MDI support enabled
  3619. TMessageParams Params;
  3620. if (AnyFound)
  3621. {
  3622. // there is at least one instance of the editor
  3623. Params.Params |= mpNeverAskAgainCheck;
  3624. }
  3625. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3626. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3627. switch (Answer)
  3628. {
  3629. case qaNeverAskAgain:
  3630. DisableDetect = true;
  3631. break;
  3632. case qaNo:
  3633. EnableMDI = true;
  3634. KeepOpen = true;
  3635. break;
  3636. }
  3637. }
  3638. if (AnyFound && (EnableMDI || DisableDetect))
  3639. {
  3640. bool Changed = false;
  3641. for (int i = 0; i < EditorList->Count; i++)
  3642. {
  3643. const TEditorPreferences * Editor = EditorList->Editors[i];
  3644. if ((Editor->Data->Editor == edExternal) &&
  3645. (Editor->Data->ExternalEditor == Data->Command) &&
  3646. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3647. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3648. {
  3649. Changed = true;
  3650. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3651. if (EnableMDI)
  3652. {
  3653. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3654. }
  3655. if (DisableDetect)
  3656. {
  3657. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3658. }
  3659. EditorList->Change(i, UpdatedEditor);
  3660. }
  3661. }
  3662. if (Changed)
  3663. {
  3664. WinConfiguration->EditorList = EditorList;
  3665. }
  3666. }
  3667. }
  3668. __finally
  3669. {
  3670. delete EditorList;
  3671. }
  3672. }
  3673. else
  3674. {
  3675. // "open" case
  3676. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3677. qaOK, HELP_APP_EARLY_CLOSED);
  3678. }
  3679. }
  3680. //---------------------------------------------------------------------------
  3681. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3682. {
  3683. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3684. }
  3685. //---------------------------------------------------------------------------
  3686. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3687. {
  3688. SideEnter(osRemote);
  3689. }
  3690. //---------------------------------------------------------------------------
  3691. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3692. {
  3693. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3694. SideEnter(osRemote);
  3695. }
  3696. //---------------------------------------------------------------------------
  3697. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3698. {
  3699. FCurrentSide = Side;
  3700. if (Visible)
  3701. {
  3702. UpdateControls();
  3703. }
  3704. }
  3705. //---------------------------------------------------------------------------
  3706. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3707. TStrings * FileList, bool Alternative)
  3708. {
  3709. TCustomDirView * DView = DirView(Side);
  3710. DView->SaveSelection();
  3711. DView->SaveSelectedNames();
  3712. DebugAssert(!FAlternativeDelete);
  3713. FAlternativeDelete = Alternative;
  3714. try
  3715. {
  3716. if (!IsSideLocalBrowser(Side))
  3717. {
  3718. DebugAssert(Terminal != NULL);
  3719. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3720. }
  3721. else
  3722. {
  3723. try
  3724. {
  3725. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3726. }
  3727. __finally
  3728. {
  3729. ReloadLocalDirectory();
  3730. }
  3731. }
  3732. FAlternativeDelete = false;
  3733. }
  3734. catch(...)
  3735. {
  3736. FAlternativeDelete = false;
  3737. DView->DiscardSavedSelection();
  3738. throw;
  3739. }
  3740. DView->RestoreSelection();
  3741. }
  3742. //---------------------------------------------------------------------------
  3743. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3744. {
  3745. DebugAssert(Terminal);
  3746. DebugAssert(!IsLocalBrowserMode());
  3747. RemoteDirView->SaveSelection();
  3748. RemoteDirView->SaveSelectedNames();
  3749. try
  3750. {
  3751. if (Lock)
  3752. {
  3753. Terminal->LockFiles(FileList);
  3754. }
  3755. else
  3756. {
  3757. Terminal->UnlockFiles(FileList);
  3758. }
  3759. }
  3760. catch(...)
  3761. {
  3762. RemoteDirView->DiscardSavedSelection();
  3763. throw;
  3764. }
  3765. RemoteDirView->RestoreSelection();
  3766. }
  3767. //---------------------------------------------------------------------------
  3768. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  3769. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3770. bool NoConfirmation, bool Move)
  3771. {
  3772. DebugAssert(Terminal != NULL);
  3773. DebugAssert(!IsLocalBrowserMode());
  3774. // update Terminal->StateData->RemoteDirectory
  3775. UpdateTerminal(Terminal);
  3776. if (Session == NULL)
  3777. {
  3778. Session = TTerminalManager::Instance()->ActiveTerminal;
  3779. }
  3780. if (Session == TTerminalManager::Instance()->ActiveTerminal)
  3781. {
  3782. if (RemoteDriveView->DropTarget != NULL)
  3783. {
  3784. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3785. }
  3786. else if (RemoteDirView->DropTarget != NULL)
  3787. {
  3788. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3789. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3790. }
  3791. else
  3792. {
  3793. Target = RemoteDirView->Path;
  3794. }
  3795. }
  3796. else
  3797. {
  3798. Target = Session->StateData->RemoteDirectory;
  3799. }
  3800. Target = UnixIncludeTrailingBackslash(Target);
  3801. if (FileList->Count == 1)
  3802. {
  3803. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3804. }
  3805. else
  3806. {
  3807. FileMask = AnyMask;
  3808. }
  3809. DirectCopy = FTerminal->IsCapable[fcRemoteCopy] || FTerminal->IsCapable[fcSecondaryShell];
  3810. bool Result = true;
  3811. if (!NoConfirmation)
  3812. {
  3813. bool Multi = (FileList->Count > 1);
  3814. if (Move)
  3815. {
  3816. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3817. }
  3818. else
  3819. {
  3820. std::unique_ptr<TStrings> Sessions(new TStringList);
  3821. std::unique_ptr<TStrings> Directories(new TStringList);
  3822. TStrings * SessionList = TTerminalManager::Instance()->TerminalList;
  3823. for (int Index = 0; Index < SessionList->Count; Index++)
  3824. {
  3825. TManagedTerminal * ASession = DebugNotNull(dynamic_cast<TManagedTerminal *>(SessionList->Objects[Index]));
  3826. if (IsActiveTerminal(ASession))
  3827. {
  3828. Sessions->AddObject(SessionList->Strings[Index], ASession);
  3829. Directories->Add(ASession->StateData->RemoteDirectory);
  3830. }
  3831. }
  3832. TDirectRemoteCopy AllowDirectCopy;
  3833. if (FTerminal->IsCapable[fcRemoteCopy] || FTerminal->CommandSessionOpened)
  3834. {
  3835. DebugAssert(DirectCopy);
  3836. AllowDirectCopy = drcAllow;
  3837. }
  3838. else if (FTerminal->IsCapable[fcSecondaryShell])
  3839. {
  3840. DebugAssert(DirectCopy);
  3841. AllowDirectCopy = drcConfirmCommandSession;
  3842. }
  3843. else
  3844. {
  3845. DebugAssert(!DirectCopy);
  3846. AllowDirectCopy = drcDisallow;
  3847. }
  3848. void * ASession = Session;
  3849. Result = DoRemoteCopyDialog(
  3850. Sessions.get(), Directories.get(), AllowDirectCopy, Multi, ASession, Target, FileMask, DirectCopy, TTerminalManager::Instance()->ActiveTerminal);
  3851. Session = static_cast<TManagedTerminal *>(ASession);
  3852. }
  3853. }
  3854. return Result;
  3855. }
  3856. //---------------------------------------------------------------------------
  3857. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3858. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  3859. {
  3860. DebugAssert(!IsLocalBrowserMode());
  3861. bool DirectCopy;
  3862. UnicodeString Target, FileMask;
  3863. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3864. if (Result)
  3865. {
  3866. if (!Move && !DirectCopy)
  3867. {
  3868. Configuration->Usage->Inc("RemoteCopyTemp");
  3869. UnicodeString RootTempDir;
  3870. UnicodeString TempDir;
  3871. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3872. TStrings * TemporaryFilesList = new TStringList();
  3873. try
  3874. {
  3875. TMakeLocalFileListParams MakeFileListParam;
  3876. MakeFileListParam.FileList = TemporaryFilesList;
  3877. MakeFileListParam.FileTimes = NULL;
  3878. MakeFileListParam.IncludeDirs = true;
  3879. MakeFileListParam.Recursive = false;
  3880. ProcessLocalDirectory(TempDir, FTerminal->MakeLocalFileList, &MakeFileListParam);
  3881. TTerminalManager::Instance()->ActiveTerminal = Session;
  3882. if (TemporaryFilesList->Count > 0)
  3883. {
  3884. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3885. CopyParam.FileMask = FileMask;
  3886. DebugAssert(!FAutoOperation);
  3887. FAutoOperation = true;
  3888. FTerminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3889. }
  3890. }
  3891. __finally
  3892. {
  3893. delete TemporaryFilesList;
  3894. FAutoOperation = false;
  3895. if (!RootTempDir.IsEmpty())
  3896. {
  3897. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3898. }
  3899. }
  3900. }
  3901. else
  3902. {
  3903. RemoteDirView->SaveSelection();
  3904. RemoteDirView->SaveSelectedNames();
  3905. try
  3906. {
  3907. if (Move)
  3908. {
  3909. Configuration->Usage->Inc("RemoteMove");
  3910. Terminal->MoveFiles(FileList, Target, FileMask);
  3911. }
  3912. else
  3913. {
  3914. Configuration->Usage->Inc("RemoteCopyDirect");
  3915. DebugAssert(DirectCopy);
  3916. DebugAssert(Session == FTerminal);
  3917. if (FTerminal->IsCapable[fcRemoteCopy] ||
  3918. FTerminal->CommandSessionOpened ||
  3919. CommandSessionFallback())
  3920. {
  3921. Terminal->CopyFiles(FileList, Target, FileMask);
  3922. }
  3923. }
  3924. }
  3925. catch(...)
  3926. {
  3927. RemoteDirView->DiscardSavedSelection();
  3928. throw;
  3929. }
  3930. RemoteDirView->RestoreSelection();
  3931. }
  3932. }
  3933. return Result;
  3934. }
  3935. //---------------------------------------------------------------------------
  3936. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3937. const UnicodeString & Path, TRemoteProperties & Properties)
  3938. {
  3939. DebugAssert(!IsLocalBrowserMode());
  3940. Properties.Valid = Properties.Valid << vpEncrypt;
  3941. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  3942. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  3943. }
  3944. //---------------------------------------------------------------------------
  3945. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3946. {
  3947. Side = GetSide(Side);
  3948. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3949. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  3950. UnicodeString Name = LoadStr(NEW_FOLDER);
  3951. int AllowedChanges =
  3952. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  3953. bool SaveSettings = false;
  3954. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  3955. {
  3956. TWindowLock Lock(this);
  3957. if (!IsSideLocalBrowser(Side))
  3958. {
  3959. if (SaveSettings)
  3960. {
  3961. GUIConfiguration->NewDirectoryProperties = Properties;
  3962. }
  3963. CreateRemoteDirectory(Name, Properties);
  3964. }
  3965. else
  3966. {
  3967. DirView(Side)->CreateDirectory(Name);
  3968. }
  3969. }
  3970. }
  3971. //---------------------------------------------------------------------------
  3972. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  3973. {
  3974. TWindowLock Lock(this);
  3975. DirView(Side)->ExecuteHomeDirectory();
  3976. }
  3977. //---------------------------------------------------------------------------
  3978. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  3979. {
  3980. DoOpenDirectoryDialog(odBrowse, Side);
  3981. }
  3982. //---------------------------------------------------------------------------
  3983. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  3984. {
  3985. UnicodeString Path = Bookmark->GetSideDirectory(Side);
  3986. bool Result = !Path.IsEmpty();
  3987. if (Result)
  3988. {
  3989. // While we might get here when the session is closed (from location profiles),
  3990. // it's not a problem as the Path setter is noop then
  3991. DirView(Side)->Path = Path;
  3992. }
  3993. return Result;
  3994. }
  3995. //---------------------------------------------------------------------------
  3996. void __fastcall TCustomScpExplorerForm::CalculateSize(
  3997. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  3998. bool & Close)
  3999. {
  4000. // terminal can be already closed (e.g. dropped connection)
  4001. if (Terminal != NULL)
  4002. {
  4003. try
  4004. {
  4005. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  4006. }
  4007. catch(...)
  4008. {
  4009. if (!Terminal->Active)
  4010. {
  4011. Close = true;
  4012. }
  4013. throw;
  4014. }
  4015. }
  4016. }
  4017. //---------------------------------------------------------------------------
  4018. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  4019. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  4020. bool & Close)
  4021. {
  4022. // terminal can be already closed (e.g. dropped connection)
  4023. if (Terminal != NULL)
  4024. {
  4025. Configuration->Usage->Inc(L"ChecksumCalculated");
  4026. try
  4027. {
  4028. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  4029. }
  4030. catch(...)
  4031. {
  4032. if (!Terminal->Active)
  4033. {
  4034. Close = true;
  4035. }
  4036. throw;
  4037. }
  4038. }
  4039. }
  4040. //---------------------------------------------------------------------------
  4041. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  4042. {
  4043. bool Result;
  4044. if (!IsSideLocalBrowser(Side))
  4045. {
  4046. TRemoteTokenList * GroupList = NULL;
  4047. TRemoteTokenList * UserList = NULL;
  4048. try
  4049. {
  4050. TRemoteProperties CurrentProperties;
  4051. if (Terminal->LoadFilesProperties(FileList))
  4052. {
  4053. RemoteDirView->Invalidate();
  4054. }
  4055. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4056. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4057. if (CapableGroupChanging || CapableOwnerChanging)
  4058. {
  4059. if (CapableGroupChanging)
  4060. {
  4061. GroupList = Terminal->Groups->Duplicate();
  4062. }
  4063. if (CapableOwnerChanging)
  4064. {
  4065. UserList = Terminal->Users->Duplicate();
  4066. }
  4067. TRemoteDirectory * Files = Terminal->Files;
  4068. int Count = Files->Count;
  4069. if (Count > 100)
  4070. {
  4071. Count = 100;
  4072. }
  4073. for (int Index = 0; Index < Count; Index++)
  4074. {
  4075. TRemoteFile * File = Files->Files[Index];
  4076. if (CapableGroupChanging)
  4077. {
  4078. GroupList->AddUnique(File->Group);
  4079. }
  4080. if (CapableOwnerChanging)
  4081. {
  4082. UserList->AddUnique(File->Owner);
  4083. }
  4084. }
  4085. // if we haven't collected tokens for all files in current directory,
  4086. // make sure we collect them at least for all selected files.
  4087. // (note that so far the files in FileList has to be from current direcotry)
  4088. if (Count < Files->Count)
  4089. {
  4090. for (int Index = 0; Index < FileList->Count; Index++)
  4091. {
  4092. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4093. if (CapableGroupChanging)
  4094. {
  4095. GroupList->AddUnique(File->Group);
  4096. }
  4097. if (CapableOwnerChanging)
  4098. {
  4099. UserList->AddUnique(File->Owner);
  4100. }
  4101. }
  4102. }
  4103. }
  4104. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4105. int Flags = 0;
  4106. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4107. if (CapableOwnerChanging) Flags |= cpOwner;
  4108. if (CapableGroupChanging) Flags |= cpGroup;
  4109. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4110. if (Terminal->IsCapable[fcCalculatingChecksum])
  4111. {
  4112. CalculateChecksumEvent = CalculateChecksum;
  4113. }
  4114. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4115. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4116. TRemoteProperties NewProperties = CurrentProperties;
  4117. Result =
  4118. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4119. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4120. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4121. CalculateSize, CalculateChecksumEvent);
  4122. if (Result)
  4123. {
  4124. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4125. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4126. }
  4127. }
  4128. __finally
  4129. {
  4130. delete GroupList;
  4131. delete UserList;
  4132. }
  4133. }
  4134. else
  4135. {
  4136. DirView(Side)->DisplayPropertiesMenu();
  4137. Result = true;
  4138. }
  4139. return Result;
  4140. }
  4141. //---------------------------------------------------------------------------
  4142. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4143. {
  4144. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4145. {
  4146. FIgnoreNextDialogChar = Key;
  4147. }
  4148. Key = 0;
  4149. }
  4150. //---------------------------------------------------------------------------
  4151. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4152. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4153. {
  4154. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4155. if (Command != NULL)
  4156. {
  4157. KeyProcessed(Key, Shift);
  4158. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4159. {
  4160. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4161. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4162. false, false, const_cast<TCustomCommandType *>(Command));
  4163. }
  4164. }
  4165. }
  4166. //---------------------------------------------------------------------------
  4167. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4168. {
  4169. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4170. {
  4171. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4172. }
  4173. if ((Key != 0) && !DirView(osCurrent)->IsEditing())
  4174. {
  4175. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4176. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4177. {
  4178. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4179. if (((Action->ShortCut == KeyShortCut) ||
  4180. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4181. AllowedAction(Action, aaShortCut))
  4182. {
  4183. // Has to be called before the action as the dialog char is already in queue.
  4184. // So when the action consumes message queue, we already need to have the
  4185. // FIgnoreNextDialogChar set
  4186. KeyProcessed(Key, Shift);
  4187. // Reset reference to previous component (when menu/toolbar was clicked).
  4188. // Needed to detect that action was invoked by keyboard shortcut
  4189. // in TNonVisualDataModule::ExplorerActionsExecute
  4190. Action->ActionComponent = NULL;
  4191. Action->Execute();
  4192. return;
  4193. }
  4194. }
  4195. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4196. {
  4197. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4198. {
  4199. KeyProcessed(Key, Shift);
  4200. TTerminalManager::Instance()->ActiveTerminalIndex = i;
  4201. return;
  4202. }
  4203. }
  4204. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4205. {
  4206. KeyProcessed(Key, Shift);
  4207. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4208. }
  4209. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4210. if (IsCustomShortCut(CustomShortCut))
  4211. {
  4212. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4213. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4214. if (WinConfiguration->SharedBookmarks != NULL)
  4215. {
  4216. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4217. if ((Bookmark != NULL) &&
  4218. OpenBookmark(FCurrentSide, Bookmark))
  4219. {
  4220. KeyProcessed(Key, Shift);
  4221. }
  4222. }
  4223. }
  4224. }
  4225. TForm::KeyDown(Key, Shift);
  4226. }
  4227. //---------------------------------------------------------------------------
  4228. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4229. {
  4230. DebugAssert(Terminal != NULL);
  4231. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4232. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  4233. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4234. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4235. bool SecurityEnabled = !SessionInfo.SecurityProtocolName.IsEmpty();
  4236. SessionStatusBar->Panels->Items[Offset + 0]->Enabled = SecurityEnabled;
  4237. // expanded from ?: to avoid memory leaks
  4238. if (SecurityEnabled)
  4239. {
  4240. SessionStatusBar->Panels->Items[Offset + 0]->Hint =
  4241. FMTLOAD(STATUS_SECURE, (SessionInfo.SecurityProtocolName));
  4242. }
  4243. else
  4244. {
  4245. SessionStatusBar->Panels->Items[Offset + 0]->Hint = LoadStr(STATUS_INSECURE);
  4246. }
  4247. if (FileSystemInfo.ProtocolName.IsEmpty())
  4248. {
  4249. SessionStatusBar->Panels->Items[Offset + 1]->Caption = SessionInfo.ProtocolName;
  4250. }
  4251. else
  4252. {
  4253. SessionStatusBar->Panels->Items[Offset + 1]->Caption = FileSystemInfo.ProtocolName;
  4254. }
  4255. SessionStatusBar->Panels->Items[Offset + 1]->Hint = LoadStr(STATUS_PROTOCOL_HINT);
  4256. SessionStatusBar->Panels->Items[Offset + 2]->Enabled =
  4257. (!SessionInfo.CSCompression.IsEmpty() || !SessionInfo.SCCompression.IsEmpty());
  4258. UnicodeString CSCompressionStr = DefaultStr(SessionInfo.CSCompression, LoadStr(NO_STR));
  4259. UnicodeString SCCompressionStr = DefaultStr(SessionInfo.SCCompression, LoadStr(NO_STR));
  4260. UnicodeString CompressionStr = CSCompressionStr;
  4261. if (CSCompressionStr != SCCompressionStr)
  4262. {
  4263. CompressionStr += UnicodeString(L"/") + SCCompressionStr;
  4264. }
  4265. SessionStatusBar->Panels->Items[Offset + 2]->Hint = FMTLOAD(STATUS_COMPRESSION_HINT, (CompressionStr));
  4266. SessionStatusBar->Panels->Items[Offset + 3]->Hint = LoadStr(STATUS_DURATION_HINT);
  4267. UpdateStatusBar();
  4268. }
  4269. //---------------------------------------------------------------------------
  4270. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4271. {
  4272. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4273. DebugAssert(SessionStatusBar != NULL);
  4274. if (!HasActiveTerminal() || (Terminal->Status < ssOpened))
  4275. {
  4276. // note: (Terminal->Status < sshReady) currently never happens here,
  4277. // so STATUS_CONNECTING is never used
  4278. SessionStatusBar->SimplePanel = true;
  4279. SessionStatusBar->SimpleText = LoadStr(
  4280. !HasActiveTerminal() ? STATUS_NOT_CONNECTED : STATUS_CONNECTING);
  4281. }
  4282. else
  4283. {
  4284. DebugAssert(Terminal);
  4285. SessionStatusBar->SimplePanel = false;
  4286. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4287. if (!FNote.IsEmpty())
  4288. {
  4289. SessionStatusBar->Panels->Items[0]->Caption = FNote;
  4290. }
  4291. else
  4292. {
  4293. UpdateStatusPanelText(SessionStatusBar->Panels->Items[0]);
  4294. }
  4295. SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1]->Caption =
  4296. FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4297. }
  4298. SessionStatusBar->Panels->Items[0]->Hint = FNoteHints;
  4299. }
  4300. //---------------------------------------------------------------------------
  4301. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4302. {
  4303. Panel->Caption = L"";
  4304. }
  4305. //---------------------------------------------------------------------------
  4306. void __fastcall TCustomScpExplorerForm::Idle()
  4307. {
  4308. if (FShowing ||
  4309. // Particularly for
  4310. // - Standalone editing
  4311. // - Detecting closed connection and automatically reconnect it while waiting for changes
  4312. // while "keeping remote directory up to date".
  4313. StandaloneOperation)
  4314. {
  4315. FEditorManager->Check();
  4316. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4317. // that needs to know if queue view is visible (and it may be closed after queue update)
  4318. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4319. }
  4320. if (FShowing)
  4321. {
  4322. if (!NonVisualDataModule->Busy &&
  4323. // Menu is opened or drag&drop is going on
  4324. (Mouse->Capture == NULL))
  4325. {
  4326. if (FRefreshRemoteDirectory)
  4327. {
  4328. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4329. {
  4330. Terminal->RefreshDirectory();
  4331. }
  4332. FRefreshRemoteDirectory = false;
  4333. }
  4334. if (FRefreshLocalDirectory)
  4335. {
  4336. ReloadLocalDirectory();
  4337. FRefreshLocalDirectory = false;
  4338. }
  4339. if (WinConfiguration->RefreshRemotePanel)
  4340. {
  4341. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4342. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4343. {
  4344. RemoteDirView->ReloadDirectory();
  4345. }
  4346. }
  4347. }
  4348. }
  4349. if (FShowing || StandaloneOperation)
  4350. {
  4351. if (FQueueStatusInvalidated)
  4352. {
  4353. UpdateQueueStatus(false);
  4354. }
  4355. RefreshQueueItems();
  4356. }
  4357. if (FShowing)
  4358. {
  4359. UpdateStatusBar();
  4360. }
  4361. if (FClipboardFakeMonitorsPendingReset && !FDownloadingFromClipboard)
  4362. {
  4363. FClipboardFakeMonitorsPendingReset = false;
  4364. FClipboardFakeMonitors.reset(NULL);
  4365. }
  4366. FIgnoreNextDialogChar = 0;
  4367. }
  4368. //---------------------------------------------------------------------------
  4369. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4370. {
  4371. try
  4372. {
  4373. FUserActionTimer->Enabled = false;
  4374. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4375. {
  4376. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4377. {
  4378. FPendingQueueActionItem->ProcessUserAction();
  4379. }
  4380. }
  4381. }
  4382. __finally
  4383. {
  4384. FPendingQueueActionItem = NULL;
  4385. }
  4386. }
  4387. //---------------------------------------------------------------------------
  4388. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4389. {
  4390. if (WinConfiguration->MinimizeToTray)
  4391. {
  4392. UpdateTrayIcon();
  4393. FTrayIcon->Visible = true;
  4394. if (Visible)
  4395. {
  4396. ShowWindow(Handle, SW_HIDE);
  4397. }
  4398. }
  4399. }
  4400. //---------------------------------------------------------------------------
  4401. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4402. {
  4403. // WORKAROUND
  4404. // When restoring maximized window from minimization,
  4405. // rarely some controls do not align properly.
  4406. // Two instances seen (both for Commander):
  4407. // - When restoring, window is temporarily narrower (not maximized),
  4408. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4409. // Once maximized already, top dock shinks back, but the session PageControl,
  4410. // do not align up, leaving space between TopDock and PageControl.
  4411. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4412. for (int Index = 0; Index < ControlCount; Index++)
  4413. {
  4414. RealignControl(Controls[Index]);
  4415. }
  4416. if (FTrayIcon->Visible)
  4417. {
  4418. FTrayIcon->Visible = false;
  4419. if (Visible)
  4420. {
  4421. ShowWindow(Handle, SW_SHOW);
  4422. }
  4423. }
  4424. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4425. {
  4426. FNeedSession = false;
  4427. NonVisualDataModule->StartBusy();
  4428. try
  4429. {
  4430. NeedSession(true);
  4431. }
  4432. __finally
  4433. {
  4434. NonVisualDataModule->EndBusy();
  4435. }
  4436. }
  4437. }
  4438. //---------------------------------------------------------------------------
  4439. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4440. {
  4441. FTrayIcon->Hint = GetMainForm()->Caption;
  4442. }
  4443. //---------------------------------------------------------------------------
  4444. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4445. {
  4446. UpdateTrayIcon();
  4447. }
  4448. //---------------------------------------------------------------------------
  4449. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4450. {
  4451. // workaround
  4452. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4453. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4454. // But WM_ACTIVATEAPP message can be received even when
  4455. // the main window is minimized to the tray and internal editor window is focused
  4456. // (after another application was previously active)
  4457. if (::IsIconic(Handle))
  4458. {
  4459. if (!IsAppIconic())
  4460. {
  4461. SetAppIconic(true);
  4462. }
  4463. }
  4464. ::ApplicationRestore();
  4465. Application->BringToFront();
  4466. }
  4467. //---------------------------------------------------------------------------
  4468. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4469. {
  4470. RestoreApp();
  4471. }
  4472. //---------------------------------------------------------------------------
  4473. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  4474. {
  4475. if (OpenInNewWindow())
  4476. {
  4477. ExecuteNewInstance(SessionUrl);
  4478. }
  4479. else
  4480. {
  4481. TTerminalManager::Instance()->NewSession(SessionUrl);
  4482. }
  4483. }
  4484. //---------------------------------------------------------------------------
  4485. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4486. {
  4487. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->SessionName;
  4488. StoredSessions->NewSession(SessionName, SessionData);
  4489. // modified only, explicit
  4490. StoredSessions->Save(false, true);
  4491. // encode session name because of slashes in hierarchical sessions
  4492. return EncodeUrlString(SessionName);
  4493. }
  4494. //---------------------------------------------------------------------------
  4495. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4496. {
  4497. // current working directories become defaults here, what is not right
  4498. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4499. return SaveHiddenDuplicateSession(SessionData.get());
  4500. }
  4501. //---------------------------------------------------------------------------
  4502. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4503. {
  4504. if (OpenInNewWindow())
  4505. {
  4506. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4507. }
  4508. else
  4509. {
  4510. // current working directories become defaults here, what is not right
  4511. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4512. TTerminalManager * Manager = TTerminalManager::Instance();
  4513. TManagedTerminal * ATerminal = Manager->NewManagedTerminal(SessionData.get());
  4514. // We definitelly want this
  4515. ATerminal->Disconnected = Terminal->Disconnected;
  4516. // Not sure about these two
  4517. ATerminal->Permanent = Terminal->Permanent;
  4518. ATerminal->DisconnectedTemporarily = Terminal->DisconnectedTemporarily;
  4519. Manager->ActiveTerminal = ATerminal;
  4520. }
  4521. }
  4522. //---------------------------------------------------------------------------
  4523. void __fastcall TCustomScpExplorerForm::RenameSession()
  4524. {
  4525. UnicodeString Name = Terminal->SessionData->SessionName;
  4526. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME))
  4527. {
  4528. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4529. TSessionData::ValidateName(Name);
  4530. // This is inconsistent with how color (for example) is handled.
  4531. Terminal->SessionData->Name = Name;
  4532. UpdateControls();
  4533. // Add/Remove distinguishing paths from sessions of the same name.
  4534. DoTerminalListChanged();
  4535. }
  4536. }
  4537. //---------------------------------------------------------------------------
  4538. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4539. {
  4540. DebugAssert(Queue != NULL);
  4541. bool Result = Queue->IsEmpty;
  4542. if (!Result)
  4543. {
  4544. SetFocus();
  4545. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4546. }
  4547. return Result;
  4548. }
  4549. //---------------------------------------------------------------------------
  4550. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4551. {
  4552. return CanCloseQueue(FQueue);
  4553. }
  4554. //---------------------------------------------------------------------------
  4555. void __fastcall TCustomScpExplorerForm::CloseSession()
  4556. {
  4557. if (CanCloseQueue())
  4558. {
  4559. TTerminalManager::Instance()->FreeActiveTerminal();
  4560. }
  4561. }
  4562. //---------------------------------------------------------------------------
  4563. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4564. {
  4565. if (CanCloseQueue())
  4566. {
  4567. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4568. }
  4569. }
  4570. //---------------------------------------------------------------------------
  4571. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4572. {
  4573. DetachTerminal(Terminal);
  4574. RemoteDirView->Terminal = Terminal;
  4575. UpdateRemotePathComboBox(false);
  4576. UpdateControls();
  4577. }
  4578. //---------------------------------------------------------------------------
  4579. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4580. {
  4581. if (DebugAlwaysTrue(!Terminal->Active))
  4582. {
  4583. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4584. }
  4585. }
  4586. //---------------------------------------------------------------------------
  4587. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4588. {
  4589. if (OpenInNewWindow())
  4590. {
  4591. // encode session name because of slashes in hierarchical sessions
  4592. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4593. }
  4594. else
  4595. {
  4596. TTerminalManager * Manager = TTerminalManager::Instance();
  4597. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4598. Manager->ActiveTerminal = Terminal;
  4599. }
  4600. }
  4601. //---------------------------------------------------------------------------
  4602. void TCustomScpExplorerForm::DoOpenFolderOrWorkspace(
  4603. const UnicodeString & Name, bool ConnectFirstTerminal, bool CheckMaxSessions)
  4604. {
  4605. TTerminalManager * Manager = TTerminalManager::Instance();
  4606. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4607. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4608. if (!CheckMaxSessions || (DataList->Count <= WinConfiguration->MaxSessions))
  4609. {
  4610. TManagedTerminal * FirstTerminal = Manager->NewTerminals(DataList.get());
  4611. // FirstTerminal can be null, if some of the
  4612. if (!ConnectFirstTerminal && (FirstTerminal != NULL))
  4613. {
  4614. FirstTerminal->Disconnected = true;
  4615. FirstTerminal->DisconnectedTemporarily = true;
  4616. }
  4617. Manager->ActiveTerminal = FirstTerminal;
  4618. }
  4619. }
  4620. //---------------------------------------------------------------------------
  4621. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4622. {
  4623. if (OpenInNewWindow())
  4624. {
  4625. ExecuteNewInstance(Name);
  4626. }
  4627. else
  4628. {
  4629. DoOpenFolderOrWorkspace(Name, true, false);
  4630. }
  4631. }
  4632. //---------------------------------------------------------------------------
  4633. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4634. bool &CanClose)
  4635. {
  4636. // See the comment in CloseApp()
  4637. if (NonVisualDataModule->Busy)
  4638. {
  4639. CanClose = false;
  4640. }
  4641. else
  4642. {
  4643. TTerminalManager * Manager = TTerminalManager::Instance();
  4644. int ActiveSessions = 0;
  4645. for (int Index = 0; Index < Manager->Count; Index++)
  4646. {
  4647. if (Manager->Terminals[Index]->Active)
  4648. {
  4649. ActiveSessions++;
  4650. }
  4651. }
  4652. // Confirm, when there's at least one active remote session or
  4653. // when there are multiple (inactive or local) sessions and auto workspace saving is not enabled.
  4654. if (((ActiveSessions > 0) || ((Manager->Count > 1) && !WinConfiguration->AutoSaveWorkspace)) &&
  4655. WinConfiguration->ConfirmClosingSession)
  4656. {
  4657. unsigned int Result;
  4658. TMessageParams Params(mpNeverAskAgainCheck);
  4659. UnicodeString Message;
  4660. int Answers = qaOK | qaCancel;
  4661. // The current session is the only active session
  4662. if ((ActiveSessions == 1) && (Terminal != NULL) && Terminal->Active)
  4663. {
  4664. if (!WinConfiguration->AutoSaveWorkspace)
  4665. {
  4666. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  4667. }
  4668. else
  4669. {
  4670. Message = LoadStr(CLOSE_SESSION);
  4671. }
  4672. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  4673. }
  4674. // Multiple active sessions or one active session, but it's not the current one
  4675. else if (ActiveSessions > 0)
  4676. {
  4677. if (!WinConfiguration->AutoSaveWorkspace)
  4678. {
  4679. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  4680. }
  4681. else
  4682. {
  4683. Message = LoadStr(CLOSE_SESSIONS);
  4684. }
  4685. }
  4686. // Multiple tabs, but all are intactive or local, and saving of worspace is not enabled
  4687. else
  4688. {
  4689. DebugAlwaysTrue(Manager->Count > 1);
  4690. DebugAlwaysTrue(!WinConfiguration->AutoSaveWorkspace);
  4691. Message = LoadStr(CLOSE_WORKSPACE);
  4692. }
  4693. UnicodeString Note;
  4694. if (WinConfiguration->AutoSaveWorkspace)
  4695. {
  4696. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  4697. }
  4698. else
  4699. {
  4700. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  4701. Answers = qaYes | qaNo | qaCancel;
  4702. }
  4703. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  4704. SetFocus();
  4705. Result = MessageDialog(Message, qtConfirmation,
  4706. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  4707. if (Result == qaNeverAskAgain)
  4708. {
  4709. WinConfiguration->ConfirmClosingSession = false;
  4710. }
  4711. if (Result == qaNo)
  4712. {
  4713. CanClose = SaveWorkspace(true);
  4714. // note that the workspace will be saved redundatly again from FormClose
  4715. }
  4716. else
  4717. {
  4718. CanClose =
  4719. (Result == qaOK) ||
  4720. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4721. (Result == qaNeverAskAgain);
  4722. }
  4723. }
  4724. if (CanClose && HasActiveTerminal())
  4725. {
  4726. CanClose = CanCloseQueue();
  4727. }
  4728. }
  4729. if (CanClose)
  4730. {
  4731. CanClose =
  4732. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4733. FEditorManager->CloseExternalFilesWithoutProcess();
  4734. if (CanClose)
  4735. {
  4736. while (CanClose && (FLocalEditors->Count > 0))
  4737. {
  4738. int PrevCount = FLocalEditors->Count;
  4739. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4740. CanClose = (FLocalEditors->Count < PrevCount);
  4741. }
  4742. if (CanClose)
  4743. {
  4744. CanClose = FEditorManager->Empty(true);
  4745. if (!CanClose)
  4746. {
  4747. SetFocus();
  4748. CanClose =
  4749. (MessageDialog(
  4750. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4751. }
  4752. }
  4753. }
  4754. }
  4755. }
  4756. //---------------------------------------------------------------------------
  4757. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4758. {
  4759. TForm * Form = dynamic_cast<TForm *>(Sender);
  4760. DebugAssert(Form != NULL);
  4761. Form->Close();
  4762. }
  4763. //---------------------------------------------------------------------------
  4764. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4765. {
  4766. TForm * Form = dynamic_cast<TForm *>(Sender);
  4767. delete Form;
  4768. }
  4769. //---------------------------------------------------------------------------
  4770. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4771. {
  4772. while (FLocalEditors->Count > 0)
  4773. {
  4774. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4775. }
  4776. }
  4777. //---------------------------------------------------------------------------
  4778. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4779. TObject *Sender)
  4780. {
  4781. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4782. try
  4783. {
  4784. SetProperties(osRemote, FileList);
  4785. }
  4786. __finally
  4787. {
  4788. delete FileList;
  4789. }
  4790. }
  4791. //---------------------------------------------------------------------------
  4792. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4793. {
  4794. if (value)
  4795. {
  4796. if (Component == fcCustomCommandsBand)
  4797. {
  4798. UpdateCustomCommandsToolbar();
  4799. }
  4800. if (Component == fcQueueFileList)
  4801. {
  4802. DebugAssert(!QueueFileListSplitter->Visible);
  4803. AdjustQueueLayout();
  4804. UpdateQueueFileList();
  4805. }
  4806. }
  4807. }
  4808. //---------------------------------------------------------------------------
  4809. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  4810. {
  4811. int MinHeightLimit =
  4812. GetStaticQueuePanelComponentsHeight() +
  4813. QueueFileListSplitter->Height +
  4814. QueueFileList->Height +
  4815. GetMinQueueViewHeight();
  4816. if (QueuePanel->ClientHeight < MinHeightLimit)
  4817. {
  4818. QueuePanel->ClientHeight = MinHeightLimit;
  4819. }
  4820. }
  4821. //---------------------------------------------------------------------------
  4822. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4823. {
  4824. TControl * Control = GetComponent(Component);
  4825. DebugAssert(Control);
  4826. bool Changed = (Control->Visible != value);
  4827. if (Changed)
  4828. {
  4829. ComponentShowing(Component, value);
  4830. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4831. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4832. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4833. if (value)
  4834. {
  4835. int RemainingHeight = Control->Parent->ClientHeight;
  4836. int RemainingWidth = Control->Parent->ClientWidth;
  4837. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4838. {
  4839. TControl * ChildControl = Control->Parent->Controls[i];
  4840. if (ChildControl->Visible)
  4841. {
  4842. switch (ChildControl->Align)
  4843. {
  4844. case alTop:
  4845. case alBottom:
  4846. RemainingHeight -= ChildControl->Height;
  4847. break;
  4848. case alLeft:
  4849. case alRight:
  4850. RemainingWidth -= ChildControl->Width;
  4851. break;
  4852. }
  4853. }
  4854. }
  4855. int Reserve = ScaleByTextHeight(this, 32);
  4856. // queue in explorer, trees in commander
  4857. if (Control->Height > RemainingHeight - Reserve)
  4858. {
  4859. Control->Height = RemainingHeight / 2;
  4860. }
  4861. if (Control->Width > RemainingWidth - Reserve)
  4862. {
  4863. Control->Width = RemainingWidth / 2;
  4864. }
  4865. }
  4866. Control->Visible = value;
  4867. if (WasFocused && Visible)
  4868. {
  4869. DirView(osCurrent)->SetFocus();
  4870. }
  4871. FixControlsPlacement();
  4872. }
  4873. }
  4874. //---------------------------------------------------------------------------
  4875. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4876. {
  4877. TControl * Control = GetComponent(Component);
  4878. if (Control == NULL)
  4879. {
  4880. return false;
  4881. }
  4882. else
  4883. {
  4884. return Control->Visible;
  4885. }
  4886. }
  4887. //---------------------------------------------------------------------------
  4888. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4889. {
  4890. bool Result;
  4891. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4892. {
  4893. Result = !IsUWP();
  4894. }
  4895. else
  4896. {
  4897. Result = true;
  4898. }
  4899. return Result;
  4900. }
  4901. //---------------------------------------------------------------------------
  4902. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4903. {
  4904. if (DirView(osOther)->ItemFocused != NULL)
  4905. {
  4906. DirView(osOther)->ItemFocused->MakeVisible(false);
  4907. }
  4908. QueueSplitter->Visible = QueuePanel->Visible;
  4909. QueueFileListSplitter->Visible = QueueFileList->Visible;
  4910. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  4911. TControl * QueueControlsOrder[] =
  4912. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  4913. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  4914. }
  4915. //---------------------------------------------------------------------------
  4916. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4917. {
  4918. switch (Component) {
  4919. case fcStatusBar: return RemoteStatusBar;
  4920. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4921. case fcQueueView: return QueuePanel;
  4922. case fcQueueToolbar: return QueueDock;
  4923. case fcRemoteTree: return RemoteDrivePanel;
  4924. case fcSessionsTabs: return SessionsPageControl;
  4925. case fcQueueFileList: return QueueFileList;
  4926. default: return NULL;
  4927. }
  4928. }
  4929. //---------------------------------------------------------------------------
  4930. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4931. TObject *Sender, TListColumn *Column, TPoint &Point)
  4932. {
  4933. DebugAssert(NonVisualDataModule && Column && Sender);
  4934. NonVisualDataModule->ListColumn = Column;
  4935. TPopupMenu * DirViewColumnMenu;
  4936. if (dynamic_cast<TUnixDirView *>(Sender) != NULL)
  4937. {
  4938. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4939. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4940. (Column->Index == uvName);
  4941. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4942. (Column->Index == uvSize);
  4943. }
  4944. else
  4945. {
  4946. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4947. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4948. (Column->Index == dvName);
  4949. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4950. (Column->Index == dvSize);
  4951. }
  4952. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  4953. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  4954. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  4955. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4956. }
  4957. //---------------------------------------------------------------------------
  4958. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4959. TObject *Sender, TListItem *Item, bool &AllowExec)
  4960. {
  4961. DoDirViewExecFile(Sender, Item, AllowExec);
  4962. }
  4963. //---------------------------------------------------------------------------
  4964. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4965. TListItem * Item, bool & AllowExec)
  4966. {
  4967. DebugAssert(Sender && Item && Configuration);
  4968. DebugAssert(AllowExec);
  4969. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  4970. bool Remote = (ADirView == DirView(osRemote)) && !IsSideLocalBrowser(osRemote);
  4971. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles();
  4972. TOperationSide Side = (Remote ? osRemote : osLocal);
  4973. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  4974. // on files only (not directories)
  4975. // and only when symlinks are resolved (apply to remote panel only)
  4976. if (!ADirView->ItemIsDirectory(Item) &&
  4977. (ResolvedSymlinks || FForceExecution))
  4978. {
  4979. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  4980. !FForceExecution && ResolvedSymlinks)
  4981. {
  4982. if (WinConfiguration->DoubleClickAction == dcaCopy)
  4983. {
  4984. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  4985. UnlockWindow();
  4986. try
  4987. {
  4988. ExecuteFileOperation(
  4989. foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  4990. }
  4991. __finally
  4992. {
  4993. LockWindow();
  4994. }
  4995. AllowExec = false;
  4996. }
  4997. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  4998. {
  4999. if (!Remote || !WinConfiguration->DisableOpenEdit)
  5000. {
  5001. ExecuteFile(Side, efDefaultEditor);
  5002. AllowExec = false;
  5003. }
  5004. }
  5005. else
  5006. {
  5007. DebugFail();
  5008. }
  5009. }
  5010. // if we have not done anything special, fall back to default behavior
  5011. if (AllowExec)
  5012. {
  5013. if (Remote && !WinConfiguration->DisableOpenEdit)
  5014. {
  5015. ExecuteFile(osRemote, efShell);
  5016. AllowExec = false;
  5017. }
  5018. }
  5019. }
  5020. }
  5021. //---------------------------------------------------------------------------
  5022. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5023. {
  5024. return ((Side == osRemote) || (Side == osCurrent));
  5025. }
  5026. //---------------------------------------------------------------------------
  5027. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5028. {
  5029. DebugFail();
  5030. }
  5031. //---------------------------------------------------------------------------
  5032. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5033. {
  5034. DebugFail();
  5035. }
  5036. //---------------------------------------------------------------------------
  5037. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5038. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5039. {
  5040. TSynchronizeParamType Params;
  5041. Params.LocalDirectory = LocalDirectory;
  5042. Params.RemoteDirectory = RemoteDirectory;
  5043. int UnusedParams =
  5044. (GUIConfiguration->SynchronizeParams &
  5045. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5046. TTerminal::spCaseSensitive));
  5047. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5048. Params.Options = GUIConfiguration->SynchronizeOptions;
  5049. bool SaveSettings = false;
  5050. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5051. &DoSynchronizeTooManyDirectories);
  5052. DebugAssert(FSynchronizeController == NULL);
  5053. FSynchronizeController = &Controller;
  5054. bool Result;
  5055. try
  5056. {
  5057. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5058. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5059. int Options =
  5060. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5061. DebugAssert(FOnFeedSynchronizeError == NULL);
  5062. DebugAssert(FOnSynchronizeAbort == NULL);
  5063. DebugAssert(FSynchronizeTerminal == NULL);
  5064. FSynchronizeTerminal = Terminal;
  5065. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5066. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5067. FOnFeedSynchronizeError, FOnSynchronizeAbort, SynchronizeInNewWindow, UseDefaults);
  5068. if (Result)
  5069. {
  5070. if (SaveSettings)
  5071. {
  5072. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5073. GUIConfiguration->SynchronizeOptions = Params.Options;
  5074. }
  5075. else
  5076. {
  5077. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5078. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5079. FLAGSET(Params.Options, soSynchronize))
  5080. {
  5081. GUIConfiguration->SynchronizeOptions =
  5082. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5083. soSynchronize;
  5084. }
  5085. }
  5086. LocalDirectory = Params.LocalDirectory;
  5087. RemoteDirectory = Params.RemoteDirectory;
  5088. }
  5089. }
  5090. __finally
  5091. {
  5092. FSynchronizeController = NULL;
  5093. DebugAssert(FOnFeedSynchronizeError == NULL);
  5094. FOnFeedSynchronizeError = NULL;
  5095. DebugAssert(FOnSynchronizeAbort == NULL);
  5096. FOnSynchronizeAbort = NULL;
  5097. FSynchronizeTerminal = NULL;
  5098. }
  5099. return Result;
  5100. }
  5101. //---------------------------------------------------------------------------
  5102. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5103. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5104. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5105. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5106. TSynchronizeOptions * Options, bool Full)
  5107. {
  5108. // Keep-up-to-date mode
  5109. if (Terminal->Status == ssOpened)
  5110. {
  5111. try
  5112. {
  5113. int PParams = Params.Params;
  5114. if (!Full)
  5115. {
  5116. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5117. if (FLAGSET(Params.Options, soRecurse))
  5118. {
  5119. PParams |= TTerminal::spSubDirs;
  5120. }
  5121. }
  5122. else
  5123. {
  5124. // if keepuptodate is non-recursive,
  5125. // full sync before has to be non-recursive as well
  5126. if (FLAGCLEAR(Params.Options, soRecurse))
  5127. {
  5128. PParams |= TTerminal::spNoRecurse;
  5129. }
  5130. }
  5131. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5132. PParams, Checklist, Options);
  5133. }
  5134. catch (Exception & E)
  5135. {
  5136. if (!Terminal->Active)
  5137. {
  5138. ShowExtendedExceptionEx(Terminal, &E);
  5139. // Do not abort the "keep up to date" when the session was succesfully reconnected.
  5140. if (!Terminal->Active)
  5141. {
  5142. throw;
  5143. }
  5144. }
  5145. else
  5146. {
  5147. if (FLAGSET(Params.Options, soContinueOnError) &&
  5148. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5149. {
  5150. // noop, already logged in MoreMessageDialog
  5151. }
  5152. else
  5153. {
  5154. // We get mostly EAbort here, so this is noop.
  5155. // Exception is an error when listing directories while looking for differences.
  5156. ShowExtendedExceptionEx(Terminal, &E);
  5157. throw;
  5158. }
  5159. }
  5160. }
  5161. }
  5162. }
  5163. //---------------------------------------------------------------------------
  5164. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5165. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5166. const UnicodeString ErrorStr)
  5167. {
  5168. if (!Directory.IsEmpty())
  5169. {
  5170. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5171. }
  5172. else
  5173. {
  5174. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5175. }
  5176. }
  5177. //---------------------------------------------------------------------------
  5178. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5179. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5180. {
  5181. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5182. {
  5183. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5184. }
  5185. else
  5186. {
  5187. TMessageParams Params(mpNeverAskAgainCheck);
  5188. unsigned int Result = MessageDialog(
  5189. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5190. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5191. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5192. {
  5193. MaxDirectories *= 2;
  5194. if (Result == qaNeverAskAgain)
  5195. {
  5196. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5197. }
  5198. }
  5199. else
  5200. {
  5201. Abort();
  5202. }
  5203. }
  5204. }
  5205. //---------------------------------------------------------------------------
  5206. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5207. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5208. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5209. TSynchronizeOptions * Options)
  5210. {
  5211. // Keep-up-to-date mode
  5212. DebugAssert(!FAutoOperation);
  5213. void * BatchStorage;
  5214. BatchStart(BatchStorage);
  5215. FAutoOperation = true;
  5216. bool AnyOperation = false;
  5217. TDateTime StartTime = Now();
  5218. TSynchronizeChecklist * AChecklist = NULL;
  5219. try
  5220. {
  5221. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5222. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5223. {
  5224. FSynchronizeProgressForm->Start();
  5225. }
  5226. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5227. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5228. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5229. Options);
  5230. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5231. SAFE_DESTROY(FSynchronizeProgressForm);
  5232. AnyOperation = (AChecklist->CheckedCount > 0);
  5233. if (AnyOperation)
  5234. {
  5235. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5236. CreateProgressForm(&SynchronizeProgress);
  5237. try
  5238. {
  5239. Terminal->SynchronizeApply(
  5240. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5241. SynchronizeProcessedItem, NULL, NULL, NULL);
  5242. }
  5243. __finally
  5244. {
  5245. DestroyProgressForm();
  5246. }
  5247. }
  5248. }
  5249. __finally
  5250. {
  5251. if (Checklist == NULL)
  5252. {
  5253. delete AChecklist;
  5254. }
  5255. else
  5256. {
  5257. *Checklist = AChecklist;
  5258. }
  5259. FAutoOperation = false;
  5260. SAFE_DESTROY(FSynchronizeProgressForm);
  5261. BatchEnd(BatchStorage);
  5262. ReloadLocalDirectory();
  5263. if (AnyOperation)
  5264. {
  5265. OperationComplete(StartTime);
  5266. }
  5267. }
  5268. }
  5269. //---------------------------------------------------------------------------
  5270. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5271. {
  5272. // can be called from command line
  5273. return Visible &&
  5274. ((DirView(osRemote)->SelCount > 0) ||
  5275. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5276. }
  5277. //---------------------------------------------------------------------------
  5278. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5279. {
  5280. LogSynchronizeEvent(Terminal, Message);
  5281. }
  5282. //---------------------------------------------------------------------------
  5283. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5284. int Params, TSynchronizeOptions & Options)
  5285. {
  5286. DebugAssert(!IsLocalBrowserMode());
  5287. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5288. {
  5289. Options.Filter = new TStringList();
  5290. Options.Filter->CaseSensitive = false;
  5291. Options.Filter->Duplicates = Types::dupAccept;
  5292. if (DirView(osRemote)->SelCount > 0)
  5293. {
  5294. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5295. }
  5296. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5297. {
  5298. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5299. }
  5300. Options.Filter->Sort();
  5301. }
  5302. }
  5303. //---------------------------------------------------------------------------
  5304. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5305. {
  5306. TCopyParamType Defaults;
  5307. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5308. ConfigStorage->AccessMode = smRead;
  5309. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5310. {
  5311. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5312. }
  5313. ConfigStorage.reset(NULL);
  5314. std::unique_ptr<TStringList> Options(new TStringList());
  5315. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5316. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5317. UnicodeString Result;
  5318. if (Options->Count > 0)
  5319. {
  5320. Result =
  5321. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5322. }
  5323. return Result;
  5324. }
  5325. //---------------------------------------------------------------------------
  5326. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5327. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5328. {
  5329. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5330. UnicodeString AdditionalParams =
  5331. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5332. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5333. SerializeCopyParamForCommandLine(CopyParams),
  5334. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5335. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5336. ExecuteNewInstance(SessionName, AdditionalParams);
  5337. }
  5338. //---------------------------------------------------------------------------
  5339. struct TSynchronizeParams
  5340. {
  5341. UnicodeString LocalDirectory;
  5342. UnicodeString RemoteDirectory;
  5343. TSynchronizeMode Mode;
  5344. TSynchronizeChecklist * Checklist;
  5345. int Params;
  5346. TCopyParamType * CopyParam;
  5347. TDateTime CollectElapsed;
  5348. TDateTime * StartTime;
  5349. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5350. };
  5351. //---------------------------------------------------------------------------
  5352. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5353. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5354. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5355. {
  5356. DebugAssert(!FAutoOperation);
  5357. void * BatchStorage;
  5358. BatchStart(BatchStorage);
  5359. FAutoOperation = true;
  5360. TFileOperationStatistics Statistics;
  5361. TDateTime Start = Now();
  5362. try
  5363. {
  5364. Params.OnProcessedItem = OnProcessedItem;
  5365. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5366. CreateProgressForm(&SynchronizeProgress);
  5367. Terminal->SynchronizeApply(
  5368. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5369. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5370. &Statistics);
  5371. }
  5372. __finally
  5373. {
  5374. Params.OnProcessedItem = NULL;
  5375. FAutoOperation = false;
  5376. DestroyProgressForm();
  5377. BatchEnd(BatchStorage);
  5378. ReloadLocalDirectory();
  5379. }
  5380. if (WinConfiguration->SynchronizeSummary)
  5381. {
  5382. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5383. // The statistics should be 0 anyway in this case
  5384. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5385. {
  5386. Message += L"\n";
  5387. if (Statistics.FilesUploaded > 0)
  5388. {
  5389. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5390. }
  5391. if (Statistics.FilesDownloaded > 0)
  5392. {
  5393. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5394. }
  5395. if (Statistics.FilesDeletedLocal > 0)
  5396. {
  5397. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5398. }
  5399. if (Statistics.FilesDeletedRemote > 0)
  5400. {
  5401. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5402. }
  5403. }
  5404. TDateTime Elapsed = (Now() - Start);
  5405. Message +=
  5406. L"\n" +
  5407. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5408. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5409. TMessageParams Params(mpNeverAskAgainCheck);
  5410. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5411. if (Result == qaNeverAskAgain)
  5412. {
  5413. WinConfiguration->SynchronizeSummary = false;
  5414. }
  5415. }
  5416. }
  5417. //---------------------------------------------------------------------------
  5418. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5419. {
  5420. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5421. {
  5422. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5423. }
  5424. // Not set in keep-up-to-date mode - Synchronize() method
  5425. if (Token != NULL)
  5426. {
  5427. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5428. if (Params.OnProcessedItem != NULL)
  5429. {
  5430. Params.OnProcessedItem(NULL, ChecklistItem);
  5431. }
  5432. }
  5433. }
  5434. //---------------------------------------------------------------------------
  5435. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5436. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5437. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5438. {
  5439. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5440. *Params.StartTime = Now();
  5441. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5442. }
  5443. //---------------------------------------------------------------------------
  5444. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5445. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5446. {
  5447. // terminal can be already closed (e.g. dropped connection)
  5448. if (Terminal != NULL)
  5449. {
  5450. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5451. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5452. }
  5453. }
  5454. //---------------------------------------------------------------------------
  5455. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5456. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5457. {
  5458. DebugAssert(!IsLocalBrowserMode());
  5459. TAutoBatch AutoBatch(this);
  5460. TAutoFlag AutoOperationFlag(FAutoOperation);
  5461. if (Side == osRemote)
  5462. {
  5463. std::unique_ptr<TStrings> FileList(new TStringList());
  5464. FileList->AddObject(FileName, RemoteFile);
  5465. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5466. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5467. RemoteDirView->SaveSelection();
  5468. RemoteDirView->SaveSelectedNames();
  5469. try
  5470. {
  5471. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5472. }
  5473. catch(...)
  5474. {
  5475. RemoteDirView->DiscardSavedSelection();
  5476. throw;
  5477. }
  5478. RemoteDirView->RestoreSelection();
  5479. }
  5480. else if (DebugAlwaysTrue(Side == osLocal))
  5481. {
  5482. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5483. {
  5484. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5485. }
  5486. UnicodeString Directory = ExtractFileDir(FileName);
  5487. ReloadLocalDirectory(Directory);
  5488. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5489. if (!SamePaths(Directory, NewDirectory))
  5490. {
  5491. ReloadLocalDirectory(NewDirectory);
  5492. }
  5493. }
  5494. }
  5495. //---------------------------------------------------------------------------
  5496. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5497. {
  5498. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5499. if (Side == osLocal)
  5500. {
  5501. if (Action == TSynchronizeChecklist::saDownloadNew)
  5502. {
  5503. OpenFolderInExplorer(LocalPath);
  5504. }
  5505. else
  5506. {
  5507. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5508. }
  5509. }
  5510. else if (DebugAlwaysTrue(Side == osRemote))
  5511. {
  5512. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5513. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5514. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5515. if (!LocalPath.IsEmpty())
  5516. {
  5517. SessionData->LocalDirectory = LocalPath;
  5518. }
  5519. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5520. ExecuteNewInstance(SessionName, FORMAT(L"%s=\"%s\"", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5521. }
  5522. }
  5523. //---------------------------------------------------------------------------
  5524. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5525. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5526. const TCopyParamType * CopyParams)
  5527. {
  5528. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5529. UnicodeString AdditionalParams =
  5530. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5531. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5532. SerializeCopyParamForCommandLine(CopyParams),
  5533. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5534. LocalDirectory, RemoteDirectory, Mode, Params));
  5535. ExecuteNewInstance(SessionName, AdditionalParams);
  5536. }
  5537. //---------------------------------------------------------------------------
  5538. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5539. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5540. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5541. {
  5542. int Result;
  5543. bool SaveSettings = false;
  5544. int Options =
  5545. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5546. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5547. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5548. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5549. bool Continue =
  5550. (UseDefaults == 0) ||
  5551. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5552. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5553. if (Continue)
  5554. {
  5555. Configuration->Usage->Inc(L"Synchronizations");
  5556. UpdateCopyParamCounters(CopyParam);
  5557. TSynchronizeOptions SynchronizeOptions;
  5558. GetSynchronizeOptions(Params, SynchronizeOptions);
  5559. if (SaveSettings)
  5560. {
  5561. GUIConfiguration->SynchronizeParams = Params;
  5562. }
  5563. else
  5564. {
  5565. SaveMode = false;
  5566. }
  5567. TDateTime StartTime = Now();
  5568. TSynchronizeChecklist * Checklist = NULL;
  5569. try
  5570. {
  5571. DebugAssert(!FAutoOperation);
  5572. FAutoOperation = true;
  5573. try
  5574. {
  5575. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5576. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5577. int Files = -1;
  5578. if (DataList->Count >= 1)
  5579. {
  5580. Files = StrToIntDef(DataList->Strings[0], Files);
  5581. }
  5582. else
  5583. {
  5584. DataList->Add(UnicodeString());
  5585. }
  5586. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5587. FSynchronizeProgressForm->Start();
  5588. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5589. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5590. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5591. &SynchronizeOptions);
  5592. if (Terminal->SessionData->CacheDirectories)
  5593. {
  5594. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5595. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5596. }
  5597. }
  5598. __finally
  5599. {
  5600. FAutoOperation = false;
  5601. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5602. SAFE_DESTROY(FSynchronizeProgressForm);
  5603. }
  5604. TSynchronizeParams SynchronizeParams;
  5605. SynchronizeParams.LocalDirectory = LocalDirectory;
  5606. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5607. SynchronizeParams.Mode = Mode;
  5608. SynchronizeParams.CopyParam = &CopyParam;
  5609. SynchronizeParams.Params = Params;
  5610. SynchronizeParams.Checklist = Checklist;
  5611. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5612. SynchronizeParams.StartTime = &StartTime;
  5613. SynchronizeParams.OnProcessedItem = NULL;
  5614. Result = Checklist->Count;
  5615. if (Checklist->Count > 0)
  5616. {
  5617. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5618. {
  5619. if (!DoSynchronizeChecklistDialog(
  5620. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5621. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  5622. {
  5623. Result = -1;
  5624. }
  5625. }
  5626. else
  5627. {
  5628. FullSynchronize(SynchronizeParams, NULL, NULL);
  5629. }
  5630. }
  5631. else
  5632. {
  5633. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5634. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5635. }
  5636. }
  5637. __finally
  5638. {
  5639. delete Checklist;
  5640. }
  5641. OperationComplete(StartTime);
  5642. }
  5643. else
  5644. {
  5645. Result = -1;
  5646. }
  5647. return Result;
  5648. }
  5649. //---------------------------------------------------------------------------
  5650. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5651. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5652. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5653. {
  5654. if (Collect)
  5655. {
  5656. DebugAssert(FSynchronizeProgressForm != NULL);
  5657. if (!FSynchronizeProgressForm->Started)
  5658. {
  5659. FSynchronizeProgressForm->Start();
  5660. }
  5661. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5662. SetTaskbarListProgressValue(CompareProgress);
  5663. }
  5664. else
  5665. {
  5666. DebugAssert(FSynchronizeProgressForm == NULL);
  5667. DebugAssert(FProgressForm != NULL);
  5668. }
  5669. }
  5670. //---------------------------------------------------------------------------
  5671. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5672. {
  5673. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  5674. TRemoteFile * File = NULL;
  5675. Terminal->ReadFile(FullFileName, File);
  5676. if (File != NULL)
  5677. {
  5678. std::unique_ptr<TRemoteFile> FileOwner(File);
  5679. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5680. Application->ShowMainForm = false;
  5681. Application->Run();
  5682. }
  5683. }
  5684. //---------------------------------------------------------------------------
  5685. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  5686. {
  5687. DebugFail();
  5688. }
  5689. //---------------------------------------------------------------------------
  5690. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5691. {
  5692. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5693. SessionData->Assign(Terminal->SessionData);
  5694. UpdateSessionData(SessionData.get());
  5695. if (Terminal->Active)
  5696. {
  5697. Terminal->UpdateSessionCredentials(SessionData.get());
  5698. }
  5699. if (!Terminal->SessionData->HasSessionName())
  5700. {
  5701. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5702. // once IsWorkspace is cleared
  5703. SessionData->Name = UnicodeString();
  5704. }
  5705. // Most uses of this method ends with saving the session, and we do not want to store the IsWorkspace flag.
  5706. // Particularly, with SaveHiddenDuplicateSession, it won't work as TSessionData::ParseUrl explicitly ignored workspace sessions.
  5707. SessionData->IsWorkspace = false;
  5708. return SessionData.release();
  5709. }
  5710. //---------------------------------------------------------------------------
  5711. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5712. {
  5713. TSessionData * SessionData = CloneCurrentSessionData();
  5714. try
  5715. {
  5716. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5717. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5718. }
  5719. __finally
  5720. {
  5721. delete SessionData;
  5722. }
  5723. }
  5724. //---------------------------------------------------------------------------
  5725. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5726. {
  5727. TTerminalManager * Manager = TTerminalManager::Instance();
  5728. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5729. if (DebugAlwaysTrue(Terminal != NULL))
  5730. {
  5731. // Update (Managed)Terminal->StateData
  5732. UpdateTerminal(Terminal);
  5733. }
  5734. Manager->SaveWorkspace(DataList.get());
  5735. return DataList.release();
  5736. }
  5737. //---------------------------------------------------------------------------
  5738. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5739. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5740. {
  5741. WinConfiguration->LastWorkspace = Name;
  5742. if (!SavePasswords)
  5743. {
  5744. for (int Index = 0; Index < DataList->Count; Index++)
  5745. {
  5746. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5747. if (SessionData->Link.IsEmpty())
  5748. {
  5749. SessionData->ClearSessionPasswords();
  5750. }
  5751. }
  5752. }
  5753. StoredSessions->NewWorkspace(Name, DataList);
  5754. // modified only
  5755. StoredSessions->Save(false, Explicit);
  5756. }
  5757. //---------------------------------------------------------------------------
  5758. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5759. {
  5760. return
  5761. DefaultStr(WinConfiguration->LastWorkspace,
  5762. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5763. }
  5764. //---------------------------------------------------------------------------
  5765. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5766. {
  5767. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5768. bool AnyNonStoredSessionWithPassword = false;
  5769. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5770. bool AllNonStoredSessionsAnonymous = true;
  5771. TTerminalManager * Manager = TTerminalManager::Instance();
  5772. for (int Index = 0; Index < DataList->Count; Index++)
  5773. {
  5774. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5775. if (SessionData->Link.IsEmpty())
  5776. {
  5777. if (SessionData->HasAnySessionPassword())
  5778. {
  5779. AnyNonStoredSessionWithPassword = true;
  5780. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  5781. {
  5782. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5783. }
  5784. }
  5785. if (!SameText(SessionData->UserName, AnonymousUserName))
  5786. {
  5787. AllNonStoredSessionsAnonymous = false;
  5788. }
  5789. }
  5790. }
  5791. bool SavePasswords;
  5792. bool * PSavePasswords;
  5793. bool NotRecommendedSavingPasswords =
  5794. !CustomWinConfiguration->UseMasterPassword &&
  5795. !AllNonStoredSessionsAnonymous;
  5796. if (Configuration->DisablePasswordStoring ||
  5797. !AnyNonStoredSessionWithPassword)
  5798. {
  5799. PSavePasswords = NULL;
  5800. SavePasswords = false;
  5801. }
  5802. else
  5803. {
  5804. PSavePasswords = &SavePasswords;
  5805. SavePasswords =
  5806. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5807. !NotRecommendedSavingPasswords;
  5808. }
  5809. UnicodeString Name = WorkspaceName();
  5810. bool CreateShortcut = false;
  5811. bool Result =
  5812. DoSaveWorkspaceDialog(
  5813. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5814. EnableAutoSave);
  5815. if (Result)
  5816. {
  5817. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5818. if (CreateShortcut)
  5819. {
  5820. TOperationVisualizer Visualizer;
  5821. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5822. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5823. }
  5824. if (EnableAutoSave)
  5825. {
  5826. WinConfiguration->AutoSaveWorkspace = true;
  5827. WinConfiguration->AutoWorkspace = Name;
  5828. if (PSavePasswords != NULL)
  5829. {
  5830. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5831. }
  5832. }
  5833. }
  5834. return Result;
  5835. }
  5836. //---------------------------------------------------------------------------
  5837. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TManagedTerminal * Terminal)
  5838. {
  5839. DebugAssert(!IsLocalBrowserMode());
  5840. SAFE_DESTROY(Terminal->RemoteExplorerState);
  5841. if (WinConfiguration->PreservePanelState)
  5842. {
  5843. Terminal->RemoteExplorerState = RemoteDirView->SaveState();
  5844. }
  5845. UpdateSessionData(Terminal->StateData);
  5846. }
  5847. //---------------------------------------------------------------------------
  5848. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5849. {
  5850. // Keep in sync with TSessionData::CopyStateData
  5851. DebugAssert(!IsLocalBrowserMode());
  5852. DebugAssert(Data != NULL);
  5853. // This is inconsistent with how color (for example) is handled.
  5854. Data->Name = Terminal->SessionData->Name;
  5855. // cannot use RemoteDirView->Path, because it is empty if connection
  5856. // was already closed
  5857. // also only peek, we may not be connected at all atm
  5858. // so make sure we do not try retrieving current directory from the server
  5859. // (particularly with FTP)
  5860. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5861. if (!ACurrentDirectory.IsEmpty())
  5862. {
  5863. Data->RemoteDirectory = ACurrentDirectory;
  5864. }
  5865. Data->Color = SessionColor;
  5866. }
  5867. //---------------------------------------------------------------------------
  5868. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5869. {
  5870. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5871. DebugAssert(Toolbar != NULL);
  5872. for (int i = 0; i < Toolbar->Items->Count; i++)
  5873. {
  5874. TTBXCustomDropDownItem * DropDownItem;
  5875. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5876. if (DropDownItem != NULL)
  5877. {
  5878. ToolbarItemResize(DropDownItem,
  5879. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5880. Toolbar->NonClientWidth);
  5881. break;
  5882. }
  5883. }
  5884. }
  5885. //---------------------------------------------------------------------------
  5886. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5887. {
  5888. Item->EditWidth = Width;
  5889. }
  5890. //---------------------------------------------------------------------------
  5891. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5892. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5893. {
  5894. for (int i = 0; i < Toolbar->Items->Count; i++)
  5895. {
  5896. TTBXCustomDropDownItem * DropDownItem;
  5897. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5898. if (DropDownItem != NULL)
  5899. {
  5900. ASize.x -= DropDownItem->EditWidth;
  5901. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5902. }
  5903. }
  5904. }
  5905. //---------------------------------------------------------------------------
  5906. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5907. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5908. {
  5909. if (WinConfiguration->DDWarnLackOfTempSpace)
  5910. {
  5911. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5912. if (!ADrive.IsEmpty())
  5913. {
  5914. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5915. DebugAssert(RequiredSpace >= 0);
  5916. __int64 RequiredWithReserve;
  5917. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5918. if (FreeSpace < RequiredWithReserve)
  5919. {
  5920. unsigned int Result;
  5921. TMessageParams Params(mpNeverAskAgainCheck);
  5922. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5923. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5924. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5925. if (Result == qaNeverAskAgain)
  5926. {
  5927. WinConfiguration->DDWarnLackOfTempSpace = false;
  5928. }
  5929. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5930. }
  5931. }
  5932. }
  5933. }
  5934. //---------------------------------------------------------------------------
  5935. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5936. {
  5937. DoOpenDirectoryDialog(odAddBookmark, Side);
  5938. }
  5939. //---------------------------------------------------------------------------
  5940. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5941. {
  5942. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5943. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  5944. try
  5945. {
  5946. TCustomDirView * DView = DirView(Side);
  5947. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5948. {
  5949. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5950. }
  5951. }
  5952. catch (...)
  5953. {
  5954. delete VisitedDirectories;
  5955. throw;
  5956. }
  5957. return VisitedDirectories;
  5958. }
  5959. //---------------------------------------------------------------------------
  5960. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5961. TOpenDirectoryMode Mode, TOperationSide Side)
  5962. {
  5963. bool Continue = true;
  5964. if (Mode == odAddBookmark)
  5965. {
  5966. TMessageParams Params(mpNeverAskAgainCheck);
  5967. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5968. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5969. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  5970. unsigned int Answer =
  5971. MessageDialog(Message,
  5972. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  5973. if (Answer == qaNeverAskAgain)
  5974. {
  5975. Continue = true;
  5976. WinConfiguration->UseSharedBookmarks = true;
  5977. }
  5978. else if (Answer == qaOK)
  5979. {
  5980. Continue = true;
  5981. WinConfiguration->UseSharedBookmarks = false;
  5982. }
  5983. else
  5984. {
  5985. Continue = false;
  5986. }
  5987. }
  5988. if (Continue)
  5989. {
  5990. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  5991. try
  5992. {
  5993. UnicodeString Name = DirView(Side)->PathName;
  5994. bool Repeat;
  5995. do
  5996. {
  5997. Repeat = false;
  5998. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  5999. // do not allow switching to location profiles,
  6000. // if we are not connected
  6001. HasDirView[osLocal] && (Terminal != NULL)))
  6002. {
  6003. TWindowLock Lock(this);
  6004. if (!TryOpenDirectory(Side, Name))
  6005. {
  6006. Repeat = true;
  6007. Mode = odBrowse;
  6008. }
  6009. }
  6010. }
  6011. while (Repeat);
  6012. }
  6013. __finally
  6014. {
  6015. delete VisitedDirectories;
  6016. }
  6017. }
  6018. }
  6019. //---------------------------------------------------------------------------
  6020. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6021. {
  6022. DebugAssert(!FTerminal->CommandSessionOpened);
  6023. return TTerminalManager::Instance()->ConnectTerminal(FTerminal->CommandSession);
  6024. }
  6025. //---------------------------------------------------------------------------
  6026. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6027. {
  6028. bool Result = FTerminal->IsCapable[Capability] ||
  6029. FTerminal->CommandSessionOpened;
  6030. if (!Result)
  6031. {
  6032. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  6033. if (!GUIConfiguration->ConfirmCommandSession)
  6034. {
  6035. Result = true;
  6036. }
  6037. else
  6038. {
  6039. TMessageParams Params(mpNeverAskAgainCheck);
  6040. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6041. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6042. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6043. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6044. if (Answer == qaNeverAskAgain)
  6045. {
  6046. GUIConfiguration->ConfirmCommandSession = false;
  6047. Result = true;
  6048. }
  6049. else if (Answer == qaOK)
  6050. {
  6051. Result = true;
  6052. }
  6053. }
  6054. if (Result)
  6055. {
  6056. Result = CommandSessionFallback();
  6057. }
  6058. }
  6059. return Result;
  6060. }
  6061. //---------------------------------------------------------------------------
  6062. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6063. {
  6064. if (EnsureCommandSessionFallback(fcAnyCommand))
  6065. {
  6066. DoConsoleDialog(Terminal, Command);
  6067. }
  6068. }
  6069. //---------------------------------------------------------------------------
  6070. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6071. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6072. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6073. {
  6074. if (!IsSideLocalBrowser(osRemote) &&
  6075. IsFileControl(DropSourceControl, osRemote) &&
  6076. (FDDExtMapFile != NULL))
  6077. {
  6078. Accept = true;
  6079. }
  6080. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6081. DebugAssert(FDDTargetControl != NULL);
  6082. }
  6083. //---------------------------------------------------------------------------
  6084. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6085. _di_IDataObject DataObj, int KeyState,
  6086. const TPoint & Point, int & Effect, bool & Accept)
  6087. {
  6088. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6089. }
  6090. //---------------------------------------------------------------------------
  6091. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6092. _di_IDataObject DataObj, int KeyState,
  6093. const TPoint & Point, int & Effect, bool & Accept)
  6094. {
  6095. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6096. }
  6097. //---------------------------------------------------------------------------
  6098. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6099. TObject * Sender)
  6100. {
  6101. DebugUsedParam(Sender);
  6102. DebugAssert(FDDTargetControl == Sender);
  6103. FDDTargetControl = NULL;
  6104. }
  6105. //---------------------------------------------------------------------------
  6106. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  6107. {
  6108. FileControlDDDragLeave(SessionsPageControl);
  6109. }
  6110. //---------------------------------------------------------------------------
  6111. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  6112. {
  6113. FileControlDDDragLeave(QueueView3);
  6114. }
  6115. //---------------------------------------------------------------------------
  6116. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6117. {
  6118. DebugAssert(!IsSideLocalBrowser(Side));
  6119. DebugUsedParam(Side);
  6120. bool Edit = false;
  6121. TRemoteFile * File = NULL;
  6122. UnicodeString FileName;
  6123. UnicodeString PointTo;
  6124. bool SymbolicLink = true;
  6125. if (RemoteDirView->ItemFocused)
  6126. {
  6127. DebugAssert(RemoteDirView->ItemFocused->Data);
  6128. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6129. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6130. if (Edit)
  6131. {
  6132. FileName = File->FileName;
  6133. PointTo = File->LinkTo;
  6134. }
  6135. else
  6136. {
  6137. PointTo = File->FileName;
  6138. }
  6139. }
  6140. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6141. Terminal->IsCapable[fcHardLink]))
  6142. {
  6143. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6144. if (Edit)
  6145. {
  6146. DebugAssert(File->FileName == FileName);
  6147. int Params = dfNoRecursive;
  6148. Terminal->ExceptionOnFail = true;
  6149. try
  6150. {
  6151. Terminal->DeleteFile(L"", File, &Params);
  6152. }
  6153. __finally
  6154. {
  6155. Terminal->ExceptionOnFail = false;
  6156. }
  6157. }
  6158. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6159. }
  6160. }
  6161. //---------------------------------------------------------------------------
  6162. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6163. {
  6164. return
  6165. (IsSideLocalBrowser(Side) ||
  6166. (HasActiveTerminal() &&
  6167. Terminal->ResolvingSymlinks &&
  6168. Terminal->IsCapable[fcSymbolicLink]));
  6169. }
  6170. //---------------------------------------------------------------------------
  6171. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6172. {
  6173. return
  6174. !IsSideLocalBrowser(FCurrentSide) &&
  6175. (RemoteDirView->ItemFocused != NULL) &&
  6176. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6177. Terminal->SessionData->ResolveSymlinks;
  6178. }
  6179. //---------------------------------------------------------------------------
  6180. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6181. {
  6182. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6183. FForceExecution = true;
  6184. try
  6185. {
  6186. DirView(osCurrent)->ExecuteCurrentFile();
  6187. }
  6188. __finally
  6189. {
  6190. FForceExecution = false;
  6191. }
  6192. }
  6193. //---------------------------------------------------------------------------
  6194. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6195. {
  6196. TEditorData ExternalEditor;
  6197. ExternalEditor.Editor = edExternal;
  6198. bool Remember = false;
  6199. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6200. {
  6201. if (Remember)
  6202. {
  6203. TEditorList * EditorList = new TEditorList;
  6204. try
  6205. {
  6206. *EditorList = *WinConfiguration->EditorList;
  6207. bool Found = false;
  6208. int i = 0;
  6209. while (!Found && (i < EditorList->Count))
  6210. {
  6211. const TEditorPreferences * Editor = EditorList->Editors[i];
  6212. if ((Editor->Data->Editor == edExternal) &&
  6213. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6214. {
  6215. Found = true;
  6216. }
  6217. i++;
  6218. }
  6219. if (!Found)
  6220. {
  6221. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6222. WinConfiguration->EditorList = EditorList;
  6223. }
  6224. }
  6225. __finally
  6226. {
  6227. delete EditorList;
  6228. }
  6229. }
  6230. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6231. }
  6232. }
  6233. //---------------------------------------------------------------------------
  6234. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6235. {
  6236. FEditorManager->ProcessFiles(FileTerminalRemoved, ATerminal);
  6237. if (FFileFindTerminal == ATerminal)
  6238. {
  6239. FFileFindTerminal = NULL;
  6240. HideFileFindDialog();
  6241. }
  6242. if (FClipboardTerminal == ATerminal)
  6243. {
  6244. ClipboardClear(); // implies ClipboardStop
  6245. }
  6246. if ((FOnSynchronizeAbort != NULL) &&
  6247. (ATerminal == FTerminal))
  6248. {
  6249. FOnSynchronizeAbort(NULL);
  6250. }
  6251. }
  6252. //---------------------------------------------------------------------------
  6253. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6254. {
  6255. DetachTerminal(Sender);
  6256. }
  6257. //---------------------------------------------------------------------------
  6258. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6259. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6260. {
  6261. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6262. DebugAssert(Terminal != NULL);
  6263. if (Data->Terminal == Terminal)
  6264. {
  6265. Data->Terminal = NULL;
  6266. }
  6267. }
  6268. //---------------------------------------------------------------------------
  6269. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  6270. {
  6271. UpdateControls();
  6272. SessionColor = TColor(0);
  6273. UpdateRemotePathComboBox(false);
  6274. NeedSession(false);
  6275. }
  6276. //---------------------------------------------------------------------------
  6277. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6278. {
  6279. try
  6280. {
  6281. // Cache, as the login dialog can change its value
  6282. // See also DoShow. Also see TTerminalManager::NewSession.
  6283. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6284. try
  6285. {
  6286. if (ShowLogin)
  6287. {
  6288. bool ReloadSessions = !Startup;
  6289. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this);
  6290. }
  6291. else if (Startup && WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  6292. // This detects if workspace was saved the last time the main widow was closed
  6293. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  6294. {
  6295. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false, true);
  6296. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  6297. }
  6298. }
  6299. __finally
  6300. {
  6301. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6302. // (so there was no chance for the user to open any session yet)
  6303. if ((ShowLogin || !Startup) &&
  6304. !WinConfiguration->KeepOpenWhenNoSession &&
  6305. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6306. {
  6307. TerminateApplication();
  6308. }
  6309. }
  6310. }
  6311. catch (EAbort &)
  6312. {
  6313. // swallow
  6314. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6315. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6316. }
  6317. }
  6318. //---------------------------------------------------------------------------
  6319. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged()
  6320. {
  6321. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  6322. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6323. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  6324. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6325. try
  6326. {
  6327. while ((SessionsPageControl->PageCount > TerminalList->Count + 1) ||
  6328. // Clear the design time unthemed tab
  6329. ((SessionsPageControl->PageCount > 0) &&
  6330. (dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[SessionsPageControl->PageCount - 1]) == NULL)))
  6331. {
  6332. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6333. }
  6334. for (int Index = 0; Index <= TerminalList->Count; Index++)
  6335. {
  6336. TThemeTabSheet * TabSheet;
  6337. if (Index >= SessionsPageControl->PageCount)
  6338. {
  6339. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6340. TabSheet->PageControl = SessionsPageControl;
  6341. }
  6342. else
  6343. {
  6344. TabSheet = DebugNotNull(dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[Index]));
  6345. }
  6346. bool IsSessionTab = (Index < TerminalList->Count);
  6347. if (IsSessionTab)
  6348. {
  6349. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  6350. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6351. UpdateSessionTab(TabSheet);
  6352. }
  6353. else
  6354. {
  6355. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  6356. TabSheet->Tag = 0; // not really needed
  6357. TabSheet->Shadowed = false;
  6358. // We know that we are at the last page, otherwise we could not call this (it assumes that new session tab is the last one)
  6359. UpdateNewSessionTab();
  6360. }
  6361. TabSheet->ShowCloseButton = IsSessionTab;
  6362. }
  6363. }
  6364. __finally
  6365. {
  6366. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6367. }
  6368. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  6369. }
  6370. //---------------------------------------------------------------------------
  6371. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  6372. {
  6373. DoTerminalListChanged();
  6374. }
  6375. //---------------------------------------------------------------------------
  6376. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  6377. {
  6378. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6379. DebugAssert(TabSheet->ImageIndex == 0);
  6380. TabSheet->Caption =
  6381. WinConfiguration->SelectiveToolbarText ?
  6382. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  6383. UnicodeString();
  6384. }
  6385. //---------------------------------------------------------------------------
  6386. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  6387. {
  6388. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6389. }
  6390. //---------------------------------------------------------------------------
  6391. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  6392. {
  6393. if (DebugAlwaysTrue(TabSheet != NULL))
  6394. {
  6395. TManagedTerminal * ATerminal = GetSessionTabTerminal(TabSheet);
  6396. if (DebugAlwaysTrue(ATerminal != NULL))
  6397. {
  6398. TColor Color = (ATerminal == FTerminal) ? FSessionColor : ATerminal->StateData->Color;
  6399. TabSheet->ImageIndex = AddSessionColor(Color);
  6400. UnicodeString TabCaption = TTerminalManager::Instance()->GetTerminalTitle(ATerminal, true);
  6401. TabSheet->Caption = SessionsPageControl->FormatCaptionWithCloseButton(TabCaption);
  6402. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  6403. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  6404. {
  6405. ThemeTabSheet->Shadowed = !ATerminal->Active;
  6406. }
  6407. }
  6408. }
  6409. }
  6410. //---------------------------------------------------------------------------
  6411. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6412. {
  6413. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6414. TManagedTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  6415. bool Result = (Terminal != NULL);
  6416. if (Result)
  6417. {
  6418. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6419. }
  6420. else
  6421. {
  6422. try
  6423. {
  6424. NewSession();
  6425. }
  6426. __finally
  6427. {
  6428. DoTerminalListChanged();
  6429. }
  6430. FSessionsPageControlNewSessionTime = Now();
  6431. }
  6432. return Result;
  6433. }
  6434. //---------------------------------------------------------------------------
  6435. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6436. {
  6437. SessionTabSwitched();
  6438. }
  6439. //---------------------------------------------------------------------------
  6440. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6441. {
  6442. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6443. DebugAssert(TransferList != NULL);
  6444. UnicodeString Name;
  6445. if (TransferList->ItemIndex <= 0)
  6446. {
  6447. Name = L"";
  6448. }
  6449. else
  6450. {
  6451. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6452. }
  6453. if (FCopyParamAutoSelected.IsEmpty())
  6454. {
  6455. // if previous preset was not autoselected, make new preset the "default"
  6456. FCopyParamDefault = Name;
  6457. }
  6458. GUIConfiguration->CopyParamCurrent = Name;
  6459. }
  6460. //---------------------------------------------------------------------------
  6461. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6462. {
  6463. // sanity check
  6464. bool ExistingPreset =
  6465. (FTransferListHoverIndex >= 0) &&
  6466. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6467. DebugAssert(ExistingPreset);
  6468. if (ExistingPreset)
  6469. {
  6470. HDC DC = GetDC(0);
  6471. TCanvas * Canvas = new TCanvas();
  6472. try
  6473. {
  6474. Canvas->Handle = DC;
  6475. Canvas->Font = GetToolbarFont(this);
  6476. UnicodeString Name;
  6477. if (FTransferListHoverIndex == 0)
  6478. {
  6479. Name = L"";
  6480. }
  6481. else
  6482. {
  6483. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6484. }
  6485. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6486. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6487. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6488. static_cast<TObject*>(GetComponent(fcTransferList)));
  6489. UnicodeString InfoStr =
  6490. GUIConfiguration->CopyParamPreset[Name].
  6491. GetInfoStr(L"; ",
  6492. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  6493. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6494. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6495. {
  6496. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6497. {
  6498. InfoStr.SetLength(InfoStr.Length() - 1);
  6499. }
  6500. InfoStr += Ellipsis;
  6501. }
  6502. // UpdateCaption does not cause invalidation of whole submenu, while
  6503. // setting Caption property does.
  6504. // also it probably does not resize the label, even if necessary
  6505. // (we do not want that anyway)
  6506. TransferLabel->UpdateCaption(InfoStr);
  6507. }
  6508. __finally
  6509. {
  6510. Canvas->Handle = NULL;
  6511. ReleaseDC(0, DC);
  6512. delete Canvas;
  6513. }
  6514. }
  6515. }
  6516. //---------------------------------------------------------------------------
  6517. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6518. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6519. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6520. {
  6521. if (FTransferListHoverIndex != AHoverIndex)
  6522. {
  6523. FTransferListHoverIndex = AHoverIndex;
  6524. UpdateTransferLabel();
  6525. }
  6526. }
  6527. //---------------------------------------------------------------------------
  6528. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6529. {
  6530. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6531. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6532. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6533. {
  6534. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6535. {
  6536. if (!NonVisualDataModule->Busy)
  6537. {
  6538. OpenDirectory(GetSide(osCurrent));
  6539. }
  6540. Message.Result = 1;
  6541. }
  6542. else
  6543. {
  6544. TForm::Dispatch(&Message);
  6545. }
  6546. }
  6547. else
  6548. {
  6549. TForm::Dispatch(&Message);
  6550. }
  6551. }
  6552. //---------------------------------------------------------------------------
  6553. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6554. {
  6555. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6556. if ((FIgnoreNextDialogChar != 0) &&
  6557. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6558. {
  6559. Message.Result = 1;
  6560. }
  6561. else
  6562. {
  6563. TForm::Dispatch(&Message);
  6564. }
  6565. FIgnoreNextDialogChar = 0;
  6566. }
  6567. //---------------------------------------------------------------------------
  6568. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6569. {
  6570. // The four low-order bits are used internally by Windows
  6571. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6572. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6573. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6574. // 61730 - restore thru double click - undocumented
  6575. // 61490 - maximize thru double click - undocumented
  6576. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6577. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6578. {
  6579. SysResizing(Cmd);
  6580. }
  6581. TForm::Dispatch(&Message);
  6582. }
  6583. //---------------------------------------------------------------------------
  6584. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6585. {
  6586. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6587. // Also there no point returning TRUE as we are not able to
  6588. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6589. // Hence the process termination might be safer :)
  6590. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6591. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6592. {
  6593. Message.Result = FALSE;
  6594. }
  6595. else
  6596. {
  6597. Message.Result = TRUE;
  6598. }
  6599. // Do not call default handling as that triggers OnCloseQuery,
  6600. // where our implementation will popup confirmation dialogs, what we do not want,
  6601. // as per Vista guidelines:
  6602. // https://docs.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6603. }
  6604. //---------------------------------------------------------------------------
  6605. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6606. {
  6607. if (Message.EndSession && IsApplicationMinimized())
  6608. {
  6609. // WORKAROUND
  6610. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6611. // when it receives WM_ENDSESSION,
  6612. // but that sometimes (particularly when application is minimized) cause crashes.
  6613. // Crash popups message that blocks log off.
  6614. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6615. // so we call ExitProcess() immediately, not even trying to cleanup.
  6616. // It still causes beep, so there's likely some popup, but it does not block
  6617. // log off.
  6618. ExitProcess(0);
  6619. }
  6620. TForm::Dispatch(&Message);
  6621. }
  6622. #ifdef _DEBUG
  6623. //---------------------------------------------------------------------------
  6624. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6625. {
  6626. TForm::Dispatch(&Message);
  6627. }
  6628. #endif
  6629. //---------------------------------------------------------------------------
  6630. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6631. {
  6632. }
  6633. //---------------------------------------------------------------------------
  6634. void __fastcall TCustomScpExplorerForm::DoShow()
  6635. {
  6636. // only now are the controls resized finally, so the size constraints
  6637. // will not conflict with possibly very small window size
  6638. RestoreFormParams();
  6639. // This indicates that the form was rescaled after parameters were loaded and
  6640. // as most probably the parameters were actually saved with the current form scaling
  6641. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6642. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6643. // and no rounding problems will occur.
  6644. // See also RestoreForm.
  6645. // (we should be safe to call RestoreParams unconditionally)
  6646. if (PixelsPerInch != Screen->PixelsPerInch)
  6647. {
  6648. RestoreParams();
  6649. }
  6650. FixControlsPlacement();
  6651. if (Position == poDefaultPosOnly)
  6652. {
  6653. CutFormToDesktop(this);
  6654. }
  6655. TForm::DoShow();
  6656. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6657. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6658. if (Terminal == NULL)
  6659. {
  6660. StartingDisconnected();
  6661. }
  6662. UpdatePixelsPerInchMainWindowCounter();
  6663. FShowing = true;
  6664. }
  6665. //---------------------------------------------------------------------------
  6666. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6667. {
  6668. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6669. }
  6670. //---------------------------------------------------------------------------
  6671. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  6672. {
  6673. DoTerminalListChanged();
  6674. }
  6675. //---------------------------------------------------------------------------
  6676. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6677. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6678. TNotifyEvent OnBalloonClick, TObject * UserData)
  6679. {
  6680. bool Do;
  6681. UnicodeString Message;
  6682. if (E == NULL)
  6683. {
  6684. Message = Str;
  6685. Do = true;
  6686. }
  6687. else
  6688. {
  6689. Do = ExceptionMessage(E, Message);
  6690. }
  6691. if (Do && WinConfiguration->BalloonNotifications)
  6692. {
  6693. UnicodeString Title;
  6694. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6695. {
  6696. Message = TrimLeft(Message);
  6697. }
  6698. else
  6699. {
  6700. Message = UnformatMessage(Message);
  6701. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6702. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6703. Title = LoadResourceString(Captions[Type]);
  6704. if (Terminal != NULL)
  6705. {
  6706. Title = FORMAT(L"%s - %s",
  6707. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), Title));
  6708. }
  6709. }
  6710. if (Seconds == 0)
  6711. {
  6712. Seconds = WinConfiguration->NotificationsTimeout;
  6713. }
  6714. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6715. }
  6716. }
  6717. //---------------------------------------------------------------------------
  6718. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6719. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6720. UnicodeString HelpKeyword, const TMessageParams * Params,
  6721. TTerminal * Terminal)
  6722. {
  6723. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6724. (FOnFeedSynchronizeError != NULL)) &&
  6725. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6726. {
  6727. if (FOnFeedSynchronizeError != NULL)
  6728. {
  6729. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6730. }
  6731. else
  6732. {
  6733. DebugAssert(FErrorList != NULL);
  6734. TStringList * MoreMessagesCopy = NULL;
  6735. if (MoreMessages)
  6736. {
  6737. MoreMessagesCopy = new TStringList();
  6738. MoreMessagesCopy->Assign(MoreMessages);
  6739. }
  6740. FErrorList->AddObject(Message, MoreMessagesCopy);
  6741. }
  6742. PopupTrayBalloon(Terminal, Message, Type);
  6743. return ContinueAnswer(Answers);
  6744. }
  6745. else
  6746. {
  6747. bool UseBalloon = FTrayIcon->Visible;
  6748. if (UseBalloon)
  6749. {
  6750. PopupTrayBalloon(Terminal, Message, Type);
  6751. }
  6752. unsigned int Result;
  6753. try
  6754. {
  6755. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6756. }
  6757. __finally
  6758. {
  6759. // cancel only balloon we popped up, otherwise we may cancel notification
  6760. // balloon that was there before the message dialog
  6761. // (such as "dd confirmation opt in balloon)
  6762. if (UseBalloon)
  6763. {
  6764. FTrayIcon->CancelBalloon();
  6765. }
  6766. }
  6767. return Result;
  6768. }
  6769. }
  6770. //---------------------------------------------------------------------------
  6771. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6772. TTerminal * Terminal, Exception * E)
  6773. {
  6774. if (FTrayIcon->Visible)
  6775. {
  6776. PopupTrayBalloon(Terminal, L"", qtError, E);
  6777. }
  6778. // particularly prevent opening new session from jump list,
  6779. // while exception is shown
  6780. NonVisualDataModule->StartBusy();
  6781. try
  6782. {
  6783. ShowExtendedExceptionEx(Terminal, E);
  6784. }
  6785. __finally
  6786. {
  6787. NonVisualDataModule->EndBusy();
  6788. FTrayIcon->CancelBalloon();
  6789. }
  6790. }
  6791. //---------------------------------------------------------------------------
  6792. void __fastcall TCustomScpExplorerForm::TerminalReady()
  6793. {
  6794. if (Terminal->Active)
  6795. {
  6796. InitStatusBar();
  6797. }
  6798. // cannot rely on active page being page for active terminal,
  6799. // as it can happen that active page is the "new session" page
  6800. // (e.g. when reconnecting active terminal, while login dialog
  6801. // invoked from "new session" page is modal)
  6802. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6803. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  6804. }
  6805. //---------------------------------------------------------------------------
  6806. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6807. TTerminal * Terminal, Exception * E)
  6808. {
  6809. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6810. if (!Terminal->Active)
  6811. {
  6812. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6813. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6814. {
  6815. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6816. }
  6817. }
  6818. }
  6819. //---------------------------------------------------------------------------
  6820. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6821. UnicodeString Message, TQueryType Type,
  6822. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6823. {
  6824. if ((E == NULL) ||
  6825. ExceptionMessage(E, Message))
  6826. {
  6827. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6828. if (Important)
  6829. {
  6830. Seconds *= 5;
  6831. }
  6832. UnicodeString NoteMessage(UnformatMessage(Message));
  6833. if (Terminal != NULL)
  6834. {
  6835. NoteMessage = FORMAT(L"%s: %s",
  6836. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), NoteMessage));
  6837. }
  6838. if (WinConfiguration->BalloonNotifications)
  6839. {
  6840. AddNote(NoteMessage);
  6841. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6842. }
  6843. else
  6844. {
  6845. FlashOnBackground();
  6846. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6847. }
  6848. }
  6849. }
  6850. //---------------------------------------------------------------------------
  6851. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6852. {
  6853. RestoreApp();
  6854. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6855. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6856. !NonVisualDataModule->Busy)
  6857. {
  6858. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  6859. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6860. if (!ComponentVisible[fcQueueView])
  6861. {
  6862. ToggleQueueVisibility();
  6863. GoToQueue();
  6864. }
  6865. }
  6866. }
  6867. //---------------------------------------------------------------------------
  6868. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  6869. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6870. {
  6871. UnicodeString Message;
  6872. TNotifyEvent OnClick = NULL;
  6873. TObject * UserData = NULL;
  6874. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6875. switch (Event)
  6876. {
  6877. case qeEmptyButMonitored:
  6878. if ((ATerminal != Terminal) || QueueInvisible)
  6879. {
  6880. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6881. OnClick = QueueEmptyNoteClicked;
  6882. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6883. TerminalNoteData->Terminal = ATerminal;
  6884. UserData = TerminalNoteData;
  6885. }
  6886. break;
  6887. case qeEmpty:
  6888. OperationComplete(ATerminal->QueueOperationStart);
  6889. break;
  6890. case qePendingUserAction:
  6891. if ((ATerminal != Terminal) ||
  6892. (QueueInvisible && !IsQueueAutoPopup()))
  6893. {
  6894. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6895. }
  6896. break;
  6897. default:
  6898. DebugFail();
  6899. }
  6900. if (!Message.IsEmpty())
  6901. {
  6902. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6903. }
  6904. }
  6905. //---------------------------------------------------------------------------
  6906. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6907. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6908. {
  6909. if (FDDExtMapFile != NULL)
  6910. {
  6911. CloseHandle(FDDExtMapFile);
  6912. FDDExtMapFile = NULL;
  6913. }
  6914. if (WinConfiguration->DDFakeFile)
  6915. {
  6916. DDFakeFileInitDrag(FileList, Created);
  6917. }
  6918. }
  6919. //---------------------------------------------------------------------------
  6920. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  6921. {
  6922. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  6923. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  6924. {
  6925. FFakeFileDropTarget = FileName;
  6926. }
  6927. }
  6928. //---------------------------------------------------------------------------
  6929. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  6930. {
  6931. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6932. if (!ForceDirectories(ApiPath(Result)))
  6933. {
  6934. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  6935. }
  6936. FileSetAttr(ApiPath(Result), faHidden);
  6937. return Result;
  6938. }
  6939. //---------------------------------------------------------------------------
  6940. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  6941. bool & Created)
  6942. {
  6943. FFakeFileDropTarget = UnicodeString();
  6944. FDragFakeDirectory = CreateFakeTransferDirectory();
  6945. FileList->AddItem(NULL, FDragFakeDirectory);
  6946. Created = true;
  6947. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  6948. {
  6949. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  6950. }
  6951. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6952. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6953. {
  6954. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6955. TDragExtCommStruct* CommStruct;
  6956. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6957. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6958. DebugAssert(CommStruct != NULL);
  6959. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  6960. CommStruct->Dragging = true;
  6961. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  6962. LENOF(CommStruct->DropDest));
  6963. NULL_TERMINATE(CommStruct->DropDest);
  6964. UnmapViewOfFile(CommStruct);
  6965. }
  6966. }
  6967. //---------------------------------------------------------------------------
  6968. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  6969. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  6970. {
  6971. bool Result;
  6972. DebugAssert(!IsLocalBrowserMode());
  6973. if (Sender == RemoteDirView)
  6974. {
  6975. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  6976. }
  6977. else
  6978. {
  6979. DebugAssert(Sender == RemoteDriveView);
  6980. TStrings * FileList = RemoteDriveView->DragFileList();
  6981. try
  6982. {
  6983. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  6984. }
  6985. __finally
  6986. {
  6987. delete FileList;
  6988. }
  6989. }
  6990. if (FDDTargetControl == RemoteDriveView)
  6991. {
  6992. RemoteDriveView->UpdateDropTarget();
  6993. }
  6994. // foRemoteMove happens when file/dir is dragged within the remote tree view
  6995. // or from tree view to dir view.
  6996. // foMove happens when file/dir is dragged from remote tree view outside of
  6997. // application via dragex
  6998. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  6999. (DropSourceControl == RemoteDriveView))
  7000. {
  7001. RemoteDriveView->UpdateDropSource();
  7002. }
  7003. return Result;
  7004. }
  7005. //---------------------------------------------------------------------------
  7006. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  7007. {
  7008. DebugAssert(!IsLocalBrowserMode());
  7009. // This also handles drops of remote files to queue.
  7010. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  7011. SAFE_DESTROY(FDDFileList);
  7012. if (!FFakeFileDropTarget.IsEmpty())
  7013. {
  7014. RemoveDir(ApiPath(FFakeFileDropTarget));
  7015. }
  7016. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7017. {
  7018. try
  7019. {
  7020. TDragResult DDResult = (Sender == RemoteDirView) ?
  7021. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7022. // Focus is moved to the target application,
  7023. // but as we are going to present the UI, we need to steal the focus back.
  7024. // This most likely won't work though (windows does not allow application
  7025. // to steal focus most of the time)
  7026. Application->BringToFront();
  7027. // On older version of Windows we never got drMove here, see also comment below.
  7028. // On Windows 10, we get the "move".
  7029. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7030. {
  7031. UnicodeString TargetDirectory;
  7032. TFileOperation Operation;
  7033. // drInvalid may mean drMove, see comment below
  7034. switch (DDResult)
  7035. {
  7036. case drCopy:
  7037. Operation = foCopy;
  7038. break;
  7039. case drMove:
  7040. Operation = foMove;
  7041. break;
  7042. default:
  7043. DebugFail();
  7044. case drInvalid:
  7045. // prefer "copy" for safety
  7046. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7047. break;
  7048. }
  7049. TTransferOperationParam Param;
  7050. UnicodeString CounterName;
  7051. bool ForceQueue;
  7052. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7053. {
  7054. // we get drInvalid both if d&d was intercepted by ddext,
  7055. // and when users drops on no-drop location.
  7056. // we tell the difference by existence of response from ddext,
  7057. // so we ignore absence of response in this case
  7058. if (DDResult != drInvalid)
  7059. {
  7060. // here we know that the extension is installed,
  7061. // as it is checked as soon as drag&drop starts from
  7062. // RemoteFileControlDDCreateDragFileList
  7063. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7064. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7065. {
  7066. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7067. }
  7068. else
  7069. {
  7070. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7071. }
  7072. }
  7073. }
  7074. else
  7075. {
  7076. // download using fake file
  7077. Param.Temp = false;
  7078. Param.DragDrop = true;
  7079. if (ForceQueue)
  7080. {
  7081. Param.Queue = asOn;
  7082. }
  7083. if (Sender == RemoteDirView)
  7084. {
  7085. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7086. }
  7087. if (RemoteFileControlFileOperation(Sender, Operation,
  7088. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7089. {
  7090. Configuration->Usage->Inc(CounterName);
  7091. }
  7092. }
  7093. }
  7094. }
  7095. __finally
  7096. {
  7097. CloseHandle(FDDExtMapFile);
  7098. FDDExtMapFile = NULL;
  7099. delete FDragFakeMonitors;
  7100. FDragFakeMonitors = NULL;
  7101. RemoveDir(ApiPath(FDragFakeDirectory));
  7102. FDragFakeDirectory = L"";
  7103. FFakeFileDropTarget = UnicodeString();
  7104. }
  7105. }
  7106. if (FDragDropSshTerminate.get() != NULL)
  7107. {
  7108. FDragDropSshTerminate->Rethrow();
  7109. }
  7110. }
  7111. //---------------------------------------------------------------------------
  7112. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7113. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7114. {
  7115. // Remember drop effect so we know (when user drops files), if we copy or move
  7116. FLastDropEffect = dwEffect;
  7117. }
  7118. //---------------------------------------------------------------------------
  7119. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7120. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7121. {
  7122. bool Result;
  7123. if (FDDTargetControl == QueueView3)
  7124. {
  7125. Directory = DefaultDownloadTargetDirectory();
  7126. Result = true;
  7127. CounterName = L"DownloadsDragDropQueue";
  7128. ForceQueue = true;
  7129. }
  7130. else if (!FFakeFileDropTarget.IsEmpty())
  7131. {
  7132. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7133. Result = true;
  7134. ForceQueue = false;
  7135. CounterName = L"DownloadsDragDropFakeFile";
  7136. }
  7137. else
  7138. {
  7139. ForceQueue = false;
  7140. Enabled = false;
  7141. try
  7142. {
  7143. int Timer = 0;
  7144. Result = false;
  7145. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7146. {
  7147. {
  7148. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7149. TDragExtCommStruct* CommStruct;
  7150. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7151. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7152. DebugAssert(CommStruct != NULL);
  7153. Result = !CommStruct->Dragging;
  7154. if (Result)
  7155. {
  7156. Directory = ExtractFilePath(CommStruct->DropDest);
  7157. CounterName = L"DownloadsDragDropExternalExt";
  7158. }
  7159. UnmapViewOfFile(CommStruct);
  7160. }
  7161. if (!Result)
  7162. {
  7163. Sleep(50);
  7164. Timer += 50;
  7165. Application->ProcessMessages();
  7166. }
  7167. }
  7168. }
  7169. __finally
  7170. {
  7171. Enabled = true;
  7172. }
  7173. }
  7174. return Result;
  7175. }
  7176. //---------------------------------------------------------------------------
  7177. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7178. const UnicodeString TempDir, int SecDelay)
  7179. {
  7180. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7181. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7182. if (FDelayedDeletionTimer == NULL)
  7183. {
  7184. DebugAssert(HandleAllocated());
  7185. FDelayedDeletionTimer = new TTimer(this);
  7186. FDelayedDeletionTimer->Interval = 10000;
  7187. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7188. }
  7189. else
  7190. {
  7191. FDelayedDeletionTimer->Enabled = true;
  7192. }
  7193. }
  7194. //---------------------------------------------------------------------------
  7195. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7196. {
  7197. DebugAssert(FDelayedDeletionList != NULL);
  7198. TDateTime N = Now();
  7199. TDateTime Alert;
  7200. UnicodeString Directory;
  7201. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7202. {
  7203. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7204. if ((N >= Alert) || (Sender == NULL))
  7205. {
  7206. Directory = FDelayedDeletionList->Strings[Index];
  7207. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7208. {
  7209. FDelayedDeletionList->Delete(Index);
  7210. }
  7211. }
  7212. }
  7213. if (FDelayedDeletionList->Count == 0)
  7214. {
  7215. FDelayedDeletionTimer->Enabled = false;
  7216. }
  7217. }
  7218. //---------------------------------------------------------------------------
  7219. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7220. {
  7221. DebugAssert(!IsLocalBrowserMode());
  7222. if (IsFileControl(FDDTargetControl, osRemote) ||
  7223. (FDDTargetControl == SessionsPageControl))
  7224. {
  7225. TTerminal * TargetTerminal = NULL;
  7226. TFileOperation Operation = foNone;
  7227. if (FDDTargetControl == SessionsPageControl)
  7228. {
  7229. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7230. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7231. // do not allow dropping on the "+" and disconnected tabs
  7232. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  7233. if (IsActiveTerminal(TargetTerminal))
  7234. {
  7235. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7236. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7237. {
  7238. Operation = foRemoteMove;
  7239. }
  7240. else
  7241. {
  7242. Operation = foRemoteCopy;
  7243. }
  7244. }
  7245. }
  7246. else
  7247. {
  7248. if (FLastDropEffect == DROPEFFECT_MOVE)
  7249. {
  7250. Operation = foRemoteMove;
  7251. }
  7252. else if (FLastDropEffect == DROPEFFECT_COPY)
  7253. {
  7254. Operation = foRemoteCopy;
  7255. }
  7256. }
  7257. if (Operation != foNone)
  7258. {
  7259. RemoteFileControlFileOperation(DropSourceControl,
  7260. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7261. }
  7262. // abort drag&drop
  7263. Abort();
  7264. }
  7265. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7266. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7267. (FDDTargetControl != QueueView3))
  7268. {
  7269. DebugAssert(!FDragTempDir.IsEmpty());
  7270. TTransferType Type;
  7271. UnicodeString TempDir = FDragTempDir;
  7272. // We clear FDragTempDir before calling
  7273. // just in case it fail (raises exception)
  7274. FDragTempDir = L"";
  7275. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7276. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7277. // empty directory parameter means temp directory -> don't display it!
  7278. UnicodeString TargetDir = L"";
  7279. int Options = 0;
  7280. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7281. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7282. {
  7283. Abort();
  7284. }
  7285. // TargetDir is set when dropped on local file control
  7286. // (this was workaround for legacy dirview event handling, now it should be
  7287. // made prettier)
  7288. if (TargetDir.IsEmpty())
  7289. {
  7290. TargetDir = TempDir;
  7291. if (ForceDirectories(ApiPath(TargetDir)))
  7292. {
  7293. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7294. FPendingTempSpaceWarn = true;
  7295. try
  7296. {
  7297. FDragDropOperation = true;
  7298. int Params = cpTemporary |
  7299. (Type == ttMove ? cpDelete : 0);
  7300. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7301. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7302. }
  7303. __finally
  7304. {
  7305. FDragDropOperation = false;
  7306. FPendingTempSpaceWarn = false;
  7307. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7308. }
  7309. }
  7310. else
  7311. {
  7312. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7313. }
  7314. }
  7315. }
  7316. }
  7317. //---------------------------------------------------------------------------
  7318. void __fastcall TCustomScpExplorerForm::DDDownload(
  7319. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  7320. {
  7321. DebugAssert(!IsLocalBrowserMode());
  7322. TAutoBatch AutoBatch(this);
  7323. UpdateCopyParamCounters(*CopyParam);
  7324. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7325. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7326. {
  7327. RemoteDriveView->UpdateDropSource();
  7328. }
  7329. }
  7330. //---------------------------------------------------------------------------
  7331. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7332. {
  7333. public:
  7334. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7335. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7336. {
  7337. }
  7338. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7339. {
  7340. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7341. TDataObjectFilesEx::AllowData(FormatEtc);
  7342. }
  7343. };
  7344. //---------------------------------------------------------------------------
  7345. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7346. TObject * Sender, TDataObject *& DataObject)
  7347. {
  7348. if (FDDExtMapFile != NULL)
  7349. {
  7350. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7351. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7352. {
  7353. Abort();
  7354. }
  7355. if (FileList->Count > 0)
  7356. {
  7357. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7358. if (!FilesObject->IsValid(true, true))
  7359. {
  7360. FilesObject->_Release();
  7361. }
  7362. else
  7363. {
  7364. DataObject = FilesObject;
  7365. }
  7366. }
  7367. }
  7368. }
  7369. //---------------------------------------------------------------------------
  7370. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7371. {
  7372. DebugFail();
  7373. }
  7374. //---------------------------------------------------------------------------
  7375. void __fastcall TCustomScpExplorerForm::GoToTree()
  7376. {
  7377. ComponentVisible[fcRemoteTree] = true;
  7378. DriveView(osRemote)->SetFocus();
  7379. }
  7380. //---------------------------------------------------------------------------
  7381. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7382. TPanelExport Export)
  7383. {
  7384. TCustomDirView * DirView = this->DirView(Side);
  7385. std::unique_ptr<TStrings> ExportData(new TStringList());
  7386. switch (Export)
  7387. {
  7388. case pePath:
  7389. ExportData->Add(DirView->PathName);
  7390. break;
  7391. case peFileList:
  7392. case peFullFileList:
  7393. {
  7394. bool FullPath = (Export == peFullFileList);
  7395. DirView->CreateFileList(false, FullPath, ExportData.get());
  7396. for (int Index = 0; Index < ExportData->Count; Index++)
  7397. {
  7398. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7399. {
  7400. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7401. }
  7402. }
  7403. }
  7404. break;
  7405. default:
  7406. DebugFail();
  7407. }
  7408. return ExportData.release();
  7409. }
  7410. //---------------------------------------------------------------------------
  7411. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7412. TPanelExport Export, TPanelExportDestination Destination)
  7413. {
  7414. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7415. PanelExportStore(Side, Export, Destination, ExportData.get());
  7416. }
  7417. //---------------------------------------------------------------------------
  7418. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7419. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7420. TStrings * ExportData)
  7421. {
  7422. if (Destination == pedClipboard)
  7423. {
  7424. TInstantOperationVisualizer Visualizer;
  7425. CopyToClipboard(ExportData);
  7426. }
  7427. else
  7428. {
  7429. DebugFail();
  7430. }
  7431. }
  7432. //---------------------------------------------------------------------------
  7433. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7434. {
  7435. TCustomDirView * DirView = this->DirView(Side);
  7436. UnicodeString Mask = DirView->Mask;
  7437. if (DoFilterMaskDialog(DirView, Mask))
  7438. {
  7439. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7440. Configuration->Usage->Inc(L"Filters");
  7441. }
  7442. }
  7443. //---------------------------------------------------------------------------
  7444. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7445. {
  7446. return FQueueController->DefaultOperation();
  7447. }
  7448. //---------------------------------------------------------------------------
  7449. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7450. TQueueOperation Operation, void ** Param)
  7451. {
  7452. switch (Operation)
  7453. {
  7454. case qoPreferences:
  7455. return true;
  7456. case qoGoTo:
  7457. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7458. case qoOnceEmpty:
  7459. return IsAnythingQueued();
  7460. default:
  7461. return FQueueController->AllowOperation(Operation, Param);
  7462. }
  7463. }
  7464. //---------------------------------------------------------------------------
  7465. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7466. {
  7467. if (DebugAlwaysTrue(QueueView3->Visible))
  7468. {
  7469. QueueView3->SetFocus();
  7470. }
  7471. }
  7472. //---------------------------------------------------------------------------
  7473. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7474. TQueueOperation Operation, void * Param)
  7475. {
  7476. if (Operation == qoGoTo)
  7477. {
  7478. GoToQueue();
  7479. }
  7480. else if (Operation == qoPreferences)
  7481. {
  7482. PreferencesDialog(pmQueue);
  7483. }
  7484. else
  7485. {
  7486. FQueueController->ExecuteOperation(Operation, Param);
  7487. }
  7488. }
  7489. //---------------------------------------------------------------------------
  7490. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7491. {
  7492. return (Queue != NULL) && Queue->Enabled;
  7493. }
  7494. //---------------------------------------------------------------------------
  7495. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7496. {
  7497. DebugAssert(Queue != NULL);
  7498. if (Queue != NULL)
  7499. {
  7500. Queue->Enabled = !Queue->Enabled;
  7501. }
  7502. }
  7503. //---------------------------------------------------------------------------
  7504. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7505. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7506. {
  7507. FQueueActedItem = QueueView3->ItemFocused;
  7508. }
  7509. //---------------------------------------------------------------------------
  7510. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7511. {
  7512. return TopDock->Height + QueueSplitter->Height;
  7513. }
  7514. //---------------------------------------------------------------------------
  7515. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  7516. {
  7517. return
  7518. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  7519. (QueueDock->Visible ? QueueDock->Height : 0) +
  7520. (QueueLabel->Visible ? QueueLabel->Height : 0);
  7521. }
  7522. //---------------------------------------------------------------------------
  7523. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  7524. {
  7525. return ScaleByTextHeight(this, 48);
  7526. }
  7527. //---------------------------------------------------------------------------
  7528. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7529. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7530. {
  7531. // when queue is hidden by double-clicking splitter, stray attempt to
  7532. // resize the panel with strange value arrives, make sure it is ignored
  7533. if (ComponentVisible[fcQueueView])
  7534. {
  7535. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  7536. if (NewSize > MaxHeightLimit)
  7537. {
  7538. NewSize = MaxHeightLimit;
  7539. }
  7540. int MinHeightLimit =
  7541. GetStaticQueuePanelComponentsHeight() +
  7542. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  7543. GetMinQueueViewHeight();
  7544. if (NewSize < MinHeightLimit)
  7545. {
  7546. NewSize = MinHeightLimit;
  7547. }
  7548. }
  7549. else
  7550. {
  7551. Accept = false;
  7552. }
  7553. }
  7554. //---------------------------------------------------------------------------
  7555. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  7556. {
  7557. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  7558. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  7559. if (QueueViewHeight < GetMinQueueViewHeight())
  7560. {
  7561. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  7562. }
  7563. }
  7564. //---------------------------------------------------------------------------
  7565. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7566. TDragObject *& /*DragObject*/)
  7567. {
  7568. FQueueActedItem = QueueView3->ItemFocused;
  7569. }
  7570. //---------------------------------------------------------------------------
  7571. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7572. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7573. {
  7574. Accept = true;
  7575. if (Source == QueueView3)
  7576. {
  7577. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7578. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7579. if (Accept)
  7580. {
  7581. TQueueItemProxy * QueueItem;
  7582. TQueueItemProxy * DestQueueItem;
  7583. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7584. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7585. Accept = (QueueItem != DestQueueItem) &&
  7586. (QueueItem->Status == TQueueItem::qsPending) &&
  7587. (DestQueueItem->Status == TQueueItem::qsPending);
  7588. }
  7589. }
  7590. }
  7591. //---------------------------------------------------------------------------
  7592. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7593. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7594. {
  7595. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7596. {
  7597. TQueueItemProxy * QueueItem;
  7598. TQueueItemProxy * DestQueueItem;
  7599. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7600. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7601. QueueItem->Move(DestQueueItem);
  7602. }
  7603. }
  7604. //---------------------------------------------------------------------------
  7605. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7606. {
  7607. if ((QueueView3->ItemFocused == NULL) &&
  7608. (QueueView3->Items->Count > 0))
  7609. {
  7610. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7611. }
  7612. QueueLabelUpdateStatus();
  7613. }
  7614. //---------------------------------------------------------------------------
  7615. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7616. {
  7617. QueueLabelUpdateStatus();
  7618. }
  7619. //---------------------------------------------------------------------------
  7620. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7621. {
  7622. QueueLabel->UpdateStatus();
  7623. }
  7624. //---------------------------------------------------------------------------
  7625. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7626. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7627. {
  7628. if (Selected)
  7629. {
  7630. NonVisualDataModule->UpdateNonVisibleActions();
  7631. }
  7632. }
  7633. //---------------------------------------------------------------------------
  7634. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7635. {
  7636. TDragDropFilesEx * Result = NULL;
  7637. if (Sender == SessionsPageControl)
  7638. {
  7639. Result = FSessionsDragDropFilesEx;
  7640. }
  7641. else if (Sender == QueueView3)
  7642. {
  7643. Result = FQueueDragDropFilesEx;
  7644. }
  7645. else
  7646. {
  7647. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7648. if (DirView != NULL)
  7649. {
  7650. Result = DirView->DragDropFilesEx;
  7651. }
  7652. else
  7653. {
  7654. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7655. if (DriveView != NULL)
  7656. {
  7657. Result = DriveView->DragDropFilesEx;
  7658. }
  7659. }
  7660. }
  7661. DebugAssert(Result != NULL);
  7662. return Result;
  7663. }
  7664. //---------------------------------------------------------------------------
  7665. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7666. {
  7667. return (DView->SelCount == DView->FilesCount);
  7668. }
  7669. //---------------------------------------------------------------------------
  7670. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7671. {
  7672. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7673. }
  7674. //---------------------------------------------------------------------------
  7675. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7676. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7677. {
  7678. DebugAssert(!IsLocalBrowserMode());
  7679. TFileOperation Operation;
  7680. switch (Effect)
  7681. {
  7682. case DROPEFFECT_MOVE:
  7683. Operation = foMove;
  7684. break;
  7685. case DROPEFFECT_COPY:
  7686. // occures on WinXP (reported by user)
  7687. default:
  7688. Operation = foCopy;
  7689. break;
  7690. };
  7691. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7692. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7693. if (DragDropFilesEx->FileList->Count > 0)
  7694. {
  7695. TStrings * FileList = new TStringList();
  7696. try
  7697. {
  7698. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7699. {
  7700. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7701. }
  7702. FDragDropOperation = true;
  7703. TTransferOperationParam Param;
  7704. Param.TargetDirectory = TargetPath;
  7705. // upload, no temp dirs
  7706. Param.Temp = false;
  7707. Param.DragDrop = DragDrop;
  7708. Param.Options =
  7709. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7710. if (ForceQueue)
  7711. {
  7712. Param.Queue = asOn;
  7713. }
  7714. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7715. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7716. {
  7717. if (IsFileControl(DropSourceControl, osLocal))
  7718. {
  7719. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7720. }
  7721. else
  7722. {
  7723. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7724. }
  7725. }
  7726. }
  7727. __finally
  7728. {
  7729. FDragDropOperation = false;
  7730. delete FileList;
  7731. }
  7732. }
  7733. }
  7734. //---------------------------------------------------------------------------
  7735. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7736. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7737. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7738. {
  7739. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7740. }
  7741. //---------------------------------------------------------------------------
  7742. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7743. TObject * Sender, int grfKeyState, int & dwEffect)
  7744. {
  7745. DebugAssert(!IsLocalBrowserMode());
  7746. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7747. if ((dwEffect != DROPEFFECT_NONE) &&
  7748. IsFileControl(DropSourceControl, osRemote))
  7749. {
  7750. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7751. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7752. (RemoteDirView->DropTarget == NULL))
  7753. {
  7754. dwEffect = DROPEFFECT_NONE;
  7755. }
  7756. else
  7757. {
  7758. if (dwEffect == DROPEFFECT_COPY)
  7759. {
  7760. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  7761. // currently we support copying always (at least via temporary directory);
  7762. // remove associated checks once this all proves stable and working
  7763. bool CopyCapable = true;
  7764. // if we do not support neither of operations, there's no discussion
  7765. if (!MoveCapable && !CopyCapable)
  7766. {
  7767. dwEffect = DROPEFFECT_NONE;
  7768. }
  7769. else
  7770. {
  7771. // The default effect inside remote panel is move,
  7772. // unless we do not support it, but support copy
  7773. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7774. {
  7775. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  7776. }
  7777. else
  7778. {
  7779. // with ctrl-down, we want copy unless it is not supported
  7780. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  7781. }
  7782. }
  7783. }
  7784. }
  7785. }
  7786. }
  7787. //---------------------------------------------------------------------------
  7788. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7789. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7790. {
  7791. DebugAssert(!IsLocalBrowserMode());
  7792. if (FDDTotalSize >= 0)
  7793. {
  7794. if (File->IsDirectory)
  7795. {
  7796. FDDTotalSize = -1;
  7797. }
  7798. else
  7799. {
  7800. FDDTotalSize += File->Size;
  7801. }
  7802. }
  7803. DebugAssert(!FDragTempDir.IsEmpty());
  7804. // TODO: this is quite ineffective
  7805. // TODO: what if invalid character replacement is disabled?
  7806. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7807. UnicodeString TransferFileName;
  7808. if (Sender == RemoteDriveView)
  7809. {
  7810. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7811. }
  7812. else
  7813. {
  7814. TransferFileName = File->FileName;
  7815. }
  7816. FDDFileList->AddObject(TransferFileName, File);
  7817. }
  7818. //---------------------------------------------------------------------------
  7819. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7820. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7821. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7822. {
  7823. // sometimes we do not get DDEnd so the list is not released
  7824. SAFE_DESTROY(FDDFileList);
  7825. FDDFileList = new TStringList();
  7826. FDragTempDir = WinConfiguration->TemporaryDir();
  7827. FDDTotalSize = 0;
  7828. FDragDropSshTerminate.reset(NULL);
  7829. }
  7830. //---------------------------------------------------------------------------
  7831. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7832. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7833. HRESULT & Result)
  7834. {
  7835. if (Result == DRAGDROP_S_DROP)
  7836. {
  7837. try
  7838. {
  7839. GlobalDragImageList->HideDragImage();
  7840. try
  7841. {
  7842. RemoteFileControlDDTargetDrop();
  7843. }
  7844. catch(ESshTerminate & E)
  7845. {
  7846. FDragDropSshTerminate.reset(E.Clone());
  7847. }
  7848. }
  7849. catch (Exception &E)
  7850. {
  7851. // If downloading fails we need to cancel drag&drop, otherwise
  7852. // Explorer shows error
  7853. // But by the way exception probably never reach this point as
  7854. // it's catched on way.
  7855. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7856. Result = DRAGDROP_S_CANCEL;
  7857. ShowExtendedException(Terminal, &E);
  7858. }
  7859. }
  7860. }
  7861. //---------------------------------------------------------------------------
  7862. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7863. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7864. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7865. {
  7866. TFileMasks::TParams MaskParams;
  7867. MaskParams.Size = Size;
  7868. MaskParams.Modification = Modification;
  7869. // this does not re-parse the mask if it is the same as the last time
  7870. FDirViewMatchMask = Masks;
  7871. bool ImplicitMatch;
  7872. Matches =
  7873. // RecurseInclude parameter has no effect as the Path is empty
  7874. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7875. (AllowImplicitMatches || !ImplicitMatch);
  7876. }
  7877. //---------------------------------------------------------------------------
  7878. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7879. TObject * Sender, TListItem * Item, WORD & Indexes)
  7880. {
  7881. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7882. UnicodeString Ext;
  7883. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  7884. {
  7885. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7886. }
  7887. else
  7888. {
  7889. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7890. }
  7891. if (SameText(Ext, Configuration->PartialExt))
  7892. {
  7893. Indexes |= oiPartial;
  7894. }
  7895. }
  7896. //---------------------------------------------------------------------------
  7897. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7898. {
  7899. return
  7900. DirViewEnabled(osCurrent) &&
  7901. DirView(osCurrent)->CanPasteFromClipBoard();
  7902. }
  7903. //---------------------------------------------------------------------------
  7904. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7905. {
  7906. bool Result = false;
  7907. if (CanPasteToDirViewFromClipBoard())
  7908. {
  7909. Result = true;
  7910. }
  7911. else
  7912. {
  7913. UnicodeString ClipboardText;
  7914. if (NonEmptyTextFromClipboard(ClipboardText) &&
  7915. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  7916. {
  7917. if (StoredSessions->IsUrl(ClipboardText))
  7918. {
  7919. Result = true;
  7920. }
  7921. else
  7922. {
  7923. Result = DirViewEnabled(osCurrent);
  7924. }
  7925. }
  7926. }
  7927. return Result;
  7928. }
  7929. //---------------------------------------------------------------------------
  7930. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7931. {
  7932. if (DoesClipboardContainOurFiles())
  7933. {
  7934. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  7935. {
  7936. DebugAssert(!IsLocalBrowserMode());
  7937. TTerminalManager * Manager = TTerminalManager::Instance();
  7938. TTerminal * TargetTerminal = Manager->ActiveTerminal;
  7939. Manager->ActiveTerminal = FClipboardTerminal;
  7940. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TargetTerminal);
  7941. }
  7942. }
  7943. else if (CanPasteToDirViewFromClipBoard())
  7944. {
  7945. DirView(osCurrent)->PasteFromClipBoard();
  7946. }
  7947. else
  7948. {
  7949. UnicodeString ClipboardText;
  7950. if (NonEmptyTextFromClipboard(ClipboardText))
  7951. {
  7952. if (StoredSessions->IsUrl(ClipboardText))
  7953. {
  7954. NewSession(ClipboardText);
  7955. }
  7956. else
  7957. {
  7958. DirView(osCurrent)->Path = ClipboardText;
  7959. }
  7960. }
  7961. }
  7962. }
  7963. //---------------------------------------------------------------------------
  7964. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  7965. {
  7966. // TBD
  7967. }
  7968. //---------------------------------------------------------------------------
  7969. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  7970. {
  7971. TCustomDirView * ADirView = DirView(Side);
  7972. ADirView->SelectAll(Mode);
  7973. ADirView->SetFocus();
  7974. }
  7975. //---------------------------------------------------------------------------
  7976. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  7977. {
  7978. TCustomDirView * ADirView = DirView(Side);
  7979. TFileFilter Filter;
  7980. DefaultFileFilter(Filter);
  7981. if (DoSelectMaskDialog(ADirView, Select, Filter))
  7982. {
  7983. Configuration->Usage->Inc(L"MaskSelections");
  7984. ADirView->SelectFiles(Filter, Select);
  7985. ADirView->SetFocus();
  7986. }
  7987. }
  7988. //---------------------------------------------------------------------------
  7989. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  7990. {
  7991. TCustomDirView * ADirView = DirView(Side);
  7992. ADirView->RestoreSelectedNames();
  7993. ADirView->SetFocus();
  7994. }
  7995. //---------------------------------------------------------------------------
  7996. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  7997. {
  7998. TCustomDirView * CurrentDirView = DirView(osCurrent);
  7999. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  8000. {
  8001. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  8002. UnicodeString Ext;
  8003. if (!IsSideLocalBrowser(osCurrent))
  8004. {
  8005. Ext = UnixExtractFileExt(FileName);
  8006. }
  8007. else
  8008. {
  8009. Ext = ExtractFileExt(FileName);
  8010. }
  8011. if (Ext.IsEmpty())
  8012. {
  8013. Ext = L".";
  8014. }
  8015. TFileFilter Filter;
  8016. Filter.Masks = FORMAT(L"*%s", (Ext));
  8017. Filter.Directories = false;
  8018. CurrentDirView->SelectFiles(Filter, Select);
  8019. }
  8020. }
  8021. //---------------------------------------------------------------------------
  8022. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8023. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8024. {
  8025. UnicodeString Result;
  8026. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  8027. {
  8028. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  8029. }
  8030. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8031. {
  8032. // noop
  8033. }
  8034. else
  8035. {
  8036. Result =
  8037. FMTLOAD(FILE_INFO_FORMAT,
  8038. (FormatBytes(FileInfo.SelectedSize),
  8039. FormatBytes(FileInfo.FilesSize),
  8040. FormatNumber(FileInfo.SelectedCount),
  8041. FormatNumber(FileInfo.FilesCount)));
  8042. }
  8043. return Result;
  8044. }
  8045. //---------------------------------------------------------------------------
  8046. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8047. TTBXStatusPanel * Panel, TOperationSide Side)
  8048. {
  8049. if (Panel->Index == 1)
  8050. {
  8051. ToggleShowHiddenFiles();
  8052. }
  8053. else if (Panel->Index == 2)
  8054. {
  8055. Filter(Side);
  8056. }
  8057. }
  8058. //---------------------------------------------------------------------------
  8059. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8060. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8061. {
  8062. DebugAssert(!StatusBar->SimplePanel);
  8063. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8064. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8065. }
  8066. //---------------------------------------------------------------------------
  8067. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8068. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8069. {
  8070. DebugAssert(StatusBar->Panels->Count >= 3);
  8071. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8072. if (FileInfo.HiddenCount > 0)
  8073. {
  8074. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8075. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8076. }
  8077. else
  8078. {
  8079. HiddenFilesPanel->ViewPriority = 0;
  8080. // not really necessary, just to cleanup no-longer-valid data
  8081. HiddenFilesPanel->Caption = L"";
  8082. }
  8083. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8084. if (FileInfo.FilteredCount > 0)
  8085. {
  8086. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8087. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8088. }
  8089. else
  8090. {
  8091. FilteredFilesPanel->ViewPriority = 0;
  8092. // not really necessary, just to cleanup no-longer-valid data
  8093. FilteredFilesPanel->Caption = L"";
  8094. }
  8095. }
  8096. //---------------------------------------------------------------------------
  8097. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8098. TObject * /*Sender*/)
  8099. {
  8100. if (DirView(osRemote)->Enabled)
  8101. {
  8102. DirView(osRemote)->SetFocus();
  8103. }
  8104. }
  8105. //---------------------------------------------------------------------------
  8106. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8107. {
  8108. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8109. switch (Config.Show)
  8110. {
  8111. case qvShow:
  8112. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8113. {
  8114. Config.Show = qvHide;
  8115. }
  8116. else
  8117. {
  8118. Config.Show = Config.LastHideShow;
  8119. }
  8120. break;
  8121. case qvHideWhenEmpty:
  8122. if (ComponentVisible[fcQueueView])
  8123. {
  8124. Config.Show = qvHide;
  8125. }
  8126. else
  8127. {
  8128. Config.LastHideShow = Config.Show;
  8129. Config.Show = qvShow;
  8130. }
  8131. break;
  8132. case qvHide:
  8133. Config.LastHideShow = Config.Show;
  8134. Config.Show = qvShow;
  8135. break;
  8136. }
  8137. WinConfiguration->QueueView = Config;
  8138. }
  8139. //---------------------------------------------------------------------------
  8140. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8141. {
  8142. UnicodeString Result;
  8143. if (FTerminal != NULL)
  8144. {
  8145. switch (WinConfiguration->PathInCaption)
  8146. {
  8147. case picShort:
  8148. Result = TTerminalManager::Instance()->GetTerminalShortPath(FTerminal);
  8149. break;
  8150. case picFull:
  8151. Result = FTerminal->CurrentDirectory;
  8152. break;
  8153. }
  8154. }
  8155. return Result;
  8156. }
  8157. //---------------------------------------------------------------------------
  8158. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8159. {
  8160. TColor Result = GetWindowColor(FSessionColor);
  8161. return Result;
  8162. }
  8163. //---------------------------------------------------------------------------
  8164. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8165. {
  8166. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8167. return Result;
  8168. }
  8169. //---------------------------------------------------------------------------
  8170. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8171. {
  8172. TTerminalManager::Instance()->UpdateAppTitle();
  8173. // WORKAROUND: Disabling list view when it is not showing yet does not set its
  8174. // background to gray on Windows 7 (works on Windows 10).
  8175. // See also EnableControl
  8176. if (Showing)
  8177. {
  8178. if (Terminal != NULL)
  8179. {
  8180. // Update path when it changes
  8181. if ((SessionsPageControl->ActivePage != NULL) && (GetSessionTabTerminal(SessionsPageControl->ActivePage) == Terminal))
  8182. {
  8183. UpdateSessionTab(SessionsPageControl->ActivePage);
  8184. }
  8185. }
  8186. NonVisualDataModule->ReconnectSessionAction->Update();
  8187. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8188. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8189. CenterReconnectToolbar();
  8190. bool HasTerminal = HasActiveTerminal();
  8191. if (HasTerminal)
  8192. {
  8193. if (!RemoteDirView->Enabled)
  8194. {
  8195. RemoteDirView->Enabled = true;
  8196. if (FRemoteDirViewWasFocused)
  8197. {
  8198. ActiveControl = RemoteDirView;
  8199. }
  8200. }
  8201. RemoteDirView->Color = PanelColor();
  8202. }
  8203. else
  8204. {
  8205. if (RemoteDirView->Enabled)
  8206. {
  8207. // This is first called when the form is being constructed
  8208. // (not anymore due to Showing test above)
  8209. // but the false is overriden in the constructor later.
  8210. // An even later in TScpCommanderForm::DoShow()
  8211. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  8212. RemoteDirView->Enabled = false;
  8213. }
  8214. RemoteDirView->Color = DisabledPanelColor();
  8215. }
  8216. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8217. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8218. RemoteDriveView->Color = RemoteDirView->Color;
  8219. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8220. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8221. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8222. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8223. QueueFileList->Enabled = QueueView3->Enabled;
  8224. QueueFileList->Color = QueueView3->Color;
  8225. QueueFileList->Font->Color = QueueView3->Font->Color;
  8226. QueueLabelUpdateStatus();
  8227. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8228. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8229. if (FImmersiveDarkMode != WinConfiguration->UseDarkTheme())
  8230. {
  8231. UpdateDarkMode();
  8232. }
  8233. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  8234. }
  8235. }
  8236. //---------------------------------------------------------------------------
  8237. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8238. {
  8239. UpdateControls();
  8240. }
  8241. //---------------------------------------------------------------------------
  8242. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8243. TObject * Sender)
  8244. {
  8245. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8246. DebugAssert(DirView != NULL);
  8247. DoDirViewLoaded(DirView);
  8248. TransferPresetAutoSelect();
  8249. }
  8250. //---------------------------------------------------------------------------
  8251. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8252. {
  8253. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8254. // first run after installation
  8255. if (double(Updates.LastCheck) == 0)
  8256. {
  8257. // make sure next time there will be an update (if enabled)
  8258. Updates.LastCheck = TDateTime(1);
  8259. WinConfiguration->Updates = Updates;
  8260. }
  8261. else if ((double(Updates.Period) > 0) &&
  8262. (Now() - Updates.LastCheck >= Updates.Period))
  8263. {
  8264. TThreadMethod OnUpdatesChecked = NULL;
  8265. if (!IsUWP())
  8266. {
  8267. OnUpdatesChecked = UpdatesChecked;
  8268. }
  8269. StartUpdateThread(OnUpdatesChecked);
  8270. }
  8271. }
  8272. //---------------------------------------------------------------------------
  8273. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8274. {
  8275. UnicodeString Message;
  8276. bool New;
  8277. TQueryType Type;
  8278. GetUpdatesMessage(Message, New, Type, false);
  8279. if (!Message.IsEmpty())
  8280. {
  8281. if (New)
  8282. {
  8283. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8284. }
  8285. if (!New && (Type != qtWarning))
  8286. {
  8287. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8288. }
  8289. else
  8290. {
  8291. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8292. }
  8293. if (New)
  8294. {
  8295. Configuration->Usage->Inc(L"UpdateNotifications");
  8296. }
  8297. }
  8298. }
  8299. //---------------------------------------------------------------------------
  8300. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8301. {
  8302. RestoreApp();
  8303. bool CanDisplay = !NonVisualDataModule->Busy;
  8304. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8305. {
  8306. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8307. }
  8308. if (CanDisplay)
  8309. {
  8310. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8311. CheckForUpdates(true);
  8312. }
  8313. else
  8314. {
  8315. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8316. }
  8317. }
  8318. //---------------------------------------------------------------------------
  8319. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8320. TCopyParamRuleData & Data)
  8321. {
  8322. DebugAssert(Terminal != NULL);
  8323. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8324. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8325. Data.RemoteDirectory = RemoteDirView->PathName;
  8326. }
  8327. //---------------------------------------------------------------------------
  8328. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8329. {
  8330. // Terminal can be null when we are changing local directory implicitly,
  8331. // because it has been deleted, while no session is connected
  8332. // (Login dialog is open)
  8333. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8334. {
  8335. DebugAssert(!IsLocalBrowserMode());
  8336. TCopyParamRuleData Data;
  8337. GetTransferPresetAutoSelectData(Data);
  8338. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8339. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8340. if (CopyParamIndex < 0)
  8341. {
  8342. // there is no preset that matches autoselection
  8343. // set preset that we consider "default"
  8344. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8345. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8346. }
  8347. else
  8348. {
  8349. // there is preset matching autoselection
  8350. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8351. if (CopyParamName == FCopyParamAutoSelected)
  8352. {
  8353. // autoselected the same preset as the last time
  8354. // make no change (i.e. preserve custom user preset, if any)
  8355. }
  8356. else
  8357. {
  8358. // autoselected the different preset then the last time (or there
  8359. // was default preset set)
  8360. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8361. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8362. }
  8363. }
  8364. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8365. {
  8366. if (CopyParamIndex >= 0)
  8367. {
  8368. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8369. }
  8370. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8371. try
  8372. {
  8373. int Fmt =
  8374. (CopyParamIndex < 0) ?
  8375. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8376. COPY_PARAM_AUTOSELECTED;
  8377. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8378. Data->Message = MainInstructions(Message);
  8379. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8380. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8381. CopyParamAttrs);
  8382. if (CopyParamIndex >= 0)
  8383. {
  8384. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8385. Info = FORMAT(L"%s\n \n%s", (Info,
  8386. FMTLOAD(COPY_PARAM_RULE,
  8387. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8388. }
  8389. Data->Message += L"\n\n" + Info;
  8390. if (WinConfiguration->CopyParamAutoSelectNotice)
  8391. {
  8392. TransferPresetNoteMessage(Data, true);
  8393. }
  8394. else
  8395. {
  8396. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8397. Data = NULL; // ownership passed
  8398. }
  8399. }
  8400. __finally
  8401. {
  8402. delete Data;
  8403. }
  8404. }
  8405. }
  8406. }
  8407. //---------------------------------------------------------------------------
  8408. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8409. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8410. {
  8411. DebugAssert(NoteData != NULL);
  8412. DebugAssert(!IsLocalBrowserMode());
  8413. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8414. TQueryButtonAlias Aliases[1];
  8415. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8416. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8417. Params.Aliases = Aliases;
  8418. Params.AliasesCount = LENOF(Aliases);
  8419. unsigned int Result =
  8420. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8421. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8422. switch (Result)
  8423. {
  8424. case qaNeverAskAgain:
  8425. DebugAssert(AllowNeverAskAgain);
  8426. WinConfiguration->CopyParamAutoSelectNotice = false;
  8427. break;
  8428. case qaIgnore:
  8429. PreferencesDialog(pmPresets);
  8430. break;
  8431. }
  8432. }
  8433. //---------------------------------------------------------------------------
  8434. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8435. {
  8436. // as of now useless, as this is used for notes only, never for balloons, ...
  8437. RestoreApp();
  8438. // .. and we should never be busy here
  8439. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8440. {
  8441. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8442. }
  8443. }
  8444. //---------------------------------------------------------------------------
  8445. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8446. TPreferencesMode APreferencesMode)
  8447. {
  8448. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8449. TCopyParamRuleData Data;
  8450. if (Terminal != NULL)
  8451. {
  8452. PreferencesData.reset(new TPreferencesDialogData());
  8453. GetTransferPresetAutoSelectData(Data);
  8454. PreferencesData->CopyParamRuleData = &Data;
  8455. }
  8456. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8457. }
  8458. //---------------------------------------------------------------------------
  8459. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8460. const TCustomCommandType & Command)
  8461. {
  8462. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8463. {
  8464. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  8465. }
  8466. }
  8467. //---------------------------------------------------------------------------
  8468. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8469. {
  8470. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8471. TCustomCommandType Command;
  8472. // make sure we use local custom command when remote are not supported
  8473. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8474. {
  8475. Command = FLastCustomCommand;
  8476. }
  8477. else
  8478. {
  8479. Command.Params = Command.Params | ccLocal;
  8480. }
  8481. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8482. FEditingFocusedAdHocCommand = OnFocused;
  8483. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8484. int Options =
  8485. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8486. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8487. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8488. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8489. {
  8490. FLastCustomCommand = Command;
  8491. UpdateCustomCommandsToolbar();
  8492. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8493. }
  8494. }
  8495. //---------------------------------------------------------------------------
  8496. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8497. {
  8498. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8499. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8500. DebugAssert(State > 0);
  8501. if (State <= 0)
  8502. {
  8503. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  8504. }
  8505. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8506. }
  8507. //---------------------------------------------------------------------------
  8508. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8509. TCustomCommandType & Command, int & State)
  8510. {
  8511. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8512. if (Result)
  8513. {
  8514. Command = FLastCustomCommand;
  8515. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8516. }
  8517. return Result;
  8518. }
  8519. //---------------------------------------------------------------------------
  8520. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8521. {
  8522. ResetIncrementalSearch();
  8523. }
  8524. //---------------------------------------------------------------------------
  8525. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8526. {
  8527. DebugAssert(ComponentVisible[Component]);
  8528. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8529. }
  8530. //---------------------------------------------------------------------------
  8531. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8532. {
  8533. // when queue panel is resized here directly, the status bar is stretched
  8534. // over whole space the panel occupied
  8535. PostComponentHide(fcQueueView);
  8536. }
  8537. //---------------------------------------------------------------------------
  8538. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  8539. {
  8540. ComponentVisible[fcQueueFileList] = false;
  8541. }
  8542. //---------------------------------------------------------------------------
  8543. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  8544. {
  8545. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  8546. RefreshColorMode();
  8547. ResetSysDarkTheme();
  8548. ConfigurationChanged();
  8549. ConfigureInterface();
  8550. // Should be called for all controls
  8551. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  8552. }
  8553. //---------------------------------------------------------------------------
  8554. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  8555. {
  8556. // Do not handle, when shutting down anyway (maybe also when not setup completelly yet?)
  8557. if (!FInvalid &&
  8558. (Message.LParam != 0) &&
  8559. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  8560. {
  8561. ThemeChanged();
  8562. }
  8563. TForm::Dispatch(&Message);
  8564. }
  8565. //---------------------------------------------------------------------------
  8566. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8567. {
  8568. TMessage * M = static_cast<TMessage*>(Message);
  8569. switch (M->Msg)
  8570. {
  8571. case CM_DIALOGCHAR:
  8572. CMDialogChar(*M);
  8573. break;
  8574. case WM_APPCOMMAND:
  8575. WMAppCommand(*M);
  8576. break;
  8577. case WM_SYSCOMMAND:
  8578. WMSysCommand(*M);
  8579. break;
  8580. case WM_QUERYENDSESSION:
  8581. WMQueryEndSession(*M);
  8582. break;
  8583. case WM_ENDSESSION:
  8584. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8585. break;
  8586. #ifdef _DEBUG
  8587. case WM_WINDOWPOSCHANGED:
  8588. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8589. break;
  8590. case CM_CUSTOMSTYLECHANGED:
  8591. TForm::Dispatch(Message);
  8592. break;
  8593. #endif
  8594. case WM_COMPONENT_HIDE:
  8595. {
  8596. Byte Component = static_cast<Byte>(M->WParam);
  8597. // sanity check
  8598. if (ComponentVisible[Component])
  8599. {
  8600. // special treatment
  8601. if (Component == fcQueueView)
  8602. {
  8603. ToggleQueueVisibility();
  8604. DebugAssert(!ComponentVisible[fcQueueView]);
  8605. }
  8606. else
  8607. {
  8608. ComponentVisible[Component] = false;
  8609. }
  8610. }
  8611. }
  8612. break;
  8613. case WM_COPYDATA:
  8614. WMCopyData(*M);
  8615. break;
  8616. case WM_MANAGES_CAPTION:
  8617. // caption managed in TTerminalManager::UpdateAppTitle()
  8618. M->Result = 1;
  8619. break;
  8620. case WM_WANTS_MOUSEWHEEL:
  8621. M->Result = 1;
  8622. break;
  8623. case CM_SHOWINGCHANGED:
  8624. CMShowingChanged(*M);
  8625. break;
  8626. case WM_CLOSE:
  8627. WMClose(*M);
  8628. break;
  8629. case WM_DPICHANGED:
  8630. WMDpiChanged(*M);
  8631. break;
  8632. case CM_DPICHANGED:
  8633. CMDpiChanged(*M);
  8634. break;
  8635. case WM_SETTINGCHANGE:
  8636. WMSettingChange(*M);
  8637. break;
  8638. case CM_DIALOGKEY:
  8639. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8640. break;
  8641. default:
  8642. TForm::Dispatch(Message);
  8643. break;
  8644. }
  8645. }
  8646. //---------------------------------------------------------------------------
  8647. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8648. {
  8649. // noop
  8650. }
  8651. //---------------------------------------------------------------------------
  8652. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8653. {
  8654. TForm::Dispatch(&Message);
  8655. FSessionColors->Clear();
  8656. AddFixedSessionImages();
  8657. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8658. UpdateImages();
  8659. }
  8660. //---------------------------------------------------------------------------
  8661. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8662. {
  8663. TForm::Dispatch(&Message);
  8664. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8665. UpdatePixelsPerInchMainWindowCounter();
  8666. }
  8667. //---------------------------------------------------------------------------
  8668. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8669. {
  8670. // Cannot close window while we are busy.
  8671. // We cannot test this in FormCloseQuery as that is called also from
  8672. // Close(), which is called by CloseApplicationAction. So we can be busy
  8673. // there already even, when it is legitimate to close the application.
  8674. // Possibly a better place to handle this would be WMSysCommand.
  8675. if (NonVisualDataModule->Busy)
  8676. {
  8677. Message.Result = 1;
  8678. }
  8679. else
  8680. {
  8681. TForm::Dispatch(&Message);
  8682. }
  8683. }
  8684. //---------------------------------------------------------------------------
  8685. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8686. {
  8687. TForm::Dispatch(&Message);
  8688. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  8689. if (Showing && !FStarted)
  8690. {
  8691. FStarted = true;
  8692. InterfaceStarted();
  8693. }
  8694. if (Showing && (Terminal == NULL))
  8695. {
  8696. // When we are starting minimized (i.e. from an installer),
  8697. // postpone showing Login dialog until we get restored.
  8698. // Otherwise the Login dialog (and Authentication window) show restored
  8699. // over invisible (minimized) main window.
  8700. if (WindowState == wsMinimized)
  8701. {
  8702. FNeedSession = true;
  8703. }
  8704. else
  8705. {
  8706. // This happens before application ever goes idle, so the toolbars would
  8707. // stay enabled (initial state) until the Login dialog is dismissed.
  8708. UpdateActions();
  8709. NonVisualDataModule->StartBusy();
  8710. try
  8711. {
  8712. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8713. // otherwise the dialog does not receive focus.
  8714. // With Commander interface the ProcessMessages is called already
  8715. // by TDriveView, but with Explorer interface, we need to call it explicily
  8716. Application->ProcessMessages();
  8717. // do not reload sessions, they have been loaded just now (optimization)
  8718. NeedSession(true);
  8719. }
  8720. __finally
  8721. {
  8722. NonVisualDataModule->EndBusy();
  8723. }
  8724. }
  8725. }
  8726. }
  8727. //---------------------------------------------------------------------------
  8728. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  8729. {
  8730. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  8731. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  8732. }
  8733. //---------------------------------------------------------------------------
  8734. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8735. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8736. int & MaxHeight)
  8737. {
  8738. // workaround for bug in TWinControl.CalcConstraints
  8739. // Check for empty rect (restore from iconic state) is done there only after
  8740. // call to AdjustClientRect, which enlarges the rect (for forms).
  8741. TRect R = GetClientRect();
  8742. // when restoring from iconic state, place no restrictions
  8743. if (IsRectEmpty(R))
  8744. {
  8745. MinWidth = 0;
  8746. MinHeight = 0;
  8747. MaxWidth = 32000;
  8748. MaxHeight = 32000;
  8749. }
  8750. }
  8751. //---------------------------------------------------------------------------
  8752. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8753. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8754. {
  8755. // terminal can be already closed (e.g. dropped connection)
  8756. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8757. {
  8758. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8759. try
  8760. {
  8761. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8762. }
  8763. catch(...)
  8764. {
  8765. if (!Terminal->Active)
  8766. {
  8767. Close = true;
  8768. }
  8769. throw;
  8770. }
  8771. }
  8772. }
  8773. //---------------------------------------------------------------------------
  8774. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8775. {
  8776. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8777. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8778. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8779. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8780. {
  8781. OnGetSpaceAvailable = GetSpaceAvailable;
  8782. }
  8783. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8784. OnGetSpaceAvailable);
  8785. }
  8786. //---------------------------------------------------------------------------
  8787. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8788. {
  8789. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8790. Terminal->FillSessionDataForCode(Data.get());
  8791. return Data.release();
  8792. }
  8793. //---------------------------------------------------------------------------
  8794. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8795. {
  8796. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8797. DoGenerateUrlDialog(Data.get(), Paths);
  8798. }
  8799. //---------------------------------------------------------------------------
  8800. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8801. {
  8802. GenerateUrl(NULL);
  8803. }
  8804. //---------------------------------------------------------------------------
  8805. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8806. {
  8807. std::unique_ptr<TStrings> Paths(new TStringList());
  8808. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8809. GenerateUrl(Paths.get());
  8810. }
  8811. //---------------------------------------------------------------------------
  8812. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  8813. {
  8814. FSessionColor = value;
  8815. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8816. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8817. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8818. DebugAssert(ColorItem != NULL);
  8819. ColorItem->Color = C;
  8820. NonVisualDataModule->ColorMenuItem->Color = C;
  8821. UpdateControls();
  8822. }
  8823. //---------------------------------------------------------------------------
  8824. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8825. {
  8826. if (value != FSessionColor)
  8827. {
  8828. UpdateSessionColor(value);
  8829. // Is null when called from LastTerminalClosed
  8830. if (Terminal != NULL)
  8831. {
  8832. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8833. }
  8834. }
  8835. }
  8836. //---------------------------------------------------------------------------
  8837. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8838. {
  8839. bool Result = FNoteTimer->Enabled;
  8840. if (Result)
  8841. {
  8842. // cannot cancel note too early
  8843. bool NotEarly =
  8844. (Now() - FNoteShown >
  8845. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8846. if (Force || NotEarly)
  8847. {
  8848. FNoteTimer->Enabled = false;
  8849. FNote = L"";
  8850. SAFE_DESTROY(FNoteData);
  8851. FOnNoteClick = NULL;
  8852. FNoteHints = FNotes->Text;
  8853. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8854. UpdateStatusBar();
  8855. }
  8856. }
  8857. return Result;
  8858. }
  8859. //---------------------------------------------------------------------------
  8860. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8861. {
  8862. DebugAssert(FNoteTimer->Enabled);
  8863. CancelNote(true);
  8864. }
  8865. //---------------------------------------------------------------------------
  8866. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  8867. {
  8868. FNoteHints = FNotes->Text.TrimRight();
  8869. }
  8870. //---------------------------------------------------------------------------
  8871. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8872. {
  8873. int P = Note.Pos(L"\n");
  8874. if (P > 0)
  8875. {
  8876. Note.SetLength(P - 1);
  8877. }
  8878. FNotes->Add(FORMAT(L"[%s] %s",
  8879. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8880. while (FNotes->Count > 10)
  8881. {
  8882. FNotes->Delete(0);
  8883. }
  8884. if (UpdateNow)
  8885. {
  8886. UpdateNoteHints();
  8887. UpdateStatusBar();
  8888. }
  8889. }
  8890. //---------------------------------------------------------------------------
  8891. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8892. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8893. {
  8894. int P = Note.Pos(L"\n");
  8895. if (P > 0)
  8896. {
  8897. Note.SetLength(P - 1);
  8898. }
  8899. UpdateNoteHints();
  8900. FNote = Note;
  8901. SAFE_DESTROY(FNoteData);
  8902. FNoteData = NoteData;
  8903. FOnNoteClick = OnNoteClick;
  8904. AddNote(Note, false);
  8905. UpdateStatusBar();
  8906. FNoteShown = Now();
  8907. FNoteTimer->Enabled = false;
  8908. if (Seconds == 0)
  8909. {
  8910. Seconds = WinConfiguration->NotificationsTimeout;
  8911. }
  8912. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8913. FNoteTimer->Enabled = true;
  8914. }
  8915. //---------------------------------------------------------------------------
  8916. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8917. {
  8918. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8919. }
  8920. //---------------------------------------------------------------------------
  8921. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8922. {
  8923. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  8924. {
  8925. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8926. }
  8927. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  8928. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  8929. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  8930. 0, NULL, NULL);
  8931. }
  8932. //---------------------------------------------------------------------------
  8933. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  8934. {
  8935. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  8936. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  8937. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  8938. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  8939. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  8940. }
  8941. //---------------------------------------------------------------------------
  8942. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  8943. {
  8944. WinConfiguration->FormatSizeBytes = Style;
  8945. }
  8946. //---------------------------------------------------------------------------
  8947. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  8948. {
  8949. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  8950. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  8951. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  8952. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  8953. }
  8954. //---------------------------------------------------------------------------
  8955. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  8956. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  8957. {
  8958. if (Panel->Index == 0)
  8959. {
  8960. if (FOnNoteClick != NULL)
  8961. {
  8962. // prevent the user data from being freed by possible call
  8963. // to CancelNote or PostNote during call to OnNoteClick
  8964. std::unique_ptr<TObject> NoteData(FNoteData);
  8965. TNotifyEvent OnNoteClick = FOnNoteClick;
  8966. FNoteData = NULL;
  8967. // need to cancel the note as we are going to delete its user data
  8968. CancelNote(true);
  8969. OnNoteClick(NoteData.get());
  8970. }
  8971. }
  8972. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  8973. {
  8974. FileSystemInfo();
  8975. }
  8976. }
  8977. //---------------------------------------------------------------------------
  8978. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  8979. {
  8980. // workaround:
  8981. // 1) for unknown reason, disabling window, while minimized,
  8982. // prevents it from restoring, even if it was enabled again meanwhile
  8983. // 2) when disabling the main window, while another has focus
  8984. // minimize is no longer possible
  8985. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  8986. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  8987. // Shouldn't we use IsApplicationMinimized() here?
  8988. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  8989. {
  8990. Enabled = false;
  8991. }
  8992. FLockLevel++;
  8993. }
  8994. //---------------------------------------------------------------------------
  8995. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  8996. {
  8997. DebugAssert(FLockLevel > 0);
  8998. FLockLevel--;
  8999. if (FLockLevel == 0)
  9000. {
  9001. DebugAssert(FLockSuspendLevel == 0);
  9002. Enabled = true;
  9003. }
  9004. }
  9005. //---------------------------------------------------------------------------
  9006. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  9007. {
  9008. // We need to make sure that window is enabled when the last modal window closes
  9009. // otherwise focus is not restored correctly.
  9010. // So we re-enable the window when modal window opens and
  9011. // disable it back after is closes
  9012. if (FLockLevel > 0)
  9013. {
  9014. // while we have nesting counter, we know that we never be called
  9015. // recursivelly as Application->OnModalBegin is called only
  9016. // for the top-level modal window
  9017. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9018. {
  9019. // won't be disabled when conditions in LockWindow() were not satisfied
  9020. FDisabledOnLockSuspend = !Enabled;
  9021. // When minimized to tray (or actually when set to SW_HIDE),
  9022. // setting Enabled makes the window focusable even when there's
  9023. // modal window over it
  9024. if (!FTrayIcon->Visible)
  9025. {
  9026. Enabled = true;
  9027. }
  9028. }
  9029. FLockSuspendLevel++;
  9030. }
  9031. }
  9032. //---------------------------------------------------------------------------
  9033. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9034. {
  9035. if (FLockSuspendLevel > 0)
  9036. {
  9037. DebugAssert(FLockLevel > 0);
  9038. FLockSuspendLevel--;
  9039. // see comment in SuspendWindowLock
  9040. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9041. {
  9042. // Note that window can be enabled here, when we were minized to tray when
  9043. // was SuspendWindowLock() called.
  9044. // We should possibly do the same check as in LockWindow(),
  9045. // if it is ever possible that the consitions change between
  9046. // SuspendWindowLock() and ResumeWindowLock()
  9047. Enabled = !FDisabledOnLockSuspend;
  9048. }
  9049. }
  9050. }
  9051. //---------------------------------------------------------------------------
  9052. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9053. {
  9054. TTBXComboBoxItem * RemotePathComboBox =
  9055. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9056. if (!TextOnly)
  9057. {
  9058. TStrings * Items = RemotePathComboBox->Strings;
  9059. Items->BeginUpdate();
  9060. try
  9061. {
  9062. Items->Clear();
  9063. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9064. {
  9065. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9066. while (!IsUnixRootPath(APath))
  9067. {
  9068. int P = APath.LastDelimiter(L'/');
  9069. DebugAssert(P >= 0);
  9070. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9071. APath.SetLength(P - 1);
  9072. }
  9073. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9074. }
  9075. }
  9076. __finally
  9077. {
  9078. Items->EndUpdate();
  9079. }
  9080. }
  9081. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9082. // Setting ItemIndex to -1 does not reset its text
  9083. if (RemotePathComboBox->Strings->Count == 0)
  9084. {
  9085. RemotePathComboBox->Text = L"";
  9086. }
  9087. }
  9088. //---------------------------------------------------------------------------
  9089. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9090. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  9091. int & ImageIndex)
  9092. {
  9093. if (AIndex < 0)
  9094. {
  9095. AIndex = Sender->ItemIndex;
  9096. }
  9097. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9098. }
  9099. //---------------------------------------------------------------------------
  9100. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9101. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9102. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9103. {
  9104. ARect.Left += (10 * AIndex);
  9105. }
  9106. //---------------------------------------------------------------------------
  9107. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9108. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9109. {
  9110. AWidth += (10 * AIndex);
  9111. }
  9112. //---------------------------------------------------------------------------
  9113. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  9114. TObject * Sender)
  9115. {
  9116. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9117. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9118. int Index = RemotePathComboBox->ItemIndex;
  9119. while (Index < RemotePathComboBox->Strings->Count - 1)
  9120. {
  9121. APath = UnixExtractFileDir(APath);
  9122. Index++;
  9123. }
  9124. // VanDyke style paths
  9125. if (APath.IsEmpty())
  9126. {
  9127. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9128. APath = ROOTDIRECTORY;
  9129. }
  9130. if (RemoteDirView->Path != APath)
  9131. {
  9132. RemoteDirView->Path = APath;
  9133. }
  9134. }
  9135. //---------------------------------------------------------------------------
  9136. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9137. {
  9138. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9139. DebugUsedParam(Sender);
  9140. UpdateRemotePathComboBox(true);
  9141. }
  9142. //---------------------------------------------------------------------------
  9143. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9144. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9145. {
  9146. ResetIncrementalSearch();
  9147. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9148. DebugAssert(DirView != NULL);
  9149. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9150. {
  9151. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9152. // OnEditing is called also from TCustomListView::CanEdit
  9153. if (Edit != NULL)
  9154. {
  9155. EditSelectBaseName(Edit);
  9156. }
  9157. }
  9158. }
  9159. //---------------------------------------------------------------------------
  9160. //---------------------------------------------------------------------------
  9161. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9162. {
  9163. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9164. Result->AutoDetectDnD = false;
  9165. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9166. Result->RenderDataOn = rdoEnterAndDropSync;
  9167. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9168. return Result;
  9169. }
  9170. //---------------------------------------------------------------------------
  9171. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9172. {
  9173. if (IsWin10Build(2004))
  9174. {
  9175. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9176. BOOL DarkMode = FImmersiveDarkMode ? TRUE : FALSE;
  9177. const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  9178. DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
  9179. }
  9180. }
  9181. //---------------------------------------------------------------------------
  9182. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9183. {
  9184. TForm::CreateWnd();
  9185. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9186. UpdateDarkMode();
  9187. if (FSessionsDragDropFilesEx == NULL)
  9188. {
  9189. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9190. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9191. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9192. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9193. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9194. }
  9195. if (FQueueDragDropFilesEx == NULL)
  9196. {
  9197. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9198. // No need to set OnDragOver as we do not have any restrictions
  9199. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9200. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9201. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9202. }
  9203. }
  9204. //---------------------------------------------------------------------------
  9205. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9206. {
  9207. TForm::DestroyWnd();
  9208. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9209. FQueueDragDropFilesEx->DragDropControl = NULL;
  9210. }
  9211. //---------------------------------------------------------------------------
  9212. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9213. {
  9214. SideEnter(FCurrentSide);
  9215. FEverShown = true;
  9216. }
  9217. //---------------------------------------------------------------------------
  9218. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9219. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9220. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9221. {
  9222. if (!NonVisualDataModule->Busy)
  9223. {
  9224. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9225. Configuration->Usage->Inc(L"FileFinds");
  9226. LockWindow(true);
  9227. NonVisualDataModule->StartBusy();
  9228. WinConfiguration->LockedInterface = true;
  9229. try
  9230. {
  9231. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9232. }
  9233. __finally
  9234. {
  9235. WinConfiguration->LockedInterface = false;
  9236. NonVisualDataModule->EndBusy();
  9237. UnlockWindow();
  9238. }
  9239. }
  9240. }
  9241. //---------------------------------------------------------------------------
  9242. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9243. {
  9244. if (!NonVisualDataModule->Busy)
  9245. {
  9246. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9247. SetFocus();
  9248. RemoteDirView->Path = UnixExtractFilePath(Path);
  9249. UnicodeString FileName = UnixExtractFileName(Path);
  9250. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9251. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9252. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9253. if (Item == NULL)
  9254. {
  9255. ReloadDirectory(osRemote);
  9256. Item = RemoteDirView->FindFileItem(FileName);
  9257. }
  9258. if (Item != NULL)
  9259. {
  9260. RemoteDirView->ItemFocused = Item;
  9261. RemoteDirView->ItemFocused->MakeVisible(false);
  9262. RemoteDirView->SetFocus();
  9263. }
  9264. }
  9265. }
  9266. //---------------------------------------------------------------------------
  9267. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9268. {
  9269. bool Result = !NonVisualDataModule->Busy;
  9270. if (Result)
  9271. {
  9272. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9273. }
  9274. return Result;
  9275. }
  9276. //---------------------------------------------------------------------------
  9277. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9278. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9279. {
  9280. if (CanOperateOnFoundFiles(ATerminal))
  9281. {
  9282. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9283. FOnFileOperationFinished = OnFileOperationFinished;
  9284. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9285. }
  9286. }
  9287. //---------------------------------------------------------------------------
  9288. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9289. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9290. {
  9291. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9292. }
  9293. //---------------------------------------------------------------------------
  9294. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9295. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9296. {
  9297. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9298. }
  9299. //---------------------------------------------------------------------------
  9300. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9301. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9302. {
  9303. if (CanOperateOnFoundFiles(ATerminal))
  9304. {
  9305. for (int Index = 0; Index < FileList->Count; Index++)
  9306. {
  9307. UnicodeString FileName = FileList->Strings[Index];
  9308. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9309. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9310. OnFileOperationFinished(FileName, true);
  9311. }
  9312. }
  9313. }
  9314. //---------------------------------------------------------------------------
  9315. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9316. {
  9317. FFileFindTerminal = Terminal;
  9318. ShowFileFindDialog(
  9319. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9320. DoDownloadFoundFiles, DoEditFoundFiles);
  9321. }
  9322. //---------------------------------------------------------------------------
  9323. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9324. {
  9325. FTaskbarList = TaskbarList;
  9326. }
  9327. //---------------------------------------------------------------------------
  9328. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9329. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9330. {
  9331. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9332. if (Index >= 0)
  9333. {
  9334. if (Button == mbLeft)
  9335. {
  9336. // WORKAROUND: "Mouse down" is raised only after tab is switched.
  9337. // If switching tab (switching session) takes long enough for user
  9338. // to actually release the button, "mouse down" is still raised,
  9339. // but we do not get "mouse up" event, so dragging is not cancelled,
  9340. // prevent that by not beginning dragging in the first place.
  9341. if (IsKeyPressed(VK_LBUTTON))
  9342. {
  9343. // when user clicks the "+", we get mouse down only after the session
  9344. // is closed, when new session tab is already on X:Y, so dragging
  9345. // starts, prevent that
  9346. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  9347. {
  9348. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9349. if (Terminal != NULL)
  9350. {
  9351. SessionsPageControl->BeginDrag(false);
  9352. }
  9353. }
  9354. }
  9355. }
  9356. else if (Button == mbMiddle)
  9357. {
  9358. // ignore middle-click for "New session tab"
  9359. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9360. if (Terminal != NULL)
  9361. {
  9362. CloseSessionTab(Index);
  9363. }
  9364. }
  9365. }
  9366. }
  9367. //---------------------------------------------------------------------------
  9368. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9369. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9370. {
  9371. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9372. // do not allow dropping on the "+" tab
  9373. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9374. if ((TargetTerminal != NULL) &&
  9375. (SessionsPageControl->ActivePage->PageIndex != Index))
  9376. {
  9377. Configuration->Usage->Inc(L"SessionTabMoves");
  9378. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  9379. // but we want to actually prevent that to avoid flicker
  9380. SessionsPageControl->ActivePage->PageIndex = Index;
  9381. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  9382. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9383. }
  9384. }
  9385. //---------------------------------------------------------------------------
  9386. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9387. TObject * Sender, TObject * Source, int X, int Y,
  9388. TDragState /*State*/, bool & Accept)
  9389. {
  9390. Accept = (Sender == Source);
  9391. if (Accept)
  9392. {
  9393. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9394. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9395. // do not allow dragging to the "+" tab
  9396. Accept = (Terminal != NULL);
  9397. }
  9398. }
  9399. //---------------------------------------------------------------------------
  9400. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  9401. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  9402. {
  9403. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9404. if (Index < 0)
  9405. {
  9406. Effect = DROPEFFECT_NONE;
  9407. }
  9408. else
  9409. {
  9410. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9411. // do not allow dropping on the "+" and disconnected tabs
  9412. if (!IsActiveTerminal(TargetTerminal))
  9413. {
  9414. Effect = DROPEFFECT_NONE;
  9415. }
  9416. else if ((TargetTerminal != Terminal) && (Effect == DROPEFFECT_MOVE))
  9417. {
  9418. Effect = DROPEFFECT_COPY;
  9419. }
  9420. }
  9421. }
  9422. //---------------------------------------------------------------------------
  9423. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9424. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9425. {
  9426. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9427. // do not allow dropping on the "+" and disconnected tabs
  9428. TManagedTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9429. if (IsActiveTerminal(TargetTerminal))
  9430. {
  9431. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9432. if (!IsFileControl(DropSourceControl, osRemote))
  9433. {
  9434. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  9435. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9436. // Why don't we use Terminal->CurrentDirectory directly?
  9437. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9438. // do not force queue, drag drop
  9439. false, true);
  9440. }
  9441. }
  9442. }
  9443. //---------------------------------------------------------------------------
  9444. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9445. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9446. {
  9447. // Downloads are handled in RemoteFileControlDDEnd
  9448. if (!IsFileControl(DropSourceControl, osRemote))
  9449. {
  9450. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9451. Terminal->CurrentDirectory,
  9452. // force queue, drag drop
  9453. true, true);
  9454. }
  9455. }
  9456. //---------------------------------------------------------------------------
  9457. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9458. {
  9459. FShowing = false;
  9460. // Do not save empty workspace
  9461. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  9462. {
  9463. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9464. UnicodeString Name = WorkspaceName();
  9465. DoSaveWorkspace(
  9466. Name, DataList.get(),
  9467. !Configuration->DisablePasswordStoring &&
  9468. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9469. WinConfiguration->LastStoredSession = Name;
  9470. }
  9471. }
  9472. //---------------------------------------------------------------------------
  9473. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9474. {
  9475. TManagedTerminal * ManagedTerminal =
  9476. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9477. if (ManagedTerminal != NULL)
  9478. {
  9479. ManagedTerminal->DirectoryLoaded = Now();
  9480. }
  9481. }
  9482. //---------------------------------------------------------------------------
  9483. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9484. TListItem * /*Item*/, bool /*Selected*/)
  9485. {
  9486. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9487. switch (DirView->LastSelectMethod)
  9488. {
  9489. case smKeyboard:
  9490. Configuration->Usage->Inc(L"KeyboardSelections");
  9491. break;
  9492. case smMouse:
  9493. Configuration->Usage->Inc(L"MouseSelections");
  9494. break;
  9495. }
  9496. }
  9497. //---------------------------------------------------------------------------
  9498. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9499. {
  9500. if (Color != 0)
  9501. {
  9502. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9503. }
  9504. else
  9505. {
  9506. return FSessionTabImageIndex;
  9507. }
  9508. }
  9509. //---------------------------------------------------------------------------
  9510. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9511. {
  9512. TPngImageCollectionItem * Item =
  9513. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9514. FSessionColors->AddPng(Item->PngImage);
  9515. return FSessionColors->Count - 1;
  9516. }
  9517. //---------------------------------------------------------------------------
  9518. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9519. {
  9520. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9521. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  9522. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9523. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9524. }
  9525. //---------------------------------------------------------------------------
  9526. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9527. {
  9528. for (int Index = 0; Index < Control->ControlCount; Index++)
  9529. {
  9530. TControl * AControl = Control->Controls[Index];
  9531. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9532. if (WinControl != NULL)
  9533. {
  9534. CollectItemsWithTextDisplayMode(WinControl);
  9535. }
  9536. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9537. if (Toolbar != NULL)
  9538. {
  9539. // we care for top-level items only
  9540. TTBCustomItem * Items = Toolbar->Items;
  9541. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9542. {
  9543. TTBCustomItem * Item = Items->Items[ItemIndex];
  9544. if (((Item->DisplayMode == nbdmImageAndText) ||
  9545. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9546. EligibleForImageDisplayMode(Item))
  9547. {
  9548. FItemsWithTextDisplayMode.insert(Item);
  9549. }
  9550. }
  9551. }
  9552. }
  9553. }
  9554. //---------------------------------------------------------------------------
  9555. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9556. {
  9557. return true;
  9558. }
  9559. //---------------------------------------------------------------------------
  9560. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9561. {
  9562. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9563. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9564. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9565. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9566. bool Result = true;
  9567. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9568. {
  9569. TTBCustomItem * Item = *i;
  9570. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9571. if (Label != NULL)
  9572. {
  9573. // optimization
  9574. if (Label->Visible == SelectiveToolbarText)
  9575. {
  9576. Result = false;
  9577. }
  9578. else
  9579. {
  9580. Label->Visible = SelectiveToolbarText;
  9581. }
  9582. }
  9583. else
  9584. {
  9585. // optimization
  9586. if (Item->DisplayMode == DisplayMode)
  9587. {
  9588. Result = false;
  9589. }
  9590. else
  9591. {
  9592. Item->DisplayMode = DisplayMode;
  9593. }
  9594. }
  9595. ++i;
  9596. }
  9597. if (Result)
  9598. {
  9599. UpdateNewSessionTab();
  9600. }
  9601. return Result;
  9602. }
  9603. //---------------------------------------------------------------------------
  9604. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9605. {
  9606. DebugFail();
  9607. }
  9608. //---------------------------------------------------------------------------
  9609. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9610. {
  9611. // Among other, a lock level is non zero, while directory is being loaded,
  9612. // even when it happens as a result of dirview navigation,
  9613. // i.e. when TNonVisualDataModule is NOT FBusy.
  9614. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9615. // Among other this prevents a panel auto update to occur while
  9616. // directory is changing.
  9617. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9618. }
  9619. //---------------------------------------------------------------------------
  9620. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  9621. {
  9622. // While the window is disabled, we still seem to process menu shortcuts at least,
  9623. // so stop it at least here.
  9624. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9625. return
  9626. (Allowed == aaUpdate) ||
  9627. !NonVisualDataModule->Busy;
  9628. }
  9629. //---------------------------------------------------------------------------
  9630. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9631. {
  9632. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9633. }
  9634. //---------------------------------------------------------------------------
  9635. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9636. {
  9637. // This is somewhat redundant to LockWindow() call from
  9638. // TTerminalManager::TerminalReadDirectoryProgress.
  9639. // But disabling window is known not to block keyboard shorcuts
  9640. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9641. if (Busy > 0)
  9642. {
  9643. if (NonVisualDataModule->Busy)
  9644. {
  9645. State = false;
  9646. }
  9647. else
  9648. {
  9649. NonVisualDataModule->StartBusy();
  9650. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9651. LockWindow();
  9652. }
  9653. }
  9654. else if (Busy < 0)
  9655. {
  9656. UnlockWindow();
  9657. NonVisualDataModule->EndBusy();
  9658. }
  9659. else
  9660. {
  9661. State = NonVisualDataModule->Busy;
  9662. }
  9663. }
  9664. //---------------------------------------------------------------------------
  9665. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9666. {
  9667. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9668. // no context menu for "New session tab"
  9669. if ((Index >= 0) && (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != NULL))
  9670. {
  9671. SessionsPageControl->ActivePageIndex = Index;
  9672. if (DebugAlwaysTrue(SessionTabSwitched()))
  9673. {
  9674. // copied from TControl.WMContextMenu
  9675. SendCancelMode(SessionsPageControl);
  9676. // explicit popup instead of using PopupMenu property
  9677. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9678. // while connecting yet not-connected session and hence
  9679. // allowing an access to commands over not-completelly connected session
  9680. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9681. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  9682. PopupMenu->PopupComponent = SessionsPageControl;
  9683. PopupMenu->Popup(Point.x, Point.y);
  9684. }
  9685. }
  9686. Handled = true;
  9687. }
  9688. //---------------------------------------------------------------------------
  9689. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9690. {
  9691. return HasActiveTerminal() && Terminal->IsCapable[fcChangePassword];
  9692. }
  9693. //---------------------------------------------------------------------------
  9694. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9695. {
  9696. Configuration->Usage->Inc(L"PasswordChanges");
  9697. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9698. ChangePasswordData->DisableAuthentationsExceptPassword();
  9699. ChangePasswordData->ChangePassword = true;
  9700. std::unique_ptr<TTerminal> ChangePasswordSession(
  9701. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9702. ChangePasswordSession->AutoReadDirectory = false;
  9703. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9704. {
  9705. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9706. }
  9707. else
  9708. {
  9709. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9710. }
  9711. }
  9712. //---------------------------------------------------------------------------
  9713. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9714. {
  9715. // No nice way to assert SSH2
  9716. return HasActiveTerminal() && (Terminal->FSProtocol == cfsSFTP);
  9717. }
  9718. //---------------------------------------------------------------------------
  9719. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9720. {
  9721. TOperationVisualizer OperationVisualizer;
  9722. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9723. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9724. }
  9725. //---------------------------------------------------------------------------
  9726. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9727. {
  9728. TForm::ChangeScale(M, D);
  9729. int APixelsPerInch = GetControlPixelsPerInch(this);
  9730. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9731. try
  9732. {
  9733. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9734. }
  9735. __finally
  9736. {
  9737. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9738. }
  9739. }
  9740. //---------------------------------------------------------------------------
  9741. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9742. {
  9743. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9744. }
  9745. //---------------------------------------------------------------------
  9746. class TPublicControl : public TControl
  9747. {
  9748. friend class TCustomScpExplorerForm;
  9749. };
  9750. //---------------------------------------------------------------------------
  9751. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9752. {
  9753. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9754. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9755. }
  9756. //---------------------------------------------------------------------------
  9757. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9758. {
  9759. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9760. OpenBookmark(Side, Bookmark);
  9761. }
  9762. //---------------------------------------------------------------------------
  9763. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9764. {
  9765. DoBookmarkClick(osLocal, Sender);
  9766. }
  9767. //---------------------------------------------------------------------------
  9768. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9769. {
  9770. DoBookmarkClick(osRemote, Sender);
  9771. }
  9772. //---------------------------------------------------------------------------
  9773. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9774. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9775. {
  9776. if (BookmarkList != NULL)
  9777. {
  9778. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9779. if (!WinConfiguration->UseLocationProfiles)
  9780. {
  9781. std::unique_ptr<TStringList> Directories(new TStringList());
  9782. Directories->CaseSensitive = (Side == osRemote);
  9783. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9784. {
  9785. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9786. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9787. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9788. {
  9789. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9790. Item->Caption = EscapeHotkey(Directory);
  9791. Item->ShortCut = Bookmark->ShortCut;
  9792. Item->OnClick = OnBookmarkClick;
  9793. Item->Tag = reinterpret_cast<int>(Bookmark);
  9794. Directories->Add(Directory);
  9795. Menu->Add(Item.release());
  9796. }
  9797. }
  9798. }
  9799. else
  9800. {
  9801. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9802. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9803. {
  9804. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9805. if (!Bookmark->Node.IsEmpty())
  9806. {
  9807. Folders->Add(Bookmark->Node);
  9808. }
  9809. }
  9810. for (int Index = 0; Index < Folders->Count; Index++)
  9811. {
  9812. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9813. Item->Caption = Folders->Strings[Index];
  9814. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction->ImageIndex;
  9815. Folders->Objects[Index] = Item.get();
  9816. Menu->Add(Item.release());
  9817. }
  9818. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9819. {
  9820. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9821. TTBCustomItem * Parent;
  9822. if (!Bookmark->Node.IsEmpty())
  9823. {
  9824. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9825. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9826. }
  9827. else
  9828. {
  9829. Parent = Menu;
  9830. }
  9831. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9832. Item->Caption = Bookmark->Name;
  9833. Item->ShortCut = Bookmark->ShortCut;
  9834. Item->OnClick = OnBookmarkClick;
  9835. Item->Tag = reinterpret_cast<int>(Bookmark);
  9836. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9837. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9838. {
  9839. // No hint for location profiles that are actually mere "bookmarks"
  9840. }
  9841. else
  9842. {
  9843. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  9844. UnicodeString LongHint;
  9845. if (!Bookmark->Local.IsEmpty())
  9846. {
  9847. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  9848. }
  9849. if (!Bookmark->Remote.IsEmpty())
  9850. {
  9851. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  9852. }
  9853. AddToList(Hint, LongHint, L"|");
  9854. Item->Hint = Hint;
  9855. }
  9856. Parent->Add(Item.release());
  9857. }
  9858. }
  9859. AddMenuSeparator(Menu);
  9860. }
  9861. }
  9862. //---------------------------------------------------------------------------
  9863. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  9864. {
  9865. Menu->Clear();
  9866. std::unique_ptr<TTBCustomItem> Item;
  9867. Item.reset(new TTBXItem(Owner));
  9868. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  9869. Item->Options = Item->Options << tboDefault;
  9870. Menu->Add(Item.release());
  9871. AddMenuSeparator(Menu);
  9872. if (Terminal != NULL)
  9873. {
  9874. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  9875. }
  9876. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  9877. Item.reset(new TTBXItem(Owner));
  9878. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction : NonVisualDataModule->RemoteAddBookmarkAction;
  9879. Menu->Add(Item.release());
  9880. }
  9881. //---------------------------------------------------------------------------
  9882. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  9883. {
  9884. bool Result = true;
  9885. try
  9886. {
  9887. if (Side == osRemote)
  9888. {
  9889. Terminal->ExceptionOnFail = true;
  9890. }
  9891. try
  9892. {
  9893. DirView(Side)->Path = Path;
  9894. }
  9895. __finally
  9896. {
  9897. if (Side == osRemote)
  9898. {
  9899. Terminal->ExceptionOnFail = false;
  9900. }
  9901. }
  9902. }
  9903. catch (Exception & E)
  9904. {
  9905. if ((Side != osRemote) || Terminal->Active)
  9906. {
  9907. ShowExtendedExceptionEx(Terminal, &E);
  9908. Result = false;
  9909. }
  9910. else
  9911. {
  9912. throw;
  9913. }
  9914. }
  9915. return Result;
  9916. }
  9917. //---------------------------------------------------------------------------
  9918. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  9919. {
  9920. // Make sure there some feedback even when loading tiny local folders
  9921. TInstantOperationVisualizer Visualizer;
  9922. DirView(Side)->ReloadDirectory();
  9923. }
  9924. //---------------------------------------------------------------------------
  9925. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  9926. {
  9927. if (Index == SessionsPageControl->TabIndex)
  9928. {
  9929. CloseSession();
  9930. }
  9931. else
  9932. {
  9933. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  9934. TTerminal * Terminal = GetSessionTabTerminal(TabSheet);
  9935. TTerminalManager * Manager = TTerminalManager::Instance();
  9936. if (!Terminal->Active)
  9937. {
  9938. Manager->FreeTerminal(Terminal);
  9939. }
  9940. else
  9941. {
  9942. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  9943. if (CanCloseQueue(Queue))
  9944. {
  9945. Manager->FreeTerminal(Terminal);
  9946. }
  9947. }
  9948. }
  9949. }
  9950. //---------------------------------------------------------------------------
  9951. void __fastcall TCustomScpExplorerForm::SessionsPageControlCloseButtonClick(TPageControl * /*Sender*/, int Index)
  9952. {
  9953. CloseSessionTab(Index);
  9954. }
  9955. //---------------------------------------------------------------------------
  9956. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  9957. {
  9958. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  9959. {
  9960. TInstantOperationVisualizer Visualizer;
  9961. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  9962. // before we create a new data object
  9963. ClipboardClear();
  9964. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  9965. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  9966. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  9967. // Intermediate variable to WORKAROUND a crash in compiler
  9968. TObjectList * ClipboardFakeMonitors =
  9969. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  9970. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  9971. if (FClipboardDragDropFilesEx.get() == NULL)
  9972. {
  9973. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  9974. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  9975. }
  9976. FClipboardDragDropFilesEx->FileList->Clear();
  9977. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  9978. FClipboardDragDropFilesEx->CopyToClipboard();
  9979. FClipboardTerminal = FTerminal;
  9980. // Need full paths, as cwd can be different once files are pasted
  9981. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  9982. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  9983. {
  9984. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  9985. }
  9986. }
  9987. }
  9988. //---------------------------------------------------------------------------
  9989. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  9990. {
  9991. if (OpenClipboard(0))
  9992. {
  9993. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  9994. CloseClipboard();
  9995. }
  9996. }
  9997. //---------------------------------------------------------------------------
  9998. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  9999. {
  10000. return !FClipboardFakeDirectory.IsEmpty();
  10001. }
  10002. //---------------------------------------------------------------------------
  10003. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  10004. {
  10005. RemoveDir(ApiPath(FClipboardFakeDirectory));
  10006. FClipboardFakeDirectory = UnicodeString();
  10007. FClipboardTerminal = NULL;
  10008. if (FDownloadingFromClipboard)
  10009. {
  10010. // We are called by the monitor, so attempt to release it would deadlonk
  10011. FClipboardFakeMonitorsPendingReset = true;
  10012. }
  10013. else
  10014. {
  10015. FClipboardFakeMonitors.reset(NULL);
  10016. }
  10017. }
  10018. //---------------------------------------------------------------------------
  10019. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10020. {
  10021. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10022. {
  10023. ClipboardStop();
  10024. }
  10025. }
  10026. //---------------------------------------------------------------------------
  10027. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10028. {
  10029. TTerminalManager * Manager = TTerminalManager::Instance();
  10030. Manager->ActiveTerminal = FClipboardTerminal;
  10031. TTransferOperationParam Params;
  10032. Params.TargetDirectory = TargetDirectory;
  10033. Params.DragDrop = DragDrop;
  10034. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10035. }
  10036. //---------------------------------------------------------------------------
  10037. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10038. {
  10039. // It can actually rarelly happen that some random file is created, while we are shutting down the monitor
  10040. // (as it pumps a Windows message queue while being shutted down)
  10041. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10042. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10043. {
  10044. // Can fail as it can be e.g. locked by AV, so we retry that later
  10045. bool Removed = RemoveDir(ApiPath(FileName));
  10046. try
  10047. {
  10048. if (!NonVisualDataModule->Busy)
  10049. {
  10050. DebugAssert(!FClipboardFakeMonitorsPendingReset);
  10051. FClipboardFakeMonitorsPendingReset = false;
  10052. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10053. TAutoFlag Flag(FDownloadingFromClipboard);
  10054. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  10055. }
  10056. }
  10057. __finally
  10058. {
  10059. if (!Removed)
  10060. {
  10061. RemoveDir(ApiPath(FileName));
  10062. }
  10063. }
  10064. }
  10065. }
  10066. //---------------------------------------------------------------------------
  10067. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10068. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10069. {
  10070. TFileMasks::TParams MaskParams;
  10071. MaskParams.Size = Size;
  10072. MaskParams.Modification = Modification;
  10073. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10074. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10075. {
  10076. bool ImplicitMatch;
  10077. if (Iter->FileMask.Matches(FileName, Directory, DirView->PathName, &MaskParams, false, ImplicitMatch) &&
  10078. !ImplicitMatch)
  10079. {
  10080. Color = Iter->Color;
  10081. break;
  10082. }
  10083. }
  10084. }
  10085. //---------------------------------------------------------------------------
  10086. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10087. {
  10088. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10089. if (!DirView->IsEditing())
  10090. {
  10091. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10092. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10093. // while searching
  10094. if (Key == VK_BACK)
  10095. {
  10096. if (!FIncrementalSearch.IsEmpty())
  10097. {
  10098. if (FIncrementalSearch.Length() == 1)
  10099. {
  10100. ResetIncrementalSearch();
  10101. }
  10102. else
  10103. {
  10104. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  10105. IncrementalSearch(NewText, false, false);
  10106. }
  10107. Key = 0;
  10108. }
  10109. }
  10110. }
  10111. }
  10112. //---------------------------------------------------------------------------
  10113. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  10114. {
  10115. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10116. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  10117. {
  10118. // Filter control sequences.
  10119. // When not searching yet, prefer use of the space for toggling file selection
  10120. // (so we cannot incrementally search for a string starting with a space).
  10121. if ((Key > VK_SPACE) ||
  10122. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  10123. {
  10124. IncrementalSearch(FIncrementalSearch + Key, false, false);
  10125. Key = 0;
  10126. }
  10127. }
  10128. }
  10129. //---------------------------------------------------------------------------
  10130. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  10131. {
  10132. if (!FIncrementalSearch.IsEmpty())
  10133. {
  10134. FIncrementalSearch = L"";
  10135. DirView(osCurrent)->UpdateStatusBar();
  10136. }
  10137. }
  10138. //---------------------------------------------------------------------------
  10139. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10140. {
  10141. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  10142. if (Item == NULL)
  10143. {
  10144. MessageBeep(MB_ICONHAND);
  10145. }
  10146. else
  10147. {
  10148. TCustomDirView * ADirView = DirView(osCurrent);
  10149. {
  10150. TAutoNestingCounter Guard(FIncrementalSearching);
  10151. ADirView->FocusItem(Item);
  10152. if (ADirView->NortonLike == nlOff)
  10153. {
  10154. if ((ADirView->Selected != Item) ||
  10155. (ADirView->SelCount > 1))
  10156. {
  10157. ADirView->ClearSelection();
  10158. ADirView->Selected = Item;
  10159. }
  10160. }
  10161. }
  10162. FIncrementalSearch = Text;
  10163. Item->MakeVisible(false);
  10164. TListItem * NextItem = SearchFile(Text, true, Reverse);
  10165. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  10166. ADirView->UpdateStatusBar();
  10167. }
  10168. }
  10169. //---------------------------------------------------------------------------
  10170. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  10171. {
  10172. int Index = Item->Index;
  10173. if (!Reverse)
  10174. {
  10175. Index++;
  10176. if (Index >= Item->Owner->Count)
  10177. {
  10178. Index = 0;
  10179. }
  10180. }
  10181. else
  10182. {
  10183. Index--;
  10184. if (Index < 0)
  10185. {
  10186. Index = Item->Owner->Count - 1;
  10187. }
  10188. }
  10189. return Item->Owner->Item[Index];
  10190. }
  10191. //---------------------------------------------------------------------------
  10192. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10193. {
  10194. TCustomDirView * ADirView = DirView(osCurrent);
  10195. if (ADirView->Items->Count == 0)
  10196. {
  10197. return NULL;
  10198. }
  10199. else
  10200. {
  10201. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  10202. TListItem * Item = CurrentItem;
  10203. if (SkipCurrent)
  10204. {
  10205. Item = DebugNotNull(GetNextFile(Item, Reverse));
  10206. }
  10207. while (true)
  10208. {
  10209. bool Matches = false;
  10210. switch (WinConfiguration->PanelSearch)
  10211. {
  10212. case isNameStartOnly:
  10213. Matches = ContainsTextSemiCaseSensitive(Item->Caption.SubString(1, Text.Length()), Text);
  10214. break;
  10215. case isName:
  10216. Matches = ContainsTextSemiCaseSensitive(Item->Caption, Text);
  10217. break;
  10218. case isAll:
  10219. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  10220. int Index = 0;
  10221. while ((Index < ColCount) && !Matches)
  10222. {
  10223. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  10224. if (Visible)
  10225. {
  10226. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  10227. }
  10228. Index++;
  10229. }
  10230. break;
  10231. }
  10232. if (Matches)
  10233. {
  10234. return Item;
  10235. }
  10236. Item = GetNextFile(Item, Reverse);
  10237. if (Item == CurrentItem)
  10238. {
  10239. return NULL;
  10240. }
  10241. }
  10242. }
  10243. }
  10244. //---------------------------------------------------------------------------
  10245. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10246. {
  10247. ResetIncrementalSearch();
  10248. }
  10249. //---------------------------------------------------------------------------
  10250. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10251. {
  10252. if (Message.CharCode == VK_TAB)
  10253. {
  10254. if (!FIncrementalSearch.IsEmpty())
  10255. {
  10256. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10257. bool Reverse = Shift.Contains(ssShift);
  10258. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10259. Message.Result = 1;
  10260. return;
  10261. }
  10262. }
  10263. else if (Message.CharCode == VK_ESCAPE)
  10264. {
  10265. if (!FIncrementalSearch.IsEmpty())
  10266. {
  10267. ResetIncrementalSearch();
  10268. Message.Result = 1;
  10269. return;
  10270. }
  10271. }
  10272. TForm::Dispatch(&Message);
  10273. }
  10274. //---------------------------------------------------------------------------
  10275. void __fastcall TCustomScpExplorerForm::Deactivate()
  10276. {
  10277. TForm::Deactivate();
  10278. // E.g. when switching to an internal editor window
  10279. ResetIncrementalSearch();
  10280. }
  10281. //---------------------------------------------------------------------------
  10282. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10283. {
  10284. // When switching to another application
  10285. ResetIncrementalSearch();
  10286. }
  10287. //---------------------------------------------------------------------------
  10288. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10289. {
  10290. ResetIncrementalSearch();
  10291. }
  10292. //---------------------------------------------------------------------------
  10293. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10294. {
  10295. if (FIncrementalSearching <= 0)
  10296. {
  10297. ResetIncrementalSearch();
  10298. }
  10299. }
  10300. //---------------------------------------------------------------------------
  10301. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10302. {
  10303. CountClicksForWindowPrint(this);
  10304. }
  10305. //---------------------------------------------------------------------------
  10306. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10307. {
  10308. CenterReconnectToolbar();
  10309. }
  10310. //---------------------------------------------------------------------------
  10311. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10312. {
  10313. if (RemoteDirView->ItemFocused != NULL)
  10314. {
  10315. RemoteDirView->ItemFocused->Selected = true;
  10316. }
  10317. }
  10318. //---------------------------------------------------------------------------
  10319. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  10320. {
  10321. if (!FQueueStatusUpdating)
  10322. {
  10323. bool Refresh;
  10324. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  10325. {
  10326. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  10327. }
  10328. else
  10329. {
  10330. if (FQueueFileList->GetCount() > 0)
  10331. {
  10332. FQueueFileList->Clear();
  10333. Refresh = true;
  10334. }
  10335. }
  10336. int Count = FQueueFileList->GetCount();
  10337. if (QueueFileList->Items->Count != Count)
  10338. {
  10339. DebugAssert(Refresh);
  10340. QueueFileList->Items->Count = Count;
  10341. }
  10342. if (Refresh)
  10343. {
  10344. QueueFileList->Invalidate();
  10345. }
  10346. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  10347. }
  10348. }
  10349. //---------------------------------------------------------------------------
  10350. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  10351. {
  10352. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  10353. }
  10354. //---------------------------------------------------------------------------
  10355. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  10356. {
  10357. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  10358. // See TSynchronizeChecklistDialog::UpdateTimer
  10359. if (QueueFileList->Items->Count > 0)
  10360. {
  10361. QueueFileList->Items->Item[0]->MakeVisible(false);
  10362. }
  10363. UpdateQueueFileList();
  10364. }
  10365. //---------------------------------------------------------------------------
  10366. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  10367. {
  10368. Active = QueueView3->Focused() || QueueFileList->Focused();
  10369. }
  10370. //---------------------------------------------------------------------------
  10371. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  10372. {
  10373. QueueLabelUpdateStatus();
  10374. }
  10375. //---------------------------------------------------------------------------
  10376. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  10377. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  10378. {
  10379. int State = FQueueFileList->GetState(Item->Index);
  10380. if (State == qfsProcessed)
  10381. {
  10382. Sender->Canvas->Font->Color = clGrayText;
  10383. }
  10384. }
  10385. //---------------------------------------------------------------------------
  10386. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  10387. {
  10388. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  10389. QueueFileList->Column[0]->Width =
  10390. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  10391. }
  10392. //---------------------------------------------------------------------------
  10393. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  10394. {
  10395. QueueFileListColumnAutoSize();
  10396. }
  10397. //---------------------------------------------------------------------------
  10398. void __fastcall TCustomScpExplorerForm::CloseApp()
  10399. {
  10400. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  10401. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  10402. // So we clear the flag here.
  10403. DebugAssert(NonVisualDataModule->Busy);
  10404. NonVisualDataModule->EndBusy();
  10405. try
  10406. {
  10407. Close();
  10408. }
  10409. __finally
  10410. {
  10411. NonVisualDataModule->StartBusy();
  10412. }
  10413. }
  10414. //---------------------------------------------------------------------------
  10415. bool __fastcall TCustomScpExplorerForm::IsActiveTerminal(TTerminal * Terminal)
  10416. {
  10417. return (Terminal != NULL) && Terminal->Active;
  10418. }
  10419. //---------------------------------------------------------------------------
  10420. bool __fastcall TCustomScpExplorerForm::HasActiveTerminal()
  10421. {
  10422. return IsActiveTerminal(Terminal);
  10423. }