CustomScpExplorer.cpp 364 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065
  1. //---------------------------------------------------------------------------
  2. #define NO_WIN32_LEAN_AND_MEAN
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include <Common.h>
  6. #include "CustomScpExplorer.h"
  7. #include <Bookmarks.h>
  8. #include <Interface.h>
  9. #include <Exceptions.h>
  10. #include <CoreMain.h>
  11. #include <FileSystems.h>
  12. #include <TextsCore.h>
  13. #include <TextsWin.h>
  14. #include <HelpWin.h>
  15. #include <VCLCommon.h>
  16. #include <Progress.h>
  17. #include <SynchronizeProgress.h>
  18. #include <DragExt.h>
  19. #include <WinApi.h>
  20. #include "GUITools.h"
  21. #include "NonVisual.h"
  22. #include "Glyphs.h"
  23. #include "Tools.h"
  24. #include "WinConfiguration.h"
  25. #include "TerminalManager.h"
  26. #include "EditorManager.h"
  27. #include "ProgParams.h"
  28. #include "Setup.h"
  29. #include <Consts.hpp>
  30. #include <DateUtils.hpp>
  31. #include <TB2Common.hpp>
  32. #include <DirectoryMonitor.hpp>
  33. #include <System.IOUtils.hpp>
  34. //---------------------------------------------------------------------------
  35. #pragma package(smart_init)
  36. #pragma link "CustomDirView"
  37. #pragma link "CustomUnixDirView"
  38. #pragma link "IEListView"
  39. #pragma link "NortonLikeListView"
  40. #pragma link "UnixDirView"
  41. #pragma link "CustomDriveView"
  42. #pragma link "UnixDriveView"
  43. #pragma link "CustomDriveView"
  44. #pragma link "TB2Dock"
  45. #pragma link "TBXStatusBars"
  46. #pragma link "TB2Item"
  47. #pragma link "TB2Toolbar"
  48. #pragma link "ThemePageControl"
  49. #pragma link "PathLabel"
  50. #pragma resource "*.dfm"
  51. //---------------------------------------------------------------------------
  52. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  53. static const int SessionPanelCount = 4;
  54. //---------------------------------------------------------------------------
  55. class TMutexGuard
  56. {
  57. public:
  58. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  59. int Timeout = 5000)
  60. {
  61. FMutex = NULL;
  62. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  63. if (WaitResult == WAIT_TIMEOUT)
  64. {
  65. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  66. }
  67. else
  68. {
  69. FMutex = AMutex;
  70. }
  71. }
  72. ~TMutexGuard()
  73. {
  74. if (FMutex != NULL)
  75. {
  76. ReleaseMutex(FMutex);
  77. }
  78. }
  79. private:
  80. HANDLE FMutex;
  81. };
  82. //---------------------------------------------------------------------------
  83. class TWindowLock
  84. {
  85. public:
  86. TWindowLock(TCustomScpExplorerForm * Form) :
  87. FForm(Form)
  88. {
  89. FForm->LockWindow();
  90. }
  91. ~TWindowLock()
  92. {
  93. FForm->UnlockWindow();
  94. }
  95. private:
  96. TCustomScpExplorerForm * FForm;
  97. };
  98. //---------------------------------------------------------------------------
  99. class TAutoBatch
  100. {
  101. public:
  102. TAutoBatch(TCustomScpExplorerForm * Form) :
  103. FForm(Form)
  104. {
  105. FForm->BatchStart(FBatchStorage);
  106. }
  107. ~TAutoBatch()
  108. {
  109. FForm->BatchEnd(FBatchStorage);
  110. }
  111. private:
  112. TCustomScpExplorerForm * FForm;
  113. void * FBatchStorage;
  114. };
  115. //---------------------------------------------------------------------------
  116. struct TTransferOperationParam
  117. {
  118. TTransferOperationParam();
  119. UnicodeString TargetDirectory;
  120. bool Temp;
  121. bool DragDrop;
  122. int Options;
  123. TAutoSwitch Queue;
  124. };
  125. //---------------------------------------------------------------------------
  126. TTransferOperationParam::TTransferOperationParam()
  127. {
  128. Temp = false;
  129. DragDrop = false;
  130. Options = 0;
  131. Queue = asAuto;
  132. }
  133. //---------------------------------------------------------------------------
  134. class TTransferPresetNoteData : public TObject
  135. {
  136. public:
  137. UnicodeString Message;
  138. };
  139. //---------------------------------------------------------------------------
  140. class TTerminalNoteData : public TObject
  141. {
  142. public:
  143. TManagedTerminal * Terminal;
  144. };
  145. //---------------------------------------------------------------------------
  146. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  147. FFormRestored(false),
  148. TForm(Owner)
  149. {
  150. FInvalid = false;
  151. FCurrentSide = osRemote;
  152. FEverShown = false;
  153. FDocks = new TList();
  154. InitControls();
  155. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  156. // So that UpdateCustomCommandsToolbar called from RestoreParams works
  157. NonVisualDataModule->ScpExplorer = this;
  158. RestoreParams();
  159. ConfigurationChanged();
  160. RemoteDirView->Invalidate();
  161. FAutoOperation = false;
  162. FOnFileOperationFinished = NULL;
  163. FForceExecution = false;
  164. FIgnoreNextDialogChar = 0;
  165. FErrorList = NULL;
  166. FSynchronizeProgressForm = NULL;
  167. FProgressForm = NULL;
  168. FRefreshLocalDirectory = false;
  169. FRefreshRemoteDirectory = false;
  170. FDDExtMapFile = NULL;
  171. // CreateMutexW keeps failing with ERROR_NOACCESS
  172. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  173. DebugAssert(FDDExtMutex != NULL);
  174. FDDTargetControl = NULL;
  175. FDelayedDeletionTimer = NULL;
  176. FDelayedDeletionList = new TStringList();
  177. FDDFileList = NULL;
  178. FPendingTempSpaceWarn = false;
  179. FCapturedLog = NULL;
  180. FDragDropOperation = false;
  181. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  182. FAllowTransferPresetAutoSelect = true;
  183. FCopyParamDefault = L"";
  184. FSynchronizeController = NULL;
  185. FPendingQueueActionItem = NULL;
  186. FLockLevel = 0;
  187. FLockSuspendLevel = 0;
  188. FDisabledOnLockSuspend = false;
  189. FAlternativeDelete = false;
  190. FTrayIcon = new ::TTrayIcon(0);
  191. FTrayIcon->OnClick = TrayIconClick;
  192. FMaxQueueLength = 0;
  193. FLastContextPopupScreenPoint = TPoint(-1, -1);
  194. FTransferResumeList = NULL;
  195. FMoveToQueue = false;
  196. StandaloneOperation = false;
  197. FOnFeedSynchronizeError = NULL;
  198. FOnSynchronizeAbort = NULL;
  199. FSynchronizeTerminal = NULL;
  200. FNeedSession = false;
  201. FDoNotIdleCurrentTerminal = 0;
  202. FIncrementalSearching = 0;
  203. FIncrementalSearchHaveNext = false;
  204. FQueueFileList.reset(new TQueueFileList());
  205. FDownloadingFromClipboard = false;
  206. FClipboardFakeMonitorsPendingReset = false;
  207. FEditorManager = new TEditorManager();
  208. FEditorManager->OnFileChange = ExecutedFileChanged;
  209. FEditorManager->OnFileReload = ExecutedFileReload;
  210. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  211. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  212. FLocalEditors = new TList();
  213. FQueueStatus = NULL;
  214. FQueueStatusSection = new TCriticalSection();
  215. FQueueStatusInvalidated = false;
  216. FQueueItemInvalidated = false;
  217. FQueueStatusUpdating = false;
  218. FQueueActedItem = NULL;
  219. FQueueController = new TQueueController(QueueView3);
  220. FUserActionTimer = new TTimer(this);
  221. FUserActionTimer->Enabled = false;
  222. FUserActionTimer->Interval = 10;
  223. FUserActionTimer->OnTimer = UserActionTimer;
  224. FNotes = new TStringList();
  225. FNoteTimer = new TTimer(this);
  226. FNoteTimer->Enabled = false;
  227. FNoteTimer->OnTimer = NoteTimer;
  228. FOnNoteClick = NULL;
  229. FOle32Library = LoadLibrary(L"Ole32.dll");
  230. FDragMoveCursor = FOle32Library != NULL ?
  231. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  232. UseSystemSettings(this);
  233. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  234. static_cast<TObject*>(GetComponent(fcTransferList)));
  235. DebugAssert(TransferList != NULL);
  236. FTransferListHoverIndex = -1;
  237. TransferList->OnChange = TransferListChange;
  238. TransferList->OnDrawItem = TransferListDrawItem;
  239. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))));
  240. SetSubmenu(NonVisualDataModule->ColorMenuItem);
  241. UseDesktopFont(SessionsPageControl);
  242. UpdateSessionsPageControlHeight();
  243. UseDesktopFont(RemoteDirView);
  244. UseDesktopFont(RemoteDriveView);
  245. UseDesktopFont(QueueView3);
  246. UseDesktopFont(QueueFileList);
  247. UseDesktopFont(QueueLabel);
  248. UseDesktopFont(RemoteStatusBar);
  249. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  250. QueueSplitter->ShowHint = true;
  251. reinterpret_cast<TLabel*>(QueueFileListSplitter)->OnDblClick = QueueFileListSplitterDblClick;
  252. QueueFileListSplitter->ShowHint = true;
  253. RemotePanelSplitter->ShowHint = true;
  254. UpdateImages();
  255. FCustomCommandMenu = CreateTBXPopupMenu(this);
  256. FCustomCommandLocalFileList = NULL;
  257. FCustomCommandRemoteFileList = NULL;
  258. FSessionColors = new TPngImageList(this);
  259. FSessionColors->ColorDepth = cd32Bit;
  260. AddFixedSessionImages();
  261. SessionsPageControl->Images = FSessionColors;
  262. UpdateQueueLabel();
  263. FRemoteDirViewWasFocused = true;
  264. CreateHiddenWindow();
  265. StartUpdates();
  266. }
  267. //---------------------------------------------------------------------------
  268. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  269. {
  270. FInvalid = true;
  271. // this has to be one of the very first things to do
  272. StopUpdateThread();
  273. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  274. TTerminalManager::Instance()->ScpExplorer = NULL;
  275. delete FCustomCommandLocalFileList;
  276. delete FCustomCommandRemoteFileList;
  277. delete FCustomCommandMenu;
  278. delete FTrayIcon;
  279. FTrayIcon = NULL;
  280. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  281. delete FEditorManager;
  282. ForceCloseLocalEditors();
  283. delete FLocalEditors;
  284. HideFileFindDialog();
  285. if (FDelayedDeletionTimer)
  286. {
  287. DoDelayedDeletion(NULL);
  288. SAFE_DESTROY(FDelayedDeletionTimer);
  289. }
  290. SAFE_DESTROY(FDelayedDeletionList);
  291. // sometimes we do not get DDEnd so the list is not released
  292. SAFE_DESTROY(FDDFileList);
  293. DebugAssert(FSynchronizeController == NULL);
  294. CloseHandle(FDDExtMutex);
  295. FDDExtMutex = NULL;
  296. FreeLibrary(FOle32Library);
  297. FOle32Library = NULL;
  298. FDragMoveCursor = NULL;
  299. DebugAssert(!FErrorList);
  300. if (FEverShown)
  301. {
  302. // when window is never shown (like when running command-line operation),
  303. // particularly window site is not restored correctly (BoundsRect value set
  304. // in RestoreForm gets lost during handle allocation), so we do not want
  305. // it to be stored
  306. StoreParams();
  307. }
  308. Terminal = NULL;
  309. Queue = NULL;
  310. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  311. NonVisualDataModule->ScpExplorer = NULL;
  312. delete FQueueController;
  313. FQueueController = NULL;
  314. delete FQueueStatusSection;
  315. FQueueStatusSection = NULL;
  316. delete FQueueStatus;
  317. FQueueStatus = NULL;
  318. delete FUserActionTimer;
  319. FUserActionTimer = NULL;
  320. delete FNoteTimer;
  321. delete FNotes;
  322. delete FNoteData;
  323. SAFE_DESTROY(FDocks);
  324. SessionsPageControl->Images = NULL;
  325. // only after clearing Terminal (after DoTerminalListChanged => ... => AddSessionColor is called)
  326. SAFE_DESTROY(FSessionColors);
  327. SAFE_DESTROY(FSessionsDragDropFilesEx);
  328. SAFE_DESTROY(FHistoryMenu[0][0]);
  329. SAFE_DESTROY(FHistoryMenu[0][1]);
  330. SAFE_DESTROY(FHistoryMenu[1][0]);
  331. SAFE_DESTROY(FHistoryMenu[1][1]);
  332. if (FHiddenWindow != NULL)
  333. {
  334. DestroyWindow(FHiddenWindow);
  335. FHiddenWindow = NULL;
  336. }
  337. }
  338. //---------------------------------------------------------------------------
  339. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  340. {
  341. std::unique_ptr<TSessionData> Data;
  342. if (!Session.IsEmpty())
  343. {
  344. bool DefaultsOnly;
  345. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  346. }
  347. TTerminalManager * Manager = TTerminalManager::Instance();
  348. for (int Index = 0; Index < Manager->Count; Index++)
  349. {
  350. TTerminal * Terminal = Manager->Terminals[Index];
  351. if (Session.IsEmpty() ||
  352. Manager->IsActiveTerminalForSite(Terminal, Data.get()))
  353. {
  354. if (Path.IsEmpty())
  355. {
  356. Terminal->ClearCaches();
  357. }
  358. else
  359. {
  360. Terminal->DirectoryModified(Path, true);
  361. }
  362. }
  363. }
  364. // We should flag a pending refresh for the background terminals or busy foreground terminals
  365. if (HasActiveTerminal() &&
  366. CanCommandLineFromAnotherInstance() &&
  367. (Session.IsEmpty() || Manager->IsActiveTerminalForSite(Terminal, Data.get())) &&
  368. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  369. {
  370. Terminal->ReloadDirectory();
  371. }
  372. }
  373. //---------------------------------------------------------------------------
  374. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  375. {
  376. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  377. size_t MessageSize = sizeof(TCopyDataMessage);
  378. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  379. if (Result)
  380. {
  381. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  382. Result = (Message.Version == TCopyDataMessage::Version1);
  383. if (Result)
  384. {
  385. switch (Message.Command)
  386. {
  387. case TCopyDataMessage::CommandCanCommandLine:
  388. Result = CanCommandLineFromAnotherInstance();
  389. break;
  390. case TCopyDataMessage::CommandCommandLine:
  391. {
  392. UnicodeString CommandLine(Message.CommandLine);
  393. Result = CommandLineFromAnotherInstance(CommandLine);
  394. }
  395. break;
  396. case TCopyDataMessage::RefreshPanel:
  397. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  398. break;
  399. case TCopyDataMessage::MainWindowCheck:
  400. Result = true;
  401. break;
  402. default:
  403. Result = false;
  404. break;
  405. }
  406. }
  407. }
  408. Message.Result = Result ? 1 : 0;
  409. }
  410. //---------------------------------------------------------------------------
  411. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  412. {
  413. WNDCLASS WindowClass = {0};
  414. WindowClass.lpfnWndProc = DefWindowProc;
  415. WindowClass.hInstance = HInstance;
  416. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  417. FHiddenWindow = NULL;
  418. if (RegisterClass(&WindowClass))
  419. {
  420. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  421. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  422. }
  423. }
  424. //---------------------------------------------------------------------------
  425. bool __fastcall TCustomScpExplorerForm::CanConsole()
  426. {
  427. return
  428. HasActiveTerminal() &&
  429. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  430. }
  431. //---------------------------------------------------------------------------
  432. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  433. {
  434. bool Result = Visible && !NonVisualDataModule->Busy;
  435. return Result;
  436. }
  437. //---------------------------------------------------------------------------
  438. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  439. const UnicodeString & CommandLine)
  440. {
  441. TProgramParams Params(CommandLine);
  442. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  443. if (Result)
  444. {
  445. NonVisualDataModule->StartBusy();
  446. try
  447. {
  448. // this action is initiated from another process,
  449. // so it's likely that our window is not visible,
  450. // and user won't see what is going on
  451. Application->BringToFront();
  452. // reload sessions as we may be asked to open a session
  453. // just stored by another instance
  454. StoredSessions->Reload();
  455. UnicodeString SessionName = Params.Param[1];
  456. std::unique_ptr<TObjectList> DataList(new TObjectList());
  457. try
  458. {
  459. UnicodeString DownloadFile; // unused
  460. int Flags = GetCommandLineParseUrlFlags(&Params);
  461. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this, Flags);
  462. }
  463. catch (EAbort &)
  464. {
  465. // Happens when opening session in PuTTY and in other situations,
  466. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  467. // to process the command-line on its own (or to pass it to yet another instance)
  468. }
  469. if (DataList->Count > 0)
  470. {
  471. TTerminalManager * Manager = TTerminalManager::Instance();
  472. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  473. }
  474. }
  475. __finally
  476. {
  477. NonVisualDataModule->EndBusy();
  478. }
  479. }
  480. return Result;
  481. }
  482. //---------------------------------------------------------------------------
  483. void __fastcall TCustomScpExplorerForm::SetTerminal(TManagedTerminal * value)
  484. {
  485. if (FTerminal != value)
  486. {
  487. TerminalChanging();
  488. DoSetTerminal(value, false);
  489. }
  490. }
  491. //---------------------------------------------------------------------------
  492. void __fastcall TCustomScpExplorerForm::DoSetTerminal(TManagedTerminal * value, bool Replace)
  493. {
  494. FTerminal = value;
  495. bool PrevAllowTransferPresetAutoSelect = FAllowTransferPresetAutoSelect;
  496. FAllowTransferPresetAutoSelect = false;
  497. try
  498. {
  499. TerminalChanged(Replace);
  500. }
  501. __finally
  502. {
  503. FAllowTransferPresetAutoSelect = PrevAllowTransferPresetAutoSelect;
  504. }
  505. if (Terminal != NULL)
  506. {
  507. TransferPresetAutoSelect();
  508. }
  509. }
  510. //---------------------------------------------------------------------------
  511. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  512. {
  513. DoSetTerminal(value, true);
  514. }
  515. //---------------------------------------------------------------------------
  516. void __fastcall TCustomScpExplorerForm::TerminalChanging()
  517. {
  518. if (FTerminal != NULL)
  519. {
  520. UpdateTerminal(Terminal);
  521. }
  522. }
  523. //---------------------------------------------------------------------------
  524. void __fastcall TCustomScpExplorerForm::TerminalChanged(bool Replaced)
  525. {
  526. if (Terminal != NULL)
  527. {
  528. UpdateSessionColor((TColor)Terminal->StateData->Color);
  529. }
  530. DoTerminalListChanged();
  531. DebugAssert(!IsLocalBrowserMode());
  532. if (Replaced)
  533. {
  534. RemoteDirView->ReplaceTerminal(Terminal);
  535. }
  536. else
  537. {
  538. RemoteDirView->Terminal = Terminal;
  539. }
  540. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  541. if (Terminal != NULL)
  542. {
  543. if (Terminal->Active)
  544. {
  545. Terminal->RefreshDirectory();
  546. }
  547. if (WinConfiguration->PreservePanelState)
  548. {
  549. if (Terminal->RemoteExplorerState != NULL)
  550. {
  551. DirView(osRemote)->RestoreState(Terminal->RemoteExplorerState);
  552. }
  553. else
  554. {
  555. DirView(osRemote)->ClearState();
  556. }
  557. }
  558. if (!Replaced && Terminal->Active)
  559. {
  560. InitStatusBar();
  561. }
  562. }
  563. UpdateTransferList();
  564. // Update panels Enable state before refreshing the labels
  565. UpdateControls();
  566. }
  567. //---------------------------------------------------------------------------
  568. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  569. {
  570. if (Queue != value)
  571. {
  572. if (FQueue != NULL)
  573. {
  574. FQueue->OnListUpdate = NULL;
  575. FQueue->OnQueueItemUpdate = NULL;
  576. }
  577. FQueue = value;
  578. if (FQueue != NULL)
  579. {
  580. DebugAssert(FQueue->OnListUpdate == NULL);
  581. FQueue->OnListUpdate = QueueListUpdate;
  582. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  583. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  584. }
  585. QueueChanged();
  586. }
  587. }
  588. //---------------------------------------------------------------------------
  589. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  590. TListItem * Item)
  591. {
  592. if (FQueueActedItem == Item)
  593. {
  594. FQueueActedItem = NULL;
  595. if ((QueueView3->PopupMenu != NULL) &&
  596. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  597. {
  598. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  599. ReleaseCapture();
  600. }
  601. }
  602. if (Item->Data == FPendingQueueActionItem)
  603. {
  604. FPendingQueueActionItem = NULL;
  605. }
  606. }
  607. //---------------------------------------------------------------------------
  608. bool TCustomScpExplorerForm::IsAnythingQueued()
  609. {
  610. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  611. }
  612. //---------------------------------------------------------------------------
  613. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  614. {
  615. {
  616. TGuard Guard(FQueueStatusSection);
  617. FQueueStatusInvalidated = false;
  618. if (FQueue != NULL)
  619. {
  620. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  621. }
  622. }
  623. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  624. {
  625. FMaxQueueLength = FQueueStatus->Count;
  626. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  627. }
  628. {
  629. TAutoFlag Flag(FQueueStatusUpdating);
  630. FQueueController->UpdateQueueStatus(FQueueStatus);
  631. }
  632. UpdateQueueFileList();
  633. SetQueueProgress();
  634. UpdateQueueView();
  635. bool IsEmpty = !IsAnythingQueued();
  636. if (IsEmpty && (Terminal != NULL))
  637. {
  638. TOnceDoneOperation OnceDoneOperation =
  639. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  640. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  641. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  642. {
  643. FQueue->Enabled = false;
  644. }
  645. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  646. {
  647. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  648. }
  649. }
  650. }
  651. //---------------------------------------------------------------------------
  652. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  653. {
  654. UnicodeString Result;
  655. if (FQueueStatus != NULL)
  656. {
  657. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  658. {
  659. TFileOperationProgressType * ProgressData =
  660. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  661. if ((ProgressData != NULL) && ProgressData->InProgress)
  662. {
  663. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  664. }
  665. }
  666. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  667. {
  668. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  669. }
  670. }
  671. return Result;
  672. }
  673. //---------------------------------------------------------------------------
  674. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  675. {
  676. ComponentVisible[fcQueueView] =
  677. (WinConfiguration->QueueView.Show == qvShow) ||
  678. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  679. (FQueueStatus != NULL) && (FQueueStatus->Count > 0));
  680. }
  681. //---------------------------------------------------------------------------
  682. void __fastcall TCustomScpExplorerForm::QueueChanged()
  683. {
  684. if (FQueueStatus != NULL)
  685. {
  686. // UpdateFileList implementation relies on the status being recreated when session changes
  687. delete FQueueStatus;
  688. FQueueStatus = NULL;
  689. }
  690. UpdateQueueStatus(true);
  691. }
  692. //---------------------------------------------------------------------------
  693. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  694. {
  695. if (FQueue == Queue)
  696. {
  697. FQueueStatusInvalidated = true;
  698. }
  699. }
  700. //---------------------------------------------------------------------------
  701. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  702. TQueueItem * Item)
  703. {
  704. if (FQueue == Queue)
  705. {
  706. TGuard Guard(FQueueStatusSection);
  707. // this may be running in parallel with QueueChanged
  708. if (FQueueStatus != NULL)
  709. {
  710. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  711. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  712. {
  713. FRefreshLocalDirectory = (QueueItem == NULL) ||
  714. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  715. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  716. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  717. }
  718. if (QueueItem != NULL)
  719. {
  720. QueueItem->UserData = (void*)true;
  721. FQueueItemInvalidated = true;
  722. }
  723. }
  724. }
  725. }
  726. //---------------------------------------------------------------------------
  727. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  728. {
  729. // during standalone editing, we have no way to see/control queue,
  730. // so we have to always popup prompts automatically
  731. return StandaloneOperation || GUIConfiguration->QueueAutoPopup;
  732. }
  733. //---------------------------------------------------------------------------
  734. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  735. {
  736. if (FQueueStatus != NULL)
  737. {
  738. bool QueueAutoPopup = IsQueueAutoPopup();
  739. bool NeedRefresh = FQueueController->NeedRefresh();
  740. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  741. FQueueItemInvalidated = false;
  742. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  743. bool Updated = false;
  744. TQueueItemProxy * QueueItem;
  745. bool WasUserAction;
  746. for (int Index = 0; Index < Limit; Index++)
  747. {
  748. bool Update = false;
  749. QueueItem = FQueueStatus->Items[Index];
  750. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  751. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  752. (FPendingQueueActionItem == NULL))
  753. {
  754. FPendingQueueActionItem = QueueItem;
  755. FUserActionTimer->Enabled = true;
  756. }
  757. if ((bool)QueueItem->UserData)
  758. {
  759. QueueItem->UserData = (void*)false;
  760. QueueItem->Update();
  761. Updated = true;
  762. Update = true;
  763. }
  764. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  765. {
  766. Update = true;
  767. }
  768. if (Update)
  769. {
  770. FQueueController->RefreshQueueItem(QueueItem);
  771. }
  772. }
  773. if (Updated)
  774. {
  775. NonVisualDataModule->UpdateNonVisibleActions();
  776. SetQueueProgress();
  777. UpdateQueueFileList();
  778. }
  779. }
  780. }
  781. //---------------------------------------------------------------------------
  782. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  783. {
  784. if (FTaskbarList != NULL)
  785. {
  786. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  787. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  788. }
  789. }
  790. //---------------------------------------------------------------------------
  791. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  792. {
  793. if (Progress >= 0)
  794. {
  795. if (FTaskbarList != NULL)
  796. {
  797. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  798. }
  799. }
  800. else
  801. {
  802. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  803. }
  804. }
  805. //---------------------------------------------------------------------------
  806. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  807. {
  808. int OverallProgress;
  809. // FProgressForm is null when this is called from SetQueueProgress
  810. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  811. {
  812. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  813. }
  814. else if (!TFileOperationProgressType::IsIndeterminateOperation(ProgressData->Operation))
  815. {
  816. OverallProgress = ProgressData->OverallProgress();
  817. }
  818. else
  819. {
  820. OverallProgress = -1;
  821. }
  822. SetTaskbarListProgressValue(OverallProgress);
  823. }
  824. //---------------------------------------------------------------------------
  825. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  826. {
  827. TTerminalManager::Instance()->QueueStatusUpdated();
  828. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  829. {
  830. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  831. {
  832. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  833. {
  834. TFileOperationProgressType * ProgressData = NULL;
  835. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  836. {
  837. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  838. }
  839. if ((ProgressData != NULL) &&
  840. ProgressData->InProgress)
  841. {
  842. SetTaskbarListProgressValue(ProgressData);
  843. }
  844. else
  845. {
  846. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  847. }
  848. }
  849. else
  850. {
  851. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  852. }
  853. }
  854. else
  855. {
  856. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  857. }
  858. }
  859. UpdateQueueLabel();
  860. }
  861. //---------------------------------------------------------------------------
  862. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  863. {
  864. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  865. if (FQueueStatus != NULL)
  866. {
  867. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  868. if (ActiveAndPendingPrimaryCount > 0)
  869. {
  870. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  871. }
  872. }
  873. QueueLabel->Caption = Caption;
  874. }
  875. //---------------------------------------------------------------------------
  876. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  877. {
  878. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  879. static_cast<TComponent*>(GetComponent(fcTransferList)));
  880. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  881. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  882. TransferList->Strings->BeginUpdate();
  883. try
  884. {
  885. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  886. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  887. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  888. if (FTransferDropDownHint.IsEmpty())
  889. {
  890. FTransferDropDownHint = TransferDropDown->Hint;
  891. }
  892. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  893. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  894. TransferDropDown->Text = StripHotkey(Name);
  895. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  896. (FTransferDropDownHint, StripHotkey(Name),
  897. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  898. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General))));
  899. // update the label, otherwise when it is updated only on the first draw
  900. // of the list, it is drawn "bold" for some reason
  901. FTransferListHoverIndex = TransferList->ItemIndex;
  902. UpdateTransferLabel();
  903. }
  904. __finally
  905. {
  906. TransferList->Strings->EndUpdate();
  907. }
  908. }
  909. //---------------------------------------------------------------------------
  910. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  911. {
  912. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  913. DebugAssert(Toolbar != NULL);
  914. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  915. }
  916. //---------------------------------------------------------------------------
  917. void __fastcall TCustomScpExplorerForm::UpdateActions()
  918. {
  919. TForm::UpdateActions();
  920. if (ComponentVisible[fcCustomCommandsBand])
  921. {
  922. UpdateCustomCommandsToolbar();
  923. }
  924. }
  925. //---------------------------------------------------------------------------
  926. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  927. {
  928. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  929. }
  930. //---------------------------------------------------------------------------
  931. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  932. {
  933. Color = GetBtnFaceColor();
  934. DebugAssert(Configuration && RemoteDirView);
  935. RemoteDirView->DDAllowMove = !WinConfiguration->DDDisableMove;
  936. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  937. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  938. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  939. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  940. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  941. if (RemoteDirView->RowSelect != WinConfiguration->FullRowSelect)
  942. {
  943. RemoteDirView->RowSelect = WinConfiguration->FullRowSelect;
  944. // Without this, the panel turns gray and unused part of header turns black
  945. RemoteDirView->Perform(CM_COLORCHANGED, 0, 0);
  946. }
  947. RemoteDriveView->DDAllowMove = !WinConfiguration->DDDisableMove;
  948. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  949. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  950. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  951. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  952. UpdateSessionsPageControlHeight();
  953. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  954. UpdateToolbarDisplayMode();
  955. UpdateTransferList();
  956. if (Terminal != NULL)
  957. {
  958. TransferPresetAutoSelect();
  959. }
  960. UpdateQueueView();
  961. UpdateControls();
  962. // this can be called even before constuctor finishes.
  963. if (FEditorManager != NULL)
  964. {
  965. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  966. }
  967. // this can be called even before constuctor finishes.
  968. if (FLocalEditors != NULL)
  969. {
  970. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  971. {
  972. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  973. }
  974. }
  975. if (ComponentVisible[fcCustomCommandsBand])
  976. {
  977. UpdateCustomCommandsToolbar();
  978. }
  979. // show only when keeping queue items forever,
  980. // otherwise, its enough to have in in the context menu
  981. QueueDeleteAllDoneQueueToolbarItem->Visible =
  982. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  983. if (FFileColorsCurrent != WinConfiguration->FileColors)
  984. {
  985. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  986. FFileColorsCurrent = WinConfiguration->FileColors;
  987. FileColorsChanged();
  988. }
  989. }
  990. //---------------------------------------------------------------------------
  991. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  992. {
  993. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  994. DirView->Invalidate();
  995. }
  996. //---------------------------------------------------------------------------
  997. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  998. {
  999. DoFileColorsChanged(RemoteDirView);
  1000. }
  1001. //---------------------------------------------------------------------------
  1002. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  1003. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  1004. void * /*Arg*/)
  1005. {
  1006. if (Token != NULL)
  1007. {
  1008. TForm * Editor = dynamic_cast<TForm*>(Token);
  1009. DebugAssert(Editor != NULL);
  1010. ReconfigureEditorForm(Editor);
  1011. }
  1012. }
  1013. //---------------------------------------------------------------------------
  1014. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  1015. {
  1016. WinConfiguration->DDTransferConfirmation = asOn;
  1017. }
  1018. //---------------------------------------------------------------------------
  1019. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1020. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1021. {
  1022. // noop
  1023. }
  1024. //---------------------------------------------------------------------------
  1025. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1026. TTransferDirection Direction, TTransferType Type, bool Temp,
  1027. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1028. bool Confirm, bool DragDrop, int Options)
  1029. {
  1030. bool Result = true;
  1031. DebugAssert(HasActiveTerminal());
  1032. // these parameters are known in advance
  1033. int Params =
  1034. FLAGMASK(Type == ttMove, cpDelete);
  1035. bool ToTemp = (Temp && (Direction == tdToLocal));
  1036. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1037. {
  1038. bool DisableNewerOnly =
  1039. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1040. ToTemp;
  1041. Options |=
  1042. FLAGMASK(ToTemp, coTemp) |
  1043. FLAGMASK(!Terminal->IsCapable[fcBackgroundTransfers], coDisableQueue) |
  1044. coDoNotShowAgain;
  1045. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1046. int CopyParamAttrs = (Direction == tdToRemote ?
  1047. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1048. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1049. int OutputOptions =
  1050. FLAGMASK(DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto),
  1051. cooDoNotShowAgain);
  1052. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1053. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1054. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1055. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1056. if (Result)
  1057. {
  1058. if (FLAGSET(OutputOptions, cooDoNotShowAgain))
  1059. {
  1060. if (DragDrop)
  1061. {
  1062. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1063. {
  1064. PopupTrayBalloon(NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation,
  1065. NULL, 0, EnableDDTransferConfirmation, NULL);
  1066. }
  1067. WinConfiguration->DDTransferConfirmation = asOff;
  1068. }
  1069. else
  1070. {
  1071. WinConfiguration->ConfirmTransferring = false;
  1072. }
  1073. }
  1074. else
  1075. {
  1076. // User explicitly unchecked "do not show again",
  1077. // so show him the dialog the next time
  1078. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1079. {
  1080. WinConfiguration->DDTransferConfirmation = asOn;
  1081. }
  1082. }
  1083. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1084. }
  1085. }
  1086. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fcBackgroundTransfers])
  1087. {
  1088. Configuration->Usage->Inc(L"TransfersOnBackground");
  1089. // these parameter are known only after transfer dialog
  1090. Params |=
  1091. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1092. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1093. Result = false;
  1094. ClearTransferSourceSelection(Direction);
  1095. }
  1096. return Result;
  1097. }
  1098. //---------------------------------------------------------------------------
  1099. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1100. {
  1101. if (FOnFileOperationFinished != NULL)
  1102. {
  1103. FOnFileOperationFinished(UnicodeString(), true);
  1104. }
  1105. else
  1106. {
  1107. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1108. if (HasDirView[Side])
  1109. {
  1110. DirView(Side)->SelectAll(smNone);
  1111. }
  1112. }
  1113. }
  1114. //---------------------------------------------------------------------------
  1115. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1116. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1117. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1118. int Params)
  1119. {
  1120. DebugAssert(Queue != NULL);
  1121. bool SingleFile = false;
  1122. if (FileList->Count == 1)
  1123. {
  1124. if (Direction == tdToRemote)
  1125. {
  1126. UnicodeString FileName = FileList->Strings[0];
  1127. SingleFile = FileExists(ApiPath(FileName));
  1128. }
  1129. else
  1130. {
  1131. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1132. SingleFile = !File->IsDirectory;
  1133. }
  1134. }
  1135. TQueueItem * QueueItem;
  1136. if (Direction == tdToRemote)
  1137. {
  1138. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1139. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1140. }
  1141. else
  1142. {
  1143. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1144. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1145. }
  1146. AddQueueItem(Queue, QueueItem, Terminal);
  1147. }
  1148. //---------------------------------------------------------------------------
  1149. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1150. {
  1151. if (Queue->IsEmpty)
  1152. {
  1153. Terminal->QueueOperationStart = Now();
  1154. }
  1155. Queue->AddItem(QueueItem);
  1156. }
  1157. //---------------------------------------------------------------------------
  1158. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1159. {
  1160. }
  1161. //---------------------------------------------------------------------------
  1162. void __fastcall TCustomScpExplorerForm::InitControls()
  1163. {
  1164. DebugAssert(FDocks != NULL);
  1165. for (int Index = 0; Index < ComponentCount; Index++)
  1166. {
  1167. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1168. if ((Dock != NULL) && (Dock->Tag == 0))
  1169. {
  1170. FDocks->Add(Dock);
  1171. }
  1172. }
  1173. CollectItemsWithTextDisplayMode(this);
  1174. }
  1175. //---------------------------------------------------------------------------
  1176. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1177. {
  1178. ConfigurationChanged();
  1179. QueuePanel->Height =
  1180. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1181. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1182. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1183. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1184. QueueFileList->Visible = WinConfiguration->QueueView.FileList;
  1185. QueueFileList->Height =
  1186. LoadDimension(WinConfiguration->QueueView.FileListHeight, WinConfiguration->QueueView.FileListHeightPixelsPerInch, this);
  1187. if (QueueFileList->Visible)
  1188. {
  1189. AdjustQueueLayout();
  1190. }
  1191. UpdateCustomCommandsToolbar();
  1192. }
  1193. //---------------------------------------------------------------------------
  1194. void __fastcall TCustomScpExplorerForm::StoreParams()
  1195. {
  1196. int APixelsPerInch = GetControlPixelsPerInch(this);
  1197. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1198. WinConfiguration->QueueView.HeightPixelsPerInch = APixelsPerInch;
  1199. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1200. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1201. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1202. WinConfiguration->QueueView.FileList = QueueFileList->Visible;
  1203. WinConfiguration->QueueView.FileListHeight = QueueFileList->Height;
  1204. WinConfiguration->QueueView.FileListHeightPixelsPerInch = APixelsPerInch;
  1205. }
  1206. //---------------------------------------------------------------------------
  1207. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1208. {
  1209. if (!FFormRestored)
  1210. {
  1211. FFormRestored = true;
  1212. RestoreFormParams();
  1213. }
  1214. TForm::CreateParams(Params);
  1215. }
  1216. //---------------------------------------------------------------------------
  1217. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1218. {
  1219. DebugAssert(FDocks != NULL);
  1220. for (int Index = 0; Index < FDocks->Count; Index++)
  1221. {
  1222. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1223. }
  1224. }
  1225. //---------------------------------------------------------------------------
  1226. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1227. {
  1228. SetDockAllowDrag(true);
  1229. while (!ButtonsStr.IsEmpty())
  1230. {
  1231. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1232. UnicodeString Name = CutToChar(S, L'=', true);
  1233. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1234. {
  1235. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1236. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1237. {
  1238. while (!S.IsEmpty())
  1239. {
  1240. UnicodeString S2 = CutToChar(S, L',', true);
  1241. UnicodeString Name = CutToChar(S2, L':', true);
  1242. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1243. {
  1244. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1245. if (SameText(GetToolbarItemName(Item), Name))
  1246. {
  1247. // forward compatibility
  1248. UnicodeString S3 = CutToChar(S2, L':', true);
  1249. bool Visible = StrToIntDef(S3, 0);
  1250. Item->Visible = Visible;
  1251. }
  1252. }
  1253. }
  1254. }
  1255. }
  1256. }
  1257. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1258. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1259. }
  1260. //---------------------------------------------------------------------------
  1261. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1262. {
  1263. UnicodeString Result;
  1264. SetDockAllowDrag(true);
  1265. Result = ::GetToolbarsLayoutStr(this);
  1266. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1267. return Result;
  1268. }
  1269. //---------------------------------------------------------------------------
  1270. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1271. {
  1272. UnicodeString Result;
  1273. if (Item->Action != NULL)
  1274. {
  1275. Result = Item->Action->Name;
  1276. Result = RemoveSuffix(Result, L"Action", true);
  1277. }
  1278. else
  1279. {
  1280. Result = Item->Name;
  1281. Result = RemoveSuffix(Result, L"SubmenuItem", true);
  1282. Result = RemoveSuffix(Result, L"Item", true);
  1283. }
  1284. return Result;
  1285. }
  1286. //---------------------------------------------------------------------------
  1287. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1288. {
  1289. UnicodeString Result;
  1290. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1291. {
  1292. UnicodeString ToolbarStr;
  1293. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1294. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1295. {
  1296. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1297. {
  1298. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1299. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1300. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1301. // and save the changes here only.
  1302. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1303. {
  1304. UnicodeString Name = GetToolbarItemName(Item);
  1305. DebugAssert(Name.Pos(L"TBX") == 0);
  1306. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1307. }
  1308. }
  1309. }
  1310. if (!ToolbarStr.IsEmpty())
  1311. {
  1312. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1313. }
  1314. }
  1315. return Result;
  1316. }
  1317. //---------------------------------------------------------------------------
  1318. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1319. {
  1320. DebugAssert(FProgressForm == NULL);
  1321. bool AllowSkip = (Terminal != NULL) ? Terminal->IsCapable[fcSkipTransfer] : false;
  1322. FProgressForm = new TProgressForm(Application, (FTransferResumeList != NULL), AllowSkip, SynchronizeProgress);
  1323. FProgressForm->DeleteLocalToRecycleBin =
  1324. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1325. FProgressForm->DeleteRemoteToRecycleBin =
  1326. (Terminal != NULL) &&
  1327. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1328. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1329. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1330. LockWindow();
  1331. // Actually, do not know what hides the progress once the operation finishes
  1332. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1333. SetTaskbarListProgressState(TBPF_NORMAL);
  1334. }
  1335. //---------------------------------------------------------------------------
  1336. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1337. {
  1338. UnlockWindow();
  1339. SAFE_DESTROY(FProgressForm);
  1340. SetQueueProgress();
  1341. }
  1342. //---------------------------------------------------------------------------
  1343. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1344. TFileOperationProgressType & ProgressData)
  1345. {
  1346. // Download to temporary local directory
  1347. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1348. {
  1349. bool Continue = true;
  1350. FPendingTempSpaceWarn = false;
  1351. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1352. if (!Continue)
  1353. {
  1354. Abort();
  1355. }
  1356. }
  1357. // operation is being executed and we still didn't show up progress form
  1358. if (ProgressData.InProgress && !FProgressForm)
  1359. {
  1360. CreateProgressForm(NULL);
  1361. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1362. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1363. }
  1364. // operation is finished (or terminated), so we hide progress form
  1365. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1366. {
  1367. DestroyProgressForm();
  1368. if (ProgressData.Operation == foCalculateSize)
  1369. {
  1370. // When calculating size before transfer, the abort caused by
  1371. // cancel flag set due to "MoveToQueue" below,
  1372. // is not propagated back to us in ExecuteFileOperation,
  1373. // as it is silently swallowed.
  1374. // So we explicitly (re)throw it here.
  1375. if (FMoveToQueue)
  1376. {
  1377. Abort();
  1378. }
  1379. }
  1380. else
  1381. {
  1382. if ((ProgressData.Cancel == csContinue) &&
  1383. !FAutoOperation)
  1384. {
  1385. OperationComplete(ProgressData.StartTime);
  1386. }
  1387. }
  1388. }
  1389. if (FProgressForm &&
  1390. // Can happen only during synchronization, when ending one of the operations
  1391. ProgressData.InProgress)
  1392. {
  1393. FProgressForm->SetProgressData(ProgressData);
  1394. DebugAssert(ProgressData.InProgress);
  1395. SetTaskbarListProgressValue(&ProgressData);
  1396. if (FProgressForm->Cancel > csContinue)
  1397. {
  1398. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1399. if (FProgressForm->Cancel == csCancelFile)
  1400. {
  1401. FProgressForm->ClearCancel();
  1402. }
  1403. // cancel cancels even the move
  1404. FMoveToQueue = false;
  1405. }
  1406. else if (FProgressForm->MoveToQueue)
  1407. {
  1408. FMoveToQueue = true;
  1409. ProgressData.SetCancelAtLeast(csCancel);
  1410. }
  1411. if ((FTransferResumeList != NULL) &&
  1412. ProgressData.InProgress &&
  1413. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1414. !ProgressData.FullFileName.IsEmpty())
  1415. {
  1416. if ((FTransferResumeList->Count == 0) ||
  1417. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1418. {
  1419. // note that we do not recognize directories from files here
  1420. FTransferResumeList->Add(ProgressData.FullFileName);
  1421. }
  1422. }
  1423. }
  1424. }
  1425. //---------------------------------------------------------------------------
  1426. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1427. const TDateTime & StartTime)
  1428. {
  1429. if (GUIConfiguration->BeepOnFinish &&
  1430. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1431. {
  1432. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1433. DWORD Sound;
  1434. if (!ExtractFileExt(BeepSound).IsEmpty())
  1435. {
  1436. Sound = SND_FILENAME;
  1437. }
  1438. else
  1439. {
  1440. Sound = SND_ALIAS;
  1441. }
  1442. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1443. }
  1444. }
  1445. //---------------------------------------------------------------------------
  1446. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1447. TFileOperationProgressType & ProgressData)
  1448. {
  1449. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1450. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1451. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1452. FileOperationProgress(ProgressData);
  1453. }
  1454. //---------------------------------------------------------------------------
  1455. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1456. {
  1457. UnicodeString Result;
  1458. if (FProgressForm != NULL)
  1459. {
  1460. Result = FProgressForm->ProgressStr();
  1461. }
  1462. return Result;
  1463. }
  1464. //---------------------------------------------------------------------------
  1465. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1466. bool DragDrop)
  1467. {
  1468. return (!DragDrop && (DropSourceControl == NULL)) ||
  1469. (DropSourceControl == DirView(osOther));
  1470. }
  1471. //---------------------------------------------------------------------------
  1472. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1473. TFileOperation Operation, TOperationSide Side,
  1474. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1475. TOnceDoneOperation & OnceDoneOperation)
  1476. {
  1477. if (!FAutoOperation)
  1478. {
  1479. // no selection on "/upload", form serves only as event handler
  1480. // (it is not displayed)
  1481. if (PanelOperation(Side, FDragDropOperation) &&
  1482. Visible && (Operation != foCalculateSize) &&
  1483. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1484. {
  1485. if (FOnFileOperationFinished != NULL)
  1486. {
  1487. FOnFileOperationFinished(FileName, Success);
  1488. }
  1489. else
  1490. {
  1491. TCustomDirView * DView = DirView(Side);
  1492. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1493. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1494. // this can happen when local drive is unplugged in the middle of the operation
  1495. if (Item != NULL)
  1496. {
  1497. if (Success) Item->Selected = false;
  1498. DView->MakeProgressVisible(Item);
  1499. }
  1500. }
  1501. }
  1502. if ((Operation == foCopy) || (Operation == foMove))
  1503. {
  1504. DebugAssert(!IsLocalBrowserMode());
  1505. if (Side == osLocal)
  1506. {
  1507. Configuration->Usage->Inc(L"UploadedFiles");
  1508. }
  1509. else
  1510. {
  1511. Configuration->Usage->Inc(L"DownloadedFiles");
  1512. }
  1513. }
  1514. }
  1515. if (Success && (FSynchronizeController != NULL))
  1516. {
  1517. DebugAssert(!IsSideLocalBrowser(osRemote));
  1518. if (Operation == foCopy)
  1519. {
  1520. DebugAssert(Side == osLocal);
  1521. FSynchronizeController->LogOperation(soUpload, FileName);
  1522. }
  1523. else if (Operation == foDelete)
  1524. {
  1525. DebugAssert(Side == osRemote);
  1526. FSynchronizeController->LogOperation(soDelete, FileName);
  1527. }
  1528. }
  1529. if (FProgressForm)
  1530. {
  1531. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1532. }
  1533. }
  1534. //---------------------------------------------------------------------------
  1535. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1536. TFileOperation Operation, TOperationSide Side,
  1537. bool Temp, const UnicodeString & FileName, Boolean Success,
  1538. TOnceDoneOperation & OnceDoneOperation)
  1539. {
  1540. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1541. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1542. OnceDoneOperation);
  1543. }
  1544. //---------------------------------------------------------------------------
  1545. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1546. {
  1547. return false;
  1548. }
  1549. //---------------------------------------------------------------------------
  1550. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1551. {
  1552. return false;
  1553. }
  1554. //---------------------------------------------------------------------------
  1555. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1556. {
  1557. return NULL;
  1558. }
  1559. //---------------------------------------------------------------------------
  1560. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1561. {
  1562. DebugAssert(GetSide(Side) == osRemote);
  1563. DebugUsedParam(Side);
  1564. return RemoteDirView;
  1565. }
  1566. //---------------------------------------------------------------------------
  1567. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1568. {
  1569. DebugAssert(GetSide(Side) == osRemote);
  1570. DebugUsedParam(Side);
  1571. return RemoteDriveView;
  1572. }
  1573. //---------------------------------------------------------------------------
  1574. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1575. {
  1576. DebugAssert(GetSide(Side) == osRemote);
  1577. DebugUsedParam(Side);
  1578. return HasActiveTerminal();
  1579. }
  1580. //---------------------------------------------------------------------------
  1581. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1582. TOperationSide Side, int FilesOnly)
  1583. {
  1584. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1585. }
  1586. //---------------------------------------------------------------------------
  1587. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1588. TOperationSide Side, int FilesOnly)
  1589. {
  1590. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1591. }
  1592. //---------------------------------------------------------------------------
  1593. struct THistoryItemData
  1594. {
  1595. short int Side;
  1596. short int Index;
  1597. };
  1598. //---------------------------------------------------------------------------
  1599. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1600. {
  1601. DirView(Side)->HistoryGo(Index);
  1602. }
  1603. //---------------------------------------------------------------------------
  1604. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1605. {
  1606. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1607. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1608. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1609. }
  1610. //---------------------------------------------------------------------------
  1611. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1612. bool Back)
  1613. {
  1614. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1615. {
  1616. TCustomDirView * DView = DirView(Side);
  1617. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1618. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1619. if (ICount > 10)
  1620. {
  1621. ICount = 10;
  1622. }
  1623. Menu->Items->Clear();
  1624. THistoryItemData Data;
  1625. Data.Side = Side;
  1626. for (short int i = 1; i <= ICount; i++)
  1627. {
  1628. TTBCustomItem * Item = new TTBXItem(Menu);
  1629. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1630. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1631. Item->Hint = DView->HistoryPath[Data.Index];
  1632. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1633. Item->Tag = *reinterpret_cast<int*>(&Data);
  1634. Item->OnClick = HistoryItemClick;
  1635. Menu->Items->Add(Item);
  1636. }
  1637. }
  1638. }
  1639. //---------------------------------------------------------------------------
  1640. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1641. TOperationSide Side, bool Back)
  1642. {
  1643. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1644. {
  1645. // workaround
  1646. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1647. // operator new allocates memory in Pascal code, but calls inline
  1648. // contructor in C++, leading in problems, the function does
  1649. // both in Pascal code
  1650. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1651. UpdateHistoryMenu(Side, Back);
  1652. }
  1653. return FHistoryMenu[Side == osLocal][Back];
  1654. }
  1655. //---------------------------------------------------------------------------
  1656. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1657. TCustomDirView *Sender)
  1658. {
  1659. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1660. UpdateHistoryMenu(Side, true);
  1661. UpdateHistoryMenu(Side, false);
  1662. }
  1663. //---------------------------------------------------------------------------
  1664. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1665. {
  1666. // remote custom commands can be executed only if the server supports shell commands
  1667. // or have secondary shell
  1668. return HasActiveTerminal() && (FTerminal->IsCapable[fcSecondaryShell] || FTerminal->IsCapable[fcShellAnyCommand]);
  1669. }
  1670. //---------------------------------------------------------------------------
  1671. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1672. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1673. {
  1674. // -1 = hidden, 0 = disabled, 1 = enabled
  1675. int Result;
  1676. TFileCustomCommand RemoteCustomCommand;
  1677. TLocalCustomCommand LocalCustomCommand;
  1678. TFileCustomCommand * NonInteractiveCustomCommand =
  1679. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1680. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1681. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1682. if (FLAGCLEAR(Command.Params, ccLocal))
  1683. {
  1684. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1685. // custom command that does not operate with files can be executed anytime ...
  1686. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1687. {
  1688. // ... but do not show such command in remote file menu
  1689. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1690. {
  1691. Result = AllowedState;
  1692. }
  1693. else
  1694. {
  1695. Result = -1;
  1696. }
  1697. }
  1698. else
  1699. {
  1700. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1701. {
  1702. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1703. }
  1704. else
  1705. {
  1706. Result = -1;
  1707. }
  1708. }
  1709. }
  1710. else
  1711. {
  1712. // custom command that does not operate with files can be executed anytime
  1713. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1714. {
  1715. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1716. }
  1717. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1718. {
  1719. if ((ListType == ccltAll) || (ListType == ccltFile))
  1720. {
  1721. // special case is "diff"-style command that can be executed over any side,
  1722. // if we have both sides
  1723. Result =
  1724. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1725. // directly (instead of EnableSelectedOperation) to pass
  1726. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1727. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1728. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1729. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1730. }
  1731. else if (ListType == ccltBoth)
  1732. {
  1733. DebugAssert(!IsLocalBrowserMode());
  1734. Result = 1;
  1735. }
  1736. else
  1737. {
  1738. Result = -1;
  1739. }
  1740. }
  1741. else
  1742. {
  1743. if ((ListType == ccltAll) ||
  1744. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1745. {
  1746. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1747. }
  1748. else
  1749. {
  1750. Result = -1;
  1751. }
  1752. }
  1753. if ((Result > 0) &&
  1754. LocalCustomCommand.IsSessionCommand(Cmd) &&
  1755. !HasActiveTerminal())
  1756. {
  1757. Result = 0;
  1758. }
  1759. }
  1760. return Result;
  1761. }
  1762. //---------------------------------------------------------------------------
  1763. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1764. TStrings * FileList, const TCustomCommandType & ACommand,
  1765. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1766. {
  1767. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1768. {
  1769. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1770. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1771. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1772. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1773. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1774. bool Capture =
  1775. FLAGSET(ACommand.Params, ccShowResults) ||
  1776. FLAGSET(ACommand.Params, ccCopyResults) ||
  1777. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1778. TCaptureOutputEvent OutputEvent = NULL;
  1779. DebugAssert(FCapturedLog == NULL);
  1780. if (Capture)
  1781. {
  1782. FCapturedLog = new TStringList();
  1783. OutputEvent = TerminalCaptureLog;
  1784. }
  1785. try
  1786. {
  1787. if (!RemoteCustomCommand.IsFileCommand(Command))
  1788. {
  1789. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1790. OutputEvent);
  1791. }
  1792. else
  1793. {
  1794. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1795. }
  1796. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1797. {
  1798. if (FLAGSET(ACommand.Params, ccCopyResults))
  1799. {
  1800. CopyToClipboard(FCapturedLog);
  1801. }
  1802. if (FLAGSET(ACommand.Params, ccShowResults))
  1803. {
  1804. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1805. }
  1806. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1807. {
  1808. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1809. }
  1810. }
  1811. }
  1812. __finally
  1813. {
  1814. SAFE_DESTROY(FCapturedLog);
  1815. }
  1816. }
  1817. }
  1818. //---------------------------------------------------------------------------
  1819. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1820. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1821. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1822. {
  1823. DebugAssert(!IsLocalBrowserMode());
  1824. TStrings * LocalFileList = NULL;
  1825. TStrings * RemoteFileList = NULL;
  1826. TStrings * RemoteFileListFull = NULL;
  1827. try
  1828. {
  1829. if (LocalFileCommand)
  1830. {
  1831. if (ALocalFileList == NULL)
  1832. {
  1833. DebugAssert(HasDirView[osLocal]);
  1834. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1835. // directly (instead of EnableSelectedOperation) to pass
  1836. // false to FocusedFileOnlyWhenFocused
  1837. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1838. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1839. }
  1840. else
  1841. {
  1842. LocalFileList = ALocalFileList;
  1843. }
  1844. if (FileListCommand)
  1845. {
  1846. if (LocalFileList->Count != 1)
  1847. {
  1848. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1849. }
  1850. }
  1851. else
  1852. {
  1853. if ((LocalFileList->Count != 1) &&
  1854. (FileList->Count != 1) &&
  1855. (LocalFileList->Count != FileList->Count))
  1856. {
  1857. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1858. }
  1859. }
  1860. }
  1861. UnicodeString RootTempDir;
  1862. UnicodeString TempDir;
  1863. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1864. if (!RemoteFiles)
  1865. {
  1866. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1867. }
  1868. try
  1869. {
  1870. TDateTimes RemoteFileTimes;
  1871. if (RemoteFiles)
  1872. {
  1873. RemoteFileList = FileList;
  1874. }
  1875. else
  1876. {
  1877. RemoteFileList = new TStringList();
  1878. TMakeLocalFileListParams MakeFileListParam;
  1879. MakeFileListParam.FileList = RemoteFileList;
  1880. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1881. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1882. MakeFileListParam.Recursive =
  1883. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1884. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1885. if (!MakeFileListParam.Recursive)
  1886. {
  1887. RemoteFileTimes.clear();
  1888. RemoteFileListFull = new TStringList();
  1889. MakeFileListParam.FileList = RemoteFileListFull;
  1890. MakeFileListParam.Recursive = true;
  1891. MakeFileListParam.IncludeDirs = false;
  1892. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1893. }
  1894. else
  1895. {
  1896. RemoteFileListFull = RemoteFileList;
  1897. }
  1898. }
  1899. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1900. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1901. if (!NonBlocking)
  1902. {
  1903. int Count;
  1904. if (FileListCommand)
  1905. {
  1906. Count = 1;
  1907. }
  1908. else if (LocalFileCommand)
  1909. {
  1910. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  1911. }
  1912. else
  1913. {
  1914. Count = RemoteFileList->Count;
  1915. }
  1916. Progress.Start(foCustomCommand, osRemote, Count);
  1917. DebugAssert(FProgressForm != NULL);
  1918. FProgressForm->ReadOnly = true;
  1919. }
  1920. try
  1921. {
  1922. if (FileListCommand)
  1923. {
  1924. UnicodeString LocalFile;
  1925. // MakeFileList does not delimit filenames
  1926. UnicodeString FileList = MakeFileList(RemoteFileList);
  1927. if (LocalFileCommand)
  1928. {
  1929. DebugAssert(LocalFileList->Count == 1);
  1930. LocalFile = LocalFileList->Strings[0];
  1931. }
  1932. TLocalCustomCommand CustomCommand(Data,
  1933. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1934. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1935. if (NonBlocking)
  1936. {
  1937. DebugAssert(!POutput);
  1938. ExecuteShellChecked(ShellCommand);
  1939. }
  1940. else
  1941. {
  1942. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1943. }
  1944. }
  1945. else if (LocalFileCommand)
  1946. {
  1947. if (LocalFileList->Count == 1)
  1948. {
  1949. UnicodeString LocalFile = LocalFileList->Strings[0];
  1950. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1951. {
  1952. UnicodeString FileName = RemoteFileList->Strings[Index];
  1953. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1954. Progress.SetFile(FileNameForProgress);
  1955. TLocalCustomCommand CustomCommand(Data,
  1956. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  1957. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1958. TOnceDoneOperation OnceDoneOperation;
  1959. // Do not unselect anything, as it won't work with two panels anyway
  1960. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1961. }
  1962. }
  1963. else if (RemoteFileList->Count == 1)
  1964. {
  1965. UnicodeString FileName = RemoteFileList->Strings[0];
  1966. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1967. {
  1968. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1969. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1970. Progress.SetFile(LocalFileNameOnly);
  1971. TLocalCustomCommand CustomCommand(
  1972. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1973. FileName, LocalFileName, L"");
  1974. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1975. TOnceDoneOperation OnceDoneOperation;
  1976. // Do not unselect anything, as it won't work with two panels anyway
  1977. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1978. }
  1979. }
  1980. else
  1981. {
  1982. if (LocalFileList->Count != RemoteFileList->Count)
  1983. {
  1984. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  1985. }
  1986. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1987. {
  1988. UnicodeString FileName = RemoteFileList->Strings[Index];
  1989. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1990. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1991. Progress.SetFile(LocalFileNameOnly); // sic
  1992. TLocalCustomCommand CustomCommand(
  1993. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1994. FileName, LocalFileName, L"");
  1995. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1996. TOnceDoneOperation OnceDoneOperation;
  1997. // Do not unselect anything, as it won't work with two panels anyway
  1998. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1999. }
  2000. }
  2001. }
  2002. else
  2003. {
  2004. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2005. {
  2006. UnicodeString FileName = RemoteFileList->Strings[Index];
  2007. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2008. Progress.SetFile(FileNameForProgress);
  2009. TLocalCustomCommand CustomCommand(Data,
  2010. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2011. FileName, L"", L"");
  2012. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2013. TOnceDoneOperation OnceDoneOperation;
  2014. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  2015. }
  2016. }
  2017. }
  2018. __finally
  2019. {
  2020. if (!NonBlocking)
  2021. {
  2022. Progress.Stop();
  2023. }
  2024. }
  2025. DebugAssert(!FAutoOperation);
  2026. if (!RemoteFiles)
  2027. {
  2028. TempDir = IncludeTrailingBackslash(TempDir);
  2029. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  2030. {
  2031. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  2032. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  2033. // Skip directories, process only nested files.
  2034. // The check is redundant as FileAge fails for directories anyway.
  2035. !DirectoryExists(FileName))
  2036. {
  2037. UnicodeString RemoteDir =
  2038. UnixExtractFileDir(
  2039. UnixIncludeTrailingBackslash(FTerminal->CurrentDirectory) +
  2040. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2041. TDateTime NewTime;
  2042. if (FileAge(FileName, NewTime) &&
  2043. (NewTime != RemoteFileTimes[Index]))
  2044. {
  2045. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2046. TemporaryFileCopyParam(CopyParam);
  2047. CopyParam.FileMask = L"";
  2048. FAutoOperation = true;
  2049. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2050. TemporaryFilesList->Add(FileName);
  2051. FTerminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2052. }
  2053. }
  2054. }
  2055. }
  2056. }
  2057. __finally
  2058. {
  2059. FAutoOperation = false;
  2060. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2061. {
  2062. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2063. }
  2064. }
  2065. }
  2066. __finally
  2067. {
  2068. if (RemoteFileList != FileList)
  2069. {
  2070. delete RemoteFileList;
  2071. }
  2072. if (LocalFileList != ALocalFileList)
  2073. {
  2074. delete LocalFileList;
  2075. }
  2076. if (RemoteFileListFull != RemoteFileList)
  2077. {
  2078. delete RemoteFileListFull;
  2079. }
  2080. }
  2081. }
  2082. //---------------------------------------------------------------------------
  2083. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2084. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2085. bool FileListCommand, UnicodeString * POutput)
  2086. {
  2087. std::unique_ptr<TStrings> SelectedFileList(DirView(osLocal)->CreateFileList(false, true, NULL));
  2088. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2089. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2090. {
  2091. UnicodeString FileName = SelectedFileList->Strings[Index];
  2092. if (DirectoryExists(FileName))
  2093. {
  2094. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2095. {
  2096. LocalFileList->Add(FileName);
  2097. }
  2098. if (FLAGSET(ACommand.Params, ccRecursive))
  2099. {
  2100. TMakeLocalFileListParams MakeFileListParam;
  2101. MakeFileListParam.FileList = LocalFileList.get();
  2102. MakeFileListParam.FileTimes = NULL;
  2103. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2104. MakeFileListParam.Recursive = true;
  2105. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2106. }
  2107. }
  2108. else
  2109. {
  2110. LocalFileList->Add(FileName);
  2111. }
  2112. }
  2113. UnicodeString RemotePath;
  2114. if (Terminal != NULL)
  2115. {
  2116. RemotePath = Terminal->CurrentDirectory;
  2117. }
  2118. if (FileListCommand)
  2119. {
  2120. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2121. TLocalCustomCommand CustomCommand(
  2122. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2123. L"", L"", FileList);
  2124. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2125. }
  2126. else
  2127. {
  2128. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2129. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2130. DebugAssert(FProgressForm != NULL);
  2131. FProgressForm->ReadOnly = true;
  2132. try
  2133. {
  2134. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2135. {
  2136. UnicodeString FileName = LocalFileList->Strings[Index];
  2137. Progress.SetFile(FileName);
  2138. TLocalCustomCommand CustomCommand(
  2139. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2140. FileName, L"", L"");
  2141. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2142. TOnceDoneOperation OnceDoneOperation;
  2143. Progress.Finish(FileName, true, OnceDoneOperation);
  2144. }
  2145. }
  2146. __finally
  2147. {
  2148. Progress.Stop();
  2149. }
  2150. }
  2151. }
  2152. //---------------------------------------------------------------------------
  2153. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2154. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2155. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2156. {
  2157. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2158. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2159. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2160. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2161. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2162. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2163. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2164. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2165. std::unique_ptr<UnicodeString> POutput;
  2166. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2167. FLAGSET(ACommand.Params, ccCopyResults))
  2168. {
  2169. POutput.reset(new UnicodeString());
  2170. }
  2171. if (!LocalCustomCommand.IsFileCommand(Command))
  2172. {
  2173. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2174. }
  2175. // remote files?
  2176. else if ((FCurrentSide == osRemote) || LocalFileCommand)
  2177. {
  2178. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2179. }
  2180. // local files
  2181. else
  2182. {
  2183. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2184. }
  2185. if (POutput.get() != NULL)
  2186. {
  2187. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2188. int P = POutput->Pos(sLineBreak);
  2189. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2190. {
  2191. POutput->SetLength(P - 1);
  2192. }
  2193. // Copy even empty output to clipboard
  2194. if (FLAGSET(ACommand.Params, ccCopyResults))
  2195. {
  2196. CopyToClipboard(*POutput);
  2197. }
  2198. // But do not show an empty message box.
  2199. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2200. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2201. !POutput->IsEmpty())
  2202. {
  2203. TClipboardHandler ClipboardHandler;
  2204. ClipboardHandler.Text = *POutput;
  2205. TMessageParams Params;
  2206. TQueryButtonAlias Aliases[1];
  2207. Aliases[0].Button = qaRetry;
  2208. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2209. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2210. Params.Aliases = Aliases;
  2211. Params.AliasesCount = LENOF(Aliases);
  2212. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2213. }
  2214. }
  2215. }
  2216. //---------------------------------------------------------------------------
  2217. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2218. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2219. {
  2220. TCustomCommandData Data;
  2221. UnicodeString Site;
  2222. UnicodeString RemotePath;
  2223. if (HasActiveTerminal())
  2224. {
  2225. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  2226. Data = TCustomCommandData(SessionData.get());
  2227. Site = Terminal->SessionData->SessionKey;
  2228. RemotePath = Terminal->CurrentDirectory;
  2229. }
  2230. UnicodeString HelpKeyword = ACommand.HomePage;
  2231. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2232. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2233. {
  2234. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2235. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2236. {
  2237. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2238. }
  2239. else
  2240. {
  2241. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2242. }
  2243. if (!DoCustomCommandOptionsDialog(
  2244. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2245. {
  2246. Abort();
  2247. }
  2248. }
  2249. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2250. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2251. {
  2252. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2253. }
  2254. else
  2255. {
  2256. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2257. }
  2258. }
  2259. //---------------------------------------------------------------------------
  2260. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2261. const TCustomCommandType & Command)
  2262. {
  2263. DebugAssert(FCustomCommandLocalFileList != NULL);
  2264. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2265. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2266. TStrings * LocalFileList = new TStringList();
  2267. TStrings * RemoteFileList = new TStringList();
  2268. try
  2269. {
  2270. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2271. {
  2272. LocalFileList->Clear();
  2273. LocalFileList->AddObject(
  2274. FCustomCommandLocalFileList->Strings[Index],
  2275. FCustomCommandLocalFileList->Objects[Index]);
  2276. RemoteFileList->Clear();
  2277. RemoteFileList->AddObject(
  2278. FCustomCommandRemoteFileList->Strings[Index],
  2279. FCustomCommandRemoteFileList->Objects[Index]);
  2280. CustomCommand(RemoteFileList, Command, LocalFileList);
  2281. }
  2282. }
  2283. __finally
  2284. {
  2285. delete LocalFileList;
  2286. delete RemoteFileList;
  2287. }
  2288. }
  2289. //---------------------------------------------------------------------------
  2290. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2291. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2292. {
  2293. delete FCustomCommandLocalFileList;
  2294. delete FCustomCommandRemoteFileList;
  2295. // takeover ownership,
  2296. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2297. FCustomCommandLocalFileList = LocalFileList;
  2298. FCustomCommandRemoteFileList = RemoteFileList;
  2299. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2300. if (Button != NULL)
  2301. {
  2302. FCustomCommandMenu->Items->Clear();
  2303. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2304. MenuPopup(FCustomCommandMenu, Button);
  2305. }
  2306. else
  2307. {
  2308. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2309. }
  2310. }
  2311. //---------------------------------------------------------------------------
  2312. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2313. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2314. {
  2315. DebugAssert(FCapturedLog != NULL);
  2316. if ((OutputType == cotOutput) || (OutputType == cotError))
  2317. {
  2318. FCapturedLog->Add(AddedLine);
  2319. }
  2320. }
  2321. //---------------------------------------------------------------------------
  2322. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2323. TOperationSide Side)
  2324. {
  2325. return
  2326. (Side == osOther) &&
  2327. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2328. }
  2329. //---------------------------------------------------------------------------
  2330. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2331. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2332. {
  2333. TTBItemOptions O;
  2334. O = Item->Options;
  2335. TCustomDirView * DView = DirView(Side);
  2336. if ((DView->ItemFocused != NULL) &&
  2337. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2338. // when resolving links is disabled, default action is to enter the directory,
  2339. // no matter what DoubleClickAction is configured to
  2340. (IsSideLocalBrowser(Side) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2341. // Can only Edit files, but can Open/Copy even directories
  2342. ((DoubleClickAction != dcaEdit) ||
  2343. !DView->ItemIsDirectory(DView->ItemFocused)))
  2344. {
  2345. Item->Options = O << tboDefault;
  2346. }
  2347. else
  2348. {
  2349. Item->Options = O >> tboDefault;
  2350. }
  2351. }
  2352. //---------------------------------------------------------------------------
  2353. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2354. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2355. {
  2356. TCustomDirView * DView = DirView(Side);
  2357. TListItem * Item = DView->ItemFocused;
  2358. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2359. EnableFocusedOperation[Side])
  2360. {
  2361. TPoint ClientPoint;
  2362. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2363. TPoint(0, 0) : MousePos;
  2364. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2365. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2366. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2367. }
  2368. }
  2369. //---------------------------------------------------------------------------
  2370. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2371. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2372. {
  2373. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2374. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2375. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2376. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2377. Handled = true;
  2378. }
  2379. //---------------------------------------------------------------------------
  2380. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2381. {
  2382. }
  2383. //---------------------------------------------------------------------------
  2384. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2385. {
  2386. DebugAssert(FErrorList == NULL);
  2387. if (WinConfiguration->ContinueOnError)
  2388. {
  2389. FErrorList = new TStringList();
  2390. Configuration->Usage->Inc(L"ContinuationsOnError");
  2391. }
  2392. NonVisualDataModule->StartBusy();
  2393. }
  2394. //---------------------------------------------------------------------------
  2395. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2396. {
  2397. NonVisualDataModule->EndBusy();
  2398. if (FErrorList)
  2399. {
  2400. HandleErrorList(FErrorList);
  2401. }
  2402. }
  2403. //---------------------------------------------------------------------------
  2404. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2405. const TCopyParamType & CopyParam)
  2406. {
  2407. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2408. {
  2409. Configuration->Usage->Inc(L"FileMaskUses");
  2410. }
  2411. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2412. {
  2413. Configuration->Usage->Inc(L"OperationMaskUses");
  2414. }
  2415. }
  2416. //---------------------------------------------------------------------------
  2417. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2418. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2419. {
  2420. DebugAssert(!IsLocalBrowserMode());
  2421. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2422. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2423. TTransferOperationParam DefaultParam;
  2424. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2425. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2426. switch (Param.Queue)
  2427. {
  2428. case asOn:
  2429. CopyParam.Queue = true;
  2430. break;
  2431. case asOff:
  2432. CopyParam.Queue = false;
  2433. break;
  2434. case asAuto:
  2435. default:
  2436. // keep default
  2437. break;
  2438. }
  2439. bool Result =
  2440. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2441. if (Result)
  2442. {
  2443. DebugAssert(Terminal);
  2444. bool SelectionRestored = false;
  2445. TCustomDirView * DView = NULL;
  2446. if (HasDirView[Side])
  2447. {
  2448. DView = DirView(Side);
  2449. DView->SaveSelection();
  2450. DView->SaveSelectedNames();
  2451. }
  2452. UpdateCopyParamCounters(CopyParam);
  2453. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2454. DebugAssert(FTransferResumeList == NULL);
  2455. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2456. FMoveToQueue = false;
  2457. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2458. try
  2459. {
  2460. TStrings * PermanentFileList;
  2461. std::unique_ptr<TStrings> PermanentFileListOwner;
  2462. try
  2463. {
  2464. if (Side == osLocal)
  2465. {
  2466. PermanentFileList = FileList;
  2467. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2468. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2469. if (Operation == foMove)
  2470. {
  2471. ReloadLocalDirectory();
  2472. if (DView != NULL)
  2473. {
  2474. DView->RestoreSelection();
  2475. }
  2476. SelectionRestored = true;
  2477. }
  2478. }
  2479. else
  2480. {
  2481. // Clone the file list as it may refer to current directory files,
  2482. // which get destroyed, when the source directory is reloaded after foMove operation.
  2483. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2484. // But for a hotfix, we are not going to do such a big change.
  2485. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2486. PermanentFileList = PermanentFileListOwner.get();
  2487. try
  2488. {
  2489. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2490. }
  2491. __finally
  2492. {
  2493. if (Operation == foMove)
  2494. {
  2495. if (DView != NULL)
  2496. {
  2497. DView->RestoreSelection();
  2498. }
  2499. SelectionRestored = true;
  2500. }
  2501. ReloadLocalDirectory(Param.TargetDirectory);
  2502. }
  2503. }
  2504. // When the transfer batch is aborted between individual file transfers, the Abort is not called.
  2505. if (FMoveToQueue)
  2506. {
  2507. Abort();
  2508. }
  2509. }
  2510. catch (EAbort &)
  2511. {
  2512. if (FMoveToQueue)
  2513. {
  2514. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2515. DebugAssert(CopyParam.TransferSkipList == NULL);
  2516. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2517. if (TransferResumeList->Count > 0)
  2518. {
  2519. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2520. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2521. }
  2522. CopyParam.TransferSkipList = TransferResumeList.release();
  2523. // not really needed, just to keep it consistent with TransferResumeList
  2524. FTransferResumeList = NULL;
  2525. FMoveToQueue = false;
  2526. Configuration->Usage->Inc("MovesToBackground");
  2527. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2528. ClearTransferSourceSelection(Direction);
  2529. }
  2530. throw;
  2531. }
  2532. }
  2533. __finally
  2534. {
  2535. if (!SelectionRestored && (DView != NULL))
  2536. {
  2537. DView->DiscardSavedSelection();
  2538. }
  2539. FTransferResumeList = NULL;
  2540. }
  2541. }
  2542. return Result;
  2543. }
  2544. //---------------------------------------------------------------------------
  2545. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2546. TOperationSide Side, TStrings * FileList, void * Param)
  2547. {
  2548. DebugAssert(FileList->Count);
  2549. // We deliberately do not toggle alternative flag (Param), but use OR,
  2550. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2551. // shortcut of CurrentDeleteAlternativeAction
  2552. bool Alternative =
  2553. bool(Param) || UseAlternativeFunction();
  2554. bool Recycle;
  2555. if (IsSideLocalBrowser(Side))
  2556. {
  2557. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2558. }
  2559. else
  2560. {
  2561. Recycle =
  2562. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2563. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2564. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2565. }
  2566. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2567. if (!Result)
  2568. {
  2569. UnicodeString Query;
  2570. if (FileList->Count == 1)
  2571. {
  2572. if (IsSideLocalBrowser(Side))
  2573. {
  2574. Query = ExtractFileName(FileList->Strings[0]);
  2575. }
  2576. else
  2577. {
  2578. Query = UnixExtractFileName(FileList->Strings[0]);
  2579. }
  2580. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2581. }
  2582. else
  2583. {
  2584. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2585. }
  2586. TMessageParams Params(mpNeverAskAgainCheck);
  2587. Params.ImageName = L"Delete file";
  2588. unsigned int Answer =
  2589. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2590. if (Answer == qaNeverAskAgain)
  2591. {
  2592. Result = true;
  2593. if (Recycle)
  2594. {
  2595. WinConfiguration->ConfirmRecycling = false;
  2596. }
  2597. else
  2598. {
  2599. WinConfiguration->ConfirmDeleting = false;
  2600. }
  2601. }
  2602. else
  2603. {
  2604. Result = (Answer == qaOK);
  2605. }
  2606. }
  2607. if (Result)
  2608. {
  2609. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2610. }
  2611. return Result;
  2612. }
  2613. //---------------------------------------------------------------------------
  2614. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2615. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2616. {
  2617. TAutoBatch AutoBatch(this);
  2618. bool Result;
  2619. if ((Operation == foCopy) || (Operation == foMove))
  2620. {
  2621. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2622. }
  2623. else if (Operation == foRename)
  2624. {
  2625. DebugAssert(DirView(Side)->ItemFocused);
  2626. DirView(Side)->ItemFocused->EditCaption();
  2627. Result = true;
  2628. }
  2629. else if (Operation == foDelete)
  2630. {
  2631. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2632. }
  2633. else if (Operation == foSetProperties)
  2634. {
  2635. DirView(osRemote)->SaveSelectedNames();
  2636. Result = SetProperties(Side, FileList);
  2637. }
  2638. else if (Operation == foCustomCommand)
  2639. {
  2640. DebugAssert(Param);
  2641. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2642. DirView(osRemote)->SaveSelectedNames();
  2643. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2644. CustomCommand(FileList, *Command, NULL);
  2645. Result = true;
  2646. }
  2647. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2648. {
  2649. DebugAssert(!IsSideLocalBrowser(Side));
  2650. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2651. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2652. }
  2653. else if (Operation == foLock)
  2654. {
  2655. DebugAssert(!IsSideLocalBrowser(Side));
  2656. LockFiles(FileList, true);
  2657. Result = true;
  2658. }
  2659. else if (Operation == foUnlock)
  2660. {
  2661. DebugAssert(!IsSideLocalBrowser(Side));
  2662. LockFiles(FileList, false);
  2663. Result = true;
  2664. }
  2665. else
  2666. {
  2667. DebugFail();
  2668. }
  2669. return Result;
  2670. }
  2671. //---------------------------------------------------------------------------
  2672. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2673. {
  2674. if (Side == osCurrent)
  2675. {
  2676. Side = FCurrentSide;
  2677. }
  2678. return Side;
  2679. }
  2680. //---------------------------------------------------------------------------
  2681. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2682. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2683. {
  2684. Side = GetSide(Side);
  2685. bool Result;
  2686. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, IsSideLocalBrowser(Side), NULL);
  2687. try
  2688. {
  2689. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2690. }
  2691. __finally
  2692. {
  2693. delete FileList;
  2694. }
  2695. return Result;
  2696. }
  2697. //---------------------------------------------------------------------------
  2698. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2699. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2700. bool NoConfirmation, void * Param)
  2701. {
  2702. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2703. {
  2704. if ((Operation == foCopy) || (Operation == foMove))
  2705. {
  2706. DebugAssert(!IsLocalBrowserMode());
  2707. if (GetSide(Side) == osLocal)
  2708. {
  2709. Configuration->Usage->Inc(L"UploadsCommand");
  2710. }
  2711. else
  2712. {
  2713. Configuration->Usage->Inc(L"DownloadsCommand");
  2714. }
  2715. }
  2716. }
  2717. }
  2718. //---------------------------------------------------------------------------
  2719. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2720. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2721. {
  2722. TTransferOperationParam Param;
  2723. if ((WinConfiguration->Interface != ifCommander) ||
  2724. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2725. {
  2726. Flags &= ~cocShortCutHint;
  2727. }
  2728. TCustomDirView * DView = DirView(Side);
  2729. Param.Options =
  2730. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2731. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2732. if (FLAGSET(Flags, cocQueue))
  2733. {
  2734. Param.Queue = asOn;
  2735. }
  2736. else if (FLAGSET(Flags, cocNonQueue))
  2737. {
  2738. Param.Queue = asOff;
  2739. }
  2740. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2741. }
  2742. //---------------------------------------------------------------------------
  2743. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2744. {
  2745. try
  2746. {
  2747. if (ErrorList->Count)
  2748. {
  2749. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2750. if (MessageDialog(Message, qtError,
  2751. qaOK | qaCancel, HELP_NONE) == qaOK)
  2752. {
  2753. unsigned int Answer;
  2754. int Index = 0;
  2755. do
  2756. {
  2757. DebugAssert(Index >= 0 && Index < ErrorList->Count);
  2758. TQueryButtonAlias Aliases[2];
  2759. Aliases[0].Button = qaYes;
  2760. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2761. Aliases[1].Button = qaNo;
  2762. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2763. TMessageParams Params;
  2764. Params.Aliases = Aliases;
  2765. Params.AliasesCount = LENOF(Aliases);
  2766. Answer = MoreMessageDialog(
  2767. FMTLOAD(ERROR_LIST_NUMBER, (Index+1, ErrorList->Count, ErrorList->Strings[Index])),
  2768. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2769. (Index ? qaYes : 0) | (Index < ErrorList->Count - 1 ? qaNo : 0) |
  2770. qaOK, HELP_NONE, &Params);
  2771. if (Answer == qaNo)
  2772. {
  2773. Index++;
  2774. }
  2775. if (Answer == qaYes)
  2776. {
  2777. Index--;
  2778. }
  2779. }
  2780. while (Answer != qaOK);
  2781. }
  2782. }
  2783. }
  2784. __finally
  2785. {
  2786. TStrings * List = ErrorList;
  2787. ErrorList = NULL;
  2788. for (int i = 0; i < List->Count; i++)
  2789. {
  2790. delete List->Objects[i];
  2791. }
  2792. delete List;
  2793. }
  2794. }
  2795. //---------------------------------------------------------------------------
  2796. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2797. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2798. {
  2799. // needed for checking filemasks, as there's no directory object
  2800. // associated with the file object
  2801. File->FullFileName = FullFileName;
  2802. TFileMasks::TParams MaskParams;
  2803. MaskParams.Size = File->Size;
  2804. MaskParams.Modification = File->Modification;
  2805. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2806. }
  2807. //---------------------------------------------------------------------------
  2808. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2809. {
  2810. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2811. Side = GetSide(Side);
  2812. TCustomDirView * DView = DirView(Side);
  2813. TListItem * FocusedItem = DView->ItemFocused;
  2814. UnicodeString Name;
  2815. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2816. {
  2817. Name = DView->ItemFileName(FocusedItem);
  2818. }
  2819. else
  2820. {
  2821. Name = LoadStr(NEW_FILE);
  2822. }
  2823. UnicodeString Names = Name;
  2824. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2825. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2826. HELP_EDIT_NEW, History.get(), true, NULL, true, 400))
  2827. {
  2828. while (!Names.IsEmpty())
  2829. {
  2830. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2831. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2832. UnicodeString TargetFileName;
  2833. UnicodeString LocalFileName;
  2834. UnicodeString RootTempDir;
  2835. UnicodeString TempDir;
  2836. UnicodeString RemoteDirectory;
  2837. bool ExistingFile = false;
  2838. if (!IsSideLocalBrowser(Side))
  2839. {
  2840. Name = AbsolutePath(FTerminal->CurrentDirectory, Name);
  2841. TRemoteFile * File = NULL;
  2842. if (FTerminal->FileExists(Name, &File) &&
  2843. !File->IsDirectory)
  2844. {
  2845. try
  2846. {
  2847. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2848. ExistingFile = true;
  2849. }
  2850. __finally
  2851. {
  2852. delete File;
  2853. }
  2854. }
  2855. if (!ExistingFile)
  2856. {
  2857. RemoteDirectory = UnixExtractFilePath(Name);
  2858. TemporaryDirectoryForRemoteFiles(
  2859. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2860. TargetFileName = UnixExtractFileName(Name);
  2861. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2862. LocalFileName = TempDir +
  2863. // We probably do not want to trim the VMS version here
  2864. FTerminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2865. }
  2866. }
  2867. else
  2868. {
  2869. if (ExtractFilePath(Name).IsEmpty())
  2870. {
  2871. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2872. }
  2873. else
  2874. {
  2875. LocalFileName = ExpandFileName(Name);
  2876. }
  2877. TargetFileName = ExtractFileName(Name);
  2878. }
  2879. if (!ExistingFile)
  2880. {
  2881. bool NewFile = !FileExists(ApiPath(LocalFileName));
  2882. if (NewFile)
  2883. {
  2884. int File = FileCreate(ApiPath(LocalFileName));
  2885. if (File < 0)
  2886. {
  2887. if (!RootTempDir.IsEmpty())
  2888. {
  2889. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2890. }
  2891. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2892. }
  2893. else
  2894. {
  2895. FileClose(File);
  2896. }
  2897. }
  2898. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2899. const TEditorData * ExternalEditor = NULL;
  2900. TFileMasks::TParams MaskParams; // size not known
  2901. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2902. false, MaskParams);
  2903. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2904. ExternalEditor, RootTempDir, RemoteDirectory, NewFile);
  2905. }
  2906. }
  2907. }
  2908. }
  2909. //---------------------------------------------------------------------------
  2910. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2911. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2912. {
  2913. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2914. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2915. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2916. return
  2917. ((ExecuteFileBy == efInternalEditor)) ||
  2918. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2919. }
  2920. //---------------------------------------------------------------------------
  2921. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2922. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2923. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2924. UnicodeString RemoteDirectory, bool NewFile)
  2925. {
  2926. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2927. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2928. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2929. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2930. Side = GetSide(Side);
  2931. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2932. if (!IsSideLocalBrowser(Side))
  2933. {
  2934. Data->Terminal = Terminal;
  2935. Data->Queue = Queue;
  2936. Data->SessionData = CloneCurrentSessionData();
  2937. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2938. Data->RemoteDirectory = RemoteDirectory;
  2939. Data->SessionName = Terminal->SessionData->SessionName;
  2940. Data->LocalRootDirectory = LocalRootDirectory;
  2941. Data->OriginalFileName = OriginalFileName;
  2942. Data->Command = L""; // will be changed later for external editor
  2943. }
  2944. if (ExecuteFileBy == efInternalEditor)
  2945. {
  2946. if (!IsSideLocalBrowser(Side))
  2947. {
  2948. UnicodeString Caption = UnixIncludeTrailingBackslash(RemoteDirectory) + OriginalFileName +
  2949. L" - " + Terminal->SessionData->SessionName;
  2950. TForm * Editor;
  2951. try
  2952. {
  2953. TNotifyEvent OnFileChanged = NULL;
  2954. TNotifyEvent OnSaveAll = NULL;
  2955. TAnyModifiedEvent OnAnyModified = NULL;
  2956. // Edited files are uploaded in background queue, what we do not support with encrypted files
  2957. if (Terminal->IsCapable[fcBackgroundTransfers])
  2958. {
  2959. OnFileChanged = FEditorManager->FileChanged;
  2960. OnSaveAll = SaveAllInternalEditors;
  2961. OnAnyModified = AnyInternalEditorModified;
  2962. }
  2963. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  2964. FEditorManager->FileReload, FEditorManager->FileClosed,
  2965. OnSaveAll, OnAnyModified,
  2966. Caption, StandaloneOperation, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  2967. }
  2968. catch(...)
  2969. {
  2970. if (!LocalRootDirectory.IsEmpty())
  2971. {
  2972. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  2973. }
  2974. throw;
  2975. }
  2976. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  2977. }
  2978. else
  2979. {
  2980. DebugAssert(!StandaloneOperation);
  2981. TForm * Editor =
  2982. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  2983. SaveAllInternalEditors, AnyInternalEditorModified,
  2984. L"", false, SessionColor, -1, NewFile);
  2985. FLocalEditors->Add(Editor);
  2986. }
  2987. }
  2988. else
  2989. {
  2990. HANDLE Process;
  2991. if (ExecuteFileBy == efExternalEditor)
  2992. {
  2993. UnicodeString Program, Params, Dir;
  2994. Data->Command = ExternalEditor->ExternalEditor;
  2995. ReformatFileNameCommand(Data->Command);
  2996. SplitCommand(Data->Command, Program, Params, Dir);
  2997. Params = ExpandFileNameCommand(Params, FileName);
  2998. Program = ExpandEnvironmentVariables(Program);
  2999. if (!ExecuteShell(Program, Params, Process))
  3000. {
  3001. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  3002. }
  3003. }
  3004. else
  3005. {
  3006. DebugAssert(!IsSideLocalBrowser(Side));
  3007. if (!ExecuteShell(FileName, L"", Process))
  3008. {
  3009. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  3010. }
  3011. }
  3012. if (IsSideLocalBrowser(Side) ||
  3013. ((ExecuteFileBy == efShell) &&
  3014. !WinConfiguration->Editor.SDIShellEditor) ||
  3015. ((ExecuteFileBy == efExternalEditor) &&
  3016. !ExternalEditor->SDIExternalEditor))
  3017. {
  3018. // no need for handle
  3019. if (Process != NULL)
  3020. {
  3021. DebugCheck(CloseHandle(Process));
  3022. }
  3023. Process = INVALID_HANDLE_VALUE;
  3024. }
  3025. else
  3026. {
  3027. if (Process == NULL)
  3028. {
  3029. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  3030. }
  3031. }
  3032. if (!IsSideLocalBrowser(Side))
  3033. {
  3034. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  3035. }
  3036. }
  3037. }
  3038. //---------------------------------------------------------------------------
  3039. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3040. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3041. void * /*Arg*/)
  3042. {
  3043. if (Token != NULL)
  3044. {
  3045. EditorFormFileSave(static_cast<TForm *>(Token));
  3046. }
  3047. }
  3048. //---------------------------------------------------------------------------
  3049. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3050. {
  3051. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3052. {
  3053. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3054. }
  3055. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3056. }
  3057. //---------------------------------------------------------------------------
  3058. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3059. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3060. void * Arg)
  3061. {
  3062. if ((Token != NULL) &&
  3063. IsEditorFormModified(static_cast<TForm *>(Token)))
  3064. {
  3065. *static_cast<bool *>(Arg) = true;
  3066. }
  3067. }
  3068. //---------------------------------------------------------------------------
  3069. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3070. TObject * /*Sender*/, bool & Modified)
  3071. {
  3072. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3073. {
  3074. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3075. {
  3076. Modified = true;
  3077. }
  3078. }
  3079. if (!Modified)
  3080. {
  3081. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3082. }
  3083. }
  3084. //---------------------------------------------------------------------------
  3085. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3086. {
  3087. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3088. }
  3089. //---------------------------------------------------------------------------
  3090. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3091. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3092. UnicodeString & Result, UnicodeString & RootDirectory)
  3093. {
  3094. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3095. {
  3096. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3097. Result = RootDirectory;
  3098. }
  3099. else
  3100. {
  3101. RootDirectory = L"";
  3102. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3103. Result = IncludeTrailingBackslash(Result);
  3104. }
  3105. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3106. {
  3107. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3108. }
  3109. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3110. {
  3111. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3112. {
  3113. RemoteDirectory.Delete(1, 1);
  3114. }
  3115. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3116. }
  3117. if (!ForceDirectories(ApiPath(Result)))
  3118. {
  3119. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3120. }
  3121. }
  3122. //---------------------------------------------------------------------------
  3123. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3124. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3125. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  3126. {
  3127. DebugAssert(!IsLocalBrowserMode());
  3128. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3129. if (ForceText)
  3130. {
  3131. CopyParam.TransferMode = tmAscii;
  3132. }
  3133. TemporaryFileCopyParam(CopyParam);
  3134. if (AllFiles)
  3135. {
  3136. CopyParam.IncludeFileMask = TFileMasks();
  3137. }
  3138. if (TempDir.IsEmpty())
  3139. {
  3140. TemporaryDirectoryForRemoteFiles(FTerminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3141. }
  3142. DebugAssert(!FAutoOperation);
  3143. FAutoOperation = AutoOperation;
  3144. Terminal->ExceptionOnFail = true;
  3145. try
  3146. {
  3147. try
  3148. {
  3149. // turn off confirmations, as for MDI editors we may possibly download
  3150. // the same file over
  3151. Terminal->CopyToLocal(
  3152. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3153. if (GetTargetNames)
  3154. {
  3155. for (int i = 0; i < FileList->Count; i++)
  3156. {
  3157. FileList->Strings[i] =
  3158. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3159. }
  3160. }
  3161. }
  3162. catch(...)
  3163. {
  3164. if (!RootTempDir.IsEmpty())
  3165. {
  3166. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3167. }
  3168. throw;
  3169. }
  3170. }
  3171. __finally
  3172. {
  3173. FAutoOperation = false;
  3174. Terminal->ExceptionOnFail = false;
  3175. }
  3176. }
  3177. //---------------------------------------------------------------------------
  3178. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3179. {
  3180. // Do not waste time checking for default editor list the next time,
  3181. // or testing if default editor is not notepad.
  3182. bool TryNextTime = false;
  3183. UnicodeString UsageState;
  3184. if (!WinConfiguration->EditorList->IsDefaultList())
  3185. {
  3186. UsageState = "H";
  3187. }
  3188. else
  3189. {
  3190. UnicodeString Executable;
  3191. UnicodeString ExecutableDescription;
  3192. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3193. {
  3194. UnicodeString Message =
  3195. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3196. unsigned int Answer =
  3197. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3198. if (Answer != qaOK)
  3199. {
  3200. UsageState = "R";
  3201. }
  3202. else
  3203. {
  3204. UsageState = "A";
  3205. TEditorData EditorData;
  3206. EditorData.Editor = edExternal;
  3207. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3208. EditorData.ExternalEditorOptionsAutodetect();
  3209. TEditorList EditorList;
  3210. EditorList = *WinConfiguration->EditorList;
  3211. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3212. WinConfiguration->EditorList = &EditorList;
  3213. }
  3214. }
  3215. }
  3216. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3217. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3218. }
  3219. //---------------------------------------------------------------------------
  3220. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3221. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3222. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3223. {
  3224. if (ExecuteFileBy == efDefaultEditor)
  3225. {
  3226. if (!WinConfiguration->OfferedEditorAutoConfig)
  3227. {
  3228. EditorAutoConfig();
  3229. }
  3230. const TEditorPreferences * Editor =
  3231. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3232. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3233. {
  3234. ExecuteFileBy = efInternalEditor;
  3235. ExternalEditor = NULL;
  3236. }
  3237. else if (Editor->Data->Editor == edOpen)
  3238. {
  3239. ExecuteFileBy = efShell;
  3240. ExternalEditor = NULL;
  3241. }
  3242. else
  3243. {
  3244. ExecuteFileBy = efExternalEditor;
  3245. ExternalEditor = Editor->Data;
  3246. }
  3247. }
  3248. }
  3249. //---------------------------------------------------------------------------
  3250. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3251. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3252. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3253. {
  3254. UnicodeString OriginalFileName;
  3255. UnicodeString LocalRootDirectory;
  3256. UnicodeString RemoteDirectory;
  3257. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3258. IsSideLocalBrowser(Side), MaskParams);
  3259. UnicodeString Counter;
  3260. UnicodeString LocalFileName;
  3261. bool Handled = false;
  3262. if (!IsSideLocalBrowser(Side))
  3263. {
  3264. // We need to trim VMS version here, so that we use name without version
  3265. // when uploading back to create a new version of the file
  3266. OriginalFileName = FTerminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3267. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3268. TObject * Token = NULL;
  3269. UnicodeString LocalDirectory;
  3270. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3271. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3272. LocalDirectory))
  3273. {
  3274. if (Token != NULL)
  3275. {
  3276. TForm * Form = dynamic_cast<TForm *>(Token);
  3277. if (Form->WindowState == wsMinimized)
  3278. {
  3279. ShowWindow(Form->Handle, SW_RESTORE);
  3280. }
  3281. else
  3282. {
  3283. Form->SetFocus();
  3284. }
  3285. Handled = true;
  3286. }
  3287. else
  3288. {
  3289. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3290. }
  3291. }
  3292. if (!Handled)
  3293. {
  3294. TStringList * FileList1 = new TStringList();
  3295. try
  3296. {
  3297. FileList1->AddObject(FullFileName, Object);
  3298. TemporarilyDownloadFiles(FileList1,
  3299. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3300. LocalRootDirectory, LocalDirectory, true, true, true);
  3301. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3302. }
  3303. __finally
  3304. {
  3305. delete FileList1;
  3306. }
  3307. switch (ExecuteFileBy)
  3308. {
  3309. case efShell:
  3310. Counter = "RemoteFilesExecuted";
  3311. break;
  3312. case efInternalEditor:
  3313. Counter = "RemoteFilesOpenedInInternalEditor";
  3314. break;
  3315. case efExternalEditor:
  3316. Counter = "RemoteFilesOpenedInExternalEditor";
  3317. break;
  3318. default:
  3319. DebugFail();
  3320. }
  3321. }
  3322. }
  3323. else
  3324. {
  3325. LocalFileName = FullFileName;
  3326. OriginalFileName = ExtractFileName(FullFileName);
  3327. switch (ExecuteFileBy)
  3328. {
  3329. case efShell:
  3330. Counter = "LocalFilesExecuted";
  3331. break;
  3332. case efInternalEditor:
  3333. Counter = "LocalFilesOpenedInInternalEditor";
  3334. break;
  3335. case efExternalEditor:
  3336. Counter = "LocalFilesOpenedInExternalEditor";
  3337. break;
  3338. default:
  3339. DebugFail();
  3340. }
  3341. }
  3342. if (!Handled)
  3343. {
  3344. Configuration->Usage->Inc(Counter);
  3345. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3346. ExternalEditor, LocalRootDirectory, RemoteDirectory, false);
  3347. }
  3348. }
  3349. //---------------------------------------------------------------------------
  3350. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3351. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3352. bool AllSelected, bool OnFocused)
  3353. {
  3354. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3355. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3356. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3357. Side = GetSide(Side);
  3358. TCustomDirView * DView = DirView(Side);
  3359. TStrings * FileList =
  3360. AllSelected ?
  3361. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3362. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3363. try
  3364. {
  3365. DebugAssert(AllSelected || (FileList->Count == 1));
  3366. for (int i = 0; i < FileList->Count; i++)
  3367. {
  3368. UnicodeString ListFileName = FileList->Strings[i];
  3369. UnicodeString FileNameOnly =
  3370. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3371. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3372. if (!DView->ItemIsDirectory(Item))
  3373. {
  3374. UnicodeString FullFileName;
  3375. if (!IsSideLocalBrowser(Side))
  3376. {
  3377. FullFileName = DirView(Side)->Path + ListFileName;
  3378. }
  3379. else
  3380. {
  3381. FullFileName = ListFileName;
  3382. }
  3383. TFileMasks::TParams MaskParams;
  3384. MaskParams.Size = DView->ItemFileSize(Item);
  3385. TDateTimePrecision Precision;
  3386. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3387. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3388. FileList->Objects[i], MaskParams);
  3389. }
  3390. }
  3391. }
  3392. __finally
  3393. {
  3394. delete FileList;
  3395. }
  3396. }
  3397. //---------------------------------------------------------------------------
  3398. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3399. {
  3400. CopyParam.FileNameCase = ncNoChange;
  3401. CopyParam.PreserveRights = false;
  3402. CopyParam.PreserveReadOnly = false;
  3403. CopyParam.ReplaceInvalidChars = true;
  3404. CopyParam.IncludeFileMask = TFileMasks();
  3405. CopyParam.NewerOnly = false;
  3406. CopyParam.FileMask = L"";
  3407. }
  3408. //---------------------------------------------------------------------------
  3409. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3410. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3411. {
  3412. TTerminalManager * Manager = TTerminalManager::Instance();
  3413. if (!IsActiveTerminal(Data->Terminal))
  3414. {
  3415. if (!NonVisualDataModule->Busy)
  3416. {
  3417. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3418. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3419. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3420. if (Data->Terminal == NULL)
  3421. {
  3422. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3423. if (SameSiteTerminal != NULL)
  3424. {
  3425. UnicodeString Message =
  3426. FMTLOAD(EDIT_SESSION_REATTACH,
  3427. (FileNameOnly, Data->SessionName, FileNameOnly));
  3428. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3429. {
  3430. Data->Terminal = Terminal;
  3431. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3432. Data->SessionName = Terminal->SessionData->SessionName;
  3433. // We might also overwrite session data
  3434. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3435. }
  3436. else
  3437. {
  3438. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3439. Abort();
  3440. }
  3441. }
  3442. }
  3443. // foreground session should reconnect itself
  3444. else if (Terminal != Data->Terminal)
  3445. {
  3446. UnicodeString Message =
  3447. MainInstructions(
  3448. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3449. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3450. {
  3451. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3452. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3453. }
  3454. else
  3455. {
  3456. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3457. Abort();
  3458. }
  3459. }
  3460. }
  3461. if (!IsActiveTerminal(Data->Terminal))
  3462. {
  3463. Configuration->Usage->Inc(L"EditInactiveSession");
  3464. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3465. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3466. (ExtractFileName(FileName), Data->SessionName)));
  3467. }
  3468. }
  3469. TStrings * FileList = new TStringList();
  3470. try
  3471. {
  3472. FileList->Add(FileName);
  3473. // consider using the same settings (preset) as when the file was downloaded
  3474. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3475. TemporaryFileCopyParam(CopyParam);
  3476. if (Data->ForceText)
  3477. {
  3478. CopyParam.TransferMode = tmAscii;
  3479. }
  3480. // so i do not need to worry if masking algorithm works in all cases
  3481. // ("" means "copy file name", no masking is actually done)
  3482. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3483. {
  3484. CopyParam.FileMask = L"";
  3485. }
  3486. else
  3487. {
  3488. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3489. }
  3490. DebugAssert(Data->Queue != NULL);
  3491. int Params = cpNoConfirmation | cpTemporary;
  3492. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3493. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3494. QueueItem->CompleteEvent = UploadCompleteEvent;
  3495. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3496. }
  3497. __finally
  3498. {
  3499. delete FileList;
  3500. }
  3501. }
  3502. //---------------------------------------------------------------------------
  3503. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3504. const UnicodeString FileName, const TEditedFileData * Data)
  3505. {
  3506. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3507. DebugAssert(!NonVisualDataModule->Busy);
  3508. if (!IsActiveTerminal(Data->Terminal))
  3509. {
  3510. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3511. (ExtractFileName(FileName), Data->SessionName)));
  3512. }
  3513. TManagedTerminal * PrevTerminal = TTerminalManager::Instance()->ActiveTerminal;
  3514. TTerminalManager::Instance()->ActiveTerminal = Data->Terminal;
  3515. NonVisualDataModule->StartBusy();
  3516. try
  3517. {
  3518. std::unique_ptr<TRemoteFile> File;
  3519. UnicodeString RemoteFileName =
  3520. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3521. FTerminal->ExceptionOnFail = true;
  3522. try
  3523. {
  3524. TRemoteFile * AFile = NULL;
  3525. FTerminal->ReadFile(RemoteFileName, AFile);
  3526. File.reset(AFile);
  3527. if (!File->HaveFullFileName)
  3528. {
  3529. File->FullFileName = RemoteFileName;
  3530. }
  3531. }
  3532. __finally
  3533. {
  3534. FTerminal->ExceptionOnFail = false;
  3535. }
  3536. std::unique_ptr<TStrings> FileList(new TStringList());
  3537. FileList->AddObject(RemoteFileName, File.get());
  3538. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3539. UnicodeString TempDir = ExtractFilePath(FileName);
  3540. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3541. TempDir, true, true, true);
  3542. // sanity check, the target file name should be still the same
  3543. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3544. }
  3545. __finally
  3546. {
  3547. NonVisualDataModule->EndBusy();
  3548. // it actually may not exist anymore...
  3549. TTerminalManager::Instance()->ActiveTerminal = PrevTerminal;
  3550. }
  3551. }
  3552. //---------------------------------------------------------------------------
  3553. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3554. const TEditedFileData * Data, bool & KeepOpen)
  3555. {
  3556. // Command is set for external editors only (not for "shell" open).
  3557. if (!Data->Command.IsEmpty())
  3558. {
  3559. bool AnyFound = false;
  3560. bool AnyMDI = false;
  3561. bool AnyNonMDI = false;
  3562. bool AnyDetect = false;
  3563. TEditorList * EditorList = new TEditorList();
  3564. try
  3565. {
  3566. *EditorList = *WinConfiguration->EditorList;
  3567. for (int i = 0; i < EditorList->Count; i++)
  3568. {
  3569. const TEditorPreferences * Editor = EditorList->Editors[i];
  3570. if ((Editor->Data->Editor == edExternal) &&
  3571. (Editor->Data->ExternalEditor == Data->Command))
  3572. {
  3573. AnyFound = true;
  3574. if (Editor->Data->SDIExternalEditor)
  3575. {
  3576. AnyNonMDI = true;
  3577. if (Editor->Data->DetectMDIExternalEditor)
  3578. {
  3579. AnyDetect = true;
  3580. }
  3581. }
  3582. else
  3583. {
  3584. AnyMDI = true;
  3585. }
  3586. }
  3587. }
  3588. bool EnableMDI = false;
  3589. bool DisableDetect = false;
  3590. if (AnyMDI)
  3591. {
  3592. KeepOpen = true;
  3593. if (AnyNonMDI)
  3594. {
  3595. // there is at least one instance of the editor with MDI support enabled,
  3596. // and one with disabled, enable it for all instances
  3597. EnableMDI = true;
  3598. }
  3599. }
  3600. else if (AnyFound && !AnyDetect)
  3601. {
  3602. // at least once instance found but all have MDI autodetection disabled
  3603. // => close the file (default action)
  3604. }
  3605. else
  3606. {
  3607. // no instance of the editor has MDI support enabled
  3608. TMessageParams Params;
  3609. if (AnyFound)
  3610. {
  3611. // there is at least one instance of the editor
  3612. Params.Params |= mpNeverAskAgainCheck;
  3613. }
  3614. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3615. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3616. switch (Answer)
  3617. {
  3618. case qaNeverAskAgain:
  3619. DisableDetect = true;
  3620. break;
  3621. case qaNo:
  3622. EnableMDI = true;
  3623. KeepOpen = true;
  3624. break;
  3625. }
  3626. }
  3627. if (AnyFound && (EnableMDI || DisableDetect))
  3628. {
  3629. bool Changed = false;
  3630. for (int i = 0; i < EditorList->Count; i++)
  3631. {
  3632. const TEditorPreferences * Editor = EditorList->Editors[i];
  3633. if ((Editor->Data->Editor == edExternal) &&
  3634. (Editor->Data->ExternalEditor == Data->Command) &&
  3635. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3636. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3637. {
  3638. Changed = true;
  3639. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3640. if (EnableMDI)
  3641. {
  3642. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3643. }
  3644. if (DisableDetect)
  3645. {
  3646. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3647. }
  3648. EditorList->Change(i, UpdatedEditor);
  3649. }
  3650. }
  3651. if (Changed)
  3652. {
  3653. WinConfiguration->EditorList = EditorList;
  3654. }
  3655. }
  3656. }
  3657. __finally
  3658. {
  3659. delete EditorList;
  3660. }
  3661. }
  3662. else
  3663. {
  3664. // "open" case
  3665. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3666. qaOK, HELP_APP_EARLY_CLOSED);
  3667. }
  3668. }
  3669. //---------------------------------------------------------------------------
  3670. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3671. {
  3672. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3673. }
  3674. //---------------------------------------------------------------------------
  3675. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3676. {
  3677. SideEnter(osRemote);
  3678. }
  3679. //---------------------------------------------------------------------------
  3680. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3681. {
  3682. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3683. SideEnter(osRemote);
  3684. }
  3685. //---------------------------------------------------------------------------
  3686. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3687. {
  3688. FCurrentSide = Side;
  3689. if (Visible)
  3690. {
  3691. UpdateControls();
  3692. }
  3693. }
  3694. //---------------------------------------------------------------------------
  3695. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3696. TStrings * FileList, bool Alternative)
  3697. {
  3698. TCustomDirView * DView = DirView(Side);
  3699. DView->SaveSelection();
  3700. DView->SaveSelectedNames();
  3701. DebugAssert(!FAlternativeDelete);
  3702. FAlternativeDelete = Alternative;
  3703. try
  3704. {
  3705. if (!IsSideLocalBrowser(Side))
  3706. {
  3707. DebugAssert(Terminal != NULL);
  3708. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3709. }
  3710. else
  3711. {
  3712. try
  3713. {
  3714. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3715. }
  3716. __finally
  3717. {
  3718. ReloadLocalDirectory();
  3719. }
  3720. }
  3721. FAlternativeDelete = false;
  3722. }
  3723. catch(...)
  3724. {
  3725. FAlternativeDelete = false;
  3726. DView->DiscardSavedSelection();
  3727. throw;
  3728. }
  3729. DView->RestoreSelection();
  3730. }
  3731. //---------------------------------------------------------------------------
  3732. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3733. {
  3734. DebugAssert(Terminal);
  3735. DebugAssert(!IsLocalBrowserMode());
  3736. RemoteDirView->SaveSelection();
  3737. RemoteDirView->SaveSelectedNames();
  3738. try
  3739. {
  3740. if (Lock)
  3741. {
  3742. Terminal->LockFiles(FileList);
  3743. }
  3744. else
  3745. {
  3746. Terminal->UnlockFiles(FileList);
  3747. }
  3748. }
  3749. catch(...)
  3750. {
  3751. RemoteDirView->DiscardSavedSelection();
  3752. throw;
  3753. }
  3754. RemoteDirView->RestoreSelection();
  3755. }
  3756. //---------------------------------------------------------------------------
  3757. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  3758. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3759. bool NoConfirmation, bool Move)
  3760. {
  3761. DebugAssert(Terminal != NULL);
  3762. DebugAssert(!IsLocalBrowserMode());
  3763. // update Terminal->StateData->RemoteDirectory
  3764. UpdateTerminal(Terminal);
  3765. if (Session == NULL)
  3766. {
  3767. Session = TTerminalManager::Instance()->ActiveTerminal;
  3768. }
  3769. if (Session == TTerminalManager::Instance()->ActiveTerminal)
  3770. {
  3771. if (RemoteDriveView->DropTarget != NULL)
  3772. {
  3773. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3774. }
  3775. else if (RemoteDirView->DropTarget != NULL)
  3776. {
  3777. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3778. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3779. }
  3780. else
  3781. {
  3782. Target = RemoteDirView->Path;
  3783. }
  3784. }
  3785. else
  3786. {
  3787. Target = Session->StateData->RemoteDirectory;
  3788. }
  3789. Target = UnixIncludeTrailingBackslash(Target);
  3790. if (FileList->Count == 1)
  3791. {
  3792. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3793. }
  3794. else
  3795. {
  3796. FileMask = AnyMask;
  3797. }
  3798. DirectCopy = FTerminal->IsCapable[fcRemoteCopy] || FTerminal->IsCapable[fcSecondaryShell];
  3799. bool Result = true;
  3800. if (!NoConfirmation)
  3801. {
  3802. bool Multi = (FileList->Count > 1);
  3803. if (Move)
  3804. {
  3805. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3806. }
  3807. else
  3808. {
  3809. std::unique_ptr<TStrings> Sessions(new TStringList);
  3810. std::unique_ptr<TStrings> Directories(new TStringList);
  3811. TStrings * SessionList = TTerminalManager::Instance()->TerminalList;
  3812. for (int Index = 0; Index < SessionList->Count; Index++)
  3813. {
  3814. TManagedTerminal * ASession = DebugNotNull(dynamic_cast<TManagedTerminal *>(SessionList->Objects[Index]));
  3815. if (IsActiveTerminal(ASession))
  3816. {
  3817. Sessions->AddObject(SessionList->Strings[Index], ASession);
  3818. Directories->Add(ASession->StateData->RemoteDirectory);
  3819. }
  3820. }
  3821. TDirectRemoteCopy AllowDirectCopy;
  3822. if (FTerminal->IsCapable[fcRemoteCopy] || FTerminal->CommandSessionOpened)
  3823. {
  3824. DebugAssert(DirectCopy);
  3825. AllowDirectCopy = drcAllow;
  3826. }
  3827. else if (FTerminal->IsCapable[fcSecondaryShell])
  3828. {
  3829. DebugAssert(DirectCopy);
  3830. AllowDirectCopy = drcConfirmCommandSession;
  3831. }
  3832. else
  3833. {
  3834. DebugAssert(!DirectCopy);
  3835. AllowDirectCopy = drcDisallow;
  3836. }
  3837. void * ASession = Session;
  3838. Result = DoRemoteCopyDialog(
  3839. Sessions.get(), Directories.get(), AllowDirectCopy, Multi, ASession, Target, FileMask, DirectCopy, TTerminalManager::Instance()->ActiveTerminal);
  3840. Session = static_cast<TManagedTerminal *>(ASession);
  3841. }
  3842. }
  3843. return Result;
  3844. }
  3845. //---------------------------------------------------------------------------
  3846. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3847. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  3848. {
  3849. DebugAssert(!IsLocalBrowserMode());
  3850. bool DirectCopy;
  3851. UnicodeString Target, FileMask;
  3852. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3853. if (Result)
  3854. {
  3855. if (!Move && !DirectCopy)
  3856. {
  3857. Configuration->Usage->Inc("RemoteCopyTemp");
  3858. UnicodeString RootTempDir;
  3859. UnicodeString TempDir;
  3860. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3861. TStrings * TemporaryFilesList = new TStringList();
  3862. try
  3863. {
  3864. TMakeLocalFileListParams MakeFileListParam;
  3865. MakeFileListParam.FileList = TemporaryFilesList;
  3866. MakeFileListParam.FileTimes = NULL;
  3867. MakeFileListParam.IncludeDirs = true;
  3868. MakeFileListParam.Recursive = false;
  3869. ProcessLocalDirectory(TempDir, FTerminal->MakeLocalFileList, &MakeFileListParam);
  3870. TTerminalManager::Instance()->ActiveTerminal = Session;
  3871. if (TemporaryFilesList->Count > 0)
  3872. {
  3873. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3874. CopyParam.FileMask = FileMask;
  3875. DebugAssert(!FAutoOperation);
  3876. FAutoOperation = true;
  3877. FTerminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3878. }
  3879. }
  3880. __finally
  3881. {
  3882. delete TemporaryFilesList;
  3883. FAutoOperation = false;
  3884. if (!RootTempDir.IsEmpty())
  3885. {
  3886. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3887. }
  3888. }
  3889. }
  3890. else
  3891. {
  3892. RemoteDirView->SaveSelection();
  3893. RemoteDirView->SaveSelectedNames();
  3894. try
  3895. {
  3896. if (Move)
  3897. {
  3898. Configuration->Usage->Inc("RemoteMove");
  3899. Terminal->MoveFiles(FileList, Target, FileMask);
  3900. }
  3901. else
  3902. {
  3903. Configuration->Usage->Inc("RemoteCopyDirect");
  3904. DebugAssert(DirectCopy);
  3905. DebugAssert(Session == FTerminal);
  3906. if (FTerminal->IsCapable[fcRemoteCopy] ||
  3907. FTerminal->CommandSessionOpened ||
  3908. CommandSessionFallback())
  3909. {
  3910. Terminal->CopyFiles(FileList, Target, FileMask);
  3911. }
  3912. }
  3913. }
  3914. catch(...)
  3915. {
  3916. RemoteDirView->DiscardSavedSelection();
  3917. throw;
  3918. }
  3919. RemoteDirView->RestoreSelection();
  3920. }
  3921. }
  3922. return Result;
  3923. }
  3924. //---------------------------------------------------------------------------
  3925. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3926. const UnicodeString & Path, TRemoteProperties & Properties)
  3927. {
  3928. DebugAssert(!IsLocalBrowserMode());
  3929. Properties.Valid = Properties.Valid << vpEncrypt;
  3930. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  3931. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  3932. }
  3933. //---------------------------------------------------------------------------
  3934. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3935. {
  3936. Side = GetSide(Side);
  3937. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3938. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  3939. UnicodeString Name = LoadStr(NEW_FOLDER);
  3940. int AllowedChanges =
  3941. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  3942. bool SaveSettings = false;
  3943. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  3944. {
  3945. TWindowLock Lock(this);
  3946. if (!IsSideLocalBrowser(Side))
  3947. {
  3948. if (SaveSettings)
  3949. {
  3950. GUIConfiguration->NewDirectoryProperties = Properties;
  3951. }
  3952. CreateRemoteDirectory(Name, Properties);
  3953. }
  3954. else
  3955. {
  3956. DirView(Side)->CreateDirectory(Name);
  3957. }
  3958. }
  3959. }
  3960. //---------------------------------------------------------------------------
  3961. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  3962. {
  3963. TWindowLock Lock(this);
  3964. DirView(Side)->ExecuteHomeDirectory();
  3965. }
  3966. //---------------------------------------------------------------------------
  3967. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  3968. {
  3969. DoOpenDirectoryDialog(odBrowse, Side);
  3970. }
  3971. //---------------------------------------------------------------------------
  3972. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  3973. {
  3974. UnicodeString Path = Bookmark->GetSideDirectory(Side);
  3975. bool Result = !Path.IsEmpty();
  3976. if (Result)
  3977. {
  3978. // While we might get here when the session is closed (from location profiles),
  3979. // it's not a problem as the Path setter is noop then
  3980. DirView(Side)->Path = Path;
  3981. }
  3982. return Result;
  3983. }
  3984. //---------------------------------------------------------------------------
  3985. void __fastcall TCustomScpExplorerForm::CalculateSize(
  3986. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  3987. bool & Close)
  3988. {
  3989. // terminal can be already closed (e.g. dropped connection)
  3990. if (Terminal != NULL)
  3991. {
  3992. try
  3993. {
  3994. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  3995. }
  3996. catch(...)
  3997. {
  3998. if (!Terminal->Active)
  3999. {
  4000. Close = true;
  4001. }
  4002. throw;
  4003. }
  4004. }
  4005. }
  4006. //---------------------------------------------------------------------------
  4007. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  4008. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  4009. bool & Close)
  4010. {
  4011. // terminal can be already closed (e.g. dropped connection)
  4012. if (Terminal != NULL)
  4013. {
  4014. Configuration->Usage->Inc(L"ChecksumCalculated");
  4015. try
  4016. {
  4017. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  4018. }
  4019. catch(...)
  4020. {
  4021. if (!Terminal->Active)
  4022. {
  4023. Close = true;
  4024. }
  4025. throw;
  4026. }
  4027. }
  4028. }
  4029. //---------------------------------------------------------------------------
  4030. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  4031. {
  4032. bool Result;
  4033. if (!IsSideLocalBrowser(Side))
  4034. {
  4035. TRemoteTokenList * GroupList = NULL;
  4036. TRemoteTokenList * UserList = NULL;
  4037. try
  4038. {
  4039. TRemoteProperties CurrentProperties;
  4040. if (Terminal->LoadFilesProperties(FileList))
  4041. {
  4042. RemoteDirView->Invalidate();
  4043. }
  4044. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4045. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4046. if (CapableGroupChanging || CapableOwnerChanging)
  4047. {
  4048. if (CapableGroupChanging)
  4049. {
  4050. GroupList = Terminal->Groups->Duplicate();
  4051. }
  4052. if (CapableOwnerChanging)
  4053. {
  4054. UserList = Terminal->Users->Duplicate();
  4055. }
  4056. TRemoteDirectory * Files = Terminal->Files;
  4057. int Count = Files->Count;
  4058. if (Count > 100)
  4059. {
  4060. Count = 100;
  4061. }
  4062. for (int Index = 0; Index < Count; Index++)
  4063. {
  4064. TRemoteFile * File = Files->Files[Index];
  4065. if (CapableGroupChanging)
  4066. {
  4067. GroupList->AddUnique(File->Group);
  4068. }
  4069. if (CapableOwnerChanging)
  4070. {
  4071. UserList->AddUnique(File->Owner);
  4072. }
  4073. }
  4074. // if we haven't collected tokens for all files in current directory,
  4075. // make sure we collect them at least for all selected files.
  4076. // (note that so far the files in FileList has to be from current direcotry)
  4077. if (Count < Files->Count)
  4078. {
  4079. for (int Index = 0; Index < FileList->Count; Index++)
  4080. {
  4081. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4082. if (CapableGroupChanging)
  4083. {
  4084. GroupList->AddUnique(File->Group);
  4085. }
  4086. if (CapableOwnerChanging)
  4087. {
  4088. UserList->AddUnique(File->Owner);
  4089. }
  4090. }
  4091. }
  4092. }
  4093. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4094. int Flags = 0;
  4095. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4096. if (CapableOwnerChanging) Flags |= cpOwner;
  4097. if (CapableGroupChanging) Flags |= cpGroup;
  4098. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4099. if (Terminal->IsCapable[fcCalculatingChecksum])
  4100. {
  4101. CalculateChecksumEvent = CalculateChecksum;
  4102. }
  4103. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4104. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4105. TRemoteProperties NewProperties = CurrentProperties;
  4106. Result =
  4107. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4108. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4109. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4110. CalculateSize, CalculateChecksumEvent);
  4111. if (Result)
  4112. {
  4113. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4114. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4115. }
  4116. }
  4117. __finally
  4118. {
  4119. delete GroupList;
  4120. delete UserList;
  4121. }
  4122. }
  4123. else
  4124. {
  4125. DirView(Side)->DisplayPropertiesMenu();
  4126. Result = true;
  4127. }
  4128. return Result;
  4129. }
  4130. //---------------------------------------------------------------------------
  4131. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4132. {
  4133. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4134. {
  4135. FIgnoreNextDialogChar = Key;
  4136. }
  4137. Key = 0;
  4138. }
  4139. //---------------------------------------------------------------------------
  4140. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4141. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4142. {
  4143. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4144. if (Command != NULL)
  4145. {
  4146. KeyProcessed(Key, Shift);
  4147. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4148. {
  4149. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4150. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4151. false, false, const_cast<TCustomCommandType *>(Command));
  4152. }
  4153. }
  4154. }
  4155. //---------------------------------------------------------------------------
  4156. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4157. {
  4158. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4159. {
  4160. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4161. }
  4162. if ((Key != 0) && !DirView(osCurrent)->IsEditing())
  4163. {
  4164. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4165. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4166. {
  4167. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4168. if (((Action->ShortCut == KeyShortCut) ||
  4169. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4170. AllowedAction(Action, aaShortCut))
  4171. {
  4172. // Has to be called before the action as the dialog char is already in queue.
  4173. // So when the action consumes message queue, we already need to have the
  4174. // FIgnoreNextDialogChar set
  4175. KeyProcessed(Key, Shift);
  4176. // Reset reference to previous component (when menu/toolbar was clicked).
  4177. // Needed to detect that action was invoked by keyboard shortcut
  4178. // in TNonVisualDataModule::ExplorerActionsExecute
  4179. Action->ActionComponent = NULL;
  4180. Action->Execute();
  4181. return;
  4182. }
  4183. }
  4184. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4185. {
  4186. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4187. {
  4188. KeyProcessed(Key, Shift);
  4189. TTerminalManager::Instance()->ActiveTerminalIndex = i;
  4190. return;
  4191. }
  4192. }
  4193. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4194. {
  4195. KeyProcessed(Key, Shift);
  4196. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4197. }
  4198. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4199. if (IsCustomShortCut(CustomShortCut))
  4200. {
  4201. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4202. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4203. if (WinConfiguration->SharedBookmarks != NULL)
  4204. {
  4205. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4206. if ((Bookmark != NULL) &&
  4207. OpenBookmark(FCurrentSide, Bookmark))
  4208. {
  4209. KeyProcessed(Key, Shift);
  4210. }
  4211. }
  4212. }
  4213. }
  4214. TForm::KeyDown(Key, Shift);
  4215. }
  4216. //---------------------------------------------------------------------------
  4217. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4218. {
  4219. DebugAssert(Terminal != NULL);
  4220. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4221. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  4222. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4223. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4224. bool SecurityEnabled = !SessionInfo.SecurityProtocolName.IsEmpty();
  4225. SessionStatusBar->Panels->Items[Offset + 0]->Enabled = SecurityEnabled;
  4226. // expanded from ?: to avoid memory leaks
  4227. if (SecurityEnabled)
  4228. {
  4229. SessionStatusBar->Panels->Items[Offset + 0]->Hint =
  4230. FMTLOAD(STATUS_SECURE, (SessionInfo.SecurityProtocolName));
  4231. }
  4232. else
  4233. {
  4234. SessionStatusBar->Panels->Items[Offset + 0]->Hint = LoadStr(STATUS_INSECURE);
  4235. }
  4236. if (FileSystemInfo.ProtocolName.IsEmpty())
  4237. {
  4238. SessionStatusBar->Panels->Items[Offset + 1]->Caption = SessionInfo.ProtocolName;
  4239. }
  4240. else
  4241. {
  4242. SessionStatusBar->Panels->Items[Offset + 1]->Caption = FileSystemInfo.ProtocolName;
  4243. }
  4244. SessionStatusBar->Panels->Items[Offset + 1]->Hint = LoadStr(STATUS_PROTOCOL_HINT);
  4245. SessionStatusBar->Panels->Items[Offset + 2]->Enabled =
  4246. (!SessionInfo.CSCompression.IsEmpty() || !SessionInfo.SCCompression.IsEmpty());
  4247. UnicodeString CSCompressionStr = DefaultStr(SessionInfo.CSCompression, LoadStr(NO_STR));
  4248. UnicodeString SCCompressionStr = DefaultStr(SessionInfo.SCCompression, LoadStr(NO_STR));
  4249. UnicodeString CompressionStr = CSCompressionStr;
  4250. if (CSCompressionStr != SCCompressionStr)
  4251. {
  4252. CompressionStr += UnicodeString(L"/") + SCCompressionStr;
  4253. }
  4254. SessionStatusBar->Panels->Items[Offset + 2]->Hint = FMTLOAD(STATUS_COMPRESSION_HINT, (CompressionStr));
  4255. SessionStatusBar->Panels->Items[Offset + 3]->Hint = LoadStr(STATUS_DURATION_HINT);
  4256. UpdateStatusBar();
  4257. }
  4258. //---------------------------------------------------------------------------
  4259. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4260. {
  4261. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4262. DebugAssert(SessionStatusBar != NULL);
  4263. if (!HasActiveTerminal() || (Terminal->Status < ssOpened))
  4264. {
  4265. // note: (Terminal->Status < sshReady) currently never happens here,
  4266. // so STATUS_CONNECTING is never used
  4267. SessionStatusBar->SimplePanel = true;
  4268. SessionStatusBar->SimpleText = LoadStr(
  4269. !HasActiveTerminal() ? STATUS_NOT_CONNECTED : STATUS_CONNECTING);
  4270. }
  4271. else
  4272. {
  4273. DebugAssert(Terminal);
  4274. SessionStatusBar->SimplePanel = false;
  4275. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4276. if (!FNote.IsEmpty())
  4277. {
  4278. SessionStatusBar->Panels->Items[0]->Caption = FNote;
  4279. }
  4280. else
  4281. {
  4282. UpdateStatusPanelText(SessionStatusBar->Panels->Items[0]);
  4283. }
  4284. SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1]->Caption =
  4285. FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4286. }
  4287. SessionStatusBar->Panels->Items[0]->Hint = FNoteHints;
  4288. }
  4289. //---------------------------------------------------------------------------
  4290. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4291. {
  4292. Panel->Caption = L"";
  4293. }
  4294. //---------------------------------------------------------------------------
  4295. void __fastcall TCustomScpExplorerForm::Idle()
  4296. {
  4297. if (FShowing ||
  4298. // Particularly to detect closed connection and automatically reconnect it while waiting for changes
  4299. // while "keeping remote directory up to date"
  4300. StandaloneOperation)
  4301. {
  4302. FEditorManager->Check();
  4303. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4304. // that needs to know if queue view is visible (and it may be closed after queue update)
  4305. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4306. }
  4307. if (FShowing)
  4308. {
  4309. if (!NonVisualDataModule->Busy &&
  4310. // Menu is opened or drag&drop is going on
  4311. (Mouse->Capture == NULL))
  4312. {
  4313. if (FRefreshRemoteDirectory)
  4314. {
  4315. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4316. {
  4317. Terminal->RefreshDirectory();
  4318. }
  4319. FRefreshRemoteDirectory = false;
  4320. }
  4321. if (FRefreshLocalDirectory)
  4322. {
  4323. ReloadLocalDirectory();
  4324. FRefreshLocalDirectory = false;
  4325. }
  4326. if (WinConfiguration->RefreshRemotePanel)
  4327. {
  4328. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4329. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4330. {
  4331. RemoteDirView->ReloadDirectory();
  4332. }
  4333. }
  4334. }
  4335. }
  4336. if (FShowing || StandaloneOperation)
  4337. {
  4338. if (FQueueStatusInvalidated)
  4339. {
  4340. UpdateQueueStatus(false);
  4341. }
  4342. RefreshQueueItems();
  4343. }
  4344. if (FShowing)
  4345. {
  4346. UpdateStatusBar();
  4347. }
  4348. if (FClipboardFakeMonitorsPendingReset && !FDownloadingFromClipboard)
  4349. {
  4350. FClipboardFakeMonitorsPendingReset = false;
  4351. FClipboardFakeMonitors.reset(NULL);
  4352. }
  4353. FIgnoreNextDialogChar = 0;
  4354. }
  4355. //---------------------------------------------------------------------------
  4356. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4357. {
  4358. try
  4359. {
  4360. FUserActionTimer->Enabled = false;
  4361. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4362. {
  4363. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4364. {
  4365. FPendingQueueActionItem->ProcessUserAction();
  4366. }
  4367. }
  4368. }
  4369. __finally
  4370. {
  4371. FPendingQueueActionItem = NULL;
  4372. }
  4373. }
  4374. //---------------------------------------------------------------------------
  4375. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4376. {
  4377. if (WinConfiguration->MinimizeToTray)
  4378. {
  4379. UpdateTrayIcon();
  4380. FTrayIcon->Visible = true;
  4381. if (Visible)
  4382. {
  4383. ShowWindow(Handle, SW_HIDE);
  4384. }
  4385. }
  4386. }
  4387. //---------------------------------------------------------------------------
  4388. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4389. {
  4390. // WORKAROUND
  4391. // When restoring maximized window from minimization,
  4392. // rarely some controls do not align properly.
  4393. // Two instances seen (both for Commander):
  4394. // - When restoring, window is temporarily narrower (not maximized),
  4395. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4396. // Once maximized already, top dock shinks back, but the session PageControl,
  4397. // do not align up, leaving space between TopDock and PageControl.
  4398. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4399. for (int Index = 0; Index < ControlCount; Index++)
  4400. {
  4401. RealignControl(Controls[Index]);
  4402. }
  4403. if (FTrayIcon->Visible)
  4404. {
  4405. FTrayIcon->Visible = false;
  4406. if (Visible)
  4407. {
  4408. ShowWindow(Handle, SW_SHOW);
  4409. }
  4410. }
  4411. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4412. {
  4413. FNeedSession = false;
  4414. NonVisualDataModule->StartBusy();
  4415. try
  4416. {
  4417. NeedSession(true);
  4418. }
  4419. __finally
  4420. {
  4421. NonVisualDataModule->EndBusy();
  4422. }
  4423. }
  4424. }
  4425. //---------------------------------------------------------------------------
  4426. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4427. {
  4428. FTrayIcon->Hint = GetMainForm()->Caption;
  4429. }
  4430. //---------------------------------------------------------------------------
  4431. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4432. {
  4433. UpdateTrayIcon();
  4434. }
  4435. //---------------------------------------------------------------------------
  4436. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4437. {
  4438. // workaround
  4439. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4440. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4441. // But WM_ACTIVATEAPP message can be received even when
  4442. // the main window is minimized to the tray and internal editor window is focused
  4443. // (after another application was previously active)
  4444. if (::IsIconic(Handle))
  4445. {
  4446. if (!IsAppIconic())
  4447. {
  4448. SetAppIconic(true);
  4449. }
  4450. }
  4451. ::ApplicationRestore();
  4452. Application->BringToFront();
  4453. }
  4454. //---------------------------------------------------------------------------
  4455. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4456. {
  4457. RestoreApp();
  4458. }
  4459. //---------------------------------------------------------------------------
  4460. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  4461. {
  4462. if (OpenInNewWindow())
  4463. {
  4464. ExecuteNewInstance(SessionUrl);
  4465. }
  4466. else
  4467. {
  4468. TTerminalManager::Instance()->NewSession(SessionUrl);
  4469. }
  4470. }
  4471. //---------------------------------------------------------------------------
  4472. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4473. {
  4474. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->SessionName;
  4475. StoredSessions->NewSession(SessionName, SessionData);
  4476. // modified only, explicit
  4477. StoredSessions->Save(false, true);
  4478. // encode session name because of slashes in hierarchical sessions
  4479. return EncodeUrlString(SessionName);
  4480. }
  4481. //---------------------------------------------------------------------------
  4482. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4483. {
  4484. // current working directories become defaults here, what is not right
  4485. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4486. return SaveHiddenDuplicateSession(SessionData.get());
  4487. }
  4488. //---------------------------------------------------------------------------
  4489. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4490. {
  4491. if (OpenInNewWindow())
  4492. {
  4493. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4494. }
  4495. else
  4496. {
  4497. // current working directories become defaults here, what is not right
  4498. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4499. TTerminalManager * Manager = TTerminalManager::Instance();
  4500. TManagedTerminal * ATerminal = Manager->NewManagedTerminal(SessionData.get());
  4501. // We definitelly want this
  4502. ATerminal->Disconnected = Terminal->Disconnected;
  4503. // Not sure about these two
  4504. ATerminal->Permanent = Terminal->Permanent;
  4505. ATerminal->DisconnectedTemporarily = Terminal->DisconnectedTemporarily; // maybe
  4506. Manager->ActiveTerminal = ATerminal;
  4507. }
  4508. }
  4509. //---------------------------------------------------------------------------
  4510. void __fastcall TCustomScpExplorerForm::RenameSession()
  4511. {
  4512. UnicodeString Name = Terminal->SessionData->SessionName;
  4513. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME))
  4514. {
  4515. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4516. TSessionData::ValidateName(Name);
  4517. // This is inconsistent with how color (for example) is handled.
  4518. Terminal->SessionData->Name = Name;
  4519. UpdateControls();
  4520. // Add/Remove distinguishing paths from sessions of the same name.
  4521. DoTerminalListChanged();
  4522. }
  4523. }
  4524. //---------------------------------------------------------------------------
  4525. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4526. {
  4527. DebugAssert(Queue != NULL);
  4528. bool Result = Queue->IsEmpty;
  4529. if (!Result)
  4530. {
  4531. SetFocus();
  4532. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4533. }
  4534. return Result;
  4535. }
  4536. //---------------------------------------------------------------------------
  4537. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4538. {
  4539. return CanCloseQueue(FQueue);
  4540. }
  4541. //---------------------------------------------------------------------------
  4542. void __fastcall TCustomScpExplorerForm::CloseSession()
  4543. {
  4544. if (CanCloseQueue())
  4545. {
  4546. TTerminalManager::Instance()->FreeActiveTerminal();
  4547. }
  4548. }
  4549. //---------------------------------------------------------------------------
  4550. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4551. {
  4552. if (CanCloseQueue())
  4553. {
  4554. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4555. }
  4556. }
  4557. //---------------------------------------------------------------------------
  4558. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4559. {
  4560. DetachTerminal(Terminal);
  4561. RemoteDirView->Terminal = Terminal;
  4562. UpdateRemotePathComboBox(false);
  4563. UpdateControls();
  4564. }
  4565. //---------------------------------------------------------------------------
  4566. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4567. {
  4568. if (DebugAlwaysTrue(!Terminal->Active))
  4569. {
  4570. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4571. }
  4572. }
  4573. //---------------------------------------------------------------------------
  4574. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4575. {
  4576. if (OpenInNewWindow())
  4577. {
  4578. // encode session name because of slashes in hierarchical sessions
  4579. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4580. }
  4581. else
  4582. {
  4583. TTerminalManager * Manager = TTerminalManager::Instance();
  4584. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4585. Manager->ActiveTerminal = Terminal;
  4586. }
  4587. }
  4588. //---------------------------------------------------------------------------
  4589. void TCustomScpExplorerForm::DoOpenFolderOrWorkspace(
  4590. const UnicodeString & Name, bool ConnectFirstTerminal, bool CheckMaxSessions)
  4591. {
  4592. TTerminalManager * Manager = TTerminalManager::Instance();
  4593. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4594. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4595. if (!CheckMaxSessions || (DataList->Count <= WinConfiguration->MaxSessions))
  4596. {
  4597. TManagedTerminal * FirstTerminal = Manager->NewTerminals(DataList.get());
  4598. // FirstTerminal can be null, if some of the
  4599. if (!ConnectFirstTerminal && (FirstTerminal != NULL))
  4600. {
  4601. FirstTerminal->Disconnected = true;
  4602. FirstTerminal->DisconnectedTemporarily = true;
  4603. }
  4604. Manager->ActiveTerminal = FirstTerminal;
  4605. }
  4606. }
  4607. //---------------------------------------------------------------------------
  4608. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4609. {
  4610. if (OpenInNewWindow())
  4611. {
  4612. ExecuteNewInstance(Name);
  4613. }
  4614. else
  4615. {
  4616. DoOpenFolderOrWorkspace(Name, true, false);
  4617. }
  4618. }
  4619. //---------------------------------------------------------------------------
  4620. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4621. bool &CanClose)
  4622. {
  4623. // See the comment in CloseApp()
  4624. if (NonVisualDataModule->Busy)
  4625. {
  4626. CanClose = false;
  4627. }
  4628. else
  4629. {
  4630. TTerminalManager * Manager = TTerminalManager::Instance();
  4631. int ActiveSessions = 0;
  4632. for (int Index = 0; Index < Manager->Count; Index++)
  4633. {
  4634. if (Manager->Terminals[Index]->Active)
  4635. {
  4636. ActiveSessions++;
  4637. }
  4638. }
  4639. // Confirm, when there's at least one active remote session or
  4640. // when there are multiple (inactive or local) sessions and auto workspace saving is not enabled.
  4641. if (((ActiveSessions > 0) || ((Manager->Count > 1) && !WinConfiguration->AutoSaveWorkspace)) &&
  4642. WinConfiguration->ConfirmClosingSession)
  4643. {
  4644. unsigned int Result;
  4645. TMessageParams Params(mpNeverAskAgainCheck);
  4646. UnicodeString Message;
  4647. int Answers = qaOK | qaCancel;
  4648. // The current session is the only active session
  4649. if ((ActiveSessions == 1) && (Terminal != NULL) && Terminal->Active)
  4650. {
  4651. if (!WinConfiguration->AutoSaveWorkspace)
  4652. {
  4653. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  4654. }
  4655. else
  4656. {
  4657. Message = LoadStr(CLOSE_SESSION);
  4658. }
  4659. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  4660. }
  4661. // Multiple active sessions or one active session, but it's not the current one
  4662. else if (ActiveSessions > 0)
  4663. {
  4664. if (!WinConfiguration->AutoSaveWorkspace)
  4665. {
  4666. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  4667. }
  4668. else
  4669. {
  4670. Message = LoadStr(CLOSE_SESSIONS);
  4671. }
  4672. }
  4673. // Multiple tabs, but all are intactive or local, and saving of worspace is not enabled
  4674. else
  4675. {
  4676. DebugAlwaysTrue(Manager->Count > 1);
  4677. DebugAlwaysTrue(!WinConfiguration->AutoSaveWorkspace);
  4678. Message = LoadStr(CLOSE_WORKSPACE);
  4679. }
  4680. UnicodeString Note;
  4681. if (WinConfiguration->AutoSaveWorkspace)
  4682. {
  4683. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  4684. }
  4685. else
  4686. {
  4687. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  4688. Answers = qaYes | qaNo | qaCancel;
  4689. }
  4690. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  4691. SetFocus();
  4692. Result = MessageDialog(Message, qtConfirmation,
  4693. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  4694. if (Result == qaNeverAskAgain)
  4695. {
  4696. WinConfiguration->ConfirmClosingSession = false;
  4697. }
  4698. if (Result == qaNo)
  4699. {
  4700. CanClose = SaveWorkspace(true);
  4701. // note that the workspace will be saved redundatly again from FormClose
  4702. }
  4703. else
  4704. {
  4705. CanClose =
  4706. (Result == qaOK) ||
  4707. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4708. (Result == qaNeverAskAgain);
  4709. }
  4710. }
  4711. if (CanClose && HasActiveTerminal())
  4712. {
  4713. CanClose = CanCloseQueue();
  4714. }
  4715. }
  4716. if (CanClose)
  4717. {
  4718. CanClose =
  4719. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4720. FEditorManager->CloseExternalFilesWithoutProcess();
  4721. if (CanClose)
  4722. {
  4723. while (CanClose && (FLocalEditors->Count > 0))
  4724. {
  4725. int PrevCount = FLocalEditors->Count;
  4726. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4727. CanClose = (FLocalEditors->Count < PrevCount);
  4728. }
  4729. if (CanClose)
  4730. {
  4731. CanClose = FEditorManager->Empty(true);
  4732. if (!CanClose)
  4733. {
  4734. SetFocus();
  4735. CanClose =
  4736. (MessageDialog(
  4737. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4738. }
  4739. }
  4740. }
  4741. }
  4742. }
  4743. //---------------------------------------------------------------------------
  4744. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4745. {
  4746. TForm * Form = dynamic_cast<TForm *>(Sender);
  4747. DebugAssert(Form != NULL);
  4748. Form->Close();
  4749. }
  4750. //---------------------------------------------------------------------------
  4751. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4752. {
  4753. TForm * Form = dynamic_cast<TForm *>(Sender);
  4754. delete Form;
  4755. }
  4756. //---------------------------------------------------------------------------
  4757. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4758. {
  4759. while (FLocalEditors->Count > 0)
  4760. {
  4761. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4762. }
  4763. }
  4764. //---------------------------------------------------------------------------
  4765. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4766. TObject *Sender)
  4767. {
  4768. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4769. try
  4770. {
  4771. SetProperties(osRemote, FileList);
  4772. }
  4773. __finally
  4774. {
  4775. delete FileList;
  4776. }
  4777. }
  4778. //---------------------------------------------------------------------------
  4779. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4780. {
  4781. if (value)
  4782. {
  4783. if (Component == fcCustomCommandsBand)
  4784. {
  4785. UpdateCustomCommandsToolbar();
  4786. }
  4787. if (Component == fcQueueFileList)
  4788. {
  4789. DebugAssert(!QueueFileListSplitter->Visible);
  4790. AdjustQueueLayout();
  4791. UpdateQueueFileList();
  4792. }
  4793. }
  4794. }
  4795. //---------------------------------------------------------------------------
  4796. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  4797. {
  4798. int MinHeightLimit =
  4799. GetStaticQueuePanelComponentsHeight() +
  4800. QueueFileListSplitter->Height +
  4801. QueueFileList->Height +
  4802. GetMinQueueViewHeight();
  4803. if (QueuePanel->ClientHeight < MinHeightLimit)
  4804. {
  4805. QueuePanel->ClientHeight = MinHeightLimit;
  4806. }
  4807. }
  4808. //---------------------------------------------------------------------------
  4809. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4810. {
  4811. TControl * Control = GetComponent(Component);
  4812. DebugAssert(Control);
  4813. bool Changed = (Control->Visible != value);
  4814. if (Changed)
  4815. {
  4816. ComponentShowing(Component, value);
  4817. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4818. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4819. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4820. if (value)
  4821. {
  4822. int RemainingHeight = Control->Parent->ClientHeight;
  4823. int RemainingWidth = Control->Parent->ClientWidth;
  4824. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4825. {
  4826. TControl * ChildControl = Control->Parent->Controls[i];
  4827. if (ChildControl->Visible)
  4828. {
  4829. switch (ChildControl->Align)
  4830. {
  4831. case alTop:
  4832. case alBottom:
  4833. RemainingHeight -= ChildControl->Height;
  4834. break;
  4835. case alLeft:
  4836. case alRight:
  4837. RemainingWidth -= ChildControl->Width;
  4838. break;
  4839. }
  4840. }
  4841. }
  4842. int Reserve = ScaleByTextHeight(this, 32);
  4843. // queue in explorer, trees in commander
  4844. if (Control->Height > RemainingHeight - Reserve)
  4845. {
  4846. Control->Height = RemainingHeight / 2;
  4847. }
  4848. if (Control->Width > RemainingWidth - Reserve)
  4849. {
  4850. Control->Width = RemainingWidth / 2;
  4851. }
  4852. }
  4853. Control->Visible = value;
  4854. if (WasFocused && Visible)
  4855. {
  4856. DirView(osCurrent)->SetFocus();
  4857. }
  4858. FixControlsPlacement();
  4859. }
  4860. }
  4861. //---------------------------------------------------------------------------
  4862. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4863. {
  4864. TControl * Control = GetComponent(Component);
  4865. if (Control == NULL)
  4866. {
  4867. return false;
  4868. }
  4869. else
  4870. {
  4871. return Control->Visible;
  4872. }
  4873. }
  4874. //---------------------------------------------------------------------------
  4875. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4876. {
  4877. bool Result;
  4878. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4879. {
  4880. Result = !IsUWP();
  4881. }
  4882. else
  4883. {
  4884. Result = true;
  4885. }
  4886. return Result;
  4887. }
  4888. //---------------------------------------------------------------------------
  4889. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4890. {
  4891. if (DirView(osOther)->ItemFocused != NULL)
  4892. {
  4893. DirView(osOther)->ItemFocused->MakeVisible(false);
  4894. }
  4895. QueueSplitter->Visible = QueuePanel->Visible;
  4896. QueueFileListSplitter->Visible = QueueFileList->Visible;
  4897. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  4898. TControl * QueueControlsOrder[] =
  4899. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  4900. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  4901. }
  4902. //---------------------------------------------------------------------------
  4903. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4904. {
  4905. switch (Component) {
  4906. case fcStatusBar: return RemoteStatusBar;
  4907. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4908. case fcQueueView: return QueuePanel;
  4909. case fcQueueToolbar: return QueueDock;
  4910. case fcRemoteTree: return RemoteDrivePanel;
  4911. case fcSessionsTabs: return SessionsPageControl;
  4912. case fcQueueFileList: return QueueFileList;
  4913. default: return NULL;
  4914. }
  4915. }
  4916. //---------------------------------------------------------------------------
  4917. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4918. TObject *Sender, TListColumn *Column, TPoint &Point)
  4919. {
  4920. DebugAssert(NonVisualDataModule && Column && Sender);
  4921. NonVisualDataModule->ListColumn = Column;
  4922. TPopupMenu * DirViewColumnMenu;
  4923. if (dynamic_cast<TUnixDirView *>(Sender) != NULL)
  4924. {
  4925. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4926. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4927. (Column->Index == uvName);
  4928. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4929. (Column->Index == uvSize);
  4930. }
  4931. else
  4932. {
  4933. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4934. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4935. (Column->Index == dvName);
  4936. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4937. (Column->Index == dvSize);
  4938. }
  4939. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  4940. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  4941. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  4942. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4943. }
  4944. //---------------------------------------------------------------------------
  4945. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4946. TObject *Sender, TListItem *Item, bool &AllowExec)
  4947. {
  4948. DoDirViewExecFile(Sender, Item, AllowExec);
  4949. }
  4950. //---------------------------------------------------------------------------
  4951. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4952. TListItem * Item, bool & AllowExec)
  4953. {
  4954. DebugAssert(Sender && Item && Configuration);
  4955. DebugAssert(AllowExec);
  4956. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  4957. bool Remote = (ADirView == DirView(osRemote)) && !IsSideLocalBrowser(osRemote);
  4958. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles();
  4959. TOperationSide Side = (Remote ? osRemote : osLocal);
  4960. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  4961. // on files only (not directories)
  4962. // and only when symlinks are resolved (apply to remote panel only)
  4963. if (!ADirView->ItemIsDirectory(Item) &&
  4964. (ResolvedSymlinks || FForceExecution))
  4965. {
  4966. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  4967. !FForceExecution && ResolvedSymlinks)
  4968. {
  4969. if (WinConfiguration->DoubleClickAction == dcaCopy)
  4970. {
  4971. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  4972. UnlockWindow();
  4973. try
  4974. {
  4975. ExecuteFileOperation(
  4976. foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  4977. }
  4978. __finally
  4979. {
  4980. LockWindow();
  4981. }
  4982. AllowExec = false;
  4983. }
  4984. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  4985. {
  4986. if (!Remote || !WinConfiguration->DisableOpenEdit)
  4987. {
  4988. ExecuteFile(Side, efDefaultEditor);
  4989. AllowExec = false;
  4990. }
  4991. }
  4992. else
  4993. {
  4994. DebugFail();
  4995. }
  4996. }
  4997. // if we have not done anything special, fall back to default behavior
  4998. if (AllowExec)
  4999. {
  5000. if (Remote && !WinConfiguration->DisableOpenEdit)
  5001. {
  5002. ExecuteFile(osRemote, efShell);
  5003. AllowExec = false;
  5004. }
  5005. }
  5006. }
  5007. }
  5008. //---------------------------------------------------------------------------
  5009. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5010. {
  5011. return ((Side == osRemote) || (Side == osCurrent));
  5012. }
  5013. //---------------------------------------------------------------------------
  5014. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5015. {
  5016. DebugFail();
  5017. }
  5018. //---------------------------------------------------------------------------
  5019. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5020. {
  5021. DebugFail();
  5022. }
  5023. //---------------------------------------------------------------------------
  5024. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5025. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5026. {
  5027. TSynchronizeParamType Params;
  5028. Params.LocalDirectory = LocalDirectory;
  5029. Params.RemoteDirectory = RemoteDirectory;
  5030. int UnusedParams =
  5031. (GUIConfiguration->SynchronizeParams &
  5032. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5033. TTerminal::spCaseSensitive));
  5034. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5035. Params.Options = GUIConfiguration->SynchronizeOptions;
  5036. bool SaveSettings = false;
  5037. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5038. &DoSynchronizeTooManyDirectories);
  5039. DebugAssert(FSynchronizeController == NULL);
  5040. FSynchronizeController = &Controller;
  5041. bool Result;
  5042. try
  5043. {
  5044. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5045. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5046. int Options =
  5047. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5048. DebugAssert(FOnFeedSynchronizeError == NULL);
  5049. DebugAssert(FOnSynchronizeAbort == NULL);
  5050. DebugAssert(FSynchronizeTerminal == NULL);
  5051. FSynchronizeTerminal = Terminal;
  5052. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5053. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5054. FOnFeedSynchronizeError, FOnSynchronizeAbort, SynchronizeInNewWindow, UseDefaults);
  5055. if (Result)
  5056. {
  5057. if (SaveSettings)
  5058. {
  5059. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5060. GUIConfiguration->SynchronizeOptions = Params.Options;
  5061. }
  5062. else
  5063. {
  5064. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5065. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5066. FLAGSET(Params.Options, soSynchronize))
  5067. {
  5068. GUIConfiguration->SynchronizeOptions =
  5069. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5070. soSynchronize;
  5071. }
  5072. }
  5073. LocalDirectory = Params.LocalDirectory;
  5074. RemoteDirectory = Params.RemoteDirectory;
  5075. }
  5076. }
  5077. __finally
  5078. {
  5079. FSynchronizeController = NULL;
  5080. DebugAssert(FOnFeedSynchronizeError == NULL);
  5081. FOnFeedSynchronizeError = NULL;
  5082. DebugAssert(FOnSynchronizeAbort == NULL);
  5083. FOnSynchronizeAbort = NULL;
  5084. FSynchronizeTerminal = NULL;
  5085. }
  5086. return Result;
  5087. }
  5088. //---------------------------------------------------------------------------
  5089. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5090. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5091. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5092. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5093. TSynchronizeOptions * Options, bool Full)
  5094. {
  5095. // Keep-up-to-date mode
  5096. if (Terminal->Status == ssOpened)
  5097. {
  5098. try
  5099. {
  5100. int PParams = Params.Params;
  5101. if (!Full)
  5102. {
  5103. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5104. if (FLAGSET(Params.Options, soRecurse))
  5105. {
  5106. PParams |= TTerminal::spSubDirs;
  5107. }
  5108. }
  5109. else
  5110. {
  5111. // if keepuptodate is non-recursive,
  5112. // full sync before has to be non-recursive as well
  5113. if (FLAGCLEAR(Params.Options, soRecurse))
  5114. {
  5115. PParams |= TTerminal::spNoRecurse;
  5116. }
  5117. }
  5118. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5119. PParams, Checklist, Options);
  5120. }
  5121. catch (Exception & E)
  5122. {
  5123. if (!Terminal->Active)
  5124. {
  5125. ShowExtendedExceptionEx(Terminal, &E);
  5126. // Do not abort the "keep up to date" when the session was succesfully reconnected.
  5127. if (!Terminal->Active)
  5128. {
  5129. throw;
  5130. }
  5131. }
  5132. else
  5133. {
  5134. if (FLAGSET(Params.Options, soContinueOnError) &&
  5135. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5136. {
  5137. // noop, already logged in MoreMessageDialog
  5138. }
  5139. else
  5140. {
  5141. // We get mostly EAbort here, so this is noop.
  5142. // Exception is an error when listing directories while looking for differences.
  5143. ShowExtendedExceptionEx(Terminal, &E);
  5144. throw;
  5145. }
  5146. }
  5147. }
  5148. }
  5149. }
  5150. //---------------------------------------------------------------------------
  5151. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5152. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5153. const UnicodeString ErrorStr)
  5154. {
  5155. if (!Directory.IsEmpty())
  5156. {
  5157. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5158. }
  5159. else
  5160. {
  5161. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5162. }
  5163. }
  5164. //---------------------------------------------------------------------------
  5165. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5166. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5167. {
  5168. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5169. {
  5170. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5171. }
  5172. else
  5173. {
  5174. TMessageParams Params(mpNeverAskAgainCheck);
  5175. unsigned int Result = MessageDialog(
  5176. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5177. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5178. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5179. {
  5180. MaxDirectories *= 2;
  5181. if (Result == qaNeverAskAgain)
  5182. {
  5183. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5184. }
  5185. }
  5186. else
  5187. {
  5188. Abort();
  5189. }
  5190. }
  5191. }
  5192. //---------------------------------------------------------------------------
  5193. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5194. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5195. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5196. TSynchronizeOptions * Options)
  5197. {
  5198. // Keep-up-to-date mode
  5199. DebugAssert(!FAutoOperation);
  5200. void * BatchStorage;
  5201. BatchStart(BatchStorage);
  5202. FAutoOperation = true;
  5203. bool AnyOperation = false;
  5204. TDateTime StartTime = Now();
  5205. TSynchronizeChecklist * AChecklist = NULL;
  5206. try
  5207. {
  5208. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5209. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5210. {
  5211. FSynchronizeProgressForm->Start();
  5212. }
  5213. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5214. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5215. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5216. Options);
  5217. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5218. SAFE_DESTROY(FSynchronizeProgressForm);
  5219. AnyOperation = (AChecklist->CheckedCount > 0);
  5220. if (AnyOperation)
  5221. {
  5222. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5223. CreateProgressForm(&SynchronizeProgress);
  5224. try
  5225. {
  5226. Terminal->SynchronizeApply(
  5227. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5228. SynchronizeProcessedItem, NULL, NULL, NULL);
  5229. }
  5230. __finally
  5231. {
  5232. DestroyProgressForm();
  5233. }
  5234. }
  5235. }
  5236. __finally
  5237. {
  5238. if (Checklist == NULL)
  5239. {
  5240. delete AChecklist;
  5241. }
  5242. else
  5243. {
  5244. *Checklist = AChecklist;
  5245. }
  5246. FAutoOperation = false;
  5247. SAFE_DESTROY(FSynchronizeProgressForm);
  5248. BatchEnd(BatchStorage);
  5249. ReloadLocalDirectory();
  5250. if (AnyOperation)
  5251. {
  5252. OperationComplete(StartTime);
  5253. }
  5254. }
  5255. }
  5256. //---------------------------------------------------------------------------
  5257. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5258. {
  5259. // can be called from command line
  5260. return Visible &&
  5261. ((DirView(osRemote)->SelCount > 0) ||
  5262. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5263. }
  5264. //---------------------------------------------------------------------------
  5265. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5266. {
  5267. LogSynchronizeEvent(Terminal, Message);
  5268. }
  5269. //---------------------------------------------------------------------------
  5270. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5271. int Params, TSynchronizeOptions & Options)
  5272. {
  5273. DebugAssert(!IsLocalBrowserMode());
  5274. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5275. {
  5276. Options.Filter = new TStringList();
  5277. Options.Filter->CaseSensitive = false;
  5278. Options.Filter->Duplicates = Types::dupAccept;
  5279. if (DirView(osRemote)->SelCount > 0)
  5280. {
  5281. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5282. }
  5283. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5284. {
  5285. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5286. }
  5287. Options.Filter->Sort();
  5288. }
  5289. }
  5290. //---------------------------------------------------------------------------
  5291. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5292. {
  5293. TCopyParamType Defaults;
  5294. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5295. ConfigStorage->AccessMode = smRead;
  5296. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5297. {
  5298. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5299. }
  5300. ConfigStorage.reset(NULL);
  5301. std::unique_ptr<TStringList> Options(new TStringList());
  5302. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5303. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5304. UnicodeString Result;
  5305. if (Options->Count > 0)
  5306. {
  5307. Result =
  5308. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5309. }
  5310. return Result;
  5311. }
  5312. //---------------------------------------------------------------------------
  5313. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5314. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5315. {
  5316. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5317. UnicodeString AdditionalParams =
  5318. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5319. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5320. SerializeCopyParamForCommandLine(CopyParams),
  5321. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5322. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5323. ExecuteNewInstance(SessionName, AdditionalParams);
  5324. }
  5325. //---------------------------------------------------------------------------
  5326. struct TSynchronizeParams
  5327. {
  5328. UnicodeString LocalDirectory;
  5329. UnicodeString RemoteDirectory;
  5330. TSynchronizeMode Mode;
  5331. TSynchronizeChecklist * Checklist;
  5332. int Params;
  5333. TCopyParamType * CopyParam;
  5334. TDateTime CollectElapsed;
  5335. TDateTime * StartTime;
  5336. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5337. };
  5338. //---------------------------------------------------------------------------
  5339. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5340. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5341. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5342. {
  5343. DebugAssert(!FAutoOperation);
  5344. void * BatchStorage;
  5345. BatchStart(BatchStorage);
  5346. FAutoOperation = true;
  5347. TFileOperationStatistics Statistics;
  5348. TDateTime Start = Now();
  5349. try
  5350. {
  5351. Params.OnProcessedItem = OnProcessedItem;
  5352. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5353. CreateProgressForm(&SynchronizeProgress);
  5354. Terminal->SynchronizeApply(
  5355. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5356. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5357. &Statistics);
  5358. }
  5359. __finally
  5360. {
  5361. Params.OnProcessedItem = NULL;
  5362. FAutoOperation = false;
  5363. DestroyProgressForm();
  5364. BatchEnd(BatchStorage);
  5365. ReloadLocalDirectory();
  5366. }
  5367. if (WinConfiguration->SynchronizeSummary)
  5368. {
  5369. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5370. // The statistics should be 0 anyway in this case
  5371. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5372. {
  5373. Message += L"\n";
  5374. if (Statistics.FilesUploaded > 0)
  5375. {
  5376. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5377. }
  5378. if (Statistics.FilesDownloaded > 0)
  5379. {
  5380. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5381. }
  5382. if (Statistics.FilesDeletedLocal > 0)
  5383. {
  5384. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5385. }
  5386. if (Statistics.FilesDeletedRemote > 0)
  5387. {
  5388. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5389. }
  5390. }
  5391. TDateTime Elapsed = (Now() - Start);
  5392. Message +=
  5393. L"\n" +
  5394. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5395. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5396. TMessageParams Params(mpNeverAskAgainCheck);
  5397. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5398. if (Result == qaNeverAskAgain)
  5399. {
  5400. WinConfiguration->SynchronizeSummary = false;
  5401. }
  5402. }
  5403. }
  5404. //---------------------------------------------------------------------------
  5405. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5406. {
  5407. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5408. {
  5409. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5410. }
  5411. // Not set in keep-up-to-date mode - Synchronize() method
  5412. if (Token != NULL)
  5413. {
  5414. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5415. if (Params.OnProcessedItem != NULL)
  5416. {
  5417. Params.OnProcessedItem(NULL, ChecklistItem);
  5418. }
  5419. }
  5420. }
  5421. //---------------------------------------------------------------------------
  5422. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5423. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5424. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5425. {
  5426. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5427. *Params.StartTime = Now();
  5428. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5429. }
  5430. //---------------------------------------------------------------------------
  5431. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5432. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5433. {
  5434. // terminal can be already closed (e.g. dropped connection)
  5435. if (Terminal != NULL)
  5436. {
  5437. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5438. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5439. }
  5440. }
  5441. //---------------------------------------------------------------------------
  5442. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5443. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5444. {
  5445. DebugAssert(!IsLocalBrowserMode());
  5446. TAutoBatch AutoBatch(this);
  5447. TAutoFlag AutoOperationFlag(FAutoOperation);
  5448. if (Side == osRemote)
  5449. {
  5450. std::unique_ptr<TStrings> FileList(new TStringList());
  5451. FileList->AddObject(FileName, RemoteFile);
  5452. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5453. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5454. RemoteDirView->SaveSelection();
  5455. RemoteDirView->SaveSelectedNames();
  5456. try
  5457. {
  5458. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5459. }
  5460. catch(...)
  5461. {
  5462. RemoteDirView->DiscardSavedSelection();
  5463. throw;
  5464. }
  5465. RemoteDirView->RestoreSelection();
  5466. }
  5467. else if (DebugAlwaysTrue(Side == osLocal))
  5468. {
  5469. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5470. {
  5471. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5472. }
  5473. UnicodeString Directory = ExtractFileDir(FileName);
  5474. ReloadLocalDirectory(Directory);
  5475. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5476. if (!SamePaths(Directory, NewDirectory))
  5477. {
  5478. ReloadLocalDirectory(NewDirectory);
  5479. }
  5480. }
  5481. }
  5482. //---------------------------------------------------------------------------
  5483. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5484. {
  5485. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5486. if (Side == osLocal)
  5487. {
  5488. if (Action == TSynchronizeChecklist::saDownloadNew)
  5489. {
  5490. OpenFolderInExplorer(LocalPath);
  5491. }
  5492. else
  5493. {
  5494. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5495. }
  5496. }
  5497. else if (DebugAlwaysTrue(Side == osRemote))
  5498. {
  5499. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5500. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5501. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5502. if (!LocalPath.IsEmpty())
  5503. {
  5504. SessionData->LocalDirectory = LocalPath;
  5505. }
  5506. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5507. ExecuteNewInstance(SessionName, FORMAT(L"%s=\"%s\"", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5508. }
  5509. }
  5510. //---------------------------------------------------------------------------
  5511. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5512. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5513. const TCopyParamType * CopyParams)
  5514. {
  5515. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5516. UnicodeString AdditionalParams =
  5517. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5518. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5519. SerializeCopyParamForCommandLine(CopyParams),
  5520. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5521. LocalDirectory, RemoteDirectory, Mode, Params));
  5522. ExecuteNewInstance(SessionName, AdditionalParams);
  5523. }
  5524. //---------------------------------------------------------------------------
  5525. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5526. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5527. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5528. {
  5529. int Result;
  5530. bool SaveSettings = false;
  5531. int Options =
  5532. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5533. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5534. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5535. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5536. bool Continue =
  5537. (UseDefaults == 0) ||
  5538. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5539. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5540. if (Continue)
  5541. {
  5542. Configuration->Usage->Inc(L"Synchronizations");
  5543. UpdateCopyParamCounters(CopyParam);
  5544. TSynchronizeOptions SynchronizeOptions;
  5545. GetSynchronizeOptions(Params, SynchronizeOptions);
  5546. if (SaveSettings)
  5547. {
  5548. GUIConfiguration->SynchronizeParams = Params;
  5549. }
  5550. else
  5551. {
  5552. SaveMode = false;
  5553. }
  5554. TDateTime StartTime = Now();
  5555. TSynchronizeChecklist * Checklist = NULL;
  5556. try
  5557. {
  5558. DebugAssert(!FAutoOperation);
  5559. FAutoOperation = true;
  5560. try
  5561. {
  5562. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5563. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5564. int Files = -1;
  5565. if (DataList->Count >= 1)
  5566. {
  5567. Files = StrToIntDef(DataList->Strings[0], Files);
  5568. }
  5569. else
  5570. {
  5571. DataList->Add(UnicodeString());
  5572. }
  5573. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5574. FSynchronizeProgressForm->Start();
  5575. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5576. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5577. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5578. &SynchronizeOptions);
  5579. if (Terminal->SessionData->CacheDirectories)
  5580. {
  5581. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5582. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5583. }
  5584. }
  5585. __finally
  5586. {
  5587. FAutoOperation = false;
  5588. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5589. SAFE_DESTROY(FSynchronizeProgressForm);
  5590. }
  5591. TSynchronizeParams SynchronizeParams;
  5592. SynchronizeParams.LocalDirectory = LocalDirectory;
  5593. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5594. SynchronizeParams.Mode = Mode;
  5595. SynchronizeParams.CopyParam = &CopyParam;
  5596. SynchronizeParams.Params = Params;
  5597. SynchronizeParams.Checklist = Checklist;
  5598. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5599. SynchronizeParams.StartTime = &StartTime;
  5600. SynchronizeParams.OnProcessedItem = NULL;
  5601. Result = Checklist->Count;
  5602. if (Checklist->Count > 0)
  5603. {
  5604. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5605. {
  5606. if (!DoSynchronizeChecklistDialog(
  5607. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5608. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  5609. {
  5610. Result = -1;
  5611. }
  5612. }
  5613. else
  5614. {
  5615. FullSynchronize(SynchronizeParams, NULL, NULL);
  5616. }
  5617. }
  5618. else
  5619. {
  5620. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5621. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5622. }
  5623. }
  5624. __finally
  5625. {
  5626. delete Checklist;
  5627. }
  5628. OperationComplete(StartTime);
  5629. }
  5630. else
  5631. {
  5632. Result = -1;
  5633. }
  5634. return Result;
  5635. }
  5636. //---------------------------------------------------------------------------
  5637. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5638. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5639. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5640. {
  5641. if (Collect)
  5642. {
  5643. DebugAssert(FSynchronizeProgressForm != NULL);
  5644. if (!FSynchronizeProgressForm->Started)
  5645. {
  5646. FSynchronizeProgressForm->Start();
  5647. }
  5648. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5649. SetTaskbarListProgressValue(CompareProgress);
  5650. }
  5651. else
  5652. {
  5653. DebugAssert(FSynchronizeProgressForm == NULL);
  5654. DebugAssert(FProgressForm != NULL);
  5655. }
  5656. }
  5657. //---------------------------------------------------------------------------
  5658. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5659. {
  5660. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  5661. TRemoteFile * File = NULL;
  5662. Terminal->ReadFile(FullFileName, File);
  5663. if (File != NULL)
  5664. {
  5665. std::unique_ptr<TRemoteFile> FileOwner(File);
  5666. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5667. Application->ShowMainForm = false;
  5668. Application->Run();
  5669. }
  5670. }
  5671. //---------------------------------------------------------------------------
  5672. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  5673. {
  5674. DebugFail();
  5675. }
  5676. //---------------------------------------------------------------------------
  5677. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5678. {
  5679. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5680. SessionData->Assign(Terminal->SessionData);
  5681. UpdateSessionData(SessionData.get());
  5682. if (Terminal->Active)
  5683. {
  5684. Terminal->UpdateSessionCredentials(SessionData.get());
  5685. }
  5686. if (!Terminal->SessionData->HasSessionName())
  5687. {
  5688. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5689. // once IsWorkspace is cleared
  5690. SessionData->Name = UnicodeString();
  5691. }
  5692. // Most uses of this method ends with saving the session, and we do not want to store the IsWorkspace flag.
  5693. // Particularly, with SaveHiddenDuplicateSession, it won't work as TSessionData::ParseUrl explicitly ignored workspace sessions.
  5694. SessionData->IsWorkspace = false;
  5695. return SessionData.release();
  5696. }
  5697. //---------------------------------------------------------------------------
  5698. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5699. {
  5700. TSessionData * SessionData = CloneCurrentSessionData();
  5701. try
  5702. {
  5703. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5704. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5705. }
  5706. __finally
  5707. {
  5708. delete SessionData;
  5709. }
  5710. }
  5711. //---------------------------------------------------------------------------
  5712. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5713. {
  5714. TTerminalManager * Manager = TTerminalManager::Instance();
  5715. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5716. if (DebugAlwaysTrue(Terminal != NULL))
  5717. {
  5718. // Update (Managed)Terminal->StateData
  5719. UpdateTerminal(Terminal);
  5720. }
  5721. Manager->SaveWorkspace(DataList.get());
  5722. return DataList.release();
  5723. }
  5724. //---------------------------------------------------------------------------
  5725. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5726. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5727. {
  5728. WinConfiguration->LastWorkspace = Name;
  5729. if (!SavePasswords)
  5730. {
  5731. for (int Index = 0; Index < DataList->Count; Index++)
  5732. {
  5733. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5734. if (SessionData->Link.IsEmpty())
  5735. {
  5736. SessionData->ClearSessionPasswords();
  5737. }
  5738. }
  5739. }
  5740. StoredSessions->NewWorkspace(Name, DataList);
  5741. // modified only
  5742. StoredSessions->Save(false, Explicit);
  5743. }
  5744. //---------------------------------------------------------------------------
  5745. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5746. {
  5747. return
  5748. DefaultStr(WinConfiguration->LastWorkspace,
  5749. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5750. }
  5751. //---------------------------------------------------------------------------
  5752. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5753. {
  5754. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5755. bool AnyNonStoredSessionWithPassword = false;
  5756. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5757. bool AllNonStoredSessionsAnonymous = true;
  5758. TTerminalManager * Manager = TTerminalManager::Instance();
  5759. for (int Index = 0; Index < DataList->Count; Index++)
  5760. {
  5761. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5762. if (SessionData->Link.IsEmpty())
  5763. {
  5764. if (SessionData->HasAnySessionPassword())
  5765. {
  5766. AnyNonStoredSessionWithPassword = true;
  5767. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  5768. {
  5769. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5770. }
  5771. }
  5772. if (!SameText(SessionData->UserName, AnonymousUserName))
  5773. {
  5774. AllNonStoredSessionsAnonymous = false;
  5775. }
  5776. }
  5777. }
  5778. bool SavePasswords;
  5779. bool * PSavePasswords;
  5780. bool NotRecommendedSavingPasswords =
  5781. !CustomWinConfiguration->UseMasterPassword &&
  5782. !AllNonStoredSessionsAnonymous;
  5783. if (Configuration->DisablePasswordStoring ||
  5784. !AnyNonStoredSessionWithPassword)
  5785. {
  5786. PSavePasswords = NULL;
  5787. SavePasswords = false;
  5788. }
  5789. else
  5790. {
  5791. PSavePasswords = &SavePasswords;
  5792. SavePasswords =
  5793. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5794. !NotRecommendedSavingPasswords;
  5795. }
  5796. UnicodeString Name = WorkspaceName();
  5797. bool CreateShortcut = false;
  5798. bool Result =
  5799. DoSaveWorkspaceDialog(
  5800. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5801. EnableAutoSave);
  5802. if (Result)
  5803. {
  5804. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5805. if (CreateShortcut)
  5806. {
  5807. TOperationVisualizer Visualizer;
  5808. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5809. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5810. }
  5811. if (EnableAutoSave)
  5812. {
  5813. WinConfiguration->AutoSaveWorkspace = true;
  5814. WinConfiguration->AutoWorkspace = Name;
  5815. if (PSavePasswords != NULL)
  5816. {
  5817. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5818. }
  5819. }
  5820. }
  5821. return Result;
  5822. }
  5823. //---------------------------------------------------------------------------
  5824. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TManagedTerminal * Terminal)
  5825. {
  5826. DebugAssert(!IsLocalBrowserMode());
  5827. SAFE_DESTROY(Terminal->RemoteExplorerState);
  5828. if (WinConfiguration->PreservePanelState)
  5829. {
  5830. Terminal->RemoteExplorerState = RemoteDirView->SaveState();
  5831. }
  5832. UpdateSessionData(Terminal->StateData);
  5833. }
  5834. //---------------------------------------------------------------------------
  5835. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5836. {
  5837. // Keep in sync with TSessionData::CopyStateData
  5838. DebugAssert(!IsLocalBrowserMode());
  5839. DebugAssert(Data != NULL);
  5840. // This is inconsistent with how color (for example) is handled.
  5841. Data->Name = Terminal->SessionData->Name;
  5842. // cannot use RemoteDirView->Path, because it is empty if connection
  5843. // was already closed
  5844. // also only peek, we may not be connected at all atm
  5845. // so make sure we do not try retrieving current directory from the server
  5846. // (particularly with FTP)
  5847. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5848. if (!ACurrentDirectory.IsEmpty())
  5849. {
  5850. Data->RemoteDirectory = ACurrentDirectory;
  5851. }
  5852. Data->Color = SessionColor;
  5853. }
  5854. //---------------------------------------------------------------------------
  5855. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5856. {
  5857. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5858. DebugAssert(Toolbar != NULL);
  5859. for (int i = 0; i < Toolbar->Items->Count; i++)
  5860. {
  5861. TTBXCustomDropDownItem * DropDownItem;
  5862. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5863. if (DropDownItem != NULL)
  5864. {
  5865. ToolbarItemResize(DropDownItem,
  5866. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5867. Toolbar->NonClientWidth);
  5868. break;
  5869. }
  5870. }
  5871. }
  5872. //---------------------------------------------------------------------------
  5873. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5874. {
  5875. Item->EditWidth = Width;
  5876. }
  5877. //---------------------------------------------------------------------------
  5878. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5879. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5880. {
  5881. for (int i = 0; i < Toolbar->Items->Count; i++)
  5882. {
  5883. TTBXCustomDropDownItem * DropDownItem;
  5884. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5885. if (DropDownItem != NULL)
  5886. {
  5887. ASize.x -= DropDownItem->EditWidth;
  5888. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5889. }
  5890. }
  5891. }
  5892. //---------------------------------------------------------------------------
  5893. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5894. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5895. {
  5896. if (WinConfiguration->DDWarnLackOfTempSpace)
  5897. {
  5898. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5899. if (!ADrive.IsEmpty())
  5900. {
  5901. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5902. DebugAssert(RequiredSpace >= 0);
  5903. __int64 RequiredWithReserve;
  5904. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5905. if (FreeSpace < RequiredWithReserve)
  5906. {
  5907. unsigned int Result;
  5908. TMessageParams Params(mpNeverAskAgainCheck);
  5909. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5910. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5911. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5912. if (Result == qaNeverAskAgain)
  5913. {
  5914. WinConfiguration->DDWarnLackOfTempSpace = false;
  5915. }
  5916. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5917. }
  5918. }
  5919. }
  5920. }
  5921. //---------------------------------------------------------------------------
  5922. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5923. {
  5924. DoOpenDirectoryDialog(odAddBookmark, Side);
  5925. }
  5926. //---------------------------------------------------------------------------
  5927. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5928. {
  5929. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5930. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  5931. try
  5932. {
  5933. TCustomDirView * DView = DirView(Side);
  5934. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5935. {
  5936. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5937. }
  5938. }
  5939. catch (...)
  5940. {
  5941. delete VisitedDirectories;
  5942. throw;
  5943. }
  5944. return VisitedDirectories;
  5945. }
  5946. //---------------------------------------------------------------------------
  5947. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5948. TOpenDirectoryMode Mode, TOperationSide Side)
  5949. {
  5950. bool Continue = true;
  5951. if (Mode == odAddBookmark)
  5952. {
  5953. TMessageParams Params(mpNeverAskAgainCheck);
  5954. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5955. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5956. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  5957. unsigned int Answer =
  5958. MessageDialog(Message,
  5959. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  5960. if (Answer == qaNeverAskAgain)
  5961. {
  5962. Continue = true;
  5963. WinConfiguration->UseSharedBookmarks = true;
  5964. }
  5965. else if (Answer == qaOK)
  5966. {
  5967. Continue = true;
  5968. WinConfiguration->UseSharedBookmarks = false;
  5969. }
  5970. else
  5971. {
  5972. Continue = false;
  5973. }
  5974. }
  5975. if (Continue)
  5976. {
  5977. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  5978. try
  5979. {
  5980. UnicodeString Name = DirView(Side)->PathName;
  5981. bool Repeat;
  5982. do
  5983. {
  5984. Repeat = false;
  5985. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  5986. // do not allow switching to location profiles,
  5987. // if we are not connected
  5988. HasDirView[osLocal] && (Terminal != NULL)))
  5989. {
  5990. TWindowLock Lock(this);
  5991. if (!TryOpenDirectory(Side, Name))
  5992. {
  5993. Repeat = true;
  5994. Mode = odBrowse;
  5995. }
  5996. }
  5997. }
  5998. while (Repeat);
  5999. }
  6000. __finally
  6001. {
  6002. delete VisitedDirectories;
  6003. }
  6004. }
  6005. }
  6006. //---------------------------------------------------------------------------
  6007. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6008. {
  6009. DebugAssert(!FTerminal->CommandSessionOpened);
  6010. return TTerminalManager::Instance()->ConnectTerminal(FTerminal->CommandSession);
  6011. }
  6012. //---------------------------------------------------------------------------
  6013. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6014. {
  6015. bool Result = FTerminal->IsCapable[Capability] ||
  6016. FTerminal->CommandSessionOpened;
  6017. if (!Result)
  6018. {
  6019. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  6020. if (!GUIConfiguration->ConfirmCommandSession)
  6021. {
  6022. Result = true;
  6023. }
  6024. else
  6025. {
  6026. TMessageParams Params(mpNeverAskAgainCheck);
  6027. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6028. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6029. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6030. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6031. if (Answer == qaNeverAskAgain)
  6032. {
  6033. GUIConfiguration->ConfirmCommandSession = false;
  6034. Result = true;
  6035. }
  6036. else if (Answer == qaOK)
  6037. {
  6038. Result = true;
  6039. }
  6040. }
  6041. if (Result)
  6042. {
  6043. Result = CommandSessionFallback();
  6044. }
  6045. }
  6046. return Result;
  6047. }
  6048. //---------------------------------------------------------------------------
  6049. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6050. {
  6051. if (EnsureCommandSessionFallback(fcAnyCommand))
  6052. {
  6053. DoConsoleDialog(Terminal, Command);
  6054. }
  6055. }
  6056. //---------------------------------------------------------------------------
  6057. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6058. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6059. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6060. {
  6061. if (!IsSideLocalBrowser(osRemote) &&
  6062. IsFileControl(DropSourceControl, osRemote) &&
  6063. (FDDExtMapFile != NULL))
  6064. {
  6065. Accept = true;
  6066. }
  6067. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6068. DebugAssert(FDDTargetControl != NULL);
  6069. }
  6070. //---------------------------------------------------------------------------
  6071. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6072. _di_IDataObject DataObj, int KeyState,
  6073. const TPoint & Point, int & Effect, bool & Accept)
  6074. {
  6075. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6076. }
  6077. //---------------------------------------------------------------------------
  6078. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6079. _di_IDataObject DataObj, int KeyState,
  6080. const TPoint & Point, int & Effect, bool & Accept)
  6081. {
  6082. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6083. }
  6084. //---------------------------------------------------------------------------
  6085. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6086. TObject * Sender)
  6087. {
  6088. DebugUsedParam(Sender);
  6089. DebugAssert(FDDTargetControl == Sender);
  6090. FDDTargetControl = NULL;
  6091. }
  6092. //---------------------------------------------------------------------------
  6093. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  6094. {
  6095. FileControlDDDragLeave(SessionsPageControl);
  6096. }
  6097. //---------------------------------------------------------------------------
  6098. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  6099. {
  6100. FileControlDDDragLeave(QueueView3);
  6101. }
  6102. //---------------------------------------------------------------------------
  6103. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6104. {
  6105. DebugAssert(!IsSideLocalBrowser(Side));
  6106. DebugUsedParam(Side);
  6107. bool Edit = false;
  6108. TRemoteFile * File = NULL;
  6109. UnicodeString FileName;
  6110. UnicodeString PointTo;
  6111. bool SymbolicLink = true;
  6112. if (RemoteDirView->ItemFocused)
  6113. {
  6114. DebugAssert(RemoteDirView->ItemFocused->Data);
  6115. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6116. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6117. if (Edit)
  6118. {
  6119. FileName = File->FileName;
  6120. PointTo = File->LinkTo;
  6121. }
  6122. else
  6123. {
  6124. PointTo = File->FileName;
  6125. }
  6126. }
  6127. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6128. Terminal->IsCapable[fcHardLink]))
  6129. {
  6130. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6131. if (Edit)
  6132. {
  6133. DebugAssert(File->FileName == FileName);
  6134. int Params = dfNoRecursive;
  6135. Terminal->ExceptionOnFail = true;
  6136. try
  6137. {
  6138. Terminal->DeleteFile(L"", File, &Params);
  6139. }
  6140. __finally
  6141. {
  6142. Terminal->ExceptionOnFail = false;
  6143. }
  6144. }
  6145. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6146. }
  6147. }
  6148. //---------------------------------------------------------------------------
  6149. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6150. {
  6151. return
  6152. (IsSideLocalBrowser(Side) ||
  6153. (HasActiveTerminal() &&
  6154. Terminal->ResolvingSymlinks &&
  6155. Terminal->IsCapable[fcSymbolicLink]));
  6156. }
  6157. //---------------------------------------------------------------------------
  6158. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6159. {
  6160. return
  6161. !IsSideLocalBrowser(FCurrentSide) &&
  6162. (RemoteDirView->ItemFocused != NULL) &&
  6163. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6164. Terminal->SessionData->ResolveSymlinks;
  6165. }
  6166. //---------------------------------------------------------------------------
  6167. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6168. {
  6169. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6170. FForceExecution = true;
  6171. try
  6172. {
  6173. DirView(osCurrent)->ExecuteCurrentFile();
  6174. }
  6175. __finally
  6176. {
  6177. FForceExecution = false;
  6178. }
  6179. }
  6180. //---------------------------------------------------------------------------
  6181. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6182. {
  6183. TEditorData ExternalEditor;
  6184. ExternalEditor.Editor = edExternal;
  6185. bool Remember = false;
  6186. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6187. {
  6188. if (Remember)
  6189. {
  6190. TEditorList * EditorList = new TEditorList;
  6191. try
  6192. {
  6193. *EditorList = *WinConfiguration->EditorList;
  6194. bool Found = false;
  6195. int i = 0;
  6196. while (!Found && (i < EditorList->Count))
  6197. {
  6198. const TEditorPreferences * Editor = EditorList->Editors[i];
  6199. if ((Editor->Data->Editor == edExternal) &&
  6200. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6201. {
  6202. Found = true;
  6203. }
  6204. i++;
  6205. }
  6206. if (!Found)
  6207. {
  6208. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6209. WinConfiguration->EditorList = EditorList;
  6210. }
  6211. }
  6212. __finally
  6213. {
  6214. delete EditorList;
  6215. }
  6216. }
  6217. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6218. }
  6219. }
  6220. //---------------------------------------------------------------------------
  6221. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6222. {
  6223. FEditorManager->ProcessFiles(FileTerminalRemoved, ATerminal);
  6224. if (FFileFindTerminal == ATerminal)
  6225. {
  6226. FFileFindTerminal = NULL;
  6227. HideFileFindDialog();
  6228. }
  6229. if (FClipboardTerminal == ATerminal)
  6230. {
  6231. ClipboardClear(); // implies ClipboardStop
  6232. }
  6233. if ((FOnSynchronizeAbort != NULL) &&
  6234. (ATerminal == FTerminal))
  6235. {
  6236. FOnSynchronizeAbort(NULL);
  6237. }
  6238. }
  6239. //---------------------------------------------------------------------------
  6240. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6241. {
  6242. DetachTerminal(Sender);
  6243. }
  6244. //---------------------------------------------------------------------------
  6245. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6246. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6247. {
  6248. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6249. DebugAssert(Terminal != NULL);
  6250. if (Data->Terminal == Terminal)
  6251. {
  6252. Data->Terminal = NULL;
  6253. }
  6254. }
  6255. //---------------------------------------------------------------------------
  6256. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  6257. {
  6258. UpdateControls();
  6259. SessionColor = TColor(0);
  6260. UpdateRemotePathComboBox(false);
  6261. NeedSession(false);
  6262. }
  6263. //---------------------------------------------------------------------------
  6264. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6265. {
  6266. try
  6267. {
  6268. // Cache, as the login dialog can change its value
  6269. // See also DoShow. Also see TTerminalManager::NewSession.
  6270. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6271. try
  6272. {
  6273. if (ShowLogin)
  6274. {
  6275. bool ReloadSessions = !Startup;
  6276. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this);
  6277. }
  6278. else if (Startup && WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  6279. // This detects if workspace was saved the last time the main widow was closed
  6280. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  6281. {
  6282. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false, true);
  6283. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  6284. }
  6285. }
  6286. __finally
  6287. {
  6288. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6289. // (so there was no chance for the user to open any session yet)
  6290. if ((ShowLogin || !Startup) &&
  6291. !WinConfiguration->KeepOpenWhenNoSession &&
  6292. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6293. {
  6294. TerminateApplication();
  6295. }
  6296. }
  6297. }
  6298. catch (EAbort &)
  6299. {
  6300. // swallow
  6301. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6302. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6303. }
  6304. }
  6305. //---------------------------------------------------------------------------
  6306. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged()
  6307. {
  6308. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  6309. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6310. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  6311. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6312. try
  6313. {
  6314. while ((SessionsPageControl->PageCount > TerminalList->Count + 1) ||
  6315. // Clear the design time unthemed tab
  6316. ((SessionsPageControl->PageCount > 0) &&
  6317. (dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[SessionsPageControl->PageCount - 1]) == NULL)))
  6318. {
  6319. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6320. }
  6321. for (int Index = 0; Index <= TerminalList->Count; Index++)
  6322. {
  6323. TThemeTabSheet * TabSheet;
  6324. if (Index >= SessionsPageControl->PageCount)
  6325. {
  6326. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6327. TabSheet->PageControl = SessionsPageControl;
  6328. }
  6329. else
  6330. {
  6331. TabSheet = DebugNotNull(dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[Index]));
  6332. }
  6333. bool IsSessionTab = (Index < TerminalList->Count);
  6334. if (IsSessionTab)
  6335. {
  6336. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  6337. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6338. UpdateSessionTab(TabSheet);
  6339. }
  6340. else
  6341. {
  6342. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  6343. TabSheet->Tag = 0; // not really needed
  6344. TabSheet->Shadowed = false;
  6345. // 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)
  6346. UpdateNewSessionTab();
  6347. }
  6348. TabSheet->ShowCloseButton = IsSessionTab;
  6349. }
  6350. }
  6351. __finally
  6352. {
  6353. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6354. }
  6355. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  6356. }
  6357. //---------------------------------------------------------------------------
  6358. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  6359. {
  6360. DoTerminalListChanged();
  6361. }
  6362. //---------------------------------------------------------------------------
  6363. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  6364. {
  6365. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6366. DebugAssert(TabSheet->ImageIndex == 0);
  6367. TabSheet->Caption =
  6368. WinConfiguration->SelectiveToolbarText ?
  6369. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  6370. UnicodeString();
  6371. }
  6372. //---------------------------------------------------------------------------
  6373. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  6374. {
  6375. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6376. }
  6377. //---------------------------------------------------------------------------
  6378. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  6379. {
  6380. if (DebugAlwaysTrue(TabSheet != NULL))
  6381. {
  6382. TManagedTerminal * ATerminal = GetSessionTabTerminal(TabSheet);
  6383. if (DebugAlwaysTrue(ATerminal != NULL))
  6384. {
  6385. TColor Color = (ATerminal == FTerminal) ? FSessionColor : ATerminal->StateData->Color;
  6386. TabSheet->ImageIndex = AddSessionColor(Color);
  6387. UnicodeString TabCaption = TTerminalManager::Instance()->GetTerminalTitle(ATerminal, true);
  6388. TabSheet->Caption = SessionsPageControl->FormatCaptionWithCloseButton(TabCaption);
  6389. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  6390. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  6391. {
  6392. ThemeTabSheet->Shadowed = !ATerminal->Active;
  6393. }
  6394. }
  6395. }
  6396. }
  6397. //---------------------------------------------------------------------------
  6398. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6399. {
  6400. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6401. TManagedTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  6402. bool Result = (Terminal != NULL);
  6403. if (Result)
  6404. {
  6405. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6406. }
  6407. else
  6408. {
  6409. try
  6410. {
  6411. NewSession();
  6412. }
  6413. __finally
  6414. {
  6415. DoTerminalListChanged();
  6416. }
  6417. FSessionsPageControlNewSessionTime = Now();
  6418. }
  6419. return Result;
  6420. }
  6421. //---------------------------------------------------------------------------
  6422. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6423. {
  6424. SessionTabSwitched();
  6425. }
  6426. //---------------------------------------------------------------------------
  6427. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6428. {
  6429. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6430. DebugAssert(TransferList != NULL);
  6431. UnicodeString Name;
  6432. if (TransferList->ItemIndex <= 0)
  6433. {
  6434. Name = L"";
  6435. }
  6436. else
  6437. {
  6438. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6439. }
  6440. if (FCopyParamAutoSelected.IsEmpty())
  6441. {
  6442. // if previous preset was not autoselected, make new preset the "default"
  6443. FCopyParamDefault = Name;
  6444. }
  6445. GUIConfiguration->CopyParamCurrent = Name;
  6446. }
  6447. //---------------------------------------------------------------------------
  6448. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6449. {
  6450. // sanity check
  6451. bool ExistingPreset =
  6452. (FTransferListHoverIndex >= 0) &&
  6453. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6454. DebugAssert(ExistingPreset);
  6455. if (ExistingPreset)
  6456. {
  6457. HDC DC = GetDC(0);
  6458. TCanvas * Canvas = new TCanvas();
  6459. try
  6460. {
  6461. Canvas->Handle = DC;
  6462. Canvas->Font = GetToolbarFont(this);
  6463. UnicodeString Name;
  6464. if (FTransferListHoverIndex == 0)
  6465. {
  6466. Name = L"";
  6467. }
  6468. else
  6469. {
  6470. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6471. }
  6472. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6473. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6474. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6475. static_cast<TObject*>(GetComponent(fcTransferList)));
  6476. UnicodeString InfoStr =
  6477. GUIConfiguration->CopyParamPreset[Name].
  6478. GetInfoStr(L"; ",
  6479. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  6480. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6481. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6482. {
  6483. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6484. {
  6485. InfoStr.SetLength(InfoStr.Length() - 1);
  6486. }
  6487. InfoStr += Ellipsis;
  6488. }
  6489. // UpdateCaption does not cause invalidation of whole submenu, while
  6490. // setting Caption property does.
  6491. // also it probably does not resize the label, even if necessary
  6492. // (we do not want that anyway)
  6493. TransferLabel->UpdateCaption(InfoStr);
  6494. }
  6495. __finally
  6496. {
  6497. Canvas->Handle = NULL;
  6498. ReleaseDC(0, DC);
  6499. delete Canvas;
  6500. }
  6501. }
  6502. }
  6503. //---------------------------------------------------------------------------
  6504. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6505. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6506. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6507. {
  6508. if (FTransferListHoverIndex != AHoverIndex)
  6509. {
  6510. FTransferListHoverIndex = AHoverIndex;
  6511. UpdateTransferLabel();
  6512. }
  6513. }
  6514. //---------------------------------------------------------------------------
  6515. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6516. {
  6517. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6518. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6519. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6520. {
  6521. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6522. {
  6523. if (!NonVisualDataModule->Busy)
  6524. {
  6525. OpenDirectory(GetSide(osCurrent));
  6526. }
  6527. Message.Result = 1;
  6528. }
  6529. else
  6530. {
  6531. TForm::Dispatch(&Message);
  6532. }
  6533. }
  6534. else
  6535. {
  6536. TForm::Dispatch(&Message);
  6537. }
  6538. }
  6539. //---------------------------------------------------------------------------
  6540. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6541. {
  6542. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6543. if ((FIgnoreNextDialogChar != 0) &&
  6544. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6545. {
  6546. Message.Result = 1;
  6547. }
  6548. else
  6549. {
  6550. TForm::Dispatch(&Message);
  6551. }
  6552. FIgnoreNextDialogChar = 0;
  6553. }
  6554. //---------------------------------------------------------------------------
  6555. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6556. {
  6557. // The four low-order bits are used internally by Windows
  6558. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6559. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6560. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6561. // 61730 - restore thru double click - undocumented
  6562. // 61490 - maximize thru double click - undocumented
  6563. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6564. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6565. {
  6566. SysResizing(Cmd);
  6567. }
  6568. TForm::Dispatch(&Message);
  6569. }
  6570. //---------------------------------------------------------------------------
  6571. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6572. {
  6573. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6574. // Also there no point returning TRUE as we are not able to
  6575. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6576. // Hence the process termination might be safer :)
  6577. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6578. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6579. {
  6580. Message.Result = FALSE;
  6581. }
  6582. else
  6583. {
  6584. Message.Result = TRUE;
  6585. }
  6586. // Do not call default handling as that triggers OnCloseQuery,
  6587. // where our implementation will popup confirmation dialogs, what we do not want,
  6588. // as per Vista guidelines:
  6589. // https://docs.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6590. }
  6591. //---------------------------------------------------------------------------
  6592. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6593. {
  6594. if (Message.EndSession && IsApplicationMinimized())
  6595. {
  6596. // WORKAROUND
  6597. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6598. // when it receives WM_ENDSESSION,
  6599. // but that sometimes (particularly when application is minimized) cause crashes.
  6600. // Crash popups message that blocks log off.
  6601. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6602. // so we call ExitProcess() immediately, not even trying to cleanup.
  6603. // It still causes beep, so there's likely some popup, but it does not block
  6604. // log off.
  6605. ExitProcess(0);
  6606. }
  6607. TForm::Dispatch(&Message);
  6608. }
  6609. #ifdef _DEBUG
  6610. //---------------------------------------------------------------------------
  6611. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6612. {
  6613. TForm::Dispatch(&Message);
  6614. }
  6615. #endif
  6616. //---------------------------------------------------------------------------
  6617. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6618. {
  6619. }
  6620. //---------------------------------------------------------------------------
  6621. void __fastcall TCustomScpExplorerForm::DoShow()
  6622. {
  6623. // only now are the controls resized finally, so the size constraints
  6624. // will not conflict with possibly very small window size
  6625. RestoreFormParams();
  6626. // This indicates that the form was rescaled after parameters were loaded and
  6627. // as most probably the parameters were actually saved with the current form scaling
  6628. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6629. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6630. // and no rounding problems will occur.
  6631. // See also RestoreForm.
  6632. // (we should be safe to call RestoreParams unconditionally)
  6633. if (PixelsPerInch != Screen->PixelsPerInch)
  6634. {
  6635. RestoreParams();
  6636. }
  6637. FixControlsPlacement();
  6638. if (Position == poDefaultPosOnly)
  6639. {
  6640. CutFormToDesktop(this);
  6641. }
  6642. TForm::DoShow();
  6643. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6644. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6645. if (Terminal == NULL)
  6646. {
  6647. StartingDisconnected();
  6648. }
  6649. UpdatePixelsPerInchMainWindowCounter();
  6650. FShowing = true;
  6651. }
  6652. //---------------------------------------------------------------------------
  6653. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6654. {
  6655. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6656. }
  6657. //---------------------------------------------------------------------------
  6658. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  6659. {
  6660. DoTerminalListChanged();
  6661. }
  6662. //---------------------------------------------------------------------------
  6663. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6664. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6665. TNotifyEvent OnBalloonClick, TObject * UserData)
  6666. {
  6667. bool Do;
  6668. UnicodeString Message;
  6669. if (E == NULL)
  6670. {
  6671. Message = Str;
  6672. Do = true;
  6673. }
  6674. else
  6675. {
  6676. Do = ExceptionMessage(E, Message);
  6677. }
  6678. if (Do && WinConfiguration->BalloonNotifications)
  6679. {
  6680. UnicodeString Title;
  6681. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6682. {
  6683. Message = TrimLeft(Message);
  6684. }
  6685. else
  6686. {
  6687. Message = UnformatMessage(Message);
  6688. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6689. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6690. Title = LoadResourceString(Captions[Type]);
  6691. if (Terminal != NULL)
  6692. {
  6693. Title = FORMAT(L"%s - %s",
  6694. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), Title));
  6695. }
  6696. }
  6697. if (Seconds == 0)
  6698. {
  6699. Seconds = WinConfiguration->NotificationsTimeout;
  6700. }
  6701. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6702. }
  6703. }
  6704. //---------------------------------------------------------------------------
  6705. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6706. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6707. UnicodeString HelpKeyword, const TMessageParams * Params,
  6708. TTerminal * Terminal)
  6709. {
  6710. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6711. (FOnFeedSynchronizeError != NULL)) &&
  6712. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6713. {
  6714. if (FOnFeedSynchronizeError != NULL)
  6715. {
  6716. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6717. }
  6718. else
  6719. {
  6720. DebugAssert(FErrorList != NULL);
  6721. TStringList * MoreMessagesCopy = NULL;
  6722. if (MoreMessages)
  6723. {
  6724. MoreMessagesCopy = new TStringList();
  6725. MoreMessagesCopy->Assign(MoreMessages);
  6726. }
  6727. FErrorList->AddObject(Message, MoreMessagesCopy);
  6728. }
  6729. PopupTrayBalloon(Terminal, Message, Type);
  6730. return ContinueAnswer(Answers);
  6731. }
  6732. else
  6733. {
  6734. bool UseBalloon = FTrayIcon->Visible;
  6735. if (UseBalloon)
  6736. {
  6737. PopupTrayBalloon(Terminal, Message, Type);
  6738. }
  6739. unsigned int Result;
  6740. try
  6741. {
  6742. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6743. }
  6744. __finally
  6745. {
  6746. // cancel only balloon we popped up, otherwise we may cancel notification
  6747. // balloon that was there before the message dialog
  6748. // (such as "dd confirmation opt in balloon)
  6749. if (UseBalloon)
  6750. {
  6751. FTrayIcon->CancelBalloon();
  6752. }
  6753. }
  6754. return Result;
  6755. }
  6756. }
  6757. //---------------------------------------------------------------------------
  6758. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6759. TTerminal * Terminal, Exception * E)
  6760. {
  6761. if (FTrayIcon->Visible)
  6762. {
  6763. PopupTrayBalloon(Terminal, L"", qtError, E);
  6764. }
  6765. // particularly prevent opening new session from jump list,
  6766. // while exception is shown
  6767. NonVisualDataModule->StartBusy();
  6768. try
  6769. {
  6770. ShowExtendedExceptionEx(Terminal, E);
  6771. }
  6772. __finally
  6773. {
  6774. NonVisualDataModule->EndBusy();
  6775. FTrayIcon->CancelBalloon();
  6776. }
  6777. }
  6778. //---------------------------------------------------------------------------
  6779. void __fastcall TCustomScpExplorerForm::TerminalReady()
  6780. {
  6781. if (Terminal->Active)
  6782. {
  6783. InitStatusBar();
  6784. }
  6785. // cannot rely on active page being page for active terminal,
  6786. // as it can happen that active page is the "new session" page
  6787. // (e.g. when reconnecting active terminal, while login dialog
  6788. // invoked from "new session" page is modal)
  6789. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6790. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  6791. }
  6792. //---------------------------------------------------------------------------
  6793. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6794. TTerminal * Terminal, Exception * E)
  6795. {
  6796. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6797. if (!Terminal->Active)
  6798. {
  6799. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6800. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6801. {
  6802. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6803. }
  6804. }
  6805. }
  6806. //---------------------------------------------------------------------------
  6807. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6808. UnicodeString Message, TQueryType Type,
  6809. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6810. {
  6811. if ((E == NULL) ||
  6812. ExceptionMessage(E, Message))
  6813. {
  6814. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6815. if (Important)
  6816. {
  6817. Seconds *= 5;
  6818. }
  6819. UnicodeString NoteMessage(UnformatMessage(Message));
  6820. if (Terminal != NULL)
  6821. {
  6822. NoteMessage = FORMAT(L"%s: %s",
  6823. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), NoteMessage));
  6824. }
  6825. if (WinConfiguration->BalloonNotifications)
  6826. {
  6827. AddNote(NoteMessage);
  6828. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6829. }
  6830. else
  6831. {
  6832. FlashOnBackground();
  6833. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6834. }
  6835. }
  6836. }
  6837. //---------------------------------------------------------------------------
  6838. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6839. {
  6840. RestoreApp();
  6841. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6842. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6843. !NonVisualDataModule->Busy)
  6844. {
  6845. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  6846. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6847. if (!ComponentVisible[fcQueueView])
  6848. {
  6849. ToggleQueueVisibility();
  6850. GoToQueue();
  6851. }
  6852. }
  6853. }
  6854. //---------------------------------------------------------------------------
  6855. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  6856. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6857. {
  6858. UnicodeString Message;
  6859. TNotifyEvent OnClick = NULL;
  6860. TObject * UserData = NULL;
  6861. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6862. switch (Event)
  6863. {
  6864. case qeEmptyButMonitored:
  6865. if ((ATerminal != Terminal) || QueueInvisible)
  6866. {
  6867. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6868. OnClick = QueueEmptyNoteClicked;
  6869. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6870. TerminalNoteData->Terminal = ATerminal;
  6871. UserData = TerminalNoteData;
  6872. }
  6873. break;
  6874. case qeEmpty:
  6875. OperationComplete(ATerminal->QueueOperationStart);
  6876. break;
  6877. case qePendingUserAction:
  6878. if ((ATerminal != Terminal) ||
  6879. (QueueInvisible && !IsQueueAutoPopup()))
  6880. {
  6881. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6882. }
  6883. break;
  6884. default:
  6885. DebugFail();
  6886. }
  6887. if (!Message.IsEmpty())
  6888. {
  6889. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6890. }
  6891. }
  6892. //---------------------------------------------------------------------------
  6893. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6894. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6895. {
  6896. if (FDDExtMapFile != NULL)
  6897. {
  6898. CloseHandle(FDDExtMapFile);
  6899. FDDExtMapFile = NULL;
  6900. }
  6901. if (WinConfiguration->DDFakeFile)
  6902. {
  6903. DDFakeFileInitDrag(FileList, Created);
  6904. }
  6905. }
  6906. //---------------------------------------------------------------------------
  6907. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  6908. {
  6909. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  6910. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  6911. {
  6912. FFakeFileDropTarget = FileName;
  6913. }
  6914. }
  6915. //---------------------------------------------------------------------------
  6916. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  6917. {
  6918. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6919. if (!ForceDirectories(ApiPath(Result)))
  6920. {
  6921. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  6922. }
  6923. FileSetAttr(ApiPath(Result), faHidden);
  6924. return Result;
  6925. }
  6926. //---------------------------------------------------------------------------
  6927. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  6928. bool & Created)
  6929. {
  6930. FFakeFileDropTarget = UnicodeString();
  6931. FDragFakeDirectory = CreateFakeTransferDirectory();
  6932. FileList->AddItem(NULL, FDragFakeDirectory);
  6933. Created = true;
  6934. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  6935. {
  6936. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  6937. }
  6938. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6939. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6940. {
  6941. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6942. TDragExtCommStruct* CommStruct;
  6943. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6944. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6945. DebugAssert(CommStruct != NULL);
  6946. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  6947. CommStruct->Dragging = true;
  6948. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  6949. LENOF(CommStruct->DropDest));
  6950. NULL_TERMINATE(CommStruct->DropDest);
  6951. UnmapViewOfFile(CommStruct);
  6952. }
  6953. }
  6954. //---------------------------------------------------------------------------
  6955. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  6956. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  6957. {
  6958. bool Result;
  6959. DebugAssert(!IsLocalBrowserMode());
  6960. if (Sender == RemoteDirView)
  6961. {
  6962. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  6963. }
  6964. else
  6965. {
  6966. DebugAssert(Sender == RemoteDriveView);
  6967. TStrings * FileList = RemoteDriveView->DragFileList();
  6968. try
  6969. {
  6970. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  6971. }
  6972. __finally
  6973. {
  6974. delete FileList;
  6975. }
  6976. }
  6977. if (FDDTargetControl == RemoteDriveView)
  6978. {
  6979. RemoteDriveView->UpdateDropTarget();
  6980. }
  6981. // foRemoteMove happens when file/dir is dragged within the remote tree view
  6982. // or from tree view to dir view.
  6983. // foMove happens when file/dir is dragged from remote tree view outside of
  6984. // application via dragex
  6985. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  6986. (DropSourceControl == RemoteDriveView))
  6987. {
  6988. RemoteDriveView->UpdateDropSource();
  6989. }
  6990. return Result;
  6991. }
  6992. //---------------------------------------------------------------------------
  6993. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  6994. {
  6995. DebugAssert(!IsLocalBrowserMode());
  6996. // This also handles drops of remote files to queue.
  6997. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  6998. SAFE_DESTROY(FDDFileList);
  6999. if (!FFakeFileDropTarget.IsEmpty())
  7000. {
  7001. RemoveDir(ApiPath(FFakeFileDropTarget));
  7002. }
  7003. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7004. {
  7005. try
  7006. {
  7007. TDragResult DDResult = (Sender == RemoteDirView) ?
  7008. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7009. // Focus is moved to the target application,
  7010. // but as we are going to present the UI, we need to steal the focus back.
  7011. // This most likely won't work though (windows does not allow application
  7012. // to steal focus most of the time)
  7013. Application->BringToFront();
  7014. // On older version of Windows we never got drMove here, see also comment below.
  7015. // On Windows 10, we get the "move".
  7016. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7017. {
  7018. UnicodeString TargetDirectory;
  7019. TFileOperation Operation;
  7020. // drInvalid may mean drMove, see comment below
  7021. switch (DDResult)
  7022. {
  7023. case drCopy:
  7024. Operation = foCopy;
  7025. break;
  7026. case drMove:
  7027. Operation = foMove;
  7028. break;
  7029. default:
  7030. DebugFail();
  7031. case drInvalid:
  7032. // prefer "copy" for safety
  7033. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7034. break;
  7035. }
  7036. TTransferOperationParam Param;
  7037. UnicodeString CounterName;
  7038. bool ForceQueue;
  7039. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7040. {
  7041. // we get drInvalid both if d&d was intercepted by ddext,
  7042. // and when users drops on no-drop location.
  7043. // we tell the difference by existence of response from ddext,
  7044. // so we ignore absence of response in this case
  7045. if (DDResult != drInvalid)
  7046. {
  7047. // here we know that the extension is installed,
  7048. // as it is checked as soon as drag&drop starts from
  7049. // RemoteFileControlDDCreateDragFileList
  7050. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7051. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7052. {
  7053. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7054. }
  7055. else
  7056. {
  7057. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7058. }
  7059. }
  7060. }
  7061. else
  7062. {
  7063. // download using fake file
  7064. Param.Temp = false;
  7065. Param.DragDrop = true;
  7066. if (ForceQueue)
  7067. {
  7068. Param.Queue = asOn;
  7069. }
  7070. if (Sender == RemoteDirView)
  7071. {
  7072. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7073. }
  7074. if (RemoteFileControlFileOperation(Sender, Operation,
  7075. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7076. {
  7077. Configuration->Usage->Inc(CounterName);
  7078. }
  7079. }
  7080. }
  7081. }
  7082. __finally
  7083. {
  7084. CloseHandle(FDDExtMapFile);
  7085. FDDExtMapFile = NULL;
  7086. delete FDragFakeMonitors;
  7087. FDragFakeMonitors = NULL;
  7088. RemoveDir(ApiPath(FDragFakeDirectory));
  7089. FDragFakeDirectory = L"";
  7090. FFakeFileDropTarget = UnicodeString();
  7091. }
  7092. }
  7093. if (FDragDropSshTerminate.get() != NULL)
  7094. {
  7095. FDragDropSshTerminate->Rethrow();
  7096. }
  7097. }
  7098. //---------------------------------------------------------------------------
  7099. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7100. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7101. {
  7102. // Remember drop effect so we know (when user drops files), if we copy or move
  7103. FLastDropEffect = dwEffect;
  7104. }
  7105. //---------------------------------------------------------------------------
  7106. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7107. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7108. {
  7109. bool Result;
  7110. if (FDDTargetControl == QueueView3)
  7111. {
  7112. Directory = DefaultDownloadTargetDirectory();
  7113. Result = true;
  7114. CounterName = L"DownloadsDragDropQueue";
  7115. ForceQueue = true;
  7116. }
  7117. else if (!FFakeFileDropTarget.IsEmpty())
  7118. {
  7119. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7120. Result = true;
  7121. ForceQueue = false;
  7122. CounterName = L"DownloadsDragDropFakeFile";
  7123. }
  7124. else
  7125. {
  7126. ForceQueue = false;
  7127. Enabled = false;
  7128. try
  7129. {
  7130. int Timer = 0;
  7131. Result = false;
  7132. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7133. {
  7134. {
  7135. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7136. TDragExtCommStruct* CommStruct;
  7137. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7138. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7139. DebugAssert(CommStruct != NULL);
  7140. Result = !CommStruct->Dragging;
  7141. if (Result)
  7142. {
  7143. Directory = ExtractFilePath(CommStruct->DropDest);
  7144. CounterName = L"DownloadsDragDropExternalExt";
  7145. }
  7146. UnmapViewOfFile(CommStruct);
  7147. }
  7148. if (!Result)
  7149. {
  7150. Sleep(50);
  7151. Timer += 50;
  7152. Application->ProcessMessages();
  7153. }
  7154. }
  7155. }
  7156. __finally
  7157. {
  7158. Enabled = true;
  7159. }
  7160. }
  7161. return Result;
  7162. }
  7163. //---------------------------------------------------------------------------
  7164. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7165. const UnicodeString TempDir, int SecDelay)
  7166. {
  7167. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7168. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7169. if (FDelayedDeletionTimer == NULL)
  7170. {
  7171. DebugAssert(HandleAllocated());
  7172. FDelayedDeletionTimer = new TTimer(this);
  7173. FDelayedDeletionTimer->Interval = 10000;
  7174. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7175. }
  7176. else
  7177. {
  7178. FDelayedDeletionTimer->Enabled = true;
  7179. }
  7180. }
  7181. //---------------------------------------------------------------------------
  7182. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7183. {
  7184. DebugAssert(FDelayedDeletionList != NULL);
  7185. TDateTime N = Now();
  7186. TDateTime Alert;
  7187. UnicodeString Directory;
  7188. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7189. {
  7190. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7191. if ((N >= Alert) || (Sender == NULL))
  7192. {
  7193. Directory = FDelayedDeletionList->Strings[Index];
  7194. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7195. {
  7196. FDelayedDeletionList->Delete(Index);
  7197. }
  7198. }
  7199. }
  7200. if (FDelayedDeletionList->Count == 0)
  7201. {
  7202. FDelayedDeletionTimer->Enabled = false;
  7203. }
  7204. }
  7205. //---------------------------------------------------------------------------
  7206. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7207. {
  7208. DebugAssert(!IsLocalBrowserMode());
  7209. if (IsFileControl(FDDTargetControl, osRemote) ||
  7210. (FDDTargetControl == SessionsPageControl))
  7211. {
  7212. TTerminal * TargetTerminal = NULL;
  7213. TFileOperation Operation = foNone;
  7214. if (FDDTargetControl == SessionsPageControl)
  7215. {
  7216. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7217. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7218. // do not allow dropping on the "+" and disconnected tabs
  7219. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  7220. if (IsActiveTerminal(TargetTerminal))
  7221. {
  7222. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7223. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7224. {
  7225. Operation = foRemoteMove;
  7226. }
  7227. else
  7228. {
  7229. Operation = foRemoteCopy;
  7230. }
  7231. }
  7232. }
  7233. else
  7234. {
  7235. if (FLastDropEffect == DROPEFFECT_MOVE)
  7236. {
  7237. Operation = foRemoteMove;
  7238. }
  7239. else if (FLastDropEffect == DROPEFFECT_COPY)
  7240. {
  7241. Operation = foRemoteCopy;
  7242. }
  7243. }
  7244. if (Operation != foNone)
  7245. {
  7246. RemoteFileControlFileOperation(DropSourceControl,
  7247. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7248. }
  7249. // abort drag&drop
  7250. Abort();
  7251. }
  7252. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7253. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7254. (FDDTargetControl != QueueView3))
  7255. {
  7256. DebugAssert(!FDragTempDir.IsEmpty());
  7257. TTransferType Type;
  7258. UnicodeString TempDir = FDragTempDir;
  7259. // We clear FDragTempDir before calling
  7260. // just in case it fail (raises exception)
  7261. FDragTempDir = L"";
  7262. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7263. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7264. // empty directory parameter means temp directory -> don't display it!
  7265. UnicodeString TargetDir = L"";
  7266. int Options = 0;
  7267. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7268. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7269. {
  7270. Abort();
  7271. }
  7272. // TargetDir is set when dropped on local file control
  7273. // (this was workaround for legacy dirview event handling, now it should be
  7274. // made prettier)
  7275. if (TargetDir.IsEmpty())
  7276. {
  7277. TargetDir = TempDir;
  7278. if (ForceDirectories(ApiPath(TargetDir)))
  7279. {
  7280. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7281. FPendingTempSpaceWarn = true;
  7282. try
  7283. {
  7284. FDragDropOperation = true;
  7285. int Params = cpTemporary |
  7286. (Type == ttMove ? cpDelete : 0);
  7287. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7288. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7289. }
  7290. __finally
  7291. {
  7292. FDragDropOperation = false;
  7293. FPendingTempSpaceWarn = false;
  7294. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7295. }
  7296. }
  7297. else
  7298. {
  7299. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7300. }
  7301. }
  7302. }
  7303. }
  7304. //---------------------------------------------------------------------------
  7305. void __fastcall TCustomScpExplorerForm::DDDownload(
  7306. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  7307. {
  7308. DebugAssert(!IsLocalBrowserMode());
  7309. TAutoBatch AutoBatch(this);
  7310. UpdateCopyParamCounters(*CopyParam);
  7311. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7312. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7313. {
  7314. RemoteDriveView->UpdateDropSource();
  7315. }
  7316. }
  7317. //---------------------------------------------------------------------------
  7318. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7319. {
  7320. public:
  7321. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7322. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7323. {
  7324. }
  7325. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7326. {
  7327. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7328. TDataObjectFilesEx::AllowData(FormatEtc);
  7329. }
  7330. };
  7331. //---------------------------------------------------------------------------
  7332. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7333. TObject * Sender, TDataObject *& DataObject)
  7334. {
  7335. if (FDDExtMapFile != NULL)
  7336. {
  7337. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7338. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7339. {
  7340. Abort();
  7341. }
  7342. if (FileList->Count > 0)
  7343. {
  7344. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7345. if (!FilesObject->IsValid(true, true))
  7346. {
  7347. FilesObject->_Release();
  7348. }
  7349. else
  7350. {
  7351. DataObject = FilesObject;
  7352. }
  7353. }
  7354. }
  7355. }
  7356. //---------------------------------------------------------------------------
  7357. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7358. {
  7359. DebugFail();
  7360. }
  7361. //---------------------------------------------------------------------------
  7362. void __fastcall TCustomScpExplorerForm::GoToTree()
  7363. {
  7364. ComponentVisible[fcRemoteTree] = true;
  7365. DriveView(osRemote)->SetFocus();
  7366. }
  7367. //---------------------------------------------------------------------------
  7368. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7369. TPanelExport Export)
  7370. {
  7371. TCustomDirView * DirView = this->DirView(Side);
  7372. std::unique_ptr<TStrings> ExportData(new TStringList());
  7373. switch (Export)
  7374. {
  7375. case pePath:
  7376. ExportData->Add(DirView->PathName);
  7377. break;
  7378. case peFileList:
  7379. case peFullFileList:
  7380. {
  7381. bool FullPath = (Export == peFullFileList);
  7382. DirView->CreateFileList(false, FullPath, ExportData.get());
  7383. for (int Index = 0; Index < ExportData->Count; Index++)
  7384. {
  7385. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7386. {
  7387. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7388. }
  7389. }
  7390. }
  7391. break;
  7392. default:
  7393. DebugFail();
  7394. }
  7395. return ExportData.release();
  7396. }
  7397. //---------------------------------------------------------------------------
  7398. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7399. TPanelExport Export, TPanelExportDestination Destination)
  7400. {
  7401. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7402. PanelExportStore(Side, Export, Destination, ExportData.get());
  7403. }
  7404. //---------------------------------------------------------------------------
  7405. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7406. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7407. TStrings * ExportData)
  7408. {
  7409. if (Destination == pedClipboard)
  7410. {
  7411. TInstantOperationVisualizer Visualizer;
  7412. CopyToClipboard(ExportData);
  7413. }
  7414. else
  7415. {
  7416. DebugFail();
  7417. }
  7418. }
  7419. //---------------------------------------------------------------------------
  7420. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7421. {
  7422. TCustomDirView * DirView = this->DirView(Side);
  7423. UnicodeString Mask = DirView->Mask;
  7424. if (DoFilterMaskDialog(DirView, Mask))
  7425. {
  7426. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7427. Configuration->Usage->Inc(L"Filters");
  7428. }
  7429. }
  7430. //---------------------------------------------------------------------------
  7431. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7432. {
  7433. return FQueueController->DefaultOperation();
  7434. }
  7435. //---------------------------------------------------------------------------
  7436. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7437. TQueueOperation Operation, void ** Param)
  7438. {
  7439. switch (Operation)
  7440. {
  7441. case qoPreferences:
  7442. return true;
  7443. case qoGoTo:
  7444. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7445. case qoOnceEmpty:
  7446. return IsAnythingQueued();
  7447. default:
  7448. return FQueueController->AllowOperation(Operation, Param);
  7449. }
  7450. }
  7451. //---------------------------------------------------------------------------
  7452. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7453. {
  7454. if (DebugAlwaysTrue(QueueView3->Visible))
  7455. {
  7456. QueueView3->SetFocus();
  7457. }
  7458. }
  7459. //---------------------------------------------------------------------------
  7460. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7461. TQueueOperation Operation, void * Param)
  7462. {
  7463. if (Operation == qoGoTo)
  7464. {
  7465. GoToQueue();
  7466. }
  7467. else if (Operation == qoPreferences)
  7468. {
  7469. PreferencesDialog(pmQueue);
  7470. }
  7471. else
  7472. {
  7473. FQueueController->ExecuteOperation(Operation, Param);
  7474. }
  7475. }
  7476. //---------------------------------------------------------------------------
  7477. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7478. {
  7479. return (Queue != NULL) && Queue->Enabled;
  7480. }
  7481. //---------------------------------------------------------------------------
  7482. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7483. {
  7484. DebugAssert(Queue != NULL);
  7485. if (Queue != NULL)
  7486. {
  7487. Queue->Enabled = !Queue->Enabled;
  7488. }
  7489. }
  7490. //---------------------------------------------------------------------------
  7491. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7492. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7493. {
  7494. FQueueActedItem = QueueView3->ItemFocused;
  7495. }
  7496. //---------------------------------------------------------------------------
  7497. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7498. {
  7499. return TopDock->Height + QueueSplitter->Height;
  7500. }
  7501. //---------------------------------------------------------------------------
  7502. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  7503. {
  7504. return
  7505. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  7506. (QueueDock->Visible ? QueueDock->Height : 0) +
  7507. (QueueLabel->Visible ? QueueLabel->Height : 0);
  7508. }
  7509. //---------------------------------------------------------------------------
  7510. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  7511. {
  7512. return ScaleByTextHeight(this, 48);
  7513. }
  7514. //---------------------------------------------------------------------------
  7515. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7516. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7517. {
  7518. // when queue is hidden by double-clicking splitter, stray attempt to
  7519. // resize the panel with strange value arrives, make sure it is ignored
  7520. if (ComponentVisible[fcQueueView])
  7521. {
  7522. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  7523. if (NewSize > MaxHeightLimit)
  7524. {
  7525. NewSize = MaxHeightLimit;
  7526. }
  7527. int MinHeightLimit =
  7528. GetStaticQueuePanelComponentsHeight() +
  7529. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  7530. GetMinQueueViewHeight();
  7531. if (NewSize < MinHeightLimit)
  7532. {
  7533. NewSize = MinHeightLimit;
  7534. }
  7535. }
  7536. else
  7537. {
  7538. Accept = false;
  7539. }
  7540. }
  7541. //---------------------------------------------------------------------------
  7542. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  7543. {
  7544. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  7545. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  7546. if (QueueViewHeight < GetMinQueueViewHeight())
  7547. {
  7548. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  7549. }
  7550. }
  7551. //---------------------------------------------------------------------------
  7552. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7553. TDragObject *& /*DragObject*/)
  7554. {
  7555. FQueueActedItem = QueueView3->ItemFocused;
  7556. }
  7557. //---------------------------------------------------------------------------
  7558. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7559. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7560. {
  7561. Accept = true;
  7562. if (Source == QueueView3)
  7563. {
  7564. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7565. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7566. if (Accept)
  7567. {
  7568. TQueueItemProxy * QueueItem;
  7569. TQueueItemProxy * DestQueueItem;
  7570. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7571. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7572. Accept = (QueueItem != DestQueueItem) &&
  7573. (QueueItem->Status == TQueueItem::qsPending) &&
  7574. (DestQueueItem->Status == TQueueItem::qsPending);
  7575. }
  7576. }
  7577. }
  7578. //---------------------------------------------------------------------------
  7579. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7580. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7581. {
  7582. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7583. {
  7584. TQueueItemProxy * QueueItem;
  7585. TQueueItemProxy * DestQueueItem;
  7586. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7587. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7588. QueueItem->Move(DestQueueItem);
  7589. }
  7590. }
  7591. //---------------------------------------------------------------------------
  7592. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7593. {
  7594. if ((QueueView3->ItemFocused == NULL) &&
  7595. (QueueView3->Items->Count > 0))
  7596. {
  7597. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7598. }
  7599. QueueLabelUpdateStatus();
  7600. }
  7601. //---------------------------------------------------------------------------
  7602. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7603. {
  7604. QueueLabelUpdateStatus();
  7605. }
  7606. //---------------------------------------------------------------------------
  7607. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7608. {
  7609. QueueLabel->UpdateStatus();
  7610. }
  7611. //---------------------------------------------------------------------------
  7612. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7613. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7614. {
  7615. if (Selected)
  7616. {
  7617. NonVisualDataModule->UpdateNonVisibleActions();
  7618. }
  7619. }
  7620. //---------------------------------------------------------------------------
  7621. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7622. {
  7623. TDragDropFilesEx * Result = NULL;
  7624. if (Sender == SessionsPageControl)
  7625. {
  7626. Result = FSessionsDragDropFilesEx;
  7627. }
  7628. else if (Sender == QueueView3)
  7629. {
  7630. Result = FQueueDragDropFilesEx;
  7631. }
  7632. else
  7633. {
  7634. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7635. if (DirView != NULL)
  7636. {
  7637. Result = DirView->DragDropFilesEx;
  7638. }
  7639. else
  7640. {
  7641. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7642. if (DriveView != NULL)
  7643. {
  7644. Result = DriveView->DragDropFilesEx;
  7645. }
  7646. }
  7647. }
  7648. DebugAssert(Result != NULL);
  7649. return Result;
  7650. }
  7651. //---------------------------------------------------------------------------
  7652. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7653. {
  7654. return (DView->SelCount == DView->FilesCount);
  7655. }
  7656. //---------------------------------------------------------------------------
  7657. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7658. {
  7659. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7660. }
  7661. //---------------------------------------------------------------------------
  7662. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7663. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7664. {
  7665. DebugAssert(!IsLocalBrowserMode());
  7666. TFileOperation Operation;
  7667. switch (Effect)
  7668. {
  7669. case DROPEFFECT_MOVE:
  7670. Operation = foMove;
  7671. break;
  7672. case DROPEFFECT_COPY:
  7673. // occures on WinXP (reported by user)
  7674. default:
  7675. Operation = foCopy;
  7676. break;
  7677. };
  7678. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7679. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7680. if (DragDropFilesEx->FileList->Count > 0)
  7681. {
  7682. TStrings * FileList = new TStringList();
  7683. try
  7684. {
  7685. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7686. {
  7687. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7688. }
  7689. FDragDropOperation = true;
  7690. TTransferOperationParam Param;
  7691. Param.TargetDirectory = TargetPath;
  7692. // upload, no temp dirs
  7693. Param.Temp = false;
  7694. Param.DragDrop = DragDrop;
  7695. Param.Options =
  7696. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7697. if (ForceQueue)
  7698. {
  7699. Param.Queue = asOn;
  7700. }
  7701. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7702. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7703. {
  7704. if (IsFileControl(DropSourceControl, osLocal))
  7705. {
  7706. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7707. }
  7708. else
  7709. {
  7710. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7711. }
  7712. }
  7713. }
  7714. __finally
  7715. {
  7716. FDragDropOperation = false;
  7717. delete FileList;
  7718. }
  7719. }
  7720. }
  7721. //---------------------------------------------------------------------------
  7722. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7723. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7724. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7725. {
  7726. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7727. }
  7728. //---------------------------------------------------------------------------
  7729. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7730. TObject * Sender, int grfKeyState, int & dwEffect)
  7731. {
  7732. DebugAssert(!IsLocalBrowserMode());
  7733. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7734. if ((dwEffect != DROPEFFECT_NONE) &&
  7735. IsFileControl(DropSourceControl, osRemote))
  7736. {
  7737. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7738. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7739. (RemoteDirView->DropTarget == NULL))
  7740. {
  7741. dwEffect = DROPEFFECT_NONE;
  7742. }
  7743. else
  7744. {
  7745. if (dwEffect == DROPEFFECT_COPY)
  7746. {
  7747. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  7748. // currently we support copying always (at least via temporary directory);
  7749. // remove associated checks once this all proves stable and working
  7750. bool CopyCapable = true;
  7751. // if we do not support neither of operations, there's no discussion
  7752. if (!MoveCapable && !CopyCapable)
  7753. {
  7754. dwEffect = DROPEFFECT_NONE;
  7755. }
  7756. else
  7757. {
  7758. // The default effect inside remote panel is move,
  7759. // unless we do not support it, but support copy
  7760. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7761. {
  7762. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  7763. }
  7764. else
  7765. {
  7766. // with ctrl-down, we want copy unless it is not supported
  7767. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  7768. }
  7769. }
  7770. }
  7771. }
  7772. }
  7773. }
  7774. //---------------------------------------------------------------------------
  7775. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7776. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7777. {
  7778. DebugAssert(!IsLocalBrowserMode());
  7779. if (FDDTotalSize >= 0)
  7780. {
  7781. if (File->IsDirectory)
  7782. {
  7783. FDDTotalSize = -1;
  7784. }
  7785. else
  7786. {
  7787. FDDTotalSize += File->Size;
  7788. }
  7789. }
  7790. DebugAssert(!FDragTempDir.IsEmpty());
  7791. // TODO: this is quite ineffective
  7792. // TODO: what if invalid character replacement is disabled?
  7793. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7794. UnicodeString TransferFileName;
  7795. if (Sender == RemoteDriveView)
  7796. {
  7797. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7798. }
  7799. else
  7800. {
  7801. TransferFileName = File->FileName;
  7802. }
  7803. FDDFileList->AddObject(TransferFileName, File);
  7804. }
  7805. //---------------------------------------------------------------------------
  7806. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7807. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7808. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7809. {
  7810. // sometimes we do not get DDEnd so the list is not released
  7811. SAFE_DESTROY(FDDFileList);
  7812. FDDFileList = new TStringList();
  7813. FDragTempDir = WinConfiguration->TemporaryDir();
  7814. FDDTotalSize = 0;
  7815. FDragDropSshTerminate.reset(NULL);
  7816. }
  7817. //---------------------------------------------------------------------------
  7818. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7819. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7820. HRESULT & Result)
  7821. {
  7822. if (Result == DRAGDROP_S_DROP)
  7823. {
  7824. try
  7825. {
  7826. GlobalDragImageList->HideDragImage();
  7827. try
  7828. {
  7829. RemoteFileControlDDTargetDrop();
  7830. }
  7831. catch(ESshTerminate & E)
  7832. {
  7833. FDragDropSshTerminate.reset(E.Clone());
  7834. }
  7835. }
  7836. catch (Exception &E)
  7837. {
  7838. // If downloading fails we need to cancel drag&drop, otherwise
  7839. // Explorer shows error
  7840. // But by the way exception probably never reach this point as
  7841. // it's catched on way.
  7842. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7843. Result = DRAGDROP_S_CANCEL;
  7844. ShowExtendedException(Terminal, &E);
  7845. }
  7846. }
  7847. }
  7848. //---------------------------------------------------------------------------
  7849. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7850. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7851. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7852. {
  7853. TFileMasks::TParams MaskParams;
  7854. MaskParams.Size = Size;
  7855. MaskParams.Modification = Modification;
  7856. // this does not re-parse the mask if it is the same as the last time
  7857. FDirViewMatchMask = Masks;
  7858. bool ImplicitMatch;
  7859. Matches =
  7860. // RecurseInclude parameter has no effect as the Path is empty
  7861. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7862. (AllowImplicitMatches || !ImplicitMatch);
  7863. }
  7864. //---------------------------------------------------------------------------
  7865. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7866. TObject * Sender, TListItem * Item, WORD & Indexes)
  7867. {
  7868. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7869. UnicodeString Ext;
  7870. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  7871. {
  7872. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7873. }
  7874. else
  7875. {
  7876. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7877. }
  7878. if (SameText(Ext, Configuration->PartialExt))
  7879. {
  7880. Indexes |= oiPartial;
  7881. }
  7882. }
  7883. //---------------------------------------------------------------------------
  7884. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7885. {
  7886. return
  7887. DirViewEnabled(osCurrent) &&
  7888. DirView(osCurrent)->CanPasteFromClipBoard();
  7889. }
  7890. //---------------------------------------------------------------------------
  7891. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7892. {
  7893. bool Result = false;
  7894. if (CanPasteToDirViewFromClipBoard())
  7895. {
  7896. Result = true;
  7897. }
  7898. else
  7899. {
  7900. UnicodeString ClipboardText;
  7901. if (NonEmptyTextFromClipboard(ClipboardText) &&
  7902. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  7903. {
  7904. if (StoredSessions->IsUrl(ClipboardText))
  7905. {
  7906. Result = true;
  7907. }
  7908. else
  7909. {
  7910. Result = DirViewEnabled(osCurrent);
  7911. }
  7912. }
  7913. }
  7914. return Result;
  7915. }
  7916. //---------------------------------------------------------------------------
  7917. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7918. {
  7919. if (DoesClipboardContainOurFiles())
  7920. {
  7921. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  7922. {
  7923. DebugAssert(!IsLocalBrowserMode());
  7924. TTerminalManager * Manager = TTerminalManager::Instance();
  7925. TTerminal * TergetTerminal = Manager->ActiveTerminal;
  7926. Manager->ActiveTerminal = FClipboardTerminal;
  7927. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TergetTerminal);
  7928. }
  7929. }
  7930. else if (CanPasteToDirViewFromClipBoard())
  7931. {
  7932. DirView(osCurrent)->PasteFromClipBoard();
  7933. }
  7934. else
  7935. {
  7936. UnicodeString ClipboardText;
  7937. if (NonEmptyTextFromClipboard(ClipboardText))
  7938. {
  7939. if (StoredSessions->IsUrl(ClipboardText))
  7940. {
  7941. NewSession(ClipboardText);
  7942. }
  7943. else
  7944. {
  7945. DirView(osCurrent)->Path = ClipboardText;
  7946. }
  7947. }
  7948. }
  7949. }
  7950. //---------------------------------------------------------------------------
  7951. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  7952. {
  7953. // TBD
  7954. }
  7955. //---------------------------------------------------------------------------
  7956. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  7957. {
  7958. TCustomDirView * ADirView = DirView(Side);
  7959. ADirView->SelectAll(Mode);
  7960. ADirView->SetFocus();
  7961. }
  7962. //---------------------------------------------------------------------------
  7963. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  7964. {
  7965. TCustomDirView * ADirView = DirView(Side);
  7966. TFileFilter Filter;
  7967. DefaultFileFilter(Filter);
  7968. if (DoSelectMaskDialog(ADirView, Select, Filter))
  7969. {
  7970. Configuration->Usage->Inc(L"MaskSelections");
  7971. ADirView->SelectFiles(Filter, Select);
  7972. ADirView->SetFocus();
  7973. }
  7974. }
  7975. //---------------------------------------------------------------------------
  7976. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  7977. {
  7978. TCustomDirView * ADirView = DirView(Side);
  7979. ADirView->RestoreSelectedNames();
  7980. ADirView->SetFocus();
  7981. }
  7982. //---------------------------------------------------------------------------
  7983. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  7984. {
  7985. TCustomDirView * CurrentDirView = DirView(osCurrent);
  7986. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  7987. {
  7988. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  7989. UnicodeString Ext;
  7990. if (!IsSideLocalBrowser(osCurrent))
  7991. {
  7992. Ext = UnixExtractFileExt(FileName);
  7993. }
  7994. else
  7995. {
  7996. Ext = ExtractFileExt(FileName);
  7997. }
  7998. if (Ext.IsEmpty())
  7999. {
  8000. Ext = L".";
  8001. }
  8002. TFileFilter Filter;
  8003. Filter.Masks = FORMAT(L"*%s", (Ext));
  8004. Filter.Directories = false;
  8005. CurrentDirView->SelectFiles(Filter, Select);
  8006. }
  8007. }
  8008. //---------------------------------------------------------------------------
  8009. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8010. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8011. {
  8012. UnicodeString Result;
  8013. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  8014. {
  8015. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  8016. }
  8017. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8018. {
  8019. // noop
  8020. }
  8021. else
  8022. {
  8023. Result =
  8024. FMTLOAD(FILE_INFO_FORMAT,
  8025. (FormatBytes(FileInfo.SelectedSize),
  8026. FormatBytes(FileInfo.FilesSize),
  8027. FormatNumber(FileInfo.SelectedCount),
  8028. FormatNumber(FileInfo.FilesCount)));
  8029. }
  8030. return Result;
  8031. }
  8032. //---------------------------------------------------------------------------
  8033. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8034. TTBXStatusPanel * Panel, TOperationSide Side)
  8035. {
  8036. if (Panel->Index == 1)
  8037. {
  8038. ToggleShowHiddenFiles();
  8039. }
  8040. else if (Panel->Index == 2)
  8041. {
  8042. Filter(Side);
  8043. }
  8044. }
  8045. //---------------------------------------------------------------------------
  8046. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8047. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8048. {
  8049. DebugAssert(!StatusBar->SimplePanel);
  8050. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8051. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8052. }
  8053. //---------------------------------------------------------------------------
  8054. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8055. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8056. {
  8057. DebugAssert(StatusBar->Panels->Count >= 3);
  8058. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8059. if (FileInfo.HiddenCount > 0)
  8060. {
  8061. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8062. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8063. }
  8064. else
  8065. {
  8066. HiddenFilesPanel->ViewPriority = 0;
  8067. // not really necessary, just to cleanup no-longer-valid data
  8068. HiddenFilesPanel->Caption = L"";
  8069. }
  8070. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8071. if (FileInfo.FilteredCount > 0)
  8072. {
  8073. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8074. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8075. }
  8076. else
  8077. {
  8078. FilteredFilesPanel->ViewPriority = 0;
  8079. // not really necessary, just to cleanup no-longer-valid data
  8080. FilteredFilesPanel->Caption = L"";
  8081. }
  8082. }
  8083. //---------------------------------------------------------------------------
  8084. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8085. TObject * /*Sender*/)
  8086. {
  8087. if (DirView(osRemote)->Enabled)
  8088. {
  8089. DirView(osRemote)->SetFocus();
  8090. }
  8091. }
  8092. //---------------------------------------------------------------------------
  8093. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8094. {
  8095. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8096. switch (Config.Show)
  8097. {
  8098. case qvShow:
  8099. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8100. {
  8101. Config.Show = qvHide;
  8102. }
  8103. else
  8104. {
  8105. Config.Show = Config.LastHideShow;
  8106. }
  8107. break;
  8108. case qvHideWhenEmpty:
  8109. if (ComponentVisible[fcQueueView])
  8110. {
  8111. Config.Show = qvHide;
  8112. }
  8113. else
  8114. {
  8115. Config.LastHideShow = Config.Show;
  8116. Config.Show = qvShow;
  8117. }
  8118. break;
  8119. case qvHide:
  8120. Config.LastHideShow = Config.Show;
  8121. Config.Show = qvShow;
  8122. break;
  8123. }
  8124. WinConfiguration->QueueView = Config;
  8125. }
  8126. //---------------------------------------------------------------------------
  8127. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8128. {
  8129. UnicodeString Result;
  8130. if (FTerminal != NULL)
  8131. {
  8132. switch (WinConfiguration->PathInCaption)
  8133. {
  8134. case picShort:
  8135. Result = TTerminalManager::Instance()->GetTerminalShortPath(FTerminal);
  8136. break;
  8137. case picFull:
  8138. Result = FTerminal->CurrentDirectory;
  8139. break;
  8140. }
  8141. }
  8142. return Result;
  8143. }
  8144. //---------------------------------------------------------------------------
  8145. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8146. {
  8147. TColor Result = GetWindowColor(FSessionColor);
  8148. return Result;
  8149. }
  8150. //---------------------------------------------------------------------------
  8151. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8152. {
  8153. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8154. return Result;
  8155. }
  8156. //---------------------------------------------------------------------------
  8157. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8158. {
  8159. TTerminalManager::Instance()->UpdateAppTitle();
  8160. // WORKAROUND: Disabling list view when it is not showing yet does not set its
  8161. // background to gray on Windows 7 (works on Windows 10).
  8162. // See also EnableControl
  8163. if (Showing)
  8164. {
  8165. if (Terminal != NULL)
  8166. {
  8167. // Update path when it changes
  8168. if ((SessionsPageControl->ActivePage != NULL) && (GetSessionTabTerminal(SessionsPageControl->ActivePage) == Terminal))
  8169. {
  8170. UpdateSessionTab(SessionsPageControl->ActivePage);
  8171. }
  8172. }
  8173. NonVisualDataModule->ReconnectSessionAction->Update();
  8174. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8175. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8176. CenterReconnectToolbar();
  8177. bool HasTerminal = HasActiveTerminal();
  8178. if (HasTerminal)
  8179. {
  8180. if (!RemoteDirView->Enabled)
  8181. {
  8182. RemoteDirView->Enabled = true;
  8183. if (FRemoteDirViewWasFocused)
  8184. {
  8185. ActiveControl = RemoteDirView;
  8186. }
  8187. }
  8188. RemoteDirView->Color = PanelColor();
  8189. }
  8190. else
  8191. {
  8192. if (RemoteDirView->Enabled)
  8193. {
  8194. // This is first called when the form is being constructed
  8195. // (not anymore due to Showing test above)
  8196. // but the false is overriden in the constructor later.
  8197. // An even later in TScpCommanderForm::DoShow()
  8198. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  8199. RemoteDirView->Enabled = false;
  8200. }
  8201. RemoteDirView->Color = DisabledPanelColor();
  8202. }
  8203. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8204. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8205. RemoteDriveView->Color = RemoteDirView->Color;
  8206. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8207. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8208. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8209. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8210. QueueFileList->Enabled = QueueView3->Enabled;
  8211. QueueFileList->Color = QueueView3->Color;
  8212. QueueFileList->Font->Color = QueueView3->Font->Color;
  8213. QueueLabelUpdateStatus();
  8214. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8215. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8216. if (FImmersiveDarkMode != WinConfiguration->UseDarkTheme())
  8217. {
  8218. UpdateDarkMode();
  8219. }
  8220. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  8221. }
  8222. }
  8223. //---------------------------------------------------------------------------
  8224. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8225. {
  8226. UpdateControls();
  8227. }
  8228. //---------------------------------------------------------------------------
  8229. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8230. TObject * Sender)
  8231. {
  8232. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8233. DebugAssert(DirView != NULL);
  8234. DoDirViewLoaded(DirView);
  8235. TransferPresetAutoSelect();
  8236. }
  8237. //---------------------------------------------------------------------------
  8238. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8239. {
  8240. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8241. // first run after installation
  8242. if (double(Updates.LastCheck) == 0)
  8243. {
  8244. // make sure next time there will be an update (if enabled)
  8245. Updates.LastCheck = TDateTime(1);
  8246. WinConfiguration->Updates = Updates;
  8247. }
  8248. else if ((double(Updates.Period) > 0) &&
  8249. (Now() - Updates.LastCheck >= Updates.Period))
  8250. {
  8251. TThreadMethod OnUpdatesChecked = NULL;
  8252. if (!IsUWP())
  8253. {
  8254. OnUpdatesChecked = UpdatesChecked;
  8255. }
  8256. StartUpdateThread(OnUpdatesChecked);
  8257. }
  8258. }
  8259. //---------------------------------------------------------------------------
  8260. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8261. {
  8262. UnicodeString Message;
  8263. bool New;
  8264. TQueryType Type;
  8265. GetUpdatesMessage(Message, New, Type, false);
  8266. if (!Message.IsEmpty())
  8267. {
  8268. if (New)
  8269. {
  8270. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8271. }
  8272. if (!New && (Type != qtWarning))
  8273. {
  8274. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8275. }
  8276. else
  8277. {
  8278. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8279. }
  8280. if (New)
  8281. {
  8282. Configuration->Usage->Inc(L"UpdateNotifications");
  8283. }
  8284. }
  8285. }
  8286. //---------------------------------------------------------------------------
  8287. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8288. {
  8289. RestoreApp();
  8290. bool CanDisplay = !NonVisualDataModule->Busy;
  8291. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8292. {
  8293. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8294. }
  8295. if (CanDisplay)
  8296. {
  8297. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8298. CheckForUpdates(true);
  8299. }
  8300. else
  8301. {
  8302. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8303. }
  8304. }
  8305. //---------------------------------------------------------------------------
  8306. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8307. TCopyParamRuleData & Data)
  8308. {
  8309. DebugAssert(Terminal != NULL);
  8310. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8311. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8312. Data.RemoteDirectory = RemoteDirView->PathName;
  8313. }
  8314. //---------------------------------------------------------------------------
  8315. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8316. {
  8317. // Terminal can be null when we are changing local directory implicitly,
  8318. // because it has been deleted, while no session is connected
  8319. // (Login dialog is open)
  8320. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8321. {
  8322. DebugAssert(!IsLocalBrowserMode());
  8323. TCopyParamRuleData Data;
  8324. GetTransferPresetAutoSelectData(Data);
  8325. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8326. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8327. if (CopyParamIndex < 0)
  8328. {
  8329. // there is no preset that matches autoselection
  8330. // set preset that we consider "default"
  8331. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8332. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8333. }
  8334. else
  8335. {
  8336. // there is preset matching autoselection
  8337. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8338. if (CopyParamName == FCopyParamAutoSelected)
  8339. {
  8340. // autoselected the same preset as the last time
  8341. // make no change (i.e. preserve custom user preset, if any)
  8342. }
  8343. else
  8344. {
  8345. // autoselected the different preset then the last time (or there
  8346. // was default preset set)
  8347. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8348. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8349. }
  8350. }
  8351. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8352. {
  8353. if (CopyParamIndex >= 0)
  8354. {
  8355. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8356. }
  8357. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8358. try
  8359. {
  8360. int Fmt =
  8361. (CopyParamIndex < 0) ?
  8362. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8363. COPY_PARAM_AUTOSELECTED;
  8364. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8365. Data->Message = MainInstructions(Message);
  8366. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8367. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8368. CopyParamAttrs);
  8369. if (CopyParamIndex >= 0)
  8370. {
  8371. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8372. Info = FORMAT(L"%s\n \n%s", (Info,
  8373. FMTLOAD(COPY_PARAM_RULE,
  8374. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8375. }
  8376. Data->Message += L"\n\n" + Info;
  8377. if (WinConfiguration->CopyParamAutoSelectNotice)
  8378. {
  8379. TransferPresetNoteMessage(Data, true);
  8380. }
  8381. else
  8382. {
  8383. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8384. Data = NULL; // ownership passed
  8385. }
  8386. }
  8387. __finally
  8388. {
  8389. delete Data;
  8390. }
  8391. }
  8392. }
  8393. }
  8394. //---------------------------------------------------------------------------
  8395. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8396. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8397. {
  8398. DebugAssert(NoteData != NULL);
  8399. DebugAssert(!IsLocalBrowserMode());
  8400. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8401. TQueryButtonAlias Aliases[1];
  8402. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8403. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8404. Params.Aliases = Aliases;
  8405. Params.AliasesCount = LENOF(Aliases);
  8406. unsigned int Result =
  8407. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8408. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8409. switch (Result)
  8410. {
  8411. case qaNeverAskAgain:
  8412. DebugAssert(AllowNeverAskAgain);
  8413. WinConfiguration->CopyParamAutoSelectNotice = false;
  8414. break;
  8415. case qaIgnore:
  8416. PreferencesDialog(pmPresets);
  8417. break;
  8418. }
  8419. }
  8420. //---------------------------------------------------------------------------
  8421. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8422. {
  8423. // as of now useless, as this is used for notes only, never for balloons, ...
  8424. RestoreApp();
  8425. // .. and we should never be busy here
  8426. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8427. {
  8428. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8429. }
  8430. }
  8431. //---------------------------------------------------------------------------
  8432. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8433. TPreferencesMode APreferencesMode)
  8434. {
  8435. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8436. TCopyParamRuleData Data;
  8437. if (Terminal != NULL)
  8438. {
  8439. PreferencesData.reset(new TPreferencesDialogData());
  8440. GetTransferPresetAutoSelectData(Data);
  8441. PreferencesData->CopyParamRuleData = &Data;
  8442. }
  8443. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8444. }
  8445. //---------------------------------------------------------------------------
  8446. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8447. const TCustomCommandType & Command)
  8448. {
  8449. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8450. {
  8451. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  8452. }
  8453. }
  8454. //---------------------------------------------------------------------------
  8455. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8456. {
  8457. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8458. TCustomCommandType Command;
  8459. // make sure we use local custom command when remote are not supported
  8460. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8461. {
  8462. Command = FLastCustomCommand;
  8463. }
  8464. else
  8465. {
  8466. Command.Params = Command.Params | ccLocal;
  8467. }
  8468. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8469. FEditingFocusedAdHocCommand = OnFocused;
  8470. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8471. int Options =
  8472. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8473. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8474. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8475. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8476. {
  8477. FLastCustomCommand = Command;
  8478. UpdateCustomCommandsToolbar();
  8479. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8480. }
  8481. }
  8482. //---------------------------------------------------------------------------
  8483. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8484. {
  8485. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8486. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8487. DebugAssert(State > 0);
  8488. if (State <= 0)
  8489. {
  8490. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  8491. }
  8492. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8493. }
  8494. //---------------------------------------------------------------------------
  8495. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8496. TCustomCommandType & Command, int & State)
  8497. {
  8498. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8499. if (Result)
  8500. {
  8501. Command = FLastCustomCommand;
  8502. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8503. }
  8504. return Result;
  8505. }
  8506. //---------------------------------------------------------------------------
  8507. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8508. {
  8509. ResetIncrementalSearch();
  8510. }
  8511. //---------------------------------------------------------------------------
  8512. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8513. {
  8514. DebugAssert(ComponentVisible[Component]);
  8515. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8516. }
  8517. //---------------------------------------------------------------------------
  8518. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8519. {
  8520. // when queue panel is resized here directly, the status bar is stretched
  8521. // over whole space the panel occupied
  8522. PostComponentHide(fcQueueView);
  8523. }
  8524. //---------------------------------------------------------------------------
  8525. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  8526. {
  8527. ComponentVisible[fcQueueFileList] = false;
  8528. }
  8529. //---------------------------------------------------------------------------
  8530. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  8531. {
  8532. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  8533. RefreshColorMode();
  8534. ResetSysDarkTheme();
  8535. ConfigurationChanged();
  8536. ConfigureInterface();
  8537. // Should be called for all controls
  8538. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  8539. }
  8540. //---------------------------------------------------------------------------
  8541. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  8542. {
  8543. // Do not handle, when shutting down anyway (maybe also when not setup completelly yet?)
  8544. if (!FInvalid &&
  8545. (Message.LParam != 0) &&
  8546. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  8547. {
  8548. ThemeChanged();
  8549. }
  8550. TForm::Dispatch(&Message);
  8551. }
  8552. //---------------------------------------------------------------------------
  8553. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8554. {
  8555. TMessage * M = static_cast<TMessage*>(Message);
  8556. switch (M->Msg)
  8557. {
  8558. case CM_DIALOGCHAR:
  8559. CMDialogChar(*M);
  8560. break;
  8561. case WM_APPCOMMAND:
  8562. WMAppCommand(*M);
  8563. break;
  8564. case WM_SYSCOMMAND:
  8565. WMSysCommand(*M);
  8566. break;
  8567. case WM_QUERYENDSESSION:
  8568. WMQueryEndSession(*M);
  8569. break;
  8570. case WM_ENDSESSION:
  8571. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8572. break;
  8573. #ifdef _DEBUG
  8574. case WM_WINDOWPOSCHANGED:
  8575. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8576. break;
  8577. case CM_CUSTOMSTYLECHANGED:
  8578. TForm::Dispatch(Message);
  8579. break;
  8580. #endif
  8581. case WM_COMPONENT_HIDE:
  8582. {
  8583. Byte Component = static_cast<Byte>(M->WParam);
  8584. // sanity check
  8585. if (ComponentVisible[Component])
  8586. {
  8587. // special treatment
  8588. if (Component == fcQueueView)
  8589. {
  8590. ToggleQueueVisibility();
  8591. DebugAssert(!ComponentVisible[fcQueueView]);
  8592. }
  8593. else
  8594. {
  8595. ComponentVisible[Component] = false;
  8596. }
  8597. }
  8598. }
  8599. break;
  8600. case WM_COPYDATA:
  8601. WMCopyData(*M);
  8602. break;
  8603. case WM_MANAGES_CAPTION:
  8604. // caption managed in TTerminalManager::UpdateAppTitle()
  8605. M->Result = 1;
  8606. break;
  8607. case WM_WANTS_MOUSEWHEEL:
  8608. M->Result = 1;
  8609. break;
  8610. case CM_SHOWINGCHANGED:
  8611. CMShowingChanged(*M);
  8612. break;
  8613. case WM_CLOSE:
  8614. WMClose(*M);
  8615. break;
  8616. case WM_DPICHANGED:
  8617. WMDpiChanged(*M);
  8618. break;
  8619. case CM_DPICHANGED:
  8620. CMDpiChanged(*M);
  8621. break;
  8622. case WM_SETTINGCHANGE:
  8623. WMSettingChange(*M);
  8624. break;
  8625. case CM_DIALOGKEY:
  8626. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8627. break;
  8628. default:
  8629. TForm::Dispatch(Message);
  8630. break;
  8631. }
  8632. }
  8633. //---------------------------------------------------------------------------
  8634. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8635. {
  8636. // noop
  8637. }
  8638. //---------------------------------------------------------------------------
  8639. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8640. {
  8641. TForm::Dispatch(&Message);
  8642. FSessionColors->Clear();
  8643. AddFixedSessionImages();
  8644. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8645. UpdateImages();
  8646. }
  8647. //---------------------------------------------------------------------------
  8648. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8649. {
  8650. TForm::Dispatch(&Message);
  8651. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8652. UpdatePixelsPerInchMainWindowCounter();
  8653. }
  8654. //---------------------------------------------------------------------------
  8655. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8656. {
  8657. // Cannot close window while we are busy.
  8658. // We cannot test this in FormCloseQuery as that is called also from
  8659. // Close(), which is called by CloseApplicationAction. So we can be busy
  8660. // there already even, when it is legitimate to close the application.
  8661. // Possibly a better place to handle this would be WMSysCommand.
  8662. if (NonVisualDataModule->Busy)
  8663. {
  8664. Message.Result = 1;
  8665. }
  8666. else
  8667. {
  8668. TForm::Dispatch(&Message);
  8669. }
  8670. }
  8671. //---------------------------------------------------------------------------
  8672. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8673. {
  8674. TForm::Dispatch(&Message);
  8675. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  8676. if (Showing && !FStarted)
  8677. {
  8678. FStarted = true;
  8679. InterfaceStarted();
  8680. }
  8681. if (Showing && (Terminal == NULL))
  8682. {
  8683. // When we are starting minimized (i.e. from an installer),
  8684. // postpone showing Login dialog until we get restored.
  8685. // Otherwise the Login dialog (and Authentication window) show restored
  8686. // over invisible (minimized) main window.
  8687. if (WindowState == wsMinimized)
  8688. {
  8689. FNeedSession = true;
  8690. }
  8691. else
  8692. {
  8693. // This happens before application ever goes idle, so the toolbars would
  8694. // stay enabled (initial state) until the Login dialog is dismissed.
  8695. UpdateActions();
  8696. NonVisualDataModule->StartBusy();
  8697. try
  8698. {
  8699. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8700. // otherwise the dialog does not receive focus.
  8701. // With Commander interface the ProcessMessages is called already
  8702. // by TDriveView, but with Explorer interface, we need to call it explicily
  8703. Application->ProcessMessages();
  8704. // do not reload sessions, they have been loaded just now (optimization)
  8705. NeedSession(true);
  8706. }
  8707. __finally
  8708. {
  8709. NonVisualDataModule->EndBusy();
  8710. }
  8711. }
  8712. }
  8713. }
  8714. //---------------------------------------------------------------------------
  8715. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  8716. {
  8717. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  8718. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  8719. }
  8720. //---------------------------------------------------------------------------
  8721. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8722. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8723. int & MaxHeight)
  8724. {
  8725. // workaround for bug in TWinControl.CalcConstraints
  8726. // Check for empty rect (restore from iconic state) is done there only after
  8727. // call to AdjustClientRect, which enlarges the rect (for forms).
  8728. TRect R = GetClientRect();
  8729. // when restoring from iconic state, place no restrictions
  8730. if (IsRectEmpty(R))
  8731. {
  8732. MinWidth = 0;
  8733. MinHeight = 0;
  8734. MaxWidth = 32000;
  8735. MaxHeight = 32000;
  8736. }
  8737. }
  8738. //---------------------------------------------------------------------------
  8739. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8740. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8741. {
  8742. // terminal can be already closed (e.g. dropped connection)
  8743. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8744. {
  8745. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8746. try
  8747. {
  8748. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8749. }
  8750. catch(...)
  8751. {
  8752. if (!Terminal->Active)
  8753. {
  8754. Close = true;
  8755. }
  8756. throw;
  8757. }
  8758. }
  8759. }
  8760. //---------------------------------------------------------------------------
  8761. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8762. {
  8763. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8764. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8765. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8766. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8767. {
  8768. OnGetSpaceAvailable = GetSpaceAvailable;
  8769. }
  8770. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8771. OnGetSpaceAvailable);
  8772. }
  8773. //---------------------------------------------------------------------------
  8774. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8775. {
  8776. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8777. Terminal->FillSessionDataForCode(Data.get());
  8778. return Data.release();
  8779. }
  8780. //---------------------------------------------------------------------------
  8781. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8782. {
  8783. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8784. DoGenerateUrlDialog(Data.get(), Paths);
  8785. }
  8786. //---------------------------------------------------------------------------
  8787. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8788. {
  8789. GenerateUrl(NULL);
  8790. }
  8791. //---------------------------------------------------------------------------
  8792. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8793. {
  8794. std::unique_ptr<TStrings> Paths(new TStringList());
  8795. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8796. GenerateUrl(Paths.get());
  8797. }
  8798. //---------------------------------------------------------------------------
  8799. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  8800. {
  8801. FSessionColor = value;
  8802. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8803. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8804. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8805. DebugAssert(ColorItem != NULL);
  8806. ColorItem->Color = C;
  8807. NonVisualDataModule->ColorMenuItem->Color = C;
  8808. UpdateControls();
  8809. }
  8810. //---------------------------------------------------------------------------
  8811. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8812. {
  8813. if (value != FSessionColor)
  8814. {
  8815. UpdateSessionColor(value);
  8816. // Is null when called from LastTerminalClosed
  8817. if (Terminal != NULL)
  8818. {
  8819. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8820. }
  8821. }
  8822. }
  8823. //---------------------------------------------------------------------------
  8824. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8825. {
  8826. bool Result = FNoteTimer->Enabled;
  8827. if (Result)
  8828. {
  8829. // cannot cancel note too early
  8830. bool NotEarly =
  8831. (Now() - FNoteShown >
  8832. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8833. if (Force || NotEarly)
  8834. {
  8835. FNoteTimer->Enabled = false;
  8836. FNote = L"";
  8837. SAFE_DESTROY(FNoteData);
  8838. FOnNoteClick = NULL;
  8839. FNoteHints = FNotes->Text;
  8840. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8841. UpdateStatusBar();
  8842. }
  8843. }
  8844. return Result;
  8845. }
  8846. //---------------------------------------------------------------------------
  8847. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8848. {
  8849. DebugAssert(FNoteTimer->Enabled);
  8850. CancelNote(true);
  8851. }
  8852. //---------------------------------------------------------------------------
  8853. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  8854. {
  8855. FNoteHints = FNotes->Text.TrimRight();
  8856. }
  8857. //---------------------------------------------------------------------------
  8858. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8859. {
  8860. int P = Note.Pos(L"\n");
  8861. if (P > 0)
  8862. {
  8863. Note.SetLength(P - 1);
  8864. }
  8865. FNotes->Add(FORMAT(L"[%s] %s",
  8866. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8867. while (FNotes->Count > 10)
  8868. {
  8869. FNotes->Delete(0);
  8870. }
  8871. if (UpdateNow)
  8872. {
  8873. UpdateNoteHints();
  8874. UpdateStatusBar();
  8875. }
  8876. }
  8877. //---------------------------------------------------------------------------
  8878. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8879. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8880. {
  8881. int P = Note.Pos(L"\n");
  8882. if (P > 0)
  8883. {
  8884. Note.SetLength(P - 1);
  8885. }
  8886. UpdateNoteHints();
  8887. FNote = Note;
  8888. SAFE_DESTROY(FNoteData);
  8889. FNoteData = NoteData;
  8890. FOnNoteClick = OnNoteClick;
  8891. AddNote(Note, false);
  8892. UpdateStatusBar();
  8893. FNoteShown = Now();
  8894. FNoteTimer->Enabled = false;
  8895. if (Seconds == 0)
  8896. {
  8897. Seconds = WinConfiguration->NotificationsTimeout;
  8898. }
  8899. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8900. FNoteTimer->Enabled = true;
  8901. }
  8902. //---------------------------------------------------------------------------
  8903. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8904. {
  8905. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8906. }
  8907. //---------------------------------------------------------------------------
  8908. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8909. {
  8910. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  8911. {
  8912. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8913. }
  8914. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  8915. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  8916. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  8917. 0, NULL, NULL);
  8918. }
  8919. //---------------------------------------------------------------------------
  8920. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  8921. {
  8922. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  8923. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  8924. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  8925. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  8926. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  8927. }
  8928. //---------------------------------------------------------------------------
  8929. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  8930. {
  8931. WinConfiguration->FormatSizeBytes = Style;
  8932. }
  8933. //---------------------------------------------------------------------------
  8934. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  8935. {
  8936. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  8937. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  8938. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  8939. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  8940. }
  8941. //---------------------------------------------------------------------------
  8942. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  8943. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  8944. {
  8945. if (Panel->Index == 0)
  8946. {
  8947. if (FOnNoteClick != NULL)
  8948. {
  8949. // prevent the user data from being freed by possible call
  8950. // to CancelNote or PostNote during call to OnNoteClick
  8951. std::unique_ptr<TObject> NoteData(FNoteData);
  8952. TNotifyEvent OnNoteClick = FOnNoteClick;
  8953. FNoteData = NULL;
  8954. // need to cancel the note as we are going to delete its user data
  8955. CancelNote(true);
  8956. OnNoteClick(NoteData.get());
  8957. }
  8958. }
  8959. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  8960. {
  8961. FileSystemInfo();
  8962. }
  8963. }
  8964. //---------------------------------------------------------------------------
  8965. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  8966. {
  8967. // workaround:
  8968. // 1) for unknown reason, disabling window, while minimized,
  8969. // prevents it from restoring, even if it was enabled again meanwhile
  8970. // 2) when disabling the main window, while another has focus
  8971. // minimize is no longer possible
  8972. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  8973. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  8974. // Shouldn't we use IsApplicationMinimized() here?
  8975. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  8976. {
  8977. Enabled = false;
  8978. }
  8979. FLockLevel++;
  8980. }
  8981. //---------------------------------------------------------------------------
  8982. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  8983. {
  8984. DebugAssert(FLockLevel > 0);
  8985. FLockLevel--;
  8986. if (FLockLevel == 0)
  8987. {
  8988. DebugAssert(FLockSuspendLevel == 0);
  8989. Enabled = true;
  8990. }
  8991. }
  8992. //---------------------------------------------------------------------------
  8993. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  8994. {
  8995. // We need to make sure that window is enabled when the last modal window closes
  8996. // otherwise focus is not restored correctly.
  8997. // So we re-enable the window when modal window opens and
  8998. // disable it back after is closes
  8999. if (FLockLevel > 0)
  9000. {
  9001. // while we have nesting counter, we know that we never be called
  9002. // recursivelly as Application->OnModalBegin is called only
  9003. // for the top-level modal window
  9004. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9005. {
  9006. // won't be disabled when conditions in LockWindow() were not satisfied
  9007. FDisabledOnLockSuspend = !Enabled;
  9008. // When minimized to tray (or actually when set to SW_HIDE),
  9009. // setting Enabled makes the window focusable even when there's
  9010. // modal window over it
  9011. if (!FTrayIcon->Visible)
  9012. {
  9013. Enabled = true;
  9014. }
  9015. }
  9016. FLockSuspendLevel++;
  9017. }
  9018. }
  9019. //---------------------------------------------------------------------------
  9020. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9021. {
  9022. if (FLockSuspendLevel > 0)
  9023. {
  9024. DebugAssert(FLockLevel > 0);
  9025. FLockSuspendLevel--;
  9026. // see comment in SuspendWindowLock
  9027. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9028. {
  9029. // Note that window can be enabled here, when we were minized to tray when
  9030. // was SuspendWindowLock() called.
  9031. // We should possibly do the same check as in LockWindow(),
  9032. // if it is ever possible that the consitions change between
  9033. // SuspendWindowLock() and ResumeWindowLock()
  9034. Enabled = !FDisabledOnLockSuspend;
  9035. }
  9036. }
  9037. }
  9038. //---------------------------------------------------------------------------
  9039. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9040. {
  9041. TTBXComboBoxItem * RemotePathComboBox =
  9042. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9043. if (!TextOnly)
  9044. {
  9045. TStrings * Items = RemotePathComboBox->Strings;
  9046. Items->BeginUpdate();
  9047. try
  9048. {
  9049. Items->Clear();
  9050. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9051. {
  9052. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9053. while (!IsUnixRootPath(APath))
  9054. {
  9055. int P = APath.LastDelimiter(L'/');
  9056. DebugAssert(P >= 0);
  9057. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9058. APath.SetLength(P - 1);
  9059. }
  9060. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9061. }
  9062. }
  9063. __finally
  9064. {
  9065. Items->EndUpdate();
  9066. }
  9067. }
  9068. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9069. // Setting ItemIndex to -1 does not reset its text
  9070. if (RemotePathComboBox->Strings->Count == 0)
  9071. {
  9072. RemotePathComboBox->Text = L"";
  9073. }
  9074. }
  9075. //---------------------------------------------------------------------------
  9076. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9077. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  9078. int & ImageIndex)
  9079. {
  9080. if (AIndex < 0)
  9081. {
  9082. AIndex = Sender->ItemIndex;
  9083. }
  9084. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9085. }
  9086. //---------------------------------------------------------------------------
  9087. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9088. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9089. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9090. {
  9091. ARect.Left += (10 * AIndex);
  9092. }
  9093. //---------------------------------------------------------------------------
  9094. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9095. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9096. {
  9097. AWidth += (10 * AIndex);
  9098. }
  9099. //---------------------------------------------------------------------------
  9100. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  9101. TObject * Sender)
  9102. {
  9103. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9104. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9105. int Index = RemotePathComboBox->ItemIndex;
  9106. while (Index < RemotePathComboBox->Strings->Count - 1)
  9107. {
  9108. APath = UnixExtractFileDir(APath);
  9109. Index++;
  9110. }
  9111. // VanDyke style paths
  9112. if (APath.IsEmpty())
  9113. {
  9114. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9115. APath = ROOTDIRECTORY;
  9116. }
  9117. if (RemoteDirView->Path != APath)
  9118. {
  9119. RemoteDirView->Path = APath;
  9120. }
  9121. }
  9122. //---------------------------------------------------------------------------
  9123. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9124. {
  9125. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9126. DebugUsedParam(Sender);
  9127. UpdateRemotePathComboBox(true);
  9128. }
  9129. //---------------------------------------------------------------------------
  9130. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9131. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9132. {
  9133. ResetIncrementalSearch();
  9134. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9135. DebugAssert(DirView != NULL);
  9136. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9137. {
  9138. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9139. // OnEditing is called also from TCustomListView::CanEdit
  9140. if (Edit != NULL)
  9141. {
  9142. EditSelectBaseName(Edit);
  9143. }
  9144. }
  9145. }
  9146. //---------------------------------------------------------------------------
  9147. //---------------------------------------------------------------------------
  9148. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9149. {
  9150. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9151. Result->AutoDetectDnD = false;
  9152. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9153. Result->RenderDataOn = rdoEnterAndDropSync;
  9154. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9155. return Result;
  9156. }
  9157. //---------------------------------------------------------------------------
  9158. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9159. {
  9160. if (IsWin10Build(2004))
  9161. {
  9162. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9163. BOOL DarkMode = FImmersiveDarkMode ? TRUE : FALSE;
  9164. const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  9165. DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
  9166. }
  9167. }
  9168. //---------------------------------------------------------------------------
  9169. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9170. {
  9171. TForm::CreateWnd();
  9172. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9173. UpdateDarkMode();
  9174. if (FSessionsDragDropFilesEx == NULL)
  9175. {
  9176. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9177. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9178. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9179. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9180. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9181. }
  9182. if (FQueueDragDropFilesEx == NULL)
  9183. {
  9184. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9185. // No need to set OnDragOver as we do not have any restrictions
  9186. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9187. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9188. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9189. }
  9190. }
  9191. //---------------------------------------------------------------------------
  9192. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9193. {
  9194. TForm::DestroyWnd();
  9195. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9196. FQueueDragDropFilesEx->DragDropControl = NULL;
  9197. }
  9198. //---------------------------------------------------------------------------
  9199. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9200. {
  9201. SideEnter(FCurrentSide);
  9202. FEverShown = true;
  9203. }
  9204. //---------------------------------------------------------------------------
  9205. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9206. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9207. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9208. {
  9209. if (!NonVisualDataModule->Busy)
  9210. {
  9211. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9212. Configuration->Usage->Inc(L"FileFinds");
  9213. LockWindow(true);
  9214. NonVisualDataModule->StartBusy();
  9215. WinConfiguration->LockedInterface = true;
  9216. try
  9217. {
  9218. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9219. }
  9220. __finally
  9221. {
  9222. WinConfiguration->LockedInterface = false;
  9223. NonVisualDataModule->EndBusy();
  9224. UnlockWindow();
  9225. }
  9226. }
  9227. }
  9228. //---------------------------------------------------------------------------
  9229. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9230. {
  9231. if (!NonVisualDataModule->Busy)
  9232. {
  9233. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9234. SetFocus();
  9235. RemoteDirView->Path = UnixExtractFilePath(Path);
  9236. UnicodeString FileName = UnixExtractFileName(Path);
  9237. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9238. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9239. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9240. if (Item == NULL)
  9241. {
  9242. ReloadDirectory(osRemote);
  9243. Item = RemoteDirView->FindFileItem(FileName);
  9244. }
  9245. if (Item != NULL)
  9246. {
  9247. RemoteDirView->ItemFocused = Item;
  9248. RemoteDirView->ItemFocused->MakeVisible(false);
  9249. RemoteDirView->SetFocus();
  9250. }
  9251. }
  9252. }
  9253. //---------------------------------------------------------------------------
  9254. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9255. {
  9256. bool Result = !NonVisualDataModule->Busy;
  9257. if (Result)
  9258. {
  9259. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9260. }
  9261. return Result;
  9262. }
  9263. //---------------------------------------------------------------------------
  9264. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9265. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9266. {
  9267. if (CanOperateOnFoundFiles(ATerminal))
  9268. {
  9269. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9270. FOnFileOperationFinished = OnFileOperationFinished;
  9271. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9272. }
  9273. }
  9274. //---------------------------------------------------------------------------
  9275. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9276. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9277. {
  9278. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9279. }
  9280. //---------------------------------------------------------------------------
  9281. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9282. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9283. {
  9284. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9285. }
  9286. //---------------------------------------------------------------------------
  9287. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9288. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9289. {
  9290. if (CanOperateOnFoundFiles(ATerminal))
  9291. {
  9292. for (int Index = 0; Index < FileList->Count; Index++)
  9293. {
  9294. UnicodeString FileName = FileList->Strings[Index];
  9295. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9296. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9297. OnFileOperationFinished(FileName, true);
  9298. }
  9299. }
  9300. }
  9301. //---------------------------------------------------------------------------
  9302. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9303. {
  9304. FFileFindTerminal = Terminal;
  9305. ShowFileFindDialog(
  9306. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9307. DoDownloadFoundFiles, DoEditFoundFiles);
  9308. }
  9309. //---------------------------------------------------------------------------
  9310. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9311. {
  9312. FTaskbarList = TaskbarList;
  9313. }
  9314. //---------------------------------------------------------------------------
  9315. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9316. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9317. {
  9318. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9319. if (Index >= 0)
  9320. {
  9321. if (Button == mbLeft)
  9322. {
  9323. // "Mouse down" is raised only after tab is switched.
  9324. // If switching tab (switching session) takes long enough for user
  9325. // to actually release the button, "mouse down" is still raised,
  9326. // but we do not get "mouse up" event, so dragging is not cancelled,
  9327. // prevent that by not beginning dragging in the first place.
  9328. if (IsKeyPressed(VK_LBUTTON))
  9329. {
  9330. // when user clicks the "+", we get mouse down only after the session
  9331. // is closed, when new session tab is already on X:Y, so dragging
  9332. // starts, prevent that
  9333. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  9334. {
  9335. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9336. if (Terminal != NULL)
  9337. {
  9338. SessionsPageControl->BeginDrag(false);
  9339. }
  9340. }
  9341. }
  9342. }
  9343. else if (Button == mbMiddle)
  9344. {
  9345. // ignore middle-click for "New session tab"
  9346. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9347. if (Terminal != NULL)
  9348. {
  9349. CloseSessionTab(Index);
  9350. }
  9351. }
  9352. }
  9353. }
  9354. //---------------------------------------------------------------------------
  9355. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9356. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9357. {
  9358. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9359. // do not allow dropping on the "+" tab
  9360. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9361. if ((TargetTerminal != NULL) &&
  9362. (SessionsPageControl->ActivePage->PageIndex != Index))
  9363. {
  9364. Configuration->Usage->Inc(L"SessionTabMoves");
  9365. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  9366. // but we want to actually prevent that to avoid flicker
  9367. SessionsPageControl->ActivePage->PageIndex = Index;
  9368. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  9369. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9370. }
  9371. }
  9372. //---------------------------------------------------------------------------
  9373. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9374. TObject * Sender, TObject * Source, int X, int Y,
  9375. TDragState /*State*/, bool & Accept)
  9376. {
  9377. Accept = (Sender == Source);
  9378. if (Accept)
  9379. {
  9380. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9381. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9382. // do not allow dragging to the "+" tab
  9383. Accept = (Terminal != NULL);
  9384. }
  9385. }
  9386. //---------------------------------------------------------------------------
  9387. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  9388. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  9389. {
  9390. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9391. if (Index < 0)
  9392. {
  9393. Effect = DROPEFFECT_NONE;
  9394. }
  9395. else
  9396. {
  9397. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9398. // do not allow dropping on the "+" and disconnected tabs
  9399. if (!IsActiveTerminal(TargetTerminal))
  9400. {
  9401. Effect = DROPEFFECT_NONE;
  9402. }
  9403. else if ((TargetTerminal != Terminal) && (Effect == DROPEFFECT_MOVE))
  9404. {
  9405. Effect = DROPEFFECT_COPY;
  9406. }
  9407. }
  9408. }
  9409. //---------------------------------------------------------------------------
  9410. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9411. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9412. {
  9413. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9414. // do not allow dropping on the "+" and disconnected tabs
  9415. TManagedTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9416. if (IsActiveTerminal(TargetTerminal))
  9417. {
  9418. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9419. if (!IsFileControl(DropSourceControl, osRemote))
  9420. {
  9421. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  9422. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9423. // Why don't we use Terminal->CurrentDirectory directly?
  9424. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9425. // do not force queue, drag drop
  9426. false, true);
  9427. }
  9428. }
  9429. }
  9430. //---------------------------------------------------------------------------
  9431. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9432. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9433. {
  9434. // Downloads are handled in RemoteFileControlDDEnd
  9435. if (!IsFileControl(DropSourceControl, osRemote))
  9436. {
  9437. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9438. Terminal->CurrentDirectory,
  9439. // force queue, drag drop
  9440. true, true);
  9441. }
  9442. }
  9443. //---------------------------------------------------------------------------
  9444. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9445. {
  9446. FShowing = false;
  9447. // Do not save empty workspace
  9448. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  9449. {
  9450. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9451. UnicodeString Name = WorkspaceName();
  9452. DoSaveWorkspace(
  9453. Name, DataList.get(),
  9454. !Configuration->DisablePasswordStoring &&
  9455. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9456. WinConfiguration->LastStoredSession = Name;
  9457. }
  9458. }
  9459. //---------------------------------------------------------------------------
  9460. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9461. {
  9462. TManagedTerminal * ManagedTerminal =
  9463. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9464. if (ManagedTerminal != NULL)
  9465. {
  9466. ManagedTerminal->DirectoryLoaded = Now();
  9467. }
  9468. }
  9469. //---------------------------------------------------------------------------
  9470. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9471. TListItem * /*Item*/, bool /*Selected*/)
  9472. {
  9473. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9474. switch (DirView->LastSelectMethod)
  9475. {
  9476. case smKeyboard:
  9477. Configuration->Usage->Inc(L"KeyboardSelections");
  9478. break;
  9479. case smMouse:
  9480. Configuration->Usage->Inc(L"MouseSelections");
  9481. break;
  9482. }
  9483. }
  9484. //---------------------------------------------------------------------------
  9485. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9486. {
  9487. if (Color != 0)
  9488. {
  9489. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9490. }
  9491. else
  9492. {
  9493. return FSessionTabImageIndex;
  9494. }
  9495. }
  9496. //---------------------------------------------------------------------------
  9497. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9498. {
  9499. TPngImageCollectionItem * Item =
  9500. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9501. FSessionColors->AddPng(Item->PngImage);
  9502. return FSessionColors->Count - 1;
  9503. }
  9504. //---------------------------------------------------------------------------
  9505. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9506. {
  9507. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9508. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  9509. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9510. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9511. }
  9512. //---------------------------------------------------------------------------
  9513. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9514. {
  9515. for (int Index = 0; Index < Control->ControlCount; Index++)
  9516. {
  9517. TControl * AControl = Control->Controls[Index];
  9518. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9519. if (WinControl != NULL)
  9520. {
  9521. CollectItemsWithTextDisplayMode(WinControl);
  9522. }
  9523. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9524. if (Toolbar != NULL)
  9525. {
  9526. // we care for top-level items only
  9527. TTBCustomItem * Items = Toolbar->Items;
  9528. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9529. {
  9530. TTBCustomItem * Item = Items->Items[ItemIndex];
  9531. if (((Item->DisplayMode == nbdmImageAndText) ||
  9532. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9533. EligibleForImageDisplayMode(Item))
  9534. {
  9535. FItemsWithTextDisplayMode.insert(Item);
  9536. }
  9537. }
  9538. }
  9539. }
  9540. }
  9541. //---------------------------------------------------------------------------
  9542. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9543. {
  9544. return true;
  9545. }
  9546. //---------------------------------------------------------------------------
  9547. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9548. {
  9549. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9550. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9551. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9552. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9553. bool Result = true;
  9554. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9555. {
  9556. TTBCustomItem * Item = *i;
  9557. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9558. if (Label != NULL)
  9559. {
  9560. // optimization
  9561. if (Label->Visible == SelectiveToolbarText)
  9562. {
  9563. Result = false;
  9564. }
  9565. else
  9566. {
  9567. Label->Visible = SelectiveToolbarText;
  9568. }
  9569. }
  9570. else
  9571. {
  9572. // optimization
  9573. if (Item->DisplayMode == DisplayMode)
  9574. {
  9575. Result = false;
  9576. }
  9577. else
  9578. {
  9579. Item->DisplayMode = DisplayMode;
  9580. }
  9581. }
  9582. ++i;
  9583. }
  9584. if (Result)
  9585. {
  9586. UpdateNewSessionTab();
  9587. }
  9588. return Result;
  9589. }
  9590. //---------------------------------------------------------------------------
  9591. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9592. {
  9593. DebugFail();
  9594. }
  9595. //---------------------------------------------------------------------------
  9596. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9597. {
  9598. // Among other, a lock level is non zero, while directory is being loaded,
  9599. // even when it happens as a result of dirview navigation,
  9600. // i.e. when TNonVisualDataModule is NOT FBusy.
  9601. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9602. // Among other this prevents a panel auto update to occur while
  9603. // directory is changing.
  9604. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9605. }
  9606. //---------------------------------------------------------------------------
  9607. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  9608. {
  9609. // While the window is disabled, we still seem to process menu shortcuts at least,
  9610. // so stop it at least here.
  9611. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9612. return
  9613. (Allowed == aaUpdate) ||
  9614. !NonVisualDataModule->Busy;
  9615. }
  9616. //---------------------------------------------------------------------------
  9617. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9618. {
  9619. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9620. }
  9621. //---------------------------------------------------------------------------
  9622. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9623. {
  9624. // This is somewhat redundant to LockWindow() call from
  9625. // TTerminalManager::TerminalReadDirectoryProgress.
  9626. // But disabling window is known not to block keyboard shorcuts
  9627. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9628. if (Busy > 0)
  9629. {
  9630. if (NonVisualDataModule->Busy)
  9631. {
  9632. State = false;
  9633. }
  9634. else
  9635. {
  9636. NonVisualDataModule->StartBusy();
  9637. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9638. LockWindow();
  9639. }
  9640. }
  9641. else if (Busy < 0)
  9642. {
  9643. UnlockWindow();
  9644. NonVisualDataModule->EndBusy();
  9645. }
  9646. else
  9647. {
  9648. State = NonVisualDataModule->Busy;
  9649. }
  9650. }
  9651. //---------------------------------------------------------------------------
  9652. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9653. {
  9654. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9655. // no context menu for "New session tab"
  9656. if ((Index >= 0) && (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != NULL))
  9657. {
  9658. SessionsPageControl->ActivePageIndex = Index;
  9659. if (DebugAlwaysTrue(SessionTabSwitched()))
  9660. {
  9661. // copied from TControl.WMContextMenu
  9662. SendCancelMode(SessionsPageControl);
  9663. // explicit popup instead of using PopupMenu property
  9664. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9665. // while connecting yet not-connected session and hence
  9666. // allowing an access to commands over not-completelly connected session
  9667. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9668. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  9669. PopupMenu->PopupComponent = SessionsPageControl;
  9670. PopupMenu->Popup(Point.x, Point.y);
  9671. }
  9672. }
  9673. Handled = true;
  9674. }
  9675. //---------------------------------------------------------------------------
  9676. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9677. {
  9678. return HasActiveTerminal() && Terminal->IsCapable[fcChangePassword];
  9679. }
  9680. //---------------------------------------------------------------------------
  9681. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9682. {
  9683. Configuration->Usage->Inc(L"PasswordChanges");
  9684. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9685. ChangePasswordData->DisableAuthentationsExceptPassword();
  9686. ChangePasswordData->ChangePassword = true;
  9687. std::unique_ptr<TTerminal> ChangePasswordSession(
  9688. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9689. ChangePasswordSession->AutoReadDirectory = false;
  9690. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9691. {
  9692. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9693. }
  9694. else
  9695. {
  9696. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9697. }
  9698. }
  9699. //---------------------------------------------------------------------------
  9700. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9701. {
  9702. // No nice way to assert SSH2
  9703. return HasActiveTerminal() && (Terminal->FSProtocol == cfsSFTP);
  9704. }
  9705. //---------------------------------------------------------------------------
  9706. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9707. {
  9708. TOperationVisualizer OperationVisualizer;
  9709. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9710. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9711. }
  9712. //---------------------------------------------------------------------------
  9713. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9714. {
  9715. TForm::ChangeScale(M, D);
  9716. int APixelsPerInch = GetControlPixelsPerInch(this);
  9717. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9718. try
  9719. {
  9720. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9721. }
  9722. __finally
  9723. {
  9724. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9725. }
  9726. }
  9727. //---------------------------------------------------------------------------
  9728. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9729. {
  9730. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9731. }
  9732. //---------------------------------------------------------------------
  9733. class TPublicControl : public TControl
  9734. {
  9735. friend class TCustomScpExplorerForm;
  9736. };
  9737. //---------------------------------------------------------------------------
  9738. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9739. {
  9740. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9741. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9742. }
  9743. //---------------------------------------------------------------------------
  9744. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9745. {
  9746. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9747. OpenBookmark(Side, Bookmark);
  9748. }
  9749. //---------------------------------------------------------------------------
  9750. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9751. {
  9752. DoBookmarkClick(osLocal, Sender);
  9753. }
  9754. //---------------------------------------------------------------------------
  9755. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9756. {
  9757. DoBookmarkClick(osRemote, Sender);
  9758. }
  9759. //---------------------------------------------------------------------------
  9760. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9761. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9762. {
  9763. if (BookmarkList != NULL)
  9764. {
  9765. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9766. if (!WinConfiguration->UseLocationProfiles)
  9767. {
  9768. std::unique_ptr<TStringList> Directories(new TStringList());
  9769. Directories->CaseSensitive = (Side == osRemote);
  9770. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9771. {
  9772. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9773. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9774. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9775. {
  9776. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9777. Item->Caption = EscapeHotkey(Directory);
  9778. Item->ShortCut = Bookmark->ShortCut;
  9779. Item->OnClick = OnBookmarkClick;
  9780. Item->Tag = reinterpret_cast<int>(Bookmark);
  9781. Directories->Add(Directory);
  9782. Menu->Add(Item.release());
  9783. }
  9784. }
  9785. }
  9786. else
  9787. {
  9788. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9789. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9790. {
  9791. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9792. if (!Bookmark->Node.IsEmpty())
  9793. {
  9794. Folders->Add(Bookmark->Node);
  9795. }
  9796. }
  9797. for (int Index = 0; Index < Folders->Count; Index++)
  9798. {
  9799. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9800. Item->Caption = Folders->Strings[Index];
  9801. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction->ImageIndex;
  9802. Folders->Objects[Index] = Item.get();
  9803. Menu->Add(Item.release());
  9804. }
  9805. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9806. {
  9807. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9808. TTBCustomItem * Parent;
  9809. if (!Bookmark->Node.IsEmpty())
  9810. {
  9811. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9812. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9813. }
  9814. else
  9815. {
  9816. Parent = Menu;
  9817. }
  9818. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9819. Item->Caption = Bookmark->Name;
  9820. Item->ShortCut = Bookmark->ShortCut;
  9821. Item->OnClick = OnBookmarkClick;
  9822. Item->Tag = reinterpret_cast<int>(Bookmark);
  9823. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9824. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9825. {
  9826. // No hint for location profiles that are actually mere "bookmarks"
  9827. }
  9828. else
  9829. {
  9830. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  9831. UnicodeString LongHint;
  9832. if (!Bookmark->Local.IsEmpty())
  9833. {
  9834. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  9835. }
  9836. if (!Bookmark->Remote.IsEmpty())
  9837. {
  9838. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  9839. }
  9840. AddToList(Hint, LongHint, L"|");
  9841. Item->Hint = Hint;
  9842. }
  9843. Parent->Add(Item.release());
  9844. }
  9845. }
  9846. AddMenuSeparator(Menu);
  9847. }
  9848. }
  9849. //---------------------------------------------------------------------------
  9850. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  9851. {
  9852. Menu->Clear();
  9853. std::unique_ptr<TTBCustomItem> Item;
  9854. Item.reset(new TTBXItem(Owner));
  9855. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  9856. Item->Options = Item->Options << tboDefault;
  9857. Menu->Add(Item.release());
  9858. AddMenuSeparator(Menu);
  9859. if (Terminal != NULL)
  9860. {
  9861. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  9862. }
  9863. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  9864. Item.reset(new TTBXItem(Owner));
  9865. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction : NonVisualDataModule->RemoteAddBookmarkAction;
  9866. Menu->Add(Item.release());
  9867. }
  9868. //---------------------------------------------------------------------------
  9869. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  9870. {
  9871. bool Result = true;
  9872. try
  9873. {
  9874. if (Side == osRemote)
  9875. {
  9876. Terminal->ExceptionOnFail = true;
  9877. }
  9878. try
  9879. {
  9880. DirView(Side)->Path = Path;
  9881. }
  9882. __finally
  9883. {
  9884. if (Side == osRemote)
  9885. {
  9886. Terminal->ExceptionOnFail = false;
  9887. }
  9888. }
  9889. }
  9890. catch (Exception & E)
  9891. {
  9892. if ((Side != osRemote) || Terminal->Active)
  9893. {
  9894. ShowExtendedExceptionEx(Terminal, &E);
  9895. Result = false;
  9896. }
  9897. else
  9898. {
  9899. throw;
  9900. }
  9901. }
  9902. return Result;
  9903. }
  9904. //---------------------------------------------------------------------------
  9905. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  9906. {
  9907. // Make sure there some feedback even when loading tiny local folders
  9908. TInstantOperationVisualizer Visualizer;
  9909. DirView(Side)->ReloadDirectory();
  9910. }
  9911. //---------------------------------------------------------------------------
  9912. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  9913. {
  9914. if (Index == SessionsPageControl->TabIndex)
  9915. {
  9916. CloseSession();
  9917. }
  9918. else
  9919. {
  9920. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  9921. TTerminal * Terminal = GetSessionTabTerminal(TabSheet);
  9922. TTerminalManager * Manager = TTerminalManager::Instance();
  9923. if (!Terminal->Active)
  9924. {
  9925. Manager->FreeTerminal(Terminal);
  9926. }
  9927. else
  9928. {
  9929. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  9930. if (CanCloseQueue(Queue))
  9931. {
  9932. Manager->FreeTerminal(Terminal);
  9933. }
  9934. }
  9935. }
  9936. }
  9937. //---------------------------------------------------------------------------
  9938. void __fastcall TCustomScpExplorerForm::SessionsPageControlCloseButtonClick(TPageControl * /*Sender*/, int Index)
  9939. {
  9940. CloseSessionTab(Index);
  9941. }
  9942. //---------------------------------------------------------------------------
  9943. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  9944. {
  9945. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  9946. {
  9947. TInstantOperationVisualizer Visualizer;
  9948. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  9949. // before we create a new data object
  9950. ClipboardClear();
  9951. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  9952. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  9953. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  9954. // Intermediate variable to WORKAROUND a crash in compiler
  9955. TObjectList * ClipboardFakeMonitors =
  9956. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  9957. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  9958. if (FClipboardDragDropFilesEx.get() == NULL)
  9959. {
  9960. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  9961. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  9962. }
  9963. FClipboardDragDropFilesEx->FileList->Clear();
  9964. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  9965. FClipboardDragDropFilesEx->CopyToClipboard();
  9966. FClipboardTerminal = FTerminal;
  9967. // Need full paths, as cwd can be different once files are pasted
  9968. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  9969. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  9970. {
  9971. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  9972. }
  9973. }
  9974. }
  9975. //---------------------------------------------------------------------------
  9976. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  9977. {
  9978. if (OpenClipboard(0))
  9979. {
  9980. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  9981. CloseClipboard();
  9982. }
  9983. }
  9984. //---------------------------------------------------------------------------
  9985. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  9986. {
  9987. return !FClipboardFakeDirectory.IsEmpty();
  9988. }
  9989. //---------------------------------------------------------------------------
  9990. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  9991. {
  9992. RemoveDir(ApiPath(FClipboardFakeDirectory));
  9993. FClipboardFakeDirectory = UnicodeString();
  9994. FClipboardTerminal = NULL;
  9995. if (FDownloadingFromClipboard)
  9996. {
  9997. // We are called by the monitor, so attempt to release it would deadlonk
  9998. FClipboardFakeMonitorsPendingReset = true;
  9999. }
  10000. else
  10001. {
  10002. FClipboardFakeMonitors.reset(NULL);
  10003. }
  10004. }
  10005. //---------------------------------------------------------------------------
  10006. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10007. {
  10008. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10009. {
  10010. ClipboardStop();
  10011. }
  10012. }
  10013. //---------------------------------------------------------------------------
  10014. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10015. {
  10016. TTerminalManager * Manager = TTerminalManager::Instance();
  10017. Manager->ActiveTerminal = FClipboardTerminal;
  10018. TTransferOperationParam Params;
  10019. Params.TargetDirectory = TargetDirectory;
  10020. Params.DragDrop = DragDrop;
  10021. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10022. }
  10023. //---------------------------------------------------------------------------
  10024. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10025. {
  10026. // It can actually rarelly happen that some random file is created, while we are shuttting down the monitor
  10027. // (as it pumps a Windows message queue while being shutted down)
  10028. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10029. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10030. {
  10031. // Can fail as it can be e.g. locked by AV, so we retry that later
  10032. bool Removed = RemoveDir(ApiPath(FileName));
  10033. try
  10034. {
  10035. if (!NonVisualDataModule->Busy)
  10036. {
  10037. DebugAssert(!FClipboardFakeMonitorsPendingReset);
  10038. FClipboardFakeMonitorsPendingReset = false;
  10039. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10040. TAutoFlag Flag(FDownloadingFromClipboard);
  10041. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  10042. }
  10043. }
  10044. __finally
  10045. {
  10046. if (!Removed)
  10047. {
  10048. RemoveDir(ApiPath(FileName));
  10049. }
  10050. }
  10051. }
  10052. }
  10053. //---------------------------------------------------------------------------
  10054. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10055. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10056. {
  10057. TFileMasks::TParams MaskParams;
  10058. MaskParams.Size = Size;
  10059. MaskParams.Modification = Modification;
  10060. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10061. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10062. {
  10063. bool ImplicitMatch;
  10064. if (Iter->FileMask.Matches(FileName, Directory, DirView->PathName, &MaskParams, false, ImplicitMatch) &&
  10065. !ImplicitMatch)
  10066. {
  10067. Color = Iter->Color;
  10068. break;
  10069. }
  10070. }
  10071. }
  10072. //---------------------------------------------------------------------------
  10073. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10074. {
  10075. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10076. if (!DirView->IsEditing())
  10077. {
  10078. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10079. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10080. // while searching
  10081. if (Key == VK_BACK)
  10082. {
  10083. if (!FIncrementalSearch.IsEmpty())
  10084. {
  10085. if (FIncrementalSearch.Length() == 1)
  10086. {
  10087. ResetIncrementalSearch();
  10088. }
  10089. else
  10090. {
  10091. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  10092. IncrementalSearch(NewText, false, false);
  10093. }
  10094. Key = 0;
  10095. }
  10096. }
  10097. }
  10098. }
  10099. //---------------------------------------------------------------------------
  10100. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  10101. {
  10102. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10103. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  10104. {
  10105. // Filter control sequences.
  10106. // When not searching yet, prefer use of the space for toggling file selection
  10107. // (so we cannot incrementally search for a string starting with a space).
  10108. if ((Key > VK_SPACE) ||
  10109. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  10110. {
  10111. IncrementalSearch(FIncrementalSearch + Key, false, false);
  10112. Key = 0;
  10113. }
  10114. }
  10115. }
  10116. //---------------------------------------------------------------------------
  10117. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  10118. {
  10119. if (!FIncrementalSearch.IsEmpty())
  10120. {
  10121. FIncrementalSearch = L"";
  10122. DirView(osCurrent)->UpdateStatusBar();
  10123. }
  10124. }
  10125. //---------------------------------------------------------------------------
  10126. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10127. {
  10128. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  10129. if (Item == NULL)
  10130. {
  10131. MessageBeep(MB_ICONHAND);
  10132. }
  10133. else
  10134. {
  10135. TCustomDirView * ADirView = DirView(osCurrent);
  10136. {
  10137. TAutoNestingCounter Guard(FIncrementalSearching);
  10138. ADirView->FocusItem(Item);
  10139. if (ADirView->NortonLike == nlOff)
  10140. {
  10141. if ((ADirView->Selected != Item) ||
  10142. (ADirView->SelCount > 1))
  10143. {
  10144. ADirView->ClearSelection();
  10145. ADirView->Selected = Item;
  10146. }
  10147. }
  10148. }
  10149. FIncrementalSearch = Text;
  10150. Item->MakeVisible(false);
  10151. TListItem * NextItem = SearchFile(Text, true, Reverse);
  10152. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  10153. ADirView->UpdateStatusBar();
  10154. }
  10155. }
  10156. //---------------------------------------------------------------------------
  10157. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  10158. {
  10159. int Index = Item->Index;
  10160. if (!Reverse)
  10161. {
  10162. Index++;
  10163. if (Index >= Item->Owner->Count)
  10164. {
  10165. Index = 0;
  10166. }
  10167. }
  10168. else
  10169. {
  10170. Index--;
  10171. if (Index < 0)
  10172. {
  10173. Index = Item->Owner->Count - 1;
  10174. }
  10175. }
  10176. return Item->Owner->Item[Index];
  10177. }
  10178. //---------------------------------------------------------------------------
  10179. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10180. {
  10181. TCustomDirView * ADirView = DirView(osCurrent);
  10182. if (ADirView->Items->Count == 0)
  10183. {
  10184. return NULL;
  10185. }
  10186. else
  10187. {
  10188. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  10189. TListItem * Item = CurrentItem;
  10190. if (SkipCurrent)
  10191. {
  10192. Item = DebugNotNull(GetNextFile(Item, Reverse));
  10193. }
  10194. while (true)
  10195. {
  10196. bool Matches = false;
  10197. switch (WinConfiguration->PanelSearch)
  10198. {
  10199. case isNameStartOnly:
  10200. Matches = ContainsTextSemiCaseSensitive(Item->Caption.SubString(1, Text.Length()), Text);
  10201. break;
  10202. case isName:
  10203. Matches = ContainsTextSemiCaseSensitive(Item->Caption, Text);
  10204. break;
  10205. case isAll:
  10206. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  10207. int Index = 0;
  10208. while ((Index < ColCount) && !Matches)
  10209. {
  10210. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  10211. if (Visible)
  10212. {
  10213. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  10214. }
  10215. Index++;
  10216. }
  10217. break;
  10218. }
  10219. if (Matches)
  10220. {
  10221. return Item;
  10222. }
  10223. Item = GetNextFile(Item, Reverse);
  10224. if (Item == CurrentItem)
  10225. {
  10226. return NULL;
  10227. }
  10228. }
  10229. }
  10230. }
  10231. //---------------------------------------------------------------------------
  10232. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10233. {
  10234. ResetIncrementalSearch();
  10235. }
  10236. //---------------------------------------------------------------------------
  10237. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10238. {
  10239. if (Message.CharCode == VK_TAB)
  10240. {
  10241. if (!FIncrementalSearch.IsEmpty())
  10242. {
  10243. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10244. bool Reverse = Shift.Contains(ssShift);
  10245. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10246. Message.Result = 1;
  10247. return;
  10248. }
  10249. }
  10250. else if (Message.CharCode == VK_ESCAPE)
  10251. {
  10252. if (!FIncrementalSearch.IsEmpty())
  10253. {
  10254. ResetIncrementalSearch();
  10255. Message.Result = 1;
  10256. return;
  10257. }
  10258. }
  10259. TForm::Dispatch(&Message);
  10260. }
  10261. //---------------------------------------------------------------------------
  10262. void __fastcall TCustomScpExplorerForm::Deactivate()
  10263. {
  10264. TForm::Deactivate();
  10265. // E.g. when switching to an internal editor window
  10266. ResetIncrementalSearch();
  10267. }
  10268. //---------------------------------------------------------------------------
  10269. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10270. {
  10271. // When switching to another application
  10272. ResetIncrementalSearch();
  10273. }
  10274. //---------------------------------------------------------------------------
  10275. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10276. {
  10277. ResetIncrementalSearch();
  10278. }
  10279. //---------------------------------------------------------------------------
  10280. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10281. {
  10282. if (FIncrementalSearching <= 0)
  10283. {
  10284. ResetIncrementalSearch();
  10285. }
  10286. }
  10287. //---------------------------------------------------------------------------
  10288. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10289. {
  10290. CountClicksForWindowPrint(this);
  10291. }
  10292. //---------------------------------------------------------------------------
  10293. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10294. {
  10295. CenterReconnectToolbar();
  10296. }
  10297. //---------------------------------------------------------------------------
  10298. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10299. {
  10300. if (RemoteDirView->ItemFocused != NULL)
  10301. {
  10302. RemoteDirView->ItemFocused->Selected = true;
  10303. }
  10304. }
  10305. //---------------------------------------------------------------------------
  10306. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  10307. {
  10308. if (!FQueueStatusUpdating)
  10309. {
  10310. bool Refresh;
  10311. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  10312. {
  10313. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  10314. }
  10315. else
  10316. {
  10317. if (FQueueFileList->GetCount() > 0)
  10318. {
  10319. FQueueFileList->Clear();
  10320. Refresh = true;
  10321. }
  10322. }
  10323. int Count = FQueueFileList->GetCount();
  10324. if (QueueFileList->Items->Count != Count)
  10325. {
  10326. DebugAssert(Refresh);
  10327. QueueFileList->Items->Count = Count;
  10328. }
  10329. if (Refresh)
  10330. {
  10331. QueueFileList->Invalidate();
  10332. }
  10333. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  10334. }
  10335. }
  10336. //---------------------------------------------------------------------------
  10337. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  10338. {
  10339. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  10340. }
  10341. //---------------------------------------------------------------------------
  10342. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  10343. {
  10344. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  10345. // See TSynchronizeChecklistDialog::UpdateTimer
  10346. if (QueueFileList->Items->Count > 0)
  10347. {
  10348. QueueFileList->Items->Item[0]->MakeVisible(false);
  10349. }
  10350. UpdateQueueFileList();
  10351. }
  10352. //---------------------------------------------------------------------------
  10353. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  10354. {
  10355. Active = QueueView3->Focused() || QueueFileList->Focused();
  10356. }
  10357. //---------------------------------------------------------------------------
  10358. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  10359. {
  10360. QueueLabelUpdateStatus();
  10361. }
  10362. //---------------------------------------------------------------------------
  10363. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  10364. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  10365. {
  10366. int State = FQueueFileList->GetState(Item->Index);
  10367. if (State == qfsProcessed)
  10368. {
  10369. Sender->Canvas->Font->Color = clGrayText;
  10370. }
  10371. }
  10372. //---------------------------------------------------------------------------
  10373. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  10374. {
  10375. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  10376. QueueFileList->Column[0]->Width =
  10377. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  10378. }
  10379. //---------------------------------------------------------------------------
  10380. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  10381. {
  10382. QueueFileListColumnAutoSize();
  10383. }
  10384. //---------------------------------------------------------------------------
  10385. void __fastcall TCustomScpExplorerForm::CloseApp()
  10386. {
  10387. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  10388. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  10389. // So we clear the flag here.
  10390. DebugAssert(NonVisualDataModule->Busy);
  10391. NonVisualDataModule->EndBusy();
  10392. try
  10393. {
  10394. Close();
  10395. }
  10396. __finally
  10397. {
  10398. NonVisualDataModule->StartBusy();
  10399. }
  10400. }
  10401. //---------------------------------------------------------------------------
  10402. bool __fastcall TCustomScpExplorerForm::IsActiveTerminal(TTerminal * Terminal)
  10403. {
  10404. return (Terminal != NULL) && Terminal->Active;
  10405. }
  10406. //---------------------------------------------------------------------------
  10407. bool __fastcall TCustomScpExplorerForm::HasActiveTerminal()
  10408. {
  10409. return IsActiveTerminal(Terminal);
  10410. }