CustomScpExplorer.cpp 367 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143
  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 = 3;
  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. TAutoBatch::TAutoBatch(TCustomScpExplorerForm * Form) :
  100. FForm(Form)
  101. {
  102. FForm->BatchStart(FBatchStorage);
  103. }
  104. //---------------------------------------------------------------------------
  105. TAutoBatch::~TAutoBatch()
  106. {
  107. FForm->BatchEnd(FBatchStorage);
  108. }
  109. //---------------------------------------------------------------------------
  110. struct TTransferOperationParam
  111. {
  112. TTransferOperationParam();
  113. UnicodeString TargetDirectory;
  114. bool Temp;
  115. bool DragDrop;
  116. int Options;
  117. TAutoSwitch Queue;
  118. };
  119. //---------------------------------------------------------------------------
  120. TTransferOperationParam::TTransferOperationParam()
  121. {
  122. Temp = false;
  123. DragDrop = false;
  124. Options = 0;
  125. Queue = asAuto;
  126. }
  127. //---------------------------------------------------------------------------
  128. class TTransferPresetNoteData : public TObject
  129. {
  130. public:
  131. UnicodeString Message;
  132. };
  133. //---------------------------------------------------------------------------
  134. class TTerminalNoteData : public TObject
  135. {
  136. public:
  137. TManagedTerminal * Terminal;
  138. };
  139. //---------------------------------------------------------------------------
  140. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  141. FFormRestored(false),
  142. TForm(Owner)
  143. {
  144. FInvalid = false;
  145. FCurrentSide = osRemote;
  146. FEverShown = false;
  147. FDocks = new TList();
  148. InitControls();
  149. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  150. // So that UpdateCustomCommandsToolbar called from RestoreParams works
  151. NonVisualDataModule->ScpExplorer = this;
  152. RestoreParams();
  153. ConfigurationChanged();
  154. RemoteDirView->Invalidate();
  155. FAutoOperation = false;
  156. FOnFileOperationFinished = NULL;
  157. FForceExecution = false;
  158. FIgnoreNextDialogChar = 0;
  159. FErrorList = NULL;
  160. FSynchronizeProgressForm = NULL;
  161. FProgressForm = NULL;
  162. FRefreshLocalDirectory = false;
  163. FRefreshRemoteDirectory = false;
  164. FDDExtMapFile = NULL;
  165. // CreateMutexW keeps failing with ERROR_NOACCESS
  166. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  167. DebugAssert(FDDExtMutex != NULL);
  168. FDDTargetControl = NULL;
  169. FDelayedDeletionTimer = NULL;
  170. FDelayedDeletionList = new TStringList();
  171. FDDFileList = NULL;
  172. FPendingTempSpaceWarn = false;
  173. FCapturedLog = NULL;
  174. FDragDropOperation = false;
  175. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  176. FAllowTransferPresetAutoSelect = true;
  177. FSessionChanging = false;
  178. FCopyParamDefault = L"";
  179. FSynchronizeController = NULL;
  180. FPendingQueueActionItem = NULL;
  181. FLockLevel = 0;
  182. FLockSuspendLevel = 0;
  183. FDisabledOnLockSuspend = false;
  184. FAlternativeDelete = false;
  185. FTrayIcon = new ::TTrayIcon(0);
  186. FTrayIcon->OnClick = TrayIconClick;
  187. FMaxQueueLength = 0;
  188. FLastContextPopupScreenPoint = TPoint(-1, -1);
  189. FTransferResumeList = NULL;
  190. FMoveToQueue = false;
  191. FStandaloneEditing = false;
  192. FOnFeedSynchronizeError = NULL;
  193. FNeedSession = false;
  194. FDoNotIdleCurrentTerminal = 0;
  195. FIncrementalSearching = 0;
  196. FIncrementalSearchHaveNext = false;
  197. FQueueFileList.reset(new TQueueFileList());
  198. FProgressSide = osCurrent;
  199. FEditorManager = new TEditorManager();
  200. FEditorManager->OnFileChange = ExecutedFileChanged;
  201. FEditorManager->OnFileReload = ExecutedFileReload;
  202. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  203. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  204. FLocalEditors = new TList();
  205. FQueueStatus = NULL;
  206. FQueueStatusSection = new TCriticalSection();
  207. FQueueStatusInvalidated = false;
  208. FQueueItemInvalidated = false;
  209. FQueueStatusUpdating = false;
  210. FQueueActedItem = NULL;
  211. FQueueController = new TQueueController(QueueView3);
  212. FUserActionTimer = new TTimer(this);
  213. FUserActionTimer->Enabled = false;
  214. FUserActionTimer->Interval = 10;
  215. FUserActionTimer->OnTimer = UserActionTimer;
  216. FNotes = new TStringList();
  217. FNoteTimer = new TTimer(this);
  218. FNoteTimer->Enabled = false;
  219. FNoteTimer->OnTimer = NoteTimer;
  220. FOnNoteClick = NULL;
  221. FOle32Library = LoadLibrary(L"Ole32.dll");
  222. FDragMoveCursor = FOle32Library != NULL ?
  223. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  224. UseSystemSettings(this);
  225. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  226. static_cast<TObject*>(GetComponent(fcTransferList)));
  227. DebugAssert(TransferList != NULL);
  228. FTransferListHoverIndex = -1;
  229. TransferList->OnChange = TransferListChange;
  230. TransferList->OnDrawItem = TransferListDrawItem;
  231. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))));
  232. SetSubmenu(NonVisualDataModule->ColorMenuItem);
  233. UseDesktopFont(SessionsPageControl);
  234. UpdateSessionsPageControlHeight();
  235. UseDesktopFont(RemoteDirView);
  236. UseDesktopFont(RemoteDriveView);
  237. UseDesktopFont(QueueView3);
  238. UseDesktopFont(QueueFileList);
  239. UseDesktopFont(QueueLabel);
  240. UseDesktopFont(RemoteStatusBar);
  241. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  242. QueueSplitter->ShowHint = true;
  243. reinterpret_cast<TLabel*>(QueueFileListSplitter)->OnDblClick = QueueFileListSplitterDblClick;
  244. QueueFileListSplitter->ShowHint = true;
  245. RemotePanelSplitter->ShowHint = true;
  246. UpdateImages();
  247. FCustomCommandMenu = CreateTBXPopupMenu(this);
  248. FCustomCommandLocalFileList = NULL;
  249. FCustomCommandRemoteFileList = NULL;
  250. FSessionColors = new TPngImageList(this);
  251. FSessionColors->ColorDepth = cd32Bit;
  252. AddFixedSessionImages();
  253. SessionsPageControl->Images = FSessionColors;
  254. UpdateQueueLabel();
  255. CreateHiddenWindow();
  256. StartUpdates();
  257. }
  258. //---------------------------------------------------------------------------
  259. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  260. {
  261. FInvalid = true;
  262. // this has to be one of the very first things to do
  263. StopUpdateThread();
  264. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  265. TTerminalManager::Instance()->ScpExplorer = NULL;
  266. delete FCustomCommandLocalFileList;
  267. delete FCustomCommandRemoteFileList;
  268. delete FCustomCommandMenu;
  269. delete FTrayIcon;
  270. FTrayIcon = NULL;
  271. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  272. delete FEditorManager;
  273. ForceCloseLocalEditors();
  274. delete FLocalEditors;
  275. HideFileFindDialog();
  276. if (FDelayedDeletionTimer)
  277. {
  278. DoDelayedDeletion(NULL);
  279. SAFE_DESTROY(FDelayedDeletionTimer);
  280. }
  281. SAFE_DESTROY(FDelayedDeletionList);
  282. // sometimes we do not get DDEnd so the list is not released
  283. SAFE_DESTROY(FDDFileList);
  284. DebugAssert(FSynchronizeController == NULL);
  285. CloseHandle(FDDExtMutex);
  286. FDDExtMutex = NULL;
  287. FreeLibrary(FOle32Library);
  288. FOle32Library = NULL;
  289. FDragMoveCursor = NULL;
  290. DebugAssert(!FErrorList);
  291. if (FEverShown)
  292. {
  293. // when window is never shown (like when running command-line operation),
  294. // particularly window site is not restored correctly (BoundsRect value set
  295. // in RestoreForm gets lost during handle allocation), so we do not want
  296. // it to be stored
  297. StoreParams();
  298. }
  299. ManagedSession = NULL;
  300. Queue = NULL;
  301. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  302. NonVisualDataModule->ScpExplorer = NULL;
  303. delete FQueueController;
  304. FQueueController = NULL;
  305. delete FQueueStatusSection;
  306. FQueueStatusSection = NULL;
  307. delete FQueueStatus;
  308. FQueueStatus = NULL;
  309. delete FUserActionTimer;
  310. FUserActionTimer = NULL;
  311. delete FNoteTimer;
  312. delete FNotes;
  313. delete FNoteData;
  314. SAFE_DESTROY(FDocks);
  315. SessionsPageControl->Images = NULL;
  316. // only after clearing Terminal (after SessionListChanged => ... => AddSessionColor is called)
  317. SAFE_DESTROY(FSessionColors);
  318. SAFE_DESTROY(FSessionsDragDropFilesEx);
  319. SAFE_DESTROY(FHistoryMenu[0][0]);
  320. SAFE_DESTROY(FHistoryMenu[0][1]);
  321. SAFE_DESTROY(FHistoryMenu[1][0]);
  322. SAFE_DESTROY(FHistoryMenu[1][1]);
  323. if (FHiddenWindow != NULL)
  324. {
  325. DestroyWindow(FHiddenWindow);
  326. FHiddenWindow = NULL;
  327. }
  328. }
  329. //---------------------------------------------------------------------------
  330. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  331. {
  332. std::unique_ptr<TSessionData> Data;
  333. if (!Session.IsEmpty())
  334. {
  335. bool DefaultsOnly;
  336. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  337. }
  338. TTerminalManager * Manager = TTerminalManager::Instance();
  339. for (int Index = 0; Index < Manager->Count; Index++)
  340. {
  341. TTerminal * Terminal = Manager->Sessions[Index];
  342. if (Session.IsEmpty() ||
  343. Manager->IsActiveTerminalForSite(Terminal, Data.get()))
  344. {
  345. if (Path.IsEmpty())
  346. {
  347. Terminal->ClearCaches();
  348. }
  349. else
  350. {
  351. Terminal->DirectoryModified(Path, true);
  352. }
  353. }
  354. }
  355. // We should flag a pending refresh for the background terminals or busy foreground terminals
  356. if (HasActiveTerminal() &&
  357. CanCommandLineFromAnotherInstance() &&
  358. (Session.IsEmpty() || Manager->IsActiveTerminalForSite(Terminal, Data.get())) &&
  359. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  360. {
  361. Terminal->ReloadDirectory();
  362. }
  363. }
  364. //---------------------------------------------------------------------------
  365. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  366. {
  367. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  368. size_t MessageSize = sizeof(TCopyDataMessage);
  369. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  370. if (Result)
  371. {
  372. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  373. Result = (Message.Version == TCopyDataMessage::Version1);
  374. if (Result)
  375. {
  376. switch (Message.Command)
  377. {
  378. case TCopyDataMessage::CommandCanCommandLine:
  379. Result = CanCommandLineFromAnotherInstance();
  380. break;
  381. case TCopyDataMessage::CommandCommandLine:
  382. {
  383. UnicodeString CommandLine(Message.CommandLine);
  384. Result = CommandLineFromAnotherInstance(CommandLine);
  385. }
  386. break;
  387. case TCopyDataMessage::RefreshPanel:
  388. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  389. break;
  390. case TCopyDataMessage::MainWindowCheck:
  391. Result = true;
  392. break;
  393. default:
  394. Result = false;
  395. break;
  396. }
  397. }
  398. }
  399. Message.Result = Result ? 1 : 0;
  400. }
  401. //---------------------------------------------------------------------------
  402. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  403. {
  404. WNDCLASS WindowClass = {0};
  405. WindowClass.lpfnWndProc = DefWindowProc;
  406. WindowClass.hInstance = HInstance;
  407. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  408. FHiddenWindow = NULL;
  409. if (RegisterClass(&WindowClass))
  410. {
  411. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  412. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  413. }
  414. }
  415. //---------------------------------------------------------------------------
  416. bool __fastcall TCustomScpExplorerForm::CanConsole()
  417. {
  418. return
  419. HasActiveTerminal() &&
  420. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  421. }
  422. //---------------------------------------------------------------------------
  423. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  424. {
  425. bool Result = Visible && !NonVisualDataModule->Busy;
  426. return Result;
  427. }
  428. //---------------------------------------------------------------------------
  429. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  430. const UnicodeString & CommandLine)
  431. {
  432. TProgramParams Params(CommandLine);
  433. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  434. if (Result)
  435. {
  436. NonVisualDataModule->StartBusy();
  437. try
  438. {
  439. // this action is initiated from another process,
  440. // so it's likely that our window is not visible,
  441. // and user won't see what is going on
  442. Application->BringToFront();
  443. // reload sessions as we may be asked to open a session
  444. // just stored by another instance
  445. StoredSessions->Reload();
  446. UnicodeString SessionName = Params.Param[1];
  447. std::unique_ptr<TObjectList> DataList(new TObjectList());
  448. UnicodeString DownloadFile; // unused
  449. try
  450. {
  451. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this);
  452. }
  453. catch (EAbort &)
  454. {
  455. // Happens when opening session in PuTTY and in other situations,
  456. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  457. // to process the command-line on its own (or to pass it to yet another instance)
  458. }
  459. if (DataList->Count > 0)
  460. {
  461. TTerminalManager * Manager = TTerminalManager::Instance();
  462. Manager->ActiveSession = Manager->NewTerminals(DataList.get());
  463. }
  464. }
  465. __finally
  466. {
  467. NonVisualDataModule->EndBusy();
  468. }
  469. }
  470. return Result;
  471. }
  472. //---------------------------------------------------------------------------
  473. TManagedTerminal * TCustomScpExplorerForm::GetTerminal()
  474. {
  475. TManagedTerminal * Result;
  476. if ((FManagedSession != NULL) && !FManagedSession->LocalBrowser)
  477. {
  478. Result = FManagedSession;
  479. }
  480. else
  481. {
  482. Result = NULL;
  483. }
  484. return Result;
  485. }
  486. //---------------------------------------------------------------------------
  487. void __fastcall TCustomScpExplorerForm::SetManagedSession(TManagedTerminal * value)
  488. {
  489. if (FManagedSession != value)
  490. {
  491. SessionChanging();
  492. DoSetManagedSession(value, false);
  493. }
  494. }
  495. //---------------------------------------------------------------------------
  496. void __fastcall TCustomScpExplorerForm::DoSetManagedSession(TManagedTerminal * value, bool Replace)
  497. {
  498. DebugAssert(!Replace || ((value != NULL) && !value->LocalBrowser));
  499. FManagedSession = value;
  500. {
  501. TValueRestorer<bool> AllowTransferPresetAutoSelectRestorer(FAllowTransferPresetAutoSelect);
  502. FAllowTransferPresetAutoSelect = false;
  503. TAutoFlag SessionChangingFlag(FSessionChanging);
  504. SessionChanged(Replace);
  505. }
  506. if (Terminal != NULL)
  507. {
  508. TransferPresetAutoSelect();
  509. }
  510. // Update app and tab titles, prevented above by FSessionChanging.
  511. UpdateControls();
  512. }
  513. //---------------------------------------------------------------------------
  514. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  515. {
  516. DoSetManagedSession(value, true);
  517. }
  518. //---------------------------------------------------------------------------
  519. void __fastcall TCustomScpExplorerForm::SessionChanging()
  520. {
  521. if (ManagedSession != NULL)
  522. {
  523. UpdateSession(ManagedSession);
  524. }
  525. }
  526. //---------------------------------------------------------------------------
  527. void __fastcall TCustomScpExplorerForm::SessionChanged(bool Replaced)
  528. {
  529. if (Terminal != NULL)
  530. {
  531. UpdateSessionColor((TColor)Terminal->StateData->Color);
  532. }
  533. if (!TTerminalManager::Instance()->Updating)
  534. {
  535. SessionListChanged();
  536. }
  537. if (Terminal != NULL)
  538. {
  539. if (Replaced)
  540. {
  541. RemoteDirView->ReplaceTerminal(Terminal);
  542. }
  543. else
  544. {
  545. RemoteDirView->Terminal = Terminal;
  546. }
  547. if (Terminal->Active)
  548. {
  549. Terminal->RefreshDirectory();
  550. }
  551. if (WinConfiguration->PreservePanelState)
  552. {
  553. DirView(osRemote)->RestoreState(Terminal->RemoteExplorerState);
  554. }
  555. }
  556. else
  557. {
  558. RemoteDirView->Terminal = NULL;
  559. }
  560. InitStatusBar();
  561. UpdateTransferList();
  562. // Update panels Enable state before refreshing the labels
  563. UpdateControls();
  564. }
  565. //---------------------------------------------------------------------------
  566. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  567. {
  568. if (Queue != value)
  569. {
  570. if (FQueue != NULL)
  571. {
  572. FQueue->OnListUpdate = NULL;
  573. FQueue->OnQueueItemUpdate = NULL;
  574. }
  575. FQueue = value;
  576. if (FQueue != NULL)
  577. {
  578. DebugAssert(FQueue->OnListUpdate == NULL);
  579. FQueue->OnListUpdate = QueueListUpdate;
  580. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  581. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  582. }
  583. QueueChanged();
  584. }
  585. }
  586. //---------------------------------------------------------------------------
  587. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  588. TListItem * Item)
  589. {
  590. if (FQueueActedItem == Item)
  591. {
  592. FQueueActedItem = NULL;
  593. if ((QueueView3->PopupMenu != NULL) &&
  594. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  595. {
  596. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  597. ReleaseCapture();
  598. }
  599. }
  600. if (Item->Data == FPendingQueueActionItem)
  601. {
  602. FPendingQueueActionItem = NULL;
  603. }
  604. }
  605. //---------------------------------------------------------------------------
  606. bool TCustomScpExplorerForm::IsAnythingQueued()
  607. {
  608. return (FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0);
  609. }
  610. //---------------------------------------------------------------------------
  611. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  612. {
  613. {
  614. TGuard Guard(FQueueStatusSection);
  615. FQueueStatusInvalidated = false;
  616. if (FQueue != NULL)
  617. {
  618. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  619. }
  620. }
  621. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  622. {
  623. FMaxQueueLength = FQueueStatus->Count;
  624. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  625. }
  626. {
  627. TAutoFlag Flag(FQueueStatusUpdating);
  628. FQueueController->UpdateQueueStatus(FQueueStatus);
  629. }
  630. UpdateQueueFileList();
  631. SetQueueProgress();
  632. UpdateQueueView();
  633. bool IsEmpty = !IsAnythingQueued();
  634. if (IsEmpty && (Terminal != NULL))
  635. {
  636. TOnceDoneOperation OnceDoneOperation =
  637. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  638. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  639. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  640. {
  641. FQueue->Enabled = false;
  642. }
  643. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  644. {
  645. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  646. }
  647. }
  648. }
  649. //---------------------------------------------------------------------------
  650. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  651. {
  652. UnicodeString Result;
  653. if (FQueueStatus != NULL)
  654. {
  655. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  656. {
  657. TFileOperationProgressType * ProgressData =
  658. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  659. if ((ProgressData != NULL) && ProgressData->InProgress)
  660. {
  661. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  662. }
  663. }
  664. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  665. {
  666. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  667. }
  668. }
  669. return Result;
  670. }
  671. //---------------------------------------------------------------------------
  672. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  673. {
  674. ComponentVisible[fcQueueView] =
  675. (WinConfiguration->QueueView.Show == qvShow) ||
  676. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  677. (FQueueStatus != NULL) && (FQueueStatus->Count > 0));
  678. }
  679. //---------------------------------------------------------------------------
  680. void __fastcall TCustomScpExplorerForm::QueueChanged()
  681. {
  682. if (FQueueStatus != NULL)
  683. {
  684. // UpdateFileList implementation relies on the status being recreated when session changes
  685. delete FQueueStatus;
  686. FQueueStatus = NULL;
  687. }
  688. UpdateQueueStatus(true);
  689. }
  690. //---------------------------------------------------------------------------
  691. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  692. {
  693. if (FQueue == Queue)
  694. {
  695. FQueueStatusInvalidated = true;
  696. }
  697. }
  698. //---------------------------------------------------------------------------
  699. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  700. TQueueItem * Item)
  701. {
  702. if (FQueue == Queue)
  703. {
  704. TGuard Guard(FQueueStatusSection);
  705. // this may be running in parallel with QueueChanged
  706. if (FQueueStatus != NULL)
  707. {
  708. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  709. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  710. {
  711. FRefreshLocalDirectory = (QueueItem == NULL) ||
  712. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  713. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  714. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  715. }
  716. if (QueueItem != NULL)
  717. {
  718. QueueItem->UserData = (void*)true;
  719. FQueueItemInvalidated = true;
  720. }
  721. }
  722. }
  723. }
  724. //---------------------------------------------------------------------------
  725. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  726. {
  727. // during standalone editing, we have no way to see/control queue,
  728. // so we have to always popup prompts automatically
  729. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  730. }
  731. //---------------------------------------------------------------------------
  732. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  733. {
  734. if (FQueueStatus != NULL)
  735. {
  736. bool QueueAutoPopup = IsQueueAutoPopup();
  737. bool NeedRefresh = FQueueController->NeedRefresh();
  738. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  739. FQueueItemInvalidated = false;
  740. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  741. bool Updated = false;
  742. TQueueItemProxy * QueueItem;
  743. bool WasUserAction;
  744. for (int Index = 0; Index < Limit; Index++)
  745. {
  746. bool Update = false;
  747. QueueItem = FQueueStatus->Items[Index];
  748. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  749. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  750. (FPendingQueueActionItem == NULL))
  751. {
  752. FPendingQueueActionItem = QueueItem;
  753. FUserActionTimer->Enabled = true;
  754. }
  755. if ((bool)QueueItem->UserData)
  756. {
  757. QueueItem->UserData = (void*)false;
  758. QueueItem->Update();
  759. Updated = true;
  760. Update = true;
  761. }
  762. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  763. {
  764. Update = true;
  765. }
  766. if (Update)
  767. {
  768. FQueueController->RefreshQueueItem(QueueItem);
  769. }
  770. }
  771. if (Updated)
  772. {
  773. NonVisualDataModule->UpdateNonVisibleActions();
  774. SetQueueProgress();
  775. UpdateQueueFileList();
  776. }
  777. }
  778. }
  779. //---------------------------------------------------------------------------
  780. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  781. {
  782. if (FTaskbarList != NULL)
  783. {
  784. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  785. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  786. }
  787. }
  788. //---------------------------------------------------------------------------
  789. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  790. {
  791. if (Progress >= 0)
  792. {
  793. if (FTaskbarList != NULL)
  794. {
  795. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  796. }
  797. }
  798. else
  799. {
  800. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  801. }
  802. }
  803. //---------------------------------------------------------------------------
  804. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  805. {
  806. int OverallProgress;
  807. // FProgressForm is null when this is called from SetQueueProgress
  808. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  809. {
  810. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  811. }
  812. else if (!TFileOperationProgressType::IsIndeterminateOperation(ProgressData->Operation))
  813. {
  814. OverallProgress = ProgressData->OverallProgress();
  815. }
  816. else
  817. {
  818. OverallProgress = -1;
  819. }
  820. SetTaskbarListProgressValue(OverallProgress);
  821. }
  822. //---------------------------------------------------------------------------
  823. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  824. {
  825. TTerminalManager::Instance()->QueueStatusUpdated();
  826. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  827. {
  828. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  829. {
  830. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  831. {
  832. TFileOperationProgressType * ProgressData = NULL;
  833. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  834. {
  835. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  836. }
  837. if ((ProgressData != NULL) &&
  838. ProgressData->InProgress)
  839. {
  840. SetTaskbarListProgressValue(ProgressData);
  841. }
  842. else
  843. {
  844. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  845. }
  846. }
  847. else
  848. {
  849. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  850. }
  851. }
  852. else
  853. {
  854. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  855. }
  856. }
  857. UpdateQueueLabel();
  858. }
  859. //---------------------------------------------------------------------------
  860. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  861. {
  862. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  863. if (FQueueStatus != NULL)
  864. {
  865. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  866. if (ActiveAndPendingPrimaryCount > 0)
  867. {
  868. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  869. }
  870. }
  871. QueueLabel->Caption = Caption;
  872. }
  873. //---------------------------------------------------------------------------
  874. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  875. {
  876. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  877. static_cast<TComponent*>(GetComponent(fcTransferList)));
  878. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  879. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  880. TransferList->Strings->BeginUpdate();
  881. try
  882. {
  883. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  884. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  885. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  886. if (FTransferDropDownHint.IsEmpty())
  887. {
  888. FTransferDropDownHint = TransferDropDown->Hint;
  889. }
  890. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  891. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  892. TransferDropDown->Text = StripHotkey(Name);
  893. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  894. (FTransferDropDownHint, StripHotkey(Name),
  895. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  896. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General))));
  897. // update the label, otherwise when it is updated only on the first draw
  898. // of the list, it is drawn "bold" for some reason
  899. FTransferListHoverIndex = TransferList->ItemIndex;
  900. UpdateTransferLabel();
  901. }
  902. __finally
  903. {
  904. TransferList->Strings->EndUpdate();
  905. }
  906. }
  907. //---------------------------------------------------------------------------
  908. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  909. {
  910. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  911. DebugAssert(Toolbar != NULL);
  912. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  913. }
  914. //---------------------------------------------------------------------------
  915. void __fastcall TCustomScpExplorerForm::UpdateActions()
  916. {
  917. TForm::UpdateActions();
  918. if (ComponentVisible[fcCustomCommandsBand])
  919. {
  920. UpdateCustomCommandsToolbar();
  921. }
  922. }
  923. //---------------------------------------------------------------------------
  924. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  925. {
  926. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  927. }
  928. //---------------------------------------------------------------------------
  929. void __fastcall TCustomScpExplorerForm::UpdateRowSelect(TCustomDirView * DirView)
  930. {
  931. if (DirView->RowSelect != WinConfiguration->FullRowSelect)
  932. {
  933. DirView->RowSelect = WinConfiguration->FullRowSelect;
  934. // selection is not redrawn automatically when RowSelect changes
  935. DirView->Invalidate();
  936. }
  937. }
  938. //---------------------------------------------------------------------------
  939. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  940. {
  941. Color = GetBtnFaceColor();
  942. DebugAssert(Configuration && RemoteDirView);
  943. RemoteDirView->DDAllowMove = !WinConfiguration->DDDisableMove;
  944. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  945. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  946. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  947. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  948. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  949. UpdateRowSelect(RemoteDirView);
  950. RemoteDriveView->DDAllowMove = !WinConfiguration->DDDisableMove;
  951. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  952. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  953. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  954. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  955. UpdateSessionsPageControlHeight();
  956. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  957. UpdateToolbarDisplayMode();
  958. UpdateTransferList();
  959. if (Terminal != NULL)
  960. {
  961. TransferPresetAutoSelect();
  962. }
  963. UpdateQueueView();
  964. UpdateControls();
  965. // this can be called even before constuctor finishes.
  966. if (FEditorManager != NULL)
  967. {
  968. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  969. }
  970. // this can be called even before constuctor finishes.
  971. if (FLocalEditors != NULL)
  972. {
  973. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  974. {
  975. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  976. }
  977. }
  978. if (ComponentVisible[fcCustomCommandsBand])
  979. {
  980. UpdateCustomCommandsToolbar();
  981. }
  982. // show only when keeping queue items forever,
  983. // otherwise, its enough to have in in the context menu
  984. QueueDeleteAllDoneQueueToolbarItem->Visible =
  985. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  986. if (FFileColorsCurrent != WinConfiguration->FileColors)
  987. {
  988. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  989. FFileColorsCurrent = WinConfiguration->FileColors;
  990. FileColorsChanged();
  991. }
  992. }
  993. //---------------------------------------------------------------------------
  994. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  995. {
  996. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  997. DirView->Invalidate();
  998. }
  999. //---------------------------------------------------------------------------
  1000. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  1001. {
  1002. DoFileColorsChanged(RemoteDirView);
  1003. }
  1004. //---------------------------------------------------------------------------
  1005. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  1006. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  1007. void * /*Arg*/)
  1008. {
  1009. if (Token != NULL)
  1010. {
  1011. TForm * Editor = dynamic_cast<TForm*>(Token);
  1012. DebugAssert(Editor != NULL);
  1013. ReconfigureEditorForm(Editor);
  1014. }
  1015. }
  1016. //---------------------------------------------------------------------------
  1017. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  1018. {
  1019. WinConfiguration->DDTransferConfirmation = asOn;
  1020. }
  1021. //---------------------------------------------------------------------------
  1022. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1023. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1024. {
  1025. // noop
  1026. }
  1027. //---------------------------------------------------------------------------
  1028. bool TCustomScpExplorerForm::GetDoNotShowCopyDialogDefault(bool DragDrop)
  1029. {
  1030. return DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto);
  1031. }
  1032. //---------------------------------------------------------------------------
  1033. void TCustomScpExplorerForm::HandleDoNotShowCopyDialogAgain(bool DragDrop, bool DoNotShowAgain)
  1034. {
  1035. if (DoNotShowAgain)
  1036. {
  1037. if (DragDrop)
  1038. {
  1039. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1040. {
  1041. PopupTrayBalloon(
  1042. NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation, NULL, 0, EnableDDTransferConfirmation, NULL);
  1043. }
  1044. WinConfiguration->DDTransferConfirmation = asOff;
  1045. }
  1046. else
  1047. {
  1048. WinConfiguration->ConfirmTransferring = false;
  1049. }
  1050. }
  1051. else
  1052. {
  1053. // User explicitly unchecked "do not show again",
  1054. // so show him the dialog the next time
  1055. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1056. {
  1057. WinConfiguration->DDTransferConfirmation = asOn;
  1058. }
  1059. }
  1060. }
  1061. //---------------------------------------------------------------------------
  1062. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1063. TTransferDirection Direction, TTransferType Type, bool Temp,
  1064. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1065. bool Confirm, bool DragDrop, int Options)
  1066. {
  1067. bool Result = true;
  1068. DebugAssert(HasActiveTerminal());
  1069. // these parameters are known in advance
  1070. int Params =
  1071. FLAGMASK(Type == ttMove, cpDelete);
  1072. bool ToTemp = (Temp && (Direction == tdToLocal));
  1073. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1074. {
  1075. bool DisableNewerOnly =
  1076. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1077. ToTemp;
  1078. Options |=
  1079. FLAGMASK(ToTemp, coTemp) |
  1080. FLAGMASK(!Terminal->IsCapable[fcBackgroundTransfers], coDisableQueue) |
  1081. coDoNotShowAgain;
  1082. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1083. int CopyParamAttrs = (Direction == tdToRemote ?
  1084. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1085. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1086. int OutputOptions = FLAGMASK(GetDoNotShowCopyDialogDefault(DragDrop), cooDoNotShowAgain);
  1087. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1088. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1089. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1090. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1091. if (Result)
  1092. {
  1093. HandleDoNotShowCopyDialogAgain(DragDrop, FLAGSET(OutputOptions, cooDoNotShowAgain));
  1094. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1095. }
  1096. }
  1097. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fcBackgroundTransfers])
  1098. {
  1099. Configuration->Usage->Inc(L"TransfersOnBackground");
  1100. // these parameter are known only after transfer dialog
  1101. Params |=
  1102. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1103. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1104. Result = false;
  1105. ClearTransferSourceSelection(Direction);
  1106. }
  1107. return Result;
  1108. }
  1109. //---------------------------------------------------------------------------
  1110. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1111. {
  1112. if (FOnFileOperationFinished != NULL)
  1113. {
  1114. FOnFileOperationFinished(UnicodeString(), true);
  1115. }
  1116. else
  1117. {
  1118. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1119. if (HasDirView[Side])
  1120. {
  1121. DirView(Side)->SelectAll(smNone);
  1122. }
  1123. }
  1124. }
  1125. //---------------------------------------------------------------------------
  1126. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1127. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1128. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1129. int Params)
  1130. {
  1131. DebugAssert(Queue != NULL);
  1132. bool SingleFile = false;
  1133. if (FileList->Count == 1)
  1134. {
  1135. if (Direction == tdToRemote)
  1136. {
  1137. UnicodeString FileName = FileList->Strings[0];
  1138. SingleFile = FileExists(ApiPath(FileName));
  1139. }
  1140. else
  1141. {
  1142. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1143. SingleFile = !File->IsDirectory;
  1144. }
  1145. }
  1146. TQueueItem * QueueItem;
  1147. if (Direction == tdToRemote)
  1148. {
  1149. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1150. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1151. }
  1152. else
  1153. {
  1154. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1155. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1156. }
  1157. AddQueueItem(Queue, QueueItem, Terminal);
  1158. }
  1159. //---------------------------------------------------------------------------
  1160. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1161. {
  1162. if (Queue->IsEmpty)
  1163. {
  1164. Terminal->QueueOperationStart = Now();
  1165. }
  1166. Queue->AddItem(QueueItem);
  1167. }
  1168. //---------------------------------------------------------------------------
  1169. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1170. {
  1171. }
  1172. //---------------------------------------------------------------------------
  1173. void __fastcall TCustomScpExplorerForm::InitControls()
  1174. {
  1175. DebugAssert(FDocks != NULL);
  1176. for (int Index = 0; Index < ComponentCount; Index++)
  1177. {
  1178. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1179. if ((Dock != NULL) && (Dock->Tag == 0))
  1180. {
  1181. FDocks->Add(Dock);
  1182. }
  1183. }
  1184. CollectItemsWithTextDisplayMode(this);
  1185. }
  1186. //---------------------------------------------------------------------------
  1187. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1188. {
  1189. ConfigurationChanged();
  1190. QueuePanel->Height =
  1191. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1192. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1193. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1194. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1195. QueueFileList->Visible = WinConfiguration->QueueView.FileList;
  1196. QueueFileList->Height =
  1197. LoadDimension(WinConfiguration->QueueView.FileListHeight, WinConfiguration->QueueView.FileListHeightPixelsPerInch, this);
  1198. if (QueueFileList->Visible)
  1199. {
  1200. AdjustQueueLayout();
  1201. }
  1202. UpdateCustomCommandsToolbar();
  1203. }
  1204. //---------------------------------------------------------------------------
  1205. void __fastcall TCustomScpExplorerForm::StoreParams()
  1206. {
  1207. int APixelsPerInch = GetControlPixelsPerInch(this);
  1208. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1209. WinConfiguration->QueueView.HeightPixelsPerInch = APixelsPerInch;
  1210. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1211. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1212. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1213. WinConfiguration->QueueView.FileList = QueueFileList->Visible;
  1214. WinConfiguration->QueueView.FileListHeight = QueueFileList->Height;
  1215. WinConfiguration->QueueView.FileListHeightPixelsPerInch = APixelsPerInch;
  1216. }
  1217. //---------------------------------------------------------------------------
  1218. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1219. {
  1220. if (!FFormRestored)
  1221. {
  1222. FFormRestored = true;
  1223. RestoreFormParams();
  1224. }
  1225. TForm::CreateParams(Params);
  1226. }
  1227. //---------------------------------------------------------------------------
  1228. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1229. {
  1230. DebugAssert(FDocks != NULL);
  1231. for (int Index = 0; Index < FDocks->Count; Index++)
  1232. {
  1233. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1234. }
  1235. }
  1236. //---------------------------------------------------------------------------
  1237. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1238. {
  1239. SetDockAllowDrag(true);
  1240. while (!ButtonsStr.IsEmpty())
  1241. {
  1242. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1243. UnicodeString Name = CutToChar(S, L'=', true);
  1244. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1245. {
  1246. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1247. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1248. {
  1249. while (!S.IsEmpty())
  1250. {
  1251. UnicodeString S2 = CutToChar(S, L',', true);
  1252. UnicodeString Name = CutToChar(S2, L':', true);
  1253. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1254. {
  1255. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1256. if (SameText(GetToolbarItemName(Item), Name))
  1257. {
  1258. // forward compatibility
  1259. UnicodeString S3 = CutToChar(S2, L':', true);
  1260. bool Visible = StrToIntDef(S3, 0);
  1261. Item->Visible = Visible;
  1262. }
  1263. }
  1264. }
  1265. }
  1266. }
  1267. }
  1268. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1269. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1270. }
  1271. //---------------------------------------------------------------------------
  1272. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1273. {
  1274. UnicodeString Result;
  1275. SetDockAllowDrag(true);
  1276. Result = ::GetToolbarsLayoutStr(this);
  1277. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1278. return Result;
  1279. }
  1280. //---------------------------------------------------------------------------
  1281. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1282. {
  1283. UnicodeString Result;
  1284. if (Item->Action != NULL)
  1285. {
  1286. Result = Item->Action->Name;
  1287. Result = RemoveSuffix(Result, L"Action", true);
  1288. }
  1289. else
  1290. {
  1291. Result = Item->Name;
  1292. Result = RemoveSuffix(Result, L"SubmenuItem", true);
  1293. Result = RemoveSuffix(Result, L"Item", true);
  1294. }
  1295. return Result;
  1296. }
  1297. //---------------------------------------------------------------------------
  1298. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1299. {
  1300. UnicodeString Result;
  1301. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1302. {
  1303. UnicodeString ToolbarStr;
  1304. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1305. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1306. {
  1307. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1308. {
  1309. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1310. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1311. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1312. // and save the changes here only.
  1313. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1314. {
  1315. UnicodeString Name = GetToolbarItemName(Item);
  1316. DebugAssert(Name.Pos(L"TBX") == 0);
  1317. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1318. }
  1319. }
  1320. }
  1321. if (!ToolbarStr.IsEmpty())
  1322. {
  1323. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1324. }
  1325. }
  1326. return Result;
  1327. }
  1328. //---------------------------------------------------------------------------
  1329. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1330. {
  1331. DebugAssert(FProgressForm == NULL);
  1332. bool AllowSkip = (Terminal != NULL) ? Terminal->IsCapable[fcSkipTransfer] : false;
  1333. FProgressForm = new TProgressForm(Application, (FTransferResumeList != NULL), AllowSkip, SynchronizeProgress);
  1334. FProgressForm->DeleteLocalToRecycleBin =
  1335. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1336. FProgressForm->DeleteRemoteToRecycleBin =
  1337. (Terminal != NULL) &&
  1338. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1339. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1340. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1341. LockWindow();
  1342. // Actually, do not know what hides the progress once the operation finishes
  1343. // (it is possibly the SetQueueProgress - and we should not rely on that)
  1344. SetTaskbarListProgressState(TBPF_NORMAL);
  1345. }
  1346. //---------------------------------------------------------------------------
  1347. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1348. {
  1349. UnlockWindow();
  1350. SAFE_DESTROY(FProgressForm);
  1351. SetQueueProgress();
  1352. }
  1353. //---------------------------------------------------------------------------
  1354. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1355. TFileOperationProgressType & ProgressData)
  1356. {
  1357. // Download to temporary local directory
  1358. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1359. {
  1360. bool Continue = true;
  1361. FPendingTempSpaceWarn = false;
  1362. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1363. if (!Continue)
  1364. {
  1365. Abort();
  1366. }
  1367. }
  1368. // operation is being executed and we still didn't show up progress form
  1369. if (ProgressData.InProgress && !FProgressForm)
  1370. {
  1371. CreateProgressForm(NULL);
  1372. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1373. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1374. }
  1375. // operation is finished (or terminated), so we hide progress form
  1376. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1377. {
  1378. DestroyProgressForm();
  1379. if (ProgressData.Operation == foCalculateSize)
  1380. {
  1381. // When calculating size before transfer, the abort caused by
  1382. // cancel flag set due to "MoveToQueue" below,
  1383. // is not propagated back to us in ExecuteFileOperation,
  1384. // as it is silently swallowed.
  1385. // So we explicitly (re)throw it here.
  1386. if (FMoveToQueue)
  1387. {
  1388. Abort();
  1389. }
  1390. }
  1391. else
  1392. {
  1393. if ((ProgressData.Cancel == csContinue) &&
  1394. !FAutoOperation)
  1395. {
  1396. OperationComplete(ProgressData.StartTime);
  1397. }
  1398. }
  1399. }
  1400. if (FProgressForm &&
  1401. // Can happen only during synchronization, when ending one of the operations
  1402. ProgressData.InProgress)
  1403. {
  1404. FProgressForm->SetProgressData(ProgressData);
  1405. DebugAssert(ProgressData.InProgress);
  1406. SetTaskbarListProgressValue(&ProgressData);
  1407. if (FProgressForm->Cancel > csContinue)
  1408. {
  1409. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1410. if (FProgressForm->Cancel == csCancelFile)
  1411. {
  1412. FProgressForm->ClearCancel();
  1413. }
  1414. // cancel cancels even the move
  1415. FMoveToQueue = false;
  1416. }
  1417. else if (FProgressForm->MoveToQueue)
  1418. {
  1419. FMoveToQueue = true;
  1420. ProgressData.SetCancelAtLeast(csCancel);
  1421. }
  1422. if ((FTransferResumeList != NULL) &&
  1423. ProgressData.InProgress &&
  1424. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1425. !ProgressData.FullFileName.IsEmpty())
  1426. {
  1427. if ((FTransferResumeList->Count == 0) ||
  1428. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1429. {
  1430. // note that we do not recognize directories from files here
  1431. FTransferResumeList->Add(ProgressData.FullFileName);
  1432. }
  1433. }
  1434. }
  1435. }
  1436. //---------------------------------------------------------------------------
  1437. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1438. const TDateTime & StartTime)
  1439. {
  1440. if (GUIConfiguration->BeepOnFinish &&
  1441. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1442. {
  1443. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1444. DWORD Sound;
  1445. if (!ExtractFileExt(BeepSound).IsEmpty())
  1446. {
  1447. Sound = SND_FILENAME;
  1448. }
  1449. else
  1450. {
  1451. Sound = SND_ALIAS;
  1452. }
  1453. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1454. }
  1455. }
  1456. //---------------------------------------------------------------------------
  1457. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1458. TFileOperationProgressType & ProgressData)
  1459. {
  1460. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1461. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1462. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1463. FileOperationProgress(ProgressData);
  1464. }
  1465. //---------------------------------------------------------------------------
  1466. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1467. {
  1468. UnicodeString Result;
  1469. if (FProgressForm != NULL)
  1470. {
  1471. Result = FProgressForm->ProgressStr();
  1472. }
  1473. return Result;
  1474. }
  1475. //---------------------------------------------------------------------------
  1476. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1477. bool DragDrop)
  1478. {
  1479. return (!DragDrop && (DropSourceControl == NULL)) ||
  1480. (DropSourceControl == DirView(osOther));
  1481. }
  1482. //---------------------------------------------------------------------------
  1483. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1484. TFileOperation Operation, TOperationSide Side,
  1485. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1486. TOnceDoneOperation & OnceDoneOperation)
  1487. {
  1488. if (!FAutoOperation)
  1489. {
  1490. // no selection on "/upload", form serves only as event handler
  1491. // (it is not displayed)
  1492. if (PanelOperation(Side, FDragDropOperation) &&
  1493. Visible && (Operation != foCalculateSize) &&
  1494. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1495. {
  1496. if (FOnFileOperationFinished != NULL)
  1497. {
  1498. FOnFileOperationFinished(FileName, Success);
  1499. }
  1500. else
  1501. {
  1502. TOperationSide DViewSide;
  1503. if ((FProgressSide != osCurrent) &&
  1504. IsLocalBrowserMode() && (Side == osLocal)) // Only to limit the impact
  1505. {
  1506. // assume the operation is over the focused panel
  1507. DViewSide = FProgressSide;
  1508. }
  1509. else
  1510. {
  1511. DViewSide = Side;
  1512. }
  1513. TCustomDirView * DView = DirView(DViewSide);
  1514. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(DViewSide));
  1515. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1516. // this can happen when local drive is unplugged in the middle of the operation
  1517. if (Item != NULL)
  1518. {
  1519. if (Success) Item->Selected = false;
  1520. DView->MakeProgressVisible(Item);
  1521. }
  1522. }
  1523. }
  1524. if ((Operation == foCopy) || (Operation == foMove))
  1525. {
  1526. if (DebugAlwaysTrue(!IsLocalBrowserMode()))
  1527. {
  1528. if (Side == osLocal)
  1529. {
  1530. Configuration->Usage->Inc(L"UploadedFiles");
  1531. }
  1532. else
  1533. {
  1534. Configuration->Usage->Inc(L"DownloadedFiles");
  1535. }
  1536. }
  1537. }
  1538. }
  1539. if (Success && (FSynchronizeController != NULL))
  1540. {
  1541. DebugAssert(!IsSideLocalBrowser(osRemote));
  1542. if (Operation == foCopy)
  1543. {
  1544. DebugAssert(Side == osLocal);
  1545. FSynchronizeController->LogOperation(soUpload, FileName);
  1546. }
  1547. else if (Operation == foDelete)
  1548. {
  1549. DebugAssert(Side == osRemote);
  1550. FSynchronizeController->LogOperation(soDelete, FileName);
  1551. }
  1552. }
  1553. if (FProgressForm)
  1554. {
  1555. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1556. }
  1557. }
  1558. //---------------------------------------------------------------------------
  1559. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1560. TFileOperation Operation, TOperationSide Side,
  1561. bool Temp, const UnicodeString & FileName, Boolean Success,
  1562. TOnceDoneOperation & OnceDoneOperation)
  1563. {
  1564. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1565. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1566. OnceDoneOperation);
  1567. }
  1568. //---------------------------------------------------------------------------
  1569. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1570. {
  1571. return false;
  1572. }
  1573. //---------------------------------------------------------------------------
  1574. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1575. {
  1576. return false;
  1577. }
  1578. //---------------------------------------------------------------------------
  1579. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1580. {
  1581. return NULL;
  1582. }
  1583. //---------------------------------------------------------------------------
  1584. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1585. {
  1586. DebugAssert(GetSide(Side) == osRemote);
  1587. DebugUsedParam(Side);
  1588. return RemoteDirView;
  1589. }
  1590. //---------------------------------------------------------------------------
  1591. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1592. {
  1593. DebugAssert(GetSide(Side) == osRemote);
  1594. DebugUsedParam(Side);
  1595. return RemoteDriveView;
  1596. }
  1597. //---------------------------------------------------------------------------
  1598. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1599. {
  1600. DebugAssert(GetSide(Side) == osRemote);
  1601. DebugUsedParam(Side);
  1602. return HasActiveTerminal();
  1603. }
  1604. //---------------------------------------------------------------------------
  1605. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1606. TOperationSide Side, int FilesOnly)
  1607. {
  1608. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1609. }
  1610. //---------------------------------------------------------------------------
  1611. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1612. TOperationSide Side, int FilesOnly)
  1613. {
  1614. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1615. }
  1616. //---------------------------------------------------------------------------
  1617. struct THistoryItemData
  1618. {
  1619. short int Side;
  1620. short int Index;
  1621. };
  1622. //---------------------------------------------------------------------------
  1623. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1624. {
  1625. DirView(Side)->HistoryGo(Index);
  1626. }
  1627. //---------------------------------------------------------------------------
  1628. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1629. {
  1630. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1631. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1632. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1633. }
  1634. //---------------------------------------------------------------------------
  1635. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1636. bool Back)
  1637. {
  1638. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1639. {
  1640. TCustomDirView * DView = DirView(Side);
  1641. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1642. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1643. if (ICount > 10)
  1644. {
  1645. ICount = 10;
  1646. }
  1647. Menu->Items->Clear();
  1648. THistoryItemData Data;
  1649. Data.Side = Side;
  1650. for (short int i = 1; i <= ICount; i++)
  1651. {
  1652. TTBCustomItem * Item = new TTBXItem(Menu);
  1653. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1654. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1655. Item->Hint = DView->HistoryPath[Data.Index];
  1656. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1657. Item->Tag = *reinterpret_cast<int*>(&Data);
  1658. Item->OnClick = HistoryItemClick;
  1659. Menu->Items->Add(Item);
  1660. }
  1661. }
  1662. }
  1663. //---------------------------------------------------------------------------
  1664. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1665. TOperationSide Side, bool Back)
  1666. {
  1667. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1668. {
  1669. // workaround
  1670. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1671. // operator new allocates memory in Pascal code, but calls inline
  1672. // contructor in C++, leading in problems, the function does
  1673. // both in Pascal code
  1674. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1675. UpdateHistoryMenu(Side, Back);
  1676. }
  1677. return FHistoryMenu[Side == osLocal][Back];
  1678. }
  1679. //---------------------------------------------------------------------------
  1680. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1681. TCustomDirView *Sender)
  1682. {
  1683. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1684. UpdateHistoryMenu(Side, true);
  1685. UpdateHistoryMenu(Side, false);
  1686. }
  1687. //---------------------------------------------------------------------------
  1688. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1689. {
  1690. // remote custom commands can be executed only if the server supports shell commands
  1691. // or have secondary shell
  1692. return HasActiveTerminal() && (Terminal->IsCapable[fcSecondaryShell] || Terminal->IsCapable[fcShellAnyCommand]);
  1693. }
  1694. //---------------------------------------------------------------------------
  1695. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1696. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1697. {
  1698. // -1 = hidden, 0 = disabled, 1 = enabled
  1699. int Result;
  1700. TFileCustomCommand RemoteCustomCommand;
  1701. TLocalCustomCommand LocalCustomCommand;
  1702. TFileCustomCommand * NonInteractiveCustomCommand =
  1703. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1704. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1705. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1706. if (FLAGCLEAR(Command.Params, ccLocal))
  1707. {
  1708. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1709. // custom command that does not operate with files can be executed anytime ...
  1710. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1711. {
  1712. // ... but do not show such command in remote file menu
  1713. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1714. {
  1715. Result = AllowedState;
  1716. }
  1717. else
  1718. {
  1719. Result = -1;
  1720. }
  1721. }
  1722. else
  1723. {
  1724. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1725. {
  1726. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1727. }
  1728. else
  1729. {
  1730. Result = -1;
  1731. }
  1732. }
  1733. }
  1734. else
  1735. {
  1736. // custom command that does not operate with files can be executed anytime
  1737. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1738. {
  1739. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1740. }
  1741. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1742. {
  1743. if ((ListType == ccltAll) || (ListType == ccltFile))
  1744. {
  1745. // special case is "diff"-style command that can be executed over any side,
  1746. // if we have both sides
  1747. Result =
  1748. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1749. // directly (instead of EnableSelectedOperation) to pass
  1750. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1751. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1752. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1753. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1754. }
  1755. else if (ListType == ccltBoth)
  1756. {
  1757. DebugAssert(!IsLocalBrowserMode());
  1758. Result = 1;
  1759. }
  1760. else
  1761. {
  1762. Result = -1;
  1763. }
  1764. }
  1765. else
  1766. {
  1767. if ((ListType == ccltAll) ||
  1768. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1769. {
  1770. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1771. }
  1772. else
  1773. {
  1774. Result = -1;
  1775. }
  1776. }
  1777. if ((Result > 0) &&
  1778. LocalCustomCommand.IsSessionCommand(Cmd) &&
  1779. !HasActiveTerminal())
  1780. {
  1781. Result = 0;
  1782. }
  1783. }
  1784. return Result;
  1785. }
  1786. //---------------------------------------------------------------------------
  1787. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1788. TStrings * FileList, const TCustomCommandType & ACommand,
  1789. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1790. {
  1791. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1792. {
  1793. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1794. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1795. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1796. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1797. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1798. bool Capture =
  1799. FLAGSET(ACommand.Params, ccShowResults) ||
  1800. FLAGSET(ACommand.Params, ccCopyResults) ||
  1801. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1802. TCaptureOutputEvent OutputEvent = NULL;
  1803. DebugAssert(FCapturedLog == NULL);
  1804. if (Capture)
  1805. {
  1806. FCapturedLog = new TStringList();
  1807. OutputEvent = TerminalCaptureLog;
  1808. }
  1809. try
  1810. {
  1811. if (!RemoteCustomCommand.IsFileCommand(Command))
  1812. {
  1813. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1814. OutputEvent);
  1815. }
  1816. else
  1817. {
  1818. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1819. }
  1820. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1821. {
  1822. if (FLAGSET(ACommand.Params, ccCopyResults))
  1823. {
  1824. CopyToClipboard(FCapturedLog);
  1825. }
  1826. if (FLAGSET(ACommand.Params, ccShowResults))
  1827. {
  1828. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1829. }
  1830. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1831. {
  1832. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1833. }
  1834. }
  1835. }
  1836. __finally
  1837. {
  1838. SAFE_DESTROY(FCapturedLog);
  1839. }
  1840. }
  1841. }
  1842. //---------------------------------------------------------------------------
  1843. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1844. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1845. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1846. {
  1847. DebugAssert(!IsLocalBrowserMode());
  1848. TStrings * LocalFileList = NULL;
  1849. TStrings * RemoteFileList = NULL;
  1850. TStrings * RemoteFileListFull = NULL;
  1851. try
  1852. {
  1853. if (LocalFileCommand)
  1854. {
  1855. if (ALocalFileList == NULL)
  1856. {
  1857. DebugAssert(HasDirView[osLocal]);
  1858. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1859. // directly (instead of EnableSelectedOperation) to pass
  1860. // false to FocusedFileOnlyWhenFocused
  1861. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1862. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1863. }
  1864. else
  1865. {
  1866. LocalFileList = ALocalFileList;
  1867. }
  1868. if (FileListCommand)
  1869. {
  1870. if (LocalFileList->Count != 1)
  1871. {
  1872. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1873. }
  1874. }
  1875. else
  1876. {
  1877. if ((LocalFileList->Count != 1) &&
  1878. (FileList->Count != 1) &&
  1879. (LocalFileList->Count != FileList->Count))
  1880. {
  1881. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1882. }
  1883. }
  1884. }
  1885. UnicodeString RootTempDir;
  1886. UnicodeString TempDir;
  1887. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1888. if (!RemoteFiles)
  1889. {
  1890. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1891. }
  1892. try
  1893. {
  1894. TDateTimes RemoteFileTimes;
  1895. if (RemoteFiles)
  1896. {
  1897. RemoteFileList = FileList;
  1898. }
  1899. else
  1900. {
  1901. RemoteFileList = new TStringList();
  1902. TMakeLocalFileListParams MakeFileListParam;
  1903. MakeFileListParam.FileList = RemoteFileList;
  1904. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1905. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1906. MakeFileListParam.Recursive =
  1907. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1908. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1909. if (!MakeFileListParam.Recursive)
  1910. {
  1911. RemoteFileTimes.clear();
  1912. RemoteFileListFull = new TStringList();
  1913. MakeFileListParam.FileList = RemoteFileListFull;
  1914. MakeFileListParam.Recursive = true;
  1915. MakeFileListParam.IncludeDirs = false;
  1916. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1917. }
  1918. else
  1919. {
  1920. RemoteFileListFull = RemoteFileList;
  1921. }
  1922. }
  1923. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1924. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1925. if (!NonBlocking)
  1926. {
  1927. int Count;
  1928. if (FileListCommand)
  1929. {
  1930. Count = 1;
  1931. }
  1932. else if (LocalFileCommand)
  1933. {
  1934. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  1935. }
  1936. else
  1937. {
  1938. Count = RemoteFileList->Count;
  1939. }
  1940. Progress.Start(foCustomCommand, osRemote, Count);
  1941. DebugAssert(FProgressForm != NULL);
  1942. FProgressForm->ReadOnly = true;
  1943. }
  1944. try
  1945. {
  1946. if (FileListCommand)
  1947. {
  1948. UnicodeString LocalFile;
  1949. // MakeFileList does not delimit filenames
  1950. UnicodeString FileList = MakeFileList(RemoteFileList);
  1951. if (LocalFileCommand)
  1952. {
  1953. DebugAssert(LocalFileList->Count == 1);
  1954. LocalFile = LocalFileList->Strings[0];
  1955. }
  1956. TLocalCustomCommand CustomCommand(Data,
  1957. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1958. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1959. if (NonBlocking)
  1960. {
  1961. DebugAssert(!POutput);
  1962. ExecuteShellChecked(ShellCommand);
  1963. }
  1964. else
  1965. {
  1966. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1967. }
  1968. }
  1969. else if (LocalFileCommand)
  1970. {
  1971. if (LocalFileList->Count == 1)
  1972. {
  1973. UnicodeString LocalFile = LocalFileList->Strings[0];
  1974. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1975. {
  1976. UnicodeString FileName = RemoteFileList->Strings[Index];
  1977. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1978. Progress.SetFile(FileNameForProgress);
  1979. TLocalCustomCommand CustomCommand(Data,
  1980. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  1981. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1982. TOnceDoneOperation OnceDoneOperation;
  1983. // Do not unselect anything, as it won't work with two panels anyway
  1984. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1985. }
  1986. }
  1987. else if (RemoteFileList->Count == 1)
  1988. {
  1989. UnicodeString FileName = RemoteFileList->Strings[0];
  1990. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1991. {
  1992. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1993. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1994. Progress.SetFile(LocalFileNameOnly);
  1995. TLocalCustomCommand CustomCommand(
  1996. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1997. FileName, LocalFileName, L"");
  1998. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1999. TOnceDoneOperation OnceDoneOperation;
  2000. // Do not unselect anything, as it won't work with two panels anyway
  2001. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2002. }
  2003. }
  2004. else
  2005. {
  2006. if (LocalFileList->Count != RemoteFileList->Count)
  2007. {
  2008. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  2009. }
  2010. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2011. {
  2012. UnicodeString FileName = RemoteFileList->Strings[Index];
  2013. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  2014. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  2015. Progress.SetFile(LocalFileNameOnly); // sic
  2016. TLocalCustomCommand CustomCommand(
  2017. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2018. FileName, LocalFileName, L"");
  2019. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2020. TOnceDoneOperation OnceDoneOperation;
  2021. // Do not unselect anything, as it won't work with two panels anyway
  2022. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  2023. }
  2024. }
  2025. }
  2026. else
  2027. {
  2028. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  2029. {
  2030. UnicodeString FileName = RemoteFileList->Strings[Index];
  2031. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  2032. Progress.SetFile(FileNameForProgress);
  2033. TLocalCustomCommand CustomCommand(Data,
  2034. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2035. FileName, L"", L"");
  2036. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2037. TOnceDoneOperation OnceDoneOperation;
  2038. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  2039. }
  2040. }
  2041. }
  2042. __finally
  2043. {
  2044. if (!NonBlocking)
  2045. {
  2046. Progress.Stop();
  2047. }
  2048. }
  2049. DebugAssert(!FAutoOperation);
  2050. if (!RemoteFiles)
  2051. {
  2052. TempDir = IncludeTrailingBackslash(TempDir);
  2053. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  2054. {
  2055. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  2056. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  2057. // Skip directories, process only nested files.
  2058. // The check is redundant as FileAge fails for directories anyway.
  2059. !DirectoryExists(FileName))
  2060. {
  2061. UnicodeString RemoteDir =
  2062. UnixExtractFileDir(
  2063. UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
  2064. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2065. TDateTime NewTime;
  2066. if (FileAge(FileName, NewTime) &&
  2067. (NewTime != RemoteFileTimes[Index]))
  2068. {
  2069. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2070. TemporaryFileCopyParam(CopyParam);
  2071. CopyParam.FileMask = L"";
  2072. FAutoOperation = true;
  2073. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2074. TemporaryFilesList->Add(FileName);
  2075. Terminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2076. }
  2077. }
  2078. }
  2079. }
  2080. }
  2081. __finally
  2082. {
  2083. FAutoOperation = false;
  2084. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2085. {
  2086. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2087. }
  2088. }
  2089. }
  2090. __finally
  2091. {
  2092. if (RemoteFileList != FileList)
  2093. {
  2094. delete RemoteFileList;
  2095. }
  2096. if (LocalFileList != ALocalFileList)
  2097. {
  2098. delete LocalFileList;
  2099. }
  2100. if (RemoteFileListFull != RemoteFileList)
  2101. {
  2102. delete RemoteFileListFull;
  2103. }
  2104. }
  2105. }
  2106. //---------------------------------------------------------------------------
  2107. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2108. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2109. bool FileListCommand, UnicodeString * POutput)
  2110. {
  2111. std::unique_ptr<TStrings> SelectedFileList(DirView(osCurrent)->CreateFileList(false, true, NULL));
  2112. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2113. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2114. {
  2115. UnicodeString FileName = SelectedFileList->Strings[Index];
  2116. if (DirectoryExists(FileName))
  2117. {
  2118. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2119. {
  2120. LocalFileList->Add(FileName);
  2121. }
  2122. if (FLAGSET(ACommand.Params, ccRecursive))
  2123. {
  2124. TMakeLocalFileListParams MakeFileListParam;
  2125. MakeFileListParam.FileList = LocalFileList.get();
  2126. MakeFileListParam.FileTimes = NULL;
  2127. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2128. MakeFileListParam.Recursive = true;
  2129. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2130. }
  2131. }
  2132. else
  2133. {
  2134. LocalFileList->Add(FileName);
  2135. }
  2136. }
  2137. UnicodeString RemotePath;
  2138. if (Terminal != NULL)
  2139. {
  2140. RemotePath = Terminal->CurrentDirectory;
  2141. }
  2142. if (FileListCommand)
  2143. {
  2144. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2145. TLocalCustomCommand CustomCommand(
  2146. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2147. L"", L"", FileList);
  2148. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2149. }
  2150. else
  2151. {
  2152. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2153. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2154. DebugAssert(FProgressForm != NULL);
  2155. FProgressForm->ReadOnly = true;
  2156. try
  2157. {
  2158. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2159. {
  2160. UnicodeString FileName = LocalFileList->Strings[Index];
  2161. Progress.SetFile(FileName);
  2162. TLocalCustomCommand CustomCommand(
  2163. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2164. FileName, L"", L"");
  2165. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2166. TOnceDoneOperation OnceDoneOperation;
  2167. Progress.Finish(FileName, true, OnceDoneOperation);
  2168. }
  2169. }
  2170. __finally
  2171. {
  2172. Progress.Stop();
  2173. }
  2174. }
  2175. }
  2176. //---------------------------------------------------------------------------
  2177. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2178. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2179. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2180. {
  2181. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2182. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2183. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2184. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2185. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2186. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2187. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2188. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2189. std::unique_ptr<UnicodeString> POutput;
  2190. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2191. FLAGSET(ACommand.Params, ccCopyResults))
  2192. {
  2193. POutput.reset(new UnicodeString());
  2194. }
  2195. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  2196. FProgressSide = FCurrentSide;
  2197. if (!LocalCustomCommand.IsFileCommand(Command))
  2198. {
  2199. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2200. }
  2201. // remote files?
  2202. else if (!IsSideLocalBrowser(FCurrentSide) || LocalFileCommand)
  2203. {
  2204. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2205. }
  2206. // local files
  2207. else
  2208. {
  2209. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2210. }
  2211. if (POutput.get() != NULL)
  2212. {
  2213. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2214. int P = POutput->Pos(sLineBreak);
  2215. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2216. {
  2217. POutput->SetLength(P - 1);
  2218. }
  2219. // Copy even empty output to clipboard
  2220. if (FLAGSET(ACommand.Params, ccCopyResults))
  2221. {
  2222. CopyToClipboard(*POutput);
  2223. }
  2224. // But do not show an empty message box.
  2225. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2226. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2227. !POutput->IsEmpty())
  2228. {
  2229. TClipboardHandler ClipboardHandler;
  2230. ClipboardHandler.Text = *POutput;
  2231. TMessageParams Params;
  2232. TQueryButtonAlias Aliases[1];
  2233. Aliases[0].Button = qaRetry;
  2234. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2235. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2236. Params.Aliases = Aliases;
  2237. Params.AliasesCount = LENOF(Aliases);
  2238. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2239. }
  2240. }
  2241. }
  2242. //---------------------------------------------------------------------------
  2243. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2244. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2245. {
  2246. TCustomCommandData Data;
  2247. UnicodeString Site;
  2248. UnicodeString RemotePath;
  2249. if (HasActiveTerminal())
  2250. {
  2251. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  2252. Data = TCustomCommandData(SessionData.get());
  2253. Site = Terminal->SessionData->SessionKey;
  2254. RemotePath = Terminal->CurrentDirectory;
  2255. }
  2256. UnicodeString HelpKeyword = ACommand.HomePage;
  2257. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2258. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2259. {
  2260. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2261. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2262. {
  2263. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2264. }
  2265. else
  2266. {
  2267. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2268. }
  2269. if (!DoCustomCommandOptionsDialog(
  2270. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2271. {
  2272. Abort();
  2273. }
  2274. }
  2275. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2276. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2277. {
  2278. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2279. }
  2280. else
  2281. {
  2282. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2283. }
  2284. }
  2285. //---------------------------------------------------------------------------
  2286. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2287. const TCustomCommandType & Command)
  2288. {
  2289. DebugAssert(FCustomCommandLocalFileList != NULL);
  2290. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2291. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2292. TStrings * LocalFileList = new TStringList();
  2293. TStrings * RemoteFileList = new TStringList();
  2294. try
  2295. {
  2296. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2297. {
  2298. LocalFileList->Clear();
  2299. LocalFileList->AddObject(
  2300. FCustomCommandLocalFileList->Strings[Index],
  2301. FCustomCommandLocalFileList->Objects[Index]);
  2302. RemoteFileList->Clear();
  2303. RemoteFileList->AddObject(
  2304. FCustomCommandRemoteFileList->Strings[Index],
  2305. FCustomCommandRemoteFileList->Objects[Index]);
  2306. CustomCommand(RemoteFileList, Command, LocalFileList);
  2307. }
  2308. }
  2309. __finally
  2310. {
  2311. delete LocalFileList;
  2312. delete RemoteFileList;
  2313. }
  2314. }
  2315. //---------------------------------------------------------------------------
  2316. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2317. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2318. {
  2319. delete FCustomCommandLocalFileList;
  2320. delete FCustomCommandRemoteFileList;
  2321. // takeover ownership,
  2322. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2323. FCustomCommandLocalFileList = LocalFileList;
  2324. FCustomCommandRemoteFileList = RemoteFileList;
  2325. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2326. if (Button != NULL)
  2327. {
  2328. FCustomCommandMenu->Items->Clear();
  2329. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2330. MenuPopup(FCustomCommandMenu, Button);
  2331. }
  2332. else
  2333. {
  2334. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2335. }
  2336. }
  2337. //---------------------------------------------------------------------------
  2338. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2339. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2340. {
  2341. DebugAssert(FCapturedLog != NULL);
  2342. if ((OutputType == cotOutput) || (OutputType == cotError))
  2343. {
  2344. FCapturedLog->Add(AddedLine);
  2345. }
  2346. }
  2347. //---------------------------------------------------------------------------
  2348. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2349. TOperationSide Side)
  2350. {
  2351. return
  2352. (Side == osOther) &&
  2353. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2354. }
  2355. //---------------------------------------------------------------------------
  2356. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2357. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2358. {
  2359. TTBItemOptions O;
  2360. O = Item->Options;
  2361. TCustomDirView * DView = DirView(Side);
  2362. if ((DView->ItemFocused != NULL) &&
  2363. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2364. // when resolving links is disabled, default action is to enter the directory,
  2365. // no matter what DoubleClickAction is configured to
  2366. (IsSideLocalBrowser(Side) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2367. // Can only Edit files, but can Open/Copy even directories
  2368. ((DoubleClickAction != dcaEdit) ||
  2369. !DView->ItemIsDirectory(DView->ItemFocused)))
  2370. {
  2371. Item->Options = O << tboDefault;
  2372. }
  2373. else
  2374. {
  2375. Item->Options = O >> tboDefault;
  2376. }
  2377. }
  2378. //---------------------------------------------------------------------------
  2379. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2380. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2381. {
  2382. TCustomDirView * DView = DirView(Side);
  2383. TListItem * Item = DView->ItemFocused;
  2384. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2385. EnableFocusedOperation[Side])
  2386. {
  2387. TPoint ClientPoint;
  2388. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2389. TPoint(0, 0) : MousePos;
  2390. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2391. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2392. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2393. }
  2394. }
  2395. //---------------------------------------------------------------------------
  2396. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2397. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2398. {
  2399. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2400. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2401. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2402. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2403. Handled = true;
  2404. }
  2405. //---------------------------------------------------------------------------
  2406. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2407. {
  2408. }
  2409. //---------------------------------------------------------------------------
  2410. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2411. {
  2412. DebugAssert(FErrorList == NULL);
  2413. if (WinConfiguration->ContinueOnError)
  2414. {
  2415. FErrorList = new TStringList();
  2416. Configuration->Usage->Inc(L"ContinuationsOnError");
  2417. }
  2418. NonVisualDataModule->StartBusy();
  2419. }
  2420. //---------------------------------------------------------------------------
  2421. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2422. {
  2423. NonVisualDataModule->EndBusy();
  2424. if (FErrorList)
  2425. {
  2426. HandleErrorList(FErrorList);
  2427. }
  2428. }
  2429. //---------------------------------------------------------------------------
  2430. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2431. const TCopyParamType & CopyParam)
  2432. {
  2433. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2434. {
  2435. Configuration->Usage->Inc(L"FileMaskUses");
  2436. }
  2437. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2438. {
  2439. Configuration->Usage->Inc(L"OperationMaskUses");
  2440. }
  2441. }
  2442. //---------------------------------------------------------------------------
  2443. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2444. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2445. {
  2446. DebugAssert(!IsLocalBrowserMode());
  2447. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2448. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2449. TTransferOperationParam DefaultParam;
  2450. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2451. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2452. switch (Param.Queue)
  2453. {
  2454. case asOn:
  2455. CopyParam.Queue = true;
  2456. break;
  2457. case asOff:
  2458. CopyParam.Queue = false;
  2459. break;
  2460. case asAuto:
  2461. default:
  2462. // keep default
  2463. break;
  2464. }
  2465. bool Result =
  2466. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2467. if (Result)
  2468. {
  2469. DebugAssert(Terminal);
  2470. bool SelectionRestored = false;
  2471. TCustomDirView * DView = NULL;
  2472. if (HasDirView[Side])
  2473. {
  2474. DView = DirView(Side);
  2475. DView->SaveSelection();
  2476. DView->SaveSelectedNames();
  2477. }
  2478. UpdateCopyParamCounters(CopyParam);
  2479. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2480. DebugAssert(FTransferResumeList == NULL);
  2481. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2482. FMoveToQueue = false;
  2483. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2484. try
  2485. {
  2486. TStrings * PermanentFileList;
  2487. std::unique_ptr<TStrings> PermanentFileListOwner;
  2488. try
  2489. {
  2490. if (Side == osLocal)
  2491. {
  2492. PermanentFileList = FileList;
  2493. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2494. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2495. if (Operation == foMove)
  2496. {
  2497. ReloadLocalDirectory();
  2498. if (DView != NULL)
  2499. {
  2500. DView->RestoreSelection();
  2501. }
  2502. SelectionRestored = true;
  2503. }
  2504. }
  2505. else
  2506. {
  2507. // Clone the file list as it may refer to current directory files,
  2508. // which get destroyed, when the source directory is reloaded after foMove operation.
  2509. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2510. // But for a hotfix, we are not going to do such a big change.
  2511. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2512. PermanentFileList = PermanentFileListOwner.get();
  2513. try
  2514. {
  2515. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2516. }
  2517. __finally
  2518. {
  2519. if (Operation == foMove)
  2520. {
  2521. if (DView != NULL)
  2522. {
  2523. DView->RestoreSelection();
  2524. }
  2525. SelectionRestored = true;
  2526. }
  2527. ReloadLocalDirectory(Param.TargetDirectory);
  2528. }
  2529. }
  2530. }
  2531. catch (EAbort &)
  2532. {
  2533. if (FMoveToQueue)
  2534. {
  2535. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2536. DebugAssert(CopyParam.TransferSkipList == NULL);
  2537. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2538. if (TransferResumeList->Count > 0)
  2539. {
  2540. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2541. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2542. }
  2543. CopyParam.TransferSkipList = TransferResumeList.release();
  2544. // not really needed, just to keep it consistent with TransferResumeList
  2545. FTransferResumeList = NULL;
  2546. FMoveToQueue = false;
  2547. Configuration->Usage->Inc("MovesToBackground");
  2548. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2549. ClearTransferSourceSelection(Direction);
  2550. }
  2551. throw;
  2552. }
  2553. }
  2554. __finally
  2555. {
  2556. if (!SelectionRestored && (DView != NULL))
  2557. {
  2558. DView->DiscardSavedSelection();
  2559. }
  2560. FTransferResumeList = NULL;
  2561. }
  2562. }
  2563. return Result;
  2564. }
  2565. //---------------------------------------------------------------------------
  2566. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2567. TOperationSide Side, TStrings * FileList, void * Param)
  2568. {
  2569. DebugAssert(FileList->Count);
  2570. // We deliberately do not toggle alternative flag (Param), but use OR,
  2571. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2572. // shortcut of CurrentDeleteAlternativeAction
  2573. bool Alternative =
  2574. bool(Param) || UseAlternativeFunction();
  2575. bool Recycle;
  2576. if (IsSideLocalBrowser(Side))
  2577. {
  2578. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2579. }
  2580. else
  2581. {
  2582. Recycle =
  2583. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2584. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2585. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2586. }
  2587. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2588. if (!Result)
  2589. {
  2590. UnicodeString Query;
  2591. if (FileList->Count == 1)
  2592. {
  2593. if (IsSideLocalBrowser(Side))
  2594. {
  2595. Query = ExtractFileName(FileList->Strings[0]);
  2596. }
  2597. else
  2598. {
  2599. Query = UnixExtractFileName(FileList->Strings[0]);
  2600. }
  2601. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2602. }
  2603. else
  2604. {
  2605. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2606. }
  2607. TMessageParams Params(mpNeverAskAgainCheck);
  2608. Params.ImageName = L"Delete file";
  2609. unsigned int Answer =
  2610. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2611. if (Answer == qaNeverAskAgain)
  2612. {
  2613. Result = true;
  2614. if (Recycle)
  2615. {
  2616. WinConfiguration->ConfirmRecycling = false;
  2617. }
  2618. else
  2619. {
  2620. WinConfiguration->ConfirmDeleting = false;
  2621. }
  2622. }
  2623. else
  2624. {
  2625. Result = (Answer == qaOK);
  2626. }
  2627. }
  2628. if (Result)
  2629. {
  2630. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2631. }
  2632. return Result;
  2633. }
  2634. //---------------------------------------------------------------------------
  2635. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2636. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2637. {
  2638. TAutoBatch AutoBatch(this);
  2639. bool Result;
  2640. if ((Operation == foCopy) || (Operation == foMove))
  2641. {
  2642. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2643. }
  2644. else if (Operation == foRename)
  2645. {
  2646. DebugAssert(DirView(Side)->ItemFocused);
  2647. DirView(Side)->ItemFocused->EditCaption();
  2648. Result = true;
  2649. }
  2650. else if (Operation == foDelete)
  2651. {
  2652. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2653. }
  2654. else if (Operation == foSetProperties)
  2655. {
  2656. DirView(osRemote)->SaveSelectedNames();
  2657. Result = SetProperties(Side, FileList);
  2658. }
  2659. else if (Operation == foCustomCommand)
  2660. {
  2661. DebugAssert(Param);
  2662. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2663. DirView(osRemote)->SaveSelectedNames();
  2664. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2665. CustomCommand(FileList, *Command, NULL);
  2666. Result = true;
  2667. }
  2668. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2669. {
  2670. DebugAssert(!IsSideLocalBrowser(Side));
  2671. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2672. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2673. }
  2674. else if (Operation == foLock)
  2675. {
  2676. DebugAssert(!IsSideLocalBrowser(Side));
  2677. LockFiles(FileList, true);
  2678. Result = true;
  2679. }
  2680. else if (Operation == foUnlock)
  2681. {
  2682. DebugAssert(!IsSideLocalBrowser(Side));
  2683. LockFiles(FileList, false);
  2684. Result = true;
  2685. }
  2686. else
  2687. {
  2688. DebugFail();
  2689. }
  2690. return Result;
  2691. }
  2692. //---------------------------------------------------------------------------
  2693. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2694. {
  2695. if (Side == osCurrent)
  2696. {
  2697. Side = FCurrentSide;
  2698. }
  2699. return Side;
  2700. }
  2701. //---------------------------------------------------------------------------
  2702. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2703. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2704. {
  2705. Side = GetSide(Side);
  2706. bool Result;
  2707. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, IsSideLocalBrowser(Side), NULL);
  2708. try
  2709. {
  2710. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2711. }
  2712. __finally
  2713. {
  2714. delete FileList;
  2715. }
  2716. return Result;
  2717. }
  2718. //---------------------------------------------------------------------------
  2719. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2720. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2721. bool NoConfirmation, void * Param)
  2722. {
  2723. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2724. {
  2725. if ((Operation == foCopy) || (Operation == foMove))
  2726. {
  2727. DebugAssert(!IsLocalBrowserMode());
  2728. if (GetSide(Side) == osLocal)
  2729. {
  2730. Configuration->Usage->Inc(L"UploadsCommand");
  2731. }
  2732. else
  2733. {
  2734. Configuration->Usage->Inc(L"DownloadsCommand");
  2735. }
  2736. }
  2737. }
  2738. }
  2739. //---------------------------------------------------------------------------
  2740. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2741. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2742. {
  2743. TTransferOperationParam Param;
  2744. if ((WinConfiguration->Interface != ifCommander) ||
  2745. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2746. {
  2747. Flags &= ~cocShortCutHint;
  2748. }
  2749. TCustomDirView * DView = DirView(Side);
  2750. Param.Options =
  2751. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2752. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2753. if (FLAGSET(Flags, cocQueue))
  2754. {
  2755. Param.Queue = asOn;
  2756. }
  2757. else if (FLAGSET(Flags, cocNonQueue))
  2758. {
  2759. Param.Queue = asOff;
  2760. }
  2761. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2762. }
  2763. //---------------------------------------------------------------------------
  2764. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2765. {
  2766. try
  2767. {
  2768. if (ErrorList->Count)
  2769. {
  2770. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2771. if (MessageDialog(Message, qtError,
  2772. qaOK | qaCancel, HELP_NONE) == qaOK)
  2773. {
  2774. unsigned int Answer;
  2775. int Index = 0;
  2776. do
  2777. {
  2778. DebugAssert(Index >= 0 && Index < ErrorList->Count);
  2779. TQueryButtonAlias Aliases[2];
  2780. Aliases[0].Button = qaYes;
  2781. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2782. Aliases[1].Button = qaNo;
  2783. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2784. TMessageParams Params;
  2785. Params.Aliases = Aliases;
  2786. Params.AliasesCount = LENOF(Aliases);
  2787. Answer = MoreMessageDialog(
  2788. FMTLOAD(ERROR_LIST_NUMBER, (Index+1, ErrorList->Count, ErrorList->Strings[Index])),
  2789. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2790. (Index ? qaYes : 0) | (Index < ErrorList->Count - 1 ? qaNo : 0) |
  2791. qaOK, HELP_NONE, &Params);
  2792. if (Answer == qaNo)
  2793. {
  2794. Index++;
  2795. }
  2796. if (Answer == qaYes)
  2797. {
  2798. Index--;
  2799. }
  2800. }
  2801. while (Answer != qaOK);
  2802. }
  2803. }
  2804. }
  2805. __finally
  2806. {
  2807. TStrings * List = ErrorList;
  2808. ErrorList = NULL;
  2809. for (int i = 0; i < List->Count; i++)
  2810. {
  2811. delete List->Objects[i];
  2812. }
  2813. delete List;
  2814. }
  2815. }
  2816. //---------------------------------------------------------------------------
  2817. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2818. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2819. {
  2820. // needed for checking filemasks, as there's no directory object
  2821. // associated with the file object
  2822. File->FullFileName = FullFileName;
  2823. TFileMasks::TParams MaskParams;
  2824. MaskParams.Size = File->Size;
  2825. MaskParams.Modification = File->Modification;
  2826. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2827. }
  2828. //---------------------------------------------------------------------------
  2829. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2830. {
  2831. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2832. Side = GetSide(Side);
  2833. TCustomDirView * DView = DirView(Side);
  2834. TListItem * FocusedItem = DView->ItemFocused;
  2835. UnicodeString Name;
  2836. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2837. {
  2838. Name = DView->ItemFileName(FocusedItem);
  2839. }
  2840. else
  2841. {
  2842. Name = LoadStr(NEW_FILE);
  2843. }
  2844. UnicodeString Names = Name;
  2845. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2846. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2847. HELP_EDIT_NEW, History.get(), true, NULL, true, 400))
  2848. {
  2849. while (!Names.IsEmpty())
  2850. {
  2851. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2852. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2853. UnicodeString TargetFileName;
  2854. UnicodeString LocalFileName;
  2855. UnicodeString RootTempDir;
  2856. UnicodeString TempDir;
  2857. UnicodeString RemoteDirectory;
  2858. bool ExistingFile = false;
  2859. if (!IsSideLocalBrowser(Side))
  2860. {
  2861. Name = AbsolutePath(Terminal->CurrentDirectory, Name);
  2862. TRemoteFile * File = NULL;
  2863. if (Terminal->FileExists(Name, &File) &&
  2864. !File->IsDirectory)
  2865. {
  2866. try
  2867. {
  2868. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2869. ExistingFile = true;
  2870. }
  2871. __finally
  2872. {
  2873. delete File;
  2874. }
  2875. }
  2876. if (!ExistingFile)
  2877. {
  2878. RemoteDirectory = UnixExtractFilePath(Name);
  2879. TemporaryDirectoryForRemoteFiles(
  2880. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2881. TargetFileName = UnixExtractFileName(Name);
  2882. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2883. LocalFileName = TempDir +
  2884. // We probably do not want to trim the VMS version here
  2885. Terminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2886. }
  2887. }
  2888. else
  2889. {
  2890. if (ExtractFilePath(Name).IsEmpty())
  2891. {
  2892. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2893. }
  2894. else
  2895. {
  2896. LocalFileName = ExpandFileName(Name);
  2897. }
  2898. TargetFileName = ExtractFileName(Name);
  2899. }
  2900. if (!ExistingFile)
  2901. {
  2902. bool NewFile = !FileExists(ApiPath(LocalFileName));
  2903. if (NewFile)
  2904. {
  2905. int File = FileCreate(ApiPath(LocalFileName));
  2906. if (File < 0)
  2907. {
  2908. if (!RootTempDir.IsEmpty())
  2909. {
  2910. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2911. }
  2912. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2913. }
  2914. else
  2915. {
  2916. FileClose(File);
  2917. }
  2918. }
  2919. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2920. const TEditorData * ExternalEditor = NULL;
  2921. TFileMasks::TParams MaskParams; // size not known
  2922. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2923. false, MaskParams);
  2924. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2925. ExternalEditor, RootTempDir, RemoteDirectory, NewFile);
  2926. }
  2927. }
  2928. }
  2929. }
  2930. //---------------------------------------------------------------------------
  2931. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2932. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2933. {
  2934. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2935. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2936. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2937. return
  2938. ((ExecuteFileBy == efInternalEditor)) ||
  2939. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2940. }
  2941. //---------------------------------------------------------------------------
  2942. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2943. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2944. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2945. UnicodeString RemoteDirectory, bool NewFile)
  2946. {
  2947. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2948. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2949. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2950. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2951. Side = GetSide(Side);
  2952. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2953. if (!IsSideLocalBrowser(Side))
  2954. {
  2955. Data->Terminal = Terminal;
  2956. Data->Queue = Queue;
  2957. Data->SessionData = CloneCurrentSessionData();
  2958. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2959. Data->RemoteDirectory = RemoteDirectory;
  2960. Data->SessionName = Terminal->SessionData->SessionName;
  2961. Data->LocalRootDirectory = LocalRootDirectory;
  2962. Data->OriginalFileName = OriginalFileName;
  2963. Data->Command = L""; // will be changed later for external editor
  2964. }
  2965. if (ExecuteFileBy == efInternalEditor)
  2966. {
  2967. if (!IsSideLocalBrowser(Side))
  2968. {
  2969. UnicodeString Caption = UnixIncludeTrailingBackslash(RemoteDirectory) + OriginalFileName +
  2970. L" - " + Terminal->SessionData->SessionName;
  2971. TForm * Editor;
  2972. try
  2973. {
  2974. TNotifyEvent OnFileChanged = NULL;
  2975. TNotifyEvent OnSaveAll = NULL;
  2976. TAnyModifiedEvent OnAnyModified = NULL;
  2977. // Edited files are uploaded in background queue, what we do not support with encrypted files
  2978. if (Terminal->IsCapable[fcBackgroundTransfers])
  2979. {
  2980. OnFileChanged = FEditorManager->FileChanged;
  2981. OnSaveAll = SaveAllInternalEditors;
  2982. OnAnyModified = AnyInternalEditorModified;
  2983. }
  2984. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  2985. FEditorManager->FileReload, FEditorManager->FileClosed,
  2986. OnSaveAll, OnAnyModified,
  2987. Caption, FStandaloneEditing, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  2988. }
  2989. catch(...)
  2990. {
  2991. if (!LocalRootDirectory.IsEmpty())
  2992. {
  2993. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  2994. }
  2995. throw;
  2996. }
  2997. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  2998. }
  2999. else
  3000. {
  3001. DebugAssert(!FStandaloneEditing);
  3002. TForm * Editor =
  3003. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  3004. SaveAllInternalEditors, AnyInternalEditorModified,
  3005. L"", false, SessionColor, -1, NewFile);
  3006. FLocalEditors->Add(Editor);
  3007. }
  3008. }
  3009. else
  3010. {
  3011. HANDLE Process;
  3012. if (ExecuteFileBy == efExternalEditor)
  3013. {
  3014. UnicodeString Program, Params, Dir;
  3015. Data->Command = ExternalEditor->ExternalEditor;
  3016. ReformatFileNameCommand(Data->Command);
  3017. SplitCommand(Data->Command, Program, Params, Dir);
  3018. Params = ExpandFileNameCommand(Params, FileName);
  3019. Program = ExpandEnvironmentVariables(Program);
  3020. if (!ExecuteShell(Program, Params, Process))
  3021. {
  3022. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  3023. }
  3024. }
  3025. else
  3026. {
  3027. DebugAssert(!IsSideLocalBrowser(Side));
  3028. if (!ExecuteShell(FileName, L"", Process))
  3029. {
  3030. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  3031. }
  3032. }
  3033. if (IsSideLocalBrowser(Side) ||
  3034. ((ExecuteFileBy == efShell) &&
  3035. !WinConfiguration->Editor.SDIShellEditor) ||
  3036. ((ExecuteFileBy == efExternalEditor) &&
  3037. !ExternalEditor->SDIExternalEditor))
  3038. {
  3039. // no need for handle
  3040. if (Process != NULL)
  3041. {
  3042. DebugCheck(CloseHandle(Process));
  3043. }
  3044. Process = INVALID_HANDLE_VALUE;
  3045. }
  3046. else
  3047. {
  3048. if (Process == NULL)
  3049. {
  3050. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  3051. }
  3052. }
  3053. if (!IsSideLocalBrowser(Side))
  3054. {
  3055. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  3056. }
  3057. }
  3058. }
  3059. //---------------------------------------------------------------------------
  3060. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  3061. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3062. void * /*Arg*/)
  3063. {
  3064. if (Token != NULL)
  3065. {
  3066. EditorFormFileSave(static_cast<TForm *>(Token));
  3067. }
  3068. }
  3069. //---------------------------------------------------------------------------
  3070. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3071. {
  3072. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3073. {
  3074. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3075. }
  3076. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3077. }
  3078. //---------------------------------------------------------------------------
  3079. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3080. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3081. void * Arg)
  3082. {
  3083. if ((Token != NULL) &&
  3084. IsEditorFormModified(static_cast<TForm *>(Token)))
  3085. {
  3086. *static_cast<bool *>(Arg) = true;
  3087. }
  3088. }
  3089. //---------------------------------------------------------------------------
  3090. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3091. TObject * /*Sender*/, bool & Modified)
  3092. {
  3093. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3094. {
  3095. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3096. {
  3097. Modified = true;
  3098. }
  3099. }
  3100. if (!Modified)
  3101. {
  3102. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3103. }
  3104. }
  3105. //---------------------------------------------------------------------------
  3106. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3107. {
  3108. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3109. }
  3110. //---------------------------------------------------------------------------
  3111. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3112. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3113. UnicodeString & Result, UnicodeString & RootDirectory)
  3114. {
  3115. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3116. {
  3117. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3118. Result = RootDirectory;
  3119. }
  3120. else
  3121. {
  3122. RootDirectory = L"";
  3123. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3124. Result = IncludeTrailingBackslash(Result);
  3125. }
  3126. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3127. {
  3128. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3129. }
  3130. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3131. {
  3132. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3133. {
  3134. RemoteDirectory.Delete(1, 1);
  3135. }
  3136. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3137. }
  3138. if (!ForceDirectories(ApiPath(Result)))
  3139. {
  3140. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3141. }
  3142. }
  3143. //---------------------------------------------------------------------------
  3144. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3145. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3146. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  3147. {
  3148. DebugAssert(!IsLocalBrowserMode());
  3149. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3150. if (ForceText)
  3151. {
  3152. CopyParam.TransferMode = tmAscii;
  3153. }
  3154. TemporaryFileCopyParam(CopyParam);
  3155. if (AllFiles)
  3156. {
  3157. CopyParam.IncludeFileMask = TFileMasks();
  3158. }
  3159. if (TempDir.IsEmpty())
  3160. {
  3161. TemporaryDirectoryForRemoteFiles(Terminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3162. }
  3163. DebugAssert(!FAutoOperation);
  3164. FAutoOperation = AutoOperation;
  3165. Terminal->ExceptionOnFail = true;
  3166. try
  3167. {
  3168. try
  3169. {
  3170. // turn off confirmations, as for MDI editors we may possibly download
  3171. // the same file over
  3172. Terminal->CopyToLocal(
  3173. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3174. if (GetTargetNames)
  3175. {
  3176. for (int i = 0; i < FileList->Count; i++)
  3177. {
  3178. FileList->Strings[i] =
  3179. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3180. }
  3181. }
  3182. }
  3183. catch(...)
  3184. {
  3185. if (!RootTempDir.IsEmpty())
  3186. {
  3187. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3188. }
  3189. throw;
  3190. }
  3191. }
  3192. __finally
  3193. {
  3194. FAutoOperation = false;
  3195. Terminal->ExceptionOnFail = false;
  3196. }
  3197. }
  3198. //---------------------------------------------------------------------------
  3199. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3200. {
  3201. // Do not waste time checking for default editor list the next time,
  3202. // or testing if default editor is not notepad.
  3203. bool TryNextTime = false;
  3204. UnicodeString UsageState;
  3205. if (!WinConfiguration->EditorList->IsDefaultList())
  3206. {
  3207. UsageState = "H";
  3208. }
  3209. else
  3210. {
  3211. UnicodeString Executable;
  3212. UnicodeString ExecutableDescription;
  3213. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3214. {
  3215. UnicodeString Message =
  3216. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3217. unsigned int Answer =
  3218. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3219. if (Answer != qaOK)
  3220. {
  3221. UsageState = "R";
  3222. }
  3223. else
  3224. {
  3225. UsageState = "A";
  3226. TEditorData EditorData;
  3227. EditorData.Editor = edExternal;
  3228. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3229. EditorData.ExternalEditorOptionsAutodetect();
  3230. TEditorList EditorList;
  3231. EditorList = *WinConfiguration->EditorList;
  3232. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3233. WinConfiguration->EditorList = &EditorList;
  3234. }
  3235. }
  3236. }
  3237. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3238. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3239. }
  3240. //---------------------------------------------------------------------------
  3241. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3242. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3243. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3244. {
  3245. if (ExecuteFileBy == efDefaultEditor)
  3246. {
  3247. if (!WinConfiguration->OfferedEditorAutoConfig)
  3248. {
  3249. EditorAutoConfig();
  3250. }
  3251. const TEditorPreferences * Editor =
  3252. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3253. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3254. {
  3255. ExecuteFileBy = efInternalEditor;
  3256. ExternalEditor = NULL;
  3257. }
  3258. else if (Editor->Data->Editor == edOpen)
  3259. {
  3260. ExecuteFileBy = efShell;
  3261. ExternalEditor = NULL;
  3262. }
  3263. else
  3264. {
  3265. ExecuteFileBy = efExternalEditor;
  3266. ExternalEditor = Editor->Data;
  3267. }
  3268. }
  3269. }
  3270. //---------------------------------------------------------------------------
  3271. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3272. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3273. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3274. {
  3275. UnicodeString OriginalFileName;
  3276. UnicodeString LocalRootDirectory;
  3277. UnicodeString RemoteDirectory;
  3278. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3279. IsSideLocalBrowser(Side), MaskParams);
  3280. UnicodeString Counter;
  3281. UnicodeString LocalFileName;
  3282. bool Handled = false;
  3283. if (!IsSideLocalBrowser(Side))
  3284. {
  3285. // We need to trim VMS version here, so that we use name without version
  3286. // when uploading back to create a new version of the file
  3287. OriginalFileName = Terminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3288. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3289. TObject * Token = NULL;
  3290. UnicodeString LocalDirectory;
  3291. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3292. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3293. LocalDirectory))
  3294. {
  3295. if (Token != NULL)
  3296. {
  3297. TForm * Form = dynamic_cast<TForm *>(Token);
  3298. if (Form->WindowState == wsMinimized)
  3299. {
  3300. ShowWindow(Form->Handle, SW_RESTORE);
  3301. }
  3302. else
  3303. {
  3304. Form->SetFocus();
  3305. }
  3306. Handled = true;
  3307. }
  3308. else
  3309. {
  3310. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3311. }
  3312. }
  3313. if (!Handled)
  3314. {
  3315. TStringList * FileList1 = new TStringList();
  3316. try
  3317. {
  3318. FileList1->AddObject(FullFileName, Object);
  3319. TemporarilyDownloadFiles(FileList1,
  3320. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3321. LocalRootDirectory, LocalDirectory, true, true, true);
  3322. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3323. }
  3324. __finally
  3325. {
  3326. delete FileList1;
  3327. }
  3328. switch (ExecuteFileBy)
  3329. {
  3330. case efShell:
  3331. Counter = "RemoteFilesExecuted";
  3332. break;
  3333. case efInternalEditor:
  3334. Counter = "RemoteFilesOpenedInInternalEditor";
  3335. break;
  3336. case efExternalEditor:
  3337. Counter = "RemoteFilesOpenedInExternalEditor";
  3338. break;
  3339. default:
  3340. DebugFail();
  3341. }
  3342. }
  3343. }
  3344. else
  3345. {
  3346. LocalFileName = FullFileName;
  3347. OriginalFileName = ExtractFileName(FullFileName);
  3348. switch (ExecuteFileBy)
  3349. {
  3350. case efShell:
  3351. Counter = "LocalFilesExecuted";
  3352. break;
  3353. case efInternalEditor:
  3354. Counter = "LocalFilesOpenedInInternalEditor";
  3355. break;
  3356. case efExternalEditor:
  3357. Counter = "LocalFilesOpenedInExternalEditor";
  3358. break;
  3359. default:
  3360. DebugFail();
  3361. }
  3362. }
  3363. if (!Handled)
  3364. {
  3365. Configuration->Usage->Inc(Counter);
  3366. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3367. ExternalEditor, LocalRootDirectory, RemoteDirectory, false);
  3368. }
  3369. }
  3370. //---------------------------------------------------------------------------
  3371. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3372. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3373. bool AllSelected, bool OnFocused)
  3374. {
  3375. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3376. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3377. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3378. Side = GetSide(Side);
  3379. TCustomDirView * DView = DirView(Side);
  3380. TStrings * FileList =
  3381. AllSelected ?
  3382. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3383. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3384. try
  3385. {
  3386. DebugAssert(AllSelected || (FileList->Count == 1));
  3387. for (int i = 0; i < FileList->Count; i++)
  3388. {
  3389. UnicodeString ListFileName = FileList->Strings[i];
  3390. UnicodeString FileNameOnly =
  3391. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3392. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3393. if (!DView->ItemIsDirectory(Item))
  3394. {
  3395. UnicodeString FullFileName;
  3396. if (!IsSideLocalBrowser(Side))
  3397. {
  3398. FullFileName = DirView(Side)->Path + ListFileName;
  3399. }
  3400. else
  3401. {
  3402. FullFileName = ListFileName;
  3403. }
  3404. TFileMasks::TParams MaskParams;
  3405. MaskParams.Size = DView->ItemFileSize(Item);
  3406. TDateTimePrecision Precision;
  3407. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3408. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3409. FileList->Objects[i], MaskParams);
  3410. }
  3411. }
  3412. }
  3413. __finally
  3414. {
  3415. delete FileList;
  3416. }
  3417. }
  3418. //---------------------------------------------------------------------------
  3419. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3420. {
  3421. // do not forget to add additional options to TemporarilyDownloadFiles, and AS
  3422. CopyParam.FileNameCase = ncNoChange;
  3423. CopyParam.PreserveRights = false;
  3424. CopyParam.PreserveReadOnly = false;
  3425. CopyParam.ReplaceInvalidChars = true;
  3426. CopyParam.IncludeFileMask = TFileMasks();
  3427. CopyParam.NewerOnly = false;
  3428. CopyParam.FileMask = L"";
  3429. }
  3430. //---------------------------------------------------------------------------
  3431. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3432. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3433. {
  3434. TTerminalManager * Manager = TTerminalManager::Instance();
  3435. if (!IsActiveTerminal(Data->Terminal))
  3436. {
  3437. if (!NonVisualDataModule->Busy)
  3438. {
  3439. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3440. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3441. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3442. if (Data->Terminal == NULL)
  3443. {
  3444. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3445. if (SameSiteTerminal != NULL)
  3446. {
  3447. UnicodeString Message =
  3448. FMTLOAD(EDIT_SESSION_REATTACH,
  3449. (FileNameOnly, Data->SessionName, FileNameOnly));
  3450. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3451. {
  3452. Data->Terminal = Terminal;
  3453. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3454. Data->SessionName = Terminal->SessionData->SessionName;
  3455. // We might also overwrite session data
  3456. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3457. }
  3458. else
  3459. {
  3460. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3461. Abort();
  3462. }
  3463. }
  3464. }
  3465. // foreground session should reconnect itself
  3466. else if (Terminal != Data->Terminal)
  3467. {
  3468. UnicodeString Message =
  3469. MainInstructions(
  3470. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3471. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3472. {
  3473. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3474. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3475. }
  3476. else
  3477. {
  3478. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3479. Abort();
  3480. }
  3481. }
  3482. }
  3483. if (!IsActiveTerminal(Data->Terminal))
  3484. {
  3485. Configuration->Usage->Inc(L"EditInactiveSession");
  3486. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3487. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3488. (ExtractFileName(FileName), Data->SessionName)));
  3489. }
  3490. }
  3491. TStrings * FileList = new TStringList();
  3492. try
  3493. {
  3494. FileList->Add(FileName);
  3495. // consider using the same settings (preset) as when the file was downloaded
  3496. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3497. TemporaryFileCopyParam(CopyParam);
  3498. if (Data->ForceText)
  3499. {
  3500. CopyParam.TransferMode = tmAscii;
  3501. }
  3502. // so i do not need to worry if masking algorithm works in all cases
  3503. // ("" means "copy file name", no masking is actually done)
  3504. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3505. {
  3506. CopyParam.FileMask = L"";
  3507. }
  3508. else
  3509. {
  3510. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3511. }
  3512. DebugAssert(Data->Queue != NULL);
  3513. int Params = cpNoConfirmation | cpTemporary;
  3514. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3515. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3516. QueueItem->CompleteEvent = UploadCompleteEvent;
  3517. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3518. }
  3519. __finally
  3520. {
  3521. delete FileList;
  3522. }
  3523. }
  3524. //---------------------------------------------------------------------------
  3525. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3526. const UnicodeString FileName, const TEditedFileData * Data)
  3527. {
  3528. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3529. DebugAssert(!NonVisualDataModule->Busy);
  3530. if (!IsActiveTerminal(Data->Terminal))
  3531. {
  3532. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3533. (ExtractFileName(FileName), Data->SessionName)));
  3534. }
  3535. TManagedTerminal * PrevSession = TTerminalManager::Instance()->ActiveSession;
  3536. TTerminalManager::Instance()->ActiveSession = Data->Terminal;
  3537. NonVisualDataModule->StartBusy();
  3538. try
  3539. {
  3540. std::unique_ptr<TRemoteFile> File;
  3541. UnicodeString RemoteFileName =
  3542. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3543. Terminal->ExceptionOnFail = true;
  3544. try
  3545. {
  3546. TRemoteFile * AFile = NULL;
  3547. Terminal->ReadFile(RemoteFileName, AFile);
  3548. File.reset(AFile);
  3549. if (!File->HaveFullFileName)
  3550. {
  3551. File->FullFileName = RemoteFileName;
  3552. }
  3553. }
  3554. __finally
  3555. {
  3556. Terminal->ExceptionOnFail = false;
  3557. }
  3558. std::unique_ptr<TStrings> FileList(new TStringList());
  3559. FileList->AddObject(RemoteFileName, File.get());
  3560. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3561. UnicodeString TempDir = ExtractFilePath(FileName);
  3562. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3563. TempDir, true, true, true);
  3564. // sanity check, the target file name should be still the same
  3565. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3566. }
  3567. __finally
  3568. {
  3569. NonVisualDataModule->EndBusy();
  3570. // it actually may not exist anymore...
  3571. TTerminalManager::Instance()->ActiveSession = PrevSession;
  3572. }
  3573. }
  3574. //---------------------------------------------------------------------------
  3575. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3576. const TEditedFileData * Data, bool & KeepOpen)
  3577. {
  3578. // Command is set for external editors only (not for "shell" open).
  3579. if (!Data->Command.IsEmpty())
  3580. {
  3581. bool AnyFound = false;
  3582. bool AnyMDI = false;
  3583. bool AnyNonMDI = false;
  3584. bool AnyDetect = false;
  3585. TEditorList * EditorList = new TEditorList();
  3586. try
  3587. {
  3588. *EditorList = *WinConfiguration->EditorList;
  3589. for (int i = 0; i < EditorList->Count; i++)
  3590. {
  3591. const TEditorPreferences * Editor = EditorList->Editors[i];
  3592. if ((Editor->Data->Editor == edExternal) &&
  3593. (Editor->Data->ExternalEditor == Data->Command))
  3594. {
  3595. AnyFound = true;
  3596. if (Editor->Data->SDIExternalEditor)
  3597. {
  3598. AnyNonMDI = true;
  3599. if (Editor->Data->DetectMDIExternalEditor)
  3600. {
  3601. AnyDetect = true;
  3602. }
  3603. }
  3604. else
  3605. {
  3606. AnyMDI = true;
  3607. }
  3608. }
  3609. }
  3610. bool EnableMDI = false;
  3611. bool DisableDetect = false;
  3612. if (AnyMDI)
  3613. {
  3614. KeepOpen = true;
  3615. if (AnyNonMDI)
  3616. {
  3617. // there is at least one instance of the editor with MDI support enabled,
  3618. // and one with disabled, enable it for all instances
  3619. EnableMDI = true;
  3620. }
  3621. }
  3622. else if (AnyFound && !AnyDetect)
  3623. {
  3624. // at least once instance found but all have MDI autodetection disabled
  3625. // => close the file (default action)
  3626. }
  3627. else
  3628. {
  3629. // no instance of the editor has MDI support enabled
  3630. TMessageParams Params;
  3631. if (AnyFound)
  3632. {
  3633. // there is at least one instance of the editor
  3634. Params.Params |= mpNeverAskAgainCheck;
  3635. }
  3636. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3637. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3638. switch (Answer)
  3639. {
  3640. case qaNeverAskAgain:
  3641. DisableDetect = true;
  3642. break;
  3643. case qaNo:
  3644. EnableMDI = true;
  3645. KeepOpen = true;
  3646. break;
  3647. }
  3648. }
  3649. if (AnyFound && (EnableMDI || DisableDetect))
  3650. {
  3651. bool Changed = false;
  3652. for (int i = 0; i < EditorList->Count; i++)
  3653. {
  3654. const TEditorPreferences * Editor = EditorList->Editors[i];
  3655. if ((Editor->Data->Editor == edExternal) &&
  3656. (Editor->Data->ExternalEditor == Data->Command) &&
  3657. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3658. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3659. {
  3660. Changed = true;
  3661. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3662. if (EnableMDI)
  3663. {
  3664. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3665. }
  3666. if (DisableDetect)
  3667. {
  3668. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3669. }
  3670. EditorList->Change(i, UpdatedEditor);
  3671. }
  3672. }
  3673. if (Changed)
  3674. {
  3675. WinConfiguration->EditorList = EditorList;
  3676. }
  3677. }
  3678. }
  3679. __finally
  3680. {
  3681. delete EditorList;
  3682. }
  3683. }
  3684. else
  3685. {
  3686. // "open" case
  3687. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3688. qaOK, HELP_APP_EARLY_CLOSED);
  3689. }
  3690. }
  3691. //---------------------------------------------------------------------------
  3692. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3693. {
  3694. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3695. }
  3696. //---------------------------------------------------------------------------
  3697. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3698. {
  3699. SideEnter(osRemote);
  3700. }
  3701. //---------------------------------------------------------------------------
  3702. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3703. {
  3704. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3705. SideEnter(osRemote);
  3706. }
  3707. //---------------------------------------------------------------------------
  3708. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3709. {
  3710. FCurrentSide = Side;
  3711. if (Visible)
  3712. {
  3713. UpdateControls();
  3714. }
  3715. }
  3716. //---------------------------------------------------------------------------
  3717. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3718. TStrings * FileList, bool Alternative)
  3719. {
  3720. TCustomDirView * DView = DirView(Side);
  3721. DView->SaveSelection();
  3722. DView->SaveSelectedNames();
  3723. DebugAssert(!FAlternativeDelete);
  3724. FAlternativeDelete = Alternative;
  3725. try
  3726. {
  3727. if (!IsSideLocalBrowser(Side))
  3728. {
  3729. DebugAssert(Terminal != NULL);
  3730. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3731. }
  3732. else
  3733. {
  3734. if (IsLocalBrowserMode())
  3735. {
  3736. Configuration->Usage->Inc(L"LocalLocalDeletes");
  3737. }
  3738. TValueRestorer<TOperationSide> ProgressSideRestorer(FProgressSide);
  3739. FProgressSide = FCurrentSide;
  3740. try
  3741. {
  3742. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3743. }
  3744. __finally
  3745. {
  3746. ReloadLocalDirectory();
  3747. }
  3748. }
  3749. FAlternativeDelete = false;
  3750. }
  3751. catch(...)
  3752. {
  3753. FAlternativeDelete = false;
  3754. DView->DiscardSavedSelection();
  3755. throw;
  3756. }
  3757. DView->RestoreSelection();
  3758. }
  3759. //---------------------------------------------------------------------------
  3760. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3761. {
  3762. DebugAssert(Terminal);
  3763. DebugAssert(!IsLocalBrowserMode());
  3764. RemoteDirView->SaveSelection();
  3765. RemoteDirView->SaveSelectedNames();
  3766. try
  3767. {
  3768. if (Lock)
  3769. {
  3770. Terminal->LockFiles(FileList);
  3771. }
  3772. else
  3773. {
  3774. Terminal->UnlockFiles(FileList);
  3775. }
  3776. }
  3777. catch(...)
  3778. {
  3779. RemoteDirView->DiscardSavedSelection();
  3780. throw;
  3781. }
  3782. RemoteDirView->RestoreSelection();
  3783. }
  3784. //---------------------------------------------------------------------------
  3785. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  3786. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3787. bool NoConfirmation, bool Move)
  3788. {
  3789. DebugAssert(Terminal != NULL);
  3790. DebugAssert(!IsLocalBrowserMode());
  3791. // update Terminal->StateData->RemoteDirectory
  3792. UpdateSession(Terminal);
  3793. if (Session == NULL)
  3794. {
  3795. Session = TTerminalManager::Instance()->ActiveTerminal;
  3796. }
  3797. if (Session == TTerminalManager::Instance()->ActiveTerminal)
  3798. {
  3799. if (RemoteDriveView->DropTarget != NULL)
  3800. {
  3801. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3802. }
  3803. else if (RemoteDirView->DropTarget != NULL)
  3804. {
  3805. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3806. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3807. }
  3808. else
  3809. {
  3810. Target = RemoteDirView->Path;
  3811. }
  3812. }
  3813. else
  3814. {
  3815. Target = Session->StateData->RemoteDirectory;
  3816. }
  3817. Target = UnixIncludeTrailingBackslash(Target);
  3818. if (FileList->Count == 1)
  3819. {
  3820. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3821. }
  3822. else
  3823. {
  3824. FileMask = AnyMask;
  3825. }
  3826. DirectCopy = Terminal->IsCapable[fcRemoteCopy] || Terminal->IsCapable[fcSecondaryShell];
  3827. bool Result = true;
  3828. if (!NoConfirmation)
  3829. {
  3830. bool Multi = (FileList->Count > 1);
  3831. if (Move)
  3832. {
  3833. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3834. }
  3835. else
  3836. {
  3837. TStrings * Sessions = TTerminalManager::Instance()->SessionList;
  3838. TStrings * Directories = new TStringList;
  3839. try
  3840. {
  3841. for (int Index = 0; Index < Sessions->Count; Index++)
  3842. {
  3843. TManagedTerminal * Terminal =
  3844. dynamic_cast<TManagedTerminal *>(Sessions->Objects[Index]);
  3845. Directories->Add(Terminal->StateData->RemoteDirectory);
  3846. }
  3847. TDirectRemoteCopy AllowDirectCopy;
  3848. if (Terminal->IsCapable[fcRemoteCopy] || Terminal->CommandSessionOpened)
  3849. {
  3850. DebugAssert(DirectCopy);
  3851. AllowDirectCopy = drcAllow;
  3852. }
  3853. else if (Terminal->IsCapable[fcSecondaryShell])
  3854. {
  3855. DebugAssert(DirectCopy);
  3856. AllowDirectCopy = drcConfirmCommandSession;
  3857. }
  3858. else
  3859. {
  3860. DebugAssert(!DirectCopy);
  3861. AllowDirectCopy = drcDisallow;
  3862. }
  3863. void * ASession = Session;
  3864. Result = DoRemoteCopyDialog(Sessions, Directories, AllowDirectCopy,
  3865. Multi, ASession, Target, FileMask, DirectCopy, TTerminalManager::Instance()->ActiveTerminal);
  3866. Session = static_cast<TManagedTerminal *>(ASession);
  3867. }
  3868. __finally
  3869. {
  3870. delete Directories;
  3871. }
  3872. }
  3873. }
  3874. return Result;
  3875. }
  3876. //---------------------------------------------------------------------------
  3877. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3878. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  3879. {
  3880. DebugAssert(!IsLocalBrowserMode());
  3881. bool DirectCopy;
  3882. UnicodeString Target, FileMask;
  3883. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3884. if (Result)
  3885. {
  3886. if (!Move && !DirectCopy)
  3887. {
  3888. Configuration->Usage->Inc("RemoteCopyTemp");
  3889. UnicodeString RootTempDir;
  3890. UnicodeString TempDir;
  3891. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3892. TStrings * TemporaryFilesList = new TStringList();
  3893. try
  3894. {
  3895. TMakeLocalFileListParams MakeFileListParam;
  3896. MakeFileListParam.FileList = TemporaryFilesList;
  3897. MakeFileListParam.FileTimes = NULL;
  3898. MakeFileListParam.IncludeDirs = true;
  3899. MakeFileListParam.Recursive = false;
  3900. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  3901. TTerminalManager::Instance()->ActiveSession = Session;
  3902. if (TemporaryFilesList->Count > 0)
  3903. {
  3904. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3905. CopyParam.FileMask = FileMask;
  3906. DebugAssert(!FAutoOperation);
  3907. FAutoOperation = true;
  3908. Terminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3909. }
  3910. }
  3911. __finally
  3912. {
  3913. delete TemporaryFilesList;
  3914. FAutoOperation = false;
  3915. if (!RootTempDir.IsEmpty())
  3916. {
  3917. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3918. }
  3919. }
  3920. }
  3921. else
  3922. {
  3923. RemoteDirView->SaveSelection();
  3924. RemoteDirView->SaveSelectedNames();
  3925. try
  3926. {
  3927. if (Move)
  3928. {
  3929. Configuration->Usage->Inc("RemoteMove");
  3930. Terminal->MoveFiles(FileList, Target, FileMask);
  3931. }
  3932. else
  3933. {
  3934. Configuration->Usage->Inc("RemoteCopyDirect");
  3935. DebugAssert(DirectCopy);
  3936. DebugAssert(Session == Terminal);
  3937. if (Terminal->IsCapable[fcRemoteCopy] ||
  3938. Terminal->CommandSessionOpened ||
  3939. CommandSessionFallback())
  3940. {
  3941. Terminal->CopyFiles(FileList, Target, FileMask);
  3942. }
  3943. }
  3944. }
  3945. catch(...)
  3946. {
  3947. RemoteDirView->DiscardSavedSelection();
  3948. throw;
  3949. }
  3950. RemoteDirView->RestoreSelection();
  3951. }
  3952. }
  3953. return Result;
  3954. }
  3955. //---------------------------------------------------------------------------
  3956. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3957. const UnicodeString & Path, TRemoteProperties & Properties)
  3958. {
  3959. DebugAssert(!IsLocalBrowserMode());
  3960. Properties.Valid = Properties.Valid << vpEncrypt;
  3961. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  3962. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  3963. }
  3964. //---------------------------------------------------------------------------
  3965. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3966. {
  3967. Side = GetSide(Side);
  3968. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3969. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  3970. UnicodeString Name = LoadStr(NEW_FOLDER);
  3971. int AllowedChanges =
  3972. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  3973. bool SaveSettings = false;
  3974. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  3975. {
  3976. TWindowLock Lock(this);
  3977. if (!IsSideLocalBrowser(Side))
  3978. {
  3979. if (SaveSettings)
  3980. {
  3981. GUIConfiguration->NewDirectoryProperties = Properties;
  3982. }
  3983. CreateRemoteDirectory(Name, Properties);
  3984. }
  3985. else
  3986. {
  3987. DirView(Side)->CreateDirectory(Name);
  3988. }
  3989. }
  3990. }
  3991. //---------------------------------------------------------------------------
  3992. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  3993. {
  3994. TWindowLock Lock(this);
  3995. DirView(Side)->ExecuteHomeDirectory();
  3996. }
  3997. //---------------------------------------------------------------------------
  3998. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  3999. {
  4000. DoOpenDirectoryDialog(odBrowse, Side);
  4001. }
  4002. //---------------------------------------------------------------------------
  4003. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  4004. {
  4005. TOperationSide BookmarkSide = IsLocalBrowserMode() ? osLocal : Side;
  4006. UnicodeString Path = Bookmark->GetSideDirectory(BookmarkSide);
  4007. bool Result = !Path.IsEmpty();
  4008. if (Result)
  4009. {
  4010. // While we might get here when the session is closed (from location profiles),
  4011. // it's not a problem as the Path setter is noop then
  4012. DirView(Side)->Path = Path;
  4013. }
  4014. return Result;
  4015. }
  4016. //---------------------------------------------------------------------------
  4017. void __fastcall TCustomScpExplorerForm::CalculateSize(
  4018. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  4019. bool & Close)
  4020. {
  4021. // terminal can be already closed (e.g. dropped connection)
  4022. if (Terminal != NULL)
  4023. {
  4024. try
  4025. {
  4026. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  4027. }
  4028. catch(...)
  4029. {
  4030. if (!Terminal->Active)
  4031. {
  4032. Close = true;
  4033. }
  4034. throw;
  4035. }
  4036. }
  4037. }
  4038. //---------------------------------------------------------------------------
  4039. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  4040. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  4041. bool & Close)
  4042. {
  4043. // terminal can be already closed (e.g. dropped connection)
  4044. if (Terminal != NULL)
  4045. {
  4046. Configuration->Usage->Inc(L"ChecksumCalculated");
  4047. try
  4048. {
  4049. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  4050. }
  4051. catch(...)
  4052. {
  4053. if (!Terminal->Active)
  4054. {
  4055. Close = true;
  4056. }
  4057. throw;
  4058. }
  4059. }
  4060. }
  4061. //---------------------------------------------------------------------------
  4062. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  4063. {
  4064. bool Result;
  4065. if (!IsSideLocalBrowser(Side))
  4066. {
  4067. TRemoteTokenList * GroupList = NULL;
  4068. TRemoteTokenList * UserList = NULL;
  4069. try
  4070. {
  4071. TRemoteProperties CurrentProperties;
  4072. if (Terminal->LoadFilesProperties(FileList))
  4073. {
  4074. RemoteDirView->Invalidate();
  4075. }
  4076. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4077. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4078. if (CapableGroupChanging || CapableOwnerChanging)
  4079. {
  4080. if (CapableGroupChanging)
  4081. {
  4082. GroupList = Terminal->Groups->Duplicate();
  4083. }
  4084. if (CapableOwnerChanging)
  4085. {
  4086. UserList = Terminal->Users->Duplicate();
  4087. }
  4088. TRemoteDirectory * Files = Terminal->Files;
  4089. int Count = Files->Count;
  4090. if (Count > 100)
  4091. {
  4092. Count = 100;
  4093. }
  4094. for (int Index = 0; Index < Count; Index++)
  4095. {
  4096. TRemoteFile * File = Files->Files[Index];
  4097. if (CapableGroupChanging)
  4098. {
  4099. GroupList->AddUnique(File->Group);
  4100. }
  4101. if (CapableOwnerChanging)
  4102. {
  4103. UserList->AddUnique(File->Owner);
  4104. }
  4105. }
  4106. // if we haven't collected tokens for all files in current directory,
  4107. // make sure we collect them at least for all selected files.
  4108. // (note that so far the files in FileList has to be from current direcotry)
  4109. if (Count < Files->Count)
  4110. {
  4111. for (int Index = 0; Index < FileList->Count; Index++)
  4112. {
  4113. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4114. if (CapableGroupChanging)
  4115. {
  4116. GroupList->AddUnique(File->Group);
  4117. }
  4118. if (CapableOwnerChanging)
  4119. {
  4120. UserList->AddUnique(File->Owner);
  4121. }
  4122. }
  4123. }
  4124. }
  4125. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4126. int Flags = 0;
  4127. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4128. if (CapableOwnerChanging) Flags |= cpOwner;
  4129. if (CapableGroupChanging) Flags |= cpGroup;
  4130. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4131. if (Terminal->IsCapable[fcCalculatingChecksum])
  4132. {
  4133. CalculateChecksumEvent = CalculateChecksum;
  4134. }
  4135. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4136. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4137. TRemoteProperties NewProperties = CurrentProperties;
  4138. Result =
  4139. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4140. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4141. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4142. CalculateSize, CalculateChecksumEvent);
  4143. if (Result)
  4144. {
  4145. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4146. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4147. }
  4148. }
  4149. __finally
  4150. {
  4151. delete GroupList;
  4152. delete UserList;
  4153. }
  4154. }
  4155. else
  4156. {
  4157. DirView(Side)->DisplayPropertiesMenu();
  4158. Result = true;
  4159. }
  4160. return Result;
  4161. }
  4162. //---------------------------------------------------------------------------
  4163. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4164. {
  4165. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4166. {
  4167. FIgnoreNextDialogChar = Key;
  4168. }
  4169. Key = 0;
  4170. }
  4171. //---------------------------------------------------------------------------
  4172. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4173. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4174. {
  4175. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4176. if (Command != NULL)
  4177. {
  4178. KeyProcessed(Key, Shift);
  4179. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4180. {
  4181. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4182. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4183. false, false, const_cast<TCustomCommandType *>(Command));
  4184. }
  4185. }
  4186. }
  4187. //---------------------------------------------------------------------------
  4188. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4189. {
  4190. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4191. {
  4192. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4193. }
  4194. if (!DirView(osCurrent)->IsEditing())
  4195. {
  4196. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4197. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4198. {
  4199. // Note that handling shortcuts on panel (current control) popup menu has precendence over this.
  4200. // But so far it's not a problem, as it does what we want.
  4201. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4202. if (((Action->ShortCut == KeyShortCut) ||
  4203. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4204. AllowedAction(Action, aaShortCut))
  4205. {
  4206. // Has to be called before the action as the dialog char is already in queue.
  4207. // So when the action consumes message queue, we already need to have the
  4208. // FIgnoreNextDialogChar set
  4209. KeyProcessed(Key, Shift);
  4210. // Reset reference to previous component (when menu/toolbar was clicked).
  4211. // Needed to detect that action was invoked by keyboard shortcut
  4212. // in TNonVisualDataModule::ExplorerActionsExecute
  4213. Action->ActionComponent = NULL;
  4214. Action->Execute();
  4215. return;
  4216. }
  4217. }
  4218. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4219. {
  4220. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4221. {
  4222. KeyProcessed(Key, Shift);
  4223. TTerminalManager::Instance()->ActiveSessionIndex = i;
  4224. return;
  4225. }
  4226. }
  4227. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4228. {
  4229. KeyProcessed(Key, Shift);
  4230. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4231. }
  4232. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4233. if (IsCustomShortCut(CustomShortCut))
  4234. {
  4235. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4236. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4237. if (WinConfiguration->SharedBookmarks != NULL)
  4238. {
  4239. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4240. if ((Bookmark != NULL) &&
  4241. OpenBookmark(FCurrentSide, Bookmark))
  4242. {
  4243. KeyProcessed(Key, Shift);
  4244. }
  4245. }
  4246. }
  4247. }
  4248. TForm::KeyDown(Key, Shift);
  4249. }
  4250. //---------------------------------------------------------------------------
  4251. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4252. {
  4253. bool OpenedTerminal = false;
  4254. UnicodeString ProtocolName;
  4255. UnicodeString ProtocolNameHint;
  4256. UnicodeString SecurityProtocolName;
  4257. UnicodeString DurationHint;
  4258. // See the comment in TScpCommanderForm::UpdateControls()
  4259. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4260. if (ActiveTerminal != NULL)
  4261. {
  4262. // Active is not enough here
  4263. if (ActiveTerminal->Status == ssOpened)
  4264. {
  4265. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4266. const TFileSystemInfo & FileSystemInfo = ActiveTerminal->GetFileSystemInfo();
  4267. if (FileSystemInfo.ProtocolName.IsEmpty())
  4268. {
  4269. ProtocolName = SessionInfo.ProtocolName;
  4270. }
  4271. else
  4272. {
  4273. ProtocolName = FileSystemInfo.ProtocolName;
  4274. }
  4275. ProtocolNameHint = LoadStr(STATUS_PROTOCOL_HINT);
  4276. SecurityProtocolName = SessionInfo.SecurityProtocolName;
  4277. DurationHint = LoadStr(STATUS_DURATION_HINT);
  4278. OpenedTerminal = true;
  4279. }
  4280. else if (ActiveTerminal->Status > ssClosed)
  4281. {
  4282. ProtocolName = LoadStr(STATUS_CONNECTING);
  4283. }
  4284. else
  4285. {
  4286. ProtocolName = LoadStr(STATUS_NOT_CONNECTED2);
  4287. }
  4288. }
  4289. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4290. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4291. TTBXStatusPanel * ProtocolStatusPanel = SessionStatusBar->Panels->Items[Offset + 0];
  4292. ProtocolStatusPanel->ViewPriority = (ActiveTerminal != NULL) ? 100 : 0;
  4293. if (ProtocolStatusPanel->Caption != ProtocolName)
  4294. {
  4295. ProtocolStatusPanel->Caption = ProtocolName;
  4296. std::unique_ptr<TCanvas> Canvas(CreateControlCanvas(SessionStatusBar));
  4297. ProtocolStatusPanel->Size = Canvas->TextWidth(ProtocolName) + ScaleByTextHeight(SessionStatusBar, 32);
  4298. }
  4299. ProtocolStatusPanel->Hint = ProtocolNameHint;
  4300. TTBXStatusPanel * SecurityStatusPanel = SessionStatusBar->Panels->Items[Offset + 1];
  4301. SecurityStatusPanel->ViewPriority = OpenedTerminal ? 98 : 0;
  4302. bool SecurityEnabled = !SecurityProtocolName.IsEmpty();
  4303. SecurityStatusPanel->Enabled = SecurityEnabled;
  4304. UnicodeString SecurityProtocolHint;
  4305. if (SecurityEnabled)
  4306. {
  4307. SecurityProtocolHint = FMTLOAD(STATUS_SECURE, (SecurityProtocolName));
  4308. }
  4309. else
  4310. {
  4311. SecurityProtocolHint = LoadStr(STATUS_INSECURE);
  4312. }
  4313. SecurityStatusPanel->Hint = SecurityProtocolHint;
  4314. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[Offset + 2];
  4315. DurationStatusPanel->ViewPriority = OpenedTerminal ? 99 : 0;
  4316. DurationStatusPanel->Hint = DurationHint;
  4317. UpdateStatusBar();
  4318. }
  4319. //---------------------------------------------------------------------------
  4320. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4321. {
  4322. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4323. DebugAssert(SessionStatusBar != NULL);
  4324. TTBXStatusPanel * NoteStatusPanel = SessionStatusBar->Panels->Items[0];
  4325. if (!FNote.IsEmpty())
  4326. {
  4327. NoteStatusPanel->Caption = FNote;
  4328. }
  4329. else
  4330. {
  4331. UpdateStatusPanelText(NoteStatusPanel);
  4332. }
  4333. NoteStatusPanel->Hint = FNoteHints;
  4334. UnicodeString LoginTime;
  4335. // See the comment in TScpCommanderForm::UpdateControls()
  4336. TTerminal * ActiveTerminal = TTerminalManager::Instance()->ActiveTerminal;
  4337. // Active is not enough here
  4338. if ((ActiveTerminal != NULL) && (ActiveTerminal->Status == ssOpened))
  4339. {
  4340. const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
  4341. LoginTime = FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4342. }
  4343. TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1];
  4344. DurationStatusPanel->Caption = LoginTime;
  4345. }
  4346. //---------------------------------------------------------------------------
  4347. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4348. {
  4349. Panel->Caption = L"";
  4350. }
  4351. //---------------------------------------------------------------------------
  4352. void __fastcall TCustomScpExplorerForm::Idle()
  4353. {
  4354. if (FShowing || FStandaloneEditing)
  4355. {
  4356. FEditorManager->Check();
  4357. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4358. // that needs to know if queue view is visible (and it may be closed after queue update)
  4359. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4360. }
  4361. if (FShowing)
  4362. {
  4363. if (!NonVisualDataModule->Busy &&
  4364. // Menu is opened or drag&drop is going on
  4365. (Mouse->Capture == NULL))
  4366. {
  4367. if (FRefreshRemoteDirectory)
  4368. {
  4369. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4370. {
  4371. Terminal->RefreshDirectory();
  4372. }
  4373. FRefreshRemoteDirectory = false;
  4374. }
  4375. if (FRefreshLocalDirectory)
  4376. {
  4377. ReloadLocalDirectory();
  4378. FRefreshLocalDirectory = false;
  4379. }
  4380. if (WinConfiguration->RefreshRemotePanel)
  4381. {
  4382. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4383. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4384. {
  4385. RemoteDirView->ReloadDirectory();
  4386. }
  4387. }
  4388. }
  4389. }
  4390. if (FShowing || FStandaloneEditing)
  4391. {
  4392. if (FQueueStatusInvalidated)
  4393. {
  4394. UpdateQueueStatus(false);
  4395. }
  4396. RefreshQueueItems();
  4397. }
  4398. if (FShowing)
  4399. {
  4400. UpdateStatusBar();
  4401. }
  4402. FIgnoreNextDialogChar = 0;
  4403. }
  4404. //---------------------------------------------------------------------------
  4405. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4406. {
  4407. try
  4408. {
  4409. FUserActionTimer->Enabled = false;
  4410. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4411. {
  4412. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4413. {
  4414. FPendingQueueActionItem->ProcessUserAction();
  4415. }
  4416. }
  4417. }
  4418. __finally
  4419. {
  4420. FPendingQueueActionItem = NULL;
  4421. }
  4422. }
  4423. //---------------------------------------------------------------------------
  4424. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4425. {
  4426. if (WinConfiguration->MinimizeToTray)
  4427. {
  4428. UpdateTrayIcon();
  4429. FTrayIcon->Visible = true;
  4430. if (Visible)
  4431. {
  4432. ShowWindow(Handle, SW_HIDE);
  4433. }
  4434. }
  4435. }
  4436. //---------------------------------------------------------------------------
  4437. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4438. {
  4439. // WORKAROUND
  4440. // When restoring maximized window from minimization,
  4441. // rarely some controls do not align properly.
  4442. // Two instances seen (both for Commander):
  4443. // - When restoring, window is temporarily narrower (not maximized),
  4444. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4445. // Once maximized already, top dock shinks back, but the session PageControl,
  4446. // do not align up, leaving space between TopDock and PageControl.
  4447. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4448. for (int Index = 0; Index < ControlCount; Index++)
  4449. {
  4450. RealignControl(Controls[Index]);
  4451. }
  4452. if (FTrayIcon->Visible)
  4453. {
  4454. FTrayIcon->Visible = false;
  4455. if (Visible)
  4456. {
  4457. ShowWindow(Handle, SW_SHOW);
  4458. }
  4459. }
  4460. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4461. {
  4462. FNeedSession = false;
  4463. NonVisualDataModule->StartBusy();
  4464. try
  4465. {
  4466. NeedSession(true);
  4467. }
  4468. __finally
  4469. {
  4470. NonVisualDataModule->EndBusy();
  4471. }
  4472. }
  4473. }
  4474. //---------------------------------------------------------------------------
  4475. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4476. {
  4477. FTrayIcon->Hint = GetMainForm()->Caption;
  4478. }
  4479. //---------------------------------------------------------------------------
  4480. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4481. {
  4482. UpdateTrayIcon();
  4483. }
  4484. //---------------------------------------------------------------------------
  4485. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4486. {
  4487. // workaround
  4488. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4489. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4490. // But WM_ACTIVATEAPP message can be received even when
  4491. // the main window is minimized to the tray and internal editor window is focused
  4492. // (after another application was previously active)
  4493. if (::IsIconic(Handle))
  4494. {
  4495. if (!IsAppIconic())
  4496. {
  4497. SetAppIconic(true);
  4498. }
  4499. }
  4500. ::ApplicationRestore();
  4501. Application->BringToFront();
  4502. }
  4503. //---------------------------------------------------------------------------
  4504. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4505. {
  4506. RestoreApp();
  4507. }
  4508. //---------------------------------------------------------------------------
  4509. void __fastcall TCustomScpExplorerForm::NewSession(const UnicodeString & SessionUrl)
  4510. {
  4511. if (OpenInNewWindow())
  4512. {
  4513. ExecuteNewInstance(SessionUrl);
  4514. }
  4515. else
  4516. {
  4517. TTerminalManager::Instance()->NewSession(SessionUrl);
  4518. }
  4519. }
  4520. //---------------------------------------------------------------------------
  4521. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4522. {
  4523. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->SessionName;
  4524. StoredSessions->NewSession(SessionName, SessionData);
  4525. // modified only, explicit
  4526. StoredSessions->Save(false, true);
  4527. // encode session name because of slashes in hierarchical sessions
  4528. return EncodeUrlString(SessionName);
  4529. }
  4530. //---------------------------------------------------------------------------
  4531. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4532. {
  4533. // current working directories become defaults here, what is not right
  4534. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4535. return SaveHiddenDuplicateSession(SessionData.get());
  4536. }
  4537. //---------------------------------------------------------------------------
  4538. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4539. {
  4540. if (OpenInNewWindow())
  4541. {
  4542. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4543. }
  4544. else
  4545. {
  4546. // current working directories become defaults here, what is not right
  4547. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4548. TTerminalManager * Manager = TTerminalManager::Instance();
  4549. TManagedTerminal * ATerminal = Manager->NewManagedTerminal(SessionData.get());
  4550. // We definitelly want this
  4551. ATerminal->Disconnected = Terminal->Disconnected;
  4552. // Not sure about these two
  4553. ATerminal->Permanent = Terminal->Permanent;
  4554. ATerminal->DisconnectedTemporarily = Terminal->DisconnectedTemporarily; // maybe
  4555. Manager->ActiveSession = ATerminal;
  4556. }
  4557. }
  4558. //---------------------------------------------------------------------------
  4559. void __fastcall TCustomScpExplorerForm::RenameSession()
  4560. {
  4561. UnicodeString Name = Terminal->SessionData->SessionName;
  4562. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME))
  4563. {
  4564. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4565. TSessionData::ValidateName(Name);
  4566. // This is inconsistent with how color (for example) is handled.
  4567. Terminal->SessionData->Name = Name;
  4568. UpdateControls();
  4569. // Add/Remove distinguishing paths from sessions of the same name.
  4570. SessionListChanged();
  4571. }
  4572. }
  4573. //---------------------------------------------------------------------------
  4574. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4575. {
  4576. DebugAssert(Queue != NULL);
  4577. bool Result = Queue->IsEmpty;
  4578. if (!Result)
  4579. {
  4580. SetFocus();
  4581. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4582. }
  4583. return Result;
  4584. }
  4585. //---------------------------------------------------------------------------
  4586. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4587. {
  4588. return CanCloseQueue(FQueue);
  4589. }
  4590. //---------------------------------------------------------------------------
  4591. void __fastcall TCustomScpExplorerForm::CloseSession()
  4592. {
  4593. if (CanCloseQueue())
  4594. {
  4595. TTerminalManager::Instance()->FreeActiveTerminal();
  4596. }
  4597. }
  4598. //---------------------------------------------------------------------------
  4599. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4600. {
  4601. if (CanCloseQueue())
  4602. {
  4603. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4604. }
  4605. }
  4606. //---------------------------------------------------------------------------
  4607. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4608. {
  4609. DetachTerminal(Terminal);
  4610. RemoteDirView->Terminal = Terminal;
  4611. UpdateRemotePathComboBox(false);
  4612. InitStatusBar();
  4613. UpdateControls();
  4614. }
  4615. //---------------------------------------------------------------------------
  4616. void __fastcall TCustomScpExplorerForm::TerminalConnecting()
  4617. {
  4618. InitStatusBar();
  4619. }
  4620. //---------------------------------------------------------------------------
  4621. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4622. {
  4623. if (DebugAlwaysTrue(!Terminal->Active))
  4624. {
  4625. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4626. }
  4627. }
  4628. //---------------------------------------------------------------------------
  4629. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4630. {
  4631. if (OpenInNewWindow())
  4632. {
  4633. // encode session name because of slashes in hierarchical sessions
  4634. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4635. }
  4636. else
  4637. {
  4638. TTerminalManager * Manager = TTerminalManager::Instance();
  4639. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4640. Manager->ActiveSession = Terminal;
  4641. }
  4642. }
  4643. //---------------------------------------------------------------------------
  4644. void __fastcall TCustomScpExplorerForm::DoOpenFolderOrWorkspace(const UnicodeString & Name, bool ConnectFirstTerminal)
  4645. {
  4646. TTerminalManager * Manager = TTerminalManager::Instance();
  4647. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4648. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4649. TManagedTerminal * FirstTerminal = Manager->NewTerminals(DataList.get());
  4650. // FirstTerminal can be null, if some of the
  4651. if (!ConnectFirstTerminal && (FirstTerminal != NULL))
  4652. {
  4653. FirstTerminal->Disconnected = true;
  4654. FirstTerminal->DisconnectedTemporarily = true;
  4655. }
  4656. Manager->ActiveSession = FirstTerminal;
  4657. }
  4658. //---------------------------------------------------------------------------
  4659. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4660. {
  4661. if (OpenInNewWindow())
  4662. {
  4663. ExecuteNewInstance(Name);
  4664. }
  4665. else
  4666. {
  4667. DoOpenFolderOrWorkspace(Name, true);
  4668. }
  4669. }
  4670. //---------------------------------------------------------------------------
  4671. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4672. bool &CanClose)
  4673. {
  4674. // See the comment in CloseApp()
  4675. if (NonVisualDataModule->Busy)
  4676. {
  4677. CanClose = false;
  4678. }
  4679. else if (Terminal != NULL)
  4680. {
  4681. if (Terminal->Active && WinConfiguration->ConfirmClosingSession)
  4682. {
  4683. unsigned int Result;
  4684. TMessageParams Params(mpNeverAskAgainCheck);
  4685. UnicodeString Message;
  4686. int Answers = qaOK | qaCancel;
  4687. if (TTerminalManager::Instance()->Count > 1)
  4688. {
  4689. if (!WinConfiguration->AutoSaveWorkspace)
  4690. {
  4691. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  4692. }
  4693. else
  4694. {
  4695. Message = LoadStr(CLOSE_SESSIONS);
  4696. }
  4697. }
  4698. else
  4699. {
  4700. if (!WinConfiguration->AutoSaveWorkspace)
  4701. {
  4702. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  4703. }
  4704. else
  4705. {
  4706. Message = LoadStr(CLOSE_SESSION);
  4707. }
  4708. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  4709. }
  4710. UnicodeString Note;
  4711. if (WinConfiguration->AutoSaveWorkspace)
  4712. {
  4713. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  4714. }
  4715. else
  4716. {
  4717. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  4718. Answers = qaYes | qaNo | qaCancel;
  4719. }
  4720. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  4721. SetFocus();
  4722. Result = MessageDialog(Message, qtConfirmation,
  4723. Answers, HELP_CLOSE_SESSION_WORKSPACE, &Params);
  4724. if (Result == qaNeverAskAgain)
  4725. {
  4726. WinConfiguration->ConfirmClosingSession = false;
  4727. }
  4728. if (Result == qaNo)
  4729. {
  4730. CanClose = SaveWorkspace(true);
  4731. // note that the workspace will be saved redundatly again from FormClose
  4732. }
  4733. else
  4734. {
  4735. CanClose =
  4736. (Result == qaOK) ||
  4737. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4738. (Result == qaNeverAskAgain);
  4739. }
  4740. }
  4741. if (CanClose)
  4742. {
  4743. CanClose = CanCloseQueue();
  4744. }
  4745. }
  4746. if (CanClose)
  4747. {
  4748. CanClose =
  4749. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4750. FEditorManager->CloseExternalFilesWithoutProcess();
  4751. if (CanClose)
  4752. {
  4753. while (CanClose && (FLocalEditors->Count > 0))
  4754. {
  4755. int PrevCount = FLocalEditors->Count;
  4756. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4757. CanClose = (FLocalEditors->Count < PrevCount);
  4758. }
  4759. if (CanClose)
  4760. {
  4761. CanClose = FEditorManager->Empty(true);
  4762. if (!CanClose)
  4763. {
  4764. SetFocus();
  4765. CanClose =
  4766. (MessageDialog(
  4767. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4768. }
  4769. }
  4770. }
  4771. }
  4772. }
  4773. //---------------------------------------------------------------------------
  4774. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4775. {
  4776. TForm * Form = dynamic_cast<TForm *>(Sender);
  4777. DebugAssert(Form != NULL);
  4778. Form->Close();
  4779. }
  4780. //---------------------------------------------------------------------------
  4781. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4782. {
  4783. TForm * Form = dynamic_cast<TForm *>(Sender);
  4784. delete Form;
  4785. }
  4786. //---------------------------------------------------------------------------
  4787. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4788. {
  4789. while (FLocalEditors->Count > 0)
  4790. {
  4791. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4792. }
  4793. }
  4794. //---------------------------------------------------------------------------
  4795. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4796. TObject *Sender)
  4797. {
  4798. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4799. try
  4800. {
  4801. SetProperties(osRemote, FileList);
  4802. }
  4803. __finally
  4804. {
  4805. delete FileList;
  4806. }
  4807. }
  4808. //---------------------------------------------------------------------------
  4809. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4810. {
  4811. if (value)
  4812. {
  4813. if (Component == fcCustomCommandsBand)
  4814. {
  4815. UpdateCustomCommandsToolbar();
  4816. }
  4817. if (Component == fcQueueFileList)
  4818. {
  4819. DebugAssert(!QueueFileListSplitter->Visible);
  4820. AdjustQueueLayout();
  4821. UpdateQueueFileList();
  4822. }
  4823. }
  4824. }
  4825. //---------------------------------------------------------------------------
  4826. void __fastcall TCustomScpExplorerForm::AdjustQueueLayout()
  4827. {
  4828. int MinHeightLimit =
  4829. GetStaticQueuePanelComponentsHeight() +
  4830. QueueFileListSplitter->Height +
  4831. QueueFileList->Height +
  4832. GetMinQueueViewHeight();
  4833. if (QueuePanel->ClientHeight < MinHeightLimit)
  4834. {
  4835. QueuePanel->ClientHeight = MinHeightLimit;
  4836. }
  4837. }
  4838. //---------------------------------------------------------------------------
  4839. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4840. {
  4841. TControl * Control = GetComponent(Component);
  4842. DebugAssert(Control);
  4843. bool Changed = (Control->Visible != value);
  4844. if (Changed)
  4845. {
  4846. ComponentShowing(Component, value);
  4847. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4848. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4849. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4850. if (value)
  4851. {
  4852. int RemainingHeight = Control->Parent->ClientHeight;
  4853. int RemainingWidth = Control->Parent->ClientWidth;
  4854. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4855. {
  4856. TControl * ChildControl = Control->Parent->Controls[i];
  4857. if (ChildControl->Visible)
  4858. {
  4859. switch (ChildControl->Align)
  4860. {
  4861. case alTop:
  4862. case alBottom:
  4863. RemainingHeight -= ChildControl->Height;
  4864. break;
  4865. case alLeft:
  4866. case alRight:
  4867. RemainingWidth -= ChildControl->Width;
  4868. break;
  4869. }
  4870. }
  4871. }
  4872. int Reserve = ScaleByTextHeight(this, 32);
  4873. // queue in explorer, trees in commander
  4874. if (Control->Height > RemainingHeight - Reserve)
  4875. {
  4876. Control->Height = RemainingHeight / 2;
  4877. }
  4878. if (Control->Width > RemainingWidth - Reserve)
  4879. {
  4880. Control->Width = RemainingWidth / 2;
  4881. }
  4882. }
  4883. Control->Visible = value;
  4884. if (WasFocused && Visible)
  4885. {
  4886. DirView(osCurrent)->SetFocus();
  4887. }
  4888. FixControlsPlacement();
  4889. }
  4890. }
  4891. //---------------------------------------------------------------------------
  4892. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4893. {
  4894. TControl * Control = GetComponent(Component);
  4895. if (Control == NULL)
  4896. {
  4897. return false;
  4898. }
  4899. else
  4900. {
  4901. return Control->Visible;
  4902. }
  4903. }
  4904. //---------------------------------------------------------------------------
  4905. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4906. {
  4907. bool Result;
  4908. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4909. {
  4910. Result = !IsUWP();
  4911. }
  4912. else
  4913. {
  4914. Result = true;
  4915. }
  4916. return Result;
  4917. }
  4918. //---------------------------------------------------------------------------
  4919. void __fastcall TCustomScpExplorerForm::MakeFocusedItemVisible(TCustomDirView * DirView)
  4920. {
  4921. if (DirView->ItemFocused != NULL)
  4922. {
  4923. DirView->ItemFocused->MakeVisible(false);
  4924. }
  4925. }
  4926. //---------------------------------------------------------------------------
  4927. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4928. {
  4929. MakeFocusedItemVisible(RemoteDirView);
  4930. QueueSplitter->Visible = QueuePanel->Visible;
  4931. QueueFileListSplitter->Visible = QueueFileList->Visible;
  4932. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  4933. TControl * QueueControlsOrder[] =
  4934. { QueueDock, QueueView3, QueueFileListSplitter, QueueFileList };
  4935. SetVerticalControlsOrder(QueueControlsOrder, LENOF(QueueControlsOrder));
  4936. }
  4937. //---------------------------------------------------------------------------
  4938. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4939. {
  4940. switch (Component) {
  4941. case fcStatusBar: return RemoteStatusBar;
  4942. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4943. case fcQueueView: return QueuePanel;
  4944. case fcQueueToolbar: return QueueDock;
  4945. case fcRemoteTree: return RemoteDrivePanel;
  4946. case fcSessionsTabs: return SessionsPageControl;
  4947. case fcQueueFileList: return QueueFileList;
  4948. default: return NULL;
  4949. }
  4950. }
  4951. //---------------------------------------------------------------------------
  4952. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4953. TObject *Sender, TListColumn *Column, TPoint &Point)
  4954. {
  4955. DebugAssert(NonVisualDataModule && Column && Sender);
  4956. NonVisualDataModule->ListColumn = Column;
  4957. TPopupMenu * DirViewColumnMenu;
  4958. if (Sender == DirView(osOther))
  4959. {
  4960. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4961. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4962. (Column->Index == uvName);
  4963. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4964. (Column->Index == uvSize);
  4965. }
  4966. else
  4967. {
  4968. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4969. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4970. (Column->Index == dvName);
  4971. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4972. (Column->Index == dvSize);
  4973. }
  4974. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  4975. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  4976. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  4977. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4978. }
  4979. //---------------------------------------------------------------------------
  4980. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4981. TObject *Sender, TListItem *Item, bool &AllowExec)
  4982. {
  4983. DoDirViewExecFile(Sender, Item, AllowExec);
  4984. }
  4985. //---------------------------------------------------------------------------
  4986. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4987. TListItem * Item, bool & AllowExec)
  4988. {
  4989. DebugAssert(Sender && Item && Configuration);
  4990. DebugAssert(AllowExec);
  4991. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  4992. TOperationSide Side = ((ADirView == DirView(osRemote)) ? osRemote : osLocal);
  4993. bool Remote = !IsSideLocalBrowser(Side);
  4994. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles();
  4995. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  4996. // on files only (not directories)
  4997. // and only when symlinks are resolved (apply to remote panel only)
  4998. if (!ADirView->ItemIsDirectory(Item) &&
  4999. (ResolvedSymlinks || FForceExecution))
  5000. {
  5001. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  5002. !FForceExecution && ResolvedSymlinks)
  5003. {
  5004. if (WinConfiguration->DoubleClickAction == dcaCopy)
  5005. {
  5006. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  5007. UnlockWindow();
  5008. try
  5009. {
  5010. if (IsLocalBrowserMode())
  5011. {
  5012. LocalLocalCopy(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation, false, 0);
  5013. }
  5014. else
  5015. {
  5016. ExecuteFileOperation(foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  5017. }
  5018. }
  5019. __finally
  5020. {
  5021. LockWindow();
  5022. }
  5023. AllowExec = false;
  5024. }
  5025. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  5026. {
  5027. if (!Remote || !WinConfiguration->DisableOpenEdit)
  5028. {
  5029. ExecuteFile(Side, efDefaultEditor);
  5030. AllowExec = false;
  5031. }
  5032. }
  5033. else
  5034. {
  5035. DebugFail();
  5036. }
  5037. }
  5038. // if we have not done anything special, fall back to default behavior
  5039. if (AllowExec)
  5040. {
  5041. if (Remote && !WinConfiguration->DisableOpenEdit)
  5042. {
  5043. ExecuteFile(osRemote, efShell);
  5044. AllowExec = false;
  5045. }
  5046. }
  5047. }
  5048. }
  5049. //---------------------------------------------------------------------------
  5050. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  5051. {
  5052. return ((Side == osRemote) || (Side == osCurrent));
  5053. }
  5054. //---------------------------------------------------------------------------
  5055. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  5056. {
  5057. DebugFail();
  5058. }
  5059. //---------------------------------------------------------------------------
  5060. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  5061. {
  5062. DebugFail();
  5063. }
  5064. //---------------------------------------------------------------------------
  5065. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  5066. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  5067. {
  5068. TSynchronizeParamType Params;
  5069. Params.LocalDirectory = LocalDirectory;
  5070. Params.RemoteDirectory = RemoteDirectory;
  5071. int UnusedParams =
  5072. (GUIConfiguration->SynchronizeParams &
  5073. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize |
  5074. TTerminal::spCaseSensitive));
  5075. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  5076. Params.Options = GUIConfiguration->SynchronizeOptions;
  5077. bool SaveSettings = false;
  5078. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  5079. &DoSynchronizeTooManyDirectories);
  5080. DebugAssert(FSynchronizeController == NULL);
  5081. FSynchronizeController = &Controller;
  5082. bool Result;
  5083. try
  5084. {
  5085. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5086. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  5087. int Options =
  5088. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  5089. DebugAssert(FOnFeedSynchronizeError == NULL);
  5090. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  5091. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  5092. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  5093. if (Result)
  5094. {
  5095. if (SaveSettings)
  5096. {
  5097. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  5098. GUIConfiguration->SynchronizeOptions = Params.Options;
  5099. }
  5100. else
  5101. {
  5102. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  5103. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  5104. FLAGSET(Params.Options, soSynchronize))
  5105. {
  5106. GUIConfiguration->SynchronizeOptions =
  5107. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  5108. soSynchronize;
  5109. }
  5110. }
  5111. LocalDirectory = Params.LocalDirectory;
  5112. RemoteDirectory = Params.RemoteDirectory;
  5113. }
  5114. }
  5115. __finally
  5116. {
  5117. FSynchronizeController = NULL;
  5118. DebugAssert(FOnFeedSynchronizeError == NULL);
  5119. FOnFeedSynchronizeError = NULL;
  5120. }
  5121. return Result;
  5122. }
  5123. //---------------------------------------------------------------------------
  5124. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  5125. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  5126. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  5127. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  5128. TSynchronizeOptions * Options, bool Full)
  5129. {
  5130. // Keep-up-to-date mode
  5131. if (Terminal->Status == ssOpened)
  5132. {
  5133. try
  5134. {
  5135. int PParams = Params.Params;
  5136. if (!Full)
  5137. {
  5138. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  5139. if (FLAGSET(Params.Options, soRecurse))
  5140. {
  5141. PParams |= TTerminal::spSubDirs;
  5142. }
  5143. }
  5144. else
  5145. {
  5146. // if keepuptodate is non-recursive,
  5147. // full sync before has to be non-recursive as well
  5148. if (FLAGCLEAR(Params.Options, soRecurse))
  5149. {
  5150. PParams |= TTerminal::spNoRecurse;
  5151. }
  5152. }
  5153. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5154. PParams, Checklist, Options);
  5155. }
  5156. catch (Exception & E)
  5157. {
  5158. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  5159. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5160. {
  5161. // noop, already logged in MoreMessageDialog
  5162. }
  5163. else
  5164. {
  5165. ShowExtendedExceptionEx(Terminal, &E);
  5166. throw;
  5167. }
  5168. }
  5169. }
  5170. }
  5171. //---------------------------------------------------------------------------
  5172. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5173. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5174. const UnicodeString ErrorStr)
  5175. {
  5176. if (!Directory.IsEmpty())
  5177. {
  5178. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5179. }
  5180. else
  5181. {
  5182. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5183. }
  5184. }
  5185. //---------------------------------------------------------------------------
  5186. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5187. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5188. {
  5189. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5190. {
  5191. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5192. }
  5193. else
  5194. {
  5195. TMessageParams Params(mpNeverAskAgainCheck);
  5196. unsigned int Result = MessageDialog(
  5197. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5198. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5199. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5200. {
  5201. MaxDirectories *= 2;
  5202. if (Result == qaNeverAskAgain)
  5203. {
  5204. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5205. }
  5206. }
  5207. else
  5208. {
  5209. Abort();
  5210. }
  5211. }
  5212. }
  5213. //---------------------------------------------------------------------------
  5214. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5215. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5216. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5217. TSynchronizeOptions * Options)
  5218. {
  5219. // Keep-up-to-date mode
  5220. DebugAssert(!FAutoOperation);
  5221. void * BatchStorage;
  5222. BatchStart(BatchStorage);
  5223. FAutoOperation = true;
  5224. bool AnyOperation = false;
  5225. TDateTime StartTime = Now();
  5226. TSynchronizeChecklist * AChecklist = NULL;
  5227. try
  5228. {
  5229. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5230. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5231. {
  5232. FSynchronizeProgressForm->Start();
  5233. }
  5234. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5235. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5236. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5237. Options);
  5238. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5239. SAFE_DESTROY(FSynchronizeProgressForm);
  5240. AnyOperation = (AChecklist->CheckedCount > 0);
  5241. if (AnyOperation)
  5242. {
  5243. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5244. CreateProgressForm(&SynchronizeProgress);
  5245. try
  5246. {
  5247. Terminal->SynchronizeApply(
  5248. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5249. SynchronizeProcessedItem, NULL, NULL, NULL);
  5250. }
  5251. __finally
  5252. {
  5253. DestroyProgressForm();
  5254. }
  5255. }
  5256. }
  5257. __finally
  5258. {
  5259. if (Checklist == NULL)
  5260. {
  5261. delete AChecklist;
  5262. }
  5263. else
  5264. {
  5265. *Checklist = AChecklist;
  5266. }
  5267. FAutoOperation = false;
  5268. SAFE_DESTROY(FSynchronizeProgressForm);
  5269. BatchEnd(BatchStorage);
  5270. ReloadLocalDirectory();
  5271. if (AnyOperation)
  5272. {
  5273. OperationComplete(StartTime);
  5274. }
  5275. }
  5276. }
  5277. //---------------------------------------------------------------------------
  5278. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5279. {
  5280. // can be called from command line
  5281. return Visible &&
  5282. ((DirView(osRemote)->SelCount > 0) ||
  5283. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5284. }
  5285. //---------------------------------------------------------------------------
  5286. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5287. {
  5288. LogSynchronizeEvent(Terminal, Message);
  5289. }
  5290. //---------------------------------------------------------------------------
  5291. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5292. int Params, TSynchronizeOptions & Options)
  5293. {
  5294. DebugAssert(!IsLocalBrowserMode());
  5295. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5296. {
  5297. Options.Filter = new TStringList();
  5298. Options.Filter->CaseSensitive = false;
  5299. Options.Filter->Duplicates = Types::dupAccept;
  5300. if (DirView(osRemote)->SelCount > 0)
  5301. {
  5302. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5303. }
  5304. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5305. {
  5306. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5307. }
  5308. Options.Filter->Sort();
  5309. }
  5310. }
  5311. //---------------------------------------------------------------------------
  5312. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5313. {
  5314. TCopyParamType Defaults;
  5315. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5316. ConfigStorage->AccessMode = smRead;
  5317. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5318. {
  5319. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5320. }
  5321. ConfigStorage.reset(NULL);
  5322. std::unique_ptr<TStringList> Options(new TStringList());
  5323. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5324. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5325. UnicodeString Result;
  5326. if (Options->Count > 0)
  5327. {
  5328. Result =
  5329. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5330. }
  5331. return Result;
  5332. }
  5333. //---------------------------------------------------------------------------
  5334. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5335. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5336. {
  5337. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5338. UnicodeString AdditionalParams =
  5339. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5340. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5341. SerializeCopyParamForCommandLine(CopyParams),
  5342. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5343. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options));
  5344. ExecuteNewInstance(SessionName, AdditionalParams);
  5345. }
  5346. //---------------------------------------------------------------------------
  5347. struct TSynchronizeParams
  5348. {
  5349. UnicodeString LocalDirectory;
  5350. UnicodeString RemoteDirectory;
  5351. TSynchronizeMode Mode;
  5352. TSynchronizeChecklist * Checklist;
  5353. int Params;
  5354. TCopyParamType * CopyParam;
  5355. TDateTime CollectElapsed;
  5356. TDateTime * StartTime;
  5357. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5358. };
  5359. //---------------------------------------------------------------------------
  5360. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5361. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5362. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5363. {
  5364. DebugAssert(!FAutoOperation);
  5365. void * BatchStorage;
  5366. BatchStart(BatchStorage);
  5367. FAutoOperation = true;
  5368. TFileOperationStatistics Statistics;
  5369. TDateTime Start = Now();
  5370. try
  5371. {
  5372. Params.OnProcessedItem = OnProcessedItem;
  5373. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5374. CreateProgressForm(&SynchronizeProgress);
  5375. Terminal->SynchronizeApply(
  5376. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5377. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5378. &Statistics);
  5379. }
  5380. __finally
  5381. {
  5382. Params.OnProcessedItem = NULL;
  5383. FAutoOperation = false;
  5384. DestroyProgressForm();
  5385. BatchEnd(BatchStorage);
  5386. ReloadLocalDirectory();
  5387. }
  5388. if (WinConfiguration->SynchronizeSummary)
  5389. {
  5390. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5391. // The statistics should be 0 anyway in this case
  5392. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5393. {
  5394. Message += L"\n";
  5395. if (Statistics.FilesUploaded > 0)
  5396. {
  5397. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5398. }
  5399. if (Statistics.FilesDownloaded > 0)
  5400. {
  5401. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5402. }
  5403. if (Statistics.FilesDeletedLocal > 0)
  5404. {
  5405. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5406. }
  5407. if (Statistics.FilesDeletedRemote > 0)
  5408. {
  5409. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5410. }
  5411. }
  5412. TDateTime Elapsed = (Now() - Start);
  5413. Message +=
  5414. L"\n" +
  5415. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5416. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5417. TMessageParams Params(mpNeverAskAgainCheck);
  5418. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5419. if (Result == qaNeverAskAgain)
  5420. {
  5421. WinConfiguration->SynchronizeSummary = false;
  5422. }
  5423. }
  5424. }
  5425. //---------------------------------------------------------------------------
  5426. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5427. {
  5428. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5429. {
  5430. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5431. }
  5432. // Not set in keep-up-to-date mode - Synchronize() method
  5433. if (Token != NULL)
  5434. {
  5435. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5436. if (Params.OnProcessedItem != NULL)
  5437. {
  5438. Params.OnProcessedItem(NULL, ChecklistItem);
  5439. }
  5440. }
  5441. }
  5442. //---------------------------------------------------------------------------
  5443. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5444. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5445. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5446. {
  5447. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5448. *Params.StartTime = Now();
  5449. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5450. }
  5451. //---------------------------------------------------------------------------
  5452. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5453. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5454. {
  5455. // terminal can be already closed (e.g. dropped connection)
  5456. if (Terminal != NULL)
  5457. {
  5458. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5459. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5460. }
  5461. }
  5462. //---------------------------------------------------------------------------
  5463. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5464. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5465. {
  5466. DebugAssert(!IsLocalBrowserMode());
  5467. TAutoBatch AutoBatch(this);
  5468. TAutoFlag AutoOperationFlag(FAutoOperation);
  5469. if (Side == osRemote)
  5470. {
  5471. std::unique_ptr<TStrings> FileList(new TStringList());
  5472. FileList->AddObject(FileName, RemoteFile);
  5473. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5474. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5475. RemoteDirView->SaveSelection();
  5476. RemoteDirView->SaveSelectedNames();
  5477. try
  5478. {
  5479. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5480. }
  5481. catch(...)
  5482. {
  5483. RemoteDirView->DiscardSavedSelection();
  5484. throw;
  5485. }
  5486. RemoteDirView->RestoreSelection();
  5487. }
  5488. else if (DebugAlwaysTrue(Side == osLocal))
  5489. {
  5490. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5491. {
  5492. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5493. }
  5494. UnicodeString Directory = ExtractFileDir(FileName);
  5495. ReloadLocalDirectory(Directory);
  5496. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5497. if (!SamePaths(Directory, NewDirectory))
  5498. {
  5499. ReloadLocalDirectory(NewDirectory);
  5500. }
  5501. }
  5502. }
  5503. //---------------------------------------------------------------------------
  5504. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5505. {
  5506. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5507. if (Side == osLocal)
  5508. {
  5509. if (Action == TSynchronizeChecklist::saDownloadNew)
  5510. {
  5511. OpenFolderInExplorer(LocalPath);
  5512. }
  5513. else
  5514. {
  5515. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5516. }
  5517. }
  5518. else if (DebugAlwaysTrue(Side == osRemote))
  5519. {
  5520. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5521. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5522. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5523. if (!LocalPath.IsEmpty())
  5524. {
  5525. SessionData->LocalDirectory = LocalPath;
  5526. }
  5527. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5528. ExecuteNewInstance(SessionName, FORMAT(L"%s=%s", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5529. }
  5530. }
  5531. //---------------------------------------------------------------------------
  5532. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5533. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5534. const TCopyParamType * CopyParams)
  5535. {
  5536. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5537. UnicodeString AdditionalParams =
  5538. FORMAT(L"%s%s %s // \"%s\" \"%s\" %d %d", (
  5539. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5540. SerializeCopyParamForCommandLine(CopyParams),
  5541. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5542. LocalDirectory, RemoteDirectory, Mode, Params));
  5543. ExecuteNewInstance(SessionName, AdditionalParams);
  5544. }
  5545. //---------------------------------------------------------------------------
  5546. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5547. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5548. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5549. {
  5550. int Result;
  5551. bool SaveSettings = false;
  5552. int Options =
  5553. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5554. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5555. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5556. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5557. bool Continue =
  5558. (UseDefaults == 0) ||
  5559. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5560. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5561. if (Continue)
  5562. {
  5563. Configuration->Usage->Inc(L"Synchronizations");
  5564. UpdateCopyParamCounters(CopyParam);
  5565. TSynchronizeOptions SynchronizeOptions;
  5566. GetSynchronizeOptions(Params, SynchronizeOptions);
  5567. if (SaveSettings)
  5568. {
  5569. GUIConfiguration->SynchronizeParams = Params;
  5570. }
  5571. else
  5572. {
  5573. SaveMode = false;
  5574. }
  5575. TDateTime StartTime = Now();
  5576. TSynchronizeChecklist * Checklist = NULL;
  5577. try
  5578. {
  5579. DebugAssert(!FAutoOperation);
  5580. FAutoOperation = true;
  5581. try
  5582. {
  5583. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5584. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5585. int Files = -1;
  5586. if (DataList->Count >= 1)
  5587. {
  5588. Files = StrToIntDef(DataList->Strings[0], Files);
  5589. }
  5590. else
  5591. {
  5592. DataList->Add(UnicodeString());
  5593. }
  5594. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5595. FSynchronizeProgressForm->Start();
  5596. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5597. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5598. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5599. &SynchronizeOptions);
  5600. if (Terminal->SessionData->CacheDirectories)
  5601. {
  5602. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5603. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5604. }
  5605. }
  5606. __finally
  5607. {
  5608. FAutoOperation = false;
  5609. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  5610. SAFE_DESTROY(FSynchronizeProgressForm);
  5611. }
  5612. TSynchronizeParams SynchronizeParams;
  5613. SynchronizeParams.LocalDirectory = LocalDirectory;
  5614. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5615. SynchronizeParams.Mode = Mode;
  5616. SynchronizeParams.CopyParam = &CopyParam;
  5617. SynchronizeParams.Params = Params;
  5618. SynchronizeParams.Checklist = Checklist;
  5619. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5620. SynchronizeParams.StartTime = &StartTime;
  5621. SynchronizeParams.OnProcessedItem = NULL;
  5622. Result = Checklist->Count;
  5623. if (Checklist->Count > 0)
  5624. {
  5625. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5626. {
  5627. if (!DoSynchronizeChecklistDialog(
  5628. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5629. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  5630. {
  5631. Result = -1;
  5632. }
  5633. }
  5634. else
  5635. {
  5636. FullSynchronize(SynchronizeParams, NULL, NULL);
  5637. }
  5638. }
  5639. else
  5640. {
  5641. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5642. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5643. }
  5644. }
  5645. __finally
  5646. {
  5647. delete Checklist;
  5648. }
  5649. OperationComplete(StartTime);
  5650. }
  5651. else
  5652. {
  5653. Result = -1;
  5654. }
  5655. return Result;
  5656. }
  5657. //---------------------------------------------------------------------------
  5658. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5659. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5660. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5661. {
  5662. if (Collect)
  5663. {
  5664. DebugAssert(FSynchronizeProgressForm != NULL);
  5665. if (!FSynchronizeProgressForm->Started)
  5666. {
  5667. FSynchronizeProgressForm->Start();
  5668. }
  5669. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5670. SetTaskbarListProgressValue(CompareProgress);
  5671. }
  5672. else
  5673. {
  5674. DebugAssert(FSynchronizeProgressForm == NULL);
  5675. DebugAssert(FProgressForm != NULL);
  5676. }
  5677. }
  5678. //---------------------------------------------------------------------------
  5679. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5680. {
  5681. UnicodeString FullFileName = AbsolutePath(Terminal->CurrentDirectory, FileName);
  5682. TRemoteFile * File = NULL;
  5683. Terminal->ReadFile(FullFileName, File);
  5684. if (File != NULL)
  5685. {
  5686. std::unique_ptr<TRemoteFile> FileOwner(File);
  5687. TAutoFlag Flag(FStandaloneEditing);
  5688. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5689. Application->ShowMainForm = false;
  5690. Application->Run();
  5691. }
  5692. }
  5693. //---------------------------------------------------------------------------
  5694. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory(TOperationSide)
  5695. {
  5696. DebugFail();
  5697. }
  5698. //---------------------------------------------------------------------------
  5699. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5700. {
  5701. DebugAssert(!IsLocalBrowserMode()); // untested
  5702. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5703. SessionData->Assign(Terminal->SessionData);
  5704. UpdateSessionData(SessionData.get());
  5705. Terminal->UpdateSessionCredentials(SessionData.get());
  5706. if (!Terminal->SessionData->HasSessionName())
  5707. {
  5708. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5709. // once IsWorkspace is cleared
  5710. SessionData->Name = UnicodeString();
  5711. SessionData->IsWorkspace = false;
  5712. }
  5713. return SessionData.release();
  5714. }
  5715. //---------------------------------------------------------------------------
  5716. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5717. {
  5718. TSessionData * SessionData = CloneCurrentSessionData();
  5719. try
  5720. {
  5721. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5722. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5723. }
  5724. __finally
  5725. {
  5726. delete SessionData;
  5727. }
  5728. }
  5729. //---------------------------------------------------------------------------
  5730. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5731. {
  5732. TTerminalManager * Manager = TTerminalManager::Instance();
  5733. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5734. if (DebugAlwaysTrue(ManagedSession != NULL))
  5735. {
  5736. // Update ManagedSession->StateData
  5737. UpdateSession(ManagedSession);
  5738. }
  5739. Manager->SaveWorkspace(DataList.get());
  5740. return DataList.release();
  5741. }
  5742. //---------------------------------------------------------------------------
  5743. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5744. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5745. {
  5746. WinConfiguration->LastWorkspace = Name;
  5747. if (!SavePasswords)
  5748. {
  5749. for (int Index = 0; Index < DataList->Count; Index++)
  5750. {
  5751. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5752. if (SessionData->Link.IsEmpty())
  5753. {
  5754. SessionData->ClearSessionPasswords();
  5755. }
  5756. }
  5757. }
  5758. StoredSessions->NewWorkspace(Name, DataList);
  5759. // modified only
  5760. StoredSessions->Save(false, Explicit);
  5761. }
  5762. //---------------------------------------------------------------------------
  5763. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5764. {
  5765. return
  5766. DefaultStr(WinConfiguration->LastWorkspace,
  5767. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5768. }
  5769. //---------------------------------------------------------------------------
  5770. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5771. {
  5772. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5773. bool AnyNonStoredSessionWithPassword = false;
  5774. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5775. bool AllNonStoredSessionsAnonymous = true;
  5776. TTerminalManager * Manager = TTerminalManager::Instance();
  5777. for (int Index = 0; Index < DataList->Count; Index++)
  5778. {
  5779. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5780. if (SessionData->Link.IsEmpty())
  5781. {
  5782. if (SessionData->HasAnySessionPassword())
  5783. {
  5784. AnyNonStoredSessionWithPassword = true;
  5785. if (!Manager->Sessions[Index]->SessionData->IsWorkspace)
  5786. {
  5787. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5788. }
  5789. }
  5790. if (!SameText(SessionData->UserName, AnonymousUserName))
  5791. {
  5792. AllNonStoredSessionsAnonymous = false;
  5793. }
  5794. }
  5795. }
  5796. bool SavePasswords;
  5797. bool * PSavePasswords;
  5798. bool NotRecommendedSavingPasswords =
  5799. !CustomWinConfiguration->UseMasterPassword &&
  5800. !AllNonStoredSessionsAnonymous;
  5801. if (Configuration->DisablePasswordStoring ||
  5802. !AnyNonStoredSessionWithPassword)
  5803. {
  5804. PSavePasswords = NULL;
  5805. SavePasswords = false;
  5806. }
  5807. else
  5808. {
  5809. PSavePasswords = &SavePasswords;
  5810. SavePasswords =
  5811. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5812. !NotRecommendedSavingPasswords;
  5813. }
  5814. UnicodeString Name = WorkspaceName();
  5815. bool CreateShortcut = false;
  5816. bool Result =
  5817. DoSaveWorkspaceDialog(
  5818. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5819. EnableAutoSave);
  5820. if (Result)
  5821. {
  5822. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5823. if (CreateShortcut)
  5824. {
  5825. TOperationVisualizer Visualizer;
  5826. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5827. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5828. }
  5829. if (EnableAutoSave)
  5830. {
  5831. WinConfiguration->AutoSaveWorkspace = true;
  5832. WinConfiguration->AutoWorkspace = Name;
  5833. if (PSavePasswords != NULL)
  5834. {
  5835. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5836. }
  5837. }
  5838. }
  5839. return Result;
  5840. }
  5841. //---------------------------------------------------------------------------
  5842. void __fastcall TCustomScpExplorerForm::UpdateSession(TManagedTerminal * Session)
  5843. {
  5844. DebugAssert(IsLocalBrowserMode() == Session->LocalBrowser);
  5845. DebugAssert(Session == ManagedSession);
  5846. if (!IsLocalBrowserMode())
  5847. {
  5848. SAFE_DESTROY(Session->RemoteExplorerState);
  5849. if (WinConfiguration->PreservePanelState)
  5850. {
  5851. Session->RemoteExplorerState = RemoteDirView->SaveState();
  5852. }
  5853. }
  5854. UpdateSessionData(Session->StateData);
  5855. }
  5856. //---------------------------------------------------------------------------
  5857. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5858. {
  5859. // Keep in sync with TSessionData::CopyStateData
  5860. DebugAssert(Data != NULL);
  5861. // This is inconsistent with how color (for example) is handled.
  5862. Data->Name = ManagedSession->SessionData->Name;
  5863. if (!IsLocalBrowserMode())
  5864. {
  5865. // cannot use RemoteDirView->Path, because it is empty if connection
  5866. // was already closed
  5867. // also only peek, we may not be connected at all atm
  5868. // so make sure we do not try retrieving current directory from the server
  5869. // (particularly with FTP)
  5870. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5871. if (!ACurrentDirectory.IsEmpty())
  5872. {
  5873. Data->RemoteDirectory = ACurrentDirectory;
  5874. }
  5875. }
  5876. Data->Color = SessionColor;
  5877. }
  5878. //---------------------------------------------------------------------------
  5879. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5880. {
  5881. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5882. DebugAssert(Toolbar != NULL);
  5883. for (int i = 0; i < Toolbar->Items->Count; i++)
  5884. {
  5885. TTBXCustomDropDownItem * DropDownItem;
  5886. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5887. if (DropDownItem != NULL)
  5888. {
  5889. ToolbarItemResize(DropDownItem,
  5890. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5891. Toolbar->NonClientWidth);
  5892. break;
  5893. }
  5894. }
  5895. }
  5896. //---------------------------------------------------------------------------
  5897. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5898. {
  5899. Item->EditWidth = Width;
  5900. }
  5901. //---------------------------------------------------------------------------
  5902. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5903. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5904. {
  5905. for (int i = 0; i < Toolbar->Items->Count; i++)
  5906. {
  5907. TTBXCustomDropDownItem * DropDownItem;
  5908. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5909. if (DropDownItem != NULL)
  5910. {
  5911. ASize.x -= DropDownItem->EditWidth;
  5912. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5913. }
  5914. }
  5915. }
  5916. //---------------------------------------------------------------------------
  5917. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5918. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5919. {
  5920. if (WinConfiguration->DDWarnLackOfTempSpace)
  5921. {
  5922. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5923. if (!ADrive.IsEmpty())
  5924. {
  5925. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5926. DebugAssert(RequiredSpace >= 0);
  5927. __int64 RequiredWithReserve;
  5928. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5929. if (FreeSpace < RequiredWithReserve)
  5930. {
  5931. unsigned int Result;
  5932. TMessageParams Params(mpNeverAskAgainCheck);
  5933. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5934. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5935. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5936. if (Result == qaNeverAskAgain)
  5937. {
  5938. WinConfiguration->DDWarnLackOfTempSpace = false;
  5939. }
  5940. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5941. }
  5942. }
  5943. }
  5944. }
  5945. //---------------------------------------------------------------------------
  5946. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5947. {
  5948. DoOpenDirectoryDialog(odAddBookmark, Side);
  5949. }
  5950. //---------------------------------------------------------------------------
  5951. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5952. {
  5953. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5954. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  5955. try
  5956. {
  5957. TCustomDirView * DView = DirView(Side);
  5958. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5959. {
  5960. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5961. }
  5962. }
  5963. catch (...)
  5964. {
  5965. delete VisitedDirectories;
  5966. throw;
  5967. }
  5968. return VisitedDirectories;
  5969. }
  5970. //---------------------------------------------------------------------------
  5971. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5972. TOpenDirectoryMode Mode, TOperationSide Side)
  5973. {
  5974. bool Continue = true;
  5975. if (Mode == odAddBookmark)
  5976. {
  5977. TMessageParams Params(mpNeverAskAgainCheck);
  5978. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5979. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5980. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  5981. unsigned int Answer =
  5982. MessageDialog(Message,
  5983. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  5984. if (Answer == qaNeverAskAgain)
  5985. {
  5986. Continue = true;
  5987. WinConfiguration->UseSharedBookmarks = true;
  5988. }
  5989. else if (Answer == qaOK)
  5990. {
  5991. Continue = true;
  5992. WinConfiguration->UseSharedBookmarks = false;
  5993. }
  5994. else
  5995. {
  5996. Continue = false;
  5997. }
  5998. }
  5999. if (Continue)
  6000. {
  6001. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  6002. try
  6003. {
  6004. UnicodeString Name = DirView(Side)->PathName;
  6005. bool Repeat;
  6006. do
  6007. {
  6008. Repeat = false;
  6009. TOperationSide BookmarkSide = IsSideLocalBrowser(Side) ? osLocal : osRemote;
  6010. if (::DoOpenDirectoryDialog(Mode, BookmarkSide, Name, VisitedDirectories, Terminal,
  6011. // do not allow switching to location profiles,
  6012. // if we are not connected
  6013. HasDirView[osLocal] && (Terminal != NULL)))
  6014. {
  6015. TWindowLock Lock(this);
  6016. if (!TryOpenDirectory(Side, Name))
  6017. {
  6018. Repeat = true;
  6019. Mode = odBrowse;
  6020. }
  6021. }
  6022. }
  6023. while (Repeat);
  6024. }
  6025. __finally
  6026. {
  6027. delete VisitedDirectories;
  6028. }
  6029. }
  6030. }
  6031. //---------------------------------------------------------------------------
  6032. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  6033. {
  6034. DebugAssert(!Terminal->CommandSessionOpened);
  6035. return TTerminalManager::Instance()->ConnectTerminal(Terminal->CommandSession);
  6036. }
  6037. //---------------------------------------------------------------------------
  6038. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  6039. {
  6040. bool Result = Terminal->IsCapable[Capability] ||
  6041. Terminal->CommandSessionOpened;
  6042. if (!Result)
  6043. {
  6044. DebugAssert(Terminal->IsCapable[fcSecondaryShell]);
  6045. if (!GUIConfiguration->ConfirmCommandSession)
  6046. {
  6047. Result = true;
  6048. }
  6049. else
  6050. {
  6051. TMessageParams Params(mpNeverAskAgainCheck);
  6052. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  6053. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  6054. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  6055. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  6056. if (Answer == qaNeverAskAgain)
  6057. {
  6058. GUIConfiguration->ConfirmCommandSession = false;
  6059. Result = true;
  6060. }
  6061. else if (Answer == qaOK)
  6062. {
  6063. Result = true;
  6064. }
  6065. }
  6066. if (Result)
  6067. {
  6068. Result = CommandSessionFallback();
  6069. }
  6070. }
  6071. return Result;
  6072. }
  6073. //---------------------------------------------------------------------------
  6074. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  6075. {
  6076. if (EnsureCommandSessionFallback(fcAnyCommand))
  6077. {
  6078. DoConsoleDialog(Terminal, Command);
  6079. }
  6080. }
  6081. //---------------------------------------------------------------------------
  6082. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  6083. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  6084. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  6085. {
  6086. if (!IsSideLocalBrowser(osRemote) &&
  6087. IsFileControl(DropSourceControl, osRemote) &&
  6088. (FDDExtMapFile != NULL))
  6089. {
  6090. Accept = true;
  6091. }
  6092. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  6093. DebugAssert(FDDTargetControl != NULL);
  6094. }
  6095. //---------------------------------------------------------------------------
  6096. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  6097. _di_IDataObject DataObj, int KeyState,
  6098. const TPoint & Point, int & Effect, bool & Accept)
  6099. {
  6100. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  6101. }
  6102. //---------------------------------------------------------------------------
  6103. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  6104. _di_IDataObject DataObj, int KeyState,
  6105. const TPoint & Point, int & Effect, bool & Accept)
  6106. {
  6107. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  6108. }
  6109. //---------------------------------------------------------------------------
  6110. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  6111. TObject * Sender)
  6112. {
  6113. DebugUsedParam(Sender);
  6114. DebugAssert(FDDTargetControl == Sender);
  6115. FDDTargetControl = NULL;
  6116. }
  6117. //---------------------------------------------------------------------------
  6118. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  6119. {
  6120. FileControlDDDragLeave(SessionsPageControl);
  6121. }
  6122. //---------------------------------------------------------------------------
  6123. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  6124. {
  6125. FileControlDDDragLeave(QueueView3);
  6126. }
  6127. //---------------------------------------------------------------------------
  6128. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  6129. {
  6130. DebugAssert(!IsSideLocalBrowser(Side));
  6131. DebugUsedParam(Side);
  6132. bool Edit = false;
  6133. TRemoteFile * File = NULL;
  6134. UnicodeString FileName;
  6135. UnicodeString PointTo;
  6136. bool SymbolicLink = true;
  6137. if (RemoteDirView->ItemFocused)
  6138. {
  6139. DebugAssert(RemoteDirView->ItemFocused->Data);
  6140. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  6141. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  6142. if (Edit)
  6143. {
  6144. FileName = File->FileName;
  6145. PointTo = File->LinkTo;
  6146. }
  6147. else
  6148. {
  6149. PointTo = File->FileName;
  6150. }
  6151. }
  6152. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  6153. Terminal->IsCapable[fcHardLink]))
  6154. {
  6155. Configuration->Usage->Inc(L"RemoteLinksCreated");
  6156. if (Edit)
  6157. {
  6158. DebugAssert(File->FileName == FileName);
  6159. int Params = dfNoRecursive;
  6160. Terminal->ExceptionOnFail = true;
  6161. try
  6162. {
  6163. Terminal->DeleteFile(L"", File, &Params);
  6164. }
  6165. __finally
  6166. {
  6167. Terminal->ExceptionOnFail = false;
  6168. }
  6169. }
  6170. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6171. }
  6172. }
  6173. //---------------------------------------------------------------------------
  6174. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6175. {
  6176. return
  6177. (IsSideLocalBrowser(Side) ||
  6178. (HasActiveTerminal() &&
  6179. Terminal->ResolvingSymlinks &&
  6180. Terminal->IsCapable[fcSymbolicLink]));
  6181. }
  6182. //---------------------------------------------------------------------------
  6183. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6184. {
  6185. return
  6186. !IsSideLocalBrowser(FCurrentSide) &&
  6187. (RemoteDirView->ItemFocused != NULL) &&
  6188. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6189. Terminal->SessionData->ResolveSymlinks;
  6190. }
  6191. //---------------------------------------------------------------------------
  6192. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6193. {
  6194. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6195. FForceExecution = true;
  6196. try
  6197. {
  6198. DirView(osCurrent)->ExecuteCurrentFile();
  6199. }
  6200. __finally
  6201. {
  6202. FForceExecution = false;
  6203. }
  6204. }
  6205. //---------------------------------------------------------------------------
  6206. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6207. {
  6208. TEditorData ExternalEditor;
  6209. ExternalEditor.Editor = edExternal;
  6210. bool Remember = false;
  6211. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6212. {
  6213. if (Remember)
  6214. {
  6215. TEditorList * EditorList = new TEditorList;
  6216. try
  6217. {
  6218. *EditorList = *WinConfiguration->EditorList;
  6219. bool Found = false;
  6220. int i = 0;
  6221. while (!Found && (i < EditorList->Count))
  6222. {
  6223. const TEditorPreferences * Editor = EditorList->Editors[i];
  6224. if ((Editor->Data->Editor == edExternal) &&
  6225. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6226. {
  6227. Found = true;
  6228. }
  6229. i++;
  6230. }
  6231. if (!Found)
  6232. {
  6233. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6234. WinConfiguration->EditorList = EditorList;
  6235. }
  6236. }
  6237. __finally
  6238. {
  6239. delete EditorList;
  6240. }
  6241. }
  6242. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6243. }
  6244. }
  6245. //---------------------------------------------------------------------------
  6246. void __fastcall TCustomScpExplorerForm::DetachTerminal(TObject * ATerminal)
  6247. {
  6248. FEditorManager->ProcessFiles(FileTerminalRemoved, ATerminal);
  6249. if (FFileFindTerminal == ATerminal)
  6250. {
  6251. FFileFindTerminal = NULL;
  6252. HideFileFindDialog();
  6253. }
  6254. if (FClipboardTerminal == ATerminal)
  6255. {
  6256. ClipboardClear(); // implies ClipboardStop
  6257. }
  6258. }
  6259. //---------------------------------------------------------------------------
  6260. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6261. {
  6262. DetachTerminal(Sender);
  6263. }
  6264. //---------------------------------------------------------------------------
  6265. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6266. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6267. {
  6268. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6269. DebugAssert(Terminal != NULL);
  6270. if (Data->Terminal == Terminal)
  6271. {
  6272. Data->Terminal = NULL;
  6273. }
  6274. }
  6275. //---------------------------------------------------------------------------
  6276. TManagedTerminal * TCustomScpExplorerForm::GetReplacementForLastSession()
  6277. {
  6278. return NULL;
  6279. }
  6280. //---------------------------------------------------------------------------
  6281. void __fastcall TCustomScpExplorerForm::LastTerminalClosed()
  6282. {
  6283. UpdateControls();
  6284. SessionColor = TColor(0);
  6285. UpdateRemotePathComboBox(false);
  6286. NeedSession(false);
  6287. }
  6288. //---------------------------------------------------------------------------
  6289. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6290. {
  6291. try
  6292. {
  6293. // Cache, as the login dialog can change its value.
  6294. // See also DoShow. Also see TTerminalManager::NewSession.
  6295. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6296. try
  6297. {
  6298. if (ShowLogin)
  6299. {
  6300. bool ReloadSessions = !Startup;
  6301. TTerminalManager::Instance()->NewSession(L"", ReloadSessions, this, true);
  6302. }
  6303. }
  6304. __finally
  6305. {
  6306. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6307. // (so there was no chance for the user to open any session yet)
  6308. if ((ShowLogin || !Startup) &&
  6309. !WinConfiguration->KeepOpenWhenNoSession &&
  6310. // this rules out the implicit Commander's local-local browser
  6311. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6312. {
  6313. TerminateApplication();
  6314. }
  6315. }
  6316. }
  6317. catch (EAbort &)
  6318. {
  6319. // swallow
  6320. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6321. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6322. }
  6323. }
  6324. //---------------------------------------------------------------------------
  6325. void __fastcall TCustomScpExplorerForm::SessionListChanged()
  6326. {
  6327. TTerminalManager * Manager = TTerminalManager::Instance();
  6328. DebugAssert(!Manager->Updating);
  6329. TStrings * SessionList = Manager->SessionList;
  6330. int ActiveSessionIndex = Manager->ActiveSessionIndex;
  6331. Configuration->Usage->SetMax(L"MaxOpenedSessions", SessionList->Count);
  6332. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6333. try
  6334. {
  6335. while ((SessionsPageControl->PageCount > SessionList->Count + 1) ||
  6336. // Clear the design time unthemed tab
  6337. ((SessionsPageControl->PageCount > 0) &&
  6338. (dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[SessionsPageControl->PageCount - 1]) == NULL)))
  6339. {
  6340. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6341. }
  6342. for (int Index = 0; Index <= SessionList->Count; Index++)
  6343. {
  6344. TThemeTabSheet * TabSheet;
  6345. if (Index >= SessionsPageControl->PageCount)
  6346. {
  6347. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6348. TabSheet->PageControl = SessionsPageControl;
  6349. }
  6350. else
  6351. {
  6352. TabSheet = DebugNotNull(dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[Index]));
  6353. }
  6354. bool IsSessionTab = (Index < SessionList->Count);
  6355. if (IsSessionTab)
  6356. {
  6357. TTerminal * Terminal = dynamic_cast<TTerminal *>(SessionList->Objects[Index]);
  6358. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6359. UpdateSessionTab(TabSheet);
  6360. }
  6361. else
  6362. {
  6363. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  6364. TabSheet->Tag = 0; // not really needed
  6365. TabSheet->Shadowed = false;
  6366. TabSheet->ShowCloseButton = false;
  6367. // 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)
  6368. UpdateNewSessionTab();
  6369. }
  6370. }
  6371. }
  6372. __finally
  6373. {
  6374. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6375. }
  6376. SessionsPageControl->ActivePageIndex = ActiveSessionIndex;
  6377. }
  6378. //---------------------------------------------------------------------------
  6379. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  6380. {
  6381. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6382. DebugAssert(TabSheet->ImageIndex == 0);
  6383. TabSheet->Caption =
  6384. WinConfiguration->SelectiveToolbarText ?
  6385. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  6386. UnicodeString();
  6387. }
  6388. //---------------------------------------------------------------------------
  6389. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabSession(TTabSheet * TabSheet)
  6390. {
  6391. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6392. }
  6393. //---------------------------------------------------------------------------
  6394. UnicodeString TCustomScpExplorerForm::GetLocalBrowserSessionTitle(TManagedTerminal *)
  6395. {
  6396. DebugFail();
  6397. return UnicodeString();
  6398. }
  6399. //---------------------------------------------------------------------------
  6400. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  6401. {
  6402. if (DebugAlwaysTrue(TabSheet != NULL))
  6403. {
  6404. TManagedTerminal * ASession = GetSessionTabSession(TabSheet);
  6405. if (DebugAlwaysTrue(ASession != NULL))
  6406. {
  6407. TColor Color = (ASession == Terminal) ? FSessionColor : ASession->StateData->Color;
  6408. if (ASession->LocalBrowser)
  6409. {
  6410. TabSheet->ImageIndex = FLocalBrowserTabImageIndex;
  6411. }
  6412. else
  6413. {
  6414. TabSheet->ImageIndex = AddSessionColor(Color);
  6415. }
  6416. TTerminalManager * Manager = TTerminalManager::Instance();
  6417. UnicodeString TabCaption = Manager->GetSessionTitle(ASession, true);
  6418. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  6419. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  6420. {
  6421. ThemeTabSheet->Shadowed = !ASession->Active && !ASession->LocalBrowser;
  6422. ThemeTabSheet->ShowCloseButton = CanCloseSession(ASession);
  6423. if (ThemeTabSheet->ShowCloseButton)
  6424. {
  6425. TabCaption = SessionsPageControl->FormatCaptionWithCloseButton(TabCaption);
  6426. }
  6427. }
  6428. TabSheet->Caption = TabCaption;
  6429. }
  6430. }
  6431. }
  6432. //---------------------------------------------------------------------------
  6433. bool TCustomScpExplorerForm::CanCloseSession(TManagedTerminal * Session)
  6434. {
  6435. return !Session->LocalBrowser || (TTerminalManager::Instance()->Count > 1);
  6436. }
  6437. //---------------------------------------------------------------------------
  6438. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6439. {
  6440. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6441. TManagedTerminal * Session = GetSessionTabSession(SessionsPageControl->ActivePage);
  6442. bool Result = (Session != NULL);
  6443. if (Result)
  6444. {
  6445. TTerminalManager::Instance()->ActiveSession = Session;
  6446. }
  6447. else
  6448. {
  6449. try
  6450. {
  6451. NewSession();
  6452. }
  6453. __finally
  6454. {
  6455. SessionListChanged();
  6456. }
  6457. FSessionsPageControlNewSessionTime = Now();
  6458. }
  6459. return Result;
  6460. }
  6461. //---------------------------------------------------------------------------
  6462. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6463. {
  6464. SessionTabSwitched();
  6465. }
  6466. //---------------------------------------------------------------------------
  6467. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6468. {
  6469. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6470. DebugAssert(TransferList != NULL);
  6471. UnicodeString Name;
  6472. if (TransferList->ItemIndex <= 0)
  6473. {
  6474. Name = L"";
  6475. }
  6476. else
  6477. {
  6478. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6479. }
  6480. if (FCopyParamAutoSelected.IsEmpty())
  6481. {
  6482. // if previous preset was not autoselected, make new preset the "default"
  6483. FCopyParamDefault = Name;
  6484. }
  6485. GUIConfiguration->CopyParamCurrent = Name;
  6486. }
  6487. //---------------------------------------------------------------------------
  6488. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6489. {
  6490. // sanity check
  6491. bool ExistingPreset =
  6492. (FTransferListHoverIndex >= 0) &&
  6493. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6494. DebugAssert(ExistingPreset);
  6495. if (ExistingPreset)
  6496. {
  6497. HDC DC = GetDC(0);
  6498. TCanvas * Canvas = new TCanvas();
  6499. try
  6500. {
  6501. Canvas->Handle = DC;
  6502. Canvas->Font = GetToolbarFont(this);
  6503. UnicodeString Name;
  6504. if (FTransferListHoverIndex == 0)
  6505. {
  6506. Name = L"";
  6507. }
  6508. else
  6509. {
  6510. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6511. }
  6512. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6513. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6514. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6515. static_cast<TObject*>(GetComponent(fcTransferList)));
  6516. UnicodeString InfoStr =
  6517. GUIConfiguration->CopyParamPreset[Name].
  6518. GetInfoStr(L"; ",
  6519. FLAGMASK(HasActiveTerminal(), Terminal->UsableCopyParamAttrs(0).General));
  6520. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6521. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6522. {
  6523. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6524. {
  6525. InfoStr.SetLength(InfoStr.Length() - 1);
  6526. }
  6527. InfoStr += Ellipsis;
  6528. }
  6529. // UpdateCaption does not cause invalidation of whole submenu, while
  6530. // setting Caption property does.
  6531. // also it probably does not resize the label, even if necessary
  6532. // (we do not want that anyway)
  6533. TransferLabel->UpdateCaption(InfoStr);
  6534. }
  6535. __finally
  6536. {
  6537. Canvas->Handle = NULL;
  6538. ReleaseDC(0, DC);
  6539. delete Canvas;
  6540. }
  6541. }
  6542. }
  6543. //---------------------------------------------------------------------------
  6544. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6545. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6546. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6547. {
  6548. if (FTransferListHoverIndex != AHoverIndex)
  6549. {
  6550. FTransferListHoverIndex = AHoverIndex;
  6551. UpdateTransferLabel();
  6552. }
  6553. }
  6554. //---------------------------------------------------------------------------
  6555. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6556. {
  6557. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6558. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6559. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6560. {
  6561. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6562. {
  6563. if (!NonVisualDataModule->Busy)
  6564. {
  6565. OpenDirectory(GetSide(osCurrent));
  6566. }
  6567. Message.Result = 1;
  6568. }
  6569. else
  6570. {
  6571. TForm::Dispatch(&Message);
  6572. }
  6573. }
  6574. else
  6575. {
  6576. TForm::Dispatch(&Message);
  6577. }
  6578. }
  6579. //---------------------------------------------------------------------------
  6580. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6581. {
  6582. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6583. if ((FIgnoreNextDialogChar != 0) &&
  6584. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6585. {
  6586. Message.Result = 1;
  6587. }
  6588. else
  6589. {
  6590. TForm::Dispatch(&Message);
  6591. }
  6592. FIgnoreNextDialogChar = 0;
  6593. }
  6594. //---------------------------------------------------------------------------
  6595. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6596. {
  6597. // The four low-order bits are used internally by Windows
  6598. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6599. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6600. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6601. // 61730 - restore thru double click - undocumented
  6602. // 61490 - maximize thru double click - undocumented
  6603. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6604. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6605. {
  6606. SysResizing(Cmd);
  6607. }
  6608. TForm::Dispatch(&Message);
  6609. }
  6610. //---------------------------------------------------------------------------
  6611. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6612. {
  6613. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6614. // Also there no point returning TRUE as we are not able to
  6615. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6616. // Hence the process termination might be safer :)
  6617. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6618. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6619. {
  6620. Message.Result = FALSE;
  6621. }
  6622. else
  6623. {
  6624. Message.Result = TRUE;
  6625. }
  6626. // Do not call default handling as that triggers OnCloseQuery,
  6627. // where our implementation will popup confirmation dialogs, what we do not want,
  6628. // as per Vista guidelines:
  6629. // https://docs.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6630. }
  6631. //---------------------------------------------------------------------------
  6632. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6633. {
  6634. if (Message.EndSession && IsApplicationMinimized())
  6635. {
  6636. // WORKAROUND
  6637. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6638. // when it receives WM_ENDSESSION,
  6639. // but that sometimes (particularly when application is minimized) cause crashes.
  6640. // Crash popups message that blocks log off.
  6641. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6642. // so we call ExitProcess() immediately, not even trying to cleanup.
  6643. // It still causes beep, so there's likely some popup, but it does not block
  6644. // log off.
  6645. ExitProcess(0);
  6646. }
  6647. TForm::Dispatch(&Message);
  6648. }
  6649. #ifdef _DEBUG
  6650. //---------------------------------------------------------------------------
  6651. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6652. {
  6653. TForm::Dispatch(&Message);
  6654. }
  6655. #endif
  6656. //---------------------------------------------------------------------------
  6657. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6658. {
  6659. }
  6660. //---------------------------------------------------------------------------
  6661. void __fastcall TCustomScpExplorerForm::DoShow()
  6662. {
  6663. // Called from TCustomScpExplorerForm::CMShowingChanged
  6664. // only now are the controls resized finally, so the size constraints
  6665. // will not conflict with possibly very small window size
  6666. RestoreFormParams();
  6667. // This indicates that the form was rescaled after parameters were loaded and
  6668. // as most probably the parameters were actually saved with the current form scaling
  6669. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6670. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6671. // and no rounding problems will occur.
  6672. // See also RestoreForm.
  6673. // (we should be safe to call RestoreParams unconditionally)
  6674. if (PixelsPerInch != Screen->PixelsPerInch)
  6675. {
  6676. RestoreParams();
  6677. }
  6678. FixControlsPlacement();
  6679. if (Position == poDefaultPosOnly)
  6680. {
  6681. CutFormToDesktop(this);
  6682. }
  6683. TForm::DoShow();
  6684. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6685. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6686. // This was previously in NeedSession, where other related code is.
  6687. // But that is called from CMShowingChanged, when the window is already visible and the local-local window state
  6688. // unpleasanly shows briefly.
  6689. if (!WinConfiguration->ShowLoginWhenNoSession &&
  6690. WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  6691. // This detects if workspace was saved the last time the main widow was closed
  6692. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  6693. {
  6694. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false);
  6695. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  6696. }
  6697. // Before main window displays
  6698. if (ManagedSession == NULL)
  6699. {
  6700. StartingWithoutSession();
  6701. }
  6702. UpdatePixelsPerInchMainWindowCounter();
  6703. FShowing = true;
  6704. }
  6705. //---------------------------------------------------------------------------
  6706. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6707. {
  6708. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6709. }
  6710. //---------------------------------------------------------------------------
  6711. void __fastcall TCustomScpExplorerForm::StartingWithoutSession()
  6712. {
  6713. SessionListChanged();
  6714. InitStatusBar();
  6715. UpdateControls();
  6716. }
  6717. //---------------------------------------------------------------------------
  6718. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6719. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6720. TNotifyEvent OnBalloonClick, TObject * UserData)
  6721. {
  6722. bool Do;
  6723. UnicodeString Message;
  6724. if (E == NULL)
  6725. {
  6726. Message = Str;
  6727. Do = true;
  6728. }
  6729. else
  6730. {
  6731. Do = ExceptionMessage(E, Message);
  6732. }
  6733. if (Do && WinConfiguration->BalloonNotifications)
  6734. {
  6735. UnicodeString Title;
  6736. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6737. {
  6738. Message = TrimLeft(Message);
  6739. }
  6740. else
  6741. {
  6742. Message = UnformatMessage(Message);
  6743. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6744. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6745. Title = LoadResourceString(Captions[Type]);
  6746. if (Terminal != NULL)
  6747. {
  6748. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  6749. Title = FORMAT(L"%s - %s",
  6750. (TTerminalManager::Instance()->GetSessionTitle(Session, true), Title));
  6751. }
  6752. }
  6753. if (Seconds == 0)
  6754. {
  6755. Seconds = WinConfiguration->NotificationsTimeout;
  6756. }
  6757. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6758. }
  6759. }
  6760. //---------------------------------------------------------------------------
  6761. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6762. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6763. UnicodeString HelpKeyword, const TMessageParams * Params,
  6764. TTerminal * Terminal)
  6765. {
  6766. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6767. (FOnFeedSynchronizeError != NULL)) &&
  6768. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6769. {
  6770. if (FOnFeedSynchronizeError != NULL)
  6771. {
  6772. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6773. }
  6774. else
  6775. {
  6776. DebugAssert(FErrorList != NULL);
  6777. TStringList * MoreMessagesCopy = NULL;
  6778. if (MoreMessages)
  6779. {
  6780. MoreMessagesCopy = new TStringList();
  6781. MoreMessagesCopy->Assign(MoreMessages);
  6782. }
  6783. FErrorList->AddObject(Message, MoreMessagesCopy);
  6784. }
  6785. PopupTrayBalloon(Terminal, Message, Type);
  6786. return ContinueAnswer(Answers);
  6787. }
  6788. else
  6789. {
  6790. bool UseBalloon = FTrayIcon->Visible;
  6791. if (UseBalloon)
  6792. {
  6793. PopupTrayBalloon(Terminal, Message, Type);
  6794. }
  6795. unsigned int Result;
  6796. try
  6797. {
  6798. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6799. }
  6800. __finally
  6801. {
  6802. // cancel only balloon we popped up, otherwise we may cancel notification
  6803. // balloon that was there before the message dialog
  6804. // (such as "dd confirmation opt in balloon)
  6805. if (UseBalloon)
  6806. {
  6807. FTrayIcon->CancelBalloon();
  6808. }
  6809. }
  6810. return Result;
  6811. }
  6812. }
  6813. //---------------------------------------------------------------------------
  6814. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6815. TTerminal * Terminal, Exception * E)
  6816. {
  6817. if (FTrayIcon->Visible)
  6818. {
  6819. PopupTrayBalloon(Terminal, L"", qtError, E);
  6820. }
  6821. // particularly prevent opening new session from jump list,
  6822. // while exception is shown
  6823. NonVisualDataModule->StartBusy();
  6824. try
  6825. {
  6826. ShowExtendedExceptionEx(Terminal, E);
  6827. }
  6828. __finally
  6829. {
  6830. NonVisualDataModule->EndBusy();
  6831. FTrayIcon->CancelBalloon();
  6832. }
  6833. }
  6834. //---------------------------------------------------------------------------
  6835. void __fastcall TCustomScpExplorerForm::SessionReady()
  6836. {
  6837. InitStatusBar();
  6838. // cannot rely on active page being page for active terminal,
  6839. // as it can happen that active page is the "new session" page
  6840. // (e.g. when reconnecting active terminal, while login dialog
  6841. // invoked from "new session" page is modal)
  6842. int ActiveSessionIndex = TTerminalManager::Instance()->ActiveSessionIndex;
  6843. UpdateSessionTab(SessionsPageControl->Pages[ActiveSessionIndex]);
  6844. }
  6845. //---------------------------------------------------------------------------
  6846. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6847. TTerminal * Terminal, Exception * E)
  6848. {
  6849. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6850. if (!Terminal->Active)
  6851. {
  6852. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6853. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6854. {
  6855. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6856. }
  6857. }
  6858. }
  6859. //---------------------------------------------------------------------------
  6860. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6861. UnicodeString Message, TQueryType Type,
  6862. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6863. {
  6864. if ((E == NULL) ||
  6865. ExceptionMessage(E, Message))
  6866. {
  6867. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6868. if (Important)
  6869. {
  6870. Seconds *= 5;
  6871. }
  6872. UnicodeString NoteMessage(UnformatMessage(Message));
  6873. if (Terminal != NULL)
  6874. {
  6875. TManagedTerminal * Session = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  6876. NoteMessage = FORMAT(L"%s: %s",
  6877. (TTerminalManager::Instance()->GetSessionTitle(Session, true), NoteMessage));
  6878. }
  6879. if (WinConfiguration->BalloonNotifications)
  6880. {
  6881. AddNote(NoteMessage);
  6882. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6883. }
  6884. else
  6885. {
  6886. FlashOnBackground();
  6887. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6888. }
  6889. }
  6890. }
  6891. //---------------------------------------------------------------------------
  6892. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6893. {
  6894. RestoreApp();
  6895. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6896. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6897. !NonVisualDataModule->Busy)
  6898. {
  6899. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  6900. TTerminalManager::Instance()->ActiveSession = Terminal;
  6901. if (!ComponentVisible[fcQueueView])
  6902. {
  6903. ToggleQueueVisibility();
  6904. GoToQueue();
  6905. }
  6906. }
  6907. }
  6908. //---------------------------------------------------------------------------
  6909. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  6910. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6911. {
  6912. UnicodeString Message;
  6913. TNotifyEvent OnClick = NULL;
  6914. TObject * UserData = NULL;
  6915. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6916. switch (Event)
  6917. {
  6918. case qeEmptyButMonitored:
  6919. if ((ATerminal != Terminal) || QueueInvisible)
  6920. {
  6921. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6922. OnClick = QueueEmptyNoteClicked;
  6923. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6924. TerminalNoteData->Terminal = ATerminal;
  6925. UserData = TerminalNoteData;
  6926. }
  6927. break;
  6928. case qeEmpty:
  6929. OperationComplete(ATerminal->QueueOperationStart);
  6930. break;
  6931. case qePendingUserAction:
  6932. if ((ATerminal != Terminal) ||
  6933. (QueueInvisible && !IsQueueAutoPopup()))
  6934. {
  6935. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6936. }
  6937. break;
  6938. default:
  6939. DebugFail();
  6940. }
  6941. if (!Message.IsEmpty())
  6942. {
  6943. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6944. }
  6945. }
  6946. //---------------------------------------------------------------------------
  6947. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6948. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6949. {
  6950. if (FDDExtMapFile != NULL)
  6951. {
  6952. CloseHandle(FDDExtMapFile);
  6953. FDDExtMapFile = NULL;
  6954. }
  6955. if (WinConfiguration->DDFakeFile)
  6956. {
  6957. DDFakeFileInitDrag(FileList, Created);
  6958. }
  6959. }
  6960. //---------------------------------------------------------------------------
  6961. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  6962. {
  6963. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  6964. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  6965. {
  6966. FFakeFileDropTarget = FileName;
  6967. }
  6968. }
  6969. //---------------------------------------------------------------------------
  6970. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  6971. {
  6972. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6973. if (!ForceDirectories(ApiPath(Result)))
  6974. {
  6975. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  6976. }
  6977. FileSetAttr(ApiPath(Result), faHidden);
  6978. return Result;
  6979. }
  6980. //---------------------------------------------------------------------------
  6981. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  6982. bool & Created)
  6983. {
  6984. FFakeFileDropTarget = UnicodeString();
  6985. FDragFakeDirectory = CreateFakeTransferDirectory();
  6986. FileList->AddItem(NULL, FDragFakeDirectory);
  6987. Created = true;
  6988. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  6989. {
  6990. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  6991. }
  6992. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6993. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6994. {
  6995. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6996. TDragExtCommStruct* CommStruct;
  6997. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6998. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6999. DebugAssert(CommStruct != NULL);
  7000. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  7001. CommStruct->Dragging = true;
  7002. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  7003. LENOF(CommStruct->DropDest));
  7004. NULL_TERMINATE(CommStruct->DropDest);
  7005. UnmapViewOfFile(CommStruct);
  7006. }
  7007. }
  7008. //---------------------------------------------------------------------------
  7009. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  7010. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  7011. {
  7012. bool Result;
  7013. DebugAssert(!IsLocalBrowserMode());
  7014. if (Sender == RemoteDirView)
  7015. {
  7016. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  7017. }
  7018. else
  7019. {
  7020. DebugAssert(Sender == RemoteDriveView);
  7021. TStrings * FileList = RemoteDriveView->DragFileList();
  7022. try
  7023. {
  7024. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  7025. }
  7026. __finally
  7027. {
  7028. delete FileList;
  7029. }
  7030. }
  7031. if (FDDTargetControl == RemoteDriveView)
  7032. {
  7033. RemoteDriveView->UpdateDropTarget();
  7034. }
  7035. // foRemoteMove happens when file/dir is dragged within the remote tree view
  7036. // or from tree view to dir view.
  7037. // foMove happens when file/dir is dragged from remote tree view outside of
  7038. // application via dragex
  7039. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  7040. (DropSourceControl == RemoteDriveView))
  7041. {
  7042. RemoteDriveView->UpdateDropSource();
  7043. }
  7044. return Result;
  7045. }
  7046. //---------------------------------------------------------------------------
  7047. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  7048. {
  7049. DebugAssert(!IsLocalBrowserMode());
  7050. // This also handles drops of remote files to queue.
  7051. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  7052. SAFE_DESTROY(FDDFileList);
  7053. if (!FFakeFileDropTarget.IsEmpty())
  7054. {
  7055. RemoveDir(ApiPath(FFakeFileDropTarget));
  7056. }
  7057. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  7058. {
  7059. try
  7060. {
  7061. TDragResult DDResult = (Sender == RemoteDirView) ?
  7062. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  7063. // Focus is moved to the target application,
  7064. // but as we are going to present the UI, we need to steal the focus back.
  7065. // This most likely won't work though (windows does not allow application
  7066. // to steal focus most of the time)
  7067. Application->BringToFront();
  7068. // On older version of Windows we never got drMove here, see also comment below.
  7069. // On Windows 10, we get the "move".
  7070. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  7071. {
  7072. UnicodeString TargetDirectory;
  7073. TFileOperation Operation;
  7074. // drInvalid may mean drMove, see comment below
  7075. switch (DDResult)
  7076. {
  7077. case drCopy:
  7078. Operation = foCopy;
  7079. break;
  7080. case drMove:
  7081. Operation = foMove;
  7082. break;
  7083. default:
  7084. DebugFail();
  7085. case drInvalid:
  7086. // prefer "copy" for safety
  7087. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  7088. break;
  7089. }
  7090. TTransferOperationParam Param;
  7091. UnicodeString CounterName;
  7092. bool ForceQueue;
  7093. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  7094. {
  7095. // we get drInvalid both if d&d was intercepted by ddext,
  7096. // and when users drops on no-drop location.
  7097. // we tell the difference by existence of response from ddext,
  7098. // so we ignore absence of response in this case
  7099. if (DDResult != drInvalid)
  7100. {
  7101. // here we know that the extension is installed,
  7102. // as it is checked as soon as drag&drop starts from
  7103. // RemoteFileControlDDCreateDragFileList
  7104. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  7105. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  7106. {
  7107. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  7108. }
  7109. else
  7110. {
  7111. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  7112. }
  7113. }
  7114. }
  7115. else
  7116. {
  7117. // download using fake file
  7118. Param.Temp = false;
  7119. Param.DragDrop = true;
  7120. if (ForceQueue)
  7121. {
  7122. Param.Queue = asOn;
  7123. }
  7124. if (Sender == RemoteDirView)
  7125. {
  7126. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  7127. }
  7128. if (RemoteFileControlFileOperation(Sender, Operation,
  7129. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7130. {
  7131. Configuration->Usage->Inc(CounterName);
  7132. }
  7133. }
  7134. }
  7135. }
  7136. __finally
  7137. {
  7138. CloseHandle(FDDExtMapFile);
  7139. FDDExtMapFile = NULL;
  7140. delete FDragFakeMonitors;
  7141. FDragFakeMonitors = NULL;
  7142. RemoveDir(ApiPath(FDragFakeDirectory));
  7143. FDragFakeDirectory = L"";
  7144. FFakeFileDropTarget = UnicodeString();
  7145. }
  7146. }
  7147. if (FDragDropSshTerminate.get() != NULL)
  7148. {
  7149. FDragDropSshTerminate->Rethrow();
  7150. }
  7151. }
  7152. //---------------------------------------------------------------------------
  7153. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  7154. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  7155. {
  7156. // Remember drop effect so we know (when user drops files), if we copy or move
  7157. FLastDropEffect = dwEffect;
  7158. }
  7159. //---------------------------------------------------------------------------
  7160. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  7161. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  7162. {
  7163. bool Result;
  7164. if (FDDTargetControl == QueueView3)
  7165. {
  7166. Directory = DefaultDownloadTargetDirectory();
  7167. Result = true;
  7168. CounterName = L"DownloadsDragDropQueue";
  7169. ForceQueue = true;
  7170. }
  7171. else if (!FFakeFileDropTarget.IsEmpty())
  7172. {
  7173. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  7174. Result = true;
  7175. ForceQueue = false;
  7176. CounterName = L"DownloadsDragDropFakeFile";
  7177. }
  7178. else
  7179. {
  7180. ForceQueue = false;
  7181. Enabled = false;
  7182. try
  7183. {
  7184. int Timer = 0;
  7185. Result = false;
  7186. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  7187. {
  7188. {
  7189. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  7190. TDragExtCommStruct* CommStruct;
  7191. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  7192. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  7193. DebugAssert(CommStruct != NULL);
  7194. Result = !CommStruct->Dragging;
  7195. if (Result)
  7196. {
  7197. Directory = ExtractFilePath(CommStruct->DropDest);
  7198. CounterName = L"DownloadsDragDropExternalExt";
  7199. }
  7200. UnmapViewOfFile(CommStruct);
  7201. }
  7202. if (!Result)
  7203. {
  7204. Sleep(50);
  7205. Timer += 50;
  7206. Application->ProcessMessages();
  7207. }
  7208. }
  7209. }
  7210. __finally
  7211. {
  7212. Enabled = true;
  7213. }
  7214. }
  7215. return Result;
  7216. }
  7217. //---------------------------------------------------------------------------
  7218. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7219. const UnicodeString TempDir, int SecDelay)
  7220. {
  7221. TDateTime Alarm = IncSecond(Now(), SecDelay);
  7222. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7223. if (FDelayedDeletionTimer == NULL)
  7224. {
  7225. DebugAssert(HandleAllocated());
  7226. FDelayedDeletionTimer = new TTimer(this);
  7227. FDelayedDeletionTimer->Interval = 10000;
  7228. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7229. }
  7230. else
  7231. {
  7232. FDelayedDeletionTimer->Enabled = true;
  7233. }
  7234. }
  7235. //---------------------------------------------------------------------------
  7236. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7237. {
  7238. DebugAssert(FDelayedDeletionList != NULL);
  7239. TDateTime N = Now();
  7240. TDateTime Alert;
  7241. UnicodeString Directory;
  7242. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7243. {
  7244. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7245. if ((N >= Alert) || (Sender == NULL))
  7246. {
  7247. Directory = FDelayedDeletionList->Strings[Index];
  7248. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7249. {
  7250. FDelayedDeletionList->Delete(Index);
  7251. }
  7252. }
  7253. }
  7254. if (FDelayedDeletionList->Count == 0)
  7255. {
  7256. FDelayedDeletionTimer->Enabled = false;
  7257. }
  7258. }
  7259. //---------------------------------------------------------------------------
  7260. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7261. {
  7262. DebugAssert(!IsLocalBrowserMode());
  7263. if (IsFileControl(FDDTargetControl, osRemote) ||
  7264. (FDDTargetControl == SessionsPageControl))
  7265. {
  7266. TTerminal * TargetTerminal = NULL;
  7267. TFileOperation Operation = foNone;
  7268. if (FDDTargetControl == SessionsPageControl)
  7269. {
  7270. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7271. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7272. // do not allow dropping on the "+" tab
  7273. TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  7274. if (TargetTerminal != NULL)
  7275. {
  7276. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7277. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7278. {
  7279. Operation = foRemoteMove;
  7280. }
  7281. else
  7282. {
  7283. Operation = foRemoteCopy;
  7284. }
  7285. }
  7286. }
  7287. else
  7288. {
  7289. if (FLastDropEffect == DROPEFFECT_MOVE)
  7290. {
  7291. Operation = foRemoteMove;
  7292. }
  7293. else if (FLastDropEffect == DROPEFFECT_COPY)
  7294. {
  7295. Operation = foRemoteCopy;
  7296. }
  7297. }
  7298. if (Operation != foNone)
  7299. {
  7300. RemoteFileControlFileOperation(DropSourceControl,
  7301. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7302. }
  7303. // abort drag&drop
  7304. Abort();
  7305. }
  7306. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7307. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7308. (FDDTargetControl != QueueView3))
  7309. {
  7310. DebugAssert(!FDragTempDir.IsEmpty());
  7311. TTransferType Type;
  7312. UnicodeString TempDir = FDragTempDir;
  7313. // We clear FDragTempDir before calling
  7314. // just in case it fail (raises exception)
  7315. FDragTempDir = L"";
  7316. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7317. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7318. // empty directory parameter means temp directory -> don't display it!
  7319. UnicodeString TargetDir = L"";
  7320. int Options = 0;
  7321. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7322. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7323. {
  7324. Abort();
  7325. }
  7326. // TargetDir is set when dropped on local file control
  7327. // (this was workaround for legacy dirview event handling, now it should be
  7328. // made prettier)
  7329. if (TargetDir.IsEmpty())
  7330. {
  7331. TargetDir = TempDir;
  7332. if (ForceDirectories(ApiPath(TargetDir)))
  7333. {
  7334. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7335. FPendingTempSpaceWarn = true;
  7336. try
  7337. {
  7338. FDragDropOperation = true;
  7339. int Params = cpTemporary |
  7340. (Type == ttMove ? cpDelete : 0);
  7341. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7342. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7343. }
  7344. __finally
  7345. {
  7346. FDragDropOperation = false;
  7347. FPendingTempSpaceWarn = false;
  7348. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7349. }
  7350. }
  7351. else
  7352. {
  7353. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7354. }
  7355. }
  7356. }
  7357. }
  7358. //---------------------------------------------------------------------------
  7359. void __fastcall TCustomScpExplorerForm::DDDownload(
  7360. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  7361. {
  7362. DebugAssert(!IsLocalBrowserMode());
  7363. TAutoBatch AutoBatch(this);
  7364. UpdateCopyParamCounters(*CopyParam);
  7365. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7366. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7367. {
  7368. RemoteDriveView->UpdateDropSource();
  7369. }
  7370. }
  7371. //---------------------------------------------------------------------------
  7372. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7373. {
  7374. public:
  7375. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7376. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7377. {
  7378. }
  7379. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7380. {
  7381. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7382. TDataObjectFilesEx::AllowData(FormatEtc);
  7383. }
  7384. };
  7385. //---------------------------------------------------------------------------
  7386. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7387. TObject * Sender, TDataObject *& DataObject)
  7388. {
  7389. if (FDDExtMapFile != NULL)
  7390. {
  7391. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7392. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7393. {
  7394. Abort();
  7395. }
  7396. if (FileList->Count > 0)
  7397. {
  7398. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7399. if (!FilesObject->IsValid(true, true))
  7400. {
  7401. FilesObject->_Release();
  7402. }
  7403. else
  7404. {
  7405. DataObject = FilesObject;
  7406. }
  7407. }
  7408. }
  7409. }
  7410. //---------------------------------------------------------------------------
  7411. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7412. {
  7413. DebugFail();
  7414. }
  7415. //---------------------------------------------------------------------------
  7416. void __fastcall TCustomScpExplorerForm::GoToTree()
  7417. {
  7418. ComponentVisible[fcRemoteTree] = true;
  7419. DriveView(osRemote)->SetFocus();
  7420. }
  7421. //---------------------------------------------------------------------------
  7422. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7423. TPanelExport Export)
  7424. {
  7425. TCustomDirView * DirView = this->DirView(Side);
  7426. std::unique_ptr<TStrings> ExportData(new TStringList());
  7427. switch (Export)
  7428. {
  7429. case pePath:
  7430. ExportData->Add(DirView->PathName);
  7431. break;
  7432. case peFileList:
  7433. case peFullFileList:
  7434. {
  7435. bool FullPath = (Export == peFullFileList);
  7436. DirView->CreateFileList(false, FullPath, ExportData.get());
  7437. for (int Index = 0; Index < ExportData->Count; Index++)
  7438. {
  7439. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7440. {
  7441. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7442. }
  7443. }
  7444. }
  7445. break;
  7446. default:
  7447. DebugFail();
  7448. }
  7449. return ExportData.release();
  7450. }
  7451. //---------------------------------------------------------------------------
  7452. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7453. TPanelExport Export, TPanelExportDestination Destination)
  7454. {
  7455. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7456. PanelExportStore(Side, Export, Destination, ExportData.get());
  7457. }
  7458. //---------------------------------------------------------------------------
  7459. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7460. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7461. TStrings * ExportData)
  7462. {
  7463. if (Destination == pedClipboard)
  7464. {
  7465. TInstantOperationVisualizer Visualizer;
  7466. CopyToClipboard(ExportData);
  7467. }
  7468. else
  7469. {
  7470. DebugFail();
  7471. }
  7472. }
  7473. //---------------------------------------------------------------------------
  7474. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7475. {
  7476. TCustomDirView * DirView = this->DirView(Side);
  7477. UnicodeString Mask = DirView->Mask;
  7478. if (DoFilterMaskDialog(DirView, Mask))
  7479. {
  7480. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7481. Configuration->Usage->Inc(L"Filters");
  7482. }
  7483. }
  7484. //---------------------------------------------------------------------------
  7485. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7486. {
  7487. return FQueueController->DefaultOperation();
  7488. }
  7489. //---------------------------------------------------------------------------
  7490. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7491. TQueueOperation Operation, void ** Param)
  7492. {
  7493. switch (Operation)
  7494. {
  7495. case qoPreferences:
  7496. return true;
  7497. case qoGoTo:
  7498. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7499. case qoOnceEmpty:
  7500. return IsAnythingQueued();
  7501. default:
  7502. return FQueueController->AllowOperation(Operation, Param);
  7503. }
  7504. }
  7505. //---------------------------------------------------------------------------
  7506. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7507. {
  7508. if (DebugAlwaysTrue(QueueView3->Visible))
  7509. {
  7510. QueueView3->SetFocus();
  7511. }
  7512. }
  7513. //---------------------------------------------------------------------------
  7514. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7515. TQueueOperation Operation, void * Param)
  7516. {
  7517. if (Operation == qoGoTo)
  7518. {
  7519. GoToQueue();
  7520. }
  7521. else if (Operation == qoPreferences)
  7522. {
  7523. PreferencesDialog(pmQueue);
  7524. }
  7525. else
  7526. {
  7527. FQueueController->ExecuteOperation(Operation, Param);
  7528. }
  7529. }
  7530. //---------------------------------------------------------------------------
  7531. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7532. {
  7533. return (Queue != NULL) && Queue->Enabled;
  7534. }
  7535. //---------------------------------------------------------------------------
  7536. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7537. {
  7538. DebugAssert(Queue != NULL);
  7539. if (Queue != NULL)
  7540. {
  7541. Queue->Enabled = !Queue->Enabled;
  7542. }
  7543. }
  7544. //---------------------------------------------------------------------------
  7545. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7546. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7547. {
  7548. FQueueActedItem = QueueView3->ItemFocused;
  7549. }
  7550. //---------------------------------------------------------------------------
  7551. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7552. {
  7553. return TopDock->Height + QueueSplitter->Height;
  7554. }
  7555. //---------------------------------------------------------------------------
  7556. int __fastcall TCustomScpExplorerForm::GetStaticQueuePanelComponentsHeight()
  7557. {
  7558. return
  7559. (QueueFileListSplitter->Visible ? QueueFileListSplitter->Height : 0) +
  7560. (QueueDock->Visible ? QueueDock->Height : 0) +
  7561. (QueueLabel->Visible ? QueueLabel->Height : 0);
  7562. }
  7563. //---------------------------------------------------------------------------
  7564. int __fastcall TCustomScpExplorerForm::GetMinQueueViewHeight()
  7565. {
  7566. return ScaleByTextHeight(this, 48);
  7567. }
  7568. //---------------------------------------------------------------------------
  7569. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7570. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7571. {
  7572. // when queue is hidden by double-clicking splitter, stray attempt to
  7573. // resize the panel with strange value arrives, make sure it is ignored
  7574. if (ComponentVisible[fcQueueView])
  7575. {
  7576. int MaxHeightLimit = ClientHeight - GetStaticComponentsHeight() - RemotePanel->Constraints->MinHeight;
  7577. if (NewSize > MaxHeightLimit)
  7578. {
  7579. NewSize = MaxHeightLimit;
  7580. }
  7581. int MinHeightLimit =
  7582. GetStaticQueuePanelComponentsHeight() +
  7583. (QueueFileList->Visible ? QueueFileList->Height : 0) +
  7584. GetMinQueueViewHeight();
  7585. if (NewSize < MinHeightLimit)
  7586. {
  7587. NewSize = MinHeightLimit;
  7588. }
  7589. }
  7590. else
  7591. {
  7592. Accept = false;
  7593. }
  7594. }
  7595. //---------------------------------------------------------------------------
  7596. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterCanResize(TObject *, int & NewSize, bool &)
  7597. {
  7598. int TotalHeight = GetStaticQueuePanelComponentsHeight() + NewSize;
  7599. int QueueViewHeight = QueuePanel->ClientHeight - TotalHeight;
  7600. if (QueueViewHeight < GetMinQueueViewHeight())
  7601. {
  7602. NewSize -= (GetMinQueueViewHeight() - QueueViewHeight);
  7603. }
  7604. }
  7605. //---------------------------------------------------------------------------
  7606. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7607. TDragObject *& /*DragObject*/)
  7608. {
  7609. FQueueActedItem = QueueView3->ItemFocused;
  7610. }
  7611. //---------------------------------------------------------------------------
  7612. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7613. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7614. {
  7615. Accept = true;
  7616. if (Source == QueueView3)
  7617. {
  7618. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7619. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7620. if (Accept)
  7621. {
  7622. TQueueItemProxy * QueueItem;
  7623. TQueueItemProxy * DestQueueItem;
  7624. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7625. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7626. Accept = (QueueItem != DestQueueItem) &&
  7627. (QueueItem->Status == TQueueItem::qsPending) &&
  7628. (DestQueueItem->Status == TQueueItem::qsPending);
  7629. }
  7630. }
  7631. }
  7632. //---------------------------------------------------------------------------
  7633. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7634. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7635. {
  7636. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7637. {
  7638. TQueueItemProxy * QueueItem;
  7639. TQueueItemProxy * DestQueueItem;
  7640. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7641. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7642. QueueItem->Move(DestQueueItem);
  7643. }
  7644. }
  7645. //---------------------------------------------------------------------------
  7646. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7647. {
  7648. if ((QueueView3->ItemFocused == NULL) &&
  7649. (QueueView3->Items->Count > 0))
  7650. {
  7651. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7652. }
  7653. QueueLabelUpdateStatus();
  7654. }
  7655. //---------------------------------------------------------------------------
  7656. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7657. {
  7658. QueueLabelUpdateStatus();
  7659. }
  7660. //---------------------------------------------------------------------------
  7661. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7662. {
  7663. QueueLabel->UpdateStatus();
  7664. }
  7665. //---------------------------------------------------------------------------
  7666. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7667. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7668. {
  7669. if (Selected)
  7670. {
  7671. NonVisualDataModule->UpdateNonVisibleActions();
  7672. }
  7673. }
  7674. //---------------------------------------------------------------------------
  7675. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7676. {
  7677. TDragDropFilesEx * Result = NULL;
  7678. if (Sender == SessionsPageControl)
  7679. {
  7680. Result = FSessionsDragDropFilesEx;
  7681. }
  7682. else if (Sender == QueueView3)
  7683. {
  7684. Result = FQueueDragDropFilesEx;
  7685. }
  7686. else
  7687. {
  7688. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7689. if (DirView != NULL)
  7690. {
  7691. Result = DirView->DragDropFilesEx;
  7692. }
  7693. else
  7694. {
  7695. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7696. if (DriveView != NULL)
  7697. {
  7698. Result = DriveView->DragDropFilesEx;
  7699. }
  7700. }
  7701. }
  7702. DebugAssert(Result != NULL);
  7703. return Result;
  7704. }
  7705. //---------------------------------------------------------------------------
  7706. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7707. {
  7708. return (DView->SelCount == DView->FilesCount);
  7709. }
  7710. //---------------------------------------------------------------------------
  7711. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7712. {
  7713. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7714. }
  7715. //---------------------------------------------------------------------------
  7716. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7717. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7718. {
  7719. DebugAssert(!IsLocalBrowserMode());
  7720. TFileOperation Operation;
  7721. switch (Effect)
  7722. {
  7723. case DROPEFFECT_MOVE:
  7724. Operation = foMove;
  7725. break;
  7726. case DROPEFFECT_COPY:
  7727. // occures on WinXP (reported by user)
  7728. default:
  7729. Operation = foCopy;
  7730. break;
  7731. };
  7732. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7733. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7734. if (DragDropFilesEx->FileList->Count > 0)
  7735. {
  7736. TStrings * FileList = new TStringList();
  7737. try
  7738. {
  7739. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7740. {
  7741. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7742. }
  7743. FDragDropOperation = true;
  7744. TTransferOperationParam Param;
  7745. Param.TargetDirectory = TargetPath;
  7746. // upload, no temp dirs
  7747. Param.Temp = false;
  7748. Param.DragDrop = DragDrop;
  7749. Param.Options =
  7750. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7751. if (ForceQueue)
  7752. {
  7753. Param.Queue = asOn;
  7754. }
  7755. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7756. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7757. {
  7758. if (IsFileControl(DropSourceControl, osLocal))
  7759. {
  7760. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7761. }
  7762. else
  7763. {
  7764. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7765. }
  7766. }
  7767. }
  7768. __finally
  7769. {
  7770. FDragDropOperation = false;
  7771. delete FileList;
  7772. }
  7773. }
  7774. }
  7775. //---------------------------------------------------------------------------
  7776. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7777. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7778. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7779. {
  7780. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7781. }
  7782. //---------------------------------------------------------------------------
  7783. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7784. TObject * Sender, int grfKeyState, int & dwEffect)
  7785. {
  7786. DebugAssert(!IsLocalBrowserMode());
  7787. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7788. if ((dwEffect != DROPEFFECT_NONE) &&
  7789. IsFileControl(DropSourceControl, osRemote))
  7790. {
  7791. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7792. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7793. (RemoteDirView->DropTarget == NULL))
  7794. {
  7795. dwEffect = DROPEFFECT_NONE;
  7796. }
  7797. else
  7798. {
  7799. if (dwEffect == DROPEFFECT_COPY)
  7800. {
  7801. bool MoveCapable = Terminal->IsCapable[fcRemoteMove];
  7802. // currently we support copying always (at least via temporary directory);
  7803. // remove associated checks once this all proves stable and working
  7804. bool CopyCapable = true;
  7805. // if we do not support neither of operations, there's no discussion
  7806. if (!MoveCapable && !CopyCapable)
  7807. {
  7808. dwEffect = DROPEFFECT_NONE;
  7809. }
  7810. else
  7811. {
  7812. // The default effect inside remote panel is move,
  7813. // unless we do not support it, but support copy
  7814. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7815. {
  7816. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  7817. }
  7818. else
  7819. {
  7820. // with ctrl-down, we want copy unless it is not supported
  7821. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  7822. }
  7823. }
  7824. }
  7825. }
  7826. }
  7827. }
  7828. //---------------------------------------------------------------------------
  7829. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7830. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7831. {
  7832. DebugAssert(!IsLocalBrowserMode());
  7833. if (FDDTotalSize >= 0)
  7834. {
  7835. if (File->IsDirectory)
  7836. {
  7837. FDDTotalSize = -1;
  7838. }
  7839. else
  7840. {
  7841. FDDTotalSize += File->Size;
  7842. }
  7843. }
  7844. DebugAssert(!FDragTempDir.IsEmpty());
  7845. // TODO: this is quite ineffective
  7846. // TODO: what if invalid character replacement is disabled?
  7847. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7848. UnicodeString TransferFileName;
  7849. if (Sender == RemoteDriveView)
  7850. {
  7851. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7852. }
  7853. else
  7854. {
  7855. TransferFileName = File->FileName;
  7856. }
  7857. FDDFileList->AddObject(TransferFileName, File);
  7858. }
  7859. //---------------------------------------------------------------------------
  7860. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7861. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7862. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7863. {
  7864. // sometimes we do not get DDEnd so the list is not released
  7865. SAFE_DESTROY(FDDFileList);
  7866. FDDFileList = new TStringList();
  7867. FDragTempDir = WinConfiguration->TemporaryDir();
  7868. FDDTotalSize = 0;
  7869. FDragDropSshTerminate.reset(NULL);
  7870. }
  7871. //---------------------------------------------------------------------------
  7872. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7873. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7874. HRESULT & Result)
  7875. {
  7876. if (Result == DRAGDROP_S_DROP)
  7877. {
  7878. try
  7879. {
  7880. GlobalDragImageList->HideDragImage();
  7881. try
  7882. {
  7883. RemoteFileControlDDTargetDrop();
  7884. }
  7885. catch(ESshTerminate & E)
  7886. {
  7887. FDragDropSshTerminate.reset(E.Clone());
  7888. }
  7889. }
  7890. catch (Exception &E)
  7891. {
  7892. // If downloading fails we need to cancel drag&drop, otherwise
  7893. // Explorer shows error
  7894. // But by the way exception probably never reach this point as
  7895. // it's catched on way.
  7896. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7897. Result = DRAGDROP_S_CANCEL;
  7898. ShowExtendedException(Terminal, &E);
  7899. }
  7900. }
  7901. }
  7902. //---------------------------------------------------------------------------
  7903. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7904. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7905. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7906. {
  7907. TFileMasks::TParams MaskParams;
  7908. MaskParams.Size = Size;
  7909. MaskParams.Modification = Modification;
  7910. // this does not re-parse the mask if it is the same as the last time
  7911. FDirViewMatchMask = Masks;
  7912. bool ImplicitMatch;
  7913. Matches =
  7914. // RecurseInclude parameter has no effect as the Path is empty
  7915. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7916. (AllowImplicitMatches || !ImplicitMatch);
  7917. }
  7918. //---------------------------------------------------------------------------
  7919. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7920. TObject * Sender, TListItem * Item, WORD & Indexes)
  7921. {
  7922. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7923. UnicodeString Ext;
  7924. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  7925. {
  7926. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7927. }
  7928. else
  7929. {
  7930. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7931. }
  7932. if (SameText(Ext, Configuration->PartialExt))
  7933. {
  7934. Indexes |= oiPartial;
  7935. }
  7936. }
  7937. //---------------------------------------------------------------------------
  7938. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7939. {
  7940. return
  7941. DirViewEnabled(osCurrent) &&
  7942. DirView(osCurrent)->CanPasteFromClipBoard();
  7943. }
  7944. //---------------------------------------------------------------------------
  7945. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7946. {
  7947. bool Result = false;
  7948. if (CanPasteToDirViewFromClipBoard())
  7949. {
  7950. Result = true;
  7951. }
  7952. else
  7953. {
  7954. UnicodeString ClipboardText;
  7955. if (NonEmptyTextFromClipboard(ClipboardText) &&
  7956. (ClipboardText.Pos(L"\n") == 0)) // it's already trimmed
  7957. {
  7958. if (StoredSessions->IsUrl(ClipboardText))
  7959. {
  7960. Result = true;
  7961. }
  7962. else
  7963. {
  7964. Result = DirViewEnabled(osCurrent);
  7965. }
  7966. }
  7967. }
  7968. return Result;
  7969. }
  7970. //---------------------------------------------------------------------------
  7971. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7972. {
  7973. if (DoesClipboardContainOurFiles())
  7974. {
  7975. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  7976. {
  7977. DebugAssert(!IsLocalBrowserMode());
  7978. TTerminalManager * Manager = TTerminalManager::Instance();
  7979. TTerminal * TergetTerminal = Manager->ActiveTerminal;
  7980. Manager->ActiveSession = FClipboardTerminal;
  7981. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TergetTerminal);
  7982. }
  7983. }
  7984. else if (CanPasteToDirViewFromClipBoard())
  7985. {
  7986. DirView(osCurrent)->PasteFromClipBoard();
  7987. }
  7988. else
  7989. {
  7990. UnicodeString ClipboardText;
  7991. if (NonEmptyTextFromClipboard(ClipboardText))
  7992. {
  7993. if (StoredSessions->IsUrl(ClipboardText))
  7994. {
  7995. NewSession(ClipboardText);
  7996. }
  7997. else
  7998. {
  7999. DirView(osCurrent)->Path = ClipboardText;
  8000. }
  8001. }
  8002. }
  8003. }
  8004. //---------------------------------------------------------------------------
  8005. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  8006. {
  8007. // TBD
  8008. }
  8009. //---------------------------------------------------------------------------
  8010. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  8011. {
  8012. TCustomDirView * ADirView = DirView(Side);
  8013. ADirView->SelectAll(Mode);
  8014. ADirView->SetFocus();
  8015. }
  8016. //---------------------------------------------------------------------------
  8017. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  8018. {
  8019. TCustomDirView * ADirView = DirView(Side);
  8020. TFileFilter Filter;
  8021. DefaultFileFilter(Filter);
  8022. if (DoSelectMaskDialog(ADirView, Select, Filter))
  8023. {
  8024. Configuration->Usage->Inc(L"MaskSelections");
  8025. ADirView->SelectFiles(Filter, Select);
  8026. ADirView->SetFocus();
  8027. }
  8028. }
  8029. //---------------------------------------------------------------------------
  8030. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  8031. {
  8032. TCustomDirView * ADirView = DirView(Side);
  8033. ADirView->RestoreSelectedNames();
  8034. ADirView->SetFocus();
  8035. }
  8036. //---------------------------------------------------------------------------
  8037. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  8038. {
  8039. TCustomDirView * CurrentDirView = DirView(osCurrent);
  8040. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  8041. {
  8042. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  8043. UnicodeString Ext;
  8044. if (!IsSideLocalBrowser(osCurrent))
  8045. {
  8046. Ext = UnixExtractFileExt(FileName);
  8047. }
  8048. else
  8049. {
  8050. Ext = ExtractFileExt(FileName);
  8051. }
  8052. if (Ext.IsEmpty())
  8053. {
  8054. Ext = L".";
  8055. }
  8056. TFileFilter Filter;
  8057. Filter.Masks = FORMAT(L"*%s", (Ext));
  8058. Filter.Directories = false;
  8059. CurrentDirView->SelectFiles(Filter, Select);
  8060. }
  8061. }
  8062. //---------------------------------------------------------------------------
  8063. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  8064. const TStatusFileInfo & FileInfo, TOperationSide Side)
  8065. {
  8066. UnicodeString Result;
  8067. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  8068. {
  8069. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  8070. }
  8071. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  8072. {
  8073. // noop
  8074. }
  8075. else
  8076. {
  8077. Result =
  8078. FMTLOAD(FILE_INFO_FORMAT,
  8079. (FormatBytes(FileInfo.SelectedSize),
  8080. FormatBytes(FileInfo.FilesSize),
  8081. FormatNumber(FileInfo.SelectedCount),
  8082. FormatNumber(FileInfo.FilesCount)));
  8083. }
  8084. return Result;
  8085. }
  8086. //---------------------------------------------------------------------------
  8087. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  8088. TTBXStatusPanel * Panel, TOperationSide Side)
  8089. {
  8090. if (Panel->Index == 1)
  8091. {
  8092. ToggleShowHiddenFiles();
  8093. }
  8094. else if (Panel->Index == 2)
  8095. {
  8096. Filter(Side);
  8097. }
  8098. }
  8099. //---------------------------------------------------------------------------
  8100. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  8101. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  8102. {
  8103. DebugAssert(!StatusBar->SimplePanel);
  8104. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  8105. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  8106. }
  8107. //---------------------------------------------------------------------------
  8108. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  8109. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  8110. {
  8111. DebugAssert(StatusBar->Panels->Count >= 3);
  8112. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  8113. if (FileInfo.HiddenCount > 0)
  8114. {
  8115. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  8116. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8117. }
  8118. else
  8119. {
  8120. HiddenFilesPanel->ViewPriority = 0;
  8121. // not really necessary, just to cleanup no-longer-valid data
  8122. HiddenFilesPanel->Caption = L"";
  8123. }
  8124. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  8125. if (FileInfo.FilteredCount > 0)
  8126. {
  8127. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  8128. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  8129. }
  8130. else
  8131. {
  8132. FilteredFilesPanel->ViewPriority = 0;
  8133. // not really necessary, just to cleanup no-longer-valid data
  8134. FilteredFilesPanel->Caption = L"";
  8135. }
  8136. }
  8137. //---------------------------------------------------------------------------
  8138. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  8139. TObject * /*Sender*/)
  8140. {
  8141. if (DirView(osOther)->Enabled)
  8142. {
  8143. DirView(osOther)->SetFocus();
  8144. }
  8145. }
  8146. //---------------------------------------------------------------------------
  8147. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  8148. {
  8149. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  8150. switch (Config.Show)
  8151. {
  8152. case qvShow:
  8153. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  8154. {
  8155. Config.Show = qvHide;
  8156. }
  8157. else
  8158. {
  8159. Config.Show = Config.LastHideShow;
  8160. }
  8161. break;
  8162. case qvHideWhenEmpty:
  8163. if (ComponentVisible[fcQueueView])
  8164. {
  8165. Config.Show = qvHide;
  8166. }
  8167. else
  8168. {
  8169. Config.LastHideShow = Config.Show;
  8170. Config.Show = qvShow;
  8171. }
  8172. break;
  8173. case qvHide:
  8174. Config.LastHideShow = Config.Show;
  8175. Config.Show = qvShow;
  8176. break;
  8177. }
  8178. WinConfiguration->QueueView = Config;
  8179. }
  8180. //---------------------------------------------------------------------------
  8181. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  8182. {
  8183. UnicodeString Result;
  8184. if (Terminal != NULL)
  8185. {
  8186. switch (WinConfiguration->PathInCaption)
  8187. {
  8188. case picShort:
  8189. Result = ExtractShortName(Terminal->CurrentDirectory, true);
  8190. break;
  8191. case picFull:
  8192. Result = Terminal->CurrentDirectory;
  8193. break;
  8194. }
  8195. }
  8196. return Result;
  8197. }
  8198. //---------------------------------------------------------------------------
  8199. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  8200. {
  8201. TColor Result = GetWindowColor(FSessionColor);
  8202. return Result;
  8203. }
  8204. //---------------------------------------------------------------------------
  8205. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  8206. {
  8207. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  8208. return Result;
  8209. }
  8210. //---------------------------------------------------------------------------
  8211. void __fastcall TCustomScpExplorerForm::UpdateControls()
  8212. {
  8213. if (!FSessionChanging)
  8214. {
  8215. TTerminalManager::Instance()->UpdateAppTitle();
  8216. }
  8217. // WORAKRDOUND: Disabling list view when it is not showing yet does not set its
  8218. // background to gray on Windows 7 (works on Windows 10).
  8219. // See also EnableControl
  8220. if (Showing)
  8221. {
  8222. if (ManagedSession != NULL)
  8223. {
  8224. // Update path when it changes
  8225. if ((SessionsPageControl->ActivePage != NULL) && (GetSessionTabSession(SessionsPageControl->ActivePage) == ManagedSession))
  8226. {
  8227. UpdateSessionTab(SessionsPageControl->ActivePage);
  8228. }
  8229. }
  8230. NonVisualDataModule->ReconnectSessionAction->Update();
  8231. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  8232. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  8233. CenterReconnectToolbar();
  8234. bool HasTerminal = HasActiveTerminal();
  8235. RemoteDirView->Enabled = HasTerminal;
  8236. RemoteDirView->Color = HasTerminal ? PanelColor() : DisabledPanelColor();
  8237. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8238. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8239. RemoteDriveView->Color = RemoteDirView->Color;
  8240. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8241. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fcBackgroundTransfers];
  8242. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8243. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8244. QueueFileList->Enabled = QueueView3->Enabled;
  8245. QueueFileList->Color = QueueView3->Color;
  8246. QueueFileList->Font->Color = QueueView3->Font->Color;
  8247. QueueLabelUpdateStatus();
  8248. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8249. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8250. if (FImmersiveDarkMode != WinConfiguration->UseDarkTheme())
  8251. {
  8252. UpdateDarkMode();
  8253. }
  8254. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal || IsLocalBrowserMode();
  8255. }
  8256. }
  8257. //---------------------------------------------------------------------------
  8258. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8259. {
  8260. UpdateControls();
  8261. }
  8262. //---------------------------------------------------------------------------
  8263. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8264. TObject * Sender)
  8265. {
  8266. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8267. DebugAssert(DirView != NULL);
  8268. DoDirViewLoaded(DirView);
  8269. TransferPresetAutoSelect();
  8270. }
  8271. //---------------------------------------------------------------------------
  8272. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8273. {
  8274. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8275. // first run after installation
  8276. if (double(Updates.LastCheck) == 0)
  8277. {
  8278. // make sure next time there will be an update (if enabled)
  8279. Updates.LastCheck = TDateTime(1);
  8280. WinConfiguration->Updates = Updates;
  8281. }
  8282. else if ((double(Updates.Period) > 0) &&
  8283. (Now() - Updates.LastCheck >= Updates.Period))
  8284. {
  8285. TThreadMethod OnUpdatesChecked = NULL;
  8286. if (!IsUWP())
  8287. {
  8288. OnUpdatesChecked = UpdatesChecked;
  8289. }
  8290. StartUpdateThread(OnUpdatesChecked);
  8291. }
  8292. }
  8293. //---------------------------------------------------------------------------
  8294. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8295. {
  8296. UnicodeString Message;
  8297. bool New;
  8298. TQueryType Type;
  8299. GetUpdatesMessage(Message, New, Type, false);
  8300. if (!Message.IsEmpty())
  8301. {
  8302. if (New)
  8303. {
  8304. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8305. }
  8306. if (!New && (Type != qtWarning))
  8307. {
  8308. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8309. }
  8310. else
  8311. {
  8312. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8313. }
  8314. if (New)
  8315. {
  8316. Configuration->Usage->Inc(L"UpdateNotifications");
  8317. }
  8318. }
  8319. }
  8320. //---------------------------------------------------------------------------
  8321. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8322. {
  8323. RestoreApp();
  8324. bool CanDisplay = !NonVisualDataModule->Busy;
  8325. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8326. {
  8327. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8328. }
  8329. if (CanDisplay)
  8330. {
  8331. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8332. CheckForUpdates(true);
  8333. }
  8334. else
  8335. {
  8336. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8337. }
  8338. }
  8339. //---------------------------------------------------------------------------
  8340. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8341. TCopyParamRuleData & Data)
  8342. {
  8343. DebugAssert(Terminal != NULL);
  8344. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8345. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8346. Data.RemoteDirectory = RemoteDirView->PathName;
  8347. }
  8348. //---------------------------------------------------------------------------
  8349. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8350. {
  8351. // Terminal can be null when we are changing local directory implicitly,
  8352. // because it has been deleted, while no session is connected
  8353. // (Login dialog is open)
  8354. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8355. {
  8356. DebugAssert(!IsLocalBrowserMode());
  8357. TCopyParamRuleData Data;
  8358. GetTransferPresetAutoSelectData(Data);
  8359. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8360. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8361. if (CopyParamIndex < 0)
  8362. {
  8363. // there is no preset that matches autoselection
  8364. // set preset that we consider "default"
  8365. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8366. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8367. }
  8368. else
  8369. {
  8370. // there is preset matching autoselection
  8371. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8372. if (CopyParamName == FCopyParamAutoSelected)
  8373. {
  8374. // autoselected the same preset as the last time
  8375. // make no change (i.e. preserve custom user preset, if any)
  8376. }
  8377. else
  8378. {
  8379. // autoselected the different preset then the last time (or there
  8380. // was default preset set)
  8381. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8382. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8383. }
  8384. }
  8385. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8386. {
  8387. if (CopyParamIndex >= 0)
  8388. {
  8389. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8390. }
  8391. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8392. try
  8393. {
  8394. int Fmt =
  8395. (CopyParamIndex < 0) ?
  8396. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8397. COPY_PARAM_AUTOSELECTED;
  8398. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8399. Data->Message = MainInstructions(Message);
  8400. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8401. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8402. CopyParamAttrs);
  8403. if (CopyParamIndex >= 0)
  8404. {
  8405. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8406. Info = FORMAT(L"%s\n \n%s", (Info,
  8407. FMTLOAD(COPY_PARAM_RULE,
  8408. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8409. }
  8410. Data->Message += L"\n\n" + Info;
  8411. if (WinConfiguration->CopyParamAutoSelectNotice)
  8412. {
  8413. TransferPresetNoteMessage(Data, true);
  8414. }
  8415. else
  8416. {
  8417. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8418. Data = NULL; // ownership passed
  8419. }
  8420. }
  8421. __finally
  8422. {
  8423. delete Data;
  8424. }
  8425. }
  8426. }
  8427. }
  8428. //---------------------------------------------------------------------------
  8429. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8430. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8431. {
  8432. DebugAssert(NoteData != NULL);
  8433. DebugAssert(!IsLocalBrowserMode());
  8434. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8435. TQueryButtonAlias Aliases[1];
  8436. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8437. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8438. Params.Aliases = Aliases;
  8439. Params.AliasesCount = LENOF(Aliases);
  8440. unsigned int Result =
  8441. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8442. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8443. switch (Result)
  8444. {
  8445. case qaNeverAskAgain:
  8446. DebugAssert(AllowNeverAskAgain);
  8447. WinConfiguration->CopyParamAutoSelectNotice = false;
  8448. break;
  8449. case qaIgnore:
  8450. PreferencesDialog(pmPresets);
  8451. break;
  8452. }
  8453. }
  8454. //---------------------------------------------------------------------------
  8455. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8456. {
  8457. // as of now useless, as this is used for notes only, never for balloons, ...
  8458. RestoreApp();
  8459. // .. and we should never be busy here
  8460. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8461. {
  8462. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8463. }
  8464. }
  8465. //---------------------------------------------------------------------------
  8466. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8467. TPreferencesMode APreferencesMode)
  8468. {
  8469. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8470. TCopyParamRuleData Data;
  8471. if (Terminal != NULL)
  8472. {
  8473. PreferencesData.reset(new TPreferencesDialogData());
  8474. GetTransferPresetAutoSelectData(Data);
  8475. PreferencesData->CopyParamRuleData = &Data;
  8476. }
  8477. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8478. }
  8479. //---------------------------------------------------------------------------
  8480. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8481. const TCustomCommandType & Command)
  8482. {
  8483. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8484. {
  8485. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (Command.Command)));
  8486. }
  8487. }
  8488. //---------------------------------------------------------------------------
  8489. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8490. {
  8491. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8492. TCustomCommandType Command;
  8493. // make sure we use local custom command when remote are not supported
  8494. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8495. {
  8496. Command = FLastCustomCommand;
  8497. }
  8498. else
  8499. {
  8500. Command.Params = Command.Params | ccLocal;
  8501. }
  8502. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8503. FEditingFocusedAdHocCommand = OnFocused;
  8504. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8505. int Options =
  8506. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8507. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8508. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8509. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8510. {
  8511. FLastCustomCommand = Command;
  8512. UpdateCustomCommandsToolbar();
  8513. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8514. }
  8515. }
  8516. //---------------------------------------------------------------------------
  8517. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8518. {
  8519. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8520. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8521. DebugAssert(State > 0);
  8522. if (State <= 0)
  8523. {
  8524. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE2, (FLastCustomCommand.Command)));
  8525. }
  8526. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8527. }
  8528. //---------------------------------------------------------------------------
  8529. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8530. TCustomCommandType & Command, int & State)
  8531. {
  8532. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8533. if (Result)
  8534. {
  8535. Command = FLastCustomCommand;
  8536. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8537. }
  8538. return Result;
  8539. }
  8540. //---------------------------------------------------------------------------
  8541. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8542. {
  8543. ResetIncrementalSearch();
  8544. }
  8545. //---------------------------------------------------------------------------
  8546. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8547. {
  8548. DebugAssert(ComponentVisible[Component]);
  8549. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8550. }
  8551. //---------------------------------------------------------------------------
  8552. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8553. {
  8554. // when queue panel is resized here directly, the status bar is stretched
  8555. // over whole space the panel occupied
  8556. PostComponentHide(fcQueueView);
  8557. }
  8558. //---------------------------------------------------------------------------
  8559. void __fastcall TCustomScpExplorerForm::QueueFileListSplitterDblClick(TObject *)
  8560. {
  8561. ComponentVisible[fcQueueFileList] = false;
  8562. }
  8563. //---------------------------------------------------------------------------
  8564. void __fastcall TCustomScpExplorerForm::ThemeChanged()
  8565. {
  8566. // We hoped this will refresh scrollbar colors, but it does not have any effect here.
  8567. RefreshColorMode();
  8568. ResetSysDarkTheme();
  8569. ConfigurationChanged();
  8570. ConfigureInterface();
  8571. // Should be called for all controls
  8572. RemoteDirView->Perform(WM_THEMECHANGED, 0, 0);
  8573. }
  8574. //---------------------------------------------------------------------------
  8575. void __fastcall TCustomScpExplorerForm::WMSettingChange(TMessage & Message)
  8576. {
  8577. // Do not handle, when shutting down anyway (maybe also when not setup completelly yet?)
  8578. if (!FInvalid &&
  8579. (Message.LParam != 0) &&
  8580. (wcscmp(reinterpret_cast<LPCWCH>(Message.LParam), L"ImmersiveColorSet") == 0))
  8581. {
  8582. ThemeChanged();
  8583. }
  8584. TForm::Dispatch(&Message);
  8585. }
  8586. //---------------------------------------------------------------------------
  8587. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8588. {
  8589. TMessage * M = static_cast<TMessage*>(Message);
  8590. switch (M->Msg)
  8591. {
  8592. case CM_DIALOGCHAR:
  8593. CMDialogChar(*M);
  8594. break;
  8595. case WM_APPCOMMAND:
  8596. WMAppCommand(*M);
  8597. break;
  8598. case WM_SYSCOMMAND:
  8599. WMSysCommand(*M);
  8600. break;
  8601. case WM_QUERYENDSESSION:
  8602. WMQueryEndSession(*M);
  8603. break;
  8604. case WM_ENDSESSION:
  8605. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8606. break;
  8607. #ifdef _DEBUG
  8608. case WM_WINDOWPOSCHANGED:
  8609. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8610. break;
  8611. case CM_CUSTOMSTYLECHANGED:
  8612. TForm::Dispatch(Message);
  8613. break;
  8614. #endif
  8615. case WM_COMPONENT_HIDE:
  8616. {
  8617. Byte Component = static_cast<Byte>(M->WParam);
  8618. // sanity check
  8619. if (ComponentVisible[Component])
  8620. {
  8621. // special treatment
  8622. if (Component == fcQueueView)
  8623. {
  8624. ToggleQueueVisibility();
  8625. DebugAssert(!ComponentVisible[fcQueueView]);
  8626. }
  8627. else
  8628. {
  8629. ComponentVisible[Component] = false;
  8630. }
  8631. }
  8632. }
  8633. break;
  8634. case WM_COPYDATA:
  8635. WMCopyData(*M);
  8636. break;
  8637. case WM_MANAGES_CAPTION:
  8638. // caption managed in TTerminalManager::UpdateAppTitle()
  8639. M->Result = 1;
  8640. break;
  8641. case WM_WANTS_MOUSEWHEEL:
  8642. M->Result = 1;
  8643. break;
  8644. case CM_SHOWINGCHANGED:
  8645. CMShowingChanged(*M);
  8646. break;
  8647. case WM_CLOSE:
  8648. WMClose(*M);
  8649. break;
  8650. case WM_DPICHANGED:
  8651. WMDpiChanged(*M);
  8652. break;
  8653. case CM_DPICHANGED:
  8654. CMDpiChanged(*M);
  8655. break;
  8656. case WM_SETTINGCHANGE:
  8657. WMSettingChange(*M);
  8658. break;
  8659. case CM_DIALOGKEY:
  8660. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8661. break;
  8662. default:
  8663. TForm::Dispatch(Message);
  8664. break;
  8665. }
  8666. }
  8667. //---------------------------------------------------------------------------
  8668. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8669. {
  8670. // noop
  8671. }
  8672. //---------------------------------------------------------------------------
  8673. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8674. {
  8675. TForm::Dispatch(&Message);
  8676. FSessionColors->Clear();
  8677. AddFixedSessionImages();
  8678. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8679. UpdateImages();
  8680. }
  8681. //---------------------------------------------------------------------------
  8682. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8683. {
  8684. TForm::Dispatch(&Message);
  8685. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8686. UpdatePixelsPerInchMainWindowCounter();
  8687. }
  8688. //---------------------------------------------------------------------------
  8689. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8690. {
  8691. // Cannot close window while we are busy.
  8692. // We cannot test this in FormCloseQuery as that is called also from
  8693. // Close(), which is called by CloseApplicationAction. So we can be busy
  8694. // there already even, when it is legitimate to close the application.
  8695. // Possibly a better place to handle this would be WMSysCommand.
  8696. if (NonVisualDataModule->Busy)
  8697. {
  8698. Message.Result = 1;
  8699. }
  8700. else
  8701. {
  8702. TForm::Dispatch(&Message);
  8703. }
  8704. }
  8705. //---------------------------------------------------------------------------
  8706. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8707. {
  8708. // Ignoring implicit local-local browser of Commander, opened from DoShow
  8709. bool NoSession = (ManagedSession == NULL);
  8710. // This calls TCustomScpExplorerForm::DoShow
  8711. TForm::Dispatch(&Message);
  8712. // Now the window is visible (TForm::Dispatch is what usually takes long, like when loading a "local" network directory)
  8713. if (Showing && !FStarted)
  8714. {
  8715. FStarted = true;
  8716. InterfaceStarted();
  8717. }
  8718. if (Showing && NoSession)
  8719. {
  8720. // When we are starting minimized (i.e. from an installer),
  8721. // postpone showing Login dialog until we get restored.
  8722. // Otherwise the Login dialog (and Authentication window) show restored
  8723. // over invisible (minimized) main window.
  8724. if (WindowState == wsMinimized)
  8725. {
  8726. FNeedSession = true;
  8727. }
  8728. else
  8729. {
  8730. // This happens before application ever goes idle, so the toolbars would
  8731. // stay enabled (initial state) until the Login dialog is dismissed.
  8732. UpdateActions();
  8733. NonVisualDataModule->StartBusy();
  8734. try
  8735. {
  8736. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8737. // otherwise the dialog does not receive focus.
  8738. // With Commander interface the ProcessMessages is called already
  8739. // by TDriveView, but with Explorer interface, we need to call it explicily
  8740. Application->ProcessMessages();
  8741. // do not reload sessions, they have been loaded just now (optimization)
  8742. NeedSession(true);
  8743. }
  8744. __finally
  8745. {
  8746. NonVisualDataModule->EndBusy();
  8747. }
  8748. }
  8749. }
  8750. }
  8751. //---------------------------------------------------------------------------
  8752. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  8753. {
  8754. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  8755. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  8756. }
  8757. //---------------------------------------------------------------------------
  8758. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8759. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8760. int & MaxHeight)
  8761. {
  8762. // workaround for bug in TWinControl.CalcConstraints
  8763. // Check for empty rect (restore from iconic state) is done there only after
  8764. // call to AdjustClientRect, which enlarges the rect (for forms).
  8765. TRect R = GetClientRect();
  8766. // when restoring from iconic state, place no restrictions
  8767. if (IsRectEmpty(R))
  8768. {
  8769. MinWidth = 0;
  8770. MinHeight = 0;
  8771. MaxWidth = 32000;
  8772. MaxHeight = 32000;
  8773. }
  8774. }
  8775. //---------------------------------------------------------------------------
  8776. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8777. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8778. {
  8779. // terminal can be already closed (e.g. dropped connection)
  8780. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8781. {
  8782. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8783. try
  8784. {
  8785. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8786. }
  8787. catch(...)
  8788. {
  8789. if (!Terminal->Active)
  8790. {
  8791. Close = true;
  8792. }
  8793. throw;
  8794. }
  8795. }
  8796. }
  8797. //---------------------------------------------------------------------------
  8798. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8799. {
  8800. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8801. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8802. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8803. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8804. {
  8805. OnGetSpaceAvailable = GetSpaceAvailable;
  8806. }
  8807. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8808. OnGetSpaceAvailable);
  8809. }
  8810. //---------------------------------------------------------------------------
  8811. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8812. {
  8813. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8814. Terminal->FillSessionDataForCode(Data.get());
  8815. return Data.release();
  8816. }
  8817. //---------------------------------------------------------------------------
  8818. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8819. {
  8820. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8821. DoGenerateUrlDialog(Data.get(), Paths);
  8822. }
  8823. //---------------------------------------------------------------------------
  8824. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8825. {
  8826. GenerateUrl(NULL);
  8827. }
  8828. //---------------------------------------------------------------------------
  8829. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8830. {
  8831. std::unique_ptr<TStrings> Paths(new TStringList());
  8832. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8833. GenerateUrl(Paths.get());
  8834. }
  8835. //---------------------------------------------------------------------------
  8836. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  8837. {
  8838. FSessionColor = value;
  8839. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8840. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8841. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8842. DebugAssert(ColorItem != NULL);
  8843. ColorItem->Color = C;
  8844. NonVisualDataModule->ColorMenuItem->Color = C;
  8845. UpdateControls();
  8846. }
  8847. //---------------------------------------------------------------------------
  8848. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8849. {
  8850. if (value != FSessionColor)
  8851. {
  8852. UpdateSessionColor(value);
  8853. // Is null when called from LastTerminalClosed
  8854. if (Terminal != NULL)
  8855. {
  8856. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8857. }
  8858. }
  8859. }
  8860. //---------------------------------------------------------------------------
  8861. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8862. {
  8863. bool Result = FNoteTimer->Enabled;
  8864. if (Result)
  8865. {
  8866. // cannot cancel note too early
  8867. bool NotEarly =
  8868. (Now() - FNoteShown >
  8869. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8870. if (Force || NotEarly)
  8871. {
  8872. FNoteTimer->Enabled = false;
  8873. FNote = L"";
  8874. SAFE_DESTROY(FNoteData);
  8875. FOnNoteClick = NULL;
  8876. FNoteHints = FNotes->Text;
  8877. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8878. UpdateStatusBar();
  8879. }
  8880. }
  8881. return Result;
  8882. }
  8883. //---------------------------------------------------------------------------
  8884. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8885. {
  8886. DebugAssert(FNoteTimer->Enabled);
  8887. CancelNote(true);
  8888. }
  8889. //---------------------------------------------------------------------------
  8890. void __fastcall TCustomScpExplorerForm::UpdateNoteHints()
  8891. {
  8892. FNoteHints = FNotes->Text.TrimRight();
  8893. }
  8894. //---------------------------------------------------------------------------
  8895. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8896. {
  8897. int P = Note.Pos(L"\n");
  8898. if (P > 0)
  8899. {
  8900. Note.SetLength(P - 1);
  8901. }
  8902. FNotes->Add(FORMAT(L"[%s] %s",
  8903. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8904. while (FNotes->Count > 10)
  8905. {
  8906. FNotes->Delete(0);
  8907. }
  8908. if (UpdateNow)
  8909. {
  8910. UpdateNoteHints();
  8911. UpdateStatusBar();
  8912. }
  8913. }
  8914. //---------------------------------------------------------------------------
  8915. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8916. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8917. {
  8918. int P = Note.Pos(L"\n");
  8919. if (P > 0)
  8920. {
  8921. Note.SetLength(P - 1);
  8922. }
  8923. UpdateNoteHints();
  8924. FNote = Note;
  8925. SAFE_DESTROY(FNoteData);
  8926. FNoteData = NoteData;
  8927. FOnNoteClick = OnNoteClick;
  8928. AddNote(Note, false);
  8929. UpdateStatusBar();
  8930. FNoteShown = Now();
  8931. FNoteTimer->Enabled = false;
  8932. if (Seconds == 0)
  8933. {
  8934. Seconds = WinConfiguration->NotificationsTimeout;
  8935. }
  8936. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8937. FNoteTimer->Enabled = true;
  8938. }
  8939. //---------------------------------------------------------------------------
  8940. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8941. {
  8942. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8943. }
  8944. //---------------------------------------------------------------------------
  8945. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8946. {
  8947. if (NonVisualDataModule->SynchronizeBrowsingAction2->Checked)
  8948. {
  8949. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8950. }
  8951. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  8952. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  8953. (NonVisualDataModule->SynchronizeBrowsingAction2->Checked ? 2 : 3)))),
  8954. 0, NULL, NULL);
  8955. }
  8956. //---------------------------------------------------------------------------
  8957. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  8958. {
  8959. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  8960. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  8961. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  8962. GetComponent(fcStatusBar)->Repaint(); // toggling ShowHiddenFiles takes time, force repaint beforehand
  8963. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  8964. }
  8965. //---------------------------------------------------------------------------
  8966. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  8967. {
  8968. WinConfiguration->FormatSizeBytes = Style;
  8969. }
  8970. //---------------------------------------------------------------------------
  8971. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  8972. {
  8973. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  8974. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  8975. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  8976. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  8977. }
  8978. //---------------------------------------------------------------------------
  8979. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  8980. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  8981. {
  8982. if (Panel->Index == 0)
  8983. {
  8984. if (FOnNoteClick != NULL)
  8985. {
  8986. // prevent the user data from being freed by possible call
  8987. // to CancelNote or PostNote during call to OnNoteClick
  8988. std::unique_ptr<TObject> NoteData(FNoteData);
  8989. TNotifyEvent OnNoteClick = FOnNoteClick;
  8990. FNoteData = NULL;
  8991. // need to cancel the note as we are going to delete its user data
  8992. CancelNote(true);
  8993. OnNoteClick(NoteData.get());
  8994. }
  8995. }
  8996. if (HasActiveTerminal() &&
  8997. (Panel->Index >= Sender->Panels->Count - SessionPanelCount))
  8998. {
  8999. FileSystemInfo();
  9000. }
  9001. }
  9002. //---------------------------------------------------------------------------
  9003. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  9004. {
  9005. // workaround:
  9006. // 1) for unknown reason, disabling window, while minimized,
  9007. // prevents it from restoring, even if it was enabled again meanwhile
  9008. // 2) when disabling the main window, while another has focus
  9009. // minimize is no longer possible
  9010. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  9011. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  9012. // Shouldn't we use IsApplicationMinimized() here?
  9013. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  9014. {
  9015. Enabled = false;
  9016. }
  9017. FLockLevel++;
  9018. }
  9019. //---------------------------------------------------------------------------
  9020. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  9021. {
  9022. DebugAssert(FLockLevel > 0);
  9023. FLockLevel--;
  9024. if (FLockLevel == 0)
  9025. {
  9026. DebugAssert(FLockSuspendLevel == 0);
  9027. Enabled = true;
  9028. }
  9029. }
  9030. //---------------------------------------------------------------------------
  9031. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  9032. {
  9033. // We need to make sure that window is enabled when the last modal window closes
  9034. // otherwise focus is not restored correctly.
  9035. // So we re-enable the window when modal window opens and
  9036. // disable it back after is closes
  9037. if (FLockLevel > 0)
  9038. {
  9039. // while we have nesting counter, we know that we never be called
  9040. // recursivelly as Application->OnModalBegin is called only
  9041. // for the top-level modal window
  9042. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9043. {
  9044. // won't be disabled when conditions in LockWindow() were not satisfied
  9045. FDisabledOnLockSuspend = !Enabled;
  9046. // When minimized to tray (or actually when set to SW_HIDE),
  9047. // setting Enabled makes the window focusable even when there's
  9048. // modal window over it
  9049. if (!FTrayIcon->Visible)
  9050. {
  9051. Enabled = true;
  9052. }
  9053. }
  9054. FLockSuspendLevel++;
  9055. }
  9056. }
  9057. //---------------------------------------------------------------------------
  9058. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  9059. {
  9060. if (FLockSuspendLevel > 0)
  9061. {
  9062. DebugAssert(FLockLevel > 0);
  9063. FLockSuspendLevel--;
  9064. // see comment in SuspendWindowLock
  9065. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  9066. {
  9067. // Note that window can be enabled here, when we were minized to tray when
  9068. // was SuspendWindowLock() called.
  9069. // We should possibly do the same check as in LockWindow(),
  9070. // if it is ever possible that the consitions change between
  9071. // SuspendWindowLock() and ResumeWindowLock()
  9072. Enabled = !FDisabledOnLockSuspend;
  9073. }
  9074. }
  9075. }
  9076. //---------------------------------------------------------------------------
  9077. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  9078. {
  9079. TTBXComboBoxItem * RemotePathComboBox =
  9080. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  9081. if (!TextOnly)
  9082. {
  9083. TStrings * Items = RemotePathComboBox->Strings;
  9084. Items->BeginUpdate();
  9085. try
  9086. {
  9087. Items->Clear();
  9088. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  9089. {
  9090. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9091. while (!IsUnixRootPath(APath))
  9092. {
  9093. int P = APath.LastDelimiter(L'/');
  9094. DebugAssert(P >= 0);
  9095. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  9096. APath.SetLength(P - 1);
  9097. }
  9098. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  9099. }
  9100. }
  9101. __finally
  9102. {
  9103. Items->EndUpdate();
  9104. }
  9105. }
  9106. RemotePathComboBox->ItemIndex = RemotePathComboBox->Strings->Count - 1;
  9107. // Setting ItemIndex to -1 does not reset its text
  9108. if (RemotePathComboBox->Strings->Count == 0)
  9109. {
  9110. RemotePathComboBox->Text = L"";
  9111. }
  9112. }
  9113. //---------------------------------------------------------------------------
  9114. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxAdjustImageIndex(
  9115. TTBXComboBoxItem * Sender, const UnicodeString DebugUsedArg(AText), int AIndex, int & ImageIndex)
  9116. {
  9117. if (AIndex < 0)
  9118. {
  9119. AIndex = Sender->ItemIndex;
  9120. }
  9121. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  9122. }
  9123. //---------------------------------------------------------------------------
  9124. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  9125. TTBXComboBoxItem * Sender, const UnicodeString AText, int AIndex,
  9126. int & ImageIndex)
  9127. {
  9128. DoRemotePathComboBoxAdjustImageIndex(Sender, AText, AIndex, ImageIndex);
  9129. }
  9130. //---------------------------------------------------------------------------
  9131. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  9132. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  9133. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  9134. {
  9135. if (!IsLocalBrowserMode())
  9136. {
  9137. ARect.Left += (10 * AIndex);
  9138. }
  9139. }
  9140. //---------------------------------------------------------------------------
  9141. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  9142. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  9143. {
  9144. AWidth += (10 * AIndex);
  9145. }
  9146. //---------------------------------------------------------------------------
  9147. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxItemClick(TObject * Sender)
  9148. {
  9149. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  9150. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  9151. int Index = RemotePathComboBox->ItemIndex;
  9152. while (Index < RemotePathComboBox->Strings->Count - 1)
  9153. {
  9154. APath = UnixExtractFileDir(APath);
  9155. Index++;
  9156. }
  9157. // VanDyke style paths
  9158. if (APath.IsEmpty())
  9159. {
  9160. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  9161. APath = ROOTDIRECTORY;
  9162. }
  9163. if (RemoteDirView->Path != APath)
  9164. {
  9165. RemoteDirView->Path = APath;
  9166. }
  9167. }
  9168. //---------------------------------------------------------------------------
  9169. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(TObject * Sender)
  9170. {
  9171. DoRemotePathComboBoxItemClick(Sender);
  9172. }
  9173. //---------------------------------------------------------------------------
  9174. void __fastcall TCustomScpExplorerForm::DoRemotePathComboBoxCancel(TObject * Sender)
  9175. {
  9176. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  9177. DebugUsedParam(Sender);
  9178. UpdateRemotePathComboBox(true);
  9179. }
  9180. //---------------------------------------------------------------------------
  9181. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  9182. {
  9183. DoRemotePathComboBoxCancel(Sender);
  9184. }
  9185. //---------------------------------------------------------------------------
  9186. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  9187. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  9188. {
  9189. ResetIncrementalSearch();
  9190. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9191. DebugAssert(DirView != NULL);
  9192. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  9193. {
  9194. HWND Edit = ListView_GetEditControl(DirView->Handle);
  9195. // OnEditing is called also from TCustomListView::CanEdit
  9196. if (Edit != NULL)
  9197. {
  9198. EditSelectBaseName(Edit);
  9199. }
  9200. }
  9201. }
  9202. //---------------------------------------------------------------------------
  9203. //---------------------------------------------------------------------------
  9204. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  9205. {
  9206. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  9207. Result->AutoDetectDnD = false;
  9208. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  9209. Result->RenderDataOn = rdoEnterAndDropSync;
  9210. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  9211. return Result;
  9212. }
  9213. //---------------------------------------------------------------------------
  9214. void __fastcall TCustomScpExplorerForm::UpdateDarkMode()
  9215. {
  9216. if (IsWin10Build(2004))
  9217. {
  9218. FImmersiveDarkMode = WinConfiguration->UseDarkTheme();
  9219. BOOL DarkMode = FImmersiveDarkMode ? TRUE : FALSE;
  9220. const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  9221. DwmSetWindowAttribute(Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, &DarkMode, sizeof(DarkMode));
  9222. }
  9223. }
  9224. //---------------------------------------------------------------------------
  9225. void __fastcall TCustomScpExplorerForm::CreateWnd()
  9226. {
  9227. TForm::CreateWnd();
  9228. // win32-darkmode calls AllowDarkModeForWindow(this, true) here, but it does not seem to have any effect
  9229. UpdateDarkMode();
  9230. if (FSessionsDragDropFilesEx == NULL)
  9231. {
  9232. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  9233. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  9234. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  9235. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  9236. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  9237. }
  9238. if (FQueueDragDropFilesEx == NULL)
  9239. {
  9240. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  9241. // No need to set OnDragOver as we do not have any restrictions
  9242. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  9243. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  9244. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  9245. }
  9246. }
  9247. //---------------------------------------------------------------------------
  9248. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  9249. {
  9250. TForm::DestroyWnd();
  9251. FSessionsDragDropFilesEx->DragDropControl = NULL;
  9252. FQueueDragDropFilesEx->DragDropControl = NULL;
  9253. }
  9254. //---------------------------------------------------------------------------
  9255. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  9256. {
  9257. SideEnter(FCurrentSide);
  9258. FEverShown = true;
  9259. }
  9260. //---------------------------------------------------------------------------
  9261. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  9262. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  9263. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  9264. {
  9265. if (!NonVisualDataModule->Busy)
  9266. {
  9267. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9268. Configuration->Usage->Inc(L"FileFinds");
  9269. LockWindow(true);
  9270. NonVisualDataModule->StartBusy();
  9271. WinConfiguration->LockedInterface = true;
  9272. try
  9273. {
  9274. Terminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  9275. }
  9276. __finally
  9277. {
  9278. WinConfiguration->LockedInterface = false;
  9279. NonVisualDataModule->EndBusy();
  9280. UnlockWindow();
  9281. }
  9282. }
  9283. }
  9284. //---------------------------------------------------------------------------
  9285. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9286. {
  9287. if (!NonVisualDataModule->Busy)
  9288. {
  9289. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9290. SetFocus();
  9291. RemoteDirView->Path = UnixExtractFilePath(Path);
  9292. UnicodeString FileName = UnixExtractFileName(Path);
  9293. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9294. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9295. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9296. if (Item == NULL)
  9297. {
  9298. ReloadDirectory(osRemote);
  9299. Item = RemoteDirView->FindFileItem(FileName);
  9300. }
  9301. if (Item != NULL)
  9302. {
  9303. RemoteDirView->ItemFocused = Item;
  9304. RemoteDirView->ItemFocused->MakeVisible(false);
  9305. RemoteDirView->SetFocus();
  9306. }
  9307. }
  9308. }
  9309. //---------------------------------------------------------------------------
  9310. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9311. {
  9312. bool Result = !NonVisualDataModule->Busy;
  9313. if (Result)
  9314. {
  9315. TTerminalManager::Instance()->ActiveSession = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9316. }
  9317. return Result;
  9318. }
  9319. //---------------------------------------------------------------------------
  9320. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9321. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9322. {
  9323. if (CanOperateOnFoundFiles(ATerminal))
  9324. {
  9325. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9326. FOnFileOperationFinished = OnFileOperationFinished;
  9327. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9328. }
  9329. }
  9330. //---------------------------------------------------------------------------
  9331. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9332. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9333. {
  9334. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9335. }
  9336. //---------------------------------------------------------------------------
  9337. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9338. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9339. {
  9340. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9341. }
  9342. //---------------------------------------------------------------------------
  9343. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9344. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9345. {
  9346. if (CanOperateOnFoundFiles(ATerminal))
  9347. {
  9348. for (int Index = 0; Index < FileList->Count; Index++)
  9349. {
  9350. UnicodeString FileName = FileList->Strings[Index];
  9351. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9352. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9353. OnFileOperationFinished(FileName, true);
  9354. }
  9355. }
  9356. }
  9357. //---------------------------------------------------------------------------
  9358. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9359. {
  9360. FFileFindTerminal = Terminal;
  9361. ShowFileFindDialog(
  9362. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9363. DoDownloadFoundFiles, DoEditFoundFiles);
  9364. }
  9365. //---------------------------------------------------------------------------
  9366. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9367. {
  9368. FTaskbarList = TaskbarList;
  9369. }
  9370. //---------------------------------------------------------------------------
  9371. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9372. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9373. {
  9374. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9375. if (Index >= 0)
  9376. {
  9377. if (Button == mbLeft)
  9378. {
  9379. // "Mouse down" is raised only after tab is switched.
  9380. // If switching tab (switching session) takes long enough for user
  9381. // to actually release the button, "mouse down" is still raised,
  9382. // but we do not get "mouse up" event, so dragging is not cancelled,
  9383. // prevent that by not beginning dragging in the first place.
  9384. if (IsKeyPressed(VK_LBUTTON))
  9385. {
  9386. // when user clicks the "+", we get mouse down only after the session
  9387. // is closed, when new session tab is already on X:Y, so dragging
  9388. // starts, prevent that
  9389. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  9390. {
  9391. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9392. if (Terminal != NULL)
  9393. {
  9394. SessionsPageControl->BeginDrag(false);
  9395. }
  9396. }
  9397. }
  9398. }
  9399. else if (Button == mbMiddle)
  9400. {
  9401. // ignore middle-click for "New session tab"
  9402. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9403. if (Terminal != NULL)
  9404. {
  9405. CloseSessionTab(Index);
  9406. }
  9407. }
  9408. }
  9409. }
  9410. //---------------------------------------------------------------------------
  9411. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9412. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9413. {
  9414. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9415. // do not allow dropping on the "+" tab
  9416. TTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9417. if ((TargetTerminal != NULL) &&
  9418. (SessionsPageControl->ActivePage->PageIndex != Index))
  9419. {
  9420. Configuration->Usage->Inc(L"SessionTabMoves");
  9421. // this is almost redundant as we would recreate tabs in SessionListChanged,
  9422. // but we want to actually prevent that to avoid flicker
  9423. SessionsPageControl->ActivePage->PageIndex = Index;
  9424. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->ActivePage);
  9425. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9426. }
  9427. }
  9428. //---------------------------------------------------------------------------
  9429. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9430. TObject * Sender, TObject * Source, int X, int Y,
  9431. TDragState /*State*/, bool & Accept)
  9432. {
  9433. Accept = (Sender == Source);
  9434. if (Accept)
  9435. {
  9436. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9437. TTerminal * Terminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9438. // do not allow dragging to the "+" tab
  9439. Accept = (Terminal != NULL);
  9440. }
  9441. }
  9442. //---------------------------------------------------------------------------
  9443. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(
  9444. int DebugUsedArg(KeyState), const TPoint & Point, int & Effect, int DebugUsedArg(PreferredEffect))
  9445. {
  9446. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9447. if (Index < 0)
  9448. {
  9449. Effect = DROPEFFECT_NONE;
  9450. }
  9451. else
  9452. {
  9453. TManagedTerminal * TargetSession = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9454. // do not allow dropping on the "+" tab or on local-local tabs
  9455. if ((TargetSession == NULL) || TargetSession->LocalBrowser)
  9456. {
  9457. Effect = DROPEFFECT_NONE;
  9458. }
  9459. else if ((TargetSession != Terminal) && (Effect == DROPEFFECT_MOVE))
  9460. {
  9461. Effect = DROPEFFECT_COPY;
  9462. }
  9463. }
  9464. }
  9465. //---------------------------------------------------------------------------
  9466. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9467. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9468. {
  9469. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9470. // do not allow dropping on the "+" tab
  9471. TManagedTerminal * TargetTerminal = GetSessionTabSession(SessionsPageControl->Pages[Index]);
  9472. if (TargetTerminal != NULL)
  9473. {
  9474. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9475. if (!IsFileControl(DropSourceControl, osRemote))
  9476. {
  9477. TTerminalManager::Instance()->ActiveSession = TargetTerminal;
  9478. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9479. // Why don't we use Terminal->CurrentDirectory directly?
  9480. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9481. // do not force queue, drag drop
  9482. false, true);
  9483. }
  9484. }
  9485. }
  9486. //---------------------------------------------------------------------------
  9487. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9488. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9489. {
  9490. // Downloads are handled in RemoteFileControlDDEnd
  9491. if (!IsFileControl(DropSourceControl, osRemote))
  9492. {
  9493. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9494. Terminal->CurrentDirectory,
  9495. // force queue, drag drop
  9496. true, true);
  9497. }
  9498. }
  9499. //---------------------------------------------------------------------------
  9500. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9501. {
  9502. FShowing = false;
  9503. // Do not save empty workspace
  9504. if (WinConfiguration->AutoSaveWorkspace && (ManagedSession != NULL))
  9505. {
  9506. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9507. UnicodeString Name = WorkspaceName();
  9508. DoSaveWorkspace(
  9509. Name, DataList.get(),
  9510. !Configuration->DisablePasswordStoring &&
  9511. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9512. WinConfiguration->LastStoredSession = Name;
  9513. }
  9514. }
  9515. //---------------------------------------------------------------------------
  9516. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9517. {
  9518. TManagedTerminal * ManagedTerminal =
  9519. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9520. if (ManagedTerminal != NULL)
  9521. {
  9522. ManagedTerminal->DirectoryLoaded = Now();
  9523. }
  9524. }
  9525. //---------------------------------------------------------------------------
  9526. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9527. TListItem * /*Item*/, bool /*Selected*/)
  9528. {
  9529. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9530. switch (DirView->LastSelectMethod)
  9531. {
  9532. case smKeyboard:
  9533. Configuration->Usage->Inc(L"KeyboardSelections");
  9534. break;
  9535. case smMouse:
  9536. Configuration->Usage->Inc(L"MouseSelections");
  9537. break;
  9538. }
  9539. }
  9540. //---------------------------------------------------------------------------
  9541. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9542. {
  9543. if (Color != 0)
  9544. {
  9545. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9546. }
  9547. else
  9548. {
  9549. return FSessionTabImageIndex;
  9550. }
  9551. }
  9552. //---------------------------------------------------------------------------
  9553. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9554. {
  9555. TPngImageCollectionItem * Item =
  9556. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9557. FSessionColors->AddPng(Item->PngImage);
  9558. return FSessionColors->Count - 1;
  9559. }
  9560. //---------------------------------------------------------------------------
  9561. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9562. {
  9563. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9564. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  9565. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9566. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9567. FLocalBrowserTabImageIndex = AddFixedSessionImage(LocalBrowserImageIndex);
  9568. }
  9569. //---------------------------------------------------------------------------
  9570. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9571. {
  9572. for (int Index = 0; Index < Control->ControlCount; Index++)
  9573. {
  9574. TControl * AControl = Control->Controls[Index];
  9575. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9576. if (WinControl != NULL)
  9577. {
  9578. CollectItemsWithTextDisplayMode(WinControl);
  9579. }
  9580. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9581. if (Toolbar != NULL)
  9582. {
  9583. // we care for top-level items only
  9584. TTBCustomItem * Items = Toolbar->Items;
  9585. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9586. {
  9587. TTBCustomItem * Item = Items->Items[ItemIndex];
  9588. if (((Item->DisplayMode == nbdmImageAndText) ||
  9589. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9590. EligibleForImageDisplayMode(Item))
  9591. {
  9592. FItemsWithTextDisplayMode.insert(Item);
  9593. }
  9594. }
  9595. }
  9596. }
  9597. }
  9598. //---------------------------------------------------------------------------
  9599. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9600. {
  9601. return true;
  9602. }
  9603. //---------------------------------------------------------------------------
  9604. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9605. {
  9606. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9607. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9608. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9609. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9610. bool Result = true;
  9611. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9612. {
  9613. TTBCustomItem * Item = *i;
  9614. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9615. if (Label != NULL)
  9616. {
  9617. // optimization
  9618. if (Label->Visible == SelectiveToolbarText)
  9619. {
  9620. Result = false;
  9621. }
  9622. else
  9623. {
  9624. Label->Visible = SelectiveToolbarText;
  9625. }
  9626. }
  9627. else
  9628. {
  9629. // optimization
  9630. if (Item->DisplayMode == DisplayMode)
  9631. {
  9632. Result = false;
  9633. }
  9634. else
  9635. {
  9636. Item->DisplayMode = DisplayMode;
  9637. }
  9638. }
  9639. ++i;
  9640. }
  9641. if (Result)
  9642. {
  9643. UpdateNewSessionTab();
  9644. }
  9645. return Result;
  9646. }
  9647. //---------------------------------------------------------------------------
  9648. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9649. {
  9650. DebugFail();
  9651. }
  9652. //---------------------------------------------------------------------------
  9653. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9654. {
  9655. // Among other, a lock level is non zero, while directory is being loaded,
  9656. // even when it happens as a result of dirview navigation,
  9657. // i.e. when TNonVisualDataModule is NOT FBusy.
  9658. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9659. // Among other this prevents a panel auto update to occur while
  9660. // directory is changing.
  9661. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9662. }
  9663. //---------------------------------------------------------------------------
  9664. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * Action, TActionAllowed Allowed)
  9665. {
  9666. // While the window is disabled, we still seem to process menu shortcuts at least,
  9667. // so stop it at least here.
  9668. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9669. bool Result;
  9670. if (Allowed == aaUpdate)
  9671. {
  9672. Result = true;
  9673. }
  9674. else
  9675. {
  9676. Result = !NonVisualDataModule->Busy;
  9677. if (Result && (Allowed == aaShortCut))
  9678. {
  9679. // Otherwise F5 for local-local will be swallowed by disabled local<>remote actions in TCustomScpExplorerForm::KeyDown
  9680. Action->Update();
  9681. Result = Action->Enabled;
  9682. }
  9683. }
  9684. return Result;
  9685. }
  9686. //---------------------------------------------------------------------------
  9687. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9688. {
  9689. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9690. }
  9691. //---------------------------------------------------------------------------
  9692. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9693. {
  9694. // This is somewhat redundant to LockWindow() call from
  9695. // TTerminalManager::TerminalReadDirectoryProgress.
  9696. // But disabling window is known not to block keyboard shorcuts
  9697. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9698. if (Busy > 0)
  9699. {
  9700. if (NonVisualDataModule->Busy)
  9701. {
  9702. State = false;
  9703. }
  9704. else
  9705. {
  9706. NonVisualDataModule->StartBusy();
  9707. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9708. LockWindow();
  9709. }
  9710. }
  9711. else if (Busy < 0)
  9712. {
  9713. UnlockWindow();
  9714. NonVisualDataModule->EndBusy();
  9715. }
  9716. else
  9717. {
  9718. State = NonVisualDataModule->Busy;
  9719. }
  9720. }
  9721. //---------------------------------------------------------------------------
  9722. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9723. {
  9724. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9725. // no context menu for "New session tab"
  9726. if ((Index >= 0) && (GetSessionTabSession(SessionsPageControl->Pages[Index]) != NULL))
  9727. {
  9728. SessionsPageControl->ActivePageIndex = Index;
  9729. if (DebugAlwaysTrue(SessionTabSwitched()))
  9730. {
  9731. // copied from TControl.WMContextMenu
  9732. SendCancelMode(SessionsPageControl);
  9733. // explicit popup instead of using PopupMenu property
  9734. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9735. // while connecting yet not-connected session and hence
  9736. // allowing an access to commands over not-completelly connected session
  9737. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9738. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  9739. PopupMenu->PopupComponent = SessionsPageControl;
  9740. PopupMenu->Popup(Point.x, Point.y);
  9741. }
  9742. }
  9743. Handled = true;
  9744. }
  9745. //---------------------------------------------------------------------------
  9746. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9747. {
  9748. return HasActiveTerminal() && Terminal->IsCapable[fcChangePassword];
  9749. }
  9750. //---------------------------------------------------------------------------
  9751. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9752. {
  9753. Configuration->Usage->Inc(L"PasswordChanges");
  9754. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9755. ChangePasswordData->DisableAuthentationsExceptPassword();
  9756. ChangePasswordData->ChangePassword = true;
  9757. std::unique_ptr<TTerminal> ChangePasswordSession(
  9758. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9759. ChangePasswordSession->AutoReadDirectory = false;
  9760. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9761. {
  9762. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9763. }
  9764. else
  9765. {
  9766. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9767. }
  9768. }
  9769. //---------------------------------------------------------------------------
  9770. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9771. {
  9772. // No nice way to assert SSH2
  9773. return HasActiveTerminal() && (Terminal->FSProtocol == cfsSFTP);
  9774. }
  9775. //---------------------------------------------------------------------------
  9776. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9777. {
  9778. TOperationVisualizer OperationVisualizer;
  9779. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9780. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9781. }
  9782. //---------------------------------------------------------------------------
  9783. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9784. {
  9785. TForm::ChangeScale(M, D);
  9786. int APixelsPerInch = GetControlPixelsPerInch(this);
  9787. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9788. try
  9789. {
  9790. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9791. }
  9792. __finally
  9793. {
  9794. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9795. }
  9796. }
  9797. //---------------------------------------------------------------------------
  9798. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9799. {
  9800. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9801. }
  9802. //---------------------------------------------------------------------
  9803. class TPublicControl : public TControl
  9804. {
  9805. friend class TCustomScpExplorerForm;
  9806. };
  9807. //---------------------------------------------------------------------------
  9808. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9809. {
  9810. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9811. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9812. }
  9813. //---------------------------------------------------------------------------
  9814. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9815. {
  9816. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9817. OpenBookmark(Side, Bookmark);
  9818. }
  9819. //---------------------------------------------------------------------------
  9820. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9821. {
  9822. DoBookmarkClick(osLocal, Sender);
  9823. }
  9824. //---------------------------------------------------------------------------
  9825. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9826. {
  9827. DoBookmarkClick(osRemote, Sender);
  9828. }
  9829. //---------------------------------------------------------------------------
  9830. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9831. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9832. {
  9833. if (BookmarkList != NULL)
  9834. {
  9835. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9836. if (IsLocalBrowserMode())
  9837. {
  9838. Side = osLocal;
  9839. }
  9840. if (!WinConfiguration->UseLocationProfiles)
  9841. {
  9842. std::unique_ptr<TStringList> Directories(new TStringList());
  9843. Directories->CaseSensitive = (Side == osRemote);
  9844. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9845. {
  9846. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9847. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9848. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9849. {
  9850. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9851. Item->Caption = EscapeHotkey(Directory);
  9852. Item->ShortCut = Bookmark->ShortCut;
  9853. Item->OnClick = OnBookmarkClick;
  9854. Item->Tag = reinterpret_cast<int>(Bookmark);
  9855. Directories->Add(Directory);
  9856. Menu->Add(Item.release());
  9857. }
  9858. }
  9859. }
  9860. else
  9861. {
  9862. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9863. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9864. {
  9865. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9866. if (!Bookmark->Node.IsEmpty())
  9867. {
  9868. Folders->Add(Bookmark->Node);
  9869. }
  9870. }
  9871. for (int Index = 0; Index < Folders->Count; Index++)
  9872. {
  9873. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9874. Item->Caption = Folders->Strings[Index];
  9875. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction2->ImageIndex;
  9876. Folders->Objects[Index] = Item.get();
  9877. Menu->Add(Item.release());
  9878. }
  9879. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9880. {
  9881. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9882. TTBCustomItem * Parent;
  9883. if (!Bookmark->Node.IsEmpty())
  9884. {
  9885. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9886. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9887. }
  9888. else
  9889. {
  9890. Parent = Menu;
  9891. }
  9892. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9893. Item->Caption = Bookmark->Name;
  9894. Item->ShortCut = Bookmark->ShortCut;
  9895. Item->OnClick = OnBookmarkClick;
  9896. Item->Tag = reinterpret_cast<int>(Bookmark);
  9897. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9898. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9899. {
  9900. // No hint for location profiles that are actually mere "bookmarks"
  9901. }
  9902. else
  9903. {
  9904. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  9905. UnicodeString LongHint;
  9906. if (!Bookmark->Local.IsEmpty())
  9907. {
  9908. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  9909. }
  9910. if (!Bookmark->Remote.IsEmpty())
  9911. {
  9912. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  9913. }
  9914. AddToList(Hint, LongHint, L"|");
  9915. Item->Hint = Hint;
  9916. }
  9917. Parent->Add(Item.release());
  9918. }
  9919. }
  9920. AddMenuSeparator(Menu);
  9921. }
  9922. }
  9923. //---------------------------------------------------------------------------
  9924. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  9925. {
  9926. Menu->Clear();
  9927. std::unique_ptr<TTBCustomItem> Item;
  9928. Item.reset(new TTBXItem(Owner));
  9929. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  9930. Item->Options = Item->Options << tboDefault;
  9931. Menu->Add(Item.release());
  9932. AddMenuSeparator(Menu);
  9933. if (Terminal != NULL)
  9934. {
  9935. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  9936. }
  9937. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  9938. Item.reset(new TTBXItem(Owner));
  9939. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction2 : NonVisualDataModule->RemoteAddBookmarkAction2;
  9940. Menu->Add(Item.release());
  9941. }
  9942. //---------------------------------------------------------------------------
  9943. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  9944. {
  9945. bool Result = true;
  9946. bool Remote = !IsSideLocalBrowser(Side);
  9947. try
  9948. {
  9949. if (Remote)
  9950. {
  9951. Terminal->ExceptionOnFail = true;
  9952. }
  9953. try
  9954. {
  9955. DirView(Side)->Path = Path;
  9956. }
  9957. __finally
  9958. {
  9959. if (Remote)
  9960. {
  9961. Terminal->ExceptionOnFail = false;
  9962. }
  9963. }
  9964. }
  9965. catch (Exception & E)
  9966. {
  9967. if (!Remote || Terminal->Active)
  9968. {
  9969. ShowExtendedExceptionEx(Terminal, &E);
  9970. Result = false;
  9971. }
  9972. else
  9973. {
  9974. throw;
  9975. }
  9976. }
  9977. return Result;
  9978. }
  9979. //---------------------------------------------------------------------------
  9980. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  9981. {
  9982. // Make sure there some feedback even when loading tiny local folders
  9983. TInstantOperationVisualizer Visualizer;
  9984. DirView(Side)->ReloadDirectory();
  9985. }
  9986. //---------------------------------------------------------------------------
  9987. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  9988. {
  9989. if (Index == SessionsPageControl->TabIndex)
  9990. {
  9991. CloseSession();
  9992. }
  9993. else
  9994. {
  9995. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  9996. TTerminal * Terminal = GetSessionTabSession(TabSheet);
  9997. TTerminalManager * Manager = TTerminalManager::Instance();
  9998. if (!Terminal->Active)
  9999. {
  10000. Manager->FreeTerminal(Terminal);
  10001. }
  10002. else
  10003. {
  10004. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  10005. if (CanCloseQueue(Queue))
  10006. {
  10007. Manager->FreeTerminal(Terminal);
  10008. }
  10009. }
  10010. }
  10011. }
  10012. //---------------------------------------------------------------------------
  10013. void __fastcall TCustomScpExplorerForm::SessionsPageControlCloseButtonClick(TPageControl * /*Sender*/, int Index)
  10014. {
  10015. CloseSessionTab(Index);
  10016. }
  10017. //---------------------------------------------------------------------------
  10018. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  10019. {
  10020. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  10021. {
  10022. TInstantOperationVisualizer Visualizer;
  10023. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  10024. // before we create a new data object
  10025. ClipboardClear();
  10026. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  10027. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  10028. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  10029. // Intermediate variable to WORKAROUND a crash in compiler
  10030. TObjectList * ClipboardFakeMonitors =
  10031. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  10032. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  10033. if (FClipboardDragDropFilesEx.get() == NULL)
  10034. {
  10035. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  10036. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  10037. }
  10038. FClipboardDragDropFilesEx->FileList->Clear();
  10039. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  10040. FClipboardDragDropFilesEx->CopyToClipboard();
  10041. FClipboardTerminal = Terminal;
  10042. // Need full paths, as cwd can be different once files are pasted
  10043. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  10044. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  10045. {
  10046. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  10047. }
  10048. }
  10049. }
  10050. //---------------------------------------------------------------------------
  10051. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  10052. {
  10053. if (OpenClipboard(0))
  10054. {
  10055. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  10056. CloseClipboard();
  10057. }
  10058. }
  10059. //---------------------------------------------------------------------------
  10060. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  10061. {
  10062. return !FClipboardFakeDirectory.IsEmpty();
  10063. }
  10064. //---------------------------------------------------------------------------
  10065. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  10066. {
  10067. RemoveDir(ApiPath(FClipboardFakeDirectory));
  10068. FClipboardFakeDirectory = UnicodeString();
  10069. FClipboardTerminal = NULL;
  10070. // Also called in Idle()
  10071. FClipboardFakeMonitors.reset(NULL);
  10072. }
  10073. //---------------------------------------------------------------------------
  10074. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  10075. {
  10076. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  10077. {
  10078. ClipboardStop();
  10079. }
  10080. }
  10081. //---------------------------------------------------------------------------
  10082. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  10083. {
  10084. TTerminalManager * Manager = TTerminalManager::Instance();
  10085. Manager->ActiveSession = FClipboardTerminal;
  10086. TTransferOperationParam Params;
  10087. Params.TargetDirectory = TargetDirectory;
  10088. Params.DragDrop = DragDrop;
  10089. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  10090. }
  10091. //---------------------------------------------------------------------------
  10092. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  10093. {
  10094. // It can actually rarelly happen that some random file is created, while we are shuttting down the monitor
  10095. // (as it pumps a Windows message queue while being shutted down)
  10096. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  10097. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  10098. {
  10099. // Can fail as it can be e.g. locked by AV, so we retry that later
  10100. bool Removed = RemoveDir(ApiPath(FileName));
  10101. try
  10102. {
  10103. if (!NonVisualDataModule->Busy)
  10104. {
  10105. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  10106. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  10107. }
  10108. }
  10109. __finally
  10110. {
  10111. if (!Removed)
  10112. {
  10113. RemoveDir(ApiPath(FileName));
  10114. }
  10115. }
  10116. }
  10117. }
  10118. //---------------------------------------------------------------------------
  10119. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  10120. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  10121. {
  10122. TFileMasks::TParams MaskParams;
  10123. MaskParams.Size = Size;
  10124. MaskParams.Modification = Modification;
  10125. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  10126. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  10127. {
  10128. bool ImplicitMatch;
  10129. if (Iter->FileMask.Matches(FileName, Directory, DirView->PathName, &MaskParams, false, ImplicitMatch) &&
  10130. !ImplicitMatch)
  10131. {
  10132. Color = Iter->Color;
  10133. break;
  10134. }
  10135. }
  10136. }
  10137. //---------------------------------------------------------------------------
  10138. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  10139. {
  10140. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10141. if (!DirView->IsEditing())
  10142. {
  10143. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  10144. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  10145. // while searching
  10146. if (Key == VK_BACK)
  10147. {
  10148. if (!FIncrementalSearch.IsEmpty())
  10149. {
  10150. if (FIncrementalSearch.Length() == 1)
  10151. {
  10152. ResetIncrementalSearch();
  10153. }
  10154. else
  10155. {
  10156. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  10157. IncrementalSearch(NewText, false, false);
  10158. }
  10159. Key = 0;
  10160. }
  10161. }
  10162. }
  10163. }
  10164. //---------------------------------------------------------------------------
  10165. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  10166. {
  10167. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  10168. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  10169. {
  10170. // Filter control sequences.
  10171. // When not searching yet, prefer use of the space for toggling file selection
  10172. // (so we cannot incrementally search for a string starting with a space).
  10173. if ((Key > VK_SPACE) ||
  10174. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  10175. {
  10176. IncrementalSearch(FIncrementalSearch + Key, false, false);
  10177. Key = 0;
  10178. }
  10179. }
  10180. }
  10181. //---------------------------------------------------------------------------
  10182. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  10183. {
  10184. if (!FIncrementalSearch.IsEmpty())
  10185. {
  10186. FIncrementalSearch = L"";
  10187. DirView(osCurrent)->UpdateStatusBar();
  10188. }
  10189. }
  10190. //---------------------------------------------------------------------------
  10191. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10192. {
  10193. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  10194. if (Item == NULL)
  10195. {
  10196. MessageBeep(MB_ICONHAND);
  10197. }
  10198. else
  10199. {
  10200. TCustomDirView * ADirView = DirView(osCurrent);
  10201. {
  10202. TAutoNestingCounter Guard(FIncrementalSearching);
  10203. ADirView->FocusItem(Item);
  10204. if (ADirView->NortonLike == nlOff)
  10205. {
  10206. if ((ADirView->Selected != Item) ||
  10207. (ADirView->SelCount > 1))
  10208. {
  10209. ADirView->ClearSelection();
  10210. ADirView->Selected = Item;
  10211. }
  10212. }
  10213. }
  10214. FIncrementalSearch = Text;
  10215. Item->MakeVisible(false);
  10216. TListItem * NextItem = SearchFile(Text, true, Reverse);
  10217. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  10218. ADirView->UpdateStatusBar();
  10219. }
  10220. }
  10221. //---------------------------------------------------------------------------
  10222. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  10223. {
  10224. int Index = Item->Index;
  10225. if (!Reverse)
  10226. {
  10227. Index++;
  10228. if (Index >= Item->Owner->Count)
  10229. {
  10230. Index = 0;
  10231. }
  10232. }
  10233. else
  10234. {
  10235. Index--;
  10236. if (Index < 0)
  10237. {
  10238. Index = Item->Owner->Count - 1;
  10239. }
  10240. }
  10241. return Item->Owner->Item[Index];
  10242. }
  10243. //---------------------------------------------------------------------------
  10244. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  10245. {
  10246. TCustomDirView * ADirView = DirView(osCurrent);
  10247. if (ADirView->Items->Count == 0)
  10248. {
  10249. return NULL;
  10250. }
  10251. else
  10252. {
  10253. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  10254. TListItem * Item = CurrentItem;
  10255. if (SkipCurrent)
  10256. {
  10257. Item = DebugNotNull(GetNextFile(Item, Reverse));
  10258. }
  10259. while (true)
  10260. {
  10261. bool Matches = false;
  10262. switch (WinConfiguration->PanelSearch)
  10263. {
  10264. case isNameStartOnly:
  10265. Matches = ContainsTextSemiCaseSensitive(Item->Caption.SubString(1, Text.Length()), Text);
  10266. break;
  10267. case isName:
  10268. Matches = ContainsTextSemiCaseSensitive(Item->Caption, Text);
  10269. break;
  10270. case isAll:
  10271. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  10272. int Index = 0;
  10273. while ((Index < ColCount) && !Matches)
  10274. {
  10275. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  10276. if (Visible)
  10277. {
  10278. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  10279. }
  10280. Index++;
  10281. }
  10282. break;
  10283. }
  10284. if (Matches)
  10285. {
  10286. return Item;
  10287. }
  10288. Item = GetNextFile(Item, Reverse);
  10289. if (Item == CurrentItem)
  10290. {
  10291. return NULL;
  10292. }
  10293. }
  10294. }
  10295. }
  10296. //---------------------------------------------------------------------------
  10297. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  10298. {
  10299. ResetIncrementalSearch();
  10300. }
  10301. //---------------------------------------------------------------------------
  10302. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  10303. {
  10304. if (Message.CharCode == VK_TAB)
  10305. {
  10306. if (!FIncrementalSearch.IsEmpty())
  10307. {
  10308. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10309. bool Reverse = Shift.Contains(ssShift);
  10310. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10311. Message.Result = 1;
  10312. return;
  10313. }
  10314. }
  10315. else if (Message.CharCode == VK_ESCAPE)
  10316. {
  10317. if (!FIncrementalSearch.IsEmpty())
  10318. {
  10319. ResetIncrementalSearch();
  10320. Message.Result = 1;
  10321. return;
  10322. }
  10323. }
  10324. TForm::Dispatch(&Message);
  10325. }
  10326. //---------------------------------------------------------------------------
  10327. void __fastcall TCustomScpExplorerForm::Deactivate()
  10328. {
  10329. TForm::Deactivate();
  10330. // E.g. when switching to an internal editor window
  10331. ResetIncrementalSearch();
  10332. }
  10333. //---------------------------------------------------------------------------
  10334. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10335. {
  10336. // When switching to another application
  10337. ResetIncrementalSearch();
  10338. }
  10339. //---------------------------------------------------------------------------
  10340. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10341. {
  10342. ResetIncrementalSearch();
  10343. }
  10344. //---------------------------------------------------------------------------
  10345. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10346. {
  10347. if (FIncrementalSearching <= 0)
  10348. {
  10349. ResetIncrementalSearch();
  10350. }
  10351. }
  10352. //---------------------------------------------------------------------------
  10353. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10354. {
  10355. CountClicksForWindowPrint(this);
  10356. }
  10357. //---------------------------------------------------------------------------
  10358. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10359. {
  10360. CenterReconnectToolbar();
  10361. }
  10362. //---------------------------------------------------------------------------
  10363. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10364. {
  10365. if (RemoteDirView->ItemFocused != NULL)
  10366. {
  10367. RemoteDirView->ItemFocused->Selected = true;
  10368. }
  10369. }
  10370. //---------------------------------------------------------------------------
  10371. void __fastcall TCustomScpExplorerForm::UpdateQueueFileList()
  10372. {
  10373. if (!FQueueStatusUpdating)
  10374. {
  10375. bool Refresh;
  10376. if ((FQueueStatus != NULL) && QueueFileList->Visible)
  10377. {
  10378. Refresh = FQueueStatus->UpdateFileList(FQueueController->GetFocusedPrimaryItem(), FQueueFileList.get());
  10379. }
  10380. else
  10381. {
  10382. if (FQueueFileList->GetCount() > 0)
  10383. {
  10384. FQueueFileList->Clear();
  10385. Refresh = true;
  10386. }
  10387. }
  10388. int Count = FQueueFileList->GetCount();
  10389. if (QueueFileList->Items->Count != Count)
  10390. {
  10391. DebugAssert(Refresh);
  10392. QueueFileList->Items->Count = Count;
  10393. }
  10394. if (Refresh)
  10395. {
  10396. QueueFileList->Invalidate();
  10397. }
  10398. DebugAssert(QueueFileList->Items->Count == FQueueFileList->GetCount());
  10399. }
  10400. }
  10401. //---------------------------------------------------------------------------
  10402. void __fastcall TCustomScpExplorerForm::QueueFileListData(TObject *, TListItem * Item)
  10403. {
  10404. Item->Caption = FQueueFileList->GetFileName(Item->Index);
  10405. }
  10406. //---------------------------------------------------------------------------
  10407. void __fastcall TCustomScpExplorerForm::QueueView3Change(TObject *, TListItem *, TItemChange)
  10408. {
  10409. // Should be improved, once we do not refresh the list when switching between details of the same batch.
  10410. // See TSynchronizeChecklistDialog::UpdateTimer
  10411. if (QueueFileList->Items->Count > 0)
  10412. {
  10413. QueueFileList->Items->Item[0]->MakeVisible(false);
  10414. }
  10415. UpdateQueueFileList();
  10416. }
  10417. //---------------------------------------------------------------------------
  10418. void __fastcall TCustomScpExplorerForm::QueueLabelGetStatus(TCustomPathLabel *, bool & Active)
  10419. {
  10420. Active = QueueView3->Focused() || QueueFileList->Focused();
  10421. }
  10422. //---------------------------------------------------------------------------
  10423. void __fastcall TCustomScpExplorerForm::QueueFileListEnterExit(TObject *)
  10424. {
  10425. QueueLabelUpdateStatus();
  10426. }
  10427. //---------------------------------------------------------------------------
  10428. void __fastcall TCustomScpExplorerForm::QueueFileListCustomDrawItem(
  10429. TCustomListView * Sender, TListItem * Item, TCustomDrawState, bool & DebugUsedArg(DefaultDraw))
  10430. {
  10431. int State = FQueueFileList->GetState(Item->Index);
  10432. if (State == qfsProcessed)
  10433. {
  10434. Sender->Canvas->Font->Color = clGrayText;
  10435. }
  10436. }
  10437. //---------------------------------------------------------------------------
  10438. void __fastcall TCustomScpExplorerForm::QueueFileListColumnAutoSize()
  10439. {
  10440. // We do not really need whole width, so we always preemptivelly keep free space for the vertical scrollbar
  10441. QueueFileList->Column[0]->Width =
  10442. QueueFileList->ClientWidth - GetSystemMetricsForControl(QueueFileList, SM_CXVSCROLL) - ScaleByTextHeight(QueueFileList, 8);
  10443. }
  10444. //---------------------------------------------------------------------------
  10445. void __fastcall TCustomScpExplorerForm::QueueFileListResize(TObject *)
  10446. {
  10447. QueueFileListColumnAutoSize();
  10448. }
  10449. //---------------------------------------------------------------------------
  10450. void __fastcall TCustomScpExplorerForm::CloseApp()
  10451. {
  10452. // Called from TNonVisualDataModule::ExplorerActionsExecute, which sets busy flag.
  10453. // FormCloseQuery must check busy state, so that the application cannot be closed while busy by Alt+F4 and X button.
  10454. // So we clear the flag here.
  10455. DebugAssert(NonVisualDataModule->Busy);
  10456. NonVisualDataModule->EndBusy();
  10457. try
  10458. {
  10459. Close();
  10460. }
  10461. __finally
  10462. {
  10463. NonVisualDataModule->StartBusy();
  10464. }
  10465. }
  10466. //---------------------------------------------------------------------------
  10467. bool __fastcall TCustomScpExplorerForm::IsActiveTerminal(TTerminal * Terminal)
  10468. {
  10469. return (Terminal != NULL) && Terminal->Active;
  10470. }
  10471. //---------------------------------------------------------------------------
  10472. bool __fastcall TCustomScpExplorerForm::HasManagedSession()
  10473. {
  10474. return (ManagedSession != NULL);
  10475. }
  10476. //---------------------------------------------------------------------------
  10477. bool __fastcall TCustomScpExplorerForm::HasActiveTerminal()
  10478. {
  10479. return IsActiveTerminal(Terminal);
  10480. }
  10481. //---------------------------------------------------------------------------
  10482. void TCustomScpExplorerForm::LocalLocalCopy(
  10483. TFileOperation, TOperationSide, bool DebugUsedArg(OnFocused), bool DebugUsedArg(NoConfirmation),
  10484. bool DebugUsedArg(DragDrop), unsigned int DebugUsedArg(Flags))
  10485. {
  10486. DebugFail();
  10487. }
  10488. //---------------------------------------------------------------------------
  10489. void TCustomScpExplorerForm::LocalLocalCopyCommand(TFileOperation Operation, TOperationSide Side, bool OnFocused, unsigned int Flags)
  10490. {
  10491. LocalLocalCopy(Operation, Side, OnFocused, !WinConfiguration->ConfirmTransferring, false, Flags);
  10492. }