CustomScpExplorer.cpp 384 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602
  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 <algorithm>
  8. #include <Bookmarks.h>
  9. #include <Interface.h>
  10. #include <Exceptions.h>
  11. #include <CoreMain.h>
  12. #include <FileSystems.h>
  13. #include <TextsCore.h>
  14. #include <TextsWin.h>
  15. #include <HelpWin.h>
  16. #include <VCLCommon.h>
  17. #include <Progress.h>
  18. #include <SynchronizeProgress.h>
  19. #include <DragExt.h>
  20. #include <WinApi.h>
  21. #include "GUITools.h"
  22. #include "NonVisual.h"
  23. #include "Glyphs.h"
  24. #include "Tools.h"
  25. #include "WinConfiguration.h"
  26. #include "TerminalManager.h"
  27. #include "EditorManager.h"
  28. #include "ProgParams.h"
  29. #include "Setup.h"
  30. #include <Consts.hpp>
  31. #include <DateUtils.hpp>
  32. #include <TB2Common.hpp>
  33. #include <DirectoryMonitor.hpp>
  34. #include <System.IOUtils.hpp>
  35. //---------------------------------------------------------------------------
  36. #pragma package(smart_init)
  37. #pragma link "CustomDirView"
  38. #pragma link "CustomUnixDirView"
  39. #pragma link "IEListView"
  40. #pragma link "NortonLikeListView"
  41. #pragma link "UnixDirView"
  42. #pragma link "CustomDriveView"
  43. #pragma link "UnixDriveView"
  44. #pragma link "CustomDriveView"
  45. #pragma link "TB2Dock"
  46. #pragma link "TBXStatusBars"
  47. #pragma link "TB2Item"
  48. #pragma link "TB2Toolbar"
  49. #pragma link "ThemePageControl"
  50. #pragma link "PathLabel"
  51. #pragma resource "*.dfm"
  52. //---------------------------------------------------------------------------
  53. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  54. static const int SessionPanelCount = 3;
  55. //---------------------------------------------------------------------------
  56. class TMutexGuard
  57. {
  58. public:
  59. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  60. int Timeout = 5000)
  61. {
  62. FMutex = NULL;
  63. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  64. if (WaitResult == WAIT_TIMEOUT)
  65. {
  66. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  67. }
  68. else
  69. {
  70. FMutex = AMutex;
  71. }
  72. }
  73. ~TMutexGuard()
  74. {
  75. if (FMutex != NULL)
  76. {
  77. ReleaseMutex(FMutex);
  78. }
  79. }
  80. private:
  81. HANDLE FMutex;
  82. };
  83. //---------------------------------------------------------------------------
  84. class TWindowLock
  85. {
  86. public:
  87. TWindowLock(TCustomScpExplorerForm * Form) :
  88. FForm(Form)
  89. {
  90. FForm->LockWindow();
  91. }
  92. ~TWindowLock()
  93. {
  94. FForm->UnlockWindow();
  95. }
  96. private:
  97. TCustomScpExplorerForm * FForm;
  98. };
  99. //---------------------------------------------------------------------------
  100. TAutoBatch::TAutoBatch(TCustomScpExplorerForm * Form) :
  101. FForm(Form)
  102. {
  103. FForm->BatchStart(FBatchStorage);
  104. }
  105. //---------------------------------------------------------------------------
  106. TAutoBatch::~TAutoBatch()
  107. {
  108. FForm->BatchEnd(FBatchStorage);
  109. }
  110. //---------------------------------------------------------------------------
  111. struct TTransferOperationParam
  112. {
  113. TTransferOperationParam();
  114. UnicodeString TargetDirectory;
  115. bool Temp;
  116. bool DragDrop;
  117. int Options;
  118. TAutoSwitch Queue;
  119. };
  120. //---------------------------------------------------------------------------
  121. TTransferOperationParam::TTransferOperationParam()
  122. {
  123. Temp = false;
  124. DragDrop = false;
  125. Options = 0;
  126. Queue = asAuto;
  127. }
  128. //---------------------------------------------------------------------------
  129. class TTransferPresetNoteData : public TObject
  130. {
  131. public:
  132. UnicodeString Message;
  133. };
  134. //---------------------------------------------------------------------------
  135. class TTerminalNoteData : public TObject
  136. {
  137. public:
  138. TManagedTerminal * Terminal;
  139. };
  140. //---------------------------------------------------------------------------
  141. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  142. FFormRestored(false),
  143. TForm(Owner)
  144. {
  145. AddStartupSequence(L"F");
  146. FInvalid = false;
  147. FCurrentSide = osRemote;
  148. FEverShown = false;
  149. FDocks = new TList();
  150. InitControls();
  151. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  152. // So that UpdateCustomCommandsToolbar called from RestoreParams works
  153. NonVisualDataModule->ScpExplorer = this;
  154. RestoreParams();
  155. ConfigurationChanged();
  156. RemoteDirView->Invalidate();
  157. FAutoOperation = false;
  158. FOnFileOperationFinished = NULL;
  159. FPrimaryOperation = foNone;
  160. FForceExecution = false;
  161. FIgnoreNextDialogChar = 0;
  162. FErrorList = NULL;
  163. FSynchronizeProgressForm = NULL;
  164. FProgressForm = NULL;
  165. FRefreshLocalDirectory = false;
  166. FRefreshRemoteDirectory = false;
  167. FDDExtMapFile = NULL;
  168. // CreateMutexW keeps failing with ERROR_NOACCESS
  169. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  170. DebugAssert(FDDExtMutex != NULL);
  171. FDDTargetControl = NULL;
  172. FDelayedDeletionTimer = NULL;
  173. FDelayedDeletionList = new TStringList();
  174. FDDFileList = NULL;
  175. FPendingTempSpaceWarn = false;
  176. FCapturedLog = NULL;
  177. FDragDropOperation = false;
  178. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  179. FAllowTransferPresetAutoSelect = true;
  180. FSessionChanging = false;
  181. FCopyParamDefault = L"";
  182. FSynchronizeController = NULL;
  183. FPendingQueueActionItem = NULL;
  184. FLockLevel = 0;
  185. FLockSuspendLevel = 0;
  186. FDisabledOnLockSuspend = false;
  187. FAlternativeDelete = false;
  188. FTrayIcon = new ::TTrayIcon(0);
  189. FTrayIcon->OnClick = TrayIconClick;
  190. FMaxQueueLength = 0;
  191. FLastContextPopupScreenPoint = TPoint(-1, -1);
  192. FTransferResumeList = NULL;
  193. FDeletedFiles = NULL;
  194. FMoveToQueue = false;
  195. StandaloneOperation = false;
  196. FOnFeedSynchronizeError = NULL;
  197. FOnSynchronizeAbort = NULL;
  198. FSynchronizeTerminal = NULL;
  199. FNeedSession = false;
  200. FDoNotIdleCurrentTerminal = 0;
  201. FIncrementalSearching = 0;
  202. FIncrementalSearchHaveNext = false;
  203. FQueueFileList.reset(new TQueueFileList());
  204. FProgressSide = osCurrent;
  205. FCalculateSizeOperation = NULL;
  206. FDownloadingFromClipboard = false;
  207. FClipboardFakeMonitorsPendingReset = false;
  208. FEditorManager = new TEditorManager();
  209. FEditorManager->OnFileChange = ExecutedFileChanged;
  210. FEditorManager->OnFileReload = ExecutedFileReload;
  211. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  212. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  213. FLocalEditors = new TList();
  214. FQueueStatus = NULL;
  215. FQueueStatusSection = new TCriticalSection();
  216. FQueueStatusInvalidated = false;
  217. FQueueItemInvalidated = false;
  218. FQueueStatusUpdating = false;
  219. FQueueActedItem = NULL;
  220. FQueueController = new TQueueController(QueueView3);
  221. FUserActionTimer = new TTimer(this);
  222. FUserActionTimer->Enabled = false;
  223. FUserActionTimer->Interval = 10;
  224. FUserActionTimer->OnTimer = UserActionTimer;
  225. FNotes = new TStringList();
  226. FNoteTimer = new TTimer(this);
  227. FNoteTimer->Enabled = false;
  228. FNoteTimer->OnTimer = NoteTimer;
  229. FOnNoteClick = NULL;
  230. FOle32Library = LoadLibrary(L"Ole32.dll");
  231. FDragMoveCursor = FOle32Library != NULL ?
  232. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  233. UseSystemSettings(this);
  234. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  235. static_cast<TObject*>(GetComponent(fcTransferList)));
  236. DebugAssert(TransferList != NULL);
  237. FTransferListHoverIndex = -1;
  238. TransferList->OnChange = TransferListChange;
  239. TransferList->OnDrawItem = TransferListDrawItem;
  240. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))), true);
  241. SetSubmenu(NonVisualDataModule->ColorMenuItem, true);
  242. UseDesktopFont(SessionsPageControl);
  243. UpdateSessionsPageControlHeight();
  244. UseDesktopFont(RemoteDirView);
  245. UseDesktopFont(RemoteDriveView);
  246. UseDesktopFont(QueueView3);
  247. UseDesktopFont(QueueFileList);
  248. UseDesktopFont(QueueLabel);
  249. UseDesktopFont(RemoteStatusBar);
  250. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  251. QueueSplitter->ShowHint = true;
  252. reinterpret_cast<TLabel*>(QueueFileListSplitter)->OnDblClick = QueueFileListSplitterDblClick;
  253. QueueFileListSplitter->ShowHint = true;
  254. RemotePanelSplitter->ShowHint = true;
  255. UpdateImages();
  256. FCustomCommandMenu = CreateTBXPopupMenu(this);
  257. FCustomCommandLocalFileList = NULL;
  258. FCustomCommandRemoteFileList = NULL;
  259. FSessionColors = new TPngImageList(this);
  260. FSessionColors->ColorDepth = cd32Bit;
  261. AddFixedSessionImages();
  262. SessionsPageControl->Images = FSessionColors;
  263. UpdateQueueLabel();
  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. ManagedSession = 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 SessionListChanged => ... => 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->Sessions[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->ActiveSession = Manager->NewSessions(DataList.get());
  473. }
  474. }
  475. __finally
  476. {
  477. NonVisualDataModule->EndBusy();
  478. }
  479. }
  480. return Result;
  481. }
  482. //---------------------------------------------------------------------------
  483. TManagedTerminal * TCustomScpExplorerForm::GetTerminal()
  484. {
  485. TManagedTerminal * Result;
  486. if ((FManagedSession != NULL) && !FManagedSession->LocalBrowser)
  487. {
  488. Result = FManagedSession;
  489. }
  490. else
  491. {
  492. Result = NULL;
  493. }
  494. return Result;
  495. }
  496. //---------------------------------------------------------------------------
  497. void __fastcall TCustomScpExplorerForm::SetManagedSession(TManagedTerminal * value)
  498. {
  499. if (FManagedSession != value)
  500. {
  501. SessionChanging();
  502. DoSetManagedSession(value, false);
  503. }
  504. }
  505. //---------------------------------------------------------------------------
  506. void __fastcall TCustomScpExplorerForm::DoSetManagedSession(TManagedTerminal * value, bool Replace)
  507. {
  508. DebugAssert(!Replace || ((value != NULL) && !value->LocalBrowser));
  509. FManagedSession = value;
  510. {
  511. TValueRestorer<bool> AllowTransferPresetAutoSelectRestorer(FAllowTransferPresetAutoSelect);
  512. FAllowTransferPresetAutoSelect = false;
  513. TAutoFlag SessionChangingFlag(FSessionChanging);
  514. SessionChanged(Replace);
  515. }
  516. if (Terminal != NULL)
  517. {
  518. TransferPresetAutoSelect();
  519. }
  520. // Update app and tab titles, prevented above by FSessionChanging.
  521. UpdateControls();
  522. }
  523. //---------------------------------------------------------------------------
  524. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  525. {
  526. DoSetManagedSession(value, true);
  527. }
  528. //---------------------------------------------------------------------------
  529. void __fastcall TCustomScpExplorerForm::SessionChanging()
  530. {
  531. if (ManagedSession != NULL)
  532. {
  533. UpdateSession(ManagedSession);
  534. }
  535. }
  536. //---------------------------------------------------------------------------
  537. void __fastcall TCustomScpExplorerForm::SessionChanged(bool Replaced)
  538. {
  539. if (Terminal != NULL)
  540. {
  541. UpdateSessionColor((TColor)Terminal->StateData->Color);
  542. }
  543. if (!TTerminalManager::Instance()->Updating)
  544. {
  545. SessionListChanged();
  546. }
  547. if (Terminal != NULL)
  548. {
  549. TObject * State = NULL;
  550. if (WinConfiguration->PreservePanelState)
  551. {
  552. State = Terminal->RemoteExplorerState;
  553. DirView(osRemote)->AnnounceState(State);
  554. }
  555. try
  556. {
  557. if (Replaced)
  558. {
  559. RemoteDirView->ReplaceTerminal(Terminal);
  560. }
  561. else
  562. {
  563. RemoteDirView->Terminal = Terminal;
  564. }
  565. if (Terminal->Active)
  566. {
  567. Terminal->RefreshDirectory();
  568. }
  569. if (State != NULL)
  570. {
  571. DirView(osRemote)->RestoreState(State);
  572. }
  573. }
  574. __finally
  575. {
  576. DirView(osRemote)->AnnounceState(NULL);
  577. }
  578. }
  579. else
  580. {
  581. RemoteDirView->Terminal = NULL;
  582. }
  583. InitStatusBar();
  584. UpdateTransferList();
  585. // Update panels Enable state before refreshing the labels
  586. UpdateControls();
  587. }
  588. //---------------------------------------------------------------------------
  589. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  590. {
  591. if (Queue != value)
  592. {
  593. if (FQueue != NULL)
  594. {
  595. FQueue->OnListUpdate = NULL;
  596. FQueue->OnQueueItemUpdate = NULL;
  597. }
  598. FQueue = value;
  599. if (FQueue != NULL)
  600. {
  601. DebugAssert(FQueue->OnListUpdate == NULL);
  602. FQueue->OnListUpdate = QueueListUpdate;
  603. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  604. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  605. }
  606. QueueChanged();
  607. }
  608. }
  609. //---------------------------------------------------------------------------
  610. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  611. TListItem * Item)
  612. {
  613. if (FQueueActedItem == Item)
  614. {
  615. FQueueActedItem = NULL;
  616. if ((QueueView3->PopupMenu != NULL) &&
  617. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  618. {
  619. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  620. ReleaseCapture();
  621. }
  622. }
  623. if (Item->Data == FPendingQueueActionItem)
  624. {
  625. FPendingQueueActionItem = NULL;
  626. }
  627. }
  628. //---------------------------------------------------------------------------
  629. bool TCustomScpExplorerForm::IsAnythingQueued()
  630. {
  631. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  632. }
  633. //---------------------------------------------------------------------------
  634. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  635. {
  636. {
  637. TGuard Guard(FQueueStatusSection);
  638. FQueueStatusInvalidated = false;
  639. if (FQueue != NULL)
  640. {
  641. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  642. }
  643. }
  644. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  645. {
  646. FMaxQueueLength = FQueueStatus->Count;
  647. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  648. }
  649. {
  650. TAutoFlag Flag(FQueueStatusUpdating);
  651. FQueueController->UpdateQueueStatus(FQueueStatus);
  652. }
  653. UpdateQueueFileList();
  654. SetQueueProgress();
  655. UpdateQueueView();
  656. bool IsEmpty = !IsAnythingQueued();
  657. if (IsEmpty && (Terminal != NULL))
  658. {
  659. TOnceDoneOperation OnceDoneOperation =
  660. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  661. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  662. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  663. {
  664. FQueue->Enabled = false;
  665. }
  666. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  667. {
  668. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  669. }
  670. }
  671. }
  672. //---------------------------------------------------------------------------
  673. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  674. {
  675. UnicodeString Result;
  676. if (FQueueStatus != NULL)
  677. {
  678. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  679. {
  680. TFileOperationProgressType * ProgressData =
  681. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  682. if ((ProgressData != NULL) && ProgressData->InProgress)
  683. {
  684. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  685. }
  686. }
  687. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  688. {
  689. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  690. }
  691. }
  692. return Result;
  693. }
  694. //---------------------------------------------------------------------------
  695. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  696. {
  697. ComponentVisible[fcQueueView] =
  698. !IsLocalBrowserMode() &&
  699. ((WinConfiguration->QueueView.Show == qvShow) ||
  700. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  701. (FQueueStatus != NULL) && (FQueueStatus->Count > 0)));
  702. }
  703. //---------------------------------------------------------------------------
  704. void __fastcall TCustomScpExplorerForm::QueueChanged()
  705. {
  706. if (FQueueStatus != NULL)
  707. {
  708. // UpdateFileList implementation relies on the status being recreated when session changes
  709. delete FQueueStatus;
  710. FQueueStatus = NULL;
  711. }
  712. UpdateQueueStatus(true);
  713. }
  714. //---------------------------------------------------------------------------
  715. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  716. {
  717. if (FQueue == Queue)
  718. {
  719. FQueueStatusInvalidated = true;
  720. }
  721. }
  722. //---------------------------------------------------------------------------
  723. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  724. TQueueItem * Item)
  725. {
  726. if (FQueue == Queue)
  727. {
  728. TGuard Guard(FQueueStatusSection);
  729. // this may be running in parallel with QueueChanged
  730. if (FQueueStatus != NULL)
  731. {
  732. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  733. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  734. {
  735. FRefreshLocalDirectory = (QueueItem == NULL) ||
  736. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  737. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  738. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  739. }
  740. if (QueueItem != NULL)
  741. {
  742. QueueItem->UserData = (void*)true;
  743. FQueueItemInvalidated = true;
  744. }
  745. }
  746. }
  747. }
  748. //---------------------------------------------------------------------------
  749. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  750. {
  751. // during standalone editing, we have no way to see/control queue,
  752. // so we have to always popup prompts automatically
  753. return StandaloneOperation || GUIConfiguration->QueueAutoPopup;
  754. }
  755. //---------------------------------------------------------------------------
  756. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  757. {
  758. if (FQueueStatus != NULL)
  759. {
  760. bool QueueAutoPopup = IsQueueAutoPopup();
  761. bool NeedRefresh = FQueueController->NeedRefresh();
  762. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  763. FQueueItemInvalidated = false;
  764. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  765. bool Updated = false;
  766. TQueueItemProxy * QueueItem;
  767. bool WasUserAction;
  768. for (int Index = 0; Index < Limit; Index++)
  769. {
  770. bool Update = false;
  771. QueueItem = FQueueStatus->Items[Index];
  772. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  773. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  774. (FPendingQueueActionItem == NULL))
  775. {
  776. FPendingQueueActionItem = QueueItem;
  777. FUserActionTimer->Enabled = true;
  778. }
  779. if ((bool)QueueItem->UserData)
  780. {
  781. QueueItem->UserData = (void*)false;
  782. QueueItem->Update();
  783. Updated = true;
  784. Update = true;
  785. }
  786. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  787. {
  788. Update = true;
  789. }
  790. if (Update)
  791. {
  792. FQueueController->RefreshQueueItem(QueueItem);
  793. }
  794. }
  795. if (Updated)
  796. {
  797. NonVisualDataModule->UpdateNonVisibleActions();
  798. SetQueueProgress();
  799. UpdateQueueFileList();
  800. }
  801. }
  802. }
  803. //---------------------------------------------------------------------------
  804. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  805. {
  806. if (FTaskbarList != NULL)
  807. {
  808. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  809. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  810. }
  811. }
  812. //---------------------------------------------------------------------------
  813. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  814. {
  815. if (Progress >= 0)
  816. {
  817. if (FTaskbarList != NULL)
  818. {
  819. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  820. }
  821. }
  822. else
  823. {
  824. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  825. }
  826. }
  827. //---------------------------------------------------------------------------
  828. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  829. {
  830. int OverallProgress;
  831. // FProgressForm is null when this is called from SetQueueProgress
  832. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  833. {
  834. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  835. }
  836. else if (!ProgressData->IsIndeterminate())
  837. {
  838. OverallProgress = ProgressData->OverallProgress();
  839. }
  840. else
  841. {
  842. OverallProgress = -1;
  843. }
  844. SetTaskbarListProgressValue(OverallProgress);
  845. }
  846. //---------------------------------------------------------------------------
  847. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  848. {
  849. TTerminalManager::Instance()->QueueStatusUpdated();
  850. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  851. {
  852. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  853. {
  854. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  855. {
  856. TFileOperationProgressType * ProgressData = NULL;
  857. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  858. {
  859. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  860. }
  861. if ((ProgressData != NULL) &&
  862. ProgressData->InProgress)
  863. {
  864. SetTaskbarListProgressValue(ProgressData);
  865. }
  866. else
  867. {
  868. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  869. }
  870. }
  871. else
  872. {
  873. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  874. }
  875. }
  876. else
  877. {
  878. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  879. }
  880. }
  881. UpdateQueueLabel();
  882. }
  883. //---------------------------------------------------------------------------
  884. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  885. {
  886. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  887. if (FQueueStatus != NULL)
  888. {
  889. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  890. if (ActiveAndPendingPrimaryCount > 0)
  891. {
  892. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  893. }
  894. }
  895. QueueLabel->Caption = Caption;
  896. }
  897. //---------------------------------------------------------------------------
  898. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  899. {
  900. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  901. static_cast<TComponent*>(GetComponent(fcTransferList)));
  902. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  903. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  904. TransferList->Strings->BeginUpdate();
  905. try
  906. {
  907. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  908. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  909. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  910. if (FTransferDropDownHint.IsEmpty())
  911. {
  912. FTransferDropDownHint = TransferDropDown->Hint;
  913. }
  914. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  915. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  916. TransferDropDown->Text = StripHotkey(Name);
  917. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  918. (FTransferDropDownHint, StripHotkey(Name),
  919. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  920. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General))));
  921. // update the label, otherwise when it is updated only on the first draw
  922. // of the list, it is drawn "bold" for some reason
  923. FTransferListHoverIndex = TransferList->ItemIndex;
  924. UpdateTransferLabel();
  925. }
  926. __finally
  927. {
  928. TransferList->Strings->EndUpdate();
  929. }
  930. }
  931. //---------------------------------------------------------------------------
  932. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  933. {
  934. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  935. DebugAssert(Toolbar != NULL);
  936. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  937. }
  938. //---------------------------------------------------------------------------
  939. void __fastcall TCustomScpExplorerForm::UpdateActions()
  940. {
  941. TForm::UpdateActions();
  942. if (ComponentVisible[fcCustomCommandsBand])
  943. {
  944. UpdateCustomCommandsToolbar();
  945. }
  946. }
  947. //---------------------------------------------------------------------------
  948. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  949. {
  950. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  951. }
  952. //---------------------------------------------------------------------------
  953. void __fastcall TCustomScpExplorerForm::UpdateRowSelect(TCustomDirView * DirView)
  954. {
  955. if (DirView->RowSelect != WinConfiguration->FullRowSelect)
  956. {
  957. DirView->RowSelect = WinConfiguration->FullRowSelect;
  958. // Without this, the panel turns gray and unused part of header turns black
  959. DirView->Perform(CM_COLORCHANGED, 0, 0);
  960. }
  961. }
  962. //---------------------------------------------------------------------------
  963. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  964. {
  965. Color = GetBtnFaceColor();
  966. DebugAssert(Configuration && RemoteDirView);
  967. RemoteDirView->DDAllowMove = !WinConfiguration->DDDisableMove;
  968. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  969. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  970. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  971. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  972. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  973. UpdateRowSelect(RemoteDirView);
  974. RemoteDriveView->DDAllowMove = !WinConfiguration->DDDisableMove;
  975. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  976. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  977. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  978. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  979. UpdateSessionsPageControlHeight();
  980. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  981. UpdateToolbarDisplayMode();
  982. UpdateTransferList();
  983. if (Terminal != NULL)
  984. {
  985. TransferPresetAutoSelect();
  986. }
  987. UpdateQueueView();
  988. UpdateControls();
  989. // this can be called even before constuctor finishes.
  990. if (FEditorManager != NULL)
  991. {
  992. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  993. }
  994. // this can be called even before constuctor finishes.
  995. if (FLocalEditors != NULL)
  996. {
  997. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  998. {
  999. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  1000. }
  1001. }
  1002. if (ComponentVisible[fcCustomCommandsBand])
  1003. {
  1004. UpdateCustomCommandsToolbar();
  1005. }
  1006. // show only when keeping queue items forever,
  1007. // otherwise, it's enough to have it in the context menu
  1008. QueueDeleteAllDoneQueueToolbarItem->Visible =
  1009. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  1010. if (FFileColorsCurrent != WinConfiguration->FileColors)
  1011. {
  1012. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  1013. FFileColorsCurrent = WinConfiguration->FileColors;
  1014. FileColorsChanged();
  1015. }
  1016. }
  1017. //---------------------------------------------------------------------------
  1018. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  1019. {
  1020. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  1021. DirView->Invalidate();
  1022. }
  1023. //---------------------------------------------------------------------------
  1024. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  1025. {
  1026. DoFileColorsChanged(RemoteDirView);
  1027. }
  1028. //---------------------------------------------------------------------------
  1029. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  1030. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  1031. void * /*Arg*/)
  1032. {
  1033. if (Token != NULL)
  1034. {
  1035. TForm * Editor = dynamic_cast<TForm*>(Token);
  1036. DebugAssert(Editor != NULL);
  1037. ReconfigureEditorForm(Editor);
  1038. }
  1039. }
  1040. //---------------------------------------------------------------------------
  1041. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  1042. {
  1043. WinConfiguration->DDTransferConfirmation = asOn;
  1044. }
  1045. //---------------------------------------------------------------------------
  1046. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1047. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1048. {
  1049. // noop
  1050. }
  1051. //---------------------------------------------------------------------------
  1052. bool TCustomScpExplorerForm::GetDoNotShowCopyDialogDefault(bool DragDrop)
  1053. {
  1054. return DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto);
  1055. }
  1056. //---------------------------------------------------------------------------
  1057. void TCustomScpExplorerForm::HandleDoNotShowCopyDialogAgain(bool DragDrop, bool DoNotShowAgain)
  1058. {
  1059. if (DoNotShowAgain)
  1060. {
  1061. if (DragDrop)
  1062. {
  1063. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1064. {
  1065. PopupTrayBalloon(
  1066. NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation, NULL, 0, EnableDDTransferConfirmation, NULL);
  1067. }
  1068. WinConfiguration->DDTransferConfirmation = asOff;
  1069. }
  1070. else
  1071. {
  1072. WinConfiguration->ConfirmTransferring = false;
  1073. }
  1074. }
  1075. else
  1076. {
  1077. // User explicitly unchecked "do not show again",
  1078. // so show him the dialog the next time
  1079. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1080. {
  1081. WinConfiguration->DDTransferConfirmation = asOn;
  1082. }
  1083. }
  1084. }
  1085. //---------------------------------------------------------------------------
  1086. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1087. TTransferDirection Direction, TTransferType Type, bool Temp,
  1088. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1089. bool Confirm, bool DragDrop, int Options)
  1090. {
  1091. bool Result = true;
  1092. DebugAssert(HasActiveTerminal());
  1093. // these parameters are known in advance
  1094. int Params =
  1095. FLAGMASK(Type == ttMove, cpDelete);
  1096. bool ToTemp = (Temp && (Direction == tdToLocal));
  1097. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1098. {
  1099. bool DisableNewerOnly =
  1100. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1101. ToTemp;
  1102. Options |=
  1103. FLAGMASK(ToTemp, coTemp) |
  1104. FLAGMASK(!Terminal->IsCapable[fcBackgroundTransfers], coDisableQueue) |
  1105. coDoNotShowAgain;
  1106. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1107. int CopyParamAttrs = (Direction == tdToRemote ?
  1108. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1109. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1110. int OutputOptions = FLAGMASK(GetDoNotShowCopyDialogDefault(DragDrop), cooDoNotShowAgain);
  1111. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1112. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1113. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1114. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1115. if (Result)
  1116. {
  1117. HandleDoNotShowCopyDialogAgain(DragDrop, FLAGSET(OutputOptions, cooDoNotShowAgain));
  1118. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1119. }
  1120. }
  1121. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fcBackgroundTransfers])
  1122. {
  1123. Configuration->Usage->Inc(L"TransfersOnBackground");
  1124. // these parameter are known only after transfer dialog
  1125. Params |=
  1126. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1127. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1128. Result = false;
  1129. ClearTransferSourceSelection(Direction);
  1130. }
  1131. return Result;
  1132. }
  1133. //---------------------------------------------------------------------------
  1134. void TCustomScpExplorerForm::ClearOperationSelection(TOperationSide Side)
  1135. {
  1136. if (FOnFileOperationFinished != NULL)
  1137. {
  1138. FOnFileOperationFinished(Side, UnicodeString(), true);
  1139. }
  1140. else
  1141. {
  1142. if (HasDirView[Side])
  1143. {
  1144. DirView(Side)->SelectAll(smNone);
  1145. }
  1146. }
  1147. }
  1148. //---------------------------------------------------------------------------
  1149. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1150. {
  1151. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1152. ClearOperationSelection(Side);
  1153. }
  1154. //---------------------------------------------------------------------------
  1155. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1156. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1157. const UnicodeString TargetDirectory, TGUICopyParamType & CopyParam,
  1158. int Params)
  1159. {
  1160. DebugAssert(Queue != NULL);
  1161. bool SingleFile = false;
  1162. if (FileList->Count == 1)
  1163. {
  1164. if (Direction == tdToRemote)
  1165. {
  1166. UnicodeString FileName = FileList->Strings[0];
  1167. SingleFile = FileExists(ApiPath(FileName));
  1168. }
  1169. else
  1170. {
  1171. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1172. SingleFile = !File->IsDirectory;
  1173. }
  1174. }
  1175. TQueueItem * QueueItem;
  1176. if (Direction == tdToRemote)
  1177. {
  1178. CopyParam.IncludeFileMask.SetRoots(FileList, TargetDirectory);
  1179. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1180. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1181. }
  1182. else
  1183. {
  1184. CopyParam.IncludeFileMask.SetRoots(TargetDirectory, FileList);
  1185. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1186. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1187. }
  1188. AddQueueItem(Queue, QueueItem, Terminal);
  1189. }
  1190. //---------------------------------------------------------------------------
  1191. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1192. {
  1193. if (Queue->IsEmpty)
  1194. {
  1195. Terminal->QueueOperationStart = Now();
  1196. }
  1197. Queue->AddItem(QueueItem);
  1198. }
  1199. //---------------------------------------------------------------------------
  1200. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1201. {
  1202. }
  1203. //---------------------------------------------------------------------------
  1204. void __fastcall TCustomScpExplorerForm::InitControls()
  1205. {
  1206. DebugAssert(FDocks != NULL);
  1207. for (int Index = 0; Index < ComponentCount; Index++)
  1208. {
  1209. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1210. if ((Dock != NULL) && (Dock->Tag == 0))
  1211. {
  1212. FDocks->Add(Dock);
  1213. }
  1214. }
  1215. CollectItemsWithTextDisplayMode(this);
  1216. }
  1217. //---------------------------------------------------------------------------
  1218. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1219. {
  1220. ConfigurationChanged();
  1221. QueuePanel->Height =
  1222. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1223. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1224. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1225. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1226. QueueFileList->Visible = WinConfiguration->QueueView.FileList;
  1227. QueueFileList->Height =
  1228. LoadDimension(WinConfiguration->QueueView.FileListHeight, WinConfiguration->QueueView.FileListHeightPixelsPerInch, this);
  1229. if (QueueFileList->Visible)
  1230. {
  1231. AdjustQueueLayout();
  1232. }
  1233. UpdateCustomCommandsToolbar();
  1234. }
  1235. //---------------------------------------------------------------------------
  1236. void __fastcall TCustomScpExplorerForm::StoreParams()
  1237. {
  1238. int APixelsPerInch = GetControlPixelsPerInch(this);
  1239. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1240. WinConfiguration->QueueView.HeightPixelsPerInch = APixelsPerInch;
  1241. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1242. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1243. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1244. WinConfiguration->QueueView.FileList = QueueFileList->Visible;
  1245. WinConfiguration->QueueView.FileListHeight = QueueFileList->Height;
  1246. WinConfiguration->QueueView.FileListHeightPixelsPerInch = APixelsPerInch;
  1247. }
  1248. //---------------------------------------------------------------------------
  1249. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1250. {
  1251. if (!FFormRestored)
  1252. {
  1253. FFormRestored = true;
  1254. RestoreFormParams();
  1255. }
  1256. TForm::CreateParams(Params);
  1257. }
  1258. //---------------------------------------------------------------------------
  1259. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1260. {
  1261. DebugAssert(FDocks != NULL);
  1262. for (int Index = 0; Index < FDocks->Count; Index++)
  1263. {
  1264. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1265. }
  1266. }
  1267. //---------------------------------------------------------------------------
  1268. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1269. {
  1270. SetDockAllowDrag(true);
  1271. while (!ButtonsStr.IsEmpty())
  1272. {
  1273. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1274. UnicodeString Name = CutToChar(S, L'=', true);
  1275. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1276. {
  1277. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1278. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1279. {
  1280. while (!S.IsEmpty())
  1281. {
  1282. UnicodeString S2 = CutToChar(S, L',', true);
  1283. UnicodeString Name = CutToChar(S2, L':', true);
  1284. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1285. {
  1286. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1287. if (SameText(GetToolbarItemName(Item), Name))
  1288. {
  1289. // forward compatibility
  1290. UnicodeString S3 = CutToChar(S2, L':', true);
  1291. bool Visible = StrToIntDef(S3, 0);
  1292. Item->Visible = Visible;
  1293. }
  1294. }
  1295. }
  1296. }
  1297. }
  1298. }
  1299. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1300. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1301. }
  1302. //---------------------------------------------------------------------------
  1303. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1304. {
  1305. UnicodeString Result;
  1306. SetDockAllowDrag(true);
  1307. Result = ::GetToolbarsLayoutStr(this);
  1308. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1309. return Result;
  1310. }
  1311. //---------------------------------------------------------------------------
  1312. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1313. {
  1314. UnicodeString Result;
  1315. if (Item->Action != NULL)
  1316. {
  1317. Result = Item->Action->Name;
  1318. Result = RemoveSuffix(Result, L"Action", true);
  1319. }
  1320. else
  1321. {
  1322. Result = Item->Name;
  1323. Result = RemoveSuffix(Result, L"SubmenuItem", true);
  1324. Result = RemoveSuffix(Result, L"Item", true);
  1325. }
  1326. return Result;
  1327. }
  1328. //---------------------------------------------------------------------------
  1329. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1330. {
  1331. UnicodeString Result;
  1332. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1333. {
  1334. UnicodeString ToolbarStr;
  1335. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1336. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1337. {
  1338. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1339. {
  1340. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1341. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1342. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1343. // and save the changes here only.
  1344. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1345. {
  1346. UnicodeString Name = GetToolbarItemName(Item);
  1347. DebugAssert(Name.Pos(L"TBX") == 0);
  1348. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1349. }
  1350. }
  1351. }
  1352. if (!ToolbarStr.IsEmpty())
  1353. {
  1354. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1355. }
  1356. }
  1357. return Result;
  1358. }
  1359. //---------------------------------------------------------------------------
  1360. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1361. {
  1362. DebugAssert(FProgressForm == NULL);
  1363. bool AllowSkip = HasActiveTerminal() ? Terminal->IsCapable[fcSkipTransfer] : false;
  1364. bool AllowMoveToQueue = (FTransferResumeList != NULL) || (FDeletedFiles != NULL);
  1365. FProgressForm = new TProgressForm(Application, AllowMoveToQueue, AllowSkip, SynchronizeProgress);
  1366. FProgressForm->DeleteLocalToRecycleBin =
  1367. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1368. FProgressForm->DeleteRemoteToRecycleBin =
  1369. HasActiveTerminal() &&
  1370. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1371. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1372. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1373. LockWindow();
  1374. // Actually, do not know what hides the progress once the operation finishes
  1375. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1376. SetTaskbarListProgressState(TBPF_NORMAL);
  1377. }
  1378. //---------------------------------------------------------------------------
  1379. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1380. {
  1381. UnlockWindow();
  1382. SAFE_DESTROY(FProgressForm);
  1383. SetQueueProgress();
  1384. }
  1385. //---------------------------------------------------------------------------
  1386. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1387. TFileOperationProgressType & ProgressData)
  1388. {
  1389. // Download to temporary local directory
  1390. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1391. {
  1392. bool Continue = true;
  1393. FPendingTempSpaceWarn = false;
  1394. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1395. if (!Continue)
  1396. {
  1397. Abort();
  1398. }
  1399. }
  1400. // operation is being executed and we still didn't show up progress form
  1401. if (ProgressData.InProgress && !FProgressForm)
  1402. {
  1403. CreateProgressForm(NULL);
  1404. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1405. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1406. }
  1407. // operation is finished (or terminated), so we hide progress form
  1408. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1409. {
  1410. DestroyProgressForm();
  1411. if (ProgressData.Operation == foCalculateSize)
  1412. {
  1413. // When calculating size before transfer, the abort caused by
  1414. // cancel flag set due to "MoveToQueue" below,
  1415. // is not propagated back to us in ExecuteFileOperation,
  1416. // as it is silently swallowed.
  1417. // So we explicitly (re)throw it here.
  1418. if (FMoveToQueue)
  1419. {
  1420. Abort();
  1421. }
  1422. }
  1423. else
  1424. {
  1425. if ((ProgressData.Cancel == csContinue) &&
  1426. !FAutoOperation)
  1427. {
  1428. OperationComplete(ProgressData.StartTime);
  1429. }
  1430. }
  1431. }
  1432. if (FProgressForm &&
  1433. // Can happen only during synchronization, when ending one of the operations
  1434. ProgressData.InProgress)
  1435. {
  1436. FProgressForm->SetProgressData(ProgressData);
  1437. DebugAssert(ProgressData.InProgress);
  1438. SetTaskbarListProgressValue(&ProgressData);
  1439. if (FProgressForm->Cancel > csContinue)
  1440. {
  1441. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1442. if (FProgressForm->Cancel == csCancelFile)
  1443. {
  1444. FProgressForm->ClearCancel();
  1445. }
  1446. // cancel cancels even the move
  1447. FMoveToQueue = false;
  1448. }
  1449. else if (FProgressForm->MoveToQueue)
  1450. {
  1451. FMoveToQueue = true;
  1452. ProgressData.SetCancelAtLeast(csCancel);
  1453. }
  1454. if ((FTransferResumeList != NULL) &&
  1455. ProgressData.InProgress &&
  1456. ProgressData.IsTransfer() &&
  1457. !ProgressData.FullFileName.IsEmpty())
  1458. {
  1459. if ((FTransferResumeList->Count == 0) ||
  1460. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1461. {
  1462. // note that we do not recognize directories from files here
  1463. FTransferResumeList->Add(ProgressData.FullFileName);
  1464. }
  1465. }
  1466. }
  1467. }
  1468. //---------------------------------------------------------------------------
  1469. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1470. const TDateTime & StartTime)
  1471. {
  1472. if (GUIConfiguration->BeepOnFinish &&
  1473. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1474. {
  1475. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1476. DWORD Sound;
  1477. if (!ExtractFileExt(BeepSound).IsEmpty())
  1478. {
  1479. Sound = SND_FILENAME;
  1480. }
  1481. else
  1482. {
  1483. Sound = SND_ALIAS;
  1484. }
  1485. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1486. }
  1487. }
  1488. //---------------------------------------------------------------------------
  1489. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1490. TFileOperationProgressType & ProgressData)
  1491. {
  1492. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1493. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1494. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1495. FileOperationProgress(ProgressData);
  1496. }
  1497. //---------------------------------------------------------------------------
  1498. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1499. {
  1500. UnicodeString Result;
  1501. if (FProgressForm != NULL)
  1502. {
  1503. Result = FProgressForm->ProgressStr();
  1504. }
  1505. return Result;
  1506. }
  1507. //---------------------------------------------------------------------------
  1508. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1509. bool DragDrop)
  1510. {
  1511. return (!DragDrop && (DropSourceControl == NULL)) ||
  1512. (DropSourceControl == DirView(osOther));
  1513. }
  1514. //---------------------------------------------------------------------------
  1515. TListItem * TCustomScpExplorerForm::VisualiseOperationFinished(TOperationSide Side, const UnicodeString & FileName, bool Unselect)
  1516. {
  1517. TCustomDirView * ADirView = DirView(Side);
  1518. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1519. TListItem * Item = ADirView->FindFileItem(FileNameOnly);
  1520. // this can happen when local drive is unplugged in the middle of the operation
  1521. if (Item != NULL)
  1522. {
  1523. if (Unselect)
  1524. {
  1525. Item->Selected = false;
  1526. }
  1527. ADirView->MakeProgressVisible(Item);
  1528. }
  1529. return Item;
  1530. }
  1531. //---------------------------------------------------------------------------
  1532. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1533. TFileOperation Operation, TOperationSide Side,
  1534. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1535. TOnceDoneOperation & OnceDoneOperation)
  1536. {
  1537. if (!FAutoOperation)
  1538. {
  1539. // no selection on "/upload", form serves only as event handler
  1540. // (it is not displayed)
  1541. if (PanelOperation(Side, FDragDropOperation) && Visible &&
  1542. ((Operation == FPrimaryOperation) ||
  1543. ((Operation != foCalculateSize) && (Operation != foGetProperties) && (Operation != foCalculateChecksum))))
  1544. {
  1545. TOperationSide DViewSide;
  1546. if ((FProgressSide != osCurrent) &&
  1547. IsLocalBrowserMode() && (Side == osLocal)) // Only to limit the impact
  1548. {
  1549. // assume the operation is over the focused panel
  1550. DViewSide = FProgressSide;
  1551. }
  1552. else
  1553. {
  1554. DViewSide = Side;
  1555. }
  1556. if (FOnFileOperationFinished != NULL)
  1557. {
  1558. FOnFileOperationFinished(DViewSide, FileName, Success);
  1559. }
  1560. else
  1561. {
  1562. VisualiseOperationFinished(DViewSide, FileName, Success);
  1563. }
  1564. }
  1565. if (TFileOperationProgressType::IsTransferOperation(Operation))
  1566. {
  1567. if (DebugAlwaysTrue(!IsLocalBrowserMode()))
  1568. {
  1569. if (Side == osLocal)
  1570. {
  1571. Configuration->Usage->Inc(L"UploadedFiles");
  1572. }
  1573. else
  1574. {
  1575. Configuration->Usage->Inc(L"DownloadedFiles");
  1576. }
  1577. }
  1578. }
  1579. }
  1580. if (Success && (FSynchronizeController != NULL))
  1581. {
  1582. DebugAssert(!IsSideLocalBrowser(osRemote));
  1583. if (Operation == foCopy)
  1584. {
  1585. DebugAssert(Side == osLocal);
  1586. FSynchronizeController->LogOperation(soUpload, FileName);
  1587. }
  1588. else if (Operation == foDelete)
  1589. {
  1590. DebugAssert(Side == osRemote);
  1591. FSynchronizeController->LogOperation(soDelete, FileName);
  1592. }
  1593. }
  1594. if (FProgressForm)
  1595. {
  1596. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1597. }
  1598. }
  1599. //---------------------------------------------------------------------------
  1600. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1601. TFileOperation Operation, TOperationSide Side,
  1602. bool Temp, const UnicodeString & FileName, Boolean Success,
  1603. TOnceDoneOperation & OnceDoneOperation)
  1604. {
  1605. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1606. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1607. OnceDoneOperation);
  1608. }
  1609. //---------------------------------------------------------------------------
  1610. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1611. {
  1612. return false;
  1613. }
  1614. //---------------------------------------------------------------------------
  1615. bool TCustomScpExplorerForm::SupportsLocalBrowser()
  1616. {
  1617. return false;
  1618. }
  1619. //---------------------------------------------------------------------------
  1620. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1621. {
  1622. return false;
  1623. }
  1624. //---------------------------------------------------------------------------
  1625. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1626. {
  1627. return NULL;
  1628. }
  1629. //---------------------------------------------------------------------------
  1630. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1631. {
  1632. DebugAssert(GetSide(Side) == osRemote);
  1633. DebugUsedParam(Side);
  1634. return RemoteDirView;
  1635. }
  1636. //---------------------------------------------------------------------------
  1637. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1638. {
  1639. DebugAssert(GetSide(Side) == osRemote);
  1640. DebugUsedParam(Side);
  1641. return RemoteDriveView;
  1642. }
  1643. //---------------------------------------------------------------------------
  1644. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1645. {
  1646. DebugAssert(GetSide(Side) == osRemote);
  1647. DebugUsedParam(Side);
  1648. return HasActiveTerminal();
  1649. }
  1650. //---------------------------------------------------------------------------
  1651. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1652. TOperationSide Side, int FilesOnly)
  1653. {
  1654. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1655. }
  1656. //---------------------------------------------------------------------------
  1657. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1658. TOperationSide Side, int FilesOnly)
  1659. {
  1660. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1661. }
  1662. //---------------------------------------------------------------------------
  1663. struct THistoryItemData
  1664. {
  1665. short int Side;
  1666. short int Index;
  1667. };
  1668. //---------------------------------------------------------------------------
  1669. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1670. {
  1671. DirView(Side)->HistoryGo(Index);
  1672. }
  1673. //---------------------------------------------------------------------------
  1674. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1675. {
  1676. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1677. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1678. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1679. }
  1680. //---------------------------------------------------------------------------
  1681. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1682. bool Back)
  1683. {
  1684. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1685. {
  1686. TCustomDirView * DView = DirView(Side);
  1687. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1688. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1689. if (ICount > 10)
  1690. {
  1691. ICount = 10;
  1692. }
  1693. Menu->Items->Clear();
  1694. THistoryItemData Data;
  1695. Data.Side = Side;
  1696. for (short int i = 1; i <= ICount; i++)
  1697. {
  1698. TTBCustomItem * Item = new TTBXItem(Menu);
  1699. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1700. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1701. Item->Hint = DView->HistoryPath[Data.Index];
  1702. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1703. Item->Tag = *reinterpret_cast<int*>(&Data);
  1704. Item->OnClick = HistoryItemClick;
  1705. Menu->Items->Add(Item);
  1706. }
  1707. }
  1708. }
  1709. //---------------------------------------------------------------------------
  1710. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1711. TOperationSide Side, bool Back)
  1712. {
  1713. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1714. {
  1715. // workaround
  1716. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1717. // operator new allocates memory in Pascal code, but calls inline
  1718. // contructor in C++, leading in problems, the function does
  1719. // both in Pascal code
  1720. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1721. UpdateHistoryMenu(Side, Back);
  1722. }
  1723. return FHistoryMenu[Side == osLocal][Back];
  1724. }
  1725. //---------------------------------------------------------------------------
  1726. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1727. TCustomDirView *Sender)
  1728. {
  1729. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1730. UpdateHistoryMenu(Side, true);
  1731. UpdateHistoryMenu(Side, false);
  1732. }
  1733. //---------------------------------------------------------------------------
  1734. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1735. {
  1736. // remote custom commands can be executed only if the server supports shell commands
  1737. // or have secondary shell
  1738. return HasActiveTerminal() && (Terminal->IsCapable[fcSecondaryShell] || Terminal->IsCapable[fcShellAnyCommand]);
  1739. }
  1740. //---------------------------------------------------------------------------
  1741. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1742. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1743. {
  1744. // -1 = hidden, 0 = disabled, 1 = enabled
  1745. int Result;
  1746. TFileCustomCommand RemoteCustomCommand;
  1747. TLocalCustomCommand LocalCustomCommand;
  1748. TFileCustomCommand * NonInteractiveCustomCommand =
  1749. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1750. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1751. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1752. if (FLAGCLEAR(Command.Params, ccLocal))
  1753. {
  1754. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1755. // custom command that does not operate with files can be executed anytime ...
  1756. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1757. {
  1758. // ... but do not show such command in remote file menu
  1759. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1760. {
  1761. Result = AllowedState;
  1762. }
  1763. else
  1764. {
  1765. Result = -1;
  1766. }
  1767. }
  1768. else
  1769. {
  1770. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1771. {
  1772. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1773. }
  1774. else
  1775. {
  1776. Result = -1;
  1777. }
  1778. }
  1779. }
  1780. else
  1781. {
  1782. // custom command that does not operate with files can be executed anytime
  1783. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1784. {
  1785. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1786. }
  1787. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1788. {
  1789. if ((ListType == ccltAll) || (ListType == ccltFile))
  1790. {
  1791. // special case is "diff"-style command that can be executed over any side,
  1792. // if we have both sides
  1793. Result =
  1794. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1795. // directly (instead of EnableSelectedOperation) to pass
  1796. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1797. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1798. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1799. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1800. }
  1801. else if (ListType == ccltBoth)
  1802. {
  1803. DebugAssert(!IsLocalBrowserMode());
  1804. Result = 1;
  1805. }
  1806. else
  1807. {
  1808. Result = -1;
  1809. }
  1810. }
  1811. else
  1812. {
  1813. if ((ListType == ccltAll) ||
  1814. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1815. {
  1816. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1817. }
  1818. else
  1819. {
  1820. Result = -1;
  1821. }
  1822. }
  1823. if ((Result > 0) &&
  1824. LocalCustomCommand.IsSessionCommand(Cmd) &&
  1825. !HasActiveTerminal())
  1826. {
  1827. Result = 0;
  1828. }
  1829. }
  1830. return Result;
  1831. }
  1832. //---------------------------------------------------------------------------
  1833. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1834. TStrings * FileList, const TCustomCommandType & ACommand,
  1835. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1836. {
  1837. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1838. {
  1839. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1840. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1841. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1842. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1843. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1844. bool Capture =
  1845. FLAGSET(ACommand.Params, ccShowResults) ||
  1846. FLAGSET(ACommand.Params, ccCopyResults) ||
  1847. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1848. TCaptureOutputEvent OutputEvent = NULL;
  1849. DebugAssert(FCapturedLog == NULL);
  1850. if (Capture)
  1851. {
  1852. FCapturedLog = new TStringList();
  1853. OutputEvent = TerminalCaptureLog;
  1854. }
  1855. try
  1856. {
  1857. if (!RemoteCustomCommand.IsFileCommand(Command))
  1858. {
  1859. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1860. OutputEvent);
  1861. }
  1862. else
  1863. {
  1864. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1865. }
  1866. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1867. {
  1868. if (FLAGSET(ACommand.Params, ccCopyResults))
  1869. {
  1870. CopyToClipboard(FCapturedLog);
  1871. }
  1872. if (FLAGSET(ACommand.Params, ccShowResults))
  1873. {
  1874. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1875. }
  1876. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1877. {
  1878. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1879. }
  1880. }
  1881. }
  1882. __finally
  1883. {
  1884. SAFE_DESTROY(FCapturedLog);
  1885. }
  1886. }
  1887. }
  1888. //---------------------------------------------------------------------------
  1889. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1890. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1891. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1892. {
  1893. DebugAssert(!IsLocalBrowserMode());
  1894. TStrings * LocalFileList = NULL;
  1895. TStrings * RemoteFileList = NULL;
  1896. TStrings * RemoteFileListFull = NULL;
  1897. try
  1898. {
  1899. if (LocalFileCommand)
  1900. {
  1901. if (ALocalFileList == NULL)
  1902. {
  1903. DebugAssert(HasDirView[osLocal]);
  1904. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1905. // directly (instead of EnableSelectedOperation) to pass
  1906. // false to FocusedFileOnlyWhenFocused
  1907. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1908. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1909. }
  1910. else
  1911. {
  1912. LocalFileList = ALocalFileList;
  1913. }
  1914. if (FileListCommand)
  1915. {
  1916. if (LocalFileList->Count != 1)
  1917. {
  1918. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1919. }
  1920. }
  1921. else
  1922. {
  1923. if ((LocalFileList->Count != 1) &&
  1924. (FileList->Count != 1) &&
  1925. (LocalFileList->Count != FileList->Count))
  1926. {
  1927. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1928. }
  1929. }
  1930. }
  1931. UnicodeString RootTempDir;
  1932. UnicodeString TempDir;
  1933. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1934. if (!RemoteFiles)
  1935. {
  1936. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, true);
  1937. }
  1938. try
  1939. {
  1940. TDateTimes RemoteFileTimes;
  1941. if (RemoteFiles)
  1942. {
  1943. RemoteFileList = FileList;
  1944. }
  1945. else
  1946. {
  1947. RemoteFileList = new TStringList();
  1948. TMakeLocalFileListParams MakeFileListParam;
  1949. MakeFileListParam.FileList = RemoteFileList;
  1950. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1951. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1952. MakeFileListParam.Recursive =
  1953. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1954. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1955. if (!MakeFileListParam.Recursive)
  1956. {
  1957. RemoteFileTimes.clear();
  1958. RemoteFileListFull = new TStringList();
  1959. MakeFileListParam.FileList = RemoteFileListFull;
  1960. MakeFileListParam.Recursive = true;
  1961. MakeFileListParam.IncludeDirs = false;
  1962. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1963. }
  1964. else
  1965. {
  1966. RemoteFileListFull = RemoteFileList;
  1967. }
  1968. }
  1969. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1970. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1971. if (!NonBlocking)
  1972. {
  1973. int Count;
  1974. if (FileListCommand)
  1975. {
  1976. Count = 1;
  1977. }
  1978. else if (LocalFileCommand)
  1979. {
  1980. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  1981. }
  1982. else
  1983. {
  1984. Count = RemoteFileList->Count;
  1985. }
  1986. Progress.Start(foCustomCommand, osRemote, Count);
  1987. DebugAssert(FProgressForm != NULL);
  1988. FProgressForm->ReadOnly = true;
  1989. }
  1990. try
  1991. {
  1992. if (FileListCommand)
  1993. {
  1994. UnicodeString LocalFile;
  1995. // MakeFileList does not delimit filenames
  1996. UnicodeString FileList = MakeFileList(RemoteFileList);
  1997. if (LocalFileCommand)
  1998. {
  1999. DebugAssert(LocalFileList->Count == 1);
  2000. LocalFile = LocalFileList->Strings[0];
  2001. }
  2002. TLocalCustomCommand CustomCommand(Data,
  2003. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  2004. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  2005. if (NonBlocking)
  2006. {
  2007. DebugAssert(!POutput);
  2008. ExecuteShellChecked(ShellCommand);
  2009. }
  2010. else
  2011. {
  2012. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  2013. }
  2014. }
  2015. else if (LocalFileCommand)
  2016. {
  2017. if (LocalFileList->Count == 1)
  2018. {
  2019. UnicodeString LocalFile = LocalFileList->Strings[0];
  2020. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2021. {
  2022. UnicodeString FileName = RemoteFileList->Strings[Index];
  2023. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2024. Progress.SetFile(FileNameForProgress);
  2025. TLocalCustomCommand CustomCommand(Data,
  2026. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  2027. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2028. TOnceDoneOperation OnceDoneOperation;
  2029. // Do not unselect anything, as it won't work with two panels anyway
  2030. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2031. }
  2032. }
  2033. else if (RemoteFileList->Count == 1)
  2034. {
  2035. UnicodeString FileName = RemoteFileList->Strings[0];
  2036. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2037. {
  2038. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2039. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2040. Progress.SetFile(LocalFileNameOnly);
  2041. TLocalCustomCommand CustomCommand(
  2042. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2043. FileName, LocalFileName, L"");
  2044. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2045. TOnceDoneOperation OnceDoneOperation;
  2046. // Do not unselect anything, as it won't work with two panels anyway
  2047. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2048. }
  2049. }
  2050. else
  2051. {
  2052. if (LocalFileList->Count != RemoteFileList->Count)
  2053. {
  2054. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  2055. }
  2056. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2057. {
  2058. UnicodeString FileName = RemoteFileList->Strings[Index];
  2059. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2060. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2061. Progress.SetFile(LocalFileNameOnly); // sic
  2062. TLocalCustomCommand CustomCommand(
  2063. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2064. FileName, LocalFileName, L"");
  2065. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2066. TOnceDoneOperation OnceDoneOperation;
  2067. // Do not unselect anything, as it won't work with two panels anyway
  2068. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2069. }
  2070. }
  2071. }
  2072. else
  2073. {
  2074. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2075. {
  2076. UnicodeString FileName = RemoteFileList->Strings[Index];
  2077. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2078. Progress.SetFile(FileNameForProgress);
  2079. TLocalCustomCommand CustomCommand(Data,
  2080. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2081. FileName, L"", L"");
  2082. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2083. TOnceDoneOperation OnceDoneOperation;
  2084. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  2085. }
  2086. }
  2087. }
  2088. __finally
  2089. {
  2090. if (!NonBlocking)
  2091. {
  2092. Progress.Stop();
  2093. }
  2094. }
  2095. DebugAssert(!FAutoOperation);
  2096. if (!RemoteFiles)
  2097. {
  2098. TempDir = IncludeTrailingBackslash(TempDir);
  2099. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  2100. {
  2101. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  2102. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  2103. // Skip directories, process only nested files.
  2104. // The check is redundant as FileAge fails for directories anyway.
  2105. !DirectoryExists(FileName))
  2106. {
  2107. UnicodeString RemoteDir =
  2108. UnixExtractFileDir(
  2109. UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
  2110. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2111. TDateTime NewTime;
  2112. if (FileAge(FileName, NewTime) &&
  2113. (NewTime != RemoteFileTimes[Index]))
  2114. {
  2115. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2116. TemporaryFileCopyParam(CopyParam);
  2117. CopyParam.FileMask = L"";
  2118. FAutoOperation = true;
  2119. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2120. TemporaryFilesList->Add(FileName);
  2121. Terminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2122. }
  2123. }
  2124. }
  2125. }
  2126. }
  2127. __finally
  2128. {
  2129. FAutoOperation = false;
  2130. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2131. {
  2132. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2133. }
  2134. }
  2135. }
  2136. __finally
  2137. {
  2138. if (RemoteFileList != FileList)
  2139. {
  2140. delete RemoteFileList;
  2141. }
  2142. if (LocalFileList != ALocalFileList)
  2143. {
  2144. delete LocalFileList;
  2145. }
  2146. if (RemoteFileListFull != RemoteFileList)
  2147. {
  2148. delete RemoteFileListFull;
  2149. }
  2150. }
  2151. }
  2152. //---------------------------------------------------------------------------
  2153. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2154. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2155. bool FileListCommand, UnicodeString * POutput)
  2156. {
  2157. std::unique_ptr<TStrings> SelectedFileList(DirView(osCurrent)->CreateFileList(false, true, NULL));
  2158. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2159. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2160. {
  2161. UnicodeString FileName = SelectedFileList->Strings[Index];
  2162. if (DirectoryExists(FileName))
  2163. {
  2164. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2165. {
  2166. LocalFileList->Add(FileName);
  2167. }
  2168. if (FLAGSET(ACommand.Params, ccRecursive))
  2169. {
  2170. TMakeLocalFileListParams MakeFileListParam;
  2171. MakeFileListParam.FileList = LocalFileList.get();
  2172. MakeFileListParam.FileTimes = NULL;
  2173. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2174. MakeFileListParam.Recursive = true;
  2175. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2176. }
  2177. }
  2178. else
  2179. {
  2180. LocalFileList->Add(FileName);
  2181. }
  2182. }
  2183. UnicodeString RemotePath;
  2184. if (Terminal != NULL)
  2185. {
  2186. RemotePath = Terminal->CurrentDirectory;
  2187. }
  2188. if (FileListCommand)
  2189. {
  2190. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2191. TLocalCustomCommand CustomCommand(
  2192. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2193. L"", L"", FileList);
  2194. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2195. }
  2196. else
  2197. {
  2198. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2199. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2200. DebugAssert(FProgressForm != NULL);
  2201. FProgressForm->ReadOnly = true;
  2202. try
  2203. {
  2204. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2205. {
  2206. UnicodeString FileName = LocalFileList->Strings[Index];
  2207. Progress.SetFile(FileName);
  2208. TLocalCustomCommand CustomCommand(
  2209. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2210. FileName, L"", L"");
  2211. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2212. TOnceDoneOperation OnceDoneOperation;
  2213. Progress.Finish(FileName, true, OnceDoneOperation);
  2214. }
  2215. }
  2216. __finally
  2217. {
  2218. Progress.Stop();
  2219. }
  2220. }
  2221. }
  2222. //---------------------------------------------------------------------------
  2223. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2224. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2225. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2226. {
  2227. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2228. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2229. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2230. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2231. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2232. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2233. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2234. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2235. std::unique_ptr<UnicodeString> POutput;
  2236. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2237. FLAGSET(ACommand.Params, ccCopyResults))
  2238. {
  2239. POutput.reset(new UnicodeString());
  2240. }
  2241. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  2242. FProgressSide = FCurrentSide;
  2243. if (!LocalCustomCommand.IsFileCommand(Command))
  2244. {
  2245. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2246. }
  2247. // remote files?
  2248. else if (!IsSideLocalBrowser(FCurrentSide) || LocalFileCommand)
  2249. {
  2250. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2251. }
  2252. // local files
  2253. else
  2254. {
  2255. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2256. }
  2257. if (POutput.get() != NULL)
  2258. {
  2259. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2260. int P = POutput->Pos(sLineBreak);
  2261. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2262. {
  2263. POutput->SetLength(P - 1);
  2264. }
  2265. // Copy even empty output to clipboard
  2266. if (FLAGSET(ACommand.Params, ccCopyResults))
  2267. {
  2268. CopyToClipboard(*POutput);
  2269. }
  2270. // But do not show an empty message box.
  2271. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2272. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2273. !POutput->IsEmpty())
  2274. {
  2275. TClipboardHandler ClipboardHandler;
  2276. ClipboardHandler.Text = *POutput;
  2277. TMessageParams Params;
  2278. TQueryButtonAlias Aliases[1];
  2279. Aliases[0].Button = qaRetry;
  2280. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2281. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2282. Params.Aliases = Aliases;
  2283. Params.AliasesCount = LENOF(Aliases);
  2284. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2285. }
  2286. }
  2287. }
  2288. //---------------------------------------------------------------------------
  2289. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2290. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2291. {
  2292. TCustomCommandData Data;
  2293. UnicodeString Site;
  2294. UnicodeString RemotePath;
  2295. if (HasActiveTerminal())
  2296. {
  2297. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  2298. Data = TCustomCommandData(SessionData.get());
  2299. Site = Terminal->SessionData->SessionKey;
  2300. RemotePath = Terminal->CurrentDirectory;
  2301. }
  2302. UnicodeString HelpKeyword = ACommand.HomePage;
  2303. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2304. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2305. {
  2306. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2307. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2308. {
  2309. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2310. }
  2311. else
  2312. {
  2313. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2314. }
  2315. if (!DoCustomCommandOptionsDialog(
  2316. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2317. {
  2318. Abort();
  2319. }
  2320. }
  2321. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2322. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2323. {
  2324. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2325. }
  2326. else
  2327. {
  2328. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2329. }
  2330. }
  2331. //---------------------------------------------------------------------------
  2332. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2333. const TCustomCommandType & Command)
  2334. {
  2335. DebugAssert(FCustomCommandLocalFileList != NULL);
  2336. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2337. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2338. TStrings * LocalFileList = new TStringList();
  2339. TStrings * RemoteFileList = new TStringList();
  2340. try
  2341. {
  2342. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2343. {
  2344. LocalFileList->Clear();
  2345. LocalFileList->AddObject(
  2346. FCustomCommandLocalFileList->Strings[Index],
  2347. FCustomCommandLocalFileList->Objects[Index]);
  2348. RemoteFileList->Clear();
  2349. RemoteFileList->AddObject(
  2350. FCustomCommandRemoteFileList->Strings[Index],
  2351. FCustomCommandRemoteFileList->Objects[Index]);
  2352. CustomCommand(RemoteFileList, Command, LocalFileList);
  2353. }
  2354. }
  2355. __finally
  2356. {
  2357. delete LocalFileList;
  2358. delete RemoteFileList;
  2359. }
  2360. }
  2361. //---------------------------------------------------------------------------
  2362. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2363. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2364. {
  2365. delete FCustomCommandLocalFileList;
  2366. delete FCustomCommandRemoteFileList;
  2367. // takeover ownership,
  2368. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2369. FCustomCommandLocalFileList = LocalFileList;
  2370. FCustomCommandRemoteFileList = RemoteFileList;
  2371. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2372. if (Button != NULL)
  2373. {
  2374. FCustomCommandMenu->Items->Clear();
  2375. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2376. MenuPopup(FCustomCommandMenu, Button);
  2377. }
  2378. else
  2379. {
  2380. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2381. }
  2382. }
  2383. //---------------------------------------------------------------------------
  2384. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2385. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2386. {
  2387. DebugAssert(FCapturedLog != NULL);
  2388. if ((OutputType == cotOutput) || (OutputType == cotError))
  2389. {
  2390. FCapturedLog->Add(AddedLine);
  2391. }
  2392. }
  2393. //---------------------------------------------------------------------------
  2394. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2395. TOperationSide Side)
  2396. {
  2397. return
  2398. (Side == osOther) &&
  2399. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2400. }
  2401. //---------------------------------------------------------------------------
  2402. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2403. TOperationSide Side, TTBXCustomItem * Item,
  2404. TResolvedDoubleClickAction DoubleClickAction1, TResolvedDoubleClickAction DoubleClickAction2)
  2405. {
  2406. TCustomDirView * DView = DirView(Side);
  2407. bool IsDefault = false;
  2408. if (DView->ItemFocused != NULL)
  2409. {
  2410. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  2411. TResolvedDoubleClickAction DefaultAction =
  2412. WinConfiguration->ResolveDoubleClickAction(DView->ItemIsDirectory(DView->ItemFocused), ATerminal);
  2413. IsDefault = (DefaultAction == DoubleClickAction1) || (DefaultAction == DoubleClickAction2);
  2414. }
  2415. TTBItemOptions O = Item->Options;
  2416. if (IsDefault)
  2417. {
  2418. Item->Options = O << tboDefault;
  2419. }
  2420. else
  2421. {
  2422. Item->Options = O >> tboDefault;
  2423. }
  2424. }
  2425. //---------------------------------------------------------------------------
  2426. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2427. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2428. {
  2429. TCustomDirView * DView = DirView(Side);
  2430. TListItem * Item = DView->ItemFocused;
  2431. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2432. EnableFocusedOperation[Side])
  2433. {
  2434. TPoint ClientPoint;
  2435. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2436. TPoint(0, 0) : MousePos;
  2437. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2438. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2439. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2440. }
  2441. }
  2442. //---------------------------------------------------------------------------
  2443. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2444. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2445. {
  2446. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, rdcaChangeDir, rdcaOpen);
  2447. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, rdcaEdit, rdcaNone);
  2448. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, rdcaCopy, rdcaNone);
  2449. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2450. Handled = true;
  2451. }
  2452. //---------------------------------------------------------------------------
  2453. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2454. {
  2455. }
  2456. //---------------------------------------------------------------------------
  2457. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2458. {
  2459. DebugAssert(FErrorList == NULL);
  2460. if (WinConfiguration->ContinueOnError)
  2461. {
  2462. FErrorList = new TStringList();
  2463. Configuration->Usage->Inc(L"ContinuationsOnError");
  2464. }
  2465. NonVisualDataModule->StartBusy();
  2466. }
  2467. //---------------------------------------------------------------------------
  2468. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2469. {
  2470. NonVisualDataModule->EndBusy();
  2471. if (FErrorList)
  2472. {
  2473. HandleErrorList(FErrorList);
  2474. }
  2475. }
  2476. //---------------------------------------------------------------------------
  2477. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2478. const TCopyParamType & CopyParam)
  2479. {
  2480. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2481. {
  2482. Configuration->Usage->Inc(L"FileMaskUses");
  2483. }
  2484. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2485. {
  2486. Configuration->Usage->Inc(L"OperationMaskUses");
  2487. }
  2488. }
  2489. //---------------------------------------------------------------------------
  2490. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2491. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2492. {
  2493. DebugAssert(!IsLocalBrowserMode());
  2494. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2495. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2496. TTransferOperationParam DefaultParam;
  2497. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2498. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2499. switch (Param.Queue)
  2500. {
  2501. case asOn:
  2502. CopyParam.Queue = true;
  2503. break;
  2504. case asOff:
  2505. CopyParam.Queue = false;
  2506. break;
  2507. case asAuto:
  2508. default:
  2509. // keep default
  2510. break;
  2511. }
  2512. bool Result =
  2513. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2514. if (Result)
  2515. {
  2516. DebugAssert(Terminal);
  2517. bool SelectionRestored = false;
  2518. TCustomDirView * DView = NULL;
  2519. if (HasDirView[Side])
  2520. {
  2521. DView = DirView(Side);
  2522. DView->SaveSelection();
  2523. DView->SaveSelectedNames();
  2524. }
  2525. UpdateCopyParamCounters(CopyParam);
  2526. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2527. DebugAssert(FTransferResumeList == NULL);
  2528. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2529. FMoveToQueue = false;
  2530. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2531. try
  2532. {
  2533. TStrings * PermanentFileList;
  2534. std::unique_ptr<TStrings> PermanentFileListOwner;
  2535. try
  2536. {
  2537. if (Side == osLocal)
  2538. {
  2539. PermanentFileList = FileList;
  2540. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2541. CopyParam.IncludeFileMask.SetRoots(FileList, Param.TargetDirectory);
  2542. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2543. if (Operation == foMove)
  2544. {
  2545. ReloadLocalDirectory();
  2546. if (DView != NULL)
  2547. {
  2548. DView->RestoreSelection();
  2549. }
  2550. SelectionRestored = true;
  2551. }
  2552. }
  2553. else
  2554. {
  2555. // Clone the file list as it may refer to current directory files,
  2556. // which get destroyed, when the source directory is reloaded after foMove operation.
  2557. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2558. // But for a hotfix, we are not going to do such a big change.
  2559. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2560. PermanentFileList = PermanentFileListOwner.get();
  2561. try
  2562. {
  2563. CopyParam.IncludeFileMask.SetRoots(Param.TargetDirectory, FileList);
  2564. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2565. }
  2566. __finally
  2567. {
  2568. if (Operation == foMove)
  2569. {
  2570. if (DView != NULL)
  2571. {
  2572. DView->RestoreSelection();
  2573. }
  2574. SelectionRestored = true;
  2575. }
  2576. ReloadLocalDirectory(Param.TargetDirectory);
  2577. }
  2578. }
  2579. // When the transfer batch is aborted between individual file transfers, the Abort is not called.
  2580. if (FMoveToQueue)
  2581. {
  2582. Abort();
  2583. }
  2584. }
  2585. catch (EAbort &)
  2586. {
  2587. if (FMoveToQueue)
  2588. {
  2589. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2590. DebugAssert(CopyParam.TransferSkipList == NULL);
  2591. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2592. if (TransferResumeList->Count > 0)
  2593. {
  2594. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2595. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2596. }
  2597. CopyParam.TransferSkipList = TransferResumeList.release();
  2598. // not really needed, just to keep it consistent with TransferResumeList
  2599. FTransferResumeList = NULL;
  2600. FMoveToQueue = false;
  2601. Configuration->Usage->Inc("MovesToBackground");
  2602. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2603. ClearTransferSourceSelection(Direction);
  2604. }
  2605. throw;
  2606. }
  2607. }
  2608. __finally
  2609. {
  2610. if (!SelectionRestored && (DView != NULL))
  2611. {
  2612. DView->DiscardSavedSelection();
  2613. }
  2614. FTransferResumeList = NULL;
  2615. }
  2616. }
  2617. return Result;
  2618. }
  2619. //---------------------------------------------------------------------------
  2620. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2621. TOperationSide Side, TStrings * FileList, void * Param)
  2622. {
  2623. DebugAssert(FileList->Count);
  2624. // We deliberately do not toggle alternative flag (Param), but use OR,
  2625. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2626. // shortcut of CurrentDeleteAlternativeAction
  2627. bool Alternative =
  2628. bool(Param) || UseAlternativeFunction();
  2629. bool Recycle;
  2630. if (IsSideLocalBrowser(Side))
  2631. {
  2632. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2633. }
  2634. else
  2635. {
  2636. Recycle =
  2637. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2638. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2639. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2640. }
  2641. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2642. if (!Result)
  2643. {
  2644. UnicodeString Query;
  2645. if (FileList->Count == 1)
  2646. {
  2647. if (IsSideLocalBrowser(Side))
  2648. {
  2649. Query = ExtractFileName(FileList->Strings[0]);
  2650. }
  2651. else
  2652. {
  2653. Query = UnixExtractFileName(FileList->Strings[0]);
  2654. }
  2655. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2656. }
  2657. else
  2658. {
  2659. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2660. }
  2661. TMessageParams Params(mpNeverAskAgainCheck);
  2662. Params.ImageName = L"Delete file";
  2663. unsigned int Answer =
  2664. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2665. if (Answer == qaNeverAskAgain)
  2666. {
  2667. Result = true;
  2668. if (Recycle)
  2669. {
  2670. WinConfiguration->ConfirmRecycling = false;
  2671. }
  2672. else
  2673. {
  2674. WinConfiguration->ConfirmDeleting = false;
  2675. }
  2676. }
  2677. else
  2678. {
  2679. Result = (Answer == qaOK);
  2680. }
  2681. }
  2682. if (Result)
  2683. {
  2684. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2685. }
  2686. return Result;
  2687. }
  2688. //---------------------------------------------------------------------------
  2689. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2690. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2691. {
  2692. TAutoBatch AutoBatch(this);
  2693. bool Result;
  2694. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2695. {
  2696. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2697. }
  2698. else if (Operation == foRename)
  2699. {
  2700. DebugAssert(DirView(Side)->ItemFocused);
  2701. DirView(Side)->ItemFocused->EditCaption();
  2702. Result = true;
  2703. }
  2704. else if (Operation == foDelete)
  2705. {
  2706. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2707. }
  2708. else if (Operation == foSetProperties)
  2709. {
  2710. DirView(osRemote)->SaveSelectedNames();
  2711. Result = SetProperties(Side, FileList);
  2712. }
  2713. else if (Operation == foCustomCommand)
  2714. {
  2715. DebugAssert(Param);
  2716. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2717. DirView(osRemote)->SaveSelectedNames();
  2718. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2719. CustomCommand(FileList, *Command, NULL);
  2720. Result = true;
  2721. }
  2722. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2723. {
  2724. DebugAssert(!IsSideLocalBrowser(Side));
  2725. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2726. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2727. }
  2728. else if (Operation == foLock)
  2729. {
  2730. DebugAssert(!IsSideLocalBrowser(Side));
  2731. LockFiles(FileList, true);
  2732. Result = true;
  2733. }
  2734. else if (Operation == foUnlock)
  2735. {
  2736. DebugAssert(!IsSideLocalBrowser(Side));
  2737. LockFiles(FileList, false);
  2738. Result = true;
  2739. }
  2740. else
  2741. {
  2742. DebugFail();
  2743. }
  2744. return Result;
  2745. }
  2746. //---------------------------------------------------------------------------
  2747. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2748. {
  2749. if (Side == osCurrent)
  2750. {
  2751. Side = FCurrentSide;
  2752. }
  2753. return Side;
  2754. }
  2755. //---------------------------------------------------------------------------
  2756. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2757. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2758. {
  2759. Side = GetSide(Side);
  2760. bool FullPath = IsSideLocalBrowser(Side);
  2761. TCustomDirView * ADirView = DirView(Side);
  2762. std::unique_ptr<TStringList> FileList(new TStringList());
  2763. ADirView->CreateFileList(OnFocused, FullPath, FileList.get());
  2764. bool ReloadProperties =
  2765. (Side == osRemote) && (Operation == foSetProperties) &&
  2766. DebugAlwaysTrue(HasActiveTerminal()) && Terminal->IsCapable[fcLoadingAdditionalProperties];
  2767. bool Result = ExecuteFileOperation(Operation, Side, FileList.get(), NoConfirmation, Param);
  2768. if (Result && ReloadProperties)
  2769. {
  2770. std::unique_ptr<TStringList> FileList2(new TStringList());
  2771. FileList->CaseSensitive = true;
  2772. FileList->Sort();
  2773. for (int Index = 0; Index < ADirView->Items->Count; Index++)
  2774. {
  2775. TListItem * Item = ADirView->Items->Item[Index];
  2776. UnicodeString FileName = ADirView->ItemFileName(Item);
  2777. int Index2;
  2778. if (FileList->Find(FileName, Index2))
  2779. {
  2780. FileList2->AddObject(FileName, ADirView->ItemData(Item));
  2781. }
  2782. }
  2783. LoadFilesProperties(FileList2.get());
  2784. }
  2785. return Result;
  2786. }
  2787. //---------------------------------------------------------------------------
  2788. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2789. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2790. bool NoConfirmation, void * Param)
  2791. {
  2792. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2793. {
  2794. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2795. {
  2796. DebugAssert(!IsLocalBrowserMode());
  2797. if (GetSide(Side) == osLocal)
  2798. {
  2799. Configuration->Usage->Inc(L"UploadsCommand");
  2800. }
  2801. else
  2802. {
  2803. Configuration->Usage->Inc(L"DownloadsCommand");
  2804. }
  2805. }
  2806. }
  2807. }
  2808. //---------------------------------------------------------------------------
  2809. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2810. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2811. {
  2812. TTransferOperationParam Param;
  2813. if ((WinConfiguration->Interface != ifCommander) ||
  2814. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2815. {
  2816. Flags &= ~cocShortCutHint;
  2817. }
  2818. TCustomDirView * DView = DirView(Side);
  2819. Param.Options =
  2820. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2821. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2822. if (FLAGSET(Flags, cocQueue))
  2823. {
  2824. Param.Queue = asOn;
  2825. }
  2826. else if (FLAGSET(Flags, cocNonQueue))
  2827. {
  2828. Param.Queue = asOff;
  2829. }
  2830. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2831. }
  2832. //---------------------------------------------------------------------------
  2833. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2834. {
  2835. int Count = ErrorList->Count;
  2836. try
  2837. {
  2838. if (ErrorList->Count)
  2839. {
  2840. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2841. if (MessageDialog(Message, qtError,
  2842. qaOK | qaCancel, HELP_NONE) == qaOK)
  2843. {
  2844. unsigned int Answer;
  2845. int Index = 0;
  2846. do
  2847. {
  2848. DebugAssert(Index >= 0 && Index < Count);
  2849. TQueryButtonAlias Aliases[2];
  2850. Aliases[0].Button = qaYes;
  2851. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2852. Aliases[1].Button = qaNo;
  2853. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2854. TMessageParams Params;
  2855. Params.Aliases = Aliases;
  2856. Params.AliasesCount = LENOF(Aliases);
  2857. int No = Index + 1;
  2858. UnicodeString Message = ErrorList->Strings[Index];
  2859. UnicodeString AMainInstructions;
  2860. UnicodeString Details;
  2861. if (ExtractMainInstructions(Message, AMainInstructions))
  2862. {
  2863. Details = Message;
  2864. Message = AMainInstructions;
  2865. }
  2866. Message = MainInstructions(FMTLOAD(ERROR_LIST_NUMBER, (No, Count, Message))) + Details;
  2867. Answer = MoreMessageDialog(
  2868. Message,
  2869. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2870. (Index ? qaYes : 0) | (Index < Count - 1 ? qaNo : 0) |
  2871. qaOK, HELP_NONE, &Params);
  2872. if (Answer == qaNo)
  2873. {
  2874. Index++;
  2875. }
  2876. if (Answer == qaYes)
  2877. {
  2878. Index--;
  2879. }
  2880. }
  2881. while (Answer != qaOK);
  2882. }
  2883. }
  2884. }
  2885. __finally
  2886. {
  2887. TStrings * List = ErrorList;
  2888. ErrorList = NULL;
  2889. for (int i = 0; i < Count; i++)
  2890. {
  2891. delete List->Objects[i];
  2892. }
  2893. delete List;
  2894. }
  2895. }
  2896. //---------------------------------------------------------------------------
  2897. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2898. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2899. {
  2900. // needed for checking filemasks, as there's no directory object
  2901. // associated with the file object
  2902. File->FullFileName = FullFileName;
  2903. TFileMasks::TParams MaskParams;
  2904. MaskParams.Size = File->Size;
  2905. MaskParams.Modification = File->Modification;
  2906. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2907. }
  2908. //---------------------------------------------------------------------------
  2909. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2910. {
  2911. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2912. Side = GetSide(Side);
  2913. TCustomDirView * DView = DirView(Side);
  2914. TListItem * FocusedItem = DView->ItemFocused;
  2915. UnicodeString Name;
  2916. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2917. {
  2918. Name = DView->ItemFileName(FocusedItem);
  2919. }
  2920. else
  2921. {
  2922. Name = LoadStr(NEW_FILE);
  2923. }
  2924. UnicodeString Names = Name;
  2925. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2926. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2927. HELP_EDIT_NEW, History.get(), true, NULL, true, 400))
  2928. {
  2929. while (!Names.IsEmpty())
  2930. {
  2931. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2932. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2933. UnicodeString TargetFileName;
  2934. UnicodeString LocalFileName;
  2935. UnicodeString RootTempDir;
  2936. UnicodeString TempDir;
  2937. UnicodeString RemoteDirectory;
  2938. bool ExistingFile = false;
  2939. if (!IsSideLocalBrowser(Side))
  2940. {
  2941. Name = AbsolutePath(Terminal->CurrentDirectory, Name);
  2942. TRemoteFile * File = NULL;
  2943. if (Terminal->FileExists(Name, &File) &&
  2944. !File->IsDirectory)
  2945. {
  2946. try
  2947. {
  2948. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2949. ExistingFile = true;
  2950. }
  2951. __finally
  2952. {
  2953. delete File;
  2954. }
  2955. }
  2956. if (!ExistingFile)
  2957. {
  2958. RemoteDirectory = UnixExtractFilePath(Name);
  2959. TemporaryDirectoryForRemoteFiles(
  2960. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2961. TargetFileName = UnixExtractFileName(Name);
  2962. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2963. LocalFileName = TempDir +
  2964. // We probably do not want to trim the VMS version here
  2965. Terminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2966. }
  2967. }
  2968. else
  2969. {
  2970. if (ExtractFilePath(Name).IsEmpty())
  2971. {
  2972. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2973. }
  2974. else
  2975. {
  2976. LocalFileName = ExpandFileName(Name);
  2977. }
  2978. TargetFileName = ExtractFileName(Name);
  2979. }
  2980. if (!ExistingFile)
  2981. {
  2982. bool NewFile = !FileExists(ApiPath(LocalFileName));
  2983. if (NewFile)
  2984. {
  2985. int File = FileCreate(ApiPath(LocalFileName));
  2986. if (File < 0)
  2987. {
  2988. if (!RootTempDir.IsEmpty())
  2989. {
  2990. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2991. }
  2992. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2993. }
  2994. else
  2995. {
  2996. FileClose(File);
  2997. }
  2998. }
  2999. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  3000. const TEditorData * ExternalEditor = NULL;
  3001. TFileMasks::TParams MaskParams; // size not known
  3002. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  3003. false, MaskParams);
  3004. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  3005. ExternalEditor, RootTempDir, RemoteDirectory, NewFile);
  3006. }
  3007. }
  3008. }
  3009. }
  3010. //---------------------------------------------------------------------------
  3011. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  3012. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  3013. {
  3014. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3015. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3016. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3017. return
  3018. ((ExecuteFileBy == efInternalEditor)) ||
  3019. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  3020. }
  3021. //---------------------------------------------------------------------------
  3022. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  3023. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  3024. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  3025. UnicodeString RemoteDirectory, bool NewFile)
  3026. {
  3027. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3028. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3029. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3030. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3031. Side = GetSide(Side);
  3032. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  3033. if (!IsSideLocalBrowser(Side))
  3034. {
  3035. Data->Terminal = Terminal;
  3036. Data->Queue = Queue;
  3037. Data->SessionData = CloneCurrentSessionData();
  3038. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  3039. Data->RemoteDirectory = RemoteDirectory;
  3040. Data->SessionName = Terminal->SessionData->SessionName;
  3041. Data->LocalRootDirectory = LocalRootDirectory;
  3042. Data->OriginalFileName = OriginalFileName;
  3043. Data->Command = L""; // will be changed later for external editor
  3044. }
  3045. if (ExecuteFileBy == efInternalEditor)
  3046. {
  3047. if (!IsSideLocalBrowser(Side))
  3048. {
  3049. UnicodeString RemotePath = UnixCombinePaths(RemoteDirectory, OriginalFileName);
  3050. UnicodeString Caption = RemotePath + TitleSeparator + Terminal->SessionData->SessionName;
  3051. TForm * Editor;
  3052. try
  3053. {
  3054. TNotifyEvent OnFileChanged = NULL;
  3055. TNotifyEvent OnSaveAll = NULL;
  3056. TAnyModifiedEvent OnAnyModified = NULL;
  3057. // Edited files are uploaded in background queue, what we do not support with encrypted files
  3058. if (Terminal->IsCapable[fcBackgroundTransfers])
  3059. {
  3060. OnFileChanged = FEditorManager->FileChanged;
  3061. OnSaveAll = SaveAllInternalEditors;
  3062. OnAnyModified = AnyInternalEditorModified;
  3063. }
  3064. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  3065. FEditorManager->FileReload, FEditorManager->FileClosed,
  3066. OnSaveAll, OnAnyModified,
  3067. Caption, StandaloneOperation, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  3068. }
  3069. catch(...)
  3070. {
  3071. if (!LocalRootDirectory.IsEmpty())
  3072. {
  3073. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  3074. }
  3075. throw;
  3076. }
  3077. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  3078. }
  3079. else
  3080. {
  3081. DebugAssert(!StandaloneOperation);
  3082. TForm * Editor =
  3083. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  3084. SaveAllInternalEditors, AnyInternalEditorModified,
  3085. L"", false, SessionColor, -1, NewFile);
  3086. FLocalEditors->Add(Editor);
  3087. }
  3088. }
  3089. else
  3090. {
  3091. HANDLE Process;
  3092. if (ExecuteFileBy == efExternalEditor)
  3093. {
  3094. UnicodeString Program, Params, Dir;
  3095. Data->Command = ExternalEditor->ExternalEditor;
  3096. ReformatFileNameCommand(Data->Command);
  3097. SplitCommand(Data->Command, Program, Params, Dir);
  3098. Params = ExpandFileNameCommand(Params, FileName);
  3099. Program = ExpandEnvironmentVariables(Program);
  3100. if (!ExecuteShell(Program, Params, Process))
  3101. {
  3102. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  3103. }
  3104. }
  3105. else
  3106. {
  3107. DebugAssert(!IsSideLocalBrowser(Side));
  3108. if (!ExecuteShell(FileName, L"", Process))
  3109. {
  3110. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  3111. }
  3112. }
  3113. if (IsSideLocalBrowser(Side) ||
  3114. ((ExecuteFileBy == efShell) &&
  3115. !WinConfiguration->Editor.SDIShellEditor) ||
  3116. ((ExecuteFileBy == efExternalEditor) &&
  3117. !ExternalEditor->SDIExternalEditor))
  3118. {
  3119. // no need for handle
  3120. if (Process != NULL)
  3121. {
  3122. DebugCheck(CloseHandle(Process));
  3123. }
  3124. Process = INVALID_HANDLE_VALUE;
  3125. }
  3126. else
  3127. {
  3128. if (Process == NULL)
  3129. {
  3130. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  3131. }
  3132. }
  3133. if (!IsSideLocalBrowser(Side))
  3134. {
  3135. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  3136. }
  3137. }
  3138. }
  3139. //---------------------------------------------------------------------------
  3140. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3141. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3142. void * /*Arg*/)
  3143. {
  3144. if (Token != NULL)
  3145. {
  3146. EditorFormFileSave(static_cast<TForm *>(Token));
  3147. }
  3148. }
  3149. //---------------------------------------------------------------------------
  3150. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3151. {
  3152. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3153. {
  3154. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3155. }
  3156. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3157. }
  3158. //---------------------------------------------------------------------------
  3159. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3160. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3161. void * Arg)
  3162. {
  3163. if ((Token != NULL) &&
  3164. IsEditorFormModified(static_cast<TForm *>(Token)))
  3165. {
  3166. *static_cast<bool *>(Arg) = true;
  3167. }
  3168. }
  3169. //---------------------------------------------------------------------------
  3170. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3171. TObject * /*Sender*/, bool & Modified)
  3172. {
  3173. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3174. {
  3175. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3176. {
  3177. Modified = true;
  3178. }
  3179. }
  3180. if (!Modified)
  3181. {
  3182. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3183. }
  3184. }
  3185. //---------------------------------------------------------------------------
  3186. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3187. {
  3188. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3189. }
  3190. //---------------------------------------------------------------------------
  3191. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3192. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3193. UnicodeString & Result, UnicodeString & RootDirectory)
  3194. {
  3195. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3196. {
  3197. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3198. Result = RootDirectory;
  3199. }
  3200. else
  3201. {
  3202. RootDirectory = L"";
  3203. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3204. Result = IncludeTrailingBackslash(Result);
  3205. }
  3206. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3207. {
  3208. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3209. }
  3210. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3211. {
  3212. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3213. {
  3214. RemoteDirectory.Delete(1, 1);
  3215. }
  3216. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3217. }
  3218. if (!ForceDirectories(ApiPath(Result)))
  3219. {
  3220. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3221. }
  3222. }
  3223. //---------------------------------------------------------------------------
  3224. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3225. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3226. bool GetTargetNames, bool AutoOperation)
  3227. {
  3228. DebugAssert(!IsLocalBrowserMode());
  3229. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3230. if (ForceText)
  3231. {
  3232. CopyParam.TransferMode = tmAscii;
  3233. }
  3234. TemporaryFileCopyParam(CopyParam);
  3235. if (TempDir.IsEmpty())
  3236. {
  3237. TemporaryDirectoryForRemoteFiles(Terminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3238. }
  3239. DebugAssert(!FAutoOperation);
  3240. FAutoOperation = AutoOperation;
  3241. Terminal->ExceptionOnFail = true;
  3242. try
  3243. {
  3244. try
  3245. {
  3246. // turn off confirmations, as for MDI editors we may possibly download
  3247. // the same file over
  3248. Terminal->CopyToLocal(
  3249. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3250. if (GetTargetNames)
  3251. {
  3252. for (int i = 0; i < FileList->Count; i++)
  3253. {
  3254. FileList->Strings[i] =
  3255. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3256. }
  3257. }
  3258. }
  3259. catch(...)
  3260. {
  3261. if (!RootTempDir.IsEmpty())
  3262. {
  3263. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3264. }
  3265. throw;
  3266. }
  3267. }
  3268. __finally
  3269. {
  3270. FAutoOperation = false;
  3271. Terminal->ExceptionOnFail = false;
  3272. }
  3273. }
  3274. //---------------------------------------------------------------------------
  3275. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3276. {
  3277. // Do not waste time checking for default editor list the next time,
  3278. // or testing if default editor is not notepad.
  3279. bool TryNextTime = false;
  3280. UnicodeString UsageState;
  3281. if (!WinConfiguration->EditorList->IsDefaultList())
  3282. {
  3283. UsageState = "H";
  3284. }
  3285. else
  3286. {
  3287. UnicodeString Executable;
  3288. UnicodeString ExecutableDescription;
  3289. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3290. {
  3291. UnicodeString Message =
  3292. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3293. unsigned int Answer =
  3294. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3295. if (Answer != qaOK)
  3296. {
  3297. UsageState = "R";
  3298. }
  3299. else
  3300. {
  3301. UsageState = "A";
  3302. TEditorData EditorData;
  3303. EditorData.Editor = edExternal;
  3304. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3305. EditorData.ExternalEditorOptionsAutodetect();
  3306. TEditorList EditorList;
  3307. EditorList = *WinConfiguration->EditorList;
  3308. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3309. WinConfiguration->EditorList = &EditorList;
  3310. }
  3311. }
  3312. }
  3313. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3314. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3315. }
  3316. //---------------------------------------------------------------------------
  3317. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3318. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3319. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3320. {
  3321. if (ExecuteFileBy == efDefaultEditor)
  3322. {
  3323. if (!WinConfiguration->OfferedEditorAutoConfig)
  3324. {
  3325. EditorAutoConfig();
  3326. }
  3327. const TEditorPreferences * Editor =
  3328. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3329. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3330. {
  3331. ExecuteFileBy = efInternalEditor;
  3332. ExternalEditor = NULL;
  3333. }
  3334. else if (Editor->Data->Editor == edOpen)
  3335. {
  3336. ExecuteFileBy = efShell;
  3337. ExternalEditor = NULL;
  3338. }
  3339. else
  3340. {
  3341. ExecuteFileBy = efExternalEditor;
  3342. ExternalEditor = Editor->Data;
  3343. }
  3344. }
  3345. }
  3346. //---------------------------------------------------------------------------
  3347. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3348. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3349. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3350. {
  3351. UnicodeString OriginalFileName;
  3352. UnicodeString LocalRootDirectory;
  3353. UnicodeString RemoteDirectory;
  3354. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3355. IsSideLocalBrowser(Side), MaskParams);
  3356. UnicodeString Counter;
  3357. UnicodeString LocalFileName;
  3358. bool Handled = false;
  3359. if (!IsSideLocalBrowser(Side))
  3360. {
  3361. // We need to trim VMS version here, so that we use name without version
  3362. // when uploading back to create a new version of the file
  3363. OriginalFileName = Terminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3364. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3365. TObject * Token = NULL;
  3366. UnicodeString LocalDirectory;
  3367. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3368. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3369. LocalDirectory))
  3370. {
  3371. if (Token != NULL)
  3372. {
  3373. TForm * Form = dynamic_cast<TForm *>(Token);
  3374. if (Form->WindowState == wsMinimized)
  3375. {
  3376. ShowWindow(Form->Handle, SW_RESTORE);
  3377. }
  3378. else
  3379. {
  3380. Form->SetFocus();
  3381. }
  3382. Handled = true;
  3383. }
  3384. else
  3385. {
  3386. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3387. }
  3388. }
  3389. if (!Handled)
  3390. {
  3391. TStringList * FileList1 = new TStringList();
  3392. try
  3393. {
  3394. FileList1->AddObject(FullFileName, Object);
  3395. TemporarilyDownloadFiles(FileList1,
  3396. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3397. LocalRootDirectory, LocalDirectory, true, true);
  3398. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3399. }
  3400. __finally
  3401. {
  3402. delete FileList1;
  3403. }
  3404. switch (ExecuteFileBy)
  3405. {
  3406. case efShell:
  3407. Counter = "RemoteFilesExecuted";
  3408. break;
  3409. case efInternalEditor:
  3410. Counter = "RemoteFilesOpenedInInternalEditor";
  3411. break;
  3412. case efExternalEditor:
  3413. Counter = "RemoteFilesOpenedInExternalEditor";
  3414. break;
  3415. default:
  3416. DebugFail();
  3417. }
  3418. }
  3419. }
  3420. else
  3421. {
  3422. LocalFileName = FullFileName;
  3423. OriginalFileName = ExtractFileName(FullFileName);
  3424. switch (ExecuteFileBy)
  3425. {
  3426. case efShell:
  3427. Counter = "LocalFilesExecuted";
  3428. break;
  3429. case efInternalEditor:
  3430. Counter = "LocalFilesOpenedInInternalEditor";
  3431. break;
  3432. case efExternalEditor:
  3433. Counter = "LocalFilesOpenedInExternalEditor";
  3434. break;
  3435. default:
  3436. DebugFail();
  3437. }
  3438. }
  3439. if (!Handled)
  3440. {
  3441. Configuration->Usage->Inc(Counter);
  3442. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3443. ExternalEditor, LocalRootDirectory, RemoteDirectory, false);
  3444. }
  3445. }
  3446. //---------------------------------------------------------------------------
  3447. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3448. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3449. bool AllSelected, bool OnFocused)
  3450. {
  3451. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3452. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3453. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3454. Side = GetSide(Side);
  3455. TCustomDirView * DView = DirView(Side);
  3456. TStrings * FileList =
  3457. AllSelected ?
  3458. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3459. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3460. try
  3461. {
  3462. DebugAssert(AllSelected || (FileList->Count == 1));
  3463. for (int i = 0; i < FileList->Count; i++)
  3464. {
  3465. UnicodeString ListFileName = FileList->Strings[i];
  3466. UnicodeString FileNameOnly =
  3467. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3468. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3469. if (!DView->ItemIsDirectory(Item))
  3470. {
  3471. UnicodeString FullFileName;
  3472. if (!IsSideLocalBrowser(Side))
  3473. {
  3474. FullFileName = DirView(Side)->Path + ListFileName;
  3475. }
  3476. else
  3477. {
  3478. FullFileName = ListFileName;
  3479. }
  3480. TFileMasks::TParams MaskParams;
  3481. MaskParams.Size = DView->ItemFileSize(Item);
  3482. TDateTimePrecision Precision;
  3483. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3484. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3485. FileList->Objects[i], MaskParams);
  3486. }
  3487. }
  3488. }
  3489. __finally
  3490. {
  3491. delete FileList;
  3492. }
  3493. }
  3494. //---------------------------------------------------------------------------
  3495. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3496. {
  3497. CopyParam.FileNameCase = ncNoChange;
  3498. CopyParam.PreserveRights = false;
  3499. CopyParam.PreserveReadOnly = false;
  3500. CopyParam.ReplaceInvalidChars = true;
  3501. CopyParam.IncludeFileMask = TFileMasks();
  3502. CopyParam.NewerOnly = false;
  3503. CopyParam.FileMask = L"";
  3504. }
  3505. //---------------------------------------------------------------------------
  3506. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3507. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3508. {
  3509. TTerminalManager * Manager = TTerminalManager::Instance();
  3510. if (!IsActiveTerminal(Data->Terminal))
  3511. {
  3512. if (!NonVisualDataModule->Busy)
  3513. {
  3514. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3515. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3516. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3517. if (Data->Terminal == NULL)
  3518. {
  3519. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3520. if (SameSiteTerminal != NULL)
  3521. {
  3522. UnicodeString Message =
  3523. FMTLOAD(EDIT_SESSION_REATTACH,
  3524. (FileNameOnly, Data->SessionName, FileNameOnly));
  3525. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3526. {
  3527. Data->Terminal = Terminal;
  3528. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3529. Data->SessionName = Terminal->SessionData->SessionName;
  3530. // We might also overwrite session data
  3531. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3532. }
  3533. else
  3534. {
  3535. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3536. Abort();
  3537. }
  3538. }
  3539. }
  3540. // foreground session should reconnect itself
  3541. else if (Terminal != Data->Terminal)
  3542. {
  3543. UnicodeString Message =
  3544. MainInstructions(
  3545. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3546. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3547. {
  3548. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3549. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3550. }
  3551. else
  3552. {
  3553. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3554. Abort();
  3555. }
  3556. }
  3557. }
  3558. if (!IsActiveTerminal(Data->Terminal))
  3559. {
  3560. Configuration->Usage->Inc(L"EditInactiveSession");
  3561. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3562. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3563. (ExtractFileName(FileName), Data->SessionName)));
  3564. }
  3565. }
  3566. TStrings * FileList = new TStringList();
  3567. try
  3568. {
  3569. FileList->Add(FileName);
  3570. // consider using the same settings (preset) as when the file was downloaded
  3571. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3572. TemporaryFileCopyParam(CopyParam);
  3573. if (Data->ForceText)
  3574. {
  3575. CopyParam.TransferMode = tmAscii;
  3576. }
  3577. // so i do not need to worry if masking algorithm works in all cases
  3578. // ("" means "copy file name", no masking is actually done)
  3579. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3580. {
  3581. CopyParam.FileMask = L"";
  3582. }
  3583. else
  3584. {
  3585. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3586. }
  3587. DebugAssert(Data->Queue != NULL);
  3588. int Params = cpNoConfirmation | cpTemporary;
  3589. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3590. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3591. QueueItem->CompleteEvent = UploadCompleteEvent;
  3592. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3593. }
  3594. __finally
  3595. {
  3596. delete FileList;
  3597. }
  3598. }
  3599. //---------------------------------------------------------------------------
  3600. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3601. const UnicodeString FileName, const TEditedFileData * Data)
  3602. {
  3603. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3604. DebugAssert(!NonVisualDataModule->Busy);
  3605. if (!IsActiveTerminal(Data->Terminal))
  3606. {
  3607. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3608. (ExtractFileName(FileName), Data->SessionName)));
  3609. }
  3610. TManagedTerminal * PrevSession = TTerminalManager::Instance()->ActiveSession;
  3611. TTerminalManager::Instance()->ActiveSession = Data->Terminal;
  3612. NonVisualDataModule->StartBusy();
  3613. try
  3614. {
  3615. std::unique_ptr<TRemoteFile> File;
  3616. UnicodeString RemoteFileName =
  3617. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3618. Terminal->ExceptionOnFail = true;
  3619. try
  3620. {
  3621. TRemoteFile * AFile = NULL;
  3622. Terminal->ReadFile(RemoteFileName, AFile);
  3623. File.reset(AFile);
  3624. if (!File->HaveFullFileName)
  3625. {
  3626. File->FullFileName = RemoteFileName;
  3627. }
  3628. }
  3629. __finally
  3630. {
  3631. Terminal->ExceptionOnFail = false;
  3632. }
  3633. std::unique_ptr<TStrings> FileList(new TStringList());
  3634. FileList->AddObject(RemoteFileName, File.get());
  3635. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3636. UnicodeString TempDir = ExtractFilePath(FileName);
  3637. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir, TempDir, true, true);
  3638. // sanity check, the target file name should be still the same
  3639. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3640. }
  3641. __finally
  3642. {
  3643. NonVisualDataModule->EndBusy();
  3644. // it actually may not exist anymore...
  3645. TTerminalManager::Instance()->ActiveSession = PrevSession;
  3646. }
  3647. }
  3648. //---------------------------------------------------------------------------
  3649. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3650. const TEditedFileData * Data, bool & KeepOpen)
  3651. {
  3652. // Command is set for external editors only (not for "shell" open).
  3653. if (!Data->Command.IsEmpty())
  3654. {
  3655. bool AnyFound = false;
  3656. bool AnyMDI = false;
  3657. bool AnyNonMDI = false;
  3658. bool AnyDetect = false;
  3659. TEditorList * EditorList = new TEditorList();
  3660. try
  3661. {
  3662. *EditorList = *WinConfiguration->EditorList;
  3663. for (int i = 0; i < EditorList->Count; i++)
  3664. {
  3665. const TEditorPreferences * Editor = EditorList->Editors[i];
  3666. if ((Editor->Data->Editor == edExternal) &&
  3667. (Editor->Data->ExternalEditor == Data->Command))
  3668. {
  3669. AnyFound = true;
  3670. if (Editor->Data->SDIExternalEditor)
  3671. {
  3672. AnyNonMDI = true;
  3673. if (Editor->Data->DetectMDIExternalEditor)
  3674. {
  3675. AnyDetect = true;
  3676. }
  3677. }
  3678. else
  3679. {
  3680. AnyMDI = true;
  3681. }
  3682. }
  3683. }
  3684. bool EnableMDI = false;
  3685. bool DisableDetect = false;
  3686. if (AnyMDI)
  3687. {
  3688. KeepOpen = true;
  3689. if (AnyNonMDI)
  3690. {
  3691. // there is at least one instance of the editor with MDI support enabled,
  3692. // and one with disabled, enable it for all instances
  3693. EnableMDI = true;
  3694. }
  3695. }
  3696. else if (AnyFound && !AnyDetect)
  3697. {
  3698. // at least once instance found but all have MDI autodetection disabled
  3699. // => close the file (default action)
  3700. }
  3701. else
  3702. {
  3703. // no instance of the editor has MDI support enabled
  3704. TMessageParams Params;
  3705. if (AnyFound)
  3706. {
  3707. // there is at least one instance of the editor
  3708. Params.Params |= mpNeverAskAgainCheck;
  3709. }
  3710. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3711. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3712. switch (Answer)
  3713. {
  3714. case qaNeverAskAgain:
  3715. DisableDetect = true;
  3716. break;
  3717. case qaNo:
  3718. EnableMDI = true;
  3719. KeepOpen = true;
  3720. break;
  3721. }
  3722. }
  3723. if (AnyFound && (EnableMDI || DisableDetect))
  3724. {
  3725. bool Changed = false;
  3726. for (int i = 0; i < EditorList->Count; i++)
  3727. {
  3728. const TEditorPreferences * Editor = EditorList->Editors[i];
  3729. if ((Editor->Data->Editor == edExternal) &&
  3730. (Editor->Data->ExternalEditor == Data->Command) &&
  3731. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3732. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3733. {
  3734. Changed = true;
  3735. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3736. if (EnableMDI)
  3737. {
  3738. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3739. }
  3740. if (DisableDetect)
  3741. {
  3742. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3743. }
  3744. EditorList->Change(i, UpdatedEditor);
  3745. }
  3746. }
  3747. if (Changed)
  3748. {
  3749. WinConfiguration->EditorList = EditorList;
  3750. }
  3751. }
  3752. }
  3753. __finally
  3754. {
  3755. delete EditorList;
  3756. }
  3757. }
  3758. else
  3759. {
  3760. // "open" case
  3761. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3762. qaOK, HELP_APP_EARLY_CLOSED);
  3763. }
  3764. }
  3765. //---------------------------------------------------------------------------
  3766. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3767. {
  3768. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3769. }
  3770. //---------------------------------------------------------------------------
  3771. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3772. {
  3773. SideEnter(osRemote);
  3774. }
  3775. //---------------------------------------------------------------------------
  3776. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3777. {
  3778. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3779. SideEnter(osRemote);
  3780. }
  3781. //---------------------------------------------------------------------------
  3782. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3783. {
  3784. FCurrentSide = Side;
  3785. if (Visible)
  3786. {
  3787. UpdateControls();
  3788. }
  3789. }
  3790. //---------------------------------------------------------------------------
  3791. void __fastcall TCustomScpExplorerForm::FileDeleted(TOperationSide Side, const UnicodeString & FileName, bool Success)
  3792. {
  3793. VisualiseOperationFinished(Side, FileName, Success);
  3794. if (DebugAlwaysTrue(FDeletedFiles != NULL) && Success)
  3795. {
  3796. FDeletedFiles->Add(FileName);
  3797. }
  3798. }
  3799. //---------------------------------------------------------------------------
  3800. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3801. TStrings * FileList, bool Alternative)
  3802. {
  3803. TCustomDirView * DView = DirView(Side);
  3804. DView->SaveSelection();
  3805. DView->SaveSelectedNames();
  3806. DebugAssert(!FAlternativeDelete);
  3807. FAlternativeDelete = Alternative;
  3808. try
  3809. {
  3810. if (!IsSideLocalBrowser(Side))
  3811. {
  3812. DebugAssert(Terminal != NULL);
  3813. // Clone the file list as it may refer to current directory files, which get destroyed, when the directory is reloaded after the operation
  3814. std::unique_ptr<TStrings> PermanentFileList(TRemoteFileList::CloneStrings(FileList));
  3815. std::unique_ptr<TStringList> DeletedFiles(new TStringList());
  3816. DeletedFiles->CaseSensitive = DebugAlwaysTrue(!IsSideLocalBrowser(Side));
  3817. int Params = FLAGMASK(Alternative, dfAlternative);
  3818. try
  3819. {
  3820. TValueRestorer<TStrings *> DeletedFilesRestorer(FDeletedFiles);
  3821. DebugAssert(FDeletedFiles == NULL);
  3822. FDeletedFiles = DeletedFiles.get();
  3823. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  3824. FOnFileOperationFinished = FileDeleted;
  3825. FMoveToQueue = false;
  3826. Terminal->DeleteFiles(FileList, Params);
  3827. // Probably not needed for deleting, just for consistency with transfer code
  3828. if (FMoveToQueue)
  3829. {
  3830. Abort();
  3831. }
  3832. }
  3833. catch (EAbort &)
  3834. {
  3835. if (FMoveToQueue)
  3836. {
  3837. DeletedFiles->Sorted = true;
  3838. for (int Index = 0; Index < PermanentFileList->Count; Index++)
  3839. {
  3840. if (DeletedFiles->IndexOf(PermanentFileList->Strings[Index]) >= 0)
  3841. {
  3842. // We should always be deleting the first item => what can be used to optimize this code, if needed
  3843. DebugAssert(Index == 0);
  3844. PermanentFileList->Delete(Index);
  3845. Index--;
  3846. }
  3847. }
  3848. Configuration->Usage->Inc("MovesToBackgroundDelete");
  3849. TQueueItem * QueueItem = new TDeleteQueueItem(Terminal, PermanentFileList.get(), Params);
  3850. AddQueueItem(Queue, QueueItem, Terminal);
  3851. ClearOperationSelection(osRemote);
  3852. }
  3853. }
  3854. }
  3855. else
  3856. {
  3857. if (IsLocalBrowserMode())
  3858. {
  3859. Configuration->Usage->Inc(L"LocalLocalDeletes");
  3860. }
  3861. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  3862. FProgressSide = FCurrentSide;
  3863. try
  3864. {
  3865. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3866. }
  3867. __finally
  3868. {
  3869. ReloadLocalDirectory();
  3870. }
  3871. }
  3872. FAlternativeDelete = false;
  3873. }
  3874. catch(...)
  3875. {
  3876. FAlternativeDelete = false;
  3877. DView->DiscardSavedSelection();
  3878. throw;
  3879. }
  3880. DView->RestoreSelection();
  3881. }
  3882. //---------------------------------------------------------------------------
  3883. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3884. {
  3885. DebugAssert(Terminal);
  3886. DebugAssert(!IsLocalBrowserMode());
  3887. RemoteDirView->SaveSelection();
  3888. RemoteDirView->SaveSelectedNames();
  3889. try
  3890. {
  3891. if (Lock)
  3892. {
  3893. Terminal->LockFiles(FileList);
  3894. }
  3895. else
  3896. {
  3897. Terminal->UnlockFiles(FileList);
  3898. }
  3899. }
  3900. catch(...)
  3901. {
  3902. RemoteDirView->DiscardSavedSelection();
  3903. throw;
  3904. }
  3905. RemoteDirView->RestoreSelection();
  3906. }
  3907. //---------------------------------------------------------------------------
  3908. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  3909. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3910. bool NoConfirmation, bool Move)
  3911. {
  3912. DebugAssert(Terminal != NULL);
  3913. DebugAssert(Terminal == TTerminalManager::Instance()->ActiveTerminal);
  3914. DebugAssert(!IsLocalBrowserMode());
  3915. if (Session == NULL)
  3916. {
  3917. Session = Terminal;
  3918. }
  3919. if (Session == Terminal)
  3920. {
  3921. if (RemoteDriveView->DropTarget != NULL)
  3922. {
  3923. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3924. }
  3925. else if (RemoteDirView->DropTarget != NULL)
  3926. {
  3927. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3928. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3929. }
  3930. else
  3931. {
  3932. Target = RemoteDirView->Path;
  3933. }
  3934. }
  3935. else
  3936. {
  3937. Target = GetSessionPath(Session, osRemote);
  3938. }
  3939. Target = UnixIncludeTrailingBackslash(Target);
  3940. if (FileList->Count == 1)
  3941. {
  3942. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3943. }
  3944. else
  3945. {
  3946. FileMask = AnyMask;
  3947. }
  3948. DirectCopy = Terminal->IsCapable[fcRemoteCopy] || Terminal->IsCapable[fcSecondaryShell];
  3949. bool Result = true;
  3950. if (!NoConfirmation)
  3951. {
  3952. bool Multi = (FileList->Count > 1);
  3953. if (Move)
  3954. {
  3955. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3956. }
  3957. else
  3958. {
  3959. std::unique_ptr<TStrings> Sessions(new TStringList);
  3960. std::unique_ptr<TStrings> Directories(new TStringList);
  3961. TStrings * SessionList = TTerminalManager::Instance()->SessionList;
  3962. for (int Index = 0; Index < SessionList->Count; Index++)
  3963. {
  3964. TManagedTerminal * ASession = DebugNotNull(dynamic_cast<TManagedTerminal *>(SessionList->Objects[Index]));
  3965. if (IsActiveTerminal(ASession) && DebugAlwaysTrue(!ASession->LocalBrowser))
  3966. {
  3967. Sessions->AddObject(SessionList->Strings[Index], ASession);
  3968. Directories->Add(GetSessionPath(ASession, osRemote));
  3969. }
  3970. }
  3971. TDirectRemoteCopy AllowDirectCopy;
  3972. if (Terminal->IsCapable[fcRemoteCopy] || Terminal->CommandSessionOpened)
  3973. {
  3974. DebugAssert(DirectCopy);
  3975. AllowDirectCopy = drcAllow;
  3976. }
  3977. else if (Terminal->IsCapable[fcSecondaryShell])
  3978. {
  3979. DebugAssert(DirectCopy);
  3980. AllowDirectCopy = drcConfirmCommandSession;
  3981. }
  3982. else
  3983. {
  3984. DebugAssert(!DirectCopy);
  3985. AllowDirectCopy = drcDisallow;
  3986. }
  3987. void * ASession = Session;
  3988. Result = DoRemoteCopyDialog(
  3989. Sessions.get(), Directories.get(), AllowDirectCopy, Multi, ASession, Target, FileMask, DirectCopy, Terminal);
  3990. Session = static_cast<TManagedTerminal *>(ASession);
  3991. }
  3992. }
  3993. return Result;
  3994. }
  3995. //---------------------------------------------------------------------------
  3996. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3997. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  3998. {
  3999. DebugAssert(!IsLocalBrowserMode());
  4000. bool DirectCopy;
  4001. UnicodeString Target, FileMask;
  4002. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  4003. if (Result)
  4004. {
  4005. if (!Move && !DirectCopy)
  4006. {
  4007. Configuration->Usage->Inc("RemoteCopyTemp");
  4008. UnicodeString RootTempDir;
  4009. UnicodeString TempDir;
  4010. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false);
  4011. TStrings * TemporaryFilesList = new TStringList();
  4012. try
  4013. {
  4014. TMakeLocalFileListParams MakeFileListParam;
  4015. MakeFileListParam.FileList = TemporaryFilesList;
  4016. MakeFileListParam.FileTimes = NULL;
  4017. MakeFileListParam.IncludeDirs = true;
  4018. MakeFileListParam.Recursive = false;
  4019. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  4020. TTerminalManager::Instance()->ActiveSession = Session;
  4021. if (TemporaryFilesList->Count > 0)
  4022. {
  4023. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4024. CopyParam.FileMask = FileMask;
  4025. DebugAssert(!FAutoOperation);
  4026. FAutoOperation = true;
  4027. Terminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  4028. }
  4029. }
  4030. __finally
  4031. {
  4032. delete TemporaryFilesList;
  4033. FAutoOperation = false;
  4034. if (!RootTempDir.IsEmpty())
  4035. {
  4036. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  4037. }
  4038. }
  4039. }
  4040. else
  4041. {
  4042. RemoteDirView->SaveSelection();
  4043. RemoteDirView->SaveSelectedNames();
  4044. try
  4045. {
  4046. if (Move)
  4047. {
  4048. Configuration->Usage->Inc("RemoteMove");
  4049. Terminal->MoveFiles(FileList, Target, FileMask);
  4050. }
  4051. else
  4052. {
  4053. Configuration->Usage->Inc("RemoteCopyDirect");
  4054. DebugAssert(DirectCopy);
  4055. DebugAssert(Session == Terminal);
  4056. if (Terminal->IsCapable[fcRemoteCopy] ||
  4057. Terminal->CommandSessionOpened ||
  4058. CommandSessionFallback())
  4059. {
  4060. Terminal->CopyFiles(FileList, Target, FileMask);
  4061. }
  4062. }
  4063. }
  4064. catch(...)
  4065. {
  4066. RemoteDirView->DiscardSavedSelection();
  4067. throw;
  4068. }
  4069. RemoteDirView->RestoreSelection();
  4070. }
  4071. }
  4072. return Result;
  4073. }
  4074. //---------------------------------------------------------------------------
  4075. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  4076. const UnicodeString & Path, TRemoteProperties & Properties)
  4077. {
  4078. DebugAssert(!IsLocalBrowserMode());
  4079. Properties.Valid = Properties.Valid << vpEncrypt;
  4080. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  4081. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  4082. }
  4083. //---------------------------------------------------------------------------
  4084. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  4085. {
  4086. Side = GetSide(Side);
  4087. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  4088. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  4089. UnicodeString Name = LoadStr(NEW_FOLDER);
  4090. int AllowedChanges =
  4091. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  4092. bool SaveSettings = false;
  4093. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  4094. {
  4095. TWindowLock Lock(this);
  4096. if (!IsSideLocalBrowser(Side))
  4097. {
  4098. if (SaveSettings)
  4099. {
  4100. GUIConfiguration->NewDirectoryProperties = Properties;
  4101. }
  4102. CreateRemoteDirectory(Name, Properties);
  4103. }
  4104. else
  4105. {
  4106. DirView(Side)->CreateDirectory(Name);
  4107. }
  4108. }
  4109. }
  4110. //---------------------------------------------------------------------------
  4111. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  4112. {
  4113. TWindowLock Lock(this);
  4114. DirView(Side)->ExecuteHomeDirectory();
  4115. }
  4116. //---------------------------------------------------------------------------
  4117. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  4118. {
  4119. DoOpenDirectoryDialog(odBrowse, Side);
  4120. }
  4121. //---------------------------------------------------------------------------
  4122. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  4123. {
  4124. TOperationSide BookmarkSide = IsLocalBrowserMode() ? osLocal : Side;
  4125. UnicodeString Path = Bookmark->GetSideDirectory(BookmarkSide);
  4126. bool Result = !Path.IsEmpty();
  4127. if (Result)
  4128. {
  4129. // While we might get here when the session is closed (from location profiles),
  4130. // it's not a problem as the Path setter is noop then
  4131. DirView(Side)->Path = Path;
  4132. }
  4133. return Result;
  4134. }
  4135. //---------------------------------------------------------------------------
  4136. void __fastcall TCustomScpExplorerForm::CalculateSize(
  4137. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  4138. bool & Close)
  4139. {
  4140. // terminal can be already closed (e.g. dropped connection)
  4141. if (Terminal != NULL)
  4142. {
  4143. try
  4144. {
  4145. TCalculateSizeParams Params;
  4146. Params.Stats = &Stats;
  4147. Terminal->CalculateFilesSize(FileList, Size, Params);
  4148. }
  4149. catch(...)
  4150. {
  4151. if (!Terminal->Active)
  4152. {
  4153. Close = true;
  4154. }
  4155. throw;
  4156. }
  4157. }
  4158. }
  4159. //---------------------------------------------------------------------------
  4160. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  4161. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  4162. bool & Close)
  4163. {
  4164. // terminal can be already closed (e.g. dropped connection)
  4165. if (Terminal != NULL)
  4166. {
  4167. Configuration->Usage->Inc(L"ChecksumCalculated");
  4168. try
  4169. {
  4170. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  4171. }
  4172. catch(...)
  4173. {
  4174. if (!Terminal->Active)
  4175. {
  4176. Close = true;
  4177. }
  4178. throw;
  4179. }
  4180. }
  4181. }
  4182. //---------------------------------------------------------------------------
  4183. void TCustomScpExplorerForm::LoadFilesProperties(TStrings * FileList)
  4184. {
  4185. if (Terminal->LoadFilesProperties(FileList))
  4186. {
  4187. RemoteDirView->Invalidate();
  4188. }
  4189. }
  4190. //---------------------------------------------------------------------------
  4191. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  4192. {
  4193. bool Result;
  4194. if (!IsSideLocalBrowser(Side))
  4195. {
  4196. TRemoteTokenList * GroupList = NULL;
  4197. TRemoteTokenList * UserList = NULL;
  4198. try
  4199. {
  4200. TRemoteProperties CurrentProperties;
  4201. LoadFilesProperties(FileList);
  4202. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4203. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4204. if (CapableGroupChanging || CapableOwnerChanging)
  4205. {
  4206. if (CapableGroupChanging)
  4207. {
  4208. GroupList = Terminal->Groups->Duplicate();
  4209. }
  4210. if (CapableOwnerChanging)
  4211. {
  4212. UserList = Terminal->Users->Duplicate();
  4213. }
  4214. TRemoteDirectory * Files = Terminal->Files;
  4215. int Count = Files->Count;
  4216. if (Count > 100)
  4217. {
  4218. Count = 100;
  4219. }
  4220. for (int Index = 0; Index < Count; Index++)
  4221. {
  4222. TRemoteFile * File = Files->Files[Index];
  4223. if (CapableGroupChanging)
  4224. {
  4225. GroupList->AddUnique(File->Group);
  4226. }
  4227. if (CapableOwnerChanging)
  4228. {
  4229. UserList->AddUnique(File->Owner);
  4230. }
  4231. }
  4232. // if we haven't collected tokens for all files in current directory,
  4233. // make sure we collect them at least for all selected files.
  4234. // (note that so far the files in FileList has to be from current direcotry)
  4235. if (Count < Files->Count)
  4236. {
  4237. for (int Index = 0; Index < FileList->Count; Index++)
  4238. {
  4239. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4240. if (CapableGroupChanging)
  4241. {
  4242. GroupList->AddUnique(File->Group);
  4243. }
  4244. if (CapableOwnerChanging)
  4245. {
  4246. UserList->AddUnique(File->Owner);
  4247. }
  4248. }
  4249. }
  4250. }
  4251. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4252. bool CapableAclChanging = Terminal->IsCapable[fcAclChangingFiles];
  4253. for (int Index = 0; (Index < FileList->Count) && CapableAclChanging; Index++)
  4254. {
  4255. if (dynamic_cast<TRemoteFile *>(FileList->Objects[Index])->IsDirectory)
  4256. {
  4257. CapableAclChanging = false;
  4258. CurrentProperties.Valid = CurrentProperties.Valid >> vpRights;
  4259. }
  4260. }
  4261. int Flags = 0;
  4262. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4263. if (CapableAclChanging) Flags |= cpAcl;
  4264. if (CapableOwnerChanging) Flags |= cpOwner;
  4265. if (CapableGroupChanging) Flags |= cpGroup;
  4266. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4267. if (Terminal->IsCapable[fcCalculatingChecksum])
  4268. {
  4269. CalculateChecksumEvent = CalculateChecksum;
  4270. }
  4271. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4272. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4273. TRemoteProperties NewProperties = CurrentProperties;
  4274. Result =
  4275. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4276. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4277. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4278. CalculateSize, CalculateChecksumEvent);
  4279. if (Result)
  4280. {
  4281. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4282. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4283. }
  4284. }
  4285. __finally
  4286. {
  4287. delete GroupList;
  4288. delete UserList;
  4289. }
  4290. }
  4291. else
  4292. {
  4293. DirView(Side)->DisplayPropertiesMenu();
  4294. Result = true;
  4295. }
  4296. return Result;
  4297. }
  4298. //---------------------------------------------------------------------------
  4299. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4300. {
  4301. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4302. {
  4303. FIgnoreNextDialogChar = Key;
  4304. }
  4305. Key = 0;
  4306. }
  4307. //---------------------------------------------------------------------------
  4308. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4309. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4310. {
  4311. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4312. if (Command != NULL)
  4313. {
  4314. KeyProcessed(Key, Shift);
  4315. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4316. {
  4317. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4318. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4319. false, false, const_cast<TCustomCommandType *>(Command));
  4320. }
  4321. }
  4322. }
  4323. //---------------------------------------------------------------------------
  4324. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4325. {
  4326. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4327. {
  4328. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4329. }
  4330. if ((Key != 0) && !DirView(osCurrent)->IsEditing())
  4331. {
  4332. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4333. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4334. {
  4335. // Note that handling shortcuts on panel (current control) popup menu has precendence over this.
  4336. // But so far it's not a problem, as it does what we want.
  4337. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4338. if (((Action->ShortCut == KeyShortCut) ||
  4339. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4340. AllowedAction(Action, aaShortCut))
  4341. {
  4342. // Has to be called before the action as the dialog char is already in queue.
  4343. // So when the action consumes message queue, we already need to have the
  4344. // FIgnoreNextDialogChar set
  4345. KeyProcessed(Key, Shift);
  4346. // Reset reference to previous component (when menu/toolbar was clicked).
  4347. // Needed to detect that action was invoked by keyboard shortcut
  4348. // in TNonVisualDataModule::ExplorerActionsExecute
  4349. Action->ActionComponent = NULL;
  4350. Action->Execute();
  4351. return;
  4352. }
  4353. }
  4354. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4355. {
  4356. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4357. {
  4358. KeyProcessed(Key, Shift);
  4359. TTerminalManager::Instance()->ActiveSessionIndex = i;
  4360. return;
  4361. }
  4362. }
  4363. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4364. {
  4365. KeyProcessed(Key, Shift);
  4366. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4367. }
  4368. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4369. if (IsCustomShortCut(CustomShortCut))
  4370. {
  4371. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4372. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4373. if (WinConfiguration->SharedBookmarks != NULL)
  4374. {
  4375. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4376. if ((Bookmark != NULL) &&
  4377. OpenBookmark(FCurrentSide, Bookmark))
  4378. {
  4379. KeyProcessed(Key, Shift);
  4380. }
  4381. }
  4382. }
  4383. }
  4384. TForm::KeyDown(Key, Shift);
  4385. }
  4386. //---------------------------------------------------------------------------
  4387. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4388. {
  4389. bool OpenedTerminal = false;
  4390. UnicodeString ProtocolName;
  4391. UnicodeString ProtocolNameHint;
  4392. UnicodeString SecurityProtocolName;
  4393. UnicodeString DurationHint;
  4394. // See the comment in TScpCommanderForm::UpdateControls()
  4395. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4396. if (ActiveTerminal != NULL)
  4397. {
  4398. // Active is not enough here
  4399. if (ActiveTerminal->Status == ssOpened)
  4400. {
  4401. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4402. const TFileSystemInfo & FileSystemInfo = ActiveTerminal->GetFileSystemInfo();
  4403. if (FileSystemInfo.ProtocolName.IsEmpty())
  4404. {
  4405. ProtocolName = SessionInfo.ProtocolName;
  4406. }
  4407. else
  4408. {
  4409. ProtocolName = FileSystemInfo.ProtocolName;
  4410. }
  4411. ProtocolNameHint = LoadStr(STATUS_PROTOCOL_HINT);
  4412. SecurityProtocolName = SessionInfo.SecurityProtocolName;
  4413. DurationHint = LoadStr(STATUS_DURATION_HINT);
  4414. OpenedTerminal = true;
  4415. }
  4416. else if (ActiveTerminal->Status > ssClosed)
  4417. {
  4418. ProtocolName = LoadStr(STATUS_CONNECTING);
  4419. }
  4420. else
  4421. {
  4422. ProtocolName = LoadStr(STATUS_NOT_CONNECTED2);
  4423. }
  4424. }
  4425. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4426. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4427. TTBXStatusPanel * ProtocolStatusPanel = SessionStatusBar->Panels->Items[Offset + 0];
  4428. ProtocolStatusPanel->ViewPriority = (ActiveTerminal != NULL) ? 100 : 0;
  4429. if (ProtocolStatusPanel->Caption != ProtocolName)
  4430. {
  4431. ProtocolStatusPanel->Caption = ProtocolName;
  4432. std::unique_ptr<TCanvas> Canvas(CreateControlCanvas(SessionStatusBar));
  4433. ProtocolStatusPanel->Size = Canvas->TextWidth(ProtocolName) + ScaleByTextHeight(SessionStatusBar, 32);
  4434. }
  4435. ProtocolStatusPanel->Hint = ProtocolNameHint;
  4436. TTBXStatusPanel * SecurityStatusPanel = SessionStatusBar->Panels->Items[Offset + 1];
  4437. SecurityStatusPanel->ViewPriority = OpenedTerminal ? 98 : 0;
  4438. bool SecurityEnabled = !SecurityProtocolName.IsEmpty();
  4439. SecurityStatusPanel->Enabled = SecurityEnabled;
  4440. UnicodeString SecurityProtocolHint;
  4441. if (SecurityEnabled)
  4442. {
  4443. SecurityProtocolHint = FMTLOAD(STATUS_SECURE, (SecurityProtocolName));
  4444. }
  4445. else
  4446. {
  4447. SecurityProtocolHint = LoadStr(STATUS_INSECURE);
  4448. }
  4449. SecurityStatusPanel->Hint = SecurityProtocolHint;
  4450. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[Offset + 2];
  4451. DurationStatusPanel->ViewPriority = OpenedTerminal ? 99 : 0;
  4452. DurationStatusPanel->Hint = DurationHint;
  4453. UpdateStatusBar();
  4454. }
  4455. //---------------------------------------------------------------------------
  4456. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4457. {
  4458. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4459. DebugAssert(SessionStatusBar != NULL);
  4460. TTBXStatusPanel * NoteStatusPanel = SessionStatusBar->Panels->Items[0];
  4461. if (!FNote.IsEmpty())
  4462. {
  4463. NoteStatusPanel->Caption = FNote;
  4464. }
  4465. else
  4466. {
  4467. UpdateStatusPanelText(NoteStatusPanel);
  4468. }
  4469. NoteStatusPanel->Hint = FNoteHints;
  4470. UnicodeString LoginTime;
  4471. // See the comment in TScpCommanderForm::UpdateControls()
  4472. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4473. // Active is not enough here
  4474. if ((ActiveTerminal != NULL) && (ActiveTerminal->Status == ssOpened))
  4475. {
  4476. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4477. LoginTime = FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4478. }
  4479. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1];
  4480. DurationStatusPanel->Caption = LoginTime;
  4481. }
  4482. //---------------------------------------------------------------------------
  4483. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4484. {
  4485. Panel->Caption = L"";
  4486. }
  4487. //---------------------------------------------------------------------------
  4488. void __fastcall TCustomScpExplorerForm::Idle()
  4489. {
  4490. if (FShowing ||
  4491. // Particularly for
  4492. // - Standalone editing
  4493. // - Detecting closed connection and automatically reconnect it while waiting for changes
  4494. // while "keeping remote directory up to date".
  4495. StandaloneOperation)
  4496. {
  4497. FEditorManager->Check();
  4498. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4499. // that needs to know if queue view is visible (and it may be closed after queue update)
  4500. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4501. }
  4502. if (FShowing)
  4503. {
  4504. if (!NonVisualDataModule->Busy &&
  4505. // Menu is opened or drag&drop is going on
  4506. (Mouse->Capture == NULL))
  4507. {
  4508. if (FRefreshRemoteDirectory)
  4509. {
  4510. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4511. {
  4512. Terminal->RefreshDirectory();
  4513. }
  4514. FRefreshRemoteDirectory = false;
  4515. }
  4516. if (FRefreshLocalDirectory)
  4517. {
  4518. ReloadLocalDirectory();
  4519. FRefreshLocalDirectory = false;
  4520. }
  4521. if (WinConfiguration->RefreshRemotePanel)
  4522. {
  4523. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4524. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4525. {
  4526. RemoteDirView->ReloadDirectory();
  4527. }
  4528. }
  4529. }
  4530. }
  4531. if (FShowing || StandaloneOperation)
  4532. {
  4533. if (FQueueStatusInvalidated)
  4534. {
  4535. UpdateQueueStatus(false);
  4536. }
  4537. RefreshQueueItems();
  4538. }
  4539. if (FShowing)
  4540. {
  4541. UpdateStatusBar();
  4542. }
  4543. if (FClipboardFakeMonitorsPendingReset && !FDownloadingFromClipboard)
  4544. {
  4545. FClipboardFakeMonitorsPendingReset = false;
  4546. FClipboardFakeMonitors.reset(NULL);
  4547. }
  4548. FIgnoreNextDialogChar = 0;
  4549. }
  4550. //---------------------------------------------------------------------------
  4551. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4552. {
  4553. try
  4554. {
  4555. FUserActionTimer->Enabled = false;
  4556. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4557. {
  4558. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4559. {
  4560. FPendingQueueActionItem->ProcessUserAction();
  4561. }
  4562. }
  4563. }
  4564. __finally
  4565. {
  4566. FPendingQueueActionItem = NULL;
  4567. }
  4568. }
  4569. //---------------------------------------------------------------------------
  4570. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4571. {
  4572. if (WinConfiguration->MinimizeToTray)
  4573. {
  4574. UpdateTrayIcon();
  4575. FTrayIcon->Visible = true;
  4576. if (Visible)
  4577. {
  4578. ShowWindow(Handle, SW_HIDE);
  4579. }
  4580. }
  4581. }
  4582. //---------------------------------------------------------------------------
  4583. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4584. {
  4585. // WORKAROUND
  4586. // When restoring maximized window from minimization,
  4587. // rarely some controls do not align properly.
  4588. // Two instances seen (both for Commander):
  4589. // - When restoring, window is temporarily narrower (not maximized),
  4590. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4591. // Once maximized already, top dock shinks back, but the session PageControl,
  4592. // do not align up, leaving space between TopDock and PageControl.
  4593. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4594. for (int Index = 0; Index < ControlCount; Index++)
  4595. {
  4596. RealignControl(Controls[Index]);
  4597. }
  4598. if (FTrayIcon->Visible)
  4599. {
  4600. FTrayIcon->Visible = false;
  4601. if (Visible)
  4602. {
  4603. ShowWindow(Handle, SW_SHOW);
  4604. }
  4605. }
  4606. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4607. {
  4608. FNeedSession = false;
  4609. NonVisualDataModule->StartBusy();
  4610. try
  4611. {
  4612. NeedSession(true);
  4613. }
  4614. __finally
  4615. {
  4616. NonVisualDataModule->EndBusy();
  4617. }
  4618. }
  4619. }
  4620. //---------------------------------------------------------------------------
  4621. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4622. {
  4623. FTrayIcon->Hint = GetMainForm()->Caption;
  4624. }
  4625. //---------------------------------------------------------------------------
  4626. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4627. {
  4628. UpdateTrayIcon();
  4629. }
  4630. //---------------------------------------------------------------------------
  4631. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4632. {
  4633. // workaround
  4634. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4635. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4636. // But WM_ACTIVATEAPP message can be received even when
  4637. // the main window is minimized to the tray and internal editor window is focused
  4638. // (after another application was previously active)
  4639. if (::IsIconic(Handle))
  4640. {
  4641. if (!IsAppIconic())
  4642. {
  4643. SetAppIconic(true);
  4644. }
  4645. }
  4646. ::ApplicationRestore();
  4647. Application->BringToFront();
  4648. }
  4649. //---------------------------------------------------------------------------
  4650. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4651. {
  4652. RestoreApp();
  4653. }
  4654. //---------------------------------------------------------------------------
  4655. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  4656. {
  4657. if (OpenInNewWindow())
  4658. {
  4659. ExecuteNewInstance(SessionUrl);
  4660. }
  4661. else
  4662. {
  4663. TTerminalManager::Instance()->NewSession(SessionUrl);
  4664. }
  4665. }
  4666. //---------------------------------------------------------------------------
  4667. void TCustomScpExplorerForm::NewTab(TOperationSide)
  4668. {
  4669. NewSession();
  4670. }
  4671. //---------------------------------------------------------------------------
  4672. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4673. {
  4674. UnicodeString SessionName = StoredSessions->HiddenPrefix + ManagedSession->SessionData->SessionName;
  4675. StoredSessions->NewSession(SessionName, SessionData);
  4676. // modified only, explicit
  4677. StoredSessions->Save(false, true);
  4678. // encode session name because of slashes in hierarchical sessions
  4679. return EncodeUrlString(SessionName);
  4680. }
  4681. //---------------------------------------------------------------------------
  4682. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4683. {
  4684. // current working directories become defaults here, what is not right
  4685. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4686. return SaveHiddenDuplicateSession(SessionData.get());
  4687. }
  4688. //---------------------------------------------------------------------------
  4689. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4690. {
  4691. if (OpenInNewWindow())
  4692. {
  4693. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4694. }
  4695. else
  4696. {
  4697. // current working directories become defaults here, what is not right
  4698. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4699. TTerminalManager * Manager = TTerminalManager::Instance();
  4700. TManagedTerminal * ASession = Manager->NewManagedTerminal(SessionData.get());
  4701. DebugAssert(ASession->LocalBrowser == ManagedSession->LocalBrowser);
  4702. // We definitelly want these
  4703. ASession->Disconnected = ManagedSession->Disconnected;
  4704. // Not sure about these two
  4705. ASession->Permanent = ManagedSession->Permanent;
  4706. ASession->DisconnectedTemporarily = ManagedSession->DisconnectedTemporarily;
  4707. Manager->ActiveSession = ASession;
  4708. }
  4709. }
  4710. //---------------------------------------------------------------------------
  4711. void __fastcall TCustomScpExplorerForm::RenameSession()
  4712. {
  4713. UnicodeString Name = ManagedSession->SessionData->SessionName;
  4714. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME) &&
  4715. // When submitting the default name, do not fix it
  4716. (Name != ManagedSession->SessionData->SessionName))
  4717. {
  4718. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4719. TSessionData::ValidateName(Name);
  4720. // This is inconsistent with how color (for example) is handled.
  4721. ManagedSession->SessionData->Name = Name;
  4722. UpdateControls();
  4723. // Add/Remove distinguishing paths from sessions of the same name.
  4724. SessionListChanged();
  4725. }
  4726. }
  4727. //---------------------------------------------------------------------------
  4728. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4729. {
  4730. DebugAssert(Queue != NULL);
  4731. bool Result = Queue->IsEmpty;
  4732. if (!Result)
  4733. {
  4734. SetFocus();
  4735. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4736. }
  4737. return Result;
  4738. }
  4739. //---------------------------------------------------------------------------
  4740. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4741. {
  4742. return CanCloseQueue(FQueue);
  4743. }
  4744. //---------------------------------------------------------------------------
  4745. void __fastcall TCustomScpExplorerForm::CloseSession()
  4746. {
  4747. if (CanCloseQueue())
  4748. {
  4749. TTerminalManager::Instance()->FreeActiveTerminal();
  4750. }
  4751. }
  4752. //---------------------------------------------------------------------------
  4753. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4754. {
  4755. if (CanCloseQueue())
  4756. {
  4757. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4758. }
  4759. }
  4760. //---------------------------------------------------------------------------
  4761. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4762. {
  4763. DetachTerminal(Terminal);
  4764. RemoteDirView->Terminal = Terminal;
  4765. UpdateRemotePathComboBox(false);
  4766. InitStatusBar();
  4767. UpdateControls();
  4768. }
  4769. //---------------------------------------------------------------------------
  4770. void __fastcall TCustomScpExplorerForm::TerminalConnecting()
  4771. {
  4772. InitStatusBar();
  4773. }
  4774. //---------------------------------------------------------------------------
  4775. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4776. {
  4777. if (DebugAlwaysTrue(!Terminal->Active))
  4778. {
  4779. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4780. }
  4781. }
  4782. //---------------------------------------------------------------------------
  4783. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4784. {
  4785. if (OpenInNewWindow())
  4786. {
  4787. // encode session name because of slashes in hierarchical sessions
  4788. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4789. }
  4790. else
  4791. {
  4792. TTerminalManager * Manager = TTerminalManager::Instance();
  4793. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4794. Manager->ActiveSession = Terminal;
  4795. }
  4796. }
  4797. //---------------------------------------------------------------------------
  4798. void TCustomScpExplorerForm::DoOpenFolderOrWorkspace(
  4799. const UnicodeString & Name, bool ConnectFirstTerminal, bool CheckMaxSessions)
  4800. {
  4801. TTerminalManager * Manager = TTerminalManager::Instance();
  4802. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4803. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4804. if (!CheckMaxSessions || (DataList->Count <= WinConfiguration->MaxSessions))
  4805. {
  4806. TManagedTerminal * FirstSession = Manager->NewSessions(DataList.get());
  4807. // FirstSession can be null, if some of the
  4808. if (!ConnectFirstTerminal && (FirstSession != NULL))
  4809. {
  4810. FirstSession->Disconnected = true;
  4811. FirstSession->DisconnectedTemporarily = true;
  4812. }
  4813. Manager->ActiveSession = FirstSession;
  4814. }
  4815. }
  4816. //---------------------------------------------------------------------------
  4817. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4818. {
  4819. if (OpenInNewWindow())
  4820. {
  4821. ExecuteNewInstance(Name);
  4822. }
  4823. else
  4824. {
  4825. DoOpenFolderOrWorkspace(Name, true, false);
  4826. }
  4827. }
  4828. //---------------------------------------------------------------------------
  4829. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4830. bool &CanClose)
  4831. {
  4832. // See the comment in CloseApp()
  4833. if (NonVisualDataModule->Busy)
  4834. {
  4835. CanClose = false;
  4836. }
  4837. else
  4838. {
  4839. TTerminalManager * Manager = TTerminalManager::Instance();
  4840. int ActiveSessions = 0;
  4841. for (int Index = 0; Index < Manager->Count; Index++)
  4842. {
  4843. if (Manager->Sessions[Index]->Active)
  4844. {
  4845. ActiveSessions++;
  4846. }
  4847. }
  4848. // Confirm, when there's at least one active remote session or
  4849. // when there are multiple (inactive or local) sessions and auto workspace saving is not enabled.
  4850. if (((ActiveSessions > 0) || ((Manager->Count > 1) && !WinConfiguration->AutoSaveWorkspace)) &&
  4851. WinConfiguration->ConfirmClosingSession)
  4852. {
  4853. unsigned int Result;
  4854. TMessageParams Params(mpNeverAskAgainCheck);
  4855. UnicodeString Message;
  4856. int Answers = qaOK | qaCancel;
  4857. // The current session is the only active session
  4858. if ((ActiveSessions == 1) && (Terminal != NULL) && Terminal->Active)
  4859. {
  4860. if (!WinConfiguration->AutoSaveWorkspace)
  4861. {
  4862. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  4863. }
  4864. else
  4865. {
  4866. Message = LoadStr(CLOSE_SESSION);
  4867. }
  4868. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  4869. }
  4870. // Multiple active sessions or one active session, but it's not the current one
  4871. else if (ActiveSessions > 0)
  4872. {
  4873. if (!WinConfiguration->AutoSaveWorkspace)
  4874. {
  4875. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  4876. }
  4877. else
  4878. {
  4879. Message = LoadStr(CLOSE_SESSIONS);
  4880. }
  4881. }
  4882. // Multiple tabs, but all are inactive or local, and saving of worspace is not enabled
  4883. else
  4884. {
  4885. DebugAlwaysTrue(Manager->Count > 1);
  4886. DebugAlwaysTrue(!WinConfiguration->AutoSaveWorkspace);
  4887. Message = LoadStr(CLOSE_WORKSPACE);
  4888. }
  4889. UnicodeString Note;
  4890. if (WinConfiguration->AutoSaveWorkspace)
  4891. {
  4892. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  4893. }
  4894. else
  4895. {
  4896. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  4897. Answers = qaYes | qaNo | qaCancel;
  4898. }
  4899. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  4900. SetFocus();
  4901. Result = MessageDialog(Message, qtConfirmation,
  4902. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  4903. if (Result == qaNeverAskAgain)
  4904. {
  4905. WinConfiguration->ConfirmClosingSession = false;
  4906. }
  4907. if (Result == qaNo)
  4908. {
  4909. CanClose = SaveWorkspace(true);
  4910. // note that the workspace will be saved redundatly again from FormClose
  4911. }
  4912. else
  4913. {
  4914. CanClose =
  4915. (Result == qaOK) ||
  4916. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4917. (Result == qaNeverAskAgain);
  4918. }
  4919. }
  4920. if (CanClose && HasActiveTerminal())
  4921. {
  4922. CanClose = CanCloseQueue();
  4923. }
  4924. }
  4925. if (CanClose)
  4926. {
  4927. CanClose =
  4928. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4929. FEditorManager->CloseExternalFilesWithoutProcess();
  4930. if (CanClose)
  4931. {
  4932. while (CanClose && (FLocalEditors->Count > 0))
  4933. {
  4934. int PrevCount = FLocalEditors->Count;
  4935. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4936. CanClose = (FLocalEditors->Count < PrevCount);
  4937. }
  4938. if (CanClose)
  4939. {
  4940. CanClose = FEditorManager->Empty(true);
  4941. if (!CanClose)
  4942. {
  4943. SetFocus();
  4944. CanClose =
  4945. (MessageDialog(
  4946. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4947. }
  4948. }
  4949. }
  4950. }
  4951. }
  4952. //---------------------------------------------------------------------------
  4953. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4954. {
  4955. TForm * Form = dynamic_cast<TForm *>(Sender);
  4956. DebugAssert(Form != NULL);
  4957. Form->Close();
  4958. }
  4959. //---------------------------------------------------------------------------
  4960. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4961. {
  4962. TForm * Form = dynamic_cast<TForm *>(Sender);
  4963. delete Form;
  4964. }
  4965. //---------------------------------------------------------------------------
  4966. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4967. {
  4968. while (FLocalEditors->Count > 0)
  4969. {
  4970. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4971. }
  4972. }
  4973. //---------------------------------------------------------------------------
  4974. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4975. TObject *Sender)
  4976. {
  4977. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4978. try
  4979. {
  4980. SetProperties(osRemote, FileList);
  4981. }
  4982. __finally
  4983. {
  4984. delete FileList;
  4985. }
  4986. }
  4987. //---------------------------------------------------------------------------
  4988. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4989. {
  4990. if (value)
  4991. {
  4992. if (Component == fcCustomCommandsBand)
  4993. {
  4994. UpdateCustomCommandsToolbar();
  4995. }
  4996. if (Component == fcQueueFileList)
  4997. {
  4998. DebugAssert(!QueueFileListSplitter->Visible);
  4999. AdjustQueueLayout();
  5000. UpdateQueueFileList();
  5001. }
  5002. }
  5003. }
  5004. //---------------------------------------------------------------------------
  5005. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  5006. {
  5007. int MinHeightLimit =
  5008. GetStaticQueuePanelComponentsHeight() +
  5009. QueueFileListSplitter->Height +
  5010. QueueFileList->Height +
  5011. GetMinQueueViewHeight();
  5012. if (QueuePanel->ClientHeight < MinHeightLimit)
  5013. {
  5014. QueuePanel->ClientHeight = MinHeightLimit;
  5015. }
  5016. }
  5017. //---------------------------------------------------------------------------
  5018. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  5019. {
  5020. TControl * Control = GetComponent(Component);
  5021. DebugAssert(Control);
  5022. bool Changed = (Control->Visible != value);
  5023. if (Changed)
  5024. {
  5025. ComponentShowing(Component, value);
  5026. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  5027. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  5028. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  5029. if (value)
  5030. {
  5031. int RemainingHeight = Control->Parent->ClientHeight;
  5032. int RemainingWidth = Control->Parent->ClientWidth;
  5033. for (int i = 0; i < Control->Parent->ControlCount; i++)
  5034. {
  5035. TControl * ChildControl = Control->Parent->Controls[i];
  5036. if (ChildControl->Visible)
  5037. {
  5038. switch (ChildControl->Align)
  5039. {
  5040. case alTop:
  5041. case alBottom:
  5042. RemainingHeight -= ChildControl->Height;
  5043. break;
  5044. case alLeft:
  5045. case alRight:
  5046. RemainingWidth -= ChildControl->Width;
  5047. break;
  5048. }
  5049. }
  5050. }
  5051. int Reserve = ScaleByTextHeight(this, 32);
  5052. // queue in explorer, trees in commander
  5053. if (Control->Height > RemainingHeight - Reserve)
  5054. {
  5055. Control->Height = RemainingHeight / 2;
  5056. }
  5057. if (Control->Width > RemainingWidth - Reserve)
  5058. {
  5059. Control->Width = RemainingWidth / 2;
  5060. }
  5061. }
  5062. Control->Visible = value;
  5063. if (WasFocused && Visible)
  5064. {
  5065. DirView(osCurrent)->SetFocus();
  5066. }
  5067. FixControlsPlacement();
  5068. }
  5069. }
  5070. //---------------------------------------------------------------------------
  5071. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  5072. {
  5073. TControl * Control = GetComponent(Component);
  5074. if (Control == NULL)
  5075. {
  5076. return false;
  5077. }
  5078. else
  5079. {
  5080. return Control->Visible;
  5081. }
  5082. }
  5083. //---------------------------------------------------------------------------
  5084. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  5085. {
  5086. bool Result;
  5087. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  5088. {
  5089. Result = !IsUWP();
  5090. }
  5091. else
  5092. {
  5093. Result = true;
  5094. }
  5095. return Result;
  5096. }
  5097. //---------------------------------------------------------------------------
  5098. void __fastcall TCustomScpExplorerForm::MakeFocusedItemVisible(TCustomDirView * DirView)
  5099. {
  5100. if (DirView->ItemFocused != NULL)
  5101. {
  5102. DirView->ItemFocused->MakeVisible(false);
  5103. }
  5104. }
  5105. //---------------------------------------------------------------------------
  5106. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  5107. {
  5108. MakeFocusedItemVisible(RemoteDirView);
  5109. QueueSplitter->Visible = QueuePanel->Visible;
  5110. QueueFileListSplitter->Visible = QueueFileList->Visible;
  5111. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  5112. TControl * QueueControlsOrder[] =
  5113. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  5114. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  5115. }
  5116. //---------------------------------------------------------------------------
  5117. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  5118. {
  5119. switch (Component) {
  5120. case fcStatusBar: return RemoteStatusBar;
  5121. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  5122. case fcQueueView: return QueuePanel;
  5123. case fcQueueToolbar: return QueueDock;
  5124. case fcRemoteTree: return RemoteDrivePanel;
  5125. case fcSessionsTabs: return SessionsPageControl;
  5126. case fcQueueFileList: return QueueFileList;
  5127. default: return NULL;
  5128. }
  5129. }
  5130. //---------------------------------------------------------------------------
  5131. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  5132. TObject *Sender, TListColumn *Column, TPoint &Point)
  5133. {
  5134. DebugAssert(NonVisualDataModule && Column && Sender);
  5135. NonVisualDataModule->ListColumn = Column;
  5136. TPopupMenu * DirViewColumnMenu;
  5137. if (Sender == DirView(osOther))
  5138. {
  5139. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  5140. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  5141. (Column->Index == uvName);
  5142. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  5143. (Column->Index == uvSize);
  5144. }
  5145. else
  5146. {
  5147. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  5148. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  5149. (Column->Index == dvName);
  5150. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  5151. (Column->Index == dvSize);
  5152. }
  5153. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  5154. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  5155. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  5156. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  5157. }
  5158. //---------------------------------------------------------------------------
  5159. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  5160. TObject *Sender, TListItem *Item, bool &AllowExec)
  5161. {
  5162. DoDirViewExecFile(Sender, Item, AllowExec);
  5163. }
  5164. //---------------------------------------------------------------------------
  5165. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  5166. TListItem * Item, bool & AllowExec)
  5167. {
  5168. DebugAssert(Sender && Item && Configuration);
  5169. DebugAssert(AllowExec);
  5170. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  5171. TOperationSide Side = ((ADirView == DirView(osRemote)) ? osRemote : osLocal);
  5172. bool Remote = !IsSideLocalBrowser(Side);
  5173. DebugAssert(Item == ADirView->ItemFocused);
  5174. bool IsDirectory = ADirView->ItemIsDirectory(Item);
  5175. TResolvedDoubleClickAction Action;
  5176. if (FForceExecution)
  5177. {
  5178. Action = IsDirectory ? rdcaChangeDir : rdcaOpen;
  5179. }
  5180. else
  5181. {
  5182. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  5183. Action = WinConfiguration->ResolveDoubleClickAction(IsDirectory, ATerminal);
  5184. }
  5185. if (Action == rdcaCopy)
  5186. {
  5187. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  5188. UnlockWindow();
  5189. try
  5190. {
  5191. if (IsLocalBrowserMode())
  5192. {
  5193. LocalLocalCopy(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation, false, 0);
  5194. }
  5195. else
  5196. {
  5197. ExecuteFileOperation(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  5198. }
  5199. }
  5200. __finally
  5201. {
  5202. LockWindow();
  5203. }
  5204. AllowExec = false;
  5205. }
  5206. else if (Action == rdcaEdit)
  5207. {
  5208. if (!Remote || !WinConfiguration->DisableOpenEdit)
  5209. {
  5210. ExecuteFile(Side, efDefaultEditor);
  5211. AllowExec = false;
  5212. }
  5213. }
  5214. else if (Action == rdcaOpen)
  5215. {
  5216. if (DebugAlwaysTrue(!IsDirectory) && Remote && !WinConfiguration->DisableOpenEdit)
  5217. {
  5218. ExecuteFile(osRemote, efShell);
  5219. AllowExec = false;
  5220. }
  5221. }
  5222. }
  5223. //---------------------------------------------------------------------------
  5224. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5225. {
  5226. return ((Side == osRemote) || (Side == osCurrent));
  5227. }
  5228. //---------------------------------------------------------------------------
  5229. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5230. {
  5231. DebugFail();
  5232. }
  5233. //---------------------------------------------------------------------------
  5234. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5235. {
  5236. DebugFail();
  5237. }
  5238. //---------------------------------------------------------------------------
  5239. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5240. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5241. {
  5242. TSynchronizeParamType Params;
  5243. Params.LocalDirectory = LocalDirectory;
  5244. Params.RemoteDirectory = RemoteDirectory;
  5245. int UnusedParams =
  5246. (GUIConfiguration->SynchronizeParams &
  5247. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5248. TTerminal::spCaseSensitive));
  5249. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5250. Params.Options = GUIConfiguration->SynchronizeOptions;
  5251. bool SaveSettings = false;
  5252. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5253. &DoSynchronizeTooManyDirectories);
  5254. DebugAssert(FSynchronizeController == NULL);
  5255. FSynchronizeController = &Controller;
  5256. bool Result;
  5257. try
  5258. {
  5259. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5260. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5261. int Options =
  5262. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5263. DebugAssert(FOnFeedSynchronizeError == NULL);
  5264. DebugAssert(FOnSynchronizeAbort == NULL);
  5265. DebugAssert(FSynchronizeTerminal == NULL);
  5266. FSynchronizeTerminal = Terminal;
  5267. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5268. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5269. FOnFeedSynchronizeError, FOnSynchronizeAbort, SynchronizeInNewWindow, UseDefaults);
  5270. if (Result)
  5271. {
  5272. if (SaveSettings)
  5273. {
  5274. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5275. GUIConfiguration->SynchronizeOptions = Params.Options;
  5276. }
  5277. else
  5278. {
  5279. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5280. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5281. FLAGSET(Params.Options, soSynchronize))
  5282. {
  5283. GUIConfiguration->SynchronizeOptions =
  5284. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5285. soSynchronize;
  5286. }
  5287. }
  5288. LocalDirectory = Params.LocalDirectory;
  5289. RemoteDirectory = Params.RemoteDirectory;
  5290. }
  5291. }
  5292. __finally
  5293. {
  5294. FSynchronizeController = NULL;
  5295. DebugAssert(FOnFeedSynchronizeError == NULL);
  5296. FOnFeedSynchronizeError = NULL;
  5297. DebugAssert(FOnSynchronizeAbort == NULL);
  5298. FOnSynchronizeAbort = NULL;
  5299. FSynchronizeTerminal = NULL;
  5300. }
  5301. return Result;
  5302. }
  5303. //---------------------------------------------------------------------------
  5304. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5305. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5306. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5307. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5308. TSynchronizeOptions * Options, bool Full)
  5309. {
  5310. // Keep-up-to-date mode
  5311. if (Terminal->Status == ssOpened)
  5312. {
  5313. try
  5314. {
  5315. int PParams = Params.Params;
  5316. if (!Full)
  5317. {
  5318. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5319. if (FLAGSET(Params.Options, soRecurse))
  5320. {
  5321. PParams |= TTerminal::spSubDirs;
  5322. }
  5323. }
  5324. else
  5325. {
  5326. // if keepuptodate is non-recursive,
  5327. // full sync before has to be non-recursive as well
  5328. if (FLAGCLEAR(Params.Options, soRecurse))
  5329. {
  5330. PParams |= TTerminal::spNoRecurse;
  5331. }
  5332. }
  5333. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5334. PParams, Checklist, Options);
  5335. }
  5336. catch (Exception & E)
  5337. {
  5338. if (!Terminal->Active)
  5339. {
  5340. ShowExtendedExceptionEx(Terminal, &E);
  5341. // Do not abort the "keep up to date" when the session was succesfully reconnected.
  5342. if (!Terminal->Active)
  5343. {
  5344. throw;
  5345. }
  5346. }
  5347. else
  5348. {
  5349. if (FLAGSET(Params.Options, soContinueOnError) &&
  5350. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5351. {
  5352. // noop, already logged in MoreMessageDialog
  5353. }
  5354. else
  5355. {
  5356. // We get mostly EAbort here, so this is noop.
  5357. // Exception is an error when listing directories while looking for differences.
  5358. ShowExtendedExceptionEx(Terminal, &E);
  5359. throw;
  5360. }
  5361. }
  5362. }
  5363. }
  5364. }
  5365. //---------------------------------------------------------------------------
  5366. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5367. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5368. const UnicodeString ErrorStr)
  5369. {
  5370. if (!Directory.IsEmpty())
  5371. {
  5372. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5373. }
  5374. else
  5375. {
  5376. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5377. }
  5378. }
  5379. //---------------------------------------------------------------------------
  5380. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5381. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5382. {
  5383. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5384. {
  5385. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5386. }
  5387. else
  5388. {
  5389. TMessageParams Params(mpNeverAskAgainCheck);
  5390. unsigned int Result = MessageDialog(
  5391. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5392. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5393. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5394. {
  5395. MaxDirectories *= 2;
  5396. if (Result == qaNeverAskAgain)
  5397. {
  5398. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5399. }
  5400. }
  5401. else
  5402. {
  5403. Abort();
  5404. }
  5405. }
  5406. }
  5407. //---------------------------------------------------------------------------
  5408. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5409. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5410. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5411. TSynchronizeOptions * Options)
  5412. {
  5413. // Keep-up-to-date mode
  5414. DebugAssert(!FAutoOperation);
  5415. void * BatchStorage;
  5416. BatchStart(BatchStorage);
  5417. FAutoOperation = true;
  5418. bool AnyOperation = false;
  5419. TDateTime StartTime = Now();
  5420. TSynchronizeChecklist * AChecklist = NULL;
  5421. try
  5422. {
  5423. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5424. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5425. {
  5426. FSynchronizeProgressForm->Start();
  5427. }
  5428. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5429. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5430. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5431. Options);
  5432. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5433. SAFE_DESTROY(FSynchronizeProgressForm);
  5434. AnyOperation = (AChecklist->CheckedCount > 0);
  5435. if (AnyOperation)
  5436. {
  5437. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5438. CreateProgressForm(&SynchronizeProgress);
  5439. try
  5440. {
  5441. Terminal->SynchronizeApply(
  5442. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5443. SynchronizeProcessedItem, NULL, NULL, NULL);
  5444. }
  5445. __finally
  5446. {
  5447. DestroyProgressForm();
  5448. }
  5449. }
  5450. }
  5451. __finally
  5452. {
  5453. if (Checklist == NULL)
  5454. {
  5455. delete AChecklist;
  5456. }
  5457. else
  5458. {
  5459. *Checklist = AChecklist;
  5460. }
  5461. FAutoOperation = false;
  5462. SAFE_DESTROY(FSynchronizeProgressForm);
  5463. BatchEnd(BatchStorage);
  5464. ReloadLocalDirectory();
  5465. if (AnyOperation)
  5466. {
  5467. OperationComplete(StartTime);
  5468. }
  5469. }
  5470. }
  5471. //---------------------------------------------------------------------------
  5472. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5473. {
  5474. // can be called from command line
  5475. return Visible &&
  5476. ((DirView(osRemote)->SelCount > 0) ||
  5477. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5478. }
  5479. //---------------------------------------------------------------------------
  5480. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5481. {
  5482. LogSynchronizeEvent(Terminal, Message);
  5483. }
  5484. //---------------------------------------------------------------------------
  5485. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5486. int Params, TSynchronizeOptions & Options)
  5487. {
  5488. DebugAssert(!IsLocalBrowserMode());
  5489. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5490. {
  5491. Options.Filter = new TStringList();
  5492. Options.Filter->CaseSensitive = false;
  5493. Options.Filter->Duplicates = Types::dupAccept;
  5494. if (DirView(osRemote)->SelCount > 0)
  5495. {
  5496. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5497. }
  5498. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5499. {
  5500. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5501. }
  5502. Options.Filter->Sort();
  5503. }
  5504. }
  5505. //---------------------------------------------------------------------------
  5506. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5507. {
  5508. TCopyParamType Defaults;
  5509. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5510. ConfigStorage->AccessMode = smRead;
  5511. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5512. {
  5513. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5514. }
  5515. ConfigStorage.reset(NULL);
  5516. std::unique_ptr<TStringList> Options(new TStringList());
  5517. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5518. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5519. UnicodeString Result;
  5520. if (Options->Count > 0)
  5521. {
  5522. Result =
  5523. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5524. }
  5525. return Result;
  5526. }
  5527. //---------------------------------------------------------------------------
  5528. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5529. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5530. {
  5531. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5532. UnicodeString AdditionalParams =
  5533. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5534. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5535. SerializeCopyParamForCommandLine(CopyParams),
  5536. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5537. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5538. ExecuteNewInstance(SessionName, AdditionalParams);
  5539. }
  5540. //---------------------------------------------------------------------------
  5541. struct TSynchronizeParams
  5542. {
  5543. UnicodeString LocalDirectory;
  5544. UnicodeString RemoteDirectory;
  5545. TSynchronizeMode Mode;
  5546. TSynchronizeChecklist * Checklist;
  5547. int Params;
  5548. TCopyParamType * CopyParam;
  5549. TDateTime CollectElapsed;
  5550. TDateTime * StartTime;
  5551. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5552. };
  5553. //---------------------------------------------------------------------------
  5554. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5555. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5556. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5557. {
  5558. DebugAssert(!FAutoOperation);
  5559. void * BatchStorage;
  5560. BatchStart(BatchStorage);
  5561. FAutoOperation = true;
  5562. TFileOperationStatistics Statistics;
  5563. TDateTime Start = Now();
  5564. try
  5565. {
  5566. Params.OnProcessedItem = OnProcessedItem;
  5567. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5568. CreateProgressForm(&SynchronizeProgress);
  5569. Terminal->SynchronizeApply(
  5570. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5571. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5572. &Statistics);
  5573. }
  5574. __finally
  5575. {
  5576. Params.OnProcessedItem = NULL;
  5577. FAutoOperation = false;
  5578. DestroyProgressForm();
  5579. BatchEnd(BatchStorage);
  5580. ReloadLocalDirectory();
  5581. }
  5582. if (WinConfiguration->SynchronizeSummary)
  5583. {
  5584. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5585. // The statistics should be 0 anyway in this case
  5586. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5587. {
  5588. Message += L"\n";
  5589. if (Statistics.FilesUploaded > 0)
  5590. {
  5591. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5592. }
  5593. if (Statistics.FilesDownloaded > 0)
  5594. {
  5595. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5596. }
  5597. if (Statistics.FilesDeletedLocal > 0)
  5598. {
  5599. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5600. }
  5601. if (Statistics.FilesDeletedRemote > 0)
  5602. {
  5603. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5604. }
  5605. }
  5606. TDateTime Elapsed = (Now() - Start);
  5607. Message +=
  5608. L"\n" +
  5609. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5610. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5611. TMessageParams Params(mpNeverAskAgainCheck);
  5612. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5613. if (Result == qaNeverAskAgain)
  5614. {
  5615. WinConfiguration->SynchronizeSummary = false;
  5616. }
  5617. }
  5618. }
  5619. //---------------------------------------------------------------------------
  5620. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5621. {
  5622. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5623. {
  5624. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5625. }
  5626. // Not set in keep-up-to-date mode - Synchronize() method
  5627. if (Token != NULL)
  5628. {
  5629. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5630. if (Params.OnProcessedItem != NULL)
  5631. {
  5632. Params.OnProcessedItem(NULL, ChecklistItem);
  5633. }
  5634. }
  5635. }
  5636. //---------------------------------------------------------------------------
  5637. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5638. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5639. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5640. {
  5641. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5642. *Params.StartTime = Now();
  5643. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5644. }
  5645. //---------------------------------------------------------------------------
  5646. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5647. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5648. {
  5649. // terminal can be already closed (e.g. dropped connection)
  5650. if (Terminal != NULL)
  5651. {
  5652. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5653. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5654. }
  5655. }
  5656. //---------------------------------------------------------------------------
  5657. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5658. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5659. {
  5660. DebugAssert(!IsLocalBrowserMode());
  5661. TAutoBatch AutoBatch(this);
  5662. TAutoFlag AutoOperationFlag(FAutoOperation);
  5663. if (Side == osRemote)
  5664. {
  5665. std::unique_ptr<TStrings> FileList(new TStringList());
  5666. FileList->AddObject(FileName, RemoteFile);
  5667. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5668. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5669. RemoteDirView->SaveSelection();
  5670. RemoteDirView->SaveSelectedNames();
  5671. try
  5672. {
  5673. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5674. }
  5675. catch(...)
  5676. {
  5677. RemoteDirView->DiscardSavedSelection();
  5678. throw;
  5679. }
  5680. RemoteDirView->RestoreSelection();
  5681. }
  5682. else if (DebugAlwaysTrue(Side == osLocal))
  5683. {
  5684. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5685. {
  5686. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5687. }
  5688. UnicodeString Directory = ExtractFileDir(FileName);
  5689. ReloadLocalDirectory(Directory);
  5690. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5691. if (!SamePaths(Directory, NewDirectory))
  5692. {
  5693. ReloadLocalDirectory(NewDirectory);
  5694. }
  5695. }
  5696. }
  5697. //---------------------------------------------------------------------------
  5698. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5699. {
  5700. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5701. if (Side == osLocal)
  5702. {
  5703. if (Action == TSynchronizeChecklist::saDownloadNew)
  5704. {
  5705. OpenFolderInExplorer(LocalPath);
  5706. }
  5707. else
  5708. {
  5709. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5710. }
  5711. }
  5712. else if (DebugAlwaysTrue(Side == osRemote))
  5713. {
  5714. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5715. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5716. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5717. if (!LocalPath.IsEmpty())
  5718. {
  5719. SessionData->LocalDirectory = LocalPath;
  5720. }
  5721. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5722. ExecuteNewInstance(SessionName, FORMAT(L"%s=\"%s\"", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5723. }
  5724. }
  5725. //---------------------------------------------------------------------------
  5726. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5727. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5728. const TCopyParamType * CopyParams)
  5729. {
  5730. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5731. UnicodeString AdditionalParams =
  5732. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5733. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5734. SerializeCopyParamForCommandLine(CopyParams),
  5735. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5736. LocalDirectory, RemoteDirectory, Mode, Params));
  5737. ExecuteNewInstance(SessionName, AdditionalParams);
  5738. }
  5739. //---------------------------------------------------------------------------
  5740. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5741. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5742. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5743. {
  5744. int Result;
  5745. bool SaveSettings = false;
  5746. int Options =
  5747. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5748. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5749. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5750. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5751. bool Continue =
  5752. (UseDefaults == 0) ||
  5753. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5754. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5755. if (Continue)
  5756. {
  5757. Configuration->Usage->Inc(L"Synchronizations");
  5758. CopyParam.IncludeFileMask.SetRoots(LocalDirectory, RemoteDirectory);
  5759. UpdateCopyParamCounters(CopyParam);
  5760. TSynchronizeOptions SynchronizeOptions;
  5761. GetSynchronizeOptions(Params, SynchronizeOptions);
  5762. if (SaveSettings)
  5763. {
  5764. GUIConfiguration->SynchronizeParams = Params;
  5765. }
  5766. else
  5767. {
  5768. SaveMode = false;
  5769. }
  5770. TDateTime StartTime = Now();
  5771. TSynchronizeChecklist * Checklist = NULL;
  5772. try
  5773. {
  5774. DebugAssert(!FAutoOperation);
  5775. FAutoOperation = true;
  5776. try
  5777. {
  5778. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5779. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5780. int Files = -1;
  5781. if (DataList->Count >= 1)
  5782. {
  5783. Files = StrToIntDef(DataList->Strings[0], Files);
  5784. }
  5785. else
  5786. {
  5787. DataList->Add(UnicodeString());
  5788. }
  5789. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5790. FSynchronizeProgressForm->Start();
  5791. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5792. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5793. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5794. &SynchronizeOptions);
  5795. if (Terminal->SessionData->CacheDirectories)
  5796. {
  5797. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5798. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5799. }
  5800. }
  5801. __finally
  5802. {
  5803. FAutoOperation = false;
  5804. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5805. SAFE_DESTROY(FSynchronizeProgressForm);
  5806. }
  5807. TSynchronizeParams SynchronizeParams;
  5808. SynchronizeParams.LocalDirectory = LocalDirectory;
  5809. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5810. SynchronizeParams.Mode = Mode;
  5811. SynchronizeParams.CopyParam = &CopyParam;
  5812. SynchronizeParams.Params = Params;
  5813. SynchronizeParams.Checklist = Checklist;
  5814. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5815. SynchronizeParams.StartTime = &StartTime;
  5816. SynchronizeParams.OnProcessedItem = NULL;
  5817. Result = Checklist->Count;
  5818. if (Checklist->Count > 0)
  5819. {
  5820. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5821. {
  5822. if (!DoSynchronizeChecklistDialog(
  5823. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5824. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  5825. {
  5826. Result = -1;
  5827. }
  5828. }
  5829. else
  5830. {
  5831. FullSynchronize(SynchronizeParams, NULL, NULL);
  5832. }
  5833. }
  5834. else
  5835. {
  5836. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5837. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5838. }
  5839. }
  5840. __finally
  5841. {
  5842. delete Checklist;
  5843. }
  5844. OperationComplete(StartTime);
  5845. }
  5846. else
  5847. {
  5848. Result = -1;
  5849. }
  5850. return Result;
  5851. }
  5852. //---------------------------------------------------------------------------
  5853. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5854. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5855. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5856. {
  5857. if (Collect)
  5858. {
  5859. DebugAssert(FSynchronizeProgressForm != NULL);
  5860. if (!FSynchronizeProgressForm->Started)
  5861. {
  5862. FSynchronizeProgressForm->Start();
  5863. }
  5864. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5865. SetTaskbarListProgressValue(CompareProgress);
  5866. }
  5867. else
  5868. {
  5869. DebugAssert(FSynchronizeProgressForm == NULL);
  5870. DebugAssert(FProgressForm != NULL);
  5871. }
  5872. }
  5873. //---------------------------------------------------------------------------
  5874. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5875. {
  5876. UnicodeString FullFileName = AbsolutePath(Terminal->CurrentDirectory, FileName);
  5877. TRemoteFile * File = NULL;
  5878. Terminal->ReadFile(FullFileName, File);
  5879. if (File != NULL)
  5880. {
  5881. std::unique_ptr<TRemoteFile> FileOwner(File);
  5882. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5883. Application->ShowMainForm = false;
  5884. Application->Run();
  5885. }
  5886. }
  5887. //---------------------------------------------------------------------------
  5888. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory(TOperationSide)
  5889. {
  5890. DebugFail();
  5891. }
  5892. //---------------------------------------------------------------------------
  5893. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5894. {
  5895. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5896. SessionData->Assign(ManagedSession->SessionData);
  5897. UpdateSessionData(SessionData.get());
  5898. DebugAssert(SessionData->IsLocalBrowser == IsLocalBrowserMode());
  5899. if (IsActiveTerminal(ManagedSession))
  5900. {
  5901. ManagedSession->UpdateSessionCredentials(SessionData.get());
  5902. }
  5903. if (!ManagedSession->SessionData->HasSessionName())
  5904. {
  5905. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5906. // once IsWorkspace is cleared
  5907. SessionData->Name = UnicodeString();
  5908. }
  5909. // Most uses of this method ends with saving the session, and we do not want to store the IsWorkspace flag.
  5910. // Particularly, with SaveHiddenDuplicateSession, it won't work as TSessionData::ParseUrl explicitly ignored workspace sessions.
  5911. SessionData->IsWorkspace = false;
  5912. return SessionData.release();
  5913. }
  5914. //---------------------------------------------------------------------------
  5915. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5916. {
  5917. TSessionData * SessionData = CloneCurrentSessionData();
  5918. try
  5919. {
  5920. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5921. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5922. }
  5923. __finally
  5924. {
  5925. delete SessionData;
  5926. }
  5927. }
  5928. //---------------------------------------------------------------------------
  5929. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5930. {
  5931. TTerminalManager * Manager = TTerminalManager::Instance();
  5932. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5933. if (DebugAlwaysTrue(ManagedSession != NULL))
  5934. {
  5935. // Update ManagedSession->StateData
  5936. UpdateSession(ManagedSession);
  5937. }
  5938. Manager->SaveWorkspace(DataList.get());
  5939. return DataList.release();
  5940. }
  5941. //---------------------------------------------------------------------------
  5942. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5943. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5944. {
  5945. WinConfiguration->LastWorkspace = Name;
  5946. if (!SavePasswords)
  5947. {
  5948. for (int Index = 0; Index < DataList->Count; Index++)
  5949. {
  5950. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5951. if (SessionData->Link.IsEmpty())
  5952. {
  5953. SessionData->ClearSessionPasswords();
  5954. }
  5955. }
  5956. }
  5957. StoredSessions->NewWorkspace(Name, DataList);
  5958. // modified only
  5959. StoredSessions->Save(false, Explicit);
  5960. }
  5961. //---------------------------------------------------------------------------
  5962. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5963. {
  5964. return
  5965. DefaultStr(WinConfiguration->LastWorkspace,
  5966. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5967. }
  5968. //---------------------------------------------------------------------------
  5969. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5970. {
  5971. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5972. bool AnyNonStoredSessionWithPassword = false;
  5973. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5974. bool AllNonStoredSessionsAnonymous = true;
  5975. TTerminalManager * Manager = TTerminalManager::Instance();
  5976. for (int Index = 0; Index < DataList->Count; Index++)
  5977. {
  5978. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5979. if (SessionData->Link.IsEmpty())
  5980. {
  5981. if (SessionData->HasAnySessionPassword())
  5982. {
  5983. AnyNonStoredSessionWithPassword = true;
  5984. if (!Manager->Sessions[Index]->SessionData->IsWorkspace)
  5985. {
  5986. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5987. }
  5988. }
  5989. if (!SameText(SessionData->UserName, AnonymousUserName))
  5990. {
  5991. AllNonStoredSessionsAnonymous = false;
  5992. }
  5993. }
  5994. }
  5995. bool SavePasswords;
  5996. bool * PSavePasswords;
  5997. bool NotRecommendedSavingPasswords =
  5998. !CustomWinConfiguration->UseMasterPassword &&
  5999. !AllNonStoredSessionsAnonymous;
  6000. if (Configuration->DisablePasswordStoring ||
  6001. !AnyNonStoredSessionWithPassword)
  6002. {
  6003. PSavePasswords = NULL;
  6004. SavePasswords = false;
  6005. }
  6006. else
  6007. {
  6008. PSavePasswords = &SavePasswords;
  6009. SavePasswords =
  6010. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  6011. !NotRecommendedSavingPasswords;
  6012. }
  6013. UnicodeString Name = WorkspaceName();
  6014. bool CreateShortcut = false;
  6015. bool Result =
  6016. DoSaveWorkspaceDialog(
  6017. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  6018. EnableAutoSave);
  6019. if (Result)
  6020. {
  6021. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  6022. if (CreateShortcut)
  6023. {
  6024. TOperationVisualizer Visualizer;
  6025. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  6026. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  6027. }
  6028. if (EnableAutoSave)
  6029. {
  6030. WinConfiguration->AutoSaveWorkspace = true;
  6031. WinConfiguration->AutoWorkspace = Name;
  6032. if (PSavePasswords != NULL)
  6033. {
  6034. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  6035. }
  6036. }
  6037. }
  6038. return Result;
  6039. }
  6040. //---------------------------------------------------------------------------
  6041. void __fastcall TCustomScpExplorerForm::UpdateSession(TManagedTerminal * Session)
  6042. {
  6043. DebugAssert(IsLocalBrowserMode() == Session->LocalBrowser);
  6044. DebugAssert(Session == ManagedSession);
  6045. if (!IsLocalBrowserMode())
  6046. {
  6047. SAFE_DESTROY(Session->RemoteExplorerState);
  6048. if (WinConfiguration->PreservePanelState)
  6049. {
  6050. Session->RemoteExplorerState = RemoteDirView->SaveState();
  6051. }
  6052. }
  6053. UpdateSessionData(Session->StateData);
  6054. }
  6055. //---------------------------------------------------------------------------
  6056. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  6057. {
  6058. // Keep in sync with TSessionData::CopyStateData
  6059. DebugAssert(Data != NULL);
  6060. // This is inconsistent with how color (for example) is handled.
  6061. Data->Name = ManagedSession->SessionData->Name;
  6062. if (!IsLocalBrowserMode())
  6063. {
  6064. // cannot use RemoteDirView->Path, because it is empty if connection
  6065. // was already closed
  6066. // also only peek, we may not be connected at all atm
  6067. // so make sure we do not try retrieving current directory from the server
  6068. // (particularly with FTP)
  6069. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  6070. if (!ACurrentDirectory.IsEmpty())
  6071. {
  6072. Data->RemoteDirectory = ACurrentDirectory;
  6073. }
  6074. }
  6075. Data->Color = SessionColor;
  6076. }
  6077. //---------------------------------------------------------------------------
  6078. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  6079. {
  6080. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  6081. DebugAssert(Toolbar != NULL);
  6082. for (int i = 0; i < Toolbar->Items->Count; i++)
  6083. {
  6084. TTBXCustomDropDownItem * DropDownItem;
  6085. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6086. if (DropDownItem != NULL)
  6087. {
  6088. ToolbarItemResize(DropDownItem,
  6089. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  6090. Toolbar->NonClientWidth);
  6091. break;
  6092. }
  6093. }
  6094. }
  6095. //---------------------------------------------------------------------------
  6096. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  6097. {
  6098. Item->EditWidth = Width;
  6099. }
  6100. //---------------------------------------------------------------------------
  6101. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  6102. TTBCustomToolbar * Toolbar, TPoint & ASize)
  6103. {
  6104. for (int i = 0; i < Toolbar->Items->Count; i++)
  6105. {
  6106. TTBXCustomDropDownItem * DropDownItem;
  6107. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6108. if (DropDownItem != NULL)
  6109. {
  6110. ASize.x -= DropDownItem->EditWidth;
  6111. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  6112. }
  6113. }
  6114. }
  6115. //---------------------------------------------------------------------------
  6116. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  6117. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  6118. {
  6119. if (WinConfiguration->DDWarnLackOfTempSpace)
  6120. {
  6121. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  6122. if (!ADrive.IsEmpty())
  6123. {
  6124. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  6125. DebugAssert(RequiredSpace >= 0);
  6126. __int64 RequiredWithReserve;
  6127. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  6128. if (FreeSpace < RequiredWithReserve)
  6129. {
  6130. unsigned int Result;
  6131. TMessageParams Params(mpNeverAskAgainCheck);
  6132. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  6133. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  6134. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  6135. if (Result == qaNeverAskAgain)
  6136. {
  6137. WinConfiguration->DDWarnLackOfTempSpace = false;
  6138. }
  6139. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  6140. }
  6141. }
  6142. }
  6143. }
  6144. //---------------------------------------------------------------------------
  6145. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  6146. {
  6147. DoOpenDirectoryDialog(odAddBookmark, Side);
  6148. }
  6149. //---------------------------------------------------------------------------
  6150. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  6151. {
  6152. // we should better use TCustomDirView::FCaseSensitive, but it is private
  6153. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  6154. try
  6155. {
  6156. TCustomDirView * DView = DirView(Side);
  6157. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  6158. {
  6159. VisitedDirectories->Add(DView->HistoryPath[Index]);
  6160. }
  6161. }
  6162. catch (...)
  6163. {
  6164. delete VisitedDirectories;
  6165. throw;
  6166. }
  6167. return VisitedDirectories;
  6168. }
  6169. //---------------------------------------------------------------------------
  6170. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  6171. TOpenDirectoryMode Mode, TOperationSide Side)
  6172. {
  6173. bool Continue = true;
  6174. if (Mode == odAddBookmark)
  6175. {
  6176. TMessageParams Params(mpNeverAskAgainCheck);
  6177. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  6178. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  6179. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  6180. unsigned int Answer =
  6181. MessageDialog(Message,
  6182. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  6183. if (Answer == qaNeverAskAgain)
  6184. {
  6185. Continue = true;
  6186. WinConfiguration->UseSharedBookmarks = true;
  6187. }
  6188. else if (Answer == qaOK)
  6189. {
  6190. Continue = true;
  6191. WinConfiguration->UseSharedBookmarks = false;
  6192. }
  6193. else
  6194. {
  6195. Continue = false;
  6196. }
  6197. }
  6198. if (Continue)
  6199. {
  6200. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  6201. try
  6202. {
  6203. UnicodeString Name = DirView(Side)->PathName;
  6204. bool Repeat;
  6205. do
  6206. {
  6207. Repeat = false;
  6208. TOperationSide BookmarkSide = IsSideLocalBrowser(Side) ? osLocal : osRemote;
  6209. if (::DoOpenDirectoryDialog(Mode, BookmarkSide, Name, VisitedDirectories, Terminal,
  6210. // do not allow switching to location profiles,
  6211. // if we are not connected
  6212. HasDirView[osLocal] && (Terminal != NULL)))
  6213. {
  6214. TWindowLock Lock(this);
  6215. if (!TryOpenDirectory(Side, Name))
  6216. {
  6217. Repeat = true;
  6218. Mode = odBrowse;
  6219. }
  6220. }
  6221. }
  6222. while (Repeat);
  6223. }
  6224. __finally
  6225. {
  6226. delete VisitedDirectories;
  6227. }
  6228. }
  6229. }
  6230. //---------------------------------------------------------------------------
  6231. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6232. {
  6233. DebugAssert(!Terminal->CommandSessionOpened);
  6234. return TTerminalManager::Instance()->ConnectTerminal(Terminal->CommandSession);
  6235. }
  6236. //---------------------------------------------------------------------------
  6237. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6238. {
  6239. bool Result = Terminal->IsCapable[Capability] ||
  6240. Terminal->CommandSessionOpened;
  6241. if (!Result)
  6242. {
  6243. DebugAssert(Terminal->IsCapable[fcSecondaryShell]);
  6244. if (!GUIConfiguration->ConfirmCommandSession)
  6245. {
  6246. Result = true;
  6247. }
  6248. else
  6249. {
  6250. TMessageParams Params(mpNeverAskAgainCheck);
  6251. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6252. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6253. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6254. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6255. if (Answer == qaNeverAskAgain)
  6256. {
  6257. GUIConfiguration->ConfirmCommandSession = false;
  6258. Result = true;
  6259. }
  6260. else if (Answer == qaOK)
  6261. {
  6262. Result = true;
  6263. }
  6264. }
  6265. if (Result)
  6266. {
  6267. Result = CommandSessionFallback();
  6268. }
  6269. }
  6270. return Result;
  6271. }
  6272. //---------------------------------------------------------------------------
  6273. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6274. {
  6275. if (EnsureCommandSessionFallback(fcAnyCommand))
  6276. {
  6277. DoConsoleDialog(Terminal, Command);
  6278. }
  6279. }
  6280. //---------------------------------------------------------------------------
  6281. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6282. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6283. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6284. {
  6285. if (!IsSideLocalBrowser(osRemote) &&
  6286. IsFileControl(DropSourceControl, osRemote) &&
  6287. (FDDExtMapFile != NULL))
  6288. {
  6289. Accept = true;
  6290. }
  6291. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6292. DebugAssert(FDDTargetControl != NULL);
  6293. }
  6294. //---------------------------------------------------------------------------
  6295. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6296. _di_IDataObject DataObj, int KeyState,
  6297. const TPoint & Point, int & Effect, bool & Accept)
  6298. {
  6299. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6300. }
  6301. //---------------------------------------------------------------------------
  6302. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6303. _di_IDataObject DataObj, int KeyState,
  6304. const TPoint & Point, int & Effect, bool & Accept)
  6305. {
  6306. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6307. }
  6308. //---------------------------------------------------------------------------
  6309. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6310. TObject * Sender)
  6311. {
  6312. DebugUsedParam(Sender);
  6313. DebugAssert(FDDTargetControl == Sender);
  6314. FDDTargetControl = NULL;
  6315. }
  6316. //---------------------------------------------------------------------------
  6317. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  6318. {
  6319. FileControlDDDragLeave(SessionsPageControl);
  6320. }
  6321. //---------------------------------------------------------------------------
  6322. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  6323. {
  6324. FileControlDDDragLeave(QueueView3);
  6325. }
  6326. //---------------------------------------------------------------------------
  6327. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6328. {
  6329. DebugAssert(!IsSideLocalBrowser(Side));
  6330. DebugUsedParam(Side);
  6331. bool Edit = false;
  6332. TRemoteFile * File = NULL;
  6333. UnicodeString FileName;
  6334. UnicodeString PointTo;
  6335. bool SymbolicLink = true;
  6336. if (RemoteDirView->ItemFocused)
  6337. {
  6338. DebugAssert(RemoteDirView->ItemFocused->Data);
  6339. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6340. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6341. if (Edit)
  6342. {
  6343. FileName = File->FileName;
  6344. PointTo = File->LinkTo;
  6345. }
  6346. else
  6347. {
  6348. PointTo = File->FileName;
  6349. }
  6350. }
  6351. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6352. Terminal->IsCapable[fcHardLink]))
  6353. {
  6354. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6355. if (Edit)
  6356. {
  6357. DebugAssert(File->FileName == FileName);
  6358. int Params = dfNoRecursive;
  6359. Terminal->ExceptionOnFail = true;
  6360. try
  6361. {
  6362. Terminal->DeleteFile(L"", File, &Params);
  6363. }
  6364. __finally
  6365. {
  6366. Terminal->ExceptionOnFail = false;
  6367. }
  6368. }
  6369. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6370. }
  6371. }
  6372. //---------------------------------------------------------------------------
  6373. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6374. {
  6375. return
  6376. (IsSideLocalBrowser(Side) ||
  6377. (HasActiveTerminal() &&
  6378. Terminal->ResolvingSymlinks &&
  6379. Terminal->IsCapable[fcSymbolicLink]));
  6380. }
  6381. //---------------------------------------------------------------------------
  6382. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6383. {
  6384. return
  6385. !IsSideLocalBrowser(FCurrentSide) &&
  6386. (RemoteDirView->ItemFocused != NULL) &&
  6387. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6388. Terminal->SessionData->ResolveSymlinks;
  6389. }
  6390. //---------------------------------------------------------------------------
  6391. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6392. {
  6393. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6394. FForceExecution = true;
  6395. try
  6396. {
  6397. DirView(osCurrent)->ExecuteCurrentFile();
  6398. }
  6399. __finally
  6400. {
  6401. FForceExecution = false;
  6402. }
  6403. }
  6404. //---------------------------------------------------------------------------
  6405. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6406. {
  6407. TEditorData ExternalEditor;
  6408. ExternalEditor.Editor = edExternal;
  6409. bool Remember = false;
  6410. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6411. {
  6412. if (Remember)
  6413. {
  6414. TEditorList * EditorList = new TEditorList;
  6415. try
  6416. {
  6417. *EditorList = *WinConfiguration->EditorList;
  6418. bool Found = false;
  6419. int i = 0;
  6420. while (!Found && (i < EditorList->Count))
  6421. {
  6422. const TEditorPreferences * Editor = EditorList->Editors[i];
  6423. if ((Editor->Data->Editor == edExternal) &&
  6424. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6425. {
  6426. Found = true;
  6427. }
  6428. i++;
  6429. }
  6430. if (!Found)
  6431. {
  6432. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6433. WinConfiguration->EditorList = EditorList;
  6434. }
  6435. }
  6436. __finally
  6437. {
  6438. delete EditorList;
  6439. }
  6440. }
  6441. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6442. }
  6443. }
  6444. //---------------------------------------------------------------------------
  6445. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6446. {
  6447. FEditorManager->ProcessFiles(FileTerminalRemoved, ATerminal);
  6448. if (FFileFindTerminal == ATerminal)
  6449. {
  6450. FFileFindTerminal = NULL;
  6451. HideFileFindDialog();
  6452. }
  6453. if (FClipboardTerminal == ATerminal)
  6454. {
  6455. ClipboardClear(); // implies ClipboardStop
  6456. }
  6457. if ((FOnSynchronizeAbort != NULL) &&
  6458. (ATerminal == FManagedSession))
  6459. {
  6460. FOnSynchronizeAbort(NULL);
  6461. }
  6462. }
  6463. //---------------------------------------------------------------------------
  6464. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6465. {
  6466. DetachTerminal(Sender);
  6467. }
  6468. //---------------------------------------------------------------------------
  6469. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6470. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6471. {
  6472. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6473. DebugAssert(Terminal != NULL);
  6474. if (Data->Terminal == Terminal)
  6475. {
  6476. Data->Terminal = NULL;
  6477. }
  6478. }
  6479. //---------------------------------------------------------------------------
  6480. TManagedTerminal * TCustomScpExplorerForm::GetReplacementForLastSession()
  6481. {
  6482. return NULL;
  6483. }
  6484. //---------------------------------------------------------------------------
  6485. void __fastcall TCustomScpExplorerForm::LastTerminalClosed()
  6486. {
  6487. UpdateControls();
  6488. SessionColor = TColor(0);
  6489. UpdateRemotePathComboBox(false);
  6490. NeedSession(false);
  6491. }
  6492. //---------------------------------------------------------------------------
  6493. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6494. {
  6495. try
  6496. {
  6497. // Cache, as the login dialog can change its value.
  6498. // See also DoShow. Also see TTerminalManager::NewSession.
  6499. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6500. try
  6501. {
  6502. if (ShowLogin)
  6503. {
  6504. bool ReloadSessions = !Startup;
  6505. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this, true);
  6506. }
  6507. }
  6508. __finally
  6509. {
  6510. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6511. // (so there was no chance for the user to open any session yet)
  6512. if ((ShowLogin || !Startup) &&
  6513. !WinConfiguration->KeepOpenWhenNoSession &&
  6514. // this rules out the implicit Commander's local-local browser
  6515. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6516. {
  6517. TerminateApplication();
  6518. }
  6519. }
  6520. }
  6521. catch (EAbort &)
  6522. {
  6523. // swallow
  6524. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6525. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6526. }
  6527. }
  6528. //---------------------------------------------------------------------------
  6529. void __fastcall TCustomScpExplorerForm::SessionListChanged(bool ForceTruncationUpdate)
  6530. {
  6531. TTerminalManager * Manager = TTerminalManager::Instance();
  6532. DebugAssert(!Manager->Updating);
  6533. TAutoFlag UpdatingSessionTabsFlag(FUpdatingSessionTabs);
  6534. int ActiveSessionIndex = Manager->ActiveSessionIndex;
  6535. Configuration->Usage->SetMax(L"MaxOpenedSessions", Manager->Count);
  6536. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6537. try
  6538. {
  6539. int PrevTabsWidth = SessionsPageControl->TotalTabsWidth();
  6540. while (SessionsPageControl->PageCount > Manager->Count + 1)
  6541. {
  6542. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6543. }
  6544. for (int Index = 0; Index <= Manager->Count; Index++)
  6545. {
  6546. TThemeTabSheet * TabSheet;
  6547. if (Index >= SessionsPageControl->PageCount)
  6548. {
  6549. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6550. TabSheet->PageControl = SessionsPageControl;
  6551. }
  6552. else
  6553. {
  6554. TabSheet = SessionsPageControl->Pages[Index];
  6555. }
  6556. bool IsSessionTab = (Index < Manager->Count);
  6557. if (IsSessionTab)
  6558. {
  6559. TTerminal * Terminal = Manager->Sessions[Index];
  6560. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6561. TabSheet->CaptionTruncation = tttEllipsis;
  6562. UpdateSessionTab(TabSheet);
  6563. }
  6564. else
  6565. {
  6566. TabSheet->Tag = 0; // not really needed
  6567. TabSheet->Shadowed = false;
  6568. TabSheet->Button = SupportsLocalBrowser() ? ttbDropDown : ttbNone;
  6569. TabSheet->CaptionTruncation = tttNoText;
  6570. // 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)
  6571. UpdateNewTabTab();
  6572. }
  6573. }
  6574. // Prevent flicker on the right side where scrolling buttons would be, unless really needed
  6575. if (ForceTruncationUpdate || (SessionsPageControl->TotalTabsWidth() != PrevTabsWidth))
  6576. {
  6577. SessionsPageControl->UpdateTabsCaptionTruncation();
  6578. }
  6579. }
  6580. __finally
  6581. {
  6582. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6583. }
  6584. SessionsPageControl->ActivePageIndex = ActiveSessionIndex;
  6585. }
  6586. //---------------------------------------------------------------------------
  6587. UnicodeString TCustomScpExplorerForm::GetNewTabTabCaption()
  6588. {
  6589. return StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewTabAction->Caption));
  6590. }
  6591. //---------------------------------------------------------------------------
  6592. void __fastcall TCustomScpExplorerForm::UpdateNewTabTab()
  6593. {
  6594. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6595. UnicodeString TabCaption;
  6596. if (WinConfiguration->SelectiveToolbarText)
  6597. {
  6598. TabCaption = GetNewTabTabCaption();
  6599. }
  6600. TabSheet->BaseCaption = TabCaption;
  6601. TabSheet->ImageIndex = GetNewTabTabImageIndex(osCurrent);
  6602. }
  6603. //---------------------------------------------------------------------------
  6604. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabSession(TTabSheet * TabSheet)
  6605. {
  6606. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6607. }
  6608. //---------------------------------------------------------------------------
  6609. UnicodeString TCustomScpExplorerForm::GetLocalBrowserSessionTitle(TManagedTerminal *)
  6610. {
  6611. DebugFail();
  6612. return UnicodeString();
  6613. }
  6614. //---------------------------------------------------------------------------
  6615. void TCustomScpExplorerForm::UpdateSessionTab(TThemeTabSheet * TabSheet)
  6616. {
  6617. if (DebugAlwaysTrue(TabSheet != NULL))
  6618. {
  6619. TManagedTerminal * ASession = GetSessionTabSession(TabSheet);
  6620. if (DebugAlwaysTrue(ASession != NULL))
  6621. {
  6622. TColor Color = (ASession == Terminal) ? FSessionColor : ASession->StateData->Color;
  6623. if (ASession->LocalBrowser)
  6624. {
  6625. TabSheet->ImageIndex = FLocalBrowserTabImageIndex;
  6626. }
  6627. else
  6628. {
  6629. TabSheet->ImageIndex = AddSessionColor(Color);
  6630. }
  6631. TTerminalManager * Manager = TTerminalManager::Instance();
  6632. TabSheet->Shadowed = !ASession->Active && !ASession->LocalBrowser;
  6633. TabSheet->Button = CanCloseSession(ASession) ? ttbClose : ttbNone;
  6634. TabSheet->BaseCaption = Manager->GetSessionTitle(ASession, true);
  6635. }
  6636. }
  6637. }
  6638. //---------------------------------------------------------------------------
  6639. bool TCustomScpExplorerForm::CanCloseSession(TManagedTerminal * Session)
  6640. {
  6641. return !Session->LocalBrowser || (TTerminalManager::Instance()->Count > 1);
  6642. }
  6643. //---------------------------------------------------------------------------
  6644. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6645. {
  6646. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6647. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->ActivePage);
  6648. bool Result = (Session != NULL);
  6649. if (Result)
  6650. {
  6651. TTerminalManager::Instance()->ActiveSession = Session;
  6652. }
  6653. else
  6654. {
  6655. try
  6656. {
  6657. NewTab();
  6658. }
  6659. __finally
  6660. {
  6661. SessionListChanged();
  6662. }
  6663. FSessionsPageControlNewTabTime = Now();
  6664. }
  6665. return Result;
  6666. }
  6667. //---------------------------------------------------------------------------
  6668. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6669. {
  6670. SessionTabSwitched();
  6671. }
  6672. //---------------------------------------------------------------------------
  6673. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6674. {
  6675. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6676. DebugAssert(TransferList != NULL);
  6677. UnicodeString Name;
  6678. if (TransferList->ItemIndex <= 0)
  6679. {
  6680. Name = L"";
  6681. }
  6682. else
  6683. {
  6684. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6685. }
  6686. if (FCopyParamAutoSelected.IsEmpty())
  6687. {
  6688. // if previous preset was not autoselected, make new preset the "default"
  6689. FCopyParamDefault = Name;
  6690. }
  6691. GUIConfiguration->CopyParamCurrent = Name;
  6692. }
  6693. //---------------------------------------------------------------------------
  6694. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6695. {
  6696. // sanity check
  6697. bool ExistingPreset =
  6698. (FTransferListHoverIndex >= 0) &&
  6699. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6700. DebugAssert(ExistingPreset);
  6701. if (ExistingPreset)
  6702. {
  6703. HDC DC = GetDC(0);
  6704. TCanvas * Canvas = new TCanvas();
  6705. try
  6706. {
  6707. Canvas->Handle = DC;
  6708. Canvas->Font = GetToolbarFont(this);
  6709. UnicodeString Name;
  6710. if (FTransferListHoverIndex == 0)
  6711. {
  6712. Name = L"";
  6713. }
  6714. else
  6715. {
  6716. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6717. }
  6718. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6719. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6720. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6721. static_cast<TObject*>(GetComponent(fcTransferList)));
  6722. UnicodeString InfoStr =
  6723. GUIConfiguration->CopyParamPreset[Name].
  6724. GetInfoStr(L"; ",
  6725. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  6726. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6727. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6728. {
  6729. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6730. {
  6731. InfoStr.SetLength(InfoStr.Length() - 1);
  6732. }
  6733. InfoStr += Ellipsis;
  6734. }
  6735. // UpdateCaption does not cause invalidation of whole submenu, while
  6736. // setting Caption property does.
  6737. // also it probably does not resize the label, even if necessary
  6738. // (we do not want that anyway)
  6739. TransferLabel->UpdateCaption(InfoStr);
  6740. }
  6741. __finally
  6742. {
  6743. Canvas->Handle = NULL;
  6744. ReleaseDC(0, DC);
  6745. delete Canvas;
  6746. }
  6747. }
  6748. }
  6749. //---------------------------------------------------------------------------
  6750. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6751. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6752. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6753. {
  6754. if (FTransferListHoverIndex != AHoverIndex)
  6755. {
  6756. FTransferListHoverIndex = AHoverIndex;
  6757. UpdateTransferLabel();
  6758. }
  6759. }
  6760. //---------------------------------------------------------------------------
  6761. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6762. {
  6763. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6764. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6765. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6766. {
  6767. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6768. {
  6769. if (!NonVisualDataModule->Busy)
  6770. {
  6771. OpenDirectory(GetSide(osCurrent));
  6772. }
  6773. Message.Result = 1;
  6774. }
  6775. else
  6776. {
  6777. TForm::Dispatch(&Message);
  6778. }
  6779. }
  6780. else
  6781. {
  6782. TForm::Dispatch(&Message);
  6783. }
  6784. }
  6785. //---------------------------------------------------------------------------
  6786. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6787. {
  6788. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6789. if ((FIgnoreNextDialogChar != 0) &&
  6790. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6791. {
  6792. Message.Result = 1;
  6793. }
  6794. else
  6795. {
  6796. TForm::Dispatch(&Message);
  6797. }
  6798. FIgnoreNextDialogChar = 0;
  6799. }
  6800. //---------------------------------------------------------------------------
  6801. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6802. {
  6803. // The four low-order bits are used internally by Windows
  6804. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6805. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6806. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6807. // 61730 - restore thru double click - undocumented
  6808. // 61490 - maximize thru double click - undocumented
  6809. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6810. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6811. {
  6812. SysResizing(Cmd);
  6813. }
  6814. TForm::Dispatch(&Message);
  6815. }
  6816. //---------------------------------------------------------------------------
  6817. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6818. {
  6819. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6820. // Also there no point returning TRUE as we are not able to
  6821. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6822. // Hence the process termination might be safer :)
  6823. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6824. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6825. {
  6826. Message.Result = FALSE;
  6827. }
  6828. else
  6829. {
  6830. Message.Result = TRUE;
  6831. }
  6832. // Do not call default handling as that triggers OnCloseQuery,
  6833. // where our implementation will popup confirmation dialogs, what we do not want,
  6834. // as per Vista guidelines:
  6835. // https://docs.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6836. }
  6837. //---------------------------------------------------------------------------
  6838. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6839. {
  6840. if (Message.EndSession && IsApplicationMinimized())
  6841. {
  6842. // WORKAROUND
  6843. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6844. // when it receives WM_ENDSESSION,
  6845. // but that sometimes (particularly when application is minimized) cause crashes.
  6846. // Crash popups message that blocks log off.
  6847. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6848. // so we call ExitProcess() immediately, not even trying to cleanup.
  6849. // It still causes beep, so there's likely some popup, but it does not block
  6850. // log off.
  6851. ExitProcess(0);
  6852. }
  6853. TForm::Dispatch(&Message);
  6854. }
  6855. #ifdef _DEBUG
  6856. //---------------------------------------------------------------------------
  6857. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6858. {
  6859. TForm::Dispatch(&Message);
  6860. }
  6861. #endif
  6862. //---------------------------------------------------------------------------
  6863. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6864. {
  6865. }
  6866. //---------------------------------------------------------------------------
  6867. void __fastcall TCustomScpExplorerForm::DoShow()
  6868. {
  6869. // Called from TCustomScpExplorerForm::CMShowingChanged
  6870. // only now are the controls resized finally, so the size constraints
  6871. // will not conflict with possibly very small window size
  6872. RestoreFormParams();
  6873. // This indicates that the form was rescaled after parameters were loaded and
  6874. // as most probably the parameters were actually saved with the current form scaling
  6875. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6876. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6877. // and no rounding problems will occur.
  6878. // See also RestoreForm.
  6879. // (we should be safe to call RestoreParams unconditionally)
  6880. if (PixelsPerInch != Screen->PixelsPerInch)
  6881. {
  6882. RestoreParams();
  6883. }
  6884. FixControlsPlacement();
  6885. if (Position == poDefaultPosOnly)
  6886. {
  6887. CutFormToDesktop(this);
  6888. }
  6889. TForm::DoShow();
  6890. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6891. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6892. // This was previously in NeedSession, where other related code is.
  6893. // But that is called from CMShowingChanged, when the window is already visible and the local-local window state
  6894. // unpleasantly shows briefly.
  6895. if ((ManagedSession == NULL) &&
  6896. !WinConfiguration->ShowLoginWhenNoSession &&
  6897. WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  6898. // This detects if workspace was saved the last time the main widow was closed
  6899. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  6900. {
  6901. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false, true);
  6902. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  6903. }
  6904. // Before main window displays
  6905. if (ManagedSession == NULL)
  6906. {
  6907. StartingWithoutSession();
  6908. }
  6909. UpdatePixelsPerInchMainWindowCounter();
  6910. FShowing = true;
  6911. }
  6912. //---------------------------------------------------------------------------
  6913. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6914. {
  6915. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6916. }
  6917. //---------------------------------------------------------------------------
  6918. void __fastcall TCustomScpExplorerForm::StartingWithoutSession()
  6919. {
  6920. SessionListChanged();
  6921. InitStatusBar();
  6922. UpdateControls();
  6923. }
  6924. //---------------------------------------------------------------------------
  6925. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6926. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6927. TNotifyEvent OnBalloonClick, TObject * UserData)
  6928. {
  6929. bool Do;
  6930. UnicodeString Message;
  6931. if (E == NULL)
  6932. {
  6933. Message = Str;
  6934. Do = true;
  6935. }
  6936. else
  6937. {
  6938. Do = ExceptionMessage(E, Message);
  6939. }
  6940. if (Do && WinConfiguration->BalloonNotifications)
  6941. {
  6942. UnicodeString Title;
  6943. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6944. {
  6945. Message = TrimLeft(Message);
  6946. }
  6947. else
  6948. {
  6949. Message = UnformatMessage(Message);
  6950. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6951. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6952. Title = LoadResourceString(Captions[Type]);
  6953. if (Terminal != NULL)
  6954. {
  6955. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  6956. Title = TTerminalManager::Instance()->GetSessionTitle(Session, true) + TitleSeparator + Title;
  6957. }
  6958. }
  6959. if (Seconds == 0)
  6960. {
  6961. Seconds = WinConfiguration->NotificationsTimeout;
  6962. }
  6963. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6964. }
  6965. }
  6966. //---------------------------------------------------------------------------
  6967. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6968. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6969. UnicodeString HelpKeyword, const TMessageParams * Params,
  6970. TTerminal * Terminal)
  6971. {
  6972. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6973. (FOnFeedSynchronizeError != NULL)) &&
  6974. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6975. {
  6976. if (FOnFeedSynchronizeError != NULL)
  6977. {
  6978. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6979. }
  6980. else
  6981. {
  6982. DebugAssert(FErrorList != NULL);
  6983. TStringList * MoreMessagesCopy = NULL;
  6984. if (MoreMessages)
  6985. {
  6986. MoreMessagesCopy = new TStringList();
  6987. MoreMessagesCopy->Assign(MoreMessages);
  6988. }
  6989. FErrorList->AddObject(Message, MoreMessagesCopy);
  6990. }
  6991. PopupTrayBalloon(Terminal, Message, Type);
  6992. return ContinueAnswer(Answers);
  6993. }
  6994. else
  6995. {
  6996. bool UseBalloon = FTrayIcon->Visible;
  6997. if (UseBalloon)
  6998. {
  6999. PopupTrayBalloon(Terminal, Message, Type);
  7000. }
  7001. unsigned int Result;
  7002. try
  7003. {
  7004. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  7005. }
  7006. __finally
  7007. {
  7008. // cancel only balloon we popped up, otherwise we may cancel notification
  7009. // balloon that was there before the message dialog
  7010. // (such as "dd confirmation opt in balloon)
  7011. if (UseBalloon)
  7012. {
  7013. FTrayIcon->CancelBalloon();
  7014. }
  7015. }
  7016. return Result;
  7017. }
  7018. }
  7019. //---------------------------------------------------------------------------
  7020. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  7021. TTerminal * Terminal, Exception * E)
  7022. {
  7023. if (FTrayIcon->Visible)
  7024. {
  7025. PopupTrayBalloon(Terminal, L"", qtError, E);
  7026. }
  7027. // particularly prevent opening new session from jump list,
  7028. // while exception is shown
  7029. NonVisualDataModule->StartBusy();
  7030. try
  7031. {
  7032. ShowExtendedExceptionEx(Terminal, E);
  7033. }
  7034. __finally
  7035. {
  7036. NonVisualDataModule->EndBusy();
  7037. FTrayIcon->CancelBalloon();
  7038. }
  7039. }
  7040. //---------------------------------------------------------------------------
  7041. void __fastcall TCustomScpExplorerForm::SessionReady()
  7042. {
  7043. InitStatusBar();
  7044. // cannot rely on active page being page for active terminal,
  7045. // as it can happen that active page is the "new session" page
  7046. // (e.g. when reconnecting active terminal, while login dialog
  7047. // invoked from "new session" page is modal)
  7048. int ActiveSessionIndex = TTerminalManager::Instance()->ActiveSessionIndex;
  7049. UpdateSessionTab(SessionsPageControl->Pages[ActiveSessionIndex]);
  7050. }
  7051. //---------------------------------------------------------------------------
  7052. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  7053. TTerminal * Terminal, Exception * E)
  7054. {
  7055. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  7056. if (!Terminal->Active)
  7057. {
  7058. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  7059. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  7060. {
  7061. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  7062. }
  7063. }
  7064. }
  7065. //---------------------------------------------------------------------------
  7066. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  7067. UnicodeString Message, TQueryType Type,
  7068. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  7069. {
  7070. if ((E == NULL) ||
  7071. ExceptionMessage(E, Message))
  7072. {
  7073. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  7074. if (Important)
  7075. {
  7076. Seconds *= 5;
  7077. }
  7078. UnicodeString NoteMessage(UnformatMessage(Message));
  7079. if (Terminal != NULL)
  7080. {
  7081. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  7082. NoteMessage = FORMAT(L"%s: %s",
  7083. (TTerminalManager::Instance()->GetSessionTitle(Session, true), NoteMessage));
  7084. }
  7085. if (WinConfiguration->BalloonNotifications)
  7086. {
  7087. AddNote(NoteMessage);
  7088. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  7089. }
  7090. else
  7091. {
  7092. FlashOnBackground();
  7093. PostNote(NoteMessage, Seconds, OnClick, UserData);
  7094. }
  7095. }
  7096. }
  7097. //---------------------------------------------------------------------------
  7098. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  7099. {
  7100. RestoreApp();
  7101. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  7102. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  7103. !NonVisualDataModule->Busy)
  7104. {
  7105. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  7106. TTerminalManager::Instance()->ActiveSession = Terminal;
  7107. if (!ComponentVisible[fcQueueView])
  7108. {
  7109. ToggleQueueVisibility();
  7110. GoToQueue();
  7111. }
  7112. }
  7113. }
  7114. //---------------------------------------------------------------------------
  7115. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  7116. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  7117. {
  7118. UnicodeString Message;
  7119. TNotifyEvent OnClick = NULL;
  7120. TObject * UserData = NULL;
  7121. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  7122. switch (Event)
  7123. {
  7124. case qeEmptyButMonitored:
  7125. if ((ATerminal != Terminal) || QueueInvisible)
  7126. {
  7127. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  7128. OnClick = QueueEmptyNoteClicked;
  7129. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  7130. TerminalNoteData->Terminal = ATerminal;
  7131. UserData = TerminalNoteData;
  7132. }
  7133. break;
  7134. case qeEmpty:
  7135. OperationComplete(ATerminal->QueueOperationStart);
  7136. break;
  7137. case qePendingUserAction:
  7138. if ((ATerminal != Terminal) ||
  7139. (QueueInvisible && !IsQueueAutoPopup()))
  7140. {
  7141. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  7142. }
  7143. break;
  7144. default:
  7145. DebugFail();
  7146. }
  7147. if (!Message.IsEmpty())
  7148. {
  7149. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  7150. }
  7151. }
  7152. //---------------------------------------------------------------------------
  7153. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  7154. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  7155. {
  7156. if (FDDExtMapFile != NULL)
  7157. {
  7158. CloseHandle(FDDExtMapFile);
  7159. FDDExtMapFile = NULL;
  7160. }
  7161. if (WinConfiguration->DDFakeFile)
  7162. {
  7163. DDFakeFileInitDrag(FileList, Created);
  7164. }
  7165. }
  7166. //---------------------------------------------------------------------------
  7167. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  7168. {
  7169. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  7170. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  7171. {
  7172. FFakeFileDropTarget = FileName;
  7173. }
  7174. }
  7175. //---------------------------------------------------------------------------
  7176. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  7177. {
  7178. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  7179. if (!ForceDirectories(ApiPath(Result)))
  7180. {
  7181. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  7182. }
  7183. FileSetAttr(ApiPath(Result), faHidden);
  7184. return Result;
  7185. }
  7186. //---------------------------------------------------------------------------
  7187. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  7188. bool & Created)
  7189. {
  7190. FFakeFileDropTarget = UnicodeString();
  7191. FDragFakeDirectory = CreateFakeTransferDirectory();
  7192. FileList->AddItem(NULL, FDragFakeDirectory);
  7193. Created = true;
  7194. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  7195. {
  7196. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  7197. }
  7198. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  7199. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  7200. {
  7201. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7202. TDragExtCommStruct* CommStruct;
  7203. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7204. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7205. DebugAssert(CommStruct != NULL);
  7206. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  7207. CommStruct->Dragging = true;
  7208. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  7209. LENOF(CommStruct->DropDest));
  7210. NULL_TERMINATE(CommStruct->DropDest);
  7211. UnmapViewOfFile(CommStruct);
  7212. }
  7213. }
  7214. //---------------------------------------------------------------------------
  7215. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  7216. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  7217. {
  7218. bool Result;
  7219. DebugAssert(!IsLocalBrowserMode());
  7220. if (Sender == RemoteDirView)
  7221. {
  7222. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  7223. }
  7224. else
  7225. {
  7226. DebugAssert(Sender == RemoteDriveView);
  7227. TStrings * FileList = RemoteDriveView->DragFileList();
  7228. try
  7229. {
  7230. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  7231. }
  7232. __finally
  7233. {
  7234. delete FileList;
  7235. }
  7236. }
  7237. if (FDDTargetControl == RemoteDriveView)
  7238. {
  7239. RemoteDriveView->UpdateDropTarget();
  7240. }
  7241. // foRemoteMove happens when file/dir is dragged within the remote tree view
  7242. // or from tree view to dir view.
  7243. // foMove happens when file/dir is dragged from remote tree view outside of
  7244. // application via dragex
  7245. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  7246. (DropSourceControl == RemoteDriveView))
  7247. {
  7248. RemoteDriveView->UpdateDropSource();
  7249. }
  7250. return Result;
  7251. }
  7252. //---------------------------------------------------------------------------
  7253. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  7254. {
  7255. DebugAssert(!IsLocalBrowserMode());
  7256. // This also handles drops of remote files to queue.
  7257. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  7258. SAFE_DESTROY(FDDFileList);
  7259. if (!FFakeFileDropTarget.IsEmpty())
  7260. {
  7261. RemoveDir(ApiPath(FFakeFileDropTarget));
  7262. }
  7263. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7264. {
  7265. try
  7266. {
  7267. TDragResult DDResult = (Sender == RemoteDirView) ?
  7268. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7269. // Focus is moved to the target application,
  7270. // but as we are going to present the UI, we need to steal the focus back.
  7271. // This most likely won't work though (windows does not allow application
  7272. // to steal focus most of the time)
  7273. Application->BringToFront();
  7274. // On older version of Windows we never got drMove here, see also comment below.
  7275. // On Windows 10, we get the "move".
  7276. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7277. {
  7278. UnicodeString TargetDirectory;
  7279. TFileOperation Operation;
  7280. // drInvalid may mean drMove, see comment below
  7281. switch (DDResult)
  7282. {
  7283. case drCopy:
  7284. Operation = foCopy;
  7285. break;
  7286. case drMove:
  7287. Operation = foMove;
  7288. break;
  7289. default:
  7290. DebugFail();
  7291. case drInvalid:
  7292. // prefer "copy" for safety
  7293. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7294. break;
  7295. }
  7296. TTransferOperationParam Param;
  7297. UnicodeString CounterName;
  7298. bool ForceQueue;
  7299. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7300. {
  7301. // we get drInvalid both if d&d was intercepted by ddext,
  7302. // and when users drops on no-drop location.
  7303. // we tell the difference by existence of response from ddext,
  7304. // so we ignore absence of response in this case
  7305. if (DDResult != drInvalid)
  7306. {
  7307. // here we know that the extension is installed,
  7308. // as it is checked as soon as drag&drop starts from
  7309. // RemoteFileControlDDCreateDragFileList
  7310. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7311. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7312. {
  7313. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7314. }
  7315. else
  7316. {
  7317. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7318. }
  7319. }
  7320. }
  7321. else
  7322. {
  7323. // download using fake file
  7324. Param.Temp = false;
  7325. Param.DragDrop = true;
  7326. if (ForceQueue)
  7327. {
  7328. Param.Queue = asOn;
  7329. }
  7330. if (Sender == RemoteDirView)
  7331. {
  7332. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7333. }
  7334. if (RemoteFileControlFileOperation(Sender, Operation,
  7335. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7336. {
  7337. Configuration->Usage->Inc(CounterName);
  7338. }
  7339. }
  7340. }
  7341. }
  7342. __finally
  7343. {
  7344. CloseHandle(FDDExtMapFile);
  7345. FDDExtMapFile = NULL;
  7346. delete FDragFakeMonitors;
  7347. FDragFakeMonitors = NULL;
  7348. RemoveDir(ApiPath(FDragFakeDirectory));
  7349. FDragFakeDirectory = L"";
  7350. FFakeFileDropTarget = UnicodeString();
  7351. }
  7352. }
  7353. if (FDragDropSshTerminate.get() != NULL)
  7354. {
  7355. FDragDropSshTerminate->Rethrow();
  7356. }
  7357. }
  7358. //---------------------------------------------------------------------------
  7359. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7360. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7361. {
  7362. // Remember drop effect so we know (when user drops files), if we copy or move
  7363. FLastDropEffect = dwEffect;
  7364. }
  7365. //---------------------------------------------------------------------------
  7366. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7367. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7368. {
  7369. bool Result;
  7370. if (FDDTargetControl == QueueView3)
  7371. {
  7372. Directory = DefaultDownloadTargetDirectory();
  7373. Result = true;
  7374. CounterName = L"DownloadsDragDropQueue";
  7375. ForceQueue = true;
  7376. }
  7377. else if (!FFakeFileDropTarget.IsEmpty())
  7378. {
  7379. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7380. Result = true;
  7381. ForceQueue = false;
  7382. CounterName = L"DownloadsDragDropFakeFile";
  7383. }
  7384. else
  7385. {
  7386. ForceQueue = false;
  7387. Enabled = false;
  7388. try
  7389. {
  7390. int Timer = 0;
  7391. Result = false;
  7392. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7393. {
  7394. {
  7395. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7396. TDragExtCommStruct* CommStruct;
  7397. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7398. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7399. DebugAssert(CommStruct != NULL);
  7400. Result = !CommStruct->Dragging;
  7401. if (Result)
  7402. {
  7403. Directory = ExtractFilePath(CommStruct->DropDest);
  7404. CounterName = L"DownloadsDragDropExternalExt";
  7405. }
  7406. UnmapViewOfFile(CommStruct);
  7407. }
  7408. if (!Result)
  7409. {
  7410. Sleep(50);
  7411. Timer += 50;
  7412. Application->ProcessMessages();
  7413. }
  7414. }
  7415. }
  7416. __finally
  7417. {
  7418. Enabled = true;
  7419. }
  7420. }
  7421. return Result;
  7422. }
  7423. //---------------------------------------------------------------------------
  7424. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7425. const UnicodeString TempDir, int SecDelay)
  7426. {
  7427. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7428. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7429. if (FDelayedDeletionTimer == NULL)
  7430. {
  7431. DebugAssert(HandleAllocated());
  7432. FDelayedDeletionTimer = new TTimer(this);
  7433. FDelayedDeletionTimer->Interval = 10000;
  7434. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7435. }
  7436. else
  7437. {
  7438. FDelayedDeletionTimer->Enabled = true;
  7439. }
  7440. }
  7441. //---------------------------------------------------------------------------
  7442. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7443. {
  7444. DebugAssert(FDelayedDeletionList != NULL);
  7445. TDateTime N = Now();
  7446. TDateTime Alert;
  7447. UnicodeString Directory;
  7448. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7449. {
  7450. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7451. if ((N >= Alert) || (Sender == NULL))
  7452. {
  7453. Directory = FDelayedDeletionList->Strings[Index];
  7454. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7455. {
  7456. FDelayedDeletionList->Delete(Index);
  7457. }
  7458. }
  7459. }
  7460. if (FDelayedDeletionList->Count == 0)
  7461. {
  7462. FDelayedDeletionTimer->Enabled = false;
  7463. }
  7464. }
  7465. //---------------------------------------------------------------------------
  7466. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7467. {
  7468. DebugAssert(!IsLocalBrowserMode());
  7469. if (IsFileControl(FDDTargetControl, osRemote) ||
  7470. (FDDTargetControl == SessionsPageControl))
  7471. {
  7472. TManagedTerminal * TargetTerminal = NULL;
  7473. TFileOperation Operation = foNone;
  7474. if (FDDTargetControl == SessionsPageControl)
  7475. {
  7476. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7477. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7478. // do not allow dropping on local-local, disconnected and the "+" tab (see also SessionsDDProcessDropped)
  7479. TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  7480. if (IsActiveTerminal(TargetTerminal) && DebugAlwaysTrue(!TargetTerminal->LocalBrowser))
  7481. {
  7482. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7483. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7484. {
  7485. Operation = foRemoteMove;
  7486. }
  7487. else
  7488. {
  7489. Operation = foRemoteCopy;
  7490. }
  7491. }
  7492. }
  7493. else
  7494. {
  7495. if (FLastDropEffect == DROPEFFECT_MOVE)
  7496. {
  7497. Operation = foRemoteMove;
  7498. }
  7499. else if (FLastDropEffect == DROPEFFECT_COPY)
  7500. {
  7501. Operation = foRemoteCopy;
  7502. }
  7503. }
  7504. if (Operation != foNone)
  7505. {
  7506. RemoteFileControlFileOperation(DropSourceControl,
  7507. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7508. }
  7509. // abort drag&drop
  7510. Abort();
  7511. }
  7512. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7513. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7514. (FDDTargetControl != QueueView3))
  7515. {
  7516. DebugAssert(!FDragTempDir.IsEmpty());
  7517. TTransferType Type;
  7518. UnicodeString TempDir = FDragTempDir;
  7519. // We clear FDragTempDir before calling
  7520. // just in case it fail (raises exception)
  7521. FDragTempDir = L"";
  7522. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7523. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7524. // empty directory parameter means temp directory -> don't display it!
  7525. UnicodeString TargetDir = L"";
  7526. int Options = 0;
  7527. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7528. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7529. {
  7530. Abort();
  7531. }
  7532. // TargetDir is set when dropped on local file control
  7533. // (this was workaround for legacy dirview event handling, now it should be
  7534. // made prettier)
  7535. if (TargetDir.IsEmpty())
  7536. {
  7537. TargetDir = TempDir;
  7538. if (ForceDirectories(ApiPath(TargetDir)))
  7539. {
  7540. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7541. FPendingTempSpaceWarn = true;
  7542. try
  7543. {
  7544. FDragDropOperation = true;
  7545. int Params = cpTemporary |
  7546. (Type == ttMove ? cpDelete : 0);
  7547. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7548. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7549. }
  7550. __finally
  7551. {
  7552. FDragDropOperation = false;
  7553. FPendingTempSpaceWarn = false;
  7554. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7555. }
  7556. }
  7557. else
  7558. {
  7559. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7560. }
  7561. }
  7562. }
  7563. }
  7564. //---------------------------------------------------------------------------
  7565. void __fastcall TCustomScpExplorerForm::DDDownload(
  7566. TStrings * FilesToCopy, const UnicodeString & TargetDir, TCopyParamType * CopyParam, int Params)
  7567. {
  7568. DebugAssert(!IsLocalBrowserMode());
  7569. TAutoBatch AutoBatch(this);
  7570. CopyParam->IncludeFileMask.SetRoots(TargetDir, FilesToCopy);
  7571. UpdateCopyParamCounters(*CopyParam);
  7572. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7573. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7574. {
  7575. RemoteDriveView->UpdateDropSource();
  7576. }
  7577. }
  7578. //---------------------------------------------------------------------------
  7579. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7580. {
  7581. public:
  7582. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7583. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7584. {
  7585. }
  7586. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7587. {
  7588. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7589. TDataObjectFilesEx::AllowData(FormatEtc);
  7590. }
  7591. };
  7592. //---------------------------------------------------------------------------
  7593. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7594. TObject * Sender, TDataObject *& DataObject)
  7595. {
  7596. if (FDDExtMapFile != NULL)
  7597. {
  7598. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7599. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7600. {
  7601. Abort();
  7602. }
  7603. if (FileList->Count > 0)
  7604. {
  7605. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7606. if (!FilesObject->IsValid(true, true))
  7607. {
  7608. FilesObject->_Release();
  7609. }
  7610. else
  7611. {
  7612. DataObject = FilesObject;
  7613. }
  7614. }
  7615. }
  7616. }
  7617. //---------------------------------------------------------------------------
  7618. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7619. {
  7620. DebugFail();
  7621. }
  7622. //---------------------------------------------------------------------------
  7623. void __fastcall TCustomScpExplorerForm::GoToTree()
  7624. {
  7625. ComponentVisible[fcRemoteTree] = true;
  7626. DriveView(osRemote)->SetFocus();
  7627. }
  7628. //---------------------------------------------------------------------------
  7629. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7630. TPanelExport Export)
  7631. {
  7632. TCustomDirView * DirView = this->DirView(Side);
  7633. std::unique_ptr<TStrings> ExportData(new TStringList());
  7634. switch (Export)
  7635. {
  7636. case pePath:
  7637. ExportData->Add(DirView->PathName);
  7638. break;
  7639. case peFileList:
  7640. case peFullFileList:
  7641. {
  7642. bool FullPath = (Export == peFullFileList);
  7643. DirView->CreateFileList(false, FullPath, ExportData.get());
  7644. for (int Index = 0; Index < ExportData->Count; Index++)
  7645. {
  7646. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7647. {
  7648. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7649. }
  7650. }
  7651. }
  7652. break;
  7653. default:
  7654. DebugFail();
  7655. }
  7656. return ExportData.release();
  7657. }
  7658. //---------------------------------------------------------------------------
  7659. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7660. TPanelExport Export, TPanelExportDestination Destination)
  7661. {
  7662. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7663. PanelExportStore(Side, Export, Destination, ExportData.get());
  7664. }
  7665. //---------------------------------------------------------------------------
  7666. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7667. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7668. TStrings * ExportData)
  7669. {
  7670. if (Destination == pedClipboard)
  7671. {
  7672. TInstantOperationVisualizer Visualizer;
  7673. CopyToClipboard(ExportData);
  7674. }
  7675. else
  7676. {
  7677. DebugFail();
  7678. }
  7679. }
  7680. //---------------------------------------------------------------------------
  7681. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7682. {
  7683. TCustomDirView * DirView = this->DirView(Side);
  7684. UnicodeString Mask = DirView->Mask;
  7685. if (DoFilterMaskDialog(DirView, Mask))
  7686. {
  7687. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7688. Configuration->Usage->Inc(L"Filters");
  7689. }
  7690. }
  7691. //---------------------------------------------------------------------------
  7692. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7693. {
  7694. return FQueueController->DefaultOperation();
  7695. }
  7696. //---------------------------------------------------------------------------
  7697. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7698. TQueueOperation Operation, void ** Param)
  7699. {
  7700. switch (Operation)
  7701. {
  7702. case qoPreferences:
  7703. return true;
  7704. case qoGoTo:
  7705. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7706. case qoOnceEmpty:
  7707. return IsAnythingQueued();
  7708. default:
  7709. return FQueueController->AllowOperation(Operation, Param);
  7710. }
  7711. }
  7712. //---------------------------------------------------------------------------
  7713. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7714. {
  7715. if (DebugAlwaysTrue(QueueView3->Visible))
  7716. {
  7717. QueueView3->SetFocus();
  7718. }
  7719. }
  7720. //---------------------------------------------------------------------------
  7721. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7722. TQueueOperation Operation, void * Param)
  7723. {
  7724. if (Operation == qoGoTo)
  7725. {
  7726. GoToQueue();
  7727. }
  7728. else if (Operation == qoPreferences)
  7729. {
  7730. PreferencesDialog(pmQueue);
  7731. }
  7732. else
  7733. {
  7734. FQueueController->ExecuteOperation(Operation, Param);
  7735. }
  7736. }
  7737. //---------------------------------------------------------------------------
  7738. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7739. {
  7740. return (Queue != NULL) && Queue->Enabled;
  7741. }
  7742. //---------------------------------------------------------------------------
  7743. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7744. {
  7745. DebugAssert(Queue != NULL);
  7746. if (Queue != NULL)
  7747. {
  7748. Queue->Enabled = !Queue->Enabled;
  7749. }
  7750. }
  7751. //---------------------------------------------------------------------------
  7752. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7753. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7754. {
  7755. FQueueActedItem = QueueView3->ItemFocused;
  7756. }
  7757. //---------------------------------------------------------------------------
  7758. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7759. {
  7760. return TopDock->Height + QueueSplitter->Height;
  7761. }
  7762. //---------------------------------------------------------------------------
  7763. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  7764. {
  7765. return
  7766. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  7767. (QueueDock->Visible ? QueueDock->Height : 0) +
  7768. (QueueLabel->Visible ? QueueLabel->Height : 0);
  7769. }
  7770. //---------------------------------------------------------------------------
  7771. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  7772. {
  7773. return ScaleByTextHeight(this, 48);
  7774. }
  7775. //---------------------------------------------------------------------------
  7776. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7777. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7778. {
  7779. // when queue is hidden by double-clicking splitter, stray attempt to
  7780. // resize the panel with strange value arrives, make sure it is ignored
  7781. if (ComponentVisible[fcQueueView])
  7782. {
  7783. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  7784. if (NewSize > MaxHeightLimit)
  7785. {
  7786. NewSize = MaxHeightLimit;
  7787. }
  7788. int MinHeightLimit =
  7789. GetStaticQueuePanelComponentsHeight() +
  7790. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  7791. GetMinQueueViewHeight();
  7792. if (NewSize < MinHeightLimit)
  7793. {
  7794. NewSize = MinHeightLimit;
  7795. }
  7796. }
  7797. else
  7798. {
  7799. Accept = false;
  7800. }
  7801. }
  7802. //---------------------------------------------------------------------------
  7803. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  7804. {
  7805. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  7806. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  7807. if (QueueViewHeight < GetMinQueueViewHeight())
  7808. {
  7809. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  7810. }
  7811. }
  7812. //---------------------------------------------------------------------------
  7813. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7814. TDragObject *& /*DragObject*/)
  7815. {
  7816. FQueueActedItem = QueueView3->ItemFocused;
  7817. }
  7818. //---------------------------------------------------------------------------
  7819. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7820. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7821. {
  7822. Accept = true;
  7823. if (Source == QueueView3)
  7824. {
  7825. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7826. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7827. if (Accept)
  7828. {
  7829. TQueueItemProxy * QueueItem;
  7830. TQueueItemProxy * DestQueueItem;
  7831. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7832. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7833. Accept = (QueueItem != DestQueueItem) &&
  7834. (QueueItem->Status == TQueueItem::qsPending) &&
  7835. (DestQueueItem->Status == TQueueItem::qsPending);
  7836. }
  7837. }
  7838. }
  7839. //---------------------------------------------------------------------------
  7840. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7841. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7842. {
  7843. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7844. {
  7845. TQueueItemProxy * QueueItem;
  7846. TQueueItemProxy * DestQueueItem;
  7847. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7848. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7849. QueueItem->Move(DestQueueItem);
  7850. }
  7851. }
  7852. //---------------------------------------------------------------------------
  7853. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7854. {
  7855. if ((QueueView3->ItemFocused == NULL) &&
  7856. (QueueView3->Items->Count > 0))
  7857. {
  7858. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7859. }
  7860. QueueLabelUpdateStatus();
  7861. }
  7862. //---------------------------------------------------------------------------
  7863. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7864. {
  7865. QueueLabelUpdateStatus();
  7866. }
  7867. //---------------------------------------------------------------------------
  7868. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7869. {
  7870. QueueLabel->UpdateStatus();
  7871. }
  7872. //---------------------------------------------------------------------------
  7873. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7874. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7875. {
  7876. if (Selected)
  7877. {
  7878. NonVisualDataModule->UpdateNonVisibleActions();
  7879. }
  7880. }
  7881. //---------------------------------------------------------------------------
  7882. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7883. {
  7884. TDragDropFilesEx * Result = NULL;
  7885. if (Sender == SessionsPageControl)
  7886. {
  7887. Result = FSessionsDragDropFilesEx;
  7888. }
  7889. else if (Sender == QueueView3)
  7890. {
  7891. Result = FQueueDragDropFilesEx;
  7892. }
  7893. else
  7894. {
  7895. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7896. if (DirView != NULL)
  7897. {
  7898. Result = DirView->DragDropFilesEx;
  7899. }
  7900. else
  7901. {
  7902. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7903. if (DriveView != NULL)
  7904. {
  7905. Result = DriveView->DragDropFilesEx;
  7906. }
  7907. }
  7908. }
  7909. DebugAssert(Result != NULL);
  7910. return Result;
  7911. }
  7912. //---------------------------------------------------------------------------
  7913. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7914. {
  7915. return (DView->SelCount == DView->FilesCount);
  7916. }
  7917. //---------------------------------------------------------------------------
  7918. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7919. {
  7920. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7921. }
  7922. //---------------------------------------------------------------------------
  7923. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7924. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7925. {
  7926. DebugAssert(!IsLocalBrowserMode());
  7927. TFileOperation Operation;
  7928. switch (Effect)
  7929. {
  7930. case DROPEFFECT_MOVE:
  7931. Operation = foMove;
  7932. break;
  7933. case DROPEFFECT_COPY:
  7934. // occures on WinXP (reported by user)
  7935. default:
  7936. Operation = foCopy;
  7937. break;
  7938. };
  7939. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7940. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7941. if (DragDropFilesEx->FileList->Count > 0)
  7942. {
  7943. TStrings * FileList = new TStringList();
  7944. try
  7945. {
  7946. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7947. {
  7948. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7949. }
  7950. FDragDropOperation = true;
  7951. TTransferOperationParam Param;
  7952. Param.TargetDirectory = TargetPath;
  7953. // upload, no temp dirs
  7954. Param.Temp = false;
  7955. Param.DragDrop = DragDrop;
  7956. Param.Options =
  7957. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7958. if (ForceQueue)
  7959. {
  7960. Param.Queue = asOn;
  7961. }
  7962. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7963. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7964. {
  7965. if (IsFileControl(DropSourceControl, osLocal))
  7966. {
  7967. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7968. }
  7969. else
  7970. {
  7971. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7972. }
  7973. }
  7974. }
  7975. __finally
  7976. {
  7977. FDragDropOperation = false;
  7978. delete FileList;
  7979. }
  7980. }
  7981. }
  7982. //---------------------------------------------------------------------------
  7983. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7984. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7985. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7986. {
  7987. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7988. }
  7989. //---------------------------------------------------------------------------
  7990. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7991. TObject * Sender, int grfKeyState, int & dwEffect)
  7992. {
  7993. DebugAssert(!IsLocalBrowserMode());
  7994. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7995. if ((dwEffect != DROPEFFECT_NONE) &&
  7996. IsFileControl(DropSourceControl, osRemote))
  7997. {
  7998. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7999. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  8000. (RemoteDirView->DropTarget == NULL))
  8001. {
  8002. dwEffect = DROPEFFECT_NONE;
  8003. }
  8004. else
  8005. {
  8006. if (dwEffect == DROPEFFECT_COPY)
  8007. {
  8008. bool MoveCapable = Terminal->IsCapable[fcRemoteMove];
  8009. // currently we support copying always (at least via temporary directory);
  8010. // remove associated checks once this all proves stable and working
  8011. bool CopyCapable = true;
  8012. // if we do not support neither of operations, there's no discussion
  8013. if (!MoveCapable && !CopyCapable)
  8014. {
  8015. dwEffect = DROPEFFECT_NONE;
  8016. }
  8017. else
  8018. {
  8019. // The default effect inside remote panel is move,
  8020. // unless we do not support it, but support copy
  8021. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  8022. {
  8023. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  8024. }
  8025. else
  8026. {
  8027. // with ctrl-down, we want copy unless it is not supported
  8028. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  8029. }
  8030. }
  8031. }
  8032. }
  8033. }
  8034. }
  8035. //---------------------------------------------------------------------------
  8036. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  8037. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  8038. {
  8039. DebugAssert(!IsLocalBrowserMode());
  8040. if (FDDTotalSize >= 0)
  8041. {
  8042. if (File->IsDirectory)
  8043. {
  8044. FDDTotalSize = -1;
  8045. }
  8046. else
  8047. {
  8048. FDDTotalSize += File->Size;
  8049. }
  8050. }
  8051. DebugAssert(!FDragTempDir.IsEmpty());
  8052. // TODO: this is quite ineffective
  8053. // TODO: what if invalid character replacement is disabled?
  8054. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  8055. UnicodeString TransferFileName;
  8056. if (Sender == RemoteDriveView)
  8057. {
  8058. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  8059. }
  8060. else
  8061. {
  8062. TransferFileName = File->FileName;
  8063. }
  8064. FDDFileList->AddObject(TransferFileName, File);
  8065. }
  8066. //---------------------------------------------------------------------------
  8067. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  8068. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  8069. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  8070. {
  8071. // sometimes we do not get DDEnd so the list is not released
  8072. SAFE_DESTROY(FDDFileList);
  8073. FDDFileList = new TStringList();
  8074. FDragTempDir = WinConfiguration->TemporaryDir();
  8075. FDDTotalSize = 0;
  8076. FDragDropSshTerminate.reset(NULL);
  8077. }
  8078. //---------------------------------------------------------------------------
  8079. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  8080. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  8081. HRESULT & Result)
  8082. {
  8083. if (Result == DRAGDROP_S_DROP)
  8084. {
  8085. try
  8086. {
  8087. GlobalDragImageList->HideDragImage();
  8088. try
  8089. {
  8090. RemoteFileControlDDTargetDrop();
  8091. }
  8092. catch(ESshTerminate & E)
  8093. {
  8094. FDragDropSshTerminate.reset(E.Clone());
  8095. }
  8096. }
  8097. catch (Exception &E)
  8098. {
  8099. // If downloading fails we need to cancel drag&drop, otherwise
  8100. // Explorer shows error
  8101. // But by the way exception probably never reach this point as
  8102. // it's catched on way.
  8103. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  8104. Result = DRAGDROP_S_CANCEL;
  8105. ShowExtendedException(Terminal, &E);
  8106. }
  8107. }
  8108. }
  8109. //---------------------------------------------------------------------------
  8110. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  8111. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  8112. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  8113. {
  8114. TFileMasks::TParams MaskParams;
  8115. MaskParams.Size = Size;
  8116. MaskParams.Modification = Modification;
  8117. // this does not re-parse the mask if it is the same as the last time
  8118. FDirViewMatchMask = Masks;
  8119. bool ImplicitMatch;
  8120. Matches =
  8121. // Local and RecurseInclude parameters have no effect as there's no path.
  8122. // This should use potential MatchesFileName overload with ImplicitMatch argument.
  8123. FDirViewMatchMask.Matches(FileName, false, Directory, &MaskParams, true, ImplicitMatch) &&
  8124. (AllowImplicitMatches || !ImplicitMatch);
  8125. }
  8126. //---------------------------------------------------------------------------
  8127. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  8128. TObject * Sender, TListItem * Item, WORD & Indexes)
  8129. {
  8130. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8131. UnicodeString Ext;
  8132. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  8133. {
  8134. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  8135. }
  8136. else
  8137. {
  8138. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  8139. }
  8140. if (SameText(Ext, Configuration->PartialExt))
  8141. {
  8142. Indexes |= oiPartial;
  8143. }
  8144. }
  8145. //---------------------------------------------------------------------------
  8146. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  8147. {
  8148. return
  8149. DirViewEnabled(osCurrent) &&
  8150. DirView(osCurrent)->CanPasteFromClipBoard();
  8151. }
  8152. //---------------------------------------------------------------------------
  8153. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  8154. {
  8155. bool Result = false;
  8156. if (CanPasteToDirViewFromClipBoard())
  8157. {
  8158. Result = true;
  8159. }
  8160. else
  8161. {
  8162. UnicodeString ClipboardText;
  8163. if (NonEmptyTextFromClipboard(ClipboardText) &&
  8164. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  8165. {
  8166. if (StoredSessions->IsUrl(ClipboardText))
  8167. {
  8168. Result = true;
  8169. }
  8170. else
  8171. {
  8172. Result = DirViewEnabled(osCurrent);
  8173. }
  8174. }
  8175. }
  8176. return Result;
  8177. }
  8178. //---------------------------------------------------------------------------
  8179. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  8180. {
  8181. // Pasting to any local panel is handled in TScpCommanderForm::PasteFromClipBoard()
  8182. if (DoesClipboardContainOurFiles())
  8183. {
  8184. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  8185. {
  8186. DebugAssert(!IsLocalBrowserMode());
  8187. TTerminalManager * Manager = TTerminalManager::Instance();
  8188. TTerminal * TargetTerminal = Manager->ActiveTerminal;
  8189. Manager->ActiveSession = FClipboardTerminal;
  8190. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TargetTerminal);
  8191. }
  8192. }
  8193. else if (CanPasteToDirViewFromClipBoard())
  8194. {
  8195. DirView(osCurrent)->PasteFromClipBoard();
  8196. }
  8197. else
  8198. {
  8199. UnicodeString ClipboardText;
  8200. if (NonEmptyTextFromClipboard(ClipboardText))
  8201. {
  8202. if (StoredSessions->IsUrl(ClipboardText))
  8203. {
  8204. NewSession(ClipboardText);
  8205. }
  8206. else
  8207. {
  8208. DirView(osCurrent)->Path = ClipboardText;
  8209. }
  8210. }
  8211. }
  8212. }
  8213. //---------------------------------------------------------------------------
  8214. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  8215. {
  8216. // TBD
  8217. }
  8218. //---------------------------------------------------------------------------
  8219. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  8220. {
  8221. TCustomDirView * ADirView = DirView(Side);
  8222. ADirView->SelectAll(Mode);
  8223. ADirView->SetFocus();
  8224. }
  8225. //---------------------------------------------------------------------------
  8226. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  8227. {
  8228. TCustomDirView * ADirView = DirView(Side);
  8229. TFileFilter Filter;
  8230. DefaultFileFilter(Filter);
  8231. if (DoSelectMaskDialog(ADirView, Select, Filter))
  8232. {
  8233. Configuration->Usage->Inc(L"MaskSelections");
  8234. ADirView->SelectFiles(Filter, Select);
  8235. ADirView->SetFocus();
  8236. }
  8237. }
  8238. //---------------------------------------------------------------------------
  8239. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  8240. {
  8241. TCustomDirView * ADirView = DirView(Side);
  8242. ADirView->RestoreSelectedNames();
  8243. ADirView->SetFocus();
  8244. }
  8245. //---------------------------------------------------------------------------
  8246. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  8247. {
  8248. TCustomDirView * CurrentDirView = DirView(osCurrent);
  8249. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  8250. {
  8251. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  8252. UnicodeString Ext;
  8253. if (!IsSideLocalBrowser(osCurrent))
  8254. {
  8255. Ext = UnixExtractFileExt(FileName);
  8256. }
  8257. else
  8258. {
  8259. Ext = ExtractFileExt(FileName);
  8260. }
  8261. if (Ext.IsEmpty())
  8262. {
  8263. Ext = L".";
  8264. }
  8265. TFileFilter Filter;
  8266. Filter.Masks = FORMAT(L"*%s", (Ext));
  8267. Filter.Directories = false;
  8268. CurrentDirView->SelectFiles(Filter, Select);
  8269. }
  8270. }
  8271. //---------------------------------------------------------------------------
  8272. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8273. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8274. {
  8275. UnicodeString Result;
  8276. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  8277. {
  8278. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  8279. }
  8280. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8281. {
  8282. // noop
  8283. }
  8284. else
  8285. {
  8286. Result =
  8287. FMTLOAD(FILE_INFO_FORMAT,
  8288. (FormatBytes(FileInfo.SelectedSize),
  8289. FormatBytes(FileInfo.FilesSize),
  8290. FormatNumber(FileInfo.SelectedCount),
  8291. FormatNumber(FileInfo.FilesCount)));
  8292. }
  8293. return Result;
  8294. }
  8295. //---------------------------------------------------------------------------
  8296. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8297. TTBXStatusPanel * Panel, TOperationSide Side)
  8298. {
  8299. if (Panel->Index == 1)
  8300. {
  8301. ToggleShowHiddenFiles();
  8302. }
  8303. else if (Panel->Index == 2)
  8304. {
  8305. Filter(Side);
  8306. }
  8307. }
  8308. //---------------------------------------------------------------------------
  8309. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8310. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8311. {
  8312. DebugAssert(!StatusBar->SimplePanel);
  8313. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8314. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8315. }
  8316. //---------------------------------------------------------------------------
  8317. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8318. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8319. {
  8320. DebugAssert(StatusBar->Panels->Count >= 3);
  8321. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8322. if (FileInfo.HiddenCount > 0)
  8323. {
  8324. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8325. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8326. }
  8327. else
  8328. {
  8329. HiddenFilesPanel->ViewPriority = 0;
  8330. // not really necessary, just to cleanup no-longer-valid data
  8331. HiddenFilesPanel->Caption = L"";
  8332. }
  8333. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8334. if (FileInfo.FilteredCount > 0)
  8335. {
  8336. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8337. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8338. }
  8339. else
  8340. {
  8341. FilteredFilesPanel->ViewPriority = 0;
  8342. // not really necessary, just to cleanup no-longer-valid data
  8343. FilteredFilesPanel->Caption = L"";
  8344. }
  8345. }
  8346. //---------------------------------------------------------------------------
  8347. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8348. TObject * /*Sender*/)
  8349. {
  8350. if (DirView(osOther)->Enabled)
  8351. {
  8352. DirView(osOther)->SetFocus();
  8353. }
  8354. }
  8355. //---------------------------------------------------------------------------
  8356. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8357. {
  8358. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8359. switch (Config.Show)
  8360. {
  8361. case qvShow:
  8362. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8363. {
  8364. Config.Show = qvHide;
  8365. }
  8366. else
  8367. {
  8368. Config.Show = Config.LastHideShow;
  8369. }
  8370. break;
  8371. case qvHideWhenEmpty:
  8372. if (ComponentVisible[fcQueueView])
  8373. {
  8374. Config.Show = qvHide;
  8375. }
  8376. else
  8377. {
  8378. Config.LastHideShow = Config.Show;
  8379. Config.Show = qvShow;
  8380. }
  8381. break;
  8382. case qvHide:
  8383. Config.LastHideShow = Config.Show;
  8384. Config.Show = qvShow;
  8385. break;
  8386. }
  8387. WinConfiguration->QueueView = Config;
  8388. }
  8389. //---------------------------------------------------------------------------
  8390. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8391. {
  8392. UnicodeString Result;
  8393. if (Terminal != NULL)
  8394. {
  8395. switch (WinConfiguration->PathInCaption)
  8396. {
  8397. case picShort:
  8398. Result = ExtractShortName(Terminal->CurrentDirectory, true);
  8399. break;
  8400. case picFull:
  8401. Result = Terminal->CurrentDirectory;
  8402. break;
  8403. }
  8404. }
  8405. return Result;
  8406. }
  8407. //---------------------------------------------------------------------------
  8408. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8409. {
  8410. TColor Result = GetWindowColor(FSessionColor);
  8411. return Result;
  8412. }
  8413. //---------------------------------------------------------------------------
  8414. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8415. {
  8416. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8417. return Result;
  8418. }
  8419. //---------------------------------------------------------------------------
  8420. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8421. {
  8422. if (!FSessionChanging)
  8423. {
  8424. TTerminalManager::Instance()->UpdateAppTitle();
  8425. }
  8426. // WORKAROUND: Disabling list view when it is not showing yet does not set its
  8427. // background to gray on Windows 7 (works on Windows 10).
  8428. // See also EnableControl
  8429. if (Showing)
  8430. {
  8431. if (ManagedSession != NULL)
  8432. {
  8433. // Update path when it changes
  8434. if ((SessionsPageControl->ActivePage != NULL) &&
  8435. (GetSessionTabSession(SessionsPageControl->ActivePage) == ManagedSession))
  8436. {
  8437. UpdateSessionTab(SessionsPageControl->ActivePage);
  8438. }
  8439. }
  8440. NonVisualDataModule->ReconnectSessionAction->Update();
  8441. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8442. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8443. CenterReconnectToolbar();
  8444. bool HasTerminal = HasActiveTerminal();
  8445. RemoteDirView->Enabled = HasTerminal;
  8446. RemoteDirView->Color = HasTerminal ? PanelColor() : DisabledPanelColor();
  8447. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8448. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8449. RemoteDriveView->Color = RemoteDirView->Color;
  8450. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8451. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8452. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8453. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8454. QueueFileList->Enabled = QueueView3->Enabled;
  8455. QueueFileList->Color = QueueView3->Color;
  8456. QueueFileList->Font->Color = QueueView3->Font->Color;
  8457. QueueLabelUpdateStatus();
  8458. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8459. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8460. if (FImmersiveDarkMode != WinConfiguration->UseDarkTheme())
  8461. {
  8462. UpdateDarkMode();
  8463. }
  8464. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal || IsLocalBrowserMode();
  8465. }
  8466. }
  8467. //---------------------------------------------------------------------------
  8468. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8469. {
  8470. UpdateControls();
  8471. }
  8472. //---------------------------------------------------------------------------
  8473. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8474. TObject * Sender)
  8475. {
  8476. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8477. DebugAssert(DirView != NULL);
  8478. DoDirViewLoaded(DirView);
  8479. TransferPresetAutoSelect();
  8480. }
  8481. //---------------------------------------------------------------------------
  8482. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8483. {
  8484. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8485. // first run after installation
  8486. if (double(Updates.LastCheck) == 0)
  8487. {
  8488. // make sure next time there will be an update (if enabled)
  8489. Updates.LastCheck = TDateTime(1);
  8490. WinConfiguration->Updates = Updates;
  8491. }
  8492. else if ((double(Updates.Period) > 0) &&
  8493. (Now() - Updates.LastCheck >= Updates.Period))
  8494. {
  8495. TThreadMethod OnUpdatesChecked = NULL;
  8496. if (!IsUWP())
  8497. {
  8498. OnUpdatesChecked = UpdatesChecked;
  8499. }
  8500. StartUpdateThread(OnUpdatesChecked);
  8501. }
  8502. }
  8503. //---------------------------------------------------------------------------
  8504. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8505. {
  8506. UnicodeString Message;
  8507. bool New;
  8508. TQueryType Type;
  8509. GetUpdatesMessage(Message, New, Type, false);
  8510. if (!Message.IsEmpty())
  8511. {
  8512. if (New)
  8513. {
  8514. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8515. }
  8516. if (!New && (Type != qtWarning))
  8517. {
  8518. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8519. }
  8520. else
  8521. {
  8522. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8523. }
  8524. if (New)
  8525. {
  8526. Configuration->Usage->Inc(L"UpdateNotifications");
  8527. }
  8528. }
  8529. }
  8530. //---------------------------------------------------------------------------
  8531. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8532. {
  8533. RestoreApp();
  8534. bool CanDisplay = !NonVisualDataModule->Busy;
  8535. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8536. {
  8537. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8538. }
  8539. if (CanDisplay)
  8540. {
  8541. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8542. CheckForUpdates(true);
  8543. }
  8544. else
  8545. {
  8546. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8547. }
  8548. }
  8549. //---------------------------------------------------------------------------
  8550. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8551. TCopyParamRuleData & Data)
  8552. {
  8553. DebugAssert(Terminal != NULL);
  8554. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8555. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8556. Data.RemoteDirectory = RemoteDirView->PathName;
  8557. }
  8558. //---------------------------------------------------------------------------
  8559. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8560. {
  8561. // Terminal can be null when we are changing local directory implicitly,
  8562. // because it has been deleted, while no session is connected
  8563. // (Login dialog is open)
  8564. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8565. {
  8566. DebugAssert(!IsLocalBrowserMode());
  8567. TCopyParamRuleData Data;
  8568. GetTransferPresetAutoSelectData(Data);
  8569. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8570. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8571. if (CopyParamIndex < 0)
  8572. {
  8573. // there is no preset that matches autoselection
  8574. // set preset that we consider "default"
  8575. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8576. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8577. }
  8578. else
  8579. {
  8580. // there is preset matching autoselection
  8581. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8582. if (CopyParamName == FCopyParamAutoSelected)
  8583. {
  8584. // autoselected the same preset as the last time
  8585. // make no change (i.e. preserve custom user preset, if any)
  8586. }
  8587. else
  8588. {
  8589. // autoselected the different preset then the last time (or there
  8590. // was default preset set)
  8591. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8592. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8593. }
  8594. }
  8595. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8596. {
  8597. if (CopyParamIndex >= 0)
  8598. {
  8599. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8600. }
  8601. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8602. try
  8603. {
  8604. int Fmt =
  8605. (CopyParamIndex < 0) ?
  8606. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8607. COPY_PARAM_AUTOSELECTED;
  8608. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8609. Data->Message = MainInstructions(Message);
  8610. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8611. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8612. CopyParamAttrs);
  8613. if (CopyParamIndex >= 0)
  8614. {
  8615. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8616. Info = FORMAT(L"%s\n \n%s", (Info,
  8617. FMTLOAD(COPY_PARAM_RULE,
  8618. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8619. }
  8620. Data->Message += L"\n\n" + Info;
  8621. if (WinConfiguration->CopyParamAutoSelectNotice)
  8622. {
  8623. TransferPresetNoteMessage(Data, true);
  8624. }
  8625. else
  8626. {
  8627. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8628. Data = NULL; // ownership passed
  8629. }
  8630. }
  8631. __finally
  8632. {
  8633. delete Data;
  8634. }
  8635. }
  8636. }
  8637. }
  8638. //---------------------------------------------------------------------------
  8639. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8640. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8641. {
  8642. DebugAssert(NoteData != NULL);
  8643. DebugAssert(!IsLocalBrowserMode());
  8644. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8645. TQueryButtonAlias Aliases[1];
  8646. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8647. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8648. Params.Aliases = Aliases;
  8649. Params.AliasesCount = LENOF(Aliases);
  8650. unsigned int Result =
  8651. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8652. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8653. switch (Result)
  8654. {
  8655. case qaNeverAskAgain:
  8656. DebugAssert(AllowNeverAskAgain);
  8657. WinConfiguration->CopyParamAutoSelectNotice = false;
  8658. break;
  8659. case qaIgnore:
  8660. PreferencesDialog(pmPresets);
  8661. break;
  8662. }
  8663. }
  8664. //---------------------------------------------------------------------------
  8665. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8666. {
  8667. // as of now useless, as this is used for notes only, never for balloons, ...
  8668. RestoreApp();
  8669. // .. and we should never be busy here
  8670. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8671. {
  8672. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8673. }
  8674. }
  8675. //---------------------------------------------------------------------------
  8676. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8677. TPreferencesMode APreferencesMode)
  8678. {
  8679. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8680. TCopyParamRuleData Data;
  8681. if (Terminal != NULL)
  8682. {
  8683. PreferencesData.reset(new TPreferencesDialogData());
  8684. GetTransferPresetAutoSelectData(Data);
  8685. PreferencesData->CopyParamRuleData = &Data;
  8686. }
  8687. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8688. }
  8689. //---------------------------------------------------------------------------
  8690. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8691. const TCustomCommandType & Command)
  8692. {
  8693. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8694. {
  8695. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  8696. }
  8697. }
  8698. //---------------------------------------------------------------------------
  8699. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8700. {
  8701. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8702. TCustomCommandType Command;
  8703. // make sure we use local custom command when remote are not supported
  8704. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8705. {
  8706. Command = FLastCustomCommand;
  8707. }
  8708. else
  8709. {
  8710. Command.Params = Command.Params | ccLocal;
  8711. }
  8712. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8713. FEditingFocusedAdHocCommand = OnFocused;
  8714. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8715. int Options =
  8716. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8717. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8718. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8719. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8720. {
  8721. FLastCustomCommand = Command;
  8722. UpdateCustomCommandsToolbar();
  8723. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8724. }
  8725. }
  8726. //---------------------------------------------------------------------------
  8727. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8728. {
  8729. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8730. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8731. DebugAssert(State > 0);
  8732. if (State <= 0)
  8733. {
  8734. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  8735. }
  8736. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8737. }
  8738. //---------------------------------------------------------------------------
  8739. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8740. TCustomCommandType & Command, int & State)
  8741. {
  8742. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8743. if (Result)
  8744. {
  8745. Command = FLastCustomCommand;
  8746. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8747. }
  8748. return Result;
  8749. }
  8750. //---------------------------------------------------------------------------
  8751. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8752. {
  8753. ResetIncrementalSearch();
  8754. }
  8755. //---------------------------------------------------------------------------
  8756. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8757. {
  8758. DebugAssert(ComponentVisible[Component]);
  8759. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8760. }
  8761. //---------------------------------------------------------------------------
  8762. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8763. {
  8764. // when queue panel is resized here directly, the status bar is stretched
  8765. // over whole space the panel occupied
  8766. PostComponentHide(fcQueueView);
  8767. }
  8768. //---------------------------------------------------------------------------
  8769. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  8770. {
  8771. ComponentVisible[fcQueueFileList] = false;
  8772. }
  8773. //---------------------------------------------------------------------------
  8774. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  8775. {
  8776. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  8777. RefreshColorMode();
  8778. ResetSysDarkTheme();
  8779. ConfigurationChanged();
  8780. ConfigureInterface();
  8781. // Should be called for all controls
  8782. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  8783. }
  8784. //---------------------------------------------------------------------------
  8785. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  8786. {
  8787. // Do not handle, when shutting down anyway (maybe also when not setup completelly yet?)
  8788. if (!FInvalid &&
  8789. (Message.LParam != 0) &&
  8790. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  8791. {
  8792. ThemeChanged();
  8793. }
  8794. TForm::Dispatch(&Message);
  8795. }
  8796. //---------------------------------------------------------------------------
  8797. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8798. {
  8799. TMessage * M = static_cast<TMessage*>(Message);
  8800. switch (M->Msg)
  8801. {
  8802. case CM_DIALOGCHAR:
  8803. CMDialogChar(*M);
  8804. break;
  8805. case WM_APPCOMMAND:
  8806. WMAppCommand(*M);
  8807. break;
  8808. case WM_SYSCOMMAND:
  8809. WMSysCommand(*M);
  8810. break;
  8811. case WM_QUERYENDSESSION:
  8812. WMQueryEndSession(*M);
  8813. break;
  8814. case WM_ENDSESSION:
  8815. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8816. break;
  8817. #ifdef _DEBUG
  8818. case WM_WINDOWPOSCHANGED:
  8819. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8820. break;
  8821. case CM_CUSTOMSTYLECHANGED:
  8822. TForm::Dispatch(Message);
  8823. break;
  8824. #endif
  8825. case WM_COMPONENT_HIDE:
  8826. {
  8827. Byte Component = static_cast<Byte>(M->WParam);
  8828. // sanity check
  8829. if (ComponentVisible[Component])
  8830. {
  8831. // special treatment
  8832. if (Component == fcQueueView)
  8833. {
  8834. ToggleQueueVisibility();
  8835. DebugAssert(!ComponentVisible[fcQueueView]);
  8836. }
  8837. else
  8838. {
  8839. ComponentVisible[Component] = false;
  8840. }
  8841. }
  8842. }
  8843. break;
  8844. case WM_COPYDATA:
  8845. WMCopyData(*M);
  8846. break;
  8847. case WM_MANAGES_CAPTION:
  8848. // caption managed in TTerminalManager::UpdateAppTitle()
  8849. M->Result = 1;
  8850. break;
  8851. case WM_WANTS_MOUSEWHEEL:
  8852. M->Result = 1;
  8853. break;
  8854. case CM_SHOWINGCHANGED:
  8855. CMShowingChanged(*M);
  8856. break;
  8857. case WM_CLOSE:
  8858. WMClose(*M);
  8859. break;
  8860. case WM_DPICHANGED:
  8861. WMDpiChanged(*M);
  8862. break;
  8863. case CM_DPICHANGED:
  8864. CMDpiChanged(*M);
  8865. break;
  8866. case WM_SETTINGCHANGE:
  8867. WMSettingChange(*M);
  8868. break;
  8869. case CM_DIALOGKEY:
  8870. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8871. break;
  8872. default:
  8873. TForm::Dispatch(Message);
  8874. break;
  8875. }
  8876. }
  8877. //---------------------------------------------------------------------------
  8878. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8879. {
  8880. // noop
  8881. }
  8882. //---------------------------------------------------------------------------
  8883. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8884. {
  8885. TForm::Dispatch(&Message);
  8886. FSessionColors->Clear();
  8887. AddFixedSessionImages();
  8888. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8889. UpdateImages();
  8890. }
  8891. //---------------------------------------------------------------------------
  8892. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8893. {
  8894. TForm::Dispatch(&Message);
  8895. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8896. UpdatePixelsPerInchMainWindowCounter();
  8897. }
  8898. //---------------------------------------------------------------------------
  8899. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8900. {
  8901. // Cannot close window while we are busy.
  8902. // We cannot test this in FormCloseQuery as that is called also from
  8903. // Close(), which is called by CloseApplicationAction. So we can be busy
  8904. // there already even, when it is legitimate to close the application.
  8905. // Possibly a better place to handle this would be WMSysCommand.
  8906. if (NonVisualDataModule->Busy)
  8907. {
  8908. Message.Result = 1;
  8909. }
  8910. else
  8911. {
  8912. TForm::Dispatch(&Message);
  8913. }
  8914. }
  8915. //---------------------------------------------------------------------------
  8916. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8917. {
  8918. if (!FShowingChanged)
  8919. {
  8920. AddStartupSequence(L"D");
  8921. FShowingChanged = true;
  8922. }
  8923. // Ignoring implicit local-local browser of Commander, opened from DoShow
  8924. bool NoSession = (ManagedSession == NULL);
  8925. // This calls TCustomScpExplorerForm::DoShow
  8926. TForm::Dispatch(&Message);
  8927. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  8928. if (Showing && !FStarted)
  8929. {
  8930. FStarted = true;
  8931. InterfaceStarted();
  8932. }
  8933. if (Showing && NoSession)
  8934. {
  8935. // When we are starting minimized (i.e. from an installer),
  8936. // postpone showing Login dialog until we get restored.
  8937. // Otherwise the Login dialog (and Authentication window) show restored
  8938. // over invisible (minimized) main window.
  8939. if (WindowState == wsMinimized)
  8940. {
  8941. FNeedSession = true;
  8942. }
  8943. else
  8944. {
  8945. // This happens before application ever goes idle, so the toolbars would
  8946. // stay enabled (initial state) until the Login dialog is dismissed.
  8947. UpdateActions();
  8948. NonVisualDataModule->StartBusy();
  8949. try
  8950. {
  8951. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8952. // otherwise the dialog does not receive focus.
  8953. // With Commander interface the ProcessMessages is called already
  8954. // by TDriveView, but with Explorer interface, we need to call it explicily
  8955. Application->ProcessMessages();
  8956. // do not reload sessions, they have been loaded just now (optimization)
  8957. NeedSession(true);
  8958. }
  8959. __finally
  8960. {
  8961. NonVisualDataModule->EndBusy();
  8962. }
  8963. }
  8964. }
  8965. }
  8966. //---------------------------------------------------------------------------
  8967. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  8968. {
  8969. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  8970. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  8971. }
  8972. //---------------------------------------------------------------------------
  8973. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8974. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8975. int & MaxHeight)
  8976. {
  8977. // workaround for bug in TWinControl.CalcConstraints
  8978. // Check for empty rect (restore from iconic state) is done there only after
  8979. // call to AdjustClientRect, which enlarges the rect (for forms).
  8980. TRect R = GetClientRect();
  8981. // when restoring from iconic state, place no restrictions
  8982. if (IsRectEmpty(R))
  8983. {
  8984. MinWidth = 0;
  8985. MinHeight = 0;
  8986. MaxWidth = 32000;
  8987. MaxHeight = 32000;
  8988. }
  8989. }
  8990. //---------------------------------------------------------------------------
  8991. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8992. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8993. {
  8994. // terminal can be already closed (e.g. dropped connection)
  8995. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8996. {
  8997. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8998. try
  8999. {
  9000. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  9001. }
  9002. catch(...)
  9003. {
  9004. if (!Terminal->Active)
  9005. {
  9006. Close = true;
  9007. }
  9008. throw;
  9009. }
  9010. }
  9011. }
  9012. //---------------------------------------------------------------------------
  9013. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  9014. {
  9015. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  9016. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  9017. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  9018. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  9019. {
  9020. OnGetSpaceAvailable = GetSpaceAvailable;
  9021. }
  9022. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  9023. OnGetSpaceAvailable);
  9024. }
  9025. //---------------------------------------------------------------------------
  9026. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  9027. {
  9028. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  9029. Terminal->FillSessionDataForCode(Data.get());
  9030. return Data.release();
  9031. }
  9032. //---------------------------------------------------------------------------
  9033. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  9034. {
  9035. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  9036. DoGenerateUrlDialog(Data.get(), Paths);
  9037. }
  9038. //---------------------------------------------------------------------------
  9039. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  9040. {
  9041. GenerateUrl(NULL);
  9042. }
  9043. //---------------------------------------------------------------------------
  9044. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  9045. {
  9046. std::unique_ptr<TStrings> Paths(new TStringList());
  9047. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  9048. GenerateUrl(Paths.get());
  9049. }
  9050. //---------------------------------------------------------------------------
  9051. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  9052. {
  9053. FSessionColor = value;
  9054. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  9055. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  9056. static_cast<TObject *>(GetComponent(fcColorMenu)));
  9057. DebugAssert(ColorItem != NULL);
  9058. ColorItem->Color = C;
  9059. NonVisualDataModule->ColorMenuItem->Color = C;
  9060. UpdateControls();
  9061. }
  9062. //---------------------------------------------------------------------------
  9063. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  9064. {
  9065. if (value != FSessionColor)
  9066. {
  9067. UpdateSessionColor(value);
  9068. // Is null when called from LastTerminalClosed
  9069. if (Terminal != NULL)
  9070. {
  9071. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  9072. }
  9073. }
  9074. }
  9075. //---------------------------------------------------------------------------
  9076. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  9077. {
  9078. bool Result = FNoteTimer->Enabled;
  9079. if (Result)
  9080. {
  9081. // cannot cancel note too early
  9082. bool NotEarly =
  9083. (Now() - FNoteShown >
  9084. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  9085. if (Force || NotEarly)
  9086. {
  9087. FNoteTimer->Enabled = false;
  9088. FNote = L"";
  9089. SAFE_DESTROY(FNoteData);
  9090. FOnNoteClick = NULL;
  9091. FNoteHints = FNotes->Text;
  9092. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  9093. UpdateStatusBar();
  9094. }
  9095. }
  9096. return Result;
  9097. }
  9098. //---------------------------------------------------------------------------
  9099. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  9100. {
  9101. DebugAssert(FNoteTimer->Enabled);
  9102. CancelNote(true);
  9103. }
  9104. //---------------------------------------------------------------------------
  9105. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  9106. {
  9107. FNoteHints = FNotes->Text.TrimRight();
  9108. }
  9109. //---------------------------------------------------------------------------
  9110. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  9111. {
  9112. int P = Note.Pos(L"\n");
  9113. if (P > 0)
  9114. {
  9115. Note.SetLength(P - 1);
  9116. }
  9117. FNotes->Add(FORMAT(L"[%s] %s",
  9118. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  9119. while (FNotes->Count > 10)
  9120. {
  9121. FNotes->Delete(0);
  9122. }
  9123. if (UpdateNow)
  9124. {
  9125. UpdateNoteHints();
  9126. UpdateStatusBar();
  9127. }
  9128. }
  9129. //---------------------------------------------------------------------------
  9130. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  9131. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  9132. {
  9133. int P = Note.Pos(L"\n");
  9134. if (P > 0)
  9135. {
  9136. Note.SetLength(P - 1);
  9137. }
  9138. UpdateNoteHints();
  9139. FNote = Note;
  9140. SAFE_DESTROY(FNoteData);
  9141. FNoteData = NoteData;
  9142. FOnNoteClick = OnNoteClick;
  9143. AddNote(Note, false);
  9144. UpdateStatusBar();
  9145. FNoteShown = Now();
  9146. FNoteTimer->Enabled = false;
  9147. if (Seconds == 0)
  9148. {
  9149. Seconds = WinConfiguration->NotificationsTimeout;
  9150. }
  9151. FNoteTimer->Interval = Seconds * MSecsPerSec;
  9152. FNoteTimer->Enabled = true;
  9153. }
  9154. //---------------------------------------------------------------------------
  9155. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  9156. {
  9157. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  9158. }
  9159. //---------------------------------------------------------------------------
  9160. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  9161. {
  9162. if (NonVisualDataModule->SynchronizeBrowsingAction2->Checked)
  9163. {
  9164. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  9165. }
  9166. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  9167. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  9168. (NonVisualDataModule->SynchronizeBrowsingAction2->Checked ? 2 : 3)))),
  9169. 0, NULL, NULL);
  9170. }
  9171. //---------------------------------------------------------------------------
  9172. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  9173. {
  9174. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  9175. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  9176. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  9177. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  9178. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  9179. }
  9180. //---------------------------------------------------------------------------
  9181. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  9182. {
  9183. WinConfiguration->FormatSizeBytes = Style;
  9184. }
  9185. //---------------------------------------------------------------------------
  9186. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  9187. {
  9188. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  9189. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  9190. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  9191. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  9192. }
  9193. //---------------------------------------------------------------------------
  9194. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  9195. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  9196. {
  9197. if (Panel->Index == 0)
  9198. {
  9199. if (FOnNoteClick != NULL)
  9200. {
  9201. // prevent the user data from being freed by possible call
  9202. // to CancelNote or PostNote during call to OnNoteClick
  9203. std::unique_ptr<TObject> NoteData(FNoteData);
  9204. TNotifyEvent OnNoteClick = FOnNoteClick;
  9205. FNoteData = NULL;
  9206. // need to cancel the note as we are going to delete its user data
  9207. CancelNote(true);
  9208. OnNoteClick(NoteData.get());
  9209. }
  9210. }
  9211. if (HasActiveTerminal() &&
  9212. (Panel->Index >= Sender->Panels->Count - SessionPanelCount))
  9213. {
  9214. FileSystemInfo();
  9215. }
  9216. }
  9217. //---------------------------------------------------------------------------
  9218. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  9219. {
  9220. // workaround:
  9221. // 1) for unknown reason, disabling window, while minimized,
  9222. // prevents it from restoring, even if it was enabled again meanwhile
  9223. // 2) when disabling the main window, while another has focus
  9224. // minimize is no longer possible
  9225. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  9226. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  9227. // Shouldn't we use IsApplicationMinimized() here?
  9228. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  9229. {
  9230. Enabled = false;
  9231. }
  9232. FLockLevel++;
  9233. }
  9234. //---------------------------------------------------------------------------
  9235. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  9236. {
  9237. DebugAssert(FLockLevel > 0);
  9238. FLockLevel--;
  9239. if (FLockLevel == 0)
  9240. {
  9241. DebugAssert(FLockSuspendLevel == 0);
  9242. Enabled = true;
  9243. }
  9244. }
  9245. //---------------------------------------------------------------------------
  9246. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  9247. {
  9248. // We need to make sure that window is enabled when the last modal window closes
  9249. // otherwise focus is not restored correctly.
  9250. // So we re-enable the window when modal window opens and
  9251. // disable it back after is closes
  9252. if (FLockLevel > 0)
  9253. {
  9254. // while we have nesting counter, we know that we never be called
  9255. // recursivelly as Application->OnModalBegin is called only
  9256. // for the top-level modal window
  9257. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9258. {
  9259. // won't be disabled when conditions in LockWindow() were not satisfied
  9260. FDisabledOnLockSuspend = !Enabled;
  9261. // When minimized to tray (or actually when set to SW_HIDE),
  9262. // setting Enabled makes the window focusable even when there's
  9263. // modal window over it
  9264. if (!FTrayIcon->Visible)
  9265. {
  9266. Enabled = true;
  9267. }
  9268. }
  9269. FLockSuspendLevel++;
  9270. }
  9271. }
  9272. //---------------------------------------------------------------------------
  9273. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9274. {
  9275. if (FLockSuspendLevel > 0)
  9276. {
  9277. DebugAssert(FLockLevel > 0);
  9278. FLockSuspendLevel--;
  9279. // see comment in SuspendWindowLock
  9280. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9281. {
  9282. // Note that window can be enabled here, when we were minized to tray when
  9283. // was SuspendWindowLock() called.
  9284. // We should possibly do the same check as in LockWindow(),
  9285. // if it is ever possible that the consitions change between
  9286. // SuspendWindowLock() and ResumeWindowLock()
  9287. Enabled = !FDisabledOnLockSuspend;
  9288. }
  9289. }
  9290. }
  9291. //---------------------------------------------------------------------------
  9292. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9293. {
  9294. TTBXComboBoxItem * RemotePathComboBox =
  9295. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9296. if (!TextOnly)
  9297. {
  9298. TStrings * Items = RemotePathComboBox->Strings;
  9299. Items->BeginUpdate();
  9300. try
  9301. {
  9302. Items->Clear();
  9303. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9304. {
  9305. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9306. while (!IsUnixRootPath(APath))
  9307. {
  9308. int P = APath.LastDelimiter(L'/');
  9309. DebugAssert(P >= 0);
  9310. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9311. APath.SetLength(P - 1);
  9312. }
  9313. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9314. }
  9315. }
  9316. __finally
  9317. {
  9318. Items->EndUpdate();
  9319. }
  9320. }
  9321. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9322. // Setting ItemIndex to -1 does not reset its text
  9323. if (RemotePathComboBox->Strings->Count == 0)
  9324. {
  9325. RemotePathComboBox->Text = L"";
  9326. }
  9327. }
  9328. //---------------------------------------------------------------------------
  9329. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxAdjustImageIndex(
  9330. TTBXComboBoxItem * Sender, const UnicodeString DebugUsedArg(AText), int AIndex, int & ImageIndex)
  9331. {
  9332. if (AIndex < 0)
  9333. {
  9334. AIndex = Sender->ItemIndex;
  9335. }
  9336. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9337. }
  9338. //---------------------------------------------------------------------------
  9339. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9340. TTBXComboBoxItem * Sender, const UnicodeString AText, int AIndex,
  9341. int & ImageIndex)
  9342. {
  9343. DoRemotePathComboBoxAdjustImageIndex(Sender, AText, AIndex, ImageIndex);
  9344. }
  9345. //---------------------------------------------------------------------------
  9346. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9347. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9348. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9349. {
  9350. if (!IsLocalBrowserMode())
  9351. {
  9352. ARect.Left += (10 * AIndex);
  9353. }
  9354. }
  9355. //---------------------------------------------------------------------------
  9356. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9357. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9358. {
  9359. AWidth += (10 * AIndex);
  9360. }
  9361. //---------------------------------------------------------------------------
  9362. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxItemClick(TObject * Sender)
  9363. {
  9364. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9365. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9366. int Index = RemotePathComboBox->ItemIndex;
  9367. while (Index < RemotePathComboBox->Strings->Count - 1)
  9368. {
  9369. APath = UnixExtractFileDir(APath);
  9370. Index++;
  9371. }
  9372. // VanDyke style paths
  9373. if (APath.IsEmpty())
  9374. {
  9375. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9376. APath = ROOTDIRECTORY;
  9377. }
  9378. if (RemoteDirView->Path != APath)
  9379. {
  9380. RemoteDirView->Path = APath;
  9381. }
  9382. }
  9383. //---------------------------------------------------------------------------
  9384. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(TObject * Sender)
  9385. {
  9386. DoRemotePathComboBoxItemClick(Sender);
  9387. }
  9388. //---------------------------------------------------------------------------
  9389. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxCancel(TObject * Sender)
  9390. {
  9391. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9392. DebugUsedParam(Sender);
  9393. UpdateRemotePathComboBox(true);
  9394. }
  9395. //---------------------------------------------------------------------------
  9396. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9397. {
  9398. DoRemotePathComboBoxCancel(Sender);
  9399. }
  9400. //---------------------------------------------------------------------------
  9401. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9402. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9403. {
  9404. ResetIncrementalSearch();
  9405. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9406. DebugAssert(DirView != NULL);
  9407. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9408. {
  9409. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9410. // OnEditing is called also from TCustomListView::CanEdit
  9411. if (Edit != NULL)
  9412. {
  9413. EditSelectBaseName(Edit);
  9414. }
  9415. }
  9416. }
  9417. //---------------------------------------------------------------------------
  9418. //---------------------------------------------------------------------------
  9419. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9420. {
  9421. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9422. Result->AutoDetectDnD = false;
  9423. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9424. Result->RenderDataOn = rdoEnterAndDropSync;
  9425. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9426. return Result;
  9427. }
  9428. //---------------------------------------------------------------------------
  9429. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9430. {
  9431. if (IsWin10Build(2004))
  9432. {
  9433. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9434. BOOL DarkMode = FImmersiveDarkMode ? TRUE : FALSE;
  9435. const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  9436. DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
  9437. }
  9438. }
  9439. //---------------------------------------------------------------------------
  9440. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9441. {
  9442. TForm::CreateWnd();
  9443. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9444. UpdateDarkMode();
  9445. if (FSessionsDragDropFilesEx == NULL)
  9446. {
  9447. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9448. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9449. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9450. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9451. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9452. }
  9453. if (FQueueDragDropFilesEx == NULL)
  9454. {
  9455. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9456. // No need to set OnDragOver as we do not have any restrictions
  9457. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9458. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9459. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9460. }
  9461. }
  9462. //---------------------------------------------------------------------------
  9463. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9464. {
  9465. TForm::DestroyWnd();
  9466. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9467. FQueueDragDropFilesEx->DragDropControl = NULL;
  9468. }
  9469. //---------------------------------------------------------------------------
  9470. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9471. {
  9472. SideEnter(FCurrentSide);
  9473. FEverShown = true;
  9474. }
  9475. //---------------------------------------------------------------------------
  9476. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9477. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9478. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9479. {
  9480. if (!NonVisualDataModule->Busy)
  9481. {
  9482. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9483. Configuration->Usage->Inc(L"FileFinds");
  9484. LockWindow(true);
  9485. NonVisualDataModule->StartBusy();
  9486. WinConfiguration->LockedInterface = true;
  9487. try
  9488. {
  9489. Terminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9490. }
  9491. __finally
  9492. {
  9493. WinConfiguration->LockedInterface = false;
  9494. NonVisualDataModule->EndBusy();
  9495. UnlockWindow();
  9496. }
  9497. }
  9498. }
  9499. //---------------------------------------------------------------------------
  9500. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9501. {
  9502. if (!NonVisualDataModule->Busy)
  9503. {
  9504. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9505. SetFocus();
  9506. RemoteDirView->Path = UnixExtractFilePath(Path);
  9507. UnicodeString FileName = UnixExtractFileName(Path);
  9508. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9509. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9510. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9511. if (Item == NULL)
  9512. {
  9513. ReloadDirectory(osRemote);
  9514. Item = RemoteDirView->FindFileItem(FileName);
  9515. }
  9516. if (Item != NULL)
  9517. {
  9518. RemoteDirView->ItemFocused = Item;
  9519. RemoteDirView->ItemFocused->MakeVisible(false);
  9520. RemoteDirView->SetFocus();
  9521. }
  9522. }
  9523. }
  9524. //---------------------------------------------------------------------------
  9525. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9526. {
  9527. bool Result = !NonVisualDataModule->Busy;
  9528. if (Result)
  9529. {
  9530. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9531. }
  9532. return Result;
  9533. }
  9534. //---------------------------------------------------------------------------
  9535. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9536. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9537. {
  9538. if (CanOperateOnFoundFiles(ATerminal))
  9539. {
  9540. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9541. FOnFileOperationFinished = OnFileOperationFinished;
  9542. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9543. }
  9544. }
  9545. //---------------------------------------------------------------------------
  9546. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9547. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9548. {
  9549. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9550. }
  9551. //---------------------------------------------------------------------------
  9552. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9553. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9554. {
  9555. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9556. }
  9557. //---------------------------------------------------------------------------
  9558. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9559. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9560. {
  9561. if (CanOperateOnFoundFiles(ATerminal))
  9562. {
  9563. for (int Index = 0; Index < FileList->Count; Index++)
  9564. {
  9565. UnicodeString FileName = FileList->Strings[Index];
  9566. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9567. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9568. OnFileOperationFinished(osRemote, FileName, true);
  9569. }
  9570. }
  9571. }
  9572. //---------------------------------------------------------------------------
  9573. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9574. {
  9575. FFileFindTerminal = Terminal;
  9576. ShowFileFindDialog(
  9577. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9578. DoDownloadFoundFiles, DoEditFoundFiles);
  9579. }
  9580. //---------------------------------------------------------------------------
  9581. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9582. {
  9583. FTaskbarList = TaskbarList;
  9584. }
  9585. //---------------------------------------------------------------------------
  9586. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9587. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9588. {
  9589. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9590. if (Index >= 0)
  9591. {
  9592. if (Button == mbLeft)
  9593. {
  9594. // WORKAROUND: "Mouse down" is raised only after tab is switched.
  9595. // If switching tab (switching session) takes long enough for user
  9596. // to actually release the button, "mouse down" is still raised,
  9597. // but we do not get "mouse up" event, so dragging is not cancelled,
  9598. // prevent that by not beginning dragging in the first place.
  9599. if (IsKeyPressed(VK_LBUTTON))
  9600. {
  9601. // when user clicks the "+", we get mouse down only after the session
  9602. // is closed, when new session tab is already on X:Y, so dragging
  9603. // starts, prevent that
  9604. if (MilliSecondsBetween(Now(), FSessionsPageControlNewTabTime) > 500)
  9605. {
  9606. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9607. if (Terminal != NULL)
  9608. {
  9609. SessionsPageControl->BeginDrag(false);
  9610. }
  9611. }
  9612. }
  9613. }
  9614. else if (Button == mbMiddle)
  9615. {
  9616. // ignore middle-click for "New session tab" and the last local-local tab
  9617. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9618. if ((Session != NULL) && CanCloseSession(Session))
  9619. {
  9620. CloseSessionTab(Index);
  9621. }
  9622. }
  9623. }
  9624. }
  9625. //---------------------------------------------------------------------------
  9626. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9627. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9628. {
  9629. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9630. // do not allow dropping on the "+" tab
  9631. TTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9632. if ((TargetTerminal != NULL) &&
  9633. (SessionsPageControl->ActivePage->PageIndex != Index))
  9634. {
  9635. Configuration->Usage->Inc(L"SessionTabMoves");
  9636. // this is almost redundant as we would recreate tabs in SessionListChanged,
  9637. // but we want to actually prevent that to avoid flicker
  9638. SessionsPageControl->ActivePage->PageIndex = Index;
  9639. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->ActivePage);
  9640. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9641. }
  9642. }
  9643. //---------------------------------------------------------------------------
  9644. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9645. TObject * Sender, TObject * Source, int X, int Y,
  9646. TDragState /*State*/, bool & Accept)
  9647. {
  9648. Accept = (Sender == Source);
  9649. if (Accept)
  9650. {
  9651. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9652. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9653. // do not allow dragging to the "+" tab
  9654. Accept = (Terminal != NULL);
  9655. }
  9656. }
  9657. //---------------------------------------------------------------------------
  9658. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  9659. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  9660. {
  9661. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9662. if (Index < 0)
  9663. {
  9664. Effect = DROPEFFECT_NONE;
  9665. }
  9666. else
  9667. {
  9668. TManagedTerminal * TargetSession = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9669. // do not allow dropping on the "+" tab or on disconnected or local-local tabs
  9670. if (!IsActiveTerminal(TargetSession) || DebugAlwaysFalse(TargetSession->LocalBrowser))
  9671. {
  9672. Effect = DROPEFFECT_NONE;
  9673. }
  9674. else if ((TargetSession != Terminal) && (Effect == DROPEFFECT_MOVE))
  9675. {
  9676. Effect = DROPEFFECT_COPY;
  9677. }
  9678. }
  9679. }
  9680. //---------------------------------------------------------------------------
  9681. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9682. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9683. {
  9684. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9685. // do not allow dropping on local-local, disconnected and the "+" tab
  9686. // (though we do not seem to get here in that case anyway, contrary to RemoteFileControlDDTargetDrop)
  9687. TManagedTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9688. if (DebugAlwaysTrue(IsActiveTerminal(TargetTerminal) && !TargetTerminal->LocalBrowser))
  9689. {
  9690. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9691. if (!IsFileControl(DropSourceControl, osRemote))
  9692. {
  9693. TTerminalManager::Instance()->ActiveSession = TargetTerminal;
  9694. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9695. // Why don't we use Terminal->CurrentDirectory directly?
  9696. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9697. // do not force queue, drag drop
  9698. false, true);
  9699. }
  9700. }
  9701. }
  9702. //---------------------------------------------------------------------------
  9703. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9704. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9705. {
  9706. // Downloads are handled in RemoteFileControlDDEnd
  9707. if (!IsFileControl(DropSourceControl, osRemote))
  9708. {
  9709. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9710. Terminal->CurrentDirectory,
  9711. // force queue, drag drop
  9712. true, true);
  9713. }
  9714. }
  9715. //---------------------------------------------------------------------------
  9716. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9717. {
  9718. FShowing = false;
  9719. // Do not save empty workspace
  9720. if (WinConfiguration->AutoSaveWorkspace && (ManagedSession != NULL))
  9721. {
  9722. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9723. UnicodeString Name = WorkspaceName();
  9724. DoSaveWorkspace(
  9725. Name, DataList.get(),
  9726. !Configuration->DisablePasswordStoring &&
  9727. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9728. WinConfiguration->LastStoredSession = Name;
  9729. }
  9730. }
  9731. //---------------------------------------------------------------------------
  9732. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9733. {
  9734. TManagedTerminal * ManagedTerminal =
  9735. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9736. if (ManagedTerminal != NULL)
  9737. {
  9738. ManagedTerminal->DirectoryLoaded = Now();
  9739. }
  9740. }
  9741. //---------------------------------------------------------------------------
  9742. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9743. TListItem * /*Item*/, bool /*Selected*/)
  9744. {
  9745. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9746. switch (DirView->LastSelectMethod)
  9747. {
  9748. case smKeyboard:
  9749. Configuration->Usage->Inc(L"KeyboardSelections");
  9750. break;
  9751. case smMouse:
  9752. Configuration->Usage->Inc(L"MouseSelections");
  9753. break;
  9754. }
  9755. }
  9756. //---------------------------------------------------------------------------
  9757. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9758. {
  9759. if (Color != 0)
  9760. {
  9761. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9762. }
  9763. else
  9764. {
  9765. return FSessionTabImageIndex;
  9766. }
  9767. }
  9768. //---------------------------------------------------------------------------
  9769. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9770. {
  9771. TPngImageCollectionItem * Item =
  9772. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9773. FSessionColors->AddPng(Item->PngImage);
  9774. return FSessionColors->Count - 1;
  9775. }
  9776. //---------------------------------------------------------------------------
  9777. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9778. {
  9779. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9780. FNewRemoteTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewRemoteTabAction->ImageIndex);
  9781. FNewLocalTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewLocalTabAction->ImageIndex);
  9782. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9783. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9784. FLocalBrowserTabImageIndex = AddFixedSessionImage(LocalBrowserImageIndex);
  9785. }
  9786. //---------------------------------------------------------------------------
  9787. int TCustomScpExplorerForm::GetNewTabActionImageIndex()
  9788. {
  9789. return NonVisualDataModule->NewRemoteTabAction->ImageIndex;
  9790. }
  9791. //---------------------------------------------------------------------------
  9792. int TCustomScpExplorerForm::GetNewTabTabImageIndex(TOperationSide Side)
  9793. {
  9794. switch (Side)
  9795. {
  9796. case osLocal:
  9797. return FNewLocalTabTabImageIndex;
  9798. case osRemote:
  9799. case osCurrent:
  9800. return FNewRemoteTabTabImageIndex;
  9801. default:
  9802. DebugFail();
  9803. return -1;
  9804. }
  9805. }
  9806. //---------------------------------------------------------------------------
  9807. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9808. {
  9809. for (int Index = 0; Index < Control->ControlCount; Index++)
  9810. {
  9811. TControl * AControl = Control->Controls[Index];
  9812. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9813. if (WinControl != NULL)
  9814. {
  9815. CollectItemsWithTextDisplayMode(WinControl);
  9816. }
  9817. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9818. if (Toolbar != NULL)
  9819. {
  9820. // we care for top-level items only
  9821. TTBCustomItem * Items = Toolbar->Items;
  9822. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9823. {
  9824. TTBCustomItem * Item = Items->Items[ItemIndex];
  9825. if (((Item->DisplayMode == nbdmImageAndText) ||
  9826. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9827. EligibleForImageDisplayMode(Item))
  9828. {
  9829. FItemsWithTextDisplayMode.insert(Item);
  9830. }
  9831. }
  9832. }
  9833. }
  9834. }
  9835. //---------------------------------------------------------------------------
  9836. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9837. {
  9838. return true;
  9839. }
  9840. //---------------------------------------------------------------------------
  9841. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9842. {
  9843. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9844. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9845. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9846. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9847. bool Result = true;
  9848. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9849. {
  9850. TTBCustomItem * Item = *i;
  9851. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9852. if (Label != NULL)
  9853. {
  9854. // optimization
  9855. if (Label->Visible == SelectiveToolbarText)
  9856. {
  9857. Result = false;
  9858. }
  9859. else
  9860. {
  9861. Label->Visible = SelectiveToolbarText;
  9862. }
  9863. }
  9864. else
  9865. {
  9866. // optimization
  9867. if (Item->DisplayMode == DisplayMode)
  9868. {
  9869. Result = false;
  9870. }
  9871. else
  9872. {
  9873. Item->DisplayMode = DisplayMode;
  9874. }
  9875. }
  9876. ++i;
  9877. }
  9878. UpdateNewTabTab();
  9879. return Result;
  9880. }
  9881. //---------------------------------------------------------------------------
  9882. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9883. {
  9884. DebugFail();
  9885. }
  9886. //---------------------------------------------------------------------------
  9887. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9888. {
  9889. // Among other, a lock level is non zero, while directory is being loaded,
  9890. // even when it happens as a result of dirview navigation,
  9891. // i.e. when TNonVisualDataModule is NOT FBusy.
  9892. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9893. // Among other this prevents a panel auto update to occur while
  9894. // directory is changing.
  9895. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9896. }
  9897. //---------------------------------------------------------------------------
  9898. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * Action, TActionAllowed Allowed)
  9899. {
  9900. // While the window is disabled, we still seem to process menu shortcuts at least,
  9901. // so stop it at least here.
  9902. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9903. bool Result;
  9904. if (Allowed == aaUpdate)
  9905. {
  9906. Result = true;
  9907. }
  9908. else
  9909. {
  9910. Result = !NonVisualDataModule->Busy;
  9911. if (Result && (Allowed == aaShortCut))
  9912. {
  9913. // Otherwise F5 for local-local will be swallowed by disabled local<>remote actions in TCustomScpExplorerForm::KeyDown
  9914. Action->Update();
  9915. Result = Action->Enabled;
  9916. }
  9917. }
  9918. return Result;
  9919. }
  9920. //---------------------------------------------------------------------------
  9921. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9922. {
  9923. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9924. }
  9925. //---------------------------------------------------------------------------
  9926. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9927. {
  9928. // This is somewhat redundant to LockWindow() call from
  9929. // TTerminalManager::TerminalReadDirectoryProgress.
  9930. // But disabling window is known not to block keyboard shorcuts
  9931. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9932. if (Busy > 0)
  9933. {
  9934. if (NonVisualDataModule->Busy)
  9935. {
  9936. State = false;
  9937. }
  9938. else
  9939. {
  9940. NonVisualDataModule->StartBusy();
  9941. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9942. LockWindow();
  9943. }
  9944. }
  9945. else if (Busy < 0)
  9946. {
  9947. UnlockWindow();
  9948. NonVisualDataModule->EndBusy();
  9949. }
  9950. else
  9951. {
  9952. State = NonVisualDataModule->Busy;
  9953. }
  9954. }
  9955. //---------------------------------------------------------------------------
  9956. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9957. {
  9958. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9959. if (Index >= 0)
  9960. {
  9961. TPopupMenu * PopupMenu = NULL;
  9962. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9963. if (Session != NULL)
  9964. {
  9965. SessionsPageControl->ActivePageIndex = Index;
  9966. if (DebugAlwaysTrue(SessionTabSwitched()))
  9967. {
  9968. // copied from TControl.WMContextMenu
  9969. SendCancelMode(SessionsPageControl);
  9970. // explicit popup instead of using PopupMenu property
  9971. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9972. // while connecting yet not-connected session and hence
  9973. // allowing an access to commands over not-completelly connected session
  9974. PopupMenu = Session->LocalBrowser ? NonVisualDataModule->LocalBrowserPopup : NonVisualDataModule->SessionsPopup;
  9975. }
  9976. }
  9977. else
  9978. {
  9979. if (SupportsLocalBrowser())
  9980. {
  9981. PopupMenu = NonVisualDataModule->NewTabPopup;
  9982. }
  9983. }
  9984. if (PopupMenu != NULL)
  9985. {
  9986. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9987. PopupMenu->PopupComponent = SessionsPageControl;
  9988. PopupMenu->Popup(Point.x, Point.y);
  9989. }
  9990. }
  9991. Handled = true;
  9992. }
  9993. //---------------------------------------------------------------------------
  9994. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9995. {
  9996. return HasActiveTerminal() && Terminal->IsCapable[fcChangePassword];
  9997. }
  9998. //---------------------------------------------------------------------------
  9999. void __fastcall TCustomScpExplorerForm::ChangePassword()
  10000. {
  10001. Configuration->Usage->Inc(L"PasswordChanges");
  10002. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  10003. ChangePasswordData->DisableAuthentationsExceptPassword();
  10004. ChangePasswordData->ChangePassword = true;
  10005. std::unique_ptr<TTerminal> ChangePasswordSession(
  10006. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  10007. ChangePasswordSession->AutoReadDirectory = false;
  10008. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  10009. {
  10010. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  10011. }
  10012. else
  10013. {
  10014. Configuration->Usage->Inc(L"PasswordChangesFailed");
  10015. }
  10016. }
  10017. //---------------------------------------------------------------------------
  10018. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  10019. {
  10020. // No nice way to assert SSH2
  10021. return HasActiveTerminal() && (Terminal->FSProtocol == cfsSFTP);
  10022. }
  10023. //---------------------------------------------------------------------------
  10024. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  10025. {
  10026. TOperationVisualizer OperationVisualizer;
  10027. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  10028. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  10029. }
  10030. //---------------------------------------------------------------------------
  10031. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  10032. {
  10033. TForm::ChangeScale(M, D);
  10034. int APixelsPerInch = GetControlPixelsPerInch(this);
  10035. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  10036. try
  10037. {
  10038. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  10039. }
  10040. __finally
  10041. {
  10042. GUIConfiguration->ChangeResourceModule(ResourceModule);
  10043. }
  10044. }
  10045. //---------------------------------------------------------------------------
  10046. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  10047. {
  10048. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  10049. }
  10050. //---------------------------------------------------------------------
  10051. class TPublicControl : public TControl
  10052. {
  10053. friend class TCustomScpExplorerForm;
  10054. };
  10055. //---------------------------------------------------------------------------
  10056. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  10057. {
  10058. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  10059. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  10060. }
  10061. //---------------------------------------------------------------------------
  10062. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  10063. {
  10064. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  10065. OpenBookmark(Side, Bookmark);
  10066. }
  10067. //---------------------------------------------------------------------------
  10068. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  10069. {
  10070. DoBookmarkClick(osLocal, Sender);
  10071. }
  10072. //---------------------------------------------------------------------------
  10073. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  10074. {
  10075. DoBookmarkClick(osRemote, Sender);
  10076. }
  10077. //---------------------------------------------------------------------------
  10078. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  10079. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  10080. {
  10081. if (BookmarkList != NULL)
  10082. {
  10083. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  10084. if (IsLocalBrowserMode())
  10085. {
  10086. Side = osLocal;
  10087. }
  10088. if (!WinConfiguration->UseLocationProfiles)
  10089. {
  10090. std::unique_ptr<TStringList> Directories(new TStringList());
  10091. Directories->CaseSensitive = (Side == osRemote);
  10092. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10093. {
  10094. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10095. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  10096. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  10097. {
  10098. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10099. Item->Caption = EscapeHotkey(Directory);
  10100. Item->ShortCut = Bookmark->ShortCut;
  10101. Item->OnClick = OnBookmarkClick;
  10102. Item->Tag = reinterpret_cast<int>(Bookmark);
  10103. Directories->Add(Directory);
  10104. Menu->Add(Item.release());
  10105. }
  10106. }
  10107. }
  10108. else
  10109. {
  10110. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  10111. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10112. {
  10113. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10114. if (!Bookmark->Node.IsEmpty())
  10115. {
  10116. Folders->Add(Bookmark->Node);
  10117. }
  10118. }
  10119. for (int Index = 0; Index < Folders->Count; Index++)
  10120. {
  10121. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  10122. Item->Caption = Folders->Strings[Index];
  10123. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction2->ImageIndex;
  10124. Folders->Objects[Index] = Item.get();
  10125. Menu->Add(Item.release());
  10126. }
  10127. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10128. {
  10129. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10130. TTBCustomItem * Parent;
  10131. if (!Bookmark->Node.IsEmpty())
  10132. {
  10133. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  10134. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  10135. }
  10136. else
  10137. {
  10138. Parent = Menu;
  10139. }
  10140. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10141. Item->Caption = Bookmark->Name;
  10142. Item->ShortCut = Bookmark->ShortCut;
  10143. Item->OnClick = OnBookmarkClick;
  10144. Item->Tag = reinterpret_cast<int>(Bookmark);
  10145. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  10146. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  10147. {
  10148. // No hint for location profiles that are actually mere "bookmarks"
  10149. }
  10150. else
  10151. {
  10152. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  10153. UnicodeString LongHint;
  10154. if (!Bookmark->Local.IsEmpty())
  10155. {
  10156. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  10157. }
  10158. if (!Bookmark->Remote.IsEmpty())
  10159. {
  10160. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  10161. }
  10162. AddToList(Hint, LongHint, L"|");
  10163. Item->Hint = Hint;
  10164. }
  10165. Parent->Add(Item.release());
  10166. }
  10167. }
  10168. AddMenuSeparator(Menu);
  10169. }
  10170. }
  10171. //---------------------------------------------------------------------------
  10172. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  10173. {
  10174. Menu->Clear();
  10175. std::unique_ptr<TTBCustomItem> Item;
  10176. Item.reset(new TTBXItem(Owner));
  10177. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  10178. Item->Options = Item->Options << tboDefault;
  10179. Menu->Add(Item.release());
  10180. AddMenuSeparator(Menu);
  10181. if (Terminal != NULL)
  10182. {
  10183. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  10184. }
  10185. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  10186. Item.reset(new TTBXItem(Owner));
  10187. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction2 : NonVisualDataModule->RemoteAddBookmarkAction2;
  10188. Menu->Add(Item.release());
  10189. }
  10190. //---------------------------------------------------------------------------
  10191. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  10192. {
  10193. bool Result = true;
  10194. bool Remote = !IsSideLocalBrowser(Side);
  10195. try
  10196. {
  10197. if (Remote)
  10198. {
  10199. Terminal->ExceptionOnFail = true;
  10200. }
  10201. try
  10202. {
  10203. DirView(Side)->Path = Path;
  10204. }
  10205. __finally
  10206. {
  10207. if (Remote)
  10208. {
  10209. Terminal->ExceptionOnFail = false;
  10210. }
  10211. }
  10212. }
  10213. catch (Exception & E)
  10214. {
  10215. if (!Remote || Terminal->Active)
  10216. {
  10217. ShowExtendedExceptionEx(Terminal, &E);
  10218. Result = false;
  10219. }
  10220. else
  10221. {
  10222. throw;
  10223. }
  10224. }
  10225. return Result;
  10226. }
  10227. //---------------------------------------------------------------------------
  10228. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  10229. {
  10230. // Make sure there some feedback even when loading tiny local folders
  10231. TInstantOperationVisualizer Visualizer;
  10232. DirView(Side)->ReloadDirectory();
  10233. }
  10234. //---------------------------------------------------------------------------
  10235. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  10236. {
  10237. if (Index == SessionsPageControl->TabIndex)
  10238. {
  10239. CloseSession();
  10240. }
  10241. else
  10242. {
  10243. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10244. TTerminal * Terminal = GetSessionTabSession(TabSheet);
  10245. TTerminalManager * Manager = TTerminalManager::Instance();
  10246. if (!Terminal->Active)
  10247. {
  10248. Manager->FreeTerminal(Terminal);
  10249. }
  10250. else
  10251. {
  10252. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  10253. if (CanCloseQueue(Queue))
  10254. {
  10255. Manager->FreeTerminal(Terminal);
  10256. }
  10257. }
  10258. }
  10259. }
  10260. //---------------------------------------------------------------------------
  10261. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabButtonClick(TPageControl *, int Index)
  10262. {
  10263. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10264. if (Session == NULL)
  10265. {
  10266. TRect ButtonRect = SessionsPageControl->TabButtonRect(Index);
  10267. ButtonRect = TRect(SessionsPageControl->ClientToScreen(ButtonRect.TopLeft()), SessionsPageControl->ClientToScreen(ButtonRect.BottomRight()));
  10268. //TPoint P = TPoint(ButtonRect.Left, ButtonRect.Bottom);
  10269. //P = SessionsPageControl->ClientToScreen(P);
  10270. MenuPopup(NonVisualDataModule->NewTabPopup, ButtonRect, SessionsPageControl);
  10271. }
  10272. else
  10273. {
  10274. CloseSessionTab(Index);
  10275. }
  10276. }
  10277. //---------------------------------------------------------------------------
  10278. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  10279. {
  10280. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  10281. {
  10282. TInstantOperationVisualizer Visualizer;
  10283. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  10284. // before we create a new data object
  10285. ClipboardClear();
  10286. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  10287. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  10288. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  10289. // Intermediate variable to WORKAROUND a crash in compiler
  10290. TObjectList * ClipboardFakeMonitors =
  10291. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  10292. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  10293. if (FClipboardDragDropFilesEx.get() == NULL)
  10294. {
  10295. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  10296. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  10297. }
  10298. FClipboardDragDropFilesEx->FileList->Clear();
  10299. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  10300. FClipboardDragDropFilesEx->CopyToClipboard();
  10301. FClipboardTerminal = Terminal;
  10302. // Need full paths, as cwd can be different once files are pasted
  10303. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  10304. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  10305. {
  10306. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  10307. }
  10308. }
  10309. }
  10310. //---------------------------------------------------------------------------
  10311. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  10312. {
  10313. if (OpenClipboard(0))
  10314. {
  10315. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  10316. CloseClipboard();
  10317. }
  10318. }
  10319. //---------------------------------------------------------------------------
  10320. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  10321. {
  10322. return !FClipboardFakeDirectory.IsEmpty();
  10323. }
  10324. //---------------------------------------------------------------------------
  10325. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  10326. {
  10327. RemoveDir(ApiPath(FClipboardFakeDirectory));
  10328. FClipboardFakeDirectory = UnicodeString();
  10329. FClipboardTerminal = NULL;
  10330. if (FDownloadingFromClipboard)
  10331. {
  10332. // We are called by the monitor, so attempt to release it would deadlock
  10333. FClipboardFakeMonitorsPendingReset = true;
  10334. }
  10335. else
  10336. {
  10337. FClipboardFakeMonitors.reset(NULL);
  10338. }
  10339. }
  10340. //---------------------------------------------------------------------------
  10341. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10342. {
  10343. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10344. {
  10345. ClipboardStop();
  10346. }
  10347. }
  10348. //---------------------------------------------------------------------------
  10349. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10350. {
  10351. TTerminalManager * Manager = TTerminalManager::Instance();
  10352. Manager->ActiveSession = FClipboardTerminal;
  10353. TTransferOperationParam Params;
  10354. Params.TargetDirectory = TargetDirectory;
  10355. Params.DragDrop = DragDrop;
  10356. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10357. }
  10358. //---------------------------------------------------------------------------
  10359. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10360. {
  10361. // It can actually rarelly happen that some random file is created, while we are shutting down the monitor
  10362. // (as it pumps a Windows message queue while being shutted down)
  10363. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10364. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10365. {
  10366. // Can fail as it can be e.g. locked by AV, so we retry that later
  10367. bool Removed = RemoveDir(ApiPath(FileName));
  10368. try
  10369. {
  10370. if (!NonVisualDataModule->Busy)
  10371. {
  10372. DebugAssert(!FClipboardFakeMonitorsPendingReset);
  10373. FClipboardFakeMonitorsPendingReset = false;
  10374. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10375. TAutoFlag Flag(FDownloadingFromClipboard);
  10376. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  10377. }
  10378. }
  10379. __finally
  10380. {
  10381. if (!Removed)
  10382. {
  10383. RemoveDir(ApiPath(FileName));
  10384. }
  10385. }
  10386. }
  10387. }
  10388. //---------------------------------------------------------------------------
  10389. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10390. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10391. {
  10392. TFileMasks::TParams MaskParams;
  10393. MaskParams.Size = Size;
  10394. MaskParams.Modification = Modification;
  10395. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10396. bool Local = (dynamic_cast<TUnixDirView *>(DirView) == NULL);
  10397. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10398. {
  10399. bool ImplicitMatch;
  10400. if (Iter->FileMask.Matches(FileName, Local, Directory, &MaskParams, false, ImplicitMatch) &&
  10401. !ImplicitMatch)
  10402. {
  10403. Color = Iter->Color;
  10404. break;
  10405. }
  10406. }
  10407. }
  10408. //---------------------------------------------------------------------------
  10409. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10410. {
  10411. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10412. if (!DirView->IsEditing())
  10413. {
  10414. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10415. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10416. // while searching
  10417. if (Key == VK_BACK)
  10418. {
  10419. if (!FIncrementalSearch.IsEmpty())
  10420. {
  10421. if (FIncrementalSearch.Length() == 1)
  10422. {
  10423. ResetIncrementalSearch();
  10424. }
  10425. else
  10426. {
  10427. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  10428. IncrementalSearch(NewText, false, false);
  10429. }
  10430. Key = 0;
  10431. }
  10432. }
  10433. }
  10434. }
  10435. //---------------------------------------------------------------------------
  10436. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  10437. {
  10438. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10439. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  10440. {
  10441. // Filter control sequences.
  10442. // When not searching yet, prefer use of the space for toggling file selection
  10443. // (so we cannot incrementally search for a string starting with a space).
  10444. if ((Key > VK_SPACE) ||
  10445. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  10446. {
  10447. IncrementalSearch(FIncrementalSearch + Key, false, false);
  10448. Key = 0;
  10449. }
  10450. }
  10451. }
  10452. //---------------------------------------------------------------------------
  10453. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  10454. {
  10455. if (!FIncrementalSearch.IsEmpty())
  10456. {
  10457. FIncrementalSearch = L"";
  10458. DirView(osCurrent)->UpdateStatusBar();
  10459. }
  10460. }
  10461. //---------------------------------------------------------------------------
  10462. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10463. {
  10464. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  10465. if (Item == NULL)
  10466. {
  10467. MessageBeep(MB_ICONHAND);
  10468. }
  10469. else
  10470. {
  10471. TCustomDirView * ADirView = DirView(osCurrent);
  10472. {
  10473. TAutoNestingCounter Guard(FIncrementalSearching);
  10474. ADirView->FocusItem(Item);
  10475. if (ADirView->NortonLike == nlOff)
  10476. {
  10477. if ((ADirView->Selected != Item) ||
  10478. (ADirView->SelCount > 1))
  10479. {
  10480. ADirView->ClearSelection();
  10481. ADirView->Selected = Item;
  10482. }
  10483. }
  10484. }
  10485. FIncrementalSearch = Text;
  10486. Item->MakeVisible(false);
  10487. TListItem * NextItem = SearchFile(Text, true, Reverse);
  10488. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  10489. ADirView->UpdateStatusBar();
  10490. }
  10491. }
  10492. //---------------------------------------------------------------------------
  10493. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  10494. {
  10495. int Index = Item->Index;
  10496. if (!Reverse)
  10497. {
  10498. Index++;
  10499. if (Index >= Item->Owner->Count)
  10500. {
  10501. Index = 0;
  10502. }
  10503. }
  10504. else
  10505. {
  10506. Index--;
  10507. if (Index < 0)
  10508. {
  10509. Index = Item->Owner->Count - 1;
  10510. }
  10511. }
  10512. return Item->Owner->Item[Index];
  10513. }
  10514. //---------------------------------------------------------------------------
  10515. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10516. {
  10517. TCustomDirView * ADirView = DirView(osCurrent);
  10518. if (ADirView->Items->Count == 0)
  10519. {
  10520. return NULL;
  10521. }
  10522. else
  10523. {
  10524. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  10525. TListItem * Item = CurrentItem;
  10526. if (SkipCurrent)
  10527. {
  10528. Item = DebugNotNull(GetNextFile(Item, Reverse));
  10529. }
  10530. while (true)
  10531. {
  10532. bool Matches = false;
  10533. switch (WinConfiguration->PanelSearch)
  10534. {
  10535. case isNameStartOnly:
  10536. Matches = ContainsTextSemiCaseSensitive(Item->Caption.SubString(1, Text.Length()), Text);
  10537. break;
  10538. case isName:
  10539. Matches = ContainsTextSemiCaseSensitive(Item->Caption, Text);
  10540. break;
  10541. case isAll:
  10542. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  10543. int Index = 0;
  10544. while ((Index < ColCount) && !Matches)
  10545. {
  10546. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  10547. if (Visible)
  10548. {
  10549. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  10550. }
  10551. Index++;
  10552. }
  10553. break;
  10554. }
  10555. if (Matches)
  10556. {
  10557. return Item;
  10558. }
  10559. Item = GetNextFile(Item, Reverse);
  10560. if (Item == CurrentItem)
  10561. {
  10562. return NULL;
  10563. }
  10564. }
  10565. }
  10566. }
  10567. //---------------------------------------------------------------------------
  10568. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10569. {
  10570. ResetIncrementalSearch();
  10571. }
  10572. //---------------------------------------------------------------------------
  10573. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10574. {
  10575. if (Message.CharCode == VK_TAB)
  10576. {
  10577. if (!FIncrementalSearch.IsEmpty())
  10578. {
  10579. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10580. bool Reverse = Shift.Contains(ssShift);
  10581. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10582. Message.Result = 1;
  10583. return;
  10584. }
  10585. }
  10586. else if (Message.CharCode == VK_ESCAPE)
  10587. {
  10588. if (!FIncrementalSearch.IsEmpty())
  10589. {
  10590. ResetIncrementalSearch();
  10591. Message.Result = 1;
  10592. return;
  10593. }
  10594. }
  10595. TForm::Dispatch(&Message);
  10596. }
  10597. //---------------------------------------------------------------------------
  10598. void __fastcall TCustomScpExplorerForm::Deactivate()
  10599. {
  10600. TForm::Deactivate();
  10601. // E.g. when switching to an internal editor window
  10602. ResetIncrementalSearch();
  10603. }
  10604. //---------------------------------------------------------------------------
  10605. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10606. {
  10607. // When switching to another application
  10608. ResetIncrementalSearch();
  10609. }
  10610. //---------------------------------------------------------------------------
  10611. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10612. {
  10613. ResetIncrementalSearch();
  10614. }
  10615. //---------------------------------------------------------------------------
  10616. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10617. {
  10618. if (FIncrementalSearching <= 0)
  10619. {
  10620. ResetIncrementalSearch();
  10621. }
  10622. }
  10623. //---------------------------------------------------------------------------
  10624. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10625. {
  10626. CountClicksForWindowPrint(this);
  10627. }
  10628. //---------------------------------------------------------------------------
  10629. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10630. {
  10631. CenterReconnectToolbar();
  10632. }
  10633. //---------------------------------------------------------------------------
  10634. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10635. {
  10636. if (RemoteDirView->ItemFocused != NULL)
  10637. {
  10638. RemoteDirView->ItemFocused->Selected = true;
  10639. }
  10640. }
  10641. //---------------------------------------------------------------------------
  10642. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  10643. {
  10644. if (!FQueueStatusUpdating)
  10645. {
  10646. bool Refresh;
  10647. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  10648. {
  10649. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  10650. }
  10651. else
  10652. {
  10653. if (FQueueFileList->GetCount() > 0)
  10654. {
  10655. FQueueFileList->Clear();
  10656. Refresh = true;
  10657. }
  10658. }
  10659. int Count = FQueueFileList->GetCount();
  10660. if (QueueFileList->Items->Count != Count)
  10661. {
  10662. DebugAssert(Refresh);
  10663. QueueFileList->Items->Count = Count;
  10664. }
  10665. if (Refresh)
  10666. {
  10667. QueueFileList->Invalidate();
  10668. }
  10669. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  10670. }
  10671. }
  10672. //---------------------------------------------------------------------------
  10673. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  10674. {
  10675. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  10676. }
  10677. //---------------------------------------------------------------------------
  10678. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  10679. {
  10680. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  10681. // See TSynchronizeChecklistDialog::UpdateTimer
  10682. if (QueueFileList->Items->Count > 0)
  10683. {
  10684. QueueFileList->Items->Item[0]->MakeVisible(false);
  10685. }
  10686. UpdateQueueFileList();
  10687. }
  10688. //---------------------------------------------------------------------------
  10689. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  10690. {
  10691. Active = QueueView3->Focused() || QueueFileList->Focused();
  10692. }
  10693. //---------------------------------------------------------------------------
  10694. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  10695. {
  10696. QueueLabelUpdateStatus();
  10697. }
  10698. //---------------------------------------------------------------------------
  10699. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  10700. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  10701. {
  10702. int State = FQueueFileList->GetState(Item->Index);
  10703. if (State == qfsProcessed)
  10704. {
  10705. Sender->Canvas->Font->Color = clGrayText;
  10706. }
  10707. }
  10708. //---------------------------------------------------------------------------
  10709. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  10710. {
  10711. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  10712. QueueFileList->Column[0]->Width =
  10713. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  10714. }
  10715. //---------------------------------------------------------------------------
  10716. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  10717. {
  10718. QueueFileListColumnAutoSize();
  10719. }
  10720. //---------------------------------------------------------------------------
  10721. void __fastcall TCustomScpExplorerForm::CloseApp()
  10722. {
  10723. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  10724. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  10725. // So we clear the flag here.
  10726. DebugAssert(NonVisualDataModule->Busy);
  10727. NonVisualDataModule->EndBusy();
  10728. try
  10729. {
  10730. Close();
  10731. }
  10732. __finally
  10733. {
  10734. NonVisualDataModule->StartBusy();
  10735. }
  10736. }
  10737. //---------------------------------------------------------------------------
  10738. bool __fastcall TCustomScpExplorerForm::IsActiveTerminal(TTerminal * Terminal)
  10739. {
  10740. return (Terminal != NULL) && Terminal->Active;
  10741. }
  10742. //---------------------------------------------------------------------------
  10743. bool __fastcall TCustomScpExplorerForm::HasManagedSession()
  10744. {
  10745. return (ManagedSession != NULL);
  10746. }
  10747. //---------------------------------------------------------------------------
  10748. bool __fastcall TCustomScpExplorerForm::HasActiveTerminal()
  10749. {
  10750. return IsActiveTerminal(Terminal);
  10751. }
  10752. //---------------------------------------------------------------------------
  10753. void TCustomScpExplorerForm::LocalLocalCopy(
  10754. TFileOperation, TOperationSide, bool DebugUsedArg(OnFocused), bool DebugUsedArg(NoConfirmation),
  10755. bool DebugUsedArg(DragDrop), unsigned int DebugUsedArg(Flags))
  10756. {
  10757. DebugFail();
  10758. }
  10759. //---------------------------------------------------------------------------
  10760. void TCustomScpExplorerForm::LocalLocalCopyCommand(TFileOperation Operation, TOperationSide Side, bool OnFocused, unsigned int Flags)
  10761. {
  10762. LocalLocalCopy(Operation, Side, OnFocused, !WinConfiguration->ConfirmTransferring, false, Flags);
  10763. }
  10764. //---------------------------------------------------------------------------
  10765. void __fastcall TCustomScpExplorerForm::SessionsPageControlResize(TObject *)
  10766. {
  10767. // Changing tab list triggers the OnResize.
  10768. if (!FUpdatingSessionTabs)
  10769. {
  10770. // UpdateTabsCaptionTruncation should be enough
  10771. SessionListChanged(true);
  10772. }
  10773. }
  10774. //---------------------------------------------------------------------------
  10775. UnicodeString TCustomScpExplorerForm::GetSessionPath(TManagedTerminal * ASession, TOperationSide Side)
  10776. {
  10777. if (ASession == ManagedSession)
  10778. {
  10779. UpdateSession(ASession);
  10780. }
  10781. TSessionData * Data = ASession->StateData;
  10782. UnicodeString Result;
  10783. if (Side == osLocal)
  10784. {
  10785. Result = Data->LocalDirectory;
  10786. }
  10787. else if (DebugAlwaysTrue(Side == osOther))
  10788. {
  10789. Result = ASession->LocalBrowser ? Data->OtherLocalDirectory : Data->RemoteDirectory;
  10790. }
  10791. return Result;
  10792. }
  10793. //---------------------------------------------------------------------------
  10794. UnicodeString TCustomScpExplorerForm::GetTabHintDetails(TManagedTerminal * ASession)
  10795. {
  10796. UnicodeString Result;
  10797. if (!ASession->Active)
  10798. {
  10799. Result = LoadStr(STATUS_NOT_CONNECTED2);
  10800. }
  10801. else
  10802. {
  10803. Result = GetSessionPath(ASession, osRemote);
  10804. }
  10805. return Result;
  10806. }
  10807. //---------------------------------------------------------------------------
  10808. UnicodeString TCustomScpExplorerForm::GetNewTabHintDetails()
  10809. {
  10810. return LoadStr(NEW_REMOTE_TAB_HINT);
  10811. }
  10812. //---------------------------------------------------------------------------
  10813. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabHint(TPageControl *, int Index, UnicodeString & Hint)
  10814. {
  10815. if (Index >= 0)
  10816. {
  10817. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10818. TManagedTerminal * TabSession = GetSessionTabSession(TabSheet);
  10819. if (TabSession == NULL)
  10820. {
  10821. UnicodeString NewTabTabCaption = GetNewTabTabCaption();
  10822. if (NewTabTabCaption != TabSheet->Caption)
  10823. {
  10824. Hint = NewTabTabCaption + L"|" + GetNewTabHintDetails();
  10825. }
  10826. }
  10827. else
  10828. {
  10829. TTerminalManager * Manager = TTerminalManager::Instance();
  10830. UnicodeString TabCaption = Manager->GetSessionTitle(TabSession, true);
  10831. Hint = TabCaption + L"|" + GetTabHintDetails(TabSession);
  10832. }
  10833. }
  10834. }
  10835. //---------------------------------------------------------------------------
  10836. typedef std::vector<TListItem *> TListItemsVector;
  10837. struct TCalculateSizeOperation
  10838. {
  10839. TCalculateSizeStats Stats;
  10840. TListItemsVector ListItems;
  10841. size_t Index;
  10842. };
  10843. //---------------------------------------------------------------------------
  10844. void TCustomScpExplorerForm::CalculateDirectorySizes()
  10845. {
  10846. TCalculateSizeOperation CalculateSizeOperation;
  10847. TOperationSide Side = GetSide(osCurrent);
  10848. TCustomDirView * ADirView = DirView(Side);
  10849. bool FullPath = IsSideLocalBrowser(Side);
  10850. std::unique_ptr<TStrings> AllFileList(ADirView->CreateFileList(false, FullPath, NULL, true));
  10851. std::unique_ptr<TStrings> FileList(new TStringList());
  10852. for (int Index = 0; Index < AllFileList->Count; Index++)
  10853. {
  10854. TListItem * Item = DebugNotNull(dynamic_cast<TListItem *>(AllFileList->Objects[Index]));
  10855. if (ADirView->ItemIsDirectory(Item))
  10856. {
  10857. FileList->AddObject(AllFileList->Strings[Index], ADirView->ItemData(Item));
  10858. CalculateSizeOperation.ListItems.push_back(Item);
  10859. }
  10860. }
  10861. CalculateSizeOperation.Index = 0;
  10862. __int64 Size = 0;
  10863. TValueRestorer<TCalculateSizeOperation *> DirectorySizeOperationRestorer(FCalculateSizeOperation);
  10864. FCalculateSizeOperation = &CalculateSizeOperation;
  10865. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  10866. FOnFileOperationFinished = DirectorySizeCalculated;
  10867. TValueRestorer<TFileOperation> PrimaryOperationRestorer(FPrimaryOperation);
  10868. FPrimaryOperation = foCalculateSize;
  10869. TCalculatedSizes CalculatedSizes;
  10870. CalculateSizeOperation.Stats.CalculatedSizes = &CalculatedSizes;
  10871. if (Side == osLocal)
  10872. {
  10873. Terminal->CalculateLocalFilesSize(FileList.get(), Size, NULL, true, NULL, &CalculatedSizes);
  10874. }
  10875. else
  10876. {
  10877. TCalculateSizeParams Params;
  10878. Params.Stats = &CalculateSizeOperation.Stats;
  10879. Params.UseCache = true;
  10880. Terminal->CalculateFilesSize(FileList.get(), Size, Params);
  10881. }
  10882. }
  10883. //---------------------------------------------------------------------------
  10884. void __fastcall TCustomScpExplorerForm::DirectorySizeCalculated(TOperationSide Side, const UnicodeString & FileName, bool)
  10885. {
  10886. DebugAlwaysTrue(FCalculateSizeOperation != NULL);
  10887. TListItem * Item = VisualiseOperationFinished(Side, FileName, false);
  10888. TCustomDirView * ADirView = dynamic_cast<TCustomDirView *>(Item->Owner->Owner);
  10889. // optimization
  10890. bool GotExpectedItem =
  10891. (FCalculateSizeOperation->Index < FCalculateSizeOperation->ListItems.size()) &&
  10892. (FCalculateSizeOperation->ListItems[FCalculateSizeOperation->Index] == Item);
  10893. int Index = -1;
  10894. if (DebugAlwaysTrue(GotExpectedItem))
  10895. {
  10896. Index = FCalculateSizeOperation->Index;
  10897. FCalculateSizeOperation->Index++;
  10898. }
  10899. else
  10900. {
  10901. TListItemsVector::iterator I =
  10902. std::find(FCalculateSizeOperation->ListItems.begin(), FCalculateSizeOperation->ListItems.end(), Item);
  10903. if (DebugAlwaysTrue(I != FCalculateSizeOperation->ListItems.end()))
  10904. {
  10905. Index = I - FCalculateSizeOperation->ListItems.begin();
  10906. }
  10907. }
  10908. if (DebugAlwaysTrue(Index >= 0))
  10909. {
  10910. __int64 Size = (*FCalculateSizeOperation->Stats.CalculatedSizes)[Index];
  10911. ADirView->SetItemCalculatedSize(Item, Size);
  10912. }
  10913. }