CustomScpExplorer.cpp 350 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699
  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. //---------------------------------------------------------------------------
  34. #pragma package(smart_init)
  35. #pragma link "CustomDirView"
  36. #pragma link "CustomUnixDirView"
  37. #pragma link "IEListView"
  38. #pragma link "NortonLikeListView"
  39. #pragma link "UnixDirView"
  40. #pragma link "CustomDriveView"
  41. #pragma link "UnixDriveView"
  42. #pragma link "CustomDriveView"
  43. #pragma link "TB2Dock"
  44. #pragma link "TBXStatusBars"
  45. #pragma link "TB2Item"
  46. #pragma link "TB2Toolbar"
  47. #pragma link "ThemePageControl"
  48. #pragma link "PathLabel"
  49. #ifndef NO_RESOURCES
  50. #pragma resource "*.dfm"
  51. #endif
  52. //---------------------------------------------------------------------------
  53. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  54. static const int SessionPanelCount = 4;
  55. //---------------------------------------------------------------------------
  56. class TMutexGuard
  57. {
  58. public:
  59. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  60. int Timeout = 5000)
  61. {
  62. FMutex = NULL;
  63. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  64. if (WaitResult == WAIT_TIMEOUT)
  65. {
  66. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  67. }
  68. else
  69. {
  70. FMutex = AMutex;
  71. }
  72. }
  73. ~TMutexGuard()
  74. {
  75. if (FMutex != NULL)
  76. {
  77. ReleaseMutex(FMutex);
  78. }
  79. }
  80. private:
  81. HANDLE FMutex;
  82. };
  83. //---------------------------------------------------------------------------
  84. class TWindowLock
  85. {
  86. public:
  87. TWindowLock(TCustomScpExplorerForm * Form) :
  88. FForm(Form)
  89. {
  90. FForm->LockWindow();
  91. }
  92. ~TWindowLock()
  93. {
  94. FForm->UnlockWindow();
  95. }
  96. private:
  97. TCustomScpExplorerForm * FForm;
  98. };
  99. //---------------------------------------------------------------------------
  100. class TAutoBatch
  101. {
  102. public:
  103. TAutoBatch(TCustomScpExplorerForm * Form) :
  104. FForm(Form)
  105. {
  106. FForm->BatchStart(FBatchStorage);
  107. }
  108. ~TAutoBatch()
  109. {
  110. FForm->BatchEnd(FBatchStorage);
  111. }
  112. private:
  113. TCustomScpExplorerForm * FForm;
  114. void * FBatchStorage;
  115. };
  116. //---------------------------------------------------------------------------
  117. struct TTransferOperationParam
  118. {
  119. TTransferOperationParam();
  120. UnicodeString TargetDirectory;
  121. bool Temp;
  122. bool DragDrop;
  123. int Options;
  124. TAutoSwitch Queue;
  125. };
  126. //---------------------------------------------------------------------------
  127. TTransferOperationParam::TTransferOperationParam()
  128. {
  129. Temp = false;
  130. DragDrop = false;
  131. Options = 0;
  132. Queue = asAuto;
  133. }
  134. //---------------------------------------------------------------------------
  135. class TTransferPresetNoteData : public TObject
  136. {
  137. public:
  138. UnicodeString Message;
  139. };
  140. //---------------------------------------------------------------------------
  141. class TTerminalNoteData : public TObject
  142. {
  143. public:
  144. TManagedTerminal * Terminal;
  145. };
  146. //---------------------------------------------------------------------------
  147. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  148. FFormRestored(false),
  149. TForm(Owner)
  150. {
  151. FCurrentSide = osRemote;
  152. FEverShown = false;
  153. FDocks = new TList();
  154. InitControls();
  155. RestoreParams();
  156. ConfigurationChanged();
  157. RemoteDirView->Invalidate();
  158. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  159. NonVisualDataModule->ScpExplorer = this;
  160. FAutoOperation = false;
  161. FOnFileOperationFinished = NULL;
  162. FForceExecution = false;
  163. FIgnoreNextDialogChar = 0;
  164. FErrorList = NULL;
  165. FSynchronizeProgressForm = NULL;
  166. FProgressForm = NULL;
  167. FRefreshLocalDirectory = false;
  168. FRefreshRemoteDirectory = false;
  169. FDDExtMapFile = NULL;
  170. // CreateMutexW keeps failing with ERROR_NOACCESS
  171. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  172. DebugAssert(FDDExtMutex != NULL);
  173. FDDTargetControl = NULL;
  174. FDelayedDeletionTimer = NULL;
  175. FDelayedDeletionList = new TStringList();
  176. FDDFileList = NULL;
  177. FPendingTempSpaceWarn = false;
  178. FCapturedLog = NULL;
  179. FDragDropOperation = false;
  180. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  181. FAllowTransferPresetAutoSelect = true;
  182. FCopyParamDefault = L"";
  183. FSynchronizeController = NULL;
  184. FPendingQueueActionItem = NULL;
  185. FLockLevel = 0;
  186. FLockSuspendLevel = 0;
  187. FDisabledOnLockSuspend = false;
  188. FAlternativeDelete = false;
  189. FTrayIcon = new ::TTrayIcon(0);
  190. FTrayIcon->OnClick = TrayIconClick;
  191. FMaxQueueLength = 0;
  192. FLastContextPopupScreenPoint = TPoint(-1, -1);
  193. FTransferResumeList = NULL;
  194. FMoveToQueue = false;
  195. FStandaloneEditing = false;
  196. FOnFeedSynchronizeError = NULL;
  197. FNeedSession = false;
  198. FDoNotIdleCurrentTerminal = 0;
  199. FIncrementalSearching = 0;
  200. FIncrementalSearchHaveNext = false;
  201. FEditorManager = new TEditorManager();
  202. FEditorManager->OnFileChange = ExecutedFileChanged;
  203. FEditorManager->OnFileReload = ExecutedFileReload;
  204. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  205. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  206. FLocalEditors = new TList();
  207. FQueueStatus = NULL;
  208. FQueueStatusSection = new TCriticalSection();
  209. FQueueStatusInvalidated = false;
  210. FQueueItemInvalidated = false;
  211. FQueueActedItem = NULL;
  212. FQueueController = new TQueueController(QueueView3);
  213. FUserActionTimer = new TTimer(this);
  214. FUserActionTimer->Enabled = false;
  215. FUserActionTimer->Interval = 10;
  216. FUserActionTimer->OnTimer = UserActionTimer;
  217. FNotes = new TStringList();
  218. FNoteTimer = new TTimer(this);
  219. FNoteTimer->Enabled = false;
  220. FNoteTimer->OnTimer = NoteTimer;
  221. FOnNoteClick = NULL;
  222. FOle32Library = LoadLibrary(L"Ole32.dll");
  223. FDragMoveCursor = FOle32Library != NULL ?
  224. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  225. UseSystemSettings(this);
  226. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  227. static_cast<TObject*>(GetComponent(fcTransferList)));
  228. DebugAssert(TransferList != NULL);
  229. FTransferListHoverIndex = -1;
  230. TransferList->OnChange = TransferListChange;
  231. TransferList->OnDrawItem = TransferListDrawItem;
  232. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))));
  233. SetSubmenu(NonVisualDataModule->ColorMenuItem);
  234. UseDesktopFont(SessionsPageControl);
  235. UpdateSessionsPageControlHeight();
  236. UseDesktopFont(RemoteDirView);
  237. UseDesktopFont(RemoteDriveView);
  238. UseDesktopFont(QueueView3);
  239. UseDesktopFont(QueueLabel);
  240. UseDesktopFont(RemoteStatusBar);
  241. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  242. QueueSplitter->ShowHint = true;
  243. RemotePanelSplitter->ShowHint = true;
  244. UpdateImages();
  245. FCustomCommandMenu = CreateTBXPopupMenu(this);
  246. FCustomCommandLocalFileList = NULL;
  247. FCustomCommandRemoteFileList = NULL;
  248. FSessionColors = new TPngImageList(this);
  249. FSessionColors->ColorDepth = cd32Bit;
  250. AddFixedSessionImages();
  251. SessionsPageControl->Images = FSessionColors;
  252. QueueLabel->FocusControl = QueueView3;
  253. UpdateQueueLabel();
  254. FRemoteDirViewWasFocused = true;
  255. CreateHiddenWindow();
  256. StartUpdates();
  257. }
  258. //---------------------------------------------------------------------------
  259. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  260. {
  261. // this has to be one of the very first things to do
  262. StopUpdateThread();
  263. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  264. TTerminalManager::Instance()->ScpExplorer = NULL;
  265. delete FCustomCommandLocalFileList;
  266. delete FCustomCommandRemoteFileList;
  267. delete FCustomCommandMenu;
  268. delete FTrayIcon;
  269. FTrayIcon = NULL;
  270. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  271. delete FEditorManager;
  272. ForceCloseLocalEditors();
  273. delete FLocalEditors;
  274. HideFileFindDialog();
  275. if (FDelayedDeletionTimer)
  276. {
  277. DoDelayedDeletion(NULL);
  278. SAFE_DESTROY(FDelayedDeletionTimer);
  279. }
  280. SAFE_DESTROY(FDelayedDeletionList);
  281. // sometimes we do not get DDEnd so the list is not released
  282. SAFE_DESTROY(FDDFileList);
  283. DebugAssert(FSynchronizeController == NULL);
  284. CloseHandle(FDDExtMutex);
  285. FDDExtMutex = NULL;
  286. FreeLibrary(FOle32Library);
  287. FOle32Library = NULL;
  288. FDragMoveCursor = NULL;
  289. DebugAssert(!FErrorList);
  290. if (FEverShown)
  291. {
  292. // when window is never shown (like when running command-line operation),
  293. // particularly window site is not restored correctly (BoundsRect value set
  294. // in RestoreForm gets lost during handle allocation), so we do not want
  295. // it to be stored
  296. StoreParams();
  297. }
  298. Terminal = NULL;
  299. Queue = NULL;
  300. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  301. NonVisualDataModule->ScpExplorer = NULL;
  302. delete FQueueController;
  303. FQueueController = NULL;
  304. delete FQueueStatusSection;
  305. FQueueStatusSection = NULL;
  306. delete FQueueStatus;
  307. FQueueStatus = NULL;
  308. delete FUserActionTimer;
  309. FUserActionTimer = NULL;
  310. delete FNoteTimer;
  311. delete FNotes;
  312. delete FNoteData;
  313. SAFE_DESTROY(FDocks);
  314. SessionsPageControl->Images = NULL;
  315. // only after clearing Terminal (after DoTerminalListChanged => ... => AddSessionColor is called)
  316. SAFE_DESTROY(FSessionColors);
  317. SAFE_DESTROY(FSessionsDragDropFilesEx);
  318. SAFE_DESTROY(FHistoryMenu[0][0]);
  319. SAFE_DESTROY(FHistoryMenu[0][1]);
  320. SAFE_DESTROY(FHistoryMenu[1][0]);
  321. SAFE_DESTROY(FHistoryMenu[1][1]);
  322. if (FHiddenWindow != NULL)
  323. {
  324. DestroyWindow(FHiddenWindow);
  325. FHiddenWindow = NULL;
  326. }
  327. }
  328. //---------------------------------------------------------------------------
  329. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  330. {
  331. std::unique_ptr<TSessionData> Data;
  332. if (!Session.IsEmpty())
  333. {
  334. bool DefaultsOnly;
  335. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  336. }
  337. TTerminalManager * Manager = TTerminalManager::Instance();
  338. for (int Index = 0; Index < Manager->Count; Index++)
  339. {
  340. TTerminal * Terminal = Manager->Terminals[Index];
  341. if (Session.IsEmpty() ||
  342. Terminal->SessionData->IsSameSite(Data.get()))
  343. {
  344. if (Path.IsEmpty())
  345. {
  346. Terminal->ClearCaches();
  347. }
  348. else
  349. {
  350. Terminal->DirectoryModified(Path, true);
  351. }
  352. }
  353. }
  354. // We should flag a pending refresh for the background terminals or busy foreground terminals
  355. if ((Terminal != NULL) && Terminal->Active &&
  356. CanCommandLineFromAnotherInstance() &&
  357. (Session.IsEmpty() || Terminal->SessionData->IsSameSite(Data.get())) &&
  358. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  359. {
  360. Terminal->ReloadDirectory();
  361. }
  362. }
  363. //---------------------------------------------------------------------------
  364. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  365. {
  366. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  367. size_t MessageSize = sizeof(TCopyDataMessage);
  368. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  369. if (Result)
  370. {
  371. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  372. Result = (Message.Version == TCopyDataMessage::Version1);
  373. if (Result)
  374. {
  375. switch (Message.Command)
  376. {
  377. case TCopyDataMessage::CommandCanCommandLine:
  378. Result = CanCommandLineFromAnotherInstance();
  379. break;
  380. case TCopyDataMessage::CommandCommandLine:
  381. {
  382. UnicodeString CommandLine(Message.CommandLine);
  383. Result = CommandLineFromAnotherInstance(CommandLine);
  384. }
  385. break;
  386. case TCopyDataMessage::RefreshPanel:
  387. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  388. break;
  389. case TCopyDataMessage::MainWindowCheck:
  390. Result = true;
  391. break;
  392. default:
  393. Result = false;
  394. break;
  395. }
  396. }
  397. }
  398. Message.Result = Result ? 1 : 0;
  399. }
  400. //---------------------------------------------------------------------------
  401. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  402. {
  403. WNDCLASS WindowClass = {0};
  404. WindowClass.lpfnWndProc = DefWindowProc;
  405. WindowClass.hInstance = HInstance;
  406. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  407. FHiddenWindow = NULL;
  408. if (RegisterClass(&WindowClass))
  409. {
  410. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  411. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  412. }
  413. }
  414. //---------------------------------------------------------------------------
  415. bool __fastcall TCustomScpExplorerForm::CanConsole()
  416. {
  417. return
  418. (Terminal != NULL) &&
  419. Terminal->Active &&
  420. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  421. }
  422. //---------------------------------------------------------------------------
  423. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  424. {
  425. bool Result = Visible && !NonVisualDataModule->Busy;
  426. return Result;
  427. }
  428. //---------------------------------------------------------------------------
  429. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  430. const UnicodeString & CommandLine)
  431. {
  432. TProgramParams Params(CommandLine);
  433. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  434. if (Result)
  435. {
  436. NonVisualDataModule->StartBusy();
  437. try
  438. {
  439. // this action is initiated from another process,
  440. // so it's likely that our window is not visible,
  441. // and user won't see what is going on
  442. Application->BringToFront();
  443. // reload sessions as we may be asked to open a session
  444. // just stored by another instance
  445. StoredSessions->Reload();
  446. UnicodeString SessionName = Params.Param[1];
  447. std::unique_ptr<TObjectList> DataList(new TObjectList());
  448. UnicodeString DownloadFile; // unused
  449. try
  450. {
  451. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this);
  452. }
  453. catch (EAbort &)
  454. {
  455. // Happens when opening session in PuTTY and in other situations,
  456. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  457. // to process the command-line on its own (or to pass it to yet another instance)
  458. }
  459. if (DataList->Count > 0)
  460. {
  461. TTerminalManager * Manager = TTerminalManager::Instance();
  462. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  463. }
  464. }
  465. __finally
  466. {
  467. NonVisualDataModule->EndBusy();
  468. }
  469. }
  470. return Result;
  471. }
  472. //---------------------------------------------------------------------------
  473. void __fastcall TCustomScpExplorerForm::SetTerminal(TManagedTerminal * value)
  474. {
  475. if (FTerminal != value)
  476. {
  477. TerminalChanging();
  478. DoSetTerminal(value, false);
  479. }
  480. }
  481. //---------------------------------------------------------------------------
  482. void __fastcall TCustomScpExplorerForm::DoSetTerminal(TManagedTerminal * value, bool Replace)
  483. {
  484. FTerminal = value;
  485. bool PrevAllowTransferPresetAutoSelect = FAllowTransferPresetAutoSelect;
  486. FAllowTransferPresetAutoSelect = false;
  487. try
  488. {
  489. TerminalChanged(Replace);
  490. }
  491. __finally
  492. {
  493. FAllowTransferPresetAutoSelect = PrevAllowTransferPresetAutoSelect;
  494. }
  495. if (Terminal != NULL)
  496. {
  497. TransferPresetAutoSelect();
  498. }
  499. }
  500. //---------------------------------------------------------------------------
  501. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  502. {
  503. DoSetTerminal(value, true);
  504. }
  505. //---------------------------------------------------------------------------
  506. void __fastcall TCustomScpExplorerForm::TerminalChanging()
  507. {
  508. if (FTerminal != NULL)
  509. {
  510. UpdateTerminal(Terminal);
  511. }
  512. }
  513. //---------------------------------------------------------------------------
  514. void __fastcall TCustomScpExplorerForm::TerminalChanged(bool Replaced)
  515. {
  516. if (Terminal != NULL)
  517. {
  518. UpdateSessionColor((TColor)Terminal->StateData->Color);
  519. }
  520. DoTerminalListChanged();
  521. DebugAssert(!IsLocalBrowserMode()); // TODO
  522. if (Replaced)
  523. {
  524. RemoteDirView->ReplaceTerminal(Terminal);
  525. }
  526. else
  527. {
  528. RemoteDirView->Terminal = Terminal;
  529. }
  530. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  531. if (Terminal != NULL)
  532. {
  533. if (Terminal->Active)
  534. {
  535. Terminal->RefreshDirectory();
  536. }
  537. if (WinConfiguration->PreservePanelState)
  538. {
  539. if (Terminal->RemoteExplorerState != NULL)
  540. {
  541. DirView(osRemote)->RestoreState(Terminal->RemoteExplorerState);
  542. }
  543. else
  544. {
  545. DirView(osRemote)->ClearState();
  546. }
  547. }
  548. if (!Replaced && Terminal->Active)
  549. {
  550. InitStatusBar();
  551. }
  552. }
  553. UpdateTransferList();
  554. // Update panels Enable state before refreshing the labels
  555. UpdateControls();
  556. }
  557. //---------------------------------------------------------------------------
  558. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  559. {
  560. if (Queue != value)
  561. {
  562. if (FQueue != NULL)
  563. {
  564. FQueue->OnListUpdate = NULL;
  565. FQueue->OnQueueItemUpdate = NULL;
  566. }
  567. FQueue = value;
  568. if (FQueue != NULL)
  569. {
  570. DebugAssert(FQueue->OnListUpdate == NULL);
  571. FQueue->OnListUpdate = QueueListUpdate;
  572. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  573. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  574. }
  575. QueueChanged();
  576. }
  577. }
  578. //---------------------------------------------------------------------------
  579. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  580. TListItem * Item)
  581. {
  582. if (FQueueActedItem == Item)
  583. {
  584. FQueueActedItem = NULL;
  585. if ((QueueView3->PopupMenu != NULL) &&
  586. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  587. {
  588. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  589. ReleaseCapture();
  590. }
  591. }
  592. if (Item->Data == FPendingQueueActionItem)
  593. {
  594. FPendingQueueActionItem = NULL;
  595. }
  596. }
  597. //---------------------------------------------------------------------------
  598. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  599. {
  600. {
  601. TGuard Guard(FQueueStatusSection);
  602. FQueueStatusInvalidated = false;
  603. if (FQueue != NULL)
  604. {
  605. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  606. }
  607. }
  608. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  609. {
  610. FMaxQueueLength = FQueueStatus->Count;
  611. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  612. }
  613. FQueueController->UpdateQueueStatus(FQueueStatus);
  614. SetQueueProgress();
  615. UpdateQueueView();
  616. bool IsEmpty = (FQueueStatus == NULL) || (FQueueStatus->Count == 0);
  617. if (IsEmpty && (Terminal != NULL))
  618. {
  619. TOnceDoneOperation OnceDoneOperation =
  620. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  621. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  622. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  623. {
  624. FQueue->Enabled = false;
  625. }
  626. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  627. {
  628. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  629. }
  630. }
  631. }
  632. //---------------------------------------------------------------------------
  633. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  634. {
  635. UnicodeString Result;
  636. if (FQueueStatus != NULL)
  637. {
  638. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  639. {
  640. TFileOperationProgressType * ProgressData =
  641. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  642. if ((ProgressData != NULL) && ProgressData->InProgress)
  643. {
  644. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  645. }
  646. }
  647. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  648. {
  649. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  650. }
  651. }
  652. return Result;
  653. }
  654. //---------------------------------------------------------------------------
  655. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  656. {
  657. ComponentVisible[fcQueueView] =
  658. (WinConfiguration->QueueView.Show == qvShow) ||
  659. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  660. (FQueueStatus != NULL) && (FQueueStatus->Count > 0));
  661. }
  662. //---------------------------------------------------------------------------
  663. void __fastcall TCustomScpExplorerForm::QueueChanged()
  664. {
  665. if (FQueueStatus != NULL)
  666. {
  667. delete FQueueStatus;
  668. FQueueStatus = NULL;
  669. }
  670. UpdateQueueStatus(true);
  671. }
  672. //---------------------------------------------------------------------------
  673. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  674. {
  675. if (FQueue == Queue)
  676. {
  677. FQueueStatusInvalidated = true;
  678. }
  679. }
  680. //---------------------------------------------------------------------------
  681. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  682. TQueueItem * Item)
  683. {
  684. if (FQueue == Queue)
  685. {
  686. TGuard Guard(FQueueStatusSection);
  687. // this may be running in parallel with QueueChanged
  688. if (FQueueStatus != NULL)
  689. {
  690. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  691. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  692. {
  693. FRefreshLocalDirectory = (QueueItem == NULL) ||
  694. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  695. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  696. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  697. }
  698. if (QueueItem != NULL)
  699. {
  700. QueueItem->UserData = (void*)true;
  701. FQueueItemInvalidated = true;
  702. }
  703. }
  704. }
  705. }
  706. //---------------------------------------------------------------------------
  707. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  708. {
  709. // during standalone editing, we have no way to see/control queue,
  710. // so we have to always popup prompts automatically
  711. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  712. }
  713. //---------------------------------------------------------------------------
  714. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  715. {
  716. if (FQueueStatus != NULL)
  717. {
  718. bool QueueAutoPopup = IsQueueAutoPopup();
  719. bool NeedRefresh = FQueueController->NeedRefresh();
  720. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  721. FQueueItemInvalidated = false;
  722. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  723. bool Updated = false;
  724. TQueueItemProxy * QueueItem;
  725. bool WasUserAction;
  726. for (int Index = 0; Index < Limit; Index++)
  727. {
  728. bool Update = false;
  729. QueueItem = FQueueStatus->Items[Index];
  730. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  731. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  732. (FPendingQueueActionItem == NULL))
  733. {
  734. FPendingQueueActionItem = QueueItem;
  735. FUserActionTimer->Enabled = true;
  736. }
  737. if ((bool)QueueItem->UserData)
  738. {
  739. QueueItem->UserData = (void*)false;
  740. QueueItem->Update();
  741. Updated = true;
  742. Update = true;
  743. }
  744. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  745. {
  746. Update = true;
  747. }
  748. if (Update)
  749. {
  750. FQueueController->RefreshQueueItem(QueueItem);
  751. }
  752. }
  753. if (Updated)
  754. {
  755. NonVisualDataModule->UpdateNonVisibleActions();
  756. SetQueueProgress();
  757. }
  758. }
  759. }
  760. //---------------------------------------------------------------------------
  761. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  762. {
  763. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  764. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  765. }
  766. //---------------------------------------------------------------------------
  767. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  768. {
  769. if (Progress >= 0)
  770. {
  771. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  772. }
  773. else
  774. {
  775. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  776. }
  777. }
  778. //---------------------------------------------------------------------------
  779. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  780. {
  781. int OverallProgress;
  782. // FProgressForm is null when this is called from SetQueueProgress
  783. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  784. {
  785. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  786. }
  787. else if (!TFileOperationProgressType::IsIndeterminateOperation(ProgressData->Operation))
  788. {
  789. OverallProgress = ProgressData->OverallProgress();
  790. }
  791. else
  792. {
  793. OverallProgress = -1;
  794. }
  795. SetTaskbarListProgressValue(OverallProgress);
  796. }
  797. //---------------------------------------------------------------------------
  798. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  799. {
  800. TTerminalManager::Instance()->QueueStatusUpdated();
  801. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  802. {
  803. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  804. {
  805. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  806. {
  807. TFileOperationProgressType * ProgressData = NULL;
  808. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  809. {
  810. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  811. }
  812. if ((ProgressData != NULL) &&
  813. ProgressData->InProgress)
  814. {
  815. SetTaskbarListProgressValue(ProgressData);
  816. }
  817. else
  818. {
  819. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  820. }
  821. }
  822. else
  823. {
  824. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  825. }
  826. }
  827. else
  828. {
  829. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  830. }
  831. }
  832. UpdateQueueLabel();
  833. }
  834. //---------------------------------------------------------------------------
  835. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  836. {
  837. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  838. if (FQueueStatus != NULL)
  839. {
  840. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  841. if (ActiveAndPendingPrimaryCount > 0)
  842. {
  843. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  844. }
  845. }
  846. QueueLabel->Caption = Caption;
  847. }
  848. //---------------------------------------------------------------------------
  849. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  850. {
  851. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  852. static_cast<TComponent*>(GetComponent(fcTransferList)));
  853. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  854. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  855. TransferList->Strings->BeginUpdate();
  856. try
  857. {
  858. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  859. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  860. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  861. if (FTransferDropDownHint.IsEmpty())
  862. {
  863. FTransferDropDownHint = TransferDropDown->Hint;
  864. }
  865. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  866. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  867. TransferDropDown->Text = StripHotkey(Name);
  868. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  869. (FTransferDropDownHint, StripHotkey(Name),
  870. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  871. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General))));
  872. // update the label, otherwise when it is updated only on the first draw
  873. // of the list, it is drawn "bold" for some reason
  874. FTransferListHoverIndex = TransferList->ItemIndex;
  875. UpdateTransferLabel();
  876. }
  877. __finally
  878. {
  879. TransferList->Strings->EndUpdate();
  880. }
  881. }
  882. //---------------------------------------------------------------------------
  883. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  884. {
  885. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  886. DebugAssert(Toolbar != NULL);
  887. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  888. }
  889. //---------------------------------------------------------------------------
  890. void __fastcall TCustomScpExplorerForm::UpdateActions()
  891. {
  892. TForm::UpdateActions();
  893. if (ComponentVisible[fcCustomCommandsBand])
  894. {
  895. UpdateCustomCommandsToolbar();
  896. }
  897. }
  898. //---------------------------------------------------------------------------
  899. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  900. {
  901. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  902. }
  903. //---------------------------------------------------------------------------
  904. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  905. {
  906. Color = GetBtnFaceColor();
  907. DebugAssert(Configuration && RemoteDirView);
  908. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  909. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  910. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  911. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  912. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  913. if (RemoteDirView->RowSelect != WinConfiguration->FullRowSelect)
  914. {
  915. RemoteDirView->RowSelect = WinConfiguration->FullRowSelect;
  916. // selection is not redrawn automatically when RowSelect changes
  917. RemoteDirView->Invalidate();
  918. }
  919. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  920. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  921. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  922. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  923. UpdateSessionsPageControlHeight();
  924. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  925. UpdateToolbarDisplayMode();
  926. UpdateTransferList();
  927. if (Terminal != NULL)
  928. {
  929. TransferPresetAutoSelect();
  930. }
  931. UpdateQueueView();
  932. UpdateControls();
  933. // this can be called even before constuctor finishes.
  934. if (FEditorManager != NULL)
  935. {
  936. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  937. }
  938. // this can be called even before constuctor finishes.
  939. if (FLocalEditors != NULL)
  940. {
  941. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  942. {
  943. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  944. }
  945. }
  946. if (ComponentVisible[fcCustomCommandsBand])
  947. {
  948. UpdateCustomCommandsToolbar();
  949. }
  950. // show only when keeping queue items forever,
  951. // otherwise, its enough to have in in the context menu
  952. QueueDeleteAllDoneQueueToolbarItem->Visible =
  953. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  954. if (FFileColorsCurrent != WinConfiguration->FileColors)
  955. {
  956. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  957. FFileColorsCurrent = WinConfiguration->FileColors;
  958. FileColorsChanged();
  959. }
  960. }
  961. //---------------------------------------------------------------------------
  962. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  963. {
  964. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  965. DirView->Invalidate();
  966. }
  967. //---------------------------------------------------------------------------
  968. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  969. {
  970. DoFileColorsChanged(RemoteDirView);
  971. }
  972. //---------------------------------------------------------------------------
  973. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  974. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  975. void * /*Arg*/)
  976. {
  977. if (Token != NULL)
  978. {
  979. TForm * Editor = dynamic_cast<TForm*>(Token);
  980. DebugAssert(Editor != NULL);
  981. ReconfigureEditorForm(Editor);
  982. }
  983. }
  984. //---------------------------------------------------------------------------
  985. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  986. {
  987. WinConfiguration->DDTransferConfirmation = asOn;
  988. }
  989. //---------------------------------------------------------------------------
  990. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  991. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  992. {
  993. // noop
  994. }
  995. //---------------------------------------------------------------------------
  996. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  997. TTransferDirection Direction, TTransferType Type, bool Temp,
  998. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  999. bool Confirm, bool DragDrop, int Options)
  1000. {
  1001. bool Result = true;
  1002. DebugAssert(Terminal && Terminal->Active);
  1003. // these parameters are known in advance
  1004. int Params =
  1005. FLAGMASK(Type == ttMove, cpDelete);
  1006. bool ToTemp = (Temp && (Direction == tdToLocal));
  1007. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1008. {
  1009. bool DisableNewerOnly =
  1010. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1011. ToTemp;
  1012. Options |=
  1013. FLAGMASK(ToTemp, coTemp) |
  1014. FLAGMASK(!Terminal->IsCapable[fsBackgroundTransfers], coDisableQueue) |
  1015. coDoNotShowAgain;
  1016. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1017. int CopyParamAttrs = (Direction == tdToRemote ?
  1018. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1019. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1020. int OutputOptions =
  1021. FLAGMASK(DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto),
  1022. cooDoNotShowAgain);
  1023. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1024. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1025. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1026. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1027. if (Result)
  1028. {
  1029. if (FLAGSET(OutputOptions, cooDoNotShowAgain))
  1030. {
  1031. if (DragDrop)
  1032. {
  1033. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1034. {
  1035. PopupTrayBalloon(NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation,
  1036. NULL, 0, EnableDDTransferConfirmation, NULL);
  1037. }
  1038. WinConfiguration->DDTransferConfirmation = asOff;
  1039. }
  1040. else
  1041. {
  1042. WinConfiguration->ConfirmTransferring = false;
  1043. }
  1044. }
  1045. else
  1046. {
  1047. // User explicitly unchecked "do not show again",
  1048. // so show him the dialog the next time
  1049. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1050. {
  1051. WinConfiguration->DDTransferConfirmation = asOn;
  1052. }
  1053. }
  1054. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1055. }
  1056. }
  1057. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fsBackgroundTransfers])
  1058. {
  1059. Configuration->Usage->Inc(L"TransfersOnBackground");
  1060. // these parameter are known only after transfer dialog
  1061. Params |=
  1062. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1063. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1064. Result = false;
  1065. ClearTransferSourceSelection(Direction);
  1066. }
  1067. return Result;
  1068. }
  1069. //---------------------------------------------------------------------------
  1070. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1071. {
  1072. if (FOnFileOperationFinished != NULL)
  1073. {
  1074. FOnFileOperationFinished(UnicodeString(), true);
  1075. }
  1076. else
  1077. {
  1078. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1079. if (HasDirView[Side])
  1080. {
  1081. DirView(Side)->SelectAll(smNone);
  1082. }
  1083. }
  1084. }
  1085. //---------------------------------------------------------------------------
  1086. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1087. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1088. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1089. int Params)
  1090. {
  1091. DebugAssert(Queue != NULL);
  1092. bool SingleFile = false;
  1093. if (FileList->Count == 1)
  1094. {
  1095. if (Direction == tdToRemote)
  1096. {
  1097. UnicodeString FileName = FileList->Strings[0];
  1098. SingleFile = FileExists(ApiPath(FileName));
  1099. }
  1100. else
  1101. {
  1102. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1103. SingleFile = !File->IsDirectory;
  1104. }
  1105. }
  1106. TQueueItem * QueueItem;
  1107. if (Direction == tdToRemote)
  1108. {
  1109. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1110. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1111. }
  1112. else
  1113. {
  1114. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1115. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1116. }
  1117. AddQueueItem(Queue, QueueItem, Terminal);
  1118. }
  1119. //---------------------------------------------------------------------------
  1120. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1121. {
  1122. if (Queue->IsEmpty)
  1123. {
  1124. Terminal->QueueOperationStart = Now();
  1125. }
  1126. Queue->AddItem(QueueItem);
  1127. }
  1128. //---------------------------------------------------------------------------
  1129. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1130. {
  1131. }
  1132. //---------------------------------------------------------------------------
  1133. void __fastcall TCustomScpExplorerForm::InitControls()
  1134. {
  1135. DebugAssert(FDocks != NULL);
  1136. for (int Index = 0; Index < ComponentCount; Index++)
  1137. {
  1138. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1139. if ((Dock != NULL) && (Dock->Tag == 0))
  1140. {
  1141. FDocks->Add(Dock);
  1142. }
  1143. }
  1144. CollectItemsWithTextDisplayMode(this);
  1145. }
  1146. //---------------------------------------------------------------------------
  1147. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1148. {
  1149. ConfigurationChanged();
  1150. QueuePanel->Height =
  1151. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1152. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1153. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1154. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1155. }
  1156. //---------------------------------------------------------------------------
  1157. void __fastcall TCustomScpExplorerForm::StoreParams()
  1158. {
  1159. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1160. WinConfiguration->QueueView.HeightPixelsPerInch = GetControlPixelsPerInch(this);
  1161. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1162. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1163. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1164. }
  1165. //---------------------------------------------------------------------------
  1166. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1167. {
  1168. if (!FFormRestored)
  1169. {
  1170. FFormRestored = true;
  1171. RestoreFormParams();
  1172. }
  1173. TForm::CreateParams(Params);
  1174. }
  1175. //---------------------------------------------------------------------------
  1176. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1177. {
  1178. DebugAssert(FDocks != NULL);
  1179. for (int Index = 0; Index < FDocks->Count; Index++)
  1180. {
  1181. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1182. }
  1183. }
  1184. //---------------------------------------------------------------------------
  1185. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1186. {
  1187. SetDockAllowDrag(true);
  1188. while (!ButtonsStr.IsEmpty())
  1189. {
  1190. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1191. UnicodeString Name = CutToChar(S, L'=', true);
  1192. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1193. {
  1194. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1195. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1196. {
  1197. while (!S.IsEmpty())
  1198. {
  1199. UnicodeString S2 = CutToChar(S, L',', true);
  1200. UnicodeString Name = CutToChar(S2, L':', true);
  1201. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1202. {
  1203. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1204. if (SameText(GetToolbarItemName(Item), Name))
  1205. {
  1206. // forward compatibility
  1207. UnicodeString S3 = CutToChar(S2, L':', true);
  1208. bool Visible = StrToIntDef(S3, 0);
  1209. Item->Visible = Visible;
  1210. }
  1211. }
  1212. }
  1213. }
  1214. }
  1215. }
  1216. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1217. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1218. }
  1219. //---------------------------------------------------------------------------
  1220. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1221. {
  1222. UnicodeString Result;
  1223. SetDockAllowDrag(true);
  1224. Result = ::GetToolbarsLayoutStr(this);
  1225. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1226. return Result;
  1227. }
  1228. //---------------------------------------------------------------------------
  1229. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1230. {
  1231. UnicodeString Result;
  1232. if (Item->Action != NULL)
  1233. {
  1234. Result = Item->Action->Name;
  1235. Result = RemoveSuffix(Result, L"Action");
  1236. }
  1237. else
  1238. {
  1239. Result = Item->Name;
  1240. Result = RemoveSuffix(Result, L"SubmenuItem");
  1241. Result = RemoveSuffix(Result, L"Item");
  1242. }
  1243. return Result;
  1244. }
  1245. //---------------------------------------------------------------------------
  1246. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1247. {
  1248. UnicodeString Result;
  1249. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1250. {
  1251. UnicodeString ToolbarStr;
  1252. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1253. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1254. {
  1255. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1256. {
  1257. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1258. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1259. // Once we introduce any buttons that aer hidden by default, we would have to remember their initial state
  1260. // and save the changes here only.
  1261. if (!Item->Visible)
  1262. {
  1263. UnicodeString Name = GetToolbarItemName(Item);
  1264. DebugAssert(Name.Pos(L"TBX") == 0);
  1265. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1266. }
  1267. }
  1268. }
  1269. if (!ToolbarStr.IsEmpty())
  1270. {
  1271. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1272. }
  1273. }
  1274. return Result;
  1275. }
  1276. //---------------------------------------------------------------------------
  1277. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1278. {
  1279. DebugAssert(FProgressForm == NULL);
  1280. bool AllowSkip = (Terminal != NULL) ? Terminal->IsCapable[fsSkipTransfer] : false;
  1281. FProgressForm = new TProgressForm(Application, (FTransferResumeList != NULL), AllowSkip, SynchronizeProgress);
  1282. FProgressForm->DeleteLocalToRecycleBin =
  1283. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1284. FProgressForm->DeleteRemoteToRecycleBin =
  1285. (Terminal != NULL) &&
  1286. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1287. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1288. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1289. LockWindow();
  1290. if (FTaskbarList != NULL)
  1291. {
  1292. // Actually, do not know what hides the progress once the operation finishes
  1293. SetTaskbarListProgressState(TBPF_NORMAL);
  1294. }
  1295. }
  1296. //---------------------------------------------------------------------------
  1297. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1298. {
  1299. UnlockWindow();
  1300. SAFE_DESTROY(FProgressForm);
  1301. SetQueueProgress();
  1302. }
  1303. //---------------------------------------------------------------------------
  1304. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1305. TFileOperationProgressType & ProgressData)
  1306. {
  1307. // Download to temporary local directory
  1308. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1309. {
  1310. bool Continue = true;
  1311. FPendingTempSpaceWarn = false;
  1312. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1313. if (!Continue)
  1314. {
  1315. Abort();
  1316. }
  1317. }
  1318. // operation is being executed and we still didn't show up progress form
  1319. if (ProgressData.InProgress && !FProgressForm)
  1320. {
  1321. CreateProgressForm(NULL);
  1322. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1323. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1324. }
  1325. // operation is finished (or terminated), so we hide progress form
  1326. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1327. {
  1328. DestroyProgressForm();
  1329. if (ProgressData.Operation == foCalculateSize)
  1330. {
  1331. // When calculating size before transfer, the abort caused by
  1332. // cancel flag set due to "MoveToQueue" below,
  1333. // is not propagated back to us in ExecuteFileOperation,
  1334. // as it is silently swallowed.
  1335. // So we explicitly (re)throw it here.
  1336. if (FMoveToQueue)
  1337. {
  1338. Abort();
  1339. }
  1340. }
  1341. else
  1342. {
  1343. if ((ProgressData.Cancel == csContinue) &&
  1344. !FAutoOperation)
  1345. {
  1346. OperationComplete(ProgressData.StartTime);
  1347. }
  1348. }
  1349. }
  1350. if (FProgressForm &&
  1351. // Can happen only during synchronization, when ending one of the operations
  1352. ProgressData.InProgress)
  1353. {
  1354. FProgressForm->SetProgressData(ProgressData);
  1355. if (FTaskbarList != NULL)
  1356. {
  1357. DebugAssert(ProgressData.InProgress);
  1358. SetTaskbarListProgressValue(&ProgressData);
  1359. }
  1360. if (FProgressForm->Cancel > csContinue)
  1361. {
  1362. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1363. if (FProgressForm->Cancel == csCancelFile)
  1364. {
  1365. FProgressForm->ClearCancel();
  1366. }
  1367. // cancel cancels even the move
  1368. FMoveToQueue = false;
  1369. }
  1370. else if (FProgressForm->MoveToQueue)
  1371. {
  1372. FMoveToQueue = true;
  1373. ProgressData.SetCancelAtLeast(csCancel);
  1374. }
  1375. if ((FTransferResumeList != NULL) &&
  1376. ProgressData.InProgress &&
  1377. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1378. !ProgressData.FullFileName.IsEmpty())
  1379. {
  1380. if ((FTransferResumeList->Count == 0) ||
  1381. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1382. {
  1383. // note that we do not recognize directories from files here
  1384. FTransferResumeList->Add(ProgressData.FullFileName);
  1385. }
  1386. }
  1387. }
  1388. }
  1389. //---------------------------------------------------------------------------
  1390. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1391. const TDateTime & StartTime)
  1392. {
  1393. if (GUIConfiguration->BeepOnFinish &&
  1394. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1395. {
  1396. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1397. DWORD Sound;
  1398. if (!ExtractFileExt(BeepSound).IsEmpty())
  1399. {
  1400. Sound = SND_FILENAME;
  1401. }
  1402. else
  1403. {
  1404. Sound = SND_ALIAS;
  1405. }
  1406. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1407. }
  1408. }
  1409. //---------------------------------------------------------------------------
  1410. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1411. TFileOperationProgressType & ProgressData)
  1412. {
  1413. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1414. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1415. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1416. FileOperationProgress(ProgressData);
  1417. }
  1418. //---------------------------------------------------------------------------
  1419. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1420. {
  1421. UnicodeString Result;
  1422. if (FProgressForm != NULL)
  1423. {
  1424. Result = FProgressForm->ProgressStr();
  1425. }
  1426. return Result;
  1427. }
  1428. //---------------------------------------------------------------------------
  1429. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1430. bool DragDrop)
  1431. {
  1432. return (!DragDrop && (DropSourceControl == NULL)) ||
  1433. (DropSourceControl == DirView(osOther));
  1434. }
  1435. //---------------------------------------------------------------------------
  1436. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1437. TFileOperation Operation, TOperationSide Side,
  1438. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1439. TOnceDoneOperation & OnceDoneOperation)
  1440. {
  1441. if (!FAutoOperation)
  1442. {
  1443. // no selection on "/upload", form servers only as event handler
  1444. // (it is not displayed)
  1445. if (PanelOperation(Side, FDragDropOperation) &&
  1446. Visible && (Operation != foCalculateSize) &&
  1447. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1448. {
  1449. if (FOnFileOperationFinished != NULL)
  1450. {
  1451. FOnFileOperationFinished(FileName, Success);
  1452. }
  1453. else
  1454. {
  1455. TCustomDirView * DView = DirView(Side);
  1456. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1457. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1458. // this can happen when local drive is unplugged in the middle of the operation
  1459. if (Item != NULL)
  1460. {
  1461. if (Success) Item->Selected = false;
  1462. DView->MakeProgressVisible(Item);
  1463. }
  1464. }
  1465. }
  1466. if ((Operation == foCopy) || (Operation == foMove))
  1467. {
  1468. DebugAssert(!IsLocalBrowserMode()); // TODO
  1469. if (Side == osLocal)
  1470. {
  1471. Configuration->Usage->Inc(L"UploadedFiles");
  1472. }
  1473. else
  1474. {
  1475. Configuration->Usage->Inc(L"DownloadedFiles");
  1476. }
  1477. }
  1478. }
  1479. if (Success && (FSynchronizeController != NULL))
  1480. {
  1481. DebugAssert(!IsSideLocalBrowser(Side));
  1482. if (Operation == foCopy)
  1483. {
  1484. DebugAssert(Side == osLocal);
  1485. FSynchronizeController->LogOperation(soUpload, FileName);
  1486. }
  1487. else if (Operation == foDelete)
  1488. {
  1489. DebugAssert(Side == osRemote);
  1490. FSynchronizeController->LogOperation(soDelete, FileName);
  1491. }
  1492. }
  1493. if (FProgressForm)
  1494. {
  1495. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1496. }
  1497. }
  1498. //---------------------------------------------------------------------------
  1499. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1500. TFileOperation Operation, TOperationSide Side,
  1501. bool Temp, const UnicodeString & FileName, Boolean Success,
  1502. TOnceDoneOperation & OnceDoneOperation)
  1503. {
  1504. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1505. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1506. OnceDoneOperation);
  1507. }
  1508. //---------------------------------------------------------------------------
  1509. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1510. {
  1511. return false;
  1512. }
  1513. //---------------------------------------------------------------------------
  1514. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1515. {
  1516. return false;
  1517. }
  1518. //---------------------------------------------------------------------------
  1519. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1520. {
  1521. return NULL;
  1522. }
  1523. //---------------------------------------------------------------------------
  1524. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1525. {
  1526. DebugAssert(GetSide(Side) == osRemote);
  1527. DebugUsedParam(Side);
  1528. return RemoteDirView;
  1529. }
  1530. //---------------------------------------------------------------------------
  1531. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1532. {
  1533. DebugAssert(GetSide(Side) == osRemote);
  1534. DebugUsedParam(Side);
  1535. return RemoteDriveView;
  1536. }
  1537. //---------------------------------------------------------------------------
  1538. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1539. {
  1540. DebugAssert(GetSide(Side) == osRemote);
  1541. DebugUsedParam(Side);
  1542. return (Terminal != NULL) && Terminal->Active;
  1543. }
  1544. //---------------------------------------------------------------------------
  1545. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1546. TOperationSide Side, int FilesOnly)
  1547. {
  1548. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1549. }
  1550. //---------------------------------------------------------------------------
  1551. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1552. TOperationSide Side, int FilesOnly)
  1553. {
  1554. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1555. }
  1556. //---------------------------------------------------------------------------
  1557. struct THistoryItemData
  1558. {
  1559. short int Side;
  1560. short int Index;
  1561. };
  1562. //---------------------------------------------------------------------------
  1563. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1564. {
  1565. DirView(Side)->HistoryGo(Index);
  1566. }
  1567. //---------------------------------------------------------------------------
  1568. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1569. {
  1570. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1571. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1572. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1573. }
  1574. //---------------------------------------------------------------------------
  1575. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1576. bool Back)
  1577. {
  1578. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1579. {
  1580. TCustomDirView * DView = DirView(Side);
  1581. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1582. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1583. if (ICount > 10)
  1584. {
  1585. ICount = 10;
  1586. }
  1587. Menu->Items->Clear();
  1588. THistoryItemData Data;
  1589. Data.Side = Side;
  1590. for (short int i = 1; i <= ICount; i++)
  1591. {
  1592. TTBCustomItem * Item = new TTBXItem(Menu);
  1593. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1594. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1595. Item->Hint = DView->HistoryPath[Data.Index];
  1596. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1597. Item->Tag = *reinterpret_cast<int*>(&Data);
  1598. Item->OnClick = HistoryItemClick;
  1599. Menu->Items->Add(Item);
  1600. }
  1601. }
  1602. }
  1603. //---------------------------------------------------------------------------
  1604. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1605. TOperationSide Side, bool Back)
  1606. {
  1607. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1608. {
  1609. // workaround
  1610. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1611. // operator new allocates memory in Pascal code, but calls inline
  1612. // contructor in C++, leading in problems, the function does
  1613. // both in Pascal code
  1614. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1615. UpdateHistoryMenu(Side, Back);
  1616. }
  1617. return FHistoryMenu[Side == osLocal][Back];
  1618. }
  1619. //---------------------------------------------------------------------------
  1620. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1621. TCustomDirView *Sender)
  1622. {
  1623. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1624. UpdateHistoryMenu(Side, true);
  1625. UpdateHistoryMenu(Side, false);
  1626. }
  1627. //---------------------------------------------------------------------------
  1628. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1629. {
  1630. // remote custom commands can be executed only if the server supports shell commands
  1631. // or have secondary shell
  1632. return (FTerminal != NULL) && FTerminal->Active && (FTerminal->IsCapable[fcSecondaryShell] || FTerminal->IsCapable[fcShellAnyCommand]);
  1633. }
  1634. //---------------------------------------------------------------------------
  1635. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1636. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1637. {
  1638. int Result;
  1639. TFileCustomCommand RemoteCustomCommand;
  1640. TLocalCustomCommand LocalCustomCommand;
  1641. TFileCustomCommand * NonInteractiveCustomCommand =
  1642. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1643. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1644. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1645. if (FLAGCLEAR(Command.Params, ccLocal))
  1646. {
  1647. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1648. // custom command that does not operate with files can be executed anytime ...
  1649. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1650. {
  1651. // ... but do not show such command in remote file menu
  1652. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1653. {
  1654. Result = AllowedState;
  1655. }
  1656. else
  1657. {
  1658. Result = -1;
  1659. }
  1660. }
  1661. else
  1662. {
  1663. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1664. {
  1665. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1666. }
  1667. else
  1668. {
  1669. Result = -1;
  1670. }
  1671. }
  1672. }
  1673. else
  1674. {
  1675. // custom command that does not operate with files can be executed anytime
  1676. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1677. {
  1678. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1679. }
  1680. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1681. {
  1682. if ((ListType == ccltAll) || (ListType == ccltFile))
  1683. {
  1684. // special case is "diff"-style command that can be executed over any side,
  1685. // if we have both sides
  1686. Result =
  1687. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1688. // directly (instead of EnableSelectedOperation) to pass
  1689. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1690. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1691. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1692. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1693. }
  1694. else if (ListType == ccltBoth)
  1695. {
  1696. DebugAssert(!IsLocalBrowserMode());
  1697. Result = 1;
  1698. }
  1699. else
  1700. {
  1701. Result = -1;
  1702. }
  1703. }
  1704. else
  1705. {
  1706. if ((ListType == ccltAll) ||
  1707. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1708. {
  1709. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1710. }
  1711. else
  1712. {
  1713. Result = -1;
  1714. }
  1715. }
  1716. }
  1717. return Result;
  1718. }
  1719. //---------------------------------------------------------------------------
  1720. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1721. TStrings * FileList, const TCustomCommandType & ACommand,
  1722. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1723. {
  1724. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1725. {
  1726. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1727. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1728. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1729. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1730. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1731. bool Capture =
  1732. FLAGSET(ACommand.Params, ccShowResults) ||
  1733. FLAGSET(ACommand.Params, ccCopyResults) ||
  1734. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1735. TCaptureOutputEvent OutputEvent = NULL;
  1736. DebugAssert(FCapturedLog == NULL);
  1737. if (Capture)
  1738. {
  1739. FCapturedLog = new TStringList();
  1740. OutputEvent = TerminalCaptureLog;
  1741. }
  1742. try
  1743. {
  1744. if (!RemoteCustomCommand.IsFileCommand(Command))
  1745. {
  1746. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1747. OutputEvent);
  1748. }
  1749. else
  1750. {
  1751. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1752. }
  1753. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1754. {
  1755. if (FLAGSET(ACommand.Params, ccCopyResults))
  1756. {
  1757. CopyToClipboard(FCapturedLog);
  1758. }
  1759. if (FLAGSET(ACommand.Params, ccShowResults))
  1760. {
  1761. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1762. }
  1763. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1764. {
  1765. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1766. }
  1767. }
  1768. }
  1769. __finally
  1770. {
  1771. SAFE_DESTROY(FCapturedLog);
  1772. }
  1773. }
  1774. }
  1775. //---------------------------------------------------------------------------
  1776. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1777. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1778. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1779. {
  1780. DebugAssert(!IsLocalBrowserMode());
  1781. TStrings * LocalFileList = NULL;
  1782. TStrings * RemoteFileList = NULL;
  1783. TStrings * RemoteFileListFull = NULL;
  1784. try
  1785. {
  1786. if (LocalFileCommand)
  1787. {
  1788. if (ALocalFileList == NULL)
  1789. {
  1790. DebugAssert(HasDirView[osLocal]);
  1791. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1792. // directly (instead of EnableSelectedOperation) to pass
  1793. // false to FocusedFileOnlyWhenFocused
  1794. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1795. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1796. }
  1797. else
  1798. {
  1799. LocalFileList = ALocalFileList;
  1800. }
  1801. if (FileListCommand)
  1802. {
  1803. if (LocalFileList->Count != 1)
  1804. {
  1805. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1806. }
  1807. }
  1808. else
  1809. {
  1810. if ((LocalFileList->Count != 1) &&
  1811. (FileList->Count != 1) &&
  1812. (LocalFileList->Count != FileList->Count))
  1813. {
  1814. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1815. }
  1816. }
  1817. }
  1818. UnicodeString RootTempDir;
  1819. UnicodeString TempDir;
  1820. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1821. if (!RemoteFiles)
  1822. {
  1823. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1824. }
  1825. try
  1826. {
  1827. TDateTimes RemoteFileTimes;
  1828. if (RemoteFiles)
  1829. {
  1830. RemoteFileList = FileList;
  1831. }
  1832. else
  1833. {
  1834. RemoteFileList = new TStringList();
  1835. TMakeLocalFileListParams MakeFileListParam;
  1836. MakeFileListParam.FileList = RemoteFileList;
  1837. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1838. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1839. MakeFileListParam.Recursive =
  1840. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1841. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1842. if (!MakeFileListParam.Recursive)
  1843. {
  1844. RemoteFileTimes.clear();
  1845. RemoteFileListFull = new TStringList();
  1846. MakeFileListParam.FileList = RemoteFileListFull;
  1847. MakeFileListParam.Recursive = true;
  1848. MakeFileListParam.IncludeDirs = false;
  1849. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1850. }
  1851. else
  1852. {
  1853. RemoteFileListFull = RemoteFileList;
  1854. }
  1855. }
  1856. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1857. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1858. if (!NonBlocking)
  1859. {
  1860. int Count;
  1861. if (FileListCommand)
  1862. {
  1863. Count = 1;
  1864. }
  1865. else if (LocalFileCommand)
  1866. {
  1867. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  1868. }
  1869. else
  1870. {
  1871. Count = RemoteFileList->Count;
  1872. }
  1873. Progress.Start(foCustomCommand, osRemote, Count);
  1874. DebugAssert(FProgressForm != NULL);
  1875. FProgressForm->ReadOnly = true;
  1876. }
  1877. try
  1878. {
  1879. if (FileListCommand)
  1880. {
  1881. UnicodeString LocalFile;
  1882. // MakeFileList does not delimit filenames
  1883. UnicodeString FileList = MakeFileList(RemoteFileList);
  1884. if (LocalFileCommand)
  1885. {
  1886. DebugAssert(LocalFileList->Count == 1);
  1887. LocalFile = LocalFileList->Strings[0];
  1888. }
  1889. TLocalCustomCommand CustomCommand(Data,
  1890. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1891. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1892. if (NonBlocking)
  1893. {
  1894. DebugAssert(!POutput);
  1895. ExecuteShellChecked(ShellCommand);
  1896. }
  1897. else
  1898. {
  1899. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1900. }
  1901. }
  1902. else if (LocalFileCommand)
  1903. {
  1904. if (LocalFileList->Count == 1)
  1905. {
  1906. UnicodeString LocalFile = LocalFileList->Strings[0];
  1907. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1908. {
  1909. UnicodeString FileName = RemoteFileList->Strings[Index];
  1910. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1911. Progress.SetFile(FileNameForProgress);
  1912. TLocalCustomCommand CustomCommand(Data,
  1913. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  1914. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1915. TOnceDoneOperation OnceDoneOperation;
  1916. // Do not unselect anything, as it won't work with two panels anyway
  1917. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1918. }
  1919. }
  1920. else if (RemoteFileList->Count == 1)
  1921. {
  1922. UnicodeString FileName = RemoteFileList->Strings[0];
  1923. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1924. {
  1925. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1926. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1927. Progress.SetFile(LocalFileNameOnly);
  1928. TLocalCustomCommand CustomCommand(
  1929. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1930. FileName, LocalFileName, L"");
  1931. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1932. TOnceDoneOperation OnceDoneOperation;
  1933. // Do not unselect anything, as it won't work with two panels anyway
  1934. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1935. }
  1936. }
  1937. else
  1938. {
  1939. if (LocalFileList->Count != RemoteFileList->Count)
  1940. {
  1941. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  1942. }
  1943. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1944. {
  1945. UnicodeString FileName = RemoteFileList->Strings[Index];
  1946. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1947. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1948. Progress.SetFile(LocalFileNameOnly); // sic
  1949. TLocalCustomCommand CustomCommand(
  1950. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1951. FileName, LocalFileName, L"");
  1952. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1953. TOnceDoneOperation OnceDoneOperation;
  1954. // Do not unselect anything, as it won't work with two panels anyway
  1955. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1956. }
  1957. }
  1958. }
  1959. else
  1960. {
  1961. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1962. {
  1963. UnicodeString FileName = RemoteFileList->Strings[Index];
  1964. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1965. Progress.SetFile(FileNameForProgress);
  1966. TLocalCustomCommand CustomCommand(Data,
  1967. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1968. FileName, L"", L"");
  1969. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1970. TOnceDoneOperation OnceDoneOperation;
  1971. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  1972. }
  1973. }
  1974. }
  1975. __finally
  1976. {
  1977. if (!NonBlocking)
  1978. {
  1979. Progress.Stop();
  1980. }
  1981. }
  1982. DebugAssert(!FAutoOperation);
  1983. if (!RemoteFiles)
  1984. {
  1985. TempDir = IncludeTrailingBackslash(TempDir);
  1986. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  1987. {
  1988. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  1989. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  1990. // Skip directories, process only nested files.
  1991. // The check is redundant as FileAge fails for directories anyway.
  1992. !DirectoryExists(FileName))
  1993. {
  1994. UnicodeString RemoteDir =
  1995. UnixExtractFileDir(
  1996. UnixIncludeTrailingBackslash(FTerminal->CurrentDirectory) +
  1997. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  1998. TDateTime NewTime;
  1999. if (FileAge(FileName, NewTime) &&
  2000. (NewTime != RemoteFileTimes[Index]))
  2001. {
  2002. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2003. TemporaryFileCopyParam(CopyParam);
  2004. CopyParam.FileMask = L"";
  2005. FAutoOperation = true;
  2006. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2007. TemporaryFilesList->Add(FileName);
  2008. FTerminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2009. }
  2010. }
  2011. }
  2012. }
  2013. }
  2014. __finally
  2015. {
  2016. FAutoOperation = false;
  2017. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2018. {
  2019. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2020. }
  2021. }
  2022. }
  2023. __finally
  2024. {
  2025. if (RemoteFileList != FileList)
  2026. {
  2027. delete RemoteFileList;
  2028. }
  2029. if (LocalFileList != ALocalFileList)
  2030. {
  2031. delete LocalFileList;
  2032. }
  2033. if (RemoteFileListFull != RemoteFileList)
  2034. {
  2035. delete RemoteFileListFull;
  2036. }
  2037. }
  2038. }
  2039. //---------------------------------------------------------------------------
  2040. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2041. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2042. bool FileListCommand, UnicodeString * POutput)
  2043. {
  2044. std::unique_ptr<TStrings> SelectedFileList(DirView(osLocal)->CreateFileList(false, true, NULL));
  2045. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2046. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2047. {
  2048. UnicodeString FileName = SelectedFileList->Strings[Index];
  2049. if (DirectoryExists(FileName))
  2050. {
  2051. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2052. {
  2053. LocalFileList->Add(FileName);
  2054. }
  2055. if (FLAGSET(ACommand.Params, ccRecursive))
  2056. {
  2057. TMakeLocalFileListParams MakeFileListParam;
  2058. MakeFileListParam.FileList = LocalFileList.get();
  2059. MakeFileListParam.FileTimes = NULL;
  2060. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2061. MakeFileListParam.Recursive = true;
  2062. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2063. }
  2064. }
  2065. else
  2066. {
  2067. LocalFileList->Add(FileName);
  2068. }
  2069. }
  2070. UnicodeString RemotePath;
  2071. if (Terminal != NULL)
  2072. {
  2073. RemotePath = Terminal->CurrentDirectory;
  2074. }
  2075. if (FileListCommand)
  2076. {
  2077. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2078. TLocalCustomCommand CustomCommand(
  2079. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2080. L"", L"", FileList);
  2081. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2082. }
  2083. else
  2084. {
  2085. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2086. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2087. DebugAssert(FProgressForm != NULL);
  2088. FProgressForm->ReadOnly = true;
  2089. try
  2090. {
  2091. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2092. {
  2093. UnicodeString FileName = LocalFileList->Strings[Index];
  2094. Progress.SetFile(FileName);
  2095. TLocalCustomCommand CustomCommand(
  2096. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2097. FileName, L"", L"");
  2098. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2099. TOnceDoneOperation OnceDoneOperation;
  2100. Progress.Finish(FileName, true, OnceDoneOperation);
  2101. }
  2102. }
  2103. __finally
  2104. {
  2105. Progress.Stop();
  2106. }
  2107. }
  2108. }
  2109. //---------------------------------------------------------------------------
  2110. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2111. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2112. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2113. {
  2114. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2115. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2116. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2117. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2118. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2119. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2120. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2121. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2122. std::unique_ptr<UnicodeString> POutput;
  2123. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2124. FLAGSET(ACommand.Params, ccCopyResults))
  2125. {
  2126. POutput.reset(new UnicodeString());
  2127. }
  2128. if (!LocalCustomCommand.IsFileCommand(Command))
  2129. {
  2130. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2131. }
  2132. // remote files?
  2133. else if ((FCurrentSide == osRemote) || LocalFileCommand)
  2134. {
  2135. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2136. }
  2137. // local files
  2138. else
  2139. {
  2140. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2141. }
  2142. if (POutput.get() != NULL)
  2143. {
  2144. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2145. int P = POutput->Pos(sLineBreak);
  2146. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2147. {
  2148. POutput->SetLength(P - 1);
  2149. }
  2150. // Copy even empty output to clipboard
  2151. if (FLAGSET(ACommand.Params, ccCopyResults))
  2152. {
  2153. CopyToClipboard(*POutput);
  2154. }
  2155. // But do not show an empty message box.
  2156. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2157. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2158. !POutput->IsEmpty())
  2159. {
  2160. TClipboardHandler ClipboardHandler;
  2161. ClipboardHandler.Text = *POutput;
  2162. TMessageParams Params;
  2163. TQueryButtonAlias Aliases[1];
  2164. Aliases[0].Button = qaRetry;
  2165. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2166. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2167. Params.Aliases = Aliases;
  2168. Params.AliasesCount = LENOF(Aliases);
  2169. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2170. }
  2171. }
  2172. }
  2173. //---------------------------------------------------------------------------
  2174. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2175. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2176. {
  2177. TCustomCommandData Data;
  2178. UnicodeString Site;
  2179. UnicodeString RemotePath;
  2180. if (Terminal != NULL)
  2181. {
  2182. Data = TCustomCommandData(Terminal);
  2183. Site = Terminal->SessionData->SessionKey;
  2184. RemotePath = Terminal->CurrentDirectory;
  2185. }
  2186. UnicodeString HelpKeyword = ACommand.HomePage;
  2187. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2188. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2189. {
  2190. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2191. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2192. {
  2193. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2194. }
  2195. else
  2196. {
  2197. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2198. }
  2199. if (!DoCustomCommandOptionsDialog(
  2200. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2201. {
  2202. Abort();
  2203. }
  2204. }
  2205. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2206. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2207. {
  2208. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2209. }
  2210. else
  2211. {
  2212. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2213. }
  2214. }
  2215. //---------------------------------------------------------------------------
  2216. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2217. const TCustomCommandType & Command)
  2218. {
  2219. DebugAssert(FCustomCommandLocalFileList != NULL);
  2220. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2221. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2222. TStrings * LocalFileList = new TStringList();
  2223. TStrings * RemoteFileList = new TStringList();
  2224. try
  2225. {
  2226. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2227. {
  2228. LocalFileList->Clear();
  2229. LocalFileList->AddObject(
  2230. FCustomCommandLocalFileList->Strings[Index],
  2231. FCustomCommandLocalFileList->Objects[Index]);
  2232. RemoteFileList->Clear();
  2233. RemoteFileList->AddObject(
  2234. FCustomCommandRemoteFileList->Strings[Index],
  2235. FCustomCommandRemoteFileList->Objects[Index]);
  2236. CustomCommand(RemoteFileList, Command, LocalFileList);
  2237. }
  2238. }
  2239. __finally
  2240. {
  2241. delete LocalFileList;
  2242. delete RemoteFileList;
  2243. }
  2244. }
  2245. //---------------------------------------------------------------------------
  2246. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2247. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2248. {
  2249. delete FCustomCommandLocalFileList;
  2250. delete FCustomCommandRemoteFileList;
  2251. // takeover ownership,
  2252. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2253. FCustomCommandLocalFileList = LocalFileList;
  2254. FCustomCommandRemoteFileList = RemoteFileList;
  2255. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2256. if (Button != NULL)
  2257. {
  2258. FCustomCommandMenu->Items->Clear();
  2259. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth);
  2260. MenuPopup(FCustomCommandMenu, Button);
  2261. }
  2262. else
  2263. {
  2264. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2265. }
  2266. }
  2267. //---------------------------------------------------------------------------
  2268. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2269. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2270. {
  2271. DebugAssert(FCapturedLog != NULL);
  2272. if ((OutputType == cotOutput) || (OutputType == cotError))
  2273. {
  2274. FCapturedLog->Add(AddedLine);
  2275. }
  2276. }
  2277. //---------------------------------------------------------------------------
  2278. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2279. TOperationSide Side)
  2280. {
  2281. return
  2282. (Side == osOther) &&
  2283. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2284. }
  2285. //---------------------------------------------------------------------------
  2286. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2287. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2288. {
  2289. TTBItemOptions O;
  2290. O = Item->Options;
  2291. TCustomDirView * DView = DirView(Side);
  2292. if ((DView->ItemFocused != NULL) &&
  2293. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2294. // when resolving links is disabled, default action is to enter the directory,
  2295. // no matter what DoubleClickAction is configured to
  2296. (IsSideLocalBrowser(Side) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2297. // Can only Edit files, but can Open/Copy even directories
  2298. ((DoubleClickAction != dcaEdit) ||
  2299. !DView->ItemIsDirectory(DView->ItemFocused)))
  2300. {
  2301. Item->Options = O << tboDefault;
  2302. }
  2303. else
  2304. {
  2305. Item->Options = O >> tboDefault;
  2306. }
  2307. }
  2308. //---------------------------------------------------------------------------
  2309. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2310. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2311. {
  2312. TCustomDirView * DView = DirView(Side);
  2313. TListItem * Item = DView->ItemFocused;
  2314. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2315. EnableFocusedOperation[Side])
  2316. {
  2317. TPoint ClientPoint;
  2318. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2319. TPoint(0, 0) : MousePos;
  2320. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2321. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2322. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2323. }
  2324. }
  2325. //---------------------------------------------------------------------------
  2326. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2327. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2328. {
  2329. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2330. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2331. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2332. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2333. Handled = true;
  2334. }
  2335. //---------------------------------------------------------------------------
  2336. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2337. {
  2338. }
  2339. //---------------------------------------------------------------------------
  2340. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2341. {
  2342. DebugAssert(FErrorList == NULL);
  2343. if (WinConfiguration->ContinueOnError)
  2344. {
  2345. FErrorList = new TStringList();
  2346. Configuration->Usage->Inc(L"ContinuationsOnError");
  2347. }
  2348. NonVisualDataModule->StartBusy();
  2349. }
  2350. //---------------------------------------------------------------------------
  2351. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2352. {
  2353. NonVisualDataModule->EndBusy();
  2354. if (FErrorList)
  2355. {
  2356. HandleErrorList(FErrorList);
  2357. }
  2358. }
  2359. //---------------------------------------------------------------------------
  2360. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2361. const TCopyParamType & CopyParam)
  2362. {
  2363. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2364. {
  2365. Configuration->Usage->Inc(L"FileMaskUses");
  2366. }
  2367. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2368. {
  2369. Configuration->Usage->Inc(L"OperationMaskUses");
  2370. }
  2371. }
  2372. //---------------------------------------------------------------------------
  2373. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2374. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2375. {
  2376. DebugAssert(!IsLocalBrowserMode());
  2377. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2378. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2379. TTransferOperationParam DefaultParam;
  2380. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2381. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2382. switch (Param.Queue)
  2383. {
  2384. case asOn:
  2385. CopyParam.Queue = true;
  2386. break;
  2387. case asOff:
  2388. CopyParam.Queue = false;
  2389. break;
  2390. case asAuto:
  2391. default:
  2392. // keep default
  2393. break;
  2394. }
  2395. bool Result =
  2396. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2397. if (Result)
  2398. {
  2399. DebugAssert(Terminal);
  2400. bool SelectionRestored = false;
  2401. TCustomDirView * DView = NULL;
  2402. if (HasDirView[Side])
  2403. {
  2404. DView = DirView(Side);
  2405. DView->SaveSelection();
  2406. DView->SaveSelectedNames();
  2407. }
  2408. UpdateCopyParamCounters(CopyParam);
  2409. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2410. DebugAssert(FTransferResumeList == NULL);
  2411. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2412. FMoveToQueue = false;
  2413. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2414. try
  2415. {
  2416. TStrings * PermanentFileList;
  2417. std::unique_ptr<TStrings> PermanentFileListOwner;
  2418. try
  2419. {
  2420. if (Side == osLocal)
  2421. {
  2422. PermanentFileList = FileList;
  2423. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2424. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2425. if (Operation == foMove)
  2426. {
  2427. ReloadLocalDirectory();
  2428. if (DView != NULL)
  2429. {
  2430. DView->RestoreSelection();
  2431. }
  2432. SelectionRestored = true;
  2433. }
  2434. }
  2435. else
  2436. {
  2437. // Clone the file list as it may refer to current directory files,
  2438. // which get destroyed, when the source directory is reloaded after foMove operation.
  2439. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2440. // But for a hotfix, we are not going to do such a big change.
  2441. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2442. PermanentFileList = PermanentFileListOwner.get();
  2443. try
  2444. {
  2445. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2446. }
  2447. __finally
  2448. {
  2449. if (Operation == foMove)
  2450. {
  2451. if (DView != NULL)
  2452. {
  2453. DView->RestoreSelection();
  2454. }
  2455. SelectionRestored = true;
  2456. }
  2457. ReloadLocalDirectory(Param.TargetDirectory);
  2458. }
  2459. }
  2460. }
  2461. catch (EAbort &)
  2462. {
  2463. if (FMoveToQueue)
  2464. {
  2465. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2466. DebugAssert(CopyParam.TransferSkipList == NULL);
  2467. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2468. if (TransferResumeList->Count > 0)
  2469. {
  2470. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2471. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2472. }
  2473. CopyParam.TransferSkipList = TransferResumeList.release();
  2474. // not really needed, just to keep it consistent with TransferResumeList
  2475. FTransferResumeList = NULL;
  2476. FMoveToQueue = false;
  2477. Configuration->Usage->Inc("MovesToBackground");
  2478. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2479. ClearTransferSourceSelection(Direction);
  2480. }
  2481. throw;
  2482. }
  2483. }
  2484. __finally
  2485. {
  2486. if (!SelectionRestored && (DView != NULL))
  2487. {
  2488. DView->DiscardSavedSelection();
  2489. }
  2490. FTransferResumeList = NULL;
  2491. }
  2492. }
  2493. return Result;
  2494. }
  2495. //---------------------------------------------------------------------------
  2496. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2497. TOperationSide Side, TStrings * FileList, void * Param)
  2498. {
  2499. DebugAssert(FileList->Count);
  2500. // We deliberately do not toggle alternative flag (Param), but use OR,
  2501. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2502. // shortcut of CurrentDeleteAlternativeAction
  2503. bool Alternative =
  2504. bool(Param) || UseAlternativeFunction();
  2505. bool Recycle;
  2506. if (IsSideLocalBrowser(Side))
  2507. {
  2508. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2509. }
  2510. else
  2511. {
  2512. Recycle =
  2513. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2514. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2515. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2516. }
  2517. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2518. if (!Result)
  2519. {
  2520. UnicodeString Query;
  2521. if (FileList->Count == 1)
  2522. {
  2523. if (IsSideLocalBrowser(Side))
  2524. {
  2525. Query = ExtractFileName(FileList->Strings[0]);
  2526. }
  2527. else
  2528. {
  2529. Query = UnixExtractFileName(FileList->Strings[0]);
  2530. }
  2531. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2532. }
  2533. else
  2534. {
  2535. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2536. }
  2537. TMessageParams Params(mpNeverAskAgainCheck);
  2538. Params.ImageName = L"Delete file";
  2539. unsigned int Answer =
  2540. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2541. if (Answer == qaNeverAskAgain)
  2542. {
  2543. Result = true;
  2544. if (Recycle)
  2545. {
  2546. WinConfiguration->ConfirmRecycling = false;
  2547. }
  2548. else
  2549. {
  2550. WinConfiguration->ConfirmDeleting = false;
  2551. }
  2552. }
  2553. else
  2554. {
  2555. Result = (Answer == qaOK);
  2556. }
  2557. }
  2558. if (Result)
  2559. {
  2560. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2561. }
  2562. return Result;
  2563. }
  2564. //---------------------------------------------------------------------------
  2565. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2566. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2567. {
  2568. TAutoBatch AutoBatch(this);
  2569. bool Result;
  2570. if ((Operation == foCopy) || (Operation == foMove))
  2571. {
  2572. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2573. }
  2574. else if (Operation == foRename)
  2575. {
  2576. DebugAssert(DirView(Side)->ItemFocused);
  2577. DirView(Side)->ItemFocused->EditCaption();
  2578. Result = true;
  2579. }
  2580. else if (Operation == foDelete)
  2581. {
  2582. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2583. }
  2584. else if (Operation == foSetProperties)
  2585. {
  2586. DirView(osRemote)->SaveSelectedNames();
  2587. Result = SetProperties(Side, FileList);
  2588. }
  2589. else if (Operation == foCustomCommand)
  2590. {
  2591. DebugAssert(Param);
  2592. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2593. DirView(osRemote)->SaveSelectedNames();
  2594. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2595. CustomCommand(FileList, *Command, NULL);
  2596. Result = true;
  2597. }
  2598. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2599. {
  2600. DebugAssert(!IsSideLocalBrowser(Side));
  2601. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2602. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2603. }
  2604. else if (Operation == foLock)
  2605. {
  2606. DebugAssert(!IsSideLocalBrowser(Side));
  2607. LockFiles(FileList, true);
  2608. Result = true;
  2609. }
  2610. else if (Operation == foUnlock)
  2611. {
  2612. DebugAssert(!IsSideLocalBrowser(Side));
  2613. LockFiles(FileList, false);
  2614. Result = true;
  2615. }
  2616. else
  2617. {
  2618. DebugFail();
  2619. }
  2620. return Result;
  2621. }
  2622. //---------------------------------------------------------------------------
  2623. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2624. {
  2625. if (Side == osCurrent)
  2626. {
  2627. Side = FCurrentSide;
  2628. }
  2629. return Side;
  2630. }
  2631. //---------------------------------------------------------------------------
  2632. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2633. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2634. {
  2635. Side = GetSide(Side);
  2636. bool Result;
  2637. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, IsSideLocalBrowser(Side), NULL);
  2638. try
  2639. {
  2640. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2641. }
  2642. __finally
  2643. {
  2644. delete FileList;
  2645. }
  2646. return Result;
  2647. }
  2648. //---------------------------------------------------------------------------
  2649. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2650. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2651. bool NoConfirmation, void * Param)
  2652. {
  2653. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2654. {
  2655. if ((Operation == foCopy) || (Operation == foMove))
  2656. {
  2657. DebugAssert(!IsLocalBrowserMode());
  2658. if (GetSide(Side) == osLocal)
  2659. {
  2660. Configuration->Usage->Inc(L"UploadsCommand");
  2661. }
  2662. else
  2663. {
  2664. Configuration->Usage->Inc(L"DownloadsCommand");
  2665. }
  2666. }
  2667. }
  2668. }
  2669. //---------------------------------------------------------------------------
  2670. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2671. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2672. {
  2673. TTransferOperationParam Param;
  2674. if ((WinConfiguration->Interface != ifCommander) ||
  2675. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2676. {
  2677. Flags &= ~cocShortCutHint;
  2678. }
  2679. TCustomDirView * DView = DirView(Side);
  2680. Param.Options =
  2681. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2682. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2683. if (FLAGSET(Flags, cocQueue))
  2684. {
  2685. Param.Queue = asOn;
  2686. }
  2687. else if (FLAGSET(Flags, cocNonQueue))
  2688. {
  2689. Param.Queue = asOff;
  2690. }
  2691. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2692. }
  2693. //---------------------------------------------------------------------------
  2694. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2695. {
  2696. try
  2697. {
  2698. if (ErrorList->Count)
  2699. {
  2700. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2701. if (MessageDialog(Message, qtError,
  2702. qaOK | qaCancel, HELP_NONE) == qaOK)
  2703. {
  2704. unsigned int Answer;
  2705. int Index = 0;
  2706. do
  2707. {
  2708. DebugAssert(Index >= 0 && Index < ErrorList->Count);
  2709. TQueryButtonAlias Aliases[2];
  2710. Aliases[0].Button = qaYes;
  2711. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2712. Aliases[1].Button = qaNo;
  2713. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2714. TMessageParams Params;
  2715. Params.Aliases = Aliases;
  2716. Params.AliasesCount = LENOF(Aliases);
  2717. Answer = MoreMessageDialog(
  2718. FMTLOAD(ERROR_LIST_NUMBER, (Index+1, ErrorList->Count, ErrorList->Strings[Index])),
  2719. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2720. (Index ? qaYes : 0) | (Index < ErrorList->Count - 1 ? qaNo : 0) |
  2721. qaOK, HELP_NONE, &Params);
  2722. if (Answer == qaNo)
  2723. {
  2724. Index++;
  2725. }
  2726. if (Answer == qaYes)
  2727. {
  2728. Index--;
  2729. }
  2730. }
  2731. while (Answer != qaOK);
  2732. }
  2733. }
  2734. }
  2735. __finally
  2736. {
  2737. TStrings * List = ErrorList;
  2738. ErrorList = NULL;
  2739. for (int i = 0; i < List->Count; i++)
  2740. {
  2741. delete List->Objects[i];
  2742. }
  2743. delete List;
  2744. }
  2745. }
  2746. //---------------------------------------------------------------------------
  2747. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2748. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2749. {
  2750. // needed for checking filemasks, as there's no directory object
  2751. // associated with the file object
  2752. File->FullFileName = FullFileName;
  2753. TFileMasks::TParams MaskParams;
  2754. MaskParams.Size = File->Size;
  2755. MaskParams.Modification = File->Modification;
  2756. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2757. }
  2758. //---------------------------------------------------------------------------
  2759. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2760. {
  2761. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2762. Side = GetSide(Side);
  2763. TCustomDirView * DView = DirView(Side);
  2764. TListItem * FocusedItem = DView->ItemFocused;
  2765. UnicodeString Name;
  2766. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2767. {
  2768. Name = DView->ItemFileName(FocusedItem);
  2769. }
  2770. else
  2771. {
  2772. Name = LoadStr(NEW_FILE);
  2773. }
  2774. UnicodeString Names = Name;
  2775. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2776. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2777. HELP_EDIT_NEW, History.get(), true))
  2778. {
  2779. while (!Names.IsEmpty())
  2780. {
  2781. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2782. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2783. UnicodeString TargetFileName;
  2784. UnicodeString LocalFileName;
  2785. UnicodeString RootTempDir;
  2786. UnicodeString TempDir;
  2787. UnicodeString RemoteDirectory;
  2788. bool ExistingFile = false;
  2789. if (!IsSideLocalBrowser(Side))
  2790. {
  2791. Name = AbsolutePath(FTerminal->CurrentDirectory, Name);
  2792. TRemoteFile * File = NULL;
  2793. if (FTerminal->FileExists(Name, &File) &&
  2794. !File->IsDirectory)
  2795. {
  2796. try
  2797. {
  2798. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2799. ExistingFile = true;
  2800. }
  2801. __finally
  2802. {
  2803. delete File;
  2804. }
  2805. }
  2806. if (!ExistingFile)
  2807. {
  2808. RemoteDirectory = UnixExtractFilePath(Name);
  2809. TemporaryDirectoryForRemoteFiles(
  2810. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2811. TargetFileName = UnixExtractFileName(Name);
  2812. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2813. LocalFileName = TempDir +
  2814. // We probably do not want to trim the VMS version here
  2815. FTerminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2816. }
  2817. }
  2818. else
  2819. {
  2820. if (ExtractFilePath(Name).IsEmpty())
  2821. {
  2822. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2823. }
  2824. else
  2825. {
  2826. LocalFileName = ExpandFileName(Name);
  2827. }
  2828. TargetFileName = ExtractFileName(Name);
  2829. }
  2830. if (!ExistingFile)
  2831. {
  2832. bool NewFile = !FileExists(ApiPath(LocalFileName));
  2833. if (NewFile)
  2834. {
  2835. int File = FileCreate(ApiPath(LocalFileName));
  2836. if (File < 0)
  2837. {
  2838. if (!RootTempDir.IsEmpty())
  2839. {
  2840. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2841. }
  2842. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2843. }
  2844. else
  2845. {
  2846. FileClose(File);
  2847. }
  2848. }
  2849. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2850. const TEditorData * ExternalEditor = NULL;
  2851. TFileMasks::TParams MaskParams; // size not known
  2852. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2853. false, MaskParams);
  2854. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2855. ExternalEditor, RootTempDir, RemoteDirectory, NewFile);
  2856. }
  2857. }
  2858. }
  2859. }
  2860. //---------------------------------------------------------------------------
  2861. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2862. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2863. {
  2864. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2865. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2866. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2867. return
  2868. ((ExecuteFileBy == efInternalEditor)) ||
  2869. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2870. }
  2871. //---------------------------------------------------------------------------
  2872. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2873. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2874. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2875. UnicodeString RemoteDirectory, bool NewFile)
  2876. {
  2877. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2878. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2879. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2880. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2881. Side = GetSide(Side);
  2882. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2883. if (!IsSideLocalBrowser(Side))
  2884. {
  2885. Data->Terminal = Terminal;
  2886. Data->Queue = Queue;
  2887. Data->SessionData = CloneCurrentSessionData();
  2888. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2889. Data->RemoteDirectory = RemoteDirectory;
  2890. Data->SessionName = Terminal->SessionData->SessionName;
  2891. Data->LocalRootDirectory = LocalRootDirectory;
  2892. Data->OriginalFileName = OriginalFileName;
  2893. Data->Command = L""; // will be changed later for external editor
  2894. }
  2895. if (ExecuteFileBy == efInternalEditor)
  2896. {
  2897. if (!IsSideLocalBrowser(Side))
  2898. {
  2899. UnicodeString Caption = UnixIncludeTrailingBackslash(RemoteDirectory) + OriginalFileName +
  2900. L" - " + Terminal->SessionData->SessionName;
  2901. TForm * Editor;
  2902. try
  2903. {
  2904. TNotifyEvent OnFileChanged = NULL;
  2905. TNotifyEvent OnSaveAll = NULL;
  2906. TAnyModifiedEvent OnAnyModified = NULL;
  2907. // Edited files are uploaded in background queue, what we do not support with encrypted files
  2908. if (Terminal->IsCapable[fsBackgroundTransfers])
  2909. {
  2910. OnFileChanged = FEditorManager->FileChanged;
  2911. OnSaveAll = SaveAllInternalEditors;
  2912. OnAnyModified = AnyInternalEditorModified;
  2913. }
  2914. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  2915. FEditorManager->FileReload, FEditorManager->FileClosed,
  2916. OnSaveAll, OnAnyModified,
  2917. Caption, FStandaloneEditing, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  2918. }
  2919. catch(...)
  2920. {
  2921. if (!LocalRootDirectory.IsEmpty())
  2922. {
  2923. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  2924. }
  2925. throw;
  2926. }
  2927. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  2928. }
  2929. else
  2930. {
  2931. DebugAssert(!FStandaloneEditing);
  2932. TForm * Editor =
  2933. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  2934. SaveAllInternalEditors, AnyInternalEditorModified,
  2935. L"", false, SessionColor, -1, NewFile);
  2936. FLocalEditors->Add(Editor);
  2937. }
  2938. }
  2939. else
  2940. {
  2941. HANDLE Process;
  2942. if (ExecuteFileBy == efExternalEditor)
  2943. {
  2944. UnicodeString Program, Params, Dir;
  2945. Data->Command = ExternalEditor->ExternalEditor;
  2946. ReformatFileNameCommand(Data->Command);
  2947. SplitCommand(Data->Command, Program, Params, Dir);
  2948. Params = ExpandFileNameCommand(Params, FileName);
  2949. Program = ExpandEnvironmentVariables(Program);
  2950. if (!ExecuteShell(Program, Params, Process))
  2951. {
  2952. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  2953. }
  2954. }
  2955. else
  2956. {
  2957. DebugAssert(IsSideLocalBrowser(Side));
  2958. if (!ExecuteShell(FileName, L"", Process))
  2959. {
  2960. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  2961. }
  2962. }
  2963. if (IsSideLocalBrowser(Side) ||
  2964. ((ExecuteFileBy == efShell) &&
  2965. !WinConfiguration->Editor.SDIShellEditor) ||
  2966. ((ExecuteFileBy == efExternalEditor) &&
  2967. !ExternalEditor->SDIExternalEditor))
  2968. {
  2969. // no need for handle
  2970. if (Process != NULL)
  2971. {
  2972. DebugCheck(CloseHandle(Process));
  2973. }
  2974. Process = INVALID_HANDLE_VALUE;
  2975. }
  2976. else
  2977. {
  2978. if (Process == NULL)
  2979. {
  2980. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  2981. }
  2982. }
  2983. if (!IsSideLocalBrowser(Side))
  2984. {
  2985. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  2986. }
  2987. }
  2988. }
  2989. //---------------------------------------------------------------------------
  2990. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  2991. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2992. void * /*Arg*/)
  2993. {
  2994. if (Token != NULL)
  2995. {
  2996. EditorFormFileSave(static_cast<TForm *>(Token));
  2997. }
  2998. }
  2999. //---------------------------------------------------------------------------
  3000. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3001. {
  3002. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3003. {
  3004. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3005. }
  3006. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3007. }
  3008. //---------------------------------------------------------------------------
  3009. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3010. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3011. void * Arg)
  3012. {
  3013. if ((Token != NULL) &&
  3014. IsEditorFormModified(static_cast<TForm *>(Token)))
  3015. {
  3016. *static_cast<bool *>(Arg) = true;
  3017. }
  3018. }
  3019. //---------------------------------------------------------------------------
  3020. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3021. TObject * /*Sender*/, bool & Modified)
  3022. {
  3023. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3024. {
  3025. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3026. {
  3027. Modified = true;
  3028. }
  3029. }
  3030. if (!Modified)
  3031. {
  3032. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3033. }
  3034. }
  3035. //---------------------------------------------------------------------------
  3036. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3037. {
  3038. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3039. }
  3040. //---------------------------------------------------------------------------
  3041. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3042. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3043. UnicodeString & Result, UnicodeString & RootDirectory)
  3044. {
  3045. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3046. {
  3047. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3048. Result = RootDirectory;
  3049. }
  3050. else
  3051. {
  3052. RootDirectory = L"";
  3053. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3054. Result = IncludeTrailingBackslash(Result);
  3055. }
  3056. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3057. {
  3058. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3059. }
  3060. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3061. {
  3062. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3063. {
  3064. RemoteDirectory.Delete(1, 1);
  3065. }
  3066. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3067. }
  3068. if (!ForceDirectories(ApiPath(Result)))
  3069. {
  3070. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3071. }
  3072. }
  3073. //---------------------------------------------------------------------------
  3074. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3075. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3076. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  3077. {
  3078. DebugAssert(!IsLocalBrowserMode());
  3079. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3080. if (ForceText)
  3081. {
  3082. CopyParam.TransferMode = tmAscii;
  3083. }
  3084. TemporaryFileCopyParam(CopyParam);
  3085. if (AllFiles)
  3086. {
  3087. CopyParam.IncludeFileMask = TFileMasks();
  3088. }
  3089. if (TempDir.IsEmpty())
  3090. {
  3091. TemporaryDirectoryForRemoteFiles(FTerminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3092. }
  3093. DebugAssert(!FAutoOperation);
  3094. FAutoOperation = AutoOperation;
  3095. Terminal->ExceptionOnFail = true;
  3096. try
  3097. {
  3098. try
  3099. {
  3100. // turn off confirmations, as for MDI editors we may possibly download
  3101. // the same file over
  3102. Terminal->CopyToLocal(
  3103. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3104. if (GetTargetNames)
  3105. {
  3106. for (int i = 0; i < FileList->Count; i++)
  3107. {
  3108. FileList->Strings[i] =
  3109. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3110. }
  3111. }
  3112. }
  3113. catch(...)
  3114. {
  3115. if (!RootTempDir.IsEmpty())
  3116. {
  3117. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3118. }
  3119. throw;
  3120. }
  3121. }
  3122. __finally
  3123. {
  3124. FAutoOperation = false;
  3125. Terminal->ExceptionOnFail = false;
  3126. }
  3127. }
  3128. //---------------------------------------------------------------------------
  3129. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3130. {
  3131. // Do not waste time checking for default editor list the next time,
  3132. // or testing if default editor is not notepad.
  3133. bool TryNextTime = false;
  3134. UnicodeString UsageState;
  3135. if (!WinConfiguration->EditorList->IsDefaultList())
  3136. {
  3137. UsageState = "H";
  3138. }
  3139. else
  3140. {
  3141. UnicodeString Executable;
  3142. UnicodeString ExecutableDescription;
  3143. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3144. {
  3145. UnicodeString Message =
  3146. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3147. unsigned int Answer =
  3148. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3149. if (Answer != qaOK)
  3150. {
  3151. UsageState = "R";
  3152. }
  3153. else
  3154. {
  3155. UsageState = "A";
  3156. TEditorData EditorData;
  3157. EditorData.Editor = edExternal;
  3158. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3159. EditorData.DecideExternalEditorText();
  3160. TEditorList EditorList;
  3161. EditorList = *WinConfiguration->EditorList;
  3162. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3163. WinConfiguration->EditorList = &EditorList;
  3164. }
  3165. }
  3166. }
  3167. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3168. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3169. }
  3170. //---------------------------------------------------------------------------
  3171. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3172. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3173. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3174. {
  3175. if (ExecuteFileBy == efDefaultEditor)
  3176. {
  3177. if (!WinConfiguration->OfferedEditorAutoConfig)
  3178. {
  3179. EditorAutoConfig();
  3180. }
  3181. const TEditorPreferences * Editor =
  3182. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3183. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3184. {
  3185. ExecuteFileBy = efInternalEditor;
  3186. ExternalEditor = NULL;
  3187. }
  3188. else if (Editor->Data->Editor == edOpen)
  3189. {
  3190. ExecuteFileBy = efShell;
  3191. ExternalEditor = NULL;
  3192. }
  3193. else
  3194. {
  3195. ExecuteFileBy = efExternalEditor;
  3196. ExternalEditor = Editor->Data;
  3197. }
  3198. }
  3199. }
  3200. //---------------------------------------------------------------------------
  3201. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3202. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3203. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3204. {
  3205. UnicodeString OriginalFileName;
  3206. UnicodeString LocalRootDirectory;
  3207. UnicodeString RemoteDirectory;
  3208. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3209. IsSideLocalBrowser(Side), MaskParams);
  3210. UnicodeString Counter;
  3211. UnicodeString LocalFileName;
  3212. bool Handled = false;
  3213. if (!IsSideLocalBrowser(Side))
  3214. {
  3215. // We need to trim VMS version here, so that we use name without version
  3216. // when uploading back to create a new version of the file
  3217. OriginalFileName = FTerminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3218. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3219. TObject * Token = NULL;
  3220. UnicodeString LocalDirectory;
  3221. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3222. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3223. LocalDirectory))
  3224. {
  3225. if (Token != NULL)
  3226. {
  3227. TForm * Form = dynamic_cast<TForm *>(Token);
  3228. if (Form->WindowState == wsMinimized)
  3229. {
  3230. ShowWindow(Form->Handle, SW_RESTORE);
  3231. }
  3232. else
  3233. {
  3234. Form->SetFocus();
  3235. }
  3236. Handled = true;
  3237. }
  3238. else
  3239. {
  3240. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3241. }
  3242. }
  3243. if (!Handled)
  3244. {
  3245. TStringList * FileList1 = new TStringList();
  3246. try
  3247. {
  3248. FileList1->AddObject(FullFileName, Object);
  3249. TemporarilyDownloadFiles(FileList1,
  3250. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3251. LocalRootDirectory, LocalDirectory, true, true, true);
  3252. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3253. }
  3254. __finally
  3255. {
  3256. delete FileList1;
  3257. }
  3258. switch (ExecuteFileBy)
  3259. {
  3260. case efShell:
  3261. Counter = "RemoteFilesExecuted";
  3262. break;
  3263. case efInternalEditor:
  3264. Counter = "RemoteFilesOpenedInInternalEditor";
  3265. break;
  3266. case efExternalEditor:
  3267. Counter = "RemoteFilesOpenedInExternalEditor";
  3268. break;
  3269. default:
  3270. DebugFail();
  3271. }
  3272. }
  3273. }
  3274. else
  3275. {
  3276. LocalFileName = FullFileName;
  3277. OriginalFileName = ExtractFileName(FullFileName);
  3278. switch (ExecuteFileBy)
  3279. {
  3280. case efShell:
  3281. Counter = "LocalFilesExecuted";
  3282. break;
  3283. case efInternalEditor:
  3284. Counter = "LocalFilesOpenedInInternalEditor";
  3285. break;
  3286. case efExternalEditor:
  3287. Counter = "LocalFilesOpenedInExternalEditor";
  3288. break;
  3289. default:
  3290. DebugFail();
  3291. }
  3292. }
  3293. if (!Handled)
  3294. {
  3295. Configuration->Usage->Inc(Counter);
  3296. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3297. ExternalEditor, LocalRootDirectory, RemoteDirectory, false);
  3298. }
  3299. }
  3300. //---------------------------------------------------------------------------
  3301. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3302. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3303. bool AllSelected, bool OnFocused)
  3304. {
  3305. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3306. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3307. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3308. Side = GetSide(Side);
  3309. TCustomDirView * DView = DirView(Side);
  3310. TStrings * FileList =
  3311. AllSelected ?
  3312. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3313. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3314. try
  3315. {
  3316. DebugAssert(AllSelected || (FileList->Count == 1));
  3317. for (int i = 0; i < FileList->Count; i++)
  3318. {
  3319. UnicodeString ListFileName = FileList->Strings[i];
  3320. UnicodeString FileNameOnly =
  3321. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3322. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3323. if (!DView->ItemIsDirectory(Item))
  3324. {
  3325. UnicodeString FullFileName;
  3326. if (!IsSideLocalBrowser(Side))
  3327. {
  3328. FullFileName = DirView(Side)->Path + ListFileName;
  3329. }
  3330. else
  3331. {
  3332. FullFileName = ListFileName;
  3333. }
  3334. TFileMasks::TParams MaskParams;
  3335. MaskParams.Size = DView->ItemFileSize(Item);
  3336. TDateTimePrecision Precision;
  3337. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3338. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3339. FileList->Objects[i], MaskParams);
  3340. }
  3341. }
  3342. }
  3343. __finally
  3344. {
  3345. delete FileList;
  3346. }
  3347. }
  3348. //---------------------------------------------------------------------------
  3349. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3350. {
  3351. // do not forget to add additional options to TemporarilyDownloadFiles, and AS
  3352. CopyParam.FileNameCase = ncNoChange;
  3353. CopyParam.PreserveRights = false;
  3354. CopyParam.PreserveReadOnly = false;
  3355. CopyParam.ReplaceInvalidChars = true;
  3356. CopyParam.IncludeFileMask = TFileMasks();
  3357. CopyParam.NewerOnly = false;
  3358. CopyParam.FileMask = L"";
  3359. }
  3360. //---------------------------------------------------------------------------
  3361. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3362. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3363. {
  3364. TTerminalManager * Manager = TTerminalManager::Instance();
  3365. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3366. {
  3367. if (!NonVisualDataModule->Busy)
  3368. {
  3369. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3370. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3371. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3372. if (Data->Terminal == NULL)
  3373. {
  3374. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3375. if (SameSiteTerminal != NULL)
  3376. {
  3377. UnicodeString Message =
  3378. FMTLOAD(EDIT_SESSION_REATTACH,
  3379. (FileNameOnly, Data->SessionName, FileNameOnly));
  3380. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3381. {
  3382. Data->Terminal = Terminal;
  3383. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3384. Data->SessionName = Terminal->SessionData->SessionName;
  3385. // We might also overwrite session data
  3386. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3387. }
  3388. else
  3389. {
  3390. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3391. Abort();
  3392. }
  3393. }
  3394. }
  3395. // foreground session should reconnect itself
  3396. else if (Terminal != Data->Terminal)
  3397. {
  3398. UnicodeString Message =
  3399. MainInstructions(
  3400. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3401. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3402. {
  3403. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3404. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3405. }
  3406. else
  3407. {
  3408. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3409. Abort();
  3410. }
  3411. }
  3412. }
  3413. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3414. {
  3415. Configuration->Usage->Inc(L"EditInactiveSession");
  3416. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3417. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3418. (ExtractFileName(FileName), Data->SessionName)));
  3419. }
  3420. }
  3421. TStrings * FileList = new TStringList();
  3422. try
  3423. {
  3424. FileList->Add(FileName);
  3425. // consider using the same settings (preset) as when the file was downloaded
  3426. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3427. TemporaryFileCopyParam(CopyParam);
  3428. if (Data->ForceText)
  3429. {
  3430. CopyParam.TransferMode = tmAscii;
  3431. }
  3432. // so i do not need to worry if masking algorithm works in all cases
  3433. // ("" means "copy file name", no masking is actually done)
  3434. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3435. {
  3436. CopyParam.FileMask = L"";
  3437. }
  3438. else
  3439. {
  3440. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3441. }
  3442. DebugAssert(Data->Queue != NULL);
  3443. int Params = cpNoConfirmation | cpTemporary;
  3444. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3445. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3446. QueueItem->CompleteEvent = UploadCompleteEvent;
  3447. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3448. }
  3449. __finally
  3450. {
  3451. delete FileList;
  3452. }
  3453. }
  3454. //---------------------------------------------------------------------------
  3455. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3456. const UnicodeString FileName, const TEditedFileData * Data)
  3457. {
  3458. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3459. DebugAssert(!NonVisualDataModule->Busy);
  3460. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3461. {
  3462. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3463. (ExtractFileName(FileName), Data->SessionName)));
  3464. }
  3465. TManagedTerminal * PrevTerminal = TTerminalManager::Instance()->ActiveTerminal;
  3466. TTerminalManager::Instance()->ActiveTerminal = Data->Terminal;
  3467. NonVisualDataModule->StartBusy();
  3468. try
  3469. {
  3470. std::unique_ptr<TRemoteFile> File;
  3471. UnicodeString RemoteFileName =
  3472. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3473. FTerminal->ExceptionOnFail = true;
  3474. try
  3475. {
  3476. TRemoteFile * AFile = NULL;
  3477. FTerminal->ReadFile(RemoteFileName, AFile);
  3478. File.reset(AFile);
  3479. if (!File->HaveFullFileName)
  3480. {
  3481. File->FullFileName = RemoteFileName;
  3482. }
  3483. }
  3484. __finally
  3485. {
  3486. FTerminal->ExceptionOnFail = false;
  3487. }
  3488. std::unique_ptr<TStrings> FileList(new TStringList());
  3489. FileList->AddObject(RemoteFileName, File.get());
  3490. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3491. UnicodeString TempDir = ExtractFilePath(FileName);
  3492. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3493. TempDir, true, true, true);
  3494. // sanity check, the target file name should be still the same
  3495. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3496. }
  3497. __finally
  3498. {
  3499. NonVisualDataModule->EndBusy();
  3500. // it actually may not exist anymore...
  3501. TTerminalManager::Instance()->ActiveTerminal = PrevTerminal;
  3502. }
  3503. }
  3504. //---------------------------------------------------------------------------
  3505. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3506. const TEditedFileData * Data, bool & KeepOpen)
  3507. {
  3508. // Command is set for external editors only (not for "shell" open).
  3509. if (!Data->Command.IsEmpty())
  3510. {
  3511. bool AnyFound = false;
  3512. bool AnyMDI = false;
  3513. bool AnyNonMDI = false;
  3514. bool AnyDetect = false;
  3515. TEditorList * EditorList = new TEditorList();
  3516. try
  3517. {
  3518. *EditorList = *WinConfiguration->EditorList;
  3519. for (int i = 0; i < EditorList->Count; i++)
  3520. {
  3521. const TEditorPreferences * Editor = EditorList->Editors[i];
  3522. if ((Editor->Data->Editor == edExternal) &&
  3523. (Editor->Data->ExternalEditor == Data->Command))
  3524. {
  3525. AnyFound = true;
  3526. if (Editor->Data->SDIExternalEditor)
  3527. {
  3528. AnyNonMDI = true;
  3529. if (Editor->Data->DetectMDIExternalEditor)
  3530. {
  3531. AnyDetect = true;
  3532. }
  3533. }
  3534. else
  3535. {
  3536. AnyMDI = true;
  3537. }
  3538. }
  3539. }
  3540. bool EnableMDI = false;
  3541. bool DisableDetect = false;
  3542. if (AnyMDI)
  3543. {
  3544. KeepOpen = true;
  3545. if (AnyNonMDI)
  3546. {
  3547. // there is at least one instance of the editor with MDI support enabled,
  3548. // and one with disabled, enable it for all instances
  3549. EnableMDI = true;
  3550. }
  3551. }
  3552. else if (AnyFound && !AnyDetect)
  3553. {
  3554. // at least once instance found but all have MDI autodetection disabled
  3555. // => close the file (default action)
  3556. }
  3557. else
  3558. {
  3559. // no instance of the editor has MDI support enabled
  3560. TMessageParams Params;
  3561. if (AnyFound)
  3562. {
  3563. // there is at least one instance of the editor
  3564. Params.Params |= mpNeverAskAgainCheck;
  3565. }
  3566. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3567. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3568. switch (Answer)
  3569. {
  3570. case qaNeverAskAgain:
  3571. DisableDetect = true;
  3572. break;
  3573. case qaNo:
  3574. EnableMDI = true;
  3575. KeepOpen = true;
  3576. break;
  3577. }
  3578. }
  3579. if (AnyFound && (EnableMDI || DisableDetect))
  3580. {
  3581. bool Changed = false;
  3582. for (int i = 0; i < EditorList->Count; i++)
  3583. {
  3584. const TEditorPreferences * Editor = EditorList->Editors[i];
  3585. if ((Editor->Data->Editor == edExternal) &&
  3586. (Editor->Data->ExternalEditor == Data->Command) &&
  3587. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3588. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3589. {
  3590. Changed = true;
  3591. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3592. if (EnableMDI)
  3593. {
  3594. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3595. }
  3596. if (DisableDetect)
  3597. {
  3598. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3599. }
  3600. EditorList->Change(i, UpdatedEditor);
  3601. }
  3602. }
  3603. if (Changed)
  3604. {
  3605. WinConfiguration->EditorList = EditorList;
  3606. }
  3607. }
  3608. }
  3609. __finally
  3610. {
  3611. delete EditorList;
  3612. }
  3613. }
  3614. else
  3615. {
  3616. // "open" case
  3617. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3618. qaOK, HELP_APP_EARLY_CLOSED);
  3619. }
  3620. }
  3621. //---------------------------------------------------------------------------
  3622. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3623. {
  3624. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3625. }
  3626. //---------------------------------------------------------------------------
  3627. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3628. {
  3629. SideEnter(osRemote);
  3630. }
  3631. //---------------------------------------------------------------------------
  3632. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3633. {
  3634. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3635. SideEnter(osRemote);
  3636. }
  3637. //---------------------------------------------------------------------------
  3638. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3639. {
  3640. FCurrentSide = Side;
  3641. if (Visible)
  3642. {
  3643. UpdateControls();
  3644. }
  3645. }
  3646. //---------------------------------------------------------------------------
  3647. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3648. TStrings * FileList, bool Alternative)
  3649. {
  3650. TCustomDirView * DView = DirView(Side);
  3651. DView->SaveSelection();
  3652. DView->SaveSelectedNames();
  3653. DebugAssert(!FAlternativeDelete);
  3654. FAlternativeDelete = Alternative;
  3655. try
  3656. {
  3657. if (!IsSideLocalBrowser(Side))
  3658. {
  3659. DebugAssert(Terminal != NULL);
  3660. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3661. }
  3662. else
  3663. {
  3664. try
  3665. {
  3666. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3667. }
  3668. __finally
  3669. {
  3670. ReloadLocalDirectory();
  3671. }
  3672. }
  3673. FAlternativeDelete = false;
  3674. }
  3675. catch(...)
  3676. {
  3677. FAlternativeDelete = false;
  3678. DView->DiscardSavedSelection();
  3679. throw;
  3680. }
  3681. DView->RestoreSelection();
  3682. }
  3683. //---------------------------------------------------------------------------
  3684. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3685. {
  3686. DebugAssert(Terminal);
  3687. DebugAssert(!IsLocalBrowserMode());
  3688. RemoteDirView->SaveSelection();
  3689. RemoteDirView->SaveSelectedNames();
  3690. try
  3691. {
  3692. if (Lock)
  3693. {
  3694. Terminal->LockFiles(FileList);
  3695. }
  3696. else
  3697. {
  3698. Terminal->UnlockFiles(FileList);
  3699. }
  3700. }
  3701. catch(...)
  3702. {
  3703. RemoteDirView->DiscardSavedSelection();
  3704. throw;
  3705. }
  3706. RemoteDirView->RestoreSelection();
  3707. }
  3708. //---------------------------------------------------------------------------
  3709. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  3710. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3711. bool NoConfirmation, bool Move)
  3712. {
  3713. DebugAssert(Terminal != NULL);
  3714. DebugAssert(!IsLocalBrowserMode());
  3715. // update Terminal->StateData->RemoteDirectory
  3716. UpdateTerminal(Terminal);
  3717. if (Session == NULL)
  3718. {
  3719. Session = TTerminalManager::Instance()->ActiveTerminal;
  3720. }
  3721. if (Session == TTerminalManager::Instance()->ActiveTerminal)
  3722. {
  3723. if (RemoteDriveView->DropTarget != NULL)
  3724. {
  3725. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3726. }
  3727. else if (RemoteDirView->DropTarget != NULL)
  3728. {
  3729. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3730. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3731. }
  3732. else
  3733. {
  3734. Target = RemoteDirView->Path;
  3735. }
  3736. }
  3737. else
  3738. {
  3739. Target = Session->StateData->RemoteDirectory;
  3740. }
  3741. Target = UnixIncludeTrailingBackslash(Target);
  3742. if (FileList->Count == 1)
  3743. {
  3744. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3745. }
  3746. else
  3747. {
  3748. FileMask = AnyMask;
  3749. }
  3750. DirectCopy = FTerminal->IsCapable[fcRemoteCopy] || FTerminal->IsCapable[fcSecondaryShell];
  3751. bool Result = true;
  3752. if (!NoConfirmation)
  3753. {
  3754. bool Multi = (FileList->Count > 1);
  3755. if (Move)
  3756. {
  3757. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3758. }
  3759. else
  3760. {
  3761. TStrings * Sessions = TTerminalManager::Instance()->TerminalList;
  3762. TStrings * Directories = new TStringList;
  3763. try
  3764. {
  3765. for (int Index = 0; Index < Sessions->Count; Index++)
  3766. {
  3767. TManagedTerminal * Terminal =
  3768. dynamic_cast<TManagedTerminal *>(Sessions->Objects[Index]);
  3769. Directories->Add(Terminal->StateData->RemoteDirectory);
  3770. }
  3771. TDirectRemoteCopy AllowDirectCopy;
  3772. if (FTerminal->IsCapable[fcRemoteCopy] || FTerminal->CommandSessionOpened)
  3773. {
  3774. DebugAssert(DirectCopy);
  3775. AllowDirectCopy = drcAllow;
  3776. }
  3777. else if (FTerminal->IsCapable[fcSecondaryShell])
  3778. {
  3779. DebugAssert(DirectCopy);
  3780. AllowDirectCopy = drcConfirmCommandSession;
  3781. }
  3782. else
  3783. {
  3784. DebugAssert(!DirectCopy);
  3785. AllowDirectCopy = drcDisallow;
  3786. }
  3787. void * ASession = Session;
  3788. Result = DoRemoteCopyDialog(Sessions, Directories, AllowDirectCopy,
  3789. Multi, ASession, Target, FileMask, DirectCopy, TTerminalManager::Instance()->ActiveTerminal);
  3790. Session = static_cast<TManagedTerminal *>(ASession);
  3791. }
  3792. __finally
  3793. {
  3794. delete Directories;
  3795. }
  3796. }
  3797. }
  3798. return Result;
  3799. }
  3800. //---------------------------------------------------------------------------
  3801. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3802. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  3803. {
  3804. DebugAssert(!IsLocalBrowserMode());
  3805. bool DirectCopy;
  3806. UnicodeString Target, FileMask;
  3807. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3808. if (Result)
  3809. {
  3810. if (!Move && !DirectCopy)
  3811. {
  3812. Configuration->Usage->Inc("RemoteCopyTemp");
  3813. UnicodeString RootTempDir;
  3814. UnicodeString TempDir;
  3815. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3816. TStrings * TemporaryFilesList = new TStringList();
  3817. try
  3818. {
  3819. TMakeLocalFileListParams MakeFileListParam;
  3820. MakeFileListParam.FileList = TemporaryFilesList;
  3821. MakeFileListParam.FileTimes = NULL;
  3822. MakeFileListParam.IncludeDirs = true;
  3823. MakeFileListParam.Recursive = false;
  3824. ProcessLocalDirectory(TempDir, FTerminal->MakeLocalFileList, &MakeFileListParam);
  3825. TTerminalManager::Instance()->ActiveTerminal = Session;
  3826. if (TemporaryFilesList->Count > 0)
  3827. {
  3828. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3829. CopyParam.FileMask = FileMask;
  3830. DebugAssert(!FAutoOperation);
  3831. FAutoOperation = true;
  3832. FTerminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3833. }
  3834. }
  3835. __finally
  3836. {
  3837. delete TemporaryFilesList;
  3838. FAutoOperation = false;
  3839. if (!RootTempDir.IsEmpty())
  3840. {
  3841. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3842. }
  3843. }
  3844. }
  3845. else
  3846. {
  3847. RemoteDirView->SaveSelection();
  3848. RemoteDirView->SaveSelectedNames();
  3849. try
  3850. {
  3851. if (Move)
  3852. {
  3853. Configuration->Usage->Inc("RemoteMove");
  3854. Terminal->MoveFiles(FileList, Target, FileMask);
  3855. }
  3856. else
  3857. {
  3858. Configuration->Usage->Inc("RemoteCopyDirect");
  3859. DebugAssert(DirectCopy);
  3860. DebugAssert(Session == FTerminal);
  3861. if (FTerminal->IsCapable[fcRemoteCopy] ||
  3862. FTerminal->CommandSessionOpened ||
  3863. CommandSessionFallback())
  3864. {
  3865. Terminal->CopyFiles(FileList, Target, FileMask);
  3866. }
  3867. }
  3868. }
  3869. catch(...)
  3870. {
  3871. RemoteDirView->DiscardSavedSelection();
  3872. throw;
  3873. }
  3874. RemoteDirView->RestoreSelection();
  3875. }
  3876. }
  3877. return Result;
  3878. }
  3879. //---------------------------------------------------------------------------
  3880. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3881. const UnicodeString & Path, TRemoteProperties & Properties)
  3882. {
  3883. DebugAssert(!IsLocalBrowserMode());
  3884. Properties.Valid = Properties.Valid << vpEncrypt;
  3885. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  3886. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  3887. }
  3888. //---------------------------------------------------------------------------
  3889. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3890. {
  3891. Side = GetSide(Side);
  3892. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3893. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  3894. UnicodeString Name = LoadStr(NEW_FOLDER);
  3895. int AllowedChanges =
  3896. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  3897. bool SaveSettings = false;
  3898. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  3899. {
  3900. TWindowLock Lock(this);
  3901. if (!IsSideLocalBrowser(Side))
  3902. {
  3903. if (SaveSettings)
  3904. {
  3905. GUIConfiguration->NewDirectoryProperties = Properties;
  3906. }
  3907. CreateRemoteDirectory(Name, Properties);
  3908. }
  3909. else
  3910. {
  3911. DirView(Side)->CreateDirectory(Name);
  3912. }
  3913. }
  3914. }
  3915. //---------------------------------------------------------------------------
  3916. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  3917. {
  3918. TWindowLock Lock(this);
  3919. DirView(Side)->ExecuteHomeDirectory();
  3920. }
  3921. //---------------------------------------------------------------------------
  3922. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  3923. {
  3924. DoOpenDirectoryDialog(odBrowse, Side);
  3925. }
  3926. //---------------------------------------------------------------------------
  3927. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  3928. {
  3929. UnicodeString Path = Bookmark->GetSideDirectory(Side);
  3930. bool Result = !Path.IsEmpty();
  3931. if (Result)
  3932. {
  3933. // While we might get here when the session is closed (from location profiles),
  3934. // it's not a problem as the Path setter is noop then
  3935. DirView(Side)->Path = Path;
  3936. }
  3937. return Result;
  3938. }
  3939. //---------------------------------------------------------------------------
  3940. void __fastcall TCustomScpExplorerForm::CalculateSize(
  3941. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  3942. bool & Close)
  3943. {
  3944. // terminal can be already closed (e.g. dropped connection)
  3945. if (Terminal != NULL)
  3946. {
  3947. try
  3948. {
  3949. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  3950. }
  3951. catch(...)
  3952. {
  3953. if (!Terminal->Active)
  3954. {
  3955. Close = true;
  3956. }
  3957. throw;
  3958. }
  3959. }
  3960. }
  3961. //---------------------------------------------------------------------------
  3962. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  3963. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  3964. bool & Close)
  3965. {
  3966. // terminal can be already closed (e.g. dropped connection)
  3967. if (Terminal != NULL)
  3968. {
  3969. Configuration->Usage->Inc(L"ChecksumCalculated");
  3970. try
  3971. {
  3972. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  3973. }
  3974. catch(...)
  3975. {
  3976. if (!Terminal->Active)
  3977. {
  3978. Close = true;
  3979. }
  3980. throw;
  3981. }
  3982. }
  3983. }
  3984. //---------------------------------------------------------------------------
  3985. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  3986. {
  3987. bool Result;
  3988. if (!IsSideLocalBrowser(Side))
  3989. {
  3990. TRemoteTokenList * GroupList = NULL;
  3991. TRemoteTokenList * UserList = NULL;
  3992. try
  3993. {
  3994. TRemoteProperties CurrentProperties;
  3995. if (Terminal->LoadFilesProperties(FileList))
  3996. {
  3997. RemoteDirView->Invalidate();
  3998. }
  3999. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4000. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4001. if (CapableGroupChanging || CapableOwnerChanging)
  4002. {
  4003. if (CapableGroupChanging)
  4004. {
  4005. GroupList = Terminal->Groups->Duplicate();
  4006. }
  4007. if (CapableOwnerChanging)
  4008. {
  4009. UserList = Terminal->Users->Duplicate();
  4010. }
  4011. TRemoteDirectory * Files = Terminal->Files;
  4012. int Count = Files->Count;
  4013. if (Count > 100)
  4014. {
  4015. Count = 100;
  4016. }
  4017. for (int Index = 0; Index < Count; Index++)
  4018. {
  4019. TRemoteFile * File = Files->Files[Index];
  4020. if (CapableGroupChanging)
  4021. {
  4022. GroupList->AddUnique(File->Group);
  4023. }
  4024. if (CapableOwnerChanging)
  4025. {
  4026. UserList->AddUnique(File->Owner);
  4027. }
  4028. }
  4029. // if we haven't collected tokens for all files in current directory,
  4030. // make sure we collect them at least for all selected files.
  4031. // (note that so far the files in FileList has to be from current direcotry)
  4032. if (Count < Files->Count)
  4033. {
  4034. for (int Index = 0; Index < FileList->Count; Index++)
  4035. {
  4036. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4037. if (CapableGroupChanging)
  4038. {
  4039. GroupList->AddUnique(File->Group);
  4040. }
  4041. if (CapableOwnerChanging)
  4042. {
  4043. UserList->AddUnique(File->Owner);
  4044. }
  4045. }
  4046. }
  4047. }
  4048. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4049. int Flags = 0;
  4050. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4051. if (CapableOwnerChanging) Flags |= cpOwner;
  4052. if (CapableGroupChanging) Flags |= cpGroup;
  4053. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4054. if (Terminal->IsCapable[fcCalculatingChecksum])
  4055. {
  4056. CalculateChecksumEvent = CalculateChecksum;
  4057. }
  4058. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4059. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4060. TRemoteProperties NewProperties = CurrentProperties;
  4061. Result =
  4062. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4063. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4064. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4065. CalculateSize, CalculateChecksumEvent);
  4066. if (Result)
  4067. {
  4068. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4069. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4070. }
  4071. }
  4072. __finally
  4073. {
  4074. delete GroupList;
  4075. delete UserList;
  4076. }
  4077. }
  4078. else
  4079. {
  4080. DirView(Side)->DisplayPropertiesMenu();
  4081. Result = true;
  4082. }
  4083. return Result;
  4084. }
  4085. //---------------------------------------------------------------------------
  4086. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4087. {
  4088. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4089. {
  4090. FIgnoreNextDialogChar = Key;
  4091. }
  4092. Key = 0;
  4093. }
  4094. //---------------------------------------------------------------------------
  4095. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4096. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4097. {
  4098. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4099. if (Command != NULL)
  4100. {
  4101. KeyProcessed(Key, Shift);
  4102. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4103. {
  4104. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4105. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4106. false, false, const_cast<TCustomCommandType *>(Command));
  4107. }
  4108. }
  4109. }
  4110. //---------------------------------------------------------------------------
  4111. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4112. {
  4113. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4114. {
  4115. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4116. }
  4117. if (!DirView(osCurrent)->IsEditing())
  4118. {
  4119. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4120. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4121. {
  4122. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4123. if (((Action->ShortCut == KeyShortCut) ||
  4124. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4125. AllowedAction(Action, aaShortCut))
  4126. {
  4127. // Has to be called before the action as the dialog char is already in queue.
  4128. // So when the action consumes message queue, we already need to have the
  4129. // FIgnoreNextDialogChar set
  4130. KeyProcessed(Key, Shift);
  4131. // Reset reference to previous component (when menu/toolbar was clicked).
  4132. // Needed to detect that action was invoked by keyboard shortcut
  4133. // in TNonVisualDataModule::ExplorerActionsExecute
  4134. Action->ActionComponent = NULL;
  4135. Action->Execute();
  4136. return;
  4137. }
  4138. }
  4139. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4140. {
  4141. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4142. {
  4143. KeyProcessed(Key, Shift);
  4144. TTerminalManager::Instance()->ActiveTerminalIndex = i;
  4145. return;
  4146. }
  4147. }
  4148. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4149. {
  4150. KeyProcessed(Key, Shift);
  4151. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4152. }
  4153. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4154. if (IsCustomShortCut(CustomShortCut))
  4155. {
  4156. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4157. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4158. if (WinConfiguration->SharedBookmarks != NULL)
  4159. {
  4160. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4161. if ((Bookmark != NULL) &&
  4162. OpenBookmark(FCurrentSide, Bookmark))
  4163. {
  4164. KeyProcessed(Key, Shift);
  4165. }
  4166. }
  4167. }
  4168. }
  4169. TForm::KeyDown(Key, Shift);
  4170. }
  4171. //---------------------------------------------------------------------------
  4172. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4173. {
  4174. DebugAssert(Terminal != NULL);
  4175. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4176. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  4177. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4178. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4179. bool SecurityEnabled = !SessionInfo.SecurityProtocolName.IsEmpty();
  4180. SessionStatusBar->Panels->Items[Offset + 0]->Enabled = SecurityEnabled;
  4181. // expanded from ?: to avoid memory leaks
  4182. if (SecurityEnabled)
  4183. {
  4184. SessionStatusBar->Panels->Items[Offset + 0]->Hint =
  4185. FMTLOAD(STATUS_SECURE, (SessionInfo.SecurityProtocolName));
  4186. }
  4187. else
  4188. {
  4189. SessionStatusBar->Panels->Items[Offset + 0]->Hint = LoadStr(STATUS_INSECURE);
  4190. }
  4191. if (FileSystemInfo.ProtocolName.IsEmpty())
  4192. {
  4193. SessionStatusBar->Panels->Items[Offset + 1]->Caption = SessionInfo.ProtocolName;
  4194. }
  4195. else
  4196. {
  4197. SessionStatusBar->Panels->Items[Offset + 1]->Caption = FileSystemInfo.ProtocolName;
  4198. }
  4199. SessionStatusBar->Panels->Items[Offset + 1]->Hint = LoadStr(STATUS_PROTOCOL_HINT);
  4200. SessionStatusBar->Panels->Items[Offset + 2]->Enabled =
  4201. (!SessionInfo.CSCompression.IsEmpty() || !SessionInfo.SCCompression.IsEmpty());
  4202. UnicodeString CSCompressionStr = DefaultStr(SessionInfo.CSCompression, LoadStr(NO_STR));
  4203. UnicodeString SCCompressionStr = DefaultStr(SessionInfo.SCCompression, LoadStr(NO_STR));
  4204. UnicodeString CompressionStr = CSCompressionStr;
  4205. if (CSCompressionStr != SCCompressionStr)
  4206. {
  4207. CompressionStr += UnicodeString(L"/") + SCCompressionStr;
  4208. }
  4209. SessionStatusBar->Panels->Items[Offset + 2]->Hint = FMTLOAD(STATUS_COMPRESSION_HINT, (CompressionStr));
  4210. SessionStatusBar->Panels->Items[Offset + 3]->Hint = LoadStr(STATUS_DURATION_HINT);
  4211. UpdateStatusBar();
  4212. }
  4213. //---------------------------------------------------------------------------
  4214. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4215. {
  4216. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4217. DebugAssert(SessionStatusBar != NULL);
  4218. if (!Terminal || !Terminal->Active || Terminal->Status < ssOpened)
  4219. {
  4220. // note: (Terminal->Status < sshReady) currently never happens here,
  4221. // so STATUS_CONNECTING is never used
  4222. SessionStatusBar->SimplePanel = true;
  4223. SessionStatusBar->SimpleText = LoadStr(
  4224. !Terminal || !Terminal->Active ? STATUS_NOT_CONNECTED : STATUS_CONNECTING);
  4225. }
  4226. else
  4227. {
  4228. DebugAssert(Terminal);
  4229. SessionStatusBar->SimplePanel = false;
  4230. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4231. if (!FNote.IsEmpty())
  4232. {
  4233. SessionStatusBar->Panels->Items[0]->Caption = FNote;
  4234. }
  4235. else
  4236. {
  4237. UpdateStatusPanelText(SessionStatusBar->Panels->Items[0]);
  4238. }
  4239. SessionStatusBar->Panels->Items[0]->Hint = FNoteHints;
  4240. SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1]->Caption =
  4241. FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4242. }
  4243. }
  4244. //---------------------------------------------------------------------------
  4245. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4246. {
  4247. Panel->Caption = L"";
  4248. }
  4249. //---------------------------------------------------------------------------
  4250. void __fastcall TCustomScpExplorerForm::Idle()
  4251. {
  4252. if (FShowing || FStandaloneEditing)
  4253. {
  4254. FEditorManager->Check();
  4255. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4256. // that needs to know if queue view is visible (and it may be closed after queue update)
  4257. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4258. }
  4259. if (FShowing)
  4260. {
  4261. if (!NonVisualDataModule->Busy &&
  4262. // Menu is opened or drag&drop is going on
  4263. (Mouse->Capture == NULL))
  4264. {
  4265. if (FRefreshRemoteDirectory)
  4266. {
  4267. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4268. {
  4269. Terminal->RefreshDirectory();
  4270. }
  4271. FRefreshRemoteDirectory = false;
  4272. }
  4273. if (FRefreshLocalDirectory)
  4274. {
  4275. ReloadLocalDirectory();
  4276. FRefreshLocalDirectory = false;
  4277. }
  4278. if (WinConfiguration->RefreshRemotePanel)
  4279. {
  4280. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4281. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4282. {
  4283. RemoteDirView->ReloadDirectory();
  4284. }
  4285. }
  4286. }
  4287. }
  4288. if (FShowing || FStandaloneEditing)
  4289. {
  4290. if (FQueueStatusInvalidated)
  4291. {
  4292. UpdateQueueStatus(false);
  4293. }
  4294. RefreshQueueItems();
  4295. }
  4296. if (FShowing)
  4297. {
  4298. UpdateStatusBar();
  4299. }
  4300. FIgnoreNextDialogChar = 0;
  4301. }
  4302. //---------------------------------------------------------------------------
  4303. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4304. {
  4305. try
  4306. {
  4307. FUserActionTimer->Enabled = false;
  4308. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4309. {
  4310. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4311. {
  4312. FPendingQueueActionItem->ProcessUserAction();
  4313. }
  4314. }
  4315. }
  4316. __finally
  4317. {
  4318. FPendingQueueActionItem = NULL;
  4319. }
  4320. }
  4321. //---------------------------------------------------------------------------
  4322. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4323. {
  4324. if (WinConfiguration->MinimizeToTray)
  4325. {
  4326. UpdateTrayIcon();
  4327. FTrayIcon->Visible = true;
  4328. if (Visible)
  4329. {
  4330. ShowWindow(Handle, SW_HIDE);
  4331. }
  4332. }
  4333. }
  4334. //---------------------------------------------------------------------------
  4335. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4336. {
  4337. // WORKAROUND
  4338. // When restoring maximized window from minimization,
  4339. // rarely some controls do not align properly.
  4340. // Two instances seen (both for Commander):
  4341. // - When restoring, window is temporarily narrower (not maximized),
  4342. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4343. // Once maximized already, top dock shinks back, but the session PageControl,
  4344. // do not align up, leaving space between TopDock and PageControl.
  4345. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4346. for (int Index = 0; Index < ControlCount; Index++)
  4347. {
  4348. RealignControl(Controls[Index]);
  4349. }
  4350. if (FTrayIcon->Visible)
  4351. {
  4352. FTrayIcon->Visible = false;
  4353. if (Visible)
  4354. {
  4355. ShowWindow(Handle, SW_SHOW);
  4356. }
  4357. }
  4358. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4359. {
  4360. FNeedSession = false;
  4361. NonVisualDataModule->StartBusy();
  4362. try
  4363. {
  4364. NeedSession(true);
  4365. }
  4366. __finally
  4367. {
  4368. NonVisualDataModule->EndBusy();
  4369. }
  4370. }
  4371. }
  4372. //---------------------------------------------------------------------------
  4373. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4374. {
  4375. FTrayIcon->Hint = GetMainForm()->Caption;
  4376. }
  4377. //---------------------------------------------------------------------------
  4378. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4379. {
  4380. UpdateTrayIcon();
  4381. }
  4382. //---------------------------------------------------------------------------
  4383. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4384. {
  4385. // workaround
  4386. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4387. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4388. // But WM_ACTIVATEAPP message can be received even when
  4389. // the main window is minimized to the tray and internal editor window is focused
  4390. // (after another application was previously active)
  4391. if (::IsIconic(Handle))
  4392. {
  4393. if (!IsAppIconic())
  4394. {
  4395. SetAppIconic(true);
  4396. }
  4397. }
  4398. ::ApplicationRestore();
  4399. Application->BringToFront();
  4400. }
  4401. //---------------------------------------------------------------------------
  4402. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4403. {
  4404. RestoreApp();
  4405. }
  4406. //---------------------------------------------------------------------------
  4407. void __fastcall TCustomScpExplorerForm::NewSession(bool FromSite, const UnicodeString & SessionUrl)
  4408. {
  4409. if (OpenInNewWindow())
  4410. {
  4411. // todo: Pass FromSite
  4412. ExecuteNewInstance(SessionUrl);
  4413. }
  4414. else
  4415. {
  4416. TTerminalManager::Instance()->NewSession(FromSite, SessionUrl);
  4417. }
  4418. }
  4419. //---------------------------------------------------------------------------
  4420. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4421. {
  4422. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->SessionName;
  4423. StoredSessions->NewSession(SessionName, SessionData);
  4424. // modified only, explicit
  4425. StoredSessions->Save(false, true);
  4426. // encode session name because of slashes in hierarchical sessions
  4427. return EncodeUrlString(SessionName);
  4428. }
  4429. //---------------------------------------------------------------------------
  4430. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4431. {
  4432. // current working directories become defaults here, what is not right
  4433. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4434. return SaveHiddenDuplicateSession(SessionData.get());
  4435. }
  4436. //---------------------------------------------------------------------------
  4437. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4438. {
  4439. if (OpenInNewWindow())
  4440. {
  4441. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4442. }
  4443. else
  4444. {
  4445. // current working directories become defaults here, what is not right
  4446. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4447. TTerminalManager * Manager = TTerminalManager::Instance();
  4448. TManagedTerminal * Terminal = Manager->NewManagedTerminal(SessionData.get());
  4449. Manager->ActiveTerminal = Terminal;
  4450. }
  4451. }
  4452. //---------------------------------------------------------------------------
  4453. void __fastcall TCustomScpExplorerForm::RenameSession()
  4454. {
  4455. UnicodeString Name = Terminal->SessionData->SessionName;
  4456. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME))
  4457. {
  4458. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4459. TSessionData::ValidateName(Name);
  4460. // This is inconsistent with how color (for example) is handled.
  4461. Terminal->SessionData->Name = Name;
  4462. UpdateControls();
  4463. // Add/Remove distinguishing paths from sessions of the same name.
  4464. DoTerminalListChanged();
  4465. }
  4466. }
  4467. //---------------------------------------------------------------------------
  4468. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4469. {
  4470. DebugAssert(Queue != NULL);
  4471. bool Result = Queue->IsEmpty;
  4472. if (!Result)
  4473. {
  4474. SetFocus();
  4475. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4476. }
  4477. return Result;
  4478. }
  4479. //---------------------------------------------------------------------------
  4480. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4481. {
  4482. return CanCloseQueue(FQueue);
  4483. }
  4484. //---------------------------------------------------------------------------
  4485. void __fastcall TCustomScpExplorerForm::CloseSession()
  4486. {
  4487. if (CanCloseQueue())
  4488. {
  4489. TTerminalManager::Instance()->FreeActiveTerminal();
  4490. }
  4491. }
  4492. //---------------------------------------------------------------------------
  4493. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4494. {
  4495. if (CanCloseQueue())
  4496. {
  4497. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4498. }
  4499. }
  4500. //---------------------------------------------------------------------------
  4501. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4502. {
  4503. RemoteDirView->Terminal = Terminal;
  4504. UpdateRemotePathComboBox(false);
  4505. UpdateControls();
  4506. }
  4507. //---------------------------------------------------------------------------
  4508. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4509. {
  4510. if (DebugAlwaysTrue(!Terminal->Active))
  4511. {
  4512. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4513. }
  4514. }
  4515. //---------------------------------------------------------------------------
  4516. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4517. {
  4518. if (OpenInNewWindow())
  4519. {
  4520. // encode session name because of slashes in hierarchical sessions
  4521. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4522. }
  4523. else
  4524. {
  4525. TTerminalManager * Manager = TTerminalManager::Instance();
  4526. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4527. Manager->ActiveTerminal = Terminal;
  4528. }
  4529. }
  4530. //---------------------------------------------------------------------------
  4531. void __fastcall TCustomScpExplorerForm::DoOpenFolderOrWorkspace(const UnicodeString & Name, bool ConnectFirstTerminal)
  4532. {
  4533. TTerminalManager * Manager = TTerminalManager::Instance();
  4534. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4535. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4536. TManagedTerminal * FirstTerminal = Manager->NewTerminals(DataList.get());
  4537. if (!ConnectFirstTerminal)
  4538. {
  4539. FirstTerminal->Disconnected = true;
  4540. FirstTerminal->DisconnectedTemporarily = true;
  4541. }
  4542. Manager->ActiveTerminal = FirstTerminal;
  4543. }
  4544. //---------------------------------------------------------------------------
  4545. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4546. {
  4547. if (OpenInNewWindow())
  4548. {
  4549. ExecuteNewInstance(Name);
  4550. }
  4551. else
  4552. {
  4553. DoOpenFolderOrWorkspace(Name, true);
  4554. }
  4555. }
  4556. //---------------------------------------------------------------------------
  4557. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4558. bool &CanClose)
  4559. {
  4560. if (Terminal != NULL)
  4561. {
  4562. if (Terminal->Active && WinConfiguration->ConfirmClosingSession)
  4563. {
  4564. unsigned int Result;
  4565. TMessageParams Params(mpNeverAskAgainCheck);
  4566. UnicodeString Message;
  4567. int Answers = qaOK | qaCancel;
  4568. if (TTerminalManager::Instance()->Count > 1)
  4569. {
  4570. if (!WinConfiguration->AutoSaveWorkspace)
  4571. {
  4572. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  4573. }
  4574. else
  4575. {
  4576. Message = LoadStr(CLOSE_SESSIONS);
  4577. }
  4578. }
  4579. else
  4580. {
  4581. if (!WinConfiguration->AutoSaveWorkspace)
  4582. {
  4583. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  4584. }
  4585. else
  4586. {
  4587. Message = LoadStr(CLOSE_SESSION);
  4588. }
  4589. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  4590. }
  4591. UnicodeString Note;
  4592. if (WinConfiguration->AutoSaveWorkspace)
  4593. {
  4594. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  4595. }
  4596. else
  4597. {
  4598. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  4599. Answers = qaYes | qaNo | qaCancel;
  4600. }
  4601. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  4602. SetFocus();
  4603. Result = MessageDialog(Message, qtConfirmation,
  4604. Answers, HELP_NONE, &Params);
  4605. if (Result == qaNeverAskAgain)
  4606. {
  4607. WinConfiguration->ConfirmClosingSession = false;
  4608. }
  4609. if (Result == qaNo)
  4610. {
  4611. CanClose = SaveWorkspace(true);
  4612. // note that the workspace will be saved redundatly again from FormClose
  4613. }
  4614. else
  4615. {
  4616. CanClose =
  4617. (Result == qaOK) ||
  4618. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4619. (Result == qaNeverAskAgain);
  4620. }
  4621. }
  4622. if (CanClose)
  4623. {
  4624. CanClose = CanCloseQueue();
  4625. }
  4626. }
  4627. if (CanClose)
  4628. {
  4629. CanClose =
  4630. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4631. FEditorManager->CloseExternalFilesWithoutProcess();
  4632. if (CanClose)
  4633. {
  4634. while (CanClose && (FLocalEditors->Count > 0))
  4635. {
  4636. int PrevCount = FLocalEditors->Count;
  4637. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4638. CanClose = (FLocalEditors->Count < PrevCount);
  4639. }
  4640. if (CanClose)
  4641. {
  4642. CanClose = FEditorManager->Empty(true);
  4643. if (!CanClose)
  4644. {
  4645. SetFocus();
  4646. CanClose =
  4647. (MessageDialog(
  4648. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4649. }
  4650. }
  4651. }
  4652. }
  4653. }
  4654. //---------------------------------------------------------------------------
  4655. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4656. {
  4657. TForm * Form = dynamic_cast<TForm *>(Sender);
  4658. DebugAssert(Form != NULL);
  4659. Form->Close();
  4660. }
  4661. //---------------------------------------------------------------------------
  4662. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4663. {
  4664. TForm * Form = dynamic_cast<TForm *>(Sender);
  4665. delete Form;
  4666. }
  4667. //---------------------------------------------------------------------------
  4668. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4669. {
  4670. while (FLocalEditors->Count > 0)
  4671. {
  4672. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4673. }
  4674. }
  4675. //---------------------------------------------------------------------------
  4676. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4677. TObject *Sender)
  4678. {
  4679. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4680. try
  4681. {
  4682. SetProperties(osRemote, FileList);
  4683. }
  4684. __finally
  4685. {
  4686. delete FileList;
  4687. }
  4688. }
  4689. //---------------------------------------------------------------------------
  4690. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4691. {
  4692. if (value)
  4693. {
  4694. if (Component == fcCustomCommandsBand)
  4695. {
  4696. UpdateCustomCommandsToolbar();
  4697. }
  4698. }
  4699. }
  4700. //---------------------------------------------------------------------------
  4701. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4702. {
  4703. TControl * Control = GetComponent(Component);
  4704. DebugAssert(Control);
  4705. bool Changed = (Control->Visible != value);
  4706. if (Changed)
  4707. {
  4708. ComponentShowing(Component, value);
  4709. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4710. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4711. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4712. if (value)
  4713. {
  4714. int RemainingHeight = Control->Parent->ClientHeight;
  4715. int RemainingWidth = Control->Parent->ClientWidth;
  4716. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4717. {
  4718. TControl * ChildControl = Control->Parent->Controls[i];
  4719. if (ChildControl->Visible)
  4720. {
  4721. switch (ChildControl->Align)
  4722. {
  4723. case alTop:
  4724. case alBottom:
  4725. RemainingHeight -= ChildControl->Height;
  4726. break;
  4727. case alLeft:
  4728. case alRight:
  4729. RemainingWidth -= ChildControl->Width;
  4730. break;
  4731. }
  4732. }
  4733. }
  4734. int Reserve = ScaleByTextHeight(this, 32);
  4735. // queue in explorer, trees in commander
  4736. if (Control->Height > RemainingHeight - Reserve)
  4737. {
  4738. Control->Height = RemainingHeight / 2;
  4739. }
  4740. if (Control->Width > RemainingWidth - Reserve)
  4741. {
  4742. Control->Width = RemainingWidth / 2;
  4743. }
  4744. }
  4745. Control->Visible = value;
  4746. if (WasFocused && Visible)
  4747. {
  4748. DirView(osCurrent)->SetFocus();
  4749. }
  4750. FixControlsPlacement();
  4751. }
  4752. }
  4753. //---------------------------------------------------------------------------
  4754. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4755. {
  4756. TControl * Control = GetComponent(Component);
  4757. if (Control == NULL)
  4758. {
  4759. return false;
  4760. }
  4761. else
  4762. {
  4763. return Control->Visible;
  4764. }
  4765. }
  4766. //---------------------------------------------------------------------------
  4767. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4768. {
  4769. bool Result;
  4770. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4771. {
  4772. Result = !IsUWP();
  4773. }
  4774. else
  4775. {
  4776. Result = true;
  4777. }
  4778. return Result;
  4779. }
  4780. //---------------------------------------------------------------------------
  4781. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4782. {
  4783. if (DirView(osOther)->ItemFocused != NULL)
  4784. {
  4785. DirView(osOther)->ItemFocused->MakeVisible(false);
  4786. }
  4787. QueueSplitter->Visible = QueuePanel->Visible;
  4788. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  4789. }
  4790. //---------------------------------------------------------------------------
  4791. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4792. {
  4793. switch (Component) {
  4794. case fcStatusBar: return RemoteStatusBar;
  4795. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4796. case fcQueueView: return QueuePanel;
  4797. case fcQueueToolbar: return QueueDock;
  4798. case fcRemoteTree: return RemoteDrivePanel;
  4799. case fcSessionsTabs: return SessionsPageControl;
  4800. default: return NULL;
  4801. }
  4802. }
  4803. //---------------------------------------------------------------------------
  4804. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4805. TObject *Sender, TListColumn *Column, TPoint &Point)
  4806. {
  4807. DebugAssert(NonVisualDataModule && Column && Sender);
  4808. NonVisualDataModule->ListColumn = Column;
  4809. TPopupMenu * DirViewColumnMenu;
  4810. if (dynamic_cast<TUnixDirView *>(Sender) != NULL)
  4811. {
  4812. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4813. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4814. (Column->Index == uvName);
  4815. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4816. (Column->Index == uvSize);
  4817. }
  4818. else
  4819. {
  4820. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4821. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4822. (Column->Index == dvName);
  4823. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4824. (Column->Index == dvSize);
  4825. }
  4826. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  4827. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  4828. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  4829. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4830. }
  4831. //---------------------------------------------------------------------------
  4832. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4833. TObject *Sender, TListItem *Item, bool &AllowExec)
  4834. {
  4835. DoDirViewExecFile(Sender, Item, AllowExec);
  4836. }
  4837. //---------------------------------------------------------------------------
  4838. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4839. TListItem * Item, bool & AllowExec)
  4840. {
  4841. DebugAssert(Sender && Item && Configuration);
  4842. DebugAssert(AllowExec);
  4843. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  4844. bool Remote = (ADirView == DirView(osRemote)) && !IsSideLocalBrowser(osRemote);
  4845. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks;
  4846. TOperationSide Side = (Remote ? osRemote : osLocal); // TODO
  4847. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  4848. // on files only (not directories)
  4849. // and only when symlinks are resolved (apply to remote panel only)
  4850. if (!ADirView->ItemIsDirectory(Item) &&
  4851. (ResolvedSymlinks || FForceExecution || Terminal->IsEncryptingFiles()))
  4852. {
  4853. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  4854. !FForceExecution &&
  4855. (ResolvedSymlinks || Terminal->IsEncryptingFiles()))
  4856. {
  4857. if (WinConfiguration->DoubleClickAction == dcaCopy)
  4858. {
  4859. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  4860. UnlockWindow();
  4861. try
  4862. {
  4863. ExecuteFileOperation(
  4864. foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  4865. }
  4866. __finally
  4867. {
  4868. LockWindow();
  4869. }
  4870. AllowExec = false;
  4871. }
  4872. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  4873. {
  4874. if (!Remote || !WinConfiguration->DisableOpenEdit)
  4875. {
  4876. ExecuteFile(Side, efDefaultEditor);
  4877. AllowExec = false;
  4878. }
  4879. }
  4880. else
  4881. {
  4882. DebugFail();
  4883. }
  4884. }
  4885. // if we have not done anything special, fall back to default behavior
  4886. if (AllowExec)
  4887. {
  4888. if (Remote && !WinConfiguration->DisableOpenEdit)
  4889. {
  4890. ExecuteFile(osRemote, efShell);
  4891. AllowExec = false;
  4892. }
  4893. }
  4894. }
  4895. }
  4896. //---------------------------------------------------------------------------
  4897. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  4898. {
  4899. return ((Side == osRemote) || (Side == osCurrent));
  4900. }
  4901. //---------------------------------------------------------------------------
  4902. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  4903. {
  4904. DebugFail();
  4905. }
  4906. //---------------------------------------------------------------------------
  4907. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  4908. {
  4909. DebugFail();
  4910. }
  4911. //---------------------------------------------------------------------------
  4912. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  4913. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  4914. {
  4915. TSynchronizeParamType Params;
  4916. Params.LocalDirectory = LocalDirectory;
  4917. Params.RemoteDirectory = RemoteDirectory;
  4918. int UnusedParams =
  4919. (GUIConfiguration->SynchronizeParams &
  4920. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize));
  4921. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  4922. Params.Options = GUIConfiguration->SynchronizeOptions;
  4923. bool SaveSettings = false;
  4924. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  4925. &DoSynchronizeTooManyDirectories);
  4926. DebugAssert(FSynchronizeController == NULL);
  4927. FSynchronizeController = &Controller;
  4928. bool Result;
  4929. try
  4930. {
  4931. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4932. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  4933. int Options =
  4934. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  4935. DebugAssert(FOnFeedSynchronizeError == NULL);
  4936. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  4937. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  4938. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  4939. if (Result)
  4940. {
  4941. if (SaveSettings)
  4942. {
  4943. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  4944. GUIConfiguration->SynchronizeOptions = Params.Options;
  4945. }
  4946. else
  4947. {
  4948. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  4949. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  4950. FLAGSET(Params.Options, soSynchronize))
  4951. {
  4952. GUIConfiguration->SynchronizeOptions =
  4953. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  4954. soSynchronize;
  4955. }
  4956. }
  4957. LocalDirectory = Params.LocalDirectory;
  4958. RemoteDirectory = Params.RemoteDirectory;
  4959. }
  4960. }
  4961. __finally
  4962. {
  4963. FSynchronizeController = NULL;
  4964. DebugAssert(FOnFeedSynchronizeError == NULL);
  4965. FOnFeedSynchronizeError = NULL;
  4966. }
  4967. return Result;
  4968. }
  4969. //---------------------------------------------------------------------------
  4970. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  4971. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  4972. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  4973. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  4974. TSynchronizeOptions * Options, bool Full)
  4975. {
  4976. // Keep-up-to-date mode
  4977. if (Terminal->Status == ssOpened)
  4978. {
  4979. try
  4980. {
  4981. int PParams = Params.Params;
  4982. if (!Full)
  4983. {
  4984. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  4985. if (FLAGSET(Params.Options, soRecurse))
  4986. {
  4987. PParams |= TTerminal::spSubDirs;
  4988. }
  4989. }
  4990. else
  4991. {
  4992. // if keepuptodate is non-recursive,
  4993. // full sync before has to be non-recursive as well
  4994. if (FLAGCLEAR(Params.Options, soRecurse))
  4995. {
  4996. PParams |= TTerminal::spNoRecurse;
  4997. }
  4998. }
  4999. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5000. PParams, Checklist, Options);
  5001. }
  5002. catch (Exception & E)
  5003. {
  5004. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  5005. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5006. {
  5007. // noop, already logged in MoreMessageDialog
  5008. }
  5009. else
  5010. {
  5011. ShowExtendedExceptionEx(Terminal, &E);
  5012. throw;
  5013. }
  5014. }
  5015. }
  5016. }
  5017. //---------------------------------------------------------------------------
  5018. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5019. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5020. const UnicodeString ErrorStr)
  5021. {
  5022. if (!Directory.IsEmpty())
  5023. {
  5024. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5025. }
  5026. else
  5027. {
  5028. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5029. }
  5030. }
  5031. //---------------------------------------------------------------------------
  5032. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5033. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5034. {
  5035. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5036. {
  5037. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5038. }
  5039. else
  5040. {
  5041. TMessageParams Params(mpNeverAskAgainCheck);
  5042. unsigned int Result = MessageDialog(
  5043. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5044. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5045. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5046. {
  5047. MaxDirectories *= 2;
  5048. if (Result == qaNeverAskAgain)
  5049. {
  5050. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5051. }
  5052. }
  5053. else
  5054. {
  5055. Abort();
  5056. }
  5057. }
  5058. }
  5059. //---------------------------------------------------------------------------
  5060. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5061. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5062. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5063. TSynchronizeOptions * Options)
  5064. {
  5065. // Keep-up-to-date mode
  5066. DebugAssert(!FAutoOperation);
  5067. void * BatchStorage;
  5068. BatchStart(BatchStorage);
  5069. FAutoOperation = true;
  5070. bool AnyOperation = false;
  5071. TDateTime StartTime = Now();
  5072. TSynchronizeChecklist * AChecklist = NULL;
  5073. try
  5074. {
  5075. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5076. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5077. {
  5078. FSynchronizeProgressForm->Start();
  5079. }
  5080. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5081. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5082. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5083. Options);
  5084. SAFE_DESTROY(FSynchronizeProgressForm);
  5085. AnyOperation = (AChecklist->CheckedCount > 0);
  5086. if (AnyOperation)
  5087. {
  5088. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5089. CreateProgressForm(&SynchronizeProgress);
  5090. try
  5091. {
  5092. Terminal->SynchronizeApply(
  5093. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5094. SynchronizeProcessedItem, NULL, NULL, NULL);
  5095. }
  5096. __finally
  5097. {
  5098. DestroyProgressForm();
  5099. }
  5100. }
  5101. }
  5102. __finally
  5103. {
  5104. if (Checklist == NULL)
  5105. {
  5106. delete AChecklist;
  5107. }
  5108. else
  5109. {
  5110. *Checklist = AChecklist;
  5111. }
  5112. FAutoOperation = false;
  5113. SAFE_DESTROY(FSynchronizeProgressForm);
  5114. BatchEnd(BatchStorage);
  5115. ReloadLocalDirectory();
  5116. if (AnyOperation)
  5117. {
  5118. OperationComplete(StartTime);
  5119. }
  5120. }
  5121. }
  5122. //---------------------------------------------------------------------------
  5123. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5124. {
  5125. // can be called from command line
  5126. return Visible &&
  5127. ((DirView(osOther)->SelCount > 0) ||
  5128. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5129. }
  5130. //---------------------------------------------------------------------------
  5131. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5132. {
  5133. LogSynchronizeEvent(Terminal, Message);
  5134. }
  5135. //---------------------------------------------------------------------------
  5136. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5137. int Params, TSynchronizeOptions & Options)
  5138. {
  5139. DebugAssert(!IsLocalBrowserMode());
  5140. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5141. {
  5142. Options.Filter = new TStringList();
  5143. Options.Filter->CaseSensitive = false;
  5144. Options.Filter->Duplicates = Types::dupAccept;
  5145. if (DirView(osRemote)->SelCount > 0)
  5146. {
  5147. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5148. }
  5149. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5150. {
  5151. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5152. }
  5153. Options.Filter->Sort();
  5154. }
  5155. }
  5156. //---------------------------------------------------------------------------
  5157. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5158. {
  5159. TCopyParamType Defaults;
  5160. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5161. ConfigStorage->AccessMode = smRead;
  5162. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5163. {
  5164. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5165. }
  5166. ConfigStorage.reset(NULL);
  5167. std::unique_ptr<TStringList> Options(new TStringList());
  5168. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5169. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5170. UnicodeString Result;
  5171. if (Options->Count > 0)
  5172. {
  5173. Result =
  5174. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5175. }
  5176. return Result;
  5177. }
  5178. //---------------------------------------------------------------------------
  5179. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5180. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5181. {
  5182. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5183. UnicodeString AdditionalParams =
  5184. FORMAT(L"%s \"%s\" \"%s\" %d %d %s%s", (
  5185. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5186. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options,
  5187. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5188. SerializeCopyParamForCommandLine(CopyParams)));
  5189. ExecuteNewInstance(SessionName, AdditionalParams);
  5190. }
  5191. //---------------------------------------------------------------------------
  5192. struct TSynchronizeParams
  5193. {
  5194. UnicodeString LocalDirectory;
  5195. UnicodeString RemoteDirectory;
  5196. TSynchronizeMode Mode;
  5197. TSynchronizeChecklist * Checklist;
  5198. int Params;
  5199. TCopyParamType * CopyParam;
  5200. TDateTime CollectElapsed;
  5201. TDateTime * StartTime;
  5202. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5203. };
  5204. //---------------------------------------------------------------------------
  5205. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5206. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5207. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5208. {
  5209. DebugAssert(!FAutoOperation);
  5210. void * BatchStorage;
  5211. BatchStart(BatchStorage);
  5212. FAutoOperation = true;
  5213. TFileOperationStatistics Statistics;
  5214. TDateTime Start = Now();
  5215. try
  5216. {
  5217. Params.OnProcessedItem = OnProcessedItem;
  5218. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5219. CreateProgressForm(&SynchronizeProgress);
  5220. Terminal->SynchronizeApply(
  5221. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5222. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5223. &Statistics);
  5224. }
  5225. __finally
  5226. {
  5227. Params.OnProcessedItem = NULL;
  5228. FAutoOperation = false;
  5229. DestroyProgressForm();
  5230. BatchEnd(BatchStorage);
  5231. ReloadLocalDirectory();
  5232. }
  5233. if (WinConfiguration->SynchronizeSummary)
  5234. {
  5235. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5236. // The statistics should be 0 anyway in this case
  5237. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5238. {
  5239. Message += L"\n";
  5240. if (Statistics.FilesUploaded > 0)
  5241. {
  5242. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5243. }
  5244. if (Statistics.FilesDownloaded > 0)
  5245. {
  5246. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5247. }
  5248. if (Statistics.FilesDeletedLocal > 0)
  5249. {
  5250. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5251. }
  5252. if (Statistics.FilesDeletedRemote > 0)
  5253. {
  5254. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5255. }
  5256. }
  5257. TDateTime Elapsed = (Now() - Start);
  5258. Message +=
  5259. L"\n" +
  5260. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5261. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5262. TMessageParams Params(mpNeverAskAgainCheck);
  5263. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5264. if (Result == qaNeverAskAgain)
  5265. {
  5266. WinConfiguration->SynchronizeSummary = false;
  5267. }
  5268. }
  5269. }
  5270. //---------------------------------------------------------------------------
  5271. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5272. {
  5273. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5274. {
  5275. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5276. }
  5277. // Not set in keep-up-to-date mode - Synchronize() method
  5278. if (Token != NULL)
  5279. {
  5280. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5281. if (Params.OnProcessedItem != NULL)
  5282. {
  5283. Params.OnProcessedItem(NULL, ChecklistItem);
  5284. }
  5285. }
  5286. }
  5287. //---------------------------------------------------------------------------
  5288. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5289. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5290. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5291. {
  5292. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5293. *Params.StartTime = Now();
  5294. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5295. }
  5296. //---------------------------------------------------------------------------
  5297. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5298. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5299. {
  5300. // terminal can be already closed (e.g. dropped connection)
  5301. if (Terminal != NULL)
  5302. {
  5303. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5304. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5305. }
  5306. }
  5307. //---------------------------------------------------------------------------
  5308. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5309. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5310. {
  5311. DebugAssert(!IsLocalBrowserMode());
  5312. TAutoBatch AutoBatch(this);
  5313. TAutoFlag AutoOperationFlag(FAutoOperation);
  5314. if (Side == osRemote)
  5315. {
  5316. std::unique_ptr<TStrings> FileList(new TStringList());
  5317. FileList->AddObject(FileName, RemoteFile);
  5318. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5319. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5320. RemoteDirView->SaveSelection();
  5321. RemoteDirView->SaveSelectedNames();
  5322. try
  5323. {
  5324. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5325. }
  5326. catch(...)
  5327. {
  5328. RemoteDirView->DiscardSavedSelection();
  5329. throw;
  5330. }
  5331. RemoteDirView->RestoreSelection();
  5332. }
  5333. else if (DebugAlwaysTrue(Side == osLocal))
  5334. {
  5335. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5336. {
  5337. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5338. }
  5339. UnicodeString Directory = ExtractFileDir(FileName);
  5340. ReloadLocalDirectory(Directory);
  5341. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5342. if (!SamePaths(Directory, NewDirectory))
  5343. {
  5344. ReloadLocalDirectory(NewDirectory);
  5345. }
  5346. }
  5347. }
  5348. //---------------------------------------------------------------------------
  5349. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, const TSynchronizeChecklist::TItem * Item)
  5350. {
  5351. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5352. if (Side == osLocal)
  5353. {
  5354. OpenFolderInExplorer(LocalPath);
  5355. }
  5356. else if (DebugAlwaysTrue(Side == osRemote))
  5357. {
  5358. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5359. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5360. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5361. if (!LocalPath.IsEmpty())
  5362. {
  5363. SessionData->LocalDirectory = LocalPath;
  5364. }
  5365. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5366. ExecuteNewInstance(SessionName);
  5367. }
  5368. }
  5369. //---------------------------------------------------------------------------
  5370. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5371. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5372. const TCopyParamType * CopyParams)
  5373. {
  5374. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5375. UnicodeString AdditionalParams =
  5376. FORMAT(L"%s \"%s\" \"%s\" %d %d %s%s", (
  5377. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5378. LocalDirectory, RemoteDirectory, Mode, Params,
  5379. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5380. SerializeCopyParamForCommandLine(CopyParams)));
  5381. ExecuteNewInstance(SessionName, AdditionalParams);
  5382. }
  5383. //---------------------------------------------------------------------------
  5384. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5385. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5386. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5387. {
  5388. int Result;
  5389. bool SaveSettings = false;
  5390. int Options =
  5391. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5392. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5393. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5394. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5395. bool Continue =
  5396. (UseDefaults == 0) ||
  5397. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5398. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5399. if (Continue)
  5400. {
  5401. Configuration->Usage->Inc(L"Synchronizations");
  5402. UpdateCopyParamCounters(CopyParam);
  5403. TSynchronizeOptions SynchronizeOptions;
  5404. GetSynchronizeOptions(Params, SynchronizeOptions);
  5405. if (SaveSettings)
  5406. {
  5407. GUIConfiguration->SynchronizeParams = Params;
  5408. }
  5409. else
  5410. {
  5411. SaveMode = false;
  5412. }
  5413. TDateTime StartTime = Now();
  5414. TSynchronizeChecklist * Checklist = NULL;
  5415. try
  5416. {
  5417. DebugAssert(!FAutoOperation);
  5418. FAutoOperation = true;
  5419. try
  5420. {
  5421. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5422. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5423. int Files = -1;
  5424. if (DataList->Count >= 1)
  5425. {
  5426. Files = StrToIntDef(DataList->Strings[0], Files);
  5427. }
  5428. else
  5429. {
  5430. DataList->Add(UnicodeString());
  5431. }
  5432. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5433. FSynchronizeProgressForm->Start();
  5434. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5435. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5436. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5437. &SynchronizeOptions);
  5438. if (Terminal->SessionData->CacheDirectories)
  5439. {
  5440. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5441. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5442. }
  5443. }
  5444. __finally
  5445. {
  5446. FAutoOperation = false;
  5447. SAFE_DESTROY(FSynchronizeProgressForm);
  5448. }
  5449. TSynchronizeParams SynchronizeParams;
  5450. SynchronizeParams.LocalDirectory = LocalDirectory;
  5451. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5452. SynchronizeParams.Mode = Mode;
  5453. SynchronizeParams.CopyParam = &CopyParam;
  5454. SynchronizeParams.Params = Params;
  5455. SynchronizeParams.Checklist = Checklist;
  5456. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5457. SynchronizeParams.StartTime = &StartTime;
  5458. SynchronizeParams.OnProcessedItem = NULL;
  5459. Result = Checklist->Count;
  5460. if (Checklist->Count > 0)
  5461. {
  5462. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5463. {
  5464. if (!DoSynchronizeChecklistDialog(
  5465. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5466. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  5467. {
  5468. Result = -1;
  5469. }
  5470. }
  5471. else
  5472. {
  5473. FullSynchronize(SynchronizeParams, NULL, NULL);
  5474. }
  5475. }
  5476. else
  5477. {
  5478. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5479. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5480. }
  5481. }
  5482. __finally
  5483. {
  5484. delete Checklist;
  5485. }
  5486. OperationComplete(StartTime);
  5487. }
  5488. else
  5489. {
  5490. Result = -1;
  5491. }
  5492. return Result;
  5493. }
  5494. //---------------------------------------------------------------------------
  5495. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5496. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5497. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5498. {
  5499. if (Collect)
  5500. {
  5501. DebugAssert(FSynchronizeProgressForm != NULL);
  5502. if (!FSynchronizeProgressForm->Started)
  5503. {
  5504. FSynchronizeProgressForm->Start();
  5505. }
  5506. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5507. SetTaskbarListProgressValue(CompareProgress);
  5508. }
  5509. else
  5510. {
  5511. DebugAssert(FSynchronizeProgressForm == NULL);
  5512. DebugAssert(FProgressForm != NULL);
  5513. }
  5514. }
  5515. //---------------------------------------------------------------------------
  5516. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5517. {
  5518. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  5519. TRemoteFile * File = NULL;
  5520. Terminal->ReadFile(FullFileName, File);
  5521. if (File != NULL)
  5522. {
  5523. std::unique_ptr<TRemoteFile> FileOwner(File);
  5524. TAutoFlag Flag(FStandaloneEditing);
  5525. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5526. Application->ShowMainForm = false;
  5527. Application->Run();
  5528. }
  5529. }
  5530. //---------------------------------------------------------------------------
  5531. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  5532. {
  5533. DebugFail();
  5534. }
  5535. //---------------------------------------------------------------------------
  5536. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5537. {
  5538. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5539. SessionData->Assign(Terminal->SessionData);
  5540. UpdateSessionData(SessionData.get());
  5541. Terminal->UpdateSessionCredentials(SessionData.get());
  5542. if (!Terminal->SessionData->HasSessionName())
  5543. {
  5544. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5545. // once IsWorkspace is cleared
  5546. SessionData->Name = UnicodeString();
  5547. SessionData->IsWorkspace = false;
  5548. }
  5549. return SessionData.release();
  5550. }
  5551. //---------------------------------------------------------------------------
  5552. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5553. {
  5554. TSessionData * SessionData = CloneCurrentSessionData();
  5555. try
  5556. {
  5557. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5558. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5559. }
  5560. __finally
  5561. {
  5562. delete SessionData;
  5563. }
  5564. }
  5565. //---------------------------------------------------------------------------
  5566. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5567. {
  5568. TTerminalManager * Manager = TTerminalManager::Instance();
  5569. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5570. if (DebugAlwaysTrue(Terminal != NULL))
  5571. {
  5572. // Update (Managed)Terminal->StateData
  5573. UpdateTerminal(Terminal);
  5574. }
  5575. Manager->SaveWorkspace(DataList.get());
  5576. return DataList.release();
  5577. }
  5578. //---------------------------------------------------------------------------
  5579. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5580. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5581. {
  5582. WinConfiguration->LastWorkspace = Name;
  5583. if (!SavePasswords)
  5584. {
  5585. for (int Index = 0; Index < DataList->Count; Index++)
  5586. {
  5587. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5588. if (SessionData->Link.IsEmpty())
  5589. {
  5590. SessionData->ClearSessionPasswords();
  5591. }
  5592. }
  5593. }
  5594. StoredSessions->NewWorkspace(Name, DataList);
  5595. // modified only
  5596. StoredSessions->Save(false, Explicit);
  5597. }
  5598. //---------------------------------------------------------------------------
  5599. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5600. {
  5601. return
  5602. DefaultStr(WinConfiguration->LastWorkspace,
  5603. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5604. }
  5605. //---------------------------------------------------------------------------
  5606. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5607. {
  5608. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5609. bool AnyNonStoredSessionWithPassword = false;
  5610. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5611. bool AllNonStoredSessionsAnonymous = true;
  5612. TTerminalManager * Manager = TTerminalManager::Instance();
  5613. for (int Index = 0; Index < DataList->Count; Index++)
  5614. {
  5615. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5616. if (SessionData->Link.IsEmpty())
  5617. {
  5618. if (SessionData->HasAnySessionPassword())
  5619. {
  5620. AnyNonStoredSessionWithPassword = true;
  5621. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  5622. {
  5623. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5624. }
  5625. }
  5626. if (!SameText(SessionData->UserName, AnonymousUserName))
  5627. {
  5628. AllNonStoredSessionsAnonymous = false;
  5629. }
  5630. }
  5631. }
  5632. bool SavePasswords;
  5633. bool * PSavePasswords;
  5634. bool NotRecommendedSavingPasswords =
  5635. !CustomWinConfiguration->UseMasterPassword &&
  5636. !AllNonStoredSessionsAnonymous;
  5637. if (Configuration->DisablePasswordStoring ||
  5638. !AnyNonStoredSessionWithPassword)
  5639. {
  5640. PSavePasswords = NULL;
  5641. SavePasswords = false;
  5642. }
  5643. else
  5644. {
  5645. PSavePasswords = &SavePasswords;
  5646. SavePasswords =
  5647. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5648. !NotRecommendedSavingPasswords;
  5649. }
  5650. UnicodeString Name = WorkspaceName();
  5651. bool CreateShortcut = false;
  5652. bool Result =
  5653. DoSaveWorkspaceDialog(
  5654. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5655. EnableAutoSave);
  5656. if (Result)
  5657. {
  5658. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5659. if (CreateShortcut)
  5660. {
  5661. TOperationVisualizer Visualizer;
  5662. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5663. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5664. }
  5665. if (EnableAutoSave)
  5666. {
  5667. WinConfiguration->AutoSaveWorkspace = true;
  5668. WinConfiguration->AutoWorkspace = Name;
  5669. if (PSavePasswords != NULL)
  5670. {
  5671. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5672. }
  5673. }
  5674. }
  5675. return Result;
  5676. }
  5677. //---------------------------------------------------------------------------
  5678. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TManagedTerminal * Terminal)
  5679. {
  5680. DebugAssert(!IsLocalBrowserMode());
  5681. SAFE_DESTROY(Terminal->RemoteExplorerState);
  5682. if (WinConfiguration->PreservePanelState)
  5683. {
  5684. Terminal->RemoteExplorerState = RemoteDirView->SaveState();
  5685. }
  5686. UpdateSessionData(Terminal->StateData);
  5687. }
  5688. //---------------------------------------------------------------------------
  5689. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5690. {
  5691. // Keep in sync with TSessionData::CopyStateData
  5692. DebugAssert(!IsLocalBrowserMode());
  5693. DebugAssert(Data != NULL);
  5694. // This is inconsistent with how color (for example) is handled.
  5695. Data->Name = Terminal->SessionData->Name;
  5696. // cannot use RemoteDirView->Path, because it is empty if connection
  5697. // was already closed
  5698. // also only peek, we may not be connected at all atm
  5699. // so make sure we do not try retrieving current directory from the server
  5700. // (particularly with FTP)
  5701. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5702. if (!ACurrentDirectory.IsEmpty())
  5703. {
  5704. Data->RemoteDirectory = ACurrentDirectory;
  5705. }
  5706. Data->Color = SessionColor;
  5707. }
  5708. //---------------------------------------------------------------------------
  5709. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5710. {
  5711. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5712. DebugAssert(Toolbar != NULL);
  5713. for (int i = 0; i < Toolbar->Items->Count; i++)
  5714. {
  5715. TTBXCustomDropDownItem * DropDownItem;
  5716. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5717. if (DropDownItem != NULL)
  5718. {
  5719. ToolbarItemResize(DropDownItem,
  5720. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5721. Toolbar->NonClientWidth);
  5722. break;
  5723. }
  5724. }
  5725. }
  5726. //---------------------------------------------------------------------------
  5727. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5728. {
  5729. Item->EditWidth = Width;
  5730. }
  5731. //---------------------------------------------------------------------------
  5732. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5733. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5734. {
  5735. for (int i = 0; i < Toolbar->Items->Count; i++)
  5736. {
  5737. TTBXCustomDropDownItem * DropDownItem;
  5738. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5739. if (DropDownItem != NULL)
  5740. {
  5741. ASize.x -= DropDownItem->EditWidth;
  5742. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5743. }
  5744. }
  5745. }
  5746. //---------------------------------------------------------------------------
  5747. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5748. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5749. {
  5750. if (WinConfiguration->DDWarnLackOfTempSpace)
  5751. {
  5752. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5753. if (!ADrive.IsEmpty())
  5754. {
  5755. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5756. DebugAssert(RequiredSpace >= 0);
  5757. __int64 RequiredWithReserve;
  5758. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5759. if (FreeSpace < RequiredWithReserve)
  5760. {
  5761. unsigned int Result;
  5762. TMessageParams Params(mpNeverAskAgainCheck);
  5763. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5764. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5765. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5766. if (Result == qaNeverAskAgain)
  5767. {
  5768. WinConfiguration->DDWarnLackOfTempSpace = false;
  5769. }
  5770. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5771. }
  5772. }
  5773. }
  5774. }
  5775. //---------------------------------------------------------------------------
  5776. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5777. {
  5778. DoOpenDirectoryDialog(odAddBookmark, Side);
  5779. }
  5780. //---------------------------------------------------------------------------
  5781. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5782. {
  5783. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5784. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  5785. try
  5786. {
  5787. TCustomDirView * DView = DirView(Side);
  5788. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5789. {
  5790. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5791. }
  5792. }
  5793. catch (...)
  5794. {
  5795. delete VisitedDirectories;
  5796. throw;
  5797. }
  5798. return VisitedDirectories;
  5799. }
  5800. //---------------------------------------------------------------------------
  5801. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5802. TOpenDirectoryMode Mode, TOperationSide Side)
  5803. {
  5804. bool Continue = true;
  5805. if (Mode == odAddBookmark)
  5806. {
  5807. TMessageParams Params(mpNeverAskAgainCheck);
  5808. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5809. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5810. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  5811. unsigned int Answer =
  5812. MessageDialog(Message,
  5813. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  5814. if (Answer == qaNeverAskAgain)
  5815. {
  5816. Continue = true;
  5817. WinConfiguration->UseSharedBookmarks = true;
  5818. }
  5819. else if (Answer == qaOK)
  5820. {
  5821. Continue = true;
  5822. WinConfiguration->UseSharedBookmarks = false;
  5823. }
  5824. else
  5825. {
  5826. Continue = false;
  5827. }
  5828. }
  5829. if (Continue)
  5830. {
  5831. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  5832. try
  5833. {
  5834. UnicodeString Name = DirView(Side)->PathName;
  5835. bool Repeat;
  5836. do
  5837. {
  5838. Repeat = false;
  5839. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  5840. // do not allow switching to location profiles,
  5841. // if we are not connected
  5842. HasDirView[osLocal] && (Terminal != NULL)))
  5843. {
  5844. TWindowLock Lock(this);
  5845. if (!TryOpenDirectory(Side, Name))
  5846. {
  5847. Repeat = true;
  5848. Mode = odBrowse;
  5849. }
  5850. }
  5851. }
  5852. while (Repeat);
  5853. }
  5854. __finally
  5855. {
  5856. delete VisitedDirectories;
  5857. }
  5858. }
  5859. }
  5860. //---------------------------------------------------------------------------
  5861. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  5862. {
  5863. DebugAssert(!FTerminal->CommandSessionOpened);
  5864. return TTerminalManager::Instance()->ConnectTerminal(FTerminal->CommandSession);
  5865. }
  5866. //---------------------------------------------------------------------------
  5867. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  5868. {
  5869. bool Result = FTerminal->IsCapable[Capability] ||
  5870. FTerminal->CommandSessionOpened;
  5871. if (!Result)
  5872. {
  5873. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  5874. if (!GUIConfiguration->ConfirmCommandSession)
  5875. {
  5876. Result = true;
  5877. }
  5878. else
  5879. {
  5880. TMessageParams Params(mpNeverAskAgainCheck);
  5881. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  5882. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  5883. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  5884. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  5885. if (Answer == qaNeverAskAgain)
  5886. {
  5887. GUIConfiguration->ConfirmCommandSession = false;
  5888. Result = true;
  5889. }
  5890. else if (Answer == qaOK)
  5891. {
  5892. Result = true;
  5893. }
  5894. }
  5895. if (Result)
  5896. {
  5897. Result = CommandSessionFallback();
  5898. }
  5899. }
  5900. return Result;
  5901. }
  5902. //---------------------------------------------------------------------------
  5903. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  5904. {
  5905. if (EnsureCommandSessionFallback(fcAnyCommand))
  5906. {
  5907. DoConsoleDialog(Terminal, Command);
  5908. }
  5909. }
  5910. //---------------------------------------------------------------------------
  5911. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  5912. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  5913. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  5914. {
  5915. if (!IsSideLocalBrowser(osRemote) &&
  5916. IsFileControl(DropSourceControl, osRemote) &&
  5917. (FDDExtMapFile != NULL))
  5918. {
  5919. Accept = true;
  5920. }
  5921. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  5922. DebugAssert(FDDTargetControl != NULL);
  5923. }
  5924. //---------------------------------------------------------------------------
  5925. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  5926. _di_IDataObject DataObj, int KeyState,
  5927. const TPoint & Point, int & Effect, bool & Accept)
  5928. {
  5929. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  5930. }
  5931. //---------------------------------------------------------------------------
  5932. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  5933. _di_IDataObject DataObj, int KeyState,
  5934. const TPoint & Point, int & Effect, bool & Accept)
  5935. {
  5936. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  5937. }
  5938. //---------------------------------------------------------------------------
  5939. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  5940. TObject * Sender)
  5941. {
  5942. DebugUsedParam(Sender);
  5943. DebugAssert(FDDTargetControl == Sender);
  5944. FDDTargetControl = NULL;
  5945. }
  5946. //---------------------------------------------------------------------------
  5947. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  5948. {
  5949. FileControlDDDragLeave(SessionsPageControl);
  5950. }
  5951. //---------------------------------------------------------------------------
  5952. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  5953. {
  5954. FileControlDDDragLeave(QueueView3);
  5955. }
  5956. //---------------------------------------------------------------------------
  5957. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  5958. {
  5959. DebugAssert(!IsSideLocalBrowser(Side));
  5960. DebugUsedParam(Side);
  5961. bool Edit = false;
  5962. TRemoteFile * File = NULL;
  5963. UnicodeString FileName;
  5964. UnicodeString PointTo;
  5965. bool SymbolicLink = true;
  5966. if (RemoteDirView->ItemFocused)
  5967. {
  5968. DebugAssert(RemoteDirView->ItemFocused->Data);
  5969. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  5970. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  5971. if (Edit)
  5972. {
  5973. FileName = File->FileName;
  5974. PointTo = File->LinkTo;
  5975. }
  5976. else
  5977. {
  5978. PointTo = File->FileName;
  5979. }
  5980. }
  5981. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  5982. Terminal->IsCapable[fcHardLink]))
  5983. {
  5984. Configuration->Usage->Inc(L"RemoteLinksCreated");
  5985. if (Edit)
  5986. {
  5987. DebugAssert(File->FileName == FileName);
  5988. int Params = dfNoRecursive;
  5989. Terminal->ExceptionOnFail = true;
  5990. try
  5991. {
  5992. Terminal->DeleteFile(L"", File, &Params);
  5993. }
  5994. __finally
  5995. {
  5996. Terminal->ExceptionOnFail = false;
  5997. }
  5998. }
  5999. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6000. }
  6001. }
  6002. //---------------------------------------------------------------------------
  6003. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6004. {
  6005. return
  6006. (IsSideLocalBrowser(Side) ||
  6007. ((Terminal != NULL) &&
  6008. Terminal->ResolvingSymlinks &&
  6009. Terminal->IsCapable[fcSymbolicLink]));
  6010. }
  6011. //---------------------------------------------------------------------------
  6012. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6013. {
  6014. return
  6015. !IsSideLocalBrowser(FCurrentSide) &&
  6016. (RemoteDirView->ItemFocused != NULL) &&
  6017. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6018. Terminal->SessionData->ResolveSymlinks;
  6019. }
  6020. //---------------------------------------------------------------------------
  6021. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6022. {
  6023. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6024. FForceExecution = true;
  6025. try
  6026. {
  6027. DirView(osCurrent)->ExecuteCurrentFile();
  6028. }
  6029. __finally
  6030. {
  6031. FForceExecution = false;
  6032. }
  6033. }
  6034. //---------------------------------------------------------------------------
  6035. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6036. {
  6037. TEditorData ExternalEditor;
  6038. ExternalEditor.Editor = edExternal;
  6039. bool Remember = false;
  6040. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6041. {
  6042. if (Remember)
  6043. {
  6044. TEditorList * EditorList = new TEditorList;
  6045. try
  6046. {
  6047. *EditorList = *WinConfiguration->EditorList;
  6048. bool Found = false;
  6049. int i = 0;
  6050. while (!Found && (i < EditorList->Count))
  6051. {
  6052. const TEditorPreferences * Editor = EditorList->Editors[i];
  6053. if ((Editor->Data->Editor == edExternal) &&
  6054. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6055. {
  6056. Found = true;
  6057. }
  6058. i++;
  6059. }
  6060. if (!Found)
  6061. {
  6062. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6063. WinConfiguration->EditorList = EditorList;
  6064. }
  6065. }
  6066. __finally
  6067. {
  6068. delete EditorList;
  6069. }
  6070. }
  6071. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6072. }
  6073. }
  6074. //---------------------------------------------------------------------------
  6075. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6076. {
  6077. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  6078. if (FFileFindTerminal == Sender)
  6079. {
  6080. FFileFindTerminal = NULL;
  6081. HideFileFindDialog();
  6082. }
  6083. if (FClipboardTerminal == Sender)
  6084. {
  6085. ClipboardClear(); // implies ClipboardStop
  6086. }
  6087. }
  6088. //---------------------------------------------------------------------------
  6089. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6090. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6091. {
  6092. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6093. DebugAssert(Terminal != NULL);
  6094. if (Data->Terminal == Terminal)
  6095. {
  6096. Data->Terminal = NULL;
  6097. }
  6098. }
  6099. //---------------------------------------------------------------------------
  6100. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  6101. {
  6102. UpdateControls();
  6103. SessionColor = TColor(0);
  6104. UpdateRemotePathComboBox(false);
  6105. NeedSession(false);
  6106. }
  6107. //---------------------------------------------------------------------------
  6108. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6109. {
  6110. try
  6111. {
  6112. // Cache, as the login dialog can change its value
  6113. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6114. try
  6115. {
  6116. if (ShowLogin)
  6117. {
  6118. bool ReloadSessions = !Startup;
  6119. TTerminalManager::Instance()->NewSession(false, L"", ReloadSessions, this);
  6120. }
  6121. else if (Startup && WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  6122. // This detects if workspace was saved the last time the main widow was closed
  6123. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  6124. {
  6125. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false);
  6126. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  6127. }
  6128. }
  6129. __finally
  6130. {
  6131. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6132. // (so there was no chance for the user to open any session yet)
  6133. if ((ShowLogin || !Startup) &&
  6134. !WinConfiguration->KeepOpenWhenNoSession &&
  6135. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6136. {
  6137. TerminateApplication();
  6138. }
  6139. }
  6140. }
  6141. catch (EAbort &)
  6142. {
  6143. // swallow
  6144. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6145. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6146. }
  6147. }
  6148. //---------------------------------------------------------------------------
  6149. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged()
  6150. {
  6151. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  6152. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6153. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  6154. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6155. try
  6156. {
  6157. while ((SessionsPageControl->PageCount > TerminalList->Count + 1) ||
  6158. // Clear the design time unthemed tab
  6159. ((SessionsPageControl->PageCount > 0) &&
  6160. (dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[SessionsPageControl->PageCount - 1]) == NULL)))
  6161. {
  6162. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6163. }
  6164. for (int Index = 0; Index <= TerminalList->Count; Index++)
  6165. {
  6166. TThemeTabSheet * TabSheet;
  6167. if (Index >= SessionsPageControl->PageCount)
  6168. {
  6169. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6170. TabSheet->PageControl = SessionsPageControl;
  6171. }
  6172. else
  6173. {
  6174. TabSheet = DebugNotNull(dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[Index]));
  6175. }
  6176. bool IsSessionTab = (Index < TerminalList->Count);
  6177. if (IsSessionTab)
  6178. {
  6179. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  6180. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6181. UpdateSessionTab(TabSheet);
  6182. }
  6183. else
  6184. {
  6185. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  6186. TabSheet->Tag = 0; // not really needed
  6187. TabSheet->Shadowed = false;
  6188. // 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)
  6189. UpdateNewSessionTab();
  6190. }
  6191. TabSheet->ShowCloseButton = IsSessionTab;
  6192. }
  6193. }
  6194. __finally
  6195. {
  6196. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6197. }
  6198. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  6199. }
  6200. //---------------------------------------------------------------------------
  6201. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  6202. {
  6203. DoTerminalListChanged();
  6204. }
  6205. //---------------------------------------------------------------------------
  6206. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  6207. {
  6208. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6209. DebugAssert(TabSheet->ImageIndex == 0);
  6210. TabSheet->Caption =
  6211. WinConfiguration->SelectiveToolbarText ?
  6212. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  6213. UnicodeString();
  6214. }
  6215. //---------------------------------------------------------------------------
  6216. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  6217. {
  6218. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6219. }
  6220. //---------------------------------------------------------------------------
  6221. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  6222. {
  6223. if (DebugAlwaysTrue(TabSheet != NULL))
  6224. {
  6225. TManagedTerminal * ATerminal = GetSessionTabTerminal(TabSheet);
  6226. if (DebugAlwaysTrue(ATerminal != NULL))
  6227. {
  6228. TColor Color = (ATerminal == FTerminal) ? FSessionColor : ATerminal->StateData->Color;
  6229. TabSheet->ImageIndex = AddSessionColor(Color);
  6230. UnicodeString TabCaption = TTerminalManager::Instance()->GetTerminalTitle(ATerminal, true);
  6231. TabSheet->Caption = SessionsPageControl->FormatCaptionWithCloseButton(TabCaption);
  6232. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  6233. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  6234. {
  6235. ThemeTabSheet->Shadowed = !ATerminal->Active;
  6236. }
  6237. }
  6238. }
  6239. }
  6240. //---------------------------------------------------------------------------
  6241. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6242. {
  6243. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6244. TManagedTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  6245. bool Result = (Terminal != NULL);
  6246. if (Result)
  6247. {
  6248. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6249. }
  6250. else
  6251. {
  6252. try
  6253. {
  6254. NewSession(false);
  6255. }
  6256. __finally
  6257. {
  6258. DoTerminalListChanged();
  6259. }
  6260. FSessionsPageControlNewSessionTime = Now();
  6261. }
  6262. return Result;
  6263. }
  6264. //---------------------------------------------------------------------------
  6265. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6266. {
  6267. SessionTabSwitched();
  6268. }
  6269. //---------------------------------------------------------------------------
  6270. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6271. {
  6272. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6273. DebugAssert(TransferList != NULL);
  6274. UnicodeString Name;
  6275. if (TransferList->ItemIndex <= 0)
  6276. {
  6277. Name = L"";
  6278. }
  6279. else
  6280. {
  6281. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6282. }
  6283. if (FCopyParamAutoSelected.IsEmpty())
  6284. {
  6285. // if previous preset was not autoselected, make new preset the "default"
  6286. FCopyParamDefault = Name;
  6287. }
  6288. GUIConfiguration->CopyParamCurrent = Name;
  6289. }
  6290. //---------------------------------------------------------------------------
  6291. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6292. {
  6293. // sanity check
  6294. bool ExistingPreset =
  6295. (FTransferListHoverIndex >= 0) &&
  6296. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6297. DebugAssert(ExistingPreset);
  6298. if (ExistingPreset)
  6299. {
  6300. HDC DC = GetDC(0);
  6301. TCanvas * Canvas = new TCanvas();
  6302. try
  6303. {
  6304. Canvas->Handle = DC;
  6305. Canvas->Font = GetToolbarFont(this);
  6306. UnicodeString Name;
  6307. if (FTransferListHoverIndex == 0)
  6308. {
  6309. Name = L"";
  6310. }
  6311. else
  6312. {
  6313. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6314. }
  6315. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6316. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6317. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6318. static_cast<TObject*>(GetComponent(fcTransferList)));
  6319. UnicodeString InfoStr =
  6320. GUIConfiguration->CopyParamPreset[Name].
  6321. GetInfoStr(L"; ",
  6322. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General));
  6323. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6324. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6325. {
  6326. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6327. {
  6328. InfoStr.SetLength(InfoStr.Length() - 1);
  6329. }
  6330. InfoStr += Ellipsis;
  6331. }
  6332. // UpdateCaption does not cause invalidation of whole submenu, while
  6333. // setting Caption property does.
  6334. // also it probably does not resize the label, even if necessary
  6335. // (we do not want that anyway)
  6336. TransferLabel->UpdateCaption(InfoStr);
  6337. }
  6338. __finally
  6339. {
  6340. Canvas->Handle = NULL;
  6341. ReleaseDC(0, DC);
  6342. delete Canvas;
  6343. }
  6344. }
  6345. }
  6346. //---------------------------------------------------------------------------
  6347. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6348. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6349. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6350. {
  6351. if (FTransferListHoverIndex != AHoverIndex)
  6352. {
  6353. FTransferListHoverIndex = AHoverIndex;
  6354. UpdateTransferLabel();
  6355. }
  6356. }
  6357. //---------------------------------------------------------------------------
  6358. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6359. {
  6360. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6361. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6362. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6363. {
  6364. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6365. {
  6366. OpenDirectory(GetSide(osCurrent));
  6367. Message.Result = 1;
  6368. }
  6369. else
  6370. {
  6371. TForm::Dispatch(&Message);
  6372. }
  6373. }
  6374. else
  6375. {
  6376. TForm::Dispatch(&Message);
  6377. }
  6378. }
  6379. //---------------------------------------------------------------------------
  6380. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6381. {
  6382. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6383. if ((FIgnoreNextDialogChar != 0) &&
  6384. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6385. {
  6386. Message.Result = 1;
  6387. }
  6388. else
  6389. {
  6390. TForm::Dispatch(&Message);
  6391. }
  6392. FIgnoreNextDialogChar = 0;
  6393. }
  6394. //---------------------------------------------------------------------------
  6395. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6396. {
  6397. // The four low-order bits are used internally by Windows
  6398. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6399. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6400. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6401. // 61730 - restore thru double click - undocumented
  6402. // 61490 - maximize thru double click - undocumented
  6403. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6404. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6405. {
  6406. SysResizing(Cmd);
  6407. }
  6408. TForm::Dispatch(&Message);
  6409. }
  6410. //---------------------------------------------------------------------------
  6411. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6412. {
  6413. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6414. // Also there no point returning TRUE as we are not able to
  6415. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6416. // Hence the process termination might be safer :)
  6417. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6418. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6419. {
  6420. Message.Result = FALSE;
  6421. }
  6422. else
  6423. {
  6424. Message.Result = TRUE;
  6425. }
  6426. // Do not call default handling as that triggers OnCloseQuery,
  6427. // where our implementation will popup confirmation dialogs, what we do not want,
  6428. // as per Vista guidelines:
  6429. // https://docs.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6430. }
  6431. //---------------------------------------------------------------------------
  6432. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6433. {
  6434. if (Message.EndSession && IsApplicationMinimized())
  6435. {
  6436. // WORKAROUND
  6437. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6438. // when it receives WM_ENDSESSION,
  6439. // but that sometimes (particularly when application is minimized) cause crashes.
  6440. // Crash popups message that blocks log off.
  6441. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6442. // so we call ExitProcess() immediately, not even trying to cleanup.
  6443. // It still causes beep, so there's likely some popup, but it does not block
  6444. // log off.
  6445. ExitProcess(0);
  6446. }
  6447. TForm::Dispatch(&Message);
  6448. }
  6449. #ifdef _DEBUG
  6450. //---------------------------------------------------------------------------
  6451. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6452. {
  6453. TForm::Dispatch(&Message);
  6454. }
  6455. #endif
  6456. //---------------------------------------------------------------------------
  6457. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6458. {
  6459. }
  6460. //---------------------------------------------------------------------------
  6461. void __fastcall TCustomScpExplorerForm::DoShow()
  6462. {
  6463. // only now are the controls resized finally, so the size constraints
  6464. // will not conflict with possibly very small window size
  6465. RestoreFormParams();
  6466. // This indicates that the form was rescaled after parameters were loaded and
  6467. // as most probably the parameters were actually saved with the current form scaling
  6468. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6469. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6470. // and no rounding problems will occur.
  6471. // See also RestoreForm.
  6472. // (we should be safe to call RestoreParams unconditionally)
  6473. if (PixelsPerInch != Screen->PixelsPerInch)
  6474. {
  6475. RestoreParams();
  6476. }
  6477. FixControlsPlacement();
  6478. if (Position == poDefaultPosOnly)
  6479. {
  6480. CutFormToDesktop(this);
  6481. }
  6482. TForm::DoShow();
  6483. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6484. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6485. if (Terminal == NULL)
  6486. {
  6487. StartingDisconnected();
  6488. }
  6489. UpdatePixelsPerInchMainWindowCounter();
  6490. FShowing = true;
  6491. }
  6492. //---------------------------------------------------------------------------
  6493. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6494. {
  6495. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6496. }
  6497. //---------------------------------------------------------------------------
  6498. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  6499. {
  6500. DoTerminalListChanged();
  6501. }
  6502. //---------------------------------------------------------------------------
  6503. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6504. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6505. TNotifyEvent OnBalloonClick, TObject * UserData)
  6506. {
  6507. bool Do;
  6508. UnicodeString Message;
  6509. if (E == NULL)
  6510. {
  6511. Message = Str;
  6512. Do = true;
  6513. }
  6514. else
  6515. {
  6516. Do = ExceptionMessage(E, Message);
  6517. }
  6518. if (Do && WinConfiguration->BalloonNotifications)
  6519. {
  6520. UnicodeString Title;
  6521. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6522. {
  6523. Message = TrimLeft(Message);
  6524. }
  6525. else
  6526. {
  6527. Message = UnformatMessage(Message);
  6528. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6529. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6530. Title = LoadResourceString(Captions[Type]);
  6531. if (Terminal != NULL)
  6532. {
  6533. Title = FORMAT(L"%s - %s",
  6534. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), Title));
  6535. }
  6536. }
  6537. if (Seconds == 0)
  6538. {
  6539. Seconds = WinConfiguration->NotificationsTimeout;
  6540. }
  6541. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6542. }
  6543. }
  6544. //---------------------------------------------------------------------------
  6545. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6546. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6547. UnicodeString HelpKeyword, const TMessageParams * Params,
  6548. TTerminal * Terminal)
  6549. {
  6550. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6551. (FOnFeedSynchronizeError != NULL)) &&
  6552. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6553. {
  6554. if (FOnFeedSynchronizeError != NULL)
  6555. {
  6556. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6557. }
  6558. else
  6559. {
  6560. DebugAssert(FErrorList != NULL);
  6561. TStringList * MoreMessagesCopy = NULL;
  6562. if (MoreMessages)
  6563. {
  6564. MoreMessagesCopy = new TStringList();
  6565. MoreMessagesCopy->Assign(MoreMessages);
  6566. }
  6567. FErrorList->AddObject(Message, MoreMessagesCopy);
  6568. }
  6569. PopupTrayBalloon(Terminal, Message, Type);
  6570. return ContinueAnswer(Answers);
  6571. }
  6572. else
  6573. {
  6574. bool UseBalloon = FTrayIcon->Visible;
  6575. if (UseBalloon)
  6576. {
  6577. PopupTrayBalloon(Terminal, Message, Type);
  6578. }
  6579. unsigned int Result;
  6580. try
  6581. {
  6582. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6583. }
  6584. __finally
  6585. {
  6586. // cancel only balloon we popped up, otherwise we may cancel notification
  6587. // balloon that was there before the message dialog
  6588. // (such as "dd confirmation opt in balloon)
  6589. if (UseBalloon)
  6590. {
  6591. FTrayIcon->CancelBalloon();
  6592. }
  6593. }
  6594. return Result;
  6595. }
  6596. }
  6597. //---------------------------------------------------------------------------
  6598. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6599. TTerminal * Terminal, Exception * E)
  6600. {
  6601. if (FTrayIcon->Visible)
  6602. {
  6603. PopupTrayBalloon(Terminal, L"", qtError, E);
  6604. }
  6605. // particularly prevent opening new session from jump list,
  6606. // while exception is shown
  6607. NonVisualDataModule->StartBusy();
  6608. try
  6609. {
  6610. ShowExtendedExceptionEx(Terminal, E);
  6611. }
  6612. __finally
  6613. {
  6614. NonVisualDataModule->EndBusy();
  6615. FTrayIcon->CancelBalloon();
  6616. }
  6617. }
  6618. //---------------------------------------------------------------------------
  6619. void __fastcall TCustomScpExplorerForm::TerminalReady()
  6620. {
  6621. if (Terminal->Active)
  6622. {
  6623. InitStatusBar();
  6624. }
  6625. // cannot rely on active page being page for active terminal,
  6626. // as it can happen that active page is the "new session" page
  6627. // (e.g. when reconnecting active terminal, while login dialog
  6628. // invoked from "new session" page is modal)
  6629. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6630. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  6631. }
  6632. //---------------------------------------------------------------------------
  6633. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6634. TTerminal * Terminal, Exception * E)
  6635. {
  6636. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6637. if (!Terminal->Active)
  6638. {
  6639. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6640. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6641. {
  6642. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6643. }
  6644. }
  6645. }
  6646. //---------------------------------------------------------------------------
  6647. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6648. UnicodeString Message, TQueryType Type,
  6649. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6650. {
  6651. if ((E == NULL) ||
  6652. ExceptionMessage(E, Message))
  6653. {
  6654. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6655. if (Important)
  6656. {
  6657. Seconds *= 5;
  6658. }
  6659. UnicodeString NoteMessage(UnformatMessage(Message));
  6660. if (Terminal != NULL)
  6661. {
  6662. NoteMessage = FORMAT(L"%s: %s",
  6663. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), NoteMessage));
  6664. }
  6665. if (WinConfiguration->BalloonNotifications)
  6666. {
  6667. AddNote(NoteMessage);
  6668. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6669. }
  6670. else
  6671. {
  6672. FlashOnBackground();
  6673. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6674. }
  6675. }
  6676. }
  6677. //---------------------------------------------------------------------------
  6678. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6679. {
  6680. RestoreApp();
  6681. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6682. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6683. !NonVisualDataModule->Busy)
  6684. {
  6685. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  6686. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6687. if (!ComponentVisible[fcQueueView])
  6688. {
  6689. ToggleQueueVisibility();
  6690. GoToQueue();
  6691. }
  6692. }
  6693. }
  6694. //---------------------------------------------------------------------------
  6695. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  6696. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6697. {
  6698. UnicodeString Message;
  6699. TNotifyEvent OnClick = NULL;
  6700. TObject * UserData = NULL;
  6701. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6702. switch (Event)
  6703. {
  6704. case qeEmptyButMonitored:
  6705. if ((ATerminal != Terminal) || QueueInvisible)
  6706. {
  6707. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6708. OnClick = QueueEmptyNoteClicked;
  6709. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6710. TerminalNoteData->Terminal = ATerminal;
  6711. UserData = TerminalNoteData;
  6712. }
  6713. break;
  6714. case qeEmpty:
  6715. OperationComplete(ATerminal->QueueOperationStart);
  6716. break;
  6717. case qePendingUserAction:
  6718. if ((ATerminal != Terminal) ||
  6719. (QueueInvisible && !IsQueueAutoPopup()))
  6720. {
  6721. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6722. }
  6723. break;
  6724. default:
  6725. DebugFail();
  6726. }
  6727. if (!Message.IsEmpty())
  6728. {
  6729. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6730. }
  6731. }
  6732. //---------------------------------------------------------------------------
  6733. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6734. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6735. {
  6736. if (FDDExtMapFile != NULL)
  6737. {
  6738. CloseHandle(FDDExtMapFile);
  6739. FDDExtMapFile = NULL;
  6740. }
  6741. if (WinConfiguration->DDFakeFile)
  6742. {
  6743. DDFakeFileInitDrag(FileList, Created);
  6744. }
  6745. }
  6746. //---------------------------------------------------------------------------
  6747. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  6748. {
  6749. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  6750. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  6751. {
  6752. FFakeFileDropTarget = FileName;
  6753. }
  6754. }
  6755. //---------------------------------------------------------------------------
  6756. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  6757. {
  6758. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6759. if (!ForceDirectories(ApiPath(Result)))
  6760. {
  6761. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  6762. }
  6763. FileSetAttr(ApiPath(Result), faHidden);
  6764. return Result;
  6765. }
  6766. //---------------------------------------------------------------------------
  6767. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  6768. bool & Created)
  6769. {
  6770. FFakeFileDropTarget = UnicodeString();
  6771. FDragFakeDirectory = CreateFakeTransferDirectory();
  6772. FileList->AddItem(NULL, FDragFakeDirectory);
  6773. Created = true;
  6774. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  6775. {
  6776. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  6777. }
  6778. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6779. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6780. {
  6781. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6782. TDragExtCommStruct* CommStruct;
  6783. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6784. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6785. DebugAssert(CommStruct != NULL);
  6786. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  6787. CommStruct->Dragging = true;
  6788. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  6789. LENOF(CommStruct->DropDest));
  6790. NULL_TERMINATE(CommStruct->DropDest);
  6791. UnmapViewOfFile(CommStruct);
  6792. }
  6793. }
  6794. //---------------------------------------------------------------------------
  6795. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  6796. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  6797. {
  6798. bool Result;
  6799. DebugAssert(!IsLocalBrowserMode());
  6800. if (Sender == RemoteDirView)
  6801. {
  6802. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  6803. }
  6804. else
  6805. {
  6806. DebugAssert(Sender == RemoteDriveView);
  6807. TStrings * FileList = RemoteDriveView->DragFileList();
  6808. try
  6809. {
  6810. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  6811. }
  6812. __finally
  6813. {
  6814. delete FileList;
  6815. }
  6816. }
  6817. if (FDDTargetControl == RemoteDriveView)
  6818. {
  6819. RemoteDriveView->UpdateDropTarget();
  6820. }
  6821. // foRemoteMove happens when file/dir is dragged within the remote tree view
  6822. // or from tree view to dir view.
  6823. // foMove happens when file/dir is dragged from remote tree view outside of
  6824. // application via dragex
  6825. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  6826. (DropSourceControl == RemoteDriveView))
  6827. {
  6828. RemoteDriveView->UpdateDropSource();
  6829. }
  6830. return Result;
  6831. }
  6832. //---------------------------------------------------------------------------
  6833. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  6834. {
  6835. DebugAssert(!IsLocalBrowserMode());
  6836. // This also handles drops of remote files to queue.
  6837. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  6838. SAFE_DESTROY(FDDFileList);
  6839. if (!FFakeFileDropTarget.IsEmpty())
  6840. {
  6841. RemoveDir(ApiPath(FFakeFileDropTarget));
  6842. }
  6843. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  6844. {
  6845. try
  6846. {
  6847. TDragResult DDResult = (Sender == RemoteDirView) ?
  6848. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  6849. // Focus is moved to the target application,
  6850. // but as we are going to present the UI, we need to steal the focus back.
  6851. // This most likely won't work though (windows does not allow application
  6852. // to steal focus most of the time)
  6853. Application->BringToFront();
  6854. // On older version of Windows we never got drMove here, see also comment below.
  6855. // On Windows 10, we get the "move".
  6856. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  6857. {
  6858. UnicodeString TargetDirectory;
  6859. TFileOperation Operation;
  6860. // drInvalid may mean drMove, see comment below
  6861. switch (DDResult)
  6862. {
  6863. case drCopy:
  6864. Operation = foCopy;
  6865. break;
  6866. case drMove:
  6867. Operation = foMove;
  6868. break;
  6869. default:
  6870. DebugFail();
  6871. case drInvalid:
  6872. // prefer "copy" for safety
  6873. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  6874. break;
  6875. }
  6876. TTransferOperationParam Param;
  6877. UnicodeString CounterName;
  6878. bool ForceQueue;
  6879. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  6880. {
  6881. // we get drInvalid both if d&d was intercepted by ddext,
  6882. // and when users drops on no-drop location.
  6883. // we tell the difference by existence of response from ddext,
  6884. // so we ignore absence of response in this case
  6885. if (DDResult != drInvalid)
  6886. {
  6887. // here we know that the extension is installed,
  6888. // as it is checked as soon as drag&drop starts from
  6889. // RemoteFileControlDDCreateDragFileList
  6890. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  6891. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  6892. {
  6893. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  6894. }
  6895. else
  6896. {
  6897. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  6898. }
  6899. }
  6900. }
  6901. else
  6902. {
  6903. // download using fake file
  6904. Param.Temp = false;
  6905. Param.DragDrop = true;
  6906. if (ForceQueue)
  6907. {
  6908. Param.Queue = asOn;
  6909. }
  6910. if (Sender == RemoteDirView)
  6911. {
  6912. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  6913. }
  6914. if (RemoteFileControlFileOperation(Sender, Operation,
  6915. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  6916. {
  6917. Configuration->Usage->Inc(CounterName);
  6918. }
  6919. }
  6920. }
  6921. }
  6922. __finally
  6923. {
  6924. CloseHandle(FDDExtMapFile);
  6925. FDDExtMapFile = NULL;
  6926. delete FDragFakeMonitors;
  6927. FDragFakeMonitors = NULL;
  6928. RemoveDir(ApiPath(FDragFakeDirectory));
  6929. FDragFakeDirectory = L"";
  6930. FFakeFileDropTarget = UnicodeString();
  6931. }
  6932. }
  6933. if (FDragDropSshTerminate.get() != NULL)
  6934. {
  6935. FDragDropSshTerminate->Rethrow();
  6936. }
  6937. }
  6938. //---------------------------------------------------------------------------
  6939. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  6940. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  6941. {
  6942. // Remember drop effect so we know (when user drops files), if we copy or move
  6943. FLastDropEffect = dwEffect;
  6944. }
  6945. //---------------------------------------------------------------------------
  6946. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  6947. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  6948. {
  6949. bool Result;
  6950. if (FDDTargetControl == QueueView3)
  6951. {
  6952. Directory = DefaultDownloadTargetDirectory();
  6953. Result = true;
  6954. CounterName = L"DownloadsDragDropQueue";
  6955. ForceQueue = true;
  6956. }
  6957. else if (!FFakeFileDropTarget.IsEmpty())
  6958. {
  6959. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  6960. Result = true;
  6961. ForceQueue = false;
  6962. CounterName = L"DownloadsDragDropFakeFile";
  6963. }
  6964. else
  6965. {
  6966. ForceQueue = false;
  6967. Enabled = false;
  6968. try
  6969. {
  6970. int Timer = 0;
  6971. Result = false;
  6972. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  6973. {
  6974. {
  6975. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6976. TDragExtCommStruct* CommStruct;
  6977. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6978. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6979. DebugAssert(CommStruct != NULL);
  6980. Result = !CommStruct->Dragging;
  6981. if (Result)
  6982. {
  6983. Directory = ExtractFilePath(CommStruct->DropDest);
  6984. CounterName = L"DownloadsDragDropExternalExt";
  6985. }
  6986. UnmapViewOfFile(CommStruct);
  6987. }
  6988. if (!Result)
  6989. {
  6990. Sleep(50);
  6991. Timer += 50;
  6992. Application->ProcessMessages();
  6993. }
  6994. }
  6995. }
  6996. __finally
  6997. {
  6998. Enabled = true;
  6999. }
  7000. }
  7001. return Result;
  7002. }
  7003. //---------------------------------------------------------------------------
  7004. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7005. const UnicodeString TempDir, int SecDelay)
  7006. {
  7007. TDateTime Alarm = Now() + (double)((double)SecDelay*OneMillisecond);
  7008. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7009. if (FDelayedDeletionTimer == NULL)
  7010. {
  7011. DebugAssert(HandleAllocated());
  7012. FDelayedDeletionTimer = new TTimer(this);
  7013. FDelayedDeletionTimer->Interval = 10000;
  7014. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7015. }
  7016. else
  7017. {
  7018. FDelayedDeletionTimer->Enabled = true;
  7019. }
  7020. }
  7021. //---------------------------------------------------------------------------
  7022. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7023. {
  7024. DebugAssert(FDelayedDeletionList != NULL);
  7025. TDateTime N = Now();
  7026. TDateTime Alert;
  7027. UnicodeString Directory;
  7028. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7029. {
  7030. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7031. if ((N >= Alert) || (Sender == NULL))
  7032. {
  7033. Directory = FDelayedDeletionList->Strings[Index];
  7034. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7035. {
  7036. FDelayedDeletionList->Delete(Index);
  7037. }
  7038. }
  7039. }
  7040. if (FDelayedDeletionList->Count == 0)
  7041. {
  7042. FDelayedDeletionTimer->Enabled = false;
  7043. }
  7044. }
  7045. //---------------------------------------------------------------------------
  7046. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7047. {
  7048. DebugAssert(!IsLocalBrowserMode());
  7049. if (IsFileControl(FDDTargetControl, osRemote) ||
  7050. (FDDTargetControl == SessionsPageControl))
  7051. {
  7052. TTerminal * TargetTerminal = NULL;
  7053. TFileOperation Operation = foNone;
  7054. if (FDDTargetControl == SessionsPageControl)
  7055. {
  7056. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7057. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7058. // do not allow dropping on the "+" tab
  7059. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  7060. if (TargetTerminal != NULL)
  7061. {
  7062. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7063. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7064. {
  7065. Operation = foRemoteMove;
  7066. }
  7067. else
  7068. {
  7069. Operation = foRemoteCopy;
  7070. }
  7071. }
  7072. }
  7073. else
  7074. {
  7075. if (FLastDropEffect == DROPEFFECT_MOVE)
  7076. {
  7077. Operation = foRemoteMove;
  7078. }
  7079. else if (FLastDropEffect == DROPEFFECT_COPY)
  7080. {
  7081. Operation = foRemoteCopy;
  7082. }
  7083. }
  7084. if (Operation != foNone)
  7085. {
  7086. RemoteFileControlFileOperation(DropSourceControl,
  7087. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7088. }
  7089. // abort drag&drop
  7090. Abort();
  7091. }
  7092. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7093. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7094. (FDDTargetControl != QueueView3))
  7095. {
  7096. DebugAssert(!FDragTempDir.IsEmpty());
  7097. TTransferType Type;
  7098. UnicodeString TempDir = FDragTempDir;
  7099. // We clear FDragTempDir before calling
  7100. // just in case it fail (raises exception)
  7101. FDragTempDir = L"";
  7102. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7103. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7104. // empty directory parameter means temp directory -> don't display it!
  7105. UnicodeString TargetDir = L"";
  7106. int Options = 0;
  7107. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7108. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7109. {
  7110. Abort();
  7111. }
  7112. // TargetDir is set when dropped on local file control
  7113. // (this was workaround for legacy dirview event handling, now it should be
  7114. // made prettier)
  7115. if (TargetDir.IsEmpty())
  7116. {
  7117. TargetDir = TempDir;
  7118. if (ForceDirectories(ApiPath(TargetDir)))
  7119. {
  7120. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7121. FPendingTempSpaceWarn = true;
  7122. try
  7123. {
  7124. FDragDropOperation = true;
  7125. int Params = cpTemporary |
  7126. (Type == ttMove ? cpDelete : 0);
  7127. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7128. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7129. }
  7130. __finally
  7131. {
  7132. FDragDropOperation = false;
  7133. FPendingTempSpaceWarn = false;
  7134. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7135. }
  7136. }
  7137. else
  7138. {
  7139. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7140. }
  7141. }
  7142. }
  7143. }
  7144. //---------------------------------------------------------------------------
  7145. void __fastcall TCustomScpExplorerForm::DDDownload(
  7146. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  7147. {
  7148. DebugAssert(!IsLocalBrowserMode());
  7149. TAutoBatch AutoBatch(this);
  7150. UpdateCopyParamCounters(*CopyParam);
  7151. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7152. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7153. {
  7154. RemoteDriveView->UpdateDropSource();
  7155. }
  7156. }
  7157. //---------------------------------------------------------------------------
  7158. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7159. {
  7160. public:
  7161. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7162. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7163. {
  7164. }
  7165. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7166. {
  7167. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7168. TDataObjectFilesEx::AllowData(FormatEtc);
  7169. }
  7170. };
  7171. //---------------------------------------------------------------------------
  7172. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7173. TObject * Sender, TDataObject *& DataObject)
  7174. {
  7175. if (FDDExtMapFile != NULL)
  7176. {
  7177. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7178. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7179. {
  7180. Abort();
  7181. }
  7182. if (FileList->Count > 0)
  7183. {
  7184. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7185. if (!FilesObject->IsValid(true, true))
  7186. {
  7187. FilesObject->_Release();
  7188. }
  7189. else
  7190. {
  7191. DataObject = FilesObject;
  7192. }
  7193. }
  7194. }
  7195. }
  7196. //---------------------------------------------------------------------------
  7197. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7198. {
  7199. DebugFail();
  7200. }
  7201. //---------------------------------------------------------------------------
  7202. void __fastcall TCustomScpExplorerForm::GoToTree()
  7203. {
  7204. ComponentVisible[fcRemoteTree] = true;
  7205. DriveView(osRemote)->SetFocus();
  7206. }
  7207. //---------------------------------------------------------------------------
  7208. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7209. TPanelExport Export)
  7210. {
  7211. TCustomDirView * DirView = this->DirView(Side);
  7212. std::unique_ptr<TStrings> ExportData(new TStringList());
  7213. switch (Export)
  7214. {
  7215. case pePath:
  7216. ExportData->Add(DirView->PathName);
  7217. break;
  7218. case peFileList:
  7219. case peFullFileList:
  7220. {
  7221. bool FullPath = (Export == peFullFileList);
  7222. DirView->CreateFileList(false, FullPath, ExportData.get());
  7223. for (int Index = 0; Index < ExportData->Count; Index++)
  7224. {
  7225. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7226. {
  7227. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7228. }
  7229. }
  7230. }
  7231. break;
  7232. default:
  7233. DebugFail();
  7234. }
  7235. return ExportData.release();
  7236. }
  7237. //---------------------------------------------------------------------------
  7238. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7239. TPanelExport Export, TPanelExportDestination Destination)
  7240. {
  7241. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7242. PanelExportStore(Side, Export, Destination, ExportData.get());
  7243. }
  7244. //---------------------------------------------------------------------------
  7245. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7246. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7247. TStrings * ExportData)
  7248. {
  7249. if (Destination == pedClipboard)
  7250. {
  7251. TInstantOperationVisualizer Visualizer;
  7252. CopyToClipboard(ExportData);
  7253. }
  7254. else
  7255. {
  7256. DebugFail();
  7257. }
  7258. }
  7259. //---------------------------------------------------------------------------
  7260. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7261. {
  7262. TCustomDirView * DirView = this->DirView(Side);
  7263. UnicodeString Mask = DirView->Mask;
  7264. if (DoFilterMaskDialog(DirView, Mask))
  7265. {
  7266. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7267. Configuration->Usage->Inc(L"Filters");
  7268. }
  7269. }
  7270. //---------------------------------------------------------------------------
  7271. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7272. {
  7273. return FQueueController->DefaultOperation();
  7274. }
  7275. //---------------------------------------------------------------------------
  7276. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7277. TQueueOperation Operation, void ** Param)
  7278. {
  7279. switch (Operation)
  7280. {
  7281. case qoPreferences:
  7282. return true;
  7283. case qoGoTo:
  7284. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7285. case qoOnceEmpty:
  7286. return !FQueueController->Empty;
  7287. default:
  7288. return FQueueController->AllowOperation(Operation, Param);
  7289. }
  7290. }
  7291. //---------------------------------------------------------------------------
  7292. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7293. {
  7294. if (DebugAlwaysTrue(QueueView3->Visible))
  7295. {
  7296. QueueView3->SetFocus();
  7297. }
  7298. }
  7299. //---------------------------------------------------------------------------
  7300. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7301. TQueueOperation Operation, void * Param)
  7302. {
  7303. if (Operation == qoGoTo)
  7304. {
  7305. GoToQueue();
  7306. }
  7307. else if (Operation == qoPreferences)
  7308. {
  7309. PreferencesDialog(pmQueue);
  7310. }
  7311. else
  7312. {
  7313. FQueueController->ExecuteOperation(Operation, Param);
  7314. }
  7315. }
  7316. //---------------------------------------------------------------------------
  7317. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7318. {
  7319. return (Queue != NULL) && Queue->Enabled;
  7320. }
  7321. //---------------------------------------------------------------------------
  7322. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7323. {
  7324. DebugAssert(Queue != NULL);
  7325. if (Queue != NULL)
  7326. {
  7327. Queue->Enabled = !Queue->Enabled;
  7328. }
  7329. }
  7330. //---------------------------------------------------------------------------
  7331. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7332. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7333. {
  7334. FQueueActedItem = QueueView3->ItemFocused;
  7335. }
  7336. //---------------------------------------------------------------------------
  7337. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7338. {
  7339. return TopDock->Height + QueueSplitter->Height;
  7340. }
  7341. //---------------------------------------------------------------------------
  7342. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7343. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7344. {
  7345. // when queue is hidden by double-clicking splitter, stray attempt to
  7346. // resize the panel with strange value arrives, make sure it is ignored
  7347. if (ComponentVisible[fcQueueView])
  7348. {
  7349. int HeightLimit = ClientHeight - GetStaticComponentsHeight() -
  7350. RemotePanel->Constraints->MinHeight;
  7351. if (NewSize > HeightLimit)
  7352. {
  7353. NewSize = HeightLimit;
  7354. }
  7355. }
  7356. else
  7357. {
  7358. Accept = false;
  7359. }
  7360. }
  7361. //---------------------------------------------------------------------------
  7362. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7363. TDragObject *& /*DragObject*/)
  7364. {
  7365. FQueueActedItem = QueueView3->ItemFocused;
  7366. }
  7367. //---------------------------------------------------------------------------
  7368. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7369. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7370. {
  7371. Accept = true;
  7372. if (Source == QueueView3)
  7373. {
  7374. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7375. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7376. if (Accept)
  7377. {
  7378. TQueueItemProxy * QueueItem;
  7379. TQueueItemProxy * DestQueueItem;
  7380. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7381. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7382. Accept = (QueueItem != DestQueueItem) &&
  7383. (QueueItem->Status == TQueueItem::qsPending) &&
  7384. (DestQueueItem->Status == TQueueItem::qsPending);
  7385. }
  7386. }
  7387. }
  7388. //---------------------------------------------------------------------------
  7389. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7390. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7391. {
  7392. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7393. {
  7394. TQueueItemProxy * QueueItem;
  7395. TQueueItemProxy * DestQueueItem;
  7396. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7397. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7398. QueueItem->Move(DestQueueItem);
  7399. }
  7400. }
  7401. //---------------------------------------------------------------------------
  7402. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7403. {
  7404. if ((QueueView3->ItemFocused == NULL) &&
  7405. (QueueView3->Items->Count > 0))
  7406. {
  7407. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7408. }
  7409. QueueLabelUpdateStatus();
  7410. }
  7411. //---------------------------------------------------------------------------
  7412. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7413. {
  7414. QueueLabelUpdateStatus();
  7415. }
  7416. //---------------------------------------------------------------------------
  7417. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7418. {
  7419. QueueLabel->UpdateStatus();
  7420. }
  7421. //---------------------------------------------------------------------------
  7422. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7423. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7424. {
  7425. if (Selected)
  7426. {
  7427. NonVisualDataModule->UpdateNonVisibleActions();
  7428. }
  7429. }
  7430. //---------------------------------------------------------------------------
  7431. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7432. {
  7433. TDragDropFilesEx * Result = NULL;
  7434. if (Sender == SessionsPageControl)
  7435. {
  7436. Result = FSessionsDragDropFilesEx;
  7437. }
  7438. else if (Sender == QueueView3)
  7439. {
  7440. Result = FQueueDragDropFilesEx;
  7441. }
  7442. else
  7443. {
  7444. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7445. if (DirView != NULL)
  7446. {
  7447. Result = DirView->DragDropFilesEx;
  7448. }
  7449. else
  7450. {
  7451. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7452. if (DriveView != NULL)
  7453. {
  7454. Result = DriveView->DragDropFilesEx;
  7455. }
  7456. }
  7457. }
  7458. DebugAssert(Result != NULL);
  7459. return Result;
  7460. }
  7461. //---------------------------------------------------------------------------
  7462. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7463. {
  7464. return (DView->SelCount == DView->FilesCount);
  7465. }
  7466. //---------------------------------------------------------------------------
  7467. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7468. {
  7469. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7470. }
  7471. //---------------------------------------------------------------------------
  7472. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7473. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7474. {
  7475. DebugAssert(!IsLocalBrowserMode());
  7476. TFileOperation Operation;
  7477. switch (Effect)
  7478. {
  7479. case DROPEFFECT_MOVE:
  7480. Operation = foMove;
  7481. break;
  7482. case DROPEFFECT_COPY:
  7483. // occures on WinXP (reported by user)
  7484. default:
  7485. Operation = foCopy;
  7486. break;
  7487. };
  7488. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7489. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7490. if (DragDropFilesEx->FileList->Count > 0)
  7491. {
  7492. TStrings * FileList = new TStringList();
  7493. try
  7494. {
  7495. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7496. {
  7497. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7498. }
  7499. FDragDropOperation = true;
  7500. TTransferOperationParam Param;
  7501. Param.TargetDirectory = TargetPath;
  7502. // upload, no temp dirs
  7503. Param.Temp = false;
  7504. Param.DragDrop = DragDrop;
  7505. Param.Options =
  7506. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7507. if (ForceQueue)
  7508. {
  7509. Param.Queue = asOn;
  7510. }
  7511. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7512. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7513. {
  7514. if (IsFileControl(DropSourceControl, osLocal))
  7515. {
  7516. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7517. }
  7518. else
  7519. {
  7520. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7521. }
  7522. }
  7523. }
  7524. __finally
  7525. {
  7526. FDragDropOperation = false;
  7527. delete FileList;
  7528. }
  7529. }
  7530. }
  7531. //---------------------------------------------------------------------------
  7532. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7533. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7534. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7535. {
  7536. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7537. }
  7538. //---------------------------------------------------------------------------
  7539. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7540. TObject * Sender, int grfKeyState, int & dwEffect)
  7541. {
  7542. DebugAssert(!IsLocalBrowserMode());
  7543. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7544. if ((dwEffect != DROPEFFECT_NONE) &&
  7545. IsFileControl(DropSourceControl, osRemote))
  7546. {
  7547. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7548. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7549. (RemoteDirView->DropTarget == NULL))
  7550. {
  7551. dwEffect = DROPEFFECT_NONE;
  7552. }
  7553. else
  7554. {
  7555. if (dwEffect == DROPEFFECT_COPY)
  7556. {
  7557. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  7558. // currently we support copying always (at least via temporary directory);
  7559. // remove associated checks once this all proves stable and working
  7560. bool CopyCapable = true;
  7561. // if we do not support neither of operations, there's no discussion
  7562. if (!MoveCapable && !CopyCapable)
  7563. {
  7564. dwEffect = DROPEFFECT_NONE;
  7565. }
  7566. else
  7567. {
  7568. // The default effect inside remote panel is move,
  7569. // unless we do not support it, but support copy
  7570. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7571. {
  7572. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  7573. }
  7574. else
  7575. {
  7576. // with ctrl-down, we want copy unless it is not supported
  7577. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  7578. }
  7579. }
  7580. }
  7581. }
  7582. }
  7583. }
  7584. //---------------------------------------------------------------------------
  7585. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7586. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7587. {
  7588. DebugAssert(!IsLocalBrowserMode());
  7589. if (FDDTotalSize >= 0)
  7590. {
  7591. if (File->IsDirectory)
  7592. {
  7593. FDDTotalSize = -1;
  7594. }
  7595. else
  7596. {
  7597. FDDTotalSize += File->Size;
  7598. }
  7599. }
  7600. DebugAssert(!FDragTempDir.IsEmpty());
  7601. // TODO: this is quite ineffective
  7602. // TODO: what if invalid character replacement is disabled?
  7603. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7604. UnicodeString TransferFileName;
  7605. if (Sender == RemoteDriveView)
  7606. {
  7607. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7608. }
  7609. else
  7610. {
  7611. TransferFileName = File->FileName;
  7612. }
  7613. FDDFileList->AddObject(TransferFileName, File);
  7614. }
  7615. //---------------------------------------------------------------------------
  7616. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7617. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7618. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7619. {
  7620. // sometimes we do not get DDEnd so the list is not released
  7621. SAFE_DESTROY(FDDFileList);
  7622. FDDFileList = new TStringList();
  7623. FDragTempDir = WinConfiguration->TemporaryDir();
  7624. FDDTotalSize = 0;
  7625. FDragDropSshTerminate.reset(NULL);
  7626. }
  7627. //---------------------------------------------------------------------------
  7628. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7629. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7630. HRESULT & Result)
  7631. {
  7632. if (Result == DRAGDROP_S_DROP)
  7633. {
  7634. try
  7635. {
  7636. GlobalDragImageList->HideDragImage();
  7637. try
  7638. {
  7639. RemoteFileControlDDTargetDrop();
  7640. }
  7641. catch(ESshTerminate & E)
  7642. {
  7643. FDragDropSshTerminate.reset(E.Clone());
  7644. }
  7645. }
  7646. catch (Exception &E)
  7647. {
  7648. // If downloading fails we need to cancel drag&drop, otherwise
  7649. // Explorer shows error
  7650. // But by the way exception probably never reach this point as
  7651. // it's catched on way.
  7652. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7653. Result = DRAGDROP_S_CANCEL;
  7654. ShowExtendedException(Terminal, &E);
  7655. }
  7656. }
  7657. }
  7658. //---------------------------------------------------------------------------
  7659. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7660. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7661. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7662. {
  7663. TFileMasks::TParams MaskParams;
  7664. MaskParams.Size = Size;
  7665. MaskParams.Modification = Modification;
  7666. // this does not re-parse the mask if it is the same as the last time
  7667. FDirViewMatchMask = Masks;
  7668. bool ImplicitMatch;
  7669. Matches =
  7670. // RecurseInclude parameter has no effect as the Path is empty
  7671. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7672. (AllowImplicitMatches || !ImplicitMatch);
  7673. }
  7674. //---------------------------------------------------------------------------
  7675. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7676. TObject * Sender, TListItem * Item, WORD & Indexes)
  7677. {
  7678. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7679. UnicodeString Ext;
  7680. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  7681. {
  7682. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7683. }
  7684. else
  7685. {
  7686. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7687. }
  7688. if (SameText(Ext, Configuration->PartialExt))
  7689. {
  7690. Indexes |= oiPartial;
  7691. }
  7692. }
  7693. //---------------------------------------------------------------------------
  7694. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7695. {
  7696. return
  7697. DirViewEnabled(osCurrent) &&
  7698. DirView(osCurrent)->CanPasteFromClipBoard();
  7699. }
  7700. //---------------------------------------------------------------------------
  7701. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7702. {
  7703. bool Result = false;
  7704. if (CanPasteToDirViewFromClipBoard())
  7705. {
  7706. Result = true;
  7707. }
  7708. else
  7709. {
  7710. UnicodeString ClipboardText;
  7711. if (NonEmptyTextFromClipboard(ClipboardText))
  7712. {
  7713. if (StoredSessions->IsUrl(ClipboardText))
  7714. {
  7715. Result = true;
  7716. }
  7717. else
  7718. {
  7719. Result = DirViewEnabled(osCurrent);
  7720. }
  7721. }
  7722. }
  7723. return Result;
  7724. }
  7725. //---------------------------------------------------------------------------
  7726. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7727. {
  7728. if (DoesClipboardContainOurFiles())
  7729. {
  7730. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  7731. {
  7732. DebugAssert(!IsLocalBrowserMode());
  7733. TTerminalManager * Manager = TTerminalManager::Instance();
  7734. TTerminal * TergetTerminal = Manager->ActiveTerminal;
  7735. Manager->ActiveTerminal = FClipboardTerminal;
  7736. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TergetTerminal);
  7737. }
  7738. }
  7739. else if (CanPasteToDirViewFromClipBoard())
  7740. {
  7741. DirView(osCurrent)->PasteFromClipBoard();
  7742. }
  7743. else
  7744. {
  7745. UnicodeString ClipboardText;
  7746. if (NonEmptyTextFromClipboard(ClipboardText))
  7747. {
  7748. if (StoredSessions->IsUrl(ClipboardText))
  7749. {
  7750. NewSession(false, ClipboardText);
  7751. }
  7752. else
  7753. {
  7754. DirView(osCurrent)->Path = ClipboardText;
  7755. }
  7756. }
  7757. }
  7758. }
  7759. //---------------------------------------------------------------------------
  7760. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  7761. {
  7762. // TBD
  7763. }
  7764. //---------------------------------------------------------------------------
  7765. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  7766. {
  7767. TCustomDirView * ADirView = DirView(Side);
  7768. ADirView->SelectAll(Mode);
  7769. ADirView->SetFocus();
  7770. }
  7771. //---------------------------------------------------------------------------
  7772. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  7773. {
  7774. TCustomDirView * ADirView = DirView(Side);
  7775. TFileFilter Filter;
  7776. DefaultFileFilter(Filter);
  7777. if (DoSelectMaskDialog(ADirView, Select, Filter))
  7778. {
  7779. Configuration->Usage->Inc(L"MaskSelections");
  7780. ADirView->SelectFiles(Filter, Select);
  7781. ADirView->SetFocus();
  7782. }
  7783. }
  7784. //---------------------------------------------------------------------------
  7785. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  7786. {
  7787. TCustomDirView * ADirView = DirView(Side);
  7788. ADirView->RestoreSelectedNames();
  7789. ADirView->SetFocus();
  7790. }
  7791. //---------------------------------------------------------------------------
  7792. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  7793. {
  7794. TCustomDirView * CurrentDirView = DirView(osCurrent);
  7795. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  7796. {
  7797. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  7798. UnicodeString Ext;
  7799. if (!IsSideLocalBrowser(osCurrent))
  7800. {
  7801. Ext = UnixExtractFileExt(FileName);
  7802. }
  7803. else
  7804. {
  7805. Ext = ExtractFileExt(FileName);
  7806. }
  7807. if (Ext.IsEmpty())
  7808. {
  7809. Ext = L".";
  7810. }
  7811. TFileFilter Filter;
  7812. Filter.Masks = FORMAT(L"*%s", (Ext));
  7813. Filter.Directories = false;
  7814. CurrentDirView->SelectFiles(Filter, Select);
  7815. }
  7816. }
  7817. //---------------------------------------------------------------------------
  7818. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  7819. const TStatusFileInfo & FileInfo, TOperationSide Side)
  7820. {
  7821. UnicodeString Result;
  7822. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  7823. {
  7824. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  7825. }
  7826. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  7827. {
  7828. // noop
  7829. }
  7830. else
  7831. {
  7832. Result =
  7833. FMTLOAD(FILE_INFO_FORMAT,
  7834. (FormatBytes(FileInfo.SelectedSize),
  7835. FormatBytes(FileInfo.FilesSize),
  7836. FormatNumber(FileInfo.SelectedCount),
  7837. FormatNumber(FileInfo.FilesCount)));
  7838. }
  7839. return Result;
  7840. }
  7841. //---------------------------------------------------------------------------
  7842. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  7843. TTBXStatusPanel * Panel, TOperationSide Side)
  7844. {
  7845. if (Panel->Index == 1)
  7846. {
  7847. ToggleShowHiddenFiles();
  7848. }
  7849. else if (Panel->Index == 2)
  7850. {
  7851. Filter(Side);
  7852. }
  7853. }
  7854. //---------------------------------------------------------------------------
  7855. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  7856. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  7857. {
  7858. DebugAssert(!StatusBar->SimplePanel);
  7859. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  7860. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  7861. }
  7862. //---------------------------------------------------------------------------
  7863. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  7864. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  7865. {
  7866. DebugAssert(StatusBar->Panels->Count >= 3);
  7867. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  7868. if (FileInfo.HiddenCount > 0)
  7869. {
  7870. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  7871. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7872. }
  7873. else
  7874. {
  7875. HiddenFilesPanel->ViewPriority = 0;
  7876. // not really necessary, just to cleanup no-longer-valid data
  7877. HiddenFilesPanel->Caption = L"";
  7878. }
  7879. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  7880. if (FileInfo.FilteredCount > 0)
  7881. {
  7882. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  7883. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7884. }
  7885. else
  7886. {
  7887. FilteredFilesPanel->ViewPriority = 0;
  7888. // not really necessary, just to cleanup no-longer-valid data
  7889. FilteredFilesPanel->Caption = L"";
  7890. }
  7891. }
  7892. //---------------------------------------------------------------------------
  7893. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  7894. TObject * /*Sender*/)
  7895. {
  7896. if (DirView(osRemote)->Enabled)
  7897. {
  7898. DirView(osRemote)->SetFocus();
  7899. }
  7900. }
  7901. //---------------------------------------------------------------------------
  7902. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  7903. {
  7904. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  7905. switch (Config.Show)
  7906. {
  7907. case qvShow:
  7908. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  7909. {
  7910. Config.Show = qvHide;
  7911. }
  7912. else
  7913. {
  7914. Config.Show = Config.LastHideShow;
  7915. }
  7916. break;
  7917. case qvHideWhenEmpty:
  7918. if (ComponentVisible[fcQueueView])
  7919. {
  7920. Config.Show = qvHide;
  7921. }
  7922. else
  7923. {
  7924. Config.LastHideShow = Config.Show;
  7925. Config.Show = qvShow;
  7926. }
  7927. break;
  7928. case qvHide:
  7929. Config.LastHideShow = Config.Show;
  7930. Config.Show = qvShow;
  7931. break;
  7932. }
  7933. WinConfiguration->QueueView = Config;
  7934. }
  7935. //---------------------------------------------------------------------------
  7936. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  7937. {
  7938. UnicodeString Result;
  7939. if (FTerminal != NULL)
  7940. {
  7941. switch (WinConfiguration->PathInCaption)
  7942. {
  7943. case picShort:
  7944. Result = TTerminalManager::Instance()->GetTerminalShortPath(FTerminal);
  7945. break;
  7946. case picFull:
  7947. Result = FTerminal->CurrentDirectory;
  7948. break;
  7949. }
  7950. }
  7951. return Result;
  7952. }
  7953. //---------------------------------------------------------------------------
  7954. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  7955. {
  7956. TColor Result = GetWindowColor(FSessionColor);
  7957. return Result;
  7958. }
  7959. //---------------------------------------------------------------------------
  7960. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  7961. {
  7962. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  7963. return Result;
  7964. }
  7965. //---------------------------------------------------------------------------
  7966. void __fastcall TCustomScpExplorerForm::UpdateControls()
  7967. {
  7968. TTerminalManager::Instance()->UpdateAppTitle();
  7969. // WORAKRDOUND: Disabling list view when it is not showing yet does not set its
  7970. // background to gray on Windows 7 (works on Windows 10).
  7971. // See also EnableControl
  7972. if (Showing)
  7973. {
  7974. if (Terminal != NULL)
  7975. {
  7976. // Update path when it changes
  7977. if ((SessionsPageControl->ActivePage != NULL) && (GetSessionTabTerminal(SessionsPageControl->ActivePage) == Terminal))
  7978. {
  7979. UpdateSessionTab(SessionsPageControl->ActivePage);
  7980. }
  7981. }
  7982. NonVisualDataModule->ReconnectSessionAction->Update();
  7983. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  7984. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  7985. CenterReconnectToolbar();
  7986. bool HasTerminal = (Terminal != NULL) && Terminal->Active;
  7987. if (HasTerminal)
  7988. {
  7989. // TODO needed yet with second local browser?
  7990. if (!RemoteDirView->Enabled)
  7991. {
  7992. RemoteDirView->Enabled = true;
  7993. if (FRemoteDirViewWasFocused)
  7994. {
  7995. ActiveControl = RemoteDirView;
  7996. }
  7997. }
  7998. RemoteDirView->Color = PanelColor();
  7999. }
  8000. else
  8001. {
  8002. if (RemoteDirView->Enabled)
  8003. {
  8004. // This is first called when the form is being constructed
  8005. // (not anymore due to Showing test above)
  8006. // but the false is overriden in the constructor later.
  8007. // An even later in TScpCommanderForm::DoShow()
  8008. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  8009. RemoteDirView->Enabled = false;
  8010. RemoteDirView->Color = DisabledPanelColor();
  8011. }
  8012. }
  8013. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8014. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8015. RemoteDriveView->Color = RemoteDirView->Color;
  8016. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8017. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fsBackgroundTransfers];
  8018. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8019. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8020. QueueLabelUpdateStatus();
  8021. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8022. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8023. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  8024. }
  8025. }
  8026. //---------------------------------------------------------------------------
  8027. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8028. {
  8029. UpdateControls();
  8030. }
  8031. //---------------------------------------------------------------------------
  8032. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8033. TObject * Sender)
  8034. {
  8035. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8036. DebugAssert(DirView != NULL);
  8037. DoDirViewLoaded(DirView);
  8038. TransferPresetAutoSelect();
  8039. }
  8040. //---------------------------------------------------------------------------
  8041. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8042. {
  8043. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8044. // first run after installation
  8045. if (double(Updates.LastCheck) == 0)
  8046. {
  8047. // make sure next time there will be an update (if enabled)
  8048. Updates.LastCheck = TDateTime(1);
  8049. WinConfiguration->Updates = Updates;
  8050. }
  8051. else if ((double(Updates.Period) > 0) &&
  8052. (Now() - Updates.LastCheck >= Updates.Period))
  8053. {
  8054. TThreadMethod OnUpdatesChecked = NULL;
  8055. if (!IsUWP())
  8056. {
  8057. OnUpdatesChecked = UpdatesChecked;
  8058. }
  8059. StartUpdateThread(OnUpdatesChecked);
  8060. }
  8061. }
  8062. //---------------------------------------------------------------------------
  8063. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8064. {
  8065. UnicodeString Message;
  8066. bool New;
  8067. TQueryType Type;
  8068. GetUpdatesMessage(Message, New, Type, false);
  8069. if (!Message.IsEmpty())
  8070. {
  8071. if (New)
  8072. {
  8073. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8074. }
  8075. if (!New && (Type != qtWarning))
  8076. {
  8077. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8078. }
  8079. else
  8080. {
  8081. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8082. }
  8083. if (New)
  8084. {
  8085. Configuration->Usage->Inc(L"UpdateNotifications");
  8086. }
  8087. }
  8088. }
  8089. //---------------------------------------------------------------------------
  8090. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8091. {
  8092. RestoreApp();
  8093. bool CanDisplay = !NonVisualDataModule->Busy;
  8094. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8095. {
  8096. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8097. }
  8098. if (CanDisplay)
  8099. {
  8100. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8101. CheckForUpdates(true);
  8102. }
  8103. else
  8104. {
  8105. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8106. }
  8107. }
  8108. //---------------------------------------------------------------------------
  8109. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8110. TCopyParamRuleData & Data)
  8111. {
  8112. DebugAssert(Terminal != NULL);
  8113. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8114. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8115. Data.RemoteDirectory = RemoteDirView->PathName;
  8116. }
  8117. //---------------------------------------------------------------------------
  8118. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8119. {
  8120. // Terminal can be null when we are changing local directory implicitly,
  8121. // because it has been deleted, while no session is connected
  8122. // (Login dialog is open)
  8123. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8124. {
  8125. DebugAssert(!IsLocalBrowserMode());
  8126. TCopyParamRuleData Data;
  8127. GetTransferPresetAutoSelectData(Data);
  8128. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8129. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8130. if (CopyParamIndex < 0)
  8131. {
  8132. // there is no preset that matches autoselection
  8133. // set preset that we consider "default"
  8134. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8135. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8136. }
  8137. else
  8138. {
  8139. // there is preset matching autoselection
  8140. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8141. if (CopyParamName == FCopyParamAutoSelected)
  8142. {
  8143. // autoselected the same preset as the last time
  8144. // make no change (i.e. preserve custom user preset, if any)
  8145. }
  8146. else
  8147. {
  8148. // autoselected the different preset then the last time (or there
  8149. // was default preset set)
  8150. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8151. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8152. }
  8153. }
  8154. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8155. {
  8156. if (CopyParamIndex >= 0)
  8157. {
  8158. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8159. }
  8160. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8161. try
  8162. {
  8163. int Fmt =
  8164. (CopyParamIndex < 0) ?
  8165. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8166. COPY_PARAM_AUTOSELECTED;
  8167. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8168. Data->Message = MainInstructions(Message);
  8169. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8170. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8171. CopyParamAttrs);
  8172. if (CopyParamIndex >= 0)
  8173. {
  8174. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8175. Info = FORMAT(L"%s\n \n%s", (Info,
  8176. FMTLOAD(COPY_PARAM_RULE,
  8177. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8178. }
  8179. Data->Message += L"\n\n" + Info;
  8180. if (WinConfiguration->CopyParamAutoSelectNotice)
  8181. {
  8182. TransferPresetNoteMessage(Data, true);
  8183. }
  8184. else
  8185. {
  8186. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8187. Data = NULL; // ownership passed
  8188. }
  8189. }
  8190. __finally
  8191. {
  8192. delete Data;
  8193. }
  8194. }
  8195. }
  8196. }
  8197. //---------------------------------------------------------------------------
  8198. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8199. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8200. {
  8201. DebugAssert(NoteData != NULL);
  8202. DebugAssert(!IsLocalBrowserMode());
  8203. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8204. TQueryButtonAlias Aliases[1];
  8205. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8206. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8207. Params.Aliases = Aliases;
  8208. Params.AliasesCount = LENOF(Aliases);
  8209. unsigned int Result =
  8210. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8211. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8212. switch (Result)
  8213. {
  8214. case qaNeverAskAgain:
  8215. DebugAssert(AllowNeverAskAgain);
  8216. WinConfiguration->CopyParamAutoSelectNotice = false;
  8217. break;
  8218. case qaIgnore:
  8219. PreferencesDialog(pmPresets);
  8220. break;
  8221. }
  8222. }
  8223. //---------------------------------------------------------------------------
  8224. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8225. {
  8226. // as of now useless, as this is used for notes only, never for balloons, ...
  8227. RestoreApp();
  8228. // .. and we should never be busy here
  8229. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8230. {
  8231. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8232. }
  8233. }
  8234. //---------------------------------------------------------------------------
  8235. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8236. TPreferencesMode APreferencesMode)
  8237. {
  8238. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8239. TCopyParamRuleData Data;
  8240. if (Terminal != NULL)
  8241. {
  8242. PreferencesData.reset(new TPreferencesDialogData());
  8243. GetTransferPresetAutoSelectData(Data);
  8244. PreferencesData->CopyParamRuleData = &Data;
  8245. }
  8246. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8247. }
  8248. //---------------------------------------------------------------------------
  8249. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8250. const TCustomCommandType & Command)
  8251. {
  8252. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8253. {
  8254. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (Command.Command)));
  8255. }
  8256. }
  8257. //---------------------------------------------------------------------------
  8258. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8259. {
  8260. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8261. TCustomCommandType Command;
  8262. // make sure we use local custom command when remote are not supported
  8263. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8264. {
  8265. Command = FLastCustomCommand;
  8266. }
  8267. else
  8268. {
  8269. Command.Params = Command.Params | ccLocal;
  8270. }
  8271. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8272. FEditingFocusedAdHocCommand = OnFocused;
  8273. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8274. int Options =
  8275. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8276. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8277. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8278. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8279. {
  8280. FLastCustomCommand = Command;
  8281. UpdateCustomCommandsToolbar();
  8282. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8283. }
  8284. }
  8285. //---------------------------------------------------------------------------
  8286. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8287. {
  8288. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8289. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8290. DebugAssert(State > 0);
  8291. if (State <= 0)
  8292. {
  8293. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (FLastCustomCommand.Command)));
  8294. }
  8295. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8296. }
  8297. //---------------------------------------------------------------------------
  8298. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8299. TCustomCommandType & Command, int & State)
  8300. {
  8301. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8302. if (Result)
  8303. {
  8304. Command = FLastCustomCommand;
  8305. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8306. }
  8307. return Result;
  8308. }
  8309. //---------------------------------------------------------------------------
  8310. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8311. {
  8312. ResetIncrementalSearch();
  8313. }
  8314. //---------------------------------------------------------------------------
  8315. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8316. {
  8317. DebugAssert(ComponentVisible[Component]);
  8318. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8319. }
  8320. //---------------------------------------------------------------------------
  8321. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8322. {
  8323. // when queue panel is resized here directly, the status bar is stretched
  8324. // over whole space the panel occupied
  8325. PostComponentHide(fcQueueView);
  8326. }
  8327. //---------------------------------------------------------------------------
  8328. void __fastcall TCustomScpExplorerForm::WMWinIniChange(TMessage & Message)
  8329. {
  8330. WinConfiguration->ResetSysDarkTheme();
  8331. ConfigurationChanged();
  8332. ConfigureInterface();
  8333. TForm::Dispatch(&Message);
  8334. }
  8335. //---------------------------------------------------------------------------
  8336. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8337. {
  8338. TMessage * M = static_cast<TMessage*>(Message);
  8339. switch (M->Msg)
  8340. {
  8341. case CM_DIALOGCHAR:
  8342. CMDialogChar(*M);
  8343. break;
  8344. case WM_APPCOMMAND:
  8345. WMAppCommand(*M);
  8346. break;
  8347. case WM_SYSCOMMAND:
  8348. WMSysCommand(*M);
  8349. break;
  8350. case WM_QUERYENDSESSION:
  8351. WMQueryEndSession(*M);
  8352. break;
  8353. case WM_ENDSESSION:
  8354. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8355. break;
  8356. #ifdef _DEBUG
  8357. case WM_WINDOWPOSCHANGED:
  8358. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8359. break;
  8360. case CM_CUSTOMSTYLECHANGED:
  8361. TForm::Dispatch(Message);
  8362. break;
  8363. #endif
  8364. case WM_COMPONENT_HIDE:
  8365. {
  8366. Byte Component = static_cast<Byte>(M->WParam);
  8367. // sanity check
  8368. if (ComponentVisible[Component])
  8369. {
  8370. // special treatment
  8371. if (Component == fcQueueView)
  8372. {
  8373. ToggleQueueVisibility();
  8374. DebugAssert(!ComponentVisible[fcQueueView]);
  8375. }
  8376. else
  8377. {
  8378. ComponentVisible[Component] = false;
  8379. }
  8380. }
  8381. }
  8382. break;
  8383. case WM_COPYDATA:
  8384. WMCopyData(*M);
  8385. break;
  8386. case WM_MANAGES_CAPTION:
  8387. // caption managed in TTerminalManager::UpdateAppTitle()
  8388. M->Result = 1;
  8389. break;
  8390. case WM_WANTS_MOUSEWHEEL:
  8391. M->Result = 1;
  8392. break;
  8393. case CM_SHOWINGCHANGED:
  8394. CMShowingChanged(*M);
  8395. break;
  8396. case WM_CLOSE:
  8397. WMClose(*M);
  8398. break;
  8399. case WM_DPICHANGED:
  8400. WMDpiChanged(*M);
  8401. break;
  8402. case CM_DPICHANGED:
  8403. CMDpiChanged(*M);
  8404. break;
  8405. case WM_WININICHANGE:
  8406. WMWinIniChange(*M);
  8407. break;
  8408. case CM_DIALOGKEY:
  8409. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8410. break;
  8411. default:
  8412. TForm::Dispatch(Message);
  8413. break;
  8414. }
  8415. }
  8416. //---------------------------------------------------------------------------
  8417. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8418. {
  8419. // noop
  8420. }
  8421. //---------------------------------------------------------------------------
  8422. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8423. {
  8424. TForm::Dispatch(&Message);
  8425. FSessionColors->Clear();
  8426. AddFixedSessionImages();
  8427. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8428. UpdateImages();
  8429. }
  8430. //---------------------------------------------------------------------------
  8431. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8432. {
  8433. TForm::Dispatch(&Message);
  8434. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8435. UpdatePixelsPerInchMainWindowCounter();
  8436. }
  8437. //---------------------------------------------------------------------------
  8438. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8439. {
  8440. // Cannot close window while we are busy.
  8441. // We cannot test this in FormCloseQuery as that is called also from
  8442. // Close(), which is called by CloseApplicationAction. So we can be busy
  8443. // there already even, when it is legitimate to close the application.
  8444. // Possibly a better place to handle this would be WMSysCommand.
  8445. if (NonVisualDataModule->Busy)
  8446. {
  8447. Message.Result = 1;
  8448. }
  8449. else
  8450. {
  8451. TForm::Dispatch(&Message);
  8452. }
  8453. }
  8454. //---------------------------------------------------------------------------
  8455. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8456. {
  8457. TForm::Dispatch(&Message);
  8458. if (Showing && (Terminal == NULL))
  8459. {
  8460. // When we are starting minimized (i.e. from an installer),
  8461. // postpone showing Login dialog until we get restored.
  8462. // Otherwise the Login dialog (and Authentication window) show restored
  8463. // over invisible (minimized) main window.
  8464. if (WindowState == wsMinimized)
  8465. {
  8466. FNeedSession = true;
  8467. }
  8468. else
  8469. {
  8470. // This happens before application ever goes idle, so the toolbars would
  8471. // stay enabled (initial state) until the Login dialog is dismissed.
  8472. UpdateActions();
  8473. NonVisualDataModule->StartBusy();
  8474. try
  8475. {
  8476. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8477. // otherwise the dialog does not receive focus.
  8478. // With Commander interface the ProcessMessages is called already
  8479. // by TDriveView, but with Explorer interface, we need to call it explicily
  8480. Application->ProcessMessages();
  8481. // do not reload sessions, they have been loaded just now (optimization)
  8482. NeedSession(true);
  8483. }
  8484. __finally
  8485. {
  8486. NonVisualDataModule->EndBusy();
  8487. }
  8488. }
  8489. }
  8490. }
  8491. //---------------------------------------------------------------------------
  8492. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  8493. {
  8494. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  8495. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  8496. }
  8497. //---------------------------------------------------------------------------
  8498. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8499. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8500. int & MaxHeight)
  8501. {
  8502. // workaround for bug in TWinControl.CalcConstraints
  8503. // Check for empty rect (restore from iconic state) is done there only after
  8504. // call to AdjustClientRect, which enlarges the rect (for forms).
  8505. TRect R = GetClientRect();
  8506. // when restoring from iconic state, place no restrictions
  8507. if (IsRectEmpty(R))
  8508. {
  8509. MinWidth = 0;
  8510. MinHeight = 0;
  8511. MaxWidth = 32000;
  8512. MaxHeight = 32000;
  8513. }
  8514. }
  8515. //---------------------------------------------------------------------------
  8516. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8517. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8518. {
  8519. // terminal can be already closed (e.g. dropped connection)
  8520. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8521. {
  8522. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8523. try
  8524. {
  8525. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8526. }
  8527. catch(...)
  8528. {
  8529. if (!Terminal->Active)
  8530. {
  8531. Close = true;
  8532. }
  8533. throw;
  8534. }
  8535. }
  8536. }
  8537. //---------------------------------------------------------------------------
  8538. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8539. {
  8540. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8541. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8542. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8543. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8544. {
  8545. OnGetSpaceAvailable = GetSpaceAvailable;
  8546. }
  8547. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8548. OnGetSpaceAvailable);
  8549. }
  8550. //---------------------------------------------------------------------------
  8551. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8552. {
  8553. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8554. Terminal->FillSessionDataForCode(Data.get());
  8555. return Data.release();
  8556. }
  8557. //---------------------------------------------------------------------------
  8558. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8559. {
  8560. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8561. DoGenerateUrlDialog(Data.get(), Paths);
  8562. }
  8563. //---------------------------------------------------------------------------
  8564. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8565. {
  8566. GenerateUrl(NULL);
  8567. }
  8568. //---------------------------------------------------------------------------
  8569. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8570. {
  8571. std::unique_ptr<TStrings> Paths(new TStringList());
  8572. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8573. GenerateUrl(Paths.get());
  8574. }
  8575. //---------------------------------------------------------------------------
  8576. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  8577. {
  8578. FSessionColor = value;
  8579. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8580. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8581. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8582. DebugAssert(ColorItem != NULL);
  8583. ColorItem->Color = C;
  8584. NonVisualDataModule->ColorMenuItem->Color = C;
  8585. UpdateControls();
  8586. }
  8587. //---------------------------------------------------------------------------
  8588. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8589. {
  8590. if (value != FSessionColor)
  8591. {
  8592. UpdateSessionColor(value);
  8593. // Is null when called from LastTerminalClosed
  8594. if (Terminal != NULL)
  8595. {
  8596. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8597. }
  8598. }
  8599. }
  8600. //---------------------------------------------------------------------------
  8601. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8602. {
  8603. bool Result = FNoteTimer->Enabled;
  8604. if (Result)
  8605. {
  8606. // cannot cancel note too early
  8607. bool NotEarly =
  8608. (Now() - FNoteShown >
  8609. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8610. if (Force || NotEarly)
  8611. {
  8612. FNoteTimer->Enabled = false;
  8613. FNote = L"";
  8614. SAFE_DESTROY(FNoteData);
  8615. FOnNoteClick = NULL;
  8616. FNoteHints = FNotes->Text;
  8617. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8618. UpdateStatusBar();
  8619. }
  8620. }
  8621. return Result;
  8622. }
  8623. //---------------------------------------------------------------------------
  8624. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8625. {
  8626. DebugAssert(FNoteTimer->Enabled);
  8627. CancelNote(true);
  8628. }
  8629. //---------------------------------------------------------------------------
  8630. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8631. {
  8632. int P = Note.Pos(L"\n");
  8633. if (P > 0)
  8634. {
  8635. Note.SetLength(P - 1);
  8636. }
  8637. FNotes->Add(FORMAT(L"[%s] %s",
  8638. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8639. while (FNotes->Count > 10)
  8640. {
  8641. FNotes->Delete(0);
  8642. }
  8643. if (UpdateNow)
  8644. {
  8645. FNoteHints = FNotes->Text;
  8646. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8647. UpdateStatusBar();
  8648. }
  8649. }
  8650. //---------------------------------------------------------------------------
  8651. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8652. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8653. {
  8654. int P = Note.Pos(L"\n");
  8655. if (P > 0)
  8656. {
  8657. Note.SetLength(P - 1);
  8658. }
  8659. FNoteHints = FNotes->Text;
  8660. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8661. FNote = Note;
  8662. SAFE_DESTROY(FNoteData);
  8663. FNoteData = NoteData;
  8664. FOnNoteClick = OnNoteClick;
  8665. AddNote(Note, false);
  8666. UpdateStatusBar();
  8667. FNoteShown = Now();
  8668. FNoteTimer->Enabled = false;
  8669. if (Seconds == 0)
  8670. {
  8671. Seconds = WinConfiguration->NotificationsTimeout;
  8672. }
  8673. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8674. FNoteTimer->Enabled = true;
  8675. }
  8676. //---------------------------------------------------------------------------
  8677. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8678. {
  8679. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8680. }
  8681. //---------------------------------------------------------------------------
  8682. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8683. {
  8684. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  8685. {
  8686. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8687. }
  8688. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  8689. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  8690. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  8691. 0, NULL, NULL);
  8692. }
  8693. //---------------------------------------------------------------------------
  8694. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  8695. {
  8696. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  8697. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  8698. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  8699. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  8700. }
  8701. //---------------------------------------------------------------------------
  8702. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  8703. {
  8704. WinConfiguration->FormatSizeBytes = Style;
  8705. }
  8706. //---------------------------------------------------------------------------
  8707. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  8708. {
  8709. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  8710. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  8711. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  8712. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  8713. }
  8714. //---------------------------------------------------------------------------
  8715. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  8716. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  8717. {
  8718. if (Panel->Index == 0)
  8719. {
  8720. if (FOnNoteClick != NULL)
  8721. {
  8722. // prevent the user data from being freed by possible call
  8723. // to CancelNote or PostNote during call to OnNoteClick
  8724. std::unique_ptr<TObject> NoteData(FNoteData);
  8725. TNotifyEvent OnNoteClick = FOnNoteClick;
  8726. FNoteData = NULL;
  8727. // need to cancel the note as we are going to delete its user data
  8728. CancelNote(true);
  8729. OnNoteClick(NoteData.get());
  8730. }
  8731. }
  8732. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  8733. {
  8734. FileSystemInfo();
  8735. }
  8736. }
  8737. //---------------------------------------------------------------------------
  8738. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  8739. {
  8740. // workaround:
  8741. // 1) for unknown reason, disabling window, while minimized,
  8742. // prevents it from restoring, even if it was enabled again meanwhile
  8743. // 2) when disabling the main window, while another has focus
  8744. // minimize is no longer possible
  8745. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  8746. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  8747. // Shouldn't we use IsApplicationMinimized() here?
  8748. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  8749. {
  8750. Enabled = false;
  8751. }
  8752. FLockLevel++;
  8753. }
  8754. //---------------------------------------------------------------------------
  8755. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  8756. {
  8757. DebugAssert(FLockLevel > 0);
  8758. FLockLevel--;
  8759. if (FLockLevel == 0)
  8760. {
  8761. DebugAssert(FLockSuspendLevel == 0);
  8762. Enabled = true;
  8763. }
  8764. }
  8765. //---------------------------------------------------------------------------
  8766. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  8767. {
  8768. // We need to make sure that window is enabled when the last modal window closes
  8769. // otherwise focus is not restored correctly.
  8770. // So we re-enable the window when modal window opens and
  8771. // disable it back after is closes
  8772. if (FLockLevel > 0)
  8773. {
  8774. // while we have nesting counter, we know that we never be called
  8775. // recursivelly as Application->OnModalBegin is called only
  8776. // for the top-level modal window
  8777. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8778. {
  8779. // won't be disabled when conditions in LockWindow() were not satisfied
  8780. FDisabledOnLockSuspend = !Enabled;
  8781. // When minimized to tray (or actually when set to SW_HIDE),
  8782. // setting Enabled makes the window focusable even when there's
  8783. // modal window over it
  8784. if (!FTrayIcon->Visible)
  8785. {
  8786. Enabled = true;
  8787. }
  8788. }
  8789. FLockSuspendLevel++;
  8790. }
  8791. }
  8792. //---------------------------------------------------------------------------
  8793. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  8794. {
  8795. if (FLockSuspendLevel > 0)
  8796. {
  8797. DebugAssert(FLockLevel > 0);
  8798. FLockSuspendLevel--;
  8799. // see comment in SuspendWindowLock
  8800. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8801. {
  8802. // Note that window can be enabled here, when we were minized to tray when
  8803. // was SuspendWindowLock() called.
  8804. // We should possibly do the same check as in LockWindow(),
  8805. // if it is ever possible that the consitions change between
  8806. // SuspendWindowLock() and ResumeWindowLock()
  8807. Enabled = !FDisabledOnLockSuspend;
  8808. }
  8809. }
  8810. }
  8811. //---------------------------------------------------------------------------
  8812. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  8813. {
  8814. if (!TextOnly)
  8815. {
  8816. TTBXComboBoxItem * RemotePathComboBox =
  8817. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  8818. TStrings * Items = RemotePathComboBox->Strings;
  8819. Items->BeginUpdate();
  8820. try
  8821. {
  8822. Items->Clear();
  8823. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  8824. {
  8825. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8826. while (!IsUnixRootPath(APath))
  8827. {
  8828. int P = APath.LastDelimiter(L'/');
  8829. DebugAssert(P >= 0);
  8830. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  8831. APath.SetLength(P - 1);
  8832. }
  8833. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  8834. }
  8835. }
  8836. __finally
  8837. {
  8838. RemotePathComboBox->ItemIndex = Items->Count - 1;
  8839. // Setting ItemIndex to -1 does not reset its text
  8840. if (Items->Count == 0)
  8841. {
  8842. RemotePathComboBox->Text = L"";
  8843. }
  8844. Items->EndUpdate();
  8845. }
  8846. }
  8847. }
  8848. //---------------------------------------------------------------------------
  8849. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  8850. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  8851. int & ImageIndex)
  8852. {
  8853. if (AIndex < 0)
  8854. {
  8855. AIndex = Sender->ItemIndex;
  8856. }
  8857. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  8858. }
  8859. //---------------------------------------------------------------------------
  8860. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  8861. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  8862. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  8863. {
  8864. ARect.Left += (10 * AIndex);
  8865. }
  8866. //---------------------------------------------------------------------------
  8867. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  8868. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  8869. {
  8870. AWidth += (10 * AIndex);
  8871. }
  8872. //---------------------------------------------------------------------------
  8873. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  8874. TObject * Sender)
  8875. {
  8876. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  8877. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8878. int Index = RemotePathComboBox->ItemIndex;
  8879. while (Index < RemotePathComboBox->Strings->Count - 1)
  8880. {
  8881. APath = UnixExtractFileDir(APath);
  8882. Index++;
  8883. }
  8884. // VanDyke style paths
  8885. if (APath.IsEmpty())
  8886. {
  8887. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  8888. APath = ROOTDIRECTORY;
  8889. }
  8890. if (RemoteDirView->Path != APath)
  8891. {
  8892. RemoteDirView->Path = APath;
  8893. }
  8894. }
  8895. //---------------------------------------------------------------------------
  8896. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  8897. {
  8898. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  8899. DebugUsedParam(Sender);
  8900. UpdateRemotePathComboBox(true);
  8901. }
  8902. //---------------------------------------------------------------------------
  8903. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  8904. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  8905. {
  8906. ResetIncrementalSearch();
  8907. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8908. DebugAssert(DirView != NULL);
  8909. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  8910. {
  8911. HWND Edit = ListView_GetEditControl(DirView->Handle);
  8912. // OnEditing is called also from TCustomListView::CanEdit
  8913. if (Edit != NULL)
  8914. {
  8915. EditSelectBaseName(Edit);
  8916. }
  8917. }
  8918. }
  8919. //---------------------------------------------------------------------------
  8920. //---------------------------------------------------------------------------
  8921. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  8922. {
  8923. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  8924. Result->AutoDetectDnD = false;
  8925. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  8926. Result->RenderDataOn = rdoEnterAndDropSync;
  8927. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  8928. return Result;
  8929. }
  8930. //---------------------------------------------------------------------------
  8931. void __fastcall TCustomScpExplorerForm::CreateWnd()
  8932. {
  8933. TForm::CreateWnd();
  8934. if (FSessionsDragDropFilesEx == NULL)
  8935. {
  8936. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  8937. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  8938. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  8939. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  8940. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  8941. }
  8942. if (FQueueDragDropFilesEx == NULL)
  8943. {
  8944. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  8945. // No need to set OnDragOver as we do not have any restrictions
  8946. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  8947. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  8948. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  8949. }
  8950. }
  8951. //---------------------------------------------------------------------------
  8952. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  8953. {
  8954. TForm::DestroyWnd();
  8955. FSessionsDragDropFilesEx->DragDropControl = NULL;
  8956. FQueueDragDropFilesEx->DragDropControl = NULL;
  8957. }
  8958. //---------------------------------------------------------------------------
  8959. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  8960. {
  8961. SideEnter(FCurrentSide);
  8962. FEverShown = true;
  8963. }
  8964. //---------------------------------------------------------------------------
  8965. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  8966. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  8967. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  8968. {
  8969. if (!NonVisualDataModule->Busy)
  8970. {
  8971. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  8972. Configuration->Usage->Inc(L"FileFinds");
  8973. LockWindow(true);
  8974. NonVisualDataModule->StartBusy();
  8975. WinConfiguration->LockedInterface = true;
  8976. try
  8977. {
  8978. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  8979. }
  8980. __finally
  8981. {
  8982. WinConfiguration->LockedInterface = false;
  8983. NonVisualDataModule->EndBusy();
  8984. UnlockWindow();
  8985. }
  8986. }
  8987. }
  8988. //---------------------------------------------------------------------------
  8989. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  8990. {
  8991. if (!NonVisualDataModule->Busy)
  8992. {
  8993. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  8994. SetFocus();
  8995. RemoteDirView->Path = UnixExtractFilePath(Path);
  8996. UnicodeString FileName = UnixExtractFileName(Path);
  8997. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  8998. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  8999. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9000. if (Item == NULL)
  9001. {
  9002. ReloadDirectory(osRemote);
  9003. Item = RemoteDirView->FindFileItem(FileName);
  9004. }
  9005. if (Item != NULL)
  9006. {
  9007. RemoteDirView->ItemFocused = Item;
  9008. RemoteDirView->ItemFocused->MakeVisible(false);
  9009. RemoteDirView->SetFocus();
  9010. }
  9011. }
  9012. }
  9013. //---------------------------------------------------------------------------
  9014. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9015. {
  9016. bool Result = !NonVisualDataModule->Busy;
  9017. if (Result)
  9018. {
  9019. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9020. }
  9021. return Result;
  9022. }
  9023. //---------------------------------------------------------------------------
  9024. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9025. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9026. {
  9027. if (CanOperateOnFoundFiles(ATerminal))
  9028. {
  9029. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9030. FOnFileOperationFinished = OnFileOperationFinished;
  9031. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9032. }
  9033. }
  9034. //---------------------------------------------------------------------------
  9035. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9036. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9037. {
  9038. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9039. }
  9040. //---------------------------------------------------------------------------
  9041. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9042. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9043. {
  9044. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9045. }
  9046. //---------------------------------------------------------------------------
  9047. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9048. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9049. {
  9050. if (CanOperateOnFoundFiles(ATerminal))
  9051. {
  9052. for (int Index = 0; Index < FileList->Count; Index++)
  9053. {
  9054. UnicodeString FileName = FileList->Strings[Index];
  9055. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9056. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9057. OnFileOperationFinished(FileName, true);
  9058. }
  9059. }
  9060. }
  9061. //---------------------------------------------------------------------------
  9062. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9063. {
  9064. FFileFindTerminal = Terminal;
  9065. ShowFileFindDialog(
  9066. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9067. DoDownloadFoundFiles, DoEditFoundFiles);
  9068. }
  9069. //---------------------------------------------------------------------------
  9070. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9071. {
  9072. FTaskbarList = TaskbarList;
  9073. }
  9074. //---------------------------------------------------------------------------
  9075. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9076. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9077. {
  9078. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9079. if (Index >= 0)
  9080. {
  9081. if (Button == mbLeft)
  9082. {
  9083. // "Mouse down" is raised only after tab is switched.
  9084. // If switching tab (switching session) takes long enough for user
  9085. // to actually release the button, "mouse down" is still raised,
  9086. // but we do not get "mouse up" event, so dragging is not cancelled,
  9087. // prevent that by not beginning dragging in the first place.
  9088. if (IsKeyPressed(VK_LBUTTON))
  9089. {
  9090. // when user clicks the "+", we get mouse down only after the session
  9091. // is closed, when new session tab is already on X:Y, so dragging
  9092. // starts, prevent that
  9093. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  9094. {
  9095. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9096. if (Terminal != NULL)
  9097. {
  9098. SessionsPageControl->BeginDrag(false);
  9099. }
  9100. }
  9101. }
  9102. }
  9103. else if (Button == mbMiddle)
  9104. {
  9105. // ignore middle-click for "New session tab"
  9106. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9107. if (Terminal != NULL)
  9108. {
  9109. CloseSessionTab(Index);
  9110. }
  9111. }
  9112. }
  9113. }
  9114. //---------------------------------------------------------------------------
  9115. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9116. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9117. {
  9118. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9119. // do not allow dropping on the "+" tab
  9120. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9121. if ((TargetTerminal != NULL) &&
  9122. (SessionsPageControl->ActivePage->PageIndex != Index))
  9123. {
  9124. Configuration->Usage->Inc(L"SessionTabMoves");
  9125. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  9126. // but we want to actually prevent that to avoid flicker
  9127. SessionsPageControl->ActivePage->PageIndex = Index;
  9128. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  9129. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9130. }
  9131. }
  9132. //---------------------------------------------------------------------------
  9133. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9134. TObject * Sender, TObject * Source, int X, int Y,
  9135. TDragState /*State*/, bool & Accept)
  9136. {
  9137. Accept = (Sender == Source);
  9138. if (Accept)
  9139. {
  9140. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9141. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9142. // do not allow dragging to the "+" tab
  9143. Accept = (Terminal != NULL);
  9144. }
  9145. }
  9146. //---------------------------------------------------------------------------
  9147. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(int /*KeyState*/,
  9148. const TPoint & Point, int & Effect)
  9149. {
  9150. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9151. if (Index < 0)
  9152. {
  9153. Effect = DROPEFFECT_NONE;
  9154. }
  9155. else
  9156. {
  9157. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9158. // do not allow dropping on the "+" tab
  9159. if (TargetTerminal == NULL)
  9160. {
  9161. Effect = DROPEFFECT_NONE;
  9162. }
  9163. else if ((TargetTerminal != Terminal) && (Effect == DROPEFFECT_MOVE))
  9164. {
  9165. Effect = DROPEFFECT_COPY;
  9166. }
  9167. }
  9168. }
  9169. //---------------------------------------------------------------------------
  9170. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9171. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9172. {
  9173. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9174. // do not allow dropping on the "+" tab
  9175. TManagedTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9176. if (TargetTerminal != NULL)
  9177. {
  9178. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9179. if (!IsFileControl(DropSourceControl, osRemote))
  9180. {
  9181. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  9182. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9183. // Why don't we use Terminal->CurrentDirectory directly?
  9184. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9185. // do not force queue, drag drop
  9186. false, true);
  9187. }
  9188. }
  9189. }
  9190. //---------------------------------------------------------------------------
  9191. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9192. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9193. {
  9194. // Downloads are handled in RemoteFileControlDDEnd
  9195. if (!IsFileControl(DropSourceControl, osRemote))
  9196. {
  9197. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9198. Terminal->CurrentDirectory,
  9199. // force queue, drag drop
  9200. true, true);
  9201. }
  9202. }
  9203. //---------------------------------------------------------------------------
  9204. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9205. {
  9206. FShowing = false;
  9207. // Do not save empty workspace
  9208. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  9209. {
  9210. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9211. UnicodeString Name = WorkspaceName();
  9212. DoSaveWorkspace(
  9213. Name, DataList.get(),
  9214. !Configuration->DisablePasswordStoring &&
  9215. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9216. WinConfiguration->LastStoredSession = Name;
  9217. }
  9218. }
  9219. //---------------------------------------------------------------------------
  9220. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9221. {
  9222. TManagedTerminal * ManagedTerminal =
  9223. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9224. if (ManagedTerminal != NULL)
  9225. {
  9226. ManagedTerminal->DirectoryLoaded = Now();
  9227. }
  9228. }
  9229. //---------------------------------------------------------------------------
  9230. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9231. TListItem * /*Item*/, bool /*Selected*/)
  9232. {
  9233. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9234. switch (DirView->LastSelectMethod)
  9235. {
  9236. case smKeyboard:
  9237. Configuration->Usage->Inc(L"KeyboardSelections");
  9238. break;
  9239. case smMouse:
  9240. Configuration->Usage->Inc(L"MouseSelections");
  9241. break;
  9242. }
  9243. }
  9244. //---------------------------------------------------------------------------
  9245. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9246. {
  9247. if (Color != 0)
  9248. {
  9249. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9250. }
  9251. else
  9252. {
  9253. return FSessionTabImageIndex;
  9254. }
  9255. }
  9256. //---------------------------------------------------------------------------
  9257. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9258. {
  9259. TPngImageCollectionItem * Item =
  9260. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9261. FSessionColors->AddPng(Item->PngImage);
  9262. return FSessionColors->Count - 1;
  9263. }
  9264. //---------------------------------------------------------------------------
  9265. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9266. {
  9267. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9268. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  9269. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9270. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9271. }
  9272. //---------------------------------------------------------------------------
  9273. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9274. {
  9275. for (int Index = 0; Index < Control->ControlCount; Index++)
  9276. {
  9277. TControl * AControl = Control->Controls[Index];
  9278. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9279. if (WinControl != NULL)
  9280. {
  9281. CollectItemsWithTextDisplayMode(WinControl);
  9282. }
  9283. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9284. if (Toolbar != NULL)
  9285. {
  9286. // we care for top-level items only
  9287. TTBCustomItem * Items = Toolbar->Items;
  9288. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9289. {
  9290. TTBCustomItem * Item = Items->Items[ItemIndex];
  9291. if (((Item->DisplayMode == nbdmImageAndText) ||
  9292. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9293. EligibleForImageDisplayMode(Item))
  9294. {
  9295. FItemsWithTextDisplayMode.insert(Item);
  9296. }
  9297. }
  9298. }
  9299. }
  9300. }
  9301. //---------------------------------------------------------------------------
  9302. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9303. {
  9304. return true;
  9305. }
  9306. //---------------------------------------------------------------------------
  9307. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9308. {
  9309. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9310. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9311. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9312. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9313. bool Result = true;
  9314. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9315. {
  9316. TTBCustomItem * Item = *i;
  9317. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9318. if (Label != NULL)
  9319. {
  9320. // optimization
  9321. if (Label->Visible == SelectiveToolbarText)
  9322. {
  9323. Result = false;
  9324. }
  9325. else
  9326. {
  9327. Label->Visible = SelectiveToolbarText;
  9328. }
  9329. }
  9330. else
  9331. {
  9332. // optimization
  9333. if (Item->DisplayMode == DisplayMode)
  9334. {
  9335. Result = false;
  9336. }
  9337. else
  9338. {
  9339. Item->DisplayMode = DisplayMode;
  9340. }
  9341. }
  9342. ++i;
  9343. }
  9344. if (Result)
  9345. {
  9346. UpdateNewSessionTab();
  9347. }
  9348. return Result;
  9349. }
  9350. //---------------------------------------------------------------------------
  9351. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9352. {
  9353. DebugFail();
  9354. }
  9355. //---------------------------------------------------------------------------
  9356. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9357. {
  9358. // Among other, a lock level is non zero, while directory is being loaded,
  9359. // even when it happens as a result of dirview navigation,
  9360. // i.e. when TNonVisualDataModule is NOT FBusy.
  9361. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9362. // Among other this prevents a panel auto update to occur while
  9363. // directory is changing.
  9364. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9365. }
  9366. //---------------------------------------------------------------------------
  9367. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  9368. {
  9369. // While the window is disabled, we still seem to process menu shortcuts at least,
  9370. // so stop it at least here.
  9371. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9372. return
  9373. (Allowed == aaUpdate) ||
  9374. !NonVisualDataModule->Busy;
  9375. }
  9376. //---------------------------------------------------------------------------
  9377. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9378. {
  9379. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9380. }
  9381. //---------------------------------------------------------------------------
  9382. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9383. {
  9384. // This is somewhat redundant to LockWindow() call from
  9385. // TTerminalManager::TerminalReadDirectoryProgress.
  9386. // But disabling window is known not to block keyboard shorcuts
  9387. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9388. if (Busy > 0)
  9389. {
  9390. if (NonVisualDataModule->Busy)
  9391. {
  9392. State = false;
  9393. }
  9394. else
  9395. {
  9396. NonVisualDataModule->StartBusy();
  9397. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9398. LockWindow();
  9399. }
  9400. }
  9401. else if (Busy < 0)
  9402. {
  9403. UnlockWindow();
  9404. NonVisualDataModule->EndBusy();
  9405. }
  9406. else
  9407. {
  9408. State = NonVisualDataModule->Busy;
  9409. }
  9410. }
  9411. //---------------------------------------------------------------------------
  9412. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9413. {
  9414. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9415. // no context menu for "New session tab"
  9416. if ((Index >= 0) && (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != NULL))
  9417. {
  9418. SessionsPageControl->ActivePageIndex = Index;
  9419. if (DebugAlwaysTrue(SessionTabSwitched()))
  9420. {
  9421. // copied from TControl.WMContextMenu
  9422. SendCancelMode(SessionsPageControl);
  9423. // explicit popup instead of using PopupMenu property
  9424. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9425. // while connecting yet not-connected session and hence
  9426. // allowing an access to commands over not-completelly connected session
  9427. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9428. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  9429. PopupMenu->PopupComponent = SessionsPageControl;
  9430. PopupMenu->Popup(Point.x, Point.y);
  9431. }
  9432. }
  9433. Handled = true;
  9434. }
  9435. //---------------------------------------------------------------------------
  9436. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9437. {
  9438. return (Terminal != NULL) && Terminal->Active && Terminal->IsCapable[fcChangePassword];
  9439. }
  9440. //---------------------------------------------------------------------------
  9441. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9442. {
  9443. Configuration->Usage->Inc(L"PasswordChanges");
  9444. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9445. ChangePasswordData->DisableAuthentationsExceptPassword();
  9446. ChangePasswordData->ChangePassword = true;
  9447. std::unique_ptr<TTerminal> ChangePasswordSession(
  9448. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9449. ChangePasswordSession->AutoReadDirectory = false;
  9450. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9451. {
  9452. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9453. }
  9454. else
  9455. {
  9456. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9457. }
  9458. }
  9459. //---------------------------------------------------------------------------
  9460. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9461. {
  9462. // No nice way to assert SSH2
  9463. return (Terminal != NULL) && Terminal->Active && (Terminal->FSProtocol == cfsSFTP);
  9464. }
  9465. //---------------------------------------------------------------------------
  9466. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9467. {
  9468. TOperationVisualizer OperationVisualizer;
  9469. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9470. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9471. }
  9472. //---------------------------------------------------------------------------
  9473. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9474. {
  9475. TForm::ChangeScale(M, D);
  9476. int APixelsPerInch = GetControlPixelsPerInch(this);
  9477. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9478. try
  9479. {
  9480. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9481. }
  9482. __finally
  9483. {
  9484. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9485. }
  9486. }
  9487. //---------------------------------------------------------------------------
  9488. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9489. {
  9490. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9491. }
  9492. //---------------------------------------------------------------------
  9493. class TPublicControl : public TControl
  9494. {
  9495. friend class TCustomScpExplorerForm;
  9496. };
  9497. //---------------------------------------------------------------------------
  9498. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9499. {
  9500. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9501. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9502. }
  9503. //---------------------------------------------------------------------------
  9504. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9505. {
  9506. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9507. OpenBookmark(Side, Bookmark);
  9508. }
  9509. //---------------------------------------------------------------------------
  9510. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9511. {
  9512. DoBookmarkClick(osLocal, Sender);
  9513. }
  9514. //---------------------------------------------------------------------------
  9515. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9516. {
  9517. DoBookmarkClick(osRemote, Sender);
  9518. }
  9519. //---------------------------------------------------------------------------
  9520. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9521. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9522. {
  9523. // TODO
  9524. if (BookmarkList != NULL)
  9525. {
  9526. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9527. if (!WinConfiguration->UseLocationProfiles)
  9528. {
  9529. std::unique_ptr<TStringList> Directories(new TStringList());
  9530. Directories->CaseSensitive = (Side == osRemote);
  9531. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9532. {
  9533. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9534. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9535. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9536. {
  9537. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9538. Item->Caption = EscapeHotkey(Directory);
  9539. Item->ShortCut = Bookmark->ShortCut;
  9540. Item->OnClick = OnBookmarkClick;
  9541. Item->Tag = reinterpret_cast<int>(Bookmark);
  9542. Directories->Add(Directory);
  9543. Menu->Add(Item.release());
  9544. }
  9545. }
  9546. }
  9547. else
  9548. {
  9549. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9550. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9551. {
  9552. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9553. if (!Bookmark->Node.IsEmpty())
  9554. {
  9555. Folders->Add(Bookmark->Node);
  9556. }
  9557. }
  9558. for (int Index = 0; Index < Folders->Count; Index++)
  9559. {
  9560. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9561. Item->Caption = Folders->Strings[Index];
  9562. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction->ImageIndex;
  9563. Folders->Objects[Index] = Item.get();
  9564. Menu->Add(Item.release());
  9565. }
  9566. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9567. {
  9568. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9569. TTBCustomItem * Parent;
  9570. if (!Bookmark->Node.IsEmpty())
  9571. {
  9572. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9573. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9574. }
  9575. else
  9576. {
  9577. Parent = Menu;
  9578. }
  9579. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9580. Item->Caption = Bookmark->Name;
  9581. Item->ShortCut = Bookmark->ShortCut;
  9582. Item->OnClick = OnBookmarkClick;
  9583. Item->Tag = reinterpret_cast<int>(Bookmark);
  9584. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9585. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9586. {
  9587. // No hint for location profiles that are actually mere "bookmarks"
  9588. }
  9589. else
  9590. {
  9591. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  9592. UnicodeString LongHint;
  9593. if (!Bookmark->Local.IsEmpty())
  9594. {
  9595. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  9596. }
  9597. if (!Bookmark->Remote.IsEmpty())
  9598. {
  9599. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  9600. }
  9601. AddToList(Hint, LongHint, L"|");
  9602. Item->Hint = Hint;
  9603. }
  9604. Parent->Add(Item.release());
  9605. }
  9606. }
  9607. AddMenuSeparator(Menu);
  9608. }
  9609. }
  9610. //---------------------------------------------------------------------------
  9611. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  9612. {
  9613. Menu->Clear();
  9614. std::unique_ptr<TTBCustomItem> Item;
  9615. Item.reset(new TTBXItem(Owner));
  9616. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  9617. Item->Options = Item->Options << tboDefault;
  9618. Menu->Add(Item.release());
  9619. AddMenuSeparator(Menu);
  9620. if (Terminal != NULL)
  9621. {
  9622. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  9623. }
  9624. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  9625. Item.reset(new TTBXItem(Owner));
  9626. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction : NonVisualDataModule->RemoteAddBookmarkAction;
  9627. Menu->Add(Item.release());
  9628. }
  9629. //---------------------------------------------------------------------------
  9630. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  9631. {
  9632. bool Result = true;
  9633. try
  9634. {
  9635. if (Side == osRemote)
  9636. {
  9637. Terminal->ExceptionOnFail = true;
  9638. }
  9639. try
  9640. {
  9641. DirView(Side)->Path = Path;
  9642. }
  9643. __finally
  9644. {
  9645. if (Side == osRemote)
  9646. {
  9647. Terminal->ExceptionOnFail = false;
  9648. }
  9649. }
  9650. }
  9651. catch (Exception & E)
  9652. {
  9653. if ((Side != osRemote) || Terminal->Active)
  9654. {
  9655. ShowExtendedExceptionEx(Terminal, &E);
  9656. Result = false;
  9657. }
  9658. else
  9659. {
  9660. throw;
  9661. }
  9662. }
  9663. return Result;
  9664. }
  9665. //---------------------------------------------------------------------------
  9666. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  9667. {
  9668. // Make sure there some feedback even when loading tiny local folders
  9669. TInstantOperationVisualizer Visualizer;
  9670. DirView(Side)->ReloadDirectory();
  9671. }
  9672. //---------------------------------------------------------------------------
  9673. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  9674. {
  9675. if (Index == SessionsPageControl->TabIndex)
  9676. {
  9677. CloseSession();
  9678. }
  9679. else
  9680. {
  9681. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  9682. TTerminal * Terminal = GetSessionTabTerminal(TabSheet);
  9683. TTerminalManager * Manager = TTerminalManager::Instance();
  9684. if (!Terminal->Active)
  9685. {
  9686. Manager->FreeTerminal(Terminal);
  9687. }
  9688. else
  9689. {
  9690. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  9691. if (CanCloseQueue(Queue))
  9692. {
  9693. Manager->FreeTerminal(Terminal);
  9694. }
  9695. }
  9696. }
  9697. }
  9698. //---------------------------------------------------------------------------
  9699. void __fastcall TCustomScpExplorerForm::SessionsPageControlCloseButtonClick(TPageControl * /*Sender*/, int Index)
  9700. {
  9701. CloseSessionTab(Index);
  9702. }
  9703. //---------------------------------------------------------------------------
  9704. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  9705. {
  9706. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  9707. {
  9708. TInstantOperationVisualizer Visualizer;
  9709. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  9710. // before we create a new data object
  9711. ClipboardClear();
  9712. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  9713. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  9714. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  9715. // Intermediate variable to WORKAROUND a crash in compiler
  9716. TObjectList * ClipboardFakeMonitors =
  9717. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  9718. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  9719. if (FClipboardDragDropFilesEx.get() == NULL)
  9720. {
  9721. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  9722. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  9723. }
  9724. FClipboardDragDropFilesEx->FileList->Clear();
  9725. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  9726. FClipboardDragDropFilesEx->CopyToClipboard();
  9727. FClipboardTerminal = FTerminal;
  9728. // Need full paths, as cwd can be different once files are pasted
  9729. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  9730. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  9731. {
  9732. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  9733. }
  9734. }
  9735. }
  9736. //---------------------------------------------------------------------------
  9737. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  9738. {
  9739. if (OpenClipboard(0))
  9740. {
  9741. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  9742. CloseClipboard();
  9743. }
  9744. }
  9745. //---------------------------------------------------------------------------
  9746. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  9747. {
  9748. return !FClipboardFakeDirectory.IsEmpty();
  9749. }
  9750. //---------------------------------------------------------------------------
  9751. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  9752. {
  9753. RemoveDir(ApiPath(FClipboardFakeDirectory));
  9754. FClipboardFakeDirectory = UnicodeString();
  9755. FClipboardTerminal = NULL;
  9756. // Also called in Idle()
  9757. FClipboardFakeMonitors.reset(NULL);
  9758. }
  9759. //---------------------------------------------------------------------------
  9760. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  9761. {
  9762. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  9763. {
  9764. ClipboardStop();
  9765. }
  9766. }
  9767. //---------------------------------------------------------------------------
  9768. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  9769. {
  9770. TTerminalManager * Manager = TTerminalManager::Instance();
  9771. Manager->ActiveTerminal = FClipboardTerminal;
  9772. TTransferOperationParam Params;
  9773. Params.TargetDirectory = TargetDirectory;
  9774. Params.DragDrop = DragDrop;
  9775. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  9776. }
  9777. //---------------------------------------------------------------------------
  9778. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  9779. {
  9780. // It can actually rarelly happen that some random file is created, while we are shuttting down the monitor
  9781. // (as it pumps a Windows message queue while being shutted down)
  9782. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  9783. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  9784. {
  9785. // Can fail as it can be e.g. locked by AV, so we retry that later
  9786. bool Removed = RemoveDir(ApiPath(FileName));
  9787. try
  9788. {
  9789. if (!NonVisualDataModule->Busy)
  9790. {
  9791. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  9792. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  9793. }
  9794. }
  9795. __finally
  9796. {
  9797. if (!Removed)
  9798. {
  9799. RemoveDir(ApiPath(FileName));
  9800. }
  9801. }
  9802. }
  9803. }
  9804. //---------------------------------------------------------------------------
  9805. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  9806. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  9807. {
  9808. TFileMasks::TParams MaskParams;
  9809. MaskParams.Size = Size;
  9810. MaskParams.Modification = Modification;
  9811. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  9812. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  9813. {
  9814. bool ImplicitMatch;
  9815. if (Iter->FileMask.Matches(FileName, Directory, DirView->PathName, &MaskParams, false, ImplicitMatch) &&
  9816. !ImplicitMatch)
  9817. {
  9818. Color = Iter->Color;
  9819. break;
  9820. }
  9821. }
  9822. }
  9823. //---------------------------------------------------------------------------
  9824. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  9825. {
  9826. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9827. if (!DirView->IsEditing())
  9828. {
  9829. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  9830. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  9831. // while searching
  9832. if (Key == VK_BACK)
  9833. {
  9834. if (!FIncrementalSearch.IsEmpty())
  9835. {
  9836. if (FIncrementalSearch.Length() == 1)
  9837. {
  9838. ResetIncrementalSearch();
  9839. }
  9840. else
  9841. {
  9842. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  9843. IncrementalSearch(NewText, false, false);
  9844. }
  9845. Key = 0;
  9846. }
  9847. }
  9848. }
  9849. }
  9850. //---------------------------------------------------------------------------
  9851. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  9852. {
  9853. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9854. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  9855. {
  9856. // Filter control sequences.
  9857. // When not searching yet, prefer use of the space for toggling file selection
  9858. // (so we cannot incrementally search for a string starting with a space).
  9859. if ((Key > VK_SPACE) ||
  9860. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  9861. {
  9862. IncrementalSearch(FIncrementalSearch + Key, false, false);
  9863. Key = 0;
  9864. }
  9865. }
  9866. }
  9867. //---------------------------------------------------------------------------
  9868. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  9869. {
  9870. if (!FIncrementalSearch.IsEmpty())
  9871. {
  9872. FIncrementalSearch = L"";
  9873. DirView(osCurrent)->UpdateStatusBar();
  9874. }
  9875. }
  9876. //---------------------------------------------------------------------------
  9877. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9878. {
  9879. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  9880. if (Item == NULL)
  9881. {
  9882. MessageBeep(MB_ICONHAND);
  9883. }
  9884. else
  9885. {
  9886. TCustomDirView * ADirView = DirView(osCurrent);
  9887. {
  9888. TAutoNestingCounter Guard(FIncrementalSearching);
  9889. ADirView->FocusItem(Item);
  9890. if (ADirView->NortonLike == nlOff)
  9891. {
  9892. if ((ADirView->Selected != Item) ||
  9893. (ADirView->SelCount > 1))
  9894. {
  9895. ADirView->ClearSelection();
  9896. ADirView->Selected = Item;
  9897. }
  9898. }
  9899. }
  9900. FIncrementalSearch = Text;
  9901. Item->MakeVisible(false);
  9902. TListItem * NextItem = SearchFile(Text, true, Reverse);
  9903. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  9904. ADirView->UpdateStatusBar();
  9905. }
  9906. }
  9907. //---------------------------------------------------------------------------
  9908. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  9909. {
  9910. int Index = Item->Index;
  9911. if (!Reverse)
  9912. {
  9913. Index++;
  9914. if (Index >= Item->Owner->Count)
  9915. {
  9916. Index = 0;
  9917. }
  9918. }
  9919. else
  9920. {
  9921. Index--;
  9922. if (Index < 0)
  9923. {
  9924. Index = Item->Owner->Count - 1;
  9925. }
  9926. }
  9927. return Item->Owner->Item[Index];
  9928. }
  9929. //---------------------------------------------------------------------------
  9930. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9931. {
  9932. TCustomDirView * ADirView = DirView(osCurrent);
  9933. if (ADirView->Items->Count == 0)
  9934. {
  9935. return NULL;
  9936. }
  9937. else
  9938. {
  9939. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  9940. TListItem * Item = CurrentItem;
  9941. if (SkipCurrent)
  9942. {
  9943. Item = DebugNotNull(GetNextFile(Item, Reverse));
  9944. }
  9945. while (true)
  9946. {
  9947. bool Matches = false;
  9948. switch (WinConfiguration->PanelSearch)
  9949. {
  9950. case isNameStartOnly:
  9951. Matches = ContainsTextSemiCaseSensitive(Item->Caption.SubString(1, Text.Length()), Text);
  9952. break;
  9953. case isName:
  9954. Matches = ContainsTextSemiCaseSensitive(Item->Caption, Text);
  9955. break;
  9956. case isAll:
  9957. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  9958. int Index = 0;
  9959. while ((Index < ColCount) && !Matches)
  9960. {
  9961. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  9962. if (Visible)
  9963. {
  9964. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  9965. }
  9966. Index++;
  9967. }
  9968. break;
  9969. }
  9970. if (Matches)
  9971. {
  9972. return Item;
  9973. }
  9974. Item = GetNextFile(Item, Reverse);
  9975. if (Item == CurrentItem)
  9976. {
  9977. return NULL;
  9978. }
  9979. }
  9980. }
  9981. }
  9982. //---------------------------------------------------------------------------
  9983. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  9984. {
  9985. ResetIncrementalSearch();
  9986. }
  9987. //---------------------------------------------------------------------------
  9988. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  9989. {
  9990. if (Message.CharCode == VK_TAB)
  9991. {
  9992. if (!FIncrementalSearch.IsEmpty())
  9993. {
  9994. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  9995. bool Reverse = Shift.Contains(ssShift);
  9996. IncrementalSearch(FIncrementalSearch, true, Reverse);
  9997. Message.Result = 1;
  9998. return;
  9999. }
  10000. }
  10001. else if (Message.CharCode == VK_ESCAPE)
  10002. {
  10003. if (!FIncrementalSearch.IsEmpty())
  10004. {
  10005. ResetIncrementalSearch();
  10006. Message.Result = 1;
  10007. return;
  10008. }
  10009. }
  10010. TForm::Dispatch(&Message);
  10011. }
  10012. //---------------------------------------------------------------------------
  10013. void __fastcall TCustomScpExplorerForm::Deactivate()
  10014. {
  10015. TForm::Deactivate();
  10016. // E.g. when switching to an internal editor window
  10017. ResetIncrementalSearch();
  10018. }
  10019. //---------------------------------------------------------------------------
  10020. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10021. {
  10022. // When switching to another application
  10023. ResetIncrementalSearch();
  10024. }
  10025. //---------------------------------------------------------------------------
  10026. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10027. {
  10028. ResetIncrementalSearch();
  10029. }
  10030. //---------------------------------------------------------------------------
  10031. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10032. {
  10033. if (FIncrementalSearching <= 0)
  10034. {
  10035. ResetIncrementalSearch();
  10036. }
  10037. }
  10038. //---------------------------------------------------------------------------
  10039. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10040. {
  10041. CountClicksForWindowPrint(this);
  10042. }
  10043. //---------------------------------------------------------------------------
  10044. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10045. {
  10046. CenterReconnectToolbar();
  10047. }
  10048. //---------------------------------------------------------------------------
  10049. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10050. {
  10051. if (RemoteDirView->ItemFocused != NULL)
  10052. {
  10053. RemoteDirView->ItemFocused->Selected = true;
  10054. }
  10055. }
  10056. //---------------------------------------------------------------------------