CustomScpExplorer.cpp 365 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077
  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 (!TFileOperationProgressType::IsIndeterminateOperation(ProgressData->Operation))
  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, its enough to have in 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.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  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 ((Operation == foCopy) || (Operation == foMove))
  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 ((Operation == foCopy) || (Operation == foMove))
  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 ((Operation == foCopy) || (Operation == foMove))
  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 to detect closed connection and automatically reconnect it while waiting for changes
  4310. // while "keeping remote directory up to date"
  4311. StandaloneOperation)
  4312. {
  4313. FEditorManager->Check();
  4314. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4315. // that needs to know if queue view is visible (and it may be closed after queue update)
  4316. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4317. }
  4318. if (FShowing)
  4319. {
  4320. if (!NonVisualDataModule->Busy &&
  4321. // Menu is opened or drag&drop is going on
  4322. (Mouse->Capture == NULL))
  4323. {
  4324. if (FRefreshRemoteDirectory)
  4325. {
  4326. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4327. {
  4328. Terminal->RefreshDirectory();
  4329. }
  4330. FRefreshRemoteDirectory = false;
  4331. }
  4332. if (FRefreshLocalDirectory)
  4333. {
  4334. ReloadLocalDirectory();
  4335. FRefreshLocalDirectory = false;
  4336. }
  4337. if (WinConfiguration->RefreshRemotePanel)
  4338. {
  4339. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4340. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4341. {
  4342. RemoteDirView->ReloadDirectory();
  4343. }
  4344. }
  4345. }
  4346. }
  4347. if (FShowing || StandaloneOperation)
  4348. {
  4349. if (FQueueStatusInvalidated)
  4350. {
  4351. UpdateQueueStatus(false);
  4352. }
  4353. RefreshQueueItems();
  4354. }
  4355. if (FShowing)
  4356. {
  4357. UpdateStatusBar();
  4358. }
  4359. if (FClipboardFakeMonitorsPendingReset && !FDownloadingFromClipboard)
  4360. {
  4361. FClipboardFakeMonitorsPendingReset = false;
  4362. FClipboardFakeMonitors.reset(NULL);
  4363. }
  4364. FIgnoreNextDialogChar = 0;
  4365. }
  4366. //---------------------------------------------------------------------------
  4367. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4368. {
  4369. try
  4370. {
  4371. FUserActionTimer->Enabled = false;
  4372. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4373. {
  4374. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4375. {
  4376. FPendingQueueActionItem->ProcessUserAction();
  4377. }
  4378. }
  4379. }
  4380. __finally
  4381. {
  4382. FPendingQueueActionItem = NULL;
  4383. }
  4384. }
  4385. //---------------------------------------------------------------------------
  4386. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4387. {
  4388. if (WinConfiguration->MinimizeToTray)
  4389. {
  4390. UpdateTrayIcon();
  4391. FTrayIcon->Visible = true;
  4392. if (Visible)
  4393. {
  4394. ShowWindow(Handle, SW_HIDE);
  4395. }
  4396. }
  4397. }
  4398. //---------------------------------------------------------------------------
  4399. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4400. {
  4401. // WORKAROUND
  4402. // When restoring maximized window from minimization,
  4403. // rarely some controls do not align properly.
  4404. // Two instances seen (both for Commander):
  4405. // - When restoring, window is temporarily narrower (not maximized),
  4406. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4407. // Once maximized already, top dock shinks back, but the session PageControl,
  4408. // do not align up, leaving space between TopDock and PageControl.
  4409. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4410. for (int Index = 0; Index < ControlCount; Index++)
  4411. {
  4412. RealignControl(Controls[Index]);
  4413. }
  4414. if (FTrayIcon->Visible)
  4415. {
  4416. FTrayIcon->Visible = false;
  4417. if (Visible)
  4418. {
  4419. ShowWindow(Handle, SW_SHOW);
  4420. }
  4421. }
  4422. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4423. {
  4424. FNeedSession = false;
  4425. NonVisualDataModule->StartBusy();
  4426. try
  4427. {
  4428. NeedSession(true);
  4429. }
  4430. __finally
  4431. {
  4432. NonVisualDataModule->EndBusy();
  4433. }
  4434. }
  4435. }
  4436. //---------------------------------------------------------------------------
  4437. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4438. {
  4439. FTrayIcon->Hint = GetMainForm()->Caption;
  4440. }
  4441. //---------------------------------------------------------------------------
  4442. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4443. {
  4444. UpdateTrayIcon();
  4445. }
  4446. //---------------------------------------------------------------------------
  4447. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4448. {
  4449. // workaround
  4450. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4451. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4452. // But WM_ACTIVATEAPP message can be received even when
  4453. // the main window is minimized to the tray and internal editor window is focused
  4454. // (after another application was previously active)
  4455. if (::IsIconic(Handle))
  4456. {
  4457. if (!IsAppIconic())
  4458. {
  4459. SetAppIconic(true);
  4460. }
  4461. }
  4462. ::ApplicationRestore();
  4463. Application->BringToFront();
  4464. }
  4465. //---------------------------------------------------------------------------
  4466. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4467. {
  4468. RestoreApp();
  4469. }
  4470. //---------------------------------------------------------------------------
  4471. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  4472. {
  4473. if (OpenInNewWindow())
  4474. {
  4475. ExecuteNewInstance(SessionUrl);
  4476. }
  4477. else
  4478. {
  4479. TTerminalManager::Instance()->NewSession(SessionUrl);
  4480. }
  4481. }
  4482. //---------------------------------------------------------------------------
  4483. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4484. {
  4485. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->SessionName;
  4486. StoredSessions->NewSession(SessionName, SessionData);
  4487. // modified only, explicit
  4488. StoredSessions->Save(false, true);
  4489. // encode session name because of slashes in hierarchical sessions
  4490. return EncodeUrlString(SessionName);
  4491. }
  4492. //---------------------------------------------------------------------------
  4493. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4494. {
  4495. // current working directories become defaults here, what is not right
  4496. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4497. return SaveHiddenDuplicateSession(SessionData.get());
  4498. }
  4499. //---------------------------------------------------------------------------
  4500. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4501. {
  4502. if (OpenInNewWindow())
  4503. {
  4504. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4505. }
  4506. else
  4507. {
  4508. // current working directories become defaults here, what is not right
  4509. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4510. TTerminalManager * Manager = TTerminalManager::Instance();
  4511. TManagedTerminal * ATerminal = Manager->NewManagedTerminal(SessionData.get());
  4512. // We definitelly want this
  4513. ATerminal->Disconnected = Terminal->Disconnected;
  4514. // Not sure about these two
  4515. ATerminal->Permanent = Terminal->Permanent;
  4516. ATerminal->DisconnectedTemporarily = Terminal->DisconnectedTemporarily;
  4517. Manager->ActiveTerminal = ATerminal;
  4518. }
  4519. }
  4520. //---------------------------------------------------------------------------
  4521. void __fastcall TCustomScpExplorerForm::RenameSession()
  4522. {
  4523. UnicodeString Name = Terminal->SessionData->SessionName;
  4524. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME))
  4525. {
  4526. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4527. TSessionData::ValidateName(Name);
  4528. // This is inconsistent with how color (for example) is handled.
  4529. Terminal->SessionData->Name = Name;
  4530. UpdateControls();
  4531. // Add/Remove distinguishing paths from sessions of the same name.
  4532. DoTerminalListChanged();
  4533. }
  4534. }
  4535. //---------------------------------------------------------------------------
  4536. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4537. {
  4538. DebugAssert(Queue != NULL);
  4539. bool Result = Queue->IsEmpty;
  4540. if (!Result)
  4541. {
  4542. SetFocus();
  4543. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4544. }
  4545. return Result;
  4546. }
  4547. //---------------------------------------------------------------------------
  4548. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4549. {
  4550. return CanCloseQueue(FQueue);
  4551. }
  4552. //---------------------------------------------------------------------------
  4553. void __fastcall TCustomScpExplorerForm::CloseSession()
  4554. {
  4555. if (CanCloseQueue())
  4556. {
  4557. TTerminalManager::Instance()->FreeActiveTerminal();
  4558. }
  4559. }
  4560. //---------------------------------------------------------------------------
  4561. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4562. {
  4563. if (CanCloseQueue())
  4564. {
  4565. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4566. }
  4567. }
  4568. //---------------------------------------------------------------------------
  4569. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4570. {
  4571. DetachTerminal(Terminal);
  4572. RemoteDirView->Terminal = Terminal;
  4573. UpdateRemotePathComboBox(false);
  4574. UpdateControls();
  4575. }
  4576. //---------------------------------------------------------------------------
  4577. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4578. {
  4579. if (DebugAlwaysTrue(!Terminal->Active))
  4580. {
  4581. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4582. }
  4583. }
  4584. //---------------------------------------------------------------------------
  4585. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4586. {
  4587. if (OpenInNewWindow())
  4588. {
  4589. // encode session name because of slashes in hierarchical sessions
  4590. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4591. }
  4592. else
  4593. {
  4594. TTerminalManager * Manager = TTerminalManager::Instance();
  4595. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4596. Manager->ActiveTerminal = Terminal;
  4597. }
  4598. }
  4599. //---------------------------------------------------------------------------
  4600. void TCustomScpExplorerForm::DoOpenFolderOrWorkspace(
  4601. const UnicodeString & Name, bool ConnectFirstTerminal, bool CheckMaxSessions)
  4602. {
  4603. TTerminalManager * Manager = TTerminalManager::Instance();
  4604. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4605. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4606. if (!CheckMaxSessions || (DataList->Count <= WinConfiguration->MaxSessions))
  4607. {
  4608. TManagedTerminal * FirstTerminal = Manager->NewTerminals(DataList.get());
  4609. // FirstTerminal can be null, if some of the
  4610. if (!ConnectFirstTerminal && (FirstTerminal != NULL))
  4611. {
  4612. FirstTerminal->Disconnected = true;
  4613. FirstTerminal->DisconnectedTemporarily = true;
  4614. }
  4615. Manager->ActiveTerminal = FirstTerminal;
  4616. }
  4617. }
  4618. //---------------------------------------------------------------------------
  4619. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4620. {
  4621. if (OpenInNewWindow())
  4622. {
  4623. ExecuteNewInstance(Name);
  4624. }
  4625. else
  4626. {
  4627. DoOpenFolderOrWorkspace(Name, true, false);
  4628. }
  4629. }
  4630. //---------------------------------------------------------------------------
  4631. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4632. bool &CanClose)
  4633. {
  4634. // See the comment in CloseApp()
  4635. if (NonVisualDataModule->Busy)
  4636. {
  4637. CanClose = false;
  4638. }
  4639. else
  4640. {
  4641. TTerminalManager * Manager = TTerminalManager::Instance();
  4642. int ActiveSessions = 0;
  4643. for (int Index = 0; Index < Manager->Count; Index++)
  4644. {
  4645. if (Manager->Terminals[Index]->Active)
  4646. {
  4647. ActiveSessions++;
  4648. }
  4649. }
  4650. // Confirm, when there's at least one active remote session or
  4651. // when there are multiple (inactive or local) sessions and auto workspace saving is not enabled.
  4652. if (((ActiveSessions > 0) || ((Manager->Count > 1) && !WinConfiguration->AutoSaveWorkspace)) &&
  4653. WinConfiguration->ConfirmClosingSession)
  4654. {
  4655. unsigned int Result;
  4656. TMessageParams Params(mpNeverAskAgainCheck);
  4657. UnicodeString Message;
  4658. int Answers = qaOK | qaCancel;
  4659. // The current session is the only active session
  4660. if ((ActiveSessions == 1) && (Terminal != NULL) && Terminal->Active)
  4661. {
  4662. if (!WinConfiguration->AutoSaveWorkspace)
  4663. {
  4664. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  4665. }
  4666. else
  4667. {
  4668. Message = LoadStr(CLOSE_SESSION);
  4669. }
  4670. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  4671. }
  4672. // Multiple active sessions or one active session, but it's not the current one
  4673. else if (ActiveSessions > 0)
  4674. {
  4675. if (!WinConfiguration->AutoSaveWorkspace)
  4676. {
  4677. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  4678. }
  4679. else
  4680. {
  4681. Message = LoadStr(CLOSE_SESSIONS);
  4682. }
  4683. }
  4684. // Multiple tabs, but all are intactive or local, and saving of worspace is not enabled
  4685. else
  4686. {
  4687. DebugAlwaysTrue(Manager->Count > 1);
  4688. DebugAlwaysTrue(!WinConfiguration->AutoSaveWorkspace);
  4689. Message = LoadStr(CLOSE_WORKSPACE);
  4690. }
  4691. UnicodeString Note;
  4692. if (WinConfiguration->AutoSaveWorkspace)
  4693. {
  4694. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  4695. }
  4696. else
  4697. {
  4698. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  4699. Answers = qaYes | qaNo | qaCancel;
  4700. }
  4701. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  4702. SetFocus();
  4703. Result = MessageDialog(Message, qtConfirmation,
  4704. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  4705. if (Result == qaNeverAskAgain)
  4706. {
  4707. WinConfiguration->ConfirmClosingSession = false;
  4708. }
  4709. if (Result == qaNo)
  4710. {
  4711. CanClose = SaveWorkspace(true);
  4712. // note that the workspace will be saved redundatly again from FormClose
  4713. }
  4714. else
  4715. {
  4716. CanClose =
  4717. (Result == qaOK) ||
  4718. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4719. (Result == qaNeverAskAgain);
  4720. }
  4721. }
  4722. if (CanClose && HasActiveTerminal())
  4723. {
  4724. CanClose = CanCloseQueue();
  4725. }
  4726. }
  4727. if (CanClose)
  4728. {
  4729. CanClose =
  4730. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4731. FEditorManager->CloseExternalFilesWithoutProcess();
  4732. if (CanClose)
  4733. {
  4734. while (CanClose && (FLocalEditors->Count > 0))
  4735. {
  4736. int PrevCount = FLocalEditors->Count;
  4737. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4738. CanClose = (FLocalEditors->Count < PrevCount);
  4739. }
  4740. if (CanClose)
  4741. {
  4742. CanClose = FEditorManager->Empty(true);
  4743. if (!CanClose)
  4744. {
  4745. SetFocus();
  4746. CanClose =
  4747. (MessageDialog(
  4748. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4749. }
  4750. }
  4751. }
  4752. }
  4753. }
  4754. //---------------------------------------------------------------------------
  4755. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4756. {
  4757. TForm * Form = dynamic_cast<TForm *>(Sender);
  4758. DebugAssert(Form != NULL);
  4759. Form->Close();
  4760. }
  4761. //---------------------------------------------------------------------------
  4762. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4763. {
  4764. TForm * Form = dynamic_cast<TForm *>(Sender);
  4765. delete Form;
  4766. }
  4767. //---------------------------------------------------------------------------
  4768. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4769. {
  4770. while (FLocalEditors->Count > 0)
  4771. {
  4772. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4773. }
  4774. }
  4775. //---------------------------------------------------------------------------
  4776. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4777. TObject *Sender)
  4778. {
  4779. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4780. try
  4781. {
  4782. SetProperties(osRemote, FileList);
  4783. }
  4784. __finally
  4785. {
  4786. delete FileList;
  4787. }
  4788. }
  4789. //---------------------------------------------------------------------------
  4790. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4791. {
  4792. if (value)
  4793. {
  4794. if (Component == fcCustomCommandsBand)
  4795. {
  4796. UpdateCustomCommandsToolbar();
  4797. }
  4798. if (Component == fcQueueFileList)
  4799. {
  4800. DebugAssert(!QueueFileListSplitter->Visible);
  4801. AdjustQueueLayout();
  4802. UpdateQueueFileList();
  4803. }
  4804. }
  4805. }
  4806. //---------------------------------------------------------------------------
  4807. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  4808. {
  4809. int MinHeightLimit =
  4810. GetStaticQueuePanelComponentsHeight() +
  4811. QueueFileListSplitter->Height +
  4812. QueueFileList->Height +
  4813. GetMinQueueViewHeight();
  4814. if (QueuePanel->ClientHeight < MinHeightLimit)
  4815. {
  4816. QueuePanel->ClientHeight = MinHeightLimit;
  4817. }
  4818. }
  4819. //---------------------------------------------------------------------------
  4820. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4821. {
  4822. TControl * Control = GetComponent(Component);
  4823. DebugAssert(Control);
  4824. bool Changed = (Control->Visible != value);
  4825. if (Changed)
  4826. {
  4827. ComponentShowing(Component, value);
  4828. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4829. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4830. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4831. if (value)
  4832. {
  4833. int RemainingHeight = Control->Parent->ClientHeight;
  4834. int RemainingWidth = Control->Parent->ClientWidth;
  4835. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4836. {
  4837. TControl * ChildControl = Control->Parent->Controls[i];
  4838. if (ChildControl->Visible)
  4839. {
  4840. switch (ChildControl->Align)
  4841. {
  4842. case alTop:
  4843. case alBottom:
  4844. RemainingHeight -= ChildControl->Height;
  4845. break;
  4846. case alLeft:
  4847. case alRight:
  4848. RemainingWidth -= ChildControl->Width;
  4849. break;
  4850. }
  4851. }
  4852. }
  4853. int Reserve = ScaleByTextHeight(this, 32);
  4854. // queue in explorer, trees in commander
  4855. if (Control->Height > RemainingHeight - Reserve)
  4856. {
  4857. Control->Height = RemainingHeight / 2;
  4858. }
  4859. if (Control->Width > RemainingWidth - Reserve)
  4860. {
  4861. Control->Width = RemainingWidth / 2;
  4862. }
  4863. }
  4864. Control->Visible = value;
  4865. if (WasFocused && Visible)
  4866. {
  4867. DirView(osCurrent)->SetFocus();
  4868. }
  4869. FixControlsPlacement();
  4870. }
  4871. }
  4872. //---------------------------------------------------------------------------
  4873. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4874. {
  4875. TControl * Control = GetComponent(Component);
  4876. if (Control == NULL)
  4877. {
  4878. return false;
  4879. }
  4880. else
  4881. {
  4882. return Control->Visible;
  4883. }
  4884. }
  4885. //---------------------------------------------------------------------------
  4886. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4887. {
  4888. bool Result;
  4889. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4890. {
  4891. Result = !IsUWP();
  4892. }
  4893. else
  4894. {
  4895. Result = true;
  4896. }
  4897. return Result;
  4898. }
  4899. //---------------------------------------------------------------------------
  4900. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4901. {
  4902. if (DirView(osOther)->ItemFocused != NULL)
  4903. {
  4904. DirView(osOther)->ItemFocused->MakeVisible(false);
  4905. }
  4906. QueueSplitter->Visible = QueuePanel->Visible;
  4907. QueueFileListSplitter->Visible = QueueFileList->Visible;
  4908. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  4909. TControl * QueueControlsOrder[] =
  4910. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  4911. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  4912. }
  4913. //---------------------------------------------------------------------------
  4914. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4915. {
  4916. switch (Component) {
  4917. case fcStatusBar: return RemoteStatusBar;
  4918. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4919. case fcQueueView: return QueuePanel;
  4920. case fcQueueToolbar: return QueueDock;
  4921. case fcRemoteTree: return RemoteDrivePanel;
  4922. case fcSessionsTabs: return SessionsPageControl;
  4923. case fcQueueFileList: return QueueFileList;
  4924. default: return NULL;
  4925. }
  4926. }
  4927. //---------------------------------------------------------------------------
  4928. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4929. TObject *Sender, TListColumn *Column, TPoint &Point)
  4930. {
  4931. DebugAssert(NonVisualDataModule && Column && Sender);
  4932. NonVisualDataModule->ListColumn = Column;
  4933. TPopupMenu * DirViewColumnMenu;
  4934. if (dynamic_cast<TUnixDirView *>(Sender) != NULL)
  4935. {
  4936. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4937. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4938. (Column->Index == uvName);
  4939. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4940. (Column->Index == uvSize);
  4941. }
  4942. else
  4943. {
  4944. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4945. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4946. (Column->Index == dvName);
  4947. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4948. (Column->Index == dvSize);
  4949. }
  4950. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  4951. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  4952. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  4953. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4954. }
  4955. //---------------------------------------------------------------------------
  4956. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4957. TObject *Sender, TListItem *Item, bool &AllowExec)
  4958. {
  4959. DoDirViewExecFile(Sender, Item, AllowExec);
  4960. }
  4961. //---------------------------------------------------------------------------
  4962. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4963. TListItem * Item, bool & AllowExec)
  4964. {
  4965. DebugAssert(Sender && Item && Configuration);
  4966. DebugAssert(AllowExec);
  4967. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  4968. bool Remote = (ADirView == DirView(osRemote)) && !IsSideLocalBrowser(osRemote);
  4969. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles();
  4970. TOperationSide Side = (Remote ? osRemote : osLocal);
  4971. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  4972. // on files only (not directories)
  4973. // and only when symlinks are resolved (apply to remote panel only)
  4974. if (!ADirView->ItemIsDirectory(Item) &&
  4975. (ResolvedSymlinks || FForceExecution))
  4976. {
  4977. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  4978. !FForceExecution && ResolvedSymlinks)
  4979. {
  4980. if (WinConfiguration->DoubleClickAction == dcaCopy)
  4981. {
  4982. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  4983. UnlockWindow();
  4984. try
  4985. {
  4986. ExecuteFileOperation(
  4987. foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  4988. }
  4989. __finally
  4990. {
  4991. LockWindow();
  4992. }
  4993. AllowExec = false;
  4994. }
  4995. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  4996. {
  4997. if (!Remote || !WinConfiguration->DisableOpenEdit)
  4998. {
  4999. ExecuteFile(Side, efDefaultEditor);
  5000. AllowExec = false;
  5001. }
  5002. }
  5003. else
  5004. {
  5005. DebugFail();
  5006. }
  5007. }
  5008. // if we have not done anything special, fall back to default behavior
  5009. if (AllowExec)
  5010. {
  5011. if (Remote && !WinConfiguration->DisableOpenEdit)
  5012. {
  5013. ExecuteFile(osRemote, efShell);
  5014. AllowExec = false;
  5015. }
  5016. }
  5017. }
  5018. }
  5019. //---------------------------------------------------------------------------
  5020. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5021. {
  5022. return ((Side == osRemote) || (Side == osCurrent));
  5023. }
  5024. //---------------------------------------------------------------------------
  5025. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5026. {
  5027. DebugFail();
  5028. }
  5029. //---------------------------------------------------------------------------
  5030. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5031. {
  5032. DebugFail();
  5033. }
  5034. //---------------------------------------------------------------------------
  5035. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5036. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5037. {
  5038. TSynchronizeParamType Params;
  5039. Params.LocalDirectory = LocalDirectory;
  5040. Params.RemoteDirectory = RemoteDirectory;
  5041. int UnusedParams =
  5042. (GUIConfiguration->SynchronizeParams &
  5043. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5044. TTerminal::spCaseSensitive));
  5045. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5046. Params.Options = GUIConfiguration->SynchronizeOptions;
  5047. bool SaveSettings = false;
  5048. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5049. &DoSynchronizeTooManyDirectories);
  5050. DebugAssert(FSynchronizeController == NULL);
  5051. FSynchronizeController = &Controller;
  5052. bool Result;
  5053. try
  5054. {
  5055. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5056. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5057. int Options =
  5058. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5059. DebugAssert(FOnFeedSynchronizeError == NULL);
  5060. DebugAssert(FOnSynchronizeAbort == NULL);
  5061. DebugAssert(FSynchronizeTerminal == NULL);
  5062. FSynchronizeTerminal = Terminal;
  5063. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5064. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5065. FOnFeedSynchronizeError, FOnSynchronizeAbort, SynchronizeInNewWindow, UseDefaults);
  5066. if (Result)
  5067. {
  5068. if (SaveSettings)
  5069. {
  5070. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5071. GUIConfiguration->SynchronizeOptions = Params.Options;
  5072. }
  5073. else
  5074. {
  5075. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5076. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5077. FLAGSET(Params.Options, soSynchronize))
  5078. {
  5079. GUIConfiguration->SynchronizeOptions =
  5080. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5081. soSynchronize;
  5082. }
  5083. }
  5084. LocalDirectory = Params.LocalDirectory;
  5085. RemoteDirectory = Params.RemoteDirectory;
  5086. }
  5087. }
  5088. __finally
  5089. {
  5090. FSynchronizeController = NULL;
  5091. DebugAssert(FOnFeedSynchronizeError == NULL);
  5092. FOnFeedSynchronizeError = NULL;
  5093. DebugAssert(FOnSynchronizeAbort == NULL);
  5094. FOnSynchronizeAbort = NULL;
  5095. FSynchronizeTerminal = NULL;
  5096. }
  5097. return Result;
  5098. }
  5099. //---------------------------------------------------------------------------
  5100. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5101. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5102. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5103. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5104. TSynchronizeOptions * Options, bool Full)
  5105. {
  5106. // Keep-up-to-date mode
  5107. if (Terminal->Status == ssOpened)
  5108. {
  5109. try
  5110. {
  5111. int PParams = Params.Params;
  5112. if (!Full)
  5113. {
  5114. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5115. if (FLAGSET(Params.Options, soRecurse))
  5116. {
  5117. PParams |= TTerminal::spSubDirs;
  5118. }
  5119. }
  5120. else
  5121. {
  5122. // if keepuptodate is non-recursive,
  5123. // full sync before has to be non-recursive as well
  5124. if (FLAGCLEAR(Params.Options, soRecurse))
  5125. {
  5126. PParams |= TTerminal::spNoRecurse;
  5127. }
  5128. }
  5129. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5130. PParams, Checklist, Options);
  5131. }
  5132. catch (Exception & E)
  5133. {
  5134. if (!Terminal->Active)
  5135. {
  5136. ShowExtendedExceptionEx(Terminal, &E);
  5137. // Do not abort the "keep up to date" when the session was succesfully reconnected.
  5138. if (!Terminal->Active)
  5139. {
  5140. throw;
  5141. }
  5142. }
  5143. else
  5144. {
  5145. if (FLAGSET(Params.Options, soContinueOnError) &&
  5146. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5147. {
  5148. // noop, already logged in MoreMessageDialog
  5149. }
  5150. else
  5151. {
  5152. // We get mostly EAbort here, so this is noop.
  5153. // Exception is an error when listing directories while looking for differences.
  5154. ShowExtendedExceptionEx(Terminal, &E);
  5155. throw;
  5156. }
  5157. }
  5158. }
  5159. }
  5160. }
  5161. //---------------------------------------------------------------------------
  5162. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5163. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5164. const UnicodeString ErrorStr)
  5165. {
  5166. if (!Directory.IsEmpty())
  5167. {
  5168. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5169. }
  5170. else
  5171. {
  5172. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5173. }
  5174. }
  5175. //---------------------------------------------------------------------------
  5176. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5177. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5178. {
  5179. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5180. {
  5181. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5182. }
  5183. else
  5184. {
  5185. TMessageParams Params(mpNeverAskAgainCheck);
  5186. unsigned int Result = MessageDialog(
  5187. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5188. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5189. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5190. {
  5191. MaxDirectories *= 2;
  5192. if (Result == qaNeverAskAgain)
  5193. {
  5194. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5195. }
  5196. }
  5197. else
  5198. {
  5199. Abort();
  5200. }
  5201. }
  5202. }
  5203. //---------------------------------------------------------------------------
  5204. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5205. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5206. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5207. TSynchronizeOptions * Options)
  5208. {
  5209. // Keep-up-to-date mode
  5210. DebugAssert(!FAutoOperation);
  5211. void * BatchStorage;
  5212. BatchStart(BatchStorage);
  5213. FAutoOperation = true;
  5214. bool AnyOperation = false;
  5215. TDateTime StartTime = Now();
  5216. TSynchronizeChecklist * AChecklist = NULL;
  5217. try
  5218. {
  5219. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5220. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5221. {
  5222. FSynchronizeProgressForm->Start();
  5223. }
  5224. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5225. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5226. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5227. Options);
  5228. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5229. SAFE_DESTROY(FSynchronizeProgressForm);
  5230. AnyOperation = (AChecklist->CheckedCount > 0);
  5231. if (AnyOperation)
  5232. {
  5233. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5234. CreateProgressForm(&SynchronizeProgress);
  5235. try
  5236. {
  5237. Terminal->SynchronizeApply(
  5238. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5239. SynchronizeProcessedItem, NULL, NULL, NULL);
  5240. }
  5241. __finally
  5242. {
  5243. DestroyProgressForm();
  5244. }
  5245. }
  5246. }
  5247. __finally
  5248. {
  5249. if (Checklist == NULL)
  5250. {
  5251. delete AChecklist;
  5252. }
  5253. else
  5254. {
  5255. *Checklist = AChecklist;
  5256. }
  5257. FAutoOperation = false;
  5258. SAFE_DESTROY(FSynchronizeProgressForm);
  5259. BatchEnd(BatchStorage);
  5260. ReloadLocalDirectory();
  5261. if (AnyOperation)
  5262. {
  5263. OperationComplete(StartTime);
  5264. }
  5265. }
  5266. }
  5267. //---------------------------------------------------------------------------
  5268. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5269. {
  5270. // can be called from command line
  5271. return Visible &&
  5272. ((DirView(osRemote)->SelCount > 0) ||
  5273. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5274. }
  5275. //---------------------------------------------------------------------------
  5276. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5277. {
  5278. LogSynchronizeEvent(Terminal, Message);
  5279. }
  5280. //---------------------------------------------------------------------------
  5281. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5282. int Params, TSynchronizeOptions & Options)
  5283. {
  5284. DebugAssert(!IsLocalBrowserMode());
  5285. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5286. {
  5287. Options.Filter = new TStringList();
  5288. Options.Filter->CaseSensitive = false;
  5289. Options.Filter->Duplicates = Types::dupAccept;
  5290. if (DirView(osRemote)->SelCount > 0)
  5291. {
  5292. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5293. }
  5294. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5295. {
  5296. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5297. }
  5298. Options.Filter->Sort();
  5299. }
  5300. }
  5301. //---------------------------------------------------------------------------
  5302. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5303. {
  5304. TCopyParamType Defaults;
  5305. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5306. ConfigStorage->AccessMode = smRead;
  5307. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5308. {
  5309. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5310. }
  5311. ConfigStorage.reset(NULL);
  5312. std::unique_ptr<TStringList> Options(new TStringList());
  5313. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5314. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5315. UnicodeString Result;
  5316. if (Options->Count > 0)
  5317. {
  5318. Result =
  5319. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5320. }
  5321. return Result;
  5322. }
  5323. //---------------------------------------------------------------------------
  5324. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5325. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5326. {
  5327. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5328. UnicodeString AdditionalParams =
  5329. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5330. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5331. SerializeCopyParamForCommandLine(CopyParams),
  5332. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5333. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5334. ExecuteNewInstance(SessionName, AdditionalParams);
  5335. }
  5336. //---------------------------------------------------------------------------
  5337. struct TSynchronizeParams
  5338. {
  5339. UnicodeString LocalDirectory;
  5340. UnicodeString RemoteDirectory;
  5341. TSynchronizeMode Mode;
  5342. TSynchronizeChecklist * Checklist;
  5343. int Params;
  5344. TCopyParamType * CopyParam;
  5345. TDateTime CollectElapsed;
  5346. TDateTime * StartTime;
  5347. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5348. };
  5349. //---------------------------------------------------------------------------
  5350. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5351. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5352. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5353. {
  5354. DebugAssert(!FAutoOperation);
  5355. void * BatchStorage;
  5356. BatchStart(BatchStorage);
  5357. FAutoOperation = true;
  5358. TFileOperationStatistics Statistics;
  5359. TDateTime Start = Now();
  5360. try
  5361. {
  5362. Params.OnProcessedItem = OnProcessedItem;
  5363. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5364. CreateProgressForm(&SynchronizeProgress);
  5365. Terminal->SynchronizeApply(
  5366. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5367. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5368. &Statistics);
  5369. }
  5370. __finally
  5371. {
  5372. Params.OnProcessedItem = NULL;
  5373. FAutoOperation = false;
  5374. DestroyProgressForm();
  5375. BatchEnd(BatchStorage);
  5376. ReloadLocalDirectory();
  5377. }
  5378. if (WinConfiguration->SynchronizeSummary)
  5379. {
  5380. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5381. // The statistics should be 0 anyway in this case
  5382. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5383. {
  5384. Message += L"\n";
  5385. if (Statistics.FilesUploaded > 0)
  5386. {
  5387. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5388. }
  5389. if (Statistics.FilesDownloaded > 0)
  5390. {
  5391. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5392. }
  5393. if (Statistics.FilesDeletedLocal > 0)
  5394. {
  5395. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5396. }
  5397. if (Statistics.FilesDeletedRemote > 0)
  5398. {
  5399. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5400. }
  5401. }
  5402. TDateTime Elapsed = (Now() - Start);
  5403. Message +=
  5404. L"\n" +
  5405. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5406. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5407. TMessageParams Params(mpNeverAskAgainCheck);
  5408. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5409. if (Result == qaNeverAskAgain)
  5410. {
  5411. WinConfiguration->SynchronizeSummary = false;
  5412. }
  5413. }
  5414. }
  5415. //---------------------------------------------------------------------------
  5416. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5417. {
  5418. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5419. {
  5420. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5421. }
  5422. // Not set in keep-up-to-date mode - Synchronize() method
  5423. if (Token != NULL)
  5424. {
  5425. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5426. if (Params.OnProcessedItem != NULL)
  5427. {
  5428. Params.OnProcessedItem(NULL, ChecklistItem);
  5429. }
  5430. }
  5431. }
  5432. //---------------------------------------------------------------------------
  5433. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5434. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5435. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5436. {
  5437. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5438. *Params.StartTime = Now();
  5439. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5440. }
  5441. //---------------------------------------------------------------------------
  5442. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5443. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5444. {
  5445. // terminal can be already closed (e.g. dropped connection)
  5446. if (Terminal != NULL)
  5447. {
  5448. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5449. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5450. }
  5451. }
  5452. //---------------------------------------------------------------------------
  5453. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5454. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5455. {
  5456. DebugAssert(!IsLocalBrowserMode());
  5457. TAutoBatch AutoBatch(this);
  5458. TAutoFlag AutoOperationFlag(FAutoOperation);
  5459. if (Side == osRemote)
  5460. {
  5461. std::unique_ptr<TStrings> FileList(new TStringList());
  5462. FileList->AddObject(FileName, RemoteFile);
  5463. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5464. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5465. RemoteDirView->SaveSelection();
  5466. RemoteDirView->SaveSelectedNames();
  5467. try
  5468. {
  5469. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5470. }
  5471. catch(...)
  5472. {
  5473. RemoteDirView->DiscardSavedSelection();
  5474. throw;
  5475. }
  5476. RemoteDirView->RestoreSelection();
  5477. }
  5478. else if (DebugAlwaysTrue(Side == osLocal))
  5479. {
  5480. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5481. {
  5482. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5483. }
  5484. UnicodeString Directory = ExtractFileDir(FileName);
  5485. ReloadLocalDirectory(Directory);
  5486. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5487. if (!SamePaths(Directory, NewDirectory))
  5488. {
  5489. ReloadLocalDirectory(NewDirectory);
  5490. }
  5491. }
  5492. }
  5493. //---------------------------------------------------------------------------
  5494. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5495. {
  5496. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5497. if (Side == osLocal)
  5498. {
  5499. if (Action == TSynchronizeChecklist::saDownloadNew)
  5500. {
  5501. OpenFolderInExplorer(LocalPath);
  5502. }
  5503. else
  5504. {
  5505. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5506. }
  5507. }
  5508. else if (DebugAlwaysTrue(Side == osRemote))
  5509. {
  5510. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5511. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5512. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5513. if (!LocalPath.IsEmpty())
  5514. {
  5515. SessionData->LocalDirectory = LocalPath;
  5516. }
  5517. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5518. ExecuteNewInstance(SessionName, FORMAT(L"%s=\"%s\"", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5519. }
  5520. }
  5521. //---------------------------------------------------------------------------
  5522. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5523. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5524. const TCopyParamType * CopyParams)
  5525. {
  5526. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5527. UnicodeString AdditionalParams =
  5528. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5529. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5530. SerializeCopyParamForCommandLine(CopyParams),
  5531. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5532. LocalDirectory, RemoteDirectory, Mode, Params));
  5533. ExecuteNewInstance(SessionName, AdditionalParams);
  5534. }
  5535. //---------------------------------------------------------------------------
  5536. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5537. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5538. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5539. {
  5540. int Result;
  5541. bool SaveSettings = false;
  5542. int Options =
  5543. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5544. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5545. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5546. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5547. bool Continue =
  5548. (UseDefaults == 0) ||
  5549. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5550. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5551. if (Continue)
  5552. {
  5553. Configuration->Usage->Inc(L"Synchronizations");
  5554. UpdateCopyParamCounters(CopyParam);
  5555. TSynchronizeOptions SynchronizeOptions;
  5556. GetSynchronizeOptions(Params, SynchronizeOptions);
  5557. if (SaveSettings)
  5558. {
  5559. GUIConfiguration->SynchronizeParams = Params;
  5560. }
  5561. else
  5562. {
  5563. SaveMode = false;
  5564. }
  5565. TDateTime StartTime = Now();
  5566. TSynchronizeChecklist * Checklist = NULL;
  5567. try
  5568. {
  5569. DebugAssert(!FAutoOperation);
  5570. FAutoOperation = true;
  5571. try
  5572. {
  5573. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5574. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5575. int Files = -1;
  5576. if (DataList->Count >= 1)
  5577. {
  5578. Files = StrToIntDef(DataList->Strings[0], Files);
  5579. }
  5580. else
  5581. {
  5582. DataList->Add(UnicodeString());
  5583. }
  5584. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5585. FSynchronizeProgressForm->Start();
  5586. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5587. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5588. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5589. &SynchronizeOptions);
  5590. if (Terminal->SessionData->CacheDirectories)
  5591. {
  5592. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5593. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5594. }
  5595. }
  5596. __finally
  5597. {
  5598. FAutoOperation = false;
  5599. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5600. SAFE_DESTROY(FSynchronizeProgressForm);
  5601. }
  5602. TSynchronizeParams SynchronizeParams;
  5603. SynchronizeParams.LocalDirectory = LocalDirectory;
  5604. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5605. SynchronizeParams.Mode = Mode;
  5606. SynchronizeParams.CopyParam = &CopyParam;
  5607. SynchronizeParams.Params = Params;
  5608. SynchronizeParams.Checklist = Checklist;
  5609. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5610. SynchronizeParams.StartTime = &StartTime;
  5611. SynchronizeParams.OnProcessedItem = NULL;
  5612. Result = Checklist->Count;
  5613. if (Checklist->Count > 0)
  5614. {
  5615. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5616. {
  5617. if (!DoSynchronizeChecklistDialog(
  5618. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5619. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  5620. {
  5621. Result = -1;
  5622. }
  5623. }
  5624. else
  5625. {
  5626. FullSynchronize(SynchronizeParams, NULL, NULL);
  5627. }
  5628. }
  5629. else
  5630. {
  5631. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5632. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5633. }
  5634. }
  5635. __finally
  5636. {
  5637. delete Checklist;
  5638. }
  5639. OperationComplete(StartTime);
  5640. }
  5641. else
  5642. {
  5643. Result = -1;
  5644. }
  5645. return Result;
  5646. }
  5647. //---------------------------------------------------------------------------
  5648. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5649. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5650. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5651. {
  5652. if (Collect)
  5653. {
  5654. DebugAssert(FSynchronizeProgressForm != NULL);
  5655. if (!FSynchronizeProgressForm->Started)
  5656. {
  5657. FSynchronizeProgressForm->Start();
  5658. }
  5659. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5660. SetTaskbarListProgressValue(CompareProgress);
  5661. }
  5662. else
  5663. {
  5664. DebugAssert(FSynchronizeProgressForm == NULL);
  5665. DebugAssert(FProgressForm != NULL);
  5666. }
  5667. }
  5668. //---------------------------------------------------------------------------
  5669. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5670. {
  5671. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  5672. TRemoteFile * File = NULL;
  5673. Terminal->ReadFile(FullFileName, File);
  5674. if (File != NULL)
  5675. {
  5676. std::unique_ptr<TRemoteFile> FileOwner(File);
  5677. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5678. Application->ShowMainForm = false;
  5679. Application->Run();
  5680. }
  5681. }
  5682. //---------------------------------------------------------------------------
  5683. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  5684. {
  5685. DebugFail();
  5686. }
  5687. //---------------------------------------------------------------------------
  5688. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5689. {
  5690. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5691. SessionData->Assign(Terminal->SessionData);
  5692. UpdateSessionData(SessionData.get());
  5693. if (Terminal->Active)
  5694. {
  5695. Terminal->UpdateSessionCredentials(SessionData.get());
  5696. }
  5697. if (!Terminal->SessionData->HasSessionName())
  5698. {
  5699. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5700. // once IsWorkspace is cleared
  5701. SessionData->Name = UnicodeString();
  5702. }
  5703. // Most uses of this method ends with saving the session, and we do not want to store the IsWorkspace flag.
  5704. // Particularly, with SaveHiddenDuplicateSession, it won't work as TSessionData::ParseUrl explicitly ignored workspace sessions.
  5705. SessionData->IsWorkspace = false;
  5706. return SessionData.release();
  5707. }
  5708. //---------------------------------------------------------------------------
  5709. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5710. {
  5711. TSessionData * SessionData = CloneCurrentSessionData();
  5712. try
  5713. {
  5714. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5715. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5716. }
  5717. __finally
  5718. {
  5719. delete SessionData;
  5720. }
  5721. }
  5722. //---------------------------------------------------------------------------
  5723. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5724. {
  5725. TTerminalManager * Manager = TTerminalManager::Instance();
  5726. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5727. if (DebugAlwaysTrue(Terminal != NULL))
  5728. {
  5729. // Update (Managed)Terminal->StateData
  5730. UpdateTerminal(Terminal);
  5731. }
  5732. Manager->SaveWorkspace(DataList.get());
  5733. return DataList.release();
  5734. }
  5735. //---------------------------------------------------------------------------
  5736. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5737. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5738. {
  5739. WinConfiguration->LastWorkspace = Name;
  5740. if (!SavePasswords)
  5741. {
  5742. for (int Index = 0; Index < DataList->Count; Index++)
  5743. {
  5744. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5745. if (SessionData->Link.IsEmpty())
  5746. {
  5747. SessionData->ClearSessionPasswords();
  5748. }
  5749. }
  5750. }
  5751. StoredSessions->NewWorkspace(Name, DataList);
  5752. // modified only
  5753. StoredSessions->Save(false, Explicit);
  5754. }
  5755. //---------------------------------------------------------------------------
  5756. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5757. {
  5758. return
  5759. DefaultStr(WinConfiguration->LastWorkspace,
  5760. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5761. }
  5762. //---------------------------------------------------------------------------
  5763. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5764. {
  5765. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5766. bool AnyNonStoredSessionWithPassword = false;
  5767. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5768. bool AllNonStoredSessionsAnonymous = true;
  5769. TTerminalManager * Manager = TTerminalManager::Instance();
  5770. for (int Index = 0; Index < DataList->Count; Index++)
  5771. {
  5772. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5773. if (SessionData->Link.IsEmpty())
  5774. {
  5775. if (SessionData->HasAnySessionPassword())
  5776. {
  5777. AnyNonStoredSessionWithPassword = true;
  5778. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  5779. {
  5780. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5781. }
  5782. }
  5783. if (!SameText(SessionData->UserName, AnonymousUserName))
  5784. {
  5785. AllNonStoredSessionsAnonymous = false;
  5786. }
  5787. }
  5788. }
  5789. bool SavePasswords;
  5790. bool * PSavePasswords;
  5791. bool NotRecommendedSavingPasswords =
  5792. !CustomWinConfiguration->UseMasterPassword &&
  5793. !AllNonStoredSessionsAnonymous;
  5794. if (Configuration->DisablePasswordStoring ||
  5795. !AnyNonStoredSessionWithPassword)
  5796. {
  5797. PSavePasswords = NULL;
  5798. SavePasswords = false;
  5799. }
  5800. else
  5801. {
  5802. PSavePasswords = &SavePasswords;
  5803. SavePasswords =
  5804. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5805. !NotRecommendedSavingPasswords;
  5806. }
  5807. UnicodeString Name = WorkspaceName();
  5808. bool CreateShortcut = false;
  5809. bool Result =
  5810. DoSaveWorkspaceDialog(
  5811. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5812. EnableAutoSave);
  5813. if (Result)
  5814. {
  5815. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5816. if (CreateShortcut)
  5817. {
  5818. TOperationVisualizer Visualizer;
  5819. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5820. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5821. }
  5822. if (EnableAutoSave)
  5823. {
  5824. WinConfiguration->AutoSaveWorkspace = true;
  5825. WinConfiguration->AutoWorkspace = Name;
  5826. if (PSavePasswords != NULL)
  5827. {
  5828. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5829. }
  5830. }
  5831. }
  5832. return Result;
  5833. }
  5834. //---------------------------------------------------------------------------
  5835. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TManagedTerminal * Terminal)
  5836. {
  5837. DebugAssert(!IsLocalBrowserMode());
  5838. SAFE_DESTROY(Terminal->RemoteExplorerState);
  5839. if (WinConfiguration->PreservePanelState)
  5840. {
  5841. Terminal->RemoteExplorerState = RemoteDirView->SaveState();
  5842. }
  5843. UpdateSessionData(Terminal->StateData);
  5844. }
  5845. //---------------------------------------------------------------------------
  5846. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5847. {
  5848. // Keep in sync with TSessionData::CopyStateData
  5849. DebugAssert(!IsLocalBrowserMode());
  5850. DebugAssert(Data != NULL);
  5851. // This is inconsistent with how color (for example) is handled.
  5852. Data->Name = Terminal->SessionData->Name;
  5853. // cannot use RemoteDirView->Path, because it is empty if connection
  5854. // was already closed
  5855. // also only peek, we may not be connected at all atm
  5856. // so make sure we do not try retrieving current directory from the server
  5857. // (particularly with FTP)
  5858. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5859. if (!ACurrentDirectory.IsEmpty())
  5860. {
  5861. Data->RemoteDirectory = ACurrentDirectory;
  5862. }
  5863. Data->Color = SessionColor;
  5864. }
  5865. //---------------------------------------------------------------------------
  5866. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5867. {
  5868. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5869. DebugAssert(Toolbar != NULL);
  5870. for (int i = 0; i < Toolbar->Items->Count; i++)
  5871. {
  5872. TTBXCustomDropDownItem * DropDownItem;
  5873. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5874. if (DropDownItem != NULL)
  5875. {
  5876. ToolbarItemResize(DropDownItem,
  5877. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5878. Toolbar->NonClientWidth);
  5879. break;
  5880. }
  5881. }
  5882. }
  5883. //---------------------------------------------------------------------------
  5884. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5885. {
  5886. Item->EditWidth = Width;
  5887. }
  5888. //---------------------------------------------------------------------------
  5889. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5890. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5891. {
  5892. for (int i = 0; i < Toolbar->Items->Count; i++)
  5893. {
  5894. TTBXCustomDropDownItem * DropDownItem;
  5895. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5896. if (DropDownItem != NULL)
  5897. {
  5898. ASize.x -= DropDownItem->EditWidth;
  5899. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5900. }
  5901. }
  5902. }
  5903. //---------------------------------------------------------------------------
  5904. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5905. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5906. {
  5907. if (WinConfiguration->DDWarnLackOfTempSpace)
  5908. {
  5909. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5910. if (!ADrive.IsEmpty())
  5911. {
  5912. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5913. DebugAssert(RequiredSpace >= 0);
  5914. __int64 RequiredWithReserve;
  5915. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5916. if (FreeSpace < RequiredWithReserve)
  5917. {
  5918. unsigned int Result;
  5919. TMessageParams Params(mpNeverAskAgainCheck);
  5920. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5921. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5922. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5923. if (Result == qaNeverAskAgain)
  5924. {
  5925. WinConfiguration->DDWarnLackOfTempSpace = false;
  5926. }
  5927. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5928. }
  5929. }
  5930. }
  5931. }
  5932. //---------------------------------------------------------------------------
  5933. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5934. {
  5935. DoOpenDirectoryDialog(odAddBookmark, Side);
  5936. }
  5937. //---------------------------------------------------------------------------
  5938. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5939. {
  5940. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5941. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  5942. try
  5943. {
  5944. TCustomDirView * DView = DirView(Side);
  5945. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5946. {
  5947. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5948. }
  5949. }
  5950. catch (...)
  5951. {
  5952. delete VisitedDirectories;
  5953. throw;
  5954. }
  5955. return VisitedDirectories;
  5956. }
  5957. //---------------------------------------------------------------------------
  5958. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5959. TOpenDirectoryMode Mode, TOperationSide Side)
  5960. {
  5961. bool Continue = true;
  5962. if (Mode == odAddBookmark)
  5963. {
  5964. TMessageParams Params(mpNeverAskAgainCheck);
  5965. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5966. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5967. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  5968. unsigned int Answer =
  5969. MessageDialog(Message,
  5970. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  5971. if (Answer == qaNeverAskAgain)
  5972. {
  5973. Continue = true;
  5974. WinConfiguration->UseSharedBookmarks = true;
  5975. }
  5976. else if (Answer == qaOK)
  5977. {
  5978. Continue = true;
  5979. WinConfiguration->UseSharedBookmarks = false;
  5980. }
  5981. else
  5982. {
  5983. Continue = false;
  5984. }
  5985. }
  5986. if (Continue)
  5987. {
  5988. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  5989. try
  5990. {
  5991. UnicodeString Name = DirView(Side)->PathName;
  5992. bool Repeat;
  5993. do
  5994. {
  5995. Repeat = false;
  5996. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  5997. // do not allow switching to location profiles,
  5998. // if we are not connected
  5999. HasDirView[osLocal] && (Terminal != NULL)))
  6000. {
  6001. TWindowLock Lock(this);
  6002. if (!TryOpenDirectory(Side, Name))
  6003. {
  6004. Repeat = true;
  6005. Mode = odBrowse;
  6006. }
  6007. }
  6008. }
  6009. while (Repeat);
  6010. }
  6011. __finally
  6012. {
  6013. delete VisitedDirectories;
  6014. }
  6015. }
  6016. }
  6017. //---------------------------------------------------------------------------
  6018. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6019. {
  6020. DebugAssert(!FTerminal->CommandSessionOpened);
  6021. return TTerminalManager::Instance()->ConnectTerminal(FTerminal->CommandSession);
  6022. }
  6023. //---------------------------------------------------------------------------
  6024. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6025. {
  6026. bool Result = FTerminal->IsCapable[Capability] ||
  6027. FTerminal->CommandSessionOpened;
  6028. if (!Result)
  6029. {
  6030. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  6031. if (!GUIConfiguration->ConfirmCommandSession)
  6032. {
  6033. Result = true;
  6034. }
  6035. else
  6036. {
  6037. TMessageParams Params(mpNeverAskAgainCheck);
  6038. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6039. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6040. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6041. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6042. if (Answer == qaNeverAskAgain)
  6043. {
  6044. GUIConfiguration->ConfirmCommandSession = false;
  6045. Result = true;
  6046. }
  6047. else if (Answer == qaOK)
  6048. {
  6049. Result = true;
  6050. }
  6051. }
  6052. if (Result)
  6053. {
  6054. Result = CommandSessionFallback();
  6055. }
  6056. }
  6057. return Result;
  6058. }
  6059. //---------------------------------------------------------------------------
  6060. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6061. {
  6062. if (EnsureCommandSessionFallback(fcAnyCommand))
  6063. {
  6064. DoConsoleDialog(Terminal, Command);
  6065. }
  6066. }
  6067. //---------------------------------------------------------------------------
  6068. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6069. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6070. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6071. {
  6072. if (!IsSideLocalBrowser(osRemote) &&
  6073. IsFileControl(DropSourceControl, osRemote) &&
  6074. (FDDExtMapFile != NULL))
  6075. {
  6076. Accept = true;
  6077. }
  6078. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6079. DebugAssert(FDDTargetControl != NULL);
  6080. }
  6081. //---------------------------------------------------------------------------
  6082. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6083. _di_IDataObject DataObj, int KeyState,
  6084. const TPoint & Point, int & Effect, bool & Accept)
  6085. {
  6086. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6087. }
  6088. //---------------------------------------------------------------------------
  6089. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6090. _di_IDataObject DataObj, int KeyState,
  6091. const TPoint & Point, int & Effect, bool & Accept)
  6092. {
  6093. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6094. }
  6095. //---------------------------------------------------------------------------
  6096. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6097. TObject * Sender)
  6098. {
  6099. DebugUsedParam(Sender);
  6100. DebugAssert(FDDTargetControl == Sender);
  6101. FDDTargetControl = NULL;
  6102. }
  6103. //---------------------------------------------------------------------------
  6104. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  6105. {
  6106. FileControlDDDragLeave(SessionsPageControl);
  6107. }
  6108. //---------------------------------------------------------------------------
  6109. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  6110. {
  6111. FileControlDDDragLeave(QueueView3);
  6112. }
  6113. //---------------------------------------------------------------------------
  6114. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6115. {
  6116. DebugAssert(!IsSideLocalBrowser(Side));
  6117. DebugUsedParam(Side);
  6118. bool Edit = false;
  6119. TRemoteFile * File = NULL;
  6120. UnicodeString FileName;
  6121. UnicodeString PointTo;
  6122. bool SymbolicLink = true;
  6123. if (RemoteDirView->ItemFocused)
  6124. {
  6125. DebugAssert(RemoteDirView->ItemFocused->Data);
  6126. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6127. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6128. if (Edit)
  6129. {
  6130. FileName = File->FileName;
  6131. PointTo = File->LinkTo;
  6132. }
  6133. else
  6134. {
  6135. PointTo = File->FileName;
  6136. }
  6137. }
  6138. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6139. Terminal->IsCapable[fcHardLink]))
  6140. {
  6141. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6142. if (Edit)
  6143. {
  6144. DebugAssert(File->FileName == FileName);
  6145. int Params = dfNoRecursive;
  6146. Terminal->ExceptionOnFail = true;
  6147. try
  6148. {
  6149. Terminal->DeleteFile(L"", File, &Params);
  6150. }
  6151. __finally
  6152. {
  6153. Terminal->ExceptionOnFail = false;
  6154. }
  6155. }
  6156. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6157. }
  6158. }
  6159. //---------------------------------------------------------------------------
  6160. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6161. {
  6162. return
  6163. (IsSideLocalBrowser(Side) ||
  6164. (HasActiveTerminal() &&
  6165. Terminal->ResolvingSymlinks &&
  6166. Terminal->IsCapable[fcSymbolicLink]));
  6167. }
  6168. //---------------------------------------------------------------------------
  6169. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6170. {
  6171. return
  6172. !IsSideLocalBrowser(FCurrentSide) &&
  6173. (RemoteDirView->ItemFocused != NULL) &&
  6174. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6175. Terminal->SessionData->ResolveSymlinks;
  6176. }
  6177. //---------------------------------------------------------------------------
  6178. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6179. {
  6180. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6181. FForceExecution = true;
  6182. try
  6183. {
  6184. DirView(osCurrent)->ExecuteCurrentFile();
  6185. }
  6186. __finally
  6187. {
  6188. FForceExecution = false;
  6189. }
  6190. }
  6191. //---------------------------------------------------------------------------
  6192. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6193. {
  6194. TEditorData ExternalEditor;
  6195. ExternalEditor.Editor = edExternal;
  6196. bool Remember = false;
  6197. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6198. {
  6199. if (Remember)
  6200. {
  6201. TEditorList * EditorList = new TEditorList;
  6202. try
  6203. {
  6204. *EditorList = *WinConfiguration->EditorList;
  6205. bool Found = false;
  6206. int i = 0;
  6207. while (!Found && (i < EditorList->Count))
  6208. {
  6209. const TEditorPreferences * Editor = EditorList->Editors[i];
  6210. if ((Editor->Data->Editor == edExternal) &&
  6211. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6212. {
  6213. Found = true;
  6214. }
  6215. i++;
  6216. }
  6217. if (!Found)
  6218. {
  6219. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6220. WinConfiguration->EditorList = EditorList;
  6221. }
  6222. }
  6223. __finally
  6224. {
  6225. delete EditorList;
  6226. }
  6227. }
  6228. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6229. }
  6230. }
  6231. //---------------------------------------------------------------------------
  6232. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6233. {
  6234. FEditorManager->ProcessFiles(FileTerminalRemoved, ATerminal);
  6235. if (FFileFindTerminal == ATerminal)
  6236. {
  6237. FFileFindTerminal = NULL;
  6238. HideFileFindDialog();
  6239. }
  6240. if (FClipboardTerminal == ATerminal)
  6241. {
  6242. ClipboardClear(); // implies ClipboardStop
  6243. }
  6244. if ((FOnSynchronizeAbort != NULL) &&
  6245. (ATerminal == FTerminal))
  6246. {
  6247. FOnSynchronizeAbort(NULL);
  6248. }
  6249. }
  6250. //---------------------------------------------------------------------------
  6251. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6252. {
  6253. DetachTerminal(Sender);
  6254. }
  6255. //---------------------------------------------------------------------------
  6256. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6257. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6258. {
  6259. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6260. DebugAssert(Terminal != NULL);
  6261. if (Data->Terminal == Terminal)
  6262. {
  6263. Data->Terminal = NULL;
  6264. }
  6265. }
  6266. //---------------------------------------------------------------------------
  6267. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  6268. {
  6269. UpdateControls();
  6270. SessionColor = TColor(0);
  6271. UpdateRemotePathComboBox(false);
  6272. NeedSession(false);
  6273. }
  6274. //---------------------------------------------------------------------------
  6275. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6276. {
  6277. try
  6278. {
  6279. // Cache, as the login dialog can change its value
  6280. // See also DoShow. Also see TTerminalManager::NewSession.
  6281. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6282. try
  6283. {
  6284. if (ShowLogin)
  6285. {
  6286. bool ReloadSessions = !Startup;
  6287. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this);
  6288. }
  6289. else if (Startup && WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  6290. // This detects if workspace was saved the last time the main widow was closed
  6291. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  6292. {
  6293. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false, true);
  6294. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  6295. }
  6296. }
  6297. __finally
  6298. {
  6299. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6300. // (so there was no chance for the user to open any session yet)
  6301. if ((ShowLogin || !Startup) &&
  6302. !WinConfiguration->KeepOpenWhenNoSession &&
  6303. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6304. {
  6305. TerminateApplication();
  6306. }
  6307. }
  6308. }
  6309. catch (EAbort &)
  6310. {
  6311. // swallow
  6312. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6313. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6314. }
  6315. }
  6316. //---------------------------------------------------------------------------
  6317. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged()
  6318. {
  6319. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  6320. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6321. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  6322. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6323. try
  6324. {
  6325. while ((SessionsPageControl->PageCount > TerminalList->Count + 1) ||
  6326. // Clear the design time unthemed tab
  6327. ((SessionsPageControl->PageCount > 0) &&
  6328. (dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[SessionsPageControl->PageCount - 1]) == NULL)))
  6329. {
  6330. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6331. }
  6332. for (int Index = 0; Index <= TerminalList->Count; Index++)
  6333. {
  6334. TThemeTabSheet * TabSheet;
  6335. if (Index >= SessionsPageControl->PageCount)
  6336. {
  6337. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6338. TabSheet->PageControl = SessionsPageControl;
  6339. }
  6340. else
  6341. {
  6342. TabSheet = DebugNotNull(dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[Index]));
  6343. }
  6344. bool IsSessionTab = (Index < TerminalList->Count);
  6345. if (IsSessionTab)
  6346. {
  6347. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  6348. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6349. UpdateSessionTab(TabSheet);
  6350. }
  6351. else
  6352. {
  6353. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  6354. TabSheet->Tag = 0; // not really needed
  6355. TabSheet->Shadowed = false;
  6356. // 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)
  6357. UpdateNewSessionTab();
  6358. }
  6359. TabSheet->ShowCloseButton = IsSessionTab;
  6360. }
  6361. }
  6362. __finally
  6363. {
  6364. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6365. }
  6366. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  6367. }
  6368. //---------------------------------------------------------------------------
  6369. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  6370. {
  6371. DoTerminalListChanged();
  6372. }
  6373. //---------------------------------------------------------------------------
  6374. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  6375. {
  6376. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6377. DebugAssert(TabSheet->ImageIndex == 0);
  6378. TabSheet->Caption =
  6379. WinConfiguration->SelectiveToolbarText ?
  6380. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  6381. UnicodeString();
  6382. }
  6383. //---------------------------------------------------------------------------
  6384. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  6385. {
  6386. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6387. }
  6388. //---------------------------------------------------------------------------
  6389. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  6390. {
  6391. if (DebugAlwaysTrue(TabSheet != NULL))
  6392. {
  6393. TManagedTerminal * ATerminal = GetSessionTabTerminal(TabSheet);
  6394. if (DebugAlwaysTrue(ATerminal != NULL))
  6395. {
  6396. TColor Color = (ATerminal == FTerminal) ? FSessionColor : ATerminal->StateData->Color;
  6397. TabSheet->ImageIndex = AddSessionColor(Color);
  6398. UnicodeString TabCaption = TTerminalManager::Instance()->GetTerminalTitle(ATerminal, true);
  6399. TabSheet->Caption = SessionsPageControl->FormatCaptionWithCloseButton(TabCaption);
  6400. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  6401. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  6402. {
  6403. ThemeTabSheet->Shadowed = !ATerminal->Active;
  6404. }
  6405. }
  6406. }
  6407. }
  6408. //---------------------------------------------------------------------------
  6409. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6410. {
  6411. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6412. TManagedTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  6413. bool Result = (Terminal != NULL);
  6414. if (Result)
  6415. {
  6416. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6417. }
  6418. else
  6419. {
  6420. try
  6421. {
  6422. NewSession();
  6423. }
  6424. __finally
  6425. {
  6426. DoTerminalListChanged();
  6427. }
  6428. FSessionsPageControlNewSessionTime = Now();
  6429. }
  6430. return Result;
  6431. }
  6432. //---------------------------------------------------------------------------
  6433. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6434. {
  6435. SessionTabSwitched();
  6436. }
  6437. //---------------------------------------------------------------------------
  6438. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6439. {
  6440. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6441. DebugAssert(TransferList != NULL);
  6442. UnicodeString Name;
  6443. if (TransferList->ItemIndex <= 0)
  6444. {
  6445. Name = L"";
  6446. }
  6447. else
  6448. {
  6449. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6450. }
  6451. if (FCopyParamAutoSelected.IsEmpty())
  6452. {
  6453. // if previous preset was not autoselected, make new preset the "default"
  6454. FCopyParamDefault = Name;
  6455. }
  6456. GUIConfiguration->CopyParamCurrent = Name;
  6457. }
  6458. //---------------------------------------------------------------------------
  6459. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6460. {
  6461. // sanity check
  6462. bool ExistingPreset =
  6463. (FTransferListHoverIndex >= 0) &&
  6464. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6465. DebugAssert(ExistingPreset);
  6466. if (ExistingPreset)
  6467. {
  6468. HDC DC = GetDC(0);
  6469. TCanvas * Canvas = new TCanvas();
  6470. try
  6471. {
  6472. Canvas->Handle = DC;
  6473. Canvas->Font = GetToolbarFont(this);
  6474. UnicodeString Name;
  6475. if (FTransferListHoverIndex == 0)
  6476. {
  6477. Name = L"";
  6478. }
  6479. else
  6480. {
  6481. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6482. }
  6483. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6484. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6485. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6486. static_cast<TObject*>(GetComponent(fcTransferList)));
  6487. UnicodeString InfoStr =
  6488. GUIConfiguration->CopyParamPreset[Name].
  6489. GetInfoStr(L"; ",
  6490. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  6491. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6492. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6493. {
  6494. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6495. {
  6496. InfoStr.SetLength(InfoStr.Length() - 1);
  6497. }
  6498. InfoStr += Ellipsis;
  6499. }
  6500. // UpdateCaption does not cause invalidation of whole submenu, while
  6501. // setting Caption property does.
  6502. // also it probably does not resize the label, even if necessary
  6503. // (we do not want that anyway)
  6504. TransferLabel->UpdateCaption(InfoStr);
  6505. }
  6506. __finally
  6507. {
  6508. Canvas->Handle = NULL;
  6509. ReleaseDC(0, DC);
  6510. delete Canvas;
  6511. }
  6512. }
  6513. }
  6514. //---------------------------------------------------------------------------
  6515. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6516. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6517. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6518. {
  6519. if (FTransferListHoverIndex != AHoverIndex)
  6520. {
  6521. FTransferListHoverIndex = AHoverIndex;
  6522. UpdateTransferLabel();
  6523. }
  6524. }
  6525. //---------------------------------------------------------------------------
  6526. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6527. {
  6528. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6529. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6530. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6531. {
  6532. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6533. {
  6534. if (!NonVisualDataModule->Busy)
  6535. {
  6536. OpenDirectory(GetSide(osCurrent));
  6537. }
  6538. Message.Result = 1;
  6539. }
  6540. else
  6541. {
  6542. TForm::Dispatch(&Message);
  6543. }
  6544. }
  6545. else
  6546. {
  6547. TForm::Dispatch(&Message);
  6548. }
  6549. }
  6550. //---------------------------------------------------------------------------
  6551. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6552. {
  6553. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6554. if ((FIgnoreNextDialogChar != 0) &&
  6555. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6556. {
  6557. Message.Result = 1;
  6558. }
  6559. else
  6560. {
  6561. TForm::Dispatch(&Message);
  6562. }
  6563. FIgnoreNextDialogChar = 0;
  6564. }
  6565. //---------------------------------------------------------------------------
  6566. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6567. {
  6568. // The four low-order bits are used internally by Windows
  6569. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6570. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6571. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6572. // 61730 - restore thru double click - undocumented
  6573. // 61490 - maximize thru double click - undocumented
  6574. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6575. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6576. {
  6577. SysResizing(Cmd);
  6578. }
  6579. TForm::Dispatch(&Message);
  6580. }
  6581. //---------------------------------------------------------------------------
  6582. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6583. {
  6584. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6585. // Also there no point returning TRUE as we are not able to
  6586. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6587. // Hence the process termination might be safer :)
  6588. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6589. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6590. {
  6591. Message.Result = FALSE;
  6592. }
  6593. else
  6594. {
  6595. Message.Result = TRUE;
  6596. }
  6597. // Do not call default handling as that triggers OnCloseQuery,
  6598. // where our implementation will popup confirmation dialogs, what we do not want,
  6599. // as per Vista guidelines:
  6600. // https://docs.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6601. }
  6602. //---------------------------------------------------------------------------
  6603. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6604. {
  6605. if (Message.EndSession && IsApplicationMinimized())
  6606. {
  6607. // WORKAROUND
  6608. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6609. // when it receives WM_ENDSESSION,
  6610. // but that sometimes (particularly when application is minimized) cause crashes.
  6611. // Crash popups message that blocks log off.
  6612. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6613. // so we call ExitProcess() immediately, not even trying to cleanup.
  6614. // It still causes beep, so there's likely some popup, but it does not block
  6615. // log off.
  6616. ExitProcess(0);
  6617. }
  6618. TForm::Dispatch(&Message);
  6619. }
  6620. #ifdef _DEBUG
  6621. //---------------------------------------------------------------------------
  6622. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6623. {
  6624. TForm::Dispatch(&Message);
  6625. }
  6626. #endif
  6627. //---------------------------------------------------------------------------
  6628. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6629. {
  6630. }
  6631. //---------------------------------------------------------------------------
  6632. void __fastcall TCustomScpExplorerForm::DoShow()
  6633. {
  6634. // only now are the controls resized finally, so the size constraints
  6635. // will not conflict with possibly very small window size
  6636. RestoreFormParams();
  6637. // This indicates that the form was rescaled after parameters were loaded and
  6638. // as most probably the parameters were actually saved with the current form scaling
  6639. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6640. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6641. // and no rounding problems will occur.
  6642. // See also RestoreForm.
  6643. // (we should be safe to call RestoreParams unconditionally)
  6644. if (PixelsPerInch != Screen->PixelsPerInch)
  6645. {
  6646. RestoreParams();
  6647. }
  6648. FixControlsPlacement();
  6649. if (Position == poDefaultPosOnly)
  6650. {
  6651. CutFormToDesktop(this);
  6652. }
  6653. TForm::DoShow();
  6654. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6655. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6656. if (Terminal == NULL)
  6657. {
  6658. StartingDisconnected();
  6659. }
  6660. UpdatePixelsPerInchMainWindowCounter();
  6661. FShowing = true;
  6662. }
  6663. //---------------------------------------------------------------------------
  6664. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6665. {
  6666. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6667. }
  6668. //---------------------------------------------------------------------------
  6669. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  6670. {
  6671. DoTerminalListChanged();
  6672. }
  6673. //---------------------------------------------------------------------------
  6674. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6675. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6676. TNotifyEvent OnBalloonClick, TObject * UserData)
  6677. {
  6678. bool Do;
  6679. UnicodeString Message;
  6680. if (E == NULL)
  6681. {
  6682. Message = Str;
  6683. Do = true;
  6684. }
  6685. else
  6686. {
  6687. Do = ExceptionMessage(E, Message);
  6688. }
  6689. if (Do && WinConfiguration->BalloonNotifications)
  6690. {
  6691. UnicodeString Title;
  6692. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6693. {
  6694. Message = TrimLeft(Message);
  6695. }
  6696. else
  6697. {
  6698. Message = UnformatMessage(Message);
  6699. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6700. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6701. Title = LoadResourceString(Captions[Type]);
  6702. if (Terminal != NULL)
  6703. {
  6704. Title = FORMAT(L"%s - %s",
  6705. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), Title));
  6706. }
  6707. }
  6708. if (Seconds == 0)
  6709. {
  6710. Seconds = WinConfiguration->NotificationsTimeout;
  6711. }
  6712. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6713. }
  6714. }
  6715. //---------------------------------------------------------------------------
  6716. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6717. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6718. UnicodeString HelpKeyword, const TMessageParams * Params,
  6719. TTerminal * Terminal)
  6720. {
  6721. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6722. (FOnFeedSynchronizeError != NULL)) &&
  6723. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6724. {
  6725. if (FOnFeedSynchronizeError != NULL)
  6726. {
  6727. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6728. }
  6729. else
  6730. {
  6731. DebugAssert(FErrorList != NULL);
  6732. TStringList * MoreMessagesCopy = NULL;
  6733. if (MoreMessages)
  6734. {
  6735. MoreMessagesCopy = new TStringList();
  6736. MoreMessagesCopy->Assign(MoreMessages);
  6737. }
  6738. FErrorList->AddObject(Message, MoreMessagesCopy);
  6739. }
  6740. PopupTrayBalloon(Terminal, Message, Type);
  6741. return ContinueAnswer(Answers);
  6742. }
  6743. else
  6744. {
  6745. bool UseBalloon = FTrayIcon->Visible;
  6746. if (UseBalloon)
  6747. {
  6748. PopupTrayBalloon(Terminal, Message, Type);
  6749. }
  6750. unsigned int Result;
  6751. try
  6752. {
  6753. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6754. }
  6755. __finally
  6756. {
  6757. // cancel only balloon we popped up, otherwise we may cancel notification
  6758. // balloon that was there before the message dialog
  6759. // (such as "dd confirmation opt in balloon)
  6760. if (UseBalloon)
  6761. {
  6762. FTrayIcon->CancelBalloon();
  6763. }
  6764. }
  6765. return Result;
  6766. }
  6767. }
  6768. //---------------------------------------------------------------------------
  6769. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6770. TTerminal * Terminal, Exception * E)
  6771. {
  6772. if (FTrayIcon->Visible)
  6773. {
  6774. PopupTrayBalloon(Terminal, L"", qtError, E);
  6775. }
  6776. // particularly prevent opening new session from jump list,
  6777. // while exception is shown
  6778. NonVisualDataModule->StartBusy();
  6779. try
  6780. {
  6781. ShowExtendedExceptionEx(Terminal, E);
  6782. }
  6783. __finally
  6784. {
  6785. NonVisualDataModule->EndBusy();
  6786. FTrayIcon->CancelBalloon();
  6787. }
  6788. }
  6789. //---------------------------------------------------------------------------
  6790. void __fastcall TCustomScpExplorerForm::TerminalReady()
  6791. {
  6792. if (Terminal->Active)
  6793. {
  6794. InitStatusBar();
  6795. }
  6796. // cannot rely on active page being page for active terminal,
  6797. // as it can happen that active page is the "new session" page
  6798. // (e.g. when reconnecting active terminal, while login dialog
  6799. // invoked from "new session" page is modal)
  6800. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6801. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  6802. }
  6803. //---------------------------------------------------------------------------
  6804. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6805. TTerminal * Terminal, Exception * E)
  6806. {
  6807. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6808. if (!Terminal->Active)
  6809. {
  6810. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6811. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6812. {
  6813. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6814. }
  6815. }
  6816. }
  6817. //---------------------------------------------------------------------------
  6818. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6819. UnicodeString Message, TQueryType Type,
  6820. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6821. {
  6822. if ((E == NULL) ||
  6823. ExceptionMessage(E, Message))
  6824. {
  6825. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6826. if (Important)
  6827. {
  6828. Seconds *= 5;
  6829. }
  6830. UnicodeString NoteMessage(UnformatMessage(Message));
  6831. if (Terminal != NULL)
  6832. {
  6833. NoteMessage = FORMAT(L"%s: %s",
  6834. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), NoteMessage));
  6835. }
  6836. if (WinConfiguration->BalloonNotifications)
  6837. {
  6838. AddNote(NoteMessage);
  6839. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6840. }
  6841. else
  6842. {
  6843. FlashOnBackground();
  6844. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6845. }
  6846. }
  6847. }
  6848. //---------------------------------------------------------------------------
  6849. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6850. {
  6851. RestoreApp();
  6852. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6853. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6854. !NonVisualDataModule->Busy)
  6855. {
  6856. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  6857. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6858. if (!ComponentVisible[fcQueueView])
  6859. {
  6860. ToggleQueueVisibility();
  6861. GoToQueue();
  6862. }
  6863. }
  6864. }
  6865. //---------------------------------------------------------------------------
  6866. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  6867. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6868. {
  6869. UnicodeString Message;
  6870. TNotifyEvent OnClick = NULL;
  6871. TObject * UserData = NULL;
  6872. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6873. switch (Event)
  6874. {
  6875. case qeEmptyButMonitored:
  6876. if ((ATerminal != Terminal) || QueueInvisible)
  6877. {
  6878. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6879. OnClick = QueueEmptyNoteClicked;
  6880. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6881. TerminalNoteData->Terminal = ATerminal;
  6882. UserData = TerminalNoteData;
  6883. }
  6884. break;
  6885. case qeEmpty:
  6886. OperationComplete(ATerminal->QueueOperationStart);
  6887. break;
  6888. case qePendingUserAction:
  6889. if ((ATerminal != Terminal) ||
  6890. (QueueInvisible && !IsQueueAutoPopup()))
  6891. {
  6892. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6893. }
  6894. break;
  6895. default:
  6896. DebugFail();
  6897. }
  6898. if (!Message.IsEmpty())
  6899. {
  6900. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6901. }
  6902. }
  6903. //---------------------------------------------------------------------------
  6904. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6905. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6906. {
  6907. if (FDDExtMapFile != NULL)
  6908. {
  6909. CloseHandle(FDDExtMapFile);
  6910. FDDExtMapFile = NULL;
  6911. }
  6912. if (WinConfiguration->DDFakeFile)
  6913. {
  6914. DDFakeFileInitDrag(FileList, Created);
  6915. }
  6916. }
  6917. //---------------------------------------------------------------------------
  6918. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  6919. {
  6920. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  6921. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  6922. {
  6923. FFakeFileDropTarget = FileName;
  6924. }
  6925. }
  6926. //---------------------------------------------------------------------------
  6927. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  6928. {
  6929. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6930. if (!ForceDirectories(ApiPath(Result)))
  6931. {
  6932. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  6933. }
  6934. FileSetAttr(ApiPath(Result), faHidden);
  6935. return Result;
  6936. }
  6937. //---------------------------------------------------------------------------
  6938. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  6939. bool & Created)
  6940. {
  6941. FFakeFileDropTarget = UnicodeString();
  6942. FDragFakeDirectory = CreateFakeTransferDirectory();
  6943. FileList->AddItem(NULL, FDragFakeDirectory);
  6944. Created = true;
  6945. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  6946. {
  6947. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  6948. }
  6949. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6950. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6951. {
  6952. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6953. TDragExtCommStruct* CommStruct;
  6954. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6955. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6956. DebugAssert(CommStruct != NULL);
  6957. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  6958. CommStruct->Dragging = true;
  6959. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  6960. LENOF(CommStruct->DropDest));
  6961. NULL_TERMINATE(CommStruct->DropDest);
  6962. UnmapViewOfFile(CommStruct);
  6963. }
  6964. }
  6965. //---------------------------------------------------------------------------
  6966. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  6967. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  6968. {
  6969. bool Result;
  6970. DebugAssert(!IsLocalBrowserMode());
  6971. if (Sender == RemoteDirView)
  6972. {
  6973. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  6974. }
  6975. else
  6976. {
  6977. DebugAssert(Sender == RemoteDriveView);
  6978. TStrings * FileList = RemoteDriveView->DragFileList();
  6979. try
  6980. {
  6981. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  6982. }
  6983. __finally
  6984. {
  6985. delete FileList;
  6986. }
  6987. }
  6988. if (FDDTargetControl == RemoteDriveView)
  6989. {
  6990. RemoteDriveView->UpdateDropTarget();
  6991. }
  6992. // foRemoteMove happens when file/dir is dragged within the remote tree view
  6993. // or from tree view to dir view.
  6994. // foMove happens when file/dir is dragged from remote tree view outside of
  6995. // application via dragex
  6996. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  6997. (DropSourceControl == RemoteDriveView))
  6998. {
  6999. RemoteDriveView->UpdateDropSource();
  7000. }
  7001. return Result;
  7002. }
  7003. //---------------------------------------------------------------------------
  7004. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  7005. {
  7006. DebugAssert(!IsLocalBrowserMode());
  7007. // This also handles drops of remote files to queue.
  7008. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  7009. SAFE_DESTROY(FDDFileList);
  7010. if (!FFakeFileDropTarget.IsEmpty())
  7011. {
  7012. RemoveDir(ApiPath(FFakeFileDropTarget));
  7013. }
  7014. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7015. {
  7016. try
  7017. {
  7018. TDragResult DDResult = (Sender == RemoteDirView) ?
  7019. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7020. // Focus is moved to the target application,
  7021. // but as we are going to present the UI, we need to steal the focus back.
  7022. // This most likely won't work though (windows does not allow application
  7023. // to steal focus most of the time)
  7024. Application->BringToFront();
  7025. // On older version of Windows we never got drMove here, see also comment below.
  7026. // On Windows 10, we get the "move".
  7027. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7028. {
  7029. UnicodeString TargetDirectory;
  7030. TFileOperation Operation;
  7031. // drInvalid may mean drMove, see comment below
  7032. switch (DDResult)
  7033. {
  7034. case drCopy:
  7035. Operation = foCopy;
  7036. break;
  7037. case drMove:
  7038. Operation = foMove;
  7039. break;
  7040. default:
  7041. DebugFail();
  7042. case drInvalid:
  7043. // prefer "copy" for safety
  7044. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7045. break;
  7046. }
  7047. TTransferOperationParam Param;
  7048. UnicodeString CounterName;
  7049. bool ForceQueue;
  7050. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7051. {
  7052. // we get drInvalid both if d&d was intercepted by ddext,
  7053. // and when users drops on no-drop location.
  7054. // we tell the difference by existence of response from ddext,
  7055. // so we ignore absence of response in this case
  7056. if (DDResult != drInvalid)
  7057. {
  7058. // here we know that the extension is installed,
  7059. // as it is checked as soon as drag&drop starts from
  7060. // RemoteFileControlDDCreateDragFileList
  7061. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7062. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7063. {
  7064. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7065. }
  7066. else
  7067. {
  7068. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7069. }
  7070. }
  7071. }
  7072. else
  7073. {
  7074. // download using fake file
  7075. Param.Temp = false;
  7076. Param.DragDrop = true;
  7077. if (ForceQueue)
  7078. {
  7079. Param.Queue = asOn;
  7080. }
  7081. if (Sender == RemoteDirView)
  7082. {
  7083. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7084. }
  7085. if (RemoteFileControlFileOperation(Sender, Operation,
  7086. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7087. {
  7088. Configuration->Usage->Inc(CounterName);
  7089. }
  7090. }
  7091. }
  7092. }
  7093. __finally
  7094. {
  7095. CloseHandle(FDDExtMapFile);
  7096. FDDExtMapFile = NULL;
  7097. delete FDragFakeMonitors;
  7098. FDragFakeMonitors = NULL;
  7099. RemoveDir(ApiPath(FDragFakeDirectory));
  7100. FDragFakeDirectory = L"";
  7101. FFakeFileDropTarget = UnicodeString();
  7102. }
  7103. }
  7104. if (FDragDropSshTerminate.get() != NULL)
  7105. {
  7106. FDragDropSshTerminate->Rethrow();
  7107. }
  7108. }
  7109. //---------------------------------------------------------------------------
  7110. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7111. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7112. {
  7113. // Remember drop effect so we know (when user drops files), if we copy or move
  7114. FLastDropEffect = dwEffect;
  7115. }
  7116. //---------------------------------------------------------------------------
  7117. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7118. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7119. {
  7120. bool Result;
  7121. if (FDDTargetControl == QueueView3)
  7122. {
  7123. Directory = DefaultDownloadTargetDirectory();
  7124. Result = true;
  7125. CounterName = L"DownloadsDragDropQueue";
  7126. ForceQueue = true;
  7127. }
  7128. else if (!FFakeFileDropTarget.IsEmpty())
  7129. {
  7130. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7131. Result = true;
  7132. ForceQueue = false;
  7133. CounterName = L"DownloadsDragDropFakeFile";
  7134. }
  7135. else
  7136. {
  7137. ForceQueue = false;
  7138. Enabled = false;
  7139. try
  7140. {
  7141. int Timer = 0;
  7142. Result = false;
  7143. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7144. {
  7145. {
  7146. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7147. TDragExtCommStruct* CommStruct;
  7148. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7149. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7150. DebugAssert(CommStruct != NULL);
  7151. Result = !CommStruct->Dragging;
  7152. if (Result)
  7153. {
  7154. Directory = ExtractFilePath(CommStruct->DropDest);
  7155. CounterName = L"DownloadsDragDropExternalExt";
  7156. }
  7157. UnmapViewOfFile(CommStruct);
  7158. }
  7159. if (!Result)
  7160. {
  7161. Sleep(50);
  7162. Timer += 50;
  7163. Application->ProcessMessages();
  7164. }
  7165. }
  7166. }
  7167. __finally
  7168. {
  7169. Enabled = true;
  7170. }
  7171. }
  7172. return Result;
  7173. }
  7174. //---------------------------------------------------------------------------
  7175. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7176. const UnicodeString TempDir, int SecDelay)
  7177. {
  7178. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7179. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7180. if (FDelayedDeletionTimer == NULL)
  7181. {
  7182. DebugAssert(HandleAllocated());
  7183. FDelayedDeletionTimer = new TTimer(this);
  7184. FDelayedDeletionTimer->Interval = 10000;
  7185. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7186. }
  7187. else
  7188. {
  7189. FDelayedDeletionTimer->Enabled = true;
  7190. }
  7191. }
  7192. //---------------------------------------------------------------------------
  7193. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7194. {
  7195. DebugAssert(FDelayedDeletionList != NULL);
  7196. TDateTime N = Now();
  7197. TDateTime Alert;
  7198. UnicodeString Directory;
  7199. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7200. {
  7201. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7202. if ((N >= Alert) || (Sender == NULL))
  7203. {
  7204. Directory = FDelayedDeletionList->Strings[Index];
  7205. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7206. {
  7207. FDelayedDeletionList->Delete(Index);
  7208. }
  7209. }
  7210. }
  7211. if (FDelayedDeletionList->Count == 0)
  7212. {
  7213. FDelayedDeletionTimer->Enabled = false;
  7214. }
  7215. }
  7216. //---------------------------------------------------------------------------
  7217. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7218. {
  7219. DebugAssert(!IsLocalBrowserMode());
  7220. if (IsFileControl(FDDTargetControl, osRemote) ||
  7221. (FDDTargetControl == SessionsPageControl))
  7222. {
  7223. TTerminal * TargetTerminal = NULL;
  7224. TFileOperation Operation = foNone;
  7225. if (FDDTargetControl == SessionsPageControl)
  7226. {
  7227. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7228. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7229. // do not allow dropping on the "+" and disconnected tabs
  7230. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  7231. if (IsActiveTerminal(TargetTerminal))
  7232. {
  7233. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7234. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7235. {
  7236. Operation = foRemoteMove;
  7237. }
  7238. else
  7239. {
  7240. Operation = foRemoteCopy;
  7241. }
  7242. }
  7243. }
  7244. else
  7245. {
  7246. if (FLastDropEffect == DROPEFFECT_MOVE)
  7247. {
  7248. Operation = foRemoteMove;
  7249. }
  7250. else if (FLastDropEffect == DROPEFFECT_COPY)
  7251. {
  7252. Operation = foRemoteCopy;
  7253. }
  7254. }
  7255. if (Operation != foNone)
  7256. {
  7257. RemoteFileControlFileOperation(DropSourceControl,
  7258. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7259. }
  7260. // abort drag&drop
  7261. Abort();
  7262. }
  7263. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7264. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7265. (FDDTargetControl != QueueView3))
  7266. {
  7267. DebugAssert(!FDragTempDir.IsEmpty());
  7268. TTransferType Type;
  7269. UnicodeString TempDir = FDragTempDir;
  7270. // We clear FDragTempDir before calling
  7271. // just in case it fail (raises exception)
  7272. FDragTempDir = L"";
  7273. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7274. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7275. // empty directory parameter means temp directory -> don't display it!
  7276. UnicodeString TargetDir = L"";
  7277. int Options = 0;
  7278. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7279. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7280. {
  7281. Abort();
  7282. }
  7283. // TargetDir is set when dropped on local file control
  7284. // (this was workaround for legacy dirview event handling, now it should be
  7285. // made prettier)
  7286. if (TargetDir.IsEmpty())
  7287. {
  7288. TargetDir = TempDir;
  7289. if (ForceDirectories(ApiPath(TargetDir)))
  7290. {
  7291. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7292. FPendingTempSpaceWarn = true;
  7293. try
  7294. {
  7295. FDragDropOperation = true;
  7296. int Params = cpTemporary |
  7297. (Type == ttMove ? cpDelete : 0);
  7298. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7299. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7300. }
  7301. __finally
  7302. {
  7303. FDragDropOperation = false;
  7304. FPendingTempSpaceWarn = false;
  7305. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7306. }
  7307. }
  7308. else
  7309. {
  7310. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7311. }
  7312. }
  7313. }
  7314. }
  7315. //---------------------------------------------------------------------------
  7316. void __fastcall TCustomScpExplorerForm::DDDownload(
  7317. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  7318. {
  7319. DebugAssert(!IsLocalBrowserMode());
  7320. TAutoBatch AutoBatch(this);
  7321. UpdateCopyParamCounters(*CopyParam);
  7322. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7323. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7324. {
  7325. RemoteDriveView->UpdateDropSource();
  7326. }
  7327. }
  7328. //---------------------------------------------------------------------------
  7329. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7330. {
  7331. public:
  7332. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7333. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7334. {
  7335. }
  7336. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7337. {
  7338. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7339. TDataObjectFilesEx::AllowData(FormatEtc);
  7340. }
  7341. };
  7342. //---------------------------------------------------------------------------
  7343. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7344. TObject * Sender, TDataObject *& DataObject)
  7345. {
  7346. if (FDDExtMapFile != NULL)
  7347. {
  7348. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7349. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7350. {
  7351. Abort();
  7352. }
  7353. if (FileList->Count > 0)
  7354. {
  7355. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7356. if (!FilesObject->IsValid(true, true))
  7357. {
  7358. FilesObject->_Release();
  7359. }
  7360. else
  7361. {
  7362. DataObject = FilesObject;
  7363. }
  7364. }
  7365. }
  7366. }
  7367. //---------------------------------------------------------------------------
  7368. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7369. {
  7370. DebugFail();
  7371. }
  7372. //---------------------------------------------------------------------------
  7373. void __fastcall TCustomScpExplorerForm::GoToTree()
  7374. {
  7375. ComponentVisible[fcRemoteTree] = true;
  7376. DriveView(osRemote)->SetFocus();
  7377. }
  7378. //---------------------------------------------------------------------------
  7379. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7380. TPanelExport Export)
  7381. {
  7382. TCustomDirView * DirView = this->DirView(Side);
  7383. std::unique_ptr<TStrings> ExportData(new TStringList());
  7384. switch (Export)
  7385. {
  7386. case pePath:
  7387. ExportData->Add(DirView->PathName);
  7388. break;
  7389. case peFileList:
  7390. case peFullFileList:
  7391. {
  7392. bool FullPath = (Export == peFullFileList);
  7393. DirView->CreateFileList(false, FullPath, ExportData.get());
  7394. for (int Index = 0; Index < ExportData->Count; Index++)
  7395. {
  7396. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7397. {
  7398. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7399. }
  7400. }
  7401. }
  7402. break;
  7403. default:
  7404. DebugFail();
  7405. }
  7406. return ExportData.release();
  7407. }
  7408. //---------------------------------------------------------------------------
  7409. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7410. TPanelExport Export, TPanelExportDestination Destination)
  7411. {
  7412. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7413. PanelExportStore(Side, Export, Destination, ExportData.get());
  7414. }
  7415. //---------------------------------------------------------------------------
  7416. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7417. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7418. TStrings * ExportData)
  7419. {
  7420. if (Destination == pedClipboard)
  7421. {
  7422. TInstantOperationVisualizer Visualizer;
  7423. CopyToClipboard(ExportData);
  7424. }
  7425. else
  7426. {
  7427. DebugFail();
  7428. }
  7429. }
  7430. //---------------------------------------------------------------------------
  7431. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7432. {
  7433. TCustomDirView * DirView = this->DirView(Side);
  7434. UnicodeString Mask = DirView->Mask;
  7435. if (DoFilterMaskDialog(DirView, Mask))
  7436. {
  7437. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7438. Configuration->Usage->Inc(L"Filters");
  7439. }
  7440. }
  7441. //---------------------------------------------------------------------------
  7442. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7443. {
  7444. return FQueueController->DefaultOperation();
  7445. }
  7446. //---------------------------------------------------------------------------
  7447. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7448. TQueueOperation Operation, void ** Param)
  7449. {
  7450. switch (Operation)
  7451. {
  7452. case qoPreferences:
  7453. return true;
  7454. case qoGoTo:
  7455. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7456. case qoOnceEmpty:
  7457. return IsAnythingQueued();
  7458. default:
  7459. return FQueueController->AllowOperation(Operation, Param);
  7460. }
  7461. }
  7462. //---------------------------------------------------------------------------
  7463. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7464. {
  7465. if (DebugAlwaysTrue(QueueView3->Visible))
  7466. {
  7467. QueueView3->SetFocus();
  7468. }
  7469. }
  7470. //---------------------------------------------------------------------------
  7471. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7472. TQueueOperation Operation, void * Param)
  7473. {
  7474. if (Operation == qoGoTo)
  7475. {
  7476. GoToQueue();
  7477. }
  7478. else if (Operation == qoPreferences)
  7479. {
  7480. PreferencesDialog(pmQueue);
  7481. }
  7482. else
  7483. {
  7484. FQueueController->ExecuteOperation(Operation, Param);
  7485. }
  7486. }
  7487. //---------------------------------------------------------------------------
  7488. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7489. {
  7490. return (Queue != NULL) && Queue->Enabled;
  7491. }
  7492. //---------------------------------------------------------------------------
  7493. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7494. {
  7495. DebugAssert(Queue != NULL);
  7496. if (Queue != NULL)
  7497. {
  7498. Queue->Enabled = !Queue->Enabled;
  7499. }
  7500. }
  7501. //---------------------------------------------------------------------------
  7502. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7503. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7504. {
  7505. FQueueActedItem = QueueView3->ItemFocused;
  7506. }
  7507. //---------------------------------------------------------------------------
  7508. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7509. {
  7510. return TopDock->Height + QueueSplitter->Height;
  7511. }
  7512. //---------------------------------------------------------------------------
  7513. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  7514. {
  7515. return
  7516. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  7517. (QueueDock->Visible ? QueueDock->Height : 0) +
  7518. (QueueLabel->Visible ? QueueLabel->Height : 0);
  7519. }
  7520. //---------------------------------------------------------------------------
  7521. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  7522. {
  7523. return ScaleByTextHeight(this, 48);
  7524. }
  7525. //---------------------------------------------------------------------------
  7526. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7527. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7528. {
  7529. // when queue is hidden by double-clicking splitter, stray attempt to
  7530. // resize the panel with strange value arrives, make sure it is ignored
  7531. if (ComponentVisible[fcQueueView])
  7532. {
  7533. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  7534. if (NewSize > MaxHeightLimit)
  7535. {
  7536. NewSize = MaxHeightLimit;
  7537. }
  7538. int MinHeightLimit =
  7539. GetStaticQueuePanelComponentsHeight() +
  7540. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  7541. GetMinQueueViewHeight();
  7542. if (NewSize < MinHeightLimit)
  7543. {
  7544. NewSize = MinHeightLimit;
  7545. }
  7546. }
  7547. else
  7548. {
  7549. Accept = false;
  7550. }
  7551. }
  7552. //---------------------------------------------------------------------------
  7553. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  7554. {
  7555. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  7556. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  7557. if (QueueViewHeight < GetMinQueueViewHeight())
  7558. {
  7559. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  7560. }
  7561. }
  7562. //---------------------------------------------------------------------------
  7563. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7564. TDragObject *& /*DragObject*/)
  7565. {
  7566. FQueueActedItem = QueueView3->ItemFocused;
  7567. }
  7568. //---------------------------------------------------------------------------
  7569. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7570. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7571. {
  7572. Accept = true;
  7573. if (Source == QueueView3)
  7574. {
  7575. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7576. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7577. if (Accept)
  7578. {
  7579. TQueueItemProxy * QueueItem;
  7580. TQueueItemProxy * DestQueueItem;
  7581. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7582. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7583. Accept = (QueueItem != DestQueueItem) &&
  7584. (QueueItem->Status == TQueueItem::qsPending) &&
  7585. (DestQueueItem->Status == TQueueItem::qsPending);
  7586. }
  7587. }
  7588. }
  7589. //---------------------------------------------------------------------------
  7590. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7591. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7592. {
  7593. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7594. {
  7595. TQueueItemProxy * QueueItem;
  7596. TQueueItemProxy * DestQueueItem;
  7597. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7598. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7599. QueueItem->Move(DestQueueItem);
  7600. }
  7601. }
  7602. //---------------------------------------------------------------------------
  7603. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7604. {
  7605. if ((QueueView3->ItemFocused == NULL) &&
  7606. (QueueView3->Items->Count > 0))
  7607. {
  7608. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7609. }
  7610. QueueLabelUpdateStatus();
  7611. }
  7612. //---------------------------------------------------------------------------
  7613. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7614. {
  7615. QueueLabelUpdateStatus();
  7616. }
  7617. //---------------------------------------------------------------------------
  7618. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7619. {
  7620. QueueLabel->UpdateStatus();
  7621. }
  7622. //---------------------------------------------------------------------------
  7623. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7624. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7625. {
  7626. if (Selected)
  7627. {
  7628. NonVisualDataModule->UpdateNonVisibleActions();
  7629. }
  7630. }
  7631. //---------------------------------------------------------------------------
  7632. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7633. {
  7634. TDragDropFilesEx * Result = NULL;
  7635. if (Sender == SessionsPageControl)
  7636. {
  7637. Result = FSessionsDragDropFilesEx;
  7638. }
  7639. else if (Sender == QueueView3)
  7640. {
  7641. Result = FQueueDragDropFilesEx;
  7642. }
  7643. else
  7644. {
  7645. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7646. if (DirView != NULL)
  7647. {
  7648. Result = DirView->DragDropFilesEx;
  7649. }
  7650. else
  7651. {
  7652. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7653. if (DriveView != NULL)
  7654. {
  7655. Result = DriveView->DragDropFilesEx;
  7656. }
  7657. }
  7658. }
  7659. DebugAssert(Result != NULL);
  7660. return Result;
  7661. }
  7662. //---------------------------------------------------------------------------
  7663. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7664. {
  7665. return (DView->SelCount == DView->FilesCount);
  7666. }
  7667. //---------------------------------------------------------------------------
  7668. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7669. {
  7670. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7671. }
  7672. //---------------------------------------------------------------------------
  7673. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7674. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7675. {
  7676. DebugAssert(!IsLocalBrowserMode());
  7677. TFileOperation Operation;
  7678. switch (Effect)
  7679. {
  7680. case DROPEFFECT_MOVE:
  7681. Operation = foMove;
  7682. break;
  7683. case DROPEFFECT_COPY:
  7684. // occures on WinXP (reported by user)
  7685. default:
  7686. Operation = foCopy;
  7687. break;
  7688. };
  7689. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7690. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7691. if (DragDropFilesEx->FileList->Count > 0)
  7692. {
  7693. TStrings * FileList = new TStringList();
  7694. try
  7695. {
  7696. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7697. {
  7698. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7699. }
  7700. FDragDropOperation = true;
  7701. TTransferOperationParam Param;
  7702. Param.TargetDirectory = TargetPath;
  7703. // upload, no temp dirs
  7704. Param.Temp = false;
  7705. Param.DragDrop = DragDrop;
  7706. Param.Options =
  7707. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7708. if (ForceQueue)
  7709. {
  7710. Param.Queue = asOn;
  7711. }
  7712. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7713. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7714. {
  7715. if (IsFileControl(DropSourceControl, osLocal))
  7716. {
  7717. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7718. }
  7719. else
  7720. {
  7721. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7722. }
  7723. }
  7724. }
  7725. __finally
  7726. {
  7727. FDragDropOperation = false;
  7728. delete FileList;
  7729. }
  7730. }
  7731. }
  7732. //---------------------------------------------------------------------------
  7733. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7734. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7735. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7736. {
  7737. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7738. }
  7739. //---------------------------------------------------------------------------
  7740. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7741. TObject * Sender, int grfKeyState, int & dwEffect)
  7742. {
  7743. DebugAssert(!IsLocalBrowserMode());
  7744. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7745. if ((dwEffect != DROPEFFECT_NONE) &&
  7746. IsFileControl(DropSourceControl, osRemote))
  7747. {
  7748. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7749. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7750. (RemoteDirView->DropTarget == NULL))
  7751. {
  7752. dwEffect = DROPEFFECT_NONE;
  7753. }
  7754. else
  7755. {
  7756. if (dwEffect == DROPEFFECT_COPY)
  7757. {
  7758. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  7759. // currently we support copying always (at least via temporary directory);
  7760. // remove associated checks once this all proves stable and working
  7761. bool CopyCapable = true;
  7762. // if we do not support neither of operations, there's no discussion
  7763. if (!MoveCapable && !CopyCapable)
  7764. {
  7765. dwEffect = DROPEFFECT_NONE;
  7766. }
  7767. else
  7768. {
  7769. // The default effect inside remote panel is move,
  7770. // unless we do not support it, but support copy
  7771. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7772. {
  7773. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  7774. }
  7775. else
  7776. {
  7777. // with ctrl-down, we want copy unless it is not supported
  7778. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  7779. }
  7780. }
  7781. }
  7782. }
  7783. }
  7784. }
  7785. //---------------------------------------------------------------------------
  7786. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7787. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7788. {
  7789. DebugAssert(!IsLocalBrowserMode());
  7790. if (FDDTotalSize >= 0)
  7791. {
  7792. if (File->IsDirectory)
  7793. {
  7794. FDDTotalSize = -1;
  7795. }
  7796. else
  7797. {
  7798. FDDTotalSize += File->Size;
  7799. }
  7800. }
  7801. DebugAssert(!FDragTempDir.IsEmpty());
  7802. // TODO: this is quite ineffective
  7803. // TODO: what if invalid character replacement is disabled?
  7804. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7805. UnicodeString TransferFileName;
  7806. if (Sender == RemoteDriveView)
  7807. {
  7808. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7809. }
  7810. else
  7811. {
  7812. TransferFileName = File->FileName;
  7813. }
  7814. FDDFileList->AddObject(TransferFileName, File);
  7815. }
  7816. //---------------------------------------------------------------------------
  7817. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7818. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7819. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7820. {
  7821. // sometimes we do not get DDEnd so the list is not released
  7822. SAFE_DESTROY(FDDFileList);
  7823. FDDFileList = new TStringList();
  7824. FDragTempDir = WinConfiguration->TemporaryDir();
  7825. FDDTotalSize = 0;
  7826. FDragDropSshTerminate.reset(NULL);
  7827. }
  7828. //---------------------------------------------------------------------------
  7829. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7830. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7831. HRESULT & Result)
  7832. {
  7833. if (Result == DRAGDROP_S_DROP)
  7834. {
  7835. try
  7836. {
  7837. GlobalDragImageList->HideDragImage();
  7838. try
  7839. {
  7840. RemoteFileControlDDTargetDrop();
  7841. }
  7842. catch(ESshTerminate & E)
  7843. {
  7844. FDragDropSshTerminate.reset(E.Clone());
  7845. }
  7846. }
  7847. catch (Exception &E)
  7848. {
  7849. // If downloading fails we need to cancel drag&drop, otherwise
  7850. // Explorer shows error
  7851. // But by the way exception probably never reach this point as
  7852. // it's catched on way.
  7853. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7854. Result = DRAGDROP_S_CANCEL;
  7855. ShowExtendedException(Terminal, &E);
  7856. }
  7857. }
  7858. }
  7859. //---------------------------------------------------------------------------
  7860. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7861. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7862. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7863. {
  7864. TFileMasks::TParams MaskParams;
  7865. MaskParams.Size = Size;
  7866. MaskParams.Modification = Modification;
  7867. // this does not re-parse the mask if it is the same as the last time
  7868. FDirViewMatchMask = Masks;
  7869. bool ImplicitMatch;
  7870. Matches =
  7871. // RecurseInclude parameter has no effect as the Path is empty
  7872. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7873. (AllowImplicitMatches || !ImplicitMatch);
  7874. }
  7875. //---------------------------------------------------------------------------
  7876. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7877. TObject * Sender, TListItem * Item, WORD & Indexes)
  7878. {
  7879. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7880. UnicodeString Ext;
  7881. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  7882. {
  7883. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7884. }
  7885. else
  7886. {
  7887. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7888. }
  7889. if (SameText(Ext, Configuration->PartialExt))
  7890. {
  7891. Indexes |= oiPartial;
  7892. }
  7893. }
  7894. //---------------------------------------------------------------------------
  7895. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7896. {
  7897. return
  7898. DirViewEnabled(osCurrent) &&
  7899. DirView(osCurrent)->CanPasteFromClipBoard();
  7900. }
  7901. //---------------------------------------------------------------------------
  7902. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7903. {
  7904. bool Result = false;
  7905. if (CanPasteToDirViewFromClipBoard())
  7906. {
  7907. Result = true;
  7908. }
  7909. else
  7910. {
  7911. UnicodeString ClipboardText;
  7912. if (NonEmptyTextFromClipboard(ClipboardText) &&
  7913. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  7914. {
  7915. if (StoredSessions->IsUrl(ClipboardText))
  7916. {
  7917. Result = true;
  7918. }
  7919. else
  7920. {
  7921. Result = DirViewEnabled(osCurrent);
  7922. }
  7923. }
  7924. }
  7925. return Result;
  7926. }
  7927. //---------------------------------------------------------------------------
  7928. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7929. {
  7930. if (DoesClipboardContainOurFiles())
  7931. {
  7932. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  7933. {
  7934. DebugAssert(!IsLocalBrowserMode());
  7935. TTerminalManager * Manager = TTerminalManager::Instance();
  7936. TTerminal * TargetTerminal = Manager->ActiveTerminal;
  7937. Manager->ActiveTerminal = FClipboardTerminal;
  7938. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TargetTerminal);
  7939. }
  7940. }
  7941. else if (CanPasteToDirViewFromClipBoard())
  7942. {
  7943. DirView(osCurrent)->PasteFromClipBoard();
  7944. }
  7945. else
  7946. {
  7947. UnicodeString ClipboardText;
  7948. if (NonEmptyTextFromClipboard(ClipboardText))
  7949. {
  7950. if (StoredSessions->IsUrl(ClipboardText))
  7951. {
  7952. NewSession(ClipboardText);
  7953. }
  7954. else
  7955. {
  7956. DirView(osCurrent)->Path = ClipboardText;
  7957. }
  7958. }
  7959. }
  7960. }
  7961. //---------------------------------------------------------------------------
  7962. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  7963. {
  7964. // TBD
  7965. }
  7966. //---------------------------------------------------------------------------
  7967. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  7968. {
  7969. TCustomDirView * ADirView = DirView(Side);
  7970. ADirView->SelectAll(Mode);
  7971. ADirView->SetFocus();
  7972. }
  7973. //---------------------------------------------------------------------------
  7974. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  7975. {
  7976. TCustomDirView * ADirView = DirView(Side);
  7977. TFileFilter Filter;
  7978. DefaultFileFilter(Filter);
  7979. if (DoSelectMaskDialog(ADirView, Select, Filter))
  7980. {
  7981. Configuration->Usage->Inc(L"MaskSelections");
  7982. ADirView->SelectFiles(Filter, Select);
  7983. ADirView->SetFocus();
  7984. }
  7985. }
  7986. //---------------------------------------------------------------------------
  7987. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  7988. {
  7989. TCustomDirView * ADirView = DirView(Side);
  7990. ADirView->RestoreSelectedNames();
  7991. ADirView->SetFocus();
  7992. }
  7993. //---------------------------------------------------------------------------
  7994. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  7995. {
  7996. TCustomDirView * CurrentDirView = DirView(osCurrent);
  7997. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  7998. {
  7999. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  8000. UnicodeString Ext;
  8001. if (!IsSideLocalBrowser(osCurrent))
  8002. {
  8003. Ext = UnixExtractFileExt(FileName);
  8004. }
  8005. else
  8006. {
  8007. Ext = ExtractFileExt(FileName);
  8008. }
  8009. if (Ext.IsEmpty())
  8010. {
  8011. Ext = L".";
  8012. }
  8013. TFileFilter Filter;
  8014. Filter.Masks = FORMAT(L"*%s", (Ext));
  8015. Filter.Directories = false;
  8016. CurrentDirView->SelectFiles(Filter, Select);
  8017. }
  8018. }
  8019. //---------------------------------------------------------------------------
  8020. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8021. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8022. {
  8023. UnicodeString Result;
  8024. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  8025. {
  8026. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  8027. }
  8028. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8029. {
  8030. // noop
  8031. }
  8032. else
  8033. {
  8034. Result =
  8035. FMTLOAD(FILE_INFO_FORMAT,
  8036. (FormatBytes(FileInfo.SelectedSize),
  8037. FormatBytes(FileInfo.FilesSize),
  8038. FormatNumber(FileInfo.SelectedCount),
  8039. FormatNumber(FileInfo.FilesCount)));
  8040. }
  8041. return Result;
  8042. }
  8043. //---------------------------------------------------------------------------
  8044. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8045. TTBXStatusPanel * Panel, TOperationSide Side)
  8046. {
  8047. if (Panel->Index == 1)
  8048. {
  8049. ToggleShowHiddenFiles();
  8050. }
  8051. else if (Panel->Index == 2)
  8052. {
  8053. Filter(Side);
  8054. }
  8055. }
  8056. //---------------------------------------------------------------------------
  8057. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8058. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8059. {
  8060. DebugAssert(!StatusBar->SimplePanel);
  8061. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8062. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8063. }
  8064. //---------------------------------------------------------------------------
  8065. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8066. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8067. {
  8068. DebugAssert(StatusBar->Panels->Count >= 3);
  8069. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8070. if (FileInfo.HiddenCount > 0)
  8071. {
  8072. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8073. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8074. }
  8075. else
  8076. {
  8077. HiddenFilesPanel->ViewPriority = 0;
  8078. // not really necessary, just to cleanup no-longer-valid data
  8079. HiddenFilesPanel->Caption = L"";
  8080. }
  8081. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8082. if (FileInfo.FilteredCount > 0)
  8083. {
  8084. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8085. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8086. }
  8087. else
  8088. {
  8089. FilteredFilesPanel->ViewPriority = 0;
  8090. // not really necessary, just to cleanup no-longer-valid data
  8091. FilteredFilesPanel->Caption = L"";
  8092. }
  8093. }
  8094. //---------------------------------------------------------------------------
  8095. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8096. TObject * /*Sender*/)
  8097. {
  8098. if (DirView(osRemote)->Enabled)
  8099. {
  8100. DirView(osRemote)->SetFocus();
  8101. }
  8102. }
  8103. //---------------------------------------------------------------------------
  8104. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8105. {
  8106. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8107. switch (Config.Show)
  8108. {
  8109. case qvShow:
  8110. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8111. {
  8112. Config.Show = qvHide;
  8113. }
  8114. else
  8115. {
  8116. Config.Show = Config.LastHideShow;
  8117. }
  8118. break;
  8119. case qvHideWhenEmpty:
  8120. if (ComponentVisible[fcQueueView])
  8121. {
  8122. Config.Show = qvHide;
  8123. }
  8124. else
  8125. {
  8126. Config.LastHideShow = Config.Show;
  8127. Config.Show = qvShow;
  8128. }
  8129. break;
  8130. case qvHide:
  8131. Config.LastHideShow = Config.Show;
  8132. Config.Show = qvShow;
  8133. break;
  8134. }
  8135. WinConfiguration->QueueView = Config;
  8136. }
  8137. //---------------------------------------------------------------------------
  8138. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8139. {
  8140. UnicodeString Result;
  8141. if (FTerminal != NULL)
  8142. {
  8143. switch (WinConfiguration->PathInCaption)
  8144. {
  8145. case picShort:
  8146. Result = TTerminalManager::Instance()->GetTerminalShortPath(FTerminal);
  8147. break;
  8148. case picFull:
  8149. Result = FTerminal->CurrentDirectory;
  8150. break;
  8151. }
  8152. }
  8153. return Result;
  8154. }
  8155. //---------------------------------------------------------------------------
  8156. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8157. {
  8158. TColor Result = GetWindowColor(FSessionColor);
  8159. return Result;
  8160. }
  8161. //---------------------------------------------------------------------------
  8162. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8163. {
  8164. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8165. return Result;
  8166. }
  8167. //---------------------------------------------------------------------------
  8168. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8169. {
  8170. TTerminalManager::Instance()->UpdateAppTitle();
  8171. // WORKAROUND: Disabling list view when it is not showing yet does not set its
  8172. // background to gray on Windows 7 (works on Windows 10).
  8173. // See also EnableControl
  8174. if (Showing)
  8175. {
  8176. if (Terminal != NULL)
  8177. {
  8178. // Update path when it changes
  8179. if ((SessionsPageControl->ActivePage != NULL) && (GetSessionTabTerminal(SessionsPageControl->ActivePage) == Terminal))
  8180. {
  8181. UpdateSessionTab(SessionsPageControl->ActivePage);
  8182. }
  8183. }
  8184. NonVisualDataModule->ReconnectSessionAction->Update();
  8185. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8186. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8187. CenterReconnectToolbar();
  8188. bool HasTerminal = HasActiveTerminal();
  8189. if (HasTerminal)
  8190. {
  8191. if (!RemoteDirView->Enabled)
  8192. {
  8193. RemoteDirView->Enabled = true;
  8194. if (FRemoteDirViewWasFocused)
  8195. {
  8196. ActiveControl = RemoteDirView;
  8197. }
  8198. }
  8199. RemoteDirView->Color = PanelColor();
  8200. }
  8201. else
  8202. {
  8203. if (RemoteDirView->Enabled)
  8204. {
  8205. // This is first called when the form is being constructed
  8206. // (not anymore due to Showing test above)
  8207. // but the false is overriden in the constructor later.
  8208. // An even later in TScpCommanderForm::DoShow()
  8209. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  8210. RemoteDirView->Enabled = false;
  8211. }
  8212. RemoteDirView->Color = DisabledPanelColor();
  8213. }
  8214. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8215. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8216. RemoteDriveView->Color = RemoteDirView->Color;
  8217. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8218. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8219. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8220. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8221. QueueFileList->Enabled = QueueView3->Enabled;
  8222. QueueFileList->Color = QueueView3->Color;
  8223. QueueFileList->Font->Color = QueueView3->Font->Color;
  8224. QueueLabelUpdateStatus();
  8225. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8226. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8227. if (FImmersiveDarkMode != WinConfiguration->UseDarkTheme())
  8228. {
  8229. UpdateDarkMode();
  8230. }
  8231. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  8232. }
  8233. }
  8234. //---------------------------------------------------------------------------
  8235. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8236. {
  8237. UpdateControls();
  8238. }
  8239. //---------------------------------------------------------------------------
  8240. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8241. TObject * Sender)
  8242. {
  8243. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8244. DebugAssert(DirView != NULL);
  8245. DoDirViewLoaded(DirView);
  8246. TransferPresetAutoSelect();
  8247. }
  8248. //---------------------------------------------------------------------------
  8249. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8250. {
  8251. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8252. // first run after installation
  8253. if (double(Updates.LastCheck) == 0)
  8254. {
  8255. // make sure next time there will be an update (if enabled)
  8256. Updates.LastCheck = TDateTime(1);
  8257. WinConfiguration->Updates = Updates;
  8258. }
  8259. else if ((double(Updates.Period) > 0) &&
  8260. (Now() - Updates.LastCheck >= Updates.Period))
  8261. {
  8262. TThreadMethod OnUpdatesChecked = NULL;
  8263. if (!IsUWP())
  8264. {
  8265. OnUpdatesChecked = UpdatesChecked;
  8266. }
  8267. StartUpdateThread(OnUpdatesChecked);
  8268. }
  8269. }
  8270. //---------------------------------------------------------------------------
  8271. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8272. {
  8273. UnicodeString Message;
  8274. bool New;
  8275. TQueryType Type;
  8276. GetUpdatesMessage(Message, New, Type, false);
  8277. if (!Message.IsEmpty())
  8278. {
  8279. if (New)
  8280. {
  8281. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8282. }
  8283. if (!New && (Type != qtWarning))
  8284. {
  8285. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8286. }
  8287. else
  8288. {
  8289. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8290. }
  8291. if (New)
  8292. {
  8293. Configuration->Usage->Inc(L"UpdateNotifications");
  8294. }
  8295. }
  8296. }
  8297. //---------------------------------------------------------------------------
  8298. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8299. {
  8300. RestoreApp();
  8301. bool CanDisplay = !NonVisualDataModule->Busy;
  8302. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8303. {
  8304. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8305. }
  8306. if (CanDisplay)
  8307. {
  8308. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8309. CheckForUpdates(true);
  8310. }
  8311. else
  8312. {
  8313. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8314. }
  8315. }
  8316. //---------------------------------------------------------------------------
  8317. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8318. TCopyParamRuleData & Data)
  8319. {
  8320. DebugAssert(Terminal != NULL);
  8321. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8322. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8323. Data.RemoteDirectory = RemoteDirView->PathName;
  8324. }
  8325. //---------------------------------------------------------------------------
  8326. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8327. {
  8328. // Terminal can be null when we are changing local directory implicitly,
  8329. // because it has been deleted, while no session is connected
  8330. // (Login dialog is open)
  8331. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8332. {
  8333. DebugAssert(!IsLocalBrowserMode());
  8334. TCopyParamRuleData Data;
  8335. GetTransferPresetAutoSelectData(Data);
  8336. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8337. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8338. if (CopyParamIndex < 0)
  8339. {
  8340. // there is no preset that matches autoselection
  8341. // set preset that we consider "default"
  8342. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8343. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8344. }
  8345. else
  8346. {
  8347. // there is preset matching autoselection
  8348. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8349. if (CopyParamName == FCopyParamAutoSelected)
  8350. {
  8351. // autoselected the same preset as the last time
  8352. // make no change (i.e. preserve custom user preset, if any)
  8353. }
  8354. else
  8355. {
  8356. // autoselected the different preset then the last time (or there
  8357. // was default preset set)
  8358. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8359. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8360. }
  8361. }
  8362. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8363. {
  8364. if (CopyParamIndex >= 0)
  8365. {
  8366. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8367. }
  8368. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8369. try
  8370. {
  8371. int Fmt =
  8372. (CopyParamIndex < 0) ?
  8373. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8374. COPY_PARAM_AUTOSELECTED;
  8375. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8376. Data->Message = MainInstructions(Message);
  8377. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8378. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8379. CopyParamAttrs);
  8380. if (CopyParamIndex >= 0)
  8381. {
  8382. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8383. Info = FORMAT(L"%s\n \n%s", (Info,
  8384. FMTLOAD(COPY_PARAM_RULE,
  8385. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8386. }
  8387. Data->Message += L"\n\n" + Info;
  8388. if (WinConfiguration->CopyParamAutoSelectNotice)
  8389. {
  8390. TransferPresetNoteMessage(Data, true);
  8391. }
  8392. else
  8393. {
  8394. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8395. Data = NULL; // ownership passed
  8396. }
  8397. }
  8398. __finally
  8399. {
  8400. delete Data;
  8401. }
  8402. }
  8403. }
  8404. }
  8405. //---------------------------------------------------------------------------
  8406. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8407. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8408. {
  8409. DebugAssert(NoteData != NULL);
  8410. DebugAssert(!IsLocalBrowserMode());
  8411. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8412. TQueryButtonAlias Aliases[1];
  8413. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8414. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8415. Params.Aliases = Aliases;
  8416. Params.AliasesCount = LENOF(Aliases);
  8417. unsigned int Result =
  8418. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8419. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8420. switch (Result)
  8421. {
  8422. case qaNeverAskAgain:
  8423. DebugAssert(AllowNeverAskAgain);
  8424. WinConfiguration->CopyParamAutoSelectNotice = false;
  8425. break;
  8426. case qaIgnore:
  8427. PreferencesDialog(pmPresets);
  8428. break;
  8429. }
  8430. }
  8431. //---------------------------------------------------------------------------
  8432. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8433. {
  8434. // as of now useless, as this is used for notes only, never for balloons, ...
  8435. RestoreApp();
  8436. // .. and we should never be busy here
  8437. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8438. {
  8439. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8440. }
  8441. }
  8442. //---------------------------------------------------------------------------
  8443. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8444. TPreferencesMode APreferencesMode)
  8445. {
  8446. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8447. TCopyParamRuleData Data;
  8448. if (Terminal != NULL)
  8449. {
  8450. PreferencesData.reset(new TPreferencesDialogData());
  8451. GetTransferPresetAutoSelectData(Data);
  8452. PreferencesData->CopyParamRuleData = &Data;
  8453. }
  8454. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8455. }
  8456. //---------------------------------------------------------------------------
  8457. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8458. const TCustomCommandType & Command)
  8459. {
  8460. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8461. {
  8462. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  8463. }
  8464. }
  8465. //---------------------------------------------------------------------------
  8466. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8467. {
  8468. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8469. TCustomCommandType Command;
  8470. // make sure we use local custom command when remote are not supported
  8471. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8472. {
  8473. Command = FLastCustomCommand;
  8474. }
  8475. else
  8476. {
  8477. Command.Params = Command.Params | ccLocal;
  8478. }
  8479. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8480. FEditingFocusedAdHocCommand = OnFocused;
  8481. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8482. int Options =
  8483. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8484. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8485. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8486. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8487. {
  8488. FLastCustomCommand = Command;
  8489. UpdateCustomCommandsToolbar();
  8490. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8491. }
  8492. }
  8493. //---------------------------------------------------------------------------
  8494. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8495. {
  8496. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8497. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8498. DebugAssert(State > 0);
  8499. if (State <= 0)
  8500. {
  8501. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  8502. }
  8503. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8504. }
  8505. //---------------------------------------------------------------------------
  8506. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8507. TCustomCommandType & Command, int & State)
  8508. {
  8509. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8510. if (Result)
  8511. {
  8512. Command = FLastCustomCommand;
  8513. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8514. }
  8515. return Result;
  8516. }
  8517. //---------------------------------------------------------------------------
  8518. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8519. {
  8520. ResetIncrementalSearch();
  8521. }
  8522. //---------------------------------------------------------------------------
  8523. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8524. {
  8525. DebugAssert(ComponentVisible[Component]);
  8526. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8527. }
  8528. //---------------------------------------------------------------------------
  8529. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8530. {
  8531. // when queue panel is resized here directly, the status bar is stretched
  8532. // over whole space the panel occupied
  8533. PostComponentHide(fcQueueView);
  8534. }
  8535. //---------------------------------------------------------------------------
  8536. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  8537. {
  8538. ComponentVisible[fcQueueFileList] = false;
  8539. }
  8540. //---------------------------------------------------------------------------
  8541. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  8542. {
  8543. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  8544. RefreshColorMode();
  8545. ResetSysDarkTheme();
  8546. ConfigurationChanged();
  8547. ConfigureInterface();
  8548. // Should be called for all controls
  8549. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  8550. }
  8551. //---------------------------------------------------------------------------
  8552. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  8553. {
  8554. // Do not handle, when shutting down anyway (maybe also when not setup completelly yet?)
  8555. if (!FInvalid &&
  8556. (Message.LParam != 0) &&
  8557. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  8558. {
  8559. ThemeChanged();
  8560. }
  8561. TForm::Dispatch(&Message);
  8562. }
  8563. //---------------------------------------------------------------------------
  8564. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8565. {
  8566. TMessage * M = static_cast<TMessage*>(Message);
  8567. switch (M->Msg)
  8568. {
  8569. case CM_DIALOGCHAR:
  8570. CMDialogChar(*M);
  8571. break;
  8572. case WM_APPCOMMAND:
  8573. WMAppCommand(*M);
  8574. break;
  8575. case WM_SYSCOMMAND:
  8576. WMSysCommand(*M);
  8577. break;
  8578. case WM_QUERYENDSESSION:
  8579. WMQueryEndSession(*M);
  8580. break;
  8581. case WM_ENDSESSION:
  8582. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8583. break;
  8584. #ifdef _DEBUG
  8585. case WM_WINDOWPOSCHANGED:
  8586. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8587. break;
  8588. case CM_CUSTOMSTYLECHANGED:
  8589. TForm::Dispatch(Message);
  8590. break;
  8591. #endif
  8592. case WM_COMPONENT_HIDE:
  8593. {
  8594. Byte Component = static_cast<Byte>(M->WParam);
  8595. // sanity check
  8596. if (ComponentVisible[Component])
  8597. {
  8598. // special treatment
  8599. if (Component == fcQueueView)
  8600. {
  8601. ToggleQueueVisibility();
  8602. DebugAssert(!ComponentVisible[fcQueueView]);
  8603. }
  8604. else
  8605. {
  8606. ComponentVisible[Component] = false;
  8607. }
  8608. }
  8609. }
  8610. break;
  8611. case WM_COPYDATA:
  8612. WMCopyData(*M);
  8613. break;
  8614. case WM_MANAGES_CAPTION:
  8615. // caption managed in TTerminalManager::UpdateAppTitle()
  8616. M->Result = 1;
  8617. break;
  8618. case WM_WANTS_MOUSEWHEEL:
  8619. M->Result = 1;
  8620. break;
  8621. case CM_SHOWINGCHANGED:
  8622. CMShowingChanged(*M);
  8623. break;
  8624. case WM_CLOSE:
  8625. WMClose(*M);
  8626. break;
  8627. case WM_DPICHANGED:
  8628. WMDpiChanged(*M);
  8629. break;
  8630. case CM_DPICHANGED:
  8631. CMDpiChanged(*M);
  8632. break;
  8633. case WM_SETTINGCHANGE:
  8634. WMSettingChange(*M);
  8635. break;
  8636. case CM_DIALOGKEY:
  8637. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8638. break;
  8639. default:
  8640. TForm::Dispatch(Message);
  8641. break;
  8642. }
  8643. }
  8644. //---------------------------------------------------------------------------
  8645. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8646. {
  8647. // noop
  8648. }
  8649. //---------------------------------------------------------------------------
  8650. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8651. {
  8652. TForm::Dispatch(&Message);
  8653. FSessionColors->Clear();
  8654. AddFixedSessionImages();
  8655. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8656. UpdateImages();
  8657. }
  8658. //---------------------------------------------------------------------------
  8659. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8660. {
  8661. TForm::Dispatch(&Message);
  8662. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8663. UpdatePixelsPerInchMainWindowCounter();
  8664. }
  8665. //---------------------------------------------------------------------------
  8666. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8667. {
  8668. // Cannot close window while we are busy.
  8669. // We cannot test this in FormCloseQuery as that is called also from
  8670. // Close(), which is called by CloseApplicationAction. So we can be busy
  8671. // there already even, when it is legitimate to close the application.
  8672. // Possibly a better place to handle this would be WMSysCommand.
  8673. if (NonVisualDataModule->Busy)
  8674. {
  8675. Message.Result = 1;
  8676. }
  8677. else
  8678. {
  8679. TForm::Dispatch(&Message);
  8680. }
  8681. }
  8682. //---------------------------------------------------------------------------
  8683. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8684. {
  8685. TForm::Dispatch(&Message);
  8686. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  8687. if (Showing && !FStarted)
  8688. {
  8689. FStarted = true;
  8690. InterfaceStarted();
  8691. }
  8692. if (Showing && (Terminal == NULL))
  8693. {
  8694. // When we are starting minimized (i.e. from an installer),
  8695. // postpone showing Login dialog until we get restored.
  8696. // Otherwise the Login dialog (and Authentication window) show restored
  8697. // over invisible (minimized) main window.
  8698. if (WindowState == wsMinimized)
  8699. {
  8700. FNeedSession = true;
  8701. }
  8702. else
  8703. {
  8704. // This happens before application ever goes idle, so the toolbars would
  8705. // stay enabled (initial state) until the Login dialog is dismissed.
  8706. UpdateActions();
  8707. NonVisualDataModule->StartBusy();
  8708. try
  8709. {
  8710. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8711. // otherwise the dialog does not receive focus.
  8712. // With Commander interface the ProcessMessages is called already
  8713. // by TDriveView, but with Explorer interface, we need to call it explicily
  8714. Application->ProcessMessages();
  8715. // do not reload sessions, they have been loaded just now (optimization)
  8716. NeedSession(true);
  8717. }
  8718. __finally
  8719. {
  8720. NonVisualDataModule->EndBusy();
  8721. }
  8722. }
  8723. }
  8724. }
  8725. //---------------------------------------------------------------------------
  8726. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  8727. {
  8728. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  8729. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  8730. }
  8731. //---------------------------------------------------------------------------
  8732. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8733. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8734. int & MaxHeight)
  8735. {
  8736. // workaround for bug in TWinControl.CalcConstraints
  8737. // Check for empty rect (restore from iconic state) is done there only after
  8738. // call to AdjustClientRect, which enlarges the rect (for forms).
  8739. TRect R = GetClientRect();
  8740. // when restoring from iconic state, place no restrictions
  8741. if (IsRectEmpty(R))
  8742. {
  8743. MinWidth = 0;
  8744. MinHeight = 0;
  8745. MaxWidth = 32000;
  8746. MaxHeight = 32000;
  8747. }
  8748. }
  8749. //---------------------------------------------------------------------------
  8750. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8751. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8752. {
  8753. // terminal can be already closed (e.g. dropped connection)
  8754. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8755. {
  8756. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8757. try
  8758. {
  8759. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8760. }
  8761. catch(...)
  8762. {
  8763. if (!Terminal->Active)
  8764. {
  8765. Close = true;
  8766. }
  8767. throw;
  8768. }
  8769. }
  8770. }
  8771. //---------------------------------------------------------------------------
  8772. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8773. {
  8774. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8775. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8776. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8777. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8778. {
  8779. OnGetSpaceAvailable = GetSpaceAvailable;
  8780. }
  8781. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8782. OnGetSpaceAvailable);
  8783. }
  8784. //---------------------------------------------------------------------------
  8785. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8786. {
  8787. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8788. Terminal->FillSessionDataForCode(Data.get());
  8789. return Data.release();
  8790. }
  8791. //---------------------------------------------------------------------------
  8792. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8793. {
  8794. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8795. DoGenerateUrlDialog(Data.get(), Paths);
  8796. }
  8797. //---------------------------------------------------------------------------
  8798. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8799. {
  8800. GenerateUrl(NULL);
  8801. }
  8802. //---------------------------------------------------------------------------
  8803. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8804. {
  8805. std::unique_ptr<TStrings> Paths(new TStringList());
  8806. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8807. GenerateUrl(Paths.get());
  8808. }
  8809. //---------------------------------------------------------------------------
  8810. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  8811. {
  8812. FSessionColor = value;
  8813. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8814. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8815. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8816. DebugAssert(ColorItem != NULL);
  8817. ColorItem->Color = C;
  8818. NonVisualDataModule->ColorMenuItem->Color = C;
  8819. UpdateControls();
  8820. }
  8821. //---------------------------------------------------------------------------
  8822. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8823. {
  8824. if (value != FSessionColor)
  8825. {
  8826. UpdateSessionColor(value);
  8827. // Is null when called from LastTerminalClosed
  8828. if (Terminal != NULL)
  8829. {
  8830. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8831. }
  8832. }
  8833. }
  8834. //---------------------------------------------------------------------------
  8835. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8836. {
  8837. bool Result = FNoteTimer->Enabled;
  8838. if (Result)
  8839. {
  8840. // cannot cancel note too early
  8841. bool NotEarly =
  8842. (Now() - FNoteShown >
  8843. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8844. if (Force || NotEarly)
  8845. {
  8846. FNoteTimer->Enabled = false;
  8847. FNote = L"";
  8848. SAFE_DESTROY(FNoteData);
  8849. FOnNoteClick = NULL;
  8850. FNoteHints = FNotes->Text;
  8851. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8852. UpdateStatusBar();
  8853. }
  8854. }
  8855. return Result;
  8856. }
  8857. //---------------------------------------------------------------------------
  8858. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8859. {
  8860. DebugAssert(FNoteTimer->Enabled);
  8861. CancelNote(true);
  8862. }
  8863. //---------------------------------------------------------------------------
  8864. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  8865. {
  8866. FNoteHints = FNotes->Text.TrimRight();
  8867. }
  8868. //---------------------------------------------------------------------------
  8869. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8870. {
  8871. int P = Note.Pos(L"\n");
  8872. if (P > 0)
  8873. {
  8874. Note.SetLength(P - 1);
  8875. }
  8876. FNotes->Add(FORMAT(L"[%s] %s",
  8877. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8878. while (FNotes->Count > 10)
  8879. {
  8880. FNotes->Delete(0);
  8881. }
  8882. if (UpdateNow)
  8883. {
  8884. UpdateNoteHints();
  8885. UpdateStatusBar();
  8886. }
  8887. }
  8888. //---------------------------------------------------------------------------
  8889. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8890. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8891. {
  8892. int P = Note.Pos(L"\n");
  8893. if (P > 0)
  8894. {
  8895. Note.SetLength(P - 1);
  8896. }
  8897. UpdateNoteHints();
  8898. FNote = Note;
  8899. SAFE_DESTROY(FNoteData);
  8900. FNoteData = NoteData;
  8901. FOnNoteClick = OnNoteClick;
  8902. AddNote(Note, false);
  8903. UpdateStatusBar();
  8904. FNoteShown = Now();
  8905. FNoteTimer->Enabled = false;
  8906. if (Seconds == 0)
  8907. {
  8908. Seconds = WinConfiguration->NotificationsTimeout;
  8909. }
  8910. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8911. FNoteTimer->Enabled = true;
  8912. }
  8913. //---------------------------------------------------------------------------
  8914. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8915. {
  8916. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8917. }
  8918. //---------------------------------------------------------------------------
  8919. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8920. {
  8921. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  8922. {
  8923. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8924. }
  8925. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  8926. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  8927. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  8928. 0, NULL, NULL);
  8929. }
  8930. //---------------------------------------------------------------------------
  8931. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  8932. {
  8933. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  8934. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  8935. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  8936. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  8937. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  8938. }
  8939. //---------------------------------------------------------------------------
  8940. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  8941. {
  8942. WinConfiguration->FormatSizeBytes = Style;
  8943. }
  8944. //---------------------------------------------------------------------------
  8945. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  8946. {
  8947. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  8948. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  8949. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  8950. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  8951. }
  8952. //---------------------------------------------------------------------------
  8953. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  8954. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  8955. {
  8956. if (Panel->Index == 0)
  8957. {
  8958. if (FOnNoteClick != NULL)
  8959. {
  8960. // prevent the user data from being freed by possible call
  8961. // to CancelNote or PostNote during call to OnNoteClick
  8962. std::unique_ptr<TObject> NoteData(FNoteData);
  8963. TNotifyEvent OnNoteClick = FOnNoteClick;
  8964. FNoteData = NULL;
  8965. // need to cancel the note as we are going to delete its user data
  8966. CancelNote(true);
  8967. OnNoteClick(NoteData.get());
  8968. }
  8969. }
  8970. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  8971. {
  8972. FileSystemInfo();
  8973. }
  8974. }
  8975. //---------------------------------------------------------------------------
  8976. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  8977. {
  8978. // workaround:
  8979. // 1) for unknown reason, disabling window, while minimized,
  8980. // prevents it from restoring, even if it was enabled again meanwhile
  8981. // 2) when disabling the main window, while another has focus
  8982. // minimize is no longer possible
  8983. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  8984. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  8985. // Shouldn't we use IsApplicationMinimized() here?
  8986. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  8987. {
  8988. Enabled = false;
  8989. }
  8990. FLockLevel++;
  8991. }
  8992. //---------------------------------------------------------------------------
  8993. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  8994. {
  8995. DebugAssert(FLockLevel > 0);
  8996. FLockLevel--;
  8997. if (FLockLevel == 0)
  8998. {
  8999. DebugAssert(FLockSuspendLevel == 0);
  9000. Enabled = true;
  9001. }
  9002. }
  9003. //---------------------------------------------------------------------------
  9004. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  9005. {
  9006. // We need to make sure that window is enabled when the last modal window closes
  9007. // otherwise focus is not restored correctly.
  9008. // So we re-enable the window when modal window opens and
  9009. // disable it back after is closes
  9010. if (FLockLevel > 0)
  9011. {
  9012. // while we have nesting counter, we know that we never be called
  9013. // recursivelly as Application->OnModalBegin is called only
  9014. // for the top-level modal window
  9015. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9016. {
  9017. // won't be disabled when conditions in LockWindow() were not satisfied
  9018. FDisabledOnLockSuspend = !Enabled;
  9019. // When minimized to tray (or actually when set to SW_HIDE),
  9020. // setting Enabled makes the window focusable even when there's
  9021. // modal window over it
  9022. if (!FTrayIcon->Visible)
  9023. {
  9024. Enabled = true;
  9025. }
  9026. }
  9027. FLockSuspendLevel++;
  9028. }
  9029. }
  9030. //---------------------------------------------------------------------------
  9031. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9032. {
  9033. if (FLockSuspendLevel > 0)
  9034. {
  9035. DebugAssert(FLockLevel > 0);
  9036. FLockSuspendLevel--;
  9037. // see comment in SuspendWindowLock
  9038. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9039. {
  9040. // Note that window can be enabled here, when we were minized to tray when
  9041. // was SuspendWindowLock() called.
  9042. // We should possibly do the same check as in LockWindow(),
  9043. // if it is ever possible that the consitions change between
  9044. // SuspendWindowLock() and ResumeWindowLock()
  9045. Enabled = !FDisabledOnLockSuspend;
  9046. }
  9047. }
  9048. }
  9049. //---------------------------------------------------------------------------
  9050. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9051. {
  9052. TTBXComboBoxItem * RemotePathComboBox =
  9053. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9054. if (!TextOnly)
  9055. {
  9056. TStrings * Items = RemotePathComboBox->Strings;
  9057. Items->BeginUpdate();
  9058. try
  9059. {
  9060. Items->Clear();
  9061. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9062. {
  9063. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9064. while (!IsUnixRootPath(APath))
  9065. {
  9066. int P = APath.LastDelimiter(L'/');
  9067. DebugAssert(P >= 0);
  9068. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9069. APath.SetLength(P - 1);
  9070. }
  9071. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9072. }
  9073. }
  9074. __finally
  9075. {
  9076. Items->EndUpdate();
  9077. }
  9078. }
  9079. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9080. // Setting ItemIndex to -1 does not reset its text
  9081. if (RemotePathComboBox->Strings->Count == 0)
  9082. {
  9083. RemotePathComboBox->Text = L"";
  9084. }
  9085. }
  9086. //---------------------------------------------------------------------------
  9087. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9088. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  9089. int & ImageIndex)
  9090. {
  9091. if (AIndex < 0)
  9092. {
  9093. AIndex = Sender->ItemIndex;
  9094. }
  9095. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9096. }
  9097. //---------------------------------------------------------------------------
  9098. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9099. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9100. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9101. {
  9102. ARect.Left += (10 * AIndex);
  9103. }
  9104. //---------------------------------------------------------------------------
  9105. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9106. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9107. {
  9108. AWidth += (10 * AIndex);
  9109. }
  9110. //---------------------------------------------------------------------------
  9111. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  9112. TObject * Sender)
  9113. {
  9114. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9115. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9116. int Index = RemotePathComboBox->ItemIndex;
  9117. while (Index < RemotePathComboBox->Strings->Count - 1)
  9118. {
  9119. APath = UnixExtractFileDir(APath);
  9120. Index++;
  9121. }
  9122. // VanDyke style paths
  9123. if (APath.IsEmpty())
  9124. {
  9125. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9126. APath = ROOTDIRECTORY;
  9127. }
  9128. if (RemoteDirView->Path != APath)
  9129. {
  9130. RemoteDirView->Path = APath;
  9131. }
  9132. }
  9133. //---------------------------------------------------------------------------
  9134. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9135. {
  9136. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9137. DebugUsedParam(Sender);
  9138. UpdateRemotePathComboBox(true);
  9139. }
  9140. //---------------------------------------------------------------------------
  9141. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9142. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9143. {
  9144. ResetIncrementalSearch();
  9145. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9146. DebugAssert(DirView != NULL);
  9147. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9148. {
  9149. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9150. // OnEditing is called also from TCustomListView::CanEdit
  9151. if (Edit != NULL)
  9152. {
  9153. EditSelectBaseName(Edit);
  9154. }
  9155. }
  9156. }
  9157. //---------------------------------------------------------------------------
  9158. //---------------------------------------------------------------------------
  9159. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9160. {
  9161. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9162. Result->AutoDetectDnD = false;
  9163. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9164. Result->RenderDataOn = rdoEnterAndDropSync;
  9165. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9166. return Result;
  9167. }
  9168. //---------------------------------------------------------------------------
  9169. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9170. {
  9171. if (IsWin10Build(2004))
  9172. {
  9173. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9174. BOOL DarkMode = FImmersiveDarkMode ? TRUE : FALSE;
  9175. const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  9176. DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
  9177. }
  9178. }
  9179. //---------------------------------------------------------------------------
  9180. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9181. {
  9182. TForm::CreateWnd();
  9183. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9184. UpdateDarkMode();
  9185. if (FSessionsDragDropFilesEx == NULL)
  9186. {
  9187. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9188. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9189. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9190. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9191. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9192. }
  9193. if (FQueueDragDropFilesEx == NULL)
  9194. {
  9195. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9196. // No need to set OnDragOver as we do not have any restrictions
  9197. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9198. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9199. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9200. }
  9201. }
  9202. //---------------------------------------------------------------------------
  9203. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9204. {
  9205. TForm::DestroyWnd();
  9206. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9207. FQueueDragDropFilesEx->DragDropControl = NULL;
  9208. }
  9209. //---------------------------------------------------------------------------
  9210. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9211. {
  9212. SideEnter(FCurrentSide);
  9213. FEverShown = true;
  9214. }
  9215. //---------------------------------------------------------------------------
  9216. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9217. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9218. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9219. {
  9220. if (!NonVisualDataModule->Busy)
  9221. {
  9222. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9223. Configuration->Usage->Inc(L"FileFinds");
  9224. LockWindow(true);
  9225. NonVisualDataModule->StartBusy();
  9226. WinConfiguration->LockedInterface = true;
  9227. try
  9228. {
  9229. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9230. }
  9231. __finally
  9232. {
  9233. WinConfiguration->LockedInterface = false;
  9234. NonVisualDataModule->EndBusy();
  9235. UnlockWindow();
  9236. }
  9237. }
  9238. }
  9239. //---------------------------------------------------------------------------
  9240. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9241. {
  9242. if (!NonVisualDataModule->Busy)
  9243. {
  9244. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9245. SetFocus();
  9246. RemoteDirView->Path = UnixExtractFilePath(Path);
  9247. UnicodeString FileName = UnixExtractFileName(Path);
  9248. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9249. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9250. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9251. if (Item == NULL)
  9252. {
  9253. ReloadDirectory(osRemote);
  9254. Item = RemoteDirView->FindFileItem(FileName);
  9255. }
  9256. if (Item != NULL)
  9257. {
  9258. RemoteDirView->ItemFocused = Item;
  9259. RemoteDirView->ItemFocused->MakeVisible(false);
  9260. RemoteDirView->SetFocus();
  9261. }
  9262. }
  9263. }
  9264. //---------------------------------------------------------------------------
  9265. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9266. {
  9267. bool Result = !NonVisualDataModule->Busy;
  9268. if (Result)
  9269. {
  9270. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9271. }
  9272. return Result;
  9273. }
  9274. //---------------------------------------------------------------------------
  9275. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9276. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9277. {
  9278. if (CanOperateOnFoundFiles(ATerminal))
  9279. {
  9280. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9281. FOnFileOperationFinished = OnFileOperationFinished;
  9282. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9283. }
  9284. }
  9285. //---------------------------------------------------------------------------
  9286. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9287. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9288. {
  9289. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9290. }
  9291. //---------------------------------------------------------------------------
  9292. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9293. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9294. {
  9295. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9296. }
  9297. //---------------------------------------------------------------------------
  9298. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9299. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9300. {
  9301. if (CanOperateOnFoundFiles(ATerminal))
  9302. {
  9303. for (int Index = 0; Index < FileList->Count; Index++)
  9304. {
  9305. UnicodeString FileName = FileList->Strings[Index];
  9306. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9307. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9308. OnFileOperationFinished(FileName, true);
  9309. }
  9310. }
  9311. }
  9312. //---------------------------------------------------------------------------
  9313. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9314. {
  9315. FFileFindTerminal = Terminal;
  9316. ShowFileFindDialog(
  9317. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9318. DoDownloadFoundFiles, DoEditFoundFiles);
  9319. }
  9320. //---------------------------------------------------------------------------
  9321. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9322. {
  9323. FTaskbarList = TaskbarList;
  9324. }
  9325. //---------------------------------------------------------------------------
  9326. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9327. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9328. {
  9329. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9330. if (Index >= 0)
  9331. {
  9332. if (Button == mbLeft)
  9333. {
  9334. // WORKAROUND: "Mouse down" is raised only after tab is switched.
  9335. // If switching tab (switching session) takes long enough for user
  9336. // to actually release the button, "mouse down" is still raised,
  9337. // but we do not get "mouse up" event, so dragging is not cancelled,
  9338. // prevent that by not beginning dragging in the first place.
  9339. if (IsKeyPressed(VK_LBUTTON))
  9340. {
  9341. // when user clicks the "+", we get mouse down only after the session
  9342. // is closed, when new session tab is already on X:Y, so dragging
  9343. // starts, prevent that
  9344. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  9345. {
  9346. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9347. if (Terminal != NULL)
  9348. {
  9349. SessionsPageControl->BeginDrag(false);
  9350. }
  9351. }
  9352. }
  9353. }
  9354. else if (Button == mbMiddle)
  9355. {
  9356. // ignore middle-click for "New session tab"
  9357. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9358. if (Terminal != NULL)
  9359. {
  9360. CloseSessionTab(Index);
  9361. }
  9362. }
  9363. }
  9364. }
  9365. //---------------------------------------------------------------------------
  9366. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9367. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9368. {
  9369. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9370. // do not allow dropping on the "+" tab
  9371. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9372. if ((TargetTerminal != NULL) &&
  9373. (SessionsPageControl->ActivePage->PageIndex != Index))
  9374. {
  9375. Configuration->Usage->Inc(L"SessionTabMoves");
  9376. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  9377. // but we want to actually prevent that to avoid flicker
  9378. SessionsPageControl->ActivePage->PageIndex = Index;
  9379. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  9380. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9381. }
  9382. }
  9383. //---------------------------------------------------------------------------
  9384. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9385. TObject * Sender, TObject * Source, int X, int Y,
  9386. TDragState /*State*/, bool & Accept)
  9387. {
  9388. Accept = (Sender == Source);
  9389. if (Accept)
  9390. {
  9391. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9392. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9393. // do not allow dragging to the "+" tab
  9394. Accept = (Terminal != NULL);
  9395. }
  9396. }
  9397. //---------------------------------------------------------------------------
  9398. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  9399. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  9400. {
  9401. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9402. if (Index < 0)
  9403. {
  9404. Effect = DROPEFFECT_NONE;
  9405. }
  9406. else
  9407. {
  9408. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9409. // do not allow dropping on the "+" and disconnected tabs
  9410. if (!IsActiveTerminal(TargetTerminal))
  9411. {
  9412. Effect = DROPEFFECT_NONE;
  9413. }
  9414. else if ((TargetTerminal != Terminal) && (Effect == DROPEFFECT_MOVE))
  9415. {
  9416. Effect = DROPEFFECT_COPY;
  9417. }
  9418. }
  9419. }
  9420. //---------------------------------------------------------------------------
  9421. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9422. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9423. {
  9424. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9425. // do not allow dropping on the "+" and disconnected tabs
  9426. TManagedTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9427. if (IsActiveTerminal(TargetTerminal))
  9428. {
  9429. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9430. if (!IsFileControl(DropSourceControl, osRemote))
  9431. {
  9432. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  9433. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9434. // Why don't we use Terminal->CurrentDirectory directly?
  9435. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9436. // do not force queue, drag drop
  9437. false, true);
  9438. }
  9439. }
  9440. }
  9441. //---------------------------------------------------------------------------
  9442. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9443. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9444. {
  9445. // Downloads are handled in RemoteFileControlDDEnd
  9446. if (!IsFileControl(DropSourceControl, osRemote))
  9447. {
  9448. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9449. Terminal->CurrentDirectory,
  9450. // force queue, drag drop
  9451. true, true);
  9452. }
  9453. }
  9454. //---------------------------------------------------------------------------
  9455. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9456. {
  9457. FShowing = false;
  9458. // Do not save empty workspace
  9459. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  9460. {
  9461. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9462. UnicodeString Name = WorkspaceName();
  9463. DoSaveWorkspace(
  9464. Name, DataList.get(),
  9465. !Configuration->DisablePasswordStoring &&
  9466. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9467. WinConfiguration->LastStoredSession = Name;
  9468. }
  9469. }
  9470. //---------------------------------------------------------------------------
  9471. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9472. {
  9473. TManagedTerminal * ManagedTerminal =
  9474. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9475. if (ManagedTerminal != NULL)
  9476. {
  9477. ManagedTerminal->DirectoryLoaded = Now();
  9478. }
  9479. }
  9480. //---------------------------------------------------------------------------
  9481. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9482. TListItem * /*Item*/, bool /*Selected*/)
  9483. {
  9484. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9485. switch (DirView->LastSelectMethod)
  9486. {
  9487. case smKeyboard:
  9488. Configuration->Usage->Inc(L"KeyboardSelections");
  9489. break;
  9490. case smMouse:
  9491. Configuration->Usage->Inc(L"MouseSelections");
  9492. break;
  9493. }
  9494. }
  9495. //---------------------------------------------------------------------------
  9496. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9497. {
  9498. if (Color != 0)
  9499. {
  9500. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9501. }
  9502. else
  9503. {
  9504. return FSessionTabImageIndex;
  9505. }
  9506. }
  9507. //---------------------------------------------------------------------------
  9508. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9509. {
  9510. TPngImageCollectionItem * Item =
  9511. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9512. FSessionColors->AddPng(Item->PngImage);
  9513. return FSessionColors->Count - 1;
  9514. }
  9515. //---------------------------------------------------------------------------
  9516. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9517. {
  9518. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9519. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  9520. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9521. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9522. }
  9523. //---------------------------------------------------------------------------
  9524. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9525. {
  9526. for (int Index = 0; Index < Control->ControlCount; Index++)
  9527. {
  9528. TControl * AControl = Control->Controls[Index];
  9529. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9530. if (WinControl != NULL)
  9531. {
  9532. CollectItemsWithTextDisplayMode(WinControl);
  9533. }
  9534. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9535. if (Toolbar != NULL)
  9536. {
  9537. // we care for top-level items only
  9538. TTBCustomItem * Items = Toolbar->Items;
  9539. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9540. {
  9541. TTBCustomItem * Item = Items->Items[ItemIndex];
  9542. if (((Item->DisplayMode == nbdmImageAndText) ||
  9543. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9544. EligibleForImageDisplayMode(Item))
  9545. {
  9546. FItemsWithTextDisplayMode.insert(Item);
  9547. }
  9548. }
  9549. }
  9550. }
  9551. }
  9552. //---------------------------------------------------------------------------
  9553. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9554. {
  9555. return true;
  9556. }
  9557. //---------------------------------------------------------------------------
  9558. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9559. {
  9560. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9561. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9562. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9563. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9564. bool Result = true;
  9565. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9566. {
  9567. TTBCustomItem * Item = *i;
  9568. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9569. if (Label != NULL)
  9570. {
  9571. // optimization
  9572. if (Label->Visible == SelectiveToolbarText)
  9573. {
  9574. Result = false;
  9575. }
  9576. else
  9577. {
  9578. Label->Visible = SelectiveToolbarText;
  9579. }
  9580. }
  9581. else
  9582. {
  9583. // optimization
  9584. if (Item->DisplayMode == DisplayMode)
  9585. {
  9586. Result = false;
  9587. }
  9588. else
  9589. {
  9590. Item->DisplayMode = DisplayMode;
  9591. }
  9592. }
  9593. ++i;
  9594. }
  9595. if (Result)
  9596. {
  9597. UpdateNewSessionTab();
  9598. }
  9599. return Result;
  9600. }
  9601. //---------------------------------------------------------------------------
  9602. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9603. {
  9604. DebugFail();
  9605. }
  9606. //---------------------------------------------------------------------------
  9607. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9608. {
  9609. // Among other, a lock level is non zero, while directory is being loaded,
  9610. // even when it happens as a result of dirview navigation,
  9611. // i.e. when TNonVisualDataModule is NOT FBusy.
  9612. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9613. // Among other this prevents a panel auto update to occur while
  9614. // directory is changing.
  9615. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9616. }
  9617. //---------------------------------------------------------------------------
  9618. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  9619. {
  9620. // While the window is disabled, we still seem to process menu shortcuts at least,
  9621. // so stop it at least here.
  9622. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9623. return
  9624. (Allowed == aaUpdate) ||
  9625. !NonVisualDataModule->Busy;
  9626. }
  9627. //---------------------------------------------------------------------------
  9628. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9629. {
  9630. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9631. }
  9632. //---------------------------------------------------------------------------
  9633. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9634. {
  9635. // This is somewhat redundant to LockWindow() call from
  9636. // TTerminalManager::TerminalReadDirectoryProgress.
  9637. // But disabling window is known not to block keyboard shorcuts
  9638. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9639. if (Busy > 0)
  9640. {
  9641. if (NonVisualDataModule->Busy)
  9642. {
  9643. State = false;
  9644. }
  9645. else
  9646. {
  9647. NonVisualDataModule->StartBusy();
  9648. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9649. LockWindow();
  9650. }
  9651. }
  9652. else if (Busy < 0)
  9653. {
  9654. UnlockWindow();
  9655. NonVisualDataModule->EndBusy();
  9656. }
  9657. else
  9658. {
  9659. State = NonVisualDataModule->Busy;
  9660. }
  9661. }
  9662. //---------------------------------------------------------------------------
  9663. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9664. {
  9665. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9666. // no context menu for "New session tab"
  9667. if ((Index >= 0) && (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != NULL))
  9668. {
  9669. SessionsPageControl->ActivePageIndex = Index;
  9670. if (DebugAlwaysTrue(SessionTabSwitched()))
  9671. {
  9672. // copied from TControl.WMContextMenu
  9673. SendCancelMode(SessionsPageControl);
  9674. // explicit popup instead of using PopupMenu property
  9675. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9676. // while connecting yet not-connected session and hence
  9677. // allowing an access to commands over not-completelly connected session
  9678. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9679. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  9680. PopupMenu->PopupComponent = SessionsPageControl;
  9681. PopupMenu->Popup(Point.x, Point.y);
  9682. }
  9683. }
  9684. Handled = true;
  9685. }
  9686. //---------------------------------------------------------------------------
  9687. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9688. {
  9689. return HasActiveTerminal() && Terminal->IsCapable[fcChangePassword];
  9690. }
  9691. //---------------------------------------------------------------------------
  9692. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9693. {
  9694. Configuration->Usage->Inc(L"PasswordChanges");
  9695. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9696. ChangePasswordData->DisableAuthentationsExceptPassword();
  9697. ChangePasswordData->ChangePassword = true;
  9698. std::unique_ptr<TTerminal> ChangePasswordSession(
  9699. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9700. ChangePasswordSession->AutoReadDirectory = false;
  9701. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9702. {
  9703. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9704. }
  9705. else
  9706. {
  9707. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9708. }
  9709. }
  9710. //---------------------------------------------------------------------------
  9711. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9712. {
  9713. // No nice way to assert SSH2
  9714. return HasActiveTerminal() && (Terminal->FSProtocol == cfsSFTP);
  9715. }
  9716. //---------------------------------------------------------------------------
  9717. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9718. {
  9719. TOperationVisualizer OperationVisualizer;
  9720. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9721. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9722. }
  9723. //---------------------------------------------------------------------------
  9724. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9725. {
  9726. TForm::ChangeScale(M, D);
  9727. int APixelsPerInch = GetControlPixelsPerInch(this);
  9728. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9729. try
  9730. {
  9731. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9732. }
  9733. __finally
  9734. {
  9735. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9736. }
  9737. }
  9738. //---------------------------------------------------------------------------
  9739. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9740. {
  9741. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9742. }
  9743. //---------------------------------------------------------------------
  9744. class TPublicControl : public TControl
  9745. {
  9746. friend class TCustomScpExplorerForm;
  9747. };
  9748. //---------------------------------------------------------------------------
  9749. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9750. {
  9751. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9752. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9753. }
  9754. //---------------------------------------------------------------------------
  9755. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9756. {
  9757. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9758. OpenBookmark(Side, Bookmark);
  9759. }
  9760. //---------------------------------------------------------------------------
  9761. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9762. {
  9763. DoBookmarkClick(osLocal, Sender);
  9764. }
  9765. //---------------------------------------------------------------------------
  9766. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9767. {
  9768. DoBookmarkClick(osRemote, Sender);
  9769. }
  9770. //---------------------------------------------------------------------------
  9771. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9772. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9773. {
  9774. if (BookmarkList != NULL)
  9775. {
  9776. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9777. if (!WinConfiguration->UseLocationProfiles)
  9778. {
  9779. std::unique_ptr<TStringList> Directories(new TStringList());
  9780. Directories->CaseSensitive = (Side == osRemote);
  9781. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9782. {
  9783. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9784. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9785. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9786. {
  9787. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9788. Item->Caption = EscapeHotkey(Directory);
  9789. Item->ShortCut = Bookmark->ShortCut;
  9790. Item->OnClick = OnBookmarkClick;
  9791. Item->Tag = reinterpret_cast<int>(Bookmark);
  9792. Directories->Add(Directory);
  9793. Menu->Add(Item.release());
  9794. }
  9795. }
  9796. }
  9797. else
  9798. {
  9799. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9800. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9801. {
  9802. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9803. if (!Bookmark->Node.IsEmpty())
  9804. {
  9805. Folders->Add(Bookmark->Node);
  9806. }
  9807. }
  9808. for (int Index = 0; Index < Folders->Count; Index++)
  9809. {
  9810. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9811. Item->Caption = Folders->Strings[Index];
  9812. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction->ImageIndex;
  9813. Folders->Objects[Index] = Item.get();
  9814. Menu->Add(Item.release());
  9815. }
  9816. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9817. {
  9818. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9819. TTBCustomItem * Parent;
  9820. if (!Bookmark->Node.IsEmpty())
  9821. {
  9822. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9823. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9824. }
  9825. else
  9826. {
  9827. Parent = Menu;
  9828. }
  9829. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9830. Item->Caption = Bookmark->Name;
  9831. Item->ShortCut = Bookmark->ShortCut;
  9832. Item->OnClick = OnBookmarkClick;
  9833. Item->Tag = reinterpret_cast<int>(Bookmark);
  9834. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9835. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9836. {
  9837. // No hint for location profiles that are actually mere "bookmarks"
  9838. }
  9839. else
  9840. {
  9841. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  9842. UnicodeString LongHint;
  9843. if (!Bookmark->Local.IsEmpty())
  9844. {
  9845. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  9846. }
  9847. if (!Bookmark->Remote.IsEmpty())
  9848. {
  9849. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  9850. }
  9851. AddToList(Hint, LongHint, L"|");
  9852. Item->Hint = Hint;
  9853. }
  9854. Parent->Add(Item.release());
  9855. }
  9856. }
  9857. AddMenuSeparator(Menu);
  9858. }
  9859. }
  9860. //---------------------------------------------------------------------------
  9861. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  9862. {
  9863. Menu->Clear();
  9864. std::unique_ptr<TTBCustomItem> Item;
  9865. Item.reset(new TTBXItem(Owner));
  9866. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  9867. Item->Options = Item->Options << tboDefault;
  9868. Menu->Add(Item.release());
  9869. AddMenuSeparator(Menu);
  9870. if (Terminal != NULL)
  9871. {
  9872. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  9873. }
  9874. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  9875. Item.reset(new TTBXItem(Owner));
  9876. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction : NonVisualDataModule->RemoteAddBookmarkAction;
  9877. Menu->Add(Item.release());
  9878. }
  9879. //---------------------------------------------------------------------------
  9880. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  9881. {
  9882. bool Result = true;
  9883. try
  9884. {
  9885. if (Side == osRemote)
  9886. {
  9887. Terminal->ExceptionOnFail = true;
  9888. }
  9889. try
  9890. {
  9891. DirView(Side)->Path = Path;
  9892. }
  9893. __finally
  9894. {
  9895. if (Side == osRemote)
  9896. {
  9897. Terminal->ExceptionOnFail = false;
  9898. }
  9899. }
  9900. }
  9901. catch (Exception & E)
  9902. {
  9903. if ((Side != osRemote) || Terminal->Active)
  9904. {
  9905. ShowExtendedExceptionEx(Terminal, &E);
  9906. Result = false;
  9907. }
  9908. else
  9909. {
  9910. throw;
  9911. }
  9912. }
  9913. return Result;
  9914. }
  9915. //---------------------------------------------------------------------------
  9916. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  9917. {
  9918. // Make sure there some feedback even when loading tiny local folders
  9919. TInstantOperationVisualizer Visualizer;
  9920. DirView(Side)->ReloadDirectory();
  9921. }
  9922. //---------------------------------------------------------------------------
  9923. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  9924. {
  9925. if (Index == SessionsPageControl->TabIndex)
  9926. {
  9927. CloseSession();
  9928. }
  9929. else
  9930. {
  9931. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  9932. TTerminal * Terminal = GetSessionTabTerminal(TabSheet);
  9933. TTerminalManager * Manager = TTerminalManager::Instance();
  9934. if (!Terminal->Active)
  9935. {
  9936. Manager->FreeTerminal(Terminal);
  9937. }
  9938. else
  9939. {
  9940. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  9941. if (CanCloseQueue(Queue))
  9942. {
  9943. Manager->FreeTerminal(Terminal);
  9944. }
  9945. }
  9946. }
  9947. }
  9948. //---------------------------------------------------------------------------
  9949. void __fastcall TCustomScpExplorerForm::SessionsPageControlCloseButtonClick(TPageControl * /*Sender*/, int Index)
  9950. {
  9951. CloseSessionTab(Index);
  9952. }
  9953. //---------------------------------------------------------------------------
  9954. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  9955. {
  9956. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  9957. {
  9958. TInstantOperationVisualizer Visualizer;
  9959. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  9960. // before we create a new data object
  9961. ClipboardClear();
  9962. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  9963. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  9964. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  9965. // Intermediate variable to WORKAROUND a crash in compiler
  9966. TObjectList * ClipboardFakeMonitors =
  9967. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  9968. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  9969. if (FClipboardDragDropFilesEx.get() == NULL)
  9970. {
  9971. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  9972. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  9973. }
  9974. FClipboardDragDropFilesEx->FileList->Clear();
  9975. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  9976. FClipboardDragDropFilesEx->CopyToClipboard();
  9977. FClipboardTerminal = FTerminal;
  9978. // Need full paths, as cwd can be different once files are pasted
  9979. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  9980. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  9981. {
  9982. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  9983. }
  9984. }
  9985. }
  9986. //---------------------------------------------------------------------------
  9987. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  9988. {
  9989. if (OpenClipboard(0))
  9990. {
  9991. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  9992. CloseClipboard();
  9993. }
  9994. }
  9995. //---------------------------------------------------------------------------
  9996. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  9997. {
  9998. return !FClipboardFakeDirectory.IsEmpty();
  9999. }
  10000. //---------------------------------------------------------------------------
  10001. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  10002. {
  10003. RemoveDir(ApiPath(FClipboardFakeDirectory));
  10004. FClipboardFakeDirectory = UnicodeString();
  10005. FClipboardTerminal = NULL;
  10006. if (FDownloadingFromClipboard)
  10007. {
  10008. // We are called by the monitor, so attempt to release it would deadlonk
  10009. FClipboardFakeMonitorsPendingReset = true;
  10010. }
  10011. else
  10012. {
  10013. FClipboardFakeMonitors.reset(NULL);
  10014. }
  10015. }
  10016. //---------------------------------------------------------------------------
  10017. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10018. {
  10019. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10020. {
  10021. ClipboardStop();
  10022. }
  10023. }
  10024. //---------------------------------------------------------------------------
  10025. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10026. {
  10027. TTerminalManager * Manager = TTerminalManager::Instance();
  10028. Manager->ActiveTerminal = FClipboardTerminal;
  10029. TTransferOperationParam Params;
  10030. Params.TargetDirectory = TargetDirectory;
  10031. Params.DragDrop = DragDrop;
  10032. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10033. }
  10034. //---------------------------------------------------------------------------
  10035. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10036. {
  10037. // It can actually rarelly happen that some random file is created, while we are shuttting down the monitor
  10038. // (as it pumps a Windows message queue while being shutted down)
  10039. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10040. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10041. {
  10042. // Can fail as it can be e.g. locked by AV, so we retry that later
  10043. bool Removed = RemoveDir(ApiPath(FileName));
  10044. try
  10045. {
  10046. if (!NonVisualDataModule->Busy)
  10047. {
  10048. DebugAssert(!FClipboardFakeMonitorsPendingReset);
  10049. FClipboardFakeMonitorsPendingReset = false;
  10050. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10051. TAutoFlag Flag(FDownloadingFromClipboard);
  10052. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  10053. }
  10054. }
  10055. __finally
  10056. {
  10057. if (!Removed)
  10058. {
  10059. RemoveDir(ApiPath(FileName));
  10060. }
  10061. }
  10062. }
  10063. }
  10064. //---------------------------------------------------------------------------
  10065. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10066. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10067. {
  10068. TFileMasks::TParams MaskParams;
  10069. MaskParams.Size = Size;
  10070. MaskParams.Modification = Modification;
  10071. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10072. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10073. {
  10074. bool ImplicitMatch;
  10075. if (Iter->FileMask.Matches(FileName, Directory, DirView->PathName, &MaskParams, false, ImplicitMatch) &&
  10076. !ImplicitMatch)
  10077. {
  10078. Color = Iter->Color;
  10079. break;
  10080. }
  10081. }
  10082. }
  10083. //---------------------------------------------------------------------------
  10084. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10085. {
  10086. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10087. if (!DirView->IsEditing())
  10088. {
  10089. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10090. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10091. // while searching
  10092. if (Key == VK_BACK)
  10093. {
  10094. if (!FIncrementalSearch.IsEmpty())
  10095. {
  10096. if (FIncrementalSearch.Length() == 1)
  10097. {
  10098. ResetIncrementalSearch();
  10099. }
  10100. else
  10101. {
  10102. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  10103. IncrementalSearch(NewText, false, false);
  10104. }
  10105. Key = 0;
  10106. }
  10107. }
  10108. }
  10109. }
  10110. //---------------------------------------------------------------------------
  10111. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  10112. {
  10113. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10114. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  10115. {
  10116. // Filter control sequences.
  10117. // When not searching yet, prefer use of the space for toggling file selection
  10118. // (so we cannot incrementally search for a string starting with a space).
  10119. if ((Key > VK_SPACE) ||
  10120. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  10121. {
  10122. IncrementalSearch(FIncrementalSearch + Key, false, false);
  10123. Key = 0;
  10124. }
  10125. }
  10126. }
  10127. //---------------------------------------------------------------------------
  10128. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  10129. {
  10130. if (!FIncrementalSearch.IsEmpty())
  10131. {
  10132. FIncrementalSearch = L"";
  10133. DirView(osCurrent)->UpdateStatusBar();
  10134. }
  10135. }
  10136. //---------------------------------------------------------------------------
  10137. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10138. {
  10139. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  10140. if (Item == NULL)
  10141. {
  10142. MessageBeep(MB_ICONHAND);
  10143. }
  10144. else
  10145. {
  10146. TCustomDirView * ADirView = DirView(osCurrent);
  10147. {
  10148. TAutoNestingCounter Guard(FIncrementalSearching);
  10149. ADirView->FocusItem(Item);
  10150. if (ADirView->NortonLike == nlOff)
  10151. {
  10152. if ((ADirView->Selected != Item) ||
  10153. (ADirView->SelCount > 1))
  10154. {
  10155. ADirView->ClearSelection();
  10156. ADirView->Selected = Item;
  10157. }
  10158. }
  10159. }
  10160. FIncrementalSearch = Text;
  10161. Item->MakeVisible(false);
  10162. TListItem * NextItem = SearchFile(Text, true, Reverse);
  10163. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  10164. ADirView->UpdateStatusBar();
  10165. }
  10166. }
  10167. //---------------------------------------------------------------------------
  10168. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  10169. {
  10170. int Index = Item->Index;
  10171. if (!Reverse)
  10172. {
  10173. Index++;
  10174. if (Index >= Item->Owner->Count)
  10175. {
  10176. Index = 0;
  10177. }
  10178. }
  10179. else
  10180. {
  10181. Index--;
  10182. if (Index < 0)
  10183. {
  10184. Index = Item->Owner->Count - 1;
  10185. }
  10186. }
  10187. return Item->Owner->Item[Index];
  10188. }
  10189. //---------------------------------------------------------------------------
  10190. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10191. {
  10192. TCustomDirView * ADirView = DirView(osCurrent);
  10193. if (ADirView->Items->Count == 0)
  10194. {
  10195. return NULL;
  10196. }
  10197. else
  10198. {
  10199. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  10200. TListItem * Item = CurrentItem;
  10201. if (SkipCurrent)
  10202. {
  10203. Item = DebugNotNull(GetNextFile(Item, Reverse));
  10204. }
  10205. while (true)
  10206. {
  10207. bool Matches = false;
  10208. switch (WinConfiguration->PanelSearch)
  10209. {
  10210. case isNameStartOnly:
  10211. Matches = ContainsTextSemiCaseSensitive(Item->Caption.SubString(1, Text.Length()), Text);
  10212. break;
  10213. case isName:
  10214. Matches = ContainsTextSemiCaseSensitive(Item->Caption, Text);
  10215. break;
  10216. case isAll:
  10217. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  10218. int Index = 0;
  10219. while ((Index < ColCount) && !Matches)
  10220. {
  10221. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  10222. if (Visible)
  10223. {
  10224. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  10225. }
  10226. Index++;
  10227. }
  10228. break;
  10229. }
  10230. if (Matches)
  10231. {
  10232. return Item;
  10233. }
  10234. Item = GetNextFile(Item, Reverse);
  10235. if (Item == CurrentItem)
  10236. {
  10237. return NULL;
  10238. }
  10239. }
  10240. }
  10241. }
  10242. //---------------------------------------------------------------------------
  10243. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10244. {
  10245. ResetIncrementalSearch();
  10246. }
  10247. //---------------------------------------------------------------------------
  10248. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10249. {
  10250. if (Message.CharCode == VK_TAB)
  10251. {
  10252. if (!FIncrementalSearch.IsEmpty())
  10253. {
  10254. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10255. bool Reverse = Shift.Contains(ssShift);
  10256. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10257. Message.Result = 1;
  10258. return;
  10259. }
  10260. }
  10261. else if (Message.CharCode == VK_ESCAPE)
  10262. {
  10263. if (!FIncrementalSearch.IsEmpty())
  10264. {
  10265. ResetIncrementalSearch();
  10266. Message.Result = 1;
  10267. return;
  10268. }
  10269. }
  10270. TForm::Dispatch(&Message);
  10271. }
  10272. //---------------------------------------------------------------------------
  10273. void __fastcall TCustomScpExplorerForm::Deactivate()
  10274. {
  10275. TForm::Deactivate();
  10276. // E.g. when switching to an internal editor window
  10277. ResetIncrementalSearch();
  10278. }
  10279. //---------------------------------------------------------------------------
  10280. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10281. {
  10282. // When switching to another application
  10283. ResetIncrementalSearch();
  10284. }
  10285. //---------------------------------------------------------------------------
  10286. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10287. {
  10288. ResetIncrementalSearch();
  10289. }
  10290. //---------------------------------------------------------------------------
  10291. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10292. {
  10293. if (FIncrementalSearching <= 0)
  10294. {
  10295. ResetIncrementalSearch();
  10296. }
  10297. }
  10298. //---------------------------------------------------------------------------
  10299. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10300. {
  10301. CountClicksForWindowPrint(this);
  10302. }
  10303. //---------------------------------------------------------------------------
  10304. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10305. {
  10306. CenterReconnectToolbar();
  10307. }
  10308. //---------------------------------------------------------------------------
  10309. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10310. {
  10311. if (RemoteDirView->ItemFocused != NULL)
  10312. {
  10313. RemoteDirView->ItemFocused->Selected = true;
  10314. }
  10315. }
  10316. //---------------------------------------------------------------------------
  10317. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  10318. {
  10319. if (!FQueueStatusUpdating)
  10320. {
  10321. bool Refresh;
  10322. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  10323. {
  10324. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  10325. }
  10326. else
  10327. {
  10328. if (FQueueFileList->GetCount() > 0)
  10329. {
  10330. FQueueFileList->Clear();
  10331. Refresh = true;
  10332. }
  10333. }
  10334. int Count = FQueueFileList->GetCount();
  10335. if (QueueFileList->Items->Count != Count)
  10336. {
  10337. DebugAssert(Refresh);
  10338. QueueFileList->Items->Count = Count;
  10339. }
  10340. if (Refresh)
  10341. {
  10342. QueueFileList->Invalidate();
  10343. }
  10344. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  10345. }
  10346. }
  10347. //---------------------------------------------------------------------------
  10348. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  10349. {
  10350. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  10351. }
  10352. //---------------------------------------------------------------------------
  10353. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  10354. {
  10355. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  10356. // See TSynchronizeChecklistDialog::UpdateTimer
  10357. if (QueueFileList->Items->Count > 0)
  10358. {
  10359. QueueFileList->Items->Item[0]->MakeVisible(false);
  10360. }
  10361. UpdateQueueFileList();
  10362. }
  10363. //---------------------------------------------------------------------------
  10364. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  10365. {
  10366. Active = QueueView3->Focused() || QueueFileList->Focused();
  10367. }
  10368. //---------------------------------------------------------------------------
  10369. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  10370. {
  10371. QueueLabelUpdateStatus();
  10372. }
  10373. //---------------------------------------------------------------------------
  10374. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  10375. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  10376. {
  10377. int State = FQueueFileList->GetState(Item->Index);
  10378. if (State == qfsProcessed)
  10379. {
  10380. Sender->Canvas->Font->Color = clGrayText;
  10381. }
  10382. }
  10383. //---------------------------------------------------------------------------
  10384. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  10385. {
  10386. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  10387. QueueFileList->Column[0]->Width =
  10388. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  10389. }
  10390. //---------------------------------------------------------------------------
  10391. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  10392. {
  10393. QueueFileListColumnAutoSize();
  10394. }
  10395. //---------------------------------------------------------------------------
  10396. void __fastcall TCustomScpExplorerForm::CloseApp()
  10397. {
  10398. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  10399. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  10400. // So we clear the flag here.
  10401. DebugAssert(NonVisualDataModule->Busy);
  10402. NonVisualDataModule->EndBusy();
  10403. try
  10404. {
  10405. Close();
  10406. }
  10407. __finally
  10408. {
  10409. NonVisualDataModule->StartBusy();
  10410. }
  10411. }
  10412. //---------------------------------------------------------------------------
  10413. bool __fastcall TCustomScpExplorerForm::IsActiveTerminal(TTerminal * Terminal)
  10414. {
  10415. return (Terminal != NULL) && Terminal->Active;
  10416. }
  10417. //---------------------------------------------------------------------------
  10418. bool __fastcall TCustomScpExplorerForm::HasActiveTerminal()
  10419. {
  10420. return IsActiveTerminal(Terminal);
  10421. }