CustomScpExplorer.cpp 384 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623
  1. //---------------------------------------------------------------------------
  2. #define NO_WIN32_LEAN_AND_MEAN
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include <Common.h>
  6. #include "CustomScpExplorer.h"
  7. #include <algorithm>
  8. #include <Bookmarks.h>
  9. #include <Interface.h>
  10. #include <Exceptions.h>
  11. #include <CoreMain.h>
  12. #include <FileSystems.h>
  13. #include <TextsCore.h>
  14. #include <TextsWin.h>
  15. #include <HelpWin.h>
  16. #include <VCLCommon.h>
  17. #include <Progress.h>
  18. #include <SynchronizeProgress.h>
  19. #include <DragExt.h>
  20. #include <WinApi.h>
  21. #include "GUITools.h"
  22. #include "NonVisual.h"
  23. #include "Glyphs.h"
  24. #include "Tools.h"
  25. #include "WinConfiguration.h"
  26. #include "TerminalManager.h"
  27. #include "EditorManager.h"
  28. #include "ProgParams.h"
  29. #include "Setup.h"
  30. #include <Consts.hpp>
  31. #include <DateUtils.hpp>
  32. #include <TB2Common.hpp>
  33. #include <DirectoryMonitor.hpp>
  34. #include <System.IOUtils.hpp>
  35. //---------------------------------------------------------------------------
  36. #pragma package(smart_init)
  37. #pragma link "CustomDirView"
  38. #pragma link "CustomUnixDirView"
  39. #pragma link "IEListView"
  40. #pragma link "NortonLikeListView"
  41. #pragma link "UnixDirView"
  42. #pragma link "CustomDriveView"
  43. #pragma link "UnixDriveView"
  44. #pragma link "CustomDriveView"
  45. #pragma link "TB2Dock"
  46. #pragma link "TBXStatusBars"
  47. #pragma link "TB2Item"
  48. #pragma link "TB2Toolbar"
  49. #pragma link "ThemePageControl"
  50. #pragma link "PathLabel"
  51. #pragma resource "*.dfm"
  52. //---------------------------------------------------------------------------
  53. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  54. static const int SessionPanelCount = 3;
  55. //---------------------------------------------------------------------------
  56. class TMutexGuard
  57. {
  58. public:
  59. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  60. int Timeout = 5000)
  61. {
  62. FMutex = NULL;
  63. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  64. if (WaitResult == WAIT_TIMEOUT)
  65. {
  66. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  67. }
  68. else
  69. {
  70. FMutex = AMutex;
  71. }
  72. }
  73. ~TMutexGuard()
  74. {
  75. if (FMutex != NULL)
  76. {
  77. ReleaseMutex(FMutex);
  78. }
  79. }
  80. private:
  81. HANDLE FMutex;
  82. };
  83. //---------------------------------------------------------------------------
  84. class TWindowLock
  85. {
  86. public:
  87. TWindowLock(TCustomScpExplorerForm * Form) :
  88. FForm(Form)
  89. {
  90. FForm->LockWindow();
  91. }
  92. ~TWindowLock()
  93. {
  94. FForm->UnlockWindow();
  95. }
  96. private:
  97. TCustomScpExplorerForm * FForm;
  98. };
  99. //---------------------------------------------------------------------------
  100. TAutoBatch::TAutoBatch(TCustomScpExplorerForm * Form) :
  101. FForm(Form)
  102. {
  103. FForm->BatchStart(FBatchStorage);
  104. }
  105. //---------------------------------------------------------------------------
  106. TAutoBatch::~TAutoBatch()
  107. {
  108. FForm->BatchEnd(FBatchStorage);
  109. }
  110. //---------------------------------------------------------------------------
  111. struct TTransferOperationParam
  112. {
  113. TTransferOperationParam();
  114. UnicodeString TargetDirectory;
  115. bool Temp;
  116. bool DragDrop;
  117. int Options;
  118. TAutoSwitch Queue;
  119. };
  120. //---------------------------------------------------------------------------
  121. TTransferOperationParam::TTransferOperationParam()
  122. {
  123. Temp = false;
  124. DragDrop = false;
  125. Options = 0;
  126. Queue = asAuto;
  127. }
  128. //---------------------------------------------------------------------------
  129. class TTransferPresetNoteData : public TObject
  130. {
  131. public:
  132. UnicodeString Message;
  133. };
  134. //---------------------------------------------------------------------------
  135. class TTerminalNoteData : public TObject
  136. {
  137. public:
  138. TManagedTerminal * Terminal;
  139. };
  140. //---------------------------------------------------------------------------
  141. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  142. FFormRestored(false),
  143. TForm(Owner)
  144. {
  145. AddStartupSequence(L"F");
  146. FInvalid = false;
  147. FCurrentSide = osRemote;
  148. FEverShown = false;
  149. FDocks = new TList();
  150. InitControls();
  151. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  152. // So that UpdateCustomCommandsToolbar called from RestoreParams works
  153. NonVisualDataModule->ScpExplorer = this;
  154. RestoreParams();
  155. ConfigurationChanged();
  156. RemoteDirView->Invalidate();
  157. FAutoOperation = false;
  158. FOnFileOperationFinished = NULL;
  159. FPrimaryOperation = foNone;
  160. FForceExecution = false;
  161. FIgnoreNextDialogChar = 0;
  162. FErrorList = NULL;
  163. FSynchronizeProgressForm = NULL;
  164. FProgressForm = NULL;
  165. FRefreshLocalDirectory = false;
  166. FRefreshRemoteDirectory = false;
  167. FDDExtMapFile = NULL;
  168. // CreateMutexW keeps failing with ERROR_NOACCESS
  169. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  170. DebugAssert(FDDExtMutex != NULL);
  171. FDDTargetControl = NULL;
  172. FDelayedDeletionTimer = NULL;
  173. FDelayedDeletionList = new TStringList();
  174. FDDFileList = NULL;
  175. FPendingTempSpaceWarn = false;
  176. FCapturedLog = NULL;
  177. FDragDropOperation = false;
  178. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  179. FAllowTransferPresetAutoSelect = true;
  180. FSessionChanging = false;
  181. FCopyParamDefault = L"";
  182. FSynchronizeController = NULL;
  183. FPendingQueueActionItem = NULL;
  184. FLockLevel = 0;
  185. FLockSuspendLevel = 0;
  186. FDisabledOnLockSuspend = false;
  187. FAlternativeDelete = false;
  188. FTrayIcon = new ::TTrayIcon(0);
  189. FTrayIcon->OnClick = TrayIconClick;
  190. FMaxQueueLength = 0;
  191. FLastContextPopupScreenPoint = TPoint(-1, -1);
  192. FTransferResumeList = NULL;
  193. FDeletedFiles = NULL;
  194. FMoveToQueue = false;
  195. StandaloneOperation = false;
  196. FOnFeedSynchronizeError = NULL;
  197. FOnSynchronizeAbort = NULL;
  198. FSynchronizeTerminal = NULL;
  199. FNeedSession = false;
  200. FDoNotIdleCurrentTerminal = 0;
  201. FIncrementalSearching = 0;
  202. FIncrementalSearchHaveNext = false;
  203. FQueueFileList.reset(new TQueueFileList());
  204. FProgressSide = osCurrent;
  205. FCalculateSizeOperation = NULL;
  206. FDownloadingFromClipboard = false;
  207. FClipboardFakeMonitorsPendingReset = false;
  208. FEditorManager = new TEditorManager();
  209. FEditorManager->OnFileChange = ExecutedFileChanged;
  210. FEditorManager->OnFileReload = ExecutedFileReload;
  211. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  212. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  213. FLocalEditors = new TList();
  214. FQueueStatus = NULL;
  215. FQueueStatusSection = new TCriticalSection();
  216. FQueueStatusInvalidated = false;
  217. FQueueItemInvalidated = false;
  218. FQueueStatusUpdating = false;
  219. FQueueActedItem = NULL;
  220. FQueueController = new TQueueController(QueueView3);
  221. FUserActionTimer = new TTimer(this);
  222. FUserActionTimer->Enabled = false;
  223. FUserActionTimer->Interval = 10;
  224. FUserActionTimer->OnTimer = UserActionTimer;
  225. FNotes = new TStringList();
  226. FNoteTimer = new TTimer(this);
  227. FNoteTimer->Enabled = false;
  228. FNoteTimer->OnTimer = NoteTimer;
  229. FOnNoteClick = NULL;
  230. FOle32Library = LoadLibrary(L"Ole32.dll");
  231. FDragMoveCursor = FOle32Library != NULL ?
  232. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  233. UseSystemSettings(this);
  234. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  235. static_cast<TObject*>(GetComponent(fcTransferList)));
  236. DebugAssert(TransferList != NULL);
  237. FTransferListHoverIndex = -1;
  238. TransferList->OnChange = TransferListChange;
  239. TransferList->OnDrawItem = TransferListDrawItem;
  240. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))), true);
  241. SetSubmenu(NonVisualDataModule->ColorMenuItem, true);
  242. UseDesktopFont(SessionsPageControl);
  243. UpdateSessionsPageControlHeight();
  244. UseDesktopFont(RemoteDirView);
  245. UseDesktopFont(RemoteDriveView);
  246. UseDesktopFont(QueueView3);
  247. UseDesktopFont(QueueFileList);
  248. UseDesktopFont(QueueLabel);
  249. UseDesktopFont(RemoteStatusBar);
  250. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  251. QueueSplitter->ShowHint = true;
  252. reinterpret_cast<TLabel*>(QueueFileListSplitter)->OnDblClick = QueueFileListSplitterDblClick;
  253. QueueFileListSplitter->ShowHint = true;
  254. RemotePanelSplitter->ShowHint = true;
  255. UpdateImages();
  256. FCustomCommandMenu = CreateTBXPopupMenu(this);
  257. FCustomCommandLocalFileList = NULL;
  258. FCustomCommandRemoteFileList = NULL;
  259. FSessionColors = new TPngImageList(this);
  260. FSessionColors->ColorDepth = cd32Bit;
  261. AddFixedSessionImages();
  262. SessionsPageControl->Images = FSessionColors;
  263. UpdateQueueLabel();
  264. CreateHiddenWindow();
  265. StartUpdates();
  266. }
  267. //---------------------------------------------------------------------------
  268. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  269. {
  270. FInvalid = true;
  271. // this has to be one of the very first things to do
  272. StopUpdateThread();
  273. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  274. TTerminalManager::Instance()->ScpExplorer = NULL;
  275. delete FCustomCommandLocalFileList;
  276. delete FCustomCommandRemoteFileList;
  277. delete FCustomCommandMenu;
  278. delete FTrayIcon;
  279. FTrayIcon = NULL;
  280. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  281. delete FEditorManager;
  282. ForceCloseLocalEditors();
  283. delete FLocalEditors;
  284. HideFileFindDialog();
  285. if (FDelayedDeletionTimer)
  286. {
  287. DoDelayedDeletion(NULL);
  288. SAFE_DESTROY(FDelayedDeletionTimer);
  289. }
  290. SAFE_DESTROY(FDelayedDeletionList);
  291. // sometimes we do not get DDEnd so the list is not released
  292. SAFE_DESTROY(FDDFileList);
  293. DebugAssert(FSynchronizeController == NULL);
  294. CloseHandle(FDDExtMutex);
  295. FDDExtMutex = NULL;
  296. FreeLibrary(FOle32Library);
  297. FOle32Library = NULL;
  298. FDragMoveCursor = NULL;
  299. DebugAssert(!FErrorList);
  300. if (FEverShown)
  301. {
  302. // when window is never shown (like when running command-line operation),
  303. // particularly window site is not restored correctly (BoundsRect value set
  304. // in RestoreForm gets lost during handle allocation), so we do not want
  305. // it to be stored
  306. StoreParams();
  307. }
  308. ManagedSession = NULL;
  309. Queue = NULL;
  310. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  311. NonVisualDataModule->ScpExplorer = NULL;
  312. delete FQueueController;
  313. FQueueController = NULL;
  314. delete FQueueStatusSection;
  315. FQueueStatusSection = NULL;
  316. delete FQueueStatus;
  317. FQueueStatus = NULL;
  318. delete FUserActionTimer;
  319. FUserActionTimer = NULL;
  320. delete FNoteTimer;
  321. delete FNotes;
  322. delete FNoteData;
  323. SAFE_DESTROY(FDocks);
  324. SessionsPageControl->Images = NULL;
  325. // only after clearing Terminal (after SessionListChanged => ... => AddSessionColor is called)
  326. SAFE_DESTROY(FSessionColors);
  327. SAFE_DESTROY(FSessionsDragDropFilesEx);
  328. SAFE_DESTROY(FHistoryMenu[0][0]);
  329. SAFE_DESTROY(FHistoryMenu[0][1]);
  330. SAFE_DESTROY(FHistoryMenu[1][0]);
  331. SAFE_DESTROY(FHistoryMenu[1][1]);
  332. if (FHiddenWindow != NULL)
  333. {
  334. DestroyWindow(FHiddenWindow);
  335. FHiddenWindow = NULL;
  336. }
  337. }
  338. //---------------------------------------------------------------------------
  339. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  340. {
  341. std::unique_ptr<TSessionData> Data;
  342. if (!Session.IsEmpty())
  343. {
  344. bool DefaultsOnly;
  345. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  346. }
  347. TTerminalManager * Manager = TTerminalManager::Instance();
  348. for (int Index = 0; Index < Manager->Count; Index++)
  349. {
  350. TTerminal * Terminal = Manager->Sessions[Index];
  351. if (Session.IsEmpty() ||
  352. Manager->IsActiveTerminalForSite(Terminal, Data.get()))
  353. {
  354. if (Path.IsEmpty())
  355. {
  356. Terminal->ClearCaches();
  357. }
  358. else
  359. {
  360. Terminal->DirectoryModified(Path, true);
  361. }
  362. }
  363. }
  364. // We should flag a pending refresh for the background terminals or busy foreground terminals
  365. if (HasActiveTerminal() &&
  366. CanCommandLineFromAnotherInstance() &&
  367. (Session.IsEmpty() || Manager->IsActiveTerminalForSite(Terminal, Data.get())) &&
  368. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  369. {
  370. Terminal->ReloadDirectory();
  371. }
  372. }
  373. //---------------------------------------------------------------------------
  374. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  375. {
  376. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  377. size_t MessageSize = sizeof(TCopyDataMessage);
  378. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  379. if (Result)
  380. {
  381. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  382. Result = (Message.Version == TCopyDataMessage::Version1);
  383. if (Result)
  384. {
  385. switch (Message.Command)
  386. {
  387. case TCopyDataMessage::CommandCanCommandLine:
  388. Result = CanCommandLineFromAnotherInstance();
  389. break;
  390. case TCopyDataMessage::CommandCommandLine:
  391. {
  392. UnicodeString CommandLine(Message.CommandLine);
  393. Result = CommandLineFromAnotherInstance(CommandLine);
  394. }
  395. break;
  396. case TCopyDataMessage::RefreshPanel:
  397. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  398. break;
  399. case TCopyDataMessage::MainWindowCheck:
  400. Result = true;
  401. break;
  402. default:
  403. Result = false;
  404. break;
  405. }
  406. }
  407. }
  408. Message.Result = Result ? 1 : 0;
  409. }
  410. //---------------------------------------------------------------------------
  411. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  412. {
  413. WNDCLASS WindowClass = {0};
  414. WindowClass.lpfnWndProc = DefWindowProc;
  415. WindowClass.hInstance = HInstance;
  416. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  417. FHiddenWindow = NULL;
  418. if (RegisterClass(&WindowClass))
  419. {
  420. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  421. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  422. }
  423. }
  424. //---------------------------------------------------------------------------
  425. bool __fastcall TCustomScpExplorerForm::CanConsole()
  426. {
  427. return
  428. HasActiveTerminal() &&
  429. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  430. }
  431. //---------------------------------------------------------------------------
  432. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  433. {
  434. bool Result = Visible && !NonVisualDataModule->Busy;
  435. return Result;
  436. }
  437. //---------------------------------------------------------------------------
  438. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  439. const UnicodeString & CommandLine)
  440. {
  441. TProgramParams Params(CommandLine);
  442. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  443. if (Result)
  444. {
  445. NonVisualDataModule->StartBusy();
  446. try
  447. {
  448. // this action is initiated from another process,
  449. // so it's likely that our window is not visible,
  450. // and user won't see what is going on
  451. Application->BringToFront();
  452. // reload sessions as we may be asked to open a session
  453. // just stored by another instance
  454. StoredSessions->Reload();
  455. UnicodeString SessionName = Params.Param[1];
  456. std::unique_ptr<TObjectList> DataList(new TObjectList());
  457. try
  458. {
  459. UnicodeString DownloadFile; // unused
  460. int Flags = GetCommandLineParseUrlFlags(&Params);
  461. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this, Flags);
  462. }
  463. catch (EAbort &)
  464. {
  465. // Happens when opening session in PuTTY and in other situations,
  466. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  467. // to process the command-line on its own (or to pass it to yet another instance)
  468. }
  469. if (DataList->Count > 0)
  470. {
  471. TTerminalManager * Manager = TTerminalManager::Instance();
  472. Manager->ActiveSession = Manager->NewSessions(DataList.get());
  473. }
  474. }
  475. __finally
  476. {
  477. NonVisualDataModule->EndBusy();
  478. }
  479. }
  480. return Result;
  481. }
  482. //---------------------------------------------------------------------------
  483. TManagedTerminal * TCustomScpExplorerForm::GetTerminal()
  484. {
  485. TManagedTerminal * Result;
  486. if ((FManagedSession != NULL) && !FManagedSession->LocalBrowser)
  487. {
  488. Result = FManagedSession;
  489. }
  490. else
  491. {
  492. Result = NULL;
  493. }
  494. return Result;
  495. }
  496. //---------------------------------------------------------------------------
  497. void __fastcall TCustomScpExplorerForm::SetManagedSession(TManagedTerminal * value)
  498. {
  499. if (FManagedSession != value)
  500. {
  501. SessionChanging();
  502. DoSetManagedSession(value, false);
  503. }
  504. }
  505. //---------------------------------------------------------------------------
  506. void __fastcall TCustomScpExplorerForm::DoSetManagedSession(TManagedTerminal * value, bool Replace)
  507. {
  508. DebugAssert(!Replace || ((value != NULL) && !value->LocalBrowser));
  509. FManagedSession = value;
  510. {
  511. TValueRestorer<bool> AllowTransferPresetAutoSelectRestorer(FAllowTransferPresetAutoSelect);
  512. FAllowTransferPresetAutoSelect = false;
  513. TAutoFlag SessionChangingFlag(FSessionChanging);
  514. SessionChanged(Replace);
  515. }
  516. if (Terminal != NULL)
  517. {
  518. TransferPresetAutoSelect();
  519. }
  520. // Update app and tab titles, prevented above by FSessionChanging.
  521. UpdateControls();
  522. }
  523. //---------------------------------------------------------------------------
  524. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  525. {
  526. DoSetManagedSession(value, true);
  527. }
  528. //---------------------------------------------------------------------------
  529. void __fastcall TCustomScpExplorerForm::SessionChanging()
  530. {
  531. if (ManagedSession != NULL)
  532. {
  533. UpdateSession(ManagedSession);
  534. }
  535. }
  536. //---------------------------------------------------------------------------
  537. void __fastcall TCustomScpExplorerForm::SessionChanged(bool Replaced)
  538. {
  539. if (Terminal != NULL)
  540. {
  541. UpdateSessionColor((TColor)Terminal->StateData->Color);
  542. }
  543. if (!TTerminalManager::Instance()->Updating)
  544. {
  545. SessionListChanged();
  546. }
  547. if (Terminal != NULL)
  548. {
  549. TObject * State = NULL;
  550. if (WinConfiguration->PreservePanelState)
  551. {
  552. State = Terminal->RemoteExplorerState;
  553. DirView(osRemote)->AnnounceState(State);
  554. }
  555. try
  556. {
  557. if (Replaced)
  558. {
  559. RemoteDirView->ReplaceTerminal(Terminal);
  560. }
  561. else
  562. {
  563. RemoteDirView->Terminal = Terminal;
  564. }
  565. if (Terminal->Active)
  566. {
  567. Terminal->RefreshDirectory();
  568. }
  569. if (State != NULL)
  570. {
  571. DirView(osRemote)->RestoreState(State);
  572. }
  573. }
  574. __finally
  575. {
  576. DirView(osRemote)->AnnounceState(NULL);
  577. }
  578. }
  579. else
  580. {
  581. RemoteDirView->Terminal = NULL;
  582. }
  583. InitStatusBar();
  584. UpdateTransferList();
  585. // Update panels Enable state before refreshing the labels
  586. UpdateControls();
  587. }
  588. //---------------------------------------------------------------------------
  589. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  590. {
  591. if (Queue != value)
  592. {
  593. if (FQueue != NULL)
  594. {
  595. FQueue->OnListUpdate = NULL;
  596. FQueue->OnQueueItemUpdate = NULL;
  597. }
  598. FQueue = value;
  599. if (FQueue != NULL)
  600. {
  601. DebugAssert(FQueue->OnListUpdate == NULL);
  602. FQueue->OnListUpdate = QueueListUpdate;
  603. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  604. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  605. }
  606. QueueChanged();
  607. }
  608. }
  609. //---------------------------------------------------------------------------
  610. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  611. TListItem * Item)
  612. {
  613. if (FQueueActedItem == Item)
  614. {
  615. FQueueActedItem = NULL;
  616. if ((QueueView3->PopupMenu != NULL) &&
  617. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  618. {
  619. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  620. ReleaseCapture();
  621. }
  622. }
  623. if (Item->Data == FPendingQueueActionItem)
  624. {
  625. FPendingQueueActionItem = NULL;
  626. }
  627. }
  628. //---------------------------------------------------------------------------
  629. bool TCustomScpExplorerForm::IsAnythingQueued()
  630. {
  631. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  632. }
  633. //---------------------------------------------------------------------------
  634. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  635. {
  636. {
  637. TGuard Guard(FQueueStatusSection);
  638. FQueueStatusInvalidated = false;
  639. if (FQueue != NULL)
  640. {
  641. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  642. }
  643. }
  644. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  645. {
  646. FMaxQueueLength = FQueueStatus->Count;
  647. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  648. }
  649. {
  650. TAutoFlag Flag(FQueueStatusUpdating);
  651. FQueueController->UpdateQueueStatus(FQueueStatus);
  652. }
  653. UpdateQueueFileList();
  654. SetQueueProgress();
  655. UpdateQueueView();
  656. bool IsEmpty = !IsAnythingQueued();
  657. if (IsEmpty && (Terminal != NULL))
  658. {
  659. TOnceDoneOperation OnceDoneOperation =
  660. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  661. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  662. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  663. {
  664. FQueue->Enabled = false;
  665. }
  666. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  667. {
  668. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  669. }
  670. }
  671. }
  672. //---------------------------------------------------------------------------
  673. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  674. {
  675. UnicodeString Result;
  676. if (FQueueStatus != NULL)
  677. {
  678. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  679. {
  680. TFileOperationProgressType * ProgressData =
  681. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  682. if ((ProgressData != NULL) && ProgressData->InProgress)
  683. {
  684. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  685. }
  686. }
  687. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  688. {
  689. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  690. }
  691. }
  692. return Result;
  693. }
  694. //---------------------------------------------------------------------------
  695. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  696. {
  697. ComponentVisible[fcQueueView] =
  698. !IsLocalBrowserMode() &&
  699. ((WinConfiguration->QueueView.Show == qvShow) ||
  700. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  701. (FQueueStatus != NULL) && (FQueueStatus->Count > 0)));
  702. }
  703. //---------------------------------------------------------------------------
  704. void __fastcall TCustomScpExplorerForm::QueueChanged()
  705. {
  706. if (FQueueStatus != NULL)
  707. {
  708. // UpdateFileList implementation relies on the status being recreated when session changes
  709. delete FQueueStatus;
  710. FQueueStatus = NULL;
  711. }
  712. UpdateQueueStatus(true);
  713. }
  714. //---------------------------------------------------------------------------
  715. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  716. {
  717. if (FQueue == Queue)
  718. {
  719. FQueueStatusInvalidated = true;
  720. }
  721. }
  722. //---------------------------------------------------------------------------
  723. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  724. TQueueItem * Item)
  725. {
  726. if (FQueue == Queue)
  727. {
  728. TGuard Guard(FQueueStatusSection);
  729. // this may be running in parallel with QueueChanged
  730. if (FQueueStatus != NULL)
  731. {
  732. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  733. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  734. {
  735. FRefreshLocalDirectory = (QueueItem == NULL) ||
  736. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  737. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  738. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  739. }
  740. if (QueueItem != NULL)
  741. {
  742. QueueItem->UserData = (void*)true;
  743. FQueueItemInvalidated = true;
  744. }
  745. }
  746. }
  747. }
  748. //---------------------------------------------------------------------------
  749. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  750. {
  751. // during standalone editing, we have no way to see/control queue,
  752. // so we have to always popup prompts automatically
  753. return StandaloneOperation || GUIConfiguration->QueueAutoPopup;
  754. }
  755. //---------------------------------------------------------------------------
  756. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  757. {
  758. if (FQueueStatus != NULL)
  759. {
  760. bool QueueAutoPopup = IsQueueAutoPopup();
  761. bool NeedRefresh = FQueueController->NeedRefresh();
  762. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  763. FQueueItemInvalidated = false;
  764. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  765. bool Updated = false;
  766. TQueueItemProxy * QueueItem;
  767. bool WasUserAction;
  768. for (int Index = 0; Index < Limit; Index++)
  769. {
  770. bool Update = false;
  771. QueueItem = FQueueStatus->Items[Index];
  772. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  773. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  774. (FPendingQueueActionItem == NULL))
  775. {
  776. FPendingQueueActionItem = QueueItem;
  777. FUserActionTimer->Enabled = true;
  778. }
  779. if ((bool)QueueItem->UserData)
  780. {
  781. QueueItem->UserData = (void*)false;
  782. QueueItem->Update();
  783. Updated = true;
  784. Update = true;
  785. }
  786. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  787. {
  788. Update = true;
  789. }
  790. if (Update)
  791. {
  792. FQueueController->RefreshQueueItem(QueueItem);
  793. }
  794. }
  795. if (Updated)
  796. {
  797. NonVisualDataModule->UpdateNonVisibleActions();
  798. SetQueueProgress();
  799. UpdateQueueFileList();
  800. }
  801. }
  802. }
  803. //---------------------------------------------------------------------------
  804. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  805. {
  806. if (FTaskbarList != NULL)
  807. {
  808. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  809. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  810. }
  811. }
  812. //---------------------------------------------------------------------------
  813. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  814. {
  815. if (Progress >= 0)
  816. {
  817. if (FTaskbarList != NULL)
  818. {
  819. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  820. }
  821. }
  822. else
  823. {
  824. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  825. }
  826. }
  827. //---------------------------------------------------------------------------
  828. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  829. {
  830. int OverallProgress;
  831. // FProgressForm is null when this is called from SetQueueProgress
  832. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  833. {
  834. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  835. }
  836. else if (!ProgressData->IsIndeterminate())
  837. {
  838. OverallProgress = ProgressData->OverallProgress();
  839. }
  840. else
  841. {
  842. OverallProgress = -1;
  843. }
  844. SetTaskbarListProgressValue(OverallProgress);
  845. }
  846. //---------------------------------------------------------------------------
  847. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  848. {
  849. TTerminalManager::Instance()->QueueStatusUpdated();
  850. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  851. {
  852. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  853. {
  854. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  855. {
  856. TFileOperationProgressType * ProgressData = NULL;
  857. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  858. {
  859. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  860. }
  861. if ((ProgressData != NULL) &&
  862. ProgressData->InProgress)
  863. {
  864. SetTaskbarListProgressValue(ProgressData);
  865. }
  866. else
  867. {
  868. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  869. }
  870. }
  871. else
  872. {
  873. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  874. }
  875. }
  876. else
  877. {
  878. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  879. }
  880. }
  881. UpdateQueueLabel();
  882. }
  883. //---------------------------------------------------------------------------
  884. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  885. {
  886. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  887. if (FQueueStatus != NULL)
  888. {
  889. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  890. if (ActiveAndPendingPrimaryCount > 0)
  891. {
  892. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  893. }
  894. }
  895. QueueLabel->Caption = Caption;
  896. }
  897. //---------------------------------------------------------------------------
  898. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  899. {
  900. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  901. static_cast<TComponent*>(GetComponent(fcTransferList)));
  902. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  903. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  904. TransferList->Strings->BeginUpdate();
  905. try
  906. {
  907. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  908. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  909. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  910. if (FTransferDropDownHint.IsEmpty())
  911. {
  912. FTransferDropDownHint = TransferDropDown->Hint;
  913. }
  914. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  915. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  916. TransferDropDown->Text = StripHotkey(Name);
  917. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  918. (FTransferDropDownHint, StripHotkey(Name),
  919. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  920. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General))));
  921. // update the label, otherwise when it is updated only on the first draw
  922. // of the list, it is drawn "bold" for some reason
  923. FTransferListHoverIndex = TransferList->ItemIndex;
  924. UpdateTransferLabel();
  925. }
  926. __finally
  927. {
  928. TransferList->Strings->EndUpdate();
  929. }
  930. }
  931. //---------------------------------------------------------------------------
  932. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  933. {
  934. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  935. DebugAssert(Toolbar != NULL);
  936. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  937. }
  938. //---------------------------------------------------------------------------
  939. void __fastcall TCustomScpExplorerForm::UpdateActions()
  940. {
  941. TForm::UpdateActions();
  942. if (ComponentVisible[fcCustomCommandsBand])
  943. {
  944. UpdateCustomCommandsToolbar();
  945. }
  946. }
  947. //---------------------------------------------------------------------------
  948. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  949. {
  950. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  951. }
  952. //---------------------------------------------------------------------------
  953. void __fastcall TCustomScpExplorerForm::UpdateRowSelect(TCustomDirView * DirView)
  954. {
  955. if (DirView->RowSelect != WinConfiguration->FullRowSelect)
  956. {
  957. DirView->RowSelect = WinConfiguration->FullRowSelect;
  958. // Without this, the panel turns gray and unused part of header turns black
  959. DirView->Perform(CM_COLORCHANGED, 0, 0);
  960. }
  961. }
  962. //---------------------------------------------------------------------------
  963. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  964. {
  965. Color = GetBtnFaceColor();
  966. DebugAssert(Configuration && RemoteDirView);
  967. RemoteDirView->DDAllowMove = !WinConfiguration->DDDisableMove;
  968. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  969. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  970. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  971. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  972. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  973. UpdateRowSelect(RemoteDirView);
  974. RemoteDriveView->DDAllowMove = !WinConfiguration->DDDisableMove;
  975. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  976. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  977. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  978. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  979. UpdateSessionsPageControlHeight();
  980. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  981. UpdateToolbarDisplayMode();
  982. UpdateTransferList();
  983. if (Terminal != NULL)
  984. {
  985. TransferPresetAutoSelect();
  986. }
  987. UpdateQueueView();
  988. UpdateControls();
  989. // this can be called even before constuctor finishes.
  990. if (FEditorManager != NULL)
  991. {
  992. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  993. }
  994. // this can be called even before constuctor finishes.
  995. if (FLocalEditors != NULL)
  996. {
  997. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  998. {
  999. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  1000. }
  1001. }
  1002. if (ComponentVisible[fcCustomCommandsBand])
  1003. {
  1004. UpdateCustomCommandsToolbar();
  1005. }
  1006. // show only when keeping queue items forever,
  1007. // otherwise, it's enough to have it in the context menu
  1008. QueueDeleteAllDoneQueueToolbarItem->Visible =
  1009. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  1010. if (FFileColorsCurrent != WinConfiguration->FileColors)
  1011. {
  1012. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  1013. FFileColorsCurrent = WinConfiguration->FileColors;
  1014. FileColorsChanged();
  1015. }
  1016. }
  1017. //---------------------------------------------------------------------------
  1018. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  1019. {
  1020. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  1021. DirView->Invalidate();
  1022. }
  1023. //---------------------------------------------------------------------------
  1024. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  1025. {
  1026. DoFileColorsChanged(RemoteDirView);
  1027. }
  1028. //---------------------------------------------------------------------------
  1029. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  1030. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  1031. void * /*Arg*/)
  1032. {
  1033. if (Token != NULL)
  1034. {
  1035. TForm * Editor = dynamic_cast<TForm*>(Token);
  1036. DebugAssert(Editor != NULL);
  1037. ReconfigureEditorForm(Editor);
  1038. }
  1039. }
  1040. //---------------------------------------------------------------------------
  1041. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  1042. {
  1043. WinConfiguration->DDTransferConfirmation = asOn;
  1044. }
  1045. //---------------------------------------------------------------------------
  1046. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1047. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1048. {
  1049. // noop
  1050. }
  1051. //---------------------------------------------------------------------------
  1052. bool TCustomScpExplorerForm::GetDoNotShowCopyDialogDefault(bool DragDrop)
  1053. {
  1054. return DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto);
  1055. }
  1056. //---------------------------------------------------------------------------
  1057. void TCustomScpExplorerForm::HandleDoNotShowCopyDialogAgain(bool DragDrop, bool DoNotShowAgain)
  1058. {
  1059. if (DoNotShowAgain)
  1060. {
  1061. if (DragDrop)
  1062. {
  1063. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1064. {
  1065. PopupTrayBalloon(
  1066. NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation, NULL, 0, EnableDDTransferConfirmation, NULL);
  1067. }
  1068. WinConfiguration->DDTransferConfirmation = asOff;
  1069. }
  1070. else
  1071. {
  1072. WinConfiguration->ConfirmTransferring = false;
  1073. }
  1074. }
  1075. else
  1076. {
  1077. // User explicitly unchecked "do not show again",
  1078. // so show him the dialog the next time
  1079. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1080. {
  1081. WinConfiguration->DDTransferConfirmation = asOn;
  1082. }
  1083. }
  1084. }
  1085. //---------------------------------------------------------------------------
  1086. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1087. TTransferDirection Direction, TTransferType Type, bool Temp,
  1088. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1089. bool Confirm, bool DragDrop, int Options)
  1090. {
  1091. bool Result = true;
  1092. DebugAssert(HasActiveTerminal());
  1093. // these parameters are known in advance
  1094. int Params =
  1095. FLAGMASK(Type == ttMove, cpDelete);
  1096. bool ToTemp = (Temp && (Direction == tdToLocal));
  1097. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1098. {
  1099. bool DisableNewerOnly =
  1100. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1101. ToTemp;
  1102. Options |=
  1103. FLAGMASK(ToTemp, coTemp) |
  1104. FLAGMASK(!Terminal->IsCapable[fcBackgroundTransfers], coDisableQueue) |
  1105. coDoNotShowAgain;
  1106. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1107. int CopyParamAttrs = (Direction == tdToRemote ?
  1108. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1109. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1110. int OutputOptions = FLAGMASK(GetDoNotShowCopyDialogDefault(DragDrop), cooDoNotShowAgain);
  1111. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1112. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1113. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1114. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1115. if (Result)
  1116. {
  1117. HandleDoNotShowCopyDialogAgain(DragDrop, FLAGSET(OutputOptions, cooDoNotShowAgain));
  1118. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1119. }
  1120. }
  1121. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fcBackgroundTransfers])
  1122. {
  1123. Configuration->Usage->Inc(L"TransfersOnBackground");
  1124. // these parameter are known only after transfer dialog
  1125. Params |=
  1126. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1127. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1128. Result = false;
  1129. ClearTransferSourceSelection(Direction);
  1130. }
  1131. return Result;
  1132. }
  1133. //---------------------------------------------------------------------------
  1134. void TCustomScpExplorerForm::ClearOperationSelection(TOperationSide Side)
  1135. {
  1136. if (FOnFileOperationFinished != NULL)
  1137. {
  1138. FOnFileOperationFinished(Side, UnicodeString(), true);
  1139. }
  1140. else
  1141. {
  1142. if (HasDirView[Side])
  1143. {
  1144. DirView(Side)->SelectAll(smNone);
  1145. }
  1146. }
  1147. }
  1148. //---------------------------------------------------------------------------
  1149. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1150. {
  1151. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1152. ClearOperationSelection(Side);
  1153. }
  1154. //---------------------------------------------------------------------------
  1155. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1156. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1157. const UnicodeString TargetDirectory, TGUICopyParamType & CopyParam,
  1158. int Params)
  1159. {
  1160. DebugAssert(Queue != NULL);
  1161. bool SingleFile = false;
  1162. if (FileList->Count == 1)
  1163. {
  1164. if (Direction == tdToRemote)
  1165. {
  1166. UnicodeString FileName = FileList->Strings[0];
  1167. SingleFile = FileExists(ApiPath(FileName));
  1168. }
  1169. else
  1170. {
  1171. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1172. SingleFile = !File->IsDirectory;
  1173. }
  1174. }
  1175. TQueueItem * QueueItem;
  1176. if (Direction == tdToRemote)
  1177. {
  1178. CopyParam.IncludeFileMask.SetRoots(FileList, TargetDirectory);
  1179. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1180. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1181. }
  1182. else
  1183. {
  1184. CopyParam.IncludeFileMask.SetRoots(TargetDirectory, FileList);
  1185. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1186. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1187. }
  1188. AddQueueItem(Queue, QueueItem, Terminal);
  1189. }
  1190. //---------------------------------------------------------------------------
  1191. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1192. {
  1193. if (Queue->IsEmpty)
  1194. {
  1195. Terminal->QueueOperationStart = Now();
  1196. }
  1197. Queue->AddItem(QueueItem);
  1198. }
  1199. //---------------------------------------------------------------------------
  1200. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1201. {
  1202. }
  1203. //---------------------------------------------------------------------------
  1204. void __fastcall TCustomScpExplorerForm::InitControls()
  1205. {
  1206. DebugAssert(FDocks != NULL);
  1207. for (int Index = 0; Index < ComponentCount; Index++)
  1208. {
  1209. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1210. if ((Dock != NULL) && (Dock->Tag == 0))
  1211. {
  1212. FDocks->Add(Dock);
  1213. }
  1214. }
  1215. CollectItemsWithTextDisplayMode(this);
  1216. }
  1217. //---------------------------------------------------------------------------
  1218. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1219. {
  1220. ConfigurationChanged();
  1221. QueuePanel->Height =
  1222. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1223. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1224. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1225. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1226. QueueFileList->Visible = WinConfiguration->QueueView.FileList;
  1227. QueueFileList->Height =
  1228. LoadDimension(WinConfiguration->QueueView.FileListHeight, WinConfiguration->QueueView.FileListHeightPixelsPerInch, this);
  1229. if (QueueFileList->Visible)
  1230. {
  1231. AdjustQueueLayout();
  1232. }
  1233. UpdateCustomCommandsToolbar();
  1234. }
  1235. //---------------------------------------------------------------------------
  1236. void __fastcall TCustomScpExplorerForm::StoreParams()
  1237. {
  1238. int APixelsPerInch = GetControlPixelsPerInch(this);
  1239. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1240. WinConfiguration->QueueView.HeightPixelsPerInch = APixelsPerInch;
  1241. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1242. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1243. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1244. WinConfiguration->QueueView.FileList = QueueFileList->Visible;
  1245. WinConfiguration->QueueView.FileListHeight = QueueFileList->Height;
  1246. WinConfiguration->QueueView.FileListHeightPixelsPerInch = APixelsPerInch;
  1247. }
  1248. //---------------------------------------------------------------------------
  1249. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1250. {
  1251. if (!FFormRestored)
  1252. {
  1253. FFormRestored = true;
  1254. RestoreFormParams();
  1255. }
  1256. TForm::CreateParams(Params);
  1257. }
  1258. //---------------------------------------------------------------------------
  1259. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1260. {
  1261. DebugAssert(FDocks != NULL);
  1262. for (int Index = 0; Index < FDocks->Count; Index++)
  1263. {
  1264. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1265. }
  1266. }
  1267. //---------------------------------------------------------------------------
  1268. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1269. {
  1270. SetDockAllowDrag(true);
  1271. while (!ButtonsStr.IsEmpty())
  1272. {
  1273. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1274. UnicodeString Name = CutToChar(S, L'=', true);
  1275. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1276. {
  1277. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1278. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1279. {
  1280. while (!S.IsEmpty())
  1281. {
  1282. UnicodeString S2 = CutToChar(S, L',', true);
  1283. UnicodeString Name = CutToChar(S2, L':', true);
  1284. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1285. {
  1286. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1287. if (SameText(GetToolbarItemName(Item), Name))
  1288. {
  1289. // forward compatibility
  1290. UnicodeString S3 = CutToChar(S2, L':', true);
  1291. bool Visible = StrToIntDef(S3, 0);
  1292. Item->Visible = Visible;
  1293. }
  1294. }
  1295. }
  1296. }
  1297. }
  1298. }
  1299. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1300. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1301. }
  1302. //---------------------------------------------------------------------------
  1303. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1304. {
  1305. UnicodeString Result;
  1306. SetDockAllowDrag(true);
  1307. Result = ::GetToolbarsLayoutStr(this);
  1308. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1309. return Result;
  1310. }
  1311. //---------------------------------------------------------------------------
  1312. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1313. {
  1314. UnicodeString Result;
  1315. if (Item->Action != NULL)
  1316. {
  1317. Result = Item->Action->Name;
  1318. Result = RemoveSuffix(Result, L"Action", true);
  1319. }
  1320. else
  1321. {
  1322. Result = Item->Name;
  1323. Result = RemoveSuffix(Result, L"SubmenuItem", true);
  1324. Result = RemoveSuffix(Result, L"Item", true);
  1325. }
  1326. return Result;
  1327. }
  1328. //---------------------------------------------------------------------------
  1329. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1330. {
  1331. UnicodeString Result;
  1332. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1333. {
  1334. UnicodeString ToolbarStr;
  1335. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1336. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1337. {
  1338. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1339. {
  1340. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1341. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1342. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1343. // and save the changes here only.
  1344. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1345. {
  1346. UnicodeString Name = GetToolbarItemName(Item);
  1347. DebugAssert(Name.Pos(L"TBX") == 0);
  1348. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1349. }
  1350. }
  1351. }
  1352. if (!ToolbarStr.IsEmpty())
  1353. {
  1354. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1355. }
  1356. }
  1357. return Result;
  1358. }
  1359. //---------------------------------------------------------------------------
  1360. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1361. {
  1362. DebugAssert(FProgressForm == NULL);
  1363. bool AllowSkip = HasActiveTerminal() ? Terminal->IsCapable[fcSkipTransfer] : false;
  1364. bool AllowMoveToQueue = (FTransferResumeList != NULL) || (FDeletedFiles != NULL);
  1365. FProgressForm = new TProgressForm(Application, AllowMoveToQueue, AllowSkip, SynchronizeProgress);
  1366. FProgressForm->DeleteLocalToRecycleBin =
  1367. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1368. FProgressForm->DeleteRemoteToRecycleBin =
  1369. HasActiveTerminal() &&
  1370. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1371. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1372. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1373. LockWindow();
  1374. // Actually, do not know what hides the progress once the operation finishes
  1375. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1376. SetTaskbarListProgressState(TBPF_NORMAL);
  1377. }
  1378. //---------------------------------------------------------------------------
  1379. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1380. {
  1381. UnlockWindow();
  1382. SAFE_DESTROY(FProgressForm);
  1383. SetQueueProgress();
  1384. }
  1385. //---------------------------------------------------------------------------
  1386. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1387. TFileOperationProgressType & ProgressData)
  1388. {
  1389. // Download to temporary local directory
  1390. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1391. {
  1392. bool Continue = true;
  1393. FPendingTempSpaceWarn = false;
  1394. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1395. if (!Continue)
  1396. {
  1397. Abort();
  1398. }
  1399. }
  1400. // operation is being executed and we still didn't show up progress form
  1401. if (ProgressData.InProgress && !FProgressForm)
  1402. {
  1403. CreateProgressForm(NULL);
  1404. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1405. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1406. }
  1407. // operation is finished (or terminated), so we hide progress form
  1408. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1409. {
  1410. DestroyProgressForm();
  1411. if (ProgressData.Operation == foCalculateSize)
  1412. {
  1413. // When calculating size before transfer, the abort caused by
  1414. // cancel flag set due to "MoveToQueue" below,
  1415. // is not propagated back to us in ExecuteFileOperation,
  1416. // as it is silently swallowed.
  1417. // So we explicitly (re)throw it here.
  1418. if (FMoveToQueue)
  1419. {
  1420. Abort();
  1421. }
  1422. }
  1423. else
  1424. {
  1425. if ((ProgressData.Cancel == csContinue) &&
  1426. !FAutoOperation)
  1427. {
  1428. OperationComplete(ProgressData.StartTime);
  1429. }
  1430. }
  1431. }
  1432. if (FProgressForm &&
  1433. // Can happen only during synchronization, when ending one of the operations
  1434. ProgressData.InProgress)
  1435. {
  1436. FProgressForm->SetProgressData(ProgressData);
  1437. DebugAssert(ProgressData.InProgress);
  1438. SetTaskbarListProgressValue(&ProgressData);
  1439. if (FProgressForm->Cancel > csContinue)
  1440. {
  1441. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1442. if (FProgressForm->Cancel == csCancelFile)
  1443. {
  1444. FProgressForm->ClearCancel();
  1445. }
  1446. // cancel cancels even the move
  1447. FMoveToQueue = false;
  1448. }
  1449. else if (FProgressForm->MoveToQueue)
  1450. {
  1451. FMoveToQueue = true;
  1452. ProgressData.SetCancelAtLeast(csCancel);
  1453. }
  1454. if ((FTransferResumeList != NULL) &&
  1455. ProgressData.InProgress &&
  1456. ProgressData.IsTransfer() &&
  1457. !ProgressData.FullFileName.IsEmpty())
  1458. {
  1459. if ((FTransferResumeList->Count == 0) ||
  1460. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1461. {
  1462. // note that we do not recognize directories from files here
  1463. FTransferResumeList->Add(ProgressData.FullFileName);
  1464. }
  1465. }
  1466. }
  1467. }
  1468. //---------------------------------------------------------------------------
  1469. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1470. const TDateTime & StartTime)
  1471. {
  1472. if (GUIConfiguration->BeepOnFinish &&
  1473. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1474. {
  1475. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1476. DWORD Sound;
  1477. if (!ExtractFileExt(BeepSound).IsEmpty())
  1478. {
  1479. Sound = SND_FILENAME;
  1480. }
  1481. else
  1482. {
  1483. Sound = SND_ALIAS;
  1484. }
  1485. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1486. }
  1487. }
  1488. //---------------------------------------------------------------------------
  1489. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1490. TFileOperationProgressType & ProgressData)
  1491. {
  1492. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1493. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1494. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1495. FileOperationProgress(ProgressData);
  1496. }
  1497. //---------------------------------------------------------------------------
  1498. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1499. {
  1500. UnicodeString Result;
  1501. if (FProgressForm != NULL)
  1502. {
  1503. Result = FProgressForm->ProgressStr();
  1504. }
  1505. return Result;
  1506. }
  1507. //---------------------------------------------------------------------------
  1508. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1509. bool DragDrop)
  1510. {
  1511. return (!DragDrop && (DropSourceControl == NULL)) ||
  1512. (DropSourceControl == DirView(osOther));
  1513. }
  1514. //---------------------------------------------------------------------------
  1515. TListItem * TCustomScpExplorerForm::VisualiseOperationFinished(TOperationSide Side, const UnicodeString & FileName, bool Unselect)
  1516. {
  1517. TCustomDirView * ADirView = DirView(Side);
  1518. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1519. TListItem * Item = ADirView->FindFileItem(FileNameOnly);
  1520. // this can happen when local drive is unplugged in the middle of the operation
  1521. if (Item != NULL)
  1522. {
  1523. if (Unselect)
  1524. {
  1525. Item->Selected = false;
  1526. }
  1527. ADirView->MakeProgressVisible(Item);
  1528. }
  1529. return Item;
  1530. }
  1531. //---------------------------------------------------------------------------
  1532. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1533. TFileOperation Operation, TOperationSide Side,
  1534. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1535. TOnceDoneOperation & OnceDoneOperation)
  1536. {
  1537. if (!FAutoOperation)
  1538. {
  1539. // no selection on "/upload", form serves only as event handler
  1540. // (it is not displayed)
  1541. if (PanelOperation(Side, FDragDropOperation) && Visible &&
  1542. ((Operation == FPrimaryOperation) ||
  1543. ((Operation != foCalculateSize) && (Operation != foGetProperties) && (Operation != foCalculateChecksum))))
  1544. {
  1545. TOperationSide DViewSide;
  1546. if ((FProgressSide != osCurrent) &&
  1547. IsLocalBrowserMode() && (Side == osLocal)) // Only to limit the impact
  1548. {
  1549. // assume the operation is over the focused panel
  1550. DViewSide = FProgressSide;
  1551. }
  1552. else
  1553. {
  1554. DViewSide = Side;
  1555. }
  1556. if (FOnFileOperationFinished != NULL)
  1557. {
  1558. FOnFileOperationFinished(DViewSide, FileName, Success);
  1559. }
  1560. else
  1561. {
  1562. VisualiseOperationFinished(DViewSide, FileName, Success);
  1563. }
  1564. }
  1565. if (TFileOperationProgressType::IsTransferOperation(Operation))
  1566. {
  1567. if (DebugAlwaysTrue(!IsLocalBrowserMode()))
  1568. {
  1569. if (Side == osLocal)
  1570. {
  1571. Configuration->Usage->Inc(L"UploadedFiles");
  1572. }
  1573. else
  1574. {
  1575. Configuration->Usage->Inc(L"DownloadedFiles");
  1576. }
  1577. }
  1578. }
  1579. }
  1580. if (Success && (FSynchronizeController != NULL))
  1581. {
  1582. DebugAssert(!IsSideLocalBrowser(osRemote));
  1583. if (Operation == foCopy)
  1584. {
  1585. DebugAssert(Side == osLocal);
  1586. FSynchronizeController->LogOperation(soUpload, FileName);
  1587. }
  1588. else if (Operation == foDelete)
  1589. {
  1590. DebugAssert(Side == osRemote);
  1591. FSynchronizeController->LogOperation(soDelete, FileName);
  1592. }
  1593. }
  1594. if (FProgressForm)
  1595. {
  1596. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1597. }
  1598. }
  1599. //---------------------------------------------------------------------------
  1600. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1601. TFileOperation Operation, TOperationSide Side,
  1602. bool Temp, const UnicodeString & FileName, Boolean Success,
  1603. TOnceDoneOperation & OnceDoneOperation)
  1604. {
  1605. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1606. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1607. OnceDoneOperation);
  1608. }
  1609. //---------------------------------------------------------------------------
  1610. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1611. {
  1612. return false;
  1613. }
  1614. //---------------------------------------------------------------------------
  1615. bool TCustomScpExplorerForm::SupportsLocalBrowser()
  1616. {
  1617. return false;
  1618. }
  1619. //---------------------------------------------------------------------------
  1620. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1621. {
  1622. return false;
  1623. }
  1624. //---------------------------------------------------------------------------
  1625. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1626. {
  1627. return NULL;
  1628. }
  1629. //---------------------------------------------------------------------------
  1630. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1631. {
  1632. DebugAssert(GetSide(Side) == osRemote);
  1633. DebugUsedParam(Side);
  1634. return RemoteDirView;
  1635. }
  1636. //---------------------------------------------------------------------------
  1637. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1638. {
  1639. DebugAssert(GetSide(Side) == osRemote);
  1640. DebugUsedParam(Side);
  1641. return RemoteDriveView;
  1642. }
  1643. //---------------------------------------------------------------------------
  1644. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1645. {
  1646. DebugAssert(GetSide(Side) == osRemote);
  1647. DebugUsedParam(Side);
  1648. return HasActiveTerminal();
  1649. }
  1650. //---------------------------------------------------------------------------
  1651. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1652. TOperationSide Side, int FilesOnly)
  1653. {
  1654. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1655. }
  1656. //---------------------------------------------------------------------------
  1657. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1658. TOperationSide Side, int FilesOnly)
  1659. {
  1660. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1661. }
  1662. //---------------------------------------------------------------------------
  1663. struct THistoryItemData
  1664. {
  1665. short int Side;
  1666. short int Index;
  1667. };
  1668. //---------------------------------------------------------------------------
  1669. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1670. {
  1671. DirView(Side)->HistoryGo(Index);
  1672. }
  1673. //---------------------------------------------------------------------------
  1674. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1675. {
  1676. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1677. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1678. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1679. }
  1680. //---------------------------------------------------------------------------
  1681. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1682. bool Back)
  1683. {
  1684. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1685. {
  1686. TCustomDirView * DView = DirView(Side);
  1687. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1688. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1689. if (ICount > 10)
  1690. {
  1691. ICount = 10;
  1692. }
  1693. Menu->Items->Clear();
  1694. THistoryItemData Data;
  1695. Data.Side = Side;
  1696. for (short int i = 1; i <= ICount; i++)
  1697. {
  1698. TTBCustomItem * Item = new TTBXItem(Menu);
  1699. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1700. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1701. Item->Hint = DView->HistoryPath[Data.Index];
  1702. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1703. Item->Tag = *reinterpret_cast<int*>(&Data);
  1704. Item->OnClick = HistoryItemClick;
  1705. Menu->Items->Add(Item);
  1706. }
  1707. }
  1708. }
  1709. //---------------------------------------------------------------------------
  1710. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1711. TOperationSide Side, bool Back)
  1712. {
  1713. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1714. {
  1715. // workaround
  1716. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1717. // operator new allocates memory in Pascal code, but calls inline
  1718. // contructor in C++, leading in problems, the function does
  1719. // both in Pascal code
  1720. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1721. UpdateHistoryMenu(Side, Back);
  1722. }
  1723. return FHistoryMenu[Side == osLocal][Back];
  1724. }
  1725. //---------------------------------------------------------------------------
  1726. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1727. TCustomDirView *Sender)
  1728. {
  1729. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1730. UpdateHistoryMenu(Side, true);
  1731. UpdateHistoryMenu(Side, false);
  1732. }
  1733. //---------------------------------------------------------------------------
  1734. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1735. {
  1736. // remote custom commands can be executed only if the server supports shell commands
  1737. // or have secondary shell
  1738. return HasActiveTerminal() && (Terminal->IsCapable[fcSecondaryShell] || Terminal->IsCapable[fcShellAnyCommand]);
  1739. }
  1740. //---------------------------------------------------------------------------
  1741. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1742. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1743. {
  1744. // -1 = hidden, 0 = disabled, 1 = enabled
  1745. int Result;
  1746. TFileCustomCommand RemoteCustomCommand;
  1747. TLocalCustomCommand LocalCustomCommand;
  1748. TFileCustomCommand * NonInteractiveCustomCommand =
  1749. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1750. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1751. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1752. if (FLAGCLEAR(Command.Params, ccLocal))
  1753. {
  1754. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1755. // custom command that does not operate with files can be executed anytime ...
  1756. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1757. {
  1758. // ... but do not show such command in remote file menu
  1759. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1760. {
  1761. Result = AllowedState;
  1762. }
  1763. else
  1764. {
  1765. Result = -1;
  1766. }
  1767. }
  1768. else
  1769. {
  1770. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1771. {
  1772. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1773. }
  1774. else
  1775. {
  1776. Result = -1;
  1777. }
  1778. }
  1779. }
  1780. else
  1781. {
  1782. // custom command that does not operate with files can be executed anytime
  1783. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1784. {
  1785. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1786. }
  1787. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1788. {
  1789. if ((ListType == ccltAll) || (ListType == ccltFile))
  1790. {
  1791. // special case is "diff"-style command that can be executed over any side,
  1792. // if we have both sides
  1793. Result =
  1794. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1795. // directly (instead of EnableSelectedOperation) to pass
  1796. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1797. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1798. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1799. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1800. }
  1801. else if (ListType == ccltBoth)
  1802. {
  1803. DebugAssert(!IsLocalBrowserMode());
  1804. Result = 1;
  1805. }
  1806. else
  1807. {
  1808. Result = -1;
  1809. }
  1810. }
  1811. else
  1812. {
  1813. if ((ListType == ccltAll) ||
  1814. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1815. {
  1816. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1817. }
  1818. else
  1819. {
  1820. Result = -1;
  1821. }
  1822. }
  1823. if ((Result > 0) &&
  1824. LocalCustomCommand.IsSessionCommand(Cmd) &&
  1825. !HasActiveTerminal())
  1826. {
  1827. Result = 0;
  1828. }
  1829. }
  1830. return Result;
  1831. }
  1832. //---------------------------------------------------------------------------
  1833. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1834. TStrings * FileList, const TCustomCommandType & ACommand,
  1835. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1836. {
  1837. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1838. {
  1839. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1840. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1841. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1842. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1843. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1844. bool Capture =
  1845. FLAGSET(ACommand.Params, ccShowResults) ||
  1846. FLAGSET(ACommand.Params, ccCopyResults) ||
  1847. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1848. TCaptureOutputEvent OutputEvent = NULL;
  1849. DebugAssert(FCapturedLog == NULL);
  1850. if (Capture)
  1851. {
  1852. FCapturedLog = new TStringList();
  1853. OutputEvent = TerminalCaptureLog;
  1854. }
  1855. try
  1856. {
  1857. if (!RemoteCustomCommand.IsFileCommand(Command))
  1858. {
  1859. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1860. OutputEvent);
  1861. }
  1862. else
  1863. {
  1864. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1865. }
  1866. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1867. {
  1868. if (FLAGSET(ACommand.Params, ccCopyResults))
  1869. {
  1870. CopyToClipboard(FCapturedLog);
  1871. }
  1872. if (FLAGSET(ACommand.Params, ccShowResults))
  1873. {
  1874. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1875. }
  1876. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1877. {
  1878. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1879. }
  1880. }
  1881. }
  1882. __finally
  1883. {
  1884. SAFE_DESTROY(FCapturedLog);
  1885. }
  1886. }
  1887. }
  1888. //---------------------------------------------------------------------------
  1889. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1890. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1891. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1892. {
  1893. DebugAssert(!IsLocalBrowserMode());
  1894. TStrings * LocalFileList = NULL;
  1895. TStrings * RemoteFileList = NULL;
  1896. TStrings * RemoteFileListFull = NULL;
  1897. try
  1898. {
  1899. if (LocalFileCommand)
  1900. {
  1901. if (ALocalFileList == NULL)
  1902. {
  1903. DebugAssert(HasDirView[osLocal]);
  1904. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1905. // directly (instead of EnableSelectedOperation) to pass
  1906. // false to FocusedFileOnlyWhenFocused
  1907. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1908. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1909. }
  1910. else
  1911. {
  1912. LocalFileList = ALocalFileList;
  1913. }
  1914. if (FileListCommand)
  1915. {
  1916. if (LocalFileList->Count != 1)
  1917. {
  1918. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1919. }
  1920. }
  1921. else
  1922. {
  1923. if ((LocalFileList->Count != 1) &&
  1924. (FileList->Count != 1) &&
  1925. (LocalFileList->Count != FileList->Count))
  1926. {
  1927. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1928. }
  1929. }
  1930. }
  1931. UnicodeString RootTempDir;
  1932. UnicodeString TempDir;
  1933. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1934. if (!RemoteFiles)
  1935. {
  1936. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, true);
  1937. }
  1938. try
  1939. {
  1940. TDateTimes RemoteFileTimes;
  1941. if (RemoteFiles)
  1942. {
  1943. RemoteFileList = FileList;
  1944. }
  1945. else
  1946. {
  1947. RemoteFileList = new TStringList();
  1948. TMakeLocalFileListParams MakeFileListParam;
  1949. MakeFileListParam.FileList = RemoteFileList;
  1950. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1951. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1952. MakeFileListParam.Recursive =
  1953. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1954. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1955. if (!MakeFileListParam.Recursive)
  1956. {
  1957. RemoteFileTimes.clear();
  1958. RemoteFileListFull = new TStringList();
  1959. MakeFileListParam.FileList = RemoteFileListFull;
  1960. MakeFileListParam.Recursive = true;
  1961. MakeFileListParam.IncludeDirs = false;
  1962. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1963. }
  1964. else
  1965. {
  1966. RemoteFileListFull = RemoteFileList;
  1967. }
  1968. }
  1969. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1970. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1971. if (!NonBlocking)
  1972. {
  1973. int Count;
  1974. if (FileListCommand)
  1975. {
  1976. Count = 1;
  1977. }
  1978. else if (LocalFileCommand)
  1979. {
  1980. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  1981. }
  1982. else
  1983. {
  1984. Count = RemoteFileList->Count;
  1985. }
  1986. Progress.Start(foCustomCommand, osRemote, Count);
  1987. DebugAssert(FProgressForm != NULL);
  1988. FProgressForm->ReadOnly = true;
  1989. }
  1990. try
  1991. {
  1992. if (FileListCommand)
  1993. {
  1994. UnicodeString LocalFile;
  1995. // MakeFileList does not delimit filenames
  1996. UnicodeString FileList = MakeFileList(RemoteFileList);
  1997. if (LocalFileCommand)
  1998. {
  1999. DebugAssert(LocalFileList->Count == 1);
  2000. LocalFile = LocalFileList->Strings[0];
  2001. }
  2002. TLocalCustomCommand CustomCommand(Data,
  2003. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  2004. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  2005. if (NonBlocking)
  2006. {
  2007. DebugAssert(!POutput);
  2008. ExecuteShellChecked(ShellCommand);
  2009. }
  2010. else
  2011. {
  2012. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  2013. }
  2014. }
  2015. else if (LocalFileCommand)
  2016. {
  2017. if (LocalFileList->Count == 1)
  2018. {
  2019. UnicodeString LocalFile = LocalFileList->Strings[0];
  2020. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2021. {
  2022. UnicodeString FileName = RemoteFileList->Strings[Index];
  2023. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2024. Progress.SetFile(FileNameForProgress);
  2025. TLocalCustomCommand CustomCommand(Data,
  2026. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  2027. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2028. TOnceDoneOperation OnceDoneOperation;
  2029. // Do not unselect anything, as it won't work with two panels anyway
  2030. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2031. }
  2032. }
  2033. else if (RemoteFileList->Count == 1)
  2034. {
  2035. UnicodeString FileName = RemoteFileList->Strings[0];
  2036. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2037. {
  2038. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2039. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2040. Progress.SetFile(LocalFileNameOnly);
  2041. TLocalCustomCommand CustomCommand(
  2042. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2043. FileName, LocalFileName, L"");
  2044. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2045. TOnceDoneOperation OnceDoneOperation;
  2046. // Do not unselect anything, as it won't work with two panels anyway
  2047. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2048. }
  2049. }
  2050. else
  2051. {
  2052. if (LocalFileList->Count != RemoteFileList->Count)
  2053. {
  2054. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  2055. }
  2056. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2057. {
  2058. UnicodeString FileName = RemoteFileList->Strings[Index];
  2059. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2060. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2061. Progress.SetFile(LocalFileNameOnly); // sic
  2062. TLocalCustomCommand CustomCommand(
  2063. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2064. FileName, LocalFileName, L"");
  2065. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2066. TOnceDoneOperation OnceDoneOperation;
  2067. // Do not unselect anything, as it won't work with two panels anyway
  2068. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2069. }
  2070. }
  2071. }
  2072. else
  2073. {
  2074. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2075. {
  2076. UnicodeString FileName = RemoteFileList->Strings[Index];
  2077. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2078. Progress.SetFile(FileNameForProgress);
  2079. TLocalCustomCommand CustomCommand(Data,
  2080. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2081. FileName, L"", L"");
  2082. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2083. TOnceDoneOperation OnceDoneOperation;
  2084. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  2085. }
  2086. }
  2087. }
  2088. __finally
  2089. {
  2090. if (!NonBlocking)
  2091. {
  2092. Progress.Stop();
  2093. }
  2094. }
  2095. DebugAssert(!FAutoOperation);
  2096. if (!RemoteFiles)
  2097. {
  2098. TempDir = IncludeTrailingBackslash(TempDir);
  2099. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  2100. {
  2101. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  2102. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  2103. // Skip directories, process only nested files.
  2104. // The check is redundant as FileAge fails for directories anyway.
  2105. !DirectoryExists(FileName))
  2106. {
  2107. UnicodeString RemoteDir =
  2108. UnixExtractFileDir(
  2109. UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
  2110. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2111. TDateTime NewTime;
  2112. if (FileAge(FileName, NewTime) &&
  2113. (NewTime != RemoteFileTimes[Index]))
  2114. {
  2115. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2116. TemporaryFileCopyParam(CopyParam);
  2117. CopyParam.FileMask = L"";
  2118. FAutoOperation = true;
  2119. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2120. TemporaryFilesList->Add(FileName);
  2121. Terminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2122. }
  2123. }
  2124. }
  2125. }
  2126. }
  2127. __finally
  2128. {
  2129. FAutoOperation = false;
  2130. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2131. {
  2132. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2133. }
  2134. }
  2135. }
  2136. __finally
  2137. {
  2138. if (RemoteFileList != FileList)
  2139. {
  2140. delete RemoteFileList;
  2141. }
  2142. if (LocalFileList != ALocalFileList)
  2143. {
  2144. delete LocalFileList;
  2145. }
  2146. if (RemoteFileListFull != RemoteFileList)
  2147. {
  2148. delete RemoteFileListFull;
  2149. }
  2150. }
  2151. }
  2152. //---------------------------------------------------------------------------
  2153. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2154. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2155. bool FileListCommand, UnicodeString * POutput)
  2156. {
  2157. std::unique_ptr<TStrings> SelectedFileList(DirView(osCurrent)->CreateFileList(false, true, NULL));
  2158. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2159. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2160. {
  2161. UnicodeString FileName = SelectedFileList->Strings[Index];
  2162. if (DirectoryExists(FileName))
  2163. {
  2164. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2165. {
  2166. LocalFileList->Add(FileName);
  2167. }
  2168. if (FLAGSET(ACommand.Params, ccRecursive))
  2169. {
  2170. TMakeLocalFileListParams MakeFileListParam;
  2171. MakeFileListParam.FileList = LocalFileList.get();
  2172. MakeFileListParam.FileTimes = NULL;
  2173. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2174. MakeFileListParam.Recursive = true;
  2175. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2176. }
  2177. }
  2178. else
  2179. {
  2180. LocalFileList->Add(FileName);
  2181. }
  2182. }
  2183. UnicodeString RemotePath;
  2184. if (Terminal != NULL)
  2185. {
  2186. RemotePath = Terminal->CurrentDirectory;
  2187. }
  2188. if (FileListCommand)
  2189. {
  2190. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2191. TLocalCustomCommand CustomCommand(
  2192. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2193. L"", L"", FileList);
  2194. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2195. }
  2196. else
  2197. {
  2198. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2199. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2200. DebugAssert(FProgressForm != NULL);
  2201. FProgressForm->ReadOnly = true;
  2202. try
  2203. {
  2204. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2205. {
  2206. UnicodeString FileName = LocalFileList->Strings[Index];
  2207. Progress.SetFile(FileName);
  2208. TLocalCustomCommand CustomCommand(
  2209. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2210. FileName, L"", L"");
  2211. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2212. TOnceDoneOperation OnceDoneOperation;
  2213. Progress.Finish(FileName, true, OnceDoneOperation);
  2214. }
  2215. }
  2216. __finally
  2217. {
  2218. Progress.Stop();
  2219. }
  2220. }
  2221. }
  2222. //---------------------------------------------------------------------------
  2223. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2224. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2225. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2226. {
  2227. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2228. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2229. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2230. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2231. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2232. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2233. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2234. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2235. std::unique_ptr<UnicodeString> POutput;
  2236. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2237. FLAGSET(ACommand.Params, ccCopyResults))
  2238. {
  2239. POutput.reset(new UnicodeString());
  2240. }
  2241. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  2242. FProgressSide = FCurrentSide;
  2243. if (!LocalCustomCommand.IsFileCommand(Command))
  2244. {
  2245. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2246. }
  2247. // remote files?
  2248. else if (!IsSideLocalBrowser(FCurrentSide) || LocalFileCommand)
  2249. {
  2250. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2251. }
  2252. // local files
  2253. else
  2254. {
  2255. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2256. }
  2257. if (POutput.get() != NULL)
  2258. {
  2259. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2260. int P = POutput->Pos(sLineBreak);
  2261. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2262. {
  2263. POutput->SetLength(P - 1);
  2264. }
  2265. // Copy even empty output to clipboard
  2266. if (FLAGSET(ACommand.Params, ccCopyResults))
  2267. {
  2268. CopyToClipboard(*POutput);
  2269. }
  2270. // But do not show an empty message box.
  2271. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2272. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2273. !POutput->IsEmpty())
  2274. {
  2275. TClipboardHandler ClipboardHandler;
  2276. ClipboardHandler.Text = *POutput;
  2277. TMessageParams Params;
  2278. TQueryButtonAlias Aliases[1];
  2279. Aliases[0].Button = qaRetry;
  2280. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2281. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2282. Params.Aliases = Aliases;
  2283. Params.AliasesCount = LENOF(Aliases);
  2284. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2285. }
  2286. }
  2287. }
  2288. //---------------------------------------------------------------------------
  2289. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2290. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2291. {
  2292. TCustomCommandData Data;
  2293. UnicodeString Site;
  2294. UnicodeString RemotePath;
  2295. if (HasActiveTerminal())
  2296. {
  2297. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  2298. Data = TCustomCommandData(SessionData.get());
  2299. Site = Terminal->SessionData->SessionKey;
  2300. RemotePath = Terminal->CurrentDirectory;
  2301. }
  2302. UnicodeString HelpKeyword = ACommand.HomePage;
  2303. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2304. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2305. {
  2306. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2307. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2308. {
  2309. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2310. }
  2311. else
  2312. {
  2313. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2314. }
  2315. if (!DoCustomCommandOptionsDialog(
  2316. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2317. {
  2318. Abort();
  2319. }
  2320. }
  2321. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2322. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2323. {
  2324. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2325. }
  2326. else
  2327. {
  2328. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2329. }
  2330. }
  2331. //---------------------------------------------------------------------------
  2332. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2333. const TCustomCommandType & Command)
  2334. {
  2335. DebugAssert(FCustomCommandLocalFileList != NULL);
  2336. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2337. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2338. TStrings * LocalFileList = new TStringList();
  2339. TStrings * RemoteFileList = new TStringList();
  2340. try
  2341. {
  2342. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2343. {
  2344. LocalFileList->Clear();
  2345. LocalFileList->AddObject(
  2346. FCustomCommandLocalFileList->Strings[Index],
  2347. FCustomCommandLocalFileList->Objects[Index]);
  2348. RemoteFileList->Clear();
  2349. RemoteFileList->AddObject(
  2350. FCustomCommandRemoteFileList->Strings[Index],
  2351. FCustomCommandRemoteFileList->Objects[Index]);
  2352. CustomCommand(RemoteFileList, Command, LocalFileList);
  2353. }
  2354. }
  2355. __finally
  2356. {
  2357. delete LocalFileList;
  2358. delete RemoteFileList;
  2359. }
  2360. }
  2361. //---------------------------------------------------------------------------
  2362. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2363. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2364. {
  2365. delete FCustomCommandLocalFileList;
  2366. delete FCustomCommandRemoteFileList;
  2367. // takeover ownership,
  2368. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2369. FCustomCommandLocalFileList = LocalFileList;
  2370. FCustomCommandRemoteFileList = RemoteFileList;
  2371. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2372. if (Button != NULL)
  2373. {
  2374. FCustomCommandMenu->Items->Clear();
  2375. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2376. MenuPopup(FCustomCommandMenu, Button);
  2377. }
  2378. else
  2379. {
  2380. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2381. }
  2382. }
  2383. //---------------------------------------------------------------------------
  2384. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2385. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2386. {
  2387. DebugAssert(FCapturedLog != NULL);
  2388. if ((OutputType == cotOutput) || (OutputType == cotError))
  2389. {
  2390. FCapturedLog->Add(AddedLine);
  2391. }
  2392. }
  2393. //---------------------------------------------------------------------------
  2394. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2395. TOperationSide Side)
  2396. {
  2397. return
  2398. (Side == osOther) &&
  2399. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2400. }
  2401. //---------------------------------------------------------------------------
  2402. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2403. TOperationSide Side, TTBXCustomItem * Item,
  2404. TResolvedDoubleClickAction DoubleClickAction1, TResolvedDoubleClickAction DoubleClickAction2)
  2405. {
  2406. TCustomDirView * DView = DirView(Side);
  2407. bool IsDefault = false;
  2408. if (DView->ItemFocused != NULL)
  2409. {
  2410. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  2411. TResolvedDoubleClickAction DefaultAction =
  2412. WinConfiguration->ResolveDoubleClickAction(DView->ItemIsDirectory(DView->ItemFocused), ATerminal);
  2413. IsDefault = (DefaultAction == DoubleClickAction1) || (DefaultAction == DoubleClickAction2);
  2414. }
  2415. TTBItemOptions O = Item->Options;
  2416. if (IsDefault)
  2417. {
  2418. Item->Options = O << tboDefault;
  2419. }
  2420. else
  2421. {
  2422. Item->Options = O >> tboDefault;
  2423. }
  2424. }
  2425. //---------------------------------------------------------------------------
  2426. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2427. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2428. {
  2429. TCustomDirView * DView = DirView(Side);
  2430. TListItem * Item = DView->ItemFocused;
  2431. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2432. EnableFocusedOperation[Side])
  2433. {
  2434. TPoint ClientPoint;
  2435. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2436. TPoint(0, 0) : MousePos;
  2437. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2438. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2439. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2440. }
  2441. }
  2442. //---------------------------------------------------------------------------
  2443. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2444. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2445. {
  2446. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, rdcaChangeDir, rdcaOpen);
  2447. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, rdcaEdit, rdcaNone);
  2448. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, rdcaCopy, rdcaNone);
  2449. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2450. Handled = true;
  2451. }
  2452. //---------------------------------------------------------------------------
  2453. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2454. {
  2455. }
  2456. //---------------------------------------------------------------------------
  2457. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2458. {
  2459. DebugAssert(FErrorList == NULL);
  2460. if (WinConfiguration->ContinueOnError)
  2461. {
  2462. FErrorList = new TStringList();
  2463. Configuration->Usage->Inc(L"ContinuationsOnError");
  2464. }
  2465. NonVisualDataModule->StartBusy();
  2466. }
  2467. //---------------------------------------------------------------------------
  2468. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2469. {
  2470. NonVisualDataModule->EndBusy();
  2471. if (FErrorList)
  2472. {
  2473. HandleErrorList(FErrorList);
  2474. }
  2475. }
  2476. //---------------------------------------------------------------------------
  2477. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2478. const TCopyParamType & CopyParam)
  2479. {
  2480. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2481. {
  2482. Configuration->Usage->Inc(L"FileMaskUses");
  2483. }
  2484. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2485. {
  2486. Configuration->Usage->Inc(L"OperationMaskUses");
  2487. }
  2488. }
  2489. //---------------------------------------------------------------------------
  2490. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2491. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2492. {
  2493. DebugAssert(!IsLocalBrowserMode());
  2494. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2495. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2496. TTransferOperationParam DefaultParam;
  2497. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2498. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2499. switch (Param.Queue)
  2500. {
  2501. case asOn:
  2502. CopyParam.Queue = true;
  2503. break;
  2504. case asOff:
  2505. CopyParam.Queue = false;
  2506. break;
  2507. case asAuto:
  2508. default:
  2509. // keep default
  2510. break;
  2511. }
  2512. bool Result =
  2513. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2514. if (Result)
  2515. {
  2516. DebugAssert(Terminal);
  2517. bool SelectionRestored = false;
  2518. TCustomDirView * DView = NULL;
  2519. if (HasDirView[Side])
  2520. {
  2521. DView = DirView(Side);
  2522. DView->SaveSelection();
  2523. DView->SaveSelectedNames();
  2524. }
  2525. UpdateCopyParamCounters(CopyParam);
  2526. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2527. DebugAssert(FTransferResumeList == NULL);
  2528. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2529. FMoveToQueue = false;
  2530. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2531. try
  2532. {
  2533. TStrings * PermanentFileList;
  2534. std::unique_ptr<TStrings> PermanentFileListOwner;
  2535. try
  2536. {
  2537. if (Side == osLocal)
  2538. {
  2539. PermanentFileList = FileList;
  2540. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2541. CopyParam.IncludeFileMask.SetRoots(FileList, Param.TargetDirectory);
  2542. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2543. if (Operation == foMove)
  2544. {
  2545. ReloadLocalDirectory();
  2546. if (DView != NULL)
  2547. {
  2548. DView->RestoreSelection();
  2549. }
  2550. SelectionRestored = true;
  2551. }
  2552. }
  2553. else
  2554. {
  2555. // Clone the file list as it may refer to current directory files,
  2556. // which get destroyed, when the source directory is reloaded after foMove operation.
  2557. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2558. // But for a hotfix, we are not going to do such a big change.
  2559. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2560. PermanentFileList = PermanentFileListOwner.get();
  2561. try
  2562. {
  2563. CopyParam.IncludeFileMask.SetRoots(Param.TargetDirectory, FileList);
  2564. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2565. }
  2566. __finally
  2567. {
  2568. if (Operation == foMove)
  2569. {
  2570. if (DView != NULL)
  2571. {
  2572. DView->RestoreSelection();
  2573. }
  2574. SelectionRestored = true;
  2575. }
  2576. ReloadLocalDirectory(Param.TargetDirectory);
  2577. }
  2578. }
  2579. // When the transfer batch is aborted between individual file transfers, the Abort is not called.
  2580. if (FMoveToQueue)
  2581. {
  2582. Abort();
  2583. }
  2584. }
  2585. catch (EAbort &)
  2586. {
  2587. if (FMoveToQueue)
  2588. {
  2589. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2590. DebugAssert(CopyParam.TransferSkipList == NULL);
  2591. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2592. if (TransferResumeList->Count > 0)
  2593. {
  2594. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2595. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2596. }
  2597. CopyParam.TransferSkipList = TransferResumeList.release();
  2598. // not really needed, just to keep it consistent with TransferResumeList
  2599. FTransferResumeList = NULL;
  2600. FMoveToQueue = false;
  2601. Configuration->Usage->Inc("MovesToBackground");
  2602. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2603. ClearTransferSourceSelection(Direction);
  2604. }
  2605. throw;
  2606. }
  2607. }
  2608. __finally
  2609. {
  2610. if (!SelectionRestored && (DView != NULL))
  2611. {
  2612. DView->DiscardSavedSelection();
  2613. }
  2614. FTransferResumeList = NULL;
  2615. }
  2616. }
  2617. return Result;
  2618. }
  2619. //---------------------------------------------------------------------------
  2620. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2621. TOperationSide Side, TStrings * FileList, void * Param)
  2622. {
  2623. DebugAssert(FileList->Count);
  2624. // We deliberately do not toggle alternative flag (Param), but use OR,
  2625. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2626. // shortcut of CurrentDeleteAlternativeAction
  2627. bool Alternative =
  2628. bool(Param) || UseAlternativeFunction();
  2629. bool Recycle;
  2630. if (IsSideLocalBrowser(Side))
  2631. {
  2632. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2633. }
  2634. else
  2635. {
  2636. Recycle =
  2637. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2638. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2639. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2640. }
  2641. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2642. if (!Result)
  2643. {
  2644. UnicodeString Query;
  2645. if (FileList->Count == 1)
  2646. {
  2647. if (IsSideLocalBrowser(Side))
  2648. {
  2649. Query = ExtractFileName(FileList->Strings[0]);
  2650. }
  2651. else
  2652. {
  2653. Query = UnixExtractFileName(FileList->Strings[0]);
  2654. }
  2655. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2656. }
  2657. else
  2658. {
  2659. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2660. }
  2661. TMessageParams Params(mpNeverAskAgainCheck);
  2662. Params.ImageName = L"Delete file";
  2663. unsigned int Answer =
  2664. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2665. if (Answer == qaNeverAskAgain)
  2666. {
  2667. Result = true;
  2668. if (Recycle)
  2669. {
  2670. WinConfiguration->ConfirmRecycling = false;
  2671. }
  2672. else
  2673. {
  2674. WinConfiguration->ConfirmDeleting = false;
  2675. }
  2676. }
  2677. else
  2678. {
  2679. Result = (Answer == qaOK);
  2680. }
  2681. }
  2682. if (Result)
  2683. {
  2684. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2685. }
  2686. return Result;
  2687. }
  2688. //---------------------------------------------------------------------------
  2689. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2690. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2691. {
  2692. TAutoBatch AutoBatch(this);
  2693. bool Result;
  2694. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2695. {
  2696. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2697. }
  2698. else if (Operation == foRename)
  2699. {
  2700. DebugAssert(DirView(Side)->ItemFocused);
  2701. DirView(Side)->ItemFocused->EditCaption();
  2702. Result = true;
  2703. }
  2704. else if (Operation == foDelete)
  2705. {
  2706. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2707. }
  2708. else if (Operation == foSetProperties)
  2709. {
  2710. DirView(osRemote)->SaveSelectedNames();
  2711. Result = SetProperties(Side, FileList);
  2712. }
  2713. else if (Operation == foCustomCommand)
  2714. {
  2715. DebugAssert(Param);
  2716. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2717. DirView(osRemote)->SaveSelectedNames();
  2718. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2719. CustomCommand(FileList, *Command, NULL);
  2720. Result = true;
  2721. }
  2722. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2723. {
  2724. DebugAssert(!IsSideLocalBrowser(Side));
  2725. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2726. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2727. }
  2728. else if (Operation == foLock)
  2729. {
  2730. DebugAssert(!IsSideLocalBrowser(Side));
  2731. LockFiles(FileList, true);
  2732. Result = true;
  2733. }
  2734. else if (Operation == foUnlock)
  2735. {
  2736. DebugAssert(!IsSideLocalBrowser(Side));
  2737. LockFiles(FileList, false);
  2738. Result = true;
  2739. }
  2740. else
  2741. {
  2742. DebugFail();
  2743. }
  2744. return Result;
  2745. }
  2746. //---------------------------------------------------------------------------
  2747. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2748. {
  2749. if (Side == osCurrent)
  2750. {
  2751. Side = FCurrentSide;
  2752. }
  2753. return Side;
  2754. }
  2755. //---------------------------------------------------------------------------
  2756. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2757. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2758. {
  2759. Side = GetSide(Side);
  2760. bool FullPath = IsSideLocalBrowser(Side);
  2761. TCustomDirView * ADirView = DirView(Side);
  2762. std::unique_ptr<TStringList> FileList(new TStringList());
  2763. ADirView->CreateFileList(OnFocused, FullPath, FileList.get());
  2764. bool ReloadProperties =
  2765. !IsSideLocalBrowser(Side) && (Operation == foSetProperties) &&
  2766. DebugAlwaysTrue(HasActiveTerminal()) && Terminal->IsCapable[fcLoadingAdditionalProperties];
  2767. bool Result = ExecuteFileOperation(Operation, Side, FileList.get(), NoConfirmation, Param);
  2768. if (Result && ReloadProperties)
  2769. {
  2770. std::unique_ptr<TStringList> FileList2(new TStringList());
  2771. FileList->CaseSensitive = true;
  2772. FileList->Sort();
  2773. for (int Index = 0; Index < ADirView->Items->Count; Index++)
  2774. {
  2775. TListItem * Item = ADirView->Items->Item[Index];
  2776. UnicodeString FileName = ADirView->ItemFileName(Item);
  2777. int Index2;
  2778. if (FileList->Find(FileName, Index2))
  2779. {
  2780. FileList2->AddObject(FileName, ADirView->ItemData(Item));
  2781. }
  2782. }
  2783. LoadFilesProperties(FileList2.get());
  2784. }
  2785. return Result;
  2786. }
  2787. //---------------------------------------------------------------------------
  2788. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2789. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2790. bool NoConfirmation, void * Param)
  2791. {
  2792. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2793. {
  2794. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2795. {
  2796. DebugAssert(!IsLocalBrowserMode());
  2797. if (GetSide(Side) == osLocal)
  2798. {
  2799. Configuration->Usage->Inc(L"UploadsCommand");
  2800. }
  2801. else
  2802. {
  2803. Configuration->Usage->Inc(L"DownloadsCommand");
  2804. }
  2805. }
  2806. }
  2807. }
  2808. //---------------------------------------------------------------------------
  2809. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2810. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2811. {
  2812. TTransferOperationParam Param;
  2813. if ((WinConfiguration->Interface != ifCommander) ||
  2814. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2815. {
  2816. Flags &= ~cocShortCutHint;
  2817. }
  2818. TCustomDirView * DView = DirView(Side);
  2819. Param.Options =
  2820. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2821. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2822. if (FLAGSET(Flags, cocQueue))
  2823. {
  2824. Param.Queue = asOn;
  2825. }
  2826. else if (FLAGSET(Flags, cocNonQueue))
  2827. {
  2828. Param.Queue = asOff;
  2829. }
  2830. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2831. }
  2832. //---------------------------------------------------------------------------
  2833. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2834. {
  2835. int Count = ErrorList->Count;
  2836. try
  2837. {
  2838. if (ErrorList->Count)
  2839. {
  2840. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2841. if (MessageDialog(Message, qtError,
  2842. qaOK | qaCancel, HELP_NONE) == qaOK)
  2843. {
  2844. unsigned int Answer;
  2845. int Index = 0;
  2846. do
  2847. {
  2848. DebugAssert(Index >= 0 && Index < Count);
  2849. TQueryButtonAlias Aliases[2];
  2850. Aliases[0].Button = qaYes;
  2851. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2852. Aliases[1].Button = qaNo;
  2853. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2854. TMessageParams Params;
  2855. Params.Aliases = Aliases;
  2856. Params.AliasesCount = LENOF(Aliases);
  2857. int No = Index + 1;
  2858. UnicodeString Message = ErrorList->Strings[Index];
  2859. UnicodeString AMainInstructions;
  2860. UnicodeString Details;
  2861. if (ExtractMainInstructions(Message, AMainInstructions))
  2862. {
  2863. Details = Message;
  2864. Message = AMainInstructions;
  2865. }
  2866. Message = MainInstructions(FMTLOAD(ERROR_LIST_NUMBER, (No, Count, Message))) + Details;
  2867. Answer = MoreMessageDialog(
  2868. Message,
  2869. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2870. (Index ? qaYes : 0) | (Index < Count - 1 ? qaNo : 0) |
  2871. qaOK, HELP_NONE, &Params);
  2872. if (Answer == qaNo)
  2873. {
  2874. Index++;
  2875. }
  2876. if (Answer == qaYes)
  2877. {
  2878. Index--;
  2879. }
  2880. }
  2881. while (Answer != qaOK);
  2882. }
  2883. }
  2884. }
  2885. __finally
  2886. {
  2887. TStrings * List = ErrorList;
  2888. ErrorList = NULL;
  2889. for (int i = 0; i < Count; i++)
  2890. {
  2891. delete List->Objects[i];
  2892. }
  2893. delete List;
  2894. }
  2895. }
  2896. //---------------------------------------------------------------------------
  2897. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2898. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2899. {
  2900. // needed for checking filemasks, as there's no directory object
  2901. // associated with the file object
  2902. File->FullFileName = FullFileName;
  2903. TFileMasks::TParams MaskParams;
  2904. MaskParams.Size = File->Size;
  2905. MaskParams.Modification = File->Modification;
  2906. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2907. }
  2908. //---------------------------------------------------------------------------
  2909. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2910. {
  2911. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2912. Side = GetSide(Side);
  2913. TCustomDirView * DView = DirView(Side);
  2914. TListItem * FocusedItem = DView->ItemFocused;
  2915. UnicodeString Name;
  2916. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2917. {
  2918. Name = DView->ItemFileName(FocusedItem);
  2919. }
  2920. else
  2921. {
  2922. Name = LoadStr(NEW_FILE);
  2923. }
  2924. UnicodeString Names = Name;
  2925. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2926. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2927. HELP_EDIT_NEW, History.get(), true, NULL, true, 400))
  2928. {
  2929. while (!Names.IsEmpty())
  2930. {
  2931. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2932. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2933. UnicodeString TargetFileName;
  2934. UnicodeString LocalFileName;
  2935. UnicodeString RootTempDir;
  2936. UnicodeString TempDir;
  2937. UnicodeString RemoteDirectory;
  2938. bool ExistingFile = false;
  2939. if (!IsSideLocalBrowser(Side))
  2940. {
  2941. Name = AbsolutePath(Terminal->CurrentDirectory, Name);
  2942. TRemoteFile * File = NULL;
  2943. if (Terminal->FileExists(Name, &File) &&
  2944. !File->IsDirectory)
  2945. {
  2946. try
  2947. {
  2948. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2949. ExistingFile = true;
  2950. }
  2951. __finally
  2952. {
  2953. delete File;
  2954. }
  2955. }
  2956. if (!ExistingFile)
  2957. {
  2958. RemoteDirectory = UnixExtractFilePath(Name);
  2959. TemporaryDirectoryForRemoteFiles(
  2960. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2961. TargetFileName = UnixExtractFileName(Name);
  2962. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2963. LocalFileName = TempDir +
  2964. // We probably do not want to trim the VMS version here
  2965. Terminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2966. }
  2967. }
  2968. else
  2969. {
  2970. if (ExtractFilePath(Name).IsEmpty())
  2971. {
  2972. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2973. }
  2974. else
  2975. {
  2976. LocalFileName = ExpandFileName(Name);
  2977. }
  2978. TargetFileName = ExtractFileName(Name);
  2979. }
  2980. if (!ExistingFile)
  2981. {
  2982. bool NewFile = !FileExists(ApiPath(LocalFileName));
  2983. if (NewFile)
  2984. {
  2985. int File = FileCreate(ApiPath(LocalFileName));
  2986. if (File < 0)
  2987. {
  2988. if (!RootTempDir.IsEmpty())
  2989. {
  2990. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2991. }
  2992. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2993. }
  2994. else
  2995. {
  2996. FileClose(File);
  2997. }
  2998. }
  2999. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  3000. const TEditorData * ExternalEditor = NULL;
  3001. TFileMasks::TParams MaskParams; // size not known
  3002. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  3003. false, MaskParams);
  3004. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  3005. ExternalEditor, RootTempDir, RemoteDirectory, NewFile);
  3006. }
  3007. }
  3008. }
  3009. }
  3010. //---------------------------------------------------------------------------
  3011. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  3012. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  3013. {
  3014. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3015. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3016. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3017. return
  3018. ((ExecuteFileBy == efInternalEditor)) ||
  3019. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  3020. }
  3021. //---------------------------------------------------------------------------
  3022. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  3023. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  3024. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  3025. UnicodeString RemoteDirectory, bool NewFile)
  3026. {
  3027. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3028. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3029. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3030. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3031. Side = GetSide(Side);
  3032. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  3033. if (!IsSideLocalBrowser(Side))
  3034. {
  3035. Data->Terminal = Terminal;
  3036. Data->Queue = Queue;
  3037. Data->SessionData = CloneCurrentSessionData();
  3038. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  3039. Data->RemoteDirectory = RemoteDirectory;
  3040. Data->SessionName = Terminal->SessionData->SessionName;
  3041. Data->LocalRootDirectory = LocalRootDirectory;
  3042. Data->OriginalFileName = OriginalFileName;
  3043. Data->Command = L""; // will be changed later for external editor
  3044. }
  3045. if (ExecuteFileBy == efInternalEditor)
  3046. {
  3047. if (!IsSideLocalBrowser(Side))
  3048. {
  3049. UnicodeString RemotePath = UnixCombinePaths(RemoteDirectory, OriginalFileName);
  3050. UnicodeString Caption = RemotePath + TitleSeparator + Terminal->SessionData->SessionName;
  3051. TForm * Editor;
  3052. try
  3053. {
  3054. Editor =
  3055. ShowEditorForm(
  3056. FileName, this,
  3057. FEditorManager->FileChanged, FEditorManager->FileReload, FEditorManager->FileClosed,
  3058. SaveAllInternalEditors, AnyInternalEditorModified,
  3059. Caption, StandaloneOperation, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  3060. }
  3061. catch(...)
  3062. {
  3063. if (!LocalRootDirectory.IsEmpty())
  3064. {
  3065. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  3066. }
  3067. throw;
  3068. }
  3069. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  3070. }
  3071. else
  3072. {
  3073. DebugAssert(!StandaloneOperation);
  3074. TForm * Editor =
  3075. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  3076. SaveAllInternalEditors, AnyInternalEditorModified,
  3077. L"", false, SessionColor, -1, NewFile);
  3078. FLocalEditors->Add(Editor);
  3079. }
  3080. }
  3081. else
  3082. {
  3083. HANDLE Process;
  3084. if (ExecuteFileBy == efExternalEditor)
  3085. {
  3086. UnicodeString Program, Params, Dir;
  3087. Data->Command = ExternalEditor->ExternalEditor;
  3088. ReformatFileNameCommand(Data->Command);
  3089. SplitCommand(Data->Command, Program, Params, Dir);
  3090. Params = ExpandFileNameCommand(Params, FileName);
  3091. Program = ExpandEnvironmentVariables(Program);
  3092. if (!ExecuteShell(Program, Params, Process))
  3093. {
  3094. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  3095. }
  3096. }
  3097. else
  3098. {
  3099. DebugAssert(!IsSideLocalBrowser(Side));
  3100. if (!ExecuteShell(FileName, L"", Process))
  3101. {
  3102. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  3103. }
  3104. }
  3105. if (IsSideLocalBrowser(Side) ||
  3106. ((ExecuteFileBy == efShell) &&
  3107. !WinConfiguration->Editor.SDIShellEditor) ||
  3108. ((ExecuteFileBy == efExternalEditor) &&
  3109. !ExternalEditor->SDIExternalEditor))
  3110. {
  3111. // no need for handle
  3112. if (Process != NULL)
  3113. {
  3114. DebugCheck(CloseHandle(Process));
  3115. }
  3116. Process = INVALID_HANDLE_VALUE;
  3117. }
  3118. else
  3119. {
  3120. if (Process == NULL)
  3121. {
  3122. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  3123. }
  3124. }
  3125. if (!IsSideLocalBrowser(Side))
  3126. {
  3127. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  3128. }
  3129. }
  3130. }
  3131. //---------------------------------------------------------------------------
  3132. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3133. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3134. void * /*Arg*/)
  3135. {
  3136. if (Token != NULL)
  3137. {
  3138. EditorFormFileSave(static_cast<TForm *>(Token));
  3139. }
  3140. }
  3141. //---------------------------------------------------------------------------
  3142. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3143. {
  3144. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3145. {
  3146. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3147. }
  3148. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3149. }
  3150. //---------------------------------------------------------------------------
  3151. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3152. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3153. void * Arg)
  3154. {
  3155. if ((Token != NULL) &&
  3156. IsEditorFormModified(static_cast<TForm *>(Token)))
  3157. {
  3158. *static_cast<bool *>(Arg) = true;
  3159. }
  3160. }
  3161. //---------------------------------------------------------------------------
  3162. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3163. TObject * /*Sender*/, bool & Modified)
  3164. {
  3165. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3166. {
  3167. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3168. {
  3169. Modified = true;
  3170. }
  3171. }
  3172. if (!Modified)
  3173. {
  3174. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3175. }
  3176. }
  3177. //---------------------------------------------------------------------------
  3178. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3179. {
  3180. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3181. }
  3182. //---------------------------------------------------------------------------
  3183. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3184. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3185. UnicodeString & Result, UnicodeString & RootDirectory)
  3186. {
  3187. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3188. {
  3189. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3190. Result = RootDirectory;
  3191. }
  3192. else
  3193. {
  3194. RootDirectory = L"";
  3195. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3196. Result = IncludeTrailingBackslash(Result);
  3197. }
  3198. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3199. {
  3200. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3201. }
  3202. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3203. {
  3204. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3205. {
  3206. RemoteDirectory.Delete(1, 1);
  3207. }
  3208. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3209. }
  3210. if (!ForceDirectories(ApiPath(Result)))
  3211. {
  3212. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3213. }
  3214. }
  3215. //---------------------------------------------------------------------------
  3216. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3217. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3218. bool GetTargetNames, bool AutoOperation)
  3219. {
  3220. DebugAssert(!IsLocalBrowserMode());
  3221. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3222. if (ForceText)
  3223. {
  3224. CopyParam.TransferMode = tmAscii;
  3225. }
  3226. TemporaryFileCopyParam(CopyParam);
  3227. if (TempDir.IsEmpty())
  3228. {
  3229. TemporaryDirectoryForRemoteFiles(Terminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3230. }
  3231. DebugAssert(!FAutoOperation);
  3232. FAutoOperation = AutoOperation;
  3233. Terminal->ExceptionOnFail = true;
  3234. try
  3235. {
  3236. try
  3237. {
  3238. // turn off confirmations, as for MDI editors we may possibly download
  3239. // the same file over
  3240. Terminal->CopyToLocal(
  3241. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3242. if (GetTargetNames)
  3243. {
  3244. for (int i = 0; i < FileList->Count; i++)
  3245. {
  3246. FileList->Strings[i] =
  3247. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3248. }
  3249. }
  3250. }
  3251. catch(...)
  3252. {
  3253. if (!RootTempDir.IsEmpty())
  3254. {
  3255. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3256. }
  3257. throw;
  3258. }
  3259. }
  3260. __finally
  3261. {
  3262. FAutoOperation = false;
  3263. Terminal->ExceptionOnFail = false;
  3264. }
  3265. }
  3266. //---------------------------------------------------------------------------
  3267. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3268. {
  3269. // Do not waste time checking for default editor list the next time,
  3270. // or testing if default editor is not notepad.
  3271. bool TryNextTime = false;
  3272. UnicodeString UsageState;
  3273. if (!WinConfiguration->EditorList->IsDefaultList())
  3274. {
  3275. UsageState = "H";
  3276. }
  3277. else
  3278. {
  3279. UnicodeString Executable;
  3280. UnicodeString ExecutableDescription;
  3281. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3282. {
  3283. UnicodeString Message =
  3284. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3285. unsigned int Answer =
  3286. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3287. if (Answer != qaOK)
  3288. {
  3289. UsageState = "R";
  3290. }
  3291. else
  3292. {
  3293. UsageState = "A";
  3294. TEditorData EditorData;
  3295. EditorData.Editor = edExternal;
  3296. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3297. EditorData.ExternalEditorOptionsAutodetect();
  3298. TEditorList EditorList;
  3299. EditorList = *WinConfiguration->EditorList;
  3300. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3301. WinConfiguration->EditorList = &EditorList;
  3302. }
  3303. }
  3304. }
  3305. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3306. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3307. }
  3308. //---------------------------------------------------------------------------
  3309. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3310. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3311. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3312. {
  3313. if (ExecuteFileBy == efDefaultEditor)
  3314. {
  3315. if (!WinConfiguration->OfferedEditorAutoConfig)
  3316. {
  3317. EditorAutoConfig();
  3318. }
  3319. const TEditorPreferences * Editor =
  3320. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3321. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3322. {
  3323. ExecuteFileBy = efInternalEditor;
  3324. ExternalEditor = NULL;
  3325. }
  3326. else if (Editor->Data->Editor == edOpen)
  3327. {
  3328. ExecuteFileBy = efShell;
  3329. ExternalEditor = NULL;
  3330. }
  3331. else
  3332. {
  3333. ExecuteFileBy = efExternalEditor;
  3334. ExternalEditor = Editor->Data;
  3335. }
  3336. }
  3337. }
  3338. //---------------------------------------------------------------------------
  3339. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3340. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3341. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3342. {
  3343. UnicodeString OriginalFileName;
  3344. UnicodeString LocalRootDirectory;
  3345. UnicodeString RemoteDirectory;
  3346. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3347. IsSideLocalBrowser(Side), MaskParams);
  3348. UnicodeString Counter;
  3349. UnicodeString LocalFileName;
  3350. bool Handled = false;
  3351. if (!IsSideLocalBrowser(Side))
  3352. {
  3353. // We need to trim VMS version here, so that we use name without version
  3354. // when uploading back to create a new version of the file
  3355. OriginalFileName = Terminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3356. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3357. TObject * Token = NULL;
  3358. UnicodeString LocalDirectory;
  3359. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3360. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3361. LocalDirectory))
  3362. {
  3363. if (Token != NULL)
  3364. {
  3365. TForm * Form = dynamic_cast<TForm *>(Token);
  3366. if (Form->WindowState == wsMinimized)
  3367. {
  3368. ShowWindow(Form->Handle, SW_RESTORE);
  3369. }
  3370. else
  3371. {
  3372. Form->SetFocus();
  3373. }
  3374. Handled = true;
  3375. }
  3376. else
  3377. {
  3378. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3379. }
  3380. }
  3381. if (!Handled)
  3382. {
  3383. TStringList * FileList1 = new TStringList();
  3384. try
  3385. {
  3386. FileList1->AddObject(FullFileName, Object);
  3387. TemporarilyDownloadFiles(FileList1,
  3388. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3389. LocalRootDirectory, LocalDirectory, true, true);
  3390. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3391. }
  3392. __finally
  3393. {
  3394. delete FileList1;
  3395. }
  3396. switch (ExecuteFileBy)
  3397. {
  3398. case efShell:
  3399. Counter = "RemoteFilesExecuted";
  3400. break;
  3401. case efInternalEditor:
  3402. Counter = "RemoteFilesOpenedInInternalEditor";
  3403. break;
  3404. case efExternalEditor:
  3405. Counter = "RemoteFilesOpenedInExternalEditor";
  3406. break;
  3407. default:
  3408. DebugFail();
  3409. }
  3410. }
  3411. }
  3412. else
  3413. {
  3414. LocalFileName = FullFileName;
  3415. OriginalFileName = ExtractFileName(FullFileName);
  3416. switch (ExecuteFileBy)
  3417. {
  3418. case efShell:
  3419. Counter = "LocalFilesExecuted";
  3420. break;
  3421. case efInternalEditor:
  3422. Counter = "LocalFilesOpenedInInternalEditor";
  3423. break;
  3424. case efExternalEditor:
  3425. Counter = "LocalFilesOpenedInExternalEditor";
  3426. break;
  3427. default:
  3428. DebugFail();
  3429. }
  3430. }
  3431. if (!Handled)
  3432. {
  3433. Configuration->Usage->Inc(Counter);
  3434. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3435. ExternalEditor, LocalRootDirectory, RemoteDirectory, false);
  3436. }
  3437. }
  3438. //---------------------------------------------------------------------------
  3439. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3440. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3441. bool AllSelected, bool OnFocused)
  3442. {
  3443. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3444. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3445. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3446. Side = GetSide(Side);
  3447. TCustomDirView * DView = DirView(Side);
  3448. TStrings * FileList =
  3449. AllSelected ?
  3450. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3451. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3452. try
  3453. {
  3454. DebugAssert(AllSelected || (FileList->Count == 1));
  3455. for (int i = 0; i < FileList->Count; i++)
  3456. {
  3457. UnicodeString ListFileName = FileList->Strings[i];
  3458. UnicodeString FileNameOnly =
  3459. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3460. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3461. if (!DView->ItemIsDirectory(Item))
  3462. {
  3463. UnicodeString FullFileName;
  3464. if (!IsSideLocalBrowser(Side))
  3465. {
  3466. FullFileName = DirView(Side)->Path + ListFileName;
  3467. }
  3468. else
  3469. {
  3470. FullFileName = ListFileName;
  3471. }
  3472. TFileMasks::TParams MaskParams;
  3473. MaskParams.Size = DView->ItemFileSize(Item);
  3474. TDateTimePrecision Precision;
  3475. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3476. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3477. FileList->Objects[i], MaskParams);
  3478. }
  3479. }
  3480. }
  3481. __finally
  3482. {
  3483. delete FileList;
  3484. }
  3485. }
  3486. //---------------------------------------------------------------------------
  3487. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3488. {
  3489. CopyParam.FileNameCase = ncNoChange;
  3490. CopyParam.PreserveRights = false;
  3491. CopyParam.PreserveReadOnly = false;
  3492. CopyParam.ReplaceInvalidChars = true;
  3493. CopyParam.IncludeFileMask = TFileMasks();
  3494. CopyParam.NewerOnly = false;
  3495. CopyParam.FileMask = L"";
  3496. }
  3497. //---------------------------------------------------------------------------
  3498. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(
  3499. const UnicodeString & FileName, TEditedFileData * Data, HANDLE UploadCompleteEvent, bool & Retry)
  3500. {
  3501. TTerminalManager * Manager = TTerminalManager::Instance();
  3502. if (!IsActiveTerminal(Data->Terminal))
  3503. {
  3504. if (!NonVisualDataModule->Busy)
  3505. {
  3506. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3507. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3508. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3509. if (Data->Terminal == NULL)
  3510. {
  3511. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3512. if (SameSiteTerminal != NULL)
  3513. {
  3514. UnicodeString Message =
  3515. FMTLOAD(EDIT_SESSION_REATTACH,
  3516. (FileNameOnly, Data->SessionName, FileNameOnly));
  3517. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3518. {
  3519. Data->Terminal = Terminal;
  3520. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3521. Data->SessionName = Terminal->SessionData->SessionName;
  3522. // We might also overwrite session data
  3523. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3524. }
  3525. else
  3526. {
  3527. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3528. Abort();
  3529. }
  3530. }
  3531. }
  3532. // foreground session should reconnect itself
  3533. else if (Terminal != Data->Terminal)
  3534. {
  3535. UnicodeString Message =
  3536. MainInstructions(
  3537. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3538. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3539. {
  3540. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3541. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3542. }
  3543. else
  3544. {
  3545. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3546. Abort();
  3547. }
  3548. }
  3549. }
  3550. if (!IsActiveTerminal(Data->Terminal))
  3551. {
  3552. Configuration->Usage->Inc(L"EditInactiveSession");
  3553. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3554. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3555. (ExtractFileName(FileName), Data->SessionName)));
  3556. }
  3557. }
  3558. TStrings * FileList = new TStringList();
  3559. try
  3560. {
  3561. FileList->Add(FileName);
  3562. // consider using the same settings (preset) as when the file was downloaded
  3563. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3564. TemporaryFileCopyParam(CopyParam);
  3565. if (Data->ForceText)
  3566. {
  3567. CopyParam.TransferMode = tmAscii;
  3568. }
  3569. // so i do not need to worry if masking algorithm works in all cases
  3570. // ("" means "copy file name", no masking is actually done)
  3571. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3572. {
  3573. CopyParam.FileMask = L"";
  3574. }
  3575. else
  3576. {
  3577. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3578. }
  3579. int Params = cpNoConfirmation | cpTemporary;
  3580. if (Terminal->IsCapable[fcBackgroundTransfers])
  3581. {
  3582. DebugAssert(Data->Queue != NULL);
  3583. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3584. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3585. QueueItem->CompleteEvent = UploadCompleteEvent;
  3586. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3587. }
  3588. else
  3589. {
  3590. if (NonVisualDataModule->Busy)
  3591. {
  3592. Retry = true;
  3593. }
  3594. else
  3595. {
  3596. Terminal->CopyToRemote(FileList, Data->RemoteDirectory, &CopyParam, Params, NULL);
  3597. SetEvent(UploadCompleteEvent);
  3598. }
  3599. }
  3600. }
  3601. __finally
  3602. {
  3603. delete FileList;
  3604. }
  3605. }
  3606. //---------------------------------------------------------------------------
  3607. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3608. const UnicodeString FileName, const TEditedFileData * Data)
  3609. {
  3610. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3611. DebugAssert(!NonVisualDataModule->Busy);
  3612. if (!IsActiveTerminal(Data->Terminal))
  3613. {
  3614. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3615. (ExtractFileName(FileName), Data->SessionName)));
  3616. }
  3617. TManagedTerminal * PrevSession = TTerminalManager::Instance()->ActiveSession;
  3618. TTerminalManager::Instance()->ActiveSession = Data->Terminal;
  3619. NonVisualDataModule->StartBusy();
  3620. try
  3621. {
  3622. std::unique_ptr<TRemoteFile> File;
  3623. UnicodeString RemoteFileName =
  3624. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3625. Terminal->ExceptionOnFail = true;
  3626. try
  3627. {
  3628. TRemoteFile * AFile = NULL;
  3629. Terminal->ReadFile(RemoteFileName, AFile);
  3630. File.reset(AFile);
  3631. if (!File->HaveFullFileName)
  3632. {
  3633. File->FullFileName = RemoteFileName;
  3634. }
  3635. }
  3636. __finally
  3637. {
  3638. Terminal->ExceptionOnFail = false;
  3639. }
  3640. std::unique_ptr<TStrings> FileList(new TStringList());
  3641. FileList->AddObject(RemoteFileName, File.get());
  3642. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3643. UnicodeString TempDir = ExtractFilePath(FileName);
  3644. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir, TempDir, true, true);
  3645. // sanity check, the target file name should be still the same
  3646. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3647. }
  3648. __finally
  3649. {
  3650. NonVisualDataModule->EndBusy();
  3651. // it actually may not exist anymore...
  3652. TTerminalManager::Instance()->ActiveSession = PrevSession;
  3653. }
  3654. }
  3655. //---------------------------------------------------------------------------
  3656. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3657. const TEditedFileData * Data, bool & KeepOpen)
  3658. {
  3659. // Command is set for external editors only (not for "shell" open).
  3660. if (!Data->Command.IsEmpty())
  3661. {
  3662. bool AnyFound = false;
  3663. bool AnyMDI = false;
  3664. bool AnyNonMDI = false;
  3665. bool AnyDetect = false;
  3666. TEditorList * EditorList = new TEditorList();
  3667. try
  3668. {
  3669. *EditorList = *WinConfiguration->EditorList;
  3670. for (int i = 0; i < EditorList->Count; i++)
  3671. {
  3672. const TEditorPreferences * Editor = EditorList->Editors[i];
  3673. if ((Editor->Data->Editor == edExternal) &&
  3674. (Editor->Data->ExternalEditor == Data->Command))
  3675. {
  3676. AnyFound = true;
  3677. if (Editor->Data->SDIExternalEditor)
  3678. {
  3679. AnyNonMDI = true;
  3680. if (Editor->Data->DetectMDIExternalEditor)
  3681. {
  3682. AnyDetect = true;
  3683. }
  3684. }
  3685. else
  3686. {
  3687. AnyMDI = true;
  3688. }
  3689. }
  3690. }
  3691. bool EnableMDI = false;
  3692. bool DisableDetect = false;
  3693. if (AnyMDI)
  3694. {
  3695. KeepOpen = true;
  3696. if (AnyNonMDI)
  3697. {
  3698. // there is at least one instance of the editor with MDI support enabled,
  3699. // and one with disabled, enable it for all instances
  3700. EnableMDI = true;
  3701. }
  3702. }
  3703. else if (AnyFound && !AnyDetect)
  3704. {
  3705. // at least once instance found but all have MDI autodetection disabled
  3706. // => close the file (default action)
  3707. }
  3708. else
  3709. {
  3710. // no instance of the editor has MDI support enabled
  3711. TMessageParams Params;
  3712. if (AnyFound)
  3713. {
  3714. // there is at least one instance of the editor
  3715. Params.Params |= mpNeverAskAgainCheck;
  3716. }
  3717. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3718. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3719. switch (Answer)
  3720. {
  3721. case qaNeverAskAgain:
  3722. DisableDetect = true;
  3723. break;
  3724. case qaNo:
  3725. EnableMDI = true;
  3726. KeepOpen = true;
  3727. break;
  3728. }
  3729. }
  3730. if (AnyFound && (EnableMDI || DisableDetect))
  3731. {
  3732. bool Changed = false;
  3733. for (int i = 0; i < EditorList->Count; i++)
  3734. {
  3735. const TEditorPreferences * Editor = EditorList->Editors[i];
  3736. if ((Editor->Data->Editor == edExternal) &&
  3737. (Editor->Data->ExternalEditor == Data->Command) &&
  3738. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3739. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3740. {
  3741. Changed = true;
  3742. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3743. if (EnableMDI)
  3744. {
  3745. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3746. }
  3747. if (DisableDetect)
  3748. {
  3749. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3750. }
  3751. EditorList->Change(i, UpdatedEditor);
  3752. }
  3753. }
  3754. if (Changed)
  3755. {
  3756. WinConfiguration->EditorList = EditorList;
  3757. }
  3758. }
  3759. }
  3760. __finally
  3761. {
  3762. delete EditorList;
  3763. }
  3764. }
  3765. else
  3766. {
  3767. // "open" case
  3768. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3769. qaOK, HELP_APP_EARLY_CLOSED);
  3770. }
  3771. }
  3772. //---------------------------------------------------------------------------
  3773. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3774. {
  3775. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3776. }
  3777. //---------------------------------------------------------------------------
  3778. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3779. {
  3780. SideEnter(osRemote);
  3781. }
  3782. //---------------------------------------------------------------------------
  3783. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3784. {
  3785. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3786. SideEnter(osRemote);
  3787. }
  3788. //---------------------------------------------------------------------------
  3789. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3790. {
  3791. FCurrentSide = Side;
  3792. if (Visible)
  3793. {
  3794. UpdateControls();
  3795. }
  3796. }
  3797. //---------------------------------------------------------------------------
  3798. void __fastcall TCustomScpExplorerForm::FileDeleted(TOperationSide Side, const UnicodeString & FileName, bool Success)
  3799. {
  3800. VisualiseOperationFinished(Side, FileName, Success);
  3801. if (DebugAlwaysTrue(FDeletedFiles != NULL) && Success)
  3802. {
  3803. FDeletedFiles->Add(FileName);
  3804. }
  3805. }
  3806. //---------------------------------------------------------------------------
  3807. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3808. TStrings * FileList, bool Alternative)
  3809. {
  3810. TCustomDirView * DView = DirView(Side);
  3811. DView->SaveSelection();
  3812. DView->SaveSelectedNames();
  3813. DebugAssert(!FAlternativeDelete);
  3814. FAlternativeDelete = Alternative;
  3815. try
  3816. {
  3817. if (!IsSideLocalBrowser(Side))
  3818. {
  3819. DebugAssert(Terminal != NULL);
  3820. // Clone the file list as it may refer to current directory files, which get destroyed, when the directory is reloaded after the operation
  3821. std::unique_ptr<TStrings> PermanentFileList(TRemoteFileList::CloneStrings(FileList));
  3822. std::unique_ptr<TStringList> DeletedFiles(new TStringList());
  3823. DeletedFiles->CaseSensitive = DebugAlwaysTrue(!IsSideLocalBrowser(Side));
  3824. int Params = FLAGMASK(Alternative, dfAlternative);
  3825. try
  3826. {
  3827. TValueRestorer<TStrings *> DeletedFilesRestorer(FDeletedFiles);
  3828. DebugAssert(FDeletedFiles == NULL);
  3829. FDeletedFiles = DeletedFiles.get();
  3830. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  3831. FOnFileOperationFinished = FileDeleted;
  3832. FMoveToQueue = false;
  3833. Terminal->DeleteFiles(FileList, Params);
  3834. // Probably not needed for deleting, just for consistency with transfer code
  3835. if (FMoveToQueue)
  3836. {
  3837. Abort();
  3838. }
  3839. }
  3840. catch (EAbort &)
  3841. {
  3842. if (FMoveToQueue)
  3843. {
  3844. DeletedFiles->Sorted = true;
  3845. for (int Index = 0; Index < PermanentFileList->Count; Index++)
  3846. {
  3847. if (DeletedFiles->IndexOf(PermanentFileList->Strings[Index]) >= 0)
  3848. {
  3849. // We should always be deleting the first item => what can be used to optimize this code, if needed
  3850. DebugAssert(Index == 0);
  3851. PermanentFileList->Delete(Index);
  3852. Index--;
  3853. }
  3854. }
  3855. Configuration->Usage->Inc("MovesToBackgroundDelete");
  3856. TQueueItem * QueueItem = new TDeleteQueueItem(Terminal, PermanentFileList.get(), Params);
  3857. AddQueueItem(Queue, QueueItem, Terminal);
  3858. ClearOperationSelection(osRemote);
  3859. }
  3860. }
  3861. }
  3862. else
  3863. {
  3864. if (IsLocalBrowserMode())
  3865. {
  3866. Configuration->Usage->Inc(L"LocalLocalDeletes");
  3867. }
  3868. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  3869. FProgressSide = FCurrentSide;
  3870. try
  3871. {
  3872. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3873. }
  3874. __finally
  3875. {
  3876. ReloadLocalDirectory();
  3877. }
  3878. }
  3879. FAlternativeDelete = false;
  3880. }
  3881. catch(...)
  3882. {
  3883. FAlternativeDelete = false;
  3884. DView->DiscardSavedSelection();
  3885. throw;
  3886. }
  3887. DView->RestoreSelection();
  3888. }
  3889. //---------------------------------------------------------------------------
  3890. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3891. {
  3892. DebugAssert(Terminal);
  3893. DebugAssert(!IsLocalBrowserMode());
  3894. RemoteDirView->SaveSelection();
  3895. RemoteDirView->SaveSelectedNames();
  3896. try
  3897. {
  3898. if (Lock)
  3899. {
  3900. Terminal->LockFiles(FileList);
  3901. }
  3902. else
  3903. {
  3904. Terminal->UnlockFiles(FileList);
  3905. }
  3906. }
  3907. catch(...)
  3908. {
  3909. RemoteDirView->DiscardSavedSelection();
  3910. throw;
  3911. }
  3912. RemoteDirView->RestoreSelection();
  3913. }
  3914. //---------------------------------------------------------------------------
  3915. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  3916. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3917. bool NoConfirmation, bool Move)
  3918. {
  3919. DebugAssert(Terminal != NULL);
  3920. DebugAssert(Terminal == TTerminalManager::Instance()->ActiveTerminal);
  3921. DebugAssert(!IsLocalBrowserMode());
  3922. if (Session == NULL)
  3923. {
  3924. Session = Terminal;
  3925. }
  3926. if (Session == Terminal)
  3927. {
  3928. if (RemoteDriveView->DropTarget != NULL)
  3929. {
  3930. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3931. }
  3932. else if (RemoteDirView->DropTarget != NULL)
  3933. {
  3934. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3935. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3936. }
  3937. else
  3938. {
  3939. Target = RemoteDirView->Path;
  3940. }
  3941. }
  3942. else
  3943. {
  3944. Target = GetSessionPath(Session, osRemote);
  3945. }
  3946. Target = UnixIncludeTrailingBackslash(Target);
  3947. if (FileList->Count == 1)
  3948. {
  3949. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3950. }
  3951. else
  3952. {
  3953. FileMask = AnyMask;
  3954. }
  3955. DirectCopy = Terminal->IsCapable[fcRemoteCopy] || Terminal->IsCapable[fcSecondaryShell];
  3956. bool Result = true;
  3957. if (!NoConfirmation)
  3958. {
  3959. bool Multi = (FileList->Count > 1);
  3960. if (Move)
  3961. {
  3962. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3963. }
  3964. else
  3965. {
  3966. std::unique_ptr<TStrings> Sessions(new TStringList);
  3967. std::unique_ptr<TStrings> Directories(new TStringList);
  3968. TStrings * SessionList = TTerminalManager::Instance()->SessionList;
  3969. for (int Index = 0; Index < SessionList->Count; Index++)
  3970. {
  3971. TManagedTerminal * ASession = DebugNotNull(dynamic_cast<TManagedTerminal *>(SessionList->Objects[Index]));
  3972. if (IsActiveTerminal(ASession) && DebugAlwaysTrue(!ASession->LocalBrowser))
  3973. {
  3974. Sessions->AddObject(SessionList->Strings[Index], ASession);
  3975. Directories->Add(GetSessionPath(ASession, osRemote));
  3976. }
  3977. }
  3978. TDirectRemoteCopy AllowDirectCopy;
  3979. if (Terminal->IsCapable[fcRemoteCopy] || Terminal->CommandSessionOpened)
  3980. {
  3981. DebugAssert(DirectCopy);
  3982. AllowDirectCopy = drcAllow;
  3983. }
  3984. else if (Terminal->IsCapable[fcSecondaryShell])
  3985. {
  3986. DebugAssert(DirectCopy);
  3987. AllowDirectCopy = drcConfirmCommandSession;
  3988. }
  3989. else
  3990. {
  3991. DebugAssert(!DirectCopy);
  3992. AllowDirectCopy = drcDisallow;
  3993. }
  3994. void * ASession = Session;
  3995. Result = DoRemoteCopyDialog(
  3996. Sessions.get(), Directories.get(), AllowDirectCopy, Multi, ASession, Target, FileMask, DirectCopy, Terminal);
  3997. Session = static_cast<TManagedTerminal *>(ASession);
  3998. }
  3999. }
  4000. return Result;
  4001. }
  4002. //---------------------------------------------------------------------------
  4003. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  4004. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  4005. {
  4006. DebugAssert(!IsLocalBrowserMode());
  4007. bool DirectCopy;
  4008. UnicodeString Target, FileMask;
  4009. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  4010. if (Result)
  4011. {
  4012. if (!Move && !DirectCopy)
  4013. {
  4014. Configuration->Usage->Inc("RemoteCopyTemp");
  4015. UnicodeString RootTempDir;
  4016. UnicodeString TempDir;
  4017. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false);
  4018. TStrings * TemporaryFilesList = new TStringList();
  4019. try
  4020. {
  4021. TMakeLocalFileListParams MakeFileListParam;
  4022. MakeFileListParam.FileList = TemporaryFilesList;
  4023. MakeFileListParam.FileTimes = NULL;
  4024. MakeFileListParam.IncludeDirs = true;
  4025. MakeFileListParam.Recursive = false;
  4026. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  4027. TTerminalManager::Instance()->ActiveSession = Session;
  4028. if (TemporaryFilesList->Count > 0)
  4029. {
  4030. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4031. CopyParam.FileMask = FileMask;
  4032. DebugAssert(!FAutoOperation);
  4033. FAutoOperation = true;
  4034. Terminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  4035. }
  4036. }
  4037. __finally
  4038. {
  4039. delete TemporaryFilesList;
  4040. FAutoOperation = false;
  4041. if (!RootTempDir.IsEmpty())
  4042. {
  4043. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  4044. }
  4045. }
  4046. }
  4047. else
  4048. {
  4049. RemoteDirView->SaveSelection();
  4050. RemoteDirView->SaveSelectedNames();
  4051. try
  4052. {
  4053. if (Move)
  4054. {
  4055. Configuration->Usage->Inc("RemoteMove");
  4056. Terminal->MoveFiles(FileList, Target, FileMask);
  4057. }
  4058. else
  4059. {
  4060. Configuration->Usage->Inc("RemoteCopyDirect");
  4061. DebugAssert(DirectCopy);
  4062. DebugAssert(Session == Terminal);
  4063. if (Terminal->IsCapable[fcRemoteCopy] ||
  4064. Terminal->CommandSessionOpened ||
  4065. CommandSessionFallback())
  4066. {
  4067. Terminal->CopyFiles(FileList, Target, FileMask);
  4068. }
  4069. }
  4070. }
  4071. catch(...)
  4072. {
  4073. RemoteDirView->DiscardSavedSelection();
  4074. throw;
  4075. }
  4076. RemoteDirView->RestoreSelection();
  4077. }
  4078. }
  4079. return Result;
  4080. }
  4081. //---------------------------------------------------------------------------
  4082. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  4083. const UnicodeString & Path, TRemoteProperties & Properties)
  4084. {
  4085. DebugAssert(!IsLocalBrowserMode());
  4086. Properties.Valid = Properties.Valid << vpEncrypt;
  4087. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  4088. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  4089. }
  4090. //---------------------------------------------------------------------------
  4091. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  4092. {
  4093. Side = GetSide(Side);
  4094. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  4095. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  4096. UnicodeString Name = LoadStr(NEW_FOLDER);
  4097. int AllowedChanges =
  4098. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  4099. bool SaveSettings = false;
  4100. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  4101. {
  4102. TWindowLock Lock(this);
  4103. if (!IsSideLocalBrowser(Side))
  4104. {
  4105. if (SaveSettings)
  4106. {
  4107. GUIConfiguration->NewDirectoryProperties = Properties;
  4108. }
  4109. CreateRemoteDirectory(Name, Properties);
  4110. }
  4111. else
  4112. {
  4113. DirView(Side)->CreateDirectory(Name);
  4114. }
  4115. }
  4116. }
  4117. //---------------------------------------------------------------------------
  4118. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  4119. {
  4120. TWindowLock Lock(this);
  4121. DirView(Side)->ExecuteHomeDirectory();
  4122. }
  4123. //---------------------------------------------------------------------------
  4124. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  4125. {
  4126. DoOpenDirectoryDialog(odBrowse, Side);
  4127. }
  4128. //---------------------------------------------------------------------------
  4129. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  4130. {
  4131. TOperationSide BookmarkSide = IsLocalBrowserMode() ? osLocal : Side;
  4132. UnicodeString Path = Bookmark->GetSideDirectory(BookmarkSide);
  4133. bool Result = !Path.IsEmpty();
  4134. if (Result)
  4135. {
  4136. TryOpenDirectory(Side, Path);
  4137. }
  4138. return Result;
  4139. }
  4140. //---------------------------------------------------------------------------
  4141. void __fastcall TCustomScpExplorerForm::CalculateSize(
  4142. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  4143. bool & Close)
  4144. {
  4145. // terminal can be already closed (e.g. dropped connection)
  4146. if (Terminal != NULL)
  4147. {
  4148. try
  4149. {
  4150. TCalculateSizeParams Params;
  4151. Params.Stats = &Stats;
  4152. Terminal->CalculateFilesSize(FileList, Size, Params);
  4153. }
  4154. catch(...)
  4155. {
  4156. if (!Terminal->Active)
  4157. {
  4158. Close = true;
  4159. }
  4160. throw;
  4161. }
  4162. }
  4163. }
  4164. //---------------------------------------------------------------------------
  4165. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  4166. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  4167. bool & Close)
  4168. {
  4169. if ((Terminal != NULL) && // terminal can be already closed (e.g. dropped connection)
  4170. EnsureCommandSessionFallback(fcCalculatingChecksum))
  4171. {
  4172. Configuration->Usage->Inc(L"ChecksumCalculated");
  4173. try
  4174. {
  4175. Terminal->CalculateFilesChecksum(Alg, FileList, OnCalculatedChecksum);
  4176. }
  4177. catch(...)
  4178. {
  4179. if (!Terminal->Active)
  4180. {
  4181. Close = true;
  4182. }
  4183. throw;
  4184. }
  4185. }
  4186. }
  4187. //---------------------------------------------------------------------------
  4188. void TCustomScpExplorerForm::LoadFilesProperties(TStrings * FileList)
  4189. {
  4190. if (Terminal->LoadFilesProperties(FileList))
  4191. {
  4192. RemoteDirView->Invalidate();
  4193. }
  4194. }
  4195. //---------------------------------------------------------------------------
  4196. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  4197. {
  4198. bool Result;
  4199. if (!IsSideLocalBrowser(Side))
  4200. {
  4201. TRemoteTokenList * GroupList = NULL;
  4202. TRemoteTokenList * UserList = NULL;
  4203. try
  4204. {
  4205. TRemoteProperties CurrentProperties;
  4206. LoadFilesProperties(FileList);
  4207. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4208. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4209. if (CapableGroupChanging || CapableOwnerChanging)
  4210. {
  4211. if (CapableGroupChanging)
  4212. {
  4213. GroupList = Terminal->Groups->Duplicate();
  4214. }
  4215. if (CapableOwnerChanging)
  4216. {
  4217. UserList = Terminal->Users->Duplicate();
  4218. }
  4219. TRemoteDirectory * Files = Terminal->Files;
  4220. int Count = Files->Count;
  4221. if (Count > 100)
  4222. {
  4223. Count = 100;
  4224. }
  4225. for (int Index = 0; Index < Count; Index++)
  4226. {
  4227. TRemoteFile * File = Files->Files[Index];
  4228. if (CapableGroupChanging)
  4229. {
  4230. GroupList->AddUnique(File->Group);
  4231. }
  4232. if (CapableOwnerChanging)
  4233. {
  4234. UserList->AddUnique(File->Owner);
  4235. }
  4236. }
  4237. // if we haven't collected tokens for all files in current directory,
  4238. // make sure we collect them at least for all selected files.
  4239. // (note that so far the files in FileList has to be from current direcotry)
  4240. if (Count < Files->Count)
  4241. {
  4242. for (int Index = 0; Index < FileList->Count; Index++)
  4243. {
  4244. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4245. if (CapableGroupChanging)
  4246. {
  4247. GroupList->AddUnique(File->Group);
  4248. }
  4249. if (CapableOwnerChanging)
  4250. {
  4251. UserList->AddUnique(File->Owner);
  4252. }
  4253. }
  4254. }
  4255. }
  4256. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4257. bool CapableAclChanging = Terminal->IsCapable[fcAclChangingFiles];
  4258. for (int Index = 0; (Index < FileList->Count) && CapableAclChanging; Index++)
  4259. {
  4260. if (dynamic_cast<TRemoteFile *>(FileList->Objects[Index])->IsDirectory)
  4261. {
  4262. CapableAclChanging = false;
  4263. CurrentProperties.Valid = CurrentProperties.Valid >> vpRights;
  4264. }
  4265. }
  4266. int Flags = 0;
  4267. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4268. if (CapableAclChanging) Flags |= cpAcl;
  4269. if (CapableOwnerChanging) Flags |= cpOwner;
  4270. if (CapableGroupChanging) Flags |= cpGroup;
  4271. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4272. if (Terminal->IsCapable[fcCalculatingChecksum] ||
  4273. (Terminal->IsCapable[fcSecondaryShell] && !Terminal->IsEncryptingFiles()))
  4274. {
  4275. CalculateChecksumEvent = CalculateChecksum;
  4276. }
  4277. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4278. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4279. TRemoteProperties NewProperties = CurrentProperties;
  4280. Result =
  4281. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4282. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4283. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4284. CalculateSize, CalculateChecksumEvent);
  4285. if (Result)
  4286. {
  4287. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4288. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4289. }
  4290. }
  4291. __finally
  4292. {
  4293. delete GroupList;
  4294. delete UserList;
  4295. }
  4296. }
  4297. else
  4298. {
  4299. DirView(Side)->DisplayPropertiesMenu();
  4300. Result = true;
  4301. }
  4302. return Result;
  4303. }
  4304. //---------------------------------------------------------------------------
  4305. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4306. {
  4307. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4308. {
  4309. FIgnoreNextDialogChar = Key;
  4310. }
  4311. Key = 0;
  4312. }
  4313. //---------------------------------------------------------------------------
  4314. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4315. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4316. {
  4317. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4318. if (Command != NULL)
  4319. {
  4320. KeyProcessed(Key, Shift);
  4321. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4322. {
  4323. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4324. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4325. false, false, const_cast<TCustomCommandType *>(Command));
  4326. }
  4327. }
  4328. }
  4329. //---------------------------------------------------------------------------
  4330. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4331. {
  4332. if (!NonVisualDataModule->Busy)
  4333. {
  4334. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4335. {
  4336. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4337. }
  4338. if ((Key != 0) && !DirView(osCurrent)->IsEditing())
  4339. {
  4340. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4341. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4342. {
  4343. // Note that handling shortcuts on panel (current control) popup menu has precendence over this.
  4344. // But so far it's not a problem, as it does what we want.
  4345. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4346. if (((Action->ShortCut == KeyShortCut) ||
  4347. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4348. AllowedAction(Action, aaShortCut))
  4349. {
  4350. // Has to be called before the action as the dialog char is already in queue.
  4351. // So when the action consumes message queue, we already need to have the
  4352. // FIgnoreNextDialogChar set
  4353. KeyProcessed(Key, Shift);
  4354. // Reset reference to previous component (when menu/toolbar was clicked).
  4355. // Needed to detect that action was invoked by keyboard shortcut
  4356. // in TNonVisualDataModule::ExplorerActionsExecute
  4357. Action->ActionComponent = NULL;
  4358. Action->Execute();
  4359. return;
  4360. }
  4361. }
  4362. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4363. {
  4364. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4365. {
  4366. KeyProcessed(Key, Shift);
  4367. TTerminalManager::Instance()->ActiveSessionIndex = i;
  4368. return;
  4369. }
  4370. }
  4371. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4372. {
  4373. KeyProcessed(Key, Shift);
  4374. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4375. }
  4376. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4377. if (IsCustomShortCut(CustomShortCut))
  4378. {
  4379. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4380. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4381. if (WinConfiguration->SharedBookmarks != NULL)
  4382. {
  4383. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4384. if ((Bookmark != NULL) &&
  4385. OpenBookmark(FCurrentSide, Bookmark))
  4386. {
  4387. KeyProcessed(Key, Shift);
  4388. }
  4389. }
  4390. }
  4391. }
  4392. }
  4393. TForm::KeyDown(Key, Shift);
  4394. }
  4395. //---------------------------------------------------------------------------
  4396. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4397. {
  4398. bool OpenedTerminal = false;
  4399. UnicodeString ProtocolName;
  4400. UnicodeString ProtocolNameHint;
  4401. UnicodeString SecurityProtocolName;
  4402. UnicodeString DurationHint;
  4403. // See the comment in TScpCommanderForm::UpdateControls()
  4404. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4405. if (ActiveTerminal != NULL)
  4406. {
  4407. // Active is not enough here
  4408. if (ActiveTerminal->Status == ssOpened)
  4409. {
  4410. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4411. const TFileSystemInfo & FileSystemInfo = ActiveTerminal->GetFileSystemInfo();
  4412. if (FileSystemInfo.ProtocolName.IsEmpty())
  4413. {
  4414. ProtocolName = SessionInfo.ProtocolName;
  4415. }
  4416. else
  4417. {
  4418. ProtocolName = FileSystemInfo.ProtocolName;
  4419. }
  4420. ProtocolNameHint = LoadStr(STATUS_PROTOCOL_HINT);
  4421. SecurityProtocolName = SessionInfo.SecurityProtocolName;
  4422. DurationHint = LoadStr(STATUS_DURATION_HINT);
  4423. OpenedTerminal = true;
  4424. }
  4425. else if (ActiveTerminal->Status > ssClosed)
  4426. {
  4427. ProtocolName = LoadStr(STATUS_CONNECTING);
  4428. }
  4429. else
  4430. {
  4431. ProtocolName = LoadStr(STATUS_NOT_CONNECTED2);
  4432. }
  4433. }
  4434. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4435. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4436. TTBXStatusPanel * ProtocolStatusPanel = SessionStatusBar->Panels->Items[Offset + 0];
  4437. ProtocolStatusPanel->ViewPriority = (ActiveTerminal != NULL) ? 100 : 0;
  4438. if (ProtocolStatusPanel->Caption != ProtocolName)
  4439. {
  4440. ProtocolStatusPanel->Caption = ProtocolName;
  4441. std::unique_ptr<TCanvas> Canvas(CreateControlCanvas(SessionStatusBar));
  4442. ProtocolStatusPanel->Size = Canvas->TextWidth(ProtocolName) + ScaleByTextHeight(SessionStatusBar, 32);
  4443. }
  4444. ProtocolStatusPanel->Hint = ProtocolNameHint;
  4445. TTBXStatusPanel * SecurityStatusPanel = SessionStatusBar->Panels->Items[Offset + 1];
  4446. SecurityStatusPanel->ViewPriority = OpenedTerminal ? 98 : 0;
  4447. bool SecurityEnabled = !SecurityProtocolName.IsEmpty();
  4448. SecurityStatusPanel->Enabled = SecurityEnabled;
  4449. UnicodeString SecurityProtocolHint;
  4450. if (SecurityEnabled)
  4451. {
  4452. SecurityProtocolHint = FMTLOAD(STATUS_SECURE, (SecurityProtocolName));
  4453. }
  4454. else
  4455. {
  4456. SecurityProtocolHint = LoadStr(STATUS_INSECURE);
  4457. }
  4458. SecurityStatusPanel->Hint = SecurityProtocolHint;
  4459. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[Offset + 2];
  4460. DurationStatusPanel->ViewPriority = OpenedTerminal ? 99 : 0;
  4461. DurationStatusPanel->Hint = DurationHint;
  4462. UpdateStatusBar();
  4463. }
  4464. //---------------------------------------------------------------------------
  4465. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4466. {
  4467. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4468. DebugAssert(SessionStatusBar != NULL);
  4469. TTBXStatusPanel * NoteStatusPanel = SessionStatusBar->Panels->Items[0];
  4470. if (!FNote.IsEmpty())
  4471. {
  4472. NoteStatusPanel->Caption = FNote;
  4473. }
  4474. else
  4475. {
  4476. UpdateStatusPanelText(NoteStatusPanel);
  4477. }
  4478. NoteStatusPanel->Hint = FNoteHints;
  4479. UnicodeString LoginTime;
  4480. // See the comment in TScpCommanderForm::UpdateControls()
  4481. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4482. // Active is not enough here
  4483. if ((ActiveTerminal != NULL) && (ActiveTerminal->Status == ssOpened))
  4484. {
  4485. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4486. LoginTime = FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4487. }
  4488. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1];
  4489. DurationStatusPanel->Caption = LoginTime;
  4490. }
  4491. //---------------------------------------------------------------------------
  4492. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4493. {
  4494. Panel->Caption = L"";
  4495. }
  4496. //---------------------------------------------------------------------------
  4497. void __fastcall TCustomScpExplorerForm::Idle()
  4498. {
  4499. if (FShowing ||
  4500. // Particularly for
  4501. // - Standalone editing
  4502. // - Detecting closed connection and automatically reconnect it while waiting for changes
  4503. // while "keeping remote directory up to date".
  4504. StandaloneOperation)
  4505. {
  4506. FEditorManager->Check();
  4507. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4508. // that needs to know if queue view is visible (and it may be closed after queue update)
  4509. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4510. }
  4511. if (FShowing)
  4512. {
  4513. if (!NonVisualDataModule->Busy &&
  4514. // Menu is opened or drag&drop is going on
  4515. (Mouse->Capture == NULL))
  4516. {
  4517. if (FRefreshRemoteDirectory)
  4518. {
  4519. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4520. {
  4521. Terminal->RefreshDirectory();
  4522. }
  4523. FRefreshRemoteDirectory = false;
  4524. }
  4525. if (FRefreshLocalDirectory)
  4526. {
  4527. ReloadLocalDirectory();
  4528. FRefreshLocalDirectory = false;
  4529. }
  4530. if (WinConfiguration->RefreshRemotePanel)
  4531. {
  4532. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4533. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4534. {
  4535. RemoteDirView->ReloadDirectory();
  4536. }
  4537. }
  4538. }
  4539. }
  4540. if (FShowing || StandaloneOperation)
  4541. {
  4542. if (FQueueStatusInvalidated)
  4543. {
  4544. UpdateQueueStatus(false);
  4545. }
  4546. RefreshQueueItems();
  4547. }
  4548. if (FShowing)
  4549. {
  4550. UpdateStatusBar();
  4551. }
  4552. if (FClipboardFakeMonitorsPendingReset && !FDownloadingFromClipboard)
  4553. {
  4554. FClipboardFakeMonitorsPendingReset = false;
  4555. FClipboardFakeMonitors.reset(NULL);
  4556. }
  4557. FIgnoreNextDialogChar = 0;
  4558. }
  4559. //---------------------------------------------------------------------------
  4560. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4561. {
  4562. try
  4563. {
  4564. FUserActionTimer->Enabled = false;
  4565. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4566. {
  4567. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4568. {
  4569. FPendingQueueActionItem->ProcessUserAction();
  4570. }
  4571. }
  4572. }
  4573. __finally
  4574. {
  4575. FPendingQueueActionItem = NULL;
  4576. }
  4577. }
  4578. //---------------------------------------------------------------------------
  4579. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4580. {
  4581. if (WinConfiguration->MinimizeToTray)
  4582. {
  4583. UpdateTrayIcon();
  4584. FTrayIcon->Visible = true;
  4585. if (Visible)
  4586. {
  4587. ShowWindow(Handle, SW_HIDE);
  4588. }
  4589. }
  4590. }
  4591. //---------------------------------------------------------------------------
  4592. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4593. {
  4594. // WORKAROUND
  4595. // When restoring maximized window from minimization,
  4596. // rarely some controls do not align properly.
  4597. // Two instances seen (both for Commander):
  4598. // - When restoring, window is temporarily narrower (not maximized),
  4599. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4600. // Once maximized already, top dock shinks back, but the session PageControl,
  4601. // do not align up, leaving space between TopDock and PageControl.
  4602. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4603. for (int Index = 0; Index < ControlCount; Index++)
  4604. {
  4605. RealignControl(Controls[Index]);
  4606. }
  4607. if (FTrayIcon->Visible)
  4608. {
  4609. FTrayIcon->Visible = false;
  4610. if (Visible)
  4611. {
  4612. ShowWindow(Handle, SW_SHOW);
  4613. }
  4614. }
  4615. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4616. {
  4617. FNeedSession = false;
  4618. NonVisualDataModule->StartBusy();
  4619. try
  4620. {
  4621. NeedSession(true);
  4622. }
  4623. __finally
  4624. {
  4625. NonVisualDataModule->EndBusy();
  4626. }
  4627. }
  4628. }
  4629. //---------------------------------------------------------------------------
  4630. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4631. {
  4632. FTrayIcon->Hint = GetMainForm()->Caption;
  4633. }
  4634. //---------------------------------------------------------------------------
  4635. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4636. {
  4637. UpdateTrayIcon();
  4638. }
  4639. //---------------------------------------------------------------------------
  4640. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4641. {
  4642. // workaround
  4643. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4644. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4645. // But WM_ACTIVATEAPP message can be received even when
  4646. // the main window is minimized to the tray and internal editor window is focused
  4647. // (after another application was previously active)
  4648. if (::IsIconic(Handle))
  4649. {
  4650. if (!IsAppIconic())
  4651. {
  4652. SetAppIconic(true);
  4653. }
  4654. }
  4655. ::ApplicationRestore();
  4656. Application->BringToFront();
  4657. }
  4658. //---------------------------------------------------------------------------
  4659. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4660. {
  4661. RestoreApp();
  4662. }
  4663. //---------------------------------------------------------------------------
  4664. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  4665. {
  4666. if (OpenInNewWindow())
  4667. {
  4668. ExecuteNewInstance(SessionUrl);
  4669. }
  4670. else
  4671. {
  4672. TTerminalManager::Instance()->NewSession(SessionUrl);
  4673. }
  4674. }
  4675. //---------------------------------------------------------------------------
  4676. void TCustomScpExplorerForm::NewTab(TOperationSide)
  4677. {
  4678. NewSession();
  4679. }
  4680. //---------------------------------------------------------------------------
  4681. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4682. {
  4683. UnicodeString SessionName = StoredSessions->HiddenPrefix + ManagedSession->SessionData->SessionName;
  4684. StoredSessions->NewSession(SessionName, SessionData);
  4685. // modified only, explicit
  4686. StoredSessions->Save(false, true);
  4687. // encode session name because of slashes in hierarchical sessions
  4688. return EncodeUrlString(SessionName);
  4689. }
  4690. //---------------------------------------------------------------------------
  4691. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4692. {
  4693. // current working directories become defaults here, what is not right
  4694. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4695. return SaveHiddenDuplicateSession(SessionData.get());
  4696. }
  4697. //---------------------------------------------------------------------------
  4698. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4699. {
  4700. if (OpenInNewWindow())
  4701. {
  4702. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4703. }
  4704. else
  4705. {
  4706. // current working directories become defaults here, what is not right
  4707. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4708. TTerminalManager * Manager = TTerminalManager::Instance();
  4709. TManagedTerminal * ASession = Manager->NewManagedTerminal(SessionData.get());
  4710. DebugAssert(ASession->LocalBrowser == ManagedSession->LocalBrowser);
  4711. // We definitelly want these
  4712. ASession->Disconnected = ManagedSession->Disconnected;
  4713. // Not sure about these two
  4714. ASession->Permanent = ManagedSession->Permanent;
  4715. ASession->DisconnectedTemporarily = ManagedSession->DisconnectedTemporarily;
  4716. Manager->ActiveSession = ASession;
  4717. }
  4718. }
  4719. //---------------------------------------------------------------------------
  4720. void __fastcall TCustomScpExplorerForm::RenameSession()
  4721. {
  4722. UnicodeString Name = ManagedSession->SessionData->SessionName;
  4723. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME) &&
  4724. // When submitting the default name, do not fix it
  4725. (Name != ManagedSession->SessionData->SessionName))
  4726. {
  4727. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4728. TSessionData::ValidateName(Name);
  4729. // This is inconsistent with how color (for example) is handled.
  4730. ManagedSession->SessionData->Name = Name;
  4731. UpdateControls();
  4732. // Add/Remove distinguishing paths from sessions of the same name.
  4733. SessionListChanged();
  4734. }
  4735. }
  4736. //---------------------------------------------------------------------------
  4737. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4738. {
  4739. DebugAssert(Queue != NULL);
  4740. bool Result = Queue->IsEmpty;
  4741. if (!Result)
  4742. {
  4743. SetFocus();
  4744. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4745. }
  4746. return Result;
  4747. }
  4748. //---------------------------------------------------------------------------
  4749. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4750. {
  4751. return CanCloseQueue(FQueue);
  4752. }
  4753. //---------------------------------------------------------------------------
  4754. void __fastcall TCustomScpExplorerForm::CloseSession()
  4755. {
  4756. if (CanCloseQueue())
  4757. {
  4758. TTerminalManager::Instance()->FreeActiveTerminal();
  4759. }
  4760. }
  4761. //---------------------------------------------------------------------------
  4762. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4763. {
  4764. if (CanCloseQueue())
  4765. {
  4766. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4767. }
  4768. }
  4769. //---------------------------------------------------------------------------
  4770. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4771. {
  4772. DetachTerminal(Terminal);
  4773. RemoteDirView->Terminal = Terminal;
  4774. UpdateRemotePathComboBox(false);
  4775. InitStatusBar();
  4776. UpdateControls();
  4777. }
  4778. //---------------------------------------------------------------------------
  4779. void __fastcall TCustomScpExplorerForm::TerminalConnecting()
  4780. {
  4781. InitStatusBar();
  4782. }
  4783. //---------------------------------------------------------------------------
  4784. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4785. {
  4786. if (DebugAlwaysTrue(!Terminal->Active))
  4787. {
  4788. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4789. }
  4790. }
  4791. //---------------------------------------------------------------------------
  4792. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4793. {
  4794. if (OpenInNewWindow())
  4795. {
  4796. // encode session name because of slashes in hierarchical sessions
  4797. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4798. }
  4799. else
  4800. {
  4801. TTerminalManager * Manager = TTerminalManager::Instance();
  4802. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4803. Manager->ActiveSession = Terminal;
  4804. }
  4805. }
  4806. //---------------------------------------------------------------------------
  4807. void TCustomScpExplorerForm::DoOpenFolderOrWorkspace(
  4808. const UnicodeString & Name, bool ConnectFirstTerminal, bool CheckMaxSessions)
  4809. {
  4810. TTerminalManager * Manager = TTerminalManager::Instance();
  4811. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4812. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4813. if (!CheckMaxSessions || (DataList->Count <= WinConfiguration->MaxSessions))
  4814. {
  4815. TManagedTerminal * FirstSession = Manager->NewSessions(DataList.get());
  4816. // FirstSession can be null, if some of the
  4817. if (!ConnectFirstTerminal && (FirstSession != NULL))
  4818. {
  4819. FirstSession->Disconnected = true;
  4820. FirstSession->DisconnectedTemporarily = true;
  4821. }
  4822. Manager->ActiveSession = FirstSession;
  4823. }
  4824. }
  4825. //---------------------------------------------------------------------------
  4826. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4827. {
  4828. if (OpenInNewWindow())
  4829. {
  4830. ExecuteNewInstance(Name);
  4831. }
  4832. else
  4833. {
  4834. DoOpenFolderOrWorkspace(Name, true, false);
  4835. }
  4836. }
  4837. //---------------------------------------------------------------------------
  4838. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4839. bool &CanClose)
  4840. {
  4841. // See the comment in CloseApp()
  4842. if (NonVisualDataModule->Busy)
  4843. {
  4844. CanClose = false;
  4845. }
  4846. else
  4847. {
  4848. TTerminalManager * Manager = TTerminalManager::Instance();
  4849. int ActiveSessions = 0;
  4850. for (int Index = 0; Index < Manager->Count; Index++)
  4851. {
  4852. if (Manager->Sessions[Index]->Active)
  4853. {
  4854. ActiveSessions++;
  4855. }
  4856. }
  4857. // Confirm, when there's at least one active remote session or
  4858. // when there are multiple (inactive or local) sessions and auto workspace saving is not enabled.
  4859. if (((ActiveSessions > 0) || ((Manager->Count > 1) && !WinConfiguration->AutoSaveWorkspace)) &&
  4860. WinConfiguration->ConfirmClosingSession)
  4861. {
  4862. unsigned int Result;
  4863. TMessageParams Params(mpNeverAskAgainCheck);
  4864. UnicodeString Message;
  4865. int Answers = qaOK | qaCancel;
  4866. // The current session is the only active session
  4867. if ((ActiveSessions == 1) && (Terminal != NULL) && Terminal->Active)
  4868. {
  4869. if (!WinConfiguration->AutoSaveWorkspace)
  4870. {
  4871. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  4872. }
  4873. else
  4874. {
  4875. Message = LoadStr(CLOSE_SESSION);
  4876. }
  4877. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  4878. }
  4879. // Multiple active sessions or one active session, but it's not the current one
  4880. else if (ActiveSessions > 0)
  4881. {
  4882. if (!WinConfiguration->AutoSaveWorkspace)
  4883. {
  4884. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  4885. }
  4886. else
  4887. {
  4888. Message = LoadStr(CLOSE_SESSIONS);
  4889. }
  4890. }
  4891. // Multiple tabs, but all are inactive or local, and saving of worspace is not enabled
  4892. else
  4893. {
  4894. DebugAlwaysTrue(Manager->Count > 1);
  4895. DebugAlwaysTrue(!WinConfiguration->AutoSaveWorkspace);
  4896. Message = LoadStr(CLOSE_WORKSPACE);
  4897. }
  4898. UnicodeString Note;
  4899. if (WinConfiguration->AutoSaveWorkspace)
  4900. {
  4901. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  4902. }
  4903. else
  4904. {
  4905. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  4906. Answers = qaYes | qaNo | qaCancel;
  4907. }
  4908. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  4909. SetFocus();
  4910. Result = MessageDialog(Message, qtConfirmation,
  4911. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  4912. if (Result == qaNeverAskAgain)
  4913. {
  4914. WinConfiguration->ConfirmClosingSession = false;
  4915. }
  4916. if (Result == qaNo)
  4917. {
  4918. CanClose = SaveWorkspace(true);
  4919. // note that the workspace will be saved redundatly again from FormClose
  4920. }
  4921. else
  4922. {
  4923. CanClose =
  4924. (Result == qaOK) ||
  4925. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4926. (Result == qaNeverAskAgain);
  4927. }
  4928. }
  4929. if (CanClose && HasActiveTerminal())
  4930. {
  4931. CanClose = CanCloseQueue();
  4932. }
  4933. }
  4934. if (CanClose)
  4935. {
  4936. CanClose =
  4937. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4938. FEditorManager->CloseExternalFilesWithoutProcess();
  4939. if (CanClose)
  4940. {
  4941. while (CanClose && (FLocalEditors->Count > 0))
  4942. {
  4943. int PrevCount = FLocalEditors->Count;
  4944. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4945. CanClose = (FLocalEditors->Count < PrevCount);
  4946. }
  4947. if (CanClose)
  4948. {
  4949. CanClose = FEditorManager->Empty(true);
  4950. if (!CanClose)
  4951. {
  4952. SetFocus();
  4953. CanClose =
  4954. (MessageDialog(
  4955. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4956. }
  4957. }
  4958. }
  4959. }
  4960. }
  4961. //---------------------------------------------------------------------------
  4962. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4963. {
  4964. TForm * Form = dynamic_cast<TForm *>(Sender);
  4965. DebugAssert(Form != NULL);
  4966. Form->Close();
  4967. }
  4968. //---------------------------------------------------------------------------
  4969. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4970. {
  4971. TForm * Form = dynamic_cast<TForm *>(Sender);
  4972. delete Form;
  4973. }
  4974. //---------------------------------------------------------------------------
  4975. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4976. {
  4977. while (FLocalEditors->Count > 0)
  4978. {
  4979. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4980. }
  4981. }
  4982. //---------------------------------------------------------------------------
  4983. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4984. TObject *Sender)
  4985. {
  4986. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4987. try
  4988. {
  4989. SetProperties(osRemote, FileList);
  4990. }
  4991. __finally
  4992. {
  4993. delete FileList;
  4994. }
  4995. }
  4996. //---------------------------------------------------------------------------
  4997. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4998. {
  4999. if (value)
  5000. {
  5001. if (Component == fcCustomCommandsBand)
  5002. {
  5003. UpdateCustomCommandsToolbar();
  5004. }
  5005. if (Component == fcQueueFileList)
  5006. {
  5007. DebugAssert(!QueueFileListSplitter->Visible);
  5008. AdjustQueueLayout();
  5009. UpdateQueueFileList();
  5010. }
  5011. }
  5012. }
  5013. //---------------------------------------------------------------------------
  5014. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  5015. {
  5016. int MinHeightLimit =
  5017. GetStaticQueuePanelComponentsHeight() +
  5018. QueueFileListSplitter->Height +
  5019. QueueFileList->Height +
  5020. GetMinQueueViewHeight();
  5021. if (QueuePanel->ClientHeight < MinHeightLimit)
  5022. {
  5023. QueuePanel->ClientHeight = MinHeightLimit;
  5024. }
  5025. }
  5026. //---------------------------------------------------------------------------
  5027. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  5028. {
  5029. TControl * Control = GetComponent(Component);
  5030. DebugAssert(Control);
  5031. bool Changed = (Control->Visible != value);
  5032. if (Changed)
  5033. {
  5034. ComponentShowing(Component, value);
  5035. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  5036. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  5037. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  5038. if (value)
  5039. {
  5040. int RemainingHeight = Control->Parent->ClientHeight;
  5041. int RemainingWidth = Control->Parent->ClientWidth;
  5042. for (int i = 0; i < Control->Parent->ControlCount; i++)
  5043. {
  5044. TControl * ChildControl = Control->Parent->Controls[i];
  5045. if (ChildControl->Visible)
  5046. {
  5047. switch (ChildControl->Align)
  5048. {
  5049. case alTop:
  5050. case alBottom:
  5051. RemainingHeight -= ChildControl->Height;
  5052. break;
  5053. case alLeft:
  5054. case alRight:
  5055. RemainingWidth -= ChildControl->Width;
  5056. break;
  5057. }
  5058. }
  5059. }
  5060. int Reserve = ScaleByTextHeight(this, 32);
  5061. // queue in explorer, trees in commander
  5062. if (Control->Height > RemainingHeight - Reserve)
  5063. {
  5064. Control->Height = RemainingHeight / 2;
  5065. }
  5066. if (Control->Width > RemainingWidth - Reserve)
  5067. {
  5068. Control->Width = RemainingWidth / 2;
  5069. }
  5070. }
  5071. Control->Visible = value;
  5072. if (WasFocused && Visible)
  5073. {
  5074. DirView(osCurrent)->SetFocus();
  5075. }
  5076. FixControlsPlacement();
  5077. }
  5078. }
  5079. //---------------------------------------------------------------------------
  5080. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  5081. {
  5082. TControl * Control = GetComponent(Component);
  5083. if (Control == NULL)
  5084. {
  5085. return false;
  5086. }
  5087. else
  5088. {
  5089. return Control->Visible;
  5090. }
  5091. }
  5092. //---------------------------------------------------------------------------
  5093. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  5094. {
  5095. bool Result;
  5096. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  5097. {
  5098. Result = !IsUWP();
  5099. }
  5100. else
  5101. {
  5102. Result = true;
  5103. }
  5104. return Result;
  5105. }
  5106. //---------------------------------------------------------------------------
  5107. void __fastcall TCustomScpExplorerForm::MakeFocusedItemVisible(TCustomDirView * DirView)
  5108. {
  5109. if (DirView->ItemFocused != NULL)
  5110. {
  5111. DirView->ItemFocused->MakeVisible(false);
  5112. }
  5113. }
  5114. //---------------------------------------------------------------------------
  5115. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  5116. {
  5117. MakeFocusedItemVisible(RemoteDirView);
  5118. QueueSplitter->Visible = QueuePanel->Visible;
  5119. QueueFileListSplitter->Visible = QueueFileList->Visible;
  5120. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  5121. TControl * QueueControlsOrder[] =
  5122. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  5123. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  5124. }
  5125. //---------------------------------------------------------------------------
  5126. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  5127. {
  5128. switch (Component) {
  5129. case fcStatusBar: return RemoteStatusBar;
  5130. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  5131. case fcQueueView: return QueuePanel;
  5132. case fcQueueToolbar: return QueueDock;
  5133. case fcRemoteTree: return RemoteDrivePanel;
  5134. case fcSessionsTabs: return SessionsPageControl;
  5135. case fcQueueFileList: return QueueFileList;
  5136. default: return NULL;
  5137. }
  5138. }
  5139. //---------------------------------------------------------------------------
  5140. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  5141. TObject *Sender, TListColumn *Column, TPoint &Point)
  5142. {
  5143. DebugAssert(NonVisualDataModule && Column && Sender);
  5144. NonVisualDataModule->ListColumn = Column;
  5145. TPopupMenu * DirViewColumnMenu;
  5146. if (Sender == DirView(osOther))
  5147. {
  5148. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  5149. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  5150. (Column->Index == uvName);
  5151. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  5152. (Column->Index == uvSize);
  5153. }
  5154. else
  5155. {
  5156. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  5157. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  5158. (Column->Index == dvName);
  5159. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  5160. (Column->Index == dvSize);
  5161. }
  5162. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  5163. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  5164. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  5165. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  5166. }
  5167. //---------------------------------------------------------------------------
  5168. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  5169. TObject *Sender, TListItem *Item, bool &AllowExec)
  5170. {
  5171. DoDirViewExecFile(Sender, Item, AllowExec);
  5172. }
  5173. //---------------------------------------------------------------------------
  5174. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  5175. TListItem * Item, bool & AllowExec)
  5176. {
  5177. DebugAssert(Sender && Item && Configuration);
  5178. DebugAssert(AllowExec);
  5179. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  5180. TOperationSide Side = ((ADirView == DirView(osRemote)) ? osRemote : osLocal);
  5181. bool Remote = !IsSideLocalBrowser(Side);
  5182. DebugAssert(Item == ADirView->ItemFocused);
  5183. bool IsDirectory = ADirView->ItemIsDirectory(Item);
  5184. TResolvedDoubleClickAction Action;
  5185. if (FForceExecution)
  5186. {
  5187. Action = IsDirectory ? rdcaChangeDir : rdcaOpen;
  5188. }
  5189. else
  5190. {
  5191. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  5192. Action = WinConfiguration->ResolveDoubleClickAction(IsDirectory, ATerminal);
  5193. }
  5194. if (Action == rdcaCopy)
  5195. {
  5196. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  5197. UnlockWindow();
  5198. try
  5199. {
  5200. if (IsLocalBrowserMode())
  5201. {
  5202. LocalLocalCopy(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation, false, 0);
  5203. }
  5204. else
  5205. {
  5206. ExecuteFileOperation(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  5207. }
  5208. }
  5209. __finally
  5210. {
  5211. LockWindow();
  5212. }
  5213. AllowExec = false;
  5214. }
  5215. else if (Action == rdcaEdit)
  5216. {
  5217. if (!Remote || !WinConfiguration->DisableOpenEdit)
  5218. {
  5219. ExecuteFile(Side, efDefaultEditor);
  5220. AllowExec = false;
  5221. }
  5222. }
  5223. else if (Action == rdcaOpen)
  5224. {
  5225. if (DebugAlwaysTrue(!IsDirectory) && Remote && !WinConfiguration->DisableOpenEdit)
  5226. {
  5227. ExecuteFile(osRemote, efShell);
  5228. AllowExec = false;
  5229. }
  5230. }
  5231. }
  5232. //---------------------------------------------------------------------------
  5233. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5234. {
  5235. return ((Side == osRemote) || (Side == osCurrent));
  5236. }
  5237. //---------------------------------------------------------------------------
  5238. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5239. {
  5240. DebugFail();
  5241. }
  5242. //---------------------------------------------------------------------------
  5243. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5244. {
  5245. DebugFail();
  5246. }
  5247. //---------------------------------------------------------------------------
  5248. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5249. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5250. {
  5251. TSynchronizeParamType Params;
  5252. Params.LocalDirectory = LocalDirectory;
  5253. Params.RemoteDirectory = RemoteDirectory;
  5254. int UnusedParams =
  5255. (GUIConfiguration->SynchronizeParams &
  5256. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5257. TTerminal::spCaseSensitive));
  5258. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5259. Params.Options = GUIConfiguration->SynchronizeOptions;
  5260. bool SaveSettings = false;
  5261. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5262. &DoSynchronizeTooManyDirectories);
  5263. DebugAssert(FSynchronizeController == NULL);
  5264. FSynchronizeController = &Controller;
  5265. bool Result;
  5266. try
  5267. {
  5268. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5269. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5270. int Options =
  5271. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5272. DebugAssert(FOnFeedSynchronizeError == NULL);
  5273. DebugAssert(FOnSynchronizeAbort == NULL);
  5274. DebugAssert(FSynchronizeTerminal == NULL);
  5275. FSynchronizeTerminal = Terminal;
  5276. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5277. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5278. FOnFeedSynchronizeError, FOnSynchronizeAbort, SynchronizeInNewWindow, UseDefaults);
  5279. if (Result)
  5280. {
  5281. if (SaveSettings)
  5282. {
  5283. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5284. GUIConfiguration->SynchronizeOptions = Params.Options;
  5285. }
  5286. else
  5287. {
  5288. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5289. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5290. FLAGSET(Params.Options, soSynchronize))
  5291. {
  5292. GUIConfiguration->SynchronizeOptions =
  5293. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5294. soSynchronize;
  5295. }
  5296. }
  5297. LocalDirectory = Params.LocalDirectory;
  5298. RemoteDirectory = Params.RemoteDirectory;
  5299. }
  5300. }
  5301. __finally
  5302. {
  5303. FSynchronizeController = NULL;
  5304. DebugAssert(FOnFeedSynchronizeError == NULL);
  5305. FOnFeedSynchronizeError = NULL;
  5306. DebugAssert(FOnSynchronizeAbort == NULL);
  5307. FOnSynchronizeAbort = NULL;
  5308. FSynchronizeTerminal = NULL;
  5309. }
  5310. return Result;
  5311. }
  5312. //---------------------------------------------------------------------------
  5313. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5314. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5315. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5316. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5317. TSynchronizeOptions * Options, bool Full)
  5318. {
  5319. // Keep-up-to-date mode
  5320. if (Terminal->Status == ssOpened)
  5321. {
  5322. try
  5323. {
  5324. int PParams = Params.Params;
  5325. if (!Full)
  5326. {
  5327. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5328. if (FLAGSET(Params.Options, soRecurse))
  5329. {
  5330. PParams |= TTerminal::spSubDirs;
  5331. }
  5332. }
  5333. else
  5334. {
  5335. // if keepuptodate is non-recursive,
  5336. // full sync before has to be non-recursive as well
  5337. if (FLAGCLEAR(Params.Options, soRecurse))
  5338. {
  5339. PParams |= TTerminal::spNoRecurse;
  5340. }
  5341. }
  5342. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5343. PParams, Checklist, Options);
  5344. }
  5345. catch (Exception & E)
  5346. {
  5347. if (!Terminal->Active)
  5348. {
  5349. ShowExtendedExceptionEx(Terminal, &E);
  5350. // Do not abort the "keep up to date" when the session was succesfully reconnected.
  5351. if (!Terminal->Active)
  5352. {
  5353. throw;
  5354. }
  5355. }
  5356. else
  5357. {
  5358. if (FLAGSET(Params.Options, soContinueOnError) &&
  5359. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5360. {
  5361. // noop, already logged in MoreMessageDialog
  5362. }
  5363. else
  5364. {
  5365. // We get mostly EAbort here, so this is noop.
  5366. // Exception is an error when listing directories while looking for differences.
  5367. ShowExtendedExceptionEx(Terminal, &E);
  5368. throw;
  5369. }
  5370. }
  5371. }
  5372. }
  5373. }
  5374. //---------------------------------------------------------------------------
  5375. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5376. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5377. const UnicodeString ErrorStr)
  5378. {
  5379. if (!Directory.IsEmpty())
  5380. {
  5381. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5382. }
  5383. else
  5384. {
  5385. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5386. }
  5387. }
  5388. //---------------------------------------------------------------------------
  5389. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5390. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5391. {
  5392. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5393. {
  5394. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5395. }
  5396. else
  5397. {
  5398. TMessageParams Params(mpNeverAskAgainCheck);
  5399. unsigned int Result = MessageDialog(
  5400. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5401. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5402. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5403. {
  5404. MaxDirectories *= 2;
  5405. if (Result == qaNeverAskAgain)
  5406. {
  5407. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5408. }
  5409. }
  5410. else
  5411. {
  5412. Abort();
  5413. }
  5414. }
  5415. }
  5416. //---------------------------------------------------------------------------
  5417. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5418. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5419. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5420. TSynchronizeOptions * Options)
  5421. {
  5422. // Keep-up-to-date mode
  5423. DebugAssert(!FAutoOperation);
  5424. void * BatchStorage;
  5425. BatchStart(BatchStorage);
  5426. FAutoOperation = true;
  5427. bool AnyOperation = false;
  5428. TDateTime StartTime = Now();
  5429. TSynchronizeChecklist * AChecklist = NULL;
  5430. try
  5431. {
  5432. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5433. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5434. {
  5435. FSynchronizeProgressForm->Start();
  5436. }
  5437. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5438. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5439. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5440. Options);
  5441. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5442. SAFE_DESTROY(FSynchronizeProgressForm);
  5443. AnyOperation = (AChecklist->CheckedCount > 0);
  5444. if (AnyOperation)
  5445. {
  5446. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5447. CreateProgressForm(&SynchronizeProgress);
  5448. try
  5449. {
  5450. Terminal->SynchronizeApply(
  5451. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5452. SynchronizeProcessedItem, NULL, NULL, NULL);
  5453. }
  5454. __finally
  5455. {
  5456. DestroyProgressForm();
  5457. }
  5458. }
  5459. }
  5460. __finally
  5461. {
  5462. if (Checklist == NULL)
  5463. {
  5464. delete AChecklist;
  5465. }
  5466. else
  5467. {
  5468. *Checklist = AChecklist;
  5469. }
  5470. FAutoOperation = false;
  5471. SAFE_DESTROY(FSynchronizeProgressForm);
  5472. BatchEnd(BatchStorage);
  5473. ReloadLocalDirectory();
  5474. if (AnyOperation)
  5475. {
  5476. OperationComplete(StartTime);
  5477. }
  5478. }
  5479. }
  5480. //---------------------------------------------------------------------------
  5481. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5482. {
  5483. // can be called from command line
  5484. return Visible &&
  5485. ((DirView(osRemote)->SelCount > 0) ||
  5486. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5487. }
  5488. //---------------------------------------------------------------------------
  5489. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5490. {
  5491. LogSynchronizeEvent(Terminal, Message);
  5492. }
  5493. //---------------------------------------------------------------------------
  5494. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5495. int Params, TSynchronizeOptions & Options)
  5496. {
  5497. DebugAssert(!IsLocalBrowserMode());
  5498. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5499. {
  5500. Options.Filter = new TStringList();
  5501. Options.Filter->CaseSensitive = false;
  5502. Options.Filter->Duplicates = Types::dupAccept;
  5503. if (DirView(osRemote)->SelCount > 0)
  5504. {
  5505. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5506. }
  5507. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5508. {
  5509. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5510. }
  5511. Options.Filter->Sort();
  5512. }
  5513. }
  5514. //---------------------------------------------------------------------------
  5515. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5516. {
  5517. TCopyParamType Defaults;
  5518. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5519. ConfigStorage->AccessMode = smRead;
  5520. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5521. {
  5522. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5523. }
  5524. ConfigStorage.reset(NULL);
  5525. std::unique_ptr<TStringList> Options(new TStringList());
  5526. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5527. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5528. UnicodeString Result;
  5529. if (Options->Count > 0)
  5530. {
  5531. Result =
  5532. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5533. }
  5534. return Result;
  5535. }
  5536. //---------------------------------------------------------------------------
  5537. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5538. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5539. {
  5540. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5541. UnicodeString AdditionalParams =
  5542. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5543. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5544. SerializeCopyParamForCommandLine(CopyParams),
  5545. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5546. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5547. ExecuteNewInstance(SessionName, AdditionalParams);
  5548. }
  5549. //---------------------------------------------------------------------------
  5550. struct TSynchronizeParams
  5551. {
  5552. UnicodeString LocalDirectory;
  5553. UnicodeString RemoteDirectory;
  5554. TSynchronizeMode Mode;
  5555. TSynchronizeChecklist * Checklist;
  5556. int Params;
  5557. TCopyParamType * CopyParam;
  5558. TDateTime CollectElapsed;
  5559. TDateTime * StartTime;
  5560. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5561. };
  5562. //---------------------------------------------------------------------------
  5563. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5564. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5565. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5566. {
  5567. DebugAssert(!FAutoOperation);
  5568. void * BatchStorage;
  5569. BatchStart(BatchStorage);
  5570. FAutoOperation = true;
  5571. TFileOperationStatistics Statistics;
  5572. TDateTime Start = Now();
  5573. try
  5574. {
  5575. Params.OnProcessedItem = OnProcessedItem;
  5576. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5577. CreateProgressForm(&SynchronizeProgress);
  5578. Terminal->SynchronizeApply(
  5579. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5580. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5581. &Statistics);
  5582. }
  5583. __finally
  5584. {
  5585. Params.OnProcessedItem = NULL;
  5586. FAutoOperation = false;
  5587. DestroyProgressForm();
  5588. BatchEnd(BatchStorage);
  5589. ReloadLocalDirectory();
  5590. }
  5591. if (WinConfiguration->SynchronizeSummary)
  5592. {
  5593. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5594. // The statistics should be 0 anyway in this case
  5595. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5596. {
  5597. Message += L"\n";
  5598. if (Statistics.FilesUploaded > 0)
  5599. {
  5600. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5601. }
  5602. if (Statistics.FilesDownloaded > 0)
  5603. {
  5604. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5605. }
  5606. if (Statistics.FilesDeletedLocal > 0)
  5607. {
  5608. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5609. }
  5610. if (Statistics.FilesDeletedRemote > 0)
  5611. {
  5612. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5613. }
  5614. }
  5615. TDateTime Elapsed = (Now() - Start);
  5616. Message +=
  5617. L"\n" +
  5618. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5619. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5620. TMessageParams Params(mpNeverAskAgainCheck);
  5621. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5622. if (Result == qaNeverAskAgain)
  5623. {
  5624. WinConfiguration->SynchronizeSummary = false;
  5625. }
  5626. }
  5627. }
  5628. //---------------------------------------------------------------------------
  5629. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5630. {
  5631. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5632. {
  5633. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5634. }
  5635. // Not set in keep-up-to-date mode - Synchronize() method
  5636. if (Token != NULL)
  5637. {
  5638. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5639. if (Params.OnProcessedItem != NULL)
  5640. {
  5641. Params.OnProcessedItem(NULL, ChecklistItem);
  5642. }
  5643. }
  5644. }
  5645. //---------------------------------------------------------------------------
  5646. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5647. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5648. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5649. {
  5650. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5651. *Params.StartTime = Now();
  5652. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5653. }
  5654. //---------------------------------------------------------------------------
  5655. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5656. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5657. {
  5658. // terminal can be already closed (e.g. dropped connection)
  5659. if (Terminal != NULL)
  5660. {
  5661. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5662. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5663. }
  5664. }
  5665. //---------------------------------------------------------------------------
  5666. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5667. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5668. {
  5669. DebugAssert(!IsLocalBrowserMode());
  5670. TAutoBatch AutoBatch(this);
  5671. TAutoFlag AutoOperationFlag(FAutoOperation);
  5672. if (Side == osRemote)
  5673. {
  5674. std::unique_ptr<TStrings> FileList(new TStringList());
  5675. FileList->AddObject(FileName, RemoteFile);
  5676. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5677. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5678. RemoteDirView->SaveSelection();
  5679. RemoteDirView->SaveSelectedNames();
  5680. try
  5681. {
  5682. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5683. }
  5684. catch(...)
  5685. {
  5686. RemoteDirView->DiscardSavedSelection();
  5687. throw;
  5688. }
  5689. RemoteDirView->RestoreSelection();
  5690. }
  5691. else if (DebugAlwaysTrue(Side == osLocal))
  5692. {
  5693. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5694. {
  5695. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5696. }
  5697. UnicodeString Directory = ExtractFileDir(FileName);
  5698. ReloadLocalDirectory(Directory);
  5699. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5700. if (!SamePaths(Directory, NewDirectory))
  5701. {
  5702. ReloadLocalDirectory(NewDirectory);
  5703. }
  5704. }
  5705. }
  5706. //---------------------------------------------------------------------------
  5707. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5708. {
  5709. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5710. if (Side == osLocal)
  5711. {
  5712. if (Action == TSynchronizeChecklist::saDownloadNew)
  5713. {
  5714. OpenFolderInExplorer(LocalPath);
  5715. }
  5716. else
  5717. {
  5718. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5719. }
  5720. }
  5721. else if (DebugAlwaysTrue(Side == osRemote))
  5722. {
  5723. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5724. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5725. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5726. if (!LocalPath.IsEmpty())
  5727. {
  5728. SessionData->LocalDirectory = LocalPath;
  5729. }
  5730. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5731. ExecuteNewInstance(SessionName, FORMAT(L"%s=\"%s\"", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5732. }
  5733. }
  5734. //---------------------------------------------------------------------------
  5735. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5736. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5737. const TCopyParamType * CopyParams)
  5738. {
  5739. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5740. UnicodeString AdditionalParams =
  5741. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5742. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5743. SerializeCopyParamForCommandLine(CopyParams),
  5744. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5745. LocalDirectory, RemoteDirectory, Mode, Params));
  5746. ExecuteNewInstance(SessionName, AdditionalParams);
  5747. }
  5748. //---------------------------------------------------------------------------
  5749. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5750. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5751. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5752. {
  5753. int Result;
  5754. bool SaveSettings = false;
  5755. int Options =
  5756. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5757. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5758. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5759. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5760. bool Continue =
  5761. (UseDefaults == 0) ||
  5762. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5763. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5764. if (Continue)
  5765. {
  5766. Configuration->Usage->Inc(L"Synchronizations");
  5767. CopyParam.IncludeFileMask.SetRoots(LocalDirectory, RemoteDirectory);
  5768. UpdateCopyParamCounters(CopyParam);
  5769. TSynchronizeOptions SynchronizeOptions;
  5770. GetSynchronizeOptions(Params, SynchronizeOptions);
  5771. if (SaveSettings)
  5772. {
  5773. GUIConfiguration->SynchronizeParams = Params;
  5774. }
  5775. else
  5776. {
  5777. SaveMode = false;
  5778. }
  5779. TDateTime StartTime = Now();
  5780. TSynchronizeChecklist * Checklist = NULL;
  5781. try
  5782. {
  5783. DebugAssert(!FAutoOperation);
  5784. FAutoOperation = true;
  5785. try
  5786. {
  5787. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5788. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5789. int Files = -1;
  5790. if (DataList->Count >= 1)
  5791. {
  5792. Files = StrToIntDef(DataList->Strings[0], Files);
  5793. }
  5794. else
  5795. {
  5796. DataList->Add(UnicodeString());
  5797. }
  5798. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5799. FSynchronizeProgressForm->Start();
  5800. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5801. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5802. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5803. &SynchronizeOptions);
  5804. if (Terminal->SessionData->CacheDirectories)
  5805. {
  5806. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5807. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5808. }
  5809. }
  5810. __finally
  5811. {
  5812. FAutoOperation = false;
  5813. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5814. SAFE_DESTROY(FSynchronizeProgressForm);
  5815. }
  5816. TSynchronizeParams SynchronizeParams;
  5817. SynchronizeParams.LocalDirectory = LocalDirectory;
  5818. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5819. SynchronizeParams.Mode = Mode;
  5820. SynchronizeParams.CopyParam = &CopyParam;
  5821. SynchronizeParams.Params = Params;
  5822. SynchronizeParams.Checklist = Checklist;
  5823. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5824. SynchronizeParams.StartTime = &StartTime;
  5825. SynchronizeParams.OnProcessedItem = NULL;
  5826. Result = Checklist->Count;
  5827. if (Checklist->Count > 0)
  5828. {
  5829. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5830. {
  5831. if (!DoSynchronizeChecklistDialog(
  5832. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5833. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  5834. {
  5835. Result = -1;
  5836. }
  5837. }
  5838. else
  5839. {
  5840. FullSynchronize(SynchronizeParams, NULL, NULL);
  5841. }
  5842. }
  5843. else
  5844. {
  5845. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5846. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5847. }
  5848. }
  5849. __finally
  5850. {
  5851. delete Checklist;
  5852. }
  5853. OperationComplete(StartTime);
  5854. }
  5855. else
  5856. {
  5857. Result = -1;
  5858. }
  5859. return Result;
  5860. }
  5861. //---------------------------------------------------------------------------
  5862. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5863. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5864. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5865. {
  5866. if (Collect)
  5867. {
  5868. DebugAssert(FSynchronizeProgressForm != NULL);
  5869. if (!FSynchronizeProgressForm->Started)
  5870. {
  5871. FSynchronizeProgressForm->Start();
  5872. }
  5873. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5874. SetTaskbarListProgressValue(CompareProgress);
  5875. }
  5876. else
  5877. {
  5878. DebugAssert(FSynchronizeProgressForm == NULL);
  5879. DebugAssert(FProgressForm != NULL);
  5880. }
  5881. }
  5882. //---------------------------------------------------------------------------
  5883. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5884. {
  5885. UnicodeString FullFileName = AbsolutePath(Terminal->CurrentDirectory, FileName);
  5886. TRemoteFile * File = NULL;
  5887. Terminal->ReadFile(FullFileName, File);
  5888. if (File != NULL)
  5889. {
  5890. std::unique_ptr<TRemoteFile> FileOwner(File);
  5891. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5892. Application->ShowMainForm = false;
  5893. Application->Run();
  5894. }
  5895. }
  5896. //---------------------------------------------------------------------------
  5897. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory(TOperationSide)
  5898. {
  5899. DebugFail();
  5900. }
  5901. //---------------------------------------------------------------------------
  5902. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5903. {
  5904. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5905. SessionData->Assign(ManagedSession->SessionData);
  5906. UpdateSessionData(SessionData.get());
  5907. DebugAssert(SessionData->IsLocalBrowser == IsLocalBrowserMode());
  5908. if (IsActiveTerminal(ManagedSession))
  5909. {
  5910. ManagedSession->UpdateSessionCredentials(SessionData.get());
  5911. }
  5912. if (!ManagedSession->SessionData->HasSessionName())
  5913. {
  5914. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5915. // once IsWorkspace is cleared
  5916. SessionData->Name = UnicodeString();
  5917. }
  5918. // Most uses of this method ends with saving the session, and we do not want to store the IsWorkspace flag.
  5919. // Particularly, with SaveHiddenDuplicateSession, it won't work as TSessionData::ParseUrl explicitly ignored workspace sessions.
  5920. SessionData->IsWorkspace = false;
  5921. return SessionData.release();
  5922. }
  5923. //---------------------------------------------------------------------------
  5924. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5925. {
  5926. TSessionData * SessionData = CloneCurrentSessionData();
  5927. try
  5928. {
  5929. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5930. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5931. }
  5932. __finally
  5933. {
  5934. delete SessionData;
  5935. }
  5936. }
  5937. //---------------------------------------------------------------------------
  5938. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5939. {
  5940. TTerminalManager * Manager = TTerminalManager::Instance();
  5941. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5942. if (DebugAlwaysTrue(ManagedSession != NULL))
  5943. {
  5944. // Update ManagedSession->StateData
  5945. UpdateSession(ManagedSession);
  5946. }
  5947. Manager->SaveWorkspace(DataList.get());
  5948. return DataList.release();
  5949. }
  5950. //---------------------------------------------------------------------------
  5951. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5952. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5953. {
  5954. WinConfiguration->LastWorkspace = Name;
  5955. if (!SavePasswords)
  5956. {
  5957. for (int Index = 0; Index < DataList->Count; Index++)
  5958. {
  5959. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5960. if (SessionData->Link.IsEmpty())
  5961. {
  5962. SessionData->ClearSessionPasswords();
  5963. }
  5964. }
  5965. }
  5966. StoredSessions->NewWorkspace(Name, DataList);
  5967. // modified only
  5968. StoredSessions->Save(false, Explicit);
  5969. }
  5970. //---------------------------------------------------------------------------
  5971. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5972. {
  5973. return
  5974. DefaultStr(WinConfiguration->LastWorkspace,
  5975. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5976. }
  5977. //---------------------------------------------------------------------------
  5978. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5979. {
  5980. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5981. bool AnyNonStoredSessionWithPassword = false;
  5982. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5983. bool AllNonStoredSessionsAnonymous = true;
  5984. TTerminalManager * Manager = TTerminalManager::Instance();
  5985. for (int Index = 0; Index < DataList->Count; Index++)
  5986. {
  5987. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5988. if (SessionData->Link.IsEmpty())
  5989. {
  5990. if (SessionData->HasAnySessionPassword())
  5991. {
  5992. AnyNonStoredSessionWithPassword = true;
  5993. if (!Manager->Sessions[Index]->SessionData->IsWorkspace)
  5994. {
  5995. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5996. }
  5997. }
  5998. if (!SameText(SessionData->UserName, AnonymousUserName))
  5999. {
  6000. AllNonStoredSessionsAnonymous = false;
  6001. }
  6002. }
  6003. }
  6004. bool SavePasswords;
  6005. bool * PSavePasswords;
  6006. bool NotRecommendedSavingPasswords =
  6007. !CustomWinConfiguration->UseMasterPassword &&
  6008. !AllNonStoredSessionsAnonymous;
  6009. if (Configuration->DisablePasswordStoring ||
  6010. !AnyNonStoredSessionWithPassword)
  6011. {
  6012. PSavePasswords = NULL;
  6013. SavePasswords = false;
  6014. }
  6015. else
  6016. {
  6017. PSavePasswords = &SavePasswords;
  6018. SavePasswords =
  6019. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  6020. !NotRecommendedSavingPasswords;
  6021. }
  6022. UnicodeString Name = WorkspaceName();
  6023. bool CreateShortcut = false;
  6024. bool Result =
  6025. DoSaveWorkspaceDialog(
  6026. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  6027. EnableAutoSave);
  6028. if (Result)
  6029. {
  6030. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  6031. if (CreateShortcut)
  6032. {
  6033. TOperationVisualizer Visualizer;
  6034. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  6035. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  6036. }
  6037. if (EnableAutoSave)
  6038. {
  6039. WinConfiguration->AutoSaveWorkspace = true;
  6040. WinConfiguration->AutoWorkspace = Name;
  6041. if (PSavePasswords != NULL)
  6042. {
  6043. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  6044. }
  6045. }
  6046. }
  6047. return Result;
  6048. }
  6049. //---------------------------------------------------------------------------
  6050. void __fastcall TCustomScpExplorerForm::UpdateSession(TManagedTerminal * Session)
  6051. {
  6052. DebugAssert(IsLocalBrowserMode() == Session->LocalBrowser);
  6053. DebugAssert(Session == ManagedSession);
  6054. if (!IsLocalBrowserMode())
  6055. {
  6056. SAFE_DESTROY(Session->RemoteExplorerState);
  6057. if (WinConfiguration->PreservePanelState)
  6058. {
  6059. Session->RemoteExplorerState = RemoteDirView->SaveState();
  6060. }
  6061. }
  6062. UpdateSessionData(Session->StateData);
  6063. }
  6064. //---------------------------------------------------------------------------
  6065. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  6066. {
  6067. // Keep in sync with TSessionData::CopyStateData
  6068. DebugAssert(Data != NULL);
  6069. // This is inconsistent with how color (for example) is handled.
  6070. Data->Name = ManagedSession->SessionData->Name;
  6071. if (!IsLocalBrowserMode())
  6072. {
  6073. // cannot use RemoteDirView->Path, because it is empty if connection
  6074. // was already closed
  6075. // also only peek, we may not be connected at all atm
  6076. // so make sure we do not try retrieving current directory from the server
  6077. // (particularly with FTP)
  6078. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  6079. if (!ACurrentDirectory.IsEmpty())
  6080. {
  6081. Data->RemoteDirectory = ACurrentDirectory;
  6082. }
  6083. }
  6084. Data->Color = SessionColor;
  6085. }
  6086. //---------------------------------------------------------------------------
  6087. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  6088. {
  6089. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  6090. DebugAssert(Toolbar != NULL);
  6091. for (int i = 0; i < Toolbar->Items->Count; i++)
  6092. {
  6093. TTBXCustomDropDownItem * DropDownItem;
  6094. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6095. if (DropDownItem != NULL)
  6096. {
  6097. ToolbarItemResize(DropDownItem,
  6098. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  6099. Toolbar->NonClientWidth);
  6100. break;
  6101. }
  6102. }
  6103. }
  6104. //---------------------------------------------------------------------------
  6105. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  6106. {
  6107. Item->EditWidth = Width;
  6108. }
  6109. //---------------------------------------------------------------------------
  6110. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  6111. TTBCustomToolbar * Toolbar, TPoint & ASize)
  6112. {
  6113. for (int i = 0; i < Toolbar->Items->Count; i++)
  6114. {
  6115. TTBXCustomDropDownItem * DropDownItem;
  6116. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6117. if (DropDownItem != NULL)
  6118. {
  6119. ASize.x -= DropDownItem->EditWidth;
  6120. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  6121. }
  6122. }
  6123. }
  6124. //---------------------------------------------------------------------------
  6125. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  6126. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  6127. {
  6128. if (WinConfiguration->DDWarnLackOfTempSpace)
  6129. {
  6130. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  6131. if (!ADrive.IsEmpty())
  6132. {
  6133. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  6134. DebugAssert(RequiredSpace >= 0);
  6135. __int64 RequiredWithReserve;
  6136. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  6137. if (FreeSpace < RequiredWithReserve)
  6138. {
  6139. unsigned int Result;
  6140. TMessageParams Params(mpNeverAskAgainCheck);
  6141. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  6142. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  6143. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  6144. if (Result == qaNeverAskAgain)
  6145. {
  6146. WinConfiguration->DDWarnLackOfTempSpace = false;
  6147. }
  6148. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  6149. }
  6150. }
  6151. }
  6152. }
  6153. //---------------------------------------------------------------------------
  6154. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  6155. {
  6156. DoOpenDirectoryDialog(odAddBookmark, Side);
  6157. }
  6158. //---------------------------------------------------------------------------
  6159. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  6160. {
  6161. // we should better use TCustomDirView::FCaseSensitive, but it is private
  6162. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  6163. try
  6164. {
  6165. TCustomDirView * DView = DirView(Side);
  6166. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  6167. {
  6168. VisitedDirectories->Add(DView->HistoryPath[Index]);
  6169. }
  6170. }
  6171. catch (...)
  6172. {
  6173. delete VisitedDirectories;
  6174. throw;
  6175. }
  6176. return VisitedDirectories;
  6177. }
  6178. //---------------------------------------------------------------------------
  6179. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  6180. TOpenDirectoryMode Mode, TOperationSide Side)
  6181. {
  6182. bool Continue = true;
  6183. if (Mode == odAddBookmark)
  6184. {
  6185. TMessageParams Params(mpNeverAskAgainCheck);
  6186. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  6187. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  6188. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  6189. unsigned int Answer =
  6190. MessageDialog(Message,
  6191. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  6192. if (Answer == qaNeverAskAgain)
  6193. {
  6194. Continue = true;
  6195. WinConfiguration->UseSharedBookmarks = true;
  6196. }
  6197. else if (Answer == qaOK)
  6198. {
  6199. Continue = true;
  6200. WinConfiguration->UseSharedBookmarks = false;
  6201. }
  6202. else
  6203. {
  6204. Continue = false;
  6205. }
  6206. }
  6207. if (Continue)
  6208. {
  6209. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  6210. try
  6211. {
  6212. UnicodeString Name = DirView(Side)->PathName;
  6213. bool Repeat;
  6214. do
  6215. {
  6216. Repeat = false;
  6217. TOperationSide BookmarkSide = IsSideLocalBrowser(Side) ? osLocal : osRemote;
  6218. if (::DoOpenDirectoryDialog(Mode, BookmarkSide, Name, VisitedDirectories, Terminal,
  6219. // do not allow switching to location profiles,
  6220. // if we are not connected
  6221. HasDirView[osLocal] && (Terminal != NULL)))
  6222. {
  6223. TWindowLock Lock(this);
  6224. if (!TryOpenDirectory(Side, Name))
  6225. {
  6226. Repeat = true;
  6227. Mode = odBrowse;
  6228. }
  6229. }
  6230. }
  6231. while (Repeat);
  6232. }
  6233. __finally
  6234. {
  6235. delete VisitedDirectories;
  6236. }
  6237. }
  6238. }
  6239. //---------------------------------------------------------------------------
  6240. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6241. {
  6242. DebugAssert(!Terminal->CommandSessionOpened);
  6243. return TTerminalManager::Instance()->ConnectTerminal(Terminal->CommandSession);
  6244. }
  6245. //---------------------------------------------------------------------------
  6246. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6247. {
  6248. bool Result = Terminal->IsCapable[Capability] ||
  6249. Terminal->CommandSessionOpened;
  6250. if (!Result)
  6251. {
  6252. DebugAssert(Terminal->IsCapable[fcSecondaryShell]);
  6253. if (!GUIConfiguration->ConfirmCommandSession)
  6254. {
  6255. Result = true;
  6256. }
  6257. else
  6258. {
  6259. TMessageParams Params(mpNeverAskAgainCheck);
  6260. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6261. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6262. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6263. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6264. if (Answer == qaNeverAskAgain)
  6265. {
  6266. GUIConfiguration->ConfirmCommandSession = false;
  6267. Result = true;
  6268. }
  6269. else if (Answer == qaOK)
  6270. {
  6271. Result = true;
  6272. }
  6273. }
  6274. if (Result)
  6275. {
  6276. Result = CommandSessionFallback();
  6277. }
  6278. }
  6279. return Result;
  6280. }
  6281. //---------------------------------------------------------------------------
  6282. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6283. {
  6284. if (EnsureCommandSessionFallback(fcAnyCommand))
  6285. {
  6286. DoConsoleDialog(Terminal, Command);
  6287. }
  6288. }
  6289. //---------------------------------------------------------------------------
  6290. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6291. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6292. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6293. {
  6294. if (!IsSideLocalBrowser(osRemote) &&
  6295. IsFileControl(DropSourceControl, osRemote) &&
  6296. (FDDExtMapFile != NULL))
  6297. {
  6298. Accept = true;
  6299. }
  6300. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6301. DebugAssert(FDDTargetControl != NULL);
  6302. }
  6303. //---------------------------------------------------------------------------
  6304. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6305. _di_IDataObject DataObj, int KeyState,
  6306. const TPoint & Point, int & Effect, bool & Accept)
  6307. {
  6308. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6309. }
  6310. //---------------------------------------------------------------------------
  6311. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6312. _di_IDataObject DataObj, int KeyState,
  6313. const TPoint & Point, int & Effect, bool & Accept)
  6314. {
  6315. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6316. }
  6317. //---------------------------------------------------------------------------
  6318. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6319. TObject * Sender)
  6320. {
  6321. DebugUsedParam(Sender);
  6322. DebugAssert(FDDTargetControl == Sender);
  6323. FDDTargetControl = NULL;
  6324. }
  6325. //---------------------------------------------------------------------------
  6326. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  6327. {
  6328. FileControlDDDragLeave(SessionsPageControl);
  6329. }
  6330. //---------------------------------------------------------------------------
  6331. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  6332. {
  6333. FileControlDDDragLeave(QueueView3);
  6334. }
  6335. //---------------------------------------------------------------------------
  6336. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6337. {
  6338. DebugAssert(!IsSideLocalBrowser(Side));
  6339. DebugUsedParam(Side);
  6340. bool Edit = false;
  6341. TRemoteFile * File = NULL;
  6342. UnicodeString FileName;
  6343. UnicodeString PointTo;
  6344. bool SymbolicLink = true;
  6345. if (RemoteDirView->ItemFocused)
  6346. {
  6347. DebugAssert(RemoteDirView->ItemFocused->Data);
  6348. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6349. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6350. if (Edit)
  6351. {
  6352. FileName = File->FileName;
  6353. PointTo = File->LinkTo;
  6354. }
  6355. else if (!File->IsParentDirectory)
  6356. {
  6357. PointTo = File->FileName;
  6358. }
  6359. }
  6360. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6361. Terminal->IsCapable[fcHardLink]))
  6362. {
  6363. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6364. if (Edit)
  6365. {
  6366. DebugAssert(File->FileName == FileName);
  6367. int Params = dfNoRecursive;
  6368. Terminal->ExceptionOnFail = true;
  6369. try
  6370. {
  6371. Terminal->DeleteFile(L"", File, &Params);
  6372. }
  6373. __finally
  6374. {
  6375. Terminal->ExceptionOnFail = false;
  6376. }
  6377. }
  6378. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6379. }
  6380. }
  6381. //---------------------------------------------------------------------------
  6382. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6383. {
  6384. return
  6385. (IsSideLocalBrowser(Side) ||
  6386. (HasActiveTerminal() &&
  6387. Terminal->ResolvingSymlinks &&
  6388. Terminal->IsCapable[fcSymbolicLink]));
  6389. }
  6390. //---------------------------------------------------------------------------
  6391. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6392. {
  6393. return
  6394. !IsSideLocalBrowser(FCurrentSide) &&
  6395. (RemoteDirView->ItemFocused != NULL) &&
  6396. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6397. Terminal->SessionData->ResolveSymlinks;
  6398. }
  6399. //---------------------------------------------------------------------------
  6400. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6401. {
  6402. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6403. FForceExecution = true;
  6404. try
  6405. {
  6406. DirView(osCurrent)->ExecuteCurrentFile();
  6407. }
  6408. __finally
  6409. {
  6410. FForceExecution = false;
  6411. }
  6412. }
  6413. //---------------------------------------------------------------------------
  6414. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6415. {
  6416. TEditorData ExternalEditor;
  6417. ExternalEditor.Editor = edExternal;
  6418. bool Remember = false;
  6419. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6420. {
  6421. if (Remember)
  6422. {
  6423. TEditorList * EditorList = new TEditorList;
  6424. try
  6425. {
  6426. *EditorList = *WinConfiguration->EditorList;
  6427. bool Found = false;
  6428. int i = 0;
  6429. while (!Found && (i < EditorList->Count))
  6430. {
  6431. const TEditorPreferences * Editor = EditorList->Editors[i];
  6432. if ((Editor->Data->Editor == edExternal) &&
  6433. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6434. {
  6435. Found = true;
  6436. }
  6437. i++;
  6438. }
  6439. if (!Found)
  6440. {
  6441. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6442. WinConfiguration->EditorList = EditorList;
  6443. }
  6444. }
  6445. __finally
  6446. {
  6447. delete EditorList;
  6448. }
  6449. }
  6450. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6451. }
  6452. }
  6453. //---------------------------------------------------------------------------
  6454. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6455. {
  6456. FEditorManager->ProcessFiles(FileTerminalRemoved, ATerminal);
  6457. if (FFileFindTerminal == ATerminal)
  6458. {
  6459. FFileFindTerminal = NULL;
  6460. HideFileFindDialog();
  6461. }
  6462. if (FClipboardTerminal == ATerminal)
  6463. {
  6464. ClipboardClear(); // implies ClipboardStop
  6465. }
  6466. if ((FOnSynchronizeAbort != NULL) &&
  6467. (ATerminal == FManagedSession))
  6468. {
  6469. FOnSynchronizeAbort(NULL);
  6470. }
  6471. }
  6472. //---------------------------------------------------------------------------
  6473. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6474. {
  6475. DetachTerminal(Sender);
  6476. }
  6477. //---------------------------------------------------------------------------
  6478. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6479. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6480. {
  6481. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6482. DebugAssert(Terminal != NULL);
  6483. if (Data->Terminal == Terminal)
  6484. {
  6485. Data->Terminal = NULL;
  6486. }
  6487. }
  6488. //---------------------------------------------------------------------------
  6489. TManagedTerminal * TCustomScpExplorerForm::GetReplacementForLastSession()
  6490. {
  6491. return NULL;
  6492. }
  6493. //---------------------------------------------------------------------------
  6494. void __fastcall TCustomScpExplorerForm::LastTerminalClosed()
  6495. {
  6496. UpdateControls();
  6497. SessionColor = TColor(0);
  6498. UpdateRemotePathComboBox(false);
  6499. NeedSession(false);
  6500. }
  6501. //---------------------------------------------------------------------------
  6502. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6503. {
  6504. try
  6505. {
  6506. // Cache, as the login dialog can change its value.
  6507. // See also DoShow. Also see TTerminalManager::NewSession.
  6508. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6509. try
  6510. {
  6511. if (ShowLogin)
  6512. {
  6513. bool ReloadSessions = !Startup;
  6514. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this, true);
  6515. }
  6516. }
  6517. __finally
  6518. {
  6519. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6520. // (so there was no chance for the user to open any session yet)
  6521. if ((ShowLogin || !Startup) &&
  6522. !WinConfiguration->KeepOpenWhenNoSession &&
  6523. // this rules out the implicit Commander's local-local browser
  6524. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6525. {
  6526. TerminateApplication();
  6527. }
  6528. }
  6529. }
  6530. catch (EAbort &)
  6531. {
  6532. // swallow
  6533. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6534. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6535. }
  6536. }
  6537. //---------------------------------------------------------------------------
  6538. void __fastcall TCustomScpExplorerForm::SessionListChanged(bool ForceTruncationUpdate)
  6539. {
  6540. TTerminalManager * Manager = TTerminalManager::Instance();
  6541. DebugAssert(!Manager->Updating);
  6542. TAutoFlag UpdatingSessionTabsFlag(FUpdatingSessionTabs);
  6543. int ActiveSessionIndex = Manager->ActiveSessionIndex;
  6544. Configuration->Usage->SetMax(L"MaxOpenedSessions", Manager->Count);
  6545. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6546. try
  6547. {
  6548. int PrevTabsWidth = SessionsPageControl->TotalTabsWidth();
  6549. while (SessionsPageControl->PageCount > Manager->Count + 1)
  6550. {
  6551. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6552. }
  6553. for (int Index = 0; Index <= Manager->Count; Index++)
  6554. {
  6555. TThemeTabSheet * TabSheet;
  6556. if (Index >= SessionsPageControl->PageCount)
  6557. {
  6558. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6559. TabSheet->PageControl = SessionsPageControl;
  6560. }
  6561. else
  6562. {
  6563. TabSheet = SessionsPageControl->Pages[Index];
  6564. }
  6565. bool IsSessionTab = (Index < Manager->Count);
  6566. if (IsSessionTab)
  6567. {
  6568. TTerminal * Terminal = Manager->Sessions[Index];
  6569. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6570. TabSheet->CaptionTruncation = tttEllipsis;
  6571. UpdateSessionTab(TabSheet);
  6572. }
  6573. else
  6574. {
  6575. TabSheet->Tag = 0; // not really needed
  6576. TabSheet->Shadowed = false;
  6577. TabSheet->Button = SupportsLocalBrowser() ? ttbDropDown : ttbNone;
  6578. TabSheet->CaptionTruncation = tttNoText;
  6579. // 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)
  6580. UpdateNewTabTab();
  6581. }
  6582. }
  6583. // Prevent flicker on the right side where scrolling buttons would be, unless really needed
  6584. if (ForceTruncationUpdate || (SessionsPageControl->TotalTabsWidth() != PrevTabsWidth))
  6585. {
  6586. SessionsPageControl->UpdateTabsCaptionTruncation();
  6587. }
  6588. }
  6589. __finally
  6590. {
  6591. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6592. }
  6593. SessionsPageControl->ActivePageIndex = ActiveSessionIndex;
  6594. }
  6595. //---------------------------------------------------------------------------
  6596. UnicodeString TCustomScpExplorerForm::GetNewTabTabCaption()
  6597. {
  6598. return StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewTabAction->Caption));
  6599. }
  6600. //---------------------------------------------------------------------------
  6601. void __fastcall TCustomScpExplorerForm::UpdateNewTabTab()
  6602. {
  6603. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6604. UnicodeString TabCaption;
  6605. if (WinConfiguration->SelectiveToolbarText)
  6606. {
  6607. TabCaption = GetNewTabTabCaption();
  6608. }
  6609. TabSheet->BaseCaption = TabCaption;
  6610. TabSheet->ImageIndex = GetNewTabTabImageIndex(osCurrent);
  6611. }
  6612. //---------------------------------------------------------------------------
  6613. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabSession(TTabSheet * TabSheet)
  6614. {
  6615. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6616. }
  6617. //---------------------------------------------------------------------------
  6618. UnicodeString TCustomScpExplorerForm::GetLocalBrowserSessionTitle(TManagedTerminal *)
  6619. {
  6620. DebugFail();
  6621. return UnicodeString();
  6622. }
  6623. //---------------------------------------------------------------------------
  6624. void TCustomScpExplorerForm::UpdateSessionTab(TThemeTabSheet * TabSheet)
  6625. {
  6626. if (DebugAlwaysTrue(TabSheet != NULL))
  6627. {
  6628. TManagedTerminal * ASession = GetSessionTabSession(TabSheet);
  6629. if (DebugAlwaysTrue(ASession != NULL))
  6630. {
  6631. TColor Color = (ASession == Terminal) ? FSessionColor : ASession->StateData->Color;
  6632. if (ASession->LocalBrowser)
  6633. {
  6634. TabSheet->ImageIndex = FLocalBrowserTabImageIndex;
  6635. }
  6636. else
  6637. {
  6638. TabSheet->ImageIndex = AddSessionColor(Color);
  6639. }
  6640. TTerminalManager * Manager = TTerminalManager::Instance();
  6641. TabSheet->Shadowed = !ASession->Active && !ASession->LocalBrowser;
  6642. TabSheet->Button = CanCloseSession(ASession) ? ttbClose : ttbNone;
  6643. TabSheet->BaseCaption = Manager->GetSessionTitle(ASession, true);
  6644. }
  6645. }
  6646. }
  6647. //---------------------------------------------------------------------------
  6648. bool TCustomScpExplorerForm::CanCloseSession(TManagedTerminal * Session)
  6649. {
  6650. return !Session->LocalBrowser || (TTerminalManager::Instance()->Count > 1);
  6651. }
  6652. //---------------------------------------------------------------------------
  6653. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6654. {
  6655. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6656. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->ActivePage);
  6657. bool Result = (Session != NULL);
  6658. if (Result)
  6659. {
  6660. TTerminalManager::Instance()->ActiveSession = Session;
  6661. }
  6662. else
  6663. {
  6664. try
  6665. {
  6666. NewTab();
  6667. }
  6668. __finally
  6669. {
  6670. SessionListChanged();
  6671. }
  6672. FSessionsPageControlNewTabTime = Now();
  6673. }
  6674. return Result;
  6675. }
  6676. //---------------------------------------------------------------------------
  6677. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6678. {
  6679. TWindowLock Lock(this);
  6680. SessionTabSwitched();
  6681. }
  6682. //---------------------------------------------------------------------------
  6683. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6684. {
  6685. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6686. DebugAssert(TransferList != NULL);
  6687. UnicodeString Name;
  6688. if (TransferList->ItemIndex <= 0)
  6689. {
  6690. Name = L"";
  6691. }
  6692. else
  6693. {
  6694. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6695. }
  6696. if (FCopyParamAutoSelected.IsEmpty())
  6697. {
  6698. // if previous preset was not autoselected, make new preset the "default"
  6699. FCopyParamDefault = Name;
  6700. }
  6701. GUIConfiguration->CopyParamCurrent = Name;
  6702. }
  6703. //---------------------------------------------------------------------------
  6704. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6705. {
  6706. // sanity check
  6707. bool ExistingPreset =
  6708. (FTransferListHoverIndex >= 0) &&
  6709. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6710. DebugAssert(ExistingPreset);
  6711. if (ExistingPreset)
  6712. {
  6713. HDC DC = GetDC(0);
  6714. TCanvas * Canvas = new TCanvas();
  6715. try
  6716. {
  6717. Canvas->Handle = DC;
  6718. Canvas->Font = GetToolbarFont(this);
  6719. UnicodeString Name;
  6720. if (FTransferListHoverIndex == 0)
  6721. {
  6722. Name = L"";
  6723. }
  6724. else
  6725. {
  6726. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6727. }
  6728. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6729. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6730. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6731. static_cast<TObject*>(GetComponent(fcTransferList)));
  6732. UnicodeString InfoStr =
  6733. GUIConfiguration->CopyParamPreset[Name].
  6734. GetInfoStr(L"; ",
  6735. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  6736. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6737. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6738. {
  6739. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6740. {
  6741. InfoStr.SetLength(InfoStr.Length() - 1);
  6742. }
  6743. InfoStr += Ellipsis;
  6744. }
  6745. // UpdateCaption does not cause invalidation of whole submenu, while
  6746. // setting Caption property does.
  6747. // also it probably does not resize the label, even if necessary
  6748. // (we do not want that anyway)
  6749. TransferLabel->UpdateCaption(InfoStr);
  6750. }
  6751. __finally
  6752. {
  6753. Canvas->Handle = NULL;
  6754. ReleaseDC(0, DC);
  6755. delete Canvas;
  6756. }
  6757. }
  6758. }
  6759. //---------------------------------------------------------------------------
  6760. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6761. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6762. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6763. {
  6764. if (FTransferListHoverIndex != AHoverIndex)
  6765. {
  6766. FTransferListHoverIndex = AHoverIndex;
  6767. UpdateTransferLabel();
  6768. }
  6769. }
  6770. //---------------------------------------------------------------------------
  6771. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6772. {
  6773. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6774. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6775. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6776. {
  6777. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6778. {
  6779. if (!NonVisualDataModule->Busy)
  6780. {
  6781. OpenDirectory(GetSide(osCurrent));
  6782. }
  6783. Message.Result = 1;
  6784. }
  6785. else
  6786. {
  6787. TForm::Dispatch(&Message);
  6788. }
  6789. }
  6790. else
  6791. {
  6792. TForm::Dispatch(&Message);
  6793. }
  6794. }
  6795. //---------------------------------------------------------------------------
  6796. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6797. {
  6798. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6799. if ((FIgnoreNextDialogChar != 0) &&
  6800. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6801. {
  6802. Message.Result = 1;
  6803. }
  6804. else
  6805. {
  6806. TForm::Dispatch(&Message);
  6807. }
  6808. FIgnoreNextDialogChar = 0;
  6809. }
  6810. //---------------------------------------------------------------------------
  6811. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6812. {
  6813. // The four low-order bits are used internally by Windows
  6814. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6815. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6816. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6817. // 61730 - restore thru double click - undocumented
  6818. // 61490 - maximize thru double click - undocumented
  6819. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6820. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6821. {
  6822. SysResizing(Cmd);
  6823. }
  6824. TForm::Dispatch(&Message);
  6825. }
  6826. //---------------------------------------------------------------------------
  6827. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6828. {
  6829. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6830. // Also there no point returning TRUE as we are not able to
  6831. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6832. // Hence the process termination might be safer :)
  6833. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6834. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6835. {
  6836. Message.Result = FALSE;
  6837. }
  6838. else
  6839. {
  6840. Message.Result = TRUE;
  6841. }
  6842. // Do not call default handling as that triggers OnCloseQuery,
  6843. // where our implementation will popup confirmation dialogs, what we do not want,
  6844. // as per Vista guidelines:
  6845. // https://docs.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6846. }
  6847. //---------------------------------------------------------------------------
  6848. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6849. {
  6850. if (Message.EndSession && IsApplicationMinimized())
  6851. {
  6852. // WORKAROUND
  6853. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6854. // when it receives WM_ENDSESSION,
  6855. // but that sometimes (particularly when application is minimized) cause crashes.
  6856. // Crash popups message that blocks log off.
  6857. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6858. // so we call ExitProcess() immediately, not even trying to cleanup.
  6859. // It still causes beep, so there's likely some popup, but it does not block
  6860. // log off.
  6861. ExitProcess(0);
  6862. }
  6863. TForm::Dispatch(&Message);
  6864. }
  6865. #ifdef _DEBUG
  6866. //---------------------------------------------------------------------------
  6867. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6868. {
  6869. TForm::Dispatch(&Message);
  6870. }
  6871. #endif
  6872. //---------------------------------------------------------------------------
  6873. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6874. {
  6875. }
  6876. //---------------------------------------------------------------------------
  6877. void __fastcall TCustomScpExplorerForm::DoShow()
  6878. {
  6879. // Called from TCustomScpExplorerForm::CMShowingChanged
  6880. // only now are the controls resized finally, so the size constraints
  6881. // will not conflict with possibly very small window size
  6882. RestoreFormParams();
  6883. // This indicates that the form was rescaled after parameters were loaded and
  6884. // as most probably the parameters were actually saved with the current form scaling
  6885. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6886. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6887. // and no rounding problems will occur.
  6888. // See also RestoreForm.
  6889. // (we should be safe to call RestoreParams unconditionally)
  6890. if (PixelsPerInch != Screen->PixelsPerInch)
  6891. {
  6892. RestoreParams();
  6893. }
  6894. FixControlsPlacement();
  6895. if (Position == poDefaultPosOnly)
  6896. {
  6897. CutFormToDesktop(this);
  6898. }
  6899. TForm::DoShow();
  6900. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6901. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6902. // This was previously in NeedSession, where other related code is.
  6903. // But that is called from CMShowingChanged, when the window is already visible and the local-local window state
  6904. // unpleasantly shows briefly.
  6905. if ((ManagedSession == NULL) &&
  6906. !WinConfiguration->ShowLoginWhenNoSession &&
  6907. WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  6908. // This detects if workspace was saved the last time the main widow was closed
  6909. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  6910. {
  6911. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false, true);
  6912. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  6913. }
  6914. // Before main window displays
  6915. if (ManagedSession == NULL)
  6916. {
  6917. StartingWithoutSession();
  6918. }
  6919. UpdatePixelsPerInchMainWindowCounter();
  6920. FShowing = true;
  6921. }
  6922. //---------------------------------------------------------------------------
  6923. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6924. {
  6925. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6926. }
  6927. //---------------------------------------------------------------------------
  6928. void __fastcall TCustomScpExplorerForm::StartingWithoutSession()
  6929. {
  6930. SessionListChanged();
  6931. InitStatusBar();
  6932. UpdateControls();
  6933. }
  6934. //---------------------------------------------------------------------------
  6935. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6936. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6937. TNotifyEvent OnBalloonClick, TObject * UserData)
  6938. {
  6939. bool Do;
  6940. UnicodeString Message;
  6941. if (E == NULL)
  6942. {
  6943. Message = Str;
  6944. Do = true;
  6945. }
  6946. else
  6947. {
  6948. Do = ExceptionMessage(E, Message);
  6949. }
  6950. if (Do && WinConfiguration->BalloonNotifications)
  6951. {
  6952. UnicodeString Title;
  6953. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6954. {
  6955. Message = TrimLeft(Message);
  6956. }
  6957. else
  6958. {
  6959. Message = UnformatMessage(Message);
  6960. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6961. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6962. Title = LoadResourceString(Captions[Type]);
  6963. if (Terminal != NULL)
  6964. {
  6965. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  6966. Title = TTerminalManager::Instance()->GetSessionTitle(Session, true) + TitleSeparator + Title;
  6967. }
  6968. }
  6969. if (Seconds == 0)
  6970. {
  6971. Seconds = WinConfiguration->NotificationsTimeout;
  6972. }
  6973. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6974. }
  6975. }
  6976. //---------------------------------------------------------------------------
  6977. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6978. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6979. UnicodeString HelpKeyword, const TMessageParams * Params,
  6980. TTerminal * Terminal)
  6981. {
  6982. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6983. (FOnFeedSynchronizeError != NULL)) &&
  6984. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6985. {
  6986. if (FOnFeedSynchronizeError != NULL)
  6987. {
  6988. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6989. }
  6990. else
  6991. {
  6992. DebugAssert(FErrorList != NULL);
  6993. TStringList * MoreMessagesCopy = NULL;
  6994. if (MoreMessages)
  6995. {
  6996. MoreMessagesCopy = new TStringList();
  6997. MoreMessagesCopy->Assign(MoreMessages);
  6998. }
  6999. FErrorList->AddObject(Message, MoreMessagesCopy);
  7000. }
  7001. PopupTrayBalloon(Terminal, Message, Type);
  7002. return ContinueAnswer(Answers);
  7003. }
  7004. else
  7005. {
  7006. bool UseBalloon = FTrayIcon->Visible;
  7007. if (UseBalloon)
  7008. {
  7009. PopupTrayBalloon(Terminal, Message, Type);
  7010. }
  7011. unsigned int Result;
  7012. try
  7013. {
  7014. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  7015. }
  7016. __finally
  7017. {
  7018. // cancel only balloon we popped up, otherwise we may cancel notification
  7019. // balloon that was there before the message dialog
  7020. // (such as "dd confirmation opt in balloon)
  7021. if (UseBalloon)
  7022. {
  7023. FTrayIcon->CancelBalloon();
  7024. }
  7025. }
  7026. return Result;
  7027. }
  7028. }
  7029. //---------------------------------------------------------------------------
  7030. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  7031. TTerminal * Terminal, Exception * E)
  7032. {
  7033. if (FTrayIcon->Visible)
  7034. {
  7035. PopupTrayBalloon(Terminal, L"", qtError, E);
  7036. }
  7037. // particularly prevent opening new session from jump list,
  7038. // while exception is shown
  7039. NonVisualDataModule->StartBusy();
  7040. try
  7041. {
  7042. ShowExtendedExceptionEx(Terminal, E);
  7043. }
  7044. __finally
  7045. {
  7046. NonVisualDataModule->EndBusy();
  7047. FTrayIcon->CancelBalloon();
  7048. }
  7049. }
  7050. //---------------------------------------------------------------------------
  7051. void __fastcall TCustomScpExplorerForm::SessionReady()
  7052. {
  7053. InitStatusBar();
  7054. // cannot rely on active page being page for active terminal,
  7055. // as it can happen that active page is the "new session" page
  7056. // (e.g. when reconnecting active terminal, while login dialog
  7057. // invoked from "new session" page is modal)
  7058. int ActiveSessionIndex = TTerminalManager::Instance()->ActiveSessionIndex;
  7059. UpdateSessionTab(SessionsPageControl->Pages[ActiveSessionIndex]);
  7060. }
  7061. //---------------------------------------------------------------------------
  7062. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  7063. TTerminal * Terminal, Exception * E)
  7064. {
  7065. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  7066. if (!Terminal->Active)
  7067. {
  7068. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  7069. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  7070. {
  7071. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  7072. }
  7073. }
  7074. }
  7075. //---------------------------------------------------------------------------
  7076. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  7077. UnicodeString Message, TQueryType Type,
  7078. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  7079. {
  7080. if ((E == NULL) ||
  7081. ExceptionMessage(E, Message))
  7082. {
  7083. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  7084. if (Important)
  7085. {
  7086. Seconds *= 5;
  7087. }
  7088. UnicodeString NoteMessage(UnformatMessage(Message));
  7089. if (Terminal != NULL)
  7090. {
  7091. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  7092. NoteMessage = FORMAT(L"%s: %s",
  7093. (TTerminalManager::Instance()->GetSessionTitle(Session, true), NoteMessage));
  7094. }
  7095. if (WinConfiguration->BalloonNotifications)
  7096. {
  7097. AddNote(NoteMessage);
  7098. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  7099. }
  7100. else
  7101. {
  7102. FlashOnBackground();
  7103. PostNote(NoteMessage, Seconds, OnClick, UserData);
  7104. }
  7105. }
  7106. }
  7107. //---------------------------------------------------------------------------
  7108. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  7109. {
  7110. RestoreApp();
  7111. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  7112. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  7113. !NonVisualDataModule->Busy)
  7114. {
  7115. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  7116. TTerminalManager::Instance()->ActiveSession = Terminal;
  7117. if (!ComponentVisible[fcQueueView])
  7118. {
  7119. ToggleQueueVisibility();
  7120. GoToQueue();
  7121. }
  7122. }
  7123. }
  7124. //---------------------------------------------------------------------------
  7125. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  7126. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  7127. {
  7128. UnicodeString Message;
  7129. TNotifyEvent OnClick = NULL;
  7130. TObject * UserData = NULL;
  7131. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  7132. switch (Event)
  7133. {
  7134. case qeEmptyButMonitored:
  7135. if ((ATerminal != Terminal) || QueueInvisible)
  7136. {
  7137. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  7138. OnClick = QueueEmptyNoteClicked;
  7139. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  7140. TerminalNoteData->Terminal = ATerminal;
  7141. UserData = TerminalNoteData;
  7142. }
  7143. break;
  7144. case qeEmpty:
  7145. OperationComplete(ATerminal->QueueOperationStart);
  7146. break;
  7147. case qePendingUserAction:
  7148. if ((ATerminal != Terminal) ||
  7149. (QueueInvisible && !IsQueueAutoPopup()))
  7150. {
  7151. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  7152. }
  7153. break;
  7154. default:
  7155. DebugFail();
  7156. }
  7157. if (!Message.IsEmpty())
  7158. {
  7159. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  7160. }
  7161. }
  7162. //---------------------------------------------------------------------------
  7163. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  7164. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  7165. {
  7166. if (FDDExtMapFile != NULL)
  7167. {
  7168. CloseHandle(FDDExtMapFile);
  7169. FDDExtMapFile = NULL;
  7170. }
  7171. if (WinConfiguration->DDFakeFile)
  7172. {
  7173. DDFakeFileInitDrag(FileList, Created);
  7174. }
  7175. }
  7176. //---------------------------------------------------------------------------
  7177. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  7178. {
  7179. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  7180. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  7181. {
  7182. FFakeFileDropTarget = FileName;
  7183. }
  7184. }
  7185. //---------------------------------------------------------------------------
  7186. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  7187. {
  7188. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  7189. if (!ForceDirectories(ApiPath(Result)))
  7190. {
  7191. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  7192. }
  7193. FileSetAttr(ApiPath(Result), faHidden);
  7194. return Result;
  7195. }
  7196. //---------------------------------------------------------------------------
  7197. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  7198. bool & Created)
  7199. {
  7200. FFakeFileDropTarget = UnicodeString();
  7201. FDragFakeDirectory = CreateFakeTransferDirectory();
  7202. FileList->AddItem(NULL, FDragFakeDirectory);
  7203. Created = true;
  7204. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  7205. {
  7206. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  7207. }
  7208. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  7209. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  7210. {
  7211. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7212. TDragExtCommStruct* CommStruct;
  7213. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7214. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7215. DebugAssert(CommStruct != NULL);
  7216. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  7217. CommStruct->Dragging = true;
  7218. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  7219. LENOF(CommStruct->DropDest));
  7220. NULL_TERMINATE(CommStruct->DropDest);
  7221. UnmapViewOfFile(CommStruct);
  7222. }
  7223. }
  7224. //---------------------------------------------------------------------------
  7225. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  7226. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  7227. {
  7228. bool Result;
  7229. DebugAssert(!IsLocalBrowserMode());
  7230. if (Sender == RemoteDirView)
  7231. {
  7232. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  7233. }
  7234. else
  7235. {
  7236. DebugAssert(Sender == RemoteDriveView);
  7237. TStrings * FileList = RemoteDriveView->DragFileList();
  7238. try
  7239. {
  7240. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  7241. }
  7242. __finally
  7243. {
  7244. delete FileList;
  7245. }
  7246. }
  7247. if (FDDTargetControl == RemoteDriveView)
  7248. {
  7249. RemoteDriveView->UpdateDropTarget();
  7250. }
  7251. // foRemoteMove happens when file/dir is dragged within the remote tree view
  7252. // or from tree view to dir view.
  7253. // foMove happens when file/dir is dragged from remote tree view outside of
  7254. // application via dragex
  7255. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  7256. (DropSourceControl == RemoteDriveView))
  7257. {
  7258. RemoteDriveView->UpdateDropSource();
  7259. }
  7260. return Result;
  7261. }
  7262. //---------------------------------------------------------------------------
  7263. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  7264. {
  7265. DebugAssert(!IsLocalBrowserMode());
  7266. // This also handles drops of remote files to queue.
  7267. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  7268. SAFE_DESTROY(FDDFileList);
  7269. if (!FFakeFileDropTarget.IsEmpty())
  7270. {
  7271. RemoveDir(ApiPath(FFakeFileDropTarget));
  7272. }
  7273. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7274. {
  7275. try
  7276. {
  7277. TDragResult DDResult = (Sender == RemoteDirView) ?
  7278. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7279. // Focus is moved to the target application,
  7280. // but as we are going to present the UI, we need to steal the focus back.
  7281. // This most likely won't work though (windows does not allow application
  7282. // to steal focus most of the time)
  7283. Application->BringToFront();
  7284. // On older version of Windows we never got drMove here, see also comment below.
  7285. // On Windows 10, we get the "move".
  7286. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7287. {
  7288. UnicodeString TargetDirectory;
  7289. TFileOperation Operation;
  7290. // drInvalid may mean drMove, see comment below
  7291. switch (DDResult)
  7292. {
  7293. case drCopy:
  7294. Operation = foCopy;
  7295. break;
  7296. case drMove:
  7297. Operation = foMove;
  7298. break;
  7299. default:
  7300. DebugFail();
  7301. case drInvalid:
  7302. // prefer "copy" for safety
  7303. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7304. break;
  7305. }
  7306. TTransferOperationParam Param;
  7307. UnicodeString CounterName;
  7308. bool ForceQueue;
  7309. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7310. {
  7311. // we get drInvalid both if d&d was intercepted by ddext,
  7312. // and when users drops on no-drop location.
  7313. // we tell the difference by existence of response from ddext,
  7314. // so we ignore absence of response in this case
  7315. if (DDResult != drInvalid)
  7316. {
  7317. // here we know that the extension is installed,
  7318. // as it is checked as soon as drag&drop starts from
  7319. // RemoteFileControlDDCreateDragFileList
  7320. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7321. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7322. {
  7323. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7324. }
  7325. else
  7326. {
  7327. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7328. }
  7329. }
  7330. }
  7331. else
  7332. {
  7333. // download using fake file
  7334. Param.Temp = false;
  7335. Param.DragDrop = true;
  7336. if (ForceQueue)
  7337. {
  7338. Param.Queue = asOn;
  7339. }
  7340. if (Sender == RemoteDirView)
  7341. {
  7342. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7343. }
  7344. if (RemoteFileControlFileOperation(Sender, Operation,
  7345. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7346. {
  7347. Configuration->Usage->Inc(CounterName);
  7348. }
  7349. }
  7350. }
  7351. }
  7352. __finally
  7353. {
  7354. CloseHandle(FDDExtMapFile);
  7355. FDDExtMapFile = NULL;
  7356. delete FDragFakeMonitors;
  7357. FDragFakeMonitors = NULL;
  7358. RemoveDir(ApiPath(FDragFakeDirectory));
  7359. FDragFakeDirectory = L"";
  7360. FFakeFileDropTarget = UnicodeString();
  7361. }
  7362. }
  7363. if (FDragDropSshTerminate.get() != NULL)
  7364. {
  7365. FDragDropSshTerminate->Rethrow();
  7366. }
  7367. }
  7368. //---------------------------------------------------------------------------
  7369. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7370. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7371. {
  7372. // Remember drop effect so we know (when user drops files), if we copy or move
  7373. FLastDropEffect = dwEffect;
  7374. }
  7375. //---------------------------------------------------------------------------
  7376. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7377. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7378. {
  7379. bool Result;
  7380. if (FDDTargetControl == QueueView3)
  7381. {
  7382. Directory = DefaultDownloadTargetDirectory();
  7383. Result = true;
  7384. CounterName = L"DownloadsDragDropQueue";
  7385. ForceQueue = true;
  7386. }
  7387. else if (!FFakeFileDropTarget.IsEmpty())
  7388. {
  7389. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7390. Result = true;
  7391. ForceQueue = false;
  7392. CounterName = L"DownloadsDragDropFakeFile";
  7393. }
  7394. else
  7395. {
  7396. ForceQueue = false;
  7397. Enabled = false;
  7398. try
  7399. {
  7400. int Timer = 0;
  7401. Result = false;
  7402. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7403. {
  7404. {
  7405. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7406. TDragExtCommStruct* CommStruct;
  7407. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7408. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7409. DebugAssert(CommStruct != NULL);
  7410. Result = !CommStruct->Dragging;
  7411. if (Result)
  7412. {
  7413. Directory = ExtractFilePath(CommStruct->DropDest);
  7414. CounterName = L"DownloadsDragDropExternalExt";
  7415. }
  7416. UnmapViewOfFile(CommStruct);
  7417. }
  7418. if (!Result)
  7419. {
  7420. Sleep(50);
  7421. Timer += 50;
  7422. Application->ProcessMessages();
  7423. }
  7424. }
  7425. }
  7426. __finally
  7427. {
  7428. Enabled = true;
  7429. }
  7430. }
  7431. return Result;
  7432. }
  7433. //---------------------------------------------------------------------------
  7434. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7435. const UnicodeString TempDir, int SecDelay)
  7436. {
  7437. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7438. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7439. if (FDelayedDeletionTimer == NULL)
  7440. {
  7441. DebugAssert(HandleAllocated());
  7442. FDelayedDeletionTimer = new TTimer(this);
  7443. FDelayedDeletionTimer->Interval = 10000;
  7444. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7445. }
  7446. else
  7447. {
  7448. FDelayedDeletionTimer->Enabled = true;
  7449. }
  7450. }
  7451. //---------------------------------------------------------------------------
  7452. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7453. {
  7454. DebugAssert(FDelayedDeletionList != NULL);
  7455. TDateTime N = Now();
  7456. TDateTime Alert;
  7457. UnicodeString Directory;
  7458. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7459. {
  7460. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7461. if ((N >= Alert) || (Sender == NULL))
  7462. {
  7463. Directory = FDelayedDeletionList->Strings[Index];
  7464. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7465. {
  7466. FDelayedDeletionList->Delete(Index);
  7467. }
  7468. }
  7469. }
  7470. if (FDelayedDeletionList->Count == 0)
  7471. {
  7472. FDelayedDeletionTimer->Enabled = false;
  7473. }
  7474. }
  7475. //---------------------------------------------------------------------------
  7476. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7477. {
  7478. DebugAssert(!IsLocalBrowserMode());
  7479. if (IsFileControl(FDDTargetControl, osRemote) ||
  7480. (FDDTargetControl == SessionsPageControl))
  7481. {
  7482. TManagedTerminal * TargetTerminal = NULL;
  7483. TFileOperation Operation = foNone;
  7484. if (FDDTargetControl == SessionsPageControl)
  7485. {
  7486. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7487. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7488. // do not allow dropping on local-local, disconnected and the "+" tab (see also SessionsDDProcessDropped)
  7489. TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  7490. if (IsActiveTerminal(TargetTerminal) && DebugAlwaysTrue(!TargetTerminal->LocalBrowser))
  7491. {
  7492. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7493. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7494. {
  7495. Operation = foRemoteMove;
  7496. }
  7497. else
  7498. {
  7499. Operation = foRemoteCopy;
  7500. }
  7501. }
  7502. }
  7503. else
  7504. {
  7505. if (FLastDropEffect == DROPEFFECT_MOVE)
  7506. {
  7507. Operation = foRemoteMove;
  7508. }
  7509. else if (FLastDropEffect == DROPEFFECT_COPY)
  7510. {
  7511. Operation = foRemoteCopy;
  7512. }
  7513. }
  7514. if (Operation != foNone)
  7515. {
  7516. RemoteFileControlFileOperation(DropSourceControl,
  7517. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7518. }
  7519. // abort drag&drop
  7520. Abort();
  7521. }
  7522. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7523. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7524. (FDDTargetControl != QueueView3))
  7525. {
  7526. DebugAssert(!FDragTempDir.IsEmpty());
  7527. TTransferType Type;
  7528. UnicodeString TempDir = FDragTempDir;
  7529. // We clear FDragTempDir before calling
  7530. // just in case it fail (raises exception)
  7531. FDragTempDir = L"";
  7532. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7533. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7534. // empty directory parameter means temp directory -> don't display it!
  7535. UnicodeString TargetDir = L"";
  7536. int Options = 0;
  7537. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7538. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7539. {
  7540. Abort();
  7541. }
  7542. // TargetDir is set when dropped on local file control
  7543. // (this was workaround for legacy dirview event handling, now it should be
  7544. // made prettier)
  7545. if (TargetDir.IsEmpty())
  7546. {
  7547. TargetDir = TempDir;
  7548. if (ForceDirectories(ApiPath(TargetDir)))
  7549. {
  7550. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7551. FPendingTempSpaceWarn = true;
  7552. try
  7553. {
  7554. FDragDropOperation = true;
  7555. int Params = cpTemporary |
  7556. (Type == ttMove ? cpDelete : 0);
  7557. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7558. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7559. }
  7560. __finally
  7561. {
  7562. FDragDropOperation = false;
  7563. FPendingTempSpaceWarn = false;
  7564. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7565. }
  7566. }
  7567. else
  7568. {
  7569. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7570. }
  7571. }
  7572. }
  7573. }
  7574. //---------------------------------------------------------------------------
  7575. void __fastcall TCustomScpExplorerForm::DDDownload(
  7576. TStrings * FilesToCopy, const UnicodeString & TargetDir, TCopyParamType * CopyParam, int Params)
  7577. {
  7578. DebugAssert(!IsLocalBrowserMode());
  7579. TAutoBatch AutoBatch(this);
  7580. CopyParam->IncludeFileMask.SetRoots(TargetDir, FilesToCopy);
  7581. UpdateCopyParamCounters(*CopyParam);
  7582. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7583. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7584. {
  7585. RemoteDriveView->UpdateDropSource();
  7586. }
  7587. }
  7588. //---------------------------------------------------------------------------
  7589. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7590. {
  7591. public:
  7592. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7593. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7594. {
  7595. }
  7596. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7597. {
  7598. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7599. TDataObjectFilesEx::AllowData(FormatEtc);
  7600. }
  7601. };
  7602. //---------------------------------------------------------------------------
  7603. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7604. TObject * Sender, TDataObject *& DataObject)
  7605. {
  7606. if (FDDExtMapFile != NULL)
  7607. {
  7608. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7609. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7610. {
  7611. Abort();
  7612. }
  7613. if (FileList->Count > 0)
  7614. {
  7615. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7616. if (!FilesObject->IsValid(true, true))
  7617. {
  7618. FilesObject->_Release();
  7619. }
  7620. else
  7621. {
  7622. DataObject = FilesObject;
  7623. }
  7624. }
  7625. }
  7626. }
  7627. //---------------------------------------------------------------------------
  7628. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7629. {
  7630. DebugFail();
  7631. }
  7632. //---------------------------------------------------------------------------
  7633. void __fastcall TCustomScpExplorerForm::GoToTree()
  7634. {
  7635. ComponentVisible[fcRemoteTree] = true;
  7636. DriveView(osRemote)->SetFocus();
  7637. }
  7638. //---------------------------------------------------------------------------
  7639. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7640. TPanelExport Export)
  7641. {
  7642. TCustomDirView * DirView = this->DirView(Side);
  7643. std::unique_ptr<TStrings> ExportData(new TStringList());
  7644. switch (Export)
  7645. {
  7646. case pePath:
  7647. ExportData->Add(DirView->PathName);
  7648. break;
  7649. case peFileList:
  7650. case peFullFileList:
  7651. {
  7652. bool FullPath = (Export == peFullFileList);
  7653. DirView->CreateFileList(false, FullPath, ExportData.get());
  7654. for (int Index = 0; Index < ExportData->Count; Index++)
  7655. {
  7656. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7657. {
  7658. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7659. }
  7660. }
  7661. }
  7662. break;
  7663. default:
  7664. DebugFail();
  7665. }
  7666. return ExportData.release();
  7667. }
  7668. //---------------------------------------------------------------------------
  7669. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7670. TPanelExport Export, TPanelExportDestination Destination)
  7671. {
  7672. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7673. PanelExportStore(Side, Export, Destination, ExportData.get());
  7674. }
  7675. //---------------------------------------------------------------------------
  7676. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7677. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7678. TStrings * ExportData)
  7679. {
  7680. if (Destination == pedClipboard)
  7681. {
  7682. TInstantOperationVisualizer Visualizer;
  7683. CopyToClipboard(ExportData);
  7684. }
  7685. else
  7686. {
  7687. DebugFail();
  7688. }
  7689. }
  7690. //---------------------------------------------------------------------------
  7691. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7692. {
  7693. TCustomDirView * DirView = this->DirView(Side);
  7694. UnicodeString Mask = DirView->Mask;
  7695. if (DoFilterMaskDialog(DirView, Mask))
  7696. {
  7697. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7698. Configuration->Usage->Inc(L"Filters");
  7699. }
  7700. }
  7701. //---------------------------------------------------------------------------
  7702. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7703. {
  7704. return FQueueController->DefaultOperation();
  7705. }
  7706. //---------------------------------------------------------------------------
  7707. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7708. TQueueOperation Operation, void ** Param)
  7709. {
  7710. switch (Operation)
  7711. {
  7712. case qoPreferences:
  7713. return true;
  7714. case qoGoTo:
  7715. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7716. case qoOnceEmpty:
  7717. return IsAnythingQueued();
  7718. default:
  7719. return FQueueController->AllowOperation(Operation, Param);
  7720. }
  7721. }
  7722. //---------------------------------------------------------------------------
  7723. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7724. {
  7725. if (DebugAlwaysTrue(QueueView3->Visible))
  7726. {
  7727. QueueView3->SetFocus();
  7728. }
  7729. }
  7730. //---------------------------------------------------------------------------
  7731. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7732. TQueueOperation Operation, void * Param)
  7733. {
  7734. if (Operation == qoGoTo)
  7735. {
  7736. GoToQueue();
  7737. }
  7738. else if (Operation == qoPreferences)
  7739. {
  7740. PreferencesDialog(pmQueue);
  7741. }
  7742. else
  7743. {
  7744. FQueueController->ExecuteOperation(Operation, Param);
  7745. }
  7746. }
  7747. //---------------------------------------------------------------------------
  7748. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7749. {
  7750. return (Queue != NULL) && Queue->Enabled;
  7751. }
  7752. //---------------------------------------------------------------------------
  7753. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7754. {
  7755. DebugAssert(Queue != NULL);
  7756. if (Queue != NULL)
  7757. {
  7758. Queue->Enabled = !Queue->Enabled;
  7759. }
  7760. }
  7761. //---------------------------------------------------------------------------
  7762. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7763. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7764. {
  7765. FQueueActedItem = QueueView3->ItemFocused;
  7766. }
  7767. //---------------------------------------------------------------------------
  7768. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7769. {
  7770. return TopDock->Height + QueueSplitter->Height;
  7771. }
  7772. //---------------------------------------------------------------------------
  7773. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  7774. {
  7775. return
  7776. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  7777. (QueueDock->Visible ? QueueDock->Height : 0) +
  7778. (QueueLabel->Visible ? QueueLabel->Height : 0);
  7779. }
  7780. //---------------------------------------------------------------------------
  7781. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  7782. {
  7783. return ScaleByTextHeight(this, 48);
  7784. }
  7785. //---------------------------------------------------------------------------
  7786. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7787. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7788. {
  7789. // when queue is hidden by double-clicking splitter, stray attempt to
  7790. // resize the panel with strange value arrives, make sure it is ignored
  7791. if (ComponentVisible[fcQueueView])
  7792. {
  7793. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  7794. if (NewSize > MaxHeightLimit)
  7795. {
  7796. NewSize = MaxHeightLimit;
  7797. }
  7798. int MinHeightLimit =
  7799. GetStaticQueuePanelComponentsHeight() +
  7800. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  7801. GetMinQueueViewHeight();
  7802. if (NewSize < MinHeightLimit)
  7803. {
  7804. NewSize = MinHeightLimit;
  7805. }
  7806. }
  7807. else
  7808. {
  7809. Accept = false;
  7810. }
  7811. }
  7812. //---------------------------------------------------------------------------
  7813. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  7814. {
  7815. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  7816. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  7817. if (QueueViewHeight < GetMinQueueViewHeight())
  7818. {
  7819. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  7820. }
  7821. }
  7822. //---------------------------------------------------------------------------
  7823. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7824. TDragObject *& /*DragObject*/)
  7825. {
  7826. FQueueActedItem = QueueView3->ItemFocused;
  7827. }
  7828. //---------------------------------------------------------------------------
  7829. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7830. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7831. {
  7832. Accept = true;
  7833. if (Source == QueueView3)
  7834. {
  7835. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7836. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7837. if (Accept)
  7838. {
  7839. TQueueItemProxy * QueueItem;
  7840. TQueueItemProxy * DestQueueItem;
  7841. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7842. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7843. Accept = (QueueItem != DestQueueItem) &&
  7844. (QueueItem->Status == TQueueItem::qsPending) &&
  7845. (DestQueueItem->Status == TQueueItem::qsPending);
  7846. }
  7847. }
  7848. }
  7849. //---------------------------------------------------------------------------
  7850. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7851. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7852. {
  7853. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7854. {
  7855. TQueueItemProxy * QueueItem;
  7856. TQueueItemProxy * DestQueueItem;
  7857. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7858. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7859. QueueItem->Move(DestQueueItem);
  7860. }
  7861. }
  7862. //---------------------------------------------------------------------------
  7863. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7864. {
  7865. if ((QueueView3->ItemFocused == NULL) &&
  7866. (QueueView3->Items->Count > 0))
  7867. {
  7868. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7869. }
  7870. QueueLabelUpdateStatus();
  7871. }
  7872. //---------------------------------------------------------------------------
  7873. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7874. {
  7875. QueueLabelUpdateStatus();
  7876. }
  7877. //---------------------------------------------------------------------------
  7878. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7879. {
  7880. QueueLabel->UpdateStatus();
  7881. }
  7882. //---------------------------------------------------------------------------
  7883. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7884. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7885. {
  7886. if (Selected)
  7887. {
  7888. NonVisualDataModule->UpdateNonVisibleActions();
  7889. }
  7890. }
  7891. //---------------------------------------------------------------------------
  7892. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7893. {
  7894. TDragDropFilesEx * Result = NULL;
  7895. if (Sender == SessionsPageControl)
  7896. {
  7897. Result = FSessionsDragDropFilesEx;
  7898. }
  7899. else if (Sender == QueueView3)
  7900. {
  7901. Result = FQueueDragDropFilesEx;
  7902. }
  7903. else
  7904. {
  7905. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7906. if (DirView != NULL)
  7907. {
  7908. Result = DirView->DragDropFilesEx;
  7909. }
  7910. else
  7911. {
  7912. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7913. if (DriveView != NULL)
  7914. {
  7915. Result = DriveView->DragDropFilesEx;
  7916. }
  7917. }
  7918. }
  7919. DebugAssert(Result != NULL);
  7920. return Result;
  7921. }
  7922. //---------------------------------------------------------------------------
  7923. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7924. {
  7925. return (DView->SelCount == DView->FilesCount);
  7926. }
  7927. //---------------------------------------------------------------------------
  7928. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7929. {
  7930. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7931. }
  7932. //---------------------------------------------------------------------------
  7933. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7934. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7935. {
  7936. DebugAssert(!IsLocalBrowserMode());
  7937. TFileOperation Operation;
  7938. switch (Effect)
  7939. {
  7940. case DROPEFFECT_MOVE:
  7941. Operation = foMove;
  7942. break;
  7943. case DROPEFFECT_COPY:
  7944. // occures on WinXP (reported by user)
  7945. default:
  7946. Operation = foCopy;
  7947. break;
  7948. };
  7949. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7950. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7951. if (DragDropFilesEx->FileList->Count > 0)
  7952. {
  7953. TStrings * FileList = new TStringList();
  7954. try
  7955. {
  7956. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7957. {
  7958. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7959. }
  7960. FDragDropOperation = true;
  7961. TTransferOperationParam Param;
  7962. Param.TargetDirectory = TargetPath;
  7963. // upload, no temp dirs
  7964. Param.Temp = false;
  7965. Param.DragDrop = DragDrop;
  7966. Param.Options =
  7967. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7968. if (ForceQueue)
  7969. {
  7970. Param.Queue = asOn;
  7971. }
  7972. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7973. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7974. {
  7975. if (IsFileControl(DropSourceControl, osLocal))
  7976. {
  7977. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7978. }
  7979. else
  7980. {
  7981. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7982. }
  7983. }
  7984. }
  7985. __finally
  7986. {
  7987. FDragDropOperation = false;
  7988. delete FileList;
  7989. }
  7990. }
  7991. }
  7992. //---------------------------------------------------------------------------
  7993. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7994. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7995. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7996. {
  7997. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7998. }
  7999. //---------------------------------------------------------------------------
  8000. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  8001. TObject * Sender, int grfKeyState, int & dwEffect)
  8002. {
  8003. DebugAssert(!IsLocalBrowserMode());
  8004. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  8005. if ((dwEffect != DROPEFFECT_NONE) &&
  8006. IsFileControl(DropSourceControl, osRemote))
  8007. {
  8008. // do not allow drop on remote panel (on free space, still allow drop on directories)
  8009. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  8010. (RemoteDirView->DropTarget == NULL))
  8011. {
  8012. dwEffect = DROPEFFECT_NONE;
  8013. }
  8014. else
  8015. {
  8016. if (dwEffect == DROPEFFECT_COPY)
  8017. {
  8018. bool MoveCapable = Terminal->IsCapable[fcRemoteMove];
  8019. // currently we support copying always (at least via temporary directory);
  8020. // remove associated checks once this all proves stable and working
  8021. bool CopyCapable = true;
  8022. // if we do not support neither of operations, there's no discussion
  8023. if (!MoveCapable && !CopyCapable)
  8024. {
  8025. dwEffect = DROPEFFECT_NONE;
  8026. }
  8027. else
  8028. {
  8029. // The default effect inside remote panel is move,
  8030. // unless we do not support it, but support copy
  8031. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  8032. {
  8033. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  8034. }
  8035. else
  8036. {
  8037. // with ctrl-down, we want copy unless it is not supported
  8038. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  8039. }
  8040. }
  8041. }
  8042. }
  8043. }
  8044. }
  8045. //---------------------------------------------------------------------------
  8046. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  8047. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  8048. {
  8049. DebugAssert(!IsLocalBrowserMode());
  8050. if (FDDTotalSize >= 0)
  8051. {
  8052. if (File->IsDirectory)
  8053. {
  8054. FDDTotalSize = -1;
  8055. }
  8056. else
  8057. {
  8058. FDDTotalSize += File->Size;
  8059. }
  8060. }
  8061. DebugAssert(!FDragTempDir.IsEmpty());
  8062. // TODO: this is quite ineffective
  8063. // TODO: what if invalid character replacement is disabled?
  8064. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  8065. UnicodeString TransferFileName;
  8066. if (Sender == RemoteDriveView)
  8067. {
  8068. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  8069. }
  8070. else
  8071. {
  8072. TransferFileName = File->FileName;
  8073. }
  8074. FDDFileList->AddObject(TransferFileName, File);
  8075. }
  8076. //---------------------------------------------------------------------------
  8077. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  8078. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  8079. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  8080. {
  8081. // sometimes we do not get DDEnd so the list is not released
  8082. SAFE_DESTROY(FDDFileList);
  8083. FDDFileList = new TStringList();
  8084. FDragTempDir = WinConfiguration->TemporaryDir();
  8085. FDDTotalSize = 0;
  8086. FDragDropSshTerminate.reset(NULL);
  8087. }
  8088. //---------------------------------------------------------------------------
  8089. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  8090. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  8091. HRESULT & Result)
  8092. {
  8093. if (Result == DRAGDROP_S_DROP)
  8094. {
  8095. try
  8096. {
  8097. GlobalDragImageList->HideDragImage();
  8098. try
  8099. {
  8100. RemoteFileControlDDTargetDrop();
  8101. }
  8102. catch(ESshTerminate & E)
  8103. {
  8104. FDragDropSshTerminate.reset(E.Clone());
  8105. }
  8106. }
  8107. catch (Exception &E)
  8108. {
  8109. // If downloading fails we need to cancel drag&drop, otherwise
  8110. // Explorer shows error
  8111. // But by the way exception probably never reach this point as
  8112. // it's catched on way.
  8113. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  8114. Result = DRAGDROP_S_CANCEL;
  8115. ShowExtendedException(Terminal, &E);
  8116. }
  8117. }
  8118. }
  8119. //---------------------------------------------------------------------------
  8120. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  8121. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  8122. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  8123. {
  8124. TFileMasks::TParams MaskParams;
  8125. MaskParams.Size = Size;
  8126. MaskParams.Modification = Modification;
  8127. // this does not re-parse the mask if it is the same as the last time
  8128. FDirViewMatchMask = Masks;
  8129. bool ImplicitMatch;
  8130. Matches =
  8131. // Local and RecurseInclude parameters have no effect as there's no path.
  8132. // This should use potential MatchesFileName overload with ImplicitMatch argument.
  8133. FDirViewMatchMask.Matches(FileName, false, Directory, &MaskParams, true, ImplicitMatch) &&
  8134. (AllowImplicitMatches || !ImplicitMatch);
  8135. }
  8136. //---------------------------------------------------------------------------
  8137. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  8138. TObject * Sender, TListItem * Item, WORD & Indexes)
  8139. {
  8140. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8141. UnicodeString Ext;
  8142. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  8143. {
  8144. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  8145. }
  8146. else
  8147. {
  8148. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  8149. }
  8150. if (SameText(Ext, Configuration->PartialExt))
  8151. {
  8152. Indexes |= oiPartial;
  8153. }
  8154. }
  8155. //---------------------------------------------------------------------------
  8156. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  8157. {
  8158. return
  8159. DirViewEnabled(osCurrent) &&
  8160. DirView(osCurrent)->CanPasteFromClipBoard();
  8161. }
  8162. //---------------------------------------------------------------------------
  8163. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  8164. {
  8165. bool Result = false;
  8166. if (CanPasteToDirViewFromClipBoard())
  8167. {
  8168. Result = true;
  8169. }
  8170. else
  8171. {
  8172. UnicodeString ClipboardText;
  8173. if (NonEmptyTextFromClipboard(ClipboardText) &&
  8174. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  8175. {
  8176. if (StoredSessions->IsUrl(ClipboardText))
  8177. {
  8178. Result = true;
  8179. }
  8180. else
  8181. {
  8182. Result = DirViewEnabled(osCurrent);
  8183. }
  8184. }
  8185. }
  8186. return Result;
  8187. }
  8188. //---------------------------------------------------------------------------
  8189. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  8190. {
  8191. // Pasting to any local panel is handled in TScpCommanderForm::PasteFromClipBoard()
  8192. if (DoesClipboardContainOurFiles())
  8193. {
  8194. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  8195. {
  8196. DebugAssert(!IsLocalBrowserMode());
  8197. TTerminalManager * Manager = TTerminalManager::Instance();
  8198. TTerminal * TargetTerminal = Manager->ActiveTerminal;
  8199. Manager->ActiveSession = FClipboardTerminal;
  8200. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TargetTerminal);
  8201. }
  8202. }
  8203. else if (CanPasteToDirViewFromClipBoard())
  8204. {
  8205. DirView(osCurrent)->PasteFromClipBoard();
  8206. }
  8207. else
  8208. {
  8209. UnicodeString ClipboardText;
  8210. if (NonEmptyTextFromClipboard(ClipboardText))
  8211. {
  8212. if (StoredSessions->IsUrl(ClipboardText))
  8213. {
  8214. NewSession(ClipboardText);
  8215. }
  8216. else
  8217. {
  8218. DirView(osCurrent)->Path = ClipboardText;
  8219. }
  8220. }
  8221. }
  8222. }
  8223. //---------------------------------------------------------------------------
  8224. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  8225. {
  8226. // TBD
  8227. }
  8228. //---------------------------------------------------------------------------
  8229. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  8230. {
  8231. TCustomDirView * ADirView = DirView(Side);
  8232. ADirView->SelectAll(Mode);
  8233. ADirView->SetFocus();
  8234. }
  8235. //---------------------------------------------------------------------------
  8236. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  8237. {
  8238. TCustomDirView * ADirView = DirView(Side);
  8239. TFileFilter Filter;
  8240. DefaultFileFilter(Filter);
  8241. if (DoSelectMaskDialog(ADirView, Select, Filter))
  8242. {
  8243. Configuration->Usage->Inc(L"MaskSelections");
  8244. ADirView->SelectFiles(Filter, Select);
  8245. ADirView->SetFocus();
  8246. }
  8247. }
  8248. //---------------------------------------------------------------------------
  8249. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  8250. {
  8251. TCustomDirView * ADirView = DirView(Side);
  8252. ADirView->RestoreSelectedNames();
  8253. ADirView->SetFocus();
  8254. }
  8255. //---------------------------------------------------------------------------
  8256. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  8257. {
  8258. TCustomDirView * CurrentDirView = DirView(osCurrent);
  8259. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  8260. {
  8261. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  8262. UnicodeString Ext;
  8263. if (!IsSideLocalBrowser(osCurrent))
  8264. {
  8265. Ext = UnixExtractFileExt(FileName);
  8266. }
  8267. else
  8268. {
  8269. Ext = ExtractFileExt(FileName);
  8270. }
  8271. if (Ext.IsEmpty())
  8272. {
  8273. Ext = L".";
  8274. }
  8275. TFileFilter Filter;
  8276. Filter.Masks = FORMAT(L"*%s", (Ext));
  8277. Filter.Directories = false;
  8278. CurrentDirView->SelectFiles(Filter, Select);
  8279. }
  8280. }
  8281. //---------------------------------------------------------------------------
  8282. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8283. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8284. {
  8285. UnicodeString Result;
  8286. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  8287. {
  8288. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  8289. }
  8290. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8291. {
  8292. // noop
  8293. }
  8294. else
  8295. {
  8296. Result =
  8297. FMTLOAD(FILE_INFO_FORMAT,
  8298. (FormatBytes(FileInfo.SelectedSize),
  8299. FormatBytes(FileInfo.FilesSize),
  8300. FormatNumber(FileInfo.SelectedCount),
  8301. FormatNumber(FileInfo.FilesCount)));
  8302. }
  8303. return Result;
  8304. }
  8305. //---------------------------------------------------------------------------
  8306. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8307. TTBXStatusPanel * Panel, TOperationSide Side)
  8308. {
  8309. if (Panel->Index == 1)
  8310. {
  8311. ToggleShowHiddenFiles();
  8312. }
  8313. else if (Panel->Index == 2)
  8314. {
  8315. Filter(Side);
  8316. }
  8317. }
  8318. //---------------------------------------------------------------------------
  8319. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8320. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8321. {
  8322. DebugAssert(!StatusBar->SimplePanel);
  8323. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8324. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8325. }
  8326. //---------------------------------------------------------------------------
  8327. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8328. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8329. {
  8330. DebugAssert(StatusBar->Panels->Count >= 3);
  8331. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8332. if (FileInfo.HiddenCount > 0)
  8333. {
  8334. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8335. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8336. }
  8337. else
  8338. {
  8339. HiddenFilesPanel->ViewPriority = 0;
  8340. // not really necessary, just to cleanup no-longer-valid data
  8341. HiddenFilesPanel->Caption = L"";
  8342. }
  8343. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8344. if (FileInfo.FilteredCount > 0)
  8345. {
  8346. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8347. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8348. }
  8349. else
  8350. {
  8351. FilteredFilesPanel->ViewPriority = 0;
  8352. // not really necessary, just to cleanup no-longer-valid data
  8353. FilteredFilesPanel->Caption = L"";
  8354. }
  8355. }
  8356. //---------------------------------------------------------------------------
  8357. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8358. TObject * /*Sender*/)
  8359. {
  8360. if (DirView(osOther)->Enabled)
  8361. {
  8362. DirView(osOther)->SetFocus();
  8363. }
  8364. }
  8365. //---------------------------------------------------------------------------
  8366. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8367. {
  8368. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8369. switch (Config.Show)
  8370. {
  8371. case qvShow:
  8372. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8373. {
  8374. Config.Show = qvHide;
  8375. }
  8376. else
  8377. {
  8378. Config.Show = Config.LastHideShow;
  8379. }
  8380. break;
  8381. case qvHideWhenEmpty:
  8382. if (ComponentVisible[fcQueueView])
  8383. {
  8384. Config.Show = qvHide;
  8385. }
  8386. else
  8387. {
  8388. Config.LastHideShow = Config.Show;
  8389. Config.Show = qvShow;
  8390. }
  8391. break;
  8392. case qvHide:
  8393. Config.LastHideShow = Config.Show;
  8394. Config.Show = qvShow;
  8395. break;
  8396. }
  8397. WinConfiguration->QueueView = Config;
  8398. }
  8399. //---------------------------------------------------------------------------
  8400. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8401. {
  8402. UnicodeString Result;
  8403. if (Terminal != NULL)
  8404. {
  8405. switch (WinConfiguration->PathInCaption)
  8406. {
  8407. case picShort:
  8408. Result = ExtractShortName(Terminal->CurrentDirectory, true);
  8409. break;
  8410. case picFull:
  8411. Result = Terminal->CurrentDirectory;
  8412. break;
  8413. }
  8414. }
  8415. return Result;
  8416. }
  8417. //---------------------------------------------------------------------------
  8418. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8419. {
  8420. TColor Result = GetWindowColor(FSessionColor);
  8421. return Result;
  8422. }
  8423. //---------------------------------------------------------------------------
  8424. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8425. {
  8426. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8427. return Result;
  8428. }
  8429. //---------------------------------------------------------------------------
  8430. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8431. {
  8432. if (!FSessionChanging)
  8433. {
  8434. TTerminalManager::Instance()->UpdateAppTitle();
  8435. }
  8436. // WORKAROUND: Disabling list view when it is not showing yet does not set its
  8437. // background to gray on Windows 7 (works on Windows 10).
  8438. // See also EnableControl
  8439. if (Showing)
  8440. {
  8441. if (ManagedSession != NULL)
  8442. {
  8443. // Update path when it changes
  8444. if ((SessionsPageControl->ActivePage != NULL) &&
  8445. (GetSessionTabSession(SessionsPageControl->ActivePage) == ManagedSession))
  8446. {
  8447. UpdateSessionTab(SessionsPageControl->ActivePage);
  8448. }
  8449. }
  8450. NonVisualDataModule->ReconnectSessionAction->Update();
  8451. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8452. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8453. CenterReconnectToolbar();
  8454. bool HasTerminal = HasActiveTerminal();
  8455. RemoteDirView->Enabled = HasTerminal;
  8456. RemoteDirView->Color = HasTerminal ? PanelColor() : DisabledPanelColor();
  8457. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8458. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8459. RemoteDriveView->Color = RemoteDirView->Color;
  8460. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8461. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8462. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8463. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8464. QueueFileList->Enabled = QueueView3->Enabled;
  8465. QueueFileList->Color = QueueView3->Color;
  8466. QueueFileList->Font->Color = QueueView3->Font->Color;
  8467. QueueLabelUpdateStatus();
  8468. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8469. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8470. if (FImmersiveDarkMode != WinConfiguration->UseDarkTheme())
  8471. {
  8472. UpdateDarkMode();
  8473. }
  8474. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal || IsLocalBrowserMode();
  8475. }
  8476. }
  8477. //---------------------------------------------------------------------------
  8478. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8479. {
  8480. UpdateControls();
  8481. }
  8482. //---------------------------------------------------------------------------
  8483. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8484. TObject * Sender)
  8485. {
  8486. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8487. DebugAssert(DirView != NULL);
  8488. DoDirViewLoaded(DirView);
  8489. TransferPresetAutoSelect();
  8490. }
  8491. //---------------------------------------------------------------------------
  8492. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8493. {
  8494. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8495. // first run after installation
  8496. if (double(Updates.LastCheck) == 0)
  8497. {
  8498. // make sure next time there will be an update (if enabled)
  8499. Updates.LastCheck = TDateTime(1);
  8500. WinConfiguration->Updates = Updates;
  8501. }
  8502. else if ((double(Updates.Period) > 0) &&
  8503. (Now() - Updates.LastCheck >= Updates.Period))
  8504. {
  8505. TThreadMethod OnUpdatesChecked = NULL;
  8506. if (!IsUWP())
  8507. {
  8508. OnUpdatesChecked = UpdatesChecked;
  8509. }
  8510. StartUpdateThread(OnUpdatesChecked);
  8511. }
  8512. }
  8513. //---------------------------------------------------------------------------
  8514. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8515. {
  8516. UnicodeString Message;
  8517. bool New;
  8518. TQueryType Type;
  8519. GetUpdatesMessage(Message, New, Type, false);
  8520. if (!Message.IsEmpty())
  8521. {
  8522. if (New)
  8523. {
  8524. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8525. }
  8526. if (!New && (Type != qtWarning))
  8527. {
  8528. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8529. }
  8530. else
  8531. {
  8532. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8533. }
  8534. if (New)
  8535. {
  8536. Configuration->Usage->Inc(L"UpdateNotifications");
  8537. }
  8538. }
  8539. }
  8540. //---------------------------------------------------------------------------
  8541. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8542. {
  8543. RestoreApp();
  8544. bool CanDisplay = !NonVisualDataModule->Busy;
  8545. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8546. {
  8547. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8548. }
  8549. if (CanDisplay)
  8550. {
  8551. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8552. CheckForUpdates(true);
  8553. }
  8554. else
  8555. {
  8556. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8557. }
  8558. }
  8559. //---------------------------------------------------------------------------
  8560. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8561. TCopyParamRuleData & Data)
  8562. {
  8563. DebugAssert(Terminal != NULL);
  8564. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8565. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8566. Data.RemoteDirectory = RemoteDirView->PathName;
  8567. }
  8568. //---------------------------------------------------------------------------
  8569. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8570. {
  8571. // Terminal can be null when we are changing local directory implicitly,
  8572. // because it has been deleted, while no session is connected
  8573. // (Login dialog is open)
  8574. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8575. {
  8576. DebugAssert(!IsLocalBrowserMode());
  8577. TCopyParamRuleData Data;
  8578. GetTransferPresetAutoSelectData(Data);
  8579. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8580. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8581. if (CopyParamIndex < 0)
  8582. {
  8583. // there is no preset that matches autoselection
  8584. // set preset that we consider "default"
  8585. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8586. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8587. }
  8588. else
  8589. {
  8590. // there is preset matching autoselection
  8591. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8592. if (CopyParamName == FCopyParamAutoSelected)
  8593. {
  8594. // autoselected the same preset as the last time
  8595. // make no change (i.e. preserve custom user preset, if any)
  8596. }
  8597. else
  8598. {
  8599. // autoselected the different preset then the last time (or there
  8600. // was default preset set)
  8601. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8602. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8603. }
  8604. }
  8605. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8606. {
  8607. if (CopyParamIndex >= 0)
  8608. {
  8609. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8610. }
  8611. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8612. try
  8613. {
  8614. int Fmt =
  8615. (CopyParamIndex < 0) ?
  8616. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8617. COPY_PARAM_AUTOSELECTED;
  8618. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8619. Data->Message = MainInstructions(Message);
  8620. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8621. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8622. CopyParamAttrs);
  8623. if (CopyParamIndex >= 0)
  8624. {
  8625. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8626. Info = FORMAT(L"%s\n \n%s", (Info,
  8627. FMTLOAD(COPY_PARAM_RULE,
  8628. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8629. }
  8630. Data->Message += L"\n\n" + Info;
  8631. if (WinConfiguration->CopyParamAutoSelectNotice)
  8632. {
  8633. TransferPresetNoteMessage(Data, true);
  8634. }
  8635. else
  8636. {
  8637. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8638. Data = NULL; // ownership passed
  8639. }
  8640. }
  8641. __finally
  8642. {
  8643. delete Data;
  8644. }
  8645. }
  8646. }
  8647. }
  8648. //---------------------------------------------------------------------------
  8649. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8650. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8651. {
  8652. DebugAssert(NoteData != NULL);
  8653. DebugAssert(!IsLocalBrowserMode());
  8654. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8655. TQueryButtonAlias Aliases[1];
  8656. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8657. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8658. Params.Aliases = Aliases;
  8659. Params.AliasesCount = LENOF(Aliases);
  8660. unsigned int Result =
  8661. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8662. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8663. switch (Result)
  8664. {
  8665. case qaNeverAskAgain:
  8666. DebugAssert(AllowNeverAskAgain);
  8667. WinConfiguration->CopyParamAutoSelectNotice = false;
  8668. break;
  8669. case qaIgnore:
  8670. PreferencesDialog(pmPresets);
  8671. break;
  8672. }
  8673. }
  8674. //---------------------------------------------------------------------------
  8675. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8676. {
  8677. // as of now useless, as this is used for notes only, never for balloons, ...
  8678. RestoreApp();
  8679. // .. and we should never be busy here
  8680. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8681. {
  8682. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8683. }
  8684. }
  8685. //---------------------------------------------------------------------------
  8686. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8687. TPreferencesMode APreferencesMode)
  8688. {
  8689. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8690. TCopyParamRuleData Data;
  8691. if (Terminal != NULL)
  8692. {
  8693. PreferencesData.reset(new TPreferencesDialogData());
  8694. GetTransferPresetAutoSelectData(Data);
  8695. PreferencesData->CopyParamRuleData = &Data;
  8696. }
  8697. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8698. }
  8699. //---------------------------------------------------------------------------
  8700. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8701. const TCustomCommandType & Command)
  8702. {
  8703. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8704. {
  8705. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  8706. }
  8707. }
  8708. //---------------------------------------------------------------------------
  8709. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8710. {
  8711. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8712. TCustomCommandType Command;
  8713. // make sure we use local custom command when remote are not supported
  8714. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8715. {
  8716. Command = FLastCustomCommand;
  8717. }
  8718. else
  8719. {
  8720. Command.Params = Command.Params | ccLocal;
  8721. }
  8722. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8723. FEditingFocusedAdHocCommand = OnFocused;
  8724. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8725. int Options =
  8726. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8727. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8728. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8729. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8730. {
  8731. FLastCustomCommand = Command;
  8732. UpdateCustomCommandsToolbar();
  8733. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8734. }
  8735. }
  8736. //---------------------------------------------------------------------------
  8737. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8738. {
  8739. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8740. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8741. DebugAssert(State > 0);
  8742. if (State <= 0)
  8743. {
  8744. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  8745. }
  8746. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8747. }
  8748. //---------------------------------------------------------------------------
  8749. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8750. TCustomCommandType & Command, int & State)
  8751. {
  8752. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8753. if (Result)
  8754. {
  8755. Command = FLastCustomCommand;
  8756. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8757. }
  8758. return Result;
  8759. }
  8760. //---------------------------------------------------------------------------
  8761. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8762. {
  8763. ResetIncrementalSearch();
  8764. }
  8765. //---------------------------------------------------------------------------
  8766. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8767. {
  8768. DebugAssert(ComponentVisible[Component]);
  8769. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8770. }
  8771. //---------------------------------------------------------------------------
  8772. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8773. {
  8774. // when queue panel is resized here directly, the status bar is stretched
  8775. // over whole space the panel occupied
  8776. PostComponentHide(fcQueueView);
  8777. }
  8778. //---------------------------------------------------------------------------
  8779. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  8780. {
  8781. ComponentVisible[fcQueueFileList] = false;
  8782. }
  8783. //---------------------------------------------------------------------------
  8784. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  8785. {
  8786. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  8787. RefreshColorMode();
  8788. ResetSysDarkTheme();
  8789. ConfigurationChanged();
  8790. ConfigureInterface();
  8791. // Should be called for all controls
  8792. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  8793. }
  8794. //---------------------------------------------------------------------------
  8795. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  8796. {
  8797. // Do not handle, when shutting down anyway (maybe also when not setup completelly yet?)
  8798. if (!FInvalid &&
  8799. (Message.LParam != 0) &&
  8800. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  8801. {
  8802. ThemeChanged();
  8803. }
  8804. TForm::Dispatch(&Message);
  8805. }
  8806. //---------------------------------------------------------------------------
  8807. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8808. {
  8809. TMessage * M = static_cast<TMessage*>(Message);
  8810. switch (M->Msg)
  8811. {
  8812. case CM_DIALOGCHAR:
  8813. CMDialogChar(*M);
  8814. break;
  8815. case WM_APPCOMMAND:
  8816. WMAppCommand(*M);
  8817. break;
  8818. case WM_SYSCOMMAND:
  8819. WMSysCommand(*M);
  8820. break;
  8821. case WM_QUERYENDSESSION:
  8822. WMQueryEndSession(*M);
  8823. break;
  8824. case WM_ENDSESSION:
  8825. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8826. break;
  8827. #ifdef _DEBUG
  8828. case WM_WINDOWPOSCHANGED:
  8829. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8830. break;
  8831. case CM_CUSTOMSTYLECHANGED:
  8832. TForm::Dispatch(Message);
  8833. break;
  8834. #endif
  8835. case WM_COMPONENT_HIDE:
  8836. {
  8837. Byte Component = static_cast<Byte>(M->WParam);
  8838. // sanity check
  8839. if (ComponentVisible[Component])
  8840. {
  8841. // special treatment
  8842. if (Component == fcQueueView)
  8843. {
  8844. ToggleQueueVisibility();
  8845. DebugAssert(!ComponentVisible[fcQueueView]);
  8846. }
  8847. else
  8848. {
  8849. ComponentVisible[Component] = false;
  8850. }
  8851. }
  8852. }
  8853. break;
  8854. case WM_COPYDATA:
  8855. WMCopyData(*M);
  8856. break;
  8857. case WM_MANAGES_CAPTION:
  8858. // caption managed in TTerminalManager::UpdateAppTitle()
  8859. M->Result = 1;
  8860. break;
  8861. case WM_WANTS_MOUSEWHEEL:
  8862. M->Result = 1;
  8863. break;
  8864. case CM_SHOWINGCHANGED:
  8865. CMShowingChanged(*M);
  8866. break;
  8867. case WM_CLOSE:
  8868. WMClose(*M);
  8869. break;
  8870. case WM_DPICHANGED:
  8871. WMDpiChanged(*M);
  8872. break;
  8873. case CM_DPICHANGED:
  8874. CMDpiChanged(*M);
  8875. break;
  8876. case WM_SETTINGCHANGE:
  8877. WMSettingChange(*M);
  8878. break;
  8879. case CM_DIALOGKEY:
  8880. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8881. break;
  8882. default:
  8883. TForm::Dispatch(Message);
  8884. break;
  8885. }
  8886. }
  8887. //---------------------------------------------------------------------------
  8888. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8889. {
  8890. // noop
  8891. }
  8892. //---------------------------------------------------------------------------
  8893. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8894. {
  8895. TForm::Dispatch(&Message);
  8896. FSessionColors->Clear();
  8897. AddFixedSessionImages();
  8898. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8899. UpdateImages();
  8900. }
  8901. //---------------------------------------------------------------------------
  8902. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8903. {
  8904. TForm::Dispatch(&Message);
  8905. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8906. UpdatePixelsPerInchMainWindowCounter();
  8907. }
  8908. //---------------------------------------------------------------------------
  8909. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8910. {
  8911. // Cannot close window while we are busy.
  8912. // We cannot test this in FormCloseQuery as that is called also from
  8913. // Close(), which is called by CloseApplicationAction. So we can be busy
  8914. // there already even, when it is legitimate to close the application.
  8915. // Possibly a better place to handle this would be WMSysCommand.
  8916. if (NonVisualDataModule->Busy)
  8917. {
  8918. Message.Result = 1;
  8919. }
  8920. else
  8921. {
  8922. TForm::Dispatch(&Message);
  8923. }
  8924. }
  8925. //---------------------------------------------------------------------------
  8926. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8927. {
  8928. if (!FShowingChanged)
  8929. {
  8930. AddStartupSequence(L"D");
  8931. FShowingChanged = true;
  8932. }
  8933. // Ignoring implicit local-local browser of Commander, opened from DoShow
  8934. bool NoSession = (ManagedSession == NULL);
  8935. // This calls TCustomScpExplorerForm::DoShow
  8936. TForm::Dispatch(&Message);
  8937. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  8938. if (Showing && !FStarted)
  8939. {
  8940. FStarted = true;
  8941. InterfaceStarted();
  8942. }
  8943. if (Showing && NoSession)
  8944. {
  8945. // When we are starting minimized (i.e. from an installer),
  8946. // postpone showing Login dialog until we get restored.
  8947. // Otherwise the Login dialog (and Authentication window) show restored
  8948. // over invisible (minimized) main window.
  8949. if (WindowState == wsMinimized)
  8950. {
  8951. FNeedSession = true;
  8952. }
  8953. else
  8954. {
  8955. // This happens before application ever goes idle, so the toolbars would
  8956. // stay enabled (initial state) until the Login dialog is dismissed.
  8957. UpdateActions();
  8958. NonVisualDataModule->StartBusy();
  8959. try
  8960. {
  8961. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8962. // otherwise the dialog does not receive focus.
  8963. // With Commander interface the ProcessMessages is called already
  8964. // by TDriveView, but with Explorer interface, we need to call it explicily
  8965. Application->ProcessMessages();
  8966. // do not reload sessions, they have been loaded just now (optimization)
  8967. NeedSession(true);
  8968. }
  8969. __finally
  8970. {
  8971. NonVisualDataModule->EndBusy();
  8972. }
  8973. }
  8974. }
  8975. }
  8976. //---------------------------------------------------------------------------
  8977. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  8978. {
  8979. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  8980. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  8981. }
  8982. //---------------------------------------------------------------------------
  8983. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8984. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8985. int & MaxHeight)
  8986. {
  8987. // workaround for bug in TWinControl.CalcConstraints
  8988. // Check for empty rect (restore from iconic state) is done there only after
  8989. // call to AdjustClientRect, which enlarges the rect (for forms).
  8990. TRect R = GetClientRect();
  8991. // when restoring from iconic state, place no restrictions
  8992. if (IsRectEmpty(R))
  8993. {
  8994. MinWidth = 0;
  8995. MinHeight = 0;
  8996. MaxWidth = 32000;
  8997. MaxHeight = 32000;
  8998. }
  8999. }
  9000. //---------------------------------------------------------------------------
  9001. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  9002. TSpaceAvailable & ASpaceAvailable, bool & Close)
  9003. {
  9004. // terminal can be already closed (e.g. dropped connection)
  9005. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  9006. {
  9007. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  9008. try
  9009. {
  9010. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  9011. }
  9012. catch(...)
  9013. {
  9014. if (!Terminal->Active)
  9015. {
  9016. Close = true;
  9017. }
  9018. throw;
  9019. }
  9020. }
  9021. }
  9022. //---------------------------------------------------------------------------
  9023. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  9024. {
  9025. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  9026. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  9027. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  9028. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  9029. {
  9030. OnGetSpaceAvailable = GetSpaceAvailable;
  9031. }
  9032. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  9033. OnGetSpaceAvailable);
  9034. }
  9035. //---------------------------------------------------------------------------
  9036. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  9037. {
  9038. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  9039. Terminal->FillSessionDataForCode(Data.get());
  9040. return Data.release();
  9041. }
  9042. //---------------------------------------------------------------------------
  9043. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  9044. {
  9045. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  9046. DoGenerateUrlDialog(Data.get(), Paths);
  9047. }
  9048. //---------------------------------------------------------------------------
  9049. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  9050. {
  9051. GenerateUrl(NULL);
  9052. }
  9053. //---------------------------------------------------------------------------
  9054. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  9055. {
  9056. std::unique_ptr<TStrings> Paths(new TStringList());
  9057. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  9058. GenerateUrl(Paths.get());
  9059. }
  9060. //---------------------------------------------------------------------------
  9061. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  9062. {
  9063. FSessionColor = value;
  9064. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  9065. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  9066. static_cast<TObject *>(GetComponent(fcColorMenu)));
  9067. DebugAssert(ColorItem != NULL);
  9068. ColorItem->Color = C;
  9069. NonVisualDataModule->ColorMenuItem->Color = C;
  9070. UpdateControls();
  9071. }
  9072. //---------------------------------------------------------------------------
  9073. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  9074. {
  9075. if (value != FSessionColor)
  9076. {
  9077. UpdateSessionColor(value);
  9078. // Is null when called from LastTerminalClosed
  9079. if (Terminal != NULL)
  9080. {
  9081. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  9082. }
  9083. }
  9084. }
  9085. //---------------------------------------------------------------------------
  9086. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  9087. {
  9088. bool Result = FNoteTimer->Enabled;
  9089. if (Result)
  9090. {
  9091. // cannot cancel note too early
  9092. bool NotEarly =
  9093. (Now() - FNoteShown >
  9094. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  9095. if (Force || NotEarly)
  9096. {
  9097. FNoteTimer->Enabled = false;
  9098. FNote = L"";
  9099. SAFE_DESTROY(FNoteData);
  9100. FOnNoteClick = NULL;
  9101. FNoteHints = FNotes->Text;
  9102. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  9103. UpdateStatusBar();
  9104. }
  9105. }
  9106. return Result;
  9107. }
  9108. //---------------------------------------------------------------------------
  9109. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  9110. {
  9111. DebugAssert(FNoteTimer->Enabled);
  9112. CancelNote(true);
  9113. }
  9114. //---------------------------------------------------------------------------
  9115. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  9116. {
  9117. FNoteHints = FNotes->Text.TrimRight();
  9118. }
  9119. //---------------------------------------------------------------------------
  9120. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  9121. {
  9122. int P = Note.Pos(L"\n");
  9123. if (P > 0)
  9124. {
  9125. Note.SetLength(P - 1);
  9126. }
  9127. FNotes->Add(FORMAT(L"[%s] %s",
  9128. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  9129. while (FNotes->Count > 10)
  9130. {
  9131. FNotes->Delete(0);
  9132. }
  9133. if (UpdateNow)
  9134. {
  9135. UpdateNoteHints();
  9136. UpdateStatusBar();
  9137. }
  9138. }
  9139. //---------------------------------------------------------------------------
  9140. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  9141. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  9142. {
  9143. int P = Note.Pos(L"\n");
  9144. if (P > 0)
  9145. {
  9146. Note.SetLength(P - 1);
  9147. }
  9148. UpdateNoteHints();
  9149. FNote = Note;
  9150. SAFE_DESTROY(FNoteData);
  9151. FNoteData = NoteData;
  9152. FOnNoteClick = OnNoteClick;
  9153. AddNote(Note, false);
  9154. UpdateStatusBar();
  9155. FNoteShown = Now();
  9156. FNoteTimer->Enabled = false;
  9157. if (Seconds == 0)
  9158. {
  9159. Seconds = WinConfiguration->NotificationsTimeout;
  9160. }
  9161. FNoteTimer->Interval = Seconds * MSecsPerSec;
  9162. FNoteTimer->Enabled = true;
  9163. }
  9164. //---------------------------------------------------------------------------
  9165. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  9166. {
  9167. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  9168. }
  9169. //---------------------------------------------------------------------------
  9170. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  9171. {
  9172. if (NonVisualDataModule->SynchronizeBrowsingAction2->Checked)
  9173. {
  9174. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  9175. }
  9176. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  9177. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  9178. (NonVisualDataModule->SynchronizeBrowsingAction2->Checked ? 2 : 3)))),
  9179. 0, NULL, NULL);
  9180. }
  9181. //---------------------------------------------------------------------------
  9182. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  9183. {
  9184. bool NewShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  9185. UnicodeString Note =
  9186. FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1), (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, (NewShowHiddenFiles ? 2 : 3))));
  9187. PostNote(Note, 0, NULL, NULL);
  9188. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  9189. WinConfiguration->ShowHiddenFiles = NewShowHiddenFiles;
  9190. }
  9191. //---------------------------------------------------------------------------
  9192. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  9193. {
  9194. WinConfiguration->FormatSizeBytes = Style;
  9195. }
  9196. //---------------------------------------------------------------------------
  9197. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  9198. {
  9199. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  9200. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  9201. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  9202. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  9203. }
  9204. //---------------------------------------------------------------------------
  9205. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  9206. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  9207. {
  9208. if (Panel->Index == 0)
  9209. {
  9210. if (FOnNoteClick != NULL)
  9211. {
  9212. // prevent the user data from being freed by possible call
  9213. // to CancelNote or PostNote during call to OnNoteClick
  9214. std::unique_ptr<TObject> NoteData(FNoteData);
  9215. TNotifyEvent OnNoteClick = FOnNoteClick;
  9216. FNoteData = NULL;
  9217. // need to cancel the note as we are going to delete its user data
  9218. CancelNote(true);
  9219. OnNoteClick(NoteData.get());
  9220. }
  9221. }
  9222. if (HasActiveTerminal() &&
  9223. (Panel->Index >= Sender->Panels->Count - SessionPanelCount))
  9224. {
  9225. FileSystemInfo();
  9226. }
  9227. }
  9228. //---------------------------------------------------------------------------
  9229. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  9230. {
  9231. // workaround:
  9232. // 1) for unknown reason, disabling window, while minimized,
  9233. // prevents it from restoring, even if it was enabled again meanwhile
  9234. // 2) when disabling the main window, while another has focus
  9235. // minimize is no longer possible
  9236. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  9237. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  9238. // Shouldn't we use IsApplicationMinimized() here?
  9239. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  9240. {
  9241. Enabled = false;
  9242. }
  9243. FLockLevel++;
  9244. }
  9245. //---------------------------------------------------------------------------
  9246. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  9247. {
  9248. DebugAssert(FLockLevel > 0);
  9249. FLockLevel--;
  9250. if (FLockLevel == 0)
  9251. {
  9252. DebugAssert(FLockSuspendLevel == 0);
  9253. Enabled = true;
  9254. }
  9255. }
  9256. //---------------------------------------------------------------------------
  9257. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  9258. {
  9259. // We need to make sure that window is enabled when the last modal window closes
  9260. // otherwise focus is not restored correctly.
  9261. // So we re-enable the window when modal window opens and
  9262. // disable it back after is closes
  9263. if (FLockLevel > 0)
  9264. {
  9265. // while we have nesting counter, we know that we never be called
  9266. // recursivelly as Application->OnModalBegin is called only
  9267. // for the top-level modal window
  9268. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9269. {
  9270. // won't be disabled when conditions in LockWindow() were not satisfied
  9271. FDisabledOnLockSuspend = !Enabled;
  9272. // When minimized to tray (or actually when set to SW_HIDE),
  9273. // setting Enabled makes the window focusable even when there's
  9274. // modal window over it
  9275. if (!FTrayIcon->Visible)
  9276. {
  9277. Enabled = true;
  9278. }
  9279. }
  9280. FLockSuspendLevel++;
  9281. }
  9282. }
  9283. //---------------------------------------------------------------------------
  9284. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9285. {
  9286. if (FLockSuspendLevel > 0)
  9287. {
  9288. DebugAssert(FLockLevel > 0);
  9289. FLockSuspendLevel--;
  9290. // see comment in SuspendWindowLock
  9291. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9292. {
  9293. // Note that window can be enabled here, when we were minized to tray when
  9294. // was SuspendWindowLock() called.
  9295. // We should possibly do the same check as in LockWindow(),
  9296. // if it is ever possible that the consitions change between
  9297. // SuspendWindowLock() and ResumeWindowLock()
  9298. Enabled = !FDisabledOnLockSuspend;
  9299. }
  9300. }
  9301. }
  9302. //---------------------------------------------------------------------------
  9303. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9304. {
  9305. TTBXComboBoxItem * RemotePathComboBox =
  9306. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9307. if (!TextOnly)
  9308. {
  9309. TStrings * Items = RemotePathComboBox->Strings;
  9310. Items->BeginUpdate();
  9311. try
  9312. {
  9313. Items->Clear();
  9314. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9315. {
  9316. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9317. while (!IsUnixRootPath(APath))
  9318. {
  9319. int P = APath.LastDelimiter(L'/');
  9320. DebugAssert(P >= 0);
  9321. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9322. APath.SetLength(P - 1);
  9323. }
  9324. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9325. }
  9326. }
  9327. __finally
  9328. {
  9329. Items->EndUpdate();
  9330. }
  9331. }
  9332. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9333. // Setting ItemIndex to -1 does not reset its text
  9334. if (RemotePathComboBox->Strings->Count == 0)
  9335. {
  9336. RemotePathComboBox->Text = L"";
  9337. }
  9338. }
  9339. //---------------------------------------------------------------------------
  9340. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxAdjustImageIndex(
  9341. TTBXComboBoxItem * Sender, const UnicodeString DebugUsedArg(AText), int AIndex, int & ImageIndex)
  9342. {
  9343. if (AIndex < 0)
  9344. {
  9345. AIndex = Sender->ItemIndex;
  9346. }
  9347. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9348. }
  9349. //---------------------------------------------------------------------------
  9350. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9351. TTBXComboBoxItem * Sender, const UnicodeString AText, int AIndex,
  9352. int & ImageIndex)
  9353. {
  9354. DoRemotePathComboBoxAdjustImageIndex(Sender, AText, AIndex, ImageIndex);
  9355. }
  9356. //---------------------------------------------------------------------------
  9357. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9358. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9359. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9360. {
  9361. if (!IsLocalBrowserMode())
  9362. {
  9363. ARect.Left += (10 * AIndex);
  9364. }
  9365. }
  9366. //---------------------------------------------------------------------------
  9367. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9368. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9369. {
  9370. AWidth += (10 * AIndex);
  9371. }
  9372. //---------------------------------------------------------------------------
  9373. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxItemClick(TObject * Sender)
  9374. {
  9375. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9376. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9377. int Index = RemotePathComboBox->ItemIndex;
  9378. while (Index < RemotePathComboBox->Strings->Count - 1)
  9379. {
  9380. APath = UnixExtractFileDir(APath);
  9381. Index++;
  9382. }
  9383. // VanDyke style paths
  9384. if (APath.IsEmpty())
  9385. {
  9386. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9387. APath = ROOTDIRECTORY;
  9388. }
  9389. if (RemoteDirView->Path != APath)
  9390. {
  9391. RemoteDirView->Path = APath;
  9392. }
  9393. }
  9394. //---------------------------------------------------------------------------
  9395. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(TObject * Sender)
  9396. {
  9397. DoRemotePathComboBoxItemClick(Sender);
  9398. }
  9399. //---------------------------------------------------------------------------
  9400. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxCancel(TObject * Sender)
  9401. {
  9402. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9403. DebugUsedParam(Sender);
  9404. UpdateRemotePathComboBox(true);
  9405. }
  9406. //---------------------------------------------------------------------------
  9407. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9408. {
  9409. DoRemotePathComboBoxCancel(Sender);
  9410. }
  9411. //---------------------------------------------------------------------------
  9412. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9413. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9414. {
  9415. ResetIncrementalSearch();
  9416. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9417. DebugAssert(DirView != NULL);
  9418. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9419. {
  9420. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9421. // OnEditing is called also from TCustomListView::CanEdit
  9422. if (Edit != NULL)
  9423. {
  9424. EditSelectBaseName(Edit);
  9425. }
  9426. }
  9427. }
  9428. //---------------------------------------------------------------------------
  9429. //---------------------------------------------------------------------------
  9430. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9431. {
  9432. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9433. Result->AutoDetectDnD = false;
  9434. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9435. Result->RenderDataOn = rdoEnterAndDropSync;
  9436. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9437. return Result;
  9438. }
  9439. //---------------------------------------------------------------------------
  9440. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9441. {
  9442. if (IsWin10Build(2004))
  9443. {
  9444. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9445. BOOL DarkMode = FImmersiveDarkMode ? TRUE : FALSE;
  9446. const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  9447. DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
  9448. }
  9449. }
  9450. //---------------------------------------------------------------------------
  9451. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9452. {
  9453. TForm::CreateWnd();
  9454. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9455. UpdateDarkMode();
  9456. if (FSessionsDragDropFilesEx == NULL)
  9457. {
  9458. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9459. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9460. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9461. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9462. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9463. }
  9464. if (FQueueDragDropFilesEx == NULL)
  9465. {
  9466. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9467. // No need to set OnDragOver as we do not have any restrictions
  9468. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9469. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9470. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9471. }
  9472. }
  9473. //---------------------------------------------------------------------------
  9474. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9475. {
  9476. TForm::DestroyWnd();
  9477. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9478. FQueueDragDropFilesEx->DragDropControl = NULL;
  9479. }
  9480. //---------------------------------------------------------------------------
  9481. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9482. {
  9483. SideEnter(FCurrentSide);
  9484. FEverShown = true;
  9485. }
  9486. //---------------------------------------------------------------------------
  9487. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9488. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9489. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9490. {
  9491. if (!NonVisualDataModule->Busy)
  9492. {
  9493. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9494. Configuration->Usage->Inc(L"FileFinds");
  9495. LockWindow(true);
  9496. NonVisualDataModule->StartBusy();
  9497. WinConfiguration->LockedInterface = true;
  9498. try
  9499. {
  9500. Terminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9501. }
  9502. __finally
  9503. {
  9504. WinConfiguration->LockedInterface = false;
  9505. NonVisualDataModule->EndBusy();
  9506. UnlockWindow();
  9507. }
  9508. }
  9509. }
  9510. //---------------------------------------------------------------------------
  9511. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9512. {
  9513. if (!NonVisualDataModule->Busy)
  9514. {
  9515. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9516. SetFocus();
  9517. RemoteDirView->Path = UnixExtractFilePath(Path);
  9518. UnicodeString FileName = UnixExtractFileName(Path);
  9519. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9520. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9521. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9522. if (Item == NULL)
  9523. {
  9524. ReloadDirectory(osRemote);
  9525. Item = RemoteDirView->FindFileItem(FileName);
  9526. }
  9527. if (Item != NULL)
  9528. {
  9529. RemoteDirView->ItemFocused = Item;
  9530. RemoteDirView->ItemFocused->MakeVisible(false);
  9531. RemoteDirView->SetFocus();
  9532. }
  9533. }
  9534. }
  9535. //---------------------------------------------------------------------------
  9536. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9537. {
  9538. bool Result = !NonVisualDataModule->Busy;
  9539. if (Result)
  9540. {
  9541. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9542. }
  9543. return Result;
  9544. }
  9545. //---------------------------------------------------------------------------
  9546. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9547. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9548. {
  9549. if (CanOperateOnFoundFiles(ATerminal))
  9550. {
  9551. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9552. FOnFileOperationFinished = OnFileOperationFinished;
  9553. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9554. }
  9555. }
  9556. //---------------------------------------------------------------------------
  9557. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9558. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9559. {
  9560. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9561. }
  9562. //---------------------------------------------------------------------------
  9563. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9564. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9565. {
  9566. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9567. }
  9568. //---------------------------------------------------------------------------
  9569. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9570. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9571. {
  9572. if (CanOperateOnFoundFiles(ATerminal))
  9573. {
  9574. for (int Index = 0; Index < FileList->Count; Index++)
  9575. {
  9576. UnicodeString FileName = FileList->Strings[Index];
  9577. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9578. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9579. OnFileOperationFinished(osRemote, FileName, true);
  9580. }
  9581. }
  9582. }
  9583. //---------------------------------------------------------------------------
  9584. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9585. {
  9586. FFileFindTerminal = Terminal;
  9587. ShowFileFindDialog(
  9588. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9589. DoDownloadFoundFiles, DoEditFoundFiles);
  9590. }
  9591. //---------------------------------------------------------------------------
  9592. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9593. {
  9594. FTaskbarList = TaskbarList;
  9595. }
  9596. //---------------------------------------------------------------------------
  9597. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9598. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9599. {
  9600. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9601. if (Index >= 0)
  9602. {
  9603. if (Button == mbLeft)
  9604. {
  9605. // WORKAROUND: "Mouse down" is raised only after tab is switched.
  9606. // If switching tab (switching session) takes long enough for user
  9607. // to actually release the button, "mouse down" is still raised,
  9608. // but we do not get "mouse up" event, so dragging is not cancelled,
  9609. // prevent that by not beginning dragging in the first place.
  9610. if (IsKeyPressed(VK_LBUTTON))
  9611. {
  9612. // when user clicks the "+", we get mouse down only after the session
  9613. // is closed, when new session tab is already on X:Y, so dragging
  9614. // starts, prevent that
  9615. if (MilliSecondsBetween(Now(), FSessionsPageControlNewTabTime) > 500)
  9616. {
  9617. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9618. if (Terminal != NULL)
  9619. {
  9620. SessionsPageControl->BeginDrag(false);
  9621. }
  9622. }
  9623. }
  9624. }
  9625. else if (Button == mbMiddle)
  9626. {
  9627. // ignore middle-click for "New session tab" and the last local-local tab
  9628. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9629. if ((Session != NULL) && CanCloseSession(Session))
  9630. {
  9631. CloseSessionTab(Index);
  9632. }
  9633. }
  9634. }
  9635. }
  9636. //---------------------------------------------------------------------------
  9637. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9638. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9639. {
  9640. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9641. // do not allow dropping on the "+" tab
  9642. TTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9643. if ((TargetTerminal != NULL) &&
  9644. (SessionsPageControl->ActivePage->PageIndex != Index))
  9645. {
  9646. Configuration->Usage->Inc(L"SessionTabMoves");
  9647. // this is almost redundant as we would recreate tabs in SessionListChanged,
  9648. // but we want to actually prevent that to avoid flicker
  9649. SessionsPageControl->ActivePage->PageIndex = Index;
  9650. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->ActivePage);
  9651. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9652. }
  9653. }
  9654. //---------------------------------------------------------------------------
  9655. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9656. TObject * Sender, TObject * Source, int X, int Y,
  9657. TDragState /*State*/, bool & Accept)
  9658. {
  9659. Accept = (Sender == Source);
  9660. if (Accept)
  9661. {
  9662. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9663. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9664. // do not allow dragging to the "+" tab
  9665. Accept = (Terminal != NULL);
  9666. }
  9667. }
  9668. //---------------------------------------------------------------------------
  9669. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  9670. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  9671. {
  9672. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9673. if (Index < 0)
  9674. {
  9675. Effect = DROPEFFECT_NONE;
  9676. }
  9677. else
  9678. {
  9679. TManagedTerminal * TargetSession = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9680. // do not allow dropping on the "+" tab or on disconnected or local-local tabs
  9681. if (!IsActiveTerminal(TargetSession) || DebugAlwaysFalse(TargetSession->LocalBrowser))
  9682. {
  9683. Effect = DROPEFFECT_NONE;
  9684. }
  9685. else if ((TargetSession != Terminal) && (Effect == DROPEFFECT_MOVE))
  9686. {
  9687. Effect = DROPEFFECT_COPY;
  9688. }
  9689. }
  9690. }
  9691. //---------------------------------------------------------------------------
  9692. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9693. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9694. {
  9695. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9696. // do not allow dropping on local-local, disconnected and the "+" tab
  9697. // (though we do not seem to get here in that case anyway, contrary to RemoteFileControlDDTargetDrop)
  9698. TManagedTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9699. if (DebugAlwaysTrue(IsActiveTerminal(TargetTerminal) && !TargetTerminal->LocalBrowser))
  9700. {
  9701. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9702. if (!IsFileControl(DropSourceControl, osRemote))
  9703. {
  9704. TTerminalManager::Instance()->ActiveSession = TargetTerminal;
  9705. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9706. // Why don't we use Terminal->CurrentDirectory directly?
  9707. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9708. // do not force queue, drag drop
  9709. false, true);
  9710. }
  9711. }
  9712. }
  9713. //---------------------------------------------------------------------------
  9714. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9715. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9716. {
  9717. // Downloads are handled in RemoteFileControlDDEnd
  9718. if (!IsFileControl(DropSourceControl, osRemote))
  9719. {
  9720. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9721. Terminal->CurrentDirectory,
  9722. // force queue, drag drop
  9723. true, true);
  9724. }
  9725. }
  9726. //---------------------------------------------------------------------------
  9727. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9728. {
  9729. FShowing = false;
  9730. // Do not save empty workspace
  9731. if (WinConfiguration->AutoSaveWorkspace && (ManagedSession != NULL))
  9732. {
  9733. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9734. UnicodeString Name = WorkspaceName();
  9735. DoSaveWorkspace(
  9736. Name, DataList.get(),
  9737. !Configuration->DisablePasswordStoring &&
  9738. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9739. WinConfiguration->LastStoredSession = Name;
  9740. }
  9741. }
  9742. //---------------------------------------------------------------------------
  9743. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9744. {
  9745. TManagedTerminal * ManagedTerminal =
  9746. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9747. if (ManagedTerminal != NULL)
  9748. {
  9749. ManagedTerminal->DirectoryLoaded = Now();
  9750. }
  9751. }
  9752. //---------------------------------------------------------------------------
  9753. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9754. TListItem * /*Item*/, bool /*Selected*/)
  9755. {
  9756. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9757. switch (DirView->LastSelectMethod)
  9758. {
  9759. case smKeyboard:
  9760. Configuration->Usage->Inc(L"KeyboardSelections");
  9761. break;
  9762. case smMouse:
  9763. Configuration->Usage->Inc(L"MouseSelections");
  9764. break;
  9765. }
  9766. }
  9767. //---------------------------------------------------------------------------
  9768. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9769. {
  9770. if (Color != 0)
  9771. {
  9772. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9773. }
  9774. else
  9775. {
  9776. return FSessionTabImageIndex;
  9777. }
  9778. }
  9779. //---------------------------------------------------------------------------
  9780. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9781. {
  9782. TPngImageCollectionItem * Item =
  9783. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9784. FSessionColors->AddPng(Item->PngImage);
  9785. return FSessionColors->Count - 1;
  9786. }
  9787. //---------------------------------------------------------------------------
  9788. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9789. {
  9790. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9791. FNewRemoteTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewRemoteTabAction->ImageIndex);
  9792. FNewLocalTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewLocalTabAction->ImageIndex);
  9793. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9794. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9795. FLocalBrowserTabImageIndex = AddFixedSessionImage(LocalBrowserImageIndex);
  9796. }
  9797. //---------------------------------------------------------------------------
  9798. int TCustomScpExplorerForm::GetNewTabActionImageIndex()
  9799. {
  9800. return NonVisualDataModule->NewRemoteTabAction->ImageIndex;
  9801. }
  9802. //---------------------------------------------------------------------------
  9803. int TCustomScpExplorerForm::GetNewTabTabImageIndex(TOperationSide Side)
  9804. {
  9805. switch (Side)
  9806. {
  9807. case osLocal:
  9808. return FNewLocalTabTabImageIndex;
  9809. case osRemote:
  9810. case osCurrent:
  9811. return FNewRemoteTabTabImageIndex;
  9812. default:
  9813. DebugFail();
  9814. return -1;
  9815. }
  9816. }
  9817. //---------------------------------------------------------------------------
  9818. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9819. {
  9820. for (int Index = 0; Index < Control->ControlCount; Index++)
  9821. {
  9822. TControl * AControl = Control->Controls[Index];
  9823. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9824. if (WinControl != NULL)
  9825. {
  9826. CollectItemsWithTextDisplayMode(WinControl);
  9827. }
  9828. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9829. if (Toolbar != NULL)
  9830. {
  9831. // we care for top-level items only
  9832. TTBCustomItem * Items = Toolbar->Items;
  9833. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9834. {
  9835. TTBCustomItem * Item = Items->Items[ItemIndex];
  9836. if (((Item->DisplayMode == nbdmImageAndText) ||
  9837. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9838. EligibleForImageDisplayMode(Item))
  9839. {
  9840. FItemsWithTextDisplayMode.insert(Item);
  9841. }
  9842. }
  9843. }
  9844. }
  9845. }
  9846. //---------------------------------------------------------------------------
  9847. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9848. {
  9849. return true;
  9850. }
  9851. //---------------------------------------------------------------------------
  9852. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9853. {
  9854. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9855. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9856. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9857. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9858. bool Result = true;
  9859. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9860. {
  9861. TTBCustomItem * Item = *i;
  9862. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9863. if (Label != NULL)
  9864. {
  9865. // optimization
  9866. if (Label->Visible == SelectiveToolbarText)
  9867. {
  9868. Result = false;
  9869. }
  9870. else
  9871. {
  9872. Label->Visible = SelectiveToolbarText;
  9873. }
  9874. }
  9875. else
  9876. {
  9877. // optimization
  9878. if (Item->DisplayMode == DisplayMode)
  9879. {
  9880. Result = false;
  9881. }
  9882. else
  9883. {
  9884. Item->DisplayMode = DisplayMode;
  9885. }
  9886. }
  9887. ++i;
  9888. }
  9889. UpdateNewTabTab();
  9890. return Result;
  9891. }
  9892. //---------------------------------------------------------------------------
  9893. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9894. {
  9895. DebugFail();
  9896. }
  9897. //---------------------------------------------------------------------------
  9898. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9899. {
  9900. // Among other, a lock level is non zero, while directory is being loaded,
  9901. // even when it happens as a result of dirview navigation,
  9902. // i.e. when TNonVisualDataModule is NOT FBusy.
  9903. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9904. // Among other this prevents a panel auto update to occur while
  9905. // directory is changing.
  9906. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9907. }
  9908. //---------------------------------------------------------------------------
  9909. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * Action, TActionAllowed Allowed)
  9910. {
  9911. // While the window is disabled, we still seem to process menu shortcuts at least,
  9912. // so stop it at least here.
  9913. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9914. bool Result;
  9915. if (Allowed == aaUpdate)
  9916. {
  9917. Result = true;
  9918. }
  9919. else
  9920. {
  9921. Result = !NonVisualDataModule->Busy;
  9922. if (Result && (Allowed == aaShortCut))
  9923. {
  9924. // Otherwise F5 for local-local will be swallowed by disabled local<>remote actions in TCustomScpExplorerForm::KeyDown
  9925. Action->Update();
  9926. Result = Action->Enabled;
  9927. }
  9928. }
  9929. return Result;
  9930. }
  9931. //---------------------------------------------------------------------------
  9932. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9933. {
  9934. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9935. }
  9936. //---------------------------------------------------------------------------
  9937. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9938. {
  9939. // This is somewhat redundant to LockWindow() call from
  9940. // TTerminalManager::TerminalReadDirectoryProgress.
  9941. // But disabling window is known not to block keyboard shorcuts
  9942. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9943. if (Busy > 0)
  9944. {
  9945. if (NonVisualDataModule->Busy)
  9946. {
  9947. State = false;
  9948. }
  9949. else
  9950. {
  9951. NonVisualDataModule->StartBusy();
  9952. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9953. LockWindow();
  9954. }
  9955. }
  9956. else if (Busy < 0)
  9957. {
  9958. UnlockWindow();
  9959. NonVisualDataModule->EndBusy();
  9960. }
  9961. else
  9962. {
  9963. State = NonVisualDataModule->Busy;
  9964. }
  9965. }
  9966. //---------------------------------------------------------------------------
  9967. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9968. {
  9969. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9970. if (Index >= 0)
  9971. {
  9972. TPopupMenu * PopupMenu = NULL;
  9973. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9974. if (Session != NULL)
  9975. {
  9976. SessionsPageControl->ActivePageIndex = Index;
  9977. if (DebugAlwaysTrue(SessionTabSwitched()))
  9978. {
  9979. // copied from TControl.WMContextMenu
  9980. SendCancelMode(SessionsPageControl);
  9981. // explicit popup instead of using PopupMenu property
  9982. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9983. // while connecting yet not-connected session and hence
  9984. // allowing an access to commands over not-completelly connected session
  9985. PopupMenu = Session->LocalBrowser ? NonVisualDataModule->LocalBrowserPopup : NonVisualDataModule->SessionsPopup;
  9986. }
  9987. }
  9988. else
  9989. {
  9990. if (SupportsLocalBrowser())
  9991. {
  9992. PopupMenu = NonVisualDataModule->NewTabPopup;
  9993. }
  9994. }
  9995. if (PopupMenu != NULL)
  9996. {
  9997. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9998. PopupMenu->PopupComponent = SessionsPageControl;
  9999. PopupMenu->Popup(Point.x, Point.y);
  10000. }
  10001. }
  10002. Handled = true;
  10003. }
  10004. //---------------------------------------------------------------------------
  10005. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  10006. {
  10007. return HasActiveTerminal() && Terminal->IsCapable[fcChangePassword];
  10008. }
  10009. //---------------------------------------------------------------------------
  10010. void __fastcall TCustomScpExplorerForm::ChangePassword()
  10011. {
  10012. Configuration->Usage->Inc(L"PasswordChanges");
  10013. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  10014. ChangePasswordData->DisableAuthentationsExceptPassword();
  10015. ChangePasswordData->ChangePassword = true;
  10016. std::unique_ptr<TTerminal> ChangePasswordSession(
  10017. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  10018. ChangePasswordSession->AutoReadDirectory = false;
  10019. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  10020. {
  10021. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  10022. }
  10023. else
  10024. {
  10025. Configuration->Usage->Inc(L"PasswordChangesFailed");
  10026. }
  10027. }
  10028. //---------------------------------------------------------------------------
  10029. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  10030. {
  10031. // No nice way to assert SSH2
  10032. return HasActiveTerminal() && (Terminal->FSProtocol == cfsSFTP);
  10033. }
  10034. //---------------------------------------------------------------------------
  10035. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  10036. {
  10037. TOperationVisualizer OperationVisualizer;
  10038. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  10039. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  10040. }
  10041. //---------------------------------------------------------------------------
  10042. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  10043. {
  10044. TForm::ChangeScale(M, D);
  10045. int APixelsPerInch = GetControlPixelsPerInch(this);
  10046. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  10047. try
  10048. {
  10049. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  10050. }
  10051. __finally
  10052. {
  10053. GUIConfiguration->ChangeResourceModule(ResourceModule);
  10054. }
  10055. }
  10056. //---------------------------------------------------------------------------
  10057. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  10058. {
  10059. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  10060. }
  10061. //---------------------------------------------------------------------
  10062. class TPublicControl : public TControl
  10063. {
  10064. friend class TCustomScpExplorerForm;
  10065. };
  10066. //---------------------------------------------------------------------------
  10067. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  10068. {
  10069. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  10070. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  10071. }
  10072. //---------------------------------------------------------------------------
  10073. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  10074. {
  10075. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  10076. OpenBookmark(Side, Bookmark);
  10077. }
  10078. //---------------------------------------------------------------------------
  10079. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  10080. {
  10081. DoBookmarkClick(osLocal, Sender);
  10082. }
  10083. //---------------------------------------------------------------------------
  10084. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  10085. {
  10086. DoBookmarkClick(osRemote, Sender);
  10087. }
  10088. //---------------------------------------------------------------------------
  10089. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  10090. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  10091. {
  10092. if (BookmarkList != NULL)
  10093. {
  10094. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  10095. if (IsLocalBrowserMode())
  10096. {
  10097. Side = osLocal;
  10098. }
  10099. if (!WinConfiguration->UseLocationProfiles)
  10100. {
  10101. std::unique_ptr<TStringList> Directories(new TStringList());
  10102. Directories->CaseSensitive = (Side == osRemote);
  10103. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10104. {
  10105. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10106. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  10107. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  10108. {
  10109. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10110. Item->Caption = EscapeHotkey(Directory);
  10111. Item->ShortCut = Bookmark->ShortCut;
  10112. Item->OnClick = OnBookmarkClick;
  10113. Item->Tag = reinterpret_cast<int>(Bookmark);
  10114. Directories->Add(Directory);
  10115. Menu->Add(Item.release());
  10116. }
  10117. }
  10118. }
  10119. else
  10120. {
  10121. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  10122. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10123. {
  10124. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10125. if (!Bookmark->Node.IsEmpty())
  10126. {
  10127. Folders->Add(Bookmark->Node);
  10128. }
  10129. }
  10130. for (int Index = 0; Index < Folders->Count; Index++)
  10131. {
  10132. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  10133. Item->Caption = Folders->Strings[Index];
  10134. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction2->ImageIndex;
  10135. Folders->Objects[Index] = Item.get();
  10136. Menu->Add(Item.release());
  10137. }
  10138. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10139. {
  10140. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10141. TTBCustomItem * Parent;
  10142. if (!Bookmark->Node.IsEmpty())
  10143. {
  10144. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  10145. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  10146. }
  10147. else
  10148. {
  10149. Parent = Menu;
  10150. }
  10151. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10152. Item->Caption = Bookmark->Name;
  10153. Item->ShortCut = Bookmark->ShortCut;
  10154. Item->OnClick = OnBookmarkClick;
  10155. Item->Tag = reinterpret_cast<int>(Bookmark);
  10156. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  10157. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  10158. {
  10159. // No hint for location profiles that are actually mere "bookmarks"
  10160. }
  10161. else
  10162. {
  10163. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  10164. UnicodeString LongHint;
  10165. if (!Bookmark->Local.IsEmpty())
  10166. {
  10167. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  10168. }
  10169. if (!Bookmark->Remote.IsEmpty())
  10170. {
  10171. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  10172. }
  10173. AddToList(Hint, LongHint, L"|");
  10174. Item->Hint = Hint;
  10175. }
  10176. Parent->Add(Item.release());
  10177. }
  10178. }
  10179. AddMenuSeparator(Menu);
  10180. }
  10181. }
  10182. //---------------------------------------------------------------------------
  10183. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  10184. {
  10185. Menu->Clear();
  10186. std::unique_ptr<TTBCustomItem> Item;
  10187. Item.reset(new TTBXItem(Owner));
  10188. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  10189. Item->Options = Item->Options << tboDefault;
  10190. Menu->Add(Item.release());
  10191. AddMenuSeparator(Menu);
  10192. if (Terminal != NULL)
  10193. {
  10194. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  10195. }
  10196. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  10197. Item.reset(new TTBXItem(Owner));
  10198. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction2 : NonVisualDataModule->RemoteAddBookmarkAction2;
  10199. Menu->Add(Item.release());
  10200. }
  10201. //---------------------------------------------------------------------------
  10202. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  10203. {
  10204. bool Result = true;
  10205. bool Remote = !IsSideLocalBrowser(Side);
  10206. try
  10207. {
  10208. UnicodeString APath = Path;
  10209. if (Remote)
  10210. {
  10211. Terminal->ExceptionOnFail = true;
  10212. }
  10213. else
  10214. {
  10215. APath = ExpandEnvironmentVariables(Path);
  10216. }
  10217. try
  10218. {
  10219. DirView(Side)->Path = APath;
  10220. }
  10221. __finally
  10222. {
  10223. if (Remote)
  10224. {
  10225. Terminal->ExceptionOnFail = false;
  10226. }
  10227. }
  10228. }
  10229. catch (Exception & E)
  10230. {
  10231. if (!Remote || Terminal->Active)
  10232. {
  10233. ShowExtendedExceptionEx(Terminal, &E);
  10234. Result = false;
  10235. }
  10236. else
  10237. {
  10238. throw;
  10239. }
  10240. }
  10241. return Result;
  10242. }
  10243. //---------------------------------------------------------------------------
  10244. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  10245. {
  10246. // Make sure there some feedback even when loading tiny local folders
  10247. TInstantOperationVisualizer Visualizer;
  10248. DirView(Side)->ReloadDirectory();
  10249. }
  10250. //---------------------------------------------------------------------------
  10251. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  10252. {
  10253. if (Index == SessionsPageControl->TabIndex)
  10254. {
  10255. CloseSession();
  10256. }
  10257. else
  10258. {
  10259. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10260. TTerminal * Terminal = GetSessionTabSession(TabSheet);
  10261. TTerminalManager * Manager = TTerminalManager::Instance();
  10262. if (!Terminal->Active)
  10263. {
  10264. Manager->FreeTerminal(Terminal);
  10265. }
  10266. else
  10267. {
  10268. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  10269. if (CanCloseQueue(Queue))
  10270. {
  10271. Manager->FreeTerminal(Terminal);
  10272. }
  10273. }
  10274. }
  10275. }
  10276. //---------------------------------------------------------------------------
  10277. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabButtonClick(TPageControl *, int Index)
  10278. {
  10279. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10280. if (Session == NULL)
  10281. {
  10282. TRect ButtonRect = SessionsPageControl->TabButtonRect(Index);
  10283. ButtonRect = TRect(SessionsPageControl->ClientToScreen(ButtonRect.TopLeft()), SessionsPageControl->ClientToScreen(ButtonRect.BottomRight()));
  10284. //TPoint P = TPoint(ButtonRect.Left, ButtonRect.Bottom);
  10285. //P = SessionsPageControl->ClientToScreen(P);
  10286. MenuPopup(NonVisualDataModule->NewTabPopup, ButtonRect, SessionsPageControl);
  10287. }
  10288. else
  10289. {
  10290. CloseSessionTab(Index);
  10291. }
  10292. }
  10293. //---------------------------------------------------------------------------
  10294. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  10295. {
  10296. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  10297. {
  10298. TInstantOperationVisualizer Visualizer;
  10299. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  10300. // before we create a new data object
  10301. ClipboardClear();
  10302. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  10303. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  10304. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  10305. // Intermediate variable to WORKAROUND a crash in compiler
  10306. TObjectList * ClipboardFakeMonitors =
  10307. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  10308. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  10309. if (FClipboardDragDropFilesEx.get() == NULL)
  10310. {
  10311. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  10312. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  10313. }
  10314. FClipboardDragDropFilesEx->FileList->Clear();
  10315. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  10316. FClipboardDragDropFilesEx->CopyToClipboard();
  10317. FClipboardTerminal = Terminal;
  10318. // Need full paths, as cwd can be different once files are pasted
  10319. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  10320. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  10321. {
  10322. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  10323. }
  10324. }
  10325. }
  10326. //---------------------------------------------------------------------------
  10327. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  10328. {
  10329. if (OpenClipboard(0))
  10330. {
  10331. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  10332. CloseClipboard();
  10333. }
  10334. }
  10335. //---------------------------------------------------------------------------
  10336. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  10337. {
  10338. return !FClipboardFakeDirectory.IsEmpty();
  10339. }
  10340. //---------------------------------------------------------------------------
  10341. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  10342. {
  10343. RemoveDir(ApiPath(FClipboardFakeDirectory));
  10344. FClipboardFakeDirectory = UnicodeString();
  10345. FClipboardTerminal = NULL;
  10346. if (FDownloadingFromClipboard)
  10347. {
  10348. // We are called by the monitor, so attempt to release it would deadlock
  10349. FClipboardFakeMonitorsPendingReset = true;
  10350. }
  10351. else
  10352. {
  10353. FClipboardFakeMonitors.reset(NULL);
  10354. }
  10355. }
  10356. //---------------------------------------------------------------------------
  10357. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10358. {
  10359. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10360. {
  10361. ClipboardStop();
  10362. }
  10363. }
  10364. //---------------------------------------------------------------------------
  10365. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10366. {
  10367. TTerminalManager * Manager = TTerminalManager::Instance();
  10368. Manager->ActiveSession = FClipboardTerminal;
  10369. TTransferOperationParam Params;
  10370. Params.TargetDirectory = TargetDirectory;
  10371. Params.DragDrop = DragDrop;
  10372. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10373. }
  10374. //---------------------------------------------------------------------------
  10375. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10376. {
  10377. // It can actually rarelly happen that some random file is created, while we are shutting down the monitor
  10378. // (as it pumps a Windows message queue while being shutted down)
  10379. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10380. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10381. {
  10382. // Can fail as it can be e.g. locked by AV, so we retry that later
  10383. bool Removed = RemoveDir(ApiPath(FileName));
  10384. try
  10385. {
  10386. if (!NonVisualDataModule->Busy)
  10387. {
  10388. DebugAssert(!FClipboardFakeMonitorsPendingReset);
  10389. FClipboardFakeMonitorsPendingReset = false;
  10390. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10391. TAutoFlag Flag(FDownloadingFromClipboard);
  10392. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  10393. }
  10394. }
  10395. __finally
  10396. {
  10397. if (!Removed)
  10398. {
  10399. RemoveDir(ApiPath(FileName));
  10400. }
  10401. }
  10402. }
  10403. }
  10404. //---------------------------------------------------------------------------
  10405. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10406. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10407. {
  10408. TFileMasks::TParams MaskParams;
  10409. MaskParams.Size = Size;
  10410. MaskParams.Modification = Modification;
  10411. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10412. bool Local = (dynamic_cast<TUnixDirView *>(DirView) == NULL);
  10413. // TUnixDirView::ItemFullFileName returns trailing backslash for directories, what filemask cannot deal with
  10414. if (!Local && Directory)
  10415. {
  10416. FileName = UnixExcludeTrailingBackslash(FileName);
  10417. }
  10418. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10419. {
  10420. bool ImplicitMatch;
  10421. if (Iter->FileMask.Matches(FileName, Local, Directory, &MaskParams, false, ImplicitMatch) &&
  10422. !ImplicitMatch)
  10423. {
  10424. Color = Iter->Color;
  10425. break;
  10426. }
  10427. }
  10428. }
  10429. //---------------------------------------------------------------------------
  10430. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10431. {
  10432. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10433. if (!DirView->IsEditing())
  10434. {
  10435. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10436. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10437. // while searching
  10438. if (Key == VK_BACK)
  10439. {
  10440. if (!FIncrementalSearch.IsEmpty())
  10441. {
  10442. if (FIncrementalSearch.Length() == 1)
  10443. {
  10444. ResetIncrementalSearch();
  10445. }
  10446. else
  10447. {
  10448. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  10449. IncrementalSearch(NewText, false, false);
  10450. }
  10451. Key = 0;
  10452. }
  10453. }
  10454. }
  10455. }
  10456. //---------------------------------------------------------------------------
  10457. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  10458. {
  10459. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10460. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  10461. {
  10462. // Filter control sequences.
  10463. // When not searching yet, prefer use of the space for toggling file selection
  10464. // (so we cannot incrementally search for a string starting with a space).
  10465. if ((Key > VK_SPACE) ||
  10466. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  10467. {
  10468. IncrementalSearch(FIncrementalSearch + Key, false, false);
  10469. Key = 0;
  10470. }
  10471. }
  10472. }
  10473. //---------------------------------------------------------------------------
  10474. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  10475. {
  10476. if (!FIncrementalSearch.IsEmpty())
  10477. {
  10478. FIncrementalSearch = L"";
  10479. DirView(osCurrent)->UpdateStatusBar();
  10480. }
  10481. }
  10482. //---------------------------------------------------------------------------
  10483. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10484. {
  10485. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  10486. if (Item == NULL)
  10487. {
  10488. MessageBeep(MB_ICONHAND);
  10489. }
  10490. else
  10491. {
  10492. TCustomDirView * ADirView = DirView(osCurrent);
  10493. {
  10494. TAutoNestingCounter Guard(FIncrementalSearching);
  10495. ADirView->FocusItem(Item);
  10496. if (ADirView->NortonLike == nlOff)
  10497. {
  10498. if ((ADirView->Selected != Item) ||
  10499. (ADirView->SelCount > 1))
  10500. {
  10501. ADirView->ClearSelection();
  10502. ADirView->Selected = Item;
  10503. }
  10504. }
  10505. }
  10506. FIncrementalSearch = Text;
  10507. Item->MakeVisible(false);
  10508. TListItem * NextItem = SearchFile(Text, true, Reverse);
  10509. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  10510. ADirView->UpdateStatusBar();
  10511. }
  10512. }
  10513. //---------------------------------------------------------------------------
  10514. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  10515. {
  10516. int Index = Item->Index;
  10517. if (!Reverse)
  10518. {
  10519. Index++;
  10520. if (Index >= Item->Owner->Count)
  10521. {
  10522. Index = 0;
  10523. }
  10524. }
  10525. else
  10526. {
  10527. Index--;
  10528. if (Index < 0)
  10529. {
  10530. Index = Item->Owner->Count - 1;
  10531. }
  10532. }
  10533. return Item->Owner->Item[Index];
  10534. }
  10535. //---------------------------------------------------------------------------
  10536. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10537. {
  10538. TCustomDirView * ADirView = DirView(osCurrent);
  10539. if (ADirView->Items->Count == 0)
  10540. {
  10541. return NULL;
  10542. }
  10543. else
  10544. {
  10545. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  10546. TListItem * Item = CurrentItem;
  10547. if (SkipCurrent)
  10548. {
  10549. Item = DebugNotNull(GetNextFile(Item, Reverse));
  10550. }
  10551. while (true)
  10552. {
  10553. bool Matches = false;
  10554. switch (WinConfiguration->PanelSearch)
  10555. {
  10556. case isNameStartOnly:
  10557. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item).SubString(1, Text.Length()), Text);
  10558. break;
  10559. case isName:
  10560. Matches = ContainsTextSemiCaseSensitive(ADirView->ItemFileName(Item), Text);
  10561. break;
  10562. case isAll:
  10563. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  10564. int Index = 0;
  10565. while ((Index < ColCount) && !Matches)
  10566. {
  10567. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  10568. if (Visible)
  10569. {
  10570. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  10571. }
  10572. Index++;
  10573. }
  10574. break;
  10575. }
  10576. if (Matches)
  10577. {
  10578. return Item;
  10579. }
  10580. Item = GetNextFile(Item, Reverse);
  10581. if (Item == CurrentItem)
  10582. {
  10583. return NULL;
  10584. }
  10585. }
  10586. }
  10587. }
  10588. //---------------------------------------------------------------------------
  10589. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10590. {
  10591. ResetIncrementalSearch();
  10592. }
  10593. //---------------------------------------------------------------------------
  10594. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10595. {
  10596. if (Message.CharCode == VK_TAB)
  10597. {
  10598. if (!FIncrementalSearch.IsEmpty())
  10599. {
  10600. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10601. bool Reverse = Shift.Contains(ssShift);
  10602. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10603. Message.Result = 1;
  10604. return;
  10605. }
  10606. }
  10607. else if (Message.CharCode == VK_ESCAPE)
  10608. {
  10609. if (!FIncrementalSearch.IsEmpty())
  10610. {
  10611. ResetIncrementalSearch();
  10612. Message.Result = 1;
  10613. return;
  10614. }
  10615. }
  10616. TForm::Dispatch(&Message);
  10617. }
  10618. //---------------------------------------------------------------------------
  10619. void __fastcall TCustomScpExplorerForm::Deactivate()
  10620. {
  10621. TForm::Deactivate();
  10622. // E.g. when switching to an internal editor window
  10623. ResetIncrementalSearch();
  10624. }
  10625. //---------------------------------------------------------------------------
  10626. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10627. {
  10628. // When switching to another application
  10629. ResetIncrementalSearch();
  10630. }
  10631. //---------------------------------------------------------------------------
  10632. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10633. {
  10634. ResetIncrementalSearch();
  10635. }
  10636. //---------------------------------------------------------------------------
  10637. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10638. {
  10639. if (FIncrementalSearching <= 0)
  10640. {
  10641. ResetIncrementalSearch();
  10642. }
  10643. }
  10644. //---------------------------------------------------------------------------
  10645. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10646. {
  10647. CountClicksForWindowPrint(this);
  10648. }
  10649. //---------------------------------------------------------------------------
  10650. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10651. {
  10652. CenterReconnectToolbar();
  10653. }
  10654. //---------------------------------------------------------------------------
  10655. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10656. {
  10657. if (RemoteDirView->ItemFocused != NULL)
  10658. {
  10659. RemoteDirView->ItemFocused->Selected = true;
  10660. }
  10661. }
  10662. //---------------------------------------------------------------------------
  10663. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  10664. {
  10665. if (!FQueueStatusUpdating)
  10666. {
  10667. bool Refresh;
  10668. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  10669. {
  10670. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  10671. }
  10672. else
  10673. {
  10674. if (FQueueFileList->GetCount() > 0)
  10675. {
  10676. FQueueFileList->Clear();
  10677. Refresh = true;
  10678. }
  10679. }
  10680. int Count = FQueueFileList->GetCount();
  10681. if (QueueFileList->Items->Count != Count)
  10682. {
  10683. DebugAssert(Refresh);
  10684. QueueFileList->Items->Count = Count;
  10685. }
  10686. if (Refresh)
  10687. {
  10688. QueueFileList->Invalidate();
  10689. }
  10690. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  10691. }
  10692. }
  10693. //---------------------------------------------------------------------------
  10694. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  10695. {
  10696. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  10697. }
  10698. //---------------------------------------------------------------------------
  10699. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  10700. {
  10701. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  10702. // See TSynchronizeChecklistDialog::UpdateTimer
  10703. if (QueueFileList->Items->Count > 0)
  10704. {
  10705. QueueFileList->Items->Item[0]->MakeVisible(false);
  10706. }
  10707. UpdateQueueFileList();
  10708. }
  10709. //---------------------------------------------------------------------------
  10710. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  10711. {
  10712. Active = QueueView3->Focused() || QueueFileList->Focused();
  10713. }
  10714. //---------------------------------------------------------------------------
  10715. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  10716. {
  10717. QueueLabelUpdateStatus();
  10718. }
  10719. //---------------------------------------------------------------------------
  10720. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  10721. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  10722. {
  10723. int State = FQueueFileList->GetState(Item->Index);
  10724. if (State == qfsProcessed)
  10725. {
  10726. Sender->Canvas->Font->Color = clGrayText;
  10727. }
  10728. }
  10729. //---------------------------------------------------------------------------
  10730. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  10731. {
  10732. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  10733. QueueFileList->Column[0]->Width =
  10734. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  10735. }
  10736. //---------------------------------------------------------------------------
  10737. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  10738. {
  10739. QueueFileListColumnAutoSize();
  10740. }
  10741. //---------------------------------------------------------------------------
  10742. void __fastcall TCustomScpExplorerForm::CloseApp()
  10743. {
  10744. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  10745. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  10746. // So we clear the flag here.
  10747. DebugAssert(NonVisualDataModule->Busy);
  10748. NonVisualDataModule->EndBusy();
  10749. try
  10750. {
  10751. Close();
  10752. }
  10753. __finally
  10754. {
  10755. NonVisualDataModule->StartBusy();
  10756. }
  10757. }
  10758. //---------------------------------------------------------------------------
  10759. bool __fastcall TCustomScpExplorerForm::IsActiveTerminal(TTerminal * Terminal)
  10760. {
  10761. return (Terminal != NULL) && Terminal->Active;
  10762. }
  10763. //---------------------------------------------------------------------------
  10764. bool __fastcall TCustomScpExplorerForm::HasManagedSession()
  10765. {
  10766. return (ManagedSession != NULL);
  10767. }
  10768. //---------------------------------------------------------------------------
  10769. bool __fastcall TCustomScpExplorerForm::HasActiveTerminal()
  10770. {
  10771. return IsActiveTerminal(Terminal);
  10772. }
  10773. //---------------------------------------------------------------------------
  10774. void TCustomScpExplorerForm::LocalLocalCopy(
  10775. TFileOperation, TOperationSide, bool DebugUsedArg(OnFocused), bool DebugUsedArg(NoConfirmation),
  10776. bool DebugUsedArg(DragDrop), unsigned int DebugUsedArg(Flags))
  10777. {
  10778. DebugFail();
  10779. }
  10780. //---------------------------------------------------------------------------
  10781. void TCustomScpExplorerForm::LocalLocalCopyCommand(TFileOperation Operation, TOperationSide Side, bool OnFocused, unsigned int Flags)
  10782. {
  10783. LocalLocalCopy(Operation, Side, OnFocused, !WinConfiguration->ConfirmTransferring, false, Flags);
  10784. }
  10785. //---------------------------------------------------------------------------
  10786. void __fastcall TCustomScpExplorerForm::SessionsPageControlResize(TObject *)
  10787. {
  10788. // Changing tab list triggers the OnResize.
  10789. if (!FUpdatingSessionTabs)
  10790. {
  10791. // UpdateTabsCaptionTruncation should be enough
  10792. SessionListChanged(true);
  10793. }
  10794. }
  10795. //---------------------------------------------------------------------------
  10796. UnicodeString TCustomScpExplorerForm::GetSessionPath(TManagedTerminal * ASession, TOperationSide Side)
  10797. {
  10798. if (ASession == ManagedSession)
  10799. {
  10800. UpdateSession(ASession);
  10801. }
  10802. TSessionData * Data = ASession->StateData;
  10803. UnicodeString Result;
  10804. if (Side == osLocal)
  10805. {
  10806. Result = Data->LocalDirectory;
  10807. }
  10808. else if (DebugAlwaysTrue(Side == osOther))
  10809. {
  10810. Result = ASession->LocalBrowser ? Data->OtherLocalDirectory : Data->RemoteDirectory;
  10811. }
  10812. return Result;
  10813. }
  10814. //---------------------------------------------------------------------------
  10815. UnicodeString TCustomScpExplorerForm::GetTabHintDetails(TManagedTerminal * ASession)
  10816. {
  10817. UnicodeString Result;
  10818. if (!ASession->Active)
  10819. {
  10820. Result = LoadStr(STATUS_NOT_CONNECTED2);
  10821. }
  10822. else
  10823. {
  10824. Result = GetSessionPath(ASession, osRemote);
  10825. }
  10826. return Result;
  10827. }
  10828. //---------------------------------------------------------------------------
  10829. UnicodeString TCustomScpExplorerForm::GetNewTabHintDetails()
  10830. {
  10831. return LoadStr(NEW_REMOTE_TAB_HINT);
  10832. }
  10833. //---------------------------------------------------------------------------
  10834. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabHint(TPageControl *, int Index, UnicodeString & Hint)
  10835. {
  10836. if (Index >= 0)
  10837. {
  10838. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10839. TManagedTerminal * TabSession = GetSessionTabSession(TabSheet);
  10840. if (TabSession == NULL)
  10841. {
  10842. UnicodeString NewTabTabCaption = GetNewTabTabCaption();
  10843. if (NewTabTabCaption != TabSheet->Caption)
  10844. {
  10845. Hint = NewTabTabCaption + L"|" + GetNewTabHintDetails();
  10846. }
  10847. }
  10848. else
  10849. {
  10850. TTerminalManager * Manager = TTerminalManager::Instance();
  10851. UnicodeString TabCaption = Manager->GetSessionTitle(TabSession, true);
  10852. Hint = TabCaption + L"|" + GetTabHintDetails(TabSession);
  10853. }
  10854. }
  10855. }
  10856. //---------------------------------------------------------------------------
  10857. typedef std::vector<TListItem *> TListItemsVector;
  10858. struct TCalculateSizeOperation
  10859. {
  10860. TCalculateSizeStats Stats;
  10861. TListItemsVector ListItems;
  10862. size_t Index;
  10863. };
  10864. //---------------------------------------------------------------------------
  10865. void TCustomScpExplorerForm::CalculateDirectorySizes()
  10866. {
  10867. TCalculateSizeOperation CalculateSizeOperation;
  10868. TOperationSide Side = GetSide(osCurrent);
  10869. TCustomDirView * ADirView = DirView(Side);
  10870. bool FullPath = IsSideLocalBrowser(Side);
  10871. std::unique_ptr<TStrings> AllFileList(ADirView->CreateFileList(false, FullPath, NULL, true));
  10872. std::unique_ptr<TStrings> FileList(new TStringList());
  10873. for (int Index = 0; Index < AllFileList->Count; Index++)
  10874. {
  10875. TListItem * Item = DebugNotNull(dynamic_cast<TListItem *>(AllFileList->Objects[Index]));
  10876. if (ADirView->ItemIsDirectory(Item))
  10877. {
  10878. FileList->AddObject(AllFileList->Strings[Index], ADirView->ItemData(Item));
  10879. CalculateSizeOperation.ListItems.push_back(Item);
  10880. }
  10881. }
  10882. CalculateSizeOperation.Index = 0;
  10883. __int64 Size = 0;
  10884. TValueRestorer<TCalculateSizeOperation *> DirectorySizeOperationRestorer(FCalculateSizeOperation);
  10885. FCalculateSizeOperation = &CalculateSizeOperation;
  10886. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  10887. FOnFileOperationFinished = DirectorySizeCalculated;
  10888. TValueRestorer<TFileOperation> PrimaryOperationRestorer(FPrimaryOperation);
  10889. FPrimaryOperation = foCalculateSize;
  10890. TCalculatedSizes CalculatedSizes;
  10891. CalculateSizeOperation.Stats.CalculatedSizes = &CalculatedSizes;
  10892. if (Side == osLocal)
  10893. {
  10894. Terminal->CalculateLocalFilesSize(FileList.get(), Size, NULL, true, NULL, &CalculatedSizes);
  10895. }
  10896. else
  10897. {
  10898. TCalculateSizeParams Params;
  10899. Params.Stats = &CalculateSizeOperation.Stats;
  10900. Params.UseCache = true;
  10901. Terminal->CalculateFilesSize(FileList.get(), Size, Params);
  10902. }
  10903. }
  10904. //---------------------------------------------------------------------------
  10905. void __fastcall TCustomScpExplorerForm::DirectorySizeCalculated(TOperationSide Side, const UnicodeString & FileName, bool)
  10906. {
  10907. DebugAlwaysTrue(FCalculateSizeOperation != NULL);
  10908. TListItem * Item = VisualiseOperationFinished(Side, FileName, false);
  10909. TCustomDirView * ADirView = dynamic_cast<TCustomDirView *>(Item->Owner->Owner);
  10910. // optimization
  10911. bool GotExpectedItem =
  10912. (FCalculateSizeOperation->Index < FCalculateSizeOperation->ListItems.size()) &&
  10913. (FCalculateSizeOperation->ListItems[FCalculateSizeOperation->Index] == Item);
  10914. int Index = -1;
  10915. if (DebugAlwaysTrue(GotExpectedItem))
  10916. {
  10917. Index = FCalculateSizeOperation->Index;
  10918. FCalculateSizeOperation->Index++;
  10919. }
  10920. else
  10921. {
  10922. TListItemsVector::iterator I =
  10923. std::find(FCalculateSizeOperation->ListItems.begin(), FCalculateSizeOperation->ListItems.end(), Item);
  10924. if (DebugAlwaysTrue(I != FCalculateSizeOperation->ListItems.end()))
  10925. {
  10926. Index = I - FCalculateSizeOperation->ListItems.begin();
  10927. }
  10928. }
  10929. if (DebugAlwaysTrue(Index >= 0))
  10930. {
  10931. __int64 Size = (*FCalculateSizeOperation->Stats.CalculatedSizes)[Index];
  10932. ADirView->SetItemCalculatedSize(Item, Size);
  10933. }
  10934. }