CustomScpExplorer.cpp 384 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605
  1. //---------------------------------------------------------------------------
  2. #define NO_WIN32_LEAN_AND_MEAN
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include <Common.h>
  6. #include "CustomScpExplorer.h"
  7. #include <algorithm>
  8. #include <Bookmarks.h>
  9. #include <Interface.h>
  10. #include <Exceptions.h>
  11. #include <CoreMain.h>
  12. #include <FileSystems.h>
  13. #include <TextsCore.h>
  14. #include <TextsWin.h>
  15. #include <HelpWin.h>
  16. #include <VCLCommon.h>
  17. #include <Progress.h>
  18. #include <SynchronizeProgress.h>
  19. #include <DragExt.h>
  20. #include <WinApi.h>
  21. #include "GUITools.h"
  22. #include "NonVisual.h"
  23. #include "Glyphs.h"
  24. #include "Tools.h"
  25. #include "WinConfiguration.h"
  26. #include "TerminalManager.h"
  27. #include "EditorManager.h"
  28. #include "ProgParams.h"
  29. #include "Setup.h"
  30. #include <Consts.hpp>
  31. #include <DateUtils.hpp>
  32. #include <TB2Common.hpp>
  33. #include <DirectoryMonitor.hpp>
  34. #include <System.IOUtils.hpp>
  35. //---------------------------------------------------------------------------
  36. #pragma package(smart_init)
  37. #pragma link "CustomDirView"
  38. #pragma link "CustomUnixDirView"
  39. #pragma link "IEListView"
  40. #pragma link "NortonLikeListView"
  41. #pragma link "UnixDirView"
  42. #pragma link "CustomDriveView"
  43. #pragma link "UnixDriveView"
  44. #pragma link "CustomDriveView"
  45. #pragma link "TB2Dock"
  46. #pragma link "TBXStatusBars"
  47. #pragma link "TB2Item"
  48. #pragma link "TB2Toolbar"
  49. #pragma link "ThemePageControl"
  50. #pragma link "PathLabel"
  51. #pragma resource "*.dfm"
  52. //---------------------------------------------------------------------------
  53. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  54. static const int SessionPanelCount = 3;
  55. //---------------------------------------------------------------------------
  56. class TMutexGuard
  57. {
  58. public:
  59. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  60. int Timeout = 5000)
  61. {
  62. FMutex = NULL;
  63. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  64. if (WaitResult == WAIT_TIMEOUT)
  65. {
  66. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  67. }
  68. else
  69. {
  70. FMutex = AMutex;
  71. }
  72. }
  73. ~TMutexGuard()
  74. {
  75. if (FMutex != NULL)
  76. {
  77. ReleaseMutex(FMutex);
  78. }
  79. }
  80. private:
  81. HANDLE FMutex;
  82. };
  83. //---------------------------------------------------------------------------
  84. class TWindowLock
  85. {
  86. public:
  87. TWindowLock(TCustomScpExplorerForm * Form) :
  88. FForm(Form)
  89. {
  90. FForm->LockWindow();
  91. }
  92. ~TWindowLock()
  93. {
  94. FForm->UnlockWindow();
  95. }
  96. private:
  97. TCustomScpExplorerForm * FForm;
  98. };
  99. //---------------------------------------------------------------------------
  100. TAutoBatch::TAutoBatch(TCustomScpExplorerForm * Form) :
  101. FForm(Form)
  102. {
  103. FForm->BatchStart(FBatchStorage);
  104. }
  105. //---------------------------------------------------------------------------
  106. TAutoBatch::~TAutoBatch()
  107. {
  108. FForm->BatchEnd(FBatchStorage);
  109. }
  110. //---------------------------------------------------------------------------
  111. struct TTransferOperationParam
  112. {
  113. TTransferOperationParam();
  114. UnicodeString TargetDirectory;
  115. bool Temp;
  116. bool DragDrop;
  117. int Options;
  118. TAutoSwitch Queue;
  119. };
  120. //---------------------------------------------------------------------------
  121. TTransferOperationParam::TTransferOperationParam()
  122. {
  123. Temp = false;
  124. DragDrop = false;
  125. Options = 0;
  126. Queue = asAuto;
  127. }
  128. //---------------------------------------------------------------------------
  129. class TTransferPresetNoteData : public TObject
  130. {
  131. public:
  132. UnicodeString Message;
  133. };
  134. //---------------------------------------------------------------------------
  135. class TTerminalNoteData : public TObject
  136. {
  137. public:
  138. TManagedTerminal * Terminal;
  139. };
  140. //---------------------------------------------------------------------------
  141. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  142. FFormRestored(false),
  143. TForm(Owner)
  144. {
  145. AddStartupSequence(L"F");
  146. FInvalid = false;
  147. FCurrentSide = osRemote;
  148. FEverShown = false;
  149. FDocks = new TList();
  150. InitControls();
  151. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  152. // So that UpdateCustomCommandsToolbar called from RestoreParams works
  153. NonVisualDataModule->ScpExplorer = this;
  154. RestoreParams();
  155. ConfigurationChanged();
  156. RemoteDirView->Invalidate();
  157. FAutoOperation = false;
  158. FOnFileOperationFinished = NULL;
  159. FPrimaryOperation = foNone;
  160. FForceExecution = false;
  161. FIgnoreNextDialogChar = 0;
  162. FErrorList = NULL;
  163. FSynchronizeProgressForm = NULL;
  164. FProgressForm = NULL;
  165. FRefreshLocalDirectory = false;
  166. FRefreshRemoteDirectory = false;
  167. FDDExtMapFile = NULL;
  168. // CreateMutexW keeps failing with ERROR_NOACCESS
  169. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  170. DebugAssert(FDDExtMutex != NULL);
  171. FDDTargetControl = NULL;
  172. FDelayedDeletionTimer = NULL;
  173. FDelayedDeletionList = new TStringList();
  174. FDDFileList = NULL;
  175. FPendingTempSpaceWarn = false;
  176. FCapturedLog = NULL;
  177. FDragDropOperation = false;
  178. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  179. FAllowTransferPresetAutoSelect = true;
  180. FSessionChanging = false;
  181. FCopyParamDefault = L"";
  182. FSynchronizeController = NULL;
  183. FPendingQueueActionItem = NULL;
  184. FLockLevel = 0;
  185. FLockSuspendLevel = 0;
  186. FDisabledOnLockSuspend = false;
  187. FAlternativeDelete = false;
  188. FTrayIcon = new ::TTrayIcon(0);
  189. FTrayIcon->OnClick = TrayIconClick;
  190. FMaxQueueLength = 0;
  191. FLastContextPopupScreenPoint = TPoint(-1, -1);
  192. FTransferResumeList = NULL;
  193. FDeletedFiles = NULL;
  194. FMoveToQueue = false;
  195. StandaloneOperation = false;
  196. FOnFeedSynchronizeError = NULL;
  197. FOnSynchronizeAbort = NULL;
  198. FSynchronizeTerminal = NULL;
  199. FNeedSession = false;
  200. FDoNotIdleCurrentTerminal = 0;
  201. FIncrementalSearching = 0;
  202. FIncrementalSearchHaveNext = false;
  203. FQueueFileList.reset(new TQueueFileList());
  204. FProgressSide = osCurrent;
  205. FCalculateSizeOperation = NULL;
  206. FDownloadingFromClipboard = false;
  207. FClipboardFakeMonitorsPendingReset = false;
  208. FEditorManager = new TEditorManager();
  209. FEditorManager->OnFileChange = ExecutedFileChanged;
  210. FEditorManager->OnFileReload = ExecutedFileReload;
  211. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  212. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  213. FLocalEditors = new TList();
  214. FQueueStatus = NULL;
  215. FQueueStatusSection = new TCriticalSection();
  216. FQueueStatusInvalidated = false;
  217. FQueueItemInvalidated = false;
  218. FQueueStatusUpdating = false;
  219. FQueueActedItem = NULL;
  220. FQueueController = new TQueueController(QueueView3);
  221. FUserActionTimer = new TTimer(this);
  222. FUserActionTimer->Enabled = false;
  223. FUserActionTimer->Interval = 10;
  224. FUserActionTimer->OnTimer = UserActionTimer;
  225. FNotes = new TStringList();
  226. FNoteTimer = new TTimer(this);
  227. FNoteTimer->Enabled = false;
  228. FNoteTimer->OnTimer = NoteTimer;
  229. FOnNoteClick = NULL;
  230. FOle32Library = LoadLibrary(L"Ole32.dll");
  231. FDragMoveCursor = FOle32Library != NULL ?
  232. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  233. UseSystemSettings(this);
  234. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  235. static_cast<TObject*>(GetComponent(fcTransferList)));
  236. DebugAssert(TransferList != NULL);
  237. FTransferListHoverIndex = -1;
  238. TransferList->OnChange = TransferListChange;
  239. TransferList->OnDrawItem = TransferListDrawItem;
  240. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))), true);
  241. SetSubmenu(NonVisualDataModule->ColorMenuItem, true);
  242. UseDesktopFont(SessionsPageControl);
  243. UpdateSessionsPageControlHeight();
  244. UseDesktopFont(RemoteDirView);
  245. UseDesktopFont(RemoteDriveView);
  246. UseDesktopFont(QueueView3);
  247. UseDesktopFont(QueueFileList);
  248. UseDesktopFont(QueueLabel);
  249. UseDesktopFont(RemoteStatusBar);
  250. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  251. QueueSplitter->ShowHint = true;
  252. reinterpret_cast<TLabel*>(QueueFileListSplitter)->OnDblClick = QueueFileListSplitterDblClick;
  253. QueueFileListSplitter->ShowHint = true;
  254. RemotePanelSplitter->ShowHint = true;
  255. UpdateImages();
  256. FCustomCommandMenu = CreateTBXPopupMenu(this);
  257. FCustomCommandLocalFileList = NULL;
  258. FCustomCommandRemoteFileList = NULL;
  259. FSessionColors = new TPngImageList(this);
  260. FSessionColors->ColorDepth = cd32Bit;
  261. AddFixedSessionImages();
  262. SessionsPageControl->Images = FSessionColors;
  263. UpdateQueueLabel();
  264. CreateHiddenWindow();
  265. StartUpdates();
  266. }
  267. //---------------------------------------------------------------------------
  268. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  269. {
  270. FInvalid = true;
  271. // this has to be one of the very first things to do
  272. StopUpdateThread();
  273. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  274. TTerminalManager::Instance()->ScpExplorer = NULL;
  275. delete FCustomCommandLocalFileList;
  276. delete FCustomCommandRemoteFileList;
  277. delete FCustomCommandMenu;
  278. delete FTrayIcon;
  279. FTrayIcon = NULL;
  280. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  281. delete FEditorManager;
  282. ForceCloseLocalEditors();
  283. delete FLocalEditors;
  284. HideFileFindDialog();
  285. if (FDelayedDeletionTimer)
  286. {
  287. DoDelayedDeletion(NULL);
  288. SAFE_DESTROY(FDelayedDeletionTimer);
  289. }
  290. SAFE_DESTROY(FDelayedDeletionList);
  291. // sometimes we do not get DDEnd so the list is not released
  292. SAFE_DESTROY(FDDFileList);
  293. DebugAssert(FSynchronizeController == NULL);
  294. CloseHandle(FDDExtMutex);
  295. FDDExtMutex = NULL;
  296. FreeLibrary(FOle32Library);
  297. FOle32Library = NULL;
  298. FDragMoveCursor = NULL;
  299. DebugAssert(!FErrorList);
  300. if (FEverShown)
  301. {
  302. // when window is never shown (like when running command-line operation),
  303. // particularly window site is not restored correctly (BoundsRect value set
  304. // in RestoreForm gets lost during handle allocation), so we do not want
  305. // it to be stored
  306. StoreParams();
  307. }
  308. ManagedSession = NULL;
  309. Queue = NULL;
  310. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  311. NonVisualDataModule->ScpExplorer = NULL;
  312. delete FQueueController;
  313. FQueueController = NULL;
  314. delete FQueueStatusSection;
  315. FQueueStatusSection = NULL;
  316. delete FQueueStatus;
  317. FQueueStatus = NULL;
  318. delete FUserActionTimer;
  319. FUserActionTimer = NULL;
  320. delete FNoteTimer;
  321. delete FNotes;
  322. delete FNoteData;
  323. SAFE_DESTROY(FDocks);
  324. SessionsPageControl->Images = NULL;
  325. // only after clearing Terminal (after SessionListChanged => ... => AddSessionColor is called)
  326. SAFE_DESTROY(FSessionColors);
  327. SAFE_DESTROY(FSessionsDragDropFilesEx);
  328. SAFE_DESTROY(FHistoryMenu[0][0]);
  329. SAFE_DESTROY(FHistoryMenu[0][1]);
  330. SAFE_DESTROY(FHistoryMenu[1][0]);
  331. SAFE_DESTROY(FHistoryMenu[1][1]);
  332. if (FHiddenWindow != NULL)
  333. {
  334. DestroyWindow(FHiddenWindow);
  335. FHiddenWindow = NULL;
  336. }
  337. }
  338. //---------------------------------------------------------------------------
  339. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  340. {
  341. std::unique_ptr<TSessionData> Data;
  342. if (!Session.IsEmpty())
  343. {
  344. bool DefaultsOnly;
  345. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  346. }
  347. TTerminalManager * Manager = TTerminalManager::Instance();
  348. for (int Index = 0; Index < Manager->Count; Index++)
  349. {
  350. TTerminal * Terminal = Manager->Sessions[Index];
  351. if (Session.IsEmpty() ||
  352. Manager->IsActiveTerminalForSite(Terminal, Data.get()))
  353. {
  354. if (Path.IsEmpty())
  355. {
  356. Terminal->ClearCaches();
  357. }
  358. else
  359. {
  360. Terminal->DirectoryModified(Path, true);
  361. }
  362. }
  363. }
  364. // We should flag a pending refresh for the background terminals or busy foreground terminals
  365. if (HasActiveTerminal() &&
  366. CanCommandLineFromAnotherInstance() &&
  367. (Session.IsEmpty() || Manager->IsActiveTerminalForSite(Terminal, Data.get())) &&
  368. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  369. {
  370. Terminal->ReloadDirectory();
  371. }
  372. }
  373. //---------------------------------------------------------------------------
  374. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  375. {
  376. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  377. size_t MessageSize = sizeof(TCopyDataMessage);
  378. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  379. if (Result)
  380. {
  381. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  382. Result = (Message.Version == TCopyDataMessage::Version1);
  383. if (Result)
  384. {
  385. switch (Message.Command)
  386. {
  387. case TCopyDataMessage::CommandCanCommandLine:
  388. Result = CanCommandLineFromAnotherInstance();
  389. break;
  390. case TCopyDataMessage::CommandCommandLine:
  391. {
  392. UnicodeString CommandLine(Message.CommandLine);
  393. Result = CommandLineFromAnotherInstance(CommandLine);
  394. }
  395. break;
  396. case TCopyDataMessage::RefreshPanel:
  397. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  398. break;
  399. case TCopyDataMessage::MainWindowCheck:
  400. Result = true;
  401. break;
  402. default:
  403. Result = false;
  404. break;
  405. }
  406. }
  407. }
  408. Message.Result = Result ? 1 : 0;
  409. }
  410. //---------------------------------------------------------------------------
  411. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  412. {
  413. WNDCLASS WindowClass = {0};
  414. WindowClass.lpfnWndProc = DefWindowProc;
  415. WindowClass.hInstance = HInstance;
  416. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  417. FHiddenWindow = NULL;
  418. if (RegisterClass(&WindowClass))
  419. {
  420. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  421. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  422. }
  423. }
  424. //---------------------------------------------------------------------------
  425. bool __fastcall TCustomScpExplorerForm::CanConsole()
  426. {
  427. return
  428. HasActiveTerminal() &&
  429. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  430. }
  431. //---------------------------------------------------------------------------
  432. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  433. {
  434. bool Result = Visible && !NonVisualDataModule->Busy;
  435. return Result;
  436. }
  437. //---------------------------------------------------------------------------
  438. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  439. const UnicodeString & CommandLine)
  440. {
  441. TProgramParams Params(CommandLine);
  442. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  443. if (Result)
  444. {
  445. NonVisualDataModule->StartBusy();
  446. try
  447. {
  448. // this action is initiated from another process,
  449. // so it's likely that our window is not visible,
  450. // and user won't see what is going on
  451. Application->BringToFront();
  452. // reload sessions as we may be asked to open a session
  453. // just stored by another instance
  454. StoredSessions->Reload();
  455. UnicodeString SessionName = Params.Param[1];
  456. std::unique_ptr<TObjectList> DataList(new TObjectList());
  457. try
  458. {
  459. UnicodeString DownloadFile; // unused
  460. int Flags = GetCommandLineParseUrlFlags(&Params);
  461. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this, Flags);
  462. }
  463. catch (EAbort &)
  464. {
  465. // Happens when opening session in PuTTY and in other situations,
  466. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  467. // to process the command-line on its own (or to pass it to yet another instance)
  468. }
  469. if (DataList->Count > 0)
  470. {
  471. TTerminalManager * Manager = TTerminalManager::Instance();
  472. Manager->ActiveSession = Manager->NewSessions(DataList.get());
  473. }
  474. }
  475. __finally
  476. {
  477. NonVisualDataModule->EndBusy();
  478. }
  479. }
  480. return Result;
  481. }
  482. //---------------------------------------------------------------------------
  483. TManagedTerminal * TCustomScpExplorerForm::GetTerminal()
  484. {
  485. TManagedTerminal * Result;
  486. if ((FManagedSession != NULL) && !FManagedSession->LocalBrowser)
  487. {
  488. Result = FManagedSession;
  489. }
  490. else
  491. {
  492. Result = NULL;
  493. }
  494. return Result;
  495. }
  496. //---------------------------------------------------------------------------
  497. void __fastcall TCustomScpExplorerForm::SetManagedSession(TManagedTerminal * value)
  498. {
  499. if (FManagedSession != value)
  500. {
  501. SessionChanging();
  502. DoSetManagedSession(value, false);
  503. }
  504. }
  505. //---------------------------------------------------------------------------
  506. void __fastcall TCustomScpExplorerForm::DoSetManagedSession(TManagedTerminal * value, bool Replace)
  507. {
  508. DebugAssert(!Replace || ((value != NULL) && !value->LocalBrowser));
  509. FManagedSession = value;
  510. {
  511. TValueRestorer<bool> AllowTransferPresetAutoSelectRestorer(FAllowTransferPresetAutoSelect);
  512. FAllowTransferPresetAutoSelect = false;
  513. TAutoFlag SessionChangingFlag(FSessionChanging);
  514. SessionChanged(Replace);
  515. }
  516. if (Terminal != NULL)
  517. {
  518. TransferPresetAutoSelect();
  519. }
  520. // Update app and tab titles, prevented above by FSessionChanging.
  521. UpdateControls();
  522. }
  523. //---------------------------------------------------------------------------
  524. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  525. {
  526. DoSetManagedSession(value, true);
  527. }
  528. //---------------------------------------------------------------------------
  529. void __fastcall TCustomScpExplorerForm::SessionChanging()
  530. {
  531. if (ManagedSession != NULL)
  532. {
  533. UpdateSession(ManagedSession);
  534. }
  535. }
  536. //---------------------------------------------------------------------------
  537. void __fastcall TCustomScpExplorerForm::SessionChanged(bool Replaced)
  538. {
  539. if (Terminal != NULL)
  540. {
  541. UpdateSessionColor((TColor)Terminal->StateData->Color);
  542. }
  543. if (!TTerminalManager::Instance()->Updating)
  544. {
  545. SessionListChanged();
  546. }
  547. if (Terminal != NULL)
  548. {
  549. TObject * State = NULL;
  550. if (WinConfiguration->PreservePanelState)
  551. {
  552. State = Terminal->RemoteExplorerState;
  553. DirView(osRemote)->AnnounceState(State);
  554. }
  555. try
  556. {
  557. if (Replaced)
  558. {
  559. RemoteDirView->ReplaceTerminal(Terminal);
  560. }
  561. else
  562. {
  563. RemoteDirView->Terminal = Terminal;
  564. }
  565. if (Terminal->Active)
  566. {
  567. Terminal->RefreshDirectory();
  568. }
  569. if (State != NULL)
  570. {
  571. DirView(osRemote)->RestoreState(State);
  572. }
  573. }
  574. __finally
  575. {
  576. DirView(osRemote)->AnnounceState(NULL);
  577. }
  578. }
  579. else
  580. {
  581. RemoteDirView->Terminal = NULL;
  582. }
  583. InitStatusBar();
  584. UpdateTransferList();
  585. // Update panels Enable state before refreshing the labels
  586. UpdateControls();
  587. }
  588. //---------------------------------------------------------------------------
  589. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  590. {
  591. if (Queue != value)
  592. {
  593. if (FQueue != NULL)
  594. {
  595. FQueue->OnListUpdate = NULL;
  596. FQueue->OnQueueItemUpdate = NULL;
  597. }
  598. FQueue = value;
  599. if (FQueue != NULL)
  600. {
  601. DebugAssert(FQueue->OnListUpdate == NULL);
  602. FQueue->OnListUpdate = QueueListUpdate;
  603. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  604. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  605. }
  606. QueueChanged();
  607. }
  608. }
  609. //---------------------------------------------------------------------------
  610. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  611. TListItem * Item)
  612. {
  613. if (FQueueActedItem == Item)
  614. {
  615. FQueueActedItem = NULL;
  616. if ((QueueView3->PopupMenu != NULL) &&
  617. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  618. {
  619. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  620. ReleaseCapture();
  621. }
  622. }
  623. if (Item->Data == FPendingQueueActionItem)
  624. {
  625. FPendingQueueActionItem = NULL;
  626. }
  627. }
  628. //---------------------------------------------------------------------------
  629. bool TCustomScpExplorerForm::IsAnythingQueued()
  630. {
  631. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  632. }
  633. //---------------------------------------------------------------------------
  634. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  635. {
  636. {
  637. TGuard Guard(FQueueStatusSection);
  638. FQueueStatusInvalidated = false;
  639. if (FQueue != NULL)
  640. {
  641. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  642. }
  643. }
  644. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  645. {
  646. FMaxQueueLength = FQueueStatus->Count;
  647. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  648. }
  649. {
  650. TAutoFlag Flag(FQueueStatusUpdating);
  651. FQueueController->UpdateQueueStatus(FQueueStatus);
  652. }
  653. UpdateQueueFileList();
  654. SetQueueProgress();
  655. UpdateQueueView();
  656. bool IsEmpty = !IsAnythingQueued();
  657. if (IsEmpty && (Terminal != NULL))
  658. {
  659. TOnceDoneOperation OnceDoneOperation =
  660. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  661. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  662. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  663. {
  664. FQueue->Enabled = false;
  665. }
  666. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  667. {
  668. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  669. }
  670. }
  671. }
  672. //---------------------------------------------------------------------------
  673. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  674. {
  675. UnicodeString Result;
  676. if (FQueueStatus != NULL)
  677. {
  678. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  679. {
  680. TFileOperationProgressType * ProgressData =
  681. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  682. if ((ProgressData != NULL) && ProgressData->InProgress)
  683. {
  684. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  685. }
  686. }
  687. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  688. {
  689. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  690. }
  691. }
  692. return Result;
  693. }
  694. //---------------------------------------------------------------------------
  695. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  696. {
  697. ComponentVisible[fcQueueView] =
  698. !IsLocalBrowserMode() &&
  699. ((WinConfiguration->QueueView.Show == qvShow) ||
  700. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  701. (FQueueStatus != NULL) && (FQueueStatus->Count > 0)));
  702. }
  703. //---------------------------------------------------------------------------
  704. void __fastcall TCustomScpExplorerForm::QueueChanged()
  705. {
  706. if (FQueueStatus != NULL)
  707. {
  708. // UpdateFileList implementation relies on the status being recreated when session changes
  709. delete FQueueStatus;
  710. FQueueStatus = NULL;
  711. }
  712. UpdateQueueStatus(true);
  713. }
  714. //---------------------------------------------------------------------------
  715. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  716. {
  717. if (FQueue == Queue)
  718. {
  719. FQueueStatusInvalidated = true;
  720. }
  721. }
  722. //---------------------------------------------------------------------------
  723. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  724. TQueueItem * Item)
  725. {
  726. if (FQueue == Queue)
  727. {
  728. TGuard Guard(FQueueStatusSection);
  729. // this may be running in parallel with QueueChanged
  730. if (FQueueStatus != NULL)
  731. {
  732. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  733. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  734. {
  735. FRefreshLocalDirectory = (QueueItem == NULL) ||
  736. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  737. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  738. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  739. }
  740. if (QueueItem != NULL)
  741. {
  742. QueueItem->UserData = (void*)true;
  743. FQueueItemInvalidated = true;
  744. }
  745. }
  746. }
  747. }
  748. //---------------------------------------------------------------------------
  749. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  750. {
  751. // during standalone editing, we have no way to see/control queue,
  752. // so we have to always popup prompts automatically
  753. return StandaloneOperation || GUIConfiguration->QueueAutoPopup;
  754. }
  755. //---------------------------------------------------------------------------
  756. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  757. {
  758. if (FQueueStatus != NULL)
  759. {
  760. bool QueueAutoPopup = IsQueueAutoPopup();
  761. bool NeedRefresh = FQueueController->NeedRefresh();
  762. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  763. FQueueItemInvalidated = false;
  764. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  765. bool Updated = false;
  766. TQueueItemProxy * QueueItem;
  767. bool WasUserAction;
  768. for (int Index = 0; Index < Limit; Index++)
  769. {
  770. bool Update = false;
  771. QueueItem = FQueueStatus->Items[Index];
  772. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  773. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  774. (FPendingQueueActionItem == NULL))
  775. {
  776. FPendingQueueActionItem = QueueItem;
  777. FUserActionTimer->Enabled = true;
  778. }
  779. if ((bool)QueueItem->UserData)
  780. {
  781. QueueItem->UserData = (void*)false;
  782. QueueItem->Update();
  783. Updated = true;
  784. Update = true;
  785. }
  786. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  787. {
  788. Update = true;
  789. }
  790. if (Update)
  791. {
  792. FQueueController->RefreshQueueItem(QueueItem);
  793. }
  794. }
  795. if (Updated)
  796. {
  797. NonVisualDataModule->UpdateNonVisibleActions();
  798. SetQueueProgress();
  799. UpdateQueueFileList();
  800. }
  801. }
  802. }
  803. //---------------------------------------------------------------------------
  804. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  805. {
  806. if (FTaskbarList != NULL)
  807. {
  808. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  809. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  810. }
  811. }
  812. //---------------------------------------------------------------------------
  813. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  814. {
  815. if (Progress >= 0)
  816. {
  817. if (FTaskbarList != NULL)
  818. {
  819. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  820. }
  821. }
  822. else
  823. {
  824. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  825. }
  826. }
  827. //---------------------------------------------------------------------------
  828. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  829. {
  830. int OverallProgress;
  831. // FProgressForm is null when this is called from SetQueueProgress
  832. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  833. {
  834. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  835. }
  836. else if (!ProgressData->IsIndeterminate())
  837. {
  838. OverallProgress = ProgressData->OverallProgress();
  839. }
  840. else
  841. {
  842. OverallProgress = -1;
  843. }
  844. SetTaskbarListProgressValue(OverallProgress);
  845. }
  846. //---------------------------------------------------------------------------
  847. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  848. {
  849. TTerminalManager::Instance()->QueueStatusUpdated();
  850. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  851. {
  852. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  853. {
  854. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  855. {
  856. TFileOperationProgressType * ProgressData = NULL;
  857. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  858. {
  859. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  860. }
  861. if ((ProgressData != NULL) &&
  862. ProgressData->InProgress)
  863. {
  864. SetTaskbarListProgressValue(ProgressData);
  865. }
  866. else
  867. {
  868. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  869. }
  870. }
  871. else
  872. {
  873. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  874. }
  875. }
  876. else
  877. {
  878. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  879. }
  880. }
  881. UpdateQueueLabel();
  882. }
  883. //---------------------------------------------------------------------------
  884. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  885. {
  886. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  887. if (FQueueStatus != NULL)
  888. {
  889. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  890. if (ActiveAndPendingPrimaryCount > 0)
  891. {
  892. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  893. }
  894. }
  895. QueueLabel->Caption = Caption;
  896. }
  897. //---------------------------------------------------------------------------
  898. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  899. {
  900. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  901. static_cast<TComponent*>(GetComponent(fcTransferList)));
  902. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  903. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  904. TransferList->Strings->BeginUpdate();
  905. try
  906. {
  907. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  908. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  909. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  910. if (FTransferDropDownHint.IsEmpty())
  911. {
  912. FTransferDropDownHint = TransferDropDown->Hint;
  913. }
  914. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  915. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  916. TransferDropDown->Text = StripHotkey(Name);
  917. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  918. (FTransferDropDownHint, StripHotkey(Name),
  919. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  920. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General))));
  921. // update the label, otherwise when it is updated only on the first draw
  922. // of the list, it is drawn "bold" for some reason
  923. FTransferListHoverIndex = TransferList->ItemIndex;
  924. UpdateTransferLabel();
  925. }
  926. __finally
  927. {
  928. TransferList->Strings->EndUpdate();
  929. }
  930. }
  931. //---------------------------------------------------------------------------
  932. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  933. {
  934. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  935. DebugAssert(Toolbar != NULL);
  936. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  937. }
  938. //---------------------------------------------------------------------------
  939. void __fastcall TCustomScpExplorerForm::UpdateActions()
  940. {
  941. TForm::UpdateActions();
  942. if (ComponentVisible[fcCustomCommandsBand])
  943. {
  944. UpdateCustomCommandsToolbar();
  945. }
  946. }
  947. //---------------------------------------------------------------------------
  948. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  949. {
  950. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  951. }
  952. //---------------------------------------------------------------------------
  953. void __fastcall TCustomScpExplorerForm::UpdateRowSelect(TCustomDirView * DirView)
  954. {
  955. if (DirView->RowSelect != WinConfiguration->FullRowSelect)
  956. {
  957. DirView->RowSelect = WinConfiguration->FullRowSelect;
  958. // Without this, the panel turns gray and unused part of header turns black
  959. DirView->Perform(CM_COLORCHANGED, 0, 0);
  960. }
  961. }
  962. //---------------------------------------------------------------------------
  963. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  964. {
  965. Color = GetBtnFaceColor();
  966. DebugAssert(Configuration && RemoteDirView);
  967. RemoteDirView->DDAllowMove = !WinConfiguration->DDDisableMove;
  968. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  969. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  970. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  971. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  972. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  973. UpdateRowSelect(RemoteDirView);
  974. RemoteDriveView->DDAllowMove = !WinConfiguration->DDDisableMove;
  975. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  976. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  977. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  978. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  979. UpdateSessionsPageControlHeight();
  980. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  981. UpdateToolbarDisplayMode();
  982. UpdateTransferList();
  983. if (Terminal != NULL)
  984. {
  985. TransferPresetAutoSelect();
  986. }
  987. UpdateQueueView();
  988. UpdateControls();
  989. // this can be called even before constuctor finishes.
  990. if (FEditorManager != NULL)
  991. {
  992. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  993. }
  994. // this can be called even before constuctor finishes.
  995. if (FLocalEditors != NULL)
  996. {
  997. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  998. {
  999. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  1000. }
  1001. }
  1002. if (ComponentVisible[fcCustomCommandsBand])
  1003. {
  1004. UpdateCustomCommandsToolbar();
  1005. }
  1006. // show only when keeping queue items forever,
  1007. // otherwise, it's enough to have it in the context menu
  1008. QueueDeleteAllDoneQueueToolbarItem->Visible =
  1009. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  1010. if (FFileColorsCurrent != WinConfiguration->FileColors)
  1011. {
  1012. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  1013. FFileColorsCurrent = WinConfiguration->FileColors;
  1014. FileColorsChanged();
  1015. }
  1016. }
  1017. //---------------------------------------------------------------------------
  1018. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  1019. {
  1020. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  1021. DirView->Invalidate();
  1022. }
  1023. //---------------------------------------------------------------------------
  1024. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  1025. {
  1026. DoFileColorsChanged(RemoteDirView);
  1027. }
  1028. //---------------------------------------------------------------------------
  1029. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  1030. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  1031. void * /*Arg*/)
  1032. {
  1033. if (Token != NULL)
  1034. {
  1035. TForm * Editor = dynamic_cast<TForm*>(Token);
  1036. DebugAssert(Editor != NULL);
  1037. ReconfigureEditorForm(Editor);
  1038. }
  1039. }
  1040. //---------------------------------------------------------------------------
  1041. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  1042. {
  1043. WinConfiguration->DDTransferConfirmation = asOn;
  1044. }
  1045. //---------------------------------------------------------------------------
  1046. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1047. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1048. {
  1049. // noop
  1050. }
  1051. //---------------------------------------------------------------------------
  1052. bool TCustomScpExplorerForm::GetDoNotShowCopyDialogDefault(bool DragDrop)
  1053. {
  1054. return DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto);
  1055. }
  1056. //---------------------------------------------------------------------------
  1057. void TCustomScpExplorerForm::HandleDoNotShowCopyDialogAgain(bool DragDrop, bool DoNotShowAgain)
  1058. {
  1059. if (DoNotShowAgain)
  1060. {
  1061. if (DragDrop)
  1062. {
  1063. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1064. {
  1065. PopupTrayBalloon(
  1066. NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation, NULL, 0, EnableDDTransferConfirmation, NULL);
  1067. }
  1068. WinConfiguration->DDTransferConfirmation = asOff;
  1069. }
  1070. else
  1071. {
  1072. WinConfiguration->ConfirmTransferring = false;
  1073. }
  1074. }
  1075. else
  1076. {
  1077. // User explicitly unchecked "do not show again",
  1078. // so show him the dialog the next time
  1079. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1080. {
  1081. WinConfiguration->DDTransferConfirmation = asOn;
  1082. }
  1083. }
  1084. }
  1085. //---------------------------------------------------------------------------
  1086. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1087. TTransferDirection Direction, TTransferType Type, bool Temp,
  1088. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1089. bool Confirm, bool DragDrop, int Options)
  1090. {
  1091. bool Result = true;
  1092. DebugAssert(HasActiveTerminal());
  1093. // these parameters are known in advance
  1094. int Params =
  1095. FLAGMASK(Type == ttMove, cpDelete);
  1096. bool ToTemp = (Temp && (Direction == tdToLocal));
  1097. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1098. {
  1099. bool DisableNewerOnly =
  1100. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1101. ToTemp;
  1102. Options |=
  1103. FLAGMASK(ToTemp, coTemp) |
  1104. FLAGMASK(!Terminal->IsCapable[fcBackgroundTransfers], coDisableQueue) |
  1105. coDoNotShowAgain;
  1106. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1107. int CopyParamAttrs = (Direction == tdToRemote ?
  1108. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1109. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1110. int OutputOptions = FLAGMASK(GetDoNotShowCopyDialogDefault(DragDrop), cooDoNotShowAgain);
  1111. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1112. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1113. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1114. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1115. if (Result)
  1116. {
  1117. HandleDoNotShowCopyDialogAgain(DragDrop, FLAGSET(OutputOptions, cooDoNotShowAgain));
  1118. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1119. }
  1120. }
  1121. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fcBackgroundTransfers])
  1122. {
  1123. Configuration->Usage->Inc(L"TransfersOnBackground");
  1124. // these parameter are known only after transfer dialog
  1125. Params |=
  1126. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1127. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1128. Result = false;
  1129. ClearTransferSourceSelection(Direction);
  1130. }
  1131. return Result;
  1132. }
  1133. //---------------------------------------------------------------------------
  1134. void TCustomScpExplorerForm::ClearOperationSelection(TOperationSide Side)
  1135. {
  1136. if (FOnFileOperationFinished != NULL)
  1137. {
  1138. FOnFileOperationFinished(Side, UnicodeString(), true);
  1139. }
  1140. else
  1141. {
  1142. if (HasDirView[Side])
  1143. {
  1144. DirView(Side)->SelectAll(smNone);
  1145. }
  1146. }
  1147. }
  1148. //---------------------------------------------------------------------------
  1149. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1150. {
  1151. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1152. ClearOperationSelection(Side);
  1153. }
  1154. //---------------------------------------------------------------------------
  1155. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1156. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1157. const UnicodeString TargetDirectory, TGUICopyParamType & CopyParam,
  1158. int Params)
  1159. {
  1160. DebugAssert(Queue != NULL);
  1161. bool SingleFile = false;
  1162. if (FileList->Count == 1)
  1163. {
  1164. if (Direction == tdToRemote)
  1165. {
  1166. UnicodeString FileName = FileList->Strings[0];
  1167. SingleFile = FileExists(ApiPath(FileName));
  1168. }
  1169. else
  1170. {
  1171. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1172. SingleFile = !File->IsDirectory;
  1173. }
  1174. }
  1175. TQueueItem * QueueItem;
  1176. if (Direction == tdToRemote)
  1177. {
  1178. CopyParam.IncludeFileMask.SetRoots(FileList, TargetDirectory);
  1179. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1180. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1181. }
  1182. else
  1183. {
  1184. CopyParam.IncludeFileMask.SetRoots(TargetDirectory, FileList);
  1185. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1186. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1187. }
  1188. AddQueueItem(Queue, QueueItem, Terminal);
  1189. }
  1190. //---------------------------------------------------------------------------
  1191. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1192. {
  1193. if (Queue->IsEmpty)
  1194. {
  1195. Terminal->QueueOperationStart = Now();
  1196. }
  1197. Queue->AddItem(QueueItem);
  1198. }
  1199. //---------------------------------------------------------------------------
  1200. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1201. {
  1202. }
  1203. //---------------------------------------------------------------------------
  1204. void __fastcall TCustomScpExplorerForm::InitControls()
  1205. {
  1206. DebugAssert(FDocks != NULL);
  1207. for (int Index = 0; Index < ComponentCount; Index++)
  1208. {
  1209. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1210. if ((Dock != NULL) && (Dock->Tag == 0))
  1211. {
  1212. FDocks->Add(Dock);
  1213. }
  1214. }
  1215. CollectItemsWithTextDisplayMode(this);
  1216. }
  1217. //---------------------------------------------------------------------------
  1218. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1219. {
  1220. ConfigurationChanged();
  1221. QueuePanel->Height =
  1222. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1223. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1224. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1225. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1226. QueueFileList->Visible = WinConfiguration->QueueView.FileList;
  1227. QueueFileList->Height =
  1228. LoadDimension(WinConfiguration->QueueView.FileListHeight, WinConfiguration->QueueView.FileListHeightPixelsPerInch, this);
  1229. if (QueueFileList->Visible)
  1230. {
  1231. AdjustQueueLayout();
  1232. }
  1233. UpdateCustomCommandsToolbar();
  1234. }
  1235. //---------------------------------------------------------------------------
  1236. void __fastcall TCustomScpExplorerForm::StoreParams()
  1237. {
  1238. int APixelsPerInch = GetControlPixelsPerInch(this);
  1239. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1240. WinConfiguration->QueueView.HeightPixelsPerInch = APixelsPerInch;
  1241. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1242. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1243. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1244. WinConfiguration->QueueView.FileList = QueueFileList->Visible;
  1245. WinConfiguration->QueueView.FileListHeight = QueueFileList->Height;
  1246. WinConfiguration->QueueView.FileListHeightPixelsPerInch = APixelsPerInch;
  1247. }
  1248. //---------------------------------------------------------------------------
  1249. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1250. {
  1251. if (!FFormRestored)
  1252. {
  1253. FFormRestored = true;
  1254. RestoreFormParams();
  1255. }
  1256. TForm::CreateParams(Params);
  1257. }
  1258. //---------------------------------------------------------------------------
  1259. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1260. {
  1261. DebugAssert(FDocks != NULL);
  1262. for (int Index = 0; Index < FDocks->Count; Index++)
  1263. {
  1264. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1265. }
  1266. }
  1267. //---------------------------------------------------------------------------
  1268. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1269. {
  1270. SetDockAllowDrag(true);
  1271. while (!ButtonsStr.IsEmpty())
  1272. {
  1273. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1274. UnicodeString Name = CutToChar(S, L'=', true);
  1275. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1276. {
  1277. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1278. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1279. {
  1280. while (!S.IsEmpty())
  1281. {
  1282. UnicodeString S2 = CutToChar(S, L',', true);
  1283. UnicodeString Name = CutToChar(S2, L':', true);
  1284. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1285. {
  1286. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1287. if (SameText(GetToolbarItemName(Item), Name))
  1288. {
  1289. // forward compatibility
  1290. UnicodeString S3 = CutToChar(S2, L':', true);
  1291. bool Visible = StrToIntDef(S3, 0);
  1292. Item->Visible = Visible;
  1293. }
  1294. }
  1295. }
  1296. }
  1297. }
  1298. }
  1299. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1300. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1301. }
  1302. //---------------------------------------------------------------------------
  1303. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1304. {
  1305. UnicodeString Result;
  1306. SetDockAllowDrag(true);
  1307. Result = ::GetToolbarsLayoutStr(this);
  1308. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1309. return Result;
  1310. }
  1311. //---------------------------------------------------------------------------
  1312. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1313. {
  1314. UnicodeString Result;
  1315. if (Item->Action != NULL)
  1316. {
  1317. Result = Item->Action->Name;
  1318. Result = RemoveSuffix(Result, L"Action", true);
  1319. }
  1320. else
  1321. {
  1322. Result = Item->Name;
  1323. Result = RemoveSuffix(Result, L"SubmenuItem", true);
  1324. Result = RemoveSuffix(Result, L"Item", true);
  1325. }
  1326. return Result;
  1327. }
  1328. //---------------------------------------------------------------------------
  1329. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1330. {
  1331. UnicodeString Result;
  1332. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1333. {
  1334. UnicodeString ToolbarStr;
  1335. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1336. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1337. {
  1338. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1339. {
  1340. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1341. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1342. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1343. // and save the changes here only.
  1344. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1345. {
  1346. UnicodeString Name = GetToolbarItemName(Item);
  1347. DebugAssert(Name.Pos(L"TBX") == 0);
  1348. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1349. }
  1350. }
  1351. }
  1352. if (!ToolbarStr.IsEmpty())
  1353. {
  1354. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1355. }
  1356. }
  1357. return Result;
  1358. }
  1359. //---------------------------------------------------------------------------
  1360. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1361. {
  1362. DebugAssert(FProgressForm == NULL);
  1363. bool AllowSkip = HasActiveTerminal() ? Terminal->IsCapable[fcSkipTransfer] : false;
  1364. bool AllowMoveToQueue = (FTransferResumeList != NULL) || (FDeletedFiles != NULL);
  1365. FProgressForm = new TProgressForm(Application, AllowMoveToQueue, AllowSkip, SynchronizeProgress);
  1366. FProgressForm->DeleteLocalToRecycleBin =
  1367. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1368. FProgressForm->DeleteRemoteToRecycleBin =
  1369. HasActiveTerminal() &&
  1370. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1371. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1372. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1373. LockWindow();
  1374. // Actually, do not know what hides the progress once the operation finishes
  1375. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1376. SetTaskbarListProgressState(TBPF_NORMAL);
  1377. }
  1378. //---------------------------------------------------------------------------
  1379. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1380. {
  1381. UnlockWindow();
  1382. SAFE_DESTROY(FProgressForm);
  1383. SetQueueProgress();
  1384. }
  1385. //---------------------------------------------------------------------------
  1386. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1387. TFileOperationProgressType & ProgressData)
  1388. {
  1389. // Download to temporary local directory
  1390. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1391. {
  1392. bool Continue = true;
  1393. FPendingTempSpaceWarn = false;
  1394. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1395. if (!Continue)
  1396. {
  1397. Abort();
  1398. }
  1399. }
  1400. // operation is being executed and we still didn't show up progress form
  1401. if (ProgressData.InProgress && !FProgressForm)
  1402. {
  1403. CreateProgressForm(NULL);
  1404. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1405. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1406. }
  1407. // operation is finished (or terminated), so we hide progress form
  1408. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1409. {
  1410. DestroyProgressForm();
  1411. if (ProgressData.Operation == foCalculateSize)
  1412. {
  1413. // When calculating size before transfer, the abort caused by
  1414. // cancel flag set due to "MoveToQueue" below,
  1415. // is not propagated back to us in ExecuteFileOperation,
  1416. // as it is silently swallowed.
  1417. // So we explicitly (re)throw it here.
  1418. if (FMoveToQueue)
  1419. {
  1420. Abort();
  1421. }
  1422. }
  1423. else
  1424. {
  1425. if ((ProgressData.Cancel == csContinue) &&
  1426. !FAutoOperation)
  1427. {
  1428. OperationComplete(ProgressData.StartTime);
  1429. }
  1430. }
  1431. }
  1432. if (FProgressForm &&
  1433. // Can happen only during synchronization, when ending one of the operations
  1434. ProgressData.InProgress)
  1435. {
  1436. FProgressForm->SetProgressData(ProgressData);
  1437. DebugAssert(ProgressData.InProgress);
  1438. SetTaskbarListProgressValue(&ProgressData);
  1439. if (FProgressForm->Cancel > csContinue)
  1440. {
  1441. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1442. if (FProgressForm->Cancel == csCancelFile)
  1443. {
  1444. FProgressForm->ClearCancel();
  1445. }
  1446. // cancel cancels even the move
  1447. FMoveToQueue = false;
  1448. }
  1449. else if (FProgressForm->MoveToQueue)
  1450. {
  1451. FMoveToQueue = true;
  1452. ProgressData.SetCancelAtLeast(csCancel);
  1453. }
  1454. if ((FTransferResumeList != NULL) &&
  1455. ProgressData.InProgress &&
  1456. ProgressData.IsTransfer() &&
  1457. !ProgressData.FullFileName.IsEmpty())
  1458. {
  1459. if ((FTransferResumeList->Count == 0) ||
  1460. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1461. {
  1462. // note that we do not recognize directories from files here
  1463. FTransferResumeList->Add(ProgressData.FullFileName);
  1464. }
  1465. }
  1466. }
  1467. }
  1468. //---------------------------------------------------------------------------
  1469. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1470. const TDateTime & StartTime)
  1471. {
  1472. if (GUIConfiguration->BeepOnFinish &&
  1473. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1474. {
  1475. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1476. DWORD Sound;
  1477. if (!ExtractFileExt(BeepSound).IsEmpty())
  1478. {
  1479. Sound = SND_FILENAME;
  1480. }
  1481. else
  1482. {
  1483. Sound = SND_ALIAS;
  1484. }
  1485. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1486. }
  1487. }
  1488. //---------------------------------------------------------------------------
  1489. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1490. TFileOperationProgressType & ProgressData)
  1491. {
  1492. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1493. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1494. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1495. FileOperationProgress(ProgressData);
  1496. }
  1497. //---------------------------------------------------------------------------
  1498. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1499. {
  1500. UnicodeString Result;
  1501. if (FProgressForm != NULL)
  1502. {
  1503. Result = FProgressForm->ProgressStr();
  1504. }
  1505. return Result;
  1506. }
  1507. //---------------------------------------------------------------------------
  1508. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1509. bool DragDrop)
  1510. {
  1511. return (!DragDrop && (DropSourceControl == NULL)) ||
  1512. (DropSourceControl == DirView(osOther));
  1513. }
  1514. //---------------------------------------------------------------------------
  1515. TListItem * TCustomScpExplorerForm::VisualiseOperationFinished(TOperationSide Side, const UnicodeString & FileName, bool Unselect)
  1516. {
  1517. TCustomDirView * ADirView = DirView(Side);
  1518. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1519. TListItem * Item = ADirView->FindFileItem(FileNameOnly);
  1520. // this can happen when local drive is unplugged in the middle of the operation
  1521. if (Item != NULL)
  1522. {
  1523. if (Unselect)
  1524. {
  1525. Item->Selected = false;
  1526. }
  1527. ADirView->MakeProgressVisible(Item);
  1528. }
  1529. return Item;
  1530. }
  1531. //---------------------------------------------------------------------------
  1532. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1533. TFileOperation Operation, TOperationSide Side,
  1534. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1535. TOnceDoneOperation & OnceDoneOperation)
  1536. {
  1537. if (!FAutoOperation)
  1538. {
  1539. // no selection on "/upload", form serves only as event handler
  1540. // (it is not displayed)
  1541. if (PanelOperation(Side, FDragDropOperation) && Visible &&
  1542. ((Operation == FPrimaryOperation) ||
  1543. ((Operation != foCalculateSize) && (Operation != foGetProperties) && (Operation != foCalculateChecksum))))
  1544. {
  1545. TOperationSide DViewSide;
  1546. if ((FProgressSide != osCurrent) &&
  1547. IsLocalBrowserMode() && (Side == osLocal)) // Only to limit the impact
  1548. {
  1549. // assume the operation is over the focused panel
  1550. DViewSide = FProgressSide;
  1551. }
  1552. else
  1553. {
  1554. DViewSide = Side;
  1555. }
  1556. if (FOnFileOperationFinished != NULL)
  1557. {
  1558. FOnFileOperationFinished(DViewSide, FileName, Success);
  1559. }
  1560. else
  1561. {
  1562. VisualiseOperationFinished(DViewSide, FileName, Success);
  1563. }
  1564. }
  1565. if (TFileOperationProgressType::IsTransferOperation(Operation))
  1566. {
  1567. if (DebugAlwaysTrue(!IsLocalBrowserMode()))
  1568. {
  1569. if (Side == osLocal)
  1570. {
  1571. Configuration->Usage->Inc(L"UploadedFiles");
  1572. }
  1573. else
  1574. {
  1575. Configuration->Usage->Inc(L"DownloadedFiles");
  1576. }
  1577. }
  1578. }
  1579. }
  1580. if (Success && (FSynchronizeController != NULL))
  1581. {
  1582. DebugAssert(!IsSideLocalBrowser(osRemote));
  1583. if (Operation == foCopy)
  1584. {
  1585. DebugAssert(Side == osLocal);
  1586. FSynchronizeController->LogOperation(soUpload, FileName);
  1587. }
  1588. else if (Operation == foDelete)
  1589. {
  1590. DebugAssert(Side == osRemote);
  1591. FSynchronizeController->LogOperation(soDelete, FileName);
  1592. }
  1593. }
  1594. if (FProgressForm)
  1595. {
  1596. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1597. }
  1598. }
  1599. //---------------------------------------------------------------------------
  1600. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1601. TFileOperation Operation, TOperationSide Side,
  1602. bool Temp, const UnicodeString & FileName, Boolean Success,
  1603. TOnceDoneOperation & OnceDoneOperation)
  1604. {
  1605. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1606. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1607. OnceDoneOperation);
  1608. }
  1609. //---------------------------------------------------------------------------
  1610. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1611. {
  1612. return false;
  1613. }
  1614. //---------------------------------------------------------------------------
  1615. bool TCustomScpExplorerForm::SupportsLocalBrowser()
  1616. {
  1617. return false;
  1618. }
  1619. //---------------------------------------------------------------------------
  1620. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1621. {
  1622. return false;
  1623. }
  1624. //---------------------------------------------------------------------------
  1625. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1626. {
  1627. return NULL;
  1628. }
  1629. //---------------------------------------------------------------------------
  1630. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1631. {
  1632. DebugAssert(GetSide(Side) == osRemote);
  1633. DebugUsedParam(Side);
  1634. return RemoteDirView;
  1635. }
  1636. //---------------------------------------------------------------------------
  1637. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1638. {
  1639. DebugAssert(GetSide(Side) == osRemote);
  1640. DebugUsedParam(Side);
  1641. return RemoteDriveView;
  1642. }
  1643. //---------------------------------------------------------------------------
  1644. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1645. {
  1646. DebugAssert(GetSide(Side) == osRemote);
  1647. DebugUsedParam(Side);
  1648. return HasActiveTerminal();
  1649. }
  1650. //---------------------------------------------------------------------------
  1651. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1652. TOperationSide Side, int FilesOnly)
  1653. {
  1654. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1655. }
  1656. //---------------------------------------------------------------------------
  1657. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1658. TOperationSide Side, int FilesOnly)
  1659. {
  1660. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1661. }
  1662. //---------------------------------------------------------------------------
  1663. struct THistoryItemData
  1664. {
  1665. short int Side;
  1666. short int Index;
  1667. };
  1668. //---------------------------------------------------------------------------
  1669. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1670. {
  1671. DirView(Side)->HistoryGo(Index);
  1672. }
  1673. //---------------------------------------------------------------------------
  1674. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1675. {
  1676. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1677. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1678. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1679. }
  1680. //---------------------------------------------------------------------------
  1681. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1682. bool Back)
  1683. {
  1684. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1685. {
  1686. TCustomDirView * DView = DirView(Side);
  1687. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1688. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1689. if (ICount > 10)
  1690. {
  1691. ICount = 10;
  1692. }
  1693. Menu->Items->Clear();
  1694. THistoryItemData Data;
  1695. Data.Side = Side;
  1696. for (short int i = 1; i <= ICount; i++)
  1697. {
  1698. TTBCustomItem * Item = new TTBXItem(Menu);
  1699. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1700. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1701. Item->Hint = DView->HistoryPath[Data.Index];
  1702. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1703. Item->Tag = *reinterpret_cast<int*>(&Data);
  1704. Item->OnClick = HistoryItemClick;
  1705. Menu->Items->Add(Item);
  1706. }
  1707. }
  1708. }
  1709. //---------------------------------------------------------------------------
  1710. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1711. TOperationSide Side, bool Back)
  1712. {
  1713. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1714. {
  1715. // workaround
  1716. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1717. // operator new allocates memory in Pascal code, but calls inline
  1718. // contructor in C++, leading in problems, the function does
  1719. // both in Pascal code
  1720. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1721. UpdateHistoryMenu(Side, Back);
  1722. }
  1723. return FHistoryMenu[Side == osLocal][Back];
  1724. }
  1725. //---------------------------------------------------------------------------
  1726. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1727. TCustomDirView *Sender)
  1728. {
  1729. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1730. UpdateHistoryMenu(Side, true);
  1731. UpdateHistoryMenu(Side, false);
  1732. }
  1733. //---------------------------------------------------------------------------
  1734. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1735. {
  1736. // remote custom commands can be executed only if the server supports shell commands
  1737. // or have secondary shell
  1738. return HasActiveTerminal() && (Terminal->IsCapable[fcSecondaryShell] || Terminal->IsCapable[fcShellAnyCommand]);
  1739. }
  1740. //---------------------------------------------------------------------------
  1741. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1742. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1743. {
  1744. // -1 = hidden, 0 = disabled, 1 = enabled
  1745. int Result;
  1746. TFileCustomCommand RemoteCustomCommand;
  1747. TLocalCustomCommand LocalCustomCommand;
  1748. TFileCustomCommand * NonInteractiveCustomCommand =
  1749. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1750. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1751. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1752. if (FLAGCLEAR(Command.Params, ccLocal))
  1753. {
  1754. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1755. // custom command that does not operate with files can be executed anytime ...
  1756. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1757. {
  1758. // ... but do not show such command in remote file menu
  1759. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1760. {
  1761. Result = AllowedState;
  1762. }
  1763. else
  1764. {
  1765. Result = -1;
  1766. }
  1767. }
  1768. else
  1769. {
  1770. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1771. {
  1772. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1773. }
  1774. else
  1775. {
  1776. Result = -1;
  1777. }
  1778. }
  1779. }
  1780. else
  1781. {
  1782. // custom command that does not operate with files can be executed anytime
  1783. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1784. {
  1785. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1786. }
  1787. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1788. {
  1789. if ((ListType == ccltAll) || (ListType == ccltFile))
  1790. {
  1791. // special case is "diff"-style command that can be executed over any side,
  1792. // if we have both sides
  1793. Result =
  1794. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1795. // directly (instead of EnableSelectedOperation) to pass
  1796. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1797. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1798. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1799. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1800. }
  1801. else if (ListType == ccltBoth)
  1802. {
  1803. DebugAssert(!IsLocalBrowserMode());
  1804. Result = 1;
  1805. }
  1806. else
  1807. {
  1808. Result = -1;
  1809. }
  1810. }
  1811. else
  1812. {
  1813. if ((ListType == ccltAll) ||
  1814. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1815. {
  1816. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1817. }
  1818. else
  1819. {
  1820. Result = -1;
  1821. }
  1822. }
  1823. if ((Result > 0) &&
  1824. LocalCustomCommand.IsSessionCommand(Cmd) &&
  1825. !HasActiveTerminal())
  1826. {
  1827. Result = 0;
  1828. }
  1829. }
  1830. return Result;
  1831. }
  1832. //---------------------------------------------------------------------------
  1833. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1834. TStrings * FileList, const TCustomCommandType & ACommand,
  1835. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1836. {
  1837. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1838. {
  1839. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1840. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1841. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1842. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1843. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1844. bool Capture =
  1845. FLAGSET(ACommand.Params, ccShowResults) ||
  1846. FLAGSET(ACommand.Params, ccCopyResults) ||
  1847. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1848. TCaptureOutputEvent OutputEvent = NULL;
  1849. DebugAssert(FCapturedLog == NULL);
  1850. if (Capture)
  1851. {
  1852. FCapturedLog = new TStringList();
  1853. OutputEvent = TerminalCaptureLog;
  1854. }
  1855. try
  1856. {
  1857. if (!RemoteCustomCommand.IsFileCommand(Command))
  1858. {
  1859. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1860. OutputEvent);
  1861. }
  1862. else
  1863. {
  1864. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1865. }
  1866. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1867. {
  1868. if (FLAGSET(ACommand.Params, ccCopyResults))
  1869. {
  1870. CopyToClipboard(FCapturedLog);
  1871. }
  1872. if (FLAGSET(ACommand.Params, ccShowResults))
  1873. {
  1874. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1875. }
  1876. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1877. {
  1878. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1879. }
  1880. }
  1881. }
  1882. __finally
  1883. {
  1884. SAFE_DESTROY(FCapturedLog);
  1885. }
  1886. }
  1887. }
  1888. //---------------------------------------------------------------------------
  1889. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1890. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1891. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1892. {
  1893. DebugAssert(!IsLocalBrowserMode());
  1894. TStrings * LocalFileList = NULL;
  1895. TStrings * RemoteFileList = NULL;
  1896. TStrings * RemoteFileListFull = NULL;
  1897. try
  1898. {
  1899. if (LocalFileCommand)
  1900. {
  1901. if (ALocalFileList == NULL)
  1902. {
  1903. DebugAssert(HasDirView[osLocal]);
  1904. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1905. // directly (instead of EnableSelectedOperation) to pass
  1906. // false to FocusedFileOnlyWhenFocused
  1907. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1908. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1909. }
  1910. else
  1911. {
  1912. LocalFileList = ALocalFileList;
  1913. }
  1914. if (FileListCommand)
  1915. {
  1916. if (LocalFileList->Count != 1)
  1917. {
  1918. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1919. }
  1920. }
  1921. else
  1922. {
  1923. if ((LocalFileList->Count != 1) &&
  1924. (FileList->Count != 1) &&
  1925. (LocalFileList->Count != FileList->Count))
  1926. {
  1927. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1928. }
  1929. }
  1930. }
  1931. UnicodeString RootTempDir;
  1932. UnicodeString TempDir;
  1933. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1934. if (!RemoteFiles)
  1935. {
  1936. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, true);
  1937. }
  1938. try
  1939. {
  1940. TDateTimes RemoteFileTimes;
  1941. if (RemoteFiles)
  1942. {
  1943. RemoteFileList = FileList;
  1944. }
  1945. else
  1946. {
  1947. RemoteFileList = new TStringList();
  1948. TMakeLocalFileListParams MakeFileListParam;
  1949. MakeFileListParam.FileList = RemoteFileList;
  1950. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1951. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1952. MakeFileListParam.Recursive =
  1953. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1954. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1955. if (!MakeFileListParam.Recursive)
  1956. {
  1957. RemoteFileTimes.clear();
  1958. RemoteFileListFull = new TStringList();
  1959. MakeFileListParam.FileList = RemoteFileListFull;
  1960. MakeFileListParam.Recursive = true;
  1961. MakeFileListParam.IncludeDirs = false;
  1962. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1963. }
  1964. else
  1965. {
  1966. RemoteFileListFull = RemoteFileList;
  1967. }
  1968. }
  1969. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1970. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1971. if (!NonBlocking)
  1972. {
  1973. int Count;
  1974. if (FileListCommand)
  1975. {
  1976. Count = 1;
  1977. }
  1978. else if (LocalFileCommand)
  1979. {
  1980. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  1981. }
  1982. else
  1983. {
  1984. Count = RemoteFileList->Count;
  1985. }
  1986. Progress.Start(foCustomCommand, osRemote, Count);
  1987. DebugAssert(FProgressForm != NULL);
  1988. FProgressForm->ReadOnly = true;
  1989. }
  1990. try
  1991. {
  1992. if (FileListCommand)
  1993. {
  1994. UnicodeString LocalFile;
  1995. // MakeFileList does not delimit filenames
  1996. UnicodeString FileList = MakeFileList(RemoteFileList);
  1997. if (LocalFileCommand)
  1998. {
  1999. DebugAssert(LocalFileList->Count == 1);
  2000. LocalFile = LocalFileList->Strings[0];
  2001. }
  2002. TLocalCustomCommand CustomCommand(Data,
  2003. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  2004. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  2005. if (NonBlocking)
  2006. {
  2007. DebugAssert(!POutput);
  2008. ExecuteShellChecked(ShellCommand);
  2009. }
  2010. else
  2011. {
  2012. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  2013. }
  2014. }
  2015. else if (LocalFileCommand)
  2016. {
  2017. if (LocalFileList->Count == 1)
  2018. {
  2019. UnicodeString LocalFile = LocalFileList->Strings[0];
  2020. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2021. {
  2022. UnicodeString FileName = RemoteFileList->Strings[Index];
  2023. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2024. Progress.SetFile(FileNameForProgress);
  2025. TLocalCustomCommand CustomCommand(Data,
  2026. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  2027. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2028. TOnceDoneOperation OnceDoneOperation;
  2029. // Do not unselect anything, as it won't work with two panels anyway
  2030. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2031. }
  2032. }
  2033. else if (RemoteFileList->Count == 1)
  2034. {
  2035. UnicodeString FileName = RemoteFileList->Strings[0];
  2036. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2037. {
  2038. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2039. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2040. Progress.SetFile(LocalFileNameOnly);
  2041. TLocalCustomCommand CustomCommand(
  2042. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2043. FileName, LocalFileName, L"");
  2044. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2045. TOnceDoneOperation OnceDoneOperation;
  2046. // Do not unselect anything, as it won't work with two panels anyway
  2047. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2048. }
  2049. }
  2050. else
  2051. {
  2052. if (LocalFileList->Count != RemoteFileList->Count)
  2053. {
  2054. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  2055. }
  2056. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2057. {
  2058. UnicodeString FileName = RemoteFileList->Strings[Index];
  2059. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2060. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2061. Progress.SetFile(LocalFileNameOnly); // sic
  2062. TLocalCustomCommand CustomCommand(
  2063. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2064. FileName, LocalFileName, L"");
  2065. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2066. TOnceDoneOperation OnceDoneOperation;
  2067. // Do not unselect anything, as it won't work with two panels anyway
  2068. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2069. }
  2070. }
  2071. }
  2072. else
  2073. {
  2074. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2075. {
  2076. UnicodeString FileName = RemoteFileList->Strings[Index];
  2077. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2078. Progress.SetFile(FileNameForProgress);
  2079. TLocalCustomCommand CustomCommand(Data,
  2080. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2081. FileName, L"", L"");
  2082. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2083. TOnceDoneOperation OnceDoneOperation;
  2084. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  2085. }
  2086. }
  2087. }
  2088. __finally
  2089. {
  2090. if (!NonBlocking)
  2091. {
  2092. Progress.Stop();
  2093. }
  2094. }
  2095. DebugAssert(!FAutoOperation);
  2096. if (!RemoteFiles)
  2097. {
  2098. TempDir = IncludeTrailingBackslash(TempDir);
  2099. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  2100. {
  2101. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  2102. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  2103. // Skip directories, process only nested files.
  2104. // The check is redundant as FileAge fails for directories anyway.
  2105. !DirectoryExists(FileName))
  2106. {
  2107. UnicodeString RemoteDir =
  2108. UnixExtractFileDir(
  2109. UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
  2110. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2111. TDateTime NewTime;
  2112. if (FileAge(FileName, NewTime) &&
  2113. (NewTime != RemoteFileTimes[Index]))
  2114. {
  2115. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2116. TemporaryFileCopyParam(CopyParam);
  2117. CopyParam.FileMask = L"";
  2118. FAutoOperation = true;
  2119. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2120. TemporaryFilesList->Add(FileName);
  2121. Terminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2122. }
  2123. }
  2124. }
  2125. }
  2126. }
  2127. __finally
  2128. {
  2129. FAutoOperation = false;
  2130. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2131. {
  2132. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2133. }
  2134. }
  2135. }
  2136. __finally
  2137. {
  2138. if (RemoteFileList != FileList)
  2139. {
  2140. delete RemoteFileList;
  2141. }
  2142. if (LocalFileList != ALocalFileList)
  2143. {
  2144. delete LocalFileList;
  2145. }
  2146. if (RemoteFileListFull != RemoteFileList)
  2147. {
  2148. delete RemoteFileListFull;
  2149. }
  2150. }
  2151. }
  2152. //---------------------------------------------------------------------------
  2153. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2154. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2155. bool FileListCommand, UnicodeString * POutput)
  2156. {
  2157. std::unique_ptr<TStrings> SelectedFileList(DirView(osCurrent)->CreateFileList(false, true, NULL));
  2158. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2159. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2160. {
  2161. UnicodeString FileName = SelectedFileList->Strings[Index];
  2162. if (DirectoryExists(FileName))
  2163. {
  2164. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2165. {
  2166. LocalFileList->Add(FileName);
  2167. }
  2168. if (FLAGSET(ACommand.Params, ccRecursive))
  2169. {
  2170. TMakeLocalFileListParams MakeFileListParam;
  2171. MakeFileListParam.FileList = LocalFileList.get();
  2172. MakeFileListParam.FileTimes = NULL;
  2173. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2174. MakeFileListParam.Recursive = true;
  2175. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2176. }
  2177. }
  2178. else
  2179. {
  2180. LocalFileList->Add(FileName);
  2181. }
  2182. }
  2183. UnicodeString RemotePath;
  2184. if (Terminal != NULL)
  2185. {
  2186. RemotePath = Terminal->CurrentDirectory;
  2187. }
  2188. if (FileListCommand)
  2189. {
  2190. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2191. TLocalCustomCommand CustomCommand(
  2192. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2193. L"", L"", FileList);
  2194. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2195. }
  2196. else
  2197. {
  2198. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2199. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2200. DebugAssert(FProgressForm != NULL);
  2201. FProgressForm->ReadOnly = true;
  2202. try
  2203. {
  2204. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2205. {
  2206. UnicodeString FileName = LocalFileList->Strings[Index];
  2207. Progress.SetFile(FileName);
  2208. TLocalCustomCommand CustomCommand(
  2209. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2210. FileName, L"", L"");
  2211. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2212. TOnceDoneOperation OnceDoneOperation;
  2213. Progress.Finish(FileName, true, OnceDoneOperation);
  2214. }
  2215. }
  2216. __finally
  2217. {
  2218. Progress.Stop();
  2219. }
  2220. }
  2221. }
  2222. //---------------------------------------------------------------------------
  2223. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2224. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2225. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2226. {
  2227. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2228. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2229. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2230. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2231. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2232. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2233. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2234. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2235. std::unique_ptr<UnicodeString> POutput;
  2236. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2237. FLAGSET(ACommand.Params, ccCopyResults))
  2238. {
  2239. POutput.reset(new UnicodeString());
  2240. }
  2241. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  2242. FProgressSide = FCurrentSide;
  2243. if (!LocalCustomCommand.IsFileCommand(Command))
  2244. {
  2245. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2246. }
  2247. // remote files?
  2248. else if (!IsSideLocalBrowser(FCurrentSide) || LocalFileCommand)
  2249. {
  2250. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2251. }
  2252. // local files
  2253. else
  2254. {
  2255. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2256. }
  2257. if (POutput.get() != NULL)
  2258. {
  2259. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2260. int P = POutput->Pos(sLineBreak);
  2261. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2262. {
  2263. POutput->SetLength(P - 1);
  2264. }
  2265. // Copy even empty output to clipboard
  2266. if (FLAGSET(ACommand.Params, ccCopyResults))
  2267. {
  2268. CopyToClipboard(*POutput);
  2269. }
  2270. // But do not show an empty message box.
  2271. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2272. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2273. !POutput->IsEmpty())
  2274. {
  2275. TClipboardHandler ClipboardHandler;
  2276. ClipboardHandler.Text = *POutput;
  2277. TMessageParams Params;
  2278. TQueryButtonAlias Aliases[1];
  2279. Aliases[0].Button = qaRetry;
  2280. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2281. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2282. Params.Aliases = Aliases;
  2283. Params.AliasesCount = LENOF(Aliases);
  2284. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2285. }
  2286. }
  2287. }
  2288. //---------------------------------------------------------------------------
  2289. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2290. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2291. {
  2292. TCustomCommandData Data;
  2293. UnicodeString Site;
  2294. UnicodeString RemotePath;
  2295. if (HasActiveTerminal())
  2296. {
  2297. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  2298. Data = TCustomCommandData(SessionData.get());
  2299. Site = Terminal->SessionData->SessionKey;
  2300. RemotePath = Terminal->CurrentDirectory;
  2301. }
  2302. UnicodeString HelpKeyword = ACommand.HomePage;
  2303. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2304. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2305. {
  2306. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2307. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2308. {
  2309. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2310. }
  2311. else
  2312. {
  2313. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2314. }
  2315. if (!DoCustomCommandOptionsDialog(
  2316. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2317. {
  2318. Abort();
  2319. }
  2320. }
  2321. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2322. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2323. {
  2324. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2325. }
  2326. else
  2327. {
  2328. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2329. }
  2330. }
  2331. //---------------------------------------------------------------------------
  2332. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2333. const TCustomCommandType & Command)
  2334. {
  2335. DebugAssert(FCustomCommandLocalFileList != NULL);
  2336. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2337. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2338. TStrings * LocalFileList = new TStringList();
  2339. TStrings * RemoteFileList = new TStringList();
  2340. try
  2341. {
  2342. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2343. {
  2344. LocalFileList->Clear();
  2345. LocalFileList->AddObject(
  2346. FCustomCommandLocalFileList->Strings[Index],
  2347. FCustomCommandLocalFileList->Objects[Index]);
  2348. RemoteFileList->Clear();
  2349. RemoteFileList->AddObject(
  2350. FCustomCommandRemoteFileList->Strings[Index],
  2351. FCustomCommandRemoteFileList->Objects[Index]);
  2352. CustomCommand(RemoteFileList, Command, LocalFileList);
  2353. }
  2354. }
  2355. __finally
  2356. {
  2357. delete LocalFileList;
  2358. delete RemoteFileList;
  2359. }
  2360. }
  2361. //---------------------------------------------------------------------------
  2362. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2363. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2364. {
  2365. delete FCustomCommandLocalFileList;
  2366. delete FCustomCommandRemoteFileList;
  2367. // takeover ownership,
  2368. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2369. FCustomCommandLocalFileList = LocalFileList;
  2370. FCustomCommandRemoteFileList = RemoteFileList;
  2371. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2372. if (Button != NULL)
  2373. {
  2374. FCustomCommandMenu->Items->Clear();
  2375. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2376. MenuPopup(FCustomCommandMenu, Button);
  2377. }
  2378. else
  2379. {
  2380. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2381. }
  2382. }
  2383. //---------------------------------------------------------------------------
  2384. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2385. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2386. {
  2387. DebugAssert(FCapturedLog != NULL);
  2388. if ((OutputType == cotOutput) || (OutputType == cotError))
  2389. {
  2390. FCapturedLog->Add(AddedLine);
  2391. }
  2392. }
  2393. //---------------------------------------------------------------------------
  2394. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2395. TOperationSide Side)
  2396. {
  2397. return
  2398. (Side == osOther) &&
  2399. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2400. }
  2401. //---------------------------------------------------------------------------
  2402. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2403. TOperationSide Side, TTBXCustomItem * Item,
  2404. TResolvedDoubleClickAction DoubleClickAction1, TResolvedDoubleClickAction DoubleClickAction2)
  2405. {
  2406. TCustomDirView * DView = DirView(Side);
  2407. bool IsDefault = false;
  2408. if (DView->ItemFocused != NULL)
  2409. {
  2410. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  2411. TResolvedDoubleClickAction DefaultAction =
  2412. WinConfiguration->ResolveDoubleClickAction(DView->ItemIsDirectory(DView->ItemFocused), ATerminal);
  2413. IsDefault = (DefaultAction == DoubleClickAction1) || (DefaultAction == DoubleClickAction2);
  2414. }
  2415. TTBItemOptions O = Item->Options;
  2416. if (IsDefault)
  2417. {
  2418. Item->Options = O << tboDefault;
  2419. }
  2420. else
  2421. {
  2422. Item->Options = O >> tboDefault;
  2423. }
  2424. }
  2425. //---------------------------------------------------------------------------
  2426. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2427. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2428. {
  2429. TCustomDirView * DView = DirView(Side);
  2430. TListItem * Item = DView->ItemFocused;
  2431. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2432. EnableFocusedOperation[Side])
  2433. {
  2434. TPoint ClientPoint;
  2435. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2436. TPoint(0, 0) : MousePos;
  2437. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2438. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2439. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2440. }
  2441. }
  2442. //---------------------------------------------------------------------------
  2443. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2444. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2445. {
  2446. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, rdcaChangeDir, rdcaOpen);
  2447. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, rdcaEdit, rdcaNone);
  2448. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, rdcaCopy, rdcaNone);
  2449. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2450. Handled = true;
  2451. }
  2452. //---------------------------------------------------------------------------
  2453. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2454. {
  2455. }
  2456. //---------------------------------------------------------------------------
  2457. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2458. {
  2459. DebugAssert(FErrorList == NULL);
  2460. if (WinConfiguration->ContinueOnError)
  2461. {
  2462. FErrorList = new TStringList();
  2463. Configuration->Usage->Inc(L"ContinuationsOnError");
  2464. }
  2465. NonVisualDataModule->StartBusy();
  2466. }
  2467. //---------------------------------------------------------------------------
  2468. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2469. {
  2470. NonVisualDataModule->EndBusy();
  2471. if (FErrorList)
  2472. {
  2473. HandleErrorList(FErrorList);
  2474. }
  2475. }
  2476. //---------------------------------------------------------------------------
  2477. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2478. const TCopyParamType & CopyParam)
  2479. {
  2480. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2481. {
  2482. Configuration->Usage->Inc(L"FileMaskUses");
  2483. }
  2484. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2485. {
  2486. Configuration->Usage->Inc(L"OperationMaskUses");
  2487. }
  2488. }
  2489. //---------------------------------------------------------------------------
  2490. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2491. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2492. {
  2493. DebugAssert(!IsLocalBrowserMode());
  2494. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2495. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2496. TTransferOperationParam DefaultParam;
  2497. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2498. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2499. switch (Param.Queue)
  2500. {
  2501. case asOn:
  2502. CopyParam.Queue = true;
  2503. break;
  2504. case asOff:
  2505. CopyParam.Queue = false;
  2506. break;
  2507. case asAuto:
  2508. default:
  2509. // keep default
  2510. break;
  2511. }
  2512. bool Result =
  2513. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2514. if (Result)
  2515. {
  2516. DebugAssert(Terminal);
  2517. bool SelectionRestored = false;
  2518. TCustomDirView * DView = NULL;
  2519. if (HasDirView[Side])
  2520. {
  2521. DView = DirView(Side);
  2522. DView->SaveSelection();
  2523. DView->SaveSelectedNames();
  2524. }
  2525. UpdateCopyParamCounters(CopyParam);
  2526. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2527. DebugAssert(FTransferResumeList == NULL);
  2528. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2529. FMoveToQueue = false;
  2530. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2531. try
  2532. {
  2533. TStrings * PermanentFileList;
  2534. std::unique_ptr<TStrings> PermanentFileListOwner;
  2535. try
  2536. {
  2537. if (Side == osLocal)
  2538. {
  2539. PermanentFileList = FileList;
  2540. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2541. CopyParam.IncludeFileMask.SetRoots(FileList, Param.TargetDirectory);
  2542. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2543. if (Operation == foMove)
  2544. {
  2545. ReloadLocalDirectory();
  2546. if (DView != NULL)
  2547. {
  2548. DView->RestoreSelection();
  2549. }
  2550. SelectionRestored = true;
  2551. }
  2552. }
  2553. else
  2554. {
  2555. // Clone the file list as it may refer to current directory files,
  2556. // which get destroyed, when the source directory is reloaded after foMove operation.
  2557. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2558. // But for a hotfix, we are not going to do such a big change.
  2559. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2560. PermanentFileList = PermanentFileListOwner.get();
  2561. try
  2562. {
  2563. CopyParam.IncludeFileMask.SetRoots(Param.TargetDirectory, FileList);
  2564. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2565. }
  2566. __finally
  2567. {
  2568. if (Operation == foMove)
  2569. {
  2570. if (DView != NULL)
  2571. {
  2572. DView->RestoreSelection();
  2573. }
  2574. SelectionRestored = true;
  2575. }
  2576. ReloadLocalDirectory(Param.TargetDirectory);
  2577. }
  2578. }
  2579. // When the transfer batch is aborted between individual file transfers, the Abort is not called.
  2580. if (FMoveToQueue)
  2581. {
  2582. Abort();
  2583. }
  2584. }
  2585. catch (EAbort &)
  2586. {
  2587. if (FMoveToQueue)
  2588. {
  2589. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2590. DebugAssert(CopyParam.TransferSkipList == NULL);
  2591. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2592. if (TransferResumeList->Count > 0)
  2593. {
  2594. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2595. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2596. }
  2597. CopyParam.TransferSkipList = TransferResumeList.release();
  2598. // not really needed, just to keep it consistent with TransferResumeList
  2599. FTransferResumeList = NULL;
  2600. FMoveToQueue = false;
  2601. Configuration->Usage->Inc("MovesToBackground");
  2602. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2603. ClearTransferSourceSelection(Direction);
  2604. }
  2605. throw;
  2606. }
  2607. }
  2608. __finally
  2609. {
  2610. if (!SelectionRestored && (DView != NULL))
  2611. {
  2612. DView->DiscardSavedSelection();
  2613. }
  2614. FTransferResumeList = NULL;
  2615. }
  2616. }
  2617. return Result;
  2618. }
  2619. //---------------------------------------------------------------------------
  2620. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2621. TOperationSide Side, TStrings * FileList, void * Param)
  2622. {
  2623. DebugAssert(FileList->Count);
  2624. // We deliberately do not toggle alternative flag (Param), but use OR,
  2625. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2626. // shortcut of CurrentDeleteAlternativeAction
  2627. bool Alternative =
  2628. bool(Param) || UseAlternativeFunction();
  2629. bool Recycle;
  2630. if (IsSideLocalBrowser(Side))
  2631. {
  2632. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2633. }
  2634. else
  2635. {
  2636. Recycle =
  2637. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2638. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2639. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2640. }
  2641. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2642. if (!Result)
  2643. {
  2644. UnicodeString Query;
  2645. if (FileList->Count == 1)
  2646. {
  2647. if (IsSideLocalBrowser(Side))
  2648. {
  2649. Query = ExtractFileName(FileList->Strings[0]);
  2650. }
  2651. else
  2652. {
  2653. Query = UnixExtractFileName(FileList->Strings[0]);
  2654. }
  2655. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2656. }
  2657. else
  2658. {
  2659. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2660. }
  2661. TMessageParams Params(mpNeverAskAgainCheck);
  2662. Params.ImageName = L"Delete file";
  2663. unsigned int Answer =
  2664. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2665. if (Answer == qaNeverAskAgain)
  2666. {
  2667. Result = true;
  2668. if (Recycle)
  2669. {
  2670. WinConfiguration->ConfirmRecycling = false;
  2671. }
  2672. else
  2673. {
  2674. WinConfiguration->ConfirmDeleting = false;
  2675. }
  2676. }
  2677. else
  2678. {
  2679. Result = (Answer == qaOK);
  2680. }
  2681. }
  2682. if (Result)
  2683. {
  2684. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2685. }
  2686. return Result;
  2687. }
  2688. //---------------------------------------------------------------------------
  2689. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2690. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2691. {
  2692. TAutoBatch AutoBatch(this);
  2693. bool Result;
  2694. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2695. {
  2696. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2697. }
  2698. else if (Operation == foRename)
  2699. {
  2700. DebugAssert(DirView(Side)->ItemFocused);
  2701. DirView(Side)->ItemFocused->EditCaption();
  2702. Result = true;
  2703. }
  2704. else if (Operation == foDelete)
  2705. {
  2706. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2707. }
  2708. else if (Operation == foSetProperties)
  2709. {
  2710. DirView(osRemote)->SaveSelectedNames();
  2711. Result = SetProperties(Side, FileList);
  2712. }
  2713. else if (Operation == foCustomCommand)
  2714. {
  2715. DebugAssert(Param);
  2716. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2717. DirView(osRemote)->SaveSelectedNames();
  2718. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2719. CustomCommand(FileList, *Command, NULL);
  2720. Result = true;
  2721. }
  2722. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2723. {
  2724. DebugAssert(!IsSideLocalBrowser(Side));
  2725. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2726. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2727. }
  2728. else if (Operation == foLock)
  2729. {
  2730. DebugAssert(!IsSideLocalBrowser(Side));
  2731. LockFiles(FileList, true);
  2732. Result = true;
  2733. }
  2734. else if (Operation == foUnlock)
  2735. {
  2736. DebugAssert(!IsSideLocalBrowser(Side));
  2737. LockFiles(FileList, false);
  2738. Result = true;
  2739. }
  2740. else
  2741. {
  2742. DebugFail();
  2743. }
  2744. return Result;
  2745. }
  2746. //---------------------------------------------------------------------------
  2747. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2748. {
  2749. if (Side == osCurrent)
  2750. {
  2751. Side = FCurrentSide;
  2752. }
  2753. return Side;
  2754. }
  2755. //---------------------------------------------------------------------------
  2756. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2757. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2758. {
  2759. Side = GetSide(Side);
  2760. bool FullPath = IsSideLocalBrowser(Side);
  2761. TCustomDirView * ADirView = DirView(Side);
  2762. std::unique_ptr<TStringList> FileList(new TStringList());
  2763. ADirView->CreateFileList(OnFocused, FullPath, FileList.get());
  2764. bool ReloadProperties =
  2765. (Side == osRemote) && (Operation == foSetProperties) &&
  2766. DebugAlwaysTrue(HasActiveTerminal()) && Terminal->IsCapable[fcLoadingAdditionalProperties];
  2767. bool Result = ExecuteFileOperation(Operation, Side, FileList.get(), NoConfirmation, Param);
  2768. if (Result && ReloadProperties)
  2769. {
  2770. std::unique_ptr<TStringList> FileList2(new TStringList());
  2771. FileList->CaseSensitive = true;
  2772. FileList->Sort();
  2773. for (int Index = 0; Index < ADirView->Items->Count; Index++)
  2774. {
  2775. TListItem * Item = ADirView->Items->Item[Index];
  2776. UnicodeString FileName = ADirView->ItemFileName(Item);
  2777. int Index2;
  2778. if (FileList->Find(FileName, Index2))
  2779. {
  2780. FileList2->AddObject(FileName, ADirView->ItemData(Item));
  2781. }
  2782. }
  2783. LoadFilesProperties(FileList2.get());
  2784. }
  2785. return Result;
  2786. }
  2787. //---------------------------------------------------------------------------
  2788. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2789. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2790. bool NoConfirmation, void * Param)
  2791. {
  2792. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2793. {
  2794. if (TFileOperationProgressType::IsTransferOperation(Operation))
  2795. {
  2796. DebugAssert(!IsLocalBrowserMode());
  2797. if (GetSide(Side) == osLocal)
  2798. {
  2799. Configuration->Usage->Inc(L"UploadsCommand");
  2800. }
  2801. else
  2802. {
  2803. Configuration->Usage->Inc(L"DownloadsCommand");
  2804. }
  2805. }
  2806. }
  2807. }
  2808. //---------------------------------------------------------------------------
  2809. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2810. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2811. {
  2812. TTransferOperationParam Param;
  2813. if ((WinConfiguration->Interface != ifCommander) ||
  2814. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2815. {
  2816. Flags &= ~cocShortCutHint;
  2817. }
  2818. TCustomDirView * DView = DirView(Side);
  2819. Param.Options =
  2820. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2821. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2822. if (FLAGSET(Flags, cocQueue))
  2823. {
  2824. Param.Queue = asOn;
  2825. }
  2826. else if (FLAGSET(Flags, cocNonQueue))
  2827. {
  2828. Param.Queue = asOff;
  2829. }
  2830. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2831. }
  2832. //---------------------------------------------------------------------------
  2833. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2834. {
  2835. int Count = ErrorList->Count;
  2836. try
  2837. {
  2838. if (ErrorList->Count)
  2839. {
  2840. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2841. if (MessageDialog(Message, qtError,
  2842. qaOK | qaCancel, HELP_NONE) == qaOK)
  2843. {
  2844. unsigned int Answer;
  2845. int Index = 0;
  2846. do
  2847. {
  2848. DebugAssert(Index >= 0 && Index < Count);
  2849. TQueryButtonAlias Aliases[2];
  2850. Aliases[0].Button = qaYes;
  2851. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2852. Aliases[1].Button = qaNo;
  2853. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2854. TMessageParams Params;
  2855. Params.Aliases = Aliases;
  2856. Params.AliasesCount = LENOF(Aliases);
  2857. int No = Index + 1;
  2858. UnicodeString Message = ErrorList->Strings[Index];
  2859. UnicodeString AMainInstructions;
  2860. UnicodeString Details;
  2861. if (ExtractMainInstructions(Message, AMainInstructions))
  2862. {
  2863. Details = Message;
  2864. Message = AMainInstructions;
  2865. }
  2866. Message = MainInstructions(FMTLOAD(ERROR_LIST_NUMBER, (No, Count, Message))) + Details;
  2867. Answer = MoreMessageDialog(
  2868. Message,
  2869. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2870. (Index ? qaYes : 0) | (Index < Count - 1 ? qaNo : 0) |
  2871. qaOK, HELP_NONE, &Params);
  2872. if (Answer == qaNo)
  2873. {
  2874. Index++;
  2875. }
  2876. if (Answer == qaYes)
  2877. {
  2878. Index--;
  2879. }
  2880. }
  2881. while (Answer != qaOK);
  2882. }
  2883. }
  2884. }
  2885. __finally
  2886. {
  2887. TStrings * List = ErrorList;
  2888. ErrorList = NULL;
  2889. for (int i = 0; i < Count; i++)
  2890. {
  2891. delete List->Objects[i];
  2892. }
  2893. delete List;
  2894. }
  2895. }
  2896. //---------------------------------------------------------------------------
  2897. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2898. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2899. {
  2900. // needed for checking filemasks, as there's no directory object
  2901. // associated with the file object
  2902. File->FullFileName = FullFileName;
  2903. TFileMasks::TParams MaskParams;
  2904. MaskParams.Size = File->Size;
  2905. MaskParams.Modification = File->Modification;
  2906. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2907. }
  2908. //---------------------------------------------------------------------------
  2909. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2910. {
  2911. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2912. Side = GetSide(Side);
  2913. TCustomDirView * DView = DirView(Side);
  2914. TListItem * FocusedItem = DView->ItemFocused;
  2915. UnicodeString Name;
  2916. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2917. {
  2918. Name = DView->ItemFileName(FocusedItem);
  2919. }
  2920. else
  2921. {
  2922. Name = LoadStr(NEW_FILE);
  2923. }
  2924. UnicodeString Names = Name;
  2925. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2926. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2927. HELP_EDIT_NEW, History.get(), true, NULL, true, 400))
  2928. {
  2929. while (!Names.IsEmpty())
  2930. {
  2931. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2932. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2933. UnicodeString TargetFileName;
  2934. UnicodeString LocalFileName;
  2935. UnicodeString RootTempDir;
  2936. UnicodeString TempDir;
  2937. UnicodeString RemoteDirectory;
  2938. bool ExistingFile = false;
  2939. if (!IsSideLocalBrowser(Side))
  2940. {
  2941. Name = AbsolutePath(Terminal->CurrentDirectory, Name);
  2942. TRemoteFile * File = NULL;
  2943. if (Terminal->FileExists(Name, &File) &&
  2944. !File->IsDirectory)
  2945. {
  2946. try
  2947. {
  2948. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2949. ExistingFile = true;
  2950. }
  2951. __finally
  2952. {
  2953. delete File;
  2954. }
  2955. }
  2956. if (!ExistingFile)
  2957. {
  2958. RemoteDirectory = UnixExtractFilePath(Name);
  2959. TemporaryDirectoryForRemoteFiles(
  2960. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2961. TargetFileName = UnixExtractFileName(Name);
  2962. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2963. LocalFileName = TempDir +
  2964. // We probably do not want to trim the VMS version here
  2965. Terminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2966. }
  2967. }
  2968. else
  2969. {
  2970. if (ExtractFilePath(Name).IsEmpty())
  2971. {
  2972. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2973. }
  2974. else
  2975. {
  2976. LocalFileName = ExpandFileName(Name);
  2977. }
  2978. TargetFileName = ExtractFileName(Name);
  2979. }
  2980. if (!ExistingFile)
  2981. {
  2982. bool NewFile = !FileExists(ApiPath(LocalFileName));
  2983. if (NewFile)
  2984. {
  2985. int File = FileCreate(ApiPath(LocalFileName));
  2986. if (File < 0)
  2987. {
  2988. if (!RootTempDir.IsEmpty())
  2989. {
  2990. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2991. }
  2992. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2993. }
  2994. else
  2995. {
  2996. FileClose(File);
  2997. }
  2998. }
  2999. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  3000. const TEditorData * ExternalEditor = NULL;
  3001. TFileMasks::TParams MaskParams; // size not known
  3002. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  3003. false, MaskParams);
  3004. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  3005. ExternalEditor, RootTempDir, RemoteDirectory, NewFile);
  3006. }
  3007. }
  3008. }
  3009. }
  3010. //---------------------------------------------------------------------------
  3011. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  3012. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  3013. {
  3014. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3015. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3016. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3017. return
  3018. ((ExecuteFileBy == efInternalEditor)) ||
  3019. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  3020. }
  3021. //---------------------------------------------------------------------------
  3022. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  3023. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  3024. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  3025. UnicodeString RemoteDirectory, bool NewFile)
  3026. {
  3027. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3028. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3029. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3030. DebugAssert(ExecuteFileBy != efDefaultEditor);
  3031. Side = GetSide(Side);
  3032. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  3033. if (!IsSideLocalBrowser(Side))
  3034. {
  3035. Data->Terminal = Terminal;
  3036. Data->Queue = Queue;
  3037. Data->SessionData = CloneCurrentSessionData();
  3038. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  3039. Data->RemoteDirectory = RemoteDirectory;
  3040. Data->SessionName = Terminal->SessionData->SessionName;
  3041. Data->LocalRootDirectory = LocalRootDirectory;
  3042. Data->OriginalFileName = OriginalFileName;
  3043. Data->Command = L""; // will be changed later for external editor
  3044. }
  3045. if (ExecuteFileBy == efInternalEditor)
  3046. {
  3047. if (!IsSideLocalBrowser(Side))
  3048. {
  3049. UnicodeString RemotePath = UnixCombinePaths(RemoteDirectory, OriginalFileName);
  3050. UnicodeString Caption = RemotePath + TitleSeparator + Terminal->SessionData->SessionName;
  3051. TForm * Editor;
  3052. try
  3053. {
  3054. TNotifyEvent OnFileChanged = NULL;
  3055. TNotifyEvent OnSaveAll = NULL;
  3056. TAnyModifiedEvent OnAnyModified = NULL;
  3057. // Edited files are uploaded in background queue, what we do not support with encrypted files
  3058. if (Terminal->IsCapable[fcBackgroundTransfers])
  3059. {
  3060. OnFileChanged = FEditorManager->FileChanged;
  3061. OnSaveAll = SaveAllInternalEditors;
  3062. OnAnyModified = AnyInternalEditorModified;
  3063. }
  3064. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  3065. FEditorManager->FileReload, FEditorManager->FileClosed,
  3066. OnSaveAll, OnAnyModified,
  3067. Caption, StandaloneOperation, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  3068. }
  3069. catch(...)
  3070. {
  3071. if (!LocalRootDirectory.IsEmpty())
  3072. {
  3073. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  3074. }
  3075. throw;
  3076. }
  3077. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  3078. }
  3079. else
  3080. {
  3081. DebugAssert(!StandaloneOperation);
  3082. TForm * Editor =
  3083. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  3084. SaveAllInternalEditors, AnyInternalEditorModified,
  3085. L"", false, SessionColor, -1, NewFile);
  3086. FLocalEditors->Add(Editor);
  3087. }
  3088. }
  3089. else
  3090. {
  3091. HANDLE Process;
  3092. if (ExecuteFileBy == efExternalEditor)
  3093. {
  3094. UnicodeString Program, Params, Dir;
  3095. Data->Command = ExternalEditor->ExternalEditor;
  3096. ReformatFileNameCommand(Data->Command);
  3097. SplitCommand(Data->Command, Program, Params, Dir);
  3098. Params = ExpandFileNameCommand(Params, FileName);
  3099. Program = ExpandEnvironmentVariables(Program);
  3100. if (!ExecuteShell(Program, Params, Process))
  3101. {
  3102. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  3103. }
  3104. }
  3105. else
  3106. {
  3107. DebugAssert(!IsSideLocalBrowser(Side));
  3108. if (!ExecuteShell(FileName, L"", Process))
  3109. {
  3110. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  3111. }
  3112. }
  3113. if (IsSideLocalBrowser(Side) ||
  3114. ((ExecuteFileBy == efShell) &&
  3115. !WinConfiguration->Editor.SDIShellEditor) ||
  3116. ((ExecuteFileBy == efExternalEditor) &&
  3117. !ExternalEditor->SDIExternalEditor))
  3118. {
  3119. // no need for handle
  3120. if (Process != NULL)
  3121. {
  3122. DebugCheck(CloseHandle(Process));
  3123. }
  3124. Process = INVALID_HANDLE_VALUE;
  3125. }
  3126. else
  3127. {
  3128. if (Process == NULL)
  3129. {
  3130. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  3131. }
  3132. }
  3133. if (!IsSideLocalBrowser(Side))
  3134. {
  3135. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  3136. }
  3137. }
  3138. }
  3139. //---------------------------------------------------------------------------
  3140. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3141. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3142. void * /*Arg*/)
  3143. {
  3144. if (Token != NULL)
  3145. {
  3146. EditorFormFileSave(static_cast<TForm *>(Token));
  3147. }
  3148. }
  3149. //---------------------------------------------------------------------------
  3150. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3151. {
  3152. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3153. {
  3154. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3155. }
  3156. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3157. }
  3158. //---------------------------------------------------------------------------
  3159. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3160. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3161. void * Arg)
  3162. {
  3163. if ((Token != NULL) &&
  3164. IsEditorFormModified(static_cast<TForm *>(Token)))
  3165. {
  3166. *static_cast<bool *>(Arg) = true;
  3167. }
  3168. }
  3169. //---------------------------------------------------------------------------
  3170. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3171. TObject * /*Sender*/, bool & Modified)
  3172. {
  3173. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3174. {
  3175. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3176. {
  3177. Modified = true;
  3178. }
  3179. }
  3180. if (!Modified)
  3181. {
  3182. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3183. }
  3184. }
  3185. //---------------------------------------------------------------------------
  3186. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3187. {
  3188. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3189. }
  3190. //---------------------------------------------------------------------------
  3191. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3192. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3193. UnicodeString & Result, UnicodeString & RootDirectory)
  3194. {
  3195. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3196. {
  3197. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3198. Result = RootDirectory;
  3199. }
  3200. else
  3201. {
  3202. RootDirectory = L"";
  3203. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3204. Result = IncludeTrailingBackslash(Result);
  3205. }
  3206. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3207. {
  3208. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3209. }
  3210. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3211. {
  3212. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3213. {
  3214. RemoteDirectory.Delete(1, 1);
  3215. }
  3216. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3217. }
  3218. if (!ForceDirectories(ApiPath(Result)))
  3219. {
  3220. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3221. }
  3222. }
  3223. //---------------------------------------------------------------------------
  3224. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3225. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3226. bool GetTargetNames, bool AutoOperation)
  3227. {
  3228. DebugAssert(!IsLocalBrowserMode());
  3229. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3230. if (ForceText)
  3231. {
  3232. CopyParam.TransferMode = tmAscii;
  3233. }
  3234. TemporaryFileCopyParam(CopyParam);
  3235. if (TempDir.IsEmpty())
  3236. {
  3237. TemporaryDirectoryForRemoteFiles(Terminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3238. }
  3239. DebugAssert(!FAutoOperation);
  3240. FAutoOperation = AutoOperation;
  3241. Terminal->ExceptionOnFail = true;
  3242. try
  3243. {
  3244. try
  3245. {
  3246. // turn off confirmations, as for MDI editors we may possibly download
  3247. // the same file over
  3248. Terminal->CopyToLocal(
  3249. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3250. if (GetTargetNames)
  3251. {
  3252. for (int i = 0; i < FileList->Count; i++)
  3253. {
  3254. FileList->Strings[i] =
  3255. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3256. }
  3257. }
  3258. }
  3259. catch(...)
  3260. {
  3261. if (!RootTempDir.IsEmpty())
  3262. {
  3263. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3264. }
  3265. throw;
  3266. }
  3267. }
  3268. __finally
  3269. {
  3270. FAutoOperation = false;
  3271. Terminal->ExceptionOnFail = false;
  3272. }
  3273. }
  3274. //---------------------------------------------------------------------------
  3275. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3276. {
  3277. // Do not waste time checking for default editor list the next time,
  3278. // or testing if default editor is not notepad.
  3279. bool TryNextTime = false;
  3280. UnicodeString UsageState;
  3281. if (!WinConfiguration->EditorList->IsDefaultList())
  3282. {
  3283. UsageState = "H";
  3284. }
  3285. else
  3286. {
  3287. UnicodeString Executable;
  3288. UnicodeString ExecutableDescription;
  3289. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3290. {
  3291. UnicodeString Message =
  3292. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3293. unsigned int Answer =
  3294. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3295. if (Answer != qaOK)
  3296. {
  3297. UsageState = "R";
  3298. }
  3299. else
  3300. {
  3301. UsageState = "A";
  3302. TEditorData EditorData;
  3303. EditorData.Editor = edExternal;
  3304. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3305. EditorData.ExternalEditorOptionsAutodetect();
  3306. TEditorList EditorList;
  3307. EditorList = *WinConfiguration->EditorList;
  3308. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3309. WinConfiguration->EditorList = &EditorList;
  3310. }
  3311. }
  3312. }
  3313. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3314. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3315. }
  3316. //---------------------------------------------------------------------------
  3317. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3318. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3319. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3320. {
  3321. if (ExecuteFileBy == efDefaultEditor)
  3322. {
  3323. if (!WinConfiguration->OfferedEditorAutoConfig)
  3324. {
  3325. EditorAutoConfig();
  3326. }
  3327. const TEditorPreferences * Editor =
  3328. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3329. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3330. {
  3331. ExecuteFileBy = efInternalEditor;
  3332. ExternalEditor = NULL;
  3333. }
  3334. else if (Editor->Data->Editor == edOpen)
  3335. {
  3336. ExecuteFileBy = efShell;
  3337. ExternalEditor = NULL;
  3338. }
  3339. else
  3340. {
  3341. ExecuteFileBy = efExternalEditor;
  3342. ExternalEditor = Editor->Data;
  3343. }
  3344. }
  3345. }
  3346. //---------------------------------------------------------------------------
  3347. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3348. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3349. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3350. {
  3351. UnicodeString OriginalFileName;
  3352. UnicodeString LocalRootDirectory;
  3353. UnicodeString RemoteDirectory;
  3354. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3355. IsSideLocalBrowser(Side), MaskParams);
  3356. UnicodeString Counter;
  3357. UnicodeString LocalFileName;
  3358. bool Handled = false;
  3359. if (!IsSideLocalBrowser(Side))
  3360. {
  3361. // We need to trim VMS version here, so that we use name without version
  3362. // when uploading back to create a new version of the file
  3363. OriginalFileName = Terminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3364. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3365. TObject * Token = NULL;
  3366. UnicodeString LocalDirectory;
  3367. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3368. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3369. LocalDirectory))
  3370. {
  3371. if (Token != NULL)
  3372. {
  3373. TForm * Form = dynamic_cast<TForm *>(Token);
  3374. if (Form->WindowState == wsMinimized)
  3375. {
  3376. ShowWindow(Form->Handle, SW_RESTORE);
  3377. }
  3378. else
  3379. {
  3380. Form->SetFocus();
  3381. }
  3382. Handled = true;
  3383. }
  3384. else
  3385. {
  3386. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3387. }
  3388. }
  3389. if (!Handled)
  3390. {
  3391. TStringList * FileList1 = new TStringList();
  3392. try
  3393. {
  3394. FileList1->AddObject(FullFileName, Object);
  3395. TemporarilyDownloadFiles(FileList1,
  3396. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3397. LocalRootDirectory, LocalDirectory, true, true);
  3398. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3399. }
  3400. __finally
  3401. {
  3402. delete FileList1;
  3403. }
  3404. switch (ExecuteFileBy)
  3405. {
  3406. case efShell:
  3407. Counter = "RemoteFilesExecuted";
  3408. break;
  3409. case efInternalEditor:
  3410. Counter = "RemoteFilesOpenedInInternalEditor";
  3411. break;
  3412. case efExternalEditor:
  3413. Counter = "RemoteFilesOpenedInExternalEditor";
  3414. break;
  3415. default:
  3416. DebugFail();
  3417. }
  3418. }
  3419. }
  3420. else
  3421. {
  3422. LocalFileName = FullFileName;
  3423. OriginalFileName = ExtractFileName(FullFileName);
  3424. switch (ExecuteFileBy)
  3425. {
  3426. case efShell:
  3427. Counter = "LocalFilesExecuted";
  3428. break;
  3429. case efInternalEditor:
  3430. Counter = "LocalFilesOpenedInInternalEditor";
  3431. break;
  3432. case efExternalEditor:
  3433. Counter = "LocalFilesOpenedInExternalEditor";
  3434. break;
  3435. default:
  3436. DebugFail();
  3437. }
  3438. }
  3439. if (!Handled)
  3440. {
  3441. Configuration->Usage->Inc(Counter);
  3442. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3443. ExternalEditor, LocalRootDirectory, RemoteDirectory, false);
  3444. }
  3445. }
  3446. //---------------------------------------------------------------------------
  3447. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3448. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3449. bool AllSelected, bool OnFocused)
  3450. {
  3451. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3452. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3453. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3454. Side = GetSide(Side);
  3455. TCustomDirView * DView = DirView(Side);
  3456. TStrings * FileList =
  3457. AllSelected ?
  3458. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3459. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3460. try
  3461. {
  3462. DebugAssert(AllSelected || (FileList->Count == 1));
  3463. for (int i = 0; i < FileList->Count; i++)
  3464. {
  3465. UnicodeString ListFileName = FileList->Strings[i];
  3466. UnicodeString FileNameOnly =
  3467. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3468. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3469. if (!DView->ItemIsDirectory(Item))
  3470. {
  3471. UnicodeString FullFileName;
  3472. if (!IsSideLocalBrowser(Side))
  3473. {
  3474. FullFileName = DirView(Side)->Path + ListFileName;
  3475. }
  3476. else
  3477. {
  3478. FullFileName = ListFileName;
  3479. }
  3480. TFileMasks::TParams MaskParams;
  3481. MaskParams.Size = DView->ItemFileSize(Item);
  3482. TDateTimePrecision Precision;
  3483. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3484. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3485. FileList->Objects[i], MaskParams);
  3486. }
  3487. }
  3488. }
  3489. __finally
  3490. {
  3491. delete FileList;
  3492. }
  3493. }
  3494. //---------------------------------------------------------------------------
  3495. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3496. {
  3497. CopyParam.FileNameCase = ncNoChange;
  3498. CopyParam.PreserveRights = false;
  3499. CopyParam.PreserveReadOnly = false;
  3500. CopyParam.ReplaceInvalidChars = true;
  3501. CopyParam.IncludeFileMask = TFileMasks();
  3502. CopyParam.NewerOnly = false;
  3503. CopyParam.FileMask = L"";
  3504. }
  3505. //---------------------------------------------------------------------------
  3506. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3507. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3508. {
  3509. TTerminalManager * Manager = TTerminalManager::Instance();
  3510. if (!IsActiveTerminal(Data->Terminal))
  3511. {
  3512. if (!NonVisualDataModule->Busy)
  3513. {
  3514. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3515. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3516. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3517. if (Data->Terminal == NULL)
  3518. {
  3519. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3520. if (SameSiteTerminal != NULL)
  3521. {
  3522. UnicodeString Message =
  3523. FMTLOAD(EDIT_SESSION_REATTACH,
  3524. (FileNameOnly, Data->SessionName, FileNameOnly));
  3525. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3526. {
  3527. Data->Terminal = Terminal;
  3528. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3529. Data->SessionName = Terminal->SessionData->SessionName;
  3530. // We might also overwrite session data
  3531. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3532. }
  3533. else
  3534. {
  3535. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3536. Abort();
  3537. }
  3538. }
  3539. }
  3540. // foreground session should reconnect itself
  3541. else if (Terminal != Data->Terminal)
  3542. {
  3543. UnicodeString Message =
  3544. MainInstructions(
  3545. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3546. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3547. {
  3548. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3549. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3550. }
  3551. else
  3552. {
  3553. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3554. Abort();
  3555. }
  3556. }
  3557. }
  3558. if (!IsActiveTerminal(Data->Terminal))
  3559. {
  3560. Configuration->Usage->Inc(L"EditInactiveSession");
  3561. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3562. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3563. (ExtractFileName(FileName), Data->SessionName)));
  3564. }
  3565. }
  3566. TStrings * FileList = new TStringList();
  3567. try
  3568. {
  3569. FileList->Add(FileName);
  3570. // consider using the same settings (preset) as when the file was downloaded
  3571. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3572. TemporaryFileCopyParam(CopyParam);
  3573. if (Data->ForceText)
  3574. {
  3575. CopyParam.TransferMode = tmAscii;
  3576. }
  3577. // so i do not need to worry if masking algorithm works in all cases
  3578. // ("" means "copy file name", no masking is actually done)
  3579. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3580. {
  3581. CopyParam.FileMask = L"";
  3582. }
  3583. else
  3584. {
  3585. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3586. }
  3587. DebugAssert(Data->Queue != NULL);
  3588. int Params = cpNoConfirmation | cpTemporary;
  3589. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3590. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3591. QueueItem->CompleteEvent = UploadCompleteEvent;
  3592. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3593. }
  3594. __finally
  3595. {
  3596. delete FileList;
  3597. }
  3598. }
  3599. //---------------------------------------------------------------------------
  3600. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3601. const UnicodeString FileName, const TEditedFileData * Data)
  3602. {
  3603. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3604. DebugAssert(!NonVisualDataModule->Busy);
  3605. if (!IsActiveTerminal(Data->Terminal))
  3606. {
  3607. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3608. (ExtractFileName(FileName), Data->SessionName)));
  3609. }
  3610. TManagedTerminal * PrevSession = TTerminalManager::Instance()->ActiveSession;
  3611. TTerminalManager::Instance()->ActiveSession = Data->Terminal;
  3612. NonVisualDataModule->StartBusy();
  3613. try
  3614. {
  3615. std::unique_ptr<TRemoteFile> File;
  3616. UnicodeString RemoteFileName =
  3617. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3618. Terminal->ExceptionOnFail = true;
  3619. try
  3620. {
  3621. TRemoteFile * AFile = NULL;
  3622. Terminal->ReadFile(RemoteFileName, AFile);
  3623. File.reset(AFile);
  3624. if (!File->HaveFullFileName)
  3625. {
  3626. File->FullFileName = RemoteFileName;
  3627. }
  3628. }
  3629. __finally
  3630. {
  3631. Terminal->ExceptionOnFail = false;
  3632. }
  3633. std::unique_ptr<TStrings> FileList(new TStringList());
  3634. FileList->AddObject(RemoteFileName, File.get());
  3635. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3636. UnicodeString TempDir = ExtractFilePath(FileName);
  3637. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir, TempDir, true, true);
  3638. // sanity check, the target file name should be still the same
  3639. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3640. }
  3641. __finally
  3642. {
  3643. NonVisualDataModule->EndBusy();
  3644. // it actually may not exist anymore...
  3645. TTerminalManager::Instance()->ActiveSession = PrevSession;
  3646. }
  3647. }
  3648. //---------------------------------------------------------------------------
  3649. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3650. const TEditedFileData * Data, bool & KeepOpen)
  3651. {
  3652. // Command is set for external editors only (not for "shell" open).
  3653. if (!Data->Command.IsEmpty())
  3654. {
  3655. bool AnyFound = false;
  3656. bool AnyMDI = false;
  3657. bool AnyNonMDI = false;
  3658. bool AnyDetect = false;
  3659. TEditorList * EditorList = new TEditorList();
  3660. try
  3661. {
  3662. *EditorList = *WinConfiguration->EditorList;
  3663. for (int i = 0; i < EditorList->Count; i++)
  3664. {
  3665. const TEditorPreferences * Editor = EditorList->Editors[i];
  3666. if ((Editor->Data->Editor == edExternal) &&
  3667. (Editor->Data->ExternalEditor == Data->Command))
  3668. {
  3669. AnyFound = true;
  3670. if (Editor->Data->SDIExternalEditor)
  3671. {
  3672. AnyNonMDI = true;
  3673. if (Editor->Data->DetectMDIExternalEditor)
  3674. {
  3675. AnyDetect = true;
  3676. }
  3677. }
  3678. else
  3679. {
  3680. AnyMDI = true;
  3681. }
  3682. }
  3683. }
  3684. bool EnableMDI = false;
  3685. bool DisableDetect = false;
  3686. if (AnyMDI)
  3687. {
  3688. KeepOpen = true;
  3689. if (AnyNonMDI)
  3690. {
  3691. // there is at least one instance of the editor with MDI support enabled,
  3692. // and one with disabled, enable it for all instances
  3693. EnableMDI = true;
  3694. }
  3695. }
  3696. else if (AnyFound && !AnyDetect)
  3697. {
  3698. // at least once instance found but all have MDI autodetection disabled
  3699. // => close the file (default action)
  3700. }
  3701. else
  3702. {
  3703. // no instance of the editor has MDI support enabled
  3704. TMessageParams Params;
  3705. if (AnyFound)
  3706. {
  3707. // there is at least one instance of the editor
  3708. Params.Params |= mpNeverAskAgainCheck;
  3709. }
  3710. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3711. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3712. switch (Answer)
  3713. {
  3714. case qaNeverAskAgain:
  3715. DisableDetect = true;
  3716. break;
  3717. case qaNo:
  3718. EnableMDI = true;
  3719. KeepOpen = true;
  3720. break;
  3721. }
  3722. }
  3723. if (AnyFound && (EnableMDI || DisableDetect))
  3724. {
  3725. bool Changed = false;
  3726. for (int i = 0; i < EditorList->Count; i++)
  3727. {
  3728. const TEditorPreferences * Editor = EditorList->Editors[i];
  3729. if ((Editor->Data->Editor == edExternal) &&
  3730. (Editor->Data->ExternalEditor == Data->Command) &&
  3731. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3732. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3733. {
  3734. Changed = true;
  3735. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3736. if (EnableMDI)
  3737. {
  3738. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3739. }
  3740. if (DisableDetect)
  3741. {
  3742. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3743. }
  3744. EditorList->Change(i, UpdatedEditor);
  3745. }
  3746. }
  3747. if (Changed)
  3748. {
  3749. WinConfiguration->EditorList = EditorList;
  3750. }
  3751. }
  3752. }
  3753. __finally
  3754. {
  3755. delete EditorList;
  3756. }
  3757. }
  3758. else
  3759. {
  3760. // "open" case
  3761. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3762. qaOK, HELP_APP_EARLY_CLOSED);
  3763. }
  3764. }
  3765. //---------------------------------------------------------------------------
  3766. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3767. {
  3768. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3769. }
  3770. //---------------------------------------------------------------------------
  3771. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3772. {
  3773. SideEnter(osRemote);
  3774. }
  3775. //---------------------------------------------------------------------------
  3776. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3777. {
  3778. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3779. SideEnter(osRemote);
  3780. }
  3781. //---------------------------------------------------------------------------
  3782. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3783. {
  3784. FCurrentSide = Side;
  3785. if (Visible)
  3786. {
  3787. UpdateControls();
  3788. }
  3789. }
  3790. //---------------------------------------------------------------------------
  3791. void __fastcall TCustomScpExplorerForm::FileDeleted(TOperationSide Side, const UnicodeString & FileName, bool Success)
  3792. {
  3793. VisualiseOperationFinished(Side, FileName, Success);
  3794. if (DebugAlwaysTrue(FDeletedFiles != NULL) && Success)
  3795. {
  3796. FDeletedFiles->Add(FileName);
  3797. }
  3798. }
  3799. //---------------------------------------------------------------------------
  3800. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3801. TStrings * FileList, bool Alternative)
  3802. {
  3803. TCustomDirView * DView = DirView(Side);
  3804. DView->SaveSelection();
  3805. DView->SaveSelectedNames();
  3806. DebugAssert(!FAlternativeDelete);
  3807. FAlternativeDelete = Alternative;
  3808. try
  3809. {
  3810. if (!IsSideLocalBrowser(Side))
  3811. {
  3812. DebugAssert(Terminal != NULL);
  3813. // Clone the file list as it may refer to current directory files, which get destroyed, when the directory is reloaded after the operation
  3814. std::unique_ptr<TStrings> PermanentFileList(TRemoteFileList::CloneStrings(FileList));
  3815. std::unique_ptr<TStringList> DeletedFiles(new TStringList());
  3816. DeletedFiles->CaseSensitive = DebugAlwaysTrue(!IsSideLocalBrowser(Side));
  3817. int Params = FLAGMASK(Alternative, dfAlternative);
  3818. try
  3819. {
  3820. TValueRestorer<TStrings *> DeletedFilesRestorer(FDeletedFiles);
  3821. DebugAssert(FDeletedFiles == NULL);
  3822. FDeletedFiles = DeletedFiles.get();
  3823. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  3824. FOnFileOperationFinished = FileDeleted;
  3825. FMoveToQueue = false;
  3826. Terminal->DeleteFiles(FileList, Params);
  3827. // Probably not needed for deleting, just for consistency with transfer code
  3828. if (FMoveToQueue)
  3829. {
  3830. Abort();
  3831. }
  3832. }
  3833. catch (EAbort &)
  3834. {
  3835. if (FMoveToQueue)
  3836. {
  3837. DeletedFiles->Sorted = true;
  3838. for (int Index = 0; Index < PermanentFileList->Count; Index++)
  3839. {
  3840. if (DeletedFiles->IndexOf(PermanentFileList->Strings[Index]) >= 0)
  3841. {
  3842. // We should always be deleting the first item => what can be used to optimize this code, if needed
  3843. DebugAssert(Index == 0);
  3844. PermanentFileList->Delete(Index);
  3845. Index--;
  3846. }
  3847. }
  3848. Configuration->Usage->Inc("MovesToBackgroundDelete");
  3849. TQueueItem * QueueItem = new TDeleteQueueItem(Terminal, PermanentFileList.get(), Params);
  3850. AddQueueItem(Queue, QueueItem, Terminal);
  3851. ClearOperationSelection(osRemote);
  3852. }
  3853. }
  3854. }
  3855. else
  3856. {
  3857. if (IsLocalBrowserMode())
  3858. {
  3859. Configuration->Usage->Inc(L"LocalLocalDeletes");
  3860. }
  3861. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  3862. FProgressSide = FCurrentSide;
  3863. try
  3864. {
  3865. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3866. }
  3867. __finally
  3868. {
  3869. ReloadLocalDirectory();
  3870. }
  3871. }
  3872. FAlternativeDelete = false;
  3873. }
  3874. catch(...)
  3875. {
  3876. FAlternativeDelete = false;
  3877. DView->DiscardSavedSelection();
  3878. throw;
  3879. }
  3880. DView->RestoreSelection();
  3881. }
  3882. //---------------------------------------------------------------------------
  3883. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3884. {
  3885. DebugAssert(Terminal);
  3886. DebugAssert(!IsLocalBrowserMode());
  3887. RemoteDirView->SaveSelection();
  3888. RemoteDirView->SaveSelectedNames();
  3889. try
  3890. {
  3891. if (Lock)
  3892. {
  3893. Terminal->LockFiles(FileList);
  3894. }
  3895. else
  3896. {
  3897. Terminal->UnlockFiles(FileList);
  3898. }
  3899. }
  3900. catch(...)
  3901. {
  3902. RemoteDirView->DiscardSavedSelection();
  3903. throw;
  3904. }
  3905. RemoteDirView->RestoreSelection();
  3906. }
  3907. //---------------------------------------------------------------------------
  3908. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  3909. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3910. bool NoConfirmation, bool Move)
  3911. {
  3912. DebugAssert(Terminal != NULL);
  3913. DebugAssert(Terminal == TTerminalManager::Instance()->ActiveTerminal);
  3914. DebugAssert(!IsLocalBrowserMode());
  3915. if (Session == NULL)
  3916. {
  3917. Session = Terminal;
  3918. }
  3919. if (Session == Terminal)
  3920. {
  3921. if (RemoteDriveView->DropTarget != NULL)
  3922. {
  3923. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3924. }
  3925. else if (RemoteDirView->DropTarget != NULL)
  3926. {
  3927. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3928. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3929. }
  3930. else
  3931. {
  3932. Target = RemoteDirView->Path;
  3933. }
  3934. }
  3935. else
  3936. {
  3937. Target = GetSessionPath(Session, osRemote);
  3938. }
  3939. Target = UnixIncludeTrailingBackslash(Target);
  3940. if (FileList->Count == 1)
  3941. {
  3942. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3943. }
  3944. else
  3945. {
  3946. FileMask = AnyMask;
  3947. }
  3948. DirectCopy = Terminal->IsCapable[fcRemoteCopy] || Terminal->IsCapable[fcSecondaryShell];
  3949. bool Result = true;
  3950. if (!NoConfirmation)
  3951. {
  3952. bool Multi = (FileList->Count > 1);
  3953. if (Move)
  3954. {
  3955. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3956. }
  3957. else
  3958. {
  3959. std::unique_ptr<TStrings> Sessions(new TStringList);
  3960. std::unique_ptr<TStrings> Directories(new TStringList);
  3961. TStrings * SessionList = TTerminalManager::Instance()->SessionList;
  3962. for (int Index = 0; Index < SessionList->Count; Index++)
  3963. {
  3964. TManagedTerminal * ASession = DebugNotNull(dynamic_cast<TManagedTerminal *>(SessionList->Objects[Index]));
  3965. if (IsActiveTerminal(ASession) && DebugAlwaysTrue(!ASession->LocalBrowser))
  3966. {
  3967. Sessions->AddObject(SessionList->Strings[Index], ASession);
  3968. Directories->Add(GetSessionPath(ASession, osRemote));
  3969. }
  3970. }
  3971. TDirectRemoteCopy AllowDirectCopy;
  3972. if (Terminal->IsCapable[fcRemoteCopy] || Terminal->CommandSessionOpened)
  3973. {
  3974. DebugAssert(DirectCopy);
  3975. AllowDirectCopy = drcAllow;
  3976. }
  3977. else if (Terminal->IsCapable[fcSecondaryShell])
  3978. {
  3979. DebugAssert(DirectCopy);
  3980. AllowDirectCopy = drcConfirmCommandSession;
  3981. }
  3982. else
  3983. {
  3984. DebugAssert(!DirectCopy);
  3985. AllowDirectCopy = drcDisallow;
  3986. }
  3987. void * ASession = Session;
  3988. Result = DoRemoteCopyDialog(
  3989. Sessions.get(), Directories.get(), AllowDirectCopy, Multi, ASession, Target, FileMask, DirectCopy, Terminal);
  3990. Session = static_cast<TManagedTerminal *>(ASession);
  3991. }
  3992. }
  3993. return Result;
  3994. }
  3995. //---------------------------------------------------------------------------
  3996. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3997. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  3998. {
  3999. DebugAssert(!IsLocalBrowserMode());
  4000. bool DirectCopy;
  4001. UnicodeString Target, FileMask;
  4002. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  4003. if (Result)
  4004. {
  4005. if (!Move && !DirectCopy)
  4006. {
  4007. Configuration->Usage->Inc("RemoteCopyTemp");
  4008. UnicodeString RootTempDir;
  4009. UnicodeString TempDir;
  4010. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false);
  4011. TStrings * TemporaryFilesList = new TStringList();
  4012. try
  4013. {
  4014. TMakeLocalFileListParams MakeFileListParam;
  4015. MakeFileListParam.FileList = TemporaryFilesList;
  4016. MakeFileListParam.FileTimes = NULL;
  4017. MakeFileListParam.IncludeDirs = true;
  4018. MakeFileListParam.Recursive = false;
  4019. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  4020. TTerminalManager::Instance()->ActiveSession = Session;
  4021. if (TemporaryFilesList->Count > 0)
  4022. {
  4023. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4024. CopyParam.FileMask = FileMask;
  4025. DebugAssert(!FAutoOperation);
  4026. FAutoOperation = true;
  4027. Terminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  4028. }
  4029. }
  4030. __finally
  4031. {
  4032. delete TemporaryFilesList;
  4033. FAutoOperation = false;
  4034. if (!RootTempDir.IsEmpty())
  4035. {
  4036. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  4037. }
  4038. }
  4039. }
  4040. else
  4041. {
  4042. RemoteDirView->SaveSelection();
  4043. RemoteDirView->SaveSelectedNames();
  4044. try
  4045. {
  4046. if (Move)
  4047. {
  4048. Configuration->Usage->Inc("RemoteMove");
  4049. Terminal->MoveFiles(FileList, Target, FileMask);
  4050. }
  4051. else
  4052. {
  4053. Configuration->Usage->Inc("RemoteCopyDirect");
  4054. DebugAssert(DirectCopy);
  4055. DebugAssert(Session == Terminal);
  4056. if (Terminal->IsCapable[fcRemoteCopy] ||
  4057. Terminal->CommandSessionOpened ||
  4058. CommandSessionFallback())
  4059. {
  4060. Terminal->CopyFiles(FileList, Target, FileMask);
  4061. }
  4062. }
  4063. }
  4064. catch(...)
  4065. {
  4066. RemoteDirView->DiscardSavedSelection();
  4067. throw;
  4068. }
  4069. RemoteDirView->RestoreSelection();
  4070. }
  4071. }
  4072. return Result;
  4073. }
  4074. //---------------------------------------------------------------------------
  4075. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  4076. const UnicodeString & Path, TRemoteProperties & Properties)
  4077. {
  4078. DebugAssert(!IsLocalBrowserMode());
  4079. Properties.Valid = Properties.Valid << vpEncrypt;
  4080. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  4081. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  4082. }
  4083. //---------------------------------------------------------------------------
  4084. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  4085. {
  4086. Side = GetSide(Side);
  4087. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  4088. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  4089. UnicodeString Name = LoadStr(NEW_FOLDER);
  4090. int AllowedChanges =
  4091. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  4092. bool SaveSettings = false;
  4093. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  4094. {
  4095. TWindowLock Lock(this);
  4096. if (!IsSideLocalBrowser(Side))
  4097. {
  4098. if (SaveSettings)
  4099. {
  4100. GUIConfiguration->NewDirectoryProperties = Properties;
  4101. }
  4102. CreateRemoteDirectory(Name, Properties);
  4103. }
  4104. else
  4105. {
  4106. DirView(Side)->CreateDirectory(Name);
  4107. }
  4108. }
  4109. }
  4110. //---------------------------------------------------------------------------
  4111. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  4112. {
  4113. TWindowLock Lock(this);
  4114. DirView(Side)->ExecuteHomeDirectory();
  4115. }
  4116. //---------------------------------------------------------------------------
  4117. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  4118. {
  4119. DoOpenDirectoryDialog(odBrowse, Side);
  4120. }
  4121. //---------------------------------------------------------------------------
  4122. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  4123. {
  4124. TOperationSide BookmarkSide = IsLocalBrowserMode() ? osLocal : Side;
  4125. UnicodeString Path = Bookmark->GetSideDirectory(BookmarkSide);
  4126. bool Result = !Path.IsEmpty();
  4127. if (Result)
  4128. {
  4129. // While we might get here when the session is closed (from location profiles),
  4130. // it's not a problem as the Path setter is noop then
  4131. DirView(Side)->Path = Path;
  4132. }
  4133. return Result;
  4134. }
  4135. //---------------------------------------------------------------------------
  4136. void __fastcall TCustomScpExplorerForm::CalculateSize(
  4137. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  4138. bool & Close)
  4139. {
  4140. // terminal can be already closed (e.g. dropped connection)
  4141. if (Terminal != NULL)
  4142. {
  4143. try
  4144. {
  4145. TCalculateSizeParams Params;
  4146. Params.Stats = &Stats;
  4147. Terminal->CalculateFilesSize(FileList, Size, Params);
  4148. }
  4149. catch(...)
  4150. {
  4151. if (!Terminal->Active)
  4152. {
  4153. Close = true;
  4154. }
  4155. throw;
  4156. }
  4157. }
  4158. }
  4159. //---------------------------------------------------------------------------
  4160. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  4161. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  4162. bool & Close)
  4163. {
  4164. // terminal can be already closed (e.g. dropped connection)
  4165. if (Terminal != NULL)
  4166. {
  4167. Configuration->Usage->Inc(L"ChecksumCalculated");
  4168. try
  4169. {
  4170. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  4171. }
  4172. catch(...)
  4173. {
  4174. if (!Terminal->Active)
  4175. {
  4176. Close = true;
  4177. }
  4178. throw;
  4179. }
  4180. }
  4181. }
  4182. //---------------------------------------------------------------------------
  4183. void TCustomScpExplorerForm::LoadFilesProperties(TStrings * FileList)
  4184. {
  4185. if (Terminal->LoadFilesProperties(FileList))
  4186. {
  4187. RemoteDirView->Invalidate();
  4188. }
  4189. }
  4190. //---------------------------------------------------------------------------
  4191. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  4192. {
  4193. bool Result;
  4194. if (!IsSideLocalBrowser(Side))
  4195. {
  4196. TRemoteTokenList * GroupList = NULL;
  4197. TRemoteTokenList * UserList = NULL;
  4198. try
  4199. {
  4200. TRemoteProperties CurrentProperties;
  4201. LoadFilesProperties(FileList);
  4202. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4203. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4204. if (CapableGroupChanging || CapableOwnerChanging)
  4205. {
  4206. if (CapableGroupChanging)
  4207. {
  4208. GroupList = Terminal->Groups->Duplicate();
  4209. }
  4210. if (CapableOwnerChanging)
  4211. {
  4212. UserList = Terminal->Users->Duplicate();
  4213. }
  4214. TRemoteDirectory * Files = Terminal->Files;
  4215. int Count = Files->Count;
  4216. if (Count > 100)
  4217. {
  4218. Count = 100;
  4219. }
  4220. for (int Index = 0; Index < Count; Index++)
  4221. {
  4222. TRemoteFile * File = Files->Files[Index];
  4223. if (CapableGroupChanging)
  4224. {
  4225. GroupList->AddUnique(File->Group);
  4226. }
  4227. if (CapableOwnerChanging)
  4228. {
  4229. UserList->AddUnique(File->Owner);
  4230. }
  4231. }
  4232. // if we haven't collected tokens for all files in current directory,
  4233. // make sure we collect them at least for all selected files.
  4234. // (note that so far the files in FileList has to be from current direcotry)
  4235. if (Count < Files->Count)
  4236. {
  4237. for (int Index = 0; Index < FileList->Count; Index++)
  4238. {
  4239. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4240. if (CapableGroupChanging)
  4241. {
  4242. GroupList->AddUnique(File->Group);
  4243. }
  4244. if (CapableOwnerChanging)
  4245. {
  4246. UserList->AddUnique(File->Owner);
  4247. }
  4248. }
  4249. }
  4250. }
  4251. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4252. bool CapableAclChanging = Terminal->IsCapable[fcAclChangingFiles];
  4253. for (int Index = 0; (Index < FileList->Count) && CapableAclChanging; Index++)
  4254. {
  4255. if (dynamic_cast<TRemoteFile *>(FileList->Objects[Index])->IsDirectory)
  4256. {
  4257. CapableAclChanging = false;
  4258. CurrentProperties.Valid = CurrentProperties.Valid >> vpRights;
  4259. }
  4260. }
  4261. int Flags = 0;
  4262. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4263. if (CapableAclChanging) Flags |= cpAcl;
  4264. if (CapableOwnerChanging) Flags |= cpOwner;
  4265. if (CapableGroupChanging) Flags |= cpGroup;
  4266. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4267. if (Terminal->IsCapable[fcCalculatingChecksum])
  4268. {
  4269. CalculateChecksumEvent = CalculateChecksum;
  4270. }
  4271. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4272. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4273. TRemoteProperties NewProperties = CurrentProperties;
  4274. Result =
  4275. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4276. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4277. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4278. CalculateSize, CalculateChecksumEvent);
  4279. if (Result)
  4280. {
  4281. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4282. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4283. }
  4284. }
  4285. __finally
  4286. {
  4287. delete GroupList;
  4288. delete UserList;
  4289. }
  4290. }
  4291. else
  4292. {
  4293. DirView(Side)->DisplayPropertiesMenu();
  4294. Result = true;
  4295. }
  4296. return Result;
  4297. }
  4298. //---------------------------------------------------------------------------
  4299. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4300. {
  4301. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4302. {
  4303. FIgnoreNextDialogChar = Key;
  4304. }
  4305. Key = 0;
  4306. }
  4307. //---------------------------------------------------------------------------
  4308. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4309. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4310. {
  4311. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4312. if (Command != NULL)
  4313. {
  4314. KeyProcessed(Key, Shift);
  4315. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4316. {
  4317. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4318. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4319. false, false, const_cast<TCustomCommandType *>(Command));
  4320. }
  4321. }
  4322. }
  4323. //---------------------------------------------------------------------------
  4324. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4325. {
  4326. if (!NonVisualDataModule->Busy)
  4327. {
  4328. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4329. {
  4330. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4331. }
  4332. if ((Key != 0) && !DirView(osCurrent)->IsEditing())
  4333. {
  4334. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4335. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4336. {
  4337. // Note that handling shortcuts on panel (current control) popup menu has precendence over this.
  4338. // But so far it's not a problem, as it does what we want.
  4339. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4340. if (((Action->ShortCut == KeyShortCut) ||
  4341. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4342. AllowedAction(Action, aaShortCut))
  4343. {
  4344. // Has to be called before the action as the dialog char is already in queue.
  4345. // So when the action consumes message queue, we already need to have the
  4346. // FIgnoreNextDialogChar set
  4347. KeyProcessed(Key, Shift);
  4348. // Reset reference to previous component (when menu/toolbar was clicked).
  4349. // Needed to detect that action was invoked by keyboard shortcut
  4350. // in TNonVisualDataModule::ExplorerActionsExecute
  4351. Action->ActionComponent = NULL;
  4352. Action->Execute();
  4353. return;
  4354. }
  4355. }
  4356. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4357. {
  4358. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4359. {
  4360. KeyProcessed(Key, Shift);
  4361. TTerminalManager::Instance()->ActiveSessionIndex = i;
  4362. return;
  4363. }
  4364. }
  4365. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4366. {
  4367. KeyProcessed(Key, Shift);
  4368. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4369. }
  4370. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4371. if (IsCustomShortCut(CustomShortCut))
  4372. {
  4373. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4374. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4375. if (WinConfiguration->SharedBookmarks != NULL)
  4376. {
  4377. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4378. if ((Bookmark != NULL) &&
  4379. OpenBookmark(FCurrentSide, Bookmark))
  4380. {
  4381. KeyProcessed(Key, Shift);
  4382. }
  4383. }
  4384. }
  4385. }
  4386. }
  4387. TForm::KeyDown(Key, Shift);
  4388. }
  4389. //---------------------------------------------------------------------------
  4390. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4391. {
  4392. bool OpenedTerminal = false;
  4393. UnicodeString ProtocolName;
  4394. UnicodeString ProtocolNameHint;
  4395. UnicodeString SecurityProtocolName;
  4396. UnicodeString DurationHint;
  4397. // See the comment in TScpCommanderForm::UpdateControls()
  4398. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4399. if (ActiveTerminal != NULL)
  4400. {
  4401. // Active is not enough here
  4402. if (ActiveTerminal->Status == ssOpened)
  4403. {
  4404. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4405. const TFileSystemInfo & FileSystemInfo = ActiveTerminal->GetFileSystemInfo();
  4406. if (FileSystemInfo.ProtocolName.IsEmpty())
  4407. {
  4408. ProtocolName = SessionInfo.ProtocolName;
  4409. }
  4410. else
  4411. {
  4412. ProtocolName = FileSystemInfo.ProtocolName;
  4413. }
  4414. ProtocolNameHint = LoadStr(STATUS_PROTOCOL_HINT);
  4415. SecurityProtocolName = SessionInfo.SecurityProtocolName;
  4416. DurationHint = LoadStr(STATUS_DURATION_HINT);
  4417. OpenedTerminal = true;
  4418. }
  4419. else if (ActiveTerminal->Status > ssClosed)
  4420. {
  4421. ProtocolName = LoadStr(STATUS_CONNECTING);
  4422. }
  4423. else
  4424. {
  4425. ProtocolName = LoadStr(STATUS_NOT_CONNECTED2);
  4426. }
  4427. }
  4428. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4429. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4430. TTBXStatusPanel * ProtocolStatusPanel = SessionStatusBar->Panels->Items[Offset + 0];
  4431. ProtocolStatusPanel->ViewPriority = (ActiveTerminal != NULL) ? 100 : 0;
  4432. if (ProtocolStatusPanel->Caption != ProtocolName)
  4433. {
  4434. ProtocolStatusPanel->Caption = ProtocolName;
  4435. std::unique_ptr<TCanvas> Canvas(CreateControlCanvas(SessionStatusBar));
  4436. ProtocolStatusPanel->Size = Canvas->TextWidth(ProtocolName) + ScaleByTextHeight(SessionStatusBar, 32);
  4437. }
  4438. ProtocolStatusPanel->Hint = ProtocolNameHint;
  4439. TTBXStatusPanel * SecurityStatusPanel = SessionStatusBar->Panels->Items[Offset + 1];
  4440. SecurityStatusPanel->ViewPriority = OpenedTerminal ? 98 : 0;
  4441. bool SecurityEnabled = !SecurityProtocolName.IsEmpty();
  4442. SecurityStatusPanel->Enabled = SecurityEnabled;
  4443. UnicodeString SecurityProtocolHint;
  4444. if (SecurityEnabled)
  4445. {
  4446. SecurityProtocolHint = FMTLOAD(STATUS_SECURE, (SecurityProtocolName));
  4447. }
  4448. else
  4449. {
  4450. SecurityProtocolHint = LoadStr(STATUS_INSECURE);
  4451. }
  4452. SecurityStatusPanel->Hint = SecurityProtocolHint;
  4453. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[Offset + 2];
  4454. DurationStatusPanel->ViewPriority = OpenedTerminal ? 99 : 0;
  4455. DurationStatusPanel->Hint = DurationHint;
  4456. UpdateStatusBar();
  4457. }
  4458. //---------------------------------------------------------------------------
  4459. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4460. {
  4461. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4462. DebugAssert(SessionStatusBar != NULL);
  4463. TTBXStatusPanel * NoteStatusPanel = SessionStatusBar->Panels->Items[0];
  4464. if (!FNote.IsEmpty())
  4465. {
  4466. NoteStatusPanel->Caption = FNote;
  4467. }
  4468. else
  4469. {
  4470. UpdateStatusPanelText(NoteStatusPanel);
  4471. }
  4472. NoteStatusPanel->Hint = FNoteHints;
  4473. UnicodeString LoginTime;
  4474. // See the comment in TScpCommanderForm::UpdateControls()
  4475. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4476. // Active is not enough here
  4477. if ((ActiveTerminal != NULL) && (ActiveTerminal->Status == ssOpened))
  4478. {
  4479. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4480. LoginTime = FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4481. }
  4482. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1];
  4483. DurationStatusPanel->Caption = LoginTime;
  4484. }
  4485. //---------------------------------------------------------------------------
  4486. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4487. {
  4488. Panel->Caption = L"";
  4489. }
  4490. //---------------------------------------------------------------------------
  4491. void __fastcall TCustomScpExplorerForm::Idle()
  4492. {
  4493. if (FShowing ||
  4494. // Particularly for
  4495. // - Standalone editing
  4496. // - Detecting closed connection and automatically reconnect it while waiting for changes
  4497. // while "keeping remote directory up to date".
  4498. StandaloneOperation)
  4499. {
  4500. FEditorManager->Check();
  4501. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4502. // that needs to know if queue view is visible (and it may be closed after queue update)
  4503. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4504. }
  4505. if (FShowing)
  4506. {
  4507. if (!NonVisualDataModule->Busy &&
  4508. // Menu is opened or drag&drop is going on
  4509. (Mouse->Capture == NULL))
  4510. {
  4511. if (FRefreshRemoteDirectory)
  4512. {
  4513. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4514. {
  4515. Terminal->RefreshDirectory();
  4516. }
  4517. FRefreshRemoteDirectory = false;
  4518. }
  4519. if (FRefreshLocalDirectory)
  4520. {
  4521. ReloadLocalDirectory();
  4522. FRefreshLocalDirectory = false;
  4523. }
  4524. if (WinConfiguration->RefreshRemotePanel)
  4525. {
  4526. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4527. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4528. {
  4529. RemoteDirView->ReloadDirectory();
  4530. }
  4531. }
  4532. }
  4533. }
  4534. if (FShowing || StandaloneOperation)
  4535. {
  4536. if (FQueueStatusInvalidated)
  4537. {
  4538. UpdateQueueStatus(false);
  4539. }
  4540. RefreshQueueItems();
  4541. }
  4542. if (FShowing)
  4543. {
  4544. UpdateStatusBar();
  4545. }
  4546. if (FClipboardFakeMonitorsPendingReset && !FDownloadingFromClipboard)
  4547. {
  4548. FClipboardFakeMonitorsPendingReset = false;
  4549. FClipboardFakeMonitors.reset(NULL);
  4550. }
  4551. FIgnoreNextDialogChar = 0;
  4552. }
  4553. //---------------------------------------------------------------------------
  4554. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4555. {
  4556. try
  4557. {
  4558. FUserActionTimer->Enabled = false;
  4559. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4560. {
  4561. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4562. {
  4563. FPendingQueueActionItem->ProcessUserAction();
  4564. }
  4565. }
  4566. }
  4567. __finally
  4568. {
  4569. FPendingQueueActionItem = NULL;
  4570. }
  4571. }
  4572. //---------------------------------------------------------------------------
  4573. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4574. {
  4575. if (WinConfiguration->MinimizeToTray)
  4576. {
  4577. UpdateTrayIcon();
  4578. FTrayIcon->Visible = true;
  4579. if (Visible)
  4580. {
  4581. ShowWindow(Handle, SW_HIDE);
  4582. }
  4583. }
  4584. }
  4585. //---------------------------------------------------------------------------
  4586. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4587. {
  4588. // WORKAROUND
  4589. // When restoring maximized window from minimization,
  4590. // rarely some controls do not align properly.
  4591. // Two instances seen (both for Commander):
  4592. // - When restoring, window is temporarily narrower (not maximized),
  4593. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4594. // Once maximized already, top dock shinks back, but the session PageControl,
  4595. // do not align up, leaving space between TopDock and PageControl.
  4596. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4597. for (int Index = 0; Index < ControlCount; Index++)
  4598. {
  4599. RealignControl(Controls[Index]);
  4600. }
  4601. if (FTrayIcon->Visible)
  4602. {
  4603. FTrayIcon->Visible = false;
  4604. if (Visible)
  4605. {
  4606. ShowWindow(Handle, SW_SHOW);
  4607. }
  4608. }
  4609. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4610. {
  4611. FNeedSession = false;
  4612. NonVisualDataModule->StartBusy();
  4613. try
  4614. {
  4615. NeedSession(true);
  4616. }
  4617. __finally
  4618. {
  4619. NonVisualDataModule->EndBusy();
  4620. }
  4621. }
  4622. }
  4623. //---------------------------------------------------------------------------
  4624. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4625. {
  4626. FTrayIcon->Hint = GetMainForm()->Caption;
  4627. }
  4628. //---------------------------------------------------------------------------
  4629. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4630. {
  4631. UpdateTrayIcon();
  4632. }
  4633. //---------------------------------------------------------------------------
  4634. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4635. {
  4636. // workaround
  4637. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4638. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4639. // But WM_ACTIVATEAPP message can be received even when
  4640. // the main window is minimized to the tray and internal editor window is focused
  4641. // (after another application was previously active)
  4642. if (::IsIconic(Handle))
  4643. {
  4644. if (!IsAppIconic())
  4645. {
  4646. SetAppIconic(true);
  4647. }
  4648. }
  4649. ::ApplicationRestore();
  4650. Application->BringToFront();
  4651. }
  4652. //---------------------------------------------------------------------------
  4653. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4654. {
  4655. RestoreApp();
  4656. }
  4657. //---------------------------------------------------------------------------
  4658. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  4659. {
  4660. if (OpenInNewWindow())
  4661. {
  4662. ExecuteNewInstance(SessionUrl);
  4663. }
  4664. else
  4665. {
  4666. TTerminalManager::Instance()->NewSession(SessionUrl);
  4667. }
  4668. }
  4669. //---------------------------------------------------------------------------
  4670. void TCustomScpExplorerForm::NewTab(TOperationSide)
  4671. {
  4672. NewSession();
  4673. }
  4674. //---------------------------------------------------------------------------
  4675. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4676. {
  4677. UnicodeString SessionName = StoredSessions->HiddenPrefix + ManagedSession->SessionData->SessionName;
  4678. StoredSessions->NewSession(SessionName, SessionData);
  4679. // modified only, explicit
  4680. StoredSessions->Save(false, true);
  4681. // encode session name because of slashes in hierarchical sessions
  4682. return EncodeUrlString(SessionName);
  4683. }
  4684. //---------------------------------------------------------------------------
  4685. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4686. {
  4687. // current working directories become defaults here, what is not right
  4688. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4689. return SaveHiddenDuplicateSession(SessionData.get());
  4690. }
  4691. //---------------------------------------------------------------------------
  4692. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4693. {
  4694. if (OpenInNewWindow())
  4695. {
  4696. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4697. }
  4698. else
  4699. {
  4700. // current working directories become defaults here, what is not right
  4701. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4702. TTerminalManager * Manager = TTerminalManager::Instance();
  4703. TManagedTerminal * ASession = Manager->NewManagedTerminal(SessionData.get());
  4704. DebugAssert(ASession->LocalBrowser == ManagedSession->LocalBrowser);
  4705. // We definitelly want these
  4706. ASession->Disconnected = ManagedSession->Disconnected;
  4707. // Not sure about these two
  4708. ASession->Permanent = ManagedSession->Permanent;
  4709. ASession->DisconnectedTemporarily = ManagedSession->DisconnectedTemporarily;
  4710. Manager->ActiveSession = ASession;
  4711. }
  4712. }
  4713. //---------------------------------------------------------------------------
  4714. void __fastcall TCustomScpExplorerForm::RenameSession()
  4715. {
  4716. UnicodeString Name = ManagedSession->SessionData->SessionName;
  4717. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME) &&
  4718. // When submitting the default name, do not fix it
  4719. (Name != ManagedSession->SessionData->SessionName))
  4720. {
  4721. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4722. TSessionData::ValidateName(Name);
  4723. // This is inconsistent with how color (for example) is handled.
  4724. ManagedSession->SessionData->Name = Name;
  4725. UpdateControls();
  4726. // Add/Remove distinguishing paths from sessions of the same name.
  4727. SessionListChanged();
  4728. }
  4729. }
  4730. //---------------------------------------------------------------------------
  4731. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4732. {
  4733. DebugAssert(Queue != NULL);
  4734. bool Result = Queue->IsEmpty;
  4735. if (!Result)
  4736. {
  4737. SetFocus();
  4738. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4739. }
  4740. return Result;
  4741. }
  4742. //---------------------------------------------------------------------------
  4743. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4744. {
  4745. return CanCloseQueue(FQueue);
  4746. }
  4747. //---------------------------------------------------------------------------
  4748. void __fastcall TCustomScpExplorerForm::CloseSession()
  4749. {
  4750. if (CanCloseQueue())
  4751. {
  4752. TTerminalManager::Instance()->FreeActiveTerminal();
  4753. }
  4754. }
  4755. //---------------------------------------------------------------------------
  4756. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4757. {
  4758. if (CanCloseQueue())
  4759. {
  4760. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4761. }
  4762. }
  4763. //---------------------------------------------------------------------------
  4764. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4765. {
  4766. DetachTerminal(Terminal);
  4767. RemoteDirView->Terminal = Terminal;
  4768. UpdateRemotePathComboBox(false);
  4769. InitStatusBar();
  4770. UpdateControls();
  4771. }
  4772. //---------------------------------------------------------------------------
  4773. void __fastcall TCustomScpExplorerForm::TerminalConnecting()
  4774. {
  4775. InitStatusBar();
  4776. }
  4777. //---------------------------------------------------------------------------
  4778. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4779. {
  4780. if (DebugAlwaysTrue(!Terminal->Active))
  4781. {
  4782. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4783. }
  4784. }
  4785. //---------------------------------------------------------------------------
  4786. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4787. {
  4788. if (OpenInNewWindow())
  4789. {
  4790. // encode session name because of slashes in hierarchical sessions
  4791. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4792. }
  4793. else
  4794. {
  4795. TTerminalManager * Manager = TTerminalManager::Instance();
  4796. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4797. Manager->ActiveSession = Terminal;
  4798. }
  4799. }
  4800. //---------------------------------------------------------------------------
  4801. void TCustomScpExplorerForm::DoOpenFolderOrWorkspace(
  4802. const UnicodeString & Name, bool ConnectFirstTerminal, bool CheckMaxSessions)
  4803. {
  4804. TTerminalManager * Manager = TTerminalManager::Instance();
  4805. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4806. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4807. if (!CheckMaxSessions || (DataList->Count <= WinConfiguration->MaxSessions))
  4808. {
  4809. TManagedTerminal * FirstSession = Manager->NewSessions(DataList.get());
  4810. // FirstSession can be null, if some of the
  4811. if (!ConnectFirstTerminal && (FirstSession != NULL))
  4812. {
  4813. FirstSession->Disconnected = true;
  4814. FirstSession->DisconnectedTemporarily = true;
  4815. }
  4816. Manager->ActiveSession = FirstSession;
  4817. }
  4818. }
  4819. //---------------------------------------------------------------------------
  4820. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4821. {
  4822. if (OpenInNewWindow())
  4823. {
  4824. ExecuteNewInstance(Name);
  4825. }
  4826. else
  4827. {
  4828. DoOpenFolderOrWorkspace(Name, true, false);
  4829. }
  4830. }
  4831. //---------------------------------------------------------------------------
  4832. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4833. bool &CanClose)
  4834. {
  4835. // See the comment in CloseApp()
  4836. if (NonVisualDataModule->Busy)
  4837. {
  4838. CanClose = false;
  4839. }
  4840. else
  4841. {
  4842. TTerminalManager * Manager = TTerminalManager::Instance();
  4843. int ActiveSessions = 0;
  4844. for (int Index = 0; Index < Manager->Count; Index++)
  4845. {
  4846. if (Manager->Sessions[Index]->Active)
  4847. {
  4848. ActiveSessions++;
  4849. }
  4850. }
  4851. // Confirm, when there's at least one active remote session or
  4852. // when there are multiple (inactive or local) sessions and auto workspace saving is not enabled.
  4853. if (((ActiveSessions > 0) || ((Manager->Count > 1) && !WinConfiguration->AutoSaveWorkspace)) &&
  4854. WinConfiguration->ConfirmClosingSession)
  4855. {
  4856. unsigned int Result;
  4857. TMessageParams Params(mpNeverAskAgainCheck);
  4858. UnicodeString Message;
  4859. int Answers = qaOK | qaCancel;
  4860. // The current session is the only active session
  4861. if ((ActiveSessions == 1) && (Terminal != NULL) && Terminal->Active)
  4862. {
  4863. if (!WinConfiguration->AutoSaveWorkspace)
  4864. {
  4865. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  4866. }
  4867. else
  4868. {
  4869. Message = LoadStr(CLOSE_SESSION);
  4870. }
  4871. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  4872. }
  4873. // Multiple active sessions or one active session, but it's not the current one
  4874. else if (ActiveSessions > 0)
  4875. {
  4876. if (!WinConfiguration->AutoSaveWorkspace)
  4877. {
  4878. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  4879. }
  4880. else
  4881. {
  4882. Message = LoadStr(CLOSE_SESSIONS);
  4883. }
  4884. }
  4885. // Multiple tabs, but all are inactive or local, and saving of worspace is not enabled
  4886. else
  4887. {
  4888. DebugAlwaysTrue(Manager->Count > 1);
  4889. DebugAlwaysTrue(!WinConfiguration->AutoSaveWorkspace);
  4890. Message = LoadStr(CLOSE_WORKSPACE);
  4891. }
  4892. UnicodeString Note;
  4893. if (WinConfiguration->AutoSaveWorkspace)
  4894. {
  4895. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  4896. }
  4897. else
  4898. {
  4899. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  4900. Answers = qaYes | qaNo | qaCancel;
  4901. }
  4902. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  4903. SetFocus();
  4904. Result = MessageDialog(Message, qtConfirmation,
  4905. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  4906. if (Result == qaNeverAskAgain)
  4907. {
  4908. WinConfiguration->ConfirmClosingSession = false;
  4909. }
  4910. if (Result == qaNo)
  4911. {
  4912. CanClose = SaveWorkspace(true);
  4913. // note that the workspace will be saved redundatly again from FormClose
  4914. }
  4915. else
  4916. {
  4917. CanClose =
  4918. (Result == qaOK) ||
  4919. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4920. (Result == qaNeverAskAgain);
  4921. }
  4922. }
  4923. if (CanClose && HasActiveTerminal())
  4924. {
  4925. CanClose = CanCloseQueue();
  4926. }
  4927. }
  4928. if (CanClose)
  4929. {
  4930. CanClose =
  4931. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4932. FEditorManager->CloseExternalFilesWithoutProcess();
  4933. if (CanClose)
  4934. {
  4935. while (CanClose && (FLocalEditors->Count > 0))
  4936. {
  4937. int PrevCount = FLocalEditors->Count;
  4938. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4939. CanClose = (FLocalEditors->Count < PrevCount);
  4940. }
  4941. if (CanClose)
  4942. {
  4943. CanClose = FEditorManager->Empty(true);
  4944. if (!CanClose)
  4945. {
  4946. SetFocus();
  4947. CanClose =
  4948. (MessageDialog(
  4949. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4950. }
  4951. }
  4952. }
  4953. }
  4954. }
  4955. //---------------------------------------------------------------------------
  4956. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4957. {
  4958. TForm * Form = dynamic_cast<TForm *>(Sender);
  4959. DebugAssert(Form != NULL);
  4960. Form->Close();
  4961. }
  4962. //---------------------------------------------------------------------------
  4963. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4964. {
  4965. TForm * Form = dynamic_cast<TForm *>(Sender);
  4966. delete Form;
  4967. }
  4968. //---------------------------------------------------------------------------
  4969. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4970. {
  4971. while (FLocalEditors->Count > 0)
  4972. {
  4973. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4974. }
  4975. }
  4976. //---------------------------------------------------------------------------
  4977. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4978. TObject *Sender)
  4979. {
  4980. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4981. try
  4982. {
  4983. SetProperties(osRemote, FileList);
  4984. }
  4985. __finally
  4986. {
  4987. delete FileList;
  4988. }
  4989. }
  4990. //---------------------------------------------------------------------------
  4991. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4992. {
  4993. if (value)
  4994. {
  4995. if (Component == fcCustomCommandsBand)
  4996. {
  4997. UpdateCustomCommandsToolbar();
  4998. }
  4999. if (Component == fcQueueFileList)
  5000. {
  5001. DebugAssert(!QueueFileListSplitter->Visible);
  5002. AdjustQueueLayout();
  5003. UpdateQueueFileList();
  5004. }
  5005. }
  5006. }
  5007. //---------------------------------------------------------------------------
  5008. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  5009. {
  5010. int MinHeightLimit =
  5011. GetStaticQueuePanelComponentsHeight() +
  5012. QueueFileListSplitter->Height +
  5013. QueueFileList->Height +
  5014. GetMinQueueViewHeight();
  5015. if (QueuePanel->ClientHeight < MinHeightLimit)
  5016. {
  5017. QueuePanel->ClientHeight = MinHeightLimit;
  5018. }
  5019. }
  5020. //---------------------------------------------------------------------------
  5021. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  5022. {
  5023. TControl * Control = GetComponent(Component);
  5024. DebugAssert(Control);
  5025. bool Changed = (Control->Visible != value);
  5026. if (Changed)
  5027. {
  5028. ComponentShowing(Component, value);
  5029. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  5030. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  5031. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  5032. if (value)
  5033. {
  5034. int RemainingHeight = Control->Parent->ClientHeight;
  5035. int RemainingWidth = Control->Parent->ClientWidth;
  5036. for (int i = 0; i < Control->Parent->ControlCount; i++)
  5037. {
  5038. TControl * ChildControl = Control->Parent->Controls[i];
  5039. if (ChildControl->Visible)
  5040. {
  5041. switch (ChildControl->Align)
  5042. {
  5043. case alTop:
  5044. case alBottom:
  5045. RemainingHeight -= ChildControl->Height;
  5046. break;
  5047. case alLeft:
  5048. case alRight:
  5049. RemainingWidth -= ChildControl->Width;
  5050. break;
  5051. }
  5052. }
  5053. }
  5054. int Reserve = ScaleByTextHeight(this, 32);
  5055. // queue in explorer, trees in commander
  5056. if (Control->Height > RemainingHeight - Reserve)
  5057. {
  5058. Control->Height = RemainingHeight / 2;
  5059. }
  5060. if (Control->Width > RemainingWidth - Reserve)
  5061. {
  5062. Control->Width = RemainingWidth / 2;
  5063. }
  5064. }
  5065. Control->Visible = value;
  5066. if (WasFocused && Visible)
  5067. {
  5068. DirView(osCurrent)->SetFocus();
  5069. }
  5070. FixControlsPlacement();
  5071. }
  5072. }
  5073. //---------------------------------------------------------------------------
  5074. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  5075. {
  5076. TControl * Control = GetComponent(Component);
  5077. if (Control == NULL)
  5078. {
  5079. return false;
  5080. }
  5081. else
  5082. {
  5083. return Control->Visible;
  5084. }
  5085. }
  5086. //---------------------------------------------------------------------------
  5087. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  5088. {
  5089. bool Result;
  5090. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  5091. {
  5092. Result = !IsUWP();
  5093. }
  5094. else
  5095. {
  5096. Result = true;
  5097. }
  5098. return Result;
  5099. }
  5100. //---------------------------------------------------------------------------
  5101. void __fastcall TCustomScpExplorerForm::MakeFocusedItemVisible(TCustomDirView * DirView)
  5102. {
  5103. if (DirView->ItemFocused != NULL)
  5104. {
  5105. DirView->ItemFocused->MakeVisible(false);
  5106. }
  5107. }
  5108. //---------------------------------------------------------------------------
  5109. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  5110. {
  5111. MakeFocusedItemVisible(RemoteDirView);
  5112. QueueSplitter->Visible = QueuePanel->Visible;
  5113. QueueFileListSplitter->Visible = QueueFileList->Visible;
  5114. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  5115. TControl * QueueControlsOrder[] =
  5116. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  5117. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  5118. }
  5119. //---------------------------------------------------------------------------
  5120. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  5121. {
  5122. switch (Component) {
  5123. case fcStatusBar: return RemoteStatusBar;
  5124. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  5125. case fcQueueView: return QueuePanel;
  5126. case fcQueueToolbar: return QueueDock;
  5127. case fcRemoteTree: return RemoteDrivePanel;
  5128. case fcSessionsTabs: return SessionsPageControl;
  5129. case fcQueueFileList: return QueueFileList;
  5130. default: return NULL;
  5131. }
  5132. }
  5133. //---------------------------------------------------------------------------
  5134. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  5135. TObject *Sender, TListColumn *Column, TPoint &Point)
  5136. {
  5137. DebugAssert(NonVisualDataModule && Column && Sender);
  5138. NonVisualDataModule->ListColumn = Column;
  5139. TPopupMenu * DirViewColumnMenu;
  5140. if (Sender == DirView(osOther))
  5141. {
  5142. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  5143. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  5144. (Column->Index == uvName);
  5145. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  5146. (Column->Index == uvSize);
  5147. }
  5148. else
  5149. {
  5150. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  5151. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  5152. (Column->Index == dvName);
  5153. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  5154. (Column->Index == dvSize);
  5155. }
  5156. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  5157. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  5158. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  5159. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  5160. }
  5161. //---------------------------------------------------------------------------
  5162. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  5163. TObject *Sender, TListItem *Item, bool &AllowExec)
  5164. {
  5165. DoDirViewExecFile(Sender, Item, AllowExec);
  5166. }
  5167. //---------------------------------------------------------------------------
  5168. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  5169. TListItem * Item, bool & AllowExec)
  5170. {
  5171. DebugAssert(Sender && Item && Configuration);
  5172. DebugAssert(AllowExec);
  5173. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  5174. TOperationSide Side = ((ADirView == DirView(osRemote)) ? osRemote : osLocal);
  5175. bool Remote = !IsSideLocalBrowser(Side);
  5176. DebugAssert(Item == ADirView->ItemFocused);
  5177. bool IsDirectory = ADirView->ItemIsDirectory(Item);
  5178. TResolvedDoubleClickAction Action;
  5179. if (FForceExecution)
  5180. {
  5181. Action = IsDirectory ? rdcaChangeDir : rdcaOpen;
  5182. }
  5183. else
  5184. {
  5185. TTerminal * ATerminal = !IsSideLocalBrowser(Side) ? Terminal : NULL;
  5186. Action = WinConfiguration->ResolveDoubleClickAction(IsDirectory, ATerminal);
  5187. }
  5188. if (Action == rdcaCopy)
  5189. {
  5190. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  5191. UnlockWindow();
  5192. try
  5193. {
  5194. if (IsLocalBrowserMode())
  5195. {
  5196. LocalLocalCopy(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation, false, 0);
  5197. }
  5198. else
  5199. {
  5200. ExecuteFileOperation(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  5201. }
  5202. }
  5203. __finally
  5204. {
  5205. LockWindow();
  5206. }
  5207. AllowExec = false;
  5208. }
  5209. else if (Action == rdcaEdit)
  5210. {
  5211. if (!Remote || !WinConfiguration->DisableOpenEdit)
  5212. {
  5213. ExecuteFile(Side, efDefaultEditor);
  5214. AllowExec = false;
  5215. }
  5216. }
  5217. else if (Action == rdcaOpen)
  5218. {
  5219. if (DebugAlwaysTrue(!IsDirectory) && Remote && !WinConfiguration->DisableOpenEdit)
  5220. {
  5221. ExecuteFile(osRemote, efShell);
  5222. AllowExec = false;
  5223. }
  5224. }
  5225. }
  5226. //---------------------------------------------------------------------------
  5227. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5228. {
  5229. return ((Side == osRemote) || (Side == osCurrent));
  5230. }
  5231. //---------------------------------------------------------------------------
  5232. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5233. {
  5234. DebugFail();
  5235. }
  5236. //---------------------------------------------------------------------------
  5237. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5238. {
  5239. DebugFail();
  5240. }
  5241. //---------------------------------------------------------------------------
  5242. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5243. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5244. {
  5245. TSynchronizeParamType Params;
  5246. Params.LocalDirectory = LocalDirectory;
  5247. Params.RemoteDirectory = RemoteDirectory;
  5248. int UnusedParams =
  5249. (GUIConfiguration->SynchronizeParams &
  5250. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5251. TTerminal::spCaseSensitive));
  5252. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5253. Params.Options = GUIConfiguration->SynchronizeOptions;
  5254. bool SaveSettings = false;
  5255. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5256. &DoSynchronizeTooManyDirectories);
  5257. DebugAssert(FSynchronizeController == NULL);
  5258. FSynchronizeController = &Controller;
  5259. bool Result;
  5260. try
  5261. {
  5262. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5263. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5264. int Options =
  5265. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5266. DebugAssert(FOnFeedSynchronizeError == NULL);
  5267. DebugAssert(FOnSynchronizeAbort == NULL);
  5268. DebugAssert(FSynchronizeTerminal == NULL);
  5269. FSynchronizeTerminal = Terminal;
  5270. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5271. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5272. FOnFeedSynchronizeError, FOnSynchronizeAbort, SynchronizeInNewWindow, UseDefaults);
  5273. if (Result)
  5274. {
  5275. if (SaveSettings)
  5276. {
  5277. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5278. GUIConfiguration->SynchronizeOptions = Params.Options;
  5279. }
  5280. else
  5281. {
  5282. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5283. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5284. FLAGSET(Params.Options, soSynchronize))
  5285. {
  5286. GUIConfiguration->SynchronizeOptions =
  5287. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5288. soSynchronize;
  5289. }
  5290. }
  5291. LocalDirectory = Params.LocalDirectory;
  5292. RemoteDirectory = Params.RemoteDirectory;
  5293. }
  5294. }
  5295. __finally
  5296. {
  5297. FSynchronizeController = NULL;
  5298. DebugAssert(FOnFeedSynchronizeError == NULL);
  5299. FOnFeedSynchronizeError = NULL;
  5300. DebugAssert(FOnSynchronizeAbort == NULL);
  5301. FOnSynchronizeAbort = NULL;
  5302. FSynchronizeTerminal = NULL;
  5303. }
  5304. return Result;
  5305. }
  5306. //---------------------------------------------------------------------------
  5307. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5308. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5309. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5310. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5311. TSynchronizeOptions * Options, bool Full)
  5312. {
  5313. // Keep-up-to-date mode
  5314. if (Terminal->Status == ssOpened)
  5315. {
  5316. try
  5317. {
  5318. int PParams = Params.Params;
  5319. if (!Full)
  5320. {
  5321. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5322. if (FLAGSET(Params.Options, soRecurse))
  5323. {
  5324. PParams |= TTerminal::spSubDirs;
  5325. }
  5326. }
  5327. else
  5328. {
  5329. // if keepuptodate is non-recursive,
  5330. // full sync before has to be non-recursive as well
  5331. if (FLAGCLEAR(Params.Options, soRecurse))
  5332. {
  5333. PParams |= TTerminal::spNoRecurse;
  5334. }
  5335. }
  5336. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5337. PParams, Checklist, Options);
  5338. }
  5339. catch (Exception & E)
  5340. {
  5341. if (!Terminal->Active)
  5342. {
  5343. ShowExtendedExceptionEx(Terminal, &E);
  5344. // Do not abort the "keep up to date" when the session was succesfully reconnected.
  5345. if (!Terminal->Active)
  5346. {
  5347. throw;
  5348. }
  5349. }
  5350. else
  5351. {
  5352. if (FLAGSET(Params.Options, soContinueOnError) &&
  5353. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5354. {
  5355. // noop, already logged in MoreMessageDialog
  5356. }
  5357. else
  5358. {
  5359. // We get mostly EAbort here, so this is noop.
  5360. // Exception is an error when listing directories while looking for differences.
  5361. ShowExtendedExceptionEx(Terminal, &E);
  5362. throw;
  5363. }
  5364. }
  5365. }
  5366. }
  5367. }
  5368. //---------------------------------------------------------------------------
  5369. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5370. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5371. const UnicodeString ErrorStr)
  5372. {
  5373. if (!Directory.IsEmpty())
  5374. {
  5375. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5376. }
  5377. else
  5378. {
  5379. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5380. }
  5381. }
  5382. //---------------------------------------------------------------------------
  5383. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5384. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5385. {
  5386. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5387. {
  5388. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5389. }
  5390. else
  5391. {
  5392. TMessageParams Params(mpNeverAskAgainCheck);
  5393. unsigned int Result = MessageDialog(
  5394. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5395. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5396. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5397. {
  5398. MaxDirectories *= 2;
  5399. if (Result == qaNeverAskAgain)
  5400. {
  5401. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5402. }
  5403. }
  5404. else
  5405. {
  5406. Abort();
  5407. }
  5408. }
  5409. }
  5410. //---------------------------------------------------------------------------
  5411. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5412. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5413. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5414. TSynchronizeOptions * Options)
  5415. {
  5416. // Keep-up-to-date mode
  5417. DebugAssert(!FAutoOperation);
  5418. void * BatchStorage;
  5419. BatchStart(BatchStorage);
  5420. FAutoOperation = true;
  5421. bool AnyOperation = false;
  5422. TDateTime StartTime = Now();
  5423. TSynchronizeChecklist * AChecklist = NULL;
  5424. try
  5425. {
  5426. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5427. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5428. {
  5429. FSynchronizeProgressForm->Start();
  5430. }
  5431. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5432. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5433. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5434. Options);
  5435. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5436. SAFE_DESTROY(FSynchronizeProgressForm);
  5437. AnyOperation = (AChecklist->CheckedCount > 0);
  5438. if (AnyOperation)
  5439. {
  5440. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5441. CreateProgressForm(&SynchronizeProgress);
  5442. try
  5443. {
  5444. Terminal->SynchronizeApply(
  5445. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5446. SynchronizeProcessedItem, NULL, NULL, NULL);
  5447. }
  5448. __finally
  5449. {
  5450. DestroyProgressForm();
  5451. }
  5452. }
  5453. }
  5454. __finally
  5455. {
  5456. if (Checklist == NULL)
  5457. {
  5458. delete AChecklist;
  5459. }
  5460. else
  5461. {
  5462. *Checklist = AChecklist;
  5463. }
  5464. FAutoOperation = false;
  5465. SAFE_DESTROY(FSynchronizeProgressForm);
  5466. BatchEnd(BatchStorage);
  5467. ReloadLocalDirectory();
  5468. if (AnyOperation)
  5469. {
  5470. OperationComplete(StartTime);
  5471. }
  5472. }
  5473. }
  5474. //---------------------------------------------------------------------------
  5475. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5476. {
  5477. // can be called from command line
  5478. return Visible &&
  5479. ((DirView(osRemote)->SelCount > 0) ||
  5480. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5481. }
  5482. //---------------------------------------------------------------------------
  5483. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5484. {
  5485. LogSynchronizeEvent(Terminal, Message);
  5486. }
  5487. //---------------------------------------------------------------------------
  5488. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5489. int Params, TSynchronizeOptions & Options)
  5490. {
  5491. DebugAssert(!IsLocalBrowserMode());
  5492. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5493. {
  5494. Options.Filter = new TStringList();
  5495. Options.Filter->CaseSensitive = false;
  5496. Options.Filter->Duplicates = Types::dupAccept;
  5497. if (DirView(osRemote)->SelCount > 0)
  5498. {
  5499. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5500. }
  5501. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5502. {
  5503. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5504. }
  5505. Options.Filter->Sort();
  5506. }
  5507. }
  5508. //---------------------------------------------------------------------------
  5509. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5510. {
  5511. TCopyParamType Defaults;
  5512. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5513. ConfigStorage->AccessMode = smRead;
  5514. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5515. {
  5516. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5517. }
  5518. ConfigStorage.reset(NULL);
  5519. std::unique_ptr<TStringList> Options(new TStringList());
  5520. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5521. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5522. UnicodeString Result;
  5523. if (Options->Count > 0)
  5524. {
  5525. Result =
  5526. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5527. }
  5528. return Result;
  5529. }
  5530. //---------------------------------------------------------------------------
  5531. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5532. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5533. {
  5534. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5535. UnicodeString AdditionalParams =
  5536. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5537. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5538. SerializeCopyParamForCommandLine(CopyParams),
  5539. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5540. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5541. ExecuteNewInstance(SessionName, AdditionalParams);
  5542. }
  5543. //---------------------------------------------------------------------------
  5544. struct TSynchronizeParams
  5545. {
  5546. UnicodeString LocalDirectory;
  5547. UnicodeString RemoteDirectory;
  5548. TSynchronizeMode Mode;
  5549. TSynchronizeChecklist * Checklist;
  5550. int Params;
  5551. TCopyParamType * CopyParam;
  5552. TDateTime CollectElapsed;
  5553. TDateTime * StartTime;
  5554. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5555. };
  5556. //---------------------------------------------------------------------------
  5557. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5558. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5559. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5560. {
  5561. DebugAssert(!FAutoOperation);
  5562. void * BatchStorage;
  5563. BatchStart(BatchStorage);
  5564. FAutoOperation = true;
  5565. TFileOperationStatistics Statistics;
  5566. TDateTime Start = Now();
  5567. try
  5568. {
  5569. Params.OnProcessedItem = OnProcessedItem;
  5570. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5571. CreateProgressForm(&SynchronizeProgress);
  5572. Terminal->SynchronizeApply(
  5573. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5574. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5575. &Statistics);
  5576. }
  5577. __finally
  5578. {
  5579. Params.OnProcessedItem = NULL;
  5580. FAutoOperation = false;
  5581. DestroyProgressForm();
  5582. BatchEnd(BatchStorage);
  5583. ReloadLocalDirectory();
  5584. }
  5585. if (WinConfiguration->SynchronizeSummary)
  5586. {
  5587. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5588. // The statistics should be 0 anyway in this case
  5589. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5590. {
  5591. Message += L"\n";
  5592. if (Statistics.FilesUploaded > 0)
  5593. {
  5594. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5595. }
  5596. if (Statistics.FilesDownloaded > 0)
  5597. {
  5598. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5599. }
  5600. if (Statistics.FilesDeletedLocal > 0)
  5601. {
  5602. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5603. }
  5604. if (Statistics.FilesDeletedRemote > 0)
  5605. {
  5606. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5607. }
  5608. }
  5609. TDateTime Elapsed = (Now() - Start);
  5610. Message +=
  5611. L"\n" +
  5612. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5613. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5614. TMessageParams Params(mpNeverAskAgainCheck);
  5615. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5616. if (Result == qaNeverAskAgain)
  5617. {
  5618. WinConfiguration->SynchronizeSummary = false;
  5619. }
  5620. }
  5621. }
  5622. //---------------------------------------------------------------------------
  5623. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5624. {
  5625. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5626. {
  5627. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5628. }
  5629. // Not set in keep-up-to-date mode - Synchronize() method
  5630. if (Token != NULL)
  5631. {
  5632. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5633. if (Params.OnProcessedItem != NULL)
  5634. {
  5635. Params.OnProcessedItem(NULL, ChecklistItem);
  5636. }
  5637. }
  5638. }
  5639. //---------------------------------------------------------------------------
  5640. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5641. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5642. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5643. {
  5644. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5645. *Params.StartTime = Now();
  5646. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5647. }
  5648. //---------------------------------------------------------------------------
  5649. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5650. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5651. {
  5652. // terminal can be already closed (e.g. dropped connection)
  5653. if (Terminal != NULL)
  5654. {
  5655. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5656. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5657. }
  5658. }
  5659. //---------------------------------------------------------------------------
  5660. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5661. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5662. {
  5663. DebugAssert(!IsLocalBrowserMode());
  5664. TAutoBatch AutoBatch(this);
  5665. TAutoFlag AutoOperationFlag(FAutoOperation);
  5666. if (Side == osRemote)
  5667. {
  5668. std::unique_ptr<TStrings> FileList(new TStringList());
  5669. FileList->AddObject(FileName, RemoteFile);
  5670. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5671. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5672. RemoteDirView->SaveSelection();
  5673. RemoteDirView->SaveSelectedNames();
  5674. try
  5675. {
  5676. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5677. }
  5678. catch(...)
  5679. {
  5680. RemoteDirView->DiscardSavedSelection();
  5681. throw;
  5682. }
  5683. RemoteDirView->RestoreSelection();
  5684. }
  5685. else if (DebugAlwaysTrue(Side == osLocal))
  5686. {
  5687. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5688. {
  5689. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5690. }
  5691. UnicodeString Directory = ExtractFileDir(FileName);
  5692. ReloadLocalDirectory(Directory);
  5693. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5694. if (!SamePaths(Directory, NewDirectory))
  5695. {
  5696. ReloadLocalDirectory(NewDirectory);
  5697. }
  5698. }
  5699. }
  5700. //---------------------------------------------------------------------------
  5701. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5702. {
  5703. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5704. if (Side == osLocal)
  5705. {
  5706. if (Action == TSynchronizeChecklist::saDownloadNew)
  5707. {
  5708. OpenFolderInExplorer(LocalPath);
  5709. }
  5710. else
  5711. {
  5712. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5713. }
  5714. }
  5715. else if (DebugAlwaysTrue(Side == osRemote))
  5716. {
  5717. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5718. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5719. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5720. if (!LocalPath.IsEmpty())
  5721. {
  5722. SessionData->LocalDirectory = LocalPath;
  5723. }
  5724. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5725. ExecuteNewInstance(SessionName, FORMAT(L"%s=\"%s\"", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5726. }
  5727. }
  5728. //---------------------------------------------------------------------------
  5729. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5730. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5731. const TCopyParamType * CopyParams)
  5732. {
  5733. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5734. UnicodeString AdditionalParams =
  5735. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5736. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5737. SerializeCopyParamForCommandLine(CopyParams),
  5738. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5739. LocalDirectory, RemoteDirectory, Mode, Params));
  5740. ExecuteNewInstance(SessionName, AdditionalParams);
  5741. }
  5742. //---------------------------------------------------------------------------
  5743. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5744. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5745. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5746. {
  5747. int Result;
  5748. bool SaveSettings = false;
  5749. int Options =
  5750. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5751. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5752. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5753. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5754. bool Continue =
  5755. (UseDefaults == 0) ||
  5756. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5757. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5758. if (Continue)
  5759. {
  5760. Configuration->Usage->Inc(L"Synchronizations");
  5761. CopyParam.IncludeFileMask.SetRoots(LocalDirectory, RemoteDirectory);
  5762. UpdateCopyParamCounters(CopyParam);
  5763. TSynchronizeOptions SynchronizeOptions;
  5764. GetSynchronizeOptions(Params, SynchronizeOptions);
  5765. if (SaveSettings)
  5766. {
  5767. GUIConfiguration->SynchronizeParams = Params;
  5768. }
  5769. else
  5770. {
  5771. SaveMode = false;
  5772. }
  5773. TDateTime StartTime = Now();
  5774. TSynchronizeChecklist * Checklist = NULL;
  5775. try
  5776. {
  5777. DebugAssert(!FAutoOperation);
  5778. FAutoOperation = true;
  5779. try
  5780. {
  5781. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5782. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5783. int Files = -1;
  5784. if (DataList->Count >= 1)
  5785. {
  5786. Files = StrToIntDef(DataList->Strings[0], Files);
  5787. }
  5788. else
  5789. {
  5790. DataList->Add(UnicodeString());
  5791. }
  5792. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5793. FSynchronizeProgressForm->Start();
  5794. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5795. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5796. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5797. &SynchronizeOptions);
  5798. if (Terminal->SessionData->CacheDirectories)
  5799. {
  5800. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5801. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5802. }
  5803. }
  5804. __finally
  5805. {
  5806. FAutoOperation = false;
  5807. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5808. SAFE_DESTROY(FSynchronizeProgressForm);
  5809. }
  5810. TSynchronizeParams SynchronizeParams;
  5811. SynchronizeParams.LocalDirectory = LocalDirectory;
  5812. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5813. SynchronizeParams.Mode = Mode;
  5814. SynchronizeParams.CopyParam = &CopyParam;
  5815. SynchronizeParams.Params = Params;
  5816. SynchronizeParams.Checklist = Checklist;
  5817. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5818. SynchronizeParams.StartTime = &StartTime;
  5819. SynchronizeParams.OnProcessedItem = NULL;
  5820. Result = Checklist->Count;
  5821. if (Checklist->Count > 0)
  5822. {
  5823. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5824. {
  5825. if (!DoSynchronizeChecklistDialog(
  5826. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5827. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  5828. {
  5829. Result = -1;
  5830. }
  5831. }
  5832. else
  5833. {
  5834. FullSynchronize(SynchronizeParams, NULL, NULL);
  5835. }
  5836. }
  5837. else
  5838. {
  5839. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5840. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5841. }
  5842. }
  5843. __finally
  5844. {
  5845. delete Checklist;
  5846. }
  5847. OperationComplete(StartTime);
  5848. }
  5849. else
  5850. {
  5851. Result = -1;
  5852. }
  5853. return Result;
  5854. }
  5855. //---------------------------------------------------------------------------
  5856. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5857. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5858. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5859. {
  5860. if (Collect)
  5861. {
  5862. DebugAssert(FSynchronizeProgressForm != NULL);
  5863. if (!FSynchronizeProgressForm->Started)
  5864. {
  5865. FSynchronizeProgressForm->Start();
  5866. }
  5867. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5868. SetTaskbarListProgressValue(CompareProgress);
  5869. }
  5870. else
  5871. {
  5872. DebugAssert(FSynchronizeProgressForm == NULL);
  5873. DebugAssert(FProgressForm != NULL);
  5874. }
  5875. }
  5876. //---------------------------------------------------------------------------
  5877. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5878. {
  5879. UnicodeString FullFileName = AbsolutePath(Terminal->CurrentDirectory, FileName);
  5880. TRemoteFile * File = NULL;
  5881. Terminal->ReadFile(FullFileName, File);
  5882. if (File != NULL)
  5883. {
  5884. std::unique_ptr<TRemoteFile> FileOwner(File);
  5885. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5886. Application->ShowMainForm = false;
  5887. Application->Run();
  5888. }
  5889. }
  5890. //---------------------------------------------------------------------------
  5891. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory(TOperationSide)
  5892. {
  5893. DebugFail();
  5894. }
  5895. //---------------------------------------------------------------------------
  5896. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5897. {
  5898. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5899. SessionData->Assign(ManagedSession->SessionData);
  5900. UpdateSessionData(SessionData.get());
  5901. DebugAssert(SessionData->IsLocalBrowser == IsLocalBrowserMode());
  5902. if (IsActiveTerminal(ManagedSession))
  5903. {
  5904. ManagedSession->UpdateSessionCredentials(SessionData.get());
  5905. }
  5906. if (!ManagedSession->SessionData->HasSessionName())
  5907. {
  5908. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5909. // once IsWorkspace is cleared
  5910. SessionData->Name = UnicodeString();
  5911. }
  5912. // Most uses of this method ends with saving the session, and we do not want to store the IsWorkspace flag.
  5913. // Particularly, with SaveHiddenDuplicateSession, it won't work as TSessionData::ParseUrl explicitly ignored workspace sessions.
  5914. SessionData->IsWorkspace = false;
  5915. return SessionData.release();
  5916. }
  5917. //---------------------------------------------------------------------------
  5918. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5919. {
  5920. TSessionData * SessionData = CloneCurrentSessionData();
  5921. try
  5922. {
  5923. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5924. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5925. }
  5926. __finally
  5927. {
  5928. delete SessionData;
  5929. }
  5930. }
  5931. //---------------------------------------------------------------------------
  5932. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5933. {
  5934. TTerminalManager * Manager = TTerminalManager::Instance();
  5935. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5936. if (DebugAlwaysTrue(ManagedSession != NULL))
  5937. {
  5938. // Update ManagedSession->StateData
  5939. UpdateSession(ManagedSession);
  5940. }
  5941. Manager->SaveWorkspace(DataList.get());
  5942. return DataList.release();
  5943. }
  5944. //---------------------------------------------------------------------------
  5945. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5946. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5947. {
  5948. WinConfiguration->LastWorkspace = Name;
  5949. if (!SavePasswords)
  5950. {
  5951. for (int Index = 0; Index < DataList->Count; Index++)
  5952. {
  5953. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5954. if (SessionData->Link.IsEmpty())
  5955. {
  5956. SessionData->ClearSessionPasswords();
  5957. }
  5958. }
  5959. }
  5960. StoredSessions->NewWorkspace(Name, DataList);
  5961. // modified only
  5962. StoredSessions->Save(false, Explicit);
  5963. }
  5964. //---------------------------------------------------------------------------
  5965. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5966. {
  5967. return
  5968. DefaultStr(WinConfiguration->LastWorkspace,
  5969. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5970. }
  5971. //---------------------------------------------------------------------------
  5972. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5973. {
  5974. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5975. bool AnyNonStoredSessionWithPassword = false;
  5976. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5977. bool AllNonStoredSessionsAnonymous = true;
  5978. TTerminalManager * Manager = TTerminalManager::Instance();
  5979. for (int Index = 0; Index < DataList->Count; Index++)
  5980. {
  5981. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5982. if (SessionData->Link.IsEmpty())
  5983. {
  5984. if (SessionData->HasAnySessionPassword())
  5985. {
  5986. AnyNonStoredSessionWithPassword = true;
  5987. if (!Manager->Sessions[Index]->SessionData->IsWorkspace)
  5988. {
  5989. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5990. }
  5991. }
  5992. if (!SameText(SessionData->UserName, AnonymousUserName))
  5993. {
  5994. AllNonStoredSessionsAnonymous = false;
  5995. }
  5996. }
  5997. }
  5998. bool SavePasswords;
  5999. bool * PSavePasswords;
  6000. bool NotRecommendedSavingPasswords =
  6001. !CustomWinConfiguration->UseMasterPassword &&
  6002. !AllNonStoredSessionsAnonymous;
  6003. if (Configuration->DisablePasswordStoring ||
  6004. !AnyNonStoredSessionWithPassword)
  6005. {
  6006. PSavePasswords = NULL;
  6007. SavePasswords = false;
  6008. }
  6009. else
  6010. {
  6011. PSavePasswords = &SavePasswords;
  6012. SavePasswords =
  6013. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  6014. !NotRecommendedSavingPasswords;
  6015. }
  6016. UnicodeString Name = WorkspaceName();
  6017. bool CreateShortcut = false;
  6018. bool Result =
  6019. DoSaveWorkspaceDialog(
  6020. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  6021. EnableAutoSave);
  6022. if (Result)
  6023. {
  6024. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  6025. if (CreateShortcut)
  6026. {
  6027. TOperationVisualizer Visualizer;
  6028. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  6029. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  6030. }
  6031. if (EnableAutoSave)
  6032. {
  6033. WinConfiguration->AutoSaveWorkspace = true;
  6034. WinConfiguration->AutoWorkspace = Name;
  6035. if (PSavePasswords != NULL)
  6036. {
  6037. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  6038. }
  6039. }
  6040. }
  6041. return Result;
  6042. }
  6043. //---------------------------------------------------------------------------
  6044. void __fastcall TCustomScpExplorerForm::UpdateSession(TManagedTerminal * Session)
  6045. {
  6046. DebugAssert(IsLocalBrowserMode() == Session->LocalBrowser);
  6047. DebugAssert(Session == ManagedSession);
  6048. if (!IsLocalBrowserMode())
  6049. {
  6050. SAFE_DESTROY(Session->RemoteExplorerState);
  6051. if (WinConfiguration->PreservePanelState)
  6052. {
  6053. Session->RemoteExplorerState = RemoteDirView->SaveState();
  6054. }
  6055. }
  6056. UpdateSessionData(Session->StateData);
  6057. }
  6058. //---------------------------------------------------------------------------
  6059. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  6060. {
  6061. // Keep in sync with TSessionData::CopyStateData
  6062. DebugAssert(Data != NULL);
  6063. // This is inconsistent with how color (for example) is handled.
  6064. Data->Name = ManagedSession->SessionData->Name;
  6065. if (!IsLocalBrowserMode())
  6066. {
  6067. // cannot use RemoteDirView->Path, because it is empty if connection
  6068. // was already closed
  6069. // also only peek, we may not be connected at all atm
  6070. // so make sure we do not try retrieving current directory from the server
  6071. // (particularly with FTP)
  6072. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  6073. if (!ACurrentDirectory.IsEmpty())
  6074. {
  6075. Data->RemoteDirectory = ACurrentDirectory;
  6076. }
  6077. }
  6078. Data->Color = SessionColor;
  6079. }
  6080. //---------------------------------------------------------------------------
  6081. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  6082. {
  6083. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  6084. DebugAssert(Toolbar != NULL);
  6085. for (int i = 0; i < Toolbar->Items->Count; i++)
  6086. {
  6087. TTBXCustomDropDownItem * DropDownItem;
  6088. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6089. if (DropDownItem != NULL)
  6090. {
  6091. ToolbarItemResize(DropDownItem,
  6092. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  6093. Toolbar->NonClientWidth);
  6094. break;
  6095. }
  6096. }
  6097. }
  6098. //---------------------------------------------------------------------------
  6099. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  6100. {
  6101. Item->EditWidth = Width;
  6102. }
  6103. //---------------------------------------------------------------------------
  6104. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  6105. TTBCustomToolbar * Toolbar, TPoint & ASize)
  6106. {
  6107. for (int i = 0; i < Toolbar->Items->Count; i++)
  6108. {
  6109. TTBXCustomDropDownItem * DropDownItem;
  6110. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  6111. if (DropDownItem != NULL)
  6112. {
  6113. ASize.x -= DropDownItem->EditWidth;
  6114. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  6115. }
  6116. }
  6117. }
  6118. //---------------------------------------------------------------------------
  6119. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  6120. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  6121. {
  6122. if (WinConfiguration->DDWarnLackOfTempSpace)
  6123. {
  6124. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  6125. if (!ADrive.IsEmpty())
  6126. {
  6127. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  6128. DebugAssert(RequiredSpace >= 0);
  6129. __int64 RequiredWithReserve;
  6130. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  6131. if (FreeSpace < RequiredWithReserve)
  6132. {
  6133. unsigned int Result;
  6134. TMessageParams Params(mpNeverAskAgainCheck);
  6135. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  6136. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  6137. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  6138. if (Result == qaNeverAskAgain)
  6139. {
  6140. WinConfiguration->DDWarnLackOfTempSpace = false;
  6141. }
  6142. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  6143. }
  6144. }
  6145. }
  6146. }
  6147. //---------------------------------------------------------------------------
  6148. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  6149. {
  6150. DoOpenDirectoryDialog(odAddBookmark, Side);
  6151. }
  6152. //---------------------------------------------------------------------------
  6153. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  6154. {
  6155. // we should better use TCustomDirView::FCaseSensitive, but it is private
  6156. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  6157. try
  6158. {
  6159. TCustomDirView * DView = DirView(Side);
  6160. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  6161. {
  6162. VisitedDirectories->Add(DView->HistoryPath[Index]);
  6163. }
  6164. }
  6165. catch (...)
  6166. {
  6167. delete VisitedDirectories;
  6168. throw;
  6169. }
  6170. return VisitedDirectories;
  6171. }
  6172. //---------------------------------------------------------------------------
  6173. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  6174. TOpenDirectoryMode Mode, TOperationSide Side)
  6175. {
  6176. bool Continue = true;
  6177. if (Mode == odAddBookmark)
  6178. {
  6179. TMessageParams Params(mpNeverAskAgainCheck);
  6180. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  6181. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  6182. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  6183. unsigned int Answer =
  6184. MessageDialog(Message,
  6185. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  6186. if (Answer == qaNeverAskAgain)
  6187. {
  6188. Continue = true;
  6189. WinConfiguration->UseSharedBookmarks = true;
  6190. }
  6191. else if (Answer == qaOK)
  6192. {
  6193. Continue = true;
  6194. WinConfiguration->UseSharedBookmarks = false;
  6195. }
  6196. else
  6197. {
  6198. Continue = false;
  6199. }
  6200. }
  6201. if (Continue)
  6202. {
  6203. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  6204. try
  6205. {
  6206. UnicodeString Name = DirView(Side)->PathName;
  6207. bool Repeat;
  6208. do
  6209. {
  6210. Repeat = false;
  6211. TOperationSide BookmarkSide = IsSideLocalBrowser(Side) ? osLocal : osRemote;
  6212. if (::DoOpenDirectoryDialog(Mode, BookmarkSide, Name, VisitedDirectories, Terminal,
  6213. // do not allow switching to location profiles,
  6214. // if we are not connected
  6215. HasDirView[osLocal] && (Terminal != NULL)))
  6216. {
  6217. TWindowLock Lock(this);
  6218. if (!TryOpenDirectory(Side, Name))
  6219. {
  6220. Repeat = true;
  6221. Mode = odBrowse;
  6222. }
  6223. }
  6224. }
  6225. while (Repeat);
  6226. }
  6227. __finally
  6228. {
  6229. delete VisitedDirectories;
  6230. }
  6231. }
  6232. }
  6233. //---------------------------------------------------------------------------
  6234. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6235. {
  6236. DebugAssert(!Terminal->CommandSessionOpened);
  6237. return TTerminalManager::Instance()->ConnectTerminal(Terminal->CommandSession);
  6238. }
  6239. //---------------------------------------------------------------------------
  6240. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6241. {
  6242. bool Result = Terminal->IsCapable[Capability] ||
  6243. Terminal->CommandSessionOpened;
  6244. if (!Result)
  6245. {
  6246. DebugAssert(Terminal->IsCapable[fcSecondaryShell]);
  6247. if (!GUIConfiguration->ConfirmCommandSession)
  6248. {
  6249. Result = true;
  6250. }
  6251. else
  6252. {
  6253. TMessageParams Params(mpNeverAskAgainCheck);
  6254. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6255. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6256. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6257. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6258. if (Answer == qaNeverAskAgain)
  6259. {
  6260. GUIConfiguration->ConfirmCommandSession = false;
  6261. Result = true;
  6262. }
  6263. else if (Answer == qaOK)
  6264. {
  6265. Result = true;
  6266. }
  6267. }
  6268. if (Result)
  6269. {
  6270. Result = CommandSessionFallback();
  6271. }
  6272. }
  6273. return Result;
  6274. }
  6275. //---------------------------------------------------------------------------
  6276. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6277. {
  6278. if (EnsureCommandSessionFallback(fcAnyCommand))
  6279. {
  6280. DoConsoleDialog(Terminal, Command);
  6281. }
  6282. }
  6283. //---------------------------------------------------------------------------
  6284. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6285. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6286. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6287. {
  6288. if (!IsSideLocalBrowser(osRemote) &&
  6289. IsFileControl(DropSourceControl, osRemote) &&
  6290. (FDDExtMapFile != NULL))
  6291. {
  6292. Accept = true;
  6293. }
  6294. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6295. DebugAssert(FDDTargetControl != NULL);
  6296. }
  6297. //---------------------------------------------------------------------------
  6298. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6299. _di_IDataObject DataObj, int KeyState,
  6300. const TPoint & Point, int & Effect, bool & Accept)
  6301. {
  6302. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6303. }
  6304. //---------------------------------------------------------------------------
  6305. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6306. _di_IDataObject DataObj, int KeyState,
  6307. const TPoint & Point, int & Effect, bool & Accept)
  6308. {
  6309. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6310. }
  6311. //---------------------------------------------------------------------------
  6312. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6313. TObject * Sender)
  6314. {
  6315. DebugUsedParam(Sender);
  6316. DebugAssert(FDDTargetControl == Sender);
  6317. FDDTargetControl = NULL;
  6318. }
  6319. //---------------------------------------------------------------------------
  6320. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  6321. {
  6322. FileControlDDDragLeave(SessionsPageControl);
  6323. }
  6324. //---------------------------------------------------------------------------
  6325. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  6326. {
  6327. FileControlDDDragLeave(QueueView3);
  6328. }
  6329. //---------------------------------------------------------------------------
  6330. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6331. {
  6332. DebugAssert(!IsSideLocalBrowser(Side));
  6333. DebugUsedParam(Side);
  6334. bool Edit = false;
  6335. TRemoteFile * File = NULL;
  6336. UnicodeString FileName;
  6337. UnicodeString PointTo;
  6338. bool SymbolicLink = true;
  6339. if (RemoteDirView->ItemFocused)
  6340. {
  6341. DebugAssert(RemoteDirView->ItemFocused->Data);
  6342. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6343. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6344. if (Edit)
  6345. {
  6346. FileName = File->FileName;
  6347. PointTo = File->LinkTo;
  6348. }
  6349. else
  6350. {
  6351. PointTo = File->FileName;
  6352. }
  6353. }
  6354. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6355. Terminal->IsCapable[fcHardLink]))
  6356. {
  6357. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6358. if (Edit)
  6359. {
  6360. DebugAssert(File->FileName == FileName);
  6361. int Params = dfNoRecursive;
  6362. Terminal->ExceptionOnFail = true;
  6363. try
  6364. {
  6365. Terminal->DeleteFile(L"", File, &Params);
  6366. }
  6367. __finally
  6368. {
  6369. Terminal->ExceptionOnFail = false;
  6370. }
  6371. }
  6372. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6373. }
  6374. }
  6375. //---------------------------------------------------------------------------
  6376. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6377. {
  6378. return
  6379. (IsSideLocalBrowser(Side) ||
  6380. (HasActiveTerminal() &&
  6381. Terminal->ResolvingSymlinks &&
  6382. Terminal->IsCapable[fcSymbolicLink]));
  6383. }
  6384. //---------------------------------------------------------------------------
  6385. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6386. {
  6387. return
  6388. !IsSideLocalBrowser(FCurrentSide) &&
  6389. (RemoteDirView->ItemFocused != NULL) &&
  6390. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6391. Terminal->SessionData->ResolveSymlinks;
  6392. }
  6393. //---------------------------------------------------------------------------
  6394. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6395. {
  6396. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6397. FForceExecution = true;
  6398. try
  6399. {
  6400. DirView(osCurrent)->ExecuteCurrentFile();
  6401. }
  6402. __finally
  6403. {
  6404. FForceExecution = false;
  6405. }
  6406. }
  6407. //---------------------------------------------------------------------------
  6408. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6409. {
  6410. TEditorData ExternalEditor;
  6411. ExternalEditor.Editor = edExternal;
  6412. bool Remember = false;
  6413. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6414. {
  6415. if (Remember)
  6416. {
  6417. TEditorList * EditorList = new TEditorList;
  6418. try
  6419. {
  6420. *EditorList = *WinConfiguration->EditorList;
  6421. bool Found = false;
  6422. int i = 0;
  6423. while (!Found && (i < EditorList->Count))
  6424. {
  6425. const TEditorPreferences * Editor = EditorList->Editors[i];
  6426. if ((Editor->Data->Editor == edExternal) &&
  6427. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6428. {
  6429. Found = true;
  6430. }
  6431. i++;
  6432. }
  6433. if (!Found)
  6434. {
  6435. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6436. WinConfiguration->EditorList = EditorList;
  6437. }
  6438. }
  6439. __finally
  6440. {
  6441. delete EditorList;
  6442. }
  6443. }
  6444. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6445. }
  6446. }
  6447. //---------------------------------------------------------------------------
  6448. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6449. {
  6450. FEditorManager->ProcessFiles(FileTerminalRemoved, ATerminal);
  6451. if (FFileFindTerminal == ATerminal)
  6452. {
  6453. FFileFindTerminal = NULL;
  6454. HideFileFindDialog();
  6455. }
  6456. if (FClipboardTerminal == ATerminal)
  6457. {
  6458. ClipboardClear(); // implies ClipboardStop
  6459. }
  6460. if ((FOnSynchronizeAbort != NULL) &&
  6461. (ATerminal == FManagedSession))
  6462. {
  6463. FOnSynchronizeAbort(NULL);
  6464. }
  6465. }
  6466. //---------------------------------------------------------------------------
  6467. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6468. {
  6469. DetachTerminal(Sender);
  6470. }
  6471. //---------------------------------------------------------------------------
  6472. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6473. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6474. {
  6475. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6476. DebugAssert(Terminal != NULL);
  6477. if (Data->Terminal == Terminal)
  6478. {
  6479. Data->Terminal = NULL;
  6480. }
  6481. }
  6482. //---------------------------------------------------------------------------
  6483. TManagedTerminal * TCustomScpExplorerForm::GetReplacementForLastSession()
  6484. {
  6485. return NULL;
  6486. }
  6487. //---------------------------------------------------------------------------
  6488. void __fastcall TCustomScpExplorerForm::LastTerminalClosed()
  6489. {
  6490. UpdateControls();
  6491. SessionColor = TColor(0);
  6492. UpdateRemotePathComboBox(false);
  6493. NeedSession(false);
  6494. }
  6495. //---------------------------------------------------------------------------
  6496. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6497. {
  6498. try
  6499. {
  6500. // Cache, as the login dialog can change its value.
  6501. // See also DoShow. Also see TTerminalManager::NewSession.
  6502. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6503. try
  6504. {
  6505. if (ShowLogin)
  6506. {
  6507. bool ReloadSessions = !Startup;
  6508. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this, true);
  6509. }
  6510. }
  6511. __finally
  6512. {
  6513. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6514. // (so there was no chance for the user to open any session yet)
  6515. if ((ShowLogin || !Startup) &&
  6516. !WinConfiguration->KeepOpenWhenNoSession &&
  6517. // this rules out the implicit Commander's local-local browser
  6518. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6519. {
  6520. TerminateApplication();
  6521. }
  6522. }
  6523. }
  6524. catch (EAbort &)
  6525. {
  6526. // swallow
  6527. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6528. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6529. }
  6530. }
  6531. //---------------------------------------------------------------------------
  6532. void __fastcall TCustomScpExplorerForm::SessionListChanged(bool ForceTruncationUpdate)
  6533. {
  6534. TTerminalManager * Manager = TTerminalManager::Instance();
  6535. DebugAssert(!Manager->Updating);
  6536. TAutoFlag UpdatingSessionTabsFlag(FUpdatingSessionTabs);
  6537. int ActiveSessionIndex = Manager->ActiveSessionIndex;
  6538. Configuration->Usage->SetMax(L"MaxOpenedSessions", Manager->Count);
  6539. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6540. try
  6541. {
  6542. int PrevTabsWidth = SessionsPageControl->TotalTabsWidth();
  6543. while (SessionsPageControl->PageCount > Manager->Count + 1)
  6544. {
  6545. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6546. }
  6547. for (int Index = 0; Index <= Manager->Count; Index++)
  6548. {
  6549. TThemeTabSheet * TabSheet;
  6550. if (Index >= SessionsPageControl->PageCount)
  6551. {
  6552. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6553. TabSheet->PageControl = SessionsPageControl;
  6554. }
  6555. else
  6556. {
  6557. TabSheet = SessionsPageControl->Pages[Index];
  6558. }
  6559. bool IsSessionTab = (Index < Manager->Count);
  6560. if (IsSessionTab)
  6561. {
  6562. TTerminal * Terminal = Manager->Sessions[Index];
  6563. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6564. TabSheet->CaptionTruncation = tttEllipsis;
  6565. UpdateSessionTab(TabSheet);
  6566. }
  6567. else
  6568. {
  6569. TabSheet->Tag = 0; // not really needed
  6570. TabSheet->Shadowed = false;
  6571. TabSheet->Button = SupportsLocalBrowser() ? ttbDropDown : ttbNone;
  6572. TabSheet->CaptionTruncation = tttNoText;
  6573. // 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)
  6574. UpdateNewTabTab();
  6575. }
  6576. }
  6577. // Prevent flicker on the right side where scrolling buttons would be, unless really needed
  6578. if (ForceTruncationUpdate || (SessionsPageControl->TotalTabsWidth() != PrevTabsWidth))
  6579. {
  6580. SessionsPageControl->UpdateTabsCaptionTruncation();
  6581. }
  6582. }
  6583. __finally
  6584. {
  6585. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6586. }
  6587. SessionsPageControl->ActivePageIndex = ActiveSessionIndex;
  6588. }
  6589. //---------------------------------------------------------------------------
  6590. UnicodeString TCustomScpExplorerForm::GetNewTabTabCaption()
  6591. {
  6592. return StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewTabAction->Caption));
  6593. }
  6594. //---------------------------------------------------------------------------
  6595. void __fastcall TCustomScpExplorerForm::UpdateNewTabTab()
  6596. {
  6597. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6598. UnicodeString TabCaption;
  6599. if (WinConfiguration->SelectiveToolbarText)
  6600. {
  6601. TabCaption = GetNewTabTabCaption();
  6602. }
  6603. TabSheet->BaseCaption = TabCaption;
  6604. TabSheet->ImageIndex = GetNewTabTabImageIndex(osCurrent);
  6605. }
  6606. //---------------------------------------------------------------------------
  6607. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabSession(TTabSheet * TabSheet)
  6608. {
  6609. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6610. }
  6611. //---------------------------------------------------------------------------
  6612. UnicodeString TCustomScpExplorerForm::GetLocalBrowserSessionTitle(TManagedTerminal *)
  6613. {
  6614. DebugFail();
  6615. return UnicodeString();
  6616. }
  6617. //---------------------------------------------------------------------------
  6618. void TCustomScpExplorerForm::UpdateSessionTab(TThemeTabSheet * TabSheet)
  6619. {
  6620. if (DebugAlwaysTrue(TabSheet != NULL))
  6621. {
  6622. TManagedTerminal * ASession = GetSessionTabSession(TabSheet);
  6623. if (DebugAlwaysTrue(ASession != NULL))
  6624. {
  6625. TColor Color = (ASession == Terminal) ? FSessionColor : ASession->StateData->Color;
  6626. if (ASession->LocalBrowser)
  6627. {
  6628. TabSheet->ImageIndex = FLocalBrowserTabImageIndex;
  6629. }
  6630. else
  6631. {
  6632. TabSheet->ImageIndex = AddSessionColor(Color);
  6633. }
  6634. TTerminalManager * Manager = TTerminalManager::Instance();
  6635. TabSheet->Shadowed = !ASession->Active && !ASession->LocalBrowser;
  6636. TabSheet->Button = CanCloseSession(ASession) ? ttbClose : ttbNone;
  6637. TabSheet->BaseCaption = Manager->GetSessionTitle(ASession, true);
  6638. }
  6639. }
  6640. }
  6641. //---------------------------------------------------------------------------
  6642. bool TCustomScpExplorerForm::CanCloseSession(TManagedTerminal * Session)
  6643. {
  6644. return !Session->LocalBrowser || (TTerminalManager::Instance()->Count > 1);
  6645. }
  6646. //---------------------------------------------------------------------------
  6647. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6648. {
  6649. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6650. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->ActivePage);
  6651. bool Result = (Session != NULL);
  6652. if (Result)
  6653. {
  6654. TTerminalManager::Instance()->ActiveSession = Session;
  6655. }
  6656. else
  6657. {
  6658. try
  6659. {
  6660. NewTab();
  6661. }
  6662. __finally
  6663. {
  6664. SessionListChanged();
  6665. }
  6666. FSessionsPageControlNewTabTime = Now();
  6667. }
  6668. return Result;
  6669. }
  6670. //---------------------------------------------------------------------------
  6671. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6672. {
  6673. SessionTabSwitched();
  6674. }
  6675. //---------------------------------------------------------------------------
  6676. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6677. {
  6678. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6679. DebugAssert(TransferList != NULL);
  6680. UnicodeString Name;
  6681. if (TransferList->ItemIndex <= 0)
  6682. {
  6683. Name = L"";
  6684. }
  6685. else
  6686. {
  6687. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6688. }
  6689. if (FCopyParamAutoSelected.IsEmpty())
  6690. {
  6691. // if previous preset was not autoselected, make new preset the "default"
  6692. FCopyParamDefault = Name;
  6693. }
  6694. GUIConfiguration->CopyParamCurrent = Name;
  6695. }
  6696. //---------------------------------------------------------------------------
  6697. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6698. {
  6699. // sanity check
  6700. bool ExistingPreset =
  6701. (FTransferListHoverIndex >= 0) &&
  6702. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6703. DebugAssert(ExistingPreset);
  6704. if (ExistingPreset)
  6705. {
  6706. HDC DC = GetDC(0);
  6707. TCanvas * Canvas = new TCanvas();
  6708. try
  6709. {
  6710. Canvas->Handle = DC;
  6711. Canvas->Font = GetToolbarFont(this);
  6712. UnicodeString Name;
  6713. if (FTransferListHoverIndex == 0)
  6714. {
  6715. Name = L"";
  6716. }
  6717. else
  6718. {
  6719. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6720. }
  6721. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6722. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6723. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6724. static_cast<TObject*>(GetComponent(fcTransferList)));
  6725. UnicodeString InfoStr =
  6726. GUIConfiguration->CopyParamPreset[Name].
  6727. GetInfoStr(L"; ",
  6728. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  6729. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6730. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6731. {
  6732. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6733. {
  6734. InfoStr.SetLength(InfoStr.Length() - 1);
  6735. }
  6736. InfoStr += Ellipsis;
  6737. }
  6738. // UpdateCaption does not cause invalidation of whole submenu, while
  6739. // setting Caption property does.
  6740. // also it probably does not resize the label, even if necessary
  6741. // (we do not want that anyway)
  6742. TransferLabel->UpdateCaption(InfoStr);
  6743. }
  6744. __finally
  6745. {
  6746. Canvas->Handle = NULL;
  6747. ReleaseDC(0, DC);
  6748. delete Canvas;
  6749. }
  6750. }
  6751. }
  6752. //---------------------------------------------------------------------------
  6753. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6754. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6755. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6756. {
  6757. if (FTransferListHoverIndex != AHoverIndex)
  6758. {
  6759. FTransferListHoverIndex = AHoverIndex;
  6760. UpdateTransferLabel();
  6761. }
  6762. }
  6763. //---------------------------------------------------------------------------
  6764. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6765. {
  6766. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6767. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6768. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6769. {
  6770. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6771. {
  6772. if (!NonVisualDataModule->Busy)
  6773. {
  6774. OpenDirectory(GetSide(osCurrent));
  6775. }
  6776. Message.Result = 1;
  6777. }
  6778. else
  6779. {
  6780. TForm::Dispatch(&Message);
  6781. }
  6782. }
  6783. else
  6784. {
  6785. TForm::Dispatch(&Message);
  6786. }
  6787. }
  6788. //---------------------------------------------------------------------------
  6789. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6790. {
  6791. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6792. if ((FIgnoreNextDialogChar != 0) &&
  6793. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6794. {
  6795. Message.Result = 1;
  6796. }
  6797. else
  6798. {
  6799. TForm::Dispatch(&Message);
  6800. }
  6801. FIgnoreNextDialogChar = 0;
  6802. }
  6803. //---------------------------------------------------------------------------
  6804. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6805. {
  6806. // The four low-order bits are used internally by Windows
  6807. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6808. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6809. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6810. // 61730 - restore thru double click - undocumented
  6811. // 61490 - maximize thru double click - undocumented
  6812. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6813. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6814. {
  6815. SysResizing(Cmd);
  6816. }
  6817. TForm::Dispatch(&Message);
  6818. }
  6819. //---------------------------------------------------------------------------
  6820. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6821. {
  6822. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6823. // Also there no point returning TRUE as we are not able to
  6824. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6825. // Hence the process termination might be safer :)
  6826. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6827. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6828. {
  6829. Message.Result = FALSE;
  6830. }
  6831. else
  6832. {
  6833. Message.Result = TRUE;
  6834. }
  6835. // Do not call default handling as that triggers OnCloseQuery,
  6836. // where our implementation will popup confirmation dialogs, what we do not want,
  6837. // as per Vista guidelines:
  6838. // https://docs.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6839. }
  6840. //---------------------------------------------------------------------------
  6841. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6842. {
  6843. if (Message.EndSession && IsApplicationMinimized())
  6844. {
  6845. // WORKAROUND
  6846. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6847. // when it receives WM_ENDSESSION,
  6848. // but that sometimes (particularly when application is minimized) cause crashes.
  6849. // Crash popups message that blocks log off.
  6850. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6851. // so we call ExitProcess() immediately, not even trying to cleanup.
  6852. // It still causes beep, so there's likely some popup, but it does not block
  6853. // log off.
  6854. ExitProcess(0);
  6855. }
  6856. TForm::Dispatch(&Message);
  6857. }
  6858. #ifdef _DEBUG
  6859. //---------------------------------------------------------------------------
  6860. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6861. {
  6862. TForm::Dispatch(&Message);
  6863. }
  6864. #endif
  6865. //---------------------------------------------------------------------------
  6866. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6867. {
  6868. }
  6869. //---------------------------------------------------------------------------
  6870. void __fastcall TCustomScpExplorerForm::DoShow()
  6871. {
  6872. // Called from TCustomScpExplorerForm::CMShowingChanged
  6873. // only now are the controls resized finally, so the size constraints
  6874. // will not conflict with possibly very small window size
  6875. RestoreFormParams();
  6876. // This indicates that the form was rescaled after parameters were loaded and
  6877. // as most probably the parameters were actually saved with the current form scaling
  6878. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6879. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6880. // and no rounding problems will occur.
  6881. // See also RestoreForm.
  6882. // (we should be safe to call RestoreParams unconditionally)
  6883. if (PixelsPerInch != Screen->PixelsPerInch)
  6884. {
  6885. RestoreParams();
  6886. }
  6887. FixControlsPlacement();
  6888. if (Position == poDefaultPosOnly)
  6889. {
  6890. CutFormToDesktop(this);
  6891. }
  6892. TForm::DoShow();
  6893. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6894. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6895. // This was previously in NeedSession, where other related code is.
  6896. // But that is called from CMShowingChanged, when the window is already visible and the local-local window state
  6897. // unpleasantly shows briefly.
  6898. if ((ManagedSession == NULL) &&
  6899. !WinConfiguration->ShowLoginWhenNoSession &&
  6900. WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  6901. // This detects if workspace was saved the last time the main widow was closed
  6902. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  6903. {
  6904. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false, true);
  6905. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  6906. }
  6907. // Before main window displays
  6908. if (ManagedSession == NULL)
  6909. {
  6910. StartingWithoutSession();
  6911. }
  6912. UpdatePixelsPerInchMainWindowCounter();
  6913. FShowing = true;
  6914. }
  6915. //---------------------------------------------------------------------------
  6916. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6917. {
  6918. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6919. }
  6920. //---------------------------------------------------------------------------
  6921. void __fastcall TCustomScpExplorerForm::StartingWithoutSession()
  6922. {
  6923. SessionListChanged();
  6924. InitStatusBar();
  6925. UpdateControls();
  6926. }
  6927. //---------------------------------------------------------------------------
  6928. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6929. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6930. TNotifyEvent OnBalloonClick, TObject * UserData)
  6931. {
  6932. bool Do;
  6933. UnicodeString Message;
  6934. if (E == NULL)
  6935. {
  6936. Message = Str;
  6937. Do = true;
  6938. }
  6939. else
  6940. {
  6941. Do = ExceptionMessage(E, Message);
  6942. }
  6943. if (Do && WinConfiguration->BalloonNotifications)
  6944. {
  6945. UnicodeString Title;
  6946. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6947. {
  6948. Message = TrimLeft(Message);
  6949. }
  6950. else
  6951. {
  6952. Message = UnformatMessage(Message);
  6953. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6954. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6955. Title = LoadResourceString(Captions[Type]);
  6956. if (Terminal != NULL)
  6957. {
  6958. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  6959. Title = TTerminalManager::Instance()->GetSessionTitle(Session, true) + TitleSeparator + Title;
  6960. }
  6961. }
  6962. if (Seconds == 0)
  6963. {
  6964. Seconds = WinConfiguration->NotificationsTimeout;
  6965. }
  6966. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6967. }
  6968. }
  6969. //---------------------------------------------------------------------------
  6970. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6971. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6972. UnicodeString HelpKeyword, const TMessageParams * Params,
  6973. TTerminal * Terminal)
  6974. {
  6975. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6976. (FOnFeedSynchronizeError != NULL)) &&
  6977. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6978. {
  6979. if (FOnFeedSynchronizeError != NULL)
  6980. {
  6981. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6982. }
  6983. else
  6984. {
  6985. DebugAssert(FErrorList != NULL);
  6986. TStringList * MoreMessagesCopy = NULL;
  6987. if (MoreMessages)
  6988. {
  6989. MoreMessagesCopy = new TStringList();
  6990. MoreMessagesCopy->Assign(MoreMessages);
  6991. }
  6992. FErrorList->AddObject(Message, MoreMessagesCopy);
  6993. }
  6994. PopupTrayBalloon(Terminal, Message, Type);
  6995. return ContinueAnswer(Answers);
  6996. }
  6997. else
  6998. {
  6999. bool UseBalloon = FTrayIcon->Visible;
  7000. if (UseBalloon)
  7001. {
  7002. PopupTrayBalloon(Terminal, Message, Type);
  7003. }
  7004. unsigned int Result;
  7005. try
  7006. {
  7007. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  7008. }
  7009. __finally
  7010. {
  7011. // cancel only balloon we popped up, otherwise we may cancel notification
  7012. // balloon that was there before the message dialog
  7013. // (such as "dd confirmation opt in balloon)
  7014. if (UseBalloon)
  7015. {
  7016. FTrayIcon->CancelBalloon();
  7017. }
  7018. }
  7019. return Result;
  7020. }
  7021. }
  7022. //---------------------------------------------------------------------------
  7023. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  7024. TTerminal * Terminal, Exception * E)
  7025. {
  7026. if (FTrayIcon->Visible)
  7027. {
  7028. PopupTrayBalloon(Terminal, L"", qtError, E);
  7029. }
  7030. // particularly prevent opening new session from jump list,
  7031. // while exception is shown
  7032. NonVisualDataModule->StartBusy();
  7033. try
  7034. {
  7035. ShowExtendedExceptionEx(Terminal, E);
  7036. }
  7037. __finally
  7038. {
  7039. NonVisualDataModule->EndBusy();
  7040. FTrayIcon->CancelBalloon();
  7041. }
  7042. }
  7043. //---------------------------------------------------------------------------
  7044. void __fastcall TCustomScpExplorerForm::SessionReady()
  7045. {
  7046. InitStatusBar();
  7047. // cannot rely on active page being page for active terminal,
  7048. // as it can happen that active page is the "new session" page
  7049. // (e.g. when reconnecting active terminal, while login dialog
  7050. // invoked from "new session" page is modal)
  7051. int ActiveSessionIndex = TTerminalManager::Instance()->ActiveSessionIndex;
  7052. UpdateSessionTab(SessionsPageControl->Pages[ActiveSessionIndex]);
  7053. }
  7054. //---------------------------------------------------------------------------
  7055. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  7056. TTerminal * Terminal, Exception * E)
  7057. {
  7058. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  7059. if (!Terminal->Active)
  7060. {
  7061. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  7062. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  7063. {
  7064. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  7065. }
  7066. }
  7067. }
  7068. //---------------------------------------------------------------------------
  7069. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  7070. UnicodeString Message, TQueryType Type,
  7071. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  7072. {
  7073. if ((E == NULL) ||
  7074. ExceptionMessage(E, Message))
  7075. {
  7076. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  7077. if (Important)
  7078. {
  7079. Seconds *= 5;
  7080. }
  7081. UnicodeString NoteMessage(UnformatMessage(Message));
  7082. if (Terminal != NULL)
  7083. {
  7084. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  7085. NoteMessage = FORMAT(L"%s: %s",
  7086. (TTerminalManager::Instance()->GetSessionTitle(Session, true), NoteMessage));
  7087. }
  7088. if (WinConfiguration->BalloonNotifications)
  7089. {
  7090. AddNote(NoteMessage);
  7091. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  7092. }
  7093. else
  7094. {
  7095. FlashOnBackground();
  7096. PostNote(NoteMessage, Seconds, OnClick, UserData);
  7097. }
  7098. }
  7099. }
  7100. //---------------------------------------------------------------------------
  7101. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  7102. {
  7103. RestoreApp();
  7104. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  7105. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  7106. !NonVisualDataModule->Busy)
  7107. {
  7108. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  7109. TTerminalManager::Instance()->ActiveSession = Terminal;
  7110. if (!ComponentVisible[fcQueueView])
  7111. {
  7112. ToggleQueueVisibility();
  7113. GoToQueue();
  7114. }
  7115. }
  7116. }
  7117. //---------------------------------------------------------------------------
  7118. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  7119. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  7120. {
  7121. UnicodeString Message;
  7122. TNotifyEvent OnClick = NULL;
  7123. TObject * UserData = NULL;
  7124. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  7125. switch (Event)
  7126. {
  7127. case qeEmptyButMonitored:
  7128. if ((ATerminal != Terminal) || QueueInvisible)
  7129. {
  7130. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  7131. OnClick = QueueEmptyNoteClicked;
  7132. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  7133. TerminalNoteData->Terminal = ATerminal;
  7134. UserData = TerminalNoteData;
  7135. }
  7136. break;
  7137. case qeEmpty:
  7138. OperationComplete(ATerminal->QueueOperationStart);
  7139. break;
  7140. case qePendingUserAction:
  7141. if ((ATerminal != Terminal) ||
  7142. (QueueInvisible && !IsQueueAutoPopup()))
  7143. {
  7144. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  7145. }
  7146. break;
  7147. default:
  7148. DebugFail();
  7149. }
  7150. if (!Message.IsEmpty())
  7151. {
  7152. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  7153. }
  7154. }
  7155. //---------------------------------------------------------------------------
  7156. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  7157. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  7158. {
  7159. if (FDDExtMapFile != NULL)
  7160. {
  7161. CloseHandle(FDDExtMapFile);
  7162. FDDExtMapFile = NULL;
  7163. }
  7164. if (WinConfiguration->DDFakeFile)
  7165. {
  7166. DDFakeFileInitDrag(FileList, Created);
  7167. }
  7168. }
  7169. //---------------------------------------------------------------------------
  7170. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  7171. {
  7172. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  7173. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  7174. {
  7175. FFakeFileDropTarget = FileName;
  7176. }
  7177. }
  7178. //---------------------------------------------------------------------------
  7179. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  7180. {
  7181. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  7182. if (!ForceDirectories(ApiPath(Result)))
  7183. {
  7184. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  7185. }
  7186. FileSetAttr(ApiPath(Result), faHidden);
  7187. return Result;
  7188. }
  7189. //---------------------------------------------------------------------------
  7190. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  7191. bool & Created)
  7192. {
  7193. FFakeFileDropTarget = UnicodeString();
  7194. FDragFakeDirectory = CreateFakeTransferDirectory();
  7195. FileList->AddItem(NULL, FDragFakeDirectory);
  7196. Created = true;
  7197. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  7198. {
  7199. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  7200. }
  7201. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  7202. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  7203. {
  7204. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7205. TDragExtCommStruct* CommStruct;
  7206. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7207. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7208. DebugAssert(CommStruct != NULL);
  7209. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  7210. CommStruct->Dragging = true;
  7211. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  7212. LENOF(CommStruct->DropDest));
  7213. NULL_TERMINATE(CommStruct->DropDest);
  7214. UnmapViewOfFile(CommStruct);
  7215. }
  7216. }
  7217. //---------------------------------------------------------------------------
  7218. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  7219. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  7220. {
  7221. bool Result;
  7222. DebugAssert(!IsLocalBrowserMode());
  7223. if (Sender == RemoteDirView)
  7224. {
  7225. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  7226. }
  7227. else
  7228. {
  7229. DebugAssert(Sender == RemoteDriveView);
  7230. TStrings * FileList = RemoteDriveView->DragFileList();
  7231. try
  7232. {
  7233. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  7234. }
  7235. __finally
  7236. {
  7237. delete FileList;
  7238. }
  7239. }
  7240. if (FDDTargetControl == RemoteDriveView)
  7241. {
  7242. RemoteDriveView->UpdateDropTarget();
  7243. }
  7244. // foRemoteMove happens when file/dir is dragged within the remote tree view
  7245. // or from tree view to dir view.
  7246. // foMove happens when file/dir is dragged from remote tree view outside of
  7247. // application via dragex
  7248. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  7249. (DropSourceControl == RemoteDriveView))
  7250. {
  7251. RemoteDriveView->UpdateDropSource();
  7252. }
  7253. return Result;
  7254. }
  7255. //---------------------------------------------------------------------------
  7256. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  7257. {
  7258. DebugAssert(!IsLocalBrowserMode());
  7259. // This also handles drops of remote files to queue.
  7260. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  7261. SAFE_DESTROY(FDDFileList);
  7262. if (!FFakeFileDropTarget.IsEmpty())
  7263. {
  7264. RemoveDir(ApiPath(FFakeFileDropTarget));
  7265. }
  7266. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7267. {
  7268. try
  7269. {
  7270. TDragResult DDResult = (Sender == RemoteDirView) ?
  7271. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7272. // Focus is moved to the target application,
  7273. // but as we are going to present the UI, we need to steal the focus back.
  7274. // This most likely won't work though (windows does not allow application
  7275. // to steal focus most of the time)
  7276. Application->BringToFront();
  7277. // On older version of Windows we never got drMove here, see also comment below.
  7278. // On Windows 10, we get the "move".
  7279. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7280. {
  7281. UnicodeString TargetDirectory;
  7282. TFileOperation Operation;
  7283. // drInvalid may mean drMove, see comment below
  7284. switch (DDResult)
  7285. {
  7286. case drCopy:
  7287. Operation = foCopy;
  7288. break;
  7289. case drMove:
  7290. Operation = foMove;
  7291. break;
  7292. default:
  7293. DebugFail();
  7294. case drInvalid:
  7295. // prefer "copy" for safety
  7296. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7297. break;
  7298. }
  7299. TTransferOperationParam Param;
  7300. UnicodeString CounterName;
  7301. bool ForceQueue;
  7302. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7303. {
  7304. // we get drInvalid both if d&d was intercepted by ddext,
  7305. // and when users drops on no-drop location.
  7306. // we tell the difference by existence of response from ddext,
  7307. // so we ignore absence of response in this case
  7308. if (DDResult != drInvalid)
  7309. {
  7310. // here we know that the extension is installed,
  7311. // as it is checked as soon as drag&drop starts from
  7312. // RemoteFileControlDDCreateDragFileList
  7313. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7314. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7315. {
  7316. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7317. }
  7318. else
  7319. {
  7320. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7321. }
  7322. }
  7323. }
  7324. else
  7325. {
  7326. // download using fake file
  7327. Param.Temp = false;
  7328. Param.DragDrop = true;
  7329. if (ForceQueue)
  7330. {
  7331. Param.Queue = asOn;
  7332. }
  7333. if (Sender == RemoteDirView)
  7334. {
  7335. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7336. }
  7337. if (RemoteFileControlFileOperation(Sender, Operation,
  7338. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7339. {
  7340. Configuration->Usage->Inc(CounterName);
  7341. }
  7342. }
  7343. }
  7344. }
  7345. __finally
  7346. {
  7347. CloseHandle(FDDExtMapFile);
  7348. FDDExtMapFile = NULL;
  7349. delete FDragFakeMonitors;
  7350. FDragFakeMonitors = NULL;
  7351. RemoveDir(ApiPath(FDragFakeDirectory));
  7352. FDragFakeDirectory = L"";
  7353. FFakeFileDropTarget = UnicodeString();
  7354. }
  7355. }
  7356. if (FDragDropSshTerminate.get() != NULL)
  7357. {
  7358. FDragDropSshTerminate->Rethrow();
  7359. }
  7360. }
  7361. //---------------------------------------------------------------------------
  7362. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7363. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7364. {
  7365. // Remember drop effect so we know (when user drops files), if we copy or move
  7366. FLastDropEffect = dwEffect;
  7367. }
  7368. //---------------------------------------------------------------------------
  7369. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7370. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7371. {
  7372. bool Result;
  7373. if (FDDTargetControl == QueueView3)
  7374. {
  7375. Directory = DefaultDownloadTargetDirectory();
  7376. Result = true;
  7377. CounterName = L"DownloadsDragDropQueue";
  7378. ForceQueue = true;
  7379. }
  7380. else if (!FFakeFileDropTarget.IsEmpty())
  7381. {
  7382. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7383. Result = true;
  7384. ForceQueue = false;
  7385. CounterName = L"DownloadsDragDropFakeFile";
  7386. }
  7387. else
  7388. {
  7389. ForceQueue = false;
  7390. Enabled = false;
  7391. try
  7392. {
  7393. int Timer = 0;
  7394. Result = false;
  7395. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7396. {
  7397. {
  7398. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7399. TDragExtCommStruct* CommStruct;
  7400. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7401. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7402. DebugAssert(CommStruct != NULL);
  7403. Result = !CommStruct->Dragging;
  7404. if (Result)
  7405. {
  7406. Directory = ExtractFilePath(CommStruct->DropDest);
  7407. CounterName = L"DownloadsDragDropExternalExt";
  7408. }
  7409. UnmapViewOfFile(CommStruct);
  7410. }
  7411. if (!Result)
  7412. {
  7413. Sleep(50);
  7414. Timer += 50;
  7415. Application->ProcessMessages();
  7416. }
  7417. }
  7418. }
  7419. __finally
  7420. {
  7421. Enabled = true;
  7422. }
  7423. }
  7424. return Result;
  7425. }
  7426. //---------------------------------------------------------------------------
  7427. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7428. const UnicodeString TempDir, int SecDelay)
  7429. {
  7430. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7431. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7432. if (FDelayedDeletionTimer == NULL)
  7433. {
  7434. DebugAssert(HandleAllocated());
  7435. FDelayedDeletionTimer = new TTimer(this);
  7436. FDelayedDeletionTimer->Interval = 10000;
  7437. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7438. }
  7439. else
  7440. {
  7441. FDelayedDeletionTimer->Enabled = true;
  7442. }
  7443. }
  7444. //---------------------------------------------------------------------------
  7445. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7446. {
  7447. DebugAssert(FDelayedDeletionList != NULL);
  7448. TDateTime N = Now();
  7449. TDateTime Alert;
  7450. UnicodeString Directory;
  7451. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7452. {
  7453. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7454. if ((N >= Alert) || (Sender == NULL))
  7455. {
  7456. Directory = FDelayedDeletionList->Strings[Index];
  7457. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7458. {
  7459. FDelayedDeletionList->Delete(Index);
  7460. }
  7461. }
  7462. }
  7463. if (FDelayedDeletionList->Count == 0)
  7464. {
  7465. FDelayedDeletionTimer->Enabled = false;
  7466. }
  7467. }
  7468. //---------------------------------------------------------------------------
  7469. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7470. {
  7471. DebugAssert(!IsLocalBrowserMode());
  7472. if (IsFileControl(FDDTargetControl, osRemote) ||
  7473. (FDDTargetControl == SessionsPageControl))
  7474. {
  7475. TManagedTerminal * TargetTerminal = NULL;
  7476. TFileOperation Operation = foNone;
  7477. if (FDDTargetControl == SessionsPageControl)
  7478. {
  7479. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7480. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7481. // do not allow dropping on local-local, disconnected and the "+" tab (see also SessionsDDProcessDropped)
  7482. TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  7483. if (IsActiveTerminal(TargetTerminal) && DebugAlwaysTrue(!TargetTerminal->LocalBrowser))
  7484. {
  7485. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7486. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7487. {
  7488. Operation = foRemoteMove;
  7489. }
  7490. else
  7491. {
  7492. Operation = foRemoteCopy;
  7493. }
  7494. }
  7495. }
  7496. else
  7497. {
  7498. if (FLastDropEffect == DROPEFFECT_MOVE)
  7499. {
  7500. Operation = foRemoteMove;
  7501. }
  7502. else if (FLastDropEffect == DROPEFFECT_COPY)
  7503. {
  7504. Operation = foRemoteCopy;
  7505. }
  7506. }
  7507. if (Operation != foNone)
  7508. {
  7509. RemoteFileControlFileOperation(DropSourceControl,
  7510. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7511. }
  7512. // abort drag&drop
  7513. Abort();
  7514. }
  7515. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7516. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7517. (FDDTargetControl != QueueView3))
  7518. {
  7519. DebugAssert(!FDragTempDir.IsEmpty());
  7520. TTransferType Type;
  7521. UnicodeString TempDir = FDragTempDir;
  7522. // We clear FDragTempDir before calling
  7523. // just in case it fail (raises exception)
  7524. FDragTempDir = L"";
  7525. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7526. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7527. // empty directory parameter means temp directory -> don't display it!
  7528. UnicodeString TargetDir = L"";
  7529. int Options = 0;
  7530. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7531. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7532. {
  7533. Abort();
  7534. }
  7535. // TargetDir is set when dropped on local file control
  7536. // (this was workaround for legacy dirview event handling, now it should be
  7537. // made prettier)
  7538. if (TargetDir.IsEmpty())
  7539. {
  7540. TargetDir = TempDir;
  7541. if (ForceDirectories(ApiPath(TargetDir)))
  7542. {
  7543. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7544. FPendingTempSpaceWarn = true;
  7545. try
  7546. {
  7547. FDragDropOperation = true;
  7548. int Params = cpTemporary |
  7549. (Type == ttMove ? cpDelete : 0);
  7550. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7551. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7552. }
  7553. __finally
  7554. {
  7555. FDragDropOperation = false;
  7556. FPendingTempSpaceWarn = false;
  7557. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7558. }
  7559. }
  7560. else
  7561. {
  7562. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7563. }
  7564. }
  7565. }
  7566. }
  7567. //---------------------------------------------------------------------------
  7568. void __fastcall TCustomScpExplorerForm::DDDownload(
  7569. TStrings * FilesToCopy, const UnicodeString & TargetDir, TCopyParamType * CopyParam, int Params)
  7570. {
  7571. DebugAssert(!IsLocalBrowserMode());
  7572. TAutoBatch AutoBatch(this);
  7573. CopyParam->IncludeFileMask.SetRoots(TargetDir, FilesToCopy);
  7574. UpdateCopyParamCounters(*CopyParam);
  7575. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7576. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7577. {
  7578. RemoteDriveView->UpdateDropSource();
  7579. }
  7580. }
  7581. //---------------------------------------------------------------------------
  7582. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7583. {
  7584. public:
  7585. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7586. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7587. {
  7588. }
  7589. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7590. {
  7591. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7592. TDataObjectFilesEx::AllowData(FormatEtc);
  7593. }
  7594. };
  7595. //---------------------------------------------------------------------------
  7596. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7597. TObject * Sender, TDataObject *& DataObject)
  7598. {
  7599. if (FDDExtMapFile != NULL)
  7600. {
  7601. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7602. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7603. {
  7604. Abort();
  7605. }
  7606. if (FileList->Count > 0)
  7607. {
  7608. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7609. if (!FilesObject->IsValid(true, true))
  7610. {
  7611. FilesObject->_Release();
  7612. }
  7613. else
  7614. {
  7615. DataObject = FilesObject;
  7616. }
  7617. }
  7618. }
  7619. }
  7620. //---------------------------------------------------------------------------
  7621. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7622. {
  7623. DebugFail();
  7624. }
  7625. //---------------------------------------------------------------------------
  7626. void __fastcall TCustomScpExplorerForm::GoToTree()
  7627. {
  7628. ComponentVisible[fcRemoteTree] = true;
  7629. DriveView(osRemote)->SetFocus();
  7630. }
  7631. //---------------------------------------------------------------------------
  7632. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7633. TPanelExport Export)
  7634. {
  7635. TCustomDirView * DirView = this->DirView(Side);
  7636. std::unique_ptr<TStrings> ExportData(new TStringList());
  7637. switch (Export)
  7638. {
  7639. case pePath:
  7640. ExportData->Add(DirView->PathName);
  7641. break;
  7642. case peFileList:
  7643. case peFullFileList:
  7644. {
  7645. bool FullPath = (Export == peFullFileList);
  7646. DirView->CreateFileList(false, FullPath, ExportData.get());
  7647. for (int Index = 0; Index < ExportData->Count; Index++)
  7648. {
  7649. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7650. {
  7651. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7652. }
  7653. }
  7654. }
  7655. break;
  7656. default:
  7657. DebugFail();
  7658. }
  7659. return ExportData.release();
  7660. }
  7661. //---------------------------------------------------------------------------
  7662. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7663. TPanelExport Export, TPanelExportDestination Destination)
  7664. {
  7665. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7666. PanelExportStore(Side, Export, Destination, ExportData.get());
  7667. }
  7668. //---------------------------------------------------------------------------
  7669. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7670. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7671. TStrings * ExportData)
  7672. {
  7673. if (Destination == pedClipboard)
  7674. {
  7675. TInstantOperationVisualizer Visualizer;
  7676. CopyToClipboard(ExportData);
  7677. }
  7678. else
  7679. {
  7680. DebugFail();
  7681. }
  7682. }
  7683. //---------------------------------------------------------------------------
  7684. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7685. {
  7686. TCustomDirView * DirView = this->DirView(Side);
  7687. UnicodeString Mask = DirView->Mask;
  7688. if (DoFilterMaskDialog(DirView, Mask))
  7689. {
  7690. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7691. Configuration->Usage->Inc(L"Filters");
  7692. }
  7693. }
  7694. //---------------------------------------------------------------------------
  7695. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7696. {
  7697. return FQueueController->DefaultOperation();
  7698. }
  7699. //---------------------------------------------------------------------------
  7700. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7701. TQueueOperation Operation, void ** Param)
  7702. {
  7703. switch (Operation)
  7704. {
  7705. case qoPreferences:
  7706. return true;
  7707. case qoGoTo:
  7708. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7709. case qoOnceEmpty:
  7710. return IsAnythingQueued();
  7711. default:
  7712. return FQueueController->AllowOperation(Operation, Param);
  7713. }
  7714. }
  7715. //---------------------------------------------------------------------------
  7716. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7717. {
  7718. if (DebugAlwaysTrue(QueueView3->Visible))
  7719. {
  7720. QueueView3->SetFocus();
  7721. }
  7722. }
  7723. //---------------------------------------------------------------------------
  7724. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7725. TQueueOperation Operation, void * Param)
  7726. {
  7727. if (Operation == qoGoTo)
  7728. {
  7729. GoToQueue();
  7730. }
  7731. else if (Operation == qoPreferences)
  7732. {
  7733. PreferencesDialog(pmQueue);
  7734. }
  7735. else
  7736. {
  7737. FQueueController->ExecuteOperation(Operation, Param);
  7738. }
  7739. }
  7740. //---------------------------------------------------------------------------
  7741. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7742. {
  7743. return (Queue != NULL) && Queue->Enabled;
  7744. }
  7745. //---------------------------------------------------------------------------
  7746. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7747. {
  7748. DebugAssert(Queue != NULL);
  7749. if (Queue != NULL)
  7750. {
  7751. Queue->Enabled = !Queue->Enabled;
  7752. }
  7753. }
  7754. //---------------------------------------------------------------------------
  7755. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7756. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7757. {
  7758. FQueueActedItem = QueueView3->ItemFocused;
  7759. }
  7760. //---------------------------------------------------------------------------
  7761. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7762. {
  7763. return TopDock->Height + QueueSplitter->Height;
  7764. }
  7765. //---------------------------------------------------------------------------
  7766. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  7767. {
  7768. return
  7769. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  7770. (QueueDock->Visible ? QueueDock->Height : 0) +
  7771. (QueueLabel->Visible ? QueueLabel->Height : 0);
  7772. }
  7773. //---------------------------------------------------------------------------
  7774. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  7775. {
  7776. return ScaleByTextHeight(this, 48);
  7777. }
  7778. //---------------------------------------------------------------------------
  7779. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7780. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7781. {
  7782. // when queue is hidden by double-clicking splitter, stray attempt to
  7783. // resize the panel with strange value arrives, make sure it is ignored
  7784. if (ComponentVisible[fcQueueView])
  7785. {
  7786. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  7787. if (NewSize > MaxHeightLimit)
  7788. {
  7789. NewSize = MaxHeightLimit;
  7790. }
  7791. int MinHeightLimit =
  7792. GetStaticQueuePanelComponentsHeight() +
  7793. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  7794. GetMinQueueViewHeight();
  7795. if (NewSize < MinHeightLimit)
  7796. {
  7797. NewSize = MinHeightLimit;
  7798. }
  7799. }
  7800. else
  7801. {
  7802. Accept = false;
  7803. }
  7804. }
  7805. //---------------------------------------------------------------------------
  7806. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  7807. {
  7808. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  7809. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  7810. if (QueueViewHeight < GetMinQueueViewHeight())
  7811. {
  7812. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  7813. }
  7814. }
  7815. //---------------------------------------------------------------------------
  7816. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7817. TDragObject *& /*DragObject*/)
  7818. {
  7819. FQueueActedItem = QueueView3->ItemFocused;
  7820. }
  7821. //---------------------------------------------------------------------------
  7822. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7823. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7824. {
  7825. Accept = true;
  7826. if (Source == QueueView3)
  7827. {
  7828. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7829. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7830. if (Accept)
  7831. {
  7832. TQueueItemProxy * QueueItem;
  7833. TQueueItemProxy * DestQueueItem;
  7834. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7835. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7836. Accept = (QueueItem != DestQueueItem) &&
  7837. (QueueItem->Status == TQueueItem::qsPending) &&
  7838. (DestQueueItem->Status == TQueueItem::qsPending);
  7839. }
  7840. }
  7841. }
  7842. //---------------------------------------------------------------------------
  7843. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7844. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7845. {
  7846. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7847. {
  7848. TQueueItemProxy * QueueItem;
  7849. TQueueItemProxy * DestQueueItem;
  7850. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7851. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7852. QueueItem->Move(DestQueueItem);
  7853. }
  7854. }
  7855. //---------------------------------------------------------------------------
  7856. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7857. {
  7858. if ((QueueView3->ItemFocused == NULL) &&
  7859. (QueueView3->Items->Count > 0))
  7860. {
  7861. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7862. }
  7863. QueueLabelUpdateStatus();
  7864. }
  7865. //---------------------------------------------------------------------------
  7866. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7867. {
  7868. QueueLabelUpdateStatus();
  7869. }
  7870. //---------------------------------------------------------------------------
  7871. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7872. {
  7873. QueueLabel->UpdateStatus();
  7874. }
  7875. //---------------------------------------------------------------------------
  7876. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7877. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7878. {
  7879. if (Selected)
  7880. {
  7881. NonVisualDataModule->UpdateNonVisibleActions();
  7882. }
  7883. }
  7884. //---------------------------------------------------------------------------
  7885. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7886. {
  7887. TDragDropFilesEx * Result = NULL;
  7888. if (Sender == SessionsPageControl)
  7889. {
  7890. Result = FSessionsDragDropFilesEx;
  7891. }
  7892. else if (Sender == QueueView3)
  7893. {
  7894. Result = FQueueDragDropFilesEx;
  7895. }
  7896. else
  7897. {
  7898. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7899. if (DirView != NULL)
  7900. {
  7901. Result = DirView->DragDropFilesEx;
  7902. }
  7903. else
  7904. {
  7905. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7906. if (DriveView != NULL)
  7907. {
  7908. Result = DriveView->DragDropFilesEx;
  7909. }
  7910. }
  7911. }
  7912. DebugAssert(Result != NULL);
  7913. return Result;
  7914. }
  7915. //---------------------------------------------------------------------------
  7916. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7917. {
  7918. return (DView->SelCount == DView->FilesCount);
  7919. }
  7920. //---------------------------------------------------------------------------
  7921. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7922. {
  7923. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7924. }
  7925. //---------------------------------------------------------------------------
  7926. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7927. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7928. {
  7929. DebugAssert(!IsLocalBrowserMode());
  7930. TFileOperation Operation;
  7931. switch (Effect)
  7932. {
  7933. case DROPEFFECT_MOVE:
  7934. Operation = foMove;
  7935. break;
  7936. case DROPEFFECT_COPY:
  7937. // occures on WinXP (reported by user)
  7938. default:
  7939. Operation = foCopy;
  7940. break;
  7941. };
  7942. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7943. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7944. if (DragDropFilesEx->FileList->Count > 0)
  7945. {
  7946. TStrings * FileList = new TStringList();
  7947. try
  7948. {
  7949. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7950. {
  7951. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7952. }
  7953. FDragDropOperation = true;
  7954. TTransferOperationParam Param;
  7955. Param.TargetDirectory = TargetPath;
  7956. // upload, no temp dirs
  7957. Param.Temp = false;
  7958. Param.DragDrop = DragDrop;
  7959. Param.Options =
  7960. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7961. if (ForceQueue)
  7962. {
  7963. Param.Queue = asOn;
  7964. }
  7965. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7966. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7967. {
  7968. if (IsFileControl(DropSourceControl, osLocal))
  7969. {
  7970. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7971. }
  7972. else
  7973. {
  7974. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7975. }
  7976. }
  7977. }
  7978. __finally
  7979. {
  7980. FDragDropOperation = false;
  7981. delete FileList;
  7982. }
  7983. }
  7984. }
  7985. //---------------------------------------------------------------------------
  7986. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7987. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7988. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7989. {
  7990. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7991. }
  7992. //---------------------------------------------------------------------------
  7993. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7994. TObject * Sender, int grfKeyState, int & dwEffect)
  7995. {
  7996. DebugAssert(!IsLocalBrowserMode());
  7997. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7998. if ((dwEffect != DROPEFFECT_NONE) &&
  7999. IsFileControl(DropSourceControl, osRemote))
  8000. {
  8001. // do not allow drop on remote panel (on free space, still allow drop on directories)
  8002. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  8003. (RemoteDirView->DropTarget == NULL))
  8004. {
  8005. dwEffect = DROPEFFECT_NONE;
  8006. }
  8007. else
  8008. {
  8009. if (dwEffect == DROPEFFECT_COPY)
  8010. {
  8011. bool MoveCapable = Terminal->IsCapable[fcRemoteMove];
  8012. // currently we support copying always (at least via temporary directory);
  8013. // remove associated checks once this all proves stable and working
  8014. bool CopyCapable = true;
  8015. // if we do not support neither of operations, there's no discussion
  8016. if (!MoveCapable && !CopyCapable)
  8017. {
  8018. dwEffect = DROPEFFECT_NONE;
  8019. }
  8020. else
  8021. {
  8022. // The default effect inside remote panel is move,
  8023. // unless we do not support it, but support copy
  8024. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  8025. {
  8026. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  8027. }
  8028. else
  8029. {
  8030. // with ctrl-down, we want copy unless it is not supported
  8031. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  8032. }
  8033. }
  8034. }
  8035. }
  8036. }
  8037. }
  8038. //---------------------------------------------------------------------------
  8039. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  8040. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  8041. {
  8042. DebugAssert(!IsLocalBrowserMode());
  8043. if (FDDTotalSize >= 0)
  8044. {
  8045. if (File->IsDirectory)
  8046. {
  8047. FDDTotalSize = -1;
  8048. }
  8049. else
  8050. {
  8051. FDDTotalSize += File->Size;
  8052. }
  8053. }
  8054. DebugAssert(!FDragTempDir.IsEmpty());
  8055. // TODO: this is quite ineffective
  8056. // TODO: what if invalid character replacement is disabled?
  8057. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  8058. UnicodeString TransferFileName;
  8059. if (Sender == RemoteDriveView)
  8060. {
  8061. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  8062. }
  8063. else
  8064. {
  8065. TransferFileName = File->FileName;
  8066. }
  8067. FDDFileList->AddObject(TransferFileName, File);
  8068. }
  8069. //---------------------------------------------------------------------------
  8070. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  8071. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  8072. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  8073. {
  8074. // sometimes we do not get DDEnd so the list is not released
  8075. SAFE_DESTROY(FDDFileList);
  8076. FDDFileList = new TStringList();
  8077. FDragTempDir = WinConfiguration->TemporaryDir();
  8078. FDDTotalSize = 0;
  8079. FDragDropSshTerminate.reset(NULL);
  8080. }
  8081. //---------------------------------------------------------------------------
  8082. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  8083. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  8084. HRESULT & Result)
  8085. {
  8086. if (Result == DRAGDROP_S_DROP)
  8087. {
  8088. try
  8089. {
  8090. GlobalDragImageList->HideDragImage();
  8091. try
  8092. {
  8093. RemoteFileControlDDTargetDrop();
  8094. }
  8095. catch(ESshTerminate & E)
  8096. {
  8097. FDragDropSshTerminate.reset(E.Clone());
  8098. }
  8099. }
  8100. catch (Exception &E)
  8101. {
  8102. // If downloading fails we need to cancel drag&drop, otherwise
  8103. // Explorer shows error
  8104. // But by the way exception probably never reach this point as
  8105. // it's catched on way.
  8106. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  8107. Result = DRAGDROP_S_CANCEL;
  8108. ShowExtendedException(Terminal, &E);
  8109. }
  8110. }
  8111. }
  8112. //---------------------------------------------------------------------------
  8113. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  8114. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  8115. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  8116. {
  8117. TFileMasks::TParams MaskParams;
  8118. MaskParams.Size = Size;
  8119. MaskParams.Modification = Modification;
  8120. // this does not re-parse the mask if it is the same as the last time
  8121. FDirViewMatchMask = Masks;
  8122. bool ImplicitMatch;
  8123. Matches =
  8124. // Local and RecurseInclude parameters have no effect as there's no path.
  8125. // This should use potential MatchesFileName overload with ImplicitMatch argument.
  8126. FDirViewMatchMask.Matches(FileName, false, Directory, &MaskParams, true, ImplicitMatch) &&
  8127. (AllowImplicitMatches || !ImplicitMatch);
  8128. }
  8129. //---------------------------------------------------------------------------
  8130. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  8131. TObject * Sender, TListItem * Item, WORD & Indexes)
  8132. {
  8133. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8134. UnicodeString Ext;
  8135. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  8136. {
  8137. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  8138. }
  8139. else
  8140. {
  8141. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  8142. }
  8143. if (SameText(Ext, Configuration->PartialExt))
  8144. {
  8145. Indexes |= oiPartial;
  8146. }
  8147. }
  8148. //---------------------------------------------------------------------------
  8149. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  8150. {
  8151. return
  8152. DirViewEnabled(osCurrent) &&
  8153. DirView(osCurrent)->CanPasteFromClipBoard();
  8154. }
  8155. //---------------------------------------------------------------------------
  8156. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  8157. {
  8158. bool Result = false;
  8159. if (CanPasteToDirViewFromClipBoard())
  8160. {
  8161. Result = true;
  8162. }
  8163. else
  8164. {
  8165. UnicodeString ClipboardText;
  8166. if (NonEmptyTextFromClipboard(ClipboardText) &&
  8167. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  8168. {
  8169. if (StoredSessions->IsUrl(ClipboardText))
  8170. {
  8171. Result = true;
  8172. }
  8173. else
  8174. {
  8175. Result = DirViewEnabled(osCurrent);
  8176. }
  8177. }
  8178. }
  8179. return Result;
  8180. }
  8181. //---------------------------------------------------------------------------
  8182. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  8183. {
  8184. // Pasting to any local panel is handled in TScpCommanderForm::PasteFromClipBoard()
  8185. if (DoesClipboardContainOurFiles())
  8186. {
  8187. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  8188. {
  8189. DebugAssert(!IsLocalBrowserMode());
  8190. TTerminalManager * Manager = TTerminalManager::Instance();
  8191. TTerminal * TargetTerminal = Manager->ActiveTerminal;
  8192. Manager->ActiveSession = FClipboardTerminal;
  8193. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TargetTerminal);
  8194. }
  8195. }
  8196. else if (CanPasteToDirViewFromClipBoard())
  8197. {
  8198. DirView(osCurrent)->PasteFromClipBoard();
  8199. }
  8200. else
  8201. {
  8202. UnicodeString ClipboardText;
  8203. if (NonEmptyTextFromClipboard(ClipboardText))
  8204. {
  8205. if (StoredSessions->IsUrl(ClipboardText))
  8206. {
  8207. NewSession(ClipboardText);
  8208. }
  8209. else
  8210. {
  8211. DirView(osCurrent)->Path = ClipboardText;
  8212. }
  8213. }
  8214. }
  8215. }
  8216. //---------------------------------------------------------------------------
  8217. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  8218. {
  8219. // TBD
  8220. }
  8221. //---------------------------------------------------------------------------
  8222. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  8223. {
  8224. TCustomDirView * ADirView = DirView(Side);
  8225. ADirView->SelectAll(Mode);
  8226. ADirView->SetFocus();
  8227. }
  8228. //---------------------------------------------------------------------------
  8229. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  8230. {
  8231. TCustomDirView * ADirView = DirView(Side);
  8232. TFileFilter Filter;
  8233. DefaultFileFilter(Filter);
  8234. if (DoSelectMaskDialog(ADirView, Select, Filter))
  8235. {
  8236. Configuration->Usage->Inc(L"MaskSelections");
  8237. ADirView->SelectFiles(Filter, Select);
  8238. ADirView->SetFocus();
  8239. }
  8240. }
  8241. //---------------------------------------------------------------------------
  8242. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  8243. {
  8244. TCustomDirView * ADirView = DirView(Side);
  8245. ADirView->RestoreSelectedNames();
  8246. ADirView->SetFocus();
  8247. }
  8248. //---------------------------------------------------------------------------
  8249. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  8250. {
  8251. TCustomDirView * CurrentDirView = DirView(osCurrent);
  8252. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  8253. {
  8254. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  8255. UnicodeString Ext;
  8256. if (!IsSideLocalBrowser(osCurrent))
  8257. {
  8258. Ext = UnixExtractFileExt(FileName);
  8259. }
  8260. else
  8261. {
  8262. Ext = ExtractFileExt(FileName);
  8263. }
  8264. if (Ext.IsEmpty())
  8265. {
  8266. Ext = L".";
  8267. }
  8268. TFileFilter Filter;
  8269. Filter.Masks = FORMAT(L"*%s", (Ext));
  8270. Filter.Directories = false;
  8271. CurrentDirView->SelectFiles(Filter, Select);
  8272. }
  8273. }
  8274. //---------------------------------------------------------------------------
  8275. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8276. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8277. {
  8278. UnicodeString Result;
  8279. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  8280. {
  8281. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  8282. }
  8283. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8284. {
  8285. // noop
  8286. }
  8287. else
  8288. {
  8289. Result =
  8290. FMTLOAD(FILE_INFO_FORMAT,
  8291. (FormatBytes(FileInfo.SelectedSize),
  8292. FormatBytes(FileInfo.FilesSize),
  8293. FormatNumber(FileInfo.SelectedCount),
  8294. FormatNumber(FileInfo.FilesCount)));
  8295. }
  8296. return Result;
  8297. }
  8298. //---------------------------------------------------------------------------
  8299. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8300. TTBXStatusPanel * Panel, TOperationSide Side)
  8301. {
  8302. if (Panel->Index == 1)
  8303. {
  8304. ToggleShowHiddenFiles();
  8305. }
  8306. else if (Panel->Index == 2)
  8307. {
  8308. Filter(Side);
  8309. }
  8310. }
  8311. //---------------------------------------------------------------------------
  8312. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8313. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8314. {
  8315. DebugAssert(!StatusBar->SimplePanel);
  8316. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8317. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8318. }
  8319. //---------------------------------------------------------------------------
  8320. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8321. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8322. {
  8323. DebugAssert(StatusBar->Panels->Count >= 3);
  8324. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8325. if (FileInfo.HiddenCount > 0)
  8326. {
  8327. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8328. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8329. }
  8330. else
  8331. {
  8332. HiddenFilesPanel->ViewPriority = 0;
  8333. // not really necessary, just to cleanup no-longer-valid data
  8334. HiddenFilesPanel->Caption = L"";
  8335. }
  8336. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8337. if (FileInfo.FilteredCount > 0)
  8338. {
  8339. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8340. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8341. }
  8342. else
  8343. {
  8344. FilteredFilesPanel->ViewPriority = 0;
  8345. // not really necessary, just to cleanup no-longer-valid data
  8346. FilteredFilesPanel->Caption = L"";
  8347. }
  8348. }
  8349. //---------------------------------------------------------------------------
  8350. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8351. TObject * /*Sender*/)
  8352. {
  8353. if (DirView(osOther)->Enabled)
  8354. {
  8355. DirView(osOther)->SetFocus();
  8356. }
  8357. }
  8358. //---------------------------------------------------------------------------
  8359. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8360. {
  8361. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8362. switch (Config.Show)
  8363. {
  8364. case qvShow:
  8365. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8366. {
  8367. Config.Show = qvHide;
  8368. }
  8369. else
  8370. {
  8371. Config.Show = Config.LastHideShow;
  8372. }
  8373. break;
  8374. case qvHideWhenEmpty:
  8375. if (ComponentVisible[fcQueueView])
  8376. {
  8377. Config.Show = qvHide;
  8378. }
  8379. else
  8380. {
  8381. Config.LastHideShow = Config.Show;
  8382. Config.Show = qvShow;
  8383. }
  8384. break;
  8385. case qvHide:
  8386. Config.LastHideShow = Config.Show;
  8387. Config.Show = qvShow;
  8388. break;
  8389. }
  8390. WinConfiguration->QueueView = Config;
  8391. }
  8392. //---------------------------------------------------------------------------
  8393. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8394. {
  8395. UnicodeString Result;
  8396. if (Terminal != NULL)
  8397. {
  8398. switch (WinConfiguration->PathInCaption)
  8399. {
  8400. case picShort:
  8401. Result = ExtractShortName(Terminal->CurrentDirectory, true);
  8402. break;
  8403. case picFull:
  8404. Result = Terminal->CurrentDirectory;
  8405. break;
  8406. }
  8407. }
  8408. return Result;
  8409. }
  8410. //---------------------------------------------------------------------------
  8411. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8412. {
  8413. TColor Result = GetWindowColor(FSessionColor);
  8414. return Result;
  8415. }
  8416. //---------------------------------------------------------------------------
  8417. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8418. {
  8419. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8420. return Result;
  8421. }
  8422. //---------------------------------------------------------------------------
  8423. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8424. {
  8425. if (!FSessionChanging)
  8426. {
  8427. TTerminalManager::Instance()->UpdateAppTitle();
  8428. }
  8429. // WORKAROUND: Disabling list view when it is not showing yet does not set its
  8430. // background to gray on Windows 7 (works on Windows 10).
  8431. // See also EnableControl
  8432. if (Showing)
  8433. {
  8434. if (ManagedSession != NULL)
  8435. {
  8436. // Update path when it changes
  8437. if ((SessionsPageControl->ActivePage != NULL) &&
  8438. (GetSessionTabSession(SessionsPageControl->ActivePage) == ManagedSession))
  8439. {
  8440. UpdateSessionTab(SessionsPageControl->ActivePage);
  8441. }
  8442. }
  8443. NonVisualDataModule->ReconnectSessionAction->Update();
  8444. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8445. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8446. CenterReconnectToolbar();
  8447. bool HasTerminal = HasActiveTerminal();
  8448. RemoteDirView->Enabled = HasTerminal;
  8449. RemoteDirView->Color = HasTerminal ? PanelColor() : DisabledPanelColor();
  8450. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8451. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8452. RemoteDriveView->Color = RemoteDirView->Color;
  8453. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8454. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8455. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8456. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8457. QueueFileList->Enabled = QueueView3->Enabled;
  8458. QueueFileList->Color = QueueView3->Color;
  8459. QueueFileList->Font->Color = QueueView3->Font->Color;
  8460. QueueLabelUpdateStatus();
  8461. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8462. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8463. if (FImmersiveDarkMode != WinConfiguration->UseDarkTheme())
  8464. {
  8465. UpdateDarkMode();
  8466. }
  8467. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal || IsLocalBrowserMode();
  8468. }
  8469. }
  8470. //---------------------------------------------------------------------------
  8471. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8472. {
  8473. UpdateControls();
  8474. }
  8475. //---------------------------------------------------------------------------
  8476. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8477. TObject * Sender)
  8478. {
  8479. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8480. DebugAssert(DirView != NULL);
  8481. DoDirViewLoaded(DirView);
  8482. TransferPresetAutoSelect();
  8483. }
  8484. //---------------------------------------------------------------------------
  8485. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8486. {
  8487. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8488. // first run after installation
  8489. if (double(Updates.LastCheck) == 0)
  8490. {
  8491. // make sure next time there will be an update (if enabled)
  8492. Updates.LastCheck = TDateTime(1);
  8493. WinConfiguration->Updates = Updates;
  8494. }
  8495. else if ((double(Updates.Period) > 0) &&
  8496. (Now() - Updates.LastCheck >= Updates.Period))
  8497. {
  8498. TThreadMethod OnUpdatesChecked = NULL;
  8499. if (!IsUWP())
  8500. {
  8501. OnUpdatesChecked = UpdatesChecked;
  8502. }
  8503. StartUpdateThread(OnUpdatesChecked);
  8504. }
  8505. }
  8506. //---------------------------------------------------------------------------
  8507. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8508. {
  8509. UnicodeString Message;
  8510. bool New;
  8511. TQueryType Type;
  8512. GetUpdatesMessage(Message, New, Type, false);
  8513. if (!Message.IsEmpty())
  8514. {
  8515. if (New)
  8516. {
  8517. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8518. }
  8519. if (!New && (Type != qtWarning))
  8520. {
  8521. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8522. }
  8523. else
  8524. {
  8525. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8526. }
  8527. if (New)
  8528. {
  8529. Configuration->Usage->Inc(L"UpdateNotifications");
  8530. }
  8531. }
  8532. }
  8533. //---------------------------------------------------------------------------
  8534. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8535. {
  8536. RestoreApp();
  8537. bool CanDisplay = !NonVisualDataModule->Busy;
  8538. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8539. {
  8540. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8541. }
  8542. if (CanDisplay)
  8543. {
  8544. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8545. CheckForUpdates(true);
  8546. }
  8547. else
  8548. {
  8549. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8550. }
  8551. }
  8552. //---------------------------------------------------------------------------
  8553. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8554. TCopyParamRuleData & Data)
  8555. {
  8556. DebugAssert(Terminal != NULL);
  8557. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8558. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8559. Data.RemoteDirectory = RemoteDirView->PathName;
  8560. }
  8561. //---------------------------------------------------------------------------
  8562. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8563. {
  8564. // Terminal can be null when we are changing local directory implicitly,
  8565. // because it has been deleted, while no session is connected
  8566. // (Login dialog is open)
  8567. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8568. {
  8569. DebugAssert(!IsLocalBrowserMode());
  8570. TCopyParamRuleData Data;
  8571. GetTransferPresetAutoSelectData(Data);
  8572. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8573. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8574. if (CopyParamIndex < 0)
  8575. {
  8576. // there is no preset that matches autoselection
  8577. // set preset that we consider "default"
  8578. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8579. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8580. }
  8581. else
  8582. {
  8583. // there is preset matching autoselection
  8584. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8585. if (CopyParamName == FCopyParamAutoSelected)
  8586. {
  8587. // autoselected the same preset as the last time
  8588. // make no change (i.e. preserve custom user preset, if any)
  8589. }
  8590. else
  8591. {
  8592. // autoselected the different preset then the last time (or there
  8593. // was default preset set)
  8594. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8595. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8596. }
  8597. }
  8598. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8599. {
  8600. if (CopyParamIndex >= 0)
  8601. {
  8602. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8603. }
  8604. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8605. try
  8606. {
  8607. int Fmt =
  8608. (CopyParamIndex < 0) ?
  8609. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8610. COPY_PARAM_AUTOSELECTED;
  8611. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8612. Data->Message = MainInstructions(Message);
  8613. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8614. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8615. CopyParamAttrs);
  8616. if (CopyParamIndex >= 0)
  8617. {
  8618. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8619. Info = FORMAT(L"%s\n \n%s", (Info,
  8620. FMTLOAD(COPY_PARAM_RULE,
  8621. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8622. }
  8623. Data->Message += L"\n\n" + Info;
  8624. if (WinConfiguration->CopyParamAutoSelectNotice)
  8625. {
  8626. TransferPresetNoteMessage(Data, true);
  8627. }
  8628. else
  8629. {
  8630. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8631. Data = NULL; // ownership passed
  8632. }
  8633. }
  8634. __finally
  8635. {
  8636. delete Data;
  8637. }
  8638. }
  8639. }
  8640. }
  8641. //---------------------------------------------------------------------------
  8642. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8643. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8644. {
  8645. DebugAssert(NoteData != NULL);
  8646. DebugAssert(!IsLocalBrowserMode());
  8647. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8648. TQueryButtonAlias Aliases[1];
  8649. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8650. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8651. Params.Aliases = Aliases;
  8652. Params.AliasesCount = LENOF(Aliases);
  8653. unsigned int Result =
  8654. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8655. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8656. switch (Result)
  8657. {
  8658. case qaNeverAskAgain:
  8659. DebugAssert(AllowNeverAskAgain);
  8660. WinConfiguration->CopyParamAutoSelectNotice = false;
  8661. break;
  8662. case qaIgnore:
  8663. PreferencesDialog(pmPresets);
  8664. break;
  8665. }
  8666. }
  8667. //---------------------------------------------------------------------------
  8668. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8669. {
  8670. // as of now useless, as this is used for notes only, never for balloons, ...
  8671. RestoreApp();
  8672. // .. and we should never be busy here
  8673. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8674. {
  8675. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8676. }
  8677. }
  8678. //---------------------------------------------------------------------------
  8679. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8680. TPreferencesMode APreferencesMode)
  8681. {
  8682. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8683. TCopyParamRuleData Data;
  8684. if (Terminal != NULL)
  8685. {
  8686. PreferencesData.reset(new TPreferencesDialogData());
  8687. GetTransferPresetAutoSelectData(Data);
  8688. PreferencesData->CopyParamRuleData = &Data;
  8689. }
  8690. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8691. }
  8692. //---------------------------------------------------------------------------
  8693. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8694. const TCustomCommandType & Command)
  8695. {
  8696. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8697. {
  8698. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  8699. }
  8700. }
  8701. //---------------------------------------------------------------------------
  8702. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8703. {
  8704. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8705. TCustomCommandType Command;
  8706. // make sure we use local custom command when remote are not supported
  8707. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8708. {
  8709. Command = FLastCustomCommand;
  8710. }
  8711. else
  8712. {
  8713. Command.Params = Command.Params | ccLocal;
  8714. }
  8715. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8716. FEditingFocusedAdHocCommand = OnFocused;
  8717. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8718. int Options =
  8719. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8720. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8721. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8722. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8723. {
  8724. FLastCustomCommand = Command;
  8725. UpdateCustomCommandsToolbar();
  8726. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8727. }
  8728. }
  8729. //---------------------------------------------------------------------------
  8730. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8731. {
  8732. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8733. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8734. DebugAssert(State > 0);
  8735. if (State <= 0)
  8736. {
  8737. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  8738. }
  8739. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8740. }
  8741. //---------------------------------------------------------------------------
  8742. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8743. TCustomCommandType & Command, int & State)
  8744. {
  8745. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8746. if (Result)
  8747. {
  8748. Command = FLastCustomCommand;
  8749. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8750. }
  8751. return Result;
  8752. }
  8753. //---------------------------------------------------------------------------
  8754. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8755. {
  8756. ResetIncrementalSearch();
  8757. }
  8758. //---------------------------------------------------------------------------
  8759. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8760. {
  8761. DebugAssert(ComponentVisible[Component]);
  8762. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8763. }
  8764. //---------------------------------------------------------------------------
  8765. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8766. {
  8767. // when queue panel is resized here directly, the status bar is stretched
  8768. // over whole space the panel occupied
  8769. PostComponentHide(fcQueueView);
  8770. }
  8771. //---------------------------------------------------------------------------
  8772. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  8773. {
  8774. ComponentVisible[fcQueueFileList] = false;
  8775. }
  8776. //---------------------------------------------------------------------------
  8777. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  8778. {
  8779. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  8780. RefreshColorMode();
  8781. ResetSysDarkTheme();
  8782. ConfigurationChanged();
  8783. ConfigureInterface();
  8784. // Should be called for all controls
  8785. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  8786. }
  8787. //---------------------------------------------------------------------------
  8788. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  8789. {
  8790. // Do not handle, when shutting down anyway (maybe also when not setup completelly yet?)
  8791. if (!FInvalid &&
  8792. (Message.LParam != 0) &&
  8793. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  8794. {
  8795. ThemeChanged();
  8796. }
  8797. TForm::Dispatch(&Message);
  8798. }
  8799. //---------------------------------------------------------------------------
  8800. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8801. {
  8802. TMessage * M = static_cast<TMessage*>(Message);
  8803. switch (M->Msg)
  8804. {
  8805. case CM_DIALOGCHAR:
  8806. CMDialogChar(*M);
  8807. break;
  8808. case WM_APPCOMMAND:
  8809. WMAppCommand(*M);
  8810. break;
  8811. case WM_SYSCOMMAND:
  8812. WMSysCommand(*M);
  8813. break;
  8814. case WM_QUERYENDSESSION:
  8815. WMQueryEndSession(*M);
  8816. break;
  8817. case WM_ENDSESSION:
  8818. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8819. break;
  8820. #ifdef _DEBUG
  8821. case WM_WINDOWPOSCHANGED:
  8822. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8823. break;
  8824. case CM_CUSTOMSTYLECHANGED:
  8825. TForm::Dispatch(Message);
  8826. break;
  8827. #endif
  8828. case WM_COMPONENT_HIDE:
  8829. {
  8830. Byte Component = static_cast<Byte>(M->WParam);
  8831. // sanity check
  8832. if (ComponentVisible[Component])
  8833. {
  8834. // special treatment
  8835. if (Component == fcQueueView)
  8836. {
  8837. ToggleQueueVisibility();
  8838. DebugAssert(!ComponentVisible[fcQueueView]);
  8839. }
  8840. else
  8841. {
  8842. ComponentVisible[Component] = false;
  8843. }
  8844. }
  8845. }
  8846. break;
  8847. case WM_COPYDATA:
  8848. WMCopyData(*M);
  8849. break;
  8850. case WM_MANAGES_CAPTION:
  8851. // caption managed in TTerminalManager::UpdateAppTitle()
  8852. M->Result = 1;
  8853. break;
  8854. case WM_WANTS_MOUSEWHEEL:
  8855. M->Result = 1;
  8856. break;
  8857. case CM_SHOWINGCHANGED:
  8858. CMShowingChanged(*M);
  8859. break;
  8860. case WM_CLOSE:
  8861. WMClose(*M);
  8862. break;
  8863. case WM_DPICHANGED:
  8864. WMDpiChanged(*M);
  8865. break;
  8866. case CM_DPICHANGED:
  8867. CMDpiChanged(*M);
  8868. break;
  8869. case WM_SETTINGCHANGE:
  8870. WMSettingChange(*M);
  8871. break;
  8872. case CM_DIALOGKEY:
  8873. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8874. break;
  8875. default:
  8876. TForm::Dispatch(Message);
  8877. break;
  8878. }
  8879. }
  8880. //---------------------------------------------------------------------------
  8881. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8882. {
  8883. // noop
  8884. }
  8885. //---------------------------------------------------------------------------
  8886. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8887. {
  8888. TForm::Dispatch(&Message);
  8889. FSessionColors->Clear();
  8890. AddFixedSessionImages();
  8891. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8892. UpdateImages();
  8893. }
  8894. //---------------------------------------------------------------------------
  8895. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8896. {
  8897. TForm::Dispatch(&Message);
  8898. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8899. UpdatePixelsPerInchMainWindowCounter();
  8900. }
  8901. //---------------------------------------------------------------------------
  8902. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8903. {
  8904. // Cannot close window while we are busy.
  8905. // We cannot test this in FormCloseQuery as that is called also from
  8906. // Close(), which is called by CloseApplicationAction. So we can be busy
  8907. // there already even, when it is legitimate to close the application.
  8908. // Possibly a better place to handle this would be WMSysCommand.
  8909. if (NonVisualDataModule->Busy)
  8910. {
  8911. Message.Result = 1;
  8912. }
  8913. else
  8914. {
  8915. TForm::Dispatch(&Message);
  8916. }
  8917. }
  8918. //---------------------------------------------------------------------------
  8919. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8920. {
  8921. if (!FShowingChanged)
  8922. {
  8923. AddStartupSequence(L"D");
  8924. FShowingChanged = true;
  8925. }
  8926. // Ignoring implicit local-local browser of Commander, opened from DoShow
  8927. bool NoSession = (ManagedSession == NULL);
  8928. // This calls TCustomScpExplorerForm::DoShow
  8929. TForm::Dispatch(&Message);
  8930. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  8931. if (Showing && !FStarted)
  8932. {
  8933. FStarted = true;
  8934. InterfaceStarted();
  8935. }
  8936. if (Showing && NoSession)
  8937. {
  8938. // When we are starting minimized (i.e. from an installer),
  8939. // postpone showing Login dialog until we get restored.
  8940. // Otherwise the Login dialog (and Authentication window) show restored
  8941. // over invisible (minimized) main window.
  8942. if (WindowState == wsMinimized)
  8943. {
  8944. FNeedSession = true;
  8945. }
  8946. else
  8947. {
  8948. // This happens before application ever goes idle, so the toolbars would
  8949. // stay enabled (initial state) until the Login dialog is dismissed.
  8950. UpdateActions();
  8951. NonVisualDataModule->StartBusy();
  8952. try
  8953. {
  8954. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8955. // otherwise the dialog does not receive focus.
  8956. // With Commander interface the ProcessMessages is called already
  8957. // by TDriveView, but with Explorer interface, we need to call it explicily
  8958. Application->ProcessMessages();
  8959. // do not reload sessions, they have been loaded just now (optimization)
  8960. NeedSession(true);
  8961. }
  8962. __finally
  8963. {
  8964. NonVisualDataModule->EndBusy();
  8965. }
  8966. }
  8967. }
  8968. }
  8969. //---------------------------------------------------------------------------
  8970. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  8971. {
  8972. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  8973. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  8974. }
  8975. //---------------------------------------------------------------------------
  8976. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8977. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8978. int & MaxHeight)
  8979. {
  8980. // workaround for bug in TWinControl.CalcConstraints
  8981. // Check for empty rect (restore from iconic state) is done there only after
  8982. // call to AdjustClientRect, which enlarges the rect (for forms).
  8983. TRect R = GetClientRect();
  8984. // when restoring from iconic state, place no restrictions
  8985. if (IsRectEmpty(R))
  8986. {
  8987. MinWidth = 0;
  8988. MinHeight = 0;
  8989. MaxWidth = 32000;
  8990. MaxHeight = 32000;
  8991. }
  8992. }
  8993. //---------------------------------------------------------------------------
  8994. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8995. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8996. {
  8997. // terminal can be already closed (e.g. dropped connection)
  8998. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8999. {
  9000. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  9001. try
  9002. {
  9003. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  9004. }
  9005. catch(...)
  9006. {
  9007. if (!Terminal->Active)
  9008. {
  9009. Close = true;
  9010. }
  9011. throw;
  9012. }
  9013. }
  9014. }
  9015. //---------------------------------------------------------------------------
  9016. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  9017. {
  9018. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  9019. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  9020. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  9021. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  9022. {
  9023. OnGetSpaceAvailable = GetSpaceAvailable;
  9024. }
  9025. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  9026. OnGetSpaceAvailable);
  9027. }
  9028. //---------------------------------------------------------------------------
  9029. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  9030. {
  9031. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  9032. Terminal->FillSessionDataForCode(Data.get());
  9033. return Data.release();
  9034. }
  9035. //---------------------------------------------------------------------------
  9036. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  9037. {
  9038. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  9039. DoGenerateUrlDialog(Data.get(), Paths);
  9040. }
  9041. //---------------------------------------------------------------------------
  9042. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  9043. {
  9044. GenerateUrl(NULL);
  9045. }
  9046. //---------------------------------------------------------------------------
  9047. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  9048. {
  9049. std::unique_ptr<TStrings> Paths(new TStringList());
  9050. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  9051. GenerateUrl(Paths.get());
  9052. }
  9053. //---------------------------------------------------------------------------
  9054. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  9055. {
  9056. FSessionColor = value;
  9057. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  9058. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  9059. static_cast<TObject *>(GetComponent(fcColorMenu)));
  9060. DebugAssert(ColorItem != NULL);
  9061. ColorItem->Color = C;
  9062. NonVisualDataModule->ColorMenuItem->Color = C;
  9063. UpdateControls();
  9064. }
  9065. //---------------------------------------------------------------------------
  9066. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  9067. {
  9068. if (value != FSessionColor)
  9069. {
  9070. UpdateSessionColor(value);
  9071. // Is null when called from LastTerminalClosed
  9072. if (Terminal != NULL)
  9073. {
  9074. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  9075. }
  9076. }
  9077. }
  9078. //---------------------------------------------------------------------------
  9079. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  9080. {
  9081. bool Result = FNoteTimer->Enabled;
  9082. if (Result)
  9083. {
  9084. // cannot cancel note too early
  9085. bool NotEarly =
  9086. (Now() - FNoteShown >
  9087. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  9088. if (Force || NotEarly)
  9089. {
  9090. FNoteTimer->Enabled = false;
  9091. FNote = L"";
  9092. SAFE_DESTROY(FNoteData);
  9093. FOnNoteClick = NULL;
  9094. FNoteHints = FNotes->Text;
  9095. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  9096. UpdateStatusBar();
  9097. }
  9098. }
  9099. return Result;
  9100. }
  9101. //---------------------------------------------------------------------------
  9102. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  9103. {
  9104. DebugAssert(FNoteTimer->Enabled);
  9105. CancelNote(true);
  9106. }
  9107. //---------------------------------------------------------------------------
  9108. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  9109. {
  9110. FNoteHints = FNotes->Text.TrimRight();
  9111. }
  9112. //---------------------------------------------------------------------------
  9113. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  9114. {
  9115. int P = Note.Pos(L"\n");
  9116. if (P > 0)
  9117. {
  9118. Note.SetLength(P - 1);
  9119. }
  9120. FNotes->Add(FORMAT(L"[%s] %s",
  9121. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  9122. while (FNotes->Count > 10)
  9123. {
  9124. FNotes->Delete(0);
  9125. }
  9126. if (UpdateNow)
  9127. {
  9128. UpdateNoteHints();
  9129. UpdateStatusBar();
  9130. }
  9131. }
  9132. //---------------------------------------------------------------------------
  9133. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  9134. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  9135. {
  9136. int P = Note.Pos(L"\n");
  9137. if (P > 0)
  9138. {
  9139. Note.SetLength(P - 1);
  9140. }
  9141. UpdateNoteHints();
  9142. FNote = Note;
  9143. SAFE_DESTROY(FNoteData);
  9144. FNoteData = NoteData;
  9145. FOnNoteClick = OnNoteClick;
  9146. AddNote(Note, false);
  9147. UpdateStatusBar();
  9148. FNoteShown = Now();
  9149. FNoteTimer->Enabled = false;
  9150. if (Seconds == 0)
  9151. {
  9152. Seconds = WinConfiguration->NotificationsTimeout;
  9153. }
  9154. FNoteTimer->Interval = Seconds * MSecsPerSec;
  9155. FNoteTimer->Enabled = true;
  9156. }
  9157. //---------------------------------------------------------------------------
  9158. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  9159. {
  9160. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  9161. }
  9162. //---------------------------------------------------------------------------
  9163. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  9164. {
  9165. if (NonVisualDataModule->SynchronizeBrowsingAction2->Checked)
  9166. {
  9167. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  9168. }
  9169. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  9170. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  9171. (NonVisualDataModule->SynchronizeBrowsingAction2->Checked ? 2 : 3)))),
  9172. 0, NULL, NULL);
  9173. }
  9174. //---------------------------------------------------------------------------
  9175. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  9176. {
  9177. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  9178. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  9179. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  9180. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  9181. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  9182. }
  9183. //---------------------------------------------------------------------------
  9184. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  9185. {
  9186. WinConfiguration->FormatSizeBytes = Style;
  9187. }
  9188. //---------------------------------------------------------------------------
  9189. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  9190. {
  9191. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  9192. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  9193. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  9194. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  9195. }
  9196. //---------------------------------------------------------------------------
  9197. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  9198. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  9199. {
  9200. if (Panel->Index == 0)
  9201. {
  9202. if (FOnNoteClick != NULL)
  9203. {
  9204. // prevent the user data from being freed by possible call
  9205. // to CancelNote or PostNote during call to OnNoteClick
  9206. std::unique_ptr<TObject> NoteData(FNoteData);
  9207. TNotifyEvent OnNoteClick = FOnNoteClick;
  9208. FNoteData = NULL;
  9209. // need to cancel the note as we are going to delete its user data
  9210. CancelNote(true);
  9211. OnNoteClick(NoteData.get());
  9212. }
  9213. }
  9214. if (HasActiveTerminal() &&
  9215. (Panel->Index >= Sender->Panels->Count - SessionPanelCount))
  9216. {
  9217. FileSystemInfo();
  9218. }
  9219. }
  9220. //---------------------------------------------------------------------------
  9221. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  9222. {
  9223. // workaround:
  9224. // 1) for unknown reason, disabling window, while minimized,
  9225. // prevents it from restoring, even if it was enabled again meanwhile
  9226. // 2) when disabling the main window, while another has focus
  9227. // minimize is no longer possible
  9228. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  9229. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  9230. // Shouldn't we use IsApplicationMinimized() here?
  9231. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  9232. {
  9233. Enabled = false;
  9234. }
  9235. FLockLevel++;
  9236. }
  9237. //---------------------------------------------------------------------------
  9238. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  9239. {
  9240. DebugAssert(FLockLevel > 0);
  9241. FLockLevel--;
  9242. if (FLockLevel == 0)
  9243. {
  9244. DebugAssert(FLockSuspendLevel == 0);
  9245. Enabled = true;
  9246. }
  9247. }
  9248. //---------------------------------------------------------------------------
  9249. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  9250. {
  9251. // We need to make sure that window is enabled when the last modal window closes
  9252. // otherwise focus is not restored correctly.
  9253. // So we re-enable the window when modal window opens and
  9254. // disable it back after is closes
  9255. if (FLockLevel > 0)
  9256. {
  9257. // while we have nesting counter, we know that we never be called
  9258. // recursivelly as Application->OnModalBegin is called only
  9259. // for the top-level modal window
  9260. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9261. {
  9262. // won't be disabled when conditions in LockWindow() were not satisfied
  9263. FDisabledOnLockSuspend = !Enabled;
  9264. // When minimized to tray (or actually when set to SW_HIDE),
  9265. // setting Enabled makes the window focusable even when there's
  9266. // modal window over it
  9267. if (!FTrayIcon->Visible)
  9268. {
  9269. Enabled = true;
  9270. }
  9271. }
  9272. FLockSuspendLevel++;
  9273. }
  9274. }
  9275. //---------------------------------------------------------------------------
  9276. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9277. {
  9278. if (FLockSuspendLevel > 0)
  9279. {
  9280. DebugAssert(FLockLevel > 0);
  9281. FLockSuspendLevel--;
  9282. // see comment in SuspendWindowLock
  9283. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9284. {
  9285. // Note that window can be enabled here, when we were minized to tray when
  9286. // was SuspendWindowLock() called.
  9287. // We should possibly do the same check as in LockWindow(),
  9288. // if it is ever possible that the consitions change between
  9289. // SuspendWindowLock() and ResumeWindowLock()
  9290. Enabled = !FDisabledOnLockSuspend;
  9291. }
  9292. }
  9293. }
  9294. //---------------------------------------------------------------------------
  9295. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9296. {
  9297. TTBXComboBoxItem * RemotePathComboBox =
  9298. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9299. if (!TextOnly)
  9300. {
  9301. TStrings * Items = RemotePathComboBox->Strings;
  9302. Items->BeginUpdate();
  9303. try
  9304. {
  9305. Items->Clear();
  9306. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9307. {
  9308. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9309. while (!IsUnixRootPath(APath))
  9310. {
  9311. int P = APath.LastDelimiter(L'/');
  9312. DebugAssert(P >= 0);
  9313. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9314. APath.SetLength(P - 1);
  9315. }
  9316. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9317. }
  9318. }
  9319. __finally
  9320. {
  9321. Items->EndUpdate();
  9322. }
  9323. }
  9324. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9325. // Setting ItemIndex to -1 does not reset its text
  9326. if (RemotePathComboBox->Strings->Count == 0)
  9327. {
  9328. RemotePathComboBox->Text = L"";
  9329. }
  9330. }
  9331. //---------------------------------------------------------------------------
  9332. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxAdjustImageIndex(
  9333. TTBXComboBoxItem * Sender, const UnicodeString DebugUsedArg(AText), int AIndex, int & ImageIndex)
  9334. {
  9335. if (AIndex < 0)
  9336. {
  9337. AIndex = Sender->ItemIndex;
  9338. }
  9339. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9340. }
  9341. //---------------------------------------------------------------------------
  9342. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9343. TTBXComboBoxItem * Sender, const UnicodeString AText, int AIndex,
  9344. int & ImageIndex)
  9345. {
  9346. DoRemotePathComboBoxAdjustImageIndex(Sender, AText, AIndex, ImageIndex);
  9347. }
  9348. //---------------------------------------------------------------------------
  9349. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9350. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9351. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9352. {
  9353. if (!IsLocalBrowserMode())
  9354. {
  9355. ARect.Left += (10 * AIndex);
  9356. }
  9357. }
  9358. //---------------------------------------------------------------------------
  9359. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9360. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9361. {
  9362. AWidth += (10 * AIndex);
  9363. }
  9364. //---------------------------------------------------------------------------
  9365. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxItemClick(TObject * Sender)
  9366. {
  9367. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9368. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9369. int Index = RemotePathComboBox->ItemIndex;
  9370. while (Index < RemotePathComboBox->Strings->Count - 1)
  9371. {
  9372. APath = UnixExtractFileDir(APath);
  9373. Index++;
  9374. }
  9375. // VanDyke style paths
  9376. if (APath.IsEmpty())
  9377. {
  9378. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9379. APath = ROOTDIRECTORY;
  9380. }
  9381. if (RemoteDirView->Path != APath)
  9382. {
  9383. RemoteDirView->Path = APath;
  9384. }
  9385. }
  9386. //---------------------------------------------------------------------------
  9387. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(TObject * Sender)
  9388. {
  9389. DoRemotePathComboBoxItemClick(Sender);
  9390. }
  9391. //---------------------------------------------------------------------------
  9392. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxCancel(TObject * Sender)
  9393. {
  9394. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9395. DebugUsedParam(Sender);
  9396. UpdateRemotePathComboBox(true);
  9397. }
  9398. //---------------------------------------------------------------------------
  9399. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9400. {
  9401. DoRemotePathComboBoxCancel(Sender);
  9402. }
  9403. //---------------------------------------------------------------------------
  9404. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9405. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9406. {
  9407. ResetIncrementalSearch();
  9408. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9409. DebugAssert(DirView != NULL);
  9410. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9411. {
  9412. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9413. // OnEditing is called also from TCustomListView::CanEdit
  9414. if (Edit != NULL)
  9415. {
  9416. EditSelectBaseName(Edit);
  9417. }
  9418. }
  9419. }
  9420. //---------------------------------------------------------------------------
  9421. //---------------------------------------------------------------------------
  9422. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9423. {
  9424. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9425. Result->AutoDetectDnD = false;
  9426. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9427. Result->RenderDataOn = rdoEnterAndDropSync;
  9428. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9429. return Result;
  9430. }
  9431. //---------------------------------------------------------------------------
  9432. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9433. {
  9434. if (IsWin10Build(2004))
  9435. {
  9436. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9437. BOOL DarkMode = FImmersiveDarkMode ? TRUE : FALSE;
  9438. const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  9439. DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
  9440. }
  9441. }
  9442. //---------------------------------------------------------------------------
  9443. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9444. {
  9445. TForm::CreateWnd();
  9446. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9447. UpdateDarkMode();
  9448. if (FSessionsDragDropFilesEx == NULL)
  9449. {
  9450. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9451. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9452. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9453. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9454. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9455. }
  9456. if (FQueueDragDropFilesEx == NULL)
  9457. {
  9458. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9459. // No need to set OnDragOver as we do not have any restrictions
  9460. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9461. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9462. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9463. }
  9464. }
  9465. //---------------------------------------------------------------------------
  9466. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9467. {
  9468. TForm::DestroyWnd();
  9469. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9470. FQueueDragDropFilesEx->DragDropControl = NULL;
  9471. }
  9472. //---------------------------------------------------------------------------
  9473. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9474. {
  9475. SideEnter(FCurrentSide);
  9476. FEverShown = true;
  9477. }
  9478. //---------------------------------------------------------------------------
  9479. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9480. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9481. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9482. {
  9483. if (!NonVisualDataModule->Busy)
  9484. {
  9485. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9486. Configuration->Usage->Inc(L"FileFinds");
  9487. LockWindow(true);
  9488. NonVisualDataModule->StartBusy();
  9489. WinConfiguration->LockedInterface = true;
  9490. try
  9491. {
  9492. Terminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9493. }
  9494. __finally
  9495. {
  9496. WinConfiguration->LockedInterface = false;
  9497. NonVisualDataModule->EndBusy();
  9498. UnlockWindow();
  9499. }
  9500. }
  9501. }
  9502. //---------------------------------------------------------------------------
  9503. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9504. {
  9505. if (!NonVisualDataModule->Busy)
  9506. {
  9507. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9508. SetFocus();
  9509. RemoteDirView->Path = UnixExtractFilePath(Path);
  9510. UnicodeString FileName = UnixExtractFileName(Path);
  9511. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9512. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9513. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9514. if (Item == NULL)
  9515. {
  9516. ReloadDirectory(osRemote);
  9517. Item = RemoteDirView->FindFileItem(FileName);
  9518. }
  9519. if (Item != NULL)
  9520. {
  9521. RemoteDirView->ItemFocused = Item;
  9522. RemoteDirView->ItemFocused->MakeVisible(false);
  9523. RemoteDirView->SetFocus();
  9524. }
  9525. }
  9526. }
  9527. //---------------------------------------------------------------------------
  9528. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9529. {
  9530. bool Result = !NonVisualDataModule->Busy;
  9531. if (Result)
  9532. {
  9533. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9534. }
  9535. return Result;
  9536. }
  9537. //---------------------------------------------------------------------------
  9538. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9539. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9540. {
  9541. if (CanOperateOnFoundFiles(ATerminal))
  9542. {
  9543. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9544. FOnFileOperationFinished = OnFileOperationFinished;
  9545. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9546. }
  9547. }
  9548. //---------------------------------------------------------------------------
  9549. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9550. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9551. {
  9552. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9553. }
  9554. //---------------------------------------------------------------------------
  9555. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9556. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9557. {
  9558. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9559. }
  9560. //---------------------------------------------------------------------------
  9561. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9562. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9563. {
  9564. if (CanOperateOnFoundFiles(ATerminal))
  9565. {
  9566. for (int Index = 0; Index < FileList->Count; Index++)
  9567. {
  9568. UnicodeString FileName = FileList->Strings[Index];
  9569. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9570. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9571. OnFileOperationFinished(osRemote, FileName, true);
  9572. }
  9573. }
  9574. }
  9575. //---------------------------------------------------------------------------
  9576. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9577. {
  9578. FFileFindTerminal = Terminal;
  9579. ShowFileFindDialog(
  9580. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9581. DoDownloadFoundFiles, DoEditFoundFiles);
  9582. }
  9583. //---------------------------------------------------------------------------
  9584. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9585. {
  9586. FTaskbarList = TaskbarList;
  9587. }
  9588. //---------------------------------------------------------------------------
  9589. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9590. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9591. {
  9592. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9593. if (Index >= 0)
  9594. {
  9595. if (Button == mbLeft)
  9596. {
  9597. // WORKAROUND: "Mouse down" is raised only after tab is switched.
  9598. // If switching tab (switching session) takes long enough for user
  9599. // to actually release the button, "mouse down" is still raised,
  9600. // but we do not get "mouse up" event, so dragging is not cancelled,
  9601. // prevent that by not beginning dragging in the first place.
  9602. if (IsKeyPressed(VK_LBUTTON))
  9603. {
  9604. // when user clicks the "+", we get mouse down only after the session
  9605. // is closed, when new session tab is already on X:Y, so dragging
  9606. // starts, prevent that
  9607. if (MilliSecondsBetween(Now(), FSessionsPageControlNewTabTime) > 500)
  9608. {
  9609. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9610. if (Terminal != NULL)
  9611. {
  9612. SessionsPageControl->BeginDrag(false);
  9613. }
  9614. }
  9615. }
  9616. }
  9617. else if (Button == mbMiddle)
  9618. {
  9619. // ignore middle-click for "New session tab" and the last local-local tab
  9620. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9621. if ((Session != NULL) && CanCloseSession(Session))
  9622. {
  9623. CloseSessionTab(Index);
  9624. }
  9625. }
  9626. }
  9627. }
  9628. //---------------------------------------------------------------------------
  9629. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9630. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9631. {
  9632. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9633. // do not allow dropping on the "+" tab
  9634. TTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9635. if ((TargetTerminal != NULL) &&
  9636. (SessionsPageControl->ActivePage->PageIndex != Index))
  9637. {
  9638. Configuration->Usage->Inc(L"SessionTabMoves");
  9639. // this is almost redundant as we would recreate tabs in SessionListChanged,
  9640. // but we want to actually prevent that to avoid flicker
  9641. SessionsPageControl->ActivePage->PageIndex = Index;
  9642. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->ActivePage);
  9643. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9644. }
  9645. }
  9646. //---------------------------------------------------------------------------
  9647. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9648. TObject * Sender, TObject * Source, int X, int Y,
  9649. TDragState /*State*/, bool & Accept)
  9650. {
  9651. Accept = (Sender == Source);
  9652. if (Accept)
  9653. {
  9654. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9655. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9656. // do not allow dragging to the "+" tab
  9657. Accept = (Terminal != NULL);
  9658. }
  9659. }
  9660. //---------------------------------------------------------------------------
  9661. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  9662. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  9663. {
  9664. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9665. if (Index < 0)
  9666. {
  9667. Effect = DROPEFFECT_NONE;
  9668. }
  9669. else
  9670. {
  9671. TManagedTerminal * TargetSession = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9672. // do not allow dropping on the "+" tab or on disconnected or local-local tabs
  9673. if (!IsActiveTerminal(TargetSession) || DebugAlwaysFalse(TargetSession->LocalBrowser))
  9674. {
  9675. Effect = DROPEFFECT_NONE;
  9676. }
  9677. else if ((TargetSession != Terminal) && (Effect == DROPEFFECT_MOVE))
  9678. {
  9679. Effect = DROPEFFECT_COPY;
  9680. }
  9681. }
  9682. }
  9683. //---------------------------------------------------------------------------
  9684. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9685. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9686. {
  9687. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9688. // do not allow dropping on local-local, disconnected and the "+" tab
  9689. // (though we do not seem to get here in that case anyway, contrary to RemoteFileControlDDTargetDrop)
  9690. TManagedTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9691. if (DebugAlwaysTrue(IsActiveTerminal(TargetTerminal) && !TargetTerminal->LocalBrowser))
  9692. {
  9693. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9694. if (!IsFileControl(DropSourceControl, osRemote))
  9695. {
  9696. TTerminalManager::Instance()->ActiveSession = TargetTerminal;
  9697. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9698. // Why don't we use Terminal->CurrentDirectory directly?
  9699. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9700. // do not force queue, drag drop
  9701. false, true);
  9702. }
  9703. }
  9704. }
  9705. //---------------------------------------------------------------------------
  9706. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9707. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9708. {
  9709. // Downloads are handled in RemoteFileControlDDEnd
  9710. if (!IsFileControl(DropSourceControl, osRemote))
  9711. {
  9712. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9713. Terminal->CurrentDirectory,
  9714. // force queue, drag drop
  9715. true, true);
  9716. }
  9717. }
  9718. //---------------------------------------------------------------------------
  9719. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9720. {
  9721. FShowing = false;
  9722. // Do not save empty workspace
  9723. if (WinConfiguration->AutoSaveWorkspace && (ManagedSession != NULL))
  9724. {
  9725. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9726. UnicodeString Name = WorkspaceName();
  9727. DoSaveWorkspace(
  9728. Name, DataList.get(),
  9729. !Configuration->DisablePasswordStoring &&
  9730. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9731. WinConfiguration->LastStoredSession = Name;
  9732. }
  9733. }
  9734. //---------------------------------------------------------------------------
  9735. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9736. {
  9737. TManagedTerminal * ManagedTerminal =
  9738. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9739. if (ManagedTerminal != NULL)
  9740. {
  9741. ManagedTerminal->DirectoryLoaded = Now();
  9742. }
  9743. }
  9744. //---------------------------------------------------------------------------
  9745. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9746. TListItem * /*Item*/, bool /*Selected*/)
  9747. {
  9748. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9749. switch (DirView->LastSelectMethod)
  9750. {
  9751. case smKeyboard:
  9752. Configuration->Usage->Inc(L"KeyboardSelections");
  9753. break;
  9754. case smMouse:
  9755. Configuration->Usage->Inc(L"MouseSelections");
  9756. break;
  9757. }
  9758. }
  9759. //---------------------------------------------------------------------------
  9760. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9761. {
  9762. if (Color != 0)
  9763. {
  9764. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9765. }
  9766. else
  9767. {
  9768. return FSessionTabImageIndex;
  9769. }
  9770. }
  9771. //---------------------------------------------------------------------------
  9772. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9773. {
  9774. TPngImageCollectionItem * Item =
  9775. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9776. FSessionColors->AddPng(Item->PngImage);
  9777. return FSessionColors->Count - 1;
  9778. }
  9779. //---------------------------------------------------------------------------
  9780. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9781. {
  9782. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9783. FNewRemoteTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewRemoteTabAction->ImageIndex);
  9784. FNewLocalTabTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewLocalTabAction->ImageIndex);
  9785. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9786. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9787. FLocalBrowserTabImageIndex = AddFixedSessionImage(LocalBrowserImageIndex);
  9788. }
  9789. //---------------------------------------------------------------------------
  9790. int TCustomScpExplorerForm::GetNewTabActionImageIndex()
  9791. {
  9792. return NonVisualDataModule->NewRemoteTabAction->ImageIndex;
  9793. }
  9794. //---------------------------------------------------------------------------
  9795. int TCustomScpExplorerForm::GetNewTabTabImageIndex(TOperationSide Side)
  9796. {
  9797. switch (Side)
  9798. {
  9799. case osLocal:
  9800. return FNewLocalTabTabImageIndex;
  9801. case osRemote:
  9802. case osCurrent:
  9803. return FNewRemoteTabTabImageIndex;
  9804. default:
  9805. DebugFail();
  9806. return -1;
  9807. }
  9808. }
  9809. //---------------------------------------------------------------------------
  9810. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9811. {
  9812. for (int Index = 0; Index < Control->ControlCount; Index++)
  9813. {
  9814. TControl * AControl = Control->Controls[Index];
  9815. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9816. if (WinControl != NULL)
  9817. {
  9818. CollectItemsWithTextDisplayMode(WinControl);
  9819. }
  9820. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9821. if (Toolbar != NULL)
  9822. {
  9823. // we care for top-level items only
  9824. TTBCustomItem * Items = Toolbar->Items;
  9825. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9826. {
  9827. TTBCustomItem * Item = Items->Items[ItemIndex];
  9828. if (((Item->DisplayMode == nbdmImageAndText) ||
  9829. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9830. EligibleForImageDisplayMode(Item))
  9831. {
  9832. FItemsWithTextDisplayMode.insert(Item);
  9833. }
  9834. }
  9835. }
  9836. }
  9837. }
  9838. //---------------------------------------------------------------------------
  9839. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9840. {
  9841. return true;
  9842. }
  9843. //---------------------------------------------------------------------------
  9844. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9845. {
  9846. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9847. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9848. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9849. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9850. bool Result = true;
  9851. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9852. {
  9853. TTBCustomItem * Item = *i;
  9854. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9855. if (Label != NULL)
  9856. {
  9857. // optimization
  9858. if (Label->Visible == SelectiveToolbarText)
  9859. {
  9860. Result = false;
  9861. }
  9862. else
  9863. {
  9864. Label->Visible = SelectiveToolbarText;
  9865. }
  9866. }
  9867. else
  9868. {
  9869. // optimization
  9870. if (Item->DisplayMode == DisplayMode)
  9871. {
  9872. Result = false;
  9873. }
  9874. else
  9875. {
  9876. Item->DisplayMode = DisplayMode;
  9877. }
  9878. }
  9879. ++i;
  9880. }
  9881. UpdateNewTabTab();
  9882. return Result;
  9883. }
  9884. //---------------------------------------------------------------------------
  9885. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9886. {
  9887. DebugFail();
  9888. }
  9889. //---------------------------------------------------------------------------
  9890. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9891. {
  9892. // Among other, a lock level is non zero, while directory is being loaded,
  9893. // even when it happens as a result of dirview navigation,
  9894. // i.e. when TNonVisualDataModule is NOT FBusy.
  9895. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9896. // Among other this prevents a panel auto update to occur while
  9897. // directory is changing.
  9898. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9899. }
  9900. //---------------------------------------------------------------------------
  9901. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * Action, TActionAllowed Allowed)
  9902. {
  9903. // While the window is disabled, we still seem to process menu shortcuts at least,
  9904. // so stop it at least here.
  9905. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9906. bool Result;
  9907. if (Allowed == aaUpdate)
  9908. {
  9909. Result = true;
  9910. }
  9911. else
  9912. {
  9913. Result = !NonVisualDataModule->Busy;
  9914. if (Result && (Allowed == aaShortCut))
  9915. {
  9916. // Otherwise F5 for local-local will be swallowed by disabled local<>remote actions in TCustomScpExplorerForm::KeyDown
  9917. Action->Update();
  9918. Result = Action->Enabled;
  9919. }
  9920. }
  9921. return Result;
  9922. }
  9923. //---------------------------------------------------------------------------
  9924. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9925. {
  9926. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9927. }
  9928. //---------------------------------------------------------------------------
  9929. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9930. {
  9931. // This is somewhat redundant to LockWindow() call from
  9932. // TTerminalManager::TerminalReadDirectoryProgress.
  9933. // But disabling window is known not to block keyboard shorcuts
  9934. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9935. if (Busy > 0)
  9936. {
  9937. if (NonVisualDataModule->Busy)
  9938. {
  9939. State = false;
  9940. }
  9941. else
  9942. {
  9943. NonVisualDataModule->StartBusy();
  9944. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9945. LockWindow();
  9946. }
  9947. }
  9948. else if (Busy < 0)
  9949. {
  9950. UnlockWindow();
  9951. NonVisualDataModule->EndBusy();
  9952. }
  9953. else
  9954. {
  9955. State = NonVisualDataModule->Busy;
  9956. }
  9957. }
  9958. //---------------------------------------------------------------------------
  9959. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9960. {
  9961. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9962. if (Index >= 0)
  9963. {
  9964. TPopupMenu * PopupMenu = NULL;
  9965. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9966. if (Session != NULL)
  9967. {
  9968. SessionsPageControl->ActivePageIndex = Index;
  9969. if (DebugAlwaysTrue(SessionTabSwitched()))
  9970. {
  9971. // copied from TControl.WMContextMenu
  9972. SendCancelMode(SessionsPageControl);
  9973. // explicit popup instead of using PopupMenu property
  9974. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9975. // while connecting yet not-connected session and hence
  9976. // allowing an access to commands over not-completelly connected session
  9977. PopupMenu = Session->LocalBrowser ? NonVisualDataModule->LocalBrowserPopup : NonVisualDataModule->SessionsPopup;
  9978. }
  9979. }
  9980. else
  9981. {
  9982. if (SupportsLocalBrowser())
  9983. {
  9984. PopupMenu = NonVisualDataModule->NewTabPopup;
  9985. }
  9986. }
  9987. if (PopupMenu != NULL)
  9988. {
  9989. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9990. PopupMenu->PopupComponent = SessionsPageControl;
  9991. PopupMenu->Popup(Point.x, Point.y);
  9992. }
  9993. }
  9994. Handled = true;
  9995. }
  9996. //---------------------------------------------------------------------------
  9997. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9998. {
  9999. return HasActiveTerminal() && Terminal->IsCapable[fcChangePassword];
  10000. }
  10001. //---------------------------------------------------------------------------
  10002. void __fastcall TCustomScpExplorerForm::ChangePassword()
  10003. {
  10004. Configuration->Usage->Inc(L"PasswordChanges");
  10005. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  10006. ChangePasswordData->DisableAuthentationsExceptPassword();
  10007. ChangePasswordData->ChangePassword = true;
  10008. std::unique_ptr<TTerminal> ChangePasswordSession(
  10009. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  10010. ChangePasswordSession->AutoReadDirectory = false;
  10011. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  10012. {
  10013. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  10014. }
  10015. else
  10016. {
  10017. Configuration->Usage->Inc(L"PasswordChangesFailed");
  10018. }
  10019. }
  10020. //---------------------------------------------------------------------------
  10021. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  10022. {
  10023. // No nice way to assert SSH2
  10024. return HasActiveTerminal() && (Terminal->FSProtocol == cfsSFTP);
  10025. }
  10026. //---------------------------------------------------------------------------
  10027. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  10028. {
  10029. TOperationVisualizer OperationVisualizer;
  10030. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  10031. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  10032. }
  10033. //---------------------------------------------------------------------------
  10034. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  10035. {
  10036. TForm::ChangeScale(M, D);
  10037. int APixelsPerInch = GetControlPixelsPerInch(this);
  10038. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  10039. try
  10040. {
  10041. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  10042. }
  10043. __finally
  10044. {
  10045. GUIConfiguration->ChangeResourceModule(ResourceModule);
  10046. }
  10047. }
  10048. //---------------------------------------------------------------------------
  10049. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  10050. {
  10051. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  10052. }
  10053. //---------------------------------------------------------------------
  10054. class TPublicControl : public TControl
  10055. {
  10056. friend class TCustomScpExplorerForm;
  10057. };
  10058. //---------------------------------------------------------------------------
  10059. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  10060. {
  10061. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  10062. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  10063. }
  10064. //---------------------------------------------------------------------------
  10065. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  10066. {
  10067. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  10068. OpenBookmark(Side, Bookmark);
  10069. }
  10070. //---------------------------------------------------------------------------
  10071. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  10072. {
  10073. DoBookmarkClick(osLocal, Sender);
  10074. }
  10075. //---------------------------------------------------------------------------
  10076. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  10077. {
  10078. DoBookmarkClick(osRemote, Sender);
  10079. }
  10080. //---------------------------------------------------------------------------
  10081. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  10082. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  10083. {
  10084. if (BookmarkList != NULL)
  10085. {
  10086. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  10087. if (IsLocalBrowserMode())
  10088. {
  10089. Side = osLocal;
  10090. }
  10091. if (!WinConfiguration->UseLocationProfiles)
  10092. {
  10093. std::unique_ptr<TStringList> Directories(new TStringList());
  10094. Directories->CaseSensitive = (Side == osRemote);
  10095. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10096. {
  10097. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10098. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  10099. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  10100. {
  10101. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10102. Item->Caption = EscapeHotkey(Directory);
  10103. Item->ShortCut = Bookmark->ShortCut;
  10104. Item->OnClick = OnBookmarkClick;
  10105. Item->Tag = reinterpret_cast<int>(Bookmark);
  10106. Directories->Add(Directory);
  10107. Menu->Add(Item.release());
  10108. }
  10109. }
  10110. }
  10111. else
  10112. {
  10113. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  10114. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10115. {
  10116. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10117. if (!Bookmark->Node.IsEmpty())
  10118. {
  10119. Folders->Add(Bookmark->Node);
  10120. }
  10121. }
  10122. for (int Index = 0; Index < Folders->Count; Index++)
  10123. {
  10124. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  10125. Item->Caption = Folders->Strings[Index];
  10126. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction2->ImageIndex;
  10127. Folders->Objects[Index] = Item.get();
  10128. Menu->Add(Item.release());
  10129. }
  10130. for (int Index = 0; Index < BookmarkList->Count; Index++)
  10131. {
  10132. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  10133. TTBCustomItem * Parent;
  10134. if (!Bookmark->Node.IsEmpty())
  10135. {
  10136. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  10137. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  10138. }
  10139. else
  10140. {
  10141. Parent = Menu;
  10142. }
  10143. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  10144. Item->Caption = Bookmark->Name;
  10145. Item->ShortCut = Bookmark->ShortCut;
  10146. Item->OnClick = OnBookmarkClick;
  10147. Item->Tag = reinterpret_cast<int>(Bookmark);
  10148. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  10149. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  10150. {
  10151. // No hint for location profiles that are actually mere "bookmarks"
  10152. }
  10153. else
  10154. {
  10155. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  10156. UnicodeString LongHint;
  10157. if (!Bookmark->Local.IsEmpty())
  10158. {
  10159. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  10160. }
  10161. if (!Bookmark->Remote.IsEmpty())
  10162. {
  10163. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  10164. }
  10165. AddToList(Hint, LongHint, L"|");
  10166. Item->Hint = Hint;
  10167. }
  10168. Parent->Add(Item.release());
  10169. }
  10170. }
  10171. AddMenuSeparator(Menu);
  10172. }
  10173. }
  10174. //---------------------------------------------------------------------------
  10175. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  10176. {
  10177. Menu->Clear();
  10178. std::unique_ptr<TTBCustomItem> Item;
  10179. Item.reset(new TTBXItem(Owner));
  10180. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  10181. Item->Options = Item->Options << tboDefault;
  10182. Menu->Add(Item.release());
  10183. AddMenuSeparator(Menu);
  10184. if (Terminal != NULL)
  10185. {
  10186. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  10187. }
  10188. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  10189. Item.reset(new TTBXItem(Owner));
  10190. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction2 : NonVisualDataModule->RemoteAddBookmarkAction2;
  10191. Menu->Add(Item.release());
  10192. }
  10193. //---------------------------------------------------------------------------
  10194. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  10195. {
  10196. bool Result = true;
  10197. bool Remote = !IsSideLocalBrowser(Side);
  10198. try
  10199. {
  10200. if (Remote)
  10201. {
  10202. Terminal->ExceptionOnFail = true;
  10203. }
  10204. try
  10205. {
  10206. DirView(Side)->Path = Path;
  10207. }
  10208. __finally
  10209. {
  10210. if (Remote)
  10211. {
  10212. Terminal->ExceptionOnFail = false;
  10213. }
  10214. }
  10215. }
  10216. catch (Exception & E)
  10217. {
  10218. if (!Remote || Terminal->Active)
  10219. {
  10220. ShowExtendedExceptionEx(Terminal, &E);
  10221. Result = false;
  10222. }
  10223. else
  10224. {
  10225. throw;
  10226. }
  10227. }
  10228. return Result;
  10229. }
  10230. //---------------------------------------------------------------------------
  10231. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  10232. {
  10233. // Make sure there some feedback even when loading tiny local folders
  10234. TInstantOperationVisualizer Visualizer;
  10235. DirView(Side)->ReloadDirectory();
  10236. }
  10237. //---------------------------------------------------------------------------
  10238. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  10239. {
  10240. if (Index == SessionsPageControl->TabIndex)
  10241. {
  10242. CloseSession();
  10243. }
  10244. else
  10245. {
  10246. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10247. TTerminal * Terminal = GetSessionTabSession(TabSheet);
  10248. TTerminalManager * Manager = TTerminalManager::Instance();
  10249. if (!Terminal->Active)
  10250. {
  10251. Manager->FreeTerminal(Terminal);
  10252. }
  10253. else
  10254. {
  10255. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  10256. if (CanCloseQueue(Queue))
  10257. {
  10258. Manager->FreeTerminal(Terminal);
  10259. }
  10260. }
  10261. }
  10262. }
  10263. //---------------------------------------------------------------------------
  10264. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabButtonClick(TPageControl *, int Index)
  10265. {
  10266. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  10267. if (Session == NULL)
  10268. {
  10269. TRect ButtonRect = SessionsPageControl->TabButtonRect(Index);
  10270. ButtonRect = TRect(SessionsPageControl->ClientToScreen(ButtonRect.TopLeft()), SessionsPageControl->ClientToScreen(ButtonRect.BottomRight()));
  10271. //TPoint P = TPoint(ButtonRect.Left, ButtonRect.Bottom);
  10272. //P = SessionsPageControl->ClientToScreen(P);
  10273. MenuPopup(NonVisualDataModule->NewTabPopup, ButtonRect, SessionsPageControl);
  10274. }
  10275. else
  10276. {
  10277. CloseSessionTab(Index);
  10278. }
  10279. }
  10280. //---------------------------------------------------------------------------
  10281. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  10282. {
  10283. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  10284. {
  10285. TInstantOperationVisualizer Visualizer;
  10286. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  10287. // before we create a new data object
  10288. ClipboardClear();
  10289. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  10290. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  10291. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  10292. // Intermediate variable to WORKAROUND a crash in compiler
  10293. TObjectList * ClipboardFakeMonitors =
  10294. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  10295. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  10296. if (FClipboardDragDropFilesEx.get() == NULL)
  10297. {
  10298. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  10299. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  10300. }
  10301. FClipboardDragDropFilesEx->FileList->Clear();
  10302. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  10303. FClipboardDragDropFilesEx->CopyToClipboard();
  10304. FClipboardTerminal = Terminal;
  10305. // Need full paths, as cwd can be different once files are pasted
  10306. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  10307. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  10308. {
  10309. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  10310. }
  10311. }
  10312. }
  10313. //---------------------------------------------------------------------------
  10314. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  10315. {
  10316. if (OpenClipboard(0))
  10317. {
  10318. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  10319. CloseClipboard();
  10320. }
  10321. }
  10322. //---------------------------------------------------------------------------
  10323. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  10324. {
  10325. return !FClipboardFakeDirectory.IsEmpty();
  10326. }
  10327. //---------------------------------------------------------------------------
  10328. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  10329. {
  10330. RemoveDir(ApiPath(FClipboardFakeDirectory));
  10331. FClipboardFakeDirectory = UnicodeString();
  10332. FClipboardTerminal = NULL;
  10333. if (FDownloadingFromClipboard)
  10334. {
  10335. // We are called by the monitor, so attempt to release it would deadlock
  10336. FClipboardFakeMonitorsPendingReset = true;
  10337. }
  10338. else
  10339. {
  10340. FClipboardFakeMonitors.reset(NULL);
  10341. }
  10342. }
  10343. //---------------------------------------------------------------------------
  10344. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10345. {
  10346. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10347. {
  10348. ClipboardStop();
  10349. }
  10350. }
  10351. //---------------------------------------------------------------------------
  10352. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10353. {
  10354. TTerminalManager * Manager = TTerminalManager::Instance();
  10355. Manager->ActiveSession = FClipboardTerminal;
  10356. TTransferOperationParam Params;
  10357. Params.TargetDirectory = TargetDirectory;
  10358. Params.DragDrop = DragDrop;
  10359. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10360. }
  10361. //---------------------------------------------------------------------------
  10362. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10363. {
  10364. // It can actually rarelly happen that some random file is created, while we are shutting down the monitor
  10365. // (as it pumps a Windows message queue while being shutted down)
  10366. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10367. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10368. {
  10369. // Can fail as it can be e.g. locked by AV, so we retry that later
  10370. bool Removed = RemoveDir(ApiPath(FileName));
  10371. try
  10372. {
  10373. if (!NonVisualDataModule->Busy)
  10374. {
  10375. DebugAssert(!FClipboardFakeMonitorsPendingReset);
  10376. FClipboardFakeMonitorsPendingReset = false;
  10377. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10378. TAutoFlag Flag(FDownloadingFromClipboard);
  10379. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  10380. }
  10381. }
  10382. __finally
  10383. {
  10384. if (!Removed)
  10385. {
  10386. RemoveDir(ApiPath(FileName));
  10387. }
  10388. }
  10389. }
  10390. }
  10391. //---------------------------------------------------------------------------
  10392. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10393. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10394. {
  10395. TFileMasks::TParams MaskParams;
  10396. MaskParams.Size = Size;
  10397. MaskParams.Modification = Modification;
  10398. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10399. bool Local = (dynamic_cast<TUnixDirView *>(DirView) == NULL);
  10400. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10401. {
  10402. bool ImplicitMatch;
  10403. if (Iter->FileMask.Matches(FileName, Local, Directory, &MaskParams, false, ImplicitMatch) &&
  10404. !ImplicitMatch)
  10405. {
  10406. Color = Iter->Color;
  10407. break;
  10408. }
  10409. }
  10410. }
  10411. //---------------------------------------------------------------------------
  10412. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10413. {
  10414. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10415. if (!DirView->IsEditing())
  10416. {
  10417. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10418. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10419. // while searching
  10420. if (Key == VK_BACK)
  10421. {
  10422. if (!FIncrementalSearch.IsEmpty())
  10423. {
  10424. if (FIncrementalSearch.Length() == 1)
  10425. {
  10426. ResetIncrementalSearch();
  10427. }
  10428. else
  10429. {
  10430. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  10431. IncrementalSearch(NewText, false, false);
  10432. }
  10433. Key = 0;
  10434. }
  10435. }
  10436. }
  10437. }
  10438. //---------------------------------------------------------------------------
  10439. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  10440. {
  10441. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10442. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  10443. {
  10444. // Filter control sequences.
  10445. // When not searching yet, prefer use of the space for toggling file selection
  10446. // (so we cannot incrementally search for a string starting with a space).
  10447. if ((Key > VK_SPACE) ||
  10448. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  10449. {
  10450. IncrementalSearch(FIncrementalSearch + Key, false, false);
  10451. Key = 0;
  10452. }
  10453. }
  10454. }
  10455. //---------------------------------------------------------------------------
  10456. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  10457. {
  10458. if (!FIncrementalSearch.IsEmpty())
  10459. {
  10460. FIncrementalSearch = L"";
  10461. DirView(osCurrent)->UpdateStatusBar();
  10462. }
  10463. }
  10464. //---------------------------------------------------------------------------
  10465. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10466. {
  10467. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  10468. if (Item == NULL)
  10469. {
  10470. MessageBeep(MB_ICONHAND);
  10471. }
  10472. else
  10473. {
  10474. TCustomDirView * ADirView = DirView(osCurrent);
  10475. {
  10476. TAutoNestingCounter Guard(FIncrementalSearching);
  10477. ADirView->FocusItem(Item);
  10478. if (ADirView->NortonLike == nlOff)
  10479. {
  10480. if ((ADirView->Selected != Item) ||
  10481. (ADirView->SelCount > 1))
  10482. {
  10483. ADirView->ClearSelection();
  10484. ADirView->Selected = Item;
  10485. }
  10486. }
  10487. }
  10488. FIncrementalSearch = Text;
  10489. Item->MakeVisible(false);
  10490. TListItem * NextItem = SearchFile(Text, true, Reverse);
  10491. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  10492. ADirView->UpdateStatusBar();
  10493. }
  10494. }
  10495. //---------------------------------------------------------------------------
  10496. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  10497. {
  10498. int Index = Item->Index;
  10499. if (!Reverse)
  10500. {
  10501. Index++;
  10502. if (Index >= Item->Owner->Count)
  10503. {
  10504. Index = 0;
  10505. }
  10506. }
  10507. else
  10508. {
  10509. Index--;
  10510. if (Index < 0)
  10511. {
  10512. Index = Item->Owner->Count - 1;
  10513. }
  10514. }
  10515. return Item->Owner->Item[Index];
  10516. }
  10517. //---------------------------------------------------------------------------
  10518. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10519. {
  10520. TCustomDirView * ADirView = DirView(osCurrent);
  10521. if (ADirView->Items->Count == 0)
  10522. {
  10523. return NULL;
  10524. }
  10525. else
  10526. {
  10527. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  10528. TListItem * Item = CurrentItem;
  10529. if (SkipCurrent)
  10530. {
  10531. Item = DebugNotNull(GetNextFile(Item, Reverse));
  10532. }
  10533. while (true)
  10534. {
  10535. bool Matches = false;
  10536. switch (WinConfiguration->PanelSearch)
  10537. {
  10538. case isNameStartOnly:
  10539. Matches = ContainsTextSemiCaseSensitive(Item->Caption.SubString(1, Text.Length()), Text);
  10540. break;
  10541. case isName:
  10542. Matches = ContainsTextSemiCaseSensitive(Item->Caption, Text);
  10543. break;
  10544. case isAll:
  10545. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  10546. int Index = 0;
  10547. while ((Index < ColCount) && !Matches)
  10548. {
  10549. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  10550. if (Visible)
  10551. {
  10552. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  10553. }
  10554. Index++;
  10555. }
  10556. break;
  10557. }
  10558. if (Matches)
  10559. {
  10560. return Item;
  10561. }
  10562. Item = GetNextFile(Item, Reverse);
  10563. if (Item == CurrentItem)
  10564. {
  10565. return NULL;
  10566. }
  10567. }
  10568. }
  10569. }
  10570. //---------------------------------------------------------------------------
  10571. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10572. {
  10573. ResetIncrementalSearch();
  10574. }
  10575. //---------------------------------------------------------------------------
  10576. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10577. {
  10578. if (Message.CharCode == VK_TAB)
  10579. {
  10580. if (!FIncrementalSearch.IsEmpty())
  10581. {
  10582. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10583. bool Reverse = Shift.Contains(ssShift);
  10584. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10585. Message.Result = 1;
  10586. return;
  10587. }
  10588. }
  10589. else if (Message.CharCode == VK_ESCAPE)
  10590. {
  10591. if (!FIncrementalSearch.IsEmpty())
  10592. {
  10593. ResetIncrementalSearch();
  10594. Message.Result = 1;
  10595. return;
  10596. }
  10597. }
  10598. TForm::Dispatch(&Message);
  10599. }
  10600. //---------------------------------------------------------------------------
  10601. void __fastcall TCustomScpExplorerForm::Deactivate()
  10602. {
  10603. TForm::Deactivate();
  10604. // E.g. when switching to an internal editor window
  10605. ResetIncrementalSearch();
  10606. }
  10607. //---------------------------------------------------------------------------
  10608. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10609. {
  10610. // When switching to another application
  10611. ResetIncrementalSearch();
  10612. }
  10613. //---------------------------------------------------------------------------
  10614. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10615. {
  10616. ResetIncrementalSearch();
  10617. }
  10618. //---------------------------------------------------------------------------
  10619. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10620. {
  10621. if (FIncrementalSearching <= 0)
  10622. {
  10623. ResetIncrementalSearch();
  10624. }
  10625. }
  10626. //---------------------------------------------------------------------------
  10627. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10628. {
  10629. CountClicksForWindowPrint(this);
  10630. }
  10631. //---------------------------------------------------------------------------
  10632. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10633. {
  10634. CenterReconnectToolbar();
  10635. }
  10636. //---------------------------------------------------------------------------
  10637. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10638. {
  10639. if (RemoteDirView->ItemFocused != NULL)
  10640. {
  10641. RemoteDirView->ItemFocused->Selected = true;
  10642. }
  10643. }
  10644. //---------------------------------------------------------------------------
  10645. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  10646. {
  10647. if (!FQueueStatusUpdating)
  10648. {
  10649. bool Refresh;
  10650. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  10651. {
  10652. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  10653. }
  10654. else
  10655. {
  10656. if (FQueueFileList->GetCount() > 0)
  10657. {
  10658. FQueueFileList->Clear();
  10659. Refresh = true;
  10660. }
  10661. }
  10662. int Count = FQueueFileList->GetCount();
  10663. if (QueueFileList->Items->Count != Count)
  10664. {
  10665. DebugAssert(Refresh);
  10666. QueueFileList->Items->Count = Count;
  10667. }
  10668. if (Refresh)
  10669. {
  10670. QueueFileList->Invalidate();
  10671. }
  10672. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  10673. }
  10674. }
  10675. //---------------------------------------------------------------------------
  10676. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  10677. {
  10678. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  10679. }
  10680. //---------------------------------------------------------------------------
  10681. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  10682. {
  10683. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  10684. // See TSynchronizeChecklistDialog::UpdateTimer
  10685. if (QueueFileList->Items->Count > 0)
  10686. {
  10687. QueueFileList->Items->Item[0]->MakeVisible(false);
  10688. }
  10689. UpdateQueueFileList();
  10690. }
  10691. //---------------------------------------------------------------------------
  10692. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  10693. {
  10694. Active = QueueView3->Focused() || QueueFileList->Focused();
  10695. }
  10696. //---------------------------------------------------------------------------
  10697. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  10698. {
  10699. QueueLabelUpdateStatus();
  10700. }
  10701. //---------------------------------------------------------------------------
  10702. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  10703. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  10704. {
  10705. int State = FQueueFileList->GetState(Item->Index);
  10706. if (State == qfsProcessed)
  10707. {
  10708. Sender->Canvas->Font->Color = clGrayText;
  10709. }
  10710. }
  10711. //---------------------------------------------------------------------------
  10712. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  10713. {
  10714. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  10715. QueueFileList->Column[0]->Width =
  10716. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  10717. }
  10718. //---------------------------------------------------------------------------
  10719. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  10720. {
  10721. QueueFileListColumnAutoSize();
  10722. }
  10723. //---------------------------------------------------------------------------
  10724. void __fastcall TCustomScpExplorerForm::CloseApp()
  10725. {
  10726. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  10727. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  10728. // So we clear the flag here.
  10729. DebugAssert(NonVisualDataModule->Busy);
  10730. NonVisualDataModule->EndBusy();
  10731. try
  10732. {
  10733. Close();
  10734. }
  10735. __finally
  10736. {
  10737. NonVisualDataModule->StartBusy();
  10738. }
  10739. }
  10740. //---------------------------------------------------------------------------
  10741. bool __fastcall TCustomScpExplorerForm::IsActiveTerminal(TTerminal * Terminal)
  10742. {
  10743. return (Terminal != NULL) && Terminal->Active;
  10744. }
  10745. //---------------------------------------------------------------------------
  10746. bool __fastcall TCustomScpExplorerForm::HasManagedSession()
  10747. {
  10748. return (ManagedSession != NULL);
  10749. }
  10750. //---------------------------------------------------------------------------
  10751. bool __fastcall TCustomScpExplorerForm::HasActiveTerminal()
  10752. {
  10753. return IsActiveTerminal(Terminal);
  10754. }
  10755. //---------------------------------------------------------------------------
  10756. void TCustomScpExplorerForm::LocalLocalCopy(
  10757. TFileOperation, TOperationSide, bool DebugUsedArg(OnFocused), bool DebugUsedArg(NoConfirmation),
  10758. bool DebugUsedArg(DragDrop), unsigned int DebugUsedArg(Flags))
  10759. {
  10760. DebugFail();
  10761. }
  10762. //---------------------------------------------------------------------------
  10763. void TCustomScpExplorerForm::LocalLocalCopyCommand(TFileOperation Operation, TOperationSide Side, bool OnFocused, unsigned int Flags)
  10764. {
  10765. LocalLocalCopy(Operation, Side, OnFocused, !WinConfiguration->ConfirmTransferring, false, Flags);
  10766. }
  10767. //---------------------------------------------------------------------------
  10768. void __fastcall TCustomScpExplorerForm::SessionsPageControlResize(TObject *)
  10769. {
  10770. // Changing tab list triggers the OnResize.
  10771. if (!FUpdatingSessionTabs)
  10772. {
  10773. // UpdateTabsCaptionTruncation should be enough
  10774. SessionListChanged(true);
  10775. }
  10776. }
  10777. //---------------------------------------------------------------------------
  10778. UnicodeString TCustomScpExplorerForm::GetSessionPath(TManagedTerminal * ASession, TOperationSide Side)
  10779. {
  10780. if (ASession == ManagedSession)
  10781. {
  10782. UpdateSession(ASession);
  10783. }
  10784. TSessionData * Data = ASession->StateData;
  10785. UnicodeString Result;
  10786. if (Side == osLocal)
  10787. {
  10788. Result = Data->LocalDirectory;
  10789. }
  10790. else if (DebugAlwaysTrue(Side == osOther))
  10791. {
  10792. Result = ASession->LocalBrowser ? Data->OtherLocalDirectory : Data->RemoteDirectory;
  10793. }
  10794. return Result;
  10795. }
  10796. //---------------------------------------------------------------------------
  10797. UnicodeString TCustomScpExplorerForm::GetTabHintDetails(TManagedTerminal * ASession)
  10798. {
  10799. UnicodeString Result;
  10800. if (!ASession->Active)
  10801. {
  10802. Result = LoadStr(STATUS_NOT_CONNECTED2);
  10803. }
  10804. else
  10805. {
  10806. Result = GetSessionPath(ASession, osRemote);
  10807. }
  10808. return Result;
  10809. }
  10810. //---------------------------------------------------------------------------
  10811. UnicodeString TCustomScpExplorerForm::GetNewTabHintDetails()
  10812. {
  10813. return LoadStr(NEW_REMOTE_TAB_HINT);
  10814. }
  10815. //---------------------------------------------------------------------------
  10816. void __fastcall TCustomScpExplorerForm::SessionsPageControlTabHint(TPageControl *, int Index, UnicodeString & Hint)
  10817. {
  10818. if (Index >= 0)
  10819. {
  10820. TThemeTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  10821. TManagedTerminal * TabSession = GetSessionTabSession(TabSheet);
  10822. if (TabSession == NULL)
  10823. {
  10824. UnicodeString NewTabTabCaption = GetNewTabTabCaption();
  10825. if (NewTabTabCaption != TabSheet->Caption)
  10826. {
  10827. Hint = NewTabTabCaption + L"|" + GetNewTabHintDetails();
  10828. }
  10829. }
  10830. else
  10831. {
  10832. TTerminalManager * Manager = TTerminalManager::Instance();
  10833. UnicodeString TabCaption = Manager->GetSessionTitle(TabSession, true);
  10834. Hint = TabCaption + L"|" + GetTabHintDetails(TabSession);
  10835. }
  10836. }
  10837. }
  10838. //---------------------------------------------------------------------------
  10839. typedef std::vector<TListItem *> TListItemsVector;
  10840. struct TCalculateSizeOperation
  10841. {
  10842. TCalculateSizeStats Stats;
  10843. TListItemsVector ListItems;
  10844. size_t Index;
  10845. };
  10846. //---------------------------------------------------------------------------
  10847. void TCustomScpExplorerForm::CalculateDirectorySizes()
  10848. {
  10849. TCalculateSizeOperation CalculateSizeOperation;
  10850. TOperationSide Side = GetSide(osCurrent);
  10851. TCustomDirView * ADirView = DirView(Side);
  10852. bool FullPath = IsSideLocalBrowser(Side);
  10853. std::unique_ptr<TStrings> AllFileList(ADirView->CreateFileList(false, FullPath, NULL, true));
  10854. std::unique_ptr<TStrings> FileList(new TStringList());
  10855. for (int Index = 0; Index < AllFileList->Count; Index++)
  10856. {
  10857. TListItem * Item = DebugNotNull(dynamic_cast<TListItem *>(AllFileList->Objects[Index]));
  10858. if (ADirView->ItemIsDirectory(Item))
  10859. {
  10860. FileList->AddObject(AllFileList->Strings[Index], ADirView->ItemData(Item));
  10861. CalculateSizeOperation.ListItems.push_back(Item);
  10862. }
  10863. }
  10864. CalculateSizeOperation.Index = 0;
  10865. __int64 Size = 0;
  10866. TValueRestorer<TCalculateSizeOperation *> DirectorySizeOperationRestorer(FCalculateSizeOperation);
  10867. FCalculateSizeOperation = &CalculateSizeOperation;
  10868. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  10869. FOnFileOperationFinished = DirectorySizeCalculated;
  10870. TValueRestorer<TFileOperation> PrimaryOperationRestorer(FPrimaryOperation);
  10871. FPrimaryOperation = foCalculateSize;
  10872. TCalculatedSizes CalculatedSizes;
  10873. CalculateSizeOperation.Stats.CalculatedSizes = &CalculatedSizes;
  10874. if (Side == osLocal)
  10875. {
  10876. Terminal->CalculateLocalFilesSize(FileList.get(), Size, NULL, true, NULL, &CalculatedSizes);
  10877. }
  10878. else
  10879. {
  10880. TCalculateSizeParams Params;
  10881. Params.Stats = &CalculateSizeOperation.Stats;
  10882. Params.UseCache = true;
  10883. Terminal->CalculateFilesSize(FileList.get(), Size, Params);
  10884. }
  10885. }
  10886. //---------------------------------------------------------------------------
  10887. void __fastcall TCustomScpExplorerForm::DirectorySizeCalculated(TOperationSide Side, const UnicodeString & FileName, bool)
  10888. {
  10889. DebugAlwaysTrue(FCalculateSizeOperation != NULL);
  10890. TListItem * Item = VisualiseOperationFinished(Side, FileName, false);
  10891. TCustomDirView * ADirView = dynamic_cast<TCustomDirView *>(Item->Owner->Owner);
  10892. // optimization
  10893. bool GotExpectedItem =
  10894. (FCalculateSizeOperation->Index < FCalculateSizeOperation->ListItems.size()) &&
  10895. (FCalculateSizeOperation->ListItems[FCalculateSizeOperation->Index] == Item);
  10896. int Index = -1;
  10897. if (DebugAlwaysTrue(GotExpectedItem))
  10898. {
  10899. Index = FCalculateSizeOperation->Index;
  10900. FCalculateSizeOperation->Index++;
  10901. }
  10902. else
  10903. {
  10904. TListItemsVector::iterator I =
  10905. std::find(FCalculateSizeOperation->ListItems.begin(), FCalculateSizeOperation->ListItems.end(), Item);
  10906. if (DebugAlwaysTrue(I != FCalculateSizeOperation->ListItems.end()))
  10907. {
  10908. Index = I - FCalculateSizeOperation->ListItems.begin();
  10909. }
  10910. }
  10911. if (DebugAlwaysTrue(Index >= 0))
  10912. {
  10913. __int64 Size = (*FCalculateSizeOperation->Stats.CalculatedSizes)[Index];
  10914. ADirView->SetItemCalculatedSize(Item, Size);
  10915. }
  10916. }