CustomScpExplorer.cpp 352 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735
  1. //---------------------------------------------------------------------------
  2. #define NO_WIN32_LEAN_AND_MEAN
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include <Common.h>
  6. #include "CustomScpExplorer.h"
  7. #include <Bookmarks.h>
  8. #include <Interface.h>
  9. #include <Exceptions.h>
  10. #include <CoreMain.h>
  11. #include <FileSystems.h>
  12. #include <TextsCore.h>
  13. #include <TextsWin.h>
  14. #include <HelpWin.h>
  15. #include <VCLCommon.h>
  16. #include <Progress.h>
  17. #include <SynchronizeProgress.h>
  18. #include <DragExt.h>
  19. #include <WinApi.h>
  20. #include "GUITools.h"
  21. #include "NonVisual.h"
  22. #include "Glyphs.h"
  23. #include "Tools.h"
  24. #include "WinConfiguration.h"
  25. #include "TerminalManager.h"
  26. #include "EditorManager.h"
  27. #include "ProgParams.h"
  28. #include "Setup.h"
  29. #include <Consts.hpp>
  30. #include <DateUtils.hpp>
  31. #include <TB2Common.hpp>
  32. #include <DirectoryMonitor.hpp>
  33. #include <System.IOUtils.hpp>
  34. //---------------------------------------------------------------------------
  35. #pragma package(smart_init)
  36. #pragma link "CustomDirView"
  37. #pragma link "CustomUnixDirView"
  38. #pragma link "IEListView"
  39. #pragma link "NortonLikeListView"
  40. #pragma link "UnixDirView"
  41. #pragma link "CustomDriveView"
  42. #pragma link "UnixDriveView"
  43. #pragma link "CustomDriveView"
  44. #pragma link "TB2Dock"
  45. #pragma link "TBXStatusBars"
  46. #pragma link "TB2Item"
  47. #pragma link "TB2Toolbar"
  48. #pragma link "ThemePageControl"
  49. #pragma link "PathLabel"
  50. #pragma resource "*.dfm"
  51. //---------------------------------------------------------------------------
  52. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  53. static const int SessionPanelCount = 4;
  54. //---------------------------------------------------------------------------
  55. class TMutexGuard
  56. {
  57. public:
  58. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  59. int Timeout = 5000)
  60. {
  61. FMutex = NULL;
  62. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  63. if (WaitResult == WAIT_TIMEOUT)
  64. {
  65. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  66. }
  67. else
  68. {
  69. FMutex = AMutex;
  70. }
  71. }
  72. ~TMutexGuard()
  73. {
  74. if (FMutex != NULL)
  75. {
  76. ReleaseMutex(FMutex);
  77. }
  78. }
  79. private:
  80. HANDLE FMutex;
  81. };
  82. //---------------------------------------------------------------------------
  83. class TWindowLock
  84. {
  85. public:
  86. TWindowLock(TCustomScpExplorerForm * Form) :
  87. FForm(Form)
  88. {
  89. FForm->LockWindow();
  90. }
  91. ~TWindowLock()
  92. {
  93. FForm->UnlockWindow();
  94. }
  95. private:
  96. TCustomScpExplorerForm * FForm;
  97. };
  98. //---------------------------------------------------------------------------
  99. class TAutoBatch
  100. {
  101. public:
  102. TAutoBatch(TCustomScpExplorerForm * Form) :
  103. FForm(Form)
  104. {
  105. FForm->BatchStart(FBatchStorage);
  106. }
  107. ~TAutoBatch()
  108. {
  109. FForm->BatchEnd(FBatchStorage);
  110. }
  111. private:
  112. TCustomScpExplorerForm * FForm;
  113. void * FBatchStorage;
  114. };
  115. //---------------------------------------------------------------------------
  116. struct TTransferOperationParam
  117. {
  118. TTransferOperationParam();
  119. UnicodeString TargetDirectory;
  120. bool Temp;
  121. bool DragDrop;
  122. int Options;
  123. TAutoSwitch Queue;
  124. };
  125. //---------------------------------------------------------------------------
  126. TTransferOperationParam::TTransferOperationParam()
  127. {
  128. Temp = false;
  129. DragDrop = false;
  130. Options = 0;
  131. Queue = asAuto;
  132. }
  133. //---------------------------------------------------------------------------
  134. class TTransferPresetNoteData : public TObject
  135. {
  136. public:
  137. UnicodeString Message;
  138. };
  139. //---------------------------------------------------------------------------
  140. class TTerminalNoteData : public TObject
  141. {
  142. public:
  143. TManagedTerminal * Terminal;
  144. };
  145. //---------------------------------------------------------------------------
  146. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  147. FFormRestored(false),
  148. TForm(Owner)
  149. {
  150. FInvalid = false;
  151. FCurrentSide = osRemote;
  152. FEverShown = false;
  153. FDocks = new TList();
  154. InitControls();
  155. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  156. // So that UpdateCustomCommandsToolbar called from RestoreParams works
  157. NonVisualDataModule->ScpExplorer = this;
  158. RestoreParams();
  159. ConfigurationChanged();
  160. RemoteDirView->Invalidate();
  161. FAutoOperation = false;
  162. FOnFileOperationFinished = NULL;
  163. FForceExecution = false;
  164. FIgnoreNextDialogChar = 0;
  165. FErrorList = NULL;
  166. FSynchronizeProgressForm = NULL;
  167. FProgressForm = NULL;
  168. FRefreshLocalDirectory = false;
  169. FRefreshRemoteDirectory = false;
  170. FDDExtMapFile = NULL;
  171. // CreateMutexW keeps failing with ERROR_NOACCESS
  172. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  173. DebugAssert(FDDExtMutex != NULL);
  174. FDDTargetControl = NULL;
  175. FDelayedDeletionTimer = NULL;
  176. FDelayedDeletionList = new TStringList();
  177. FDDFileList = NULL;
  178. FPendingTempSpaceWarn = false;
  179. FCapturedLog = NULL;
  180. FDragDropOperation = false;
  181. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  182. FAllowTransferPresetAutoSelect = true;
  183. FCopyParamDefault = L"";
  184. FSynchronizeController = NULL;
  185. FPendingQueueActionItem = NULL;
  186. FLockLevel = 0;
  187. FLockSuspendLevel = 0;
  188. FDisabledOnLockSuspend = false;
  189. FAlternativeDelete = false;
  190. FTrayIcon = new ::TTrayIcon(0);
  191. FTrayIcon->OnClick = TrayIconClick;
  192. FMaxQueueLength = 0;
  193. FLastContextPopupScreenPoint = TPoint(-1, -1);
  194. FTransferResumeList = NULL;
  195. FMoveToQueue = false;
  196. FStandaloneEditing = false;
  197. FOnFeedSynchronizeError = NULL;
  198. FNeedSession = false;
  199. FDoNotIdleCurrentTerminal = 0;
  200. FIncrementalSearching = 0;
  201. FIncrementalSearchHaveNext = false;
  202. FEditorManager = new TEditorManager();
  203. FEditorManager->OnFileChange = ExecutedFileChanged;
  204. FEditorManager->OnFileReload = ExecutedFileReload;
  205. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  206. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  207. FLocalEditors = new TList();
  208. FQueueStatus = NULL;
  209. FQueueStatusSection = new TCriticalSection();
  210. FQueueStatusInvalidated = false;
  211. FQueueItemInvalidated = false;
  212. FQueueActedItem = NULL;
  213. FQueueController = new TQueueController(QueueView3);
  214. FUserActionTimer = new TTimer(this);
  215. FUserActionTimer->Enabled = false;
  216. FUserActionTimer->Interval = 10;
  217. FUserActionTimer->OnTimer = UserActionTimer;
  218. FNotes = new TStringList();
  219. FNoteTimer = new TTimer(this);
  220. FNoteTimer->Enabled = false;
  221. FNoteTimer->OnTimer = NoteTimer;
  222. FOnNoteClick = NULL;
  223. FOle32Library = LoadLibrary(L"Ole32.dll");
  224. FDragMoveCursor = FOle32Library != NULL ?
  225. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  226. UseSystemSettings(this);
  227. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  228. static_cast<TObject*>(GetComponent(fcTransferList)));
  229. DebugAssert(TransferList != NULL);
  230. FTransferListHoverIndex = -1;
  231. TransferList->OnChange = TransferListChange;
  232. TransferList->OnDrawItem = TransferListDrawItem;
  233. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))));
  234. SetSubmenu(NonVisualDataModule->ColorMenuItem);
  235. UseDesktopFont(SessionsPageControl);
  236. UpdateSessionsPageControlHeight();
  237. UseDesktopFont(RemoteDirView);
  238. UseDesktopFont(RemoteDriveView);
  239. UseDesktopFont(QueueView3);
  240. UseDesktopFont(QueueLabel);
  241. UseDesktopFont(RemoteStatusBar);
  242. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  243. QueueSplitter->ShowHint = true;
  244. RemotePanelSplitter->ShowHint = true;
  245. UpdateImages();
  246. FCustomCommandMenu = CreateTBXPopupMenu(this);
  247. FCustomCommandLocalFileList = NULL;
  248. FCustomCommandRemoteFileList = NULL;
  249. FSessionColors = new TPngImageList(this);
  250. FSessionColors->ColorDepth = cd32Bit;
  251. AddFixedSessionImages();
  252. SessionsPageControl->Images = FSessionColors;
  253. QueueLabel->FocusControl = QueueView3;
  254. UpdateQueueLabel();
  255. FRemoteDirViewWasFocused = true;
  256. CreateHiddenWindow();
  257. StartUpdates();
  258. }
  259. //---------------------------------------------------------------------------
  260. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  261. {
  262. FInvalid = true;
  263. // this has to be one of the very first things to do
  264. StopUpdateThread();
  265. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  266. TTerminalManager::Instance()->ScpExplorer = NULL;
  267. delete FCustomCommandLocalFileList;
  268. delete FCustomCommandRemoteFileList;
  269. delete FCustomCommandMenu;
  270. delete FTrayIcon;
  271. FTrayIcon = NULL;
  272. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  273. delete FEditorManager;
  274. ForceCloseLocalEditors();
  275. delete FLocalEditors;
  276. HideFileFindDialog();
  277. if (FDelayedDeletionTimer)
  278. {
  279. DoDelayedDeletion(NULL);
  280. SAFE_DESTROY(FDelayedDeletionTimer);
  281. }
  282. SAFE_DESTROY(FDelayedDeletionList);
  283. // sometimes we do not get DDEnd so the list is not released
  284. SAFE_DESTROY(FDDFileList);
  285. DebugAssert(FSynchronizeController == NULL);
  286. CloseHandle(FDDExtMutex);
  287. FDDExtMutex = NULL;
  288. FreeLibrary(FOle32Library);
  289. FOle32Library = NULL;
  290. FDragMoveCursor = NULL;
  291. DebugAssert(!FErrorList);
  292. if (FEverShown)
  293. {
  294. // when window is never shown (like when running command-line operation),
  295. // particularly window site is not restored correctly (BoundsRect value set
  296. // in RestoreForm gets lost during handle allocation), so we do not want
  297. // it to be stored
  298. StoreParams();
  299. }
  300. Terminal = NULL;
  301. Queue = NULL;
  302. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  303. NonVisualDataModule->ScpExplorer = NULL;
  304. delete FQueueController;
  305. FQueueController = NULL;
  306. delete FQueueStatusSection;
  307. FQueueStatusSection = NULL;
  308. delete FQueueStatus;
  309. FQueueStatus = NULL;
  310. delete FUserActionTimer;
  311. FUserActionTimer = NULL;
  312. delete FNoteTimer;
  313. delete FNotes;
  314. delete FNoteData;
  315. SAFE_DESTROY(FDocks);
  316. SessionsPageControl->Images = NULL;
  317. // only after clearing Terminal (after DoTerminalListChanged => ... => AddSessionColor is called)
  318. SAFE_DESTROY(FSessionColors);
  319. SAFE_DESTROY(FSessionsDragDropFilesEx);
  320. SAFE_DESTROY(FHistoryMenu[0][0]);
  321. SAFE_DESTROY(FHistoryMenu[0][1]);
  322. SAFE_DESTROY(FHistoryMenu[1][0]);
  323. SAFE_DESTROY(FHistoryMenu[1][1]);
  324. if (FHiddenWindow != NULL)
  325. {
  326. DestroyWindow(FHiddenWindow);
  327. FHiddenWindow = NULL;
  328. }
  329. }
  330. //---------------------------------------------------------------------------
  331. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  332. {
  333. std::unique_ptr<TSessionData> Data;
  334. if (!Session.IsEmpty())
  335. {
  336. bool DefaultsOnly;
  337. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  338. }
  339. TTerminalManager * Manager = TTerminalManager::Instance();
  340. for (int Index = 0; Index < Manager->Count; Index++)
  341. {
  342. TTerminal * Terminal = Manager->Terminals[Index];
  343. if (Session.IsEmpty() ||
  344. Terminal->SessionData->IsSameSite(Data.get()))
  345. {
  346. if (Path.IsEmpty())
  347. {
  348. Terminal->ClearCaches();
  349. }
  350. else
  351. {
  352. Terminal->DirectoryModified(Path, true);
  353. }
  354. }
  355. }
  356. // We should flag a pending refresh for the background terminals or busy foreground terminals
  357. if ((Terminal != NULL) && Terminal->Active &&
  358. CanCommandLineFromAnotherInstance() &&
  359. (Session.IsEmpty() || Terminal->SessionData->IsSameSite(Data.get())) &&
  360. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  361. {
  362. Terminal->ReloadDirectory();
  363. }
  364. }
  365. //---------------------------------------------------------------------------
  366. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  367. {
  368. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  369. size_t MessageSize = sizeof(TCopyDataMessage);
  370. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  371. if (Result)
  372. {
  373. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  374. Result = (Message.Version == TCopyDataMessage::Version1);
  375. if (Result)
  376. {
  377. switch (Message.Command)
  378. {
  379. case TCopyDataMessage::CommandCanCommandLine:
  380. Result = CanCommandLineFromAnotherInstance();
  381. break;
  382. case TCopyDataMessage::CommandCommandLine:
  383. {
  384. UnicodeString CommandLine(Message.CommandLine);
  385. Result = CommandLineFromAnotherInstance(CommandLine);
  386. }
  387. break;
  388. case TCopyDataMessage::RefreshPanel:
  389. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  390. break;
  391. case TCopyDataMessage::MainWindowCheck:
  392. Result = true;
  393. break;
  394. default:
  395. Result = false;
  396. break;
  397. }
  398. }
  399. }
  400. Message.Result = Result ? 1 : 0;
  401. }
  402. //---------------------------------------------------------------------------
  403. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  404. {
  405. WNDCLASS WindowClass = {0};
  406. WindowClass.lpfnWndProc = DefWindowProc;
  407. WindowClass.hInstance = HInstance;
  408. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  409. FHiddenWindow = NULL;
  410. if (RegisterClass(&WindowClass))
  411. {
  412. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  413. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  414. }
  415. }
  416. //---------------------------------------------------------------------------
  417. bool __fastcall TCustomScpExplorerForm::CanConsole()
  418. {
  419. return
  420. (Terminal != NULL) &&
  421. Terminal->Active &&
  422. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  423. }
  424. //---------------------------------------------------------------------------
  425. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  426. {
  427. bool Result = Visible && !NonVisualDataModule->Busy;
  428. return Result;
  429. }
  430. //---------------------------------------------------------------------------
  431. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  432. const UnicodeString & CommandLine)
  433. {
  434. TProgramParams Params(CommandLine);
  435. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  436. if (Result)
  437. {
  438. NonVisualDataModule->StartBusy();
  439. try
  440. {
  441. // this action is initiated from another process,
  442. // so it's likely that our window is not visible,
  443. // and user won't see what is going on
  444. Application->BringToFront();
  445. // reload sessions as we may be asked to open a session
  446. // just stored by another instance
  447. StoredSessions->Reload();
  448. UnicodeString SessionName = Params.Param[1];
  449. std::unique_ptr<TObjectList> DataList(new TObjectList());
  450. UnicodeString DownloadFile; // unused
  451. try
  452. {
  453. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this);
  454. }
  455. catch (EAbort &)
  456. {
  457. // Happens when opening session in PuTTY and in other situations,
  458. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  459. // to process the command-line on its own (or to pass it to yet another instance)
  460. }
  461. if (DataList->Count > 0)
  462. {
  463. TTerminalManager * Manager = TTerminalManager::Instance();
  464. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  465. }
  466. }
  467. __finally
  468. {
  469. NonVisualDataModule->EndBusy();
  470. }
  471. }
  472. return Result;
  473. }
  474. //---------------------------------------------------------------------------
  475. void __fastcall TCustomScpExplorerForm::SetTerminal(TManagedTerminal * value)
  476. {
  477. if (FTerminal != value)
  478. {
  479. TerminalChanging();
  480. DoSetTerminal(value, false);
  481. }
  482. }
  483. //---------------------------------------------------------------------------
  484. void __fastcall TCustomScpExplorerForm::DoSetTerminal(TManagedTerminal * value, bool Replace)
  485. {
  486. FTerminal = value;
  487. bool PrevAllowTransferPresetAutoSelect = FAllowTransferPresetAutoSelect;
  488. FAllowTransferPresetAutoSelect = false;
  489. try
  490. {
  491. TerminalChanged(Replace);
  492. }
  493. __finally
  494. {
  495. FAllowTransferPresetAutoSelect = PrevAllowTransferPresetAutoSelect;
  496. }
  497. if (Terminal != NULL)
  498. {
  499. TransferPresetAutoSelect();
  500. }
  501. }
  502. //---------------------------------------------------------------------------
  503. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  504. {
  505. DoSetTerminal(value, true);
  506. }
  507. //---------------------------------------------------------------------------
  508. void __fastcall TCustomScpExplorerForm::TerminalChanging()
  509. {
  510. if (FTerminal != NULL)
  511. {
  512. UpdateTerminal(Terminal);
  513. }
  514. }
  515. //---------------------------------------------------------------------------
  516. void __fastcall TCustomScpExplorerForm::TerminalChanged(bool Replaced)
  517. {
  518. if (Terminal != NULL)
  519. {
  520. UpdateSessionColor((TColor)Terminal->StateData->Color);
  521. }
  522. DoTerminalListChanged();
  523. DebugAssert(!IsLocalBrowserMode()); // TODO
  524. if (Replaced)
  525. {
  526. RemoteDirView->ReplaceTerminal(Terminal);
  527. }
  528. else
  529. {
  530. RemoteDirView->Terminal = Terminal;
  531. }
  532. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  533. if (Terminal != NULL)
  534. {
  535. if (Terminal->Active)
  536. {
  537. Terminal->RefreshDirectory();
  538. }
  539. if (WinConfiguration->PreservePanelState)
  540. {
  541. if (Terminal->RemoteExplorerState != NULL)
  542. {
  543. DirView(osRemote)->RestoreState(Terminal->RemoteExplorerState);
  544. }
  545. else
  546. {
  547. DirView(osRemote)->ClearState();
  548. }
  549. }
  550. if (!Replaced && Terminal->Active)
  551. {
  552. InitStatusBar();
  553. }
  554. }
  555. UpdateTransferList();
  556. // Update panels Enable state before refreshing the labels
  557. UpdateControls();
  558. }
  559. //---------------------------------------------------------------------------
  560. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  561. {
  562. if (Queue != value)
  563. {
  564. if (FQueue != NULL)
  565. {
  566. FQueue->OnListUpdate = NULL;
  567. FQueue->OnQueueItemUpdate = NULL;
  568. }
  569. FQueue = value;
  570. if (FQueue != NULL)
  571. {
  572. DebugAssert(FQueue->OnListUpdate == NULL);
  573. FQueue->OnListUpdate = QueueListUpdate;
  574. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  575. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  576. }
  577. QueueChanged();
  578. }
  579. }
  580. //---------------------------------------------------------------------------
  581. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  582. TListItem * Item)
  583. {
  584. if (FQueueActedItem == Item)
  585. {
  586. FQueueActedItem = NULL;
  587. if ((QueueView3->PopupMenu != NULL) &&
  588. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  589. {
  590. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  591. ReleaseCapture();
  592. }
  593. }
  594. if (Item->Data == FPendingQueueActionItem)
  595. {
  596. FPendingQueueActionItem = NULL;
  597. }
  598. }
  599. //---------------------------------------------------------------------------
  600. bool TCustomScpExplorerForm::IsAnythingQueued()
  601. {
  602. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  603. }
  604. //---------------------------------------------------------------------------
  605. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  606. {
  607. {
  608. TGuard Guard(FQueueStatusSection);
  609. FQueueStatusInvalidated = false;
  610. if (FQueue != NULL)
  611. {
  612. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  613. }
  614. }
  615. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  616. {
  617. FMaxQueueLength = FQueueStatus->Count;
  618. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  619. }
  620. FQueueController->UpdateQueueStatus(FQueueStatus);
  621. SetQueueProgress();
  622. UpdateQueueView();
  623. bool IsEmpty = !IsAnythingQueued();
  624. if (IsEmpty && (Terminal != NULL))
  625. {
  626. TOnceDoneOperation OnceDoneOperation =
  627. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  628. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  629. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  630. {
  631. FQueue->Enabled = false;
  632. }
  633. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  634. {
  635. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  636. }
  637. }
  638. }
  639. //---------------------------------------------------------------------------
  640. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  641. {
  642. UnicodeString Result;
  643. if (FQueueStatus != NULL)
  644. {
  645. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  646. {
  647. TFileOperationProgressType * ProgressData =
  648. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  649. if ((ProgressData != NULL) && ProgressData->InProgress)
  650. {
  651. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  652. }
  653. }
  654. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  655. {
  656. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  657. }
  658. }
  659. return Result;
  660. }
  661. //---------------------------------------------------------------------------
  662. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  663. {
  664. ComponentVisible[fcQueueView] =
  665. (WinConfiguration->QueueView.Show == qvShow) ||
  666. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  667. (FQueueStatus != NULL) && (FQueueStatus->Count > 0));
  668. }
  669. //---------------------------------------------------------------------------
  670. void __fastcall TCustomScpExplorerForm::QueueChanged()
  671. {
  672. if (FQueueStatus != NULL)
  673. {
  674. delete FQueueStatus;
  675. FQueueStatus = NULL;
  676. }
  677. UpdateQueueStatus(true);
  678. }
  679. //---------------------------------------------------------------------------
  680. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  681. {
  682. if (FQueue == Queue)
  683. {
  684. FQueueStatusInvalidated = true;
  685. }
  686. }
  687. //---------------------------------------------------------------------------
  688. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  689. TQueueItem * Item)
  690. {
  691. if (FQueue == Queue)
  692. {
  693. TGuard Guard(FQueueStatusSection);
  694. // this may be running in parallel with QueueChanged
  695. if (FQueueStatus != NULL)
  696. {
  697. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  698. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  699. {
  700. FRefreshLocalDirectory = (QueueItem == NULL) ||
  701. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  702. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  703. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  704. }
  705. if (QueueItem != NULL)
  706. {
  707. QueueItem->UserData = (void*)true;
  708. FQueueItemInvalidated = true;
  709. }
  710. }
  711. }
  712. }
  713. //---------------------------------------------------------------------------
  714. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  715. {
  716. // during standalone editing, we have no way to see/control queue,
  717. // so we have to always popup prompts automatically
  718. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  719. }
  720. //---------------------------------------------------------------------------
  721. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  722. {
  723. if (FQueueStatus != NULL)
  724. {
  725. bool QueueAutoPopup = IsQueueAutoPopup();
  726. bool NeedRefresh = FQueueController->NeedRefresh();
  727. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  728. FQueueItemInvalidated = false;
  729. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  730. bool Updated = false;
  731. TQueueItemProxy * QueueItem;
  732. bool WasUserAction;
  733. for (int Index = 0; Index < Limit; Index++)
  734. {
  735. bool Update = false;
  736. QueueItem = FQueueStatus->Items[Index];
  737. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  738. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  739. (FPendingQueueActionItem == NULL))
  740. {
  741. FPendingQueueActionItem = QueueItem;
  742. FUserActionTimer->Enabled = true;
  743. }
  744. if ((bool)QueueItem->UserData)
  745. {
  746. QueueItem->UserData = (void*)false;
  747. QueueItem->Update();
  748. Updated = true;
  749. Update = true;
  750. }
  751. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  752. {
  753. Update = true;
  754. }
  755. if (Update)
  756. {
  757. FQueueController->RefreshQueueItem(QueueItem);
  758. }
  759. }
  760. if (Updated)
  761. {
  762. NonVisualDataModule->UpdateNonVisibleActions();
  763. SetQueueProgress();
  764. }
  765. }
  766. }
  767. //---------------------------------------------------------------------------
  768. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  769. {
  770. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  771. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  772. }
  773. //---------------------------------------------------------------------------
  774. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  775. {
  776. if (Progress >= 0)
  777. {
  778. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  779. }
  780. else
  781. {
  782. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  783. }
  784. }
  785. //---------------------------------------------------------------------------
  786. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  787. {
  788. int OverallProgress;
  789. // FProgressForm is null when this is called from SetQueueProgress
  790. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  791. {
  792. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  793. }
  794. else if (!TFileOperationProgressType::IsIndeterminateOperation(ProgressData->Operation))
  795. {
  796. OverallProgress = ProgressData->OverallProgress();
  797. }
  798. else
  799. {
  800. OverallProgress = -1;
  801. }
  802. SetTaskbarListProgressValue(OverallProgress);
  803. }
  804. //---------------------------------------------------------------------------
  805. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  806. {
  807. TTerminalManager::Instance()->QueueStatusUpdated();
  808. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  809. {
  810. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  811. {
  812. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  813. {
  814. TFileOperationProgressType * ProgressData = NULL;
  815. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  816. {
  817. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  818. }
  819. if ((ProgressData != NULL) &&
  820. ProgressData->InProgress)
  821. {
  822. SetTaskbarListProgressValue(ProgressData);
  823. }
  824. else
  825. {
  826. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  827. }
  828. }
  829. else
  830. {
  831. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  832. }
  833. }
  834. else
  835. {
  836. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  837. }
  838. }
  839. UpdateQueueLabel();
  840. }
  841. //---------------------------------------------------------------------------
  842. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  843. {
  844. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  845. if (FQueueStatus != NULL)
  846. {
  847. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  848. if (ActiveAndPendingPrimaryCount > 0)
  849. {
  850. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  851. }
  852. }
  853. QueueLabel->Caption = Caption;
  854. }
  855. //---------------------------------------------------------------------------
  856. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  857. {
  858. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  859. static_cast<TComponent*>(GetComponent(fcTransferList)));
  860. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  861. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  862. TransferList->Strings->BeginUpdate();
  863. try
  864. {
  865. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  866. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  867. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  868. if (FTransferDropDownHint.IsEmpty())
  869. {
  870. FTransferDropDownHint = TransferDropDown->Hint;
  871. }
  872. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  873. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  874. TransferDropDown->Text = StripHotkey(Name);
  875. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  876. (FTransferDropDownHint, StripHotkey(Name),
  877. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  878. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General))));
  879. // update the label, otherwise when it is updated only on the first draw
  880. // of the list, it is drawn "bold" for some reason
  881. FTransferListHoverIndex = TransferList->ItemIndex;
  882. UpdateTransferLabel();
  883. }
  884. __finally
  885. {
  886. TransferList->Strings->EndUpdate();
  887. }
  888. }
  889. //---------------------------------------------------------------------------
  890. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  891. {
  892. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  893. DebugAssert(Toolbar != NULL);
  894. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  895. }
  896. //---------------------------------------------------------------------------
  897. void __fastcall TCustomScpExplorerForm::UpdateActions()
  898. {
  899. TForm::UpdateActions();
  900. if (ComponentVisible[fcCustomCommandsBand])
  901. {
  902. UpdateCustomCommandsToolbar();
  903. }
  904. }
  905. //---------------------------------------------------------------------------
  906. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  907. {
  908. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  909. }
  910. //---------------------------------------------------------------------------
  911. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  912. {
  913. Color = GetBtnFaceColor();
  914. DebugAssert(Configuration && RemoteDirView);
  915. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  916. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  917. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  918. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  919. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  920. if (RemoteDirView->RowSelect != WinConfiguration->FullRowSelect)
  921. {
  922. RemoteDirView->RowSelect = WinConfiguration->FullRowSelect;
  923. // selection is not redrawn automatically when RowSelect changes
  924. RemoteDirView->Invalidate();
  925. }
  926. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  927. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  928. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  929. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  930. UpdateSessionsPageControlHeight();
  931. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  932. UpdateToolbarDisplayMode();
  933. UpdateTransferList();
  934. if (Terminal != NULL)
  935. {
  936. TransferPresetAutoSelect();
  937. }
  938. UpdateQueueView();
  939. UpdateControls();
  940. // this can be called even before constuctor finishes.
  941. if (FEditorManager != NULL)
  942. {
  943. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  944. }
  945. // this can be called even before constuctor finishes.
  946. if (FLocalEditors != NULL)
  947. {
  948. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  949. {
  950. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  951. }
  952. }
  953. if (ComponentVisible[fcCustomCommandsBand])
  954. {
  955. UpdateCustomCommandsToolbar();
  956. }
  957. // show only when keeping queue items forever,
  958. // otherwise, its enough to have in in the context menu
  959. QueueDeleteAllDoneQueueToolbarItem->Visible =
  960. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  961. if (FFileColorsCurrent != WinConfiguration->FileColors)
  962. {
  963. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  964. FFileColorsCurrent = WinConfiguration->FileColors;
  965. FileColorsChanged();
  966. }
  967. }
  968. //---------------------------------------------------------------------------
  969. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  970. {
  971. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  972. DirView->Invalidate();
  973. }
  974. //---------------------------------------------------------------------------
  975. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  976. {
  977. DoFileColorsChanged(RemoteDirView);
  978. }
  979. //---------------------------------------------------------------------------
  980. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  981. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  982. void * /*Arg*/)
  983. {
  984. if (Token != NULL)
  985. {
  986. TForm * Editor = dynamic_cast<TForm*>(Token);
  987. DebugAssert(Editor != NULL);
  988. ReconfigureEditorForm(Editor);
  989. }
  990. }
  991. //---------------------------------------------------------------------------
  992. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  993. {
  994. WinConfiguration->DDTransferConfirmation = asOn;
  995. }
  996. //---------------------------------------------------------------------------
  997. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  998. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  999. {
  1000. // noop
  1001. }
  1002. //---------------------------------------------------------------------------
  1003. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1004. TTransferDirection Direction, TTransferType Type, bool Temp,
  1005. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1006. bool Confirm, bool DragDrop, int Options)
  1007. {
  1008. bool Result = true;
  1009. DebugAssert(Terminal && Terminal->Active);
  1010. // these parameters are known in advance
  1011. int Params =
  1012. FLAGMASK(Type == ttMove, cpDelete);
  1013. bool ToTemp = (Temp && (Direction == tdToLocal));
  1014. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1015. {
  1016. bool DisableNewerOnly =
  1017. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1018. ToTemp;
  1019. Options |=
  1020. FLAGMASK(ToTemp, coTemp) |
  1021. FLAGMASK(!Terminal->IsCapable[fsBackgroundTransfers], coDisableQueue) |
  1022. coDoNotShowAgain;
  1023. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1024. int CopyParamAttrs = (Direction == tdToRemote ?
  1025. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1026. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1027. int OutputOptions =
  1028. FLAGMASK(DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto),
  1029. cooDoNotShowAgain);
  1030. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1031. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1032. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1033. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1034. if (Result)
  1035. {
  1036. if (FLAGSET(OutputOptions, cooDoNotShowAgain))
  1037. {
  1038. if (DragDrop)
  1039. {
  1040. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1041. {
  1042. PopupTrayBalloon(NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation,
  1043. NULL, 0, EnableDDTransferConfirmation, NULL);
  1044. }
  1045. WinConfiguration->DDTransferConfirmation = asOff;
  1046. }
  1047. else
  1048. {
  1049. WinConfiguration->ConfirmTransferring = false;
  1050. }
  1051. }
  1052. else
  1053. {
  1054. // User explicitly unchecked "do not show again",
  1055. // so show him the dialog the next time
  1056. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1057. {
  1058. WinConfiguration->DDTransferConfirmation = asOn;
  1059. }
  1060. }
  1061. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1062. }
  1063. }
  1064. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fsBackgroundTransfers])
  1065. {
  1066. Configuration->Usage->Inc(L"TransfersOnBackground");
  1067. // these parameter are known only after transfer dialog
  1068. Params |=
  1069. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1070. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1071. Result = false;
  1072. ClearTransferSourceSelection(Direction);
  1073. }
  1074. return Result;
  1075. }
  1076. //---------------------------------------------------------------------------
  1077. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1078. {
  1079. if (FOnFileOperationFinished != NULL)
  1080. {
  1081. FOnFileOperationFinished(UnicodeString(), true);
  1082. }
  1083. else
  1084. {
  1085. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1086. if (HasDirView[Side])
  1087. {
  1088. DirView(Side)->SelectAll(smNone);
  1089. }
  1090. }
  1091. }
  1092. //---------------------------------------------------------------------------
  1093. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1094. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1095. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1096. int Params)
  1097. {
  1098. DebugAssert(Queue != NULL);
  1099. bool SingleFile = false;
  1100. if (FileList->Count == 1)
  1101. {
  1102. if (Direction == tdToRemote)
  1103. {
  1104. UnicodeString FileName = FileList->Strings[0];
  1105. SingleFile = FileExists(ApiPath(FileName));
  1106. }
  1107. else
  1108. {
  1109. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1110. SingleFile = !File->IsDirectory;
  1111. }
  1112. }
  1113. TQueueItem * QueueItem;
  1114. if (Direction == tdToRemote)
  1115. {
  1116. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1117. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1118. }
  1119. else
  1120. {
  1121. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1122. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1123. }
  1124. AddQueueItem(Queue, QueueItem, Terminal);
  1125. }
  1126. //---------------------------------------------------------------------------
  1127. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1128. {
  1129. if (Queue->IsEmpty)
  1130. {
  1131. Terminal->QueueOperationStart = Now();
  1132. }
  1133. Queue->AddItem(QueueItem);
  1134. }
  1135. //---------------------------------------------------------------------------
  1136. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1137. {
  1138. }
  1139. //---------------------------------------------------------------------------
  1140. void __fastcall TCustomScpExplorerForm::InitControls()
  1141. {
  1142. DebugAssert(FDocks != NULL);
  1143. for (int Index = 0; Index < ComponentCount; Index++)
  1144. {
  1145. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1146. if ((Dock != NULL) && (Dock->Tag == 0))
  1147. {
  1148. FDocks->Add(Dock);
  1149. }
  1150. }
  1151. CollectItemsWithTextDisplayMode(this);
  1152. }
  1153. //---------------------------------------------------------------------------
  1154. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1155. {
  1156. ConfigurationChanged();
  1157. QueuePanel->Height =
  1158. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1159. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1160. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1161. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1162. UpdateCustomCommandsToolbar();
  1163. }
  1164. //---------------------------------------------------------------------------
  1165. void __fastcall TCustomScpExplorerForm::StoreParams()
  1166. {
  1167. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1168. WinConfiguration->QueueView.HeightPixelsPerInch = GetControlPixelsPerInch(this);
  1169. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1170. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1171. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1172. }
  1173. //---------------------------------------------------------------------------
  1174. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1175. {
  1176. if (!FFormRestored)
  1177. {
  1178. FFormRestored = true;
  1179. RestoreFormParams();
  1180. }
  1181. TForm::CreateParams(Params);
  1182. }
  1183. //---------------------------------------------------------------------------
  1184. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1185. {
  1186. DebugAssert(FDocks != NULL);
  1187. for (int Index = 0; Index < FDocks->Count; Index++)
  1188. {
  1189. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1190. }
  1191. }
  1192. //---------------------------------------------------------------------------
  1193. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1194. {
  1195. SetDockAllowDrag(true);
  1196. while (!ButtonsStr.IsEmpty())
  1197. {
  1198. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1199. UnicodeString Name = CutToChar(S, L'=', true);
  1200. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1201. {
  1202. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1203. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1204. {
  1205. while (!S.IsEmpty())
  1206. {
  1207. UnicodeString S2 = CutToChar(S, L',', true);
  1208. UnicodeString Name = CutToChar(S2, L':', true);
  1209. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1210. {
  1211. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1212. if (SameText(GetToolbarItemName(Item), Name))
  1213. {
  1214. // forward compatibility
  1215. UnicodeString S3 = CutToChar(S2, L':', true);
  1216. bool Visible = StrToIntDef(S3, 0);
  1217. Item->Visible = Visible;
  1218. }
  1219. }
  1220. }
  1221. }
  1222. }
  1223. }
  1224. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1225. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1226. }
  1227. //---------------------------------------------------------------------------
  1228. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1229. {
  1230. UnicodeString Result;
  1231. SetDockAllowDrag(true);
  1232. Result = ::GetToolbarsLayoutStr(this);
  1233. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1234. return Result;
  1235. }
  1236. //---------------------------------------------------------------------------
  1237. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1238. {
  1239. UnicodeString Result;
  1240. if (Item->Action != NULL)
  1241. {
  1242. Result = Item->Action->Name;
  1243. Result = RemoveSuffix(Result, L"Action");
  1244. }
  1245. else
  1246. {
  1247. Result = Item->Name;
  1248. Result = RemoveSuffix(Result, L"SubmenuItem");
  1249. Result = RemoveSuffix(Result, L"Item");
  1250. }
  1251. return Result;
  1252. }
  1253. //---------------------------------------------------------------------------
  1254. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1255. {
  1256. UnicodeString Result;
  1257. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1258. {
  1259. UnicodeString ToolbarStr;
  1260. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1261. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1262. {
  1263. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1264. {
  1265. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1266. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1267. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1268. // and save the changes here only.
  1269. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1270. {
  1271. UnicodeString Name = GetToolbarItemName(Item);
  1272. DebugAssert(Name.Pos(L"TBX") == 0);
  1273. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1274. }
  1275. }
  1276. }
  1277. if (!ToolbarStr.IsEmpty())
  1278. {
  1279. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1280. }
  1281. }
  1282. return Result;
  1283. }
  1284. //---------------------------------------------------------------------------
  1285. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1286. {
  1287. DebugAssert(FProgressForm == NULL);
  1288. bool AllowSkip = (Terminal != NULL) ? Terminal->IsCapable[fsSkipTransfer] : false;
  1289. FProgressForm = new TProgressForm(Application, (FTransferResumeList != NULL), AllowSkip, SynchronizeProgress);
  1290. FProgressForm->DeleteLocalToRecycleBin =
  1291. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1292. FProgressForm->DeleteRemoteToRecycleBin =
  1293. (Terminal != NULL) &&
  1294. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1295. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1296. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1297. LockWindow();
  1298. if (FTaskbarList != NULL)
  1299. {
  1300. // Actually, do not know what hides the progress once the operation finishes
  1301. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1302. SetTaskbarListProgressState(TBPF_NORMAL);
  1303. }
  1304. }
  1305. //---------------------------------------------------------------------------
  1306. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1307. {
  1308. UnlockWindow();
  1309. SAFE_DESTROY(FProgressForm);
  1310. SetQueueProgress();
  1311. }
  1312. //---------------------------------------------------------------------------
  1313. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1314. TFileOperationProgressType & ProgressData)
  1315. {
  1316. // Download to temporary local directory
  1317. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1318. {
  1319. bool Continue = true;
  1320. FPendingTempSpaceWarn = false;
  1321. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1322. if (!Continue)
  1323. {
  1324. Abort();
  1325. }
  1326. }
  1327. // operation is being executed and we still didn't show up progress form
  1328. if (ProgressData.InProgress && !FProgressForm)
  1329. {
  1330. CreateProgressForm(NULL);
  1331. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1332. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1333. }
  1334. // operation is finished (or terminated), so we hide progress form
  1335. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1336. {
  1337. DestroyProgressForm();
  1338. if (ProgressData.Operation == foCalculateSize)
  1339. {
  1340. // When calculating size before transfer, the abort caused by
  1341. // cancel flag set due to "MoveToQueue" below,
  1342. // is not propagated back to us in ExecuteFileOperation,
  1343. // as it is silently swallowed.
  1344. // So we explicitly (re)throw it here.
  1345. if (FMoveToQueue)
  1346. {
  1347. Abort();
  1348. }
  1349. }
  1350. else
  1351. {
  1352. if ((ProgressData.Cancel == csContinue) &&
  1353. !FAutoOperation)
  1354. {
  1355. OperationComplete(ProgressData.StartTime);
  1356. }
  1357. }
  1358. }
  1359. if (FProgressForm &&
  1360. // Can happen only during synchronization, when ending one of the operations
  1361. ProgressData.InProgress)
  1362. {
  1363. FProgressForm->SetProgressData(ProgressData);
  1364. if (FTaskbarList != NULL)
  1365. {
  1366. DebugAssert(ProgressData.InProgress);
  1367. SetTaskbarListProgressValue(&ProgressData);
  1368. }
  1369. if (FProgressForm->Cancel > csContinue)
  1370. {
  1371. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1372. if (FProgressForm->Cancel == csCancelFile)
  1373. {
  1374. FProgressForm->ClearCancel();
  1375. }
  1376. // cancel cancels even the move
  1377. FMoveToQueue = false;
  1378. }
  1379. else if (FProgressForm->MoveToQueue)
  1380. {
  1381. FMoveToQueue = true;
  1382. ProgressData.SetCancelAtLeast(csCancel);
  1383. }
  1384. if ((FTransferResumeList != NULL) &&
  1385. ProgressData.InProgress &&
  1386. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1387. !ProgressData.FullFileName.IsEmpty())
  1388. {
  1389. if ((FTransferResumeList->Count == 0) ||
  1390. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1391. {
  1392. // note that we do not recognize directories from files here
  1393. FTransferResumeList->Add(ProgressData.FullFileName);
  1394. }
  1395. }
  1396. }
  1397. }
  1398. //---------------------------------------------------------------------------
  1399. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1400. const TDateTime & StartTime)
  1401. {
  1402. if (GUIConfiguration->BeepOnFinish &&
  1403. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1404. {
  1405. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1406. DWORD Sound;
  1407. if (!ExtractFileExt(BeepSound).IsEmpty())
  1408. {
  1409. Sound = SND_FILENAME;
  1410. }
  1411. else
  1412. {
  1413. Sound = SND_ALIAS;
  1414. }
  1415. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1416. }
  1417. }
  1418. //---------------------------------------------------------------------------
  1419. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1420. TFileOperationProgressType & ProgressData)
  1421. {
  1422. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1423. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1424. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1425. FileOperationProgress(ProgressData);
  1426. }
  1427. //---------------------------------------------------------------------------
  1428. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1429. {
  1430. UnicodeString Result;
  1431. if (FProgressForm != NULL)
  1432. {
  1433. Result = FProgressForm->ProgressStr();
  1434. }
  1435. return Result;
  1436. }
  1437. //---------------------------------------------------------------------------
  1438. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1439. bool DragDrop)
  1440. {
  1441. return (!DragDrop && (DropSourceControl == NULL)) ||
  1442. (DropSourceControl == DirView(osOther));
  1443. }
  1444. //---------------------------------------------------------------------------
  1445. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1446. TFileOperation Operation, TOperationSide Side,
  1447. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1448. TOnceDoneOperation & OnceDoneOperation)
  1449. {
  1450. if (!FAutoOperation)
  1451. {
  1452. // no selection on "/upload", form servers only as event handler
  1453. // (it is not displayed)
  1454. if (PanelOperation(Side, FDragDropOperation) &&
  1455. Visible && (Operation != foCalculateSize) &&
  1456. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1457. {
  1458. if (FOnFileOperationFinished != NULL)
  1459. {
  1460. FOnFileOperationFinished(FileName, Success);
  1461. }
  1462. else
  1463. {
  1464. TCustomDirView * DView = DirView(Side);
  1465. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1466. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1467. // this can happen when local drive is unplugged in the middle of the operation
  1468. if (Item != NULL)
  1469. {
  1470. if (Success) Item->Selected = false;
  1471. DView->MakeProgressVisible(Item);
  1472. }
  1473. }
  1474. }
  1475. if ((Operation == foCopy) || (Operation == foMove))
  1476. {
  1477. DebugAssert(!IsLocalBrowserMode()); // TODO
  1478. if (Side == osLocal)
  1479. {
  1480. Configuration->Usage->Inc(L"UploadedFiles");
  1481. }
  1482. else
  1483. {
  1484. Configuration->Usage->Inc(L"DownloadedFiles");
  1485. }
  1486. }
  1487. }
  1488. if (Success && (FSynchronizeController != NULL))
  1489. {
  1490. DebugAssert(!IsSideLocalBrowser(osRemote));
  1491. if (Operation == foCopy)
  1492. {
  1493. DebugAssert(Side == osLocal);
  1494. FSynchronizeController->LogOperation(soUpload, FileName);
  1495. }
  1496. else if (Operation == foDelete)
  1497. {
  1498. DebugAssert(Side == osRemote);
  1499. FSynchronizeController->LogOperation(soDelete, FileName);
  1500. }
  1501. }
  1502. if (FProgressForm)
  1503. {
  1504. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1505. }
  1506. }
  1507. //---------------------------------------------------------------------------
  1508. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1509. TFileOperation Operation, TOperationSide Side,
  1510. bool Temp, const UnicodeString & FileName, Boolean Success,
  1511. TOnceDoneOperation & OnceDoneOperation)
  1512. {
  1513. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1514. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1515. OnceDoneOperation);
  1516. }
  1517. //---------------------------------------------------------------------------
  1518. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1519. {
  1520. return false;
  1521. }
  1522. //---------------------------------------------------------------------------
  1523. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1524. {
  1525. return false;
  1526. }
  1527. //---------------------------------------------------------------------------
  1528. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1529. {
  1530. return NULL;
  1531. }
  1532. //---------------------------------------------------------------------------
  1533. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1534. {
  1535. DebugAssert(GetSide(Side) == osRemote);
  1536. DebugUsedParam(Side);
  1537. return RemoteDirView;
  1538. }
  1539. //---------------------------------------------------------------------------
  1540. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1541. {
  1542. DebugAssert(GetSide(Side) == osRemote);
  1543. DebugUsedParam(Side);
  1544. return RemoteDriveView;
  1545. }
  1546. //---------------------------------------------------------------------------
  1547. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1548. {
  1549. DebugAssert(GetSide(Side) == osRemote);
  1550. DebugUsedParam(Side);
  1551. return (Terminal != NULL) && Terminal->Active;
  1552. }
  1553. //---------------------------------------------------------------------------
  1554. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1555. TOperationSide Side, int FilesOnly)
  1556. {
  1557. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1558. }
  1559. //---------------------------------------------------------------------------
  1560. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1561. TOperationSide Side, int FilesOnly)
  1562. {
  1563. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1564. }
  1565. //---------------------------------------------------------------------------
  1566. struct THistoryItemData
  1567. {
  1568. short int Side;
  1569. short int Index;
  1570. };
  1571. //---------------------------------------------------------------------------
  1572. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1573. {
  1574. DirView(Side)->HistoryGo(Index);
  1575. }
  1576. //---------------------------------------------------------------------------
  1577. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1578. {
  1579. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1580. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1581. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1582. }
  1583. //---------------------------------------------------------------------------
  1584. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1585. bool Back)
  1586. {
  1587. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1588. {
  1589. TCustomDirView * DView = DirView(Side);
  1590. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1591. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1592. if (ICount > 10)
  1593. {
  1594. ICount = 10;
  1595. }
  1596. Menu->Items->Clear();
  1597. THistoryItemData Data;
  1598. Data.Side = Side;
  1599. for (short int i = 1; i <= ICount; i++)
  1600. {
  1601. TTBCustomItem * Item = new TTBXItem(Menu);
  1602. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1603. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1604. Item->Hint = DView->HistoryPath[Data.Index];
  1605. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1606. Item->Tag = *reinterpret_cast<int*>(&Data);
  1607. Item->OnClick = HistoryItemClick;
  1608. Menu->Items->Add(Item);
  1609. }
  1610. }
  1611. }
  1612. //---------------------------------------------------------------------------
  1613. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1614. TOperationSide Side, bool Back)
  1615. {
  1616. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1617. {
  1618. // workaround
  1619. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1620. // operator new allocates memory in Pascal code, but calls inline
  1621. // contructor in C++, leading in problems, the function does
  1622. // both in Pascal code
  1623. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1624. UpdateHistoryMenu(Side, Back);
  1625. }
  1626. return FHistoryMenu[Side == osLocal][Back];
  1627. }
  1628. //---------------------------------------------------------------------------
  1629. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1630. TCustomDirView *Sender)
  1631. {
  1632. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1633. UpdateHistoryMenu(Side, true);
  1634. UpdateHistoryMenu(Side, false);
  1635. }
  1636. //---------------------------------------------------------------------------
  1637. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1638. {
  1639. // remote custom commands can be executed only if the server supports shell commands
  1640. // or have secondary shell
  1641. return (FTerminal != NULL) && FTerminal->Active && (FTerminal->IsCapable[fcSecondaryShell] || FTerminal->IsCapable[fcShellAnyCommand]);
  1642. }
  1643. //---------------------------------------------------------------------------
  1644. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1645. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1646. {
  1647. int Result;
  1648. TFileCustomCommand RemoteCustomCommand;
  1649. TLocalCustomCommand LocalCustomCommand;
  1650. TFileCustomCommand * NonInteractiveCustomCommand =
  1651. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1652. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1653. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1654. if (FLAGCLEAR(Command.Params, ccLocal))
  1655. {
  1656. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1657. // custom command that does not operate with files can be executed anytime ...
  1658. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1659. {
  1660. // ... but do not show such command in remote file menu
  1661. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1662. {
  1663. Result = AllowedState;
  1664. }
  1665. else
  1666. {
  1667. Result = -1;
  1668. }
  1669. }
  1670. else
  1671. {
  1672. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1673. {
  1674. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1675. }
  1676. else
  1677. {
  1678. Result = -1;
  1679. }
  1680. }
  1681. }
  1682. else
  1683. {
  1684. // custom command that does not operate with files can be executed anytime
  1685. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1686. {
  1687. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1688. }
  1689. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1690. {
  1691. if ((ListType == ccltAll) || (ListType == ccltFile))
  1692. {
  1693. // special case is "diff"-style command that can be executed over any side,
  1694. // if we have both sides
  1695. Result =
  1696. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1697. // directly (instead of EnableSelectedOperation) to pass
  1698. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1699. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1700. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1701. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1702. }
  1703. else if (ListType == ccltBoth)
  1704. {
  1705. DebugAssert(!IsLocalBrowserMode());
  1706. Result = 1;
  1707. }
  1708. else
  1709. {
  1710. Result = -1;
  1711. }
  1712. }
  1713. else
  1714. {
  1715. if ((ListType == ccltAll) ||
  1716. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1717. {
  1718. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1719. }
  1720. else
  1721. {
  1722. Result = -1;
  1723. }
  1724. }
  1725. }
  1726. return Result;
  1727. }
  1728. //---------------------------------------------------------------------------
  1729. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1730. TStrings * FileList, const TCustomCommandType & ACommand,
  1731. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1732. {
  1733. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1734. {
  1735. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1736. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1737. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1738. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1739. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1740. bool Capture =
  1741. FLAGSET(ACommand.Params, ccShowResults) ||
  1742. FLAGSET(ACommand.Params, ccCopyResults) ||
  1743. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1744. TCaptureOutputEvent OutputEvent = NULL;
  1745. DebugAssert(FCapturedLog == NULL);
  1746. if (Capture)
  1747. {
  1748. FCapturedLog = new TStringList();
  1749. OutputEvent = TerminalCaptureLog;
  1750. }
  1751. try
  1752. {
  1753. if (!RemoteCustomCommand.IsFileCommand(Command))
  1754. {
  1755. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1756. OutputEvent);
  1757. }
  1758. else
  1759. {
  1760. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1761. }
  1762. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1763. {
  1764. if (FLAGSET(ACommand.Params, ccCopyResults))
  1765. {
  1766. CopyToClipboard(FCapturedLog);
  1767. }
  1768. if (FLAGSET(ACommand.Params, ccShowResults))
  1769. {
  1770. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1771. }
  1772. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1773. {
  1774. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1775. }
  1776. }
  1777. }
  1778. __finally
  1779. {
  1780. SAFE_DESTROY(FCapturedLog);
  1781. }
  1782. }
  1783. }
  1784. //---------------------------------------------------------------------------
  1785. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1786. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1787. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1788. {
  1789. DebugAssert(!IsLocalBrowserMode());
  1790. TStrings * LocalFileList = NULL;
  1791. TStrings * RemoteFileList = NULL;
  1792. TStrings * RemoteFileListFull = NULL;
  1793. try
  1794. {
  1795. if (LocalFileCommand)
  1796. {
  1797. if (ALocalFileList == NULL)
  1798. {
  1799. DebugAssert(HasDirView[osLocal]);
  1800. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1801. // directly (instead of EnableSelectedOperation) to pass
  1802. // false to FocusedFileOnlyWhenFocused
  1803. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1804. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1805. }
  1806. else
  1807. {
  1808. LocalFileList = ALocalFileList;
  1809. }
  1810. if (FileListCommand)
  1811. {
  1812. if (LocalFileList->Count != 1)
  1813. {
  1814. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1815. }
  1816. }
  1817. else
  1818. {
  1819. if ((LocalFileList->Count != 1) &&
  1820. (FileList->Count != 1) &&
  1821. (LocalFileList->Count != FileList->Count))
  1822. {
  1823. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1824. }
  1825. }
  1826. }
  1827. UnicodeString RootTempDir;
  1828. UnicodeString TempDir;
  1829. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1830. if (!RemoteFiles)
  1831. {
  1832. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1833. }
  1834. try
  1835. {
  1836. TDateTimes RemoteFileTimes;
  1837. if (RemoteFiles)
  1838. {
  1839. RemoteFileList = FileList;
  1840. }
  1841. else
  1842. {
  1843. RemoteFileList = new TStringList();
  1844. TMakeLocalFileListParams MakeFileListParam;
  1845. MakeFileListParam.FileList = RemoteFileList;
  1846. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1847. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1848. MakeFileListParam.Recursive =
  1849. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1850. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1851. if (!MakeFileListParam.Recursive)
  1852. {
  1853. RemoteFileTimes.clear();
  1854. RemoteFileListFull = new TStringList();
  1855. MakeFileListParam.FileList = RemoteFileListFull;
  1856. MakeFileListParam.Recursive = true;
  1857. MakeFileListParam.IncludeDirs = false;
  1858. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1859. }
  1860. else
  1861. {
  1862. RemoteFileListFull = RemoteFileList;
  1863. }
  1864. }
  1865. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1866. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1867. if (!NonBlocking)
  1868. {
  1869. int Count;
  1870. if (FileListCommand)
  1871. {
  1872. Count = 1;
  1873. }
  1874. else if (LocalFileCommand)
  1875. {
  1876. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  1877. }
  1878. else
  1879. {
  1880. Count = RemoteFileList->Count;
  1881. }
  1882. Progress.Start(foCustomCommand, osRemote, Count);
  1883. DebugAssert(FProgressForm != NULL);
  1884. FProgressForm->ReadOnly = true;
  1885. }
  1886. try
  1887. {
  1888. if (FileListCommand)
  1889. {
  1890. UnicodeString LocalFile;
  1891. // MakeFileList does not delimit filenames
  1892. UnicodeString FileList = MakeFileList(RemoteFileList);
  1893. if (LocalFileCommand)
  1894. {
  1895. DebugAssert(LocalFileList->Count == 1);
  1896. LocalFile = LocalFileList->Strings[0];
  1897. }
  1898. TLocalCustomCommand CustomCommand(Data,
  1899. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1900. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1901. if (NonBlocking)
  1902. {
  1903. DebugAssert(!POutput);
  1904. ExecuteShellChecked(ShellCommand);
  1905. }
  1906. else
  1907. {
  1908. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1909. }
  1910. }
  1911. else if (LocalFileCommand)
  1912. {
  1913. if (LocalFileList->Count == 1)
  1914. {
  1915. UnicodeString LocalFile = LocalFileList->Strings[0];
  1916. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1917. {
  1918. UnicodeString FileName = RemoteFileList->Strings[Index];
  1919. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1920. Progress.SetFile(FileNameForProgress);
  1921. TLocalCustomCommand CustomCommand(Data,
  1922. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  1923. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1924. TOnceDoneOperation OnceDoneOperation;
  1925. // Do not unselect anything, as it won't work with two panels anyway
  1926. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1927. }
  1928. }
  1929. else if (RemoteFileList->Count == 1)
  1930. {
  1931. UnicodeString FileName = RemoteFileList->Strings[0];
  1932. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1933. {
  1934. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1935. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1936. Progress.SetFile(LocalFileNameOnly);
  1937. TLocalCustomCommand CustomCommand(
  1938. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1939. FileName, LocalFileName, L"");
  1940. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1941. TOnceDoneOperation OnceDoneOperation;
  1942. // Do not unselect anything, as it won't work with two panels anyway
  1943. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1944. }
  1945. }
  1946. else
  1947. {
  1948. if (LocalFileList->Count != RemoteFileList->Count)
  1949. {
  1950. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  1951. }
  1952. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1953. {
  1954. UnicodeString FileName = RemoteFileList->Strings[Index];
  1955. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1956. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1957. Progress.SetFile(LocalFileNameOnly); // sic
  1958. TLocalCustomCommand CustomCommand(
  1959. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1960. FileName, LocalFileName, L"");
  1961. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1962. TOnceDoneOperation OnceDoneOperation;
  1963. // Do not unselect anything, as it won't work with two panels anyway
  1964. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1965. }
  1966. }
  1967. }
  1968. else
  1969. {
  1970. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1971. {
  1972. UnicodeString FileName = RemoteFileList->Strings[Index];
  1973. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1974. Progress.SetFile(FileNameForProgress);
  1975. TLocalCustomCommand CustomCommand(Data,
  1976. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1977. FileName, L"", L"");
  1978. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1979. TOnceDoneOperation OnceDoneOperation;
  1980. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  1981. }
  1982. }
  1983. }
  1984. __finally
  1985. {
  1986. if (!NonBlocking)
  1987. {
  1988. Progress.Stop();
  1989. }
  1990. }
  1991. DebugAssert(!FAutoOperation);
  1992. if (!RemoteFiles)
  1993. {
  1994. TempDir = IncludeTrailingBackslash(TempDir);
  1995. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  1996. {
  1997. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  1998. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  1999. // Skip directories, process only nested files.
  2000. // The check is redundant as FileAge fails for directories anyway.
  2001. !DirectoryExists(FileName))
  2002. {
  2003. UnicodeString RemoteDir =
  2004. UnixExtractFileDir(
  2005. UnixIncludeTrailingBackslash(FTerminal->CurrentDirectory) +
  2006. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2007. TDateTime NewTime;
  2008. if (FileAge(FileName, NewTime) &&
  2009. (NewTime != RemoteFileTimes[Index]))
  2010. {
  2011. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2012. TemporaryFileCopyParam(CopyParam);
  2013. CopyParam.FileMask = L"";
  2014. FAutoOperation = true;
  2015. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2016. TemporaryFilesList->Add(FileName);
  2017. FTerminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2018. }
  2019. }
  2020. }
  2021. }
  2022. }
  2023. __finally
  2024. {
  2025. FAutoOperation = false;
  2026. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2027. {
  2028. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2029. }
  2030. }
  2031. }
  2032. __finally
  2033. {
  2034. if (RemoteFileList != FileList)
  2035. {
  2036. delete RemoteFileList;
  2037. }
  2038. if (LocalFileList != ALocalFileList)
  2039. {
  2040. delete LocalFileList;
  2041. }
  2042. if (RemoteFileListFull != RemoteFileList)
  2043. {
  2044. delete RemoteFileListFull;
  2045. }
  2046. }
  2047. }
  2048. //---------------------------------------------------------------------------
  2049. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2050. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2051. bool FileListCommand, UnicodeString * POutput)
  2052. {
  2053. std::unique_ptr<TStrings> SelectedFileList(DirView(osLocal)->CreateFileList(false, true, NULL));
  2054. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2055. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2056. {
  2057. UnicodeString FileName = SelectedFileList->Strings[Index];
  2058. if (DirectoryExists(FileName))
  2059. {
  2060. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2061. {
  2062. LocalFileList->Add(FileName);
  2063. }
  2064. if (FLAGSET(ACommand.Params, ccRecursive))
  2065. {
  2066. TMakeLocalFileListParams MakeFileListParam;
  2067. MakeFileListParam.FileList = LocalFileList.get();
  2068. MakeFileListParam.FileTimes = NULL;
  2069. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2070. MakeFileListParam.Recursive = true;
  2071. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2072. }
  2073. }
  2074. else
  2075. {
  2076. LocalFileList->Add(FileName);
  2077. }
  2078. }
  2079. UnicodeString RemotePath;
  2080. if (Terminal != NULL)
  2081. {
  2082. RemotePath = Terminal->CurrentDirectory;
  2083. }
  2084. if (FileListCommand)
  2085. {
  2086. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2087. TLocalCustomCommand CustomCommand(
  2088. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2089. L"", L"", FileList);
  2090. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2091. }
  2092. else
  2093. {
  2094. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2095. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2096. DebugAssert(FProgressForm != NULL);
  2097. FProgressForm->ReadOnly = true;
  2098. try
  2099. {
  2100. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2101. {
  2102. UnicodeString FileName = LocalFileList->Strings[Index];
  2103. Progress.SetFile(FileName);
  2104. TLocalCustomCommand CustomCommand(
  2105. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2106. FileName, L"", L"");
  2107. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2108. TOnceDoneOperation OnceDoneOperation;
  2109. Progress.Finish(FileName, true, OnceDoneOperation);
  2110. }
  2111. }
  2112. __finally
  2113. {
  2114. Progress.Stop();
  2115. }
  2116. }
  2117. }
  2118. //---------------------------------------------------------------------------
  2119. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2120. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2121. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2122. {
  2123. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2124. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2125. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2126. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2127. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2128. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2129. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2130. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2131. std::unique_ptr<UnicodeString> POutput;
  2132. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2133. FLAGSET(ACommand.Params, ccCopyResults))
  2134. {
  2135. POutput.reset(new UnicodeString());
  2136. }
  2137. if (!LocalCustomCommand.IsFileCommand(Command))
  2138. {
  2139. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2140. }
  2141. // remote files?
  2142. else if ((FCurrentSide == osRemote) || LocalFileCommand)
  2143. {
  2144. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2145. }
  2146. // local files
  2147. else
  2148. {
  2149. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2150. }
  2151. if (POutput.get() != NULL)
  2152. {
  2153. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2154. int P = POutput->Pos(sLineBreak);
  2155. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2156. {
  2157. POutput->SetLength(P - 1);
  2158. }
  2159. // Copy even empty output to clipboard
  2160. if (FLAGSET(ACommand.Params, ccCopyResults))
  2161. {
  2162. CopyToClipboard(*POutput);
  2163. }
  2164. // But do not show an empty message box.
  2165. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2166. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2167. !POutput->IsEmpty())
  2168. {
  2169. TClipboardHandler ClipboardHandler;
  2170. ClipboardHandler.Text = *POutput;
  2171. TMessageParams Params;
  2172. TQueryButtonAlias Aliases[1];
  2173. Aliases[0].Button = qaRetry;
  2174. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2175. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2176. Params.Aliases = Aliases;
  2177. Params.AliasesCount = LENOF(Aliases);
  2178. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2179. }
  2180. }
  2181. }
  2182. //---------------------------------------------------------------------------
  2183. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2184. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2185. {
  2186. TCustomCommandData Data;
  2187. UnicodeString Site;
  2188. UnicodeString RemotePath;
  2189. if (Terminal != NULL)
  2190. {
  2191. Data = TCustomCommandData(Terminal);
  2192. Site = Terminal->SessionData->SessionKey;
  2193. RemotePath = Terminal->CurrentDirectory;
  2194. }
  2195. UnicodeString HelpKeyword = ACommand.HomePage;
  2196. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2197. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2198. {
  2199. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2200. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2201. {
  2202. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2203. }
  2204. else
  2205. {
  2206. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2207. }
  2208. if (!DoCustomCommandOptionsDialog(
  2209. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2210. {
  2211. Abort();
  2212. }
  2213. }
  2214. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2215. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2216. {
  2217. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2218. }
  2219. else
  2220. {
  2221. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2222. }
  2223. }
  2224. //---------------------------------------------------------------------------
  2225. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2226. const TCustomCommandType & Command)
  2227. {
  2228. DebugAssert(FCustomCommandLocalFileList != NULL);
  2229. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2230. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2231. TStrings * LocalFileList = new TStringList();
  2232. TStrings * RemoteFileList = new TStringList();
  2233. try
  2234. {
  2235. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2236. {
  2237. LocalFileList->Clear();
  2238. LocalFileList->AddObject(
  2239. FCustomCommandLocalFileList->Strings[Index],
  2240. FCustomCommandLocalFileList->Objects[Index]);
  2241. RemoteFileList->Clear();
  2242. RemoteFileList->AddObject(
  2243. FCustomCommandRemoteFileList->Strings[Index],
  2244. FCustomCommandRemoteFileList->Objects[Index]);
  2245. CustomCommand(RemoteFileList, Command, LocalFileList);
  2246. }
  2247. }
  2248. __finally
  2249. {
  2250. delete LocalFileList;
  2251. delete RemoteFileList;
  2252. }
  2253. }
  2254. //---------------------------------------------------------------------------
  2255. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2256. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2257. {
  2258. delete FCustomCommandLocalFileList;
  2259. delete FCustomCommandRemoteFileList;
  2260. // takeover ownership,
  2261. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2262. FCustomCommandLocalFileList = LocalFileList;
  2263. FCustomCommandRemoteFileList = RemoteFileList;
  2264. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2265. if (Button != NULL)
  2266. {
  2267. FCustomCommandMenu->Items->Clear();
  2268. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2269. MenuPopup(FCustomCommandMenu, Button);
  2270. }
  2271. else
  2272. {
  2273. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2274. }
  2275. }
  2276. //---------------------------------------------------------------------------
  2277. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2278. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2279. {
  2280. DebugAssert(FCapturedLog != NULL);
  2281. if ((OutputType == cotOutput) || (OutputType == cotError))
  2282. {
  2283. FCapturedLog->Add(AddedLine);
  2284. }
  2285. }
  2286. //---------------------------------------------------------------------------
  2287. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2288. TOperationSide Side)
  2289. {
  2290. return
  2291. (Side == osOther) &&
  2292. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2293. }
  2294. //---------------------------------------------------------------------------
  2295. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2296. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2297. {
  2298. TTBItemOptions O;
  2299. O = Item->Options;
  2300. TCustomDirView * DView = DirView(Side);
  2301. if ((DView->ItemFocused != NULL) &&
  2302. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2303. // when resolving links is disabled, default action is to enter the directory,
  2304. // no matter what DoubleClickAction is configured to
  2305. (IsSideLocalBrowser(Side) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2306. // Can only Edit files, but can Open/Copy even directories
  2307. ((DoubleClickAction != dcaEdit) ||
  2308. !DView->ItemIsDirectory(DView->ItemFocused)))
  2309. {
  2310. Item->Options = O << tboDefault;
  2311. }
  2312. else
  2313. {
  2314. Item->Options = O >> tboDefault;
  2315. }
  2316. }
  2317. //---------------------------------------------------------------------------
  2318. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2319. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2320. {
  2321. TCustomDirView * DView = DirView(Side);
  2322. TListItem * Item = DView->ItemFocused;
  2323. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2324. EnableFocusedOperation[Side])
  2325. {
  2326. TPoint ClientPoint;
  2327. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2328. TPoint(0, 0) : MousePos;
  2329. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2330. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2331. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2332. }
  2333. }
  2334. //---------------------------------------------------------------------------
  2335. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2336. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2337. {
  2338. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2339. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2340. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2341. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2342. Handled = true;
  2343. }
  2344. //---------------------------------------------------------------------------
  2345. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2346. {
  2347. }
  2348. //---------------------------------------------------------------------------
  2349. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2350. {
  2351. DebugAssert(FErrorList == NULL);
  2352. if (WinConfiguration->ContinueOnError)
  2353. {
  2354. FErrorList = new TStringList();
  2355. Configuration->Usage->Inc(L"ContinuationsOnError");
  2356. }
  2357. NonVisualDataModule->StartBusy();
  2358. }
  2359. //---------------------------------------------------------------------------
  2360. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2361. {
  2362. NonVisualDataModule->EndBusy();
  2363. if (FErrorList)
  2364. {
  2365. HandleErrorList(FErrorList);
  2366. }
  2367. }
  2368. //---------------------------------------------------------------------------
  2369. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2370. const TCopyParamType & CopyParam)
  2371. {
  2372. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2373. {
  2374. Configuration->Usage->Inc(L"FileMaskUses");
  2375. }
  2376. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2377. {
  2378. Configuration->Usage->Inc(L"OperationMaskUses");
  2379. }
  2380. }
  2381. //---------------------------------------------------------------------------
  2382. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2383. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2384. {
  2385. DebugAssert(!IsLocalBrowserMode());
  2386. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2387. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2388. TTransferOperationParam DefaultParam;
  2389. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2390. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2391. switch (Param.Queue)
  2392. {
  2393. case asOn:
  2394. CopyParam.Queue = true;
  2395. break;
  2396. case asOff:
  2397. CopyParam.Queue = false;
  2398. break;
  2399. case asAuto:
  2400. default:
  2401. // keep default
  2402. break;
  2403. }
  2404. bool Result =
  2405. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2406. if (Result)
  2407. {
  2408. DebugAssert(Terminal);
  2409. bool SelectionRestored = false;
  2410. TCustomDirView * DView = NULL;
  2411. if (HasDirView[Side])
  2412. {
  2413. DView = DirView(Side);
  2414. DView->SaveSelection();
  2415. DView->SaveSelectedNames();
  2416. }
  2417. UpdateCopyParamCounters(CopyParam);
  2418. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2419. DebugAssert(FTransferResumeList == NULL);
  2420. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2421. FMoveToQueue = false;
  2422. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2423. try
  2424. {
  2425. TStrings * PermanentFileList;
  2426. std::unique_ptr<TStrings> PermanentFileListOwner;
  2427. try
  2428. {
  2429. if (Side == osLocal)
  2430. {
  2431. PermanentFileList = FileList;
  2432. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2433. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2434. if (Operation == foMove)
  2435. {
  2436. ReloadLocalDirectory();
  2437. if (DView != NULL)
  2438. {
  2439. DView->RestoreSelection();
  2440. }
  2441. SelectionRestored = true;
  2442. }
  2443. }
  2444. else
  2445. {
  2446. // Clone the file list as it may refer to current directory files,
  2447. // which get destroyed, when the source directory is reloaded after foMove operation.
  2448. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2449. // But for a hotfix, we are not going to do such a big change.
  2450. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2451. PermanentFileList = PermanentFileListOwner.get();
  2452. try
  2453. {
  2454. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2455. }
  2456. __finally
  2457. {
  2458. if (Operation == foMove)
  2459. {
  2460. if (DView != NULL)
  2461. {
  2462. DView->RestoreSelection();
  2463. }
  2464. SelectionRestored = true;
  2465. }
  2466. ReloadLocalDirectory(Param.TargetDirectory);
  2467. }
  2468. }
  2469. }
  2470. catch (EAbort &)
  2471. {
  2472. if (FMoveToQueue)
  2473. {
  2474. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2475. DebugAssert(CopyParam.TransferSkipList == NULL);
  2476. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2477. if (TransferResumeList->Count > 0)
  2478. {
  2479. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2480. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2481. }
  2482. CopyParam.TransferSkipList = TransferResumeList.release();
  2483. // not really needed, just to keep it consistent with TransferResumeList
  2484. FTransferResumeList = NULL;
  2485. FMoveToQueue = false;
  2486. Configuration->Usage->Inc("MovesToBackground");
  2487. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2488. ClearTransferSourceSelection(Direction);
  2489. }
  2490. throw;
  2491. }
  2492. }
  2493. __finally
  2494. {
  2495. if (!SelectionRestored && (DView != NULL))
  2496. {
  2497. DView->DiscardSavedSelection();
  2498. }
  2499. FTransferResumeList = NULL;
  2500. }
  2501. }
  2502. return Result;
  2503. }
  2504. //---------------------------------------------------------------------------
  2505. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2506. TOperationSide Side, TStrings * FileList, void * Param)
  2507. {
  2508. DebugAssert(FileList->Count);
  2509. // We deliberately do not toggle alternative flag (Param), but use OR,
  2510. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2511. // shortcut of CurrentDeleteAlternativeAction
  2512. bool Alternative =
  2513. bool(Param) || UseAlternativeFunction();
  2514. bool Recycle;
  2515. if (IsSideLocalBrowser(Side))
  2516. {
  2517. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2518. }
  2519. else
  2520. {
  2521. Recycle =
  2522. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2523. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2524. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2525. }
  2526. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2527. if (!Result)
  2528. {
  2529. UnicodeString Query;
  2530. if (FileList->Count == 1)
  2531. {
  2532. if (IsSideLocalBrowser(Side))
  2533. {
  2534. Query = ExtractFileName(FileList->Strings[0]);
  2535. }
  2536. else
  2537. {
  2538. Query = UnixExtractFileName(FileList->Strings[0]);
  2539. }
  2540. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2541. }
  2542. else
  2543. {
  2544. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2545. }
  2546. TMessageParams Params(mpNeverAskAgainCheck);
  2547. Params.ImageName = L"Delete file";
  2548. unsigned int Answer =
  2549. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2550. if (Answer == qaNeverAskAgain)
  2551. {
  2552. Result = true;
  2553. if (Recycle)
  2554. {
  2555. WinConfiguration->ConfirmRecycling = false;
  2556. }
  2557. else
  2558. {
  2559. WinConfiguration->ConfirmDeleting = false;
  2560. }
  2561. }
  2562. else
  2563. {
  2564. Result = (Answer == qaOK);
  2565. }
  2566. }
  2567. if (Result)
  2568. {
  2569. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2570. }
  2571. return Result;
  2572. }
  2573. //---------------------------------------------------------------------------
  2574. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2575. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2576. {
  2577. TAutoBatch AutoBatch(this);
  2578. bool Result;
  2579. if ((Operation == foCopy) || (Operation == foMove))
  2580. {
  2581. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2582. }
  2583. else if (Operation == foRename)
  2584. {
  2585. DebugAssert(DirView(Side)->ItemFocused);
  2586. DirView(Side)->ItemFocused->EditCaption();
  2587. Result = true;
  2588. }
  2589. else if (Operation == foDelete)
  2590. {
  2591. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2592. }
  2593. else if (Operation == foSetProperties)
  2594. {
  2595. DirView(osRemote)->SaveSelectedNames();
  2596. Result = SetProperties(Side, FileList);
  2597. }
  2598. else if (Operation == foCustomCommand)
  2599. {
  2600. DebugAssert(Param);
  2601. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2602. DirView(osRemote)->SaveSelectedNames();
  2603. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2604. CustomCommand(FileList, *Command, NULL);
  2605. Result = true;
  2606. }
  2607. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2608. {
  2609. DebugAssert(!IsSideLocalBrowser(Side));
  2610. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2611. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2612. }
  2613. else if (Operation == foLock)
  2614. {
  2615. DebugAssert(!IsSideLocalBrowser(Side));
  2616. LockFiles(FileList, true);
  2617. Result = true;
  2618. }
  2619. else if (Operation == foUnlock)
  2620. {
  2621. DebugAssert(!IsSideLocalBrowser(Side));
  2622. LockFiles(FileList, false);
  2623. Result = true;
  2624. }
  2625. else
  2626. {
  2627. DebugFail();
  2628. }
  2629. return Result;
  2630. }
  2631. //---------------------------------------------------------------------------
  2632. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2633. {
  2634. if (Side == osCurrent)
  2635. {
  2636. Side = FCurrentSide;
  2637. }
  2638. return Side;
  2639. }
  2640. //---------------------------------------------------------------------------
  2641. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2642. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2643. {
  2644. Side = GetSide(Side);
  2645. bool Result;
  2646. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, IsSideLocalBrowser(Side), NULL);
  2647. try
  2648. {
  2649. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2650. }
  2651. __finally
  2652. {
  2653. delete FileList;
  2654. }
  2655. return Result;
  2656. }
  2657. //---------------------------------------------------------------------------
  2658. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2659. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2660. bool NoConfirmation, void * Param)
  2661. {
  2662. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2663. {
  2664. if ((Operation == foCopy) || (Operation == foMove))
  2665. {
  2666. DebugAssert(!IsLocalBrowserMode());
  2667. if (GetSide(Side) == osLocal)
  2668. {
  2669. Configuration->Usage->Inc(L"UploadsCommand");
  2670. }
  2671. else
  2672. {
  2673. Configuration->Usage->Inc(L"DownloadsCommand");
  2674. }
  2675. }
  2676. }
  2677. }
  2678. //---------------------------------------------------------------------------
  2679. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2680. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2681. {
  2682. TTransferOperationParam Param;
  2683. if ((WinConfiguration->Interface != ifCommander) ||
  2684. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2685. {
  2686. Flags &= ~cocShortCutHint;
  2687. }
  2688. TCustomDirView * DView = DirView(Side);
  2689. Param.Options =
  2690. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2691. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2692. if (FLAGSET(Flags, cocQueue))
  2693. {
  2694. Param.Queue = asOn;
  2695. }
  2696. else if (FLAGSET(Flags, cocNonQueue))
  2697. {
  2698. Param.Queue = asOff;
  2699. }
  2700. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2701. }
  2702. //---------------------------------------------------------------------------
  2703. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2704. {
  2705. try
  2706. {
  2707. if (ErrorList->Count)
  2708. {
  2709. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2710. if (MessageDialog(Message, qtError,
  2711. qaOK | qaCancel, HELP_NONE) == qaOK)
  2712. {
  2713. unsigned int Answer;
  2714. int Index = 0;
  2715. do
  2716. {
  2717. DebugAssert(Index >= 0 && Index < ErrorList->Count);
  2718. TQueryButtonAlias Aliases[2];
  2719. Aliases[0].Button = qaYes;
  2720. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2721. Aliases[1].Button = qaNo;
  2722. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2723. TMessageParams Params;
  2724. Params.Aliases = Aliases;
  2725. Params.AliasesCount = LENOF(Aliases);
  2726. Answer = MoreMessageDialog(
  2727. FMTLOAD(ERROR_LIST_NUMBER, (Index+1, ErrorList->Count, ErrorList->Strings[Index])),
  2728. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2729. (Index ? qaYes : 0) | (Index < ErrorList->Count - 1 ? qaNo : 0) |
  2730. qaOK, HELP_NONE, &Params);
  2731. if (Answer == qaNo)
  2732. {
  2733. Index++;
  2734. }
  2735. if (Answer == qaYes)
  2736. {
  2737. Index--;
  2738. }
  2739. }
  2740. while (Answer != qaOK);
  2741. }
  2742. }
  2743. }
  2744. __finally
  2745. {
  2746. TStrings * List = ErrorList;
  2747. ErrorList = NULL;
  2748. for (int i = 0; i < List->Count; i++)
  2749. {
  2750. delete List->Objects[i];
  2751. }
  2752. delete List;
  2753. }
  2754. }
  2755. //---------------------------------------------------------------------------
  2756. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2757. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2758. {
  2759. // needed for checking filemasks, as there's no directory object
  2760. // associated with the file object
  2761. File->FullFileName = FullFileName;
  2762. TFileMasks::TParams MaskParams;
  2763. MaskParams.Size = File->Size;
  2764. MaskParams.Modification = File->Modification;
  2765. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2766. }
  2767. //---------------------------------------------------------------------------
  2768. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2769. {
  2770. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2771. Side = GetSide(Side);
  2772. TCustomDirView * DView = DirView(Side);
  2773. TListItem * FocusedItem = DView->ItemFocused;
  2774. UnicodeString Name;
  2775. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2776. {
  2777. Name = DView->ItemFileName(FocusedItem);
  2778. }
  2779. else
  2780. {
  2781. Name = LoadStr(NEW_FILE);
  2782. }
  2783. UnicodeString Names = Name;
  2784. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2785. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2786. HELP_EDIT_NEW, History.get(), true))
  2787. {
  2788. while (!Names.IsEmpty())
  2789. {
  2790. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2791. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2792. UnicodeString TargetFileName;
  2793. UnicodeString LocalFileName;
  2794. UnicodeString RootTempDir;
  2795. UnicodeString TempDir;
  2796. UnicodeString RemoteDirectory;
  2797. bool ExistingFile = false;
  2798. if (!IsSideLocalBrowser(Side))
  2799. {
  2800. Name = AbsolutePath(FTerminal->CurrentDirectory, Name);
  2801. TRemoteFile * File = NULL;
  2802. if (FTerminal->FileExists(Name, &File) &&
  2803. !File->IsDirectory)
  2804. {
  2805. try
  2806. {
  2807. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2808. ExistingFile = true;
  2809. }
  2810. __finally
  2811. {
  2812. delete File;
  2813. }
  2814. }
  2815. if (!ExistingFile)
  2816. {
  2817. RemoteDirectory = UnixExtractFilePath(Name);
  2818. TemporaryDirectoryForRemoteFiles(
  2819. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2820. TargetFileName = UnixExtractFileName(Name);
  2821. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2822. LocalFileName = TempDir +
  2823. // We probably do not want to trim the VMS version here
  2824. FTerminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2825. }
  2826. }
  2827. else
  2828. {
  2829. if (ExtractFilePath(Name).IsEmpty())
  2830. {
  2831. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2832. }
  2833. else
  2834. {
  2835. LocalFileName = ExpandFileName(Name);
  2836. }
  2837. TargetFileName = ExtractFileName(Name);
  2838. }
  2839. if (!ExistingFile)
  2840. {
  2841. bool NewFile = !FileExists(ApiPath(LocalFileName));
  2842. if (NewFile)
  2843. {
  2844. int File = FileCreate(ApiPath(LocalFileName));
  2845. if (File < 0)
  2846. {
  2847. if (!RootTempDir.IsEmpty())
  2848. {
  2849. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2850. }
  2851. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2852. }
  2853. else
  2854. {
  2855. FileClose(File);
  2856. }
  2857. }
  2858. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2859. const TEditorData * ExternalEditor = NULL;
  2860. TFileMasks::TParams MaskParams; // size not known
  2861. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2862. false, MaskParams);
  2863. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2864. ExternalEditor, RootTempDir, RemoteDirectory, NewFile);
  2865. }
  2866. }
  2867. }
  2868. }
  2869. //---------------------------------------------------------------------------
  2870. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2871. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2872. {
  2873. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2874. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2875. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2876. return
  2877. ((ExecuteFileBy == efInternalEditor)) ||
  2878. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2879. }
  2880. //---------------------------------------------------------------------------
  2881. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2882. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2883. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2884. UnicodeString RemoteDirectory, bool NewFile)
  2885. {
  2886. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2887. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2888. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2889. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2890. Side = GetSide(Side);
  2891. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2892. if (!IsSideLocalBrowser(Side))
  2893. {
  2894. Data->Terminal = Terminal;
  2895. Data->Queue = Queue;
  2896. Data->SessionData = CloneCurrentSessionData();
  2897. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2898. Data->RemoteDirectory = RemoteDirectory;
  2899. Data->SessionName = Terminal->SessionData->SessionName;
  2900. Data->LocalRootDirectory = LocalRootDirectory;
  2901. Data->OriginalFileName = OriginalFileName;
  2902. Data->Command = L""; // will be changed later for external editor
  2903. }
  2904. if (ExecuteFileBy == efInternalEditor)
  2905. {
  2906. if (!IsSideLocalBrowser(Side))
  2907. {
  2908. UnicodeString Caption = UnixIncludeTrailingBackslash(RemoteDirectory) + OriginalFileName +
  2909. L" - " + Terminal->SessionData->SessionName;
  2910. TForm * Editor;
  2911. try
  2912. {
  2913. TNotifyEvent OnFileChanged = NULL;
  2914. TNotifyEvent OnSaveAll = NULL;
  2915. TAnyModifiedEvent OnAnyModified = NULL;
  2916. // Edited files are uploaded in background queue, what we do not support with encrypted files
  2917. if (Terminal->IsCapable[fsBackgroundTransfers])
  2918. {
  2919. OnFileChanged = FEditorManager->FileChanged;
  2920. OnSaveAll = SaveAllInternalEditors;
  2921. OnAnyModified = AnyInternalEditorModified;
  2922. }
  2923. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  2924. FEditorManager->FileReload, FEditorManager->FileClosed,
  2925. OnSaveAll, OnAnyModified,
  2926. Caption, FStandaloneEditing, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  2927. }
  2928. catch(...)
  2929. {
  2930. if (!LocalRootDirectory.IsEmpty())
  2931. {
  2932. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  2933. }
  2934. throw;
  2935. }
  2936. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  2937. }
  2938. else
  2939. {
  2940. DebugAssert(!FStandaloneEditing);
  2941. TForm * Editor =
  2942. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  2943. SaveAllInternalEditors, AnyInternalEditorModified,
  2944. L"", false, SessionColor, -1, NewFile);
  2945. FLocalEditors->Add(Editor);
  2946. }
  2947. }
  2948. else
  2949. {
  2950. HANDLE Process;
  2951. if (ExecuteFileBy == efExternalEditor)
  2952. {
  2953. UnicodeString Program, Params, Dir;
  2954. Data->Command = ExternalEditor->ExternalEditor;
  2955. ReformatFileNameCommand(Data->Command);
  2956. SplitCommand(Data->Command, Program, Params, Dir);
  2957. Params = ExpandFileNameCommand(Params, FileName);
  2958. Program = ExpandEnvironmentVariables(Program);
  2959. if (!ExecuteShell(Program, Params, Process))
  2960. {
  2961. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  2962. }
  2963. }
  2964. else
  2965. {
  2966. DebugAssert(!IsSideLocalBrowser(Side));
  2967. if (!ExecuteShell(FileName, L"", Process))
  2968. {
  2969. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  2970. }
  2971. }
  2972. if (IsSideLocalBrowser(Side) ||
  2973. ((ExecuteFileBy == efShell) &&
  2974. !WinConfiguration->Editor.SDIShellEditor) ||
  2975. ((ExecuteFileBy == efExternalEditor) &&
  2976. !ExternalEditor->SDIExternalEditor))
  2977. {
  2978. // no need for handle
  2979. if (Process != NULL)
  2980. {
  2981. DebugCheck(CloseHandle(Process));
  2982. }
  2983. Process = INVALID_HANDLE_VALUE;
  2984. }
  2985. else
  2986. {
  2987. if (Process == NULL)
  2988. {
  2989. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  2990. }
  2991. }
  2992. if (!IsSideLocalBrowser(Side))
  2993. {
  2994. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  2995. }
  2996. }
  2997. }
  2998. //---------------------------------------------------------------------------
  2999. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3000. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3001. void * /*Arg*/)
  3002. {
  3003. if (Token != NULL)
  3004. {
  3005. EditorFormFileSave(static_cast<TForm *>(Token));
  3006. }
  3007. }
  3008. //---------------------------------------------------------------------------
  3009. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3010. {
  3011. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3012. {
  3013. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3014. }
  3015. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3016. }
  3017. //---------------------------------------------------------------------------
  3018. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3019. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3020. void * Arg)
  3021. {
  3022. if ((Token != NULL) &&
  3023. IsEditorFormModified(static_cast<TForm *>(Token)))
  3024. {
  3025. *static_cast<bool *>(Arg) = true;
  3026. }
  3027. }
  3028. //---------------------------------------------------------------------------
  3029. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3030. TObject * /*Sender*/, bool & Modified)
  3031. {
  3032. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3033. {
  3034. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3035. {
  3036. Modified = true;
  3037. }
  3038. }
  3039. if (!Modified)
  3040. {
  3041. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3042. }
  3043. }
  3044. //---------------------------------------------------------------------------
  3045. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3046. {
  3047. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3048. }
  3049. //---------------------------------------------------------------------------
  3050. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3051. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3052. UnicodeString & Result, UnicodeString & RootDirectory)
  3053. {
  3054. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3055. {
  3056. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3057. Result = RootDirectory;
  3058. }
  3059. else
  3060. {
  3061. RootDirectory = L"";
  3062. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3063. Result = IncludeTrailingBackslash(Result);
  3064. }
  3065. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3066. {
  3067. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3068. }
  3069. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3070. {
  3071. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3072. {
  3073. RemoteDirectory.Delete(1, 1);
  3074. }
  3075. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3076. }
  3077. if (!ForceDirectories(ApiPath(Result)))
  3078. {
  3079. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3080. }
  3081. }
  3082. //---------------------------------------------------------------------------
  3083. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3084. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3085. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  3086. {
  3087. DebugAssert(!IsLocalBrowserMode());
  3088. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3089. if (ForceText)
  3090. {
  3091. CopyParam.TransferMode = tmAscii;
  3092. }
  3093. TemporaryFileCopyParam(CopyParam);
  3094. if (AllFiles)
  3095. {
  3096. CopyParam.IncludeFileMask = TFileMasks();
  3097. }
  3098. if (TempDir.IsEmpty())
  3099. {
  3100. TemporaryDirectoryForRemoteFiles(FTerminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3101. }
  3102. DebugAssert(!FAutoOperation);
  3103. FAutoOperation = AutoOperation;
  3104. Terminal->ExceptionOnFail = true;
  3105. try
  3106. {
  3107. try
  3108. {
  3109. // turn off confirmations, as for MDI editors we may possibly download
  3110. // the same file over
  3111. Terminal->CopyToLocal(
  3112. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3113. if (GetTargetNames)
  3114. {
  3115. for (int i = 0; i < FileList->Count; i++)
  3116. {
  3117. FileList->Strings[i] =
  3118. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3119. }
  3120. }
  3121. }
  3122. catch(...)
  3123. {
  3124. if (!RootTempDir.IsEmpty())
  3125. {
  3126. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3127. }
  3128. throw;
  3129. }
  3130. }
  3131. __finally
  3132. {
  3133. FAutoOperation = false;
  3134. Terminal->ExceptionOnFail = false;
  3135. }
  3136. }
  3137. //---------------------------------------------------------------------------
  3138. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3139. {
  3140. // Do not waste time checking for default editor list the next time,
  3141. // or testing if default editor is not notepad.
  3142. bool TryNextTime = false;
  3143. UnicodeString UsageState;
  3144. if (!WinConfiguration->EditorList->IsDefaultList())
  3145. {
  3146. UsageState = "H";
  3147. }
  3148. else
  3149. {
  3150. UnicodeString Executable;
  3151. UnicodeString ExecutableDescription;
  3152. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3153. {
  3154. UnicodeString Message =
  3155. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3156. unsigned int Answer =
  3157. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3158. if (Answer != qaOK)
  3159. {
  3160. UsageState = "R";
  3161. }
  3162. else
  3163. {
  3164. UsageState = "A";
  3165. TEditorData EditorData;
  3166. EditorData.Editor = edExternal;
  3167. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3168. EditorData.DecideExternalEditorText();
  3169. TEditorList EditorList;
  3170. EditorList = *WinConfiguration->EditorList;
  3171. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3172. WinConfiguration->EditorList = &EditorList;
  3173. }
  3174. }
  3175. }
  3176. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3177. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3178. }
  3179. //---------------------------------------------------------------------------
  3180. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3181. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3182. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3183. {
  3184. if (ExecuteFileBy == efDefaultEditor)
  3185. {
  3186. if (!WinConfiguration->OfferedEditorAutoConfig)
  3187. {
  3188. EditorAutoConfig();
  3189. }
  3190. const TEditorPreferences * Editor =
  3191. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3192. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3193. {
  3194. ExecuteFileBy = efInternalEditor;
  3195. ExternalEditor = NULL;
  3196. }
  3197. else if (Editor->Data->Editor == edOpen)
  3198. {
  3199. ExecuteFileBy = efShell;
  3200. ExternalEditor = NULL;
  3201. }
  3202. else
  3203. {
  3204. ExecuteFileBy = efExternalEditor;
  3205. ExternalEditor = Editor->Data;
  3206. }
  3207. }
  3208. }
  3209. //---------------------------------------------------------------------------
  3210. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3211. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3212. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3213. {
  3214. UnicodeString OriginalFileName;
  3215. UnicodeString LocalRootDirectory;
  3216. UnicodeString RemoteDirectory;
  3217. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3218. IsSideLocalBrowser(Side), MaskParams);
  3219. UnicodeString Counter;
  3220. UnicodeString LocalFileName;
  3221. bool Handled = false;
  3222. if (!IsSideLocalBrowser(Side))
  3223. {
  3224. // We need to trim VMS version here, so that we use name without version
  3225. // when uploading back to create a new version of the file
  3226. OriginalFileName = FTerminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3227. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3228. TObject * Token = NULL;
  3229. UnicodeString LocalDirectory;
  3230. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3231. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3232. LocalDirectory))
  3233. {
  3234. if (Token != NULL)
  3235. {
  3236. TForm * Form = dynamic_cast<TForm *>(Token);
  3237. if (Form->WindowState == wsMinimized)
  3238. {
  3239. ShowWindow(Form->Handle, SW_RESTORE);
  3240. }
  3241. else
  3242. {
  3243. Form->SetFocus();
  3244. }
  3245. Handled = true;
  3246. }
  3247. else
  3248. {
  3249. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3250. }
  3251. }
  3252. if (!Handled)
  3253. {
  3254. TStringList * FileList1 = new TStringList();
  3255. try
  3256. {
  3257. FileList1->AddObject(FullFileName, Object);
  3258. TemporarilyDownloadFiles(FileList1,
  3259. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3260. LocalRootDirectory, LocalDirectory, true, true, true);
  3261. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3262. }
  3263. __finally
  3264. {
  3265. delete FileList1;
  3266. }
  3267. switch (ExecuteFileBy)
  3268. {
  3269. case efShell:
  3270. Counter = "RemoteFilesExecuted";
  3271. break;
  3272. case efInternalEditor:
  3273. Counter = "RemoteFilesOpenedInInternalEditor";
  3274. break;
  3275. case efExternalEditor:
  3276. Counter = "RemoteFilesOpenedInExternalEditor";
  3277. break;
  3278. default:
  3279. DebugFail();
  3280. }
  3281. }
  3282. }
  3283. else
  3284. {
  3285. LocalFileName = FullFileName;
  3286. OriginalFileName = ExtractFileName(FullFileName);
  3287. switch (ExecuteFileBy)
  3288. {
  3289. case efShell:
  3290. Counter = "LocalFilesExecuted";
  3291. break;
  3292. case efInternalEditor:
  3293. Counter = "LocalFilesOpenedInInternalEditor";
  3294. break;
  3295. case efExternalEditor:
  3296. Counter = "LocalFilesOpenedInExternalEditor";
  3297. break;
  3298. default:
  3299. DebugFail();
  3300. }
  3301. }
  3302. if (!Handled)
  3303. {
  3304. Configuration->Usage->Inc(Counter);
  3305. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3306. ExternalEditor, LocalRootDirectory, RemoteDirectory, false);
  3307. }
  3308. }
  3309. //---------------------------------------------------------------------------
  3310. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3311. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3312. bool AllSelected, bool OnFocused)
  3313. {
  3314. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3315. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3316. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3317. Side = GetSide(Side);
  3318. TCustomDirView * DView = DirView(Side);
  3319. TStrings * FileList =
  3320. AllSelected ?
  3321. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3322. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3323. try
  3324. {
  3325. DebugAssert(AllSelected || (FileList->Count == 1));
  3326. for (int i = 0; i < FileList->Count; i++)
  3327. {
  3328. UnicodeString ListFileName = FileList->Strings[i];
  3329. UnicodeString FileNameOnly =
  3330. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3331. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3332. if (!DView->ItemIsDirectory(Item))
  3333. {
  3334. UnicodeString FullFileName;
  3335. if (!IsSideLocalBrowser(Side))
  3336. {
  3337. FullFileName = DirView(Side)->Path + ListFileName;
  3338. }
  3339. else
  3340. {
  3341. FullFileName = ListFileName;
  3342. }
  3343. TFileMasks::TParams MaskParams;
  3344. MaskParams.Size = DView->ItemFileSize(Item);
  3345. TDateTimePrecision Precision;
  3346. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3347. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3348. FileList->Objects[i], MaskParams);
  3349. }
  3350. }
  3351. }
  3352. __finally
  3353. {
  3354. delete FileList;
  3355. }
  3356. }
  3357. //---------------------------------------------------------------------------
  3358. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3359. {
  3360. // do not forget to add additional options to TemporarilyDownloadFiles, and AS
  3361. CopyParam.FileNameCase = ncNoChange;
  3362. CopyParam.PreserveRights = false;
  3363. CopyParam.PreserveReadOnly = false;
  3364. CopyParam.ReplaceInvalidChars = true;
  3365. CopyParam.IncludeFileMask = TFileMasks();
  3366. CopyParam.NewerOnly = false;
  3367. CopyParam.FileMask = L"";
  3368. }
  3369. //---------------------------------------------------------------------------
  3370. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3371. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3372. {
  3373. TTerminalManager * Manager = TTerminalManager::Instance();
  3374. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3375. {
  3376. if (!NonVisualDataModule->Busy)
  3377. {
  3378. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3379. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3380. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3381. if (Data->Terminal == NULL)
  3382. {
  3383. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3384. if (SameSiteTerminal != NULL)
  3385. {
  3386. UnicodeString Message =
  3387. FMTLOAD(EDIT_SESSION_REATTACH,
  3388. (FileNameOnly, Data->SessionName, FileNameOnly));
  3389. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3390. {
  3391. Data->Terminal = Terminal;
  3392. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3393. Data->SessionName = Terminal->SessionData->SessionName;
  3394. // We might also overwrite session data
  3395. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3396. }
  3397. else
  3398. {
  3399. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3400. Abort();
  3401. }
  3402. }
  3403. }
  3404. // foreground session should reconnect itself
  3405. else if (Terminal != Data->Terminal)
  3406. {
  3407. UnicodeString Message =
  3408. MainInstructions(
  3409. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3410. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3411. {
  3412. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3413. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3414. }
  3415. else
  3416. {
  3417. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3418. Abort();
  3419. }
  3420. }
  3421. }
  3422. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3423. {
  3424. Configuration->Usage->Inc(L"EditInactiveSession");
  3425. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3426. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3427. (ExtractFileName(FileName), Data->SessionName)));
  3428. }
  3429. }
  3430. TStrings * FileList = new TStringList();
  3431. try
  3432. {
  3433. FileList->Add(FileName);
  3434. // consider using the same settings (preset) as when the file was downloaded
  3435. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3436. TemporaryFileCopyParam(CopyParam);
  3437. if (Data->ForceText)
  3438. {
  3439. CopyParam.TransferMode = tmAscii;
  3440. }
  3441. // so i do not need to worry if masking algorithm works in all cases
  3442. // ("" means "copy file name", no masking is actually done)
  3443. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3444. {
  3445. CopyParam.FileMask = L"";
  3446. }
  3447. else
  3448. {
  3449. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3450. }
  3451. DebugAssert(Data->Queue != NULL);
  3452. int Params = cpNoConfirmation | cpTemporary;
  3453. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3454. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3455. QueueItem->CompleteEvent = UploadCompleteEvent;
  3456. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3457. }
  3458. __finally
  3459. {
  3460. delete FileList;
  3461. }
  3462. }
  3463. //---------------------------------------------------------------------------
  3464. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3465. const UnicodeString FileName, const TEditedFileData * Data)
  3466. {
  3467. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3468. DebugAssert(!NonVisualDataModule->Busy);
  3469. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3470. {
  3471. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3472. (ExtractFileName(FileName), Data->SessionName)));
  3473. }
  3474. TManagedTerminal * PrevTerminal = TTerminalManager::Instance()->ActiveTerminal;
  3475. TTerminalManager::Instance()->ActiveTerminal = Data->Terminal;
  3476. NonVisualDataModule->StartBusy();
  3477. try
  3478. {
  3479. std::unique_ptr<TRemoteFile> File;
  3480. UnicodeString RemoteFileName =
  3481. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3482. FTerminal->ExceptionOnFail = true;
  3483. try
  3484. {
  3485. TRemoteFile * AFile = NULL;
  3486. FTerminal->ReadFile(RemoteFileName, AFile);
  3487. File.reset(AFile);
  3488. if (!File->HaveFullFileName)
  3489. {
  3490. File->FullFileName = RemoteFileName;
  3491. }
  3492. }
  3493. __finally
  3494. {
  3495. FTerminal->ExceptionOnFail = false;
  3496. }
  3497. std::unique_ptr<TStrings> FileList(new TStringList());
  3498. FileList->AddObject(RemoteFileName, File.get());
  3499. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3500. UnicodeString TempDir = ExtractFilePath(FileName);
  3501. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3502. TempDir, true, true, true);
  3503. // sanity check, the target file name should be still the same
  3504. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3505. }
  3506. __finally
  3507. {
  3508. NonVisualDataModule->EndBusy();
  3509. // it actually may not exist anymore...
  3510. TTerminalManager::Instance()->ActiveTerminal = PrevTerminal;
  3511. }
  3512. }
  3513. //---------------------------------------------------------------------------
  3514. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3515. const TEditedFileData * Data, bool & KeepOpen)
  3516. {
  3517. // Command is set for external editors only (not for "shell" open).
  3518. if (!Data->Command.IsEmpty())
  3519. {
  3520. bool AnyFound = false;
  3521. bool AnyMDI = false;
  3522. bool AnyNonMDI = false;
  3523. bool AnyDetect = false;
  3524. TEditorList * EditorList = new TEditorList();
  3525. try
  3526. {
  3527. *EditorList = *WinConfiguration->EditorList;
  3528. for (int i = 0; i < EditorList->Count; i++)
  3529. {
  3530. const TEditorPreferences * Editor = EditorList->Editors[i];
  3531. if ((Editor->Data->Editor == edExternal) &&
  3532. (Editor->Data->ExternalEditor == Data->Command))
  3533. {
  3534. AnyFound = true;
  3535. if (Editor->Data->SDIExternalEditor)
  3536. {
  3537. AnyNonMDI = true;
  3538. if (Editor->Data->DetectMDIExternalEditor)
  3539. {
  3540. AnyDetect = true;
  3541. }
  3542. }
  3543. else
  3544. {
  3545. AnyMDI = true;
  3546. }
  3547. }
  3548. }
  3549. bool EnableMDI = false;
  3550. bool DisableDetect = false;
  3551. if (AnyMDI)
  3552. {
  3553. KeepOpen = true;
  3554. if (AnyNonMDI)
  3555. {
  3556. // there is at least one instance of the editor with MDI support enabled,
  3557. // and one with disabled, enable it for all instances
  3558. EnableMDI = true;
  3559. }
  3560. }
  3561. else if (AnyFound && !AnyDetect)
  3562. {
  3563. // at least once instance found but all have MDI autodetection disabled
  3564. // => close the file (default action)
  3565. }
  3566. else
  3567. {
  3568. // no instance of the editor has MDI support enabled
  3569. TMessageParams Params;
  3570. if (AnyFound)
  3571. {
  3572. // there is at least one instance of the editor
  3573. Params.Params |= mpNeverAskAgainCheck;
  3574. }
  3575. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3576. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3577. switch (Answer)
  3578. {
  3579. case qaNeverAskAgain:
  3580. DisableDetect = true;
  3581. break;
  3582. case qaNo:
  3583. EnableMDI = true;
  3584. KeepOpen = true;
  3585. break;
  3586. }
  3587. }
  3588. if (AnyFound && (EnableMDI || DisableDetect))
  3589. {
  3590. bool Changed = false;
  3591. for (int i = 0; i < EditorList->Count; i++)
  3592. {
  3593. const TEditorPreferences * Editor = EditorList->Editors[i];
  3594. if ((Editor->Data->Editor == edExternal) &&
  3595. (Editor->Data->ExternalEditor == Data->Command) &&
  3596. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3597. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3598. {
  3599. Changed = true;
  3600. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3601. if (EnableMDI)
  3602. {
  3603. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3604. }
  3605. if (DisableDetect)
  3606. {
  3607. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3608. }
  3609. EditorList->Change(i, UpdatedEditor);
  3610. }
  3611. }
  3612. if (Changed)
  3613. {
  3614. WinConfiguration->EditorList = EditorList;
  3615. }
  3616. }
  3617. }
  3618. __finally
  3619. {
  3620. delete EditorList;
  3621. }
  3622. }
  3623. else
  3624. {
  3625. // "open" case
  3626. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3627. qaOK, HELP_APP_EARLY_CLOSED);
  3628. }
  3629. }
  3630. //---------------------------------------------------------------------------
  3631. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3632. {
  3633. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3634. }
  3635. //---------------------------------------------------------------------------
  3636. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3637. {
  3638. SideEnter(osRemote);
  3639. }
  3640. //---------------------------------------------------------------------------
  3641. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3642. {
  3643. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3644. SideEnter(osRemote);
  3645. }
  3646. //---------------------------------------------------------------------------
  3647. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3648. {
  3649. FCurrentSide = Side;
  3650. if (Visible)
  3651. {
  3652. UpdateControls();
  3653. }
  3654. }
  3655. //---------------------------------------------------------------------------
  3656. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3657. TStrings * FileList, bool Alternative)
  3658. {
  3659. TCustomDirView * DView = DirView(Side);
  3660. DView->SaveSelection();
  3661. DView->SaveSelectedNames();
  3662. DebugAssert(!FAlternativeDelete);
  3663. FAlternativeDelete = Alternative;
  3664. try
  3665. {
  3666. if (!IsSideLocalBrowser(Side))
  3667. {
  3668. DebugAssert(Terminal != NULL);
  3669. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3670. }
  3671. else
  3672. {
  3673. try
  3674. {
  3675. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3676. }
  3677. __finally
  3678. {
  3679. ReloadLocalDirectory();
  3680. }
  3681. }
  3682. FAlternativeDelete = false;
  3683. }
  3684. catch(...)
  3685. {
  3686. FAlternativeDelete = false;
  3687. DView->DiscardSavedSelection();
  3688. throw;
  3689. }
  3690. DView->RestoreSelection();
  3691. }
  3692. //---------------------------------------------------------------------------
  3693. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3694. {
  3695. DebugAssert(Terminal);
  3696. DebugAssert(!IsLocalBrowserMode());
  3697. RemoteDirView->SaveSelection();
  3698. RemoteDirView->SaveSelectedNames();
  3699. try
  3700. {
  3701. if (Lock)
  3702. {
  3703. Terminal->LockFiles(FileList);
  3704. }
  3705. else
  3706. {
  3707. Terminal->UnlockFiles(FileList);
  3708. }
  3709. }
  3710. catch(...)
  3711. {
  3712. RemoteDirView->DiscardSavedSelection();
  3713. throw;
  3714. }
  3715. RemoteDirView->RestoreSelection();
  3716. }
  3717. //---------------------------------------------------------------------------
  3718. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  3719. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3720. bool NoConfirmation, bool Move)
  3721. {
  3722. DebugAssert(Terminal != NULL);
  3723. DebugAssert(!IsLocalBrowserMode());
  3724. // update Terminal->StateData->RemoteDirectory
  3725. UpdateTerminal(Terminal);
  3726. if (Session == NULL)
  3727. {
  3728. Session = TTerminalManager::Instance()->ActiveTerminal;
  3729. }
  3730. if (Session == TTerminalManager::Instance()->ActiveTerminal)
  3731. {
  3732. if (RemoteDriveView->DropTarget != NULL)
  3733. {
  3734. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3735. }
  3736. else if (RemoteDirView->DropTarget != NULL)
  3737. {
  3738. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3739. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3740. }
  3741. else
  3742. {
  3743. Target = RemoteDirView->Path;
  3744. }
  3745. }
  3746. else
  3747. {
  3748. Target = Session->StateData->RemoteDirectory;
  3749. }
  3750. Target = UnixIncludeTrailingBackslash(Target);
  3751. if (FileList->Count == 1)
  3752. {
  3753. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3754. }
  3755. else
  3756. {
  3757. FileMask = AnyMask;
  3758. }
  3759. DirectCopy = FTerminal->IsCapable[fcRemoteCopy] || FTerminal->IsCapable[fcSecondaryShell];
  3760. bool Result = true;
  3761. if (!NoConfirmation)
  3762. {
  3763. bool Multi = (FileList->Count > 1);
  3764. if (Move)
  3765. {
  3766. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3767. }
  3768. else
  3769. {
  3770. TStrings * Sessions = TTerminalManager::Instance()->TerminalList;
  3771. TStrings * Directories = new TStringList;
  3772. try
  3773. {
  3774. for (int Index = 0; Index < Sessions->Count; Index++)
  3775. {
  3776. TManagedTerminal * Terminal =
  3777. dynamic_cast<TManagedTerminal *>(Sessions->Objects[Index]);
  3778. Directories->Add(Terminal->StateData->RemoteDirectory);
  3779. }
  3780. TDirectRemoteCopy AllowDirectCopy;
  3781. if (FTerminal->IsCapable[fcRemoteCopy] || FTerminal->CommandSessionOpened)
  3782. {
  3783. DebugAssert(DirectCopy);
  3784. AllowDirectCopy = drcAllow;
  3785. }
  3786. else if (FTerminal->IsCapable[fcSecondaryShell])
  3787. {
  3788. DebugAssert(DirectCopy);
  3789. AllowDirectCopy = drcConfirmCommandSession;
  3790. }
  3791. else
  3792. {
  3793. DebugAssert(!DirectCopy);
  3794. AllowDirectCopy = drcDisallow;
  3795. }
  3796. void * ASession = Session;
  3797. Result = DoRemoteCopyDialog(Sessions, Directories, AllowDirectCopy,
  3798. Multi, ASession, Target, FileMask, DirectCopy, TTerminalManager::Instance()->ActiveTerminal);
  3799. Session = static_cast<TManagedTerminal *>(ASession);
  3800. }
  3801. __finally
  3802. {
  3803. delete Directories;
  3804. }
  3805. }
  3806. }
  3807. return Result;
  3808. }
  3809. //---------------------------------------------------------------------------
  3810. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3811. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  3812. {
  3813. DebugAssert(!IsLocalBrowserMode());
  3814. bool DirectCopy;
  3815. UnicodeString Target, FileMask;
  3816. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3817. if (Result)
  3818. {
  3819. if (!Move && !DirectCopy)
  3820. {
  3821. Configuration->Usage->Inc("RemoteCopyTemp");
  3822. UnicodeString RootTempDir;
  3823. UnicodeString TempDir;
  3824. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3825. TStrings * TemporaryFilesList = new TStringList();
  3826. try
  3827. {
  3828. TMakeLocalFileListParams MakeFileListParam;
  3829. MakeFileListParam.FileList = TemporaryFilesList;
  3830. MakeFileListParam.FileTimes = NULL;
  3831. MakeFileListParam.IncludeDirs = true;
  3832. MakeFileListParam.Recursive = false;
  3833. ProcessLocalDirectory(TempDir, FTerminal->MakeLocalFileList, &MakeFileListParam);
  3834. TTerminalManager::Instance()->ActiveTerminal = Session;
  3835. if (TemporaryFilesList->Count > 0)
  3836. {
  3837. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3838. CopyParam.FileMask = FileMask;
  3839. DebugAssert(!FAutoOperation);
  3840. FAutoOperation = true;
  3841. FTerminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3842. }
  3843. }
  3844. __finally
  3845. {
  3846. delete TemporaryFilesList;
  3847. FAutoOperation = false;
  3848. if (!RootTempDir.IsEmpty())
  3849. {
  3850. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3851. }
  3852. }
  3853. }
  3854. else
  3855. {
  3856. RemoteDirView->SaveSelection();
  3857. RemoteDirView->SaveSelectedNames();
  3858. try
  3859. {
  3860. if (Move)
  3861. {
  3862. Configuration->Usage->Inc("RemoteMove");
  3863. Terminal->MoveFiles(FileList, Target, FileMask);
  3864. }
  3865. else
  3866. {
  3867. Configuration->Usage->Inc("RemoteCopyDirect");
  3868. DebugAssert(DirectCopy);
  3869. DebugAssert(Session == FTerminal);
  3870. if (FTerminal->IsCapable[fcRemoteCopy] ||
  3871. FTerminal->CommandSessionOpened ||
  3872. CommandSessionFallback())
  3873. {
  3874. Terminal->CopyFiles(FileList, Target, FileMask);
  3875. }
  3876. }
  3877. }
  3878. catch(...)
  3879. {
  3880. RemoteDirView->DiscardSavedSelection();
  3881. throw;
  3882. }
  3883. RemoteDirView->RestoreSelection();
  3884. }
  3885. }
  3886. return Result;
  3887. }
  3888. //---------------------------------------------------------------------------
  3889. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3890. const UnicodeString & Path, TRemoteProperties & Properties)
  3891. {
  3892. DebugAssert(!IsLocalBrowserMode());
  3893. Properties.Valid = Properties.Valid << vpEncrypt;
  3894. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  3895. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  3896. }
  3897. //---------------------------------------------------------------------------
  3898. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3899. {
  3900. Side = GetSide(Side);
  3901. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3902. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  3903. UnicodeString Name = LoadStr(NEW_FOLDER);
  3904. int AllowedChanges =
  3905. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  3906. bool SaveSettings = false;
  3907. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  3908. {
  3909. TWindowLock Lock(this);
  3910. if (!IsSideLocalBrowser(Side))
  3911. {
  3912. if (SaveSettings)
  3913. {
  3914. GUIConfiguration->NewDirectoryProperties = Properties;
  3915. }
  3916. CreateRemoteDirectory(Name, Properties);
  3917. }
  3918. else
  3919. {
  3920. DirView(Side)->CreateDirectory(Name);
  3921. }
  3922. }
  3923. }
  3924. //---------------------------------------------------------------------------
  3925. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  3926. {
  3927. TWindowLock Lock(this);
  3928. DirView(Side)->ExecuteHomeDirectory();
  3929. }
  3930. //---------------------------------------------------------------------------
  3931. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  3932. {
  3933. DoOpenDirectoryDialog(odBrowse, Side);
  3934. }
  3935. //---------------------------------------------------------------------------
  3936. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  3937. {
  3938. UnicodeString Path = Bookmark->GetSideDirectory(Side);
  3939. bool Result = !Path.IsEmpty();
  3940. if (Result)
  3941. {
  3942. // While we might get here when the session is closed (from location profiles),
  3943. // it's not a problem as the Path setter is noop then
  3944. DirView(Side)->Path = Path;
  3945. }
  3946. return Result;
  3947. }
  3948. //---------------------------------------------------------------------------
  3949. void __fastcall TCustomScpExplorerForm::CalculateSize(
  3950. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  3951. bool & Close)
  3952. {
  3953. // terminal can be already closed (e.g. dropped connection)
  3954. if (Terminal != NULL)
  3955. {
  3956. try
  3957. {
  3958. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  3959. }
  3960. catch(...)
  3961. {
  3962. if (!Terminal->Active)
  3963. {
  3964. Close = true;
  3965. }
  3966. throw;
  3967. }
  3968. }
  3969. }
  3970. //---------------------------------------------------------------------------
  3971. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  3972. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  3973. bool & Close)
  3974. {
  3975. // terminal can be already closed (e.g. dropped connection)
  3976. if (Terminal != NULL)
  3977. {
  3978. Configuration->Usage->Inc(L"ChecksumCalculated");
  3979. try
  3980. {
  3981. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  3982. }
  3983. catch(...)
  3984. {
  3985. if (!Terminal->Active)
  3986. {
  3987. Close = true;
  3988. }
  3989. throw;
  3990. }
  3991. }
  3992. }
  3993. //---------------------------------------------------------------------------
  3994. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  3995. {
  3996. bool Result;
  3997. if (!IsSideLocalBrowser(Side))
  3998. {
  3999. TRemoteTokenList * GroupList = NULL;
  4000. TRemoteTokenList * UserList = NULL;
  4001. try
  4002. {
  4003. TRemoteProperties CurrentProperties;
  4004. if (Terminal->LoadFilesProperties(FileList))
  4005. {
  4006. RemoteDirView->Invalidate();
  4007. }
  4008. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4009. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4010. if (CapableGroupChanging || CapableOwnerChanging)
  4011. {
  4012. if (CapableGroupChanging)
  4013. {
  4014. GroupList = Terminal->Groups->Duplicate();
  4015. }
  4016. if (CapableOwnerChanging)
  4017. {
  4018. UserList = Terminal->Users->Duplicate();
  4019. }
  4020. TRemoteDirectory * Files = Terminal->Files;
  4021. int Count = Files->Count;
  4022. if (Count > 100)
  4023. {
  4024. Count = 100;
  4025. }
  4026. for (int Index = 0; Index < Count; Index++)
  4027. {
  4028. TRemoteFile * File = Files->Files[Index];
  4029. if (CapableGroupChanging)
  4030. {
  4031. GroupList->AddUnique(File->Group);
  4032. }
  4033. if (CapableOwnerChanging)
  4034. {
  4035. UserList->AddUnique(File->Owner);
  4036. }
  4037. }
  4038. // if we haven't collected tokens for all files in current directory,
  4039. // make sure we collect them at least for all selected files.
  4040. // (note that so far the files in FileList has to be from current direcotry)
  4041. if (Count < Files->Count)
  4042. {
  4043. for (int Index = 0; Index < FileList->Count; Index++)
  4044. {
  4045. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4046. if (CapableGroupChanging)
  4047. {
  4048. GroupList->AddUnique(File->Group);
  4049. }
  4050. if (CapableOwnerChanging)
  4051. {
  4052. UserList->AddUnique(File->Owner);
  4053. }
  4054. }
  4055. }
  4056. }
  4057. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4058. int Flags = 0;
  4059. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4060. if (CapableOwnerChanging) Flags |= cpOwner;
  4061. if (CapableGroupChanging) Flags |= cpGroup;
  4062. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4063. if (Terminal->IsCapable[fcCalculatingChecksum])
  4064. {
  4065. CalculateChecksumEvent = CalculateChecksum;
  4066. }
  4067. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4068. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4069. TRemoteProperties NewProperties = CurrentProperties;
  4070. Result =
  4071. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4072. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4073. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4074. CalculateSize, CalculateChecksumEvent);
  4075. if (Result)
  4076. {
  4077. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4078. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4079. }
  4080. }
  4081. __finally
  4082. {
  4083. delete GroupList;
  4084. delete UserList;
  4085. }
  4086. }
  4087. else
  4088. {
  4089. DirView(Side)->DisplayPropertiesMenu();
  4090. Result = true;
  4091. }
  4092. return Result;
  4093. }
  4094. //---------------------------------------------------------------------------
  4095. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4096. {
  4097. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4098. {
  4099. FIgnoreNextDialogChar = Key;
  4100. }
  4101. Key = 0;
  4102. }
  4103. //---------------------------------------------------------------------------
  4104. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4105. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4106. {
  4107. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4108. if (Command != NULL)
  4109. {
  4110. KeyProcessed(Key, Shift);
  4111. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4112. {
  4113. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4114. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4115. false, false, const_cast<TCustomCommandType *>(Command));
  4116. }
  4117. }
  4118. }
  4119. //---------------------------------------------------------------------------
  4120. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4121. {
  4122. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4123. {
  4124. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4125. }
  4126. if (!DirView(osCurrent)->IsEditing())
  4127. {
  4128. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4129. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4130. {
  4131. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4132. if (((Action->ShortCut == KeyShortCut) ||
  4133. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4134. AllowedAction(Action, aaShortCut))
  4135. {
  4136. // Has to be called before the action as the dialog char is already in queue.
  4137. // So when the action consumes message queue, we already need to have the
  4138. // FIgnoreNextDialogChar set
  4139. KeyProcessed(Key, Shift);
  4140. // Reset reference to previous component (when menu/toolbar was clicked).
  4141. // Needed to detect that action was invoked by keyboard shortcut
  4142. // in TNonVisualDataModule::ExplorerActionsExecute
  4143. Action->ActionComponent = NULL;
  4144. Action->Execute();
  4145. return;
  4146. }
  4147. }
  4148. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4149. {
  4150. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4151. {
  4152. KeyProcessed(Key, Shift);
  4153. TTerminalManager::Instance()->ActiveTerminalIndex = i;
  4154. return;
  4155. }
  4156. }
  4157. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4158. {
  4159. KeyProcessed(Key, Shift);
  4160. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4161. }
  4162. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4163. if (IsCustomShortCut(CustomShortCut))
  4164. {
  4165. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4166. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4167. if (WinConfiguration->SharedBookmarks != NULL)
  4168. {
  4169. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4170. if ((Bookmark != NULL) &&
  4171. OpenBookmark(FCurrentSide, Bookmark))
  4172. {
  4173. KeyProcessed(Key, Shift);
  4174. }
  4175. }
  4176. }
  4177. }
  4178. TForm::KeyDown(Key, Shift);
  4179. }
  4180. //---------------------------------------------------------------------------
  4181. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4182. {
  4183. DebugAssert(Terminal != NULL);
  4184. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4185. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  4186. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4187. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4188. bool SecurityEnabled = !SessionInfo.SecurityProtocolName.IsEmpty();
  4189. SessionStatusBar->Panels->Items[Offset + 0]->Enabled = SecurityEnabled;
  4190. // expanded from ?: to avoid memory leaks
  4191. if (SecurityEnabled)
  4192. {
  4193. SessionStatusBar->Panels->Items[Offset + 0]->Hint =
  4194. FMTLOAD(STATUS_SECURE, (SessionInfo.SecurityProtocolName));
  4195. }
  4196. else
  4197. {
  4198. SessionStatusBar->Panels->Items[Offset + 0]->Hint = LoadStr(STATUS_INSECURE);
  4199. }
  4200. if (FileSystemInfo.ProtocolName.IsEmpty())
  4201. {
  4202. SessionStatusBar->Panels->Items[Offset + 1]->Caption = SessionInfo.ProtocolName;
  4203. }
  4204. else
  4205. {
  4206. SessionStatusBar->Panels->Items[Offset + 1]->Caption = FileSystemInfo.ProtocolName;
  4207. }
  4208. SessionStatusBar->Panels->Items[Offset + 1]->Hint = LoadStr(STATUS_PROTOCOL_HINT);
  4209. SessionStatusBar->Panels->Items[Offset + 2]->Enabled =
  4210. (!SessionInfo.CSCompression.IsEmpty() || !SessionInfo.SCCompression.IsEmpty());
  4211. UnicodeString CSCompressionStr = DefaultStr(SessionInfo.CSCompression, LoadStr(NO_STR));
  4212. UnicodeString SCCompressionStr = DefaultStr(SessionInfo.SCCompression, LoadStr(NO_STR));
  4213. UnicodeString CompressionStr = CSCompressionStr;
  4214. if (CSCompressionStr != SCCompressionStr)
  4215. {
  4216. CompressionStr += UnicodeString(L"/") + SCCompressionStr;
  4217. }
  4218. SessionStatusBar->Panels->Items[Offset + 2]->Hint = FMTLOAD(STATUS_COMPRESSION_HINT, (CompressionStr));
  4219. SessionStatusBar->Panels->Items[Offset + 3]->Hint = LoadStr(STATUS_DURATION_HINT);
  4220. UpdateStatusBar();
  4221. }
  4222. //---------------------------------------------------------------------------
  4223. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4224. {
  4225. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4226. DebugAssert(SessionStatusBar != NULL);
  4227. if (!Terminal || !Terminal->Active || Terminal->Status < ssOpened)
  4228. {
  4229. // note: (Terminal->Status < sshReady) currently never happens here,
  4230. // so STATUS_CONNECTING is never used
  4231. SessionStatusBar->SimplePanel = true;
  4232. SessionStatusBar->SimpleText = LoadStr(
  4233. !Terminal || !Terminal->Active ? STATUS_NOT_CONNECTED : STATUS_CONNECTING);
  4234. }
  4235. else
  4236. {
  4237. DebugAssert(Terminal);
  4238. SessionStatusBar->SimplePanel = false;
  4239. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4240. if (!FNote.IsEmpty())
  4241. {
  4242. SessionStatusBar->Panels->Items[0]->Caption = FNote;
  4243. }
  4244. else
  4245. {
  4246. UpdateStatusPanelText(SessionStatusBar->Panels->Items[0]);
  4247. }
  4248. SessionStatusBar->Panels->Items[0]->Hint = FNoteHints;
  4249. SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1]->Caption =
  4250. FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4251. }
  4252. }
  4253. //---------------------------------------------------------------------------
  4254. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4255. {
  4256. Panel->Caption = L"";
  4257. }
  4258. //---------------------------------------------------------------------------
  4259. void __fastcall TCustomScpExplorerForm::Idle()
  4260. {
  4261. if (FShowing || FStandaloneEditing)
  4262. {
  4263. FEditorManager->Check();
  4264. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4265. // that needs to know if queue view is visible (and it may be closed after queue update)
  4266. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4267. }
  4268. if (FShowing)
  4269. {
  4270. if (!NonVisualDataModule->Busy &&
  4271. // Menu is opened or drag&drop is going on
  4272. (Mouse->Capture == NULL))
  4273. {
  4274. if (FRefreshRemoteDirectory)
  4275. {
  4276. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4277. {
  4278. Terminal->RefreshDirectory();
  4279. }
  4280. FRefreshRemoteDirectory = false;
  4281. }
  4282. if (FRefreshLocalDirectory)
  4283. {
  4284. ReloadLocalDirectory();
  4285. FRefreshLocalDirectory = false;
  4286. }
  4287. if (WinConfiguration->RefreshRemotePanel)
  4288. {
  4289. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4290. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4291. {
  4292. RemoteDirView->ReloadDirectory();
  4293. }
  4294. }
  4295. }
  4296. }
  4297. if (FShowing || FStandaloneEditing)
  4298. {
  4299. if (FQueueStatusInvalidated)
  4300. {
  4301. UpdateQueueStatus(false);
  4302. }
  4303. RefreshQueueItems();
  4304. }
  4305. if (FShowing)
  4306. {
  4307. UpdateStatusBar();
  4308. }
  4309. FIgnoreNextDialogChar = 0;
  4310. }
  4311. //---------------------------------------------------------------------------
  4312. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4313. {
  4314. try
  4315. {
  4316. FUserActionTimer->Enabled = false;
  4317. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4318. {
  4319. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4320. {
  4321. FPendingQueueActionItem->ProcessUserAction();
  4322. }
  4323. }
  4324. }
  4325. __finally
  4326. {
  4327. FPendingQueueActionItem = NULL;
  4328. }
  4329. }
  4330. //---------------------------------------------------------------------------
  4331. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4332. {
  4333. if (WinConfiguration->MinimizeToTray)
  4334. {
  4335. UpdateTrayIcon();
  4336. FTrayIcon->Visible = true;
  4337. if (Visible)
  4338. {
  4339. ShowWindow(Handle, SW_HIDE);
  4340. }
  4341. }
  4342. }
  4343. //---------------------------------------------------------------------------
  4344. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4345. {
  4346. // WORKAROUND
  4347. // When restoring maximized window from minimization,
  4348. // rarely some controls do not align properly.
  4349. // Two instances seen (both for Commander):
  4350. // - When restoring, window is temporarily narrower (not maximized),
  4351. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4352. // Once maximized already, top dock shinks back, but the session PageControl,
  4353. // do not align up, leaving space between TopDock and PageControl.
  4354. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4355. for (int Index = 0; Index < ControlCount; Index++)
  4356. {
  4357. RealignControl(Controls[Index]);
  4358. }
  4359. if (FTrayIcon->Visible)
  4360. {
  4361. FTrayIcon->Visible = false;
  4362. if (Visible)
  4363. {
  4364. ShowWindow(Handle, SW_SHOW);
  4365. }
  4366. }
  4367. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4368. {
  4369. FNeedSession = false;
  4370. NonVisualDataModule->StartBusy();
  4371. try
  4372. {
  4373. NeedSession(true);
  4374. }
  4375. __finally
  4376. {
  4377. NonVisualDataModule->EndBusy();
  4378. }
  4379. }
  4380. }
  4381. //---------------------------------------------------------------------------
  4382. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4383. {
  4384. FTrayIcon->Hint = GetMainForm()->Caption;
  4385. }
  4386. //---------------------------------------------------------------------------
  4387. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4388. {
  4389. UpdateTrayIcon();
  4390. }
  4391. //---------------------------------------------------------------------------
  4392. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4393. {
  4394. // workaround
  4395. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4396. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4397. // But WM_ACTIVATEAPP message can be received even when
  4398. // the main window is minimized to the tray and internal editor window is focused
  4399. // (after another application was previously active)
  4400. if (::IsIconic(Handle))
  4401. {
  4402. if (!IsAppIconic())
  4403. {
  4404. SetAppIconic(true);
  4405. }
  4406. }
  4407. ::ApplicationRestore();
  4408. Application->BringToFront();
  4409. }
  4410. //---------------------------------------------------------------------------
  4411. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4412. {
  4413. RestoreApp();
  4414. }
  4415. //---------------------------------------------------------------------------
  4416. void __fastcall TCustomScpExplorerForm::NewSession(bool FromSite, const UnicodeString & SessionUrl)
  4417. {
  4418. if (OpenInNewWindow())
  4419. {
  4420. // todo: Pass FromSite
  4421. ExecuteNewInstance(SessionUrl);
  4422. }
  4423. else
  4424. {
  4425. TTerminalManager::Instance()->NewSession(FromSite, SessionUrl);
  4426. }
  4427. }
  4428. //---------------------------------------------------------------------------
  4429. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4430. {
  4431. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->SessionName;
  4432. StoredSessions->NewSession(SessionName, SessionData);
  4433. // modified only, explicit
  4434. StoredSessions->Save(false, true);
  4435. // encode session name because of slashes in hierarchical sessions
  4436. return EncodeUrlString(SessionName);
  4437. }
  4438. //---------------------------------------------------------------------------
  4439. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4440. {
  4441. // current working directories become defaults here, what is not right
  4442. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4443. return SaveHiddenDuplicateSession(SessionData.get());
  4444. }
  4445. //---------------------------------------------------------------------------
  4446. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4447. {
  4448. if (OpenInNewWindow())
  4449. {
  4450. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4451. }
  4452. else
  4453. {
  4454. // current working directories become defaults here, what is not right
  4455. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4456. TTerminalManager * Manager = TTerminalManager::Instance();
  4457. TManagedTerminal * Terminal = Manager->NewManagedTerminal(SessionData.get());
  4458. Manager->ActiveTerminal = Terminal;
  4459. }
  4460. }
  4461. //---------------------------------------------------------------------------
  4462. void __fastcall TCustomScpExplorerForm::RenameSession()
  4463. {
  4464. UnicodeString Name = Terminal->SessionData->SessionName;
  4465. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME))
  4466. {
  4467. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4468. TSessionData::ValidateName(Name);
  4469. // This is inconsistent with how color (for example) is handled.
  4470. Terminal->SessionData->Name = Name;
  4471. UpdateControls();
  4472. // Add/Remove distinguishing paths from sessions of the same name.
  4473. DoTerminalListChanged();
  4474. }
  4475. }
  4476. //---------------------------------------------------------------------------
  4477. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4478. {
  4479. DebugAssert(Queue != NULL);
  4480. bool Result = Queue->IsEmpty;
  4481. if (!Result)
  4482. {
  4483. SetFocus();
  4484. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4485. }
  4486. return Result;
  4487. }
  4488. //---------------------------------------------------------------------------
  4489. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4490. {
  4491. return CanCloseQueue(FQueue);
  4492. }
  4493. //---------------------------------------------------------------------------
  4494. void __fastcall TCustomScpExplorerForm::CloseSession()
  4495. {
  4496. if (CanCloseQueue())
  4497. {
  4498. TTerminalManager::Instance()->FreeActiveTerminal();
  4499. }
  4500. }
  4501. //---------------------------------------------------------------------------
  4502. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4503. {
  4504. if (CanCloseQueue())
  4505. {
  4506. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4507. }
  4508. }
  4509. //---------------------------------------------------------------------------
  4510. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4511. {
  4512. RemoteDirView->Terminal = Terminal;
  4513. UpdateRemotePathComboBox(false);
  4514. UpdateControls();
  4515. }
  4516. //---------------------------------------------------------------------------
  4517. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4518. {
  4519. if (DebugAlwaysTrue(!Terminal->Active))
  4520. {
  4521. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4522. }
  4523. }
  4524. //---------------------------------------------------------------------------
  4525. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4526. {
  4527. if (OpenInNewWindow())
  4528. {
  4529. // encode session name because of slashes in hierarchical sessions
  4530. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4531. }
  4532. else
  4533. {
  4534. TTerminalManager * Manager = TTerminalManager::Instance();
  4535. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4536. Manager->ActiveTerminal = Terminal;
  4537. }
  4538. }
  4539. //---------------------------------------------------------------------------
  4540. void __fastcall TCustomScpExplorerForm::DoOpenFolderOrWorkspace(const UnicodeString & Name, bool ConnectFirstTerminal)
  4541. {
  4542. TTerminalManager * Manager = TTerminalManager::Instance();
  4543. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4544. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4545. TManagedTerminal * FirstTerminal = Manager->NewTerminals(DataList.get());
  4546. if (!ConnectFirstTerminal)
  4547. {
  4548. FirstTerminal->Disconnected = true;
  4549. FirstTerminal->DisconnectedTemporarily = true;
  4550. }
  4551. Manager->ActiveTerminal = FirstTerminal;
  4552. }
  4553. //---------------------------------------------------------------------------
  4554. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4555. {
  4556. if (OpenInNewWindow())
  4557. {
  4558. ExecuteNewInstance(Name);
  4559. }
  4560. else
  4561. {
  4562. DoOpenFolderOrWorkspace(Name, true);
  4563. }
  4564. }
  4565. //---------------------------------------------------------------------------
  4566. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4567. bool &CanClose)
  4568. {
  4569. if (Terminal != NULL)
  4570. {
  4571. if (Terminal->Active && WinConfiguration->ConfirmClosingSession)
  4572. {
  4573. unsigned int Result;
  4574. TMessageParams Params(mpNeverAskAgainCheck);
  4575. UnicodeString Message;
  4576. int Answers = qaOK | qaCancel;
  4577. if (TTerminalManager::Instance()->Count > 1)
  4578. {
  4579. if (!WinConfiguration->AutoSaveWorkspace)
  4580. {
  4581. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  4582. }
  4583. else
  4584. {
  4585. Message = LoadStr(CLOSE_SESSIONS);
  4586. }
  4587. }
  4588. else
  4589. {
  4590. if (!WinConfiguration->AutoSaveWorkspace)
  4591. {
  4592. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  4593. }
  4594. else
  4595. {
  4596. Message = LoadStr(CLOSE_SESSION);
  4597. }
  4598. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  4599. }
  4600. UnicodeString Note;
  4601. if (WinConfiguration->AutoSaveWorkspace)
  4602. {
  4603. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  4604. }
  4605. else
  4606. {
  4607. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  4608. Answers = qaYes | qaNo | qaCancel;
  4609. }
  4610. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  4611. SetFocus();
  4612. Result = MessageDialog(Message, qtConfirmation,
  4613. Answers, HELP_NONE, &Params);
  4614. if (Result == qaNeverAskAgain)
  4615. {
  4616. WinConfiguration->ConfirmClosingSession = false;
  4617. }
  4618. if (Result == qaNo)
  4619. {
  4620. CanClose = SaveWorkspace(true);
  4621. // note that the workspace will be saved redundatly again from FormClose
  4622. }
  4623. else
  4624. {
  4625. CanClose =
  4626. (Result == qaOK) ||
  4627. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4628. (Result == qaNeverAskAgain);
  4629. }
  4630. }
  4631. if (CanClose)
  4632. {
  4633. CanClose = CanCloseQueue();
  4634. }
  4635. }
  4636. if (CanClose)
  4637. {
  4638. CanClose =
  4639. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4640. FEditorManager->CloseExternalFilesWithoutProcess();
  4641. if (CanClose)
  4642. {
  4643. while (CanClose && (FLocalEditors->Count > 0))
  4644. {
  4645. int PrevCount = FLocalEditors->Count;
  4646. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4647. CanClose = (FLocalEditors->Count < PrevCount);
  4648. }
  4649. if (CanClose)
  4650. {
  4651. CanClose = FEditorManager->Empty(true);
  4652. if (!CanClose)
  4653. {
  4654. SetFocus();
  4655. CanClose =
  4656. (MessageDialog(
  4657. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4658. }
  4659. }
  4660. }
  4661. }
  4662. }
  4663. //---------------------------------------------------------------------------
  4664. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4665. {
  4666. TForm * Form = dynamic_cast<TForm *>(Sender);
  4667. DebugAssert(Form != NULL);
  4668. Form->Close();
  4669. }
  4670. //---------------------------------------------------------------------------
  4671. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4672. {
  4673. TForm * Form = dynamic_cast<TForm *>(Sender);
  4674. delete Form;
  4675. }
  4676. //---------------------------------------------------------------------------
  4677. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4678. {
  4679. while (FLocalEditors->Count > 0)
  4680. {
  4681. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4682. }
  4683. }
  4684. //---------------------------------------------------------------------------
  4685. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4686. TObject *Sender)
  4687. {
  4688. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4689. try
  4690. {
  4691. SetProperties(osRemote, FileList);
  4692. }
  4693. __finally
  4694. {
  4695. delete FileList;
  4696. }
  4697. }
  4698. //---------------------------------------------------------------------------
  4699. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4700. {
  4701. if (value)
  4702. {
  4703. if (Component == fcCustomCommandsBand)
  4704. {
  4705. UpdateCustomCommandsToolbar();
  4706. }
  4707. }
  4708. }
  4709. //---------------------------------------------------------------------------
  4710. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4711. {
  4712. TControl * Control = GetComponent(Component);
  4713. DebugAssert(Control);
  4714. bool Changed = (Control->Visible != value);
  4715. if (Changed)
  4716. {
  4717. ComponentShowing(Component, value);
  4718. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4719. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4720. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4721. if (value)
  4722. {
  4723. int RemainingHeight = Control->Parent->ClientHeight;
  4724. int RemainingWidth = Control->Parent->ClientWidth;
  4725. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4726. {
  4727. TControl * ChildControl = Control->Parent->Controls[i];
  4728. if (ChildControl->Visible)
  4729. {
  4730. switch (ChildControl->Align)
  4731. {
  4732. case alTop:
  4733. case alBottom:
  4734. RemainingHeight -= ChildControl->Height;
  4735. break;
  4736. case alLeft:
  4737. case alRight:
  4738. RemainingWidth -= ChildControl->Width;
  4739. break;
  4740. }
  4741. }
  4742. }
  4743. int Reserve = ScaleByTextHeight(this, 32);
  4744. // queue in explorer, trees in commander
  4745. if (Control->Height > RemainingHeight - Reserve)
  4746. {
  4747. Control->Height = RemainingHeight / 2;
  4748. }
  4749. if (Control->Width > RemainingWidth - Reserve)
  4750. {
  4751. Control->Width = RemainingWidth / 2;
  4752. }
  4753. }
  4754. Control->Visible = value;
  4755. if (WasFocused && Visible)
  4756. {
  4757. DirView(osCurrent)->SetFocus();
  4758. }
  4759. FixControlsPlacement();
  4760. }
  4761. }
  4762. //---------------------------------------------------------------------------
  4763. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4764. {
  4765. TControl * Control = GetComponent(Component);
  4766. if (Control == NULL)
  4767. {
  4768. return false;
  4769. }
  4770. else
  4771. {
  4772. return Control->Visible;
  4773. }
  4774. }
  4775. //---------------------------------------------------------------------------
  4776. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4777. {
  4778. bool Result;
  4779. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4780. {
  4781. Result = !IsUWP();
  4782. }
  4783. else
  4784. {
  4785. Result = true;
  4786. }
  4787. return Result;
  4788. }
  4789. //---------------------------------------------------------------------------
  4790. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4791. {
  4792. if (DirView(osOther)->ItemFocused != NULL)
  4793. {
  4794. DirView(osOther)->ItemFocused->MakeVisible(false);
  4795. }
  4796. QueueSplitter->Visible = QueuePanel->Visible;
  4797. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  4798. }
  4799. //---------------------------------------------------------------------------
  4800. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4801. {
  4802. switch (Component) {
  4803. case fcStatusBar: return RemoteStatusBar;
  4804. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4805. case fcQueueView: return QueuePanel;
  4806. case fcQueueToolbar: return QueueDock;
  4807. case fcRemoteTree: return RemoteDrivePanel;
  4808. case fcSessionsTabs: return SessionsPageControl;
  4809. default: return NULL;
  4810. }
  4811. }
  4812. //---------------------------------------------------------------------------
  4813. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4814. TObject *Sender, TListColumn *Column, TPoint &Point)
  4815. {
  4816. DebugAssert(NonVisualDataModule && Column && Sender);
  4817. NonVisualDataModule->ListColumn = Column;
  4818. TPopupMenu * DirViewColumnMenu;
  4819. if (dynamic_cast<TUnixDirView *>(Sender) != NULL)
  4820. {
  4821. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4822. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4823. (Column->Index == uvName);
  4824. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4825. (Column->Index == uvSize);
  4826. }
  4827. else
  4828. {
  4829. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4830. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4831. (Column->Index == dvName);
  4832. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4833. (Column->Index == dvSize);
  4834. }
  4835. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  4836. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  4837. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  4838. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4839. }
  4840. //---------------------------------------------------------------------------
  4841. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4842. TObject *Sender, TListItem *Item, bool &AllowExec)
  4843. {
  4844. DoDirViewExecFile(Sender, Item, AllowExec);
  4845. }
  4846. //---------------------------------------------------------------------------
  4847. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4848. TListItem * Item, bool & AllowExec)
  4849. {
  4850. DebugAssert(Sender && Item && Configuration);
  4851. DebugAssert(AllowExec);
  4852. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  4853. bool Remote = (ADirView == DirView(osRemote)) && !IsSideLocalBrowser(osRemote);
  4854. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks;
  4855. TOperationSide Side = (Remote ? osRemote : osLocal); // TODO
  4856. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  4857. // on files only (not directories)
  4858. // and only when symlinks are resolved (apply to remote panel only)
  4859. if (!ADirView->ItemIsDirectory(Item) &&
  4860. (ResolvedSymlinks || FForceExecution || Terminal->IsEncryptingFiles()))
  4861. {
  4862. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  4863. !FForceExecution &&
  4864. (ResolvedSymlinks || Terminal->IsEncryptingFiles()))
  4865. {
  4866. if (WinConfiguration->DoubleClickAction == dcaCopy)
  4867. {
  4868. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  4869. UnlockWindow();
  4870. try
  4871. {
  4872. ExecuteFileOperation(
  4873. foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  4874. }
  4875. __finally
  4876. {
  4877. LockWindow();
  4878. }
  4879. AllowExec = false;
  4880. }
  4881. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  4882. {
  4883. if (!Remote || !WinConfiguration->DisableOpenEdit)
  4884. {
  4885. ExecuteFile(Side, efDefaultEditor);
  4886. AllowExec = false;
  4887. }
  4888. }
  4889. else
  4890. {
  4891. DebugFail();
  4892. }
  4893. }
  4894. // if we have not done anything special, fall back to default behavior
  4895. if (AllowExec)
  4896. {
  4897. if (Remote && !WinConfiguration->DisableOpenEdit)
  4898. {
  4899. ExecuteFile(osRemote, efShell);
  4900. AllowExec = false;
  4901. }
  4902. }
  4903. }
  4904. }
  4905. //---------------------------------------------------------------------------
  4906. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  4907. {
  4908. return ((Side == osRemote) || (Side == osCurrent));
  4909. }
  4910. //---------------------------------------------------------------------------
  4911. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  4912. {
  4913. DebugFail();
  4914. }
  4915. //---------------------------------------------------------------------------
  4916. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  4917. {
  4918. DebugFail();
  4919. }
  4920. //---------------------------------------------------------------------------
  4921. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  4922. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  4923. {
  4924. TSynchronizeParamType Params;
  4925. Params.LocalDirectory = LocalDirectory;
  4926. Params.RemoteDirectory = RemoteDirectory;
  4927. int UnusedParams =
  4928. (GUIConfiguration->SynchronizeParams &
  4929. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize));
  4930. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  4931. Params.Options = GUIConfiguration->SynchronizeOptions;
  4932. bool SaveSettings = false;
  4933. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  4934. &DoSynchronizeTooManyDirectories);
  4935. DebugAssert(FSynchronizeController == NULL);
  4936. FSynchronizeController = &Controller;
  4937. bool Result;
  4938. try
  4939. {
  4940. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4941. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  4942. int Options =
  4943. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  4944. DebugAssert(FOnFeedSynchronizeError == NULL);
  4945. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  4946. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  4947. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  4948. if (Result)
  4949. {
  4950. if (SaveSettings)
  4951. {
  4952. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  4953. GUIConfiguration->SynchronizeOptions = Params.Options;
  4954. }
  4955. else
  4956. {
  4957. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  4958. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  4959. FLAGSET(Params.Options, soSynchronize))
  4960. {
  4961. GUIConfiguration->SynchronizeOptions =
  4962. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  4963. soSynchronize;
  4964. }
  4965. }
  4966. LocalDirectory = Params.LocalDirectory;
  4967. RemoteDirectory = Params.RemoteDirectory;
  4968. }
  4969. }
  4970. __finally
  4971. {
  4972. FSynchronizeController = NULL;
  4973. DebugAssert(FOnFeedSynchronizeError == NULL);
  4974. FOnFeedSynchronizeError = NULL;
  4975. }
  4976. return Result;
  4977. }
  4978. //---------------------------------------------------------------------------
  4979. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  4980. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  4981. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  4982. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  4983. TSynchronizeOptions * Options, bool Full)
  4984. {
  4985. // Keep-up-to-date mode
  4986. if (Terminal->Status == ssOpened)
  4987. {
  4988. try
  4989. {
  4990. int PParams = Params.Params;
  4991. if (!Full)
  4992. {
  4993. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  4994. if (FLAGSET(Params.Options, soRecurse))
  4995. {
  4996. PParams |= TTerminal::spSubDirs;
  4997. }
  4998. }
  4999. else
  5000. {
  5001. // if keepuptodate is non-recursive,
  5002. // full sync before has to be non-recursive as well
  5003. if (FLAGCLEAR(Params.Options, soRecurse))
  5004. {
  5005. PParams |= TTerminal::spNoRecurse;
  5006. }
  5007. }
  5008. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5009. PParams, Checklist, Options);
  5010. }
  5011. catch (Exception & E)
  5012. {
  5013. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  5014. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5015. {
  5016. // noop, already logged in MoreMessageDialog
  5017. }
  5018. else
  5019. {
  5020. ShowExtendedExceptionEx(Terminal, &E);
  5021. throw;
  5022. }
  5023. }
  5024. }
  5025. }
  5026. //---------------------------------------------------------------------------
  5027. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5028. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5029. const UnicodeString ErrorStr)
  5030. {
  5031. if (!Directory.IsEmpty())
  5032. {
  5033. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5034. }
  5035. else
  5036. {
  5037. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5038. }
  5039. }
  5040. //---------------------------------------------------------------------------
  5041. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5042. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5043. {
  5044. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5045. {
  5046. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5047. }
  5048. else
  5049. {
  5050. TMessageParams Params(mpNeverAskAgainCheck);
  5051. unsigned int Result = MessageDialog(
  5052. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5053. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5054. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5055. {
  5056. MaxDirectories *= 2;
  5057. if (Result == qaNeverAskAgain)
  5058. {
  5059. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5060. }
  5061. }
  5062. else
  5063. {
  5064. Abort();
  5065. }
  5066. }
  5067. }
  5068. //---------------------------------------------------------------------------
  5069. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5070. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5071. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5072. TSynchronizeOptions * Options)
  5073. {
  5074. // Keep-up-to-date mode
  5075. DebugAssert(!FAutoOperation);
  5076. void * BatchStorage;
  5077. BatchStart(BatchStorage);
  5078. FAutoOperation = true;
  5079. bool AnyOperation = false;
  5080. TDateTime StartTime = Now();
  5081. TSynchronizeChecklist * AChecklist = NULL;
  5082. try
  5083. {
  5084. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5085. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5086. {
  5087. FSynchronizeProgressForm->Start();
  5088. }
  5089. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5090. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5091. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5092. Options);
  5093. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5094. SAFE_DESTROY(FSynchronizeProgressForm);
  5095. AnyOperation = (AChecklist->CheckedCount > 0);
  5096. if (AnyOperation)
  5097. {
  5098. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5099. CreateProgressForm(&SynchronizeProgress);
  5100. try
  5101. {
  5102. Terminal->SynchronizeApply(
  5103. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5104. SynchronizeProcessedItem, NULL, NULL, NULL);
  5105. }
  5106. __finally
  5107. {
  5108. DestroyProgressForm();
  5109. }
  5110. }
  5111. }
  5112. __finally
  5113. {
  5114. if (Checklist == NULL)
  5115. {
  5116. delete AChecklist;
  5117. }
  5118. else
  5119. {
  5120. *Checklist = AChecklist;
  5121. }
  5122. FAutoOperation = false;
  5123. SAFE_DESTROY(FSynchronizeProgressForm);
  5124. BatchEnd(BatchStorage);
  5125. ReloadLocalDirectory();
  5126. if (AnyOperation)
  5127. {
  5128. OperationComplete(StartTime);
  5129. }
  5130. }
  5131. }
  5132. //---------------------------------------------------------------------------
  5133. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5134. {
  5135. // can be called from command line
  5136. return Visible &&
  5137. ((DirView(osOther)->SelCount > 0) ||
  5138. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5139. }
  5140. //---------------------------------------------------------------------------
  5141. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5142. {
  5143. LogSynchronizeEvent(Terminal, Message);
  5144. }
  5145. //---------------------------------------------------------------------------
  5146. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5147. int Params, TSynchronizeOptions & Options)
  5148. {
  5149. DebugAssert(!IsLocalBrowserMode());
  5150. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5151. {
  5152. Options.Filter = new TStringList();
  5153. Options.Filter->CaseSensitive = false;
  5154. Options.Filter->Duplicates = Types::dupAccept;
  5155. if (DirView(osRemote)->SelCount > 0)
  5156. {
  5157. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5158. }
  5159. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5160. {
  5161. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5162. }
  5163. Options.Filter->Sort();
  5164. }
  5165. }
  5166. //---------------------------------------------------------------------------
  5167. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5168. {
  5169. TCopyParamType Defaults;
  5170. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5171. ConfigStorage->AccessMode = smRead;
  5172. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5173. {
  5174. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5175. }
  5176. ConfigStorage.reset(NULL);
  5177. std::unique_ptr<TStringList> Options(new TStringList());
  5178. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5179. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5180. UnicodeString Result;
  5181. if (Options->Count > 0)
  5182. {
  5183. Result =
  5184. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5185. }
  5186. return Result;
  5187. }
  5188. //---------------------------------------------------------------------------
  5189. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5190. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5191. {
  5192. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5193. UnicodeString AdditionalParams =
  5194. FORMAT(L"%s \"%s\" \"%s\" %d %d %s%s", (
  5195. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5196. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options,
  5197. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5198. SerializeCopyParamForCommandLine(CopyParams)));
  5199. ExecuteNewInstance(SessionName, AdditionalParams);
  5200. }
  5201. //---------------------------------------------------------------------------
  5202. struct TSynchronizeParams
  5203. {
  5204. UnicodeString LocalDirectory;
  5205. UnicodeString RemoteDirectory;
  5206. TSynchronizeMode Mode;
  5207. TSynchronizeChecklist * Checklist;
  5208. int Params;
  5209. TCopyParamType * CopyParam;
  5210. TDateTime CollectElapsed;
  5211. TDateTime * StartTime;
  5212. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5213. };
  5214. //---------------------------------------------------------------------------
  5215. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5216. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5217. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5218. {
  5219. DebugAssert(!FAutoOperation);
  5220. void * BatchStorage;
  5221. BatchStart(BatchStorage);
  5222. FAutoOperation = true;
  5223. TFileOperationStatistics Statistics;
  5224. TDateTime Start = Now();
  5225. try
  5226. {
  5227. Params.OnProcessedItem = OnProcessedItem;
  5228. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5229. CreateProgressForm(&SynchronizeProgress);
  5230. Terminal->SynchronizeApply(
  5231. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5232. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5233. &Statistics);
  5234. }
  5235. __finally
  5236. {
  5237. Params.OnProcessedItem = NULL;
  5238. FAutoOperation = false;
  5239. DestroyProgressForm();
  5240. BatchEnd(BatchStorage);
  5241. ReloadLocalDirectory();
  5242. }
  5243. if (WinConfiguration->SynchronizeSummary)
  5244. {
  5245. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5246. // The statistics should be 0 anyway in this case
  5247. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5248. {
  5249. Message += L"\n";
  5250. if (Statistics.FilesUploaded > 0)
  5251. {
  5252. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5253. }
  5254. if (Statistics.FilesDownloaded > 0)
  5255. {
  5256. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5257. }
  5258. if (Statistics.FilesDeletedLocal > 0)
  5259. {
  5260. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5261. }
  5262. if (Statistics.FilesDeletedRemote > 0)
  5263. {
  5264. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5265. }
  5266. }
  5267. TDateTime Elapsed = (Now() - Start);
  5268. Message +=
  5269. L"\n" +
  5270. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5271. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5272. TMessageParams Params(mpNeverAskAgainCheck);
  5273. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5274. if (Result == qaNeverAskAgain)
  5275. {
  5276. WinConfiguration->SynchronizeSummary = false;
  5277. }
  5278. }
  5279. }
  5280. //---------------------------------------------------------------------------
  5281. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5282. {
  5283. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5284. {
  5285. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5286. }
  5287. // Not set in keep-up-to-date mode - Synchronize() method
  5288. if (Token != NULL)
  5289. {
  5290. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5291. if (Params.OnProcessedItem != NULL)
  5292. {
  5293. Params.OnProcessedItem(NULL, ChecklistItem);
  5294. }
  5295. }
  5296. }
  5297. //---------------------------------------------------------------------------
  5298. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5299. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5300. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5301. {
  5302. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5303. *Params.StartTime = Now();
  5304. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5305. }
  5306. //---------------------------------------------------------------------------
  5307. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5308. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5309. {
  5310. // terminal can be already closed (e.g. dropped connection)
  5311. if (Terminal != NULL)
  5312. {
  5313. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5314. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5315. }
  5316. }
  5317. //---------------------------------------------------------------------------
  5318. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5319. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5320. {
  5321. DebugAssert(!IsLocalBrowserMode());
  5322. TAutoBatch AutoBatch(this);
  5323. TAutoFlag AutoOperationFlag(FAutoOperation);
  5324. if (Side == osRemote)
  5325. {
  5326. std::unique_ptr<TStrings> FileList(new TStringList());
  5327. FileList->AddObject(FileName, RemoteFile);
  5328. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5329. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5330. RemoteDirView->SaveSelection();
  5331. RemoteDirView->SaveSelectedNames();
  5332. try
  5333. {
  5334. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5335. }
  5336. catch(...)
  5337. {
  5338. RemoteDirView->DiscardSavedSelection();
  5339. throw;
  5340. }
  5341. RemoteDirView->RestoreSelection();
  5342. }
  5343. else if (DebugAlwaysTrue(Side == osLocal))
  5344. {
  5345. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5346. {
  5347. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5348. }
  5349. UnicodeString Directory = ExtractFileDir(FileName);
  5350. ReloadLocalDirectory(Directory);
  5351. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5352. if (!SamePaths(Directory, NewDirectory))
  5353. {
  5354. ReloadLocalDirectory(NewDirectory);
  5355. }
  5356. }
  5357. }
  5358. //---------------------------------------------------------------------------
  5359. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5360. {
  5361. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5362. if (Side == osLocal)
  5363. {
  5364. if (Action == TSynchronizeChecklist::saDownloadNew)
  5365. {
  5366. OpenFolderInExplorer(LocalPath);
  5367. }
  5368. else
  5369. {
  5370. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5371. }
  5372. }
  5373. else if (DebugAlwaysTrue(Side == osRemote))
  5374. {
  5375. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5376. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5377. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5378. if (!LocalPath.IsEmpty())
  5379. {
  5380. SessionData->LocalDirectory = LocalPath;
  5381. }
  5382. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5383. ExecuteNewInstance(SessionName, FORMAT(L"%s=%s", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5384. }
  5385. }
  5386. //---------------------------------------------------------------------------
  5387. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5388. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5389. const TCopyParamType * CopyParams)
  5390. {
  5391. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5392. UnicodeString AdditionalParams =
  5393. FORMAT(L"%s \"%s\" \"%s\" %d %d %s%s", (
  5394. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5395. LocalDirectory, RemoteDirectory, Mode, Params,
  5396. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5397. SerializeCopyParamForCommandLine(CopyParams)));
  5398. ExecuteNewInstance(SessionName, AdditionalParams);
  5399. }
  5400. //---------------------------------------------------------------------------
  5401. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5402. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5403. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5404. {
  5405. int Result;
  5406. bool SaveSettings = false;
  5407. int Options =
  5408. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5409. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5410. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5411. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5412. bool Continue =
  5413. (UseDefaults == 0) ||
  5414. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5415. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5416. if (Continue)
  5417. {
  5418. Configuration->Usage->Inc(L"Synchronizations");
  5419. UpdateCopyParamCounters(CopyParam);
  5420. TSynchronizeOptions SynchronizeOptions;
  5421. GetSynchronizeOptions(Params, SynchronizeOptions);
  5422. if (SaveSettings)
  5423. {
  5424. GUIConfiguration->SynchronizeParams = Params;
  5425. }
  5426. else
  5427. {
  5428. SaveMode = false;
  5429. }
  5430. TDateTime StartTime = Now();
  5431. TSynchronizeChecklist * Checklist = NULL;
  5432. try
  5433. {
  5434. DebugAssert(!FAutoOperation);
  5435. FAutoOperation = true;
  5436. try
  5437. {
  5438. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5439. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5440. int Files = -1;
  5441. if (DataList->Count >= 1)
  5442. {
  5443. Files = StrToIntDef(DataList->Strings[0], Files);
  5444. }
  5445. else
  5446. {
  5447. DataList->Add(UnicodeString());
  5448. }
  5449. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5450. FSynchronizeProgressForm->Start();
  5451. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5452. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5453. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5454. &SynchronizeOptions);
  5455. if (Terminal->SessionData->CacheDirectories)
  5456. {
  5457. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5458. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5459. }
  5460. }
  5461. __finally
  5462. {
  5463. FAutoOperation = false;
  5464. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5465. SAFE_DESTROY(FSynchronizeProgressForm);
  5466. }
  5467. TSynchronizeParams SynchronizeParams;
  5468. SynchronizeParams.LocalDirectory = LocalDirectory;
  5469. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5470. SynchronizeParams.Mode = Mode;
  5471. SynchronizeParams.CopyParam = &CopyParam;
  5472. SynchronizeParams.Params = Params;
  5473. SynchronizeParams.Checklist = Checklist;
  5474. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5475. SynchronizeParams.StartTime = &StartTime;
  5476. SynchronizeParams.OnProcessedItem = NULL;
  5477. Result = Checklist->Count;
  5478. if (Checklist->Count > 0)
  5479. {
  5480. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5481. {
  5482. if (!DoSynchronizeChecklistDialog(
  5483. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5484. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  5485. {
  5486. Result = -1;
  5487. }
  5488. }
  5489. else
  5490. {
  5491. FullSynchronize(SynchronizeParams, NULL, NULL);
  5492. }
  5493. }
  5494. else
  5495. {
  5496. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5497. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5498. }
  5499. }
  5500. __finally
  5501. {
  5502. delete Checklist;
  5503. }
  5504. OperationComplete(StartTime);
  5505. }
  5506. else
  5507. {
  5508. Result = -1;
  5509. }
  5510. return Result;
  5511. }
  5512. //---------------------------------------------------------------------------
  5513. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5514. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5515. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5516. {
  5517. if (Collect)
  5518. {
  5519. DebugAssert(FSynchronizeProgressForm != NULL);
  5520. if (!FSynchronizeProgressForm->Started)
  5521. {
  5522. FSynchronizeProgressForm->Start();
  5523. }
  5524. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5525. SetTaskbarListProgressValue(CompareProgress);
  5526. }
  5527. else
  5528. {
  5529. DebugAssert(FSynchronizeProgressForm == NULL);
  5530. DebugAssert(FProgressForm != NULL);
  5531. }
  5532. }
  5533. //---------------------------------------------------------------------------
  5534. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5535. {
  5536. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  5537. TRemoteFile * File = NULL;
  5538. Terminal->ReadFile(FullFileName, File);
  5539. if (File != NULL)
  5540. {
  5541. std::unique_ptr<TRemoteFile> FileOwner(File);
  5542. TAutoFlag Flag(FStandaloneEditing);
  5543. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5544. Application->ShowMainForm = false;
  5545. Application->Run();
  5546. }
  5547. }
  5548. //---------------------------------------------------------------------------
  5549. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  5550. {
  5551. DebugFail();
  5552. }
  5553. //---------------------------------------------------------------------------
  5554. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5555. {
  5556. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5557. SessionData->Assign(Terminal->SessionData);
  5558. UpdateSessionData(SessionData.get());
  5559. Terminal->UpdateSessionCredentials(SessionData.get());
  5560. if (!Terminal->SessionData->HasSessionName())
  5561. {
  5562. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5563. // once IsWorkspace is cleared
  5564. SessionData->Name = UnicodeString();
  5565. SessionData->IsWorkspace = false;
  5566. }
  5567. return SessionData.release();
  5568. }
  5569. //---------------------------------------------------------------------------
  5570. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5571. {
  5572. TSessionData * SessionData = CloneCurrentSessionData();
  5573. try
  5574. {
  5575. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5576. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5577. }
  5578. __finally
  5579. {
  5580. delete SessionData;
  5581. }
  5582. }
  5583. //---------------------------------------------------------------------------
  5584. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5585. {
  5586. TTerminalManager * Manager = TTerminalManager::Instance();
  5587. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5588. if (DebugAlwaysTrue(Terminal != NULL))
  5589. {
  5590. // Update (Managed)Terminal->StateData
  5591. UpdateTerminal(Terminal);
  5592. }
  5593. Manager->SaveWorkspace(DataList.get());
  5594. return DataList.release();
  5595. }
  5596. //---------------------------------------------------------------------------
  5597. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5598. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5599. {
  5600. WinConfiguration->LastWorkspace = Name;
  5601. if (!SavePasswords)
  5602. {
  5603. for (int Index = 0; Index < DataList->Count; Index++)
  5604. {
  5605. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5606. if (SessionData->Link.IsEmpty())
  5607. {
  5608. SessionData->ClearSessionPasswords();
  5609. }
  5610. }
  5611. }
  5612. StoredSessions->NewWorkspace(Name, DataList);
  5613. // modified only
  5614. StoredSessions->Save(false, Explicit);
  5615. }
  5616. //---------------------------------------------------------------------------
  5617. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5618. {
  5619. return
  5620. DefaultStr(WinConfiguration->LastWorkspace,
  5621. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5622. }
  5623. //---------------------------------------------------------------------------
  5624. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5625. {
  5626. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5627. bool AnyNonStoredSessionWithPassword = false;
  5628. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5629. bool AllNonStoredSessionsAnonymous = true;
  5630. TTerminalManager * Manager = TTerminalManager::Instance();
  5631. for (int Index = 0; Index < DataList->Count; Index++)
  5632. {
  5633. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5634. if (SessionData->Link.IsEmpty())
  5635. {
  5636. if (SessionData->HasAnySessionPassword())
  5637. {
  5638. AnyNonStoredSessionWithPassword = true;
  5639. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  5640. {
  5641. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5642. }
  5643. }
  5644. if (!SameText(SessionData->UserName, AnonymousUserName))
  5645. {
  5646. AllNonStoredSessionsAnonymous = false;
  5647. }
  5648. }
  5649. }
  5650. bool SavePasswords;
  5651. bool * PSavePasswords;
  5652. bool NotRecommendedSavingPasswords =
  5653. !CustomWinConfiguration->UseMasterPassword &&
  5654. !AllNonStoredSessionsAnonymous;
  5655. if (Configuration->DisablePasswordStoring ||
  5656. !AnyNonStoredSessionWithPassword)
  5657. {
  5658. PSavePasswords = NULL;
  5659. SavePasswords = false;
  5660. }
  5661. else
  5662. {
  5663. PSavePasswords = &SavePasswords;
  5664. SavePasswords =
  5665. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5666. !NotRecommendedSavingPasswords;
  5667. }
  5668. UnicodeString Name = WorkspaceName();
  5669. bool CreateShortcut = false;
  5670. bool Result =
  5671. DoSaveWorkspaceDialog(
  5672. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5673. EnableAutoSave);
  5674. if (Result)
  5675. {
  5676. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5677. if (CreateShortcut)
  5678. {
  5679. TOperationVisualizer Visualizer;
  5680. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5681. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5682. }
  5683. if (EnableAutoSave)
  5684. {
  5685. WinConfiguration->AutoSaveWorkspace = true;
  5686. WinConfiguration->AutoWorkspace = Name;
  5687. if (PSavePasswords != NULL)
  5688. {
  5689. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5690. }
  5691. }
  5692. }
  5693. return Result;
  5694. }
  5695. //---------------------------------------------------------------------------
  5696. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TManagedTerminal * Terminal)
  5697. {
  5698. DebugAssert(!IsLocalBrowserMode());
  5699. SAFE_DESTROY(Terminal->RemoteExplorerState);
  5700. if (WinConfiguration->PreservePanelState)
  5701. {
  5702. Terminal->RemoteExplorerState = RemoteDirView->SaveState();
  5703. }
  5704. UpdateSessionData(Terminal->StateData);
  5705. }
  5706. //---------------------------------------------------------------------------
  5707. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5708. {
  5709. // Keep in sync with TSessionData::CopyStateData
  5710. DebugAssert(!IsLocalBrowserMode());
  5711. DebugAssert(Data != NULL);
  5712. // This is inconsistent with how color (for example) is handled.
  5713. Data->Name = Terminal->SessionData->Name;
  5714. // cannot use RemoteDirView->Path, because it is empty if connection
  5715. // was already closed
  5716. // also only peek, we may not be connected at all atm
  5717. // so make sure we do not try retrieving current directory from the server
  5718. // (particularly with FTP)
  5719. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5720. if (!ACurrentDirectory.IsEmpty())
  5721. {
  5722. Data->RemoteDirectory = ACurrentDirectory;
  5723. }
  5724. Data->Color = SessionColor;
  5725. }
  5726. //---------------------------------------------------------------------------
  5727. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5728. {
  5729. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5730. DebugAssert(Toolbar != NULL);
  5731. for (int i = 0; i < Toolbar->Items->Count; i++)
  5732. {
  5733. TTBXCustomDropDownItem * DropDownItem;
  5734. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5735. if (DropDownItem != NULL)
  5736. {
  5737. ToolbarItemResize(DropDownItem,
  5738. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5739. Toolbar->NonClientWidth);
  5740. break;
  5741. }
  5742. }
  5743. }
  5744. //---------------------------------------------------------------------------
  5745. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5746. {
  5747. Item->EditWidth = Width;
  5748. }
  5749. //---------------------------------------------------------------------------
  5750. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5751. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5752. {
  5753. for (int i = 0; i < Toolbar->Items->Count; i++)
  5754. {
  5755. TTBXCustomDropDownItem * DropDownItem;
  5756. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5757. if (DropDownItem != NULL)
  5758. {
  5759. ASize.x -= DropDownItem->EditWidth;
  5760. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5761. }
  5762. }
  5763. }
  5764. //---------------------------------------------------------------------------
  5765. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5766. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5767. {
  5768. if (WinConfiguration->DDWarnLackOfTempSpace)
  5769. {
  5770. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5771. if (!ADrive.IsEmpty())
  5772. {
  5773. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5774. DebugAssert(RequiredSpace >= 0);
  5775. __int64 RequiredWithReserve;
  5776. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5777. if (FreeSpace < RequiredWithReserve)
  5778. {
  5779. unsigned int Result;
  5780. TMessageParams Params(mpNeverAskAgainCheck);
  5781. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5782. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5783. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5784. if (Result == qaNeverAskAgain)
  5785. {
  5786. WinConfiguration->DDWarnLackOfTempSpace = false;
  5787. }
  5788. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5789. }
  5790. }
  5791. }
  5792. }
  5793. //---------------------------------------------------------------------------
  5794. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5795. {
  5796. DoOpenDirectoryDialog(odAddBookmark, Side);
  5797. }
  5798. //---------------------------------------------------------------------------
  5799. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5800. {
  5801. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5802. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  5803. try
  5804. {
  5805. TCustomDirView * DView = DirView(Side);
  5806. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5807. {
  5808. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5809. }
  5810. }
  5811. catch (...)
  5812. {
  5813. delete VisitedDirectories;
  5814. throw;
  5815. }
  5816. return VisitedDirectories;
  5817. }
  5818. //---------------------------------------------------------------------------
  5819. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5820. TOpenDirectoryMode Mode, TOperationSide Side)
  5821. {
  5822. bool Continue = true;
  5823. if (Mode == odAddBookmark)
  5824. {
  5825. TMessageParams Params(mpNeverAskAgainCheck);
  5826. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5827. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5828. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  5829. unsigned int Answer =
  5830. MessageDialog(Message,
  5831. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  5832. if (Answer == qaNeverAskAgain)
  5833. {
  5834. Continue = true;
  5835. WinConfiguration->UseSharedBookmarks = true;
  5836. }
  5837. else if (Answer == qaOK)
  5838. {
  5839. Continue = true;
  5840. WinConfiguration->UseSharedBookmarks = false;
  5841. }
  5842. else
  5843. {
  5844. Continue = false;
  5845. }
  5846. }
  5847. if (Continue)
  5848. {
  5849. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  5850. try
  5851. {
  5852. UnicodeString Name = DirView(Side)->PathName;
  5853. bool Repeat;
  5854. do
  5855. {
  5856. Repeat = false;
  5857. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  5858. // do not allow switching to location profiles,
  5859. // if we are not connected
  5860. HasDirView[osLocal] && (Terminal != NULL)))
  5861. {
  5862. TWindowLock Lock(this);
  5863. if (!TryOpenDirectory(Side, Name))
  5864. {
  5865. Repeat = true;
  5866. Mode = odBrowse;
  5867. }
  5868. }
  5869. }
  5870. while (Repeat);
  5871. }
  5872. __finally
  5873. {
  5874. delete VisitedDirectories;
  5875. }
  5876. }
  5877. }
  5878. //---------------------------------------------------------------------------
  5879. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  5880. {
  5881. DebugAssert(!FTerminal->CommandSessionOpened);
  5882. return TTerminalManager::Instance()->ConnectTerminal(FTerminal->CommandSession);
  5883. }
  5884. //---------------------------------------------------------------------------
  5885. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  5886. {
  5887. bool Result = FTerminal->IsCapable[Capability] ||
  5888. FTerminal->CommandSessionOpened;
  5889. if (!Result)
  5890. {
  5891. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  5892. if (!GUIConfiguration->ConfirmCommandSession)
  5893. {
  5894. Result = true;
  5895. }
  5896. else
  5897. {
  5898. TMessageParams Params(mpNeverAskAgainCheck);
  5899. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  5900. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  5901. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  5902. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  5903. if (Answer == qaNeverAskAgain)
  5904. {
  5905. GUIConfiguration->ConfirmCommandSession = false;
  5906. Result = true;
  5907. }
  5908. else if (Answer == qaOK)
  5909. {
  5910. Result = true;
  5911. }
  5912. }
  5913. if (Result)
  5914. {
  5915. Result = CommandSessionFallback();
  5916. }
  5917. }
  5918. return Result;
  5919. }
  5920. //---------------------------------------------------------------------------
  5921. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  5922. {
  5923. if (EnsureCommandSessionFallback(fcAnyCommand))
  5924. {
  5925. DoConsoleDialog(Terminal, Command);
  5926. }
  5927. }
  5928. //---------------------------------------------------------------------------
  5929. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  5930. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  5931. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  5932. {
  5933. if (!IsSideLocalBrowser(osRemote) &&
  5934. IsFileControl(DropSourceControl, osRemote) &&
  5935. (FDDExtMapFile != NULL))
  5936. {
  5937. Accept = true;
  5938. }
  5939. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  5940. DebugAssert(FDDTargetControl != NULL);
  5941. }
  5942. //---------------------------------------------------------------------------
  5943. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  5944. _di_IDataObject DataObj, int KeyState,
  5945. const TPoint & Point, int & Effect, bool & Accept)
  5946. {
  5947. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  5948. }
  5949. //---------------------------------------------------------------------------
  5950. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  5951. _di_IDataObject DataObj, int KeyState,
  5952. const TPoint & Point, int & Effect, bool & Accept)
  5953. {
  5954. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  5955. }
  5956. //---------------------------------------------------------------------------
  5957. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  5958. TObject * Sender)
  5959. {
  5960. DebugUsedParam(Sender);
  5961. DebugAssert(FDDTargetControl == Sender);
  5962. FDDTargetControl = NULL;
  5963. }
  5964. //---------------------------------------------------------------------------
  5965. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  5966. {
  5967. FileControlDDDragLeave(SessionsPageControl);
  5968. }
  5969. //---------------------------------------------------------------------------
  5970. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  5971. {
  5972. FileControlDDDragLeave(QueueView3);
  5973. }
  5974. //---------------------------------------------------------------------------
  5975. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  5976. {
  5977. DebugAssert(!IsSideLocalBrowser(Side));
  5978. DebugUsedParam(Side);
  5979. bool Edit = false;
  5980. TRemoteFile * File = NULL;
  5981. UnicodeString FileName;
  5982. UnicodeString PointTo;
  5983. bool SymbolicLink = true;
  5984. if (RemoteDirView->ItemFocused)
  5985. {
  5986. DebugAssert(RemoteDirView->ItemFocused->Data);
  5987. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  5988. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  5989. if (Edit)
  5990. {
  5991. FileName = File->FileName;
  5992. PointTo = File->LinkTo;
  5993. }
  5994. else
  5995. {
  5996. PointTo = File->FileName;
  5997. }
  5998. }
  5999. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6000. Terminal->IsCapable[fcHardLink]))
  6001. {
  6002. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6003. if (Edit)
  6004. {
  6005. DebugAssert(File->FileName == FileName);
  6006. int Params = dfNoRecursive;
  6007. Terminal->ExceptionOnFail = true;
  6008. try
  6009. {
  6010. Terminal->DeleteFile(L"", File, &Params);
  6011. }
  6012. __finally
  6013. {
  6014. Terminal->ExceptionOnFail = false;
  6015. }
  6016. }
  6017. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6018. }
  6019. }
  6020. //---------------------------------------------------------------------------
  6021. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6022. {
  6023. return
  6024. (IsSideLocalBrowser(Side) ||
  6025. ((Terminal != NULL) &&
  6026. Terminal->ResolvingSymlinks &&
  6027. Terminal->IsCapable[fcSymbolicLink]));
  6028. }
  6029. //---------------------------------------------------------------------------
  6030. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6031. {
  6032. return
  6033. !IsSideLocalBrowser(FCurrentSide) &&
  6034. (RemoteDirView->ItemFocused != NULL) &&
  6035. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6036. Terminal->SessionData->ResolveSymlinks;
  6037. }
  6038. //---------------------------------------------------------------------------
  6039. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6040. {
  6041. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6042. FForceExecution = true;
  6043. try
  6044. {
  6045. DirView(osCurrent)->ExecuteCurrentFile();
  6046. }
  6047. __finally
  6048. {
  6049. FForceExecution = false;
  6050. }
  6051. }
  6052. //---------------------------------------------------------------------------
  6053. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6054. {
  6055. TEditorData ExternalEditor;
  6056. ExternalEditor.Editor = edExternal;
  6057. bool Remember = false;
  6058. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6059. {
  6060. if (Remember)
  6061. {
  6062. TEditorList * EditorList = new TEditorList;
  6063. try
  6064. {
  6065. *EditorList = *WinConfiguration->EditorList;
  6066. bool Found = false;
  6067. int i = 0;
  6068. while (!Found && (i < EditorList->Count))
  6069. {
  6070. const TEditorPreferences * Editor = EditorList->Editors[i];
  6071. if ((Editor->Data->Editor == edExternal) &&
  6072. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6073. {
  6074. Found = true;
  6075. }
  6076. i++;
  6077. }
  6078. if (!Found)
  6079. {
  6080. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6081. WinConfiguration->EditorList = EditorList;
  6082. }
  6083. }
  6084. __finally
  6085. {
  6086. delete EditorList;
  6087. }
  6088. }
  6089. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6090. }
  6091. }
  6092. //---------------------------------------------------------------------------
  6093. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6094. {
  6095. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  6096. if (FFileFindTerminal == Sender)
  6097. {
  6098. FFileFindTerminal = NULL;
  6099. HideFileFindDialog();
  6100. }
  6101. if (FClipboardTerminal == Sender)
  6102. {
  6103. ClipboardClear(); // implies ClipboardStop
  6104. }
  6105. }
  6106. //---------------------------------------------------------------------------
  6107. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6108. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6109. {
  6110. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6111. DebugAssert(Terminal != NULL);
  6112. if (Data->Terminal == Terminal)
  6113. {
  6114. Data->Terminal = NULL;
  6115. }
  6116. }
  6117. //---------------------------------------------------------------------------
  6118. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  6119. {
  6120. UpdateControls();
  6121. SessionColor = TColor(0);
  6122. UpdateRemotePathComboBox(false);
  6123. NeedSession(false);
  6124. }
  6125. //---------------------------------------------------------------------------
  6126. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6127. {
  6128. try
  6129. {
  6130. // Cache, as the login dialog can change its value
  6131. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6132. try
  6133. {
  6134. if (ShowLogin)
  6135. {
  6136. bool ReloadSessions = !Startup;
  6137. TTerminalManager::Instance()->NewSession(false, L"", ReloadSessions, this);
  6138. }
  6139. else if (Startup && WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  6140. // This detects if workspace was saved the last time the main widow was closed
  6141. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  6142. {
  6143. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false);
  6144. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  6145. }
  6146. }
  6147. __finally
  6148. {
  6149. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6150. // (so there was no chance for the user to open any session yet)
  6151. if ((ShowLogin || !Startup) &&
  6152. !WinConfiguration->KeepOpenWhenNoSession &&
  6153. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6154. {
  6155. TerminateApplication();
  6156. }
  6157. }
  6158. }
  6159. catch (EAbort &)
  6160. {
  6161. // swallow
  6162. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6163. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6164. }
  6165. }
  6166. //---------------------------------------------------------------------------
  6167. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged()
  6168. {
  6169. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  6170. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6171. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  6172. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6173. try
  6174. {
  6175. while ((SessionsPageControl->PageCount > TerminalList->Count + 1) ||
  6176. // Clear the design time unthemed tab
  6177. ((SessionsPageControl->PageCount > 0) &&
  6178. (dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[SessionsPageControl->PageCount - 1]) == NULL)))
  6179. {
  6180. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6181. }
  6182. for (int Index = 0; Index <= TerminalList->Count; Index++)
  6183. {
  6184. TThemeTabSheet * TabSheet;
  6185. if (Index >= SessionsPageControl->PageCount)
  6186. {
  6187. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6188. TabSheet->PageControl = SessionsPageControl;
  6189. }
  6190. else
  6191. {
  6192. TabSheet = DebugNotNull(dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[Index]));
  6193. }
  6194. bool IsSessionTab = (Index < TerminalList->Count);
  6195. if (IsSessionTab)
  6196. {
  6197. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  6198. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6199. UpdateSessionTab(TabSheet);
  6200. }
  6201. else
  6202. {
  6203. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  6204. TabSheet->Tag = 0; // not really needed
  6205. TabSheet->Shadowed = false;
  6206. // 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)
  6207. UpdateNewSessionTab();
  6208. }
  6209. TabSheet->ShowCloseButton = IsSessionTab;
  6210. }
  6211. }
  6212. __finally
  6213. {
  6214. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6215. }
  6216. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  6217. }
  6218. //---------------------------------------------------------------------------
  6219. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  6220. {
  6221. DoTerminalListChanged();
  6222. }
  6223. //---------------------------------------------------------------------------
  6224. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  6225. {
  6226. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6227. DebugAssert(TabSheet->ImageIndex == 0);
  6228. TabSheet->Caption =
  6229. WinConfiguration->SelectiveToolbarText ?
  6230. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  6231. UnicodeString();
  6232. }
  6233. //---------------------------------------------------------------------------
  6234. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  6235. {
  6236. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6237. }
  6238. //---------------------------------------------------------------------------
  6239. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  6240. {
  6241. if (DebugAlwaysTrue(TabSheet != NULL))
  6242. {
  6243. TManagedTerminal * ATerminal = GetSessionTabTerminal(TabSheet);
  6244. if (DebugAlwaysTrue(ATerminal != NULL))
  6245. {
  6246. TColor Color = (ATerminal == FTerminal) ? FSessionColor : ATerminal->StateData->Color;
  6247. TabSheet->ImageIndex = AddSessionColor(Color);
  6248. UnicodeString TabCaption = TTerminalManager::Instance()->GetTerminalTitle(ATerminal, true);
  6249. TabSheet->Caption = SessionsPageControl->FormatCaptionWithCloseButton(TabCaption);
  6250. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  6251. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  6252. {
  6253. ThemeTabSheet->Shadowed = !ATerminal->Active;
  6254. }
  6255. }
  6256. }
  6257. }
  6258. //---------------------------------------------------------------------------
  6259. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6260. {
  6261. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6262. TManagedTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  6263. bool Result = (Terminal != NULL);
  6264. if (Result)
  6265. {
  6266. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6267. }
  6268. else
  6269. {
  6270. try
  6271. {
  6272. NewSession(false);
  6273. }
  6274. __finally
  6275. {
  6276. DoTerminalListChanged();
  6277. }
  6278. FSessionsPageControlNewSessionTime = Now();
  6279. }
  6280. return Result;
  6281. }
  6282. //---------------------------------------------------------------------------
  6283. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6284. {
  6285. SessionTabSwitched();
  6286. }
  6287. //---------------------------------------------------------------------------
  6288. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6289. {
  6290. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6291. DebugAssert(TransferList != NULL);
  6292. UnicodeString Name;
  6293. if (TransferList->ItemIndex <= 0)
  6294. {
  6295. Name = L"";
  6296. }
  6297. else
  6298. {
  6299. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6300. }
  6301. if (FCopyParamAutoSelected.IsEmpty())
  6302. {
  6303. // if previous preset was not autoselected, make new preset the "default"
  6304. FCopyParamDefault = Name;
  6305. }
  6306. GUIConfiguration->CopyParamCurrent = Name;
  6307. }
  6308. //---------------------------------------------------------------------------
  6309. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6310. {
  6311. // sanity check
  6312. bool ExistingPreset =
  6313. (FTransferListHoverIndex >= 0) &&
  6314. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6315. DebugAssert(ExistingPreset);
  6316. if (ExistingPreset)
  6317. {
  6318. HDC DC = GetDC(0);
  6319. TCanvas * Canvas = new TCanvas();
  6320. try
  6321. {
  6322. Canvas->Handle = DC;
  6323. Canvas->Font = GetToolbarFont(this);
  6324. UnicodeString Name;
  6325. if (FTransferListHoverIndex == 0)
  6326. {
  6327. Name = L"";
  6328. }
  6329. else
  6330. {
  6331. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6332. }
  6333. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6334. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6335. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6336. static_cast<TObject*>(GetComponent(fcTransferList)));
  6337. UnicodeString InfoStr =
  6338. GUIConfiguration->CopyParamPreset[Name].
  6339. GetInfoStr(L"; ",
  6340. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General));
  6341. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6342. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6343. {
  6344. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6345. {
  6346. InfoStr.SetLength(InfoStr.Length() - 1);
  6347. }
  6348. InfoStr += Ellipsis;
  6349. }
  6350. // UpdateCaption does not cause invalidation of whole submenu, while
  6351. // setting Caption property does.
  6352. // also it probably does not resize the label, even if necessary
  6353. // (we do not want that anyway)
  6354. TransferLabel->UpdateCaption(InfoStr);
  6355. }
  6356. __finally
  6357. {
  6358. Canvas->Handle = NULL;
  6359. ReleaseDC(0, DC);
  6360. delete Canvas;
  6361. }
  6362. }
  6363. }
  6364. //---------------------------------------------------------------------------
  6365. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6366. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6367. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6368. {
  6369. if (FTransferListHoverIndex != AHoverIndex)
  6370. {
  6371. FTransferListHoverIndex = AHoverIndex;
  6372. UpdateTransferLabel();
  6373. }
  6374. }
  6375. //---------------------------------------------------------------------------
  6376. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6377. {
  6378. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6379. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6380. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6381. {
  6382. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6383. {
  6384. OpenDirectory(GetSide(osCurrent));
  6385. Message.Result = 1;
  6386. }
  6387. else
  6388. {
  6389. TForm::Dispatch(&Message);
  6390. }
  6391. }
  6392. else
  6393. {
  6394. TForm::Dispatch(&Message);
  6395. }
  6396. }
  6397. //---------------------------------------------------------------------------
  6398. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6399. {
  6400. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6401. if ((FIgnoreNextDialogChar != 0) &&
  6402. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6403. {
  6404. Message.Result = 1;
  6405. }
  6406. else
  6407. {
  6408. TForm::Dispatch(&Message);
  6409. }
  6410. FIgnoreNextDialogChar = 0;
  6411. }
  6412. //---------------------------------------------------------------------------
  6413. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6414. {
  6415. // The four low-order bits are used internally by Windows
  6416. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6417. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6418. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6419. // 61730 - restore thru double click - undocumented
  6420. // 61490 - maximize thru double click - undocumented
  6421. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6422. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6423. {
  6424. SysResizing(Cmd);
  6425. }
  6426. TForm::Dispatch(&Message);
  6427. }
  6428. //---------------------------------------------------------------------------
  6429. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6430. {
  6431. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6432. // Also there no point returning TRUE as we are not able to
  6433. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6434. // Hence the process termination might be safer :)
  6435. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6436. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6437. {
  6438. Message.Result = FALSE;
  6439. }
  6440. else
  6441. {
  6442. Message.Result = TRUE;
  6443. }
  6444. // Do not call default handling as that triggers OnCloseQuery,
  6445. // where our implementation will popup confirmation dialogs, what we do not want,
  6446. // as per Vista guidelines:
  6447. // https://docs.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6448. }
  6449. //---------------------------------------------------------------------------
  6450. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6451. {
  6452. if (Message.EndSession && IsApplicationMinimized())
  6453. {
  6454. // WORKAROUND
  6455. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6456. // when it receives WM_ENDSESSION,
  6457. // but that sometimes (particularly when application is minimized) cause crashes.
  6458. // Crash popups message that blocks log off.
  6459. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6460. // so we call ExitProcess() immediately, not even trying to cleanup.
  6461. // It still causes beep, so there's likely some popup, but it does not block
  6462. // log off.
  6463. ExitProcess(0);
  6464. }
  6465. TForm::Dispatch(&Message);
  6466. }
  6467. #ifdef _DEBUG
  6468. //---------------------------------------------------------------------------
  6469. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6470. {
  6471. TForm::Dispatch(&Message);
  6472. }
  6473. #endif
  6474. //---------------------------------------------------------------------------
  6475. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6476. {
  6477. }
  6478. //---------------------------------------------------------------------------
  6479. void __fastcall TCustomScpExplorerForm::DoShow()
  6480. {
  6481. // only now are the controls resized finally, so the size constraints
  6482. // will not conflict with possibly very small window size
  6483. RestoreFormParams();
  6484. // This indicates that the form was rescaled after parameters were loaded and
  6485. // as most probably the parameters were actually saved with the current form scaling
  6486. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6487. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6488. // and no rounding problems will occur.
  6489. // See also RestoreForm.
  6490. // (we should be safe to call RestoreParams unconditionally)
  6491. if (PixelsPerInch != Screen->PixelsPerInch)
  6492. {
  6493. RestoreParams();
  6494. }
  6495. FixControlsPlacement();
  6496. if (Position == poDefaultPosOnly)
  6497. {
  6498. CutFormToDesktop(this);
  6499. }
  6500. TForm::DoShow();
  6501. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6502. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6503. if (Terminal == NULL)
  6504. {
  6505. StartingDisconnected();
  6506. }
  6507. UpdatePixelsPerInchMainWindowCounter();
  6508. FShowing = true;
  6509. }
  6510. //---------------------------------------------------------------------------
  6511. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6512. {
  6513. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6514. }
  6515. //---------------------------------------------------------------------------
  6516. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  6517. {
  6518. DoTerminalListChanged();
  6519. }
  6520. //---------------------------------------------------------------------------
  6521. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6522. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6523. TNotifyEvent OnBalloonClick, TObject * UserData)
  6524. {
  6525. bool Do;
  6526. UnicodeString Message;
  6527. if (E == NULL)
  6528. {
  6529. Message = Str;
  6530. Do = true;
  6531. }
  6532. else
  6533. {
  6534. Do = ExceptionMessage(E, Message);
  6535. }
  6536. if (Do && WinConfiguration->BalloonNotifications)
  6537. {
  6538. UnicodeString Title;
  6539. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6540. {
  6541. Message = TrimLeft(Message);
  6542. }
  6543. else
  6544. {
  6545. Message = UnformatMessage(Message);
  6546. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6547. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6548. Title = LoadResourceString(Captions[Type]);
  6549. if (Terminal != NULL)
  6550. {
  6551. Title = FORMAT(L"%s - %s",
  6552. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), Title));
  6553. }
  6554. }
  6555. if (Seconds == 0)
  6556. {
  6557. Seconds = WinConfiguration->NotificationsTimeout;
  6558. }
  6559. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6560. }
  6561. }
  6562. //---------------------------------------------------------------------------
  6563. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6564. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6565. UnicodeString HelpKeyword, const TMessageParams * Params,
  6566. TTerminal * Terminal)
  6567. {
  6568. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6569. (FOnFeedSynchronizeError != NULL)) &&
  6570. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6571. {
  6572. if (FOnFeedSynchronizeError != NULL)
  6573. {
  6574. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6575. }
  6576. else
  6577. {
  6578. DebugAssert(FErrorList != NULL);
  6579. TStringList * MoreMessagesCopy = NULL;
  6580. if (MoreMessages)
  6581. {
  6582. MoreMessagesCopy = new TStringList();
  6583. MoreMessagesCopy->Assign(MoreMessages);
  6584. }
  6585. FErrorList->AddObject(Message, MoreMessagesCopy);
  6586. }
  6587. PopupTrayBalloon(Terminal, Message, Type);
  6588. return ContinueAnswer(Answers);
  6589. }
  6590. else
  6591. {
  6592. bool UseBalloon = FTrayIcon->Visible;
  6593. if (UseBalloon)
  6594. {
  6595. PopupTrayBalloon(Terminal, Message, Type);
  6596. }
  6597. unsigned int Result;
  6598. try
  6599. {
  6600. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6601. }
  6602. __finally
  6603. {
  6604. // cancel only balloon we popped up, otherwise we may cancel notification
  6605. // balloon that was there before the message dialog
  6606. // (such as "dd confirmation opt in balloon)
  6607. if (UseBalloon)
  6608. {
  6609. FTrayIcon->CancelBalloon();
  6610. }
  6611. }
  6612. return Result;
  6613. }
  6614. }
  6615. //---------------------------------------------------------------------------
  6616. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6617. TTerminal * Terminal, Exception * E)
  6618. {
  6619. if (FTrayIcon->Visible)
  6620. {
  6621. PopupTrayBalloon(Terminal, L"", qtError, E);
  6622. }
  6623. // particularly prevent opening new session from jump list,
  6624. // while exception is shown
  6625. NonVisualDataModule->StartBusy();
  6626. try
  6627. {
  6628. ShowExtendedExceptionEx(Terminal, E);
  6629. }
  6630. __finally
  6631. {
  6632. NonVisualDataModule->EndBusy();
  6633. FTrayIcon->CancelBalloon();
  6634. }
  6635. }
  6636. //---------------------------------------------------------------------------
  6637. void __fastcall TCustomScpExplorerForm::TerminalReady()
  6638. {
  6639. if (Terminal->Active)
  6640. {
  6641. InitStatusBar();
  6642. }
  6643. // cannot rely on active page being page for active terminal,
  6644. // as it can happen that active page is the "new session" page
  6645. // (e.g. when reconnecting active terminal, while login dialog
  6646. // invoked from "new session" page is modal)
  6647. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6648. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  6649. }
  6650. //---------------------------------------------------------------------------
  6651. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6652. TTerminal * Terminal, Exception * E)
  6653. {
  6654. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6655. if (!Terminal->Active)
  6656. {
  6657. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6658. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6659. {
  6660. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6661. }
  6662. }
  6663. }
  6664. //---------------------------------------------------------------------------
  6665. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6666. UnicodeString Message, TQueryType Type,
  6667. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6668. {
  6669. if ((E == NULL) ||
  6670. ExceptionMessage(E, Message))
  6671. {
  6672. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6673. if (Important)
  6674. {
  6675. Seconds *= 5;
  6676. }
  6677. UnicodeString NoteMessage(UnformatMessage(Message));
  6678. if (Terminal != NULL)
  6679. {
  6680. NoteMessage = FORMAT(L"%s: %s",
  6681. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), NoteMessage));
  6682. }
  6683. if (WinConfiguration->BalloonNotifications)
  6684. {
  6685. AddNote(NoteMessage);
  6686. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6687. }
  6688. else
  6689. {
  6690. FlashOnBackground();
  6691. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6692. }
  6693. }
  6694. }
  6695. //---------------------------------------------------------------------------
  6696. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6697. {
  6698. RestoreApp();
  6699. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6700. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6701. !NonVisualDataModule->Busy)
  6702. {
  6703. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  6704. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6705. if (!ComponentVisible[fcQueueView])
  6706. {
  6707. ToggleQueueVisibility();
  6708. GoToQueue();
  6709. }
  6710. }
  6711. }
  6712. //---------------------------------------------------------------------------
  6713. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  6714. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6715. {
  6716. UnicodeString Message;
  6717. TNotifyEvent OnClick = NULL;
  6718. TObject * UserData = NULL;
  6719. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6720. switch (Event)
  6721. {
  6722. case qeEmptyButMonitored:
  6723. if ((ATerminal != Terminal) || QueueInvisible)
  6724. {
  6725. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6726. OnClick = QueueEmptyNoteClicked;
  6727. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6728. TerminalNoteData->Terminal = ATerminal;
  6729. UserData = TerminalNoteData;
  6730. }
  6731. break;
  6732. case qeEmpty:
  6733. OperationComplete(ATerminal->QueueOperationStart);
  6734. break;
  6735. case qePendingUserAction:
  6736. if ((ATerminal != Terminal) ||
  6737. (QueueInvisible && !IsQueueAutoPopup()))
  6738. {
  6739. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6740. }
  6741. break;
  6742. default:
  6743. DebugFail();
  6744. }
  6745. if (!Message.IsEmpty())
  6746. {
  6747. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6748. }
  6749. }
  6750. //---------------------------------------------------------------------------
  6751. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6752. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6753. {
  6754. if (FDDExtMapFile != NULL)
  6755. {
  6756. CloseHandle(FDDExtMapFile);
  6757. FDDExtMapFile = NULL;
  6758. }
  6759. if (WinConfiguration->DDFakeFile)
  6760. {
  6761. DDFakeFileInitDrag(FileList, Created);
  6762. }
  6763. }
  6764. //---------------------------------------------------------------------------
  6765. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  6766. {
  6767. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  6768. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  6769. {
  6770. FFakeFileDropTarget = FileName;
  6771. }
  6772. }
  6773. //---------------------------------------------------------------------------
  6774. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  6775. {
  6776. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6777. if (!ForceDirectories(ApiPath(Result)))
  6778. {
  6779. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  6780. }
  6781. FileSetAttr(ApiPath(Result), faHidden);
  6782. return Result;
  6783. }
  6784. //---------------------------------------------------------------------------
  6785. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  6786. bool & Created)
  6787. {
  6788. FFakeFileDropTarget = UnicodeString();
  6789. FDragFakeDirectory = CreateFakeTransferDirectory();
  6790. FileList->AddItem(NULL, FDragFakeDirectory);
  6791. Created = true;
  6792. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  6793. {
  6794. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  6795. }
  6796. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6797. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6798. {
  6799. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6800. TDragExtCommStruct* CommStruct;
  6801. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6802. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6803. DebugAssert(CommStruct != NULL);
  6804. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  6805. CommStruct->Dragging = true;
  6806. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  6807. LENOF(CommStruct->DropDest));
  6808. NULL_TERMINATE(CommStruct->DropDest);
  6809. UnmapViewOfFile(CommStruct);
  6810. }
  6811. }
  6812. //---------------------------------------------------------------------------
  6813. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  6814. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  6815. {
  6816. bool Result;
  6817. DebugAssert(!IsLocalBrowserMode());
  6818. if (Sender == RemoteDirView)
  6819. {
  6820. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  6821. }
  6822. else
  6823. {
  6824. DebugAssert(Sender == RemoteDriveView);
  6825. TStrings * FileList = RemoteDriveView->DragFileList();
  6826. try
  6827. {
  6828. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  6829. }
  6830. __finally
  6831. {
  6832. delete FileList;
  6833. }
  6834. }
  6835. if (FDDTargetControl == RemoteDriveView)
  6836. {
  6837. RemoteDriveView->UpdateDropTarget();
  6838. }
  6839. // foRemoteMove happens when file/dir is dragged within the remote tree view
  6840. // or from tree view to dir view.
  6841. // foMove happens when file/dir is dragged from remote tree view outside of
  6842. // application via dragex
  6843. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  6844. (DropSourceControl == RemoteDriveView))
  6845. {
  6846. RemoteDriveView->UpdateDropSource();
  6847. }
  6848. return Result;
  6849. }
  6850. //---------------------------------------------------------------------------
  6851. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  6852. {
  6853. DebugAssert(!IsLocalBrowserMode());
  6854. // This also handles drops of remote files to queue.
  6855. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  6856. SAFE_DESTROY(FDDFileList);
  6857. if (!FFakeFileDropTarget.IsEmpty())
  6858. {
  6859. RemoveDir(ApiPath(FFakeFileDropTarget));
  6860. }
  6861. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  6862. {
  6863. try
  6864. {
  6865. TDragResult DDResult = (Sender == RemoteDirView) ?
  6866. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  6867. // Focus is moved to the target application,
  6868. // but as we are going to present the UI, we need to steal the focus back.
  6869. // This most likely won't work though (windows does not allow application
  6870. // to steal focus most of the time)
  6871. Application->BringToFront();
  6872. // On older version of Windows we never got drMove here, see also comment below.
  6873. // On Windows 10, we get the "move".
  6874. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  6875. {
  6876. UnicodeString TargetDirectory;
  6877. TFileOperation Operation;
  6878. // drInvalid may mean drMove, see comment below
  6879. switch (DDResult)
  6880. {
  6881. case drCopy:
  6882. Operation = foCopy;
  6883. break;
  6884. case drMove:
  6885. Operation = foMove;
  6886. break;
  6887. default:
  6888. DebugFail();
  6889. case drInvalid:
  6890. // prefer "copy" for safety
  6891. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  6892. break;
  6893. }
  6894. TTransferOperationParam Param;
  6895. UnicodeString CounterName;
  6896. bool ForceQueue;
  6897. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  6898. {
  6899. // we get drInvalid both if d&d was intercepted by ddext,
  6900. // and when users drops on no-drop location.
  6901. // we tell the difference by existence of response from ddext,
  6902. // so we ignore absence of response in this case
  6903. if (DDResult != drInvalid)
  6904. {
  6905. // here we know that the extension is installed,
  6906. // as it is checked as soon as drag&drop starts from
  6907. // RemoteFileControlDDCreateDragFileList
  6908. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  6909. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  6910. {
  6911. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  6912. }
  6913. else
  6914. {
  6915. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  6916. }
  6917. }
  6918. }
  6919. else
  6920. {
  6921. // download using fake file
  6922. Param.Temp = false;
  6923. Param.DragDrop = true;
  6924. if (ForceQueue)
  6925. {
  6926. Param.Queue = asOn;
  6927. }
  6928. if (Sender == RemoteDirView)
  6929. {
  6930. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  6931. }
  6932. if (RemoteFileControlFileOperation(Sender, Operation,
  6933. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  6934. {
  6935. Configuration->Usage->Inc(CounterName);
  6936. }
  6937. }
  6938. }
  6939. }
  6940. __finally
  6941. {
  6942. CloseHandle(FDDExtMapFile);
  6943. FDDExtMapFile = NULL;
  6944. delete FDragFakeMonitors;
  6945. FDragFakeMonitors = NULL;
  6946. RemoveDir(ApiPath(FDragFakeDirectory));
  6947. FDragFakeDirectory = L"";
  6948. FFakeFileDropTarget = UnicodeString();
  6949. }
  6950. }
  6951. if (FDragDropSshTerminate.get() != NULL)
  6952. {
  6953. FDragDropSshTerminate->Rethrow();
  6954. }
  6955. }
  6956. //---------------------------------------------------------------------------
  6957. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  6958. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  6959. {
  6960. // Remember drop effect so we know (when user drops files), if we copy or move
  6961. FLastDropEffect = dwEffect;
  6962. }
  6963. //---------------------------------------------------------------------------
  6964. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  6965. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  6966. {
  6967. bool Result;
  6968. if (FDDTargetControl == QueueView3)
  6969. {
  6970. Directory = DefaultDownloadTargetDirectory();
  6971. Result = true;
  6972. CounterName = L"DownloadsDragDropQueue";
  6973. ForceQueue = true;
  6974. }
  6975. else if (!FFakeFileDropTarget.IsEmpty())
  6976. {
  6977. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  6978. Result = true;
  6979. ForceQueue = false;
  6980. CounterName = L"DownloadsDragDropFakeFile";
  6981. }
  6982. else
  6983. {
  6984. ForceQueue = false;
  6985. Enabled = false;
  6986. try
  6987. {
  6988. int Timer = 0;
  6989. Result = false;
  6990. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  6991. {
  6992. {
  6993. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6994. TDragExtCommStruct* CommStruct;
  6995. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6996. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6997. DebugAssert(CommStruct != NULL);
  6998. Result = !CommStruct->Dragging;
  6999. if (Result)
  7000. {
  7001. Directory = ExtractFilePath(CommStruct->DropDest);
  7002. CounterName = L"DownloadsDragDropExternalExt";
  7003. }
  7004. UnmapViewOfFile(CommStruct);
  7005. }
  7006. if (!Result)
  7007. {
  7008. Sleep(50);
  7009. Timer += 50;
  7010. Application->ProcessMessages();
  7011. }
  7012. }
  7013. }
  7014. __finally
  7015. {
  7016. Enabled = true;
  7017. }
  7018. }
  7019. return Result;
  7020. }
  7021. //---------------------------------------------------------------------------
  7022. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7023. const UnicodeString TempDir, int SecDelay)
  7024. {
  7025. TDateTime Alarm = Now() + (double)((double)SecDelay*OneMillisecond);
  7026. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7027. if (FDelayedDeletionTimer == NULL)
  7028. {
  7029. DebugAssert(HandleAllocated());
  7030. FDelayedDeletionTimer = new TTimer(this);
  7031. FDelayedDeletionTimer->Interval = 10000;
  7032. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7033. }
  7034. else
  7035. {
  7036. FDelayedDeletionTimer->Enabled = true;
  7037. }
  7038. }
  7039. //---------------------------------------------------------------------------
  7040. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7041. {
  7042. DebugAssert(FDelayedDeletionList != NULL);
  7043. TDateTime N = Now();
  7044. TDateTime Alert;
  7045. UnicodeString Directory;
  7046. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7047. {
  7048. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7049. if ((N >= Alert) || (Sender == NULL))
  7050. {
  7051. Directory = FDelayedDeletionList->Strings[Index];
  7052. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7053. {
  7054. FDelayedDeletionList->Delete(Index);
  7055. }
  7056. }
  7057. }
  7058. if (FDelayedDeletionList->Count == 0)
  7059. {
  7060. FDelayedDeletionTimer->Enabled = false;
  7061. }
  7062. }
  7063. //---------------------------------------------------------------------------
  7064. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7065. {
  7066. DebugAssert(!IsLocalBrowserMode());
  7067. if (IsFileControl(FDDTargetControl, osRemote) ||
  7068. (FDDTargetControl == SessionsPageControl))
  7069. {
  7070. TTerminal * TargetTerminal = NULL;
  7071. TFileOperation Operation = foNone;
  7072. if (FDDTargetControl == SessionsPageControl)
  7073. {
  7074. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7075. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7076. // do not allow dropping on the "+" tab
  7077. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  7078. if (TargetTerminal != NULL)
  7079. {
  7080. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7081. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7082. {
  7083. Operation = foRemoteMove;
  7084. }
  7085. else
  7086. {
  7087. Operation = foRemoteCopy;
  7088. }
  7089. }
  7090. }
  7091. else
  7092. {
  7093. if (FLastDropEffect == DROPEFFECT_MOVE)
  7094. {
  7095. Operation = foRemoteMove;
  7096. }
  7097. else if (FLastDropEffect == DROPEFFECT_COPY)
  7098. {
  7099. Operation = foRemoteCopy;
  7100. }
  7101. }
  7102. if (Operation != foNone)
  7103. {
  7104. RemoteFileControlFileOperation(DropSourceControl,
  7105. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7106. }
  7107. // abort drag&drop
  7108. Abort();
  7109. }
  7110. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7111. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7112. (FDDTargetControl != QueueView3))
  7113. {
  7114. DebugAssert(!FDragTempDir.IsEmpty());
  7115. TTransferType Type;
  7116. UnicodeString TempDir = FDragTempDir;
  7117. // We clear FDragTempDir before calling
  7118. // just in case it fail (raises exception)
  7119. FDragTempDir = L"";
  7120. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7121. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7122. // empty directory parameter means temp directory -> don't display it!
  7123. UnicodeString TargetDir = L"";
  7124. int Options = 0;
  7125. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7126. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7127. {
  7128. Abort();
  7129. }
  7130. // TargetDir is set when dropped on local file control
  7131. // (this was workaround for legacy dirview event handling, now it should be
  7132. // made prettier)
  7133. if (TargetDir.IsEmpty())
  7134. {
  7135. TargetDir = TempDir;
  7136. if (ForceDirectories(ApiPath(TargetDir)))
  7137. {
  7138. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7139. FPendingTempSpaceWarn = true;
  7140. try
  7141. {
  7142. FDragDropOperation = true;
  7143. int Params = cpTemporary |
  7144. (Type == ttMove ? cpDelete : 0);
  7145. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7146. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7147. }
  7148. __finally
  7149. {
  7150. FDragDropOperation = false;
  7151. FPendingTempSpaceWarn = false;
  7152. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7153. }
  7154. }
  7155. else
  7156. {
  7157. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7158. }
  7159. }
  7160. }
  7161. }
  7162. //---------------------------------------------------------------------------
  7163. void __fastcall TCustomScpExplorerForm::DDDownload(
  7164. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  7165. {
  7166. DebugAssert(!IsLocalBrowserMode());
  7167. TAutoBatch AutoBatch(this);
  7168. UpdateCopyParamCounters(*CopyParam);
  7169. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7170. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7171. {
  7172. RemoteDriveView->UpdateDropSource();
  7173. }
  7174. }
  7175. //---------------------------------------------------------------------------
  7176. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7177. {
  7178. public:
  7179. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7180. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7181. {
  7182. }
  7183. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7184. {
  7185. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7186. TDataObjectFilesEx::AllowData(FormatEtc);
  7187. }
  7188. };
  7189. //---------------------------------------------------------------------------
  7190. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7191. TObject * Sender, TDataObject *& DataObject)
  7192. {
  7193. if (FDDExtMapFile != NULL)
  7194. {
  7195. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7196. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7197. {
  7198. Abort();
  7199. }
  7200. if (FileList->Count > 0)
  7201. {
  7202. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7203. if (!FilesObject->IsValid(true, true))
  7204. {
  7205. FilesObject->_Release();
  7206. }
  7207. else
  7208. {
  7209. DataObject = FilesObject;
  7210. }
  7211. }
  7212. }
  7213. }
  7214. //---------------------------------------------------------------------------
  7215. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7216. {
  7217. DebugFail();
  7218. }
  7219. //---------------------------------------------------------------------------
  7220. void __fastcall TCustomScpExplorerForm::GoToTree()
  7221. {
  7222. ComponentVisible[fcRemoteTree] = true;
  7223. DriveView(osRemote)->SetFocus();
  7224. }
  7225. //---------------------------------------------------------------------------
  7226. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7227. TPanelExport Export)
  7228. {
  7229. TCustomDirView * DirView = this->DirView(Side);
  7230. std::unique_ptr<TStrings> ExportData(new TStringList());
  7231. switch (Export)
  7232. {
  7233. case pePath:
  7234. ExportData->Add(DirView->PathName);
  7235. break;
  7236. case peFileList:
  7237. case peFullFileList:
  7238. {
  7239. bool FullPath = (Export == peFullFileList);
  7240. DirView->CreateFileList(false, FullPath, ExportData.get());
  7241. for (int Index = 0; Index < ExportData->Count; Index++)
  7242. {
  7243. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7244. {
  7245. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7246. }
  7247. }
  7248. }
  7249. break;
  7250. default:
  7251. DebugFail();
  7252. }
  7253. return ExportData.release();
  7254. }
  7255. //---------------------------------------------------------------------------
  7256. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7257. TPanelExport Export, TPanelExportDestination Destination)
  7258. {
  7259. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7260. PanelExportStore(Side, Export, Destination, ExportData.get());
  7261. }
  7262. //---------------------------------------------------------------------------
  7263. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7264. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7265. TStrings * ExportData)
  7266. {
  7267. if (Destination == pedClipboard)
  7268. {
  7269. TInstantOperationVisualizer Visualizer;
  7270. CopyToClipboard(ExportData);
  7271. }
  7272. else
  7273. {
  7274. DebugFail();
  7275. }
  7276. }
  7277. //---------------------------------------------------------------------------
  7278. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7279. {
  7280. TCustomDirView * DirView = this->DirView(Side);
  7281. UnicodeString Mask = DirView->Mask;
  7282. if (DoFilterMaskDialog(DirView, Mask))
  7283. {
  7284. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7285. Configuration->Usage->Inc(L"Filters");
  7286. }
  7287. }
  7288. //---------------------------------------------------------------------------
  7289. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7290. {
  7291. return FQueueController->DefaultOperation();
  7292. }
  7293. //---------------------------------------------------------------------------
  7294. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7295. TQueueOperation Operation, void ** Param)
  7296. {
  7297. switch (Operation)
  7298. {
  7299. case qoPreferences:
  7300. return true;
  7301. case qoGoTo:
  7302. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7303. case qoOnceEmpty:
  7304. return IsAnythingQueued();
  7305. default:
  7306. return FQueueController->AllowOperation(Operation, Param);
  7307. }
  7308. }
  7309. //---------------------------------------------------------------------------
  7310. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7311. {
  7312. if (DebugAlwaysTrue(QueueView3->Visible))
  7313. {
  7314. QueueView3->SetFocus();
  7315. }
  7316. }
  7317. //---------------------------------------------------------------------------
  7318. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7319. TQueueOperation Operation, void * Param)
  7320. {
  7321. if (Operation == qoGoTo)
  7322. {
  7323. GoToQueue();
  7324. }
  7325. else if (Operation == qoPreferences)
  7326. {
  7327. PreferencesDialog(pmQueue);
  7328. }
  7329. else
  7330. {
  7331. FQueueController->ExecuteOperation(Operation, Param);
  7332. }
  7333. }
  7334. //---------------------------------------------------------------------------
  7335. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7336. {
  7337. return (Queue != NULL) && Queue->Enabled;
  7338. }
  7339. //---------------------------------------------------------------------------
  7340. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7341. {
  7342. DebugAssert(Queue != NULL);
  7343. if (Queue != NULL)
  7344. {
  7345. Queue->Enabled = !Queue->Enabled;
  7346. }
  7347. }
  7348. //---------------------------------------------------------------------------
  7349. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7350. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7351. {
  7352. FQueueActedItem = QueueView3->ItemFocused;
  7353. }
  7354. //---------------------------------------------------------------------------
  7355. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7356. {
  7357. return TopDock->Height + QueueSplitter->Height;
  7358. }
  7359. //---------------------------------------------------------------------------
  7360. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7361. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7362. {
  7363. // when queue is hidden by double-clicking splitter, stray attempt to
  7364. // resize the panel with strange value arrives, make sure it is ignored
  7365. if (ComponentVisible[fcQueueView])
  7366. {
  7367. int HeightLimit = ClientHeight - GetStaticComponentsHeight() -
  7368. RemotePanel->Constraints->MinHeight;
  7369. if (NewSize > HeightLimit)
  7370. {
  7371. NewSize = HeightLimit;
  7372. }
  7373. }
  7374. else
  7375. {
  7376. Accept = false;
  7377. }
  7378. }
  7379. //---------------------------------------------------------------------------
  7380. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7381. TDragObject *& /*DragObject*/)
  7382. {
  7383. FQueueActedItem = QueueView3->ItemFocused;
  7384. }
  7385. //---------------------------------------------------------------------------
  7386. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7387. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7388. {
  7389. Accept = true;
  7390. if (Source == QueueView3)
  7391. {
  7392. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7393. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7394. if (Accept)
  7395. {
  7396. TQueueItemProxy * QueueItem;
  7397. TQueueItemProxy * DestQueueItem;
  7398. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7399. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7400. Accept = (QueueItem != DestQueueItem) &&
  7401. (QueueItem->Status == TQueueItem::qsPending) &&
  7402. (DestQueueItem->Status == TQueueItem::qsPending);
  7403. }
  7404. }
  7405. }
  7406. //---------------------------------------------------------------------------
  7407. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7408. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7409. {
  7410. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7411. {
  7412. TQueueItemProxy * QueueItem;
  7413. TQueueItemProxy * DestQueueItem;
  7414. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7415. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7416. QueueItem->Move(DestQueueItem);
  7417. }
  7418. }
  7419. //---------------------------------------------------------------------------
  7420. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7421. {
  7422. if ((QueueView3->ItemFocused == NULL) &&
  7423. (QueueView3->Items->Count > 0))
  7424. {
  7425. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7426. }
  7427. QueueLabelUpdateStatus();
  7428. }
  7429. //---------------------------------------------------------------------------
  7430. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7431. {
  7432. QueueLabelUpdateStatus();
  7433. }
  7434. //---------------------------------------------------------------------------
  7435. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7436. {
  7437. QueueLabel->UpdateStatus();
  7438. }
  7439. //---------------------------------------------------------------------------
  7440. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7441. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7442. {
  7443. if (Selected)
  7444. {
  7445. NonVisualDataModule->UpdateNonVisibleActions();
  7446. }
  7447. }
  7448. //---------------------------------------------------------------------------
  7449. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7450. {
  7451. TDragDropFilesEx * Result = NULL;
  7452. if (Sender == SessionsPageControl)
  7453. {
  7454. Result = FSessionsDragDropFilesEx;
  7455. }
  7456. else if (Sender == QueueView3)
  7457. {
  7458. Result = FQueueDragDropFilesEx;
  7459. }
  7460. else
  7461. {
  7462. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7463. if (DirView != NULL)
  7464. {
  7465. Result = DirView->DragDropFilesEx;
  7466. }
  7467. else
  7468. {
  7469. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7470. if (DriveView != NULL)
  7471. {
  7472. Result = DriveView->DragDropFilesEx;
  7473. }
  7474. }
  7475. }
  7476. DebugAssert(Result != NULL);
  7477. return Result;
  7478. }
  7479. //---------------------------------------------------------------------------
  7480. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7481. {
  7482. return (DView->SelCount == DView->FilesCount);
  7483. }
  7484. //---------------------------------------------------------------------------
  7485. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7486. {
  7487. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7488. }
  7489. //---------------------------------------------------------------------------
  7490. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7491. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7492. {
  7493. DebugAssert(!IsLocalBrowserMode());
  7494. TFileOperation Operation;
  7495. switch (Effect)
  7496. {
  7497. case DROPEFFECT_MOVE:
  7498. Operation = foMove;
  7499. break;
  7500. case DROPEFFECT_COPY:
  7501. // occures on WinXP (reported by user)
  7502. default:
  7503. Operation = foCopy;
  7504. break;
  7505. };
  7506. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7507. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7508. if (DragDropFilesEx->FileList->Count > 0)
  7509. {
  7510. TStrings * FileList = new TStringList();
  7511. try
  7512. {
  7513. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7514. {
  7515. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7516. }
  7517. FDragDropOperation = true;
  7518. TTransferOperationParam Param;
  7519. Param.TargetDirectory = TargetPath;
  7520. // upload, no temp dirs
  7521. Param.Temp = false;
  7522. Param.DragDrop = DragDrop;
  7523. Param.Options =
  7524. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7525. if (ForceQueue)
  7526. {
  7527. Param.Queue = asOn;
  7528. }
  7529. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7530. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7531. {
  7532. if (IsFileControl(DropSourceControl, osLocal))
  7533. {
  7534. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7535. }
  7536. else
  7537. {
  7538. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7539. }
  7540. }
  7541. }
  7542. __finally
  7543. {
  7544. FDragDropOperation = false;
  7545. delete FileList;
  7546. }
  7547. }
  7548. }
  7549. //---------------------------------------------------------------------------
  7550. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7551. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7552. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7553. {
  7554. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7555. }
  7556. //---------------------------------------------------------------------------
  7557. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7558. TObject * Sender, int grfKeyState, int & dwEffect)
  7559. {
  7560. DebugAssert(!IsLocalBrowserMode());
  7561. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7562. if ((dwEffect != DROPEFFECT_NONE) &&
  7563. IsFileControl(DropSourceControl, osRemote))
  7564. {
  7565. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7566. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7567. (RemoteDirView->DropTarget == NULL))
  7568. {
  7569. dwEffect = DROPEFFECT_NONE;
  7570. }
  7571. else
  7572. {
  7573. if (dwEffect == DROPEFFECT_COPY)
  7574. {
  7575. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  7576. // currently we support copying always (at least via temporary directory);
  7577. // remove associated checks once this all proves stable and working
  7578. bool CopyCapable = true;
  7579. // if we do not support neither of operations, there's no discussion
  7580. if (!MoveCapable && !CopyCapable)
  7581. {
  7582. dwEffect = DROPEFFECT_NONE;
  7583. }
  7584. else
  7585. {
  7586. // The default effect inside remote panel is move,
  7587. // unless we do not support it, but support copy
  7588. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7589. {
  7590. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  7591. }
  7592. else
  7593. {
  7594. // with ctrl-down, we want copy unless it is not supported
  7595. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  7596. }
  7597. }
  7598. }
  7599. }
  7600. }
  7601. }
  7602. //---------------------------------------------------------------------------
  7603. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7604. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7605. {
  7606. DebugAssert(!IsLocalBrowserMode());
  7607. if (FDDTotalSize >= 0)
  7608. {
  7609. if (File->IsDirectory)
  7610. {
  7611. FDDTotalSize = -1;
  7612. }
  7613. else
  7614. {
  7615. FDDTotalSize += File->Size;
  7616. }
  7617. }
  7618. DebugAssert(!FDragTempDir.IsEmpty());
  7619. // TODO: this is quite ineffective
  7620. // TODO: what if invalid character replacement is disabled?
  7621. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7622. UnicodeString TransferFileName;
  7623. if (Sender == RemoteDriveView)
  7624. {
  7625. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7626. }
  7627. else
  7628. {
  7629. TransferFileName = File->FileName;
  7630. }
  7631. FDDFileList->AddObject(TransferFileName, File);
  7632. }
  7633. //---------------------------------------------------------------------------
  7634. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7635. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7636. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7637. {
  7638. // sometimes we do not get DDEnd so the list is not released
  7639. SAFE_DESTROY(FDDFileList);
  7640. FDDFileList = new TStringList();
  7641. FDragTempDir = WinConfiguration->TemporaryDir();
  7642. FDDTotalSize = 0;
  7643. FDragDropSshTerminate.reset(NULL);
  7644. }
  7645. //---------------------------------------------------------------------------
  7646. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7647. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7648. HRESULT & Result)
  7649. {
  7650. if (Result == DRAGDROP_S_DROP)
  7651. {
  7652. try
  7653. {
  7654. GlobalDragImageList->HideDragImage();
  7655. try
  7656. {
  7657. RemoteFileControlDDTargetDrop();
  7658. }
  7659. catch(ESshTerminate & E)
  7660. {
  7661. FDragDropSshTerminate.reset(E.Clone());
  7662. }
  7663. }
  7664. catch (Exception &E)
  7665. {
  7666. // If downloading fails we need to cancel drag&drop, otherwise
  7667. // Explorer shows error
  7668. // But by the way exception probably never reach this point as
  7669. // it's catched on way.
  7670. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7671. Result = DRAGDROP_S_CANCEL;
  7672. ShowExtendedException(Terminal, &E);
  7673. }
  7674. }
  7675. }
  7676. //---------------------------------------------------------------------------
  7677. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7678. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7679. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7680. {
  7681. TFileMasks::TParams MaskParams;
  7682. MaskParams.Size = Size;
  7683. MaskParams.Modification = Modification;
  7684. // this does not re-parse the mask if it is the same as the last time
  7685. FDirViewMatchMask = Masks;
  7686. bool ImplicitMatch;
  7687. Matches =
  7688. // RecurseInclude parameter has no effect as the Path is empty
  7689. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7690. (AllowImplicitMatches || !ImplicitMatch);
  7691. }
  7692. //---------------------------------------------------------------------------
  7693. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7694. TObject * Sender, TListItem * Item, WORD & Indexes)
  7695. {
  7696. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7697. UnicodeString Ext;
  7698. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  7699. {
  7700. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7701. }
  7702. else
  7703. {
  7704. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7705. }
  7706. if (SameText(Ext, Configuration->PartialExt))
  7707. {
  7708. Indexes |= oiPartial;
  7709. }
  7710. }
  7711. //---------------------------------------------------------------------------
  7712. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7713. {
  7714. return
  7715. DirViewEnabled(osCurrent) &&
  7716. DirView(osCurrent)->CanPasteFromClipBoard();
  7717. }
  7718. //---------------------------------------------------------------------------
  7719. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7720. {
  7721. bool Result = false;
  7722. if (CanPasteToDirViewFromClipBoard())
  7723. {
  7724. Result = true;
  7725. }
  7726. else
  7727. {
  7728. UnicodeString ClipboardText;
  7729. if (NonEmptyTextFromClipboard(ClipboardText))
  7730. {
  7731. if (StoredSessions->IsUrl(ClipboardText))
  7732. {
  7733. Result = true;
  7734. }
  7735. else
  7736. {
  7737. Result = DirViewEnabled(osCurrent);
  7738. }
  7739. }
  7740. }
  7741. return Result;
  7742. }
  7743. //---------------------------------------------------------------------------
  7744. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7745. {
  7746. if (DoesClipboardContainOurFiles())
  7747. {
  7748. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  7749. {
  7750. DebugAssert(!IsLocalBrowserMode());
  7751. TTerminalManager * Manager = TTerminalManager::Instance();
  7752. TTerminal * TergetTerminal = Manager->ActiveTerminal;
  7753. Manager->ActiveTerminal = FClipboardTerminal;
  7754. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TergetTerminal);
  7755. }
  7756. }
  7757. else if (CanPasteToDirViewFromClipBoard())
  7758. {
  7759. DirView(osCurrent)->PasteFromClipBoard();
  7760. }
  7761. else
  7762. {
  7763. UnicodeString ClipboardText;
  7764. if (NonEmptyTextFromClipboard(ClipboardText))
  7765. {
  7766. if (StoredSessions->IsUrl(ClipboardText))
  7767. {
  7768. NewSession(false, ClipboardText);
  7769. }
  7770. else
  7771. {
  7772. DirView(osCurrent)->Path = ClipboardText;
  7773. }
  7774. }
  7775. }
  7776. }
  7777. //---------------------------------------------------------------------------
  7778. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  7779. {
  7780. // TBD
  7781. }
  7782. //---------------------------------------------------------------------------
  7783. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  7784. {
  7785. TCustomDirView * ADirView = DirView(Side);
  7786. ADirView->SelectAll(Mode);
  7787. ADirView->SetFocus();
  7788. }
  7789. //---------------------------------------------------------------------------
  7790. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  7791. {
  7792. TCustomDirView * ADirView = DirView(Side);
  7793. TFileFilter Filter;
  7794. DefaultFileFilter(Filter);
  7795. if (DoSelectMaskDialog(ADirView, Select, Filter))
  7796. {
  7797. Configuration->Usage->Inc(L"MaskSelections");
  7798. ADirView->SelectFiles(Filter, Select);
  7799. ADirView->SetFocus();
  7800. }
  7801. }
  7802. //---------------------------------------------------------------------------
  7803. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  7804. {
  7805. TCustomDirView * ADirView = DirView(Side);
  7806. ADirView->RestoreSelectedNames();
  7807. ADirView->SetFocus();
  7808. }
  7809. //---------------------------------------------------------------------------
  7810. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  7811. {
  7812. TCustomDirView * CurrentDirView = DirView(osCurrent);
  7813. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  7814. {
  7815. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  7816. UnicodeString Ext;
  7817. if (!IsSideLocalBrowser(osCurrent))
  7818. {
  7819. Ext = UnixExtractFileExt(FileName);
  7820. }
  7821. else
  7822. {
  7823. Ext = ExtractFileExt(FileName);
  7824. }
  7825. if (Ext.IsEmpty())
  7826. {
  7827. Ext = L".";
  7828. }
  7829. TFileFilter Filter;
  7830. Filter.Masks = FORMAT(L"*%s", (Ext));
  7831. Filter.Directories = false;
  7832. CurrentDirView->SelectFiles(Filter, Select);
  7833. }
  7834. }
  7835. //---------------------------------------------------------------------------
  7836. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  7837. const TStatusFileInfo & FileInfo, TOperationSide Side)
  7838. {
  7839. UnicodeString Result;
  7840. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  7841. {
  7842. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  7843. }
  7844. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  7845. {
  7846. // noop
  7847. }
  7848. else
  7849. {
  7850. Result =
  7851. FMTLOAD(FILE_INFO_FORMAT,
  7852. (FormatBytes(FileInfo.SelectedSize),
  7853. FormatBytes(FileInfo.FilesSize),
  7854. FormatNumber(FileInfo.SelectedCount),
  7855. FormatNumber(FileInfo.FilesCount)));
  7856. }
  7857. return Result;
  7858. }
  7859. //---------------------------------------------------------------------------
  7860. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  7861. TTBXStatusPanel * Panel, TOperationSide Side)
  7862. {
  7863. if (Panel->Index == 1)
  7864. {
  7865. ToggleShowHiddenFiles();
  7866. }
  7867. else if (Panel->Index == 2)
  7868. {
  7869. Filter(Side);
  7870. }
  7871. }
  7872. //---------------------------------------------------------------------------
  7873. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  7874. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  7875. {
  7876. DebugAssert(!StatusBar->SimplePanel);
  7877. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  7878. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  7879. }
  7880. //---------------------------------------------------------------------------
  7881. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  7882. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  7883. {
  7884. DebugAssert(StatusBar->Panels->Count >= 3);
  7885. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  7886. if (FileInfo.HiddenCount > 0)
  7887. {
  7888. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  7889. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7890. }
  7891. else
  7892. {
  7893. HiddenFilesPanel->ViewPriority = 0;
  7894. // not really necessary, just to cleanup no-longer-valid data
  7895. HiddenFilesPanel->Caption = L"";
  7896. }
  7897. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  7898. if (FileInfo.FilteredCount > 0)
  7899. {
  7900. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  7901. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7902. }
  7903. else
  7904. {
  7905. FilteredFilesPanel->ViewPriority = 0;
  7906. // not really necessary, just to cleanup no-longer-valid data
  7907. FilteredFilesPanel->Caption = L"";
  7908. }
  7909. }
  7910. //---------------------------------------------------------------------------
  7911. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  7912. TObject * /*Sender*/)
  7913. {
  7914. if (DirView(osRemote)->Enabled)
  7915. {
  7916. DirView(osRemote)->SetFocus();
  7917. }
  7918. }
  7919. //---------------------------------------------------------------------------
  7920. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  7921. {
  7922. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  7923. switch (Config.Show)
  7924. {
  7925. case qvShow:
  7926. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  7927. {
  7928. Config.Show = qvHide;
  7929. }
  7930. else
  7931. {
  7932. Config.Show = Config.LastHideShow;
  7933. }
  7934. break;
  7935. case qvHideWhenEmpty:
  7936. if (ComponentVisible[fcQueueView])
  7937. {
  7938. Config.Show = qvHide;
  7939. }
  7940. else
  7941. {
  7942. Config.LastHideShow = Config.Show;
  7943. Config.Show = qvShow;
  7944. }
  7945. break;
  7946. case qvHide:
  7947. Config.LastHideShow = Config.Show;
  7948. Config.Show = qvShow;
  7949. break;
  7950. }
  7951. WinConfiguration->QueueView = Config;
  7952. }
  7953. //---------------------------------------------------------------------------
  7954. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  7955. {
  7956. UnicodeString Result;
  7957. if (FTerminal != NULL)
  7958. {
  7959. switch (WinConfiguration->PathInCaption)
  7960. {
  7961. case picShort:
  7962. Result = TTerminalManager::Instance()->GetTerminalShortPath(FTerminal);
  7963. break;
  7964. case picFull:
  7965. Result = FTerminal->CurrentDirectory;
  7966. break;
  7967. }
  7968. }
  7969. return Result;
  7970. }
  7971. //---------------------------------------------------------------------------
  7972. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  7973. {
  7974. TColor Result = GetWindowColor(FSessionColor);
  7975. return Result;
  7976. }
  7977. //---------------------------------------------------------------------------
  7978. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  7979. {
  7980. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  7981. return Result;
  7982. }
  7983. //---------------------------------------------------------------------------
  7984. void __fastcall TCustomScpExplorerForm::UpdateControls()
  7985. {
  7986. TTerminalManager::Instance()->UpdateAppTitle();
  7987. // WORAKRDOUND: Disabling list view when it is not showing yet does not set its
  7988. // background to gray on Windows 7 (works on Windows 10).
  7989. // See also EnableControl
  7990. if (Showing)
  7991. {
  7992. if (Terminal != NULL)
  7993. {
  7994. // Update path when it changes
  7995. if ((SessionsPageControl->ActivePage != NULL) && (GetSessionTabTerminal(SessionsPageControl->ActivePage) == Terminal))
  7996. {
  7997. UpdateSessionTab(SessionsPageControl->ActivePage);
  7998. }
  7999. }
  8000. NonVisualDataModule->ReconnectSessionAction->Update();
  8001. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8002. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8003. CenterReconnectToolbar();
  8004. bool HasTerminal = (Terminal != NULL) && Terminal->Active;
  8005. if (HasTerminal)
  8006. {
  8007. // TODO needed yet with second local browser?
  8008. if (!RemoteDirView->Enabled)
  8009. {
  8010. RemoteDirView->Enabled = true;
  8011. if (FRemoteDirViewWasFocused)
  8012. {
  8013. ActiveControl = RemoteDirView;
  8014. }
  8015. }
  8016. RemoteDirView->Color = PanelColor();
  8017. }
  8018. else
  8019. {
  8020. if (RemoteDirView->Enabled)
  8021. {
  8022. // This is first called when the form is being constructed
  8023. // (not anymore due to Showing test above)
  8024. // but the false is overriden in the constructor later.
  8025. // An even later in TScpCommanderForm::DoShow()
  8026. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  8027. RemoteDirView->Enabled = false;
  8028. }
  8029. RemoteDirView->Color = DisabledPanelColor();
  8030. }
  8031. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8032. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8033. RemoteDriveView->Color = RemoteDirView->Color;
  8034. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8035. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fsBackgroundTransfers];
  8036. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8037. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8038. QueueLabelUpdateStatus();
  8039. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8040. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8041. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  8042. }
  8043. }
  8044. //---------------------------------------------------------------------------
  8045. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8046. {
  8047. UpdateControls();
  8048. }
  8049. //---------------------------------------------------------------------------
  8050. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8051. TObject * Sender)
  8052. {
  8053. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8054. DebugAssert(DirView != NULL);
  8055. DoDirViewLoaded(DirView);
  8056. TransferPresetAutoSelect();
  8057. }
  8058. //---------------------------------------------------------------------------
  8059. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8060. {
  8061. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8062. // first run after installation
  8063. if (double(Updates.LastCheck) == 0)
  8064. {
  8065. // make sure next time there will be an update (if enabled)
  8066. Updates.LastCheck = TDateTime(1);
  8067. WinConfiguration->Updates = Updates;
  8068. }
  8069. else if ((double(Updates.Period) > 0) &&
  8070. (Now() - Updates.LastCheck >= Updates.Period))
  8071. {
  8072. TThreadMethod OnUpdatesChecked = NULL;
  8073. if (!IsUWP())
  8074. {
  8075. OnUpdatesChecked = UpdatesChecked;
  8076. }
  8077. StartUpdateThread(OnUpdatesChecked);
  8078. }
  8079. }
  8080. //---------------------------------------------------------------------------
  8081. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8082. {
  8083. UnicodeString Message;
  8084. bool New;
  8085. TQueryType Type;
  8086. GetUpdatesMessage(Message, New, Type, false);
  8087. if (!Message.IsEmpty())
  8088. {
  8089. if (New)
  8090. {
  8091. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8092. }
  8093. if (!New && (Type != qtWarning))
  8094. {
  8095. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8096. }
  8097. else
  8098. {
  8099. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8100. }
  8101. if (New)
  8102. {
  8103. Configuration->Usage->Inc(L"UpdateNotifications");
  8104. }
  8105. }
  8106. }
  8107. //---------------------------------------------------------------------------
  8108. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8109. {
  8110. RestoreApp();
  8111. bool CanDisplay = !NonVisualDataModule->Busy;
  8112. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8113. {
  8114. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8115. }
  8116. if (CanDisplay)
  8117. {
  8118. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8119. CheckForUpdates(true);
  8120. }
  8121. else
  8122. {
  8123. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8124. }
  8125. }
  8126. //---------------------------------------------------------------------------
  8127. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8128. TCopyParamRuleData & Data)
  8129. {
  8130. DebugAssert(Terminal != NULL);
  8131. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8132. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8133. Data.RemoteDirectory = RemoteDirView->PathName;
  8134. }
  8135. //---------------------------------------------------------------------------
  8136. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8137. {
  8138. // Terminal can be null when we are changing local directory implicitly,
  8139. // because it has been deleted, while no session is connected
  8140. // (Login dialog is open)
  8141. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8142. {
  8143. DebugAssert(!IsLocalBrowserMode());
  8144. TCopyParamRuleData Data;
  8145. GetTransferPresetAutoSelectData(Data);
  8146. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8147. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8148. if (CopyParamIndex < 0)
  8149. {
  8150. // there is no preset that matches autoselection
  8151. // set preset that we consider "default"
  8152. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8153. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8154. }
  8155. else
  8156. {
  8157. // there is preset matching autoselection
  8158. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8159. if (CopyParamName == FCopyParamAutoSelected)
  8160. {
  8161. // autoselected the same preset as the last time
  8162. // make no change (i.e. preserve custom user preset, if any)
  8163. }
  8164. else
  8165. {
  8166. // autoselected the different preset then the last time (or there
  8167. // was default preset set)
  8168. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8169. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8170. }
  8171. }
  8172. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8173. {
  8174. if (CopyParamIndex >= 0)
  8175. {
  8176. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8177. }
  8178. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8179. try
  8180. {
  8181. int Fmt =
  8182. (CopyParamIndex < 0) ?
  8183. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8184. COPY_PARAM_AUTOSELECTED;
  8185. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8186. Data->Message = MainInstructions(Message);
  8187. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8188. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8189. CopyParamAttrs);
  8190. if (CopyParamIndex >= 0)
  8191. {
  8192. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8193. Info = FORMAT(L"%s\n \n%s", (Info,
  8194. FMTLOAD(COPY_PARAM_RULE,
  8195. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8196. }
  8197. Data->Message += L"\n\n" + Info;
  8198. if (WinConfiguration->CopyParamAutoSelectNotice)
  8199. {
  8200. TransferPresetNoteMessage(Data, true);
  8201. }
  8202. else
  8203. {
  8204. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8205. Data = NULL; // ownership passed
  8206. }
  8207. }
  8208. __finally
  8209. {
  8210. delete Data;
  8211. }
  8212. }
  8213. }
  8214. }
  8215. //---------------------------------------------------------------------------
  8216. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8217. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8218. {
  8219. DebugAssert(NoteData != NULL);
  8220. DebugAssert(!IsLocalBrowserMode());
  8221. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8222. TQueryButtonAlias Aliases[1];
  8223. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8224. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8225. Params.Aliases = Aliases;
  8226. Params.AliasesCount = LENOF(Aliases);
  8227. unsigned int Result =
  8228. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8229. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8230. switch (Result)
  8231. {
  8232. case qaNeverAskAgain:
  8233. DebugAssert(AllowNeverAskAgain);
  8234. WinConfiguration->CopyParamAutoSelectNotice = false;
  8235. break;
  8236. case qaIgnore:
  8237. PreferencesDialog(pmPresets);
  8238. break;
  8239. }
  8240. }
  8241. //---------------------------------------------------------------------------
  8242. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8243. {
  8244. // as of now useless, as this is used for notes only, never for balloons, ...
  8245. RestoreApp();
  8246. // .. and we should never be busy here
  8247. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8248. {
  8249. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8250. }
  8251. }
  8252. //---------------------------------------------------------------------------
  8253. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8254. TPreferencesMode APreferencesMode)
  8255. {
  8256. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8257. TCopyParamRuleData Data;
  8258. if (Terminal != NULL)
  8259. {
  8260. PreferencesData.reset(new TPreferencesDialogData());
  8261. GetTransferPresetAutoSelectData(Data);
  8262. PreferencesData->CopyParamRuleData = &Data;
  8263. }
  8264. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8265. }
  8266. //---------------------------------------------------------------------------
  8267. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8268. const TCustomCommandType & Command)
  8269. {
  8270. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8271. {
  8272. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (Command.Command)));
  8273. }
  8274. }
  8275. //---------------------------------------------------------------------------
  8276. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8277. {
  8278. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8279. TCustomCommandType Command;
  8280. // make sure we use local custom command when remote are not supported
  8281. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8282. {
  8283. Command = FLastCustomCommand;
  8284. }
  8285. else
  8286. {
  8287. Command.Params = Command.Params | ccLocal;
  8288. }
  8289. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8290. FEditingFocusedAdHocCommand = OnFocused;
  8291. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8292. int Options =
  8293. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8294. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8295. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8296. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8297. {
  8298. FLastCustomCommand = Command;
  8299. UpdateCustomCommandsToolbar();
  8300. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8301. }
  8302. }
  8303. //---------------------------------------------------------------------------
  8304. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8305. {
  8306. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8307. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8308. DebugAssert(State > 0);
  8309. if (State <= 0)
  8310. {
  8311. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (FLastCustomCommand.Command)));
  8312. }
  8313. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8314. }
  8315. //---------------------------------------------------------------------------
  8316. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8317. TCustomCommandType & Command, int & State)
  8318. {
  8319. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8320. if (Result)
  8321. {
  8322. Command = FLastCustomCommand;
  8323. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8324. }
  8325. return Result;
  8326. }
  8327. //---------------------------------------------------------------------------
  8328. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8329. {
  8330. ResetIncrementalSearch();
  8331. }
  8332. //---------------------------------------------------------------------------
  8333. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8334. {
  8335. DebugAssert(ComponentVisible[Component]);
  8336. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8337. }
  8338. //---------------------------------------------------------------------------
  8339. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8340. {
  8341. // when queue panel is resized here directly, the status bar is stretched
  8342. // over whole space the panel occupied
  8343. PostComponentHide(fcQueueView);
  8344. }
  8345. //---------------------------------------------------------------------------
  8346. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  8347. {
  8348. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  8349. RefreshColorMode();
  8350. WinConfiguration->ResetSysDarkTheme();
  8351. ConfigurationChanged();
  8352. ConfigureInterface();
  8353. // Should be called for all controls
  8354. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  8355. }
  8356. //---------------------------------------------------------------------------
  8357. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  8358. {
  8359. // Do not handle, when shutting down anyway (maybe also when not setup completelly yet?)
  8360. if (!FInvalid &&
  8361. (Message.LParam != 0) &&
  8362. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  8363. {
  8364. ThemeChanged();
  8365. }
  8366. TForm::Dispatch(&Message);
  8367. }
  8368. //---------------------------------------------------------------------------
  8369. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8370. {
  8371. TMessage * M = static_cast<TMessage*>(Message);
  8372. switch (M->Msg)
  8373. {
  8374. case CM_DIALOGCHAR:
  8375. CMDialogChar(*M);
  8376. break;
  8377. case WM_APPCOMMAND:
  8378. WMAppCommand(*M);
  8379. break;
  8380. case WM_SYSCOMMAND:
  8381. WMSysCommand(*M);
  8382. break;
  8383. case WM_QUERYENDSESSION:
  8384. WMQueryEndSession(*M);
  8385. break;
  8386. case WM_ENDSESSION:
  8387. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8388. break;
  8389. #ifdef _DEBUG
  8390. case WM_WINDOWPOSCHANGED:
  8391. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8392. break;
  8393. case CM_CUSTOMSTYLECHANGED:
  8394. TForm::Dispatch(Message);
  8395. break;
  8396. #endif
  8397. case WM_COMPONENT_HIDE:
  8398. {
  8399. Byte Component = static_cast<Byte>(M->WParam);
  8400. // sanity check
  8401. if (ComponentVisible[Component])
  8402. {
  8403. // special treatment
  8404. if (Component == fcQueueView)
  8405. {
  8406. ToggleQueueVisibility();
  8407. DebugAssert(!ComponentVisible[fcQueueView]);
  8408. }
  8409. else
  8410. {
  8411. ComponentVisible[Component] = false;
  8412. }
  8413. }
  8414. }
  8415. break;
  8416. case WM_COPYDATA:
  8417. WMCopyData(*M);
  8418. break;
  8419. case WM_MANAGES_CAPTION:
  8420. // caption managed in TTerminalManager::UpdateAppTitle()
  8421. M->Result = 1;
  8422. break;
  8423. case WM_WANTS_MOUSEWHEEL:
  8424. M->Result = 1;
  8425. break;
  8426. case CM_SHOWINGCHANGED:
  8427. CMShowingChanged(*M);
  8428. break;
  8429. case WM_CLOSE:
  8430. WMClose(*M);
  8431. break;
  8432. case WM_DPICHANGED:
  8433. WMDpiChanged(*M);
  8434. break;
  8435. case CM_DPICHANGED:
  8436. CMDpiChanged(*M);
  8437. break;
  8438. case WM_SETTINGCHANGE:
  8439. WMSettingChange(*M);
  8440. break;
  8441. case CM_DIALOGKEY:
  8442. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8443. break;
  8444. default:
  8445. TForm::Dispatch(Message);
  8446. break;
  8447. }
  8448. }
  8449. //---------------------------------------------------------------------------
  8450. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8451. {
  8452. // noop
  8453. }
  8454. //---------------------------------------------------------------------------
  8455. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8456. {
  8457. TForm::Dispatch(&Message);
  8458. FSessionColors->Clear();
  8459. AddFixedSessionImages();
  8460. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8461. UpdateImages();
  8462. }
  8463. //---------------------------------------------------------------------------
  8464. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8465. {
  8466. TForm::Dispatch(&Message);
  8467. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8468. UpdatePixelsPerInchMainWindowCounter();
  8469. }
  8470. //---------------------------------------------------------------------------
  8471. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8472. {
  8473. // Cannot close window while we are busy.
  8474. // We cannot test this in FormCloseQuery as that is called also from
  8475. // Close(), which is called by CloseApplicationAction. So we can be busy
  8476. // there already even, when it is legitimate to close the application.
  8477. // Possibly a better place to handle this would be WMSysCommand.
  8478. if (NonVisualDataModule->Busy)
  8479. {
  8480. Message.Result = 1;
  8481. }
  8482. else
  8483. {
  8484. TForm::Dispatch(&Message);
  8485. }
  8486. }
  8487. //---------------------------------------------------------------------------
  8488. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8489. {
  8490. TForm::Dispatch(&Message);
  8491. if (Showing && (Terminal == NULL))
  8492. {
  8493. // When we are starting minimized (i.e. from an installer),
  8494. // postpone showing Login dialog until we get restored.
  8495. // Otherwise the Login dialog (and Authentication window) show restored
  8496. // over invisible (minimized) main window.
  8497. if (WindowState == wsMinimized)
  8498. {
  8499. FNeedSession = true;
  8500. }
  8501. else
  8502. {
  8503. // This happens before application ever goes idle, so the toolbars would
  8504. // stay enabled (initial state) until the Login dialog is dismissed.
  8505. UpdateActions();
  8506. NonVisualDataModule->StartBusy();
  8507. try
  8508. {
  8509. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8510. // otherwise the dialog does not receive focus.
  8511. // With Commander interface the ProcessMessages is called already
  8512. // by TDriveView, but with Explorer interface, we need to call it explicily
  8513. Application->ProcessMessages();
  8514. // do not reload sessions, they have been loaded just now (optimization)
  8515. NeedSession(true);
  8516. }
  8517. __finally
  8518. {
  8519. NonVisualDataModule->EndBusy();
  8520. }
  8521. }
  8522. }
  8523. }
  8524. //---------------------------------------------------------------------------
  8525. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  8526. {
  8527. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  8528. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  8529. }
  8530. //---------------------------------------------------------------------------
  8531. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8532. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8533. int & MaxHeight)
  8534. {
  8535. // workaround for bug in TWinControl.CalcConstraints
  8536. // Check for empty rect (restore from iconic state) is done there only after
  8537. // call to AdjustClientRect, which enlarges the rect (for forms).
  8538. TRect R = GetClientRect();
  8539. // when restoring from iconic state, place no restrictions
  8540. if (IsRectEmpty(R))
  8541. {
  8542. MinWidth = 0;
  8543. MinHeight = 0;
  8544. MaxWidth = 32000;
  8545. MaxHeight = 32000;
  8546. }
  8547. }
  8548. //---------------------------------------------------------------------------
  8549. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8550. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8551. {
  8552. // terminal can be already closed (e.g. dropped connection)
  8553. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8554. {
  8555. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8556. try
  8557. {
  8558. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8559. }
  8560. catch(...)
  8561. {
  8562. if (!Terminal->Active)
  8563. {
  8564. Close = true;
  8565. }
  8566. throw;
  8567. }
  8568. }
  8569. }
  8570. //---------------------------------------------------------------------------
  8571. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8572. {
  8573. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8574. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8575. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8576. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8577. {
  8578. OnGetSpaceAvailable = GetSpaceAvailable;
  8579. }
  8580. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8581. OnGetSpaceAvailable);
  8582. }
  8583. //---------------------------------------------------------------------------
  8584. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8585. {
  8586. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8587. Terminal->FillSessionDataForCode(Data.get());
  8588. return Data.release();
  8589. }
  8590. //---------------------------------------------------------------------------
  8591. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8592. {
  8593. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8594. DoGenerateUrlDialog(Data.get(), Paths);
  8595. }
  8596. //---------------------------------------------------------------------------
  8597. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8598. {
  8599. GenerateUrl(NULL);
  8600. }
  8601. //---------------------------------------------------------------------------
  8602. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8603. {
  8604. std::unique_ptr<TStrings> Paths(new TStringList());
  8605. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8606. GenerateUrl(Paths.get());
  8607. }
  8608. //---------------------------------------------------------------------------
  8609. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  8610. {
  8611. FSessionColor = value;
  8612. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8613. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8614. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8615. DebugAssert(ColorItem != NULL);
  8616. ColorItem->Color = C;
  8617. NonVisualDataModule->ColorMenuItem->Color = C;
  8618. UpdateControls();
  8619. }
  8620. //---------------------------------------------------------------------------
  8621. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8622. {
  8623. if (value != FSessionColor)
  8624. {
  8625. UpdateSessionColor(value);
  8626. // Is null when called from LastTerminalClosed
  8627. if (Terminal != NULL)
  8628. {
  8629. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8630. }
  8631. }
  8632. }
  8633. //---------------------------------------------------------------------------
  8634. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8635. {
  8636. bool Result = FNoteTimer->Enabled;
  8637. if (Result)
  8638. {
  8639. // cannot cancel note too early
  8640. bool NotEarly =
  8641. (Now() - FNoteShown >
  8642. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8643. if (Force || NotEarly)
  8644. {
  8645. FNoteTimer->Enabled = false;
  8646. FNote = L"";
  8647. SAFE_DESTROY(FNoteData);
  8648. FOnNoteClick = NULL;
  8649. FNoteHints = FNotes->Text;
  8650. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8651. UpdateStatusBar();
  8652. }
  8653. }
  8654. return Result;
  8655. }
  8656. //---------------------------------------------------------------------------
  8657. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8658. {
  8659. DebugAssert(FNoteTimer->Enabled);
  8660. CancelNote(true);
  8661. }
  8662. //---------------------------------------------------------------------------
  8663. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8664. {
  8665. int P = Note.Pos(L"\n");
  8666. if (P > 0)
  8667. {
  8668. Note.SetLength(P - 1);
  8669. }
  8670. FNotes->Add(FORMAT(L"[%s] %s",
  8671. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8672. while (FNotes->Count > 10)
  8673. {
  8674. FNotes->Delete(0);
  8675. }
  8676. if (UpdateNow)
  8677. {
  8678. FNoteHints = FNotes->Text;
  8679. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8680. UpdateStatusBar();
  8681. }
  8682. }
  8683. //---------------------------------------------------------------------------
  8684. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8685. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8686. {
  8687. int P = Note.Pos(L"\n");
  8688. if (P > 0)
  8689. {
  8690. Note.SetLength(P - 1);
  8691. }
  8692. FNoteHints = FNotes->Text;
  8693. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8694. FNote = Note;
  8695. SAFE_DESTROY(FNoteData);
  8696. FNoteData = NoteData;
  8697. FOnNoteClick = OnNoteClick;
  8698. AddNote(Note, false);
  8699. UpdateStatusBar();
  8700. FNoteShown = Now();
  8701. FNoteTimer->Enabled = false;
  8702. if (Seconds == 0)
  8703. {
  8704. Seconds = WinConfiguration->NotificationsTimeout;
  8705. }
  8706. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8707. FNoteTimer->Enabled = true;
  8708. }
  8709. //---------------------------------------------------------------------------
  8710. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8711. {
  8712. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8713. }
  8714. //---------------------------------------------------------------------------
  8715. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8716. {
  8717. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  8718. {
  8719. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8720. }
  8721. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  8722. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  8723. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  8724. 0, NULL, NULL);
  8725. }
  8726. //---------------------------------------------------------------------------
  8727. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  8728. {
  8729. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  8730. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  8731. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  8732. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  8733. }
  8734. //---------------------------------------------------------------------------
  8735. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  8736. {
  8737. WinConfiguration->FormatSizeBytes = Style;
  8738. }
  8739. //---------------------------------------------------------------------------
  8740. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  8741. {
  8742. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  8743. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  8744. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  8745. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  8746. }
  8747. //---------------------------------------------------------------------------
  8748. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  8749. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  8750. {
  8751. if (Panel->Index == 0)
  8752. {
  8753. if (FOnNoteClick != NULL)
  8754. {
  8755. // prevent the user data from being freed by possible call
  8756. // to CancelNote or PostNote during call to OnNoteClick
  8757. std::unique_ptr<TObject> NoteData(FNoteData);
  8758. TNotifyEvent OnNoteClick = FOnNoteClick;
  8759. FNoteData = NULL;
  8760. // need to cancel the note as we are going to delete its user data
  8761. CancelNote(true);
  8762. OnNoteClick(NoteData.get());
  8763. }
  8764. }
  8765. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  8766. {
  8767. FileSystemInfo();
  8768. }
  8769. }
  8770. //---------------------------------------------------------------------------
  8771. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  8772. {
  8773. // workaround:
  8774. // 1) for unknown reason, disabling window, while minimized,
  8775. // prevents it from restoring, even if it was enabled again meanwhile
  8776. // 2) when disabling the main window, while another has focus
  8777. // minimize is no longer possible
  8778. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  8779. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  8780. // Shouldn't we use IsApplicationMinimized() here?
  8781. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  8782. {
  8783. Enabled = false;
  8784. }
  8785. FLockLevel++;
  8786. }
  8787. //---------------------------------------------------------------------------
  8788. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  8789. {
  8790. DebugAssert(FLockLevel > 0);
  8791. FLockLevel--;
  8792. if (FLockLevel == 0)
  8793. {
  8794. DebugAssert(FLockSuspendLevel == 0);
  8795. Enabled = true;
  8796. }
  8797. }
  8798. //---------------------------------------------------------------------------
  8799. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  8800. {
  8801. // We need to make sure that window is enabled when the last modal window closes
  8802. // otherwise focus is not restored correctly.
  8803. // So we re-enable the window when modal window opens and
  8804. // disable it back after is closes
  8805. if (FLockLevel > 0)
  8806. {
  8807. // while we have nesting counter, we know that we never be called
  8808. // recursivelly as Application->OnModalBegin is called only
  8809. // for the top-level modal window
  8810. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8811. {
  8812. // won't be disabled when conditions in LockWindow() were not satisfied
  8813. FDisabledOnLockSuspend = !Enabled;
  8814. // When minimized to tray (or actually when set to SW_HIDE),
  8815. // setting Enabled makes the window focusable even when there's
  8816. // modal window over it
  8817. if (!FTrayIcon->Visible)
  8818. {
  8819. Enabled = true;
  8820. }
  8821. }
  8822. FLockSuspendLevel++;
  8823. }
  8824. }
  8825. //---------------------------------------------------------------------------
  8826. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  8827. {
  8828. if (FLockSuspendLevel > 0)
  8829. {
  8830. DebugAssert(FLockLevel > 0);
  8831. FLockSuspendLevel--;
  8832. // see comment in SuspendWindowLock
  8833. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8834. {
  8835. // Note that window can be enabled here, when we were minized to tray when
  8836. // was SuspendWindowLock() called.
  8837. // We should possibly do the same check as in LockWindow(),
  8838. // if it is ever possible that the consitions change between
  8839. // SuspendWindowLock() and ResumeWindowLock()
  8840. Enabled = !FDisabledOnLockSuspend;
  8841. }
  8842. }
  8843. }
  8844. //---------------------------------------------------------------------------
  8845. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  8846. {
  8847. if (!TextOnly)
  8848. {
  8849. TTBXComboBoxItem * RemotePathComboBox =
  8850. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  8851. TStrings * Items = RemotePathComboBox->Strings;
  8852. Items->BeginUpdate();
  8853. try
  8854. {
  8855. Items->Clear();
  8856. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  8857. {
  8858. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8859. while (!IsUnixRootPath(APath))
  8860. {
  8861. int P = APath.LastDelimiter(L'/');
  8862. DebugAssert(P >= 0);
  8863. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  8864. APath.SetLength(P - 1);
  8865. }
  8866. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  8867. }
  8868. }
  8869. __finally
  8870. {
  8871. RemotePathComboBox->ItemIndex = Items->Count - 1;
  8872. // Setting ItemIndex to -1 does not reset its text
  8873. if (Items->Count == 0)
  8874. {
  8875. RemotePathComboBox->Text = L"";
  8876. }
  8877. Items->EndUpdate();
  8878. }
  8879. }
  8880. }
  8881. //---------------------------------------------------------------------------
  8882. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  8883. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  8884. int & ImageIndex)
  8885. {
  8886. if (AIndex < 0)
  8887. {
  8888. AIndex = Sender->ItemIndex;
  8889. }
  8890. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  8891. }
  8892. //---------------------------------------------------------------------------
  8893. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  8894. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  8895. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  8896. {
  8897. ARect.Left += (10 * AIndex);
  8898. }
  8899. //---------------------------------------------------------------------------
  8900. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  8901. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  8902. {
  8903. AWidth += (10 * AIndex);
  8904. }
  8905. //---------------------------------------------------------------------------
  8906. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  8907. TObject * Sender)
  8908. {
  8909. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  8910. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8911. int Index = RemotePathComboBox->ItemIndex;
  8912. while (Index < RemotePathComboBox->Strings->Count - 1)
  8913. {
  8914. APath = UnixExtractFileDir(APath);
  8915. Index++;
  8916. }
  8917. // VanDyke style paths
  8918. if (APath.IsEmpty())
  8919. {
  8920. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  8921. APath = ROOTDIRECTORY;
  8922. }
  8923. if (RemoteDirView->Path != APath)
  8924. {
  8925. RemoteDirView->Path = APath;
  8926. }
  8927. }
  8928. //---------------------------------------------------------------------------
  8929. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  8930. {
  8931. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  8932. DebugUsedParam(Sender);
  8933. UpdateRemotePathComboBox(true);
  8934. }
  8935. //---------------------------------------------------------------------------
  8936. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  8937. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  8938. {
  8939. ResetIncrementalSearch();
  8940. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8941. DebugAssert(DirView != NULL);
  8942. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  8943. {
  8944. HWND Edit = ListView_GetEditControl(DirView->Handle);
  8945. // OnEditing is called also from TCustomListView::CanEdit
  8946. if (Edit != NULL)
  8947. {
  8948. EditSelectBaseName(Edit);
  8949. }
  8950. }
  8951. }
  8952. //---------------------------------------------------------------------------
  8953. //---------------------------------------------------------------------------
  8954. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  8955. {
  8956. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  8957. Result->AutoDetectDnD = false;
  8958. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  8959. Result->RenderDataOn = rdoEnterAndDropSync;
  8960. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  8961. return Result;
  8962. }
  8963. //---------------------------------------------------------------------------
  8964. void __fastcall TCustomScpExplorerForm::CreateWnd()
  8965. {
  8966. TForm::CreateWnd();
  8967. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  8968. if (FSessionsDragDropFilesEx == NULL)
  8969. {
  8970. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  8971. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  8972. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  8973. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  8974. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  8975. }
  8976. if (FQueueDragDropFilesEx == NULL)
  8977. {
  8978. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  8979. // No need to set OnDragOver as we do not have any restrictions
  8980. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  8981. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  8982. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  8983. }
  8984. }
  8985. //---------------------------------------------------------------------------
  8986. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  8987. {
  8988. TForm::DestroyWnd();
  8989. FSessionsDragDropFilesEx->DragDropControl = NULL;
  8990. FQueueDragDropFilesEx->DragDropControl = NULL;
  8991. }
  8992. //---------------------------------------------------------------------------
  8993. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  8994. {
  8995. SideEnter(FCurrentSide);
  8996. FEverShown = true;
  8997. }
  8998. //---------------------------------------------------------------------------
  8999. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9000. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9001. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9002. {
  9003. if (!NonVisualDataModule->Busy)
  9004. {
  9005. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9006. Configuration->Usage->Inc(L"FileFinds");
  9007. LockWindow(true);
  9008. NonVisualDataModule->StartBusy();
  9009. WinConfiguration->LockedInterface = true;
  9010. try
  9011. {
  9012. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9013. }
  9014. __finally
  9015. {
  9016. WinConfiguration->LockedInterface = false;
  9017. NonVisualDataModule->EndBusy();
  9018. UnlockWindow();
  9019. }
  9020. }
  9021. }
  9022. //---------------------------------------------------------------------------
  9023. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9024. {
  9025. if (!NonVisualDataModule->Busy)
  9026. {
  9027. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9028. SetFocus();
  9029. RemoteDirView->Path = UnixExtractFilePath(Path);
  9030. UnicodeString FileName = UnixExtractFileName(Path);
  9031. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9032. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9033. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9034. if (Item == NULL)
  9035. {
  9036. ReloadDirectory(osRemote);
  9037. Item = RemoteDirView->FindFileItem(FileName);
  9038. }
  9039. if (Item != NULL)
  9040. {
  9041. RemoteDirView->ItemFocused = Item;
  9042. RemoteDirView->ItemFocused->MakeVisible(false);
  9043. RemoteDirView->SetFocus();
  9044. }
  9045. }
  9046. }
  9047. //---------------------------------------------------------------------------
  9048. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9049. {
  9050. bool Result = !NonVisualDataModule->Busy;
  9051. if (Result)
  9052. {
  9053. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9054. }
  9055. return Result;
  9056. }
  9057. //---------------------------------------------------------------------------
  9058. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9059. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9060. {
  9061. if (CanOperateOnFoundFiles(ATerminal))
  9062. {
  9063. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9064. FOnFileOperationFinished = OnFileOperationFinished;
  9065. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9066. }
  9067. }
  9068. //---------------------------------------------------------------------------
  9069. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9070. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9071. {
  9072. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9073. }
  9074. //---------------------------------------------------------------------------
  9075. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9076. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9077. {
  9078. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9079. }
  9080. //---------------------------------------------------------------------------
  9081. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9082. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9083. {
  9084. if (CanOperateOnFoundFiles(ATerminal))
  9085. {
  9086. for (int Index = 0; Index < FileList->Count; Index++)
  9087. {
  9088. UnicodeString FileName = FileList->Strings[Index];
  9089. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9090. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9091. OnFileOperationFinished(FileName, true);
  9092. }
  9093. }
  9094. }
  9095. //---------------------------------------------------------------------------
  9096. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9097. {
  9098. FFileFindTerminal = Terminal;
  9099. ShowFileFindDialog(
  9100. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9101. DoDownloadFoundFiles, DoEditFoundFiles);
  9102. }
  9103. //---------------------------------------------------------------------------
  9104. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9105. {
  9106. FTaskbarList = TaskbarList;
  9107. }
  9108. //---------------------------------------------------------------------------
  9109. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9110. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9111. {
  9112. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9113. if (Index >= 0)
  9114. {
  9115. if (Button == mbLeft)
  9116. {
  9117. // "Mouse down" is raised only after tab is switched.
  9118. // If switching tab (switching session) takes long enough for user
  9119. // to actually release the button, "mouse down" is still raised,
  9120. // but we do not get "mouse up" event, so dragging is not cancelled,
  9121. // prevent that by not beginning dragging in the first place.
  9122. if (IsKeyPressed(VK_LBUTTON))
  9123. {
  9124. // when user clicks the "+", we get mouse down only after the session
  9125. // is closed, when new session tab is already on X:Y, so dragging
  9126. // starts, prevent that
  9127. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  9128. {
  9129. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9130. if (Terminal != NULL)
  9131. {
  9132. SessionsPageControl->BeginDrag(false);
  9133. }
  9134. }
  9135. }
  9136. }
  9137. else if (Button == mbMiddle)
  9138. {
  9139. // ignore middle-click for "New session tab"
  9140. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9141. if (Terminal != NULL)
  9142. {
  9143. CloseSessionTab(Index);
  9144. }
  9145. }
  9146. }
  9147. }
  9148. //---------------------------------------------------------------------------
  9149. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9150. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9151. {
  9152. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9153. // do not allow dropping on the "+" tab
  9154. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9155. if ((TargetTerminal != NULL) &&
  9156. (SessionsPageControl->ActivePage->PageIndex != Index))
  9157. {
  9158. Configuration->Usage->Inc(L"SessionTabMoves");
  9159. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  9160. // but we want to actually prevent that to avoid flicker
  9161. SessionsPageControl->ActivePage->PageIndex = Index;
  9162. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  9163. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9164. }
  9165. }
  9166. //---------------------------------------------------------------------------
  9167. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9168. TObject * Sender, TObject * Source, int X, int Y,
  9169. TDragState /*State*/, bool & Accept)
  9170. {
  9171. Accept = (Sender == Source);
  9172. if (Accept)
  9173. {
  9174. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9175. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9176. // do not allow dragging to the "+" tab
  9177. Accept = (Terminal != NULL);
  9178. }
  9179. }
  9180. //---------------------------------------------------------------------------
  9181. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(int /*KeyState*/,
  9182. const TPoint & Point, int & Effect)
  9183. {
  9184. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9185. if (Index < 0)
  9186. {
  9187. Effect = DROPEFFECT_NONE;
  9188. }
  9189. else
  9190. {
  9191. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9192. // do not allow dropping on the "+" tab
  9193. if (TargetTerminal == NULL)
  9194. {
  9195. Effect = DROPEFFECT_NONE;
  9196. }
  9197. else if ((TargetTerminal != Terminal) && (Effect == DROPEFFECT_MOVE))
  9198. {
  9199. Effect = DROPEFFECT_COPY;
  9200. }
  9201. }
  9202. }
  9203. //---------------------------------------------------------------------------
  9204. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9205. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9206. {
  9207. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9208. // do not allow dropping on the "+" tab
  9209. TManagedTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9210. if (TargetTerminal != NULL)
  9211. {
  9212. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9213. if (!IsFileControl(DropSourceControl, osRemote))
  9214. {
  9215. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  9216. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9217. // Why don't we use Terminal->CurrentDirectory directly?
  9218. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9219. // do not force queue, drag drop
  9220. false, true);
  9221. }
  9222. }
  9223. }
  9224. //---------------------------------------------------------------------------
  9225. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9226. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9227. {
  9228. // Downloads are handled in RemoteFileControlDDEnd
  9229. if (!IsFileControl(DropSourceControl, osRemote))
  9230. {
  9231. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9232. Terminal->CurrentDirectory,
  9233. // force queue, drag drop
  9234. true, true);
  9235. }
  9236. }
  9237. //---------------------------------------------------------------------------
  9238. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9239. {
  9240. FShowing = false;
  9241. // Do not save empty workspace
  9242. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  9243. {
  9244. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9245. UnicodeString Name = WorkspaceName();
  9246. DoSaveWorkspace(
  9247. Name, DataList.get(),
  9248. !Configuration->DisablePasswordStoring &&
  9249. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9250. WinConfiguration->LastStoredSession = Name;
  9251. }
  9252. }
  9253. //---------------------------------------------------------------------------
  9254. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9255. {
  9256. TManagedTerminal * ManagedTerminal =
  9257. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9258. if (ManagedTerminal != NULL)
  9259. {
  9260. ManagedTerminal->DirectoryLoaded = Now();
  9261. }
  9262. }
  9263. //---------------------------------------------------------------------------
  9264. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9265. TListItem * /*Item*/, bool /*Selected*/)
  9266. {
  9267. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9268. switch (DirView->LastSelectMethod)
  9269. {
  9270. case smKeyboard:
  9271. Configuration->Usage->Inc(L"KeyboardSelections");
  9272. break;
  9273. case smMouse:
  9274. Configuration->Usage->Inc(L"MouseSelections");
  9275. break;
  9276. }
  9277. }
  9278. //---------------------------------------------------------------------------
  9279. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9280. {
  9281. if (Color != 0)
  9282. {
  9283. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9284. }
  9285. else
  9286. {
  9287. return FSessionTabImageIndex;
  9288. }
  9289. }
  9290. //---------------------------------------------------------------------------
  9291. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9292. {
  9293. TPngImageCollectionItem * Item =
  9294. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9295. FSessionColors->AddPng(Item->PngImage);
  9296. return FSessionColors->Count - 1;
  9297. }
  9298. //---------------------------------------------------------------------------
  9299. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9300. {
  9301. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9302. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  9303. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9304. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9305. }
  9306. //---------------------------------------------------------------------------
  9307. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9308. {
  9309. for (int Index = 0; Index < Control->ControlCount; Index++)
  9310. {
  9311. TControl * AControl = Control->Controls[Index];
  9312. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9313. if (WinControl != NULL)
  9314. {
  9315. CollectItemsWithTextDisplayMode(WinControl);
  9316. }
  9317. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9318. if (Toolbar != NULL)
  9319. {
  9320. // we care for top-level items only
  9321. TTBCustomItem * Items = Toolbar->Items;
  9322. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9323. {
  9324. TTBCustomItem * Item = Items->Items[ItemIndex];
  9325. if (((Item->DisplayMode == nbdmImageAndText) ||
  9326. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9327. EligibleForImageDisplayMode(Item))
  9328. {
  9329. FItemsWithTextDisplayMode.insert(Item);
  9330. }
  9331. }
  9332. }
  9333. }
  9334. }
  9335. //---------------------------------------------------------------------------
  9336. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9337. {
  9338. return true;
  9339. }
  9340. //---------------------------------------------------------------------------
  9341. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9342. {
  9343. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9344. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9345. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9346. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9347. bool Result = true;
  9348. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9349. {
  9350. TTBCustomItem * Item = *i;
  9351. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9352. if (Label != NULL)
  9353. {
  9354. // optimization
  9355. if (Label->Visible == SelectiveToolbarText)
  9356. {
  9357. Result = false;
  9358. }
  9359. else
  9360. {
  9361. Label->Visible = SelectiveToolbarText;
  9362. }
  9363. }
  9364. else
  9365. {
  9366. // optimization
  9367. if (Item->DisplayMode == DisplayMode)
  9368. {
  9369. Result = false;
  9370. }
  9371. else
  9372. {
  9373. Item->DisplayMode = DisplayMode;
  9374. }
  9375. }
  9376. ++i;
  9377. }
  9378. if (Result)
  9379. {
  9380. UpdateNewSessionTab();
  9381. }
  9382. return Result;
  9383. }
  9384. //---------------------------------------------------------------------------
  9385. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9386. {
  9387. DebugFail();
  9388. }
  9389. //---------------------------------------------------------------------------
  9390. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9391. {
  9392. // Among other, a lock level is non zero, while directory is being loaded,
  9393. // even when it happens as a result of dirview navigation,
  9394. // i.e. when TNonVisualDataModule is NOT FBusy.
  9395. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9396. // Among other this prevents a panel auto update to occur while
  9397. // directory is changing.
  9398. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9399. }
  9400. //---------------------------------------------------------------------------
  9401. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  9402. {
  9403. // While the window is disabled, we still seem to process menu shortcuts at least,
  9404. // so stop it at least here.
  9405. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9406. return
  9407. (Allowed == aaUpdate) ||
  9408. !NonVisualDataModule->Busy;
  9409. }
  9410. //---------------------------------------------------------------------------
  9411. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9412. {
  9413. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9414. }
  9415. //---------------------------------------------------------------------------
  9416. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9417. {
  9418. // This is somewhat redundant to LockWindow() call from
  9419. // TTerminalManager::TerminalReadDirectoryProgress.
  9420. // But disabling window is known not to block keyboard shorcuts
  9421. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9422. if (Busy > 0)
  9423. {
  9424. if (NonVisualDataModule->Busy)
  9425. {
  9426. State = false;
  9427. }
  9428. else
  9429. {
  9430. NonVisualDataModule->StartBusy();
  9431. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9432. LockWindow();
  9433. }
  9434. }
  9435. else if (Busy < 0)
  9436. {
  9437. UnlockWindow();
  9438. NonVisualDataModule->EndBusy();
  9439. }
  9440. else
  9441. {
  9442. State = NonVisualDataModule->Busy;
  9443. }
  9444. }
  9445. //---------------------------------------------------------------------------
  9446. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9447. {
  9448. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9449. // no context menu for "New session tab"
  9450. if ((Index >= 0) && (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != NULL))
  9451. {
  9452. SessionsPageControl->ActivePageIndex = Index;
  9453. if (DebugAlwaysTrue(SessionTabSwitched()))
  9454. {
  9455. // copied from TControl.WMContextMenu
  9456. SendCancelMode(SessionsPageControl);
  9457. // explicit popup instead of using PopupMenu property
  9458. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9459. // while connecting yet not-connected session and hence
  9460. // allowing an access to commands over not-completelly connected session
  9461. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9462. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  9463. PopupMenu->PopupComponent = SessionsPageControl;
  9464. PopupMenu->Popup(Point.x, Point.y);
  9465. }
  9466. }
  9467. Handled = true;
  9468. }
  9469. //---------------------------------------------------------------------------
  9470. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9471. {
  9472. return (Terminal != NULL) && Terminal->Active && Terminal->IsCapable[fcChangePassword];
  9473. }
  9474. //---------------------------------------------------------------------------
  9475. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9476. {
  9477. Configuration->Usage->Inc(L"PasswordChanges");
  9478. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9479. ChangePasswordData->DisableAuthentationsExceptPassword();
  9480. ChangePasswordData->ChangePassword = true;
  9481. std::unique_ptr<TTerminal> ChangePasswordSession(
  9482. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9483. ChangePasswordSession->AutoReadDirectory = false;
  9484. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9485. {
  9486. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9487. }
  9488. else
  9489. {
  9490. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9491. }
  9492. }
  9493. //---------------------------------------------------------------------------
  9494. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9495. {
  9496. // No nice way to assert SSH2
  9497. return (Terminal != NULL) && Terminal->Active && (Terminal->FSProtocol == cfsSFTP);
  9498. }
  9499. //---------------------------------------------------------------------------
  9500. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9501. {
  9502. TOperationVisualizer OperationVisualizer;
  9503. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9504. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9505. }
  9506. //---------------------------------------------------------------------------
  9507. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9508. {
  9509. TForm::ChangeScale(M, D);
  9510. int APixelsPerInch = GetControlPixelsPerInch(this);
  9511. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9512. try
  9513. {
  9514. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9515. }
  9516. __finally
  9517. {
  9518. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9519. }
  9520. }
  9521. //---------------------------------------------------------------------------
  9522. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9523. {
  9524. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9525. }
  9526. //---------------------------------------------------------------------
  9527. class TPublicControl : public TControl
  9528. {
  9529. friend class TCustomScpExplorerForm;
  9530. };
  9531. //---------------------------------------------------------------------------
  9532. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9533. {
  9534. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9535. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9536. }
  9537. //---------------------------------------------------------------------------
  9538. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9539. {
  9540. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9541. OpenBookmark(Side, Bookmark);
  9542. }
  9543. //---------------------------------------------------------------------------
  9544. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9545. {
  9546. DoBookmarkClick(osLocal, Sender);
  9547. }
  9548. //---------------------------------------------------------------------------
  9549. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9550. {
  9551. DoBookmarkClick(osRemote, Sender);
  9552. }
  9553. //---------------------------------------------------------------------------
  9554. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9555. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9556. {
  9557. // TODO
  9558. if (BookmarkList != NULL)
  9559. {
  9560. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9561. if (!WinConfiguration->UseLocationProfiles)
  9562. {
  9563. std::unique_ptr<TStringList> Directories(new TStringList());
  9564. Directories->CaseSensitive = (Side == osRemote);
  9565. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9566. {
  9567. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9568. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9569. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9570. {
  9571. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9572. Item->Caption = EscapeHotkey(Directory);
  9573. Item->ShortCut = Bookmark->ShortCut;
  9574. Item->OnClick = OnBookmarkClick;
  9575. Item->Tag = reinterpret_cast<int>(Bookmark);
  9576. Directories->Add(Directory);
  9577. Menu->Add(Item.release());
  9578. }
  9579. }
  9580. }
  9581. else
  9582. {
  9583. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9584. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9585. {
  9586. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9587. if (!Bookmark->Node.IsEmpty())
  9588. {
  9589. Folders->Add(Bookmark->Node);
  9590. }
  9591. }
  9592. for (int Index = 0; Index < Folders->Count; Index++)
  9593. {
  9594. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9595. Item->Caption = Folders->Strings[Index];
  9596. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction->ImageIndex;
  9597. Folders->Objects[Index] = Item.get();
  9598. Menu->Add(Item.release());
  9599. }
  9600. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9601. {
  9602. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9603. TTBCustomItem * Parent;
  9604. if (!Bookmark->Node.IsEmpty())
  9605. {
  9606. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9607. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9608. }
  9609. else
  9610. {
  9611. Parent = Menu;
  9612. }
  9613. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9614. Item->Caption = Bookmark->Name;
  9615. Item->ShortCut = Bookmark->ShortCut;
  9616. Item->OnClick = OnBookmarkClick;
  9617. Item->Tag = reinterpret_cast<int>(Bookmark);
  9618. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9619. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9620. {
  9621. // No hint for location profiles that are actually mere "bookmarks"
  9622. }
  9623. else
  9624. {
  9625. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  9626. UnicodeString LongHint;
  9627. if (!Bookmark->Local.IsEmpty())
  9628. {
  9629. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  9630. }
  9631. if (!Bookmark->Remote.IsEmpty())
  9632. {
  9633. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  9634. }
  9635. AddToList(Hint, LongHint, L"|");
  9636. Item->Hint = Hint;
  9637. }
  9638. Parent->Add(Item.release());
  9639. }
  9640. }
  9641. AddMenuSeparator(Menu);
  9642. }
  9643. }
  9644. //---------------------------------------------------------------------------
  9645. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  9646. {
  9647. Menu->Clear();
  9648. std::unique_ptr<TTBCustomItem> Item;
  9649. Item.reset(new TTBXItem(Owner));
  9650. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  9651. Item->Options = Item->Options << tboDefault;
  9652. Menu->Add(Item.release());
  9653. AddMenuSeparator(Menu);
  9654. if (Terminal != NULL)
  9655. {
  9656. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  9657. }
  9658. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  9659. Item.reset(new TTBXItem(Owner));
  9660. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction : NonVisualDataModule->RemoteAddBookmarkAction;
  9661. Menu->Add(Item.release());
  9662. }
  9663. //---------------------------------------------------------------------------
  9664. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  9665. {
  9666. bool Result = true;
  9667. try
  9668. {
  9669. if (Side == osRemote)
  9670. {
  9671. Terminal->ExceptionOnFail = true;
  9672. }
  9673. try
  9674. {
  9675. DirView(Side)->Path = Path;
  9676. }
  9677. __finally
  9678. {
  9679. if (Side == osRemote)
  9680. {
  9681. Terminal->ExceptionOnFail = false;
  9682. }
  9683. }
  9684. }
  9685. catch (Exception & E)
  9686. {
  9687. if ((Side != osRemote) || Terminal->Active)
  9688. {
  9689. ShowExtendedExceptionEx(Terminal, &E);
  9690. Result = false;
  9691. }
  9692. else
  9693. {
  9694. throw;
  9695. }
  9696. }
  9697. return Result;
  9698. }
  9699. //---------------------------------------------------------------------------
  9700. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  9701. {
  9702. // Make sure there some feedback even when loading tiny local folders
  9703. TInstantOperationVisualizer Visualizer;
  9704. DirView(Side)->ReloadDirectory();
  9705. }
  9706. //---------------------------------------------------------------------------
  9707. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  9708. {
  9709. if (Index == SessionsPageControl->TabIndex)
  9710. {
  9711. CloseSession();
  9712. }
  9713. else
  9714. {
  9715. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  9716. TTerminal * Terminal = GetSessionTabTerminal(TabSheet);
  9717. TTerminalManager * Manager = TTerminalManager::Instance();
  9718. if (!Terminal->Active)
  9719. {
  9720. Manager->FreeTerminal(Terminal);
  9721. }
  9722. else
  9723. {
  9724. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  9725. if (CanCloseQueue(Queue))
  9726. {
  9727. Manager->FreeTerminal(Terminal);
  9728. }
  9729. }
  9730. }
  9731. }
  9732. //---------------------------------------------------------------------------
  9733. void __fastcall TCustomScpExplorerForm::SessionsPageControlCloseButtonClick(TPageControl * /*Sender*/, int Index)
  9734. {
  9735. CloseSessionTab(Index);
  9736. }
  9737. //---------------------------------------------------------------------------
  9738. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  9739. {
  9740. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  9741. {
  9742. TInstantOperationVisualizer Visualizer;
  9743. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  9744. // before we create a new data object
  9745. ClipboardClear();
  9746. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  9747. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  9748. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  9749. // Intermediate variable to WORKAROUND a crash in compiler
  9750. TObjectList * ClipboardFakeMonitors =
  9751. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  9752. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  9753. if (FClipboardDragDropFilesEx.get() == NULL)
  9754. {
  9755. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  9756. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  9757. }
  9758. FClipboardDragDropFilesEx->FileList->Clear();
  9759. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  9760. FClipboardDragDropFilesEx->CopyToClipboard();
  9761. FClipboardTerminal = FTerminal;
  9762. // Need full paths, as cwd can be different once files are pasted
  9763. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  9764. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  9765. {
  9766. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  9767. }
  9768. }
  9769. }
  9770. //---------------------------------------------------------------------------
  9771. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  9772. {
  9773. if (OpenClipboard(0))
  9774. {
  9775. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  9776. CloseClipboard();
  9777. }
  9778. }
  9779. //---------------------------------------------------------------------------
  9780. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  9781. {
  9782. return !FClipboardFakeDirectory.IsEmpty();
  9783. }
  9784. //---------------------------------------------------------------------------
  9785. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  9786. {
  9787. RemoveDir(ApiPath(FClipboardFakeDirectory));
  9788. FClipboardFakeDirectory = UnicodeString();
  9789. FClipboardTerminal = NULL;
  9790. // Also called in Idle()
  9791. FClipboardFakeMonitors.reset(NULL);
  9792. }
  9793. //---------------------------------------------------------------------------
  9794. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  9795. {
  9796. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  9797. {
  9798. ClipboardStop();
  9799. }
  9800. }
  9801. //---------------------------------------------------------------------------
  9802. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  9803. {
  9804. TTerminalManager * Manager = TTerminalManager::Instance();
  9805. Manager->ActiveTerminal = FClipboardTerminal;
  9806. TTransferOperationParam Params;
  9807. Params.TargetDirectory = TargetDirectory;
  9808. Params.DragDrop = DragDrop;
  9809. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  9810. }
  9811. //---------------------------------------------------------------------------
  9812. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  9813. {
  9814. // It can actually rarelly happen that some random file is created, while we are shuttting down the monitor
  9815. // (as it pumps a Windows message queue while being shutted down)
  9816. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  9817. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  9818. {
  9819. // Can fail as it can be e.g. locked by AV, so we retry that later
  9820. bool Removed = RemoveDir(ApiPath(FileName));
  9821. try
  9822. {
  9823. if (!NonVisualDataModule->Busy)
  9824. {
  9825. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  9826. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  9827. }
  9828. }
  9829. __finally
  9830. {
  9831. if (!Removed)
  9832. {
  9833. RemoveDir(ApiPath(FileName));
  9834. }
  9835. }
  9836. }
  9837. }
  9838. //---------------------------------------------------------------------------
  9839. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  9840. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  9841. {
  9842. TFileMasks::TParams MaskParams;
  9843. MaskParams.Size = Size;
  9844. MaskParams.Modification = Modification;
  9845. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  9846. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  9847. {
  9848. bool ImplicitMatch;
  9849. if (Iter->FileMask.Matches(FileName, Directory, DirView->PathName, &MaskParams, false, ImplicitMatch) &&
  9850. !ImplicitMatch)
  9851. {
  9852. Color = Iter->Color;
  9853. break;
  9854. }
  9855. }
  9856. }
  9857. //---------------------------------------------------------------------------
  9858. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  9859. {
  9860. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9861. if (!DirView->IsEditing())
  9862. {
  9863. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  9864. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  9865. // while searching
  9866. if (Key == VK_BACK)
  9867. {
  9868. if (!FIncrementalSearch.IsEmpty())
  9869. {
  9870. if (FIncrementalSearch.Length() == 1)
  9871. {
  9872. ResetIncrementalSearch();
  9873. }
  9874. else
  9875. {
  9876. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  9877. IncrementalSearch(NewText, false, false);
  9878. }
  9879. Key = 0;
  9880. }
  9881. }
  9882. }
  9883. }
  9884. //---------------------------------------------------------------------------
  9885. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  9886. {
  9887. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9888. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  9889. {
  9890. // Filter control sequences.
  9891. // When not searching yet, prefer use of the space for toggling file selection
  9892. // (so we cannot incrementally search for a string starting with a space).
  9893. if ((Key > VK_SPACE) ||
  9894. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  9895. {
  9896. IncrementalSearch(FIncrementalSearch + Key, false, false);
  9897. Key = 0;
  9898. }
  9899. }
  9900. }
  9901. //---------------------------------------------------------------------------
  9902. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  9903. {
  9904. if (!FIncrementalSearch.IsEmpty())
  9905. {
  9906. FIncrementalSearch = L"";
  9907. DirView(osCurrent)->UpdateStatusBar();
  9908. }
  9909. }
  9910. //---------------------------------------------------------------------------
  9911. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9912. {
  9913. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  9914. if (Item == NULL)
  9915. {
  9916. MessageBeep(MB_ICONHAND);
  9917. }
  9918. else
  9919. {
  9920. TCustomDirView * ADirView = DirView(osCurrent);
  9921. {
  9922. TAutoNestingCounter Guard(FIncrementalSearching);
  9923. ADirView->FocusItem(Item);
  9924. if (ADirView->NortonLike == nlOff)
  9925. {
  9926. if ((ADirView->Selected != Item) ||
  9927. (ADirView->SelCount > 1))
  9928. {
  9929. ADirView->ClearSelection();
  9930. ADirView->Selected = Item;
  9931. }
  9932. }
  9933. }
  9934. FIncrementalSearch = Text;
  9935. Item->MakeVisible(false);
  9936. TListItem * NextItem = SearchFile(Text, true, Reverse);
  9937. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  9938. ADirView->UpdateStatusBar();
  9939. }
  9940. }
  9941. //---------------------------------------------------------------------------
  9942. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  9943. {
  9944. int Index = Item->Index;
  9945. if (!Reverse)
  9946. {
  9947. Index++;
  9948. if (Index >= Item->Owner->Count)
  9949. {
  9950. Index = 0;
  9951. }
  9952. }
  9953. else
  9954. {
  9955. Index--;
  9956. if (Index < 0)
  9957. {
  9958. Index = Item->Owner->Count - 1;
  9959. }
  9960. }
  9961. return Item->Owner->Item[Index];
  9962. }
  9963. //---------------------------------------------------------------------------
  9964. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9965. {
  9966. TCustomDirView * ADirView = DirView(osCurrent);
  9967. if (ADirView->Items->Count == 0)
  9968. {
  9969. return NULL;
  9970. }
  9971. else
  9972. {
  9973. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  9974. TListItem * Item = CurrentItem;
  9975. if (SkipCurrent)
  9976. {
  9977. Item = DebugNotNull(GetNextFile(Item, Reverse));
  9978. }
  9979. while (true)
  9980. {
  9981. bool Matches = false;
  9982. switch (WinConfiguration->PanelSearch)
  9983. {
  9984. case isNameStartOnly:
  9985. Matches = ContainsTextSemiCaseSensitive(Item->Caption.SubString(1, Text.Length()), Text);
  9986. break;
  9987. case isName:
  9988. Matches = ContainsTextSemiCaseSensitive(Item->Caption, Text);
  9989. break;
  9990. case isAll:
  9991. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  9992. int Index = 0;
  9993. while ((Index < ColCount) && !Matches)
  9994. {
  9995. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  9996. if (Visible)
  9997. {
  9998. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  9999. }
  10000. Index++;
  10001. }
  10002. break;
  10003. }
  10004. if (Matches)
  10005. {
  10006. return Item;
  10007. }
  10008. Item = GetNextFile(Item, Reverse);
  10009. if (Item == CurrentItem)
  10010. {
  10011. return NULL;
  10012. }
  10013. }
  10014. }
  10015. }
  10016. //---------------------------------------------------------------------------
  10017. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10018. {
  10019. ResetIncrementalSearch();
  10020. }
  10021. //---------------------------------------------------------------------------
  10022. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10023. {
  10024. if (Message.CharCode == VK_TAB)
  10025. {
  10026. if (!FIncrementalSearch.IsEmpty())
  10027. {
  10028. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10029. bool Reverse = Shift.Contains(ssShift);
  10030. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10031. Message.Result = 1;
  10032. return;
  10033. }
  10034. }
  10035. else if (Message.CharCode == VK_ESCAPE)
  10036. {
  10037. if (!FIncrementalSearch.IsEmpty())
  10038. {
  10039. ResetIncrementalSearch();
  10040. Message.Result = 1;
  10041. return;
  10042. }
  10043. }
  10044. TForm::Dispatch(&Message);
  10045. }
  10046. //---------------------------------------------------------------------------
  10047. void __fastcall TCustomScpExplorerForm::Deactivate()
  10048. {
  10049. TForm::Deactivate();
  10050. // E.g. when switching to an internal editor window
  10051. ResetIncrementalSearch();
  10052. }
  10053. //---------------------------------------------------------------------------
  10054. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10055. {
  10056. // When switching to another application
  10057. ResetIncrementalSearch();
  10058. }
  10059. //---------------------------------------------------------------------------
  10060. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10061. {
  10062. ResetIncrementalSearch();
  10063. }
  10064. //---------------------------------------------------------------------------
  10065. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10066. {
  10067. if (FIncrementalSearching <= 0)
  10068. {
  10069. ResetIncrementalSearch();
  10070. }
  10071. }
  10072. //---------------------------------------------------------------------------
  10073. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10074. {
  10075. CountClicksForWindowPrint(this);
  10076. }
  10077. //---------------------------------------------------------------------------
  10078. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10079. {
  10080. CenterReconnectToolbar();
  10081. }
  10082. //---------------------------------------------------------------------------
  10083. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10084. {
  10085. if (RemoteDirView->ItemFocused != NULL)
  10086. {
  10087. RemoteDirView->ItemFocused->Selected = true;
  10088. }
  10089. }
  10090. //---------------------------------------------------------------------------