CustomScpExplorer.cpp 378 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434
  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. FInvalid = false;
  146. FCurrentSide = osRemote;
  147. FEverShown = false;
  148. FDocks = new TList();
  149. InitControls();
  150. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  151. // So that UpdateCustomCommandsToolbar called from RestoreParams works
  152. NonVisualDataModule->ScpExplorer = this;
  153. RestoreParams();
  154. ConfigurationChanged();
  155. RemoteDirView->Invalidate();
  156. FAutoOperation = false;
  157. FOnFileOperationFinished = NULL;
  158. FPrimaryOperation = foNone;
  159. FForceExecution = false;
  160. FIgnoreNextDialogChar = 0;
  161. FErrorList = NULL;
  162. FSynchronizeProgressForm = NULL;
  163. FProgressForm = NULL;
  164. FRefreshLocalDirectory = false;
  165. FRefreshRemoteDirectory = false;
  166. FDDExtMapFile = NULL;
  167. // CreateMutexW keeps failing with ERROR_NOACCESS
  168. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  169. DebugAssert(FDDExtMutex != NULL);
  170. FDDTargetControl = NULL;
  171. FDelayedDeletionTimer = NULL;
  172. FDelayedDeletionList = new TStringList();
  173. FDDFileList = NULL;
  174. FPendingTempSpaceWarn = false;
  175. FCapturedLog = NULL;
  176. FDragDropOperation = false;
  177. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  178. FAllowTransferPresetAutoSelect = true;
  179. FSessionChanging = false;
  180. FCopyParamDefault = L"";
  181. FSynchronizeController = NULL;
  182. FPendingQueueActionItem = NULL;
  183. FLockLevel = 0;
  184. FLockSuspendLevel = 0;
  185. FDisabledOnLockSuspend = false;
  186. FAlternativeDelete = false;
  187. FTrayIcon = new ::TTrayIcon(0);
  188. FTrayIcon->OnClick = TrayIconClick;
  189. FMaxQueueLength = 0;
  190. FLastContextPopupScreenPoint = TPoint(-1, -1);
  191. FTransferResumeList = NULL;
  192. FMoveToQueue = false;
  193. FStandaloneEditing = false;
  194. FOnFeedSynchronizeError = NULL;
  195. FNeedSession = false;
  196. FDoNotIdleCurrentTerminal = 0;
  197. FIncrementalSearching = 0;
  198. FIncrementalSearchHaveNext = false;
  199. FQueueFileList.reset(new TQueueFileList());
  200. FProgressSide = osCurrent;
  201. FCalculateSizeOperation = NULL;
  202. FEditorManager = new TEditorManager();
  203. FEditorManager->OnFileChange = ExecutedFileChanged;
  204. FEditorManager->OnFileReload = ExecutedFileReload;
  205. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  206. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  207. FLocalEditors = new TList();
  208. FQueueStatus = NULL;
  209. FQueueStatusSection = new TCriticalSection();
  210. FQueueStatusInvalidated = false;
  211. FQueueItemInvalidated = false;
  212. FQueueStatusUpdating = false;
  213. FQueueActedItem = NULL;
  214. FQueueController = new TQueueController(QueueView3);
  215. FUserActionTimer = new TTimer(this);
  216. FUserActionTimer->Enabled = false;
  217. FUserActionTimer->Interval = 10;
  218. FUserActionTimer->OnTimer = UserActionTimer;
  219. FNotes = new TStringList();
  220. FNoteTimer = new TTimer(this);
  221. FNoteTimer->Enabled = false;
  222. FNoteTimer->OnTimer = NoteTimer;
  223. FOnNoteClick = NULL;
  224. FOle32Library = LoadLibrary(L"Ole32.dll");
  225. FDragMoveCursor = FOle32Library != NULL ?
  226. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  227. UseSystemSettings(this);
  228. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  229. static_cast<TObject*>(GetComponent(fcTransferList)));
  230. DebugAssert(TransferList != NULL);
  231. FTransferListHoverIndex = -1;
  232. TransferList->OnChange = TransferListChange;
  233. TransferList->OnDrawItem = TransferListDrawItem;
  234. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))), true);
  235. SetSubmenu(NonVisualDataModule->ColorMenuItem, true);
  236. UseDesktopFont(SessionsPageControl);
  237. UpdateSessionsPageControlHeight();
  238. UseDesktopFont(RemoteDirView);
  239. UseDesktopFont(RemoteDriveView);
  240. UseDesktopFont(QueueView3);
  241. UseDesktopFont(QueueFileList);
  242. UseDesktopFont(QueueLabel);
  243. UseDesktopFont(RemoteStatusBar);
  244. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  245. QueueSplitter->ShowHint = true;
  246. reinterpret_cast<TLabel*>(QueueFileListSplitter)->OnDblClick = QueueFileListSplitterDblClick;
  247. QueueFileListSplitter->ShowHint = true;
  248. RemotePanelSplitter->ShowHint = true;
  249. UpdateImages();
  250. FCustomCommandMenu = CreateTBXPopupMenu(this);
  251. FCustomCommandLocalFileList = NULL;
  252. FCustomCommandRemoteFileList = NULL;
  253. FSessionColors = new TPngImageList(this);
  254. FSessionColors->ColorDepth = cd32Bit;
  255. AddFixedSessionImages();
  256. SessionsPageControl->Images = FSessionColors;
  257. UpdateQueueLabel();
  258. CreateHiddenWindow();
  259. StartUpdates();
  260. }
  261. //---------------------------------------------------------------------------
  262. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  263. {
  264. FInvalid = true;
  265. // this has to be one of the very first things to do
  266. StopUpdateThread();
  267. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  268. TTerminalManager::Instance()->ScpExplorer = NULL;
  269. delete FCustomCommandLocalFileList;
  270. delete FCustomCommandRemoteFileList;
  271. delete FCustomCommandMenu;
  272. delete FTrayIcon;
  273. FTrayIcon = NULL;
  274. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  275. delete FEditorManager;
  276. ForceCloseLocalEditors();
  277. delete FLocalEditors;
  278. HideFileFindDialog();
  279. if (FDelayedDeletionTimer)
  280. {
  281. DoDelayedDeletion(NULL);
  282. SAFE_DESTROY(FDelayedDeletionTimer);
  283. }
  284. SAFE_DESTROY(FDelayedDeletionList);
  285. // sometimes we do not get DDEnd so the list is not released
  286. SAFE_DESTROY(FDDFileList);
  287. DebugAssert(FSynchronizeController == NULL);
  288. CloseHandle(FDDExtMutex);
  289. FDDExtMutex = NULL;
  290. FreeLibrary(FOle32Library);
  291. FOle32Library = NULL;
  292. FDragMoveCursor = NULL;
  293. DebugAssert(!FErrorList);
  294. if (FEverShown)
  295. {
  296. // when window is never shown (like when running command-line operation),
  297. // particularly window site is not restored correctly (BoundsRect value set
  298. // in RestoreForm gets lost during handle allocation), so we do not want
  299. // it to be stored
  300. StoreParams();
  301. }
  302. ManagedSession = NULL;
  303. Queue = NULL;
  304. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  305. NonVisualDataModule->ScpExplorer = NULL;
  306. delete FQueueController;
  307. FQueueController = NULL;
  308. delete FQueueStatusSection;
  309. FQueueStatusSection = NULL;
  310. delete FQueueStatus;
  311. FQueueStatus = NULL;
  312. delete FUserActionTimer;
  313. FUserActionTimer = NULL;
  314. delete FNoteTimer;
  315. delete FNotes;
  316. delete FNoteData;
  317. SAFE_DESTROY(FDocks);
  318. SessionsPageControl->Images = NULL;
  319. // only after clearing Terminal (after SessionListChanged => ... => AddSessionColor is called)
  320. SAFE_DESTROY(FSessionColors);
  321. SAFE_DESTROY(FSessionsDragDropFilesEx);
  322. SAFE_DESTROY(FHistoryMenu[0][0]);
  323. SAFE_DESTROY(FHistoryMenu[0][1]);
  324. SAFE_DESTROY(FHistoryMenu[1][0]);
  325. SAFE_DESTROY(FHistoryMenu[1][1]);
  326. if (FHiddenWindow != NULL)
  327. {
  328. DestroyWindow(FHiddenWindow);
  329. FHiddenWindow = NULL;
  330. }
  331. }
  332. //---------------------------------------------------------------------------
  333. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  334. {
  335. std::unique_ptr<TSessionData> Data;
  336. if (!Session.IsEmpty())
  337. {
  338. bool DefaultsOnly;
  339. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  340. }
  341. TTerminalManager * Manager = TTerminalManager::Instance();
  342. for (int Index = 0; Index < Manager->Count; Index++)
  343. {
  344. TTerminal * Terminal = Manager->Sessions[Index];
  345. if (Session.IsEmpty() ||
  346. Manager->IsActiveTerminalForSite(Terminal, Data.get()))
  347. {
  348. if (Path.IsEmpty())
  349. {
  350. Terminal->ClearCaches();
  351. }
  352. else
  353. {
  354. Terminal->DirectoryModified(Path, true);
  355. }
  356. }
  357. }
  358. // We should flag a pending refresh for the background terminals or busy foreground terminals
  359. if (HasActiveTerminal() &&
  360. CanCommandLineFromAnotherInstance() &&
  361. (Session.IsEmpty() || Manager->IsActiveTerminalForSite(Terminal, Data.get())) &&
  362. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  363. {
  364. Terminal->ReloadDirectory();
  365. }
  366. }
  367. //---------------------------------------------------------------------------
  368. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  369. {
  370. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  371. size_t MessageSize = sizeof(TCopyDataMessage);
  372. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  373. if (Result)
  374. {
  375. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  376. Result = (Message.Version == TCopyDataMessage::Version1);
  377. if (Result)
  378. {
  379. switch (Message.Command)
  380. {
  381. case TCopyDataMessage::CommandCanCommandLine:
  382. Result = CanCommandLineFromAnotherInstance();
  383. break;
  384. case TCopyDataMessage::CommandCommandLine:
  385. {
  386. UnicodeString CommandLine(Message.CommandLine);
  387. Result = CommandLineFromAnotherInstance(CommandLine);
  388. }
  389. break;
  390. case TCopyDataMessage::RefreshPanel:
  391. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  392. break;
  393. case TCopyDataMessage::MainWindowCheck:
  394. Result = true;
  395. break;
  396. default:
  397. Result = false;
  398. break;
  399. }
  400. }
  401. }
  402. Message.Result = Result ? 1 : 0;
  403. }
  404. //---------------------------------------------------------------------------
  405. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  406. {
  407. WNDCLASS WindowClass = {0};
  408. WindowClass.lpfnWndProc = DefWindowProc;
  409. WindowClass.hInstance = HInstance;
  410. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  411. FHiddenWindow = NULL;
  412. if (RegisterClass(&WindowClass))
  413. {
  414. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  415. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  416. }
  417. }
  418. //---------------------------------------------------------------------------
  419. bool __fastcall TCustomScpExplorerForm::CanConsole()
  420. {
  421. return
  422. HasActiveTerminal() &&
  423. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  424. }
  425. //---------------------------------------------------------------------------
  426. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  427. {
  428. bool Result = Visible && !NonVisualDataModule->Busy;
  429. return Result;
  430. }
  431. //---------------------------------------------------------------------------
  432. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  433. const UnicodeString & CommandLine)
  434. {
  435. TProgramParams Params(CommandLine);
  436. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  437. if (Result)
  438. {
  439. NonVisualDataModule->StartBusy();
  440. try
  441. {
  442. // this action is initiated from another process,
  443. // so it's likely that our window is not visible,
  444. // and user won't see what is going on
  445. Application->BringToFront();
  446. // reload sessions as we may be asked to open a session
  447. // just stored by another instance
  448. StoredSessions->Reload();
  449. UnicodeString SessionName = Params.Param[1];
  450. std::unique_ptr<TObjectList> DataList(new TObjectList());
  451. UnicodeString DownloadFile; // unused
  452. try
  453. {
  454. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this);
  455. }
  456. catch (EAbort &)
  457. {
  458. // Happens when opening session in PuTTY and in other situations,
  459. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  460. // to process the command-line on its own (or to pass it to yet another instance)
  461. }
  462. if (DataList->Count > 0)
  463. {
  464. TTerminalManager * Manager = TTerminalManager::Instance();
  465. Manager->ActiveSession = Manager->NewSessions(DataList.get());
  466. }
  467. }
  468. __finally
  469. {
  470. NonVisualDataModule->EndBusy();
  471. }
  472. }
  473. return Result;
  474. }
  475. //---------------------------------------------------------------------------
  476. TManagedTerminal * TCustomScpExplorerForm::GetTerminal()
  477. {
  478. TManagedTerminal * Result;
  479. if ((FManagedSession != NULL) && !FManagedSession->LocalBrowser)
  480. {
  481. Result = FManagedSession;
  482. }
  483. else
  484. {
  485. Result = NULL;
  486. }
  487. return Result;
  488. }
  489. //---------------------------------------------------------------------------
  490. void __fastcall TCustomScpExplorerForm::SetManagedSession(TManagedTerminal * value)
  491. {
  492. if (FManagedSession != value)
  493. {
  494. SessionChanging();
  495. DoSetManagedSession(value, false);
  496. }
  497. }
  498. //---------------------------------------------------------------------------
  499. void __fastcall TCustomScpExplorerForm::DoSetManagedSession(TManagedTerminal * value, bool Replace)
  500. {
  501. DebugAssert(!Replace || ((value != NULL) && !value->LocalBrowser));
  502. FManagedSession = value;
  503. {
  504. TValueRestorer<bool> AllowTransferPresetAutoSelectRestorer(FAllowTransferPresetAutoSelect);
  505. FAllowTransferPresetAutoSelect = false;
  506. TAutoFlag SessionChangingFlag(FSessionChanging);
  507. SessionChanged(Replace);
  508. }
  509. if (Terminal != NULL)
  510. {
  511. TransferPresetAutoSelect();
  512. }
  513. // Update app and tab titles, prevented above by FSessionChanging.
  514. UpdateControls();
  515. }
  516. //---------------------------------------------------------------------------
  517. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  518. {
  519. DoSetManagedSession(value, true);
  520. }
  521. //---------------------------------------------------------------------------
  522. void __fastcall TCustomScpExplorerForm::SessionChanging()
  523. {
  524. if (ManagedSession != NULL)
  525. {
  526. UpdateSession(ManagedSession);
  527. }
  528. }
  529. //---------------------------------------------------------------------------
  530. void __fastcall TCustomScpExplorerForm::SessionChanged(bool Replaced)
  531. {
  532. if (Terminal != NULL)
  533. {
  534. UpdateSessionColor((TColor)Terminal->StateData->Color);
  535. }
  536. if (!TTerminalManager::Instance()->Updating)
  537. {
  538. SessionListChanged();
  539. }
  540. if (Terminal != NULL)
  541. {
  542. TObject * State = NULL;
  543. if (WinConfiguration->PreservePanelState)
  544. {
  545. State = Terminal->RemoteExplorerState;
  546. DirView(osRemote)->AnnounceState(State);
  547. }
  548. try
  549. {
  550. if (Replaced)
  551. {
  552. RemoteDirView->ReplaceTerminal(Terminal);
  553. }
  554. else
  555. {
  556. RemoteDirView->Terminal = Terminal;
  557. }
  558. if (Terminal->Active)
  559. {
  560. Terminal->RefreshDirectory();
  561. }
  562. if (State != NULL)
  563. {
  564. DirView(osRemote)->RestoreState(State);
  565. }
  566. }
  567. __finally
  568. {
  569. DirView(osRemote)->AnnounceState(NULL);
  570. }
  571. }
  572. else
  573. {
  574. RemoteDirView->Terminal = NULL;
  575. }
  576. InitStatusBar();
  577. UpdateTransferList();
  578. // Update panels Enable state before refreshing the labels
  579. UpdateControls();
  580. }
  581. //---------------------------------------------------------------------------
  582. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  583. {
  584. if (Queue != value)
  585. {
  586. if (FQueue != NULL)
  587. {
  588. FQueue->OnListUpdate = NULL;
  589. FQueue->OnQueueItemUpdate = NULL;
  590. }
  591. FQueue = value;
  592. if (FQueue != NULL)
  593. {
  594. DebugAssert(FQueue->OnListUpdate == NULL);
  595. FQueue->OnListUpdate = QueueListUpdate;
  596. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  597. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  598. }
  599. QueueChanged();
  600. }
  601. }
  602. //---------------------------------------------------------------------------
  603. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  604. TListItem * Item)
  605. {
  606. if (FQueueActedItem == Item)
  607. {
  608. FQueueActedItem = NULL;
  609. if ((QueueView3->PopupMenu != NULL) &&
  610. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  611. {
  612. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  613. ReleaseCapture();
  614. }
  615. }
  616. if (Item->Data == FPendingQueueActionItem)
  617. {
  618. FPendingQueueActionItem = NULL;
  619. }
  620. }
  621. //---------------------------------------------------------------------------
  622. bool TCustomScpExplorerForm::IsAnythingQueued()
  623. {
  624. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  625. }
  626. //---------------------------------------------------------------------------
  627. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  628. {
  629. {
  630. TGuard Guard(FQueueStatusSection);
  631. FQueueStatusInvalidated = false;
  632. if (FQueue != NULL)
  633. {
  634. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  635. }
  636. }
  637. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  638. {
  639. FMaxQueueLength = FQueueStatus->Count;
  640. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  641. }
  642. {
  643. TAutoFlag Flag(FQueueStatusUpdating);
  644. FQueueController->UpdateQueueStatus(FQueueStatus);
  645. }
  646. UpdateQueueFileList();
  647. SetQueueProgress();
  648. UpdateQueueView();
  649. bool IsEmpty = !IsAnythingQueued();
  650. if (IsEmpty && (Terminal != NULL))
  651. {
  652. TOnceDoneOperation OnceDoneOperation =
  653. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  654. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  655. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  656. {
  657. FQueue->Enabled = false;
  658. }
  659. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  660. {
  661. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  662. }
  663. }
  664. }
  665. //---------------------------------------------------------------------------
  666. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  667. {
  668. UnicodeString Result;
  669. if (FQueueStatus != NULL)
  670. {
  671. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  672. {
  673. TFileOperationProgressType * ProgressData =
  674. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  675. if ((ProgressData != NULL) && ProgressData->InProgress)
  676. {
  677. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  678. }
  679. }
  680. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  681. {
  682. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  683. }
  684. }
  685. return Result;
  686. }
  687. //---------------------------------------------------------------------------
  688. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  689. {
  690. ComponentVisible[fcQueueView] =
  691. !IsLocalBrowserMode() &&
  692. ((WinConfiguration->QueueView.Show == qvShow) ||
  693. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  694. (FQueueStatus != NULL) && (FQueueStatus->Count > 0)));
  695. }
  696. //---------------------------------------------------------------------------
  697. void __fastcall TCustomScpExplorerForm::QueueChanged()
  698. {
  699. if (FQueueStatus != NULL)
  700. {
  701. // UpdateFileList implementation relies on the status being recreated when session changes
  702. delete FQueueStatus;
  703. FQueueStatus = NULL;
  704. }
  705. UpdateQueueStatus(true);
  706. }
  707. //---------------------------------------------------------------------------
  708. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  709. {
  710. if (FQueue == Queue)
  711. {
  712. FQueueStatusInvalidated = true;
  713. }
  714. }
  715. //---------------------------------------------------------------------------
  716. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  717. TQueueItem * Item)
  718. {
  719. if (FQueue == Queue)
  720. {
  721. TGuard Guard(FQueueStatusSection);
  722. // this may be running in parallel with QueueChanged
  723. if (FQueueStatus != NULL)
  724. {
  725. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  726. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  727. {
  728. FRefreshLocalDirectory = (QueueItem == NULL) ||
  729. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  730. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  731. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  732. }
  733. if (QueueItem != NULL)
  734. {
  735. QueueItem->UserData = (void*)true;
  736. FQueueItemInvalidated = true;
  737. }
  738. }
  739. }
  740. }
  741. //---------------------------------------------------------------------------
  742. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  743. {
  744. // during standalone editing, we have no way to see/control queue,
  745. // so we have to always popup prompts automatically
  746. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  747. }
  748. //---------------------------------------------------------------------------
  749. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  750. {
  751. if (FQueueStatus != NULL)
  752. {
  753. bool QueueAutoPopup = IsQueueAutoPopup();
  754. bool NeedRefresh = FQueueController->NeedRefresh();
  755. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  756. FQueueItemInvalidated = false;
  757. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  758. bool Updated = false;
  759. TQueueItemProxy * QueueItem;
  760. bool WasUserAction;
  761. for (int Index = 0; Index < Limit; Index++)
  762. {
  763. bool Update = false;
  764. QueueItem = FQueueStatus->Items[Index];
  765. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  766. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  767. (FPendingQueueActionItem == NULL))
  768. {
  769. FPendingQueueActionItem = QueueItem;
  770. FUserActionTimer->Enabled = true;
  771. }
  772. if ((bool)QueueItem->UserData)
  773. {
  774. QueueItem->UserData = (void*)false;
  775. QueueItem->Update();
  776. Updated = true;
  777. Update = true;
  778. }
  779. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  780. {
  781. Update = true;
  782. }
  783. if (Update)
  784. {
  785. FQueueController->RefreshQueueItem(QueueItem);
  786. }
  787. }
  788. if (Updated)
  789. {
  790. NonVisualDataModule->UpdateNonVisibleActions();
  791. SetQueueProgress();
  792. UpdateQueueFileList();
  793. }
  794. }
  795. }
  796. //---------------------------------------------------------------------------
  797. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  798. {
  799. if (FTaskbarList != NULL)
  800. {
  801. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  802. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  803. }
  804. }
  805. //---------------------------------------------------------------------------
  806. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  807. {
  808. if (Progress >= 0)
  809. {
  810. if (FTaskbarList != NULL)
  811. {
  812. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  813. }
  814. }
  815. else
  816. {
  817. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  818. }
  819. }
  820. //---------------------------------------------------------------------------
  821. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  822. {
  823. int OverallProgress;
  824. // FProgressForm is null when this is called from SetQueueProgress
  825. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  826. {
  827. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  828. }
  829. else if (!TFileOperationProgressType::IsIndeterminateOperation(ProgressData->Operation))
  830. {
  831. OverallProgress = ProgressData->OverallProgress();
  832. }
  833. else
  834. {
  835. OverallProgress = -1;
  836. }
  837. SetTaskbarListProgressValue(OverallProgress);
  838. }
  839. //---------------------------------------------------------------------------
  840. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  841. {
  842. TTerminalManager::Instance()->QueueStatusUpdated();
  843. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  844. {
  845. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  846. {
  847. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  848. {
  849. TFileOperationProgressType * ProgressData = NULL;
  850. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  851. {
  852. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  853. }
  854. if ((ProgressData != NULL) &&
  855. ProgressData->InProgress)
  856. {
  857. SetTaskbarListProgressValue(ProgressData);
  858. }
  859. else
  860. {
  861. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  862. }
  863. }
  864. else
  865. {
  866. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  867. }
  868. }
  869. else
  870. {
  871. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  872. }
  873. }
  874. UpdateQueueLabel();
  875. }
  876. //---------------------------------------------------------------------------
  877. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  878. {
  879. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  880. if (FQueueStatus != NULL)
  881. {
  882. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  883. if (ActiveAndPendingPrimaryCount > 0)
  884. {
  885. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  886. }
  887. }
  888. QueueLabel->Caption = Caption;
  889. }
  890. //---------------------------------------------------------------------------
  891. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  892. {
  893. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  894. static_cast<TComponent*>(GetComponent(fcTransferList)));
  895. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  896. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  897. TransferList->Strings->BeginUpdate();
  898. try
  899. {
  900. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  901. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  902. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  903. if (FTransferDropDownHint.IsEmpty())
  904. {
  905. FTransferDropDownHint = TransferDropDown->Hint;
  906. }
  907. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  908. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  909. TransferDropDown->Text = StripHotkey(Name);
  910. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  911. (FTransferDropDownHint, StripHotkey(Name),
  912. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  913. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General))));
  914. // update the label, otherwise when it is updated only on the first draw
  915. // of the list, it is drawn "bold" for some reason
  916. FTransferListHoverIndex = TransferList->ItemIndex;
  917. UpdateTransferLabel();
  918. }
  919. __finally
  920. {
  921. TransferList->Strings->EndUpdate();
  922. }
  923. }
  924. //---------------------------------------------------------------------------
  925. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  926. {
  927. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  928. DebugAssert(Toolbar != NULL);
  929. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  930. }
  931. //---------------------------------------------------------------------------
  932. void __fastcall TCustomScpExplorerForm::UpdateActions()
  933. {
  934. TForm::UpdateActions();
  935. if (ComponentVisible[fcCustomCommandsBand])
  936. {
  937. UpdateCustomCommandsToolbar();
  938. }
  939. }
  940. //---------------------------------------------------------------------------
  941. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  942. {
  943. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  944. }
  945. //---------------------------------------------------------------------------
  946. void __fastcall TCustomScpExplorerForm::UpdateRowSelect(TCustomDirView * DirView)
  947. {
  948. if (DirView->RowSelect != WinConfiguration->FullRowSelect)
  949. {
  950. DirView->RowSelect = WinConfiguration->FullRowSelect;
  951. // selection is not redrawn automatically when RowSelect changes
  952. DirView->Invalidate();
  953. }
  954. }
  955. //---------------------------------------------------------------------------
  956. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  957. {
  958. Color = GetBtnFaceColor();
  959. DebugAssert(Configuration && RemoteDirView);
  960. RemoteDirView->DDAllowMove = !WinConfiguration->DDDisableMove;
  961. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  962. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  963. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  964. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  965. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  966. UpdateRowSelect(RemoteDirView);
  967. RemoteDriveView->DDAllowMove = !WinConfiguration->DDDisableMove;
  968. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  969. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  970. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  971. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  972. UpdateSessionsPageControlHeight();
  973. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  974. UpdateToolbarDisplayMode();
  975. UpdateTransferList();
  976. if (Terminal != NULL)
  977. {
  978. TransferPresetAutoSelect();
  979. }
  980. UpdateQueueView();
  981. UpdateControls();
  982. // this can be called even before constuctor finishes.
  983. if (FEditorManager != NULL)
  984. {
  985. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  986. }
  987. // this can be called even before constuctor finishes.
  988. if (FLocalEditors != NULL)
  989. {
  990. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  991. {
  992. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  993. }
  994. }
  995. if (ComponentVisible[fcCustomCommandsBand])
  996. {
  997. UpdateCustomCommandsToolbar();
  998. }
  999. // show only when keeping queue items forever,
  1000. // otherwise, its enough to have in in the context menu
  1001. QueueDeleteAllDoneQueueToolbarItem->Visible =
  1002. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  1003. if (FFileColorsCurrent != WinConfiguration->FileColors)
  1004. {
  1005. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  1006. FFileColorsCurrent = WinConfiguration->FileColors;
  1007. FileColorsChanged();
  1008. }
  1009. }
  1010. //---------------------------------------------------------------------------
  1011. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  1012. {
  1013. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  1014. DirView->Invalidate();
  1015. }
  1016. //---------------------------------------------------------------------------
  1017. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  1018. {
  1019. DoFileColorsChanged(RemoteDirView);
  1020. }
  1021. //---------------------------------------------------------------------------
  1022. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  1023. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  1024. void * /*Arg*/)
  1025. {
  1026. if (Token != NULL)
  1027. {
  1028. TForm * Editor = dynamic_cast<TForm*>(Token);
  1029. DebugAssert(Editor != NULL);
  1030. ReconfigureEditorForm(Editor);
  1031. }
  1032. }
  1033. //---------------------------------------------------------------------------
  1034. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  1035. {
  1036. WinConfiguration->DDTransferConfirmation = asOn;
  1037. }
  1038. //---------------------------------------------------------------------------
  1039. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1040. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1041. {
  1042. // noop
  1043. }
  1044. //---------------------------------------------------------------------------
  1045. bool TCustomScpExplorerForm::GetDoNotShowCopyDialogDefault(bool DragDrop)
  1046. {
  1047. return DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto);
  1048. }
  1049. //---------------------------------------------------------------------------
  1050. void TCustomScpExplorerForm::HandleDoNotShowCopyDialogAgain(bool DragDrop, bool DoNotShowAgain)
  1051. {
  1052. if (DoNotShowAgain)
  1053. {
  1054. if (DragDrop)
  1055. {
  1056. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1057. {
  1058. PopupTrayBalloon(
  1059. NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation, NULL, 0, EnableDDTransferConfirmation, NULL);
  1060. }
  1061. WinConfiguration->DDTransferConfirmation = asOff;
  1062. }
  1063. else
  1064. {
  1065. WinConfiguration->ConfirmTransferring = false;
  1066. }
  1067. }
  1068. else
  1069. {
  1070. // User explicitly unchecked "do not show again",
  1071. // so show him the dialog the next time
  1072. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1073. {
  1074. WinConfiguration->DDTransferConfirmation = asOn;
  1075. }
  1076. }
  1077. }
  1078. //---------------------------------------------------------------------------
  1079. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1080. TTransferDirection Direction, TTransferType Type, bool Temp,
  1081. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1082. bool Confirm, bool DragDrop, int Options)
  1083. {
  1084. bool Result = true;
  1085. DebugAssert(HasActiveTerminal());
  1086. // these parameters are known in advance
  1087. int Params =
  1088. FLAGMASK(Type == ttMove, cpDelete);
  1089. bool ToTemp = (Temp && (Direction == tdToLocal));
  1090. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1091. {
  1092. bool DisableNewerOnly =
  1093. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1094. ToTemp;
  1095. Options |=
  1096. FLAGMASK(ToTemp, coTemp) |
  1097. FLAGMASK(!Terminal->IsCapable[fcBackgroundTransfers], coDisableQueue) |
  1098. coDoNotShowAgain;
  1099. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1100. int CopyParamAttrs = (Direction == tdToRemote ?
  1101. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1102. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1103. int OutputOptions = FLAGMASK(GetDoNotShowCopyDialogDefault(DragDrop), cooDoNotShowAgain);
  1104. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1105. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1106. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1107. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1108. if (Result)
  1109. {
  1110. HandleDoNotShowCopyDialogAgain(DragDrop, FLAGSET(OutputOptions, cooDoNotShowAgain));
  1111. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1112. }
  1113. }
  1114. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fcBackgroundTransfers])
  1115. {
  1116. Configuration->Usage->Inc(L"TransfersOnBackground");
  1117. // these parameter are known only after transfer dialog
  1118. Params |=
  1119. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1120. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1121. Result = false;
  1122. ClearTransferSourceSelection(Direction);
  1123. }
  1124. return Result;
  1125. }
  1126. //---------------------------------------------------------------------------
  1127. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1128. {
  1129. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1130. if (FOnFileOperationFinished != NULL)
  1131. {
  1132. FOnFileOperationFinished(Side, UnicodeString(), true);
  1133. }
  1134. else
  1135. {
  1136. if (HasDirView[Side])
  1137. {
  1138. DirView(Side)->SelectAll(smNone);
  1139. }
  1140. }
  1141. }
  1142. //---------------------------------------------------------------------------
  1143. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1144. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1145. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1146. int Params)
  1147. {
  1148. DebugAssert(Queue != NULL);
  1149. bool SingleFile = false;
  1150. if (FileList->Count == 1)
  1151. {
  1152. if (Direction == tdToRemote)
  1153. {
  1154. UnicodeString FileName = FileList->Strings[0];
  1155. SingleFile = FileExists(ApiPath(FileName));
  1156. }
  1157. else
  1158. {
  1159. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1160. SingleFile = !File->IsDirectory;
  1161. }
  1162. }
  1163. TQueueItem * QueueItem;
  1164. if (Direction == tdToRemote)
  1165. {
  1166. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1167. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1168. }
  1169. else
  1170. {
  1171. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1172. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1173. }
  1174. AddQueueItem(Queue, QueueItem, Terminal);
  1175. }
  1176. //---------------------------------------------------------------------------
  1177. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1178. {
  1179. if (Queue->IsEmpty)
  1180. {
  1181. Terminal->QueueOperationStart = Now();
  1182. }
  1183. Queue->AddItem(QueueItem);
  1184. }
  1185. //---------------------------------------------------------------------------
  1186. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1187. {
  1188. }
  1189. //---------------------------------------------------------------------------
  1190. void __fastcall TCustomScpExplorerForm::InitControls()
  1191. {
  1192. DebugAssert(FDocks != NULL);
  1193. for (int Index = 0; Index < ComponentCount; Index++)
  1194. {
  1195. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1196. if ((Dock != NULL) && (Dock->Tag == 0))
  1197. {
  1198. FDocks->Add(Dock);
  1199. }
  1200. }
  1201. CollectItemsWithTextDisplayMode(this);
  1202. }
  1203. //---------------------------------------------------------------------------
  1204. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1205. {
  1206. ConfigurationChanged();
  1207. QueuePanel->Height =
  1208. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1209. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1210. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1211. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1212. QueueFileList->Visible = WinConfiguration->QueueView.FileList;
  1213. QueueFileList->Height =
  1214. LoadDimension(WinConfiguration->QueueView.FileListHeight, WinConfiguration->QueueView.FileListHeightPixelsPerInch, this);
  1215. if (QueueFileList->Visible)
  1216. {
  1217. AdjustQueueLayout();
  1218. }
  1219. UpdateCustomCommandsToolbar();
  1220. }
  1221. //---------------------------------------------------------------------------
  1222. void __fastcall TCustomScpExplorerForm::StoreParams()
  1223. {
  1224. int APixelsPerInch = GetControlPixelsPerInch(this);
  1225. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1226. WinConfiguration->QueueView.HeightPixelsPerInch = APixelsPerInch;
  1227. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1228. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1229. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1230. WinConfiguration->QueueView.FileList = QueueFileList->Visible;
  1231. WinConfiguration->QueueView.FileListHeight = QueueFileList->Height;
  1232. WinConfiguration->QueueView.FileListHeightPixelsPerInch = APixelsPerInch;
  1233. }
  1234. //---------------------------------------------------------------------------
  1235. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1236. {
  1237. if (!FFormRestored)
  1238. {
  1239. FFormRestored = true;
  1240. RestoreFormParams();
  1241. }
  1242. TForm::CreateParams(Params);
  1243. }
  1244. //---------------------------------------------------------------------------
  1245. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1246. {
  1247. DebugAssert(FDocks != NULL);
  1248. for (int Index = 0; Index < FDocks->Count; Index++)
  1249. {
  1250. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1251. }
  1252. }
  1253. //---------------------------------------------------------------------------
  1254. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1255. {
  1256. SetDockAllowDrag(true);
  1257. while (!ButtonsStr.IsEmpty())
  1258. {
  1259. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1260. UnicodeString Name = CutToChar(S, L'=', true);
  1261. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1262. {
  1263. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1264. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1265. {
  1266. while (!S.IsEmpty())
  1267. {
  1268. UnicodeString S2 = CutToChar(S, L',', true);
  1269. UnicodeString Name = CutToChar(S2, L':', true);
  1270. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1271. {
  1272. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1273. if (SameText(GetToolbarItemName(Item), Name))
  1274. {
  1275. // forward compatibility
  1276. UnicodeString S3 = CutToChar(S2, L':', true);
  1277. bool Visible = StrToIntDef(S3, 0);
  1278. Item->Visible = Visible;
  1279. }
  1280. }
  1281. }
  1282. }
  1283. }
  1284. }
  1285. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1286. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1287. }
  1288. //---------------------------------------------------------------------------
  1289. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1290. {
  1291. UnicodeString Result;
  1292. SetDockAllowDrag(true);
  1293. Result = ::GetToolbarsLayoutStr(this);
  1294. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1295. return Result;
  1296. }
  1297. //---------------------------------------------------------------------------
  1298. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1299. {
  1300. UnicodeString Result;
  1301. if (Item->Action != NULL)
  1302. {
  1303. Result = Item->Action->Name;
  1304. Result = RemoveSuffix(Result, L"Action", true);
  1305. }
  1306. else
  1307. {
  1308. Result = Item->Name;
  1309. Result = RemoveSuffix(Result, L"SubmenuItem", true);
  1310. Result = RemoveSuffix(Result, L"Item", true);
  1311. }
  1312. return Result;
  1313. }
  1314. //---------------------------------------------------------------------------
  1315. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1316. {
  1317. UnicodeString Result;
  1318. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1319. {
  1320. UnicodeString ToolbarStr;
  1321. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1322. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1323. {
  1324. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1325. {
  1326. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1327. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1328. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1329. // and save the changes here only.
  1330. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1331. {
  1332. UnicodeString Name = GetToolbarItemName(Item);
  1333. DebugAssert(Name.Pos(L"TBX") == 0);
  1334. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1335. }
  1336. }
  1337. }
  1338. if (!ToolbarStr.IsEmpty())
  1339. {
  1340. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1341. }
  1342. }
  1343. return Result;
  1344. }
  1345. //---------------------------------------------------------------------------
  1346. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1347. {
  1348. DebugAssert(FProgressForm == NULL);
  1349. bool AllowSkip = HasActiveTerminal() ? Terminal->IsCapable[fcSkipTransfer] : false;
  1350. FProgressForm = new TProgressForm(Application, (FTransferResumeList != NULL), AllowSkip, SynchronizeProgress);
  1351. FProgressForm->DeleteLocalToRecycleBin =
  1352. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1353. FProgressForm->DeleteRemoteToRecycleBin =
  1354. HasActiveTerminal() &&
  1355. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1356. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1357. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1358. LockWindow();
  1359. // Actually, do not know what hides the progress once the operation finishes
  1360. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1361. SetTaskbarListProgressState(TBPF_NORMAL);
  1362. }
  1363. //---------------------------------------------------------------------------
  1364. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1365. {
  1366. UnlockWindow();
  1367. SAFE_DESTROY(FProgressForm);
  1368. SetQueueProgress();
  1369. }
  1370. //---------------------------------------------------------------------------
  1371. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1372. TFileOperationProgressType & ProgressData)
  1373. {
  1374. // Download to temporary local directory
  1375. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1376. {
  1377. bool Continue = true;
  1378. FPendingTempSpaceWarn = false;
  1379. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1380. if (!Continue)
  1381. {
  1382. Abort();
  1383. }
  1384. }
  1385. // operation is being executed and we still didn't show up progress form
  1386. if (ProgressData.InProgress && !FProgressForm)
  1387. {
  1388. CreateProgressForm(NULL);
  1389. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1390. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1391. }
  1392. // operation is finished (or terminated), so we hide progress form
  1393. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1394. {
  1395. DestroyProgressForm();
  1396. if (ProgressData.Operation == foCalculateSize)
  1397. {
  1398. // When calculating size before transfer, the abort caused by
  1399. // cancel flag set due to "MoveToQueue" below,
  1400. // is not propagated back to us in ExecuteFileOperation,
  1401. // as it is silently swallowed.
  1402. // So we explicitly (re)throw it here.
  1403. if (FMoveToQueue)
  1404. {
  1405. Abort();
  1406. }
  1407. }
  1408. else
  1409. {
  1410. if ((ProgressData.Cancel == csContinue) &&
  1411. !FAutoOperation)
  1412. {
  1413. OperationComplete(ProgressData.StartTime);
  1414. }
  1415. }
  1416. }
  1417. if (FProgressForm &&
  1418. // Can happen only during synchronization, when ending one of the operations
  1419. ProgressData.InProgress)
  1420. {
  1421. FProgressForm->SetProgressData(ProgressData);
  1422. DebugAssert(ProgressData.InProgress);
  1423. SetTaskbarListProgressValue(&ProgressData);
  1424. if (FProgressForm->Cancel > csContinue)
  1425. {
  1426. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1427. if (FProgressForm->Cancel == csCancelFile)
  1428. {
  1429. FProgressForm->ClearCancel();
  1430. }
  1431. // cancel cancels even the move
  1432. FMoveToQueue = false;
  1433. }
  1434. else if (FProgressForm->MoveToQueue)
  1435. {
  1436. FMoveToQueue = true;
  1437. ProgressData.SetCancelAtLeast(csCancel);
  1438. }
  1439. if ((FTransferResumeList != NULL) &&
  1440. ProgressData.InProgress &&
  1441. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1442. !ProgressData.FullFileName.IsEmpty())
  1443. {
  1444. if ((FTransferResumeList->Count == 0) ||
  1445. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1446. {
  1447. // note that we do not recognize directories from files here
  1448. FTransferResumeList->Add(ProgressData.FullFileName);
  1449. }
  1450. }
  1451. }
  1452. }
  1453. //---------------------------------------------------------------------------
  1454. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1455. const TDateTime & StartTime)
  1456. {
  1457. if (GUIConfiguration->BeepOnFinish &&
  1458. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1459. {
  1460. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1461. DWORD Sound;
  1462. if (!ExtractFileExt(BeepSound).IsEmpty())
  1463. {
  1464. Sound = SND_FILENAME;
  1465. }
  1466. else
  1467. {
  1468. Sound = SND_ALIAS;
  1469. }
  1470. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1471. }
  1472. }
  1473. //---------------------------------------------------------------------------
  1474. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1475. TFileOperationProgressType & ProgressData)
  1476. {
  1477. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1478. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1479. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1480. FileOperationProgress(ProgressData);
  1481. }
  1482. //---------------------------------------------------------------------------
  1483. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1484. {
  1485. UnicodeString Result;
  1486. if (FProgressForm != NULL)
  1487. {
  1488. Result = FProgressForm->ProgressStr();
  1489. }
  1490. return Result;
  1491. }
  1492. //---------------------------------------------------------------------------
  1493. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1494. bool DragDrop)
  1495. {
  1496. return (!DragDrop && (DropSourceControl == NULL)) ||
  1497. (DropSourceControl == DirView(osOther));
  1498. }
  1499. //---------------------------------------------------------------------------
  1500. TListItem * TCustomScpExplorerForm::VisualiseOperationFinished(TOperationSide Side, const UnicodeString & FileName, bool Unselect)
  1501. {
  1502. TCustomDirView * ADirView = DirView(Side);
  1503. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1504. TListItem * Item = ADirView->FindFileItem(FileNameOnly);
  1505. // this can happen when local drive is unplugged in the middle of the operation
  1506. if (Item != NULL)
  1507. {
  1508. if (Unselect)
  1509. {
  1510. Item->Selected = false;
  1511. }
  1512. ADirView->MakeProgressVisible(Item);
  1513. }
  1514. return Item;
  1515. }
  1516. //---------------------------------------------------------------------------
  1517. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1518. TFileOperation Operation, TOperationSide Side,
  1519. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1520. TOnceDoneOperation & OnceDoneOperation)
  1521. {
  1522. if (!FAutoOperation)
  1523. {
  1524. // no selection on "/upload", form serves only as event handler
  1525. // (it is not displayed)
  1526. if (PanelOperation(Side, FDragDropOperation) && Visible &&
  1527. ((Operation == FPrimaryOperation) ||
  1528. ((Operation != foCalculateSize) && (Operation != foGetProperties) && (Operation != foCalculateChecksum))))
  1529. {
  1530. TOperationSide DViewSide;
  1531. if ((FProgressSide != osCurrent) &&
  1532. IsLocalBrowserMode() && (Side == osLocal)) // Only to limit the impact
  1533. {
  1534. // assume the operation is over the focused panel
  1535. DViewSide = FProgressSide;
  1536. }
  1537. else
  1538. {
  1539. DViewSide = Side;
  1540. }
  1541. if (FOnFileOperationFinished != NULL)
  1542. {
  1543. FOnFileOperationFinished(DViewSide, FileName, Success);
  1544. }
  1545. else
  1546. {
  1547. VisualiseOperationFinished(DViewSide, FileName, Success);
  1548. }
  1549. }
  1550. if ((Operation == foCopy) || (Operation == foMove))
  1551. {
  1552. if (DebugAlwaysTrue(!IsLocalBrowserMode()))
  1553. {
  1554. if (Side == osLocal)
  1555. {
  1556. Configuration->Usage->Inc(L"UploadedFiles");
  1557. }
  1558. else
  1559. {
  1560. Configuration->Usage->Inc(L"DownloadedFiles");
  1561. }
  1562. }
  1563. }
  1564. }
  1565. if (Success && (FSynchronizeController != NULL))
  1566. {
  1567. DebugAssert(!IsSideLocalBrowser(osRemote));
  1568. if (Operation == foCopy)
  1569. {
  1570. DebugAssert(Side == osLocal);
  1571. FSynchronizeController->LogOperation(soUpload, FileName);
  1572. }
  1573. else if (Operation == foDelete)
  1574. {
  1575. DebugAssert(Side == osRemote);
  1576. FSynchronizeController->LogOperation(soDelete, FileName);
  1577. }
  1578. }
  1579. if (FProgressForm)
  1580. {
  1581. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1582. }
  1583. }
  1584. //---------------------------------------------------------------------------
  1585. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1586. TFileOperation Operation, TOperationSide Side,
  1587. bool Temp, const UnicodeString & FileName, Boolean Success,
  1588. TOnceDoneOperation & OnceDoneOperation)
  1589. {
  1590. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1591. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1592. OnceDoneOperation);
  1593. }
  1594. //---------------------------------------------------------------------------
  1595. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1596. {
  1597. return false;
  1598. }
  1599. //---------------------------------------------------------------------------
  1600. bool TCustomScpExplorerForm::SupportsLocalBrowser()
  1601. {
  1602. return false;
  1603. }
  1604. //---------------------------------------------------------------------------
  1605. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1606. {
  1607. return false;
  1608. }
  1609. //---------------------------------------------------------------------------
  1610. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1611. {
  1612. return NULL;
  1613. }
  1614. //---------------------------------------------------------------------------
  1615. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1616. {
  1617. DebugAssert(GetSide(Side) == osRemote);
  1618. DebugUsedParam(Side);
  1619. return RemoteDirView;
  1620. }
  1621. //---------------------------------------------------------------------------
  1622. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1623. {
  1624. DebugAssert(GetSide(Side) == osRemote);
  1625. DebugUsedParam(Side);
  1626. return RemoteDriveView;
  1627. }
  1628. //---------------------------------------------------------------------------
  1629. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1630. {
  1631. DebugAssert(GetSide(Side) == osRemote);
  1632. DebugUsedParam(Side);
  1633. return HasActiveTerminal();
  1634. }
  1635. //---------------------------------------------------------------------------
  1636. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1637. TOperationSide Side, int FilesOnly)
  1638. {
  1639. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1640. }
  1641. //---------------------------------------------------------------------------
  1642. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1643. TOperationSide Side, int FilesOnly)
  1644. {
  1645. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1646. }
  1647. //---------------------------------------------------------------------------
  1648. struct THistoryItemData
  1649. {
  1650. short int Side;
  1651. short int Index;
  1652. };
  1653. //---------------------------------------------------------------------------
  1654. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1655. {
  1656. DirView(Side)->HistoryGo(Index);
  1657. }
  1658. //---------------------------------------------------------------------------
  1659. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1660. {
  1661. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1662. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1663. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1664. }
  1665. //---------------------------------------------------------------------------
  1666. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1667. bool Back)
  1668. {
  1669. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1670. {
  1671. TCustomDirView * DView = DirView(Side);
  1672. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1673. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1674. if (ICount > 10)
  1675. {
  1676. ICount = 10;
  1677. }
  1678. Menu->Items->Clear();
  1679. THistoryItemData Data;
  1680. Data.Side = Side;
  1681. for (short int i = 1; i <= ICount; i++)
  1682. {
  1683. TTBCustomItem * Item = new TTBXItem(Menu);
  1684. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1685. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1686. Item->Hint = DView->HistoryPath[Data.Index];
  1687. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1688. Item->Tag = *reinterpret_cast<int*>(&Data);
  1689. Item->OnClick = HistoryItemClick;
  1690. Menu->Items->Add(Item);
  1691. }
  1692. }
  1693. }
  1694. //---------------------------------------------------------------------------
  1695. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1696. TOperationSide Side, bool Back)
  1697. {
  1698. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1699. {
  1700. // workaround
  1701. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1702. // operator new allocates memory in Pascal code, but calls inline
  1703. // contructor in C++, leading in problems, the function does
  1704. // both in Pascal code
  1705. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1706. UpdateHistoryMenu(Side, Back);
  1707. }
  1708. return FHistoryMenu[Side == osLocal][Back];
  1709. }
  1710. //---------------------------------------------------------------------------
  1711. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1712. TCustomDirView *Sender)
  1713. {
  1714. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1715. UpdateHistoryMenu(Side, true);
  1716. UpdateHistoryMenu(Side, false);
  1717. }
  1718. //---------------------------------------------------------------------------
  1719. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1720. {
  1721. // remote custom commands can be executed only if the server supports shell commands
  1722. // or have secondary shell
  1723. return HasActiveTerminal() && (Terminal->IsCapable[fcSecondaryShell] || Terminal->IsCapable[fcShellAnyCommand]);
  1724. }
  1725. //---------------------------------------------------------------------------
  1726. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1727. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1728. {
  1729. // -1 = hidden, 0 = disabled, 1 = enabled
  1730. int Result;
  1731. TFileCustomCommand RemoteCustomCommand;
  1732. TLocalCustomCommand LocalCustomCommand;
  1733. TFileCustomCommand * NonInteractiveCustomCommand =
  1734. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1735. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1736. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1737. if (FLAGCLEAR(Command.Params, ccLocal))
  1738. {
  1739. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1740. // custom command that does not operate with files can be executed anytime ...
  1741. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1742. {
  1743. // ... but do not show such command in remote file menu
  1744. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1745. {
  1746. Result = AllowedState;
  1747. }
  1748. else
  1749. {
  1750. Result = -1;
  1751. }
  1752. }
  1753. else
  1754. {
  1755. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1756. {
  1757. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1758. }
  1759. else
  1760. {
  1761. Result = -1;
  1762. }
  1763. }
  1764. }
  1765. else
  1766. {
  1767. // custom command that does not operate with files can be executed anytime
  1768. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1769. {
  1770. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1771. }
  1772. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1773. {
  1774. if ((ListType == ccltAll) || (ListType == ccltFile))
  1775. {
  1776. // special case is "diff"-style command that can be executed over any side,
  1777. // if we have both sides
  1778. Result =
  1779. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1780. // directly (instead of EnableSelectedOperation) to pass
  1781. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1782. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1783. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1784. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1785. }
  1786. else if (ListType == ccltBoth)
  1787. {
  1788. DebugAssert(!IsLocalBrowserMode());
  1789. Result = 1;
  1790. }
  1791. else
  1792. {
  1793. Result = -1;
  1794. }
  1795. }
  1796. else
  1797. {
  1798. if ((ListType == ccltAll) ||
  1799. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1800. {
  1801. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1802. }
  1803. else
  1804. {
  1805. Result = -1;
  1806. }
  1807. }
  1808. if ((Result > 0) &&
  1809. LocalCustomCommand.IsSessionCommand(Cmd) &&
  1810. !HasActiveTerminal())
  1811. {
  1812. Result = 0;
  1813. }
  1814. }
  1815. return Result;
  1816. }
  1817. //---------------------------------------------------------------------------
  1818. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1819. TStrings * FileList, const TCustomCommandType & ACommand,
  1820. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1821. {
  1822. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1823. {
  1824. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1825. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1826. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1827. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1828. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1829. bool Capture =
  1830. FLAGSET(ACommand.Params, ccShowResults) ||
  1831. FLAGSET(ACommand.Params, ccCopyResults) ||
  1832. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1833. TCaptureOutputEvent OutputEvent = NULL;
  1834. DebugAssert(FCapturedLog == NULL);
  1835. if (Capture)
  1836. {
  1837. FCapturedLog = new TStringList();
  1838. OutputEvent = TerminalCaptureLog;
  1839. }
  1840. try
  1841. {
  1842. if (!RemoteCustomCommand.IsFileCommand(Command))
  1843. {
  1844. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1845. OutputEvent);
  1846. }
  1847. else
  1848. {
  1849. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1850. }
  1851. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1852. {
  1853. if (FLAGSET(ACommand.Params, ccCopyResults))
  1854. {
  1855. CopyToClipboard(FCapturedLog);
  1856. }
  1857. if (FLAGSET(ACommand.Params, ccShowResults))
  1858. {
  1859. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1860. }
  1861. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1862. {
  1863. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1864. }
  1865. }
  1866. }
  1867. __finally
  1868. {
  1869. SAFE_DESTROY(FCapturedLog);
  1870. }
  1871. }
  1872. }
  1873. //---------------------------------------------------------------------------
  1874. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1875. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1876. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1877. {
  1878. DebugAssert(!IsLocalBrowserMode());
  1879. TStrings * LocalFileList = NULL;
  1880. TStrings * RemoteFileList = NULL;
  1881. TStrings * RemoteFileListFull = NULL;
  1882. try
  1883. {
  1884. if (LocalFileCommand)
  1885. {
  1886. if (ALocalFileList == NULL)
  1887. {
  1888. DebugAssert(HasDirView[osLocal]);
  1889. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1890. // directly (instead of EnableSelectedOperation) to pass
  1891. // false to FocusedFileOnlyWhenFocused
  1892. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1893. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1894. }
  1895. else
  1896. {
  1897. LocalFileList = ALocalFileList;
  1898. }
  1899. if (FileListCommand)
  1900. {
  1901. if (LocalFileList->Count != 1)
  1902. {
  1903. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1904. }
  1905. }
  1906. else
  1907. {
  1908. if ((LocalFileList->Count != 1) &&
  1909. (FileList->Count != 1) &&
  1910. (LocalFileList->Count != FileList->Count))
  1911. {
  1912. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1913. }
  1914. }
  1915. }
  1916. UnicodeString RootTempDir;
  1917. UnicodeString TempDir;
  1918. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1919. if (!RemoteFiles)
  1920. {
  1921. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1922. }
  1923. try
  1924. {
  1925. TDateTimes RemoteFileTimes;
  1926. if (RemoteFiles)
  1927. {
  1928. RemoteFileList = FileList;
  1929. }
  1930. else
  1931. {
  1932. RemoteFileList = new TStringList();
  1933. TMakeLocalFileListParams MakeFileListParam;
  1934. MakeFileListParam.FileList = RemoteFileList;
  1935. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1936. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1937. MakeFileListParam.Recursive =
  1938. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1939. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1940. if (!MakeFileListParam.Recursive)
  1941. {
  1942. RemoteFileTimes.clear();
  1943. RemoteFileListFull = new TStringList();
  1944. MakeFileListParam.FileList = RemoteFileListFull;
  1945. MakeFileListParam.Recursive = true;
  1946. MakeFileListParam.IncludeDirs = false;
  1947. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1948. }
  1949. else
  1950. {
  1951. RemoteFileListFull = RemoteFileList;
  1952. }
  1953. }
  1954. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1955. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1956. if (!NonBlocking)
  1957. {
  1958. int Count;
  1959. if (FileListCommand)
  1960. {
  1961. Count = 1;
  1962. }
  1963. else if (LocalFileCommand)
  1964. {
  1965. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  1966. }
  1967. else
  1968. {
  1969. Count = RemoteFileList->Count;
  1970. }
  1971. Progress.Start(foCustomCommand, osRemote, Count);
  1972. DebugAssert(FProgressForm != NULL);
  1973. FProgressForm->ReadOnly = true;
  1974. }
  1975. try
  1976. {
  1977. if (FileListCommand)
  1978. {
  1979. UnicodeString LocalFile;
  1980. // MakeFileList does not delimit filenames
  1981. UnicodeString FileList = MakeFileList(RemoteFileList);
  1982. if (LocalFileCommand)
  1983. {
  1984. DebugAssert(LocalFileList->Count == 1);
  1985. LocalFile = LocalFileList->Strings[0];
  1986. }
  1987. TLocalCustomCommand CustomCommand(Data,
  1988. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1989. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1990. if (NonBlocking)
  1991. {
  1992. DebugAssert(!POutput);
  1993. ExecuteShellChecked(ShellCommand);
  1994. }
  1995. else
  1996. {
  1997. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1998. }
  1999. }
  2000. else if (LocalFileCommand)
  2001. {
  2002. if (LocalFileList->Count == 1)
  2003. {
  2004. UnicodeString LocalFile = LocalFileList->Strings[0];
  2005. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2006. {
  2007. UnicodeString FileName = RemoteFileList->Strings[Index];
  2008. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2009. Progress.SetFile(FileNameForProgress);
  2010. TLocalCustomCommand CustomCommand(Data,
  2011. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  2012. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2013. TOnceDoneOperation OnceDoneOperation;
  2014. // Do not unselect anything, as it won't work with two panels anyway
  2015. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2016. }
  2017. }
  2018. else if (RemoteFileList->Count == 1)
  2019. {
  2020. UnicodeString FileName = RemoteFileList->Strings[0];
  2021. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2022. {
  2023. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2024. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2025. Progress.SetFile(LocalFileNameOnly);
  2026. TLocalCustomCommand CustomCommand(
  2027. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2028. FileName, LocalFileName, L"");
  2029. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2030. TOnceDoneOperation OnceDoneOperation;
  2031. // Do not unselect anything, as it won't work with two panels anyway
  2032. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2033. }
  2034. }
  2035. else
  2036. {
  2037. if (LocalFileList->Count != RemoteFileList->Count)
  2038. {
  2039. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  2040. }
  2041. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2042. {
  2043. UnicodeString FileName = RemoteFileList->Strings[Index];
  2044. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2045. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2046. Progress.SetFile(LocalFileNameOnly); // sic
  2047. TLocalCustomCommand CustomCommand(
  2048. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2049. FileName, LocalFileName, L"");
  2050. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2051. TOnceDoneOperation OnceDoneOperation;
  2052. // Do not unselect anything, as it won't work with two panels anyway
  2053. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2054. }
  2055. }
  2056. }
  2057. else
  2058. {
  2059. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2060. {
  2061. UnicodeString FileName = RemoteFileList->Strings[Index];
  2062. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2063. Progress.SetFile(FileNameForProgress);
  2064. TLocalCustomCommand CustomCommand(Data,
  2065. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2066. FileName, L"", L"");
  2067. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2068. TOnceDoneOperation OnceDoneOperation;
  2069. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  2070. }
  2071. }
  2072. }
  2073. __finally
  2074. {
  2075. if (!NonBlocking)
  2076. {
  2077. Progress.Stop();
  2078. }
  2079. }
  2080. DebugAssert(!FAutoOperation);
  2081. if (!RemoteFiles)
  2082. {
  2083. TempDir = IncludeTrailingBackslash(TempDir);
  2084. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  2085. {
  2086. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  2087. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  2088. // Skip directories, process only nested files.
  2089. // The check is redundant as FileAge fails for directories anyway.
  2090. !DirectoryExists(FileName))
  2091. {
  2092. UnicodeString RemoteDir =
  2093. UnixExtractFileDir(
  2094. UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
  2095. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2096. TDateTime NewTime;
  2097. if (FileAge(FileName, NewTime) &&
  2098. (NewTime != RemoteFileTimes[Index]))
  2099. {
  2100. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2101. TemporaryFileCopyParam(CopyParam);
  2102. CopyParam.FileMask = L"";
  2103. FAutoOperation = true;
  2104. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2105. TemporaryFilesList->Add(FileName);
  2106. Terminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2107. }
  2108. }
  2109. }
  2110. }
  2111. }
  2112. __finally
  2113. {
  2114. FAutoOperation = false;
  2115. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2116. {
  2117. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2118. }
  2119. }
  2120. }
  2121. __finally
  2122. {
  2123. if (RemoteFileList != FileList)
  2124. {
  2125. delete RemoteFileList;
  2126. }
  2127. if (LocalFileList != ALocalFileList)
  2128. {
  2129. delete LocalFileList;
  2130. }
  2131. if (RemoteFileListFull != RemoteFileList)
  2132. {
  2133. delete RemoteFileListFull;
  2134. }
  2135. }
  2136. }
  2137. //---------------------------------------------------------------------------
  2138. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2139. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2140. bool FileListCommand, UnicodeString * POutput)
  2141. {
  2142. std::unique_ptr<TStrings> SelectedFileList(DirView(osCurrent)->CreateFileList(false, true, NULL));
  2143. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2144. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2145. {
  2146. UnicodeString FileName = SelectedFileList->Strings[Index];
  2147. if (DirectoryExists(FileName))
  2148. {
  2149. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2150. {
  2151. LocalFileList->Add(FileName);
  2152. }
  2153. if (FLAGSET(ACommand.Params, ccRecursive))
  2154. {
  2155. TMakeLocalFileListParams MakeFileListParam;
  2156. MakeFileListParam.FileList = LocalFileList.get();
  2157. MakeFileListParam.FileTimes = NULL;
  2158. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2159. MakeFileListParam.Recursive = true;
  2160. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2161. }
  2162. }
  2163. else
  2164. {
  2165. LocalFileList->Add(FileName);
  2166. }
  2167. }
  2168. UnicodeString RemotePath;
  2169. if (Terminal != NULL)
  2170. {
  2171. RemotePath = Terminal->CurrentDirectory;
  2172. }
  2173. if (FileListCommand)
  2174. {
  2175. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2176. TLocalCustomCommand CustomCommand(
  2177. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2178. L"", L"", FileList);
  2179. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2180. }
  2181. else
  2182. {
  2183. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2184. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2185. DebugAssert(FProgressForm != NULL);
  2186. FProgressForm->ReadOnly = true;
  2187. try
  2188. {
  2189. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2190. {
  2191. UnicodeString FileName = LocalFileList->Strings[Index];
  2192. Progress.SetFile(FileName);
  2193. TLocalCustomCommand CustomCommand(
  2194. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2195. FileName, L"", L"");
  2196. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2197. TOnceDoneOperation OnceDoneOperation;
  2198. Progress.Finish(FileName, true, OnceDoneOperation);
  2199. }
  2200. }
  2201. __finally
  2202. {
  2203. Progress.Stop();
  2204. }
  2205. }
  2206. }
  2207. //---------------------------------------------------------------------------
  2208. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2209. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2210. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2211. {
  2212. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2213. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2214. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2215. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2216. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2217. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2218. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2219. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2220. std::unique_ptr<UnicodeString> POutput;
  2221. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2222. FLAGSET(ACommand.Params, ccCopyResults))
  2223. {
  2224. POutput.reset(new UnicodeString());
  2225. }
  2226. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  2227. FProgressSide = FCurrentSide;
  2228. if (!LocalCustomCommand.IsFileCommand(Command))
  2229. {
  2230. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2231. }
  2232. // remote files?
  2233. else if (!IsSideLocalBrowser(FCurrentSide) || LocalFileCommand)
  2234. {
  2235. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2236. }
  2237. // local files
  2238. else
  2239. {
  2240. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2241. }
  2242. if (POutput.get() != NULL)
  2243. {
  2244. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2245. int P = POutput->Pos(sLineBreak);
  2246. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2247. {
  2248. POutput->SetLength(P - 1);
  2249. }
  2250. // Copy even empty output to clipboard
  2251. if (FLAGSET(ACommand.Params, ccCopyResults))
  2252. {
  2253. CopyToClipboard(*POutput);
  2254. }
  2255. // But do not show an empty message box.
  2256. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2257. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2258. !POutput->IsEmpty())
  2259. {
  2260. TClipboardHandler ClipboardHandler;
  2261. ClipboardHandler.Text = *POutput;
  2262. TMessageParams Params;
  2263. TQueryButtonAlias Aliases[1];
  2264. Aliases[0].Button = qaRetry;
  2265. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2266. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2267. Params.Aliases = Aliases;
  2268. Params.AliasesCount = LENOF(Aliases);
  2269. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2270. }
  2271. }
  2272. }
  2273. //---------------------------------------------------------------------------
  2274. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2275. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2276. {
  2277. TCustomCommandData Data;
  2278. UnicodeString Site;
  2279. UnicodeString RemotePath;
  2280. if (HasActiveTerminal())
  2281. {
  2282. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  2283. Data = TCustomCommandData(SessionData.get());
  2284. Site = Terminal->SessionData->SessionKey;
  2285. RemotePath = Terminal->CurrentDirectory;
  2286. }
  2287. UnicodeString HelpKeyword = ACommand.HomePage;
  2288. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2289. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2290. {
  2291. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2292. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2293. {
  2294. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2295. }
  2296. else
  2297. {
  2298. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2299. }
  2300. if (!DoCustomCommandOptionsDialog(
  2301. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2302. {
  2303. Abort();
  2304. }
  2305. }
  2306. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2307. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2308. {
  2309. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2310. }
  2311. else
  2312. {
  2313. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2314. }
  2315. }
  2316. //---------------------------------------------------------------------------
  2317. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2318. const TCustomCommandType & Command)
  2319. {
  2320. DebugAssert(FCustomCommandLocalFileList != NULL);
  2321. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2322. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2323. TStrings * LocalFileList = new TStringList();
  2324. TStrings * RemoteFileList = new TStringList();
  2325. try
  2326. {
  2327. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2328. {
  2329. LocalFileList->Clear();
  2330. LocalFileList->AddObject(
  2331. FCustomCommandLocalFileList->Strings[Index],
  2332. FCustomCommandLocalFileList->Objects[Index]);
  2333. RemoteFileList->Clear();
  2334. RemoteFileList->AddObject(
  2335. FCustomCommandRemoteFileList->Strings[Index],
  2336. FCustomCommandRemoteFileList->Objects[Index]);
  2337. CustomCommand(RemoteFileList, Command, LocalFileList);
  2338. }
  2339. }
  2340. __finally
  2341. {
  2342. delete LocalFileList;
  2343. delete RemoteFileList;
  2344. }
  2345. }
  2346. //---------------------------------------------------------------------------
  2347. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2348. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2349. {
  2350. delete FCustomCommandLocalFileList;
  2351. delete FCustomCommandRemoteFileList;
  2352. // takeover ownership,
  2353. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2354. FCustomCommandLocalFileList = LocalFileList;
  2355. FCustomCommandRemoteFileList = RemoteFileList;
  2356. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2357. if (Button != NULL)
  2358. {
  2359. FCustomCommandMenu->Items->Clear();
  2360. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2361. MenuPopup(FCustomCommandMenu, Button);
  2362. }
  2363. else
  2364. {
  2365. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2366. }
  2367. }
  2368. //---------------------------------------------------------------------------
  2369. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2370. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2371. {
  2372. DebugAssert(FCapturedLog != NULL);
  2373. if ((OutputType == cotOutput) || (OutputType == cotError))
  2374. {
  2375. FCapturedLog->Add(AddedLine);
  2376. }
  2377. }
  2378. //---------------------------------------------------------------------------
  2379. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2380. TOperationSide Side)
  2381. {
  2382. return
  2383. (Side == osOther) &&
  2384. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2385. }
  2386. //---------------------------------------------------------------------------
  2387. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2388. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2389. {
  2390. TTBItemOptions O;
  2391. O = Item->Options;
  2392. TCustomDirView * DView = DirView(Side);
  2393. if ((DView->ItemFocused != NULL) &&
  2394. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2395. // when resolving links is disabled, default action is to enter the directory,
  2396. // no matter what DoubleClickAction is configured to
  2397. (IsSideLocalBrowser(Side) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2398. // Can only Edit files, but can Open/Copy even directories
  2399. ((DoubleClickAction != dcaEdit) ||
  2400. !DView->ItemIsDirectory(DView->ItemFocused)))
  2401. {
  2402. Item->Options = O << tboDefault;
  2403. }
  2404. else
  2405. {
  2406. Item->Options = O >> tboDefault;
  2407. }
  2408. }
  2409. //---------------------------------------------------------------------------
  2410. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2411. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2412. {
  2413. TCustomDirView * DView = DirView(Side);
  2414. TListItem * Item = DView->ItemFocused;
  2415. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2416. EnableFocusedOperation[Side])
  2417. {
  2418. TPoint ClientPoint;
  2419. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2420. TPoint(0, 0) : MousePos;
  2421. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2422. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2423. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2424. }
  2425. }
  2426. //---------------------------------------------------------------------------
  2427. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2428. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2429. {
  2430. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2431. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2432. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2433. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2434. Handled = true;
  2435. }
  2436. //---------------------------------------------------------------------------
  2437. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2438. {
  2439. }
  2440. //---------------------------------------------------------------------------
  2441. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2442. {
  2443. DebugAssert(FErrorList == NULL);
  2444. if (WinConfiguration->ContinueOnError)
  2445. {
  2446. FErrorList = new TStringList();
  2447. Configuration->Usage->Inc(L"ContinuationsOnError");
  2448. }
  2449. NonVisualDataModule->StartBusy();
  2450. }
  2451. //---------------------------------------------------------------------------
  2452. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2453. {
  2454. NonVisualDataModule->EndBusy();
  2455. if (FErrorList)
  2456. {
  2457. HandleErrorList(FErrorList);
  2458. }
  2459. }
  2460. //---------------------------------------------------------------------------
  2461. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2462. const TCopyParamType & CopyParam)
  2463. {
  2464. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2465. {
  2466. Configuration->Usage->Inc(L"FileMaskUses");
  2467. }
  2468. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2469. {
  2470. Configuration->Usage->Inc(L"OperationMaskUses");
  2471. }
  2472. }
  2473. //---------------------------------------------------------------------------
  2474. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2475. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2476. {
  2477. DebugAssert(!IsLocalBrowserMode());
  2478. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2479. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2480. TTransferOperationParam DefaultParam;
  2481. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2482. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2483. switch (Param.Queue)
  2484. {
  2485. case asOn:
  2486. CopyParam.Queue = true;
  2487. break;
  2488. case asOff:
  2489. CopyParam.Queue = false;
  2490. break;
  2491. case asAuto:
  2492. default:
  2493. // keep default
  2494. break;
  2495. }
  2496. bool Result =
  2497. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2498. if (Result)
  2499. {
  2500. DebugAssert(Terminal);
  2501. bool SelectionRestored = false;
  2502. TCustomDirView * DView = NULL;
  2503. if (HasDirView[Side])
  2504. {
  2505. DView = DirView(Side);
  2506. DView->SaveSelection();
  2507. DView->SaveSelectedNames();
  2508. }
  2509. UpdateCopyParamCounters(CopyParam);
  2510. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2511. DebugAssert(FTransferResumeList == NULL);
  2512. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2513. FMoveToQueue = false;
  2514. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2515. try
  2516. {
  2517. TStrings * PermanentFileList;
  2518. std::unique_ptr<TStrings> PermanentFileListOwner;
  2519. try
  2520. {
  2521. if (Side == osLocal)
  2522. {
  2523. PermanentFileList = FileList;
  2524. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2525. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2526. if (Operation == foMove)
  2527. {
  2528. ReloadLocalDirectory();
  2529. if (DView != NULL)
  2530. {
  2531. DView->RestoreSelection();
  2532. }
  2533. SelectionRestored = true;
  2534. }
  2535. }
  2536. else
  2537. {
  2538. // Clone the file list as it may refer to current directory files,
  2539. // which get destroyed, when the source directory is reloaded after foMove operation.
  2540. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2541. // But for a hotfix, we are not going to do such a big change.
  2542. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2543. PermanentFileList = PermanentFileListOwner.get();
  2544. try
  2545. {
  2546. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2547. }
  2548. __finally
  2549. {
  2550. if (Operation == foMove)
  2551. {
  2552. if (DView != NULL)
  2553. {
  2554. DView->RestoreSelection();
  2555. }
  2556. SelectionRestored = true;
  2557. }
  2558. ReloadLocalDirectory(Param.TargetDirectory);
  2559. }
  2560. }
  2561. // When the transfer batch is aborted between individual file transfers, the Abort is not called.
  2562. if (FMoveToQueue)
  2563. {
  2564. Abort();
  2565. }
  2566. }
  2567. catch (EAbort &)
  2568. {
  2569. if (FMoveToQueue)
  2570. {
  2571. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2572. DebugAssert(CopyParam.TransferSkipList == NULL);
  2573. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2574. if (TransferResumeList->Count > 0)
  2575. {
  2576. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2577. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2578. }
  2579. CopyParam.TransferSkipList = TransferResumeList.release();
  2580. // not really needed, just to keep it consistent with TransferResumeList
  2581. FTransferResumeList = NULL;
  2582. FMoveToQueue = false;
  2583. Configuration->Usage->Inc("MovesToBackground");
  2584. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2585. ClearTransferSourceSelection(Direction);
  2586. }
  2587. throw;
  2588. }
  2589. }
  2590. __finally
  2591. {
  2592. if (!SelectionRestored && (DView != NULL))
  2593. {
  2594. DView->DiscardSavedSelection();
  2595. }
  2596. FTransferResumeList = NULL;
  2597. }
  2598. }
  2599. return Result;
  2600. }
  2601. //---------------------------------------------------------------------------
  2602. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2603. TOperationSide Side, TStrings * FileList, void * Param)
  2604. {
  2605. DebugAssert(FileList->Count);
  2606. // We deliberately do not toggle alternative flag (Param), but use OR,
  2607. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2608. // shortcut of CurrentDeleteAlternativeAction
  2609. bool Alternative =
  2610. bool(Param) || UseAlternativeFunction();
  2611. bool Recycle;
  2612. if (IsSideLocalBrowser(Side))
  2613. {
  2614. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2615. }
  2616. else
  2617. {
  2618. Recycle =
  2619. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2620. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2621. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2622. }
  2623. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2624. if (!Result)
  2625. {
  2626. UnicodeString Query;
  2627. if (FileList->Count == 1)
  2628. {
  2629. if (IsSideLocalBrowser(Side))
  2630. {
  2631. Query = ExtractFileName(FileList->Strings[0]);
  2632. }
  2633. else
  2634. {
  2635. Query = UnixExtractFileName(FileList->Strings[0]);
  2636. }
  2637. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2638. }
  2639. else
  2640. {
  2641. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2642. }
  2643. TMessageParams Params(mpNeverAskAgainCheck);
  2644. Params.ImageName = L"Delete file";
  2645. unsigned int Answer =
  2646. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2647. if (Answer == qaNeverAskAgain)
  2648. {
  2649. Result = true;
  2650. if (Recycle)
  2651. {
  2652. WinConfiguration->ConfirmRecycling = false;
  2653. }
  2654. else
  2655. {
  2656. WinConfiguration->ConfirmDeleting = false;
  2657. }
  2658. }
  2659. else
  2660. {
  2661. Result = (Answer == qaOK);
  2662. }
  2663. }
  2664. if (Result)
  2665. {
  2666. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2667. }
  2668. return Result;
  2669. }
  2670. //---------------------------------------------------------------------------
  2671. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2672. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2673. {
  2674. TAutoBatch AutoBatch(this);
  2675. bool Result;
  2676. if ((Operation == foCopy) || (Operation == foMove))
  2677. {
  2678. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2679. }
  2680. else if (Operation == foRename)
  2681. {
  2682. DebugAssert(DirView(Side)->ItemFocused);
  2683. DirView(Side)->ItemFocused->EditCaption();
  2684. Result = true;
  2685. }
  2686. else if (Operation == foDelete)
  2687. {
  2688. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2689. }
  2690. else if (Operation == foSetProperties)
  2691. {
  2692. DirView(osRemote)->SaveSelectedNames();
  2693. Result = SetProperties(Side, FileList);
  2694. }
  2695. else if (Operation == foCustomCommand)
  2696. {
  2697. DebugAssert(Param);
  2698. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2699. DirView(osRemote)->SaveSelectedNames();
  2700. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2701. CustomCommand(FileList, *Command, NULL);
  2702. Result = true;
  2703. }
  2704. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2705. {
  2706. DebugAssert(!IsSideLocalBrowser(Side));
  2707. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2708. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2709. }
  2710. else if (Operation == foLock)
  2711. {
  2712. DebugAssert(!IsSideLocalBrowser(Side));
  2713. LockFiles(FileList, true);
  2714. Result = true;
  2715. }
  2716. else if (Operation == foUnlock)
  2717. {
  2718. DebugAssert(!IsSideLocalBrowser(Side));
  2719. LockFiles(FileList, false);
  2720. Result = true;
  2721. }
  2722. else
  2723. {
  2724. DebugFail();
  2725. }
  2726. return Result;
  2727. }
  2728. //---------------------------------------------------------------------------
  2729. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2730. {
  2731. if (Side == osCurrent)
  2732. {
  2733. Side = FCurrentSide;
  2734. }
  2735. return Side;
  2736. }
  2737. //---------------------------------------------------------------------------
  2738. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2739. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2740. {
  2741. Side = GetSide(Side);
  2742. bool Result;
  2743. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, IsSideLocalBrowser(Side), NULL);
  2744. try
  2745. {
  2746. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2747. }
  2748. __finally
  2749. {
  2750. delete FileList;
  2751. }
  2752. return Result;
  2753. }
  2754. //---------------------------------------------------------------------------
  2755. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2756. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2757. bool NoConfirmation, void * Param)
  2758. {
  2759. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2760. {
  2761. if ((Operation == foCopy) || (Operation == foMove))
  2762. {
  2763. DebugAssert(!IsLocalBrowserMode());
  2764. if (GetSide(Side) == osLocal)
  2765. {
  2766. Configuration->Usage->Inc(L"UploadsCommand");
  2767. }
  2768. else
  2769. {
  2770. Configuration->Usage->Inc(L"DownloadsCommand");
  2771. }
  2772. }
  2773. }
  2774. }
  2775. //---------------------------------------------------------------------------
  2776. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2777. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2778. {
  2779. TTransferOperationParam Param;
  2780. if ((WinConfiguration->Interface != ifCommander) ||
  2781. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2782. {
  2783. Flags &= ~cocShortCutHint;
  2784. }
  2785. TCustomDirView * DView = DirView(Side);
  2786. Param.Options =
  2787. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2788. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2789. if (FLAGSET(Flags, cocQueue))
  2790. {
  2791. Param.Queue = asOn;
  2792. }
  2793. else if (FLAGSET(Flags, cocNonQueue))
  2794. {
  2795. Param.Queue = asOff;
  2796. }
  2797. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2798. }
  2799. //---------------------------------------------------------------------------
  2800. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2801. {
  2802. int Count = ErrorList->Count;
  2803. try
  2804. {
  2805. if (ErrorList->Count)
  2806. {
  2807. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2808. if (MessageDialog(Message, qtError,
  2809. qaOK | qaCancel, HELP_NONE) == qaOK)
  2810. {
  2811. unsigned int Answer;
  2812. int Index = 0;
  2813. do
  2814. {
  2815. DebugAssert(Index >= 0 && Index < Count);
  2816. TQueryButtonAlias Aliases[2];
  2817. Aliases[0].Button = qaYes;
  2818. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2819. Aliases[1].Button = qaNo;
  2820. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2821. TMessageParams Params;
  2822. Params.Aliases = Aliases;
  2823. Params.AliasesCount = LENOF(Aliases);
  2824. int No = Index + 1;
  2825. UnicodeString Message = ErrorList->Strings[Index];
  2826. UnicodeString AMainInstructions;
  2827. UnicodeString Details;
  2828. if (ExtractMainInstructions(Message, AMainInstructions))
  2829. {
  2830. Details = Message;
  2831. Message = AMainInstructions;
  2832. }
  2833. Message = MainInstructions(FMTLOAD(ERROR_LIST_NUMBER, (No, Count, Message))) + Details;
  2834. Answer = MoreMessageDialog(
  2835. Message,
  2836. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2837. (Index ? qaYes : 0) | (Index < Count - 1 ? qaNo : 0) |
  2838. qaOK, HELP_NONE, &Params);
  2839. if (Answer == qaNo)
  2840. {
  2841. Index++;
  2842. }
  2843. if (Answer == qaYes)
  2844. {
  2845. Index--;
  2846. }
  2847. }
  2848. while (Answer != qaOK);
  2849. }
  2850. }
  2851. }
  2852. __finally
  2853. {
  2854. TStrings * List = ErrorList;
  2855. ErrorList = NULL;
  2856. for (int i = 0; i < Count; i++)
  2857. {
  2858. delete List->Objects[i];
  2859. }
  2860. delete List;
  2861. }
  2862. }
  2863. //---------------------------------------------------------------------------
  2864. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2865. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2866. {
  2867. // needed for checking filemasks, as there's no directory object
  2868. // associated with the file object
  2869. File->FullFileName = FullFileName;
  2870. TFileMasks::TParams MaskParams;
  2871. MaskParams.Size = File->Size;
  2872. MaskParams.Modification = File->Modification;
  2873. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2874. }
  2875. //---------------------------------------------------------------------------
  2876. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2877. {
  2878. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2879. Side = GetSide(Side);
  2880. TCustomDirView * DView = DirView(Side);
  2881. TListItem * FocusedItem = DView->ItemFocused;
  2882. UnicodeString Name;
  2883. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2884. {
  2885. Name = DView->ItemFileName(FocusedItem);
  2886. }
  2887. else
  2888. {
  2889. Name = LoadStr(NEW_FILE);
  2890. }
  2891. UnicodeString Names = Name;
  2892. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2893. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2894. HELP_EDIT_NEW, History.get(), true, NULL, true, 400))
  2895. {
  2896. while (!Names.IsEmpty())
  2897. {
  2898. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2899. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2900. UnicodeString TargetFileName;
  2901. UnicodeString LocalFileName;
  2902. UnicodeString RootTempDir;
  2903. UnicodeString TempDir;
  2904. UnicodeString RemoteDirectory;
  2905. bool ExistingFile = false;
  2906. if (!IsSideLocalBrowser(Side))
  2907. {
  2908. Name = AbsolutePath(Terminal->CurrentDirectory, Name);
  2909. TRemoteFile * File = NULL;
  2910. if (Terminal->FileExists(Name, &File) &&
  2911. !File->IsDirectory)
  2912. {
  2913. try
  2914. {
  2915. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2916. ExistingFile = true;
  2917. }
  2918. __finally
  2919. {
  2920. delete File;
  2921. }
  2922. }
  2923. if (!ExistingFile)
  2924. {
  2925. RemoteDirectory = UnixExtractFilePath(Name);
  2926. TemporaryDirectoryForRemoteFiles(
  2927. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2928. TargetFileName = UnixExtractFileName(Name);
  2929. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2930. LocalFileName = TempDir +
  2931. // We probably do not want to trim the VMS version here
  2932. Terminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2933. }
  2934. }
  2935. else
  2936. {
  2937. if (ExtractFilePath(Name).IsEmpty())
  2938. {
  2939. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2940. }
  2941. else
  2942. {
  2943. LocalFileName = ExpandFileName(Name);
  2944. }
  2945. TargetFileName = ExtractFileName(Name);
  2946. }
  2947. if (!ExistingFile)
  2948. {
  2949. bool NewFile = !FileExists(ApiPath(LocalFileName));
  2950. if (NewFile)
  2951. {
  2952. int File = FileCreate(ApiPath(LocalFileName));
  2953. if (File < 0)
  2954. {
  2955. if (!RootTempDir.IsEmpty())
  2956. {
  2957. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2958. }
  2959. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2960. }
  2961. else
  2962. {
  2963. FileClose(File);
  2964. }
  2965. }
  2966. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2967. const TEditorData * ExternalEditor = NULL;
  2968. TFileMasks::TParams MaskParams; // size not known
  2969. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2970. false, MaskParams);
  2971. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2972. ExternalEditor, RootTempDir, RemoteDirectory, NewFile);
  2973. }
  2974. }
  2975. }
  2976. }
  2977. //---------------------------------------------------------------------------
  2978. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2979. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2980. {
  2981. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2982. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2983. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2984. return
  2985. ((ExecuteFileBy == efInternalEditor)) ||
  2986. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2987. }
  2988. //---------------------------------------------------------------------------
  2989. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2990. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2991. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2992. UnicodeString RemoteDirectory, bool NewFile)
  2993. {
  2994. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2995. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2996. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2997. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2998. Side = GetSide(Side);
  2999. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  3000. if (!IsSideLocalBrowser(Side))
  3001. {
  3002. Data->Terminal = Terminal;
  3003. Data->Queue = Queue;
  3004. Data->SessionData = CloneCurrentSessionData();
  3005. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  3006. Data->RemoteDirectory = RemoteDirectory;
  3007. Data->SessionName = Terminal->SessionData->SessionName;
  3008. Data->LocalRootDirectory = LocalRootDirectory;
  3009. Data->OriginalFileName = OriginalFileName;
  3010. Data->Command = L""; // will be changed later for external editor
  3011. }
  3012. if (ExecuteFileBy == efInternalEditor)
  3013. {
  3014. if (!IsSideLocalBrowser(Side))
  3015. {
  3016. UnicodeString RemotePath = UnixCombinePaths(RemoteDirectory, OriginalFileName);
  3017. UnicodeString Caption = RemotePath + TitleSeparator + Terminal->SessionData->SessionName;
  3018. TForm * Editor;
  3019. try
  3020. {
  3021. TNotifyEvent OnFileChanged = NULL;
  3022. TNotifyEvent OnSaveAll = NULL;
  3023. TAnyModifiedEvent OnAnyModified = NULL;
  3024. // Edited files are uploaded in background queue, what we do not support with encrypted files
  3025. if (Terminal->IsCapable[fcBackgroundTransfers])
  3026. {
  3027. OnFileChanged = FEditorManager->FileChanged;
  3028. OnSaveAll = SaveAllInternalEditors;
  3029. OnAnyModified = AnyInternalEditorModified;
  3030. }
  3031. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  3032. FEditorManager->FileReload, FEditorManager->FileClosed,
  3033. OnSaveAll, OnAnyModified,
  3034. Caption, FStandaloneEditing, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  3035. }
  3036. catch(...)
  3037. {
  3038. if (!LocalRootDirectory.IsEmpty())
  3039. {
  3040. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  3041. }
  3042. throw;
  3043. }
  3044. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  3045. }
  3046. else
  3047. {
  3048. DebugAssert(!FStandaloneEditing);
  3049. TForm * Editor =
  3050. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  3051. SaveAllInternalEditors, AnyInternalEditorModified,
  3052. L"", false, SessionColor, -1, NewFile);
  3053. FLocalEditors->Add(Editor);
  3054. }
  3055. }
  3056. else
  3057. {
  3058. HANDLE Process;
  3059. if (ExecuteFileBy == efExternalEditor)
  3060. {
  3061. UnicodeString Program, Params, Dir;
  3062. Data->Command = ExternalEditor->ExternalEditor;
  3063. ReformatFileNameCommand(Data->Command);
  3064. SplitCommand(Data->Command, Program, Params, Dir);
  3065. Params = ExpandFileNameCommand(Params, FileName);
  3066. Program = ExpandEnvironmentVariables(Program);
  3067. if (!ExecuteShell(Program, Params, Process))
  3068. {
  3069. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  3070. }
  3071. }
  3072. else
  3073. {
  3074. DebugAssert(!IsSideLocalBrowser(Side));
  3075. if (!ExecuteShell(FileName, L"", Process))
  3076. {
  3077. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  3078. }
  3079. }
  3080. if (IsSideLocalBrowser(Side) ||
  3081. ((ExecuteFileBy == efShell) &&
  3082. !WinConfiguration->Editor.SDIShellEditor) ||
  3083. ((ExecuteFileBy == efExternalEditor) &&
  3084. !ExternalEditor->SDIExternalEditor))
  3085. {
  3086. // no need for handle
  3087. if (Process != NULL)
  3088. {
  3089. DebugCheck(CloseHandle(Process));
  3090. }
  3091. Process = INVALID_HANDLE_VALUE;
  3092. }
  3093. else
  3094. {
  3095. if (Process == NULL)
  3096. {
  3097. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  3098. }
  3099. }
  3100. if (!IsSideLocalBrowser(Side))
  3101. {
  3102. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  3103. }
  3104. }
  3105. }
  3106. //---------------------------------------------------------------------------
  3107. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3108. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3109. void * /*Arg*/)
  3110. {
  3111. if (Token != NULL)
  3112. {
  3113. EditorFormFileSave(static_cast<TForm *>(Token));
  3114. }
  3115. }
  3116. //---------------------------------------------------------------------------
  3117. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3118. {
  3119. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3120. {
  3121. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3122. }
  3123. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3124. }
  3125. //---------------------------------------------------------------------------
  3126. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3127. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3128. void * Arg)
  3129. {
  3130. if ((Token != NULL) &&
  3131. IsEditorFormModified(static_cast<TForm *>(Token)))
  3132. {
  3133. *static_cast<bool *>(Arg) = true;
  3134. }
  3135. }
  3136. //---------------------------------------------------------------------------
  3137. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3138. TObject * /*Sender*/, bool & Modified)
  3139. {
  3140. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3141. {
  3142. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3143. {
  3144. Modified = true;
  3145. }
  3146. }
  3147. if (!Modified)
  3148. {
  3149. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3150. }
  3151. }
  3152. //---------------------------------------------------------------------------
  3153. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3154. {
  3155. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3156. }
  3157. //---------------------------------------------------------------------------
  3158. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3159. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3160. UnicodeString & Result, UnicodeString & RootDirectory)
  3161. {
  3162. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3163. {
  3164. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3165. Result = RootDirectory;
  3166. }
  3167. else
  3168. {
  3169. RootDirectory = L"";
  3170. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3171. Result = IncludeTrailingBackslash(Result);
  3172. }
  3173. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3174. {
  3175. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3176. }
  3177. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3178. {
  3179. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3180. {
  3181. RemoteDirectory.Delete(1, 1);
  3182. }
  3183. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3184. }
  3185. if (!ForceDirectories(ApiPath(Result)))
  3186. {
  3187. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3188. }
  3189. }
  3190. //---------------------------------------------------------------------------
  3191. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3192. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3193. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  3194. {
  3195. DebugAssert(!IsLocalBrowserMode());
  3196. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3197. if (ForceText)
  3198. {
  3199. CopyParam.TransferMode = tmAscii;
  3200. }
  3201. TemporaryFileCopyParam(CopyParam);
  3202. if (AllFiles)
  3203. {
  3204. CopyParam.IncludeFileMask = TFileMasks();
  3205. }
  3206. if (TempDir.IsEmpty())
  3207. {
  3208. TemporaryDirectoryForRemoteFiles(Terminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3209. }
  3210. DebugAssert(!FAutoOperation);
  3211. FAutoOperation = AutoOperation;
  3212. Terminal->ExceptionOnFail = true;
  3213. try
  3214. {
  3215. try
  3216. {
  3217. // turn off confirmations, as for MDI editors we may possibly download
  3218. // the same file over
  3219. Terminal->CopyToLocal(
  3220. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3221. if (GetTargetNames)
  3222. {
  3223. for (int i = 0; i < FileList->Count; i++)
  3224. {
  3225. FileList->Strings[i] =
  3226. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3227. }
  3228. }
  3229. }
  3230. catch(...)
  3231. {
  3232. if (!RootTempDir.IsEmpty())
  3233. {
  3234. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3235. }
  3236. throw;
  3237. }
  3238. }
  3239. __finally
  3240. {
  3241. FAutoOperation = false;
  3242. Terminal->ExceptionOnFail = false;
  3243. }
  3244. }
  3245. //---------------------------------------------------------------------------
  3246. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3247. {
  3248. // Do not waste time checking for default editor list the next time,
  3249. // or testing if default editor is not notepad.
  3250. bool TryNextTime = false;
  3251. UnicodeString UsageState;
  3252. if (!WinConfiguration->EditorList->IsDefaultList())
  3253. {
  3254. UsageState = "H";
  3255. }
  3256. else
  3257. {
  3258. UnicodeString Executable;
  3259. UnicodeString ExecutableDescription;
  3260. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3261. {
  3262. UnicodeString Message =
  3263. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3264. unsigned int Answer =
  3265. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3266. if (Answer != qaOK)
  3267. {
  3268. UsageState = "R";
  3269. }
  3270. else
  3271. {
  3272. UsageState = "A";
  3273. TEditorData EditorData;
  3274. EditorData.Editor = edExternal;
  3275. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3276. EditorData.ExternalEditorOptionsAutodetect();
  3277. TEditorList EditorList;
  3278. EditorList = *WinConfiguration->EditorList;
  3279. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3280. WinConfiguration->EditorList = &EditorList;
  3281. }
  3282. }
  3283. }
  3284. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3285. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3286. }
  3287. //---------------------------------------------------------------------------
  3288. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3289. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3290. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3291. {
  3292. if (ExecuteFileBy == efDefaultEditor)
  3293. {
  3294. if (!WinConfiguration->OfferedEditorAutoConfig)
  3295. {
  3296. EditorAutoConfig();
  3297. }
  3298. const TEditorPreferences * Editor =
  3299. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3300. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3301. {
  3302. ExecuteFileBy = efInternalEditor;
  3303. ExternalEditor = NULL;
  3304. }
  3305. else if (Editor->Data->Editor == edOpen)
  3306. {
  3307. ExecuteFileBy = efShell;
  3308. ExternalEditor = NULL;
  3309. }
  3310. else
  3311. {
  3312. ExecuteFileBy = efExternalEditor;
  3313. ExternalEditor = Editor->Data;
  3314. }
  3315. }
  3316. }
  3317. //---------------------------------------------------------------------------
  3318. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3319. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3320. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3321. {
  3322. UnicodeString OriginalFileName;
  3323. UnicodeString LocalRootDirectory;
  3324. UnicodeString RemoteDirectory;
  3325. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3326. IsSideLocalBrowser(Side), MaskParams);
  3327. UnicodeString Counter;
  3328. UnicodeString LocalFileName;
  3329. bool Handled = false;
  3330. if (!IsSideLocalBrowser(Side))
  3331. {
  3332. // We need to trim VMS version here, so that we use name without version
  3333. // when uploading back to create a new version of the file
  3334. OriginalFileName = Terminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3335. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3336. TObject * Token = NULL;
  3337. UnicodeString LocalDirectory;
  3338. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3339. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3340. LocalDirectory))
  3341. {
  3342. if (Token != NULL)
  3343. {
  3344. TForm * Form = dynamic_cast<TForm *>(Token);
  3345. if (Form->WindowState == wsMinimized)
  3346. {
  3347. ShowWindow(Form->Handle, SW_RESTORE);
  3348. }
  3349. else
  3350. {
  3351. Form->SetFocus();
  3352. }
  3353. Handled = true;
  3354. }
  3355. else
  3356. {
  3357. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3358. }
  3359. }
  3360. if (!Handled)
  3361. {
  3362. TStringList * FileList1 = new TStringList();
  3363. try
  3364. {
  3365. FileList1->AddObject(FullFileName, Object);
  3366. TemporarilyDownloadFiles(FileList1,
  3367. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3368. LocalRootDirectory, LocalDirectory, true, true, true);
  3369. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3370. }
  3371. __finally
  3372. {
  3373. delete FileList1;
  3374. }
  3375. switch (ExecuteFileBy)
  3376. {
  3377. case efShell:
  3378. Counter = "RemoteFilesExecuted";
  3379. break;
  3380. case efInternalEditor:
  3381. Counter = "RemoteFilesOpenedInInternalEditor";
  3382. break;
  3383. case efExternalEditor:
  3384. Counter = "RemoteFilesOpenedInExternalEditor";
  3385. break;
  3386. default:
  3387. DebugFail();
  3388. }
  3389. }
  3390. }
  3391. else
  3392. {
  3393. LocalFileName = FullFileName;
  3394. OriginalFileName = ExtractFileName(FullFileName);
  3395. switch (ExecuteFileBy)
  3396. {
  3397. case efShell:
  3398. Counter = "LocalFilesExecuted";
  3399. break;
  3400. case efInternalEditor:
  3401. Counter = "LocalFilesOpenedInInternalEditor";
  3402. break;
  3403. case efExternalEditor:
  3404. Counter = "LocalFilesOpenedInExternalEditor";
  3405. break;
  3406. default:
  3407. DebugFail();
  3408. }
  3409. }
  3410. if (!Handled)
  3411. {
  3412. Configuration->Usage->Inc(Counter);
  3413. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3414. ExternalEditor, LocalRootDirectory, RemoteDirectory, false);
  3415. }
  3416. }
  3417. //---------------------------------------------------------------------------
  3418. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3419. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3420. bool AllSelected, bool OnFocused)
  3421. {
  3422. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3423. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3424. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3425. Side = GetSide(Side);
  3426. TCustomDirView * DView = DirView(Side);
  3427. TStrings * FileList =
  3428. AllSelected ?
  3429. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3430. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3431. try
  3432. {
  3433. DebugAssert(AllSelected || (FileList->Count == 1));
  3434. for (int i = 0; i < FileList->Count; i++)
  3435. {
  3436. UnicodeString ListFileName = FileList->Strings[i];
  3437. UnicodeString FileNameOnly =
  3438. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3439. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3440. if (!DView->ItemIsDirectory(Item))
  3441. {
  3442. UnicodeString FullFileName;
  3443. if (!IsSideLocalBrowser(Side))
  3444. {
  3445. FullFileName = DirView(Side)->Path + ListFileName;
  3446. }
  3447. else
  3448. {
  3449. FullFileName = ListFileName;
  3450. }
  3451. TFileMasks::TParams MaskParams;
  3452. MaskParams.Size = DView->ItemFileSize(Item);
  3453. TDateTimePrecision Precision;
  3454. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3455. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3456. FileList->Objects[i], MaskParams);
  3457. }
  3458. }
  3459. }
  3460. __finally
  3461. {
  3462. delete FileList;
  3463. }
  3464. }
  3465. //---------------------------------------------------------------------------
  3466. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3467. {
  3468. CopyParam.FileNameCase = ncNoChange;
  3469. CopyParam.PreserveRights = false;
  3470. CopyParam.PreserveReadOnly = false;
  3471. CopyParam.ReplaceInvalidChars = true;
  3472. CopyParam.IncludeFileMask = TFileMasks();
  3473. CopyParam.NewerOnly = false;
  3474. CopyParam.FileMask = L"";
  3475. }
  3476. //---------------------------------------------------------------------------
  3477. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3478. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3479. {
  3480. TTerminalManager * Manager = TTerminalManager::Instance();
  3481. if (!IsActiveTerminal(Data->Terminal))
  3482. {
  3483. if (!NonVisualDataModule->Busy)
  3484. {
  3485. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3486. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3487. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3488. if (Data->Terminal == NULL)
  3489. {
  3490. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3491. if (SameSiteTerminal != NULL)
  3492. {
  3493. UnicodeString Message =
  3494. FMTLOAD(EDIT_SESSION_REATTACH,
  3495. (FileNameOnly, Data->SessionName, FileNameOnly));
  3496. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3497. {
  3498. Data->Terminal = Terminal;
  3499. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3500. Data->SessionName = Terminal->SessionData->SessionName;
  3501. // We might also overwrite session data
  3502. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3503. }
  3504. else
  3505. {
  3506. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3507. Abort();
  3508. }
  3509. }
  3510. }
  3511. // foreground session should reconnect itself
  3512. else if (Terminal != Data->Terminal)
  3513. {
  3514. UnicodeString Message =
  3515. MainInstructions(
  3516. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3517. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3518. {
  3519. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3520. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3521. }
  3522. else
  3523. {
  3524. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3525. Abort();
  3526. }
  3527. }
  3528. }
  3529. if (!IsActiveTerminal(Data->Terminal))
  3530. {
  3531. Configuration->Usage->Inc(L"EditInactiveSession");
  3532. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3533. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3534. (ExtractFileName(FileName), Data->SessionName)));
  3535. }
  3536. }
  3537. TStrings * FileList = new TStringList();
  3538. try
  3539. {
  3540. FileList->Add(FileName);
  3541. // consider using the same settings (preset) as when the file was downloaded
  3542. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3543. TemporaryFileCopyParam(CopyParam);
  3544. if (Data->ForceText)
  3545. {
  3546. CopyParam.TransferMode = tmAscii;
  3547. }
  3548. // so i do not need to worry if masking algorithm works in all cases
  3549. // ("" means "copy file name", no masking is actually done)
  3550. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3551. {
  3552. CopyParam.FileMask = L"";
  3553. }
  3554. else
  3555. {
  3556. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3557. }
  3558. DebugAssert(Data->Queue != NULL);
  3559. int Params = cpNoConfirmation | cpTemporary;
  3560. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3561. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3562. QueueItem->CompleteEvent = UploadCompleteEvent;
  3563. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3564. }
  3565. __finally
  3566. {
  3567. delete FileList;
  3568. }
  3569. }
  3570. //---------------------------------------------------------------------------
  3571. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3572. const UnicodeString FileName, const TEditedFileData * Data)
  3573. {
  3574. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3575. DebugAssert(!NonVisualDataModule->Busy);
  3576. if (!IsActiveTerminal(Data->Terminal))
  3577. {
  3578. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3579. (ExtractFileName(FileName), Data->SessionName)));
  3580. }
  3581. TManagedTerminal * PrevSession = TTerminalManager::Instance()->ActiveSession;
  3582. TTerminalManager::Instance()->ActiveSession = Data->Terminal;
  3583. NonVisualDataModule->StartBusy();
  3584. try
  3585. {
  3586. std::unique_ptr<TRemoteFile> File;
  3587. UnicodeString RemoteFileName =
  3588. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3589. Terminal->ExceptionOnFail = true;
  3590. try
  3591. {
  3592. TRemoteFile * AFile = NULL;
  3593. Terminal->ReadFile(RemoteFileName, AFile);
  3594. File.reset(AFile);
  3595. if (!File->HaveFullFileName)
  3596. {
  3597. File->FullFileName = RemoteFileName;
  3598. }
  3599. }
  3600. __finally
  3601. {
  3602. Terminal->ExceptionOnFail = false;
  3603. }
  3604. std::unique_ptr<TStrings> FileList(new TStringList());
  3605. FileList->AddObject(RemoteFileName, File.get());
  3606. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3607. UnicodeString TempDir = ExtractFilePath(FileName);
  3608. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3609. TempDir, true, true, true);
  3610. // sanity check, the target file name should be still the same
  3611. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3612. }
  3613. __finally
  3614. {
  3615. NonVisualDataModule->EndBusy();
  3616. // it actually may not exist anymore...
  3617. TTerminalManager::Instance()->ActiveSession = PrevSession;
  3618. }
  3619. }
  3620. //---------------------------------------------------------------------------
  3621. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3622. const TEditedFileData * Data, bool & KeepOpen)
  3623. {
  3624. // Command is set for external editors only (not for "shell" open).
  3625. if (!Data->Command.IsEmpty())
  3626. {
  3627. bool AnyFound = false;
  3628. bool AnyMDI = false;
  3629. bool AnyNonMDI = false;
  3630. bool AnyDetect = false;
  3631. TEditorList * EditorList = new TEditorList();
  3632. try
  3633. {
  3634. *EditorList = *WinConfiguration->EditorList;
  3635. for (int i = 0; i < EditorList->Count; i++)
  3636. {
  3637. const TEditorPreferences * Editor = EditorList->Editors[i];
  3638. if ((Editor->Data->Editor == edExternal) &&
  3639. (Editor->Data->ExternalEditor == Data->Command))
  3640. {
  3641. AnyFound = true;
  3642. if (Editor->Data->SDIExternalEditor)
  3643. {
  3644. AnyNonMDI = true;
  3645. if (Editor->Data->DetectMDIExternalEditor)
  3646. {
  3647. AnyDetect = true;
  3648. }
  3649. }
  3650. else
  3651. {
  3652. AnyMDI = true;
  3653. }
  3654. }
  3655. }
  3656. bool EnableMDI = false;
  3657. bool DisableDetect = false;
  3658. if (AnyMDI)
  3659. {
  3660. KeepOpen = true;
  3661. if (AnyNonMDI)
  3662. {
  3663. // there is at least one instance of the editor with MDI support enabled,
  3664. // and one with disabled, enable it for all instances
  3665. EnableMDI = true;
  3666. }
  3667. }
  3668. else if (AnyFound && !AnyDetect)
  3669. {
  3670. // at least once instance found but all have MDI autodetection disabled
  3671. // => close the file (default action)
  3672. }
  3673. else
  3674. {
  3675. // no instance of the editor has MDI support enabled
  3676. TMessageParams Params;
  3677. if (AnyFound)
  3678. {
  3679. // there is at least one instance of the editor
  3680. Params.Params |= mpNeverAskAgainCheck;
  3681. }
  3682. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3683. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3684. switch (Answer)
  3685. {
  3686. case qaNeverAskAgain:
  3687. DisableDetect = true;
  3688. break;
  3689. case qaNo:
  3690. EnableMDI = true;
  3691. KeepOpen = true;
  3692. break;
  3693. }
  3694. }
  3695. if (AnyFound && (EnableMDI || DisableDetect))
  3696. {
  3697. bool Changed = false;
  3698. for (int i = 0; i < EditorList->Count; i++)
  3699. {
  3700. const TEditorPreferences * Editor = EditorList->Editors[i];
  3701. if ((Editor->Data->Editor == edExternal) &&
  3702. (Editor->Data->ExternalEditor == Data->Command) &&
  3703. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3704. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3705. {
  3706. Changed = true;
  3707. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3708. if (EnableMDI)
  3709. {
  3710. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3711. }
  3712. if (DisableDetect)
  3713. {
  3714. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3715. }
  3716. EditorList->Change(i, UpdatedEditor);
  3717. }
  3718. }
  3719. if (Changed)
  3720. {
  3721. WinConfiguration->EditorList = EditorList;
  3722. }
  3723. }
  3724. }
  3725. __finally
  3726. {
  3727. delete EditorList;
  3728. }
  3729. }
  3730. else
  3731. {
  3732. // "open" case
  3733. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3734. qaOK, HELP_APP_EARLY_CLOSED);
  3735. }
  3736. }
  3737. //---------------------------------------------------------------------------
  3738. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3739. {
  3740. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3741. }
  3742. //---------------------------------------------------------------------------
  3743. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3744. {
  3745. SideEnter(osRemote);
  3746. }
  3747. //---------------------------------------------------------------------------
  3748. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3749. {
  3750. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3751. SideEnter(osRemote);
  3752. }
  3753. //---------------------------------------------------------------------------
  3754. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3755. {
  3756. FCurrentSide = Side;
  3757. if (Visible)
  3758. {
  3759. UpdateControls();
  3760. }
  3761. }
  3762. //---------------------------------------------------------------------------
  3763. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3764. TStrings * FileList, bool Alternative)
  3765. {
  3766. TCustomDirView * DView = DirView(Side);
  3767. DView->SaveSelection();
  3768. DView->SaveSelectedNames();
  3769. DebugAssert(!FAlternativeDelete);
  3770. FAlternativeDelete = Alternative;
  3771. try
  3772. {
  3773. if (!IsSideLocalBrowser(Side))
  3774. {
  3775. DebugAssert(Terminal != NULL);
  3776. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3777. }
  3778. else
  3779. {
  3780. if (IsLocalBrowserMode())
  3781. {
  3782. Configuration->Usage->Inc(L"LocalLocalDeletes");
  3783. }
  3784. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  3785. FProgressSide = FCurrentSide;
  3786. try
  3787. {
  3788. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3789. }
  3790. __finally
  3791. {
  3792. ReloadLocalDirectory();
  3793. }
  3794. }
  3795. FAlternativeDelete = false;
  3796. }
  3797. catch(...)
  3798. {
  3799. FAlternativeDelete = false;
  3800. DView->DiscardSavedSelection();
  3801. throw;
  3802. }
  3803. DView->RestoreSelection();
  3804. }
  3805. //---------------------------------------------------------------------------
  3806. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3807. {
  3808. DebugAssert(Terminal);
  3809. DebugAssert(!IsLocalBrowserMode());
  3810. RemoteDirView->SaveSelection();
  3811. RemoteDirView->SaveSelectedNames();
  3812. try
  3813. {
  3814. if (Lock)
  3815. {
  3816. Terminal->LockFiles(FileList);
  3817. }
  3818. else
  3819. {
  3820. Terminal->UnlockFiles(FileList);
  3821. }
  3822. }
  3823. catch(...)
  3824. {
  3825. RemoteDirView->DiscardSavedSelection();
  3826. throw;
  3827. }
  3828. RemoteDirView->RestoreSelection();
  3829. }
  3830. //---------------------------------------------------------------------------
  3831. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  3832. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3833. bool NoConfirmation, bool Move)
  3834. {
  3835. DebugAssert(Terminal != NULL);
  3836. DebugAssert(Terminal == TTerminalManager::Instance()->ActiveTerminal);
  3837. DebugAssert(!IsLocalBrowserMode());
  3838. if (Session == NULL)
  3839. {
  3840. Session = Terminal;
  3841. }
  3842. if (Session == Terminal)
  3843. {
  3844. if (RemoteDriveView->DropTarget != NULL)
  3845. {
  3846. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3847. }
  3848. else if (RemoteDirView->DropTarget != NULL)
  3849. {
  3850. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3851. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3852. }
  3853. else
  3854. {
  3855. Target = RemoteDirView->Path;
  3856. }
  3857. }
  3858. else
  3859. {
  3860. Target = GetSessionPath(Session, osRemote);
  3861. }
  3862. Target = UnixIncludeTrailingBackslash(Target);
  3863. if (FileList->Count == 1)
  3864. {
  3865. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3866. }
  3867. else
  3868. {
  3869. FileMask = AnyMask;
  3870. }
  3871. DirectCopy = Terminal->IsCapable[fcRemoteCopy] || Terminal->IsCapable[fcSecondaryShell];
  3872. bool Result = true;
  3873. if (!NoConfirmation)
  3874. {
  3875. bool Multi = (FileList->Count > 1);
  3876. if (Move)
  3877. {
  3878. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3879. }
  3880. else
  3881. {
  3882. std::unique_ptr<TStrings> Sessions(new TStringList);
  3883. std::unique_ptr<TStrings> Directories(new TStringList);
  3884. TStrings * SessionList = TTerminalManager::Instance()->SessionList;
  3885. for (int Index = 0; Index < SessionList->Count; Index++)
  3886. {
  3887. TManagedTerminal * ASession = DebugNotNull(dynamic_cast<TManagedTerminal *>(SessionList->Objects[Index]));
  3888. if (IsActiveTerminal(ASession) && DebugAlwaysTrue(!ASession->LocalBrowser))
  3889. {
  3890. Sessions->AddObject(SessionList->Strings[Index], ASession);
  3891. Directories->Add(GetSessionPath(ASession, osRemote));
  3892. }
  3893. }
  3894. TDirectRemoteCopy AllowDirectCopy;
  3895. if (Terminal->IsCapable[fcRemoteCopy] || Terminal->CommandSessionOpened)
  3896. {
  3897. DebugAssert(DirectCopy);
  3898. AllowDirectCopy = drcAllow;
  3899. }
  3900. else if (Terminal->IsCapable[fcSecondaryShell])
  3901. {
  3902. DebugAssert(DirectCopy);
  3903. AllowDirectCopy = drcConfirmCommandSession;
  3904. }
  3905. else
  3906. {
  3907. DebugAssert(!DirectCopy);
  3908. AllowDirectCopy = drcDisallow;
  3909. }
  3910. void * ASession = Session;
  3911. Result = DoRemoteCopyDialog(
  3912. Sessions.get(), Directories.get(), AllowDirectCopy, Multi, ASession, Target, FileMask, DirectCopy, Terminal);
  3913. Session = static_cast<TManagedTerminal *>(ASession);
  3914. }
  3915. }
  3916. return Result;
  3917. }
  3918. //---------------------------------------------------------------------------
  3919. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3920. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  3921. {
  3922. DebugAssert(!IsLocalBrowserMode());
  3923. bool DirectCopy;
  3924. UnicodeString Target, FileMask;
  3925. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3926. if (Result)
  3927. {
  3928. if (!Move && !DirectCopy)
  3929. {
  3930. Configuration->Usage->Inc("RemoteCopyTemp");
  3931. UnicodeString RootTempDir;
  3932. UnicodeString TempDir;
  3933. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3934. TStrings * TemporaryFilesList = new TStringList();
  3935. try
  3936. {
  3937. TMakeLocalFileListParams MakeFileListParam;
  3938. MakeFileListParam.FileList = TemporaryFilesList;
  3939. MakeFileListParam.FileTimes = NULL;
  3940. MakeFileListParam.IncludeDirs = true;
  3941. MakeFileListParam.Recursive = false;
  3942. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  3943. TTerminalManager::Instance()->ActiveSession = Session;
  3944. if (TemporaryFilesList->Count > 0)
  3945. {
  3946. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3947. CopyParam.FileMask = FileMask;
  3948. DebugAssert(!FAutoOperation);
  3949. FAutoOperation = true;
  3950. Terminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3951. }
  3952. }
  3953. __finally
  3954. {
  3955. delete TemporaryFilesList;
  3956. FAutoOperation = false;
  3957. if (!RootTempDir.IsEmpty())
  3958. {
  3959. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3960. }
  3961. }
  3962. }
  3963. else
  3964. {
  3965. RemoteDirView->SaveSelection();
  3966. RemoteDirView->SaveSelectedNames();
  3967. try
  3968. {
  3969. if (Move)
  3970. {
  3971. Configuration->Usage->Inc("RemoteMove");
  3972. Terminal->MoveFiles(FileList, Target, FileMask);
  3973. }
  3974. else
  3975. {
  3976. Configuration->Usage->Inc("RemoteCopyDirect");
  3977. DebugAssert(DirectCopy);
  3978. DebugAssert(Session == Terminal);
  3979. if (Terminal->IsCapable[fcRemoteCopy] ||
  3980. Terminal->CommandSessionOpened ||
  3981. CommandSessionFallback())
  3982. {
  3983. Terminal->CopyFiles(FileList, Target, FileMask);
  3984. }
  3985. }
  3986. }
  3987. catch(...)
  3988. {
  3989. RemoteDirView->DiscardSavedSelection();
  3990. throw;
  3991. }
  3992. RemoteDirView->RestoreSelection();
  3993. }
  3994. }
  3995. return Result;
  3996. }
  3997. //---------------------------------------------------------------------------
  3998. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3999. const UnicodeString & Path, TRemoteProperties & Properties)
  4000. {
  4001. DebugAssert(!IsLocalBrowserMode());
  4002. Properties.Valid = Properties.Valid << vpEncrypt;
  4003. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  4004. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  4005. }
  4006. //---------------------------------------------------------------------------
  4007. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  4008. {
  4009. Side = GetSide(Side);
  4010. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  4011. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  4012. UnicodeString Name = LoadStr(NEW_FOLDER);
  4013. int AllowedChanges =
  4014. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  4015. bool SaveSettings = false;
  4016. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  4017. {
  4018. TWindowLock Lock(this);
  4019. if (!IsSideLocalBrowser(Side))
  4020. {
  4021. if (SaveSettings)
  4022. {
  4023. GUIConfiguration->NewDirectoryProperties = Properties;
  4024. }
  4025. CreateRemoteDirectory(Name, Properties);
  4026. }
  4027. else
  4028. {
  4029. DirView(Side)->CreateDirectory(Name);
  4030. }
  4031. }
  4032. }
  4033. //---------------------------------------------------------------------------
  4034. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  4035. {
  4036. TWindowLock Lock(this);
  4037. DirView(Side)->ExecuteHomeDirectory();
  4038. }
  4039. //---------------------------------------------------------------------------
  4040. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  4041. {
  4042. DoOpenDirectoryDialog(odBrowse, Side);
  4043. }
  4044. //---------------------------------------------------------------------------
  4045. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  4046. {
  4047. TOperationSide BookmarkSide = IsLocalBrowserMode() ? osLocal : Side;
  4048. UnicodeString Path = Bookmark->GetSideDirectory(BookmarkSide);
  4049. bool Result = !Path.IsEmpty();
  4050. if (Result)
  4051. {
  4052. // While we might get here when the session is closed (from location profiles),
  4053. // it's not a problem as the Path setter is noop then
  4054. DirView(Side)->Path = Path;
  4055. }
  4056. return Result;
  4057. }
  4058. //---------------------------------------------------------------------------
  4059. void __fastcall TCustomScpExplorerForm::CalculateSize(
  4060. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  4061. bool & Close)
  4062. {
  4063. // terminal can be already closed (e.g. dropped connection)
  4064. if (Terminal != NULL)
  4065. {
  4066. try
  4067. {
  4068. TCalculateSizeParams Params;
  4069. Params.Stats = &Stats;
  4070. Terminal->CalculateFilesSize(FileList, Size, Params);
  4071. }
  4072. catch(...)
  4073. {
  4074. if (!Terminal->Active)
  4075. {
  4076. Close = true;
  4077. }
  4078. throw;
  4079. }
  4080. }
  4081. }
  4082. //---------------------------------------------------------------------------
  4083. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  4084. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  4085. bool & Close)
  4086. {
  4087. // terminal can be already closed (e.g. dropped connection)
  4088. if (Terminal != NULL)
  4089. {
  4090. Configuration->Usage->Inc(L"ChecksumCalculated");
  4091. try
  4092. {
  4093. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  4094. }
  4095. catch(...)
  4096. {
  4097. if (!Terminal->Active)
  4098. {
  4099. Close = true;
  4100. }
  4101. throw;
  4102. }
  4103. }
  4104. }
  4105. //---------------------------------------------------------------------------
  4106. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  4107. {
  4108. bool Result;
  4109. if (!IsSideLocalBrowser(Side))
  4110. {
  4111. TRemoteTokenList * GroupList = NULL;
  4112. TRemoteTokenList * UserList = NULL;
  4113. try
  4114. {
  4115. TRemoteProperties CurrentProperties;
  4116. if (Terminal->LoadFilesProperties(FileList))
  4117. {
  4118. RemoteDirView->Invalidate();
  4119. }
  4120. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4121. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4122. if (CapableGroupChanging || CapableOwnerChanging)
  4123. {
  4124. if (CapableGroupChanging)
  4125. {
  4126. GroupList = Terminal->Groups->Duplicate();
  4127. }
  4128. if (CapableOwnerChanging)
  4129. {
  4130. UserList = Terminal->Users->Duplicate();
  4131. }
  4132. TRemoteDirectory * Files = Terminal->Files;
  4133. int Count = Files->Count;
  4134. if (Count > 100)
  4135. {
  4136. Count = 100;
  4137. }
  4138. for (int Index = 0; Index < Count; Index++)
  4139. {
  4140. TRemoteFile * File = Files->Files[Index];
  4141. if (CapableGroupChanging)
  4142. {
  4143. GroupList->AddUnique(File->Group);
  4144. }
  4145. if (CapableOwnerChanging)
  4146. {
  4147. UserList->AddUnique(File->Owner);
  4148. }
  4149. }
  4150. // if we haven't collected tokens for all files in current directory,
  4151. // make sure we collect them at least for all selected files.
  4152. // (note that so far the files in FileList has to be from current direcotry)
  4153. if (Count < Files->Count)
  4154. {
  4155. for (int Index = 0; Index < FileList->Count; Index++)
  4156. {
  4157. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4158. if (CapableGroupChanging)
  4159. {
  4160. GroupList->AddUnique(File->Group);
  4161. }
  4162. if (CapableOwnerChanging)
  4163. {
  4164. UserList->AddUnique(File->Owner);
  4165. }
  4166. }
  4167. }
  4168. }
  4169. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4170. int Flags = 0;
  4171. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4172. if (CapableOwnerChanging) Flags |= cpOwner;
  4173. if (CapableGroupChanging) Flags |= cpGroup;
  4174. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4175. if (Terminal->IsCapable[fcCalculatingChecksum])
  4176. {
  4177. CalculateChecksumEvent = CalculateChecksum;
  4178. }
  4179. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4180. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4181. TRemoteProperties NewProperties = CurrentProperties;
  4182. Result =
  4183. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4184. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4185. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4186. CalculateSize, CalculateChecksumEvent);
  4187. if (Result)
  4188. {
  4189. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4190. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4191. }
  4192. }
  4193. __finally
  4194. {
  4195. delete GroupList;
  4196. delete UserList;
  4197. }
  4198. }
  4199. else
  4200. {
  4201. DirView(Side)->DisplayPropertiesMenu();
  4202. Result = true;
  4203. }
  4204. return Result;
  4205. }
  4206. //---------------------------------------------------------------------------
  4207. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4208. {
  4209. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4210. {
  4211. FIgnoreNextDialogChar = Key;
  4212. }
  4213. Key = 0;
  4214. }
  4215. //---------------------------------------------------------------------------
  4216. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4217. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4218. {
  4219. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4220. if (Command != NULL)
  4221. {
  4222. KeyProcessed(Key, Shift);
  4223. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4224. {
  4225. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4226. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4227. false, false, const_cast<TCustomCommandType *>(Command));
  4228. }
  4229. }
  4230. }
  4231. //---------------------------------------------------------------------------
  4232. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4233. {
  4234. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4235. {
  4236. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4237. }
  4238. if (!DirView(osCurrent)->IsEditing())
  4239. {
  4240. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4241. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4242. {
  4243. // Note that handling shortcuts on panel (current control) popup menu has precendence over this.
  4244. // But so far it's not a problem, as it does what we want.
  4245. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4246. if (((Action->ShortCut == KeyShortCut) ||
  4247. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4248. AllowedAction(Action, aaShortCut))
  4249. {
  4250. // Has to be called before the action as the dialog char is already in queue.
  4251. // So when the action consumes message queue, we already need to have the
  4252. // FIgnoreNextDialogChar set
  4253. KeyProcessed(Key, Shift);
  4254. // Reset reference to previous component (when menu/toolbar was clicked).
  4255. // Needed to detect that action was invoked by keyboard shortcut
  4256. // in TNonVisualDataModule::ExplorerActionsExecute
  4257. Action->ActionComponent = NULL;
  4258. Action->Execute();
  4259. return;
  4260. }
  4261. }
  4262. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4263. {
  4264. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4265. {
  4266. KeyProcessed(Key, Shift);
  4267. TTerminalManager::Instance()->ActiveSessionIndex = i;
  4268. return;
  4269. }
  4270. }
  4271. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4272. {
  4273. KeyProcessed(Key, Shift);
  4274. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4275. }
  4276. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4277. if (IsCustomShortCut(CustomShortCut))
  4278. {
  4279. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4280. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4281. if (WinConfiguration->SharedBookmarks != NULL)
  4282. {
  4283. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4284. if ((Bookmark != NULL) &&
  4285. OpenBookmark(FCurrentSide, Bookmark))
  4286. {
  4287. KeyProcessed(Key, Shift);
  4288. }
  4289. }
  4290. }
  4291. }
  4292. TForm::KeyDown(Key, Shift);
  4293. }
  4294. //---------------------------------------------------------------------------
  4295. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4296. {
  4297. bool OpenedTerminal = false;
  4298. UnicodeString ProtocolName;
  4299. UnicodeString ProtocolNameHint;
  4300. UnicodeString SecurityProtocolName;
  4301. UnicodeString DurationHint;
  4302. // See the comment in TScpCommanderForm::UpdateControls()
  4303. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4304. if (ActiveTerminal != NULL)
  4305. {
  4306. // Active is not enough here
  4307. if (ActiveTerminal->Status == ssOpened)
  4308. {
  4309. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4310. const TFileSystemInfo & FileSystemInfo = ActiveTerminal->GetFileSystemInfo();
  4311. if (FileSystemInfo.ProtocolName.IsEmpty())
  4312. {
  4313. ProtocolName = SessionInfo.ProtocolName;
  4314. }
  4315. else
  4316. {
  4317. ProtocolName = FileSystemInfo.ProtocolName;
  4318. }
  4319. ProtocolNameHint = LoadStr(STATUS_PROTOCOL_HINT);
  4320. SecurityProtocolName = SessionInfo.SecurityProtocolName;
  4321. DurationHint = LoadStr(STATUS_DURATION_HINT);
  4322. OpenedTerminal = true;
  4323. }
  4324. else if (ActiveTerminal->Status > ssClosed)
  4325. {
  4326. ProtocolName = LoadStr(STATUS_CONNECTING);
  4327. }
  4328. else
  4329. {
  4330. ProtocolName = LoadStr(STATUS_NOT_CONNECTED2);
  4331. }
  4332. }
  4333. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4334. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4335. TTBXStatusPanel * ProtocolStatusPanel = SessionStatusBar->Panels->Items[Offset + 0];
  4336. ProtocolStatusPanel->ViewPriority = (ActiveTerminal != NULL) ? 100 : 0;
  4337. if (ProtocolStatusPanel->Caption != ProtocolName)
  4338. {
  4339. ProtocolStatusPanel->Caption = ProtocolName;
  4340. std::unique_ptr<TCanvas> Canvas(CreateControlCanvas(SessionStatusBar));
  4341. ProtocolStatusPanel->Size = Canvas->TextWidth(ProtocolName) + ScaleByTextHeight(SessionStatusBar, 32);
  4342. }
  4343. ProtocolStatusPanel->Hint = ProtocolNameHint;
  4344. TTBXStatusPanel * SecurityStatusPanel = SessionStatusBar->Panels->Items[Offset + 1];
  4345. SecurityStatusPanel->ViewPriority = OpenedTerminal ? 98 : 0;
  4346. bool SecurityEnabled = !SecurityProtocolName.IsEmpty();
  4347. SecurityStatusPanel->Enabled = SecurityEnabled;
  4348. UnicodeString SecurityProtocolHint;
  4349. if (SecurityEnabled)
  4350. {
  4351. SecurityProtocolHint = FMTLOAD(STATUS_SECURE, (SecurityProtocolName));
  4352. }
  4353. else
  4354. {
  4355. SecurityProtocolHint = LoadStr(STATUS_INSECURE);
  4356. }
  4357. SecurityStatusPanel->Hint = SecurityProtocolHint;
  4358. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[Offset + 2];
  4359. DurationStatusPanel->ViewPriority = OpenedTerminal ? 99 : 0;
  4360. DurationStatusPanel->Hint = DurationHint;
  4361. UpdateStatusBar();
  4362. }
  4363. //---------------------------------------------------------------------------
  4364. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4365. {
  4366. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4367. DebugAssert(SessionStatusBar != NULL);
  4368. TTBXStatusPanel * NoteStatusPanel = SessionStatusBar->Panels->Items[0];
  4369. if (!FNote.IsEmpty())
  4370. {
  4371. NoteStatusPanel->Caption = FNote;
  4372. }
  4373. else
  4374. {
  4375. UpdateStatusPanelText(NoteStatusPanel);
  4376. }
  4377. NoteStatusPanel->Hint = FNoteHints;
  4378. UnicodeString LoginTime;
  4379. // See the comment in TScpCommanderForm::UpdateControls()
  4380. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4381. // Active is not enough here
  4382. if ((ActiveTerminal != NULL) && (ActiveTerminal->Status == ssOpened))
  4383. {
  4384. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4385. LoginTime = FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4386. }
  4387. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1];
  4388. DurationStatusPanel->Caption = LoginTime;
  4389. }
  4390. //---------------------------------------------------------------------------
  4391. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4392. {
  4393. Panel->Caption = L"";
  4394. }
  4395. //---------------------------------------------------------------------------
  4396. void __fastcall TCustomScpExplorerForm::Idle()
  4397. {
  4398. if (FShowing || FStandaloneEditing)
  4399. {
  4400. FEditorManager->Check();
  4401. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4402. // that needs to know if queue view is visible (and it may be closed after queue update)
  4403. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4404. }
  4405. if (FShowing)
  4406. {
  4407. if (!NonVisualDataModule->Busy &&
  4408. // Menu is opened or drag&drop is going on
  4409. (Mouse->Capture == NULL))
  4410. {
  4411. if (FRefreshRemoteDirectory)
  4412. {
  4413. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4414. {
  4415. Terminal->RefreshDirectory();
  4416. }
  4417. FRefreshRemoteDirectory = false;
  4418. }
  4419. if (FRefreshLocalDirectory)
  4420. {
  4421. ReloadLocalDirectory();
  4422. FRefreshLocalDirectory = false;
  4423. }
  4424. if (WinConfiguration->RefreshRemotePanel)
  4425. {
  4426. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4427. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4428. {
  4429. RemoteDirView->ReloadDirectory();
  4430. }
  4431. }
  4432. }
  4433. }
  4434. if (FShowing || FStandaloneEditing)
  4435. {
  4436. if (FQueueStatusInvalidated)
  4437. {
  4438. UpdateQueueStatus(false);
  4439. }
  4440. RefreshQueueItems();
  4441. }
  4442. if (FShowing)
  4443. {
  4444. UpdateStatusBar();
  4445. }
  4446. FIgnoreNextDialogChar = 0;
  4447. }
  4448. //---------------------------------------------------------------------------
  4449. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4450. {
  4451. try
  4452. {
  4453. FUserActionTimer->Enabled = false;
  4454. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4455. {
  4456. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4457. {
  4458. FPendingQueueActionItem->ProcessUserAction();
  4459. }
  4460. }
  4461. }
  4462. __finally
  4463. {
  4464. FPendingQueueActionItem = NULL;
  4465. }
  4466. }
  4467. //---------------------------------------------------------------------------
  4468. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4469. {
  4470. if (WinConfiguration->MinimizeToTray)
  4471. {
  4472. UpdateTrayIcon();
  4473. FTrayIcon->Visible = true;
  4474. if (Visible)
  4475. {
  4476. ShowWindow(Handle, SW_HIDE);
  4477. }
  4478. }
  4479. }
  4480. //---------------------------------------------------------------------------
  4481. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4482. {
  4483. // WORKAROUND
  4484. // When restoring maximized window from minimization,
  4485. // rarely some controls do not align properly.
  4486. // Two instances seen (both for Commander):
  4487. // - When restoring, window is temporarily narrower (not maximized),
  4488. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4489. // Once maximized already, top dock shinks back, but the session PageControl,
  4490. // do not align up, leaving space between TopDock and PageControl.
  4491. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4492. for (int Index = 0; Index < ControlCount; Index++)
  4493. {
  4494. RealignControl(Controls[Index]);
  4495. }
  4496. if (FTrayIcon->Visible)
  4497. {
  4498. FTrayIcon->Visible = false;
  4499. if (Visible)
  4500. {
  4501. ShowWindow(Handle, SW_SHOW);
  4502. }
  4503. }
  4504. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4505. {
  4506. FNeedSession = false;
  4507. NonVisualDataModule->StartBusy();
  4508. try
  4509. {
  4510. NeedSession(true);
  4511. }
  4512. __finally
  4513. {
  4514. NonVisualDataModule->EndBusy();
  4515. }
  4516. }
  4517. }
  4518. //---------------------------------------------------------------------------
  4519. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4520. {
  4521. FTrayIcon->Hint = GetMainForm()->Caption;
  4522. }
  4523. //---------------------------------------------------------------------------
  4524. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4525. {
  4526. UpdateTrayIcon();
  4527. }
  4528. //---------------------------------------------------------------------------
  4529. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4530. {
  4531. // workaround
  4532. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4533. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4534. // But WM_ACTIVATEAPP message can be received even when
  4535. // the main window is minimized to the tray and internal editor window is focused
  4536. // (after another application was previously active)
  4537. if (::IsIconic(Handle))
  4538. {
  4539. if (!IsAppIconic())
  4540. {
  4541. SetAppIconic(true);
  4542. }
  4543. }
  4544. ::ApplicationRestore();
  4545. Application->BringToFront();
  4546. }
  4547. //---------------------------------------------------------------------------
  4548. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4549. {
  4550. RestoreApp();
  4551. }
  4552. //---------------------------------------------------------------------------
  4553. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  4554. {
  4555. if (OpenInNewWindow())
  4556. {
  4557. ExecuteNewInstance(SessionUrl);
  4558. }
  4559. else
  4560. {
  4561. TTerminalManager::Instance()->NewSession(SessionUrl);
  4562. }
  4563. }
  4564. //---------------------------------------------------------------------------
  4565. void TCustomScpExplorerForm::NewTab(TOperationSide)
  4566. {
  4567. NewSession();
  4568. }
  4569. //---------------------------------------------------------------------------
  4570. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4571. {
  4572. UnicodeString SessionName = StoredSessions->HiddenPrefix + ManagedSession->SessionData->SessionName;
  4573. StoredSessions->NewSession(SessionName, SessionData);
  4574. // modified only, explicit
  4575. StoredSessions->Save(false, true);
  4576. // encode session name because of slashes in hierarchical sessions
  4577. return EncodeUrlString(SessionName);
  4578. }
  4579. //---------------------------------------------------------------------------
  4580. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4581. {
  4582. // current working directories become defaults here, what is not right
  4583. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4584. return SaveHiddenDuplicateSession(SessionData.get());
  4585. }
  4586. //---------------------------------------------------------------------------
  4587. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4588. {
  4589. if (OpenInNewWindow())
  4590. {
  4591. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4592. }
  4593. else
  4594. {
  4595. // current working directories become defaults here, what is not right
  4596. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4597. TTerminalManager * Manager = TTerminalManager::Instance();
  4598. TManagedTerminal * ASession = Manager->NewManagedTerminal(SessionData.get());
  4599. DebugAssert(ASession->LocalBrowser == ManagedSession->LocalBrowser);
  4600. // We definitelly want these
  4601. ASession->Disconnected = ManagedSession->Disconnected;
  4602. // Not sure about these two
  4603. ASession->Permanent = ManagedSession->Permanent;
  4604. ASession->DisconnectedTemporarily = ManagedSession->DisconnectedTemporarily;
  4605. Manager->ActiveSession = ASession;
  4606. }
  4607. }
  4608. //---------------------------------------------------------------------------
  4609. void __fastcall TCustomScpExplorerForm::RenameSession()
  4610. {
  4611. UnicodeString Name = ManagedSession->SessionData->SessionName;
  4612. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME) &&
  4613. // When submitting the default name, do not fix it
  4614. (Name != ManagedSession->SessionData->SessionName))
  4615. {
  4616. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4617. TSessionData::ValidateName(Name);
  4618. // This is inconsistent with how color (for example) is handled.
  4619. ManagedSession->SessionData->Name = Name;
  4620. UpdateControls();
  4621. // Add/Remove distinguishing paths from sessions of the same name.
  4622. SessionListChanged();
  4623. }
  4624. }
  4625. //---------------------------------------------------------------------------
  4626. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4627. {
  4628. DebugAssert(Queue != NULL);
  4629. bool Result = Queue->IsEmpty;
  4630. if (!Result)
  4631. {
  4632. SetFocus();
  4633. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4634. }
  4635. return Result;
  4636. }
  4637. //---------------------------------------------------------------------------
  4638. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4639. {
  4640. return CanCloseQueue(FQueue);
  4641. }
  4642. //---------------------------------------------------------------------------
  4643. void __fastcall TCustomScpExplorerForm::CloseSession()
  4644. {
  4645. if (CanCloseQueue())
  4646. {
  4647. TTerminalManager::Instance()->FreeActiveTerminal();
  4648. }
  4649. }
  4650. //---------------------------------------------------------------------------
  4651. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4652. {
  4653. if (CanCloseQueue())
  4654. {
  4655. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4656. }
  4657. }
  4658. //---------------------------------------------------------------------------
  4659. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4660. {
  4661. DetachTerminal(Terminal);
  4662. RemoteDirView->Terminal = Terminal;
  4663. UpdateRemotePathComboBox(false);
  4664. InitStatusBar();
  4665. UpdateControls();
  4666. }
  4667. //---------------------------------------------------------------------------
  4668. void __fastcall TCustomScpExplorerForm::TerminalConnecting()
  4669. {
  4670. InitStatusBar();
  4671. }
  4672. //---------------------------------------------------------------------------
  4673. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4674. {
  4675. if (DebugAlwaysTrue(!Terminal->Active))
  4676. {
  4677. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4678. }
  4679. }
  4680. //---------------------------------------------------------------------------
  4681. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4682. {
  4683. if (OpenInNewWindow())
  4684. {
  4685. // encode session name because of slashes in hierarchical sessions
  4686. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4687. }
  4688. else
  4689. {
  4690. TTerminalManager * Manager = TTerminalManager::Instance();
  4691. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4692. Manager->ActiveSession = Terminal;
  4693. }
  4694. }
  4695. //---------------------------------------------------------------------------
  4696. void __fastcall TCustomScpExplorerForm::DoOpenFolderOrWorkspace(const UnicodeString & Name, bool ConnectFirstTerminal)
  4697. {
  4698. TTerminalManager * Manager = TTerminalManager::Instance();
  4699. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4700. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4701. TManagedTerminal * FirstSession = Manager->NewSessions(DataList.get());
  4702. // FirstSession can be null, if some of the
  4703. if (!ConnectFirstTerminal && (FirstSession != NULL))
  4704. {
  4705. FirstSession->Disconnected = true;
  4706. FirstSession->DisconnectedTemporarily = true;
  4707. }
  4708. Manager->ActiveSession = FirstSession;
  4709. }
  4710. //---------------------------------------------------------------------------
  4711. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4712. {
  4713. if (OpenInNewWindow())
  4714. {
  4715. ExecuteNewInstance(Name);
  4716. }
  4717. else
  4718. {
  4719. DoOpenFolderOrWorkspace(Name, true);
  4720. }
  4721. }
  4722. //---------------------------------------------------------------------------
  4723. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4724. bool &CanClose)
  4725. {
  4726. // See the comment in CloseApp()
  4727. if (NonVisualDataModule->Busy)
  4728. {
  4729. CanClose = false;
  4730. }
  4731. else
  4732. {
  4733. TTerminalManager * Manager = TTerminalManager::Instance();
  4734. int ActiveSessions = 0;
  4735. for (int Index = 0; Index < Manager->Count; Index++)
  4736. {
  4737. if (Manager->Sessions[Index]->Active)
  4738. {
  4739. ActiveSessions++;
  4740. }
  4741. }
  4742. // Confirm, when there's at least one active remote session or
  4743. // when there are multiple (inactive or local) sessions and auto workspace saving is not enabled.
  4744. if (((ActiveSessions > 0) || ((Manager->Count > 1) && !WinConfiguration->AutoSaveWorkspace)) &&
  4745. WinConfiguration->ConfirmClosingSession)
  4746. {
  4747. unsigned int Result;
  4748. TMessageParams Params(mpNeverAskAgainCheck);
  4749. UnicodeString Message;
  4750. int Answers = qaOK | qaCancel;
  4751. // The current session is the only active session
  4752. if ((ActiveSessions == 1) && (Terminal != NULL) && Terminal->Active)
  4753. {
  4754. if (!WinConfiguration->AutoSaveWorkspace)
  4755. {
  4756. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  4757. }
  4758. else
  4759. {
  4760. Message = LoadStr(CLOSE_SESSION);
  4761. }
  4762. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  4763. }
  4764. // Multiple active sessions or one active session, but it's not the current one
  4765. else if (ActiveSessions > 0)
  4766. {
  4767. if (!WinConfiguration->AutoSaveWorkspace)
  4768. {
  4769. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  4770. }
  4771. else
  4772. {
  4773. Message = LoadStr(CLOSE_SESSIONS);
  4774. }
  4775. }
  4776. // Multiple tabs, but all are intactive or local, and saving of worspace is not enabled
  4777. else
  4778. {
  4779. DebugAlwaysTrue(Manager->Count > 1);
  4780. DebugAlwaysTrue(!WinConfiguration->AutoSaveWorkspace);
  4781. Message = LoadStr(CLOSE_WORKSPACE);
  4782. }
  4783. UnicodeString Note;
  4784. if (WinConfiguration->AutoSaveWorkspace)
  4785. {
  4786. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  4787. }
  4788. else
  4789. {
  4790. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  4791. Answers = qaYes | qaNo | qaCancel;
  4792. }
  4793. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  4794. SetFocus();
  4795. Result = MessageDialog(Message, qtConfirmation,
  4796. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  4797. if (Result == qaNeverAskAgain)
  4798. {
  4799. WinConfiguration->ConfirmClosingSession = false;
  4800. }
  4801. if (Result == qaNo)
  4802. {
  4803. CanClose = SaveWorkspace(true);
  4804. // note that the workspace will be saved redundatly again from FormClose
  4805. }
  4806. else
  4807. {
  4808. CanClose =
  4809. (Result == qaOK) ||
  4810. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4811. (Result == qaNeverAskAgain);
  4812. }
  4813. }
  4814. if (CanClose && HasActiveTerminal())
  4815. {
  4816. CanClose = CanCloseQueue();
  4817. }
  4818. }
  4819. if (CanClose)
  4820. {
  4821. CanClose =
  4822. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4823. FEditorManager->CloseExternalFilesWithoutProcess();
  4824. if (CanClose)
  4825. {
  4826. while (CanClose && (FLocalEditors->Count > 0))
  4827. {
  4828. int PrevCount = FLocalEditors->Count;
  4829. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4830. CanClose = (FLocalEditors->Count < PrevCount);
  4831. }
  4832. if (CanClose)
  4833. {
  4834. CanClose = FEditorManager->Empty(true);
  4835. if (!CanClose)
  4836. {
  4837. SetFocus();
  4838. CanClose =
  4839. (MessageDialog(
  4840. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4841. }
  4842. }
  4843. }
  4844. }
  4845. }
  4846. //---------------------------------------------------------------------------
  4847. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4848. {
  4849. TForm * Form = dynamic_cast<TForm *>(Sender);
  4850. DebugAssert(Form != NULL);
  4851. Form->Close();
  4852. }
  4853. //---------------------------------------------------------------------------
  4854. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4855. {
  4856. TForm * Form = dynamic_cast<TForm *>(Sender);
  4857. delete Form;
  4858. }
  4859. //---------------------------------------------------------------------------
  4860. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4861. {
  4862. while (FLocalEditors->Count > 0)
  4863. {
  4864. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4865. }
  4866. }
  4867. //---------------------------------------------------------------------------
  4868. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4869. TObject *Sender)
  4870. {
  4871. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4872. try
  4873. {
  4874. SetProperties(osRemote, FileList);
  4875. }
  4876. __finally
  4877. {
  4878. delete FileList;
  4879. }
  4880. }
  4881. //---------------------------------------------------------------------------
  4882. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4883. {
  4884. if (value)
  4885. {
  4886. if (Component == fcCustomCommandsBand)
  4887. {
  4888. UpdateCustomCommandsToolbar();
  4889. }
  4890. if (Component == fcQueueFileList)
  4891. {
  4892. DebugAssert(!QueueFileListSplitter->Visible);
  4893. AdjustQueueLayout();
  4894. UpdateQueueFileList();
  4895. }
  4896. }
  4897. }
  4898. //---------------------------------------------------------------------------
  4899. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  4900. {
  4901. int MinHeightLimit =
  4902. GetStaticQueuePanelComponentsHeight() +
  4903. QueueFileListSplitter->Height +
  4904. QueueFileList->Height +
  4905. GetMinQueueViewHeight();
  4906. if (QueuePanel->ClientHeight < MinHeightLimit)
  4907. {
  4908. QueuePanel->ClientHeight = MinHeightLimit;
  4909. }
  4910. }
  4911. //---------------------------------------------------------------------------
  4912. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4913. {
  4914. TControl * Control = GetComponent(Component);
  4915. DebugAssert(Control);
  4916. bool Changed = (Control->Visible != value);
  4917. if (Changed)
  4918. {
  4919. ComponentShowing(Component, value);
  4920. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4921. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4922. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4923. if (value)
  4924. {
  4925. int RemainingHeight = Control->Parent->ClientHeight;
  4926. int RemainingWidth = Control->Parent->ClientWidth;
  4927. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4928. {
  4929. TControl * ChildControl = Control->Parent->Controls[i];
  4930. if (ChildControl->Visible)
  4931. {
  4932. switch (ChildControl->Align)
  4933. {
  4934. case alTop:
  4935. case alBottom:
  4936. RemainingHeight -= ChildControl->Height;
  4937. break;
  4938. case alLeft:
  4939. case alRight:
  4940. RemainingWidth -= ChildControl->Width;
  4941. break;
  4942. }
  4943. }
  4944. }
  4945. int Reserve = ScaleByTextHeight(this, 32);
  4946. // queue in explorer, trees in commander
  4947. if (Control->Height > RemainingHeight - Reserve)
  4948. {
  4949. Control->Height = RemainingHeight / 2;
  4950. }
  4951. if (Control->Width > RemainingWidth - Reserve)
  4952. {
  4953. Control->Width = RemainingWidth / 2;
  4954. }
  4955. }
  4956. Control->Visible = value;
  4957. if (WasFocused && Visible)
  4958. {
  4959. DirView(osCurrent)->SetFocus();
  4960. }
  4961. FixControlsPlacement();
  4962. }
  4963. }
  4964. //---------------------------------------------------------------------------
  4965. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4966. {
  4967. TControl * Control = GetComponent(Component);
  4968. if (Control == NULL)
  4969. {
  4970. return false;
  4971. }
  4972. else
  4973. {
  4974. return Control->Visible;
  4975. }
  4976. }
  4977. //---------------------------------------------------------------------------
  4978. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4979. {
  4980. bool Result;
  4981. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4982. {
  4983. Result = !IsUWP();
  4984. }
  4985. else
  4986. {
  4987. Result = true;
  4988. }
  4989. return Result;
  4990. }
  4991. //---------------------------------------------------------------------------
  4992. void __fastcall TCustomScpExplorerForm::MakeFocusedItemVisible(TCustomDirView * DirView)
  4993. {
  4994. if (DirView->ItemFocused != NULL)
  4995. {
  4996. DirView->ItemFocused->MakeVisible(false);
  4997. }
  4998. }
  4999. //---------------------------------------------------------------------------
  5000. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  5001. {
  5002. MakeFocusedItemVisible(RemoteDirView);
  5003. QueueSplitter->Visible = QueuePanel->Visible;
  5004. QueueFileListSplitter->Visible = QueueFileList->Visible;
  5005. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  5006. TControl * QueueControlsOrder[] =
  5007. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  5008. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  5009. }
  5010. //---------------------------------------------------------------------------
  5011. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  5012. {
  5013. switch (Component) {
  5014. case fcStatusBar: return RemoteStatusBar;
  5015. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  5016. case fcQueueView: return QueuePanel;
  5017. case fcQueueToolbar: return QueueDock;
  5018. case fcRemoteTree: return RemoteDrivePanel;
  5019. case fcSessionsTabs: return SessionsPageControl;
  5020. case fcQueueFileList: return QueueFileList;
  5021. default: return NULL;
  5022. }
  5023. }
  5024. //---------------------------------------------------------------------------
  5025. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  5026. TObject *Sender, TListColumn *Column, TPoint &Point)
  5027. {
  5028. DebugAssert(NonVisualDataModule && Column && Sender);
  5029. NonVisualDataModule->ListColumn = Column;
  5030. TPopupMenu * DirViewColumnMenu;
  5031. if (Sender == DirView(osOther))
  5032. {
  5033. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  5034. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  5035. (Column->Index == uvName);
  5036. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  5037. (Column->Index == uvSize);
  5038. }
  5039. else
  5040. {
  5041. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  5042. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  5043. (Column->Index == dvName);
  5044. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  5045. (Column->Index == dvSize);
  5046. }
  5047. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  5048. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  5049. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  5050. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  5051. }
  5052. //---------------------------------------------------------------------------
  5053. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  5054. TObject *Sender, TListItem *Item, bool &AllowExec)
  5055. {
  5056. DoDirViewExecFile(Sender, Item, AllowExec);
  5057. }
  5058. //---------------------------------------------------------------------------
  5059. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  5060. TListItem * Item, bool & AllowExec)
  5061. {
  5062. DebugAssert(Sender && Item && Configuration);
  5063. DebugAssert(AllowExec);
  5064. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  5065. TOperationSide Side = ((ADirView == DirView(osRemote)) ? osRemote : osLocal);
  5066. bool Remote = !IsSideLocalBrowser(Side);
  5067. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles();
  5068. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  5069. // on files only (not directories)
  5070. // and only when symlinks are resolved (apply to remote panel only)
  5071. if (!ADirView->ItemIsDirectory(Item) &&
  5072. (ResolvedSymlinks || FForceExecution))
  5073. {
  5074. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  5075. !FForceExecution && ResolvedSymlinks)
  5076. {
  5077. if (WinConfiguration->DoubleClickAction == dcaCopy)
  5078. {
  5079. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  5080. UnlockWindow();
  5081. try
  5082. {
  5083. if (IsLocalBrowserMode())
  5084. {
  5085. LocalLocalCopy(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation, false, 0);
  5086. }
  5087. else
  5088. {
  5089. ExecuteFileOperation(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  5090. }
  5091. }
  5092. __finally
  5093. {
  5094. LockWindow();
  5095. }
  5096. AllowExec = false;
  5097. }
  5098. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  5099. {
  5100. if (!Remote || !WinConfiguration->DisableOpenEdit)
  5101. {
  5102. ExecuteFile(Side, efDefaultEditor);
  5103. AllowExec = false;
  5104. }
  5105. }
  5106. else
  5107. {
  5108. DebugFail();
  5109. }
  5110. }
  5111. // if we have not done anything special, fall back to default behavior
  5112. if (AllowExec)
  5113. {
  5114. if (Remote && !WinConfiguration->DisableOpenEdit)
  5115. {
  5116. ExecuteFile(osRemote, efShell);
  5117. AllowExec = false;
  5118. }
  5119. }
  5120. }
  5121. }
  5122. //---------------------------------------------------------------------------
  5123. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5124. {
  5125. return ((Side == osRemote) || (Side == osCurrent));
  5126. }
  5127. //---------------------------------------------------------------------------
  5128. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5129. {
  5130. DebugFail();
  5131. }
  5132. //---------------------------------------------------------------------------
  5133. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5134. {
  5135. DebugFail();
  5136. }
  5137. //---------------------------------------------------------------------------
  5138. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5139. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5140. {
  5141. TSynchronizeParamType Params;
  5142. Params.LocalDirectory = LocalDirectory;
  5143. Params.RemoteDirectory = RemoteDirectory;
  5144. int UnusedParams =
  5145. (GUIConfiguration->SynchronizeParams &
  5146. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5147. TTerminal::spCaseSensitive));
  5148. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5149. Params.Options = GUIConfiguration->SynchronizeOptions;
  5150. bool SaveSettings = false;
  5151. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5152. &DoSynchronizeTooManyDirectories);
  5153. DebugAssert(FSynchronizeController == NULL);
  5154. FSynchronizeController = &Controller;
  5155. bool Result;
  5156. try
  5157. {
  5158. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5159. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5160. int Options =
  5161. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5162. DebugAssert(FOnFeedSynchronizeError == NULL);
  5163. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5164. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5165. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  5166. if (Result)
  5167. {
  5168. if (SaveSettings)
  5169. {
  5170. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5171. GUIConfiguration->SynchronizeOptions = Params.Options;
  5172. }
  5173. else
  5174. {
  5175. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5176. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5177. FLAGSET(Params.Options, soSynchronize))
  5178. {
  5179. GUIConfiguration->SynchronizeOptions =
  5180. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5181. soSynchronize;
  5182. }
  5183. }
  5184. LocalDirectory = Params.LocalDirectory;
  5185. RemoteDirectory = Params.RemoteDirectory;
  5186. }
  5187. }
  5188. __finally
  5189. {
  5190. FSynchronizeController = NULL;
  5191. DebugAssert(FOnFeedSynchronizeError == NULL);
  5192. FOnFeedSynchronizeError = NULL;
  5193. }
  5194. return Result;
  5195. }
  5196. //---------------------------------------------------------------------------
  5197. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5198. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5199. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5200. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5201. TSynchronizeOptions * Options, bool Full)
  5202. {
  5203. // Keep-up-to-date mode
  5204. if (Terminal->Status == ssOpened)
  5205. {
  5206. try
  5207. {
  5208. int PParams = Params.Params;
  5209. if (!Full)
  5210. {
  5211. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5212. if (FLAGSET(Params.Options, soRecurse))
  5213. {
  5214. PParams |= TTerminal::spSubDirs;
  5215. }
  5216. }
  5217. else
  5218. {
  5219. // if keepuptodate is non-recursive,
  5220. // full sync before has to be non-recursive as well
  5221. if (FLAGCLEAR(Params.Options, soRecurse))
  5222. {
  5223. PParams |= TTerminal::spNoRecurse;
  5224. }
  5225. }
  5226. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5227. PParams, Checklist, Options);
  5228. }
  5229. catch (Exception & E)
  5230. {
  5231. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  5232. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5233. {
  5234. // noop, already logged in MoreMessageDialog
  5235. }
  5236. else
  5237. {
  5238. ShowExtendedExceptionEx(Terminal, &E);
  5239. throw;
  5240. }
  5241. }
  5242. }
  5243. }
  5244. //---------------------------------------------------------------------------
  5245. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5246. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5247. const UnicodeString ErrorStr)
  5248. {
  5249. if (!Directory.IsEmpty())
  5250. {
  5251. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5252. }
  5253. else
  5254. {
  5255. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5256. }
  5257. }
  5258. //---------------------------------------------------------------------------
  5259. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5260. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5261. {
  5262. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5263. {
  5264. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5265. }
  5266. else
  5267. {
  5268. TMessageParams Params(mpNeverAskAgainCheck);
  5269. unsigned int Result = MessageDialog(
  5270. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5271. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5272. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5273. {
  5274. MaxDirectories *= 2;
  5275. if (Result == qaNeverAskAgain)
  5276. {
  5277. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5278. }
  5279. }
  5280. else
  5281. {
  5282. Abort();
  5283. }
  5284. }
  5285. }
  5286. //---------------------------------------------------------------------------
  5287. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5288. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5289. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5290. TSynchronizeOptions * Options)
  5291. {
  5292. // Keep-up-to-date mode
  5293. DebugAssert(!FAutoOperation);
  5294. void * BatchStorage;
  5295. BatchStart(BatchStorage);
  5296. FAutoOperation = true;
  5297. bool AnyOperation = false;
  5298. TDateTime StartTime = Now();
  5299. TSynchronizeChecklist * AChecklist = NULL;
  5300. try
  5301. {
  5302. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5303. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5304. {
  5305. FSynchronizeProgressForm->Start();
  5306. }
  5307. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5308. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5309. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5310. Options);
  5311. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5312. SAFE_DESTROY(FSynchronizeProgressForm);
  5313. AnyOperation = (AChecklist->CheckedCount > 0);
  5314. if (AnyOperation)
  5315. {
  5316. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5317. CreateProgressForm(&SynchronizeProgress);
  5318. try
  5319. {
  5320. Terminal->SynchronizeApply(
  5321. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5322. SynchronizeProcessedItem, NULL, NULL, NULL);
  5323. }
  5324. __finally
  5325. {
  5326. DestroyProgressForm();
  5327. }
  5328. }
  5329. }
  5330. __finally
  5331. {
  5332. if (Checklist == NULL)
  5333. {
  5334. delete AChecklist;
  5335. }
  5336. else
  5337. {
  5338. *Checklist = AChecklist;
  5339. }
  5340. FAutoOperation = false;
  5341. SAFE_DESTROY(FSynchronizeProgressForm);
  5342. BatchEnd(BatchStorage);
  5343. ReloadLocalDirectory();
  5344. if (AnyOperation)
  5345. {
  5346. OperationComplete(StartTime);
  5347. }
  5348. }
  5349. }
  5350. //---------------------------------------------------------------------------
  5351. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5352. {
  5353. // can be called from command line
  5354. return Visible &&
  5355. ((DirView(osRemote)->SelCount > 0) ||
  5356. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5357. }
  5358. //---------------------------------------------------------------------------
  5359. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5360. {
  5361. LogSynchronizeEvent(Terminal, Message);
  5362. }
  5363. //---------------------------------------------------------------------------
  5364. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5365. int Params, TSynchronizeOptions & Options)
  5366. {
  5367. DebugAssert(!IsLocalBrowserMode());
  5368. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5369. {
  5370. Options.Filter = new TStringList();
  5371. Options.Filter->CaseSensitive = false;
  5372. Options.Filter->Duplicates = Types::dupAccept;
  5373. if (DirView(osRemote)->SelCount > 0)
  5374. {
  5375. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5376. }
  5377. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5378. {
  5379. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5380. }
  5381. Options.Filter->Sort();
  5382. }
  5383. }
  5384. //---------------------------------------------------------------------------
  5385. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5386. {
  5387. TCopyParamType Defaults;
  5388. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5389. ConfigStorage->AccessMode = smRead;
  5390. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5391. {
  5392. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5393. }
  5394. ConfigStorage.reset(NULL);
  5395. std::unique_ptr<TStringList> Options(new TStringList());
  5396. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5397. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5398. UnicodeString Result;
  5399. if (Options->Count > 0)
  5400. {
  5401. Result =
  5402. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5403. }
  5404. return Result;
  5405. }
  5406. //---------------------------------------------------------------------------
  5407. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5408. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5409. {
  5410. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5411. UnicodeString AdditionalParams =
  5412. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5413. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5414. SerializeCopyParamForCommandLine(CopyParams),
  5415. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5416. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5417. ExecuteNewInstance(SessionName, AdditionalParams);
  5418. }
  5419. //---------------------------------------------------------------------------
  5420. struct TSynchronizeParams
  5421. {
  5422. UnicodeString LocalDirectory;
  5423. UnicodeString RemoteDirectory;
  5424. TSynchronizeMode Mode;
  5425. TSynchronizeChecklist * Checklist;
  5426. int Params;
  5427. TCopyParamType * CopyParam;
  5428. TDateTime CollectElapsed;
  5429. TDateTime * StartTime;
  5430. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5431. };
  5432. //---------------------------------------------------------------------------
  5433. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5434. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5435. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5436. {
  5437. DebugAssert(!FAutoOperation);
  5438. void * BatchStorage;
  5439. BatchStart(BatchStorage);
  5440. FAutoOperation = true;
  5441. TFileOperationStatistics Statistics;
  5442. TDateTime Start = Now();
  5443. try
  5444. {
  5445. Params.OnProcessedItem = OnProcessedItem;
  5446. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5447. CreateProgressForm(&SynchronizeProgress);
  5448. Terminal->SynchronizeApply(
  5449. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5450. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5451. &Statistics);
  5452. }
  5453. __finally
  5454. {
  5455. Params.OnProcessedItem = NULL;
  5456. FAutoOperation = false;
  5457. DestroyProgressForm();
  5458. BatchEnd(BatchStorage);
  5459. ReloadLocalDirectory();
  5460. }
  5461. if (WinConfiguration->SynchronizeSummary)
  5462. {
  5463. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5464. // The statistics should be 0 anyway in this case
  5465. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5466. {
  5467. Message += L"\n";
  5468. if (Statistics.FilesUploaded > 0)
  5469. {
  5470. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5471. }
  5472. if (Statistics.FilesDownloaded > 0)
  5473. {
  5474. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5475. }
  5476. if (Statistics.FilesDeletedLocal > 0)
  5477. {
  5478. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5479. }
  5480. if (Statistics.FilesDeletedRemote > 0)
  5481. {
  5482. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5483. }
  5484. }
  5485. TDateTime Elapsed = (Now() - Start);
  5486. Message +=
  5487. L"\n" +
  5488. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5489. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5490. TMessageParams Params(mpNeverAskAgainCheck);
  5491. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5492. if (Result == qaNeverAskAgain)
  5493. {
  5494. WinConfiguration->SynchronizeSummary = false;
  5495. }
  5496. }
  5497. }
  5498. //---------------------------------------------------------------------------
  5499. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5500. {
  5501. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5502. {
  5503. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5504. }
  5505. // Not set in keep-up-to-date mode - Synchronize() method
  5506. if (Token != NULL)
  5507. {
  5508. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5509. if (Params.OnProcessedItem != NULL)
  5510. {
  5511. Params.OnProcessedItem(NULL, ChecklistItem);
  5512. }
  5513. }
  5514. }
  5515. //---------------------------------------------------------------------------
  5516. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5517. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5518. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5519. {
  5520. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5521. *Params.StartTime = Now();
  5522. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5523. }
  5524. //---------------------------------------------------------------------------
  5525. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5526. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5527. {
  5528. // terminal can be already closed (e.g. dropped connection)
  5529. if (Terminal != NULL)
  5530. {
  5531. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5532. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5533. }
  5534. }
  5535. //---------------------------------------------------------------------------
  5536. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5537. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5538. {
  5539. DebugAssert(!IsLocalBrowserMode());
  5540. TAutoBatch AutoBatch(this);
  5541. TAutoFlag AutoOperationFlag(FAutoOperation);
  5542. if (Side == osRemote)
  5543. {
  5544. std::unique_ptr<TStrings> FileList(new TStringList());
  5545. FileList->AddObject(FileName, RemoteFile);
  5546. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5547. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5548. RemoteDirView->SaveSelection();
  5549. RemoteDirView->SaveSelectedNames();
  5550. try
  5551. {
  5552. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5553. }
  5554. catch(...)
  5555. {
  5556. RemoteDirView->DiscardSavedSelection();
  5557. throw;
  5558. }
  5559. RemoteDirView->RestoreSelection();
  5560. }
  5561. else if (DebugAlwaysTrue(Side == osLocal))
  5562. {
  5563. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5564. {
  5565. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5566. }
  5567. UnicodeString Directory = ExtractFileDir(FileName);
  5568. ReloadLocalDirectory(Directory);
  5569. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5570. if (!SamePaths(Directory, NewDirectory))
  5571. {
  5572. ReloadLocalDirectory(NewDirectory);
  5573. }
  5574. }
  5575. }
  5576. //---------------------------------------------------------------------------
  5577. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5578. {
  5579. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5580. if (Side == osLocal)
  5581. {
  5582. if (Action == TSynchronizeChecklist::saDownloadNew)
  5583. {
  5584. OpenFolderInExplorer(LocalPath);
  5585. }
  5586. else
  5587. {
  5588. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5589. }
  5590. }
  5591. else if (DebugAlwaysTrue(Side == osRemote))
  5592. {
  5593. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5594. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5595. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5596. if (!LocalPath.IsEmpty())
  5597. {
  5598. SessionData->LocalDirectory = LocalPath;
  5599. }
  5600. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5601. ExecuteNewInstance(SessionName, FORMAT(L"%s=%s", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5602. }
  5603. }
  5604. //---------------------------------------------------------------------------
  5605. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5606. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5607. const TCopyParamType * CopyParams)
  5608. {
  5609. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5610. UnicodeString AdditionalParams =
  5611. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5612. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5613. SerializeCopyParamForCommandLine(CopyParams),
  5614. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5615. LocalDirectory, RemoteDirectory, Mode, Params));
  5616. ExecuteNewInstance(SessionName, AdditionalParams);
  5617. }
  5618. //---------------------------------------------------------------------------
  5619. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5620. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5621. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5622. {
  5623. int Result;
  5624. bool SaveSettings = false;
  5625. int Options =
  5626. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5627. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5628. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5629. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5630. bool Continue =
  5631. (UseDefaults == 0) ||
  5632. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5633. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5634. if (Continue)
  5635. {
  5636. Configuration->Usage->Inc(L"Synchronizations");
  5637. UpdateCopyParamCounters(CopyParam);
  5638. TSynchronizeOptions SynchronizeOptions;
  5639. GetSynchronizeOptions(Params, SynchronizeOptions);
  5640. if (SaveSettings)
  5641. {
  5642. GUIConfiguration->SynchronizeParams = Params;
  5643. }
  5644. else
  5645. {
  5646. SaveMode = false;
  5647. }
  5648. TDateTime StartTime = Now();
  5649. TSynchronizeChecklist * Checklist = NULL;
  5650. try
  5651. {
  5652. DebugAssert(!FAutoOperation);
  5653. FAutoOperation = true;
  5654. try
  5655. {
  5656. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5657. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5658. int Files = -1;
  5659. if (DataList->Count >= 1)
  5660. {
  5661. Files = StrToIntDef(DataList->Strings[0], Files);
  5662. }
  5663. else
  5664. {
  5665. DataList->Add(UnicodeString());
  5666. }
  5667. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5668. FSynchronizeProgressForm->Start();
  5669. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5670. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5671. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5672. &SynchronizeOptions);
  5673. if (Terminal->SessionData->CacheDirectories)
  5674. {
  5675. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5676. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5677. }
  5678. }
  5679. __finally
  5680. {
  5681. FAutoOperation = false;
  5682. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5683. SAFE_DESTROY(FSynchronizeProgressForm);
  5684. }
  5685. TSynchronizeParams SynchronizeParams;
  5686. SynchronizeParams.LocalDirectory = LocalDirectory;
  5687. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5688. SynchronizeParams.Mode = Mode;
  5689. SynchronizeParams.CopyParam = &CopyParam;
  5690. SynchronizeParams.Params = Params;
  5691. SynchronizeParams.Checklist = Checklist;
  5692. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5693. SynchronizeParams.StartTime = &StartTime;
  5694. SynchronizeParams.OnProcessedItem = NULL;
  5695. Result = Checklist->Count;
  5696. if (Checklist->Count > 0)
  5697. {
  5698. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5699. {
  5700. if (!DoSynchronizeChecklistDialog(
  5701. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5702. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  5703. {
  5704. Result = -1;
  5705. }
  5706. }
  5707. else
  5708. {
  5709. FullSynchronize(SynchronizeParams, NULL, NULL);
  5710. }
  5711. }
  5712. else
  5713. {
  5714. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5715. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5716. }
  5717. }
  5718. __finally
  5719. {
  5720. delete Checklist;
  5721. }
  5722. OperationComplete(StartTime);
  5723. }
  5724. else
  5725. {
  5726. Result = -1;
  5727. }
  5728. return Result;
  5729. }
  5730. //---------------------------------------------------------------------------
  5731. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5732. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5733. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5734. {
  5735. if (Collect)
  5736. {
  5737. DebugAssert(FSynchronizeProgressForm != NULL);
  5738. if (!FSynchronizeProgressForm->Started)
  5739. {
  5740. FSynchronizeProgressForm->Start();
  5741. }
  5742. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5743. SetTaskbarListProgressValue(CompareProgress);
  5744. }
  5745. else
  5746. {
  5747. DebugAssert(FSynchronizeProgressForm == NULL);
  5748. DebugAssert(FProgressForm != NULL);
  5749. }
  5750. }
  5751. //---------------------------------------------------------------------------
  5752. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5753. {
  5754. UnicodeString FullFileName = AbsolutePath(Terminal->CurrentDirectory, FileName);
  5755. TRemoteFile * File = NULL;
  5756. Terminal->ReadFile(FullFileName, File);
  5757. if (File != NULL)
  5758. {
  5759. std::unique_ptr<TRemoteFile> FileOwner(File);
  5760. TAutoFlag Flag(FStandaloneEditing);
  5761. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5762. Application->ShowMainForm = false;
  5763. Application->Run();
  5764. }
  5765. }
  5766. //---------------------------------------------------------------------------
  5767. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory(TOperationSide)
  5768. {
  5769. DebugFail();
  5770. }
  5771. //---------------------------------------------------------------------------
  5772. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5773. {
  5774. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5775. SessionData->Assign(ManagedSession->SessionData);
  5776. UpdateSessionData(SessionData.get());
  5777. DebugAssert(SessionData->IsLocalBrowser == IsLocalBrowserMode());
  5778. if (IsActiveTerminal(ManagedSession))
  5779. {
  5780. ManagedSession->UpdateSessionCredentials(SessionData.get());
  5781. }
  5782. if (!ManagedSession->SessionData->HasSessionName())
  5783. {
  5784. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5785. // once IsWorkspace is cleared
  5786. SessionData->Name = UnicodeString();
  5787. }
  5788. // Most uses of this method ends with saving the session, and we do not want to store the IsWorkspace flag.
  5789. // Particularly, with SaveHiddenDuplicateSession, it won't work as TSessionData::ParseUrl explicitly ignored workspace sessions.
  5790. SessionData->IsWorkspace = false;
  5791. return SessionData.release();
  5792. }
  5793. //---------------------------------------------------------------------------
  5794. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5795. {
  5796. TSessionData * SessionData = CloneCurrentSessionData();
  5797. try
  5798. {
  5799. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5800. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5801. }
  5802. __finally
  5803. {
  5804. delete SessionData;
  5805. }
  5806. }
  5807. //---------------------------------------------------------------------------
  5808. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5809. {
  5810. TTerminalManager * Manager = TTerminalManager::Instance();
  5811. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5812. if (DebugAlwaysTrue(ManagedSession != NULL))
  5813. {
  5814. // Update ManagedSession->StateData
  5815. UpdateSession(ManagedSession);
  5816. }
  5817. Manager->SaveWorkspace(DataList.get());
  5818. return DataList.release();
  5819. }
  5820. //---------------------------------------------------------------------------
  5821. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5822. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5823. {
  5824. WinConfiguration->LastWorkspace = Name;
  5825. if (!SavePasswords)
  5826. {
  5827. for (int Index = 0; Index < DataList->Count; Index++)
  5828. {
  5829. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5830. if (SessionData->Link.IsEmpty())
  5831. {
  5832. SessionData->ClearSessionPasswords();
  5833. }
  5834. }
  5835. }
  5836. StoredSessions->NewWorkspace(Name, DataList);
  5837. // modified only
  5838. StoredSessions->Save(false, Explicit);
  5839. }
  5840. //---------------------------------------------------------------------------
  5841. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5842. {
  5843. return
  5844. DefaultStr(WinConfiguration->LastWorkspace,
  5845. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5846. }
  5847. //---------------------------------------------------------------------------
  5848. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5849. {
  5850. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5851. bool AnyNonStoredSessionWithPassword = false;
  5852. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5853. bool AllNonStoredSessionsAnonymous = true;
  5854. TTerminalManager * Manager = TTerminalManager::Instance();
  5855. for (int Index = 0; Index < DataList->Count; Index++)
  5856. {
  5857. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5858. if (SessionData->Link.IsEmpty())
  5859. {
  5860. if (SessionData->HasAnySessionPassword())
  5861. {
  5862. AnyNonStoredSessionWithPassword = true;
  5863. if (!Manager->Sessions[Index]->SessionData->IsWorkspace)
  5864. {
  5865. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5866. }
  5867. }
  5868. if (!SameText(SessionData->UserName, AnonymousUserName))
  5869. {
  5870. AllNonStoredSessionsAnonymous = false;
  5871. }
  5872. }
  5873. }
  5874. bool SavePasswords;
  5875. bool * PSavePasswords;
  5876. bool NotRecommendedSavingPasswords =
  5877. !CustomWinConfiguration->UseMasterPassword &&
  5878. !AllNonStoredSessionsAnonymous;
  5879. if (Configuration->DisablePasswordStoring ||
  5880. !AnyNonStoredSessionWithPassword)
  5881. {
  5882. PSavePasswords = NULL;
  5883. SavePasswords = false;
  5884. }
  5885. else
  5886. {
  5887. PSavePasswords = &SavePasswords;
  5888. SavePasswords =
  5889. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5890. !NotRecommendedSavingPasswords;
  5891. }
  5892. UnicodeString Name = WorkspaceName();
  5893. bool CreateShortcut = false;
  5894. bool Result =
  5895. DoSaveWorkspaceDialog(
  5896. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5897. EnableAutoSave);
  5898. if (Result)
  5899. {
  5900. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5901. if (CreateShortcut)
  5902. {
  5903. TOperationVisualizer Visualizer;
  5904. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5905. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5906. }
  5907. if (EnableAutoSave)
  5908. {
  5909. WinConfiguration->AutoSaveWorkspace = true;
  5910. WinConfiguration->AutoWorkspace = Name;
  5911. if (PSavePasswords != NULL)
  5912. {
  5913. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5914. }
  5915. }
  5916. }
  5917. return Result;
  5918. }
  5919. //---------------------------------------------------------------------------
  5920. void __fastcall TCustomScpExplorerForm::UpdateSession(TManagedTerminal * Session)
  5921. {
  5922. DebugAssert(IsLocalBrowserMode() == Session->LocalBrowser);
  5923. DebugAssert(Session == ManagedSession);
  5924. if (!IsLocalBrowserMode())
  5925. {
  5926. SAFE_DESTROY(Session->RemoteExplorerState);
  5927. if (WinConfiguration->PreservePanelState)
  5928. {
  5929. Session->RemoteExplorerState = RemoteDirView->SaveState();
  5930. }
  5931. }
  5932. UpdateSessionData(Session->StateData);
  5933. }
  5934. //---------------------------------------------------------------------------
  5935. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5936. {
  5937. // Keep in sync with TSessionData::CopyStateData
  5938. DebugAssert(Data != NULL);
  5939. // This is inconsistent with how color (for example) is handled.
  5940. Data->Name = ManagedSession->SessionData->Name;
  5941. if (!IsLocalBrowserMode())
  5942. {
  5943. // cannot use RemoteDirView->Path, because it is empty if connection
  5944. // was already closed
  5945. // also only peek, we may not be connected at all atm
  5946. // so make sure we do not try retrieving current directory from the server
  5947. // (particularly with FTP)
  5948. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5949. if (!ACurrentDirectory.IsEmpty())
  5950. {
  5951. Data->RemoteDirectory = ACurrentDirectory;
  5952. }
  5953. }
  5954. Data->Color = SessionColor;
  5955. }
  5956. //---------------------------------------------------------------------------
  5957. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5958. {
  5959. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5960. DebugAssert(Toolbar != NULL);
  5961. for (int i = 0; i < Toolbar->Items->Count; i++)
  5962. {
  5963. TTBXCustomDropDownItem * DropDownItem;
  5964. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5965. if (DropDownItem != NULL)
  5966. {
  5967. ToolbarItemResize(DropDownItem,
  5968. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5969. Toolbar->NonClientWidth);
  5970. break;
  5971. }
  5972. }
  5973. }
  5974. //---------------------------------------------------------------------------
  5975. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5976. {
  5977. Item->EditWidth = Width;
  5978. }
  5979. //---------------------------------------------------------------------------
  5980. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5981. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5982. {
  5983. for (int i = 0; i < Toolbar->Items->Count; i++)
  5984. {
  5985. TTBXCustomDropDownItem * DropDownItem;
  5986. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5987. if (DropDownItem != NULL)
  5988. {
  5989. ASize.x -= DropDownItem->EditWidth;
  5990. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5991. }
  5992. }
  5993. }
  5994. //---------------------------------------------------------------------------
  5995. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5996. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5997. {
  5998. if (WinConfiguration->DDWarnLackOfTempSpace)
  5999. {
  6000. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  6001. if (!ADrive.IsEmpty())
  6002. {
  6003. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  6004. DebugAssert(RequiredSpace >= 0);
  6005. __int64 RequiredWithReserve;
  6006. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  6007. if (FreeSpace < RequiredWithReserve)
  6008. {
  6009. unsigned int Result;
  6010. TMessageParams Params(mpNeverAskAgainCheck);
  6011. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  6012. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  6013. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  6014. if (Result == qaNeverAskAgain)
  6015. {
  6016. WinConfiguration->DDWarnLackOfTempSpace = false;
  6017. }
  6018. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  6019. }
  6020. }
  6021. }
  6022. }
  6023. //---------------------------------------------------------------------------
  6024. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  6025. {
  6026. DoOpenDirectoryDialog(odAddBookmark, Side);
  6027. }
  6028. //---------------------------------------------------------------------------
  6029. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  6030. {
  6031. // we should better use TCustomDirView::FCaseSensitive, but it is private
  6032. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  6033. try
  6034. {
  6035. TCustomDirView * DView = DirView(Side);
  6036. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  6037. {
  6038. VisitedDirectories->Add(DView->HistoryPath[Index]);
  6039. }
  6040. }
  6041. catch (...)
  6042. {
  6043. delete VisitedDirectories;
  6044. throw;
  6045. }
  6046. return VisitedDirectories;
  6047. }
  6048. //---------------------------------------------------------------------------
  6049. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  6050. TOpenDirectoryMode Mode, TOperationSide Side)
  6051. {
  6052. bool Continue = true;
  6053. if (Mode == odAddBookmark)
  6054. {
  6055. TMessageParams Params(mpNeverAskAgainCheck);
  6056. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  6057. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  6058. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  6059. unsigned int Answer =
  6060. MessageDialog(Message,
  6061. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  6062. if (Answer == qaNeverAskAgain)
  6063. {
  6064. Continue = true;
  6065. WinConfiguration->UseSharedBookmarks = true;
  6066. }
  6067. else if (Answer == qaOK)
  6068. {
  6069. Continue = true;
  6070. WinConfiguration->UseSharedBookmarks = false;
  6071. }
  6072. else
  6073. {
  6074. Continue = false;
  6075. }
  6076. }
  6077. if (Continue)
  6078. {
  6079. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  6080. try
  6081. {
  6082. UnicodeString Name = DirView(Side)->PathName;
  6083. bool Repeat;
  6084. do
  6085. {
  6086. Repeat = false;
  6087. TOperationSide BookmarkSide = IsSideLocalBrowser(Side) ? osLocal : osRemote;
  6088. if (::DoOpenDirectoryDialog(Mode, BookmarkSide, Name, VisitedDirectories, Terminal,
  6089. // do not allow switching to location profiles,
  6090. // if we are not connected
  6091. HasDirView[osLocal] && (Terminal != NULL)))
  6092. {
  6093. TWindowLock Lock(this);
  6094. if (!TryOpenDirectory(Side, Name))
  6095. {
  6096. Repeat = true;
  6097. Mode = odBrowse;
  6098. }
  6099. }
  6100. }
  6101. while (Repeat);
  6102. }
  6103. __finally
  6104. {
  6105. delete VisitedDirectories;
  6106. }
  6107. }
  6108. }
  6109. //---------------------------------------------------------------------------
  6110. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6111. {
  6112. DebugAssert(!Terminal->CommandSessionOpened);
  6113. return TTerminalManager::Instance()->ConnectTerminal(Terminal->CommandSession);
  6114. }
  6115. //---------------------------------------------------------------------------
  6116. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6117. {
  6118. bool Result = Terminal->IsCapable[Capability] ||
  6119. Terminal->CommandSessionOpened;
  6120. if (!Result)
  6121. {
  6122. DebugAssert(Terminal->IsCapable[fcSecondaryShell]);
  6123. if (!GUIConfiguration->ConfirmCommandSession)
  6124. {
  6125. Result = true;
  6126. }
  6127. else
  6128. {
  6129. TMessageParams Params(mpNeverAskAgainCheck);
  6130. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6131. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6132. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6133. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6134. if (Answer == qaNeverAskAgain)
  6135. {
  6136. GUIConfiguration->ConfirmCommandSession = false;
  6137. Result = true;
  6138. }
  6139. else if (Answer == qaOK)
  6140. {
  6141. Result = true;
  6142. }
  6143. }
  6144. if (Result)
  6145. {
  6146. Result = CommandSessionFallback();
  6147. }
  6148. }
  6149. return Result;
  6150. }
  6151. //---------------------------------------------------------------------------
  6152. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6153. {
  6154. if (EnsureCommandSessionFallback(fcAnyCommand))
  6155. {
  6156. DoConsoleDialog(Terminal, Command);
  6157. }
  6158. }
  6159. //---------------------------------------------------------------------------
  6160. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6161. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6162. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6163. {
  6164. if (!IsSideLocalBrowser(osRemote) &&
  6165. IsFileControl(DropSourceControl, osRemote) &&
  6166. (FDDExtMapFile != NULL))
  6167. {
  6168. Accept = true;
  6169. }
  6170. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6171. DebugAssert(FDDTargetControl != NULL);
  6172. }
  6173. //---------------------------------------------------------------------------
  6174. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6175. _di_IDataObject DataObj, int KeyState,
  6176. const TPoint & Point, int & Effect, bool & Accept)
  6177. {
  6178. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6179. }
  6180. //---------------------------------------------------------------------------
  6181. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6182. _di_IDataObject DataObj, int KeyState,
  6183. const TPoint & Point, int & Effect, bool & Accept)
  6184. {
  6185. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6186. }
  6187. //---------------------------------------------------------------------------
  6188. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6189. TObject * Sender)
  6190. {
  6191. DebugUsedParam(Sender);
  6192. DebugAssert(FDDTargetControl == Sender);
  6193. FDDTargetControl = NULL;
  6194. }
  6195. //---------------------------------------------------------------------------
  6196. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  6197. {
  6198. FileControlDDDragLeave(SessionsPageControl);
  6199. }
  6200. //---------------------------------------------------------------------------
  6201. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  6202. {
  6203. FileControlDDDragLeave(QueueView3);
  6204. }
  6205. //---------------------------------------------------------------------------
  6206. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6207. {
  6208. DebugAssert(!IsSideLocalBrowser(Side));
  6209. DebugUsedParam(Side);
  6210. bool Edit = false;
  6211. TRemoteFile * File = NULL;
  6212. UnicodeString FileName;
  6213. UnicodeString PointTo;
  6214. bool SymbolicLink = true;
  6215. if (RemoteDirView->ItemFocused)
  6216. {
  6217. DebugAssert(RemoteDirView->ItemFocused->Data);
  6218. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6219. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6220. if (Edit)
  6221. {
  6222. FileName = File->FileName;
  6223. PointTo = File->LinkTo;
  6224. }
  6225. else
  6226. {
  6227. PointTo = File->FileName;
  6228. }
  6229. }
  6230. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6231. Terminal->IsCapable[fcHardLink]))
  6232. {
  6233. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6234. if (Edit)
  6235. {
  6236. DebugAssert(File->FileName == FileName);
  6237. int Params = dfNoRecursive;
  6238. Terminal->ExceptionOnFail = true;
  6239. try
  6240. {
  6241. Terminal->DeleteFile(L"", File, &Params);
  6242. }
  6243. __finally
  6244. {
  6245. Terminal->ExceptionOnFail = false;
  6246. }
  6247. }
  6248. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6249. }
  6250. }
  6251. //---------------------------------------------------------------------------
  6252. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6253. {
  6254. return
  6255. (IsSideLocalBrowser(Side) ||
  6256. (HasActiveTerminal() &&
  6257. Terminal->ResolvingSymlinks &&
  6258. Terminal->IsCapable[fcSymbolicLink]));
  6259. }
  6260. //---------------------------------------------------------------------------
  6261. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6262. {
  6263. return
  6264. !IsSideLocalBrowser(FCurrentSide) &&
  6265. (RemoteDirView->ItemFocused != NULL) &&
  6266. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6267. Terminal->SessionData->ResolveSymlinks;
  6268. }
  6269. //---------------------------------------------------------------------------
  6270. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6271. {
  6272. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6273. FForceExecution = true;
  6274. try
  6275. {
  6276. DirView(osCurrent)->ExecuteCurrentFile();
  6277. }
  6278. __finally
  6279. {
  6280. FForceExecution = false;
  6281. }
  6282. }
  6283. //---------------------------------------------------------------------------
  6284. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6285. {
  6286. TEditorData ExternalEditor;
  6287. ExternalEditor.Editor = edExternal;
  6288. bool Remember = false;
  6289. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6290. {
  6291. if (Remember)
  6292. {
  6293. TEditorList * EditorList = new TEditorList;
  6294. try
  6295. {
  6296. *EditorList = *WinConfiguration->EditorList;
  6297. bool Found = false;
  6298. int i = 0;
  6299. while (!Found && (i < EditorList->Count))
  6300. {
  6301. const TEditorPreferences * Editor = EditorList->Editors[i];
  6302. if ((Editor->Data->Editor == edExternal) &&
  6303. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6304. {
  6305. Found = true;
  6306. }
  6307. i++;
  6308. }
  6309. if (!Found)
  6310. {
  6311. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6312. WinConfiguration->EditorList = EditorList;
  6313. }
  6314. }
  6315. __finally
  6316. {
  6317. delete EditorList;
  6318. }
  6319. }
  6320. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6321. }
  6322. }
  6323. //---------------------------------------------------------------------------
  6324. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6325. {
  6326. FEditorManager->ProcessFiles(FileTerminalRemoved, ATerminal);
  6327. if (FFileFindTerminal == ATerminal)
  6328. {
  6329. FFileFindTerminal = NULL;
  6330. HideFileFindDialog();
  6331. }
  6332. if (FClipboardTerminal == ATerminal)
  6333. {
  6334. ClipboardClear(); // implies ClipboardStop
  6335. }
  6336. }
  6337. //---------------------------------------------------------------------------
  6338. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6339. {
  6340. DetachTerminal(Sender);
  6341. }
  6342. //---------------------------------------------------------------------------
  6343. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6344. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6345. {
  6346. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6347. DebugAssert(Terminal != NULL);
  6348. if (Data->Terminal == Terminal)
  6349. {
  6350. Data->Terminal = NULL;
  6351. }
  6352. }
  6353. //---------------------------------------------------------------------------
  6354. TManagedTerminal * TCustomScpExplorerForm::GetReplacementForLastSession()
  6355. {
  6356. return NULL;
  6357. }
  6358. //---------------------------------------------------------------------------
  6359. void __fastcall TCustomScpExplorerForm::LastTerminalClosed()
  6360. {
  6361. UpdateControls();
  6362. SessionColor = TColor(0);
  6363. UpdateRemotePathComboBox(false);
  6364. NeedSession(false);
  6365. }
  6366. //---------------------------------------------------------------------------
  6367. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6368. {
  6369. try
  6370. {
  6371. // Cache, as the login dialog can change its value.
  6372. // See also DoShow. Also see TTerminalManager::NewSession.
  6373. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6374. try
  6375. {
  6376. if (ShowLogin)
  6377. {
  6378. bool ReloadSessions = !Startup;
  6379. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this, true);
  6380. }
  6381. }
  6382. __finally
  6383. {
  6384. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6385. // (so there was no chance for the user to open any session yet)
  6386. if ((ShowLogin || !Startup) &&
  6387. !WinConfiguration->KeepOpenWhenNoSession &&
  6388. // this rules out the implicit Commander's local-local browser
  6389. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6390. {
  6391. TerminateApplication();
  6392. }
  6393. }
  6394. }
  6395. catch (EAbort &)
  6396. {
  6397. // swallow
  6398. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6399. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6400. }
  6401. }
  6402. //---------------------------------------------------------------------------
  6403. void __fastcall TCustomScpExplorerForm::SessionListChanged(bool ForceTruncationUpdate)
  6404. {
  6405. TTerminalManager * Manager = TTerminalManager::Instance();
  6406. DebugAssert(!Manager->Updating);
  6407. TAutoFlag UpdatingSessionTabsFlag(FUpdatingSessionTabs);
  6408. int ActiveSessionIndex = Manager->ActiveSessionIndex;
  6409. Configuration->Usage->SetMax(L"MaxOpenedSessions", Manager->Count);
  6410. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6411. try
  6412. {
  6413. int PrevTabsWidth = SessionsPageControl->TotalTabsWidth();
  6414. while (SessionsPageControl->PageCount > Manager->Count + 1)
  6415. {
  6416. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6417. }
  6418. for (int Index = 0; Index <= Manager->Count; Index++)
  6419. {
  6420. TThemeTabSheet * TabSheet;
  6421. if (Index >= SessionsPageControl->PageCount)
  6422. {
  6423. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6424. TabSheet->PageControl = SessionsPageControl;
  6425. }
  6426. else
  6427. {
  6428. TabSheet = SessionsPageControl->Pages[Index];
  6429. }
  6430. bool IsSessionTab = (Index < Manager->Count);
  6431. if (IsSessionTab)
  6432. {
  6433. TTerminal * Terminal = Manager->Sessions[Index];
  6434. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6435. TabSheet->CaptionTruncation = tttEllipsis;
  6436. UpdateSessionTab(TabSheet);
  6437. }
  6438. else
  6439. {
  6440. TabSheet->Tag = 0; // not really needed
  6441. TabSheet->Shadowed = false;
  6442. TabSheet->Button = SupportsLocalBrowser() ? ttbDropDown : ttbNone;
  6443. TabSheet->CaptionTruncation = tttNoText;
  6444. // 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)
  6445. UpdateNewTabTab();
  6446. }
  6447. }
  6448. // Prevent flicker on the right side where scrolling buttons would be, unless really needed
  6449. if (ForceTruncationUpdate || (SessionsPageControl->TotalTabsWidth() != PrevTabsWidth))
  6450. {
  6451. SessionsPageControl->UpdateTabsCaptionTruncation();
  6452. }
  6453. }
  6454. __finally
  6455. {
  6456. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6457. }
  6458. SessionsPageControl->ActivePageIndex = ActiveSessionIndex;
  6459. }
  6460. //---------------------------------------------------------------------------
  6461. UnicodeString TCustomScpExplorerForm::GetNewTabTabCaption()
  6462. {
  6463. return StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewTabAction->Caption));
  6464. }
  6465. //---------------------------------------------------------------------------
  6466. void __fastcall TCustomScpExplorerForm::UpdateNewTabTab()
  6467. {
  6468. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6469. UnicodeString TabCaption;
  6470. if (WinConfiguration->SelectiveToolbarText)
  6471. {
  6472. TabCaption = GetNewTabTabCaption();
  6473. }
  6474. TabSheet->BaseCaption = TabCaption;
  6475. TabSheet->ImageIndex = GetNewTabTabImageIndex(osCurrent);
  6476. }
  6477. //---------------------------------------------------------------------------
  6478. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabSession(TTabSheet * TabSheet)
  6479. {
  6480. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6481. }
  6482. //---------------------------------------------------------------------------
  6483. UnicodeString TCustomScpExplorerForm::GetLocalBrowserSessionTitle(TManagedTerminal *)
  6484. {
  6485. DebugFail();
  6486. return UnicodeString();
  6487. }
  6488. //---------------------------------------------------------------------------
  6489. void TCustomScpExplorerForm::UpdateSessionTab(TThemeTabSheet * TabSheet)
  6490. {
  6491. if (DebugAlwaysTrue(TabSheet != NULL))
  6492. {
  6493. TManagedTerminal * ASession = GetSessionTabSession(TabSheet);
  6494. if (DebugAlwaysTrue(ASession != NULL))
  6495. {
  6496. TColor Color = (ASession == Terminal) ? FSessionColor : ASession->StateData->Color;
  6497. if (ASession->LocalBrowser)
  6498. {
  6499. TabSheet->ImageIndex = FLocalBrowserTabImageIndex;
  6500. }
  6501. else
  6502. {
  6503. TabSheet->ImageIndex = AddSessionColor(Color);
  6504. }
  6505. TTerminalManager * Manager = TTerminalManager::Instance();
  6506. TabSheet->Shadowed = !ASession->Active && !ASession->LocalBrowser;
  6507. TabSheet->Button = CanCloseSession(ASession) ? ttbClose : ttbNone;
  6508. TabSheet->BaseCaption = Manager->GetSessionTitle(ASession, true);
  6509. }
  6510. }
  6511. }
  6512. //---------------------------------------------------------------------------
  6513. bool TCustomScpExplorerForm::CanCloseSession(TManagedTerminal * Session)
  6514. {
  6515. return !Session->LocalBrowser || (TTerminalManager::Instance()->Count > 1);
  6516. }
  6517. //---------------------------------------------------------------------------
  6518. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6519. {
  6520. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6521. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->ActivePage);
  6522. bool Result = (Session != NULL);
  6523. if (Result)
  6524. {
  6525. TTerminalManager::Instance()->ActiveSession = Session;
  6526. }
  6527. else
  6528. {
  6529. try
  6530. {
  6531. NewTab();
  6532. }
  6533. __finally
  6534. {
  6535. SessionListChanged();
  6536. }
  6537. FSessionsPageControlNewTabTime = Now();
  6538. }
  6539. return Result;
  6540. }
  6541. //---------------------------------------------------------------------------
  6542. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6543. {
  6544. SessionTabSwitched();
  6545. }
  6546. //---------------------------------------------------------------------------
  6547. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6548. {
  6549. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6550. DebugAssert(TransferList != NULL);
  6551. UnicodeString Name;
  6552. if (TransferList->ItemIndex <= 0)
  6553. {
  6554. Name = L"";
  6555. }
  6556. else
  6557. {
  6558. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6559. }
  6560. if (FCopyParamAutoSelected.IsEmpty())
  6561. {
  6562. // if previous preset was not autoselected, make new preset the "default"
  6563. FCopyParamDefault = Name;
  6564. }
  6565. GUIConfiguration->CopyParamCurrent = Name;
  6566. }
  6567. //---------------------------------------------------------------------------
  6568. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6569. {
  6570. // sanity check
  6571. bool ExistingPreset =
  6572. (FTransferListHoverIndex >= 0) &&
  6573. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6574. DebugAssert(ExistingPreset);
  6575. if (ExistingPreset)
  6576. {
  6577. HDC DC = GetDC(0);
  6578. TCanvas * Canvas = new TCanvas();
  6579. try
  6580. {
  6581. Canvas->Handle = DC;
  6582. Canvas->Font = GetToolbarFont(this);
  6583. UnicodeString Name;
  6584. if (FTransferListHoverIndex == 0)
  6585. {
  6586. Name = L"";
  6587. }
  6588. else
  6589. {
  6590. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6591. }
  6592. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6593. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6594. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6595. static_cast<TObject*>(GetComponent(fcTransferList)));
  6596. UnicodeString InfoStr =
  6597. GUIConfiguration->CopyParamPreset[Name].
  6598. GetInfoStr(L"; ",
  6599. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  6600. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6601. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6602. {
  6603. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6604. {
  6605. InfoStr.SetLength(InfoStr.Length() - 1);
  6606. }
  6607. InfoStr += Ellipsis;
  6608. }
  6609. // UpdateCaption does not cause invalidation of whole submenu, while
  6610. // setting Caption property does.
  6611. // also it probably does not resize the label, even if necessary
  6612. // (we do not want that anyway)
  6613. TransferLabel->UpdateCaption(InfoStr);
  6614. }
  6615. __finally
  6616. {
  6617. Canvas->Handle = NULL;
  6618. ReleaseDC(0, DC);
  6619. delete Canvas;
  6620. }
  6621. }
  6622. }
  6623. //---------------------------------------------------------------------------
  6624. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6625. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6626. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6627. {
  6628. if (FTransferListHoverIndex != AHoverIndex)
  6629. {
  6630. FTransferListHoverIndex = AHoverIndex;
  6631. UpdateTransferLabel();
  6632. }
  6633. }
  6634. //---------------------------------------------------------------------------
  6635. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6636. {
  6637. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6638. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6639. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6640. {
  6641. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6642. {
  6643. if (!NonVisualDataModule->Busy)
  6644. {
  6645. OpenDirectory(GetSide(osCurrent));
  6646. }
  6647. Message.Result = 1;
  6648. }
  6649. else
  6650. {
  6651. TForm::Dispatch(&Message);
  6652. }
  6653. }
  6654. else
  6655. {
  6656. TForm::Dispatch(&Message);
  6657. }
  6658. }
  6659. //---------------------------------------------------------------------------
  6660. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6661. {
  6662. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6663. if ((FIgnoreNextDialogChar != 0) &&
  6664. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6665. {
  6666. Message.Result = 1;
  6667. }
  6668. else
  6669. {
  6670. TForm::Dispatch(&Message);
  6671. }
  6672. FIgnoreNextDialogChar = 0;
  6673. }
  6674. //---------------------------------------------------------------------------
  6675. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6676. {
  6677. // The four low-order bits are used internally by Windows
  6678. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6679. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6680. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6681. // 61730 - restore thru double click - undocumented
  6682. // 61490 - maximize thru double click - undocumented
  6683. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6684. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6685. {
  6686. SysResizing(Cmd);
  6687. }
  6688. TForm::Dispatch(&Message);
  6689. }
  6690. //---------------------------------------------------------------------------
  6691. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6692. {
  6693. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6694. // Also there no point returning TRUE as we are not able to
  6695. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6696. // Hence the process termination might be safer :)
  6697. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6698. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6699. {
  6700. Message.Result = FALSE;
  6701. }
  6702. else
  6703. {
  6704. Message.Result = TRUE;
  6705. }
  6706. // Do not call default handling as that triggers OnCloseQuery,
  6707. // where our implementation will popup confirmation dialogs, what we do not want,
  6708. // as per Vista guidelines:
  6709. // https://docs.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6710. }
  6711. //---------------------------------------------------------------------------
  6712. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6713. {
  6714. if (Message.EndSession && IsApplicationMinimized())
  6715. {
  6716. // WORKAROUND
  6717. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6718. // when it receives WM_ENDSESSION,
  6719. // but that sometimes (particularly when application is minimized) cause crashes.
  6720. // Crash popups message that blocks log off.
  6721. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6722. // so we call ExitProcess() immediately, not even trying to cleanup.
  6723. // It still causes beep, so there's likely some popup, but it does not block
  6724. // log off.
  6725. ExitProcess(0);
  6726. }
  6727. TForm::Dispatch(&Message);
  6728. }
  6729. #ifdef _DEBUG
  6730. //---------------------------------------------------------------------------
  6731. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6732. {
  6733. TForm::Dispatch(&Message);
  6734. }
  6735. #endif
  6736. //---------------------------------------------------------------------------
  6737. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6738. {
  6739. }
  6740. //---------------------------------------------------------------------------
  6741. void __fastcall TCustomScpExplorerForm::DoShow()
  6742. {
  6743. // Called from TCustomScpExplorerForm::CMShowingChanged
  6744. // only now are the controls resized finally, so the size constraints
  6745. // will not conflict with possibly very small window size
  6746. RestoreFormParams();
  6747. // This indicates that the form was rescaled after parameters were loaded and
  6748. // as most probably the parameters were actually saved with the current form scaling
  6749. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6750. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6751. // and no rounding problems will occur.
  6752. // See also RestoreForm.
  6753. // (we should be safe to call RestoreParams unconditionally)
  6754. if (PixelsPerInch != Screen->PixelsPerInch)
  6755. {
  6756. RestoreParams();
  6757. }
  6758. FixControlsPlacement();
  6759. if (Position == poDefaultPosOnly)
  6760. {
  6761. CutFormToDesktop(this);
  6762. }
  6763. TForm::DoShow();
  6764. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6765. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6766. // This was previously in NeedSession, where other related code is.
  6767. // But that is called from CMShowingChanged, when the window is already visible and the local-local window state
  6768. // unpleasanly shows briefly.
  6769. if ((ManagedSession == NULL) &&
  6770. !WinConfiguration->ShowLoginWhenNoSession &&
  6771. WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  6772. // This detects if workspace was saved the last time the main widow was closed
  6773. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  6774. {
  6775. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false);
  6776. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  6777. }
  6778. // Before main window displays
  6779. if (ManagedSession == NULL)
  6780. {
  6781. StartingWithoutSession();
  6782. }
  6783. UpdatePixelsPerInchMainWindowCounter();
  6784. FShowing = true;
  6785. }
  6786. //---------------------------------------------------------------------------
  6787. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6788. {
  6789. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6790. }
  6791. //---------------------------------------------------------------------------
  6792. void __fastcall TCustomScpExplorerForm::StartingWithoutSession()
  6793. {
  6794. SessionListChanged();
  6795. InitStatusBar();
  6796. UpdateControls();
  6797. }
  6798. //---------------------------------------------------------------------------
  6799. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6800. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6801. TNotifyEvent OnBalloonClick, TObject * UserData)
  6802. {
  6803. bool Do;
  6804. UnicodeString Message;
  6805. if (E == NULL)
  6806. {
  6807. Message = Str;
  6808. Do = true;
  6809. }
  6810. else
  6811. {
  6812. Do = ExceptionMessage(E, Message);
  6813. }
  6814. if (Do && WinConfiguration->BalloonNotifications)
  6815. {
  6816. UnicodeString Title;
  6817. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6818. {
  6819. Message = TrimLeft(Message);
  6820. }
  6821. else
  6822. {
  6823. Message = UnformatMessage(Message);
  6824. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6825. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6826. Title = LoadResourceString(Captions[Type]);
  6827. if (Terminal != NULL)
  6828. {
  6829. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  6830. Title = TTerminalManager::Instance()->GetSessionTitle(Session, true) + TitleSeparator + Title;
  6831. }
  6832. }
  6833. if (Seconds == 0)
  6834. {
  6835. Seconds = WinConfiguration->NotificationsTimeout;
  6836. }
  6837. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6838. }
  6839. }
  6840. //---------------------------------------------------------------------------
  6841. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6842. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6843. UnicodeString HelpKeyword, const TMessageParams * Params,
  6844. TTerminal * Terminal)
  6845. {
  6846. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6847. (FOnFeedSynchronizeError != NULL)) &&
  6848. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6849. {
  6850. if (FOnFeedSynchronizeError != NULL)
  6851. {
  6852. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6853. }
  6854. else
  6855. {
  6856. DebugAssert(FErrorList != NULL);
  6857. TStringList * MoreMessagesCopy = NULL;
  6858. if (MoreMessages)
  6859. {
  6860. MoreMessagesCopy = new TStringList();
  6861. MoreMessagesCopy->Assign(MoreMessages);
  6862. }
  6863. FErrorList->AddObject(Message, MoreMessagesCopy);
  6864. }
  6865. PopupTrayBalloon(Terminal, Message, Type);
  6866. return ContinueAnswer(Answers);
  6867. }
  6868. else
  6869. {
  6870. bool UseBalloon = FTrayIcon->Visible;
  6871. if (UseBalloon)
  6872. {
  6873. PopupTrayBalloon(Terminal, Message, Type);
  6874. }
  6875. unsigned int Result;
  6876. try
  6877. {
  6878. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6879. }
  6880. __finally
  6881. {
  6882. // cancel only balloon we popped up, otherwise we may cancel notification
  6883. // balloon that was there before the message dialog
  6884. // (such as "dd confirmation opt in balloon)
  6885. if (UseBalloon)
  6886. {
  6887. FTrayIcon->CancelBalloon();
  6888. }
  6889. }
  6890. return Result;
  6891. }
  6892. }
  6893. //---------------------------------------------------------------------------
  6894. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6895. TTerminal * Terminal, Exception * E)
  6896. {
  6897. if (FTrayIcon->Visible)
  6898. {
  6899. PopupTrayBalloon(Terminal, L"", qtError, E);
  6900. }
  6901. // particularly prevent opening new session from jump list,
  6902. // while exception is shown
  6903. NonVisualDataModule->StartBusy();
  6904. try
  6905. {
  6906. ShowExtendedExceptionEx(Terminal, E);
  6907. }
  6908. __finally
  6909. {
  6910. NonVisualDataModule->EndBusy();
  6911. FTrayIcon->CancelBalloon();
  6912. }
  6913. }
  6914. //---------------------------------------------------------------------------
  6915. void __fastcall TCustomScpExplorerForm::SessionReady()
  6916. {
  6917. InitStatusBar();
  6918. // cannot rely on active page being page for active terminal,
  6919. // as it can happen that active page is the "new session" page
  6920. // (e.g. when reconnecting active terminal, while login dialog
  6921. // invoked from "new session" page is modal)
  6922. int ActiveSessionIndex = TTerminalManager::Instance()->ActiveSessionIndex;
  6923. UpdateSessionTab(SessionsPageControl->Pages[ActiveSessionIndex]);
  6924. }
  6925. //---------------------------------------------------------------------------
  6926. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6927. TTerminal * Terminal, Exception * E)
  6928. {
  6929. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6930. if (!Terminal->Active)
  6931. {
  6932. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6933. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6934. {
  6935. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6936. }
  6937. }
  6938. }
  6939. //---------------------------------------------------------------------------
  6940. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6941. UnicodeString Message, TQueryType Type,
  6942. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6943. {
  6944. if ((E == NULL) ||
  6945. ExceptionMessage(E, Message))
  6946. {
  6947. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6948. if (Important)
  6949. {
  6950. Seconds *= 5;
  6951. }
  6952. UnicodeString NoteMessage(UnformatMessage(Message));
  6953. if (Terminal != NULL)
  6954. {
  6955. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  6956. NoteMessage = FORMAT(L"%s: %s",
  6957. (TTerminalManager::Instance()->GetSessionTitle(Session, true), NoteMessage));
  6958. }
  6959. if (WinConfiguration->BalloonNotifications)
  6960. {
  6961. AddNote(NoteMessage);
  6962. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6963. }
  6964. else
  6965. {
  6966. FlashOnBackground();
  6967. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6968. }
  6969. }
  6970. }
  6971. //---------------------------------------------------------------------------
  6972. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6973. {
  6974. RestoreApp();
  6975. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6976. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6977. !NonVisualDataModule->Busy)
  6978. {
  6979. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  6980. TTerminalManager::Instance()->ActiveSession = Terminal;
  6981. if (!ComponentVisible[fcQueueView])
  6982. {
  6983. ToggleQueueVisibility();
  6984. GoToQueue();
  6985. }
  6986. }
  6987. }
  6988. //---------------------------------------------------------------------------
  6989. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  6990. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6991. {
  6992. UnicodeString Message;
  6993. TNotifyEvent OnClick = NULL;
  6994. TObject * UserData = NULL;
  6995. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6996. switch (Event)
  6997. {
  6998. case qeEmptyButMonitored:
  6999. if ((ATerminal != Terminal) || QueueInvisible)
  7000. {
  7001. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  7002. OnClick = QueueEmptyNoteClicked;
  7003. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  7004. TerminalNoteData->Terminal = ATerminal;
  7005. UserData = TerminalNoteData;
  7006. }
  7007. break;
  7008. case qeEmpty:
  7009. OperationComplete(ATerminal->QueueOperationStart);
  7010. break;
  7011. case qePendingUserAction:
  7012. if ((ATerminal != Terminal) ||
  7013. (QueueInvisible && !IsQueueAutoPopup()))
  7014. {
  7015. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  7016. }
  7017. break;
  7018. default:
  7019. DebugFail();
  7020. }
  7021. if (!Message.IsEmpty())
  7022. {
  7023. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  7024. }
  7025. }
  7026. //---------------------------------------------------------------------------
  7027. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  7028. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  7029. {
  7030. if (FDDExtMapFile != NULL)
  7031. {
  7032. CloseHandle(FDDExtMapFile);
  7033. FDDExtMapFile = NULL;
  7034. }
  7035. if (WinConfiguration->DDFakeFile)
  7036. {
  7037. DDFakeFileInitDrag(FileList, Created);
  7038. }
  7039. }
  7040. //---------------------------------------------------------------------------
  7041. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  7042. {
  7043. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  7044. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  7045. {
  7046. FFakeFileDropTarget = FileName;
  7047. }
  7048. }
  7049. //---------------------------------------------------------------------------
  7050. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  7051. {
  7052. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  7053. if (!ForceDirectories(ApiPath(Result)))
  7054. {
  7055. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  7056. }
  7057. FileSetAttr(ApiPath(Result), faHidden);
  7058. return Result;
  7059. }
  7060. //---------------------------------------------------------------------------
  7061. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  7062. bool & Created)
  7063. {
  7064. FFakeFileDropTarget = UnicodeString();
  7065. FDragFakeDirectory = CreateFakeTransferDirectory();
  7066. FileList->AddItem(NULL, FDragFakeDirectory);
  7067. Created = true;
  7068. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  7069. {
  7070. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  7071. }
  7072. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  7073. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  7074. {
  7075. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7076. TDragExtCommStruct* CommStruct;
  7077. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7078. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7079. DebugAssert(CommStruct != NULL);
  7080. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  7081. CommStruct->Dragging = true;
  7082. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  7083. LENOF(CommStruct->DropDest));
  7084. NULL_TERMINATE(CommStruct->DropDest);
  7085. UnmapViewOfFile(CommStruct);
  7086. }
  7087. }
  7088. //---------------------------------------------------------------------------
  7089. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  7090. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  7091. {
  7092. bool Result;
  7093. DebugAssert(!IsLocalBrowserMode());
  7094. if (Sender == RemoteDirView)
  7095. {
  7096. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  7097. }
  7098. else
  7099. {
  7100. DebugAssert(Sender == RemoteDriveView);
  7101. TStrings * FileList = RemoteDriveView->DragFileList();
  7102. try
  7103. {
  7104. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  7105. }
  7106. __finally
  7107. {
  7108. delete FileList;
  7109. }
  7110. }
  7111. if (FDDTargetControl == RemoteDriveView)
  7112. {
  7113. RemoteDriveView->UpdateDropTarget();
  7114. }
  7115. // foRemoteMove happens when file/dir is dragged within the remote tree view
  7116. // or from tree view to dir view.
  7117. // foMove happens when file/dir is dragged from remote tree view outside of
  7118. // application via dragex
  7119. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  7120. (DropSourceControl == RemoteDriveView))
  7121. {
  7122. RemoteDriveView->UpdateDropSource();
  7123. }
  7124. return Result;
  7125. }
  7126. //---------------------------------------------------------------------------
  7127. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  7128. {
  7129. DebugAssert(!IsLocalBrowserMode());
  7130. // This also handles drops of remote files to queue.
  7131. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  7132. SAFE_DESTROY(FDDFileList);
  7133. if (!FFakeFileDropTarget.IsEmpty())
  7134. {
  7135. RemoveDir(ApiPath(FFakeFileDropTarget));
  7136. }
  7137. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7138. {
  7139. try
  7140. {
  7141. TDragResult DDResult = (Sender == RemoteDirView) ?
  7142. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7143. // Focus is moved to the target application,
  7144. // but as we are going to present the UI, we need to steal the focus back.
  7145. // This most likely won't work though (windows does not allow application
  7146. // to steal focus most of the time)
  7147. Application->BringToFront();
  7148. // On older version of Windows we never got drMove here, see also comment below.
  7149. // On Windows 10, we get the "move".
  7150. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7151. {
  7152. UnicodeString TargetDirectory;
  7153. TFileOperation Operation;
  7154. // drInvalid may mean drMove, see comment below
  7155. switch (DDResult)
  7156. {
  7157. case drCopy:
  7158. Operation = foCopy;
  7159. break;
  7160. case drMove:
  7161. Operation = foMove;
  7162. break;
  7163. default:
  7164. DebugFail();
  7165. case drInvalid:
  7166. // prefer "copy" for safety
  7167. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7168. break;
  7169. }
  7170. TTransferOperationParam Param;
  7171. UnicodeString CounterName;
  7172. bool ForceQueue;
  7173. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7174. {
  7175. // we get drInvalid both if d&d was intercepted by ddext,
  7176. // and when users drops on no-drop location.
  7177. // we tell the difference by existence of response from ddext,
  7178. // so we ignore absence of response in this case
  7179. if (DDResult != drInvalid)
  7180. {
  7181. // here we know that the extension is installed,
  7182. // as it is checked as soon as drag&drop starts from
  7183. // RemoteFileControlDDCreateDragFileList
  7184. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7185. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7186. {
  7187. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7188. }
  7189. else
  7190. {
  7191. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7192. }
  7193. }
  7194. }
  7195. else
  7196. {
  7197. // download using fake file
  7198. Param.Temp = false;
  7199. Param.DragDrop = true;
  7200. if (ForceQueue)
  7201. {
  7202. Param.Queue = asOn;
  7203. }
  7204. if (Sender == RemoteDirView)
  7205. {
  7206. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7207. }
  7208. if (RemoteFileControlFileOperation(Sender, Operation,
  7209. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7210. {
  7211. Configuration->Usage->Inc(CounterName);
  7212. }
  7213. }
  7214. }
  7215. }
  7216. __finally
  7217. {
  7218. CloseHandle(FDDExtMapFile);
  7219. FDDExtMapFile = NULL;
  7220. delete FDragFakeMonitors;
  7221. FDragFakeMonitors = NULL;
  7222. RemoveDir(ApiPath(FDragFakeDirectory));
  7223. FDragFakeDirectory = L"";
  7224. FFakeFileDropTarget = UnicodeString();
  7225. }
  7226. }
  7227. if (FDragDropSshTerminate.get() != NULL)
  7228. {
  7229. FDragDropSshTerminate->Rethrow();
  7230. }
  7231. }
  7232. //---------------------------------------------------------------------------
  7233. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7234. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7235. {
  7236. // Remember drop effect so we know (when user drops files), if we copy or move
  7237. FLastDropEffect = dwEffect;
  7238. }
  7239. //---------------------------------------------------------------------------
  7240. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7241. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7242. {
  7243. bool Result;
  7244. if (FDDTargetControl == QueueView3)
  7245. {
  7246. Directory = DefaultDownloadTargetDirectory();
  7247. Result = true;
  7248. CounterName = L"DownloadsDragDropQueue";
  7249. ForceQueue = true;
  7250. }
  7251. else if (!FFakeFileDropTarget.IsEmpty())
  7252. {
  7253. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7254. Result = true;
  7255. ForceQueue = false;
  7256. CounterName = L"DownloadsDragDropFakeFile";
  7257. }
  7258. else
  7259. {
  7260. ForceQueue = false;
  7261. Enabled = false;
  7262. try
  7263. {
  7264. int Timer = 0;
  7265. Result = false;
  7266. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7267. {
  7268. {
  7269. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7270. TDragExtCommStruct* CommStruct;
  7271. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7272. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7273. DebugAssert(CommStruct != NULL);
  7274. Result = !CommStruct->Dragging;
  7275. if (Result)
  7276. {
  7277. Directory = ExtractFilePath(CommStruct->DropDest);
  7278. CounterName = L"DownloadsDragDropExternalExt";
  7279. }
  7280. UnmapViewOfFile(CommStruct);
  7281. }
  7282. if (!Result)
  7283. {
  7284. Sleep(50);
  7285. Timer += 50;
  7286. Application->ProcessMessages();
  7287. }
  7288. }
  7289. }
  7290. __finally
  7291. {
  7292. Enabled = true;
  7293. }
  7294. }
  7295. return Result;
  7296. }
  7297. //---------------------------------------------------------------------------
  7298. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7299. const UnicodeString TempDir, int SecDelay)
  7300. {
  7301. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7302. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7303. if (FDelayedDeletionTimer == NULL)
  7304. {
  7305. DebugAssert(HandleAllocated());
  7306. FDelayedDeletionTimer = new TTimer(this);
  7307. FDelayedDeletionTimer->Interval = 10000;
  7308. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7309. }
  7310. else
  7311. {
  7312. FDelayedDeletionTimer->Enabled = true;
  7313. }
  7314. }
  7315. //---------------------------------------------------------------------------
  7316. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7317. {
  7318. DebugAssert(FDelayedDeletionList != NULL);
  7319. TDateTime N = Now();
  7320. TDateTime Alert;
  7321. UnicodeString Directory;
  7322. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7323. {
  7324. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7325. if ((N >= Alert) || (Sender == NULL))
  7326. {
  7327. Directory = FDelayedDeletionList->Strings[Index];
  7328. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7329. {
  7330. FDelayedDeletionList->Delete(Index);
  7331. }
  7332. }
  7333. }
  7334. if (FDelayedDeletionList->Count == 0)
  7335. {
  7336. FDelayedDeletionTimer->Enabled = false;
  7337. }
  7338. }
  7339. //---------------------------------------------------------------------------
  7340. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7341. {
  7342. DebugAssert(!IsLocalBrowserMode());
  7343. if (IsFileControl(FDDTargetControl, osRemote) ||
  7344. (FDDTargetControl == SessionsPageControl))
  7345. {
  7346. TManagedTerminal * TargetTerminal = NULL;
  7347. TFileOperation Operation = foNone;
  7348. if (FDDTargetControl == SessionsPageControl)
  7349. {
  7350. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7351. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7352. // do not allow dropping on local-local, disconnected and the "+" tab (see also SessionsDDProcessDropped)
  7353. TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  7354. if (IsActiveTerminal(TargetTerminal) && DebugAlwaysTrue(!TargetTerminal->LocalBrowser))
  7355. {
  7356. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7357. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7358. {
  7359. Operation = foRemoteMove;
  7360. }
  7361. else
  7362. {
  7363. Operation = foRemoteCopy;
  7364. }
  7365. }
  7366. }
  7367. else
  7368. {
  7369. if (FLastDropEffect == DROPEFFECT_MOVE)
  7370. {
  7371. Operation = foRemoteMove;
  7372. }
  7373. else if (FLastDropEffect == DROPEFFECT_COPY)
  7374. {
  7375. Operation = foRemoteCopy;
  7376. }
  7377. }
  7378. if (Operation != foNone)
  7379. {
  7380. RemoteFileControlFileOperation(DropSourceControl,
  7381. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7382. }
  7383. // abort drag&drop
  7384. Abort();
  7385. }
  7386. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7387. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7388. (FDDTargetControl != QueueView3))
  7389. {
  7390. DebugAssert(!FDragTempDir.IsEmpty());
  7391. TTransferType Type;
  7392. UnicodeString TempDir = FDragTempDir;
  7393. // We clear FDragTempDir before calling
  7394. // just in case it fail (raises exception)
  7395. FDragTempDir = L"";
  7396. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7397. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7398. // empty directory parameter means temp directory -> don't display it!
  7399. UnicodeString TargetDir = L"";
  7400. int Options = 0;
  7401. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7402. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7403. {
  7404. Abort();
  7405. }
  7406. // TargetDir is set when dropped on local file control
  7407. // (this was workaround for legacy dirview event handling, now it should be
  7408. // made prettier)
  7409. if (TargetDir.IsEmpty())
  7410. {
  7411. TargetDir = TempDir;
  7412. if (ForceDirectories(ApiPath(TargetDir)))
  7413. {
  7414. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7415. FPendingTempSpaceWarn = true;
  7416. try
  7417. {
  7418. FDragDropOperation = true;
  7419. int Params = cpTemporary |
  7420. (Type == ttMove ? cpDelete : 0);
  7421. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7422. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7423. }
  7424. __finally
  7425. {
  7426. FDragDropOperation = false;
  7427. FPendingTempSpaceWarn = false;
  7428. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7429. }
  7430. }
  7431. else
  7432. {
  7433. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7434. }
  7435. }
  7436. }
  7437. }
  7438. //---------------------------------------------------------------------------
  7439. void __fastcall TCustomScpExplorerForm::DDDownload(
  7440. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  7441. {
  7442. DebugAssert(!IsLocalBrowserMode());
  7443. TAutoBatch AutoBatch(this);
  7444. UpdateCopyParamCounters(*CopyParam);
  7445. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7446. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7447. {
  7448. RemoteDriveView->UpdateDropSource();
  7449. }
  7450. }
  7451. //---------------------------------------------------------------------------
  7452. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7453. {
  7454. public:
  7455. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7456. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7457. {
  7458. }
  7459. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7460. {
  7461. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7462. TDataObjectFilesEx::AllowData(FormatEtc);
  7463. }
  7464. };
  7465. //---------------------------------------------------------------------------
  7466. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7467. TObject * Sender, TDataObject *& DataObject)
  7468. {
  7469. if (FDDExtMapFile != NULL)
  7470. {
  7471. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7472. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7473. {
  7474. Abort();
  7475. }
  7476. if (FileList->Count > 0)
  7477. {
  7478. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7479. if (!FilesObject->IsValid(true, true))
  7480. {
  7481. FilesObject->_Release();
  7482. }
  7483. else
  7484. {
  7485. DataObject = FilesObject;
  7486. }
  7487. }
  7488. }
  7489. }
  7490. //---------------------------------------------------------------------------
  7491. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7492. {
  7493. DebugFail();
  7494. }
  7495. //---------------------------------------------------------------------------
  7496. void __fastcall TCustomScpExplorerForm::GoToTree()
  7497. {
  7498. ComponentVisible[fcRemoteTree] = true;
  7499. DriveView(osRemote)->SetFocus();
  7500. }
  7501. //---------------------------------------------------------------------------
  7502. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7503. TPanelExport Export)
  7504. {
  7505. TCustomDirView * DirView = this->DirView(Side);
  7506. std::unique_ptr<TStrings> ExportData(new TStringList());
  7507. switch (Export)
  7508. {
  7509. case pePath:
  7510. ExportData->Add(DirView->PathName);
  7511. break;
  7512. case peFileList:
  7513. case peFullFileList:
  7514. {
  7515. bool FullPath = (Export == peFullFileList);
  7516. DirView->CreateFileList(false, FullPath, ExportData.get());
  7517. for (int Index = 0; Index < ExportData->Count; Index++)
  7518. {
  7519. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7520. {
  7521. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7522. }
  7523. }
  7524. }
  7525. break;
  7526. default:
  7527. DebugFail();
  7528. }
  7529. return ExportData.release();
  7530. }
  7531. //---------------------------------------------------------------------------
  7532. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7533. TPanelExport Export, TPanelExportDestination Destination)
  7534. {
  7535. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7536. PanelExportStore(Side, Export, Destination, ExportData.get());
  7537. }
  7538. //---------------------------------------------------------------------------
  7539. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7540. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7541. TStrings * ExportData)
  7542. {
  7543. if (Destination == pedClipboard)
  7544. {
  7545. TInstantOperationVisualizer Visualizer;
  7546. CopyToClipboard(ExportData);
  7547. }
  7548. else
  7549. {
  7550. DebugFail();
  7551. }
  7552. }
  7553. //---------------------------------------------------------------------------
  7554. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7555. {
  7556. TCustomDirView * DirView = this->DirView(Side);
  7557. UnicodeString Mask = DirView->Mask;
  7558. if (DoFilterMaskDialog(DirView, Mask))
  7559. {
  7560. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7561. Configuration->Usage->Inc(L"Filters");
  7562. }
  7563. }
  7564. //---------------------------------------------------------------------------
  7565. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7566. {
  7567. return FQueueController->DefaultOperation();
  7568. }
  7569. //---------------------------------------------------------------------------
  7570. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7571. TQueueOperation Operation, void ** Param)
  7572. {
  7573. switch (Operation)
  7574. {
  7575. case qoPreferences:
  7576. return true;
  7577. case qoGoTo:
  7578. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7579. case qoOnceEmpty:
  7580. return IsAnythingQueued();
  7581. default:
  7582. return FQueueController->AllowOperation(Operation, Param);
  7583. }
  7584. }
  7585. //---------------------------------------------------------------------------
  7586. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7587. {
  7588. if (DebugAlwaysTrue(QueueView3->Visible))
  7589. {
  7590. QueueView3->SetFocus();
  7591. }
  7592. }
  7593. //---------------------------------------------------------------------------
  7594. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7595. TQueueOperation Operation, void * Param)
  7596. {
  7597. if (Operation == qoGoTo)
  7598. {
  7599. GoToQueue();
  7600. }
  7601. else if (Operation == qoPreferences)
  7602. {
  7603. PreferencesDialog(pmQueue);
  7604. }
  7605. else
  7606. {
  7607. FQueueController->ExecuteOperation(Operation, Param);
  7608. }
  7609. }
  7610. //---------------------------------------------------------------------------
  7611. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7612. {
  7613. return (Queue != NULL) && Queue->Enabled;
  7614. }
  7615. //---------------------------------------------------------------------------
  7616. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7617. {
  7618. DebugAssert(Queue != NULL);
  7619. if (Queue != NULL)
  7620. {
  7621. Queue->Enabled = !Queue->Enabled;
  7622. }
  7623. }
  7624. //---------------------------------------------------------------------------
  7625. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7626. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7627. {
  7628. FQueueActedItem = QueueView3->ItemFocused;
  7629. }
  7630. //---------------------------------------------------------------------------
  7631. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7632. {
  7633. return TopDock->Height + QueueSplitter->Height;
  7634. }
  7635. //---------------------------------------------------------------------------
  7636. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  7637. {
  7638. return
  7639. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  7640. (QueueDock->Visible ? QueueDock->Height : 0) +
  7641. (QueueLabel->Visible ? QueueLabel->Height : 0);
  7642. }
  7643. //---------------------------------------------------------------------------
  7644. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  7645. {
  7646. return ScaleByTextHeight(this, 48);
  7647. }
  7648. //---------------------------------------------------------------------------
  7649. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7650. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7651. {
  7652. // when queue is hidden by double-clicking splitter, stray attempt to
  7653. // resize the panel with strange value arrives, make sure it is ignored
  7654. if (ComponentVisible[fcQueueView])
  7655. {
  7656. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  7657. if (NewSize > MaxHeightLimit)
  7658. {
  7659. NewSize = MaxHeightLimit;
  7660. }
  7661. int MinHeightLimit =
  7662. GetStaticQueuePanelComponentsHeight() +
  7663. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  7664. GetMinQueueViewHeight();
  7665. if (NewSize < MinHeightLimit)
  7666. {
  7667. NewSize = MinHeightLimit;
  7668. }
  7669. }
  7670. else
  7671. {
  7672. Accept = false;
  7673. }
  7674. }
  7675. //---------------------------------------------------------------------------
  7676. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  7677. {
  7678. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  7679. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  7680. if (QueueViewHeight < GetMinQueueViewHeight())
  7681. {
  7682. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  7683. }
  7684. }
  7685. //---------------------------------------------------------------------------
  7686. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7687. TDragObject *& /*DragObject*/)
  7688. {
  7689. FQueueActedItem = QueueView3->ItemFocused;
  7690. }
  7691. //---------------------------------------------------------------------------
  7692. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7693. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7694. {
  7695. Accept = true;
  7696. if (Source == QueueView3)
  7697. {
  7698. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7699. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7700. if (Accept)
  7701. {
  7702. TQueueItemProxy * QueueItem;
  7703. TQueueItemProxy * DestQueueItem;
  7704. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7705. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7706. Accept = (QueueItem != DestQueueItem) &&
  7707. (QueueItem->Status == TQueueItem::qsPending) &&
  7708. (DestQueueItem->Status == TQueueItem::qsPending);
  7709. }
  7710. }
  7711. }
  7712. //---------------------------------------------------------------------------
  7713. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7714. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7715. {
  7716. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7717. {
  7718. TQueueItemProxy * QueueItem;
  7719. TQueueItemProxy * DestQueueItem;
  7720. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7721. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7722. QueueItem->Move(DestQueueItem);
  7723. }
  7724. }
  7725. //---------------------------------------------------------------------------
  7726. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7727. {
  7728. if ((QueueView3->ItemFocused == NULL) &&
  7729. (QueueView3->Items->Count > 0))
  7730. {
  7731. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7732. }
  7733. QueueLabelUpdateStatus();
  7734. }
  7735. //---------------------------------------------------------------------------
  7736. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7737. {
  7738. QueueLabelUpdateStatus();
  7739. }
  7740. //---------------------------------------------------------------------------
  7741. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7742. {
  7743. QueueLabel->UpdateStatus();
  7744. }
  7745. //---------------------------------------------------------------------------
  7746. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7747. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7748. {
  7749. if (Selected)
  7750. {
  7751. NonVisualDataModule->UpdateNonVisibleActions();
  7752. }
  7753. }
  7754. //---------------------------------------------------------------------------
  7755. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7756. {
  7757. TDragDropFilesEx * Result = NULL;
  7758. if (Sender == SessionsPageControl)
  7759. {
  7760. Result = FSessionsDragDropFilesEx;
  7761. }
  7762. else if (Sender == QueueView3)
  7763. {
  7764. Result = FQueueDragDropFilesEx;
  7765. }
  7766. else
  7767. {
  7768. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7769. if (DirView != NULL)
  7770. {
  7771. Result = DirView->DragDropFilesEx;
  7772. }
  7773. else
  7774. {
  7775. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7776. if (DriveView != NULL)
  7777. {
  7778. Result = DriveView->DragDropFilesEx;
  7779. }
  7780. }
  7781. }
  7782. DebugAssert(Result != NULL);
  7783. return Result;
  7784. }
  7785. //---------------------------------------------------------------------------
  7786. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7787. {
  7788. return (DView->SelCount == DView->FilesCount);
  7789. }
  7790. //---------------------------------------------------------------------------
  7791. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7792. {
  7793. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7794. }
  7795. //---------------------------------------------------------------------------
  7796. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7797. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7798. {
  7799. DebugAssert(!IsLocalBrowserMode());
  7800. TFileOperation Operation;
  7801. switch (Effect)
  7802. {
  7803. case DROPEFFECT_MOVE:
  7804. Operation = foMove;
  7805. break;
  7806. case DROPEFFECT_COPY:
  7807. // occures on WinXP (reported by user)
  7808. default:
  7809. Operation = foCopy;
  7810. break;
  7811. };
  7812. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7813. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7814. if (DragDropFilesEx->FileList->Count > 0)
  7815. {
  7816. TStrings * FileList = new TStringList();
  7817. try
  7818. {
  7819. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7820. {
  7821. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7822. }
  7823. FDragDropOperation = true;
  7824. TTransferOperationParam Param;
  7825. Param.TargetDirectory = TargetPath;
  7826. // upload, no temp dirs
  7827. Param.Temp = false;
  7828. Param.DragDrop = DragDrop;
  7829. Param.Options =
  7830. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7831. if (ForceQueue)
  7832. {
  7833. Param.Queue = asOn;
  7834. }
  7835. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7836. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7837. {
  7838. if (IsFileControl(DropSourceControl, osLocal))
  7839. {
  7840. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7841. }
  7842. else
  7843. {
  7844. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7845. }
  7846. }
  7847. }
  7848. __finally
  7849. {
  7850. FDragDropOperation = false;
  7851. delete FileList;
  7852. }
  7853. }
  7854. }
  7855. //---------------------------------------------------------------------------
  7856. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7857. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7858. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7859. {
  7860. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7861. }
  7862. //---------------------------------------------------------------------------
  7863. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7864. TObject * Sender, int grfKeyState, int & dwEffect)
  7865. {
  7866. DebugAssert(!IsLocalBrowserMode());
  7867. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7868. if ((dwEffect != DROPEFFECT_NONE) &&
  7869. IsFileControl(DropSourceControl, osRemote))
  7870. {
  7871. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7872. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7873. (RemoteDirView->DropTarget == NULL))
  7874. {
  7875. dwEffect = DROPEFFECT_NONE;
  7876. }
  7877. else
  7878. {
  7879. if (dwEffect == DROPEFFECT_COPY)
  7880. {
  7881. bool MoveCapable = Terminal->IsCapable[fcRemoteMove];
  7882. // currently we support copying always (at least via temporary directory);
  7883. // remove associated checks once this all proves stable and working
  7884. bool CopyCapable = true;
  7885. // if we do not support neither of operations, there's no discussion
  7886. if (!MoveCapable && !CopyCapable)
  7887. {
  7888. dwEffect = DROPEFFECT_NONE;
  7889. }
  7890. else
  7891. {
  7892. // The default effect inside remote panel is move,
  7893. // unless we do not support it, but support copy
  7894. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7895. {
  7896. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  7897. }
  7898. else
  7899. {
  7900. // with ctrl-down, we want copy unless it is not supported
  7901. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  7902. }
  7903. }
  7904. }
  7905. }
  7906. }
  7907. }
  7908. //---------------------------------------------------------------------------
  7909. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7910. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7911. {
  7912. DebugAssert(!IsLocalBrowserMode());
  7913. if (FDDTotalSize >= 0)
  7914. {
  7915. if (File->IsDirectory)
  7916. {
  7917. FDDTotalSize = -1;
  7918. }
  7919. else
  7920. {
  7921. FDDTotalSize += File->Size;
  7922. }
  7923. }
  7924. DebugAssert(!FDragTempDir.IsEmpty());
  7925. // TODO: this is quite ineffective
  7926. // TODO: what if invalid character replacement is disabled?
  7927. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7928. UnicodeString TransferFileName;
  7929. if (Sender == RemoteDriveView)
  7930. {
  7931. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7932. }
  7933. else
  7934. {
  7935. TransferFileName = File->FileName;
  7936. }
  7937. FDDFileList->AddObject(TransferFileName, File);
  7938. }
  7939. //---------------------------------------------------------------------------
  7940. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7941. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7942. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7943. {
  7944. // sometimes we do not get DDEnd so the list is not released
  7945. SAFE_DESTROY(FDDFileList);
  7946. FDDFileList = new TStringList();
  7947. FDragTempDir = WinConfiguration->TemporaryDir();
  7948. FDDTotalSize = 0;
  7949. FDragDropSshTerminate.reset(NULL);
  7950. }
  7951. //---------------------------------------------------------------------------
  7952. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7953. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7954. HRESULT & Result)
  7955. {
  7956. if (Result == DRAGDROP_S_DROP)
  7957. {
  7958. try
  7959. {
  7960. GlobalDragImageList->HideDragImage();
  7961. try
  7962. {
  7963. RemoteFileControlDDTargetDrop();
  7964. }
  7965. catch(ESshTerminate & E)
  7966. {
  7967. FDragDropSshTerminate.reset(E.Clone());
  7968. }
  7969. }
  7970. catch (Exception &E)
  7971. {
  7972. // If downloading fails we need to cancel drag&drop, otherwise
  7973. // Explorer shows error
  7974. // But by the way exception probably never reach this point as
  7975. // it's catched on way.
  7976. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7977. Result = DRAGDROP_S_CANCEL;
  7978. ShowExtendedException(Terminal, &E);
  7979. }
  7980. }
  7981. }
  7982. //---------------------------------------------------------------------------
  7983. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7984. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7985. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7986. {
  7987. TFileMasks::TParams MaskParams;
  7988. MaskParams.Size = Size;
  7989. MaskParams.Modification = Modification;
  7990. // this does not re-parse the mask if it is the same as the last time
  7991. FDirViewMatchMask = Masks;
  7992. bool ImplicitMatch;
  7993. Matches =
  7994. // RecurseInclude parameter has no effect as the Path is empty
  7995. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7996. (AllowImplicitMatches || !ImplicitMatch);
  7997. }
  7998. //---------------------------------------------------------------------------
  7999. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  8000. TObject * Sender, TListItem * Item, WORD & Indexes)
  8001. {
  8002. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8003. UnicodeString Ext;
  8004. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  8005. {
  8006. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  8007. }
  8008. else
  8009. {
  8010. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  8011. }
  8012. if (SameText(Ext, Configuration->PartialExt))
  8013. {
  8014. Indexes |= oiPartial;
  8015. }
  8016. }
  8017. //---------------------------------------------------------------------------
  8018. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  8019. {
  8020. return
  8021. DirViewEnabled(osCurrent) &&
  8022. DirView(osCurrent)->CanPasteFromClipBoard();
  8023. }
  8024. //---------------------------------------------------------------------------
  8025. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  8026. {
  8027. bool Result = false;
  8028. if (CanPasteToDirViewFromClipBoard())
  8029. {
  8030. Result = true;
  8031. }
  8032. else
  8033. {
  8034. UnicodeString ClipboardText;
  8035. if (NonEmptyTextFromClipboard(ClipboardText) &&
  8036. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  8037. {
  8038. if (StoredSessions->IsUrl(ClipboardText))
  8039. {
  8040. Result = true;
  8041. }
  8042. else
  8043. {
  8044. Result = DirViewEnabled(osCurrent);
  8045. }
  8046. }
  8047. }
  8048. return Result;
  8049. }
  8050. //---------------------------------------------------------------------------
  8051. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  8052. {
  8053. // Pasting to any local panel is handled in TScpCommanderForm::PasteFromClipBoard()
  8054. if (DoesClipboardContainOurFiles())
  8055. {
  8056. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  8057. {
  8058. DebugAssert(!IsLocalBrowserMode());
  8059. TTerminalManager * Manager = TTerminalManager::Instance();
  8060. TTerminal * TargetTerminal = Manager->ActiveTerminal;
  8061. Manager->ActiveSession = FClipboardTerminal;
  8062. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TargetTerminal);
  8063. }
  8064. }
  8065. else if (CanPasteToDirViewFromClipBoard())
  8066. {
  8067. DirView(osCurrent)->PasteFromClipBoard();
  8068. }
  8069. else
  8070. {
  8071. UnicodeString ClipboardText;
  8072. if (NonEmptyTextFromClipboard(ClipboardText))
  8073. {
  8074. if (StoredSessions->IsUrl(ClipboardText))
  8075. {
  8076. NewSession(ClipboardText);
  8077. }
  8078. else
  8079. {
  8080. DirView(osCurrent)->Path = ClipboardText;
  8081. }
  8082. }
  8083. }
  8084. }
  8085. //---------------------------------------------------------------------------
  8086. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  8087. {
  8088. // TBD
  8089. }
  8090. //---------------------------------------------------------------------------
  8091. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  8092. {
  8093. TCustomDirView * ADirView = DirView(Side);
  8094. ADirView->SelectAll(Mode);
  8095. ADirView->SetFocus();
  8096. }
  8097. //---------------------------------------------------------------------------
  8098. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  8099. {
  8100. TCustomDirView * ADirView = DirView(Side);
  8101. TFileFilter Filter;
  8102. DefaultFileFilter(Filter);
  8103. if (DoSelectMaskDialog(ADirView, Select, Filter))
  8104. {
  8105. Configuration->Usage->Inc(L"MaskSelections");
  8106. ADirView->SelectFiles(Filter, Select);
  8107. ADirView->SetFocus();
  8108. }
  8109. }
  8110. //---------------------------------------------------------------------------
  8111. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  8112. {
  8113. TCustomDirView * ADirView = DirView(Side);
  8114. ADirView->RestoreSelectedNames();
  8115. ADirView->SetFocus();
  8116. }
  8117. //---------------------------------------------------------------------------
  8118. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  8119. {
  8120. TCustomDirView * CurrentDirView = DirView(osCurrent);
  8121. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  8122. {
  8123. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  8124. UnicodeString Ext;
  8125. if (!IsSideLocalBrowser(osCurrent))
  8126. {
  8127. Ext = UnixExtractFileExt(FileName);
  8128. }
  8129. else
  8130. {
  8131. Ext = ExtractFileExt(FileName);
  8132. }
  8133. if (Ext.IsEmpty())
  8134. {
  8135. Ext = L".";
  8136. }
  8137. TFileFilter Filter;
  8138. Filter.Masks = FORMAT(L"*%s", (Ext));
  8139. Filter.Directories = false;
  8140. CurrentDirView->SelectFiles(Filter, Select);
  8141. }
  8142. }
  8143. //---------------------------------------------------------------------------
  8144. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8145. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8146. {
  8147. UnicodeString Result;
  8148. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  8149. {
  8150. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  8151. }
  8152. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8153. {
  8154. // noop
  8155. }
  8156. else
  8157. {
  8158. Result =
  8159. FMTLOAD(FILE_INFO_FORMAT,
  8160. (FormatBytes(FileInfo.SelectedSize),
  8161. FormatBytes(FileInfo.FilesSize),
  8162. FormatNumber(FileInfo.SelectedCount),
  8163. FormatNumber(FileInfo.FilesCount)));
  8164. }
  8165. return Result;
  8166. }
  8167. //---------------------------------------------------------------------------
  8168. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8169. TTBXStatusPanel * Panel, TOperationSide Side)
  8170. {
  8171. if (Panel->Index == 1)
  8172. {
  8173. ToggleShowHiddenFiles();
  8174. }
  8175. else if (Panel->Index == 2)
  8176. {
  8177. Filter(Side);
  8178. }
  8179. }
  8180. //---------------------------------------------------------------------------
  8181. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8182. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8183. {
  8184. DebugAssert(!StatusBar->SimplePanel);
  8185. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8186. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8187. }
  8188. //---------------------------------------------------------------------------
  8189. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8190. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8191. {
  8192. DebugAssert(StatusBar->Panels->Count >= 3);
  8193. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8194. if (FileInfo.HiddenCount > 0)
  8195. {
  8196. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8197. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8198. }
  8199. else
  8200. {
  8201. HiddenFilesPanel->ViewPriority = 0;
  8202. // not really necessary, just to cleanup no-longer-valid data
  8203. HiddenFilesPanel->Caption = L"";
  8204. }
  8205. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8206. if (FileInfo.FilteredCount > 0)
  8207. {
  8208. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8209. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8210. }
  8211. else
  8212. {
  8213. FilteredFilesPanel->ViewPriority = 0;
  8214. // not really necessary, just to cleanup no-longer-valid data
  8215. FilteredFilesPanel->Caption = L"";
  8216. }
  8217. }
  8218. //---------------------------------------------------------------------------
  8219. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8220. TObject * /*Sender*/)
  8221. {
  8222. if (DirView(osOther)->Enabled)
  8223. {
  8224. DirView(osOther)->SetFocus();
  8225. }
  8226. }
  8227. //---------------------------------------------------------------------------
  8228. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8229. {
  8230. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8231. switch (Config.Show)
  8232. {
  8233. case qvShow:
  8234. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8235. {
  8236. Config.Show = qvHide;
  8237. }
  8238. else
  8239. {
  8240. Config.Show = Config.LastHideShow;
  8241. }
  8242. break;
  8243. case qvHideWhenEmpty:
  8244. if (ComponentVisible[fcQueueView])
  8245. {
  8246. Config.Show = qvHide;
  8247. }
  8248. else
  8249. {
  8250. Config.LastHideShow = Config.Show;
  8251. Config.Show = qvShow;
  8252. }
  8253. break;
  8254. case qvHide:
  8255. Config.LastHideShow = Config.Show;
  8256. Config.Show = qvShow;
  8257. break;
  8258. }
  8259. WinConfiguration->QueueView = Config;
  8260. }
  8261. //---------------------------------------------------------------------------
  8262. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8263. {
  8264. UnicodeString Result;
  8265. if (Terminal != NULL)
  8266. {
  8267. switch (WinConfiguration->PathInCaption)
  8268. {
  8269. case picShort:
  8270. Result = ExtractShortName(Terminal->CurrentDirectory, true);
  8271. break;
  8272. case picFull:
  8273. Result = Terminal->CurrentDirectory;
  8274. break;
  8275. }
  8276. }
  8277. return Result;
  8278. }
  8279. //---------------------------------------------------------------------------
  8280. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8281. {
  8282. TColor Result = GetWindowColor(FSessionColor);
  8283. return Result;
  8284. }
  8285. //---------------------------------------------------------------------------
  8286. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8287. {
  8288. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8289. return Result;
  8290. }
  8291. //---------------------------------------------------------------------------
  8292. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8293. {
  8294. if (!FSessionChanging)
  8295. {
  8296. TTerminalManager::Instance()->UpdateAppTitle();
  8297. }
  8298. // WORKAROUND: Disabling list view when it is not showing yet does not set its
  8299. // background to gray on Windows 7 (works on Windows 10).
  8300. // See also EnableControl
  8301. if (Showing)
  8302. {
  8303. if (ManagedSession != NULL)
  8304. {
  8305. // Update path when it changes
  8306. if ((SessionsPageControl->ActivePage != NULL) &&
  8307. (GetSessionTabSession(SessionsPageControl->ActivePage) == ManagedSession))
  8308. {
  8309. UpdateSessionTab(SessionsPageControl->ActivePage);
  8310. }
  8311. }
  8312. NonVisualDataModule->ReconnectSessionAction->Update();
  8313. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8314. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8315. CenterReconnectToolbar();
  8316. bool HasTerminal = HasActiveTerminal();
  8317. RemoteDirView->Enabled = HasTerminal;
  8318. RemoteDirView->Color = HasTerminal ? PanelColor() : DisabledPanelColor();
  8319. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8320. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8321. RemoteDriveView->Color = RemoteDirView->Color;
  8322. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8323. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8324. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8325. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8326. QueueFileList->Enabled = QueueView3->Enabled;
  8327. QueueFileList->Color = QueueView3->Color;
  8328. QueueFileList->Font->Color = QueueView3->Font->Color;
  8329. QueueLabelUpdateStatus();
  8330. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8331. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8332. if (FImmersiveDarkMode != WinConfiguration->UseDarkTheme())
  8333. {
  8334. UpdateDarkMode();
  8335. }
  8336. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal || IsLocalBrowserMode();
  8337. }
  8338. }
  8339. //---------------------------------------------------------------------------
  8340. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8341. {
  8342. UpdateControls();
  8343. }
  8344. //---------------------------------------------------------------------------
  8345. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8346. TObject * Sender)
  8347. {
  8348. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8349. DebugAssert(DirView != NULL);
  8350. DoDirViewLoaded(DirView);
  8351. TransferPresetAutoSelect();
  8352. }
  8353. //---------------------------------------------------------------------------
  8354. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8355. {
  8356. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8357. // first run after installation
  8358. if (double(Updates.LastCheck) == 0)
  8359. {
  8360. // make sure next time there will be an update (if enabled)
  8361. Updates.LastCheck = TDateTime(1);
  8362. WinConfiguration->Updates = Updates;
  8363. }
  8364. else if ((double(Updates.Period) > 0) &&
  8365. (Now() - Updates.LastCheck >= Updates.Period))
  8366. {
  8367. TThreadMethod OnUpdatesChecked = NULL;
  8368. if (!IsUWP())
  8369. {
  8370. OnUpdatesChecked = UpdatesChecked;
  8371. }
  8372. StartUpdateThread(OnUpdatesChecked);
  8373. }
  8374. }
  8375. //---------------------------------------------------------------------------
  8376. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8377. {
  8378. UnicodeString Message;
  8379. bool New;
  8380. TQueryType Type;
  8381. GetUpdatesMessage(Message, New, Type, false);
  8382. if (!Message.IsEmpty())
  8383. {
  8384. if (New)
  8385. {
  8386. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8387. }
  8388. if (!New && (Type != qtWarning))
  8389. {
  8390. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8391. }
  8392. else
  8393. {
  8394. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8395. }
  8396. if (New)
  8397. {
  8398. Configuration->Usage->Inc(L"UpdateNotifications");
  8399. }
  8400. }
  8401. }
  8402. //---------------------------------------------------------------------------
  8403. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8404. {
  8405. RestoreApp();
  8406. bool CanDisplay = !NonVisualDataModule->Busy;
  8407. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8408. {
  8409. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8410. }
  8411. if (CanDisplay)
  8412. {
  8413. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8414. CheckForUpdates(true);
  8415. }
  8416. else
  8417. {
  8418. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8419. }
  8420. }
  8421. //---------------------------------------------------------------------------
  8422. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8423. TCopyParamRuleData & Data)
  8424. {
  8425. DebugAssert(Terminal != NULL);
  8426. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8427. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8428. Data.RemoteDirectory = RemoteDirView->PathName;
  8429. }
  8430. //---------------------------------------------------------------------------
  8431. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8432. {
  8433. // Terminal can be null when we are changing local directory implicitly,
  8434. // because it has been deleted, while no session is connected
  8435. // (Login dialog is open)
  8436. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8437. {
  8438. DebugAssert(!IsLocalBrowserMode());
  8439. TCopyParamRuleData Data;
  8440. GetTransferPresetAutoSelectData(Data);
  8441. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8442. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8443. if (CopyParamIndex < 0)
  8444. {
  8445. // there is no preset that matches autoselection
  8446. // set preset that we consider "default"
  8447. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8448. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8449. }
  8450. else
  8451. {
  8452. // there is preset matching autoselection
  8453. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8454. if (CopyParamName == FCopyParamAutoSelected)
  8455. {
  8456. // autoselected the same preset as the last time
  8457. // make no change (i.e. preserve custom user preset, if any)
  8458. }
  8459. else
  8460. {
  8461. // autoselected the different preset then the last time (or there
  8462. // was default preset set)
  8463. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8464. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8465. }
  8466. }
  8467. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8468. {
  8469. if (CopyParamIndex >= 0)
  8470. {
  8471. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8472. }
  8473. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8474. try
  8475. {
  8476. int Fmt =
  8477. (CopyParamIndex < 0) ?
  8478. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8479. COPY_PARAM_AUTOSELECTED;
  8480. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8481. Data->Message = MainInstructions(Message);
  8482. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8483. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8484. CopyParamAttrs);
  8485. if (CopyParamIndex >= 0)
  8486. {
  8487. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8488. Info = FORMAT(L"%s\n \n%s", (Info,
  8489. FMTLOAD(COPY_PARAM_RULE,
  8490. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8491. }
  8492. Data->Message += L"\n\n" + Info;
  8493. if (WinConfiguration->CopyParamAutoSelectNotice)
  8494. {
  8495. TransferPresetNoteMessage(Data, true);
  8496. }
  8497. else
  8498. {
  8499. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8500. Data = NULL; // ownership passed
  8501. }
  8502. }
  8503. __finally
  8504. {
  8505. delete Data;
  8506. }
  8507. }
  8508. }
  8509. }
  8510. //---------------------------------------------------------------------------
  8511. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8512. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8513. {
  8514. DebugAssert(NoteData != NULL);
  8515. DebugAssert(!IsLocalBrowserMode());
  8516. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8517. TQueryButtonAlias Aliases[1];
  8518. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8519. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8520. Params.Aliases = Aliases;
  8521. Params.AliasesCount = LENOF(Aliases);
  8522. unsigned int Result =
  8523. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8524. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8525. switch (Result)
  8526. {
  8527. case qaNeverAskAgain:
  8528. DebugAssert(AllowNeverAskAgain);
  8529. WinConfiguration->CopyParamAutoSelectNotice = false;
  8530. break;
  8531. case qaIgnore:
  8532. PreferencesDialog(pmPresets);
  8533. break;
  8534. }
  8535. }
  8536. //---------------------------------------------------------------------------
  8537. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8538. {
  8539. // as of now useless, as this is used for notes only, never for balloons, ...
  8540. RestoreApp();
  8541. // .. and we should never be busy here
  8542. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8543. {
  8544. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8545. }
  8546. }
  8547. //---------------------------------------------------------------------------
  8548. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8549. TPreferencesMode APreferencesMode)
  8550. {
  8551. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8552. TCopyParamRuleData Data;
  8553. if (Terminal != NULL)
  8554. {
  8555. PreferencesData.reset(new TPreferencesDialogData());
  8556. GetTransferPresetAutoSelectData(Data);
  8557. PreferencesData->CopyParamRuleData = &Data;
  8558. }
  8559. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8560. }
  8561. //---------------------------------------------------------------------------
  8562. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8563. const TCustomCommandType & Command)
  8564. {
  8565. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8566. {
  8567. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  8568. }
  8569. }
  8570. //---------------------------------------------------------------------------
  8571. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8572. {
  8573. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8574. TCustomCommandType Command;
  8575. // make sure we use local custom command when remote are not supported
  8576. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8577. {
  8578. Command = FLastCustomCommand;
  8579. }
  8580. else
  8581. {
  8582. Command.Params = Command.Params | ccLocal;
  8583. }
  8584. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8585. FEditingFocusedAdHocCommand = OnFocused;
  8586. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8587. int Options =
  8588. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8589. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8590. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8591. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8592. {
  8593. FLastCustomCommand = Command;
  8594. UpdateCustomCommandsToolbar();
  8595. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8596. }
  8597. }
  8598. //---------------------------------------------------------------------------
  8599. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8600. {
  8601. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8602. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8603. DebugAssert(State > 0);
  8604. if (State <= 0)
  8605. {
  8606. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  8607. }
  8608. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8609. }
  8610. //---------------------------------------------------------------------------
  8611. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8612. TCustomCommandType & Command, int & State)
  8613. {
  8614. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8615. if (Result)
  8616. {
  8617. Command = FLastCustomCommand;
  8618. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8619. }
  8620. return Result;
  8621. }
  8622. //---------------------------------------------------------------------------
  8623. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8624. {
  8625. ResetIncrementalSearch();
  8626. }
  8627. //---------------------------------------------------------------------------
  8628. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8629. {
  8630. DebugAssert(ComponentVisible[Component]);
  8631. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8632. }
  8633. //---------------------------------------------------------------------------
  8634. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8635. {
  8636. // when queue panel is resized here directly, the status bar is stretched
  8637. // over whole space the panel occupied
  8638. PostComponentHide(fcQueueView);
  8639. }
  8640. //---------------------------------------------------------------------------
  8641. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  8642. {
  8643. ComponentVisible[fcQueueFileList] = false;
  8644. }
  8645. //---------------------------------------------------------------------------
  8646. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  8647. {
  8648. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  8649. RefreshColorMode();
  8650. ResetSysDarkTheme();
  8651. ConfigurationChanged();
  8652. ConfigureInterface();
  8653. // Should be called for all controls
  8654. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  8655. }
  8656. //---------------------------------------------------------------------------
  8657. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  8658. {
  8659. // Do not handle, when shutting down anyway (maybe also when not setup completelly yet?)
  8660. if (!FInvalid &&
  8661. (Message.LParam != 0) &&
  8662. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  8663. {
  8664. ThemeChanged();
  8665. }
  8666. TForm::Dispatch(&Message);
  8667. }
  8668. //---------------------------------------------------------------------------
  8669. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8670. {
  8671. TMessage * M = static_cast<TMessage*>(Message);
  8672. switch (M->Msg)
  8673. {
  8674. case CM_DIALOGCHAR:
  8675. CMDialogChar(*M);
  8676. break;
  8677. case WM_APPCOMMAND:
  8678. WMAppCommand(*M);
  8679. break;
  8680. case WM_SYSCOMMAND:
  8681. WMSysCommand(*M);
  8682. break;
  8683. case WM_QUERYENDSESSION:
  8684. WMQueryEndSession(*M);
  8685. break;
  8686. case WM_ENDSESSION:
  8687. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8688. break;
  8689. #ifdef _DEBUG
  8690. case WM_WINDOWPOSCHANGED:
  8691. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8692. break;
  8693. case CM_CUSTOMSTYLECHANGED:
  8694. TForm::Dispatch(Message);
  8695. break;
  8696. #endif
  8697. case WM_COMPONENT_HIDE:
  8698. {
  8699. Byte Component = static_cast<Byte>(M->WParam);
  8700. // sanity check
  8701. if (ComponentVisible[Component])
  8702. {
  8703. // special treatment
  8704. if (Component == fcQueueView)
  8705. {
  8706. ToggleQueueVisibility();
  8707. DebugAssert(!ComponentVisible[fcQueueView]);
  8708. }
  8709. else
  8710. {
  8711. ComponentVisible[Component] = false;
  8712. }
  8713. }
  8714. }
  8715. break;
  8716. case WM_COPYDATA:
  8717. WMCopyData(*M);
  8718. break;
  8719. case WM_MANAGES_CAPTION:
  8720. // caption managed in TTerminalManager::UpdateAppTitle()
  8721. M->Result = 1;
  8722. break;
  8723. case WM_WANTS_MOUSEWHEEL:
  8724. M->Result = 1;
  8725. break;
  8726. case CM_SHOWINGCHANGED:
  8727. CMShowingChanged(*M);
  8728. break;
  8729. case WM_CLOSE:
  8730. WMClose(*M);
  8731. break;
  8732. case WM_DPICHANGED:
  8733. WMDpiChanged(*M);
  8734. break;
  8735. case CM_DPICHANGED:
  8736. CMDpiChanged(*M);
  8737. break;
  8738. case WM_SETTINGCHANGE:
  8739. WMSettingChange(*M);
  8740. break;
  8741. case CM_DIALOGKEY:
  8742. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8743. break;
  8744. default:
  8745. TForm::Dispatch(Message);
  8746. break;
  8747. }
  8748. }
  8749. //---------------------------------------------------------------------------
  8750. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8751. {
  8752. // noop
  8753. }
  8754. //---------------------------------------------------------------------------
  8755. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8756. {
  8757. TForm::Dispatch(&Message);
  8758. FSessionColors->Clear();
  8759. AddFixedSessionImages();
  8760. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8761. UpdateImages();
  8762. }
  8763. //---------------------------------------------------------------------------
  8764. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8765. {
  8766. TForm::Dispatch(&Message);
  8767. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8768. UpdatePixelsPerInchMainWindowCounter();
  8769. }
  8770. //---------------------------------------------------------------------------
  8771. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8772. {
  8773. // Cannot close window while we are busy.
  8774. // We cannot test this in FormCloseQuery as that is called also from
  8775. // Close(), which is called by CloseApplicationAction. So we can be busy
  8776. // there already even, when it is legitimate to close the application.
  8777. // Possibly a better place to handle this would be WMSysCommand.
  8778. if (NonVisualDataModule->Busy)
  8779. {
  8780. Message.Result = 1;
  8781. }
  8782. else
  8783. {
  8784. TForm::Dispatch(&Message);
  8785. }
  8786. }
  8787. //---------------------------------------------------------------------------
  8788. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8789. {
  8790. // Ignoring implicit local-local browser of Commander, opened from DoShow
  8791. bool NoSession = (ManagedSession == NULL);
  8792. // This calls TCustomScpExplorerForm::DoShow
  8793. TForm::Dispatch(&Message);
  8794. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  8795. if (Showing && !FStarted)
  8796. {
  8797. FStarted = true;
  8798. InterfaceStarted();
  8799. }
  8800. if (Showing && NoSession)
  8801. {
  8802. // When we are starting minimized (i.e. from an installer),
  8803. // postpone showing Login dialog until we get restored.
  8804. // Otherwise the Login dialog (and Authentication window) show restored
  8805. // over invisible (minimized) main window.
  8806. if (WindowState == wsMinimized)
  8807. {
  8808. FNeedSession = true;
  8809. }
  8810. else
  8811. {
  8812. // This happens before application ever goes idle, so the toolbars would
  8813. // stay enabled (initial state) until the Login dialog is dismissed.
  8814. UpdateActions();
  8815. NonVisualDataModule->StartBusy();
  8816. try
  8817. {
  8818. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8819. // otherwise the dialog does not receive focus.
  8820. // With Commander interface the ProcessMessages is called already
  8821. // by TDriveView, but with Explorer interface, we need to call it explicily
  8822. Application->ProcessMessages();
  8823. // do not reload sessions, they have been loaded just now (optimization)
  8824. NeedSession(true);
  8825. }
  8826. __finally
  8827. {
  8828. NonVisualDataModule->EndBusy();
  8829. }
  8830. }
  8831. }
  8832. }
  8833. //---------------------------------------------------------------------------
  8834. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  8835. {
  8836. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  8837. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  8838. }
  8839. //---------------------------------------------------------------------------
  8840. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8841. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8842. int & MaxHeight)
  8843. {
  8844. // workaround for bug in TWinControl.CalcConstraints
  8845. // Check for empty rect (restore from iconic state) is done there only after
  8846. // call to AdjustClientRect, which enlarges the rect (for forms).
  8847. TRect R = GetClientRect();
  8848. // when restoring from iconic state, place no restrictions
  8849. if (IsRectEmpty(R))
  8850. {
  8851. MinWidth = 0;
  8852. MinHeight = 0;
  8853. MaxWidth = 32000;
  8854. MaxHeight = 32000;
  8855. }
  8856. }
  8857. //---------------------------------------------------------------------------
  8858. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8859. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8860. {
  8861. // terminal can be already closed (e.g. dropped connection)
  8862. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8863. {
  8864. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8865. try
  8866. {
  8867. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8868. }
  8869. catch(...)
  8870. {
  8871. if (!Terminal->Active)
  8872. {
  8873. Close = true;
  8874. }
  8875. throw;
  8876. }
  8877. }
  8878. }
  8879. //---------------------------------------------------------------------------
  8880. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8881. {
  8882. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8883. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8884. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8885. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8886. {
  8887. OnGetSpaceAvailable = GetSpaceAvailable;
  8888. }
  8889. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8890. OnGetSpaceAvailable);
  8891. }
  8892. //---------------------------------------------------------------------------
  8893. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8894. {
  8895. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8896. Terminal->FillSessionDataForCode(Data.get());
  8897. return Data.release();
  8898. }
  8899. //---------------------------------------------------------------------------
  8900. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8901. {
  8902. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8903. DoGenerateUrlDialog(Data.get(), Paths);
  8904. }
  8905. //---------------------------------------------------------------------------
  8906. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8907. {
  8908. GenerateUrl(NULL);
  8909. }
  8910. //---------------------------------------------------------------------------
  8911. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8912. {
  8913. std::unique_ptr<TStrings> Paths(new TStringList());
  8914. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8915. GenerateUrl(Paths.get());
  8916. }
  8917. //---------------------------------------------------------------------------
  8918. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  8919. {
  8920. FSessionColor = value;
  8921. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8922. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8923. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8924. DebugAssert(ColorItem != NULL);
  8925. ColorItem->Color = C;
  8926. NonVisualDataModule->ColorMenuItem->Color = C;
  8927. UpdateControls();
  8928. }
  8929. //---------------------------------------------------------------------------
  8930. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8931. {
  8932. if (value != FSessionColor)
  8933. {
  8934. UpdateSessionColor(value);
  8935. // Is null when called from LastTerminalClosed
  8936. if (Terminal != NULL)
  8937. {
  8938. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8939. }
  8940. }
  8941. }
  8942. //---------------------------------------------------------------------------
  8943. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8944. {
  8945. bool Result = FNoteTimer->Enabled;
  8946. if (Result)
  8947. {
  8948. // cannot cancel note too early
  8949. bool NotEarly =
  8950. (Now() - FNoteShown >
  8951. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8952. if (Force || NotEarly)
  8953. {
  8954. FNoteTimer->Enabled = false;
  8955. FNote = L"";
  8956. SAFE_DESTROY(FNoteData);
  8957. FOnNoteClick = NULL;
  8958. FNoteHints = FNotes->Text;
  8959. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8960. UpdateStatusBar();
  8961. }
  8962. }
  8963. return Result;
  8964. }
  8965. //---------------------------------------------------------------------------
  8966. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8967. {
  8968. DebugAssert(FNoteTimer->Enabled);
  8969. CancelNote(true);
  8970. }
  8971. //---------------------------------------------------------------------------
  8972. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  8973. {
  8974. FNoteHints = FNotes->Text.TrimRight();
  8975. }
  8976. //---------------------------------------------------------------------------
  8977. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8978. {
  8979. int P = Note.Pos(L"\n");
  8980. if (P > 0)
  8981. {
  8982. Note.SetLength(P - 1);
  8983. }
  8984. FNotes->Add(FORMAT(L"[%s] %s",
  8985. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8986. while (FNotes->Count > 10)
  8987. {
  8988. FNotes->Delete(0);
  8989. }
  8990. if (UpdateNow)
  8991. {
  8992. UpdateNoteHints();
  8993. UpdateStatusBar();
  8994. }
  8995. }
  8996. //---------------------------------------------------------------------------
  8997. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8998. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8999. {
  9000. int P = Note.Pos(L"\n");
  9001. if (P > 0)
  9002. {
  9003. Note.SetLength(P - 1);
  9004. }
  9005. UpdateNoteHints();
  9006. FNote = Note;
  9007. SAFE_DESTROY(FNoteData);
  9008. FNoteData = NoteData;
  9009. FOnNoteClick = OnNoteClick;
  9010. AddNote(Note, false);
  9011. UpdateStatusBar();
  9012. FNoteShown = Now();
  9013. FNoteTimer->Enabled = false;
  9014. if (Seconds == 0)
  9015. {
  9016. Seconds = WinConfiguration->NotificationsTimeout;
  9017. }
  9018. FNoteTimer->Interval = Seconds * MSecsPerSec;
  9019. FNoteTimer->Enabled = true;
  9020. }
  9021. //---------------------------------------------------------------------------
  9022. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  9023. {
  9024. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  9025. }
  9026. //---------------------------------------------------------------------------
  9027. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  9028. {
  9029. if (NonVisualDataModule->SynchronizeBrowsingAction2->Checked)
  9030. {
  9031. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  9032. }
  9033. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  9034. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  9035. (NonVisualDataModule->SynchronizeBrowsingAction2->Checked ? 2 : 3)))),
  9036. 0, NULL, NULL);
  9037. }
  9038. //---------------------------------------------------------------------------
  9039. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  9040. {
  9041. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  9042. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  9043. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  9044. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  9045. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  9046. }
  9047. //---------------------------------------------------------------------------
  9048. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  9049. {
  9050. WinConfiguration->FormatSizeBytes = Style;
  9051. }
  9052. //---------------------------------------------------------------------------
  9053. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  9054. {
  9055. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  9056. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  9057. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  9058. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  9059. }
  9060. //---------------------------------------------------------------------------
  9061. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  9062. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  9063. {
  9064. if (Panel->Index == 0)
  9065. {
  9066. if (FOnNoteClick != NULL)
  9067. {
  9068. // prevent the user data from being freed by possible call
  9069. // to CancelNote or PostNote during call to OnNoteClick
  9070. std::unique_ptr<TObject> NoteData(FNoteData);
  9071. TNotifyEvent OnNoteClick = FOnNoteClick;
  9072. FNoteData = NULL;
  9073. // need to cancel the note as we are going to delete its user data
  9074. CancelNote(true);
  9075. OnNoteClick(NoteData.get());
  9076. }
  9077. }
  9078. if (HasActiveTerminal() &&
  9079. (Panel->Index >= Sender->Panels->Count - SessionPanelCount))
  9080. {
  9081. FileSystemInfo();
  9082. }
  9083. }
  9084. //---------------------------------------------------------------------------
  9085. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  9086. {
  9087. // workaround:
  9088. // 1) for unknown reason, disabling window, while minimized,
  9089. // prevents it from restoring, even if it was enabled again meanwhile
  9090. // 2) when disabling the main window, while another has focus
  9091. // minimize is no longer possible
  9092. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  9093. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  9094. // Shouldn't we use IsApplicationMinimized() here?
  9095. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  9096. {
  9097. Enabled = false;
  9098. }
  9099. FLockLevel++;
  9100. }
  9101. //---------------------------------------------------------------------------
  9102. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  9103. {
  9104. DebugAssert(FLockLevel > 0);
  9105. FLockLevel--;
  9106. if (FLockLevel == 0)
  9107. {
  9108. DebugAssert(FLockSuspendLevel == 0);
  9109. Enabled = true;
  9110. }
  9111. }
  9112. //---------------------------------------------------------------------------
  9113. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  9114. {
  9115. // We need to make sure that window is enabled when the last modal window closes
  9116. // otherwise focus is not restored correctly.
  9117. // So we re-enable the window when modal window opens and
  9118. // disable it back after is closes
  9119. if (FLockLevel > 0)
  9120. {
  9121. // while we have nesting counter, we know that we never be called
  9122. // recursivelly as Application->OnModalBegin is called only
  9123. // for the top-level modal window
  9124. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9125. {
  9126. // won't be disabled when conditions in LockWindow() were not satisfied
  9127. FDisabledOnLockSuspend = !Enabled;
  9128. // When minimized to tray (or actually when set to SW_HIDE),
  9129. // setting Enabled makes the window focusable even when there's
  9130. // modal window over it
  9131. if (!FTrayIcon->Visible)
  9132. {
  9133. Enabled = true;
  9134. }
  9135. }
  9136. FLockSuspendLevel++;
  9137. }
  9138. }
  9139. //---------------------------------------------------------------------------
  9140. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9141. {
  9142. if (FLockSuspendLevel > 0)
  9143. {
  9144. DebugAssert(FLockLevel > 0);
  9145. FLockSuspendLevel--;
  9146. // see comment in SuspendWindowLock
  9147. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9148. {
  9149. // Note that window can be enabled here, when we were minized to tray when
  9150. // was SuspendWindowLock() called.
  9151. // We should possibly do the same check as in LockWindow(),
  9152. // if it is ever possible that the consitions change between
  9153. // SuspendWindowLock() and ResumeWindowLock()
  9154. Enabled = !FDisabledOnLockSuspend;
  9155. }
  9156. }
  9157. }
  9158. //---------------------------------------------------------------------------
  9159. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9160. {
  9161. TTBXComboBoxItem * RemotePathComboBox =
  9162. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9163. if (!TextOnly)
  9164. {
  9165. TStrings * Items = RemotePathComboBox->Strings;
  9166. Items->BeginUpdate();
  9167. try
  9168. {
  9169. Items->Clear();
  9170. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9171. {
  9172. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9173. while (!IsUnixRootPath(APath))
  9174. {
  9175. int P = APath.LastDelimiter(L'/');
  9176. DebugAssert(P >= 0);
  9177. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9178. APath.SetLength(P - 1);
  9179. }
  9180. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9181. }
  9182. }
  9183. __finally
  9184. {
  9185. Items->EndUpdate();
  9186. }
  9187. }
  9188. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9189. // Setting ItemIndex to -1 does not reset its text
  9190. if (RemotePathComboBox->Strings->Count == 0)
  9191. {
  9192. RemotePathComboBox->Text = L"";
  9193. }
  9194. }
  9195. //---------------------------------------------------------------------------
  9196. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxAdjustImageIndex(
  9197. TTBXComboBoxItem * Sender, const UnicodeString DebugUsedArg(AText), int AIndex, int & ImageIndex)
  9198. {
  9199. if (AIndex < 0)
  9200. {
  9201. AIndex = Sender->ItemIndex;
  9202. }
  9203. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9204. }
  9205. //---------------------------------------------------------------------------
  9206. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9207. TTBXComboBoxItem * Sender, const UnicodeString AText, int AIndex,
  9208. int & ImageIndex)
  9209. {
  9210. DoRemotePathComboBoxAdjustImageIndex(Sender, AText, AIndex, ImageIndex);
  9211. }
  9212. //---------------------------------------------------------------------------
  9213. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9214. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9215. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9216. {
  9217. if (!IsLocalBrowserMode())
  9218. {
  9219. ARect.Left += (10 * AIndex);
  9220. }
  9221. }
  9222. //---------------------------------------------------------------------------
  9223. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9224. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9225. {
  9226. AWidth += (10 * AIndex);
  9227. }
  9228. //---------------------------------------------------------------------------
  9229. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxItemClick(TObject * Sender)
  9230. {
  9231. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9232. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9233. int Index = RemotePathComboBox->ItemIndex;
  9234. while (Index < RemotePathComboBox->Strings->Count - 1)
  9235. {
  9236. APath = UnixExtractFileDir(APath);
  9237. Index++;
  9238. }
  9239. // VanDyke style paths
  9240. if (APath.IsEmpty())
  9241. {
  9242. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9243. APath = ROOTDIRECTORY;
  9244. }
  9245. if (RemoteDirView->Path != APath)
  9246. {
  9247. RemoteDirView->Path = APath;
  9248. }
  9249. }
  9250. //---------------------------------------------------------------------------
  9251. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(TObject * Sender)
  9252. {
  9253. DoRemotePathComboBoxItemClick(Sender);
  9254. }
  9255. //---------------------------------------------------------------------------
  9256. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxCancel(TObject * Sender)
  9257. {
  9258. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9259. DebugUsedParam(Sender);
  9260. UpdateRemotePathComboBox(true);
  9261. }
  9262. //---------------------------------------------------------------------------
  9263. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9264. {
  9265. DoRemotePathComboBoxCancel(Sender);
  9266. }
  9267. //---------------------------------------------------------------------------
  9268. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9269. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9270. {
  9271. ResetIncrementalSearch();
  9272. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9273. DebugAssert(DirView != NULL);
  9274. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9275. {
  9276. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9277. // OnEditing is called also from TCustomListView::CanEdit
  9278. if (Edit != NULL)
  9279. {
  9280. EditSelectBaseName(Edit);
  9281. }
  9282. }
  9283. }
  9284. //---------------------------------------------------------------------------
  9285. //---------------------------------------------------------------------------
  9286. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9287. {
  9288. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9289. Result->AutoDetectDnD = false;
  9290. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9291. Result->RenderDataOn = rdoEnterAndDropSync;
  9292. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9293. return Result;
  9294. }
  9295. //---------------------------------------------------------------------------
  9296. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9297. {
  9298. if (IsWin10Build(2004))
  9299. {
  9300. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9301. BOOL DarkMode = FImmersiveDarkMode ? TRUE : FALSE;
  9302. const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  9303. DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
  9304. }
  9305. }
  9306. //---------------------------------------------------------------------------
  9307. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9308. {
  9309. TForm::CreateWnd();
  9310. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9311. UpdateDarkMode();
  9312. if (FSessionsDragDropFilesEx == NULL)
  9313. {
  9314. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9315. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9316. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9317. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9318. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9319. }
  9320. if (FQueueDragDropFilesEx == NULL)
  9321. {
  9322. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9323. // No need to set OnDragOver as we do not have any restrictions
  9324. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9325. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9326. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9327. }
  9328. }
  9329. //---------------------------------------------------------------------------
  9330. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9331. {
  9332. TForm::DestroyWnd();
  9333. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9334. FQueueDragDropFilesEx->DragDropControl = NULL;
  9335. }
  9336. //---------------------------------------------------------------------------
  9337. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9338. {
  9339. SideEnter(FCurrentSide);
  9340. FEverShown = true;
  9341. }
  9342. //---------------------------------------------------------------------------
  9343. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9344. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9345. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9346. {
  9347. if (!NonVisualDataModule->Busy)
  9348. {
  9349. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9350. Configuration->Usage->Inc(L"FileFinds");
  9351. LockWindow(true);
  9352. NonVisualDataModule->StartBusy();
  9353. WinConfiguration->LockedInterface = true;
  9354. try
  9355. {
  9356. Terminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9357. }
  9358. __finally
  9359. {
  9360. WinConfiguration->LockedInterface = false;
  9361. NonVisualDataModule->EndBusy();
  9362. UnlockWindow();
  9363. }
  9364. }
  9365. }
  9366. //---------------------------------------------------------------------------
  9367. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9368. {
  9369. if (!NonVisualDataModule->Busy)
  9370. {
  9371. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9372. SetFocus();
  9373. RemoteDirView->Path = UnixExtractFilePath(Path);
  9374. UnicodeString FileName = UnixExtractFileName(Path);
  9375. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9376. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9377. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9378. if (Item == NULL)
  9379. {
  9380. ReloadDirectory(osRemote);
  9381. Item = RemoteDirView->FindFileItem(FileName);
  9382. }
  9383. if (Item != NULL)
  9384. {
  9385. RemoteDirView->ItemFocused = Item;
  9386. RemoteDirView->ItemFocused->MakeVisible(false);
  9387. RemoteDirView->SetFocus();
  9388. }
  9389. }
  9390. }
  9391. //---------------------------------------------------------------------------
  9392. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9393. {
  9394. bool Result = !NonVisualDataModule->Busy;
  9395. if (Result)
  9396. {
  9397. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9398. }
  9399. return Result;
  9400. }
  9401. //---------------------------------------------------------------------------
  9402. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9403. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9404. {
  9405. if (CanOperateOnFoundFiles(ATerminal))
  9406. {
  9407. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9408. FOnFileOperationFinished = OnFileOperationFinished;
  9409. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9410. }
  9411. }
  9412. //---------------------------------------------------------------------------
  9413. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9414. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9415. {
  9416. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9417. }
  9418. //---------------------------------------------------------------------------
  9419. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9420. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9421. {
  9422. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9423. }
  9424. //---------------------------------------------------------------------------
  9425. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9426. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9427. {
  9428. if (CanOperateOnFoundFiles(ATerminal))
  9429. {
  9430. for (int Index = 0; Index < FileList->Count; Index++)
  9431. {
  9432. UnicodeString FileName = FileList->Strings[Index];
  9433. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9434. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9435. OnFileOperationFinished(osRemote, FileName, true);
  9436. }
  9437. }
  9438. }
  9439. //---------------------------------------------------------------------------
  9440. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9441. {
  9442. FFileFindTerminal = Terminal;
  9443. ShowFileFindDialog(
  9444. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9445. DoDownloadFoundFiles, DoEditFoundFiles);
  9446. }
  9447. //---------------------------------------------------------------------------
  9448. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9449. {
  9450. FTaskbarList = TaskbarList;
  9451. }
  9452. //---------------------------------------------------------------------------
  9453. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9454. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9455. {
  9456. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9457. if (Index >= 0)
  9458. {
  9459. if (Button == mbLeft)
  9460. {
  9461. // WORKAROUND: "Mouse down" is raised only after tab is switched.
  9462. // If switching tab (switching session) takes long enough for user
  9463. // to actually release the button, "mouse down" is still raised,
  9464. // but we do not get "mouse up" event, so dragging is not cancelled,
  9465. // prevent that by not beginning dragging in the first place.
  9466. if (IsKeyPressed(VK_LBUTTON))
  9467. {
  9468. // when user clicks the "+", we get mouse down only after the session
  9469. // is closed, when new session tab is already on X:Y, so dragging
  9470. // starts, prevent that
  9471. if (MilliSecondsBetween(Now(), FSessionsPageControlNewTabTime) > 500)
  9472. {
  9473. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9474. if (Terminal != NULL)
  9475. {
  9476. SessionsPageControl->BeginDrag(false);
  9477. }
  9478. }
  9479. }
  9480. }
  9481. else if (Button == mbMiddle)
  9482. {
  9483. // ignore middle-click for "New session tab" and the last local-local tab
  9484. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9485. if ((Session != NULL) && CanCloseSession(Session))
  9486. {
  9487. CloseSessionTab(Index);
  9488. }
  9489. }
  9490. }
  9491. }
  9492. //---------------------------------------------------------------------------
  9493. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9494. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9495. {
  9496. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9497. // do not allow dropping on the "+" tab
  9498. TTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9499. if ((TargetTerminal != NULL) &&
  9500. (SessionsPageControl->ActivePage->PageIndex != Index))
  9501. {
  9502. Configuration->Usage->Inc(L"SessionTabMoves");
  9503. // this is almost redundant as we would recreate tabs in SessionListChanged,
  9504. // but we want to actually prevent that to avoid flicker
  9505. SessionsPageControl->ActivePage->PageIndex = Index;
  9506. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->ActivePage);
  9507. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9508. }
  9509. }
  9510. //---------------------------------------------------------------------------
  9511. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9512. TObject * Sender, TObject * Source, int X, int Y,
  9513. TDragState /*State*/, bool & Accept)
  9514. {
  9515. Accept = (Sender == Source);
  9516. if (Accept)
  9517. {
  9518. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9519. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9520. // do not allow dragging to the "+" tab
  9521. Accept = (Terminal != NULL);
  9522. }
  9523. }
  9524. //---------------------------------------------------------------------------
  9525. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  9526. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  9527. {
  9528. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9529. if (Index < 0)
  9530. {
  9531. Effect = DROPEFFECT_NONE;
  9532. }
  9533. else
  9534. {
  9535. TManagedTerminal * TargetSession = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9536. // do not allow dropping on the "+" tab or on disconnected or local-local tabs
  9537. if (!IsActiveTerminal(TargetSession) || DebugAlwaysFalse(TargetSession->LocalBrowser))
  9538. {
  9539. Effect = DROPEFFECT_NONE;
  9540. }
  9541. else if ((TargetSession != Terminal) && (Effect == DROPEFFECT_MOVE))
  9542. {
  9543. Effect = DROPEFFECT_COPY;
  9544. }
  9545. }
  9546. }
  9547. //---------------------------------------------------------------------------
  9548. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9549. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9550. {
  9551. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9552. // do not allow dropping on local-local, disconnected and the "+" tab
  9553. // (though we do not seem to get here in that case anyway, contrary to RemoteFileControlDDTargetDrop)
  9554. TManagedTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9555. if (DebugAlwaysTrue(IsActiveTerminal(TargetTerminal) && !TargetTerminal->LocalBrowser))
  9556. {
  9557. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9558. if (!IsFileControl(DropSourceControl, osRemote))
  9559. {
  9560. TTerminalManager::Instance()->ActiveSession = TargetTerminal;
  9561. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9562. // Why don't we use Terminal->CurrentDirectory directly?
  9563. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9564. // do not force queue, drag drop
  9565. false, true);
  9566. }
  9567. }
  9568. }
  9569. //---------------------------------------------------------------------------
  9570. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9571. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9572. {
  9573. // Downloads are handled in RemoteFileControlDDEnd
  9574. if (!IsFileControl(DropSourceControl, osRemote))
  9575. {
  9576. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9577. Terminal->CurrentDirectory,
  9578. // force queue, drag drop
  9579. true, true);
  9580. }
  9581. }
  9582. //---------------------------------------------------------------------------
  9583. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9584. {
  9585. FShowing = false;
  9586. // Do not save empty workspace
  9587. if (WinConfiguration->AutoSaveWorkspace && (ManagedSession != NULL))
  9588. {
  9589. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9590. UnicodeString Name = WorkspaceName();
  9591. DoSaveWorkspace(
  9592. Name, DataList.get(),
  9593. !Configuration->DisablePasswordStoring &&
  9594. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9595. WinConfiguration->LastStoredSession = Name;
  9596. }
  9597. }
  9598. //---------------------------------------------------------------------------
  9599. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9600. {
  9601. TManagedTerminal * ManagedTerminal =
  9602. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9603. if (ManagedTerminal != NULL)
  9604. {
  9605. ManagedTerminal->DirectoryLoaded = Now();
  9606. }
  9607. }
  9608. //---------------------------------------------------------------------------
  9609. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9610. TListItem * /*Item*/, bool /*Selected*/)
  9611. {
  9612. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9613. switch (DirView->LastSelectMethod)
  9614. {
  9615. case smKeyboard:
  9616. Configuration->Usage->Inc(L"KeyboardSelections");
  9617. break;
  9618. case smMouse:
  9619. Configuration->Usage->Inc(L"MouseSelections");
  9620. break;
  9621. }
  9622. }
  9623. //---------------------------------------------------------------------------
  9624. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9625. {
  9626. if (Color != 0)
  9627. {
  9628. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9629. }
  9630. else
  9631. {
  9632. return FSessionTabImageIndex;
  9633. }
  9634. }
  9635. //---------------------------------------------------------------------------
  9636. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9637. {
  9638. TPngImageCollectionItem * Item =
  9639. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9640. FSessionColors->AddPng(Item->PngImage);
  9641. return FSessionColors->Count - 1;
  9642. }
  9643. //---------------------------------------------------------------------------
  9644. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9645. {
  9646. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9647. FNewRemoteTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewRemoteTabAction->ImageIndex);
  9648. FNewLocalTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewLocalTabAction->ImageIndex);
  9649. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9650. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9651. FLocalBrowserTabImageIndex = AddFixedSessionImage(LocalBrowserImageIndex);
  9652. }
  9653. //---------------------------------------------------------------------------
  9654. int TCustomScpExplorerForm::GetNewTabActionImageIndex()
  9655. {
  9656. return NonVisualDataModule->NewRemoteTabAction->ImageIndex;
  9657. }
  9658. //---------------------------------------------------------------------------
  9659. int TCustomScpExplorerForm::GetNewTabTabImageIndex(TOperationSide Side)
  9660. {
  9661. switch (Side)
  9662. {
  9663. case osLocal:
  9664. return FNewLocalTabTabImageIndex;
  9665. case osRemote:
  9666. case osCurrent:
  9667. return FNewRemoteTabTabImageIndex;
  9668. default:
  9669. DebugFail();
  9670. return -1;
  9671. }
  9672. }
  9673. //---------------------------------------------------------------------------
  9674. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9675. {
  9676. for (int Index = 0; Index < Control->ControlCount; Index++)
  9677. {
  9678. TControl * AControl = Control->Controls[Index];
  9679. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9680. if (WinControl != NULL)
  9681. {
  9682. CollectItemsWithTextDisplayMode(WinControl);
  9683. }
  9684. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9685. if (Toolbar != NULL)
  9686. {
  9687. // we care for top-level items only
  9688. TTBCustomItem * Items = Toolbar->Items;
  9689. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9690. {
  9691. TTBCustomItem * Item = Items->Items[ItemIndex];
  9692. if (((Item->DisplayMode == nbdmImageAndText) ||
  9693. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9694. EligibleForImageDisplayMode(Item))
  9695. {
  9696. FItemsWithTextDisplayMode.insert(Item);
  9697. }
  9698. }
  9699. }
  9700. }
  9701. }
  9702. //---------------------------------------------------------------------------
  9703. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9704. {
  9705. return true;
  9706. }
  9707. //---------------------------------------------------------------------------
  9708. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9709. {
  9710. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9711. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9712. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9713. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9714. bool Result = true;
  9715. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9716. {
  9717. TTBCustomItem * Item = *i;
  9718. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9719. if (Label != NULL)
  9720. {
  9721. // optimization
  9722. if (Label->Visible == SelectiveToolbarText)
  9723. {
  9724. Result = false;
  9725. }
  9726. else
  9727. {
  9728. Label->Visible = SelectiveToolbarText;
  9729. }
  9730. }
  9731. else
  9732. {
  9733. // optimization
  9734. if (Item->DisplayMode == DisplayMode)
  9735. {
  9736. Result = false;
  9737. }
  9738. else
  9739. {
  9740. Item->DisplayMode = DisplayMode;
  9741. }
  9742. }
  9743. ++i;
  9744. }
  9745. UpdateNewTabTab();
  9746. return Result;
  9747. }
  9748. //---------------------------------------------------------------------------
  9749. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9750. {
  9751. DebugFail();
  9752. }
  9753. //---------------------------------------------------------------------------
  9754. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9755. {
  9756. // Among other, a lock level is non zero, while directory is being loaded,
  9757. // even when it happens as a result of dirview navigation,
  9758. // i.e. when TNonVisualDataModule is NOT FBusy.
  9759. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9760. // Among other this prevents a panel auto update to occur while
  9761. // directory is changing.
  9762. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9763. }
  9764. //---------------------------------------------------------------------------
  9765. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * Action, TActionAllowed Allowed)
  9766. {
  9767. // While the window is disabled, we still seem to process menu shortcuts at least,
  9768. // so stop it at least here.
  9769. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9770. bool Result;
  9771. if (Allowed == aaUpdate)
  9772. {
  9773. Result = true;
  9774. }
  9775. else
  9776. {
  9777. Result = !NonVisualDataModule->Busy;
  9778. if (Result && (Allowed == aaShortCut))
  9779. {
  9780. // Otherwise F5 for local-local will be swallowed by disabled local<>remote actions in TCustomScpExplorerForm::KeyDown
  9781. Action->Update();
  9782. Result = Action->Enabled;
  9783. }
  9784. }
  9785. return Result;
  9786. }
  9787. //---------------------------------------------------------------------------
  9788. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9789. {
  9790. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9791. }
  9792. //---------------------------------------------------------------------------
  9793. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9794. {
  9795. // This is somewhat redundant to LockWindow() call from
  9796. // TTerminalManager::TerminalReadDirectoryProgress.
  9797. // But disabling window is known not to block keyboard shorcuts
  9798. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9799. if (Busy > 0)
  9800. {
  9801. if (NonVisualDataModule->Busy)
  9802. {
  9803. State = false;
  9804. }
  9805. else
  9806. {
  9807. NonVisualDataModule->StartBusy();
  9808. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9809. LockWindow();
  9810. }
  9811. }
  9812. else if (Busy < 0)
  9813. {
  9814. UnlockWindow();
  9815. NonVisualDataModule->EndBusy();
  9816. }
  9817. else
  9818. {
  9819. State = NonVisualDataModule->Busy;
  9820. }
  9821. }
  9822. //---------------------------------------------------------------------------
  9823. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9824. {
  9825. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9826. if (Index >= 0)
  9827. {
  9828. TPopupMenu * PopupMenu = NULL;
  9829. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9830. if (Session != NULL)
  9831. {
  9832. SessionsPageControl->ActivePageIndex = Index;
  9833. if (DebugAlwaysTrue(SessionTabSwitched()))
  9834. {
  9835. // copied from TControl.WMContextMenu
  9836. SendCancelMode(SessionsPageControl);
  9837. // explicit popup instead of using PopupMenu property
  9838. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9839. // while connecting yet not-connected session and hence
  9840. // allowing an access to commands over not-completelly connected session
  9841. PopupMenu = Session->LocalBrowser ? NonVisualDataModule->LocalBrowserPopup : NonVisualDataModule->SessionsPopup;
  9842. }
  9843. }
  9844. else
  9845. {
  9846. if (SupportsLocalBrowser())
  9847. {
  9848. PopupMenu = NonVisualDataModule->NewTabPopup;
  9849. }
  9850. }
  9851. if (PopupMenu != NULL)
  9852. {
  9853. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9854. PopupMenu->PopupComponent = SessionsPageControl;
  9855. PopupMenu->Popup(Point.x, Point.y);
  9856. }
  9857. }
  9858. Handled = true;
  9859. }
  9860. //---------------------------------------------------------------------------
  9861. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9862. {
  9863. return HasActiveTerminal() && Terminal->IsCapable[fcChangePassword];
  9864. }
  9865. //---------------------------------------------------------------------------
  9866. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9867. {
  9868. Configuration->Usage->Inc(L"PasswordChanges");
  9869. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9870. ChangePasswordData->DisableAuthentationsExceptPassword();
  9871. ChangePasswordData->ChangePassword = true;
  9872. std::unique_ptr<TTerminal> ChangePasswordSession(
  9873. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9874. ChangePasswordSession->AutoReadDirectory = false;
  9875. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9876. {
  9877. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9878. }
  9879. else
  9880. {
  9881. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9882. }
  9883. }
  9884. //---------------------------------------------------------------------------
  9885. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9886. {
  9887. // No nice way to assert SSH2
  9888. return HasActiveTerminal() && (Terminal->FSProtocol == cfsSFTP);
  9889. }
  9890. //---------------------------------------------------------------------------
  9891. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9892. {
  9893. TOperationVisualizer OperationVisualizer;
  9894. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9895. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9896. }
  9897. //---------------------------------------------------------------------------
  9898. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9899. {
  9900. TForm::ChangeScale(M, D);
  9901. int APixelsPerInch = GetControlPixelsPerInch(this);
  9902. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9903. try
  9904. {
  9905. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9906. }
  9907. __finally
  9908. {
  9909. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9910. }
  9911. }
  9912. //---------------------------------------------------------------------------
  9913. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9914. {
  9915. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9916. }
  9917. //---------------------------------------------------------------------
  9918. class TPublicControl : public TControl
  9919. {
  9920. friend class TCustomScpExplorerForm;
  9921. };
  9922. //---------------------------------------------------------------------------
  9923. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9924. {
  9925. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9926. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9927. }
  9928. //---------------------------------------------------------------------------
  9929. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9930. {
  9931. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9932. OpenBookmark(Side, Bookmark);
  9933. }
  9934. //---------------------------------------------------------------------------
  9935. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9936. {
  9937. DoBookmarkClick(osLocal, Sender);
  9938. }
  9939. //---------------------------------------------------------------------------
  9940. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9941. {
  9942. DoBookmarkClick(osRemote, Sender);
  9943. }
  9944. //---------------------------------------------------------------------------
  9945. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9946. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9947. {
  9948. if (BookmarkList != NULL)
  9949. {
  9950. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9951. if (IsLocalBrowserMode())
  9952. {
  9953. Side = osLocal;
  9954. }
  9955. if (!WinConfiguration->UseLocationProfiles)
  9956. {
  9957. std::unique_ptr<TStringList> Directories(new TStringList());
  9958. Directories->CaseSensitive = (Side == osRemote);
  9959. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9960. {
  9961. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9962. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9963. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9964. {
  9965. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9966. Item->Caption = EscapeHotkey(Directory);
  9967. Item->ShortCut = Bookmark->ShortCut;
  9968. Item->OnClick = OnBookmarkClick;
  9969. Item->Tag = reinterpret_cast<int>(Bookmark);
  9970. Directories->Add(Directory);
  9971. Menu->Add(Item.release());
  9972. }
  9973. }
  9974. }
  9975. else
  9976. {
  9977. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9978. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9979. {
  9980. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9981. if (!Bookmark->Node.IsEmpty())
  9982. {
  9983. Folders->Add(Bookmark->Node);
  9984. }
  9985. }
  9986. for (int Index = 0; Index < Folders->Count; Index++)
  9987. {
  9988. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9989. Item->Caption = Folders->Strings[Index];
  9990. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction2->ImageIndex;
  9991. Folders->Objects[Index] = Item.get();
  9992. Menu->Add(Item.release());
  9993. }
  9994. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9995. {
  9996. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9997. TTBCustomItem * Parent;
  9998. if (!Bookmark->Node.IsEmpty())
  9999. {
  10000. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  10001. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  10002. }
  10003. else
  10004. {
  10005. Parent = Menu;
  10006. }
  10007. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10008. Item->Caption = Bookmark->Name;
  10009. Item->ShortCut = Bookmark->ShortCut;
  10010. Item->OnClick = OnBookmarkClick;
  10011. Item->Tag = reinterpret_cast<int>(Bookmark);
  10012. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  10013. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  10014. {
  10015. // No hint for location profiles that are actually mere "bookmarks"
  10016. }
  10017. else
  10018. {
  10019. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  10020. UnicodeString LongHint;
  10021. if (!Bookmark->Local.IsEmpty())
  10022. {
  10023. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  10024. }
  10025. if (!Bookmark->Remote.IsEmpty())
  10026. {
  10027. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  10028. }
  10029. AddToList(Hint, LongHint, L"|");
  10030. Item->Hint = Hint;
  10031. }
  10032. Parent->Add(Item.release());
  10033. }
  10034. }
  10035. AddMenuSeparator(Menu);
  10036. }
  10037. }
  10038. //---------------------------------------------------------------------------
  10039. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  10040. {
  10041. Menu->Clear();
  10042. std::unique_ptr<TTBCustomItem> Item;
  10043. Item.reset(new TTBXItem(Owner));
  10044. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  10045. Item->Options = Item->Options << tboDefault;
  10046. Menu->Add(Item.release());
  10047. AddMenuSeparator(Menu);
  10048. if (Terminal != NULL)
  10049. {
  10050. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  10051. }
  10052. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  10053. Item.reset(new TTBXItem(Owner));
  10054. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction2 : NonVisualDataModule->RemoteAddBookmarkAction2;
  10055. Menu->Add(Item.release());
  10056. }
  10057. //---------------------------------------------------------------------------
  10058. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  10059. {
  10060. bool Result = true;
  10061. bool Remote = !IsSideLocalBrowser(Side);
  10062. try
  10063. {
  10064. if (Remote)
  10065. {
  10066. Terminal->ExceptionOnFail = true;
  10067. }
  10068. try
  10069. {
  10070. DirView(Side)->Path = Path;
  10071. }
  10072. __finally
  10073. {
  10074. if (Remote)
  10075. {
  10076. Terminal->ExceptionOnFail = false;
  10077. }
  10078. }
  10079. }
  10080. catch (Exception & E)
  10081. {
  10082. if (!Remote || Terminal->Active)
  10083. {
  10084. ShowExtendedExceptionEx(Terminal, &E);
  10085. Result = false;
  10086. }
  10087. else
  10088. {
  10089. throw;
  10090. }
  10091. }
  10092. return Result;
  10093. }
  10094. //---------------------------------------------------------------------------
  10095. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  10096. {
  10097. // Make sure there some feedback even when loading tiny local folders
  10098. TInstantOperationVisualizer Visualizer;
  10099. DirView(Side)->ReloadDirectory();
  10100. }
  10101. //---------------------------------------------------------------------------
  10102. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  10103. {
  10104. if (Index == SessionsPageControl->TabIndex)
  10105. {
  10106. CloseSession();
  10107. }
  10108. else
  10109. {
  10110. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10111. TTerminal * Terminal = GetSessionTabSession(TabSheet);
  10112. TTerminalManager * Manager = TTerminalManager::Instance();
  10113. if (!Terminal->Active)
  10114. {
  10115. Manager->FreeTerminal(Terminal);
  10116. }
  10117. else
  10118. {
  10119. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  10120. if (CanCloseQueue(Queue))
  10121. {
  10122. Manager->FreeTerminal(Terminal);
  10123. }
  10124. }
  10125. }
  10126. }
  10127. //---------------------------------------------------------------------------
  10128. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabButtonClick(TPageControl *, int Index)
  10129. {
  10130. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10131. if (Session == NULL)
  10132. {
  10133. TRect ButtonRect = SessionsPageControl->TabButtonRect(Index);
  10134. ButtonRect = TRect(SessionsPageControl->ClientToScreen(ButtonRect.TopLeft()), SessionsPageControl->ClientToScreen(ButtonRect.BottomRight()));
  10135. //TPoint P = TPoint(ButtonRect.Left, ButtonRect.Bottom);
  10136. //P = SessionsPageControl->ClientToScreen(P);
  10137. MenuPopup(NonVisualDataModule->NewTabPopup, ButtonRect, SessionsPageControl);
  10138. }
  10139. else
  10140. {
  10141. CloseSessionTab(Index);
  10142. }
  10143. }
  10144. //---------------------------------------------------------------------------
  10145. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  10146. {
  10147. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  10148. {
  10149. TInstantOperationVisualizer Visualizer;
  10150. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  10151. // before we create a new data object
  10152. ClipboardClear();
  10153. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  10154. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  10155. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  10156. // Intermediate variable to WORKAROUND a crash in compiler
  10157. TObjectList * ClipboardFakeMonitors =
  10158. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  10159. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  10160. if (FClipboardDragDropFilesEx.get() == NULL)
  10161. {
  10162. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  10163. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  10164. }
  10165. FClipboardDragDropFilesEx->FileList->Clear();
  10166. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  10167. FClipboardDragDropFilesEx->CopyToClipboard();
  10168. FClipboardTerminal = Terminal;
  10169. // Need full paths, as cwd can be different once files are pasted
  10170. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  10171. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  10172. {
  10173. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  10174. }
  10175. }
  10176. }
  10177. //---------------------------------------------------------------------------
  10178. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  10179. {
  10180. if (OpenClipboard(0))
  10181. {
  10182. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  10183. CloseClipboard();
  10184. }
  10185. }
  10186. //---------------------------------------------------------------------------
  10187. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  10188. {
  10189. return !FClipboardFakeDirectory.IsEmpty();
  10190. }
  10191. //---------------------------------------------------------------------------
  10192. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  10193. {
  10194. RemoveDir(ApiPath(FClipboardFakeDirectory));
  10195. FClipboardFakeDirectory = UnicodeString();
  10196. FClipboardTerminal = NULL;
  10197. // Also called in Idle()
  10198. FClipboardFakeMonitors.reset(NULL);
  10199. }
  10200. //---------------------------------------------------------------------------
  10201. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10202. {
  10203. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10204. {
  10205. ClipboardStop();
  10206. }
  10207. }
  10208. //---------------------------------------------------------------------------
  10209. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10210. {
  10211. TTerminalManager * Manager = TTerminalManager::Instance();
  10212. Manager->ActiveSession = FClipboardTerminal;
  10213. TTransferOperationParam Params;
  10214. Params.TargetDirectory = TargetDirectory;
  10215. Params.DragDrop = DragDrop;
  10216. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10217. }
  10218. //---------------------------------------------------------------------------
  10219. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10220. {
  10221. // It can actually rarelly happen that some random file is created, while we are shuttting down the monitor
  10222. // (as it pumps a Windows message queue while being shutted down)
  10223. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10224. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10225. {
  10226. // Can fail as it can be e.g. locked by AV, so we retry that later
  10227. bool Removed = RemoveDir(ApiPath(FileName));
  10228. try
  10229. {
  10230. if (!NonVisualDataModule->Busy)
  10231. {
  10232. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10233. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  10234. }
  10235. }
  10236. __finally
  10237. {
  10238. if (!Removed)
  10239. {
  10240. RemoveDir(ApiPath(FileName));
  10241. }
  10242. }
  10243. }
  10244. }
  10245. //---------------------------------------------------------------------------
  10246. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10247. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10248. {
  10249. TFileMasks::TParams MaskParams;
  10250. MaskParams.Size = Size;
  10251. MaskParams.Modification = Modification;
  10252. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10253. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10254. {
  10255. bool ImplicitMatch;
  10256. if (Iter->FileMask.Matches(FileName, Directory, DirView->PathName, &MaskParams, false, ImplicitMatch) &&
  10257. !ImplicitMatch)
  10258. {
  10259. Color = Iter->Color;
  10260. break;
  10261. }
  10262. }
  10263. }
  10264. //---------------------------------------------------------------------------
  10265. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10266. {
  10267. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10268. if (!DirView->IsEditing())
  10269. {
  10270. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10271. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10272. // while searching
  10273. if (Key == VK_BACK)
  10274. {
  10275. if (!FIncrementalSearch.IsEmpty())
  10276. {
  10277. if (FIncrementalSearch.Length() == 1)
  10278. {
  10279. ResetIncrementalSearch();
  10280. }
  10281. else
  10282. {
  10283. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  10284. IncrementalSearch(NewText, false, false);
  10285. }
  10286. Key = 0;
  10287. }
  10288. }
  10289. }
  10290. }
  10291. //---------------------------------------------------------------------------
  10292. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  10293. {
  10294. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10295. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  10296. {
  10297. // Filter control sequences.
  10298. // When not searching yet, prefer use of the space for toggling file selection
  10299. // (so we cannot incrementally search for a string starting with a space).
  10300. if ((Key > VK_SPACE) ||
  10301. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  10302. {
  10303. IncrementalSearch(FIncrementalSearch + Key, false, false);
  10304. Key = 0;
  10305. }
  10306. }
  10307. }
  10308. //---------------------------------------------------------------------------
  10309. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  10310. {
  10311. if (!FIncrementalSearch.IsEmpty())
  10312. {
  10313. FIncrementalSearch = L"";
  10314. DirView(osCurrent)->UpdateStatusBar();
  10315. }
  10316. }
  10317. //---------------------------------------------------------------------------
  10318. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10319. {
  10320. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  10321. if (Item == NULL)
  10322. {
  10323. MessageBeep(MB_ICONHAND);
  10324. }
  10325. else
  10326. {
  10327. TCustomDirView * ADirView = DirView(osCurrent);
  10328. {
  10329. TAutoNestingCounter Guard(FIncrementalSearching);
  10330. ADirView->FocusItem(Item);
  10331. if (ADirView->NortonLike == nlOff)
  10332. {
  10333. if ((ADirView->Selected != Item) ||
  10334. (ADirView->SelCount > 1))
  10335. {
  10336. ADirView->ClearSelection();
  10337. ADirView->Selected = Item;
  10338. }
  10339. }
  10340. }
  10341. FIncrementalSearch = Text;
  10342. Item->MakeVisible(false);
  10343. TListItem * NextItem = SearchFile(Text, true, Reverse);
  10344. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  10345. ADirView->UpdateStatusBar();
  10346. }
  10347. }
  10348. //---------------------------------------------------------------------------
  10349. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  10350. {
  10351. int Index = Item->Index;
  10352. if (!Reverse)
  10353. {
  10354. Index++;
  10355. if (Index >= Item->Owner->Count)
  10356. {
  10357. Index = 0;
  10358. }
  10359. }
  10360. else
  10361. {
  10362. Index--;
  10363. if (Index < 0)
  10364. {
  10365. Index = Item->Owner->Count - 1;
  10366. }
  10367. }
  10368. return Item->Owner->Item[Index];
  10369. }
  10370. //---------------------------------------------------------------------------
  10371. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10372. {
  10373. TCustomDirView * ADirView = DirView(osCurrent);
  10374. if (ADirView->Items->Count == 0)
  10375. {
  10376. return NULL;
  10377. }
  10378. else
  10379. {
  10380. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  10381. TListItem * Item = CurrentItem;
  10382. if (SkipCurrent)
  10383. {
  10384. Item = DebugNotNull(GetNextFile(Item, Reverse));
  10385. }
  10386. while (true)
  10387. {
  10388. bool Matches = false;
  10389. switch (WinConfiguration->PanelSearch)
  10390. {
  10391. case isNameStartOnly:
  10392. Matches = ContainsTextSemiCaseSensitive(Item->Caption.SubString(1, Text.Length()), Text);
  10393. break;
  10394. case isName:
  10395. Matches = ContainsTextSemiCaseSensitive(Item->Caption, Text);
  10396. break;
  10397. case isAll:
  10398. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  10399. int Index = 0;
  10400. while ((Index < ColCount) && !Matches)
  10401. {
  10402. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  10403. if (Visible)
  10404. {
  10405. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  10406. }
  10407. Index++;
  10408. }
  10409. break;
  10410. }
  10411. if (Matches)
  10412. {
  10413. return Item;
  10414. }
  10415. Item = GetNextFile(Item, Reverse);
  10416. if (Item == CurrentItem)
  10417. {
  10418. return NULL;
  10419. }
  10420. }
  10421. }
  10422. }
  10423. //---------------------------------------------------------------------------
  10424. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10425. {
  10426. ResetIncrementalSearch();
  10427. }
  10428. //---------------------------------------------------------------------------
  10429. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10430. {
  10431. if (Message.CharCode == VK_TAB)
  10432. {
  10433. if (!FIncrementalSearch.IsEmpty())
  10434. {
  10435. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10436. bool Reverse = Shift.Contains(ssShift);
  10437. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10438. Message.Result = 1;
  10439. return;
  10440. }
  10441. }
  10442. else if (Message.CharCode == VK_ESCAPE)
  10443. {
  10444. if (!FIncrementalSearch.IsEmpty())
  10445. {
  10446. ResetIncrementalSearch();
  10447. Message.Result = 1;
  10448. return;
  10449. }
  10450. }
  10451. TForm::Dispatch(&Message);
  10452. }
  10453. //---------------------------------------------------------------------------
  10454. void __fastcall TCustomScpExplorerForm::Deactivate()
  10455. {
  10456. TForm::Deactivate();
  10457. // E.g. when switching to an internal editor window
  10458. ResetIncrementalSearch();
  10459. }
  10460. //---------------------------------------------------------------------------
  10461. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10462. {
  10463. // When switching to another application
  10464. ResetIncrementalSearch();
  10465. }
  10466. //---------------------------------------------------------------------------
  10467. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10468. {
  10469. ResetIncrementalSearch();
  10470. }
  10471. //---------------------------------------------------------------------------
  10472. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10473. {
  10474. if (FIncrementalSearching <= 0)
  10475. {
  10476. ResetIncrementalSearch();
  10477. }
  10478. }
  10479. //---------------------------------------------------------------------------
  10480. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10481. {
  10482. CountClicksForWindowPrint(this);
  10483. }
  10484. //---------------------------------------------------------------------------
  10485. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10486. {
  10487. CenterReconnectToolbar();
  10488. }
  10489. //---------------------------------------------------------------------------
  10490. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10491. {
  10492. if (RemoteDirView->ItemFocused != NULL)
  10493. {
  10494. RemoteDirView->ItemFocused->Selected = true;
  10495. }
  10496. }
  10497. //---------------------------------------------------------------------------
  10498. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  10499. {
  10500. if (!FQueueStatusUpdating)
  10501. {
  10502. bool Refresh;
  10503. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  10504. {
  10505. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  10506. }
  10507. else
  10508. {
  10509. if (FQueueFileList->GetCount() > 0)
  10510. {
  10511. FQueueFileList->Clear();
  10512. Refresh = true;
  10513. }
  10514. }
  10515. int Count = FQueueFileList->GetCount();
  10516. if (QueueFileList->Items->Count != Count)
  10517. {
  10518. DebugAssert(Refresh);
  10519. QueueFileList->Items->Count = Count;
  10520. }
  10521. if (Refresh)
  10522. {
  10523. QueueFileList->Invalidate();
  10524. }
  10525. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  10526. }
  10527. }
  10528. //---------------------------------------------------------------------------
  10529. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  10530. {
  10531. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  10532. }
  10533. //---------------------------------------------------------------------------
  10534. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  10535. {
  10536. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  10537. // See TSynchronizeChecklistDialog::UpdateTimer
  10538. if (QueueFileList->Items->Count > 0)
  10539. {
  10540. QueueFileList->Items->Item[0]->MakeVisible(false);
  10541. }
  10542. UpdateQueueFileList();
  10543. }
  10544. //---------------------------------------------------------------------------
  10545. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  10546. {
  10547. Active = QueueView3->Focused() || QueueFileList->Focused();
  10548. }
  10549. //---------------------------------------------------------------------------
  10550. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  10551. {
  10552. QueueLabelUpdateStatus();
  10553. }
  10554. //---------------------------------------------------------------------------
  10555. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  10556. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  10557. {
  10558. int State = FQueueFileList->GetState(Item->Index);
  10559. if (State == qfsProcessed)
  10560. {
  10561. Sender->Canvas->Font->Color = clGrayText;
  10562. }
  10563. }
  10564. //---------------------------------------------------------------------------
  10565. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  10566. {
  10567. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  10568. QueueFileList->Column[0]->Width =
  10569. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  10570. }
  10571. //---------------------------------------------------------------------------
  10572. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  10573. {
  10574. QueueFileListColumnAutoSize();
  10575. }
  10576. //---------------------------------------------------------------------------
  10577. void __fastcall TCustomScpExplorerForm::CloseApp()
  10578. {
  10579. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  10580. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  10581. // So we clear the flag here.
  10582. DebugAssert(NonVisualDataModule->Busy);
  10583. NonVisualDataModule->EndBusy();
  10584. try
  10585. {
  10586. Close();
  10587. }
  10588. __finally
  10589. {
  10590. NonVisualDataModule->StartBusy();
  10591. }
  10592. }
  10593. //---------------------------------------------------------------------------
  10594. bool __fastcall TCustomScpExplorerForm::IsActiveTerminal(TTerminal * Terminal)
  10595. {
  10596. return (Terminal != NULL) && Terminal->Active;
  10597. }
  10598. //---------------------------------------------------------------------------
  10599. bool __fastcall TCustomScpExplorerForm::HasManagedSession()
  10600. {
  10601. return (ManagedSession != NULL);
  10602. }
  10603. //---------------------------------------------------------------------------
  10604. bool __fastcall TCustomScpExplorerForm::HasActiveTerminal()
  10605. {
  10606. return IsActiveTerminal(Terminal);
  10607. }
  10608. //---------------------------------------------------------------------------
  10609. void TCustomScpExplorerForm::LocalLocalCopy(
  10610. TFileOperation, TOperationSide, bool DebugUsedArg(OnFocused), bool DebugUsedArg(NoConfirmation),
  10611. bool DebugUsedArg(DragDrop), unsigned int DebugUsedArg(Flags))
  10612. {
  10613. DebugFail();
  10614. }
  10615. //---------------------------------------------------------------------------
  10616. void TCustomScpExplorerForm::LocalLocalCopyCommand(TFileOperation Operation, TOperationSide Side, bool OnFocused, unsigned int Flags)
  10617. {
  10618. LocalLocalCopy(Operation, Side, OnFocused, !WinConfiguration->ConfirmTransferring, false, Flags);
  10619. }
  10620. //---------------------------------------------------------------------------
  10621. void __fastcall TCustomScpExplorerForm::SessionsPageControlResize(TObject *)
  10622. {
  10623. // Changing tab list triggers the OnResize.
  10624. if (!FUpdatingSessionTabs)
  10625. {
  10626. // UpdateTabsCaptionTruncation should be enough
  10627. SessionListChanged(true);
  10628. }
  10629. }
  10630. //---------------------------------------------------------------------------
  10631. UnicodeString TCustomScpExplorerForm::GetSessionPath(TManagedTerminal * ASession, TOperationSide Side)
  10632. {
  10633. if (ASession == ManagedSession)
  10634. {
  10635. UpdateSession(ASession);
  10636. }
  10637. TSessionData * Data = ASession->StateData;
  10638. UnicodeString Result;
  10639. if (Side == osLocal)
  10640. {
  10641. Result = Data->LocalDirectory;
  10642. }
  10643. else if (DebugAlwaysTrue(Side == osOther))
  10644. {
  10645. Result = ASession->LocalBrowser ? Data->OtherLocalDirectory : Data->RemoteDirectory;
  10646. }
  10647. return Result;
  10648. }
  10649. //---------------------------------------------------------------------------
  10650. UnicodeString TCustomScpExplorerForm::GetTabHintDetails(TManagedTerminal * ASession)
  10651. {
  10652. UnicodeString Result;
  10653. if (!ASession->Active)
  10654. {
  10655. Result = LoadStr(STATUS_NOT_CONNECTED2);
  10656. }
  10657. else
  10658. {
  10659. Result = GetSessionPath(ASession, osRemote);
  10660. }
  10661. return Result;
  10662. }
  10663. //---------------------------------------------------------------------------
  10664. UnicodeString TCustomScpExplorerForm::GetNewTabHintDetails()
  10665. {
  10666. return LoadStr(NEW_REMOTE_TAB_HINT);
  10667. }
  10668. //---------------------------------------------------------------------------
  10669. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabHint(TPageControl *, int Index, UnicodeString & Hint)
  10670. {
  10671. if (Index >= 0)
  10672. {
  10673. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10674. TManagedTerminal * TabSession = GetSessionTabSession(TabSheet);
  10675. if (TabSession == NULL)
  10676. {
  10677. UnicodeString NewTabTabCaption = GetNewTabTabCaption();
  10678. if (NewTabTabCaption != TabSheet->Caption)
  10679. {
  10680. Hint = NewTabTabCaption + L"|" + GetNewTabHintDetails();
  10681. }
  10682. }
  10683. else
  10684. {
  10685. TTerminalManager * Manager = TTerminalManager::Instance();
  10686. UnicodeString TabCaption = Manager->GetSessionTitle(TabSession, true);
  10687. Hint = TabCaption + L"|" + GetTabHintDetails(TabSession);
  10688. }
  10689. }
  10690. }
  10691. //---------------------------------------------------------------------------
  10692. typedef std::vector<TListItem *> TListItemsVector;
  10693. struct TCalculateSizeOperation
  10694. {
  10695. TCalculateSizeStats Stats;
  10696. TListItemsVector ListItems;
  10697. size_t Index;
  10698. };
  10699. //---------------------------------------------------------------------------
  10700. void TCustomScpExplorerForm::CalculateDirectorySizes()
  10701. {
  10702. TCalculateSizeOperation CalculateSizeOperation;
  10703. TOperationSide Side = GetSide(osCurrent);
  10704. TCustomDirView * ADirView = DirView(Side);
  10705. bool FullPath = IsSideLocalBrowser(Side);
  10706. std::unique_ptr<TStrings> AllFileList(ADirView->CreateFileList(false, FullPath, NULL, true));
  10707. std::unique_ptr<TStrings> FileList(new TStringList());
  10708. for (int Index = 0; Index < AllFileList->Count; Index++)
  10709. {
  10710. TListItem * Item = DebugNotNull(dynamic_cast<TListItem *>(AllFileList->Objects[Index]));
  10711. if (ADirView->ItemIsDirectory(Item))
  10712. {
  10713. FileList->AddObject(AllFileList->Strings[Index], ADirView->ItemData(Item));
  10714. CalculateSizeOperation.ListItems.push_back(Item);
  10715. }
  10716. }
  10717. CalculateSizeOperation.Index = 0;
  10718. __int64 Size = 0;
  10719. TValueRestorer<TCalculateSizeOperation *> DirectorySizeOperationRestorer(FCalculateSizeOperation);
  10720. FCalculateSizeOperation = &CalculateSizeOperation;
  10721. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  10722. FOnFileOperationFinished = DirectorySizeCalculated;
  10723. TValueRestorer<TFileOperation> PrimaryOperationRestorer(FPrimaryOperation);
  10724. FPrimaryOperation = foCalculateSize;
  10725. TCalculatedSizes CalculatedSizes;
  10726. CalculateSizeOperation.Stats.CalculatedSizes = &CalculatedSizes;
  10727. if (Side == osLocal)
  10728. {
  10729. Terminal->CalculateLocalFilesSize(FileList.get(), Size, NULL, true, NULL, &CalculatedSizes);
  10730. }
  10731. else
  10732. {
  10733. TCalculateSizeParams Params;
  10734. Params.Stats = &CalculateSizeOperation.Stats;
  10735. Params.UseCache = true;
  10736. Terminal->CalculateFilesSize(FileList.get(), Size, Params);
  10737. }
  10738. }
  10739. //---------------------------------------------------------------------------
  10740. void __fastcall TCustomScpExplorerForm::DirectorySizeCalculated(TOperationSide Side, const UnicodeString & FileName, bool)
  10741. {
  10742. DebugAlwaysTrue(FCalculateSizeOperation != NULL);
  10743. TListItem * Item = VisualiseOperationFinished(Side, FileName, false);
  10744. TCustomDirView * ADirView = dynamic_cast<TCustomDirView *>(Item->Owner->Owner);
  10745. // optimization
  10746. bool GotExpectedItem =
  10747. (FCalculateSizeOperation->Index < FCalculateSizeOperation->ListItems.size()) &&
  10748. (FCalculateSizeOperation->ListItems[FCalculateSizeOperation->Index] == Item);
  10749. int Index = -1;
  10750. if (DebugAlwaysTrue(GotExpectedItem))
  10751. {
  10752. Index = FCalculateSizeOperation->Index;
  10753. FCalculateSizeOperation->Index++;
  10754. }
  10755. else
  10756. {
  10757. TListItemsVector::iterator I =
  10758. std::find(FCalculateSizeOperation->ListItems.begin(), FCalculateSizeOperation->ListItems.end(), Item);
  10759. if (DebugAlwaysTrue(I != FCalculateSizeOperation->ListItems.end()))
  10760. {
  10761. Index = I - FCalculateSizeOperation->ListItems.begin();
  10762. }
  10763. }
  10764. if (DebugAlwaysTrue(Index >= 0))
  10765. {
  10766. __int64 Size = (*FCalculateSizeOperation->Stats.CalculatedSizes)[Index];
  10767. ADirView->SetItemCalculatedSize(Item, Size);
  10768. }
  10769. }